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