Debian Codegraph @

away.c

raw ◀ ▶
1 Debian patch touch this file
1 /* Away - terminal locking program
2 * Copyright (C) 1999-2002 Cameron Moore and others
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * Or try here: http://www.fsf.org/copyleft/gpl.html
18 *
19 * $Id: away.c,v 1.14 2002/08/22 05:23:39 hrothgar Exp $
20 */
21
22 #include "away.h"
23
24 int main(int argc, char **argv) {
25 int option_count = 0;
26 int option_index = 0;
27 int c = 0;
28 short mesg_exec = 0;
29 short restart = 0;
30
31 signal(SIGINT , SIG_IGN);
32 signal(SIGTERM, SIG_IGN);
33 signal(SIGQUIT, SIG_IGN);
34 signal(SIGTSTP, SIG_IGN);
35 signal(SIGUSR1, SIG_IGN);
36 signal(SIGUSR2, SIG_IGN);
37
38 if (argc == 1) { short_help(argv[0]); }
39 while ((c = getopt_long(argc, argv, "cCf:FhmpPt:Tv", long_options, &option_index)) && c != -1) {
40 restart = 1;
41 switch (c) {
42 case 'c':
43 setenv(AWAY_MAIL, "1", 1);
44 option_count++;
45 break;
46 case 'C':
47 setenv(AWAY_MAIL, "0", 1);
48 option_count++;
49 break;
50 case 'f':
51 setenv(AWAY_RCFILE, (char *)strdup(optarg), 1);
52 option_count += strchr(argv[option_count],'=') ? 1 : 2;
53 break;
54 case 'F':
55 setenv(AWAY_NORCFILE, "1", 1);
56 option_count++;
57 break;
58 case 'h':
59 ext_help(argv[0]);
60 break;
61 case 'm':
62 mesg_exec = 1;
63 option_count++;
64 break;
65 case 'p':
66 setenv(AWAY_PERSIST, "1", 1);
67 option_count++;
68 break;
69 case 'P':
70 setenv(AWAY_PERSIST, "0", 1);
71 option_count++;
72 break;
73 case 't':
74 if (atoi(optarg) >= MIN_TIME)
75 setenv(AWAY_TIME, (char *)strdup(optarg), 1);
76 option_count += strchr(argv[option_count],'=') ? 1 : 2;
77 break;
78 case 'T':
79 setenv(AWAY_NOTIME, "1", 1);
80 option_count++;
81 break;
82 case 'v':
83 print_version();
84 break;
85 case '?':
86 fprintf(stderr, "Try `%s --help` for more information.\n", argv[0]);
87 exit(0);
88 break;
89 default:
90 break;
91 }
92 }
93
94 /* if passed an option, we need to re-exec */
95 if (restart) {
96 /* make sure we have enough args for a message */
97 if (option_count+1 == argc)
98 short_help(argv[0]);
99 else
100 re_exec(argc, argv, option_count, mesg_exec);
101 }
102
103 check_env();
104 master();
105 return 0;
106 }
107
108 /* Master function */
109 void master(void) {
110 pthread_t mail_thread;
111 short error = 1;
112 struct passwd *pw = NULL;
113 Mailbox *mailboxRoot = NULL;
114
115 /* get home dir */
116 pw = getpwuid(getuid());
117 if (!pw) { fprintf(stderr, "You don't exist!\n"); exit(1); }
118 read_config(&mailboxRoot, pw->pw_dir, pw->pw_name);
119
120 /* start mail checking thread */
121 if (CHECK_MAIL != 0)
122 pthread_create(&mail_thread, NULL, (void*)&mail_thread_f, &mailboxRoot);
123
124 /* lock this sucka up */
125 while (error) {
126 stall();
127 /* authenticate the user */
128 pam_active = 1;
129 error = authenticate(pw->pw_name);
130 pam_active = 0;
131 /* on invalid passwd */
132 if (error) {
133 printf("\a** Invalid password **\n");
134 /* mail was found during authentication... */
135 if ((!notified) && (mail_found)) {
136 printf("\a\n You have new mail in %.200s.\n", found_in);
137 notified = 1;
138 }
139 }
140 } /* while */
141
142 /* mail was found during authentication... */
143 if ((!notified) && (mail_found))
144 printf("\a\n You have new mail in %.200s.\n", found_in);
145 salutations();
146 free_mailboxes(mailboxRoot);
147 }
148
149 /* Authenticate Password */
150 short authenticate(char *username) {
151 short error;
152 pam_handle_t *pamh = NULL;
153
154 /* start PAM session for 'away'. *
155 * the session name is what PAM looks for *
156 * in /etc/pam.d/ or /etc/pam.config */
157 error = pam_start("away", username, &conv, &pamh);
158
159 /* authenticate user */
160 if (!error) error = pam_authenticate(pamh, 0);
161
162 /* close PAM session */
163 if (pam_end(pamh,error) != PAM_SUCCESS) {
164 pamh = NULL;
165 fprintf(stderr,"away: ** failed to release PAM authenticator\n");
166 error = 1;
167 }
168 return error;
169 }
170
171 /* Make a Path */
172 char *make_path(char *dirs, char *filename) {
173 unsigned int size = 8;
174 char *buffer = (char *) malloc (size),
175 *last = strrchr(dirs, '/'), *format = NULL;
176
177 if (last != 0 && strlen(last) == 1) format = "%s%s";
178 else format = "%s/%s";
179
180 while (1) {
181 /* try to print in the allocated space. */
182 int nchars = snprintf (buffer, size, format, dirs, filename);
183 /* if that worked, return the string. */
184 if (nchars < size) return buffer;
185 /* else try again with twice as much space. */
186 size *= 2;
187 buffer = (char *) realloc (buffer,size);
188 }
189 }
190
191 /* Version */
192 void print_version(void) {
193 printf("away " VERSION "\n");
194 printf("Copyright (c) 1999-2002 Cameron Moore and others.\n");
195 printf("This software comes with NO WARRANTY, to the extent permitted by law.\n");
196 printf("You may redistribute copies of this software under the terms of the\n");
197 printf("GNU General Public License. For more information about these matters,\n");
198 printf("see the file named COPYING distributed with this software.\n\n");
199 printf("To contact the developers, please send mail to <" MLIST ">.\n");
200 exit (0);
201 }
202
203 /* Short Help */
204 void short_help(char *argv0) {
205 fprintf(stderr,"Usage: %s [OPTIONS] message [...]\n", argv0);
206 exit (0);
207 }
208
209 /* Extended Help */
210 void ext_help(char *argv0) {
211 printf("Usage: %s [OPTIONS] message [...]\n", argv0);
212 printf("A terminal locking program.\n\n");
213 printf(" -c, --mail enable checking of mail\n");
214 printf(" -C, --nomail disable checking of mail\n");
215 printf(" -f, --rcfile=FILE specifies an alternative configuration file\n");
216 printf(" -F, --norcfile ignore user configuration file\n");
217 printf(" -h, --help display this help information\n");
218 printf(" -m, --message execute showing only the message passed\n");
219 printf(" to the program\n");
220 printf(" -p, --persist continue checking for new mail as long as\n");
221 printf(" there is at least one mailbox that has not\n");
222 printf(" recieved new mail\n");
223 printf(" -P, --nopersist stop checking mail if any mailbox is found\n");
224 printf(" to have new mail\n");
225 printf(" -t, --time=SECONDS sets the time interval by which the program\n");
226 printf(" performs its background tasks\n");
227 printf(" -T, --notime ignore any options to set the time interval\n");
228 printf(" and use the default of %d seconds\n",
229 TIME);
230 printf(" -v, --version display version copyright information\n\n");
231 printf("Report bugs to " MLIST ".\n");
232 exit (0);
233 }
234
235 /* Stall */
236 void stall(void) {
•237 struct termios old, new;
238
239 printf("\n You went away at %.20s", make_time());
240 printf("\n\n -- Press [Enter] to come back online --\n");
•241
242 /* save term state */
243 tcgetattr(0, &old);
244 new = old;
245
246 /* turn off echo */
247 new.c_lflag &= ~ECHO;
248 tcsetattr(0, TCSANOW, &new);
249 getchar();
•250
251 /* reset term */
252 tcsetattr(0, TCSANOW, &old);
253 printf("\n");
254 }
255
256 /* Get Current Time */
257 char *make_time(void) {
258 unsigned int size = 8;
259 char *buffer = (char *) malloc (size);
260 time_t t = time(NULL);
261 struct tm *thetime = localtime(&t);
262
263 while (1) {
264 /* try to print in the allocated space. */
265 int nchars = snprintf (buffer, size, "%.02d:%.02d:%.02d",
266 thetime->tm_hour, thetime->tm_min, thetime->tm_sec);
267 /* if that worked, return the string. */
268 if (nchars < size) return buffer;
269 /* else try again with twice as much space. */
270 size *= 2;
271 buffer = (char *) realloc (buffer,size);
272 }
273 }
274
275 /* Salutations */
276 void salutations(void) {
277 printf("\n Welcome back. It's %.20s\n\n", make_time());
278 }
279
280 /* Check for New Mail */
281 short new_mail(Mailbox *mb) {
282 struct stat status;
283 time_t mailread = 0, mailrecv = 0;
284
285 if (stat(mb->path, &status) < 0) {
286 /* couldn't stat */
287 if (!pam_active) {
288 if (access(mb->path, F_OK) == -1)
289 fprintf(stderr, "Could not open %s: file does not exist\n", mb->path);
290 else if (access(mb->path, R_OK) == -1)
291 fprintf(stderr, "Could not open %s: access denied\n", mb->path);
292 else
293 fprintf(stderr, "Could not open %s\n", mb->path);
294 }
295 mail_found = 0;
296 } else if (status.st_size != 0) {
297 mailrecv = status.st_mtime;
298 mailread = status.st_atime;
299 }
300
301 if ((mailread < mailrecv) &&
302 (mb->mtime != mailrecv) &&
303 (mb->atime != mailread))
304 {
305 mb->mtime = mailrecv;
306 mb->atime = mailread;
307 return mail_found = 1;
308 } else {
309 return mail_found = 0;
310 }
311 }
312
313 /* Mail Thread Function */
314 void mail_thread_f(Mailbox **root) {
315 Mailbox *mb;
316 short slept = 0;
317
318 while (1) {
319 mb = *root;
320 while (mb != NULL && (!mail_found || PERSIST)) {
321 if (new_mail(mb)) {
322 found_in = mb->desc;
323 /* make sure the main process has *
324 * time to outputs its stuff... */
325 if (!slept) { sleep(1); slept++; }
326 if (!pam_active) {
327 printf("\a\n You have new mail in %.200s.\n", found_in);
328 notified = 1;
329 }
330 }
331 mb = mb->next;
332 } /* while */
333 if (mail_found && !PERSIST) break;
334 sleep(TIME);
335 }
336
337 /* exit the thread */
338 pthread_exit(NULL);
339 }
340
341 /* Get CmdCode */
342 static CmdCodes get_opcode(const char *cp, const char *filename, int linenum) {
343 unsigned int i;
344 for (i = 0; commands[i].name; i++)
345 if (strcasecmp(cp, commands[i].name) == 0)
346 return commands[i].opcode;
347 fprintf(stderr, "%.200s: line %d: Bad configuration option: %.200s\n",
348 filename, linenum, cp);
349 return oBadCmd;
350 }
351
352 /* Allocate New Link Node */
353 static void *my_malloc(int n) {
354 void *p = malloc( (size_t)n );
355 if (p == NULL) {
356 fprintf(stderr, "Out of Memory");
357 exit(1);
358 } return p;
359 }
360
361 /* Setup New Mailbox Container */
362 static Mailbox *make_cell(void) {
363 Mailbox *m = (Mailbox *) my_malloc(sizeof(Mailbox));
364 m->path = m->desc = NULL;
365 m->next = NULL;
366 return m;
367 }
368
369 /* Find New Node and Set String Values */
370 static Mailbox *snocString(Mailbox *root, char *path, char *desc) {
371 if (root == NULL) {
372 Mailbox *tmp = make_cell();
373 tmp->path = (char *) my_malloc(5 + strlen(path));
374 tmp->desc = (char *) my_malloc(5 + strlen(desc));
375 strcpy(tmp->path, path);
376 strcpy(tmp->desc, desc);
377 return tmp;
378 } else {
379 Mailbox *tmp = root;
380 while (tmp->next != NULL) tmp = tmp->next;
381 tmp->next = snocString(tmp->next, path, desc);
382 return root;
383 }
384 }
385
386 /* Add New Mailbox to List */
387 static void add_mailbox(Mailbox **list, char *path, char *desc) {
388 if (path != NULL) {
389 int i, j, k, gotPath = 0;
390 char tmpPath[FILE_NAME_LEN], tmpDesc[FILE_NAME_LEN];
391
392 i = 0;
393 while (1) {
394 if (path[i] == 0) break;
395 path += i;
396 i = 0;
397 while (isspace((int)(path[0]))) path++;
398 while (path[i] != 0 && !isspace((int)(path[i]))) i++;
399 if (i > 0) {
400 k = i; if (k > FILE_NAME_LEN-10) k = FILE_NAME_LEN-10;
401 for (j = 0; j < k; j++) tmpPath[j] = path[j];
402 tmpPath[k] = 0;
403 gotPath = 1;
404 }
405 }
406 i = 0;
407 while (gotPath) {
408 if (desc[i] == 0) break;
409 desc += i;
410 i = 0;
411 while (isspace((int)(desc[0]))) desc++;
412 while (desc[i] != 0) i++;
413 if (i > 0) {
414 k = i; if (k > FILE_NAME_LEN-10) k = FILE_NAME_LEN-10;
415 for (j = 0; j < k; j++) tmpDesc[j] = desc[j];
416 tmpDesc[k] = 0;
417 APPEND_MAILBOX(*list, tmpPath, tmpDesc);
418 }
419 }
420 }
421 }
422
423 /* Set Defaults */
424 void set_defaults(Mailbox **root, char *name) {
425 add_mailbox(root, make_path(_PATH_MAILDIR, name), "INBOX"); }
426
427 /* Read Configuration File */
428 void read_config(Mailbox **root, char *homedir, char *username) {
429 FILE *f = NULL;
430 char line[1024], filename[256];
431 char *cp = NULL, *maildir = "";
432 unsigned int linenum = 0, opcode = 0;
433 short changed_mail = 0, changed_persist = 0, changed_time = 0;
434
435 if (RCFILE_OP)
436 snprintf(filename, sizeof filename, "%.200s", rcfile);
437 else
438 snprintf(filename, sizeof filename, "%.100s/%.100s", homedir,rcfile);
439
440 /* check for existance of conf file */
441 if (access(filename, F_OK) == -1) {
442 if (RCFILE_OP) {
443 fprintf(stderr, "Could not open %s: file does not exist\n", filename);
444 }
445 set_defaults(root, username);
446 return;
447 }
448
449 /* check for existance of conf file */
450 if (access(filename, R_OK) == -1) {
451 fprintf(stderr, "Could not open %s: access denied\n", filename);
452 set_defaults(root, username);
453 return;
454 }
455
456 /* open rc file */
457 f = fopen(filename, "r");
458 if (!f) {
459 set_defaults(root, username);
460 return;
461 }
462
463 /* go line by line */
464 while (fgets(line, sizeof(line), f)) {
465 linenum++;
466 /* skip whitespace */
467 cp = line + strspn(line, WHITESPACE);
468 if (!*cp || *cp == '\n' || *cp == '#');
469 else {
470 /* get opcode */
471 cp = strtok(cp, WHITESPACE);
472 opcode = get_opcode(cp, filename, linenum);
473
474 switch (opcode) {
475 case oMail:
476 /* if changed from command line */
477 if (MAIL_OP) {
478 cp = strtok(NULL, "\n");
479 break;
480 }
481 if (changed_mail) {
482 cp = strtok(NULL, WHITESPACE);
483 fprintf(stderr,"%s line %d: multiple Mail commands.\n",
484 filename, linenum);
485 } else {
486 cp = strtok(NULL, WHITESPACE);
487 if (!cp)
488 fprintf(stderr,"%s line %d: missing argument.\n",
489 filename, linenum);
490 else if (strcasecmp(cp,"yes") == 0 || atoi(cp) == 1)
491 CHECK_MAIL = 1;
492 else if (strcasecmp(cp,"no") == 0 || atoi(cp) == 0)
493 CHECK_MAIL = 0;
494 else {
495 fprintf(stderr,
496 "%s line %d: Invalid Mail argument (%.200s).\n",
497 filename, linenum, cp);
498 }
499 changed_mail = 1;
500 }
501 break;
502
503 case oMaildir:
504 if (!CHECK_MAIL) {
505 cp = strtok(NULL, "\n");
506 break;
507 }
508 /* save the maildir for building paths to mailboxes */
509 cp = strtok(NULL, WHITESPACE);
510 if (!cp)
511 fprintf(stderr,"%s line %d: missing argument.\n",
512 filename, linenum);
513 else if (strcmp(cp,maildir) != 0)
514 if (cp[0] == '~') {
515 /* handle references to home dir as ~, ~foo, etc. */
516 char *save = NULL;
517 cp = strtok_r(cp,"~",&save);
518 if (strstr(cp, "/") != NULL)
519 maildir = strtok_r(cp, "/",&save);
520 else maildir = cp;
521 if (maildir != NULL && strcmp(maildir,username) == 0)
522 cp = strcat(cp=strdup("/"), save);
523 if (cp == NULL) cp = homedir;
524 else cp = strcat(homedir, cp);
525 }
526 maildir = make_path(cp,"");
527 break;
528
529 case oMailbox:
530 if (!CHECK_MAIL) {
531 cp = strtok(NULL, "\n");
532 break;
533 }
534 /* append mailbox to maildir and save */
535 cp = strtok(NULL, WHITESPACE);
536 if (!cp)
537 fprintf(stderr,"%s line %d: missing argument.\n",
538 filename, linenum);
539 else if (strcmp(maildir,"") != 0) {
540 char *desc = strtok(NULL, "\n");
541 if (!desc) desc = strdup(cp);
542 else if (strstr(desc,"{") && strstr(desc,"}")) {
543 if (*desc == '{') {
544 desc++;
545 desc = strtok(desc,"}");
546 } else {
547 strtok(desc,"{}");
548 desc = strtok(NULL,"}");
549 }
550 } else if (strlen(desc) == strspn(desc, WHITESPACE)) {
551 desc = strdup(cp);
552 } else {
553 fprintf(stderr,"%s line %d: garbage at end of line.\n",
554 filename, linenum);
555 desc = strdup(cp);
556 }
557 add_mailbox(root, make_path(maildir,cp), desc);
558 } else {
559 fprintf(stderr,
560 "%s line %d: Mailbox option needs a Maildir parent.\n",
561 filename, linenum);
562 }
563 break;
564
565 case oPersist:
566 /* if changed from command line */
567 if (PERSIST_OP) {
568 cp = strtok(NULL, "\n");
569 break;
570 }
571 if (changed_persist) {
572 cp = strtok(NULL, WHITESPACE);
573 fprintf(stderr,"%s line %d: multiple Persist commands.\n",
574 filename, linenum);
575 } else {
576 cp = strtok(NULL, WHITESPACE);
577 if (!cp)
578 fprintf(stderr,"%s line %d: missing argument.\n",
579 filename, linenum);
580 else if (strcasecmp(cp,"yes") == 0 || atoi(cp) == 1)
581 PERSIST = 1;
582 else if (strcasecmp(cp,"no") == 0 || atoi(cp) == 0)
583 PERSIST = 0;
584 else {
585 fprintf(stderr,
586 "%s line %d: Invalid Persist argument (%.200s).\n",
587 filename, linenum, cp);
588 }
589 changed_persist = 1;
590 }
591 break;
592
593 case oTime:
594 /* if changed from command line */
595 if (TIME_OP) {
596 cp = strtok(NULL, "\n");
597 break;
598 }
599 /* change TIME */
600 if (changed_time) {
601 cp = strtok(NULL, WHITESPACE);
602 fprintf(stderr,"%s line %d: multiple Time commands.\n",
603 filename, linenum);
604 } else {
605 cp = strtok(NULL, WHITESPACE);
606 if (!cp)
607 fprintf(stderr,"%s line %d: missing argument.\n",
608 filename, linenum);
609 else if (atoi(cp) >= MIN_TIME) TIME = atoi(cp);
610 else {
611 fprintf(stderr,
612 "%s line %d: Time value less than minimum (%d).\n",
613 filename, linenum, MIN_TIME);
614 TIME = MIN_TIME;
615 }
616 changed_time = 1;
617 }
618 break;
619
620 default:
621 break;
622 } /* switch */
623
624 /* check for garbage at EOL */
625 if (strtok(NULL, WHITESPACE) != NULL)
626 fprintf(stderr,"%s line %d: garbage at end of line.\n",
627 filename, linenum);
628 } /* else */
629 } /* while */
630 fclose(f);
631
632 /* if no mailboxes where setup, add system inbox */
633 if (CHECK_MAIL && root == NULL) { set_defaults(root, username); }
634 else if (!CHECK_MAIL) { free_mailboxes(*root); }
635 }
636
637 /* Re-execute */
638 void re_exec(int argc, char *argv[], int opt_cnt, short as_mesg) {
639 /* preserve location of binary */
640 char *original = argv[0];
641 /* create new argv */
642 char *newargv[argc];
643
644 int i = (as_mesg ? 0 : 1);
645 for (; i < argc-(opt_cnt+as_mesg); i++)
646 newargv[i] = argv[i+(opt_cnt+as_mesg)];
647
648 /* keep original exec name */
649 if (!as_mesg) { newargv[0] = argv[0]; }
650
651 /* pad with space to make use of one word arguments to -m */
652 newargv[argc-(opt_cnt+as_mesg)] = " ";
653 i = opt_cnt;// - (as_mesg ? 1 : 0);
654 for(; i > 0; i--)
655 newargv[argc-i] = NULL;
656
657 /* exec THIS! */
658 execvp(original, newargv);
659 }
660
661 /* check ENV for options */
662 void check_env(void) {
663 if (getenv(AWAY_RCFILE)) {
664 rcfile = getenv(AWAY_RCFILE);
665 RCFILE_OP = 1;
666 unsetenv(AWAY_RCFILE);
667 }
668
669 if (getenv(AWAY_NOTIME)) {
670 TIME_OP = 1;
671 unsetenv(AWAY_NOTIME);
672 } else if (getenv(AWAY_TIME)) {
673 if (atoi(getenv(AWAY_TIME)) >= MIN_TIME) {
674 TIME = atoi(getenv(AWAY_TIME));
675 TIME_OP = 1;
676 }
677 unsetenv(AWAY_TIME);
678 }
679
680 if (getenv(AWAY_PERSIST)) {
681 PERSIST = atoi(getenv(AWAY_PERSIST));
682 PERSIST_OP = 1;
683 unsetenv(AWAY_PERSIST);
684 }
685
686 if (getenv(AWAY_MAIL)) {
687 CHECK_MAIL = atoi(getenv(AWAY_MAIL));
688 MAIL_OP = 1;
689 unsetenv(AWAY_MAIL);
690 }
691 }
692
693 /* free mailboxes linked list */
694 void free_mailboxes(Mailbox *root) {
695 while (root != NULL) {
696 Mailbox *next = root->next;
697 if (root->path != NULL) free(root->path);
698 free(root);
699 root = next;
700 };
701 }