keyfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*
  2. * keyfs
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <ctype.h>
  7. #include <authsrv.h>
  8. #include <fcall.h>
  9. #include <bio.h>
  10. #include <mp.h>
  11. #include <libsec.h>
  12. #include "authcmdlib.h"
  13. #pragma varargck type "W" char*
  14. char authkey[8];
  15. typedef struct Fid Fid;
  16. typedef struct User User;
  17. enum {
  18. Qroot,
  19. Quser,
  20. Qkey,
  21. Qsecret,
  22. Qlog,
  23. Qstatus,
  24. Qexpire,
  25. Qwarnings,
  26. Qmax,
  27. Nuser = 512,
  28. MAXBAD = 10, /* max # of bad attempts before disabling the account */
  29. /* file must be randomly addressible, so names have fixed length */
  30. Namelen = ANAMELEN,
  31. };
  32. enum {
  33. Sok,
  34. Sdisabled,
  35. Smax,
  36. };
  37. struct Fid {
  38. int fid;
  39. ulong qtype;
  40. User *user;
  41. int busy;
  42. Fid *next;
  43. };
  44. struct User {
  45. char *name;
  46. char key[DESKEYLEN];
  47. char secret[SECRETLEN];
  48. ulong expire; /* 0 == never */
  49. uchar status;
  50. ulong bad; /* # of consecutive bad authentication attempts */
  51. int ref;
  52. char removed;
  53. uchar warnings;
  54. long purgatory; /* time purgatory ends */
  55. ulong uniq;
  56. User *link;
  57. };
  58. char *qinfo[Qmax] = {
  59. [Qroot] "keys",
  60. [Quser] ".",
  61. [Qkey] "key",
  62. [Qsecret] "secret",
  63. [Qlog] "log",
  64. [Qexpire] "expire",
  65. [Qstatus] "status",
  66. [Qwarnings] "warnings",
  67. };
  68. char *status[Smax] = {
  69. [Sok] "ok",
  70. [Sdisabled] "disabled",
  71. };
  72. Fid *fids;
  73. User *users[Nuser];
  74. char *userkeys;
  75. int nuser;
  76. ulong uniq = 1;
  77. Fcall rhdr,
  78. thdr;
  79. int usepass;
  80. char *warnarg;
  81. uchar mdata[8192 + IOHDRSZ];
  82. int messagesize = sizeof mdata;
  83. int readusers(void);
  84. ulong hash(char*);
  85. Fid *findfid(int);
  86. User *finduser(char*);
  87. User *installuser(char*);
  88. int removeuser(User*);
  89. void insertuser(User*);
  90. void writeusers(void);
  91. void io(int, int);
  92. void *emalloc(ulong);
  93. Qid mkqid(User*, ulong);
  94. int dostat(User*, ulong, void*, int);
  95. int newkeys(void);
  96. void warning(void);
  97. int weirdfmt(Fmt *f);
  98. char *Auth(Fid*), *Attach(Fid*), *Version(Fid*),
  99. *Flush(Fid*), *Walk(Fid*),
  100. *Open(Fid*), *Create(Fid*),
  101. *Read(Fid *), *Write(Fid*), *Clunk(Fid*),
  102. *Remove(Fid *), *Stat(Fid*), *Wstat(Fid*);
  103. char *(*fcalls[])(Fid*) = {
  104. [Tattach] Attach,
  105. [Tauth] Auth,
  106. [Tclunk] Clunk,
  107. [Tcreate] Create,
  108. [Tflush] Flush,
  109. [Topen] Open,
  110. [Tread] Read,
  111. [Tremove] Remove,
  112. [Tstat] Stat,
  113. [Tversion] Version,
  114. [Twalk] Walk,
  115. [Twrite] Write,
  116. [Twstat] Wstat,
  117. };
  118. static void
  119. usage(void)
  120. {
  121. fprint(2, "usage: %s [-p] [-m mtpt] [-w warn] [keyfile]\n", argv0);
  122. exits("usage");
  123. }
  124. void
  125. main(int argc, char *argv[])
  126. {
  127. char *mntpt;
  128. int p[2];
  129. fmtinstall('W', weirdfmt);
  130. mntpt = "/mnt/keys";
  131. ARGBEGIN{
  132. case 'm':
  133. mntpt = EARGF(usage());
  134. break;
  135. case 'p':
  136. usepass = 1;
  137. break;
  138. case 'w':
  139. warnarg = EARGF(usage());
  140. break;
  141. default:
  142. usage();
  143. break;
  144. }ARGEND
  145. argv0 = "keyfs";
  146. userkeys = "/adm/keys";
  147. if(argc > 1)
  148. usage();
  149. if(argc == 1)
  150. userkeys = argv[0];
  151. if(pipe(p) < 0)
  152. error("can't make pipe: %r");
  153. if(usepass) {
  154. getpass(authkey, nil, 0, 0);
  155. } else {
  156. if(!getauthkey(authkey))
  157. print("keyfs: warning: can't read NVRAM\n");
  158. }
  159. switch(rfork(RFPROC|RFNAMEG|RFNOTEG|RFNOWAIT|RFENVG|RFFDG)){
  160. case 0:
  161. close(p[0]);
  162. io(p[1], p[1]);
  163. exits(0);
  164. case -1:
  165. error("fork");
  166. default:
  167. close(p[1]);
  168. if(mount(p[0], -1, mntpt, MREPL|MCREATE, "") < 0)
  169. error("can't mount: %r");
  170. exits(0);
  171. }
  172. }
  173. char *
  174. Flush(Fid *f)
  175. {
  176. USED(f);
  177. return 0;
  178. }
  179. char *
  180. Auth(Fid *)
  181. {
  182. return "keyfs: authentication not required";
  183. }
  184. char *
  185. Attach(Fid *f)
  186. {
  187. if(f->busy)
  188. Clunk(f);
  189. f->user = 0;
  190. f->qtype = Qroot;
  191. f->busy = 1;
  192. thdr.qid = mkqid(f->user, f->qtype);
  193. return 0;
  194. }
  195. char*
  196. Version(Fid*)
  197. {
  198. Fid *f;
  199. for(f = fids; f; f = f->next)
  200. if(f->busy)
  201. Clunk(f);
  202. if(rhdr.msize > sizeof mdata)
  203. thdr.msize = sizeof mdata;
  204. else
  205. thdr.msize = rhdr.msize;
  206. messagesize = thdr.msize;
  207. if(strncmp(rhdr.version, "9P2000", 6) != 0)
  208. return "bad 9P version";
  209. thdr.version = "9P2000";
  210. return 0;
  211. }
  212. char *
  213. Walk(Fid *f)
  214. {
  215. char *name, *err;
  216. int i, j, max;
  217. Fid *nf;
  218. ulong qtype;
  219. User *user;
  220. if(!f->busy)
  221. return "walk of unused fid";
  222. nf = nil;
  223. qtype = f->qtype;
  224. user = f->user;
  225. if(rhdr.fid != rhdr.newfid){
  226. nf = findfid(rhdr.newfid);
  227. if(nf->busy)
  228. return "fid in use";
  229. f = nf; /* walk f */
  230. }
  231. err = nil;
  232. i = 0;
  233. if(rhdr.nwname > 0){
  234. for(; i<rhdr.nwname; i++){
  235. if(i >= MAXWELEM){
  236. err = "too many path name elements";
  237. break;
  238. }
  239. name = rhdr.wname[i];
  240. switch(qtype){
  241. case Qroot:
  242. if(strcmp(name, "..") == 0)
  243. goto Accept;
  244. user = finduser(name);
  245. if(!user)
  246. goto Out;
  247. qtype = Quser;
  248. Accept:
  249. thdr.wqid[i] = mkqid(user, qtype);
  250. break;
  251. case Quser:
  252. if(strcmp(name, "..") == 0) {
  253. qtype = Qroot;
  254. user = 0;
  255. goto Accept;
  256. }
  257. max = Qmax;
  258. for(j = Quser + 1; j < Qmax; j++)
  259. if(strcmp(name, qinfo[j]) == 0){
  260. qtype = j;
  261. break;
  262. }
  263. if(j < max)
  264. goto Accept;
  265. goto Out;
  266. default:
  267. err = "file is not a directory";
  268. goto Out;
  269. }
  270. }
  271. Out:
  272. if(i < rhdr.nwname && err == nil)
  273. err = "file not found";
  274. }
  275. if(err != nil){
  276. return err;
  277. }
  278. /* if we cloned and then completed the walk, update new fid */
  279. if(rhdr.fid != rhdr.newfid && i == rhdr.nwname){
  280. nf->busy = 1;
  281. nf->qtype = qtype;
  282. if(nf->user = user)
  283. nf->user->ref++;
  284. }else if(nf == nil && rhdr.nwname > 0){ /* walk without clone (rare) */
  285. Clunk(f);
  286. f->busy = 1;
  287. f->qtype = qtype;
  288. if(f->user = user)
  289. f->user->ref++;
  290. }
  291. thdr.nwqid = i;
  292. return 0;
  293. }
  294. char *
  295. Clunk(Fid *f)
  296. {
  297. f->busy = 0;
  298. if(f->user && --f->user->ref == 0 && f->user->removed) {
  299. free(f->user->name);
  300. free(f->user);
  301. }
  302. f->user = 0;
  303. return 0;
  304. }
  305. char *
  306. Open(Fid *f)
  307. {
  308. int mode;
  309. if(!f->busy)
  310. return "open of unused fid";
  311. mode = rhdr.mode;
  312. if(f->qtype == Quser && (mode & (OWRITE|OTRUNC)))
  313. return "user already exists";
  314. thdr.qid = mkqid(f->user, f->qtype);
  315. thdr.iounit = messagesize - IOHDRSZ;
  316. return 0;
  317. }
  318. char *
  319. Create(Fid *f)
  320. {
  321. char *name;
  322. long perm;
  323. if(!f->busy)
  324. return "create of unused fid";
  325. name = rhdr.name;
  326. if(f->user){
  327. return "permission denied";
  328. }else{
  329. perm = rhdr.perm;
  330. if(!(perm & DMDIR))
  331. return "permission denied";
  332. if(strcmp(name, "") == 0)
  333. return "empty file name";
  334. if(strlen(name) >= Namelen)
  335. return "file name too long";
  336. if(finduser(name))
  337. return "user already exists";
  338. f->user = installuser(name);
  339. f->user->ref++;
  340. f->qtype = Quser;
  341. }
  342. thdr.qid = mkqid(f->user, f->qtype);
  343. thdr.iounit = messagesize - IOHDRSZ;
  344. writeusers();
  345. return 0;
  346. }
  347. char *
  348. Read(Fid *f)
  349. {
  350. User *u;
  351. char *data;
  352. ulong off, n, m;
  353. int i, j, max;
  354. if(!f->busy)
  355. return "read of unused fid";
  356. n = rhdr.count;
  357. off = rhdr.offset;
  358. thdr.count = 0;
  359. data = thdr.data;
  360. switch(f->qtype){
  361. case Qroot:
  362. j = 0;
  363. for(i = 0; i < Nuser; i++)
  364. for(u = users[i]; u; j += m, u = u->link){
  365. m = dostat(u, Quser, data, n);
  366. if(m <= BIT16SZ)
  367. break;
  368. if(j < off)
  369. continue;
  370. data += m;
  371. n -= m;
  372. }
  373. thdr.count = data - thdr.data;
  374. return 0;
  375. case Quser:
  376. max = Qmax;
  377. max -= Quser + 1;
  378. j = 0;
  379. for(i = 0; i < max; j += m, i++){
  380. m = dostat(f->user, i + Quser + 1, data, n);
  381. if(m <= BIT16SZ)
  382. break;
  383. if(j < off)
  384. continue;
  385. data += m;
  386. n -= m;
  387. }
  388. thdr.count = data - thdr.data;
  389. return 0;
  390. case Qkey:
  391. if(f->user->status != Sok)
  392. return "user disabled";
  393. if(f->user->purgatory > time(0))
  394. return "user in purgatory";
  395. if(f->user->expire != 0 && f->user->expire < time(0))
  396. return "user expired";
  397. if(off != 0)
  398. return 0;
  399. if(n > DESKEYLEN)
  400. n = DESKEYLEN;
  401. memmove(thdr.data, f->user->key, n);
  402. thdr.count = n;
  403. return 0;
  404. case Qsecret:
  405. if(f->user->status != Sok)
  406. return "user disabled";
  407. if(f->user->purgatory > time(0))
  408. return "user in purgatory";
  409. if(f->user->expire != 0 && f->user->expire < time(0))
  410. return "user expired";
  411. if(off != 0)
  412. return 0;
  413. if(n > strlen(f->user->secret))
  414. n = strlen(f->user->secret);
  415. memmove(thdr.data, f->user->secret, n);
  416. thdr.count = n;
  417. return 0;
  418. case Qstatus:
  419. if(off != 0){
  420. thdr.count = 0;
  421. return 0;
  422. }
  423. if(f->user->status == Sok && f->user->expire && f->user->expire < time(0))
  424. sprint(thdr.data, "expired\n");
  425. else
  426. sprint(thdr.data, "%s\n", status[f->user->status]);
  427. thdr.count = strlen(thdr.data);
  428. return 0;
  429. case Qexpire:
  430. if(off != 0){
  431. thdr.count = 0;
  432. return 0;
  433. }
  434. if(!f->user->expire)
  435. strcpy(data, "never\n");
  436. else
  437. sprint(data, "%lud\n", f->user->expire);
  438. if(n > strlen(data))
  439. n = strlen(data);
  440. thdr.count = n;
  441. return 0;
  442. case Qlog:
  443. if(off != 0){
  444. thdr.count = 0;
  445. return 0;
  446. }
  447. sprint(data, "%lud\n", f->user->bad);
  448. if(n > strlen(data))
  449. n = strlen(data);
  450. thdr.count = n;
  451. return 0;
  452. case Qwarnings:
  453. if(off != 0){
  454. thdr.count = 0;
  455. return 0;
  456. }
  457. sprint(data, "%ud\n", f->user->warnings);
  458. if(n > strlen(data))
  459. n = strlen(data);
  460. thdr.count = n;
  461. return 0;
  462. default:
  463. return "permission denied: unknown qid";
  464. }
  465. }
  466. char *
  467. Write(Fid *f)
  468. {
  469. char *data, *p;
  470. ulong n, expire;
  471. int i;
  472. if(!f->busy)
  473. return "permission denied";
  474. n = rhdr.count;
  475. data = rhdr.data;
  476. switch(f->qtype){
  477. case Qkey:
  478. if(n != DESKEYLEN)
  479. return "garbled write data";
  480. memmove(f->user->key, data, DESKEYLEN);
  481. thdr.count = DESKEYLEN;
  482. break;
  483. case Qsecret:
  484. if(n >= SECRETLEN)
  485. return "garbled write data";
  486. memmove(f->user->secret, data, n);
  487. f->user->secret[n] = 0;
  488. thdr.count = n;
  489. break;
  490. case Qstatus:
  491. data[n] = '\0';
  492. if(p = strchr(data, '\n'))
  493. *p = '\0';
  494. for(i = 0; i < Smax; i++)
  495. if(strcmp(data, status[i]) == 0){
  496. f->user->status = i;
  497. break;
  498. }
  499. if(i == Smax)
  500. return "unknown status";
  501. f->user->bad = 0;
  502. thdr.count = n;
  503. break;
  504. case Qexpire:
  505. data[n] = '\0';
  506. if(p = strchr(data, '\n'))
  507. *p = '\0';
  508. else
  509. p = &data[n];
  510. if(strcmp(data, "never") == 0)
  511. expire = 0;
  512. else{
  513. expire = strtoul(data, &data, 10);
  514. if(data != p)
  515. return "bad expiration date";
  516. }
  517. f->user->expire = expire;
  518. f->user->warnings = 0;
  519. thdr.count = n;
  520. break;
  521. case Qlog:
  522. data[n] = '\0';
  523. if(strcmp(data, "good") == 0)
  524. f->user->bad = 0;
  525. else
  526. f->user->bad++;
  527. if(f->user->bad && ((f->user->bad)%MAXBAD) == 0)
  528. f->user->purgatory = time(0) + f->user->bad;
  529. return 0;
  530. case Qwarnings:
  531. data[n] = '\0';
  532. f->user->warnings = strtoul(data, 0, 10);
  533. thdr.count = n;
  534. break;
  535. case Qroot:
  536. case Quser:
  537. default:
  538. return "permission denied";
  539. }
  540. writeusers();
  541. return 0;
  542. }
  543. char *
  544. Remove(Fid *f)
  545. {
  546. if(!f->busy)
  547. return "permission denied";
  548. if(f->qtype == Qwarnings)
  549. f->user->warnings = 0;
  550. else if(f->qtype == Quser)
  551. removeuser(f->user);
  552. else {
  553. Clunk(f);
  554. return "permission denied";
  555. }
  556. Clunk(f);
  557. writeusers();
  558. return 0;
  559. }
  560. char *
  561. Stat(Fid *f)
  562. {
  563. static uchar statbuf[1024];
  564. if(!f->busy)
  565. return "stat on unattached fid";
  566. thdr.nstat = dostat(f->user, f->qtype, statbuf, sizeof statbuf);
  567. if(thdr.nstat <= BIT16SZ)
  568. return "stat buffer too small";
  569. thdr.stat = statbuf;
  570. return 0;
  571. }
  572. char *
  573. Wstat(Fid *f)
  574. {
  575. Dir d;
  576. int n;
  577. char buf[1024];
  578. if(!f->busy || f->qtype != Quser)
  579. return "permission denied";
  580. if(rhdr.nstat > sizeof buf)
  581. return "wstat buffer too big";
  582. if(convM2D(rhdr.stat, rhdr.nstat, &d, buf) == 0)
  583. return "bad stat buffer";
  584. n = strlen(d.name);
  585. if(n == 0 || n >= Namelen)
  586. return "bad user name";
  587. if(finduser(d.name))
  588. return "user already exists";
  589. if(!removeuser(f->user))
  590. return "user previously removed";
  591. free(f->user->name);
  592. f->user->name = strdup(d.name);
  593. if(f->user->name == nil)
  594. error("wstat: malloc failed: %r");
  595. insertuser(f->user);
  596. writeusers();
  597. return 0;
  598. }
  599. Qid
  600. mkqid(User *u, ulong qtype)
  601. {
  602. Qid q;
  603. q.vers = 0;
  604. q.path = qtype;
  605. if(u)
  606. q.path |= u->uniq * 0x100;
  607. if(qtype == Quser || qtype == Qroot)
  608. q.type = QTDIR;
  609. else
  610. q.type = QTFILE;
  611. return q;
  612. }
  613. int
  614. dostat(User *user, ulong qtype, void *p, int n)
  615. {
  616. Dir d;
  617. if(qtype == Quser)
  618. d.name = user->name;
  619. else
  620. d.name = qinfo[qtype];
  621. d.uid = d.gid = d.muid = "auth";
  622. d.qid = mkqid(user, qtype);
  623. if(d.qid.type & QTDIR)
  624. d.mode = 0777|DMDIR;
  625. else
  626. d.mode = 0666;
  627. d.atime = d.mtime = time(0);
  628. d.length = 0;
  629. return convD2M(&d, p, n);
  630. }
  631. int
  632. passline(Biobuf *b, void *vbuf)
  633. {
  634. char *buf = vbuf;
  635. if(Bread(b, buf, KEYDBLEN) != KEYDBLEN)
  636. return 0;
  637. decrypt(authkey, buf, KEYDBLEN);
  638. buf[Namelen-1] = '\0';
  639. return 1;
  640. }
  641. void
  642. randombytes(uchar *p, int len)
  643. {
  644. int i, fd;
  645. fd = open("/dev/random", OREAD);
  646. if(fd < 0){
  647. fprint(2, "keyfs: can't open /dev/random, using rand()\n");
  648. srand(time(0));
  649. for(i = 0; i < len; i++)
  650. p[i] = rand();
  651. return;
  652. }
  653. read(fd, p, len);
  654. close(fd);
  655. }
  656. void
  657. oldCBCencrypt(char *key7, uchar *p, int len)
  658. {
  659. uchar ivec[8];
  660. uchar key[8];
  661. DESstate s;
  662. memset(ivec, 0, 8);
  663. des56to64((uchar*)key7, key);
  664. setupDESstate(&s, key, ivec);
  665. desCBCencrypt((uchar*)p, len, &s);
  666. }
  667. void
  668. oldCBCdecrypt(char *key7, uchar *p, int len)
  669. {
  670. uchar ivec[8];
  671. uchar key[8];
  672. DESstate s;
  673. memset(ivec, 0, 8);
  674. des56to64((uchar*)key7, key);
  675. setupDESstate(&s, key, ivec);
  676. desCBCdecrypt((uchar*)p, len, &s);
  677. }
  678. void
  679. writeusers(void)
  680. {
  681. int fd, i, nu;
  682. User *u;
  683. uchar *p, *buf;
  684. ulong expire;
  685. /* count users */
  686. nu = 0;
  687. for(i = 0; i < Nuser; i++)
  688. for(u = users[i]; u; u = u->link)
  689. nu++;
  690. /* pack into buffer */
  691. buf = malloc(KEYDBOFF + nu*KEYDBLEN);
  692. if(buf == 0){
  693. fprint(2, "keyfs: can't write keys file, out of memory\n");
  694. return;
  695. }
  696. p = buf;
  697. randombytes(p, KEYDBOFF);
  698. p += KEYDBOFF;
  699. for(i = 0; i < Nuser; i++)
  700. for(u = users[i]; u; u = u->link){
  701. strncpy((char*)p, u->name, Namelen);
  702. p += Namelen;
  703. memmove(p, u->key, DESKEYLEN);
  704. p += DESKEYLEN;
  705. *p++ = u->status;
  706. *p++ = u->warnings;
  707. expire = u->expire;
  708. *p++ = expire;
  709. *p++ = expire >> 8;
  710. *p++ = expire >> 16;
  711. *p++ = expire >> 24;
  712. memmove(p, u->secret, SECRETLEN);
  713. p += SECRETLEN;
  714. }
  715. /* encrypt */
  716. oldCBCencrypt(authkey, buf, p - buf);
  717. /* write file */
  718. fd = create(userkeys, OWRITE, 0660);
  719. if(fd < 0){
  720. free(buf);
  721. fprint(2, "keyfs: can't write keys file\n");
  722. return;
  723. }
  724. if(write(fd, buf, p - buf) != (p - buf))
  725. fprint(2, "keyfs: can't write keys file\n");
  726. free(buf);
  727. close(fd);
  728. }
  729. int
  730. weirdfmt(Fmt *f)
  731. {
  732. char *s, buf[ANAMELEN*4 + 1];
  733. int i, j, n;
  734. Rune r;
  735. s = va_arg(f->args, char*);
  736. j = 0;
  737. for(i = 0; i < ANAMELEN; i += n){
  738. n = chartorune(&r, s + i);
  739. if(r == Runeerror)
  740. j += sprint(buf+j, "[%.2x]", buf[i]);
  741. else if(isascii(r) && iscntrl(r))
  742. j += sprint(buf+j, "[%.2x]", r);
  743. else if(r == ' ' || r == '/')
  744. j += sprint(buf+j, "[%c]", r);
  745. else
  746. j += sprint(buf+j, "%C", r);
  747. }
  748. return fmtstrcpy(f, buf);
  749. }
  750. int
  751. userok(char *user, int nu)
  752. {
  753. int i, n, rv;
  754. Rune r;
  755. char buf[ANAMELEN+1];
  756. memset(buf, 0, sizeof buf);
  757. memmove(buf, user, ANAMELEN);
  758. if(buf[ANAMELEN-1] != 0){
  759. fprint(2, "keyfs: %d: no termination: %W\n", nu, buf);
  760. return -1;
  761. }
  762. rv = 0;
  763. for(i = 0; buf[i]; i += n){
  764. n = chartorune(&r, buf+i);
  765. if(r == Runeerror){
  766. // fprint(2, "keyfs: name %W bad rune byte %d\n", buf, i);
  767. rv = -1;
  768. } else if(isascii(r) && iscntrl(r) || r == ' ' || r == '/'){
  769. // fprint(2, "keyfs: name %W bad char %C\n", buf, r);
  770. rv = -1;
  771. }
  772. }
  773. if(i == 0){
  774. fprint(2, "keyfs: %d: nil name\n", nu);
  775. return -1;
  776. }
  777. if(rv == -1)
  778. fprint(2, "keyfs: %d: bad syntax: %W\n", nu, buf);
  779. return rv;
  780. }
  781. int
  782. readusers(void)
  783. {
  784. int fd, i, n, nu;
  785. uchar *p, *buf, *ep;
  786. User *u;
  787. Dir *d;
  788. /* read file into an array */
  789. fd = open(userkeys, OREAD);
  790. if(fd < 0)
  791. return 0;
  792. d = dirfstat(fd);
  793. if(d == nil){
  794. close(fd);
  795. return 0;
  796. }
  797. buf = malloc(d->length);
  798. if(buf == 0){
  799. close(fd);
  800. free(d);
  801. return 0;
  802. }
  803. n = readn(fd, buf, d->length);
  804. close(fd);
  805. free(d);
  806. if(n != d->length){
  807. free(buf);
  808. return 0;
  809. }
  810. /* decrypt */
  811. n -= n % KEYDBLEN;
  812. oldCBCdecrypt(authkey, buf, n);
  813. /* unpack */
  814. nu = 0;
  815. for(i = KEYDBOFF; i < n; i += KEYDBLEN){
  816. ep = buf + i;
  817. if(userok((char*)ep, i/KEYDBLEN) < 0)
  818. continue;
  819. u = finduser((char*)ep);
  820. if(u == 0)
  821. u = installuser((char*)ep);
  822. memmove(u->key, ep + Namelen, DESKEYLEN);
  823. p = ep + Namelen + DESKEYLEN;
  824. u->status = *p++;
  825. u->warnings = *p++;
  826. if(u->status >= Smax)
  827. fprint(2, "keyfs: warning: bad status in key file\n");
  828. u->expire = p[0] + (p[1]<<8) + (p[2]<<16) + (p[3]<<24);
  829. p += 4;
  830. memmove(u->secret, p, SECRETLEN);
  831. u->secret[SECRETLEN-1] = 0;
  832. nu++;
  833. }
  834. free(buf);
  835. print("%d keys read\n", nu);
  836. return 1;
  837. }
  838. User *
  839. installuser(char *name)
  840. {
  841. User *u;
  842. int h;
  843. h = hash(name);
  844. u = emalloc(sizeof *u);
  845. u->name = strdup(name);
  846. if(u->name == nil)
  847. error("malloc failed: %r");
  848. u->removed = 0;
  849. u->ref = 0;
  850. u->purgatory = 0;
  851. u->expire = 0;
  852. u->status = Sok;
  853. u->bad = 0;
  854. u->warnings = 0;
  855. u->uniq = uniq++;
  856. u->link = users[h];
  857. users[h] = u;
  858. return u;
  859. }
  860. User *
  861. finduser(char *name)
  862. {
  863. User *u;
  864. for(u = users[hash(name)]; u; u = u->link)
  865. if(strcmp(name, u->name) == 0)
  866. return u;
  867. return 0;
  868. }
  869. int
  870. removeuser(User *user)
  871. {
  872. User *u, **last;
  873. char *name;
  874. user->removed = 1;
  875. name = user->name;
  876. last = &users[hash(name)];
  877. for(u = *last; u; u = *last){
  878. if(strcmp(name, u->name) == 0){
  879. *last = u->link;
  880. return 1;
  881. }
  882. last = &u->link;
  883. }
  884. return 0;
  885. }
  886. void
  887. insertuser(User *user)
  888. {
  889. int h;
  890. user->removed = 0;
  891. h = hash(user->name);
  892. user->link = users[h];
  893. users[h] = user;
  894. }
  895. ulong
  896. hash(char *s)
  897. {
  898. ulong h;
  899. h = 0;
  900. while(*s)
  901. h = (h << 1) ^ *s++;
  902. return h % Nuser;
  903. }
  904. Fid *
  905. findfid(int fid)
  906. {
  907. Fid *f, *ff;
  908. ff = 0;
  909. for(f = fids; f; f = f->next)
  910. if(f->fid == fid)
  911. return f;
  912. else if(!ff && !f->busy)
  913. ff = f;
  914. if(ff){
  915. ff->fid = fid;
  916. return ff;
  917. }
  918. f = emalloc(sizeof *f);
  919. f->fid = fid;
  920. f->busy = 0;
  921. f->user = 0;
  922. f->next = fids;
  923. fids = f;
  924. return f;
  925. }
  926. void
  927. io(int in, int out)
  928. {
  929. char *err;
  930. int n;
  931. long now, lastwarning;
  932. /* after restart, let the system settle for 5 mins before warning */
  933. lastwarning = time(0) - 24*60*60 + 5*60;
  934. for(;;){
  935. n = read9pmsg(in, mdata, messagesize);
  936. if(n == 0)
  937. continue;
  938. if(n < 0)
  939. error("mount read %d", n);
  940. if(convM2S(mdata, n, &rhdr) == 0)
  941. continue;
  942. if(newkeys())
  943. readusers();
  944. thdr.data = (char*)mdata + IOHDRSZ;
  945. thdr.fid = rhdr.fid;
  946. if(!fcalls[rhdr.type])
  947. err = "fcall request";
  948. else
  949. err = (*fcalls[rhdr.type])(findfid(rhdr.fid));
  950. thdr.tag = rhdr.tag;
  951. thdr.type = rhdr.type+1;
  952. if(err){
  953. thdr.type = Rerror;
  954. thdr.ename = err;
  955. }
  956. n = convS2M(&thdr, mdata, messagesize);
  957. if(write(out, mdata, n) != n)
  958. error("mount write");
  959. now = time(0);
  960. if(warnarg && (now - lastwarning > 24*60*60)){
  961. syslog(0, "auth", "keyfs starting warnings: %lux %lux",
  962. now, lastwarning);
  963. warning();
  964. lastwarning = now;
  965. }
  966. }
  967. }
  968. int
  969. newkeys(void)
  970. {
  971. Dir *d;
  972. static long ftime;
  973. d = dirstat(userkeys);
  974. if(d == nil)
  975. return 0;
  976. if(d->mtime > ftime){
  977. ftime = d->mtime;
  978. free(d);
  979. return 1;
  980. }
  981. free(d);
  982. return 0;
  983. }
  984. void *
  985. emalloc(ulong n)
  986. {
  987. void *p;
  988. if(p = malloc(n))
  989. return p;
  990. error("out of memory");
  991. return 0; /* not reached */
  992. }
  993. void
  994. warning(void)
  995. {
  996. int i;
  997. char buf[64];
  998. snprint(buf, sizeof buf, "-%s", warnarg);
  999. switch(rfork(RFPROC|RFNAMEG|RFNOTEG|RFNOWAIT|RFENVG|RFFDG)){
  1000. case 0:
  1001. i = open("/sys/log/auth", OWRITE);
  1002. if(i >= 0){
  1003. dup(i, 2);
  1004. seek(2, 0, 2);
  1005. close(i);
  1006. }
  1007. execl("/bin/auth/warning", "warning", warnarg, nil);
  1008. error("can't exec warning");
  1009. }
  1010. }