File: com-syslog.c

package info (click to toggle)
proxy-suite 1.9.2.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,420 kB
  • ctags: 436
  • sloc: ansic: 6,530; sh: 2,955; makefile: 341
file content (697 lines) | stat: -rw-r--r-- 15,617 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
 * $Id: com-syslog.c,v 1.6.2.1 2003/05/07 11:14:34 mt Exp $
 *
 * Common file/syslog logging functions
 *
 * Author(s): Jens-Gero Boehm <jens-gero.boehm@suse.de>
 *            Pieter Hollants <pieter.hollants@suse.de>
 *            Marius Tomaschewski <mt@suse.de>
 *            Volker Wiegand <volker.wiegand@suse.de>
 *
 * This file is part of the SuSE Proxy Suite
 *            See also  http://proxy-suite.suse.de/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * A history log can be found at the end of this file.
 */

#ifndef lint
static char rcsid[] = "$Id: com-syslog.c,v 1.6.2.1 2003/05/07 11:14:34 mt Exp $";
#endif

#include <config.h>

#if defined(STDC_HEADERS)
#  include <stdio.h>
#  include <string.h>
#  include <stdlib.h>
#  include <stdarg.h>
#  include <errno.h>
#endif

#if defined(HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#if TIME_WITH_SYS_TIME
#  include <sys/time.h>
#  include <time.h>
#else
#  if HAVE_SYS_TIME_H
#    include <sys/time.h>
#  else
#    include <time.h>
#  endif
#endif

#if defined(HAVE_FCNTL_H)
#  include <fcntl.h>
#elif defined(HAVE_SYS_FCNTL_H)
#  include <sys/fcntl.h>
#endif

#if defined(HAVE_SYSLOG_H)
#  include <syslog.h>
#  if defined(NEED_SYS_SYSLOG_H)
#    include <sys/syslog.h>
#  endif
#endif

#include <sys/types.h>
#include <sys/stat.h>

#include "com-debug.h"
#include "com-misc.h"
#include "com-syslog.h"

/*
** default log level is LOG_INFO (verbose)
*/
#if	!defined(DEFAULT_LOG_LEVEL)
#define		DEFAULT_LOG_LEVEL	LOG_INFO
#endif


/* ------------------------------------------------------------ */

typedef struct {
	char *name;		/* Syslog facility name		*/
	int   code;		/* The corresponding code	*/
} FACIL;


/* ------------------------------------------------------------ */

static int initflag = 0;	/* Have we been initialized?	*/

static int    log_level  = DEFAULT_LOG_LEVEL;
static char  *log_name   = NULL;
static FILE  *log_file   = NULL;
static FILE  *log_pipe   = NULL;
static FACIL *log_syslog = NULL;

static FACIL facilities[] = {
#ifdef LOG_AUTH
	{ "auth",   LOG_AUTH   },
#endif
#ifdef LOG_CRON
	{ "cron",   LOG_CRON   },
#endif
#ifdef LOG_DAEMON
	{ "daemon", LOG_DAEMON },
#endif
#ifdef LOG_FTP
	{ "ftp",    LOG_FTP    },
#endif
#ifdef LOG_KERN
	{ "kern",   LOG_KERN   },
#endif
#ifdef LOG_LOCAL0
	{ "local0", LOG_LOCAL0 },
#endif
#ifdef LOG_LOCAL1
	{ "local1", LOG_LOCAL1 },
#endif
#ifdef LOG_LOCAL2
	{ "local2", LOG_LOCAL2 },
#endif
#ifdef LOG_LOCAL3
	{ "local3", LOG_LOCAL3 },
#endif
#ifdef LOG_LOCAL4
	{ "local4", LOG_LOCAL4 },
#endif
#ifdef LOG_LOCAL5
	{ "local5", LOG_LOCAL5 },
#endif
#ifdef LOG_LOCAL6
	{ "local6", LOG_LOCAL6 },
#endif
#ifdef LOG_LOCAL7
	{ "local7", LOG_LOCAL7 },
#endif
#ifdef LOG_LPR
	{ "lpr",    LOG_LPR    },
#endif
#ifdef LOG_MAIL
	{ "mail",   LOG_MAIL   },
#endif
#ifdef LOG_NEWS
	{ "news",   LOG_NEWS   },
#endif
#ifdef LOG_USER
	{ "user",   LOG_USER   },
#endif
#ifdef LOG_UUCP
	{ "uucp",   LOG_UUCP   },
#endif
	{ NULL,     0          }
};

