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*68457Seric static char sccsid[] = "@(#)conf.c 8.89.1.1 (Berkeley) 02/28/95"; 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 */ 57*68457Seric "resent-sender", H_FROM|H_RESENT, 58*68457Seric "resent-from", H_FROM|H_RESENT, 59*68457Seric "resent-reply-to", H_FROM|H_RESENT, 60*68457Seric "sender", H_FROM, 61*68457Seric "from", H_FROM, 62*68457Seric "reply-to", H_FROM, 63*68457Seric "full-name", H_ACHECK, 64*68457Seric "return-receipt-to", H_FROM|H_RECEIPTTO, 65*68457Seric "errors-to", H_FROM|H_ERRORSTO, 6658796Seric 678060Seric /* destination fields */ 68*68457Seric "to", H_RCPT, 69*68457Seric "resent-to", H_RCPT|H_RESENT, 70*68457Seric "cc", H_RCPT, 71*68457Seric "resent-cc", H_RCPT|H_RESENT, 72*68457Seric "bcc", H_RCPT|H_ACHECK, 73*68457Seric "resent-bcc", H_RCPT|H_ACHECK|H_RESENT, 74*68457Seric "apparently-to", H_RCPT, 7558796Seric 768060Seric /* message identification and control */ 77*68457Seric "message-id", 0, 78*68457Seric "resent-message-id", H_RESENT, 79*68457Seric "message", H_EOH, 80*68457Seric "text", H_EOH, 8158796Seric 8211417Seric /* date fields */ 83*68457Seric "date", 0, 84*68457Seric "resent-date", H_RESENT, 8558796Seric 868060Seric /* trace fields */ 87*68457Seric "received", H_TRACE|H_FORCE, 88*68457Seric "x400-received", H_TRACE|H_FORCE, 89*68457Seric "via", H_TRACE|H_FORCE, 90*68457Seric "mail-from", H_TRACE|H_FORCE, 918060Seric 9258796Seric /* miscellaneous fields */ 93*68457Seric "comments", H_FORCE, 94*68457Seric "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 */ 136*68457Seric 137*68457Seric 138*68457Seric /* 139*68457Seric ** Following should be config parameters (and probably will be in 140*68457Seric ** future releases). In the meantime, setting these is considered 141*68457Seric ** unsupported, and is intentionally undocumented. 142*68457Seric */ 143*68457Seric 144*68457Seric #ifdef BROKENSMTPPEERS 145*68457Seric bool BrokenSmtpPeers = TRUE; /* set if you have broken SMTP peers */ 146*68457Seric #else 147*68457Seric bool BrokenSmtpPeers = FALSE; /* set if you have broken SMTP peers */ 148*68457Seric #endif 149*68457Seric #ifdef NOLOOPBACKCHECK 150*68457Seric bool CheckLoopBack = FALSE; /* set to check HELO loopback */ 151*68457Seric #else 152*68457Seric bool CheckLoopBack = TRUE; /* set to check HELO loopback */ 153*68457Seric #endif 154*68457Seric 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 */ 192*68457Seric SevenBit = FALSE; /* option 7 */ 19357438Seric MaxMciCache = 1; /* option k */ 19457438Seric MciCacheTimeout = 300; /* option K */ 19557438Seric LogLevel = 9; /* option L */ 196*68457Seric settimeouts(NULL); /* option r */ 197*68457Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 198*68457Seric 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 261*68457Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 26257403Seric makemailer(buf); 26357403Seric 264*68457Seric 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, 313*68457Seric map_parseargs, nis_map_open, nis_map_close, 314*68457Seric nis_map_lookup, nis_map_store); 31560207Seric #endif 31660207Seric 31760207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 318*68457Seric 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 325*68457Seric /* host DNS lookup */ 326*68457Seric MAPDEF("host", NULL, 0, 327*68457Seric host_map_init, null_map_open, null_map_close, 328*68457Seric 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 ** 467*68457Seric ** '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); 498*68457Seric 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"); 502*68457Seric /* 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; 52664035Seric if (sigaction(sig, &n, &o) < 0) 52764035Seric return SIG_ERR; 52864035Seric return o.sa_handler; 52964035Seric #endif 53064035Seric } 53164035Seric /* 5329369Seric ** HOLDSIGS -- arrange to hold all signals 5339369Seric ** 5349369Seric ** Parameters: 5359369Seric ** none. 5369369Seric ** 5379369Seric ** Returns: 5389369Seric ** none. 5399369Seric ** 5409369Seric ** Side Effects: 5419369Seric ** Arranges that signals are held. 5429369Seric */ 5439369Seric 5449369Seric holdsigs() 5459369Seric { 5469369Seric } 5479369Seric /* 5489369Seric ** RLSESIGS -- arrange to release all signals 5499369Seric ** 5509369Seric ** This undoes the effect of holdsigs. 5519369Seric ** 5529369Seric ** Parameters: 5539369Seric ** none. 5549369Seric ** 5559369Seric ** Returns: 5569369Seric ** none. 5579369Seric ** 5589369Seric ** Side Effects: 5599369Seric ** Arranges that signals are released. 5609369Seric */ 5619369Seric 5629369Seric rlsesigs() 5639369Seric { 5649369Seric } 56514872Seric /* 56664705Seric ** INIT_MD -- do machine dependent initializations 56764705Seric ** 56864705Seric ** Systems that have global modes that should be set should do 56964705Seric ** them here rather than in main. 57064705Seric */ 57164705Seric 57264705Seric #ifdef _AUX_SOURCE 57364705Seric # include <compat.h> 57464705Seric #endif 57564705Seric 57664825Seric init_md(argc, argv) 57764825Seric int argc; 57864825Seric char **argv; 57964705Seric { 58064705Seric #ifdef _AUX_SOURCE 58164705Seric setcompat(getcompat() | COMPAT_BSDPROT); 58264705Seric #endif 58364705Seric } 58464705Seric /* 58514872Seric ** GETLA -- get the current load average 58614872Seric ** 58714881Seric ** This code stolen from la.c. 58814881Seric ** 58914872Seric ** Parameters: 59014872Seric ** none. 59114872Seric ** 59214872Seric ** Returns: 59314872Seric ** The current load average as an integer. 59414872Seric ** 59514872Seric ** Side Effects: 59614872Seric ** none. 59714872Seric */ 59814872Seric 59951920Seric /* try to guess what style of load average we have */ 60051920Seric #define LA_ZERO 1 /* always return load average as zero */ 60164360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 60251920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 60351920Seric #define LA_SUBR 4 /* call getloadavg */ 60464295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 60564360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 60666301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 60714872Seric 60863962Seric /* do guesses based on general OS type */ 60951920Seric #ifndef LA_TYPE 61063962Seric # define LA_TYPE LA_ZERO 61151920Seric #endif 61251920Seric 61364360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 61451920Seric 61514872Seric #include <nlist.h> 61614872Seric 61751920Seric #ifndef LA_AVENRUN 61863962Seric # ifdef SYSTEM5 61963962Seric # define LA_AVENRUN "avenrun" 62063962Seric # else 62163962Seric # define LA_AVENRUN "_avenrun" 62263962Seric # endif 62351920Seric #endif 62451920Seric 62551920Seric /* _PATH_UNIX should be defined in <paths.h> */ 62651920Seric #ifndef _PATH_UNIX 62763962Seric # if defined(SYSTEM5) 62863962Seric # define _PATH_UNIX "/unix" 62963962Seric # else 63063962Seric # define _PATH_UNIX "/vmunix" 63163962Seric # endif 63251920Seric #endif 63351920Seric 63414872Seric struct nlist Nl[] = 63514872Seric { 63651920Seric { LA_AVENRUN }, 63714872Seric #define X_AVENRUN 0 63814872Seric { 0 }, 63914872Seric }; 64014872Seric 64160583Seric #ifndef FSHIFT 64260583Seric # if defined(unixpc) 64360583Seric # define FSHIFT 5 64460583Seric # endif 64557736Seric 64666763Seric # if defined(__alpha) || defined(IRIX) 64760583Seric # define FSHIFT 10 64860583Seric # endif 64966788Seric #endif 65057977Seric 65166788Seric #ifndef FSHIFT 65266788Seric # define FSHIFT 8 65357736Seric #endif 65460583Seric 65566788Seric #ifndef FSCALE 65666788Seric # define FSCALE (1 << FSHIFT) 65751920Seric #endif 65840930Srick 65914872Seric getla() 66014872Seric { 66114872Seric static int kmem = -1; 66251920Seric #if LA_TYPE == LA_INT 66324943Seric long avenrun[3]; 66451920Seric #else 66564360Seric # if LA_TYPE == LA_SHORT 66664360Seric short avenrun[3]; 66764360Seric # else 66851920Seric double avenrun[3]; 66964360Seric # endif 67051920Seric #endif 67125615Seric extern off_t lseek(); 67257736Seric extern int errno; 67314872Seric 67414872Seric if (kmem < 0) 67514872Seric { 67624945Seric kmem = open("/dev/kmem", 0, 0); 67714872Seric if (kmem < 0) 67857736Seric { 67957736Seric if (tTd(3, 1)) 68057736Seric printf("getla: open(/dev/kmem): %s\n", 68157736Seric errstring(errno)); 68214872Seric return (-1); 68357736Seric } 68451920Seric (void) fcntl(kmem, F_SETFD, 1); 68557736Seric if (nlist(_PATH_UNIX, Nl) < 0) 68657736Seric { 68757736Seric if (tTd(3, 1)) 68857736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 68957736Seric errstring(errno)); 69014872Seric return (-1); 69157736Seric } 69259253Seric if (Nl[X_AVENRUN].n_value == 0) 69359253Seric { 69459253Seric if (tTd(3, 1)) 69559253Seric printf("getla: nlist(%s, %s) ==> 0\n", 69659253Seric _PATH_UNIX, LA_AVENRUN); 69759253Seric return (-1); 69859253Seric } 699*68457Seric #ifdef IRIX 700*68457Seric Nl[X_AVENRUN].n_value &= 0x7fffffff; 70166763Seric #endif 70214872Seric } 70357736Seric if (tTd(3, 20)) 70457736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 705*68457Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 70623118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 70719967Seric { 70819967Seric /* thank you Ian */ 70957736Seric if (tTd(3, 1)) 71057736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 71119967Seric return (-1); 71219967Seric } 71364360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 71457736Seric if (tTd(3, 5)) 71557736Seric { 71657736Seric printf("getla: avenrun = %d", avenrun[0]); 71757736Seric if (tTd(3, 15)) 71857736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 71957736Seric printf("\n"); 72057736Seric } 72157736Seric if (tTd(3, 1)) 72257736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 72324943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 72451920Seric #else 72557736Seric if (tTd(3, 5)) 72657736Seric { 72757736Seric printf("getla: avenrun = %g", avenrun[0]); 72857736Seric if (tTd(3, 15)) 72957736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 73057736Seric printf("\n"); 73157736Seric } 73257736Seric if (tTd(3, 1)) 73357736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 73451920Seric return ((int) (avenrun[0] + 0.5)); 73551920Seric #endif 73614872Seric } 73714872Seric 73851773Seric #else 73951920Seric #if LA_TYPE == LA_SUBR 74051773Seric 74164718Seric #ifdef DGUX 74264718Seric 74364718Seric #include <sys/dg_sys_info.h> 74464718Seric 745*68457Seric int getla() 74664718Seric { 74764718Seric struct dg_sys_info_load_info load_info; 74864718Seric 74964718Seric dg_sys_info((long *)&load_info, 75064718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 75164718Seric 75264718Seric return((int) (load_info.one_minute + 0.5)); 75364718Seric } 75464718Seric 75564718Seric #else 75664718Seric 75751773Seric getla() 75851773Seric { 75951920Seric double avenrun[3]; 76051920Seric 76151920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 76257736Seric { 76357736Seric if (tTd(3, 1)) 76457736Seric perror("getla: getloadavg failed:"); 76551920Seric return (-1); 76657736Seric } 76757736Seric if (tTd(3, 1)) 76857736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 76951920Seric return ((int) (avenrun[0] + 0.5)); 77051773Seric } 77151773Seric 77264718Seric #endif /* DGUX */ 77351773Seric #else 77464295Seric #if LA_TYPE == LA_MACH 77551773Seric 77664295Seric /* 77765173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 77864295Seric */ 77964295Seric 78065173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 78165173Seric # include <mach/mach.h> 78265173Seric #else 78365173Seric # include <mach.h> 78465173Seric #endif 78564295Seric 78651773Seric getla() 78751773Seric { 78864295Seric processor_set_t default_set; 78964295Seric kern_return_t error; 79064295Seric unsigned int info_count; 79164295Seric struct processor_set_basic_info info; 79264295Seric host_t host; 79364295Seric 79464295Seric error = processor_set_default(host_self(), &default_set); 79564295Seric if (error != KERN_SUCCESS) 79664295Seric return -1; 79764295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 79864295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 79964295Seric &host, (processor_set_info_t)&info, 80064295Seric &info_count) != KERN_SUCCESS) 80164295Seric { 80264295Seric return -1; 80364295Seric } 80464295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 80564295Seric } 80664295Seric 80764295Seric 80864295Seric #else 80966313Seric #if LA_TYPE == LA_PROCSTR 81064295Seric 81166301Seric /* 81266301Seric ** Read /proc/loadavg for the load average. This is assumed to be 81366301Seric ** in a format like "0.15 0.12 0.06". 81466301Seric ** 81566301Seric ** Initially intended for Linux. This has been in the kernel 81666301Seric ** since at least 0.99.15. 81766301Seric */ 81866301Seric 81966301Seric # ifndef _PATH_LOADAVG 82066301Seric # define _PATH_LOADAVG "/proc/loadavg" 82166301Seric # endif 82266301Seric 82366301Seric int 82464295Seric getla() 82564295Seric { 82666319Seric double avenrun; 82766301Seric register int result; 82866319Seric FILE *fp; 82966301Seric 83066319Seric fp = fopen(_PATH_LOADAVG, "r"); 83166319Seric if (fp == NULL) 83266301Seric { 83366319Seric if (tTd(3, 1)) 83466319Seric printf("getla: fopen(%s): %s\n", 83566319Seric _PATH_LOADAVG, errstring(errno)); 83666319Seric return -1; 83766301Seric } 83866301Seric result = fscanf(fp, "%lf", &avenrun); 83966319Seric fclose(fp); 84066301Seric if (result != 1) 84166301Seric { 84266310Seric if (tTd(3, 1)) 84366301Seric printf("getla: fscanf() = %d: %s\n", 84466301Seric result, errstring(errno)); 84566301Seric return -1; 84666301Seric } 84766301Seric 84857736Seric if (tTd(3, 1)) 84966301Seric printf("getla(): %.2f\n", avenrun); 85066301Seric 85166301Seric return ((int) (avenrun + 0.5)); 85266301Seric } 85366301Seric 85466301Seric #else 85566301Seric 85666301Seric getla() 85766301Seric { 85866301Seric if (tTd(3, 1)) 85957736Seric printf("getla: ZERO\n"); 86051920Seric return (0); 86151773Seric } 86251773Seric 86351773Seric #endif 86451773Seric #endif 86564295Seric #endif 86666301Seric #endif 86766045Seric 86866045Seric 86966045Seric /* 87066045Seric * Copyright 1989 Massachusetts Institute of Technology 87166045Seric * 87266045Seric * Permission to use, copy, modify, distribute, and sell this software and its 87366045Seric * documentation for any purpose is hereby granted without fee, provided that 87466045Seric * the above copyright notice appear in all copies and that both that 87566045Seric * copyright notice and this permission notice appear in supporting 87666045Seric * documentation, and that the name of M.I.T. not be used in advertising or 87766045Seric * publicity pertaining to distribution of the software without specific, 87866045Seric * written prior permission. M.I.T. makes no representations about the 87966045Seric * suitability of this software for any purpose. It is provided "as is" 88066045Seric * without express or implied warranty. 88166045Seric * 88266045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 88366045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 88466045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 88566045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 88666045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 88766045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 88866045Seric * 88966045Seric * Authors: Many and varied... 89066045Seric */ 89166045Seric 89266045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 89366045Seric #ifndef lint 89466045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 89566045Seric #endif /* !lint */ 89666045Seric 89766179Seric #ifdef apollo 89866045Seric # undef volatile 89966045Seric # include <apollo/base.h> 90066045Seric 90166045Seric /* ARGSUSED */ 90266045Seric int getloadavg( call_data ) 90366045Seric caddr_t call_data; /* pointer to (double) return value */ 90466045Seric { 90566045Seric double *avenrun = (double *) call_data; 90666045Seric int i; 90766045Seric status_$t st; 90866045Seric long loadav[3]; 90966045Seric proc1_$get_loadav(loadav, &st); 91066045Seric *avenrun = loadav[0] / (double) (1 << 16); 91166045Seric return(0); 91266045Seric } 91366045Seric # endif /* apollo */ 91424943Seric /* 91524943Seric ** SHOULDQUEUE -- should this message be queued or sent? 91624943Seric ** 91724943Seric ** Compares the message cost to the load average to decide. 91824943Seric ** 91924943Seric ** Parameters: 92024943Seric ** pri -- the priority of the message in question. 92157438Seric ** ctime -- the message creation time. 92224943Seric ** 92324943Seric ** Returns: 92424943Seric ** TRUE -- if this message should be queued up for the 92524943Seric ** time being. 92624943Seric ** FALSE -- if the load is low enough to send this message. 92724943Seric ** 92824943Seric ** Side Effects: 92924943Seric ** none. 93024943Seric */ 93124943Seric 93224943Seric bool 93357438Seric shouldqueue(pri, ctime) 93424943Seric long pri; 93557438Seric time_t ctime; 93624943Seric { 93751920Seric if (CurrentLA < QueueLA) 93824943Seric return (FALSE); 93958132Seric if (CurrentLA >= RefuseLA) 94058132Seric return (TRUE); 941*68457Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 94224943Seric } 94324943Seric /* 94453037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 94553037Seric ** 94653037Seric ** Parameters: 94753037Seric ** none. 94853037Seric ** 94953037Seric ** Returns: 95053037Seric ** TRUE if incoming SMTP connections should be refused 95153037Seric ** (for now). 95253037Seric ** FALSE if we should accept new work. 95353037Seric ** 95453037Seric ** Side Effects: 95553037Seric ** none. 95653037Seric */ 95753037Seric 95853037Seric bool 95953037Seric refuseconnections() 96053037Seric { 96159156Seric #ifdef XLA 96259156Seric if (!xla_smtp_ok()) 96359156Seric return TRUE; 96459156Seric #endif 96559156Seric 96653037Seric /* this is probably too simplistic */ 967*68457Seric return (CurrentLA >= RefuseLA); 96853037Seric } 96953037Seric /* 97024943Seric ** SETPROCTITLE -- set process title for ps 97124943Seric ** 97224943Seric ** Parameters: 97358674Seric ** fmt -- a printf style format string. 97458674Seric ** a, b, c -- possible parameters to fmt. 97524943Seric ** 97624943Seric ** Returns: 97724943Seric ** none. 97824943Seric ** 97924943Seric ** Side Effects: 98024943Seric ** Clobbers argv of our main procedure so ps(1) will 98124943Seric ** display the title. 98224943Seric */ 98324943Seric 984*68457Seric #ifdef SETPROCTITLE 985*68457Seric # ifdef HASSETPROCTITLE 986*68457Seric *** ERROR *** Cannot have both SETPROCTITLE and HASSETPROCTITLE defined 987*68457Seric # endif 988*68457Seric # ifdef __hpux 98958689Seric # include <sys/pstat.h> 99058689Seric # endif 991*68457Seric # ifdef BSD4_4 99259732Seric # include <machine/vmparam.h> 99359732Seric # include <sys/exec.h> 994*68457Seric # ifdef __bsdi__ 995*68457Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 996*68457Seric # define PROCTITLEPAD '\0' 99764247Seric # endif 998*68457Seric # ifdef PS_STRINGS 999*68457Seric # define SETPROC_STATIC static 1000*68457Seric # endif 100159732Seric # endif 1002*68457Seric # ifndef SETPROC_STATIC 100359732Seric # define SETPROC_STATIC 100459732Seric # endif 1005*68457Seric #endif 100667626Seric 1007*68457Seric #ifndef PROCTITLEPAD 1008*68457Seric # define PROCTITLEPAD ' ' 1009*68457Seric #endif 101058689Seric 1011*68457Seric #ifndef HASSETPROCTITLE 101264843Seric 101324943Seric /*VARARGS1*/ 1014*68457Seric #ifdef __STDC__ 101557642Seric setproctitle(char *fmt, ...) 1016*68457Seric #else 101757642Seric setproctitle(fmt, va_alist) 101824943Seric char *fmt; 101957642Seric va_dcl 1020*68457Seric #endif 102124943Seric { 1022*68457Seric # ifdef SETPROCTITLE 102324943Seric register char *p; 102425049Seric register int i; 102559732Seric SETPROC_STATIC char buf[MAXLINE]; 102656852Seric VA_LOCAL_DECL 1027*68457Seric # ifdef __hpux 102858689Seric union pstun pst; 102958689Seric # endif 103024943Seric extern char **Argv; 103124943Seric extern char *LastArgv; 103224943Seric 103358674Seric p = buf; 103424943Seric 103558674Seric /* print sendmail: heading for grep */ 103658674Seric (void) strcpy(p, "sendmail: "); 103758674Seric p += strlen(p); 103824943Seric 103958674Seric /* print the argument string */ 104058674Seric VA_START(fmt); 104158674Seric (void) vsprintf(p, fmt, ap); 104256852Seric VA_END; 104354996Seric 104458674Seric i = strlen(buf); 104558689Seric 1046*68457Seric # ifdef __hpux 104758689Seric pst.pst_command = buf; 104858689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 104958689Seric # else 1050*68457Seric # ifdef PS_STRINGS 105159732Seric PS_STRINGS->ps_nargvstr = 1; 105259732Seric PS_STRINGS->ps_argvstr = buf; 105359732Seric # else 105454996Seric if (i > LastArgv - Argv[0] - 2) 105525049Seric { 105654996Seric i = LastArgv - Argv[0] - 2; 105758674Seric buf[i] = '\0'; 105825049Seric } 105958674Seric (void) strcpy(Argv[0], buf); 106054997Seric p = &Argv[0][i]; 106124943Seric while (p < LastArgv) 1062*68457Seric *p++ = PROCTITLEPAD; 1063*68457Seric # endif 1064*68457Seric # endif 1065*68457Seric # endif /* SETPROCTITLE */ 106624943Seric } 106765982Seric 1068*68457Seric #endif 106925698Seric /* 107025698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 107125698Seric ** 107225698Seric ** Parameters: 1073*68457Seric ** none. 107425698Seric ** 107525698Seric ** Returns: 107625698Seric ** none. 107725698Seric ** 107825698Seric ** Side Effects: 107925698Seric ** Picks up extant zombies. 108025698Seric */ 108125698Seric 108246928Sbostic void 1083*68457Seric reapchild() 108425698Seric { 108564738Seric int olderrno = errno; 108663966Seric # ifdef HASWAITPID 108760219Seric auto int status; 108860560Seric int count; 108960560Seric int pid; 109060219Seric 109160560Seric count = 0; 109260560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 109360560Seric { 109460560Seric if (count++ > 1000) 109560560Seric { 109666748Seric #ifdef LOG 109760560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 109860560Seric pid, status); 109966748Seric #endif 110060560Seric break; 110160560Seric } 110260560Seric } 110360219Seric # else 110425698Seric # ifdef WNOHANG 110525698Seric union wait status; 110625698Seric 110763753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 110825698Seric continue; 110956795Seric # else /* WNOHANG */ 111025698Seric auto int status; 111125698Seric 111260219Seric while (wait(&status) > 0) 111325698Seric continue; 111456795Seric # endif /* WNOHANG */ 111560219Seric # endif 111664035Seric # ifdef SYS5SIGNALS 111764035Seric (void) setsignal(SIGCHLD, reapchild); 111858061Seric # endif 111964738Seric errno = olderrno; 112025698Seric } 112155418Seric /* 112255418Seric ** UNSETENV -- remove a variable from the environment 112355418Seric ** 112455418Seric ** Not needed on newer systems. 112555418Seric ** 112655418Seric ** Parameters: 112755418Seric ** name -- the string name of the environment variable to be 112855418Seric ** deleted from the current environment. 112955418Seric ** 113055418Seric ** Returns: 113155418Seric ** none. 113255418Seric ** 113355418Seric ** Globals: 113455418Seric ** environ -- a pointer to the current environment. 113555418Seric ** 113655418Seric ** Side Effects: 113755418Seric ** Modifies environ. 113855418Seric */ 113955418Seric 114063962Seric #ifndef HASUNSETENV 114155418Seric 114255418Seric void 114355418Seric unsetenv(name) 114455418Seric char *name; 114555418Seric { 114655418Seric extern char **environ; 114755418Seric register char **pp; 114855418Seric int len = strlen(name); 114955418Seric 115055418Seric for (pp = environ; *pp != NULL; pp++) 115155418Seric { 115255418Seric if (strncmp(name, *pp, len) == 0 && 115355418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 115455418Seric break; 115555418Seric } 115655418Seric 115755418Seric for (; *pp != NULL; pp++) 115855418Seric *pp = pp[1]; 115955418Seric } 116055418Seric 116163962Seric #endif 116256215Seric /* 116356215Seric ** GETDTABLESIZE -- return number of file descriptors 116456215Seric ** 116556215Seric ** Only on non-BSD systems 116656215Seric ** 116756215Seric ** Parameters: 116856215Seric ** none 116956215Seric ** 117056215Seric ** Returns: 117156215Seric ** size of file descriptor table 117256215Seric ** 117356215Seric ** Side Effects: 117456215Seric ** none 117556215Seric */ 117656215Seric 117763787Seric #ifdef SOLARIS 117863787Seric # include <sys/resource.h> 117963787Seric #endif 118056215Seric 118156215Seric int 118263787Seric getdtsize() 118356215Seric { 118463787Seric #ifdef RLIMIT_NOFILE 118563787Seric struct rlimit rl; 118663787Seric 118763787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 118863787Seric return rl.rlim_cur; 118963787Seric #endif 119063787Seric 119164031Seric # ifdef HASGETDTABLESIZE 119264031Seric return getdtablesize(); 119364031Seric # else 119464031Seric # ifdef _SC_OPEN_MAX 119558689Seric return sysconf(_SC_OPEN_MAX); 119663787Seric # else 119756215Seric return NOFILE; 119863787Seric # endif 119958689Seric # endif 120056215Seric } 120157631Seric /* 120257631Seric ** UNAME -- get the UUCP name of this system. 120357631Seric */ 120457631Seric 120557943Seric #ifndef HASUNAME 120657631Seric 120757631Seric int 120857631Seric uname(name) 120957631Seric struct utsname *name; 121057631Seric { 121157631Seric FILE *file; 121257631Seric char *n; 121357631Seric 121457631Seric name->nodename[0] = '\0'; 121557631Seric 121657661Seric /* try /etc/whoami -- one line with the node name */ 121757631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 121857631Seric { 121957661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 122057631Seric (void) fclose(file); 122157661Seric n = strchr(name->nodename, '\n'); 122257631Seric if (n != NULL) 122357631Seric *n = '\0'; 122457631Seric if (name->nodename[0] != '\0') 122557631Seric return (0); 122657631Seric } 122757631Seric 122857661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 122957631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 123057631Seric { 123157631Seric char buf[MAXLINE]; 123257631Seric 123357631Seric while (fgets(buf, MAXLINE, file) != NULL) 123457631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 123557631Seric NODE_LENGTH, name->nodename) > 0) 123657631Seric break; 123757631Seric (void) fclose(file); 123857631Seric if (name->nodename[0] != '\0') 123957631Seric return (0); 124057631Seric } 124157631Seric 124257631Seric #ifdef TRUST_POPEN 124357631Seric /* 124457631Seric ** Popen is known to have security holes. 124557631Seric */ 124657631Seric 124757661Seric /* try uuname -l to return local name */ 124857631Seric if ((file = popen("uuname -l", "r")) != NULL) 124957631Seric { 125057661Seric (void) fgets(name, NODE_LENGTH + 1, file); 125157631Seric (void) pclose(file); 125257661Seric n = strchr(name, '\n'); 125357631Seric if (n != NULL) 125457631Seric *n = '\0'; 125557661Seric if (name->nodename[0] != '\0') 125657631Seric return (0); 125757631Seric } 125857631Seric #endif 125957631Seric 126057631Seric return (-1); 126157631Seric } 126257943Seric #endif /* HASUNAME */ 126358068Seric /* 126458068Seric ** INITGROUPS -- initialize groups 126558068Seric ** 126658068Seric ** Stub implementation for System V style systems 126758068Seric */ 126858068Seric 126958068Seric #ifndef HASINITGROUPS 127058068Seric 127158068Seric initgroups(name, basegid) 127258068Seric char *name; 127358068Seric int basegid; 127458068Seric { 127558068Seric return 0; 127658068Seric } 127758068Seric 127858068Seric #endif 127958082Seric /* 128059289Seric ** SETSID -- set session id (for non-POSIX systems) 128159289Seric */ 128259289Seric 128359289Seric #ifndef HASSETSID 128459289Seric 128563753Seric pid_t 128663753Seric setsid __P ((void)) 128759289Seric { 128864155Seric #ifdef TIOCNOTTY 128964155Seric int fd; 129064155Seric 129166743Seric fd = open("/dev/tty", O_RDWR, 0); 129264155Seric if (fd >= 0) 129364155Seric { 129464155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 129564155Seric (void) close(fd); 129664155Seric } 129764155Seric #endif /* TIOCNOTTY */ 129864705Seric # ifdef SYS5SETPGRP 129963753Seric return setpgrp(); 130063753Seric # else 130164155Seric return setpgid(0, getpid()); 130259289Seric # endif 130359289Seric } 130459289Seric 130559289Seric #endif 130659289Seric /* 130766748Seric ** FSYNC -- dummy fsync 130866748Seric */ 130966748Seric 131066748Seric #ifdef NEEDFSYNC 131166748Seric 131266748Seric fsync(fd) 131366748Seric int fd; 131466748Seric { 131566748Seric # ifdef O_SYNC 131666748Seric return fcntl(fd, F_SETFL, O_SYNC); 131766748Seric # else 131866748Seric /* nothing we can do */ 131966748Seric return 0; 132066748Seric # endif 132166748Seric } 132266748Seric 132366748Seric #endif 132466748Seric /* 132564718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 132664718Seric ** 132764718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 1328*68457Seric ** instead of a long. This patches things. 132964718Seric */ 133064718Seric 1331*68457Seric #ifdef DGUX 133264718Seric 133364718Seric #undef inet_addr 133464718Seric 133564718Seric long 133664718Seric dgux_inet_addr(host) 133764718Seric char *host; 133864718Seric { 133964718Seric struct in_addr haddr; 134064718Seric 134164718Seric haddr = inet_addr(host); 134264718Seric return haddr.s_addr; 134364718Seric } 134464718Seric 134564718Seric #endif 134664718Seric /* 134763969Seric ** GETOPT -- for old systems or systems with bogus implementations 134863969Seric */ 134963969Seric 135063969Seric #ifdef NEEDGETOPT 135163969Seric 135263969Seric /* 135363969Seric * Copyright (c) 1985 Regents of the University of California. 135463969Seric * All rights reserved. The Berkeley software License Agreement 135563969Seric * specifies the terms and conditions for redistribution. 135663969Seric */ 135763969Seric 135863969Seric 135963969Seric /* 136063969Seric ** this version hacked to add `atend' flag to allow state machine 136163969Seric ** to reset if invoked by the program to scan args for a 2nd time 136263969Seric */ 136363969Seric 136463969Seric #if defined(LIBC_SCCS) && !defined(lint) 136563969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 136664072Seric #endif /* LIBC_SCCS and not lint */ 136763969Seric 136863969Seric #include <stdio.h> 136963969Seric 137063969Seric /* 137163969Seric * get option letter from argument vector 137263969Seric */ 137365352Seric #ifdef _CONVEX_SOURCE 137465352Seric extern int optind, opterr; 137565352Seric #else 137665352Seric int opterr = 1; /* if error message should be printed */ 137765352Seric int optind = 1; /* index into parent argv vector */ 137865352Seric #endif 137965352Seric int optopt; /* character checked for validity */ 138063969Seric char *optarg; /* argument associated with option */ 138163969Seric 138263969Seric #define BADCH (int)'?' 138363969Seric #define EMSG "" 138463969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 138563969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 138663969Seric 138763969Seric getopt(nargc,nargv,ostr) 138865160Seric int nargc; 138965160Seric char *const *nargv; 139065160Seric const char *ostr; 139163969Seric { 139263969Seric static char *place = EMSG; /* option letter processing */ 139363969Seric static char atend = 0; 139463969Seric register char *oli; /* option letter list index */ 139563969Seric 139663969Seric if (atend) { 139763969Seric atend = 0; 139863969Seric place = EMSG; 139963969Seric } 140063969Seric if(!*place) { /* update scanning pointer */ 140164136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 140263969Seric atend++; 140363969Seric return(EOF); 140463969Seric } 140563969Seric if (*place == '-') { /* found "--" */ 140663969Seric ++optind; 140763969Seric atend++; 140863969Seric return(EOF); 140963969Seric } 141063969Seric } /* option letter okay? */ 141164125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 141264136Seric if (!*place) ++optind; 141363969Seric tell(": illegal option -- "); 141463969Seric } 141563969Seric if (*++oli != ':') { /* don't need argument */ 141663969Seric optarg = NULL; 141763969Seric if (!*place) ++optind; 141863969Seric } 141963969Seric else { /* need an argument */ 142063969Seric if (*place) optarg = place; /* no white space */ 142163969Seric else if (nargc <= ++optind) { /* no arg */ 142263969Seric place = EMSG; 142363969Seric tell(": option requires an argument -- "); 142463969Seric } 142563969Seric else optarg = nargv[optind]; /* white space */ 142663969Seric place = EMSG; 142763969Seric ++optind; 142863969Seric } 142963969Seric return(optopt); /* dump back option letter */ 143063969Seric } 143163969Seric 143263969Seric #endif 143363969Seric /* 143463969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 143563969Seric */ 143663969Seric 143763969Seric #ifdef NEEDVPRINTF 143863969Seric 143963969Seric #define MAXARG 16 144063969Seric 144163969Seric vfprintf(fp, fmt, ap) 144263969Seric FILE * fp; 144363969Seric char * fmt; 144463969Seric char ** ap; 144563969Seric { 144663969Seric char * bp[MAXARG]; 144763969Seric int i = 0; 144863969Seric 144963969Seric while (*ap && i < MAXARG) 145063969Seric bp[i++] = *ap++; 145163969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 145263969Seric bp[4], bp[5], bp[6], bp[7], 145363969Seric bp[8], bp[9], bp[10], bp[11], 145463969Seric bp[12], bp[13], bp[14], bp[15]); 145563969Seric } 145663969Seric 145763969Seric vsprintf(s, fmt, ap) 145863969Seric char * s; 145963969Seric char * fmt; 146063969Seric char ** ap; 146163969Seric { 146263969Seric char * bp[MAXARG]; 146363969Seric int i = 0; 146463969Seric 146563969Seric while (*ap && i < MAXARG) 146663969Seric bp[i++] = *ap++; 146763969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 146863969Seric bp[4], bp[5], bp[6], bp[7], 146963969Seric bp[8], bp[9], bp[10], bp[11], 147063969Seric bp[12], bp[13], bp[14], bp[15]); 147163969Seric } 147263969Seric 147363969Seric #endif 147463969Seric /* 147565220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 147665208Seric ** 147765211Seric ** Parameters: 147865211Seric ** shell -- the user's shell from /etc/passwd 147965211Seric ** 148065211Seric ** Returns: 148165211Seric ** TRUE -- if it is ok to use this for unrestricted access. 148265211Seric ** FALSE -- if the shell is restricted. 148365208Seric */ 148465208Seric 148565587Seric #if !HASGETUSERSHELL 148665587Seric 148765587Seric # ifndef _PATH_SHELLS 148865587Seric # define _PATH_SHELLS "/etc/shells" 148965587Seric # endif 149065587Seric 149165587Seric char *DefaultUserShells[] = 149265587Seric { 1493*68457Seric "/bin/sh", 149465587Seric "/usr/bin/sh", 1495*68457Seric "/bin/csh", 149665587Seric "/usr/bin/csh", 149765587Seric #ifdef __hpux 1498*68457Seric "/bin/rsh", 1499*68457Seric "/bin/ksh", 1500*68457Seric "/bin/rksh", 150165587Seric "/bin/pam", 1502*68457Seric "/usr/bin/keysh", 150365587Seric "/bin/posix/sh", 150465211Seric #endif 150565587Seric NULL 150665587Seric }; 150765587Seric 150865208Seric #endif 150965208Seric 151065747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 151165747Seric 151265211Seric bool 151365211Seric usershellok(shell) 151465211Seric char *shell; 151565208Seric { 151665211Seric #if HASGETUSERSHELL 151765211Seric register char *p; 151865211Seric extern char *getusershell(); 151965208Seric 1520*68457Seric if (shell == NULL || shell[0] == '\0') 1521*68457Seric return TRUE; 1522*68457Seric 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 1533*68457Seric if (shell == NULL || shell[0] == '\0') 1534*68457Seric return TRUE; 1535*68457Seric 153665211Seric shellf = fopen(_PATH_SHELLS, "r"); 153765211Seric if (shellf == NULL) 153865211Seric { 153965211Seric /* no /etc/shells; see if it is one of the std shells */ 154065587Seric char **d; 154165587Seric 154265587Seric for (d = DefaultUserShells; *d != NULL; d++) 154365587Seric { 154465587Seric if (strcmp(shell, *d) == 0) 154565587Seric return TRUE; 154665587Seric } 154765587Seric return FALSE; 154865211Seric } 154965208Seric 155065211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 155165211Seric { 155265211Seric register char *p, *q; 155365208Seric 155465211Seric p = buf; 155565211Seric while (*p != '\0' && *p != '#' && *p != '/') 155665211Seric p++; 155765211Seric if (*p == '#' || *p == '\0') 155865208Seric continue; 155965211Seric q = p; 156065211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 156165211Seric p++; 156265211Seric *p = '\0'; 156365747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 156465211Seric { 156565211Seric fclose(shellf); 156665211Seric return TRUE; 156765211Seric } 156865208Seric } 156965211Seric fclose(shellf); 157065211Seric return FALSE; 157165211Seric #endif 157265208Seric } 157365208Seric /* 157464311Seric ** FREESPACE -- see how much free space is on the queue filesystem 157558082Seric ** 157658082Seric ** Only implemented if you have statfs. 157758082Seric ** 157858082Seric ** Parameters: 157964311Seric ** dir -- the directory in question. 158064311Seric ** bsize -- a variable into which the filesystem 158164311Seric ** block size is stored. 158258082Seric ** 158358082Seric ** Returns: 158464311Seric ** The number of bytes free on the queue filesystem. 158564311Seric ** -1 if the statfs call fails. 158664311Seric ** 158764311Seric ** Side effects: 158864311Seric ** Puts the filesystem block size into bsize. 158958082Seric */ 159058082Seric 159165749Seric /* statfs types */ 159265749Seric #define SFS_NONE 0 /* no statfs implementation */ 159365749Seric #define SFS_USTAT 1 /* use ustat */ 159465749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 159565749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 159665749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 159765749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 159866752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 159965749Seric 160065749Seric #ifndef SFS_TYPE 160165749Seric # define SFS_TYPE SFS_NONE 160258157Seric #endif 160358157Seric 160465749Seric #if SFS_TYPE == SFS_USTAT 160558157Seric # include <ustat.h> 160658157Seric #endif 160765749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 160865749Seric # include <sys/statfs.h> 160958082Seric #endif 161065749Seric #if SFS_TYPE == SFS_VFS 161165749Seric # include <sys/vfs.h> 161265749Seric #endif 161365749Seric #if SFS_TYPE == SFS_MOUNT 161465749Seric # include <sys/mount.h> 161565749Seric #endif 161666752Seric #if SFS_TYPE == SFS_STATVFS 161766752Seric # include <sys/statvfs.h> 161866752Seric #endif 161958082Seric 162064311Seric long 162164311Seric freespace(dir, bsize) 162264311Seric char *dir; 162364311Seric long *bsize; 162458082Seric { 162565749Seric #if SFS_TYPE != SFS_NONE 162665749Seric # if SFS_TYPE == SFS_USTAT 162758153Seric struct ustat fs; 162858153Seric struct stat statbuf; 162958366Seric # define FSBLOCKSIZE DEV_BSIZE 1630*68457Seric # define f_bavail f_tfree 163158157Seric # else 163258157Seric # if defined(ultrix) 163358157Seric struct fs_data fs; 1634*68457Seric # define f_bavail fd_bfreen 1635*68457Seric # define FSBLOCKSIZE fs.fd_bsize 163658153Seric # else 163766752Seric # if SFS_TYPE == SFS_STATVFS 163866752Seric struct statvfs fs; 1639*68457Seric # define FSBLOCKSIZE fs.f_bsize 164066752Seric # else 164158082Seric struct statfs fs; 164266752Seric # define FSBLOCKSIZE fs.f_bsize 1643*68457Seric # if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) 1644*68457Seric # define f_bavail f_bfree 1645*68457Seric # endif 164663987Seric # endif 164758153Seric # endif 164858133Seric # endif 1649*68457Seric extern int errno; 165058082Seric 165165749Seric # if SFS_TYPE == SFS_USTAT 165264311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 165358157Seric # else 165465749Seric # if SFS_TYPE == SFS_4ARGS 165564311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 165658157Seric # else 1657*68457Seric # if defined(ultrix) 1658*68457Seric if (statfs(dir, &fs) > 0) 165967159Seric # else 166064311Seric if (statfs(dir, &fs) == 0) 166158153Seric # endif 166258133Seric # endif 166358133Seric # endif 166458133Seric { 166564311Seric if (bsize != NULL) 166664311Seric *bsize = FSBLOCKSIZE; 1667*68457Seric return (fs.f_bavail); 166864311Seric } 166964337Seric #endif 167064311Seric return (-1); 167164311Seric } 167264311Seric /* 167364311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 167464311Seric ** 167564311Seric ** Only implemented if you have statfs. 167664311Seric ** 167764311Seric ** Parameters: 167864311Seric ** msize -- the size to check against. If zero, we don't yet 167964311Seric ** know how big the message will be, so just check for 168064311Seric ** a "reasonable" amount. 168164311Seric ** 168264311Seric ** Returns: 168364311Seric ** TRUE if there is enough space. 168464311Seric ** FALSE otherwise. 168564311Seric */ 168664311Seric 168764311Seric bool 168864311Seric enoughspace(msize) 168964311Seric long msize; 169064311Seric { 169164311Seric long bfree, bsize; 169264311Seric 169364311Seric if (MinBlocksFree <= 0 && msize <= 0) 169464311Seric { 169558133Seric if (tTd(4, 80)) 169664311Seric printf("enoughspace: no threshold\n"); 169764311Seric return TRUE; 169864311Seric } 169964311Seric 170064311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 170164311Seric { 170264311Seric if (tTd(4, 80)) 170358333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 170464311Seric bfree, msize); 170558333Seric 170658333Seric /* convert msize to block count */ 170764311Seric msize = msize / bsize + 1; 170858333Seric if (MinBlocksFree >= 0) 170958333Seric msize += MinBlocksFree; 171058333Seric 171164311Seric if (bfree < msize) 171258090Seric { 171358090Seric #ifdef LOG 171458090Seric if (LogLevel > 0) 171564254Seric syslog(LOG_ALERT, 171664254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 1717*68457Seric CurEnv->e_id, bfree, 1718*68457Seric CurHostName, msize, QueueDir); 171958090Seric #endif 172058082Seric return FALSE; 172158090Seric } 172258082Seric } 172358082Seric else if (tTd(4, 80)) 172458333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 172558333Seric MinBlocksFree, msize, errstring(errno)); 172658082Seric return TRUE; 172758082Seric } 172858542Seric /* 172958542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 173058542Seric ** 173158542Seric ** This looks at an errno value and tells if this is likely to 173258542Seric ** go away if retried later. 173358542Seric ** 173458542Seric ** Parameters: 173558542Seric ** err -- the errno code to classify. 173658542Seric ** 173758542Seric ** Returns: 173858542Seric ** TRUE if this is probably transient. 173958542Seric ** FALSE otherwise. 174058542Seric */ 174158542Seric 174258542Seric bool 174358542Seric transienterror(err) 174458542Seric int err; 174558542Seric { 174658542Seric switch (err) 174758542Seric { 174858542Seric case EIO: /* I/O error */ 174958542Seric case ENXIO: /* Device not configured */ 175058542Seric case EAGAIN: /* Resource temporarily unavailable */ 175158542Seric case ENOMEM: /* Cannot allocate memory */ 175258542Seric case ENODEV: /* Operation not supported by device */ 175358542Seric case ENFILE: /* Too many open files in system */ 175458542Seric case EMFILE: /* Too many open files */ 175558542Seric case ENOSPC: /* No space left on device */ 175658542Seric #ifdef ETIMEDOUT 175758542Seric case ETIMEDOUT: /* Connection timed out */ 175858542Seric #endif 175958542Seric #ifdef ESTALE 176058542Seric case ESTALE: /* Stale NFS file handle */ 176158542Seric #endif 176258542Seric #ifdef ENETDOWN 176358542Seric case ENETDOWN: /* Network is down */ 176458542Seric #endif 176558542Seric #ifdef ENETUNREACH 176658542Seric case ENETUNREACH: /* Network is unreachable */ 176758542Seric #endif 176858542Seric #ifdef ENETRESET 176958542Seric case ENETRESET: /* Network dropped connection on reset */ 177058542Seric #endif 177158542Seric #ifdef ECONNABORTED 177258542Seric case ECONNABORTED: /* Software caused connection abort */ 177358542Seric #endif 177458542Seric #ifdef ECONNRESET 177558542Seric case ECONNRESET: /* Connection reset by peer */ 177658542Seric #endif 177758542Seric #ifdef ENOBUFS 177858542Seric case ENOBUFS: /* No buffer space available */ 177958542Seric #endif 178058542Seric #ifdef ESHUTDOWN 178158542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 178258542Seric #endif 178358542Seric #ifdef ECONNREFUSED 178458542Seric case ECONNREFUSED: /* Connection refused */ 178558542Seric #endif 178658542Seric #ifdef EHOSTDOWN 178758542Seric case EHOSTDOWN: /* Host is down */ 178858542Seric #endif 178958542Seric #ifdef EHOSTUNREACH 179058542Seric case EHOSTUNREACH: /* No route to host */ 179158542Seric #endif 179258542Seric #ifdef EDQUOT 179358542Seric case EDQUOT: /* Disc quota exceeded */ 179458542Seric #endif 179558542Seric #ifdef EPROCLIM 179658542Seric case EPROCLIM: /* Too many processes */ 179758542Seric #endif 179858542Seric #ifdef EUSERS 179958542Seric case EUSERS: /* Too many users */ 180058542Seric #endif 180158542Seric #ifdef EDEADLK 180258542Seric case EDEADLK: /* Resource deadlock avoided */ 180358542Seric #endif 180458542Seric #ifdef EISCONN 180558542Seric case EISCONN: /* Socket already connected */ 180658542Seric #endif 180758542Seric #ifdef EINPROGRESS 180858542Seric case EINPROGRESS: /* Operation now in progress */ 180958542Seric #endif 181058542Seric #ifdef EALREADY 181158542Seric case EALREADY: /* Operation already in progress */ 181258542Seric #endif 181358542Seric #ifdef EADDRINUSE 181458542Seric case EADDRINUSE: /* Address already in use */ 181558542Seric #endif 181658542Seric #ifdef EADDRNOTAVAIL 181758542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 181858542Seric #endif 181966018Seric #ifdef ETXTBSY 182066017Seric case ETXTBSY: /* (Apollo) file locked */ 182166017Seric #endif 182263834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 182358542Seric case ENOSR: /* Out of streams resources */ 182458542Seric #endif 182558542Seric return TRUE; 182658542Seric } 182758542Seric 182858542Seric /* nope, must be permanent */ 182958542Seric return FALSE; 183058542Seric } 183158689Seric /* 183264035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 183358689Seric ** 183458689Seric ** Parameters: 183558689Seric ** fd -- the file descriptor of the file. 183658689Seric ** filename -- the file name (for error messages). 183764335Seric ** ext -- the filename extension. 183858689Seric ** type -- type of the lock. Bits can be: 183958689Seric ** LOCK_EX -- exclusive lock. 184058689Seric ** LOCK_NB -- non-blocking. 184158689Seric ** 184258689Seric ** Returns: 184358689Seric ** TRUE if the lock was acquired. 184458689Seric ** FALSE otherwise. 184558689Seric */ 184658689Seric 184758689Seric bool 184864335Seric lockfile(fd, filename, ext, type) 184958689Seric int fd; 185058689Seric char *filename; 185164335Seric char *ext; 185258689Seric int type; 185358689Seric { 185465830Seric # if !HASFLOCK 185558689Seric int action; 185658689Seric struct flock lfd; 185764335Seric 185864335Seric if (ext == NULL) 185964335Seric ext = ""; 186064035Seric 186163983Seric bzero(&lfd, sizeof lfd); 186259447Seric if (bitset(LOCK_UN, type)) 186359447Seric lfd.l_type = F_UNLCK; 186459447Seric else if (bitset(LOCK_EX, type)) 186558689Seric lfd.l_type = F_WRLCK; 186658689Seric else 186758689Seric lfd.l_type = F_RDLCK; 186858689Seric 186958689Seric if (bitset(LOCK_NB, type)) 187058689Seric action = F_SETLK; 187158689Seric else 187258689Seric action = F_SETLKW; 187358689Seric 187464264Seric if (tTd(55, 60)) 187564335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 187664335Seric filename, ext, action, lfd.l_type); 187764264Seric 187858689Seric if (fcntl(fd, action, &lfd) >= 0) 187964264Seric { 188064264Seric if (tTd(55, 60)) 188164264Seric printf("SUCCESS\n"); 188258689Seric return TRUE; 188364264Seric } 188458689Seric 188564264Seric if (tTd(55, 60)) 188664264Seric printf("(%s) ", errstring(errno)); 188764264Seric 188864136Seric /* 188964136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 189064136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 189164136Seric ** as type "tmp" (that is, served from swap space), the 189264136Seric ** previous fcntl will fail with "Invalid argument" errors. 189364136Seric ** Since this is fairly common during testing, we will assume 189464136Seric ** that this indicates that the lock is successfully grabbed. 189564136Seric */ 189664136Seric 189764136Seric if (errno == EINVAL) 189864264Seric { 189964264Seric if (tTd(55, 60)) 190064264Seric printf("SUCCESS\n"); 190164136Seric return TRUE; 190264264Seric } 190364136Seric 190458689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 190564378Seric { 190664378Seric int omode = -1; 190764378Seric # ifdef F_GETFL 190864378Seric int oerrno = errno; 190964378Seric 191064378Seric (void) fcntl(fd, F_GETFL, &omode); 191164378Seric errno = oerrno; 191264378Seric # endif 191364378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 191464378Seric filename, ext, fd, type, omode, geteuid()); 191564378Seric } 191658689Seric # else 191764335Seric if (ext == NULL) 191864335Seric ext = ""; 191964335Seric 192064264Seric if (tTd(55, 60)) 192164335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 192264264Seric 192358689Seric if (flock(fd, type) >= 0) 192464264Seric { 192564264Seric if (tTd(55, 60)) 192664264Seric printf("SUCCESS\n"); 192758689Seric return TRUE; 192864264Seric } 192958689Seric 193064264Seric if (tTd(55, 60)) 193164264Seric printf("(%s) ", errstring(errno)); 193264264Seric 193358689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 193464378Seric { 193564378Seric int omode = -1; 193664378Seric # ifdef F_GETFL 193764378Seric int oerrno = errno; 193864378Seric 193964378Seric (void) fcntl(fd, F_GETFL, &omode); 194064378Seric errno = oerrno; 194164378Seric # endif 194264378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 194364378Seric filename, ext, fd, type, omode, geteuid()); 194464386Seric } 194558689Seric # endif 194664264Seric if (tTd(55, 60)) 194764264Seric printf("FAIL\n"); 194858689Seric return FALSE; 194958689Seric } 195064556Seric /* 195165948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 195265948Seric ** 195365948Seric ** Parameters: 195465948Seric ** fd -- the file descriptor to check. 195565948Seric ** 195665948Seric ** Returns: 195765948Seric ** TRUE -- if only root can chown the file to an arbitrary 195865948Seric ** user. 195965948Seric ** FALSE -- if an arbitrary user can give away a file. 196065948Seric */ 196165948Seric 196265948Seric bool 196365948Seric chownsafe(fd) 196465948Seric int fd; 196565948Seric { 196665950Seric #ifdef __hpux 196765948Seric char *s; 196865948Seric int tfd; 196965948Seric uid_t o_uid, o_euid; 197065948Seric gid_t o_gid, o_egid; 197165948Seric bool rval; 197265948Seric struct stat stbuf; 197365948Seric 197465948Seric o_uid = getuid(); 197565948Seric o_euid = geteuid(); 197665948Seric o_gid = getgid(); 197765948Seric o_egid = getegid(); 197865959Seric fstat(fd, &stbuf); 197965948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 198065948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 198165948Seric s = tmpnam(NULL); 198265948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 198365974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 198465948Seric close(tfd); 198565948Seric unlink(s); 198665948Seric setreuid(o_uid, o_euid); 198765948Seric setresgid(o_gid, o_egid, -1); 198865948Seric return rval; 198965948Seric #else 199066088Seric # ifdef _POSIX_CHOWN_RESTRICTED 199166088Seric # if _POSIX_CHOWN_RESTRICTED == -1 199266088Seric return FALSE; 199366088Seric # else 199466088Seric return TRUE; 199566088Seric # endif 199666088Seric # else 199766088Seric # ifdef _PC_CHOWN_RESTRICTED 1998*68457Seric return fpathconf(fd, _PC_CHOWN_RESTRICTED) > 0; 1999*68457Seric # else 2000*68457Seric # ifdef BSD 200167447Seric return TRUE; 2002*68457Seric # else 200365948Seric return FALSE; 2004*68457Seric # endif 200565948Seric # endif 200665948Seric # endif 200765948Seric #endif 200865948Seric } 200965948Seric /* 201064556Seric ** GETCFNAME -- return the name of the .cf file. 201164556Seric ** 201264556Seric ** Some systems (e.g., NeXT) determine this dynamically. 201364556Seric */ 201464556Seric 201564556Seric char * 201664556Seric getcfname() 201764556Seric { 201864556Seric if (ConfFile != NULL) 201964556Seric return ConfFile; 202065214Seric #ifdef NETINFO 202165214Seric { 202265214Seric extern char *ni_propval(); 202365214Seric char *cflocation; 202465214Seric 2025*68457Seric cflocation = ni_propval("/locations/sendmail", "sendmail.cf"); 202665214Seric if (cflocation != NULL) 202765214Seric return cflocation; 202865214Seric } 202965214Seric #endif 203064556Seric return _PATH_SENDMAILCF; 203164556Seric } 203264718Seric /* 203364718Seric ** SETVENDOR -- process vendor code from V configuration line 203464718Seric ** 203564718Seric ** Parameters: 203664718Seric ** vendor -- string representation of vendor. 203764718Seric ** 203864718Seric ** Returns: 203964718Seric ** TRUE -- if ok. 204064718Seric ** FALSE -- if vendor code could not be processed. 204164926Seric ** 204264926Seric ** Side Effects: 204364926Seric ** It is reasonable to set mode flags here to tweak 204464926Seric ** processing in other parts of the code if necessary. 204564926Seric ** For example, if you are a vendor that uses $%y to 204664926Seric ** indicate YP lookups, you could enable that here. 204764718Seric */ 204864718Seric 204964718Seric bool 205064718Seric setvendor(vendor) 205164718Seric char *vendor; 205264718Seric { 205364926Seric if (strcasecmp(vendor, "Berkeley") == 0) 205464926Seric return TRUE; 205564926Seric 205664926Seric /* add vendor extensions here */ 205764926Seric 205864926Seric return FALSE; 205964718Seric } 206064816Seric /* 206164816Seric ** STRTOL -- convert string to long integer 206264816Seric ** 206364816Seric ** For systems that don't have it in the C library. 206466161Seric ** 206566161Seric ** This is taken verbatim from the 4.4-Lite C library. 206664816Seric */ 206764816Seric 206864816Seric #ifdef NEEDSTRTOL 206964816Seric 207066161Seric #if defined(LIBC_SCCS) && !defined(lint) 207166161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 207266161Seric #endif /* LIBC_SCCS and not lint */ 207366161Seric 207466161Seric #include <limits.h> 207566161Seric 207666161Seric /* 207766161Seric * Convert a string to a long integer. 207866161Seric * 207966161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 208066161Seric * alphabets and digits are each contiguous. 208166161Seric */ 208266161Seric 208364816Seric long 208466161Seric strtol(nptr, endptr, base) 208566161Seric const char *nptr; 208666161Seric char **endptr; 208766161Seric register int base; 208864816Seric { 208966161Seric register const char *s = nptr; 209066161Seric register unsigned long acc; 209166161Seric register int c; 209266161Seric register unsigned long cutoff; 209366161Seric register int neg = 0, any, cutlim; 209464816Seric 209566161Seric /* 209666161Seric * Skip white space and pick up leading +/- sign if any. 209766161Seric * If base is 0, allow 0x for hex and 0 for octal, else 209866161Seric * assume decimal; if base is already 16, allow 0x. 209966161Seric */ 210066161Seric do { 210166161Seric c = *s++; 210266161Seric } while (isspace(c)); 210366161Seric if (c == '-') { 210466161Seric neg = 1; 210566161Seric c = *s++; 210666161Seric } else if (c == '+') 210766161Seric c = *s++; 210866161Seric if ((base == 0 || base == 16) && 210966161Seric c == '0' && (*s == 'x' || *s == 'X')) { 211066161Seric c = s[1]; 211166161Seric s += 2; 211266161Seric base = 16; 211366161Seric } 211466161Seric if (base == 0) 211566161Seric base = c == '0' ? 8 : 10; 211664816Seric 211766161Seric /* 211866161Seric * Compute the cutoff value between legal numbers and illegal 211966161Seric * numbers. That is the largest legal value, divided by the 212066161Seric * base. An input number that is greater than this value, if 212166161Seric * followed by a legal input character, is too big. One that 212266161Seric * is equal to this value may be valid or not; the limit 212366161Seric * between valid and invalid numbers is then based on the last 212466161Seric * digit. For instance, if the range for longs is 212566161Seric * [-2147483648..2147483647] and the input base is 10, 212666161Seric * cutoff will be set to 214748364 and cutlim to either 212766161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 212866161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 212966161Seric * the number is too big, and we will return a range error. 213066161Seric * 213166161Seric * Set any if any `digits' consumed; make it negative to indicate 213266161Seric * overflow. 213366161Seric */ 213466161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 213566161Seric cutlim = cutoff % (unsigned long)base; 213666161Seric cutoff /= (unsigned long)base; 213766161Seric for (acc = 0, any = 0;; c = *s++) { 213866161Seric if (isdigit(c)) 213966161Seric c -= '0'; 214066161Seric else if (isalpha(c)) 214166161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 214266161Seric else 214366161Seric break; 214466161Seric if (c >= base) 214566161Seric break; 214666161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 214766161Seric any = -1; 214866161Seric else { 214966161Seric any = 1; 215066161Seric acc *= base; 215166161Seric acc += c; 215264816Seric } 215364816Seric } 215466161Seric if (any < 0) { 215566161Seric acc = neg ? LONG_MIN : LONG_MAX; 215666161Seric errno = ERANGE; 215766161Seric } else if (neg) 215866161Seric acc = -acc; 215966161Seric if (endptr != 0) 216066161Seric *endptr = (char *)(any ? s - 1 : nptr); 216166161Seric return (acc); 216264816Seric } 216364816Seric 216464816Seric #endif 216564841Seric /* 216664841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 216764841Seric ** 2168*68457Seric ** Solaris versions prior through 2.3 don't properly deliver a 216964841Seric ** canonical h_name field. This tries to work around it. 217064841Seric */ 217164841Seric 217264841Seric #ifdef SOLARIS 217364841Seric 217464841Seric struct hostent * 217564841Seric solaris_gethostbyname(name) 217665094Seric const char *name; 217764841Seric { 217864841Seric # ifdef SOLARIS_2_3 217964841Seric static struct hostent hp; 218064841Seric static char buf[1000]; 218164841Seric extern struct hostent *_switch_gethostbyname_r(); 218264841Seric 218364841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 218464841Seric # else 218565009Seric extern struct hostent *__switch_gethostbyname(); 218665009Seric 218764841Seric return __switch_gethostbyname(name); 218864841Seric # endif 218964841Seric } 219064841Seric 219164841Seric struct hostent * 219264841Seric solaris_gethostbyaddr(addr, len, type) 219365094Seric const char *addr; 219464841Seric int len; 219564841Seric int type; 219664841Seric { 219764841Seric # ifdef SOLARIS_2_3 219864841Seric static struct hostent hp; 219964841Seric static char buf[1000]; 220064841Seric extern struct hostent *_switch_gethostbyaddr_r(); 220164841Seric 220264841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 220364841Seric # else 220465009Seric extern struct hostent *__switch_gethostbyaddr(); 220565009Seric 220664841Seric return __switch_gethostbyaddr(addr, len, type); 220764841Seric # endif 220864841Seric } 220964841Seric 221064841Seric #endif 221165214Seric /* 221265214Seric ** NI_PROPVAL -- netinfo property value lookup routine 221365214Seric ** 221465214Seric ** Parameters: 2215*68457Seric ** directory -- the Netinfo directory name. 2216*68457Seric ** propname -- the Netinfo property name. 221765214Seric ** 221865214Seric ** Returns: 221965214Seric ** NULL -- if: 222065214Seric ** 1. the directory is not found 222165214Seric ** 2. the property name is not found 222265214Seric ** 3. the property contains multiple values 222365214Seric ** 4. some error occured 222465214Seric ** else -- the location of the config file. 222565214Seric ** 222665214Seric ** Notes: 222765214Seric ** Caller should free the return value of ni_proval 222865214Seric */ 222965214Seric 223065214Seric #ifdef NETINFO 223165214Seric 223265214Seric # include <netinfo/ni.h> 223365214Seric 223465214Seric # define LOCAL_NETINFO_DOMAIN "." 223565214Seric # define PARENT_NETINFO_DOMAIN ".." 223665214Seric # define MAX_NI_LEVELS 256 223765214Seric 223865214Seric char * 2239*68457Seric ni_propval(directory, propname) 2240*68457Seric char *directory; 2241*68457Seric char *propname; 224265214Seric { 224365820Seric char *propval = NULL; 224465214Seric int i; 224565214Seric void *ni = NULL; 224665214Seric void *lastni = NULL; 224765214Seric ni_status nis; 224865214Seric ni_id nid; 224965214Seric ni_namelist ninl; 225065214Seric 225165214Seric /* 225265214Seric ** If the passed directory and property name are found 225365214Seric ** in one of netinfo domains we need to search (starting 225465214Seric ** from the local domain moving all the way back to the 225565214Seric ** root domain) set propval to the property's value 225665214Seric ** and return it. 225765214Seric */ 225865214Seric 225965214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 226065214Seric { 226165214Seric if (i == 0) 226265214Seric { 226365214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 226465214Seric } 226565214Seric else 226665214Seric { 226765214Seric if (lastni != NULL) 226865214Seric ni_free(lastni); 226965214Seric lastni = ni; 227065214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 227165214Seric } 227265214Seric 227365214Seric /* 227465214Seric ** Don't bother if we didn't get a handle on a 227565214Seric ** proper domain. This is not necessarily an error. 227665214Seric ** We would get a positive ni_status if, for instance 227765214Seric ** we never found the directory or property and tried 227865214Seric ** to open the parent of the root domain! 227965214Seric */ 228065214Seric 228165214Seric if (nis != 0) 228265214Seric break; 228365214Seric 228465214Seric /* 228565214Seric ** Find the path to the server information. 228665214Seric */ 228765214Seric 2288*68457Seric if (ni_pathsearch(ni, &nid, directory) != 0) 228965214Seric continue; 229065214Seric 229165214Seric /* 2292*68457Seric ** Find "host" information. 229365214Seric */ 229465214Seric 2295*68457Seric if (ni_lookupprop(ni, &nid, propname, &ninl) != 0) 229665214Seric continue; 229765214Seric 229865214Seric /* 2299*68457Seric ** If there's only one name in 2300*68457Seric ** the list, assume we've got 2301*68457Seric ** what we want. 230265214Seric */ 230365214Seric 2304*68457Seric if (ninl.ni_namelist_len == 1) 230565214Seric { 2306*68457Seric propval = ni_name_dup(ninl.ni_namelist_val[0]); 2307*68457Seric break; 230865214Seric } 230965214Seric } 231065214Seric 231165214Seric /* 231265214Seric ** Clean up. 231365214Seric */ 231465214Seric 231565214Seric if (ni != NULL) 231665214Seric ni_free(ni); 231765214Seric if (lastni != NULL && ni != lastni) 231865214Seric ni_free(lastni); 231965214Seric 232065214Seric return propval; 232165214Seric } 232265214Seric 232365214Seric #endif /* NETINFO */ 232465504Seric /* 232565504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 232665504Seric ** 232765504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 232865504Seric ** syslog succeeds during interrupt handlers. 232965504Seric */ 233065504Seric 233165504Seric #ifdef __hpux 233265504Seric 233365504Seric # define MAXSYSLOGTRIES 100 233465504Seric # undef syslog 233565504Seric 233665504Seric # ifdef __STDC__ 233765504Seric hard_syslog(int pri, char *msg, ...) 233865504Seric # else 233965504Seric hard_syslog(pri, msg, va_alist) 234065504Seric int pri; 234165504Seric char *msg; 234265504Seric va_dcl 234365504Seric # endif 234465504Seric { 234565504Seric int i; 234665504Seric char buf[SYSLOG_BUFSIZE * 2]; 234765504Seric VA_LOCAL_DECL; 234865504Seric 234965504Seric VA_START(msg); 235065504Seric vsprintf(buf, msg, ap); 235165504Seric VA_END; 235265504Seric 235365504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 235465504Seric continue; 235565504Seric } 235665504Seric 235765504Seric #endif 2358