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