6 #include "authcmdlib.h"
8 /* working directory */
15 void douser(Fs*, char*);
17 int mail(Fs*, char*, char*, long);
18 int mailin(Fs*, char*, long, char*, char*);
19 void complain(char*, ...);
20 long readnumfile(char*);
21 void writenumfile(char*, long);
26 fprint(2, "usage: %s [-n] [-p]\n", argv0);
31 main(int argc, char **argv)
52 which |= Plan9 | Securenet;
56 dodir(&fs[Securenet]);
65 if(chdir(f->keys) < 0){
66 complain("can't chdir to %s: %r", f->keys);
69 fd = open(".", OREAD);
71 complain("can't open %s: %r\n", f->keys);
74 nfiles = dirreadall(fd, &dirbuf);
76 for(i = 0; i < nfiles; i++)
77 douser(f, dirbuf[i].name);
81 * check for expiration
84 douser(Fs *f, char *user)
91 sprint(buf, "%s/expire", user);
92 et = readnumfile(buf);
95 /* start warning 2 weeks ahead of time */
96 if(et <= now || et > now+14*24*60*60)
99 sprint(buf, "%s/warnings", user);
100 nwarn = readnumfile(buf);
101 if(et <= now+14*24*60*60 && et > now+7*24*60*60){
102 /* one warning 2 weeks before expiration */
107 /* one warning 1 week before expiration */
114 * if we can't open the who file, just mail to the user and hope
118 if(Bseek(f->b, 0, 0) < 0){
124 f->b = Bopen(f->who, OREAD);
126 if(mail(f, user, user, et) > 0)
127 writenumfile(buf, nwarn);
133 * look for matches in the who file and mail to every address on
137 while(l = Brdline(f->b, '\n')){
139 if(strncmp(l, user, n) == 0 && (l[n] == ' ' || l[n] == '\t'))
140 rcvrs += mailin(f, user, et, l, l+Blinelen(f->b));
144 * if no matches, try the user directly
147 rcvrs = mail(f, user, user, et);
148 rcvrs += mail(f, "netkeys", user, et);
150 writenumfile(buf, nwarn);
154 * anything in <>'s is an address
157 mailin(Fs *f, char *user, long et, char *l, char *e)
175 if(n > 0 && n <= sizeof(addr) - 2){
178 rcvrs += mail(f, addr, user, et);
192 mail(Fs *f, char *rcvr, char *user, long et)
201 complain("out of pipes: %r");
205 switch(pid = fork()){
207 complain("can't fork: %r");
213 fprint(2, "started %d\n", pid);
216 p = strchr(ct, '\n');
218 fprint(pfd[1], "User '%s's %s expires on %s\n", user, f->msg, ct);
220 fprint(pfd[1], "If you wish to renew contact your local administrator.\n");
221 p = strrchr(f->keys, '/');
226 sprint(buf, "/adm/warn.%s", p);
227 fd = open(buf, OREAD);
229 while((i = read(fd, buf, sizeof(buf))) > 0)
230 write(pfd[1], buf, i);
235 /* wait for warning to be mailed */
242 fprint(2, "%d terminated: %s\n", pid, w->msg);
256 /* get out of the current namespace */
262 putenv("upasname", "netkeys");
264 print("\nto %s\n", rcvr);
265 execl("/bin/cat", "cat", nil);
267 execl("/bin/upas/send", "send", "-r", rcvr, nil);
270 sysfatal("can't exec send: %r");
272 return 0; /* for compiler */
276 complain(char *fmt, ...)
282 s += sprint(s, "%s: ", argv0);
284 s = vseprint(s, buf + sizeof(buf) / sizeof(*buf), fmt, arg);
287 write(2, buf, s - buf);
291 readnumfile(char *file)
296 fd = open(file, OREAD);
298 complain("can't open %s: %r", file);
301 n = read(fd, buf, sizeof(buf)-1);
304 complain("can't read %s: %r", file);
312 writenumfile(char *file, long num)
316 fd = open(file, OWRITE);
318 complain("can't open %s: %r", file);
321 fprint(fd, "%ld", num);