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*64914Seric static char sccsid[] = "@(#)conf.c 8.47 (Berkeley) 11/17/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 */ 88964843Seric # define PROCTITLEPAD '\0' 89064247Seric # endif 89159992Seric # ifdef PS_STRINGS 89259992Seric # define SETPROC_STATIC static 89359992Seric # endif 89459732Seric # endif 89559732Seric # ifndef SETPROC_STATIC 89659732Seric # define SETPROC_STATIC 89759732Seric # endif 89858689Seric #endif 89958689Seric 90064843Seric #ifndef PROCTITLEPAD 90164843Seric # define PROCTITLEPAD ' ' 90264843Seric #endif 90364843Seric 90424943Seric /*VARARGS1*/ 90557642Seric #ifdef __STDC__ 90657642Seric setproctitle(char *fmt, ...) 90757642Seric #else 90857642Seric setproctitle(fmt, va_alist) 90924943Seric char *fmt; 91057642Seric va_dcl 91157642Seric #endif 91224943Seric { 91324943Seric # ifdef SETPROCTITLE 91424943Seric register char *p; 91525049Seric register int i; 91659732Seric SETPROC_STATIC char buf[MAXLINE]; 91756852Seric VA_LOCAL_DECL 91858689Seric # ifdef __hpux 91958689Seric union pstun pst; 92058689Seric # endif 92124943Seric extern char **Argv; 92224943Seric extern char *LastArgv; 92324943Seric 92458674Seric p = buf; 92524943Seric 92658674Seric /* print sendmail: heading for grep */ 92758674Seric (void) strcpy(p, "sendmail: "); 92858674Seric p += strlen(p); 92924943Seric 93058674Seric /* print the argument string */ 93158674Seric VA_START(fmt); 93258674Seric (void) vsprintf(p, fmt, ap); 93356852Seric VA_END; 93454996Seric 93558674Seric i = strlen(buf); 93658689Seric 93758689Seric # ifdef __hpux 93858689Seric pst.pst_command = buf; 93958689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 94058689Seric # else 94159992Seric # ifdef PS_STRINGS 94259732Seric PS_STRINGS->ps_nargvstr = 1; 94359732Seric PS_STRINGS->ps_argvstr = buf; 94459732Seric # else 94554996Seric if (i > LastArgv - Argv[0] - 2) 94625049Seric { 94754996Seric i = LastArgv - Argv[0] - 2; 94858674Seric buf[i] = '\0'; 94925049Seric } 95058674Seric (void) strcpy(Argv[0], buf); 95154997Seric p = &Argv[0][i]; 95224943Seric while (p < LastArgv) 95364843Seric *p++ = PROCTITLEPAD; 95459732Seric # endif 95558689Seric # endif 95656795Seric # endif /* SETPROCTITLE */ 95724943Seric } 95825698Seric /* 95925698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 96025698Seric ** 96125698Seric ** Parameters: 96225698Seric ** none. 96325698Seric ** 96425698Seric ** Returns: 96525698Seric ** none. 96625698Seric ** 96725698Seric ** Side Effects: 96825698Seric ** Picks up extant zombies. 96925698Seric */ 97025698Seric 97146928Sbostic void 97225698Seric reapchild() 97325698Seric { 97464738Seric int olderrno = errno; 97563966Seric # ifdef HASWAITPID 97660219Seric auto int status; 97760560Seric int count; 97860560Seric int pid; 97960219Seric 98060560Seric count = 0; 98160560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 98260560Seric { 98360560Seric if (count++ > 1000) 98460560Seric { 98560560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 98660560Seric pid, status); 98760560Seric break; 98860560Seric } 98960560Seric } 99060219Seric # else 99125698Seric # ifdef WNOHANG 99225698Seric union wait status; 99325698Seric 99463753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 99525698Seric continue; 99656795Seric # else /* WNOHANG */ 99725698Seric auto int status; 99825698Seric 99960219Seric while (wait(&status) > 0) 100025698Seric continue; 100156795Seric # endif /* WNOHANG */ 100260219Seric # endif 100364035Seric # ifdef SYS5SIGNALS 100464035Seric (void) setsignal(SIGCHLD, reapchild); 100558061Seric # endif 100664738Seric errno = olderrno; 100725698Seric } 100855418Seric /* 100955418Seric ** UNSETENV -- remove a variable from the environment 101055418Seric ** 101155418Seric ** Not needed on newer systems. 101255418Seric ** 101355418Seric ** Parameters: 101455418Seric ** name -- the string name of the environment variable to be 101555418Seric ** deleted from the current environment. 101655418Seric ** 101755418Seric ** Returns: 101855418Seric ** none. 101955418Seric ** 102055418Seric ** Globals: 102155418Seric ** environ -- a pointer to the current environment. 102255418Seric ** 102355418Seric ** Side Effects: 102455418Seric ** Modifies environ. 102555418Seric */ 102655418Seric 102763962Seric #ifndef HASUNSETENV 102855418Seric 102955418Seric void 103055418Seric unsetenv(name) 103155418Seric char *name; 103255418Seric { 103355418Seric extern char **environ; 103455418Seric register char **pp; 103555418Seric int len = strlen(name); 103655418Seric 103755418Seric for (pp = environ; *pp != NULL; pp++) 103855418Seric { 103955418Seric if (strncmp(name, *pp, len) == 0 && 104055418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 104155418Seric break; 104255418Seric } 104355418Seric 104455418Seric for (; *pp != NULL; pp++) 104555418Seric *pp = pp[1]; 104655418Seric } 104755418Seric 104863962Seric #endif 104956215Seric /* 105056215Seric ** GETDTABLESIZE -- return number of file descriptors 105156215Seric ** 105256215Seric ** Only on non-BSD systems 105356215Seric ** 105456215Seric ** Parameters: 105556215Seric ** none 105656215Seric ** 105756215Seric ** Returns: 105856215Seric ** size of file descriptor table 105956215Seric ** 106056215Seric ** Side Effects: 106156215Seric ** none 106256215Seric */ 106356215Seric 106463787Seric #ifdef SOLARIS 106563787Seric # include <sys/resource.h> 106663787Seric #endif 106756215Seric 106856215Seric int 106963787Seric getdtsize() 107056215Seric { 107163787Seric #ifdef RLIMIT_NOFILE 107263787Seric struct rlimit rl; 107363787Seric 107463787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 107563787Seric return rl.rlim_cur; 107663787Seric #endif 107763787Seric 107864031Seric # ifdef HASGETDTABLESIZE 107964031Seric return getdtablesize(); 108064031Seric # else 108164031Seric # ifdef _SC_OPEN_MAX 108258689Seric return sysconf(_SC_OPEN_MAX); 108363787Seric # else 108456215Seric return NOFILE; 108563787Seric # endif 108658689Seric # endif 108756215Seric } 108857631Seric /* 108957631Seric ** UNAME -- get the UUCP name of this system. 109057631Seric */ 109157631Seric 109257943Seric #ifndef HASUNAME 109357631Seric 109457631Seric int 109557631Seric uname(name) 109657631Seric struct utsname *name; 109757631Seric { 109857631Seric FILE *file; 109957631Seric char *n; 110057631Seric 110157631Seric name->nodename[0] = '\0'; 110257631Seric 110357661Seric /* try /etc/whoami -- one line with the node name */ 110457631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 110557631Seric { 110657661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 110757631Seric (void) fclose(file); 110857661Seric n = strchr(name->nodename, '\n'); 110957631Seric if (n != NULL) 111057631Seric *n = '\0'; 111157631Seric if (name->nodename[0] != '\0') 111257631Seric return (0); 111357631Seric } 111457631Seric 111557661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 111657631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 111757631Seric { 111857631Seric char buf[MAXLINE]; 111957631Seric 112057631Seric while (fgets(buf, MAXLINE, file) != NULL) 112157631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 112257631Seric NODE_LENGTH, name->nodename) > 0) 112357631Seric break; 112457631Seric (void) fclose(file); 112557631Seric if (name->nodename[0] != '\0') 112657631Seric return (0); 112757631Seric } 112857631Seric 112957631Seric #ifdef TRUST_POPEN 113057631Seric /* 113157631Seric ** Popen is known to have security holes. 113257631Seric */ 113357631Seric 113457661Seric /* try uuname -l to return local name */ 113557631Seric if ((file = popen("uuname -l", "r")) != NULL) 113657631Seric { 113757661Seric (void) fgets(name, NODE_LENGTH + 1, file); 113857631Seric (void) pclose(file); 113957661Seric n = strchr(name, '\n'); 114057631Seric if (n != NULL) 114157631Seric *n = '\0'; 114257661Seric if (name->nodename[0] != '\0') 114357631Seric return (0); 114457631Seric } 114557631Seric #endif 114657631Seric 114757631Seric return (-1); 114857631Seric } 114957943Seric #endif /* HASUNAME */ 115058068Seric /* 115158068Seric ** INITGROUPS -- initialize groups 115258068Seric ** 115358068Seric ** Stub implementation for System V style systems 115458068Seric */ 115558068Seric 115658068Seric #ifndef HASINITGROUPS 115758068Seric 115858068Seric initgroups(name, basegid) 115958068Seric char *name; 116058068Seric int basegid; 116158068Seric { 116258068Seric return 0; 116358068Seric } 116458068Seric 116558068Seric #endif 116658082Seric /* 116759289Seric ** SETSID -- set session id (for non-POSIX systems) 116859289Seric */ 116959289Seric 117059289Seric #ifndef HASSETSID 117159289Seric 117263753Seric pid_t 117363753Seric setsid __P ((void)) 117459289Seric { 117564155Seric #ifdef TIOCNOTTY 117664155Seric int fd; 117764155Seric 117864155Seric fd = open("/dev/tty", 2); 117964155Seric if (fd >= 0) 118064155Seric { 118164155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 118264155Seric (void) close(fd); 118364155Seric } 118464155Seric #endif /* TIOCNOTTY */ 118564705Seric # ifdef SYS5SETPGRP 118663753Seric return setpgrp(); 118763753Seric # else 118864155Seric return setpgid(0, getpid()); 118959289Seric # endif 119059289Seric } 119159289Seric 119259289Seric #endif 119359289Seric /* 119464718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 119564718Seric ** 119664718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 119764718Seric ** instead of a long. This patches things. 119864718Seric */ 119964718Seric 120064718Seric #ifdef DGUX 120164718Seric 120264718Seric #undef inet_addr 120364718Seric 120464718Seric long 120564718Seric dgux_inet_addr(host) 120664718Seric char *host; 120764718Seric { 120864718Seric struct in_addr haddr; 120964718Seric 121064718Seric haddr = inet_addr(host); 121164718Seric return haddr.s_addr; 121264718Seric } 121364718Seric 121464718Seric #endif 121564718Seric /* 121663969Seric ** GETOPT -- for old systems or systems with bogus implementations 121763969Seric */ 121863969Seric 121963969Seric #ifdef NEEDGETOPT 122063969Seric 122163969Seric /* 122263969Seric * Copyright (c) 1985 Regents of the University of California. 122363969Seric * All rights reserved. The Berkeley software License Agreement 122463969Seric * specifies the terms and conditions for redistribution. 122563969Seric */ 122663969Seric 122763969Seric 122863969Seric /* 122963969Seric ** this version hacked to add `atend' flag to allow state machine 123063969Seric ** to reset if invoked by the program to scan args for a 2nd time 123163969Seric */ 123263969Seric 123363969Seric #if defined(LIBC_SCCS) && !defined(lint) 123463969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 123564072Seric #endif /* LIBC_SCCS and not lint */ 123663969Seric 123763969Seric #include <stdio.h> 123863969Seric 123963969Seric /* 124063969Seric * get option letter from argument vector 124163969Seric */ 124263969Seric int opterr = 1, /* if error message should be printed */ 124363969Seric optind = 1, /* index into parent argv vector */ 124463969Seric optopt; /* character checked for validity */ 124563969Seric char *optarg; /* argument associated with option */ 124663969Seric 124763969Seric #define BADCH (int)'?' 124863969Seric #define EMSG "" 124963969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 125063969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 125163969Seric 125263969Seric getopt(nargc,nargv,ostr) 125363969Seric int nargc; 125463969Seric char **nargv, 125563969Seric *ostr; 125663969Seric { 125763969Seric static char *place = EMSG; /* option letter processing */ 125863969Seric static char atend = 0; 125963969Seric register char *oli; /* option letter list index */ 126063969Seric 126163969Seric if (atend) { 126263969Seric atend = 0; 126363969Seric place = EMSG; 126463969Seric } 126563969Seric if(!*place) { /* update scanning pointer */ 126664136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 126763969Seric atend++; 126863969Seric return(EOF); 126963969Seric } 127063969Seric if (*place == '-') { /* found "--" */ 127163969Seric ++optind; 127263969Seric atend++; 127363969Seric return(EOF); 127463969Seric } 127563969Seric } /* option letter okay? */ 127664125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 127764136Seric if (!*place) ++optind; 127863969Seric tell(": illegal option -- "); 127963969Seric } 128063969Seric if (*++oli != ':') { /* don't need argument */ 128163969Seric optarg = NULL; 128263969Seric if (!*place) ++optind; 128363969Seric } 128463969Seric else { /* need an argument */ 128563969Seric if (*place) optarg = place; /* no white space */ 128663969Seric else if (nargc <= ++optind) { /* no arg */ 128763969Seric place = EMSG; 128863969Seric tell(": option requires an argument -- "); 128963969Seric } 129063969Seric else optarg = nargv[optind]; /* white space */ 129163969Seric place = EMSG; 129263969Seric ++optind; 129363969Seric } 129463969Seric return(optopt); /* dump back option letter */ 129563969Seric } 129663969Seric 129763969Seric #endif 129863969Seric /* 129963969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 130063969Seric */ 130163969Seric 130263969Seric #ifdef NEEDVPRINTF 130363969Seric 130463969Seric #define MAXARG 16 130563969Seric 130663969Seric vfprintf(fp, fmt, ap) 130763969Seric FILE * fp; 130863969Seric char * fmt; 130963969Seric char ** ap; 131063969Seric { 131163969Seric char * bp[MAXARG]; 131263969Seric int i = 0; 131363969Seric 131463969Seric while (*ap && i < MAXARG) 131563969Seric bp[i++] = *ap++; 131663969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 131763969Seric bp[4], bp[5], bp[6], bp[7], 131863969Seric bp[8], bp[9], bp[10], bp[11], 131963969Seric bp[12], bp[13], bp[14], bp[15]); 132063969Seric } 132163969Seric 132263969Seric vsprintf(s, fmt, ap) 132363969Seric char * s; 132463969Seric char * fmt; 132563969Seric char ** ap; 132663969Seric { 132763969Seric char * bp[MAXARG]; 132863969Seric int i = 0; 132963969Seric 133063969Seric while (*ap && i < MAXARG) 133163969Seric bp[i++] = *ap++; 133263969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 133363969Seric bp[4], bp[5], bp[6], bp[7], 133463969Seric bp[8], bp[9], bp[10], bp[11], 133563969Seric bp[12], bp[13], bp[14], bp[15]); 133663969Seric } 133763969Seric 133863969Seric #endif 133963969Seric /* 134064311Seric ** FREESPACE -- see how much free space is on the queue filesystem 134158082Seric ** 134258082Seric ** Only implemented if you have statfs. 134358082Seric ** 134458082Seric ** Parameters: 134564311Seric ** dir -- the directory in question. 134664311Seric ** bsize -- a variable into which the filesystem 134764311Seric ** block size is stored. 134858082Seric ** 134958082Seric ** Returns: 135064311Seric ** The number of bytes free on the queue filesystem. 135164311Seric ** -1 if the statfs call fails. 135264311Seric ** 135364311Seric ** Side effects: 135464311Seric ** Puts the filesystem block size into bsize. 135558082Seric */ 135658082Seric 135758082Seric #ifdef HASSTATFS 135858157Seric # undef HASUSTAT 135958157Seric #endif 136058157Seric 136158157Seric #if defined(HASUSTAT) 136258157Seric # include <ustat.h> 136358157Seric #endif 136458157Seric 136558157Seric #ifdef HASSTATFS 1366*64914Seric # if defined(IRIX) || defined(apollo) || defined(_SCO_unix_) || defined(UMAXV) || defined(DGUX) || defined(_AIX3) 136758133Seric # include <sys/statfs.h> 136858133Seric # else 136964816Seric # if (defined(sun) && !defined(BSD)) || defined(__hpux) || defined(_CONVEX_SOURCE) || defined(NeXT) || defined(_AUX_SOURCE) || defined(MACH386) 137058133Seric # include <sys/vfs.h> 137158133Seric # else 137258157Seric # include <sys/mount.h> 137358133Seric # endif 137458133Seric # endif 137558082Seric #endif 137658082Seric 137764311Seric long 137864311Seric freespace(dir, bsize) 137964311Seric char *dir; 138064311Seric long *bsize; 138158082Seric { 138258160Seric #if defined(HASSTATFS) || defined(HASUSTAT) 138358157Seric # if defined(HASUSTAT) 138458153Seric struct ustat fs; 138558153Seric struct stat statbuf; 138658366Seric # define FSBLOCKSIZE DEV_BSIZE 138758157Seric # define f_bavail f_tfree 138858157Seric # else 138958157Seric # if defined(ultrix) 139058157Seric struct fs_data fs; 139158157Seric # define f_bavail fd_bfreen 139258366Seric # define FSBLOCKSIZE fs.fd_bsize 139358153Seric # else 139458082Seric struct statfs fs; 139558366Seric # define FSBLOCKSIZE fs.f_bsize 139664336Seric # if defined(_SCO_unix_) || defined(IRIX) 139763987Seric # define f_bavail f_bfree 139863987Seric # endif 139958153Seric # endif 140058133Seric # endif 140158082Seric extern int errno; 140258082Seric 140358157Seric # if defined(HASUSTAT) 140464311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 140558157Seric # else 140664718Seric # if defined(IRIX) || defined(apollo) || defined(UMAXV) || defined(DGUX) 140764311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 140858157Seric # else 140958157Seric # if defined(ultrix) 141064311Seric if (statfs(dir, &fs) > 0) 141158153Seric # else 141264311Seric if (statfs(dir, &fs) == 0) 141358153Seric # endif 141458133Seric # endif 141558133Seric # endif 141658133Seric { 141764311Seric if (bsize != NULL) 141864311Seric *bsize = FSBLOCKSIZE; 141964311Seric return (fs.f_bavail); 142064311Seric } 142164337Seric #endif 142264311Seric return (-1); 142364311Seric } 142464311Seric /* 142564311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 142664311Seric ** 142764311Seric ** Only implemented if you have statfs. 142864311Seric ** 142964311Seric ** Parameters: 143064311Seric ** msize -- the size to check against. If zero, we don't yet 143164311Seric ** know how big the message will be, so just check for 143264311Seric ** a "reasonable" amount. 143364311Seric ** 143464311Seric ** Returns: 143564311Seric ** TRUE if there is enough space. 143664311Seric ** FALSE otherwise. 143764311Seric */ 143864311Seric 143964311Seric bool 144064311Seric enoughspace(msize) 144164311Seric long msize; 144264311Seric { 144364311Seric long bfree, bsize; 144464311Seric 144564311Seric if (MinBlocksFree <= 0 && msize <= 0) 144664311Seric { 144758133Seric if (tTd(4, 80)) 144864311Seric printf("enoughspace: no threshold\n"); 144964311Seric return TRUE; 145064311Seric } 145164311Seric 145264311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 145364311Seric { 145464311Seric if (tTd(4, 80)) 145558333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 145664311Seric bfree, msize); 145758333Seric 145858333Seric /* convert msize to block count */ 145964311Seric msize = msize / bsize + 1; 146058333Seric if (MinBlocksFree >= 0) 146158333Seric msize += MinBlocksFree; 146258333Seric 146364311Seric if (bfree < msize) 146458090Seric { 146558090Seric #ifdef LOG 146658090Seric if (LogLevel > 0) 146764254Seric syslog(LOG_ALERT, 146864254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 146964311Seric CurEnv->e_id, bfree, 147064254Seric CurHostName, msize, QueueDir); 147158090Seric #endif 147258082Seric return FALSE; 147358090Seric } 147458082Seric } 147558082Seric else if (tTd(4, 80)) 147658333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 147758333Seric MinBlocksFree, msize, errstring(errno)); 147858082Seric return TRUE; 147958082Seric } 148058542Seric /* 148158542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 148258542Seric ** 148358542Seric ** This looks at an errno value and tells if this is likely to 148458542Seric ** go away if retried later. 148558542Seric ** 148658542Seric ** Parameters: 148758542Seric ** err -- the errno code to classify. 148858542Seric ** 148958542Seric ** Returns: 149058542Seric ** TRUE if this is probably transient. 149158542Seric ** FALSE otherwise. 149258542Seric */ 149358542Seric 149458542Seric bool 149558542Seric transienterror(err) 149658542Seric int err; 149758542Seric { 149858542Seric switch (err) 149958542Seric { 150058542Seric case EIO: /* I/O error */ 150158542Seric case ENXIO: /* Device not configured */ 150258542Seric case EAGAIN: /* Resource temporarily unavailable */ 150358542Seric case ENOMEM: /* Cannot allocate memory */ 150458542Seric case ENODEV: /* Operation not supported by device */ 150558542Seric case ENFILE: /* Too many open files in system */ 150658542Seric case EMFILE: /* Too many open files */ 150758542Seric case ENOSPC: /* No space left on device */ 150858542Seric #ifdef ETIMEDOUT 150958542Seric case ETIMEDOUT: /* Connection timed out */ 151058542Seric #endif 151158542Seric #ifdef ESTALE 151258542Seric case ESTALE: /* Stale NFS file handle */ 151358542Seric #endif 151458542Seric #ifdef ENETDOWN 151558542Seric case ENETDOWN: /* Network is down */ 151658542Seric #endif 151758542Seric #ifdef ENETUNREACH 151858542Seric case ENETUNREACH: /* Network is unreachable */ 151958542Seric #endif 152058542Seric #ifdef ENETRESET 152158542Seric case ENETRESET: /* Network dropped connection on reset */ 152258542Seric #endif 152358542Seric #ifdef ECONNABORTED 152458542Seric case ECONNABORTED: /* Software caused connection abort */ 152558542Seric #endif 152658542Seric #ifdef ECONNRESET 152758542Seric case ECONNRESET: /* Connection reset by peer */ 152858542Seric #endif 152958542Seric #ifdef ENOBUFS 153058542Seric case ENOBUFS: /* No buffer space available */ 153158542Seric #endif 153258542Seric #ifdef ESHUTDOWN 153358542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 153458542Seric #endif 153558542Seric #ifdef ECONNREFUSED 153658542Seric case ECONNREFUSED: /* Connection refused */ 153758542Seric #endif 153858542Seric #ifdef EHOSTDOWN 153958542Seric case EHOSTDOWN: /* Host is down */ 154058542Seric #endif 154158542Seric #ifdef EHOSTUNREACH 154258542Seric case EHOSTUNREACH: /* No route to host */ 154358542Seric #endif 154458542Seric #ifdef EDQUOT 154558542Seric case EDQUOT: /* Disc quota exceeded */ 154658542Seric #endif 154758542Seric #ifdef EPROCLIM 154858542Seric case EPROCLIM: /* Too many processes */ 154958542Seric #endif 155058542Seric #ifdef EUSERS 155158542Seric case EUSERS: /* Too many users */ 155258542Seric #endif 155358542Seric #ifdef EDEADLK 155458542Seric case EDEADLK: /* Resource deadlock avoided */ 155558542Seric #endif 155658542Seric #ifdef EISCONN 155758542Seric case EISCONN: /* Socket already connected */ 155858542Seric #endif 155958542Seric #ifdef EINPROGRESS 156058542Seric case EINPROGRESS: /* Operation now in progress */ 156158542Seric #endif 156258542Seric #ifdef EALREADY 156358542Seric case EALREADY: /* Operation already in progress */ 156458542Seric #endif 156558542Seric #ifdef EADDRINUSE 156658542Seric case EADDRINUSE: /* Address already in use */ 156758542Seric #endif 156858542Seric #ifdef EADDRNOTAVAIL 156958542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 157058542Seric #endif 157163834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 157258542Seric case ENOSR: /* Out of streams resources */ 157358542Seric #endif 157458542Seric return TRUE; 157558542Seric } 157658542Seric 157758542Seric /* nope, must be permanent */ 157858542Seric return FALSE; 157958542Seric } 158058689Seric /* 158164035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 158258689Seric ** 158358689Seric ** Parameters: 158458689Seric ** fd -- the file descriptor of the file. 158558689Seric ** filename -- the file name (for error messages). 158664335Seric ** ext -- the filename extension. 158758689Seric ** type -- type of the lock. Bits can be: 158858689Seric ** LOCK_EX -- exclusive lock. 158958689Seric ** LOCK_NB -- non-blocking. 159058689Seric ** 159158689Seric ** Returns: 159258689Seric ** TRUE if the lock was acquired. 159358689Seric ** FALSE otherwise. 159458689Seric */ 159558689Seric 159658689Seric bool 159764335Seric lockfile(fd, filename, ext, type) 159858689Seric int fd; 159958689Seric char *filename; 160064335Seric char *ext; 160158689Seric int type; 160258689Seric { 160364035Seric # ifndef HASFLOCK 160458689Seric int action; 160558689Seric struct flock lfd; 160664335Seric 160764335Seric if (ext == NULL) 160864335Seric ext = ""; 160964035Seric 161063983Seric bzero(&lfd, sizeof lfd); 161159447Seric if (bitset(LOCK_UN, type)) 161259447Seric lfd.l_type = F_UNLCK; 161359447Seric else if (bitset(LOCK_EX, type)) 161458689Seric lfd.l_type = F_WRLCK; 161558689Seric else 161658689Seric lfd.l_type = F_RDLCK; 161758689Seric 161858689Seric if (bitset(LOCK_NB, type)) 161958689Seric action = F_SETLK; 162058689Seric else 162158689Seric action = F_SETLKW; 162258689Seric 162364264Seric if (tTd(55, 60)) 162464335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 162564335Seric filename, ext, action, lfd.l_type); 162664264Seric 162758689Seric if (fcntl(fd, action, &lfd) >= 0) 162864264Seric { 162964264Seric if (tTd(55, 60)) 163064264Seric printf("SUCCESS\n"); 163158689Seric return TRUE; 163264264Seric } 163358689Seric 163464264Seric if (tTd(55, 60)) 163564264Seric printf("(%s) ", errstring(errno)); 163664264Seric 163764136Seric /* 163864136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 163964136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 164064136Seric ** as type "tmp" (that is, served from swap space), the 164164136Seric ** previous fcntl will fail with "Invalid argument" errors. 164264136Seric ** Since this is fairly common during testing, we will assume 164364136Seric ** that this indicates that the lock is successfully grabbed. 164464136Seric */ 164564136Seric 164664136Seric if (errno == EINVAL) 164764264Seric { 164864264Seric if (tTd(55, 60)) 164964264Seric printf("SUCCESS\n"); 165064136Seric return TRUE; 165164264Seric } 165264136Seric 165358689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 165464378Seric { 165564378Seric int omode = -1; 165664378Seric # ifdef F_GETFL 165764378Seric int oerrno = errno; 165864378Seric 165964378Seric (void) fcntl(fd, F_GETFL, &omode); 166064378Seric errno = oerrno; 166164378Seric # endif 166264378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 166364378Seric filename, ext, fd, type, omode, geteuid()); 166464378Seric } 166558689Seric # else 166664335Seric if (ext == NULL) 166764335Seric ext = ""; 166864335Seric 166964264Seric if (tTd(55, 60)) 167064335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 167164264Seric 167258689Seric if (flock(fd, type) >= 0) 167364264Seric { 167464264Seric if (tTd(55, 60)) 167564264Seric printf("SUCCESS\n"); 167658689Seric return TRUE; 167764264Seric } 167858689Seric 167964264Seric if (tTd(55, 60)) 168064264Seric printf("(%s) ", errstring(errno)); 168164264Seric 168258689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 168364378Seric { 168464378Seric int omode = -1; 168564378Seric # ifdef F_GETFL 168664378Seric int oerrno = errno; 168764378Seric 168864378Seric (void) fcntl(fd, F_GETFL, &omode); 168964378Seric errno = oerrno; 169064378Seric # endif 169164378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 169264378Seric filename, ext, fd, type, omode, geteuid()); 169364386Seric } 169458689Seric # endif 169564264Seric if (tTd(55, 60)) 169664264Seric printf("FAIL\n"); 169758689Seric return FALSE; 169858689Seric } 169964556Seric /* 170064556Seric ** GETCFNAME -- return the name of the .cf file. 170164556Seric ** 170264556Seric ** Some systems (e.g., NeXT) determine this dynamically. 170364556Seric */ 170464556Seric 170564556Seric char * 170664556Seric getcfname() 170764556Seric { 170864556Seric if (ConfFile != NULL) 170964556Seric return ConfFile; 171064556Seric return _PATH_SENDMAILCF; 171164556Seric } 171264718Seric /* 171364718Seric ** SETVENDOR -- process vendor code from V configuration line 171464718Seric ** 171564718Seric ** Parameters: 171664718Seric ** vendor -- string representation of vendor. 171764718Seric ** 171864718Seric ** Returns: 171964718Seric ** TRUE -- if ok. 172064718Seric ** FALSE -- if vendor code could not be processed. 172164718Seric */ 172264718Seric 172364718Seric bool 172464718Seric setvendor(vendor) 172564718Seric char *vendor; 172664718Seric { 172764718Seric return (strcasecmp(vendor, "Berkeley") == 0); 172864718Seric } 172964816Seric /* 173064816Seric ** STRTOL -- convert string to long integer 173164816Seric ** 173264816Seric ** For systems that don't have it in the C library. 173364816Seric */ 173464816Seric 173564816Seric #ifdef NEEDSTRTOL 173664816Seric 173764816Seric long 173864816Seric strtol(p, ep, b) 173964816Seric char *p; 174064816Seric char **ep; 174164816Seric int b; 174264816Seric { 174364816Seric long l = 0; 174464816Seric char c; 174564816Seric char maxd; 174664816Seric int neg = 1; 174764816Seric 174864816Seric maxd = (b > 10) ? '9' : b + '0'; 174964816Seric 175064816Seric if (p && *p == '-') { 175164816Seric neg = -1; 175264816Seric p++; 175364816Seric } 175464816Seric while (p && (c = *p)) { 175564816Seric if (c >= '0' && c <= maxd) { 175664816Seric l = l*b + *p++ - '0'; 175764816Seric continue; 175864816Seric } 175964816Seric if (c >= 'A' && c <= 'Z') 176064816Seric c -= 'A' + 'a'; 176164816Seric c = c - 'a' + 10; 176264816Seric if (b > c) { 176364816Seric l = l*b + c; 176464816Seric p++; 176564816Seric continue; 176664816Seric } 176764816Seric break; 176864816Seric } 176964816Seric l *= neg; 177064816Seric if (ep) 177164816Seric *ep = p; 177264816Seric return l; 177364816Seric } 177464816Seric 177564816Seric #endif 177664841Seric /* 177764841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 177864841Seric ** 177964841Seric ** Solaris versions prior through 2.3 don't properly deliver a 178064841Seric ** canonical h_name field. This tries to work around it. 178164841Seric */ 178264841Seric 178364841Seric #ifdef SOLARIS 178464841Seric 178564841Seric struct hostent * 178664841Seric solaris_gethostbyname(name) 178764841Seric char *name; 178864841Seric { 178964841Seric # ifdef SOLARIS_2_3 179064841Seric static struct hostent hp; 179164841Seric static char buf[1000]; 179264841Seric extern struct hostent *_switch_gethostbyname_r(); 179364841Seric 179464841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 179564841Seric # else 179664841Seric return __switch_gethostbyname(name); 179764841Seric # endif 179864841Seric } 179964841Seric 180064841Seric struct hostent * 180164841Seric solaris_gethostbyaddr(addr, len, type) 180264841Seric char *addr; 180364841Seric int len; 180464841Seric int type; 180564841Seric { 180664841Seric # ifdef SOLARIS_2_3 180764841Seric static struct hostent hp; 180864841Seric static char buf[1000]; 180964841Seric extern struct hostent *_switch_gethostbyaddr_r(); 181064841Seric 181164841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 181264841Seric # else 181364841Seric return __switch_gethostbyaddr(addr, len, type); 181464841Seric # endif 181564841Seric } 181664841Seric 181764841Seric #endif 1818