main.c 26 KB

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