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

Annotation of src/usr.bin/sudo/logging.c, Revision 1.5

1.1       millert     1: /*
1.5     ! millert     2:  * Copyright (c) 1994-1996,1998-2001 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       millert     3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  *
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  *
                     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.
                     15:  *
                     16:  * 3. The name of the author may not be used to endorse or promote products
                     17:  *    derived from this software without specific prior written permission.
                     18:  *
                     19:  * 4. Products derived from this software may not be called "Sudo" nor
                     20:  *    may "Sudo" appear in their names without specific prior written
                     21:  *    permission from the author.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     24:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     25:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
                     26:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     27:  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     28:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     29:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     30:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     31:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     32:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     33:  */
                     34:
                     35: #include "config.h"
                     36:
1.5     ! millert    37: #include <sys/types.h>
        !            38: #include <sys/param.h>
        !            39: #include <sys/stat.h>
        !            40: #include <sys/wait.h>
1.1       millert    41: #include <stdio.h>
                     42: #ifdef STDC_HEADERS
1.5     ! millert    43: # include <stdlib.h>
        !            44: # include <stddef.h>
        !            45: #else
        !            46: # ifdef HAVE_STDLIB_H
        !            47: #  include <stdlib.h>
        !            48: # endif
1.1       millert    49: #endif /* STDC_HEADERS */
1.5     ! millert    50: #ifdef HAVE_STRING_H
        !            51: # include <string.h>
        !            52: #else
        !            53: # ifdef HAVE_STRINGS_H
        !            54: #  include <strings.h>
        !            55: # endif
        !            56: #endif /* HAVE_STRING_H */
1.1       millert    57: #ifdef HAVE_UNISTD_H
1.5     ! millert    58: # include <unistd.h>
1.1       millert    59: #endif /* HAVE_UNISTD_H */
                     60: #include <pwd.h>
                     61: #include <signal.h>
                     62: #include <time.h>
                     63: #include <errno.h>
                     64:
                     65: #include "sudo.h"
                     66:
                     67: #ifndef lint
1.5     ! millert    68: static const char rcsid[] = "$Sudo: logging.c,v 1.151 2001/12/14 23:33:38 millert Exp $";
1.1       millert    69: #endif /* lint */
                     70:
                     71: static void do_syslog          __P((int, char *));
                     72: static void do_logfile         __P((char *));
                     73: static void send_mail          __P((char *));
                     74: static void mail_auth          __P((int, char *));
                     75: static char *get_timestr       __P((void));
1.5     ! millert    76: static void mysyslog           __P((int, const char *, ...));
1.1       millert    77:
1.5     ! millert    78: #define MAXSYSLOGTRIES 16      /* num of retries for broken syslogs */
1.1       millert    79:
                     80: /*
1.5     ! millert    81:  * We do an openlog(3)/closelog(3) for each message because some
        !            82:  * authentication methods (notably PAM) use syslog(3) for their
        !            83:  * own nefarious purposes and may call openlog(3) and closelog(3).
        !            84:  * Note that because we don't want to assume that all systems have
        !            85:  * vsyslog(3) (HP-UX doesn't) "%m" will not be expanded.
        !            86:  * Sadly this is a maze of #ifdefs.
1.1       millert    87:  */
                     88: static void
1.5     ! millert    89: #ifdef __STDC__
        !            90: mysyslog(int pri, const char *fmt, ...)
        !            91: #else
        !            92: mysyslog(pri, fmt, va_alist)
1.1       millert    93:     int pri;
                     94:     const char *fmt;
1.5     ! millert    95:     va_dcl
        !            96: #endif
