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*67546Seric static char sccsid[] = "@(#)conf.c 8.98 (Berkeley) 07/23/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 14924943Seric /* 15024943Seric ** SETDEFAULTS -- set default values 15124943Seric ** 15224943Seric ** Because of the way freezing is done, these must be initialized 15324943Seric ** using direct code. 15424943Seric ** 15524943Seric ** Parameters: 15658734Seric ** e -- the default envelope. 15724943Seric ** 15824943Seric ** Returns: 15924943Seric ** none. 16024943Seric ** 16124943Seric ** Side Effects: 16224943Seric ** Initializes a bunch of global variables to their 16324943Seric ** default values. 16424943Seric */ 16524943Seric 16658737Seric #define DAYS * 24 * 60 * 60 16758737Seric 16858734Seric setdefaults(e) 16958734Seric register ENVELOPE *e; 17024943Seric { 17157438Seric SpaceSub = ' '; /* option B */ 17257438Seric QueueLA = 8; /* option x */ 17357438Seric RefuseLA = 12; /* option X */ 17457438Seric WkRecipFact = 30000L; /* option y */ 17557438Seric WkClassFact = 1800L; /* option z */ 17657438Seric WkTimeFact = 90000L; /* option Z */ 17757438Seric QueueFactor = WkRecipFact * 20; /* option q */ 17863787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 17957438Seric /* option F */ 18057438Seric DefUid = 1; /* option u */ 18157438Seric DefGid = 1; /* option g */ 18257438Seric CheckpointInterval = 10; /* option C */ 18357438Seric MaxHopCount = 25; /* option h */ 18458734Seric e->e_sendmode = SM_FORK; /* option d */ 18558734Seric e->e_errormode = EM_PRINT; /* option e */ 186*67546Seric SevenBitInput = FALSE; /* option 7 */ 18757438Seric MaxMciCache = 1; /* option k */ 18857438Seric MciCacheTimeout = 300; /* option K */ 18957438Seric LogLevel = 9; /* option L */ 19058112Seric settimeouts(NULL); /* option r */ 19158737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 19258737Seric TimeOuts.to_q_warning = 0; /* option T */ 19358853Seric PrivacyFlags = 0; /* option p */ 194*67546Seric MimeMode = MM_CVTMIME|MM_PASS8BIT; /* option 8 */ 19540973Sbostic setdefuser(); 19653654Seric setupmaps(); 19757402Seric setupmailers(); 19824943Seric } 199294Seric 20040973Sbostic 2014326Seric /* 20240973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20340973Sbostic */ 20440973Sbostic 20540973Sbostic setdefuser() 20640973Sbostic { 20740973Sbostic struct passwd *defpwent; 20857386Seric static char defuserbuf[40]; 20940973Sbostic 21057386Seric DefUser = defuserbuf; 21140973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 21257386Seric strcpy(defuserbuf, defpwent->pw_name); 21340973Sbostic else 21457386Seric strcpy(defuserbuf, "nobody"); 21540973Sbostic } 21653654Seric /* 21756836Seric ** HOST_MAP_INIT -- initialize host class structures 21856836Seric */ 21956836Seric 22056836Seric bool 22160219Seric host_map_init(map, args) 22256836Seric MAP *map; 22356836Seric char *args; 22456836Seric { 22556836Seric register char *p = args; 22656836Seric 22756836Seric for (;;) 22856836Seric { 22958050Seric while (isascii(*p) && isspace(*p)) 23056836Seric p++; 23156836Seric if (*p != '-') 23256836Seric break; 23356836Seric switch (*++p) 23456836Seric { 23556836Seric case 'a': 23656836Seric map->map_app = ++p; 23756836Seric break; 23856836Seric } 23958050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 24056836Seric p++; 24156836Seric if (*p != '\0') 24256836Seric *p++ = '\0'; 24356836Seric } 24456836Seric if (map->map_app != NULL) 24556836Seric map->map_app = newstr(map->map_app); 24656836Seric return TRUE; 24756836Seric } 24857402Seric /* 24957402Seric ** SETUPMAILERS -- initialize default mailers 25057402Seric */ 25156836Seric 25257402Seric setupmailers() 25357402Seric { 25457402Seric char buf[100]; 25557402Seric 25657403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 25757403Seric makemailer(buf); 25857403Seric 25959883Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE"); 26057402Seric makemailer(buf); 26157402Seric 26257402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 26357402Seric makemailer(buf); 26457402Seric } 26556836Seric /* 26660207Seric ** SETUPMAPS -- set up map classes 26760207Seric */ 26860207Seric 26960207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 27060207Seric { \ 27160207Seric extern bool parse __P((MAP *, char *)); \ 27260207Seric extern bool open __P((MAP *, int)); \ 27360207Seric extern void close __P((MAP *)); \ 27460207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 27560207Seric extern void store __P((MAP *, char *, char *)); \ 27660207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 27760207Seric s->s_mapclass.map_cname = name; \ 27860207Seric s->s_mapclass.map_ext = ext; \ 27960207Seric s->s_mapclass.map_cflags = flags; \ 28060207Seric s->s_mapclass.map_parse = parse; \ 28160207Seric s->s_mapclass.map_open = open; \ 28260207Seric s->s_mapclass.map_close = close; \ 28360207Seric s->s_mapclass.map_lookup = lookup; \ 28460207Seric s->s_mapclass.map_store = store; \ 28560207Seric } 28660207Seric 28760207Seric setupmaps() 28860207Seric { 28960207Seric register STAB *s; 29060207Seric 29160207Seric #ifdef NEWDB 29260207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29360207Seric map_parseargs, hash_map_open, db_map_close, 29460207Seric db_map_lookup, db_map_store); 29560207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29660207Seric map_parseargs, bt_map_open, db_map_close, 29760207Seric db_map_lookup, db_map_store); 29860207Seric #endif 29960207Seric 30060207Seric #ifdef NDBM 30160207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 30260207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 30360207Seric ndbm_map_lookup, ndbm_map_store); 30460207Seric #endif 30560207Seric 30660207Seric #ifdef NIS 30760207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 30860207Seric map_parseargs, nis_map_open, nis_map_close, 30960207Seric nis_map_lookup, nis_map_store); 31060207Seric #endif 31160207Seric 31260207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 31360207Seric map_parseargs, stab_map_open, stab_map_close, 31460207Seric stab_map_lookup, stab_map_store); 31560207Seric 31660207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 31760207Seric map_parseargs, impl_map_open, impl_map_close, 31860207Seric impl_map_lookup, impl_map_store); 31960207Seric 32060207Seric /* host DNS lookup */ 32160207Seric MAPDEF("host", NULL, 0, 32260207Seric host_map_init, null_map_open, null_map_close, 32360207Seric host_map_lookup, null_map_store); 32460207Seric 32560207Seric /* dequote map */ 32660207Seric MAPDEF("dequote", NULL, 0, 32760207Seric dequote_init, null_map_open, null_map_close, 32860207Seric dequote_map, null_map_store); 32960207Seric 33060207Seric #if 0 33160207Seric # ifdef USERDB 33260207Seric /* user database */ 33360207Seric MAPDEF("udb", ".db", 0, 33460207Seric udb_map_parse, null_map_open, null_map_close, 33560207Seric udb_map_lookup, null_map_store); 33660207Seric # endif 33760207Seric #endif 33860207Seric } 33960207Seric 34060207Seric #undef MAPDEF 34160207Seric /* 3429369Seric ** USERNAME -- return the user id of the logged in user. 3439369Seric ** 3449369Seric ** Parameters: 3459369Seric ** none. 3469369Seric ** 3479369Seric ** Returns: 3489369Seric ** The login name of the logged in user. 3499369Seric ** 3509369Seric ** Side Effects: 3519369Seric ** none. 3529369Seric ** 3539369Seric ** Notes: 3549369Seric ** The return value is statically allocated. 3559369Seric */ 3569369Seric 3579369Seric char * 3589369Seric username() 3599369Seric { 36017469Seric static char *myname = NULL; 3619369Seric extern char *getlogin(); 36219904Smiriam register struct passwd *pw; 3639369Seric 36417469Seric /* cache the result */ 36517469Seric if (myname == NULL) 36617469Seric { 36717469Seric myname = getlogin(); 36817469Seric if (myname == NULL || myname[0] == '\0') 36917469Seric { 37063787Seric pw = getpwuid(RealUid); 37117469Seric if (pw != NULL) 37240993Sbostic myname = newstr(pw->pw_name); 37317469Seric } 37419904Smiriam else 37519904Smiriam { 37663787Seric uid_t uid = RealUid; 37719873Smiriam 37840993Sbostic myname = newstr(myname); 37940993Sbostic if ((pw = getpwnam(myname)) == NULL || 38058736Seric (uid != 0 && uid != pw->pw_uid)) 38119904Smiriam { 38258736Seric pw = getpwuid(uid); 38324945Seric if (pw != NULL) 38440993Sbostic myname = newstr(pw->pw_name); 38519873Smiriam } 38619873Smiriam } 38717469Seric if (myname == NULL || myname[0] == '\0') 38817469Seric { 38958151Seric syserr("554 Who are you?"); 39017469Seric myname = "postmaster"; 39117469Seric } 39217469Seric } 39317469Seric 39417469Seric return (myname); 3959369Seric } 3969369Seric /* 3974190Seric ** TTYPATH -- Get the path of the user's tty 398294Seric ** 399294Seric ** Returns the pathname of the user's tty. Returns NULL if 400294Seric ** the user is not logged in or if s/he has write permission 401294Seric ** denied. 402294Seric ** 403294Seric ** Parameters: 404294Seric ** none 405294Seric ** 406294Seric ** Returns: 407294Seric ** pathname of the user's tty. 408294Seric ** NULL if not logged in or write permission denied. 409294Seric ** 410294Seric ** Side Effects: 411294Seric ** none. 412294Seric ** 413294Seric ** WARNING: 414294Seric ** Return value is in a local buffer. 415294Seric ** 416294Seric ** Called By: 417294Seric ** savemail 418294Seric */ 419294Seric 420294Seric char * 421294Seric ttypath() 422294Seric { 423294Seric struct stat stbuf; 424294Seric register char *pathn; 425294Seric extern char *ttyname(); 4264081Seric extern char *getlogin(); 427294Seric 428294Seric /* compute the pathname of the controlling tty */ 4299369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4309369Seric (pathn = ttyname(0)) == NULL) 431294Seric { 432294Seric errno = 0; 433294Seric return (NULL); 434294Seric } 435294Seric 436294Seric /* see if we have write permission */ 4372967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 438294Seric { 439294Seric errno = 0; 440294Seric return (NULL); 441294Seric } 442294Seric 443294Seric /* see if the user is logged in */ 444294Seric if (getlogin() == NULL) 445294Seric return (NULL); 446294Seric 447294Seric /* looks good */ 448294Seric return (pathn); 449294Seric } 4502967Seric /* 4512967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4522967Seric ** 4532967Seric ** This routine can be supplied on a per-installation basis 4542967Seric ** to determine whether a person is allowed to send a message. 4552967Seric ** This allows restriction of certain types of internet 4562967Seric ** forwarding or registration of users. 4572967Seric ** 4582967Seric ** If the hosts are found to be incompatible, an error 45957454Seric ** message should be given using "usrerr" and 0 should 4602967Seric ** be returned. 4612967Seric ** 46267473Seric ** EF_NORETURN can be set in e->e_flags to suppress the return-to-sender 4634288Seric ** function; this should be done on huge messages. 4644288Seric ** 4652967Seric ** Parameters: 4662967Seric ** to -- the person being sent to. 4672967Seric ** 4682967Seric ** Returns: 46957459Seric ** an exit status 4702967Seric ** 4712967Seric ** Side Effects: 4722967Seric ** none (unless you include the usrerr stuff) 4732967Seric */ 4742967Seric 47555012Seric checkcompat(to, e) 4762967Seric register ADDRESS *to; 47755012Seric register ENVELOPE *e; 4782967Seric { 47912133Seric # ifdef lint 48012133Seric if (to == NULL) 48112133Seric to++; 48263834Seric # endif /* lint */ 48364402Seric 48464402Seric if (tTd(49, 1)) 48564402Seric printf("checkcompat(to=%s, from=%s)\n", 48664402Seric to->q_paddr, e->e_from.q_paddr); 48764402Seric 48810698Seric # ifdef EXAMPLE_CODE 48910698Seric /* this code is intended as an example only */ 4904437Seric register STAB *s; 4914437Seric 4924437Seric s = stab("arpa", ST_MAILER, ST_FIND); 49367473Seric if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 && 4949369Seric to->q_mailer == s->s_mailer) 4954437Seric { 49658151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 49767473Seric /* e->e_flags |= EF_NORETURN; to supress return copy */ 49857459Seric return (EX_UNAVAILABLE); 4994437Seric } 50056795Seric # endif /* EXAMPLE_CODE */ 50157459Seric return (EX_OK); 5022967Seric } 5039369Seric /* 50464035Seric ** SETSIGNAL -- set a signal handler 50564035Seric ** 50664035Seric ** This is essentially old BSD "signal(3)". 50764035Seric */ 50864035Seric 50964561Seric sigfunc_t 51064035Seric setsignal(sig, handler) 51164035Seric int sig; 51264561Seric sigfunc_t handler; 51364035Seric { 51464380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 51564035Seric return signal(sig, handler); 51664035Seric #else 51764035Seric struct sigaction n, o; 51864035Seric 51964035Seric bzero(&n, sizeof n); 52064035Seric n.sa_handler = handler; 52167159Seric # ifdef SA_RESTART 52267159Seric n.sa_flags = SA_RESTART; 52367159Seric # endif 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 */ 60466301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 60514872Seric 60663962Seric /* do guesses based on general OS type */ 60751920Seric #ifndef LA_TYPE 60863962Seric # define LA_TYPE LA_ZERO 60951920Seric #endif 61051920Seric 61164360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 61251920Seric 61314872Seric #include <nlist.h> 61414872Seric 61551920Seric #ifndef LA_AVENRUN 61663962Seric # ifdef SYSTEM5 61763962Seric # define LA_AVENRUN "avenrun" 61863962Seric # else 61963962Seric # define LA_AVENRUN "_avenrun" 62063962Seric # endif 62151920Seric #endif 62251920Seric 62351920Seric /* _PATH_UNIX should be defined in <paths.h> */ 62451920Seric #ifndef _PATH_UNIX 62563962Seric # if defined(SYSTEM5) 62663962Seric # define _PATH_UNIX "/unix" 62763962Seric # else 62863962Seric # define _PATH_UNIX "/vmunix" 62963962Seric # endif 63051920Seric #endif 63151920Seric 63214872Seric struct nlist Nl[] = 63314872Seric { 63451920Seric { LA_AVENRUN }, 63514872Seric #define X_AVENRUN 0 63614872Seric { 0 }, 63714872Seric }; 63814872Seric 63960583Seric #ifndef FSHIFT 64060583Seric # if defined(unixpc) 64160583Seric # define FSHIFT 5 64260583Seric # endif 64357736Seric 64466763Seric # if defined(__alpha) || defined(IRIX) 64560583Seric # define FSHIFT 10 64660583Seric # endif 64766788Seric #endif 64857977Seric 64966788Seric #ifndef FSHIFT 65066788Seric # define FSHIFT 8 65157736Seric #endif 65260583Seric 65366788Seric #ifndef FSCALE 65466788Seric # define FSCALE (1 << FSHIFT) 65551920Seric #endif 65640930Srick 65714872Seric getla() 65814872Seric { 65914872Seric static int kmem = -1; 66051920Seric #if LA_TYPE == LA_INT 66124943Seric long avenrun[3]; 66251920Seric #else 66364360Seric # if LA_TYPE == LA_SHORT 66464360Seric short avenrun[3]; 66564360Seric # else 66651920Seric double avenrun[3]; 66764360Seric # endif 66851920Seric #endif 66925615Seric extern off_t lseek(); 67057736Seric extern int errno; 67114872Seric 67214872Seric if (kmem < 0) 67314872Seric { 67424945Seric kmem = open("/dev/kmem", 0, 0); 67514872Seric if (kmem < 0) 67657736Seric { 67757736Seric if (tTd(3, 1)) 67857736Seric printf("getla: open(/dev/kmem): %s\n", 67957736Seric errstring(errno)); 68014872Seric return (-1); 68157736Seric } 68251920Seric (void) fcntl(kmem, F_SETFD, 1); 68357736Seric if (nlist(_PATH_UNIX, Nl) < 0) 68457736Seric { 68557736Seric if (tTd(3, 1)) 68657736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 68757736Seric errstring(errno)); 68814872Seric return (-1); 68957736Seric } 69059253Seric if (Nl[X_AVENRUN].n_value == 0) 69159253Seric { 69259253Seric if (tTd(3, 1)) 69359253Seric printf("getla: nlist(%s, %s) ==> 0\n", 69459253Seric _PATH_UNIX, LA_AVENRUN); 69559253Seric return (-1); 69659253Seric } 69767435Seric #ifdef NAMELISTMASK 69867435Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 69966763Seric #endif 70014872Seric } 70157736Seric if (tTd(3, 20)) 70257736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 70324945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 70423118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 70519967Seric { 70619967Seric /* thank you Ian */ 70757736Seric if (tTd(3, 1)) 70857736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 70919967Seric return (-1); 71019967Seric } 71164360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 71257736Seric if (tTd(3, 5)) 71357736Seric { 71457736Seric printf("getla: avenrun = %d", avenrun[0]); 71557736Seric if (tTd(3, 15)) 71657736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 71757736Seric printf("\n"); 71857736Seric } 71957736Seric if (tTd(3, 1)) 72057736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 72124943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 72251920Seric #else 72357736Seric if (tTd(3, 5)) 72457736Seric { 72557736Seric printf("getla: avenrun = %g", avenrun[0]); 72657736Seric if (tTd(3, 15)) 72757736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 72857736Seric printf("\n"); 72957736Seric } 73057736Seric if (tTd(3, 1)) 73157736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 73251920Seric return ((int) (avenrun[0] + 0.5)); 73351920Seric #endif 73414872Seric } 73514872Seric 73651773Seric #else 73751920Seric #if LA_TYPE == LA_SUBR 73851773Seric 73964718Seric #ifdef DGUX 74064718Seric 74164718Seric #include <sys/dg_sys_info.h> 74264718Seric 74364718Seric int getla() 74464718Seric { 74564718Seric struct dg_sys_info_load_info load_info; 74664718Seric 74764718Seric dg_sys_info((long *)&load_info, 74864718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 74964718Seric 75064718Seric return((int) (load_info.one_minute + 0.5)); 75164718Seric } 75264718Seric 75364718Seric #else 75464718Seric 75551773Seric getla() 75651773Seric { 75751920Seric double avenrun[3]; 75851920Seric 75951920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 76057736Seric { 76157736Seric if (tTd(3, 1)) 76257736Seric perror("getla: getloadavg failed:"); 76351920Seric return (-1); 76457736Seric } 76557736Seric if (tTd(3, 1)) 76657736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 76751920Seric return ((int) (avenrun[0] + 0.5)); 76851773Seric } 76951773Seric 77064718Seric #endif /* DGUX */ 77151773Seric #else 77264295Seric #if LA_TYPE == LA_MACH 77351773Seric 77464295Seric /* 77565173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 77664295Seric */ 77764295Seric 77865173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 77965173Seric # include <mach/mach.h> 78065173Seric #else 78165173Seric # include <mach.h> 78265173Seric #endif 78364295Seric 78451773Seric getla() 78551773Seric { 78664295Seric processor_set_t default_set; 78764295Seric kern_return_t error; 78864295Seric unsigned int info_count; 78964295Seric struct processor_set_basic_info info; 79064295Seric host_t host; 79164295Seric 79264295Seric error = processor_set_default(host_self(), &default_set); 79364295Seric if (error != KERN_SUCCESS) 79464295Seric return -1; 79564295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 79664295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 79764295Seric &host, (processor_set_info_t)&info, 79864295Seric &info_count) != KERN_SUCCESS) 79964295Seric { 80064295Seric return -1; 80164295Seric } 80264295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 80364295Seric } 80464295Seric 80564295Seric 80664295Seric #else 80766313Seric #if LA_TYPE == LA_PROCSTR 80864295Seric 80966301Seric /* 81066301Seric ** Read /proc/loadavg for the load average. This is assumed to be 81166301Seric ** in a format like "0.15 0.12 0.06". 81266301Seric ** 81366301Seric ** Initially intended for Linux. This has been in the kernel 81466301Seric ** since at least 0.99.15. 81566301Seric */ 81666301Seric 81766301Seric # ifndef _PATH_LOADAVG 81866301Seric # define _PATH_LOADAVG "/proc/loadavg" 81966301Seric # endif 82066301Seric 82166301Seric int 82264295Seric getla() 82364295Seric { 82466319Seric double avenrun; 82566301Seric register int result; 82666319Seric FILE *fp; 82766301Seric 82866319Seric fp = fopen(_PATH_LOADAVG, "r"); 82966319Seric if (fp == NULL) 83066301Seric { 83166319Seric if (tTd(3, 1)) 83266319Seric printf("getla: fopen(%s): %s\n", 83366319Seric _PATH_LOADAVG, errstring(errno)); 83466319Seric return -1; 83566301Seric } 83666301Seric result = fscanf(fp, "%lf", &avenrun); 83766319Seric fclose(fp); 83866301Seric if (result != 1) 83966301Seric { 84066310Seric if (tTd(3, 1)) 84166301Seric printf("getla: fscanf() = %d: %s\n", 84266301Seric result, errstring(errno)); 84366301Seric return -1; 84466301Seric } 84566301Seric 84657736Seric if (tTd(3, 1)) 84766301Seric printf("getla(): %.2f\n", avenrun); 84866301Seric 84966301Seric return ((int) (avenrun + 0.5)); 85066301Seric } 85166301Seric 85266301Seric #else 85366301Seric 85466301Seric getla() 85566301Seric { 85666301Seric if (tTd(3, 1)) 85757736Seric printf("getla: ZERO\n"); 85851920Seric return (0); 85951773Seric } 86051773Seric 86151773Seric #endif 86251773Seric #endif 86364295Seric #endif 86466301Seric #endif 86566045Seric 86666045Seric 86766045Seric /* 86866045Seric * Copyright 1989 Massachusetts Institute of Technology 86966045Seric * 87066045Seric * Permission to use, copy, modify, distribute, and sell this software and its 87166045Seric * documentation for any purpose is hereby granted without fee, provided that 87266045Seric * the above copyright notice appear in all copies and that both that 87366045Seric * copyright notice and this permission notice appear in supporting 87466045Seric * documentation, and that the name of M.I.T. not be used in advertising or 87566045Seric * publicity pertaining to distribution of the software without specific, 87666045Seric * written prior permission. M.I.T. makes no representations about the 87766045Seric * suitability of this software for any purpose. It is provided "as is" 87866045Seric * without express or implied warranty. 87966045Seric * 88066045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 88166045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 88266045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 88366045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 88466045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 88566045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 88666045Seric * 88766045Seric * Authors: Many and varied... 88866045Seric */ 88966045Seric 89066045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 89166045Seric #ifndef lint 89266045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 89366045Seric #endif /* !lint */ 89466045Seric 89566179Seric #ifdef apollo 89666045Seric # undef volatile 89766045Seric # include <apollo/base.h> 89866045Seric 89966045Seric /* ARGSUSED */ 90066045Seric int getloadavg( call_data ) 90166045Seric caddr_t call_data; /* pointer to (double) return value */ 90266045Seric { 90366045Seric double *avenrun = (double *) call_data; 90466045Seric int i; 90566045Seric status_$t st; 90666045Seric long loadav[3]; 90766045Seric proc1_$get_loadav(loadav, &st); 90866045Seric *avenrun = loadav[0] / (double) (1 << 16); 90966045Seric return(0); 91066045Seric } 91166045Seric # endif /* apollo */ 91224943Seric /* 91324943Seric ** SHOULDQUEUE -- should this message be queued or sent? 91424943Seric ** 91524943Seric ** Compares the message cost to the load average to decide. 91624943Seric ** 91724943Seric ** Parameters: 91824943Seric ** pri -- the priority of the message in question. 91957438Seric ** ctime -- the message creation time. 92024943Seric ** 92124943Seric ** Returns: 92224943Seric ** TRUE -- if this message should be queued up for the 92324943Seric ** time being. 92424943Seric ** FALSE -- if the load is low enough to send this message. 92524943Seric ** 92624943Seric ** Side Effects: 92724943Seric ** none. 92824943Seric */ 92924943Seric 93024943Seric bool 93157438Seric shouldqueue(pri, ctime) 93224943Seric long pri; 93357438Seric time_t ctime; 93424943Seric { 93551920Seric if (CurrentLA < QueueLA) 93624943Seric return (FALSE); 93758132Seric if (CurrentLA >= RefuseLA) 93858132Seric return (TRUE); 93951920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 94024943Seric } 94124943Seric /* 94253037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 94353037Seric ** 94453037Seric ** Parameters: 94553037Seric ** none. 94653037Seric ** 94753037Seric ** Returns: 94853037Seric ** TRUE if incoming SMTP connections should be refused 94953037Seric ** (for now). 95053037Seric ** FALSE if we should accept new work. 95153037Seric ** 95253037Seric ** Side Effects: 95353037Seric ** none. 95453037Seric */ 95553037Seric 95653037Seric bool 95753037Seric refuseconnections() 95853037Seric { 95959156Seric #ifdef XLA 96059156Seric if (!xla_smtp_ok()) 96159156Seric return TRUE; 96259156Seric #endif 96359156Seric 96453037Seric /* this is probably too simplistic */ 96558132Seric return (CurrentLA >= RefuseLA); 96653037Seric } 96753037Seric /* 96824943Seric ** SETPROCTITLE -- set process title for ps 96924943Seric ** 97024943Seric ** Parameters: 97158674Seric ** fmt -- a printf style format string. 97258674Seric ** a, b, c -- possible parameters to fmt. 97324943Seric ** 97424943Seric ** Returns: 97524943Seric ** none. 97624943Seric ** 97724943Seric ** Side Effects: 97824943Seric ** Clobbers argv of our main procedure so ps(1) will 97924943Seric ** display the title. 98024943Seric */ 98124943Seric 98258689Seric #ifdef SETPROCTITLE 98366002Seric # ifdef HASSETPROCTITLE 98466002Seric *** ERROR *** Cannot have both SETPROCTITLE and HASSETPROCTITLE defined 98566002Seric # endif 98658689Seric # ifdef __hpux 98758689Seric # include <sys/pstat.h> 98858689Seric # endif 98959732Seric # ifdef BSD4_4 99059732Seric # include <machine/vmparam.h> 99159732Seric # include <sys/exec.h> 99264247Seric # ifdef __bsdi__ 99364247Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 99464843Seric # define PROCTITLEPAD '\0' 99564247Seric # endif 99659992Seric # ifdef PS_STRINGS 99759992Seric # define SETPROC_STATIC static 99859992Seric # endif 99959732Seric # endif 100059732Seric # ifndef SETPROC_STATIC 100159732Seric # define SETPROC_STATIC 100259732Seric # endif 100358689Seric #endif 100458689Seric 100564843Seric #ifndef PROCTITLEPAD 100664843Seric # define PROCTITLEPAD ' ' 100764843Seric #endif 100864843Seric 100965982Seric #ifndef HASSETPROCTITLE 101065982Seric 101124943Seric /*VARARGS1*/ 101257642Seric #ifdef __STDC__ 101357642Seric setproctitle(char *fmt, ...) 101457642Seric #else 101557642Seric setproctitle(fmt, va_alist) 101624943Seric char *fmt; 101757642Seric va_dcl 101857642Seric #endif 101924943Seric { 102024943Seric # ifdef SETPROCTITLE 102124943Seric register char *p; 102225049Seric register int i; 102359732Seric SETPROC_STATIC char buf[MAXLINE]; 102456852Seric VA_LOCAL_DECL 102558689Seric # ifdef __hpux 102658689Seric union pstun pst; 102758689Seric # endif 102824943Seric extern char **Argv; 102924943Seric extern char *LastArgv; 103024943Seric 103158674Seric p = buf; 103224943Seric 103358674Seric /* print sendmail: heading for grep */ 103458674Seric (void) strcpy(p, "sendmail: "); 103558674Seric p += strlen(p); 103624943Seric 103758674Seric /* print the argument string */ 103858674Seric VA_START(fmt); 103958674Seric (void) vsprintf(p, fmt, ap); 104056852Seric VA_END; 104154996Seric 104258674Seric i = strlen(buf); 104358689Seric 104458689Seric # ifdef __hpux 104558689Seric pst.pst_command = buf; 104658689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 104758689Seric # else 104859992Seric # ifdef PS_STRINGS 104959732Seric PS_STRINGS->ps_nargvstr = 1; 105059732Seric PS_STRINGS->ps_argvstr = buf; 105159732Seric # else 105254996Seric if (i > LastArgv - Argv[0] - 2) 105325049Seric { 105454996Seric i = LastArgv - Argv[0] - 2; 105558674Seric buf[i] = '\0'; 105625049Seric } 105758674Seric (void) strcpy(Argv[0], buf); 105854997Seric p = &Argv[0][i]; 105924943Seric while (p < LastArgv) 106064843Seric *p++ = PROCTITLEPAD; 106159732Seric # endif 106258689Seric # endif 106356795Seric # endif /* SETPROCTITLE */ 106424943Seric } 106565982Seric 106665982Seric #endif 106725698Seric /* 106825698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 106925698Seric ** 107025698Seric ** Parameters: 107125698Seric ** none. 107225698Seric ** 107325698Seric ** Returns: 107425698Seric ** none. 107525698Seric ** 107625698Seric ** Side Effects: 107725698Seric ** Picks up extant zombies. 107825698Seric */ 107925698Seric 108046928Sbostic void 108125698Seric reapchild() 108225698Seric { 108364738Seric int olderrno = errno; 108463966Seric # ifdef HASWAITPID 108560219Seric auto int status; 108660560Seric int count; 108760560Seric int pid; 108860219Seric 108960560Seric count = 0; 109060560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 109160560Seric { 109260560Seric if (count++ > 1000) 109360560Seric { 109466748Seric #ifdef LOG 109560560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 109660560Seric pid, status); 109766748Seric #endif 109860560Seric break; 109960560Seric } 110060560Seric } 110160219Seric # else 110225698Seric # ifdef WNOHANG 110325698Seric union wait status; 110425698Seric 110563753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 110625698Seric continue; 110756795Seric # else /* WNOHANG */ 110825698Seric auto int status; 110925698Seric 111060219Seric while (wait(&status) > 0) 111125698Seric continue; 111256795Seric # endif /* WNOHANG */ 111360219Seric # endif 111464035Seric # ifdef SYS5SIGNALS 111564035Seric (void) setsignal(SIGCHLD, reapchild); 111658061Seric # endif 111764738Seric errno = olderrno; 111825698Seric } 111955418Seric /* 112055418Seric ** UNSETENV -- remove a variable from the environment 112155418Seric ** 112255418Seric ** Not needed on newer systems. 112355418Seric ** 112455418Seric ** Parameters: 112555418Seric ** name -- the string name of the environment variable to be 112655418Seric ** deleted from the current environment. 112755418Seric ** 112855418Seric ** Returns: 112955418Seric ** none. 113055418Seric ** 113155418Seric ** Globals: 113255418Seric ** environ -- a pointer to the current environment. 113355418Seric ** 113455418Seric ** Side Effects: 113555418Seric ** Modifies environ. 113655418Seric */ 113755418Seric 113863962Seric #ifndef HASUNSETENV 113955418Seric 114055418Seric void 114155418Seric unsetenv(name) 114255418Seric char *name; 114355418Seric { 114455418Seric extern char **environ; 114555418Seric register char **pp; 114655418Seric int len = strlen(name); 114755418Seric 114855418Seric for (pp = environ; *pp != NULL; pp++) 114955418Seric { 115055418Seric if (strncmp(name, *pp, len) == 0 && 115155418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 115255418Seric break; 115355418Seric } 115455418Seric 115555418Seric for (; *pp != NULL; pp++) 115655418Seric *pp = pp[1]; 115755418Seric } 115855418Seric 115963962Seric #endif 116056215Seric /* 116156215Seric ** GETDTABLESIZE -- return number of file descriptors 116256215Seric ** 116356215Seric ** Only on non-BSD systems 116456215Seric ** 116556215Seric ** Parameters: 116656215Seric ** none 116756215Seric ** 116856215Seric ** Returns: 116956215Seric ** size of file descriptor table 117056215Seric ** 117156215Seric ** Side Effects: 117256215Seric ** none 117356215Seric */ 117456215Seric 117563787Seric #ifdef SOLARIS 117663787Seric # include <sys/resource.h> 117763787Seric #endif 117856215Seric 117956215Seric int 118063787Seric getdtsize() 118156215Seric { 118263787Seric #ifdef RLIMIT_NOFILE 118363787Seric struct rlimit rl; 118463787Seric 118563787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 118663787Seric return rl.rlim_cur; 118763787Seric #endif 118863787Seric 118964031Seric # ifdef HASGETDTABLESIZE 119064031Seric return getdtablesize(); 119164031Seric # else 119264031Seric # ifdef _SC_OPEN_MAX 119358689Seric return sysconf(_SC_OPEN_MAX); 119463787Seric # else 119556215Seric return NOFILE; 119663787Seric # endif 119758689Seric # endif 119856215Seric } 119957631Seric /* 120057631Seric ** UNAME -- get the UUCP name of this system. 120157631Seric */ 120257631Seric 120357943Seric #ifndef HASUNAME 120457631Seric 120557631Seric int 120657631Seric uname(name) 120757631Seric struct utsname *name; 120857631Seric { 120957631Seric FILE *file; 121057631Seric char *n; 121157631Seric 121257631Seric name->nodename[0] = '\0'; 121357631Seric 121457661Seric /* try /etc/whoami -- one line with the node name */ 121557631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 121657631Seric { 121757661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 121857631Seric (void) fclose(file); 121957661Seric n = strchr(name->nodename, '\n'); 122057631Seric if (n != NULL) 122157631Seric *n = '\0'; 122257631Seric if (name->nodename[0] != '\0') 122357631Seric return (0); 122457631Seric } 122557631Seric 122657661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 122757631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 122857631Seric { 122957631Seric char buf[MAXLINE]; 123057631Seric 123157631Seric while (fgets(buf, MAXLINE, file) != NULL) 123257631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 123357631Seric NODE_LENGTH, name->nodename) > 0) 123457631Seric break; 123557631Seric (void) fclose(file); 123657631Seric if (name->nodename[0] != '\0') 123757631Seric return (0); 123857631Seric } 123957631Seric 124057631Seric #ifdef TRUST_POPEN 124157631Seric /* 124257631Seric ** Popen is known to have security holes. 124357631Seric */ 124457631Seric 124557661Seric /* try uuname -l to return local name */ 124657631Seric if ((file = popen("uuname -l", "r")) != NULL) 124757631Seric { 124857661Seric (void) fgets(name, NODE_LENGTH + 1, file); 124957631Seric (void) pclose(file); 125057661Seric n = strchr(name, '\n'); 125157631Seric if (n != NULL) 125257631Seric *n = '\0'; 125357661Seric if (name->nodename[0] != '\0') 125457631Seric return (0); 125557631Seric } 125657631Seric #endif 125757631Seric 125857631Seric return (-1); 125957631Seric } 126057943Seric #endif /* HASUNAME */ 126158068Seric /* 126258068Seric ** INITGROUPS -- initialize groups 126358068Seric ** 126458068Seric ** Stub implementation for System V style systems 126558068Seric */ 126658068Seric 126758068Seric #ifndef HASINITGROUPS 126858068Seric 126958068Seric initgroups(name, basegid) 127058068Seric char *name; 127158068Seric int basegid; 127258068Seric { 127358068Seric return 0; 127458068Seric } 127558068Seric 127658068Seric #endif 127758082Seric /* 127859289Seric ** SETSID -- set session id (for non-POSIX systems) 127959289Seric */ 128059289Seric 128159289Seric #ifndef HASSETSID 128259289Seric 128363753Seric pid_t 128463753Seric setsid __P ((void)) 128559289Seric { 128664155Seric #ifdef TIOCNOTTY 128764155Seric int fd; 128864155Seric 128966743Seric fd = open("/dev/tty", O_RDWR, 0); 129064155Seric if (fd >= 0) 129164155Seric { 129264155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 129364155Seric (void) close(fd); 129464155Seric } 129564155Seric #endif /* TIOCNOTTY */ 129664705Seric # ifdef SYS5SETPGRP 129763753Seric return setpgrp(); 129863753Seric # else 129964155Seric return setpgid(0, getpid()); 130059289Seric # endif 130159289Seric } 130259289Seric 130359289Seric #endif 130459289Seric /* 130566748Seric ** FSYNC -- dummy fsync 130666748Seric */ 130766748Seric 130866748Seric #ifdef NEEDFSYNC 130966748Seric 131066748Seric fsync(fd) 131166748Seric int fd; 131266748Seric { 131366748Seric # ifdef O_SYNC 131466748Seric return fcntl(fd, F_SETFL, O_SYNC); 131566748Seric # else 131666748Seric /* nothing we can do */ 131766748Seric return 0; 131866748Seric # endif 131966748Seric } 132066748Seric 132166748Seric #endif 132266748Seric /* 132364718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 132464718Seric ** 132564718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 132667427Seric ** instead of a long. This patches things. Only needed on versions 132767427Seric ** prior to 5.4.3. 132864718Seric */ 132964718Seric 133067427Seric #ifdef DGUX_5_4_2 133164718Seric 133264718Seric #undef inet_addr 133364718Seric 133464718Seric long 133564718Seric dgux_inet_addr(host) 133664718Seric char *host; 133764718Seric { 133864718Seric struct in_addr haddr; 133964718Seric 134064718Seric haddr = inet_addr(host); 134164718Seric return haddr.s_addr; 134264718Seric } 134364718Seric 134464718Seric #endif 134564718Seric /* 134663969Seric ** GETOPT -- for old systems or systems with bogus implementations 134763969Seric */ 134863969Seric 134963969Seric #ifdef NEEDGETOPT 135063969Seric 135163969Seric /* 135263969Seric * Copyright (c) 1985 Regents of the University of California. 135363969Seric * All rights reserved. The Berkeley software License Agreement 135463969Seric * specifies the terms and conditions for redistribution. 135563969Seric */ 135663969Seric 135763969Seric 135863969Seric /* 135963969Seric ** this version hacked to add `atend' flag to allow state machine 136063969Seric ** to reset if invoked by the program to scan args for a 2nd time 136163969Seric */ 136263969Seric 136363969Seric #if defined(LIBC_SCCS) && !defined(lint) 136463969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 136564072Seric #endif /* LIBC_SCCS and not lint */ 136663969Seric 136763969Seric #include <stdio.h> 136863969Seric 136963969Seric /* 137063969Seric * get option letter from argument vector 137163969Seric */ 137265352Seric #ifdef _CONVEX_SOURCE 137365352Seric extern int optind, opterr; 137465352Seric #else 137565352Seric int opterr = 1; /* if error message should be printed */ 137665352Seric int optind = 1; /* index into parent argv vector */ 137765352Seric #endif 137865352Seric int optopt; /* character checked for validity */ 137963969Seric char *optarg; /* argument associated with option */ 138063969Seric 138163969Seric #define BADCH (int)'?' 138263969Seric #define EMSG "" 138363969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 138463969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 138563969Seric 138663969Seric getopt(nargc,nargv,ostr) 138765160Seric int nargc; 138865160Seric char *const *nargv; 138965160Seric const char *ostr; 139063969Seric { 139163969Seric static char *place = EMSG; /* option letter processing */ 139263969Seric static char atend = 0; 139363969Seric register char *oli; /* option letter list index */ 139463969Seric 139563969Seric if (atend) { 139663969Seric atend = 0; 139763969Seric place = EMSG; 139863969Seric } 139963969Seric if(!*place) { /* update scanning pointer */ 140064136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 140163969Seric atend++; 140263969Seric return(EOF); 140363969Seric } 140463969Seric if (*place == '-') { /* found "--" */ 140563969Seric ++optind; 140663969Seric atend++; 140763969Seric return(EOF); 140863969Seric } 140963969Seric } /* option letter okay? */ 141064125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 141164136Seric if (!*place) ++optind; 141263969Seric tell(": illegal option -- "); 141363969Seric } 141463969Seric if (*++oli != ':') { /* don't need argument */ 141563969Seric optarg = NULL; 141663969Seric if (!*place) ++optind; 141763969Seric } 141863969Seric else { /* need an argument */ 141963969Seric if (*place) optarg = place; /* no white space */ 142063969Seric else if (nargc <= ++optind) { /* no arg */ 142163969Seric place = EMSG; 142263969Seric tell(": option requires an argument -- "); 142363969Seric } 142463969Seric else optarg = nargv[optind]; /* white space */ 142563969Seric place = EMSG; 142663969Seric ++optind; 142763969Seric } 142863969Seric return(optopt); /* dump back option letter */ 142963969Seric } 143063969Seric 143163969Seric #endif 143263969Seric /* 143363969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 143463969Seric */ 143563969Seric 143663969Seric #ifdef NEEDVPRINTF 143763969Seric 143863969Seric #define MAXARG 16 143963969Seric 144063969Seric vfprintf(fp, fmt, ap) 144163969Seric FILE * fp; 144263969Seric char * fmt; 144363969Seric char ** ap; 144463969Seric { 144563969Seric char * bp[MAXARG]; 144663969Seric int i = 0; 144763969Seric 144863969Seric while (*ap && i < MAXARG) 144963969Seric bp[i++] = *ap++; 145063969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 145163969Seric bp[4], bp[5], bp[6], bp[7], 145263969Seric bp[8], bp[9], bp[10], bp[11], 145363969Seric bp[12], bp[13], bp[14], bp[15]); 145463969Seric } 145563969Seric 145663969Seric vsprintf(s, fmt, ap) 145763969Seric char * s; 145863969Seric char * fmt; 145963969Seric char ** ap; 146063969Seric { 146163969Seric char * bp[MAXARG]; 146263969Seric int i = 0; 146363969Seric 146463969Seric while (*ap && i < MAXARG) 146563969Seric bp[i++] = *ap++; 146663969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 146763969Seric bp[4], bp[5], bp[6], bp[7], 146863969Seric bp[8], bp[9], bp[10], bp[11], 146963969Seric bp[12], bp[13], bp[14], bp[15]); 147063969Seric } 147163969Seric 147263969Seric #endif 147363969Seric /* 147465220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 147565208Seric ** 147665211Seric ** Parameters: 147765211Seric ** shell -- the user's shell from /etc/passwd 147865211Seric ** 147965211Seric ** Returns: 148065211Seric ** TRUE -- if it is ok to use this for unrestricted access. 148165211Seric ** FALSE -- if the shell is restricted. 148265208Seric */ 148365208Seric 148465587Seric #if !HASGETUSERSHELL 148565587Seric 148665587Seric # ifndef _PATH_SHELLS 148765587Seric # define _PATH_SHELLS "/etc/shells" 148865587Seric # endif 148965587Seric 149065587Seric char *DefaultUserShells[] = 149165587Seric { 149265587Seric "/bin/sh", 149365587Seric "/usr/bin/sh", 149465587Seric "/bin/csh", 149565587Seric "/usr/bin/csh", 149665587Seric #ifdef __hpux 149765587Seric "/bin/rsh", 149865587Seric "/bin/ksh", 149965587Seric "/bin/rksh", 150065587Seric "/bin/pam", 150165587Seric "/usr/bin/keysh", 150265587Seric "/bin/posix/sh", 150365211Seric #endif 150465587Seric NULL 150565587Seric }; 150665587Seric 150765208Seric #endif 150865208Seric 150965747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 151065747Seric 151165211Seric bool 151265211Seric usershellok(shell) 151365211Seric char *shell; 151465208Seric { 151565211Seric #if HASGETUSERSHELL 151665211Seric register char *p; 151765211Seric extern char *getusershell(); 151865208Seric 151965211Seric setusershell(); 152065211Seric while ((p = getusershell()) != NULL) 152165747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 152265211Seric break; 152365211Seric endusershell(); 152465211Seric return p != NULL; 152565211Seric #else 152665211Seric register FILE *shellf; 152765211Seric char buf[MAXLINE]; 152865208Seric 152965211Seric shellf = fopen(_PATH_SHELLS, "r"); 153065211Seric if (shellf == NULL) 153165211Seric { 153265211Seric /* no /etc/shells; see if it is one of the std shells */ 153365587Seric char **d; 153465587Seric 153565587Seric for (d = DefaultUserShells; *d != NULL; d++) 153665587Seric { 153765587Seric if (strcmp(shell, *d) == 0) 153865587Seric return TRUE; 153965587Seric } 154065587Seric return FALSE; 154165211Seric } 154265208Seric 154365211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 154465211Seric { 154565211Seric register char *p, *q; 154665208Seric 154765211Seric p = buf; 154865211Seric while (*p != '\0' && *p != '#' && *p != '/') 154965211Seric p++; 155065211Seric if (*p == '#' || *p == '\0') 155165208Seric continue; 155265211Seric q = p; 155365211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 155465211Seric p++; 155565211Seric *p = '\0'; 155665747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 155765211Seric { 155865211Seric fclose(shellf); 155965211Seric return TRUE; 156065211Seric } 156165208Seric } 156265211Seric fclose(shellf); 156365211Seric return FALSE; 156465211Seric #endif 156565208Seric } 156665208Seric /* 156764311Seric ** FREESPACE -- see how much free space is on the queue filesystem 156858082Seric ** 156958082Seric ** Only implemented if you have statfs. 157058082Seric ** 157158082Seric ** Parameters: 157264311Seric ** dir -- the directory in question. 157364311Seric ** bsize -- a variable into which the filesystem 157464311Seric ** block size is stored. 157558082Seric ** 157658082Seric ** Returns: 157764311Seric ** The number of bytes free on the queue filesystem. 157864311Seric ** -1 if the statfs call fails. 157964311Seric ** 158064311Seric ** Side effects: 158164311Seric ** Puts the filesystem block size into bsize. 158258082Seric */ 158358082Seric 158465749Seric /* statfs types */ 158565749Seric #define SFS_NONE 0 /* no statfs implementation */ 158665749Seric #define SFS_USTAT 1 /* use ustat */ 158765749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 158865749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 158965749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 159065749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 159166752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 159265749Seric 159365749Seric #ifndef SFS_TYPE 159465749Seric # define SFS_TYPE SFS_NONE 159558157Seric #endif 159658157Seric 159765749Seric #if SFS_TYPE == SFS_USTAT 159858157Seric # include <ustat.h> 159958157Seric #endif 160065749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 160165749Seric # include <sys/statfs.h> 160258082Seric #endif 160365749Seric #if SFS_TYPE == SFS_VFS 160465749Seric # include <sys/vfs.h> 160565749Seric #endif 160665749Seric #if SFS_TYPE == SFS_MOUNT 160765749Seric # include <sys/mount.h> 160865749Seric #endif 160966752Seric #if SFS_TYPE == SFS_STATVFS 161066752Seric # include <sys/statvfs.h> 161166752Seric #endif 161258082Seric 161364311Seric long 161464311Seric freespace(dir, bsize) 161564311Seric char *dir; 161664311Seric long *bsize; 161758082Seric { 161865749Seric #if SFS_TYPE != SFS_NONE 161965749Seric # if SFS_TYPE == SFS_USTAT 162058153Seric struct ustat fs; 162158153Seric struct stat statbuf; 162258366Seric # define FSBLOCKSIZE DEV_BSIZE 162358157Seric # define f_bavail f_tfree 162458157Seric # else 162558157Seric # if defined(ultrix) 162658157Seric struct fs_data fs; 162758157Seric # define f_bavail fd_bfreen 162867467Seric # define FSBLOCKSIZE 1024L 162958153Seric # else 163066752Seric # if SFS_TYPE == SFS_STATVFS 163166752Seric struct statvfs fs; 163266893Seric # define FSBLOCKSIZE fs.f_bsize 163366752Seric # else 163458082Seric struct statfs fs; 163566752Seric # define FSBLOCKSIZE fs.f_bsize 163666752Seric # if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) 163766752Seric # define f_bavail f_bfree 163866752Seric # endif 163963987Seric # endif 164058153Seric # endif 164158133Seric # endif 164258082Seric extern int errno; 164358082Seric 164465749Seric # if SFS_TYPE == SFS_USTAT 164564311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 164658157Seric # else 164765749Seric # if SFS_TYPE == SFS_4ARGS 164864311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 164958157Seric # else 165067159Seric # if SFS_TYPE == SFS_STATVFS 165167159Seric if (statvfs(dir, &fs) == 0) 165267159Seric # else 165367159Seric # if defined(ultrix) 165464311Seric if (statfs(dir, &fs) > 0) 165567159Seric # else 165664311Seric if (statfs(dir, &fs) == 0) 165767159Seric # endif 165858153Seric # endif 165958133Seric # endif 166058133Seric # endif 166158133Seric { 166264311Seric if (bsize != NULL) 166364311Seric *bsize = FSBLOCKSIZE; 166464311Seric return (fs.f_bavail); 166564311Seric } 166664337Seric #endif 166764311Seric return (-1); 166864311Seric } 166964311Seric /* 167064311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 167164311Seric ** 167264311Seric ** Only implemented if you have statfs. 167364311Seric ** 167464311Seric ** Parameters: 167564311Seric ** msize -- the size to check against. If zero, we don't yet 167664311Seric ** know how big the message will be, so just check for 167764311Seric ** a "reasonable" amount. 167864311Seric ** 167964311Seric ** Returns: 168064311Seric ** TRUE if there is enough space. 168164311Seric ** FALSE otherwise. 168264311Seric */ 168364311Seric 168464311Seric bool 168564311Seric enoughspace(msize) 168664311Seric long msize; 168764311Seric { 168864311Seric long bfree, bsize; 168964311Seric 169064311Seric if (MinBlocksFree <= 0 && msize <= 0) 169164311Seric { 169258133Seric if (tTd(4, 80)) 169364311Seric printf("enoughspace: no threshold\n"); 169464311Seric return TRUE; 169564311Seric } 169664311Seric 169764311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 169864311Seric { 169964311Seric if (tTd(4, 80)) 170058333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 170164311Seric bfree, msize); 170258333Seric 170358333Seric /* convert msize to block count */ 170464311Seric msize = msize / bsize + 1; 170558333Seric if (MinBlocksFree >= 0) 170658333Seric msize += MinBlocksFree; 170758333Seric 170864311Seric if (bfree < msize) 170958090Seric { 171058090Seric #ifdef LOG 171158090Seric if (LogLevel > 0) 171264254Seric syslog(LOG_ALERT, 171364254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 171464311Seric CurEnv->e_id, bfree, 171564254Seric CurHostName, msize, QueueDir); 171658090Seric #endif 171758082Seric return FALSE; 171858090Seric } 171958082Seric } 172058082Seric else if (tTd(4, 80)) 172158333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 172258333Seric MinBlocksFree, msize, errstring(errno)); 172358082Seric return TRUE; 172458082Seric } 172558542Seric /* 172658542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 172758542Seric ** 172858542Seric ** This looks at an errno value and tells if this is likely to 172958542Seric ** go away if retried later. 173058542Seric ** 173158542Seric ** Parameters: 173258542Seric ** err -- the errno code to classify. 173358542Seric ** 173458542Seric ** Returns: 173558542Seric ** TRUE if this is probably transient. 173658542Seric ** FALSE otherwise. 173758542Seric */ 173858542Seric 173958542Seric bool 174058542Seric transienterror(err) 174158542Seric int err; 174258542Seric { 174358542Seric switch (err) 174458542Seric { 174558542Seric case EIO: /* I/O error */ 174658542Seric case ENXIO: /* Device not configured */ 174758542Seric case EAGAIN: /* Resource temporarily unavailable */ 174858542Seric case ENOMEM: /* Cannot allocate memory */ 174958542Seric case ENODEV: /* Operation not supported by device */ 175058542Seric case ENFILE: /* Too many open files in system */ 175158542Seric case EMFILE: /* Too many open files */ 175258542Seric case ENOSPC: /* No space left on device */ 175358542Seric #ifdef ETIMEDOUT 175458542Seric case ETIMEDOUT: /* Connection timed out */ 175558542Seric #endif 175658542Seric #ifdef ESTALE 175758542Seric case ESTALE: /* Stale NFS file handle */ 175858542Seric #endif 175958542Seric #ifdef ENETDOWN 176058542Seric case ENETDOWN: /* Network is down */ 176158542Seric #endif 176258542Seric #ifdef ENETUNREACH 176358542Seric case ENETUNREACH: /* Network is unreachable */ 176458542Seric #endif 176558542Seric #ifdef ENETRESET 176658542Seric case ENETRESET: /* Network dropped connection on reset */ 176758542Seric #endif 176858542Seric #ifdef ECONNABORTED 176958542Seric case ECONNABORTED: /* Software caused connection abort */ 177058542Seric #endif 177158542Seric #ifdef ECONNRESET 177258542Seric case ECONNRESET: /* Connection reset by peer */ 177358542Seric #endif 177458542Seric #ifdef ENOBUFS 177558542Seric case ENOBUFS: /* No buffer space available */ 177658542Seric #endif 177758542Seric #ifdef ESHUTDOWN 177858542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 177958542Seric #endif 178058542Seric #ifdef ECONNREFUSED 178158542Seric case ECONNREFUSED: /* Connection refused */ 178258542Seric #endif 178358542Seric #ifdef EHOSTDOWN 178458542Seric case EHOSTDOWN: /* Host is down */ 178558542Seric #endif 178658542Seric #ifdef EHOSTUNREACH 178758542Seric case EHOSTUNREACH: /* No route to host */ 178858542Seric #endif 178958542Seric #ifdef EDQUOT 179058542Seric case EDQUOT: /* Disc quota exceeded */ 179158542Seric #endif 179258542Seric #ifdef EPROCLIM 179358542Seric case EPROCLIM: /* Too many processes */ 179458542Seric #endif 179558542Seric #ifdef EUSERS 179658542Seric case EUSERS: /* Too many users */ 179758542Seric #endif 179858542Seric #ifdef EDEADLK 179958542Seric case EDEADLK: /* Resource deadlock avoided */ 180058542Seric #endif 180158542Seric #ifdef EISCONN 180258542Seric case EISCONN: /* Socket already connected */ 180358542Seric #endif 180458542Seric #ifdef EINPROGRESS 180558542Seric case EINPROGRESS: /* Operation now in progress */ 180658542Seric #endif 180758542Seric #ifdef EALREADY 180858542Seric case EALREADY: /* Operation already in progress */ 180958542Seric #endif 181058542Seric #ifdef EADDRINUSE 181158542Seric case EADDRINUSE: /* Address already in use */ 181258542Seric #endif 181358542Seric #ifdef EADDRNOTAVAIL 181458542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 181558542Seric #endif 181666018Seric #ifdef ETXTBSY 181766017Seric case ETXTBSY: /* (Apollo) file locked */ 181866017Seric #endif 181963834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 182058542Seric case ENOSR: /* Out of streams resources */ 182158542Seric #endif 182258542Seric return TRUE; 182358542Seric } 182458542Seric 182558542Seric /* nope, must be permanent */ 182658542Seric return FALSE; 182758542Seric } 182858689Seric /* 182964035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 183058689Seric ** 183158689Seric ** Parameters: 183258689Seric ** fd -- the file descriptor of the file. 183358689Seric ** filename -- the file name (for error messages). 183464335Seric ** ext -- the filename extension. 183558689Seric ** type -- type of the lock. Bits can be: 183658689Seric ** LOCK_EX -- exclusive lock. 183758689Seric ** LOCK_NB -- non-blocking. 183858689Seric ** 183958689Seric ** Returns: 184058689Seric ** TRUE if the lock was acquired. 184158689Seric ** FALSE otherwise. 184258689Seric */ 184358689Seric 184458689Seric bool 184564335Seric lockfile(fd, filename, ext, type) 184658689Seric int fd; 184758689Seric char *filename; 184864335Seric char *ext; 184958689Seric int type; 185058689Seric { 185165830Seric # if !HASFLOCK 185258689Seric int action; 185358689Seric struct flock lfd; 185464335Seric 185564335Seric if (ext == NULL) 185664335Seric ext = ""; 185764035Seric 185863983Seric bzero(&lfd, sizeof lfd); 185959447Seric if (bitset(LOCK_UN, type)) 186059447Seric lfd.l_type = F_UNLCK; 186159447Seric else if (bitset(LOCK_EX, type)) 186258689Seric lfd.l_type = F_WRLCK; 186358689Seric else 186458689Seric lfd.l_type = F_RDLCK; 186558689Seric 186658689Seric if (bitset(LOCK_NB, type)) 186758689Seric action = F_SETLK; 186858689Seric else 186958689Seric action = F_SETLKW; 187058689Seric 187164264Seric if (tTd(55, 60)) 187264335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 187364335Seric filename, ext, action, lfd.l_type); 187464264Seric 187558689Seric if (fcntl(fd, action, &lfd) >= 0) 187664264Seric { 187764264Seric if (tTd(55, 60)) 187864264Seric printf("SUCCESS\n"); 187958689Seric return TRUE; 188064264Seric } 188158689Seric 188264264Seric if (tTd(55, 60)) 188364264Seric printf("(%s) ", errstring(errno)); 188464264Seric 188564136Seric /* 188664136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 188764136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 188864136Seric ** as type "tmp" (that is, served from swap space), the 188964136Seric ** previous fcntl will fail with "Invalid argument" errors. 189064136Seric ** Since this is fairly common during testing, we will assume 189164136Seric ** that this indicates that the lock is successfully grabbed. 189264136Seric */ 189364136Seric 189464136Seric if (errno == EINVAL) 189564264Seric { 189664264Seric if (tTd(55, 60)) 189764264Seric printf("SUCCESS\n"); 189864136Seric return TRUE; 189964264Seric } 190064136Seric 190158689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 190264378Seric { 190364378Seric int omode = -1; 190464378Seric # ifdef F_GETFL 190564378Seric int oerrno = errno; 190664378Seric 190764378Seric (void) fcntl(fd, F_GETFL, &omode); 190864378Seric errno = oerrno; 190964378Seric # endif 191064378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 191164378Seric filename, ext, fd, type, omode, geteuid()); 191264378Seric } 191358689Seric # else 191464335Seric if (ext == NULL) 191564335Seric ext = ""; 191664335Seric 191764264Seric if (tTd(55, 60)) 191864335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 191964264Seric 192058689Seric if (flock(fd, type) >= 0) 192164264Seric { 192264264Seric if (tTd(55, 60)) 192364264Seric printf("SUCCESS\n"); 192458689Seric return TRUE; 192564264Seric } 192658689Seric 192764264Seric if (tTd(55, 60)) 192864264Seric printf("(%s) ", errstring(errno)); 192964264Seric 193058689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 193164378Seric { 193264378Seric int omode = -1; 193364378Seric # ifdef F_GETFL 193464378Seric int oerrno = errno; 193564378Seric 193664378Seric (void) fcntl(fd, F_GETFL, &omode); 193764378Seric errno = oerrno; 193864378Seric # endif 193964378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 194064378Seric filename, ext, fd, type, omode, geteuid()); 194164386Seric } 194258689Seric # endif 194364264Seric if (tTd(55, 60)) 194464264Seric printf("FAIL\n"); 194558689Seric return FALSE; 194658689Seric } 194764556Seric /* 194865948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 194965948Seric ** 195065948Seric ** Parameters: 195165948Seric ** fd -- the file descriptor to check. 195265948Seric ** 195365948Seric ** Returns: 195465948Seric ** TRUE -- if only root can chown the file to an arbitrary 195565948Seric ** user. 195665948Seric ** FALSE -- if an arbitrary user can give away a file. 195765948Seric */ 195865948Seric 195965948Seric bool 196065948Seric chownsafe(fd) 196165948Seric int fd; 196265948Seric { 196365950Seric #ifdef __hpux 196465948Seric char *s; 196565948Seric int tfd; 196665948Seric uid_t o_uid, o_euid; 196765948Seric gid_t o_gid, o_egid; 196865948Seric bool rval; 196965948Seric struct stat stbuf; 197065948Seric 197165948Seric o_uid = getuid(); 197265948Seric o_euid = geteuid(); 197365948Seric o_gid = getgid(); 197465948Seric o_egid = getegid(); 197565959Seric fstat(fd, &stbuf); 197665948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 197765948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 197865948Seric s = tmpnam(NULL); 197965948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 198065974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 198165948Seric close(tfd); 198265948Seric unlink(s); 198365948Seric setreuid(o_uid, o_euid); 198465948Seric setresgid(o_gid, o_egid, -1); 198565948Seric return rval; 198665948Seric #else 198766088Seric # ifdef _POSIX_CHOWN_RESTRICTED 198866088Seric # if _POSIX_CHOWN_RESTRICTED == -1 198966088Seric return FALSE; 199066088Seric # else 199166088Seric return TRUE; 199266088Seric # endif 199366088Seric # else 199466088Seric # ifdef _PC_CHOWN_RESTRICTED 199567447Seric int rval; 199667447Seric 199767447Seric /* 199867447Seric ** Some systems (e.g., SunOS) seem to have the call and the 199967447Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 200067447Seric ** the call. This heuristic checks for that. 200167447Seric */ 200267447Seric 200367447Seric errno = 0; 200467447Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 200567447Seric if (errno == 0) 200667447Seric return rval > 0; 200767447Seric # endif 200867447Seric # ifdef BSD 200967447Seric return TRUE; 201066088Seric # else 201165948Seric return FALSE; 201265948Seric # endif 201365948Seric # endif 201465948Seric #endif 201565948Seric } 201665948Seric /* 201767430Seric ** RESETLIMITS -- reset system controlled resource limits 201867430Seric ** 201967430Seric ** This is to avoid denial-of-service attacks 202067430Seric ** 202167430Seric ** Parameters: 202267430Seric ** none 202367430Seric ** 202467430Seric ** Returns: 202567430Seric ** none 202667430Seric */ 202767430Seric 202867430Seric #if HASSETRLIMIT 202967430Seric # include <sys/resource.h> 203067430Seric #endif 203167430Seric 203267430Seric resetlimits() 203367430Seric { 203467430Seric #if HASSETRLIMIT 203567430Seric struct rlimit lim; 203667430Seric 203767430Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 203867430Seric (void) setrlimit(RLIMIT_CPU, &lim); 203967430Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 204067430Seric #else 204167430Seric # if HASULIMIT 204267430Seric (void) ulimit(2, 0x3fffff); 204367430Seric # endif 204467430Seric #endif 204567430Seric } 204667430Seric /* 204764556Seric ** GETCFNAME -- return the name of the .cf file. 204864556Seric ** 204964556Seric ** Some systems (e.g., NeXT) determine this dynamically. 205064556Seric */ 205164556Seric 205264556Seric char * 205364556Seric getcfname() 205464556Seric { 205564556Seric if (ConfFile != NULL) 205664556Seric return ConfFile; 205765214Seric #ifdef NETINFO 205865214Seric { 205965214Seric extern char *ni_propval(); 206065214Seric char *cflocation; 206165214Seric 206265214Seric cflocation = ni_propval("/locations/sendmail", "sendmail.cf"); 206365214Seric if (cflocation != NULL) 206465214Seric return cflocation; 206565214Seric } 206665214Seric #endif 206764556Seric return _PATH_SENDMAILCF; 206864556Seric } 206964718Seric /* 207064718Seric ** SETVENDOR -- process vendor code from V configuration line 207164718Seric ** 207264718Seric ** Parameters: 207364718Seric ** vendor -- string representation of vendor. 207464718Seric ** 207564718Seric ** Returns: 207664718Seric ** TRUE -- if ok. 207764718Seric ** FALSE -- if vendor code could not be processed. 207864926Seric ** 207964926Seric ** Side Effects: 208064926Seric ** It is reasonable to set mode flags here to tweak 208164926Seric ** processing in other parts of the code if necessary. 208264926Seric ** For example, if you are a vendor that uses $%y to 208364926Seric ** indicate YP lookups, you could enable that here. 208464718Seric */ 208564718Seric 208664718Seric bool 208764718Seric setvendor(vendor) 208864718Seric char *vendor; 208964718Seric { 209064926Seric if (strcasecmp(vendor, "Berkeley") == 0) 209164926Seric return TRUE; 209264926Seric 209364926Seric /* add vendor extensions here */ 209464926Seric 209564926Seric return FALSE; 209664718Seric } 209764816Seric /* 209864816Seric ** STRTOL -- convert string to long integer 209964816Seric ** 210064816Seric ** For systems that don't have it in the C library. 210166161Seric ** 210266161Seric ** This is taken verbatim from the 4.4-Lite C library. 210364816Seric */ 210464816Seric 210564816Seric #ifdef NEEDSTRTOL 210664816Seric 210766161Seric #if defined(LIBC_SCCS) && !defined(lint) 210866161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 210966161Seric #endif /* LIBC_SCCS and not lint */ 211066161Seric 211166161Seric #include <limits.h> 211266161Seric 211366161Seric /* 211466161Seric * Convert a string to a long integer. 211566161Seric * 211666161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 211766161Seric * alphabets and digits are each contiguous. 211866161Seric */ 211966161Seric 212064816Seric long 212166161Seric strtol(nptr, endptr, base) 212266161Seric const char *nptr; 212366161Seric char **endptr; 212466161Seric register int base; 212564816Seric { 212666161Seric register const char *s = nptr; 212766161Seric register unsigned long acc; 212866161Seric register int c; 212966161Seric register unsigned long cutoff; 213066161Seric register int neg = 0, any, cutlim; 213164816Seric 213266161Seric /* 213366161Seric * Skip white space and pick up leading +/- sign if any. 213466161Seric * If base is 0, allow 0x for hex and 0 for octal, else 213566161Seric * assume decimal; if base is already 16, allow 0x. 213666161Seric */ 213766161Seric do { 213866161Seric c = *s++; 213966161Seric } while (isspace(c)); 214066161Seric if (c == '-') { 214166161Seric neg = 1; 214266161Seric c = *s++; 214366161Seric } else if (c == '+') 214466161Seric c = *s++; 214566161Seric if ((base == 0 || base == 16) && 214666161Seric c == '0' && (*s == 'x' || *s == 'X')) { 214766161Seric c = s[1]; 214866161Seric s += 2; 214966161Seric base = 16; 215066161Seric } 215166161Seric if (base == 0) 215266161Seric base = c == '0' ? 8 : 10; 215364816Seric 215466161Seric /* 215566161Seric * Compute the cutoff value between legal numbers and illegal 215666161Seric * numbers. That is the largest legal value, divided by the 215766161Seric * base. An input number that is greater than this value, if 215866161Seric * followed by a legal input character, is too big. One that 215966161Seric * is equal to this value may be valid or not; the limit 216066161Seric * between valid and invalid numbers is then based on the last 216166161Seric * digit. For instance, if the range for longs is 216266161Seric * [-2147483648..2147483647] and the input base is 10, 216366161Seric * cutoff will be set to 214748364 and cutlim to either 216466161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 216566161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 216666161Seric * the number is too big, and we will return a range error. 216766161Seric * 216866161Seric * Set any if any `digits' consumed; make it negative to indicate 216966161Seric * overflow. 217066161Seric */ 217166161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 217266161Seric cutlim = cutoff % (unsigned long)base; 217366161Seric cutoff /= (unsigned long)base; 217466161Seric for (acc = 0, any = 0;; c = *s++) { 217566161Seric if (isdigit(c)) 217666161Seric c -= '0'; 217766161Seric else if (isalpha(c)) 217866161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 217966161Seric else 218066161Seric break; 218166161Seric if (c >= base) 218266161Seric break; 218366161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 218466161Seric any = -1; 218566161Seric else { 218666161Seric any = 1; 218766161Seric acc *= base; 218866161Seric acc += c; 218964816Seric } 219064816Seric } 219166161Seric if (any < 0) { 219266161Seric acc = neg ? LONG_MIN : LONG_MAX; 219366161Seric errno = ERANGE; 219466161Seric } else if (neg) 219566161Seric acc = -acc; 219666161Seric if (endptr != 0) 219766161Seric *endptr = (char *)(any ? s - 1 : nptr); 219866161Seric return (acc); 219964816Seric } 220064816Seric 220164816Seric #endif 220264841Seric /* 220364841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 220464841Seric ** 220567456Seric ** Solaris versions at least through 2.3 don't properly deliver a 220664841Seric ** canonical h_name field. This tries to work around it. 220764841Seric */ 220864841Seric 220964841Seric #ifdef SOLARIS 221064841Seric 221167456Seric extern int h_errno; 221267456Seric 221364841Seric struct hostent * 221464841Seric solaris_gethostbyname(name) 221565094Seric const char *name; 221664841Seric { 221764841Seric # ifdef SOLARIS_2_3 221864841Seric static struct hostent hp; 221964841Seric static char buf[1000]; 222064841Seric extern struct hostent *_switch_gethostbyname_r(); 222164841Seric 222264841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 222364841Seric # else 222465009Seric extern struct hostent *__switch_gethostbyname(); 222565009Seric 222664841Seric return __switch_gethostbyname(name); 222764841Seric # endif 222864841Seric } 222964841Seric 223064841Seric struct hostent * 223164841Seric solaris_gethostbyaddr(addr, len, type) 223265094Seric const char *addr; 223364841Seric int len; 223464841Seric int type; 223564841Seric { 223664841Seric # ifdef SOLARIS_2_3 223764841Seric static struct hostent hp; 223864841Seric static char buf[1000]; 223964841Seric extern struct hostent *_switch_gethostbyaddr_r(); 224064841Seric 224164841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 224264841Seric # else 224365009Seric extern struct hostent *__switch_gethostbyaddr(); 224465009Seric 224564841Seric return __switch_gethostbyaddr(addr, len, type); 224664841Seric # endif 224764841Seric } 224864841Seric 224964841Seric #endif 225065214Seric /* 225165214Seric ** NI_PROPVAL -- netinfo property value lookup routine 225265214Seric ** 225365214Seric ** Parameters: 225465214Seric ** directory -- the Netinfo directory name. 225565214Seric ** propname -- the Netinfo property name. 225665214Seric ** 225765214Seric ** Returns: 225865214Seric ** NULL -- if: 225965214Seric ** 1. the directory is not found 226065214Seric ** 2. the property name is not found 226165214Seric ** 3. the property contains multiple values 226265214Seric ** 4. some error occured 226365214Seric ** else -- the location of the config file. 226465214Seric ** 226565214Seric ** Notes: 226665214Seric ** Caller should free the return value of ni_proval 226765214Seric */ 226865214Seric 226965214Seric #ifdef NETINFO 227065214Seric 227165214Seric # include <netinfo/ni.h> 227265214Seric 227365214Seric # define LOCAL_NETINFO_DOMAIN "." 227465214Seric # define PARENT_NETINFO_DOMAIN ".." 227565214Seric # define MAX_NI_LEVELS 256 227665214Seric 227765214Seric char * 227865214Seric ni_propval(directory, propname) 227965214Seric char *directory; 228065214Seric char *propname; 228165214Seric { 228265820Seric char *propval = NULL; 228365214Seric int i; 228465214Seric void *ni = NULL; 228565214Seric void *lastni = NULL; 228665214Seric ni_status nis; 228765214Seric ni_id nid; 228865214Seric ni_namelist ninl; 228965214Seric 229065214Seric /* 229165214Seric ** If the passed directory and property name are found 229265214Seric ** in one of netinfo domains we need to search (starting 229365214Seric ** from the local domain moving all the way back to the 229465214Seric ** root domain) set propval to the property's value 229565214Seric ** and return it. 229665214Seric */ 229765214Seric 229865214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 229965214Seric { 230065214Seric if (i == 0) 230165214Seric { 230265214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 230365214Seric } 230465214Seric else 230565214Seric { 230665214Seric if (lastni != NULL) 230765214Seric ni_free(lastni); 230865214Seric lastni = ni; 230965214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 231065214Seric } 231165214Seric 231265214Seric /* 231365214Seric ** Don't bother if we didn't get a handle on a 231465214Seric ** proper domain. This is not necessarily an error. 231565214Seric ** We would get a positive ni_status if, for instance 231665214Seric ** we never found the directory or property and tried 231765214Seric ** to open the parent of the root domain! 231865214Seric */ 231965214Seric 232065214Seric if (nis != 0) 232165214Seric break; 232265214Seric 232365214Seric /* 232465214Seric ** Find the path to the server information. 232565214Seric */ 232665214Seric 232765214Seric if (ni_pathsearch(ni, &nid, directory) != 0) 232865214Seric continue; 232965214Seric 233065214Seric /* 233165214Seric ** Find "host" information. 233265214Seric */ 233365214Seric 233465214Seric if (ni_lookupprop(ni, &nid, propname, &ninl) != 0) 233565214Seric continue; 233665214Seric 233765214Seric /* 233865214Seric ** If there's only one name in 233965214Seric ** the list, assume we've got 234065214Seric ** what we want. 234165214Seric */ 234265214Seric 234365214Seric if (ninl.ni_namelist_len == 1) 234465214Seric { 234565214Seric propval = ni_name_dup(ninl.ni_namelist_val[0]); 234665214Seric break; 234765214Seric } 234865214Seric } 234965214Seric 235065214Seric /* 235165214Seric ** Clean up. 235265214Seric */ 235365214Seric 235465214Seric if (ni != NULL) 235565214Seric ni_free(ni); 235665214Seric if (lastni != NULL && ni != lastni) 235765214Seric ni_free(lastni); 235865214Seric 235965214Seric return propval; 236065214Seric } 236165214Seric 236265214Seric #endif /* NETINFO */ 236365504Seric /* 236465504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 236565504Seric ** 236665504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 236765504Seric ** syslog succeeds during interrupt handlers. 236865504Seric */ 236965504Seric 237065504Seric #ifdef __hpux 237165504Seric 237265504Seric # define MAXSYSLOGTRIES 100 237365504Seric # undef syslog 237465504Seric 237565504Seric # ifdef __STDC__ 237665504Seric hard_syslog(int pri, char *msg, ...) 237765504Seric # else 237865504Seric hard_syslog(pri, msg, va_alist) 237965504Seric int pri; 238065504Seric char *msg; 238165504Seric va_dcl 238265504Seric # endif 238365504Seric { 238465504Seric int i; 238565504Seric char buf[SYSLOG_BUFSIZE * 2]; 238665504Seric VA_LOCAL_DECL; 238765504Seric 238865504Seric VA_START(msg); 238965504Seric vsprintf(buf, msg, ap); 239065504Seric VA_END; 239165504Seric 239265504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 239365504Seric continue; 239465504Seric } 239565504Seric 239665504Seric #endif 2397