/* ------------------------------------------------------------ **
**
**	Function......: syslog_stderr
**
**	Parameters....: (none)
**
**	Return........: (none)
**
**	Purpose.......: Assign log destination to stderr.
**	                This allows to print errors durring the
**	                initialization phase - before the first
**	                syslog_open call is done
**
** ------------------------------------------------------------ */

void syslog_stderr()
{
	syslog_close();
	log_file = stderr;
	log_level = LOG_ERR;
}

/* ------------------------------------------------------------ **
**
**	Function......:	syslog_open
**
**	Parameters....:	name		Logfile or syslog name
**	Parameters....:	level		log level name (optional)
**
**	Return........:	(none)
**
**	Purpose.......: Open a logfile, logpipe or syslog.
**
** ------------------------------------------------------------ */

void syslog_open(char *name, char *level)
{
	int fd;

	if (initflag == 0) {
		if(stderr == log_file) {
			log_file  = NULL;
		}

		atexit(syslog_close);
		initflag = 1;
	}

	if (misc_strequ(name, log_name)) {
		/*
		** log destination hasn't changed
		** rotate, if a log file is used
		*/
		if(log_file) {
			syslog_rotate();
		}
		return;
	}

	if (NULL != log_name && NULL != name && '\0' != name[0]) {
		/*
		** this is a reopen - write a message with
		** the new log destination into the old one
		*/
		syslog_write(T_INF,
			     "reopening log - new destination is '%.*s'",
			     MAX_PATH_SIZE, name);
	}

	syslog_close();
	if (NULL == name || '\0' == name[0]) {
		/*
		** hmm... it was a close...
		*/
		return;
	}

	if(level && *level) {
		if( !strcasecmp("FLT", level)) {
			log_level = LOG_CRIT;
		} else
		if( !strcasecmp("ERR", level)) {
			log_level = LOG_ERR;
		} else
		if( !strcasecmp("WRN", level)) {
			log_level = LOG_WARNING;
		} else
		if( !strcasecmp("INF", level)) {
			log_level = LOG_INFO;
		} else
		if( !strcasecmp("DBG", level)) {
			log_level = LOG_DEBUG;
		} else {
			misc_die(FL, "invalid log level '%.3s'");
		}
	}

	/*
	** So we do have a destination now ...
	*/
	if (*name == '/') {
		char tmp_name[MAX_PATH_SIZE];
		/*
		** Logging to a regular file
		*/
		if(syslog_rename(tmp_name, name, sizeof(tmp_name))<0)
		{
			if (unlink(name) != 0 && errno != ENOENT) {
				misc_die(FL, "can't remove logfile '%.*s'",
						MAX_PATH_SIZE, name);
			}
		}
		if ((fd = open(name, O_RDWR | O_CREAT | O_EXCL,
							0640)) < 0) {
			misc_die(FL, "can't open logfile '%.*s'",
						MAX_PATH_SIZE, name);
		}
		if ((log_file = fdopen(fd, "w")) == NULL) {
			misc_die(FL, "can't open logfile '%.*s'",
						MAX_PATH_SIZE, name);
		}
	} else if (*name == '|') {
		/*
		** Logging to a command pipe
		*/
		for (++name; *name == ' ' || *name == '\t'; name++)
			;
		if ((log_pipe = popen(name, "w")) == NULL) {
			misc_die(FL, "can't open logpipe '%.*s'",
						MAX_PATH_SIZE, name);
		}
	} else {
		/*
		** Must be syslog, go and check the facility
		*/
		for (log_syslog = facilities;
				log_syslog->name; log_syslog++) {
			if (strcmp(name, log_syslog->name) == 0)
				break;
		}
		if (log_syslog->name == NULL) {
			log_syslog = NULL;
			misc_usage("invalid syslog facility '%.64s'",
								name);
		}
		openlog(misc_getprog(),
				LOG_PID | LOG_CONS | LOG_NDELAY,
				log_syslog->code);
		setlogmask(LOG_UPTO(log_level));
	}
	log_name = misc_strdup(FL, name);
}


