changeuser.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <authsrv.h>
  12. #include <ctype.h>
  13. #include <bio.h>
  14. #include "authcmdlib.h"
  15. void install(char*, char*, char*, int32_t, int);
  16. int exists (char*, char*);
  17. void
  18. usage(void)
  19. {
  20. fprint(2, "usage: changeuser [-pn] user\n");
  21. exits("usage");
  22. }
  23. void
  24. main(int argc, char *argv[])
  25. {
  26. char *u, key[DESKEYLEN], answer[32], p9pass[32];
  27. int which, i, newkey, newbio, dosecret;
  28. int32_t t;
  29. Acctbio a;
  30. Fs *f;
  31. srand(getpid()*time(0));
  32. fmtinstall('K', keyfmt);
  33. which = 0;
  34. ARGBEGIN{
  35. case 'p':
  36. which |= Plan9;
  37. break;
  38. case 'n':
  39. which |= Securenet;
  40. break;
  41. default:
  42. usage();
  43. }ARGEND
  44. argv0 = "changeuser";
  45. if(argc != 1)
  46. usage();
  47. u = *argv;
  48. if(memchr(u, '\0', ANAMELEN) == 0)
  49. error("bad user name");
  50. if(!which)
  51. which = Plan9;
  52. newbio = 0;
  53. t = 0;
  54. a.user = 0;
  55. if(which & Plan9){
  56. f = &fs[Plan9];
  57. newkey = 1;
  58. if(exists(f->keys, u)){
  59. readln("assign new password? [y/n]: ", answer, sizeof answer, 0);
  60. if(answer[0] != 'y' && answer[0] != 'Y')
  61. newkey = 0;
  62. }
  63. if(newkey)
  64. getpass(key, p9pass, 1, 1);
  65. dosecret = getsecret(newkey, p9pass);
  66. t = getexpiration(f->keys, u);
  67. install(f->keys, u, key, t, newkey);
  68. if(dosecret && setsecret(KEYDB, u, p9pass) == 0)
  69. error("error writing Inferno/pop secret");
  70. newbio = querybio(f->who, u, &a);
  71. if(newbio)
  72. wrbio(f->who, &a);
  73. print("user %s installed for Plan 9\n", u);
  74. syslog(0, AUTHLOG, "user %s installed for plan 9", u);
  75. }
  76. if(which & Securenet){
  77. f = &fs[Securenet];
  78. newkey = 1;
  79. if(exists(f->keys, u)){
  80. readln("assign new key? [y/n]: ", answer, sizeof answer, 0);
  81. if(answer[0] != 'y' && answer[0] != 'Y')
  82. newkey = 0;
  83. }
  84. if(newkey)
  85. for(i=0; i<DESKEYLEN; i++)
  86. key[i] = nrand(256);
  87. if(a.user == 0){
  88. t = getexpiration(f->keys, u);
  89. newbio = querybio(f->who, u, &a);
  90. }
  91. install(f->keys, u, key, t, newkey);
  92. if(newbio)
  93. wrbio(f->who, &a);
  94. findkey(f->keys, u, key);
  95. print("user %s: SecureNet key: %K\n", u, key);
  96. checksum(key, answer);
  97. print("verify with checksum %s\n", answer);
  98. print("user %s installed for SecureNet\n", u);
  99. syslog(0, AUTHLOG, "user %s installed for securenet", u);
  100. }
  101. exits(0);
  102. }
  103. void
  104. install(char *db, char *u, char *key, int32_t t, int newkey)
  105. {
  106. char buf[KEYDBBUF+ANAMELEN+20];
  107. int fd;
  108. if(!exists(db, u)){
  109. snprint(buf, sizeof buf, "%s/%s", db, u);
  110. fd = create(buf, OREAD, 0777|DMDIR);
  111. if(fd < 0)
  112. error("can't create user %s: %r", u);
  113. close(fd);
  114. }
  115. if(newkey){
  116. snprint(buf, sizeof buf, "%s/%s/key", db, u);
  117. fd = open(buf, OWRITE);
  118. if(fd < 0 || write(fd, key, DESKEYLEN) != DESKEYLEN)
  119. error("can't set key: %r");
  120. close(fd);
  121. }
  122. if(t == -1)
  123. return;
  124. snprint(buf, sizeof buf, "%s/%s/expire", db, u);
  125. fd = open(buf, OWRITE);
  126. if(fd < 0 || fprint(fd, "%ld", t) < 0)
  127. error("can't write expiration time");
  128. close(fd);
  129. }
  130. int
  131. exists(char *db, char *u)
  132. {
  133. char buf[KEYDBBUF+ANAMELEN+6];
  134. snprint(buf, sizeof buf, "%s/%s/expire", db, u);
  135. if(access(buf, 0) < 0)
  136. return 0;
  137. return 1;
  138. }