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*66161Seric static char sccsid[] = "@(#)conf.c 8.77 (Berkeley) 02/19/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 */ 60514872Seric 60663962Seric /* do guesses based on general OS type */ 60751920Seric #ifndef LA_TYPE 60863962Seric # define LA_TYPE LA_ZERO 60951920Seric #endif 61051920Seric 61164360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 61251920Seric 61314872Seric #include <nlist.h> 61414872Seric 61551920Seric #ifndef LA_AVENRUN 61663962Seric # ifdef SYSTEM5 61763962Seric # define LA_AVENRUN "avenrun" 61863962Seric # else 61963962Seric # define LA_AVENRUN "_avenrun" 62063962Seric # endif 62151920Seric #endif 62251920Seric 62351920Seric /* _PATH_UNIX should be defined in <paths.h> */ 62451920Seric #ifndef _PATH_UNIX 62563962Seric # if defined(SYSTEM5) 62663962Seric # define _PATH_UNIX "/unix" 62763962Seric # else 62863962Seric # define _PATH_UNIX "/vmunix" 62963962Seric # endif 63051920Seric #endif 63151920Seric 63214872Seric struct nlist Nl[] = 63314872Seric { 63451920Seric { LA_AVENRUN }, 63514872Seric #define X_AVENRUN 0 63614872Seric { 0 }, 63714872Seric }; 63814872Seric 63960583Seric #ifndef FSHIFT 64060583Seric # if defined(unixpc) 64160583Seric # define FSHIFT 5 64260583Seric # endif 64357736Seric 64460583Seric # if defined(__alpha) 64560583Seric # define FSHIFT 10 64660583Seric # endif 64757977Seric 64864360Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 64951920Seric # define FSHIFT 8 65060583Seric # endif 65157736Seric #endif 65260583Seric 65364360Seric #if ((LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT)) && !defined(FSCALE) 65451920Seric # define FSCALE (1 << FSHIFT) 65551920Seric #endif 65640930Srick 65714872Seric getla() 65814872Seric { 65914872Seric static int kmem = -1; 66051920Seric #if LA_TYPE == LA_INT 66124943Seric long avenrun[3]; 66251920Seric #else 66364360Seric # if LA_TYPE == LA_SHORT 66464360Seric short avenrun[3]; 66564360Seric # else 66651920Seric double avenrun[3]; 66764360Seric # endif 66851920Seric #endif 66925615Seric extern off_t lseek(); 67057736Seric extern int errno; 67114872Seric 67214872Seric if (kmem < 0) 67314872Seric { 67424945Seric kmem = open("/dev/kmem", 0, 0); 67514872Seric if (kmem < 0) 67657736Seric { 67757736Seric if (tTd(3, 1)) 67857736Seric printf("getla: open(/dev/kmem): %s\n", 67957736Seric errstring(errno)); 68014872Seric return (-1); 68157736Seric } 68251920Seric (void) fcntl(kmem, F_SETFD, 1); 68357736Seric if (nlist(_PATH_UNIX, Nl) < 0) 68457736Seric { 68557736Seric if (tTd(3, 1)) 68657736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 68757736Seric errstring(errno)); 68814872Seric return (-1); 68957736Seric } 69059253Seric if (Nl[X_AVENRUN].n_value == 0) 69159253Seric { 69259253Seric if (tTd(3, 1)) 69359253Seric printf("getla: nlist(%s, %s) ==> 0\n", 69459253Seric _PATH_UNIX, LA_AVENRUN); 69559253Seric return (-1); 69659253Seric } 69714872Seric } 69857736Seric if (tTd(3, 20)) 69957736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 70024945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 70123118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 70219967Seric { 70319967Seric /* thank you Ian */ 70457736Seric if (tTd(3, 1)) 70557736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 70619967Seric return (-1); 70719967Seric } 70864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 70957736Seric if (tTd(3, 5)) 71057736Seric { 71157736Seric printf("getla: avenrun = %d", avenrun[0]); 71257736Seric if (tTd(3, 15)) 71357736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 71457736Seric printf("\n"); 71557736Seric } 71657736Seric if (tTd(3, 1)) 71757736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 71824943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 71951920Seric #else 72057736Seric if (tTd(3, 5)) 72157736Seric { 72257736Seric printf("getla: avenrun = %g", avenrun[0]); 72357736Seric if (tTd(3, 15)) 72457736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 72557736Seric printf("\n"); 72657736Seric } 72757736Seric if (tTd(3, 1)) 72857736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 72951920Seric return ((int) (avenrun[0] + 0.5)); 73051920Seric #endif 73114872Seric } 73214872Seric 73351773Seric #else 73451920Seric #if LA_TYPE == LA_SUBR 73551773Seric 73664718Seric #ifdef DGUX 73764718Seric 73864718Seric #include <sys/dg_sys_info.h> 73964718Seric 74064718Seric int getla() 74164718Seric { 74264718Seric struct dg_sys_info_load_info load_info; 74364718Seric 74464718Seric dg_sys_info((long *)&load_info, 74564718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 74664718Seric 74764718Seric return((int) (load_info.one_minute + 0.5)); 74864718Seric } 74964718Seric 75064718Seric #else 75164718Seric 75251773Seric getla() 75351773Seric { 75451920Seric double avenrun[3]; 75551920Seric 75651920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 75757736Seric { 75857736Seric if (tTd(3, 1)) 75957736Seric perror("getla: getloadavg failed:"); 76051920Seric return (-1); 76157736Seric } 76257736Seric if (tTd(3, 1)) 76357736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 76451920Seric return ((int) (avenrun[0] + 0.5)); 76551773Seric } 76651773Seric 76764718Seric #endif /* DGUX */ 76851773Seric #else 76964295Seric #if LA_TYPE == LA_MACH 77051773Seric 77164295Seric /* 77265173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 77364295Seric */ 77464295Seric 77565173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 77665173Seric # include <mach/mach.h> 77765173Seric #else 77865173Seric # include <mach.h> 77965173Seric #endif 78064295Seric 78151773Seric getla() 78251773Seric { 78364295Seric processor_set_t default_set; 78464295Seric kern_return_t error; 78564295Seric unsigned int info_count; 78664295Seric struct processor_set_basic_info info; 78764295Seric host_t host; 78864295Seric 78964295Seric error = processor_set_default(host_self(), &default_set); 79064295Seric if (error != KERN_SUCCESS) 79164295Seric return -1; 79264295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 79364295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 79464295Seric &host, (processor_set_info_t)&info, 79564295Seric &info_count) != KERN_SUCCESS) 79664295Seric { 79764295Seric return -1; 79864295Seric } 79964295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 80064295Seric } 80164295Seric 80264295Seric 80364295Seric #else 80464295Seric 80564295Seric getla() 80664295Seric { 80757736Seric if (tTd(3, 1)) 80857736Seric printf("getla: ZERO\n"); 80951920Seric return (0); 81051773Seric } 81151773Seric 81251773Seric #endif 81351773Seric #endif 81464295Seric #endif 81566045Seric 81666045Seric 81766045Seric /* 81866045Seric * Copyright 1989 Massachusetts Institute of Technology 81966045Seric * 82066045Seric * Permission to use, copy, modify, distribute, and sell this software and its 82166045Seric * documentation for any purpose is hereby granted without fee, provided that 82266045Seric * the above copyright notice appear in all copies and that both that 82366045Seric * copyright notice and this permission notice appear in supporting 82466045Seric * documentation, and that the name of M.I.T. not be used in advertising or 82566045Seric * publicity pertaining to distribution of the software without specific, 82666045Seric * written prior permission. M.I.T. makes no representations about the 82766045Seric * suitability of this software for any purpose. It is provided "as is" 82866045Seric * without express or implied warranty. 82966045Seric * 83066045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 83166045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 83266045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 83366045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 83466045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 83566045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 83666045Seric * 83766045Seric * Authors: Many and varied... 83866045Seric */ 83966045Seric 84066045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 84166045Seric #ifndef lint 84266045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 84366045Seric #endif /* !lint */ 84466045Seric 84566045Seric # ifdef apollo 84666045Seric # undef volatile 84766045Seric # include <apollo/base.h> 84866045Seric # include <apollo/time.h> 84966045Seric # include <sys/types.h> 85066045Seric 85166045Seric /* ARGSUSED */ 85266045Seric int getloadavg( call_data ) 85366045Seric caddr_t call_data; /* pointer to (double) return value */ 85466045Seric { 85566045Seric double *avenrun = (double *) call_data; 85666045Seric int i; 85766045Seric status_$t st; 85866045Seric long loadav[3]; 85966045Seric proc1_$get_loadav(loadav, &st); 86066045Seric *avenrun = loadav[0] / (double) (1 << 16); 86166045Seric return(0); 86266045Seric } 86366045Seric # endif /* apollo */ 86424943Seric /* 86524943Seric ** SHOULDQUEUE -- should this message be queued or sent? 86624943Seric ** 86724943Seric ** Compares the message cost to the load average to decide. 86824943Seric ** 86924943Seric ** Parameters: 87024943Seric ** pri -- the priority of the message in question. 87157438Seric ** ctime -- the message creation time. 87224943Seric ** 87324943Seric ** Returns: 87424943Seric ** TRUE -- if this message should be queued up for the 87524943Seric ** time being. 87624943Seric ** FALSE -- if the load is low enough to send this message. 87724943Seric ** 87824943Seric ** Side Effects: 87924943Seric ** none. 88024943Seric */ 88124943Seric 88224943Seric bool 88357438Seric shouldqueue(pri, ctime) 88424943Seric long pri; 88557438Seric time_t ctime; 88624943Seric { 88751920Seric if (CurrentLA < QueueLA) 88824943Seric return (FALSE); 88958132Seric if (CurrentLA >= RefuseLA) 89058132Seric return (TRUE); 89151920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 89224943Seric } 89324943Seric /* 89453037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 89553037Seric ** 89653037Seric ** Parameters: 89753037Seric ** none. 89853037Seric ** 89953037Seric ** Returns: 90053037Seric ** TRUE if incoming SMTP connections should be refused 90153037Seric ** (for now). 90253037Seric ** FALSE if we should accept new work. 90353037Seric ** 90453037Seric ** Side Effects: 90553037Seric ** none. 90653037Seric */ 90753037Seric 90853037Seric bool 90953037Seric refuseconnections() 91053037Seric { 91159156Seric #ifdef XLA 91259156Seric if (!xla_smtp_ok()) 91359156Seric return TRUE; 91459156Seric #endif 91559156Seric 91653037Seric /* this is probably too simplistic */ 91758132Seric return (CurrentLA >= RefuseLA); 91853037Seric } 91953037Seric /* 92024943Seric ** SETPROCTITLE -- set process title for ps 92124943Seric ** 92224943Seric ** Parameters: 92358674Seric ** fmt -- a printf style format string. 92458674Seric ** a, b, c -- possible parameters to fmt. 92524943Seric ** 92624943Seric ** Returns: 92724943Seric ** none. 92824943Seric ** 92924943Seric ** Side Effects: 93024943Seric ** Clobbers argv of our main procedure so ps(1) will 93124943Seric ** display the title. 93224943Seric */ 93324943Seric 93458689Seric #ifdef SETPROCTITLE 93566002Seric # ifdef HASSETPROCTITLE 93666002Seric *** ERROR *** Cannot have both SETPROCTITLE and HASSETPROCTITLE defined 93766002Seric # endif 93858689Seric # ifdef __hpux 93958689Seric # include <sys/pstat.h> 94058689Seric # endif 94159732Seric # ifdef BSD4_4 94259732Seric # include <machine/vmparam.h> 94359732Seric # include <sys/exec.h> 94464247Seric # ifdef __bsdi__ 94564247Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 94664843Seric # define PROCTITLEPAD '\0' 94764247Seric # endif 94859992Seric # ifdef PS_STRINGS 94959992Seric # define SETPROC_STATIC static 95059992Seric # endif 95159732Seric # endif 95259732Seric # ifndef SETPROC_STATIC 95359732Seric # define SETPROC_STATIC 95459732Seric # endif 95558689Seric #endif 95658689Seric 95764843Seric #ifndef PROCTITLEPAD 95864843Seric # define PROCTITLEPAD ' ' 95964843Seric #endif 96064843Seric 96165982Seric #ifndef HASSETPROCTITLE 96265982Seric 96324943Seric /*VARARGS1*/ 96457642Seric #ifdef __STDC__ 96557642Seric setproctitle(char *fmt, ...) 96657642Seric #else 96757642Seric setproctitle(fmt, va_alist) 96824943Seric char *fmt; 96957642Seric va_dcl 97057642Seric #endif 97124943Seric { 97224943Seric # ifdef SETPROCTITLE 97324943Seric register char *p; 97425049Seric register int i; 97559732Seric SETPROC_STATIC char buf[MAXLINE]; 97656852Seric VA_LOCAL_DECL 97758689Seric # ifdef __hpux 97858689Seric union pstun pst; 97958689Seric # endif 98024943Seric extern char **Argv; 98124943Seric extern char *LastArgv; 98224943Seric 98358674Seric p = buf; 98424943Seric 98558674Seric /* print sendmail: heading for grep */ 98658674Seric (void) strcpy(p, "sendmail: "); 98758674Seric p += strlen(p); 98824943Seric 98958674Seric /* print the argument string */ 99058674Seric VA_START(fmt); 99158674Seric (void) vsprintf(p, fmt, ap); 99256852Seric VA_END; 99354996Seric 99458674Seric i = strlen(buf); 99558689Seric 99658689Seric # ifdef __hpux 99758689Seric pst.pst_command = buf; 99858689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 99958689Seric # else 100059992Seric # ifdef PS_STRINGS 100159732Seric PS_STRINGS->ps_nargvstr = 1; 100259732Seric PS_STRINGS->ps_argvstr = buf; 100359732Seric # else 100454996Seric if (i > LastArgv - Argv[0] - 2) 100525049Seric { 100654996Seric i = LastArgv - Argv[0] - 2; 100758674Seric buf[i] = '\0'; 100825049Seric } 100958674Seric (void) strcpy(Argv[0], buf); 101054997Seric p = &Argv[0][i]; 101124943Seric while (p < LastArgv) 101264843Seric *p++ = PROCTITLEPAD; 101359732Seric # endif 101458689Seric # endif 101556795Seric # endif /* SETPROCTITLE */ 101624943Seric } 101765982Seric 101865982Seric #endif 101925698Seric /* 102025698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 102125698Seric ** 102225698Seric ** Parameters: 102325698Seric ** none. 102425698Seric ** 102525698Seric ** Returns: 102625698Seric ** none. 102725698Seric ** 102825698Seric ** Side Effects: 102925698Seric ** Picks up extant zombies. 103025698Seric */ 103125698Seric 103246928Sbostic void 103325698Seric reapchild() 103425698Seric { 103564738Seric int olderrno = errno; 103663966Seric # ifdef HASWAITPID 103760219Seric auto int status; 103860560Seric int count; 103960560Seric int pid; 104060219Seric 104160560Seric count = 0; 104260560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 104360560Seric { 104460560Seric if (count++ > 1000) 104560560Seric { 104660560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 104760560Seric pid, status); 104860560Seric break; 104960560Seric } 105060560Seric } 105160219Seric # else 105225698Seric # ifdef WNOHANG 105325698Seric union wait status; 105425698Seric 105563753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 105625698Seric continue; 105756795Seric # else /* WNOHANG */ 105825698Seric auto int status; 105925698Seric 106060219Seric while (wait(&status) > 0) 106125698Seric continue; 106256795Seric # endif /* WNOHANG */ 106360219Seric # endif 106464035Seric # ifdef SYS5SIGNALS 106564035Seric (void) setsignal(SIGCHLD, reapchild); 106658061Seric # endif 106764738Seric errno = olderrno; 106825698Seric } 106955418Seric /* 107055418Seric ** UNSETENV -- remove a variable from the environment 107155418Seric ** 107255418Seric ** Not needed on newer systems. 107355418Seric ** 107455418Seric ** Parameters: 107555418Seric ** name -- the string name of the environment variable to be 107655418Seric ** deleted from the current environment. 107755418Seric ** 107855418Seric ** Returns: 107955418Seric ** none. 108055418Seric ** 108155418Seric ** Globals: 108255418Seric ** environ -- a pointer to the current environment. 108355418Seric ** 108455418Seric ** Side Effects: 108555418Seric ** Modifies environ. 108655418Seric */ 108755418Seric 108863962Seric #ifndef HASUNSETENV 108955418Seric 109055418Seric void 109155418Seric unsetenv(name) 109255418Seric char *name; 109355418Seric { 109455418Seric extern char **environ; 109555418Seric register char **pp; 109655418Seric int len = strlen(name); 109755418Seric 109855418Seric for (pp = environ; *pp != NULL; pp++) 109955418Seric { 110055418Seric if (strncmp(name, *pp, len) == 0 && 110155418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 110255418Seric break; 110355418Seric } 110455418Seric 110555418Seric for (; *pp != NULL; pp++) 110655418Seric *pp = pp[1]; 110755418Seric } 110855418Seric 110963962Seric #endif 111056215Seric /* 111156215Seric ** GETDTABLESIZE -- return number of file descriptors 111256215Seric ** 111356215Seric ** Only on non-BSD systems 111456215Seric ** 111556215Seric ** Parameters: 111656215Seric ** none 111756215Seric ** 111856215Seric ** Returns: 111956215Seric ** size of file descriptor table 112056215Seric ** 112156215Seric ** Side Effects: 112256215Seric ** none 112356215Seric */ 112456215Seric 112563787Seric #ifdef SOLARIS 112663787Seric # include <sys/resource.h> 112763787Seric #endif 112856215Seric 112956215Seric int 113063787Seric getdtsize() 113156215Seric { 113263787Seric #ifdef RLIMIT_NOFILE 113363787Seric struct rlimit rl; 113463787Seric 113563787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 113663787Seric return rl.rlim_cur; 113763787Seric #endif 113863787Seric 113964031Seric # ifdef HASGETDTABLESIZE 114064031Seric return getdtablesize(); 114164031Seric # else 114264031Seric # ifdef _SC_OPEN_MAX 114358689Seric return sysconf(_SC_OPEN_MAX); 114463787Seric # else 114556215Seric return NOFILE; 114663787Seric # endif 114758689Seric # endif 114856215Seric } 114957631Seric /* 115057631Seric ** UNAME -- get the UUCP name of this system. 115157631Seric */ 115257631Seric 115357943Seric #ifndef HASUNAME 115457631Seric 115557631Seric int 115657631Seric uname(name) 115757631Seric struct utsname *name; 115857631Seric { 115957631Seric FILE *file; 116057631Seric char *n; 116157631Seric 116257631Seric name->nodename[0] = '\0'; 116357631Seric 116457661Seric /* try /etc/whoami -- one line with the node name */ 116557631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 116657631Seric { 116757661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 116857631Seric (void) fclose(file); 116957661Seric n = strchr(name->nodename, '\n'); 117057631Seric if (n != NULL) 117157631Seric *n = '\0'; 117257631Seric if (name->nodename[0] != '\0') 117357631Seric return (0); 117457631Seric } 117557631Seric 117657661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 117757631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 117857631Seric { 117957631Seric char buf[MAXLINE]; 118057631Seric 118157631Seric while (fgets(buf, MAXLINE, file) != NULL) 118257631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 118357631Seric NODE_LENGTH, name->nodename) > 0) 118457631Seric break; 118557631Seric (void) fclose(file); 118657631Seric if (name->nodename[0] != '\0') 118757631Seric return (0); 118857631Seric } 118957631Seric 119057631Seric #ifdef TRUST_POPEN 119157631Seric /* 119257631Seric ** Popen is known to have security holes. 119357631Seric */ 119457631Seric 119557661Seric /* try uuname -l to return local name */ 119657631Seric if ((file = popen("uuname -l", "r")) != NULL) 119757631Seric { 119857661Seric (void) fgets(name, NODE_LENGTH + 1, file); 119957631Seric (void) pclose(file); 120057661Seric n = strchr(name, '\n'); 120157631Seric if (n != NULL) 120257631Seric *n = '\0'; 120357661Seric if (name->nodename[0] != '\0') 120457631Seric return (0); 120557631Seric } 120657631Seric #endif 120757631Seric 120857631Seric return (-1); 120957631Seric } 121057943Seric #endif /* HASUNAME */ 121158068Seric /* 121258068Seric ** INITGROUPS -- initialize groups 121358068Seric ** 121458068Seric ** Stub implementation for System V style systems 121558068Seric */ 121658068Seric 121758068Seric #ifndef HASINITGROUPS 121858068Seric 121958068Seric initgroups(name, basegid) 122058068Seric char *name; 122158068Seric int basegid; 122258068Seric { 122358068Seric return 0; 122458068Seric } 122558068Seric 122658068Seric #endif 122758082Seric /* 122859289Seric ** SETSID -- set session id (for non-POSIX systems) 122959289Seric */ 123059289Seric 123159289Seric #ifndef HASSETSID 123259289Seric 123363753Seric pid_t 123463753Seric setsid __P ((void)) 123559289Seric { 123664155Seric #ifdef TIOCNOTTY 123764155Seric int fd; 123864155Seric 123964155Seric fd = open("/dev/tty", 2); 124064155Seric if (fd >= 0) 124164155Seric { 124264155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 124364155Seric (void) close(fd); 124464155Seric } 124564155Seric #endif /* TIOCNOTTY */ 124664705Seric # ifdef SYS5SETPGRP 124763753Seric return setpgrp(); 124863753Seric # else 124964155Seric return setpgid(0, getpid()); 125059289Seric # endif 125159289Seric } 125259289Seric 125359289Seric #endif 125459289Seric /* 125564718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 125664718Seric ** 125764718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 125864718Seric ** instead of a long. This patches things. 125964718Seric */ 126064718Seric 126164718Seric #ifdef DGUX 126264718Seric 126364718Seric #undef inet_addr 126464718Seric 126564718Seric long 126664718Seric dgux_inet_addr(host) 126764718Seric char *host; 126864718Seric { 126964718Seric struct in_addr haddr; 127064718Seric 127164718Seric haddr = inet_addr(host); 127264718Seric return haddr.s_addr; 127364718Seric } 127464718Seric 127564718Seric #endif 127664718Seric /* 127763969Seric ** GETOPT -- for old systems or systems with bogus implementations 127863969Seric */ 127963969Seric 128063969Seric #ifdef NEEDGETOPT 128163969Seric 128263969Seric /* 128363969Seric * Copyright (c) 1985 Regents of the University of California. 128463969Seric * All rights reserved. The Berkeley software License Agreement 128563969Seric * specifies the terms and conditions for redistribution. 128663969Seric */ 128763969Seric 128863969Seric 128963969Seric /* 129063969Seric ** this version hacked to add `atend' flag to allow state machine 129163969Seric ** to reset if invoked by the program to scan args for a 2nd time 129263969Seric */ 129363969Seric 129463969Seric #if defined(LIBC_SCCS) && !defined(lint) 129563969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 129664072Seric #endif /* LIBC_SCCS and not lint */ 129763969Seric 129863969Seric #include <stdio.h> 129963969Seric 130063969Seric /* 130163969Seric * get option letter from argument vector 130263969Seric */ 130365352Seric #ifdef _CONVEX_SOURCE 130465352Seric extern int optind, opterr; 130565352Seric #else 130665352Seric int opterr = 1; /* if error message should be printed */ 130765352Seric int optind = 1; /* index into parent argv vector */ 130865352Seric #endif 130965352Seric int optopt; /* character checked for validity */ 131063969Seric char *optarg; /* argument associated with option */ 131163969Seric 131263969Seric #define BADCH (int)'?' 131363969Seric #define EMSG "" 131463969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 131563969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 131663969Seric 131763969Seric getopt(nargc,nargv,ostr) 131865160Seric int nargc; 131965160Seric char *const *nargv; 132065160Seric const char *ostr; 132163969Seric { 132263969Seric static char *place = EMSG; /* option letter processing */ 132363969Seric static char atend = 0; 132463969Seric register char *oli; /* option letter list index */ 132563969Seric 132663969Seric if (atend) { 132763969Seric atend = 0; 132863969Seric place = EMSG; 132963969Seric } 133063969Seric if(!*place) { /* update scanning pointer */ 133164136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 133263969Seric atend++; 133363969Seric return(EOF); 133463969Seric } 133563969Seric if (*place == '-') { /* found "--" */ 133663969Seric ++optind; 133763969Seric atend++; 133863969Seric return(EOF); 133963969Seric } 134063969Seric } /* option letter okay? */ 134164125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 134264136Seric if (!*place) ++optind; 134363969Seric tell(": illegal option -- "); 134463969Seric } 134563969Seric if (*++oli != ':') { /* don't need argument */ 134663969Seric optarg = NULL; 134763969Seric if (!*place) ++optind; 134863969Seric } 134963969Seric else { /* need an argument */ 135063969Seric if (*place) optarg = place; /* no white space */ 135163969Seric else if (nargc <= ++optind) { /* no arg */ 135263969Seric place = EMSG; 135363969Seric tell(": option requires an argument -- "); 135463969Seric } 135563969Seric else optarg = nargv[optind]; /* white space */ 135663969Seric place = EMSG; 135763969Seric ++optind; 135863969Seric } 135963969Seric return(optopt); /* dump back option letter */ 136063969Seric } 136163969Seric 136263969Seric #endif 136363969Seric /* 136463969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 136563969Seric */ 136663969Seric 136763969Seric #ifdef NEEDVPRINTF 136863969Seric 136963969Seric #define MAXARG 16 137063969Seric 137163969Seric vfprintf(fp, fmt, ap) 137263969Seric FILE * fp; 137363969Seric char * fmt; 137463969Seric char ** ap; 137563969Seric { 137663969Seric char * bp[MAXARG]; 137763969Seric int i = 0; 137863969Seric 137963969Seric while (*ap && i < MAXARG) 138063969Seric bp[i++] = *ap++; 138163969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 138263969Seric bp[4], bp[5], bp[6], bp[7], 138363969Seric bp[8], bp[9], bp[10], bp[11], 138463969Seric bp[12], bp[13], bp[14], bp[15]); 138563969Seric } 138663969Seric 138763969Seric vsprintf(s, fmt, ap) 138863969Seric char * s; 138963969Seric char * fmt; 139063969Seric char ** ap; 139163969Seric { 139263969Seric char * bp[MAXARG]; 139363969Seric int i = 0; 139463969Seric 139563969Seric while (*ap && i < MAXARG) 139663969Seric bp[i++] = *ap++; 139763969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 139863969Seric bp[4], bp[5], bp[6], bp[7], 139963969Seric bp[8], bp[9], bp[10], bp[11], 140063969Seric bp[12], bp[13], bp[14], bp[15]); 140163969Seric } 140263969Seric 140363969Seric #endif 140463969Seric /* 140565220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 140665208Seric ** 140765211Seric ** Parameters: 140865211Seric ** shell -- the user's shell from /etc/passwd 140965211Seric ** 141065211Seric ** Returns: 141165211Seric ** TRUE -- if it is ok to use this for unrestricted access. 141265211Seric ** FALSE -- if the shell is restricted. 141365208Seric */ 141465208Seric 141565587Seric #if !HASGETUSERSHELL 141665587Seric 141765587Seric # ifndef _PATH_SHELLS 141865587Seric # define _PATH_SHELLS "/etc/shells" 141965587Seric # endif 142065587Seric 142165587Seric char *DefaultUserShells[] = 142265587Seric { 142365587Seric "/bin/sh", 142465587Seric "/usr/bin/sh", 142565587Seric "/bin/csh", 142665587Seric "/usr/bin/csh", 142765587Seric #ifdef __hpux 142865587Seric "/bin/rsh", 142965587Seric "/bin/ksh", 143065587Seric "/bin/rksh", 143165587Seric "/bin/pam", 143265587Seric "/usr/bin/keysh", 143365587Seric "/bin/posix/sh", 143465211Seric #endif 143565587Seric NULL 143665587Seric }; 143765587Seric 143865208Seric #endif 143965208Seric 144065747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 144165747Seric 144265211Seric bool 144365211Seric usershellok(shell) 144465211Seric char *shell; 144565208Seric { 144665211Seric #if HASGETUSERSHELL 144765211Seric register char *p; 144865211Seric extern char *getusershell(); 144965208Seric 145065211Seric setusershell(); 145165211Seric while ((p = getusershell()) != NULL) 145265747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 145365211Seric break; 145465211Seric endusershell(); 145565211Seric return p != NULL; 145665211Seric #else 145765211Seric register FILE *shellf; 145865211Seric char buf[MAXLINE]; 145965208Seric 146065211Seric shellf = fopen(_PATH_SHELLS, "r"); 146165211Seric if (shellf == NULL) 146265211Seric { 146365211Seric /* no /etc/shells; see if it is one of the std shells */ 146465587Seric char **d; 146565587Seric 146665587Seric for (d = DefaultUserShells; *d != NULL; d++) 146765587Seric { 146865587Seric if (strcmp(shell, *d) == 0) 146965587Seric return TRUE; 147065587Seric } 147165587Seric return FALSE; 147265211Seric } 147365208Seric 147465211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 147565211Seric { 147665211Seric register char *p, *q; 147765208Seric 147865211Seric p = buf; 147965211Seric while (*p != '\0' && *p != '#' && *p != '/') 148065211Seric p++; 148165211Seric if (*p == '#' || *p == '\0') 148265208Seric continue; 148365211Seric q = p; 148465211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 148565211Seric p++; 148665211Seric *p = '\0'; 148765747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 148865211Seric { 148965211Seric fclose(shellf); 149065211Seric return TRUE; 149165211Seric } 149265208Seric } 149365211Seric fclose(shellf); 149465211Seric return FALSE; 149565211Seric #endif 149665208Seric } 149765208Seric /* 149864311Seric ** FREESPACE -- see how much free space is on the queue filesystem 149958082Seric ** 150058082Seric ** Only implemented if you have statfs. 150158082Seric ** 150258082Seric ** Parameters: 150364311Seric ** dir -- the directory in question. 150464311Seric ** bsize -- a variable into which the filesystem 150564311Seric ** block size is stored. 150658082Seric ** 150758082Seric ** Returns: 150864311Seric ** The number of bytes free on the queue filesystem. 150964311Seric ** -1 if the statfs call fails. 151064311Seric ** 151164311Seric ** Side effects: 151264311Seric ** Puts the filesystem block size into bsize. 151358082Seric */ 151458082Seric 151565749Seric /* statfs types */ 151665749Seric #define SFS_NONE 0 /* no statfs implementation */ 151765749Seric #define SFS_USTAT 1 /* use ustat */ 151865749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 151965749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 152065749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 152165749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 152265749Seric 152365749Seric #ifndef SFS_TYPE 152465749Seric # define SFS_TYPE SFS_NONE 152558157Seric #endif 152658157Seric 152765749Seric #if SFS_TYPE == SFS_USTAT 152858157Seric # include <ustat.h> 152958157Seric #endif 153065749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 153165749Seric # include <sys/statfs.h> 153258082Seric #endif 153365749Seric #if SFS_TYPE == SFS_VFS 153465749Seric # include <sys/vfs.h> 153565749Seric #endif 153665749Seric #if SFS_TYPE == SFS_MOUNT 153765749Seric # include <sys/mount.h> 153865749Seric #endif 153958082Seric 154064311Seric long 154164311Seric freespace(dir, bsize) 154264311Seric char *dir; 154364311Seric long *bsize; 154458082Seric { 154565749Seric #if SFS_TYPE != SFS_NONE 154665749Seric # if SFS_TYPE == SFS_USTAT 154758153Seric struct ustat fs; 154858153Seric struct stat statbuf; 154958366Seric # define FSBLOCKSIZE DEV_BSIZE 155058157Seric # define f_bavail f_tfree 155158157Seric # else 155258157Seric # if defined(ultrix) 155358157Seric struct fs_data fs; 155458157Seric # define f_bavail fd_bfreen 155558366Seric # define FSBLOCKSIZE fs.fd_bsize 155658153Seric # else 155758082Seric struct statfs fs; 155858366Seric # define FSBLOCKSIZE fs.f_bsize 155965820Seric # if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) 156063987Seric # define f_bavail f_bfree 156163987Seric # endif 156258153Seric # endif 156358133Seric # endif 156458082Seric extern int errno; 156558082Seric 156665749Seric # if SFS_TYPE == SFS_USTAT 156764311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 156858157Seric # else 156965749Seric # if SFS_TYPE == SFS_4ARGS 157064311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 157158157Seric # else 157258157Seric # if defined(ultrix) 157364311Seric if (statfs(dir, &fs) > 0) 157458153Seric # else 157564311Seric if (statfs(dir, &fs) == 0) 157658153Seric # endif 157758133Seric # endif 157858133Seric # endif 157958133Seric { 158064311Seric if (bsize != NULL) 158164311Seric *bsize = FSBLOCKSIZE; 158264311Seric return (fs.f_bavail); 158364311Seric } 158464337Seric #endif 158564311Seric return (-1); 158664311Seric } 158764311Seric /* 158864311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 158964311Seric ** 159064311Seric ** Only implemented if you have statfs. 159164311Seric ** 159264311Seric ** Parameters: 159364311Seric ** msize -- the size to check against. If zero, we don't yet 159464311Seric ** know how big the message will be, so just check for 159564311Seric ** a "reasonable" amount. 159664311Seric ** 159764311Seric ** Returns: 159864311Seric ** TRUE if there is enough space. 159964311Seric ** FALSE otherwise. 160064311Seric */ 160164311Seric 160264311Seric bool 160364311Seric enoughspace(msize) 160464311Seric long msize; 160564311Seric { 160664311Seric long bfree, bsize; 160764311Seric 160864311Seric if (MinBlocksFree <= 0 && msize <= 0) 160964311Seric { 161058133Seric if (tTd(4, 80)) 161164311Seric printf("enoughspace: no threshold\n"); 161264311Seric return TRUE; 161364311Seric } 161464311Seric 161564311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 161664311Seric { 161764311Seric if (tTd(4, 80)) 161858333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 161964311Seric bfree, msize); 162058333Seric 162158333Seric /* convert msize to block count */ 162264311Seric msize = msize / bsize + 1; 162358333Seric if (MinBlocksFree >= 0) 162458333Seric msize += MinBlocksFree; 162558333Seric 162664311Seric if (bfree < msize) 162758090Seric { 162858090Seric #ifdef LOG 162958090Seric if (LogLevel > 0) 163064254Seric syslog(LOG_ALERT, 163164254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 163264311Seric CurEnv->e_id, bfree, 163364254Seric CurHostName, msize, QueueDir); 163458090Seric #endif 163558082Seric return FALSE; 163658090Seric } 163758082Seric } 163858082Seric else if (tTd(4, 80)) 163958333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 164058333Seric MinBlocksFree, msize, errstring(errno)); 164158082Seric return TRUE; 164258082Seric } 164358542Seric /* 164458542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 164558542Seric ** 164658542Seric ** This looks at an errno value and tells if this is likely to 164758542Seric ** go away if retried later. 164858542Seric ** 164958542Seric ** Parameters: 165058542Seric ** err -- the errno code to classify. 165158542Seric ** 165258542Seric ** Returns: 165358542Seric ** TRUE if this is probably transient. 165458542Seric ** FALSE otherwise. 165558542Seric */ 165658542Seric 165758542Seric bool 165858542Seric transienterror(err) 165958542Seric int err; 166058542Seric { 166158542Seric switch (err) 166258542Seric { 166358542Seric case EIO: /* I/O error */ 166458542Seric case ENXIO: /* Device not configured */ 166558542Seric case EAGAIN: /* Resource temporarily unavailable */ 166658542Seric case ENOMEM: /* Cannot allocate memory */ 166758542Seric case ENODEV: /* Operation not supported by device */ 166858542Seric case ENFILE: /* Too many open files in system */ 166958542Seric case EMFILE: /* Too many open files */ 167058542Seric case ENOSPC: /* No space left on device */ 167158542Seric #ifdef ETIMEDOUT 167258542Seric case ETIMEDOUT: /* Connection timed out */ 167358542Seric #endif 167458542Seric #ifdef ESTALE 167558542Seric case ESTALE: /* Stale NFS file handle */ 167658542Seric #endif 167758542Seric #ifdef ENETDOWN 167858542Seric case ENETDOWN: /* Network is down */ 167958542Seric #endif 168058542Seric #ifdef ENETUNREACH 168158542Seric case ENETUNREACH: /* Network is unreachable */ 168258542Seric #endif 168358542Seric #ifdef ENETRESET 168458542Seric case ENETRESET: /* Network dropped connection on reset */ 168558542Seric #endif 168658542Seric #ifdef ECONNABORTED 168758542Seric case ECONNABORTED: /* Software caused connection abort */ 168858542Seric #endif 168958542Seric #ifdef ECONNRESET 169058542Seric case ECONNRESET: /* Connection reset by peer */ 169158542Seric #endif 169258542Seric #ifdef ENOBUFS 169358542Seric case ENOBUFS: /* No buffer space available */ 169458542Seric #endif 169558542Seric #ifdef ESHUTDOWN 169658542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 169758542Seric #endif 169858542Seric #ifdef ECONNREFUSED 169958542Seric case ECONNREFUSED: /* Connection refused */ 170058542Seric #endif 170158542Seric #ifdef EHOSTDOWN 170258542Seric case EHOSTDOWN: /* Host is down */ 170358542Seric #endif 170458542Seric #ifdef EHOSTUNREACH 170558542Seric case EHOSTUNREACH: /* No route to host */ 170658542Seric #endif 170758542Seric #ifdef EDQUOT 170858542Seric case EDQUOT: /* Disc quota exceeded */ 170958542Seric #endif 171058542Seric #ifdef EPROCLIM 171158542Seric case EPROCLIM: /* Too many processes */ 171258542Seric #endif 171358542Seric #ifdef EUSERS 171458542Seric case EUSERS: /* Too many users */ 171558542Seric #endif 171658542Seric #ifdef EDEADLK 171758542Seric case EDEADLK: /* Resource deadlock avoided */ 171858542Seric #endif 171958542Seric #ifdef EISCONN 172058542Seric case EISCONN: /* Socket already connected */ 172158542Seric #endif 172258542Seric #ifdef EINPROGRESS 172358542Seric case EINPROGRESS: /* Operation now in progress */ 172458542Seric #endif 172558542Seric #ifdef EALREADY 172658542Seric case EALREADY: /* Operation already in progress */ 172758542Seric #endif 172858542Seric #ifdef EADDRINUSE 172958542Seric case EADDRINUSE: /* Address already in use */ 173058542Seric #endif 173158542Seric #ifdef EADDRNOTAVAIL 173258542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 173358542Seric #endif 173466018Seric #ifdef ETXTBSY 173566017Seric case ETXTBSY: /* (Apollo) file locked */ 173666017Seric #endif 173763834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 173858542Seric case ENOSR: /* Out of streams resources */ 173958542Seric #endif 174058542Seric return TRUE; 174158542Seric } 174258542Seric 174358542Seric /* nope, must be permanent */ 174458542Seric return FALSE; 174558542Seric } 174658689Seric /* 174764035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 174858689Seric ** 174958689Seric ** Parameters: 175058689Seric ** fd -- the file descriptor of the file. 175158689Seric ** filename -- the file name (for error messages). 175264335Seric ** ext -- the filename extension. 175358689Seric ** type -- type of the lock. Bits can be: 175458689Seric ** LOCK_EX -- exclusive lock. 175558689Seric ** LOCK_NB -- non-blocking. 175658689Seric ** 175758689Seric ** Returns: 175858689Seric ** TRUE if the lock was acquired. 175958689Seric ** FALSE otherwise. 176058689Seric */ 176158689Seric 176258689Seric bool 176364335Seric lockfile(fd, filename, ext, type) 176458689Seric int fd; 176558689Seric char *filename; 176664335Seric char *ext; 176758689Seric int type; 176858689Seric { 176965830Seric # if !HASFLOCK 177058689Seric int action; 177158689Seric struct flock lfd; 177264335Seric 177364335Seric if (ext == NULL) 177464335Seric ext = ""; 177564035Seric 177663983Seric bzero(&lfd, sizeof lfd); 177759447Seric if (bitset(LOCK_UN, type)) 177859447Seric lfd.l_type = F_UNLCK; 177959447Seric else if (bitset(LOCK_EX, type)) 178058689Seric lfd.l_type = F_WRLCK; 178158689Seric else 178258689Seric lfd.l_type = F_RDLCK; 178358689Seric 178458689Seric if (bitset(LOCK_NB, type)) 178558689Seric action = F_SETLK; 178658689Seric else 178758689Seric action = F_SETLKW; 178858689Seric 178964264Seric if (tTd(55, 60)) 179064335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 179164335Seric filename, ext, action, lfd.l_type); 179264264Seric 179358689Seric if (fcntl(fd, action, &lfd) >= 0) 179464264Seric { 179564264Seric if (tTd(55, 60)) 179664264Seric printf("SUCCESS\n"); 179758689Seric return TRUE; 179864264Seric } 179958689Seric 180064264Seric if (tTd(55, 60)) 180164264Seric printf("(%s) ", errstring(errno)); 180264264Seric 180364136Seric /* 180464136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 180564136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 180664136Seric ** as type "tmp" (that is, served from swap space), the 180764136Seric ** previous fcntl will fail with "Invalid argument" errors. 180864136Seric ** Since this is fairly common during testing, we will assume 180964136Seric ** that this indicates that the lock is successfully grabbed. 181064136Seric */ 181164136Seric 181264136Seric if (errno == EINVAL) 181364264Seric { 181464264Seric if (tTd(55, 60)) 181564264Seric printf("SUCCESS\n"); 181664136Seric return TRUE; 181764264Seric } 181864136Seric 181958689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 182064378Seric { 182164378Seric int omode = -1; 182264378Seric # ifdef F_GETFL 182364378Seric int oerrno = errno; 182464378Seric 182564378Seric (void) fcntl(fd, F_GETFL, &omode); 182664378Seric errno = oerrno; 182764378Seric # endif 182864378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 182964378Seric filename, ext, fd, type, omode, geteuid()); 183064378Seric } 183158689Seric # else 183264335Seric if (ext == NULL) 183364335Seric ext = ""; 183464335Seric 183564264Seric if (tTd(55, 60)) 183664335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 183764264Seric 183858689Seric if (flock(fd, type) >= 0) 183964264Seric { 184064264Seric if (tTd(55, 60)) 184164264Seric printf("SUCCESS\n"); 184258689Seric return TRUE; 184364264Seric } 184458689Seric 184564264Seric if (tTd(55, 60)) 184664264Seric printf("(%s) ", errstring(errno)); 184764264Seric 184858689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 184964378Seric { 185064378Seric int omode = -1; 185164378Seric # ifdef F_GETFL 185264378Seric int oerrno = errno; 185364378Seric 185464378Seric (void) fcntl(fd, F_GETFL, &omode); 185564378Seric errno = oerrno; 185664378Seric # endif 185764378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 185864378Seric filename, ext, fd, type, omode, geteuid()); 185964386Seric } 186058689Seric # endif 186164264Seric if (tTd(55, 60)) 186264264Seric printf("FAIL\n"); 186358689Seric return FALSE; 186458689Seric } 186564556Seric /* 186665948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 186765948Seric ** 186865948Seric ** Parameters: 186965948Seric ** fd -- the file descriptor to check. 187065948Seric ** 187165948Seric ** Returns: 187265948Seric ** TRUE -- if only root can chown the file to an arbitrary 187365948Seric ** user. 187465948Seric ** FALSE -- if an arbitrary user can give away a file. 187565948Seric */ 187665948Seric 187765948Seric bool 187865948Seric chownsafe(fd) 187965948Seric int fd; 188065948Seric { 188165950Seric #ifdef __hpux 188265948Seric char *s; 188365948Seric int tfd; 188465948Seric uid_t o_uid, o_euid; 188565948Seric gid_t o_gid, o_egid; 188665948Seric bool rval; 188765948Seric struct stat stbuf; 188865948Seric 188965948Seric o_uid = getuid(); 189065948Seric o_euid = geteuid(); 189165948Seric o_gid = getgid(); 189265948Seric o_egid = getegid(); 189365959Seric fstat(fd, &stbuf); 189465948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 189565948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 189665948Seric s = tmpnam(NULL); 189765948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 189865974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 189965948Seric close(tfd); 190065948Seric unlink(s); 190165948Seric setreuid(o_uid, o_euid); 190265948Seric setresgid(o_gid, o_egid, -1); 190365948Seric return rval; 190465948Seric #else 190566088Seric # ifdef _POSIX_CHOWN_RESTRICTED 190666088Seric # if _POSIX_CHOWN_RESTRICTED == -1 190766088Seric return FALSE; 190866088Seric # else 190966088Seric return TRUE; 191066088Seric # endif 191166088Seric # else 191266088Seric # ifdef _PC_CHOWN_RESTRICTED 191365948Seric return fpathconf(fd, _PC_CHOWN_RESTRICTED) > 0; 191466088Seric # else 191566088Seric # ifdef BSD 191665948Seric return TRUE; 191766088Seric # else 191865948Seric return FALSE; 191966088Seric # endif 192065948Seric # endif 192165948Seric # endif 192265948Seric #endif 192365948Seric } 192465948Seric /* 192564556Seric ** GETCFNAME -- return the name of the .cf file. 192664556Seric ** 192764556Seric ** Some systems (e.g., NeXT) determine this dynamically. 192864556Seric */ 192964556Seric 193064556Seric char * 193164556Seric getcfname() 193264556Seric { 193364556Seric if (ConfFile != NULL) 193464556Seric return ConfFile; 193565214Seric #ifdef NETINFO 193665214Seric { 193765214Seric extern char *ni_propval(); 193865214Seric char *cflocation; 193965214Seric 194065214Seric cflocation = ni_propval("/locations/sendmail", "sendmail.cf"); 194165214Seric if (cflocation != NULL) 194265214Seric return cflocation; 194365214Seric } 194465214Seric #endif 194564556Seric return _PATH_SENDMAILCF; 194664556Seric } 194764718Seric /* 194864718Seric ** SETVENDOR -- process vendor code from V configuration line 194964718Seric ** 195064718Seric ** Parameters: 195164718Seric ** vendor -- string representation of vendor. 195264718Seric ** 195364718Seric ** Returns: 195464718Seric ** TRUE -- if ok. 195564718Seric ** FALSE -- if vendor code could not be processed. 195664926Seric ** 195764926Seric ** Side Effects: 195864926Seric ** It is reasonable to set mode flags here to tweak 195964926Seric ** processing in other parts of the code if necessary. 196064926Seric ** For example, if you are a vendor that uses $%y to 196164926Seric ** indicate YP lookups, you could enable that here. 196264718Seric */ 196364718Seric 196464718Seric bool 196564718Seric setvendor(vendor) 196664718Seric char *vendor; 196764718Seric { 196864926Seric if (strcasecmp(vendor, "Berkeley") == 0) 196964926Seric return TRUE; 197064926Seric 197164926Seric /* add vendor extensions here */ 197264926Seric 197364926Seric return FALSE; 197464718Seric } 197564816Seric /* 197664816Seric ** STRTOL -- convert string to long integer 197764816Seric ** 197864816Seric ** For systems that don't have it in the C library. 1979*66161Seric ** 1980*66161Seric ** This is taken verbatim from the 4.4-Lite C library. 198164816Seric */ 198264816Seric 198364816Seric #ifdef NEEDSTRTOL 198464816Seric 1985*66161Seric #if defined(LIBC_SCCS) && !defined(lint) 1986*66161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 1987*66161Seric #endif /* LIBC_SCCS and not lint */ 1988*66161Seric 1989*66161Seric #include <limits.h> 1990*66161Seric 1991*66161Seric /* 1992*66161Seric * Convert a string to a long integer. 1993*66161Seric * 1994*66161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 1995*66161Seric * alphabets and digits are each contiguous. 1996*66161Seric */ 1997*66161Seric 199864816Seric long 1999*66161Seric strtol(nptr, endptr, base) 2000*66161Seric const char *nptr; 2001*66161Seric char **endptr; 2002*66161Seric register int base; 200364816Seric { 2004*66161Seric register const char *s = nptr; 2005*66161Seric register unsigned long acc; 2006*66161Seric register int c; 2007*66161Seric register unsigned long cutoff; 2008*66161Seric register int neg = 0, any, cutlim; 200964816Seric 2010*66161Seric /* 2011*66161Seric * Skip white space and pick up leading +/- sign if any. 2012*66161Seric * If base is 0, allow 0x for hex and 0 for octal, else 2013*66161Seric * assume decimal; if base is already 16, allow 0x. 2014*66161Seric */ 2015*66161Seric do { 2016*66161Seric c = *s++; 2017*66161Seric } while (isspace(c)); 2018*66161Seric if (c == '-') { 2019*66161Seric neg = 1; 2020*66161Seric c = *s++; 2021*66161Seric } else if (c == '+') 2022*66161Seric c = *s++; 2023*66161Seric if ((base == 0 || base == 16) && 2024*66161Seric c == '0' && (*s == 'x' || *s == 'X')) { 2025*66161Seric c = s[1]; 2026*66161Seric s += 2; 2027*66161Seric base = 16; 2028*66161Seric } 2029*66161Seric if (base == 0) 2030*66161Seric base = c == '0' ? 8 : 10; 203164816Seric 2032*66161Seric /* 2033*66161Seric * Compute the cutoff value between legal numbers and illegal 2034*66161Seric * numbers. That is the largest legal value, divided by the 2035*66161Seric * base. An input number that is greater than this value, if 2036*66161Seric * followed by a legal input character, is too big. One that 2037*66161Seric * is equal to this value may be valid or not; the limit 2038*66161Seric * between valid and invalid numbers is then based on the last 2039*66161Seric * digit. For instance, if the range for longs is 2040*66161Seric * [-2147483648..2147483647] and the input base is 10, 2041*66161Seric * cutoff will be set to 214748364 and cutlim to either 2042*66161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 2043*66161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 2044*66161Seric * the number is too big, and we will return a range error. 2045*66161Seric * 2046*66161Seric * Set any if any `digits' consumed; make it negative to indicate 2047*66161Seric * overflow. 2048*66161Seric */ 2049*66161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 2050*66161Seric cutlim = cutoff % (unsigned long)base; 2051*66161Seric cutoff /= (unsigned long)base; 2052*66161Seric for (acc = 0, any = 0;; c = *s++) { 2053*66161Seric if (isdigit(c)) 2054*66161Seric c -= '0'; 2055*66161Seric else if (isalpha(c)) 2056*66161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 2057*66161Seric else 2058*66161Seric break; 2059*66161Seric if (c >= base) 2060*66161Seric break; 2061*66161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 2062*66161Seric any = -1; 2063*66161Seric else { 2064*66161Seric any = 1; 2065*66161Seric acc *= base; 2066*66161Seric acc += c; 206764816Seric } 206864816Seric } 2069*66161Seric if (any < 0) { 2070*66161Seric acc = neg ? LONG_MIN : LONG_MAX; 2071*66161Seric errno = ERANGE; 2072*66161Seric } else if (neg) 2073*66161Seric acc = -acc; 2074*66161Seric if (endptr != 0) 2075*66161Seric *endptr = (char *)(any ? s - 1 : nptr); 2076*66161Seric return (acc); 207764816Seric } 207864816Seric 207964816Seric #endif 208064841Seric /* 208164841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 208264841Seric ** 208364841Seric ** Solaris versions prior through 2.3 don't properly deliver a 208464841Seric ** canonical h_name field. This tries to work around it. 208564841Seric */ 208664841Seric 208764841Seric #ifdef SOLARIS 208864841Seric 208964841Seric struct hostent * 209064841Seric solaris_gethostbyname(name) 209165094Seric const char *name; 209264841Seric { 209364841Seric # ifdef SOLARIS_2_3 209464841Seric static struct hostent hp; 209564841Seric static char buf[1000]; 209664841Seric extern struct hostent *_switch_gethostbyname_r(); 209764841Seric 209864841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 209964841Seric # else 210065009Seric extern struct hostent *__switch_gethostbyname(); 210165009Seric 210264841Seric return __switch_gethostbyname(name); 210364841Seric # endif 210464841Seric } 210564841Seric 210664841Seric struct hostent * 210764841Seric solaris_gethostbyaddr(addr, len, type) 210865094Seric const char *addr; 210964841Seric int len; 211064841Seric int type; 211164841Seric { 211264841Seric # ifdef SOLARIS_2_3 211364841Seric static struct hostent hp; 211464841Seric static char buf[1000]; 211564841Seric extern struct hostent *_switch_gethostbyaddr_r(); 211664841Seric 211764841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 211864841Seric # else 211965009Seric extern struct hostent *__switch_gethostbyaddr(); 212065009Seric 212164841Seric return __switch_gethostbyaddr(addr, len, type); 212264841Seric # endif 212364841Seric } 212464841Seric 212564841Seric #endif 212665214Seric /* 212765214Seric ** NI_PROPVAL -- netinfo property value lookup routine 212865214Seric ** 212965214Seric ** Parameters: 213065214Seric ** directory -- the Netinfo directory name. 213165214Seric ** propname -- the Netinfo property name. 213265214Seric ** 213365214Seric ** Returns: 213465214Seric ** NULL -- if: 213565214Seric ** 1. the directory is not found 213665214Seric ** 2. the property name is not found 213765214Seric ** 3. the property contains multiple values 213865214Seric ** 4. some error occured 213965214Seric ** else -- the location of the config file. 214065214Seric ** 214165214Seric ** Notes: 214265214Seric ** Caller should free the return value of ni_proval 214365214Seric */ 214465214Seric 214565214Seric #ifdef NETINFO 214665214Seric 214765214Seric # include <netinfo/ni.h> 214865214Seric 214965214Seric # define LOCAL_NETINFO_DOMAIN "." 215065214Seric # define PARENT_NETINFO_DOMAIN ".." 215165214Seric # define MAX_NI_LEVELS 256 215265214Seric 215365214Seric char * 215465214Seric ni_propval(directory, propname) 215565214Seric char *directory; 215665214Seric char *propname; 215765214Seric { 215865820Seric char *propval = NULL; 215965214Seric int i; 216065214Seric void *ni = NULL; 216165214Seric void *lastni = NULL; 216265214Seric ni_status nis; 216365214Seric ni_id nid; 216465214Seric ni_namelist ninl; 216565214Seric 216665214Seric /* 216765214Seric ** If the passed directory and property name are found 216865214Seric ** in one of netinfo domains we need to search (starting 216965214Seric ** from the local domain moving all the way back to the 217065214Seric ** root domain) set propval to the property's value 217165214Seric ** and return it. 217265214Seric */ 217365214Seric 217465214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 217565214Seric { 217665214Seric if (i == 0) 217765214Seric { 217865214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 217965214Seric } 218065214Seric else 218165214Seric { 218265214Seric if (lastni != NULL) 218365214Seric ni_free(lastni); 218465214Seric lastni = ni; 218565214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 218665214Seric } 218765214Seric 218865214Seric /* 218965214Seric ** Don't bother if we didn't get a handle on a 219065214Seric ** proper domain. This is not necessarily an error. 219165214Seric ** We would get a positive ni_status if, for instance 219265214Seric ** we never found the directory or property and tried 219365214Seric ** to open the parent of the root domain! 219465214Seric */ 219565214Seric 219665214Seric if (nis != 0) 219765214Seric break; 219865214Seric 219965214Seric /* 220065214Seric ** Find the path to the server information. 220165214Seric */ 220265214Seric 220365214Seric if (ni_pathsearch(ni, &nid, directory) != 0) 220465214Seric continue; 220565214Seric 220665214Seric /* 220765214Seric ** Find "host" information. 220865214Seric */ 220965214Seric 221065214Seric if (ni_lookupprop(ni, &nid, propname, &ninl) != 0) 221165214Seric continue; 221265214Seric 221365214Seric /* 221465214Seric ** If there's only one name in 221565214Seric ** the list, assume we've got 221665214Seric ** what we want. 221765214Seric */ 221865214Seric 221965214Seric if (ninl.ni_namelist_len == 1) 222065214Seric { 222165214Seric propval = ni_name_dup(ninl.ni_namelist_val[0]); 222265214Seric break; 222365214Seric } 222465214Seric } 222565214Seric 222665214Seric /* 222765214Seric ** Clean up. 222865214Seric */ 222965214Seric 223065214Seric if (ni != NULL) 223165214Seric ni_free(ni); 223265214Seric if (lastni != NULL && ni != lastni) 223365214Seric ni_free(lastni); 223465214Seric 223565214Seric return propval; 223665214Seric } 223765214Seric 223865214Seric #endif /* NETINFO */ 223965504Seric /* 224065504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 224165504Seric ** 224265504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 224365504Seric ** syslog succeeds during interrupt handlers. 224465504Seric */ 224565504Seric 224665504Seric #ifdef __hpux 224765504Seric 224865504Seric # define MAXSYSLOGTRIES 100 224965504Seric # undef syslog 225065504Seric 225165504Seric # ifdef __STDC__ 225265504Seric hard_syslog(int pri, char *msg, ...) 225365504Seric # else 225465504Seric hard_syslog(pri, msg, va_alist) 225565504Seric int pri; 225665504Seric char *msg; 225765504Seric va_dcl 225865504Seric # endif 225965504Seric { 226065504Seric int i; 226165504Seric char buf[SYSLOG_BUFSIZE * 2]; 226265504Seric VA_LOCAL_DECL; 226365504Seric 226465504Seric VA_START(msg); 226565504Seric vsprintf(buf, msg, ap); 226665504Seric VA_END; 226765504Seric 226865504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 226965504Seric continue; 227065504Seric } 227165504Seric 227265504Seric #endif 2273