Diff for /embedaddon/sudo/plugins/sudoers/logging.c between versions 1.1.1.3 and 1.1.1.4

version 1.1.1.3, 2012/10/09 09:29:52 version 1.1.1.4, 2013/07/22 10:46:12
Line 1 Line 1
 /*  /*
 * Copyright (c) 1994-1996, 1998-2011 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 1994-1996, 1998-2013 Todd C. Miller <Todd.Miller@courtesan.com>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
Line 25 Line 25
 #include <config.h>  #include <config.h>
   
 #include <sys/types.h>  #include <sys/types.h>
 #include <sys/param.h>  
 #include <sys/stat.h>  #include <sys/stat.h>
 #include <sys/ioctl.h>  #include <sys/ioctl.h>
 #include <sys/wait.h>  #include <sys/wait.h>
Line 47 Line 46
 #ifdef HAVE_UNISTD_H  #ifdef HAVE_UNISTD_H
 # include <unistd.h>  # include <unistd.h>
 #endif /* HAVE_UNISTD_H */  #endif /* HAVE_UNISTD_H */
 #ifdef HAVE_SETLOCALE  
 # include <locale.h>  
 #endif /* HAVE_SETLOCALE */  
 #ifdef HAVE_NL_LANGINFO  #ifdef HAVE_NL_LANGINFO
 # include <langinfo.h>  # include <langinfo.h>
 #endif /* HAVE_NL_LANGINFO */  #endif /* HAVE_NL_LANGINFO */
Line 60 Line 56
 #include <ctype.h>  #include <ctype.h>
 #include <errno.h>  #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #include <setjmp.h>  
   
 #include "sudoers.h"  #include "sudoers.h"
   
   #ifndef va_copy
   # define va_copy(d, s) memcpy(&(d), &(s), sizeof(d));
   #endif
   
   /* Special message for log_warning() so we know to use ngettext() */
   #define INCORRECT_PASSWORD_ATTEMPT      ((char *)0x01)
   
 static void do_syslog(int, char *);  static void do_syslog(int, char *);
 static void do_logfile(char *);  static void do_logfile(char *);
 static void send_mail(const char *fmt, ...);  static void send_mail(const char *fmt, ...);
Line 71  static int should_mail(int); Line 73  static int should_mail(int);
 static void mysyslog(int, const char *, ...);  static void mysyslog(int, const char *, ...);
 static char *new_logline(const char *, int);  static char *new_logline(const char *, int);
   
 extern sigjmp_buf error_jmp;  
   
 extern char **NewArgv; /* XXX - for auditing */  extern char **NewArgv; /* XXX - for auditing */
   
 #define MAXSYSLOGTRIES  16      /* num of retries for broken syslogs */  #define MAXSYSLOGTRIES  16      /* num of retries for broken syslogs */