/* ------------------------------------------------------------ **
**
**	Function......:	syslog_write
**
**	Parameters....:	level		Loglevel (similar to syslog)
**			fmt		Format string for output
**
**	Return........:	(none)
**
**	Purpose.......: Write a message to the current log.
**			Do not call misc_die() to avoid loops.
**
** ------------------------------------------------------------ */

void syslog_write(int level, char *fmt, ...)
{
	int tmperr = errno;		/* Save errno for later	*/
	va_list aptr;
	int loglvl, dbglvl;
	FILE *fp;
	time_t now;
	struct tm *t;
	char *logstr, buf[32], str[MAX_PATH_SIZE * 4];

	va_start(aptr, fmt);
#if defined(HAVE_VSNPRINTF)
	vsnprintf(str, sizeof(str), fmt, aptr);
#else
	vsprintf(str, fmt, aptr);
#endif
	va_end(aptr);

	switch (level) {
		case T_DBG:	loglvl = LOG_DEBUG;
				logstr = "TECH-DBG";
				dbglvl = 3;
				break;
		case T_INF:	loglvl = LOG_INFO;
				logstr = "TECH-INF";
				dbglvl = 2;
				break;
		case T_WRN:	loglvl = LOG_WARNING;
				logstr = "TECH-WRN";
				dbglvl = 1;
				break;
		case T_ERR:	loglvl = LOG_ERR;
				logstr = "TECH-ERR";
				dbglvl = 1;
				break;
		case T_FTL:	loglvl = LOG_CRIT;
				logstr = "TECH-FTL";
				dbglvl = 1;
				break;

		case U_DBG:	loglvl = LOG_DEBUG;
				logstr = "USER-DBG";
				dbglvl = 3;
				break;
		case U_INF:	loglvl = LOG_INFO;
				logstr = "USER-INF";
				dbglvl = 2;
				break;
		case U_WRN:	loglvl = LOG_WARNING;
				logstr = "USER-WRN";
				dbglvl = 1;
				break;
		case U_ERR:	loglvl = LOG_ERR;
				logstr = "USER-ERR";
				dbglvl = 1;
				break;
		case U_FTL:	loglvl = LOG_CRIT;
				logstr = "USER-FTL";
				dbglvl = 1;
				break;

		default:	loglvl = LOG_CRIT;
				logstr = "TECH-FLT";
				dbglvl = 1;
	}

#if defined(COMPILE_DEBUG)
	debug(dbglvl, "%s %s", logstr, str);
#endif

	if (log_level < loglvl) {
		errno = tmperr;		/* Restore errno	*/
		return;
	}

	if (log_syslog) {
		syslog(loglvl, "%s %s", logstr, str);
		errno = tmperr;		/* Restore errno	*/
		return;
	}

	if ((fp = (log_file ? log_file : log_pipe)) == NULL) {
		errno = tmperr;		/* Restore errno	*/
		return;
	}

	time(&now);
	t = localtime(&now);
#if defined(HAVE_SNPRINTF)
	snprintf(buf, sizeof(buf), "%02d/%02d-%02d:%02d:%02d",
			t->tm_mon + 1, t->tm_mday,
			t->tm_hour, t->tm_min, t->tm_sec);
#else
	sprintf(buf, "%02d/%02d-%02d:%02d:%02d",
			t->tm_mon + 1, t->tm_mday,
			t->tm_hour, t->tm_min, t->tm_sec);
#endif
	fprintf(fp, "%s [%d] <%s> %s %s\n", misc_getprog(),
				(int) getpid(), buf, logstr, str);
	fflush(fp);

	errno = tmperr;			/* Restore errno	*/
}


