[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.32

1.32    ! zhuk        1: /*     $OpenBSD: calendar.c,v 1.31 2015/04/18 18:28:37 deraadt 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:
1.15      pjanzen    32: #include <sys/stat.h>
                     33: #include <sys/types.h>
                     34: #include <sys/wait.h>
1.1       deraadt    35: #include <err.h>
                     36: #include <errno.h>
1.5       millert    37: #include <locale.h>
1.15      pjanzen    38: #include <login_cap.h>
1.1       deraadt    39: #include <pwd.h>
1.15      pjanzen    40: #include <signal.h>
1.1       deraadt    41: #include <stdio.h>
                     42: #include <stdlib.h>
                     43: #include <string.h>
1.31      deraadt    44: #include <limits.h>
1.5       millert    45: #include <time.h>
1.1       deraadt    46: #include <unistd.h>
                     47:
                     48: #include "pathnames.h"
1.5       millert    49: #include "calendar.h"
1.1       deraadt    50:
1.15      pjanzen    51: char *calendarFile = "calendar";  /* default calendar file */
                     52: char *calendarHome = ".calendar"; /* HOME */
                     53: char *calendarNoMail = "nomail";  /* don't sent mail if this file exists */
                     54:
1.1       deraadt    55: struct passwd *pw;
1.5       millert    56: int doall = 0;
                     57: time_t f_time = 0;
1.16      mickey     58: int bodun_always = 0;
1.1       deraadt    59:
1.5       millert    60: int f_dayAfter = 0; /* days after current date */
                     61: int f_dayBefore = 0; /* days before current date */
1.24      mickey     62: int f_SetdayAfter = 0; /* calendar invoked with -A */
1.1       deraadt    63:
1.12      pjanzen    64: struct specialev spev[NUMEV];
                     65:
1.17      millert    66: void childsig(int);
1.15      pjanzen    67:
1.1       deraadt    68: int
1.21      deraadt    69: main(int argc, char *argv[])
1.1       deraadt    70: {
1.15      pjanzen    71:        int ch;
1.31      deraadt    72:        const char *errstr;
1.1       deraadt    73:        char *caldir;
                     74:
1.12      pjanzen    75:        (void)setlocale(LC_ALL, "");
1.5       millert    76:
1.19      millert    77:        while ((ch = getopt(argc, argv, "abf:t:A:B:-")) != -1)
1.1       deraadt    78:                switch (ch) {
                     79:                case '-':               /* backward contemptible */
                     80:                case 'a':
1.5       millert    81:                        if (getuid())
1.13      millert    82:                                errx(1, "%s", strerror(EPERM));
1.1       deraadt    83:                        doall = 1;
                     84:                        break;
1.5       millert    85:
1.16      mickey     86:                case 'b':
1.29      deraadt    87:                        bodun_always = 1;
1.16      mickey     88:                        break;
                     89:
1.5       millert    90:                case 'f': /* other calendar file */
                     91:                        calendarFile = optarg;
                     92:                        break;
                     93:
                     94:                case 't': /* other date, undocumented, for tests */
1.12      pjanzen    95:                        if ((f_time = Mktime(optarg)) <= 0)
1.11      pjanzen    96:                                errx(1, "specified date is outside allowed range");
1.5       millert    97:                        break;
                     98:
                     99:                case 'A': /* days after current date */
1.31      deraadt   100:                        f_dayAfter = strtonum(optarg, 0, INT_MAX, &errstr);
                    101:                        if (errstr)
                    102:                                errx(1, "-A %s: %s", optarg, errstr);
1.24      mickey    103:                        f_SetdayAfter = 1;
1.5       millert   104:                        break;
                    105:
                    106:                case 'B': /* days before current date */
1.31      deraadt   107:                        f_dayBefore = strtonum(optarg, 0, INT_MAX, &errstr);
                    108:                        if (errstr)
                    109:                                errx(1, "-B %s: %s", optarg, errstr);
1.5       millert   110:                        break;
                    111:
1.1       deraadt   112:                default:
                    113:                        usage();
                    114:                }
                    115:        argc -= optind;
                    116:        argv += optind;
                    117:
                    118:        if (argc)
                    119:                usage();
                    120:
1.5       millert   121:        /* use current time */
                    122:        if (f_time <= 0)
                    123:            (void)time(&f_time);
                    124:
1.12      pjanzen   125:        if (f_dayBefore) {
                    126:                /* Move back in time and only look forwards */
                    127:                f_dayAfter += f_dayBefore;
                    128:                f_time -= SECSPERDAY * f_dayBefore;
                    129:                f_dayBefore = 0;
                    130:        }
                    131:        settime(&f_time);
1.5       millert   132:
1.10      millert   133:        if (doall) {
1.15      pjanzen   134:                pid_t kid, deadkid;
                    135:                int kidstat, kidreaped, runningkids;
                    136:                int acstat;
                    137:                struct stat sbuf;
                    138:                time_t t;
                    139:                unsigned int sleeptime;
                    140:
                    141:                signal(SIGCHLD, childsig);
                    142:                runningkids = 0;
                    143:                t = time(NULL);
1.1       deraadt   144:                while ((pw = getpwent()) != NULL) {
1.15      pjanzen   145:                        acstat = 0;
                    146:                        /* Avoid unnecessary forks.  The calendar file is only
                    147:                         * opened as the user later; if it can't be opened,
                    148:                         * it's no big deal.  Also, get to correct directory.
                    149:                         * Note that in an NFS environment root may get EACCES
                    150:                         * on a chdir(), in which case we have to fork.  As long as
                    151:                         * we can chdir() we can stat(), unless the user is
                    152:                         * modifying permissions while this is running.
                    153:                         */
                    154:                        if (chdir(pw->pw_dir)) {
                    155:                                if (errno == EACCES)
                    156:                                        acstat = 1;
                    157:                                else
                    158:                                        continue;
                    159:                        }
                    160:                        if (stat(calendarFile, &sbuf) != 0) {
                    161:                                if (chdir(calendarHome)) {
                    162:                                        if (errno == EACCES)
                    163:                                                acstat = 1;
                    164:                                        else
                    165:                                                continue;
                    166:                                }
                    167:                                if (stat(calendarNoMail, &sbuf) == 0 ||
                    168:                                    stat(calendarFile, &sbuf) != 0)
                    169:                                        continue;
                    170:                        }
                    171:                        sleeptime = USERTIMEOUT;
                    172:                        switch ((kid = fork())) {
                    173:                        case -1:        /* error */
                    174:                                warn("fork");
                    175:                                continue;
                    176:                        case 0: /* child */
1.28      otto      177:                                (void)setpgid(getpid(), getpid());
1.15      pjanzen   178:                                (void)setlocale(LC_ALL, "");
                    179:                                if (setusercontext(NULL, pw, pw->pw_uid,
                    180:                                    LOGIN_SETALL ^ LOGIN_SETLOGIN))
1.18      deraadt   181:                                        err(1, "unable to set user context (uid %u)",
                    182:                                            pw->pw_uid);
1.15      pjanzen   183:                                if (acstat) {
                    184:                                        if (chdir(pw->pw_dir) ||
                    185:                                            stat(calendarFile, &sbuf) != 0 ||
1.32    ! zhuk      186:                                            chdir(calendarHome) ||
1.15      pjanzen   187:                                            stat(calendarNoMail, &sbuf) == 0 ||
                    188:                                            stat(calendarFile, &sbuf) != 0)
                    189:                                                exit(0);
                    190:                                }
1.1       deraadt   191:                                cal();
1.15      pjanzen   192:                                exit(0);
                    193:                        }
                    194:                        /* parent: wait a reasonable time, then kill child if
                    195:                         * necessary.
                    196:                         */
                    197:                        runningkids++;
                    198:                        kidreaped = 0;
                    199:                        do {
                    200:                                sleeptime = sleep(sleeptime);
                    201:                                /* Note that there is the possibility, if the sleep
                    202:                                 * stops early due to some other signal, of the child
                    203:                                 * terminating and not getting detected during the next
                    204:                                 * sleep.  In that unlikely worst case, we just sleep
                    205:                                 * too long for that user.
                    206:                                 */
                    207:                                for (;;) {
                    208:                                        deadkid = waitpid(-1, &kidstat, WNOHANG);
                    209:                                        if (deadkid <= 0)
                    210:                                                break;
                    211:                                        runningkids--;
                    212:                                        if (deadkid == kid) {
                    213:                                                kidreaped = 1;
                    214:                                                sleeptime = 0;
                    215:                                        }
                    216:                                }
                    217:                        } while (sleeptime);
                    218:
                    219:                        if (!kidreaped) {
                    220:                                /* It doesn't _really_ matter if the kill fails, e.g.
                    221:                                 * if there's only a zombie now.
                    222:                                 */
1.28      otto      223:                                if (getpgid(kid) != getpgrp())
                    224:                                        (void)killpg(getpgid(kid), SIGTERM);
                    225:                                else
                    226:                                        (void)kill(kid, SIGTERM);
1.18      deraadt   227:                                warnx("uid %u did not finish in time", pw->pw_uid);
1.12      pjanzen   228:                        }
1.15      pjanzen   229:                        if (time(NULL) - t >= SECSPERDAY)
1.25      deraadt   230:                                errx(2, "'calendar -a' took more than a day; "
                    231:                                    "stopped at uid %u",
1.18      deraadt   232:                                    pw->pw_uid);
1.15      pjanzen   233:                }
                    234:                for (;;) {
                    235:                        deadkid = waitpid(-1, &kidstat, WNOHANG);
                    236:                        if (deadkid <= 0)
                    237:                                break;
                    238:                        runningkids--;
1.1       deraadt   239:                }
1.15      pjanzen   240:                if (runningkids)
1.25      deraadt   241:                        warnx("%d child processes still running when "
                    242:                            "'calendar -a' finished", runningkids);
                    243:        } else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
1.5       millert   244:                if(!chdir(caldir))
                    245:                        cal();
1.1       deraadt   246:        } else
                    247:                cal();
1.5       millert   248:
1.1       deraadt   249:        exit(0);
                    250: }
                    251:
                    252:
                    253: void
1.21      deraadt   254: usage(void)
1.1       deraadt   255: {
1.5       millert   256:        (void)fprintf(stderr,
1.22      jmc       257:            "usage: calendar [-ab] [-A num] [-B num] [-f calendarfile] "
1.27      jmc       258:            "[-t [[[cc]yy]mm]dd]\n");
1.1       deraadt   259:        exit(1);
1.15      pjanzen   260: }
                    261:
                    262:
                    263: void
1.21      deraadt   264: childsig(int signo)
1.15      pjanzen   265: {
1.1       deraadt   266: }