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