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