Line 119  mysyslog(int pri, const char *fmt, ...) Line 119  mysyslog(int pri, const char *fmt, ...)
     debug_return;      debug_return;
 }  }
   
 #define FMT_FIRST "%8s : %s"  
 #define FMT_CONTD "%8s : (command continued) %s"  
   
 /*  /*
  * Log a message to syslog, pre-pending the username and splitting the   * Log a message to syslog, pre-pending the username and splitting the
  * message into parts if it is longer than MAXSYSLOGLEN.   * message into parts if it is longer than MAXSYSLOGLEN.
Line 132  do_syslog(int pri, char *msg) Line 129  do_syslog(int pri, char *msg)
     size_t len, maxlen;      size_t len, maxlen;
     char *p, *tmp, save;      char *p, *tmp, save;
     const char *fmt;      const char *fmt;
#ifdef HAVE_SETLOCALE    int oldlocale;
    const char *old_locale = estrdup(setlocale(LC_ALL, NULL)); 
#endif 
     debug_decl(do_syslog, SUDO_DEBUG_LOGGING)      debug_decl(do_syslog, SUDO_DEBUG_LOGGING)
   
#ifdef HAVE_SETLOCALE    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
    if (!setlocale(LC_ALL, def_sudoers_locale)) 
        setlocale(LC_ALL, "C"); 
#endif /* HAVE_SETLOCALE */ 
   
     /*      /*
      * Log the full line, breaking into multiple syslog(3) calls if necessary       * Log the full line, breaking into multiple syslog(3) calls if necessary
      */       */
    fmt = _(FMT_FIRST);    fmt = _("%8s : %s");
     maxlen = MAXSYSLOGLEN - (strlen(fmt) - 5 + strlen(user_name));      maxlen = MAXSYSLOGLEN - (strlen(fmt) - 5 + strlen(user_name));
     for (p = msg; *p != '\0'; ) {      for (p = msg; *p != '\0'; ) {
         len = strlen(p);          len = strlen(p);
Line 173  do_syslog(int pri, char *msg) Line 165  do_syslog(int pri, char *msg)
             mysyslog(pri, fmt, user_name, p);              mysyslog(pri, fmt, user_name, p);
             p += len;              p += len;
         }          }
        fmt = _(FMT_CONTD);        fmt = _("%8s : (command continued) %s");
         maxlen = MAXSYSLOGLEN - (strlen(fmt) - 5 + strlen(user_name));          maxlen = MAXSYSLOGLEN - (strlen(fmt) - 5 + strlen(user_name));
     }      }
   
#ifdef HAVE_SETLOCALE    sudoers_setlocale(oldlocale, NULL);
    setlocale(LC_ALL, old_locale); 
    efree((void *)old_locale); 
#endif /* HAVE_SETLOCALE */ 
   
     debug_return;      debug_return;
 }  }
Line 192  do_logfile(char *msg) Line 181  do_logfile(char *msg)
     size_t len;      size_t len;
     mode_t oldmask;      mode_t oldmask;
     time_t now;      time_t now;
       int oldlocale;
     FILE *fp;      FILE *fp;
     debug_decl(do_logfile, SUDO_DEBUG_LOGGING)      debug_decl(do_logfile, SUDO_DEBUG_LOGGING)
   
       sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
   
     oldmask = umask(077);      oldmask = umask(077);
     fp = fopen(def_logfile, "a");      fp = fopen(def_logfile, "a");
     (void) umask(oldmask);      (void) umask(oldmask);
Line 205  do_logfile(char *msg) Line 197  do_logfile(char *msg)
         send_mail(_("unable to lock log file: %s: %s"),          send_mail(_("unable to lock log file: %s: %s"),
             def_logfile, strerror(errno));              def_logfile, strerror(errno));
     } else {      } else {
#ifdef HAVE_SETLOCALE        time(&now);
        const char *old_locale = estrdup(setlocale(LC_ALL, NULL)); 
        if (!setlocale(LC_ALL, def_sudoers_locale)) 
            setlocale(LC_ALL, "C"); 
#endif /* HAVE_SETLOCALE */ 
 
        now = time(NULL); 
         if (def_loglinelen < sizeof(LOG_INDENT)) {          if (def_loglinelen < sizeof(LOG_INDENT)) {
             /* Don't pretty-print long log file lines (hard to grep) */              /* Don't pretty-print long log file lines (hard to grep) */
             if (def_log_host)              if (def_log_host)
Line 237  do_logfile(char *msg) Line 223  do_logfile(char *msg)
         (void) fflush(fp);          (void) fflush(fp);
         (void) lock_file(fileno(fp), SUDO_UNLOCK);          (void) lock_file(fileno(fp), SUDO_UNLOCK);
         (void) fclose(fp);          (void) fclose(fp);
   
 #ifdef HAVE_SETLOCALE  
         setlocale(LC_ALL, old_locale);  
         efree((void *)old_locale);  
 #endif /* HAVE_SETLOCALE */  
     }      }
       sudoers_setlocale(oldlocale, NULL);
   
     debug_return;      debug_return;
 }  }
   
 /*  /*
 * Log and mail the denial message, optionally informing the user. * Log, audit and mail the denial message, optionally informing the user.
  */   */
