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*64362Seric static char sccsid[] = "@(#)util.c 8.9 (Berkeley) 08/26/93"; 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 7723121Seric p = malloc((unsigned) sz); 78298Seric if (p == NULL) 79298Seric { 80298Seric syserr("Out of memory!!"); 8110685Seric abort(); 8210685Seric /* exit(EX_UNAVAILABLE); */ 83298Seric } 84298Seric return (p); 85298Seric } 86298Seric /* 873151Seric ** COPYPLIST -- copy list of pointers. 883151Seric ** 893151Seric ** This routine is the equivalent of newstr for lists of 903151Seric ** pointers. 913151Seric ** 923151Seric ** Parameters: 933151Seric ** list -- list of pointers to copy. 943151Seric ** Must be NULL terminated. 953151Seric ** copycont -- if TRUE, copy the contents of the vector 963151Seric ** (which must be a string) also. 973151Seric ** 983151Seric ** Returns: 993151Seric ** a copy of 'list'. 1003151Seric ** 1013151Seric ** Side Effects: 1023151Seric ** none. 1033151Seric */ 1043151Seric 1053151Seric char ** 1063151Seric copyplist(list, copycont) 1073151Seric char **list; 1083151Seric bool copycont; 1093151Seric { 1103151Seric register char **vp; 1113151Seric register char **newvp; 1123151Seric 1133151Seric for (vp = list; *vp != NULL; vp++) 1143151Seric continue; 1153151Seric 1163151Seric vp++; 1173151Seric 11816897Seric newvp = (char **) xalloc((int) (vp - list) * sizeof *vp); 11916897Seric bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp); 1203151Seric 1213151Seric if (copycont) 1223151Seric { 1233151Seric for (vp = newvp; *vp != NULL; vp++) 1243151Seric *vp = newstr(*vp); 1253151Seric } 1263151Seric 1273151Seric return (newvp); 1283151Seric } 1293151Seric /* 13058170Seric ** COPYQUEUE -- copy address queue. 13158170Seric ** 13258170Seric ** This routine is the equivalent of newstr for address queues 13358170Seric ** addresses marked with QDONTSEND aren't copied 13458170Seric ** 13558170Seric ** Parameters: 13658170Seric ** addr -- list of address structures to copy. 13758170Seric ** 13858170Seric ** Returns: 13958170Seric ** a copy of 'addr'. 14058170Seric ** 14158170Seric ** Side Effects: 14258170Seric ** none. 14358170Seric */ 14458170Seric 14558170Seric ADDRESS * 14658170Seric copyqueue(addr) 14758170Seric ADDRESS *addr; 14858170Seric { 14958170Seric register ADDRESS *newaddr; 15058170Seric ADDRESS *ret; 15158170Seric register ADDRESS **tail = &ret; 15258170Seric 15358170Seric while (addr != NULL) 15458170Seric { 15558170Seric if (!bitset(QDONTSEND, addr->q_flags)) 15658170Seric { 15758170Seric newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS)); 15858170Seric STRUCTCOPY(*addr, *newaddr); 15958170Seric *tail = newaddr; 16058170Seric tail = &newaddr->q_next; 16158170Seric } 16258170Seric addr = addr->q_next; 16358170Seric } 16458170Seric *tail = NULL; 16558170Seric 16658170Seric return ret; 16758170Seric } 16858170Seric /* 1693151Seric ** PRINTAV -- print argument vector. 1703151Seric ** 1713151Seric ** Parameters: 1723151Seric ** av -- argument vector. 1733151Seric ** 1743151Seric ** Returns: 1753151Seric ** none. 1763151Seric ** 1773151Seric ** Side Effects: 1783151Seric ** prints av. 1793151Seric */ 1803151Seric 1813151Seric printav(av) 1823151Seric register char **av; 1833151Seric { 1843151Seric while (*av != NULL) 1853151Seric { 1868063Seric if (tTd(0, 44)) 1878063Seric printf("\n\t%08x=", *av); 1888063Seric else 18923105Seric (void) putchar(' '); 1903151Seric xputs(*av++); 1913151Seric } 19223105Seric (void) putchar('\n'); 1933151Seric } 1943151Seric /* 1953151Seric ** LOWER -- turn letter into lower case. 1963151Seric ** 1973151Seric ** Parameters: 1983151Seric ** c -- character to turn into lower case. 1993151Seric ** 2003151Seric ** Returns: 2013151Seric ** c, in lower case. 2023151Seric ** 2033151Seric ** Side Effects: 2043151Seric ** none. 2053151Seric */ 2063151Seric 2073151Seric char 2083151Seric lower(c) 2093151Seric register char c; 2103151Seric { 21158050Seric return((isascii(c) && isupper(c)) ? tolower(c) : c); 2123151Seric } 2133151Seric /* 2143151Seric ** XPUTS -- put string doing control escapes. 2153151Seric ** 2163151Seric ** Parameters: 2173151Seric ** s -- string to put. 2183151Seric ** 2193151Seric ** Returns: 2203151Seric ** none. 2213151Seric ** 2223151Seric ** Side Effects: 2233151Seric ** output to stdout 2243151Seric */ 2253151Seric 2263151Seric xputs(s) 2273151Seric register char *s; 2283151Seric { 22958050Seric register int c; 23051781Seric register struct metamac *mp; 23151781Seric extern struct metamac MetaMacros[]; 2323151Seric 2338055Seric if (s == NULL) 2348055Seric { 2358055Seric printf("<null>"); 2368055Seric return; 2378055Seric } 23858050Seric while ((c = (*s++ & 0377)) != '\0') 2393151Seric { 2403151Seric if (!isascii(c)) 2413151Seric { 24258050Seric if (c == MATCHREPL || c == MACROEXPAND) 24358050Seric { 24458050Seric putchar('$'); 24558050Seric continue; 24658050Seric } 24758050Seric for (mp = MetaMacros; mp->metaname != '\0'; mp++) 24858050Seric { 24958050Seric if ((mp->metaval & 0377) == c) 25058050Seric { 25158050Seric printf("$%c", mp->metaname); 25258050Seric break; 25358050Seric } 25458050Seric } 25558050Seric if (mp->metaname != '\0') 25658050Seric continue; 25723105Seric (void) putchar('\\'); 2583151Seric c &= 0177; 2593151Seric } 26057589Seric if (isprint(c)) 2613151Seric { 26257589Seric putchar(c); 26357589Seric continue; 26457589Seric } 26552050Seric 26657589Seric /* wasn't a meta-macro -- find another way to print it */ 26757589Seric switch (c) 26857589Seric { 26957589Seric case '\0': 27057589Seric continue; 27152050Seric 27257589Seric case '\n': 27357589Seric c = 'n'; 27457589Seric break; 27552050Seric 27657589Seric case '\r': 27757589Seric c = 'r'; 27857589Seric break; 27952637Seric 28057589Seric case '\t': 28157589Seric c = 't'; 28257589Seric break; 28357589Seric 28457589Seric default: 28557589Seric (void) putchar('^'); 28657589Seric (void) putchar(c ^ 0100); 28757589Seric continue; 2883151Seric } 2893151Seric } 2904086Seric (void) fflush(stdout); 2913151Seric } 2923151Seric /* 2933151Seric ** MAKELOWER -- Translate a line into lower case 2943151Seric ** 2953151Seric ** Parameters: 2963151Seric ** p -- the string to translate. If NULL, return is 2973151Seric ** immediate. 2983151Seric ** 2993151Seric ** Returns: 3003151Seric ** none. 3013151Seric ** 3023151Seric ** Side Effects: 3033151Seric ** String pointed to by p is translated to lower case. 3043151Seric ** 3053151Seric ** Called By: 3063151Seric ** parse 3073151Seric */ 3083151Seric 3093151Seric makelower(p) 3103151Seric register char *p; 3113151Seric { 3123151Seric register char c; 3133151Seric 3143151Seric if (p == NULL) 3153151Seric return; 3163151Seric for (; (c = *p) != '\0'; p++) 3173151Seric if (isascii(c) && isupper(c)) 31833724Sbostic *p = tolower(c); 3193151Seric } 3204059Seric /* 3215196Seric ** BUILDFNAME -- build full name from gecos style entry. 3224375Seric ** 3235196Seric ** This routine interprets the strange entry that would appear 3245196Seric ** in the GECOS field of the password file. 3255196Seric ** 3264375Seric ** Parameters: 3275196Seric ** p -- name to build. 3285196Seric ** login -- the login name of this user (for &). 3295196Seric ** buf -- place to put the result. 3304375Seric ** 3314375Seric ** Returns: 3324375Seric ** none. 3334375Seric ** 3344375Seric ** Side Effects: 3354375Seric ** none. 3364375Seric */ 3374375Seric 33854984Seric buildfname(gecos, login, buf) 33954984Seric register char *gecos; 3405196Seric char *login; 3414375Seric char *buf; 3424375Seric { 34354984Seric register char *p; 3444375Seric register char *bp = buf; 34554984Seric int l; 3464375Seric 34754984Seric if (*gecos == '*') 34854984Seric gecos++; 34954984Seric 35057123Seric /* find length of final string */ 35154984Seric l = 0; 35254984Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 35354984Seric { 35454984Seric if (*p == '&') 35554984Seric l += strlen(login); 35654984Seric else 35754984Seric l++; 35854984Seric } 35954984Seric 36054984Seric /* now fill in buf */ 36155193Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 3624375Seric { 3634375Seric if (*p == '&') 3644375Seric { 3655196Seric (void) strcpy(bp, login); 3664375Seric *bp = toupper(*bp); 3674375Seric while (*bp != '\0') 3684375Seric bp++; 3694375Seric } 3704375Seric else 37155193Seric *bp++ = *p; 3724375Seric } 3734375Seric *bp = '\0'; 3744375Seric } 3754375Seric /* 3764538Seric ** SAFEFILE -- return true if a file exists and is safe for a user. 3774538Seric ** 3784538Seric ** Parameters: 3794538Seric ** fn -- filename to check. 38064083Seric ** uid -- user id to compare against. 38164083Seric ** gid -- group id to compare against. 38264083Seric ** uname -- user name to compare against (used for group 38364083Seric ** sets). 38463753Seric ** mustown -- to be safe, this uid must own the file. 3854538Seric ** mode -- mode bits that must match. 3864538Seric ** 3874538Seric ** Returns: 38858247Seric ** 0 if fn exists, is owned by uid, and matches mode. 38958247Seric ** An errno otherwise. The actual errno is cleared. 3904538Seric ** 3914538Seric ** Side Effects: 3924538Seric ** none. 3934538Seric */ 3944538Seric 39564083Seric #include <grp.h> 39664083Seric 39763581Seric #ifndef S_IXOTH 39863581Seric # define S_IXOTH (S_IEXEC >> 6) 39963581Seric #endif 40063581Seric 40164083Seric #ifndef S_IXGRP 40264083Seric # define S_IXGRP (S_IEXEC >> 3) 40364083Seric #endif 40464083Seric 40563753Seric #ifndef S_IXUSR 40663753Seric # define S_IXUSR (S_IEXEC) 40763753Seric #endif 40863753Seric 40958247Seric int 41064083Seric safefile(fn, uid, gid, uname, mustown, mode) 4114538Seric char *fn; 41255372Seric uid_t uid; 41364083Seric gid_t gid; 41464083Seric char *uname; 41563753Seric bool mustown; 4164538Seric int mode; 4174538Seric { 41863581Seric register char *p; 41964083Seric register struct group *gr = NULL; 4204538Seric struct stat stbuf; 4214538Seric 42263581Seric if (tTd(54, 4)) 42364083Seric printf("safefile(%s, uid=%d, gid=%d, mustown=%d, mode=%o):\n", 42464083Seric fn, uid, gid, mustown, mode); 42563581Seric errno = 0; 42663581Seric 42763581Seric for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/') 42863581Seric { 42963581Seric *p = '\0'; 43064083Seric if (stat(fn, &stbuf) < 0) 43164083Seric break; 432*64362Seric if (stbuf.st_uid == uid && bitset(S_IXUSR, stbuf.st_mode)) 433*64362Seric continue; 43464083Seric if (stbuf.st_gid == gid && bitset(S_IXGRP, stbuf.st_mode)) 43564083Seric continue; 43664084Seric #ifndef NO_GROUP_SET 43764083Seric if (uname != NULL && 43864083Seric ((gr != NULL && gr->gr_gid == stbuf.st_gid) || 43964083Seric (gr = getgrgid(stbuf.st_gid)) != NULL)) 44063581Seric { 44164083Seric register char **gp; 44263581Seric 44364083Seric for (gp = gr->gr_mem; *gp != NULL; gp++) 44464083Seric if (strcmp(*gp, uname) == 0) 44564083Seric break; 446*64362Seric if (*gp != NULL && bitset(S_IXGRP, stbuf.st_mode)) 447*64362Seric continue; 44863581Seric } 44964084Seric #endif 45064083Seric if (!bitset(S_IXOTH, stbuf.st_mode)) 45164083Seric break; 45263581Seric } 45364083Seric if (p != NULL) 45464083Seric { 45564083Seric int ret = errno; 45663581Seric 45764083Seric if (ret == 0) 45864083Seric ret = EACCES; 45964083Seric if (tTd(54, 4)) 46064083Seric printf("\t[dir %s] %s\n", fn, errstring(ret)); 46164083Seric *p = '/'; 46264083Seric return ret; 46364083Seric } 46464083Seric 46558247Seric if (stat(fn, &stbuf) < 0) 46658247Seric { 46758247Seric int ret = errno; 46858247Seric 46963581Seric if (tTd(54, 4)) 47064083Seric printf("\t%s\n", errstring(ret)); 47163581Seric 47258247Seric errno = 0; 47358247Seric return ret; 47458247Seric } 47564084Seric if (uid == 0) 47663581Seric mode >>= 6; 47764084Seric else if (stbuf.st_uid != uid) 47864084Seric { 47964084Seric mode >>= 3; 48064084Seric if (stbuf.st_gid == gid) 48164084Seric ; 48264084Seric #ifndef NO_GROUP_SET 48364084Seric else if (uname != NULL && 48464084Seric ((gr != NULL && gr->gr_gid == stbuf.st_gid) || 48564084Seric (gr = getgrgid(stbuf.st_gid)) != NULL)) 48664084Seric { 48764084Seric register char **gp; 48864084Seric 48964084Seric for (gp = gr->gr_mem; *gp != NULL; gp++) 49064084Seric if (strcmp(*gp, uname) == 0) 49164084Seric break; 49264084Seric if (*gp == NULL) 49364084Seric mode >>= 3; 49464084Seric } 49564084Seric #endif 49664084Seric else 49764084Seric mode >>= 3; 49864084Seric } 49963581Seric if (tTd(54, 4)) 50064084Seric printf("\t[uid %d, stat %o, mode %o] ", 50164084Seric stbuf.st_uid, stbuf.st_mode, mode); 50263753Seric if ((stbuf.st_uid == uid || uid == 0 || !mustown) && 50363581Seric (stbuf.st_mode & mode) == mode) 50463581Seric { 50563581Seric if (tTd(54, 4)) 50664083Seric printf("\tOK\n"); 50758247Seric return 0; 50863581Seric } 50963581Seric if (tTd(54, 4)) 51064083Seric printf("\tEACCES\n"); 51163581Seric return EACCES; 5124538Seric } 5134538Seric /* 5144557Seric ** FIXCRLF -- fix <CR><LF> in line. 5154557Seric ** 5164557Seric ** Looks for the <CR><LF> combination and turns it into the 5174557Seric ** UNIX canonical <NL> character. It only takes one line, 5184557Seric ** i.e., it is assumed that the first <NL> found is the end 5194557Seric ** of the line. 5204557Seric ** 5214557Seric ** Parameters: 5224557Seric ** line -- the line to fix. 5234557Seric ** stripnl -- if true, strip the newline also. 5244557Seric ** 5254557Seric ** Returns: 5264557Seric ** none. 5274557Seric ** 5284557Seric ** Side Effects: 5294557Seric ** line is changed in place. 5304557Seric */ 5314557Seric 5324557Seric fixcrlf(line, stripnl) 5334557Seric char *line; 5344557Seric bool stripnl; 5354557Seric { 5364557Seric register char *p; 5374557Seric 53856795Seric p = strchr(line, '\n'); 5394557Seric if (p == NULL) 5404557Seric return; 54136291Sbostic if (p > line && p[-1] == '\r') 5424557Seric p--; 5434557Seric if (!stripnl) 5444557Seric *p++ = '\n'; 5454557Seric *p = '\0'; 5464557Seric } 5474557Seric /* 5486890Seric ** DFOPEN -- determined file open 5496890Seric ** 5506890Seric ** This routine has the semantics of fopen, except that it will 5516890Seric ** keep trying a few times to make this happen. The idea is that 5526890Seric ** on very loaded systems, we may run out of resources (inodes, 5536890Seric ** whatever), so this tries to get around it. 5546890Seric */ 5556890Seric 55663753Seric #ifndef O_ACCMODE 55763753Seric # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) 55863753Seric #endif 55963753Seric 56059745Seric struct omodes 56159745Seric { 56259745Seric int mask; 56359745Seric int mode; 56459745Seric char *farg; 56559745Seric } OpenModes[] = 56659745Seric { 56759745Seric O_ACCMODE, O_RDONLY, "r", 56859745Seric O_ACCMODE|O_APPEND, O_WRONLY, "w", 56959745Seric O_ACCMODE|O_APPEND, O_WRONLY|O_APPEND, "a", 57059745Seric O_TRUNC, 0, "w+", 57159745Seric O_APPEND, O_APPEND, "a+", 57259745Seric 0, 0, "r+", 57359745Seric }; 57459745Seric 5756890Seric FILE * 57659745Seric dfopen(filename, omode, cmode) 5776890Seric char *filename; 57859745Seric int omode; 57959745Seric int cmode; 5806890Seric { 5816890Seric register int tries; 58259745Seric int fd; 58359745Seric register struct omodes *om; 58459431Seric struct stat st; 5856890Seric 58659745Seric for (om = OpenModes; om->mask != 0; om++) 58759745Seric if ((omode & om->mask) == om->mode) 58859745Seric break; 58959745Seric 5906890Seric for (tries = 0; tries < 10; tries++) 5916890Seric { 59225618Seric sleep((unsigned) (10 * tries)); 5936890Seric errno = 0; 59459745Seric fd = open(filename, omode, cmode); 59559745Seric if (fd >= 0) 5966890Seric break; 5979376Seric if (errno != ENFILE && errno != EINTR) 5989376Seric break; 5996890Seric } 60059745Seric if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode)) 60156328Seric { 60256328Seric int locktype; 60356328Seric 60456328Seric /* lock the file to avoid accidental conflicts */ 60559745Seric if ((omode & O_ACCMODE) != O_RDONLY) 60656328Seric locktype = LOCK_EX; 60756328Seric else 60856328Seric locktype = LOCK_SH; 60964335Seric (void) lockfile(fd, filename, NULL, locktype); 61056328Seric errno = 0; 61156328Seric } 61263787Seric if (fd < 0) 61363787Seric return NULL; 61463787Seric else 61563787Seric return fdopen(fd, om->farg); 6166890Seric } 6177124Seric /* 6187124Seric ** PUTLINE -- put a line like fputs obeying SMTP conventions 6197124Seric ** 6207753Seric ** This routine always guarantees outputing a newline (or CRLF, 6217753Seric ** as appropriate) at the end of the string. 6227753Seric ** 6237124Seric ** Parameters: 6247124Seric ** l -- line to put. 6257124Seric ** fp -- file to put it onto. 62610172Seric ** m -- the mailer used to control output. 6277124Seric ** 6287124Seric ** Returns: 6297124Seric ** none 6307124Seric ** 6317124Seric ** Side Effects: 6327124Seric ** output of l to fp. 6337124Seric */ 6347124Seric 63510172Seric putline(l, fp, m) 6367753Seric register char *l; 6377124Seric FILE *fp; 63810172Seric MAILER *m; 6397124Seric { 6407124Seric register char *p; 64147157Sbostic register char svchar; 6427124Seric 64311275Seric /* strip out 0200 bits -- these can look like TELNET protocol */ 64452106Seric if (bitnset(M_7BITS, m->m_flags)) 64511275Seric { 64661707Seric for (p = l; (svchar = *p) != '\0'; ++p) 64761707Seric if (bitset(0200, svchar)) 64847157Sbostic *p = svchar &~ 0200; 64911275Seric } 65011275Seric 6517753Seric do 6527124Seric { 6537753Seric /* find the end of the line */ 65456795Seric p = strchr(l, '\n'); 6557753Seric if (p == NULL) 6567753Seric p = &l[strlen(l)]; 6577124Seric 65863753Seric if (TrafficLogFile != NULL) 65963753Seric fprintf(TrafficLogFile, "%05d >>> ", getpid()); 66063753Seric 6617753Seric /* check for line overflow */ 66252106Seric while (m->m_linelimit > 0 && (p - l) > m->m_linelimit) 6637753Seric { 66452106Seric register char *q = &l[m->m_linelimit - 1]; 6657124Seric 6667753Seric svchar = *q; 6677753Seric *q = '\0'; 66810685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 66963753Seric { 67023105Seric (void) putc('.', fp); 67163753Seric if (TrafficLogFile != NULL) 67263753Seric (void) putc('.', TrafficLogFile); 67363753Seric } 6747753Seric fputs(l, fp); 67523105Seric (void) putc('!', fp); 67610326Seric fputs(m->m_eol, fp); 67763753Seric if (TrafficLogFile != NULL) 67863753Seric fprintf(TrafficLogFile, "%s!\n%05d >>> ", 67963753Seric l, getpid()); 6807753Seric *q = svchar; 6817753Seric l = q; 6827753Seric } 6837124Seric 6847753Seric /* output last part */ 68510685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 68663753Seric { 68723105Seric (void) putc('.', fp); 68863753Seric if (TrafficLogFile != NULL) 68963753Seric (void) putc('.', TrafficLogFile); 69063753Seric } 69163753Seric if (TrafficLogFile != NULL) 69263753Seric fprintf(TrafficLogFile, "%.*s\n", p - l, l); 69347157Sbostic for ( ; l < p; ++l) 69447157Sbostic (void) putc(*l, fp); 69510326Seric fputs(m->m_eol, fp); 6967753Seric if (*l == '\n') 69747157Sbostic ++l; 6987753Seric } while (l[0] != '\0'); 6997124Seric } 7007676Seric /* 7017676Seric ** XUNLINK -- unlink a file, doing logging as appropriate. 7027676Seric ** 7037676Seric ** Parameters: 7047676Seric ** f -- name of file to unlink. 7057676Seric ** 7067676Seric ** Returns: 7077676Seric ** none. 7087676Seric ** 7097676Seric ** Side Effects: 7107676Seric ** f is unlinked. 7117676Seric */ 7127676Seric 7137676Seric xunlink(f) 7147676Seric char *f; 7157676Seric { 7167676Seric register int i; 7177676Seric 7187676Seric # ifdef LOG 71958020Seric if (LogLevel > 98) 72058020Seric syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 72156795Seric # endif /* LOG */ 7227676Seric 7237676Seric i = unlink(f); 7247676Seric # ifdef LOG 72558020Seric if (i < 0 && LogLevel > 97) 7267942Seric syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 72756795Seric # endif /* LOG */ 7287676Seric } 7297685Seric /* 73058680Seric ** XFCLOSE -- close a file, doing logging as appropriate. 73158680Seric ** 73258680Seric ** Parameters: 73358680Seric ** fp -- file pointer for the file to close 73458680Seric ** a, b -- miscellaneous crud to print for debugging 73558680Seric ** 73658680Seric ** Returns: 73758680Seric ** none. 73858680Seric ** 73958680Seric ** Side Effects: 74058680Seric ** fp is closed. 74158680Seric */ 74258680Seric 74358680Seric xfclose(fp, a, b) 74458680Seric FILE *fp; 74558680Seric char *a, *b; 74658680Seric { 74758796Seric if (tTd(53, 99)) 74858680Seric printf("xfclose(%x) %s %s\n", fp, a, b); 74958796Seric if (fclose(fp) < 0 && tTd(53, 99)) 75058680Seric printf("xfclose FAILURE: %s\n", errstring(errno)); 75158680Seric } 75258680Seric /* 75314885Seric ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 7547685Seric ** 7557685Seric ** Parameters: 7567685Seric ** buf -- place to put the input line. 7577685Seric ** siz -- size of buf. 7587685Seric ** fp -- file to read from. 75957384Seric ** timeout -- the timeout before error occurs. 76061093Seric ** during -- what we are trying to read (for error messages). 7617685Seric ** 7627685Seric ** Returns: 76315533Seric ** NULL on error (including timeout). This will also leave 76415533Seric ** buf containing a null string. 7657685Seric ** buf otherwise. 7667685Seric ** 7677685Seric ** Side Effects: 7687685Seric ** none. 7697685Seric */ 7707685Seric 77114885Seric static jmp_buf CtxReadTimeout; 77263937Seric static int readtimeout(); 7737685Seric 7747685Seric char * 77561093Seric sfgets(buf, siz, fp, timeout, during) 7767685Seric char *buf; 7777685Seric int siz; 7787685Seric FILE *fp; 77957384Seric time_t timeout; 78061093Seric char *during; 7817685Seric { 7827942Seric register EVENT *ev = NULL; 7837685Seric register char *p; 7847685Seric 78514885Seric /* set the timeout */ 78657384Seric if (timeout != 0) 78714885Seric { 78814885Seric if (setjmp(CtxReadTimeout) != 0) 78914885Seric { 79036233Skarels # ifdef LOG 79136230Skarels syslog(LOG_NOTICE, 79261093Seric "timeout waiting for input from %s during %s\n", 79361093Seric CurHostName? CurHostName: "local", during); 79436233Skarels # endif 79536230Skarels errno = 0; 79661093Seric usrerr("451 timeout waiting for input during %s", 79761093Seric during); 79819037Seric buf[0] = '\0'; 79963753Seric #ifdef XDEBUG 80063753Seric checkfd012(during); 80163753Seric #endif 80214885Seric return (NULL); 80314885Seric } 80457384Seric ev = setevent(timeout, readtimeout, 0); 80514885Seric } 80614885Seric 80714885Seric /* try to read */ 80815533Seric p = NULL; 80915533Seric while (p == NULL && !feof(fp) && !ferror(fp)) 8107942Seric { 8117942Seric errno = 0; 8127942Seric p = fgets(buf, siz, fp); 81315533Seric if (errno == EINTR) 81415533Seric clearerr(fp); 81515533Seric } 81614885Seric 81714885Seric /* clear the event if it has not sprung */ 8187685Seric clrevent(ev); 81914885Seric 82014885Seric /* clean up the books and exit */ 8218055Seric LineNumber++; 82215533Seric if (p == NULL) 82316880Seric { 82415533Seric buf[0] = '\0'; 82563753Seric if (TrafficLogFile != NULL) 82663753Seric fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid()); 82716880Seric return (NULL); 82816880Seric } 82963753Seric if (TrafficLogFile != NULL) 83063753Seric fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf); 83159709Seric if (SevenBit) 83252106Seric for (p = buf; *p != '\0'; p++) 83352106Seric *p &= ~0200; 83416880Seric return (buf); 8357685Seric } 8367685Seric 8377685Seric static 8387685Seric readtimeout() 8397685Seric { 84014885Seric longjmp(CtxReadTimeout, 1); 8417685Seric } 8427786Seric /* 8437786Seric ** FGETFOLDED -- like fgets, but know about folded lines. 8447786Seric ** 8457786Seric ** Parameters: 8467786Seric ** buf -- place to put result. 8477786Seric ** n -- bytes available. 8487786Seric ** f -- file to read from. 8497786Seric ** 8507786Seric ** Returns: 85157135Seric ** input line(s) on success, NULL on error or EOF. 85257135Seric ** This will normally be buf -- unless the line is too 85357135Seric ** long, when it will be xalloc()ed. 8547786Seric ** 8557786Seric ** Side Effects: 8567786Seric ** buf gets lines from f, with continuation lines (lines 8577786Seric ** with leading white space) appended. CRLF's are mapped 8587786Seric ** into single newlines. Any trailing NL is stripped. 8597786Seric */ 8607786Seric 8617786Seric char * 8627786Seric fgetfolded(buf, n, f) 8637786Seric char *buf; 8647786Seric register int n; 8657786Seric FILE *f; 8667786Seric { 8677786Seric register char *p = buf; 86857135Seric char *bp = buf; 8697786Seric register int i; 8707786Seric 8717786Seric n--; 87217350Seric while ((i = getc(f)) != EOF) 8737786Seric { 87417350Seric if (i == '\r') 87517350Seric { 87617350Seric i = getc(f); 87717350Seric if (i != '\n') 87817350Seric { 87917350Seric if (i != EOF) 88023105Seric (void) ungetc(i, f); 88117350Seric i = '\r'; 88217350Seric } 88317350Seric } 88457135Seric if (--n <= 0) 88557135Seric { 88657135Seric /* allocate new space */ 88757135Seric char *nbp; 88857135Seric int nn; 88957135Seric 89057135Seric nn = (p - bp); 89157232Seric if (nn < MEMCHUNKSIZE) 89257135Seric nn *= 2; 89357135Seric else 89457232Seric nn += MEMCHUNKSIZE; 89557135Seric nbp = xalloc(nn); 89657135Seric bcopy(bp, nbp, p - bp); 89757135Seric p = &nbp[p - bp]; 89857135Seric if (bp != buf) 89957135Seric free(bp); 90057135Seric bp = nbp; 90157135Seric n = nn - (p - bp); 90257135Seric } 90357135Seric *p++ = i; 90417350Seric if (i == '\n') 90517350Seric { 90617350Seric LineNumber++; 90717350Seric i = getc(f); 90817350Seric if (i != EOF) 90923105Seric (void) ungetc(i, f); 91017350Seric if (i != ' ' && i != '\t') 91152647Seric break; 91217350Seric } 9137786Seric } 91457135Seric if (p == bp) 91552647Seric return (NULL); 91652647Seric *--p = '\0'; 91757135Seric return (bp); 9187786Seric } 9197860Seric /* 9207886Seric ** CURTIME -- return current time. 9217886Seric ** 9227886Seric ** Parameters: 9237886Seric ** none. 9247886Seric ** 9257886Seric ** Returns: 9267886Seric ** the current time. 9277886Seric ** 9287886Seric ** Side Effects: 9297886Seric ** none. 9307886Seric */ 9317886Seric 9327886Seric time_t 9337886Seric curtime() 9347886Seric { 9357886Seric auto time_t t; 9367886Seric 9377886Seric (void) time(&t); 9387886Seric return (t); 9397886Seric } 9408264Seric /* 9418264Seric ** ATOBOOL -- convert a string representation to boolean. 9428264Seric ** 9438264Seric ** Defaults to "TRUE" 9448264Seric ** 9458264Seric ** Parameters: 9468264Seric ** s -- string to convert. Takes "tTyY" as true, 9478264Seric ** others as false. 9488264Seric ** 9498264Seric ** Returns: 9508264Seric ** A boolean representation of the string. 9518264Seric ** 9528264Seric ** Side Effects: 9538264Seric ** none. 9548264Seric */ 9558264Seric 9568264Seric bool 9578264Seric atobool(s) 9588264Seric register char *s; 9598264Seric { 96063833Seric if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL) 9618264Seric return (TRUE); 9628264Seric return (FALSE); 9638264Seric } 9649048Seric /* 9659048Seric ** ATOOCT -- convert a string representation to octal. 9669048Seric ** 9679048Seric ** Parameters: 9689048Seric ** s -- string to convert. 9699048Seric ** 9709048Seric ** Returns: 9719048Seric ** An integer representing the string interpreted as an 9729048Seric ** octal number. 9739048Seric ** 9749048Seric ** Side Effects: 9759048Seric ** none. 9769048Seric */ 9779048Seric 9789048Seric atooct(s) 9799048Seric register char *s; 9809048Seric { 9819048Seric register int i = 0; 9829048Seric 9839048Seric while (*s >= '0' && *s <= '7') 9849048Seric i = (i << 3) | (*s++ - '0'); 9859048Seric return (i); 9869048Seric } 9879376Seric /* 9889376Seric ** WAITFOR -- wait for a particular process id. 9899376Seric ** 9909376Seric ** Parameters: 9919376Seric ** pid -- process id to wait for. 9929376Seric ** 9939376Seric ** Returns: 9949376Seric ** status of pid. 9959376Seric ** -1 if pid never shows up. 9969376Seric ** 9979376Seric ** Side Effects: 9989376Seric ** none. 9999376Seric */ 10009376Seric 10019376Seric waitfor(pid) 10029376Seric int pid; 10039376Seric { 10049376Seric auto int st; 10059376Seric int i; 10069376Seric 10079376Seric do 10089376Seric { 10099376Seric errno = 0; 10109376Seric i = wait(&st); 10119376Seric } while ((i >= 0 || errno == EINTR) && i != pid); 10129376Seric if (i < 0) 10139376Seric st = -1; 10149376Seric return (st); 10159376Seric } 10169376Seric /* 101710685Seric ** BITINTERSECT -- tell if two bitmaps intersect 101810685Seric ** 101910685Seric ** Parameters: 102010685Seric ** a, b -- the bitmaps in question 102110685Seric ** 102210685Seric ** Returns: 102310685Seric ** TRUE if they have a non-null intersection 102410685Seric ** FALSE otherwise 102510685Seric ** 102610685Seric ** Side Effects: 102710685Seric ** none. 102810685Seric */ 102910685Seric 103010685Seric bool 103110685Seric bitintersect(a, b) 103210685Seric BITMAP a; 103310685Seric BITMAP b; 103410685Seric { 103510685Seric int i; 103610685Seric 103710685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 103810685Seric if ((a[i] & b[i]) != 0) 103910685Seric return (TRUE); 104010685Seric return (FALSE); 104110685Seric } 104210685Seric /* 104310685Seric ** BITZEROP -- tell if a bitmap is all zero 104410685Seric ** 104510685Seric ** Parameters: 104610685Seric ** map -- the bit map to check 104710685Seric ** 104810685Seric ** Returns: 104910685Seric ** TRUE if map is all zero. 105010685Seric ** FALSE if there are any bits set in map. 105110685Seric ** 105210685Seric ** Side Effects: 105310685Seric ** none. 105410685Seric */ 105510685Seric 105610685Seric bool 105710685Seric bitzerop(map) 105810685Seric BITMAP map; 105910685Seric { 106010685Seric int i; 106110685Seric 106210685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 106310685Seric if (map[i] != 0) 106410685Seric return (FALSE); 106510685Seric return (TRUE); 106610685Seric } 106758247Seric /* 106858318Seric ** STRCONTAINEDIN -- tell if one string is contained in another 106958318Seric ** 107058318Seric ** Parameters: 107158318Seric ** a -- possible substring. 107258318Seric ** b -- possible superstring. 107358318Seric ** 107458318Seric ** Returns: 107558318Seric ** TRUE if a is contained in b. 107658318Seric ** FALSE otherwise. 107758318Seric */ 107858318Seric 107958318Seric bool 108058318Seric strcontainedin(a, b) 108158318Seric register char *a; 108258318Seric register char *b; 108358318Seric { 108458318Seric int l; 108558318Seric 108658318Seric l = strlen(a); 108758318Seric for (;;) 108858318Seric { 108958318Seric b = strchr(b, a[0]); 109058318Seric if (b == NULL) 109158318Seric return FALSE; 109258318Seric if (strncmp(a, b, l) == 0) 109358318Seric return TRUE; 109458318Seric b++; 109558318Seric } 109658318Seric } 109763753Seric /* 109863753Seric ** CHECKFD012 -- check low numbered file descriptors 109963753Seric ** 110063753Seric ** File descriptors 0, 1, and 2 should be open at all times. 110163753Seric ** This routine verifies that, and fixes it if not true. 110263753Seric ** 110363753Seric ** Parameters: 110463753Seric ** where -- a tag printed if the assertion failed 110563753Seric ** 110663753Seric ** Returns: 110763753Seric ** none 110863753Seric */ 110963753Seric 111063753Seric checkfd012(where) 111163753Seric char *where; 111263753Seric { 111363753Seric #ifdef XDEBUG 111463753Seric register int i; 111563753Seric struct stat stbuf; 111663753Seric 111763753Seric for (i = 0; i < 3; i++) 111863753Seric { 111963753Seric if (fstat(i, &stbuf) < 0) 112063753Seric { 112163753Seric /* oops.... */ 112263753Seric int fd; 112363753Seric 112463753Seric syserr("%s: fd %d not open", where, i); 112563753Seric fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666); 112663753Seric if (fd != i) 112763753Seric { 112863753Seric (void) dup2(fd, i); 112963753Seric (void) close(fd); 113063753Seric } 113163753Seric } 113263753Seric } 113363937Seric #endif /* XDEBUG */ 113463753Seric } 1135