1.1       millert    97: {
1.5     ! millert    98: #ifdef BROKEN_SYSLOG
1.1       millert    99:     int i;
1.5     ! millert   100: #endif
        !           101:     char buf[MAXSYSLOGLEN+1];
        !           102:     va_list ap;
1.1       millert   103:
1.5     ! millert   104: #ifdef __STDC__
        !           105:     va_start(ap, fmt);
        !           106: #else
        !           107:     va_start(ap);
        !           108: #endif
        !           109: #ifdef LOG_NFACILITIES
        !           110:     openlog(Argv[0], 0, def_ival(I_LOGFAC));
        !           111: #else
        !           112:     openlog(Argv[0], 0);
        !           113: #endif
        !           114:     vsnprintf(buf, sizeof(buf), fmt, ap);
        !           115: #ifdef BROKEN_SYSLOG
        !           116:     /*
        !           117:      * Some versions of syslog(3) don't guarantee success and return
        !           118:      * an int (notably HP-UX < 10.0).  So, if at first we don't succeed,
        !           119:      * try, try again...
        !           120:      */
1.1       millert   121:     for (i = 0; i < MAXSYSLOGTRIES; i++)
1.5     ! millert   122:        if (syslog(pri, "%s", buf) == 0)
1.1       millert   123:            break;
                    124: #else
1.5     ! millert   125:     syslog(pri, "%s", buf);
1.1       millert   126: #endif /* BROKEN_SYSLOG */
1.5     ! millert   127:     va_end(ap);
        !           128:     closelog();
        !           129: }
