secureidcheck.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. /*
  10. * This code uses RADIUS as a portable way to validate tokens such as SecurID.
  11. * It is relatively simple to send a UDP packet and get a response, but various
  12. * things can go wrong. Speaking the proprietary ACE protocol would allow
  13. * handling "next token code" and other error messages. More importantly, the
  14. * timeout threshold is inherently hard to pick. We observe responses taking
  15. * longer than 10 seconds in normal times. That is a long time to wait before
  16. * retrying on a second server. Moreover, if the UDP response is lost, retrying
  17. * on a second server will also fail because the valid token code may be
  18. * presented only once. This whole approach is flawed, but best we can do.
  19. */
  20. /* RFC2138 */
  21. #include <u.h>
  22. #include <libc.h>
  23. #include <ip.h>
  24. #include <ctype.h>
  25. #include <mp.h>
  26. #include <libsec.h>
  27. #include <bio.h>
  28. #include <ndb.h>
  29. #define AUTHLOG "auth"
  30. enum{
  31. R_AccessRequest =1, /* Packet code */
  32. R_AccessAccept =2,
  33. R_AccessReject =3,
  34. R_AccessChallenge=11,
  35. R_UserName =1,
  36. R_UserPassword =2,
  37. R_NASIPAddress =4,
  38. R_ReplyMessage =18,
  39. R_State =24,
  40. R_NASIdentifier =32,
  41. };
  42. typedef struct Secret{
  43. uint8_t *s;
  44. int len;
  45. } Secret;
  46. typedef struct Attribute{
  47. struct Attribute *next;
  48. uint8_t type;
  49. uint8_t len; /* number of bytes in value */
  50. uint8_t val[256];
  51. } Attribute;
  52. typedef struct Packet{
  53. uint8_t code, ID;
  54. uint8_t authenticator[16];
  55. Attribute first;
  56. } Packet;
  57. /* assumes pass is at most 16 chars */
  58. void
  59. hide(Secret *shared, uint8_t *auth, Secret *pass, uint8_t *x)
  60. {
  61. DigestState *M;
  62. int i, n = pass->len;
  63. M = md5(shared->s, shared->len, nil, nil);
  64. md5(auth, 16, x, M);
  65. if(n > 16)
  66. n = 16;
  67. for(i = 0; i < n; i++)
  68. x[i] ^= pass->s[i];
  69. }
  70. int
  71. authcmp(Secret *shared, uint8_t *buf, int m, uint8_t *auth)
  72. {
  73. DigestState *M;
  74. uint8_t x[16];
  75. M = md5(buf, 4, nil, nil); /* Code+ID+Length */
  76. M = md5(auth, 16, nil, M); /* RequestAuth */
  77. M = md5(buf+20, m-20, nil, M); /* Attributes */
  78. md5(shared->s, shared->len, x, M);
  79. return memcmp(x, buf+4, 16);
  80. }
  81. Packet*
  82. newRequest(uint8_t *auth)
  83. {
  84. static uint8_t ID = 0;
  85. Packet *p;
  86. p = (Packet*)malloc(sizeof(*p));
  87. if(p == nil)
  88. return nil;
  89. p->code = R_AccessRequest;
  90. p->ID = ++ID;
  91. memmove(p->authenticator, auth, 16);
  92. p->first.next = nil;
  93. p->first.type = 0;
  94. return p;
  95. }
  96. void
  97. freePacket(Packet *p)
  98. {
  99. Attribute *a, *x;
  100. if(!p)
  101. return;
  102. a = p->first.next;
  103. while(a){
  104. x = a;
  105. a = a->next;
  106. free(x);
  107. }
  108. free(p);
  109. }
  110. int
  111. ding(void *v, char *msg)
  112. {
  113. syslog(0, AUTHLOG, "ding %s", msg);
  114. if(strstr(msg, "alarm"))
  115. return 1;
  116. return 0;
  117. }
  118. Packet *
  119. rpc(char *dest, Secret *shared, Packet *req)
  120. {
  121. uint8_t buf[4096], buf2[4096], *b, *e;
  122. Packet *resp;
  123. Attribute *a;
  124. int m, n, fd, try;
  125. /* marshal request */
  126. e = buf + sizeof buf;
  127. buf[0] = req->code;
  128. buf[1] = req->ID;
  129. memmove(buf+4, req->authenticator, 16);
  130. b = buf+20;
  131. for(a = &req->first; a; a = a->next){
  132. if(b + 2 + a->len > e)
  133. return nil;
  134. *b++ = a->type;
  135. *b++ = 2 + a->len;
  136. memmove(b, a->val, a->len);
  137. b += a->len;
  138. }
  139. n = b-buf;
  140. buf[2] = n>>8;
  141. buf[3] = n;
  142. /* send request, wait for reply */
  143. fd = dial(dest, 0, 0, 0);
  144. if(fd < 0){
  145. syslog(0, AUTHLOG, "%s: rpc can't get udp channel", dest);
  146. return nil;
  147. }
  148. atnotify(ding, 1);
  149. m = -1;
  150. for(try = 0; try < 2; try++){
  151. /*
  152. * increased timeout from 4sec to 15sec because
  153. * corporate server really takes that long.
  154. */
  155. alarm(15000);
  156. m = write(fd, buf, n);
  157. if(m != n){
  158. syslog(0, AUTHLOG, "%s: rpc write err %d %d: %r",
  159. dest, m, n);
  160. m = -1;
  161. break;
  162. }
  163. m = read(fd, buf2, sizeof buf2);
  164. alarm(0);
  165. if(m < 0){
  166. syslog(0, AUTHLOG, "%s rpc read err %d: %r", dest, m);
  167. break; /* failure */
  168. }
  169. if(m == 0 || buf2[1] != buf[1]){ /* need matching ID */
  170. syslog(0, AUTHLOG, "%s unmatched reply %d", dest, m);
  171. continue;
  172. }
  173. if(authcmp(shared, buf2, m, buf+4) == 0)
  174. break;
  175. syslog(0, AUTHLOG, "%s bad rpc chksum", dest);
  176. }
  177. close(fd);
  178. if(m <= 0)
  179. return nil;
  180. /* unmarshal reply */
  181. b = buf2;
  182. e = buf2+m;
  183. resp = (Packet*)malloc(sizeof(*resp));
  184. if(resp == nil)
  185. return nil;
  186. resp->code = *b++;
  187. resp->ID = *b++;
  188. n = *b++;
  189. n = (n<<8) | *b++;
  190. if(m != n){
  191. syslog(0, AUTHLOG, "rpc got %d bytes, length said %d", m, n);
  192. if(m > n)
  193. e = buf2+n;
  194. }
  195. memmove(resp->authenticator, b, 16);
  196. b += 16;
  197. a = &resp->first;
  198. a->type = 0;
  199. for(;;){
  200. if(b >= e){
  201. a->next = nil;
  202. break; /* exit loop */
  203. }
  204. a->type = *b++;
  205. a->len = (*b++) - 2;
  206. if(b + a->len > e){ /* corrupt packet */
  207. a->next = nil;
  208. freePacket(resp);
  209. return nil;
  210. }
  211. memmove(a->val, b, a->len);
  212. b += a->len;
  213. if(b < e){ /* any more attributes? */
  214. a->next = (Attribute*)malloc(sizeof(*a));
  215. if(a->next == nil){
  216. free(req);
  217. return nil;
  218. }
  219. a = a->next;
  220. }
  221. }
  222. return resp;
  223. }
  224. int
  225. setAttribute(Packet *p, uint8_t type, uint8_t *s, int n)
  226. {
  227. Attribute *a;
  228. a = &p->first;
  229. if(a->type != 0){
  230. a = (Attribute*)malloc(sizeof(*a));
  231. if(a == nil)
  232. return -1;
  233. a->next = p->first.next;
  234. p->first.next = a;
  235. }
  236. a->type = type;
  237. a->len = n;
  238. if(a->len > 253) /* RFC2138, section 5 */
  239. a->len = 253;
  240. memmove(a->val, s, a->len);
  241. return 0;
  242. }
  243. /* return a reply message attribute string */
  244. char*
  245. replymsg(Packet *p)
  246. {
  247. Attribute *a;
  248. static char buf[255];
  249. for(a = &p->first; a; a = a->next)
  250. if(a->type == R_ReplyMessage){
  251. if(a->len >= sizeof buf)
  252. a->len = sizeof(buf)-1;
  253. memmove(buf, a->val, a->len);
  254. buf[a->len] = 0;
  255. }
  256. return buf;
  257. }
  258. /* for convenience while debugging */
  259. char *replymess;
  260. Attribute *stateattr;
  261. void
  262. logPacket(Packet *p)
  263. {
  264. int i;
  265. char *np, *e;
  266. char buf[255], pbuf[4*1024];
  267. uint8_t *au = p->authenticator;
  268. Attribute *a;
  269. e = pbuf + sizeof(pbuf);
  270. np = seprint(pbuf, e, "Packet ID=%d auth=%x %x %x... ",
  271. p->ID, au[0], au[1], au[2]);
  272. switch(p->code){
  273. case R_AccessRequest:
  274. np = seprint(np, e, "request\n");
  275. break;
  276. case R_AccessAccept:
  277. np = seprint(np, e, "accept\n");
  278. break;
  279. case R_AccessReject:
  280. np = seprint(np, e, "reject\n");
  281. break;
  282. case R_AccessChallenge:
  283. np = seprint(np, e, "challenge\n");
  284. break;
  285. default:
  286. np = seprint(np, e, "code=%d\n", p->code);
  287. break;
  288. }
  289. replymess = "0000000";
  290. for(a = &p->first; a; a = a->next){
  291. if(a->len > 253 )
  292. a->len = 253;
  293. memmove(buf, a->val, a->len);
  294. np = seprint(np, e, " [%d]", a->type);
  295. for(i = 0; i < a->len; i++)
  296. if(isprint(a->val[i]))
  297. np = seprint(np, e, "%c", a->val[i]);
  298. else
  299. np = seprint(np, e, "\\%o", a->val[i]);
  300. np = seprint(np, e, "\n");
  301. buf[a->len] = 0;
  302. if(a->type == R_ReplyMessage)
  303. replymess = strdup(buf);
  304. else if(a->type == R_State)
  305. stateattr = a;
  306. }
  307. syslog(0, AUTHLOG, "%s", pbuf);
  308. }
  309. static uint8_t*
  310. getipv4addr(void)
  311. {
  312. Ipifc *nifc;
  313. Iplifc *lifc;
  314. static Ipifc *ifc;
  315. ifc = readipifc("/net", ifc, -1);
  316. for(nifc = ifc; nifc; nifc = nifc->next)
  317. for(lifc = nifc->lifc; lifc; lifc = lifc->next)
  318. if (ipcmp(lifc->ip, IPnoaddr) != 0 &&
  319. ipcmp(lifc->ip, v4prefix) != 0)
  320. return lifc->ip;
  321. return nil;
  322. }
  323. extern Ndb *db;
  324. /* returns 0 on success, error message on failure */
  325. char*
  326. secureidcheck(char *user, char *response)
  327. {
  328. char *radiussecret = nil;
  329. char *rv = "authentication failed";
  330. char dest[3*IPaddrlen+20], ruser[64];
  331. uint8_t *ip;
  332. uint8_t x[16];
  333. uint32_t u[4];
  334. Ndbs s;
  335. Ndbtuple *t = nil, *nt, *tt;
  336. Packet *req = nil, *resp = nil;
  337. Secret shared, pass;
  338. static Ndb *netdb;
  339. if(netdb == nil)
  340. netdb = ndbopen(0);
  341. /* bad responses make them disable the fob, avoid silly checks */
  342. if(strlen(response) < 4 || strpbrk(response,"abcdefABCDEF") != nil)
  343. goto out;
  344. /* get radius secret */
  345. radiussecret = ndbgetvalue(db, &s, "radius", "lra-radius", "secret", &t);
  346. if(radiussecret == nil){
  347. syslog(0, AUTHLOG, "secureidcheck: nil radius secret: %r");
  348. goto out;
  349. }
  350. /* translate user name if we have to */
  351. strcpy(ruser, user);
  352. for(nt = t; nt; nt = nt->entry)
  353. if(strcmp(nt->attr, "uid") == 0 && strcmp(nt->val, user) == 0)
  354. for(tt = nt->line; tt != nt; tt = tt->line)
  355. if(strcmp(tt->attr, "rid") == 0){
  356. strcpy(ruser, tt->val);
  357. break;
  358. }
  359. ndbfree(t);
  360. t = nil;
  361. u[0] = fastrand();
  362. u[1] = fastrand();
  363. u[2] = fastrand();
  364. u[3] = fastrand();
  365. req = newRequest((uint8_t*)u);
  366. if(req == nil)
  367. goto out;
  368. shared.s = (uint8_t*)radiussecret;
  369. shared.len = strlen(radiussecret);
  370. ip = getipv4addr();
  371. if(ip == nil){
  372. syslog(0, AUTHLOG, "no interfaces: %r\n");
  373. goto out;
  374. }
  375. if(setAttribute(req, R_NASIPAddress, ip + IPv4off, 4) < 0)
  376. goto out;
  377. if(setAttribute(req, R_UserName, (uint8_t*)ruser, strlen(ruser)) < 0)
  378. goto out;
  379. pass.s = (uint8_t*)response;
  380. pass.len = strlen(response);
  381. hide(&shared, req->authenticator, &pass, x);
  382. if(setAttribute(req, R_UserPassword, x, 16) < 0)
  383. goto out;
  384. t = ndbsearch(netdb, &s, "sys", "lra-radius");
  385. if(t == nil){
  386. syslog(0, AUTHLOG, "secureidcheck: nil radius sys search: %r\n");
  387. goto out;
  388. }
  389. for(nt = t; nt; nt = nt->entry){
  390. if(strcmp(nt->attr, "ip") != 0)
  391. continue;
  392. snprint(dest, sizeof dest, "udp!%s!radius", nt->val);
  393. resp = rpc(dest, &shared, req);
  394. if(resp == nil){
  395. syslog(0, AUTHLOG, "%s nil response", dest);
  396. continue;
  397. }
  398. if(resp->ID != req->ID){
  399. syslog(0, AUTHLOG, "%s mismatched ID req=%d resp=%d",
  400. dest, req->ID, resp->ID);
  401. freePacket(resp);
  402. resp = nil;
  403. continue;
  404. }
  405. switch(resp->code){
  406. case R_AccessAccept:
  407. syslog(0, AUTHLOG, "%s accepted ruser=%s", dest, ruser);
  408. rv = nil;
  409. break;
  410. case R_AccessReject:
  411. syslog(0, AUTHLOG, "%s rejected ruser=%s %s",
  412. dest, ruser, replymsg(resp));
  413. rv = "secureid failed";
  414. break;
  415. case R_AccessChallenge:
  416. syslog(0, AUTHLOG, "%s challenge ruser=%s %s",
  417. dest, ruser, replymsg(resp));
  418. rv = "secureid out of sync";
  419. break;
  420. default:
  421. syslog(0, AUTHLOG, "%s code=%d ruser=%s %s",
  422. dest, resp->code, ruser, replymsg(resp));
  423. break;
  424. }
  425. break; /* we have a proper reply, no need to ask again */
  426. }
  427. out:
  428. if (t)
  429. ndbfree(t);
  430. free(radiussecret);
  431. freePacket(req);
  432. freePacket(resp);
  433. return rv;
  434. }