]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
996acce8 | 4 | * Log file routines for the CUPS scheduler. |
ef416fc2 | 5 | * |
0dc4a6bf | 6 | * Copyright 2007-2015 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> | |
0dc4a6bf MS |
22 | #ifdef HAVE_ASL_H |
23 | # include <asl.h> | |
24 | #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
25 | # define SD_JOURNAL_SUPPRESS_LOCATION | |
26 | # include <systemd/sd-journal.h> | |
27 | #endif /* HAVE_ASL_H */ | |
b423cd4c | 28 | #include <syslog.h> |
ef416fc2 | 29 | |
30 | ||
0dc4a6bf MS |
31 | /* |
32 | * Constants for log keys from PWG 5110.3 (PWG Common Log Format)... | |
33 | */ | |
34 | ||
35 | #define PWG_DeviceUUID "DUU" | |
36 | #define PWG_Event "E" | |
37 | #define PWG_LogNaturalLanguage "NL" | |
38 | #define PWG_Status "S" | |
39 | #define PWG_ServiceURI "URI" | |
40 | #define PWG_UserHost "UH" | |
41 | #define PWG_UserName "UN" | |
42 | #define PWG_UserURI "UU" | |
43 | #define PWG_ServiceIsAcceptingJobs "IAJ" | |
44 | #define PWG_ServiceState "ST" | |
45 | #define PWG_ServiceStateReasons "SR" | |
46 | #define PWG_ServiceUUID "SUU" | |
47 | #define PWG_JobID "JID" | |
48 | #define PWG_JobUUID "JUU" | |
49 | #define PWG_JobImagesCompleted "JIM" | |
50 | #define PWG_JobImpressionsCompleted "JIC" | |
51 | #define PWG_JobDestinationURI "JD" | |
52 | #define PWG_JobState "JS" | |
53 | #define PWG_JobStateReasons "JR" | |
54 | #define PWG_JobAccountingID "JA" | |
55 | #define PWG_JobAcountingUserName "JAUN" | |
56 | #define PWG_JobAccountingUserURI "JAUU" | |
57 | ||
58 | ||
75bd9771 MS |
59 | /* |
60 | * Local globals... | |
61 | */ | |
62 | ||
28a463e0 MS |
63 | static _cups_mutex_t log_mutex = _CUPS_MUTEX_INITIALIZER; |
64 | /* Mutex for logging */ | |
7e86f2f6 | 65 | static size_t log_linesize = 0; /* Size of line for output file */ |
75bd9771 MS |
66 | static char *log_line = NULL; /* Line for output file */ |
67 | ||
0dc4a6bf MS |
68 | #ifdef HAVE_ASL_H |
69 | static const int log_levels[] = /* ASL levels... */ | |
70 | { | |
71 | ASL_LEVEL_EMERG, | |
72 | ASL_LEVEL_EMERG, | |
73 | ASL_LEVEL_ALERT, | |
74 | ASL_LEVEL_CRIT, | |
75 | ASL_LEVEL_ERR, | |
76 | ASL_LEVEL_WARNING, | |
77 | ASL_LEVEL_NOTICE, | |
78 | ASL_LEVEL_INFO, | |
79 | ASL_LEVEL_DEBUG, | |
80 | ASL_LEVEL_DEBUG | |
81 | }; | |
82 | #elif defined(HAVE_VSYSLOG) || defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
83 | static const int log_levels[] = /* SYSLOG levels... */ | |
cb7f98ee MS |
84 | { |
85 | 0, | |
86 | LOG_EMERG, | |
87 | LOG_ALERT, | |
88 | LOG_CRIT, | |
89 | LOG_ERR, | |
90 | LOG_WARNING, | |
91 | LOG_NOTICE, | |
92 | LOG_INFO, | |
93 | LOG_DEBUG, | |
94 | LOG_DEBUG | |
95 | }; | |
0dc4a6bf | 96 | #endif /* HAVE_ASL_H */ |
cb7f98ee | 97 | |
75bd9771 | 98 | |
ef416fc2 | 99 | /* |
100 | * Local functions... | |
101 | */ | |
102 | ||
005dd1eb | 103 | static int format_log_line(const char *message, va_list ap); |
ef416fc2 | 104 | |
105 | ||
22c9029b MS |
106 | /* |
107 | * 'cupsdCheckLogFile()' - Open/rotate a log file if it needs it. | |
108 | */ | |
109 | ||
110 | int /* O - 1 if log file open */ | |
111 | cupsdCheckLogFile(cups_file_t **lf, /* IO - Log file */ | |
112 | const char *logname) /* I - Log filename */ | |
113 | { | |
114 | char backname[1024], /* Backup log filename */ | |
115 | filename[1024], /* Formatted log filename */ | |
116 | *ptr; /* Pointer into filename */ | |
117 | const char *logptr; /* Pointer into log filename */ | |
118 | ||
119 | ||
120 | /* | |
121 | * See if we have a log file to check... | |
122 | */ | |
123 | ||
124 | if (!lf || !logname || !logname[0]) | |
125 | return (1); | |
126 | ||
127 | /* | |
128 | * Format the filename as needed... | |
129 | */ | |
130 | ||
131 | if (!*lf || | |
132 | (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize && | |
133 | MaxLogSize > 0)) | |
134 | { | |
135 | /* | |
136 | * Handle format strings... | |
137 | */ | |
138 | ||
139 | filename[sizeof(filename) - 1] = '\0'; | |
140 | ||
141 | if (logname[0] != '/') | |
142 | { | |
143 | strlcpy(filename, ServerRoot, sizeof(filename)); | |
144 | strlcat(filename, "/", sizeof(filename)); | |
145 | } | |
146 | else | |
147 | filename[0] = '\0'; | |
148 | ||
149 | for (logptr = logname, ptr = filename + strlen(filename); | |
150 | *logptr && ptr < (filename + sizeof(filename) - 1); | |
151 | logptr ++) | |
152 | if (*logptr == '%') | |
153 | { | |
154 | /* | |
155 | * Format spec... | |
156 | */ | |
157 | ||
158 | logptr ++; | |
159 | if (*logptr == 's') | |
160 | { | |
161 | /* | |
162 | * Insert the server name... | |
163 | */ | |
164 | ||
7e86f2f6 | 165 | strlcpy(ptr, ServerName, sizeof(filename) - (size_t)(ptr - filename)); |
22c9029b MS |
166 | ptr += strlen(ptr); |
167 | } | |
168 | else | |
169 | { | |
170 | /* | |
171 | * Otherwise just insert the character... | |
172 | */ | |
173 | ||
174 | *ptr++ = *logptr; | |
175 | } | |
176 | } | |
177 | else | |
178 | *ptr++ = *logptr; | |
179 | ||
180 | *ptr = '\0'; | |
181 | } | |
182 | ||
183 | /* | |
184 | * See if the log file is open... | |
185 | */ | |
186 | ||
187 | if (!*lf) | |
188 | { | |
189 | /* | |
190 | * Nope, open the log file... | |
191 | */ | |
192 | ||
193 | if ((*lf = cupsFileOpen(filename, "a")) == NULL) | |
194 | { | |
195 | /* | |
196 | * If the file is in CUPS_LOGDIR then try to create a missing directory... | |
197 | */ | |
198 | ||
199 | if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR))) | |
200 | { | |
201 | /* | |
202 | * Try updating the permissions of the containing log directory, using | |
203 | * the log file permissions as a basis... | |
204 | */ | |
205 | ||
7e86f2f6 | 206 | mode_t log_dir_perm = (mode_t)(0300 | LogFilePerm); |
22c9029b MS |
207 | /* LogFilePerm + owner write/search */ |
208 | if (log_dir_perm & 0040) | |
209 | log_dir_perm |= 0010; /* Add group search */ | |
210 | if (log_dir_perm & 0004) | |
211 | log_dir_perm |= 0001; /* Add other search */ | |
212 | ||
7e86f2f6 | 213 | cupsdCheckPermissions(CUPS_LOGDIR, NULL, log_dir_perm, RunUser, Group, 1, -1); |
22c9029b MS |
214 | |
215 | *lf = cupsFileOpen(filename, "a"); | |
216 | } | |
217 | ||
218 | if (*lf == NULL) | |
219 | { | |
0dc4a6bf MS |
220 | #ifdef HAVE_ASL_H |
221 | asl_object_t m; /* Log message */ | |
222 | ||
223 | m = asl_new(ASL_TYPE_MSG); | |
224 | asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd"); | |
225 | asl_log(NULL, m, ASL_LEVEL_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno)); | |
226 | asl_release(m); | |
227 | ||
228 | #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
229 | sd_journal_print(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno)); | |
230 | ||
231 | #else | |
232 | syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno)); | |
233 | #endif /* HAVE_ASL_H */ | |
22c9029b MS |
234 | |
235 | if (FatalErrors & CUPSD_FATAL_LOG) | |
236 | cupsdEndProcess(getpid(), 0); | |
237 | ||
238 | return (0); | |
239 | } | |
240 | } | |
241 | ||
242 | if (strncmp(filename, "/dev/", 5)) | |
243 | { | |
244 | /* | |
245 | * Change ownership and permissions of non-device logs... | |
246 | */ | |
247 | ||
248 | fchown(cupsFileNumber(*lf), RunUser, Group); | |
249 | fchmod(cupsFileNumber(*lf), LogFilePerm); | |
250 | } | |
251 | } | |
252 | ||
253 | /* | |
254 | * Do we need to rotate the log? | |
255 | */ | |
256 | ||
257 | if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize && | |
258 | MaxLogSize > 0) | |
259 | { | |
260 | /* | |
261 | * Rotate log file... | |
262 | */ | |
263 | ||
264 | cupsFileClose(*lf); | |
265 | ||
5a9febac | 266 | strlcpy(backname, filename, sizeof(backname)); |
22c9029b MS |
267 | strlcat(backname, ".O", sizeof(backname)); |
268 | ||
269 | unlink(backname); | |
270 | rename(filename, backname); | |
271 | ||
272 | if ((*lf = cupsFileOpen(filename, "a")) == NULL) | |
273 | { | |
0dc4a6bf MS |
274 | #ifdef HAVE_ASL_H |
275 | asl_object_t m; /* Log message */ | |
276 | ||
277 | m = asl_new(ASL_TYPE_MSG); | |
278 | asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd"); | |
279 | asl_log(NULL, m, ASL_LEVEL_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno)); | |
280 | asl_release(m); | |
281 | ||
282 | #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
283 | sd_journal_print(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno)); | |
284 | ||
285 | #else | |
286 | syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno)); | |
287 | #endif /* HAVE_ASL_H */ | |
22c9029b MS |
288 | |
289 | if (FatalErrors & CUPSD_FATAL_LOG) | |
290 | cupsdEndProcess(getpid(), 0); | |
291 | ||
292 | return (0); | |
293 | } | |
294 | ||
295 | /* | |
296 | * Change ownership and permissions of non-device logs... | |
297 | */ | |
298 | ||
299 | fchown(cupsFileNumber(*lf), RunUser, Group); | |
300 | fchmod(cupsFileNumber(*lf), LogFilePerm); | |
301 | } | |
302 | ||
303 | return (1); | |
304 | } | |
305 | ||
306 | ||
ef416fc2 | 307 | /* |
308 | * 'cupsdGetDateTime()' - Returns a pointer to a date/time string. | |
309 | */ | |
310 | ||
311 | char * /* O - Date/time string */ | |
dfd5680b MS |
312 | cupsdGetDateTime(struct timeval *t, /* I - Time value or NULL for current */ |
313 | cupsd_time_t format) /* I - Format to use */ | |
ef416fc2 | 314 | { |
dfd5680b MS |
315 | struct timeval curtime; /* Current time value */ |
316 | struct tm *date; /* Date/time value */ | |
317 | static struct timeval last_time = { 0, 0 }; | |
318 | /* Last time we formatted */ | |
319 | static char s[1024]; /* Date/time string */ | |
ef416fc2 | 320 | static const char * const months[12] =/* Months */ |
321 | { | |
322 | "Jan", | |
323 | "Feb", | |
324 | "Mar", | |
325 | "Apr", | |
326 | "May", | |
327 | "Jun", | |
328 | "Jul", | |
329 | "Aug", | |
330 | "Sep", | |
331 | "Oct", | |
332 | "Nov", | |
333 | "Dec" | |
334 | }; | |
335 | ||
336 | ||
839a51c8 MS |
337 | /* |
338 | * Make sure we have a valid time... | |
339 | */ | |
340 | ||
341 | if (!t) | |
dfd5680b MS |
342 | { |
343 | gettimeofday(&curtime, NULL); | |
344 | t = &curtime; | |
345 | } | |
839a51c8 | 346 | |
dfd5680b MS |
347 | if (t->tv_sec != last_time.tv_sec || |
348 | (LogTimeFormat == CUPSD_TIME_USECS && t->tv_usec != last_time.tv_usec)) | |
ef416fc2 | 349 | { |
dfd5680b | 350 | last_time = *t; |
ef416fc2 | 351 | |
352 | /* | |
353 | * Get the date and time from the UNIX time value, and then format it | |
354 | * into a string. Note that we *can't* use the strftime() function since | |
355 | * it is localized and will seriously confuse automatic programs if the | |
356 | * month names are in the wrong language! | |
357 | * | |
358 | * Also, we use the "timezone" variable that contains the current timezone | |
359 | * offset from GMT in seconds so that we are reporting local time in the | |
360 | * log files. If you want GMT, set the TZ environment variable accordingly | |
361 | * before starting the scheduler. | |
362 | * | |
363 | * (*BSD and Darwin store the timezone offset in the tm structure) | |
364 | */ | |
365 | ||
dfd5680b | 366 | date = localtime(&(t->tv_sec)); |
ef416fc2 | 367 | |
dfd5680b MS |
368 | if (format == CUPSD_TIME_STANDARD) |
369 | snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]", | |
370 | date->tm_mday, months[date->tm_mon], 1900 + date->tm_year, | |
371 | date->tm_hour, date->tm_min, date->tm_sec, | |
372 | #ifdef HAVE_TM_GMTOFF | |
373 | date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60); | |
374 | #else | |
375 | timezone / 3600, (timezone / 60) % 60); | |
376 | #endif /* HAVE_TM_GMTOFF */ | |
377 | else | |
378 | snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d.%06d %+03ld%02ld]", | |
379 | date->tm_mday, months[date->tm_mon], 1900 + date->tm_year, | |
bf3816c7 | 380 | date->tm_hour, date->tm_min, date->tm_sec, (int)t->tv_usec, |
ef416fc2 | 381 | #ifdef HAVE_TM_GMTOFF |
dfd5680b | 382 | date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60); |
ef416fc2 | 383 | #else |
dfd5680b | 384 | timezone / 3600, (timezone / 60) % 60); |
ef416fc2 | 385 | #endif /* HAVE_TM_GMTOFF */ |
386 | } | |
387 | ||
388 | return (s); | |
389 | } | |
390 | ||
391 | ||
22c9029b MS |
392 | /* |
393 | * 'cupsdLogFCMessage()' - Log a file checking message. | |
394 | */ | |
395 | ||
396 | void | |
397 | cupsdLogFCMessage( | |
398 | void *context, /* I - Printer (if any) */ | |
399 | _cups_fc_result_t result, /* I - Check result */ | |
400 | const char *message) /* I - Message to log */ | |
401 | { | |
402 | cupsd_printer_t *p = (cupsd_printer_t *)context; | |
403 | /* Printer */ | |
404 | cupsd_loglevel_t level; /* Log level */ | |
405 | ||
406 | ||
407 | if (result == _CUPS_FILE_CHECK_OK) | |
408 | level = CUPSD_LOG_DEBUG2; | |
409 | else | |
410 | level = CUPSD_LOG_ERROR; | |
411 | ||
412 | if (p) | |
413 | { | |
414 | cupsdLogMessage(level, "%s: %s", p->name, message); | |
415 | ||
416 | if (result == _CUPS_FILE_CHECK_MISSING || | |
417 | result == _CUPS_FILE_CHECK_WRONG_TYPE) | |
418 | { | |
419 | strlcpy(p->state_message, message, sizeof(p->state_message)); | |
420 | ||
421 | if (cupsdSetPrinterReasons(p, "+cups-missing-filter-warning")) | |
422 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, p, NULL, "%s", message); | |
423 | } | |
88f9aafc MS |
424 | else if (result == _CUPS_FILE_CHECK_PERMISSIONS || |
425 | result == _CUPS_FILE_CHECK_RELATIVE_PATH) | |
22c9029b MS |
426 | { |
427 | strlcpy(p->state_message, message, sizeof(p->state_message)); | |
428 | ||
429 | if (cupsdSetPrinterReasons(p, "+cups-insecure-filter-warning")) | |
430 | cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, p, NULL, "%s", message); | |
431 | } | |
432 | } | |
433 | else | |
434 | cupsdLogMessage(level, "%s", message); | |
435 | } | |
436 | ||
437 | ||
f7deaa1a | 438 | #ifdef HAVE_GSSAPI |
439 | /* | |
440 | * 'cupsdLogGSSMessage()' - Log a GSSAPI error... | |
441 | */ | |
442 | ||
443 | int /* O - 1 on success, 0 on error */ | |
444 | cupsdLogGSSMessage( | |
445 | int level, /* I - Log level */ | |
7e86f2f6 MS |
446 | OM_uint32 major_status, /* I - Major GSSAPI status */ |
447 | OM_uint32 minor_status, /* I - Minor GSSAPI status */ | |
f7deaa1a | 448 | const char *message, /* I - printf-style message string */ |
449 | ...) /* I - Additional args as needed */ | |
450 | { | |
451 | OM_uint32 err_major_status, /* Major status code for display */ | |
452 | err_minor_status; /* Minor status code for display */ | |
453 | OM_uint32 msg_ctx; /* Message context */ | |
454 | gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER, | |
455 | /* Major status message */ | |
456 | minor_status_string = GSS_C_EMPTY_BUFFER; | |
457 | /* Minor status message */ | |
458 | int ret; /* Return value */ | |
dcb445bc | 459 | char buffer[8192]; /* Buffer for vsnprintf */ |
f7deaa1a | 460 | |
461 | ||
dcb445bc MS |
462 | if (strchr(message, '%')) |
463 | { | |
464 | /* | |
465 | * Format the message string... | |
466 | */ | |
467 | ||
468 | va_list ap; /* Pointer to arguments */ | |
469 | ||
470 | va_start(ap, message); | |
471 | vsnprintf(buffer, sizeof(buffer), message, ap); | |
472 | va_end(ap); | |
473 | ||
474 | message = buffer; | |
475 | } | |
476 | ||
f7deaa1a | 477 | msg_ctx = 0; |
478 | err_major_status = gss_display_status(&err_minor_status, | |
479 | major_status, | |
480 | GSS_C_GSS_CODE, | |
481 | GSS_C_NO_OID, | |
482 | &msg_ctx, | |
483 | &major_status_string); | |
484 | ||
485 | if (!GSS_ERROR(err_major_status)) | |
1f0275e3 MS |
486 | gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE, |
487 | GSS_C_NULL_OID, &msg_ctx, &minor_status_string); | |
f7deaa1a | 488 | |
489 | ret = cupsdLogMessage(level, "%s: %s, %s", message, | |
490 | (char *)major_status_string.value, | |
491 | (char *)minor_status_string.value); | |
492 | gss_release_buffer(&err_minor_status, &major_status_string); | |
493 | gss_release_buffer(&err_minor_status, &minor_status_string); | |
494 | ||
495 | return (ret); | |
496 | } | |
497 | #endif /* HAVE_GSSAPI */ | |
498 | ||
499 | ||
996acce8 MS |
500 | /* |
501 | * 'cupsdLogClient()' - Log a client message. | |
502 | */ | |
503 | ||
504 | int /* O - 1 on success, 0 on error */ | |
505 | cupsdLogClient(cupsd_client_t *con, /* I - Client connection */ | |
506 | int level, /* I - Log level */ | |
507 | const char *message, /* I - Printf-style message string */ | |
508 | ...) /* I - Additional arguments as needed */ | |
509 | { | |
510 | va_list ap, ap2; /* Argument pointers */ | |
511 | char clientmsg[1024];/* Format string for client message */ | |
512 | int status; /* Formatting status */ | |
513 | ||
514 | ||
515 | /* | |
516 | * See if we want to log this message... | |
517 | */ | |
518 | ||
519 | if (TestConfigFile || !ErrorLog) | |
520 | return (1); | |
521 | ||
522 | if (level > LogLevel) | |
523 | return (1); | |
524 | ||
525 | /* | |
526 | * Format and write the log message... | |
527 | */ | |
528 | ||
529 | if (con) | |
530 | snprintf(clientmsg, sizeof(clientmsg), "[Client %d] %s", con->number, | |
531 | message); | |
532 | else | |
533 | strlcpy(clientmsg, message, sizeof(clientmsg)); | |
534 | ||
535 | va_start(ap, message); | |
536 | ||
537 | do | |
538 | { | |
539 | va_copy(ap2, ap); | |
540 | status = format_log_line(clientmsg, ap2); | |
541 | va_end(ap2); | |
542 | } | |
543 | while (status == 0); | |
544 | ||
545 | va_end(ap); | |
546 | ||
547 | if (status > 0) | |
548 | return (cupsdWriteErrorLog(level, log_line)); | |
549 | else | |
550 | return (cupsdWriteErrorLog(CUPSD_LOG_ERROR, | |
551 | "Unable to allocate memory for log line.")); | |
552 | } | |
553 | ||
554 | ||
ef416fc2 | 555 | /* |
75bd9771 | 556 | * 'cupsdLogJob()' - Log a job message. |
ef416fc2 | 557 | */ |
558 | ||
559 | int /* O - 1 on success, 0 on error */ | |
75bd9771 MS |
560 | cupsdLogJob(cupsd_job_t *job, /* I - Job */ |
561 | int level, /* I - Log level */ | |
562 | const char *message, /* I - Printf-style message string */ | |
563 | ...) /* I - Additional arguments as needed */ | |
ef416fc2 | 564 | { |
dcb445bc | 565 | va_list ap, ap2; /* Argument pointers */ |
005dd1eb MS |
566 | char jobmsg[1024]; /* Format string for job message */ |
567 | int status; /* Formatting status */ | |
ef416fc2 | 568 | |
569 | ||
570 | /* | |
571 | * See if we want to log this message... | |
572 | */ | |
573 | ||
94da7e34 MS |
574 | if (TestConfigFile || !ErrorLog) |
575 | return (1); | |
576 | ||
178cb736 MS |
577 | if ((level > LogLevel || |
578 | (level == CUPSD_LOG_INFO && LogLevel < CUPSD_LOG_DEBUG)) && | |
579 | LogDebugHistory <= 0) | |
2e4ff8af | 580 | return (1); |
2e4ff8af | 581 | |
0dc4a6bf MS |
582 | #ifdef HAVE_ASL_H |
583 | if (!strcmp(ErrorLog, "syslog")) | |
584 | { | |
585 | asl_object_t m; /* Log message */ | |
586 | char job_id[32], /* job-id string */ | |
587 | completed[32]; /* job-impressions-completed string */ | |
588 | static const char * const job_states[] = | |
589 | { /* job-state strings */ | |
590 | "Pending", | |
591 | "PendingHeld", | |
592 | "Processing", | |
593 | "ProcessingStopped", | |
594 | "Canceled", | |
595 | "Aborted", | |
596 | "Completed" | |
597 | }; | |
598 | ||
599 | snprintf(job_id, sizeof(job_id), "%d", job->id); | |
600 | ||
601 | m = asl_new(ASL_TYPE_MSG); | |
602 | asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd"); | |
603 | asl_set(m, PWG_Event, "JobStateChanged"); | |
604 | asl_set(m, PWG_ServiceURI, job->printer->uri); | |
605 | asl_set(m, PWG_JobID, job_id); | |
606 | asl_set(m, PWG_JobState, job_states[job->state_value - IPP_JSTATE_PENDING]); | |
607 | ||
608 | if (job->impressions) | |
609 | { | |
610 | snprintf(completed, sizeof(completed), "%d", ippGetInteger(job->impressions, 0)); | |
611 | asl_set(m, PWG_JobImpressionsCompleted, completed); | |
612 | } | |
613 | ||
614 | va_start(ap, message); | |
615 | asl_vlog(NULL, m, log_levels[level], message, ap); | |
616 | va_end(ap); | |
617 | ||
618 | asl_release(m); | |
619 | return (1); | |
620 | } | |
621 | ||
622 | #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
623 | if (!strcmp(ErrorLog, "syslog")) | |
624 | { | |
625 | char completed[32]; /* job-impressions-completed string */ | |
626 | static const char * const job_states[] = | |
627 | { /* job-state strings */ | |
628 | "Pending", | |
629 | "PendingHeld", | |
630 | "Processing", | |
631 | "ProcessingStopped", | |
632 | "Canceled", | |
633 | "Aborted", | |
634 | "Completed" | |
635 | }; | |
636 | ||
637 | va_start(ap, message); | |
638 | ||
639 | do | |
640 | { | |
641 | va_copy(ap2, ap); | |
642 | status = format_log_line(message, ap2); | |
643 | va_end(ap2); | |
644 | } | |
645 | while (status == 0); | |
646 | ||
647 | va_end(ap); | |
648 | ||
649 | sd_journal_send("MESSAGE=%s", log_line, | |
650 | "PRIORITY=%i", log_levels[level], | |
651 | PWG_Event"=JobStateChanged", | |
652 | PWG_ServiceURI"=%s", job->printer->uri, | |
653 | PWG_JobID"=%d", job->id, | |
654 | PWG_JobState"=%s", job_states[job->state_value - IPP_JSTATE_PENDING], | |
655 | PWG_JobImpressionsCompleted"=%d", ippGetInteger(job->impressions, 0), | |
656 | NULL); | |
657 | return (1); | |
658 | } | |
659 | ||
660 | #endif /* HAVE_ASL_H */ | |
661 | ||
ef416fc2 | 662 | /* |
75bd9771 | 663 | * Format and write the log message... |
ef416fc2 | 664 | */ |
665 | ||
dcb445bc MS |
666 | if (job) |
667 | snprintf(jobmsg, sizeof(jobmsg), "[Job %d] %s", job->id, message); | |
668 | else | |
669 | strlcpy(jobmsg, message, sizeof(jobmsg)); | |
670 | ||
671 | va_start(ap, message); | |
ef416fc2 | 672 | |
005dd1eb MS |
673 | do |
674 | { | |
dcb445bc MS |
675 | va_copy(ap2, ap); |
676 | status = format_log_line(jobmsg, ap2); | |
677 | va_end(ap2); | |
005dd1eb MS |
678 | } |
679 | while (status == 0); | |
771bd8cb | 680 | |
dcb445bc MS |
681 | va_end(ap); |
682 | ||
005dd1eb | 683 | if (status > 0) |
178cb736 | 684 | { |
dcb445bc MS |
685 | if (job && |
686 | (level > LogLevel || | |
178cb736 MS |
687 | (level == CUPSD_LOG_INFO && LogLevel < CUPSD_LOG_DEBUG)) && |
688 | LogDebugHistory > 0) | |
689 | { | |
690 | /* | |
691 | * Add message to the job history... | |
692 | */ | |
693 | ||
694 | cupsd_joblog_t *temp; /* Copy of log message */ | |
ff2b08f9 MS |
695 | size_t log_len = strlen(log_line); |
696 | /* Length of log message */ | |
178cb736 | 697 | |
ff2b08f9 | 698 | if ((temp = malloc(sizeof(cupsd_joblog_t) + log_len)) != NULL) |
178cb736 MS |
699 | { |
700 | temp->time = time(NULL); | |
ff2b08f9 | 701 | memcpy(temp->message, log_line, log_len + 1); |
178cb736 MS |
702 | } |
703 | ||
704 | if (!job->history) | |
705 | job->history = cupsArrayNew(NULL, NULL); | |
706 | ||
707 | if (job->history && temp) | |
708 | { | |
709 | cupsArrayAdd(job->history, temp); | |
710 | ||
711 | if (cupsArrayCount(job->history) > LogDebugHistory) | |
712 | { | |
713 | /* | |
714 | * Remove excess messages... | |
715 | */ | |
716 | ||
717 | temp = cupsArrayFirst(job->history); | |
718 | cupsArrayRemove(job->history, temp); | |
719 | free(temp); | |
720 | } | |
721 | } | |
722 | else if (temp) | |
723 | free(temp); | |
724 | ||
725 | return (1); | |
726 | } | |
727 | else if (level <= LogLevel && | |
728 | (level != CUPSD_LOG_INFO || LogLevel >= CUPSD_LOG_DEBUG)) | |
729 | return (cupsdWriteErrorLog(level, log_line)); | |
730 | else | |
731 | return (1); | |
732 | } | |
75bd9771 MS |
733 | else |
734 | return (cupsdWriteErrorLog(CUPSD_LOG_ERROR, | |
996acce8 | 735 | "Unable to allocate memory for log line.")); |
75bd9771 | 736 | } |
ef416fc2 | 737 | |
ef416fc2 | 738 | |
75bd9771 MS |
739 | /* |
740 | * 'cupsdLogMessage()' - Log a message to the error log file. | |
741 | */ | |
ef416fc2 | 742 | |
75bd9771 MS |
743 | int /* O - 1 on success, 0 on error */ |
744 | cupsdLogMessage(int level, /* I - Log level */ | |
745 | const char *message, /* I - printf-style message string */ | |
746 | ...) /* I - Additional args as needed */ | |
747 | { | |
dcb445bc | 748 | va_list ap, ap2; /* Argument pointers */ |
005dd1eb | 749 | int status; /* Formatting status */ |
ef416fc2 | 750 | |
ef416fc2 | 751 | |
752 | /* | |
75bd9771 | 753 | * See if we want to log this message... |
ef416fc2 | 754 | */ |
755 | ||
85dda01c | 756 | if ((TestConfigFile || !ErrorLog) && level <= CUPSD_LOG_WARN) |
ef416fc2 | 757 | { |
a4845881 | 758 | va_start(ap, message); |
0dc4a6bf MS |
759 | |
760 | #ifdef HAVE_ASL_H | |
761 | asl_object_t m; /* Log message */ | |
762 | ||
763 | m = asl_new(ASL_TYPE_MSG); | |
764 | asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd"); | |
765 | asl_vlog(NULL, m, log_levels[level], message, ap); | |
766 | asl_release(m); | |
767 | ||
768 | #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
769 | sd_journal_printv(log_levels[level], message, ap); | |
770 | ||
771 | #elif defined(HAVE_VSYSLOG) | |
772 | vsyslog(LOG_LPR | log_levels[level], message, ap); | |
773 | ||
cb7f98ee | 774 | #else |
a4845881 MS |
775 | vfprintf(stderr, message, ap); |
776 | putc('\n', stderr); | |
cb7f98ee | 777 | #endif /* HAVE_VSYSLOG */ |
0dc4a6bf | 778 | |
a4845881 | 779 | va_end(ap); |
ef416fc2 | 780 | |
75bd9771 | 781 | return (1); |
ef416fc2 | 782 | } |
783 | ||
75bd9771 MS |
784 | if (level > LogLevel || !ErrorLog) |
785 | return (1); | |
ef416fc2 | 786 | |
0dc4a6bf MS |
787 | #ifdef HAVE_ASL_H |
788 | if (!strcmp(ErrorLog, "syslog")) | |
789 | { | |
790 | asl_object_t m; /* Log message */ | |
791 | ||
792 | m = asl_new(ASL_TYPE_MSG); | |
793 | asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd"); | |
794 | ||
795 | va_start(ap, message); | |
796 | asl_vlog(NULL, m, log_levels[level], message, ap); | |
797 | va_end(ap); | |
798 | ||
799 | asl_release(m); | |
800 | return (1); | |
801 | } | |
802 | ||
803 | #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
804 | sd_journal_printv(log_levels[level], message, ap); | |
805 | ||
806 | #endif /* HAVE_ASL_H */ | |
807 | ||
ef416fc2 | 808 | /* |
75bd9771 | 809 | * Format and write the log message... |
ef416fc2 | 810 | */ |
811 | ||
dcb445bc MS |
812 | va_start(ap, message); |
813 | ||
005dd1eb MS |
814 | do |
815 | { | |
dcb445bc MS |
816 | va_copy(ap2, ap); |
817 | status = format_log_line(message, ap2); | |
818 | va_end(ap2); | |
005dd1eb MS |
819 | } |
820 | while (status == 0); | |
ef416fc2 | 821 | |
dcb445bc MS |
822 | va_end(ap); |
823 | ||
005dd1eb MS |
824 | if (status > 0) |
825 | return (cupsdWriteErrorLog(level, log_line)); | |
75bd9771 MS |
826 | else |
827 | return (cupsdWriteErrorLog(CUPSD_LOG_ERROR, | |
828 | "Unable to allocate memory for log line!")); | |
ef416fc2 | 829 | } |
830 | ||
831 | ||
832 | /* | |
833 | * 'cupsdLogPage()' - Log a page to the page log file. | |
834 | */ | |
835 | ||
836 | int /* O - 1 on success, 0 on error */ | |
837 | cupsdLogPage(cupsd_job_t *job, /* I - Job being printed */ | |
838 | const char *page) /* I - Page being printed */ | |
839 | { | |
01ce6322 MS |
840 | int i; /* Looping var */ |
841 | char buffer[2048], /* Buffer for page log */ | |
842 | *bufptr, /* Pointer into buffer */ | |
843 | name[256]; /* Attribute name */ | |
844 | const char *format, /* Pointer into PageLogFormat */ | |
845 | *nameend; /* End of attribute name */ | |
846 | ipp_attribute_t *attr; /* Current attribute */ | |
771bd8cb MS |
847 | char number[256]; /* Page number */ |
848 | int copies; /* Number of copies */ | |
ef416fc2 | 849 | |
850 | ||
01ce6322 MS |
851 | /* |
852 | * Format the line going into the page log... | |
853 | */ | |
854 | ||
855 | if (!PageLogFormat) | |
856 | return (1); | |
857 | ||
5a9febac | 858 | strlcpy(number, "1", sizeof(number)); |
771bd8cb MS |
859 | copies = 1; |
860 | sscanf(page, "%255s%d", number, &copies); | |
01ce6322 MS |
861 | |
862 | for (format = PageLogFormat, bufptr = buffer; *format; format ++) | |
863 | { | |
864 | if (*format == '%') | |
865 | { | |
866 | format ++; | |
867 | ||
868 | switch (*format) | |
869 | { | |
870 | case '%' : /* Literal % */ | |
871 | if (bufptr < (buffer + sizeof(buffer) - 1)) | |
872 | *bufptr++ = '%'; | |
873 | break; | |
874 | ||
875 | case 'p' : /* Printer name */ | |
7e86f2f6 | 876 | strlcpy(bufptr, job->printer->name, sizeof(buffer) - (size_t)(bufptr - buffer)); |
01ce6322 MS |
877 | bufptr += strlen(bufptr); |
878 | break; | |
879 | ||
880 | case 'j' : /* Job ID */ | |
7e86f2f6 | 881 | snprintf(bufptr, sizeof(buffer) - (size_t)(bufptr - buffer), "%d", job->id); |
01ce6322 MS |
882 | bufptr += strlen(bufptr); |
883 | break; | |
884 | ||
885 | case 'u' : /* Username */ | |
7e86f2f6 | 886 | strlcpy(bufptr, job->username ? job->username : "-", sizeof(buffer) - (size_t)(bufptr - buffer)); |
01ce6322 MS |
887 | bufptr += strlen(bufptr); |
888 | break; | |
889 | ||
890 | case 'T' : /* Date and time */ | |
7e86f2f6 | 891 | strlcpy(bufptr, cupsdGetDateTime(NULL, LogTimeFormat), sizeof(buffer) - (size_t)(bufptr - buffer)); |
01ce6322 MS |
892 | bufptr += strlen(bufptr); |
893 | break; | |
894 | ||
895 | case 'P' : /* Page number */ | |
7e86f2f6 | 896 | strlcpy(bufptr, number, sizeof(buffer) - (size_t)(bufptr - buffer)); |
01ce6322 MS |
897 | bufptr += strlen(bufptr); |
898 | break; | |
899 | ||
900 | case 'C' : /* Number of copies */ | |
7e86f2f6 | 901 | snprintf(bufptr, sizeof(buffer) - (size_t)(bufptr - buffer), "%d", copies); |
01ce6322 MS |
902 | bufptr += strlen(bufptr); |
903 | break; | |
904 | ||
905 | case '{' : /* {attribute} */ | |
7e86f2f6 | 906 | if ((nameend = strchr(format, '}')) != NULL && (size_t)(nameend - format - 2) < (sizeof(name) - 1)) |
01ce6322 MS |
907 | { |
908 | /* | |
909 | * Pull the name from inside the brackets... | |
910 | */ | |
911 | ||
07623986 | 912 | memcpy(name, format + 1, (size_t)(nameend - format - 1)); |
75bd9771 MS |
913 | name[nameend - format - 1] = '\0'; |
914 | ||
915 | format = nameend; | |
01ce6322 | 916 | |
bf1bc4c6 MS |
917 | attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO); |
918 | if (!attr && !strcmp(name, "job-billing")) | |
919 | { | |
920 | /* | |
921 | * Handle alias "job-account-id" (which was standardized after | |
922 | * "job-billing" was defined for CUPS... | |
923 | */ | |
924 | ||
925 | attr = ippFindAttribute(job->attrs, "job-account-id", IPP_TAG_ZERO); | |
926 | } | |
927 | else if (!attr && !strcmp(name, "media")) | |
928 | { | |
929 | /* | |
930 | * Handle alias "media-col" which uses dimensions instead of | |
931 | * names... | |
932 | */ | |
933 | ||
934 | attr = ippFindAttribute(job->attrs, "media-col/media-size", IPP_TAG_BEGIN_COLLECTION); | |
935 | } | |
936 | ||
937 | if (attr) | |
01ce6322 MS |
938 | { |
939 | /* | |
940 | * Add the attribute value... | |
941 | */ | |
942 | ||
01ce6322 MS |
943 | for (i = 0; |
944 | i < attr->num_values && | |
945 | bufptr < (buffer + sizeof(buffer) - 1); | |
946 | i ++) | |
947 | { | |
948 | if (i) | |
949 | *bufptr++ = ','; | |
950 | ||
951 | switch (attr->value_tag) | |
952 | { | |
953 | case IPP_TAG_INTEGER : | |
954 | case IPP_TAG_ENUM : | |
7e86f2f6 | 955 | snprintf(bufptr, sizeof(buffer) - (size_t)(bufptr - buffer), "%d", attr->values[i].integer); |
01ce6322 MS |
956 | bufptr += strlen(bufptr); |
957 | break; | |
958 | ||
959 | case IPP_TAG_BOOLEAN : | |
7e86f2f6 | 960 | snprintf(bufptr, sizeof(buffer) - (size_t)(bufptr - buffer), "%d", attr->values[i].boolean); |
01ce6322 MS |
961 | bufptr += strlen(bufptr); |
962 | break; | |
963 | ||
964 | case IPP_TAG_TEXTLANG : | |
965 | case IPP_TAG_NAMELANG : | |
966 | case IPP_TAG_TEXT : | |
967 | case IPP_TAG_NAME : | |
968 | case IPP_TAG_KEYWORD : | |
969 | case IPP_TAG_URI : | |
970 | case IPP_TAG_URISCHEME : | |
971 | case IPP_TAG_CHARSET : | |
972 | case IPP_TAG_LANGUAGE : | |
973 | case IPP_TAG_MIMETYPE : | |
7e86f2f6 | 974 | strlcpy(bufptr, attr->values[i].string.text, sizeof(buffer) - (size_t)(bufptr - buffer)); |
01ce6322 MS |
975 | bufptr += strlen(bufptr); |
976 | break; | |
977 | ||
bf1bc4c6 MS |
978 | case IPP_TAG_BEGIN_COLLECTION : |
979 | if (!strcmp(attr->name, "media-size")) | |
980 | { | |
981 | ipp_attribute_t *x_dimension = ippFindAttribute(ippGetCollection(attr, 0), "x-dimension", IPP_TAG_INTEGER); | |
982 | ipp_attribute_t *y_dimension = ippFindAttribute(ippGetCollection(attr, 0), "y-dimension", IPP_TAG_INTEGER); | |
983 | /* Media dimensions */ | |
984 | ||
985 | if (x_dimension && y_dimension) | |
986 | { | |
987 | pwg_media_t *pwg = pwgMediaForSize(ippGetInteger(x_dimension, 0), ippGetInteger(y_dimension, 0)); | |
988 | /* PWG media name */ | |
989 | strlcpy(bufptr, pwg->pwg, sizeof(buffer) - (size_t)(bufptr - buffer)); | |
990 | break; | |
991 | } | |
992 | } | |
993 | ||
01ce6322 | 994 | default : |
7e86f2f6 | 995 | strlcpy(bufptr, "???", sizeof(buffer) - (size_t)(bufptr - buffer)); |
01ce6322 MS |
996 | bufptr += strlen(bufptr); |
997 | break; | |
998 | } | |
999 | } | |
1000 | } | |
1001 | else if (bufptr < (buffer + sizeof(buffer) - 1)) | |
1002 | *bufptr++ = '-'; | |
1003 | break; | |
1004 | } | |
1005 | ||
1006 | default : | |
1007 | if (bufptr < (buffer + sizeof(buffer) - 2)) | |
1008 | { | |
1009 | *bufptr++ = '%'; | |
1010 | *bufptr++ = *format; | |
1011 | } | |
1012 | break; | |
1013 | } | |
1014 | } | |
1015 | else if (bufptr < (buffer + sizeof(buffer) - 1)) | |
1016 | *bufptr++ = *format; | |
1017 | } | |
ef416fc2 | 1018 | |
01ce6322 | 1019 | *bufptr = '\0'; |
771bd8cb | 1020 | |
0dc4a6bf MS |
1021 | #ifdef HAVE_ASL_H |
1022 | if (!strcmp(ErrorLog, "syslog")) | |
1023 | { | |
1024 | asl_object_t m; /* Log message */ | |
1025 | char job_id[32], /* job-id string */ | |
1026 | completed[32]; /* job-impressions-completed string */ | |
1027 | static const char * const job_states[] = | |
1028 | { /* job-state strings */ | |
1029 | "Pending", | |
1030 | "PendingHeld", | |
1031 | "Processing", | |
1032 | "ProcessingStopped", | |
1033 | "Canceled", | |
1034 | "Aborted", | |
1035 | "Completed" | |
1036 | }; | |
1037 | ||
1038 | snprintf(job_id, sizeof(job_id), "%d", job->id); | |
1039 | ||
1040 | m = asl_new(ASL_TYPE_MSG); | |
1041 | asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd"); | |
1042 | asl_set(m, PWG_Event, "JobStateChanged"); | |
1043 | asl_set(m, PWG_ServiceURI, job->printer->uri); | |
1044 | asl_set(m, PWG_JobID, job_id); | |
1045 | asl_set(m, PWG_JobState, job_states[job->state_value - IPP_JSTATE_PENDING]); | |
1046 | ||
1047 | if (job->impressions) | |
1048 | { | |
1049 | snprintf(completed, sizeof(completed), "%d", ippGetInteger(job->impressions, 0)); | |
1050 | asl_set(m, PWG_JobImpressionsCompleted, completed); | |
1051 | } | |
1052 | ||
1053 | asl_log(NULL, m, ASL_LEVEL_INFO, "%s", buffer); | |
1054 | ||
1055 | asl_release(m); | |
1056 | return (1); | |
1057 | } | |
1058 | ||
1059 | #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
1060 | if (!strcmp(ErrorLog, "syslog")) | |
1061 | { | |
1062 | char completed[32]; /* job-impressions-completed string */ | |
1063 | static const char * const job_states[] = | |
1064 | { /* job-state strings */ | |
1065 | "Pending", | |
1066 | "PendingHeld", | |
1067 | "Processing", | |
1068 | "ProcessingStopped", | |
1069 | "Canceled", | |
1070 | "Aborted", | |
1071 | "Completed" | |
1072 | }; | |
1073 | ||
1074 | va_start(ap, message); | |
1075 | ||
1076 | do | |
1077 | { | |
1078 | va_copy(ap2, ap); | |
1079 | status = format_log_line(message, ap2); | |
1080 | va_end(ap2); | |
1081 | } | |
1082 | while (status == 0); | |
1083 | ||
1084 | va_end(ap); | |
1085 | ||
1086 | sd_journal_send("MESSAGE=%s", log_line, | |
1087 | "PRIORITY=%i", log_levels[level], | |
1088 | PWG_Event"=JobStateChanged", | |
1089 | PWG_ServiceURI"=%s", job->printer->uri, | |
1090 | PWG_JobID"=%d", job->id, | |
1091 | PWG_JobState"=%s", job_states[job->state_value - IPP_JSTATE_PENDING], | |
1092 | PWG_JobImpressionsCompleted"=%d", ippGetInteger(job->impressions, 0), | |
1093 | NULL); | |
1094 | return (1); | |
1095 | } | |
1096 | ||
1097 | #elif defined(HAVE_VSYSLOG) | |
ef416fc2 | 1098 | /* |
1099 | * See if we are logging pages via syslog... | |
1100 | */ | |
1101 | ||
1102 | if (!strcmp(PageLog, "syslog")) | |
1103 | { | |
01ce6322 | 1104 | syslog(LOG_INFO, "%s", buffer); |
ef416fc2 | 1105 | |
1106 | return (1); | |
1107 | } | |
0dc4a6bf | 1108 | #endif /* HAVE_ASL_H */ |
ef416fc2 | 1109 | |
1110 | /* | |
1111 | * Not using syslog; check the log file... | |
1112 | */ | |
1113 | ||
22c9029b | 1114 | if (!cupsdCheckLogFile(&PageFile, PageLog)) |
ef416fc2 | 1115 | return (0); |
1116 | ||
1117 | /* | |
1118 | * Print a page log entry of the form: | |
1119 | * | |
ac884b6a | 1120 | * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \ |
ef416fc2 | 1121 | * billing hostname |
1122 | */ | |
1123 | ||
01ce6322 | 1124 | cupsFilePrintf(PageFile, "%s\n", buffer); |
ef416fc2 | 1125 | cupsFileFlush(PageFile); |
1126 | ||
1127 | return (1); | |
1128 | } | |
1129 | ||
1130 | ||
1131 | /* | |
1132 | * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format. | |
1133 | */ | |
1134 | ||
1135 | int /* O - 1 on success, 0 on error */ | |
1136 | cupsdLogRequest(cupsd_client_t *con, /* I - Request to log */ | |
1137 | http_status_t code) /* I - Response code */ | |
1138 | { | |
839a51c8 | 1139 | char temp[2048]; /* Temporary string for URI */ |
ef416fc2 | 1140 | static const char * const states[] = /* HTTP client states... */ |
1141 | { | |
1142 | "WAITING", | |
1143 | "OPTIONS", | |
1144 | "GET", | |
1145 | "GET", | |
1146 | "HEAD", | |
1147 | "POST", | |
1148 | "POST", | |
1149 | "POST", | |
1150 | "PUT", | |
1151 | "PUT", | |
1152 | "DELETE", | |
1153 | "TRACE", | |
1154 | "CLOSE", | |
1155 | "STATUS" | |
1156 | }; | |
1157 | ||
1158 | ||
1f0275e3 MS |
1159 | /* |
1160 | * Filter requests as needed... | |
1161 | */ | |
1162 | ||
c9dcc485 MS |
1163 | if (AccessLogLevel == CUPSD_ACCESSLOG_NONE) |
1164 | return (1); | |
1165 | else if (AccessLogLevel < CUPSD_ACCESSLOG_ALL) | |
1f0275e3 MS |
1166 | { |
1167 | /* | |
ba55dc12 | 1168 | * Eliminate simple GET, POST, and PUT requests... |
1f0275e3 MS |
1169 | */ |
1170 | ||
1171 | if ((con->operation == HTTP_GET && | |
1172 | strncmp(con->uri, "/admin/conf", 11) && | |
1173 | strncmp(con->uri, "/admin/log", 10)) || | |
1174 | (con->operation == HTTP_POST && !con->request && | |
1175 | strncmp(con->uri, "/admin", 6)) || | |
ba55dc12 MS |
1176 | (con->operation != HTTP_GET && con->operation != HTTP_POST && |
1177 | con->operation != HTTP_PUT)) | |
1f0275e3 MS |
1178 | return (1); |
1179 | ||
1180 | if (con->request && con->response && | |
e07d4801 MS |
1181 | (con->response->request.status.status_code < IPP_REDIRECTION_OTHER_SITE || |
1182 | con->response->request.status.status_code == IPP_NOT_FOUND)) | |
1f0275e3 MS |
1183 | { |
1184 | /* | |
1185 | * Check successful requests... | |
1186 | */ | |
1187 | ||
1188 | ipp_op_t op = con->request->request.op.operation_id; | |
1189 | static cupsd_accesslog_t standard_ops[] = | |
1190 | { | |
1191 | CUPSD_ACCESSLOG_ALL, /* reserved */ | |
1192 | CUPSD_ACCESSLOG_ALL, /* reserved */ | |
1193 | CUPSD_ACCESSLOG_ACTIONS,/* Print-Job */ | |
1194 | CUPSD_ACCESSLOG_ACTIONS,/* Print-URI */ | |
1195 | CUPSD_ACCESSLOG_ACTIONS,/* Validate-Job */ | |
1196 | CUPSD_ACCESSLOG_ACTIONS,/* Create-Job */ | |
1197 | CUPSD_ACCESSLOG_ACTIONS,/* Send-Document */ | |
1198 | CUPSD_ACCESSLOG_ACTIONS,/* Send-URI */ | |
1199 | CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Job */ | |
1200 | CUPSD_ACCESSLOG_ALL, /* Get-Job-Attributes */ | |
1201 | CUPSD_ACCESSLOG_ALL, /* Get-Jobs */ | |
1202 | CUPSD_ACCESSLOG_ALL, /* Get-Printer-Attributes */ | |
1203 | CUPSD_ACCESSLOG_ACTIONS,/* Hold-Job */ | |
1204 | CUPSD_ACCESSLOG_ACTIONS,/* Release-Job */ | |
1205 | CUPSD_ACCESSLOG_ACTIONS,/* Restart-Job */ | |
1206 | CUPSD_ACCESSLOG_ALL, /* reserved */ | |
1207 | CUPSD_ACCESSLOG_CONFIG, /* Pause-Printer */ | |
1208 | CUPSD_ACCESSLOG_CONFIG, /* Resume-Printer */ | |
1209 | CUPSD_ACCESSLOG_CONFIG, /* Purge-Jobs */ | |
1210 | CUPSD_ACCESSLOG_CONFIG, /* Set-Printer-Attributes */ | |
1211 | CUPSD_ACCESSLOG_ACTIONS,/* Set-Job-Attributes */ | |
1212 | CUPSD_ACCESSLOG_CONFIG, /* Get-Printer-Supported-Values */ | |
1213 | CUPSD_ACCESSLOG_ACTIONS,/* Create-Printer-Subscription */ | |
1214 | CUPSD_ACCESSLOG_ACTIONS,/* Create-Job-Subscription */ | |
1215 | CUPSD_ACCESSLOG_ALL, /* Get-Subscription-Attributes */ | |
1216 | CUPSD_ACCESSLOG_ALL, /* Get-Subscriptions */ | |
1217 | CUPSD_ACCESSLOG_ACTIONS,/* Renew-Subscription */ | |
1218 | CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Subscription */ | |
1219 | CUPSD_ACCESSLOG_ALL, /* Get-Notifications */ | |
1220 | CUPSD_ACCESSLOG_ACTIONS,/* Send-Notifications */ | |
1221 | CUPSD_ACCESSLOG_ALL, /* reserved */ | |
1222 | CUPSD_ACCESSLOG_ALL, /* reserved */ | |
1223 | CUPSD_ACCESSLOG_ALL, /* reserved */ | |
1224 | CUPSD_ACCESSLOG_ALL, /* Get-Print-Support-Files */ | |
1225 | CUPSD_ACCESSLOG_CONFIG, /* Enable-Printer */ | |
1226 | CUPSD_ACCESSLOG_CONFIG, /* Disable-Printer */ | |
1227 | CUPSD_ACCESSLOG_CONFIG, /* Pause-Printer-After-Current-Job */ | |
1228 | CUPSD_ACCESSLOG_ACTIONS,/* Hold-New-Jobs */ | |
1229 | CUPSD_ACCESSLOG_ACTIONS,/* Release-Held-New-Jobs */ | |
1230 | CUPSD_ACCESSLOG_CONFIG, /* Deactivate-Printer */ | |
1231 | CUPSD_ACCESSLOG_CONFIG, /* Activate-Printer */ | |
1232 | CUPSD_ACCESSLOG_CONFIG, /* Restart-Printer */ | |
1233 | CUPSD_ACCESSLOG_CONFIG, /* Shutdown-Printer */ | |
1234 | CUPSD_ACCESSLOG_CONFIG, /* Startup-Printer */ | |
1235 | CUPSD_ACCESSLOG_ACTIONS,/* Reprocess-Job */ | |
1236 | CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Current-Job */ | |
1237 | CUPSD_ACCESSLOG_ACTIONS,/* Suspend-Current-Job */ | |
1238 | CUPSD_ACCESSLOG_ACTIONS,/* Resume-Job */ | |
1239 | CUPSD_ACCESSLOG_ACTIONS,/* Promote-Job */ | |
1240 | CUPSD_ACCESSLOG_ACTIONS /* Schedule-Job-After */ | |
1241 | }; | |
1242 | static cupsd_accesslog_t cups_ops[] = | |
1243 | { | |
1244 | CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Default */ | |
1245 | CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Printers */ | |
1246 | CUPSD_ACCESSLOG_CONFIG, /* CUPS-Add-Modify-Printer */ | |
1247 | CUPSD_ACCESSLOG_CONFIG, /* CUPS-Delete-Printer */ | |
1248 | CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Classes */ | |
1249 | CUPSD_ACCESSLOG_CONFIG, /* CUPS-Add-Modify-Class */ | |
1250 | CUPSD_ACCESSLOG_CONFIG, /* CUPS-Delete-Class */ | |
1251 | CUPSD_ACCESSLOG_CONFIG, /* CUPS-Accept-Jobs */ | |
1252 | CUPSD_ACCESSLOG_CONFIG, /* CUPS-Reject-Jobs */ | |
1253 | CUPSD_ACCESSLOG_CONFIG, /* CUPS-Set-Default */ | |
1254 | CUPSD_ACCESSLOG_CONFIG, /* CUPS-Get-Devices */ | |
1255 | CUPSD_ACCESSLOG_CONFIG, /* CUPS-Get-PPDs */ | |
1256 | CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Move-Job */ | |
1257 | CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Authenticate-Job */ | |
1258 | CUPSD_ACCESSLOG_ALL /* CUPS-Get-PPD */ | |
1259 | }; | |
771bd8cb | 1260 | |
1f0275e3 MS |
1261 | |
1262 | if ((op <= IPP_SCHEDULE_JOB_AFTER && standard_ops[op] > AccessLogLevel) || | |
1263 | (op >= CUPS_GET_DEFAULT && op <= CUPS_GET_PPD && | |
1264 | cups_ops[op - CUPS_GET_DEFAULT] > AccessLogLevel)) | |
1265 | return (1); | |
1266 | } | |
1267 | } | |
1268 | ||
0dc4a6bf MS |
1269 | #ifdef HAVE_ASL_H |
1270 | if (!strcmp(ErrorLog, "syslog")) | |
1271 | { | |
1272 | asl_object_t m; /* Log message */ | |
1273 | ||
1274 | m = asl_new(ASL_TYPE_MSG); | |
1275 | asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd"); | |
1276 | ||
1277 | asl_log(NULL, m, ASL_LEVEL_INFO, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n", | |
1278 | con->http->hostname, con->username[0] != '\0' ? con->username : "-", | |
1279 | states[con->operation], _httpEncodeURI(temp, con->uri, sizeof(temp)), | |
1280 | con->http->version / 100, con->http->version % 100, | |
1281 | code, CUPS_LLCAST con->bytes, | |
1282 | con->request ? | |
1283 | ippOpString(con->request->request.op.operation_id) : "-", | |
1284 | con->response ? | |
1285 | ippErrorString(con->response->request.status.status_code) : "-"); | |
1286 | ||
1287 | asl_release(m); | |
1288 | return (1); | |
1289 | } | |
1290 | ||
1291 | #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
1292 | sd_journal_print(LOG_INFO, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s", con->http->hostname, con->username[0] != '\0' ? con->username : "-", states[con->operation], _httpEncodeURI(temp, con->uri, sizeof(temp)), con->http->version / 100, con->http->version % 100, code, CUPS_LLCAST con->bytes, con->request ? ippOpString(con->request->request.op.operation_id) : "-", con->response ? ippErrorString(con->response->request.status.status_code) : "-"); | |
1293 | ||
1294 | #elif defined(HAVE_VSYSLOG) | |
ef416fc2 | 1295 | /* |
1296 | * See if we are logging accesses via syslog... | |
1297 | */ | |
1298 | ||
1299 | if (!strcmp(AccessLog, "syslog")) | |
1300 | { | |
2abf387c | 1301 | syslog(LOG_INFO, |
1302 | "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n", | |
996acce8 | 1303 | con->http->hostname, con->username[0] != '\0' ? con->username : "-", |
839a51c8 | 1304 | states[con->operation], _httpEncodeURI(temp, con->uri, sizeof(temp)), |
996acce8 | 1305 | con->http->version / 100, con->http->version % 100, |
2abf387c | 1306 | code, CUPS_LLCAST con->bytes, |
1307 | con->request ? | |
1308 | ippOpString(con->request->request.op.operation_id) : "-", | |
1309 | con->response ? | |
1310 | ippErrorString(con->response->request.status.status_code) : "-"); | |
ef416fc2 | 1311 | |
1312 | return (1); | |
1313 | } | |
0dc4a6bf | 1314 | #endif /* HAVE_ASL_H */ |
ef416fc2 | 1315 | |
1316 | /* | |
1317 | * Not using syslog; check the log file... | |
1318 | */ | |
1319 | ||
22c9029b | 1320 | if (!cupsdCheckLogFile(&AccessFile, AccessLog)) |
ef416fc2 | 1321 | return (0); |
1322 | ||
1323 | /* | |
1324 | * Write a log of the request in "common log format"... | |
1325 | */ | |
1326 | ||
1327 | cupsFilePrintf(AccessFile, | |
1328 | "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n", | |
996acce8 | 1329 | con->http->hostname, |
c8fef167 | 1330 | con->username[0] != '\0' ? con->username : "-", |
dfd5680b MS |
1331 | cupsdGetDateTime(&(con->start), LogTimeFormat), |
1332 | states[con->operation], | |
839a51c8 | 1333 | _httpEncodeURI(temp, con->uri, sizeof(temp)), |
996acce8 | 1334 | con->http->version / 100, con->http->version % 100, |
ef416fc2 | 1335 | code, CUPS_LLCAST con->bytes, |
1336 | con->request ? | |
1337 | ippOpString(con->request->request.op.operation_id) : "-", | |
1338 | con->response ? | |
1339 | ippErrorString(con->response->request.status.status_code) : | |
1340 | "-"); | |
1341 | ||
1342 | cupsFileFlush(AccessFile); | |
1343 | ||
1344 | return (1); | |
1345 | } | |
1346 | ||
1347 | ||
75bd9771 MS |
1348 | /* |
1349 | * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog. | |
1350 | */ | |
1351 | ||
1352 | int /* O - 1 on success, 0 on failure */ | |
1353 | cupsdWriteErrorLog(int level, /* I - Log level */ | |
1354 | const char *message) /* I - Message string */ | |
1355 | { | |
28a463e0 | 1356 | int ret = 1; /* Return value */ |
75bd9771 MS |
1357 | static const char levels[] = /* Log levels... */ |
1358 | { | |
1359 | ' ', | |
1360 | 'X', | |
1361 | 'A', | |
1362 | 'C', | |
1363 | 'E', | |
1364 | 'W', | |
1365 | 'N', | |
1366 | 'I', | |
1367 | 'D', | |
1368 | 'd' | |
1369 | }; | |
75bd9771 MS |
1370 | |
1371 | ||
0dc4a6bf MS |
1372 | #ifdef HAVE_ASL_H |
1373 | if (!strcmp(ErrorLog, "syslog")) | |
1374 | { | |
1375 | asl_object_t m; /* Log message */ | |
1376 | ||
1377 | m = asl_new(ASL_TYPE_MSG); | |
1378 | asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd"); | |
1379 | asl_log(NULL, m, ASL_LEVEL_INFO, "%s", message); | |
1380 | ||
1381 | asl_release(m); | |
1382 | return (1); | |
1383 | } | |
1384 | ||
1385 | #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
1386 | sd_journal_print(log_levels[level], "%s", message); | |
1387 | ||
1388 | #elif defined(HAVE_VSYSLOG) | |
75bd9771 MS |
1389 | /* |
1390 | * See if we are logging errors via syslog... | |
1391 | */ | |
1392 | ||
1393 | if (!strcmp(ErrorLog, "syslog")) | |
1394 | { | |
0dc4a6bf | 1395 | syslog(log_levels[level], "%s", message); |
75bd9771 MS |
1396 | return (1); |
1397 | } | |
0dc4a6bf | 1398 | #endif /* HAVE_ASL_H */ |
75bd9771 MS |
1399 | |
1400 | /* | |
1401 | * Not using syslog; check the log file... | |
1402 | */ | |
1403 | ||
28a463e0 MS |
1404 | _cupsMutexLock(&log_mutex); |
1405 | ||
22c9029b | 1406 | if (!cupsdCheckLogFile(&ErrorFile, ErrorLog)) |
28a463e0 MS |
1407 | { |
1408 | ret = 0; | |
1409 | } | |
1410 | else | |
1411 | { | |
1412 | /* | |
1413 | * Write the log message... | |
1414 | */ | |
75bd9771 | 1415 | |
28a463e0 MS |
1416 | cupsFilePrintf(ErrorFile, "%c %s %s\n", levels[level], |
1417 | cupsdGetDateTime(NULL, LogTimeFormat), message); | |
1418 | cupsFileFlush(ErrorFile); | |
1419 | } | |
75bd9771 | 1420 | |
28a463e0 | 1421 | _cupsMutexUnlock(&log_mutex); |
75bd9771 | 1422 | |
28a463e0 | 1423 | return (ret); |
75bd9771 MS |
1424 | } |
1425 | ||
1426 | ||
ef416fc2 | 1427 | /* |
75bd9771 MS |
1428 | * 'format_log_line()' - Format a line for a log file. |
1429 | * | |
1430 | * This function resizes a global string buffer as needed. Each call returns | |
1431 | * a pointer to this buffer, so the contents are only good until the next call | |
1432 | * to format_log_line()... | |
1433 | */ | |
1434 | ||
005dd1eb | 1435 | static int /* O - -1 for fatal, 0 for retry, 1 for success */ |
75bd9771 MS |
1436 | format_log_line(const char *message, /* I - Printf-style format string */ |
1437 | va_list ap) /* I - Argument list */ | |
1438 | { | |
7e86f2f6 | 1439 | ssize_t len; /* Length of formatted line */ |
75bd9771 MS |
1440 | |
1441 | ||
1442 | /* | |
1443 | * Allocate the line buffer as needed... | |
1444 | */ | |
1445 | ||
1446 | if (!log_linesize) | |
1447 | { | |
1448 | log_linesize = 8192; | |
1449 | log_line = malloc(log_linesize); | |
1450 | ||
1451 | if (!log_line) | |
005dd1eb | 1452 | return (-1); |
75bd9771 MS |
1453 | } |
1454 | ||
1455 | /* | |
1456 | * Format the log message... | |
1457 | */ | |
1458 | ||
1459 | len = vsnprintf(log_line, log_linesize, message, ap); | |
1460 | ||
1461 | /* | |
1462 | * Resize the buffer as needed... | |
1463 | */ | |
1464 | ||
7e86f2f6 | 1465 | if ((size_t)len >= log_linesize && log_linesize < 65536) |
75bd9771 MS |
1466 | { |
1467 | char *temp; /* Temporary string pointer */ | |
1468 | ||
1469 | ||
1470 | len ++; | |
1471 | ||
1472 | if (len < 8192) | |
1473 | len = 8192; | |
1474 | else if (len > 65536) | |
1475 | len = 65536; | |
1476 | ||
7e86f2f6 | 1477 | temp = realloc(log_line, (size_t)len); |
75bd9771 MS |
1478 | |
1479 | if (temp) | |
1480 | { | |
1481 | log_line = temp; | |
7e86f2f6 | 1482 | log_linesize = (size_t)len; |
75bd9771 | 1483 | |
005dd1eb MS |
1484 | return (0); |
1485 | } | |
75bd9771 MS |
1486 | } |
1487 | ||
005dd1eb | 1488 | return (1); |
75bd9771 MS |
1489 | } |
1490 | ||
1491 | ||
1492 | /* | |
f2d18633 | 1493 | * End of "$Id$". |
ef416fc2 | 1494 | */ |