148652Sbostic /*- 248652Sbostic * Copyright (c) 1985 The Regents of the University of California. 348652Sbostic * All rights reserved. 448652Sbostic * 548652Sbostic * %sccs.include.proprietary.c% 648652Sbostic */ 748652Sbostic 813661Ssam #ifndef lint 9*60115Storek static char sccsid[] = "@(#)logent.c 5.11 (Berkeley) 05/17/93"; 1048652Sbostic #endif /* not lint */ 1113661Ssam 1213661Ssam #include "uucp.h" 1317835Sralph #ifdef BSD4_2 1413706Ssam #include <sys/time.h> 1517835Sralph #else 1617835Sralph #include <time.h> 1717835Sralph #endif 1817835Sralph #if defined(USG) || defined(BSD4_2) 1917835Sralph #include <fcntl.h> 2017835Sralph #endif 2113661Ssam 2234168Srick extern int errno; 2334168Srick extern int sys_nerr; 24*60115Storek extern const char *const sys_errlist[]; 2534168Srick 2617835Sralph static FILE *Lp = NULL; 2717835Sralph static FILE *Sp = NULL; 2834168Srick #ifndef USE_SYSLOG 2934168Srick static FILE *Ep = NULL; 3034168Srick #endif /* !USE_SYSLOG */ 3134168Srick static int pid = 0; 3213661Ssam 3323611Sbloom /*LINTLIBRARY*/ 3423611Sbloom 3518620Sralph /* 3618620Sralph * make log entry 3713661Ssam */ 3834168Srick FILE * 3934168Srick get_logfd(pname, logfilename) 4034168Srick char *pname; 4134168Srick char *logfilename; 4213661Ssam { 4334168Srick FILE *fp; 4434168Srick int savemask; 4518620Sralph #ifdef LOGBYSITE 4618620Sralph char lfile[MAXFULLNAME]; 4718620Sralph #endif LOGBYSITE 4834168Srick 4934168Srick savemask = umask(LOGMASK); 5018620Sralph #ifdef LOGBYSITE 5134168Srick if (pname != NULL) { 5234168Srick (void) sprintf(lfile, "%s/%s/%s", LOGBYSITE, pname, Rmtname); 5334168Srick logfilename = lfile; 5418620Sralph } 5518620Sralph #endif LOGBYSITE 5634168Srick fp = fopen(logfilename, "a"); 5734168Srick umask(savemask); 5834168Srick if (fp) { 5934168Srick #ifdef F_SETFL 6034168Srick int flags; 6134168Srick flags = fcntl(fileno(fp), F_GETFL, 0); 6234168Srick fcntl(fileno(Lp), F_SETFL, flags|O_APPEND); 6334168Srick #endif /* F_SETFL */ 6434168Srick fioclex(fileno(fp)); 6534168Srick } else /* we really want to log this, but it's the logging that failed*/ 6634168Srick perror(logfilename); 6734168Srick return fp; 6813661Ssam } 6913661Ssam 7018620Sralph /* 7118620Sralph * make a log entry 7213661Ssam */ 7313661Ssam mlogent(fp, status, text) 7413661Ssam char *text, *status; 7513661Ssam register FILE *fp; 7613661Ssam { 7713661Ssam register struct tm *tp; 7813661Ssam extern struct tm *localtime(); 7913661Ssam 8017835Sralph if (text == NULL) 8117835Sralph text = ""; 8217835Sralph if (status == NULL) 8317835Sralph status = ""; 8433566Srick if (pid == 0) 8513661Ssam pid = getpid(); 8617835Sralph #ifdef USG 8725134Sbloom time(&Now.time); 8825134Sbloom Now.millitm = 0; 8918620Sralph #else !USG 9025134Sbloom ftime(&Now); 9118620Sralph #endif !USG 9225134Sbloom tp = localtime(&Now.time); 9325134Sbloom #ifdef USG 9425134Sbloom fprintf(fp, "%s %s (%d/%d-%2.2d:%2.2d-%d) ", 9525134Sbloom #else !USG 9625134Sbloom fprintf(fp, "%s %s (%d/%d-%02d:%02d-%d) ", 9725134Sbloom #endif !USG 9825134Sbloom User, Rmtname, tp->tm_mon + 1, tp->tm_mday, 9925134Sbloom tp->tm_hour, tp->tm_min, pid); 10034168Srick fprintf(fp, "%s %s\n", status, text); 10113661Ssam 10213661Ssam /* Since it's buffered */ 10318620Sralph #ifndef F_SETFL 10413661Ssam lseek (fileno(fp), (long)0, 2); 10518620Sralph #endif !F_SETFL 10613661Ssam fflush (fp); 10717835Sralph if (Debug) { 10813661Ssam fprintf(stderr, "%s %s ", User, Rmtname); 10917835Sralph #ifdef USG 11017835Sralph fprintf(stderr, "(%d/%d-%2.2d:%2.2d-%d) ", tp->tm_mon + 1, 11113661Ssam tp->tm_mday, tp->tm_hour, tp->tm_min, pid); 11218620Sralph #else !USG 11317835Sralph fprintf(stderr, "(%d/%d-%02d:%02d-%d) ", tp->tm_mon + 1, 11417835Sralph tp->tm_mday, tp->tm_hour, tp->tm_min, pid); 11518620Sralph #endif !USG 11634168Srick fprintf(stderr, "%s %s\n", status, text); 11713661Ssam } 11813661Ssam } 11913661Ssam 12018620Sralph /* 12118620Sralph * close log file 12213661Ssam */ 12313661Ssam logcls() 12413661Ssam { 12513661Ssam if (Lp != NULL) 12613661Ssam fclose(Lp); 12713661Ssam Lp = NULL; 12813661Ssam 12913661Ssam if (Sp != NULL) 13013661Ssam fclose (Sp); 13113661Ssam Sp = NULL; 13234168Srick #ifndef USE_SYSLOG 13334168Srick if (Ep != NULL) 13434168Srick fclose (Ep); 13534168Srick Ep = NULL; 13634168Srick #endif /* !USE_SYSLOG */ 13713661Ssam } 13813661Ssam 13918620Sralph /* 14013661Ssam * Arrange to close fd on exec(II). 14113661Ssam * Otherwise unwanted file descriptors are inherited 14213661Ssam * by other programs. And that may be a security hole. 14313661Ssam */ 14417835Sralph #ifndef USG 14513661Ssam #include <sgtty.h> 14613661Ssam #endif 14713661Ssam 14813661Ssam fioclex(fd) 14913661Ssam int fd; 15013661Ssam { 15113661Ssam register int ret; 15213661Ssam 15317835Sralph #if defined(USG) || defined(BSD4_2) 15413661Ssam ret = fcntl(fd, F_SETFD, 1); /* Steve Bellovin says this does it */ 15517835Sralph #else 15613661Ssam ret = ioctl(fd, FIOCLEX, STBNULL); 15713661Ssam #endif 15813661Ssam if (ret) 15913661Ssam DEBUG(2, "CAN'T FIOCLEX %d\n", fd); 16013661Ssam } 16134168Srick 16234168Srick logent(text, status) 16334168Srick char *text, *status; 16434168Srick { 16534168Srick if (Lp == NULL) 16634168Srick Lp = get_logfd(Progname, LOGFILE); 16734168Srick 16834168Srick mlogent(Lp, status, text); 16934168Srick } 17034168Srick 17134168Srick /* 17234168Srick * make system log entry 17334168Srick */ 17434168Srick log_xferstats(text) 17534168Srick char *text; 17634168Srick { 17734168Srick char tbuf[BUFSIZ]; 17834168Srick if (Sp == NULL) 17934168Srick Sp = get_logfd("xferstats", SYSLOG); 18034168Srick sprintf(tbuf, "(%ld.%02u)", Now.time, Now.millitm/10); 18134168Srick mlogent(Sp, tbuf, text); 18234168Srick } 18334168Srick 18434168Srick #ifndef USE_SYSLOG 18534168Srick /* 18634168Srick * This is for sites that don't have a decent syslog() in their library 18734168Srick * This routine would be a lot simpler if syslog() didn't permit %m 18834168Srick * (or if printf did!) 18934168Srick */ 19034168Srick syslog(priority, format, p0, p1, p2, p3, p4) 19134168Srick int priority; 19234168Srick char *format; 19334168Srick { 19434168Srick char nformat[BUFSIZ], sbuf[BUFSIZ]; 19534168Srick register char *s, *d; 19634168Srick register int c; 19734168Srick long now; 19834168Srick 19934168Srick s = format; 20034168Srick d = nformat; 20134168Srick while ((c = *s++) != '\0' && c != '\n' && d < &nformat[BUFSIZ]) { 20234168Srick if (c != '%') { 20334168Srick *d++ = c; 20434168Srick continue; 20534168Srick } 20634168Srick if ((c = *s++) != 'm') { 20734168Srick *d++ = '%'; 20834168Srick *d++ = c; 20934168Srick continue; 21034168Srick } 21134168Srick if ((unsigned)errno > sys_nerr) 21234168Srick sprintf(d, "error %d", errno); 21334168Srick else 21434168Srick strcpy(d, sys_errlist[errno]); 21534168Srick d += strlen(d); 21634168Srick } 21734168Srick *d = '\0'; 21834168Srick 21934168Srick if (Ep == NULL) 22034168Srick Ep = get_logfd(NULL, ERRLOG); 22134168Srick sprintf(sbuf, nformat, p0, p1, p2, p3, p4); 22234168Srick mlogent(Ep, sbuf, ""); 22334168Srick } 22434168Srick #endif /* !USE_SYSLOG */ 225