1.1       millert   130:
                    131: /*
                    132:  * Log a message to syslog, pre-pending the username and splitting the
                    133:  * message into parts if it is longer than MAXSYSLOGLEN.
                    134:  */
                    135: static void
                    136: do_syslog(pri, msg)
                    137:     int pri;
                    138:     char *msg;
                    139: {
                    140:     int count;
                    141:     char *p;
                    142:     char *tmp;
                    143:     char save;
                    144:
                    145:     /*
                    146:      * Log the full line, breaking into multiple syslog(3) calls if necessary
                    147:      */
1.3       millert   148:     for (p = msg, count = 0; *p && count < strlen(msg) / MAXSYSLOGLEN + 1;
                    149:        count++) {
1.1       millert   150:        if (strlen(p) > MAXSYSLOGLEN) {
                    151:            /*
                    152:             * Break up the line into what will fit on one syslog(3) line
                    153:             * Try to break on a word boundary if possible.
                    154:             */
                    155:            for (tmp = p + MAXSYSLOGLEN; tmp > p && *tmp != ' '; tmp--)
                    156:                ;
                    157:            if (tmp <= p)
                    158:                tmp = p + MAXSYSLOGLEN;
                    159:
                    160:            /* NULL terminate line, but save the char to restore later */
                    161:            save = *tmp;
                    162:            *tmp = '\0';
                    163:
                    164:            if (count == 0)
1.5     ! millert   165:                mysyslog(pri, "%8.8s : %s", user_name, p);
1.1       millert   166:            else
1.5     ! millert   167:                mysyslog(pri, "%8.8s : (command continued) %s", user_name, p);
1.1       millert   168:
                    169:            *tmp = save;                        /* restore saved character */
                    170:
                    171:            /* Eliminate leading whitespace */
1.3       millert   172:            for (p = tmp; *p != ' ' && *p !='\0'; p++)
1.1       millert   173:                ;
                    174:        } else {
                    175:            if (count == 0)
1.5     ! millert   176:                mysyslog(pri, "%8.8s : %s", user_name, p);
1.1       millert   177:            else
1.5     ! millert   178:                mysyslog(pri, "%8.8s : (command continued) %s", user_name, p);
1.1       millert   179:        }
                    180:     }
                    181: }
                    182:
                    183: static void
                    184: do_logfile(msg)
                    185:     char *msg;
                    186: {
                    187:     char *full_line;
                    188:     char *beg, *oldend, *end;
                    189:     FILE *fp;
                    190:     mode_t oldmask;
1.5     ! millert   191:     int maxlen = def_ival(I_LOGLINELEN);
1.1       millert   192:
                    193:     oldmask = umask(077);
                    194:     fp = fopen(def_str(I_LOGFILE), "a");
                    195:     (void) umask(oldmask);
                    196:     if (fp == NULL) {
                    197:        easprintf(&full_line, "Can't open log file: %s: %s",
                    198:            def_str(I_LOGFILE), strerror(errno));
                    199:        send_mail(full_line);
                    200:        free(full_line);
                    201:     } else if (!lock_file(fileno(fp), SUDO_LOCK)) {
                    202:        easprintf(&full_line, "Can't lock log file: %s: %s",
                    203:            def_str(I_LOGFILE), strerror(errno));
                    204:        send_mail(full_line);
                    205:        free(full_line);
                    206:     } else {
1.5     ! millert   207:        if (def_ival(I_LOGLINELEN) == 0) {
1.1       millert   208:            /* Don't pretty-print long log file lines (hard to grep) */
                    209:            if (def_flag(I_LOG_HOST))
                    210:                (void) fprintf(fp, "%s : %s : HOST=%s : %s\n", get_timestr(),
                    211:                    user_name, user_shost, msg);
                    212:            else
                    213:                (void) fprintf(fp, "%s : %s : %s\n", get_timestr(),
                    214:                    user_name, msg);
                    215:        } else {
                    216:            if (def_flag(I_LOG_HOST))
                    217:                easprintf(&full_line, "%s : %s : HOST=%s : %s", get_timestr(),
                    218:                    user_name, user_shost, msg);
                    219:            else
                    220:                easprintf(&full_line, "%s : %s : %s", get_timestr(),
                    221:                    user_name, msg);
                    222:
                    223:            /*
                    224:             * Print out full_line with word wrap
                    225:             */
                    226:            beg = end = full_line;
                    227:            while (beg) {
                    228:                oldend = end;
                    229:                end = strchr(oldend, ' ');
                    230:
                    231:                if (maxlen > 0 && end) {
                    232:                    *end = '\0';
                    233:                    if (strlen(beg) > maxlen) {
                    234:                        /* too far, need to back up & print the line */
                    235:
                    236:                        if (beg == (char *)full_line)
                    237:                            maxlen -= 4;        /* don't indent first line */
                    238:
                    239:                        *end = ' ';
                    240:                        if (oldend != beg) {
                    241:                            /* rewind & print */
                    242:                            end = oldend-1;
                    243:                            while (*end == ' ')
                    244:                                --end;
                    245:                            *(++end) = '\0';
                    246:                            (void) fprintf(fp, "%s\n    ", beg);
                    247:                            *end = ' ';
                    248:                        } else {
                    249:                            (void) fprintf(fp, "%s\n    ", beg);
                    250:                        }
                    251:
                    252:                        /* reset beg to point to the start of the new substr */
                    253:                        beg = end;
                    254:                        while (*beg == ' ')
                    255:                            ++beg;
                    256:                    } else {
                    257:                        /* we still have room */
                    258:                        *end = ' ';
                    259:                    }
                    260:
                    261:                    /* remove leading whitespace */
                    262:                    while (*end == ' ')
                    263:                        ++end;
                    264:                } else {
                    265:                    /* final line */
                    266:                    (void) fprintf(fp, "%s\n", beg);
                    267:                    beg = NULL;                 /* exit condition */
                    268:                }
                    269:            }
                    270:            free(full_line);
                    271:        }
                    272:        (void) fflush(fp);
                    273:        (void) lock_file(fileno(fp), SUDO_UNLOCK);
                    274:        (void) fclose(fp);
                    275:     }
                    276: }
                    277:
                    278: /*
                    279:  * Two main functions, log_error() to log errors and log_auth() to
                    280:  * log allow/deny messages.
                    281:  */
                    282: void
                    283: log_auth(status, inform_user)
                    284:     int status;
                    285:     int inform_user;
                    286: {
                    287:     char *message;
                    288:     char *logline;
                    289:     int pri;
                    290:
                    291:     if (status & VALIDATE_OK)
                    292:        pri = def_ival(I_GOODPRI);
                    293:     else
                    294:        pri = def_ival(I_BADPRI);
                    295:
                    296:     /* Set error message, if any. */
                    297:     if (status & VALIDATE_OK)
                    298:        message = "";
                    299:     else if (status & FLAG_NO_USER)
                    300:        message = "user NOT in sudoers ; ";
                    301:     else if (status & FLAG_NO_HOST)
                    302:        message = "user NOT authorized on host ; ";
                    303:     else if (status & VALIDATE_NOT_OK)
                    304:        message = "command not allowed ; ";
                    305:     else
                    306:        message = "unknown error ; ";
                    307:
                    308:     easprintf(&logline, "%sTTY=%s ; PWD=%s ; USER=%s ; COMMAND=%s%s%s",
                    309:        message, user_tty, user_cwd, *user_runas, user_cmnd,
                    310:        user_args ? " " : "", user_args ? user_args : "");
                    311:
                    312:     mail_auth(status, logline);                /* send mail based on status */
                    313:
                    314:     /* Inform the user if they failed to authenticate.  */
                    315:     if (inform_user && (status & VALIDATE_NOT_OK)) {
                    316:        if (status & FLAG_NO_USER)
                    317:            (void) fprintf(stderr, "%s is not in the sudoers file.  %s",
                    318:                user_name, "This incident will be reported.\n");
                    319:        else if (status & FLAG_NO_HOST)
                    320:            (void) fprintf(stderr, "%s is not allowed to run sudo on %s.  %s",
                    321:                user_name, user_shost, "This incident will be reported.\n");
                    322:        else if (status & FLAG_NO_CHECK)
                    323:            (void) fprintf(stderr, "Sorry, user %s may not run sudo on %s.\n",
                    324:                user_name, user_shost);
                    325:        else
                    326:            (void) fprintf(stderr,
                    327:                "Sorry, user %s is not allowed to execute '%s%s%s' as %s on %s.\n",
                    328:                user_name, user_cmnd, user_args ? " " : "",
                    329:                user_args ? user_args : "", *user_runas, user_host);
                    330:     }
                    331:
                    332:     /*
                    333:      * Log via syslog and/or a file.
                    334:      */
1.5     ! millert   335:     if (def_str(I_SYSLOG))
1.1       millert   336:        do_syslog(pri, logline);
                    337:     if (def_str(I_LOGFILE))
                    338:        do_logfile(logline);
                    339:
                    340:     free(logline);
                    341: }
                    342:
                    343: void
                    344: #ifdef __STDC__
                    345: log_error(int flags, const char *fmt, ...)
                    346: #else
                    347: log_error(va_alist)
                    348:     va_dcl
                    349: #endif
                    350: {
                    351:     int serrno = errno;
                    352:     char *message;
                    353:     char *logline;
                    354:     va_list ap;
                    355: #ifdef __STDC__
                    356:     va_start(ap, fmt);
                    357: #else
                    358:     int flags;
                    359:     const char *fmt;
                    360:
                    361:     va_start(ap);
                    362:     flags = va_arg(ap, int);
                    363:     fmt = va_arg(ap, const char *);
                    364: #endif
                    365:
                    366:     /* Become root if we are not already to avoid user control */
                    367:     if (geteuid() != 0)
                    368:        set_perms(PERM_ROOT, 0);
                    369:
                    370:     /* Expand printf-style format + args. */
                    371:     evasprintf(&message, fmt, ap);
                    372:     va_end(ap);
                    373:
                    374:     if (flags & MSG_ONLY)
                    375:        logline = message;
                    376:     else if (flags & USE_ERRNO) {
                    377:        if (user_args) {
                    378:            easprintf(&logline,
                    379:                "%s: %s ; TTY=%s ; PWD=%s ; USER=%s ; COMMAND=%s %s",
                    380:                message, strerror(serrno), user_tty, user_cwd, *user_runas,
                    381:                user_cmnd, user_args);
                    382:        } else {
                    383:            easprintf(&logline,
                    384:                "%s: %s ; TTY=%s ; PWD=%s ; USER=%s ; COMMAND=%s", message,
                    385:                strerror(serrno), user_tty, user_cwd, *user_runas, user_cmnd);
                    386:        }
                    387:     } else {
                    388:        if (user_args) {
                    389:            easprintf(&logline,
                    390:                "%s ; TTY=%s ; PWD=%s ; USER=%s ; COMMAND=%s %s", message,
                    391:                user_tty, user_cwd, *user_runas, user_cmnd, user_args);
                    392:        } else {
                    393:            easprintf(&logline,
                    394:                "%s ; TTY=%s ; PWD=%s ; USER=%s ; COMMAND=%s", message,
                    395:                user_tty, user_cwd, *user_runas, user_cmnd);
                    396:        }
                    397:     }
                    398:
                    399:     /*
                    400:      * Tell the user.
                    401:      */
                    402:     (void) fprintf(stderr, "%s: %s", Argv[0], message);
                    403:     if (flags & USE_ERRNO)
                    404:        (void) fprintf(stderr, ": %s", strerror(serrno));
                    405:     (void) fputc('\n', stderr);
                    406:
                    407:     /*
                    408:      * Send a copy of the error via mail.
                    409:      */
                    410:     if (!(flags & NO_MAIL))
                    411:        send_mail(logline);
                    412:
                    413:     /*
                    414:      * Log to syslog and/or a file.
                    415:      */
1.5     ! millert   416:     if (def_str(I_SYSLOG))
1.1       millert   417:        do_syslog(def_ival(I_BADPRI), logline);
                    418:     if (def_str(I_LOGFILE))
                    419:        do_logfile(logline);
                    420:
1.5     ! millert   421:     free(message);
        !           422:     if (logline != message)
        !           423:        free(logline);
1.1       millert   424:
                    425:     if (!(flags & NO_EXIT))
                    426:        exit(1);
                    427: }
                    428:
                    429: #define MAX_MAILFLAGS  63
                    430:
                    431: /*
                    432:  * Send a message to MAILTO user
                    433:  */
                    434: static void
                    435: send_mail(line)
                    436:     char *line;
                    437: {
                    438:     FILE *mail;
                    439:     char *p;
1.2       millert   440:     int pfd[2], pid, status;
                    441:     sigset_t set, oset;
1.1       millert   442:
                    443:     /* Just return if mailer is disabled. */
                    444:     if (!def_str(I_MAILERPATH) || !def_str(I_MAILTO))
                    445:        return;
                    446:
1.2       millert   447:     (void) sigemptyset(&set);
                    448:     (void) sigaddset(&set, SIGCHLD);
                    449:     (void) sigprocmask(SIG_BLOCK, &set, &oset);
1.1       millert   450:
1.2       millert   451:     if (pipe(pfd) == -1) {
                    452:        (void) fprintf(stderr, "%s: cannot open pipe: %s\n",
                    453:            Argv[0], strerror(errno));
                    454:        exit(1);
                    455:     }
1.1       millert   456:
1.2       millert   457:     switch (pid = fork()) {
                    458:        case -1:
                    459:            /* Error. */
                    460:            (void) fprintf(stderr, "%s: cannot fork: %s\n",
1.1       millert   461:                Argv[0], strerror(errno));
                    462:            exit(1);
1.2       millert   463:            break;
                    464:        case 0:
                    465:            {
                    466:                char *argv[MAX_MAILFLAGS + 1];
                    467:                char *mpath, *mflags;
                    468:                int i;
                    469:
1.5     ! millert   470:                /* Child, set stdin to output side of the pipe */
        !           471:                if (pfd[0] != STDIN_FILENO) {
        !           472:                    (void) dup2(pfd[0], STDIN_FILENO);
        !           473:                    (void) close(pfd[0]);
        !           474:                }
1.2       millert   475:                (void) close(pfd[1]);
                    476:
                    477:                /* Build up an argv based the mailer path and flags */
                    478:                mflags = estrdup(def_str(I_MAILERFLAGS));
                    479:                mpath = estrdup(def_str(I_MAILERPATH));
                    480:                if ((argv[0] = strrchr(mpath, ' ')))
                    481:                    argv[0]++;
                    482:                else
                    483:                    argv[0] = mpath;
                    484:
                    485:                i = 1;
                    486:                if ((p = strtok(mflags, " \t"))) {
                    487:                    do {
                    488:                        argv[i] = p;
                    489:                    } while (++i < MAX_MAILFLAGS && (p = strtok(NULL, " \t")));
                    490:                }
                    491:                argv[i] = NULL;
1.1       millert   492:
1.5     ! millert   493:                /* Close password file so we don't leak the fd. */
        !           494:                endpwent();
        !           495:
1.2       millert   496:                /* Run mailer as root so user cannot kill it. */
                    497:                set_perms(PERM_ROOT, 0);
                    498:                execv(mpath, argv);
                    499:                _exit(127);
                    500:            }
                    501:            break;
                    502:     }
1.1       millert   503:
1.5     ! millert   504:     (void) close(pfd[0]);
1.2       millert   505:     mail = fdopen(pfd[1], "w");
1.1       millert   506:
1.2       millert   507:     /* Pipes are all setup, send message via sendmail. */
                    508:     (void) fprintf(mail, "To: %s\nFrom: %s\nSubject: ",
                    509:        def_str(I_MAILTO), user_name);
                    510:     for (p = def_str(I_MAILSUB); *p; p++) {
                    511:        /* Expand escapes in the subject */
                    512:        if (*p == '%' && *(p+1) != '%') {
                    513:            switch (*(++p)) {
                    514:                case 'h':
                    515:                    (void) fputs(user_host, mail);
                    516:                    break;
                    517:                case 'u':
                    518:                    (void) fputs(user_name, mail);
                    519:                    break;
                    520:                default:
                    521:                    p--;
                    522:                    break;
                    523:            }
                    524:        } else
                    525:            (void) fputc(*p, mail);
                    526:     }
                    527:     (void) fprintf(mail, "\n\n%s : %s : %s : %s\n\n", user_host,
                    528:        get_timestr(), user_name, line);
                    529:     fclose(mail);
1.1       millert   530:
1.2       millert   531:     /* If mailer is done, wait for it now.  If not reapchild will get it.  */
                    532: #ifdef sudo_waitpid
                    533:     (void) sudo_waitpid(pid, &status, WNOHANG);
                    534: #endif
                    535:     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
1.1       millert   536: }
                    537:
                    538: /*
                    539:  * Send mail based on the value of "status" and compile-time options.
                    540:  */
                    541: static void
                    542: mail_auth(status, line)
                    543:     int status;
                    544:     char *line;
                    545: {
                    546:     int mail_mask;
                    547:
                    548:     /* If any of these bits are set in status, we send mail. */
                    549:     if (def_flag(I_MAIL_ALWAYS))
                    550:        mail_mask =
                    551:            VALIDATE_ERROR|VALIDATE_OK|FLAG_NO_USER|FLAG_NO_HOST|VALIDATE_NOT_OK;
                    552:     else {
                    553:        mail_mask = VALIDATE_ERROR;
1.5     ! millert   554:        if (def_flag(I_MAIL_NO_USER))
1.1       millert   555:            mail_mask |= FLAG_NO_USER;
1.5     ! millert   556:        if (def_flag(I_MAIL_NO_HOST))
1.1       millert   557:            mail_mask |= FLAG_NO_HOST;
1.5     ! millert   558:        if (def_flag(I_MAIL_NO_PERMS))
1.1       millert   559:            mail_mask |= VALIDATE_NOT_OK;
                    560:     }
                    561:
                    562:     if ((status & mail_mask) != 0)
                    563:        send_mail(line);
                    564: }
                    565:
                    566: /*
                    567:  * SIGCHLD sig handler--wait for children as they die.
                    568:  */
                    569: RETSIGTYPE
                    570: reapchild(sig)
                    571:     int sig;
                    572: {
                    573:     int status, serrno = errno;
                    574:
                    575: #ifdef sudo_waitpid
1.2       millert   576:     while (sudo_waitpid(-1, &status, WNOHANG) != -1 && errno == EINTR)
1.1       millert   577:        ;
                    578: #else
                    579:     (void) wait(&status);
                    580: #endif
                    581:     errno = serrno;
                    582: }
                    583:
                    584: /*
                    585:  * Return an ascii string with the current date + time
                    586:  * Uses strftime() if available, else falls back to ctime().
                    587:  */
                    588: static char *
                    589: get_timestr()
                    590: {
                    591:     char *s;
                    592:     time_t now = time((time_t) 0);
                    593: #ifdef HAVE_STRFTIME
                    594:     static char buf[128];
                    595:     struct tm *timeptr;
                    596:
                    597:     timeptr = localtime(&now);
                    598:     if (def_flag(I_LOG_YEAR))
                    599:        s = "%h %e %T %Y";
                    600:     else
                    601:        s = "%h %e %T";
                    602:
                    603:     /* strftime() does not guarantee to NUL-terminate so we must check. */
                    604:     buf[sizeof(buf) - 1] = '\0';
                    605:     if (strftime(buf, sizeof(buf), s, timeptr) && buf[sizeof(buf) - 1] == '\0')
                    606:        return(buf);
                    607:
                    608: #endif /* HAVE_STRFTIME */
                    609:
                    610:     s = ctime(&now) + 4;               /* skip day of the week */
                    611:     if (def_flag(I_LOG_YEAR))
                    612:        s[20] = '\0';                   /* avoid the newline */
                    613:     else
                    614:        s[15] = '\0';                   /* don't care about year */
                    615:
                    616:     return(s);
                    617: }