1148496Simura/* $FreeBSD$ */
2148496Simura
387866Ssheldonh#include <sys/param.h>
487866Ssheldonh#include <sys/time.h>
5148496Simura#include <grp.h>
6148496Simura#include <pwd.h>
787866Ssheldonh#include <stdio.h>
8148496Simura#include <stdlib.h>
987866Ssheldonh#include <string.h>
1087866Ssheldonh#include <unistd.h>
1188282Ssheldonh#ifdef APPLE
1288282Ssheldonh#include <err.h>
1388282Ssheldonh#include <sysexits.h>
1488282Ssheldonh#endif
1587866Ssheldonh
1687866Ssheldonh#include <netsmb/smb_lib.h>
1787866Ssheldonh#include <netsmb/smb_conn.h>
1887866Ssheldonh
1987866Ssheldonh#include "common.h"
2087866Ssheldonh
2187866Ssheldonh#define	DEFBIT(bit)	{bit, #bit}
2287866Ssheldonh
2387866Ssheldonhstatic struct smb_bitname conn_caps[] = {
2487866Ssheldonh	DEFBIT(SMB_CAP_RAW_MODE),
2587866Ssheldonh	DEFBIT(SMB_CAP_MPX_MODE),
2687866Ssheldonh	DEFBIT(SMB_CAP_UNICODE),
2787866Ssheldonh	DEFBIT(SMB_CAP_LARGE_FILES),
2887866Ssheldonh	DEFBIT(SMB_CAP_NT_SMBS),
2987866Ssheldonh	DEFBIT(SMB_CAP_NT_FIND),
3087866Ssheldonh	DEFBIT(SMB_CAP_EXT_SECURITY),
3187866Ssheldonh	{0, NULL}
3287866Ssheldonh};
3387866Ssheldonh
3487866Ssheldonhstatic struct smb_bitname vc_flags[] = {
3587866Ssheldonh	DEFBIT(SMBV_PERMANENT),
3687866Ssheldonh	{SMBV_PRIVATE,	"private"},
3787866Ssheldonh	{SMBV_SINGLESHARE, "singleshare"},
3887866Ssheldonh	{SMBV_ENCRYPT,	"encpwd"},
3987866Ssheldonh	{SMBV_WIN95,	"win95"},
4087866Ssheldonh	{SMBV_LONGNAMES,"longnames"},
4187866Ssheldonh	{0, NULL}
4287866Ssheldonh};
4387866Ssheldonh
4487866Ssheldonhstatic struct smb_bitname ss_flags[] = {
4587866Ssheldonh	DEFBIT(SMBS_PERMANENT),
4687866Ssheldonh	{0, NULL}
4787866Ssheldonh};
4887866Ssheldonh
4987866Ssheldonhstatic char *conn_proto[] = {
5087866Ssheldonh	"unknown",
5187866Ssheldonh	"PC NETWORK PROGRAM 1.0, PCLAN1.0",
5287866Ssheldonh	"MICROSOFT NETWORKS 1.03",
5387866Ssheldonh	"MICROSOFT NETWORKS 3.0, LANMAN1.0",
5487866Ssheldonh	"LM1.2X002, DOS LM1.2X002",
5587866Ssheldonh	"DOS LANMAN2.1, LANMAN2.1",
5687866Ssheldonh	"NT LM 0.12, Windows for Workgroups 3.1a, NT LANMAN 1.0"
5787866Ssheldonh};
5887866Ssheldonh
5987866Ssheldonhstatic char *iod_state[] = {
6087866Ssheldonh	"Not connected",
6187866Ssheldonh	"Reconnecting",
6287866Ssheldonh	"Transport activated",
6387866Ssheldonh	"Session active",
6487866Ssheldonh	"Session dead"
6587866Ssheldonh};
6687866Ssheldonh
6787866Ssheldonhstatic void
6887866Ssheldonhprint_vcinfo(struct smb_vc_info *vip)
6987866Ssheldonh{
7087866Ssheldonh	char buf[200];
7187866Ssheldonh
7287866Ssheldonh	printf("VC: \\\\%s\\%s\n", vip->srvname, vip->vcname);
7387866Ssheldonh	printf("(%s:%s) %o", user_from_uid(vip->uid, 0),
7487866Ssheldonh	    group_from_gid(vip->gid, 0), vip->mode);
7587866Ssheldonh	printf("\n");
7687866Ssheldonh	if (!verbose)
7787866Ssheldonh		return;
7887866Ssheldonh	iprintf(4, "state:    %s\n", iod_state[vip->iodstate]);
7987866Ssheldonh	iprintf(4, "flags:    0x%04x %s\n", vip->flags,
8087866Ssheldonh	    smb_printb(buf, vip->flags, vc_flags));
8187866Ssheldonh	iprintf(4, "usecount: %d\n", vip->usecount);
8287866Ssheldonh	iprintf(4, "dialect:  %d (%s)\n", vip->sopt.sv_proto, conn_proto[vip->sopt.sv_proto]);
8387866Ssheldonh	iprintf(4, "smode:    %d\n", vip->sopt.sv_sm);
8487866Ssheldonh	iprintf(4, "caps:     0x%04x %s\n", vip->sopt.sv_caps,
8587866Ssheldonh	    smb_printb(buf, vip->sopt.sv_caps, conn_caps));
8687866Ssheldonh	iprintf(4, "maxmux:   %d\n", vip->sopt.sv_maxmux);
8787866Ssheldonh	iprintf(4, "maxvcs:   %d\n", vip->sopt.sv_maxvcs);
8887866Ssheldonh}
8987866Ssheldonh
9087866Ssheldonhstatic void
9187866Ssheldonhprint_shareinfo(struct smb_share_info *sip)
9287866Ssheldonh{
9387866Ssheldonh	char buf[200];
9487866Ssheldonh
9587866Ssheldonh	iprintf(4, "Share:    %s", sip->sname);
9687866Ssheldonh	printf("(%s:%s) %o", user_from_uid(sip->uid, 0),
9787866Ssheldonh	    group_from_gid(sip->gid, 0), sip->mode);
9887866Ssheldonh	printf("\n");
9987866Ssheldonh	if (!verbose)
10087866Ssheldonh		return;
10187866Ssheldonh	iprintf(8, "flags:    0x%04x %s\n", sip->flags,
10287866Ssheldonh	    smb_printb(buf, sip->flags, ss_flags));
10387866Ssheldonh	iprintf(8, "usecount: %d\n", sip->usecount);
10487866Ssheldonh}
10587866Ssheldonh
10687866Ssheldonhint
10787866Ssheldonhcmd_dumptree(int argc, char *argv[])
10887866Ssheldonh{
10987866Ssheldonh	void *p, *op;
11087866Ssheldonh	int *itype;
11187866Ssheldonh
11287866Ssheldonh	printf("SMB connections:\n");
11388282Ssheldonh#ifdef APPLE
11488282Ssheldonh	if (loadsmbvfs())
11588282Ssheldonh		errx(EX_OSERR, "SMB filesystem is not available");
11688282Ssheldonh#endif
11787866Ssheldonh	p = smb_dumptree();
11887866Ssheldonh	if (p == NULL) {
11987866Ssheldonh		printf("None\n");
12087866Ssheldonh		return 0;
12187866Ssheldonh	}
12287866Ssheldonh	op = p;
12387866Ssheldonh	for (;;) {
12487866Ssheldonh		itype = p;
12587866Ssheldonh		if (*itype == SMB_INFO_NONE)
12687866Ssheldonh			break;
12787866Ssheldonh		switch (*itype) {
12887866Ssheldonh		    case SMB_INFO_VC:
12987866Ssheldonh			print_vcinfo(p);
13087866Ssheldonh			p = (struct smb_vc_info*)p + 1;
13187866Ssheldonh			break;
13287866Ssheldonh		    case SMB_INFO_SHARE:
13387866Ssheldonh			print_shareinfo(p);
13487866Ssheldonh			p = (struct smb_share_info*)p + 1;
13587866Ssheldonh			break;
13687866Ssheldonh		    default:
13787866Ssheldonh			printf("Out of sync\n");
13887866Ssheldonh			free(op);
13987866Ssheldonh			return 1;
14087866Ssheldonh
14187866Ssheldonh		}
14287866Ssheldonh	}
14387866Ssheldonh	free(op);
14487866Ssheldonh	printf("\n");
14587866Ssheldonh	return 0;
14687866Ssheldonh}
147