]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/log.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / scheduler / log.c
index 1aa41a704a49613a168a0bdb8aad0daa37de53ea..bc113c63406fb3cd1b8b03557c847eb341296580 100644 (file)
@@ -1,33 +1,25 @@
 /*
- * "$Id: log.c 4860 2005-12-01 22:07:26Z mike $"
+ * "$Id: log.c 6875 2007-08-27 23:25:06Z mike $"
  *
  *   Log file routines for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 1997-2005 by Easy Software Products, all rights reserved.
+ *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  * Contents:
  *
- *   cupsdGetDateTime()    - Returns a pointer to a date/time string.
- *   cupsdLogMessage()     - Log a message to the error log file.
- *   cupsdLogPage()        - Log a page to the page log file.
- *   cupsdLogRequest()     - Log an HTTP request in Common Log Format.
- *   check_log_file() - Open/rotate a log file if it needs it.
+ *   cupsdGetDateTime()   - Returns a pointer to a date/time string.
+ *   cupsdLogGSSMessage() - Log a GSSAPI error...
+ *   cupsdLogMessage()    - Log a message to the error log file.
+ *   cupsdLogPage()       - Log a page to the page log file.
+ *   cupsdLogRequest()    - Log an HTTP request in Common Log Format.
+ *   check_log_file()     - Open/rotate a log file if it needs it.
  */
 
 /*
 
 #include "cupsd.h"
 #include <stdarg.h>
-
-#ifdef HAVE_VSYSLOG
-#  include <syslog.h>
-#endif /* HAVE_VSYSLOG */
+#include <syslog.h>
 
 
 /*
@@ -110,6 +99,56 @@ cupsdGetDateTime(time_t t)          /* I - Time value */
 }
 
 
+#ifdef HAVE_GSSAPI
+/*
+ * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
+ */
+
+int                                    /* O - 1 on success, 0 on error */
+cupsdLogGSSMessage(
+    int        level,                  /* I - Log level */
+    int               major_status,            /* I - Major GSSAPI status */
+    int               minor_status,            /* I - Minor GSSAPI status */
+    const char *message,               /* I - printf-style message string */
+    ...)                               /* I - Additional args as needed */
+{
+  OM_uint32    err_major_status,       /* Major status code for display */
+               err_minor_status;       /* Minor status code for display */
+  OM_uint32    msg_ctx;                /* Message context */
+  gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
+                                       /* Major status message */
+               minor_status_string = GSS_C_EMPTY_BUFFER;
+                                       /* Minor status message */
+  int          ret;                    /* Return value */
+
+
+  msg_ctx             = 0;
+  err_major_status    = gss_display_status(&err_minor_status,
+                                          major_status,
+                                          GSS_C_GSS_CODE,
+                                          GSS_C_NO_OID,
+                                          &msg_ctx,
+                                          &major_status_string);
+
+  if (!GSS_ERROR(err_major_status))
+    err_major_status = gss_display_status(&err_minor_status,
+                                         minor_status,
+                                         GSS_C_MECH_CODE,
+                                         GSS_C_NULL_OID,
+                                         &msg_ctx,
+                                         &minor_status_string);
+
+  ret = cupsdLogMessage(level, "%s: %s, %s", message,
+                       (char *)major_status_string.value,
+                       (char *)minor_status_string.value);
+  gss_release_buffer(&err_minor_status, &major_status_string);
+  gss_release_buffer(&err_minor_status, &minor_status_string);
+
+  return (ret);
+}
+#endif /* HAVE_GSSAPI */
+
+
 /*
  * 'cupsdLogMessage()' - Log a message to the error log file.
  */
