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