From bc876bbd8286abb67cece1153b1e2792f817280b Mon Sep 17 00:00:00 2001 From: Stefan Ritt Date: Thu, 22 Jul 2004 20:23:39 +0000 Subject: [PATCH] Message handling implemented by Recai Oktas SVN revision: 968 --- src/elogd.c | 1057 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 638 insertions(+), 419 deletions(-) diff --git a/src/elogd.c b/src/elogd.c index c25e0935..b9e9bd38 100755 --- a/src/elogd.c +++ b/src/elogd.c @@ -2,10 +2,13 @@ Name: elogd.c Created by: Stefan Ritt - + Contents: Web server program for Electronic Logbook ELOG - + $Log$ + Revision 1.392 2004/07/22 20:23:39 midas + Message handling implemented by Recai Oktas + Revision 1.391 2004/07/15 20:40:33 midas Return error number in retrieve_remote_md5() @@ -283,12 +286,15 @@ Removed debug print - + \********************************************************************/ /* Version of ELOG */ #define VERSION "2.5.3" +/* ELOG identification */ +static const char ELOGID[] = "elogd " VERSION " built " __DATE__ ", " __TIME__; + #include #include #include @@ -355,6 +361,7 @@ typedef int BOOL; #include #include #include +#include #define closesocket(s) close(s) #ifndef O_BINARY @@ -369,6 +376,12 @@ uid_t orig_uid; /* Original effective UID before dropping privil char pidfile[256]; /* Pidfile name */ +static void (*printf_handler) (const char *); /* Handler to printf for logging */ +static void (*fputs_handler) (const char *); /* Handler to fputs for logging */ +static FILE *current_output_stream = NULL; /* Currently used output stream */ + +#define SYSLOG_PRIORITY LOG_NOTICE /* Default priority for syslog facility */ + typedef int INT; #define TELL(fh) lseek(fh, 0, SEEK_CUR) @@ -385,6 +398,7 @@ typedef int INT; #define DEFAULT_DATE_FORMAT "%x" #define SUCCESS 1 +#define FAILURE 0 #define EL_SUCCESS 1 #define EL_FIRST_MSG 2 @@ -663,6 +677,160 @@ char *stristr(const char *str, const char *pattern) return NULL; } +/*----------------------- Message handling -------------------------*/ + +#ifdef OS_UNIX + +/* Safe replacement for vasprintf (adapted code from Samba) */ +int xvasprintf(char **ptr, const char *format, va_list ap) +{ + int n; + va_list save; + +#ifdef va_copy + va_copy(save, ap); +#else +# ifdef __va_copy + __va_copy(save, ap); +# else + save = ap; +# endif +#endif + + n = vasprintf(ptr, format, save); + + if (n == -1 || !*ptr) { + printf("Not enough memory"); + exit(EXIT_FAILURE); + } + + return n; +} + +/* Driver for printf_handler, drop-in replacement for printf */ +void eprintf(const char *format, ...) +{ + va_list ap; + char *msg; + + va_start(ap, format); + xvasprintf(&msg, format, ap); + va_end(ap); + + (*printf_handler) (msg); + + free(msg); +} + +#else /* OS_UNIX */ + +/* Driver for printf_handler, drop-in replacement for printf */ +void eprintf(const char *format, ...) +{ + va_list ap; + char msg[10000]; + + va_start(ap, format); + vsprintf(msg, format, ap); + va_end(ap); + + (*printf_handler) (msg); +} + +#endif + +/* Driver for fputs_handler, drop-in replacement for fputs(buf, fd) */ +void efputs(const char *buf) +{ + (*fputs_handler) (buf); +} + +/* Dump with the newline, drop-in replacement for puts(buf) */ +void eputs(const char *buf) +{ + (*fputs_handler) (buf); + (*fputs_handler) ("\n"); +} + +/* Flush the current output stream */ +void eflush(void) +{ + /* Do this only for non-NULL streams (uninitiated stream or a syslog) */ + if (current_output_stream != NULL) + fflush(current_output_stream); +} + +#ifdef OS_WINNT +HANDLE hEventLog; +#endif + +/* Print MSG to syslog */ +void print_syslog(const char *msg) +{ +#ifdef OS_UNIX + syslog(SYSLOG_PRIORITY, "%s", msg); +#else + ReportEvent(hEventLog, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, &msg, NULL); +#endif +} + +/* Print MSG to stderr */ +void print_stderr(const char *msg) +{ + fprintf(stderr, "%s", msg); +} + +/* Dump BUF to syslog */ +void fputs_syslog(const char *buf) +{ +#ifdef OS_UNIX + syslog(SYSLOG_PRIORITY, "%s", buf); +#else + ReportEvent(hEventLog, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, &buf, NULL); +#endif +} + +/* Dump BUF to stderr */ +void fputs_stderr(const char *buf) +{ + fputs(buf, stderr); +} + +/* Redirect all messages handled with eprintf/efputs + to syslog (Unix) or event log (Windows) */ +void redirect_to_syslog(void) +{ + static int has_inited = 0; + + /* initiate syslog */ + if (!has_inited) { +#ifdef OS_UNIX + setlogmask(LOG_UPTO(SYSLOG_PRIORITY)); + openlog("elogd", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); +#else + hEventLog = RegisterEventSource(NULL, "ELOG"); +#endif + } + has_inited = 1; + + printf_handler = print_syslog; + fputs_handler = fputs_syslog; + + /* tells that the syslog facility is currently used as output */ + current_output_stream = NULL; +} + +/* Redirect all messages handled with eprintf/efputs to stderr */ +void redirect_to_stderr(void) +{ + printf_handler = print_stderr; + fputs_handler = fputs_stderr; + + current_output_stream = stderr; +} + +/*------------------------------------------------------------------*/ + /*---- strlcpy and strlcat to avoid buffer overflow ----------------*/ /* @@ -956,9 +1124,9 @@ void do_crypt(char *s, char *d) } /*------------------------------------------------------------------*\ - + MD5 Checksum Routines - + \*------------------------------------------------------------------*/ typedef struct { @@ -1096,76 +1264,76 @@ void _MD5_transform(unsigned int state[4], unsigned char block[64]) _MD5_decode(x, block, 64); /* round 1 */ - FF(lA, lB, lC, lD, x[0], 7, 0xd76aa478); // 1 - FF(lD, lA, lB, lC, x[1], 12, 0xe8c7b756); // 2 - FF(lC, lD, lA, lB, x[2], 17, 0x242070db); // 3 - FF(lB, lC, lD, lA, x[3], 22, 0xc1bdceee); // 4 - FF(lA, lB, lC, lD, x[4], 7, 0xf57c0faf); // 5 - FF(lD, lA, lB, lC, x[5], 12, 0x4787c62a); // 6 - FF(lC, lD, lA, lB, x[6], 17, 0xa8304613); // 7 - FF(lB, lC, lD, lA, x[7], 22, 0xfd469501); // 8 - FF(lA, lB, lC, lD, x[8], 7, 0x698098d8); // 9 - FF(lD, lA, lB, lC, x[9], 12, 0x8b44f7af); // 10 - FF(lC, lD, lA, lB, x[10], 17, 0xffff5bb1); // 11 - FF(lB, lC, lD, lA, x[11], 22, 0x895cd7be); // 12 - FF(lA, lB, lC, lD, x[12], 7, 0x6b901122); // 13 - FF(lD, lA, lB, lC, x[13], 12, 0xfd987193); // 14 - FF(lC, lD, lA, lB, x[14], 17, 0xa679438e); // 15 - FF(lB, lC, lD, lA, x[15], 22, 0x49b40821); // 16 + FF(lA, lB, lC, lD, x[0], 7, 0xd76aa478); // 1 + FF(lD, lA, lB, lC, x[1], 12, 0xe8c7b756); // 2 + FF(lC, lD, lA, lB, x[2], 17, 0x242070db); // 3 + FF(lB, lC, lD, lA, x[3], 22, 0xc1bdceee); // 4 + FF(lA, lB, lC, lD, x[4], 7, 0xf57c0faf); // 5 + FF(lD, lA, lB, lC, x[5], 12, 0x4787c62a); // 6 + FF(lC, lD, lA, lB, x[6], 17, 0xa8304613); // 7 + FF(lB, lC, lD, lA, x[7], 22, 0xfd469501); // 8 + FF(lA, lB, lC, lD, x[8], 7, 0x698098d8); // 9 + FF(lD, lA, lB, lC, x[9], 12, 0x8b44f7af); // 10 + FF(lC, lD, lA, lB, x[10], 17, 0xffff5bb1); // 11 + FF(lB, lC, lD, lA, x[11], 22, 0x895cd7be); // 12 + FF(lA, lB, lC, lD, x[12], 7, 0x6b901122); // 13 + FF(lD, lA, lB, lC, x[13], 12, 0xfd987193); // 14 + FF(lC, lD, lA, lB, x[14], 17, 0xa679438e); // 15 + FF(lB, lC, lD, lA, x[15], 22, 0x49b40821); // 16 /* round 2 */ - GG(lA, lB, lC, lD, x[1], 5, 0xf61e2562); // 17 - GG(lD, lA, lB, lC, x[6], 9, 0xc040b340); // 18 - GG(lC, lD, lA, lB, x[11], 14, 0x265e5a51); // 19 - GG(lB, lC, lD, lA, x[0], 20, 0xe9b6c7aa); // 20 - GG(lA, lB, lC, lD, x[5], 5, 0xd62f105d); // 21 - GG(lD, lA, lB, lC, x[10], 9, 0x2441453); // 22 - GG(lC, lD, lA, lB, x[15], 14, 0xd8a1e681); // 23 - GG(lB, lC, lD, lA, x[4], 20, 0xe7d3fbc8); // 24 - GG(lA, lB, lC, lD, x[9], 5, 0x21e1cde6); // 25 - GG(lD, lA, lB, lC, x[14], 9, 0xc33707d6); // 26 - GG(lC, lD, lA, lB, x[3], 14, 0xf4d50d87); // 27 - GG(lB, lC, lD, lA, x[8], 20, 0x455a14ed); // 28 - GG(lA, lB, lC, lD, x[13], 5, 0xa9e3e905); // 29 - GG(lD, lA, lB, lC, x[2], 9, 0xfcefa3f8); // 30 - GG(lC, lD, lA, lB, x[7], 14, 0x676f02d9); // 31 - GG(lB, lC, lD, lA, x[12], 20, 0x8d2a4c8a); // 32 + GG(lA, lB, lC, lD, x[1], 5, 0xf61e2562); // 17 + GG(lD, lA, lB, lC, x[6], 9, 0xc040b340); // 18 + GG(lC, lD, lA, lB, x[11], 14, 0x265e5a51); // 19 + GG(lB, lC, lD, lA, x[0], 20, 0xe9b6c7aa); // 20 + GG(lA, lB, lC, lD, x[5], 5, 0xd62f105d); // 21 + GG(lD, lA, lB, lC, x[10], 9, 0x2441453); // 22 + GG(lC, lD, lA, lB, x[15], 14, 0xd8a1e681); // 23 + GG(lB, lC, lD, lA, x[4], 20, 0xe7d3fbc8); // 24 + GG(lA, lB, lC, lD, x[9], 5, 0x21e1cde6); // 25 + GG(lD, lA, lB, lC, x[14], 9, 0xc33707d6); // 26 + GG(lC, lD, lA, lB, x[3], 14, 0xf4d50d87); // 27 + GG(lB, lC, lD, lA, x[8], 20, 0x455a14ed); // 28 + GG(lA, lB, lC, lD, x[13], 5, 0xa9e3e905); // 29 + GG(lD, lA, lB, lC, x[2], 9, 0xfcefa3f8); // 30 + GG(lC, lD, lA, lB, x[7], 14, 0x676f02d9); // 31 + GG(lB, lC, lD, lA, x[12], 20, 0x8d2a4c8a); // 32 /* round 3 */ - HH(lA, lB, lC, lD, x[5], 4, 0xfffa3942); // 33 - HH(lD, lA, lB, lC, x[8], 11, 0x8771f681); // 34 - HH(lC, lD, lA, lB, x[11], 16, 0x6d9d6122); // 35 - HH(lB, lC, lD, lA, x[14], 23, 0xfde5380c); // 36 - HH(lA, lB, lC, lD, x[1], 4, 0xa4beea44); // 37 - HH(lD, lA, lB, lC, x[4], 11, 0x4bdecfa9); // 38 - HH(lC, lD, lA, lB, x[7], 16, 0xf6bb4b60); // 39 - HH(lB, lC, lD, lA, x[10], 23, 0xbebfbc70); // 40 - HH(lA, lB, lC, lD, x[13], 4, 0x289b7ec6); // 41 - HH(lD, lA, lB, lC, x[0], 11, 0xeaa127fa); // 42 - HH(lC, lD, lA, lB, x[3], 16, 0xd4ef3085); // 43 - HH(lB, lC, lD, lA, x[6], 23, 0x4881d05); // 44 - HH(lA, lB, lC, lD, x[9], 4, 0xd9d4d039); // 45 - HH(lD, lA, lB, lC, x[12], 11, 0xe6db99e5); // 46 - HH(lC, lD, lA, lB, x[15], 16, 0x1fa27cf8); // 47 - HH(lB, lC, lD, lA, x[2], 23, 0xc4ac5665); // 48 + HH(lA, lB, lC, lD, x[5], 4, 0xfffa3942); // 33 + HH(lD, lA, lB, lC, x[8], 11, 0x8771f681); // 34 + HH(lC, lD, lA, lB, x[11], 16, 0x6d9d6122); // 35 + HH(lB, lC, lD, lA, x[14], 23, 0xfde5380c); // 36 + HH(lA, lB, lC, lD, x[1], 4, 0xa4beea44); // 37 + HH(lD, lA, lB, lC, x[4], 11, 0x4bdecfa9); // 38 + HH(lC, lD, lA, lB, x[7], 16, 0xf6bb4b60); // 39 + HH(lB, lC, lD, lA, x[10], 23, 0xbebfbc70); // 40 + HH(lA, lB, lC, lD, x[13], 4, 0x289b7ec6); // 41 + HH(lD, lA, lB, lC, x[0], 11, 0xeaa127fa); // 42 + HH(lC, lD, lA, lB, x[3], 16, 0xd4ef3085); // 43 + HH(lB, lC, lD, lA, x[6], 23, 0x4881d05); // 44 + HH(lA, lB, lC, lD, x[9], 4, 0xd9d4d039); // 45 + HH(lD, lA, lB, lC, x[12], 11, 0xe6db99e5); // 46 + HH(lC, lD, lA, lB, x[15], 16, 0x1fa27cf8); // 47 + HH(lB, lC, lD, lA, x[2], 23, 0xc4ac5665); // 48 /* round 4 */ - II(lA, lB, lC, lD, x[0], 6, 0xf4292244); // 49 - II(lD, lA, lB, lC, x[7], 10, 0x432aff97); // 50 - II(lC, lD, lA, lB, x[14], 15, 0xab9423a7); // 51 - II(lB, lC, lD, lA, x[5], 21, 0xfc93a039); // 52 - II(lA, lB, lC, lD, x[12], 6, 0x655b59c3); // 53 - II(lD, lA, lB, lC, x[3], 10, 0x8f0ccc92); // 54 - II(lC, lD, lA, lB, x[10], 15, 0xffeff47d); // 55 - II(lB, lC, lD, lA, x[1], 21, 0x85845dd1); // 56 - II(lA, lB, lC, lD, x[8], 6, 0x6fa87e4f); // 57 - II(lD, lA, lB, lC, x[15], 10, 0xfe2ce6e0); // 58 - II(lC, lD, lA, lB, x[6], 15, 0xa3014314); // 59 - II(lB, lC, lD, lA, x[13], 21, 0x4e0811a1); // 60 - II(lA, lB, lC, lD, x[4], 6, 0xf7537e82); // 61 - II(lD, lA, lB, lC, x[11], 10, 0xbd3af235); // 62 - II(lC, lD, lA, lB, x[2], 15, 0x2ad7d2bb); // 63 - II(lB, lC, lD, lA, x[9], 21, 0xeb86d391); // 64 + II(lA, lB, lC, lD, x[0], 6, 0xf4292244); // 49 + II(lD, lA, lB, lC, x[7], 10, 0x432aff97); // 50 + II(lC, lD, lA, lB, x[14], 15, 0xab9423a7); // 51 + II(lB, lC, lD, lA, x[5], 21, 0xfc93a039); // 52 + II(lA, lB, lC, lD, x[12], 6, 0x655b59c3); // 53 + II(lD, lA, lB, lC, x[3], 10, 0x8f0ccc92); // 54 + II(lC, lD, lA, lB, x[10], 15, 0xffeff47d); // 55 + II(lB, lC, lD, lA, x[1], 21, 0x85845dd1); // 56 + II(lA, lB, lC, lD, x[8], 6, 0x6fa87e4f); // 57 + II(lD, lA, lB, lC, x[15], 10, 0xfe2ce6e0); // 58 + II(lC, lD, lA, lB, x[6], 15, 0xa3014314); // 59 + II(lB, lC, lD, lA, x[13], 21, 0x4e0811a1); // 60 + II(lA, lB, lC, lD, x[4], 6, 0xf7537e82); // 61 + II(lD, lA, lB, lC, x[11], 10, 0xbd3af235); // 62 + II(lC, lD, lA, lB, x[2], 15, 0x2ad7d2bb); // 63 + II(lB, lC, lD, lA, x[9], 21, 0xeb86d391); // 64 state[0] += lA; state[1] += lB; @@ -1178,7 +1346,7 @@ void _MD5_transform(unsigned int state[4], unsigned char block[64]) /*------------------------------------------------------------------*/ -/* encodes input (unsigned int) into output (unsigned char), +/* encodes input (unsigned int) into output (unsigned char), assumes that lLen is a multiple of 4 */ void _MD5_encode(unsigned char *pout, unsigned int *pin, unsigned int len) { @@ -1252,10 +1420,10 @@ int setgroup(char *str) if (setegid(gr->gr_gid) >= 0 && initgroups(gr->gr_name, gr->gr_gid) >= 0) return 0; else { - printf("Cannot set effective GID to group \"%s\"\n", gr->gr_name); - perror("setgroup"); + eprintf("Cannot set effective GID to group \"%s\"\n", gr->gr_name); + eprintf("setgroup: %s\n", strerror(errno)); } else - printf("Group \"%s\" not found\n", str); + eprintf("Group \"%s\" not found\n", str); return -1; #else @@ -1275,10 +1443,10 @@ int setuser(char *str) if (seteuid(pw->pw_uid) >= 0) return 0; else { - printf("Cannot set effective UID to user \"%s\"\n", str); - perror("setuser"); + eprintf("Cannot set effective UID to user \"%s\"\n", str); + eprintf("setuser: %s\n", strerror(errno)); } else - printf("User \"%s\" not found\n", str); + eprintf("User \"%s\" not found\n", str); return -1; #else @@ -1342,7 +1510,7 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, char list[1024][NAME_LENGTH], buffer[256]; if (verbose) - printf("\n\nEmail from %s to %s, SMTP host %s:\n", from, to, smtp_host); + eprintf("\n\nEmail from %s to %s, SMTP host %s:\n", from, to, smtp_host); logf(lbs, "Email from %s to %s, SMTP host %s:\n", from, to, smtp_host); /* count attachments */ @@ -1375,7 +1543,7 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, recv_string(s, str, strsize, 10000); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); /* drain server messages */ @@ -1383,28 +1551,28 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, str[0] = 0; recv_string(s, str, strsize, 300); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); } while (str[0]); snprintf(str, strsize - 1, "HELO %s\r\n", host_name); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); recv_string(s, str, strsize, 3000); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); snprintf(str, strsize - 1, "MAIL FROM: <%s>\r\n", from); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); recv_string(s, str, strsize, 3000); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); /* break recipients into list */ @@ -1414,24 +1582,24 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, snprintf(str, strsize - 1, "RCPT TO: <%s>\r\n", list[i]); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); /* increased timeout for SMTP servers with long alias lists */ recv_string(s, str, strsize, 30000); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); } snprintf(str, strsize - 1, "DATA\r\n"); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); recv_string(s, str, strsize, 3000); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); if (email_to) @@ -1441,33 +1609,33 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); snprintf(str, strsize - 1, "From: %s\r\nSubject: %s\r\n", from, subject); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); snprintf(str, strsize - 1, "X-Mailer: Elog, Version %s\r\n", VERSION); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); if (url) { snprintf(str, strsize - 1, "X-Elog-URL: %s\r\n", url); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); } snprintf(str, strsize - 1, "X-Elog-submit-type: web|elog\r\n"); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); time(&now); @@ -1480,14 +1648,14 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, (int) ((abs((int) offset) / 60) % 60)); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); if (n_att > 0) { snprintf(str, strsize - 1, "MIME-Version: 1.0\r\n"); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); sprintf(boundary, "%04X-%04X=:%04X", rand(), rand(), rand()); @@ -1495,34 +1663,34 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, boundary); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); snprintf(str, strsize - 1, " This message is in MIME format. The first part should be readable text,\r\n"); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); snprintf(str, strsize - 1, " while the remaining parts are likely unreadable without MIME-aware tools.\r\n\r\n"); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); snprintf(str, strsize - 1, "--%s\r\nContent-Type: TEXT/PLAIN; charset=US-ASCII\r\n\r\n", boundary); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); } else { snprintf(str, strsize - 1, "Content-Type: TEXT/PLAIN; charset=US-ASCII\r\n\r\n"); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); } @@ -1539,13 +1707,13 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, strlcat(str, "\r\n\r\n", strsize); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); if (n_att > 0) { snprintf(str, strsize - 1, "--%s\r\n", boundary); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); for (index = 0; index < n_att; index++) { @@ -1571,13 +1739,13 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); snprintf(str, strsize - 1, "Content-Transfer-Encoding: BASE64\r\n"); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); snprintf(str, strsize - 1, @@ -1585,7 +1753,7 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, att_file[index] + 14); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); /* encode file */ @@ -1603,7 +1771,7 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, strcat(str, "\r\n"); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); } while (1); close(fh); @@ -1617,7 +1785,7 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); } } @@ -1626,22 +1794,22 @@ INT sendmail(LOGBOOK * lbs, char *smtp_host, char *from, char *to, snprintf(str, strsize - 1, ".\r\n"); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); recv_string(s, str, strsize, 3000); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); snprintf(str, strsize - 1, "QUIT\r\n"); send(s, str, strlen(str), 0); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); recv_string(s, str, strsize, 3000); if (verbose) - fputs(str, stdout); + efputs(str); logf(lbs, str); closesocket(s); @@ -1800,7 +1968,7 @@ INT ss_daemon_init() int i, fd, pid; if ((pid = fork()) < 0) - return 0; + return FAILURE; else if (pid != 0) exit(EXIT_SUCCESS); /* parent finished */ @@ -1814,12 +1982,12 @@ INT ss_daemon_init() if (fd < 0) fd = open("/dev/null", O_WRONLY, 0); if (fd < 0) { - printf("Can't open /dev/null"); - return 0; + eprintf("Can't open /dev/null"); + return FAILURE; } if (fd != i) { - printf("Did not get file descriptor"); - return 0; + eprintf("Did not get file descriptor"); + return FAILURE; } } @@ -1852,7 +2020,7 @@ void check_config_file(BOOL force) } return; } - + /* force re-read configuration file if changed */ if (stat(config_file, &cfg_stat) == 0) { if (cfgfile_mtime < cfg_stat.st_mtime) { @@ -1864,7 +2032,7 @@ void check_config_file(BOOL force) } } } else { - perror("Cannot stat() config file"); + eprintf("Cannot stat() config file; %s\n", strerror(errno)); } } @@ -2023,7 +2191,7 @@ int getcfg_simple(char *group, char *param, char *value) *pstr-- = 0; if (str[0] == '"' && str[strlen(str) - 1] == '"' && - strchr(str+1, '"') == str+strlen(str)-1) { + strchr(str + 1, '"') == str + strlen(str) - 1) { strcpy(value, str + 1); value[strlen(value) - 1] = 0; } else @@ -2056,9 +2224,9 @@ int getcfg_simple(char *group, char *param, char *value) /*-------------------------------------------------------------------*/ int getcfg(char *group, char *param, char *value) -/* +/* Read parameter from configuration file. - + - if group == [global] and top group exists, read from [global ] @@ -2451,8 +2619,8 @@ char *loc(char *orig) } getcfg("global", "Language", language); - printf("Language error: string \"%s\" not found for language \"%s\"\n", orig, - language); + eprintf("Language error: string \"%s\" not found for language \"%s\"\n", orig, + language); return orig; } @@ -2476,7 +2644,7 @@ char *unloc(char *orig) return orig; } - printf("Language error: string \"%s\" not found in English\n", orig); + eprintf("Language error: string \"%s\" not found in English\n", orig); return orig; } @@ -2536,10 +2704,10 @@ void check_config() /*-------------------------------------------------------------------*/ -void retrieve_email_from(LOGBOOK *lbs, char *ret) +void retrieve_email_from(LOGBOOK * lbs, char *ret) { char str[256]; - + if (isparam("user_email") && *getparam("user_email")) strcpy(str, getparam("user_email")); else if (!getcfg(lbs->name, "Use Email from", str)) @@ -2611,7 +2779,7 @@ void el_enum_attr(char *message, int n, char *attr_name, char *attr_value) p = strchr(p, '\n'); if (!p) { - str[0] = 0; /* not a valid line */ + str[0] = 0; /* not a valid line */ break; } while (*p == '\n' || *p == '\r') @@ -2693,19 +2861,19 @@ INT ss_file_find(char *path, char *pattern, char **plist) /********************************************************************\ Routine: ss_file_find - + Purpose: Return list of files matching 'pattern' from the 'path' location - + Input: char *path Name of a file in file system to check char *pattern pattern string (wildcard allowed) - + Output: char **plist pointer to the file list - + Function value: int Number of files matching request - + \********************************************************************/ { #ifdef OS_UNIX @@ -2799,7 +2967,7 @@ int el_build_index(LOGBOOK * lbs, BOOL rebuild) if (strieq(lb_list[i].data_dir, lbs->data_dir) && &lb_list[i] != lbs) { if (verbose) - printf("\n Same index as logbook %s\n", lb_list[i].name); + eprintf("\n Same index as logbook %s\n", lb_list[i].name); lbs->el_index = lb_list[i].el_index; lbs->n_el_index = lb_list[i].n_el_index; @@ -2835,8 +3003,8 @@ int el_build_index(LOGBOOK * lbs, BOOL rebuild) fh = open(file_name, O_RDWR | O_BINARY, 0644); if (fh < 0) { - sprintf(str, "Cannot open file \"%s\":", file_name); - perror(str); + sprintf(str, "Cannot open file \"%s\"", file_name); + eprintf("%s; %s\n", str, strerror(errno)); return EL_FILE_ERROR; } @@ -2846,7 +3014,7 @@ int el_build_index(LOGBOOK * lbs, BOOL rebuild) if (length > 0) { buffer = malloc(length + 1); if (buffer == NULL) { - printf("Not enough memory to allocate file buffer (%d bytes)\n", length); + eprintf("Not enough memory to allocate file buffer (%d bytes)\n", length); return EL_MEM_ERROR; } @@ -2865,7 +3033,7 @@ int el_build_index(LOGBOOK * lbs, BOOL rebuild) lbs->el_index = realloc(lbs->el_index, sizeof(EL_INDEX) * (*lbs->n_el_index + 1)); if (lbs->el_index == NULL) { - printf("Not enough memory to allocate entry index\n"); + eprintf("Not enough memory to allocate entry index\n"); return EL_MEM_ERROR; } @@ -2892,17 +3060,17 @@ int el_build_index(LOGBOOK * lbs, BOOL rebuild) if (lbs->el_index[*lbs->n_el_index].message_id > 0) { if (verbose) { if (*lbs->n_el_index == 0) - printf("\n"); + eprintf("\n"); - printf(" ID %3d, %s, ofs %5d, %s, MD5=", - lbs->el_index[*lbs->n_el_index].message_id, - str, lbs->el_index[*lbs->n_el_index].offset, - lbs->el_index[*lbs->n_el_index]. - in_reply_to ? "reply" : "thead"); + eprintf(" ID %3d, %s, ofs %5d, %s, MD5=", + lbs->el_index[*lbs->n_el_index].message_id, + str, lbs->el_index[*lbs->n_el_index].offset, + lbs->el_index[*lbs->n_el_index]. + in_reply_to ? "reply" : "thead"); for (i = 0; i < 16; i++) - printf("%02X", lbs->el_index[*lbs->n_el_index].md5_digest[i]); - printf("\n"); + eprintf("%02X", lbs->el_index[*lbs->n_el_index].md5_digest[i]); + eprintf("\n"); } /* valid ID */ @@ -2925,10 +3093,10 @@ int el_build_index(LOGBOOK * lbs, BOOL rebuild) qsort(lbs->el_index, *lbs->n_el_index, sizeof(EL_INDEX), eli_compare); if (verbose) { - printf("After sort:\n"); + eprintf("After sort:\n"); for (i = 0; i < *lbs->n_el_index; i++) - printf(" ID %3d, %s, ofs %5d\n", lbs->el_index[i].message_id, - lbs->el_index[i].file_name, lbs->el_index[i].offset); + eprintf(" ID %3d, %s, ofs %5d\n", lbs->el_index[i].message_id, + lbs->el_index[i].file_name, lbs->el_index[i].offset); } return EL_SUCCESS; @@ -2978,9 +3146,9 @@ int el_index_logbooks() continue; /* check for duplicate name */ - for (j = 0; j < i && lb_list[j].name[0] ; j++) + for (j = 0; j < i && lb_list[j].name[0]; j++) if (strieq(lb_list[j].name, logbook)) { - printf("Error in configuration file: Duplicate logbook \"%s\"\n", logbook); + eprintf("Error in configuration file: Duplicate logbook \"%s\"\n", logbook); return EL_DUPLICATE; } @@ -3057,10 +3225,10 @@ int el_index_logbooks() if (j == 0) { if (verbose) - printf("Created directory \"%s\"\n", str); + eprintf("Created directory \"%s\"\n", str); } else { - perror("el_index_logbooks"); - printf("Cannot create directory \"%s\"\n", str); + eprintf("el_index_logbooks: %s\n", strerror(errno)); + eprintf("Cannot create directory \"%s\"\n", str); } chdir(str); @@ -3074,21 +3242,22 @@ int el_index_logbooks() lb_list[n].el_index = NULL; if (verbose) - printf("Indexing logbook \"%s\" ... ", logbook); - fflush(stdout); + eprintf("Indexing logbook \"%s\" ... ", logbook); + eflush(); status = el_build_index(&lb_list[n], FALSE); if (verbose) - printf("ok\n"); + if (status == EL_SUCCESS) + eprintf("ok\n"); if (status == EL_EMPTY) { if (verbose) - printf("Found empty logbook \"%s\"\n", logbook); + eprintf("Found empty logbook \"%s\"\n", logbook); } else if (status == EL_UPGRADE) { - printf("Please upgrade data files in \"%s\" with the elconv program.\n", - data_dir); + eprintf("Please upgrade data files in \"%s\" with the elconv program.\n", + data_dir); return EL_UPGRADE; } else if (status != EL_SUCCESS) { - printf("Error generating index.\n"); + eprintf("Error generating index.\n"); return status; } @@ -3120,16 +3289,16 @@ int el_search_message(LOGBOOK * lbs, int mode, int message_id, BOOL head_only) /********************************************************************\ Routine: el_search_message - + Purpose: Search for a specific message in a logbook - + Input: int mode Search mode, EL_FIRST, EL_LAST, EL_NEXT, EL_PREV int message_id Message id for EL_NEXT and EL_PREV - + Function value: int New message id - + \********************************************************************/ { int i; @@ -3562,7 +3731,7 @@ int el_submit(LOGBOOK * lbs, int message_id, BOOL bedit, Input: LOGBOOK lbs Logbook structure - int message_id Message id + int message_id Message id BOOL bedit TRUE for existing message, FALSE for new message char *date Message date char attr_name[][] Name of attributes @@ -4466,7 +4635,8 @@ void rsputs2(const char *str) if (strncmp(str + i, list[l], strlen(list[l])) == 0) { p = (char *) (str + i + strlen(list[l])); i += strlen(list[l]); - for (k = 0; *p && strcspn(p, " ,;\t\n\r({[)}]") && k<(int)sizeof(link); k++, i++) + for (k = 0; *p && strcspn(p, " ,;\t\n\r({[)}]") && k < (int) sizeof(link); + k++, i++) link[k] = *p++; link[k] = 0; i--; @@ -4809,7 +4979,7 @@ void extract_host(char *str) p++; *p = 0; - strcpy(str2, str+7); + strcpy(str2, str + 7); strcpy(str, str2); } } @@ -5470,7 +5640,7 @@ int is_logbook_in_group(LBLIST pgrp, char *logbook) /*------------------------------------------------------------------*/ -void change_logbook_in_group(LOGBOOK *lbs, char *new_name) +void change_logbook_in_group(LOGBOOK * lbs, char *new_name) { int i, j, n, flag; char str[1000], grpname[256], grpmembers[1000]; @@ -5503,10 +5673,10 @@ void change_logbook_in_group(LOGBOOK *lbs, char *new_name) } } } - + /*------------------------------------------------------------------*/ -void add_logbook_to_group(LOGBOOK *lbs, char *new_name) +void add_logbook_to_group(LOGBOOK * lbs, char *new_name) { int i, j, n, flag; char str[1000], grpname[256], grpmembers[1000]; @@ -5539,7 +5709,7 @@ void add_logbook_to_group(LOGBOOK *lbs, char *new_name) } } } - + /*------------------------------------------------------------------*/ void show_standard_title(char *logbook, char *text, int printable) @@ -5564,7 +5734,7 @@ void show_standard_title(char *logbook, char *text, int printable) pnext = NULL; if (!printable && (!getcfg(logbook, "logbook tabs", str) - || atoi(str) == 1)) { + || atoi(str) == 1)) { for (level = 0;; level++) { rsprintf("\n"); @@ -6039,7 +6209,7 @@ int build_subst_list(LOGBOOK * lbs, char list[][NAME_LENGTH], char value[][NAME_ if (attrib) { if ((attr_flags[i] & AF_DATE) && format_date) { - t = (time_t)atoi(attrib[i]); + t = (time_t) atoi(attrib[i]); ts = localtime(&t); if (!getcfg(lbs->name, "Date format", format)) strcpy(format, DEFAULT_DATE_FORMAT); @@ -6243,9 +6413,10 @@ void show_change_pwd_page(LOGBOOK * lbs) if (old_pwd[0] || new_pwd[0]) { if (user[0] && get_user_line(lbs->name, user, act_pwd, NULL, NULL, NULL)) { - - /* administrator does not have to supply old password if changing other user's password*/ - if (is_admin_user(lbs->name, getparam("unm")) && stricmp(getparam("unm"), user) != 0) + + /* administrator does not have to supply old password if changing other user's password */ + if (is_admin_user(lbs->name, getparam("unm")) + && stricmp(getparam("unm"), user) != 0) wrong_pwd = 0; else { if (strcmp(old_pwd, act_pwd) != 0) @@ -6892,7 +7063,7 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL rsprintf("\n"); - + /*---- title row ----*/ show_standard_title(lbs->name, "", 0); @@ -6907,13 +7078,16 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL rsprintf ("\n", loc("Submit")); - rsprintf("\n", loc("Back")); + rsprintf + ("\n", + loc("Back")); rsprintf("\n\n"); /*---- entry form ----*/ /* table for two-column items */ - rsprintf(""); + rsprintf + ("
"); /* print required message if one of the attributes has it set */ for (i = 0; i < lbs->n_attr; i++) { @@ -6981,7 +7155,8 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL } if (index == 0 || (format_flags[index] & AFF_SAME_LINE) == 0) - rsprintf("
"); + rsprintf + ("
"); strcpy(star, (attr_flags[index] & AF_REQUIRED) ? "*" : ""); @@ -6989,8 +7164,8 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL sprintf(str, "Preset %s", attr_list[index]); if ((i = getcfg(lbs->name, str, preset)) > 0) { - if ((!bedit && !breply) || /* don't subst on edit or reply */ - (breedit && i == 2)) { /* subst on reedit only if preset is under condition */ + if ((!bedit && !breply) || /* don't subst on edit or reply */ + (breedit && i == 2)) { /* subst on reedit only if preset is under condition */ /* do not format date for date attributes */ i = build_subst_list(lbs, slist, svalue, attrib, @@ -7014,7 +7189,7 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL sprintf(str, "Preset on reply %s", attr_list[index]); if ((i = getcfg(lbs->name, str, preset)) > 0 && breply) { - if (!breedit || (breedit && i == 2)) { /* subst on reedit only if preset is under condition */ + if (!breedit || (breedit && i == 2)) { /* subst on reedit only if preset is under condition */ /* do not format date for date attributes */ i = build_subst_list(lbs, slist, svalue, attrib, @@ -7040,7 +7215,7 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL title[0] = 0; if (getcfg(lbs->name, str, comment)) sprintf(title, " title=\"%s\"", comment); - + rsprintf("", title); /* display attribute name */ @@ -7088,8 +7263,7 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL } } else { strencode2(str, attrib[index]); - rsprintf("\n", - ua, str); + rsprintf("\n", ua, str); } } else { if (attr_options[index][0][0] == 0) { @@ -7176,7 +7350,9 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL if (attr_flags[index] & AF_EXTENDABLE) { sprintf(str, loc("Add %s"), attr_list[index]); - rsprintf("\n", str); + rsprintf + ("\n", + str); } rsprintf("\n"); @@ -7204,7 +7380,9 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL if (attr_flags[index] & AF_EXTENDABLE) { sprintf(str, loc("Add %s"), attr_list[index]); - rsprintf("\n", str); + rsprintf + ("\n", + str); } rsprintf("\n"); @@ -7219,8 +7397,9 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL ("", ua, attr_options[index][i]); else - rsprintf("", - ua, attr_options[index][i]); + rsprintf + ("", + ua, attr_options[index][i]); sprintf(str, "Icon comment %s", attr_options[index][i]); getcfg(lbs->name, str, comment); @@ -7276,7 +7455,9 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL if (attr_flags[index] & AF_EXTENDABLE) { sprintf(str, loc("Add %s"), attr_list[index]); - rsprintf("\n", str); + rsprintf + ("\n", + str); } rsprintf("\n"); @@ -7363,8 +7544,10 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL strcpy(str, " readonly"); else strcpy(str, ""); - - rsprintf("