debug.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. * Test various aspects of the authentication setup.
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. #include <bio.h>
  15. #include <ndb.h>
  16. #include <auth.h>
  17. #include <authsrv.h>
  18. /* private copy with added debugging */
  19. int
  20. authdial(char *netroot, char *dom)
  21. {
  22. char *p;
  23. int rv;
  24. if(dom != nil){
  25. /* look up an auth server in an authentication domain */
  26. p = csgetvalue(netroot, "authdom", dom, "auth", nil);
  27. /* if that didn't work, just try the IP domain */
  28. if(p == nil)
  29. p = csgetvalue(netroot, "dom", dom, "auth", nil);
  30. if(p == nil){
  31. werrstr("no auth server found for %s", dom);
  32. return -1;
  33. }
  34. print("\tdialing auth server %s\n",
  35. netmkaddr(p, netroot, "ticket"));
  36. rv = dial(netmkaddr(p, netroot, "ticket"), 0, 0, 0);
  37. free(p);
  38. return rv;
  39. } else
  40. /* look for one relative to my machine */
  41. return dial(netmkaddr("$auth", netroot, "ticket"), 0, 0, 0);
  42. }
  43. void
  44. usage(void)
  45. {
  46. fprint(2, "usage: auth/debug\n");
  47. exits("usage");
  48. }
  49. static char*
  50. readcons(char *prompt, char *def, int raw, char *buf, int nbuf)
  51. {
  52. int fdin, fdout, ctl, n, m;
  53. char line[10];
  54. fdin = open("/dev/cons", OREAD);
  55. if(fdin < 0)
  56. fdin = 0;
  57. fdout = open("/dev/cons", OWRITE);
  58. if(fdout < 0)
  59. fdout = 1;
  60. if(def != nil)
  61. fprint(fdout, "%s[%s]: ", prompt, def);
  62. else
  63. fprint(fdout, "%s: ", prompt);
  64. if(raw){
  65. ctl = open("/dev/consctl", OWRITE);
  66. if(ctl >= 0)
  67. write(ctl, "rawon", 5);
  68. } else
  69. ctl = -1;
  70. m = 0;
  71. for(;;){
  72. n = read(fdin, line, 1);
  73. if(n == 0){
  74. close(ctl);
  75. werrstr("readcons: EOF");
  76. return nil;
  77. }
  78. if(n < 0){
  79. close(ctl);
  80. werrstr("can't read cons");
  81. return nil;
  82. }
  83. if(line[0] == 0x7f)
  84. exits(0);
  85. if(n == 0 || line[0] == '\n' || line[0] == '\r'){
  86. if(raw){
  87. write(ctl, "rawoff", 6);
  88. write(fdout, "\n", 1);
  89. close(ctl);
  90. }
  91. buf[m] = '\0';
  92. if(buf[0]=='\0' && def)
  93. strcpy(buf, def);
  94. return buf;
  95. }
  96. if(line[0] == '\b'){
  97. if(m > 0)
  98. m--;
  99. }else if(line[0] == 0x15){ /* ^U: line kill */
  100. m = 0;
  101. if(def != nil)
  102. fprint(fdout, "%s[%s]: ", prompt, def);
  103. else
  104. fprint(fdout, "%s: ", prompt);
  105. }else{
  106. if(m >= nbuf-1){
  107. fprint(fdout, "line too long\n");
  108. m = 0;
  109. if(def != nil)
  110. fprint(fdout, "%s[%s]: ", prompt, def);
  111. else
  112. fprint(fdout, "%s: ", prompt);
  113. }else
  114. buf[m++] = line[0];
  115. }
  116. }
  117. }
  118. void authdialfutz(char*, char*);
  119. void authfutz(char*, char*);
  120. /* scan factotum for p9sk1 keys; check them */
  121. void
  122. debugfactotumkeys(void)
  123. {
  124. char *s, *dom, *proto, *user;
  125. int found;
  126. Attr *a;
  127. Biobuf *b;
  128. b = Bopen("/mnt/factotum/ctl", OREAD);
  129. if(b == nil){
  130. fprint(2, "debug: cannot open /mnt/factotum/ctl\n");
  131. return;
  132. }
  133. found = 0;
  134. while((s = Brdstr(b, '\n', 1)) != nil){
  135. if(strncmp(s, "key ", 4) != 0){
  136. print("malformed ctl line: %s\n", s);
  137. free(s);
  138. continue;
  139. }
  140. a = _parseattr(s+4);
  141. free(s);
  142. proto = _strfindattr(a, "proto");
  143. if(proto==nil || strcmp(proto, "p9sk1")!=0)
  144. continue;
  145. dom = _strfindattr(a, "dom");
  146. if(dom == nil){
  147. print("p9sk1 key with no dom: %A\n", a);
  148. _freeattr(a);
  149. continue;
  150. }
  151. user = _strfindattr(a, "user");
  152. if(user == nil){
  153. print("p9sk1 key with no user: %A\n", a);
  154. _freeattr(a);
  155. continue;
  156. }
  157. print("p9sk1 key: %A\n", a);
  158. found = 1;
  159. authdialfutz(dom, user);
  160. _freeattr(a);
  161. }
  162. if(!found)
  163. print("no p9sk1 keys found in factotum\n");
  164. }
  165. void
  166. authdialfutz(char *dom, char *user)
  167. {
  168. int fd;
  169. char *server;
  170. char *addr;
  171. fd = authdial(nil, dom);
  172. if(fd >= 0){
  173. print("\tsuccessfully dialed auth server\n");
  174. close(fd);
  175. authfutz(dom, user);
  176. return;
  177. }
  178. print("\tcannot dial auth server: %r\n");
  179. server = csgetvalue(nil, "authdom", dom, "auth", nil);
  180. if(server){
  181. print("\tcsquery authdom=%q auth=%s\n", dom, server);
  182. free(server);
  183. return;
  184. }
  185. print("\tcsquery authdom=%q auth=* failed\n", dom);
  186. server = csgetvalue(nil, "dom", dom, "auth", nil);
  187. if(server){
  188. print("\tcsquery dom=%q auth=%q\n", dom, server);
  189. free(server);
  190. return;
  191. }
  192. print("\tcsquery dom=%q auth=*\n", dom);
  193. fd = dial(addr=netmkaddr("$auth", nil, "ticket"), 0, 0, 0);
  194. if(fd >= 0){
  195. print("\tdial %s succeeded\n", addr);
  196. close(fd);
  197. return;
  198. }
  199. print("\tdial %s failed: %r\n", addr);
  200. }
  201. void
  202. authfutz(char *dom, char *user)
  203. {
  204. int fd, nobootes;
  205. char pw[128], prompt[128], key[DESKEYLEN], booteskey[DESKEYLEN], tbuf[2*TICKETLEN],
  206. trbuf[TICKREQLEN];
  207. Ticket t;
  208. Ticketreq tr;
  209. snprint(prompt, sizeof prompt, "\tpassword for %s@%s [hit enter to skip test]", user, dom);
  210. readcons(prompt, nil, 1, pw, sizeof pw);
  211. if(pw[0] == '\0')
  212. return;
  213. passtokey(key, pw);
  214. fd = authdial(nil, dom);
  215. if(fd < 0){
  216. print("\tauthdial failed(!): %r\n");
  217. return;
  218. }
  219. /* try ticket request using just user key */
  220. tr.type = AuthTreq;
  221. strecpy(tr.authid, tr.authid+sizeof tr.authid, user);
  222. strecpy(tr.authdom, tr.authdom+sizeof tr.authdom, dom);
  223. strecpy(tr.hostid, tr.hostid+sizeof tr.hostid, user);
  224. strecpy(tr.uid, tr.uid+sizeof tr.uid, user);
  225. memset(tr.chal, 0xAA, sizeof tr.chal);
  226. convTR2M(&tr, trbuf);
  227. if(_asgetticket(fd, trbuf, tbuf) < 0){
  228. close(fd);
  229. print("\t_asgetticket failed: %r\n");
  230. return;
  231. }
  232. convM2T(tbuf, &t, key);
  233. if(t.num != AuthTc){
  234. print("\tcannot decrypt ticket1 from auth server (bad t.num=0x%.2ux)\n", t.num);
  235. print("\tauth server and you do not agree on key for %s@%s\n", user, dom);
  236. return;
  237. }
  238. if(memcmp(t.chal, tr.chal, sizeof tr.chal) != 0){
  239. print("\tbad challenge1 from auth server got %.*H wanted %.*H\n",
  240. sizeof t.chal, t.chal, sizeof tr.chal, tr.chal);
  241. print("\tauth server is rogue\n");
  242. return;
  243. }
  244. convM2T(tbuf+TICKETLEN, &t, key);
  245. if(t.num != AuthTs){
  246. print("\tcannot decrypt ticket2 from auth server (bad t.num=0x%.2ux)\n", t.num);
  247. print("\tauth server and you do not agree on key for %s@%s\n", user, dom);
  248. return;
  249. }
  250. if(memcmp(t.chal, tr.chal, sizeof tr.chal) != 0){
  251. print("\tbad challenge2 from auth server got %.*H wanted %.*H\n",
  252. sizeof t.chal, t.chal, sizeof tr.chal, tr.chal);
  253. print("\tauth server is rogue\n");
  254. return;
  255. }
  256. print("\tticket request using %s@%s key succeeded\n", user, dom);
  257. /* try ticket request using bootes key */
  258. snprint(prompt, sizeof prompt, "\tcpu server owner for domain %s ", dom);
  259. readcons(prompt, "bootes", 0, tr.authid, sizeof tr.authid);
  260. convTR2M(&tr, trbuf);
  261. if(_asgetticket(fd, trbuf, tbuf) < 0){
  262. close(fd);
  263. print("\t_asgetticket failed: %r\n");
  264. return;
  265. }
  266. convM2T(tbuf, &t, key);
  267. if(t.num != AuthTc){
  268. print("\tcannot decrypt ticket1 from auth server (bad t.num=0x%.2ux)\n", t.num);
  269. print("\tauth server and you do not agree on key for %s@%s\n", user, dom);
  270. return;
  271. }
  272. if(memcmp(t.chal, tr.chal, sizeof tr.chal) != 0){
  273. print("\tbad challenge1 from auth server got %.*H wanted %.*H\n",
  274. sizeof t.chal, t.chal, sizeof tr.chal, tr.chal);
  275. print("\tauth server is rogue\n");
  276. return;
  277. }
  278. snprint(prompt, sizeof prompt, "\tpassword for %s@%s [hit enter to skip test]", tr.authid, dom);
  279. readcons(prompt, nil, 1, pw, sizeof pw);
  280. if(pw[0] == '\0'){
  281. nobootes=1;
  282. goto Nobootes;
  283. }
  284. nobootes = 0;
  285. passtokey(booteskey, pw);
  286. convM2T(tbuf+TICKETLEN, &t, booteskey);
  287. if(t.num != AuthTs){
  288. print("\tcannot decrypt ticket2 from auth server (bad t.num=0x%.2ux)\n", t.num);
  289. print("\tauth server and you do not agree on key for %s@%s\n", tr.authid, dom);
  290. return;
  291. }
  292. if(memcmp(t.chal, tr.chal, sizeof tr.chal) != 0){
  293. print("\tbad challenge2 from auth server got %.*H wanted %.*H\n",
  294. sizeof t.chal, t.chal, sizeof tr.chal, tr.chal);
  295. print("\tauth server is rogue\n");
  296. return;
  297. }
  298. print("\tticket request using %s@%s key succeeded\n", tr.authid, dom);
  299. Nobootes:;
  300. USED(nobootes);
  301. /* try p9sk1 exchange with local factotum to test that key is right */
  302. /*
  303. * try p9sk1 exchange with factotum on
  304. * auth server (assumes running cpu service)
  305. * to test that bootes key is right over there
  306. */
  307. }
  308. void
  309. main(int argc, char **argv)
  310. {
  311. quotefmtinstall();
  312. fmtinstall('A', _attrfmt);
  313. fmtinstall('H', encodefmt);
  314. ARGBEGIN{
  315. default:
  316. usage();
  317. }ARGEND
  318. if(argc != 0)
  319. usage();
  320. debugfactotumkeys();
  321. }