Imported Upstream version 1.7.6p1
[debian/sudo] / logging.c
index 2b329683f187c73c59073c17efa4be9673d51ad1..d0aeab9906f3836f972a55b23a1e2608171e0e06 100644 (file)
--- a/logging.c
+++ b/logging.c
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#ifdef HAVE_SETLOCALE
+# include <locale.h>
+#endif /* HAVE_SETLOCALE */
+#ifdef HAVE_NL_LANGINFO
+# include <langinfo.h>
+#endif /* HAVE_NL_LANGINFO */
 #include <pwd.h>
 #include <grp.h>
 #include <signal.h>
@@ -132,6 +138,12 @@ do_syslog(pri, msg)
     char *p, *tmp, save;
     const char *fmt;
 
+#ifdef HAVE_SETLOCALE
+    const char *old_locale = estrdup(setlocale(LC_ALL, NULL));
+    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
      */
@@ -166,6 +178,11 @@ do_syslog(pri, msg)
        fmt = FMT_CONTD;
        maxlen = MAXSYSLOGLEN - (sizeof(FMT_CONTD) - 6 + strlen(user_name));
     }
+
+#ifdef HAVE_SETLOCALE
+    setlocale(LC_ALL, old_locale);
+    efree((void *)old_locale);
+#endif /* HAVE_SETLOCALE */
 }
 
 static void
@@ -189,6 +206,12 @@ do_logfile(msg)
     } else {
        time_t now;
 
+#ifdef HAVE_SETLOCALE
+       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 == 0) {
            /* Don't pretty-print long log file lines (hard to grep) */
@@ -258,6 +281,11 @@ do_logfile(msg)
        (void) fflush(fp);
        (void) lock_file(fileno(fp), SUDO_UNLOCK);
        (void) fclose(fp);
+
+#ifdef HAVE_SETLOCALE
+       setlocale(LC_ALL, old_locale);
+       efree((void *)old_locale);
+#endif /* HAVE_SETLOCALE */
     }
 }
 
@@ -437,7 +465,7 @@ send_mail(fmt, va_alist)
        "USER=root",
        NULL
     };
-#endif
+#endif /* NO_ROOT_MAILER */
 
     /* Just return if mailer is disabled. */
     if (!def_mailerpath || !def_mailto)
@@ -479,13 +507,22 @@ send_mail(fmt, va_alist)
     /* Daemonize - disassociate from session/tty. */
     if (setsid() == -1)
       warning("setsid");
-    (void) chdir("/");
+    if (chdir("/") == -1)
+      warning("chdir(/)");
     if ((fd = open(_PATH_DEVNULL, O_RDWR, 0644)) != -1) {
        (void) dup2(fd, STDIN_FILENO);
        (void) dup2(fd, STDOUT_FILENO);
        (void) dup2(fd, STDERR_FILENO);
     }
 
+#ifdef HAVE_SETLOCALE
+    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. */
     sudo_endpwent();
     sudo_endgrent();
@@ -582,6 +619,11 @@ send_mail(fmt, va_alist)
            (void) fputc(*p, mail);
     }
 
+#ifdef HAVE_NL_LANGINFO
+    if (strcmp(def_sudoers_locale, "C") != 0)
+       (void) fprintf(mail, "\nContent-Type: text/plain; charset=\"%s\"\nContent-Transfer-Encoding: 8bit", nl_langinfo(CODESET));
+#endif /* HAVE_NL_LANGINFO */
+
     (void) fprintf(mail, "\n\n%s : %s : %s : ", user_host,
        get_timestr(time(NULL), def_log_year), user_name);
 #ifdef __STDC__
@@ -612,10 +654,10 @@ should_mail(status)
     int status;
 {
 
-    return(def_mail_always || ISSET(status, VALIDATE_ERROR) ||
+    return def_mail_always || ISSET(status, VALIDATE_ERROR) ||
        (def_mail_no_user && ISSET(status, FLAG_NO_USER)) ||
        (def_mail_no_host && ISSET(status, FLAG_NO_HOST)) ||
-       (def_mail_no_perms && !ISSET(status, VALIDATE_OK)));
+       (def_mail_no_perms && !ISSET(status, VALIDATE_OK));
 }
 
 #define        LL_TTY_STR      "TTY="
@@ -669,7 +711,10 @@ new_logline(message, serrno)
        }
        len += sizeof(LL_ENV_STR) + 2 + evlen;
     }
+    /* Note: we log "sudo -l command arg ..." as "list command arg ..." */
     len += sizeof(LL_CMND_STR) - 1 + strlen(user_cmnd);
+    if (ISSET(sudo_mode, MODE_CHECK))
+       len += sizeof("list ") - 1;
     if (user_args != NULL)
        len += strlen(user_args) + 1;
 
@@ -722,8 +767,11 @@ new_logline(message, serrno)
            goto toobig;
        efree(evstr);
     }
-    if (strlcat(line, LL_CMND_STR, len) >= len ||
-       strlcat(line, user_cmnd, len) >= len)
+    if (strlcat(line, LL_CMND_STR, len) >= len)
+       goto toobig;
+    if (ISSET(sudo_mode, MODE_CHECK) && strlcat(line, "list ", len) >= len)
+       goto toobig;
+    if (strlcat(line, user_cmnd, len) >= len)
        goto toobig;
     if (user_args != NULL) {
        if (strlcat(line, " ", len) >= len ||
@@ -731,7 +779,7 @@ new_logline(message, serrno)
            goto toobig;
     }
 
-    return (line);
+    return line;
 toobig:
     errorx(1, "internal error: insufficient space for log line");
 }