static voidvoid
 log_denial(int status, bool inform_user)  log_denial(int status, bool inform_user)
 {  {
    char *logline, *message;    const char *message;
     char *logline;
     int oldlocale;
     debug_decl(log_denial, SUDO_DEBUG_LOGGING)      debug_decl(log_denial, SUDO_DEBUG_LOGGING)
   
       /* Handle auditing first (audit_failure() handles the locale itself). */
       if (ISSET(status, FLAG_NO_USER | FLAG_NO_HOST))
           audit_failure(NewArgv, N_("No user or host"));
       else
           audit_failure(NewArgv, N_("validation failure"));
   
       /* Log and mail messages should be in the sudoers locale. */
       sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
   
     /* Set error message. */      /* Set error message. */
     if (ISSET(status, FLAG_NO_USER))      if (ISSET(status, FLAG_NO_USER))
         message = _("user NOT in sudoers");          message = _("user NOT in sudoers");
Line 265  log_denial(int status, bool inform_user) Line 259  log_denial(int status, bool inform_user)
   
     logline = new_logline(message, 0);      logline = new_logline(message, 0);
   
       /* Become root if we are not already. */
       set_perms(PERM_ROOT|PERM_NOEXIT);
   
     if (should_mail(status))      if (should_mail(status))
         send_mail("%s", logline);       /* send mail based on status */          send_mail("%s", logline);       /* send mail based on status */
   
    /* Inform the user if they failed to authenticate.  */    /*
      * Log via syslog and/or a file.
      */
     if (def_syslog)
         do_syslog(def_syslog_badpri, logline);
     if (def_logfile)
         do_logfile(logline);
 
     restore_perms();
 
     efree(logline);
 
     /* Restore locale. */
     sudoers_setlocale(oldlocale, NULL);
 
     /* Inform the user if they failed to authenticate (in their locale).  */
     if (inform_user) {      if (inform_user) {
           sudoers_setlocale(SUDOERS_LOCALE_USER, &oldlocale);
   
         if (ISSET(status, FLAG_NO_USER)) {          if (ISSET(status, FLAG_NO_USER)) {
             sudo_printf(SUDO_CONV_ERROR_MSG, _("%s is not in the sudoers "              sudo_printf(SUDO_CONV_ERROR_MSG, _("%s is not in the sudoers "
                 "file.  This incident will be reported.\n"), user_name);                  "file.  This incident will be reported.\n"), user_name);
Line 289  log_denial(int status, bool inform_user) Line 303  log_denial(int status, bool inform_user)
                 runas_pw->pw_name : user_name, runas_gr ? ":" : "",                  runas_pw->pw_name : user_name, runas_gr ? ":" : "",
                 runas_gr ? runas_gr->gr_name : "", user_host);                  runas_gr ? runas_gr->gr_name : "", user_host);
         }          }
           sudoers_setlocale(oldlocale, NULL);
     }      }
   
     /*  
      * Log via syslog and/or a file.  
      */  
     if (def_syslog)  
         do_syslog(def_syslog_badpri, logline);  
     if (def_logfile)  
         do_logfile(logline);  
   
     efree(logline);  
     debug_return;      debug_return;
 }  }
   
