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*66743Seric static char sccsid[] = "@(#)conf.c 8.83 (Berkeley) 04/12/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, 12458082Seric "goaway", PRIV_GOAWAY, 12558789Seric NULL, 0, 12658082Seric }; 12758082Seric 12858082Seric 12958082Seric 13058082Seric /* 13124943Seric ** Miscellaneous stuff. 1329039Seric */ 1339039Seric 13424943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13564644Seric 13664644Seric 13764644Seric /* 13864644Seric ** Following should be config parameters (and probably will be in 13964644Seric ** future releases). In the meantime, setting these is considered 14064644Seric ** unsupported, and is intentionally undocumented. 14164644Seric */ 14264644Seric 14364644Seric #ifdef BROKENSMTPPEERS 14464644Seric bool BrokenSmtpPeers = TRUE; /* set if you have broken SMTP peers */ 14564644Seric #else 14664644Seric bool BrokenSmtpPeers = FALSE; /* set if you have broken SMTP peers */ 14764644Seric #endif 14864644Seric #ifdef NOLOOPBACKCHECK 14964644Seric bool CheckLoopBack = FALSE; /* set to check HELO loopback */ 15064644Seric #else 15164644Seric bool CheckLoopBack = TRUE; /* set to check HELO loopback */ 15264644Seric #endif 15364644Seric 15424943Seric /* 15524943Seric ** SETDEFAULTS -- set default values 15624943Seric ** 15724943Seric ** Because of the way freezing is done, these must be initialized 15824943Seric ** using direct code. 15924943Seric ** 16024943Seric ** Parameters: 16158734Seric ** e -- the default envelope. 16224943Seric ** 16324943Seric ** Returns: 16424943Seric ** none. 16524943Seric ** 16624943Seric ** Side Effects: 16724943Seric ** Initializes a bunch of global variables to their 16824943Seric ** default values. 16924943Seric */ 17024943Seric 17158737Seric #define DAYS * 24 * 60 * 60 17258737Seric 17358734Seric setdefaults(e) 17458734Seric register ENVELOPE *e; 17524943Seric { 17657438Seric SpaceSub = ' '; /* option B */ 17757438Seric QueueLA = 8; /* option x */ 17857438Seric RefuseLA = 12; /* option X */ 17957438Seric WkRecipFact = 30000L; /* option y */ 18057438Seric WkClassFact = 1800L; /* option z */ 18157438Seric WkTimeFact = 90000L; /* option Z */ 18257438Seric QueueFactor = WkRecipFact * 20; /* option q */ 18363787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 18457438Seric /* option F */ 18557438Seric DefUid = 1; /* option u */ 18657438Seric DefGid = 1; /* option g */ 18757438Seric CheckpointInterval = 10; /* option C */ 18857438Seric MaxHopCount = 25; /* option h */ 18958734Seric e->e_sendmode = SM_FORK; /* option d */ 19058734Seric e->e_errormode = EM_PRINT; /* option e */ 19159709Seric SevenBit = FALSE; /* option 7 */ 19257438Seric MaxMciCache = 1; /* option k */ 19357438Seric MciCacheTimeout = 300; /* option K */ 19457438Seric LogLevel = 9; /* option L */ 19558112Seric settimeouts(NULL); /* option r */ 19658737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 19758737Seric TimeOuts.to_q_warning = 0; /* option T */ 19858853Seric PrivacyFlags = 0; /* option p */ 19940973Sbostic setdefuser(); 20053654Seric setupmaps(); 20157402Seric setupmailers(); 20224943Seric } 203294Seric 20440973Sbostic 2054326Seric /* 20640973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20740973Sbostic */ 20840973Sbostic 20940973Sbostic setdefuser() 21040973Sbostic { 21140973Sbostic struct passwd *defpwent; 21257386Seric static char defuserbuf[40]; 21340973Sbostic 21457386Seric DefUser = defuserbuf; 21540973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 21657386Seric strcpy(defuserbuf, defpwent->pw_name); 21740973Sbostic else 21857386Seric strcpy(defuserbuf, "nobody"); 21940973Sbostic } 22053654Seric /* 22156836Seric ** HOST_MAP_INIT -- initialize host class structures 22256836Seric */ 22356836Seric 22456836Seric bool 22560219Seric host_map_init(map, args) 22656836Seric MAP *map; 22756836Seric char *args; 22856836Seric { 22956836Seric register char *p = args; 23056836Seric 23156836Seric for (;;) 23256836Seric { 23358050Seric while (isascii(*p) && isspace(*p)) 23456836Seric p++; 23556836Seric if (*p != '-') 23656836Seric break; 23756836Seric switch (*++p) 23856836Seric { 23956836Seric case 'a': 24056836Seric map->map_app = ++p; 24156836Seric break; 24256836Seric } 24358050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 24456836Seric p++; 24556836Seric if (*p != '\0') 24656836Seric *p++ = '\0'; 24756836Seric } 24856836Seric if (map->map_app != NULL) 24956836Seric map->map_app = newstr(map->map_app); 25056836Seric return TRUE; 25156836Seric } 25257402Seric /* 25357402Seric ** SETUPMAILERS -- initialize default mailers 25457402Seric */ 25556836Seric 25657402Seric setupmailers() 25757402Seric { 25857402Seric char buf[100]; 25957402Seric 26057403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 26157403Seric makemailer(buf); 26257403Seric 26359883Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE"); 26457402Seric makemailer(buf); 26557402Seric 26657402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 26757402Seric makemailer(buf); 26857402Seric } 26956836Seric /* 27060207Seric ** SETUPMAPS -- set up map classes 27160207Seric */ 27260207Seric 27360207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 27460207Seric { \ 27560207Seric extern bool parse __P((MAP *, char *)); \ 27660207Seric extern bool open __P((MAP *, int)); \ 27760207Seric extern void close __P((MAP *)); \ 27860207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 27960207Seric extern void store __P((MAP *, char *, char *)); \ 28060207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 28160207Seric s->s_mapclass.map_cname = name; \ 28260207Seric s->s_mapclass.map_ext = ext; \ 28360207Seric s->s_mapclass.map_cflags = flags; \ 28460207Seric s->s_mapclass.map_parse = parse; \ 28560207Seric s->s_mapclass.map_open = open; \ 28660207Seric s->s_mapclass.map_close = close; \ 28760207Seric s->s_mapclass.map_lookup = lookup; \ 28860207Seric s->s_mapclass.map_store = store; \ 28960207Seric } 29060207Seric 29160207Seric setupmaps() 29260207Seric { 29360207Seric register STAB *s; 29460207Seric 29560207Seric #ifdef NEWDB 29660207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29760207Seric map_parseargs, hash_map_open, db_map_close, 29860207Seric db_map_lookup, db_map_store); 29960207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 30060207Seric map_parseargs, bt_map_open, db_map_close, 30160207Seric db_map_lookup, db_map_store); 30260207Seric #endif 30360207Seric 30460207Seric #ifdef NDBM 30560207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 30660207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 30760207Seric ndbm_map_lookup, ndbm_map_store); 30860207Seric #endif 30960207Seric 31060207Seric #ifdef NIS 31160207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 31260207Seric map_parseargs, nis_map_open, nis_map_close, 31360207Seric nis_map_lookup, nis_map_store); 31460207Seric #endif 31560207Seric 31660207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 31760207Seric map_parseargs, stab_map_open, stab_map_close, 31860207Seric stab_map_lookup, stab_map_store); 31960207Seric 32060207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 32160207Seric map_parseargs, impl_map_open, impl_map_close, 32260207Seric impl_map_lookup, impl_map_store); 32360207Seric 32460207Seric /* host DNS lookup */ 32560207Seric MAPDEF("host", NULL, 0, 32660207Seric host_map_init, null_map_open, null_map_close, 32760207Seric host_map_lookup, null_map_store); 32860207Seric 32960207Seric /* dequote map */ 33060207Seric MAPDEF("dequote", NULL, 0, 33160207Seric dequote_init, null_map_open, null_map_close, 33260207Seric dequote_map, null_map_store); 33360207Seric 33460207Seric #if 0 33560207Seric # ifdef USERDB 33660207Seric /* user database */ 33760207Seric MAPDEF("udb", ".db", 0, 33860207Seric udb_map_parse, null_map_open, null_map_close, 33960207Seric udb_map_lookup, null_map_store); 34060207Seric # endif 34160207Seric #endif 34260207Seric } 34360207Seric 34460207Seric #undef MAPDEF 34560207Seric /* 3469369Seric ** USERNAME -- return the user id of the logged in user. 3479369Seric ** 3489369Seric ** Parameters: 3499369Seric ** none. 3509369Seric ** 3519369Seric ** Returns: 3529369Seric ** The login name of the logged in user. 3539369Seric ** 3549369Seric ** Side Effects: 3559369Seric ** none. 3569369Seric ** 3579369Seric ** Notes: 3589369Seric ** The return value is statically allocated. 3599369Seric */ 3609369Seric 3619369Seric char * 3629369Seric username() 3639369Seric { 36417469Seric static char *myname = NULL; 3659369Seric extern char *getlogin(); 36619904Smiriam register struct passwd *pw; 3679369Seric 36817469Seric /* cache the result */ 36917469Seric if (myname == NULL) 37017469Seric { 37117469Seric myname = getlogin(); 37217469Seric if (myname == NULL || myname[0] == '\0') 37317469Seric { 37463787Seric pw = getpwuid(RealUid); 37517469Seric if (pw != NULL) 37640993Sbostic myname = newstr(pw->pw_name); 37717469Seric } 37819904Smiriam else 37919904Smiriam { 38063787Seric uid_t uid = RealUid; 38119873Smiriam 38240993Sbostic myname = newstr(myname); 38340993Sbostic if ((pw = getpwnam(myname)) == NULL || 38458736Seric (uid != 0 && uid != pw->pw_uid)) 38519904Smiriam { 38658736Seric pw = getpwuid(uid); 38724945Seric if (pw != NULL) 38840993Sbostic myname = newstr(pw->pw_name); 38919873Smiriam } 39019873Smiriam } 39117469Seric if (myname == NULL || myname[0] == '\0') 39217469Seric { 39358151Seric syserr("554 Who are you?"); 39417469Seric myname = "postmaster"; 39517469Seric } 39617469Seric } 39717469Seric 39817469Seric return (myname); 3999369Seric } 4009369Seric /* 4014190Seric ** TTYPATH -- Get the path of the user's tty 402294Seric ** 403294Seric ** Returns the pathname of the user's tty. Returns NULL if 404294Seric ** the user is not logged in or if s/he has write permission 405294Seric ** denied. 406294Seric ** 407294Seric ** Parameters: 408294Seric ** none 409294Seric ** 410294Seric ** Returns: 411294Seric ** pathname of the user's tty. 412294Seric ** NULL if not logged in or write permission denied. 413294Seric ** 414294Seric ** Side Effects: 415294Seric ** none. 416294Seric ** 417294Seric ** WARNING: 418294Seric ** Return value is in a local buffer. 419294Seric ** 420294Seric ** Called By: 421294Seric ** savemail 422294Seric */ 423294Seric 424294Seric char * 425294Seric ttypath() 426294Seric { 427294Seric struct stat stbuf; 428294Seric register char *pathn; 429294Seric extern char *ttyname(); 4304081Seric extern char *getlogin(); 431294Seric 432294Seric /* compute the pathname of the controlling tty */ 4339369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4349369Seric (pathn = ttyname(0)) == NULL) 435294Seric { 436294Seric errno = 0; 437294Seric return (NULL); 438294Seric } 439294Seric 440294Seric /* see if we have write permission */ 4412967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 442294Seric { 443294Seric errno = 0; 444294Seric return (NULL); 445294Seric } 446294Seric 447294Seric /* see if the user is logged in */ 448294Seric if (getlogin() == NULL) 449294Seric return (NULL); 450294Seric 451294Seric /* looks good */ 452294Seric return (pathn); 453294Seric } 4542967Seric /* 4552967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4562967Seric ** 4572967Seric ** This routine can be supplied on a per-installation basis 4582967Seric ** to determine whether a person is allowed to send a message. 4592967Seric ** This allows restriction of certain types of internet 4602967Seric ** forwarding or registration of users. 4612967Seric ** 4622967Seric ** If the hosts are found to be incompatible, an error 46357454Seric ** message should be given using "usrerr" and 0 should 4642967Seric ** be returned. 4652967Seric ** 4664288Seric ** 'NoReturn' can be set to suppress the return-to-sender 4674288Seric ** function; this should be done on huge messages. 4684288Seric ** 4692967Seric ** Parameters: 4702967Seric ** to -- the person being sent to. 4712967Seric ** 4722967Seric ** Returns: 47357459Seric ** an exit status 4742967Seric ** 4752967Seric ** Side Effects: 4762967Seric ** none (unless you include the usrerr stuff) 4772967Seric */ 4782967Seric 47955012Seric checkcompat(to, e) 4802967Seric register ADDRESS *to; 48155012Seric register ENVELOPE *e; 4822967Seric { 48312133Seric # ifdef lint 48412133Seric if (to == NULL) 48512133Seric to++; 48663834Seric # endif /* lint */ 48764402Seric 48864402Seric if (tTd(49, 1)) 48964402Seric printf("checkcompat(to=%s, from=%s)\n", 49064402Seric to->q_paddr, e->e_from.q_paddr); 49164402Seric 49210698Seric # ifdef EXAMPLE_CODE 49310698Seric /* this code is intended as an example only */ 4944437Seric register STAB *s; 4954437Seric 4964437Seric s = stab("arpa", ST_MAILER, ST_FIND); 49755012Seric if (s != NULL && e->e_from.q_mailer != LocalMailer && 4989369Seric to->q_mailer == s->s_mailer) 4994437Seric { 50058151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 50110698Seric /* NoReturn = TRUE; to supress return copy */ 50257459Seric return (EX_UNAVAILABLE); 5034437Seric } 50456795Seric # endif /* EXAMPLE_CODE */ 50557459Seric return (EX_OK); 5062967Seric } 5079369Seric /* 50864035Seric ** SETSIGNAL -- set a signal handler 50964035Seric ** 51064035Seric ** This is essentially old BSD "signal(3)". 51164035Seric */ 51264035Seric 51364561Seric sigfunc_t 51464035Seric setsignal(sig, handler) 51564035Seric int sig; 51664561Seric sigfunc_t handler; 51764035Seric { 51864380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 51964035Seric return signal(sig, handler); 52064035Seric #else 52164035Seric struct sigaction n, o; 52264035Seric 52364035Seric bzero(&n, sizeof n); 52464035Seric n.sa_handler = handler; 52564035Seric if (sigaction(sig, &n, &o) < 0) 52664035Seric return SIG_ERR; 52764035Seric return o.sa_handler; 52864035Seric #endif 52964035Seric } 53064035Seric /* 5319369Seric ** HOLDSIGS -- arrange to hold all signals 5329369Seric ** 5339369Seric ** Parameters: 5349369Seric ** none. 5359369Seric ** 5369369Seric ** Returns: 5379369Seric ** none. 5389369Seric ** 5399369Seric ** Side Effects: 5409369Seric ** Arranges that signals are held. 5419369Seric */ 5429369Seric 5439369Seric holdsigs() 5449369Seric { 5459369Seric } 5469369Seric /* 5479369Seric ** RLSESIGS -- arrange to release all signals 5489369Seric ** 5499369Seric ** This undoes the effect of holdsigs. 5509369Seric ** 5519369Seric ** Parameters: 5529369Seric ** none. 5539369Seric ** 5549369Seric ** Returns: 5559369Seric ** none. 5569369Seric ** 5579369Seric ** Side Effects: 5589369Seric ** Arranges that signals are released. 5599369Seric */ 5609369Seric 5619369Seric rlsesigs() 5629369Seric { 5639369Seric } 56414872Seric /* 56564705Seric ** INIT_MD -- do machine dependent initializations 56664705Seric ** 56764705Seric ** Systems that have global modes that should be set should do 56864705Seric ** them here rather than in main. 56964705Seric */ 57064705Seric 57164705Seric #ifdef _AUX_SOURCE 57264705Seric # include <compat.h> 57364705Seric #endif 57464705Seric 57564825Seric init_md(argc, argv) 57664825Seric int argc; 57764825Seric char **argv; 57864705Seric { 57964705Seric #ifdef _AUX_SOURCE 58064705Seric setcompat(getcompat() | COMPAT_BSDPROT); 58164705Seric #endif 58264705Seric } 58364705Seric /* 58414872Seric ** GETLA -- get the current load average 58514872Seric ** 58614881Seric ** This code stolen from la.c. 58714881Seric ** 58814872Seric ** Parameters: 58914872Seric ** none. 59014872Seric ** 59114872Seric ** Returns: 59214872Seric ** The current load average as an integer. 59314872Seric ** 59414872Seric ** Side Effects: 59514872Seric ** none. 59614872Seric */ 59714872Seric 59851920Seric /* try to guess what style of load average we have */ 59951920Seric #define LA_ZERO 1 /* always return load average as zero */ 60064360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 60151920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 60251920Seric #define LA_SUBR 4 /* call getloadavg */ 60364295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 60464360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 60566301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 60614872Seric 60763962Seric /* do guesses based on general OS type */ 60851920Seric #ifndef LA_TYPE 60963962Seric # define LA_TYPE LA_ZERO 61051920Seric #endif 61151920Seric 61264360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 61351920Seric 61414872Seric #include <nlist.h> 61514872Seric 61651920Seric #ifndef LA_AVENRUN 61763962Seric # ifdef SYSTEM5 61863962Seric # define LA_AVENRUN "avenrun" 61963962Seric # else 62063962Seric # define LA_AVENRUN "_avenrun" 62163962Seric # endif 62251920Seric #endif 62351920Seric 62451920Seric /* _PATH_UNIX should be defined in <paths.h> */ 62551920Seric #ifndef _PATH_UNIX 62663962Seric # if defined(SYSTEM5) 62763962Seric # define _PATH_UNIX "/unix" 62863962Seric # else 62963962Seric # define _PATH_UNIX "/vmunix" 63063962Seric # endif 63151920Seric #endif 63251920Seric 63314872Seric struct nlist Nl[] = 63414872Seric { 63551920Seric { LA_AVENRUN }, 63614872Seric #define X_AVENRUN 0 63714872Seric { 0 }, 63814872Seric }; 63914872Seric 64060583Seric #ifndef FSHIFT 64160583Seric # if defined(unixpc) 64260583Seric # define FSHIFT 5 64360583Seric # endif 64457736Seric 64560583Seric # if defined(__alpha) 64660583Seric # define FSHIFT 10 64760583Seric # endif 64857977Seric 64964360Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 65051920Seric # define FSHIFT 8 65160583Seric # endif 65257736Seric #endif 65360583Seric 65464360Seric #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE) 65551920Seric # define FSCALE (1 << FSHIFT) 65651920Seric #endif 65740930Srick 65814872Seric getla() 65914872Seric { 66014872Seric static int kmem = -1; 66151920Seric #if LA_TYPE == LA_INT 66224943Seric long avenrun[3]; 66351920Seric #else 66464360Seric # if LA_TYPE == LA_SHORT 66564360Seric short avenrun[3]; 66664360Seric # else 66751920Seric double avenrun[3]; 66864360Seric # endif 66951920Seric #endif 67025615Seric extern off_t lseek(); 67157736Seric extern int errno; 67214872Seric 67314872Seric if (kmem < 0) 67414872Seric { 67524945Seric kmem = open("/dev/kmem", 0, 0); 67614872Seric if (kmem < 0) 67757736Seric { 67857736Seric if (tTd(3, 1)) 67957736Seric printf("getla: open(/dev/kmem): %s\n", 68057736Seric errstring(errno)); 68114872Seric return (-1); 68257736Seric } 68351920Seric (void) fcntl(kmem, F_SETFD, 1); 68457736Seric if (nlist(_PATH_UNIX, Nl) < 0) 68557736Seric { 68657736Seric if (tTd(3, 1)) 68757736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 68857736Seric errstring(errno)); 68914872Seric return (-1); 69057736Seric } 69159253Seric if (Nl[X_AVENRUN].n_value == 0) 69259253Seric { 69359253Seric if (tTd(3, 1)) 69459253Seric printf("getla: nlist(%s, %s) ==> 0\n", 69559253Seric _PATH_UNIX, LA_AVENRUN); 69659253Seric return (-1); 69759253Seric } 69814872Seric } 69957736Seric if (tTd(3, 20)) 70057736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 70124945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 70223118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 70319967Seric { 70419967Seric /* thank you Ian */ 70557736Seric if (tTd(3, 1)) 70657736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 70719967Seric return (-1); 70819967Seric } 70964360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 71057736Seric if (tTd(3, 5)) 71157736Seric { 71257736Seric printf("getla: avenrun = %d", avenrun[0]); 71357736Seric if (tTd(3, 15)) 71457736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 71557736Seric printf("\n"); 71657736Seric } 71757736Seric if (tTd(3, 1)) 71857736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 71924943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 72051920Seric #else 72157736Seric if (tTd(3, 5)) 72257736Seric { 72357736Seric printf("getla: avenrun = %g", avenrun[0]); 72457736Seric if (tTd(3, 15)) 72557736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 72657736Seric printf("\n"); 72757736Seric } 72857736Seric if (tTd(3, 1)) 72957736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 73051920Seric return ((int) (avenrun[0] + 0.5)); 73151920Seric #endif 73214872Seric } 73314872Seric 73451773Seric #else 73551920Seric #if LA_TYPE == LA_SUBR 73651773Seric 73764718Seric #ifdef DGUX 73864718Seric 73964718Seric #include <sys/dg_sys_info.h> 74064718Seric 74164718Seric int getla() 74264718Seric { 74364718Seric struct dg_sys_info_load_info load_info; 74464718Seric 74564718Seric dg_sys_info((long *)&load_info, 74664718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 74764718Seric 74864718Seric return((int) (load_info.one_minute + 0.5)); 74964718Seric } 75064718Seric 75164718Seric #else 75264718Seric 75351773Seric getla() 75451773Seric { 75551920Seric double avenrun[3]; 75651920Seric 75751920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 75857736Seric { 75957736Seric if (tTd(3, 1)) 76057736Seric perror("getla: getloadavg failed:"); 76151920Seric return (-1); 76257736Seric } 76357736Seric if (tTd(3, 1)) 76457736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 76551920Seric return ((int) (avenrun[0] + 0.5)); 76651773Seric } 76751773Seric 76864718Seric #endif /* DGUX */ 76951773Seric #else 77064295Seric #if LA_TYPE == LA_MACH 77151773Seric 77264295Seric /* 77365173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 77464295Seric */ 77564295Seric 77665173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 77765173Seric # include <mach/mach.h> 77865173Seric #else 77965173Seric # include <mach.h> 78065173Seric #endif 78164295Seric 78251773Seric getla() 78351773Seric { 78464295Seric processor_set_t default_set; 78564295Seric kern_return_t error; 78664295Seric unsigned int info_count; 78764295Seric struct processor_set_basic_info info; 78864295Seric host_t host; 78964295Seric 79064295Seric error = processor_set_default(host_self(), &default_set); 79164295Seric if (error != KERN_SUCCESS) 79264295Seric return -1; 79364295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 79464295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 79564295Seric &host, (processor_set_info_t)&info, 79664295Seric &info_count) != KERN_SUCCESS) 79764295Seric { 79864295Seric return -1; 79964295Seric } 80064295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 80164295Seric } 80264295Seric 80364295Seric 80464295Seric #else 80566313Seric #if LA_TYPE == LA_PROCSTR 80664295Seric 80766301Seric /* 80866301Seric ** Read /proc/loadavg for the load average. This is assumed to be 80966301Seric ** in a format like "0.15 0.12 0.06". 81066301Seric ** 81166301Seric ** Initially intended for Linux. This has been in the kernel 81266301Seric ** since at least 0.99.15. 81366301Seric */ 81466301Seric 81566301Seric # ifndef _PATH_LOADAVG 81666301Seric # define _PATH_LOADAVG "/proc/loadavg" 81766301Seric # endif 81866301Seric 81966301Seric int 82064295Seric getla() 82164295Seric { 82266319Seric double avenrun; 82366301Seric register int result; 82466319Seric FILE *fp; 82566301Seric 82666319Seric fp = fopen(_PATH_LOADAVG, "r"); 82766319Seric if (fp == NULL) 82866301Seric { 82966319Seric if (tTd(3, 1)) 83066319Seric printf("getla: fopen(%s): %s\n", 83166319Seric _PATH_LOADAVG, errstring(errno)); 83266319Seric return -1; 83366301Seric } 83466301Seric result = fscanf(fp, "%lf", &avenrun); 83566319Seric fclose(fp); 83666301Seric if (result != 1) 83766301Seric { 83866310Seric if (tTd(3, 1)) 83966301Seric printf("getla: fscanf() = %d: %s\n", 84066301Seric result, errstring(errno)); 84166301Seric return -1; 84266301Seric } 84366301Seric 84457736Seric if (tTd(3, 1)) 84566301Seric printf("getla(): %.2f\n", avenrun); 84666301Seric 84766301Seric return ((int) (avenrun + 0.5)); 84866301Seric } 84966301Seric 85066301Seric #else 85166301Seric 85266301Seric getla() 85366301Seric { 85466301Seric if (tTd(3, 1)) 85557736Seric printf("getla: ZERO\n"); 85651920Seric return (0); 85751773Seric } 85851773Seric 85951773Seric #endif 86051773Seric #endif 86164295Seric #endif 86266301Seric #endif 86366045Seric 86466045Seric 86566045Seric /* 86666045Seric * Copyright 1989 Massachusetts Institute of Technology 86766045Seric * 86866045Seric * Permission to use, copy, modify, distribute, and sell this software and its 86966045Seric * documentation for any purpose is hereby granted without fee, provided that 87066045Seric * the above copyright notice appear in all copies and that both that 87166045Seric * copyright notice and this permission notice appear in supporting 87266045Seric * documentation, and that the name of M.I.T. not be used in advertising or 87366045Seric * publicity pertaining to distribution of the software without specific, 87466045Seric * written prior permission. M.I.T. makes no representations about the 87566045Seric * suitability of this software for any purpose. It is provided "as is" 87666045Seric * without express or implied warranty. 87766045Seric * 87866045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 87966045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 88066045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 88166045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 88266045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 88366045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 88466045Seric * 88566045Seric * Authors: Many and varied... 88666045Seric */ 88766045Seric 88866045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 88966045Seric #ifndef lint 89066045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 89166045Seric #endif /* !lint */ 89266045Seric 89366179Seric #ifdef apollo 89466045Seric # undef volatile 89566045Seric # include <apollo/base.h> 89666045Seric 89766045Seric /* ARGSUSED */ 89866045Seric int getloadavg( call_data ) 89966045Seric caddr_t call_data; /* pointer to (double) return value */ 90066045Seric { 90166045Seric double *avenrun = (double *) call_data; 90266045Seric int i; 90366045Seric status_$t st; 90466045Seric long loadav[3]; 90566045Seric proc1_$get_loadav(loadav, &st); 90666045Seric *avenrun = loadav[0] / (double) (1 << 16); 90766045Seric return(0); 90866045Seric } 90966045Seric # endif /* apollo */ 91024943Seric /* 91124943Seric ** SHOULDQUEUE -- should this message be queued or sent? 91224943Seric ** 91324943Seric ** Compares the message cost to the load average to decide. 91424943Seric ** 91524943Seric ** Parameters: 91624943Seric ** pri -- the priority of the message in question. 91757438Seric ** ctime -- the message creation time. 91824943Seric ** 91924943Seric ** Returns: 92024943Seric ** TRUE -- if this message should be queued up for the 92124943Seric ** time being. 92224943Seric ** FALSE -- if the load is low enough to send this message. 92324943Seric ** 92424943Seric ** Side Effects: 92524943Seric ** none. 92624943Seric */ 92724943Seric 92824943Seric bool 92957438Seric shouldqueue(pri, ctime) 93024943Seric long pri; 93157438Seric time_t ctime; 93224943Seric { 93351920Seric if (CurrentLA < QueueLA) 93424943Seric return (FALSE); 93558132Seric if (CurrentLA >= RefuseLA) 93658132Seric return (TRUE); 93751920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 93824943Seric } 93924943Seric /* 94053037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 94153037Seric ** 94253037Seric ** Parameters: 94353037Seric ** none. 94453037Seric ** 94553037Seric ** Returns: 94653037Seric ** TRUE if incoming SMTP connections should be refused 94753037Seric ** (for now). 94853037Seric ** FALSE if we should accept new work. 94953037Seric ** 95053037Seric ** Side Effects: 95153037Seric ** none. 95253037Seric */ 95353037Seric 95453037Seric bool 95553037Seric refuseconnections() 95653037Seric { 95759156Seric #ifdef XLA 95859156Seric if (!xla_smtp_ok()) 95959156Seric return TRUE; 96059156Seric #endif 96159156Seric 96253037Seric /* this is probably too simplistic */ 96358132Seric return (CurrentLA >= RefuseLA); 96453037Seric } 96553037Seric /* 96624943Seric ** SETPROCTITLE -- set process title for ps 96724943Seric ** 96824943Seric ** Parameters: 96958674Seric ** fmt -- a printf style format string. 97058674Seric ** a, b, c -- possible parameters to fmt. 97124943Seric ** 97224943Seric ** Returns: 97324943Seric ** none. 97424943Seric ** 97524943Seric ** Side Effects: 97624943Seric ** Clobbers argv of our main procedure so ps(1) will 97724943Seric ** display the title. 97824943Seric */ 97924943Seric 98058689Seric #ifdef SETPROCTITLE 98166002Seric # ifdef HASSETPROCTITLE 98266002Seric *** ERROR *** Cannot have both SETPROCTITLE and HASSETPROCTITLE defined 98366002Seric # endif 98458689Seric # ifdef __hpux 98558689Seric # include <sys/pstat.h> 98658689Seric # endif 98759732Seric # ifdef BSD4_4 98859732Seric # include <machine/vmparam.h> 98959732Seric # include <sys/exec.h> 99064247Seric # ifdef __bsdi__ 99164247Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 99264843Seric # define PROCTITLEPAD '\0' 99364247Seric # endif 99459992Seric # ifdef PS_STRINGS 99559992Seric # define SETPROC_STATIC static 99659992Seric # endif 99759732Seric # endif 99859732Seric # ifndef SETPROC_STATIC 99959732Seric # define SETPROC_STATIC 100059732Seric # endif 100158689Seric #endif 100258689Seric 100364843Seric #ifndef PROCTITLEPAD 100464843Seric # define PROCTITLEPAD ' ' 100564843Seric #endif 100664843Seric 100765982Seric #ifndef HASSETPROCTITLE 100865982Seric 100924943Seric /*VARARGS1*/ 101057642Seric #ifdef __STDC__ 101157642Seric setproctitle(char *fmt, ...) 101257642Seric #else 101357642Seric setproctitle(fmt, va_alist) 101424943Seric char *fmt; 101557642Seric va_dcl 101657642Seric #endif 101724943Seric { 101824943Seric # ifdef SETPROCTITLE 101924943Seric register char *p; 102025049Seric register int i; 102159732Seric SETPROC_STATIC char buf[MAXLINE]; 102256852Seric VA_LOCAL_DECL 102358689Seric # ifdef __hpux 102458689Seric union pstun pst; 102558689Seric # endif 102624943Seric extern char **Argv; 102724943Seric extern char *LastArgv; 102824943Seric 102958674Seric p = buf; 103024943Seric 103158674Seric /* print sendmail: heading for grep */ 103258674Seric (void) strcpy(p, "sendmail: "); 103358674Seric p += strlen(p); 103424943Seric 103558674Seric /* print the argument string */ 103658674Seric VA_START(fmt); 103758674Seric (void) vsprintf(p, fmt, ap); 103856852Seric VA_END; 103954996Seric 104058674Seric i = strlen(buf); 104158689Seric 104258689Seric # ifdef __hpux 104358689Seric pst.pst_command = buf; 104458689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 104558689Seric # else 104659992Seric # ifdef PS_STRINGS 104759732Seric PS_STRINGS->ps_nargvstr = 1; 104859732Seric PS_STRINGS->ps_argvstr = buf; 104959732Seric # else 105054996Seric if (i > LastArgv - Argv[0] - 2) 105125049Seric { 105254996Seric i = LastArgv - Argv[0] - 2; 105358674Seric buf[i] = '\0'; 105425049Seric } 105558674Seric (void) strcpy(Argv[0], buf); 105654997Seric p = &Argv[0][i]; 105724943Seric while (p < LastArgv) 105864843Seric *p++ = PROCTITLEPAD; 105959732Seric # endif 106058689Seric # endif 106156795Seric # endif /* SETPROCTITLE */ 106224943Seric } 106365982Seric 106465982Seric #endif 106525698Seric /* 106625698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 106725698Seric ** 106825698Seric ** Parameters: 106925698Seric ** none. 107025698Seric ** 107125698Seric ** Returns: 107225698Seric ** none. 107325698Seric ** 107425698Seric ** Side Effects: 107525698Seric ** Picks up extant zombies. 107625698Seric */ 107725698Seric 107846928Sbostic void 107925698Seric reapchild() 108025698Seric { 108164738Seric int olderrno = errno; 108263966Seric # ifdef HASWAITPID 108360219Seric auto int status; 108460560Seric int count; 108560560Seric int pid; 108660219Seric 108760560Seric count = 0; 108860560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 108960560Seric { 109060560Seric if (count++ > 1000) 109160560Seric { 109260560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 109360560Seric pid, status); 109460560Seric break; 109560560Seric } 109660560Seric } 109760219Seric # else 109825698Seric # ifdef WNOHANG 109925698Seric union wait status; 110025698Seric 110163753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 110225698Seric continue; 110356795Seric # else /* WNOHANG */ 110425698Seric auto int status; 110525698Seric 110660219Seric while (wait(&status) > 0) 110725698Seric continue; 110856795Seric # endif /* WNOHANG */ 110960219Seric # endif 111064035Seric # ifdef SYS5SIGNALS 111164035Seric (void) setsignal(SIGCHLD, reapchild); 111258061Seric # endif 111364738Seric errno = olderrno; 111425698Seric } 111555418Seric /* 111655418Seric ** UNSETENV -- remove a variable from the environment 111755418Seric ** 111855418Seric ** Not needed on newer systems. 111955418Seric ** 112055418Seric ** Parameters: 112155418Seric ** name -- the string name of the environment variable to be 112255418Seric ** deleted from the current environment. 112355418Seric ** 112455418Seric ** Returns: 112555418Seric ** none. 112655418Seric ** 112755418Seric ** Globals: 112855418Seric ** environ -- a pointer to the current environment. 112955418Seric ** 113055418Seric ** Side Effects: 113155418Seric ** Modifies environ. 113255418Seric */ 113355418Seric 113463962Seric #ifndef HASUNSETENV 113555418Seric 113655418Seric void 113755418Seric unsetenv(name) 113855418Seric char *name; 113955418Seric { 114055418Seric extern char **environ; 114155418Seric register char **pp; 114255418Seric int len = strlen(name); 114355418Seric 114455418Seric for (pp = environ; *pp != NULL; pp++) 114555418Seric { 114655418Seric if (strncmp(name, *pp, len) == 0 && 114755418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 114855418Seric break; 114955418Seric } 115055418Seric 115155418Seric for (; *pp != NULL; pp++) 115255418Seric *pp = pp[1]; 115355418Seric } 115455418Seric 115563962Seric #endif 115656215Seric /* 115756215Seric ** GETDTABLESIZE -- return number of file descriptors 115856215Seric ** 115956215Seric ** Only on non-BSD systems 116056215Seric ** 116156215Seric ** Parameters: 116256215Seric ** none 116356215Seric ** 116456215Seric ** Returns: 116556215Seric ** size of file descriptor table 116656215Seric ** 116756215Seric ** Side Effects: 116856215Seric ** none 116956215Seric */ 117056215Seric 117163787Seric #ifdef SOLARIS 117263787Seric # include <sys/resource.h> 117363787Seric #endif 117456215Seric 117556215Seric int 117663787Seric getdtsize() 117756215Seric { 117863787Seric #ifdef RLIMIT_NOFILE 117963787Seric struct rlimit rl; 118063787Seric 118163787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 118263787Seric return rl.rlim_cur; 118363787Seric #endif 118463787Seric 118564031Seric # ifdef HASGETDTABLESIZE 118664031Seric return getdtablesize(); 118764031Seric # else 118864031Seric # ifdef _SC_OPEN_MAX 118958689Seric return sysconf(_SC_OPEN_MAX); 119063787Seric # else 119156215Seric return NOFILE; 119263787Seric # endif 119358689Seric # endif 119456215Seric } 119557631Seric /* 119657631Seric ** UNAME -- get the UUCP name of this system. 119757631Seric */ 119857631Seric 119957943Seric #ifndef HASUNAME 120057631Seric 120157631Seric int 120257631Seric uname(name) 120357631Seric struct utsname *name; 120457631Seric { 120557631Seric FILE *file; 120657631Seric char *n; 120757631Seric 120857631Seric name->nodename[0] = '\0'; 120957631Seric 121057661Seric /* try /etc/whoami -- one line with the node name */ 121157631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 121257631Seric { 121357661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 121457631Seric (void) fclose(file); 121557661Seric n = strchr(name->nodename, '\n'); 121657631Seric if (n != NULL) 121757631Seric *n = '\0'; 121857631Seric if (name->nodename[0] != '\0') 121957631Seric return (0); 122057631Seric } 122157631Seric 122257661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 122357631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 122457631Seric { 122557631Seric char buf[MAXLINE]; 122657631Seric 122757631Seric while (fgets(buf, MAXLINE, file) != NULL) 122857631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 122957631Seric NODE_LENGTH, name->nodename) > 0) 123057631Seric break; 123157631Seric (void) fclose(file); 123257631Seric if (name->nodename[0] != '\0') 123357631Seric return (0); 123457631Seric } 123557631Seric 123657631Seric #ifdef TRUST_POPEN 123757631Seric /* 123857631Seric ** Popen is known to have security holes. 123957631Seric */ 124057631Seric 124157661Seric /* try uuname -l to return local name */ 124257631Seric if ((file = popen("uuname -l", "r")) != NULL) 124357631Seric { 124457661Seric (void) fgets(name, NODE_LENGTH + 1, file); 124557631Seric (void) pclose(file); 124657661Seric n = strchr(name, '\n'); 124757631Seric if (n != NULL) 124857631Seric *n = '\0'; 124957661Seric if (name->nodename[0] != '\0') 125057631Seric return (0); 125157631Seric } 125257631Seric #endif 125357631Seric 125457631Seric return (-1); 125557631Seric } 125657943Seric #endif /* HASUNAME */ 125758068Seric /* 125858068Seric ** INITGROUPS -- initialize groups 125958068Seric ** 126058068Seric ** Stub implementation for System V style systems 126158068Seric */ 126258068Seric 126358068Seric #ifndef HASINITGROUPS 126458068Seric 126558068Seric initgroups(name, basegid) 126658068Seric char *name; 126758068Seric int basegid; 126858068Seric { 126958068Seric return 0; 127058068Seric } 127158068Seric 127258068Seric #endif 127358082Seric /* 127459289Seric ** SETSID -- set session id (for non-POSIX systems) 127559289Seric */ 127659289Seric 127759289Seric #ifndef HASSETSID 127859289Seric 127963753Seric pid_t 128063753Seric setsid __P ((void)) 128159289Seric { 128264155Seric #ifdef TIOCNOTTY 128364155Seric int fd; 128464155Seric 1285*66743Seric fd = open("/dev/tty", O_RDWR, 0); 128664155Seric if (fd >= 0) 128764155Seric { 128864155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 128964155Seric (void) close(fd); 129064155Seric } 129164155Seric #endif /* TIOCNOTTY */ 129264705Seric # ifdef SYS5SETPGRP 129363753Seric return setpgrp(); 129463753Seric # else 129564155Seric return setpgid(0, getpid()); 129659289Seric # endif 129759289Seric } 129859289Seric 129959289Seric #endif 130059289Seric /* 130164718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 130264718Seric ** 130364718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 130464718Seric ** instead of a long. This patches things. 130564718Seric */ 130664718Seric 130764718Seric #ifdef DGUX 130864718Seric 130964718Seric #undef inet_addr 131064718Seric 131164718Seric long 131264718Seric dgux_inet_addr(host) 131364718Seric char *host; 131464718Seric { 131564718Seric struct in_addr haddr; 131664718Seric 131764718Seric haddr = inet_addr(host); 131864718Seric return haddr.s_addr; 131964718Seric } 132064718Seric 132164718Seric #endif 132264718Seric /* 132363969Seric ** GETOPT -- for old systems or systems with bogus implementations 132463969Seric */ 132563969Seric 132663969Seric #ifdef NEEDGETOPT 132763969Seric 132863969Seric /* 132963969Seric * Copyright (c) 1985 Regents of the University of California. 133063969Seric * All rights reserved. The Berkeley software License Agreement 133163969Seric * specifies the terms and conditions for redistribution. 133263969Seric */ 133363969Seric 133463969Seric 133563969Seric /* 133663969Seric ** this version hacked to add `atend' flag to allow state machine 133763969Seric ** to reset if invoked by the program to scan args for a 2nd time 133863969Seric */ 133963969Seric 134063969Seric #if defined(LIBC_SCCS) && !defined(lint) 134163969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 134264072Seric #endif /* LIBC_SCCS and not lint */ 134363969Seric 134463969Seric #include <stdio.h> 134563969Seric 134663969Seric /* 134763969Seric * get option letter from argument vector 134863969Seric */ 134965352Seric #ifdef _CONVEX_SOURCE 135065352Seric extern int optind, opterr; 135165352Seric #else 135265352Seric int opterr = 1; /* if error message should be printed */ 135365352Seric int optind = 1; /* index into parent argv vector */ 135465352Seric #endif 135565352Seric int optopt; /* character checked for validity */ 135663969Seric char *optarg; /* argument associated with option */ 135763969Seric 135863969Seric #define BADCH (int)'?' 135963969Seric #define EMSG "" 136063969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 136163969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 136263969Seric 136363969Seric getopt(nargc,nargv,ostr) 136465160Seric int nargc; 136565160Seric char *const *nargv; 136665160Seric const char *ostr; 136763969Seric { 136863969Seric static char *place = EMSG; /* option letter processing */ 136963969Seric static char atend = 0; 137063969Seric register char *oli; /* option letter list index */ 137163969Seric 137263969Seric if (atend) { 137363969Seric atend = 0; 137463969Seric place = EMSG; 137563969Seric } 137663969Seric if(!*place) { /* update scanning pointer */ 137764136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 137863969Seric atend++; 137963969Seric return(EOF); 138063969Seric } 138163969Seric if (*place == '-') { /* found "--" */ 138263969Seric ++optind; 138363969Seric atend++; 138463969Seric return(EOF); 138563969Seric } 138663969Seric } /* option letter okay? */ 138764125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 138864136Seric if (!*place) ++optind; 138963969Seric tell(": illegal option -- "); 139063969Seric } 139163969Seric if (*++oli != ':') { /* don't need argument */ 139263969Seric optarg = NULL; 139363969Seric if (!*place) ++optind; 139463969Seric } 139563969Seric else { /* need an argument */ 139663969Seric if (*place) optarg = place; /* no white space */ 139763969Seric else if (nargc <= ++optind) { /* no arg */ 139863969Seric place = EMSG; 139963969Seric tell(": option requires an argument -- "); 140063969Seric } 140163969Seric else optarg = nargv[optind]; /* white space */ 140263969Seric place = EMSG; 140363969Seric ++optind; 140463969Seric } 140563969Seric return(optopt); /* dump back option letter */ 140663969Seric } 140763969Seric 140863969Seric #endif 140963969Seric /* 141063969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 141163969Seric */ 141263969Seric 141363969Seric #ifdef NEEDVPRINTF 141463969Seric 141563969Seric #define MAXARG 16 141663969Seric 141763969Seric vfprintf(fp, fmt, ap) 141863969Seric FILE * fp; 141963969Seric char * fmt; 142063969Seric char ** ap; 142163969Seric { 142263969Seric char * bp[MAXARG]; 142363969Seric int i = 0; 142463969Seric 142563969Seric while (*ap && i < MAXARG) 142663969Seric bp[i++] = *ap++; 142763969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 142863969Seric bp[4], bp[5], bp[6], bp[7], 142963969Seric bp[8], bp[9], bp[10], bp[11], 143063969Seric bp[12], bp[13], bp[14], bp[15]); 143163969Seric } 143263969Seric 143363969Seric vsprintf(s, fmt, ap) 143463969Seric char * s; 143563969Seric char * fmt; 143663969Seric char ** ap; 143763969Seric { 143863969Seric char * bp[MAXARG]; 143963969Seric int i = 0; 144063969Seric 144163969Seric while (*ap && i < MAXARG) 144263969Seric bp[i++] = *ap++; 144363969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 144463969Seric bp[4], bp[5], bp[6], bp[7], 144563969Seric bp[8], bp[9], bp[10], bp[11], 144663969Seric bp[12], bp[13], bp[14], bp[15]); 144763969Seric } 144863969Seric 144963969Seric #endif 145063969Seric /* 145165220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 145265208Seric ** 145365211Seric ** Parameters: 145465211Seric ** shell -- the user's shell from /etc/passwd 145565211Seric ** 145665211Seric ** Returns: 145765211Seric ** TRUE -- if it is ok to use this for unrestricted access. 145865211Seric ** FALSE -- if the shell is restricted. 145965208Seric */ 146065208Seric 146165587Seric #if !HASGETUSERSHELL 146265587Seric 146365587Seric # ifndef _PATH_SHELLS 146465587Seric # define _PATH_SHELLS "/etc/shells" 146565587Seric # endif 146665587Seric 146765587Seric char *DefaultUserShells[] = 146865587Seric { 146965587Seric "/bin/sh", 147065587Seric "/usr/bin/sh", 147165587Seric "/bin/csh", 147265587Seric "/usr/bin/csh", 147365587Seric #ifdef __hpux 147465587Seric "/bin/rsh", 147565587Seric "/bin/ksh", 147665587Seric "/bin/rksh", 147765587Seric "/bin/pam", 147865587Seric "/usr/bin/keysh", 147965587Seric "/bin/posix/sh", 148065211Seric #endif 148165587Seric NULL 148265587Seric }; 148365587Seric 148465208Seric #endif 148565208Seric 148665747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 148765747Seric 148865211Seric bool 148965211Seric usershellok(shell) 149065211Seric char *shell; 149165208Seric { 149265211Seric #if HASGETUSERSHELL 149365211Seric register char *p; 149465211Seric extern char *getusershell(); 149565208Seric 149665211Seric setusershell(); 149765211Seric while ((p = getusershell()) != NULL) 149865747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 149965211Seric break; 150065211Seric endusershell(); 150165211Seric return p != NULL; 150265211Seric #else 150365211Seric register FILE *shellf; 150465211Seric char buf[MAXLINE]; 150565208Seric 150665211Seric shellf = fopen(_PATH_SHELLS, "r"); 150765211Seric if (shellf == NULL) 150865211Seric { 150965211Seric /* no /etc/shells; see if it is one of the std shells */ 151065587Seric char **d; 151165587Seric 151265587Seric for (d = DefaultUserShells; *d != NULL; d++) 151365587Seric { 151465587Seric if (strcmp(shell, *d) == 0) 151565587Seric return TRUE; 151665587Seric } 151765587Seric return FALSE; 151865211Seric } 151965208Seric 152065211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 152165211Seric { 152265211Seric register char *p, *q; 152365208Seric 152465211Seric p = buf; 152565211Seric while (*p != '\0' && *p != '#' && *p != '/') 152665211Seric p++; 152765211Seric if (*p == '#' || *p == '\0') 152865208Seric continue; 152965211Seric q = p; 153065211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 153165211Seric p++; 153265211Seric *p = '\0'; 153365747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 153465211Seric { 153565211Seric fclose(shellf); 153665211Seric return TRUE; 153765211Seric } 153865208Seric } 153965211Seric fclose(shellf); 154065211Seric return FALSE; 154165211Seric #endif 154265208Seric } 154365208Seric /* 154464311Seric ** FREESPACE -- see how much free space is on the queue filesystem 154558082Seric ** 154658082Seric ** Only implemented if you have statfs. 154758082Seric ** 154858082Seric ** Parameters: 154964311Seric ** dir -- the directory in question. 155064311Seric ** bsize -- a variable into which the filesystem 155164311Seric ** block size is stored. 155258082Seric ** 155358082Seric ** Returns: 155464311Seric ** The number of bytes free on the queue filesystem. 155564311Seric ** -1 if the statfs call fails. 155664311Seric ** 155764311Seric ** Side effects: 155864311Seric ** Puts the filesystem block size into bsize. 155958082Seric */ 156058082Seric 156165749Seric /* statfs types */ 156265749Seric #define SFS_NONE 0 /* no statfs implementation */ 156365749Seric #define SFS_USTAT 1 /* use ustat */ 156465749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 156565749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 156665749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 156765749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 156865749Seric 156965749Seric #ifndef SFS_TYPE 157065749Seric # define SFS_TYPE SFS_NONE 157158157Seric #endif 157258157Seric 157365749Seric #if SFS_TYPE == SFS_USTAT 157458157Seric # include <ustat.h> 157558157Seric #endif 157665749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 157765749Seric # include <sys/statfs.h> 157858082Seric #endif 157965749Seric #if SFS_TYPE == SFS_VFS 158065749Seric # include <sys/vfs.h> 158165749Seric #endif 158265749Seric #if SFS_TYPE == SFS_MOUNT 158365749Seric # include <sys/mount.h> 158465749Seric #endif 158558082Seric 158664311Seric long 158764311Seric freespace(dir, bsize) 158864311Seric char *dir; 158964311Seric long *bsize; 159058082Seric { 159165749Seric #if SFS_TYPE != SFS_NONE 159265749Seric # if SFS_TYPE == SFS_USTAT 159358153Seric struct ustat fs; 159458153Seric struct stat statbuf; 159558366Seric # define FSBLOCKSIZE DEV_BSIZE 159658157Seric # define f_bavail f_tfree 159758157Seric # else 159858157Seric # if defined(ultrix) 159958157Seric struct fs_data fs; 160058157Seric # define f_bavail fd_bfreen 160158366Seric # define FSBLOCKSIZE fs.fd_bsize 160258153Seric # else 160358082Seric struct statfs fs; 160458366Seric # define FSBLOCKSIZE fs.f_bsize 160565820Seric # if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) 160663987Seric # define f_bavail f_bfree 160763987Seric # endif 160858153Seric # endif 160958133Seric # endif 161058082Seric extern int errno; 161158082Seric 161265749Seric # if SFS_TYPE == SFS_USTAT 161364311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 161458157Seric # else 161565749Seric # if SFS_TYPE == SFS_4ARGS 161664311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 161758157Seric # else 161858157Seric # if defined(ultrix) 161964311Seric if (statfs(dir, &fs) > 0) 162058153Seric # else 162164311Seric if (statfs(dir, &fs) == 0) 162258153Seric # endif 162358133Seric # endif 162458133Seric # endif 162558133Seric { 162664311Seric if (bsize != NULL) 162764311Seric *bsize = FSBLOCKSIZE; 162864311Seric return (fs.f_bavail); 162964311Seric } 163064337Seric #endif 163164311Seric return (-1); 163264311Seric } 163364311Seric /* 163464311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 163564311Seric ** 163664311Seric ** Only implemented if you have statfs. 163764311Seric ** 163864311Seric ** Parameters: 163964311Seric ** msize -- the size to check against. If zero, we don't yet 164064311Seric ** know how big the message will be, so just check for 164164311Seric ** a "reasonable" amount. 164264311Seric ** 164364311Seric ** Returns: 164464311Seric ** TRUE if there is enough space. 164564311Seric ** FALSE otherwise. 164664311Seric */ 164764311Seric 164864311Seric bool 164964311Seric enoughspace(msize) 165064311Seric long msize; 165164311Seric { 165264311Seric long bfree, bsize; 165364311Seric 165464311Seric if (MinBlocksFree <= 0 && msize <= 0) 165564311Seric { 165658133Seric if (tTd(4, 80)) 165764311Seric printf("enoughspace: no threshold\n"); 165864311Seric return TRUE; 165964311Seric } 166064311Seric 166164311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 166264311Seric { 166364311Seric if (tTd(4, 80)) 166458333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 166564311Seric bfree, msize); 166658333Seric 166758333Seric /* convert msize to block count */ 166864311Seric msize = msize / bsize + 1; 166958333Seric if (MinBlocksFree >= 0) 167058333Seric msize += MinBlocksFree; 167158333Seric 167264311Seric if (bfree < msize) 167358090Seric { 167458090Seric #ifdef LOG 167558090Seric if (LogLevel > 0) 167664254Seric syslog(LOG_ALERT, 167764254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 167864311Seric CurEnv->e_id, bfree, 167964254Seric CurHostName, msize, QueueDir); 168058090Seric #endif 168158082Seric return FALSE; 168258090Seric } 168358082Seric } 168458082Seric else if (tTd(4, 80)) 168558333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 168658333Seric MinBlocksFree, msize, errstring(errno)); 168758082Seric return TRUE; 168858082Seric } 168958542Seric /* 169058542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 169158542Seric ** 169258542Seric ** This looks at an errno value and tells if this is likely to 169358542Seric ** go away if retried later. 169458542Seric ** 169558542Seric ** Parameters: 169658542Seric ** err -- the errno code to classify. 169758542Seric ** 169858542Seric ** Returns: 169958542Seric ** TRUE if this is probably transient. 170058542Seric ** FALSE otherwise. 170158542Seric */ 170258542Seric 170358542Seric bool 170458542Seric transienterror(err) 170558542Seric int err; 170658542Seric { 170758542Seric switch (err) 170858542Seric { 170958542Seric case EIO: /* I/O error */ 171058542Seric case ENXIO: /* Device not configured */ 171158542Seric case EAGAIN: /* Resource temporarily unavailable */ 171258542Seric case ENOMEM: /* Cannot allocate memory */ 171358542Seric case ENODEV: /* Operation not supported by device */ 171458542Seric case ENFILE: /* Too many open files in system */ 171558542Seric case EMFILE: /* Too many open files */ 171658542Seric case ENOSPC: /* No space left on device */ 171758542Seric #ifdef ETIMEDOUT 171858542Seric case ETIMEDOUT: /* Connection timed out */ 171958542Seric #endif 172058542Seric #ifdef ESTALE 172158542Seric case ESTALE: /* Stale NFS file handle */ 172258542Seric #endif 172358542Seric #ifdef ENETDOWN 172458542Seric case ENETDOWN: /* Network is down */ 172558542Seric #endif 172658542Seric #ifdef ENETUNREACH 172758542Seric case ENETUNREACH: /* Network is unreachable */ 172858542Seric #endif 172958542Seric #ifdef ENETRESET 173058542Seric case ENETRESET: /* Network dropped connection on reset */ 173158542Seric #endif 173258542Seric #ifdef ECONNABORTED 173358542Seric case ECONNABORTED: /* Software caused connection abort */ 173458542Seric #endif 173558542Seric #ifdef ECONNRESET 173658542Seric case ECONNRESET: /* Connection reset by peer */ 173758542Seric #endif 173858542Seric #ifdef ENOBUFS 173958542Seric case ENOBUFS: /* No buffer space available */ 174058542Seric #endif 174158542Seric #ifdef ESHUTDOWN 174258542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 174358542Seric #endif 174458542Seric #ifdef ECONNREFUSED 174558542Seric case ECONNREFUSED: /* Connection refused */ 174658542Seric #endif 174758542Seric #ifdef EHOSTDOWN 174858542Seric case EHOSTDOWN: /* Host is down */ 174958542Seric #endif 175058542Seric #ifdef EHOSTUNREACH 175158542Seric case EHOSTUNREACH: /* No route to host */ 175258542Seric #endif 175358542Seric #ifdef EDQUOT 175458542Seric case EDQUOT: /* Disc quota exceeded */ 175558542Seric #endif 175658542Seric #ifdef EPROCLIM 175758542Seric case EPROCLIM: /* Too many processes */ 175858542Seric #endif 175958542Seric #ifdef EUSERS 176058542Seric case EUSERS: /* Too many users */ 176158542Seric #endif 176258542Seric #ifdef EDEADLK 176358542Seric case EDEADLK: /* Resource deadlock avoided */ 176458542Seric #endif 176558542Seric #ifdef EISCONN 176658542Seric case EISCONN: /* Socket already connected */ 176758542Seric #endif 176858542Seric #ifdef EINPROGRESS 176958542Seric case EINPROGRESS: /* Operation now in progress */ 177058542Seric #endif 177158542Seric #ifdef EALREADY 177258542Seric case EALREADY: /* Operation already in progress */ 177358542Seric #endif 177458542Seric #ifdef EADDRINUSE 177558542Seric case EADDRINUSE: /* Address already in use */ 177658542Seric #endif 177758542Seric #ifdef EADDRNOTAVAIL 177858542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 177958542Seric #endif 178066018Seric #ifdef ETXTBSY 178166017Seric case ETXTBSY: /* (Apollo) file locked */ 178266017Seric #endif 178363834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 178458542Seric case ENOSR: /* Out of streams resources */ 178558542Seric #endif 178658542Seric return TRUE; 178758542Seric } 178858542Seric 178958542Seric /* nope, must be permanent */ 179058542Seric return FALSE; 179158542Seric } 179258689Seric /* 179364035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 179458689Seric ** 179558689Seric ** Parameters: 179658689Seric ** fd -- the file descriptor of the file. 179758689Seric ** filename -- the file name (for error messages). 179864335Seric ** ext -- the filename extension. 179958689Seric ** type -- type of the lock. Bits can be: 180058689Seric ** LOCK_EX -- exclusive lock. 180158689Seric ** LOCK_NB -- non-blocking. 180258689Seric ** 180358689Seric ** Returns: 180458689Seric ** TRUE if the lock was acquired. 180558689Seric ** FALSE otherwise. 180658689Seric */ 180758689Seric 180858689Seric bool 180964335Seric lockfile(fd, filename, ext, type) 181058689Seric int fd; 181158689Seric char *filename; 181264335Seric char *ext; 181358689Seric int type; 181458689Seric { 181565830Seric # if !HASFLOCK 181658689Seric int action; 181758689Seric struct flock lfd; 181864335Seric 181964335Seric if (ext == NULL) 182064335Seric ext = ""; 182164035Seric 182263983Seric bzero(&lfd, sizeof lfd); 182359447Seric if (bitset(LOCK_UN, type)) 182459447Seric lfd.l_type = F_UNLCK; 182559447Seric else if (bitset(LOCK_EX, type)) 182658689Seric lfd.l_type = F_WRLCK; 182758689Seric else 182858689Seric lfd.l_type = F_RDLCK; 182958689Seric 183058689Seric if (bitset(LOCK_NB, type)) 183158689Seric action = F_SETLK; 183258689Seric else 183358689Seric action = F_SETLKW; 183458689Seric 183564264Seric if (tTd(55, 60)) 183664335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 183764335Seric filename, ext, action, lfd.l_type); 183864264Seric 183958689Seric if (fcntl(fd, action, &lfd) >= 0) 184064264Seric { 184164264Seric if (tTd(55, 60)) 184264264Seric printf("SUCCESS\n"); 184358689Seric return TRUE; 184464264Seric } 184558689Seric 184664264Seric if (tTd(55, 60)) 184764264Seric printf("(%s) ", errstring(errno)); 184864264Seric 184964136Seric /* 185064136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 185164136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 185264136Seric ** as type "tmp" (that is, served from swap space), the 185364136Seric ** previous fcntl will fail with "Invalid argument" errors. 185464136Seric ** Since this is fairly common during testing, we will assume 185564136Seric ** that this indicates that the lock is successfully grabbed. 185664136Seric */ 185764136Seric 185864136Seric if (errno == EINVAL) 185964264Seric { 186064264Seric if (tTd(55, 60)) 186164264Seric printf("SUCCESS\n"); 186264136Seric return TRUE; 186364264Seric } 186464136Seric 186558689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 186664378Seric { 186764378Seric int omode = -1; 186864378Seric # ifdef F_GETFL 186964378Seric int oerrno = errno; 187064378Seric 187164378Seric (void) fcntl(fd, F_GETFL, &omode); 187264378Seric errno = oerrno; 187364378Seric # endif 187464378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 187564378Seric filename, ext, fd, type, omode, geteuid()); 187664378Seric } 187758689Seric # else 187864335Seric if (ext == NULL) 187964335Seric ext = ""; 188064335Seric 188164264Seric if (tTd(55, 60)) 188264335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 188364264Seric 188458689Seric if (flock(fd, type) >= 0) 188564264Seric { 188664264Seric if (tTd(55, 60)) 188764264Seric printf("SUCCESS\n"); 188858689Seric return TRUE; 188964264Seric } 189058689Seric 189164264Seric if (tTd(55, 60)) 189264264Seric printf("(%s) ", errstring(errno)); 189364264Seric 189458689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 189564378Seric { 189664378Seric int omode = -1; 189764378Seric # ifdef F_GETFL 189864378Seric int oerrno = errno; 189964378Seric 190064378Seric (void) fcntl(fd, F_GETFL, &omode); 190164378Seric errno = oerrno; 190264378Seric # endif 190364378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 190464378Seric filename, ext, fd, type, omode, geteuid()); 190564386Seric } 190658689Seric # endif 190764264Seric if (tTd(55, 60)) 190864264Seric printf("FAIL\n"); 190958689Seric return FALSE; 191058689Seric } 191164556Seric /* 191265948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 191365948Seric ** 191465948Seric ** Parameters: 191565948Seric ** fd -- the file descriptor to check. 191665948Seric ** 191765948Seric ** Returns: 191865948Seric ** TRUE -- if only root can chown the file to an arbitrary 191965948Seric ** user. 192065948Seric ** FALSE -- if an arbitrary user can give away a file. 192165948Seric */ 192265948Seric 192365948Seric bool 192465948Seric chownsafe(fd) 192565948Seric int fd; 192665948Seric { 192765950Seric #ifdef __hpux 192865948Seric char *s; 192965948Seric int tfd; 193065948Seric uid_t o_uid, o_euid; 193165948Seric gid_t o_gid, o_egid; 193265948Seric bool rval; 193365948Seric struct stat stbuf; 193465948Seric 193565948Seric o_uid = getuid(); 193665948Seric o_euid = geteuid(); 193765948Seric o_gid = getgid(); 193865948Seric o_egid = getegid(); 193965959Seric fstat(fd, &stbuf); 194065948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 194165948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 194265948Seric s = tmpnam(NULL); 194365948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 194465974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 194565948Seric close(tfd); 194665948Seric unlink(s); 194765948Seric setreuid(o_uid, o_euid); 194865948Seric setresgid(o_gid, o_egid, -1); 194965948Seric return rval; 195065948Seric #else 195166088Seric # ifdef _POSIX_CHOWN_RESTRICTED 195266088Seric # if _POSIX_CHOWN_RESTRICTED == -1 195366088Seric return FALSE; 195466088Seric # else 195566088Seric return TRUE; 195666088Seric # endif 195766088Seric # else 195866088Seric # ifdef _PC_CHOWN_RESTRICTED 195965948Seric return fpathconf(fd, _PC_CHOWN_RESTRICTED) > 0; 196066088Seric # else 196166088Seric # ifdef BSD 196265948Seric return TRUE; 196366088Seric # else 196465948Seric return FALSE; 196566088Seric # endif 196665948Seric # endif 196765948Seric # endif 196865948Seric #endif 196965948Seric } 197065948Seric /* 197164556Seric ** GETCFNAME -- return the name of the .cf file. 197264556Seric ** 197364556Seric ** Some systems (e.g., NeXT) determine this dynamically. 197464556Seric */ 197564556Seric 197664556Seric char * 197764556Seric getcfname() 197864556Seric { 197964556Seric if (ConfFile != NULL) 198064556Seric return ConfFile; 198165214Seric #ifdef NETINFO 198265214Seric { 198365214Seric extern char *ni_propval(); 198465214Seric char *cflocation; 198565214Seric 198665214Seric cflocation = ni_propval("/locations/sendmail", "sendmail.cf"); 198765214Seric if (cflocation != NULL) 198865214Seric return cflocation; 198965214Seric } 199065214Seric #endif 199164556Seric return _PATH_SENDMAILCF; 199264556Seric } 199364718Seric /* 199464718Seric ** SETVENDOR -- process vendor code from V configuration line 199564718Seric ** 199664718Seric ** Parameters: 199764718Seric ** vendor -- string representation of vendor. 199864718Seric ** 199964718Seric ** Returns: 200064718Seric ** TRUE -- if ok. 200164718Seric ** FALSE -- if vendor code could not be processed. 200264926Seric ** 200364926Seric ** Side Effects: 200464926Seric ** It is reasonable to set mode flags here to tweak 200564926Seric ** processing in other parts of the code if necessary. 200664926Seric ** For example, if you are a vendor that uses $%y to 200764926Seric ** indicate YP lookups, you could enable that here. 200864718Seric */ 200964718Seric 201064718Seric bool 201164718Seric setvendor(vendor) 201264718Seric char *vendor; 201364718Seric { 201464926Seric if (strcasecmp(vendor, "Berkeley") == 0) 201564926Seric return TRUE; 201664926Seric 201764926Seric /* add vendor extensions here */ 201864926Seric 201964926Seric return FALSE; 202064718Seric } 202164816Seric /* 202264816Seric ** STRTOL -- convert string to long integer 202364816Seric ** 202464816Seric ** For systems that don't have it in the C library. 202566161Seric ** 202666161Seric ** This is taken verbatim from the 4.4-Lite C library. 202764816Seric */ 202864816Seric 202964816Seric #ifdef NEEDSTRTOL 203064816Seric 203166161Seric #if defined(LIBC_SCCS) && !defined(lint) 203266161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 203366161Seric #endif /* LIBC_SCCS and not lint */ 203466161Seric 203566161Seric #include <limits.h> 203666161Seric 203766161Seric /* 203866161Seric * Convert a string to a long integer. 203966161Seric * 204066161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 204166161Seric * alphabets and digits are each contiguous. 204266161Seric */ 204366161Seric 204464816Seric long 204566161Seric strtol(nptr, endptr, base) 204666161Seric const char *nptr; 204766161Seric char **endptr; 204866161Seric register int base; 204964816Seric { 205066161Seric register const char *s = nptr; 205166161Seric register unsigned long acc; 205266161Seric register int c; 205366161Seric register unsigned long cutoff; 205466161Seric register int neg = 0, any, cutlim; 205564816Seric 205666161Seric /* 205766161Seric * Skip white space and pick up leading +/- sign if any. 205866161Seric * If base is 0, allow 0x for hex and 0 for octal, else 205966161Seric * assume decimal; if base is already 16, allow 0x. 206066161Seric */ 206166161Seric do { 206266161Seric c = *s++; 206366161Seric } while (isspace(c)); 206466161Seric if (c == '-') { 206566161Seric neg = 1; 206666161Seric c = *s++; 206766161Seric } else if (c == '+') 206866161Seric c = *s++; 206966161Seric if ((base == 0 || base == 16) && 207066161Seric c == '0' && (*s == 'x' || *s == 'X')) { 207166161Seric c = s[1]; 207266161Seric s += 2; 207366161Seric base = 16; 207466161Seric } 207566161Seric if (base == 0) 207666161Seric base = c == '0' ? 8 : 10; 207764816Seric 207866161Seric /* 207966161Seric * Compute the cutoff value between legal numbers and illegal 208066161Seric * numbers. That is the largest legal value, divided by the 208166161Seric * base. An input number that is greater than this value, if 208266161Seric * followed by a legal input character, is too big. One that 208366161Seric * is equal to this value may be valid or not; the limit 208466161Seric * between valid and invalid numbers is then based on the last 208566161Seric * digit. For instance, if the range for longs is 208666161Seric * [-2147483648..2147483647] and the input base is 10, 208766161Seric * cutoff will be set to 214748364 and cutlim to either 208866161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 208966161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 209066161Seric * the number is too big, and we will return a range error. 209166161Seric * 209266161Seric * Set any if any `digits' consumed; make it negative to indicate 209366161Seric * overflow. 209466161Seric */ 209566161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 209666161Seric cutlim = cutoff % (unsigned long)base; 209766161Seric cutoff /= (unsigned long)base; 209866161Seric for (acc = 0, any = 0;; c = *s++) { 209966161Seric if (isdigit(c)) 210066161Seric c -= '0'; 210166161Seric else if (isalpha(c)) 210266161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 210366161Seric else 210466161Seric break; 210566161Seric if (c >= base) 210666161Seric break; 210766161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 210866161Seric any = -1; 210966161Seric else { 211066161Seric any = 1; 211166161Seric acc *= base; 211266161Seric acc += c; 211364816Seric } 211464816Seric } 211566161Seric if (any < 0) { 211666161Seric acc = neg ? LONG_MIN : LONG_MAX; 211766161Seric errno = ERANGE; 211866161Seric } else if (neg) 211966161Seric acc = -acc; 212066161Seric if (endptr != 0) 212166161Seric *endptr = (char *)(any ? s - 1 : nptr); 212266161Seric return (acc); 212364816Seric } 212464816Seric 212564816Seric #endif 212664841Seric /* 212764841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 212864841Seric ** 212964841Seric ** Solaris versions prior through 2.3 don't properly deliver a 213064841Seric ** canonical h_name field. This tries to work around it. 213164841Seric */ 213264841Seric 213364841Seric #ifdef SOLARIS 213464841Seric 213564841Seric struct hostent * 213664841Seric solaris_gethostbyname(name) 213765094Seric const char *name; 213864841Seric { 213964841Seric # ifdef SOLARIS_2_3 214064841Seric static struct hostent hp; 214164841Seric static char buf[1000]; 214264841Seric extern struct hostent *_switch_gethostbyname_r(); 214364841Seric 214464841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 214564841Seric # else 214665009Seric extern struct hostent *__switch_gethostbyname(); 214765009Seric 214864841Seric return __switch_gethostbyname(name); 214964841Seric # endif 215064841Seric } 215164841Seric 215264841Seric struct hostent * 215364841Seric solaris_gethostbyaddr(addr, len, type) 215465094Seric const char *addr; 215564841Seric int len; 215664841Seric int type; 215764841Seric { 215864841Seric # ifdef SOLARIS_2_3 215964841Seric static struct hostent hp; 216064841Seric static char buf[1000]; 216164841Seric extern struct hostent *_switch_gethostbyaddr_r(); 216264841Seric 216364841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 216464841Seric # else 216565009Seric extern struct hostent *__switch_gethostbyaddr(); 216665009Seric 216764841Seric return __switch_gethostbyaddr(addr, len, type); 216864841Seric # endif 216964841Seric } 217064841Seric 217164841Seric #endif 217265214Seric /* 217365214Seric ** NI_PROPVAL -- netinfo property value lookup routine 217465214Seric ** 217565214Seric ** Parameters: 217665214Seric ** directory -- the Netinfo directory name. 217765214Seric ** propname -- the Netinfo property name. 217865214Seric ** 217965214Seric ** Returns: 218065214Seric ** NULL -- if: 218165214Seric ** 1. the directory is not found 218265214Seric ** 2. the property name is not found 218365214Seric ** 3. the property contains multiple values 218465214Seric ** 4. some error occured 218565214Seric ** else -- the location of the config file. 218665214Seric ** 218765214Seric ** Notes: 218865214Seric ** Caller should free the return value of ni_proval 218965214Seric */ 219065214Seric 219165214Seric #ifdef NETINFO 219265214Seric 219365214Seric # include <netinfo/ni.h> 219465214Seric 219565214Seric # define LOCAL_NETINFO_DOMAIN "." 219665214Seric # define PARENT_NETINFO_DOMAIN ".." 219765214Seric # define MAX_NI_LEVELS 256 219865214Seric 219965214Seric char * 220065214Seric ni_propval(directory, propname) 220165214Seric char *directory; 220265214Seric char *propname; 220365214Seric { 220465820Seric char *propval = NULL; 220565214Seric int i; 220665214Seric void *ni = NULL; 220765214Seric void *lastni = NULL; 220865214Seric ni_status nis; 220965214Seric ni_id nid; 221065214Seric ni_namelist ninl; 221165214Seric 221265214Seric /* 221365214Seric ** If the passed directory and property name are found 221465214Seric ** in one of netinfo domains we need to search (starting 221565214Seric ** from the local domain moving all the way back to the 221665214Seric ** root domain) set propval to the property's value 221765214Seric ** and return it. 221865214Seric */ 221965214Seric 222065214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 222165214Seric { 222265214Seric if (i == 0) 222365214Seric { 222465214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 222565214Seric } 222665214Seric else 222765214Seric { 222865214Seric if (lastni != NULL) 222965214Seric ni_free(lastni); 223065214Seric lastni = ni; 223165214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 223265214Seric } 223365214Seric 223465214Seric /* 223565214Seric ** Don't bother if we didn't get a handle on a 223665214Seric ** proper domain. This is not necessarily an error. 223765214Seric ** We would get a positive ni_status if, for instance 223865214Seric ** we never found the directory or property and tried 223965214Seric ** to open the parent of the root domain! 224065214Seric */ 224165214Seric 224265214Seric if (nis != 0) 224365214Seric break; 224465214Seric 224565214Seric /* 224665214Seric ** Find the path to the server information. 224765214Seric */ 224865214Seric 224965214Seric if (ni_pathsearch(ni, &nid, directory) != 0) 225065214Seric continue; 225165214Seric 225265214Seric /* 225365214Seric ** Find "host" information. 225465214Seric */ 225565214Seric 225665214Seric if (ni_lookupprop(ni, &nid, propname, &ninl) != 0) 225765214Seric continue; 225865214Seric 225965214Seric /* 226065214Seric ** If there's only one name in 226165214Seric ** the list, assume we've got 226265214Seric ** what we want. 226365214Seric */ 226465214Seric 226565214Seric if (ninl.ni_namelist_len == 1) 226665214Seric { 226765214Seric propval = ni_name_dup(ninl.ni_namelist_val[0]); 226865214Seric break; 226965214Seric } 227065214Seric } 227165214Seric 227265214Seric /* 227365214Seric ** Clean up. 227465214Seric */ 227565214Seric 227665214Seric if (ni != NULL) 227765214Seric ni_free(ni); 227865214Seric if (lastni != NULL && ni != lastni) 227965214Seric ni_free(lastni); 228065214Seric 228165214Seric return propval; 228265214Seric } 228365214Seric 228465214Seric #endif /* NETINFO */ 228565504Seric /* 228665504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 228765504Seric ** 228865504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 228965504Seric ** syslog succeeds during interrupt handlers. 229065504Seric */ 229165504Seric 229265504Seric #ifdef __hpux 229365504Seric 229465504Seric # define MAXSYSLOGTRIES 100 229565504Seric # undef syslog 229665504Seric 229765504Seric # ifdef __STDC__ 229865504Seric hard_syslog(int pri, char *msg, ...) 229965504Seric # else 230065504Seric hard_syslog(pri, msg, va_alist) 230165504Seric int pri; 230265504Seric char *msg; 230365504Seric va_dcl 230465504Seric # endif 230565504Seric { 230665504Seric int i; 230765504Seric char buf[SYSLOG_BUFSIZE * 2]; 230865504Seric VA_LOCAL_DECL; 230965504Seric 231065504Seric VA_START(msg); 231165504Seric vsprintf(buf, msg, ap); 231265504Seric VA_END; 231365504Seric 231465504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 231565504Seric continue; 231665504Seric } 231765504Seric 231865504Seric #endif 2319