Imported Upstream version 1.8.2
[debian/sudo] / plugins / sudoers / logging.c
index 82f3e69095bdc7d363c8e0ecea4ddd28fb6f5230..35d6a00fd2d47909652258d2d4e5b9793e9efb29 100644 (file)
@@ -138,8 +138,8 @@ do_syslog(int pri, char *msg)
     /*
      * Log the full line, breaking into multiple syslog(3) calls if necessary
      */
-    fmt = FMT_FIRST;
-    maxlen = MAXSYSLOGLEN - (sizeof(FMT_FIRST) - 6 + strlen(user_name));
+    fmt = _(FMT_FIRST);
+    maxlen = MAXSYSLOGLEN - (strlen(fmt) - 5 + strlen(user_name));
     for (p = msg; *p != '\0'; ) {
        len = strlen(p);
        if (len > maxlen) {
@@ -166,8 +166,8 @@ do_syslog(int pri, char *msg)
            mysyslog(pri, fmt, user_name, p);
            p += len;
        }
-       fmt = FMT_CONTD;
-       maxlen = MAXSYSLOGLEN - (sizeof(FMT_CONTD) - 6 + strlen(user_name));
+       fmt = _(FMT_CONTD);
+       maxlen = MAXSYSLOGLEN - (strlen(fmt) - 5 + strlen(user_name));
     }
 
 #ifdef HAVE_SETLOCALE