Line 309  log_denial(int status, bool inform_user) Line 314  log_denial(int status, bool inform_user)
 void  void
 log_failure(int status, int flags)  log_failure(int status, int flags)
 {  {
     debug_decl(log_failure, SUDO_DEBUG_LOGGING)  
     bool inform_user = true;      bool inform_user = true;
       debug_decl(log_failure, SUDO_DEBUG_LOGGING)
   
     /* Handle auditing first. */  
     if (ISSET(status, FLAG_NO_USER | FLAG_NO_HOST))  
         audit_failure(NewArgv, _("No user or host"));  
     else  
         audit_failure(NewArgv, _("validation failure"));  
   
     /* The user doesn't always get to see the log message (path info). */      /* The user doesn't always get to see the log message (path info). */
     if (!ISSET(status, FLAG_NO_USER | FLAG_NO_HOST) && def_path_info &&      if (!ISSET(status, FLAG_NO_USER | FLAG_NO_HOST) && def_path_info &&
         (flags == NOT_FOUND_DOT || flags == NOT_FOUND))          (flags == NOT_FOUND_DOT || flags == NOT_FOUND))
Line 351  log_auth_failure(int status, int tries) Line 350  log_auth_failure(int status, int tries)
     debug_decl(log_auth_failure, SUDO_DEBUG_LOGGING)      debug_decl(log_auth_failure, SUDO_DEBUG_LOGGING)
   
     /* Handle auditing first. */      /* Handle auditing first. */
    audit_failure(NewArgv, _("authentication failure"));    audit_failure(NewArgv, N_("authentication failure"));
   
     /*      /*
      * Do we need to send mail?       * Do we need to send mail?
Line 374  log_auth_failure(int status, int tries) Line 373  log_auth_failure(int status, int tries)
     /*      /*
      * If sudoers denied the command we'll log that separately.       * If sudoers denied the command we'll log that separately.
      */       */
    if (ISSET(status, FLAG_BAD_PASSWORD)) {    if (ISSET(status, FLAG_BAD_PASSWORD))
        log_error(flags, ngettext("%d incorrect password attempt",        log_warning(flags, INCORRECT_PASSWORD_ATTEMPT, tries);
            "%d incorrect password attempts", tries), tries);    else if (ISSET(status, FLAG_NON_INTERACTIVE))
    } else if (ISSET(status, FLAG_NON_INTERACTIVE)) {        log_warning(flags, N_("a password is required"));
        log_error(flags, _("a password is required")); 
    } 
   
     debug_return;      debug_return;
 }  }
Line 391  void Line 388  void
 log_allowed(int status)  log_allowed(int status)
 {  {
     char *logline;      char *logline;
       int oldlocale;
     debug_decl(log_allowed, SUDO_DEBUG_LOGGING)      debug_decl(log_allowed, SUDO_DEBUG_LOGGING)
   
       /* Log and mail messages should be in the sudoers locale. */
       sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
   
     logline = new_logline(NULL, 0);      logline = new_logline(NULL, 0);
   
       /* Become root if we are not already. */
       set_perms(PERM_ROOT|PERM_NOEXIT);
   
     if (should_mail(status))      if (should_mail(status))
         send_mail("%s", logline);       /* send mail based on status */          send_mail("%s", logline);       /* send mail based on status */
   
Line 406  log_allowed(int status) Line 410  log_allowed(int status)
     if (def_logfile)      if (def_logfile)
         do_logfile(logline);          do_logfile(logline);
   
       restore_perms();
   
     efree(logline);      efree(logline);
   
       sudoers_setlocale(oldlocale, NULL);
   
     debug_return;      debug_return;
 }  }
   
 /*  /*
 * Perform logging for log_error()/log_fatal() * Perform logging for log_warning()/log_fatal()
  */   */
 static void  static void
