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
|
/*
$Id: log.c,v 1.9 2002/02/10 19:31:54 matt Exp $
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, "%s", 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, "%s", 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);
}
|