122698Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333728Sbostic * Copyright (c) 1988 Regents of the University of California. 433728Sbostic * All rights reserved. 533728Sbostic * 642825Sbostic * %sccs.include.redist.c% 733728Sbostic */ 822698Sdist 922698Sdist #ifndef lint 10*58366Seric static char sccsid[] = "@(#)conf.c 6.32 (Berkeley) 03/02/93"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1314881Seric # include <sys/ioctl.h> 1458082Seric # include <sys/param.h> 1558153Seric # include <signal.h> 1636928Sbostic # include <pwd.h> 173309Seric # include "sendmail.h" 1840980Sbostic # include "pathnames.h" 19404Seric 20294Seric /* 213309Seric ** CONF.C -- Sendmail Configuration Tables. 22294Seric ** 23294Seric ** Defines the configuration of this installation. 24294Seric ** 251388Seric ** Configuration Variables: 262897Seric ** HdrInfo -- a table describing well-known header fields. 272897Seric ** Each entry has the field name and some flags, 284147Seric ** which are described in sendmail.h. 294093Seric ** 304093Seric ** Notes: 314093Seric ** I have tried to put almost all the reasonable 324093Seric ** configuration information into the configuration 334093Seric ** file read at runtime. My intent is that anything 344093Seric ** here is a function of the version of UNIX you 354093Seric ** are running, or is really static -- for example 364093Seric ** the headers are a superset of widely used 374093Seric ** protocols. If you find yourself playing with 384093Seric ** this file too much, you may be making a mistake! 39294Seric */ 40294Seric 41294Seric 42294Seric 43294Seric 444437Seric /* 452897Seric ** Header info table 463057Seric ** Final (null) entry contains the flags used for any other field. 474147Seric ** 484147Seric ** Not all of these are actually handled specially by sendmail 494147Seric ** at this time. They are included as placeholders, to let 504147Seric ** you know that "someday" I intend to have sendmail do 514147Seric ** something with them. 522897Seric */ 532897Seric 542897Seric struct hdrinfo HdrInfo[] = 552897Seric { 568060Seric /* originator fields, most to least significant */ 5711417Seric "resent-sender", H_FROM|H_RESENT, 5811417Seric "resent-from", H_FROM|H_RESENT, 5925686Seric "resent-reply-to", H_FROM|H_RESENT, 609055Seric "sender", H_FROM, 619055Seric "from", H_FROM, 6225686Seric "reply-to", H_FROM, 639055Seric "full-name", H_ACHECK, 6457359Seric "return-receipt-to", H_FROM /* |H_RECEIPTTO */, 6557359Seric "errors-to", H_FROM|H_ERRORSTO, 668060Seric /* destination fields */ 679055Seric "to", H_RCPT, 6811417Seric "resent-to", H_RCPT|H_RESENT, 699055Seric "cc", H_RCPT, 7011417Seric "resent-cc", H_RCPT|H_RESENT, 719055Seric "bcc", H_RCPT|H_ACHECK, 7211417Seric "resent-bcc", H_RCPT|H_ACHECK|H_RESENT, 7356215Seric "apparently-to", H_RCPT, 748060Seric /* message identification and control */ 7511417Seric "message-id", 0, 7611417Seric "resent-message-id", H_RESENT, 779055Seric "message", H_EOH, 789055Seric "text", H_EOH, 7911417Seric /* date fields */ 8011417Seric "date", 0, 8111417Seric "resent-date", H_RESENT, 828060Seric /* trace fields */ 839055Seric "received", H_TRACE|H_FORCE, 849055Seric "via", H_TRACE|H_FORCE, 859055Seric "mail-from", H_TRACE|H_FORCE, 868060Seric 879055Seric NULL, 0, 882897Seric }; 894166Seric 904166Seric 914166Seric 924282Seric /* 934282Seric ** Location of system files/databases/etc. 944282Seric */ 954282Seric 9640980Sbostic char *ConfFile = _PATH_SENDMAILCF; /* runtime configuration */ 9740980Sbostic char *FreezeFile = _PATH_SENDMAILFC; /* frozen version of above */ 9858082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 999039Seric 1009064Seric 1019064Seric 1029039Seric /* 10358082Seric ** Privacy values 10458082Seric */ 10558082Seric 10658082Seric struct prival PrivacyValues[] = 10758082Seric { 10858082Seric "public", PRIV_PUBLIC, 10958082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11058114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11158082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 11258082Seric "noexpn", PRIV_NOEXPN, 11358082Seric "novrfy", PRIV_NOVRFY, 11458249Seric "restrictmailq", PRIV_RESTRMAILQ, 11558082Seric "goaway", PRIV_GOAWAY, 11658082Seric NULL, PRIV_PUBLIC, 11758082Seric }; 11858082Seric 11958082Seric 12058082Seric 12158082Seric /* 12224943Seric ** Miscellaneous stuff. 1239039Seric */ 1249039Seric 12524943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 12624943Seric /* 12724943Seric ** SETDEFAULTS -- set default values 12824943Seric ** 12924943Seric ** Because of the way freezing is done, these must be initialized 13024943Seric ** using direct code. 13124943Seric ** 13224943Seric ** Parameters: 13324943Seric ** none. 13424943Seric ** 13524943Seric ** Returns: 13624943Seric ** none. 13724943Seric ** 13824943Seric ** Side Effects: 13924943Seric ** Initializes a bunch of global variables to their 14024943Seric ** default values. 14124943Seric */ 14224943Seric 14324943Seric setdefaults() 14424943Seric { 14557438Seric SpaceSub = ' '; /* option B */ 14657438Seric QueueLA = 8; /* option x */ 14757438Seric RefuseLA = 12; /* option X */ 14857438Seric WkRecipFact = 30000L; /* option y */ 14957438Seric WkClassFact = 1800L; /* option z */ 15057438Seric WkTimeFact = 90000L; /* option Z */ 15157438Seric QueueFactor = WkRecipFact * 20; /* option q */ 15257142Seric FileMode = (getuid() != geteuid()) ? 0644 : 0600; 15357438Seric /* option F */ 15457438Seric DefUid = 1; /* option u */ 15557438Seric DefGid = 1; /* option g */ 15657438Seric CheckpointInterval = 10; /* option C */ 15757438Seric MaxHopCount = 25; /* option h */ 15857438Seric SendMode = SM_FORK; /* option d */ 15957438Seric ErrorMode = EM_PRINT; /* option e */ 16057438Seric EightBit = FALSE; /* option 8 */ 16157438Seric MaxMciCache = 1; /* option k */ 16257438Seric MciCacheTimeout = 300; /* option K */ 16357438Seric LogLevel = 9; /* option L */ 16458112Seric settimeouts(NULL); /* option r */ 16558112Seric TimeOut = 5 * 24 * 60 * 60; /* option T */ 16640973Sbostic setdefuser(); 16753654Seric setupmaps(); 16857402Seric setupmailers(); 16924943Seric } 170294Seric 17140973Sbostic 1724326Seric /* 17340973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 17440973Sbostic */ 17540973Sbostic 17640973Sbostic setdefuser() 17740973Sbostic { 17840973Sbostic struct passwd *defpwent; 17957386Seric static char defuserbuf[40]; 18040973Sbostic 18157386Seric DefUser = defuserbuf; 18240973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 18357386Seric strcpy(defuserbuf, defpwent->pw_name); 18440973Sbostic else 18557386Seric strcpy(defuserbuf, "nobody"); 18640973Sbostic } 18753654Seric /* 18853654Seric ** SETUPMAPS -- set up map classes 18953654Seric ** 19053654Seric ** Since these are compiled in, they cannot be in the config file. 19153654Seric ** 19253654Seric */ 19340973Sbostic 19453654Seric setupmaps() 19553654Seric { 19653654Seric register STAB *s; 19756836Seric extern bool host_map_init(); 19853654Seric extern char *maphostname(); 19940973Sbostic 20053654Seric /* set up host name lookup map */ 20153654Seric s = stab("host", ST_MAPCLASS, ST_ENTER); 20256836Seric s->s_mapclass.map_init = host_map_init; 20353654Seric s->s_mapclass.map_lookup = maphostname; 20453654Seric 20553654Seric /* 20653654Seric ** Set up other map classes. 20753654Seric */ 20853654Seric 20953654Seric # ifdef DBM_MAP 21053654Seric /* dbm file access */ 21153654Seric { 21256836Seric extern bool dbm_map_init(); 21353654Seric extern char *dbm_map_lookup(); 21453654Seric 21553654Seric s = stab("dbm", ST_MAPCLASS, ST_ENTER); 21653654Seric s->s_mapclass.map_init = dbm_map_init; 21753654Seric s->s_mapclass.map_lookup = dbm_map_lookup; 21853654Seric } 21953654Seric # endif 22053654Seric 22153654Seric # ifdef BTREE_MAP 22253654Seric /* new database file access -- btree files */ 22353654Seric { 22456823Seric extern bool bt_map_init(); 22556823Seric extern char *db_map_lookup(); 22653654Seric 22753654Seric s = stab("btree", ST_MAPCLASS, ST_ENTER); 22853654Seric s->s_mapclass.map_init = bt_map_init; 22956823Seric s->s_mapclass.map_lookup = db_map_lookup; 23053654Seric } 23153654Seric # endif 23253654Seric 23353654Seric # ifdef HASH_MAP 23453654Seric /* new database file access -- hash files */ 23553654Seric { 23656823Seric extern bool hash_map_init(); 23756823Seric extern char *db_map_lookup(); 23853654Seric 23953654Seric s = stab("hash", ST_MAPCLASS, ST_ENTER); 24053654Seric s->s_mapclass.map_init = hash_map_init; 24156823Seric s->s_mapclass.map_lookup = db_map_lookup; 24253654Seric } 24353654Seric # endif 24453654Seric 24557208Seric # ifdef NIS_MAP 24657208Seric /* NIS map access */ 24757208Seric { 24857208Seric extern bool nis_map_init(); 24957208Seric extern char *nis_map_lookup(); 25057208Seric 25157208Seric s = stab("nis", ST_MAPCLASS, ST_ENTER); 25257208Seric s->s_mapclass.map_init = nis_map_init; 25357208Seric s->s_mapclass.map_lookup = nis_map_lookup; 25457208Seric } 25557208Seric # endif 25657208Seric 25753654Seric # ifdef USERDB_MAP 25853654Seric /* user database */ 25953654Seric { 26056836Seric extern bool udb_map_init(); 26153654Seric extern char *udb_map_lookup(); 26253654Seric 26353654Seric s = stab("udb", ST_MAPCLASS, ST_ENTER); 26453654Seric s->s_mapclass.map_init = udb_map_init; 26553654Seric s->s_mapclass.map_lookup = udb_map_lookup; 26653654Seric } 26753654Seric # endif 26853654Seric } 26953654Seric /* 27056836Seric ** HOST_MAP_INIT -- initialize host class structures 27156836Seric */ 27256836Seric 27356836Seric bool 27456836Seric host_map_init(map, mapname, args) 27556836Seric MAP *map; 27656836Seric char *mapname; 27756836Seric char *args; 27856836Seric { 27956836Seric register char *p = args; 28056836Seric 28156836Seric for (;;) 28256836Seric { 28358050Seric while (isascii(*p) && isspace(*p)) 28456836Seric p++; 28556836Seric if (*p != '-') 28656836Seric break; 28756836Seric switch (*++p) 28856836Seric { 28956836Seric case 'a': 29056836Seric map->map_app = ++p; 29156836Seric break; 29256836Seric } 29358050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 29456836Seric p++; 29556836Seric if (*p != '\0') 29656836Seric *p++ = '\0'; 29756836Seric } 29856836Seric if (map->map_app != NULL) 29956836Seric map->map_app = newstr(map->map_app); 30056836Seric return TRUE; 30156836Seric } 30257402Seric /* 30357402Seric ** SETUPMAILERS -- initialize default mailers 30457402Seric */ 30556836Seric 30657402Seric setupmailers() 30757402Seric { 30857402Seric char buf[100]; 30957402Seric 31057403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 31157403Seric makemailer(buf); 31257403Seric 31357402Seric strcpy(buf, "*file*, P=/dev/null, F=lsDEu, A=FILE"); 31457402Seric makemailer(buf); 31557402Seric 31657402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 31757402Seric makemailer(buf); 31857402Seric } 31956836Seric /* 3204326Seric ** GETRUID -- get real user id (V7) 3214326Seric */ 3224326Seric 3234326Seric getruid() 3244326Seric { 3259274Seric if (OpMode == MD_DAEMON) 3264536Seric return (RealUid); 3274536Seric else 3284536Seric return (getuid()); 3294326Seric } 3304326Seric 3314326Seric 3324326Seric /* 3334326Seric ** GETRGID -- get real group id (V7). 3344326Seric */ 3354326Seric 3364326Seric getrgid() 3374326Seric { 3389274Seric if (OpMode == MD_DAEMON) 3394536Seric return (RealGid); 3404536Seric else 3414536Seric return (getgid()); 3424326Seric } 34353654Seric /* 3449369Seric ** USERNAME -- return the user id of the logged in user. 3459369Seric ** 3469369Seric ** Parameters: 3479369Seric ** none. 3489369Seric ** 3499369Seric ** Returns: 3509369Seric ** The login name of the logged in user. 3519369Seric ** 3529369Seric ** Side Effects: 3539369Seric ** none. 3549369Seric ** 3559369Seric ** Notes: 3569369Seric ** The return value is statically allocated. 3579369Seric */ 3589369Seric 3599369Seric char * 3609369Seric username() 3619369Seric { 36217469Seric static char *myname = NULL; 3639369Seric extern char *getlogin(); 36419904Smiriam register struct passwd *pw; 3659369Seric 36617469Seric /* cache the result */ 36717469Seric if (myname == NULL) 36817469Seric { 36917469Seric myname = getlogin(); 37017469Seric if (myname == NULL || myname[0] == '\0') 37117469Seric { 37217469Seric 37317469Seric pw = getpwuid(getruid()); 37417469Seric if (pw != NULL) 37540993Sbostic myname = newstr(pw->pw_name); 37617469Seric } 37719904Smiriam else 37819904Smiriam { 37919873Smiriam 38040993Sbostic myname = newstr(myname); 38140993Sbostic if ((pw = getpwnam(myname)) == NULL || 38240993Sbostic getuid() != pw->pw_uid) 38319904Smiriam { 38419873Smiriam pw = getpwuid(getuid()); 38524945Seric if (pw != NULL) 38640993Sbostic myname = newstr(pw->pw_name); 38719873Smiriam } 38819873Smiriam } 38917469Seric if (myname == NULL || myname[0] == '\0') 39017469Seric { 39158151Seric syserr("554 Who are you?"); 39217469Seric myname = "postmaster"; 39317469Seric } 39417469Seric } 39517469Seric 39617469Seric return (myname); 3979369Seric } 3989369Seric /* 3994190Seric ** TTYPATH -- Get the path of the user's tty 400294Seric ** 401294Seric ** Returns the pathname of the user's tty. Returns NULL if 402294Seric ** the user is not logged in or if s/he has write permission 403294Seric ** denied. 404294Seric ** 405294Seric ** Parameters: 406294Seric ** none 407294Seric ** 408294Seric ** Returns: 409294Seric ** pathname of the user's tty. 410294Seric ** NULL if not logged in or write permission denied. 411294Seric ** 412294Seric ** Side Effects: 413294Seric ** none. 414294Seric ** 415294Seric ** WARNING: 416294Seric ** Return value is in a local buffer. 417294Seric ** 418294Seric ** Called By: 419294Seric ** savemail 420294Seric */ 421294Seric 422294Seric # include <sys/stat.h> 423294Seric 424294Seric char * 425294Seric ttypath() 426294Seric { 427294Seric struct stat stbuf; 428294Seric register char *pathn; 429294Seric extern char *ttyname(); 4304081Seric extern char *getlogin(); 431294Seric 432294Seric /* compute the pathname of the controlling tty */ 4339369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4349369Seric (pathn = ttyname(0)) == NULL) 435294Seric { 436294Seric errno = 0; 437294Seric return (NULL); 438294Seric } 439294Seric 440294Seric /* see if we have write permission */ 4412967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 442294Seric { 443294Seric errno = 0; 444294Seric return (NULL); 445294Seric } 446294Seric 447294Seric /* see if the user is logged in */ 448294Seric if (getlogin() == NULL) 449294Seric return (NULL); 450294Seric 451294Seric /* looks good */ 452294Seric return (pathn); 453294Seric } 4542967Seric /* 4552967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4562967Seric ** 4572967Seric ** This routine can be supplied on a per-installation basis 4582967Seric ** to determine whether a person is allowed to send a message. 4592967Seric ** This allows restriction of certain types of internet 4602967Seric ** forwarding or registration of users. 4612967Seric ** 4622967Seric ** If the hosts are found to be incompatible, an error 46357454Seric ** message should be given using "usrerr" and 0 should 4642967Seric ** be returned. 4652967Seric ** 4664288Seric ** 'NoReturn' can be set to suppress the return-to-sender 4674288Seric ** function; this should be done on huge messages. 4684288Seric ** 4692967Seric ** Parameters: 4702967Seric ** to -- the person being sent to. 4712967Seric ** 4722967Seric ** Returns: 47357459Seric ** an exit status 4742967Seric ** 4752967Seric ** Side Effects: 4762967Seric ** none (unless you include the usrerr stuff) 4772967Seric */ 4782967Seric 47955012Seric checkcompat(to, e) 4802967Seric register ADDRESS *to; 48155012Seric register ENVELOPE *e; 4822967Seric { 48312133Seric # ifdef lint 48412133Seric if (to == NULL) 48512133Seric to++; 48612133Seric # endif lint 48710698Seric # ifdef EXAMPLE_CODE 48810698Seric /* this code is intended as an example only */ 4894437Seric register STAB *s; 4904437Seric 4914437Seric s = stab("arpa", ST_MAILER, ST_FIND); 49255012Seric if (s != NULL && e->e_from.q_mailer != LocalMailer && 4939369Seric to->q_mailer == s->s_mailer) 4944437Seric { 49558151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 49610698Seric /* NoReturn = TRUE; to supress return copy */ 49757459Seric return (EX_UNAVAILABLE); 4984437Seric } 49956795Seric # endif /* EXAMPLE_CODE */ 50057459Seric return (EX_OK); 5012967Seric } 5029369Seric /* 5039369Seric ** HOLDSIGS -- arrange to hold all signals 5049369Seric ** 5059369Seric ** Parameters: 5069369Seric ** none. 5079369Seric ** 5089369Seric ** Returns: 5099369Seric ** none. 5109369Seric ** 5119369Seric ** Side Effects: 5129369Seric ** Arranges that signals are held. 5139369Seric */ 5149369Seric 5159369Seric holdsigs() 5169369Seric { 5179369Seric } 5189369Seric /* 5199369Seric ** RLSESIGS -- arrange to release all signals 5209369Seric ** 5219369Seric ** This undoes the effect of holdsigs. 5229369Seric ** 5239369Seric ** Parameters: 5249369Seric ** none. 5259369Seric ** 5269369Seric ** Returns: 5279369Seric ** none. 5289369Seric ** 5299369Seric ** Side Effects: 5309369Seric ** Arranges that signals are released. 5319369Seric */ 5329369Seric 5339369Seric rlsesigs() 5349369Seric { 5359369Seric } 53614872Seric /* 53714872Seric ** GETLA -- get the current load average 53814872Seric ** 53914881Seric ** This code stolen from la.c. 54014881Seric ** 54114872Seric ** Parameters: 54214872Seric ** none. 54314872Seric ** 54414872Seric ** Returns: 54514872Seric ** The current load average as an integer. 54614872Seric ** 54714872Seric ** Side Effects: 54814872Seric ** none. 54914872Seric */ 55014872Seric 55151920Seric /* try to guess what style of load average we have */ 55251920Seric #define LA_ZERO 1 /* always return load average as zero */ 55351920Seric #define LA_INT 2 /* read kmem for avenrun; interpret as int */ 55451920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 55551920Seric #define LA_SUBR 4 /* call getloadavg */ 55614872Seric 55751920Seric #ifndef LA_TYPE 55851920Seric # if defined(sun) 55951920Seric # define LA_TYPE LA_INT 56051920Seric # endif 56157977Seric # if defined(mips) || defined(__alpha) 56257977Seric /* Ultrix or OSF/1 or RISC/os */ 56351920Seric # define LA_TYPE LA_INT 56451920Seric # define LA_AVENRUN "avenrun" 56551920Seric # endif 56651920Seric # if defined(hpux) 56751920Seric # define LA_TYPE LA_FLOAT 56851920Seric # endif 56951920Seric 57051920Seric # ifndef LA_TYPE 57157736Seric # if defined(SYSTEM5) 57257736Seric # define LA_TYPE LA_INT 57357736Seric # define LA_AVENRUN "avenrun" 57457736Seric # else 57557736Seric # if defined(BSD) 57657736Seric # define LA_TYPE LA_SUBR 57757736Seric # else 57857736Seric # define LA_TYPE LA_ZERO 57957736Seric # endif 58057736Seric # endif 58151920Seric # endif 58251920Seric #endif 58351920Seric 58451920Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) 58551920Seric 58614872Seric #include <nlist.h> 58751920Seric #include <fcntl.h> 58814872Seric 58951920Seric #ifndef LA_AVENRUN 59051920Seric #define LA_AVENRUN "_avenrun" 59151920Seric #endif 59251920Seric 59351920Seric /* _PATH_UNIX should be defined in <paths.h> */ 59451920Seric #ifndef _PATH_UNIX 59551920Seric # if defined(hpux) 59651920Seric # define _PATH_UNIX "/hp-ux" 59751920Seric # endif 59851920Seric # if defined(mips) && !defined(ultrix) 59951920Seric /* powerful RISC/os */ 60051920Seric # define _PATH_UNIX "/unix" 60151920Seric # endif 60257736Seric # if defined(SYSTEM5) 60357977Seric # ifndef _PATH_UNIX 60457977Seric # define _PATH_UNIX "/unix" 60557977Seric # endif 60657736Seric # endif 60751920Seric # ifndef _PATH_UNIX 60851920Seric # define _PATH_UNIX "/vmunix" 60951920Seric # endif 61051920Seric #endif 61151920Seric 61214872Seric struct nlist Nl[] = 61314872Seric { 61451920Seric { LA_AVENRUN }, 61514872Seric #define X_AVENRUN 0 61614872Seric { 0 }, 61714872Seric }; 61814872Seric 61957736Seric #if defined(unixpc) 62057736Seric # define FSHIFT 5 62157736Seric #endif 62257736Seric 62357977Seric #if defined(__alpha) 62457977Seric # define FSHIFT 10 62557977Seric #endif 62657977Seric 62751920Seric #if (LA_TYPE == LA_INT) && !defined(FSHIFT) 62851920Seric # define FSHIFT 8 62957736Seric #endif 63057736Seric #if (LA_TYPE == LA_INT) && !defined(FSCALE) 63151920Seric # define FSCALE (1 << FSHIFT) 63251920Seric #endif 63340930Srick 63414872Seric getla() 63514872Seric { 63614872Seric static int kmem = -1; 63751920Seric #if LA_TYPE == LA_INT 63824943Seric long avenrun[3]; 63951920Seric #else 64051920Seric double avenrun[3]; 64151920Seric #endif 64225615Seric extern off_t lseek(); 64357736Seric extern char *errstring(); 64457736Seric extern int errno; 64514872Seric 64614872Seric if (kmem < 0) 64714872Seric { 64824945Seric kmem = open("/dev/kmem", 0, 0); 64914872Seric if (kmem < 0) 65057736Seric { 65157736Seric if (tTd(3, 1)) 65257736Seric printf("getla: open(/dev/kmem): %s\n", 65357736Seric errstring(errno)); 65414872Seric return (-1); 65557736Seric } 65651920Seric (void) fcntl(kmem, F_SETFD, 1); 65757736Seric if (nlist(_PATH_UNIX, Nl) < 0) 65857736Seric { 65957736Seric if (tTd(3, 1)) 66057736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 66157736Seric errstring(errno)); 66214872Seric return (-1); 66357736Seric } 66414872Seric } 66557736Seric if (tTd(3, 20)) 66657736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 66724945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 66823118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 66919967Seric { 67019967Seric /* thank you Ian */ 67157736Seric if (tTd(3, 1)) 67257736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 67319967Seric return (-1); 67419967Seric } 67551920Seric #if LA_TYPE == LA_INT 67657736Seric if (tTd(3, 5)) 67757736Seric { 67857736Seric printf("getla: avenrun = %d", avenrun[0]); 67957736Seric if (tTd(3, 15)) 68057736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 68157736Seric printf("\n"); 68257736Seric } 68357736Seric if (tTd(3, 1)) 68457736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 68524943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 68651920Seric #else 68757736Seric if (tTd(3, 5)) 68857736Seric { 68957736Seric printf("getla: avenrun = %g", avenrun[0]); 69057736Seric if (tTd(3, 15)) 69157736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 69257736Seric printf("\n"); 69357736Seric } 69457736Seric if (tTd(3, 1)) 69557736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 69651920Seric return ((int) (avenrun[0] + 0.5)); 69751920Seric #endif 69814872Seric } 69914872Seric 70051773Seric #else 70151920Seric #if LA_TYPE == LA_SUBR 70251773Seric 70351773Seric getla() 70451773Seric { 70551920Seric double avenrun[3]; 70651920Seric 70751920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 70857736Seric { 70957736Seric if (tTd(3, 1)) 71057736Seric perror("getla: getloadavg failed:"); 71151920Seric return (-1); 71257736Seric } 71357736Seric if (tTd(3, 1)) 71457736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 71551920Seric return ((int) (avenrun[0] + 0.5)); 71651773Seric } 71751773Seric 71851773Seric #else 71951773Seric 72051773Seric getla() 72151773Seric { 72257736Seric if (tTd(3, 1)) 72357736Seric printf("getla: ZERO\n"); 72451920Seric return (0); 72551773Seric } 72651773Seric 72751773Seric #endif 72851773Seric #endif 72924943Seric /* 73024943Seric ** SHOULDQUEUE -- should this message be queued or sent? 73124943Seric ** 73224943Seric ** Compares the message cost to the load average to decide. 73324943Seric ** 73424943Seric ** Parameters: 73524943Seric ** pri -- the priority of the message in question. 73657438Seric ** ctime -- the message creation time. 73724943Seric ** 73824943Seric ** Returns: 73924943Seric ** TRUE -- if this message should be queued up for the 74024943Seric ** time being. 74124943Seric ** FALSE -- if the load is low enough to send this message. 74224943Seric ** 74324943Seric ** Side Effects: 74424943Seric ** none. 74524943Seric */ 74624943Seric 74724943Seric bool 74857438Seric shouldqueue(pri, ctime) 74924943Seric long pri; 75057438Seric time_t ctime; 75124943Seric { 75251920Seric if (CurrentLA < QueueLA) 75324943Seric return (FALSE); 75458132Seric if (CurrentLA >= RefuseLA) 75558132Seric return (TRUE); 75651920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 75724943Seric } 75824943Seric /* 75953037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 76053037Seric ** 76153037Seric ** Parameters: 76253037Seric ** none. 76353037Seric ** 76453037Seric ** Returns: 76553037Seric ** TRUE if incoming SMTP connections should be refused 76653037Seric ** (for now). 76753037Seric ** FALSE if we should accept new work. 76853037Seric ** 76953037Seric ** Side Effects: 77053037Seric ** none. 77153037Seric */ 77253037Seric 77353037Seric bool 77453037Seric refuseconnections() 77553037Seric { 77653037Seric /* this is probably too simplistic */ 77758132Seric return (CurrentLA >= RefuseLA); 77853037Seric } 77953037Seric /* 78024943Seric ** SETPROCTITLE -- set process title for ps 78124943Seric ** 78224943Seric ** Parameters: 78324943Seric ** fmt -- a printf style format string. 78424943Seric ** a, b, c -- possible parameters to fmt. 78524943Seric ** 78624943Seric ** Returns: 78724943Seric ** none. 78824943Seric ** 78924943Seric ** Side Effects: 79024943Seric ** Clobbers argv of our main procedure so ps(1) will 79124943Seric ** display the title. 79224943Seric */ 79324943Seric 79424943Seric /*VARARGS1*/ 79557642Seric #ifdef __STDC__ 79657642Seric setproctitle(char *fmt, ...) 79757642Seric #else 79857642Seric setproctitle(fmt, va_alist) 79924943Seric char *fmt; 80057642Seric va_dcl 80157642Seric #endif 80224943Seric { 80324943Seric # ifdef SETPROCTITLE 80424943Seric register char *p; 80525049Seric register int i; 80656852Seric char buf[MAXLINE]; 80756852Seric VA_LOCAL_DECL 80824943Seric extern char **Argv; 80924943Seric extern char *LastArgv; 81024943Seric 81154996Seric p = buf; 81224943Seric 81354996Seric /* print sendmail: heading for grep */ 81454996Seric (void) strcpy(p, "sendmail: "); 81554996Seric p += strlen(p); 81624943Seric 81754996Seric /* print the argument string */ 81856852Seric VA_START(fmt); 81956852Seric (void) vsprintf(p, fmt, ap); 82056852Seric VA_END; 82154996Seric 82225049Seric i = strlen(buf); 82354996Seric if (i > LastArgv - Argv[0] - 2) 82425049Seric { 82554996Seric i = LastArgv - Argv[0] - 2; 82625049Seric buf[i] = '\0'; 82725049Seric } 82854996Seric (void) strcpy(Argv[0], buf); 82954997Seric p = &Argv[0][i]; 83024943Seric while (p < LastArgv) 83124943Seric *p++ = ' '; 83256795Seric # endif /* SETPROCTITLE */ 83324943Seric } 83425698Seric /* 83525698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 83625698Seric ** 83725698Seric ** Parameters: 83825698Seric ** none. 83925698Seric ** 84025698Seric ** Returns: 84125698Seric ** none. 84225698Seric ** 84325698Seric ** Side Effects: 84425698Seric ** Picks up extant zombies. 84525698Seric */ 84625698Seric 84725698Seric # include <sys/wait.h> 84825698Seric 84946928Sbostic void 85025698Seric reapchild() 85125698Seric { 85225698Seric # ifdef WNOHANG 85325698Seric union wait status; 85425698Seric 85546928Sbostic while (wait3((int *)&status, WNOHANG, (struct rusage *) NULL) > 0) 85625698Seric continue; 85756795Seric # else /* WNOHANG */ 85825698Seric auto int status; 85925698Seric 86046928Sbostic while (wait((int *)&status) > 0) 86125698Seric continue; 86256795Seric # endif /* WNOHANG */ 86358061Seric # ifdef SYSTEM5 86458061Seric (void) signal(SIGCHLD, reapchild); 86558061Seric # endif 86625698Seric } 86755418Seric /* 86855418Seric ** UNSETENV -- remove a variable from the environment 86955418Seric ** 87055418Seric ** Not needed on newer systems. 87155418Seric ** 87255418Seric ** Parameters: 87355418Seric ** name -- the string name of the environment variable to be 87455418Seric ** deleted from the current environment. 87555418Seric ** 87655418Seric ** Returns: 87755418Seric ** none. 87855418Seric ** 87955418Seric ** Globals: 88055418Seric ** environ -- a pointer to the current environment. 88155418Seric ** 88255418Seric ** Side Effects: 88355418Seric ** Modifies environ. 88455418Seric */ 88555418Seric 88655418Seric #ifdef UNSETENV 88755418Seric 88855418Seric void 88955418Seric unsetenv(name) 89055418Seric char *name; 89155418Seric { 89255418Seric extern char **environ; 89355418Seric register char **pp; 89455418Seric int len = strlen(name); 89555418Seric 89655418Seric for (pp = environ; *pp != NULL; pp++) 89755418Seric { 89855418Seric if (strncmp(name, *pp, len) == 0 && 89955418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 90055418Seric break; 90155418Seric } 90255418Seric 90355418Seric for (; *pp != NULL; pp++) 90455418Seric *pp = pp[1]; 90555418Seric } 90655418Seric 90755418Seric #endif /* UNSETENV */ 90856215Seric /* 90956215Seric ** GETDTABLESIZE -- return number of file descriptors 91056215Seric ** 91156215Seric ** Only on non-BSD systems 91256215Seric ** 91356215Seric ** Parameters: 91456215Seric ** none 91556215Seric ** 91656215Seric ** Returns: 91756215Seric ** size of file descriptor table 91856215Seric ** 91956215Seric ** Side Effects: 92056215Seric ** none 92156215Seric */ 92256215Seric 92356215Seric #ifdef SYSTEM5 92456215Seric 92556215Seric int 92656215Seric getdtablesize() 92756215Seric { 92856215Seric return NOFILE; 92956215Seric } 93056215Seric 93156215Seric #endif 93257631Seric /* 93357631Seric ** UNAME -- get the UUCP name of this system. 93457631Seric */ 93557631Seric 93657943Seric #ifndef HASUNAME 93757631Seric 93857631Seric int 93957631Seric uname(name) 94057631Seric struct utsname *name; 94157631Seric { 94257631Seric FILE *file; 94357631Seric char *n; 94457631Seric 94557631Seric name->nodename[0] = '\0'; 94657631Seric 94757661Seric /* try /etc/whoami -- one line with the node name */ 94857631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 94957631Seric { 95057661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 95157631Seric (void) fclose(file); 95257661Seric n = strchr(name->nodename, '\n'); 95357631Seric if (n != NULL) 95457631Seric *n = '\0'; 95557631Seric if (name->nodename[0] != '\0') 95657631Seric return (0); 95757631Seric } 95857631Seric 95957661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 96057631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 96157631Seric { 96257631Seric char buf[MAXLINE]; 96357631Seric 96457631Seric while (fgets(buf, MAXLINE, file) != NULL) 96557631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 96657631Seric NODE_LENGTH, name->nodename) > 0) 96757631Seric break; 96857631Seric (void) fclose(file); 96957631Seric if (name->nodename[0] != '\0') 97057631Seric return (0); 97157631Seric } 97257631Seric 97357631Seric #ifdef TRUST_POPEN 97457631Seric /* 97557631Seric ** Popen is known to have security holes. 97657631Seric */ 97757631Seric 97857661Seric /* try uuname -l to return local name */ 97957631Seric if ((file = popen("uuname -l", "r")) != NULL) 98057631Seric { 98157661Seric (void) fgets(name, NODE_LENGTH + 1, file); 98257631Seric (void) pclose(file); 98357661Seric n = strchr(name, '\n'); 98457631Seric if (n != NULL) 98557631Seric *n = '\0'; 98657661Seric if (name->nodename[0] != '\0') 98757631Seric return (0); 98857631Seric } 98957631Seric #endif 99057631Seric 99157631Seric return (-1); 99257631Seric } 99357943Seric #endif /* HASUNAME */ 99458068Seric /* 99558068Seric ** INITGROUPS -- initialize groups 99658068Seric ** 99758068Seric ** Stub implementation for System V style systems 99858068Seric */ 99958068Seric 100058068Seric #ifndef HASINITGROUPS 100158068Seric # if !defined(SYSTEM5) || defined(hpux) 100258068Seric # define HASINITGROUPS 100358068Seric # endif 100458068Seric #endif 100558068Seric 100658068Seric #ifndef HASINITGROUPS 100758068Seric 100858068Seric initgroups(name, basegid) 100958068Seric char *name; 101058068Seric int basegid; 101158068Seric { 101258068Seric return 0; 101358068Seric } 101458068Seric 101558068Seric #endif 101658082Seric /* 101758082Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 101858082Seric ** 101958082Seric ** Only implemented if you have statfs. 102058082Seric ** 102158082Seric ** Parameters: 102258333Seric ** msize -- the size to check against. If zero, we don't yet 102358333Seric ** know how big the message will be, so just check for 102458333Seric ** a "reasonable" amount. 102558082Seric ** 102658082Seric ** Returns: 102758082Seric ** TRUE if there is enough space. 102858082Seric ** FALSE otherwise. 102958082Seric */ 103058082Seric 103158082Seric #ifndef HASSTATFS 103258082Seric # if defined(BSD4_4) || defined(__osf__) 103358082Seric # define HASSTATFS 103458082Seric # endif 103558082Seric #endif 103658082Seric 103758082Seric #ifdef HASSTATFS 103858157Seric # undef HASUSTAT 103958157Seric #endif 104058157Seric 104158157Seric #if defined(HASUSTAT) 104258157Seric # include <sys/stat.h> 104358157Seric # include <ustat.h> 104458157Seric #endif 104558157Seric 104658157Seric #ifdef HASSTATFS 104758133Seric # if defined(sgi) || defined(apollo) 104858133Seric # include <sys/statfs.h> 104958133Seric # else 105058133Seric # if defined(sun) || defined(hpux) 105158133Seric # include <sys/vfs.h> 105258133Seric # else 105358157Seric # include <sys/mount.h> 105458133Seric # endif 105558133Seric # endif 105658082Seric #endif 105758082Seric 105858082Seric bool 105958333Seric enoughspace(msize) 106058333Seric long msize; 106158082Seric { 106258160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 106358157Seric # if defined(HASUSTAT) 106458153Seric struct ustat fs; 106558153Seric struct stat statbuf; 1066*58366Seric # define FSBLOCKSIZE DEV_BSIZE 106758157Seric # define f_bavail f_tfree 106858157Seric # else 106958157Seric # if defined(ultrix) 107058157Seric struct fs_data fs; 107158157Seric # define f_bavail fd_bfreen 1072*58366Seric # define FSBLOCKSIZE fs.fd_bsize 107358153Seric # else 107458082Seric struct statfs fs; 1075*58366Seric # define FSBLOCKSIZE fs.f_bsize 107658153Seric # endif 107758133Seric # endif 107858333Seric long blocksneeded; 107958082Seric extern int errno; 108058082Seric extern char *errstring(); 108158082Seric 108258333Seric if (MinBlocksFree <= 0 && msize <= 0) 108358082Seric { 108458082Seric if (tTd(4, 80)) 108558133Seric printf("enoughspace: no threshold\n"); 108658133Seric return TRUE; 108758133Seric } 108858333Seric 108958157Seric # if defined(HASUSTAT) 109058157Seric if (stat(QueueDir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 109158157Seric # else 109258157Seric # if defined(sgi) || defined(apollo) 109358133Seric if (statfs(QueueDir, &fs, sizeof fs, 0) == 0) 109458157Seric # else 109558157Seric # if defined(ultrix) 109658133Seric if (statfs(QueueDir, &fs) > 0) 109758153Seric # else 109858133Seric if (statfs(QueueDir, &fs) == 0) 109958153Seric # endif 110058133Seric # endif 110158133Seric # endif 110258133Seric { 110358133Seric if (tTd(4, 80)) 110458333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 110558333Seric fs.f_bavail, msize); 110658333Seric 110758333Seric /* convert msize to block count */ 1108*58366Seric msize = msize / FSBLOCKSIZE + 1; 110958333Seric if (MinBlocksFree >= 0) 111058333Seric msize += MinBlocksFree; 111158333Seric 111258333Seric if (fs.f_bavail < msize) 111358090Seric { 111458090Seric #ifdef LOG 111558090Seric if (LogLevel > 0) 111658090Seric syslog(LOG_ALERT, "%s: low on space (have %ld, need %ld)", 111758333Seric QueueDir, fs.f_bavail, msize); 111858090Seric #endif 111958082Seric return FALSE; 112058090Seric } 112158082Seric } 112258082Seric else if (tTd(4, 80)) 112358333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 112458333Seric MinBlocksFree, msize, errstring(errno)); 112558082Seric #endif 112658082Seric return TRUE; 112758082Seric } 1128