vlog_error(int flags, const char *fmt, va_list ap)vlog_warning(int flags, const char *fmt, va_list ap)
 {  {
    int serrno = errno;    int oldlocale, serrno = errno;
     char *logline, *message;      char *logline, *message;
       va_list ap2;
     debug_decl(vlog_error, SUDO_DEBUG_LOGGING)      debug_decl(vlog_error, SUDO_DEBUG_LOGGING)
   
    /* Expand printf-style format + args. */    /* Need extra copy of ap for warning() below. */
    evasprintf(&message, fmt, ap);    if (!ISSET(flags, NO_STDERR))
         va_copy(ap2, ap);
   
    /* Become root if we are not already to avoid user interference */    /* Log messages should be in the sudoers locale. */
    set_perms(PERM_ROOT|PERM_NOEXIT);    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
   
    if (ISSET(flags, MSG_ONLY))    /* Expand printf-style format + args (with a special case). */
     if (fmt == INCORRECT_PASSWORD_ATTEMPT) {
         int tries = va_arg(ap, int);
         easprintf(&message, ngettext("%d incorrect password attempt",
             "%d incorrect password attempts", tries), tries);
     } else {
         evasprintf(&message, _(fmt), ap);
     }
 
     if (ISSET(flags, MSG_ONLY)) {
         logline = message;          logline = message;
    else    } else {
         logline = new_logline(message, ISSET(flags, USE_ERRNO) ? serrno : 0);          logline = new_logline(message, ISSET(flags, USE_ERRNO) ? serrno : 0);
   
     /*  
      * Tell the user.  
      */  
     if (!ISSET(flags, NO_STDERR)) {  
         if (ISSET(flags, USE_ERRNO))  
             warning("%s", message);  
         else  
             warningx("%s", message);  
     }  
     if (logline != message)  
         efree(message);          efree(message);
       }
   
       /* Become root if we are not already. */
       set_perms(PERM_ROOT|PERM_NOEXIT);
   
     /*      /*
      * Send a copy of the error via mail.       * Send a copy of the error via mail.
      */       */
Line 459  vlog_error(int flags, const char *fmt, va_list ap) Line 472  vlog_error(int flags, const char *fmt, va_list ap)
             do_logfile(logline);              do_logfile(logline);
     }      }
   
       restore_perms();
   
     efree(logline);      efree(logline);
   
    restore_perms();    sudoers_setlocale(oldlocale, NULL);
   
       /*
        * Tell the user (in their locale).
        */
       if (!ISSET(flags, NO_STDERR)) {
           if (fmt == INCORRECT_PASSWORD_ATTEMPT) {
               int tries = va_arg(ap2, int);
               warningx(ngettext("%d incorrect password attempt",
                   "%d incorrect password attempts", tries), tries);
           } else {
               if (ISSET(flags, USE_ERRNO))
                   vwarning(fmt, ap2);
               else
                   vwarningx(fmt, ap2);
           }
           va_end(ap2);
       }
   
     debug_return;      debug_return;
 }  }
   
 void  void
log_error(int flags, const char *fmt, ...)log_warning(int flags, const char *fmt, ...)
 {  {
     va_list ap;      va_list ap;
     debug_decl(log_error, SUDO_DEBUG_LOGGING)      debug_decl(log_error, SUDO_DEBUG_LOGGING)
   
     /* Log the error. */      /* Log the error. */
     va_start(ap, fmt);      va_start(ap, fmt);
    vlog_error(flags, fmt, ap);    vlog_warning(flags, fmt, ap);
     va_end(ap);      va_end(ap);
   
     debug_return;      debug_return;
Line 488  log_fatal(int flags, const char *fmt, ...) Line 520  log_fatal(int flags, const char *fmt, ...)
   
     /* Log the error. */      /* Log the error. */
     va_start(ap, fmt);      va_start(ap, fmt);
    vlog_error(flags, fmt, ap);    vlog_warning(flags, fmt, ap);
     va_end(ap);      va_end(ap);
   
     /* Exit the plugin. */      /* Exit the plugin. */
    plugin_cleanup(0);    sudoers_cleanup();
     sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);      sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
    siglongjmp(error_jmp, 1);    fatal_longjmp(1);
 }  }
   
 #define MAX_MAILFLAGS   63  #define MAX_MAILFLAGS   63
