]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/log.c
Merge changes from CUPS 1.5svn-r9198.
[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 *
ba55dc12 6 * Copyright 2007-2010 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 /*
ba55dc12 563 * Eliminate simple GET, POST, and PUT requests...
1f0275e3
MS
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)) ||
ba55dc12
MS
571 (con->operation != HTTP_GET && con->operation != HTTP_POST &&
572 con->operation != HTTP_PUT))
1f0275e3
MS
573 return (1);
574
575 if (con->request && con->response &&
e07d4801
MS
576 (con->response->request.status.status_code < IPP_REDIRECTION_OTHER_SITE ||
577 con->response->request.status.status_code == IPP_NOT_FOUND))
1f0275e3
MS
578 {
579 /*
580 * Check successful requests...
581 */
582
583 ipp_op_t op = con->request->request.op.operation_id;
584 static cupsd_accesslog_t standard_ops[] =
585 {
586 CUPSD_ACCESSLOG_ALL, /* reserved */
587 CUPSD_ACCESSLOG_ALL, /* reserved */
588 CUPSD_ACCESSLOG_ACTIONS,/* Print-Job */
589 CUPSD_ACCESSLOG_ACTIONS,/* Print-URI */
590 CUPSD_ACCESSLOG_ACTIONS,/* Validate-Job */
591 CUPSD_ACCESSLOG_ACTIONS,/* Create-Job */
592 CUPSD_ACCESSLOG_ACTIONS,/* Send-Document */
593 CUPSD_ACCESSLOG_ACTIONS,/* Send-URI */
594 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Job */
595 CUPSD_ACCESSLOG_ALL, /* Get-Job-Attributes */
596 CUPSD_ACCESSLOG_ALL, /* Get-Jobs */
597 CUPSD_ACCESSLOG_ALL, /* Get-Printer-Attributes */
598 CUPSD_ACCESSLOG_ACTIONS,/* Hold-Job */
599 CUPSD_ACCESSLOG_ACTIONS,/* Release-Job */
600 CUPSD_ACCESSLOG_ACTIONS,/* Restart-Job */
601 CUPSD_ACCESSLOG_ALL, /* reserved */
602 CUPSD_ACCESSLOG_CONFIG, /* Pause-Printer */
603 CUPSD_ACCESSLOG_CONFIG, /* Resume-Printer */
604 CUPSD_ACCESSLOG_CONFIG, /* Purge-Jobs */
605 CUPSD_ACCESSLOG_CONFIG, /* Set-Printer-Attributes */
606 CUPSD_ACCESSLOG_ACTIONS,/* Set-Job-Attributes */
607 CUPSD_ACCESSLOG_CONFIG, /* Get-Printer-Supported-Values */
608 CUPSD_ACCESSLOG_ACTIONS,/* Create-Printer-Subscription */
609 CUPSD_ACCESSLOG_ACTIONS,/* Create-Job-Subscription */
610 CUPSD_ACCESSLOG_ALL, /* Get-Subscription-Attributes */
611 CUPSD_ACCESSLOG_ALL, /* Get-Subscriptions */
612 CUPSD_ACCESSLOG_ACTIONS,/* Renew-Subscription */
613 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Subscription */
614 CUPSD_ACCESSLOG_ALL, /* Get-Notifications */
615 CUPSD_ACCESSLOG_ACTIONS,/* Send-Notifications */
616 CUPSD_ACCESSLOG_ALL, /* reserved */
617 CUPSD_ACCESSLOG_ALL, /* reserved */
618 CUPSD_ACCESSLOG_ALL, /* reserved */
619 CUPSD_ACCESSLOG_ALL, /* Get-Print-Support-Files */
620 CUPSD_ACCESSLOG_CONFIG, /* Enable-Printer */
621 CUPSD_ACCESSLOG_CONFIG, /* Disable-Printer */
622 CUPSD_ACCESSLOG_CONFIG, /* Pause-Printer-After-Current-Job */
623 CUPSD_ACCESSLOG_ACTIONS,/* Hold-New-Jobs */
624 CUPSD_ACCESSLOG_ACTIONS,/* Release-Held-New-Jobs */
625 CUPSD_ACCESSLOG_CONFIG, /* Deactivate-Printer */
626 CUPSD_ACCESSLOG_CONFIG, /* Activate-Printer */
627 CUPSD_ACCESSLOG_CONFIG, /* Restart-Printer */
628 CUPSD_ACCESSLOG_CONFIG, /* Shutdown-Printer */
629 CUPSD_ACCESSLOG_CONFIG, /* Startup-Printer */
630 CUPSD_ACCESSLOG_ACTIONS,/* Reprocess-Job */
631 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Current-Job */
632 CUPSD_ACCESSLOG_ACTIONS,/* Suspend-Current-Job */
633 CUPSD_ACCESSLOG_ACTIONS,/* Resume-Job */
634 CUPSD_ACCESSLOG_ACTIONS,/* Promote-Job */
635 CUPSD_ACCESSLOG_ACTIONS /* Schedule-Job-After */
636 };
637 static cupsd_accesslog_t cups_ops[] =
638 {
639 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Default */
640 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Printers */
641 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Add-Modify-Printer */
642 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Delete-Printer */
643 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Classes */
644 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Add-Modify-Class */
645 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Delete-Class */
646 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Accept-Jobs */
647 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Reject-Jobs */
648 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Set-Default */
649 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Get-Devices */
650 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Get-PPDs */
651 CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Move-Job */
652 CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Authenticate-Job */
653 CUPSD_ACCESSLOG_ALL /* CUPS-Get-PPD */
654 };
655
656
657 if ((op <= IPP_SCHEDULE_JOB_AFTER && standard_ops[op] > AccessLogLevel) ||
658 (op >= CUPS_GET_DEFAULT && op <= CUPS_GET_PPD &&
659 cups_ops[op - CUPS_GET_DEFAULT] > AccessLogLevel))
660 return (1);
661 }
662 }
663
ef416fc2 664#ifdef HAVE_VSYSLOG
665 /*
666 * See if we are logging accesses via syslog...
667 */
668
669 if (!strcmp(AccessLog, "syslog"))
670 {
2abf387c 671 syslog(LOG_INFO,
672 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
ef416fc2 673 con->http.hostname, con->username[0] != '\0' ? con->username : "-",
839a51c8 674 states[con->operation], _httpEncodeURI(temp, con->uri, sizeof(temp)),
ef416fc2 675 con->http.version / 100, con->http.version % 100,
2abf387c 676 code, CUPS_LLCAST con->bytes,
677 con->request ?
678 ippOpString(con->request->request.op.operation_id) : "-",
679 con->response ?
680 ippErrorString(con->response->request.status.status_code) : "-");
ef416fc2 681
682 return (1);
683 }
684#endif /* HAVE_VSYSLOG */
685
686 /*
687 * Not using syslog; check the log file...
688 */
689
690 if (!check_log_file(&AccessFile, AccessLog))
691 return (0);
692
693 /*
694 * Write a log of the request in "common log format"...
695 */
696
697 cupsFilePrintf(AccessFile,
698 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
699 con->http.hostname, con->username[0] != '\0' ? con->username : "-",
dfd5680b
MS
700 cupsdGetDateTime(&(con->start), LogTimeFormat),
701 states[con->operation],
839a51c8 702 _httpEncodeURI(temp, con->uri, sizeof(temp)),
ef416fc2 703 con->http.version / 100, con->http.version % 100,
704 code, CUPS_LLCAST con->bytes,
705 con->request ?
706 ippOpString(con->request->request.op.operation_id) : "-",
707 con->response ?
708 ippErrorString(con->response->request.status.status_code) :
709 "-");
710
711 cupsFileFlush(AccessFile);
712
713 return (1);
714}
715
716
75bd9771
MS
717/*
718 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
719 */
720
721int /* O - 1 on success, 0 on failure */
722cupsdWriteErrorLog(int level, /* I - Log level */
723 const char *message) /* I - Message string */
724{
725 static const char levels[] = /* Log levels... */
726 {
727 ' ',
728 'X',
729 'A',
730 'C',
731 'E',
732 'W',
733 'N',
734 'I',
735 'D',
736 'd'
737 };
738#ifdef HAVE_VSYSLOG
739 static const int syslevels[] = /* SYSLOG levels... */
740 {
741 0,
742 LOG_EMERG,
743 LOG_ALERT,
744 LOG_CRIT,
745 LOG_ERR,
746 LOG_WARNING,
747 LOG_NOTICE,
748 LOG_INFO,
749 LOG_DEBUG,
750 LOG_DEBUG
751 };
752#endif /* HAVE_VSYSLOG */
753
754
755#ifdef HAVE_VSYSLOG
756 /*
757 * See if we are logging errors via syslog...
758 */
759
760 if (!strcmp(ErrorLog, "syslog"))
761 {
762 syslog(syslevels[level], "%s", message);
763 return (1);
764 }
765#endif /* HAVE_VSYSLOG */
766
767 /*
768 * Not using syslog; check the log file...
769 */
770
771 if (!check_log_file(&ErrorFile, ErrorLog))
772 return (0);
773
774 /*
775 * Write the log message...
776 */
777
778 cupsFilePrintf(ErrorFile, "%c %s %s\n", levels[level],
dfd5680b 779 cupsdGetDateTime(NULL, LogTimeFormat), message);
75bd9771
MS
780 cupsFileFlush(ErrorFile);
781
782 return (1);
783}
784
785
ef416fc2 786/*
787 * 'check_log_file()' - Open/rotate a log file if it needs it.
788 */
789
790static int /* O - 1 if log file open */
791check_log_file(cups_file_t **lf, /* IO - Log file */
792 const char *logname) /* I - Log filename */
793{
f7deaa1a 794 char backname[1024], /* Backup log filename */
795 filename[1024], /* Formatted log filename */
796 *ptr; /* Pointer into filename */
797 const char *logptr; /* Pointer into log filename */
ef416fc2 798
799
800 /*
801 * See if we have a log file to check...
802 */
803
804 if (!lf || !logname || !logname[0])
805 return (1);
806
807 /*
808 * Format the filename as needed...
809 */
810
a74454a7 811 if (!*lf ||
812 (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
813 MaxLogSize > 0))
ef416fc2 814 {
815 /*
816 * Handle format strings...
817 */
818
819 filename[sizeof(filename) - 1] = '\0';
820
821 if (logname[0] != '/')
822 {
823 strlcpy(filename, ServerRoot, sizeof(filename));
824 strlcat(filename, "/", sizeof(filename));
825 }
826 else
827 filename[0] = '\0';
828
f7deaa1a 829 for (logptr = logname, ptr = filename + strlen(filename);
830 *logptr && ptr < (filename + sizeof(filename) - 1);
831 logptr ++)
832 if (*logptr == '%')
ef416fc2 833 {
834 /*
835 * Format spec...
836 */
837
f7deaa1a 838 logptr ++;
839 if (*logptr == 's')
ef416fc2 840 {
841 /*
842 * Insert the server name...
843 */
844
845 strlcpy(ptr, ServerName, sizeof(filename) - (ptr - filename));
846 ptr += strlen(ptr);
847 }
848 else
849 {
850 /*
851 * Otherwise just insert the character...
852 */
853
f7deaa1a 854 *ptr++ = *logptr;
ef416fc2 855 }
856 }
857 else
f7deaa1a 858 *ptr++ = *logptr;
ef416fc2 859
860 *ptr = '\0';
861 }
862
863 /*
864 * See if the log file is open...
865 */
866
867 if (!*lf)
868 {
869 /*
870 * Nope, open the log file...
871 */
872
873 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
874 {
b94498cf 875 /*
876 * If the file is in CUPS_LOGDIR then try to create a missing directory...
877 */
ef416fc2 878
b94498cf 879 if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR)))
880 {
49d87452
MS
881 /*
882 * Try updating the permissions of the containing log directory, using
883 * the log file permissions as a basis...
884 */
885
886 int log_dir_perm = 0300 | LogFilePerm;
887 /* LogFilePerm + owner write/search */
888 if (log_dir_perm & 0040)
889 log_dir_perm |= 0010; /* Add group search */
890 if (log_dir_perm & 0004)
891 log_dir_perm |= 0001; /* Add other search */
892
893 cupsdCheckPermissions(CUPS_LOGDIR, NULL, log_dir_perm, RunUser, Group,
894 1, -1);
b94498cf 895
896 *lf = cupsFileOpen(filename, "a");
897 }
898
899 if (*lf == NULL)
900 {
901 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
902 strerror(errno));
49d87452
MS
903
904 if (FatalErrors & CUPSD_FATAL_LOG)
905 cupsdEndProcess(getpid(), 0);
906
b94498cf 907 return (0);
908 }
ef416fc2 909 }
910
911 if (strncmp(filename, "/dev/", 5))
912 {
913 /*
914 * Change ownership and permissions of non-device logs...
915 */
916
917 fchown(cupsFileNumber(*lf), RunUser, Group);
918 fchmod(cupsFileNumber(*lf), LogFilePerm);
919 }
920 }
921
922 /*
923 * Do we need to rotate the log?
924 */
925
a74454a7 926 if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
927 MaxLogSize > 0)
ef416fc2 928 {
929 /*
930 * Rotate log file...
931 */
932
933 cupsFileClose(*lf);
934
935 strcpy(backname, filename);
936 strlcat(backname, ".O", sizeof(backname));
937
938 unlink(backname);
939 rename(filename, backname);
940
941 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
942 {
943 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
944 strerror(errno));
945
49d87452
MS
946 if (FatalErrors & CUPSD_FATAL_LOG)
947 cupsdEndProcess(getpid(), 0);
948
ef416fc2 949 return (0);
950 }
951
a74454a7 952 /*
953 * Change ownership and permissions of non-device logs...
954 */
ef416fc2 955
a74454a7 956 fchown(cupsFileNumber(*lf), RunUser, Group);
957 fchmod(cupsFileNumber(*lf), LogFilePerm);
ef416fc2 958 }
959
960 return (1);
961}
962
963
964/*
75bd9771
MS
965 * 'format_log_line()' - Format a line for a log file.
966 *
967 * This function resizes a global string buffer as needed. Each call returns
968 * a pointer to this buffer, so the contents are only good until the next call
969 * to format_log_line()...
970 */
971
005dd1eb 972static int /* O - -1 for fatal, 0 for retry, 1 for success */
75bd9771
MS
973format_log_line(const char *message, /* I - Printf-style format string */
974 va_list ap) /* I - Argument list */
975{
005dd1eb 976 int len; /* Length of formatted line */
75bd9771
MS
977
978
979 /*
980 * Allocate the line buffer as needed...
981 */
982
983 if (!log_linesize)
984 {
985 log_linesize = 8192;
986 log_line = malloc(log_linesize);
987
988 if (!log_line)
005dd1eb 989 return (-1);
75bd9771
MS
990 }
991
992 /*
993 * Format the log message...
994 */
995
996 len = vsnprintf(log_line, log_linesize, message, ap);
997
998 /*
999 * Resize the buffer as needed...
1000 */
1001
ed6e7faf 1002 if (len >= log_linesize && log_linesize < 65536)
75bd9771
MS
1003 {
1004 char *temp; /* Temporary string pointer */
1005
1006
1007 len ++;
1008
1009 if (len < 8192)
1010 len = 8192;
1011 else if (len > 65536)
1012 len = 65536;
1013
1014 temp = realloc(log_line, len);
1015
1016 if (temp)
1017 {
1018 log_line = temp;
1019 log_linesize = len;
75bd9771 1020
005dd1eb
MS
1021 return (0);
1022 }
75bd9771
MS
1023 }
1024
005dd1eb 1025 return (1);
75bd9771
MS
1026}
1027
1028
1029/*
b19ccc9e 1030 * End of "$Id: log.c 7918 2008-09-08 22:03:01Z mike $".
ef416fc2 1031 */