122698Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 362522Sbostic * Copyright (c) 1988, 1993 462522Sbostic * The Regents of the University of California. All rights reserved. 533728Sbostic * 642825Sbostic * %sccs.include.redist.c% 733728Sbostic */ 822698Sdist 922698Sdist #ifndef lint 10*64841Seric static char sccsid[] = "@(#)conf.c 8.45 (Berkeley) 11/14/93"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1363937Seric # include "sendmail.h" 1463937Seric # include "pathnames.h" 1514881Seric # include <sys/ioctl.h> 1658082Seric # include <sys/param.h> 1736928Sbostic # include <pwd.h> 18404Seric 19294Seric /* 203309Seric ** CONF.C -- Sendmail Configuration Tables. 21294Seric ** 22294Seric ** Defines the configuration of this installation. 23294Seric ** 241388Seric ** Configuration Variables: 252897Seric ** HdrInfo -- a table describing well-known header fields. 262897Seric ** Each entry has the field name and some flags, 274147Seric ** which are described in sendmail.h. 284093Seric ** 294093Seric ** Notes: 304093Seric ** I have tried to put almost all the reasonable 314093Seric ** configuration information into the configuration 324093Seric ** file read at runtime. My intent is that anything 334093Seric ** here is a function of the version of UNIX you 344093Seric ** are running, or is really static -- for example 354093Seric ** the headers are a superset of widely used 364093Seric ** protocols. If you find yourself playing with 374093Seric ** this file too much, you may be making a mistake! 38294Seric */ 39294Seric 40294Seric 41294Seric 42294Seric 434437Seric /* 442897Seric ** Header info table 453057Seric ** Final (null) entry contains the flags used for any other field. 464147Seric ** 474147Seric ** Not all of these are actually handled specially by sendmail 484147Seric ** at this time. They are included as placeholders, to let 494147Seric ** you know that "someday" I intend to have sendmail do 504147Seric ** something with them. 512897Seric */ 522897Seric 532897Seric struct hdrinfo HdrInfo[] = 542897Seric { 558060Seric /* originator fields, most to least significant */ 5611417Seric "resent-sender", H_FROM|H_RESENT, 5711417Seric "resent-from", H_FROM|H_RESENT, 5825686Seric "resent-reply-to", H_FROM|H_RESENT, 599055Seric "sender", H_FROM, 609055Seric "from", H_FROM, 6125686Seric "reply-to", H_FROM, 629055Seric "full-name", H_ACHECK, 6364682Seric "return-receipt-to", H_FROM|H_RECEIPTTO, 6457359Seric "errors-to", H_FROM|H_ERRORSTO, 6558796Seric 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, 7458796Seric 758060Seric /* message identification and control */ 7611417Seric "message-id", 0, 7711417Seric "resent-message-id", H_RESENT, 789055Seric "message", H_EOH, 799055Seric "text", H_EOH, 8058796Seric 8111417Seric /* date fields */ 8211417Seric "date", 0, 8311417Seric "resent-date", H_RESENT, 8458796Seric 858060Seric /* trace fields */ 869055Seric "received", H_TRACE|H_FORCE, 8759318Seric "x400-received", H_TRACE|H_FORCE, 889055Seric "via", H_TRACE|H_FORCE, 899055Seric "mail-from", H_TRACE|H_FORCE, 908060Seric 9158796Seric /* miscellaneous fields */ 9258796Seric "comments", H_FORCE, 9363753Seric "return-path", H_FORCE|H_ACHECK, 9458796Seric 959055Seric NULL, 0, 962897Seric }; 974166Seric 984166Seric 994166Seric 1004282Seric /* 1014282Seric ** Location of system files/databases/etc. 1024282Seric */ 1034282Seric 10458082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1059039Seric 1069064Seric 1079064Seric 1089039Seric /* 10958082Seric ** Privacy values 11058082Seric */ 11158082Seric 11258082Seric struct prival PrivacyValues[] = 11358082Seric { 11458082Seric "public", PRIV_PUBLIC, 11558082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11658114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11758082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 11858082Seric "noexpn", PRIV_NOEXPN, 11958082Seric "novrfy", PRIV_NOVRFY, 12064333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12164333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12258789Seric "authwarnings", PRIV_AUTHWARNINGS, 12358082Seric "goaway", PRIV_GOAWAY, 12458789Seric NULL, 0, 12558082Seric }; 12658082Seric 12758082Seric 12858082Seric 12958082Seric /* 13024943Seric ** Miscellaneous stuff. 1319039Seric */ 1329039Seric 13324943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13464644Seric 13564644Seric 13664644Seric /* 13764644Seric ** Following should be config parameters (and probably will be in 13864644Seric ** future releases). In the meantime, setting these is considered 13964644Seric ** unsupported, and is intentionally undocumented. 14064644Seric */ 14164644Seric 14264644Seric #ifdef BROKENSMTPPEERS 14364644Seric bool BrokenSmtpPeers = TRUE; /* set if you have broken SMTP peers */ 14464644Seric #else 14564644Seric bool BrokenSmtpPeers = FALSE; /* set if you have broken SMTP peers */ 14664644Seric #endif 14764644Seric #ifdef NOLOOPBACKCHECK 14864644Seric bool CheckLoopBack = FALSE; /* set to check HELO loopback */ 14964644Seric #else 15064644Seric bool CheckLoopBack = TRUE; /* set to check HELO loopback */ 15164644Seric #endif 15264644Seric 15324943Seric /* 15424943Seric ** SETDEFAULTS -- set default values 15524943Seric ** 15624943Seric ** Because of the way freezing is done, these must be initialized 15724943Seric ** using direct code. 15824943Seric ** 15924943Seric ** Parameters: 16058734Seric ** e -- the default envelope. 16124943Seric ** 16224943Seric ** Returns: 16324943Seric ** none. 16424943Seric ** 16524943Seric ** Side Effects: 16624943Seric ** Initializes a bunch of global variables to their 16724943Seric ** default values. 16824943Seric */ 16924943Seric 17058737Seric #define DAYS * 24 * 60 * 60 17158737Seric 17258734Seric setdefaults(e) 17358734Seric register ENVELOPE *e; 17424943Seric { 17557438Seric SpaceSub = ' '; /* option B */ 17657438Seric QueueLA = 8; /* option x */ 17757438Seric RefuseLA = 12; /* option X */ 17857438Seric WkRecipFact = 30000L; /* option y */ 17957438Seric WkClassFact = 1800L; /* option z */ 18057438Seric WkTimeFact = 90000L; /* option Z */ 18157438Seric QueueFactor = WkRecipFact * 20; /* option q */ 18263787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 18357438Seric /* option F */ 18457438Seric DefUid = 1; /* option u */ 18557438Seric DefGid = 1; /* option g */ 18657438Seric CheckpointInterval = 10; /* option C */ 18757438Seric MaxHopCount = 25; /* option h */ 18858734Seric e->e_sendmode = SM_FORK; /* option d */ 18958734Seric e->e_errormode = EM_PRINT; /* option e */ 19059709Seric SevenBit = FALSE; /* option 7 */ 19157438Seric MaxMciCache = 1; /* option k */ 19257438Seric MciCacheTimeout = 300; /* option K */ 19357438Seric LogLevel = 9; /* option L */ 19458112Seric settimeouts(NULL); /* option r */ 19558737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 19658737Seric TimeOuts.to_q_warning = 0; /* option T */ 19758853Seric PrivacyFlags = 0; /* option p */ 19840973Sbostic setdefuser(); 19953654Seric setupmaps(); 20057402Seric setupmailers(); 20124943Seric } 202294Seric 20340973Sbostic 2044326Seric /* 20540973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20640973Sbostic */ 20740973Sbostic 20840973Sbostic setdefuser() 20940973Sbostic { 21040973Sbostic struct passwd *defpwent; 21157386Seric static char defuserbuf[40]; 21240973Sbostic 21357386Seric DefUser = defuserbuf; 21440973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 21557386Seric strcpy(defuserbuf, defpwent->pw_name); 21640973Sbostic else 21757386Seric strcpy(defuserbuf, "nobody"); 21840973Sbostic } 21953654Seric /* 22056836Seric ** HOST_MAP_INIT -- initialize host class structures 22156836Seric */ 22256836Seric 22356836Seric bool 22460219Seric host_map_init(map, args) 22556836Seric MAP *map; 22656836Seric char *args; 22756836Seric { 22856836Seric register char *p = args; 22956836Seric 23056836Seric for (;;) 23156836Seric { 23258050Seric while (isascii(*p) && isspace(*p)) 23356836Seric p++; 23456836Seric if (*p != '-') 23556836Seric break; 23656836Seric switch (*++p) 23756836Seric { 23856836Seric case 'a': 23956836Seric map->map_app = ++p; 24056836Seric break; 24156836Seric } 24258050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 24356836Seric p++; 24456836Seric if (*p != '\0') 24556836Seric *p++ = '\0'; 24656836Seric } 24756836Seric if (map->map_app != NULL) 24856836Seric map->map_app = newstr(map->map_app); 24956836Seric return TRUE; 25056836Seric } 25157402Seric /* 25257402Seric ** SETUPMAILERS -- initialize default mailers 25357402Seric */ 25456836Seric 25557402Seric setupmailers() 25657402Seric { 25757402Seric char buf[100]; 25857402Seric 25957403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 26057403Seric makemailer(buf); 26157403Seric 26259883Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE"); 26357402Seric makemailer(buf); 26457402Seric 26557402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 26657402Seric makemailer(buf); 26757402Seric } 26856836Seric /* 26960207Seric ** SETUPMAPS -- set up map classes 27060207Seric */ 27160207Seric 27260207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 27360207Seric { \ 27460207Seric extern bool parse __P((MAP *, char *)); \ 27560207Seric extern bool open __P((MAP *, int)); \ 27660207Seric extern void close __P((MAP *)); \ 27760207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 27860207Seric extern void store __P((MAP *, char *, char *)); \ 27960207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 28060207Seric s->s_mapclass.map_cname = name; \ 28160207Seric s->s_mapclass.map_ext = ext; \ 28260207Seric s->s_mapclass.map_cflags = flags; \ 28360207Seric s->s_mapclass.map_parse = parse; \ 28460207Seric s->s_mapclass.map_open = open; \ 28560207Seric s->s_mapclass.map_close = close; \ 28660207Seric s->s_mapclass.map_lookup = lookup; \ 28760207Seric s->s_mapclass.map_store = store; \ 28860207Seric } 28960207Seric 29060207Seric setupmaps() 29160207Seric { 29260207Seric register STAB *s; 29360207Seric 29460207Seric #ifdef NEWDB 29560207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29660207Seric map_parseargs, hash_map_open, db_map_close, 29760207Seric db_map_lookup, db_map_store); 29860207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29960207Seric map_parseargs, bt_map_open, db_map_close, 30060207Seric db_map_lookup, db_map_store); 30160207Seric #endif 30260207Seric 30360207Seric #ifdef NDBM 30460207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 30560207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 30660207Seric ndbm_map_lookup, ndbm_map_store); 30760207Seric #endif 30860207Seric 30960207Seric #ifdef NIS 31060207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 31160207Seric map_parseargs, nis_map_open, nis_map_close, 31260207Seric nis_map_lookup, nis_map_store); 31360207Seric #endif 31460207Seric 31560207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 31660207Seric map_parseargs, stab_map_open, stab_map_close, 31760207Seric stab_map_lookup, stab_map_store); 31860207Seric 31960207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 32060207Seric map_parseargs, impl_map_open, impl_map_close, 32160207Seric impl_map_lookup, impl_map_store); 32260207Seric 32360207Seric /* host DNS lookup */ 32460207Seric MAPDEF("host", NULL, 0, 32560207Seric host_map_init, null_map_open, null_map_close, 32660207Seric host_map_lookup, null_map_store); 32760207Seric 32860207Seric /* dequote map */ 32960207Seric MAPDEF("dequote", NULL, 0, 33060207Seric dequote_init, null_map_open, null_map_close, 33160207Seric dequote_map, null_map_store); 33260207Seric 33360207Seric #if 0 33460207Seric # ifdef USERDB 33560207Seric /* user database */ 33660207Seric MAPDEF("udb", ".db", 0, 33760207Seric udb_map_parse, null_map_open, null_map_close, 33860207Seric udb_map_lookup, null_map_store); 33960207Seric # endif 34060207Seric #endif 34160207Seric } 34260207Seric 34360207Seric #undef MAPDEF 34460207Seric /* 3459369Seric ** USERNAME -- return the user id of the logged in user. 3469369Seric ** 3479369Seric ** Parameters: 3489369Seric ** none. 3499369Seric ** 3509369Seric ** Returns: 3519369Seric ** The login name of the logged in user. 3529369Seric ** 3539369Seric ** Side Effects: 3549369Seric ** none. 3559369Seric ** 3569369Seric ** Notes: 3579369Seric ** The return value is statically allocated. 3589369Seric */ 3599369Seric 3609369Seric char * 3619369Seric username() 3629369Seric { 36317469Seric static char *myname = NULL; 3649369Seric extern char *getlogin(); 36519904Smiriam register struct passwd *pw; 3669369Seric 36717469Seric /* cache the result */ 36817469Seric if (myname == NULL) 36917469Seric { 37017469Seric myname = getlogin(); 37117469Seric if (myname == NULL || myname[0] == '\0') 37217469Seric { 37363787Seric pw = getpwuid(RealUid); 37417469Seric if (pw != NULL) 37540993Sbostic myname = newstr(pw->pw_name); 37617469Seric } 37719904Smiriam else 37819904Smiriam { 37963787Seric uid_t uid = RealUid; 38019873Smiriam 38140993Sbostic myname = newstr(myname); 38240993Sbostic if ((pw = getpwnam(myname)) == NULL || 38358736Seric (uid != 0 && uid != pw->pw_uid)) 38419904Smiriam { 38558736Seric pw = getpwuid(uid); 38624945Seric if (pw != NULL) 38740993Sbostic myname = newstr(pw->pw_name); 38819873Smiriam } 38919873Smiriam } 39017469Seric if (myname == NULL || myname[0] == '\0') 39117469Seric { 39258151Seric syserr("554 Who are you?"); 39317469Seric myname = "postmaster"; 39417469Seric } 39517469Seric } 39617469Seric 39717469Seric return (myname); 3989369Seric } 3999369Seric /* 4004190Seric ** TTYPATH -- Get the path of the user's tty 401294Seric ** 402294Seric ** Returns the pathname of the user's tty. Returns NULL if 403294Seric ** the user is not logged in or if s/he has write permission 404294Seric ** denied. 405294Seric ** 406294Seric ** Parameters: 407294Seric ** none 408294Seric ** 409294Seric ** Returns: 410294Seric ** pathname of the user's tty. 411294Seric ** NULL if not logged in or write permission denied. 412294Seric ** 413294Seric ** Side Effects: 414294Seric ** none. 415294Seric ** 416294Seric ** WARNING: 417294Seric ** Return value is in a local buffer. 418294Seric ** 419294Seric ** Called By: 420294Seric ** savemail 421294Seric */ 422294Seric 423294Seric char * 424294Seric ttypath() 425294Seric { 426294Seric struct stat stbuf; 427294Seric register char *pathn; 428294Seric extern char *ttyname(); 4294081Seric extern char *getlogin(); 430294Seric 431294Seric /* compute the pathname of the controlling tty */ 4329369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4339369Seric (pathn = ttyname(0)) == NULL) 434294Seric { 435294Seric errno = 0; 436294Seric return (NULL); 437294Seric } 438294Seric 439294Seric /* see if we have write permission */ 4402967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 441294Seric { 442294Seric errno = 0; 443294Seric return (NULL); 444294Seric } 445294Seric 446294Seric /* see if the user is logged in */ 447294Seric if (getlogin() == NULL) 448294Seric return (NULL); 449294Seric 450294Seric /* looks good */ 451294Seric return (pathn); 452294Seric } 4532967Seric /* 4542967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4552967Seric ** 4562967Seric ** This routine can be supplied on a per-installation basis 4572967Seric ** to determine whether a person is allowed to send a message. 4582967Seric ** This allows restriction of certain types of internet 4592967Seric ** forwarding or registration of users. 4602967Seric ** 4612967Seric ** If the hosts are found to be incompatible, an error 46257454Seric ** message should be given using "usrerr" and 0 should 4632967Seric ** be returned. 4642967Seric ** 4654288Seric ** 'NoReturn' can be set to suppress the return-to-sender 4664288Seric ** function; this should be done on huge messages. 4674288Seric ** 4682967Seric ** Parameters: 4692967Seric ** to -- the person being sent to. 4702967Seric ** 4712967Seric ** Returns: 47257459Seric ** an exit status 4732967Seric ** 4742967Seric ** Side Effects: 4752967Seric ** none (unless you include the usrerr stuff) 4762967Seric */ 4772967Seric 47855012Seric checkcompat(to, e) 4792967Seric register ADDRESS *to; 48055012Seric register ENVELOPE *e; 4812967Seric { 48212133Seric # ifdef lint 48312133Seric if (to == NULL) 48412133Seric to++; 48563834Seric # endif /* lint */ 48664402Seric 48764402Seric if (tTd(49, 1)) 48864402Seric printf("checkcompat(to=%s, from=%s)\n", 48964402Seric to->q_paddr, e->e_from.q_paddr); 49064402Seric 49110698Seric # ifdef EXAMPLE_CODE 49210698Seric /* this code is intended as an example only */ 4934437Seric register STAB *s; 4944437Seric 4954437Seric s = stab("arpa", ST_MAILER, ST_FIND); 49655012Seric if (s != NULL && e->e_from.q_mailer != LocalMailer && 4979369Seric to->q_mailer == s->s_mailer) 4984437Seric { 49958151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 50010698Seric /* NoReturn = TRUE; to supress return copy */ 50157459Seric return (EX_UNAVAILABLE); 5024437Seric } 50356795Seric # endif /* EXAMPLE_CODE */ 50457459Seric return (EX_OK); 5052967Seric } 5069369Seric /* 50764035Seric ** SETSIGNAL -- set a signal handler 50864035Seric ** 50964035Seric ** This is essentially old BSD "signal(3)". 51064035Seric */ 51164035Seric 51264561Seric sigfunc_t 51364035Seric setsignal(sig, handler) 51464035Seric int sig; 51564561Seric sigfunc_t handler; 51664035Seric { 51764380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 51864035Seric return signal(sig, handler); 51964035Seric #else 52064035Seric struct sigaction n, o; 52164035Seric 52264035Seric bzero(&n, sizeof n); 52364035Seric n.sa_handler = handler; 52464035Seric if (sigaction(sig, &n, &o) < 0) 52564035Seric return SIG_ERR; 52664035Seric return o.sa_handler; 52764035Seric #endif 52864035Seric } 52964035Seric /* 5309369Seric ** HOLDSIGS -- arrange to hold all signals 5319369Seric ** 5329369Seric ** Parameters: 5339369Seric ** none. 5349369Seric ** 5359369Seric ** Returns: 5369369Seric ** none. 5379369Seric ** 5389369Seric ** Side Effects: 5399369Seric ** Arranges that signals are held. 5409369Seric */ 5419369Seric 5429369Seric holdsigs() 5439369Seric { 5449369Seric } 5459369Seric /* 5469369Seric ** RLSESIGS -- arrange to release all signals 5479369Seric ** 5489369Seric ** This undoes the effect of holdsigs. 5499369Seric ** 5509369Seric ** Parameters: 5519369Seric ** none. 5529369Seric ** 5539369Seric ** Returns: 5549369Seric ** none. 5559369Seric ** 5569369Seric ** Side Effects: 5579369Seric ** Arranges that signals are released. 5589369Seric */ 5599369Seric 5609369Seric rlsesigs() 5619369Seric { 5629369Seric } 56314872Seric /* 56464705Seric ** INIT_MD -- do machine dependent initializations 56564705Seric ** 56664705Seric ** Systems that have global modes that should be set should do 56764705Seric ** them here rather than in main. 56864705Seric */ 56964705Seric 57064705Seric #ifdef _AUX_SOURCE 57164705Seric # include <compat.h> 57264705Seric #endif 57364705Seric 57464825Seric init_md(argc, argv) 57564825Seric int argc; 57664825Seric char **argv; 57764705Seric { 57864705Seric #ifdef _AUX_SOURCE 57964705Seric setcompat(getcompat() | COMPAT_BSDPROT); 58064705Seric #endif 58164705Seric } 58264705Seric /* 58314872Seric ** GETLA -- get the current load average 58414872Seric ** 58514881Seric ** This code stolen from la.c. 58614881Seric ** 58714872Seric ** Parameters: 58814872Seric ** none. 58914872Seric ** 59014872Seric ** Returns: 59114872Seric ** The current load average as an integer. 59214872Seric ** 59314872Seric ** Side Effects: 59414872Seric ** none. 59514872Seric */ 59614872Seric 59751920Seric /* try to guess what style of load average we have */ 59851920Seric #define LA_ZERO 1 /* always return load average as zero */ 59964360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 60051920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 60151920Seric #define LA_SUBR 4 /* call getloadavg */ 60264295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 60364360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 60414872Seric 60563962Seric /* do guesses based on general OS type */ 60651920Seric #ifndef LA_TYPE 60763962Seric # define LA_TYPE LA_ZERO 60851920Seric #endif 60951920Seric 61064360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 61151920Seric 61214872Seric #include <nlist.h> 61314872Seric 61451920Seric #ifndef LA_AVENRUN 61563962Seric # ifdef SYSTEM5 61663962Seric # define LA_AVENRUN "avenrun" 61763962Seric # else 61863962Seric # define LA_AVENRUN "_avenrun" 61963962Seric # endif 62051920Seric #endif 62151920Seric 62251920Seric /* _PATH_UNIX should be defined in <paths.h> */ 62351920Seric #ifndef _PATH_UNIX 62463962Seric # if defined(SYSTEM5) 62563962Seric # define _PATH_UNIX "/unix" 62663962Seric # else 62763962Seric # define _PATH_UNIX "/vmunix" 62863962Seric # endif 62951920Seric #endif 63051920Seric 63114872Seric struct nlist Nl[] = 63214872Seric { 63351920Seric { LA_AVENRUN }, 63414872Seric #define X_AVENRUN 0 63514872Seric { 0 }, 63614872Seric }; 63714872Seric 63860583Seric #ifndef FSHIFT 63960583Seric # if defined(unixpc) 64060583Seric # define FSHIFT 5 64160583Seric # endif 64257736Seric 64360583Seric # if defined(__alpha) 64460583Seric # define FSHIFT 10 64560583Seric # endif 64657977Seric 64764360Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 64851920Seric # define FSHIFT 8 64960583Seric # endif 65057736Seric #endif 65160583Seric 65264360Seric #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE) 65351920Seric # define FSCALE (1 << FSHIFT) 65451920Seric #endif 65540930Srick 65614872Seric getla() 65714872Seric { 65814872Seric static int kmem = -1; 65951920Seric #if LA_TYPE == LA_INT 66024943Seric long avenrun[3]; 66151920Seric #else 66264360Seric # if LA_TYPE == LA_SHORT 66364360Seric short avenrun[3]; 66464360Seric # else 66551920Seric double avenrun[3]; 66664360Seric # endif 66751920Seric #endif 66825615Seric extern off_t lseek(); 66957736Seric extern int errno; 67014872Seric 67114872Seric if (kmem < 0) 67214872Seric { 67324945Seric kmem = open("/dev/kmem", 0, 0); 67414872Seric if (kmem < 0) 67557736Seric { 67657736Seric if (tTd(3, 1)) 67757736Seric printf("getla: open(/dev/kmem): %s\n", 67857736Seric errstring(errno)); 67914872Seric return (-1); 68057736Seric } 68151920Seric (void) fcntl(kmem, F_SETFD, 1); 68257736Seric if (nlist(_PATH_UNIX, Nl) < 0) 68357736Seric { 68457736Seric if (tTd(3, 1)) 68557736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 68657736Seric errstring(errno)); 68714872Seric return (-1); 68857736Seric } 68959253Seric if (Nl[X_AVENRUN].n_value == 0) 69059253Seric { 69159253Seric if (tTd(3, 1)) 69259253Seric printf("getla: nlist(%s, %s) ==> 0\n", 69359253Seric _PATH_UNIX, LA_AVENRUN); 69459253Seric return (-1); 69559253Seric } 69614872Seric } 69757736Seric if (tTd(3, 20)) 69857736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 69924945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 70023118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 70119967Seric { 70219967Seric /* thank you Ian */ 70357736Seric if (tTd(3, 1)) 70457736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 70519967Seric return (-1); 70619967Seric } 70764360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 70857736Seric if (tTd(3, 5)) 70957736Seric { 71057736Seric printf("getla: avenrun = %d", avenrun[0]); 71157736Seric if (tTd(3, 15)) 71257736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 71357736Seric printf("\n"); 71457736Seric } 71557736Seric if (tTd(3, 1)) 71657736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 71724943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 71851920Seric #else 71957736Seric if (tTd(3, 5)) 72057736Seric { 72157736Seric printf("getla: avenrun = %g", avenrun[0]); 72257736Seric if (tTd(3, 15)) 72357736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 72457736Seric printf("\n"); 72557736Seric } 72657736Seric if (tTd(3, 1)) 72757736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 72851920Seric return ((int) (avenrun[0] + 0.5)); 72951920Seric #endif 73014872Seric } 73114872Seric 73251773Seric #else 73351920Seric #if LA_TYPE == LA_SUBR 73451773Seric 73564718Seric #ifdef DGUX 73664718Seric 73764718Seric #include <sys/dg_sys_info.h> 73864718Seric 73964718Seric int getla() 74064718Seric { 74164718Seric struct dg_sys_info_load_info load_info; 74264718Seric 74364718Seric dg_sys_info((long *)&load_info, 74464718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 74564718Seric 74664718Seric return((int) (load_info.one_minute + 0.5)); 74764718Seric } 74864718Seric 74964718Seric #else 75064718Seric 75151773Seric getla() 75251773Seric { 75351920Seric double avenrun[3]; 75451920Seric 75551920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 75657736Seric { 75757736Seric if (tTd(3, 1)) 75857736Seric perror("getla: getloadavg failed:"); 75951920Seric return (-1); 76057736Seric } 76157736Seric if (tTd(3, 1)) 76257736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 76351920Seric return ((int) (avenrun[0] + 0.5)); 76451773Seric } 76551773Seric 76664718Seric #endif /* DGUX */ 76751773Seric #else 76864295Seric #if LA_TYPE == LA_MACH 76951773Seric 77064295Seric /* 77164295Seric ** This has been tested on NeXT release 2.1. 77264295Seric */ 77364295Seric 77464295Seric #include <mach.h> 77564295Seric 77651773Seric getla() 77751773Seric { 77864295Seric processor_set_t default_set; 77964295Seric kern_return_t error; 78064295Seric unsigned int info_count; 78164295Seric struct processor_set_basic_info info; 78264295Seric host_t host; 78364295Seric 78464295Seric error = processor_set_default(host_self(), &default_set); 78564295Seric if (error != KERN_SUCCESS) 78664295Seric return -1; 78764295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 78864295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 78964295Seric &host, (processor_set_info_t)&info, 79064295Seric &info_count) != KERN_SUCCESS) 79164295Seric { 79264295Seric return -1; 79364295Seric } 79464295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 79564295Seric } 79664295Seric 79764295Seric 79864295Seric #else 79964295Seric 80064295Seric getla() 80164295Seric { 80257736Seric if (tTd(3, 1)) 80357736Seric printf("getla: ZERO\n"); 80451920Seric return (0); 80551773Seric } 80651773Seric 80751773Seric #endif 80851773Seric #endif 80964295Seric #endif 81024943Seric /* 81124943Seric ** SHOULDQUEUE -- should this message be queued or sent? 81224943Seric ** 81324943Seric ** Compares the message cost to the load average to decide. 81424943Seric ** 81524943Seric ** Parameters: 81624943Seric ** pri -- the priority of the message in question. 81757438Seric ** ctime -- the message creation time. 81824943Seric ** 81924943Seric ** Returns: 82024943Seric ** TRUE -- if this message should be queued up for the 82124943Seric ** time being. 82224943Seric ** FALSE -- if the load is low enough to send this message. 82324943Seric ** 82424943Seric ** Side Effects: 82524943Seric ** none. 82624943Seric */ 82724943Seric 82824943Seric bool 82957438Seric shouldqueue(pri, ctime) 83024943Seric long pri; 83157438Seric time_t ctime; 83224943Seric { 83351920Seric if (CurrentLA < QueueLA) 83424943Seric return (FALSE); 83558132Seric if (CurrentLA >= RefuseLA) 83658132Seric return (TRUE); 83751920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 83824943Seric } 83924943Seric /* 84053037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 84153037Seric ** 84253037Seric ** Parameters: 84353037Seric ** none. 84453037Seric ** 84553037Seric ** Returns: 84653037Seric ** TRUE if incoming SMTP connections should be refused 84753037Seric ** (for now). 84853037Seric ** FALSE if we should accept new work. 84953037Seric ** 85053037Seric ** Side Effects: 85153037Seric ** none. 85253037Seric */ 85353037Seric 85453037Seric bool 85553037Seric refuseconnections() 85653037Seric { 85759156Seric #ifdef XLA 85859156Seric if (!xla_smtp_ok()) 85959156Seric return TRUE; 86059156Seric #endif 86159156Seric 86253037Seric /* this is probably too simplistic */ 86358132Seric return (CurrentLA >= RefuseLA); 86453037Seric } 86553037Seric /* 86624943Seric ** SETPROCTITLE -- set process title for ps 86724943Seric ** 86824943Seric ** Parameters: 86958674Seric ** fmt -- a printf style format string. 87058674Seric ** a, b, c -- possible parameters to fmt. 87124943Seric ** 87224943Seric ** Returns: 87324943Seric ** none. 87424943Seric ** 87524943Seric ** Side Effects: 87624943Seric ** Clobbers argv of our main procedure so ps(1) will 87724943Seric ** display the title. 87824943Seric */ 87924943Seric 88058689Seric #ifdef SETPROCTITLE 88158689Seric # ifdef __hpux 88258689Seric # include <sys/pstat.h> 88358689Seric # endif 88459732Seric # ifdef BSD4_4 88559732Seric # include <machine/vmparam.h> 88659732Seric # include <sys/exec.h> 88764247Seric # ifdef __bsdi__ 88864247Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 88964247Seric # endif 89059992Seric # ifdef PS_STRINGS 89159992Seric # define SETPROC_STATIC static 89259992Seric # endif 89359732Seric # endif 89459732Seric # ifndef SETPROC_STATIC 89559732Seric # define SETPROC_STATIC 89659732Seric # endif 89758689Seric #endif 89858689Seric 89924943Seric /*VARARGS1*/ 90057642Seric #ifdef __STDC__ 90157642Seric setproctitle(char *fmt, ...) 90257642Seric #else 90357642Seric setproctitle(fmt, va_alist) 90424943Seric char *fmt; 90557642Seric va_dcl 90657642Seric #endif 90724943Seric { 90824943Seric # ifdef SETPROCTITLE 90924943Seric register char *p; 91025049Seric register int i; 91159732Seric SETPROC_STATIC char buf[MAXLINE]; 91256852Seric VA_LOCAL_DECL 91358689Seric # ifdef __hpux 91458689Seric union pstun pst; 91558689Seric # endif 91624943Seric extern char **Argv; 91724943Seric extern char *LastArgv; 91824943Seric 91958674Seric p = buf; 92024943Seric 92158674Seric /* print sendmail: heading for grep */ 92258674Seric (void) strcpy(p, "sendmail: "); 92358674Seric p += strlen(p); 92424943Seric 92558674Seric /* print the argument string */ 92658674Seric VA_START(fmt); 92758674Seric (void) vsprintf(p, fmt, ap); 92856852Seric VA_END; 92954996Seric 93058674Seric i = strlen(buf); 93158689Seric 93258689Seric # ifdef __hpux 93358689Seric pst.pst_command = buf; 93458689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 93558689Seric # else 93659992Seric # ifdef PS_STRINGS 93759732Seric PS_STRINGS->ps_nargvstr = 1; 93859732Seric PS_STRINGS->ps_argvstr = buf; 93959732Seric # else 94054996Seric if (i > LastArgv - Argv[0] - 2) 94125049Seric { 94254996Seric i = LastArgv - Argv[0] - 2; 94358674Seric buf[i] = '\0'; 94425049Seric } 94558674Seric (void) strcpy(Argv[0], buf); 94654997Seric p = &Argv[0][i]; 94724943Seric while (p < LastArgv) 94824943Seric *p++ = ' '; 94959732Seric # endif 95058689Seric # endif 95156795Seric # endif /* SETPROCTITLE */ 95224943Seric } 95325698Seric /* 95425698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 95525698Seric ** 95625698Seric ** Parameters: 95725698Seric ** none. 95825698Seric ** 95925698Seric ** Returns: 96025698Seric ** none. 96125698Seric ** 96225698Seric ** Side Effects: 96325698Seric ** Picks up extant zombies. 96425698Seric */ 96525698Seric 96646928Sbostic void 96725698Seric reapchild() 96825698Seric { 96964738Seric int olderrno = errno; 97063966Seric # ifdef HASWAITPID 97160219Seric auto int status; 97260560Seric int count; 97360560Seric int pid; 97460219Seric 97560560Seric count = 0; 97660560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 97760560Seric { 97860560Seric if (count++ > 1000) 97960560Seric { 98060560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 98160560Seric pid, status); 98260560Seric break; 98360560Seric } 98460560Seric } 98560219Seric # else 98625698Seric # ifdef WNOHANG 98725698Seric union wait status; 98825698Seric 98963753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 99025698Seric continue; 99156795Seric # else /* WNOHANG */ 99225698Seric auto int status; 99325698Seric 99460219Seric while (wait(&status) > 0) 99525698Seric continue; 99656795Seric # endif /* WNOHANG */ 99760219Seric # endif 99864035Seric # ifdef SYS5SIGNALS 99964035Seric (void) setsignal(SIGCHLD, reapchild); 100058061Seric # endif 100164738Seric errno = olderrno; 100225698Seric } 100355418Seric /* 100455418Seric ** UNSETENV -- remove a variable from the environment 100555418Seric ** 100655418Seric ** Not needed on newer systems. 100755418Seric ** 100855418Seric ** Parameters: 100955418Seric ** name -- the string name of the environment variable to be 101055418Seric ** deleted from the current environment. 101155418Seric ** 101255418Seric ** Returns: 101355418Seric ** none. 101455418Seric ** 101555418Seric ** Globals: 101655418Seric ** environ -- a pointer to the current environment. 101755418Seric ** 101855418Seric ** Side Effects: 101955418Seric ** Modifies environ. 102055418Seric */ 102155418Seric 102263962Seric #ifndef HASUNSETENV 102355418Seric 102455418Seric void 102555418Seric unsetenv(name) 102655418Seric char *name; 102755418Seric { 102855418Seric extern char **environ; 102955418Seric register char **pp; 103055418Seric int len = strlen(name); 103155418Seric 103255418Seric for (pp = environ; *pp != NULL; pp++) 103355418Seric { 103455418Seric if (strncmp(name, *pp, len) == 0 && 103555418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 103655418Seric break; 103755418Seric } 103855418Seric 103955418Seric for (; *pp != NULL; pp++) 104055418Seric *pp = pp[1]; 104155418Seric } 104255418Seric 104363962Seric #endif 104456215Seric /* 104556215Seric ** GETDTABLESIZE -- return number of file descriptors 104656215Seric ** 104756215Seric ** Only on non-BSD systems 104856215Seric ** 104956215Seric ** Parameters: 105056215Seric ** none 105156215Seric ** 105256215Seric ** Returns: 105356215Seric ** size of file descriptor table 105456215Seric ** 105556215Seric ** Side Effects: 105656215Seric ** none 105756215Seric */ 105856215Seric 105963787Seric #ifdef SOLARIS 106063787Seric # include <sys/resource.h> 106163787Seric #endif 106256215Seric 106356215Seric int 106463787Seric getdtsize() 106556215Seric { 106663787Seric #ifdef RLIMIT_NOFILE 106763787Seric struct rlimit rl; 106863787Seric 106963787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 107063787Seric return rl.rlim_cur; 107163787Seric #endif 107263787Seric 107364031Seric # ifdef HASGETDTABLESIZE 107464031Seric return getdtablesize(); 107564031Seric # else 107664031Seric # ifdef _SC_OPEN_MAX 107758689Seric return sysconf(_SC_OPEN_MAX); 107863787Seric # else 107956215Seric return NOFILE; 108063787Seric # endif 108158689Seric # endif 108256215Seric } 108357631Seric /* 108457631Seric ** UNAME -- get the UUCP name of this system. 108557631Seric */ 108657631Seric 108757943Seric #ifndef HASUNAME 108857631Seric 108957631Seric int 109057631Seric uname(name) 109157631Seric struct utsname *name; 109257631Seric { 109357631Seric FILE *file; 109457631Seric char *n; 109557631Seric 109657631Seric name->nodename[0] = '\0'; 109757631Seric 109857661Seric /* try /etc/whoami -- one line with the node name */ 109957631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 110057631Seric { 110157661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 110257631Seric (void) fclose(file); 110357661Seric n = strchr(name->nodename, '\n'); 110457631Seric if (n != NULL) 110557631Seric *n = '\0'; 110657631Seric if (name->nodename[0] != '\0') 110757631Seric return (0); 110857631Seric } 110957631Seric 111057661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 111157631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 111257631Seric { 111357631Seric char buf[MAXLINE]; 111457631Seric 111557631Seric while (fgets(buf, MAXLINE, file) != NULL) 111657631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 111757631Seric NODE_LENGTH, name->nodename) > 0) 111857631Seric break; 111957631Seric (void) fclose(file); 112057631Seric if (name->nodename[0] != '\0') 112157631Seric return (0); 112257631Seric } 112357631Seric 112457631Seric #ifdef TRUST_POPEN 112557631Seric /* 112657631Seric ** Popen is known to have security holes. 112757631Seric */ 112857631Seric 112957661Seric /* try uuname -l to return local name */ 113057631Seric if ((file = popen("uuname -l", "r")) != NULL) 113157631Seric { 113257661Seric (void) fgets(name, NODE_LENGTH + 1, file); 113357631Seric (void) pclose(file); 113457661Seric n = strchr(name, '\n'); 113557631Seric if (n != NULL) 113657631Seric *n = '\0'; 113757661Seric if (name->nodename[0] != '\0') 113857631Seric return (0); 113957631Seric } 114057631Seric #endif 114157631Seric 114257631Seric return (-1); 114357631Seric } 114457943Seric #endif /* HASUNAME */ 114558068Seric /* 114658068Seric ** INITGROUPS -- initialize groups 114758068Seric ** 114858068Seric ** Stub implementation for System V style systems 114958068Seric */ 115058068Seric 115158068Seric #ifndef HASINITGROUPS 115258068Seric 115358068Seric initgroups(name, basegid) 115458068Seric char *name; 115558068Seric int basegid; 115658068Seric { 115758068Seric return 0; 115858068Seric } 115958068Seric 116058068Seric #endif 116158082Seric /* 116259289Seric ** SETSID -- set session id (for non-POSIX systems) 116359289Seric */ 116459289Seric 116559289Seric #ifndef HASSETSID 116659289Seric 116763753Seric pid_t 116863753Seric setsid __P ((void)) 116959289Seric { 117064155Seric #ifdef TIOCNOTTY 117164155Seric int fd; 117264155Seric 117364155Seric fd = open("/dev/tty", 2); 117464155Seric if (fd >= 0) 117564155Seric { 117664155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 117764155Seric (void) close(fd); 117864155Seric } 117964155Seric #endif /* TIOCNOTTY */ 118064705Seric # ifdef SYS5SETPGRP 118163753Seric return setpgrp(); 118263753Seric # else 118364155Seric return setpgid(0, getpid()); 118459289Seric # endif 118559289Seric } 118659289Seric 118759289Seric #endif 118859289Seric /* 118964718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 119064718Seric ** 119164718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 119264718Seric ** instead of a long. This patches things. 119364718Seric */ 119464718Seric 119564718Seric #ifdef DGUX 119664718Seric 119764718Seric #undef inet_addr 119864718Seric 119964718Seric long 120064718Seric dgux_inet_addr(host) 120164718Seric char *host; 120264718Seric { 120364718Seric struct in_addr haddr; 120464718Seric 120564718Seric haddr = inet_addr(host); 120664718Seric return haddr.s_addr; 120764718Seric } 120864718Seric 120964718Seric #endif 121064718Seric /* 121163969Seric ** GETOPT -- for old systems or systems with bogus implementations 121263969Seric */ 121363969Seric 121463969Seric #ifdef NEEDGETOPT 121563969Seric 121663969Seric /* 121763969Seric * Copyright (c) 1985 Regents of the University of California. 121863969Seric * All rights reserved. The Berkeley software License Agreement 121963969Seric * specifies the terms and conditions for redistribution. 122063969Seric */ 122163969Seric 122263969Seric 122363969Seric /* 122463969Seric ** this version hacked to add `atend' flag to allow state machine 122563969Seric ** to reset if invoked by the program to scan args for a 2nd time 122663969Seric */ 122763969Seric 122863969Seric #if defined(LIBC_SCCS) && !defined(lint) 122963969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 123064072Seric #endif /* LIBC_SCCS and not lint */ 123163969Seric 123263969Seric #include <stdio.h> 123363969Seric 123463969Seric /* 123563969Seric * get option letter from argument vector 123663969Seric */ 123763969Seric int opterr = 1, /* if error message should be printed */ 123863969Seric optind = 1, /* index into parent argv vector */ 123963969Seric optopt; /* character checked for validity */ 124063969Seric char *optarg; /* argument associated with option */ 124163969Seric 124263969Seric #define BADCH (int)'?' 124363969Seric #define EMSG "" 124463969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 124563969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 124663969Seric 124763969Seric getopt(nargc,nargv,ostr) 124863969Seric int nargc; 124963969Seric char **nargv, 125063969Seric *ostr; 125163969Seric { 125263969Seric static char *place = EMSG; /* option letter processing */ 125363969Seric static char atend = 0; 125463969Seric register char *oli; /* option letter list index */ 125563969Seric 125663969Seric if (atend) { 125763969Seric atend = 0; 125863969Seric place = EMSG; 125963969Seric } 126063969Seric if(!*place) { /* update scanning pointer */ 126164136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 126263969Seric atend++; 126363969Seric return(EOF); 126463969Seric } 126563969Seric if (*place == '-') { /* found "--" */ 126663969Seric ++optind; 126763969Seric atend++; 126863969Seric return(EOF); 126963969Seric } 127063969Seric } /* option letter okay? */ 127164125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 127264136Seric if (!*place) ++optind; 127363969Seric tell(": illegal option -- "); 127463969Seric } 127563969Seric if (*++oli != ':') { /* don't need argument */ 127663969Seric optarg = NULL; 127763969Seric if (!*place) ++optind; 127863969Seric } 127963969Seric else { /* need an argument */ 128063969Seric if (*place) optarg = place; /* no white space */ 128163969Seric else if (nargc <= ++optind) { /* no arg */ 128263969Seric place = EMSG; 128363969Seric tell(": option requires an argument -- "); 128463969Seric } 128563969Seric else optarg = nargv[optind]; /* white space */ 128663969Seric place = EMSG; 128763969Seric ++optind; 128863969Seric } 128963969Seric return(optopt); /* dump back option letter */ 129063969Seric } 129163969Seric 129263969Seric #endif 129363969Seric /* 129463969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 129563969Seric */ 129663969Seric 129763969Seric #ifdef NEEDVPRINTF 129863969Seric 129963969Seric #define MAXARG 16 130063969Seric 130163969Seric vfprintf(fp, fmt, ap) 130263969Seric FILE * fp; 130363969Seric char * fmt; 130463969Seric char ** ap; 130563969Seric { 130663969Seric char * bp[MAXARG]; 130763969Seric int i = 0; 130863969Seric 130963969Seric while (*ap && i < MAXARG) 131063969Seric bp[i++] = *ap++; 131163969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 131263969Seric bp[4], bp[5], bp[6], bp[7], 131363969Seric bp[8], bp[9], bp[10], bp[11], 131463969Seric bp[12], bp[13], bp[14], bp[15]); 131563969Seric } 131663969Seric 131763969Seric vsprintf(s, fmt, ap) 131863969Seric char * s; 131963969Seric char * fmt; 132063969Seric char ** ap; 132163969Seric { 132263969Seric char * bp[MAXARG]; 132363969Seric int i = 0; 132463969Seric 132563969Seric while (*ap && i < MAXARG) 132663969Seric bp[i++] = *ap++; 132763969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 132863969Seric bp[4], bp[5], bp[6], bp[7], 132963969Seric bp[8], bp[9], bp[10], bp[11], 133063969Seric bp[12], bp[13], bp[14], bp[15]); 133163969Seric } 133263969Seric 133363969Seric #endif 133463969Seric /* 133564311Seric ** FREESPACE -- see how much free space is on the queue filesystem 133658082Seric ** 133758082Seric ** Only implemented if you have statfs. 133858082Seric ** 133958082Seric ** Parameters: 134064311Seric ** dir -- the directory in question. 134164311Seric ** bsize -- a variable into which the filesystem 134264311Seric ** block size is stored. 134358082Seric ** 134458082Seric ** Returns: 134564311Seric ** The number of bytes free on the queue filesystem. 134664311Seric ** -1 if the statfs call fails. 134764311Seric ** 134864311Seric ** Side effects: 134964311Seric ** Puts the filesystem block size into bsize. 135058082Seric */ 135158082Seric 135258082Seric #ifdef HASSTATFS 135358157Seric # undef HASUSTAT 135458157Seric #endif 135558157Seric 135658157Seric #if defined(HASUSTAT) 135758157Seric # include <ustat.h> 135858157Seric #endif 135958157Seric 136058157Seric #ifdef HASSTATFS 136164718Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) || defined(UMAXV) || defined(DGUX) 136258133Seric # include <sys/statfs.h> 136358133Seric # else 136464816Seric # if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE) || defined(MACH386) 136558133Seric # include <sys/vfs.h> 136658133Seric # else 136758157Seric # include <sys/mount.h> 136858133Seric # endif 136958133Seric # endif 137058082Seric #endif 137158082Seric 137264311Seric long 137364311Seric freespace(dir, bsize) 137464311Seric char *dir; 137564311Seric long *bsize; 137658082Seric { 137758160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 137858157Seric # if defined(HASUSTAT) 137958153Seric struct ustat fs; 138058153Seric struct stat statbuf; 138158366Seric # define FSBLOCKSIZE DEV_BSIZE 138258157Seric # define f_bavail f_tfree 138358157Seric # else 138458157Seric # if defined(ultrix) 138558157Seric struct fs_data fs; 138658157Seric # define f_bavail fd_bfreen 138758366Seric # define FSBLOCKSIZE fs.fd_bsize 138858153Seric # else 138958082Seric struct statfs fs; 139058366Seric # define FSBLOCKSIZE fs.f_bsize 139164336Seric # if defined(_SCO_unix_) || defined(IRIX) 139263987Seric # define f_bavail f_bfree 139363987Seric # endif 139458153Seric # endif 139558133Seric # endif 139658082Seric extern int errno; 139758082Seric 139858157Seric # if defined(HASUSTAT) 139964311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 140058157Seric # else 140164718Seric # if defined(IRIX) || defined(apollo) || defined(UMAXV) || defined(DGUX) 140264311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 140358157Seric # else 140458157Seric # if defined(ultrix) 140564311Seric if (statfs(dir, &fs) > 0) 140658153Seric # else 140764311Seric if (statfs(dir, &fs) == 0) 140858153Seric # endif 140958133Seric # endif 141058133Seric # endif 141158133Seric { 141264311Seric if (bsize != NULL) 141364311Seric *bsize = FSBLOCKSIZE; 141464311Seric return (fs.f_bavail); 141564311Seric } 141664337Seric #endif 141764311Seric return (-1); 141864311Seric } 141964311Seric /* 142064311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 142164311Seric ** 142264311Seric ** Only implemented if you have statfs. 142364311Seric ** 142464311Seric ** Parameters: 142564311Seric ** msize -- the size to check against. If zero, we don't yet 142664311Seric ** know how big the message will be, so just check for 142764311Seric ** a "reasonable" amount. 142864311Seric ** 142964311Seric ** Returns: 143064311Seric ** TRUE if there is enough space. 143164311Seric ** FALSE otherwise. 143264311Seric */ 143364311Seric 143464311Seric bool 143564311Seric enoughspace(msize) 143664311Seric long msize; 143764311Seric { 143864311Seric long bfree, bsize; 143964311Seric 144064311Seric if (MinBlocksFree <= 0 && msize <= 0) 144164311Seric { 144258133Seric if (tTd(4, 80)) 144364311Seric printf("enoughspace: no threshold\n"); 144464311Seric return TRUE; 144564311Seric } 144664311Seric 144764311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 144864311Seric { 144964311Seric if (tTd(4, 80)) 145058333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 145164311Seric bfree, msize); 145258333Seric 145358333Seric /* convert msize to block count */ 145464311Seric msize = msize / bsize + 1; 145558333Seric if (MinBlocksFree >= 0) 145658333Seric msize += MinBlocksFree; 145758333Seric 145864311Seric if (bfree < msize) 145958090Seric { 146058090Seric #ifdef LOG 146158090Seric if (LogLevel > 0) 146264254Seric syslog(LOG_ALERT, 146364254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 146464311Seric CurEnv->e_id, bfree, 146564254Seric CurHostName, msize, QueueDir); 146658090Seric #endif 146758082Seric return FALSE; 146858090Seric } 146958082Seric } 147058082Seric else if (tTd(4, 80)) 147158333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 147258333Seric MinBlocksFree, msize, errstring(errno)); 147358082Seric return TRUE; 147458082Seric } 147558542Seric /* 147658542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 147758542Seric ** 147858542Seric ** This looks at an errno value and tells if this is likely to 147958542Seric ** go away if retried later. 148058542Seric ** 148158542Seric ** Parameters: 148258542Seric ** err -- the errno code to classify. 148358542Seric ** 148458542Seric ** Returns: 148558542Seric ** TRUE if this is probably transient. 148658542Seric ** FALSE otherwise. 148758542Seric */ 148858542Seric 148958542Seric bool 149058542Seric transienterror(err) 149158542Seric int err; 149258542Seric { 149358542Seric switch (err) 149458542Seric { 149558542Seric case EIO: /* I/O error */ 149658542Seric case ENXIO: /* Device not configured */ 149758542Seric case EAGAIN: /* Resource temporarily unavailable */ 149858542Seric case ENOMEM: /* Cannot allocate memory */ 149958542Seric case ENODEV: /* Operation not supported by device */ 150058542Seric case ENFILE: /* Too many open files in system */ 150158542Seric case EMFILE: /* Too many open files */ 150258542Seric case ENOSPC: /* No space left on device */ 150358542Seric #ifdef ETIMEDOUT 150458542Seric case ETIMEDOUT: /* Connection timed out */ 150558542Seric #endif 150658542Seric #ifdef ESTALE 150758542Seric case ESTALE: /* Stale NFS file handle */ 150858542Seric #endif 150958542Seric #ifdef ENETDOWN 151058542Seric case ENETDOWN: /* Network is down */ 151158542Seric #endif 151258542Seric #ifdef ENETUNREACH 151358542Seric case ENETUNREACH: /* Network is unreachable */ 151458542Seric #endif 151558542Seric #ifdef ENETRESET 151658542Seric case ENETRESET: /* Network dropped connection on reset */ 151758542Seric #endif 151858542Seric #ifdef ECONNABORTED 151958542Seric case ECONNABORTED: /* Software caused connection abort */ 152058542Seric #endif 152158542Seric #ifdef ECONNRESET 152258542Seric case ECONNRESET: /* Connection reset by peer */ 152358542Seric #endif 152458542Seric #ifdef ENOBUFS 152558542Seric case ENOBUFS: /* No buffer space available */ 152658542Seric #endif 152758542Seric #ifdef ESHUTDOWN 152858542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 152958542Seric #endif 153058542Seric #ifdef ECONNREFUSED 153158542Seric case ECONNREFUSED: /* Connection refused */ 153258542Seric #endif 153358542Seric #ifdef EHOSTDOWN 153458542Seric case EHOSTDOWN: /* Host is down */ 153558542Seric #endif 153658542Seric #ifdef EHOSTUNREACH 153758542Seric case EHOSTUNREACH: /* No route to host */ 153858542Seric #endif 153958542Seric #ifdef EDQUOT 154058542Seric case EDQUOT: /* Disc quota exceeded */ 154158542Seric #endif 154258542Seric #ifdef EPROCLIM 154358542Seric case EPROCLIM: /* Too many processes */ 154458542Seric #endif 154558542Seric #ifdef EUSERS 154658542Seric case EUSERS: /* Too many users */ 154758542Seric #endif 154858542Seric #ifdef EDEADLK 154958542Seric case EDEADLK: /* Resource deadlock avoided */ 155058542Seric #endif 155158542Seric #ifdef EISCONN 155258542Seric case EISCONN: /* Socket already connected */ 155358542Seric #endif 155458542Seric #ifdef EINPROGRESS 155558542Seric case EINPROGRESS: /* Operation now in progress */ 155658542Seric #endif 155758542Seric #ifdef EALREADY 155858542Seric case EALREADY: /* Operation already in progress */ 155958542Seric #endif 156058542Seric #ifdef EADDRINUSE 156158542Seric case EADDRINUSE: /* Address already in use */ 156258542Seric #endif 156358542Seric #ifdef EADDRNOTAVAIL 156458542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 156558542Seric #endif 156663834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 156758542Seric case ENOSR: /* Out of streams resources */ 156858542Seric #endif 156958542Seric return TRUE; 157058542Seric } 157158542Seric 157258542Seric /* nope, must be permanent */ 157358542Seric return FALSE; 157458542Seric } 157558689Seric /* 157664035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 157758689Seric ** 157858689Seric ** Parameters: 157958689Seric ** fd -- the file descriptor of the file. 158058689Seric ** filename -- the file name (for error messages). 158164335Seric ** ext -- the filename extension. 158258689Seric ** type -- type of the lock. Bits can be: 158358689Seric ** LOCK_EX -- exclusive lock. 158458689Seric ** LOCK_NB -- non-blocking. 158558689Seric ** 158658689Seric ** Returns: 158758689Seric ** TRUE if the lock was acquired. 158858689Seric ** FALSE otherwise. 158958689Seric */ 159058689Seric 159158689Seric bool 159264335Seric lockfile(fd, filename, ext, type) 159358689Seric int fd; 159458689Seric char *filename; 159564335Seric char *ext; 159658689Seric int type; 159758689Seric { 159864035Seric # ifndef HASFLOCK 159958689Seric int action; 160058689Seric struct flock lfd; 160164335Seric 160264335Seric if (ext == NULL) 160364335Seric ext = ""; 160464035Seric 160563983Seric bzero(&lfd, sizeof lfd); 160659447Seric if (bitset(LOCK_UN, type)) 160759447Seric lfd.l_type = F_UNLCK; 160859447Seric else if (bitset(LOCK_EX, type)) 160958689Seric lfd.l_type = F_WRLCK; 161058689Seric else 161158689Seric lfd.l_type = F_RDLCK; 161258689Seric 161358689Seric if (bitset(LOCK_NB, type)) 161458689Seric action = F_SETLK; 161558689Seric else 161658689Seric action = F_SETLKW; 161758689Seric 161864264Seric if (tTd(55, 60)) 161964335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 162064335Seric filename, ext, action, lfd.l_type); 162164264Seric 162258689Seric if (fcntl(fd, action, &lfd) >= 0) 162364264Seric { 162464264Seric if (tTd(55, 60)) 162564264Seric printf("SUCCESS\n"); 162658689Seric return TRUE; 162764264Seric } 162858689Seric 162964264Seric if (tTd(55, 60)) 163064264Seric printf("(%s) ", errstring(errno)); 163164264Seric 163264136Seric /* 163364136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 163464136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 163564136Seric ** as type "tmp" (that is, served from swap space), the 163664136Seric ** previous fcntl will fail with "Invalid argument" errors. 163764136Seric ** Since this is fairly common during testing, we will assume 163864136Seric ** that this indicates that the lock is successfully grabbed. 163964136Seric */ 164064136Seric 164164136Seric if (errno == EINVAL) 164264264Seric { 164364264Seric if (tTd(55, 60)) 164464264Seric printf("SUCCESS\n"); 164564136Seric return TRUE; 164664264Seric } 164764136Seric 164858689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 164964378Seric { 165064378Seric int omode = -1; 165164378Seric # ifdef F_GETFL 165264378Seric int oerrno = errno; 165364378Seric 165464378Seric (void) fcntl(fd, F_GETFL, &omode); 165564378Seric errno = oerrno; 165664378Seric # endif 165764378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 165864378Seric filename, ext, fd, type, omode, geteuid()); 165964378Seric } 166058689Seric # else 166164335Seric if (ext == NULL) 166264335Seric ext = ""; 166364335Seric 166464264Seric if (tTd(55, 60)) 166564335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 166664264Seric 166758689Seric if (flock(fd, type) >= 0) 166864264Seric { 166964264Seric if (tTd(55, 60)) 167064264Seric printf("SUCCESS\n"); 167158689Seric return TRUE; 167264264Seric } 167358689Seric 167464264Seric if (tTd(55, 60)) 167564264Seric printf("(%s) ", errstring(errno)); 167664264Seric 167758689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 167864378Seric { 167964378Seric int omode = -1; 168064378Seric # ifdef F_GETFL 168164378Seric int oerrno = errno; 168264378Seric 168364378Seric (void) fcntl(fd, F_GETFL, &omode); 168464378Seric errno = oerrno; 168564378Seric # endif 168664378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 168764378Seric filename, ext, fd, type, omode, geteuid()); 168864386Seric } 168958689Seric # endif 169064264Seric if (tTd(55, 60)) 169164264Seric printf("FAIL\n"); 169258689Seric return FALSE; 169358689Seric } 169464556Seric /* 169564556Seric ** GETCFNAME -- return the name of the .cf file. 169664556Seric ** 169764556Seric ** Some systems (e.g., NeXT) determine this dynamically. 169864556Seric */ 169964556Seric 170064556Seric char * 170164556Seric getcfname() 170264556Seric { 170364556Seric if (ConfFile != NULL) 170464556Seric return ConfFile; 170564556Seric return _PATH_SENDMAILCF; 170664556Seric } 170764718Seric /* 170864718Seric ** SETVENDOR -- process vendor code from V configuration line 170964718Seric ** 171064718Seric ** Parameters: 171164718Seric ** vendor -- string representation of vendor. 171264718Seric ** 171364718Seric ** Returns: 171464718Seric ** TRUE -- if ok. 171564718Seric ** FALSE -- if vendor code could not be processed. 171664718Seric */ 171764718Seric 171864718Seric bool 171964718Seric setvendor(vendor) 172064718Seric char *vendor; 172164718Seric { 172264718Seric return (strcasecmp(vendor, "Berkeley") == 0); 172364718Seric } 172464816Seric /* 172564816Seric ** STRTOL -- convert string to long integer 172664816Seric ** 172764816Seric ** For systems that don't have it in the C library. 172864816Seric */ 172964816Seric 173064816Seric #ifdef NEEDSTRTOL 173164816Seric 173264816Seric long 173364816Seric strtol(p, ep, b) 173464816Seric char *p; 173564816Seric char **ep; 173664816Seric int b; 173764816Seric { 173864816Seric long l = 0; 173964816Seric char c; 174064816Seric char maxd; 174164816Seric int neg = 1; 174264816Seric 174364816Seric maxd = (b > 10) ? '9' : b + '0'; 174464816Seric 174564816Seric if (p && *p == '-') { 174664816Seric neg = -1; 174764816Seric p++; 174864816Seric } 174964816Seric while (p && (c = *p)) { 175064816Seric if (c >= '0' && c <= maxd) { 175164816Seric l = l*b + *p++ - '0'; 175264816Seric continue; 175364816Seric } 175464816Seric if (c >= 'A' && c <= 'Z') 175564816Seric c -= 'A' + 'a'; 175664816Seric c = c - 'a' + 10; 175764816Seric if (b > c) { 175864816Seric l = l*b + c; 175964816Seric p++; 176064816Seric continue; 176164816Seric } 176264816Seric break; 176364816Seric } 176464816Seric l *= neg; 176564816Seric if (ep) 176664816Seric *ep = p; 176764816Seric return l; 176864816Seric } 176964816Seric 177064816Seric #endif 1771*64841Seric /* 1772*64841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 1773*64841Seric ** 1774*64841Seric ** Solaris versions prior through 2.3 don't properly deliver a 1775*64841Seric ** canonical h_name field. This tries to work around it. 1776*64841Seric */ 1777*64841Seric 1778*64841Seric #ifdef SOLARIS 1779*64841Seric 1780*64841Seric struct hostent * 1781*64841Seric solaris_gethostbyname(name) 1782*64841Seric char *name; 1783*64841Seric { 1784*64841Seric # ifdef SOLARIS_2_3 1785*64841Seric static struct hostent hp; 1786*64841Seric static char buf[1000]; 1787*64841Seric extern struct hostent *_switch_gethostbyname_r(); 1788*64841Seric 1789*64841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 1790*64841Seric # else 1791*64841Seric return __switch_gethostbyname(name); 1792*64841Seric # endif 1793*64841Seric } 1794*64841Seric 1795*64841Seric struct hostent * 1796*64841Seric solaris_gethostbyaddr(addr, len, type) 1797*64841Seric char *addr; 1798*64841Seric int len; 1799*64841Seric int type; 1800*64841Seric { 1801*64841Seric # ifdef SOLARIS_2_3 1802*64841Seric static struct hostent hp; 1803*64841Seric static char buf[1000]; 1804*64841Seric extern struct hostent *_switch_gethostbyaddr_r(); 1805*64841Seric 1806*64841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 1807*64841Seric # else 1808*64841Seric return __switch_gethostbyaddr(addr, len, type); 1809*64841Seric # endif 1810*64841Seric } 1811*64841Seric 1812*64841Seric #endif 1813