Line 531  send_mail(const char *fmt, ...) Line 563  send_mail(const char *fmt, ...)
     switch (pid = sudo_debug_fork()) {      switch (pid = sudo_debug_fork()) {
         case -1:          case -1:
             /* Error. */              /* Error. */
            error(1, _("unable to fork"));            fatal(_("unable to fork"));
             break;              break;
         case 0:          case 0:
             /* Child. */              /* Child. */
Line 569  send_mail(const char *fmt, ...) Line 601  send_mail(const char *fmt, ...)
         (void) dup2(fd, STDERR_FILENO);          (void) dup2(fd, STDERR_FILENO);
     }      }
   
#ifdef HAVE_SETLOCALE    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, NULL);
    if (!setlocale(LC_ALL, def_sudoers_locale)) { 
        setlocale(LC_ALL, "C"); 
        efree(def_sudoers_locale); 
        def_sudoers_locale = estrdup("C"); 
    } 
#endif /* HAVE_SETLOCALE */ 
   
     /* Close password, group and other fds so we don't leak. */      /* Close password, group and other fds so we don't leak. */
     sudo_endpwent();      sudo_endpwent();
Line 665  send_mail(const char *fmt, ...) Line 691  send_mail(const char *fmt, ...)
     /* Pipes are all setup, send message. */      /* Pipes are all setup, send message. */
     (void) fprintf(mail, "To: %s\nFrom: %s\nAuto-Submitted: %s\nSubject: ",      (void) fprintf(mail, "To: %s\nFrom: %s\nAuto-Submitted: %s\nSubject: ",
         def_mailto, def_mailfrom ? def_mailfrom : user_name, "auto-generated");          def_mailto, def_mailfrom ? def_mailfrom : user_name, "auto-generated");
    for (p = def_mailsub; *p; p++) {    for (p = _(def_mailsub); *p; p++) {
         /* Expand escapes in the subject */          /* Expand escapes in the subject */
         if (*p == '%' && *(p+1) != '%') {          if (*p == '%' && *(p+1) != '%') {
             switch (*(++p)) {              switch (*(++p)) {
Line 739  should_mail(int status) Line 765  should_mail(int status)
 static char *  static char *
 new_logline(const char *message, int serrno)  new_logline(const char *message, int serrno)
 {  {
       char *line, *errstr = NULL, *evstr = NULL;
   #ifndef SUDOERS_NO_SEQ
       char sessid[7];
   #endif
       const char *tsid = NULL;
     size_t len = 0;      size_t len = 0;
     char *errstr = NULL;  
     char *evstr = NULL;  
     char *line, sessid[7], *tsid = NULL;  
     debug_decl(new_logline, SUDO_DEBUG_LOGGING)      debug_decl(new_logline, SUDO_DEBUG_LOGGING)
   
   #ifndef SUDOERS_NO_SEQ
     /* A TSID may be a sudoers-style session ID or a free-form string. */      /* A TSID may be a sudoers-style session ID or a free-form string. */
     if (sudo_user.iolog_file != NULL) {      if (sudo_user.iolog_file != NULL) {
         if (IS_SESSID(sudo_user.iolog_file)) {          if (IS_SESSID(sudo_user.iolog_file)) {
Line 760  new_logline(const char *message, int serrno) Line 789  new_logline(const char *message, int serrno)
             tsid = sudo_user.iolog_file;              tsid = sudo_user.iolog_file;
         }          }
     }      }
   #endif
   
     /*      /*
      * Compute line length       * Compute line length
Line 866  new_logline(const char *message, int serrno) Line 896  new_logline(const char *message, int serrno)
   
     debug_return_str(line);      debug_return_str(line);
 toobig:  toobig:
    errorx(1, _("internal error: insufficient space for log line"));    fatalx(_("internal error: insufficient space for log line"));
 }  }

Removed from v.1.1.1.3  
changed lines
  Added in v.1.1.1.4


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>