main.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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 <fcall.h>
  12. #include <thread.h>
  13. #include <libsec.h>
  14. #include <9p.h>
  15. #include "cifs.h"
  16. #define max(a,b) (((a) > (b))? (a): (b))
  17. #define min(a,b) (((a) < (b))? (a): (b))
  18. typedef struct Aux Aux;
  19. struct Aux {
  20. Aux *next;
  21. Aux *prev;
  22. char *path; /* full path fo file */
  23. Share *sp; /* this share's info */
  24. int32_t expire; /* expiration time of cache */
  25. int32_t off; /* file pos of start of cache */
  26. int32_t end; /* file pos of end of cache */
  27. char *cache;
  28. int fh; /* file handle */
  29. int sh; /* search handle */
  30. int32_t srch; /* find first's internal state */
  31. };
  32. extern int chatty9p;
  33. int Checkcase = 1; /* enforce case significance on filenames */
  34. int Dfstout = 100; /* timeout (in ms) for ping of dfs servers (assume they are local) */
  35. int Billtrog = 1; /* enable file owner/group resolution */
  36. int Attachpid; /* pid of proc that attaches (ugh !) */
  37. char *Debug = nil; /* messages */
  38. Qid Root; /* root of remote system */
  39. Share Ipc; /* Share info of IPC$ share */
  40. Session *Sess; /* current session */
  41. int Active = IDLE_TIME; /* secs until next keepalive is sent */
  42. static int Keeppid; /* process ID of keepalive thread */
  43. Share Shares[MAX_SHARES]; /* table of connected shares */
  44. int Nshares = 0; /* number of Shares connected */
  45. Aux *Auxroot = nil; /* linked list of Aux structs */
  46. char *Host = nil; /* host we are connected to */
  47. static char *Ipcname = "IPC$";
  48. #define ptype(x) (((x) & 0xf))
  49. #define pindex(x) (((x) & 0xff0) >> 4)
  50. void
  51. setup(void)
  52. {
  53. int fd;
  54. char buf[32];
  55. /*
  56. * This is revolting but I cannot see any other way to get
  57. * the pid of the server. We need this as Windows doesn't
  58. * drop the TCP connection when it closes a connection.
  59. * Thus we keepalive() to detect when/if we are thrown off.
  60. */
  61. Attachpid = getpid();
  62. snprint(buf, sizeof buf, "#p/%d/args", getpid());
  63. if((fd = open(buf, OWRITE)) >= 0){
  64. fprint(fd, "%s network", Host);
  65. close(fd);
  66. }
  67. }
  68. int
  69. filetableinfo(Fmt *f)
  70. {
  71. Aux *ap;
  72. char *type;
  73. if((ap = Auxroot) != nil)
  74. do{
  75. type = "walked";
  76. if(ap->sh != -1)
  77. type = "opendir";
  78. if(ap->fh != -1)
  79. type = "openfile";
  80. fmtprint(f, "%-9s %s\n", type, ap->path);
  81. ap = ap->next;
  82. }while(ap != Auxroot);
  83. return 0;
  84. }
  85. Qid
  86. mkqid(char *s, int is_dir, int32_t vers, int subtype, int32_t path)
  87. {
  88. Qid q;
  89. union { /* align digest suitably */
  90. uint8_t digest[SHA1dlen];
  91. uint64_t uvl;
  92. } u;
  93. sha1((uint8_t *)s, strlen(s), u.digest, nil);
  94. q.type = (is_dir)? QTDIR: 0;
  95. q.vers = vers;
  96. if(subtype){
  97. q.path = *((uint64_t *)u.digest) & ~0xfffL;
  98. q.path |= ((path & 0xff) << 4);
  99. q.path |= (subtype & 0xf);
  100. }
  101. else
  102. q.path = *((uint64_t *)u.digest) & ~0xfL;
  103. return q;
  104. }
  105. /*
  106. * used only for root dir and shares
  107. */
  108. static void
  109. V2D(Dir *d, Qid qid, char *name)
  110. {
  111. memset(d, 0, sizeof(Dir));
  112. d->type = 'C';
  113. d->dev = 1;
  114. d->name = estrdup9p(name);
  115. d->uid = estrdup9p("bill");
  116. d->muid = estrdup9p("boyd");
  117. d->gid = estrdup9p("trog");
  118. d->mode = 0755 | DMDIR;
  119. d->atime = time(nil);
  120. d->mtime = d->atime;
  121. d->length = 0;
  122. d->qid = qid;
  123. }
  124. static void
  125. I2D(Dir *d, Share *sp, char *path, FInfo *fi)
  126. {
  127. char *name;
  128. if((name = strrchr(fi->name, '\\')) != nil)
  129. name++;
  130. else
  131. name = fi->name;
  132. d->name = estrdup9p(name);
  133. d->type = 'C';
  134. d->dev = sp->tid;
  135. d->uid = estrdup9p("bill");
  136. d->gid = estrdup9p("trog");
  137. d->muid = estrdup9p("boyd");
  138. d->atime = fi->accessed;
  139. d->mtime = fi->written;
  140. if(fi->attribs & ATTR_READONLY)
  141. d->mode = 0444;
  142. else
  143. d->mode = 0666;
  144. d->length = fi->size;
  145. d->qid = mkqid(path, fi->attribs & ATTR_DIRECTORY, fi->changed, 0, 0);
  146. if(fi->attribs & ATTR_DIRECTORY){
  147. d->length = 0;
  148. d->mode |= DMDIR|0111;
  149. }
  150. }
  151. static void
  152. responderrstr(Req *r)
  153. {
  154. char e[ERRMAX];
  155. *e = 0;
  156. rerrstr(e, sizeof e);
  157. respond(r, e);
  158. }
  159. static char *
  160. newpath(char *path, char *name)
  161. {
  162. char *p, *q;
  163. assert((p = strrchr(path, '/')) != nil);
  164. if(strcmp(name, "..") == 0){
  165. if(p == path)
  166. return estrdup9p("/");
  167. q = emalloc9p((p-path)+1);
  168. strecpy(q, q+(p-path)+1, path);
  169. return q;
  170. }
  171. if(strcmp(path, "/") == 0)
  172. return smprint("/%s", name);
  173. return smprint("%s/%s", path, name);
  174. }
  175. static int
  176. dirgen(int slot, Dir *d, void *aux)
  177. {
  178. int32_t off;
  179. FInfo *fi;
  180. int rc, got;
  181. Aux *a = aux;
  182. char *npath;
  183. int numinf = numinfo();
  184. int slots = min(Sess->mtu, MTU) / sizeof(FInfo);
  185. if(strcmp(a->path, "/") == 0){
  186. if(slot < numinf){
  187. dirgeninfo(slot, d);
  188. return 0;
  189. } else
  190. slot -= numinf;
  191. if(slot >= Nshares)
  192. return -1;
  193. V2D(d, mkqid(Shares[slot].name, 1, 1, Pshare, slot),
  194. Shares[slot].name);
  195. return 0;
  196. }
  197. off = slot * sizeof(FInfo);
  198. if(off >= a->off && off < a->end && time(nil) < a->expire)
  199. goto from_cache;
  200. if(off == 0){
  201. fi = (FInfo *)a->cache;
  202. npath = smprint("%s/*", mapfile(a->path));
  203. a->sh = T2findfirst(Sess, a->sp, slots, npath, &got, &a->srch,
  204. (FInfo *)a->cache);
  205. free(npath);
  206. if(a->sh == -1)
  207. return -1;
  208. a->off = 0;
  209. a->end = got * sizeof(FInfo);
  210. if(got >= 2 && strcmp(fi[0].name, ".") == 0 &&
  211. strcmp(fi[1].name, "..") == 0){
  212. a->end = (got - 2) * sizeof(FInfo);
  213. memmove(a->cache, a->cache + sizeof(FInfo)*2,
  214. a->end - a->off);
  215. }
  216. }
  217. while(off >= a->end && a->sh != -1){
  218. fi = (FInfo *)(a->cache + (a->end - a->off) - sizeof(FInfo));
  219. a->off = a->end;
  220. npath = smprint("%s/%s", mapfile(a->path), fi->name);
  221. rc = T2findnext(Sess, a->sp, slots, npath,
  222. &got, &a->srch, (FInfo *)a->cache, a->sh);
  223. free(npath);
  224. if(rc == -1 || got == 0)
  225. break;
  226. a->end = a->off + got * sizeof(FInfo);
  227. }
  228. a->expire = time(nil) + CACHETIME;
  229. if(got < slots){
  230. if(a->sh != -1)
  231. CIFSfindclose2(Sess, a->sp, a->sh);
  232. a->sh = -1;
  233. }
  234. if(off >= a->end)
  235. return -1;
  236. from_cache:
  237. fi = (FInfo *)(a->cache + (off - a->off));
  238. npath = smprint("%s/%s", mapfile(a->path), fi->name);
  239. I2D(d, a->sp, npath, fi);
  240. if(Billtrog == 0)
  241. upd_names(Sess, a->sp, npath, d);
  242. free(npath);
  243. return 0;
  244. }
  245. static void
  246. fsattach(Req *r)
  247. {
  248. Aux *a;
  249. static int first = 1;
  250. char *spec = r->ifcall.aname;
  251. if(first)
  252. setup();
  253. if(spec && *spec){
  254. respond(r, "invalid attach specifier");
  255. return;
  256. }
  257. r->ofcall.qid = mkqid("/", 1, 1, Proot, 0);
  258. r->fid->qid = r->ofcall.qid;
  259. a = r->fid->aux = emalloc9p(sizeof(Aux));
  260. memset(a, 0, sizeof(Aux));
  261. a->path = estrdup9p("/");
  262. a->sp = nil;
  263. a->fh = -1;
  264. a->sh = -1;
  265. if(Auxroot){
  266. a->prev = Auxroot;
  267. a->next = Auxroot->next;
  268. Auxroot->next->prev = a;
  269. Auxroot->next = a;
  270. } else {
  271. Auxroot = a;
  272. a->next = a;
  273. a->prev = a;
  274. }
  275. respond(r, nil);
  276. }
  277. static char*
  278. fsclone(Fid *ofid, Fid *fid)
  279. {
  280. Aux *oa = ofid->aux;
  281. Aux *a = emalloc9p(sizeof(Aux));
  282. fid->aux = a;
  283. memset(a, 0, sizeof(Aux));
  284. a->sh = -1;
  285. a->fh = -1;
  286. a->sp = oa->sp;
  287. a->path = estrdup9p(oa->path);
  288. if(Auxroot){
  289. a->prev = Auxroot;
  290. a->next = Auxroot->next;
  291. Auxroot->next->prev = a;
  292. Auxroot->next = a;
  293. } else {
  294. Auxroot = a;
  295. a->next = a;
  296. a->prev = a;
  297. }
  298. return nil;
  299. }
  300. /*
  301. * for some weird reason T2queryall() returns share names
  302. * in lower case so we have to do an extra test against
  303. * our share table to validate filename case.
  304. *
  305. * on top of this here (snell & Wilcox) most of our
  306. * redirections point to a share of the same name,
  307. * but some do not, thus the tail of the filename
  308. * returned by T2queryall() is not the same as
  309. * the name we wanted.
  310. *
  311. * We work around this by not validating the names
  312. * or files which resolve to share names as they must
  313. * be correct, having been enforced in the dfs layer.
  314. */
  315. static int
  316. validfile(char *found, char *want, char *winpath, Share *sp)
  317. {
  318. char *share;
  319. if(strcmp(want, "..") == 0)
  320. return 1;
  321. if(strcmp(winpath, "/") == 0){
  322. share = trimshare(sp->name);
  323. if(cistrcmp(want, share) == 0)
  324. return strcmp(want, share) == 0;
  325. /*
  326. * OK, a DFS redirection points us from a directory XXX
  327. * to a share named YYY. There is no case checking we can
  328. * do so we allow either case - it's all we can do.
  329. */
  330. return 1;
  331. }
  332. if(cistrcmp(found, want) != 0)
  333. return 0;
  334. if(!Checkcase)
  335. return 1;
  336. if(strcmp(found, want) == 0)
  337. return 1;
  338. return 0;
  339. }
  340. static char*
  341. fswalk1(Fid *fid, char *name, Qid *qid)
  342. {
  343. FInfo fi;
  344. int rc, n, i;
  345. Aux *a = fid->aux;
  346. static char e[ERRMAX];
  347. char *p, *npath, *winpath;
  348. *e = 0;
  349. npath = newpath(a->path, name);
  350. if(strcmp(npath, "/") == 0){ /* root dir */
  351. *qid = mkqid("/", 1, 1, Proot, 0);
  352. free(a->path);
  353. a->path = npath;
  354. fid->qid = *qid;
  355. return nil;
  356. }
  357. if(strrchr(npath, '/') == npath){ /* top level dir */
  358. if((n = walkinfo(name)) != -1){ /* info file */
  359. *qid = mkqid(npath, 0, 1, Pinfo, n);
  360. }
  361. else { /* volume name */
  362. for(i = 0; i < Nshares; i++){
  363. n = strlen(Shares[i].name);
  364. if(cistrncmp(npath+1, Shares[i].name, n) != 0)
  365. continue;
  366. if(Checkcase && strncmp(npath+1, Shares[i].name, n) != 0)
  367. continue;
  368. if(npath[n+1] != 0 && npath[n+1] != '/')
  369. continue;
  370. break;
  371. }
  372. if(i >= Nshares){
  373. free(npath);
  374. return "not found";
  375. }
  376. a->sp = Shares+i;
  377. *qid = mkqid(npath, 1, 1, Pshare, i);
  378. }
  379. free(a->path);
  380. a->path = npath;
  381. fid->qid = *qid;
  382. return nil;
  383. }
  384. /* must be a vanilla file or directory */
  385. again:
  386. if(mapshare(npath, &a->sp) == -1){
  387. rerrstr(e, sizeof(e));
  388. free(npath);
  389. return e;
  390. }
  391. winpath = mapfile(npath);
  392. memset(&fi, 0, sizeof fi);
  393. if(Sess->caps & CAP_NT_SMBS)
  394. rc = T2queryall(Sess, a->sp, winpath, &fi);
  395. else
  396. rc = T2querystandard(Sess, a->sp, winpath, &fi);
  397. if(rc == -1){
  398. rerrstr(e, sizeof(e));
  399. free(npath);
  400. return e;
  401. }
  402. if((a->sp->options & SMB_SHARE_IS_IN_DFS) != 0 &&
  403. (fi.attribs & ATTR_REPARSE) != 0){
  404. if(redirect(Sess, a->sp, npath) != -1)
  405. goto again;
  406. }
  407. if((p = strrchr(fi.name, '/')) == nil && (p = strrchr(fi.name, '\\')) == nil)
  408. p = fi.name;
  409. else
  410. p++;
  411. if(! validfile(p, name, winpath, a->sp)){
  412. free(npath);
  413. return "not found";
  414. }
  415. *qid = mkqid(npath, fi.attribs & ATTR_DIRECTORY, fi.changed, 0, 0);
  416. free(a->path);
  417. a->path = npath;
  418. fid->qid = *qid;
  419. return nil;
  420. }
  421. static void
  422. fsstat(Req *r)
  423. {
  424. int rc;
  425. FInfo fi;
  426. Aux *a = r->fid->aux;
  427. if(ptype(r->fid->qid.path) == Proot)
  428. V2D(&r->d, r->fid->qid, "");
  429. else if(ptype(r->fid->qid.path) == Pinfo)
  430. dirgeninfo(pindex(r->fid->qid.path), &r->d);
  431. else if(ptype(r->fid->qid.path) == Pshare)
  432. V2D(&r->d, r->fid->qid, a->path +1);
  433. else{
  434. memset(&fi, 0, sizeof fi);
  435. if(Sess->caps & CAP_NT_SMBS)
  436. rc = T2queryall(Sess, a->sp, mapfile(a->path), &fi);
  437. else
  438. rc = T2querystandard(Sess, a->sp, mapfile(a->path), &fi);
  439. if(rc == -1){
  440. responderrstr(r);
  441. return;
  442. }
  443. I2D(&r->d, a->sp, a->path, &fi);
  444. if(Billtrog == 0)
  445. upd_names(Sess, a->sp, mapfile(a->path), &r->d);
  446. }
  447. respond(r, nil);
  448. }
  449. static int
  450. smbcreateopen(Aux *a, char *path, int mode, int perm, int is_create,
  451. int is_dir, FInfo *fip)
  452. {
  453. int rc, action, attrs, access, result;
  454. if(is_create && is_dir){
  455. if(CIFScreatedirectory(Sess, a->sp, path) == -1)
  456. return -1;
  457. return 0;
  458. }
  459. if(mode & DMAPPEND) {
  460. werrstr("filesystem does not support DMAPPEND");
  461. return -1;
  462. }
  463. if(is_create)
  464. action = 0x12;
  465. else if(mode & OTRUNC)
  466. action = 0x02;
  467. else
  468. action = 0x01;
  469. if(perm & 0222)
  470. attrs = ATTR_NORMAL;
  471. else
  472. attrs = ATTR_NORMAL|ATTR_READONLY;
  473. switch (mode & OMASK){
  474. case OREAD:
  475. access = 0;
  476. break;
  477. case OWRITE:
  478. access = 1;
  479. break;
  480. case ORDWR:
  481. access = 2;
  482. break;
  483. case OEXEC:
  484. access = 3;
  485. break;
  486. default:
  487. werrstr("%d bad open mode", mode & OMASK);
  488. return -1;
  489. break;
  490. }
  491. if(mode & DMEXCL == 0)
  492. access |= 0x10;
  493. else
  494. access |= 0x40;
  495. if((a->fh = CIFS_SMB_opencreate(Sess, a->sp, path, access, attrs,
  496. action, &result)) == -1)
  497. return -1;
  498. if(Sess->caps & CAP_NT_SMBS)
  499. rc = T2queryall(Sess, a->sp, mapfile(a->path), fip);
  500. else
  501. rc = T2querystandard(Sess, a->sp, mapfile(a->path), fip);
  502. if(rc == -1){
  503. fprint(2, "internal error: stat of newly open/created file failed\n");
  504. return -1;
  505. }
  506. if((mode & OEXCL) && (result & 0x8000) == 0){
  507. werrstr("%d bad open mode", mode & OMASK);
  508. return -1;
  509. }
  510. return 0;
  511. }
  512. /* Uncle Bill, you have a lot to answer for... */
  513. static int
  514. ntcreateopen(Aux *a, char *path, int mode, int perm, int is_create,
  515. int is_dir, FInfo *fip)
  516. {
  517. int options, result, attrs, flags, access, action, share;
  518. if(mode & DMAPPEND){
  519. werrstr("CIFSopen, DMAPPEND not supported");
  520. return -1;
  521. }
  522. if(is_create){
  523. if(mode & OEXCL)
  524. action = FILE_OPEN;
  525. else if(mode & OTRUNC)
  526. action = FILE_CREATE;
  527. else
  528. action = FILE_OVERWRITE_IF;
  529. } else {
  530. if(mode & OTRUNC)
  531. action = FILE_OVERWRITE_IF;
  532. else
  533. action = FILE_OPEN_IF;
  534. }
  535. flags = 0; /* FIXME: really not sure */
  536. if(mode & OEXCL)
  537. share = FILE_NO_SHARE;
  538. else
  539. share = FILE_SHARE_ALL;
  540. switch (mode & OMASK){
  541. case OREAD:
  542. access = GENERIC_READ;
  543. break;
  544. case OWRITE:
  545. access = GENERIC_WRITE;
  546. break;
  547. case ORDWR:
  548. access = GENERIC_ALL;
  549. break;
  550. case OEXEC:
  551. access = GENERIC_EXECUTE;
  552. break;
  553. default:
  554. werrstr("%d bad open mode", mode & OMASK);
  555. return -1;
  556. break;
  557. }
  558. if(is_dir){
  559. action = FILE_CREATE;
  560. options = FILE_DIRECTORY_FILE;
  561. if(perm & 0222)
  562. attrs = ATTR_DIRECTORY;
  563. else
  564. attrs = ATTR_DIRECTORY|ATTR_READONLY;
  565. } else {
  566. options = FILE_NON_DIRECTORY_FILE;
  567. if(perm & 0222)
  568. attrs = ATTR_NORMAL;
  569. else
  570. attrs = ATTR_NORMAL|ATTR_READONLY;
  571. }
  572. if(mode & ORCLOSE){
  573. options |= FILE_DELETE_ON_CLOSE;
  574. attrs |= ATTR_DELETE_ON_CLOSE;
  575. }
  576. if((a->fh = CIFS_NT_opencreate(Sess, a->sp, path, flags, options,
  577. attrs, access, share, action, &result, fip)) == -1)
  578. return -1;
  579. if((mode & OEXCL) && (result & 0x8000) == 0){
  580. werrstr("%d bad open mode", mode & OMASK);
  581. return -1;
  582. }
  583. return 0;
  584. }
  585. static void
  586. fscreate(Req *r)
  587. {
  588. FInfo fi;
  589. int rc, is_dir;
  590. char *npath;
  591. Aux *a = r->fid->aux;
  592. a->end = a->off = 0;
  593. a->cache = emalloc9p(max(Sess->mtu, MTU));
  594. is_dir = (r->ifcall.perm & DMDIR) == DMDIR;
  595. npath = smprint("%s/%s", a->path, r->ifcall.name);
  596. if(Sess->caps & CAP_NT_SMBS)
  597. rc = ntcreateopen(a, mapfile(npath), r->ifcall.mode,
  598. r->ifcall.perm, 1, is_dir, &fi);
  599. else
  600. rc = smbcreateopen(a, mapfile(npath), r->ifcall.mode,
  601. r->ifcall.perm, 1, is_dir, &fi);
  602. if(rc == -1){
  603. free(npath);
  604. responderrstr(r);
  605. return;
  606. }
  607. r->fid->qid = mkqid(npath, fi.attribs & ATTR_DIRECTORY, fi.changed, 0, 0);
  608. r->ofcall.qid = r->fid->qid;
  609. free(a->path);
  610. a->path = npath;
  611. respond(r, nil);
  612. }
  613. static void
  614. fsopen(Req *r)
  615. {
  616. int rc;
  617. FInfo fi;
  618. Aux *a = r->fid->aux;
  619. a->end = a->off = 0;
  620. a->cache = emalloc9p(max(Sess->mtu, MTU));
  621. if(ptype(r->fid->qid.path) == Pinfo){
  622. if(makeinfo(pindex(r->fid->qid.path)) != -1)
  623. respond(r, nil);
  624. else
  625. respond(r, "cannot generate info");
  626. return;
  627. }
  628. if(r->fid->qid.type & QTDIR){
  629. respond(r, nil);
  630. return;
  631. }
  632. if(Sess->caps & CAP_NT_SMBS)
  633. rc = ntcreateopen(a, mapfile(a->path), r->ifcall.mode, 0777,
  634. 0, 0, &fi);
  635. else
  636. rc = smbcreateopen(a, mapfile(a->path), r->ifcall.mode, 0777,
  637. 0, 0, &fi);
  638. if(rc == -1){
  639. responderrstr(r);
  640. return;
  641. }
  642. respond(r, nil);
  643. }
  644. static void
  645. fswrite(Req *r)
  646. {
  647. int64_t n, m, got;
  648. Aux *a = r->fid->aux;
  649. int64_t len = r->ifcall.count;
  650. int64_t off = r->ifcall.offset;
  651. char *buf = r->ifcall.data;
  652. got = 0;
  653. n = Sess->mtu -OVERHEAD;
  654. do{
  655. if(len - got < n)
  656. n = len - got;
  657. m = CIFSwrite(Sess, a->sp, a->fh, off + got, buf + got, n);
  658. if(m != -1)
  659. got += m;
  660. } while(got < len && m >= n);
  661. r->ofcall.count = got;
  662. if(m == -1)
  663. responderrstr(r);
  664. else
  665. respond(r, nil);
  666. }
  667. static void
  668. fsread(Req *r)
  669. {
  670. int64_t n, m, got;
  671. Aux *a = r->fid->aux;
  672. char *buf = r->ofcall.data;
  673. int64_t len = r->ifcall.count;
  674. int64_t off = r->ifcall.offset;
  675. if(ptype(r->fid->qid.path) == Pinfo){
  676. r->ofcall.count = readinfo(pindex(r->fid->qid.path), buf, len,
  677. off);
  678. respond(r, nil);
  679. return;
  680. }
  681. if(r->fid->qid.type & QTDIR){
  682. dirread9p(r, dirgen, a);
  683. respond(r, nil);
  684. return;
  685. }
  686. got = 0;
  687. n = Sess->mtu -OVERHEAD;
  688. do{
  689. if(len - got < n)
  690. n = len - got;
  691. m = CIFSread(Sess, a->sp, a->fh, off + got, buf + got, n, len);
  692. if(m != -1)
  693. got += m;
  694. } while(got < len && m >= n);
  695. r->ofcall.count = got;
  696. if(m == -1)
  697. responderrstr(r);
  698. else
  699. respond(r, nil);
  700. }
  701. static void
  702. fsdestroyfid(Fid *f)
  703. {
  704. Aux *a = f->aux;
  705. if(ptype(f->qid.path) == Pinfo)
  706. freeinfo(pindex(f->qid.path));
  707. f->omode = -1;
  708. if(! a)
  709. return;
  710. if(a->fh != -1)
  711. if(CIFSclose(Sess, a->sp, a->fh) == -1)
  712. fprint(2, "%s: close failed fh=%d %r\n", argv0, a->fh);
  713. if(a->sh != -1)
  714. if(CIFSfindclose2(Sess, a->sp, a->sh) == -1)
  715. fprint(2, "%s: findclose failed sh=%d %r\n",
  716. argv0, a->sh);
  717. if(a->path)
  718. free(a->path);
  719. if(a->cache)
  720. free(a->cache);
  721. if(a == Auxroot)
  722. Auxroot = a->next;
  723. a->prev->next = a->next;
  724. a->next->prev = a->prev;
  725. if(a->next == a->prev)
  726. Auxroot = nil;
  727. if(a)
  728. free(a);
  729. }
  730. int
  731. rdonly(Session *s, Share *sp, char *path, int rdonly)
  732. {
  733. int rc;
  734. FInfo fi;
  735. if(Sess->caps & CAP_NT_SMBS)
  736. rc = T2queryall(s, sp, path, &fi);
  737. else
  738. rc = T2querystandard(s, sp, path, &fi);
  739. if(rc == -1)
  740. return -1;
  741. if((rdonly && !(fi.attribs & ATTR_READONLY)) ||
  742. (!rdonly && (fi.attribs & ATTR_READONLY))){
  743. fi.attribs &= ~ATTR_READONLY;
  744. fi.attribs |= rdonly? ATTR_READONLY: 0;
  745. rc = CIFSsetinfo(s, sp, path, &fi);
  746. }
  747. return rc;
  748. }
  749. static void
  750. fsremove(Req *r)
  751. {
  752. int try, rc;
  753. char e[ERRMAX];
  754. Aux *ap, *a = r->fid->aux;
  755. *e = 0;
  756. if(ptype(r->fid->qid.path) == Proot ||
  757. ptype(r->fid->qid.path) == Pshare){
  758. respond(r, "illegal operation");
  759. return;
  760. }
  761. /* close all instences of this file/dir */
  762. if((ap = Auxroot) != nil)
  763. do{
  764. if(strcmp(ap->path, a->path) == 0){
  765. if(ap->sh != -1)
  766. CIFSfindclose2(Sess, ap->sp, ap->sh);
  767. ap->sh = -1;
  768. if(ap->fh != -1)
  769. CIFSclose(Sess, ap->sp, ap->fh);
  770. ap->fh = -1;
  771. }
  772. ap = ap->next;
  773. }while(ap != Auxroot);
  774. try = 0;
  775. again:
  776. if(r->fid->qid.type & QTDIR)
  777. rc = CIFSdeletedirectory(Sess, a->sp, mapfile(a->path));
  778. else
  779. rc = CIFSdeletefile(Sess, a->sp, mapfile(a->path));
  780. rerrstr(e, sizeof(e));
  781. if(rc == -1 && try++ == 0 && strcmp(e, "permission denied") == 0 &&
  782. rdonly(Sess, a->sp, mapfile(a->path), 0) == 0)
  783. goto again;
  784. if(rc == -1)
  785. responderrstr(r);
  786. else
  787. respond(r, nil);
  788. }
  789. static void
  790. fswstat(Req *r)
  791. {
  792. int fh, result, rc;
  793. FInfo fi, tmpfi;
  794. char *p, *from, *npath;
  795. Aux *a = r->fid->aux;
  796. if(ptype(r->fid->qid.path) == Proot ||
  797. ptype(r->fid->qid.path) == Pshare){
  798. respond(r, "illegal operation");
  799. return;
  800. }
  801. if((r->d.uid && r->d.uid[0]) || (r->d.gid && r->d.gid[0])){
  802. respond(r, "cannot change ownership");
  803. return;
  804. }
  805. /*
  806. * get current info
  807. */
  808. if(Sess->caps & CAP_NT_SMBS)
  809. rc = T2queryall(Sess, a->sp, mapfile(a->path), &fi);
  810. else
  811. rc = T2querystandard(Sess, a->sp, mapfile(a->path), &fi);
  812. if(rc == -1){
  813. werrstr("(query) - %r");
  814. responderrstr(r);
  815. return;
  816. }
  817. /*
  818. * always clear the readonly attribute if set,
  819. * before trying to set any other fields.
  820. * wstat() fails if the file/dir is readonly
  821. * and this function is so full of races - who cares about one more?
  822. */
  823. rdonly(Sess, a->sp, mapfile(a->path), 0);
  824. /*
  825. * rename - one piece of joy, renaming open files
  826. * is legal (sharing permitting).
  827. */
  828. if(r->d.name && r->d.name[0]){
  829. if((p = strrchr(a->path, '/')) == nil){
  830. respond(r, "illegal path");
  831. return;
  832. }
  833. npath = emalloc9p((p-a->path)+strlen(r->d.name)+2);
  834. strecpy(npath, npath+(p- a->path)+2, a->path);
  835. strcat(npath, r->d.name);
  836. from = estrdup9p(mapfile(a->path));
  837. if(CIFSrename(Sess, a->sp, from, mapfile(npath)) == -1){
  838. werrstr("(rename) - %r");
  839. responderrstr(r);
  840. free(npath);
  841. free(from);
  842. return;
  843. }
  844. free(from);
  845. free(a->path);
  846. a->path = npath;
  847. }
  848. /*
  849. * set the files length, do this before setting
  850. * the file times as open() will alter them
  851. */
  852. if(~r->d.length){
  853. fi.size = r->d.length;
  854. if(Sess->caps & CAP_NT_SMBS){
  855. if((fh = CIFS_NT_opencreate(Sess, a->sp, mapfile(a->path),
  856. 0, FILE_NON_DIRECTORY_FILE,
  857. ATTR_NORMAL, GENERIC_WRITE, FILE_SHARE_ALL,
  858. FILE_OPEN_IF, &result, &tmpfi)) == -1){
  859. werrstr("(set length, open) - %r");
  860. responderrstr(r);
  861. return;
  862. }
  863. rc = T2setfilelength(Sess, a->sp, fh, &fi);
  864. CIFSclose(Sess, a->sp, fh);
  865. if(rc == -1){
  866. werrstr("(set length), set) - %r");
  867. responderrstr(r);
  868. return;
  869. }
  870. } else {
  871. if((fh = CIFS_SMB_opencreate(Sess, a->sp, mapfile(a->path),
  872. 1, ATTR_NORMAL, 1, &result)) == -1){
  873. werrstr("(set length, open) failed - %r");
  874. responderrstr(r);
  875. return;
  876. }
  877. rc = CIFSwrite(Sess, a->sp, fh, fi.size, 0, 0);
  878. CIFSclose(Sess, a->sp, fh);
  879. if(rc == -1){
  880. werrstr("(set length, write) - %r");
  881. responderrstr(r);
  882. return;
  883. }
  884. }
  885. }
  886. /*
  887. * This doesn't appear to set length or
  888. * attributes, no idea why, so I do those seperately
  889. */
  890. if(~r->d.mtime || ~r->d.atime){
  891. if(~r->d.mtime)
  892. fi.written = r->d.mtime;
  893. if(~r->d.atime)
  894. fi.accessed = r->d.atime;
  895. if(T2setpathinfo(Sess, a->sp, mapfile(a->path), &fi) == -1){
  896. werrstr("(set path info) - %r");
  897. responderrstr(r);
  898. return;
  899. }
  900. }
  901. /*
  902. * always update the readonly flag as
  903. * we may have cleared it above.
  904. */
  905. if(~r->d.mode){
  906. if(r->d.mode & 0222)
  907. fi.attribs &= ~ATTR_READONLY;
  908. else
  909. fi.attribs |= ATTR_READONLY;
  910. }
  911. if(rdonly(Sess, a->sp, mapfile(a->path), fi.attribs & ATTR_READONLY) == -1){
  912. werrstr("(set info) - %r");
  913. responderrstr(r);
  914. return;
  915. }
  916. /*
  917. * Win95 has a broken write-behind cache for metadata
  918. * on open files (writes go to the cache, reads bypass
  919. * the cache), so we must flush the file.
  920. */
  921. if(r->fid->omode != -1 && CIFSflush(Sess, a->sp, a->fh) == -1){
  922. werrstr("(flush) %r");
  923. responderrstr(r);
  924. return;
  925. }
  926. respond(r, nil);
  927. }
  928. static void
  929. fsend(Srv *srv)
  930. {
  931. int i;
  932. USED(srv);
  933. for(i = 0; i < Nshares; i++)
  934. CIFStreedisconnect(Sess, Shares+i);
  935. CIFSlogoff(Sess);
  936. postnote(PNPROC, Keeppid, "die");
  937. }
  938. Srv fs = {
  939. .destroyfid = fsdestroyfid,
  940. .attach= fsattach,
  941. .open= fsopen,
  942. .create= fscreate,
  943. .read= fsread,
  944. .write= fswrite,
  945. .remove= fsremove,
  946. .stat= fsstat,
  947. .wstat= fswstat,
  948. .clone= fsclone,
  949. .walk1= fswalk1,
  950. .end= fsend,
  951. };
  952. void
  953. usage(void)
  954. {
  955. fprint(2, "usage: %s [-d name] [-Dvb] [-a auth-method] [-s srvname] "
  956. "[-n called-name] [-k factotum-params] [-m mntpnt] "
  957. "host [share...]\n", argv0);
  958. exits("usage");
  959. }
  960. /*
  961. * SMBecho looks like the function to use for keepalives,
  962. * sadly the echo packet does not seem to reload the
  963. * idle timer in Microsoft's servers. Instead we use
  964. * "get file system size" on each share until we get one that succeeds.
  965. */
  966. static void
  967. keepalive(void)
  968. {
  969. char buf[32];
  970. uint64_t tot, fre;
  971. int fd, i, slot, rc;
  972. snprint(buf, sizeof buf, "#p/%d/args", getpid());
  973. if((fd = open(buf, OWRITE)) >= 0){
  974. fprint(fd, "%s keepalive", Host);
  975. close(fd);
  976. }
  977. rc = 0;
  978. slot = 0;
  979. do{
  980. sleep(6000);
  981. if(Active-- != 0)
  982. continue;
  983. for(i = 0; i < Nshares; i++){
  984. if((rc = T2fssizeinfo(Sess, &Shares[slot], &tot, &fre)) == 0)
  985. break;
  986. if(++slot >= Nshares)
  987. slot = 0;
  988. }
  989. }while(rc != -1);
  990. postnote(PNPROC, Attachpid, "die");
  991. }
  992. static void
  993. ding(void *u, char *msg)
  994. {
  995. USED(u);
  996. if(strstr(msg, "alarm") != nil)
  997. noted(NCONT);
  998. noted(NDFLT);
  999. }
  1000. void
  1001. dmpkey(char *s, void *v, int n)
  1002. {
  1003. int i;
  1004. unsigned char *p = (unsigned char *)v;
  1005. print("%s", s);
  1006. for(i = 0; i < n; i++)
  1007. print("%02ux ", *p++);
  1008. print("\n");
  1009. }
  1010. void
  1011. main(int argc, char **argv)
  1012. {
  1013. int i, n;
  1014. int32_t svrtime;
  1015. char windom[64], cname[64];
  1016. char *method, *sysname, *keyp, *mtpt, *svs;
  1017. *cname = 0;
  1018. keyp = "";
  1019. method = nil;
  1020. strcpy(windom, "unknown");
  1021. mtpt = svs = nil;
  1022. notify(ding);
  1023. ARGBEGIN{
  1024. case 'a':
  1025. method = EARGF(autherr());
  1026. break;
  1027. case 'b':
  1028. Billtrog ^= 1;
  1029. break;
  1030. case 'D':
  1031. chatty9p++;
  1032. break;
  1033. case 'd':
  1034. Debug = EARGF(usage());
  1035. break;
  1036. case 'i':
  1037. Checkcase = 0;
  1038. break;
  1039. case 'k':
  1040. keyp = EARGF(usage());
  1041. break;
  1042. case 'm':
  1043. mtpt = EARGF(usage());
  1044. break;
  1045. case 'n':
  1046. strncpy(cname, EARGF(usage()), sizeof(cname));
  1047. cname[sizeof(cname) - 1] = 0;
  1048. break;
  1049. case 's':
  1050. svs = EARGF(usage());
  1051. break;
  1052. case 't':
  1053. Dfstout = atoi(EARGF(usage()));
  1054. break;
  1055. default:
  1056. usage();
  1057. break;
  1058. }ARGEND
  1059. if(argc < 1)
  1060. usage();
  1061. Host = argv[0];
  1062. if(mtpt == nil && svs == nil)
  1063. mtpt = smprint("/n/%s", Host);
  1064. if((sysname = getenv("sysname")) == nil)
  1065. sysname = "unknown";
  1066. if(*cname && (Sess = cifsdial(Host, cname, sysname)) != nil)
  1067. goto connected;
  1068. if(calledname(Host, cname) == 0 &&
  1069. (Sess = cifsdial(Host, cname, sysname)) != nil)
  1070. goto connected;
  1071. strcpy(cname, Host);
  1072. if((Sess = cifsdial(Host, Host, sysname)) != nil ||
  1073. (Sess = cifsdial(Host, "*SMBSERVER", sysname)) != nil)
  1074. goto connected;
  1075. sysfatal("%s - cannot dial, %r\n", Host);
  1076. connected:
  1077. if(CIFSnegotiate(Sess, &svrtime, windom, sizeof windom, cname, sizeof cname) == -1)
  1078. sysfatal("%s - cannot negioate common protocol, %r\n", Host);
  1079. #ifndef DEBUG_MAC
  1080. Sess->secmode &= ~SECMODE_SIGN_ENABLED;
  1081. #endif
  1082. Sess->auth = getauth(method, windom, keyp, Sess->secmode, Sess->chal,
  1083. Sess->challen);
  1084. if(CIFSsession(Sess) < 0)
  1085. sysfatal("session authentication failed, %r\n");
  1086. Sess->slip = svrtime - time(nil);
  1087. Sess->cname = estrdup9p(cname);
  1088. if(CIFStreeconnect(Sess, cname, Ipcname, &Ipc) == -1)
  1089. fprint(2, "%s, %r - can't connect\n", Ipcname);
  1090. Nshares = 0;
  1091. if(argc == 1){
  1092. Share *sip;
  1093. if((n = RAPshareenum(Sess, &Ipc, &sip)) < 1)
  1094. sysfatal("can't enumerate shares: %r - specify share "
  1095. "names on command line\n");
  1096. for(i = 0; i < n; i++){
  1097. #ifdef NO_HIDDEN_SHARES
  1098. int l = strlen(sip[i].name);
  1099. if(l > 1 && sip[i].name[l-1] == '$'){
  1100. free(sip[i].name);
  1101. continue;
  1102. }
  1103. #endif
  1104. memcpy(Shares+Nshares, sip+i, sizeof(Share));
  1105. if(CIFStreeconnect(Sess, Sess->cname,
  1106. Shares[Nshares].name, Shares+Nshares) == -1){
  1107. free(Shares[Nshares].name);
  1108. continue;
  1109. }
  1110. Nshares++;
  1111. }
  1112. free(sip);
  1113. } else
  1114. for(i = 1; i < argc; i++){
  1115. if(CIFStreeconnect(Sess, Sess->cname, argv[i],
  1116. Shares+Nshares) == -1){
  1117. fprint(2, "%s: %s %q - can't connect to share"
  1118. ", %r\n", argv0, Host, argv[i]);
  1119. continue;
  1120. }
  1121. Shares[Nshares].name = strlwr(estrdup9p(argv[i]));
  1122. Nshares++;
  1123. }
  1124. if(Nshares == 0)
  1125. fprint(2, "no available shares\n");
  1126. if((Keeppid = rfork(RFPROC|RFMEM|RFNOTEG|RFFDG|RFNAMEG)) == 0){
  1127. keepalive();
  1128. exits(nil);
  1129. }
  1130. postmountsrv(&fs, svs, mtpt, MREPL|MCREATE);
  1131. exits(nil);
  1132. }