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*67159Seric static char sccsid[] = "@(#)conf.c 8.90 (Berkeley) 05/17/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; 526*67159Seric # ifdef SA_RESTART 527*67159Seric n.sa_flags = SA_RESTART; 528*67159Seric # 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 } 70266763Seric #ifdef IRIX 70366763Seric Nl[X_AVENRUN].n_value &= 0x7fffffff; 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 133164718Seric ** instead of a long. This patches things. 133264718Seric */ 133364718Seric 133464718Seric #ifdef DGUX 133564718Seric 133664718Seric #undef inet_addr 133764718Seric 133864718Seric long 133964718Seric dgux_inet_addr(host) 134064718Seric char *host; 134164718Seric { 134264718Seric struct in_addr haddr; 134364718Seric 134464718Seric haddr = inet_addr(host); 134564718Seric return haddr.s_addr; 134664718Seric } 134764718Seric 134864718Seric #endif 134964718Seric /* 135063969Seric ** GETOPT -- for old systems or systems with bogus implementations 135163969Seric */ 135263969Seric 135363969Seric #ifdef NEEDGETOPT 135463969Seric 135563969Seric /* 135663969Seric * Copyright (c) 1985 Regents of the University of California. 135763969Seric * All rights reserved. The Berkeley software License Agreement 135863969Seric * specifies the terms and conditions for redistribution. 135963969Seric */ 136063969Seric 136163969Seric 136263969Seric /* 136363969Seric ** this version hacked to add `atend' flag to allow state machine 136463969Seric ** to reset if invoked by the program to scan args for a 2nd time 136563969Seric */ 136663969Seric 136763969Seric #if defined(LIBC_SCCS) && !defined(lint) 136863969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 136964072Seric #endif /* LIBC_SCCS and not lint */ 137063969Seric 137163969Seric #include <stdio.h> 137263969Seric 137363969Seric /* 137463969Seric * get option letter from argument vector 137563969Seric */ 137665352Seric #ifdef _CONVEX_SOURCE 137765352Seric extern int optind, opterr; 137865352Seric #else 137965352Seric int opterr = 1; /* if error message should be printed */ 138065352Seric int optind = 1; /* index into parent argv vector */ 138165352Seric #endif 138265352Seric int optopt; /* character checked for validity */ 138363969Seric char *optarg; /* argument associated with option */ 138463969Seric 138563969Seric #define BADCH (int)'?' 138663969Seric #define EMSG "" 138763969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 138863969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 138963969Seric 139063969Seric getopt(nargc,nargv,ostr) 139165160Seric int nargc; 139265160Seric char *const *nargv; 139365160Seric const char *ostr; 139463969Seric { 139563969Seric static char *place = EMSG; /* option letter processing */ 139663969Seric static char atend = 0; 139763969Seric register char *oli; /* option letter list index */ 139863969Seric 139963969Seric if (atend) { 140063969Seric atend = 0; 140163969Seric place = EMSG; 140263969Seric } 140363969Seric if(!*place) { /* update scanning pointer */ 140464136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 140563969Seric atend++; 140663969Seric return(EOF); 140763969Seric } 140863969Seric if (*place == '-') { /* found "--" */ 140963969Seric ++optind; 141063969Seric atend++; 141163969Seric return(EOF); 141263969Seric } 141363969Seric } /* option letter okay? */ 141464125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 141564136Seric if (!*place) ++optind; 141663969Seric tell(": illegal option -- "); 141763969Seric } 141863969Seric if (*++oli != ':') { /* don't need argument */ 141963969Seric optarg = NULL; 142063969Seric if (!*place) ++optind; 142163969Seric } 142263969Seric else { /* need an argument */ 142363969Seric if (*place) optarg = place; /* no white space */ 142463969Seric else if (nargc <= ++optind) { /* no arg */ 142563969Seric place = EMSG; 142663969Seric tell(": option requires an argument -- "); 142763969Seric } 142863969Seric else optarg = nargv[optind]; /* white space */ 142963969Seric place = EMSG; 143063969Seric ++optind; 143163969Seric } 143263969Seric return(optopt); /* dump back option letter */ 143363969Seric } 143463969Seric 143563969Seric #endif 143663969Seric /* 143763969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 143863969Seric */ 143963969Seric 144063969Seric #ifdef NEEDVPRINTF 144163969Seric 144263969Seric #define MAXARG 16 144363969Seric 144463969Seric vfprintf(fp, fmt, ap) 144563969Seric FILE * fp; 144663969Seric char * fmt; 144763969Seric char ** ap; 144863969Seric { 144963969Seric char * bp[MAXARG]; 145063969Seric int i = 0; 145163969Seric 145263969Seric while (*ap && i < MAXARG) 145363969Seric bp[i++] = *ap++; 145463969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 145563969Seric bp[4], bp[5], bp[6], bp[7], 145663969Seric bp[8], bp[9], bp[10], bp[11], 145763969Seric bp[12], bp[13], bp[14], bp[15]); 145863969Seric } 145963969Seric 146063969Seric vsprintf(s, fmt, ap) 146163969Seric char * s; 146263969Seric char * fmt; 146363969Seric char ** ap; 146463969Seric { 146563969Seric char * bp[MAXARG]; 146663969Seric int i = 0; 146763969Seric 146863969Seric while (*ap && i < MAXARG) 146963969Seric bp[i++] = *ap++; 147063969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 147163969Seric bp[4], bp[5], bp[6], bp[7], 147263969Seric bp[8], bp[9], bp[10], bp[11], 147363969Seric bp[12], bp[13], bp[14], bp[15]); 147463969Seric } 147563969Seric 147663969Seric #endif 147763969Seric /* 147865220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 147965208Seric ** 148065211Seric ** Parameters: 148165211Seric ** shell -- the user's shell from /etc/passwd 148265211Seric ** 148365211Seric ** Returns: 148465211Seric ** TRUE -- if it is ok to use this for unrestricted access. 148565211Seric ** FALSE -- if the shell is restricted. 148665208Seric */ 148765208Seric 148865587Seric #if !HASGETUSERSHELL 148965587Seric 149065587Seric # ifndef _PATH_SHELLS 149165587Seric # define _PATH_SHELLS "/etc/shells" 149265587Seric # endif 149365587Seric 149465587Seric char *DefaultUserShells[] = 149565587Seric { 149665587Seric "/bin/sh", 149765587Seric "/usr/bin/sh", 149865587Seric "/bin/csh", 149965587Seric "/usr/bin/csh", 150065587Seric #ifdef __hpux 150165587Seric "/bin/rsh", 150265587Seric "/bin/ksh", 150365587Seric "/bin/rksh", 150465587Seric "/bin/pam", 150565587Seric "/usr/bin/keysh", 150665587Seric "/bin/posix/sh", 150765211Seric #endif 150865587Seric NULL 150965587Seric }; 151065587Seric 151165208Seric #endif 151265208Seric 151365747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 151465747Seric 151565211Seric bool 151665211Seric usershellok(shell) 151765211Seric char *shell; 151865208Seric { 151965211Seric #if HASGETUSERSHELL 152065211Seric register char *p; 152165211Seric extern char *getusershell(); 152265208Seric 152365211Seric setusershell(); 152465211Seric while ((p = getusershell()) != NULL) 152565747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 152665211Seric break; 152765211Seric endusershell(); 152865211Seric return p != NULL; 152965211Seric #else 153065211Seric register FILE *shellf; 153165211Seric char buf[MAXLINE]; 153265208Seric 153365211Seric shellf = fopen(_PATH_SHELLS, "r"); 153465211Seric if (shellf == NULL) 153565211Seric { 153665211Seric /* no /etc/shells; see if it is one of the std shells */ 153765587Seric char **d; 153865587Seric 153965587Seric for (d = DefaultUserShells; *d != NULL; d++) 154065587Seric { 154165587Seric if (strcmp(shell, *d) == 0) 154265587Seric return TRUE; 154365587Seric } 154465587Seric return FALSE; 154565211Seric } 154665208Seric 154765211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 154865211Seric { 154965211Seric register char *p, *q; 155065208Seric 155165211Seric p = buf; 155265211Seric while (*p != '\0' && *p != '#' && *p != '/') 155365211Seric p++; 155465211Seric if (*p == '#' || *p == '\0') 155565208Seric continue; 155665211Seric q = p; 155765211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 155865211Seric p++; 155965211Seric *p = '\0'; 156065747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 156165211Seric { 156265211Seric fclose(shellf); 156365211Seric return TRUE; 156465211Seric } 156565208Seric } 156665211Seric fclose(shellf); 156765211Seric return FALSE; 156865211Seric #endif 156965208Seric } 157065208Seric /* 157164311Seric ** FREESPACE -- see how much free space is on the queue filesystem 157258082Seric ** 157358082Seric ** Only implemented if you have statfs. 157458082Seric ** 157558082Seric ** Parameters: 157664311Seric ** dir -- the directory in question. 157764311Seric ** bsize -- a variable into which the filesystem 157864311Seric ** block size is stored. 157958082Seric ** 158058082Seric ** Returns: 158164311Seric ** The number of bytes free on the queue filesystem. 158264311Seric ** -1 if the statfs call fails. 158364311Seric ** 158464311Seric ** Side effects: 158564311Seric ** Puts the filesystem block size into bsize. 158658082Seric */ 158758082Seric 158865749Seric /* statfs types */ 158965749Seric #define SFS_NONE 0 /* no statfs implementation */ 159065749Seric #define SFS_USTAT 1 /* use ustat */ 159165749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 159265749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 159365749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 159465749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 159566752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 159665749Seric 159765749Seric #ifndef SFS_TYPE 159865749Seric # define SFS_TYPE SFS_NONE 159958157Seric #endif 160058157Seric 160165749Seric #if SFS_TYPE == SFS_USTAT 160258157Seric # include <ustat.h> 160358157Seric #endif 160465749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 160565749Seric # include <sys/statfs.h> 160658082Seric #endif 160765749Seric #if SFS_TYPE == SFS_VFS 160865749Seric # include <sys/vfs.h> 160965749Seric #endif 161065749Seric #if SFS_TYPE == SFS_MOUNT 161165749Seric # include <sys/mount.h> 161265749Seric #endif 161366752Seric #if SFS_TYPE == SFS_STATVFS 161466752Seric # include <sys/statvfs.h> 161566752Seric #endif 161658082Seric 161764311Seric long 161864311Seric freespace(dir, bsize) 161964311Seric char *dir; 162064311Seric long *bsize; 162158082Seric { 162265749Seric #if SFS_TYPE != SFS_NONE 162365749Seric # if SFS_TYPE == SFS_USTAT 162458153Seric struct ustat fs; 162558153Seric struct stat statbuf; 162658366Seric # define FSBLOCKSIZE DEV_BSIZE 162758157Seric # define f_bavail f_tfree 162858157Seric # else 162958157Seric # if defined(ultrix) 163058157Seric struct fs_data fs; 163158157Seric # define f_bavail fd_bfreen 163258366Seric # define FSBLOCKSIZE fs.fd_bsize 163358153Seric # else 163466752Seric # if SFS_TYPE == SFS_STATVFS 163566752Seric struct statvfs fs; 163666893Seric # define FSBLOCKSIZE fs.f_bsize 163766752Seric # else 163858082Seric struct statfs fs; 163966752Seric # define FSBLOCKSIZE fs.f_bsize 164066752Seric # if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) 164166752Seric # define f_bavail f_bfree 164266752Seric # endif 164363987Seric # endif 164458153Seric # endif 164558133Seric # endif 164658082Seric extern int errno; 164758082Seric 164865749Seric # if SFS_TYPE == SFS_USTAT 164964311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 165058157Seric # else 165165749Seric # if SFS_TYPE == SFS_4ARGS 165264311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 165358157Seric # else 1654*67159Seric # if SFS_TYPE == SFS_STATVFS 1655*67159Seric if (statvfs(dir, &fs) == 0) 1656*67159Seric # else 1657*67159Seric # if defined(ultrix) 165864311Seric if (statfs(dir, &fs) > 0) 1659*67159Seric # else 166064311Seric if (statfs(dir, &fs) == 0) 1661*67159Seric # endif 166258153Seric # endif 166358133Seric # endif 166458133Seric # endif 166558133Seric { 166664311Seric if (bsize != NULL) 166764311Seric *bsize = FSBLOCKSIZE; 166864311Seric return (fs.f_bavail); 166964311Seric } 167064337Seric #endif 167164311Seric return (-1); 167264311Seric } 167364311Seric /* 167464311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 167564311Seric ** 167664311Seric ** Only implemented if you have statfs. 167764311Seric ** 167864311Seric ** Parameters: 167964311Seric ** msize -- the size to check against. If zero, we don't yet 168064311Seric ** know how big the message will be, so just check for 168164311Seric ** a "reasonable" amount. 168264311Seric ** 168364311Seric ** Returns: 168464311Seric ** TRUE if there is enough space. 168564311Seric ** FALSE otherwise. 168664311Seric */ 168764311Seric 168864311Seric bool 168964311Seric enoughspace(msize) 169064311Seric long msize; 169164311Seric { 169264311Seric long bfree, bsize; 169364311Seric 169464311Seric if (MinBlocksFree <= 0 && msize <= 0) 169564311Seric { 169658133Seric if (tTd(4, 80)) 169764311Seric printf("enoughspace: no threshold\n"); 169864311Seric return TRUE; 169964311Seric } 170064311Seric 170164311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 170264311Seric { 170364311Seric if (tTd(4, 80)) 170458333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 170564311Seric bfree, msize); 170658333Seric 170758333Seric /* convert msize to block count */ 170864311Seric msize = msize / bsize + 1; 170958333Seric if (MinBlocksFree >= 0) 171058333Seric msize += MinBlocksFree; 171158333Seric 171264311Seric if (bfree < msize) 171358090Seric { 171458090Seric #ifdef LOG 171558090Seric if (LogLevel > 0) 171664254Seric syslog(LOG_ALERT, 171764254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 171864311Seric CurEnv->e_id, bfree, 171964254Seric CurHostName, msize, QueueDir); 172058090Seric #endif 172158082Seric return FALSE; 172258090Seric } 172358082Seric } 172458082Seric else if (tTd(4, 80)) 172558333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 172658333Seric MinBlocksFree, msize, errstring(errno)); 172758082Seric return TRUE; 172858082Seric } 172958542Seric /* 173058542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 173158542Seric ** 173258542Seric ** This looks at an errno value and tells if this is likely to 173358542Seric ** go away if retried later. 173458542Seric ** 173558542Seric ** Parameters: 173658542Seric ** err -- the errno code to classify. 173758542Seric ** 173858542Seric ** Returns: 173958542Seric ** TRUE if this is probably transient. 174058542Seric ** FALSE otherwise. 174158542Seric */ 174258542Seric 174358542Seric bool 174458542Seric transienterror(err) 174558542Seric int err; 174658542Seric { 174758542Seric switch (err) 174858542Seric { 174958542Seric case EIO: /* I/O error */ 175058542Seric case ENXIO: /* Device not configured */ 175158542Seric case EAGAIN: /* Resource temporarily unavailable */ 175258542Seric case ENOMEM: /* Cannot allocate memory */ 175358542Seric case ENODEV: /* Operation not supported by device */ 175458542Seric case ENFILE: /* Too many open files in system */ 175558542Seric case EMFILE: /* Too many open files */ 175658542Seric case ENOSPC: /* No space left on device */ 175758542Seric #ifdef ETIMEDOUT 175858542Seric case ETIMEDOUT: /* Connection timed out */ 175958542Seric #endif 176058542Seric #ifdef ESTALE 176158542Seric case ESTALE: /* Stale NFS file handle */ 176258542Seric #endif 176358542Seric #ifdef ENETDOWN 176458542Seric case ENETDOWN: /* Network is down */ 176558542Seric #endif 176658542Seric #ifdef ENETUNREACH 176758542Seric case ENETUNREACH: /* Network is unreachable */ 176858542Seric #endif 176958542Seric #ifdef ENETRESET 177058542Seric case ENETRESET: /* Network dropped connection on reset */ 177158542Seric #endif 177258542Seric #ifdef ECONNABORTED 177358542Seric case ECONNABORTED: /* Software caused connection abort */ 177458542Seric #endif 177558542Seric #ifdef ECONNRESET 177658542Seric case ECONNRESET: /* Connection reset by peer */ 177758542Seric #endif 177858542Seric #ifdef ENOBUFS 177958542Seric case ENOBUFS: /* No buffer space available */ 178058542Seric #endif 178158542Seric #ifdef ESHUTDOWN 178258542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 178358542Seric #endif 178458542Seric #ifdef ECONNREFUSED 178558542Seric case ECONNREFUSED: /* Connection refused */ 178658542Seric #endif 178758542Seric #ifdef EHOSTDOWN 178858542Seric case EHOSTDOWN: /* Host is down */ 178958542Seric #endif 179058542Seric #ifdef EHOSTUNREACH 179158542Seric case EHOSTUNREACH: /* No route to host */ 179258542Seric #endif 179358542Seric #ifdef EDQUOT 179458542Seric case EDQUOT: /* Disc quota exceeded */ 179558542Seric #endif 179658542Seric #ifdef EPROCLIM 179758542Seric case EPROCLIM: /* Too many processes */ 179858542Seric #endif 179958542Seric #ifdef EUSERS 180058542Seric case EUSERS: /* Too many users */ 180158542Seric #endif 180258542Seric #ifdef EDEADLK 180358542Seric case EDEADLK: /* Resource deadlock avoided */ 180458542Seric #endif 180558542Seric #ifdef EISCONN 180658542Seric case EISCONN: /* Socket already connected */ 180758542Seric #endif 180858542Seric #ifdef EINPROGRESS 180958542Seric case EINPROGRESS: /* Operation now in progress */ 181058542Seric #endif 181158542Seric #ifdef EALREADY 181258542Seric case EALREADY: /* Operation already in progress */ 181358542Seric #endif 181458542Seric #ifdef EADDRINUSE 181558542Seric case EADDRINUSE: /* Address already in use */ 181658542Seric #endif 181758542Seric #ifdef EADDRNOTAVAIL 181858542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 181958542Seric #endif 182066018Seric #ifdef ETXTBSY 182166017Seric case ETXTBSY: /* (Apollo) file locked */ 182266017Seric #endif 182363834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 182458542Seric case ENOSR: /* Out of streams resources */ 182558542Seric #endif 182658542Seric return TRUE; 182758542Seric } 182858542Seric 182958542Seric /* nope, must be permanent */ 183058542Seric return FALSE; 183158542Seric } 183258689Seric /* 183364035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 183458689Seric ** 183558689Seric ** Parameters: 183658689Seric ** fd -- the file descriptor of the file. 183758689Seric ** filename -- the file name (for error messages). 183864335Seric ** ext -- the filename extension. 183958689Seric ** type -- type of the lock. Bits can be: 184058689Seric ** LOCK_EX -- exclusive lock. 184158689Seric ** LOCK_NB -- non-blocking. 184258689Seric ** 184358689Seric ** Returns: 184458689Seric ** TRUE if the lock was acquired. 184558689Seric ** FALSE otherwise. 184658689Seric */ 184758689Seric 184858689Seric bool 184964335Seric lockfile(fd, filename, ext, type) 185058689Seric int fd; 185158689Seric char *filename; 185264335Seric char *ext; 185358689Seric int type; 185458689Seric { 185565830Seric # if !HASFLOCK 185658689Seric int action; 185758689Seric struct flock lfd; 185864335Seric 185964335Seric if (ext == NULL) 186064335Seric ext = ""; 186164035Seric 186263983Seric bzero(&lfd, sizeof lfd); 186359447Seric if (bitset(LOCK_UN, type)) 186459447Seric lfd.l_type = F_UNLCK; 186559447Seric else if (bitset(LOCK_EX, type)) 186658689Seric lfd.l_type = F_WRLCK; 186758689Seric else 186858689Seric lfd.l_type = F_RDLCK; 186958689Seric 187058689Seric if (bitset(LOCK_NB, type)) 187158689Seric action = F_SETLK; 187258689Seric else 187358689Seric action = F_SETLKW; 187458689Seric 187564264Seric if (tTd(55, 60)) 187664335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 187764335Seric filename, ext, action, lfd.l_type); 187864264Seric 187958689Seric if (fcntl(fd, action, &lfd) >= 0) 188064264Seric { 188164264Seric if (tTd(55, 60)) 188264264Seric printf("SUCCESS\n"); 188358689Seric return TRUE; 188464264Seric } 188558689Seric 188664264Seric if (tTd(55, 60)) 188764264Seric printf("(%s) ", errstring(errno)); 188864264Seric 188964136Seric /* 189064136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 189164136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 189264136Seric ** as type "tmp" (that is, served from swap space), the 189364136Seric ** previous fcntl will fail with "Invalid argument" errors. 189464136Seric ** Since this is fairly common during testing, we will assume 189564136Seric ** that this indicates that the lock is successfully grabbed. 189664136Seric */ 189764136Seric 189864136Seric if (errno == EINVAL) 189964264Seric { 190064264Seric if (tTd(55, 60)) 190164264Seric printf("SUCCESS\n"); 190264136Seric return TRUE; 190364264Seric } 190464136Seric 190558689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 190664378Seric { 190764378Seric int omode = -1; 190864378Seric # ifdef F_GETFL 190964378Seric int oerrno = errno; 191064378Seric 191164378Seric (void) fcntl(fd, F_GETFL, &omode); 191264378Seric errno = oerrno; 191364378Seric # endif 191464378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 191564378Seric filename, ext, fd, type, omode, geteuid()); 191664378Seric } 191758689Seric # else 191864335Seric if (ext == NULL) 191964335Seric ext = ""; 192064335Seric 192164264Seric if (tTd(55, 60)) 192264335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 192364264Seric 192458689Seric if (flock(fd, type) >= 0) 192564264Seric { 192664264Seric if (tTd(55, 60)) 192764264Seric printf("SUCCESS\n"); 192858689Seric return TRUE; 192964264Seric } 193058689Seric 193164264Seric if (tTd(55, 60)) 193264264Seric printf("(%s) ", errstring(errno)); 193364264Seric 193458689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 193564378Seric { 193664378Seric int omode = -1; 193764378Seric # ifdef F_GETFL 193864378Seric int oerrno = errno; 193964378Seric 194064378Seric (void) fcntl(fd, F_GETFL, &omode); 194164378Seric errno = oerrno; 194264378Seric # endif 194364378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 194464378Seric filename, ext, fd, type, omode, geteuid()); 194564386Seric } 194658689Seric # endif 194764264Seric if (tTd(55, 60)) 194864264Seric printf("FAIL\n"); 194958689Seric return FALSE; 195058689Seric } 195164556Seric /* 195265948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 195365948Seric ** 195465948Seric ** Parameters: 195565948Seric ** fd -- the file descriptor to check. 195665948Seric ** 195765948Seric ** Returns: 195865948Seric ** TRUE -- if only root can chown the file to an arbitrary 195965948Seric ** user. 196065948Seric ** FALSE -- if an arbitrary user can give away a file. 196165948Seric */ 196265948Seric 196365948Seric bool 196465948Seric chownsafe(fd) 196565948Seric int fd; 196665948Seric { 196765950Seric #ifdef __hpux 196865948Seric char *s; 196965948Seric int tfd; 197065948Seric uid_t o_uid, o_euid; 197165948Seric gid_t o_gid, o_egid; 197265948Seric bool rval; 197365948Seric struct stat stbuf; 197465948Seric 197565948Seric o_uid = getuid(); 197665948Seric o_euid = geteuid(); 197765948Seric o_gid = getgid(); 197865948Seric o_egid = getegid(); 197965959Seric fstat(fd, &stbuf); 198065948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 198165948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 198265948Seric s = tmpnam(NULL); 198365948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 198465974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 198565948Seric close(tfd); 198665948Seric unlink(s); 198765948Seric setreuid(o_uid, o_euid); 198865948Seric setresgid(o_gid, o_egid, -1); 198965948Seric return rval; 199065948Seric #else 199166088Seric # ifdef _POSIX_CHOWN_RESTRICTED 199266088Seric # if _POSIX_CHOWN_RESTRICTED == -1 199366088Seric return FALSE; 199466088Seric # else 199566088Seric return TRUE; 199666088Seric # endif 199766088Seric # else 199866088Seric # ifdef _PC_CHOWN_RESTRICTED 199965948Seric return fpathconf(fd, _PC_CHOWN_RESTRICTED) > 0; 200066088Seric # else 200166088Seric # ifdef BSD 200265948Seric return TRUE; 200366088Seric # else 200465948Seric return FALSE; 200566088Seric # endif 200665948Seric # endif 200765948Seric # endif 200865948Seric #endif 200965948Seric } 201065948Seric /* 201164556Seric ** GETCFNAME -- return the name of the .cf file. 201264556Seric ** 201364556Seric ** Some systems (e.g., NeXT) determine this dynamically. 201464556Seric */ 201564556Seric 201664556Seric char * 201764556Seric getcfname() 201864556Seric { 201964556Seric if (ConfFile != NULL) 202064556Seric return ConfFile; 202165214Seric #ifdef NETINFO 202265214Seric { 202365214Seric extern char *ni_propval(); 202465214Seric char *cflocation; 202565214Seric 202665214Seric cflocation = ni_propval("/locations/sendmail", "sendmail.cf"); 202765214Seric if (cflocation != NULL) 202865214Seric return cflocation; 202965214Seric } 203065214Seric #endif 203164556Seric return _PATH_SENDMAILCF; 203264556Seric } 203364718Seric /* 203464718Seric ** SETVENDOR -- process vendor code from V configuration line 203564718Seric ** 203664718Seric ** Parameters: 203764718Seric ** vendor -- string representation of vendor. 203864718Seric ** 203964718Seric ** Returns: 204064718Seric ** TRUE -- if ok. 204164718Seric ** FALSE -- if vendor code could not be processed. 204264926Seric ** 204364926Seric ** Side Effects: 204464926Seric ** It is reasonable to set mode flags here to tweak 204564926Seric ** processing in other parts of the code if necessary. 204664926Seric ** For example, if you are a vendor that uses $%y to 204764926Seric ** indicate YP lookups, you could enable that here. 204864718Seric */ 204964718Seric 205064718Seric bool 205164718Seric setvendor(vendor) 205264718Seric char *vendor; 205364718Seric { 205464926Seric if (strcasecmp(vendor, "Berkeley") == 0) 205564926Seric return TRUE; 205664926Seric 205764926Seric /* add vendor extensions here */ 205864926Seric 205964926Seric return FALSE; 206064718Seric } 206164816Seric /* 206264816Seric ** STRTOL -- convert string to long integer 206364816Seric ** 206464816Seric ** For systems that don't have it in the C library. 206566161Seric ** 206666161Seric ** This is taken verbatim from the 4.4-Lite C library. 206764816Seric */ 206864816Seric 206964816Seric #ifdef NEEDSTRTOL 207064816Seric 207166161Seric #if defined(LIBC_SCCS) && !defined(lint) 207266161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 207366161Seric #endif /* LIBC_SCCS and not lint */ 207466161Seric 207566161Seric #include <limits.h> 207666161Seric 207766161Seric /* 207866161Seric * Convert a string to a long integer. 207966161Seric * 208066161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 208166161Seric * alphabets and digits are each contiguous. 208266161Seric */ 208366161Seric 208464816Seric long 208566161Seric strtol(nptr, endptr, base) 208666161Seric const char *nptr; 208766161Seric char **endptr; 208866161Seric register int base; 208964816Seric { 209066161Seric register const char *s = nptr; 209166161Seric register unsigned long acc; 209266161Seric register int c; 209366161Seric register unsigned long cutoff; 209466161Seric register int neg = 0, any, cutlim; 209564816Seric 209666161Seric /* 209766161Seric * Skip white space and pick up leading +/- sign if any. 209866161Seric * If base is 0, allow 0x for hex and 0 for octal, else 209966161Seric * assume decimal; if base is already 16, allow 0x. 210066161Seric */ 210166161Seric do { 210266161Seric c = *s++; 210366161Seric } while (isspace(c)); 210466161Seric if (c == '-') { 210566161Seric neg = 1; 210666161Seric c = *s++; 210766161Seric } else if (c == '+') 210866161Seric c = *s++; 210966161Seric if ((base == 0 || base == 16) && 211066161Seric c == '0' && (*s == 'x' || *s == 'X')) { 211166161Seric c = s[1]; 211266161Seric s += 2; 211366161Seric base = 16; 211466161Seric } 211566161Seric if (base == 0) 211666161Seric base = c == '0' ? 8 : 10; 211764816Seric 211866161Seric /* 211966161Seric * Compute the cutoff value between legal numbers and illegal 212066161Seric * numbers. That is the largest legal value, divided by the 212166161Seric * base. An input number that is greater than this value, if 212266161Seric * followed by a legal input character, is too big. One that 212366161Seric * is equal to this value may be valid or not; the limit 212466161Seric * between valid and invalid numbers is then based on the last 212566161Seric * digit. For instance, if the range for longs is 212666161Seric * [-2147483648..2147483647] and the input base is 10, 212766161Seric * cutoff will be set to 214748364 and cutlim to either 212866161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 212966161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 213066161Seric * the number is too big, and we will return a range error. 213166161Seric * 213266161Seric * Set any if any `digits' consumed; make it negative to indicate 213366161Seric * overflow. 213466161Seric */ 213566161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 213666161Seric cutlim = cutoff % (unsigned long)base; 213766161Seric cutoff /= (unsigned long)base; 213866161Seric for (acc = 0, any = 0;; c = *s++) { 213966161Seric if (isdigit(c)) 214066161Seric c -= '0'; 214166161Seric else if (isalpha(c)) 214266161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 214366161Seric else 214466161Seric break; 214566161Seric if (c >= base) 214666161Seric break; 214766161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 214866161Seric any = -1; 214966161Seric else { 215066161Seric any = 1; 215166161Seric acc *= base; 215266161Seric acc += c; 215364816Seric } 215464816Seric } 215566161Seric if (any < 0) { 215666161Seric acc = neg ? LONG_MIN : LONG_MAX; 215766161Seric errno = ERANGE; 215866161Seric } else if (neg) 215966161Seric acc = -acc; 216066161Seric if (endptr != 0) 216166161Seric *endptr = (char *)(any ? s - 1 : nptr); 216266161Seric return (acc); 216364816Seric } 216464816Seric 216564816Seric #endif 216664841Seric /* 216764841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 216864841Seric ** 216964841Seric ** Solaris versions prior through 2.3 don't properly deliver a 217064841Seric ** canonical h_name field. This tries to work around it. 217164841Seric */ 217264841Seric 217364841Seric #ifdef SOLARIS 217464841Seric 217564841Seric struct hostent * 217664841Seric solaris_gethostbyname(name) 217765094Seric const char *name; 217864841Seric { 217964841Seric # ifdef SOLARIS_2_3 218064841Seric static struct hostent hp; 218164841Seric static char buf[1000]; 218264841Seric extern struct hostent *_switch_gethostbyname_r(); 218364841Seric 218464841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 218564841Seric # else 218665009Seric extern struct hostent *__switch_gethostbyname(); 218765009Seric 218864841Seric return __switch_gethostbyname(name); 218964841Seric # endif 219064841Seric } 219164841Seric 219264841Seric struct hostent * 219364841Seric solaris_gethostbyaddr(addr, len, type) 219465094Seric const char *addr; 219564841Seric int len; 219664841Seric int type; 219764841Seric { 219864841Seric # ifdef SOLARIS_2_3 219964841Seric static struct hostent hp; 220064841Seric static char buf[1000]; 220164841Seric extern struct hostent *_switch_gethostbyaddr_r(); 220264841Seric 220364841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 220464841Seric # else 220565009Seric extern struct hostent *__switch_gethostbyaddr(); 220665009Seric 220764841Seric return __switch_gethostbyaddr(addr, len, type); 220864841Seric # endif 220964841Seric } 221064841Seric 221164841Seric #endif 221265214Seric /* 221365214Seric ** NI_PROPVAL -- netinfo property value lookup routine 221465214Seric ** 221565214Seric ** Parameters: 221665214Seric ** directory -- the Netinfo directory name. 221765214Seric ** propname -- the Netinfo property name. 221865214Seric ** 221965214Seric ** Returns: 222065214Seric ** NULL -- if: 222165214Seric ** 1. the directory is not found 222265214Seric ** 2. the property name is not found 222365214Seric ** 3. the property contains multiple values 222465214Seric ** 4. some error occured 222565214Seric ** else -- the location of the config file. 222665214Seric ** 222765214Seric ** Notes: 222865214Seric ** Caller should free the return value of ni_proval 222965214Seric */ 223065214Seric 223165214Seric #ifdef NETINFO 223265214Seric 223365214Seric # include <netinfo/ni.h> 223465214Seric 223565214Seric # define LOCAL_NETINFO_DOMAIN "." 223665214Seric # define PARENT_NETINFO_DOMAIN ".." 223765214Seric # define MAX_NI_LEVELS 256 223865214Seric 223965214Seric char * 224065214Seric ni_propval(directory, propname) 224165214Seric char *directory; 224265214Seric char *propname; 224365214Seric { 224465820Seric char *propval = NULL; 224565214Seric int i; 224665214Seric void *ni = NULL; 224765214Seric void *lastni = NULL; 224865214Seric ni_status nis; 224965214Seric ni_id nid; 225065214Seric ni_namelist ninl; 225165214Seric 225265214Seric /* 225365214Seric ** If the passed directory and property name are found 225465214Seric ** in one of netinfo domains we need to search (starting 225565214Seric ** from the local domain moving all the way back to the 225665214Seric ** root domain) set propval to the property's value 225765214Seric ** and return it. 225865214Seric */ 225965214Seric 226065214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 226165214Seric { 226265214Seric if (i == 0) 226365214Seric { 226465214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 226565214Seric } 226665214Seric else 226765214Seric { 226865214Seric if (lastni != NULL) 226965214Seric ni_free(lastni); 227065214Seric lastni = ni; 227165214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 227265214Seric } 227365214Seric 227465214Seric /* 227565214Seric ** Don't bother if we didn't get a handle on a 227665214Seric ** proper domain. This is not necessarily an error. 227765214Seric ** We would get a positive ni_status if, for instance 227865214Seric ** we never found the directory or property and tried 227965214Seric ** to open the parent of the root domain! 228065214Seric */ 228165214Seric 228265214Seric if (nis != 0) 228365214Seric break; 228465214Seric 228565214Seric /* 228665214Seric ** Find the path to the server information. 228765214Seric */ 228865214Seric 228965214Seric if (ni_pathsearch(ni, &nid, directory) != 0) 229065214Seric continue; 229165214Seric 229265214Seric /* 229365214Seric ** Find "host" information. 229465214Seric */ 229565214Seric 229665214Seric if (ni_lookupprop(ni, &nid, propname, &ninl) != 0) 229765214Seric continue; 229865214Seric 229965214Seric /* 230065214Seric ** If there's only one name in 230165214Seric ** the list, assume we've got 230265214Seric ** what we want. 230365214Seric */ 230465214Seric 230565214Seric if (ninl.ni_namelist_len == 1) 230665214Seric { 230765214Seric propval = ni_name_dup(ninl.ni_namelist_val[0]); 230865214Seric break; 230965214Seric } 231065214Seric } 231165214Seric 231265214Seric /* 231365214Seric ** Clean up. 231465214Seric */ 231565214Seric 231665214Seric if (ni != NULL) 231765214Seric ni_free(ni); 231865214Seric if (lastni != NULL && ni != lastni) 231965214Seric ni_free(lastni); 232065214Seric 232165214Seric return propval; 232265214Seric } 232365214Seric 232465214Seric #endif /* NETINFO */ 232565504Seric /* 232665504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 232765504Seric ** 232865504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 232965504Seric ** syslog succeeds during interrupt handlers. 233065504Seric */ 233165504Seric 233265504Seric #ifdef __hpux 233365504Seric 233465504Seric # define MAXSYSLOGTRIES 100 233565504Seric # undef syslog 233665504Seric 233765504Seric # ifdef __STDC__ 233865504Seric hard_syslog(int pri, char *msg, ...) 233965504Seric # else 234065504Seric hard_syslog(pri, msg, va_alist) 234165504Seric int pri; 234265504Seric char *msg; 234365504Seric va_dcl 234465504Seric # endif 234565504Seric { 234665504Seric int i; 234765504Seric char buf[SYSLOG_BUFSIZE * 2]; 234865504Seric VA_LOCAL_DECL; 234965504Seric 235065504Seric VA_START(msg); 235165504Seric vsprintf(buf, msg, ap); 235265504Seric VA_END; 235365504Seric 235465504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 235565504Seric continue; 235665504Seric } 235765504Seric 235865504Seric #endif 2359