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