122717Sdist /* 268839Seric * Copyright (c) 1983, 1995 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*69662Seric static char sccsid[] = "@(#)util.c 8.69 (Berkeley) 05/24/95"; 1133731Sbostic #endif /* not lint */ 1222717Sdist 1358332Seric # include "sendmail.h" 14298Seric # include <sysexits.h> 1557135Seric /* 16298Seric ** STRIPQUOTES -- Strip quotes & quote bits from a string. 17298Seric ** 18298Seric ** Runs through a string and strips off unquoted quote 19298Seric ** characters and quote bits. This is done in place. 20298Seric ** 21298Seric ** Parameters: 22298Seric ** s -- the string to strip. 23298Seric ** 24298Seric ** Returns: 25298Seric ** none. 26298Seric ** 27298Seric ** Side Effects: 28298Seric ** none. 29298Seric ** 30298Seric ** Called By: 31298Seric ** deliver 32298Seric */ 33298Seric 3454983Seric stripquotes(s) 35298Seric char *s; 36298Seric { 37298Seric register char *p; 38298Seric register char *q; 39298Seric register char c; 40298Seric 414101Seric if (s == NULL) 424101Seric return; 434101Seric 4454983Seric p = q = s; 4554983Seric do 46298Seric { 4754983Seric c = *p++; 4854983Seric if (c == '\\') 4954983Seric c = *p++; 5054983Seric else if (c == '"') 5154983Seric continue; 5254983Seric *q++ = c; 5354983Seric } while (c != '\0'); 54298Seric } 55298Seric /* 56298Seric ** XALLOC -- Allocate memory and bitch wildly on failure. 57298Seric ** 58298Seric ** THIS IS A CLUDGE. This should be made to give a proper 59298Seric ** error -- but after all, what can we do? 60298Seric ** 61298Seric ** Parameters: 62298Seric ** sz -- size of area to allocate. 63298Seric ** 64298Seric ** Returns: 65298Seric ** pointer to data region. 66298Seric ** 67298Seric ** Side Effects: 68298Seric ** Memory is allocated. 69298Seric */ 70298Seric 71298Seric char * 72298Seric xalloc(sz) 737007Seric register int sz; 74298Seric { 75298Seric register char *p; 76298Seric 7766747Seric /* some systems can't handle size zero mallocs */ 7866747Seric if (sz <= 0) 7966747Seric sz = 1; 8066747Seric 8123121Seric p = malloc((unsigned) sz); 82298Seric if (p == NULL) 83298Seric { 84298Seric syserr("Out of memory!!"); 8510685Seric abort(); 8610685Seric /* exit(EX_UNAVAILABLE); */ 87298Seric } 88298Seric return (p); 89298Seric } 90298Seric /* 913151Seric ** COPYPLIST -- copy list of pointers. 923151Seric ** 933151Seric ** This routine is the equivalent of newstr for lists of 943151Seric ** pointers. 953151Seric ** 963151Seric ** Parameters: 973151Seric ** list -- list of pointers to copy. 983151Seric ** Must be NULL terminated. 993151Seric ** copycont -- if TRUE, copy the contents of the vector 1003151Seric ** (which must be a string) also. 1013151Seric ** 1023151Seric ** Returns: 1033151Seric ** a copy of 'list'. 1043151Seric ** 1053151Seric ** Side Effects: 1063151Seric ** none. 1073151Seric */ 1083151Seric 1093151Seric char ** 1103151Seric copyplist(list, copycont) 1113151Seric char **list; 1123151Seric bool copycont; 1133151Seric { 1143151Seric register char **vp; 1153151Seric register char **newvp; 1163151Seric 1173151Seric for (vp = list; *vp != NULL; vp++) 1183151Seric continue; 1193151Seric 1203151Seric vp++; 1213151Seric 12216897Seric newvp = (char **) xalloc((int) (vp - list) * sizeof *vp); 12316897Seric bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp); 1243151Seric 1253151Seric if (copycont) 1263151Seric { 1273151Seric for (vp = newvp; *vp != NULL; vp++) 1283151Seric *vp = newstr(*vp); 1293151Seric } 1303151Seric 1313151Seric return (newvp); 1323151Seric } 1333151Seric /* 13458170Seric ** COPYQUEUE -- copy address queue. 13558170Seric ** 13658170Seric ** This routine is the equivalent of newstr for address queues 13758170Seric ** addresses marked with QDONTSEND aren't copied 13858170Seric ** 13958170Seric ** Parameters: 14058170Seric ** addr -- list of address structures to copy. 14158170Seric ** 14258170Seric ** Returns: 14358170Seric ** a copy of 'addr'. 14458170Seric ** 14558170Seric ** Side Effects: 14658170Seric ** none. 14758170Seric */ 14858170Seric 14958170Seric ADDRESS * 15058170Seric copyqueue(addr) 15158170Seric ADDRESS *addr; 15258170Seric { 15358170Seric register ADDRESS *newaddr; 15458170Seric ADDRESS *ret; 15558170Seric register ADDRESS **tail = &ret; 15658170Seric 15758170Seric while (addr != NULL) 15858170Seric { 15958170Seric if (!bitset(QDONTSEND, addr->q_flags)) 16058170Seric { 16158170Seric newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS)); 16258170Seric STRUCTCOPY(*addr, *newaddr); 16358170Seric *tail = newaddr; 16458170Seric tail = &newaddr->q_next; 16558170Seric } 16658170Seric addr = addr->q_next; 16758170Seric } 16858170Seric *tail = NULL; 16958170Seric 17058170Seric return ret; 17158170Seric } 17258170Seric /* 1733151Seric ** PRINTAV -- print argument vector. 1743151Seric ** 1753151Seric ** Parameters: 1763151Seric ** av -- argument vector. 1773151Seric ** 1783151Seric ** Returns: 1793151Seric ** none. 1803151Seric ** 1813151Seric ** Side Effects: 1823151Seric ** prints av. 1833151Seric */ 1843151Seric 1853151Seric printav(av) 1863151Seric register char **av; 1873151Seric { 1883151Seric while (*av != NULL) 1893151Seric { 1908063Seric if (tTd(0, 44)) 1918063Seric printf("\n\t%08x=", *av); 1928063Seric else 19323105Seric (void) putchar(' '); 1943151Seric xputs(*av++); 1953151Seric } 19623105Seric (void) putchar('\n'); 1973151Seric } 1983151Seric /* 1993151Seric ** LOWER -- turn letter into lower case. 2003151Seric ** 2013151Seric ** Parameters: 2023151Seric ** c -- character to turn into lower case. 2033151Seric ** 2043151Seric ** Returns: 2053151Seric ** c, in lower case. 2063151Seric ** 2073151Seric ** Side Effects: 2083151Seric ** none. 2093151Seric */ 2103151Seric 2113151Seric char 2123151Seric lower(c) 2133151Seric register char c; 2143151Seric { 21558050Seric return((isascii(c) && isupper(c)) ? tolower(c) : c); 2163151Seric } 2173151Seric /* 2183151Seric ** XPUTS -- put string doing control escapes. 2193151Seric ** 2203151Seric ** Parameters: 2213151Seric ** s -- string to put. 2223151Seric ** 2233151Seric ** Returns: 2243151Seric ** none. 2253151Seric ** 2263151Seric ** Side Effects: 2273151Seric ** output to stdout 2283151Seric */ 2293151Seric 2303151Seric xputs(s) 2313151Seric register char *s; 2323151Seric { 23358050Seric register int c; 23451781Seric register struct metamac *mp; 23551781Seric extern struct metamac MetaMacros[]; 2363151Seric 2378055Seric if (s == NULL) 2388055Seric { 2398055Seric printf("<null>"); 2408055Seric return; 2418055Seric } 24258050Seric while ((c = (*s++ & 0377)) != '\0') 2433151Seric { 2443151Seric if (!isascii(c)) 2453151Seric { 24668481Seric if (c == MATCHREPL) 24758050Seric { 24858050Seric putchar('$'); 24958050Seric continue; 25058050Seric } 25168481Seric if (c == MACROEXPAND) 25268481Seric { 25368481Seric putchar('$'); 25468481Seric if (bitset(0200, *s)) 25568481Seric printf("{%s}", macname(*s++ & 0377)); 25668481Seric continue; 25768481Seric } 25858050Seric for (mp = MetaMacros; mp->metaname != '\0'; mp++) 25958050Seric { 26058050Seric if ((mp->metaval & 0377) == c) 26158050Seric { 26258050Seric printf("$%c", mp->metaname); 26358050Seric break; 26458050Seric } 26558050Seric } 26658050Seric if (mp->metaname != '\0') 26758050Seric continue; 26823105Seric (void) putchar('\\'); 2693151Seric c &= 0177; 2703151Seric } 27157589Seric if (isprint(c)) 2723151Seric { 27357589Seric putchar(c); 27457589Seric continue; 27557589Seric } 27652050Seric 27757589Seric /* wasn't a meta-macro -- find another way to print it */ 27857589Seric switch (c) 27957589Seric { 28057589Seric case '\n': 28157589Seric c = 'n'; 28257589Seric break; 28352050Seric 28457589Seric case '\r': 28557589Seric c = 'r'; 28657589Seric break; 28752637Seric 28857589Seric case '\t': 28957589Seric c = 't'; 29057589Seric break; 29157589Seric 29257589Seric default: 29357589Seric (void) putchar('^'); 29457589Seric (void) putchar(c ^ 0100); 29557589Seric continue; 2963151Seric } 29768500Seric (void) putchar('\\'); 29868500Seric (void) putchar(c); 2993151Seric } 3004086Seric (void) fflush(stdout); 3013151Seric } 3023151Seric /* 3033151Seric ** MAKELOWER -- Translate a line into lower case 3043151Seric ** 3053151Seric ** Parameters: 3063151Seric ** p -- the string to translate. If NULL, return is 3073151Seric ** immediate. 3083151Seric ** 3093151Seric ** Returns: 3103151Seric ** none. 3113151Seric ** 3123151Seric ** Side Effects: 3133151Seric ** String pointed to by p is translated to lower case. 3143151Seric ** 3153151Seric ** Called By: 3163151Seric ** parse 3173151Seric */ 3183151Seric 31968481Seric void 3203151Seric makelower(p) 3213151Seric register char *p; 3223151Seric { 3233151Seric register char c; 3243151Seric 3253151Seric if (p == NULL) 3263151Seric return; 3273151Seric for (; (c = *p) != '\0'; p++) 3283151Seric if (isascii(c) && isupper(c)) 32933724Sbostic *p = tolower(c); 3303151Seric } 3314059Seric /* 3325196Seric ** BUILDFNAME -- build full name from gecos style entry. 3334375Seric ** 3345196Seric ** This routine interprets the strange entry that would appear 3355196Seric ** in the GECOS field of the password file. 3365196Seric ** 3374375Seric ** Parameters: 3385196Seric ** p -- name to build. 3395196Seric ** login -- the login name of this user (for &). 3405196Seric ** buf -- place to put the result. 3414375Seric ** 3424375Seric ** Returns: 34365006Seric ** none. 3444375Seric ** 3454375Seric ** Side Effects: 3464375Seric ** none. 3474375Seric */ 3484375Seric 34954984Seric buildfname(gecos, login, buf) 35065006Seric register char *gecos; 35165006Seric char *login; 35265006Seric char *buf; 3534375Seric { 35465006Seric register char *p; 35565006Seric register char *bp = buf; 35665006Seric int l; 3574375Seric 35865006Seric if (*gecos == '*') 35965006Seric gecos++; 36065006Seric 36165006Seric /* find length of final string */ 36265006Seric l = 0; 36365006Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 36454984Seric { 36565006Seric if (*p == '&') 36665006Seric l += strlen(login); 36765006Seric else 36865006Seric l++; 36954984Seric } 37065006Seric 37165006Seric /* now fill in buf */ 37265006Seric for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 3734375Seric { 37465006Seric if (*p == '&') 3754375Seric { 37665006Seric (void) strcpy(bp, login); 37765006Seric *bp = toupper(*bp); 37865006Seric while (*bp != '\0') 37965006Seric bp++; 3804375Seric } 38165006Seric else 38265006Seric *bp++ = *p; 3834375Seric } 3844375Seric *bp = '\0'; 3854375Seric } 3864375Seric /* 3874538Seric ** SAFEFILE -- return true if a file exists and is safe for a user. 3884538Seric ** 3894538Seric ** Parameters: 3904538Seric ** fn -- filename to check. 39164083Seric ** uid -- user id to compare against. 39264083Seric ** gid -- group id to compare against. 39364083Seric ** uname -- user name to compare against (used for group 39464083Seric ** sets). 39564944Seric ** flags -- modifiers: 39665064Seric ** SFF_MUSTOWN -- "uid" must own this file. 39765064Seric ** SFF_NOSLINK -- file cannot be a symbolic link. 3984538Seric ** mode -- mode bits that must match. 39968494Seric ** st -- if set, points to a stat structure that will 40068494Seric ** get the stat info for the file. 4014538Seric ** 4024538Seric ** Returns: 40358247Seric ** 0 if fn exists, is owned by uid, and matches mode. 40458247Seric ** An errno otherwise. The actual errno is cleared. 4054538Seric ** 4064538Seric ** Side Effects: 4074538Seric ** none. 4084538Seric */ 4094538Seric 41064083Seric #include <grp.h> 41164083Seric 41263581Seric #ifndef S_IXOTH 41363581Seric # define S_IXOTH (S_IEXEC >> 6) 41463581Seric #endif 41563581Seric 41664083Seric #ifndef S_IXGRP 41764083Seric # define S_IXGRP (S_IEXEC >> 3) 41864083Seric #endif 41964083Seric 42063753Seric #ifndef S_IXUSR 42163753Seric # define S_IXUSR (S_IEXEC) 42263753Seric #endif 42363753Seric 42468494Seric #define ST_MODE_NOFILE 0171147 /* unlikely to occur */ 42568494Seric 42658247Seric int 42768494Seric safefile(fn, uid, gid, uname, flags, mode, st) 4284538Seric char *fn; 42955372Seric uid_t uid; 43064083Seric gid_t gid; 43164083Seric char *uname; 43264944Seric int flags; 4334538Seric int mode; 43468494Seric struct stat *st; 4354538Seric { 43663581Seric register char *p; 43764083Seric register struct group *gr = NULL; 4384538Seric struct stat stbuf; 4394538Seric 44063581Seric if (tTd(54, 4)) 44164944Seric printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n", 44264944Seric fn, uid, gid, flags, mode); 44363581Seric errno = 0; 44468494Seric if (st == NULL) 44568494Seric st = &stbuf; 44663581Seric 44768481Seric if (!bitset(SFF_NOPATHCHECK, flags) || 44868481Seric (uid == 0 && !bitset(SFF_ROOTOK, flags))) 44963581Seric { 45068481Seric /* check the path to the file for acceptability */ 45168481Seric for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/') 45265225Seric { 45368481Seric *p = '\0'; 45468481Seric if (stat(fn, &stbuf) < 0) 45568481Seric break; 45668513Seric if (uid == 0 && bitset(S_IWGRP|S_IWOTH, stbuf.st_mode)) 45768513Seric message("051 WARNING: writable directory %s", 45868513Seric fn); 45968481Seric if (uid == 0 && !bitset(SFF_ROOTOK, flags)) 46068481Seric { 46168481Seric if (bitset(S_IXOTH, stbuf.st_mode)) 46268481Seric continue; 46368481Seric break; 46468481Seric } 46568481Seric if (stbuf.st_uid == uid && 46668481Seric bitset(S_IXUSR, stbuf.st_mode)) 46765225Seric continue; 46868481Seric if (stbuf.st_gid == gid && 46968481Seric bitset(S_IXGRP, stbuf.st_mode)) 47068481Seric continue; 47168481Seric #ifndef NO_GROUP_SET 47268481Seric if (uname != NULL && 47368481Seric ((gr != NULL && gr->gr_gid == stbuf.st_gid) || 47468481Seric (gr = getgrgid(stbuf.st_gid)) != NULL)) 47568481Seric { 47668481Seric register char **gp; 47768481Seric 47868481Seric for (gp = gr->gr_mem; gp != NULL && *gp != NULL; gp++) 47968481Seric if (strcmp(*gp, uname) == 0) 48068481Seric break; 48168481Seric if (gp != NULL && *gp != NULL && 48268481Seric bitset(S_IXGRP, stbuf.st_mode)) 48368481Seric continue; 48468481Seric } 48568481Seric #endif 48668481Seric if (!bitset(S_IXOTH, stbuf.st_mode)) 48768481Seric break; 48868478Seric } 48968481Seric if (p != NULL) 49063581Seric { 49168481Seric int ret = errno; 49263581Seric 49368481Seric if (ret == 0) 49468481Seric ret = EACCES; 49568481Seric if (tTd(54, 4)) 49668481Seric printf("\t[dir %s] %s\n", fn, errstring(ret)); 49768481Seric *p = '/'; 49868481Seric return ret; 49963581Seric } 50063581Seric } 50163581Seric 50264944Seric #ifdef HASLSTAT 50368494Seric if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, st) 50468494Seric : stat(fn, st)) < 0) 50564944Seric #else 50668494Seric if (stat(fn, st) < 0) 50764944Seric #endif 50858247Seric { 50958247Seric int ret = errno; 51058247Seric 51163581Seric if (tTd(54, 4)) 51264083Seric printf("\t%s\n", errstring(ret)); 51363581Seric 51458247Seric errno = 0; 51568494Seric if (!bitset(SFF_CREAT, flags)) 51668494Seric return ret; 51768494Seric 51868494Seric /* check to see if legal to create the file */ 51968494Seric p = strrchr(fn, '/'); 52068494Seric if (p == NULL) 52168494Seric return ENOTDIR; 52268494Seric *p = '\0'; 52368494Seric if (stat(fn, &stbuf) >= 0) 52468494Seric { 52568494Seric int md = S_IWRITE|S_IEXEC; 52668494Seric if (stbuf.st_uid != uid) 52768494Seric md >>= 6; 52868494Seric if ((stbuf.st_mode & md) != md) 52968494Seric errno = EACCES; 53068494Seric } 53168494Seric ret = errno; 53268494Seric if (tTd(54, 4)) 53368494Seric printf("\t[final dir %s uid %d mode %o] %s\n", 53468494Seric fn, stbuf.st_uid, stbuf.st_mode, 53568494Seric errstring(ret)); 53668494Seric *p = '/'; 53768494Seric st->st_mode = ST_MODE_NOFILE; 53858247Seric return ret; 53958247Seric } 54064944Seric 54164944Seric #ifdef S_ISLNK 54268494Seric if (bitset(SFF_NOSLINK, flags) && S_ISLNK(st->st_mode)) 54364944Seric { 54464944Seric if (tTd(54, 4)) 54568494Seric printf("\t[slink mode %o]\tEPERM\n", st->st_mode); 54664944Seric return EPERM; 54764944Seric } 54864944Seric #endif 54968513Seric if (bitset(SFF_REGONLY, flags) && !S_ISREG(st->st_mode)) 55068513Seric { 55168513Seric if (tTd(54, 4)) 55268513Seric printf("\t[non-reg mode %o]\tEPERM\n", st->st_mode); 55368513Seric return EPERM; 55468513Seric } 55568494Seric if (bitset(S_IWUSR|S_IWGRP|S_IWOTH, mode) && bitset(0111, st->st_mode)) 55668494Seric { 55768494Seric if (tTd(29, 5)) 55868494Seric printf("failed (mode %o: x bits)\n", st->st_mode); 55968513Seric return EPERM; 56068494Seric } 56168494Seric 56268494Seric if (bitset(SFF_SETUIDOK, flags)) 56368494Seric { 56468494Seric if (bitset(S_ISUID, st->st_mode) && 56568494Seric (st->st_uid != 0 || bitset(SFF_ROOTOK, flags))) 56668494Seric { 56768494Seric uid = st->st_uid; 56868494Seric uname = NULL; 56968494Seric } 57068494Seric if (bitset(S_ISGID, st->st_mode) && 57168494Seric (st->st_gid != 0 || bitset(SFF_ROOTOK, flags))) 57268494Seric gid = st->st_gid; 57368494Seric } 57468494Seric 57565139Seric if (uid == 0 && !bitset(SFF_ROOTOK, flags)) 57663581Seric mode >>= 6; 57768494Seric else if (st->st_uid != uid) 57864084Seric { 57964084Seric mode >>= 3; 58068494Seric if (st->st_gid == gid) 58164084Seric ; 58264084Seric #ifndef NO_GROUP_SET 58364084Seric else if (uname != NULL && 58468494Seric ((gr != NULL && gr->gr_gid == st->st_gid) || 58568494Seric (gr = getgrgid(st->st_gid)) != NULL)) 58664084Seric { 58764084Seric register char **gp; 58864084Seric 58964084Seric for (gp = gr->gr_mem; *gp != NULL; gp++) 59064084Seric if (strcmp(*gp, uname) == 0) 59164084Seric break; 59264084Seric if (*gp == NULL) 59364084Seric mode >>= 3; 59464084Seric } 59564084Seric #endif 59664084Seric else 59764084Seric mode >>= 3; 59864084Seric } 59963581Seric if (tTd(54, 4)) 60064084Seric printf("\t[uid %d, stat %o, mode %o] ", 60168494Seric st->st_uid, st->st_mode, mode); 60268494Seric if ((st->st_uid == uid || st->st_uid == 0 || 60365064Seric !bitset(SFF_MUSTOWN, flags)) && 60468494Seric (st->st_mode & mode) == mode) 60563581Seric { 60663581Seric if (tTd(54, 4)) 60764083Seric printf("\tOK\n"); 60858247Seric return 0; 60963581Seric } 61063581Seric if (tTd(54, 4)) 61164083Seric printf("\tEACCES\n"); 61263581Seric return EACCES; 6134538Seric } 6144538Seric /* 61568494Seric ** SAFEFOPEN -- do a file open with extra checking 61668494Seric ** 61768494Seric ** Parameters: 61868494Seric ** fn -- the file name to open. 61968494Seric ** omode -- the open-style mode flags. 62068494Seric ** cmode -- the create-style mode flags. 62168494Seric ** sff -- safefile flags. 62268494Seric ** 62368494Seric ** Returns: 62468494Seric ** Same as fopen. 62568494Seric */ 62668494Seric 62768703Seric #ifndef O_ACCMODE 62868703Seric # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) 62968703Seric #endif 63068703Seric 63168494Seric FILE * 63268494Seric safefopen(fn, omode, cmode, sff) 63368494Seric char *fn; 63468494Seric int omode; 63568494Seric int cmode; 63668494Seric int sff; 63768494Seric { 63868494Seric int rval; 63968494Seric FILE *fp; 64068494Seric int smode; 64168494Seric struct stat stb, sta; 64268494Seric extern char RealUserName[]; 64368494Seric 64468494Seric if (bitset(O_CREAT, omode)) 64568494Seric sff |= SFF_CREAT; 64668494Seric smode = 0; 64768494Seric switch (omode & O_ACCMODE) 64868494Seric { 64968494Seric case O_RDONLY: 65068494Seric smode = S_IREAD; 65168494Seric break; 65268494Seric 65368494Seric case O_WRONLY: 65468494Seric smode = S_IWRITE; 65568494Seric break; 65668494Seric 65768494Seric case O_RDWR: 65868494Seric smode = S_IREAD|S_IWRITE; 65968494Seric break; 66068494Seric 66168494Seric default: 66268494Seric smode = 0; 66368494Seric break; 66468494Seric } 66568513Seric if (bitset(SFF_OPENASROOT, sff)) 66668513Seric rval = safefile(fn, 0, 0, NULL, sff, smode, &stb); 66768513Seric else 66868513Seric rval = safefile(fn, RealUid, RealGid, RealUserName, 66968513Seric sff, smode, &stb); 67068494Seric if (rval != 0) 67168494Seric { 67268494Seric errno = rval; 67368494Seric return NULL; 67468494Seric } 67568494Seric if (stb.st_mode == ST_MODE_NOFILE) 67668494Seric omode |= O_EXCL; 67768494Seric 67868494Seric fp = dfopen(fn, omode, cmode); 67968494Seric if (fp == NULL) 68068494Seric return NULL; 68168494Seric if (bitset(O_EXCL, omode)) 68268494Seric return fp; 68368494Seric if (fstat(fileno(fp), &sta) < 0 || 68468494Seric sta.st_nlink != stb.st_nlink || 68568494Seric sta.st_dev != stb.st_dev || 68668494Seric sta.st_ino != stb.st_ino || 68768494Seric sta.st_uid != stb.st_uid || 68868494Seric sta.st_gid != stb.st_gid) 68968494Seric { 69068494Seric syserr("554 cannot open: file %s changed after open", fn); 69168513Seric fclose(fp); 69268494Seric errno = EPERM; 69368494Seric return NULL; 69468494Seric } 69568494Seric return fp; 69668494Seric } 69768494Seric /* 6984557Seric ** FIXCRLF -- fix <CR><LF> in line. 6994557Seric ** 7004557Seric ** Looks for the <CR><LF> combination and turns it into the 7014557Seric ** UNIX canonical <NL> character. It only takes one line, 7024557Seric ** i.e., it is assumed that the first <NL> found is the end 7034557Seric ** of the line. 7044557Seric ** 7054557Seric ** Parameters: 7064557Seric ** line -- the line to fix. 7074557Seric ** stripnl -- if true, strip the newline also. 7084557Seric ** 7094557Seric ** Returns: 7104557Seric ** none. 7114557Seric ** 7124557Seric ** Side Effects: 7134557Seric ** line is changed in place. 7144557Seric */ 7154557Seric 7164557Seric fixcrlf(line, stripnl) 7174557Seric char *line; 7184557Seric bool stripnl; 7194557Seric { 7204557Seric register char *p; 7214557Seric 72256795Seric p = strchr(line, '\n'); 7234557Seric if (p == NULL) 7244557Seric return; 72536291Sbostic if (p > line && p[-1] == '\r') 7264557Seric p--; 7274557Seric if (!stripnl) 7284557Seric *p++ = '\n'; 7294557Seric *p = '\0'; 7304557Seric } 7314557Seric /* 7326890Seric ** DFOPEN -- determined file open 7336890Seric ** 7346890Seric ** This routine has the semantics of fopen, except that it will 7356890Seric ** keep trying a few times to make this happen. The idea is that 7366890Seric ** on very loaded systems, we may run out of resources (inodes, 7376890Seric ** whatever), so this tries to get around it. 7386890Seric */ 7396890Seric 74059745Seric struct omodes 74159745Seric { 74259745Seric int mask; 74359745Seric int mode; 74459745Seric char *farg; 74559745Seric } OpenModes[] = 74659745Seric { 74759745Seric O_ACCMODE, O_RDONLY, "r", 74859745Seric O_ACCMODE|O_APPEND, O_WRONLY, "w", 74959745Seric O_ACCMODE|O_APPEND, O_WRONLY|O_APPEND, "a", 75059745Seric O_TRUNC, 0, "w+", 75159745Seric O_APPEND, O_APPEND, "a+", 75259745Seric 0, 0, "r+", 75359745Seric }; 75459745Seric 7556890Seric FILE * 75659745Seric dfopen(filename, omode, cmode) 7576890Seric char *filename; 75859745Seric int omode; 75959745Seric int cmode; 7606890Seric { 7616890Seric register int tries; 76259745Seric int fd; 76359745Seric register struct omodes *om; 76459431Seric struct stat st; 7656890Seric 76659745Seric for (om = OpenModes; om->mask != 0; om++) 76759745Seric if ((omode & om->mask) == om->mode) 76859745Seric break; 76959745Seric 7706890Seric for (tries = 0; tries < 10; tries++) 7716890Seric { 77225618Seric sleep((unsigned) (10 * tries)); 7736890Seric errno = 0; 77459745Seric fd = open(filename, omode, cmode); 77559745Seric if (fd >= 0) 7766890Seric break; 77766017Seric switch (errno) 77866017Seric { 77966017Seric case ENFILE: /* system file table full */ 78066017Seric case EINTR: /* interrupted syscall */ 78166017Seric #ifdef ETXTBSY 78266017Seric case ETXTBSY: /* Apollo: net file locked */ 78366017Seric #endif 78466017Seric continue; 78566017Seric } 78666017Seric break; 7876890Seric } 78859745Seric if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode)) 78956328Seric { 79056328Seric int locktype; 79156328Seric 79256328Seric /* lock the file to avoid accidental conflicts */ 79359745Seric if ((omode & O_ACCMODE) != O_RDONLY) 79456328Seric locktype = LOCK_EX; 79556328Seric else 79656328Seric locktype = LOCK_SH; 79764335Seric (void) lockfile(fd, filename, NULL, locktype); 79856328Seric errno = 0; 79956328Seric } 80063787Seric if (fd < 0) 80163787Seric return NULL; 80263787Seric else 80363787Seric return fdopen(fd, om->farg); 8046890Seric } 8057124Seric /* 8067124Seric ** PUTLINE -- put a line like fputs obeying SMTP conventions 8077124Seric ** 8087753Seric ** This routine always guarantees outputing a newline (or CRLF, 8097753Seric ** as appropriate) at the end of the string. 8107753Seric ** 8117124Seric ** Parameters: 8127124Seric ** l -- line to put. 81365870Seric ** mci -- the mailer connection information. 8147124Seric ** 8157124Seric ** Returns: 8167124Seric ** none 8177124Seric ** 8187124Seric ** Side Effects: 8197124Seric ** output of l to fp. 8207124Seric */ 8217124Seric 82265870Seric putline(l, mci) 8237753Seric register char *l; 82465870Seric register MCI *mci; 8257124Seric { 82668515Seric extern void putxline(); 82768515Seric 82868847Seric putxline(l, mci, PXLF_MAPFROM); 82968515Seric } 83068847Seric /* 83168847Seric ** PUTXLINE -- putline with flags bits. 83268847Seric ** 83368847Seric ** This routine always guarantees outputing a newline (or CRLF, 83468847Seric ** as appropriate) at the end of the string. 83568847Seric ** 83668847Seric ** Parameters: 83768847Seric ** l -- line to put. 83868847Seric ** mci -- the mailer connection information. 83968847Seric ** pxflags -- flag bits: 84068847Seric ** PXLF_MAPFROM -- map From_ to >From_. 84168847Seric ** PXLF_STRIP8BIT -- strip 8th bit. 84268847Seric ** 84368847Seric ** Returns: 84468847Seric ** none 84568847Seric ** 84668847Seric ** Side Effects: 84768847Seric ** output of l to fp. 84868847Seric */ 84968515Seric 85068515Seric void 85168847Seric putxline(l, mci, pxflags) 85268515Seric register char *l; 85368515Seric register MCI *mci; 85468847Seric int pxflags; 85568515Seric { 8567124Seric register char *p; 85747157Sbostic register char svchar; 85866004Seric int slop = 0; 8597124Seric 86011275Seric /* strip out 0200 bits -- these can look like TELNET protocol */ 86168847Seric if (bitset(MCIF_7BIT, mci->mci_flags) || 86268847Seric bitset(PXLF_STRIP8BIT, pxflags)) 86311275Seric { 86461707Seric for (p = l; (svchar = *p) != '\0'; ++p) 86561707Seric if (bitset(0200, svchar)) 86647157Sbostic *p = svchar &~ 0200; 86711275Seric } 86811275Seric 8697753Seric do 8707124Seric { 8717753Seric /* find the end of the line */ 87256795Seric p = strchr(l, '\n'); 8737753Seric if (p == NULL) 8747753Seric p = &l[strlen(l)]; 8757124Seric 87663753Seric if (TrafficLogFile != NULL) 87763753Seric fprintf(TrafficLogFile, "%05d >>> ", getpid()); 87863753Seric 8797753Seric /* check for line overflow */ 88065870Seric while (mci->mci_mailer->m_linelimit > 0 && 88166004Seric (p - l + slop) > mci->mci_mailer->m_linelimit) 8827753Seric { 88366004Seric register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1]; 8847124Seric 8857753Seric svchar = *q; 8867753Seric *q = '\0'; 88766004Seric if (l[0] == '.' && slop == 0 && 88866004Seric bitnset(M_XDOT, mci->mci_mailer->m_flags)) 88963753Seric { 89065870Seric (void) putc('.', mci->mci_out); 89163753Seric if (TrafficLogFile != NULL) 89263753Seric (void) putc('.', TrafficLogFile); 89363753Seric } 89468847Seric else if (l[0] == 'F' && slop == 0 && 89568847Seric bitset(PXLF_MAPFROM, pxflags) && 89668847Seric strncmp(l, "From ", 5) == 0 && 89768515Seric bitnset(M_ESCFROM, mci->mci_mailer->m_flags)) 89868515Seric { 89968515Seric (void) putc('>', mci->mci_out); 90068515Seric if (TrafficLogFile != NULL) 90168515Seric (void) putc('>', TrafficLogFile); 90268515Seric } 90365870Seric fputs(l, mci->mci_out); 90465870Seric (void) putc('!', mci->mci_out); 90565870Seric fputs(mci->mci_mailer->m_eol, mci->mci_out); 90666004Seric (void) putc(' ', mci->mci_out); 90763753Seric if (TrafficLogFile != NULL) 90866004Seric fprintf(TrafficLogFile, "%s!\n%05d >>> ", 90963753Seric l, getpid()); 9107753Seric *q = svchar; 9117753Seric l = q; 91266004Seric slop = 1; 9137753Seric } 9147124Seric 9157753Seric /* output last part */ 91666004Seric if (l[0] == '.' && slop == 0 && 91766004Seric bitnset(M_XDOT, mci->mci_mailer->m_flags)) 91863753Seric { 91965870Seric (void) putc('.', mci->mci_out); 92063753Seric if (TrafficLogFile != NULL) 92163753Seric (void) putc('.', TrafficLogFile); 92263753Seric } 92363753Seric if (TrafficLogFile != NULL) 92463753Seric fprintf(TrafficLogFile, "%.*s\n", p - l, l); 92547157Sbostic for ( ; l < p; ++l) 92665870Seric (void) putc(*l, mci->mci_out); 92765870Seric fputs(mci->mci_mailer->m_eol, mci->mci_out); 9287753Seric if (*l == '\n') 92947157Sbostic ++l; 9307753Seric } while (l[0] != '\0'); 9317124Seric } 9327676Seric /* 9337676Seric ** XUNLINK -- unlink a file, doing logging as appropriate. 9347676Seric ** 9357676Seric ** Parameters: 9367676Seric ** f -- name of file to unlink. 9377676Seric ** 9387676Seric ** Returns: 9397676Seric ** none. 9407676Seric ** 9417676Seric ** Side Effects: 9427676Seric ** f is unlinked. 9437676Seric */ 9447676Seric 9457676Seric xunlink(f) 9467676Seric char *f; 9477676Seric { 9487676Seric register int i; 9497676Seric 9507676Seric # ifdef LOG 95158020Seric if (LogLevel > 98) 95258020Seric syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 95356795Seric # endif /* LOG */ 9547676Seric 9557676Seric i = unlink(f); 9567676Seric # ifdef LOG 95758020Seric if (i < 0 && LogLevel > 97) 9587942Seric syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 95956795Seric # endif /* LOG */ 9607676Seric } 9617685Seric /* 96258680Seric ** XFCLOSE -- close a file, doing logging as appropriate. 96358680Seric ** 96458680Seric ** Parameters: 96558680Seric ** fp -- file pointer for the file to close 96658680Seric ** a, b -- miscellaneous crud to print for debugging 96758680Seric ** 96858680Seric ** Returns: 96958680Seric ** none. 97058680Seric ** 97158680Seric ** Side Effects: 97258680Seric ** fp is closed. 97358680Seric */ 97458680Seric 97558680Seric xfclose(fp, a, b) 97658680Seric FILE *fp; 97758680Seric char *a, *b; 97858680Seric { 97958796Seric if (tTd(53, 99)) 98058680Seric printf("xfclose(%x) %s %s\n", fp, a, b); 98164401Seric #ifdef XDEBUG 98264401Seric if (fileno(fp) == 1) 98364401Seric syserr("xfclose(%s %s): fd = 1", a, b); 98464401Seric #endif 98558796Seric if (fclose(fp) < 0 && tTd(53, 99)) 98658680Seric printf("xfclose FAILURE: %s\n", errstring(errno)); 98758680Seric } 98858680Seric /* 98914885Seric ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 9907685Seric ** 9917685Seric ** Parameters: 9927685Seric ** buf -- place to put the input line. 9937685Seric ** siz -- size of buf. 9947685Seric ** fp -- file to read from. 99557384Seric ** timeout -- the timeout before error occurs. 99661093Seric ** during -- what we are trying to read (for error messages). 9977685Seric ** 9987685Seric ** Returns: 99915533Seric ** NULL on error (including timeout). This will also leave 100015533Seric ** buf containing a null string. 10017685Seric ** buf otherwise. 10027685Seric ** 10037685Seric ** Side Effects: 10047685Seric ** none. 10057685Seric */ 10067685Seric 100714885Seric static jmp_buf CtxReadTimeout; 100868481Seric static void readtimeout(); 10097685Seric 10107685Seric char * 101161093Seric sfgets(buf, siz, fp, timeout, during) 10127685Seric char *buf; 10137685Seric int siz; 10147685Seric FILE *fp; 101557384Seric time_t timeout; 101661093Seric char *during; 10177685Seric { 10187942Seric register EVENT *ev = NULL; 10197685Seric register char *p; 10207685Seric 102166332Seric if (fp == NULL) 102266332Seric { 102366332Seric buf[0] = '\0'; 102466332Seric return NULL; 102566332Seric } 102666332Seric 102714885Seric /* set the timeout */ 102857384Seric if (timeout != 0) 102914885Seric { 103014885Seric if (setjmp(CtxReadTimeout) != 0) 103114885Seric { 103236233Skarels # ifdef LOG 103336230Skarels syslog(LOG_NOTICE, 103461093Seric "timeout waiting for input from %s during %s\n", 103561093Seric CurHostName? CurHostName: "local", during); 103636233Skarels # endif 103736230Skarels errno = 0; 103861093Seric usrerr("451 timeout waiting for input during %s", 103961093Seric during); 104019037Seric buf[0] = '\0'; 104163753Seric #ifdef XDEBUG 104263753Seric checkfd012(during); 104363753Seric #endif 104414885Seric return (NULL); 104514885Seric } 104668481Seric ev = setevent(timeout, readtimeout, 0); 104714885Seric } 104814885Seric 104914885Seric /* try to read */ 105015533Seric p = NULL; 105165190Seric while (!feof(fp) && !ferror(fp)) 10527942Seric { 10537942Seric errno = 0; 10547942Seric p = fgets(buf, siz, fp); 105565190Seric if (p != NULL || errno != EINTR) 105665186Seric break; 105765190Seric clearerr(fp); 105815533Seric } 105914885Seric 106014885Seric /* clear the event if it has not sprung */ 106168481Seric clrevent(ev); 106214885Seric 106314885Seric /* clean up the books and exit */ 10648055Seric LineNumber++; 106515533Seric if (p == NULL) 106616880Seric { 106715533Seric buf[0] = '\0'; 106863753Seric if (TrafficLogFile != NULL) 106963753Seric fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid()); 107016880Seric return (NULL); 107116880Seric } 107263753Seric if (TrafficLogFile != NULL) 107363753Seric fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf); 107468481Seric if (SevenBitInput) 107568481Seric { 107652106Seric for (p = buf; *p != '\0'; p++) 107752106Seric *p &= ~0200; 107867546Seric } 107968481Seric else if (!HasEightBits) 108067546Seric { 108168481Seric for (p = buf; *p != '\0'; p++) 108268481Seric { 108368481Seric if (bitset(0200, *p)) 108468481Seric { 108568481Seric HasEightBits = TRUE; 108668481Seric break; 108768481Seric } 108868481Seric } 108967546Seric } 109068481Seric return (buf); 10917685Seric } 10927685Seric 109368481Seric static void 109466765Seric readtimeout(timeout) 109566765Seric time_t timeout; 10967685Seric { 109768481Seric longjmp(CtxReadTimeout, 1); 10987685Seric } 10997786Seric /* 11007786Seric ** FGETFOLDED -- like fgets, but know about folded lines. 11017786Seric ** 11027786Seric ** Parameters: 11037786Seric ** buf -- place to put result. 11047786Seric ** n -- bytes available. 11057786Seric ** f -- file to read from. 11067786Seric ** 11077786Seric ** Returns: 110857135Seric ** input line(s) on success, NULL on error or EOF. 110957135Seric ** This will normally be buf -- unless the line is too 111057135Seric ** long, when it will be xalloc()ed. 11117786Seric ** 11127786Seric ** Side Effects: 11137786Seric ** buf gets lines from f, with continuation lines (lines 11147786Seric ** with leading white space) appended. CRLF's are mapped 11157786Seric ** into single newlines. Any trailing NL is stripped. 11167786Seric */ 11177786Seric 11187786Seric char * 11197786Seric fgetfolded(buf, n, f) 11207786Seric char *buf; 11217786Seric register int n; 11227786Seric FILE *f; 11237786Seric { 11247786Seric register char *p = buf; 112557135Seric char *bp = buf; 11267786Seric register int i; 11277786Seric 11287786Seric n--; 112917350Seric while ((i = getc(f)) != EOF) 11307786Seric { 113117350Seric if (i == '\r') 113217350Seric { 113317350Seric i = getc(f); 113417350Seric if (i != '\n') 113517350Seric { 113617350Seric if (i != EOF) 113723105Seric (void) ungetc(i, f); 113817350Seric i = '\r'; 113917350Seric } 114017350Seric } 114157135Seric if (--n <= 0) 114257135Seric { 114357135Seric /* allocate new space */ 114457135Seric char *nbp; 114557135Seric int nn; 114657135Seric 114757135Seric nn = (p - bp); 114857232Seric if (nn < MEMCHUNKSIZE) 114957135Seric nn *= 2; 115057135Seric else 115157232Seric nn += MEMCHUNKSIZE; 115257135Seric nbp = xalloc(nn); 115357135Seric bcopy(bp, nbp, p - bp); 115457135Seric p = &nbp[p - bp]; 115557135Seric if (bp != buf) 115657135Seric free(bp); 115757135Seric bp = nbp; 115857135Seric n = nn - (p - bp); 115957135Seric } 116057135Seric *p++ = i; 116117350Seric if (i == '\n') 116217350Seric { 116317350Seric LineNumber++; 116417350Seric i = getc(f); 116517350Seric if (i != EOF) 116623105Seric (void) ungetc(i, f); 116717350Seric if (i != ' ' && i != '\t') 116852647Seric break; 116917350Seric } 11707786Seric } 117157135Seric if (p == bp) 117252647Seric return (NULL); 117352647Seric *--p = '\0'; 117457135Seric return (bp); 11757786Seric } 11767860Seric /* 11777886Seric ** CURTIME -- return current time. 11787886Seric ** 11797886Seric ** Parameters: 11807886Seric ** none. 11817886Seric ** 11827886Seric ** Returns: 11837886Seric ** the current time. 11847886Seric ** 11857886Seric ** Side Effects: 11867886Seric ** none. 11877886Seric */ 11887886Seric 11897886Seric time_t 11907886Seric curtime() 11917886Seric { 11927886Seric auto time_t t; 11937886Seric 11947886Seric (void) time(&t); 11957886Seric return (t); 11967886Seric } 11978264Seric /* 11988264Seric ** ATOBOOL -- convert a string representation to boolean. 11998264Seric ** 12008264Seric ** Defaults to "TRUE" 12018264Seric ** 12028264Seric ** Parameters: 12038264Seric ** s -- string to convert. Takes "tTyY" as true, 12048264Seric ** others as false. 12058264Seric ** 12068264Seric ** Returns: 12078264Seric ** A boolean representation of the string. 12088264Seric ** 12098264Seric ** Side Effects: 12108264Seric ** none. 12118264Seric */ 12128264Seric 12138264Seric bool 12148264Seric atobool(s) 12158264Seric register char *s; 12168264Seric { 121763833Seric if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL) 12188264Seric return (TRUE); 12198264Seric return (FALSE); 12208264Seric } 12219048Seric /* 12229048Seric ** ATOOCT -- convert a string representation to octal. 12239048Seric ** 12249048Seric ** Parameters: 12259048Seric ** s -- string to convert. 12269048Seric ** 12279048Seric ** Returns: 12289048Seric ** An integer representing the string interpreted as an 12299048Seric ** octal number. 12309048Seric ** 12319048Seric ** Side Effects: 12329048Seric ** none. 12339048Seric */ 12349048Seric 12359048Seric atooct(s) 12369048Seric register char *s; 12379048Seric { 12389048Seric register int i = 0; 12399048Seric 12409048Seric while (*s >= '0' && *s <= '7') 12419048Seric i = (i << 3) | (*s++ - '0'); 12429048Seric return (i); 12439048Seric } 12449376Seric /* 12459376Seric ** WAITFOR -- wait for a particular process id. 12469376Seric ** 12479376Seric ** Parameters: 12489376Seric ** pid -- process id to wait for. 12499376Seric ** 12509376Seric ** Returns: 12519376Seric ** status of pid. 12529376Seric ** -1 if pid never shows up. 12539376Seric ** 12549376Seric ** Side Effects: 12559376Seric ** none. 12569376Seric */ 12579376Seric 125864562Seric int 12599376Seric waitfor(pid) 12609376Seric int pid; 12619376Seric { 126264562Seric #ifdef WAITUNION 126364562Seric union wait st; 126464562Seric #else 12659376Seric auto int st; 126664562Seric #endif 12679376Seric int i; 12689376Seric 12699376Seric do 12709376Seric { 12719376Seric errno = 0; 12729376Seric i = wait(&st); 12739376Seric } while ((i >= 0 || errno == EINTR) && i != pid); 12749376Seric if (i < 0) 127564562Seric return -1; 127664562Seric #ifdef WAITUNION 127764562Seric return st.w_status; 127864562Seric #else 127964562Seric return st; 128064562Seric #endif 12819376Seric } 12829376Seric /* 128310685Seric ** BITINTERSECT -- tell if two bitmaps intersect 128410685Seric ** 128510685Seric ** Parameters: 128610685Seric ** a, b -- the bitmaps in question 128710685Seric ** 128810685Seric ** Returns: 128910685Seric ** TRUE if they have a non-null intersection 129010685Seric ** FALSE otherwise 129110685Seric ** 129210685Seric ** Side Effects: 129310685Seric ** none. 129410685Seric */ 129510685Seric 129610685Seric bool 129710685Seric bitintersect(a, b) 129810685Seric BITMAP a; 129910685Seric BITMAP b; 130010685Seric { 130110685Seric int i; 130210685Seric 130310685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 130410685Seric if ((a[i] & b[i]) != 0) 130510685Seric return (TRUE); 130610685Seric return (FALSE); 130710685Seric } 130810685Seric /* 130910685Seric ** BITZEROP -- tell if a bitmap is all zero 131010685Seric ** 131110685Seric ** Parameters: 131210685Seric ** map -- the bit map to check 131310685Seric ** 131410685Seric ** Returns: 131510685Seric ** TRUE if map is all zero. 131610685Seric ** FALSE if there are any bits set in map. 131710685Seric ** 131810685Seric ** Side Effects: 131910685Seric ** none. 132010685Seric */ 132110685Seric 132210685Seric bool 132310685Seric bitzerop(map) 132410685Seric BITMAP map; 132510685Seric { 132610685Seric int i; 132710685Seric 132810685Seric for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 132910685Seric if (map[i] != 0) 133010685Seric return (FALSE); 133110685Seric return (TRUE); 133210685Seric } 133358247Seric /* 133458318Seric ** STRCONTAINEDIN -- tell if one string is contained in another 133558318Seric ** 133658318Seric ** Parameters: 133758318Seric ** a -- possible substring. 133858318Seric ** b -- possible superstring. 133958318Seric ** 134058318Seric ** Returns: 134158318Seric ** TRUE if a is contained in b. 134258318Seric ** FALSE otherwise. 134358318Seric */ 134458318Seric 134558318Seric bool 134658318Seric strcontainedin(a, b) 134758318Seric register char *a; 134858318Seric register char *b; 134958318Seric { 135065012Seric int la; 135165012Seric int lb; 135265012Seric int c; 135358318Seric 135465012Seric la = strlen(a); 135565012Seric lb = strlen(b); 135665012Seric c = *a; 135765012Seric if (isascii(c) && isupper(c)) 135865012Seric c = tolower(c); 135965012Seric for (; lb-- >= la; b++) 136058318Seric { 136165012Seric if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c) 136265012Seric continue; 136365012Seric if (strncasecmp(a, b, la) == 0) 136458318Seric return TRUE; 136558318Seric } 136665012Seric return FALSE; 136758318Seric } 136863753Seric /* 136963753Seric ** CHECKFD012 -- check low numbered file descriptors 137063753Seric ** 137163753Seric ** File descriptors 0, 1, and 2 should be open at all times. 137263753Seric ** This routine verifies that, and fixes it if not true. 137363753Seric ** 137463753Seric ** Parameters: 137563753Seric ** where -- a tag printed if the assertion failed 137663753Seric ** 137763753Seric ** Returns: 137863753Seric ** none 137963753Seric */ 138063753Seric 138163753Seric checkfd012(where) 138263753Seric char *where; 138363753Seric { 138463753Seric #ifdef XDEBUG 138563753Seric register int i; 138663753Seric struct stat stbuf; 138763753Seric 138863753Seric for (i = 0; i < 3; i++) 138963753Seric { 139064735Seric if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP) 139163753Seric { 139263753Seric /* oops.... */ 139363753Seric int fd; 139463753Seric 139563753Seric syserr("%s: fd %d not open", where, i); 139663753Seric fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666); 139763753Seric if (fd != i) 139863753Seric { 139963753Seric (void) dup2(fd, i); 140063753Seric (void) close(fd); 140163753Seric } 140263753Seric } 140363753Seric } 140463937Seric #endif /* XDEBUG */ 140563753Seric } 140664725Seric /* 140764725Seric ** PRINTOPENFDS -- print the open file descriptors (for debugging) 140864725Seric ** 140964725Seric ** Parameters: 141064725Seric ** logit -- if set, send output to syslog; otherwise 141164725Seric ** print for debugging. 141264725Seric ** 141364725Seric ** Returns: 141464725Seric ** none. 141564725Seric */ 141664725Seric 141764725Seric #include <arpa/inet.h> 141864725Seric 141964725Seric printopenfds(logit) 142064725Seric bool logit; 142164725Seric { 142264725Seric register int fd; 142364743Seric extern int DtableSize; 142464743Seric 142564743Seric for (fd = 0; fd < DtableSize; fd++) 142664743Seric dumpfd(fd, FALSE, logit); 142764743Seric } 142864743Seric /* 142964743Seric ** DUMPFD -- dump a file descriptor 143064743Seric ** 143164743Seric ** Parameters: 143264743Seric ** fd -- the file descriptor to dump. 143364743Seric ** printclosed -- if set, print a notification even if 143464743Seric ** it is closed; otherwise print nothing. 143564743Seric ** logit -- if set, send output to syslog instead of stdout. 143664743Seric */ 143764743Seric 143864743Seric dumpfd(fd, printclosed, logit) 143964743Seric int fd; 144064743Seric bool printclosed; 144164743Seric bool logit; 144264743Seric { 144364725Seric register char *p; 144468777Seric char *hp; 144566747Seric char *fmtstr; 144668777Seric SOCKADDR sa; 144764743Seric auto int slen; 144864743Seric struct stat st; 144964725Seric char buf[200]; 145068777Seric extern char *hostnamebyanyaddr(); 145164725Seric 145264743Seric p = buf; 145364743Seric sprintf(p, "%3d: ", fd); 145464743Seric p += strlen(p); 145564743Seric 145664743Seric if (fstat(fd, &st) < 0) 145764725Seric { 145864743Seric if (printclosed || errno != EBADF) 145964743Seric { 146064743Seric sprintf(p, "CANNOT STAT (%s)", errstring(errno)); 146164743Seric goto printit; 146264743Seric } 146364743Seric return; 146464743Seric } 146564725Seric 146664743Seric slen = fcntl(fd, F_GETFL, NULL); 146764743Seric if (slen != -1) 146864743Seric { 146964743Seric sprintf(p, "fl=0x%x, ", slen); 147064743Seric p += strlen(p); 147164743Seric } 147264725Seric 147364743Seric sprintf(p, "mode=%o: ", st.st_mode); 147464743Seric p += strlen(p); 147564743Seric switch (st.st_mode & S_IFMT) 147664743Seric { 147764807Seric #ifdef S_IFSOCK 147864743Seric case S_IFSOCK: 147964743Seric sprintf(p, "SOCK "); 148064725Seric p += strlen(p); 148168777Seric slen = sizeof sa; 148268777Seric if (getsockname(fd, &sa.sa, &slen) < 0) 148368810Seric sprintf(p, "(%s)", errstring(errno)); 148464743Seric else 148564725Seric { 148668777Seric hp = hostnamebyanyaddr(&sa); 148768777Seric if (sa.sa.sa_family == AF_INET) 148868777Seric sprintf(p, "%s/%d", hp, ntohs(sa.sin.sin_port)); 148968777Seric else 149068777Seric sprintf(p, "%s", hp); 149164743Seric } 149264743Seric p += strlen(p); 149364743Seric sprintf(p, "->"); 149464743Seric p += strlen(p); 149568777Seric slen = sizeof sa; 149668777Seric if (getpeername(fd, &sa.sa, &slen) < 0) 149768810Seric sprintf(p, "(%s)", errstring(errno)); 149864743Seric else 149964743Seric { 150068777Seric hp = hostnamebyanyaddr(&sa); 150168777Seric if (sa.sa.sa_family == AF_INET) 150268777Seric sprintf(p, "%s/%d", hp, ntohs(sa.sin.sin_port)); 150368777Seric else 150468777Seric sprintf(p, "%s", hp); 150564743Seric } 150664743Seric break; 150764807Seric #endif 150864725Seric 150964743Seric case S_IFCHR: 151064743Seric sprintf(p, "CHR: "); 151164743Seric p += strlen(p); 151264743Seric goto defprint; 151364725Seric 151464743Seric case S_IFBLK: 151564743Seric sprintf(p, "BLK: "); 151664743Seric p += strlen(p); 151764743Seric goto defprint; 151864725Seric 151966252Seric #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK) 152065378Seric case S_IFIFO: 152165378Seric sprintf(p, "FIFO: "); 152265378Seric p += strlen(p); 152365378Seric goto defprint; 152465378Seric #endif 152565378Seric 152665378Seric #ifdef S_IFDIR 152765378Seric case S_IFDIR: 152865378Seric sprintf(p, "DIR: "); 152965378Seric p += strlen(p); 153065378Seric goto defprint; 153165378Seric #endif 153265378Seric 153365378Seric #ifdef S_IFLNK 153465378Seric case S_IFLNK: 153565378Seric sprintf(p, "LNK: "); 153665378Seric p += strlen(p); 153765378Seric goto defprint; 153865378Seric #endif 153965378Seric 154064743Seric default: 154164725Seric defprint: 154266785Seric if (sizeof st.st_size > sizeof (long)) 154366751Seric fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd"; 154466747Seric else 154566751Seric fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld"; 154666747Seric sprintf(p, fmtstr, 154764743Seric major(st.st_dev), minor(st.st_dev), st.st_ino, 154864743Seric st.st_nlink, st.st_uid, st.st_gid, st.st_size); 154964743Seric break; 155064743Seric } 155164725Seric 155264743Seric printit: 155366748Seric #ifdef LOG 155464743Seric if (logit) 155565006Seric syslog(LOG_DEBUG, "%s", buf); 155664743Seric else 155766748Seric #endif 155864743Seric printf("%s\n", buf); 155964725Seric } 156065015Seric /* 156165015Seric ** SHORTENSTRING -- return short version of a string 156265015Seric ** 156365015Seric ** If the string is already short, just return it. If it is too 156465015Seric ** long, return the head and tail of the string. 156565015Seric ** 156665015Seric ** Parameters: 156765015Seric ** s -- the string to shorten. 156865015Seric ** m -- the max length of the string. 156965015Seric ** 157065015Seric ** Returns: 157165015Seric ** Either s or a short version of s. 157265015Seric */ 157365015Seric 157465015Seric #ifndef MAXSHORTSTR 157565055Seric # define MAXSHORTSTR 203 157665015Seric #endif 157765015Seric 157865015Seric char * 157965015Seric shortenstring(s, m) 158065015Seric register char *s; 158165015Seric int m; 158265015Seric { 158365015Seric int l; 158465015Seric static char buf[MAXSHORTSTR + 1]; 158565015Seric 158665015Seric l = strlen(s); 158765015Seric if (l < m) 158865015Seric return s; 158965015Seric if (m > MAXSHORTSTR) 159065015Seric m = MAXSHORTSTR; 159165015Seric else if (m < 10) 159265015Seric { 159365015Seric if (m < 5) 159465015Seric { 159565015Seric strncpy(buf, s, m); 159665015Seric buf[m] = '\0'; 159765015Seric return buf; 159865015Seric } 159965015Seric strncpy(buf, s, m - 3); 160065015Seric strcpy(buf + m - 3, "..."); 160165015Seric return buf; 160265015Seric } 160365015Seric m = (m - 3) / 2; 160465015Seric strncpy(buf, s, m); 160565015Seric strcpy(buf + m, "..."); 160665015Seric strcpy(buf + m + 3, s + l - m); 160765015Seric return buf; 160865015Seric } 160967848Seric /* 161069401Seric ** SHORTEN_HOSTNAME -- strip local domain information off of hostname. 161169401Seric ** 161269401Seric ** Parameters: 161369401Seric ** host -- the host to shorten (stripped in place). 161469401Seric ** 161569401Seric ** Returns: 161669401Seric ** none. 161769401Seric */ 161869401Seric 161969401Seric void 162069401Seric shorten_hostname(host) 162169401Seric char host[]; 162269401Seric { 162369401Seric register char *p; 162469401Seric char *mydom; 162569401Seric int i; 162669401Seric 162769401Seric /* strip off final dot */ 162869401Seric p = &host[strlen(host) - 1]; 162969401Seric if (*p == '.') 163069401Seric *p = '\0'; 163169401Seric 163269401Seric /* see if there is any domain at all -- if not, we are done */ 163369401Seric p = strchr(host, '.'); 163469401Seric if (p == NULL) 163569401Seric return; 163669401Seric 163769401Seric /* yes, we have a domain -- see if it looks like us */ 163869401Seric mydom = macvalue('m', CurEnv); 163969401Seric if (mydom == NULL) 164069401Seric mydom = ""; 164169401Seric i = strlen(++p); 164269401Seric if (strncasecmp(p, mydom, i) == 0 && 164369401Seric (mydom[i] == '.' || mydom[i] == '\0')) 164469401Seric *--p = '\0'; 164569401Seric } 164669401Seric /* 164769453Seric ** PROG_OPEN -- open a program for reading 164869453Seric ** 164969453Seric ** Parameters: 165069453Seric ** argv -- the argument list. 165169453Seric ** pfd -- pointer to a place to store the file descriptor. 165269453Seric ** e -- the current envelope. 165369453Seric ** 165469453Seric ** Returns: 165569453Seric ** pid of the process -- -1 if it failed. 165669453Seric */ 165769453Seric 165869453Seric int 165969453Seric prog_open(argv, pfd, e) 166069453Seric char **argv; 166169453Seric int *pfd; 166269453Seric ENVELOPE *e; 166369453Seric { 166469453Seric int pid; 166569453Seric int i; 166669453Seric int saveerrno; 166769453Seric int fdv[2]; 166869453Seric char *p, *q; 166969453Seric char buf[MAXLINE + 1]; 167069453Seric extern int DtableSize; 167169453Seric 167269453Seric if (pipe(fdv) < 0) 167369453Seric { 167469453Seric syserr("%s: cannot create pipe for stdout", argv[0]); 167569453Seric return -1; 167669453Seric } 167769453Seric pid = fork(); 167869453Seric if (pid < 0) 167969453Seric { 168069453Seric syserr("%s: cannot fork", argv[0]); 168169453Seric close(fdv[0]); 168269453Seric close(fdv[1]); 168369453Seric return -1; 168469453Seric } 168569453Seric if (pid > 0) 168669453Seric { 168769453Seric /* parent */ 168869453Seric close(fdv[1]); 168969453Seric *pfd = fdv[0]; 169069453Seric return pid; 169169453Seric } 169269453Seric 169369453Seric /* child -- close stdin */ 169469453Seric close(0); 169569453Seric 169669453Seric /* stdout goes back to parent */ 169769453Seric close(fdv[0]); 169869453Seric if (dup2(fdv[1], 1) < 0) 169969453Seric { 170069453Seric syserr("%s: cannot dup2 for stdout", argv[0]); 170169453Seric _exit(EX_OSERR); 170269453Seric } 170369453Seric close(fdv[1]); 170469453Seric 170569453Seric /* stderr goes to transcript if available */ 170669453Seric if (e->e_xfp != NULL) 170769453Seric { 170869453Seric if (dup2(fileno(e->e_xfp), 2) < 0) 170969453Seric { 171069453Seric syserr("%s: cannot dup2 for stderr", argv[0]); 171169453Seric _exit(EX_OSERR); 171269453Seric } 171369453Seric } 171469453Seric 171569453Seric /* this process has no right to the queue file */ 171669453Seric if (e->e_lockfp != NULL) 171769453Seric close(fileno(e->e_lockfp)); 171869453Seric 171969453Seric /* run as default user */ 172069453Seric setgid(DefGid); 172169453Seric setuid(DefUid); 172269453Seric 172369453Seric /* run in some directory */ 172469453Seric if (ProgMailer != NULL) 172569453Seric p = ProgMailer->m_execdir; 172669453Seric else 172769453Seric p = NULL; 172869453Seric for (; p != NULL; p = q) 172969453Seric { 173069453Seric q = strchr(p, ':'); 173169453Seric if (q != NULL) 173269453Seric *q = '\0'; 173369453Seric expand(p, buf, sizeof buf, e); 173469453Seric if (q != NULL) 173569453Seric *q++ = ':'; 173669453Seric if (buf[0] != '\0' && chdir(buf) >= 0) 173769453Seric break; 173869453Seric } 173969453Seric if (p == NULL) 174069453Seric { 174169453Seric /* backup directories */ 174269453Seric if (chdir("/tmp") < 0) 174369453Seric (void) chdir("/"); 174469453Seric } 174569453Seric 174669453Seric /* arrange for all the files to be closed */ 174769453Seric for (i = 3; i < DtableSize; i++) 174869453Seric { 174969453Seric register int j; 175069453Seric 175169453Seric if ((j = fcntl(i, F_GETFD, 0)) != -1) 175269453Seric (void) fcntl(i, F_SETFD, j | 1); 175369453Seric } 175469453Seric 175569453Seric /* now exec the process */ 175669476Seric execve(argv[0], (ARGV_T) argv, (ARGV_T) UserEnviron); 175769453Seric 175869453Seric /* woops! failed */ 175969453Seric saveerrno = errno; 176069453Seric syserr("%s: cannot exec", argv[0]); 176169453Seric if (transienterror(saveerrno)) 176269453Seric _exit(EX_OSERR); 176369453Seric _exit(EX_CONFIG); 176469453Seric } 176569453Seric /* 176668481Seric ** GET_COLUMN -- look up a Column in a line buffer 176768481Seric ** 176868481Seric ** Parameters: 176968481Seric ** line -- the raw text line to search. 177068481Seric ** col -- the column number to fetch. 177168481Seric ** delim -- the delimiter between columns. If null, 177268481Seric ** use white space. 177368481Seric ** buf -- the output buffer. 177468481Seric ** 177568481Seric ** Returns: 177668481Seric ** buf if successful. 177768481Seric ** NULL otherwise. 177868481Seric */ 177968481Seric 178068481Seric char * 178168481Seric get_column(line, col, delim, buf) 178268481Seric char line[]; 178368481Seric int col; 178468481Seric char delim; 178568481Seric char buf[]; 178668481Seric { 178768481Seric char *p; 178868481Seric char *begin, *end; 178968481Seric int i; 179068481Seric char delimbuf[3]; 179168481Seric 179268481Seric if (delim == '\0') 1793*69662Seric strcpy(delimbuf, "\n\t "); 179468481Seric else 179568481Seric { 179668481Seric delimbuf[0] = delim; 179768481Seric delimbuf[1] = '\0'; 179868481Seric } 179968481Seric 180068481Seric p = line; 180168481Seric if (*p == '\0') 180268481Seric return NULL; /* line empty */ 180368481Seric if (*p == delim && col == 0) 180468481Seric return NULL; /* first column empty */ 180568481Seric 180668481Seric begin = line; 180768481Seric 180868481Seric if (col == 0 && delim == '\0') 180968481Seric { 181068481Seric while (*begin && isspace(*begin)) 181168481Seric begin++; 181268481Seric } 181368481Seric 181468481Seric for (i = 0; i < col; i++) 181568481Seric { 181668481Seric if ((begin = strpbrk(begin, delimbuf)) == NULL) 181768481Seric return NULL; /* no such column */ 181868481Seric begin++; 181968481Seric if (delim == '\0') 182068481Seric { 182168481Seric while (*begin && isspace(*begin)) 182268481Seric begin++; 182368481Seric } 182468481Seric } 182568481Seric 182668481Seric end = strpbrk(begin, delimbuf); 182768481Seric if (end == NULL) 182868481Seric { 182968481Seric strcpy(buf, begin); 183068481Seric } 183168481Seric else 183268481Seric { 183368481Seric strncpy(buf, begin, end - begin); 183468481Seric buf[end - begin] = '\0'; 183568481Seric } 183668481Seric return buf; 183768481Seric } 183868481Seric /* 183968267Seric ** CLEANSTRCPY -- copy string keeping out bogus characters 184068267Seric ** 184168267Seric ** Parameters: 184268267Seric ** t -- "to" string. 184368267Seric ** f -- "from" string. 184468267Seric ** l -- length of space available in "to" string. 184568267Seric ** 184668267Seric ** Returns: 184768267Seric ** none. 184868267Seric */ 184968267Seric 185068267Seric void 185168267Seric cleanstrcpy(t, f, l) 185268267Seric register char *t; 185368267Seric register char *f; 185468267Seric int l; 185568267Seric { 185668281Seric #ifdef LOG 185768281Seric /* check for newlines and log if necessary */ 185868478Seric (void) denlstring(f, TRUE, TRUE); 185968281Seric #endif 186068281Seric 186168267Seric l--; 186268267Seric while (l > 0 && *f != '\0') 186368267Seric { 186468267Seric if (isascii(*f) && 186568267Seric (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL)) 186668267Seric { 186768267Seric l--; 186868267Seric *t++ = *f; 186968267Seric } 187068267Seric f++; 187168267Seric } 187268267Seric *t = '\0'; 187368267Seric } 187468267Seric /* 187568267Seric ** DENLSTRING -- convert newlines in a string to spaces 187668267Seric ** 187768267Seric ** Parameters: 187868267Seric ** s -- the input string 187968478Seric ** strict -- if set, don't permit continuation lines. 188068457Seric ** logattacks -- if set, log attempted attacks. 188168267Seric ** 188268267Seric ** Returns: 188368267Seric ** A pointer to a version of the string with newlines 188468267Seric ** mapped to spaces. This should be copied. 188568267Seric */ 188668267Seric 188768267Seric char * 188868478Seric denlstring(s, strict, logattacks) 188968267Seric char *s; 189068702Seric bool strict; 189168702Seric bool logattacks; 189268267Seric { 189368267Seric register char *p; 189468267Seric int l; 189568267Seric static char *bp = NULL; 189668267Seric static int bl = 0; 189768267Seric 189868478Seric p = s; 189968478Seric while ((p = strchr(p, '\n')) != NULL) 190068478Seric if (strict || (*++p != ' ' && *p != '\t')) 190168478Seric break; 190268478Seric if (p == NULL) 190368267Seric return s; 190468267Seric 190568267Seric l = strlen(s) + 1; 190668267Seric if (bl < l) 190768267Seric { 190868267Seric /* allocate more space */ 190968267Seric if (bp != NULL) 191068267Seric free(bp); 191168267Seric bp = xalloc(l); 191268267Seric bl = l; 191368267Seric } 191468267Seric strcpy(bp, s); 191568267Seric for (p = bp; (p = strchr(p, '\n')) != NULL; ) 191668267Seric *p++ = ' '; 191768281Seric 191868481Seric /* 191968281Seric #ifdef LOG 192068457Seric if (logattacks) 192168457Seric { 192268457Seric syslog(LOG_NOTICE, "POSSIBLE ATTACK from %s: newline in string \"%s\"", 192368457Seric RealHostName == NULL ? "[UNKNOWN]" : RealHostName, 192468457Seric shortenstring(bp, 80)); 192568457Seric } 192668281Seric #endif 192768481Seric */ 192868281Seric 192968267Seric return bp; 193068267Seric } 1931