2 * "$Id: log.c 7699 2008-06-27 20:44:23Z mike $"
4 * Log file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdGetDateTime() - Returns a pointer to a date/time string.
18 * cupsdLogGSSMessage() - Log a GSSAPI error...
19 * cupsdLogJob() - Log a job message.
20 * cupsdLogMessage() - Log a message to the error log file.
21 * cupsdLogPage() - Log a page to the page log file.
22 * cupsdLogRequest() - Log an HTTP request in Common Log Format.
23 * cupsdWriteErrorLog() - Write a line to the ErrorLog.
24 * check_log_file() - Open/rotate a log file if it needs it.
25 * format_log_line() - Format a line for a log file.
29 * Include necessary headers...
41 static int log_linesize
= 0; /* Size of line for output file */
42 static char *log_line
= NULL
; /* Line for output file */
49 static int check_log_file(cups_file_t
**lf
, const char *logname
);
50 static char *format_log_line(const char *message
, va_list ap
);
54 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
57 char * /* O - Date/time string */
58 cupsdGetDateTime(time_t t
) /* I - Time value */
60 struct tm
*date
; /* Date/time value */
61 static time_t last_time
= -1; /* Last time value */
62 static char s
[1024]; /* Date/time string */
63 static const char * const months
[12] =/* Months */
81 * Make sure we have a valid time...
92 * Get the date and time from the UNIX time value, and then format it
93 * into a string. Note that we *can't* use the strftime() function since
94 * it is localized and will seriously confuse automatic programs if the
95 * month names are in the wrong language!
97 * Also, we use the "timezone" variable that contains the current timezone
98 * offset from GMT in seconds so that we are reporting local time in the
99 * log files. If you want GMT, set the TZ environment variable accordingly
100 * before starting the scheduler.
102 * (*BSD and Darwin store the timezone offset in the tm structure)
105 date
= localtime(&t
);
107 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
108 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
109 date
->tm_hour
, date
->tm_min
, date
->tm_sec
,
110 #ifdef HAVE_TM_GMTOFF
111 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
113 timezone
/ 3600, (timezone
/ 60) % 60);
114 #endif /* HAVE_TM_GMTOFF */
123 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
126 int /* O - 1 on success, 0 on error */
128 int level
, /* I - Log level */
129 int major_status
, /* I - Major GSSAPI status */
130 int minor_status
, /* I - Minor GSSAPI status */
131 const char *message
, /* I - printf-style message string */
132 ...) /* I - Additional args as needed */
134 OM_uint32 err_major_status
, /* Major status code for display */
135 err_minor_status
; /* Minor status code for display */
136 OM_uint32 msg_ctx
; /* Message context */
137 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
138 /* Major status message */
139 minor_status_string
= GSS_C_EMPTY_BUFFER
;
140 /* Minor status message */
141 int ret
; /* Return value */
145 err_major_status
= gss_display_status(&err_minor_status
,
150 &major_status_string
);
152 if (!GSS_ERROR(err_major_status
))
153 gss_display_status(&err_minor_status
, minor_status
, GSS_C_MECH_CODE
,
154 GSS_C_NULL_OID
, &msg_ctx
, &minor_status_string
);
156 ret
= cupsdLogMessage(level
, "%s: %s, %s", message
,
157 (char *)major_status_string
.value
,
158 (char *)minor_status_string
.value
);
159 gss_release_buffer(&err_minor_status
, &major_status_string
);
160 gss_release_buffer(&err_minor_status
, &minor_status_string
);
164 #endif /* HAVE_GSSAPI */
168 * 'cupsdLogJob()' - Log a job message.
171 int /* O - 1 on success, 0 on error */
172 cupsdLogJob(cupsd_job_t
*job
, /* I - Job */
173 int level
, /* I - Log level */
174 const char *message
, /* I - Printf-style message string */
175 ...) /* I - Additional arguments as needed */
177 va_list ap
; /* Argument pointer */
178 char jobmsg
[1024], /* Format string for job message */
179 *line
; /* Message line */
183 * See if we want to log this message...
186 if (TestConfigFile
|| level
> LogLevel
|| !ErrorLog
)
190 * Format and write the log message...
193 snprintf(jobmsg
, sizeof(jobmsg
), "[Job %d] %s", job
->id
, message
);
195 va_start(ap
, message
);
196 line
= format_log_line(jobmsg
, ap
);
200 return (cupsdWriteErrorLog(level
, line
));
202 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
203 "Unable to allocate memory for log line!"));
208 * 'cupsdLogMessage()' - Log a message to the error log file.
211 int /* O - 1 on success, 0 on error */
212 cupsdLogMessage(int level
, /* I - Log level */
213 const char *message
, /* I - printf-style message string */
214 ...) /* I - Additional args as needed */
216 va_list ap
; /* Argument pointer */
217 char *line
; /* Message line */
221 * See if we want to log this message...
226 if (level
<= CUPSD_LOG_WARN
)
228 va_start(ap
, message
);
229 vfprintf(stderr
, message
, ap
);
237 if (level
> LogLevel
|| !ErrorLog
)
241 * Format and write the log message...
244 va_start(ap
, message
);
245 line
= format_log_line(message
, ap
);
249 return (cupsdWriteErrorLog(level
, line
));
251 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
252 "Unable to allocate memory for log line!"));
257 * 'cupsdLogPage()' - Log a page to the page log file.
260 int /* O - 1 on success, 0 on error */
261 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
262 const char *page
) /* I - Page being printed */
264 int i
; /* Looping var */
265 char buffer
[2048], /* Buffer for page log */
266 *bufptr
, /* Pointer into buffer */
267 name
[256]; /* Attribute name */
268 const char *format
, /* Pointer into PageLogFormat */
269 *nameend
; /* End of attribute name */
270 ipp_attribute_t
*attr
; /* Current attribute */
271 int number
; /* Page number */
272 char copies
[256]; /* Number of copies */
276 * Format the line going into the page log...
284 sscanf(page
, "%d%255s", &number
, copies
);
286 for (format
= PageLogFormat
, bufptr
= buffer
; *format
; format
++)
294 case '%' : /* Literal % */
295 if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
299 case 'p' : /* Printer name */
300 strlcpy(bufptr
, job
->printer
->name
,
301 sizeof(buffer
) - (bufptr
- buffer
));
302 bufptr
+= strlen(bufptr
);
305 case 'j' : /* Job ID */
306 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
), "%d", job
->id
);
307 bufptr
+= strlen(bufptr
);
310 case 'u' : /* Username */
311 strlcpy(bufptr
, job
->username
? job
->username
: "-",
312 sizeof(buffer
) - (bufptr
- buffer
));
313 bufptr
+= strlen(bufptr
);
316 case 'T' : /* Date and time */
317 strlcpy(bufptr
, cupsdGetDateTime(time(NULL
)),
318 sizeof(buffer
) - (bufptr
- buffer
));
319 bufptr
+= strlen(bufptr
);
322 case 'P' : /* Page number */
323 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
), "%d", number
);
324 bufptr
+= strlen(bufptr
);
327 case 'C' : /* Number of copies */
328 strlcpy(bufptr
, copies
, sizeof(buffer
) - (bufptr
- buffer
));
329 bufptr
+= strlen(bufptr
);
332 case '{' : /* {attribute} */
333 if ((nameend
= strchr(format
, '}')) != NULL
&&
334 (nameend
- format
- 2) < (sizeof(name
) - 1))
337 * Pull the name from inside the brackets...
340 memcpy(name
, format
+ 1, nameend
- format
- 1);
341 name
[nameend
- format
- 1] = '\0';
345 if ((attr
= ippFindAttribute(job
->attrs
, name
,
346 IPP_TAG_ZERO
)) != NULL
)
349 * Add the attribute value...
353 i
< attr
->num_values
&&
354 bufptr
< (buffer
+ sizeof(buffer
) - 1);
360 switch (attr
->value_tag
)
362 case IPP_TAG_INTEGER
:
364 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
),
365 "%d", attr
->values
[i
].integer
);
366 bufptr
+= strlen(bufptr
);
369 case IPP_TAG_BOOLEAN
:
370 snprintf(bufptr
, sizeof(buffer
) - (bufptr
- buffer
),
371 "%d", attr
->values
[i
].boolean
);
372 bufptr
+= strlen(bufptr
);
375 case IPP_TAG_TEXTLANG
:
376 case IPP_TAG_NAMELANG
:
379 case IPP_TAG_KEYWORD
:
381 case IPP_TAG_URISCHEME
:
382 case IPP_TAG_CHARSET
:
383 case IPP_TAG_LANGUAGE
:
384 case IPP_TAG_MIMETYPE
:
385 strlcpy(bufptr
, attr
->values
[i
].string
.text
,
386 sizeof(buffer
) - (bufptr
- buffer
));
387 bufptr
+= strlen(bufptr
);
391 strlcpy(bufptr
, "???",
392 sizeof(buffer
) - (bufptr
- buffer
));
393 bufptr
+= strlen(bufptr
);
398 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
404 if (bufptr
< (buffer
+ sizeof(buffer
) - 2))
412 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
420 * See if we are logging pages via syslog...
423 if (!strcmp(PageLog
, "syslog"))
425 syslog(LOG_INFO
, "%s", buffer
);
429 #endif /* HAVE_VSYSLOG */
432 * Not using syslog; check the log file...
435 if (!check_log_file(&PageFile
, PageLog
))
439 * Print a page log entry of the form:
441 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
445 cupsFilePrintf(PageFile
, "%s\n", buffer
);
446 cupsFileFlush(PageFile
);
453 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
456 int /* O - 1 on success, 0 on error */
457 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
458 http_status_t code
) /* I - Response code */
460 char temp
[2048]; /* Temporary string for URI */
461 static const char * const states
[] = /* HTTP client states... */
481 * Filter requests as needed...
484 if (AccessLogLevel
< CUPSD_ACCESSLOG_ALL
)
487 * Eliminate simple GET requests...
490 if ((con
->operation
== HTTP_GET
&&
491 strncmp(con
->uri
, "/admin/conf", 11) &&
492 strncmp(con
->uri
, "/admin/log", 10)) ||
493 (con
->operation
== HTTP_POST
&& !con
->request
&&
494 strncmp(con
->uri
, "/admin", 6)) ||
495 (con
->operation
!= HTTP_POST
&& con
->operation
!= HTTP_PUT
))
498 if (con
->request
&& con
->response
&&
499 con
->response
->request
.status
.status_code
< IPP_REDIRECTION_OTHER_SITE
)
502 * Check successful requests...
505 ipp_op_t op
= con
->request
->request
.op
.operation_id
;
506 static cupsd_accesslog_t standard_ops
[] =
508 CUPSD_ACCESSLOG_ALL
, /* reserved */
509 CUPSD_ACCESSLOG_ALL
, /* reserved */
510 CUPSD_ACCESSLOG_ACTIONS
,/* Print-Job */
511 CUPSD_ACCESSLOG_ACTIONS
,/* Print-URI */
512 CUPSD_ACCESSLOG_ACTIONS
,/* Validate-Job */
513 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job */
514 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Document */
515 CUPSD_ACCESSLOG_ACTIONS
,/* Send-URI */
516 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Job */
517 CUPSD_ACCESSLOG_ALL
, /* Get-Job-Attributes */
518 CUPSD_ACCESSLOG_ALL
, /* Get-Jobs */
519 CUPSD_ACCESSLOG_ALL
, /* Get-Printer-Attributes */
520 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-Job */
521 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Job */
522 CUPSD_ACCESSLOG_ACTIONS
,/* Restart-Job */
523 CUPSD_ACCESSLOG_ALL
, /* reserved */
524 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer */
525 CUPSD_ACCESSLOG_CONFIG
, /* Resume-Printer */
526 CUPSD_ACCESSLOG_CONFIG
, /* Purge-Jobs */
527 CUPSD_ACCESSLOG_CONFIG
, /* Set-Printer-Attributes */
528 CUPSD_ACCESSLOG_ACTIONS
,/* Set-Job-Attributes */
529 CUPSD_ACCESSLOG_CONFIG
, /* Get-Printer-Supported-Values */
530 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Printer-Subscription */
531 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job-Subscription */
532 CUPSD_ACCESSLOG_ALL
, /* Get-Subscription-Attributes */
533 CUPSD_ACCESSLOG_ALL
, /* Get-Subscriptions */
534 CUPSD_ACCESSLOG_ACTIONS
,/* Renew-Subscription */
535 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Subscription */
536 CUPSD_ACCESSLOG_ALL
, /* Get-Notifications */
537 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Notifications */
538 CUPSD_ACCESSLOG_ALL
, /* reserved */
539 CUPSD_ACCESSLOG_ALL
, /* reserved */
540 CUPSD_ACCESSLOG_ALL
, /* reserved */
541 CUPSD_ACCESSLOG_ALL
, /* Get-Print-Support-Files */
542 CUPSD_ACCESSLOG_CONFIG
, /* Enable-Printer */
543 CUPSD_ACCESSLOG_CONFIG
, /* Disable-Printer */
544 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer-After-Current-Job */
545 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-New-Jobs */
546 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Held-New-Jobs */
547 CUPSD_ACCESSLOG_CONFIG
, /* Deactivate-Printer */
548 CUPSD_ACCESSLOG_CONFIG
, /* Activate-Printer */
549 CUPSD_ACCESSLOG_CONFIG
, /* Restart-Printer */
550 CUPSD_ACCESSLOG_CONFIG
, /* Shutdown-Printer */
551 CUPSD_ACCESSLOG_CONFIG
, /* Startup-Printer */
552 CUPSD_ACCESSLOG_ACTIONS
,/* Reprocess-Job */
553 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Current-Job */
554 CUPSD_ACCESSLOG_ACTIONS
,/* Suspend-Current-Job */
555 CUPSD_ACCESSLOG_ACTIONS
,/* Resume-Job */
556 CUPSD_ACCESSLOG_ACTIONS
,/* Promote-Job */
557 CUPSD_ACCESSLOG_ACTIONS
/* Schedule-Job-After */
559 static cupsd_accesslog_t cups_ops
[] =
561 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Default */
562 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Printers */
563 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Printer */
564 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Printer */
565 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Classes */
566 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Class */
567 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Class */
568 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Accept-Jobs */
569 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Reject-Jobs */
570 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Set-Default */
571 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-Devices */
572 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-PPDs */
573 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Move-Job */
574 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Authenticate-Job */
575 CUPSD_ACCESSLOG_ALL
/* CUPS-Get-PPD */
579 if ((op
<= IPP_SCHEDULE_JOB_AFTER
&& standard_ops
[op
] > AccessLogLevel
) ||
580 (op
>= CUPS_GET_DEFAULT
&& op
<= CUPS_GET_PPD
&&
581 cups_ops
[op
- CUPS_GET_DEFAULT
] > AccessLogLevel
))
588 * See if we are logging accesses via syslog...
591 if (!strcmp(AccessLog
, "syslog"))
594 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
595 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
596 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
597 con
->http
.version
/ 100, con
->http
.version
% 100,
598 code
, CUPS_LLCAST con
->bytes
,
600 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
602 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
606 #endif /* HAVE_VSYSLOG */
609 * Not using syslog; check the log file...
612 if (!check_log_file(&AccessFile
, AccessLog
))
616 * Write a log of the request in "common log format"...
619 cupsFilePrintf(AccessFile
,
620 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
621 con
->http
.hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
622 cupsdGetDateTime(con
->start
), states
[con
->operation
],
623 _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
624 con
->http
.version
/ 100, con
->http
.version
% 100,
625 code
, CUPS_LLCAST con
->bytes
,
627 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
629 ippErrorString(con
->response
->request
.status
.status_code
) :
632 cupsFileFlush(AccessFile
);
639 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
642 int /* O - 1 on success, 0 on failure */
643 cupsdWriteErrorLog(int level
, /* I - Log level */
644 const char *message
) /* I - Message string */
646 static const char levels
[] = /* Log levels... */
660 static const int syslevels
[] = /* SYSLOG levels... */
673 #endif /* HAVE_VSYSLOG */
678 * See if we are logging errors via syslog...
681 if (!strcmp(ErrorLog
, "syslog"))
683 syslog(syslevels
[level
], "%s", message
);
686 #endif /* HAVE_VSYSLOG */
689 * Not using syslog; check the log file...
692 if (!check_log_file(&ErrorFile
, ErrorLog
))
696 * Write the log message...
699 cupsFilePrintf(ErrorFile
, "%c %s %s\n", levels
[level
],
700 cupsdGetDateTime(time(NULL
)), message
);
701 cupsFileFlush(ErrorFile
);
708 * 'check_log_file()' - Open/rotate a log file if it needs it.
711 static int /* O - 1 if log file open */
712 check_log_file(cups_file_t
**lf
, /* IO - Log file */
713 const char *logname
) /* I - Log filename */
715 char backname
[1024], /* Backup log filename */
716 filename
[1024], /* Formatted log filename */
717 *ptr
; /* Pointer into filename */
718 const char *logptr
; /* Pointer into log filename */
722 * See if we have a log file to check...
725 if (!lf
|| !logname
|| !logname
[0])
729 * Format the filename as needed...
733 (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
737 * Handle format strings...
740 filename
[sizeof(filename
) - 1] = '\0';
742 if (logname
[0] != '/')
744 strlcpy(filename
, ServerRoot
, sizeof(filename
));
745 strlcat(filename
, "/", sizeof(filename
));
750 for (logptr
= logname
, ptr
= filename
+ strlen(filename
);
751 *logptr
&& ptr
< (filename
+ sizeof(filename
) - 1);
763 * Insert the server name...
766 strlcpy(ptr
, ServerName
, sizeof(filename
) - (ptr
- filename
));
772 * Otherwise just insert the character...
785 * See if the log file is open...
791 * Nope, open the log file...
794 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
797 * If the file is in CUPS_LOGDIR then try to create a missing directory...
800 if (!strncmp(filename
, CUPS_LOGDIR
, strlen(CUPS_LOGDIR
)))
802 cupsdCheckPermissions(CUPS_LOGDIR
, NULL
, 0755, RunUser
, Group
, 1, -1);
804 *lf
= cupsFileOpen(filename
, "a");
809 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
815 if (strncmp(filename
, "/dev/", 5))
818 * Change ownership and permissions of non-device logs...
821 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
822 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
827 * Do we need to rotate the log?
830 if (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
839 strcpy(backname
, filename
);
840 strlcat(backname
, ".O", sizeof(backname
));
843 rename(filename
, backname
);
845 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
847 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
,
854 * Change ownership and permissions of non-device logs...
857 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
858 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
866 * 'format_log_line()' - Format a line for a log file.
868 * This function resizes a global string buffer as needed. Each call returns
869 * a pointer to this buffer, so the contents are only good until the next call
870 * to format_log_line()...
873 static char * /* O - Text or NULL on error */
874 format_log_line(const char *message
, /* I - Printf-style format string */
875 va_list ap
) /* I - Argument list */
877 int len
; /* Length of formatted line */
881 * Allocate the line buffer as needed...
887 log_line
= malloc(log_linesize
);
894 * Format the log message...
897 len
= vsnprintf(log_line
, log_linesize
, message
, ap
);
900 * Resize the buffer as needed...
903 if (len
>= log_linesize
)
905 char *temp
; /* Temporary string pointer */
912 else if (len
> 65536)
915 temp
= realloc(log_line
, len
);
923 vsnprintf(log_line
, log_linesize
, message
, ap
);
931 * End of "$Id: log.c 7699 2008-06-27 20:44:23Z mike $".