]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/log.c
Merge changes from CUPS 1.4svn-r8628.
[thirdparty/cups.git] / scheduler / log.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: log.c 7918 2008-09-08 22:03:01Z mike $"
ef416fc2 3 *
4 * Log file routines for the Common UNIX Printing System (CUPS).
5 *
dfd5680b 6 * Copyright 2007-2009 by Apple Inc.
b94498cf 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 14 *
15 * Contents:
16 *
f7deaa1a 17 * cupsdGetDateTime() - Returns a pointer to a date/time string.
18 * cupsdLogGSSMessage() - Log a GSSAPI error...
75bd9771 19 * cupsdLogJob() - Log a job message.
f7deaa1a 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.
75bd9771 23 * cupsdWriteErrorLog() - Write a line to the ErrorLog.
f7deaa1a 24 * check_log_file() - Open/rotate a log file if it needs it.
75bd9771 25 * format_log_line() - Format a line for a log file.
ef416fc2 26 */
27
28/*
29 * Include necessary headers...
30 */
31
32#include "cupsd.h"
33#include <stdarg.h>
b423cd4c 34#include <syslog.h>
ef416fc2 35
36
75bd9771
MS
37/*
38 * Local globals...
39 */
40
41static int log_linesize = 0; /* Size of line for output file */
42static char *log_line = NULL; /* Line for output file */
43
44
ef416fc2 45/*
46 * Local functions...
47 */
48
75bd9771 49static int check_log_file(cups_file_t **lf, const char *logname);
005dd1eb 50static int format_log_line(const char *message, va_list ap);
ef416fc2 51
52
53/*
54 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
55 */
56
57char * /* O - Date/time string */
dfd5680b
MS
58cupsdGetDateTime(struct timeval *t, /* I - Time value or NULL for current */
59 cupsd_time_t format) /* I - Format to use */
ef416fc2 60{
dfd5680b
MS
61 struct timeval curtime; /* Current time value */
62 struct tm *date; /* Date/time value */
63 static struct timeval last_time = { 0, 0 };
64 /* Last time we formatted */
65 static char s[1024]; /* Date/time string */
ef416fc2 66 static const char * const months[12] =/* Months */
67 {
68 "Jan",
69 "Feb",
70 "Mar",
71 "Apr",
72 "May",
73 "Jun",
74 "Jul",
75 "Aug",
76 "Sep",
77 "Oct",
78 "Nov",
79 "Dec"
80 };
81
82
839a51c8
MS
83 /*
84 * Make sure we have a valid time...
85 */
86
87 if (!t)
dfd5680b
MS
88 {
89 gettimeofday(&curtime, NULL);
90 t = &curtime;
91 }
839a51c8 92
dfd5680b
MS
93 if (t->tv_sec != last_time.tv_sec ||
94 (LogTimeFormat == CUPSD_TIME_USECS && t->tv_usec != last_time.tv_usec))
ef416fc2 95 {
dfd5680b 96 last_time = *t;
ef416fc2 97
98 /*
99 * Get the date and time from the UNIX time value, and then format it
100 * into a string. Note that we *can't* use the strftime() function since
101 * it is localized and will seriously confuse automatic programs if the
102 * month names are in the wrong language!
103 *
104 * Also, we use the "timezone" variable that contains the current timezone
105 * offset from GMT in seconds so that we are reporting local time in the
106 * log files. If you want GMT, set the TZ environment variable accordingly
107 * before starting the scheduler.
108 *
109 * (*BSD and Darwin store the timezone offset in the tm structure)
110 */
111
dfd5680b 112 date = localtime(&(t->tv_sec));
ef416fc2 113
dfd5680b
MS
114 if (format == CUPSD_TIME_STANDARD)
115 snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
116 date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
117 date->tm_hour, date->tm_min, date->tm_sec,
118#ifdef HAVE_TM_GMTOFF
119 date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
120#else
121 timezone / 3600, (timezone / 60) % 60);
122#endif /* HAVE_TM_GMTOFF */
123 else
124 snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d.%06d %+03ld%02ld]",
125 date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
bf3816c7 126 date->tm_hour, date->tm_min, date->tm_sec, (int)t->tv_usec,
ef416fc2 127#ifdef HAVE_TM_GMTOFF
dfd5680b 128 date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
ef416fc2 129#else
dfd5680b 130 timezone / 3600, (timezone / 60) % 60);
ef416fc2 131#endif /* HAVE_TM_GMTOFF */
132 }
133
134 return (s);
135}
136
137
f7deaa1a 138#ifdef HAVE_GSSAPI
139/*
140 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
141 */
142
143int /* O - 1 on success, 0 on error */
144cupsdLogGSSMessage(
145 int level, /* I - Log level */
146 int major_status, /* I - Major GSSAPI status */
147 int minor_status, /* I - Minor GSSAPI status */
148 const char *message, /* I - printf-style message string */
149 ...) /* I - Additional args as needed */
150{
151 OM_uint32 err_major_status, /* Major status code for display */
152 err_minor_status; /* Minor status code for display */
153 OM_uint32 msg_ctx; /* Message context */
154 gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
155 /* Major status message */
156 minor_status_string = GSS_C_EMPTY_BUFFER;
157 /* Minor status message */
158 int ret; /* Return value */
159
160
161 msg_ctx = 0;
162 err_major_status = gss_display_status(&err_minor_status,
163 major_status,
164 GSS_C_GSS_CODE,
165 GSS_C_NO_OID,
166 &msg_ctx,
167 &major_status_string);
168
169 if (!GSS_ERROR(err_major_status))
1f0275e3
MS
170 gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE,
171 GSS_C_NULL_OID, &msg_ctx, &minor_status_string);
f7deaa1a 172
173 ret = cupsdLogMessage(level, "%s: %s, %s", message,
174 (char *)major_status_string.value,
175 (char *)minor_status_string.value);
176 gss_release_buffer(&err_minor_status, &major_status_string);
177 gss_release_buffer(&err_minor_status, &minor_status_string);
178
179 return (ret);
180}
181#endif /* HAVE_GSSAPI */
182
183
ef416fc2 184/*
75bd9771 185 * 'cupsdLogJob()' - Log a job message.
ef416fc2 186 */
187
188int /* O - 1 on success, 0 on error */
75bd9771
MS
189cupsdLogJob(cupsd_job_t *job, /* I - Job */
190 int level, /* I - Log level */
191 const char *message, /* I - Printf-style message string */
192 ...) /* I - Additional arguments as needed */
ef416fc2 193{
ef416fc2 194 va_list ap; /* Argument pointer */
005dd1eb
MS
195 char jobmsg[1024]; /* Format string for job message */
196 int status; /* Formatting status */
ef416fc2 197
198
199 /*
200 * See if we want to log this message...
201 */
202
94da7e34
MS
203 if (TestConfigFile || !ErrorLog)
204 return (1);
205
178cb736
MS
206 if ((level > LogLevel ||
207 (level == CUPSD_LOG_INFO && LogLevel < CUPSD_LOG_DEBUG)) &&
208 LogDebugHistory <= 0)
2e4ff8af 209 return (1);
2e4ff8af 210
ef416fc2 211 /*
75bd9771 212 * Format and write the log message...
ef416fc2 213 */
214
75bd9771 215 snprintf(jobmsg, sizeof(jobmsg), "[Job %d] %s", job->id, message);
ef416fc2 216
005dd1eb
MS
217 do
218 {
219 va_start(ap, message);
220 status = format_log_line(jobmsg, ap);
221 va_end(ap);
222 }
223 while (status == 0);
178cb736 224
005dd1eb 225 if (status > 0)
178cb736
MS
226 {
227 if ((level > LogLevel ||
228 (level == CUPSD_LOG_INFO && LogLevel < CUPSD_LOG_DEBUG)) &&
229 LogDebugHistory > 0)
230 {
231 /*
232 * Add message to the job history...
233 */
234
235 cupsd_joblog_t *temp; /* Copy of log message */
236
237
238 if ((temp = malloc(sizeof(cupsd_joblog_t) + strlen(log_line))) != NULL)
239 {
240 temp->time = time(NULL);
241 strcpy(temp->message, log_line);
242 }
243
244 if (!job->history)
245 job->history = cupsArrayNew(NULL, NULL);
246
247 if (job->history && temp)
248 {
249 cupsArrayAdd(job->history, temp);
250
251 if (cupsArrayCount(job->history) > LogDebugHistory)
252 {
253 /*
254 * Remove excess messages...
255 */
256
257 temp = cupsArrayFirst(job->history);
258 cupsArrayRemove(job->history, temp);
259 free(temp);
260 }
261 }
262 else if (temp)
263 free(temp);
264
265 return (1);
266 }
267 else if (level <= LogLevel &&
268 (level != CUPSD_LOG_INFO || LogLevel >= CUPSD_LOG_DEBUG))
269 return (cupsdWriteErrorLog(level, log_line));
270 else
271 return (1);
272 }
75bd9771
MS
273 else
274 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR,
275 "Unable to allocate memory for log line!"));
276}
ef416fc2 277
ef416fc2 278
75bd9771
MS
279/*
280 * 'cupsdLogMessage()' - Log a message to the error log file.
281 */
ef416fc2 282
75bd9771
MS
283int /* O - 1 on success, 0 on error */
284cupsdLogMessage(int level, /* I - Log level */
285 const char *message, /* I - printf-style message string */
286 ...) /* I - Additional args as needed */
287{
288 va_list ap; /* Argument pointer */
005dd1eb 289 int status; /* Formatting status */
ef416fc2 290
ef416fc2 291
292 /*
75bd9771 293 * See if we want to log this message...
ef416fc2 294 */
295
75bd9771 296 if (TestConfigFile)
ef416fc2 297 {
75bd9771 298 if (level <= CUPSD_LOG_WARN)
ef416fc2 299 {
75bd9771
MS
300 va_start(ap, message);
301 vfprintf(stderr, message, ap);
302 putc('\n', stderr);
303 va_end(ap);
ef416fc2 304 }
305
75bd9771 306 return (1);
ef416fc2 307 }
308
75bd9771
MS
309 if (level > LogLevel || !ErrorLog)
310 return (1);
ef416fc2 311
312 /*
75bd9771 313 * Format and write the log message...
ef416fc2 314 */
315
005dd1eb
MS
316 do
317 {
318 va_start(ap, message);
319 status = format_log_line(message, ap);
320 va_end(ap);
321 }
322 while (status == 0);
ef416fc2 323
005dd1eb
MS
324 if (status > 0)
325 return (cupsdWriteErrorLog(level, log_line));
75bd9771
MS
326 else
327 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR,
328 "Unable to allocate memory for log line!"));
ef416fc2 329}
330
331
332/*
333 * 'cupsdLogPage()' - Log a page to the page log file.
334 */
335
336int /* O - 1 on success, 0 on error */
337cupsdLogPage(cupsd_job_t *job, /* I - Job being printed */
338 const char *page) /* I - Page being printed */
339{
01ce6322
MS
340 int i; /* Looping var */
341 char buffer[2048], /* Buffer for page log */
342 *bufptr, /* Pointer into buffer */
343 name[256]; /* Attribute name */
344 const char *format, /* Pointer into PageLogFormat */
345 *nameend; /* End of attribute name */
346 ipp_attribute_t *attr; /* Current attribute */
347 int number; /* Page number */
348 char copies[256]; /* Number of copies */
ef416fc2 349
350
01ce6322
MS
351 /*
352 * Format the line going into the page log...
353 */
354
355 if (!PageLogFormat)
356 return (1);
357
358 number = 1;
359 strcpy(copies, "1");
360 sscanf(page, "%d%255s", &number, copies);
361
362 for (format = PageLogFormat, bufptr = buffer; *format; format ++)
363 {
364 if (*format == '%')
365 {
366 format ++;
367
368 switch (*format)
369 {
370 case '%' : /* Literal % */
371 if (bufptr < (buffer + sizeof(buffer) - 1))
372 *bufptr++ = '%';
373 break;
374
375 case 'p' : /* Printer name */
376 strlcpy(bufptr, job->printer->name,
377 sizeof(buffer) - (bufptr - buffer));
378 bufptr += strlen(bufptr);
379 break;
380
381 case 'j' : /* Job ID */
382 snprintf(bufptr, sizeof(buffer) - (bufptr - buffer), "%d", job->id);
383 bufptr += strlen(bufptr);
384 break;
385
386 case 'u' : /* Username */
387 strlcpy(bufptr, job->username ? job->username : "-",
388 sizeof(buffer) - (bufptr - buffer));
389 bufptr += strlen(bufptr);
390 break;
391
392 case 'T' : /* Date and time */
dfd5680b 393 strlcpy(bufptr, cupsdGetDateTime(NULL, LogTimeFormat),
01ce6322
MS
394 sizeof(buffer) - (bufptr - buffer));
395 bufptr += strlen(bufptr);
396 break;
397
398 case 'P' : /* Page number */
399 snprintf(bufptr, sizeof(buffer) - (bufptr - buffer), "%d", number);
400 bufptr += strlen(bufptr);
401 break;
402
403 case 'C' : /* Number of copies */
404 strlcpy(bufptr, copies, sizeof(buffer) - (bufptr - buffer));
405 bufptr += strlen(bufptr);
406 break;
407
408 case '{' : /* {attribute} */
409 if ((nameend = strchr(format, '}')) != NULL &&
410 (nameend - format - 2) < (sizeof(name) - 1))
411 {
412 /*
413 * Pull the name from inside the brackets...
414 */
415
75bd9771
MS
416 memcpy(name, format + 1, nameend - format - 1);
417 name[nameend - format - 1] = '\0';
418
419 format = nameend;
01ce6322
MS
420
421 if ((attr = ippFindAttribute(job->attrs, name,
422 IPP_TAG_ZERO)) != NULL)
423 {
424 /*
425 * Add the attribute value...
426 */
427
01ce6322
MS
428 for (i = 0;
429 i < attr->num_values &&
430 bufptr < (buffer + sizeof(buffer) - 1);
431 i ++)
432 {
433 if (i)
434 *bufptr++ = ',';
435
436 switch (attr->value_tag)
437 {
438 case IPP_TAG_INTEGER :
439 case IPP_TAG_ENUM :
440 snprintf(bufptr, sizeof(buffer) - (bufptr - buffer),
441 "%d", attr->values[i].integer);
442 bufptr += strlen(bufptr);
443 break;
444
445 case IPP_TAG_BOOLEAN :
446 snprintf(bufptr, sizeof(buffer) - (bufptr - buffer),
447 "%d", attr->values[i].boolean);
448 bufptr += strlen(bufptr);
449 break;
450
451 case IPP_TAG_TEXTLANG :
452 case IPP_TAG_NAMELANG :
453 case IPP_TAG_TEXT :
454 case IPP_TAG_NAME :
455 case IPP_TAG_KEYWORD :
456 case IPP_TAG_URI :
457 case IPP_TAG_URISCHEME :
458 case IPP_TAG_CHARSET :
459 case IPP_TAG_LANGUAGE :
460 case IPP_TAG_MIMETYPE :
461 strlcpy(bufptr, attr->values[i].string.text,
462 sizeof(buffer) - (bufptr - buffer));
463 bufptr += strlen(bufptr);
464 break;
465
466 default :
467 strlcpy(bufptr, "???",
468 sizeof(buffer) - (bufptr - buffer));
469 bufptr += strlen(bufptr);
470 break;
471 }
472 }
473 }
474 else if (bufptr < (buffer + sizeof(buffer) - 1))
475 *bufptr++ = '-';
476 break;
477 }
478
479 default :
480 if (bufptr < (buffer + sizeof(buffer) - 2))
481 {
482 *bufptr++ = '%';
483 *bufptr++ = *format;
484 }
485 break;
486 }
487 }
488 else if (bufptr < (buffer + sizeof(buffer) - 1))
489 *bufptr++ = *format;
490 }
ef416fc2 491
01ce6322
MS
492 *bufptr = '\0';
493
ef416fc2 494#ifdef HAVE_VSYSLOG
495 /*
496 * See if we are logging pages via syslog...
497 */
498
499 if (!strcmp(PageLog, "syslog"))
500 {
01ce6322 501 syslog(LOG_INFO, "%s", buffer);
ef416fc2 502
503 return (1);
504 }
505#endif /* HAVE_VSYSLOG */
506
507 /*
508 * Not using syslog; check the log file...
509 */
510
511 if (!check_log_file(&PageFile, PageLog))
512 return (0);
513
514 /*
515 * Print a page log entry of the form:
516 *
ac884b6a 517 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
ef416fc2 518 * billing hostname
519 */
520
01ce6322 521 cupsFilePrintf(PageFile, "%s\n", buffer);
ef416fc2 522 cupsFileFlush(PageFile);
523
524 return (1);
525}
526
527
528/*
529 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
530 */
531
532int /* O - 1 on success, 0 on error */
533cupsdLogRequest(cupsd_client_t *con, /* I - Request to log */
534 http_status_t code) /* I - Response code */
535{
839a51c8 536 char temp[2048]; /* Temporary string for URI */
ef416fc2 537 static const char * const states[] = /* HTTP client states... */
538 {
539 "WAITING",
540 "OPTIONS",
541 "GET",
542 "GET",
543 "HEAD",
544 "POST",
545 "POST",
546 "POST",
547 "PUT",
548 "PUT",
549 "DELETE",
550 "TRACE",
551 "CLOSE",
552 "STATUS"
553 };
554
555
1f0275e3
MS
556 /*
557 * Filter requests as needed...
558 */
559
560 if (AccessLogLevel < CUPSD_ACCESSLOG_ALL)
561 {
562 /*
563 * Eliminate simple GET requests...
564 */
565
566 if ((con->operation == HTTP_GET &&
567 strncmp(con->uri, "/admin/conf", 11) &&
568 strncmp(con->uri, "/admin/log", 10)) ||
569 (con->operation == HTTP_POST && !con->request &&
570 strncmp(con->uri, "/admin", 6)) ||
571 (con->operation != HTTP_POST && con->operation != HTTP_PUT))
572 return (1);
573
574 if (con->request && con->response &&
e07d4801
MS
575 (con->response->request.status.status_code < IPP_REDIRECTION_OTHER_SITE ||
576 con->response->request.status.status_code == IPP_NOT_FOUND))
1f0275e3
MS
577 {
578 /*
579 * Check successful requests...
580 */
581
582 ipp_op_t op = con->request->request.op.operation_id;
583 static cupsd_accesslog_t standard_ops[] =
584 {
585 CUPSD_ACCESSLOG_ALL, /* reserved */
586 CUPSD_ACCESSLOG_ALL, /* reserved */
587 CUPSD_ACCESSLOG_ACTIONS,/* Print-Job */
588 CUPSD_ACCESSLOG_ACTIONS,/* Print-URI */
589 CUPSD_ACCESSLOG_ACTIONS,/* Validate-Job */
590 CUPSD_ACCESSLOG_ACTIONS,/* Create-Job */
591 CUPSD_ACCESSLOG_ACTIONS,/* Send-Document */
592 CUPSD_ACCESSLOG_ACTIONS,/* Send-URI */
593 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Job */
594 CUPSD_ACCESSLOG_ALL, /* Get-Job-Attributes */
595 CUPSD_ACCESSLOG_ALL, /* Get-Jobs */
596 CUPSD_ACCESSLOG_ALL, /* Get-Printer-Attributes */
597 CUPSD_ACCESSLOG_ACTIONS,/* Hold-Job */
598 CUPSD_ACCESSLOG_ACTIONS,/* Release-Job */
599 CUPSD_ACCESSLOG_ACTIONS,/* Restart-Job */
600 CUPSD_ACCESSLOG_ALL, /* reserved */
601 CUPSD_ACCESSLOG_CONFIG, /* Pause-Printer */
602 CUPSD_ACCESSLOG_CONFIG, /* Resume-Printer */
603 CUPSD_ACCESSLOG_CONFIG, /* Purge-Jobs */
604 CUPSD_ACCESSLOG_CONFIG, /* Set-Printer-Attributes */
605 CUPSD_ACCESSLOG_ACTIONS,/* Set-Job-Attributes */
606 CUPSD_ACCESSLOG_CONFIG, /* Get-Printer-Supported-Values */
607 CUPSD_ACCESSLOG_ACTIONS,/* Create-Printer-Subscription */
608 CUPSD_ACCESSLOG_ACTIONS,/* Create-Job-Subscription */
609 CUPSD_ACCESSLOG_ALL, /* Get-Subscription-Attributes */
610 CUPSD_ACCESSLOG_ALL, /* Get-Subscriptions */
611 CUPSD_ACCESSLOG_ACTIONS,/* Renew-Subscription */
612 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Subscription */
613 CUPSD_ACCESSLOG_ALL, /* Get-Notifications */
614 CUPSD_ACCESSLOG_ACTIONS,/* Send-Notifications */
615 CUPSD_ACCESSLOG_ALL, /* reserved */
616 CUPSD_ACCESSLOG_ALL, /* reserved */
617 CUPSD_ACCESSLOG_ALL, /* reserved */
618 CUPSD_ACCESSLOG_ALL, /* Get-Print-Support-Files */
619 CUPSD_ACCESSLOG_CONFIG, /* Enable-Printer */
620 CUPSD_ACCESSLOG_CONFIG, /* Disable-Printer */
621 CUPSD_ACCESSLOG_CONFIG, /* Pause-Printer-After-Current-Job */
622 CUPSD_ACCESSLOG_ACTIONS,/* Hold-New-Jobs */
623 CUPSD_ACCESSLOG_ACTIONS,/* Release-Held-New-Jobs */
624 CUPSD_ACCESSLOG_CONFIG, /* Deactivate-Printer */
625 CUPSD_ACCESSLOG_CONFIG, /* Activate-Printer */
626 CUPSD_ACCESSLOG_CONFIG, /* Restart-Printer */
627 CUPSD_ACCESSLOG_CONFIG, /* Shutdown-Printer */
628 CUPSD_ACCESSLOG_CONFIG, /* Startup-Printer */
629 CUPSD_ACCESSLOG_ACTIONS,/* Reprocess-Job */
630 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Current-Job */
631 CUPSD_ACCESSLOG_ACTIONS,/* Suspend-Current-Job */
632 CUPSD_ACCESSLOG_ACTIONS,/* Resume-Job */
633 CUPSD_ACCESSLOG_ACTIONS,/* Promote-Job */
634 CUPSD_ACCESSLOG_ACTIONS /* Schedule-Job-After */
635 };
636 static cupsd_accesslog_t cups_ops[] =
637 {
638 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Default */
639 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Printers */
640 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Add-Modify-Printer */
641 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Delete-Printer */
642 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Classes */
643 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Add-Modify-Class */
644 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Delete-Class */
645 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Accept-Jobs */
646 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Reject-Jobs */
647 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Set-Default */
648 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Get-Devices */
649 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Get-PPDs */
650 CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Move-Job */
651 CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Authenticate-Job */
652 CUPSD_ACCESSLOG_ALL /* CUPS-Get-PPD */
653 };
654
655
656 if ((op <= IPP_SCHEDULE_JOB_AFTER && standard_ops[op] > AccessLogLevel) ||
657 (op >= CUPS_GET_DEFAULT && op <= CUPS_GET_PPD &&
658 cups_ops[op - CUPS_GET_DEFAULT] > AccessLogLevel))
659 return (1);
660 }
661 }
662
ef416fc2 663#ifdef HAVE_VSYSLOG
664 /*
665 * See if we are logging accesses via syslog...
666 */
667
668 if (!strcmp(AccessLog, "syslog"))
669 {
2abf387c 670 syslog(LOG_INFO,
671 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
ef416fc2 672 con->http.hostname, con->username[0] != '\0' ? con->username : "-",
839a51c8 673 states[con->operation], _httpEncodeURI(temp, con->uri, sizeof(temp)),
ef416fc2 674 con->http.version / 100, con->http.version % 100,
2abf387c 675 code, CUPS_LLCAST con->bytes,
676 con->request ?
677 ippOpString(con->request->request.op.operation_id) : "-",
678 con->response ?
679 ippErrorString(con->response->request.status.status_code) : "-");
ef416fc2 680
681 return (1);
682 }
683#endif /* HAVE_VSYSLOG */
684
685 /*
686 * Not using syslog; check the log file...
687 */
688
689 if (!check_log_file(&AccessFile, AccessLog))
690 return (0);
691
692 /*
693 * Write a log of the request in "common log format"...
694 */
695
696 cupsFilePrintf(AccessFile,
697 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
698 con->http.hostname, con->username[0] != '\0' ? con->username : "-",
dfd5680b
MS
699 cupsdGetDateTime(&(con->start), LogTimeFormat),
700 states[con->operation],
839a51c8 701 _httpEncodeURI(temp, con->uri, sizeof(temp)),
ef416fc2 702 con->http.version / 100, con->http.version % 100,
703 code, CUPS_LLCAST con->bytes,
704 con->request ?
705 ippOpString(con->request->request.op.operation_id) : "-",
706 con->response ?
707 ippErrorString(con->response->request.status.status_code) :
708 "-");
709
710 cupsFileFlush(AccessFile);
711
712 return (1);
713}
714
715
75bd9771
MS
716/*
717 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
718 */
719
720int /* O - 1 on success, 0 on failure */
721cupsdWriteErrorLog(int level, /* I - Log level */
722 const char *message) /* I - Message string */
723{
724 static const char levels[] = /* Log levels... */
725 {
726 ' ',
727 'X',
728 'A',
729 'C',
730 'E',
731 'W',
732 'N',
733 'I',
734 'D',
735 'd'
736 };
737#ifdef HAVE_VSYSLOG
738 static const int syslevels[] = /* SYSLOG levels... */
739 {
740 0,
741 LOG_EMERG,
742 LOG_ALERT,
743 LOG_CRIT,
744 LOG_ERR,
745 LOG_WARNING,
746 LOG_NOTICE,
747 LOG_INFO,
748 LOG_DEBUG,
749 LOG_DEBUG
750 };
751#endif /* HAVE_VSYSLOG */
752
753
754#ifdef HAVE_VSYSLOG
755 /*
756 * See if we are logging errors via syslog...
757 */
758
759 if (!strcmp(ErrorLog, "syslog"))
760 {
761 syslog(syslevels[level], "%s", message);
762 return (1);
763 }
764#endif /* HAVE_VSYSLOG */
765
766 /*
767 * Not using syslog; check the log file...
768 */
769
770 if (!check_log_file(&ErrorFile, ErrorLog))
771 return (0);
772
773 /*
774 * Write the log message...
775 */
776
777 cupsFilePrintf(ErrorFile, "%c %s %s\n", levels[level],
dfd5680b 778 cupsdGetDateTime(NULL, LogTimeFormat), message);
75bd9771
MS
779 cupsFileFlush(ErrorFile);
780
781 return (1);
782}
783
784
ef416fc2 785/*
786 * 'check_log_file()' - Open/rotate a log file if it needs it.
787 */
788
789static int /* O - 1 if log file open */
790check_log_file(cups_file_t **lf, /* IO - Log file */
791 const char *logname) /* I - Log filename */
792{
f7deaa1a 793 char backname[1024], /* Backup log filename */
794 filename[1024], /* Formatted log filename */
795 *ptr; /* Pointer into filename */
796 const char *logptr; /* Pointer into log filename */
ef416fc2 797
798
799 /*
800 * See if we have a log file to check...
801 */
802
803 if (!lf || !logname || !logname[0])
804 return (1);
805
806 /*
807 * Format the filename as needed...
808 */
809
a74454a7 810 if (!*lf ||
811 (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
812 MaxLogSize > 0))
ef416fc2 813 {
814 /*
815 * Handle format strings...
816 */
817
818 filename[sizeof(filename) - 1] = '\0';
819
820 if (logname[0] != '/')
821 {
822 strlcpy(filename, ServerRoot, sizeof(filename));
823 strlcat(filename, "/", sizeof(filename));
824 }
825 else
826 filename[0] = '\0';
827
f7deaa1a 828 for (logptr = logname, ptr = filename + strlen(filename);
829 *logptr && ptr < (filename + sizeof(filename) - 1);
830 logptr ++)
831 if (*logptr == '%')
ef416fc2 832 {
833 /*
834 * Format spec...
835 */
836
f7deaa1a 837 logptr ++;
838 if (*logptr == 's')
ef416fc2 839 {
840 /*
841 * Insert the server name...
842 */
843
844 strlcpy(ptr, ServerName, sizeof(filename) - (ptr - filename));
845 ptr += strlen(ptr);
846 }
847 else
848 {
849 /*
850 * Otherwise just insert the character...
851 */
852
f7deaa1a 853 *ptr++ = *logptr;
ef416fc2 854 }
855 }
856 else
f7deaa1a 857 *ptr++ = *logptr;
ef416fc2 858
859 *ptr = '\0';
860 }
861
862 /*
863 * See if the log file is open...
864 */
865
866 if (!*lf)
867 {
868 /*
869 * Nope, open the log file...
870 */
871
872 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
873 {
b94498cf 874 /*
875 * If the file is in CUPS_LOGDIR then try to create a missing directory...
876 */
ef416fc2 877
b94498cf 878 if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR)))
879 {
49d87452
MS
880 /*
881 * Try updating the permissions of the containing log directory, using
882 * the log file permissions as a basis...
883 */
884
885 int log_dir_perm = 0300 | LogFilePerm;
886 /* LogFilePerm + owner write/search */
887 if (log_dir_perm & 0040)
888 log_dir_perm |= 0010; /* Add group search */
889 if (log_dir_perm & 0004)
890 log_dir_perm |= 0001; /* Add other search */
891
892 cupsdCheckPermissions(CUPS_LOGDIR, NULL, log_dir_perm, RunUser, Group,
893 1, -1);
b94498cf 894
895 *lf = cupsFileOpen(filename, "a");
896 }
897
898 if (*lf == NULL)
899 {
900 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
901 strerror(errno));
49d87452
MS
902
903 if (FatalErrors & CUPSD_FATAL_LOG)
904 cupsdEndProcess(getpid(), 0);
905
b94498cf 906 return (0);
907 }
ef416fc2 908 }
909
910 if (strncmp(filename, "/dev/", 5))
911 {
912 /*
913 * Change ownership and permissions of non-device logs...
914 */
915
916 fchown(cupsFileNumber(*lf), RunUser, Group);
917 fchmod(cupsFileNumber(*lf), LogFilePerm);
918 }
919 }
920
921 /*
922 * Do we need to rotate the log?
923 */
924
a74454a7 925 if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
926 MaxLogSize > 0)
ef416fc2 927 {
928 /*
929 * Rotate log file...
930 */
931
932 cupsFileClose(*lf);
933
934 strcpy(backname, filename);
935 strlcat(backname, ".O", sizeof(backname));
936
937 unlink(backname);
938 rename(filename, backname);
939
940 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
941 {
942 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
943 strerror(errno));
944
49d87452
MS
945 if (FatalErrors & CUPSD_FATAL_LOG)
946 cupsdEndProcess(getpid(), 0);
947
ef416fc2 948 return (0);
949 }
950
a74454a7 951 /*
952 * Change ownership and permissions of non-device logs...
953 */
ef416fc2 954
a74454a7 955 fchown(cupsFileNumber(*lf), RunUser, Group);
956 fchmod(cupsFileNumber(*lf), LogFilePerm);
ef416fc2 957 }
958
959 return (1);
960}
961
962
963/*
75bd9771
MS
964 * 'format_log_line()' - Format a line for a log file.
965 *
966 * This function resizes a global string buffer as needed. Each call returns
967 * a pointer to this buffer, so the contents are only good until the next call
968 * to format_log_line()...
969 */
970
005dd1eb 971static int /* O - -1 for fatal, 0 for retry, 1 for success */
75bd9771
MS
972format_log_line(const char *message, /* I - Printf-style format string */
973 va_list ap) /* I - Argument list */
974{
005dd1eb 975 int len; /* Length of formatted line */
75bd9771
MS
976
977
978 /*
979 * Allocate the line buffer as needed...
980 */
981
982 if (!log_linesize)
983 {
984 log_linesize = 8192;
985 log_line = malloc(log_linesize);
986
987 if (!log_line)
005dd1eb 988 return (-1);
75bd9771
MS
989 }
990
991 /*
992 * Format the log message...
993 */
994
995 len = vsnprintf(log_line, log_linesize, message, ap);
996
997 /*
998 * Resize the buffer as needed...
999 */
1000
ed6e7faf 1001 if (len >= log_linesize && log_linesize < 65536)
75bd9771
MS
1002 {
1003 char *temp; /* Temporary string pointer */
1004
1005
1006 len ++;
1007
1008 if (len < 8192)
1009 len = 8192;
1010 else if (len > 65536)
1011 len = 65536;
1012
1013 temp = realloc(log_line, len);
1014
1015 if (temp)
1016 {
1017 log_line = temp;
1018 log_linesize = len;
75bd9771 1019
005dd1eb
MS
1020 return (0);
1021 }
75bd9771
MS
1022 }
1023
005dd1eb 1024 return (1);
75bd9771
MS
1025}
1026
1027
1028/*
b19ccc9e 1029 * End of "$Id: log.c 7918 2008-09-08 22:03:01Z mike $".
ef416fc2 1030 */