113661Ssam #ifndef lint 2*17835Sralph static char sccsid[] = "@(#)logent.c 5.3 (Berkeley) 01/22/85"; 313661Ssam #endif 413661Ssam 513661Ssam #include "uucp.h" 613661Ssam #include <sys/types.h> 7*17835Sralph #ifdef BSD4_2 813706Ssam #include <sys/time.h> 9*17835Sralph #else 10*17835Sralph #include <time.h> 11*17835Sralph #endif 12*17835Sralph #if defined(USG) || defined(BSD4_2) 13*17835Sralph #include <fcntl.h> 14*17835Sralph #endif 1513661Ssam 1613661Ssam extern time_t time(); 1713661Ssam 1813661Ssam /* This logfile stuff was awful -- it did output to an 1913661Ssam * unbuffered stream. 2013661Ssam * 2113661Ssam * This new version just open the single logfile and writes 2213661Ssam * the record in the stdio buffer. Once that's done, it 2313661Ssam * positions itself at the end of the file (lseek), and 2413661Ssam * writes the buffer out. This could mangle things but 2513661Ssam * it isn't likely. -- ittvax!swatt 2613661Ssam * 27*17835Sralph * Under USG UNIX & 4.2BSD, the files are opened with "guaranteed append to end" 28*17835Sralph * and the lseeks are removed. 2913661Ssam */ 3013661Ssam 3113661Ssam 32*17835Sralph static FILE *Lp = NULL; 33*17835Sralph static FILE *Sp = NULL; 3413661Ssam static Ltried = 0; 3513661Ssam static Stried = 0; 3613661Ssam 3713661Ssam /******* 3813661Ssam * logent(text, status) make log entry 3913661Ssam * char *text, *status; 4013661Ssam * 4113661Ssam * return code - none 4213661Ssam */ 4313661Ssam 4413661Ssam logent(text, status) 4513661Ssam char *text, *status; 4613661Ssam { 4713661Ssam /* Open the log file if necessary */ 4813661Ssam if (Lp == NULL) { 4913661Ssam if (!Ltried) { 5013661Ssam int savemask; 51*17835Sralph #if defined(USG) || defined(BSD4_2) 52*17835Sralph int flags; 53*17835Sralph #endif 5413661Ssam savemask = umask(LOGMASK); 5513661Ssam Lp = fopen (LOGFILE, "a"); 5613661Ssam umask(savemask); 57*17835Sralph #if defined(USG) || defined(BSD4_2) 58*17835Sralph flags = fcntl(fileno(Lp), F_GETFL, 0); 59*17835Sralph fcntl(fileno(Lp), F_SETFL, flags|O_APPEND); 60*17835Sralph #endif 6113661Ssam } 6213661Ssam Ltried = 1; 6313661Ssam if (Lp == NULL) 6413661Ssam return; 6513661Ssam fioclex(fileno(Lp)); 6613661Ssam } 6713661Ssam 6813661Ssam /* make entry in existing temp log file */ 6913661Ssam mlogent(Lp, status, text); 7013661Ssam } 7113661Ssam 7213661Ssam /*** 7313661Ssam * mlogent(fp, status, text) - make a log entry 7413661Ssam */ 7513661Ssam 7613661Ssam mlogent(fp, status, text) 7713661Ssam char *text, *status; 7813661Ssam register FILE *fp; 7913661Ssam { 8013661Ssam static pid = 0; 8113661Ssam register struct tm *tp; 8213661Ssam extern struct tm *localtime(); 8313661Ssam time_t clock; 8413661Ssam 85*17835Sralph if (text == NULL) 86*17835Sralph text = ""; 87*17835Sralph if (status == NULL) 88*17835Sralph status = ""; 8913661Ssam if (!pid) 9013661Ssam pid = getpid(); 91*17835Sralph if (Rmtname[0] == '\0') 92*17835Sralph strcpy(Rmtname, Myname); 9313661Ssam time(&clock); 9413661Ssam tp = localtime(&clock); 9513661Ssam fprintf(fp, "%s %s ", User, Rmtname); 96*17835Sralph #ifdef USG 97*17835Sralph fprintf(fp, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1, 9813661Ssam tp->tm_mday, tp->tm_hour, tp->tm_min, pid); 99*17835Sralph #endif 100*17835Sralph #ifndef USG 101*17835Sralph fprintf(fp, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1, 102*17835Sralph tp->tm_mday, tp->tm_hour, tp->tm_min, pid); 103*17835Sralph #endif 10413661Ssam fprintf(fp, "%s (%s)\n", status, text); 10513661Ssam 10613661Ssam /* Since it's buffered */ 107*17835Sralph #ifndef USG 10813661Ssam lseek (fileno(fp), (long)0, 2); 109*17835Sralph #endif 11013661Ssam fflush (fp); 111*17835Sralph if (Debug) { 11213661Ssam fprintf(stderr, "%s %s ", User, Rmtname); 113*17835Sralph #ifdef USG 114*17835Sralph fprintf(stderr, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1, 11513661Ssam tp->tm_mday, tp->tm_hour, tp->tm_min, pid); 116*17835Sralph #endif 117*17835Sralph #ifndef USG 118*17835Sralph fprintf(stderr, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1, 119*17835Sralph tp->tm_mday, tp->tm_hour, tp->tm_min, pid); 120*17835Sralph #endif 12113661Ssam fprintf(stderr, "%s (%s)\n", status, text); 12213661Ssam } 12313661Ssam } 12413661Ssam 12513661Ssam /*** 12613661Ssam * logcls() close log file 12713661Ssam * 12813661Ssam * return codes: none 12913661Ssam */ 13013661Ssam 13113661Ssam logcls() 13213661Ssam { 13313661Ssam if (Lp != NULL) 13413661Ssam fclose(Lp); 13513661Ssam Lp = NULL; 13613661Ssam Ltried = 0; 13713661Ssam 13813661Ssam if (Sp != NULL) 13913661Ssam fclose (Sp); 14013661Ssam Sp = NULL; 14113661Ssam Stried = 0; 14213661Ssam } 14313661Ssam 14413661Ssam 14513661Ssam /*** 14613661Ssam * syslog(text) make system log entry 14713661Ssam * char *text; 14813661Ssam * 14913661Ssam * return codes - none 15013661Ssam */ 15113661Ssam 15213661Ssam syslog(text) 15313661Ssam char *text; 15413661Ssam { 15513661Ssam register struct tm *tp; 15613661Ssam extern struct tm *localtime(); 15713661Ssam time_t clock; 15813661Ssam 15913661Ssam if (Sp == NULL) { 16013661Ssam if (!Stried) { 16113661Ssam int savemask; 162*17835Sralph #if defined(USG) || defined(BSD4_2) 163*17835Sralph int flags; 164*17835Sralph #endif 16513661Ssam savemask = umask(LOGMASK); 16613661Ssam Sp = fopen(SYSLOG, "a"); 16713661Ssam umask(savemask); 168*17835Sralph #if defined(USG) || defined(BSD4_2) 169*17835Sralph flags = fcntl(fileno(Sp), F_GETFL, 0); 170*17835Sralph fcntl(fileno(Sp), F_SETFL, flags|O_APPEND); 171*17835Sralph #endif 17213661Ssam } 17313661Ssam Stried = 1; 17413661Ssam if (Sp == NULL) 17513661Ssam return; 17613661Ssam fioclex(fileno(Sp)); 17713661Ssam } 178*17835Sralph 17913661Ssam time(&clock); 18013661Ssam tp = localtime(&clock); 18113661Ssam 18213661Ssam fprintf(Sp, "%s %s ", User, Rmtname); 183*17835Sralph #ifdef USG 184*17835Sralph fprintf(Sp, "(%d/%d-%2.2d:%2.2d) ", tp->tm_mon + 1, 18513661Ssam tp->tm_mday, tp->tm_hour, tp->tm_min); 186*17835Sralph #endif 187*17835Sralph #ifndef USG 188*17835Sralph fprintf(Sp, "(%d/%d-%02d:%02d) ", tp->tm_mon + 1, 189*17835Sralph tp->tm_mday, tp->tm_hour, tp->tm_min); 190*17835Sralph #endif 19113661Ssam fprintf(Sp, "(%ld) %s\n", clock, text); 19213661Ssam 19313661Ssam /* Position at end and flush */ 19413661Ssam lseek (fileno(Sp), (long)0, 2); 19513661Ssam fflush (Sp); 19613661Ssam } 19713661Ssam 19813661Ssam /* 19913661Ssam * Arrange to close fd on exec(II). 20013661Ssam * Otherwise unwanted file descriptors are inherited 20113661Ssam * by other programs. And that may be a security hole. 20213661Ssam */ 203*17835Sralph #ifndef USG 20413661Ssam #include <sgtty.h> 20513661Ssam #endif 20613661Ssam 20713661Ssam fioclex(fd) 20813661Ssam int fd; 20913661Ssam { 21013661Ssam register int ret; 21113661Ssam 212*17835Sralph #if defined(USG) || defined(BSD4_2) 21313661Ssam ret = fcntl(fd, F_SETFD, 1); /* Steve Bellovin says this does it */ 214*17835Sralph #else 21513661Ssam ret = ioctl(fd, FIOCLEX, STBNULL); 21613661Ssam #endif 21713661Ssam if (ret) 21813661Ssam DEBUG(2, "CAN'T FIOCLEX %d\n", fd); 21913661Ssam } 220