File: log.c%2Cv

package info (click to toggle)
ssmtp 2.50.6.3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 720 kB
  • ctags: 111
  • sloc: ansic: 1,596; sh: 326; makefile: 116
file content (437 lines) | stat: -rw-r--r-- 6,594 bytes parent folder | download
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
head	1.9;
access;
symbols;
locks; strict;
comment	@ * @;


1.9
date	2002.02.10.19.31.54;	author matt;	state Exp;
branches;
next	1.8;

1.8
date	2002.02.10.18.30.46;	author matt;	state Exp;
branches;
next	1.7;

1.7
date	2001.07.30.11.40.45;	author matt;	state Exp;
branches;
next	1.6;

1.6
date	2001.07.30.11.39.44;	author matt;	state Exp;
branches;
next	1.5;

1.5
date	2001.07.30.11.38.23;	author matt;	state Exp;
branches;
next	1.4;

1.4
date	2001.07.21.12.00.21;	author matt;	state Exp;
branches;
next	1.3;

1.3
date	2001.07.18.17.31.10;	author matt;	state Exp;
branches;
next	1.2;

1.2
date	2001.07.06.17.39.24;	author matt;	state Exp;
branches;
next	1.1;

1.1
date	2001.05.24.12.10.31;	author matt;	state Exp;
branches;
next	;


desc
@Log output to syslog and potentially a log file.
@


1.9
log
@Added cast on NULL check
@
text
@/*

 $Id: log.c,v 1.8 2002/02/10 18:30:46 matt Exp matt $

 See COPYRIGHT for the license

*/
#include <sys/param.h>
#include <unistd.h>
#include <stdarg.h>
#include <syslog.h>
#include <stdio.h>
#include <pwd.h>
#include "ssmtp.h"

extern char *prog_name;
extern int log_level;


void log_event(int priority, char *format, ...)
{
	char buf[(BUF_SZ + 1)];
	va_list ap;

	va_start(ap, format);
	(void)vsnprintf(buf, BUF_SZ, format, ap);
	va_end(ap);

#ifdef LOGFILE
	if((fp = fopen("/tmp/ssmtp.log", "a")) != (FILE *)NULL) {
		(void)fprintf(fp, "%s\n", buf);
		(void)fclose(fp);
	}
	else {
		(void)fprintf(stderr, "Can't write to /tmp/ssmtp.log\n");
	}
#endif

#if HAVE_SYSLOG_H
#if OLDSYSLOG
	openlog("sSMTP", LOG_PID);
#else
	openlog("sSMTP", LOG_PID, LOG_MAIL);
#endif
	syslog(priority, buf);
	closelog();
#endif
}

/*
dead_letter() -- save stdin to ~/dead.letter, if possible
*/
void dead_letter(void)
{
	char path[(MAXPATHLEN + 1)], buf[(BUF_SZ + 1)];
	struct passwd *pw;
	uid_t uid;
	FILE *fp;

	uid = getuid();
	pw = getpwuid(uid);

	if(isatty(fileno(stdin))) {
		if(log_level > 0) {
			log_event(LOG_ERR, "stdin is a TTY - not saving to %s/dead.letter, pw->pw_dir");
		}
		return;
	}

	if(pw == (struct passwd *)NULL) {
		/* Far to early to save things. */
		if(log_level > 0) {
			log_event(LOG_ERR, "No sender (can't happen), failing horribly");
		}
		return;
	}

	if(snprintf(path, BUF_SZ, "%s/dead.letter", pw->pw_dir) == -1) {
		/* can't use die() here since dead_letter() is called from die() */
		exit(1);
	}

	if((fp = fopen(path, "a")) == (FILE *)NULL) {
		/* Perhaps the person doesn't have a homedir... */
		if(log_level > 0) {
			log_event(LOG_ERR, "Can't open %s, failing horribly!", path);
		}
		return;
	}

	/* Make sure we start on a new line, */
	/* with a blank line separating messages. */
	(void)fprintf(fp, "\n\n");

	while(fgets(buf, sizeof(buf), stdin)) {
		(void)fputs(buf, fp);
	}

	if(fclose(fp) == -1) {
		if(log_level > 0) {
			log_event(LOG_ERR, "Can't close %s/dead.letter, possibly truncated", pw->pw_dir);
		}
	}
}

void die(char *format, ...)
{
	char buf[(BUF_SZ + 1)];
	va_list ap;

	va_start(ap, format);
	(void)vsnprintf(buf, BUF_SZ, format, ap);
	va_end(ap);

	(void)fprintf(stderr, "%s: %s\n", prog_name, buf);
	log_event(LOG_ERR, buf);

	/* Send message to dead.letter */
	(void)dead_letter();

	exit(1);
}

void paq(char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	(void)vfprintf(stderr, format, ap);
	va_end(ap);

	exit(0);
}
@