/* ------------------------------------------------------------ **
**
**	Function......:	syslog_error
**
**			fmt		Format string for output
**
**	Return........:	(none)
**
**	Purpose.......: Write a message to the current log.
**			Tag is ERR. Also write a debug info.
**
** ------------------------------------------------------------ */

void syslog_error(char *fmt, ...)
{
	int tmperr = errno;		/* Save errno for later	*/
	va_list aptr;
	FILE *fp;
	time_t now;
	struct tm *t;
	char buf[32], str[MAX_PATH_SIZE * 4];

	va_start(aptr, fmt);
#if defined(HAVE_VSNPRINTF)
	vsnprintf(str, sizeof(str), fmt, aptr);
#else
	vsprintf(str, fmt, aptr);
#endif
	va_end(aptr);

#if defined(COMPILE_DEBUG)
	if (tmperr == 0)
		debug(1, "TECH-ERR %s", str);
	else
		debug(1, "TECH-ERR %s (errno=%d [%.200s])",
				str, tmperr, strerror(tmperr));
#endif

	if (log_level < LOG_ERR) {
		errno = tmperr;		/* Restore errno	*/
		return;
	}

	if (log_syslog) {
		if (tmperr == 0)
			syslog(LOG_ERR, "TECH-ERR %s", str);
		else
			syslog(LOG_ERR, "TECH-ERR %s (errno=%d [%.256s])",
					str, tmperr, strerror(tmperr));
		errno = tmperr;		/* Restore errno	*/
		return;
	}

	if ((fp = (log_file ? log_file : log_pipe)) == NULL) {
		errno = tmperr;		/* Restore errno	*/
		return;
	}

	time(&now);
	t = localtime(&now);
#if defined(HAVE_SNPRINTF)
	snprintf(buf, sizeof(buf), "%02d/%02d-%02d:%02d:%02d",
			t->tm_mon + 1, t->tm_mday,
			t->tm_hour, t->tm_min, t->tm_sec);
#else
	sprintf(buf, "%02d/%02d-%02d:%02d:%02d",
			t->tm_mon + 1, t->tm_mday,
			t->tm_hour, t->tm_min, t->tm_sec);
#endif
	fprintf(fp, "%s [%d] <%s> TECH-ERR %s\n",
			misc_getprog(), (int) getpid(), buf, str);
	fflush(fp);

	errno = tmperr;			/* Restore errno	*/
}


/* ------------------------------------------------------------ **
**
**	Function......:	syslog_rename
**
**	Parameters....:	new_name, log_name, len
**
**	Return........:	-1 on error, 0 on success,
**			1 if log_name does not exists
**
**	Purpose.......: if file specified as log_name exists,
**                      rename it to new_name with a maximal
**                      length given in len.
**
** ------------------------------------------------------------ */

