]> granicus.if.org Git - sudo/commitdiff
Redo the long syslog line splitting based on a patch from Eygene Ryabinkin.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 14 Jun 2007 16:06:25 +0000 (16:06 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 14 Jun 2007 16:06:25 +0000 (16:06 +0000)
Include memrchr() for systems without it.

logging.c
sudo.h

index aafc77c709b0b644ff9cbfb3ea56d4838de43908..07c83d37251758ad5757e122eb40ea6ce152efdb 100644 (file)
--- a/logging.c
+++ b/logging.c
@@ -128,46 +128,45 @@ do_syslog(pri, msg)
     int pri;
     char *msg;
 {
-    size_t count;
-    char *p;
-    char *tmp;
-    char save;
+    size_t len, maxlen;
+    char *p, *tmp, save;
+    const char *fmt;
+    const char fmt_first[] = "%8s : %s";
+    const char fmt_contd[] = "%8s : (command continued) %s";
 
     /*
      * Log the full line, breaking into multiple syslog(3) calls if necessary
      */
-    for (p = msg, count = 0; *p && count < strlen(msg) / MAXSYSLOGLEN + 1;
-       count++) {
-       if (strlen(p) > MAXSYSLOGLEN) {
+    fmt = fmt_first;
+    maxlen = MAXSYSLOGLEN - (sizeof(fmt_first) - 6 + strlen(user_name));
+    for (p = msg; *p != '\0'; ) {
+       len = strlen(p);
+       if (len > maxlen) {
            /*
             * Break up the line into what will fit on one syslog(3) line
-            * Try to break on a word boundary if possible.
+            * Try to avoid breaking words into several lines if possible.
             */
-           for (tmp = p + MAXSYSLOGLEN; tmp > p && *tmp != ' '; tmp--)
-               ;
-           if (tmp <= p)
-               tmp = p + MAXSYSLOGLEN;
+           tmp = memrchr(p, ' ', maxlen);
+           if (tmp == NULL)
+               tmp = p + maxlen;
 
            /* NULL terminate line, but save the char to restore later */
            save = *tmp;
            *tmp = '\0';
 
-           if (count == 0)
-               mysyslog(pri, "%8s : %s", user_name, p);
-           else
-               mysyslog(pri, "%8s : (command continued) %s", user_name, p);
+           mysyslog(pri, fmt, user_name, p);
 
            *tmp = save;                        /* restore saved character */
 
-           /* Eliminate leading whitespace */
-           for (p = tmp; *p != ' ' && *p !='\0'; p++)
+           /* Advance p and eliminate leading whitespace */
+           for (p = tmp; *p == ' '; p++)
                ;
        } else {
-           if (count == 0)
-               mysyslog(pri, "%8s : %s", user_name, p);
-           else
-               mysyslog(pri, "%8s : (command continued) %s", user_name, p);
+           mysyslog(pri, fmt, user_name, p);
+           p += len;
        }
+       fmt = fmt_contd;
+       maxlen = MAXSYSLOGLEN - (sizeof(fmt_contd) - 6 + strlen(user_name));
     }
 }
 
diff --git a/sudo.h b/sudo.h
index f30b1d0b08e723f60e65c69b50e8541129225463..da514026c46bbca0036efc12b22c33d68a0e7255 100644 (file)
--- a/sudo.h
+++ b/sudo.h
@@ -207,6 +207,12 @@ size_t strlcat             __P((char *, const char *, size_t));
 #ifndef HAVE_STRLCPY
 size_t strlcpy         __P((char *, const char *, size_t));
 #endif
+#ifndef HAVE_MEMRCHR
+VOID *memrchr          __P((const VOID *, int, size_t));
+#endif
+#ifndef HAVE_MKSTEMP
+int mkstemp            __P((char *));
+#endif
 char *sudo_goodpath    __P((const char *, struct stat *));
 char *tgetpass         __P((const char *, int, int));
 int find_path          __P((char *, char **, struct stat *, char *));