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*63833Seric static char sccsid[] = "@(#)util.c 8.4 (Berkeley) 07/16/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. 3804538Seric ** uid -- uid to compare against. 38163753Seric ** mustown -- to be safe, this uid must own the file. 3824538Seric ** mode -- mode bits that must match. 3834538Seric ** 3844538Seric ** Returns: 38558247Seric ** 0 if fn exists, is owned by uid, and matches mode. 38658247Seric ** An errno otherwise. The actual errno is cleared. 3874538Seric ** 3884538Seric ** Side Effects: 3894538Seric ** none. 3904538Seric */ 3914538Seric 39263581Seric #ifndef S_IXOTH 39363581Seric # define S_IXOTH (S_IEXEC >> 6) 39463581Seric #endif 39563581Seric 39663753Seric #ifndef S_IXUSR 39763753Seric # define S_IXUSR (S_IEXEC) 39863753Seric #endif 39963753Seric 40058247Seric int 40163753Seric safefile(fn, uid, mustown, mode) 4024538Seric char *fn; 40355372Seric uid_t uid; 40463753Seric bool mustown; 4054538Seric int mode; 4064538Seric { 40763581Seric register char *p; 4084538Seric struct stat stbuf; 4094538Seric 41063581Seric if (tTd(54, 4)) 41163753Seric printf("safefile(%s, %d, %d, %o): ", fn, uid, mustown, mode); 41263581Seric errno = 0; 41363581Seric 41463581Seric for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/') 41563581Seric { 41663581Seric *p = '\0'; 41763753Seric if (stat(fn, &stbuf) < 0 || 41863753Seric !bitset(stbuf.st_uid == uid ? S_IXUSR : S_IXOTH, 41963753Seric stbuf.st_mode)) 42063581Seric { 42163581Seric int ret = errno; 42263581Seric 42363581Seric if (ret == 0) 42463581Seric ret = EACCES; 42563581Seric if (tTd(54, 4)) 42663581Seric printf("[dir %s] %s\n", fn, errstring(ret)); 42763581Seric *p = '/'; 42863581Seric return ret; 42963581Seric } 43063581Seric } 43163581Seric 43258247Seric if (stat(fn, &stbuf) < 0) 43358247Seric { 43458247Seric int ret = errno; 43558247Seric 43663581Seric if (tTd(54, 4)) 43763581Seric printf("%s\n", errstring(ret)); 43863581Seric 43958247Seric errno = 0; 44058247Seric return ret; 44158247Seric } 44263753Seric if (stbuf.st_uid != uid || uid == 0 || !mustown) 44363581Seric mode >>= 6; 44463581Seric if (tTd(54, 4)) 44563581Seric printf("[uid %d, stat %o] ", stbuf.st_uid, stbuf.st_mode); 44663753Seric if ((stbuf.st_uid == uid || uid == 0 || !mustown) && 44763581Seric (stbuf.st_mode & mode) == mode) 44863581Seric { 44963581Seric if (tTd(54, 4)) 45063581Seric printf("OK\n"); 45158247Seric return 0; 45263581Seric } 45363581Seric if (tTd(54, 4)) 45463581Seric printf("EACCES\n"); 45563581Seric return EACCES; 4564538Seric } 4574538Seric /* 4584557Seric ** FIXCRLF -- fix <CR><LF> in line. 4594557Seric ** 4604557Seric ** Looks for the <CR><LF> combination and turns it into the 4614557Seric ** UNIX canonical <NL> character. It only takes one line, 4624557Seric ** i.e., it is assumed that the first <NL> found is the end 4634557Seric ** of the line. 4644557Seric ** 4654557Seric ** Parameters: 4664557Seric ** line -- the line to fix. 4674557Seric ** stripnl -- if true, strip the newline also. 4684557Seric ** 4694557Seric ** Returns: 4704557Seric ** none. 4714557Seric ** 4724557Seric ** Side Effects: 4734557Seric ** line is changed in place. 4744557Seric */ 4754557Seric 4764557Seric fixcrlf(line, stripnl) 4774557Seric char *line; 4784557Seric bool stripnl; 4794557Seric { 4804557Seric register char *p; 4814557Seric 48256795Seric p = strchr(line, '\n'); 4834557Seric if (p == NULL) 4844557Seric return; 48536291Sbostic if (p > line && p[-1] == '\r') 4864557Seric p--; 4874557Seric if (!stripnl) 4884557Seric *p++ = '\n'; 4894557Seric *p = '\0'; 4904557Seric } 4914557Seric /* 4926890Seric ** DFOPEN -- determined file open 4936890Seric ** 4946890Seric ** This routine has the semantics of fopen, except that it will 4956890Seric ** keep trying a few times to make this happen. The idea is that 4966890Seric ** on very loaded systems, we may run out of resources (inodes, 4976890Seric ** whatever), so this tries to get around it. 4986890Seric */ 4996890Seric 50063753Seric #ifndef O_ACCMODE 50163753Seric # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) 50263753Seric #endif 50363753Seric 50459745Seric struct omodes 50559745Seric { 50659745Seric int mask; 50759745Seric int mode; 50859745Seric char *farg; 50959745Seric } OpenModes[] = 51059745Seric { 51159745Seric O_ACCMODE, O_RDONLY, "r", 51259745Seric O_ACCMODE|O_APPEND, O_WRONLY, "w", 51359745Seric O_ACCMODE|O_APPEND, O_WRONLY|O_APPEND, "a", 51459745Seric O_TRUNC, 0, "w+", 51559745Seric O_APPEND, O_APPEND, "a+", 51659745Seric 0, 0, "r+", 51759745Seric }; 51859745Seric 5196890Seric FILE * 52059745Seric dfopen(filename, omode, cmode) 5216890Seric char *filename; 52259745Seric int omode; 52359745Seric int cmode; 5246890Seric { 5256890Seric register int tries; 52659745Seric int fd; 52759745Seric register struct omodes *om; 52859431Seric struct stat st; 5296890Seric 53059745Seric for (om = OpenModes; om->mask != 0; om++) 53159745Seric if ((omode & om->mask) == om->mode) 53259745Seric break; 53359745Seric 5346890Seric for (tries = 0; tries < 10; tries++) 5356890Seric { 53625618Seric sleep((unsigned) (10 * tries)); 5376890Seric errno = 0; 53859745Seric fd = open(filename, omode, cmode); 53959745Seric if (fd >= 0) 5406890Seric break; 5419376Seric if (errno != ENFILE && errno != EINTR) 5429376Seric break; 5436890Seric } 54459745Seric if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode)) 54556328Seric { 54656328Seric int locktype; 54756328Seric 54856328Seric /* lock the file to avoid accidental conflicts */ 54959745Seric if ((omode & O_ACCMODE) != O_RDONLY) 55056328Seric locktype = LOCK_EX; 55156328Seric else 55256328Seric locktype = LOCK_SH; 55359745Seric (void) lockfile(fd, filename, locktype); 55456328Seric errno = 0; 55556328Seric } 55663787Seric if (fd < 0) 55763787Seric return NULL; 55863787Seric else 55963787Seric return fdopen(fd, om->farg); 5606890Seric } 5617124Seric /* 5627124Seric ** PUTLINE -- put a line like fputs obeying SMTP conventions 5637124Seric ** 5647753Seric ** This routine always guarantees outputing a newline (or CRLF, 5657753Seric ** as appropriate) at the end of the string. 5667753Seric ** 5677124Seric ** Parameters: 5687124Seric ** l -- line to put. 5697124Seric ** fp -- file to put it onto. 57010172Seric ** m -- the mailer used to control output. 5717124Seric ** 5727124Seric ** Returns: 5737124Seric ** none 5747124Seric ** 5757124Seric ** Side Effects: 5767124Seric ** output of l to fp. 5777124Seric */ 5787124Seric 57910172Seric putline(l, fp, m) 5807753Seric register char *l; 5817124Seric FILE *fp; 58210172Seric MAILER *m; 5837124Seric { 5847124Seric register char *p; 58547157Sbostic register char svchar; 5867124Seric 58711275Seric /* strip out 0200 bits -- these can look like TELNET protocol */ 58852106Seric if (bitnset(M_7BITS, m->m_flags)) 58911275Seric { 59061707Seric for (p = l; (svchar = *p) != '\0'; ++p) 59161707Seric if (bitset(0200, svchar)) 59247157Sbostic *p = svchar &~ 0200; 59311275Seric } 59411275Seric 5957753Seric do 5967124Seric { 5977753Seric /* find the end of the line */ 59856795Seric p = strchr(l, '\n'); 5997753Seric if (p == NULL) 6007753Seric p = &l[strlen(l)]; 6017124Seric 60263753Seric if (TrafficLogFile != NULL) 60363753Seric fprintf(TrafficLogFile, "%05d >>> ", getpid()); 60463753Seric 6057753Seric /* check for line overflow */ 60652106Seric while (m->m_linelimit > 0 && (p - l) > m->m_linelimit) 6077753Seric { 60852106Seric register char *q = &l[m->m_linelimit - 1]; 6097124Seric 6107753Seric svchar = *q; 6117753Seric *q = '\0'; 61210685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 61363753Seric { 61423105Seric (void) putc('.', fp); 61563753Seric if (TrafficLogFile != NULL) 61663753Seric (void) putc('.', TrafficLogFile); 61763753Seric } 6187753Seric fputs(l, fp); 61923105Seric (void) putc('!', fp); 62010326Seric fputs(m->m_eol, fp); 62163753Seric if (TrafficLogFile != NULL) 62263753Seric fprintf(TrafficLogFile, "%s!\n%05d >>> ", 62363753Seric l, getpid()); 6247753Seric *q = svchar; 6257753Seric l = q; 6267753Seric } 6277124Seric 6287753Seric /* output last part */ 62910685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 63063753Seric { 63123105Seric (void) putc('.', fp); 63263753Seric if (TrafficLogFile != NULL) 63363753Seric (void) putc('.', TrafficLogFile); 63463753Seric } 63563753Seric if (TrafficLogFile != NULL) 63663753Seric fprintf(TrafficLogFile, "%.*s\n", p - l, l); 63747157Sbostic for ( ; l < p; ++l) 63847157Sbostic (void) putc(*l, fp); 63910326Seric fputs(m->m_eol, fp); 6407753Seric if (*l == '\n') 64147157Sbostic ++l; 6427753Seric } while (l[0] != '\0'); 6437124Seric } 6447676Seric /* 6457676Seric ** XUNLINK -- unlink a file, doing logging as appropriate. 6467676Seric ** 6477676Seric ** Parameters: 6487676Seric ** f -- name of file to unlink. 6497676Seric ** 6507676Seric ** Returns: 6517676Seric ** none. 6527676Seric ** 6537676Seric ** Side Effects: 6547676Seric ** f is unlinked. 6557676Seric */ 6567676Seric 6577676Seric xunlink(f) 6587676Seric char *f; 6597676Seric { 6607676Seric register int i; 6617676Seric 6627676Seric # ifdef LOG 66358020Seric if (LogLevel > 98) 66458020Seric syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 66556795Seric # endif /* LOG */ 6667676Seric 6677676Seric i = unlink(f); 6687676Seric # ifdef LOG 66958020Seric if (i < 0 && LogLevel > 97) 6707942Seric syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 67156795Seric # endif /* LOG */ 6727676Seric } 6737685Seric /* 67458680Seric ** XFCLOSE -- close a file, doing logging as appropriate. 67558680Seric ** 67658680Seric ** Parameters: 67758680Seric ** fp -- file pointer for the file to close 67858680Seric ** a, b -- miscellaneous crud to print for debugging 67958680Seric ** 68058680Seric ** Returns: 68158680Seric ** none. 68258680Seric ** 68358680Seric ** Side Effects: 68458680Seric ** fp is closed. 68558680Seric */ 68658680Seric 68758680Seric xfclose(fp, a, b) 68858680Seric FILE *fp; 68958680Seric char *a, *b; 69058680Seric { 69158796Seric if (tTd(53, 99)) 69258680Seric printf("xfclose(%x) %s %s\n", fp, a, b); 69358796Seric if (fclose(fp) < 0 && tTd(53, 99)) 69458680Seric printf("xfclose FAILURE: %s\n", errstring(errno)); 69558680Seric } 69658680Seric /* 69714885Seric ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 6987685Seric ** 6997685Seric ** Parameters: 7007685Seric ** buf -- place to put the input line. 7017685Seric ** siz -- size of buf. 7027685Seric ** fp -- file to read from. 70357384Seric ** timeout -- the timeout before error occurs. 70461093Seric ** during -- what we are trying to read (for error messages). 7057685Seric ** 7067685Seric ** Returns: 70715533Seric ** NULL on error (including timeout). This will also leave 70815533Seric ** buf containing a null string. 7097685Seric ** buf otherwise. 7107685Seric ** 7117685Seric ** Side Effects: 7127685Seric ** none. 7137685Seric */ 7147685Seric 71514885Seric static jmp_buf CtxReadTimeout; 7167685Seric 7177685Seric char * 71861093Seric sfgets(buf, siz, fp, timeout, during) 7197685Seric char *buf; 7207685Seric int siz; 7217685Seric FILE *fp; 72257384Seric time_t timeout; 72361093Seric char *during; 7247685Seric { 7257942Seric register EVENT *ev = NULL; 7267685Seric register char *p; 72746928Sbostic static int readtimeout(); 7287685Seric 72914885Seric /* set the timeout */ 73057384Seric if (timeout != 0) 73114885Seric { 73214885Seric if (setjmp(CtxReadTimeout) != 0) 73314885Seric { 73436233Skarels # ifdef LOG 73536230Skarels syslog(LOG_NOTICE, 73661093Seric "timeout waiting for input from %s during %s\n", 73761093Seric CurHostName? CurHostName: "local", during); 73836233Skarels # endif 73936230Skarels errno = 0; 74061093Seric usrerr("451 timeout waiting for input during %s", 74161093Seric during); 74219037Seric buf[0] = '\0'; 74363753Seric #ifdef XDEBUG 74463753Seric checkfd012(during); 74563753Seric #endif 74614885Seric return (NULL); 74714885Seric } 74857384Seric ev = setevent(timeout, readtimeout, 0); 74914885Seric } 75014885Seric 75114885Seric /* try to read */ 75215533Seric p = NULL; 75315533Seric while (p == NULL && !feof(fp) && !ferror(fp)) 7547942Seric { 7557942Seric errno = 0; 7567942Seric p = fgets(buf, siz, fp); 75715533Seric if (errno == EINTR) 75815533Seric clearerr(fp); 75915533Seric } 76014885Seric 76114885Seric /* clear the event if it has not sprung */ 7627685Seric clrevent(ev); 76314885Seric 76414885Seric /* clean up the books and exit */ 7658055Seric LineNumber++; 76615533Seric if (p == NULL) 76716880Seric { 76815533Seric buf[0] = '\0'; 76963753Seric if (TrafficLogFile != NULL) 77063753Seric fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid()); 77116880Seric return (NULL); 77216880Seric } 77363753Seric if (TrafficLogFile != NULL) 77463753Seric fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf); 77559709Seric if (SevenBit) 77652106Seric for (p = buf; *p != '\0'; p++) 77752106Seric *p &= ~0200; 77816880Seric return (buf); 7797685Seric } 7807685Seric 7817685Seric static 7827685Seric readtimeout() 7837685Seric { 78414885Seric longjmp(CtxReadTimeout, 1); 7857685Seric } 7867786Seric /* 7877786Seric ** FGETFOLDED -- like fgets, but know about folded lines. 7887786Seric ** 7897786Seric ** Parameters: 7907786Seric ** buf -- place to put result. 7917786Seric ** n -- bytes available. 7927786Seric ** f -- file to read from. 7937786Seric ** 7947786Seric ** Returns: 79557135Seric ** input line(s) on success, NULL on error or EOF. 79657135Seric ** This will normally be buf -- unless the line is too 79757135Seric ** long, when it will be xalloc()ed. 7987786Seric ** 7997786Seric ** Side Effects: 8007786Seric ** buf gets lines from f, with continuation lines (lines 8017786Seric ** with leading white space) appended. CRLF's are mapped 8027786Seric ** into single newlines. Any trailing NL is stripped. 8037786Seric */ 8047786Seric 8057786Seric char * 8067786Seric fgetfolded(buf, n, f) 8077786Seric char *buf; 8087786Seric register int n; 8097786Seric FILE *f; 8107786Seric { 8117786Seric register char *p = buf; 81257135Seric char *bp = buf; 8137786Seric register int i; 8147786Seric 8157786Seric n--; 81617350Seric while ((i = getc(f)) != EOF) 8177786Seric { 81817350Seric if (i == '\r') 81917350Seric { 82017350Seric i = getc(f); 82117350Seric if (i != '\n') 82217350Seric { 82317350Seric if (i != EOF) 82423105Seric (void) ungetc(i, f); 82517350Seric i = '\r'; 82617350Seric } 82717350Seric } 82857135Seric if (--n <= 0) 82957135Seric { 83057135Seric /* allocate new space */ 83157135Seric char *nbp; 83257135Seric int nn; 83357135Seric 83457135Seric nn = (p - bp); 83557232Seric if (nn < MEMCHUNKSIZE) 83657135Seric nn *= 2; 83757135Seric else 83857232Seric nn += MEMCHUNKSIZE; 83957135Seric nbp = xalloc(nn); 84057135Seric bcopy(bp, nbp, p - bp); 84157135Seric p = &nbp[p - bp]; 84257135Seric if (bp != buf) 84357135Seric free(bp); 84457135Seric bp = nbp; 84557135Seric n = nn - (p - bp); 84657135Seric } 84757135Seric *p++ = i; 84817350Seric if (i == '\n') 84917350Seric { 85017350Seric LineNumber++; 85117350Seric i = getc(f); 85217350Seric if (i != EOF) 85323105Seric (void) ungetc(i, f); 85417350Seric if (i != ' ' && i != '\t') 85552647Seric break; 85617350Seric } 8577786Seric } 85857135Seric if (p == bp) 85952647Seric return (NULL); 86052647Seric *--p = '\0'; 86157135Seric return (bp); 8627786Seric } 8637860Seric /* 8647886Seric ** CURTIME -- return current time. 8657886Seric ** 8667886Seric ** Parameters: 8677886Seric ** none. 8687886Seric ** 8697886Seric ** Returns: 8707886Seric ** the current time. 8717886Seric ** 8727886Seric ** Side Effects: 8737886Seric ** none. 8747886Seric */ 8757886Seric 8767886Seric time_t 8777886Seric curtime() 8787886Seric { 8797886Seric auto time_t t; 8807886Seric 8817886Seric (void) time(&t); 8827886Seric return (t); 8837886Seric } 8848264Seric /* 8858264Seric ** ATOBOOL -- convert a string representation to boolean. 8868264Seric ** 8878264Seric ** Defaults to "TRUE" 8888264Seric ** 8898264Seric ** Parameters: 8908264Seric ** s -- string to convert. Takes "tTyY" as true, 8918264Seric ** others as false. 8928264Seric ** 8938264Seric ** Returns: 8948264Seric ** A boolean representation of the string. 8958264Seric ** 8968264Seric ** Side Effects: 8978264Seric ** none. 8988264Seric */ 8998264Seric 9008264Seric bool 9018264Seric atobool(s) 9028264Seric register char *s; 9038264Seric { 904*63833Seric if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL) 9058264Seric return (TRUE); 9068264Seric return (FALSE); 9078264Seric } 9089048Seric /* 9099048Seric ** ATOOCT -- convert a string representation to octal. 9109048Seric ** 9119048Seric ** Parameters: 9129048Seric ** s -- string to convert. 9139048Seric ** 9149048Seric ** Returns: 9159048Seric ** An integer representing the string interpreted as an 9169048Seric ** octal number. 9179048Seric ** 9189048Seric ** Side Effects: 9199048Seric ** none. 9209048Seric */ 9219048Seric 9229048Seric atooct(s) 9239048Seric register char *s; 9249048Seric { 9259048Seric register int i = 0; 9269048Seric 9279048Seric while (*s >= '0' && *s <= '7') 9289048Seric i = (i << 3) | (*s++ - '0'); 9299048Seric return (i); 9309048Seric } 9319376Seric /* 9329376Seric ** WAITFOR -- wait for a particular process id. 9339376Seric ** 9349376Seric ** Parameters: 9359376Seric ** pid -- process id to wait for. 9369376Seric ** 9379376Seric ** Returns: 9389376Seric ** status of pid. 9399376Seric ** -1 if pid never shows up. 9409376Seric ** 9419376Seric ** Side Effects: 9429376Seric ** none. 9439376Seric */ 9449376Seric 9459376Seric waitfor(pid) 9469376Seric int pid; 9479376Seric { 9489376Seric auto int st; 9499376Seric int i; 9509376Seric 9519376Seric do 9529376Seric { 9539376Seric errno = 0; 9549376Seric i = wait(&st); 9559376Seric } while ((i >= 0 || errno == EINTR) && i != pid); 9569376Seric if (i < 0) 9579376Seric st = -1; 9589376Seric return (st); 9599376Seric } 9609376Seric /* 96110685Seric ** BITINTERSECT -- tell if two bitmaps intersect 96210685Seric ** 96310685Seric ** Parameters: 96410685Seric ** a, b -- the bitmaps in question 96510685Seric ** 96610685Seric ** Returns: 96710685Seric ** TRUE if they have a non-null intersection 96810685Seric ** FALSE otherwise 96910685Seric ** 97010685Seric ** Side Effects: 97110685Seric ** none. 97210685Seric */ 97310685Seric 97410685Seric bool 97510685Seric bitintersect(a, b) 97610685Seric BITMAP a; 97710685Seric BITMAP b; 97810685Seric { 97910685Seric int i; 98010685Seric 98110685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 98210685Seric if ((a[i] & b[i]) != 0) 98310685Seric return (TRUE); 98410685Seric return (FALSE); 98510685Seric } 98610685Seric /* 98710685Seric ** BITZEROP -- tell if a bitmap is all zero 98810685Seric ** 98910685Seric ** Parameters: 99010685Seric ** map -- the bit map to check 99110685Seric ** 99210685Seric ** Returns: 99310685Seric ** TRUE if map is all zero. 99410685Seric ** FALSE if there are any bits set in map. 99510685Seric ** 99610685Seric ** Side Effects: 99710685Seric ** none. 99810685Seric */ 99910685Seric 100010685Seric bool 100110685Seric bitzerop(map) 100210685Seric BITMAP map; 100310685Seric { 100410685Seric int i; 100510685Seric 100610685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 100710685Seric if (map[i] != 0) 100810685Seric return (FALSE); 100910685Seric return (TRUE); 101010685Seric } 101158247Seric /* 101258318Seric ** STRCONTAINEDIN -- tell if one string is contained in another 101358318Seric ** 101458318Seric ** Parameters: 101558318Seric ** a -- possible substring. 101658318Seric ** b -- possible superstring. 101758318Seric ** 101858318Seric ** Returns: 101958318Seric ** TRUE if a is contained in b. 102058318Seric ** FALSE otherwise. 102158318Seric */ 102258318Seric 102358318Seric bool 102458318Seric strcontainedin(a, b) 102558318Seric register char *a; 102658318Seric register char *b; 102758318Seric { 102858318Seric int l; 102958318Seric 103058318Seric l = strlen(a); 103158318Seric for (;;) 103258318Seric { 103358318Seric b = strchr(b, a[0]); 103458318Seric if (b == NULL) 103558318Seric return FALSE; 103658318Seric if (strncmp(a, b, l) == 0) 103758318Seric return TRUE; 103858318Seric b++; 103958318Seric } 104058318Seric } 104163753Seric /* 104263753Seric ** CHECKFD012 -- check low numbered file descriptors 104363753Seric ** 104463753Seric ** File descriptors 0, 1, and 2 should be open at all times. 104563753Seric ** This routine verifies that, and fixes it if not true. 104663753Seric ** 104763753Seric ** Parameters: 104863753Seric ** where -- a tag printed if the assertion failed 104963753Seric ** 105063753Seric ** Returns: 105163753Seric ** none 105263753Seric */ 105363753Seric 105463753Seric checkfd012(where) 105563753Seric char *where; 105663753Seric { 105763753Seric #ifdef XDEBUG 105863753Seric register int i; 105963753Seric struct stat stbuf; 106063753Seric 106163753Seric for (i = 0; i < 3; i++) 106263753Seric { 106363753Seric if (fstat(i, &stbuf) < 0) 106463753Seric { 106563753Seric /* oops.... */ 106663753Seric int fd; 106763753Seric 106863753Seric syserr("%s: fd %d not open", where, i); 106963753Seric fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666); 107063753Seric if (fd != i) 107163753Seric { 107263753Seric (void) dup2(fd, i); 107363753Seric (void) close(fd); 107463753Seric } 107563753Seric } 107663753Seric } 107763753Seric #endif XDEBUG 107863753Seric } 1079