@@ -180,22 +180,21 @@ static void
 do_logfile(char *msg)
 {
     char *full_line;
-    char *beg, *oldend, *end;
-    FILE *fp;
+    size_t len;
     mode_t oldmask;
-    size_t maxlen;
+    time_t now;
+    FILE *fp;
 
     oldmask = umask(077);
-    maxlen = def_loglinelen > 0 ? def_loglinelen : 0;
     fp = fopen(def_logfile, "a");
     (void) umask(oldmask);
     if (fp == NULL) {
-       send_mail("Can't open log file: %s: %s", def_logfile, strerror(errno));
+       send_mail(_("unable to open log file: %s: %s"),
+           def_logfile, strerror(errno));
     } else if (!lock_file(fileno(fp), SUDO_LOCK)) {
-       send_mail("Can't lock log file: %s: %s", def_logfile, strerror(errno));
+       send_mail(_("unable to lock log file: %s: %s"),
+           def_logfile, strerror(errno));
     } else {
-       time_t now;
-
 #ifdef HAVE_SETLOCALE
        const char *old_locale = estrdup(setlocale(LC_ALL, NULL));
        if (!setlocale(LC_ALL, def_sudoers_locale))
@@ -203,7 +202,7 @@ do_logfile(char *msg)
 #endif /* HAVE_SETLOCALE */
 
        now = time(NULL);
-       if (def_loglinelen == 0) {
+       if (def_loglinelen < sizeof(LOG_INDENT)) {
            /* Don't pretty-print long log file lines (hard to grep) */
            if (def_log_host)
                (void) fprintf(fp, "%s : %s : HOST=%s : %s\n",
@@ -213,59 +212,16 @@ do_logfile(char *msg)
                    get_timestr(now, def_log_year), user_name, msg);
        } else {
            if (def_log_host)
-               easprintf(&full_line, "%s : %s : HOST=%s : %s",
+               len = easprintf(&full_line, "%s : %s : HOST=%s : %s",
                    get_timestr(now, def_log_year), user_name, user_shost, msg);
            else
-               easprintf(&full_line, "%s : %s : %s",
+               len = easprintf(&full_line, "%s : %s : %s",
                    get_timestr(now, def_log_year), user_name, msg);
 
            /*
-            * Print out full_line with word wrap
+            * Print out full_line with word wrap around def_loglinelen chars.
             */
-           beg = end = full_line;
-           while (beg) {
-               oldend = end;
-               end = strchr(oldend, ' ');
-
-               if (maxlen > 0 && end) {
-                   *end = '\0';
-                   if (strlen(beg) > maxlen) {
-                       /* too far, need to back up & print the line */
-
-                       if (beg == (char *)full_line)
-                           maxlen -= 4;        /* don't indent first line */
-
-                       *end = ' ';
-                       if (oldend != beg) {
-                           /* rewind & print */
-                           end = oldend-1;
-                           while (*end == ' ')
-                               --end;
-                           *(++end) = '\0';
-                           (void) fprintf(fp, "%s\n    ", beg);
-                           *end = ' ';
-                       } else {
-                           (void) fprintf(fp, "%s\n    ", beg);
-                       }
-
-                       /* reset beg to point to the start of the new substr */
-                       beg = end;
-                       while (*beg == ' ')
-                           ++beg;
-                   } else {
-                       /* we still have room */
-                       *end = ' ';
-                   }
-
-                   /* remove leading whitespace */
-                   while (*end == ' ')
-                       ++end;
-               } else {
-                   /* final line */
-                   (void) fprintf(fp, "%s\n", beg);
-                   beg = NULL;                 /* exit condition */
-               }
-           }
+           writeln_wrap(fp, full_line, len, def_loglinelen);
            efree(full_line);
        }
        (void) fflush(fp);
@@ -290,11 +246,11 @@ log_denial(int status, int inform_user)
 
     /* Set error message. */
     if (ISSET(status, FLAG_NO_USER))
-       message = "user NOT in sudoers";
+       message = _("user NOT in sudoers");
     else if (ISSET(status, FLAG_NO_HOST))
-       message = "user NOT authorized on host";
+       message = _("user NOT authorized on host");
     else
-       message = "command not allowed";
+       message = _("command not allowed");
 
     logline = new_logline(message, 0);
 
@@ -304,18 +260,18 @@ log_denial(int status, int inform_user)
     /* Inform the user if they failed to authenticate.  */
     if (inform_user) {
        if (ISSET(status, FLAG_NO_USER)) {
-           sudo_printf(SUDO_CONV_ERROR_MSG, "%s is not in the sudoers file.  "
-               "This incident will be reported.\n", user_name);
+           sudo_printf(SUDO_CONV_ERROR_MSG, _("%s is not in the sudoers "
+               "file.  This incident will be reported.\n"), user_name);
        } else if (ISSET(status, FLAG_NO_HOST)) {
-           sudo_printf(SUDO_CONV_ERROR_MSG, "%s is not allowed to run sudo "
-               "on %s.  This incident will be reported.\n",
+           sudo_printf(SUDO_CONV_ERROR_MSG, _("%s is not allowed to run sudo "
+               "on %s.  This incident will be reported.\n"),
                user_name, user_shost);
        } else if (ISSET(status, FLAG_NO_CHECK)) {
-           sudo_printf(SUDO_CONV_ERROR_MSG, "Sorry, user %s may not run "
-               "sudo on %s.\n", user_name, user_shost);
+           sudo_printf(SUDO_CONV_ERROR_MSG, _("Sorry, user %s may not run "
+               "sudo on %s.\n"), user_name, user_shost);
        } else {
-           sudo_printf(SUDO_CONV_ERROR_MSG, "Sorry, user %s is not allowed "
-               "to execute '%s%s%s' as %s%s%s on %s.\n",
+           sudo_printf(SUDO_CONV_ERROR_MSG, _("Sorry, user %s is not allowed "
+               "to execute '%s%s%s' as %s%s%s on %s.\n"),
                user_name, user_cmnd, user_args ? " " : "",
                user_args ? user_args : "",
                list_pw ? list_pw->pw_name : runas_pw ?
@@ -449,14 +405,14 @@ send_mail(const char *fmt, ...)
     switch (pid = fork()) {
        case -1:
            /* Error. */
-           error(1, "cannot fork");
+           error(1, _("unable to fork"));
            break;
        case 0:
            /* Child. */
            switch (pid = fork()) {
                case -1:
                    /* Error. */
-                   mysyslog(LOG_ERR, "cannot fork: %m");
+                   mysyslog(LOG_ERR, _("unable to fork: %m"));
                    _exit(1);
                case 0:
                    /* Grandchild continues below. */
@@ -506,14 +462,14 @@ send_mail(const char *fmt, ...)
     (void) sigaction(SIGPIPE, &sa, NULL);
 
     if (pipe(pfd) == -1) {
-       mysyslog(LOG_ERR, "cannot open pipe: %m");
+       mysyslog(LOG_ERR, _("unable to open pipe: %m"));
        _exit(1);
     }
 
     switch (pid = fork()) {
        case -1:
            /* Error. */
-           mysyslog(LOG_ERR, "cannot fork: %m");
+           mysyslog(LOG_ERR, _("unable to fork: %m"));
            _exit(1);
            break;
        case 0:
@@ -525,7 +481,7 @@ send_mail(const char *fmt, ...)
                /* Child, set stdin to output side of the pipe */
                if (pfd[0] != STDIN_FILENO) {
                    if (dup2(pfd[0], STDIN_FILENO) == -1) {
-                       mysyslog(LOG_ERR, "cannot dup stdin: %m");
+                       mysyslog(LOG_ERR, _("unable to dup stdin: %m"));
                        _exit(127);
                    }
                    (void) close(pfd[0]);
@@ -559,7 +515,7 @@ send_mail(const char *fmt, ...)
                set_perms(PERM_FULL_USER|PERM_NOEXIT);
                execv(mpath, argv);
 #endif /* NO_ROOT_MAILER */
-               mysyslog(LOG_ERR, "cannot execute %s: %m", mpath);
+               mysyslog(LOG_ERR, _("unable to execute %s: %m"), mpath);
                _exit(127);
            }
            break;
@@ -769,5 +725,5 @@ new_logline(const char *message, int serrno)
 
     return line;
 toobig:
-    errorx(1, "internal error: insufficient space for log line");
+    errorx(1, _("internal error: insufficient space for log line"));
 }