1.8
log
@Renamed variable
@
text
@d3 1
a3 1
 $Id: logging.c,v 1.7 2001/07/30 11:40:45 matt Exp matt $
d30 1
a30 1
	if((fp = fopen("/tmp/ssmtp.log", "a")) != NULL) {
d70 1
a70 1
	if(pw == NULL) {
d83 1
a83 1
	if((fp = fopen(path, "a")) == NULL) {
@


1.7
log
@Header clean-up (again)
@
text
@d3 1
a3 1
 $Id: logging.c,v 1.6 2001/07/30 11:39:44 matt Exp matt $
d16 1
a16 1
extern char *ProgName;
d115 1
a115 1
	(void)fprintf(stderr, "%s: %s\n", ProgName, buf);
@


1.6
log
@Header clean-up
@
text
@d3 3
a5 1
 $Id: logging.c,v 1.5 2001/07/30 11:38:23 matt Exp matt $
@


1.5
log
@Code clean-up
@
text
@d2 3
a4 3
*
* $Id: logging.c,v 1.4 2001/07/21 12:00:21 matt Exp matt $
*
@


1.4
log
@Added new paq (print and quit) function
@
text
@d3 1
a3 1
* $Id: logging.c,v 1.3 2001/07/18 17:31:10 matt Exp matt $
d6 2
d9 1
d11 2
d14 2
a15 8
#if HAVE_SYSLOG_H
#include <syslog.h>
#else
#define LOG_ERR 0
#define LOG_INFO 0
#endif

#include "ssmtp.h"
d18 1
a18 1
void log_event(int syslog_code, char *format, ...)
d20 1
a20 1
	char buffer[(MAXLINE + 1)];
d24 1
a24 1
	(void)vsnprintf(buffer, MAXLINE, format, ap);
d29 2
a30 2
		fprintf(fp, "%s\n", buffer);
		fclose(fp);
d33 1
a33 1
		fprintf(stderr, "Can't write to /tmp/ssmtp.log\n");
d43 1
a43 1
	(void)syslog(syslog_code, buffer);
d48 55
a102 1
extern char *ProgName;
d106 1
a106 1
	char buffer[(MAXLINE + 1)];
d110 1
a110 1
	(void)vsnprintf(buffer, MAXLINE, format, ap);
d113 2
a114 2
	(void)fprintf(stderr, "%s: %s\n", ProgName, buffer);
	log_event(LOG_ERR, buffer);
d117 1
a117 1
	flush();
d127 1
a127 1
	(void)vprintf(format, ap);
a128 3

	/* Send message to dead.letter */
	flush();
@


1.3
log
@Removed malloc() and rely on fixed strings
@
text
@d3 1
a3 1
* $Id: logging.c,v 1.2 2001/07/06 17:39:24 matt Exp $
d67 14
@


1.2
log
@Small changes to syslog message
@
text
@d3 1
a3 1
* $Id: logging.c,v 1.1 2001/05/24 12:10:31 matt Exp matt $
d6 1
a7 3
#include <stdarg.h>
#include <assert.h>
#include <stdlib.h>
d10 1
a10 1
#include <syslog.h>		/* For logging. */
a18 5
extern char *ProgName ; /* Name of the program */

/*
 * log_event -- log something to syslog and the log file.
 */
d21 1
a21 4
#if HAVE_SYSLOG_H
	static int syslogOpen = NO;
#endif
	int msgsize = 1024;
a22 2
	char *buf;
	FILE *fp;
d25 1
a25 13
	if((fp = fopen("/dev/null","w")) != NULL) {
		assert(fp);
		msgsize = vfprintf(fp, format, ap);
		fclose(fp);
	}

	buf = (char *)malloc(((msgsize + 1) * sizeof(char *)));
	if(!buf) {
		perror("Cannot allocate memory");
		exit(1);
	}
	vsprintf(buf, format, ap);

d30 1
a30 2
		fprintf(fp, buf);
		putc('\n', fp);
a38 2
	if(syslogOpen == NO) {
		syslogOpen = YES;
d40 1
a40 1
		openlog("sSMTP", LOG_PID);
d42 1
a42 1
		openlog("sSMTP", LOG_PID, LOG_MAIL);
d44 1
a44 3
	}
	(void)syslog(syslog_code, buf);

a46 1
	free(buf);
d49 2
a50 4
/*
 * die -- say something and exit with a non-zero return code.
 *      Save the message on stdin in dead.letter.
 */
d53 1
d57 1
a57 5
	fprintf(stderr, "%s: ", ProgName);
	vfprintf(stderr, format, ap);
	putc('\n', stderr);
	flush();			/* Send message to dead.letter */
	log_event(LOG_ERR, format, ap);
d59 6
@


1.1
log
@Logging functions for sSMTP
@
text
@d3 1
a3 1
* $Id$
d67 1
a67 1
		openlog("sSMTP mail", LOG_PID);
d69 1
a69 1
		openlog("sSMTP mail", LOG_PID, LOG_MAIL);
@