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*67447Seric static char sccsid[] = "@(#)conf.c 8.94 (Berkeley) 06/25/94"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1363937Seric # include "sendmail.h" 1463937Seric # include "pathnames.h" 1514881Seric # include <sys/ioctl.h> 1658082Seric # include <sys/param.h> 1765018Seric # include <netdb.h> 1836928Sbostic # include <pwd.h> 19404Seric 20294Seric /* 213309Seric ** CONF.C -- Sendmail Configuration Tables. 22294Seric ** 23294Seric ** Defines the configuration of this installation. 24294Seric ** 251388Seric ** Configuration Variables: 262897Seric ** HdrInfo -- a table describing well-known header fields. 272897Seric ** Each entry has the field name and some flags, 284147Seric ** which are described in sendmail.h. 294093Seric ** 304093Seric ** Notes: 314093Seric ** I have tried to put almost all the reasonable 324093Seric ** configuration information into the configuration 334093Seric ** file read at runtime. My intent is that anything 344093Seric ** here is a function of the version of UNIX you 354093Seric ** are running, or is really static -- for example 364093Seric ** the headers are a superset of widely used 374093Seric ** protocols. If you find yourself playing with 384093Seric ** this file too much, you may be making a mistake! 39294Seric */ 40294Seric 41294Seric 42294Seric 43294Seric 444437Seric /* 452897Seric ** Header info table 463057Seric ** Final (null) entry contains the flags used for any other field. 474147Seric ** 484147Seric ** Not all of these are actually handled specially by sendmail 494147Seric ** at this time. They are included as placeholders, to let 504147Seric ** you know that "someday" I intend to have sendmail do 514147Seric ** something with them. 522897Seric */ 532897Seric 542897Seric struct hdrinfo HdrInfo[] = 552897Seric { 568060Seric /* originator fields, most to least significant */ 5711417Seric "resent-sender", H_FROM|H_RESENT, 5811417Seric "resent-from", H_FROM|H_RESENT, 5925686Seric "resent-reply-to", H_FROM|H_RESENT, 609055Seric "sender", H_FROM, 619055Seric "from", H_FROM, 6225686Seric "reply-to", H_FROM, 639055Seric "full-name", H_ACHECK, 6464682Seric "return-receipt-to", H_FROM|H_RECEIPTTO, 6557359Seric "errors-to", H_FROM|H_ERRORSTO, 6658796Seric 678060Seric /* destination fields */ 689055Seric "to", H_RCPT, 6911417Seric "resent-to", H_RCPT|H_RESENT, 709055Seric "cc", H_RCPT, 7111417Seric "resent-cc", H_RCPT|H_RESENT, 729055Seric "bcc", H_RCPT|H_ACHECK, 7311417Seric "resent-bcc", H_RCPT|H_ACHECK|H_RESENT, 7456215Seric "apparently-to", H_RCPT, 7558796Seric 768060Seric /* message identification and control */ 7711417Seric "message-id", 0, 7811417Seric "resent-message-id", H_RESENT, 799055Seric "message", H_EOH, 809055Seric "text", H_EOH, 8158796Seric 8211417Seric /* date fields */ 8311417Seric "date", 0, 8411417Seric "resent-date", H_RESENT, 8558796Seric 868060Seric /* trace fields */ 879055Seric "received", H_TRACE|H_FORCE, 8859318Seric "x400-received", H_TRACE|H_FORCE, 899055Seric "via", H_TRACE|H_FORCE, 909055Seric "mail-from", H_TRACE|H_FORCE, 918060Seric 9258796Seric /* miscellaneous fields */ 9358796Seric "comments", H_FORCE, 9463753Seric "return-path", H_FORCE|H_ACHECK, 9558796Seric 969055Seric NULL, 0, 972897Seric }; 984166Seric 994166Seric 1004166Seric 1014282Seric /* 1024282Seric ** Location of system files/databases/etc. 1034282Seric */ 1044282Seric 10558082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1069039Seric 1079064Seric 1089064Seric 1099039Seric /* 11058082Seric ** Privacy values 11158082Seric */ 11258082Seric 11358082Seric struct prival PrivacyValues[] = 11458082Seric { 11558082Seric "public", PRIV_PUBLIC, 11658082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11758114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11858082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 11958082Seric "noexpn", PRIV_NOEXPN, 12058082Seric "novrfy", PRIV_NOVRFY, 12164333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12264333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12358789Seric "authwarnings", PRIV_AUTHWARNINGS, 12466783Seric "noreceipts", PRIV_NORECEIPTS, 12558082Seric "goaway", PRIV_GOAWAY, 12658789Seric NULL, 0, 12758082Seric }; 12858082Seric 12958082Seric 13058082Seric 13158082Seric /* 13224943Seric ** Miscellaneous stuff. 1339039Seric */ 1349039Seric 13524943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13664644Seric 13764644Seric 13864644Seric /* 13964644Seric ** Following should be config parameters (and probably will be in 14064644Seric ** future releases). In the meantime, setting these is considered 14164644Seric ** unsupported, and is intentionally undocumented. 14264644Seric */ 14364644Seric 14464644Seric #ifdef BROKENSMTPPEERS 14564644Seric bool BrokenSmtpPeers = TRUE; /* set if you have broken SMTP peers */ 14664644Seric #else 14764644Seric bool BrokenSmtpPeers = FALSE; /* set if you have broken SMTP peers */ 14864644Seric #endif 14964644Seric #ifdef NOLOOPBACKCHECK 15064644Seric bool CheckLoopBack = FALSE; /* set to check HELO loopback */ 15164644Seric #else 15264644Seric bool CheckLoopBack = TRUE; /* set to check HELO loopback */ 15364644Seric #endif 15464644Seric 15524943Seric /* 15624943Seric ** SETDEFAULTS -- set default values 15724943Seric ** 15824943Seric ** Because of the way freezing is done, these must be initialized 15924943Seric ** using direct code. 16024943Seric ** 16124943Seric ** Parameters: 16258734Seric ** e -- the default envelope. 16324943Seric ** 16424943Seric ** Returns: 16524943Seric ** none. 16624943Seric ** 16724943Seric ** Side Effects: 16824943Seric ** Initializes a bunch of global variables to their 16924943Seric ** default values. 17024943Seric */ 17124943Seric 17258737Seric #define DAYS * 24 * 60 * 60 17358737Seric 17458734Seric setdefaults(e) 17558734Seric register ENVELOPE *e; 17624943Seric { 17757438Seric SpaceSub = ' '; /* option B */ 17857438Seric QueueLA = 8; /* option x */ 17957438Seric RefuseLA = 12; /* option X */ 18057438Seric WkRecipFact = 30000L; /* option y */ 18157438Seric WkClassFact = 1800L; /* option z */ 18257438Seric WkTimeFact = 90000L; /* option Z */ 18357438Seric QueueFactor = WkRecipFact * 20; /* option q */ 18463787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 18557438Seric /* option F */ 18657438Seric DefUid = 1; /* option u */ 18757438Seric DefGid = 1; /* option g */ 18857438Seric CheckpointInterval = 10; /* option C */ 18957438Seric MaxHopCount = 25; /* option h */ 19058734Seric e->e_sendmode = SM_FORK; /* option d */ 19158734Seric e->e_errormode = EM_PRINT; /* option e */ 19259709Seric SevenBit = FALSE; /* option 7 */ 19357438Seric MaxMciCache = 1; /* option k */ 19457438Seric MciCacheTimeout = 300; /* option K */ 19557438Seric LogLevel = 9; /* option L */ 19658112Seric settimeouts(NULL); /* option r */ 19758737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 19858737Seric TimeOuts.to_q_warning = 0; /* option T */ 19958853Seric PrivacyFlags = 0; /* option p */ 20040973Sbostic setdefuser(); 20153654Seric setupmaps(); 20257402Seric setupmailers(); 20324943Seric } 204294Seric 20540973Sbostic 2064326Seric /* 20740973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20840973Sbostic */ 20940973Sbostic 21040973Sbostic setdefuser() 21140973Sbostic { 21240973Sbostic struct passwd *defpwent; 21357386Seric static char defuserbuf[40]; 21440973Sbostic 21557386Seric DefUser = defuserbuf; 21640973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 21757386Seric strcpy(defuserbuf, defpwent->pw_name); 21840973Sbostic else 21957386Seric strcpy(defuserbuf, "nobody"); 22040973Sbostic } 22153654Seric /* 22256836Seric ** HOST_MAP_INIT -- initialize host class structures 22356836Seric */ 22456836Seric 22556836Seric bool 22660219Seric host_map_init(map, args) 22756836Seric MAP *map; 22856836Seric char *args; 22956836Seric { 23056836Seric register char *p = args; 23156836Seric 23256836Seric for (;;) 23356836Seric { 23458050Seric while (isascii(*p) && isspace(*p)) 23556836Seric p++; 23656836Seric if (*p != '-') 23756836Seric break; 23856836Seric switch (*++p) 23956836Seric { 24056836Seric case 'a': 24156836Seric map->map_app = ++p; 24256836Seric break; 24356836Seric } 24458050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 24556836Seric p++; 24656836Seric if (*p != '\0') 24756836Seric *p++ = '\0'; 24856836Seric } 24956836Seric if (map->map_app != NULL) 25056836Seric map->map_app = newstr(map->map_app); 25156836Seric return TRUE; 25256836Seric } 25357402Seric /* 25457402Seric ** SETUPMAILERS -- initialize default mailers 25557402Seric */ 25656836Seric 25757402Seric setupmailers() 25857402Seric { 25957402Seric char buf[100]; 26057402Seric 26157403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 26257403Seric makemailer(buf); 26357403Seric 26459883Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE"); 26557402Seric makemailer(buf); 26657402Seric 26757402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 26857402Seric makemailer(buf); 26957402Seric } 27056836Seric /* 27160207Seric ** SETUPMAPS -- set up map classes 27260207Seric */ 27360207Seric 27460207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 27560207Seric { \ 27660207Seric extern bool parse __P((MAP *, char *)); \ 27760207Seric extern bool open __P((MAP *, int)); \ 27860207Seric extern void close __P((MAP *)); \ 27960207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 28060207Seric extern void store __P((MAP *, char *, char *)); \ 28160207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 28260207Seric s->s_mapclass.map_cname = name; \ 28360207Seric s->s_mapclass.map_ext = ext; \ 28460207Seric s->s_mapclass.map_cflags = flags; \ 28560207Seric s->s_mapclass.map_parse = parse; \ 28660207Seric s->s_mapclass.map_open = open; \ 28760207Seric s->s_mapclass.map_close = close; \ 28860207Seric s->s_mapclass.map_lookup = lookup; \ 28960207Seric s->s_mapclass.map_store = store; \ 29060207Seric } 29160207Seric 29260207Seric setupmaps() 29360207Seric { 29460207Seric register STAB *s; 29560207Seric 29660207Seric #ifdef NEWDB 29760207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29860207Seric map_parseargs, hash_map_open, db_map_close, 29960207Seric db_map_lookup, db_map_store); 30060207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 30160207Seric map_parseargs, bt_map_open, db_map_close, 30260207Seric db_map_lookup, db_map_store); 30360207Seric #endif 30460207Seric 30560207Seric #ifdef NDBM 30660207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 30760207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 30860207Seric ndbm_map_lookup, ndbm_map_store); 30960207Seric #endif 31060207Seric 31160207Seric #ifdef NIS 31260207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 31360207Seric map_parseargs, nis_map_open, nis_map_close, 31460207Seric nis_map_lookup, nis_map_store); 31560207Seric #endif 31660207Seric 31760207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 31860207Seric map_parseargs, stab_map_open, stab_map_close, 31960207Seric stab_map_lookup, stab_map_store); 32060207Seric 32160207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 32260207Seric map_parseargs, impl_map_open, impl_map_close, 32360207Seric impl_map_lookup, impl_map_store); 32460207Seric 32560207Seric /* host DNS lookup */ 32660207Seric MAPDEF("host", NULL, 0, 32760207Seric host_map_init, null_map_open, null_map_close, 32860207Seric host_map_lookup, null_map_store); 32960207Seric 33060207Seric /* dequote map */ 33160207Seric MAPDEF("dequote", NULL, 0, 33260207Seric dequote_init, null_map_open, null_map_close, 33360207Seric dequote_map, null_map_store); 33460207Seric 33560207Seric #if 0 33660207Seric # ifdef USERDB 33760207Seric /* user database */ 33860207Seric MAPDEF("udb", ".db", 0, 33960207Seric udb_map_parse, null_map_open, null_map_close, 34060207Seric udb_map_lookup, null_map_store); 34160207Seric # endif 34260207Seric #endif 34360207Seric } 34460207Seric 34560207Seric #undef MAPDEF 34660207Seric /* 3479369Seric ** USERNAME -- return the user id of the logged in user. 3489369Seric ** 3499369Seric ** Parameters: 3509369Seric ** none. 3519369Seric ** 3529369Seric ** Returns: 3539369Seric ** The login name of the logged in user. 3549369Seric ** 3559369Seric ** Side Effects: 3569369Seric ** none. 3579369Seric ** 3589369Seric ** Notes: 3599369Seric ** The return value is statically allocated. 3609369Seric */ 3619369Seric 3629369Seric char * 3639369Seric username() 3649369Seric { 36517469Seric static char *myname = NULL; 3669369Seric extern char *getlogin(); 36719904Smiriam register struct passwd *pw; 3689369Seric 36917469Seric /* cache the result */ 37017469Seric if (myname == NULL) 37117469Seric { 37217469Seric myname = getlogin(); 37317469Seric if (myname == NULL || myname[0] == '\0') 37417469Seric { 37563787Seric pw = getpwuid(RealUid); 37617469Seric if (pw != NULL) 37740993Sbostic myname = newstr(pw->pw_name); 37817469Seric } 37919904Smiriam else 38019904Smiriam { 38163787Seric uid_t uid = RealUid; 38219873Smiriam 38340993Sbostic myname = newstr(myname); 38440993Sbostic if ((pw = getpwnam(myname)) == NULL || 38558736Seric (uid != 0 && uid != pw->pw_uid)) 38619904Smiriam { 38758736Seric pw = getpwuid(uid); 38824945Seric if (pw != NULL) 38940993Sbostic myname = newstr(pw->pw_name); 39019873Smiriam } 39119873Smiriam } 39217469Seric if (myname == NULL || myname[0] == '\0') 39317469Seric { 39458151Seric syserr("554 Who are you?"); 39517469Seric myname = "postmaster"; 39617469Seric } 39717469Seric } 39817469Seric 39917469Seric return (myname); 4009369Seric } 4019369Seric /* 4024190Seric ** TTYPATH -- Get the path of the user's tty 403294Seric ** 404294Seric ** Returns the pathname of the user's tty. Returns NULL if 405294Seric ** the user is not logged in or if s/he has write permission 406294Seric ** denied. 407294Seric ** 408294Seric ** Parameters: 409294Seric ** none 410294Seric ** 411294Seric ** Returns: 412294Seric ** pathname of the user's tty. 413294Seric ** NULL if not logged in or write permission denied. 414294Seric ** 415294Seric ** Side Effects: 416294Seric ** none. 417294Seric ** 418294Seric ** WARNING: 419294Seric ** Return value is in a local buffer. 420294Seric ** 421294Seric ** Called By: 422294Seric ** savemail 423294Seric */ 424294Seric 425294Seric char * 426294Seric ttypath() 427294Seric { 428294Seric struct stat stbuf; 429294Seric register char *pathn; 430294Seric extern char *ttyname(); 4314081Seric extern char *getlogin(); 432294Seric 433294Seric /* compute the pathname of the controlling tty */ 4349369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4359369Seric (pathn = ttyname(0)) == NULL) 436294Seric { 437294Seric errno = 0; 438294Seric return (NULL); 439294Seric } 440294Seric 441294Seric /* see if we have write permission */ 4422967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 443294Seric { 444294Seric errno = 0; 445294Seric return (NULL); 446294Seric } 447294Seric 448294Seric /* see if the user is logged in */ 449294Seric if (getlogin() == NULL) 450294Seric return (NULL); 451294Seric 452294Seric /* looks good */ 453294Seric return (pathn); 454294Seric } 4552967Seric /* 4562967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4572967Seric ** 4582967Seric ** This routine can be supplied on a per-installation basis 4592967Seric ** to determine whether a person is allowed to send a message. 4602967Seric ** This allows restriction of certain types of internet 4612967Seric ** forwarding or registration of users. 4622967Seric ** 4632967Seric ** If the hosts are found to be incompatible, an error 46457454Seric ** message should be given using "usrerr" and 0 should 4652967Seric ** be returned. 4662967Seric ** 4674288Seric ** 'NoReturn' can be set to suppress the return-to-sender 4684288Seric ** function; this should be done on huge messages. 4694288Seric ** 4702967Seric ** Parameters: 4712967Seric ** to -- the person being sent to. 4722967Seric ** 4732967Seric ** Returns: 47457459Seric ** an exit status 4752967Seric ** 4762967Seric ** Side Effects: 4772967Seric ** none (unless you include the usrerr stuff) 4782967Seric */ 4792967Seric 48055012Seric checkcompat(to, e) 4812967Seric register ADDRESS *to; 48255012Seric register ENVELOPE *e; 4832967Seric { 48412133Seric # ifdef lint 48512133Seric if (to == NULL) 48612133Seric to++; 48763834Seric # endif /* lint */ 48864402Seric 48964402Seric if (tTd(49, 1)) 49064402Seric printf("checkcompat(to=%s, from=%s)\n", 49164402Seric to->q_paddr, e->e_from.q_paddr); 49264402Seric 49310698Seric # ifdef EXAMPLE_CODE 49410698Seric /* this code is intended as an example only */ 4954437Seric register STAB *s; 4964437Seric 4974437Seric s = stab("arpa", ST_MAILER, ST_FIND); 49855012Seric if (s != NULL && e->e_from.q_mailer != LocalMailer && 4999369Seric to->q_mailer == s->s_mailer) 5004437Seric { 50158151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 50210698Seric /* NoReturn = TRUE; to supress return copy */ 50357459Seric return (EX_UNAVAILABLE); 5044437Seric } 50556795Seric # endif /* EXAMPLE_CODE */ 50657459Seric return (EX_OK); 5072967Seric } 5089369Seric /* 50964035Seric ** SETSIGNAL -- set a signal handler 51064035Seric ** 51164035Seric ** This is essentially old BSD "signal(3)". 51264035Seric */ 51364035Seric 51464561Seric sigfunc_t 51564035Seric setsignal(sig, handler) 51664035Seric int sig; 51764561Seric sigfunc_t handler; 51864035Seric { 51964380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 52064035Seric return signal(sig, handler); 52164035Seric #else 52264035Seric struct sigaction n, o; 52364035Seric 52464035Seric bzero(&n, sizeof n); 52564035Seric n.sa_handler = handler; 52667159Seric # ifdef SA_RESTART 52767159Seric n.sa_flags = SA_RESTART; 52867159Seric # endif 52964035Seric if (sigaction(sig, &n, &o) < 0) 53064035Seric return SIG_ERR; 53164035Seric return o.sa_handler; 53264035Seric #endif 53364035Seric } 53464035Seric /* 5359369Seric ** HOLDSIGS -- arrange to hold all signals 5369369Seric ** 5379369Seric ** Parameters: 5389369Seric ** none. 5399369Seric ** 5409369Seric ** Returns: 5419369Seric ** none. 5429369Seric ** 5439369Seric ** Side Effects: 5449369Seric ** Arranges that signals are held. 5459369Seric */ 5469369Seric 5479369Seric holdsigs() 5489369Seric { 5499369Seric } 5509369Seric /* 5519369Seric ** RLSESIGS -- arrange to release all signals 5529369Seric ** 5539369Seric ** This undoes the effect of holdsigs. 5549369Seric ** 5559369Seric ** Parameters: 5569369Seric ** none. 5579369Seric ** 5589369Seric ** Returns: 5599369Seric ** none. 5609369Seric ** 5619369Seric ** Side Effects: 5629369Seric ** Arranges that signals are released. 5639369Seric */ 5649369Seric 5659369Seric rlsesigs() 5669369Seric { 5679369Seric } 56814872Seric /* 56964705Seric ** INIT_MD -- do machine dependent initializations 57064705Seric ** 57164705Seric ** Systems that have global modes that should be set should do 57264705Seric ** them here rather than in main. 57364705Seric */ 57464705Seric 57564705Seric #ifdef _AUX_SOURCE 57664705Seric # include <compat.h> 57764705Seric #endif 57864705Seric 57964825Seric init_md(argc, argv) 58064825Seric int argc; 58164825Seric char **argv; 58264705Seric { 58364705Seric #ifdef _AUX_SOURCE 58464705Seric setcompat(getcompat() | COMPAT_BSDPROT); 58564705Seric #endif 58664705Seric } 58764705Seric /* 58814872Seric ** GETLA -- get the current load average 58914872Seric ** 59014881Seric ** This code stolen from la.c. 59114881Seric ** 59214872Seric ** Parameters: 59314872Seric ** none. 59414872Seric ** 59514872Seric ** Returns: 59614872Seric ** The current load average as an integer. 59714872Seric ** 59814872Seric ** Side Effects: 59914872Seric ** none. 60014872Seric */ 60114872Seric 60251920Seric /* try to guess what style of load average we have */ 60351920Seric #define LA_ZERO 1 /* always return load average as zero */ 60464360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 60551920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 60651920Seric #define LA_SUBR 4 /* call getloadavg */ 60764295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 60864360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 60966301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 61014872Seric 61163962Seric /* do guesses based on general OS type */ 61251920Seric #ifndef LA_TYPE 61363962Seric # define LA_TYPE LA_ZERO 61451920Seric #endif 61551920Seric 61664360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 61751920Seric 61814872Seric #include <nlist.h> 61914872Seric 62051920Seric #ifndef LA_AVENRUN 62163962Seric # ifdef SYSTEM5 62263962Seric # define LA_AVENRUN "avenrun" 62363962Seric # else 62463962Seric # define LA_AVENRUN "_avenrun" 62563962Seric # endif 62651920Seric #endif 62751920Seric 62851920Seric /* _PATH_UNIX should be defined in <paths.h> */ 62951920Seric #ifndef _PATH_UNIX 63063962Seric # if defined(SYSTEM5) 63163962Seric # define _PATH_UNIX "/unix" 63263962Seric # else 63363962Seric # define _PATH_UNIX "/vmunix" 63463962Seric # endif 63551920Seric #endif 63651920Seric 63714872Seric struct nlist Nl[] = 63814872Seric { 63951920Seric { LA_AVENRUN }, 64014872Seric #define X_AVENRUN 0 64114872Seric { 0 }, 64214872Seric }; 64314872Seric 64460583Seric #ifndef FSHIFT 64560583Seric # if defined(unixpc) 64660583Seric # define FSHIFT 5 64760583Seric # endif 64857736Seric 64966763Seric # if defined(__alpha) || defined(IRIX) 65060583Seric # define FSHIFT 10 65160583Seric # endif 65266788Seric #endif 65357977Seric 65466788Seric #ifndef FSHIFT 65566788Seric # define FSHIFT 8 65657736Seric #endif 65760583Seric 65866788Seric #ifndef FSCALE 65966788Seric # define FSCALE (1 << FSHIFT) 66051920Seric #endif 66140930Srick 66214872Seric getla() 66314872Seric { 66414872Seric static int kmem = -1; 66551920Seric #if LA_TYPE == LA_INT 66624943Seric long avenrun[3]; 66751920Seric #else 66864360Seric # if LA_TYPE == LA_SHORT 66964360Seric short avenrun[3]; 67064360Seric # else 67151920Seric double avenrun[3]; 67264360Seric # endif 67351920Seric #endif 67425615Seric extern off_t lseek(); 67557736Seric extern int errno; 67614872Seric 67714872Seric if (kmem < 0) 67814872Seric { 67924945Seric kmem = open("/dev/kmem", 0, 0); 68014872Seric if (kmem < 0) 68157736Seric { 68257736Seric if (tTd(3, 1)) 68357736Seric printf("getla: open(/dev/kmem): %s\n", 68457736Seric errstring(errno)); 68514872Seric return (-1); 68657736Seric } 68751920Seric (void) fcntl(kmem, F_SETFD, 1); 68857736Seric if (nlist(_PATH_UNIX, Nl) < 0) 68957736Seric { 69057736Seric if (tTd(3, 1)) 69157736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 69257736Seric errstring(errno)); 69314872Seric return (-1); 69457736Seric } 69559253Seric if (Nl[X_AVENRUN].n_value == 0) 69659253Seric { 69759253Seric if (tTd(3, 1)) 69859253Seric printf("getla: nlist(%s, %s) ==> 0\n", 69959253Seric _PATH_UNIX, LA_AVENRUN); 70059253Seric return (-1); 70159253Seric } 70267435Seric #ifdef NAMELISTMASK 70367435Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 70466763Seric #endif 70514872Seric } 70657736Seric if (tTd(3, 20)) 70757736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 70824945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 70923118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 71019967Seric { 71119967Seric /* thank you Ian */ 71257736Seric if (tTd(3, 1)) 71357736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 71419967Seric return (-1); 71519967Seric } 71664360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 71757736Seric if (tTd(3, 5)) 71857736Seric { 71957736Seric printf("getla: avenrun = %d", avenrun[0]); 72057736Seric if (tTd(3, 15)) 72157736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 72257736Seric printf("\n"); 72357736Seric } 72457736Seric if (tTd(3, 1)) 72557736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 72624943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 72751920Seric #else 72857736Seric if (tTd(3, 5)) 72957736Seric { 73057736Seric printf("getla: avenrun = %g", avenrun[0]); 73157736Seric if (tTd(3, 15)) 73257736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 73357736Seric printf("\n"); 73457736Seric } 73557736Seric if (tTd(3, 1)) 73657736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 73751920Seric return ((int) (avenrun[0] + 0.5)); 73851920Seric #endif 73914872Seric } 74014872Seric 74151773Seric #else 74251920Seric #if LA_TYPE == LA_SUBR 74351773Seric 74464718Seric #ifdef DGUX 74564718Seric 74664718Seric #include <sys/dg_sys_info.h> 74764718Seric 74864718Seric int getla() 74964718Seric { 75064718Seric struct dg_sys_info_load_info load_info; 75164718Seric 75264718Seric dg_sys_info((long *)&load_info, 75364718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 75464718Seric 75564718Seric return((int) (load_info.one_minute + 0.5)); 75664718Seric } 75764718Seric 75864718Seric #else 75964718Seric 76051773Seric getla() 76151773Seric { 76251920Seric double avenrun[3]; 76351920Seric 76451920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 76557736Seric { 76657736Seric if (tTd(3, 1)) 76757736Seric perror("getla: getloadavg failed:"); 76851920Seric return (-1); 76957736Seric } 77057736Seric if (tTd(3, 1)) 77157736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 77251920Seric return ((int) (avenrun[0] + 0.5)); 77351773Seric } 77451773Seric 77564718Seric #endif /* DGUX */ 77651773Seric #else 77764295Seric #if LA_TYPE == LA_MACH 77851773Seric 77964295Seric /* 78065173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 78164295Seric */ 78264295Seric 78365173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 78465173Seric # include <mach/mach.h> 78565173Seric #else 78665173Seric # include <mach.h> 78765173Seric #endif 78864295Seric 78951773Seric getla() 79051773Seric { 79164295Seric processor_set_t default_set; 79264295Seric kern_return_t error; 79364295Seric unsigned int info_count; 79464295Seric struct processor_set_basic_info info; 79564295Seric host_t host; 79664295Seric 79764295Seric error = processor_set_default(host_self(), &default_set); 79864295Seric if (error != KERN_SUCCESS) 79964295Seric return -1; 80064295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 80164295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 80264295Seric &host, (processor_set_info_t)&info, 80364295Seric &info_count) != KERN_SUCCESS) 80464295Seric { 80564295Seric return -1; 80664295Seric } 80764295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 80864295Seric } 80964295Seric 81064295Seric 81164295Seric #else 81266313Seric #if LA_TYPE == LA_PROCSTR 81364295Seric 81466301Seric /* 81566301Seric ** Read /proc/loadavg for the load average. This is assumed to be 81666301Seric ** in a format like "0.15 0.12 0.06". 81766301Seric ** 81866301Seric ** Initially intended for Linux. This has been in the kernel 81966301Seric ** since at least 0.99.15. 82066301Seric */ 82166301Seric 82266301Seric # ifndef _PATH_LOADAVG 82366301Seric # define _PATH_LOADAVG "/proc/loadavg" 82466301Seric # endif 82566301Seric 82666301Seric int 82764295Seric getla() 82864295Seric { 82966319Seric double avenrun; 83066301Seric register int result; 83166319Seric FILE *fp; 83266301Seric 83366319Seric fp = fopen(_PATH_LOADAVG, "r"); 83466319Seric if (fp == NULL) 83566301Seric { 83666319Seric if (tTd(3, 1)) 83766319Seric printf("getla: fopen(%s): %s\n", 83866319Seric _PATH_LOADAVG, errstring(errno)); 83966319Seric return -1; 84066301Seric } 84166301Seric result = fscanf(fp, "%lf", &avenrun); 84266319Seric fclose(fp); 84366301Seric if (result != 1) 84466301Seric { 84566310Seric if (tTd(3, 1)) 84666301Seric printf("getla: fscanf() = %d: %s\n", 84766301Seric result, errstring(errno)); 84866301Seric return -1; 84966301Seric } 85066301Seric 85157736Seric if (tTd(3, 1)) 85266301Seric printf("getla(): %.2f\n", avenrun); 85366301Seric 85466301Seric return ((int) (avenrun + 0.5)); 85566301Seric } 85666301Seric 85766301Seric #else 85866301Seric 85966301Seric getla() 86066301Seric { 86166301Seric if (tTd(3, 1)) 86257736Seric printf("getla: ZERO\n"); 86351920Seric return (0); 86451773Seric } 86551773Seric 86651773Seric #endif 86751773Seric #endif 86864295Seric #endif 86966301Seric #endif 87066045Seric 87166045Seric 87266045Seric /* 87366045Seric * Copyright 1989 Massachusetts Institute of Technology 87466045Seric * 87566045Seric * Permission to use, copy, modify, distribute, and sell this software and its 87666045Seric * documentation for any purpose is hereby granted without fee, provided that 87766045Seric * the above copyright notice appear in all copies and that both that 87866045Seric * copyright notice and this permission notice appear in supporting 87966045Seric * documentation, and that the name of M.I.T. not be used in advertising or 88066045Seric * publicity pertaining to distribution of the software without specific, 88166045Seric * written prior permission. M.I.T. makes no representations about the 88266045Seric * suitability of this software for any purpose. It is provided "as is" 88366045Seric * without express or implied warranty. 88466045Seric * 88566045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 88666045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 88766045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 88866045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 88966045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 89066045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 89166045Seric * 89266045Seric * Authors: Many and varied... 89366045Seric */ 89466045Seric 89566045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 89666045Seric #ifndef lint 89766045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 89866045Seric #endif /* !lint */ 89966045Seric 90066179Seric #ifdef apollo 90166045Seric # undef volatile 90266045Seric # include <apollo/base.h> 90366045Seric 90466045Seric /* ARGSUSED */ 90566045Seric int getloadavg( call_data ) 90666045Seric caddr_t call_data; /* pointer to (double) return value */ 90766045Seric { 90866045Seric double *avenrun = (double *) call_data; 90966045Seric int i; 91066045Seric status_$t st; 91166045Seric long loadav[3]; 91266045Seric proc1_$get_loadav(loadav, &st); 91366045Seric *avenrun = loadav[0] / (double) (1 << 16); 91466045Seric return(0); 91566045Seric } 91666045Seric # endif /* apollo */ 91724943Seric /* 91824943Seric ** SHOULDQUEUE -- should this message be queued or sent? 91924943Seric ** 92024943Seric ** Compares the message cost to the load average to decide. 92124943Seric ** 92224943Seric ** Parameters: 92324943Seric ** pri -- the priority of the message in question. 92457438Seric ** ctime -- the message creation time. 92524943Seric ** 92624943Seric ** Returns: 92724943Seric ** TRUE -- if this message should be queued up for the 92824943Seric ** time being. 92924943Seric ** FALSE -- if the load is low enough to send this message. 93024943Seric ** 93124943Seric ** Side Effects: 93224943Seric ** none. 93324943Seric */ 93424943Seric 93524943Seric bool 93657438Seric shouldqueue(pri, ctime) 93724943Seric long pri; 93857438Seric time_t ctime; 93924943Seric { 94051920Seric if (CurrentLA < QueueLA) 94124943Seric return (FALSE); 94258132Seric if (CurrentLA >= RefuseLA) 94358132Seric return (TRUE); 94451920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 94524943Seric } 94624943Seric /* 94753037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 94853037Seric ** 94953037Seric ** Parameters: 95053037Seric ** none. 95153037Seric ** 95253037Seric ** Returns: 95353037Seric ** TRUE if incoming SMTP connections should be refused 95453037Seric ** (for now). 95553037Seric ** FALSE if we should accept new work. 95653037Seric ** 95753037Seric ** Side Effects: 95853037Seric ** none. 95953037Seric */ 96053037Seric 96153037Seric bool 96253037Seric refuseconnections() 96353037Seric { 96459156Seric #ifdef XLA 96559156Seric if (!xla_smtp_ok()) 96659156Seric return TRUE; 96759156Seric #endif 96859156Seric 96953037Seric /* this is probably too simplistic */ 97058132Seric return (CurrentLA >= RefuseLA); 97153037Seric } 97253037Seric /* 97324943Seric ** SETPROCTITLE -- set process title for ps 97424943Seric ** 97524943Seric ** Parameters: 97658674Seric ** fmt -- a printf style format string. 97758674Seric ** a, b, c -- possible parameters to fmt. 97824943Seric ** 97924943Seric ** Returns: 98024943Seric ** none. 98124943Seric ** 98224943Seric ** Side Effects: 98324943Seric ** Clobbers argv of our main procedure so ps(1) will 98424943Seric ** display the title. 98524943Seric */ 98624943Seric 98758689Seric #ifdef SETPROCTITLE 98866002Seric # ifdef HASSETPROCTITLE 98966002Seric *** ERROR *** Cannot have both SETPROCTITLE and HASSETPROCTITLE defined 99066002Seric # endif 99158689Seric # ifdef __hpux 99258689Seric # include <sys/pstat.h> 99358689Seric # endif 99459732Seric # ifdef BSD4_4 99559732Seric # include <machine/vmparam.h> 99659732Seric # include <sys/exec.h> 99764247Seric # ifdef __bsdi__ 99864247Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 99964843Seric # define PROCTITLEPAD '\0' 100064247Seric # endif 100159992Seric # ifdef PS_STRINGS 100259992Seric # define SETPROC_STATIC static 100359992Seric # endif 100459732Seric # endif 100559732Seric # ifndef SETPROC_STATIC 100659732Seric # define SETPROC_STATIC 100759732Seric # endif 100858689Seric #endif 100958689Seric 101064843Seric #ifndef PROCTITLEPAD 101164843Seric # define PROCTITLEPAD ' ' 101264843Seric #endif 101364843Seric 101465982Seric #ifndef HASSETPROCTITLE 101565982Seric 101624943Seric /*VARARGS1*/ 101757642Seric #ifdef __STDC__ 101857642Seric setproctitle(char *fmt, ...) 101957642Seric #else 102057642Seric setproctitle(fmt, va_alist) 102124943Seric char *fmt; 102257642Seric va_dcl 102357642Seric #endif 102424943Seric { 102524943Seric # ifdef SETPROCTITLE 102624943Seric register char *p; 102725049Seric register int i; 102859732Seric SETPROC_STATIC char buf[MAXLINE]; 102956852Seric VA_LOCAL_DECL 103058689Seric # ifdef __hpux 103158689Seric union pstun pst; 103258689Seric # endif 103324943Seric extern char **Argv; 103424943Seric extern char *LastArgv; 103524943Seric 103658674Seric p = buf; 103724943Seric 103858674Seric /* print sendmail: heading for grep */ 103958674Seric (void) strcpy(p, "sendmail: "); 104058674Seric p += strlen(p); 104124943Seric 104258674Seric /* print the argument string */ 104358674Seric VA_START(fmt); 104458674Seric (void) vsprintf(p, fmt, ap); 104556852Seric VA_END; 104654996Seric 104758674Seric i = strlen(buf); 104858689Seric 104958689Seric # ifdef __hpux 105058689Seric pst.pst_command = buf; 105158689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 105258689Seric # else 105359992Seric # ifdef PS_STRINGS 105459732Seric PS_STRINGS->ps_nargvstr = 1; 105559732Seric PS_STRINGS->ps_argvstr = buf; 105659732Seric # else 105754996Seric if (i > LastArgv - Argv[0] - 2) 105825049Seric { 105954996Seric i = LastArgv - Argv[0] - 2; 106058674Seric buf[i] = '\0'; 106125049Seric } 106258674Seric (void) strcpy(Argv[0], buf); 106354997Seric p = &Argv[0][i]; 106424943Seric while (p < LastArgv) 106564843Seric *p++ = PROCTITLEPAD; 106659732Seric # endif 106758689Seric # endif 106856795Seric # endif /* SETPROCTITLE */ 106924943Seric } 107065982Seric 107165982Seric #endif 107225698Seric /* 107325698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 107425698Seric ** 107525698Seric ** Parameters: 107625698Seric ** none. 107725698Seric ** 107825698Seric ** Returns: 107925698Seric ** none. 108025698Seric ** 108125698Seric ** Side Effects: 108225698Seric ** Picks up extant zombies. 108325698Seric */ 108425698Seric 108546928Sbostic void 108625698Seric reapchild() 108725698Seric { 108864738Seric int olderrno = errno; 108963966Seric # ifdef HASWAITPID 109060219Seric auto int status; 109160560Seric int count; 109260560Seric int pid; 109360219Seric 109460560Seric count = 0; 109560560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 109660560Seric { 109760560Seric if (count++ > 1000) 109860560Seric { 109966748Seric #ifdef LOG 110060560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 110160560Seric pid, status); 110266748Seric #endif 110360560Seric break; 110460560Seric } 110560560Seric } 110660219Seric # else 110725698Seric # ifdef WNOHANG 110825698Seric union wait status; 110925698Seric 111063753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 111125698Seric continue; 111256795Seric # else /* WNOHANG */ 111325698Seric auto int status; 111425698Seric 111560219Seric while (wait(&status) > 0) 111625698Seric continue; 111756795Seric # endif /* WNOHANG */ 111860219Seric # endif 111964035Seric # ifdef SYS5SIGNALS 112064035Seric (void) setsignal(SIGCHLD, reapchild); 112158061Seric # endif 112264738Seric errno = olderrno; 112325698Seric } 112455418Seric /* 112555418Seric ** UNSETENV -- remove a variable from the environment 112655418Seric ** 112755418Seric ** Not needed on newer systems. 112855418Seric ** 112955418Seric ** Parameters: 113055418Seric ** name -- the string name of the environment variable to be 113155418Seric ** deleted from the current environment. 113255418Seric ** 113355418Seric ** Returns: 113455418Seric ** none. 113555418Seric ** 113655418Seric ** Globals: 113755418Seric ** environ -- a pointer to the current environment. 113855418Seric ** 113955418Seric ** Side Effects: 114055418Seric ** Modifies environ. 114155418Seric */ 114255418Seric 114363962Seric #ifndef HASUNSETENV 114455418Seric 114555418Seric void 114655418Seric unsetenv(name) 114755418Seric char *name; 114855418Seric { 114955418Seric extern char **environ; 115055418Seric register char **pp; 115155418Seric int len = strlen(name); 115255418Seric 115355418Seric for (pp = environ; *pp != NULL; pp++) 115455418Seric { 115555418Seric if (strncmp(name, *pp, len) == 0 && 115655418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 115755418Seric break; 115855418Seric } 115955418Seric 116055418Seric for (; *pp != NULL; pp++) 116155418Seric *pp = pp[1]; 116255418Seric } 116355418Seric 116463962Seric #endif 116556215Seric /* 116656215Seric ** GETDTABLESIZE -- return number of file descriptors 116756215Seric ** 116856215Seric ** Only on non-BSD systems 116956215Seric ** 117056215Seric ** Parameters: 117156215Seric ** none 117256215Seric ** 117356215Seric ** Returns: 117456215Seric ** size of file descriptor table 117556215Seric ** 117656215Seric ** Side Effects: 117756215Seric ** none 117856215Seric */ 117956215Seric 118063787Seric #ifdef SOLARIS 118163787Seric # include <sys/resource.h> 118263787Seric #endif 118356215Seric 118456215Seric int 118563787Seric getdtsize() 118656215Seric { 118763787Seric #ifdef RLIMIT_NOFILE 118863787Seric struct rlimit rl; 118963787Seric 119063787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 119163787Seric return rl.rlim_cur; 119263787Seric #endif 119363787Seric 119464031Seric # ifdef HASGETDTABLESIZE 119564031Seric return getdtablesize(); 119664031Seric # else 119764031Seric # ifdef _SC_OPEN_MAX 119858689Seric return sysconf(_SC_OPEN_MAX); 119963787Seric # else 120056215Seric return NOFILE; 120163787Seric # endif 120258689Seric # endif 120356215Seric } 120457631Seric /* 120557631Seric ** UNAME -- get the UUCP name of this system. 120657631Seric */ 120757631Seric 120857943Seric #ifndef HASUNAME 120957631Seric 121057631Seric int 121157631Seric uname(name) 121257631Seric struct utsname *name; 121357631Seric { 121457631Seric FILE *file; 121557631Seric char *n; 121657631Seric 121757631Seric name->nodename[0] = '\0'; 121857631Seric 121957661Seric /* try /etc/whoami -- one line with the node name */ 122057631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 122157631Seric { 122257661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 122357631Seric (void) fclose(file); 122457661Seric n = strchr(name->nodename, '\n'); 122557631Seric if (n != NULL) 122657631Seric *n = '\0'; 122757631Seric if (name->nodename[0] != '\0') 122857631Seric return (0); 122957631Seric } 123057631Seric 123157661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 123257631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 123357631Seric { 123457631Seric char buf[MAXLINE]; 123557631Seric 123657631Seric while (fgets(buf, MAXLINE, file) != NULL) 123757631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 123857631Seric NODE_LENGTH, name->nodename) > 0) 123957631Seric break; 124057631Seric (void) fclose(file); 124157631Seric if (name->nodename[0] != '\0') 124257631Seric return (0); 124357631Seric } 124457631Seric 124557631Seric #ifdef TRUST_POPEN 124657631Seric /* 124757631Seric ** Popen is known to have security holes. 124857631Seric */ 124957631Seric 125057661Seric /* try uuname -l to return local name */ 125157631Seric if ((file = popen("uuname -l", "r")) != NULL) 125257631Seric { 125357661Seric (void) fgets(name, NODE_LENGTH + 1, file); 125457631Seric (void) pclose(file); 125557661Seric n = strchr(name, '\n'); 125657631Seric if (n != NULL) 125757631Seric *n = '\0'; 125857661Seric if (name->nodename[0] != '\0') 125957631Seric return (0); 126057631Seric } 126157631Seric #endif 126257631Seric 126357631Seric return (-1); 126457631Seric } 126557943Seric #endif /* HASUNAME */ 126658068Seric /* 126758068Seric ** INITGROUPS -- initialize groups 126858068Seric ** 126958068Seric ** Stub implementation for System V style systems 127058068Seric */ 127158068Seric 127258068Seric #ifndef HASINITGROUPS 127358068Seric 127458068Seric initgroups(name, basegid) 127558068Seric char *name; 127658068Seric int basegid; 127758068Seric { 127858068Seric return 0; 127958068Seric } 128058068Seric 128158068Seric #endif 128258082Seric /* 128359289Seric ** SETSID -- set session id (for non-POSIX systems) 128459289Seric */ 128559289Seric 128659289Seric #ifndef HASSETSID 128759289Seric 128863753Seric pid_t 128963753Seric setsid __P ((void)) 129059289Seric { 129164155Seric #ifdef TIOCNOTTY 129264155Seric int fd; 129364155Seric 129466743Seric fd = open("/dev/tty", O_RDWR, 0); 129564155Seric if (fd >= 0) 129664155Seric { 129764155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 129864155Seric (void) close(fd); 129964155Seric } 130064155Seric #endif /* TIOCNOTTY */ 130164705Seric # ifdef SYS5SETPGRP 130263753Seric return setpgrp(); 130363753Seric # else 130464155Seric return setpgid(0, getpid()); 130559289Seric # endif 130659289Seric } 130759289Seric 130859289Seric #endif 130959289Seric /* 131066748Seric ** FSYNC -- dummy fsync 131166748Seric */ 131266748Seric 131366748Seric #ifdef NEEDFSYNC 131466748Seric 131566748Seric fsync(fd) 131666748Seric int fd; 131766748Seric { 131866748Seric # ifdef O_SYNC 131966748Seric return fcntl(fd, F_SETFL, O_SYNC); 132066748Seric # else 132166748Seric /* nothing we can do */ 132266748Seric return 0; 132366748Seric # endif 132466748Seric } 132566748Seric 132666748Seric #endif 132766748Seric /* 132864718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 132964718Seric ** 133064718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 133167427Seric ** instead of a long. This patches things. Only needed on versions 133267427Seric ** prior to 5.4.3. 133364718Seric */ 133464718Seric 133567427Seric #ifdef DGUX_5_4_2 133664718Seric 133764718Seric #undef inet_addr 133864718Seric 133964718Seric long 134064718Seric dgux_inet_addr(host) 134164718Seric char *host; 134264718Seric { 134364718Seric struct in_addr haddr; 134464718Seric 134564718Seric haddr = inet_addr(host); 134664718Seric return haddr.s_addr; 134764718Seric } 134864718Seric 134964718Seric #endif 135064718Seric /* 135163969Seric ** GETOPT -- for old systems or systems with bogus implementations 135263969Seric */ 135363969Seric 135463969Seric #ifdef NEEDGETOPT 135563969Seric 135663969Seric /* 135763969Seric * Copyright (c) 1985 Regents of the University of California. 135863969Seric * All rights reserved. The Berkeley software License Agreement 135963969Seric * specifies the terms and conditions for redistribution. 136063969Seric */ 136163969Seric 136263969Seric 136363969Seric /* 136463969Seric ** this version hacked to add `atend' flag to allow state machine 136563969Seric ** to reset if invoked by the program to scan args for a 2nd time 136663969Seric */ 136763969Seric 136863969Seric #if defined(LIBC_SCCS) && !defined(lint) 136963969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 137064072Seric #endif /* LIBC_SCCS and not lint */ 137163969Seric 137263969Seric #include <stdio.h> 137363969Seric 137463969Seric /* 137563969Seric * get option letter from argument vector 137663969Seric */ 137765352Seric #ifdef _CONVEX_SOURCE 137865352Seric extern int optind, opterr; 137965352Seric #else 138065352Seric int opterr = 1; /* if error message should be printed */ 138165352Seric int optind = 1; /* index into parent argv vector */ 138265352Seric #endif 138365352Seric int optopt; /* character checked for validity */ 138463969Seric char *optarg; /* argument associated with option */ 138563969Seric 138663969Seric #define BADCH (int)'?' 138763969Seric #define EMSG "" 138863969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 138963969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 139063969Seric 139163969Seric getopt(nargc,nargv,ostr) 139265160Seric int nargc; 139365160Seric char *const *nargv; 139465160Seric const char *ostr; 139563969Seric { 139663969Seric static char *place = EMSG; /* option letter processing */ 139763969Seric static char atend = 0; 139863969Seric register char *oli; /* option letter list index */ 139963969Seric 140063969Seric if (atend) { 140163969Seric atend = 0; 140263969Seric place = EMSG; 140363969Seric } 140463969Seric if(!*place) { /* update scanning pointer */ 140564136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 140663969Seric atend++; 140763969Seric return(EOF); 140863969Seric } 140963969Seric if (*place == '-') { /* found "--" */ 141063969Seric ++optind; 141163969Seric atend++; 141263969Seric return(EOF); 141363969Seric } 141463969Seric } /* option letter okay? */ 141564125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 141664136Seric if (!*place) ++optind; 141763969Seric tell(": illegal option -- "); 141863969Seric } 141963969Seric if (*++oli != ':') { /* don't need argument */ 142063969Seric optarg = NULL; 142163969Seric if (!*place) ++optind; 142263969Seric } 142363969Seric else { /* need an argument */ 142463969Seric if (*place) optarg = place; /* no white space */ 142563969Seric else if (nargc <= ++optind) { /* no arg */ 142663969Seric place = EMSG; 142763969Seric tell(": option requires an argument -- "); 142863969Seric } 142963969Seric else optarg = nargv[optind]; /* white space */ 143063969Seric place = EMSG; 143163969Seric ++optind; 143263969Seric } 143363969Seric return(optopt); /* dump back option letter */ 143463969Seric } 143563969Seric 143663969Seric #endif 143763969Seric /* 143863969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 143963969Seric */ 144063969Seric 144163969Seric #ifdef NEEDVPRINTF 144263969Seric 144363969Seric #define MAXARG 16 144463969Seric 144563969Seric vfprintf(fp, fmt, ap) 144663969Seric FILE * fp; 144763969Seric char * fmt; 144863969Seric char ** ap; 144963969Seric { 145063969Seric char * bp[MAXARG]; 145163969Seric int i = 0; 145263969Seric 145363969Seric while (*ap && i < MAXARG) 145463969Seric bp[i++] = *ap++; 145563969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 145663969Seric bp[4], bp[5], bp[6], bp[7], 145763969Seric bp[8], bp[9], bp[10], bp[11], 145863969Seric bp[12], bp[13], bp[14], bp[15]); 145963969Seric } 146063969Seric 146163969Seric vsprintf(s, fmt, ap) 146263969Seric char * s; 146363969Seric char * fmt; 146463969Seric char ** ap; 146563969Seric { 146663969Seric char * bp[MAXARG]; 146763969Seric int i = 0; 146863969Seric 146963969Seric while (*ap && i < MAXARG) 147063969Seric bp[i++] = *ap++; 147163969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 147263969Seric bp[4], bp[5], bp[6], bp[7], 147363969Seric bp[8], bp[9], bp[10], bp[11], 147463969Seric bp[12], bp[13], bp[14], bp[15]); 147563969Seric } 147663969Seric 147763969Seric #endif 147863969Seric /* 147965220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 148065208Seric ** 148165211Seric ** Parameters: 148265211Seric ** shell -- the user's shell from /etc/passwd 148365211Seric ** 148465211Seric ** Returns: 148565211Seric ** TRUE -- if it is ok to use this for unrestricted access. 148665211Seric ** FALSE -- if the shell is restricted. 148765208Seric */ 148865208Seric 148965587Seric #if !HASGETUSERSHELL 149065587Seric 149165587Seric # ifndef _PATH_SHELLS 149265587Seric # define _PATH_SHELLS "/etc/shells" 149365587Seric # endif 149465587Seric 149565587Seric char *DefaultUserShells[] = 149665587Seric { 149765587Seric "/bin/sh", 149865587Seric "/usr/bin/sh", 149965587Seric "/bin/csh", 150065587Seric "/usr/bin/csh", 150165587Seric #ifdef __hpux 150265587Seric "/bin/rsh", 150365587Seric "/bin/ksh", 150465587Seric "/bin/rksh", 150565587Seric "/bin/pam", 150665587Seric "/usr/bin/keysh", 150765587Seric "/bin/posix/sh", 150865211Seric #endif 150965587Seric NULL 151065587Seric }; 151165587Seric 151265208Seric #endif 151365208Seric 151465747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 151565747Seric 151665211Seric bool 151765211Seric usershellok(shell) 151865211Seric char *shell; 151965208Seric { 152065211Seric #if HASGETUSERSHELL 152165211Seric register char *p; 152265211Seric extern char *getusershell(); 152365208Seric 152465211Seric setusershell(); 152565211Seric while ((p = getusershell()) != NULL) 152665747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 152765211Seric break; 152865211Seric endusershell(); 152965211Seric return p != NULL; 153065211Seric #else 153165211Seric register FILE *shellf; 153265211Seric char buf[MAXLINE]; 153365208Seric 153465211Seric shellf = fopen(_PATH_SHELLS, "r"); 153565211Seric if (shellf == NULL) 153665211Seric { 153765211Seric /* no /etc/shells; see if it is one of the std shells */ 153865587Seric char **d; 153965587Seric 154065587Seric for (d = DefaultUserShells; *d != NULL; d++) 154165587Seric { 154265587Seric if (strcmp(shell, *d) == 0) 154365587Seric return TRUE; 154465587Seric } 154565587Seric return FALSE; 154665211Seric } 154765208Seric 154865211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 154965211Seric { 155065211Seric register char *p, *q; 155165208Seric 155265211Seric p = buf; 155365211Seric while (*p != '\0' && *p != '#' && *p != '/') 155465211Seric p++; 155565211Seric if (*p == '#' || *p == '\0') 155665208Seric continue; 155765211Seric q = p; 155865211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 155965211Seric p++; 156065211Seric *p = '\0'; 156165747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 156265211Seric { 156365211Seric fclose(shellf); 156465211Seric return TRUE; 156565211Seric } 156665208Seric } 156765211Seric fclose(shellf); 156865211Seric return FALSE; 156965211Seric #endif 157065208Seric } 157165208Seric /* 157264311Seric ** FREESPACE -- see how much free space is on the queue filesystem 157358082Seric ** 157458082Seric ** Only implemented if you have statfs. 157558082Seric ** 157658082Seric ** Parameters: 157764311Seric ** dir -- the directory in question. 157864311Seric ** bsize -- a variable into which the filesystem 157964311Seric ** block size is stored. 158058082Seric ** 158158082Seric ** Returns: 158264311Seric ** The number of bytes free on the queue filesystem. 158364311Seric ** -1 if the statfs call fails. 158464311Seric ** 158564311Seric ** Side effects: 158664311Seric ** Puts the filesystem block size into bsize. 158758082Seric */ 158858082Seric 158965749Seric /* statfs types */ 159065749Seric #define SFS_NONE 0 /* no statfs implementation */ 159165749Seric #define SFS_USTAT 1 /* use ustat */ 159265749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 159365749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 159465749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 159565749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 159666752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 159765749Seric 159865749Seric #ifndef SFS_TYPE 159965749Seric # define SFS_TYPE SFS_NONE 160058157Seric #endif 160158157Seric 160265749Seric #if SFS_TYPE == SFS_USTAT 160358157Seric # include <ustat.h> 160458157Seric #endif 160565749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 160665749Seric # include <sys/statfs.h> 160758082Seric #endif 160865749Seric #if SFS_TYPE == SFS_VFS 160965749Seric # include <sys/vfs.h> 161065749Seric #endif 161165749Seric #if SFS_TYPE == SFS_MOUNT 161265749Seric # include <sys/mount.h> 161365749Seric #endif 161466752Seric #if SFS_TYPE == SFS_STATVFS 161566752Seric # include <sys/statvfs.h> 161666752Seric #endif 161758082Seric 161864311Seric long 161964311Seric freespace(dir, bsize) 162064311Seric char *dir; 162164311Seric long *bsize; 162258082Seric { 162365749Seric #if SFS_TYPE != SFS_NONE 162465749Seric # if SFS_TYPE == SFS_USTAT 162558153Seric struct ustat fs; 162658153Seric struct stat statbuf; 162758366Seric # define FSBLOCKSIZE DEV_BSIZE 162858157Seric # define f_bavail f_tfree 162958157Seric # else 163058157Seric # if defined(ultrix) 163158157Seric struct fs_data fs; 163258157Seric # define f_bavail fd_bfreen 163358366Seric # define FSBLOCKSIZE fs.fd_bsize 163458153Seric # else 163566752Seric # if SFS_TYPE == SFS_STATVFS 163666752Seric struct statvfs fs; 163766893Seric # define FSBLOCKSIZE fs.f_bsize 163866752Seric # else 163958082Seric struct statfs fs; 164066752Seric # define FSBLOCKSIZE fs.f_bsize 164166752Seric # if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) 164266752Seric # define f_bavail f_bfree 164366752Seric # endif 164463987Seric # endif 164558153Seric # endif 164658133Seric # endif 164758082Seric extern int errno; 164858082Seric 164965749Seric # if SFS_TYPE == SFS_USTAT 165064311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 165158157Seric # else 165265749Seric # if SFS_TYPE == SFS_4ARGS 165364311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 165458157Seric # else 165567159Seric # if SFS_TYPE == SFS_STATVFS 165667159Seric if (statvfs(dir, &fs) == 0) 165767159Seric # else 165867159Seric # if defined(ultrix) 165964311Seric if (statfs(dir, &fs) > 0) 166067159Seric # else 166164311Seric if (statfs(dir, &fs) == 0) 166267159Seric # endif 166358153Seric # endif 166458133Seric # endif 166558133Seric # endif 166658133Seric { 166764311Seric if (bsize != NULL) 166864311Seric *bsize = FSBLOCKSIZE; 166964311Seric return (fs.f_bavail); 167064311Seric } 167164337Seric #endif 167264311Seric return (-1); 167364311Seric } 167464311Seric /* 167564311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 167664311Seric ** 167764311Seric ** Only implemented if you have statfs. 167864311Seric ** 167964311Seric ** Parameters: 168064311Seric ** msize -- the size to check against. If zero, we don't yet 168164311Seric ** know how big the message will be, so just check for 168264311Seric ** a "reasonable" amount. 168364311Seric ** 168464311Seric ** Returns: 168564311Seric ** TRUE if there is enough space. 168664311Seric ** FALSE otherwise. 168764311Seric */ 168864311Seric 168964311Seric bool 169064311Seric enoughspace(msize) 169164311Seric long msize; 169264311Seric { 169364311Seric long bfree, bsize; 169464311Seric 169564311Seric if (MinBlocksFree <= 0 && msize <= 0) 169664311Seric { 169758133Seric if (tTd(4, 80)) 169864311Seric printf("enoughspace: no threshold\n"); 169964311Seric return TRUE; 170064311Seric } 170164311Seric 170264311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 170364311Seric { 170464311Seric if (tTd(4, 80)) 170558333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 170664311Seric bfree, msize); 170758333Seric 170858333Seric /* convert msize to block count */ 170964311Seric msize = msize / bsize + 1; 171058333Seric if (MinBlocksFree >= 0) 171158333Seric msize += MinBlocksFree; 171258333Seric 171364311Seric if (bfree < msize) 171458090Seric { 171558090Seric #ifdef LOG 171658090Seric if (LogLevel > 0) 171764254Seric syslog(LOG_ALERT, 171864254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 171964311Seric CurEnv->e_id, bfree, 172064254Seric CurHostName, msize, QueueDir); 172158090Seric #endif 172258082Seric return FALSE; 172358090Seric } 172458082Seric } 172558082Seric else if (tTd(4, 80)) 172658333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 172758333Seric MinBlocksFree, msize, errstring(errno)); 172858082Seric return TRUE; 172958082Seric } 173058542Seric /* 173158542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 173258542Seric ** 173358542Seric ** This looks at an errno value and tells if this is likely to 173458542Seric ** go away if retried later. 173558542Seric ** 173658542Seric ** Parameters: 173758542Seric ** err -- the errno code to classify. 173858542Seric ** 173958542Seric ** Returns: 174058542Seric ** TRUE if this is probably transient. 174158542Seric ** FALSE otherwise. 174258542Seric */ 174358542Seric 174458542Seric bool 174558542Seric transienterror(err) 174658542Seric int err; 174758542Seric { 174858542Seric switch (err) 174958542Seric { 175058542Seric case EIO: /* I/O error */ 175158542Seric case ENXIO: /* Device not configured */ 175258542Seric case EAGAIN: /* Resource temporarily unavailable */ 175358542Seric case ENOMEM: /* Cannot allocate memory */ 175458542Seric case ENODEV: /* Operation not supported by device */ 175558542Seric case ENFILE: /* Too many open files in system */ 175658542Seric case EMFILE: /* Too many open files */ 175758542Seric case ENOSPC: /* No space left on device */ 175858542Seric #ifdef ETIMEDOUT 175958542Seric case ETIMEDOUT: /* Connection timed out */ 176058542Seric #endif 176158542Seric #ifdef ESTALE 176258542Seric case ESTALE: /* Stale NFS file handle */ 176358542Seric #endif 176458542Seric #ifdef ENETDOWN 176558542Seric case ENETDOWN: /* Network is down */ 176658542Seric #endif 176758542Seric #ifdef ENETUNREACH 176858542Seric case ENETUNREACH: /* Network is unreachable */ 176958542Seric #endif 177058542Seric #ifdef ENETRESET 177158542Seric case ENETRESET: /* Network dropped connection on reset */ 177258542Seric #endif 177358542Seric #ifdef ECONNABORTED 177458542Seric case ECONNABORTED: /* Software caused connection abort */ 177558542Seric #endif 177658542Seric #ifdef ECONNRESET 177758542Seric case ECONNRESET: /* Connection reset by peer */ 177858542Seric #endif 177958542Seric #ifdef ENOBUFS 178058542Seric case ENOBUFS: /* No buffer space available */ 178158542Seric #endif 178258542Seric #ifdef ESHUTDOWN 178358542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 178458542Seric #endif 178558542Seric #ifdef ECONNREFUSED 178658542Seric case ECONNREFUSED: /* Connection refused */ 178758542Seric #endif 178858542Seric #ifdef EHOSTDOWN 178958542Seric case EHOSTDOWN: /* Host is down */ 179058542Seric #endif 179158542Seric #ifdef EHOSTUNREACH 179258542Seric case EHOSTUNREACH: /* No route to host */ 179358542Seric #endif 179458542Seric #ifdef EDQUOT 179558542Seric case EDQUOT: /* Disc quota exceeded */ 179658542Seric #endif 179758542Seric #ifdef EPROCLIM 179858542Seric case EPROCLIM: /* Too many processes */ 179958542Seric #endif 180058542Seric #ifdef EUSERS 180158542Seric case EUSERS: /* Too many users */ 180258542Seric #endif 180358542Seric #ifdef EDEADLK 180458542Seric case EDEADLK: /* Resource deadlock avoided */ 180558542Seric #endif 180658542Seric #ifdef EISCONN 180758542Seric case EISCONN: /* Socket already connected */ 180858542Seric #endif 180958542Seric #ifdef EINPROGRESS 181058542Seric case EINPROGRESS: /* Operation now in progress */ 181158542Seric #endif 181258542Seric #ifdef EALREADY 181358542Seric case EALREADY: /* Operation already in progress */ 181458542Seric #endif 181558542Seric #ifdef EADDRINUSE 181658542Seric case EADDRINUSE: /* Address already in use */ 181758542Seric #endif 181858542Seric #ifdef EADDRNOTAVAIL 181958542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 182058542Seric #endif 182166018Seric #ifdef ETXTBSY 182266017Seric case ETXTBSY: /* (Apollo) file locked */ 182366017Seric #endif 182463834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 182558542Seric case ENOSR: /* Out of streams resources */ 182658542Seric #endif 182758542Seric return TRUE; 182858542Seric } 182958542Seric 183058542Seric /* nope, must be permanent */ 183158542Seric return FALSE; 183258542Seric } 183358689Seric /* 183464035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 183558689Seric ** 183658689Seric ** Parameters: 183758689Seric ** fd -- the file descriptor of the file. 183858689Seric ** filename -- the file name (for error messages). 183964335Seric ** ext -- the filename extension. 184058689Seric ** type -- type of the lock. Bits can be: 184158689Seric ** LOCK_EX -- exclusive lock. 184258689Seric ** LOCK_NB -- non-blocking. 184358689Seric ** 184458689Seric ** Returns: 184558689Seric ** TRUE if the lock was acquired. 184658689Seric ** FALSE otherwise. 184758689Seric */ 184858689Seric 184958689Seric bool 185064335Seric lockfile(fd, filename, ext, type) 185158689Seric int fd; 185258689Seric char *filename; 185364335Seric char *ext; 185458689Seric int type; 185558689Seric { 185665830Seric # if !HASFLOCK 185758689Seric int action; 185858689Seric struct flock lfd; 185964335Seric 186064335Seric if (ext == NULL) 186164335Seric ext = ""; 186264035Seric 186363983Seric bzero(&lfd, sizeof lfd); 186459447Seric if (bitset(LOCK_UN, type)) 186559447Seric lfd.l_type = F_UNLCK; 186659447Seric else if (bitset(LOCK_EX, type)) 186758689Seric lfd.l_type = F_WRLCK; 186858689Seric else 186958689Seric lfd.l_type = F_RDLCK; 187058689Seric 187158689Seric if (bitset(LOCK_NB, type)) 187258689Seric action = F_SETLK; 187358689Seric else 187458689Seric action = F_SETLKW; 187558689Seric 187664264Seric if (tTd(55, 60)) 187764335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 187864335Seric filename, ext, action, lfd.l_type); 187964264Seric 188058689Seric if (fcntl(fd, action, &lfd) >= 0) 188164264Seric { 188264264Seric if (tTd(55, 60)) 188364264Seric printf("SUCCESS\n"); 188458689Seric return TRUE; 188564264Seric } 188658689Seric 188764264Seric if (tTd(55, 60)) 188864264Seric printf("(%s) ", errstring(errno)); 188964264Seric 189064136Seric /* 189164136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 189264136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 189364136Seric ** as type "tmp" (that is, served from swap space), the 189464136Seric ** previous fcntl will fail with "Invalid argument" errors. 189564136Seric ** Since this is fairly common during testing, we will assume 189664136Seric ** that this indicates that the lock is successfully grabbed. 189764136Seric */ 189864136Seric 189964136Seric if (errno == EINVAL) 190064264Seric { 190164264Seric if (tTd(55, 60)) 190264264Seric printf("SUCCESS\n"); 190364136Seric return TRUE; 190464264Seric } 190564136Seric 190658689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 190764378Seric { 190864378Seric int omode = -1; 190964378Seric # ifdef F_GETFL 191064378Seric int oerrno = errno; 191164378Seric 191264378Seric (void) fcntl(fd, F_GETFL, &omode); 191364378Seric errno = oerrno; 191464378Seric # endif 191564378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 191664378Seric filename, ext, fd, type, omode, geteuid()); 191764378Seric } 191858689Seric # else 191964335Seric if (ext == NULL) 192064335Seric ext = ""; 192164335Seric 192264264Seric if (tTd(55, 60)) 192364335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 192464264Seric 192558689Seric if (flock(fd, type) >= 0) 192664264Seric { 192764264Seric if (tTd(55, 60)) 192864264Seric printf("SUCCESS\n"); 192958689Seric return TRUE; 193064264Seric } 193158689Seric 193264264Seric if (tTd(55, 60)) 193364264Seric printf("(%s) ", errstring(errno)); 193464264Seric 193558689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 193664378Seric { 193764378Seric int omode = -1; 193864378Seric # ifdef F_GETFL 193964378Seric int oerrno = errno; 194064378Seric 194164378Seric (void) fcntl(fd, F_GETFL, &omode); 194264378Seric errno = oerrno; 194364378Seric # endif 194464378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 194564378Seric filename, ext, fd, type, omode, geteuid()); 194664386Seric } 194758689Seric # endif 194864264Seric if (tTd(55, 60)) 194964264Seric printf("FAIL\n"); 195058689Seric return FALSE; 195158689Seric } 195264556Seric /* 195365948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 195465948Seric ** 195565948Seric ** Parameters: 195665948Seric ** fd -- the file descriptor to check. 195765948Seric ** 195865948Seric ** Returns: 195965948Seric ** TRUE -- if only root can chown the file to an arbitrary 196065948Seric ** user. 196165948Seric ** FALSE -- if an arbitrary user can give away a file. 196265948Seric */ 196365948Seric 196465948Seric bool 196565948Seric chownsafe(fd) 196665948Seric int fd; 196765948Seric { 196865950Seric #ifdef __hpux 196965948Seric char *s; 197065948Seric int tfd; 197165948Seric uid_t o_uid, o_euid; 197265948Seric gid_t o_gid, o_egid; 197365948Seric bool rval; 197465948Seric struct stat stbuf; 197565948Seric 197665948Seric o_uid = getuid(); 197765948Seric o_euid = geteuid(); 197865948Seric o_gid = getgid(); 197965948Seric o_egid = getegid(); 198065959Seric fstat(fd, &stbuf); 198165948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 198265948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 198365948Seric s = tmpnam(NULL); 198465948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 198565974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 198665948Seric close(tfd); 198765948Seric unlink(s); 198865948Seric setreuid(o_uid, o_euid); 198965948Seric setresgid(o_gid, o_egid, -1); 199065948Seric return rval; 199165948Seric #else 199266088Seric # ifdef _POSIX_CHOWN_RESTRICTED 199366088Seric # if _POSIX_CHOWN_RESTRICTED == -1 199466088Seric return FALSE; 199566088Seric # else 199666088Seric return TRUE; 199766088Seric # endif 199866088Seric # else 199966088Seric # ifdef _PC_CHOWN_RESTRICTED 2000*67447Seric int rval; 2001*67447Seric 2002*67447Seric /* 2003*67447Seric ** Some systems (e.g., SunOS) seem to have the call and the 2004*67447Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 2005*67447Seric ** the call. This heuristic checks for that. 2006*67447Seric */ 2007*67447Seric 2008*67447Seric errno = 0; 2009*67447Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 2010*67447Seric if (errno == 0) 2011*67447Seric return rval > 0; 2012*67447Seric # endif 2013*67447Seric # ifdef BSD 2014*67447Seric return TRUE; 201566088Seric # else 201665948Seric return FALSE; 201765948Seric # endif 201865948Seric # endif 201965948Seric #endif 202065948Seric } 202165948Seric /* 202267430Seric ** RESETLIMITS -- reset system controlled resource limits 202367430Seric ** 202467430Seric ** This is to avoid denial-of-service attacks 202567430Seric ** 202667430Seric ** Parameters: 202767430Seric ** none 202867430Seric ** 202967430Seric ** Returns: 203067430Seric ** none 203167430Seric */ 203267430Seric 203367430Seric #if HASSETRLIMIT 203467430Seric # include <sys/resource.h> 203567430Seric #endif 203667430Seric 203767430Seric resetlimits() 203867430Seric { 203967430Seric #if HASSETRLIMIT 204067430Seric struct rlimit lim; 204167430Seric 204267430Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 204367430Seric (void) setrlimit(RLIMIT_CPU, &lim); 204467430Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 204567430Seric #else 204667430Seric # if HASULIMIT 204767430Seric (void) ulimit(2, 0x3fffff); 204867430Seric # endif 204967430Seric #endif 205067430Seric } 205167430Seric /* 205264556Seric ** GETCFNAME -- return the name of the .cf file. 205364556Seric ** 205464556Seric ** Some systems (e.g., NeXT) determine this dynamically. 205564556Seric */ 205664556Seric 205764556Seric char * 205864556Seric getcfname() 205964556Seric { 206064556Seric if (ConfFile != NULL) 206164556Seric return ConfFile; 206265214Seric #ifdef NETINFO 206365214Seric { 206465214Seric extern char *ni_propval(); 206565214Seric char *cflocation; 206665214Seric 206765214Seric cflocation = ni_propval("/locations/sendmail", "sendmail.cf"); 206865214Seric if (cflocation != NULL) 206965214Seric return cflocation; 207065214Seric } 207165214Seric #endif 207264556Seric return _PATH_SENDMAILCF; 207364556Seric } 207464718Seric /* 207564718Seric ** SETVENDOR -- process vendor code from V configuration line 207664718Seric ** 207764718Seric ** Parameters: 207864718Seric ** vendor -- string representation of vendor. 207964718Seric ** 208064718Seric ** Returns: 208164718Seric ** TRUE -- if ok. 208264718Seric ** FALSE -- if vendor code could not be processed. 208364926Seric ** 208464926Seric ** Side Effects: 208564926Seric ** It is reasonable to set mode flags here to tweak 208664926Seric ** processing in other parts of the code if necessary. 208764926Seric ** For example, if you are a vendor that uses $%y to 208864926Seric ** indicate YP lookups, you could enable that here. 208964718Seric */ 209064718Seric 209164718Seric bool 209264718Seric setvendor(vendor) 209364718Seric char *vendor; 209464718Seric { 209564926Seric if (strcasecmp(vendor, "Berkeley") == 0) 209664926Seric return TRUE; 209764926Seric 209864926Seric /* add vendor extensions here */ 209964926Seric 210064926Seric return FALSE; 210164718Seric } 210264816Seric /* 210364816Seric ** STRTOL -- convert string to long integer 210464816Seric ** 210564816Seric ** For systems that don't have it in the C library. 210666161Seric ** 210766161Seric ** This is taken verbatim from the 4.4-Lite C library. 210864816Seric */ 210964816Seric 211064816Seric #ifdef NEEDSTRTOL 211164816Seric 211266161Seric #if defined(LIBC_SCCS) && !defined(lint) 211366161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 211466161Seric #endif /* LIBC_SCCS and not lint */ 211566161Seric 211666161Seric #include <limits.h> 211766161Seric 211866161Seric /* 211966161Seric * Convert a string to a long integer. 212066161Seric * 212166161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 212266161Seric * alphabets and digits are each contiguous. 212366161Seric */ 212466161Seric 212564816Seric long 212666161Seric strtol(nptr, endptr, base) 212766161Seric const char *nptr; 212866161Seric char **endptr; 212966161Seric register int base; 213064816Seric { 213166161Seric register const char *s = nptr; 213266161Seric register unsigned long acc; 213366161Seric register int c; 213466161Seric register unsigned long cutoff; 213566161Seric register int neg = 0, any, cutlim; 213664816Seric 213766161Seric /* 213866161Seric * Skip white space and pick up leading +/- sign if any. 213966161Seric * If base is 0, allow 0x for hex and 0 for octal, else 214066161Seric * assume decimal; if base is already 16, allow 0x. 214166161Seric */ 214266161Seric do { 214366161Seric c = *s++; 214466161Seric } while (isspace(c)); 214566161Seric if (c == '-') { 214666161Seric neg = 1; 214766161Seric c = *s++; 214866161Seric } else if (c == '+') 214966161Seric c = *s++; 215066161Seric if ((base == 0 || base == 16) && 215166161Seric c == '0' && (*s == 'x' || *s == 'X')) { 215266161Seric c = s[1]; 215366161Seric s += 2; 215466161Seric base = 16; 215566161Seric } 215666161Seric if (base == 0) 215766161Seric base = c == '0' ? 8 : 10; 215864816Seric 215966161Seric /* 216066161Seric * Compute the cutoff value between legal numbers and illegal 216166161Seric * numbers. That is the largest legal value, divided by the 216266161Seric * base. An input number that is greater than this value, if 216366161Seric * followed by a legal input character, is too big. One that 216466161Seric * is equal to this value may be valid or not; the limit 216566161Seric * between valid and invalid numbers is then based on the last 216666161Seric * digit. For instance, if the range for longs is 216766161Seric * [-2147483648..2147483647] and the input base is 10, 216866161Seric * cutoff will be set to 214748364 and cutlim to either 216966161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 217066161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 217166161Seric * the number is too big, and we will return a range error. 217266161Seric * 217366161Seric * Set any if any `digits' consumed; make it negative to indicate 217466161Seric * overflow. 217566161Seric */ 217666161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 217766161Seric cutlim = cutoff % (unsigned long)base; 217866161Seric cutoff /= (unsigned long)base; 217966161Seric for (acc = 0, any = 0;; c = *s++) { 218066161Seric if (isdigit(c)) 218166161Seric c -= '0'; 218266161Seric else if (isalpha(c)) 218366161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 218466161Seric else 218566161Seric break; 218666161Seric if (c >= base) 218766161Seric break; 218866161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 218966161Seric any = -1; 219066161Seric else { 219166161Seric any = 1; 219266161Seric acc *= base; 219366161Seric acc += c; 219464816Seric } 219564816Seric } 219666161Seric if (any < 0) { 219766161Seric acc = neg ? LONG_MIN : LONG_MAX; 219866161Seric errno = ERANGE; 219966161Seric } else if (neg) 220066161Seric acc = -acc; 220166161Seric if (endptr != 0) 220266161Seric *endptr = (char *)(any ? s - 1 : nptr); 220366161Seric return (acc); 220464816Seric } 220564816Seric 220664816Seric #endif 220764841Seric /* 220864841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 220964841Seric ** 221064841Seric ** Solaris versions prior through 2.3 don't properly deliver a 221164841Seric ** canonical h_name field. This tries to work around it. 221264841Seric */ 221364841Seric 221464841Seric #ifdef SOLARIS 221564841Seric 221664841Seric struct hostent * 221764841Seric solaris_gethostbyname(name) 221865094Seric const char *name; 221964841Seric { 222064841Seric # ifdef SOLARIS_2_3 222164841Seric static struct hostent hp; 222264841Seric static char buf[1000]; 222364841Seric extern struct hostent *_switch_gethostbyname_r(); 222464841Seric 222564841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 222664841Seric # else 222765009Seric extern struct hostent *__switch_gethostbyname(); 222865009Seric 222964841Seric return __switch_gethostbyname(name); 223064841Seric # endif 223164841Seric } 223264841Seric 223364841Seric struct hostent * 223464841Seric solaris_gethostbyaddr(addr, len, type) 223565094Seric const char *addr; 223664841Seric int len; 223764841Seric int type; 223864841Seric { 223964841Seric # ifdef SOLARIS_2_3 224064841Seric static struct hostent hp; 224164841Seric static char buf[1000]; 224264841Seric extern struct hostent *_switch_gethostbyaddr_r(); 224364841Seric 224464841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 224564841Seric # else 224665009Seric extern struct hostent *__switch_gethostbyaddr(); 224765009Seric 224864841Seric return __switch_gethostbyaddr(addr, len, type); 224964841Seric # endif 225064841Seric } 225164841Seric 225264841Seric #endif 225365214Seric /* 225465214Seric ** NI_PROPVAL -- netinfo property value lookup routine 225565214Seric ** 225665214Seric ** Parameters: 225765214Seric ** directory -- the Netinfo directory name. 225865214Seric ** propname -- the Netinfo property name. 225965214Seric ** 226065214Seric ** Returns: 226165214Seric ** NULL -- if: 226265214Seric ** 1. the directory is not found 226365214Seric ** 2. the property name is not found 226465214Seric ** 3. the property contains multiple values 226565214Seric ** 4. some error occured 226665214Seric ** else -- the location of the config file. 226765214Seric ** 226865214Seric ** Notes: 226965214Seric ** Caller should free the return value of ni_proval 227065214Seric */ 227165214Seric 227265214Seric #ifdef NETINFO 227365214Seric 227465214Seric # include <netinfo/ni.h> 227565214Seric 227665214Seric # define LOCAL_NETINFO_DOMAIN "." 227765214Seric # define PARENT_NETINFO_DOMAIN ".." 227865214Seric # define MAX_NI_LEVELS 256 227965214Seric 228065214Seric char * 228165214Seric ni_propval(directory, propname) 228265214Seric char *directory; 228365214Seric char *propname; 228465214Seric { 228565820Seric char *propval = NULL; 228665214Seric int i; 228765214Seric void *ni = NULL; 228865214Seric void *lastni = NULL; 228965214Seric ni_status nis; 229065214Seric ni_id nid; 229165214Seric ni_namelist ninl; 229265214Seric 229365214Seric /* 229465214Seric ** If the passed directory and property name are found 229565214Seric ** in one of netinfo domains we need to search (starting 229665214Seric ** from the local domain moving all the way back to the 229765214Seric ** root domain) set propval to the property's value 229865214Seric ** and return it. 229965214Seric */ 230065214Seric 230165214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 230265214Seric { 230365214Seric if (i == 0) 230465214Seric { 230565214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 230665214Seric } 230765214Seric else 230865214Seric { 230965214Seric if (lastni != NULL) 231065214Seric ni_free(lastni); 231165214Seric lastni = ni; 231265214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 231365214Seric } 231465214Seric 231565214Seric /* 231665214Seric ** Don't bother if we didn't get a handle on a 231765214Seric ** proper domain. This is not necessarily an error. 231865214Seric ** We would get a positive ni_status if, for instance 231965214Seric ** we never found the directory or property and tried 232065214Seric ** to open the parent of the root domain! 232165214Seric */ 232265214Seric 232365214Seric if (nis != 0) 232465214Seric break; 232565214Seric 232665214Seric /* 232765214Seric ** Find the path to the server information. 232865214Seric */ 232965214Seric 233065214Seric if (ni_pathsearch(ni, &nid, directory) != 0) 233165214Seric continue; 233265214Seric 233365214Seric /* 233465214Seric ** Find "host" information. 233565214Seric */ 233665214Seric 233765214Seric if (ni_lookupprop(ni, &nid, propname, &ninl) != 0) 233865214Seric continue; 233965214Seric 234065214Seric /* 234165214Seric ** If there's only one name in 234265214Seric ** the list, assume we've got 234365214Seric ** what we want. 234465214Seric */ 234565214Seric 234665214Seric if (ninl.ni_namelist_len == 1) 234765214Seric { 234865214Seric propval = ni_name_dup(ninl.ni_namelist_val[0]); 234965214Seric break; 235065214Seric } 235165214Seric } 235265214Seric 235365214Seric /* 235465214Seric ** Clean up. 235565214Seric */ 235665214Seric 235765214Seric if (ni != NULL) 235865214Seric ni_free(ni); 235965214Seric if (lastni != NULL && ni != lastni) 236065214Seric ni_free(lastni); 236165214Seric 236265214Seric return propval; 236365214Seric } 236465214Seric 236565214Seric #endif /* NETINFO */ 236665504Seric /* 236765504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 236865504Seric ** 236965504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 237065504Seric ** syslog succeeds during interrupt handlers. 237165504Seric */ 237265504Seric 237365504Seric #ifdef __hpux 237465504Seric 237565504Seric # define MAXSYSLOGTRIES 100 237665504Seric # undef syslog 237765504Seric 237865504Seric # ifdef __STDC__ 237965504Seric hard_syslog(int pri, char *msg, ...) 238065504Seric # else 238165504Seric hard_syslog(pri, msg, va_alist) 238265504Seric int pri; 238365504Seric char *msg; 238465504Seric va_dcl 238565504Seric # endif 238665504Seric { 238765504Seric int i; 238865504Seric char buf[SYSLOG_BUFSIZE * 2]; 238965504Seric VA_LOCAL_DECL; 239065504Seric 239165504Seric VA_START(msg); 239265504Seric vsprintf(buf, msg, ap); 239365504Seric VA_END; 239465504Seric 239565504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 239665504Seric continue; 239765504Seric } 239865504Seric 239965504Seric #endif 2400