int  syslog_rename(char *new_name, char *log_name, size_t len)
{
	time_t now;
	struct tm *t;
	struct stat st;

	if( initflag == 0)
		return -1;

	if( !(new_name && log_name && len > (strlen(log_name) + 17)))
		return -1;

	time(&now);
	t = localtime(&now);

	memset(new_name, 0, len);
#if defined(HAVE_SNPRINTF)
	snprintf(new_name, len, "%.*s.%d%02d%02d-%02d%02d%02d",
		(int)len-17, log_name, t->tm_year + 1900,
		t->tm_mon + 1, t->tm_mday,
		t->tm_hour, t->tm_min, t->tm_sec);
#else
	sprintf(new_name, "%.*s.%d%02d%02d-%02d%02d%02d",
		(int)len-17, log_name, t->tm_year + 1900,
		t->tm_mon + 1, t->tm_mday,
		t->tm_hour, t->tm_min, t->tm_sec);
#endif

	if( lstat(log_name, &st))
		return 1;

	if( !lstat(new_name, &st)) {
		if( !(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
			return -1;
		}
		unlink(new_name);
	}

	if (rename(log_name, new_name)) {
		return -1;
	}
	return 0;
}

/* ------------------------------------------------------------ **
**
**	Function......:	syslog_rotate
**
**	Parameters....:	(none)
**
**	Return........:	(none)
**
**	Purpose.......: Rotate the log, if logging to a file.
**
** ------------------------------------------------------------ */

void syslog_rotate(void)
{
	char tmp_name[MAX_PATH_SIZE];
	int fd;

	if( initflag == 0)
		return;

	/* Only useful if logging to file */
	if (log_file == NULL)
		return;

#if defined(COMPILE_DEBUG)
	debug(2, "rotating log file '%.*s'",
	         MAX_PATH_SIZE, log_name);
#endif
	syslog_write(T_INF, "rotating log file '%.*s'",
	                    MAX_PATH_SIZE, log_name);

	fclose(log_file);
	log_file = NULL;

	if(syslog_rename(tmp_name, log_name, sizeof(tmp_name))<0) {
		misc_die(FL, "can't rotate logfile '%.*s'",
			MAX_PATH_SIZE, log_name);
	}
	if ((fd = open(log_name, O_RDWR | O_CREAT | O_EXCL,
						0640)) < 0) {
		misc_die(FL, "can't open logfile '%.*s'",
					MAX_PATH_SIZE, log_name);
	}
	if ((log_file = fdopen(fd, "w")) == NULL) {
		misc_die(FL, "can't open logfile '%.*s'",
					MAX_PATH_SIZE, log_name);
	}
}


/* ------------------------------------------------------------ **
**
**	Function......:	syslog_close
**
**	Parameters....:	(none)
**
**	Return........:	(none)
**
**	Purpose.......: Clean up at program exit.
**
** ------------------------------------------------------------ */

void syslog_close(void)
{
	if (log_syslog) {
		closelog();
		log_syslog = NULL;
	}

	if (log_file) {
		if(stderr != log_file)
			fclose(log_file);
		log_file = NULL;
	}

	if (log_pipe) {
		pclose(log_pipe);
		log_pipe = NULL;
	}

	if (log_name) {
		void *tmp = (void *) log_name;
		log_name = NULL;
		misc_free(FL, tmp);
	}

	log_level = DEFAULT_LOG_LEVEL;
}


/* ------------------------------------------------------------
 * $Log: com-syslog.c,v $
 * Revision 1.6.2.1  2003/05/07 11:14:34  mt
 * added check for empty name in log_open
 *
 * Revision 1.6  2002/05/02 13:01:58  mt
 * merged with v1.8.2.2
 *
 * Revision 1.5.2.2  2002/04/04 14:28:53  mt
 * replaced stat with lstat for rename check in log rotation
 *
 * Revision 1.5.2.1  2002/01/28 01:51:21  mt
 * replaced question marks sequences that may be misinterpreted as trigraphs
 *
 * Revision 1.5  2002/01/14 18:30:15  mt
 * implemented syslog_stderr function to redirect log to stderr
 * added LogLevel option handling allowing to set the maximal level
 * added snprintf usage, replaced strcpy/strncpy with misc_strncpy
 *
 * Revision 1.4  2001/11/06 23:04:43  mt
 * applied / merged with transparent proxy patches v8
 * see ftp-proxy/NEWS for more detailed release news
 *
 * Revision 1.3  1999/09/26 13:25:05  wiegand
 * protection of debug/pid/log files against attacks
 *
 * Revision 1.2  1999/09/17 06:32:28  wiegand
 * buffer length and overflow protection review
 *
 * Revision 1.1  1999/09/15 14:05:38  wiegand
 * initial checkin
 *
 * ------------------------------------------------------------ */