[BACK]Return to calendar.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / calendar

Annotation of src/usr.bin/calendar/calendar.c, Revision 1.25

1.25    ! deraadt     1: /*     $OpenBSD: calendar.c,v 1.24 2005/08/09 12:32:58 mickey Exp $    */
1.1       deraadt     2:
                      3: /*
                      4:  * Copyright (c) 1989, 1993, 1994
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.20      millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #ifndef lint
1.5       millert    33: static const char copyright[] =
1.1       deraadt    34: "@(#) Copyright (c) 1989, 1993\n\
                     35:        The Regents of the University of California.  All rights reserved.\n";
                     36: #endif /* not lint */
                     37:
                     38: #ifndef lint
                     39: #if 0
1.5       millert    40: static const char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
                     41: #else
1.25    ! deraadt    42: static const char rcsid[] = "$OpenBSD: calendar.c,v 1.24 2005/08/09 12:32:58 mickey Exp $";
1.1       deraadt    43: #endif
                     44: #endif /* not lint */
                     45:
1.15      pjanzen    46: #include <sys/stat.h>
                     47: #include <sys/types.h>
                     48: #include <sys/wait.h>
1.1       deraadt    49: #include <err.h>
                     50: #include <errno.h>
1.5       millert    51: #include <locale.h>
1.15      pjanzen    52: #include <login_cap.h>
1.1       deraadt    53: #include <pwd.h>
1.15      pjanzen    54: #include <signal.h>
1.1       deraadt    55: #include <stdio.h>
                     56: #include <stdlib.h>
                     57: #include <string.h>
1.5       millert    58: #include <time.h>
1.12      pjanzen    59: #include <tzfile.h>
1.1       deraadt    60: #include <unistd.h>
                     61:
                     62: #include "pathnames.h"
1.5       millert    63: #include "calendar.h"
1.1       deraadt    64:
1.15      pjanzen    65: char *calendarFile = "calendar";  /* default calendar file */
                     66: char *calendarHome = ".calendar"; /* HOME */
                     67: char *calendarNoMail = "nomail";  /* don't sent mail if this file exists */
                     68:
1.1       deraadt    69: struct passwd *pw;
1.5       millert    70: int doall = 0;
                     71: time_t f_time = 0;
1.16      mickey     72: int bodun_always = 0;
1.1       deraadt    73:
1.5       millert    74: int f_dayAfter = 0; /* days after current date */
                     75: int f_dayBefore = 0; /* days before current date */
1.24      mickey     76: int f_SetdayAfter = 0; /* calendar invoked with -A */
1.1       deraadt    77:
1.12      pjanzen    78: struct specialev spev[NUMEV];
                     79:
