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