fs.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <auth.h>
  12. #include <fcall.h>
  13. #include <thread.h>
  14. #include <9p.h>
  15. #include "cifs.h"
  16. static char *period(int32_t sec);
  17. int
  18. shareinfo(Fmt *f)
  19. {
  20. int i, j, n;
  21. char *type;
  22. Shareinfo2 si2;
  23. Share *sp, *sip;
  24. if((n = RAPshareenum(Sess, &Ipc, &sip)) < 1){
  25. fmtprint(f, "can't enumerate shares: %r\n");
  26. return 0;
  27. }
  28. for(i = 0; i < n; i++){
  29. fmtprint(f, "%-13q ", sip[i].name);
  30. sp = &sip[i];
  31. for(j = 0; j < Nshares; j++)
  32. if(strcmp(Shares[j].name, sip[i].name) == 0){
  33. sp = &Shares[j];
  34. break;
  35. }
  36. if(j >= Nshares)
  37. sp->tid = Ipc.tid;
  38. if(RAPshareinfo(Sess, sp, sp->name, &si2) != -1){
  39. switch(si2.type){
  40. case STYPE_DISKTREE: type = "disk"; break;
  41. case STYPE_PRINTQ: type = "printq"; break;
  42. case STYPE_DEVICE: type = "device"; break;
  43. case STYPE_IPC: type = "ipc"; break;
  44. case STYPE_SPECIAL: type = "special"; break;
  45. case STYPE_TEMP: type = "temp"; break;
  46. default: type = "unknown"; break;
  47. }
  48. fmtprint(f, "%-8s %5d/%-5d %s", type,
  49. si2.activeusrs, si2.maxusrs, si2.comment);
  50. free(si2.name);
  51. free(si2.comment);
  52. free(si2.path);
  53. free(si2.passwd);
  54. }
  55. fmtprint(f, "\n");
  56. }
  57. free(sip);
  58. return 0;
  59. }
  60. int
  61. openfileinfo(Fmt *f)
  62. {
  63. int got, i;
  64. Fileinfo *fi;
  65. fi = nil;
  66. if((got = RAPFileenum2(Sess, &Ipc, "", "", &fi)) == -1){
  67. fmtprint(f, "RAPfileenum: %r\n");
  68. return 0;
  69. }
  70. for(i = 0; i < got; i++){
  71. fmtprint(f, "%c%c%c %-4d %-24q %q ",
  72. (fi[i].perms & 1)? 'r': '-',
  73. (fi[i].perms & 2)? 'w': '-',
  74. (fi[i].perms & 4)? 'c': '-',
  75. fi[i].locks, fi[i].user, fi[i].path);
  76. free(fi[i].path);
  77. free(fi[i].user);
  78. }
  79. free(fi);
  80. return 0;
  81. }
  82. int
  83. conninfo(Fmt *f)
  84. {
  85. int i;
  86. typedef struct {
  87. int val;
  88. char *name;
  89. } Tab;
  90. static Tab captab[] = {
  91. { 1, "raw-mode" },
  92. { 2, "mpx-mode" },
  93. { 4, "unicode" },
  94. { 8, "large-files" },
  95. { 0x10, "NT-smbs" },
  96. { 0x20, "rpc-remote-APIs" },
  97. { 0x40, "status32" },
  98. { 0x80, "l2-oplocks" },
  99. { 0x100, "lock-read" },
  100. { 0x200, "NT-find" },
  101. { 0x1000, "Dfs" },
  102. { 0x2000, "info-passthru" },
  103. { 0x4000, "large-readx" },
  104. { 0x8000, "large-writex" },
  105. { 0x800000, "Unix" },
  106. { 0x20000000, "bulk-transfer" },
  107. { 0x40000000, "compressed" },
  108. { 0x80000000, "extended-security" },
  109. };
  110. static Tab sectab[] = {
  111. { 1, "user-auth" },
  112. { 2, "challange-response" },
  113. { 4, "signing-available" },
  114. { 8, "signing-required" },
  115. };
  116. fmtprint(f, "%q %q %q %q %+ldsec %dmtu %s\n",
  117. Sess->auth->user, Sess->cname,
  118. Sess->auth->windom, Sess->remos,
  119. Sess->slip, Sess->mtu, Sess->isguest? "as guest": "");
  120. fmtprint(f, "caps: ");
  121. for(i = 0; i < nelem(captab); i++)
  122. if(Sess->caps & captab[i].val)
  123. fmtprint(f, "%s ", captab[i].name);
  124. fmtprint(f, "\n");
  125. fmtprint(f, "security: ");
  126. for(i = 0; i < nelem(sectab); i++)
  127. if(Sess->secmode & sectab[i].val)
  128. fmtprint(f, "%s ", sectab[i].name);
  129. fmtprint(f, "\n");
  130. if(Sess->nbt)
  131. fmtprint(f, "transport: cifs over netbios\n");
  132. else
  133. fmtprint(f, "transport: cifs\n");
  134. return 0;
  135. }
  136. int
  137. sessioninfo(Fmt *f)
  138. {
  139. int got, i;
  140. Sessinfo *si;
  141. si = nil;
  142. if((got = RAPsessionenum(Sess, &Ipc, &si)) == -1){
  143. fmtprint(f, "RAPsessionenum: %r\n");
  144. return 0;
  145. }
  146. for(i = 0; i < got; i++){
  147. fmtprint(f, "%-24q %-24q ", si[i].user, si[i].wrkstn);
  148. fmtprint(f, "%12s ", period(si[i].sesstime));
  149. fmtprint(f, "%12s\n", period(si[i].idletime));
  150. free(si[i].wrkstn);
  151. free(si[i].user);
  152. }
  153. free(si);
  154. return 0;
  155. }
  156. /*
  157. * We request the domain referral for "" which gives the
  158. * list of all the trusted domains in the clients forest, and
  159. * other trusted forests.
  160. *
  161. * We then sumbit each of these names in turn which gives the
  162. * names of the domain controllers for that domain.
  163. *
  164. * We get a DNS domain name for each domain controller as well as a
  165. * netbios name. I THINK I am correct in saying that a name
  166. * containing a dot ('.') must be a DNS name, as the NetBios
  167. * name munging cannot encode one. Thus names which contain no
  168. * dots must be netbios names.
  169. *
  170. */
  171. static void
  172. dfsredir(Fmt *f, char *path, int depth)
  173. {
  174. Refer *re, retab[128];
  175. int n, used, flags;
  176. n = T2getdfsreferral(Sess, &Ipc, path, &flags, &used, retab, nelem(retab));
  177. if(n == -1)
  178. return;
  179. for(re = retab; re < retab+n; re++){
  180. if(strcmp(path, re->path) != 0)
  181. dfsredir(f, re->path, depth+1);
  182. else
  183. fmtprint(f, "%-32q %q\n", re->path, re->addr);
  184. free(re->addr);
  185. free(re->path);
  186. }
  187. }
  188. int
  189. dfsrootinfo(Fmt *f)
  190. {
  191. dfsredir(f, "", 0);
  192. return 0;
  193. }
  194. int
  195. userinfo(Fmt *f)
  196. {
  197. int got, i;
  198. Namelist *nl;
  199. Userinfo ui;
  200. nl = nil;
  201. if((got = RAPuserenum2(Sess, &Ipc, &nl)) == -1)
  202. if((got = RAPuserenum(Sess, &Ipc, &nl)) == -1){
  203. fmtprint(f, "RAPuserenum: %r\n");
  204. return 0;
  205. }
  206. for(i = 0; i < got; i++){
  207. fmtprint(f, "%-24q ", nl[i].name);
  208. if(RAPuserinfo(Sess, &Ipc, nl[i].name, &ui) != -1){
  209. fmtprint(f, "%-48q %q", ui.fullname, ui.comment);
  210. free(ui.user);
  211. free(ui.comment);
  212. free(ui.fullname);
  213. free(ui.user_comment);
  214. }
  215. free(nl[i].name);
  216. fmtprint(f, "\n");
  217. }
  218. free(nl);
  219. return 0;
  220. }
  221. int
  222. groupinfo(Fmt *f)
  223. {
  224. int got1, got2, i, j;
  225. Namelist *grps, *usrs;
  226. grps = nil;
  227. if((got1 = RAPgroupenum(Sess, &Ipc, &grps)) == -1){
  228. fmtprint(f, "RAPgroupenum: %r\n");
  229. return 0;
  230. }
  231. for(i = 0; i < got1; i++){
  232. fmtprint(f, "%q ", grps[i].name);
  233. usrs = nil;
  234. if((got2 = RAPgroupusers(Sess, &Ipc, grps[i].name, &usrs)) != -1){
  235. for(j = 0; j < got2; j++){
  236. fmtprint(f, "%q ", usrs[j].name);
  237. free(usrs[j].name);
  238. }
  239. free(usrs);
  240. }
  241. free(grps[i].name);
  242. fmtprint(f, "\n");
  243. }
  244. free(grps);
  245. return 0;
  246. }
  247. static int
  248. nodelist(Fmt *f, int type)
  249. {
  250. int more, got, i, j;
  251. Serverinfo *si;
  252. static char *types[] = {
  253. [0] = "workstation",
  254. [1] = "server",
  255. [2] = "SQL server",
  256. [3] = "DC",
  257. [4] = "backup DC",
  258. [5] = "time source",
  259. [6] = "Apple server",
  260. [7] = "Novell server",
  261. [8] = "domain member",
  262. [9] = "printer server",
  263. [10] = "dial-up server",
  264. [11] = "Unix",
  265. [12] = "NT",
  266. [13] = "WFW",
  267. [14] = "MFPN (?)",
  268. [15] = "NT server",
  269. [16] = "potential browser",
  270. [17] = "backup browser",
  271. [18] = "LMB",
  272. [19] = "DMB",
  273. [20] = "OSF Unix",
  274. [21] = "VMS",
  275. [22] = "Win95",
  276. [23] = "DFS",
  277. [24] = "NT cluster",
  278. [25] = "Terminal server",
  279. [26] = "[26] =",
  280. [27] = "[27] =",
  281. [28] = "IBM DSS",
  282. };
  283. si = nil;
  284. if((got = RAPServerenum2(Sess, &Ipc, Sess->auth->windom, type, &more,
  285. &si)) == -1){
  286. fmtprint(f, "RAPServerenum2: %r\n");
  287. return 0;
  288. }
  289. if(more)
  290. if((got = RAPServerenum3(Sess, &Ipc, Sess->auth->windom, type,
  291. got-1, si)) == -1){
  292. fmtprint(f, "RAPServerenum3: %r\n");
  293. return 0;
  294. }
  295. for(i = 0; i < got; i++){
  296. fmtprint(f, "%-16q %-16q ", si[i].name, si[i].comment);
  297. if(type != LIST_DOMAINS_ONLY){
  298. fmtprint(f, "v%d.%d ", si[i].major, si[i].minor);
  299. for(j = 0; j < nelem(types); j++)
  300. if(si[i].type & (1 << j) && types[j])
  301. fmtprint(f, "%s,", types[j]);
  302. }
  303. fmtprint(f, "\n");
  304. free(si[i].name);
  305. free(si[i].comment);
  306. }
  307. free(si);
  308. return 0;
  309. }
  310. int
  311. domaininfo(Fmt *f)
  312. {
  313. return nodelist(f, LIST_DOMAINS_ONLY);
  314. }
  315. int
  316. workstationinfo(Fmt *f)
  317. {
  318. return nodelist(f, ALL_LEARNT_IN_DOMAIN);
  319. }
  320. static char *
  321. period(int32_t sec)
  322. {
  323. int days, hrs, min;
  324. static char when[32];
  325. days = sec / (60L * 60L * 24L);
  326. sec -= days * (60L * 60L * 24L);
  327. hrs = sec / (60L * 60L);
  328. sec -= hrs * (60L * 60L);
  329. min = sec / 60L;
  330. sec -= min * 60L;
  331. if(days)
  332. snprint(when, sizeof(when), "%d %d:%d:%ld ", days, hrs, min, sec);
  333. else
  334. snprint(when, sizeof(when), "%d:%d:%ld ", hrs, min, sec);
  335. return when;
  336. }