main.c 26 KB

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