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*67626Seric static char sccsid[] = "@(#)conf.c 8.100 (Berkeley) 08/09/94"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1363937Seric # include "sendmail.h" 1463937Seric # include "pathnames.h" 1514881Seric # include <sys/ioctl.h> 1658082Seric # include <sys/param.h> 1765018Seric # include <netdb.h> 1836928Sbostic # include <pwd.h> 19404Seric 20294Seric /* 213309Seric ** CONF.C -- Sendmail Configuration Tables. 22294Seric ** 23294Seric ** Defines the configuration of this installation. 24294Seric ** 251388Seric ** Configuration Variables: 262897Seric ** HdrInfo -- a table describing well-known header fields. 272897Seric ** Each entry has the field name and some flags, 284147Seric ** which are described in sendmail.h. 294093Seric ** 304093Seric ** Notes: 314093Seric ** I have tried to put almost all the reasonable 324093Seric ** configuration information into the configuration 334093Seric ** file read at runtime. My intent is that anything 344093Seric ** here is a function of the version of UNIX you 354093Seric ** are running, or is really static -- for example 364093Seric ** the headers are a superset of widely used 374093Seric ** protocols. If you find yourself playing with 384093Seric ** this file too much, you may be making a mistake! 39294Seric */ 40294Seric 41294Seric 42294Seric 43294Seric 444437Seric /* 452897Seric ** Header info table 463057Seric ** Final (null) entry contains the flags used for any other field. 474147Seric ** 484147Seric ** Not all of these are actually handled specially by sendmail 494147Seric ** at this time. They are included as placeholders, to let 504147Seric ** you know that "someday" I intend to have sendmail do 514147Seric ** something with them. 522897Seric */ 532897Seric 542897Seric struct hdrinfo HdrInfo[] = 552897Seric { 568060Seric /* originator fields, most to least significant */ 5711417Seric "resent-sender", H_FROM|H_RESENT, 5811417Seric "resent-from", H_FROM|H_RESENT, 5925686Seric "resent-reply-to", H_FROM|H_RESENT, 609055Seric "sender", H_FROM, 619055Seric "from", H_FROM, 6225686Seric "reply-to", H_FROM, 639055Seric "full-name", H_ACHECK, 6464682Seric "return-receipt-to", H_FROM|H_RECEIPTTO, 6557359Seric "errors-to", H_FROM|H_ERRORSTO, 6658796Seric 678060Seric /* destination fields */ 689055Seric "to", H_RCPT, 6911417Seric "resent-to", H_RCPT|H_RESENT, 709055Seric "cc", H_RCPT, 7111417Seric "resent-cc", H_RCPT|H_RESENT, 729055Seric "bcc", H_RCPT|H_ACHECK, 7311417Seric "resent-bcc", H_RCPT|H_ACHECK|H_RESENT, 7456215Seric "apparently-to", H_RCPT, 7558796Seric 768060Seric /* message identification and control */ 7711417Seric "message-id", 0, 7811417Seric "resent-message-id", H_RESENT, 799055Seric "message", H_EOH, 809055Seric "text", H_EOH, 8158796Seric 8211417Seric /* date fields */ 8311417Seric "date", 0, 8411417Seric "resent-date", H_RESENT, 8558796Seric 868060Seric /* trace fields */ 879055Seric "received", H_TRACE|H_FORCE, 8859318Seric "x400-received", H_TRACE|H_FORCE, 899055Seric "via", H_TRACE|H_FORCE, 909055Seric "mail-from", H_TRACE|H_FORCE, 918060Seric 9258796Seric /* miscellaneous fields */ 9358796Seric "comments", H_FORCE, 9463753Seric "return-path", H_FORCE|H_ACHECK, 9558796Seric 969055Seric NULL, 0, 972897Seric }; 984166Seric 994166Seric 1004166Seric 1014282Seric /* 1024282Seric ** Location of system files/databases/etc. 1034282Seric */ 1044282Seric 10558082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1069039Seric 1079064Seric 1089064Seric 1099039Seric /* 11058082Seric ** Privacy values 11158082Seric */ 11258082Seric 11358082Seric struct prival PrivacyValues[] = 11458082Seric { 11558082Seric "public", PRIV_PUBLIC, 11658082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11758114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11858082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 11958082Seric "noexpn", PRIV_NOEXPN, 12058082Seric "novrfy", PRIV_NOVRFY, 12164333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12264333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12358789Seric "authwarnings", PRIV_AUTHWARNINGS, 12466783Seric "noreceipts", PRIV_NORECEIPTS, 12558082Seric "goaway", PRIV_GOAWAY, 12658789Seric NULL, 0, 12758082Seric }; 12858082Seric 12958082Seric 13058082Seric 13158082Seric /* 13224943Seric ** Miscellaneous stuff. 1339039Seric */ 1349039Seric 13524943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13624943Seric /* 13724943Seric ** SETDEFAULTS -- set default values 13824943Seric ** 13924943Seric ** Because of the way freezing is done, these must be initialized 14024943Seric ** using direct code. 14124943Seric ** 14224943Seric ** Parameters: 14358734Seric ** e -- the default envelope. 14424943Seric ** 14524943Seric ** Returns: 14624943Seric ** none. 14724943Seric ** 14824943Seric ** Side Effects: 14924943Seric ** Initializes a bunch of global variables to their 15024943Seric ** default values. 15124943Seric */ 15224943Seric 15358737Seric #define DAYS * 24 * 60 * 60 15458737Seric 15558734Seric setdefaults(e) 15658734Seric register ENVELOPE *e; 15724943Seric { 15857438Seric SpaceSub = ' '; /* option B */ 15957438Seric QueueLA = 8; /* option x */ 16057438Seric RefuseLA = 12; /* option X */ 16157438Seric WkRecipFact = 30000L; /* option y */ 16257438Seric WkClassFact = 1800L; /* option z */ 16357438Seric WkTimeFact = 90000L; /* option Z */ 16457438Seric QueueFactor = WkRecipFact * 20; /* option q */ 16563787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 16657438Seric /* option F */ 16757438Seric DefUid = 1; /* option u */ 16857438Seric DefGid = 1; /* option g */ 16957438Seric CheckpointInterval = 10; /* option C */ 17057438Seric MaxHopCount = 25; /* option h */ 17158734Seric e->e_sendmode = SM_FORK; /* option d */ 17258734Seric e->e_errormode = EM_PRINT; /* option e */ 17367546Seric SevenBitInput = FALSE; /* option 7 */ 17457438Seric MaxMciCache = 1; /* option k */ 17557438Seric MciCacheTimeout = 300; /* option K */ 17657438Seric LogLevel = 9; /* option L */ 17758112Seric settimeouts(NULL); /* option r */ 17858737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 17958737Seric TimeOuts.to_q_warning = 0; /* option T */ 18058853Seric PrivacyFlags = 0; /* option p */ 18167546Seric MimeMode = MM_CVTMIME|MM_PASS8BIT; /* option 8 */ 18240973Sbostic setdefuser(); 18353654Seric setupmaps(); 18457402Seric setupmailers(); 18524943Seric } 186294Seric 18740973Sbostic 1884326Seric /* 18940973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 19040973Sbostic */ 19140973Sbostic 19240973Sbostic setdefuser() 19340973Sbostic { 19440973Sbostic struct passwd *defpwent; 19557386Seric static char defuserbuf[40]; 19640973Sbostic 19757386Seric DefUser = defuserbuf; 19840973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 19957386Seric strcpy(defuserbuf, defpwent->pw_name); 20040973Sbostic else 20157386Seric strcpy(defuserbuf, "nobody"); 20240973Sbostic } 20353654Seric /* 20456836Seric ** HOST_MAP_INIT -- initialize host class structures 20556836Seric */ 20656836Seric 20756836Seric bool 20860219Seric host_map_init(map, args) 20956836Seric MAP *map; 21056836Seric char *args; 21156836Seric { 21256836Seric register char *p = args; 21356836Seric 21456836Seric for (;;) 21556836Seric { 21658050Seric while (isascii(*p) && isspace(*p)) 21756836Seric p++; 21856836Seric if (*p != '-') 21956836Seric break; 22056836Seric switch (*++p) 22156836Seric { 22256836Seric case 'a': 22356836Seric map->map_app = ++p; 22456836Seric break; 22556836Seric } 22658050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 22756836Seric p++; 22856836Seric if (*p != '\0') 22956836Seric *p++ = '\0'; 23056836Seric } 23156836Seric if (map->map_app != NULL) 23256836Seric map->map_app = newstr(map->map_app); 23356836Seric return TRUE; 23456836Seric } 23557402Seric /* 23657402Seric ** SETUPMAILERS -- initialize default mailers 23757402Seric */ 23856836Seric 23957402Seric setupmailers() 24057402Seric { 24157402Seric char buf[100]; 24257402Seric 24357403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 24457403Seric makemailer(buf); 24557403Seric 24659883Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE"); 24757402Seric makemailer(buf); 24857402Seric 24957402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 25057402Seric makemailer(buf); 25157402Seric } 25256836Seric /* 25360207Seric ** SETUPMAPS -- set up map classes 25460207Seric */ 25560207Seric 25660207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 25760207Seric { \ 25860207Seric extern bool parse __P((MAP *, char *)); \ 25960207Seric extern bool open __P((MAP *, int)); \ 26060207Seric extern void close __P((MAP *)); \ 26160207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 26260207Seric extern void store __P((MAP *, char *, char *)); \ 26360207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 26460207Seric s->s_mapclass.map_cname = name; \ 26560207Seric s->s_mapclass.map_ext = ext; \ 26660207Seric s->s_mapclass.map_cflags = flags; \ 26760207Seric s->s_mapclass.map_parse = parse; \ 26860207Seric s->s_mapclass.map_open = open; \ 26960207Seric s->s_mapclass.map_close = close; \ 27060207Seric s->s_mapclass.map_lookup = lookup; \ 27160207Seric s->s_mapclass.map_store = store; \ 27260207Seric } 27360207Seric 27460207Seric setupmaps() 27560207Seric { 27660207Seric register STAB *s; 27760207Seric 27860207Seric #ifdef NEWDB 27960207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 28060207Seric map_parseargs, hash_map_open, db_map_close, 28160207Seric db_map_lookup, db_map_store); 28260207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 28360207Seric map_parseargs, bt_map_open, db_map_close, 28460207Seric db_map_lookup, db_map_store); 28560207Seric #endif 28660207Seric 28760207Seric #ifdef NDBM 28860207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 28960207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 29060207Seric ndbm_map_lookup, ndbm_map_store); 29160207Seric #endif 29260207Seric 29360207Seric #ifdef NIS 29460207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 29560207Seric map_parseargs, nis_map_open, nis_map_close, 29660207Seric nis_map_lookup, nis_map_store); 29760207Seric #endif 29860207Seric 29960207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 30060207Seric map_parseargs, stab_map_open, stab_map_close, 30160207Seric stab_map_lookup, stab_map_store); 30260207Seric 30360207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 30460207Seric map_parseargs, impl_map_open, impl_map_close, 30560207Seric impl_map_lookup, impl_map_store); 30660207Seric 30760207Seric /* host DNS lookup */ 30860207Seric MAPDEF("host", NULL, 0, 30960207Seric host_map_init, null_map_open, null_map_close, 31060207Seric host_map_lookup, null_map_store); 31160207Seric 31260207Seric /* dequote map */ 31360207Seric MAPDEF("dequote", NULL, 0, 31460207Seric dequote_init, null_map_open, null_map_close, 31560207Seric dequote_map, null_map_store); 31660207Seric 31760207Seric #if 0 31860207Seric # ifdef USERDB 31960207Seric /* user database */ 32060207Seric MAPDEF("udb", ".db", 0, 32160207Seric udb_map_parse, null_map_open, null_map_close, 32260207Seric udb_map_lookup, null_map_store); 32360207Seric # endif 32460207Seric #endif 32560207Seric } 32660207Seric 32760207Seric #undef MAPDEF 32860207Seric /* 3299369Seric ** USERNAME -- return the user id of the logged in user. 3309369Seric ** 3319369Seric ** Parameters: 3329369Seric ** none. 3339369Seric ** 3349369Seric ** Returns: 3359369Seric ** The login name of the logged in user. 3369369Seric ** 3379369Seric ** Side Effects: 3389369Seric ** none. 3399369Seric ** 3409369Seric ** Notes: 3419369Seric ** The return value is statically allocated. 3429369Seric */ 3439369Seric 3449369Seric char * 3459369Seric username() 3469369Seric { 34717469Seric static char *myname = NULL; 3489369Seric extern char *getlogin(); 34919904Smiriam register struct passwd *pw; 3509369Seric 35117469Seric /* cache the result */ 35217469Seric if (myname == NULL) 35317469Seric { 35417469Seric myname = getlogin(); 35517469Seric if (myname == NULL || myname[0] == '\0') 35617469Seric { 35763787Seric pw = getpwuid(RealUid); 35817469Seric if (pw != NULL) 35940993Sbostic myname = newstr(pw->pw_name); 36017469Seric } 36119904Smiriam else 36219904Smiriam { 36363787Seric uid_t uid = RealUid; 36419873Smiriam 36540993Sbostic myname = newstr(myname); 36640993Sbostic if ((pw = getpwnam(myname)) == NULL || 36758736Seric (uid != 0 && uid != pw->pw_uid)) 36819904Smiriam { 36958736Seric pw = getpwuid(uid); 37024945Seric if (pw != NULL) 37140993Sbostic myname = newstr(pw->pw_name); 37219873Smiriam } 37319873Smiriam } 37417469Seric if (myname == NULL || myname[0] == '\0') 37517469Seric { 37658151Seric syserr("554 Who are you?"); 37717469Seric myname = "postmaster"; 37817469Seric } 37917469Seric } 38017469Seric 38117469Seric return (myname); 3829369Seric } 3839369Seric /* 3844190Seric ** TTYPATH -- Get the path of the user's tty 385294Seric ** 386294Seric ** Returns the pathname of the user's tty. Returns NULL if 387294Seric ** the user is not logged in or if s/he has write permission 388294Seric ** denied. 389294Seric ** 390294Seric ** Parameters: 391294Seric ** none 392294Seric ** 393294Seric ** Returns: 394294Seric ** pathname of the user's tty. 395294Seric ** NULL if not logged in or write permission denied. 396294Seric ** 397294Seric ** Side Effects: 398294Seric ** none. 399294Seric ** 400294Seric ** WARNING: 401294Seric ** Return value is in a local buffer. 402294Seric ** 403294Seric ** Called By: 404294Seric ** savemail 405294Seric */ 406294Seric 407294Seric char * 408294Seric ttypath() 409294Seric { 410294Seric struct stat stbuf; 411294Seric register char *pathn; 412294Seric extern char *ttyname(); 4134081Seric extern char *getlogin(); 414294Seric 415294Seric /* compute the pathname of the controlling tty */ 4169369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4179369Seric (pathn = ttyname(0)) == NULL) 418294Seric { 419294Seric errno = 0; 420294Seric return (NULL); 421294Seric } 422294Seric 423294Seric /* see if we have write permission */ 4242967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 425294Seric { 426294Seric errno = 0; 427294Seric return (NULL); 428294Seric } 429294Seric 430294Seric /* see if the user is logged in */ 431294Seric if (getlogin() == NULL) 432294Seric return (NULL); 433294Seric 434294Seric /* looks good */ 435294Seric return (pathn); 436294Seric } 4372967Seric /* 4382967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4392967Seric ** 4402967Seric ** This routine can be supplied on a per-installation basis 4412967Seric ** to determine whether a person is allowed to send a message. 4422967Seric ** This allows restriction of certain types of internet 4432967Seric ** forwarding or registration of users. 4442967Seric ** 4452967Seric ** If the hosts are found to be incompatible, an error 44657454Seric ** message should be given using "usrerr" and 0 should 4472967Seric ** be returned. 4482967Seric ** 44967473Seric ** EF_NORETURN can be set in e->e_flags to suppress the return-to-sender 4504288Seric ** function; this should be done on huge messages. 4514288Seric ** 4522967Seric ** Parameters: 4532967Seric ** to -- the person being sent to. 4542967Seric ** 4552967Seric ** Returns: 45657459Seric ** an exit status 4572967Seric ** 4582967Seric ** Side Effects: 4592967Seric ** none (unless you include the usrerr stuff) 4602967Seric */ 4612967Seric 46255012Seric checkcompat(to, e) 4632967Seric register ADDRESS *to; 46455012Seric register ENVELOPE *e; 4652967Seric { 46612133Seric # ifdef lint 46712133Seric if (to == NULL) 46812133Seric to++; 46963834Seric # endif /* lint */ 47064402Seric 47164402Seric if (tTd(49, 1)) 47264402Seric printf("checkcompat(to=%s, from=%s)\n", 47364402Seric to->q_paddr, e->e_from.q_paddr); 47464402Seric 47510698Seric # ifdef EXAMPLE_CODE 47610698Seric /* this code is intended as an example only */ 4774437Seric register STAB *s; 4784437Seric 4794437Seric s = stab("arpa", ST_MAILER, ST_FIND); 48067473Seric if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 && 4819369Seric to->q_mailer == s->s_mailer) 4824437Seric { 48358151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 48467473Seric /* e->e_flags |= EF_NORETURN; to supress return copy */ 48557459Seric return (EX_UNAVAILABLE); 4864437Seric } 48756795Seric # endif /* EXAMPLE_CODE */ 48857459Seric return (EX_OK); 4892967Seric } 4909369Seric /* 49164035Seric ** SETSIGNAL -- set a signal handler 49264035Seric ** 49364035Seric ** This is essentially old BSD "signal(3)". 49464035Seric */ 49564035Seric 49664561Seric sigfunc_t 49764035Seric setsignal(sig, handler) 49864035Seric int sig; 49964561Seric sigfunc_t handler; 50064035Seric { 50164380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 50264035Seric return signal(sig, handler); 50364035Seric #else 50464035Seric struct sigaction n, o; 50564035Seric 50664035Seric bzero(&n, sizeof n); 50764035Seric n.sa_handler = handler; 50867159Seric # ifdef SA_RESTART 50967159Seric n.sa_flags = SA_RESTART; 51067159Seric # endif 51164035Seric if (sigaction(sig, &n, &o) < 0) 51264035Seric return SIG_ERR; 51364035Seric return o.sa_handler; 51464035Seric #endif 51564035Seric } 51664035Seric /* 5179369Seric ** HOLDSIGS -- arrange to hold all signals 5189369Seric ** 5199369Seric ** Parameters: 5209369Seric ** none. 5219369Seric ** 5229369Seric ** Returns: 5239369Seric ** none. 5249369Seric ** 5259369Seric ** Side Effects: 5269369Seric ** Arranges that signals are held. 5279369Seric */ 5289369Seric 5299369Seric holdsigs() 5309369Seric { 5319369Seric } 5329369Seric /* 5339369Seric ** RLSESIGS -- arrange to release all signals 5349369Seric ** 5359369Seric ** This undoes the effect of holdsigs. 5369369Seric ** 5379369Seric ** Parameters: 5389369Seric ** none. 5399369Seric ** 5409369Seric ** Returns: 5419369Seric ** none. 5429369Seric ** 5439369Seric ** Side Effects: 5449369Seric ** Arranges that signals are released. 5459369Seric */ 5469369Seric 5479369Seric rlsesigs() 5489369Seric { 5499369Seric } 55014872Seric /* 55164705Seric ** INIT_MD -- do machine dependent initializations 55264705Seric ** 55364705Seric ** Systems that have global modes that should be set should do 55464705Seric ** them here rather than in main. 55564705Seric */ 55664705Seric 55764705Seric #ifdef _AUX_SOURCE 55864705Seric # include <compat.h> 55964705Seric #endif 56064705Seric 56164825Seric init_md(argc, argv) 56264825Seric int argc; 56364825Seric char **argv; 56464705Seric { 56564705Seric #ifdef _AUX_SOURCE 56664705Seric setcompat(getcompat() | COMPAT_BSDPROT); 56764705Seric #endif 56864705Seric } 56964705Seric /* 57014872Seric ** GETLA -- get the current load average 57114872Seric ** 57214881Seric ** This code stolen from la.c. 57314881Seric ** 57414872Seric ** Parameters: 57514872Seric ** none. 57614872Seric ** 57714872Seric ** Returns: 57814872Seric ** The current load average as an integer. 57914872Seric ** 58014872Seric ** Side Effects: 58114872Seric ** none. 58214872Seric */ 58314872Seric 58451920Seric /* try to guess what style of load average we have */ 58551920Seric #define LA_ZERO 1 /* always return load average as zero */ 58664360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 58751920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 58851920Seric #define LA_SUBR 4 /* call getloadavg */ 58964295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 59064360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 59166301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 59214872Seric 59363962Seric /* do guesses based on general OS type */ 59451920Seric #ifndef LA_TYPE 59563962Seric # define LA_TYPE LA_ZERO 59651920Seric #endif 59751920Seric 59864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 59951920Seric 60014872Seric #include <nlist.h> 60114872Seric 60251920Seric #ifndef LA_AVENRUN 60363962Seric # ifdef SYSTEM5 60463962Seric # define LA_AVENRUN "avenrun" 60563962Seric # else 60663962Seric # define LA_AVENRUN "_avenrun" 60763962Seric # endif 60851920Seric #endif 60951920Seric 61051920Seric /* _PATH_UNIX should be defined in <paths.h> */ 61151920Seric #ifndef _PATH_UNIX 61263962Seric # if defined(SYSTEM5) 61363962Seric # define _PATH_UNIX "/unix" 61463962Seric # else 61563962Seric # define _PATH_UNIX "/vmunix" 61663962Seric # endif 61751920Seric #endif 61851920Seric 61914872Seric struct nlist Nl[] = 62014872Seric { 62151920Seric { LA_AVENRUN }, 62214872Seric #define X_AVENRUN 0 62314872Seric { 0 }, 62414872Seric }; 62514872Seric 62660583Seric #ifndef FSHIFT 62760583Seric # if defined(unixpc) 62860583Seric # define FSHIFT 5 62960583Seric # endif 63057736Seric 63166763Seric # if defined(__alpha) || defined(IRIX) 63260583Seric # define FSHIFT 10 63360583Seric # endif 63466788Seric #endif 63557977Seric 63666788Seric #ifndef FSHIFT 63766788Seric # define FSHIFT 8 63857736Seric #endif 63960583Seric 64066788Seric #ifndef FSCALE 64166788Seric # define FSCALE (1 << FSHIFT) 64251920Seric #endif 64340930Srick 64414872Seric getla() 64514872Seric { 64614872Seric static int kmem = -1; 64751920Seric #if LA_TYPE == LA_INT 64824943Seric long avenrun[3]; 64951920Seric #else 65064360Seric # if LA_TYPE == LA_SHORT 65164360Seric short avenrun[3]; 65264360Seric # else 65351920Seric double avenrun[3]; 65464360Seric # endif 65551920Seric #endif 65625615Seric extern off_t lseek(); 65757736Seric extern int errno; 65814872Seric 65914872Seric if (kmem < 0) 66014872Seric { 66124945Seric kmem = open("/dev/kmem", 0, 0); 66214872Seric if (kmem < 0) 66357736Seric { 66457736Seric if (tTd(3, 1)) 66557736Seric printf("getla: open(/dev/kmem): %s\n", 66657736Seric errstring(errno)); 66714872Seric return (-1); 66857736Seric } 66951920Seric (void) fcntl(kmem, F_SETFD, 1); 67057736Seric if (nlist(_PATH_UNIX, Nl) < 0) 67157736Seric { 67257736Seric if (tTd(3, 1)) 67357736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 67457736Seric errstring(errno)); 67514872Seric return (-1); 67657736Seric } 67759253Seric if (Nl[X_AVENRUN].n_value == 0) 67859253Seric { 67959253Seric if (tTd(3, 1)) 68059253Seric printf("getla: nlist(%s, %s) ==> 0\n", 68159253Seric _PATH_UNIX, LA_AVENRUN); 68259253Seric return (-1); 68359253Seric } 68467435Seric #ifdef NAMELISTMASK 68567435Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 68666763Seric #endif 68714872Seric } 68857736Seric if (tTd(3, 20)) 68957736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 69024945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 69123118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 69219967Seric { 69319967Seric /* thank you Ian */ 69457736Seric if (tTd(3, 1)) 69557736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 69619967Seric return (-1); 69719967Seric } 69864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 69957736Seric if (tTd(3, 5)) 70057736Seric { 70157736Seric printf("getla: avenrun = %d", avenrun[0]); 70257736Seric if (tTd(3, 15)) 70357736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 70457736Seric printf("\n"); 70557736Seric } 70657736Seric if (tTd(3, 1)) 70757736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 70824943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 70951920Seric #else 71057736Seric if (tTd(3, 5)) 71157736Seric { 71257736Seric printf("getla: avenrun = %g", avenrun[0]); 71357736Seric if (tTd(3, 15)) 71457736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 71557736Seric printf("\n"); 71657736Seric } 71757736Seric if (tTd(3, 1)) 71857736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 71951920Seric return ((int) (avenrun[0] + 0.5)); 72051920Seric #endif 72114872Seric } 72214872Seric 72351773Seric #else 72451920Seric #if LA_TYPE == LA_SUBR 72551773Seric 72664718Seric #ifdef DGUX 72764718Seric 72864718Seric #include <sys/dg_sys_info.h> 72964718Seric 73064718Seric int getla() 73164718Seric { 73264718Seric struct dg_sys_info_load_info load_info; 73364718Seric 73464718Seric dg_sys_info((long *)&load_info, 73564718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 73664718Seric 73764718Seric return((int) (load_info.one_minute + 0.5)); 73864718Seric } 73964718Seric 74064718Seric #else 74164718Seric 74251773Seric getla() 74351773Seric { 74451920Seric double avenrun[3]; 74551920Seric 74651920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 74757736Seric { 74857736Seric if (tTd(3, 1)) 74957736Seric perror("getla: getloadavg failed:"); 75051920Seric return (-1); 75157736Seric } 75257736Seric if (tTd(3, 1)) 75357736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 75451920Seric return ((int) (avenrun[0] + 0.5)); 75551773Seric } 75651773Seric 75764718Seric #endif /* DGUX */ 75851773Seric #else 75964295Seric #if LA_TYPE == LA_MACH 76051773Seric 76164295Seric /* 76265173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 76364295Seric */ 76464295Seric 76565173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 76665173Seric # include <mach/mach.h> 76765173Seric #else 76865173Seric # include <mach.h> 76965173Seric #endif 77064295Seric 77151773Seric getla() 77251773Seric { 77364295Seric processor_set_t default_set; 77464295Seric kern_return_t error; 77564295Seric unsigned int info_count; 77664295Seric struct processor_set_basic_info info; 77764295Seric host_t host; 77864295Seric 77964295Seric error = processor_set_default(host_self(), &default_set); 78064295Seric if (error != KERN_SUCCESS) 78164295Seric return -1; 78264295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 78364295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 78464295Seric &host, (processor_set_info_t)&info, 78564295Seric &info_count) != KERN_SUCCESS) 78664295Seric { 78764295Seric return -1; 78864295Seric } 78964295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 79064295Seric } 79164295Seric 79264295Seric 79364295Seric #else 79466313Seric #if LA_TYPE == LA_PROCSTR 79564295Seric 79666301Seric /* 79766301Seric ** Read /proc/loadavg for the load average. This is assumed to be 79866301Seric ** in a format like "0.15 0.12 0.06". 79966301Seric ** 80066301Seric ** Initially intended for Linux. This has been in the kernel 80166301Seric ** since at least 0.99.15. 80266301Seric */ 80366301Seric 80466301Seric # ifndef _PATH_LOADAVG 80566301Seric # define _PATH_LOADAVG "/proc/loadavg" 80666301Seric # endif 80766301Seric 80866301Seric int 80964295Seric getla() 81064295Seric { 81166319Seric double avenrun; 81266301Seric register int result; 81366319Seric FILE *fp; 81466301Seric 81566319Seric fp = fopen(_PATH_LOADAVG, "r"); 81666319Seric if (fp == NULL) 81766301Seric { 81866319Seric if (tTd(3, 1)) 81966319Seric printf("getla: fopen(%s): %s\n", 82066319Seric _PATH_LOADAVG, errstring(errno)); 82166319Seric return -1; 82266301Seric } 82366301Seric result = fscanf(fp, "%lf", &avenrun); 82466319Seric fclose(fp); 82566301Seric if (result != 1) 82666301Seric { 82766310Seric if (tTd(3, 1)) 82866301Seric printf("getla: fscanf() = %d: %s\n", 82966301Seric result, errstring(errno)); 83066301Seric return -1; 83166301Seric } 83266301Seric 83357736Seric if (tTd(3, 1)) 83466301Seric printf("getla(): %.2f\n", avenrun); 83566301Seric 83666301Seric return ((int) (avenrun + 0.5)); 83766301Seric } 83866301Seric 83966301Seric #else 84066301Seric 84166301Seric getla() 84266301Seric { 84366301Seric if (tTd(3, 1)) 84457736Seric printf("getla: ZERO\n"); 84551920Seric return (0); 84651773Seric } 84751773Seric 84851773Seric #endif 84951773Seric #endif 85064295Seric #endif 85166301Seric #endif 85266045Seric 85366045Seric 85466045Seric /* 85566045Seric * Copyright 1989 Massachusetts Institute of Technology 85666045Seric * 85766045Seric * Permission to use, copy, modify, distribute, and sell this software and its 85866045Seric * documentation for any purpose is hereby granted without fee, provided that 85966045Seric * the above copyright notice appear in all copies and that both that 86066045Seric * copyright notice and this permission notice appear in supporting 86166045Seric * documentation, and that the name of M.I.T. not be used in advertising or 86266045Seric * publicity pertaining to distribution of the software without specific, 86366045Seric * written prior permission. M.I.T. makes no representations about the 86466045Seric * suitability of this software for any purpose. It is provided "as is" 86566045Seric * without express or implied warranty. 86666045Seric * 86766045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 86866045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 86966045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 87066045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 87166045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 87266045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 87366045Seric * 87466045Seric * Authors: Many and varied... 87566045Seric */ 87666045Seric 87766045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 87866045Seric #ifndef lint 87966045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 88066045Seric #endif /* !lint */ 88166045Seric 88266179Seric #ifdef apollo 88366045Seric # undef volatile 88466045Seric # include <apollo/base.h> 88566045Seric 88666045Seric /* ARGSUSED */ 88766045Seric int getloadavg( call_data ) 88866045Seric caddr_t call_data; /* pointer to (double) return value */ 88966045Seric { 89066045Seric double *avenrun = (double *) call_data; 89166045Seric int i; 89266045Seric status_$t st; 89366045Seric long loadav[3]; 89466045Seric proc1_$get_loadav(loadav, &st); 89566045Seric *avenrun = loadav[0] / (double) (1 << 16); 89666045Seric return(0); 89766045Seric } 89866045Seric # endif /* apollo */ 89924943Seric /* 90024943Seric ** SHOULDQUEUE -- should this message be queued or sent? 90124943Seric ** 90224943Seric ** Compares the message cost to the load average to decide. 90324943Seric ** 90424943Seric ** Parameters: 90524943Seric ** pri -- the priority of the message in question. 90657438Seric ** ctime -- the message creation time. 90724943Seric ** 90824943Seric ** Returns: 90924943Seric ** TRUE -- if this message should be queued up for the 91024943Seric ** time being. 91124943Seric ** FALSE -- if the load is low enough to send this message. 91224943Seric ** 91324943Seric ** Side Effects: 91424943Seric ** none. 91524943Seric */ 91624943Seric 91724943Seric bool 91857438Seric shouldqueue(pri, ctime) 91924943Seric long pri; 92057438Seric time_t ctime; 92124943Seric { 92251920Seric if (CurrentLA < QueueLA) 92324943Seric return (FALSE); 92458132Seric if (CurrentLA >= RefuseLA) 92558132Seric return (TRUE); 92651920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 92724943Seric } 92824943Seric /* 92953037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 93053037Seric ** 93153037Seric ** Parameters: 93253037Seric ** none. 93353037Seric ** 93453037Seric ** Returns: 93553037Seric ** TRUE if incoming SMTP connections should be refused 93653037Seric ** (for now). 93753037Seric ** FALSE if we should accept new work. 93853037Seric ** 93953037Seric ** Side Effects: 94053037Seric ** none. 94153037Seric */ 94253037Seric 94353037Seric bool 94453037Seric refuseconnections() 94553037Seric { 94659156Seric #ifdef XLA 94759156Seric if (!xla_smtp_ok()) 94859156Seric return TRUE; 94959156Seric #endif 95059156Seric 95153037Seric /* this is probably too simplistic */ 95258132Seric return (CurrentLA >= RefuseLA); 95353037Seric } 95453037Seric /* 95524943Seric ** SETPROCTITLE -- set process title for ps 95624943Seric ** 95724943Seric ** Parameters: 95858674Seric ** fmt -- a printf style format string. 95958674Seric ** a, b, c -- possible parameters to fmt. 96024943Seric ** 96124943Seric ** Returns: 96224943Seric ** none. 96324943Seric ** 96424943Seric ** Side Effects: 96524943Seric ** Clobbers argv of our main procedure so ps(1) will 96624943Seric ** display the title. 96724943Seric */ 96824943Seric 96958689Seric #ifdef SETPROCTITLE 970*67626Seric 97166002Seric # ifdef HASSETPROCTITLE 97266002Seric *** ERROR *** Cannot have both SETPROCTITLE and HASSETPROCTITLE defined 97366002Seric # endif 974*67626Seric 97558689Seric # ifdef __hpux 97658689Seric # include <sys/pstat.h> 97758689Seric # endif 97859732Seric # ifdef BSD4_4 97959732Seric # include <machine/vmparam.h> 98059732Seric # include <sys/exec.h> 98164247Seric # ifdef __bsdi__ 98264247Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 98364843Seric # define PROCTITLEPAD '\0' 98464247Seric # endif 98559992Seric # ifdef PS_STRINGS 98659992Seric # define SETPROC_STATIC static 98759992Seric # endif 98859732Seric # endif 989*67626Seric # ifdef _AIX3 990*67626Seric # define PROCTITLEPAD '\0' 991*67626Seric # endif 992*67626Seric 99359732Seric # ifndef SETPROC_STATIC 99459732Seric # define SETPROC_STATIC 99559732Seric # endif 996*67626Seric 99758689Seric #endif 99858689Seric 99964843Seric #ifndef PROCTITLEPAD 100064843Seric # define PROCTITLEPAD ' ' 100164843Seric #endif 100264843Seric 100365982Seric #ifndef HASSETPROCTITLE 100465982Seric 100524943Seric /*VARARGS1*/ 100657642Seric #ifdef __STDC__ 100757642Seric setproctitle(char *fmt, ...) 100857642Seric #else 100957642Seric setproctitle(fmt, va_alist) 101024943Seric char *fmt; 101157642Seric va_dcl 101257642Seric #endif 101324943Seric { 101424943Seric # ifdef SETPROCTITLE 101524943Seric register char *p; 101625049Seric register int i; 101759732Seric SETPROC_STATIC char buf[MAXLINE]; 101856852Seric VA_LOCAL_DECL 101958689Seric # ifdef __hpux 102058689Seric union pstun pst; 102158689Seric # endif 102224943Seric extern char **Argv; 102324943Seric extern char *LastArgv; 102424943Seric 102558674Seric p = buf; 102624943Seric 102758674Seric /* print sendmail: heading for grep */ 102858674Seric (void) strcpy(p, "sendmail: "); 102958674Seric p += strlen(p); 103024943Seric 103158674Seric /* print the argument string */ 103258674Seric VA_START(fmt); 103358674Seric (void) vsprintf(p, fmt, ap); 103456852Seric VA_END; 103554996Seric 103658674Seric i = strlen(buf); 103758689Seric 103858689Seric # ifdef __hpux 103958689Seric pst.pst_command = buf; 104058689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 104158689Seric # else 104259992Seric # ifdef PS_STRINGS 104359732Seric PS_STRINGS->ps_nargvstr = 1; 104459732Seric PS_STRINGS->ps_argvstr = buf; 104559732Seric # else 104654996Seric if (i > LastArgv - Argv[0] - 2) 104725049Seric { 104854996Seric i = LastArgv - Argv[0] - 2; 104958674Seric buf[i] = '\0'; 105025049Seric } 105158674Seric (void) strcpy(Argv[0], buf); 105254997Seric p = &Argv[0][i]; 105324943Seric while (p < LastArgv) 105464843Seric *p++ = PROCTITLEPAD; 1055*67626Seric Argv[1] = NULL; 105659732Seric # endif 105758689Seric # endif 105856795Seric # endif /* SETPROCTITLE */ 105924943Seric } 106065982Seric 106165982Seric #endif 106225698Seric /* 106325698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 106425698Seric ** 106525698Seric ** Parameters: 106625698Seric ** none. 106725698Seric ** 106825698Seric ** Returns: 106925698Seric ** none. 107025698Seric ** 107125698Seric ** Side Effects: 107225698Seric ** Picks up extant zombies. 107325698Seric */ 107425698Seric 107546928Sbostic void 107625698Seric reapchild() 107725698Seric { 107864738Seric int olderrno = errno; 107963966Seric # ifdef HASWAITPID 108060219Seric auto int status; 108160560Seric int count; 108260560Seric int pid; 108360219Seric 108460560Seric count = 0; 108560560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 108660560Seric { 108760560Seric if (count++ > 1000) 108860560Seric { 108966748Seric #ifdef LOG 109060560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 109160560Seric pid, status); 109266748Seric #endif 109360560Seric break; 109460560Seric } 109560560Seric } 109660219Seric # else 109725698Seric # ifdef WNOHANG 109825698Seric union wait status; 109925698Seric 110063753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 110125698Seric continue; 110256795Seric # else /* WNOHANG */ 110325698Seric auto int status; 110425698Seric 110560219Seric while (wait(&status) > 0) 110625698Seric continue; 110756795Seric # endif /* WNOHANG */ 110860219Seric # endif 110964035Seric # ifdef SYS5SIGNALS 111064035Seric (void) setsignal(SIGCHLD, reapchild); 111158061Seric # endif 111264738Seric errno = olderrno; 111325698Seric } 111455418Seric /* 111555418Seric ** UNSETENV -- remove a variable from the environment 111655418Seric ** 111755418Seric ** Not needed on newer systems. 111855418Seric ** 111955418Seric ** Parameters: 112055418Seric ** name -- the string name of the environment variable to be 112155418Seric ** deleted from the current environment. 112255418Seric ** 112355418Seric ** Returns: 112455418Seric ** none. 112555418Seric ** 112655418Seric ** Globals: 112755418Seric ** environ -- a pointer to the current environment. 112855418Seric ** 112955418Seric ** Side Effects: 113055418Seric ** Modifies environ. 113155418Seric */ 113255418Seric 113363962Seric #ifndef HASUNSETENV 113455418Seric 113555418Seric void 113655418Seric unsetenv(name) 113755418Seric char *name; 113855418Seric { 113955418Seric extern char **environ; 114055418Seric register char **pp; 114155418Seric int len = strlen(name); 114255418Seric 114355418Seric for (pp = environ; *pp != NULL; pp++) 114455418Seric { 114555418Seric if (strncmp(name, *pp, len) == 0 && 114655418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 114755418Seric break; 114855418Seric } 114955418Seric 115055418Seric for (; *pp != NULL; pp++) 115155418Seric *pp = pp[1]; 115255418Seric } 115355418Seric 115463962Seric #endif 115556215Seric /* 115656215Seric ** GETDTABLESIZE -- return number of file descriptors 115756215Seric ** 115856215Seric ** Only on non-BSD systems 115956215Seric ** 116056215Seric ** Parameters: 116156215Seric ** none 116256215Seric ** 116356215Seric ** Returns: 116456215Seric ** size of file descriptor table 116556215Seric ** 116656215Seric ** Side Effects: 116756215Seric ** none 116856215Seric */ 116956215Seric 117063787Seric #ifdef SOLARIS 117163787Seric # include <sys/resource.h> 117263787Seric #endif 117356215Seric 117456215Seric int 117563787Seric getdtsize() 117656215Seric { 117763787Seric #ifdef RLIMIT_NOFILE 117863787Seric struct rlimit rl; 117963787Seric 118063787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 118163787Seric return rl.rlim_cur; 118263787Seric #endif 118363787Seric 118464031Seric # ifdef HASGETDTABLESIZE 118564031Seric return getdtablesize(); 118664031Seric # else 118764031Seric # ifdef _SC_OPEN_MAX 118858689Seric return sysconf(_SC_OPEN_MAX); 118963787Seric # else 119056215Seric return NOFILE; 119163787Seric # endif 119258689Seric # endif 119356215Seric } 119457631Seric /* 119557631Seric ** UNAME -- get the UUCP name of this system. 119657631Seric */ 119757631Seric 119857943Seric #ifndef HASUNAME 119957631Seric 120057631Seric int 120157631Seric uname(name) 120257631Seric struct utsname *name; 120357631Seric { 120457631Seric FILE *file; 120557631Seric char *n; 120657631Seric 120757631Seric name->nodename[0] = '\0'; 120857631Seric 120957661Seric /* try /etc/whoami -- one line with the node name */ 121057631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 121157631Seric { 121257661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 121357631Seric (void) fclose(file); 121457661Seric n = strchr(name->nodename, '\n'); 121557631Seric if (n != NULL) 121657631Seric *n = '\0'; 121757631Seric if (name->nodename[0] != '\0') 121857631Seric return (0); 121957631Seric } 122057631Seric 122157661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 122257631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 122357631Seric { 122457631Seric char buf[MAXLINE]; 122557631Seric 122657631Seric while (fgets(buf, MAXLINE, file) != NULL) 122757631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 122857631Seric NODE_LENGTH, name->nodename) > 0) 122957631Seric break; 123057631Seric (void) fclose(file); 123157631Seric if (name->nodename[0] != '\0') 123257631Seric return (0); 123357631Seric } 123457631Seric 123557631Seric #ifdef TRUST_POPEN 123657631Seric /* 123757631Seric ** Popen is known to have security holes. 123857631Seric */ 123957631Seric 124057661Seric /* try uuname -l to return local name */ 124157631Seric if ((file = popen("uuname -l", "r")) != NULL) 124257631Seric { 124357661Seric (void) fgets(name, NODE_LENGTH + 1, file); 124457631Seric (void) pclose(file); 124557661Seric n = strchr(name, '\n'); 124657631Seric if (n != NULL) 124757631Seric *n = '\0'; 124857661Seric if (name->nodename[0] != '\0') 124957631Seric return (0); 125057631Seric } 125157631Seric #endif 125257631Seric 125357631Seric return (-1); 125457631Seric } 125557943Seric #endif /* HASUNAME */ 125658068Seric /* 125758068Seric ** INITGROUPS -- initialize groups 125858068Seric ** 125958068Seric ** Stub implementation for System V style systems 126058068Seric */ 126158068Seric 126258068Seric #ifndef HASINITGROUPS 126358068Seric 126458068Seric initgroups(name, basegid) 126558068Seric char *name; 126658068Seric int basegid; 126758068Seric { 126858068Seric return 0; 126958068Seric } 127058068Seric 127158068Seric #endif 127258082Seric /* 127359289Seric ** SETSID -- set session id (for non-POSIX systems) 127459289Seric */ 127559289Seric 127659289Seric #ifndef HASSETSID 127759289Seric 127863753Seric pid_t 127963753Seric setsid __P ((void)) 128059289Seric { 128164155Seric #ifdef TIOCNOTTY 128264155Seric int fd; 128364155Seric 128466743Seric fd = open("/dev/tty", O_RDWR, 0); 128564155Seric if (fd >= 0) 128664155Seric { 128764155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 128864155Seric (void) close(fd); 128964155Seric } 129064155Seric #endif /* TIOCNOTTY */ 129164705Seric # ifdef SYS5SETPGRP 129263753Seric return setpgrp(); 129363753Seric # else 129464155Seric return setpgid(0, getpid()); 129559289Seric # endif 129659289Seric } 129759289Seric 129859289Seric #endif 129959289Seric /* 130066748Seric ** FSYNC -- dummy fsync 130166748Seric */ 130266748Seric 130366748Seric #ifdef NEEDFSYNC 130466748Seric 130566748Seric fsync(fd) 130666748Seric int fd; 130766748Seric { 130866748Seric # ifdef O_SYNC 130966748Seric return fcntl(fd, F_SETFL, O_SYNC); 131066748Seric # else 131166748Seric /* nothing we can do */ 131266748Seric return 0; 131366748Seric # endif 131466748Seric } 131566748Seric 131666748Seric #endif 131766748Seric /* 131864718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 131964718Seric ** 132064718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 132167427Seric ** instead of a long. This patches things. Only needed on versions 132267427Seric ** prior to 5.4.3. 132364718Seric */ 132464718Seric 132567427Seric #ifdef DGUX_5_4_2 132664718Seric 132764718Seric #undef inet_addr 132864718Seric 132964718Seric long 133064718Seric dgux_inet_addr(host) 133164718Seric char *host; 133264718Seric { 133364718Seric struct in_addr haddr; 133464718Seric 133564718Seric haddr = inet_addr(host); 133664718Seric return haddr.s_addr; 133764718Seric } 133864718Seric 133964718Seric #endif 134064718Seric /* 134163969Seric ** GETOPT -- for old systems or systems with bogus implementations 134263969Seric */ 134363969Seric 134463969Seric #ifdef NEEDGETOPT 134563969Seric 134663969Seric /* 134763969Seric * Copyright (c) 1985 Regents of the University of California. 134863969Seric * All rights reserved. The Berkeley software License Agreement 134963969Seric * specifies the terms and conditions for redistribution. 135063969Seric */ 135163969Seric 135263969Seric 135363969Seric /* 135463969Seric ** this version hacked to add `atend' flag to allow state machine 135563969Seric ** to reset if invoked by the program to scan args for a 2nd time 135663969Seric */ 135763969Seric 135863969Seric #if defined(LIBC_SCCS) && !defined(lint) 135963969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 136064072Seric #endif /* LIBC_SCCS and not lint */ 136163969Seric 136263969Seric #include <stdio.h> 136363969Seric 136463969Seric /* 136563969Seric * get option letter from argument vector 136663969Seric */ 136765352Seric #ifdef _CONVEX_SOURCE 136865352Seric extern int optind, opterr; 136965352Seric #else 137065352Seric int opterr = 1; /* if error message should be printed */ 137165352Seric int optind = 1; /* index into parent argv vector */ 137265352Seric #endif 137365352Seric int optopt; /* character checked for validity */ 137463969Seric char *optarg; /* argument associated with option */ 137563969Seric 137663969Seric #define BADCH (int)'?' 137763969Seric #define EMSG "" 137863969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 137963969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 138063969Seric 138163969Seric getopt(nargc,nargv,ostr) 138265160Seric int nargc; 138365160Seric char *const *nargv; 138465160Seric const char *ostr; 138563969Seric { 138663969Seric static char *place = EMSG; /* option letter processing */ 138763969Seric static char atend = 0; 138863969Seric register char *oli; /* option letter list index */ 138963969Seric 139063969Seric if (atend) { 139163969Seric atend = 0; 139263969Seric place = EMSG; 139363969Seric } 139463969Seric if(!*place) { /* update scanning pointer */ 139564136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 139663969Seric atend++; 139763969Seric return(EOF); 139863969Seric } 139963969Seric if (*place == '-') { /* found "--" */ 140063969Seric ++optind; 140163969Seric atend++; 140263969Seric return(EOF); 140363969Seric } 140463969Seric } /* option letter okay? */ 140564125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 140664136Seric if (!*place) ++optind; 140763969Seric tell(": illegal option -- "); 140863969Seric } 140963969Seric if (*++oli != ':') { /* don't need argument */ 141063969Seric optarg = NULL; 141163969Seric if (!*place) ++optind; 141263969Seric } 141363969Seric else { /* need an argument */ 141463969Seric if (*place) optarg = place; /* no white space */ 141563969Seric else if (nargc <= ++optind) { /* no arg */ 141663969Seric place = EMSG; 141763969Seric tell(": option requires an argument -- "); 141863969Seric } 141963969Seric else optarg = nargv[optind]; /* white space */ 142063969Seric place = EMSG; 142163969Seric ++optind; 142263969Seric } 142363969Seric return(optopt); /* dump back option letter */ 142463969Seric } 142563969Seric 142663969Seric #endif 142763969Seric /* 142863969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 142963969Seric */ 143063969Seric 143163969Seric #ifdef NEEDVPRINTF 143263969Seric 143363969Seric #define MAXARG 16 143463969Seric 143563969Seric vfprintf(fp, fmt, ap) 143663969Seric FILE * fp; 143763969Seric char * fmt; 143863969Seric char ** ap; 143963969Seric { 144063969Seric char * bp[MAXARG]; 144163969Seric int i = 0; 144263969Seric 144363969Seric while (*ap && i < MAXARG) 144463969Seric bp[i++] = *ap++; 144563969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 144663969Seric bp[4], bp[5], bp[6], bp[7], 144763969Seric bp[8], bp[9], bp[10], bp[11], 144863969Seric bp[12], bp[13], bp[14], bp[15]); 144963969Seric } 145063969Seric 145163969Seric vsprintf(s, fmt, ap) 145263969Seric char * s; 145363969Seric char * fmt; 145463969Seric char ** ap; 145563969Seric { 145663969Seric char * bp[MAXARG]; 145763969Seric int i = 0; 145863969Seric 145963969Seric while (*ap && i < MAXARG) 146063969Seric bp[i++] = *ap++; 146163969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 146263969Seric bp[4], bp[5], bp[6], bp[7], 146363969Seric bp[8], bp[9], bp[10], bp[11], 146463969Seric bp[12], bp[13], bp[14], bp[15]); 146563969Seric } 146663969Seric 146763969Seric #endif 146863969Seric /* 146965220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 147065208Seric ** 147165211Seric ** Parameters: 147265211Seric ** shell -- the user's shell from /etc/passwd 147365211Seric ** 147465211Seric ** Returns: 147565211Seric ** TRUE -- if it is ok to use this for unrestricted access. 147665211Seric ** FALSE -- if the shell is restricted. 147765208Seric */ 147865208Seric 147965587Seric #if !HASGETUSERSHELL 148065587Seric 148165587Seric # ifndef _PATH_SHELLS 148265587Seric # define _PATH_SHELLS "/etc/shells" 148365587Seric # endif 148465587Seric 148565587Seric char *DefaultUserShells[] = 148665587Seric { 148765587Seric "/bin/sh", 148865587Seric "/usr/bin/sh", 148965587Seric "/bin/csh", 149065587Seric "/usr/bin/csh", 149165587Seric #ifdef __hpux 149265587Seric "/bin/rsh", 149365587Seric "/bin/ksh", 149465587Seric "/bin/rksh", 149565587Seric "/bin/pam", 149665587Seric "/usr/bin/keysh", 149765587Seric "/bin/posix/sh", 149865211Seric #endif 149965587Seric NULL 150065587Seric }; 150165587Seric 150265208Seric #endif 150365208Seric 150465747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 150565747Seric 150665211Seric bool 150765211Seric usershellok(shell) 150865211Seric char *shell; 150965208Seric { 151065211Seric #if HASGETUSERSHELL 151165211Seric register char *p; 151265211Seric extern char *getusershell(); 151365208Seric 151465211Seric setusershell(); 151565211Seric while ((p = getusershell()) != NULL) 151665747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 151765211Seric break; 151865211Seric endusershell(); 151965211Seric return p != NULL; 152065211Seric #else 152165211Seric register FILE *shellf; 152265211Seric char buf[MAXLINE]; 152365208Seric 152465211Seric shellf = fopen(_PATH_SHELLS, "r"); 152565211Seric if (shellf == NULL) 152665211Seric { 152765211Seric /* no /etc/shells; see if it is one of the std shells */ 152865587Seric char **d; 152965587Seric 153065587Seric for (d = DefaultUserShells; *d != NULL; d++) 153165587Seric { 153265587Seric if (strcmp(shell, *d) == 0) 153365587Seric return TRUE; 153465587Seric } 153565587Seric return FALSE; 153665211Seric } 153765208Seric 153865211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 153965211Seric { 154065211Seric register char *p, *q; 154165208Seric 154265211Seric p = buf; 154365211Seric while (*p != '\0' && *p != '#' && *p != '/') 154465211Seric p++; 154565211Seric if (*p == '#' || *p == '\0') 154665208Seric continue; 154765211Seric q = p; 154865211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 154965211Seric p++; 155065211Seric *p = '\0'; 155165747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 155265211Seric { 155365211Seric fclose(shellf); 155465211Seric return TRUE; 155565211Seric } 155665208Seric } 155765211Seric fclose(shellf); 155865211Seric return FALSE; 155965211Seric #endif 156065208Seric } 156165208Seric /* 156264311Seric ** FREESPACE -- see how much free space is on the queue filesystem 156358082Seric ** 156458082Seric ** Only implemented if you have statfs. 156558082Seric ** 156658082Seric ** Parameters: 156764311Seric ** dir -- the directory in question. 156864311Seric ** bsize -- a variable into which the filesystem 156964311Seric ** block size is stored. 157058082Seric ** 157158082Seric ** Returns: 157264311Seric ** The number of bytes free on the queue filesystem. 157364311Seric ** -1 if the statfs call fails. 157464311Seric ** 157564311Seric ** Side effects: 157664311Seric ** Puts the filesystem block size into bsize. 157758082Seric */ 157858082Seric 157965749Seric /* statfs types */ 158065749Seric #define SFS_NONE 0 /* no statfs implementation */ 158165749Seric #define SFS_USTAT 1 /* use ustat */ 158265749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 158365749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 158465749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 158565749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 158666752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 158765749Seric 158865749Seric #ifndef SFS_TYPE 158965749Seric # define SFS_TYPE SFS_NONE 159058157Seric #endif 159158157Seric 159265749Seric #if SFS_TYPE == SFS_USTAT 159358157Seric # include <ustat.h> 159458157Seric #endif 159565749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 159665749Seric # include <sys/statfs.h> 159758082Seric #endif 159865749Seric #if SFS_TYPE == SFS_VFS 159965749Seric # include <sys/vfs.h> 160065749Seric #endif 160165749Seric #if SFS_TYPE == SFS_MOUNT 160265749Seric # include <sys/mount.h> 160365749Seric #endif 160466752Seric #if SFS_TYPE == SFS_STATVFS 160566752Seric # include <sys/statvfs.h> 160666752Seric #endif 160758082Seric 160864311Seric long 160964311Seric freespace(dir, bsize) 161064311Seric char *dir; 161164311Seric long *bsize; 161258082Seric { 161365749Seric #if SFS_TYPE != SFS_NONE 161465749Seric # if SFS_TYPE == SFS_USTAT 161558153Seric struct ustat fs; 161658153Seric struct stat statbuf; 161758366Seric # define FSBLOCKSIZE DEV_BSIZE 161858157Seric # define f_bavail f_tfree 161958157Seric # else 162058157Seric # if defined(ultrix) 162158157Seric struct fs_data fs; 162258157Seric # define f_bavail fd_bfreen 162367467Seric # define FSBLOCKSIZE 1024L 162458153Seric # else 162566752Seric # if SFS_TYPE == SFS_STATVFS 162666752Seric struct statvfs fs; 162766893Seric # define FSBLOCKSIZE fs.f_bsize 162866752Seric # else 162958082Seric struct statfs fs; 163066752Seric # define FSBLOCKSIZE fs.f_bsize 163166752Seric # if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) 163266752Seric # define f_bavail f_bfree 163366752Seric # endif 163463987Seric # endif 163558153Seric # endif 163658133Seric # endif 163758082Seric extern int errno; 163858082Seric 163965749Seric # if SFS_TYPE == SFS_USTAT 164064311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 164158157Seric # else 164265749Seric # if SFS_TYPE == SFS_4ARGS 164364311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 164458157Seric # else 164567159Seric # if SFS_TYPE == SFS_STATVFS 164667159Seric if (statvfs(dir, &fs) == 0) 164767159Seric # else 164867159Seric # if defined(ultrix) 164964311Seric if (statfs(dir, &fs) > 0) 165067159Seric # else 165164311Seric if (statfs(dir, &fs) == 0) 165267159Seric # endif 165358153Seric # endif 165458133Seric # endif 165558133Seric # endif 165658133Seric { 165764311Seric if (bsize != NULL) 165864311Seric *bsize = FSBLOCKSIZE; 165964311Seric return (fs.f_bavail); 166064311Seric } 166164337Seric #endif 166264311Seric return (-1); 166364311Seric } 166464311Seric /* 166564311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 166664311Seric ** 166764311Seric ** Only implemented if you have statfs. 166864311Seric ** 166964311Seric ** Parameters: 167064311Seric ** msize -- the size to check against. If zero, we don't yet 167164311Seric ** know how big the message will be, so just check for 167264311Seric ** a "reasonable" amount. 167364311Seric ** 167464311Seric ** Returns: 167564311Seric ** TRUE if there is enough space. 167664311Seric ** FALSE otherwise. 167764311Seric */ 167864311Seric 167964311Seric bool 168064311Seric enoughspace(msize) 168164311Seric long msize; 168264311Seric { 168364311Seric long bfree, bsize; 168464311Seric 168564311Seric if (MinBlocksFree <= 0 && msize <= 0) 168664311Seric { 168758133Seric if (tTd(4, 80)) 168864311Seric printf("enoughspace: no threshold\n"); 168964311Seric return TRUE; 169064311Seric } 169164311Seric 169264311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 169364311Seric { 169464311Seric if (tTd(4, 80)) 169558333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 169664311Seric bfree, msize); 169758333Seric 169858333Seric /* convert msize to block count */ 169964311Seric msize = msize / bsize + 1; 170058333Seric if (MinBlocksFree >= 0) 170158333Seric msize += MinBlocksFree; 170258333Seric 170364311Seric if (bfree < msize) 170458090Seric { 170558090Seric #ifdef LOG 170658090Seric if (LogLevel > 0) 170764254Seric syslog(LOG_ALERT, 170864254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 170964311Seric CurEnv->e_id, bfree, 171064254Seric CurHostName, msize, QueueDir); 171158090Seric #endif 171258082Seric return FALSE; 171358090Seric } 171458082Seric } 171558082Seric else if (tTd(4, 80)) 171658333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 171758333Seric MinBlocksFree, msize, errstring(errno)); 171858082Seric return TRUE; 171958082Seric } 172058542Seric /* 172158542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 172258542Seric ** 172358542Seric ** This looks at an errno value and tells if this is likely to 172458542Seric ** go away if retried later. 172558542Seric ** 172658542Seric ** Parameters: 172758542Seric ** err -- the errno code to classify. 172858542Seric ** 172958542Seric ** Returns: 173058542Seric ** TRUE if this is probably transient. 173158542Seric ** FALSE otherwise. 173258542Seric */ 173358542Seric 173458542Seric bool 173558542Seric transienterror(err) 173658542Seric int err; 173758542Seric { 173858542Seric switch (err) 173958542Seric { 174058542Seric case EIO: /* I/O error */ 174158542Seric case ENXIO: /* Device not configured */ 174258542Seric case EAGAIN: /* Resource temporarily unavailable */ 174358542Seric case ENOMEM: /* Cannot allocate memory */ 174458542Seric case ENODEV: /* Operation not supported by device */ 174558542Seric case ENFILE: /* Too many open files in system */ 174658542Seric case EMFILE: /* Too many open files */ 174758542Seric case ENOSPC: /* No space left on device */ 174858542Seric #ifdef ETIMEDOUT 174958542Seric case ETIMEDOUT: /* Connection timed out */ 175058542Seric #endif 175158542Seric #ifdef ESTALE 175258542Seric case ESTALE: /* Stale NFS file handle */ 175358542Seric #endif 175458542Seric #ifdef ENETDOWN 175558542Seric case ENETDOWN: /* Network is down */ 175658542Seric #endif 175758542Seric #ifdef ENETUNREACH 175858542Seric case ENETUNREACH: /* Network is unreachable */ 175958542Seric #endif 176058542Seric #ifdef ENETRESET 176158542Seric case ENETRESET: /* Network dropped connection on reset */ 176258542Seric #endif 176358542Seric #ifdef ECONNABORTED 176458542Seric case ECONNABORTED: /* Software caused connection abort */ 176558542Seric #endif 176658542Seric #ifdef ECONNRESET 176758542Seric case ECONNRESET: /* Connection reset by peer */ 176858542Seric #endif 176958542Seric #ifdef ENOBUFS 177058542Seric case ENOBUFS: /* No buffer space available */ 177158542Seric #endif 177258542Seric #ifdef ESHUTDOWN 177358542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 177458542Seric #endif 177558542Seric #ifdef ECONNREFUSED 177658542Seric case ECONNREFUSED: /* Connection refused */ 177758542Seric #endif 177858542Seric #ifdef EHOSTDOWN 177958542Seric case EHOSTDOWN: /* Host is down */ 178058542Seric #endif 178158542Seric #ifdef EHOSTUNREACH 178258542Seric case EHOSTUNREACH: /* No route to host */ 178358542Seric #endif 178458542Seric #ifdef EDQUOT 178558542Seric case EDQUOT: /* Disc quota exceeded */ 178658542Seric #endif 178758542Seric #ifdef EPROCLIM 178858542Seric case EPROCLIM: /* Too many processes */ 178958542Seric #endif 179058542Seric #ifdef EUSERS 179158542Seric case EUSERS: /* Too many users */ 179258542Seric #endif 179358542Seric #ifdef EDEADLK 179458542Seric case EDEADLK: /* Resource deadlock avoided */ 179558542Seric #endif 179658542Seric #ifdef EISCONN 179758542Seric case EISCONN: /* Socket already connected */ 179858542Seric #endif 179958542Seric #ifdef EINPROGRESS 180058542Seric case EINPROGRESS: /* Operation now in progress */ 180158542Seric #endif 180258542Seric #ifdef EALREADY 180358542Seric case EALREADY: /* Operation already in progress */ 180458542Seric #endif 180558542Seric #ifdef EADDRINUSE 180658542Seric case EADDRINUSE: /* Address already in use */ 180758542Seric #endif 180858542Seric #ifdef EADDRNOTAVAIL 180958542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 181058542Seric #endif 181166018Seric #ifdef ETXTBSY 181266017Seric case ETXTBSY: /* (Apollo) file locked */ 181366017Seric #endif 181463834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 181558542Seric case ENOSR: /* Out of streams resources */ 181658542Seric #endif 181758542Seric return TRUE; 181858542Seric } 181958542Seric 182058542Seric /* nope, must be permanent */ 182158542Seric return FALSE; 182258542Seric } 182358689Seric /* 182464035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 182558689Seric ** 182658689Seric ** Parameters: 182758689Seric ** fd -- the file descriptor of the file. 182858689Seric ** filename -- the file name (for error messages). 182964335Seric ** ext -- the filename extension. 183058689Seric ** type -- type of the lock. Bits can be: 183158689Seric ** LOCK_EX -- exclusive lock. 183258689Seric ** LOCK_NB -- non-blocking. 183358689Seric ** 183458689Seric ** Returns: 183558689Seric ** TRUE if the lock was acquired. 183658689Seric ** FALSE otherwise. 183758689Seric */ 183858689Seric 183958689Seric bool 184064335Seric lockfile(fd, filename, ext, type) 184158689Seric int fd; 184258689Seric char *filename; 184364335Seric char *ext; 184458689Seric int type; 184558689Seric { 184665830Seric # if !HASFLOCK 184758689Seric int action; 184858689Seric struct flock lfd; 184964335Seric 185064335Seric if (ext == NULL) 185164335Seric ext = ""; 185264035Seric 185363983Seric bzero(&lfd, sizeof lfd); 185459447Seric if (bitset(LOCK_UN, type)) 185559447Seric lfd.l_type = F_UNLCK; 185659447Seric else if (bitset(LOCK_EX, type)) 185758689Seric lfd.l_type = F_WRLCK; 185858689Seric else 185958689Seric lfd.l_type = F_RDLCK; 186058689Seric 186158689Seric if (bitset(LOCK_NB, type)) 186258689Seric action = F_SETLK; 186358689Seric else 186458689Seric action = F_SETLKW; 186558689Seric 186664264Seric if (tTd(55, 60)) 186764335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 186864335Seric filename, ext, action, lfd.l_type); 186964264Seric 187058689Seric if (fcntl(fd, action, &lfd) >= 0) 187164264Seric { 187264264Seric if (tTd(55, 60)) 187364264Seric printf("SUCCESS\n"); 187458689Seric return TRUE; 187564264Seric } 187658689Seric 187764264Seric if (tTd(55, 60)) 187864264Seric printf("(%s) ", errstring(errno)); 187964264Seric 188064136Seric /* 188164136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 188264136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 188364136Seric ** as type "tmp" (that is, served from swap space), the 188464136Seric ** previous fcntl will fail with "Invalid argument" errors. 188564136Seric ** Since this is fairly common during testing, we will assume 188664136Seric ** that this indicates that the lock is successfully grabbed. 188764136Seric */ 188864136Seric 188964136Seric if (errno == EINVAL) 189064264Seric { 189164264Seric if (tTd(55, 60)) 189264264Seric printf("SUCCESS\n"); 189364136Seric return TRUE; 189464264Seric } 189564136Seric 189658689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 189764378Seric { 189864378Seric int omode = -1; 189964378Seric # ifdef F_GETFL 190064378Seric int oerrno = errno; 190164378Seric 190264378Seric (void) fcntl(fd, F_GETFL, &omode); 190364378Seric errno = oerrno; 190464378Seric # endif 190564378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 190664378Seric filename, ext, fd, type, omode, geteuid()); 190764378Seric } 190858689Seric # else 190964335Seric if (ext == NULL) 191064335Seric ext = ""; 191164335Seric 191264264Seric if (tTd(55, 60)) 191364335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 191464264Seric 191558689Seric if (flock(fd, type) >= 0) 191664264Seric { 191764264Seric if (tTd(55, 60)) 191864264Seric printf("SUCCESS\n"); 191958689Seric return TRUE; 192064264Seric } 192158689Seric 192264264Seric if (tTd(55, 60)) 192364264Seric printf("(%s) ", errstring(errno)); 192464264Seric 192558689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 192664378Seric { 192764378Seric int omode = -1; 192864378Seric # ifdef F_GETFL 192964378Seric int oerrno = errno; 193064378Seric 193164378Seric (void) fcntl(fd, F_GETFL, &omode); 193264378Seric errno = oerrno; 193364378Seric # endif 193464378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 193564378Seric filename, ext, fd, type, omode, geteuid()); 193664386Seric } 193758689Seric # endif 193864264Seric if (tTd(55, 60)) 193964264Seric printf("FAIL\n"); 194058689Seric return FALSE; 194158689Seric } 194264556Seric /* 194365948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 194465948Seric ** 194565948Seric ** Parameters: 194665948Seric ** fd -- the file descriptor to check. 194765948Seric ** 194865948Seric ** Returns: 194965948Seric ** TRUE -- if only root can chown the file to an arbitrary 195065948Seric ** user. 195165948Seric ** FALSE -- if an arbitrary user can give away a file. 195265948Seric */ 195365948Seric 195465948Seric bool 195565948Seric chownsafe(fd) 195665948Seric int fd; 195765948Seric { 195865950Seric #ifdef __hpux 195965948Seric char *s; 196065948Seric int tfd; 196165948Seric uid_t o_uid, o_euid; 196265948Seric gid_t o_gid, o_egid; 196365948Seric bool rval; 196465948Seric struct stat stbuf; 196565948Seric 196665948Seric o_uid = getuid(); 196765948Seric o_euid = geteuid(); 196865948Seric o_gid = getgid(); 196965948Seric o_egid = getegid(); 197065959Seric fstat(fd, &stbuf); 197165948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 197265948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 197365948Seric s = tmpnam(NULL); 197465948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 197565974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 197665948Seric close(tfd); 197765948Seric unlink(s); 197865948Seric setreuid(o_uid, o_euid); 197965948Seric setresgid(o_gid, o_egid, -1); 198065948Seric return rval; 198165948Seric #else 198266088Seric # ifdef _POSIX_CHOWN_RESTRICTED 198366088Seric # if _POSIX_CHOWN_RESTRICTED == -1 198466088Seric return FALSE; 198566088Seric # else 198666088Seric return TRUE; 198766088Seric # endif 198866088Seric # else 198966088Seric # ifdef _PC_CHOWN_RESTRICTED 199067447Seric int rval; 199167447Seric 199267447Seric /* 199367447Seric ** Some systems (e.g., SunOS) seem to have the call and the 199467447Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 199567447Seric ** the call. This heuristic checks for that. 199667447Seric */ 199767447Seric 199867447Seric errno = 0; 199967447Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 200067447Seric if (errno == 0) 200167447Seric return rval > 0; 200267447Seric # endif 200367447Seric # ifdef BSD 200467447Seric return TRUE; 200566088Seric # else 200665948Seric return FALSE; 200765948Seric # endif 200865948Seric # endif 200965948Seric #endif 201065948Seric } 201165948Seric /* 201267430Seric ** RESETLIMITS -- reset system controlled resource limits 201367430Seric ** 201467430Seric ** This is to avoid denial-of-service attacks 201567430Seric ** 201667430Seric ** Parameters: 201767430Seric ** none 201867430Seric ** 201967430Seric ** Returns: 202067430Seric ** none 202167430Seric */ 202267430Seric 202367430Seric #if HASSETRLIMIT 202467430Seric # include <sys/resource.h> 202567430Seric #endif 202667430Seric 202767430Seric resetlimits() 202867430Seric { 202967430Seric #if HASSETRLIMIT 203067430Seric struct rlimit lim; 203167430Seric 203267430Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 203367430Seric (void) setrlimit(RLIMIT_CPU, &lim); 203467430Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 203567430Seric #else 203667430Seric # if HASULIMIT 203767430Seric (void) ulimit(2, 0x3fffff); 203867430Seric # endif 203967430Seric #endif 204067430Seric } 204167430Seric /* 204264556Seric ** GETCFNAME -- return the name of the .cf file. 204364556Seric ** 204464556Seric ** Some systems (e.g., NeXT) determine this dynamically. 204564556Seric */ 204664556Seric 204764556Seric char * 204864556Seric getcfname() 204964556Seric { 205064556Seric if (ConfFile != NULL) 205164556Seric return ConfFile; 205265214Seric #ifdef NETINFO 205365214Seric { 205465214Seric extern char *ni_propval(); 205565214Seric char *cflocation; 205665214Seric 205765214Seric cflocation = ni_propval("/locations/sendmail", "sendmail.cf"); 205865214Seric if (cflocation != NULL) 205965214Seric return cflocation; 206065214Seric } 206165214Seric #endif 206264556Seric return _PATH_SENDMAILCF; 206364556Seric } 206464718Seric /* 206564718Seric ** SETVENDOR -- process vendor code from V configuration line 206664718Seric ** 206764718Seric ** Parameters: 206864718Seric ** vendor -- string representation of vendor. 206964718Seric ** 207064718Seric ** Returns: 207164718Seric ** TRUE -- if ok. 207264718Seric ** FALSE -- if vendor code could not be processed. 207364926Seric ** 207464926Seric ** Side Effects: 207564926Seric ** It is reasonable to set mode flags here to tweak 207664926Seric ** processing in other parts of the code if necessary. 207764926Seric ** For example, if you are a vendor that uses $%y to 207864926Seric ** indicate YP lookups, you could enable that here. 207964718Seric */ 208064718Seric 208164718Seric bool 208264718Seric setvendor(vendor) 208364718Seric char *vendor; 208464718Seric { 208564926Seric if (strcasecmp(vendor, "Berkeley") == 0) 208664926Seric return TRUE; 208764926Seric 208864926Seric /* add vendor extensions here */ 208964926Seric 209064926Seric return FALSE; 209164718Seric } 209264816Seric /* 209364816Seric ** STRTOL -- convert string to long integer 209464816Seric ** 209564816Seric ** For systems that don't have it in the C library. 209666161Seric ** 209766161Seric ** This is taken verbatim from the 4.4-Lite C library. 209864816Seric */ 209964816Seric 210064816Seric #ifdef NEEDSTRTOL 210164816Seric 210266161Seric #if defined(LIBC_SCCS) && !defined(lint) 210366161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 210466161Seric #endif /* LIBC_SCCS and not lint */ 210566161Seric 210666161Seric #include <limits.h> 210766161Seric 210866161Seric /* 210966161Seric * Convert a string to a long integer. 211066161Seric * 211166161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 211266161Seric * alphabets and digits are each contiguous. 211366161Seric */ 211466161Seric 211564816Seric long 211666161Seric strtol(nptr, endptr, base) 211766161Seric const char *nptr; 211866161Seric char **endptr; 211966161Seric register int base; 212064816Seric { 212166161Seric register const char *s = nptr; 212266161Seric register unsigned long acc; 212366161Seric register int c; 212466161Seric register unsigned long cutoff; 212566161Seric register int neg = 0, any, cutlim; 212664816Seric 212766161Seric /* 212866161Seric * Skip white space and pick up leading +/- sign if any. 212966161Seric * If base is 0, allow 0x for hex and 0 for octal, else 213066161Seric * assume decimal; if base is already 16, allow 0x. 213166161Seric */ 213266161Seric do { 213366161Seric c = *s++; 213466161Seric } while (isspace(c)); 213566161Seric if (c == '-') { 213666161Seric neg = 1; 213766161Seric c = *s++; 213866161Seric } else if (c == '+') 213966161Seric c = *s++; 214066161Seric if ((base == 0 || base == 16) && 214166161Seric c == '0' && (*s == 'x' || *s == 'X')) { 214266161Seric c = s[1]; 214366161Seric s += 2; 214466161Seric base = 16; 214566161Seric } 214666161Seric if (base == 0) 214766161Seric base = c == '0' ? 8 : 10; 214864816Seric 214966161Seric /* 215066161Seric * Compute the cutoff value between legal numbers and illegal 215166161Seric * numbers. That is the largest legal value, divided by the 215266161Seric * base. An input number that is greater than this value, if 215366161Seric * followed by a legal input character, is too big. One that 215466161Seric * is equal to this value may be valid or not; the limit 215566161Seric * between valid and invalid numbers is then based on the last 215666161Seric * digit. For instance, if the range for longs is 215766161Seric * [-2147483648..2147483647] and the input base is 10, 215866161Seric * cutoff will be set to 214748364 and cutlim to either 215966161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 216066161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 216166161Seric * the number is too big, and we will return a range error. 216266161Seric * 216366161Seric * Set any if any `digits' consumed; make it negative to indicate 216466161Seric * overflow. 216566161Seric */ 216666161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 216766161Seric cutlim = cutoff % (unsigned long)base; 216866161Seric cutoff /= (unsigned long)base; 216966161Seric for (acc = 0, any = 0;; c = *s++) { 217066161Seric if (isdigit(c)) 217166161Seric c -= '0'; 217266161Seric else if (isalpha(c)) 217366161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 217466161Seric else 217566161Seric break; 217666161Seric if (c >= base) 217766161Seric break; 217866161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 217966161Seric any = -1; 218066161Seric else { 218166161Seric any = 1; 218266161Seric acc *= base; 218366161Seric acc += c; 218464816Seric } 218564816Seric } 218666161Seric if (any < 0) { 218766161Seric acc = neg ? LONG_MIN : LONG_MAX; 218866161Seric errno = ERANGE; 218966161Seric } else if (neg) 219066161Seric acc = -acc; 219166161Seric if (endptr != 0) 219266161Seric *endptr = (char *)(any ? s - 1 : nptr); 219366161Seric return (acc); 219464816Seric } 219564816Seric 219664816Seric #endif 219764841Seric /* 219864841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 219964841Seric ** 220067456Seric ** Solaris versions at least through 2.3 don't properly deliver a 220164841Seric ** canonical h_name field. This tries to work around it. 220264841Seric */ 220364841Seric 220464841Seric #ifdef SOLARIS 220564841Seric 220667456Seric extern int h_errno; 220767456Seric 220864841Seric struct hostent * 220964841Seric solaris_gethostbyname(name) 221065094Seric const char *name; 221164841Seric { 221264841Seric # ifdef SOLARIS_2_3 221364841Seric static struct hostent hp; 221464841Seric static char buf[1000]; 221564841Seric extern struct hostent *_switch_gethostbyname_r(); 221664841Seric 221764841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 221864841Seric # else 221965009Seric extern struct hostent *__switch_gethostbyname(); 222065009Seric 222164841Seric return __switch_gethostbyname(name); 222264841Seric # endif 222364841Seric } 222464841Seric 222564841Seric struct hostent * 222664841Seric solaris_gethostbyaddr(addr, len, type) 222765094Seric const char *addr; 222864841Seric int len; 222964841Seric int type; 223064841Seric { 223164841Seric # ifdef SOLARIS_2_3 223264841Seric static struct hostent hp; 223364841Seric static char buf[1000]; 223464841Seric extern struct hostent *_switch_gethostbyaddr_r(); 223564841Seric 223664841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 223764841Seric # else 223865009Seric extern struct hostent *__switch_gethostbyaddr(); 223965009Seric 224064841Seric return __switch_gethostbyaddr(addr, len, type); 224164841Seric # endif 224264841Seric } 224364841Seric 224464841Seric #endif 224565214Seric /* 224665214Seric ** NI_PROPVAL -- netinfo property value lookup routine 224765214Seric ** 224865214Seric ** Parameters: 224965214Seric ** directory -- the Netinfo directory name. 225065214Seric ** propname -- the Netinfo property name. 225165214Seric ** 225265214Seric ** Returns: 225365214Seric ** NULL -- if: 225465214Seric ** 1. the directory is not found 225565214Seric ** 2. the property name is not found 225665214Seric ** 3. the property contains multiple values 225765214Seric ** 4. some error occured 225865214Seric ** else -- the location of the config file. 225965214Seric ** 226065214Seric ** Notes: 226165214Seric ** Caller should free the return value of ni_proval 226265214Seric */ 226365214Seric 226465214Seric #ifdef NETINFO 226565214Seric 226665214Seric # include <netinfo/ni.h> 226765214Seric 226865214Seric # define LOCAL_NETINFO_DOMAIN "." 226965214Seric # define PARENT_NETINFO_DOMAIN ".." 227065214Seric # define MAX_NI_LEVELS 256 227165214Seric 227265214Seric char * 227365214Seric ni_propval(directory, propname) 227465214Seric char *directory; 227565214Seric char *propname; 227665214Seric { 227765820Seric char *propval = NULL; 227865214Seric int i; 227965214Seric void *ni = NULL; 228065214Seric void *lastni = NULL; 228165214Seric ni_status nis; 228265214Seric ni_id nid; 228365214Seric ni_namelist ninl; 228465214Seric 228565214Seric /* 228665214Seric ** If the passed directory and property name are found 228765214Seric ** in one of netinfo domains we need to search (starting 228865214Seric ** from the local domain moving all the way back to the 228965214Seric ** root domain) set propval to the property's value 229065214Seric ** and return it. 229165214Seric */ 229265214Seric 229365214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 229465214Seric { 229565214Seric if (i == 0) 229665214Seric { 229765214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 229865214Seric } 229965214Seric else 230065214Seric { 230165214Seric if (lastni != NULL) 230265214Seric ni_free(lastni); 230365214Seric lastni = ni; 230465214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 230565214Seric } 230665214Seric 230765214Seric /* 230865214Seric ** Don't bother if we didn't get a handle on a 230965214Seric ** proper domain. This is not necessarily an error. 231065214Seric ** We would get a positive ni_status if, for instance 231165214Seric ** we never found the directory or property and tried 231265214Seric ** to open the parent of the root domain! 231365214Seric */ 231465214Seric 231565214Seric if (nis != 0) 231665214Seric break; 231765214Seric 231865214Seric /* 231965214Seric ** Find the path to the server information. 232065214Seric */ 232165214Seric 232265214Seric if (ni_pathsearch(ni, &nid, directory) != 0) 232365214Seric continue; 232465214Seric 232565214Seric /* 232665214Seric ** Find "host" information. 232765214Seric */ 232865214Seric 232965214Seric if (ni_lookupprop(ni, &nid, propname, &ninl) != 0) 233065214Seric continue; 233165214Seric 233265214Seric /* 233365214Seric ** If there's only one name in 233465214Seric ** the list, assume we've got 233565214Seric ** what we want. 233665214Seric */ 233765214Seric 233865214Seric if (ninl.ni_namelist_len == 1) 233965214Seric { 234065214Seric propval = ni_name_dup(ninl.ni_namelist_val[0]); 234165214Seric break; 234265214Seric } 234365214Seric } 234465214Seric 234565214Seric /* 234665214Seric ** Clean up. 234765214Seric */ 234865214Seric 234965214Seric if (ni != NULL) 235065214Seric ni_free(ni); 235165214Seric if (lastni != NULL && ni != lastni) 235265214Seric ni_free(lastni); 235365214Seric 235465214Seric return propval; 235565214Seric } 235665214Seric 235765214Seric #endif /* NETINFO */ 235865504Seric /* 235965504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 236065504Seric ** 236165504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 236265504Seric ** syslog succeeds during interrupt handlers. 236365504Seric */ 236465504Seric 236565504Seric #ifdef __hpux 236665504Seric 236765504Seric # define MAXSYSLOGTRIES 100 236865504Seric # undef syslog 236965504Seric 237065504Seric # ifdef __STDC__ 237165504Seric hard_syslog(int pri, char *msg, ...) 237265504Seric # else 237365504Seric hard_syslog(pri, msg, va_alist) 237465504Seric int pri; 237565504Seric char *msg; 237665504Seric va_dcl 237765504Seric # endif 237865504Seric { 237965504Seric int i; 238065504Seric char buf[SYSLOG_BUFSIZE * 2]; 238165504Seric VA_LOCAL_DECL; 238265504Seric 238365504Seric VA_START(msg); 238465504Seric vsprintf(buf, msg, ap); 238565504Seric VA_END; 238665504Seric 238765504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 238865504Seric continue; 238965504Seric } 239065504Seric 239165504Seric #endif 2392