Merge from vendor branch ZLIB:
[dragonfly.git] / contrib / smbfs / smbutil / dumptree.c
1 #include <sys/param.h>
2 #include <sys/time.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #ifdef APPLE
8 #include <err.h>
9 #include <sysexits.h>
10 #endif
11
12 #include <netsmb/smb_lib.h>
13 #include <netsmb/smb_conn.h>
14
15 #include "common.h"
16
17 #define DEFBIT(bit)     {bit, #bit}
18
19 static struct smb_bitname conn_caps[] = {
20         DEFBIT(SMB_CAP_RAW_MODE),
21         DEFBIT(SMB_CAP_MPX_MODE),
22         DEFBIT(SMB_CAP_UNICODE),
23         DEFBIT(SMB_CAP_LARGE_FILES),
24         DEFBIT(SMB_CAP_NT_SMBS),
25         DEFBIT(SMB_CAP_NT_FIND),
26         DEFBIT(SMB_CAP_EXT_SECURITY),
27         {0, NULL}
28 };
29
30 static struct smb_bitname vc_flags[] = {
31         DEFBIT(SMBV_PERMANENT),
32         {SMBV_PRIVATE,  "private"},
33         {SMBV_SINGLESHARE, "singleshare"},
34         {SMBV_ENCRYPT,  "encpwd"},
35         {SMBV_WIN95,    "win95"},
36         {SMBV_LONGNAMES,"longnames"},
37         {0, NULL}
38 };
39
40 static struct smb_bitname ss_flags[] = {
41         DEFBIT(SMBS_PERMANENT),
42         {0, NULL}
43 };
44
45 static char *conn_proto[] = {
46         "unknown",
47         "PC NETWORK PROGRAM 1.0, PCLAN1.0",
48         "MICROSOFT NETWORKS 1.03",
49         "MICROSOFT NETWORKS 3.0, LANMAN1.0",
50         "LM1.2X002, DOS LM1.2X002",
51         "DOS LANMAN2.1, LANMAN2.1",
52         "NT LM 0.12, Windows for Workgroups 3.1a, NT LANMAN 1.0"
53 };
54
55 static char *iod_state[] = {
56         "Not connected",
57         "Reconnecting",
58         "Transport activated",
59         "Session active",
60         "Session dead"
61 };
62
63 static void
64 print_vcinfo(struct smb_vc_info *vip)
65 {
66         char buf[200];
67
68         printf("VC: \\\\%s\\%s\n", vip->srvname, vip->vcname);
69         printf("(%s:%s) %o", user_from_uid(vip->uid, 0), 
70             group_from_gid(vip->gid, 0), vip->mode);
71         printf("\n");
72         if (!verbose)
73                 return;
74         iprintf(4, "state:    %s\n", iod_state[vip->iodstate]);
75         iprintf(4, "flags:    0x%04x %s\n", vip->flags,
76             smb_printb(buf, vip->flags, vc_flags));
77         iprintf(4, "usecount: %d\n", vip->usecount);
78         iprintf(4, "dialect:  %d (%s)\n", vip->sopt.sv_proto, conn_proto[vip->sopt.sv_proto]);
79         iprintf(4, "smode:    %d\n", vip->sopt.sv_sm);
80         iprintf(4, "caps:     0x%04x %s\n", vip->sopt.sv_caps,
81             smb_printb(buf, vip->sopt.sv_caps, conn_caps));
82         iprintf(4, "maxmux:   %d\n", vip->sopt.sv_maxmux);
83         iprintf(4, "maxvcs:   %d\n", vip->sopt.sv_maxvcs);
84 }
85
86 static void
87 print_shareinfo(struct smb_share_info *sip)
88 {
89         char buf[200];
90
91         iprintf(4, "Share:    %s", sip->sname);
92         printf("(%s:%s) %o", user_from_uid(sip->uid, 0), 
93             group_from_gid(sip->gid, 0), sip->mode);
94         printf("\n");
95         if (!verbose)
96                 return;
97         iprintf(8, "flags:    0x%04x %s\n", sip->flags,
98             smb_printb(buf, sip->flags, ss_flags));
99         iprintf(8, "usecount: %d\n", sip->usecount);
100 }
101
102 int
103 cmd_dumptree(int argc, char *argv[])
104 {
105         void *p, *op;
106         int *itype;
107
108         printf("SMB connections:\n");
109 #ifdef APPLE
110         if (loadsmbvfs())
111                 errx(EX_OSERR, "SMB filesystem is not available");
112 #endif
113         p = smb_dumptree();
114         if (p == NULL) {
115                 printf("None\n");
116                 return 0;
117         }
118         op = p;
119         for (;;) {
120                 itype = p;
121                 if (*itype == SMB_INFO_NONE)
122                         break;
123                 switch (*itype) {
124                     case SMB_INFO_VC:
125                         print_vcinfo(p);
126                         p = (struct smb_vc_info*)p + 1;
127                         break;
128                     case SMB_INFO_SHARE:
129                         print_shareinfo(p);
130                         p = (struct smb_share_info*)p + 1;
131                         break;
132                     default:
133                         printf("Out of sync\n");
134                         free(op);
135                         return 1;
136                     
137                 }
138         }
139         free(op);
140         printf("\n");
141         return 0;
142 }