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*65006Seric static char sccsid[] = "@(#)util.c 8.18 (Berkeley) 12/01/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: 332*65006Seric ** none. 3334375Seric ** 3344375Seric ** Side Effects: 3354375Seric ** none. 3364375Seric */ 3374375Seric 33854984Seric buildfname(gecos, login, buf) 339*65006Seric register char *gecos; 340*65006Seric char *login; 341*65006Seric char *buf; 3424375Seric { 343*65006Seric register char *p; 344*65006Seric register char *bp = buf; 345*65006Seric int l; 3464375Seric 347*65006Seric if (*gecos == '*') 348*65006Seric gecos++; 349*65006Seric 350*65006Seric /* find length of final string */ 351*65006Seric l = 0; 352*65006Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 35354984Seric { 354*65006Seric if (*p == '&') 355*65006Seric l += strlen(login); 356*65006Seric else 357*65006Seric l++; 35854984Seric } 359*65006Seric 360*65006Seric /* now fill in buf */ 361*65006Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 3624375Seric { 363*65006Seric if (*p == '&') 3644375Seric { 365*65006Seric (void) strcpy(bp, login); 366*65006Seric *bp = toupper(*bp); 367*65006Seric while (*bp != '\0') 368*65006Seric bp++; 3694375Seric } 370*65006Seric else 371*65006Seric *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). 38464944Seric ** flags -- modifiers: 38564944Seric ** SF_MUSTOWN -- "uid" must own this file. 38664944Seric ** SF_NOSLINK -- file cannot be a symbolic link. 3874538Seric ** mode -- mode bits that must match. 3884538Seric ** 3894538Seric ** Returns: 39058247Seric ** 0 if fn exists, is owned by uid, and matches mode. 39158247Seric ** An errno otherwise. The actual errno is cleared. 3924538Seric ** 3934538Seric ** Side Effects: 3944538Seric ** none. 3954538Seric */ 3964538Seric 39764083Seric #include <grp.h> 39864083Seric 39963581Seric #ifndef S_IXOTH 40063581Seric # define S_IXOTH (S_IEXEC >> 6) 40163581Seric #endif 40263581Seric 40364083Seric #ifndef S_IXGRP 40464083Seric # define S_IXGRP (S_IEXEC >> 3) 40564083Seric #endif 40664083Seric 40763753Seric #ifndef S_IXUSR 40863753Seric # define S_IXUSR (S_IEXEC) 40963753Seric #endif 41063753Seric 41158247Seric int 41264944Seric safefile(fn, uid, gid, uname, flags, mode) 4134538Seric char *fn; 41455372Seric uid_t uid; 41564083Seric gid_t gid; 41664083Seric char *uname; 41764944Seric int flags; 4184538Seric int mode; 4194538Seric { 42063581Seric register char *p; 42164083Seric register struct group *gr = NULL; 4224538Seric struct stat stbuf; 4234538Seric 42463581Seric if (tTd(54, 4)) 42564944Seric printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n", 42664944Seric fn, uid, gid, flags, mode); 42763581Seric errno = 0; 42863581Seric 42963581Seric for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/') 43063581Seric { 43163581Seric *p = '\0'; 43264083Seric if (stat(fn, &stbuf) < 0) 43364083Seric break; 43464362Seric if (stbuf.st_uid == uid && bitset(S_IXUSR, stbuf.st_mode)) 43564362Seric continue; 43664083Seric if (stbuf.st_gid == gid && bitset(S_IXGRP, stbuf.st_mode)) 43764083Seric continue; 43864084Seric #ifndef NO_GROUP_SET 43964083Seric if (uname != NULL && 44064083Seric ((gr != NULL && gr->gr_gid == stbuf.st_gid) || 44164083Seric (gr = getgrgid(stbuf.st_gid)) != NULL)) 44263581Seric { 44364083Seric register char **gp; 44463581Seric 44564083Seric for (gp = gr->gr_mem; *gp != NULL; gp++) 44664083Seric if (strcmp(*gp, uname) == 0) 44764083Seric break; 44864362Seric if (*gp != NULL && bitset(S_IXGRP, stbuf.st_mode)) 44964362Seric continue; 45063581Seric } 45164084Seric #endif 45264083Seric if (!bitset(S_IXOTH, stbuf.st_mode)) 45364083Seric break; 45463581Seric } 45564083Seric if (p != NULL) 45664083Seric { 45764083Seric int ret = errno; 45863581Seric 45964083Seric if (ret == 0) 46064083Seric ret = EACCES; 46164083Seric if (tTd(54, 4)) 46264083Seric printf("\t[dir %s] %s\n", fn, errstring(ret)); 46364083Seric *p = '/'; 46464083Seric return ret; 46564083Seric } 46664083Seric 46764944Seric #ifdef HASLSTAT 46864944Seric if ((bitset(SF_NOSLINK, flags) ? lstat(fn, &stbuf) 46964944Seric : stat(fn, &stbuf)) < 0) 47064944Seric #else 47158247Seric if (stat(fn, &stbuf) < 0) 47264944Seric #endif 47358247Seric { 47458247Seric int ret = errno; 47558247Seric 47663581Seric if (tTd(54, 4)) 47764083Seric printf("\t%s\n", errstring(ret)); 47863581Seric 47958247Seric errno = 0; 48058247Seric return ret; 48158247Seric } 48264944Seric 48364944Seric #ifdef S_ISLNK 48464944Seric if (bitset(SF_NOSLINK, flags) && S_ISLNK(stbuf.st_mode)) 48564944Seric { 48664944Seric if (tTd(54, 4)) 48764944Seric printf("\t[mode %o]\tEPERM\n"); 48864944Seric return EPERM; 48964944Seric } 49064944Seric #endif 49164944Seric 49264084Seric if (uid == 0) 49363581Seric mode >>= 6; 49464084Seric else if (stbuf.st_uid != uid) 49564084Seric { 49664084Seric mode >>= 3; 49764084Seric if (stbuf.st_gid == gid) 49864084Seric ; 49964084Seric #ifndef NO_GROUP_SET 50064084Seric else if (uname != NULL && 50164084Seric ((gr != NULL && gr->gr_gid == stbuf.st_gid) || 50264084Seric (gr = getgrgid(stbuf.st_gid)) != NULL)) 50364084Seric { 50464084Seric register char **gp; 50564084Seric 50664084Seric for (gp = gr->gr_mem; *gp != NULL; gp++) 50764084Seric if (strcmp(*gp, uname) == 0) 50864084Seric break; 50964084Seric if (*gp == NULL) 51064084Seric mode >>= 3; 51164084Seric } 51264084Seric #endif 51364084Seric else 51464084Seric mode >>= 3; 51564084Seric } 51663581Seric if (tTd(54, 4)) 51764084Seric printf("\t[uid %d, stat %o, mode %o] ", 51864084Seric stbuf.st_uid, stbuf.st_mode, mode); 51964944Seric if ((stbuf.st_uid == uid || stbuf.st_uid == 0 || 52064944Seric !bitset(SF_MUSTOWN, flags)) && 52163581Seric (stbuf.st_mode & mode) == mode) 52263581Seric { 52363581Seric if (tTd(54, 4)) 52464083Seric printf("\tOK\n"); 52558247Seric return 0; 52663581Seric } 52763581Seric if (tTd(54, 4)) 52864083Seric printf("\tEACCES\n"); 52963581Seric return EACCES; 5304538Seric } 5314538Seric /* 5324557Seric ** FIXCRLF -- fix <CR><LF> in line. 5334557Seric ** 5344557Seric ** Looks for the <CR><LF> combination and turns it into the 5354557Seric ** UNIX canonical <NL> character. It only takes one line, 5364557Seric ** i.e., it is assumed that the first <NL> found is the end 5374557Seric ** of the line. 5384557Seric ** 5394557Seric ** Parameters: 5404557Seric ** line -- the line to fix. 5414557Seric ** stripnl -- if true, strip the newline also. 5424557Seric ** 5434557Seric ** Returns: 5444557Seric ** none. 5454557Seric ** 5464557Seric ** Side Effects: 5474557Seric ** line is changed in place. 5484557Seric */ 5494557Seric 5504557Seric fixcrlf(line, stripnl) 5514557Seric char *line; 5524557Seric bool stripnl; 5534557Seric { 5544557Seric register char *p; 5554557Seric 55656795Seric p = strchr(line, '\n'); 5574557Seric if (p == NULL) 5584557Seric return; 55936291Sbostic if (p > line && p[-1] == '\r') 5604557Seric p--; 5614557Seric if (!stripnl) 5624557Seric *p++ = '\n'; 5634557Seric *p = '\0'; 5644557Seric } 5654557Seric /* 5666890Seric ** DFOPEN -- determined file open 5676890Seric ** 5686890Seric ** This routine has the semantics of fopen, except that it will 5696890Seric ** keep trying a few times to make this happen. The idea is that 5706890Seric ** on very loaded systems, we may run out of resources (inodes, 5716890Seric ** whatever), so this tries to get around it. 5726890Seric */ 5736890Seric 57463753Seric #ifndef O_ACCMODE 57563753Seric # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) 57663753Seric #endif 57763753Seric 57859745Seric struct omodes 57959745Seric { 58059745Seric int mask; 58159745Seric int mode; 58259745Seric char *farg; 58359745Seric } OpenModes[] = 58459745Seric { 58559745Seric O_ACCMODE, O_RDONLY, "r", 58659745Seric O_ACCMODE|O_APPEND, O_WRONLY, "w", 58759745Seric O_ACCMODE|O_APPEND, O_WRONLY|O_APPEND, "a", 58859745Seric O_TRUNC, 0, "w+", 58959745Seric O_APPEND, O_APPEND, "a+", 59059745Seric 0, 0, "r+", 59159745Seric }; 59259745Seric 5936890Seric FILE * 59459745Seric dfopen(filename, omode, cmode) 5956890Seric char *filename; 59659745Seric int omode; 59759745Seric int cmode; 5986890Seric { 5996890Seric register int tries; 60059745Seric int fd; 60159745Seric register struct omodes *om; 60259431Seric struct stat st; 6036890Seric 60459745Seric for (om = OpenModes; om->mask != 0; om++) 60559745Seric if ((omode & om->mask) == om->mode) 60659745Seric break; 60759745Seric 6086890Seric for (tries = 0; tries < 10; tries++) 6096890Seric { 61025618Seric sleep((unsigned) (10 * tries)); 6116890Seric errno = 0; 61259745Seric fd = open(filename, omode, cmode); 61359745Seric if (fd >= 0) 6146890Seric break; 6159376Seric if (errno != ENFILE && errno != EINTR) 6169376Seric break; 6176890Seric } 61859745Seric if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode)) 61956328Seric { 62056328Seric int locktype; 62156328Seric 62256328Seric /* lock the file to avoid accidental conflicts */ 62359745Seric if ((omode & O_ACCMODE) != O_RDONLY) 62456328Seric locktype = LOCK_EX; 62556328Seric else 62656328Seric locktype = LOCK_SH; 62764335Seric (void) lockfile(fd, filename, NULL, locktype); 62856328Seric errno = 0; 62956328Seric } 63063787Seric if (fd < 0) 63163787Seric return NULL; 63263787Seric else 63363787Seric return fdopen(fd, om->farg); 6346890Seric } 6357124Seric /* 6367124Seric ** PUTLINE -- put a line like fputs obeying SMTP conventions 6377124Seric ** 6387753Seric ** This routine always guarantees outputing a newline (or CRLF, 6397753Seric ** as appropriate) at the end of the string. 6407753Seric ** 6417124Seric ** Parameters: 6427124Seric ** l -- line to put. 6437124Seric ** fp -- file to put it onto. 64410172Seric ** m -- the mailer used to control output. 6457124Seric ** 6467124Seric ** Returns: 6477124Seric ** none 6487124Seric ** 6497124Seric ** Side Effects: 6507124Seric ** output of l to fp. 6517124Seric */ 6527124Seric 65310172Seric putline(l, fp, m) 6547753Seric register char *l; 6557124Seric FILE *fp; 65610172Seric MAILER *m; 6577124Seric { 6587124Seric register char *p; 65947157Sbostic register char svchar; 6607124Seric 66111275Seric /* strip out 0200 bits -- these can look like TELNET protocol */ 66252106Seric if (bitnset(M_7BITS, m->m_flags)) 66311275Seric { 66461707Seric for (p = l; (svchar = *p) != '\0'; ++p) 66561707Seric if (bitset(0200, svchar)) 66647157Sbostic *p = svchar &~ 0200; 66711275Seric } 66811275Seric 6697753Seric do 6707124Seric { 6717753Seric /* find the end of the line */ 67256795Seric p = strchr(l, '\n'); 6737753Seric if (p == NULL) 6747753Seric p = &l[strlen(l)]; 6757124Seric 67663753Seric if (TrafficLogFile != NULL) 67763753Seric fprintf(TrafficLogFile, "%05d >>> ", getpid()); 67863753Seric 6797753Seric /* check for line overflow */ 68052106Seric while (m->m_linelimit > 0 && (p - l) > m->m_linelimit) 6817753Seric { 68252106Seric register char *q = &l[m->m_linelimit - 1]; 6837124Seric 6847753Seric svchar = *q; 6857753Seric *q = '\0'; 68610685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 68763753Seric { 68823105Seric (void) putc('.', fp); 68963753Seric if (TrafficLogFile != NULL) 69063753Seric (void) putc('.', TrafficLogFile); 69163753Seric } 6927753Seric fputs(l, fp); 69323105Seric (void) putc('!', fp); 69410326Seric fputs(m->m_eol, fp); 69563753Seric if (TrafficLogFile != NULL) 69663753Seric fprintf(TrafficLogFile, "%s!\n%05d >>> ", 69763753Seric l, getpid()); 6987753Seric *q = svchar; 6997753Seric l = q; 7007753Seric } 7017124Seric 7027753Seric /* output last part */ 70310685Seric if (l[0] == '.' && bitnset(M_XDOT, m->m_flags)) 70463753Seric { 70523105Seric (void) putc('.', fp); 70663753Seric if (TrafficLogFile != NULL) 70763753Seric (void) putc('.', TrafficLogFile); 70863753Seric } 70963753Seric if (TrafficLogFile != NULL) 71063753Seric fprintf(TrafficLogFile, "%.*s\n", p - l, l); 71147157Sbostic for ( ; l < p; ++l) 71247157Sbostic (void) putc(*l, fp); 71310326Seric fputs(m->m_eol, fp); 7147753Seric if (*l == '\n') 71547157Sbostic ++l; 7167753Seric } while (l[0] != '\0'); 7177124Seric } 7187676Seric /* 7197676Seric ** XUNLINK -- unlink a file, doing logging as appropriate. 7207676Seric ** 7217676Seric ** Parameters: 7227676Seric ** f -- name of file to unlink. 7237676Seric ** 7247676Seric ** Returns: 7257676Seric ** none. 7267676Seric ** 7277676Seric ** Side Effects: 7287676Seric ** f is unlinked. 7297676Seric */ 7307676Seric 7317676Seric xunlink(f) 7327676Seric char *f; 7337676Seric { 7347676Seric register int i; 7357676Seric 7367676Seric # ifdef LOG 73758020Seric if (LogLevel > 98) 73858020Seric syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 73956795Seric # endif /* LOG */ 7407676Seric 7417676Seric i = unlink(f); 7427676Seric # ifdef LOG 74358020Seric if (i < 0 && LogLevel > 97) 7447942Seric syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 74556795Seric # endif /* LOG */ 7467676Seric } 7477685Seric /* 74858680Seric ** XFCLOSE -- close a file, doing logging as appropriate. 74958680Seric ** 75058680Seric ** Parameters: 75158680Seric ** fp -- file pointer for the file to close 75258680Seric ** a, b -- miscellaneous crud to print for debugging 75358680Seric ** 75458680Seric ** Returns: 75558680Seric ** none. 75658680Seric ** 75758680Seric ** Side Effects: 75858680Seric ** fp is closed. 75958680Seric */ 76058680Seric 76158680Seric xfclose(fp, a, b) 76258680Seric FILE *fp; 76358680Seric char *a, *b; 76458680Seric { 76558796Seric if (tTd(53, 99)) 76658680Seric printf("xfclose(%x) %s %s\n", fp, a, b); 76764401Seric #ifdef XDEBUG 76864401Seric if (fileno(fp) == 1) 76964401Seric syserr("xfclose(%s %s): fd = 1", a, b); 77064401Seric #endif 77158796Seric if (fclose(fp) < 0 && tTd(53, 99)) 77258680Seric printf("xfclose FAILURE: %s\n", errstring(errno)); 77358680Seric } 77458680Seric /* 77514885Seric ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 7767685Seric ** 7777685Seric ** Parameters: 7787685Seric ** buf -- place to put the input line. 7797685Seric ** siz -- size of buf. 7807685Seric ** fp -- file to read from. 78157384Seric ** timeout -- the timeout before error occurs. 78261093Seric ** during -- what we are trying to read (for error messages). 7837685Seric ** 7847685Seric ** Returns: 78515533Seric ** NULL on error (including timeout). This will also leave 78615533Seric ** buf containing a null string. 7877685Seric ** buf otherwise. 7887685Seric ** 7897685Seric ** Side Effects: 7907685Seric ** none. 7917685Seric */ 7927685Seric 79314885Seric static jmp_buf CtxReadTimeout; 79463937Seric static int readtimeout(); 7957685Seric 7967685Seric char * 79761093Seric sfgets(buf, siz, fp, timeout, during) 7987685Seric char *buf; 7997685Seric int siz; 8007685Seric FILE *fp; 80157384Seric time_t timeout; 80261093Seric char *during; 8037685Seric { 8047942Seric register EVENT *ev = NULL; 8057685Seric register char *p; 8067685Seric 80714885Seric /* set the timeout */ 80857384Seric if (timeout != 0) 80914885Seric { 81014885Seric if (setjmp(CtxReadTimeout) != 0) 81114885Seric { 81236233Skarels # ifdef LOG 81336230Skarels syslog(LOG_NOTICE, 81461093Seric "timeout waiting for input from %s during %s\n", 81561093Seric CurHostName? CurHostName: "local", during); 81636233Skarels # endif 81736230Skarels errno = 0; 81861093Seric usrerr("451 timeout waiting for input during %s", 81961093Seric during); 82019037Seric buf[0] = '\0'; 82163753Seric #ifdef XDEBUG 82263753Seric checkfd012(during); 82363753Seric #endif 82414885Seric return (NULL); 82514885Seric } 82657384Seric ev = setevent(timeout, readtimeout, 0); 82714885Seric } 82814885Seric 82914885Seric /* try to read */ 83015533Seric p = NULL; 83115533Seric while (p == NULL && !feof(fp) && !ferror(fp)) 8327942Seric { 8337942Seric errno = 0; 8347942Seric p = fgets(buf, siz, fp); 83515533Seric if (errno == EINTR) 83615533Seric clearerr(fp); 83715533Seric } 83814885Seric 83914885Seric /* clear the event if it has not sprung */ 8407685Seric clrevent(ev); 84114885Seric 84214885Seric /* clean up the books and exit */ 8438055Seric LineNumber++; 84415533Seric if (p == NULL) 84516880Seric { 84615533Seric buf[0] = '\0'; 84763753Seric if (TrafficLogFile != NULL) 84863753Seric fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid()); 84916880Seric return (NULL); 85016880Seric } 85163753Seric if (TrafficLogFile != NULL) 85263753Seric fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf); 85359709Seric if (SevenBit) 85452106Seric for (p = buf; *p != '\0'; p++) 85552106Seric *p &= ~0200; 85616880Seric return (buf); 8577685Seric } 8587685Seric 8597685Seric static 8607685Seric readtimeout() 8617685Seric { 86214885Seric longjmp(CtxReadTimeout, 1); 8637685Seric } 8647786Seric /* 8657786Seric ** FGETFOLDED -- like fgets, but know about folded lines. 8667786Seric ** 8677786Seric ** Parameters: 8687786Seric ** buf -- place to put result. 8697786Seric ** n -- bytes available. 8707786Seric ** f -- file to read from. 8717786Seric ** 8727786Seric ** Returns: 87357135Seric ** input line(s) on success, NULL on error or EOF. 87457135Seric ** This will normally be buf -- unless the line is too 87557135Seric ** long, when it will be xalloc()ed. 8767786Seric ** 8777786Seric ** Side Effects: 8787786Seric ** buf gets lines from f, with continuation lines (lines 8797786Seric ** with leading white space) appended. CRLF's are mapped 8807786Seric ** into single newlines. Any trailing NL is stripped. 8817786Seric */ 8827786Seric 8837786Seric char * 8847786Seric fgetfolded(buf, n, f) 8857786Seric char *buf; 8867786Seric register int n; 8877786Seric FILE *f; 8887786Seric { 8897786Seric register char *p = buf; 89057135Seric char *bp = buf; 8917786Seric register int i; 8927786Seric 8937786Seric n--; 89417350Seric while ((i = getc(f)) != EOF) 8957786Seric { 89617350Seric if (i == '\r') 89717350Seric { 89817350Seric i = getc(f); 89917350Seric if (i != '\n') 90017350Seric { 90117350Seric if (i != EOF) 90223105Seric (void) ungetc(i, f); 90317350Seric i = '\r'; 90417350Seric } 90517350Seric } 90657135Seric if (--n <= 0) 90757135Seric { 90857135Seric /* allocate new space */ 90957135Seric char *nbp; 91057135Seric int nn; 91157135Seric 91257135Seric nn = (p - bp); 91357232Seric if (nn < MEMCHUNKSIZE) 91457135Seric nn *= 2; 91557135Seric else 91657232Seric nn += MEMCHUNKSIZE; 91757135Seric nbp = xalloc(nn); 91857135Seric bcopy(bp, nbp, p - bp); 91957135Seric p = &nbp[p - bp]; 92057135Seric if (bp != buf) 92157135Seric free(bp); 92257135Seric bp = nbp; 92357135Seric n = nn - (p - bp); 92457135Seric } 92557135Seric *p++ = i; 92617350Seric if (i == '\n') 92717350Seric { 92817350Seric LineNumber++; 92917350Seric i = getc(f); 93017350Seric if (i != EOF) 93123105Seric (void) ungetc(i, f); 93217350Seric if (i != ' ' && i != '\t') 93352647Seric break; 93417350Seric } 9357786Seric } 93657135Seric if (p == bp) 93752647Seric return (NULL); 93852647Seric *--p = '\0'; 93957135Seric return (bp); 9407786Seric } 9417860Seric /* 9427886Seric ** CURTIME -- return current time. 9437886Seric ** 9447886Seric ** Parameters: 9457886Seric ** none. 9467886Seric ** 9477886Seric ** Returns: 9487886Seric ** the current time. 9497886Seric ** 9507886Seric ** Side Effects: 9517886Seric ** none. 9527886Seric */ 9537886Seric 9547886Seric time_t 9557886Seric curtime() 9567886Seric { 9577886Seric auto time_t t; 9587886Seric 9597886Seric (void) time(&t); 9607886Seric return (t); 9617886Seric } 9628264Seric /* 9638264Seric ** ATOBOOL -- convert a string representation to boolean. 9648264Seric ** 9658264Seric ** Defaults to "TRUE" 9668264Seric ** 9678264Seric ** Parameters: 9688264Seric ** s -- string to convert. Takes "tTyY" as true, 9698264Seric ** others as false. 9708264Seric ** 9718264Seric ** Returns: 9728264Seric ** A boolean representation of the string. 9738264Seric ** 9748264Seric ** Side Effects: 9758264Seric ** none. 9768264Seric */ 9778264Seric 9788264Seric bool 9798264Seric atobool(s) 9808264Seric register char *s; 9818264Seric { 98263833Seric if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL) 9838264Seric return (TRUE); 9848264Seric return (FALSE); 9858264Seric } 9869048Seric /* 9879048Seric ** ATOOCT -- convert a string representation to octal. 9889048Seric ** 9899048Seric ** Parameters: 9909048Seric ** s -- string to convert. 9919048Seric ** 9929048Seric ** Returns: 9939048Seric ** An integer representing the string interpreted as an 9949048Seric ** octal number. 9959048Seric ** 9969048Seric ** Side Effects: 9979048Seric ** none. 9989048Seric */ 9999048Seric 10009048Seric atooct(s) 10019048Seric register char *s; 10029048Seric { 10039048Seric register int i = 0; 10049048Seric 10059048Seric while (*s >= '0' && *s <= '7') 10069048Seric i = (i << 3) | (*s++ - '0'); 10079048Seric return (i); 10089048Seric } 10099376Seric /* 10109376Seric ** WAITFOR -- wait for a particular process id. 10119376Seric ** 10129376Seric ** Parameters: 10139376Seric ** pid -- process id to wait for. 10149376Seric ** 10159376Seric ** Returns: 10169376Seric ** status of pid. 10179376Seric ** -1 if pid never shows up. 10189376Seric ** 10199376Seric ** Side Effects: 10209376Seric ** none. 10219376Seric */ 10229376Seric 102364562Seric int 10249376Seric waitfor(pid) 10259376Seric int pid; 10269376Seric { 102764562Seric #ifdef WAITUNION 102864562Seric union wait st; 102964562Seric #else 10309376Seric auto int st; 103164562Seric #endif 10329376Seric int i; 10339376Seric 10349376Seric do 10359376Seric { 10369376Seric errno = 0; 10379376Seric i = wait(&st); 10389376Seric } while ((i >= 0 || errno == EINTR) && i != pid); 10399376Seric if (i < 0) 104064562Seric return -1; 104164562Seric #ifdef WAITUNION 104264562Seric return st.w_status; 104364562Seric #else 104464562Seric return st; 104564562Seric #endif 10469376Seric } 10479376Seric /* 104810685Seric ** BITINTERSECT -- tell if two bitmaps intersect 104910685Seric ** 105010685Seric ** Parameters: 105110685Seric ** a, b -- the bitmaps in question 105210685Seric ** 105310685Seric ** Returns: 105410685Seric ** TRUE if they have a non-null intersection 105510685Seric ** FALSE otherwise 105610685Seric ** 105710685Seric ** Side Effects: 105810685Seric ** none. 105910685Seric */ 106010685Seric 106110685Seric bool 106210685Seric bitintersect(a, b) 106310685Seric BITMAP a; 106410685Seric BITMAP b; 106510685Seric { 106610685Seric int i; 106710685Seric 106810685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 106910685Seric if ((a[i] & b[i]) != 0) 107010685Seric return (TRUE); 107110685Seric return (FALSE); 107210685Seric } 107310685Seric /* 107410685Seric ** BITZEROP -- tell if a bitmap is all zero 107510685Seric ** 107610685Seric ** Parameters: 107710685Seric ** map -- the bit map to check 107810685Seric ** 107910685Seric ** Returns: 108010685Seric ** TRUE if map is all zero. 108110685Seric ** FALSE if there are any bits set in map. 108210685Seric ** 108310685Seric ** Side Effects: 108410685Seric ** none. 108510685Seric */ 108610685Seric 108710685Seric bool 108810685Seric bitzerop(map) 108910685Seric BITMAP map; 109010685Seric { 109110685Seric int i; 109210685Seric 109310685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 109410685Seric if (map[i] != 0) 109510685Seric return (FALSE); 109610685Seric return (TRUE); 109710685Seric } 109858247Seric /* 109958318Seric ** STRCONTAINEDIN -- tell if one string is contained in another 110058318Seric ** 110158318Seric ** Parameters: 110258318Seric ** a -- possible substring. 110358318Seric ** b -- possible superstring. 110458318Seric ** 110558318Seric ** Returns: 110658318Seric ** TRUE if a is contained in b. 110758318Seric ** FALSE otherwise. 110858318Seric */ 110958318Seric 111058318Seric bool 111158318Seric strcontainedin(a, b) 111258318Seric register char *a; 111358318Seric register char *b; 111458318Seric { 111558318Seric int l; 111658318Seric 111758318Seric l = strlen(a); 111858318Seric for (;;) 111958318Seric { 112058318Seric b = strchr(b, a[0]); 112158318Seric if (b == NULL) 112258318Seric return FALSE; 112358318Seric if (strncmp(a, b, l) == 0) 112458318Seric return TRUE; 112558318Seric b++; 112658318Seric } 112758318Seric } 112863753Seric /* 112963753Seric ** CHECKFD012 -- check low numbered file descriptors 113063753Seric ** 113163753Seric ** File descriptors 0, 1, and 2 should be open at all times. 113263753Seric ** This routine verifies that, and fixes it if not true. 113363753Seric ** 113463753Seric ** Parameters: 113563753Seric ** where -- a tag printed if the assertion failed 113663753Seric ** 113763753Seric ** Returns: 113863753Seric ** none 113963753Seric */ 114063753Seric 114163753Seric checkfd012(where) 114263753Seric char *where; 114363753Seric { 114463753Seric #ifdef XDEBUG 114563753Seric register int i; 114663753Seric struct stat stbuf; 114763753Seric 114863753Seric for (i = 0; i < 3; i++) 114963753Seric { 115064735Seric if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP) 115163753Seric { 115263753Seric /* oops.... */ 115363753Seric int fd; 115463753Seric 115563753Seric syserr("%s: fd %d not open", where, i); 115663753Seric fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666); 115763753Seric if (fd != i) 115863753Seric { 115963753Seric (void) dup2(fd, i); 116063753Seric (void) close(fd); 116163753Seric } 116263753Seric } 116363753Seric } 116463937Seric #endif /* XDEBUG */ 116563753Seric } 116664725Seric /* 116764725Seric ** PRINTOPENFDS -- print the open file descriptors (for debugging) 116864725Seric ** 116964725Seric ** Parameters: 117064725Seric ** logit -- if set, send output to syslog; otherwise 117164725Seric ** print for debugging. 117264725Seric ** 117364725Seric ** Returns: 117464725Seric ** none. 117564725Seric */ 117664725Seric 117764725Seric #include <netdb.h> 117864725Seric #include <arpa/inet.h> 117964725Seric 118064725Seric printopenfds(logit) 118164725Seric bool logit; 118264725Seric { 118364725Seric register int fd; 118464743Seric extern int DtableSize; 118564743Seric 118664743Seric for (fd = 0; fd < DtableSize; fd++) 118764743Seric dumpfd(fd, FALSE, logit); 118864743Seric } 118964743Seric /* 119064743Seric ** DUMPFD -- dump a file descriptor 119164743Seric ** 119264743Seric ** Parameters: 119364743Seric ** fd -- the file descriptor to dump. 119464743Seric ** printclosed -- if set, print a notification even if 119564743Seric ** it is closed; otherwise print nothing. 119664743Seric ** logit -- if set, send output to syslog instead of stdout. 119764743Seric */ 119864743Seric 119964743Seric dumpfd(fd, printclosed, logit) 120064743Seric int fd; 120164743Seric bool printclosed; 120264743Seric bool logit; 120364743Seric { 120464725Seric register struct hostent *hp; 120564725Seric register char *p; 120664743Seric struct sockaddr_in sin; 120764743Seric auto int slen; 120864743Seric struct stat st; 120964725Seric char buf[200]; 121064725Seric 121164743Seric p = buf; 121264743Seric sprintf(p, "%3d: ", fd); 121364743Seric p += strlen(p); 121464743Seric 121564743Seric if (fstat(fd, &st) < 0) 121664725Seric { 121764743Seric if (printclosed || errno != EBADF) 121864743Seric { 121964743Seric sprintf(p, "CANNOT STAT (%s)", errstring(errno)); 122064743Seric goto printit; 122164743Seric } 122264743Seric return; 122364743Seric } 122464725Seric 122564743Seric slen = fcntl(fd, F_GETFL, NULL); 122664743Seric if (slen != -1) 122764743Seric { 122864743Seric sprintf(p, "fl=0x%x, ", slen); 122964743Seric p += strlen(p); 123064743Seric } 123164725Seric 123264743Seric sprintf(p, "mode=%o: ", st.st_mode); 123364743Seric p += strlen(p); 123464743Seric switch (st.st_mode & S_IFMT) 123564743Seric { 123664807Seric #ifdef S_IFSOCK 123764743Seric case S_IFSOCK: 123864743Seric sprintf(p, "SOCK "); 123964725Seric p += strlen(p); 124064743Seric slen = sizeof sin; 124164743Seric if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0) 124264743Seric sprintf(p, "(badsock)"); 124364743Seric else 124464725Seric { 124564743Seric hp = gethostbyaddr((char *) &sin.sin_addr, slen, AF_INET); 124664743Seric sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr) 124764743Seric : hp->h_name, ntohs(sin.sin_port)); 124864743Seric } 124964743Seric p += strlen(p); 125064743Seric sprintf(p, "->"); 125164743Seric p += strlen(p); 125264743Seric slen = sizeof sin; 125364743Seric if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0) 125464743Seric sprintf(p, "(badsock)"); 125564743Seric else 125664743Seric { 125764743Seric hp = gethostbyaddr((char *) &sin.sin_addr, slen, AF_INET); 125864743Seric sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr) 125964743Seric : hp->h_name, ntohs(sin.sin_port)); 126064743Seric } 126164743Seric break; 126264807Seric #endif 126364725Seric 126464743Seric case S_IFCHR: 126564743Seric sprintf(p, "CHR: "); 126664743Seric p += strlen(p); 126764743Seric goto defprint; 126864725Seric 126964743Seric case S_IFBLK: 127064743Seric sprintf(p, "BLK: "); 127164743Seric p += strlen(p); 127264743Seric goto defprint; 127364725Seric 127464743Seric default: 127564725Seric defprint: 127664743Seric sprintf(p, "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld", 127764743Seric major(st.st_dev), minor(st.st_dev), st.st_ino, 127864743Seric st.st_nlink, st.st_uid, st.st_gid, st.st_size); 127964743Seric break; 128064743Seric } 128164725Seric 128264743Seric printit: 128364743Seric if (logit) 1284*65006Seric syslog(LOG_DEBUG, "%s", buf); 128564743Seric else 128664743Seric printf("%s\n", buf); 128764725Seric } 1288