1.17      millert    80: void childsig(int);
1.15      pjanzen    81:
1.1       deraadt    82: int
1.21      deraadt    83: main(int argc, char *argv[])
1.1       deraadt    84: {
1.15      pjanzen    85:        int ch;
1.1       deraadt    86:        char *caldir;
                     87:
1.12      pjanzen    88:        (void)setlocale(LC_ALL, "");
1.5       millert    89:
1.19      millert    90:        while ((ch = getopt(argc, argv, "abf:t:A:B:-")) != -1)
1.1       deraadt    91:                switch (ch) {
                     92:                case '-':               /* backward contemptible */
                     93:                case 'a':
1.5       millert    94:                        if (getuid())
1.13      millert    95:                                errx(1, "%s", strerror(EPERM));
1.1       deraadt    96:                        doall = 1;
                     97:                        break;
1.5       millert    98:
1.16      mickey     99:                case 'b':
                    100:                        bodun_always++;
                    101:                        break;
                    102:
1.5       millert   103:                case 'f': /* other calendar file */
                    104:                        calendarFile = optarg;
                    105:                        break;
                    106:
                    107:                case 't': /* other date, undocumented, for tests */
1.12      pjanzen   108:                        if ((f_time = Mktime(optarg)) <= 0)
1.11      pjanzen   109:                                errx(1, "specified date is outside allowed range");
1.5       millert   110:                        break;
                    111:
                    112:                case 'A': /* days after current date */
                    113:                        f_dayAfter = atoi(optarg);
1.24      mickey    114:                        f_SetdayAfter = 1;
1.5       millert   115:                        break;
                    116:
                    117:                case 'B': /* days before current date */
                    118:                        f_dayBefore = atoi(optarg);
                    119:                        break;
                    120:
1.1       deraadt   121:                default:
                    122:                        usage();
                    123:                }
                    124:        argc -= optind;
                    125:        argv += optind;
                    126:
                    127:        if (argc)
                    128:                usage();
                    129:
1.5       millert   130:        /* use current time */
                    131:        if (f_time <= 0)
                    132:            (void)time(&f_time);
                    133:
1.12      pjanzen   134:        if (f_dayBefore) {
                    135:                /* Move back in time and only look forwards */
                    136:                f_dayAfter += f_dayBefore;
                    137:                f_time -= SECSPERDAY * f_dayBefore;
                    138:                f_dayBefore = 0;
                    139:        }
                    140:        settime(&f_time);
1.5       millert   141:
1.10      millert   142:        if (doall) {
1.15      pjanzen   143:                pid_t kid, deadkid;
                    144:                int kidstat, kidreaped, runningkids;
                    145:                int acstat;
                    146:                struct stat sbuf;
                    147:                time_t t;
                    148:                unsigned int sleeptime;
                    149:
                    150:                signal(SIGCHLD, childsig);
                    151:                runningkids = 0;
                    152:                t = time(NULL);
1.1       deraadt   153:                while ((pw = getpwent()) != NULL) {
1.15      pjanzen   154:                        acstat = 0;
                    155:                        /* Avoid unnecessary forks.  The calendar file is only
                    156:                         * opened as the user later; if it can't be opened,
                    157:                         * it's no big deal.  Also, get to correct directory.
                    158:                         * Note that in an NFS environment root may get EACCES
                    159:                         * on a chdir(), in which case we have to fork.  As long as
                    160:                         * we can chdir() we can stat(), unless the user is
                    161:                         * modifying permissions while this is running.
                    162:                         */
                    163:                        if (chdir(pw->pw_dir)) {
                    164:                                if (errno == EACCES)
                    165:                                        acstat = 1;
                    166:                                else
                    167:                                        continue;
                    168:                        }
                    169:                        if (stat(calendarFile, &sbuf) != 0) {
                    170:                                if (chdir(calendarHome)) {
                    171:                                        if (errno == EACCES)
                    172:                                                acstat = 1;
                    173:                                        else
                    174:                                                continue;
                    175:                                }
                    176:                                if (stat(calendarNoMail, &sbuf) == 0 ||
                    177:                                    stat(calendarFile, &sbuf) != 0)
                    178:                                        continue;
                    179:                        }
                    180:                        sleeptime = USERTIMEOUT;
                    181:                        switch ((kid = fork())) {
                    182:                        case -1:        /* error */
                    183:                                warn("fork");
                    184:                                continue;
                    185:                        case 0: /* child */
                    186:                                (void)setlocale(LC_ALL, "");
                    187:                                if (setusercontext(NULL, pw, pw->pw_uid,
                    188:                                    LOGIN_SETALL ^ LOGIN_SETLOGIN))
1.18      deraadt   189:                                        err(1, "unable to set user context (uid %u)",
                    190:                                            pw->pw_uid);
1.15      pjanzen   191:                                if (acstat) {
                    192:                                        if (chdir(pw->pw_dir) ||
                    193:                                            stat(calendarFile, &sbuf) != 0 ||
                    194:                                            chdir(calendarHome) ||
                    195:                                            stat(calendarNoMail, &sbuf) == 0 ||
                    196:                                            stat(calendarFile, &sbuf) != 0)
                    197:                                                exit(0);
                    198:                                }
1.1       deraadt   199:                                cal();
1.15      pjanzen   200:                                exit(0);
                    201:                        }
                    202:                        /* parent: wait a reasonable time, then kill child if
                    203:                         * necessary.
                    204:                         */
                    205:                        runningkids++;
                    206:                        kidreaped = 0;
                    207:                        do {
                    208:                                sleeptime = sleep(sleeptime);
                    209:                                /* Note that there is the possibility, if the sleep
                    210:                                 * stops early due to some other signal, of the child
                    211:                                 * terminating and not getting detected during the next
                    212:                                 * sleep.  In that unlikely worst case, we just sleep
                    213:                                 * too long for that user.
                    214:                                 */
                    215:                                for (;;) {
                    216:                                        deadkid = waitpid(-1, &kidstat, WNOHANG);
                    217:                                        if (deadkid <= 0)
                    218:                                                break;
                    219:                                        runningkids--;
                    220:                                        if (deadkid == kid) {
                    221:                                                kidreaped = 1;
                    222:                                                sleeptime = 0;
                    223:                                        }
                    224:                                }
                    225:                        } while (sleeptime);
                    226:
                    227:                        if (!kidreaped) {
                    228:                                /* It doesn't _really_ matter if the kill fails, e.g.
                    229:                                 * if there's only a zombie now.
                    230:                                 */
                    231:                                (void)kill(kid, SIGTERM);
1.18      deraadt   232:                                warnx("uid %u did not finish in time", pw->pw_uid);
1.12      pjanzen   233:                        }
1.15      pjanzen   234:                        if (time(NULL) - t >= SECSPERDAY)
1.25    ! deraadt   235:                                errx(2, "'calendar -a' took more than a day; "
        !           236:                                    "stopped at uid %u",
1.18      deraadt   237:                                    pw->pw_uid);
1.15      pjanzen   238:                }
                    239:                for (;;) {
                    240:                        deadkid = waitpid(-1, &kidstat, WNOHANG);
                    241:                        if (deadkid <= 0)
                    242:                                break;
                    243:                        runningkids--;
1.1       deraadt   244:                }
1.15      pjanzen   245:                if (runningkids)
1.25    ! deraadt   246:                        warnx("%d child processes still running when "
        !           247:                            "'calendar -a' finished", runningkids);
        !           248:        } else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
1.5       millert   249:                if(!chdir(caldir))
                    250:                        cal();
1.1       deraadt   251:        } else
                    252:                cal();
1.5       millert   253:
1.1       deraadt   254:        exit(0);
                    255: }
                    256:
                    257:
                    258: void
1.21      deraadt   259: usage(void)
1.1       deraadt   260: {
1.5       millert   261:        (void)fprintf(stderr,
1.22      jmc       262:            "usage: calendar [-ab] [-A num] [-B num] [-f calendarfile] "
                    263:            "[-t [[[cc]yy][mm]]dd]\n");
1.1       deraadt   264:        exit(1);
1.15      pjanzen   265: }
                    266:
                    267:
                    268: void
1.21      deraadt   269: childsig(int signo)
1.15      pjanzen   270: {
1.1       deraadt   271: }