Debian Codegraph @

away.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 printf("\n You went away at %.20s", make_time());
238 printf("\n\n -- Press [Enter] to come back online --\n");
239 getchar();
240 }
241
242 /* Get Current Time */
243 char *make_time(void) {
244 unsigned int size = 8;
245 char *buffer = (char *) malloc (size);
246 time_t t = time(NULL);
247 struct tm *thetime = localtime(&t);
248
249 while (1) {
250 /* try to print in the allocated space. */
251 int nchars = snprintf (buffer, size, "%.02d:%.02d:%.02d",
252 thetime->tm_hour, thetime->tm_min, thetime->tm_sec);
253 /* if that worked, return the string. */
254 if (nchars < size) return buffer;
255 /* else try again with twice as much space. */
256 size *= 2;
257 buffer = (char *) realloc (buffer,size);
258 }
259 }
260
261 /* Salutations */
262 void salutations(void) {
263 printf("\n Welcome back. It's %.20s\n\n", make_time());
264 }
265
266 /* Check for New Mail */
267 short new_mail(Mailbox *mb) {
268 struct stat status;
269 time_t mailread = 0, mailrecv = 0;
270
271 if (stat(mb->path, &status) < 0) {
272 /* couldn't stat */
273 if (!pam_active) {
274 if (access(mb->path, F_OK) == -1)
275 fprintf(stderr, "Could not open %s: file does not exist\n", mb->path);
276 else if (access(mb->path, R_OK) == -1)
277 fprintf(stderr, "Could not open %s: access denied\n", mb->path);
278 else
279 fprintf(stderr, "Could not open %s\n", mb->path);
280 }
281 mail_found = 0;
282 } else if (status.st_size != 0) {
283 mailrecv = status.st_mtime;
284 mailread = status.st_atime;
285 }
286
287 if ((mailread < mailrecv) &&
288 (mb->mtime != mailrecv) &&
289 (mb->atime != mailread))
290 {
291 mb->mtime = mailrecv;
292 mb->atime = mailread;
293 return mail_found = 1;
294 } else {
295 return mail_found = 0;
296 }
297 }
298
299 /* Mail Thread Function */
300 void mail_thread_f(Mailbox **root) {
301 Mailbox *mb;
302 short slept = 0;
303
304 while (1) {
305 mb = *root;
306 while (mb != NULL && (!mail_found || PERSIST)) {
307 if (new_mail(mb)) {
308 found_in = mb->desc;
309 /* make sure the main process has *
310 * time to outputs its stuff... */
311 if (!slept) { sleep(1); slept++; }
312 if (!pam_active) {
313 printf("\a\n You have new mail in %.200s.\n", found_in);
314 notified = 1;
315 }
316 }
317 mb = mb->next;
318 } /* while */
319 if (mail_found && !PERSIST) break;
320 sleep(TIME);
321 }
322
323 /* exit the thread */
324 pthread_exit(NULL);
325 }
326
327 /* Get CmdCode */
328 static CmdCodes get_opcode(const char *cp, const char *filename, int linenum) {
329 unsigned int i;
330 for (i = 0; commands[i].name; i++)
331 if (strcasecmp(cp, commands[i].name) == 0)
332 return commands[i].opcode;
333 fprintf(stderr, "%.200s: line %d: Bad configuration option: %.200s\n",
334 filename, linenum, cp);
335 return oBadCmd;
336 }
337
338 /* Allocate New Link Node */
339 static void *my_malloc(int n) {
340 void *p = malloc( (size_t)n );
341 if (p == NULL) {
342 fprintf(stderr, "Out of Memory");
343 exit(1);
344 } return p;
345 }
346
347 /* Setup New Mailbox Container */
348 static Mailbox *make_cell(void) {
349 Mailbox *m = (Mailbox *) my_malloc(sizeof(Mailbox));
350 m->path = m->desc = NULL;
351 m->next = NULL;
352 return m;
353 }
354
355 /* Find New Node and Set String Values */
356 static Mailbox *snocString(Mailbox *root, char *path, char *desc) {
357 if (root == NULL) {
358 Mailbox *tmp = make_cell();
359 tmp->path = (char *) my_malloc(5 + strlen(path));
360 tmp->desc = (char *) my_malloc(5 + strlen(desc));
361 strcpy(tmp->path, path);
362 strcpy(tmp->desc, desc);
363 return tmp;
364 } else {
365 Mailbox *tmp = root;
366 while (tmp->next != NULL) tmp = tmp->next;
367 tmp->next = snocString(tmp->next, path, desc);
368 return root;
369 }
370 }
371
372 /* Add New Mailbox to List */
373 static void add_mailbox(Mailbox **list, char *path, char *desc) {
374 if (path != NULL) {
375 int i, j, k, gotPath = 0;
376 char tmpPath[FILE_NAME_LEN], tmpDesc[FILE_NAME_LEN];
377
378 i = 0;
379 while (1) {
380 if (path[i] == 0) break;
381 path += i;
382 i = 0;
383 while (isspace((int)(path[0]))) path++;
384 while (path[i] != 0 && !isspace((int)(path[i]))) i++;
385 if (i > 0) {
386 k = i; if (k > FILE_NAME_LEN-10) k = FILE_NAME_LEN-10;
387 for (j = 0; j < k; j++) tmpPath[j] = path[j];
388 tmpPath[k] = 0;
389 gotPath = 1;
390 }
391 }
392 i = 0;
393 while (gotPath) {
394 if (desc[i] == 0) break;
395 desc += i;
396 i = 0;
397 while (isspace((int)(desc[0]))) desc++;
398 while (desc[i] != 0) 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++) tmpDesc[j] = desc[j];
402 tmpDesc[k] = 0;
403 APPEND_MAILBOX(*list, tmpPath, tmpDesc);
404 }
405 }
406 }
407 }
408
409 /* Set Defaults */
410 void set_defaults(Mailbox **root, char *name) {
411 add_mailbox(root, make_path(_PATH_MAILDIR, name), "INBOX"); }
412
413 /* Read Configuration File */
414 void read_config(Mailbox **root, char *homedir, char *username) {
415 FILE *f = NULL;
416 char line[1024], filename[256];
417 char *cp = NULL, *maildir = "";
418 unsigned int linenum = 0, opcode = 0;
419 short changed_mail = 0, changed_persist = 0, changed_time = 0;
420
421 if (RCFILE_OP)
422 snprintf(filename, sizeof filename, "%.200s", rcfile);
423 else
424 snprintf(filename, sizeof filename, "%.100s/%.100s", homedir,rcfile);
425
426 /* check for existance of conf file */
427 if (access(filename, F_OK) == -1) {
428 if (RCFILE_OP) {
429 fprintf(stderr, "Could not open %s: file does not exist\n", filename);
430 }
431 set_defaults(root, username);
432 return;
433 }
434
435 /* check for existance of conf file */
436 if (access(filename, R_OK) == -1) {
437 fprintf(stderr, "Could not open %s: access denied\n", filename);
438 set_defaults(root, username);
439 return;
440 }
441
442 /* open rc file */
443 f = fopen(filename, "r");
444 if (!f) {
445 set_defaults(root, username);
446 return;
447 }
448
449 /* go line by line */
450 while (fgets(line, sizeof(line), f)) {
451 linenum++;
452 /* skip whitespace */
453 cp = line + strspn(line, WHITESPACE);
454 if (!*cp || *cp == '\n' || *cp == '#');
455 else {
456 /* get opcode */
457 cp = strtok(cp, WHITESPACE);
458 opcode = get_opcode(cp, filename, linenum);
459
460 switch (opcode) {
461 case oMail:
462 /* if changed from command line */
463 if (MAIL_OP) {
464 cp = strtok(NULL, "\n");
465 break;
466 }
467 if (changed_mail) {
468 cp = strtok(NULL, WHITESPACE);
469 fprintf(stderr,"%s line %d: multiple Mail commands.\n",
470 filename, linenum);
471 } else {
472 cp = strtok(NULL, WHITESPACE);
473 if (!cp)
474 fprintf(stderr,"%s line %d: missing argument.\n",
475 filename, linenum);
476 else if (strcasecmp(cp,"yes") == 0 || atoi(cp) == 1)
477 CHECK_MAIL = 1;
478 else if (strcasecmp(cp,"no") == 0 || atoi(cp) == 0)
479 CHECK_MAIL = 0;
480 else {
481 fprintf(stderr,
482 "%s line %d: Invalid Mail argument (%.200s).\n",
483 filename, linenum, cp);
484 }
485 changed_mail = 1;
486 }
487 break;
488
489 case oMaildir:
490 if (!CHECK_MAIL) {
491 cp = strtok(NULL, "\n");
492 break;
493 }
494 /* save the maildir for building paths to mailboxes */
495 cp = strtok(NULL, WHITESPACE);
496 if (!cp)
497 fprintf(stderr,"%s line %d: missing argument.\n",
498 filename, linenum);
499 else if (strcmp(cp,maildir) != 0)
500 if (cp[0] == '~') {
501 /* handle references to home dir as ~, ~foo, etc. */
502 char *save = NULL;
503 cp = strtok_r(cp,"~",&save);
504 if (strstr(cp, "/") != NULL)
505 maildir = strtok_r(cp, "/",&save);
506 else maildir = cp;
507 if (maildir != NULL && strcmp(maildir,username) == 0)
508 cp = strcat(cp=strdup("/"), save);
509 if (cp == NULL) cp = homedir;
510 else cp = strcat(homedir, cp);
511 }
512 maildir = make_path(cp,"");
513 break;
514
515 case oMailbox:
516 if (!CHECK_MAIL) {
517 cp = strtok(NULL, "\n");
518 break;
519 }
520 /* append mailbox to maildir and save */
521 cp = strtok(NULL, WHITESPACE);
522 if (!cp)
523 fprintf(stderr,"%s line %d: missing argument.\n",
524 filename, linenum);
525 else if (strcmp(maildir,"") != 0) {
526 char *desc = strtok(NULL, "\n");
527 if (!desc) desc = strdup(cp);
528 else if (strstr(desc,"{") && strstr(desc,"}")) {
529 if (*desc == '{') {
530 desc++;
531 desc = strtok(desc,"}");
532 } else {
533 strtok(desc,"{}");
534 desc = strtok(NULL,"}");
535 }
536 } else if (strlen(desc) == strspn(desc, WHITESPACE)) {
537 desc = strdup(cp);
538 } else {
539 fprintf(stderr,"%s line %d: garbage at end of line.\n",
540 filename, linenum);
541 desc = strdup(cp);
542 }
543 add_mailbox(root, make_path(maildir,cp), desc);
544 } else {
545 fprintf(stderr,
546 "%s line %d: Mailbox option needs a Maildir parent.\n",
547 filename, linenum);
548 }
549 break;
550
551 case oPersist:
552 /* if changed from command line */
553 if (PERSIST_OP) {
554 cp = strtok(NULL, "\n");
555 break;
556 }
557 if (changed_persist) {
558 cp = strtok(NULL, WHITESPACE);
559 fprintf(stderr,"%s line %d: multiple Persist commands.\n",
560 filename, linenum);
561 } else {
562 cp = strtok(NULL, WHITESPACE);
563 if (!cp)
564 fprintf(stderr,"%s line %d: missing argument.\n",
565 filename, linenum);
566 else if (strcasecmp(cp,"yes") == 0 || atoi(cp) == 1)
567 PERSIST = 1;
568 else if (strcasecmp(cp,"no") == 0 || atoi(cp) == 0)
569 PERSIST = 0;
570 else {
571 fprintf(stderr,
572 "%s line %d: Invalid Persist argument (%.200s).\n",
573 filename, linenum, cp);
574 }
575 changed_persist = 1;
576 }
577 break;
578
579 case oTime:
580 /* if changed from command line */
581 if (TIME_OP) {
582 cp = strtok(NULL, "\n");
583 break;
584 }
585 /* change TIME */
586 if (changed_time) {
587 cp = strtok(NULL, WHITESPACE);
588 fprintf(stderr,"%s line %d: multiple Time commands.\n",
589 filename, linenum);
590 } else {
591 cp = strtok(NULL, WHITESPACE);
592 if (!cp)
593 fprintf(stderr,"%s line %d: missing argument.\n",
594 filename, linenum);
595 else if (atoi(cp) >= MIN_TIME) TIME = atoi(cp);
596 else {
597 fprintf(stderr,
598 "%s line %d: Time value less than minimum (%d).\n",
599 filename, linenum, MIN_TIME);
600 TIME = MIN_TIME;
601 }
602 changed_time = 1;
603 }
604 break;
605
606 default:
607 break;
608 } /* switch */
609
610 /* check for garbage at EOL */
611 if (strtok(NULL, WHITESPACE) != NULL)
612 fprintf(stderr,"%s line %d: garbage at end of line.\n",
613 filename, linenum);
614 } /* else */
615 } /* while */
616 fclose(f);
617
618 /* if no mailboxes where setup, add system inbox */
619 if (CHECK_MAIL && root == NULL) { set_defaults(root, username); }
620 else if (!CHECK_MAIL) { free_mailboxes(*root); }
621 }
622
623 /* Re-execute */
624 void re_exec(int argc, char *argv[], int opt_cnt, short as_mesg) {
625 /* preserve location of binary */
626 char *original = argv[0];
627 /* create new argv */
628 char *newargv[argc];
629
630 int i = (as_mesg ? 0 : 1);
631 for (; i < argc-(opt_cnt+as_mesg); i++)
632 newargv[i] = argv[i+(opt_cnt+as_mesg)];
633
634 /* keep original exec name */
635 if (!as_mesg) { newargv[0] = argv[0]; }
636
637 /* pad with space to make use of one word arguments to -m */
638 newargv[argc-(opt_cnt+as_mesg)] = " ";
639 i = opt_cnt;// - (as_mesg ? 1 : 0);
640 for(; i > 0; i--)
641 newargv[argc-i] = NULL;
642
643 /* exec THIS! */
644 execvp(original, newargv);
645 }
646
647 /* check ENV for options */
648 void check_env(void) {
649 if (getenv(AWAY_RCFILE)) {
650 rcfile = getenv(AWAY_RCFILE);
651 RCFILE_OP = 1;
652 unsetenv(AWAY_RCFILE);
653 }
654
655 if (getenv(AWAY_NOTIME)) {
656 TIME_OP = 1;
657 unsetenv(AWAY_NOTIME);
658 } else if (getenv(AWAY_TIME)) {
659 if (atoi(getenv(AWAY_TIME)) >= MIN_TIME) {
660 TIME = atoi(getenv(AWAY_TIME));
661 TIME_OP = 1;
662 }
663 unsetenv(AWAY_TIME);
664 }
665
666 if (getenv(AWAY_PERSIST)) {
667 PERSIST = atoi(getenv(AWAY_PERSIST));
668 PERSIST_OP = 1;
669 unsetenv(AWAY_PERSIST);
670 }
671
672 if (getenv(AWAY_MAIL)) {
673 CHECK_MAIL = atoi(getenv(AWAY_MAIL));
674 MAIL_OP = 1;
675 unsetenv(AWAY_MAIL);
676 }
677 }
678
679 /* free mailboxes linked list */
680 void free_mailboxes(Mailbox *root) {
681 while (root != NULL) {
682 Mailbox *next = root->next;
683 if (root->path != NULL) free(root->path);
684 free(root);
685 root = next;
686 };
687 }