auth.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "all.h"
  2. #include "io.h"
  3. #include <authsrv.h>
  4. Nvrsafe nvr;
  5. static int gotnvr; /* flag: nvr contains nvram; it could be bad */
  6. char*
  7. nvrgetconfig(void)
  8. {
  9. return conf.confdev;
  10. }
  11. /*
  12. * we shouldn't be writing nvram any more.
  13. * the secstore/config field is now just secstore key.
  14. * we still use authid, authdom and machkey for authentication.
  15. */
  16. int
  17. nvrcheck(void)
  18. {
  19. uchar csum;
  20. if (readnvram(&nvr, NVread) < 0) {
  21. print("nvrcheck: can't read nvram\n");
  22. return 1;
  23. } else
  24. gotnvr = 1;
  25. print("nvr read\n");
  26. csum = nvcsum(nvr.machkey, sizeof nvr.machkey);
  27. if(csum != nvr.machsum) {
  28. print("\n\n ** NVR key checksum is incorrect **\n");
  29. print(" ** set password to allow attaches **\n\n");
  30. memset(nvr.machkey, 0, sizeof nvr.machkey);
  31. return 1;
  32. }
  33. return 0;
  34. }
  35. int
  36. nvrsetconfig(char* word)
  37. {
  38. /* config block is on device `word' */
  39. USED(word);
  40. return 0;
  41. }
  42. int
  43. conslock(void)
  44. {
  45. char *ln;
  46. char nkey1[DESKEYLEN];
  47. static char zeroes[DESKEYLEN];
  48. if(memcmp(nvr.machkey, zeroes, DESKEYLEN) == 0) {
  49. print("no password set\n");
  50. return 0;
  51. }
  52. for(;;) {
  53. print("%s password:", service);
  54. /* could turn off echo here */
  55. if ((ln = Brdline(&bin, '\n')) == nil)
  56. return 0;
  57. ln[Blinelen(&bin)-1] = '\0';
  58. /* could turn on echo here */
  59. memset(nkey1, 0, DESKEYLEN);
  60. passtokey(nkey1, ln);
  61. if(memcmp(nkey1, nvr.machkey, DESKEYLEN) == 0) {
  62. prdate();
  63. break;
  64. }
  65. print("Bad password\n");
  66. delay(1000);
  67. }
  68. return 1;
  69. }
  70. /*
  71. * authentication specific to 9P2000
  72. */
  73. /* authentication states */
  74. enum
  75. {
  76. HaveProtos=1,
  77. NeedProto,
  78. HaveOK,
  79. NeedCchal,
  80. HaveSinfo,
  81. NeedTicket,
  82. HaveSauthenticator,
  83. SSuccess,
  84. };
  85. char *phasename[] =
  86. {
  87. [HaveProtos] "HaveProtos",
  88. [NeedProto] "NeedProto",
  89. [HaveOK] "HaveOK",
  90. [NeedCchal] "NeedCchal",
  91. [HaveSinfo] "HaveSinfo",
  92. [NeedTicket] "NeedTicket",
  93. [HaveSauthenticator] "HaveSauthenticator",
  94. [SSuccess] "SSuccess",
  95. };
  96. /* authentication structure */
  97. struct Auth
  98. {
  99. int inuse;
  100. char uname[NAMELEN]; /* requestor's remote user name */
  101. char aname[NAMELEN]; /* requested aname */
  102. Userid uid; /* uid decided on */
  103. int phase;
  104. char cchal[CHALLEN];
  105. char tbuf[TICKETLEN+AUTHENTLEN]; /* server ticket */
  106. Ticket t;
  107. Ticketreq tr;
  108. };
  109. Auth* auths;
  110. Lock authlock;
  111. void
  112. authinit(void)
  113. {
  114. auths = malloc(conf.nauth * sizeof(*auths));
  115. }
  116. static int
  117. failure(Auth *s, char *why)
  118. {
  119. int i;
  120. if(*why)print("authentication failed: %s: %s\n", phasename[s->phase], why);
  121. srand((ulong)s + time(nil));
  122. for(i = 0; i < CHALLEN; i++)
  123. s->tr.chal[i] = nrand(256);
  124. s->uid = -1;
  125. strncpy(s->tr.authid, nvr.authid, NAMELEN);
  126. strncpy(s->tr.authdom, nvr.authdom, DOMLEN);
  127. memmove(s->cchal, s->tr.chal, sizeof(s->cchal));
  128. s->phase = HaveProtos;
  129. return -1;
  130. }
  131. Auth*
  132. authnew(char *uname, char *aname)
  133. {
  134. static int si = 0;
  135. int i, nwrap;
  136. Auth *s;
  137. i = si;
  138. nwrap = 0;
  139. for(;;){
  140. if(i < 0 || i >= conf.nauth){
  141. if(++nwrap > 1)
  142. return nil;
  143. i = 0;
  144. }
  145. s = &auths[i++];
  146. if(s->inuse)
  147. continue;
  148. lock(&authlock);
  149. if(s->inuse == 0){
  150. s->inuse = 1;
  151. strncpy(s->uname, uname, NAMELEN-1);
  152. strncpy(s->aname, aname, NAMELEN-1);
  153. failure(s, "");
  154. si = i;
  155. unlock(&authlock);
  156. break;
  157. }
  158. unlock(&authlock);
  159. }
  160. return s;
  161. }
  162. void
  163. authfree(Auth *s)
  164. {
  165. if(s != nil)
  166. s->inuse = 0;
  167. }
  168. int
  169. authread(File* file, uchar* data, int n)
  170. {
  171. Auth *s;
  172. int m;
  173. s = file->auth;
  174. if(s == nil)
  175. return -1;
  176. switch(s->phase){
  177. default:
  178. return failure(s, "unexpected phase");
  179. case HaveProtos:
  180. m = snprint((char*)data, n, "v.2 p9sk1@%s", nvr.authdom) + 1;
  181. s->phase = NeedProto;
  182. break;
  183. case HaveOK:
  184. m = 3;
  185. if(n < m)
  186. return failure(s, "read too short");
  187. strcpy((char*)data, "OK");
  188. s->phase = NeedCchal;
  189. break;
  190. case HaveSinfo:
  191. m = TICKREQLEN;
  192. if(n < m)
  193. return failure(s, "read too short");
  194. convTR2M(&s->tr, (char*)data);
  195. s->phase = NeedTicket;
  196. break;
  197. case HaveSauthenticator:
  198. m = AUTHENTLEN;
  199. if(n < m)
  200. return failure(s, "read too short");
  201. memmove(data, s->tbuf+TICKETLEN, m);
  202. s->phase = SSuccess;
  203. break;
  204. }
  205. return m;
  206. }
  207. int
  208. authwrite(File* file, uchar *data, int n)
  209. {
  210. Auth *s;
  211. int m;
  212. char *p, *d;
  213. Authenticator a;
  214. s = file->auth;
  215. if(s == nil)
  216. return -1;
  217. switch(s->phase){
  218. default:
  219. return failure(s, "unknown phase");
  220. case NeedProto:
  221. p = (char*)data;
  222. if(p[n-1] != 0)
  223. return failure(s, "proto missing terminator");
  224. d = strchr(p, ' ');
  225. if(d == nil)
  226. return failure(s, "proto missing separator");
  227. *d++ = 0;
  228. if(strcmp(p, "p9sk1") != 0)
  229. return failure(s, "unknown proto");
  230. if(strcmp(d, nvr.authdom) != 0)
  231. return failure(s, "unknown domain");
  232. s->phase = HaveOK;
  233. m = n;
  234. break;
  235. case NeedCchal:
  236. m = CHALLEN;
  237. if(n < m)
  238. return failure(s, "client challenge too short");
  239. memmove(s->cchal, data, sizeof(s->cchal));
  240. s->phase = HaveSinfo;
  241. break;
  242. case NeedTicket:
  243. m = TICKETLEN+AUTHENTLEN;
  244. if(n < m)
  245. return failure(s, "ticket+auth too short");
  246. convM2T((char*)data, &s->t, nvr.machkey);
  247. if(s->t.num != AuthTs
  248. || memcmp(s->t.chal, s->tr.chal, sizeof(s->t.chal)) != 0)
  249. return failure(s, "bad ticket");
  250. convM2A((char*)data+TICKETLEN, &a, s->t.key);
  251. if(a.num != AuthAc
  252. || memcmp(a.chal, s->tr.chal, sizeof(a.chal)) != 0
  253. || a.id != 0)
  254. return failure(s, "bad authenticator");
  255. /* at this point, we're convinced */
  256. s->uid = strtouid(s->t.suid);
  257. if(s->uid < 0)
  258. return failure(s, "unknown user");
  259. if(cons.flags & authdebugflag)
  260. print("user %s = %d authenticated\n",
  261. s->t.suid, s->uid);
  262. /* create an authenticator to send back */
  263. a.num = AuthAs;
  264. memmove(a.chal, s->cchal, sizeof(a.chal));
  265. a.id = 0;
  266. convA2M(&a, s->tbuf+TICKETLEN, s->t.key);
  267. s->phase = HaveSauthenticator;
  268. break;
  269. }
  270. return m;
  271. }
  272. int
  273. authuid(Auth* s)
  274. {
  275. return s->uid;
  276. }
  277. char*
  278. authaname(Auth* s)
  279. {
  280. return s->aname;
  281. }
  282. char*
  283. authuname(Auth* s)
  284. {
  285. return s->uname;
  286. }