@@ -157,6 +196,19 @@ cupsdLogMessage(int        level,  /* I - Log level */
   * See if we want to log this message...
   */
 
+  if (TestConfigFile)
+  {
+    if (level <= CUPSD_LOG_WARN)
+    {
+      va_start(ap, message);
+      vfprintf(stderr, message, ap);
+      putc('\n', stderr);
+      va_end(ap);
+    }
+
+    return (1);
+  }
+
   if (level > LogLevel || !ErrorLog)
     return (1);
 
@@ -222,6 +274,9 @@ cupsdLogMessage(int        level,   /* I - Log level */
 
   if (len >= linesize)
   {
+    char       *temp;                  /* Temporary string pointer */
+
+
     len ++;
 
     if (len < 8192)
@@ -229,18 +284,12 @@ cupsdLogMessage(int        level, /* I - Log level */
     else if (len > 65536)
       len = 65536;
 
-    line = realloc(line, len);
+    temp = realloc(line, len);
 
-    if (line)
-      linesize = len;
-    else
+    if (temp)
     {
-      cupsFilePrintf(ErrorFile,
-                     "ERROR: Unable to allocate memory for line - %s\n",
-                     strerror(errno));
-      cupsFileFlush(ErrorFile);
-
-      return (0);
+      line     = temp;
+      linesize = len;
     }
 
     va_start(ap, message);
@@ -365,11 +414,16 @@ cupsdLogRequest(cupsd_client_t *con,      /* I - Request to log */
 
   if (!strcmp(AccessLog, "syslog"))
   {
-    syslog(LOG_INFO, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT "\n",
+    syslog(LOG_INFO,
+           "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
            con->http.hostname, con->username[0] != '\0' ? con->username : "-",
           states[con->operation], con->uri,
           con->http.version / 100, con->http.version % 100,
-          code, CUPS_LLCAST con->bytes);
+          code, CUPS_LLCAST con->bytes,
+          con->request ?
+              ippOpString(con->request->request.op.operation_id) : "-",
+          con->response ?
+              ippErrorString(con->response->request.status.status_code) : "-");
 
     return (1);
   }
@@ -412,9 +466,10 @@ static int                         /* O  - 1 if log file open */
 check_log_file(cups_file_t **lf,       /* IO - Log file */
               const char  *logname)    /* I  - Log filename */
 {
-  char backname[1024],                 /* Backup log filename */
-       filename[1024],                 /* Formatted log filename */
-       *ptr;                           /* Pointer into filename */
+  char         backname[1024],         /* Backup log filename */
+               filename[1024],         /* Formatted log filename */
+               *ptr;                   /* Pointer into filename */
+  const char   *logptr;                /* Pointer into log filename */
 
 
  /*
@@ -428,7 +483,9 @@ check_log_file(cups_file_t **lf,    /* IO - Log file */
   * Format the filename as needed...
   */
 
-  if (!*lf || (cupsFileTell(*lf) > MaxLogSize && MaxLogSize > 0))
+  if (!*lf ||
+      (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
+       MaxLogSize > 0))
   {
    /*
     * Handle format strings...
@@ -444,17 +501,17 @@ check_log_file(cups_file_t **lf,  /* IO - Log file */
     else
       filename[0] = '\0';
 
-    for (ptr = filename + strlen(filename);
-         *logname && ptr < (filename + sizeof(filename) - 1);
-        logname ++)
-      if (*logname == '%')
+    for (logptr = logname, ptr = filename + strlen(filename);
+         *logptr && ptr < (filename + sizeof(filename) - 1);
+        logptr ++)
+      if (*logptr == '%')
       {
        /*
         * Format spec...
        */
 
-        logname ++;
-       if (*logname == 's')
+        logptr ++;
+       if (*logptr == 's')
        {
         /*
          * Insert the server name...
@@ -469,11 +526,11 @@ check_log_file(cups_file_t **lf,  /* IO - Log file */
          * Otherwise just insert the character...
          */
 
-         *ptr++ = *logname;
+         *ptr++ = *logptr;
        }
       }
       else
-       *ptr++ = *logname;
+       *ptr++ = *logptr;
 
     *ptr = '\0';
   }
@@ -490,10 +547,23 @@ check_log_file(cups_file_t **lf,  /* IO - Log file */
 
     if ((*lf = cupsFileOpen(filename, "a")) == NULL)
     {
-      syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
-             strerror(errno));
+     /*
+      * If the file is in CUPS_LOGDIR then try to create a missing directory...
+      */
 
-      return (0);
+      if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR)))
+      {
+        cupsdCheckPermissions(CUPS_LOGDIR, NULL, 0755, RunUser, Group, 1, -1);
+
+        *lf = cupsFileOpen(filename, "a");
+      }
+
+      if (*lf == NULL)
+      {
+       syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
+              strerror(errno));
+       return (0);
+      }
     }
 
     if (strncmp(filename, "/dev/", 5))
@@ -511,7 +581,8 @@ check_log_file(cups_file_t **lf,    /* IO - Log file */
   * Do we need to rotate the log?
   */
 
-  if (cupsFileTell(*lf) > MaxLogSize && MaxLogSize > 0)
+  if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
+      MaxLogSize > 0)
   {
    /*
     * Rotate log file...
@@ -533,15 +604,12 @@ check_log_file(cups_file_t **lf,  /* IO - Log file */
       return (0);
     }
 
-    if (strncmp(filename, "/dev/", 5))
-    {
-     /*
-      * Change ownership and permissions of non-device logs...
-      */
+   /*
+    * Change ownership and permissions of non-device logs...
+    */
 
-      fchown(cupsFileNumber(*lf), RunUser, Group);
-      fchmod(cupsFileNumber(*lf), LogFilePerm);
-    }
+    fchown(cupsFileNumber(*lf), RunUser, Group);
+    fchmod(cupsFileNumber(*lf), LogFilePerm);
   }
 
   return (1);
@@ -549,5 +617,5 @@ check_log_file(cups_file_t **lf,    /* IO - Log file */
 
 
 /*
- * End of "$Id: log.c 4860 2005-12-01 22:07:26Z mike $".
+ * End of "$Id: log.c 6875 2007-08-27 23:25:06Z mike $".
  */