]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/log.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / log.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: log.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * Log file routines for the Common UNIX Printing System (CUPS).
5 *
bc44d920 6 * Copyright 2007 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...
19 * cupsdLogMessage() - Log a message to the error log file.
20 * cupsdLogPage() - Log a page to the page log file.
21 * cupsdLogRequest() - Log an HTTP request in Common Log Format.
22 * check_log_file() - Open/rotate a log file if it needs it.
ef416fc2 23 */
24
25/*
26 * Include necessary headers...
27 */
28
29#include "cupsd.h"
30#include <stdarg.h>
b423cd4c 31#include <syslog.h>
ef416fc2 32
33
34/*
35 * Local functions...
36 */
37
38static int check_log_file(cups_file_t **, const char *);
39
40
41/*
42 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
43 */
44
45char * /* O - Date/time string */
46cupsdGetDateTime(time_t t) /* I - Time value */
47{
48 struct tm *date; /* Date/time value */
49 static time_t last_time = -1; /* Last time value */
50 static char s[1024]; /* Date/time string */
51 static const char * const months[12] =/* Months */
52 {
53 "Jan",
54 "Feb",
55 "Mar",
56 "Apr",
57 "May",
58 "Jun",
59 "Jul",
60 "Aug",
61 "Sep",
62 "Oct",
63 "Nov",
64 "Dec"
65 };
66
67
68 if (t != last_time)
69 {
70 last_time = t;
71
72 /*
73 * Get the date and time from the UNIX time value, and then format it
74 * into a string. Note that we *can't* use the strftime() function since
75 * it is localized and will seriously confuse automatic programs if the
76 * month names are in the wrong language!
77 *
78 * Also, we use the "timezone" variable that contains the current timezone
79 * offset from GMT in seconds so that we are reporting local time in the
80 * log files. If you want GMT, set the TZ environment variable accordingly
81 * before starting the scheduler.
82 *
83 * (*BSD and Darwin store the timezone offset in the tm structure)
84 */
85
86 date = localtime(&t);
87
88 snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
89 date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
90 date->tm_hour, date->tm_min, date->tm_sec,
91#ifdef HAVE_TM_GMTOFF
92 date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
93#else
94 timezone / 3600, (timezone / 60) % 60);
95#endif /* HAVE_TM_GMTOFF */
96 }
97
98 return (s);
99}
100
101
f7deaa1a 102#ifdef HAVE_GSSAPI
103/*
104 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
105 */
106
107int /* O - 1 on success, 0 on error */
108cupsdLogGSSMessage(
109 int level, /* I - Log level */
110 int major_status, /* I - Major GSSAPI status */
111 int minor_status, /* I - Minor GSSAPI status */
112 const char *message, /* I - printf-style message string */
113 ...) /* I - Additional args as needed */
114{
115 OM_uint32 err_major_status, /* Major status code for display */
116 err_minor_status; /* Minor status code for display */
117 OM_uint32 msg_ctx; /* Message context */
118 gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
119 /* Major status message */
120 minor_status_string = GSS_C_EMPTY_BUFFER;
121 /* Minor status message */
122 int ret; /* Return value */
123
124
125 msg_ctx = 0;
126 err_major_status = gss_display_status(&err_minor_status,
127 major_status,
128 GSS_C_GSS_CODE,
129 GSS_C_NO_OID,
130 &msg_ctx,
131 &major_status_string);
132
133 if (!GSS_ERROR(err_major_status))
134 err_major_status = gss_display_status(&err_minor_status,
135 minor_status,
136 GSS_C_MECH_CODE,
137 GSS_C_NULL_OID,
138 &msg_ctx,
139 &minor_status_string);
140
141 ret = cupsdLogMessage(level, "%s: %s, %s", message,
142 (char *)major_status_string.value,
143 (char *)minor_status_string.value);
144 gss_release_buffer(&err_minor_status, &major_status_string);
145 gss_release_buffer(&err_minor_status, &minor_status_string);
146
147 return (ret);
148}
149#endif /* HAVE_GSSAPI */
150
151
ef416fc2 152/*
153 * 'cupsdLogMessage()' - Log a message to the error log file.
154 */
155
156int /* O - 1 on success, 0 on error */
157cupsdLogMessage(int level, /* I - Log level */
158 const char *message, /* I - printf-style message string */
159 ...) /* I - Additional args as needed */
160{
161 int len; /* Length of message */
162 va_list ap; /* Argument pointer */
163 static const char levels[] = /* Log levels... */
164 {
165 ' ',
166 'X',
167 'A',
168 'C',
169 'E',
170 'W',
171 'N',
172 'I',
173 'D',
174 'd'
175 };
176#ifdef HAVE_VSYSLOG
177 static const int syslevels[] = /* SYSLOG levels... */
178 {
179 0,
180 LOG_EMERG,
181 LOG_ALERT,
182 LOG_CRIT,
183 LOG_ERR,
184 LOG_WARNING,
185 LOG_NOTICE,
186 LOG_INFO,
187 LOG_DEBUG,
188 LOG_DEBUG
189 };
190#endif /* HAVE_VSYSLOG */
191 static int linesize = 0; /* Size of line for output file */
192 static char *line = NULL; /* Line for output file */
193
194
195 /*
196 * See if we want to log this message...
197 */
198
199 if (level > LogLevel || !ErrorLog)
200 return (1);
201
202#ifdef HAVE_VSYSLOG
203 /*
204 * See if we are logging errors via syslog...
205 */
206
207 if (!strcmp(ErrorLog, "syslog"))
208 {
209 va_start(ap, message);
210 vsyslog(syslevels[level], message, ap);
211 va_end(ap);
212
213 return (1);
214 }
215#endif /* HAVE_VSYSLOG */
216
217 /*
218 * Not using syslog; check the log file...
219 */
220
221 if (!check_log_file(&ErrorFile, ErrorLog))
222 return (0);
223
224 /*
225 * Print the log level and date/time...
226 */
227
228 cupsFilePrintf(ErrorFile, "%c %s ", levels[level], cupsdGetDateTime(time(NULL)));
229
230 /*
231 * Allocate the line buffer as needed...
232 */
233
234 if (!linesize)
235 {
236 linesize = 8192;
237 line = malloc(linesize);
238
239 if (!line)
240 {
241 cupsFilePrintf(ErrorFile,
242 "ERROR: Unable to allocate memory for line - %s\n",
243 strerror(errno));
244 cupsFileFlush(ErrorFile);
245
246 return (0);
247 }
248 }
249
250 /*
251 * Format the log message...
252 */
253
254 va_start(ap, message);
255 len = vsnprintf(line, linesize, message, ap);
256 va_end(ap);
257
258 /*
259 * Resize the buffer as needed...
260 */
261
262 if (len >= linesize)
263 {
264 len ++;
265
266 if (len < 8192)
267 len = 8192;
268 else if (len > 65536)
269 len = 65536;
270
271 line = realloc(line, len);
272
273 if (line)
274 linesize = len;
275 else
276 {
277 cupsFilePrintf(ErrorFile,
278 "ERROR: Unable to allocate memory for line - %s\n",
279 strerror(errno));
280 cupsFileFlush(ErrorFile);
281
282 return (0);
283 }
284
285 va_start(ap, message);
286 len = vsnprintf(line, linesize, message, ap);
287 va_end(ap);
288 }
289
290 if (len >= linesize)
291 len = linesize - 1;
292
293 /*
294 * Then the log message...
295 */
296
297 cupsFilePuts(ErrorFile, line);
298
299 /*
300 * Then a newline...
301 */
302
303 if (len > 0 && line[len - 1] != '\n')
304 cupsFilePutChar(ErrorFile, '\n');
305
306 /*
307 * Flush the line to the file and return...
308 */
309
310 cupsFileFlush(ErrorFile);
311
312 return (1);
313}
314
315
316/*
317 * 'cupsdLogPage()' - Log a page to the page log file.
318 */
319
320int /* O - 1 on success, 0 on error */
321cupsdLogPage(cupsd_job_t *job, /* I - Job being printed */
322 const char *page) /* I - Page being printed */
323{
324 ipp_attribute_t *billing, /* job-billing attribute */
325 *hostname; /* job-originating-host-name attribute */
326
327
328 billing = ippFindAttribute(job->attrs, "job-billing", IPP_TAG_ZERO);
329 hostname = ippFindAttribute(job->attrs, "job-originating-host-name",
330 IPP_TAG_ZERO);
331
332#ifdef HAVE_VSYSLOG
333 /*
334 * See if we are logging pages via syslog...
335 */
336
337 if (!strcmp(PageLog, "syslog"))
338 {
339 syslog(LOG_INFO, "PAGE %s %s %d %s %s %s", job->printer->name,
340 job->username ? job->username : "-",
341 job->id, page, billing ? billing->values[0].string.text : "-",
342 hostname->values[0].string.text);
343
344 return (1);
345 }
346#endif /* HAVE_VSYSLOG */
347
348 /*
349 * Not using syslog; check the log file...
350 */
351
352 if (!check_log_file(&PageFile, PageLog))
353 return (0);
354
355 /*
356 * Print a page log entry of the form:
357 *
358 * printer job-id user [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
359 * billing hostname
360 */
361
362 cupsFilePrintf(PageFile, "%s %s %d %s %s %s %s\n", job->printer->name,
363 job->username ? job->username : "-",
364 job->id, cupsdGetDateTime(time(NULL)), page,
365 billing ? billing->values[0].string.text : "-",
366 hostname->values[0].string.text);
367 cupsFileFlush(PageFile);
368
369 return (1);
370}
371
372
373/*
374 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
375 */
376
377int /* O - 1 on success, 0 on error */
378cupsdLogRequest(cupsd_client_t *con, /* I - Request to log */
379 http_status_t code) /* I - Response code */
380{
381 static const char * const states[] = /* HTTP client states... */
382 {
383 "WAITING",
384 "OPTIONS",
385 "GET",
386 "GET",
387 "HEAD",
388 "POST",
389 "POST",
390 "POST",
391 "PUT",
392 "PUT",
393 "DELETE",
394 "TRACE",
395 "CLOSE",
396 "STATUS"
397 };
398
399
400#ifdef HAVE_VSYSLOG
401 /*
402 * See if we are logging accesses via syslog...
403 */
404
405 if (!strcmp(AccessLog, "syslog"))
406 {
2abf387c 407 syslog(LOG_INFO,
408 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
ef416fc2 409 con->http.hostname, con->username[0] != '\0' ? con->username : "-",
410 states[con->operation], con->uri,
411 con->http.version / 100, con->http.version % 100,
2abf387c 412 code, CUPS_LLCAST con->bytes,
413 con->request ?
414 ippOpString(con->request->request.op.operation_id) : "-",
415 con->response ?
416 ippErrorString(con->response->request.status.status_code) : "-");
ef416fc2 417
418 return (1);
419 }
420#endif /* HAVE_VSYSLOG */
421
422 /*
423 * Not using syslog; check the log file...
424 */
425
426 if (!check_log_file(&AccessFile, AccessLog))
427 return (0);
428
429 /*
430 * Write a log of the request in "common log format"...
431 */
432
433 cupsFilePrintf(AccessFile,
434 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
435 con->http.hostname, con->username[0] != '\0' ? con->username : "-",
436 cupsdGetDateTime(con->start), states[con->operation], con->uri,
437 con->http.version / 100, con->http.version % 100,
438 code, CUPS_LLCAST con->bytes,
439 con->request ?
440 ippOpString(con->request->request.op.operation_id) : "-",
441 con->response ?
442 ippErrorString(con->response->request.status.status_code) :
443 "-");
444
445 cupsFileFlush(AccessFile);
446
447 return (1);
448}
449
450
451/*
452 * 'check_log_file()' - Open/rotate a log file if it needs it.
453 */
454
455static int /* O - 1 if log file open */
456check_log_file(cups_file_t **lf, /* IO - Log file */
457 const char *logname) /* I - Log filename */
458{
f7deaa1a 459 char backname[1024], /* Backup log filename */
460 filename[1024], /* Formatted log filename */
461 *ptr; /* Pointer into filename */
462 const char *logptr; /* Pointer into log filename */
ef416fc2 463
464
465 /*
466 * See if we have a log file to check...
467 */
468
469 if (!lf || !logname || !logname[0])
470 return (1);
471
472 /*
473 * Format the filename as needed...
474 */
475
a74454a7 476 if (!*lf ||
477 (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
478 MaxLogSize > 0))
ef416fc2 479 {
480 /*
481 * Handle format strings...
482 */
483
484 filename[sizeof(filename) - 1] = '\0';
485
486 if (logname[0] != '/')
487 {
488 strlcpy(filename, ServerRoot, sizeof(filename));
489 strlcat(filename, "/", sizeof(filename));
490 }
491 else
492 filename[0] = '\0';
493
f7deaa1a 494 for (logptr = logname, ptr = filename + strlen(filename);
495 *logptr && ptr < (filename + sizeof(filename) - 1);
496 logptr ++)
497 if (*logptr == '%')
ef416fc2 498 {
499 /*
500 * Format spec...
501 */
502
f7deaa1a 503 logptr ++;
504 if (*logptr == 's')
ef416fc2 505 {
506 /*
507 * Insert the server name...
508 */
509
510 strlcpy(ptr, ServerName, sizeof(filename) - (ptr - filename));
511 ptr += strlen(ptr);
512 }
513 else
514 {
515 /*
516 * Otherwise just insert the character...
517 */
518
f7deaa1a 519 *ptr++ = *logptr;
ef416fc2 520 }
521 }
522 else
f7deaa1a 523 *ptr++ = *logptr;
ef416fc2 524
525 *ptr = '\0';
526 }
527
528 /*
529 * See if the log file is open...
530 */
531
532 if (!*lf)
533 {
534 /*
535 * Nope, open the log file...
536 */
537
538 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
539 {
b94498cf 540 /*
541 * If the file is in CUPS_LOGDIR then try to create a missing directory...
542 */
ef416fc2 543
b94498cf 544 if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR)))
545 {
546 cupsdCheckPermissions(CUPS_LOGDIR, NULL, 0755, RunUser, Group, 1, -1);
547
548 *lf = cupsFileOpen(filename, "a");
549 }
550
551 if (*lf == NULL)
552 {
553 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
554 strerror(errno));
555 return (0);
556 }
ef416fc2 557 }
558
559 if (strncmp(filename, "/dev/", 5))
560 {
561 /*
562 * Change ownership and permissions of non-device logs...
563 */
564
565 fchown(cupsFileNumber(*lf), RunUser, Group);
566 fchmod(cupsFileNumber(*lf), LogFilePerm);
567 }
568 }
569
570 /*
571 * Do we need to rotate the log?
572 */
573
a74454a7 574 if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
575 MaxLogSize > 0)
ef416fc2 576 {
577 /*
578 * Rotate log file...
579 */
580
581 cupsFileClose(*lf);
582
583 strcpy(backname, filename);
584 strlcat(backname, ".O", sizeof(backname));
585
586 unlink(backname);
587 rename(filename, backname);
588
589 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
590 {
591 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
592 strerror(errno));
593
594 return (0);
595 }
596
a74454a7 597 /*
598 * Change ownership and permissions of non-device logs...
599 */
ef416fc2 600
a74454a7 601 fchown(cupsFileNumber(*lf), RunUser, Group);
602 fchmod(cupsFileNumber(*lf), LogFilePerm);
ef416fc2 603 }
604
605 return (1);
606}
607
608
609/*
bc44d920 610 * End of "$Id: log.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 611 */