warning.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <auth.h>
  5. #include "authcmdlib.h"
  6. /* working directory */
  7. Dir *dirbuf;
  8. long ndirbuf = 0;
  9. int debug;
  10. long readdirect(int);
  11. void douser(Fs*, char*);
  12. void dodir(Fs*);
  13. int mail(Fs*, char*, char*, long);
  14. int mailin(Fs*, char*, long, char*, char*);
  15. void complain(char*, ...);
  16. long readnumfile(char*);
  17. void writenumfile(char*, long);
  18. void
  19. usage(void)
  20. {
  21. fprint(2, "usage: %s [-n] [-p]\n", argv0);
  22. exits("usage");
  23. }
  24. void
  25. main(int argc, char **argv)
  26. {
  27. int which;
  28. which = 0;
  29. ARGBEGIN{
  30. case 'p':
  31. which |= Plan9;
  32. break;
  33. case 'n':
  34. which |= Securenet;
  35. break;
  36. case 'd':
  37. debug++;
  38. break;
  39. default:
  40. usage();
  41. }ARGEND
  42. argv0 = "warning";
  43. if(!which)
  44. which |= Plan9 | Securenet;
  45. if(which & Plan9)
  46. dodir(&fs[Plan9]);
  47. if(which & Securenet)
  48. dodir(&fs[Securenet]);
  49. }
  50. void
  51. dodir(Fs *f)
  52. {
  53. int nfiles;
  54. int i, fd;
  55. if(chdir(f->keys) < 0){
  56. complain("can't chdir to %s: %r", f->keys);
  57. return;
  58. }
  59. fd = open(".", OREAD);
  60. if(fd < 0){
  61. complain("can't open %s: %r\n", f->keys);
  62. return;
  63. }
  64. nfiles = dirreadall(fd, &dirbuf);
  65. close(fd);
  66. for(i = 0; i < nfiles; i++)
  67. douser(f, dirbuf[i].name);
  68. }
  69. /*
  70. * check for expiration
  71. */
  72. void
  73. douser(Fs *f, char *user)
  74. {
  75. int n, nwarn;
  76. char buf[128];
  77. long rcvrs, et, now;
  78. char *l;
  79. sprint(buf, "%s/expire", user);
  80. et = readnumfile(buf);
  81. now = time(0);
  82. /* start warning 2 weeks ahead of time */
  83. if(et <= now || et > now+14*24*60*60)
  84. return;
  85. sprint(buf, "%s/warnings", user);
  86. nwarn = readnumfile(buf);
  87. if(et <= now+14*24*60*60 && et > now+7*24*60*60){
  88. /* one warning 2 weeks before expiration */
  89. if(nwarn > 0)
  90. return;
  91. nwarn = 1;
  92. } else {
  93. /* one warning 1 week before expiration */
  94. if(nwarn > 1)
  95. return;
  96. nwarn = 2;
  97. }
  98. /*
  99. * if we can't open the who file, just mail to the user and hope
  100. * for it makes it.
  101. */
  102. if(f->b){
  103. if(Bseek(f->b, 0, 0) < 0){
  104. Bterm(f->b);
  105. f->b = 0;
  106. }
  107. }
  108. if(f->b == 0){
  109. f->b = Bopen(f->who, OREAD);
  110. if(f->b == 0){
  111. if(mail(f, user, user, et) > 0)
  112. writenumfile(buf, nwarn);
  113. return;
  114. }
  115. }
  116. /*
  117. * look for matches in the who file and mail to every address on
  118. * matching lines
  119. */
  120. rcvrs = 0;
  121. while(l = Brdline(f->b, '\n')){
  122. n = strlen(user);
  123. if(strncmp(l, user, n) == 0 && (l[n] == ' ' || l[n] == '\t'))
  124. rcvrs += mailin(f, user, et, l, l+Blinelen(f->b));
  125. }
  126. /*
  127. * if no matches, try the user directly
  128. */
  129. if(rcvrs == 0)
  130. rcvrs = mail(f, user, user, et);
  131. rcvrs += mail(f, "netkeys", user, et);
  132. if(rcvrs)
  133. writenumfile(buf, nwarn);
  134. }
  135. /*
  136. * anything in <>'s is an address
  137. */
  138. int
  139. mailin(Fs *f, char *user, long et, char *l, char *e)
  140. {
  141. int n;
  142. int rcvrs;
  143. char *p;
  144. char addr[256];
  145. p = 0;
  146. rcvrs = 0;
  147. while(l < e){
  148. switch(*l){
  149. case '<':
  150. p = l + 1;
  151. break;
  152. case '>':
  153. if(p == 0)
  154. break;
  155. n = l - p;
  156. if(n > 0 && n <= sizeof(addr) - 2){
  157. memmove(addr, p, n);
  158. addr[n] = 0;
  159. rcvrs += mail(f, addr, user, et);
  160. }
  161. p = 0;
  162. break;
  163. }
  164. l++;
  165. }
  166. return rcvrs;
  167. }
  168. /*
  169. * send mail
  170. */
  171. int
  172. mail(Fs *f, char *rcvr, char *user, long et)
  173. {
  174. int pid, i, fd;
  175. int pfd[2];
  176. char *ct, *p;
  177. Waitmsg *w;
  178. char buf[128];
  179. if(pipe(pfd) < 0){
  180. complain("out of pipes: %r");
  181. return 0;
  182. }
  183. switch(pid = fork()){
  184. case -1:
  185. complain("can't fork: %r");
  186. return 0;
  187. case 0:
  188. break;
  189. default:
  190. if(debug)
  191. fprint(2, "started %d\n", pid);
  192. close(pfd[0]);
  193. ct = ctime(et);
  194. p = strchr(ct, '\n');
  195. *p = '.';
  196. fprint(pfd[1], "User '%s's %s expires on %s\n", user, f->msg, ct);
  197. if(f != fs)
  198. fprint(pfd[1], "If you wish to renew contact your local administrator.\n");
  199. p = strrchr(f->keys, '/');
  200. if(p)
  201. p++;
  202. else
  203. p = f->keys;
  204. sprint(buf, "/adm/warn.%s", p);
  205. fd = open(buf, OREAD);
  206. if(fd >= 0){
  207. while((i = read(fd, buf, sizeof(buf))) > 0)
  208. write(pfd[1], buf, i);
  209. close(fd);
  210. }
  211. close(pfd[1]);
  212. /* wait for warning to be mailed */
  213. for(;;){
  214. w = wait();
  215. if(w == nil)
  216. break;
  217. if(w->pid == pid){
  218. if(debug)
  219. fprint(2, "%d terminated: %s\n", pid, w->msg);
  220. if(w->msg[0] == 0){
  221. free(w);
  222. break;
  223. }else{
  224. free(w);
  225. return 0;
  226. }
  227. }else
  228. free(w);
  229. }
  230. return 1;
  231. }
  232. /* get out of the current namespace */
  233. newns("none", 0);
  234. dup(pfd[0], 0);
  235. close(pfd[0]);
  236. close(pfd[1]);
  237. putenv("upasname", "netkeys");
  238. if(debug){
  239. print("\nto %s\n", rcvr);
  240. execl("/bin/cat", "cat", nil);
  241. }
  242. execl("/bin/upas/send", "send", "-r", rcvr, nil);
  243. /* just in case */
  244. sysfatal("can't exec send: %r");
  245. return 0; /* for compiler */
  246. }
  247. void
  248. complain(char *fmt, ...)
  249. {
  250. char buf[8192], *s;
  251. va_list arg;
  252. s = buf;
  253. s += sprint(s, "%s: ", argv0);
  254. va_start(arg, fmt);
  255. s = vseprint(s, buf + sizeof(buf) / sizeof(*buf), fmt, arg);
  256. va_end(arg);
  257. *s++ = '\n';
  258. write(2, buf, s - buf);
  259. }
  260. long
  261. readnumfile(char *file)
  262. {
  263. int fd, n;
  264. char buf[64];
  265. fd = open(file, OREAD);
  266. if(fd < 0){
  267. complain("can't open %s: %r", file);
  268. return 0;
  269. }
  270. n = read(fd, buf, sizeof(buf)-1);
  271. close(fd);
  272. if(n < 0){
  273. complain("can't read %s: %r", file);
  274. return 0;
  275. }
  276. buf[n] = 0;
  277. return atol(buf);
  278. }
  279. void
  280. writenumfile(char *file, long num)
  281. {
  282. int fd;
  283. fd = open(file, OWRITE);
  284. if(fd < 0){
  285. complain("can't open %s: %r", file);
  286. return;
  287. }
  288. fprint(fd, "%ld", num);
  289. close(fd);
  290. }