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*63753Seric static char sccsid[] = "@(#)util.c 8.2 (Berkeley) 07/11/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. 381*63753Seric ** 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 396*63753Seric #ifndef S_IXUSR 397*63753Seric # define S_IXUSR (S_IEXEC) 398*63753Seric #endif 399*63753Seric 40058247Seric int 401*63753Seric safefile(fn, uid, mustown, mode) 4024538Seric char *fn; 40355372Seric uid_t uid; 404*63753Seric bool mustown; 4054538Seric int mode; 4064538Seric { 40763581Seric register char *p; 4084538Seric struct stat stbuf; 4094538Seric 41063581Seric if (tTd(54, 4)) 411*63753Seric 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'; 417*63753Seric if (stat(fn, &stbuf) < 0 || 418*63753Seric !bitset(stbuf.st_uid == uid ? S_IXUSR : S_IXOTH, 419*63753Seric 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 } 442*63753Seric 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); 446*63753Seric 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 500*63753Seric #ifndef O_ACCMODE 501*63753Seric # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) 502*63753Seric #endif 503*63753Seric 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 } 55659745Seric return fdopen(fd, om->farg); 5576890Seric } 5587124Seric /* 5597124Seric ** PUTLINE -- put a line like fputs obeying SMTP conventions 5607124Seric ** 5617753Seric ** This routine always guarantees outputing a newline (or CRLF, 5627753Seric ** as appropriate) at the end of the string. 5637753Seric ** 5647124Seric ** Parameters: 5657124Seric ** l -- line to put. 5667124Seric ** fp -- file to put it onto. 56710172Seric ** m -- the mailer used to control output. 5687124Seric ** 5697124Seric ** Returns: 5707124Seric ** none 5717124Seric ** 5727124Seric ** Side Effects: 5737124Seric ** output of l to fp. 5747124Seric */ 5757124Seric 57610172Seric putline(l, fp, m) 5777753Seric register char *l; 5787124Seric FILE *fp; 57910172Seric MAILER *m; 5807124Seric { 5817124Seric register char *p; 58247157Sbostic register char svchar; 5837124Seric 58411275Seric /* strip out 0200 bits -- these can look like TELNET protocol */ 58552106Seric if (bitnset(M_7BITS, m->m_flags)) 58611275Seric { 58761707Seric for (p = l; (svchar = *p) != '\0'; ++p) 58861707Seric if (bitset(0200, svchar)) 58947157Sbostic *p = svchar &~ 0200; 59011275Seric } 59111275Seric 5927753Seric do 5937124Seric { 5947753Seric /* find the end of the line */ 59556795Seric p = strchr(l, '\n'); 5967753Seric if (p == NULL) 5977753Seric p = &l[strlen(l)]; 5987124Seric 599*63753Seric if (TrafficLogFile != NULL) 600*63753Seric fprintf(TrafficLogFile, "%05d >>> ", getpid()); 601*63753Seric 6027753Seric /* check for line overflow */ 60352106Seric while (m->m_linelimit > 0 && (p - l) > m->m_linelimit) 6047753Seric { 60552106Seric register char *q = &l[m->m_linelimit - 1]; 6067124Seric 6077753Seric svchar = *q; 6087753Seric *q = '\0'; 60910685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 610*63753Seric { 61123105Seric (void) putc('.', fp); 612*63753Seric if (TrafficLogFile != NULL) 613*63753Seric (void) putc('.', TrafficLogFile); 614*63753Seric } 6157753Seric fputs(l, fp); 61623105Seric (void) putc('!', fp); 61710326Seric fputs(m->m_eol, fp); 618*63753Seric if (TrafficLogFile != NULL) 619*63753Seric fprintf(TrafficLogFile, "%s!\n%05d >>> ", 620*63753Seric l, getpid()); 6217753Seric *q = svchar; 6227753Seric l = q; 6237753Seric } 6247124Seric 6257753Seric /* output last part */ 62610685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 627*63753Seric { 62823105Seric (void) putc('.', fp); 629*63753Seric if (TrafficLogFile != NULL) 630*63753Seric (void) putc('.', TrafficLogFile); 631*63753Seric } 632*63753Seric if (TrafficLogFile != NULL) 633*63753Seric fprintf(TrafficLogFile, "%.*s\n", p - l, l); 63447157Sbostic for ( ; l < p; ++l) 63547157Sbostic (void) putc(*l, fp); 63610326Seric fputs(m->m_eol, fp); 6377753Seric if (*l == '\n') 63847157Sbostic ++l; 6397753Seric } while (l[0] != '\0'); 6407124Seric } 6417676Seric /* 6427676Seric ** XUNLINK -- unlink a file, doing logging as appropriate. 6437676Seric ** 6447676Seric ** Parameters: 6457676Seric ** f -- name of file to unlink. 6467676Seric ** 6477676Seric ** Returns: 6487676Seric ** none. 6497676Seric ** 6507676Seric ** Side Effects: 6517676Seric ** f is unlinked. 6527676Seric */ 6537676Seric 6547676Seric xunlink(f) 6557676Seric char *f; 6567676Seric { 6577676Seric register int i; 6587676Seric 6597676Seric # ifdef LOG 66058020Seric if (LogLevel > 98) 66158020Seric syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 66256795Seric # endif /* LOG */ 6637676Seric 6647676Seric i = unlink(f); 6657676Seric # ifdef LOG 66658020Seric if (i < 0 && LogLevel > 97) 6677942Seric syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 66856795Seric # endif /* LOG */ 6697676Seric } 6707685Seric /* 67158680Seric ** XFCLOSE -- close a file, doing logging as appropriate. 67258680Seric ** 67358680Seric ** Parameters: 67458680Seric ** fp -- file pointer for the file to close 67558680Seric ** a, b -- miscellaneous crud to print for debugging 67658680Seric ** 67758680Seric ** Returns: 67858680Seric ** none. 67958680Seric ** 68058680Seric ** Side Effects: 68158680Seric ** fp is closed. 68258680Seric */ 68358680Seric 68458680Seric xfclose(fp, a, b) 68558680Seric FILE *fp; 68658680Seric char *a, *b; 68758680Seric { 68858796Seric if (tTd(53, 99)) 68958680Seric printf("xfclose(%x) %s %s\n", fp, a, b); 69058796Seric if (fclose(fp) < 0 && tTd(53, 99)) 69158680Seric printf("xfclose FAILURE: %s\n", errstring(errno)); 69258680Seric } 69358680Seric /* 69414885Seric ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 6957685Seric ** 6967685Seric ** Parameters: 6977685Seric ** buf -- place to put the input line. 6987685Seric ** siz -- size of buf. 6997685Seric ** fp -- file to read from. 70057384Seric ** timeout -- the timeout before error occurs. 70161093Seric ** during -- what we are trying to read (for error messages). 7027685Seric ** 7037685Seric ** Returns: 70415533Seric ** NULL on error (including timeout). This will also leave 70515533Seric ** buf containing a null string. 7067685Seric ** buf otherwise. 7077685Seric ** 7087685Seric ** Side Effects: 7097685Seric ** none. 7107685Seric */ 7117685Seric 71214885Seric static jmp_buf CtxReadTimeout; 7137685Seric 7147685Seric char * 71561093Seric sfgets(buf, siz, fp, timeout, during) 7167685Seric char *buf; 7177685Seric int siz; 7187685Seric FILE *fp; 71957384Seric time_t timeout; 72061093Seric char *during; 7217685Seric { 7227942Seric register EVENT *ev = NULL; 7237685Seric register char *p; 72446928Sbostic static int readtimeout(); 7257685Seric 72614885Seric /* set the timeout */ 72757384Seric if (timeout != 0) 72814885Seric { 72914885Seric if (setjmp(CtxReadTimeout) != 0) 73014885Seric { 73136233Skarels # ifdef LOG 73236230Skarels syslog(LOG_NOTICE, 73361093Seric "timeout waiting for input from %s during %s\n", 73461093Seric CurHostName? CurHostName: "local", during); 73536233Skarels # endif 73636230Skarels errno = 0; 73761093Seric usrerr("451 timeout waiting for input during %s", 73861093Seric during); 73919037Seric buf[0] = '\0'; 740*63753Seric #ifdef XDEBUG 741*63753Seric checkfd012(during); 742*63753Seric #endif 74314885Seric return (NULL); 74414885Seric } 74557384Seric ev = setevent(timeout, readtimeout, 0); 74614885Seric } 74714885Seric 74814885Seric /* try to read */ 74915533Seric p = NULL; 75015533Seric while (p == NULL && !feof(fp) && !ferror(fp)) 7517942Seric { 7527942Seric errno = 0; 7537942Seric p = fgets(buf, siz, fp); 75415533Seric if (errno == EINTR) 75515533Seric clearerr(fp); 75615533Seric } 75714885Seric 75814885Seric /* clear the event if it has not sprung */ 7597685Seric clrevent(ev); 76014885Seric 76114885Seric /* clean up the books and exit */ 7628055Seric LineNumber++; 76315533Seric if (p == NULL) 76416880Seric { 76515533Seric buf[0] = '\0'; 766*63753Seric if (TrafficLogFile != NULL) 767*63753Seric fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid()); 76816880Seric return (NULL); 76916880Seric } 770*63753Seric if (TrafficLogFile != NULL) 771*63753Seric fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf); 77259709Seric if (SevenBit) 77352106Seric for (p = buf; *p != '\0'; p++) 77452106Seric *p &= ~0200; 77516880Seric return (buf); 7767685Seric } 7777685Seric 7787685Seric static 7797685Seric readtimeout() 7807685Seric { 78114885Seric longjmp(CtxReadTimeout, 1); 7827685Seric } 7837786Seric /* 7847786Seric ** FGETFOLDED -- like fgets, but know about folded lines. 7857786Seric ** 7867786Seric ** Parameters: 7877786Seric ** buf -- place to put result. 7887786Seric ** n -- bytes available. 7897786Seric ** f -- file to read from. 7907786Seric ** 7917786Seric ** Returns: 79257135Seric ** input line(s) on success, NULL on error or EOF. 79357135Seric ** This will normally be buf -- unless the line is too 79457135Seric ** long, when it will be xalloc()ed. 7957786Seric ** 7967786Seric ** Side Effects: 7977786Seric ** buf gets lines from f, with continuation lines (lines 7987786Seric ** with leading white space) appended. CRLF's are mapped 7997786Seric ** into single newlines. Any trailing NL is stripped. 8007786Seric */ 8017786Seric 8027786Seric char * 8037786Seric fgetfolded(buf, n, f) 8047786Seric char *buf; 8057786Seric register int n; 8067786Seric FILE *f; 8077786Seric { 8087786Seric register char *p = buf; 80957135Seric char *bp = buf; 8107786Seric register int i; 8117786Seric 8127786Seric n--; 81317350Seric while ((i = getc(f)) != EOF) 8147786Seric { 81517350Seric if (i == '\r') 81617350Seric { 81717350Seric i = getc(f); 81817350Seric if (i != '\n') 81917350Seric { 82017350Seric if (i != EOF) 82123105Seric (void) ungetc(i, f); 82217350Seric i = '\r'; 82317350Seric } 82417350Seric } 82557135Seric if (--n <= 0) 82657135Seric { 82757135Seric /* allocate new space */ 82857135Seric char *nbp; 82957135Seric int nn; 83057135Seric 83157135Seric nn = (p - bp); 83257232Seric if (nn < MEMCHUNKSIZE) 83357135Seric nn *= 2; 83457135Seric else 83557232Seric nn += MEMCHUNKSIZE; 83657135Seric nbp = xalloc(nn); 83757135Seric bcopy(bp, nbp, p - bp); 83857135Seric p = &nbp[p - bp]; 83957135Seric if (bp != buf) 84057135Seric free(bp); 84157135Seric bp = nbp; 84257135Seric n = nn - (p - bp); 84357135Seric } 84457135Seric *p++ = i; 84517350Seric if (i == '\n') 84617350Seric { 84717350Seric LineNumber++; 84817350Seric i = getc(f); 84917350Seric if (i != EOF) 85023105Seric (void) ungetc(i, f); 85117350Seric if (i != ' ' && i != '\t') 85252647Seric break; 85317350Seric } 8547786Seric } 85557135Seric if (p == bp) 85652647Seric return (NULL); 85752647Seric *--p = '\0'; 85857135Seric return (bp); 8597786Seric } 8607860Seric /* 8617886Seric ** CURTIME -- return current time. 8627886Seric ** 8637886Seric ** Parameters: 8647886Seric ** none. 8657886Seric ** 8667886Seric ** Returns: 8677886Seric ** the current time. 8687886Seric ** 8697886Seric ** Side Effects: 8707886Seric ** none. 8717886Seric */ 8727886Seric 8737886Seric time_t 8747886Seric curtime() 8757886Seric { 8767886Seric auto time_t t; 8777886Seric 8787886Seric (void) time(&t); 8797886Seric return (t); 8807886Seric } 8818264Seric /* 8828264Seric ** ATOBOOL -- convert a string representation to boolean. 8838264Seric ** 8848264Seric ** Defaults to "TRUE" 8858264Seric ** 8868264Seric ** Parameters: 8878264Seric ** s -- string to convert. Takes "tTyY" as true, 8888264Seric ** others as false. 8898264Seric ** 8908264Seric ** Returns: 8918264Seric ** A boolean representation of the string. 8928264Seric ** 8938264Seric ** Side Effects: 8948264Seric ** none. 8958264Seric */ 8968264Seric 8978264Seric bool 8988264Seric atobool(s) 8998264Seric register char *s; 9008264Seric { 90156795Seric if (*s == '\0' || strchr("tTyY", *s) != NULL) 9028264Seric return (TRUE); 9038264Seric return (FALSE); 9048264Seric } 9059048Seric /* 9069048Seric ** ATOOCT -- convert a string representation to octal. 9079048Seric ** 9089048Seric ** Parameters: 9099048Seric ** s -- string to convert. 9109048Seric ** 9119048Seric ** Returns: 9129048Seric ** An integer representing the string interpreted as an 9139048Seric ** octal number. 9149048Seric ** 9159048Seric ** Side Effects: 9169048Seric ** none. 9179048Seric */ 9189048Seric 9199048Seric atooct(s) 9209048Seric register char *s; 9219048Seric { 9229048Seric register int i = 0; 9239048Seric 9249048Seric while (*s >= '0' && *s <= '7') 9259048Seric i = (i << 3) | (*s++ - '0'); 9269048Seric return (i); 9279048Seric } 9289376Seric /* 9299376Seric ** WAITFOR -- wait for a particular process id. 9309376Seric ** 9319376Seric ** Parameters: 9329376Seric ** pid -- process id to wait for. 9339376Seric ** 9349376Seric ** Returns: 9359376Seric ** status of pid. 9369376Seric ** -1 if pid never shows up. 9379376Seric ** 9389376Seric ** Side Effects: 9399376Seric ** none. 9409376Seric */ 9419376Seric 9429376Seric waitfor(pid) 9439376Seric int pid; 9449376Seric { 9459376Seric auto int st; 9469376Seric int i; 9479376Seric 9489376Seric do 9499376Seric { 9509376Seric errno = 0; 9519376Seric i = wait(&st); 9529376Seric } while ((i >= 0 || errno == EINTR) && i != pid); 9539376Seric if (i < 0) 9549376Seric st = -1; 9559376Seric return (st); 9569376Seric } 9579376Seric /* 95810685Seric ** BITINTERSECT -- tell if two bitmaps intersect 95910685Seric ** 96010685Seric ** Parameters: 96110685Seric ** a, b -- the bitmaps in question 96210685Seric ** 96310685Seric ** Returns: 96410685Seric ** TRUE if they have a non-null intersection 96510685Seric ** FALSE otherwise 96610685Seric ** 96710685Seric ** Side Effects: 96810685Seric ** none. 96910685Seric */ 97010685Seric 97110685Seric bool 97210685Seric bitintersect(a, b) 97310685Seric BITMAP a; 97410685Seric BITMAP b; 97510685Seric { 97610685Seric int i; 97710685Seric 97810685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 97910685Seric if ((a[i] & b[i]) != 0) 98010685Seric return (TRUE); 98110685Seric return (FALSE); 98210685Seric } 98310685Seric /* 98410685Seric ** BITZEROP -- tell if a bitmap is all zero 98510685Seric ** 98610685Seric ** Parameters: 98710685Seric ** map -- the bit map to check 98810685Seric ** 98910685Seric ** Returns: 99010685Seric ** TRUE if map is all zero. 99110685Seric ** FALSE if there are any bits set in map. 99210685Seric ** 99310685Seric ** Side Effects: 99410685Seric ** none. 99510685Seric */ 99610685Seric 99710685Seric bool 99810685Seric bitzerop(map) 99910685Seric BITMAP map; 100010685Seric { 100110685Seric int i; 100210685Seric 100310685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 100410685Seric if (map[i] != 0) 100510685Seric return (FALSE); 100610685Seric return (TRUE); 100710685Seric } 100858247Seric /* 100958318Seric ** STRCONTAINEDIN -- tell if one string is contained in another 101058318Seric ** 101158318Seric ** Parameters: 101258318Seric ** a -- possible substring. 101358318Seric ** b -- possible superstring. 101458318Seric ** 101558318Seric ** Returns: 101658318Seric ** TRUE if a is contained in b. 101758318Seric ** FALSE otherwise. 101858318Seric */ 101958318Seric 102058318Seric bool 102158318Seric strcontainedin(a, b) 102258318Seric register char *a; 102358318Seric register char *b; 102458318Seric { 102558318Seric int l; 102658318Seric 102758318Seric l = strlen(a); 102858318Seric for (;;) 102958318Seric { 103058318Seric b = strchr(b, a[0]); 103158318Seric if (b == NULL) 103258318Seric return FALSE; 103358318Seric if (strncmp(a, b, l) == 0) 103458318Seric return TRUE; 103558318Seric b++; 103658318Seric } 103758318Seric } 1038*63753Seric /* 1039*63753Seric ** CHECKFD012 -- check low numbered file descriptors 1040*63753Seric ** 1041*63753Seric ** File descriptors 0, 1, and 2 should be open at all times. 1042*63753Seric ** This routine verifies that, and fixes it if not true. 1043*63753Seric ** 1044*63753Seric ** Parameters: 1045*63753Seric ** where -- a tag printed if the assertion failed 1046*63753Seric ** 1047*63753Seric ** Returns: 1048*63753Seric ** none 1049*63753Seric */ 1050*63753Seric 1051*63753Seric checkfd012(where) 1052*63753Seric char *where; 1053*63753Seric { 1054*63753Seric #ifdef XDEBUG 1055*63753Seric register int i; 1056*63753Seric struct stat stbuf; 1057*63753Seric 1058*63753Seric for (i = 0; i < 3; i++) 1059*63753Seric { 1060*63753Seric if (fstat(i, &stbuf) < 0) 1061*63753Seric { 1062*63753Seric /* oops.... */ 1063*63753Seric int fd; 1064*63753Seric 1065*63753Seric syserr("%s: fd %d not open", where, i); 1066*63753Seric fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666); 1067*63753Seric if (fd != i) 1068*63753Seric { 1069*63753Seric (void) dup2(fd, i); 1070*63753Seric (void) close(fd); 1071*63753Seric } 1072*63753Seric } 1073*63753Seric } 1074*63753Seric #endif XDEBUG 1075*63753Seric } 1076