122717Sdist /* 242833Sbostic * Copyright (c) 1983 Eric P. Allman 363589Sbostic * Copyright (c) 1988, 1993 463589Sbostic * The Regents of the University of California. All rights reserved. 533731Sbostic * 642833Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822717Sdist 922717Sdist #ifndef lint 10*67599Seric static char sccsid[] = "@(#)util.c 8.43 (Berkeley) 08/07/94"; 1133731Sbostic #endif /* not lint */ 1222717Sdist 1358332Seric # include "sendmail.h" 14298Seric # include <sysexits.h> 1557135Seric /* 16298Seric ** STRIPQUOTES -- Strip quotes & quote bits from a string. 17298Seric ** 18298Seric ** Runs through a string and strips off unquoted quote 19298Seric ** characters and quote bits. This is done in place. 20298Seric ** 21298Seric ** Parameters: 22298Seric ** s -- the string to strip. 23298Seric ** 24298Seric ** Returns: 25298Seric ** none. 26298Seric ** 27298Seric ** Side Effects: 28298Seric ** none. 29298Seric ** 30298Seric ** Called By: 31298Seric ** deliver 32298Seric */ 33298Seric 3454983Seric stripquotes(s) 35298Seric char *s; 36298Seric { 37298Seric register char *p; 38298Seric register char *q; 39298Seric register char c; 40298Seric 414101Seric if (s == NULL) 424101Seric return; 434101Seric 4454983Seric p = q = s; 4554983Seric do 46298Seric { 4754983Seric c = *p++; 4854983Seric if (c == '\\') 4954983Seric c = *p++; 5054983Seric else if (c == '"') 5154983Seric continue; 5254983Seric *q++ = c; 5354983Seric } while (c != '\0'); 54298Seric } 55298Seric /* 56298Seric ** XALLOC -- Allocate memory and bitch wildly on failure. 57298Seric ** 58298Seric ** THIS IS A CLUDGE. This should be made to give a proper 59298Seric ** error -- but after all, what can we do? 60298Seric ** 61298Seric ** Parameters: 62298Seric ** sz -- size of area to allocate. 63298Seric ** 64298Seric ** Returns: 65298Seric ** pointer to data region. 66298Seric ** 67298Seric ** Side Effects: 68298Seric ** Memory is allocated. 69298Seric */ 70298Seric 71298Seric char * 72298Seric xalloc(sz) 737007Seric register int sz; 74298Seric { 75298Seric register char *p; 76298Seric 7766747Seric /* some systems can't handle size zero mallocs */ 7866747Seric if (sz <= 0) 7966747Seric sz = 1; 8066747Seric 8123121Seric p = malloc((unsigned) sz); 82298Seric if (p == NULL) 83298Seric { 84298Seric syserr("Out of memory!!"); 8510685Seric abort(); 8610685Seric /* exit(EX_UNAVAILABLE); */ 87298Seric } 88298Seric return (p); 89298Seric } 90298Seric /* 913151Seric ** COPYPLIST -- copy list of pointers. 923151Seric ** 933151Seric ** This routine is the equivalent of newstr for lists of 943151Seric ** pointers. 953151Seric ** 963151Seric ** Parameters: 973151Seric ** list -- list of pointers to copy. 983151Seric ** Must be NULL terminated. 993151Seric ** copycont -- if TRUE, copy the contents of the vector 1003151Seric ** (which must be a string) also. 1013151Seric ** 1023151Seric ** Returns: 1033151Seric ** a copy of 'list'. 1043151Seric ** 1053151Seric ** Side Effects: 1063151Seric ** none. 1073151Seric */ 1083151Seric 1093151Seric char ** 1103151Seric copyplist(list, copycont) 1113151Seric char **list; 1123151Seric bool copycont; 1133151Seric { 1143151Seric register char **vp; 1153151Seric register char **newvp; 1163151Seric 1173151Seric for (vp = list; *vp != NULL; vp++) 1183151Seric continue; 1193151Seric 1203151Seric vp++; 1213151Seric 12216897Seric newvp = (char **) xalloc((int) (vp - list) * sizeof *vp); 12316897Seric bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp); 1243151Seric 1253151Seric if (copycont) 1263151Seric { 1273151Seric for (vp = newvp; *vp != NULL; vp++) 1283151Seric *vp = newstr(*vp); 1293151Seric } 1303151Seric 1313151Seric return (newvp); 1323151Seric } 1333151Seric /* 13458170Seric ** COPYQUEUE -- copy address queue. 13558170Seric ** 13658170Seric ** This routine is the equivalent of newstr for address queues 13758170Seric ** addresses marked with QDONTSEND aren't copied 13858170Seric ** 13958170Seric ** Parameters: 14058170Seric ** addr -- list of address structures to copy. 14158170Seric ** 14258170Seric ** Returns: 14358170Seric ** a copy of 'addr'. 14458170Seric ** 14558170Seric ** Side Effects: 14658170Seric ** none. 14758170Seric */ 14858170Seric 14958170Seric ADDRESS * 15058170Seric copyqueue(addr) 15158170Seric ADDRESS *addr; 15258170Seric { 15358170Seric register ADDRESS *newaddr; 15458170Seric ADDRESS *ret; 15558170Seric register ADDRESS **tail = &ret; 15658170Seric 15758170Seric while (addr != NULL) 15858170Seric { 15958170Seric if (!bitset(QDONTSEND, addr->q_flags)) 16058170Seric { 16158170Seric newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS)); 16258170Seric STRUCTCOPY(*addr, *newaddr); 16358170Seric *tail = newaddr; 16458170Seric tail = &newaddr->q_next; 16558170Seric } 16658170Seric addr = addr->q_next; 16758170Seric } 16858170Seric *tail = NULL; 16958170Seric 17058170Seric return ret; 17158170Seric } 17258170Seric /* 1733151Seric ** PRINTAV -- print argument vector. 1743151Seric ** 1753151Seric ** Parameters: 1763151Seric ** av -- argument vector. 1773151Seric ** 1783151Seric ** Returns: 1793151Seric ** none. 1803151Seric ** 1813151Seric ** Side Effects: 1823151Seric ** prints av. 1833151Seric */ 1843151Seric 1853151Seric printav(av) 1863151Seric register char **av; 1873151Seric { 1883151Seric while (*av != NULL) 1893151Seric { 1908063Seric if (tTd(0, 44)) 1918063Seric printf("\n\t%08x=", *av); 1928063Seric else 19323105Seric (void) putchar(' '); 1943151Seric xputs(*av++); 1953151Seric } 19623105Seric (void) putchar('\n'); 1973151Seric } 1983151Seric /* 1993151Seric ** LOWER -- turn letter into lower case. 2003151Seric ** 2013151Seric ** Parameters: 2023151Seric ** c -- character to turn into lower case. 2033151Seric ** 2043151Seric ** Returns: 2053151Seric ** c, in lower case. 2063151Seric ** 2073151Seric ** Side Effects: 2083151Seric ** none. 2093151Seric */ 2103151Seric 2113151Seric char 2123151Seric lower(c) 2133151Seric register char c; 2143151Seric { 21558050Seric return((isascii(c) && isupper(c)) ? tolower(c) : c); 2163151Seric } 2173151Seric /* 2183151Seric ** XPUTS -- put string doing control escapes. 2193151Seric ** 2203151Seric ** Parameters: 2213151Seric ** s -- string to put. 2223151Seric ** 2233151Seric ** Returns: 2243151Seric ** none. 2253151Seric ** 2263151Seric ** Side Effects: 2273151Seric ** output to stdout 2283151Seric */ 2293151Seric 2303151Seric xputs(s) 2313151Seric register char *s; 2323151Seric { 23358050Seric register int c; 23451781Seric register struct metamac *mp; 23551781Seric extern struct metamac MetaMacros[]; 2363151Seric 2378055Seric if (s == NULL) 2388055Seric { 2398055Seric printf("<null>"); 2408055Seric return; 2418055Seric } 24258050Seric while ((c = (*s++ & 0377)) != '\0') 2433151Seric { 2443151Seric if (!isascii(c)) 2453151Seric { 24658050Seric if (c == MATCHREPL || c == MACROEXPAND) 24758050Seric { 24858050Seric putchar('$'); 24958050Seric continue; 25058050Seric } 25158050Seric for (mp = MetaMacros; mp->metaname != '\0'; mp++) 25258050Seric { 25358050Seric if ((mp->metaval & 0377) == c) 25458050Seric { 25558050Seric printf("$%c", mp->metaname); 25658050Seric break; 25758050Seric } 25858050Seric } 25958050Seric if (mp->metaname != '\0') 26058050Seric continue; 26123105Seric (void) putchar('\\'); 2623151Seric c &= 0177; 2633151Seric } 26457589Seric if (isprint(c)) 2653151Seric { 26657589Seric putchar(c); 26757589Seric continue; 26857589Seric } 26952050Seric 27057589Seric /* wasn't a meta-macro -- find another way to print it */ 27157589Seric switch (c) 27257589Seric { 27357589Seric case '\0': 27457589Seric continue; 27552050Seric 27657589Seric case '\n': 27757589Seric c = 'n'; 27857589Seric break; 27952050Seric 28057589Seric case '\r': 28157589Seric c = 'r'; 28257589Seric break; 28352637Seric 28457589Seric case '\t': 28557589Seric c = 't'; 28657589Seric break; 28757589Seric 28857589Seric default: 28957589Seric (void) putchar('^'); 29057589Seric (void) putchar(c ^ 0100); 29157589Seric continue; 2923151Seric } 2933151Seric } 2944086Seric (void) fflush(stdout); 2953151Seric } 2963151Seric /* 2973151Seric ** MAKELOWER -- Translate a line into lower case 2983151Seric ** 2993151Seric ** Parameters: 3003151Seric ** p -- the string to translate. If NULL, return is 3013151Seric ** immediate. 3023151Seric ** 3033151Seric ** Returns: 3043151Seric ** none. 3053151Seric ** 3063151Seric ** Side Effects: 3073151Seric ** String pointed to by p is translated to lower case. 3083151Seric ** 3093151Seric ** Called By: 3103151Seric ** parse 3113151Seric */ 3123151Seric 3133151Seric makelower(p) 3143151Seric register char *p; 3153151Seric { 3163151Seric register char c; 3173151Seric 3183151Seric if (p == NULL) 3193151Seric return; 3203151Seric for (; (c = *p) != '\0'; p++) 3213151Seric if (isascii(c) && isupper(c)) 32233724Sbostic *p = tolower(c); 3233151Seric } 3244059Seric /* 3255196Seric ** BUILDFNAME -- build full name from gecos style entry. 3264375Seric ** 3275196Seric ** This routine interprets the strange entry that would appear 3285196Seric ** in the GECOS field of the password file. 3295196Seric ** 3304375Seric ** Parameters: 3315196Seric ** p -- name to build. 3325196Seric ** login -- the login name of this user (for &). 3335196Seric ** buf -- place to put the result. 3344375Seric ** 3354375Seric ** Returns: 33665006Seric ** none. 3374375Seric ** 3384375Seric ** Side Effects: 3394375Seric ** none. 3404375Seric */ 3414375Seric 34254984Seric buildfname(gecos, login, buf) 34365006Seric register char *gecos; 34465006Seric char *login; 34565006Seric char *buf; 3464375Seric { 34765006Seric register char *p; 34865006Seric register char *bp = buf; 34965006Seric int l; 3504375Seric 35165006Seric if (*gecos == '*') 35265006Seric gecos++; 35365006Seric 35465006Seric /* find length of final string */ 35565006Seric l = 0; 35665006Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 35754984Seric { 35865006Seric if (*p == '&') 35965006Seric l += strlen(login); 36065006Seric else 36165006Seric l++; 36254984Seric } 36365006Seric 36465006Seric /* now fill in buf */ 36565006Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 3664375Seric { 36765006Seric if (*p == '&') 3684375Seric { 36965006Seric (void) strcpy(bp, login); 37065006Seric *bp = toupper(*bp); 37165006Seric while (*bp != '\0') 37265006Seric bp++; 3734375Seric } 37465006Seric else 37565006Seric *bp++ = *p; 3764375Seric } 3774375Seric *bp = '\0'; 3784375Seric } 3794375Seric /* 3804538Seric ** SAFEFILE -- return true if a file exists and is safe for a user. 3814538Seric ** 3824538Seric ** Parameters: 3834538Seric ** fn -- filename to check. 38464083Seric ** uid -- user id to compare against. 38564083Seric ** gid -- group id to compare against. 38664083Seric ** uname -- user name to compare against (used for group 38764083Seric ** sets). 38864944Seric ** flags -- modifiers: 38965064Seric ** SFF_MUSTOWN -- "uid" must own this file. 39065064Seric ** SFF_NOSLINK -- file cannot be a symbolic link. 3914538Seric ** mode -- mode bits that must match. 3924538Seric ** 3934538Seric ** Returns: 39458247Seric ** 0 if fn exists, is owned by uid, and matches mode. 39558247Seric ** An errno otherwise. The actual errno is cleared. 3964538Seric ** 3974538Seric ** Side Effects: 3984538Seric ** none. 3994538Seric */ 4004538Seric 40164083Seric #include <grp.h> 40264083Seric 40363581Seric #ifndef S_IXOTH 40463581Seric # define S_IXOTH (S_IEXEC >> 6) 40563581Seric #endif 40663581Seric 40764083Seric #ifndef S_IXGRP 40864083Seric # define S_IXGRP (S_IEXEC >> 3) 40964083Seric #endif 41064083Seric 41163753Seric #ifndef S_IXUSR 41263753Seric # define S_IXUSR (S_IEXEC) 41363753Seric #endif 41463753Seric 41558247Seric int 41664944Seric safefile(fn, uid, gid, uname, flags, mode) 4174538Seric char *fn; 41855372Seric uid_t uid; 41964083Seric gid_t gid; 42064083Seric char *uname; 42164944Seric int flags; 4224538Seric int mode; 4234538Seric { 42463581Seric register char *p; 42564083Seric register struct group *gr = NULL; 4264538Seric struct stat stbuf; 4274538Seric 42863581Seric if (tTd(54, 4)) 42964944Seric printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n", 43064944Seric fn, uid, gid, flags, mode); 43163581Seric errno = 0; 43263581Seric 43363581Seric for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/') 43463581Seric { 43563581Seric *p = '\0'; 43664083Seric if (stat(fn, &stbuf) < 0) 43764083Seric break; 43865225Seric if (uid == 0 && !bitset(SFF_ROOTOK, flags)) 43965225Seric { 44065225Seric if (bitset(S_IXOTH, stbuf.st_mode)) 44165225Seric continue; 44265225Seric break; 44365225Seric } 44464362Seric if (stbuf.st_uid == uid && bitset(S_IXUSR, stbuf.st_mode)) 44564362Seric continue; 44664083Seric if (stbuf.st_gid == gid && bitset(S_IXGRP, stbuf.st_mode)) 44764083Seric continue; 44864084Seric #ifndef NO_GROUP_SET 44964083Seric if (uname != NULL && 45064083Seric ((gr != NULL && gr->gr_gid == stbuf.st_gid) || 45164083Seric (gr = getgrgid(stbuf.st_gid)) != NULL)) 45263581Seric { 45364083Seric register char **gp; 45463581Seric 45564083Seric for (gp = gr->gr_mem; *gp != NULL; gp++) 45664083Seric if (strcmp(*gp, uname) == 0) 45764083Seric break; 45864362Seric if (*gp != NULL && bitset(S_IXGRP, stbuf.st_mode)) 45964362Seric continue; 46063581Seric } 46164084Seric #endif 46264083Seric if (!bitset(S_IXOTH, stbuf.st_mode)) 46364083Seric break; 46463581Seric } 46564083Seric if (p != NULL) 46664083Seric { 46764083Seric int ret = errno; 46863581Seric 46964083Seric if (ret == 0) 47064083Seric ret = EACCES; 47164083Seric if (tTd(54, 4)) 47264083Seric printf("\t[dir %s] %s\n", fn, errstring(ret)); 47364083Seric *p = '/'; 47464083Seric return ret; 47564083Seric } 47664083Seric 47764944Seric #ifdef HASLSTAT 47865064Seric if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, &stbuf) 47965064Seric : stat(fn, &stbuf)) < 0) 48064944Seric #else 48158247Seric if (stat(fn, &stbuf) < 0) 48264944Seric #endif 48358247Seric { 48458247Seric int ret = errno; 48558247Seric 48663581Seric if (tTd(54, 4)) 48764083Seric printf("\t%s\n", errstring(ret)); 48863581Seric 48958247Seric errno = 0; 49058247Seric return ret; 49158247Seric } 49264944Seric 49364944Seric #ifdef S_ISLNK 49465064Seric if (bitset(SFF_NOSLINK, flags) && S_ISLNK(stbuf.st_mode)) 49564944Seric { 49664944Seric if (tTd(54, 4)) 49765123Seric printf("\t[slink mode %o]\tEPERM\n", stbuf.st_mode); 49864944Seric return EPERM; 49964944Seric } 50064944Seric #endif 50164944Seric 50265139Seric if (uid == 0 && !bitset(SFF_ROOTOK, flags)) 50363581Seric mode >>= 6; 50464084Seric else if (stbuf.st_uid != uid) 50564084Seric { 50664084Seric mode >>= 3; 50764084Seric if (stbuf.st_gid == gid) 50864084Seric ; 50964084Seric #ifndef NO_GROUP_SET 51064084Seric else if (uname != NULL && 51164084Seric ((gr != NULL && gr->gr_gid == stbuf.st_gid) || 51264084Seric (gr = getgrgid(stbuf.st_gid)) != NULL)) 51364084Seric { 51464084Seric register char **gp; 51564084Seric 51664084Seric for (gp = gr->gr_mem; *gp != NULL; gp++) 51764084Seric if (strcmp(*gp, uname) == 0) 51864084Seric break; 51964084Seric if (*gp == NULL) 52064084Seric mode >>= 3; 52164084Seric } 52264084Seric #endif 52364084Seric else 52464084Seric mode >>= 3; 52564084Seric } 52663581Seric if (tTd(54, 4)) 52764084Seric printf("\t[uid %d, stat %o, mode %o] ", 52864084Seric stbuf.st_uid, stbuf.st_mode, mode); 52964944Seric if ((stbuf.st_uid == uid || stbuf.st_uid == 0 || 53065064Seric !bitset(SFF_MUSTOWN, flags)) && 53163581Seric (stbuf.st_mode & mode) == mode) 53263581Seric { 53363581Seric if (tTd(54, 4)) 53464083Seric printf("\tOK\n"); 53558247Seric return 0; 53663581Seric } 53763581Seric if (tTd(54, 4)) 53864083Seric printf("\tEACCES\n"); 53963581Seric return EACCES; 5404538Seric } 5414538Seric /* 5424557Seric ** FIXCRLF -- fix <CR><LF> in line. 5434557Seric ** 5444557Seric ** Looks for the <CR><LF> combination and turns it into the 5454557Seric ** UNIX canonical <NL> character. It only takes one line, 5464557Seric ** i.e., it is assumed that the first <NL> found is the end 5474557Seric ** of the line. 5484557Seric ** 5494557Seric ** Parameters: 5504557Seric ** line -- the line to fix. 5514557Seric ** stripnl -- if true, strip the newline also. 5524557Seric ** 5534557Seric ** Returns: 5544557Seric ** none. 5554557Seric ** 5564557Seric ** Side Effects: 5574557Seric ** line is changed in place. 5584557Seric */ 5594557Seric 5604557Seric fixcrlf(line, stripnl) 5614557Seric char *line; 5624557Seric bool stripnl; 5634557Seric { 5644557Seric register char *p; 5654557Seric 56656795Seric p = strchr(line, '\n'); 5674557Seric if (p == NULL) 5684557Seric return; 56936291Sbostic if (p > line && p[-1] == '\r') 5704557Seric p--; 5714557Seric if (!stripnl) 5724557Seric *p++ = '\n'; 5734557Seric *p = '\0'; 5744557Seric } 5754557Seric /* 5766890Seric ** DFOPEN -- determined file open 5776890Seric ** 5786890Seric ** This routine has the semantics of fopen, except that it will 5796890Seric ** keep trying a few times to make this happen. The idea is that 5806890Seric ** on very loaded systems, we may run out of resources (inodes, 5816890Seric ** whatever), so this tries to get around it. 5826890Seric */ 5836890Seric 58463753Seric #ifndef O_ACCMODE 58563753Seric # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) 58663753Seric #endif 58763753Seric 58859745Seric struct omodes 58959745Seric { 59059745Seric int mask; 59159745Seric int mode; 59259745Seric char *farg; 59359745Seric } OpenModes[] = 59459745Seric { 59559745Seric O_ACCMODE, O_RDONLY, "r", 59659745Seric O_ACCMODE|O_APPEND, O_WRONLY, "w", 59759745Seric O_ACCMODE|O_APPEND, O_WRONLY|O_APPEND, "a", 59859745Seric O_TRUNC, 0, "w+", 59959745Seric O_APPEND, O_APPEND, "a+", 60059745Seric 0, 0, "r+", 60159745Seric }; 60259745Seric 6036890Seric FILE * 60459745Seric dfopen(filename, omode, cmode) 6056890Seric char *filename; 60659745Seric int omode; 60759745Seric int cmode; 6086890Seric { 6096890Seric register int tries; 61059745Seric int fd; 61159745Seric register struct omodes *om; 61259431Seric struct stat st; 6136890Seric 61459745Seric for (om = OpenModes; om->mask != 0; om++) 61559745Seric if ((omode & om->mask) == om->mode) 61659745Seric break; 61759745Seric 6186890Seric for (tries = 0; tries < 10; tries++) 6196890Seric { 62025618Seric sleep((unsigned) (10 * tries)); 6216890Seric errno = 0; 62259745Seric fd = open(filename, omode, cmode); 62359745Seric if (fd >= 0) 6246890Seric break; 62566017Seric switch (errno) 62666017Seric { 62766017Seric case ENFILE: /* system file table full */ 62866017Seric case EINTR: /* interrupted syscall */ 62966017Seric #ifdef ETXTBSY 63066017Seric case ETXTBSY: /* Apollo: net file locked */ 63166017Seric #endif 63266017Seric continue; 63366017Seric } 63466017Seric break; 6356890Seric } 63659745Seric if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode)) 63756328Seric { 63856328Seric int locktype; 63956328Seric 64056328Seric /* lock the file to avoid accidental conflicts */ 64159745Seric if ((omode & O_ACCMODE) != O_RDONLY) 64256328Seric locktype = LOCK_EX; 64356328Seric else 64456328Seric locktype = LOCK_SH; 64564335Seric (void) lockfile(fd, filename, NULL, locktype); 64656328Seric errno = 0; 64756328Seric } 64863787Seric if (fd < 0) 64963787Seric return NULL; 65063787Seric else 65163787Seric return fdopen(fd, om->farg); 6526890Seric } 6537124Seric /* 6547124Seric ** PUTLINE -- put a line like fputs obeying SMTP conventions 6557124Seric ** 6567753Seric ** This routine always guarantees outputing a newline (or CRLF, 6577753Seric ** as appropriate) at the end of the string. 6587753Seric ** 6597124Seric ** Parameters: 6607124Seric ** l -- line to put. 66165870Seric ** mci -- the mailer connection information. 6627124Seric ** 6637124Seric ** Returns: 6647124Seric ** none 6657124Seric ** 6667124Seric ** Side Effects: 6677124Seric ** output of l to fp. 6687124Seric */ 6697124Seric 67065870Seric putline(l, mci) 6717753Seric register char *l; 67265870Seric register MCI *mci; 6737124Seric { 6747124Seric register char *p; 67547157Sbostic register char svchar; 67666004Seric int slop = 0; 6777124Seric 67811275Seric /* strip out 0200 bits -- these can look like TELNET protocol */ 67965870Seric if (bitset(MCIF_7BIT, mci->mci_flags)) 68011275Seric { 68161707Seric for (p = l; (svchar = *p) != '\0'; ++p) 68261707Seric if (bitset(0200, svchar)) 68347157Sbostic *p = svchar &~ 0200; 68411275Seric } 68511275Seric 6867753Seric do 6877124Seric { 6887753Seric /* find the end of the line */ 68956795Seric p = strchr(l, '\n'); 6907753Seric if (p == NULL) 6917753Seric p = &l[strlen(l)]; 6927124Seric 69363753Seric if (TrafficLogFile != NULL) 69463753Seric fprintf(TrafficLogFile, "%05d >>> ", getpid()); 69563753Seric 6967753Seric /* check for line overflow */ 69765870Seric while (mci->mci_mailer->m_linelimit > 0 && 69866004Seric (p - l + slop) > mci->mci_mailer->m_linelimit) 6997753Seric { 70066004Seric register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1]; 7017124Seric 7027753Seric svchar = *q; 7037753Seric *q = '\0'; 70466004Seric if (l[0] == '.' && slop == 0 && 70566004Seric bitnset(M_XDOT, mci->mci_mailer->m_flags)) 70663753Seric { 70765870Seric (void) putc('.', mci->mci_out); 70863753Seric if (TrafficLogFile != NULL) 70963753Seric (void) putc('.', TrafficLogFile); 71063753Seric } 71165870Seric fputs(l, mci->mci_out); 71265870Seric (void) putc('!', mci->mci_out); 71365870Seric fputs(mci->mci_mailer->m_eol, mci->mci_out); 71466004Seric (void) putc(' ', mci->mci_out); 71563753Seric if (TrafficLogFile != NULL) 71666004Seric fprintf(TrafficLogFile, "%s!\n%05d >>> ", 71763753Seric l, getpid()); 7187753Seric *q = svchar; 7197753Seric l = q; 72066004Seric slop = 1; 7217753Seric } 7227124Seric 7237753Seric /* output last part */ 72466004Seric if (l[0] == '.' && slop == 0 && 72566004Seric bitnset(M_XDOT, mci->mci_mailer->m_flags)) 72663753Seric { 72765870Seric (void) putc('.', mci->mci_out); 72863753Seric if (TrafficLogFile != NULL) 72963753Seric (void) putc('.', TrafficLogFile); 73063753Seric } 73163753Seric if (TrafficLogFile != NULL) 73263753Seric fprintf(TrafficLogFile, "%.*s\n", p - l, l); 73347157Sbostic for ( ; l < p; ++l) 73465870Seric (void) putc(*l, mci->mci_out); 73565870Seric fputs(mci->mci_mailer->m_eol, mci->mci_out); 7367753Seric if (*l == '\n') 73747157Sbostic ++l; 7387753Seric } while (l[0] != '\0'); 7397124Seric } 7407676Seric /* 7417676Seric ** XUNLINK -- unlink a file, doing logging as appropriate. 7427676Seric ** 7437676Seric ** Parameters: 7447676Seric ** f -- name of file to unlink. 7457676Seric ** 7467676Seric ** Returns: 7477676Seric ** none. 7487676Seric ** 7497676Seric ** Side Effects: 7507676Seric ** f is unlinked. 7517676Seric */ 7527676Seric 7537676Seric xunlink(f) 7547676Seric char *f; 7557676Seric { 7567676Seric register int i; 7577676Seric 7587676Seric # ifdef LOG 75958020Seric if (LogLevel > 98) 76058020Seric syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 76156795Seric # endif /* LOG */ 7627676Seric 7637676Seric i = unlink(f); 7647676Seric # ifdef LOG 76558020Seric if (i < 0 && LogLevel > 97) 7667942Seric syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 76756795Seric # endif /* LOG */ 7687676Seric } 7697685Seric /* 77058680Seric ** XFCLOSE -- close a file, doing logging as appropriate. 77158680Seric ** 77258680Seric ** Parameters: 77358680Seric ** fp -- file pointer for the file to close 77458680Seric ** a, b -- miscellaneous crud to print for debugging 77558680Seric ** 77658680Seric ** Returns: 77758680Seric ** none. 77858680Seric ** 77958680Seric ** Side Effects: 78058680Seric ** fp is closed. 78158680Seric */ 78258680Seric 78358680Seric xfclose(fp, a, b) 78458680Seric FILE *fp; 78558680Seric char *a, *b; 78658680Seric { 78758796Seric if (tTd(53, 99)) 78858680Seric printf("xfclose(%x) %s %s\n", fp, a, b); 78964401Seric #ifdef XDEBUG 79064401Seric if (fileno(fp) == 1) 79164401Seric syserr("xfclose(%s %s): fd = 1", a, b); 79264401Seric #endif 79358796Seric if (fclose(fp) < 0 && tTd(53, 99)) 79458680Seric printf("xfclose FAILURE: %s\n", errstring(errno)); 79558680Seric } 79658680Seric /* 79714885Seric ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 7987685Seric ** 7997685Seric ** Parameters: 8007685Seric ** buf -- place to put the input line. 8017685Seric ** siz -- size of buf. 8027685Seric ** fp -- file to read from. 80357384Seric ** timeout -- the timeout before error occurs. 80461093Seric ** during -- what we are trying to read (for error messages). 8057685Seric ** 8067685Seric ** Returns: 80715533Seric ** NULL on error (including timeout). This will also leave 80815533Seric ** buf containing a null string. 8097685Seric ** buf otherwise. 8107685Seric ** 8117685Seric ** Side Effects: 8127685Seric ** none. 8137685Seric */ 8147685Seric 81514885Seric static jmp_buf CtxReadTimeout; 81663937Seric static int readtimeout(); 8177685Seric 8187685Seric char * 81961093Seric sfgets(buf, siz, fp, timeout, during) 8207685Seric char *buf; 8217685Seric int siz; 8227685Seric FILE *fp; 82357384Seric time_t timeout; 82461093Seric char *during; 8257685Seric { 8267942Seric register EVENT *ev = NULL; 8277685Seric register char *p; 8287685Seric 82966332Seric if (fp == NULL) 83066332Seric { 83166332Seric buf[0] = '\0'; 83266332Seric return NULL; 83366332Seric } 83466332Seric 83514885Seric /* set the timeout */ 83657384Seric if (timeout != 0) 83714885Seric { 83814885Seric if (setjmp(CtxReadTimeout) != 0) 83914885Seric { 84036233Skarels # ifdef LOG 84136230Skarels syslog(LOG_NOTICE, 84261093Seric "timeout waiting for input from %s during %s\n", 84361093Seric CurHostName? CurHostName: "local", during); 84436233Skarels # endif 84536230Skarels errno = 0; 84661093Seric usrerr("451 timeout waiting for input during %s", 84761093Seric during); 84819037Seric buf[0] = '\0'; 84963753Seric #ifdef XDEBUG 85063753Seric checkfd012(during); 85163753Seric #endif 85214885Seric return (NULL); 85314885Seric } 854*67599Seric ev = setevent(timeout, readtimeout, 0); 85514885Seric } 85614885Seric 85714885Seric /* try to read */ 85815533Seric p = NULL; 85965190Seric while (!feof(fp) && !ferror(fp)) 8607942Seric { 8617942Seric errno = 0; 8627942Seric p = fgets(buf, siz, fp); 86365190Seric if (p != NULL || errno != EINTR) 86465186Seric break; 86565190Seric clearerr(fp); 86615533Seric } 86714885Seric 86814885Seric /* clear the event if it has not sprung */ 869*67599Seric clrevent(ev); 87014885Seric 87114885Seric /* clean up the books and exit */ 8728055Seric LineNumber++; 87315533Seric if (p == NULL) 87416880Seric { 87515533Seric buf[0] = '\0'; 87663753Seric if (TrafficLogFile != NULL) 87763753Seric fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid()); 87816880Seric return (NULL); 87916880Seric } 88063753Seric if (TrafficLogFile != NULL) 88163753Seric fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf); 88267546Seric if (SevenBitInput) 88367546Seric { 88452106Seric for (p = buf; *p != '\0'; p++) 88552106Seric *p &= ~0200; 88667546Seric } 88767546Seric else if (!HasEightBits) 88867546Seric { 88967546Seric for (p = buf; *p != '\0'; p++) 89067546Seric { 89167546Seric if (bitset(0200, *p)) 89267546Seric { 89367546Seric HasEightBits = TRUE; 89467546Seric break; 89567546Seric } 89667546Seric } 89767546Seric } 89816880Seric return (buf); 8997685Seric } 9007685Seric 9017685Seric static 90266765Seric readtimeout(timeout) 90366765Seric time_t timeout; 9047685Seric { 905*67599Seric longjmp(CtxReadTimeout, 1); 9067685Seric } 9077786Seric /* 9087786Seric ** FGETFOLDED -- like fgets, but know about folded lines. 9097786Seric ** 9107786Seric ** Parameters: 9117786Seric ** buf -- place to put result. 9127786Seric ** n -- bytes available. 9137786Seric ** f -- file to read from. 9147786Seric ** 9157786Seric ** Returns: 91657135Seric ** input line(s) on success, NULL on error or EOF. 91757135Seric ** This will normally be buf -- unless the line is too 91857135Seric ** long, when it will be xalloc()ed. 9197786Seric ** 9207786Seric ** Side Effects: 9217786Seric ** buf gets lines from f, with continuation lines (lines 9227786Seric ** with leading white space) appended. CRLF's are mapped 9237786Seric ** into single newlines. Any trailing NL is stripped. 9247786Seric */ 9257786Seric 9267786Seric char * 9277786Seric fgetfolded(buf, n, f) 9287786Seric char *buf; 9297786Seric register int n; 9307786Seric FILE *f; 9317786Seric { 9327786Seric register char *p = buf; 93357135Seric char *bp = buf; 9347786Seric register int i; 9357786Seric 9367786Seric n--; 93717350Seric while ((i = getc(f)) != EOF) 9387786Seric { 93917350Seric if (i == '\r') 94017350Seric { 94117350Seric i = getc(f); 94217350Seric if (i != '\n') 94317350Seric { 94417350Seric if (i != EOF) 94523105Seric (void) ungetc(i, f); 94617350Seric i = '\r'; 94717350Seric } 94817350Seric } 94957135Seric if (--n <= 0) 95057135Seric { 95157135Seric /* allocate new space */ 95257135Seric char *nbp; 95357135Seric int nn; 95457135Seric 95557135Seric nn = (p - bp); 95657232Seric if (nn < MEMCHUNKSIZE) 95757135Seric nn *= 2; 95857135Seric else 95957232Seric nn += MEMCHUNKSIZE; 96057135Seric nbp = xalloc(nn); 96157135Seric bcopy(bp, nbp, p - bp); 96257135Seric p = &nbp[p - bp]; 96357135Seric if (bp != buf) 96457135Seric free(bp); 96557135Seric bp = nbp; 96657135Seric n = nn - (p - bp); 96757135Seric } 96857135Seric *p++ = i; 96917350Seric if (i == '\n') 97017350Seric { 97117350Seric LineNumber++; 97217350Seric i = getc(f); 97317350Seric if (i != EOF) 97423105Seric (void) ungetc(i, f); 97517350Seric if (i != ' ' && i != '\t') 97652647Seric break; 97717350Seric } 9787786Seric } 97957135Seric if (p == bp) 98052647Seric return (NULL); 98152647Seric *--p = '\0'; 98257135Seric return (bp); 9837786Seric } 9847860Seric /* 9857886Seric ** CURTIME -- return current time. 9867886Seric ** 9877886Seric ** Parameters: 9887886Seric ** none. 9897886Seric ** 9907886Seric ** Returns: 9917886Seric ** the current time. 9927886Seric ** 9937886Seric ** Side Effects: 9947886Seric ** none. 9957886Seric */ 9967886Seric 9977886Seric time_t 9987886Seric curtime() 9997886Seric { 10007886Seric auto time_t t; 10017886Seric 10027886Seric (void) time(&t); 10037886Seric return (t); 10047886Seric } 10058264Seric /* 10068264Seric ** ATOBOOL -- convert a string representation to boolean. 10078264Seric ** 10088264Seric ** Defaults to "TRUE" 10098264Seric ** 10108264Seric ** Parameters: 10118264Seric ** s -- string to convert. Takes "tTyY" as true, 10128264Seric ** others as false. 10138264Seric ** 10148264Seric ** Returns: 10158264Seric ** A boolean representation of the string. 10168264Seric ** 10178264Seric ** Side Effects: 10188264Seric ** none. 10198264Seric */ 10208264Seric 10218264Seric bool 10228264Seric atobool(s) 10238264Seric register char *s; 10248264Seric { 102563833Seric if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL) 10268264Seric return (TRUE); 10278264Seric return (FALSE); 10288264Seric } 10299048Seric /* 10309048Seric ** ATOOCT -- convert a string representation to octal. 10319048Seric ** 10329048Seric ** Parameters: 10339048Seric ** s -- string to convert. 10349048Seric ** 10359048Seric ** Returns: 10369048Seric ** An integer representing the string interpreted as an 10379048Seric ** octal number. 10389048Seric ** 10399048Seric ** Side Effects: 10409048Seric ** none. 10419048Seric */ 10429048Seric 10439048Seric atooct(s) 10449048Seric register char *s; 10459048Seric { 10469048Seric register int i = 0; 10479048Seric 10489048Seric while (*s >= '0' && *s <= '7') 10499048Seric i = (i << 3) | (*s++ - '0'); 10509048Seric return (i); 10519048Seric } 10529376Seric /* 10539376Seric ** WAITFOR -- wait for a particular process id. 10549376Seric ** 10559376Seric ** Parameters: 10569376Seric ** pid -- process id to wait for. 10579376Seric ** 10589376Seric ** Returns: 10599376Seric ** status of pid. 10609376Seric ** -1 if pid never shows up. 10619376Seric ** 10629376Seric ** Side Effects: 10639376Seric ** none. 10649376Seric */ 10659376Seric 106664562Seric int 10679376Seric waitfor(pid) 10689376Seric int pid; 10699376Seric { 107064562Seric #ifdef WAITUNION 107164562Seric union wait st; 107264562Seric #else 10739376Seric auto int st; 107464562Seric #endif 10759376Seric int i; 10769376Seric 10779376Seric do 10789376Seric { 10799376Seric errno = 0; 10809376Seric i = wait(&st); 10819376Seric } while ((i >= 0 || errno == EINTR) && i != pid); 10829376Seric if (i < 0) 108364562Seric return -1; 108464562Seric #ifdef WAITUNION 108564562Seric return st.w_status; 108664562Seric #else 108764562Seric return st; 108864562Seric #endif 10899376Seric } 10909376Seric /* 109110685Seric ** BITINTERSECT -- tell if two bitmaps intersect 109210685Seric ** 109310685Seric ** Parameters: 109410685Seric ** a, b -- the bitmaps in question 109510685Seric ** 109610685Seric ** Returns: 109710685Seric ** TRUE if they have a non-null intersection 109810685Seric ** FALSE otherwise 109910685Seric ** 110010685Seric ** Side Effects: 110110685Seric ** none. 110210685Seric */ 110310685Seric 110410685Seric bool 110510685Seric bitintersect(a, b) 110610685Seric BITMAP a; 110710685Seric BITMAP b; 110810685Seric { 110910685Seric int i; 111010685Seric 111110685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 111210685Seric if ((a[i] & b[i]) != 0) 111310685Seric return (TRUE); 111410685Seric return (FALSE); 111510685Seric } 111610685Seric /* 111710685Seric ** BITZEROP -- tell if a bitmap is all zero 111810685Seric ** 111910685Seric ** Parameters: 112010685Seric ** map -- the bit map to check 112110685Seric ** 112210685Seric ** Returns: 112310685Seric ** TRUE if map is all zero. 112410685Seric ** FALSE if there are any bits set in map. 112510685Seric ** 112610685Seric ** Side Effects: 112710685Seric ** none. 112810685Seric */ 112910685Seric 113010685Seric bool 113110685Seric bitzerop(map) 113210685Seric BITMAP map; 113310685Seric { 113410685Seric int i; 113510685Seric 113610685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 113710685Seric if (map[i] != 0) 113810685Seric return (FALSE); 113910685Seric return (TRUE); 114010685Seric } 114158247Seric /* 114258318Seric ** STRCONTAINEDIN -- tell if one string is contained in another 114358318Seric ** 114458318Seric ** Parameters: 114558318Seric ** a -- possible substring. 114658318Seric ** b -- possible superstring. 114758318Seric ** 114858318Seric ** Returns: 114958318Seric ** TRUE if a is contained in b. 115058318Seric ** FALSE otherwise. 115158318Seric */ 115258318Seric 115358318Seric bool 115458318Seric strcontainedin(a, b) 115558318Seric register char *a; 115658318Seric register char *b; 115758318Seric { 115865012Seric int la; 115965012Seric int lb; 116065012Seric int c; 116158318Seric 116265012Seric la = strlen(a); 116365012Seric lb = strlen(b); 116465012Seric c = *a; 116565012Seric if (isascii(c) && isupper(c)) 116665012Seric c = tolower(c); 116765012Seric for (; lb-- >= la; b++) 116858318Seric { 116965012Seric if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c) 117065012Seric continue; 117165012Seric if (strncasecmp(a, b, la) == 0) 117258318Seric return TRUE; 117358318Seric } 117465012Seric return FALSE; 117558318Seric } 117663753Seric /* 117763753Seric ** CHECKFD012 -- check low numbered file descriptors 117863753Seric ** 117963753Seric ** File descriptors 0, 1, and 2 should be open at all times. 118063753Seric ** This routine verifies that, and fixes it if not true. 118163753Seric ** 118263753Seric ** Parameters: 118363753Seric ** where -- a tag printed if the assertion failed 118463753Seric ** 118563753Seric ** Returns: 118663753Seric ** none 118763753Seric */ 118863753Seric 118963753Seric checkfd012(where) 119063753Seric char *where; 119163753Seric { 119263753Seric #ifdef XDEBUG 119363753Seric register int i; 119463753Seric struct stat stbuf; 119563753Seric 119663753Seric for (i = 0; i < 3; i++) 119763753Seric { 119864735Seric if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP) 119963753Seric { 120063753Seric /* oops.... */ 120163753Seric int fd; 120263753Seric 120363753Seric syserr("%s: fd %d not open", where, i); 120463753Seric fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666); 120563753Seric if (fd != i) 120663753Seric { 120763753Seric (void) dup2(fd, i); 120863753Seric (void) close(fd); 120963753Seric } 121063753Seric } 121163753Seric } 121263937Seric #endif /* XDEBUG */ 121363753Seric } 121464725Seric /* 121564725Seric ** PRINTOPENFDS -- print the open file descriptors (for debugging) 121664725Seric ** 121764725Seric ** Parameters: 121864725Seric ** logit -- if set, send output to syslog; otherwise 121964725Seric ** print for debugging. 122064725Seric ** 122164725Seric ** Returns: 122264725Seric ** none. 122364725Seric */ 122464725Seric 122564725Seric #include <netdb.h> 122664725Seric #include <arpa/inet.h> 122764725Seric 122864725Seric printopenfds(logit) 122964725Seric bool logit; 123064725Seric { 123164725Seric register int fd; 123264743Seric extern int DtableSize; 123364743Seric 123464743Seric for (fd = 0; fd < DtableSize; fd++) 123564743Seric dumpfd(fd, FALSE, logit); 123664743Seric } 123764743Seric /* 123864743Seric ** DUMPFD -- dump a file descriptor 123964743Seric ** 124064743Seric ** Parameters: 124164743Seric ** fd -- the file descriptor to dump. 124264743Seric ** printclosed -- if set, print a notification even if 124364743Seric ** it is closed; otherwise print nothing. 124464743Seric ** logit -- if set, send output to syslog instead of stdout. 124564743Seric */ 124664743Seric 124764743Seric dumpfd(fd, printclosed, logit) 124864743Seric int fd; 124964743Seric bool printclosed; 125064743Seric bool logit; 125164743Seric { 125264725Seric register struct hostent *hp; 125364725Seric register char *p; 125466747Seric char *fmtstr; 125564743Seric struct sockaddr_in sin; 125664743Seric auto int slen; 125764743Seric struct stat st; 125864725Seric char buf[200]; 125964725Seric 126064743Seric p = buf; 126164743Seric sprintf(p, "%3d: ", fd); 126264743Seric p += strlen(p); 126364743Seric 126464743Seric if (fstat(fd, &st) < 0) 126564725Seric { 126664743Seric if (printclosed || errno != EBADF) 126764743Seric { 126864743Seric sprintf(p, "CANNOT STAT (%s)", errstring(errno)); 126964743Seric goto printit; 127064743Seric } 127164743Seric return; 127264743Seric } 127364725Seric 127464743Seric slen = fcntl(fd, F_GETFL, NULL); 127564743Seric if (slen != -1) 127664743Seric { 127764743Seric sprintf(p, "fl=0x%x, ", slen); 127864743Seric p += strlen(p); 127964743Seric } 128064725Seric 128164743Seric sprintf(p, "mode=%o: ", st.st_mode); 128264743Seric p += strlen(p); 128364743Seric switch (st.st_mode & S_IFMT) 128464743Seric { 128564807Seric #ifdef S_IFSOCK 128664743Seric case S_IFSOCK: 128764743Seric sprintf(p, "SOCK "); 128864725Seric p += strlen(p); 128964743Seric slen = sizeof sin; 129064743Seric if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0) 129164743Seric sprintf(p, "(badsock)"); 129264743Seric else 129364725Seric { 129467419Seric hp = gethostbyaddr((char *) &sin.sin_addr, 129567421Seric INADDRSZ, AF_INET); 129664743Seric sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr) 129764743Seric : hp->h_name, ntohs(sin.sin_port)); 129864743Seric } 129964743Seric p += strlen(p); 130064743Seric sprintf(p, "->"); 130164743Seric p += strlen(p); 130264743Seric slen = sizeof sin; 130364743Seric if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0) 130464743Seric sprintf(p, "(badsock)"); 130564743Seric else 130664743Seric { 130767419Seric hp = gethostbyaddr((char *) &sin.sin_addr, 130867421Seric INADDRSZ, AF_INET); 130964743Seric sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr) 131064743Seric : hp->h_name, ntohs(sin.sin_port)); 131164743Seric } 131264743Seric break; 131364807Seric #endif 131464725Seric 131564743Seric case S_IFCHR: 131664743Seric sprintf(p, "CHR: "); 131764743Seric p += strlen(p); 131864743Seric goto defprint; 131964725Seric 132064743Seric case S_IFBLK: 132164743Seric sprintf(p, "BLK: "); 132264743Seric p += strlen(p); 132364743Seric goto defprint; 132464725Seric 132566252Seric #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK) 132665378Seric case S_IFIFO: 132765378Seric sprintf(p, "FIFO: "); 132865378Seric p += strlen(p); 132965378Seric goto defprint; 133065378Seric #endif 133165378Seric 133265378Seric #ifdef S_IFDIR 133365378Seric case S_IFDIR: 133465378Seric sprintf(p, "DIR: "); 133565378Seric p += strlen(p); 133665378Seric goto defprint; 133765378Seric #endif 133865378Seric 133965378Seric #ifdef S_IFLNK 134065378Seric case S_IFLNK: 134165378Seric sprintf(p, "LNK: "); 134265378Seric p += strlen(p); 134365378Seric goto defprint; 134465378Seric #endif 134565378Seric 134664743Seric default: 134764725Seric defprint: 134866785Seric if (sizeof st.st_size > sizeof (long)) 134966751Seric fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd"; 135066747Seric else 135166751Seric fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld"; 135266747Seric sprintf(p, fmtstr, 135364743Seric major(st.st_dev), minor(st.st_dev), st.st_ino, 135464743Seric st.st_nlink, st.st_uid, st.st_gid, st.st_size); 135564743Seric break; 135664743Seric } 135764725Seric 135864743Seric printit: 135966748Seric #ifdef LOG 136064743Seric if (logit) 136165006Seric syslog(LOG_DEBUG, "%s", buf); 136264743Seric else 136366748Seric #endif 136464743Seric printf("%s\n", buf); 136564725Seric } 136665015Seric /* 136765015Seric ** SHORTENSTRING -- return short version of a string 136865015Seric ** 136965015Seric ** If the string is already short, just return it. If it is too 137065015Seric ** long, return the head and tail of the string. 137165015Seric ** 137265015Seric ** Parameters: 137365015Seric ** s -- the string to shorten. 137465015Seric ** m -- the max length of the string. 137565015Seric ** 137665015Seric ** Returns: 137765015Seric ** Either s or a short version of s. 137865015Seric */ 137965015Seric 138065015Seric #ifndef MAXSHORTSTR 138165055Seric # define MAXSHORTSTR 203 138265015Seric #endif 138365015Seric 138465015Seric char * 138565015Seric shortenstring(s, m) 138665015Seric register char *s; 138765015Seric int m; 138865015Seric { 138965015Seric int l; 139065015Seric static char buf[MAXSHORTSTR + 1]; 139165015Seric 139265015Seric l = strlen(s); 139365015Seric if (l < m) 139465015Seric return s; 139565015Seric if (m > MAXSHORTSTR) 139665015Seric m = MAXSHORTSTR; 139765015Seric else if (m < 10) 139865015Seric { 139965015Seric if (m < 5) 140065015Seric { 140165015Seric strncpy(buf, s, m); 140265015Seric buf[m] = '\0'; 140365015Seric return buf; 140465015Seric } 140565015Seric strncpy(buf, s, m - 3); 140665015Seric strcpy(buf + m - 3, "..."); 140765015Seric return buf; 140865015Seric } 140965015Seric m = (m - 3) / 2; 141065015Seric strncpy(buf, s, m); 141165015Seric strcpy(buf + m, "..."); 141265015Seric strcpy(buf + m + 3, s + l - m); 141365015Seric return buf; 141465015Seric } 1415