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*67473Seric static char sccsid[] = "@(#)conf.c 8.97 (Berkeley) 07/03/94"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1363937Seric # include "sendmail.h" 1463937Seric # include "pathnames.h" 1514881Seric # include <sys/ioctl.h> 1658082Seric # include <sys/param.h> 1765018Seric # include <netdb.h> 1836928Sbostic # include <pwd.h> 19404Seric 20294Seric /* 213309Seric ** CONF.C -- Sendmail Configuration Tables. 22294Seric ** 23294Seric ** Defines the configuration of this installation. 24294Seric ** 251388Seric ** Configuration Variables: 262897Seric ** HdrInfo -- a table describing well-known header fields. 272897Seric ** Each entry has the field name and some flags, 284147Seric ** which are described in sendmail.h. 294093Seric ** 304093Seric ** Notes: 314093Seric ** I have tried to put almost all the reasonable 324093Seric ** configuration information into the configuration 334093Seric ** file read at runtime. My intent is that anything 344093Seric ** here is a function of the version of UNIX you 354093Seric ** are running, or is really static -- for example 364093Seric ** the headers are a superset of widely used 374093Seric ** protocols. If you find yourself playing with 384093Seric ** this file too much, you may be making a mistake! 39294Seric */ 40294Seric 41294Seric 42294Seric 43294Seric 444437Seric /* 452897Seric ** Header info table 463057Seric ** Final (null) entry contains the flags used for any other field. 474147Seric ** 484147Seric ** Not all of these are actually handled specially by sendmail 494147Seric ** at this time. They are included as placeholders, to let 504147Seric ** you know that "someday" I intend to have sendmail do 514147Seric ** something with them. 522897Seric */ 532897Seric 542897Seric struct hdrinfo HdrInfo[] = 552897Seric { 568060Seric /* originator fields, most to least significant */ 5711417Seric "resent-sender", H_FROM|H_RESENT, 5811417Seric "resent-from", H_FROM|H_RESENT, 5925686Seric "resent-reply-to", H_FROM|H_RESENT, 609055Seric "sender", H_FROM, 619055Seric "from", H_FROM, 6225686Seric "reply-to", H_FROM, 639055Seric "full-name", H_ACHECK, 6464682Seric "return-receipt-to", H_FROM|H_RECEIPTTO, 6557359Seric "errors-to", H_FROM|H_ERRORSTO, 6658796Seric 678060Seric /* destination fields */ 689055Seric "to", H_RCPT, 6911417Seric "resent-to", H_RCPT|H_RESENT, 709055Seric "cc", H_RCPT, 7111417Seric "resent-cc", H_RCPT|H_RESENT, 729055Seric "bcc", H_RCPT|H_ACHECK, 7311417Seric "resent-bcc", H_RCPT|H_ACHECK|H_RESENT, 7456215Seric "apparently-to", H_RCPT, 7558796Seric 768060Seric /* message identification and control */ 7711417Seric "message-id", 0, 7811417Seric "resent-message-id", H_RESENT, 799055Seric "message", H_EOH, 809055Seric "text", H_EOH, 8158796Seric 8211417Seric /* date fields */ 8311417Seric "date", 0, 8411417Seric "resent-date", H_RESENT, 8558796Seric 868060Seric /* trace fields */ 879055Seric "received", H_TRACE|H_FORCE, 8859318Seric "x400-received", H_TRACE|H_FORCE, 899055Seric "via", H_TRACE|H_FORCE, 909055Seric "mail-from", H_TRACE|H_FORCE, 918060Seric 9258796Seric /* miscellaneous fields */ 9358796Seric "comments", H_FORCE, 9463753Seric "return-path", H_FORCE|H_ACHECK, 9558796Seric 969055Seric NULL, 0, 972897Seric }; 984166Seric 994166Seric 1004166Seric 1014282Seric /* 1024282Seric ** Location of system files/databases/etc. 1034282Seric */ 1044282Seric 10558082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1069039Seric 1079064Seric 1089064Seric 1099039Seric /* 11058082Seric ** Privacy values 11158082Seric */ 11258082Seric 11358082Seric struct prival PrivacyValues[] = 11458082Seric { 11558082Seric "public", PRIV_PUBLIC, 11658082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11758114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11858082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 11958082Seric "noexpn", PRIV_NOEXPN, 12058082Seric "novrfy", PRIV_NOVRFY, 12164333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12264333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12358789Seric "authwarnings", PRIV_AUTHWARNINGS, 12466783Seric "noreceipts", PRIV_NORECEIPTS, 12558082Seric "goaway", PRIV_GOAWAY, 12658789Seric NULL, 0, 12758082Seric }; 12858082Seric 12958082Seric 13058082Seric 13158082Seric /* 13224943Seric ** Miscellaneous stuff. 1339039Seric */ 1349039Seric 13524943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13664644Seric 13764644Seric 13864644Seric /* 13964644Seric ** Following should be config parameters (and probably will be in 14064644Seric ** future releases). In the meantime, setting these is considered 14164644Seric ** unsupported, and is intentionally undocumented. 14264644Seric */ 14364644Seric 14464644Seric #ifdef BROKENSMTPPEERS 14564644Seric bool BrokenSmtpPeers = TRUE; /* set if you have broken SMTP peers */ 14664644Seric #else 14764644Seric bool BrokenSmtpPeers = FALSE; /* set if you have broken SMTP peers */ 14864644Seric #endif 14924943Seric /* 15024943Seric ** SETDEFAULTS -- set default values 15124943Seric ** 15224943Seric ** Because of the way freezing is done, these must be initialized 15324943Seric ** using direct code. 15424943Seric ** 15524943Seric ** Parameters: 15658734Seric ** e -- the default envelope. 15724943Seric ** 15824943Seric ** Returns: 15924943Seric ** none. 16024943Seric ** 16124943Seric ** Side Effects: 16224943Seric ** Initializes a bunch of global variables to their 16324943Seric ** default values. 16424943Seric */ 16524943Seric 16658737Seric #define DAYS * 24 * 60 * 60 16758737Seric 16858734Seric setdefaults(e) 16958734Seric register ENVELOPE *e; 17024943Seric { 17157438Seric SpaceSub = ' '; /* option B */ 17257438Seric QueueLA = 8; /* option x */ 17357438Seric RefuseLA = 12; /* option X */ 17457438Seric WkRecipFact = 30000L; /* option y */ 17557438Seric WkClassFact = 1800L; /* option z */ 17657438Seric WkTimeFact = 90000L; /* option Z */ 17757438Seric QueueFactor = WkRecipFact * 20; /* option q */ 17863787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 17957438Seric /* option F */ 18057438Seric DefUid = 1; /* option u */ 18157438Seric DefGid = 1; /* option g */ 18257438Seric CheckpointInterval = 10; /* option C */ 18357438Seric MaxHopCount = 25; /* option h */ 18458734Seric e->e_sendmode = SM_FORK; /* option d */ 18558734Seric e->e_errormode = EM_PRINT; /* option e */ 18659709Seric SevenBit = FALSE; /* option 7 */ 18757438Seric MaxMciCache = 1; /* option k */ 18857438Seric MciCacheTimeout = 300; /* option K */ 18957438Seric LogLevel = 9; /* option L */ 19058112Seric settimeouts(NULL); /* option r */ 19158737Seric TimeOuts.to_q_return = 5 DAYS; /* option T */ 19258737Seric TimeOuts.to_q_warning = 0; /* option T */ 19358853Seric PrivacyFlags = 0; /* option p */ 19440973Sbostic setdefuser(); 19553654Seric setupmaps(); 19657402Seric setupmailers(); 19724943Seric } 198294Seric 19940973Sbostic 2004326Seric /* 20140973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20240973Sbostic */ 20340973Sbostic 20440973Sbostic setdefuser() 20540973Sbostic { 20640973Sbostic struct passwd *defpwent; 20757386Seric static char defuserbuf[40]; 20840973Sbostic 20957386Seric DefUser = defuserbuf; 21040973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 21157386Seric strcpy(defuserbuf, defpwent->pw_name); 21240973Sbostic else 21357386Seric strcpy(defuserbuf, "nobody"); 21440973Sbostic } 21553654Seric /* 21656836Seric ** HOST_MAP_INIT -- initialize host class structures 21756836Seric */ 21856836Seric 21956836Seric bool 22060219Seric host_map_init(map, args) 22156836Seric MAP *map; 22256836Seric char *args; 22356836Seric { 22456836Seric register char *p = args; 22556836Seric 22656836Seric for (;;) 22756836Seric { 22858050Seric while (isascii(*p) && isspace(*p)) 22956836Seric p++; 23056836Seric if (*p != '-') 23156836Seric break; 23256836Seric switch (*++p) 23356836Seric { 23456836Seric case 'a': 23556836Seric map->map_app = ++p; 23656836Seric break; 23756836Seric } 23858050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 23956836Seric p++; 24056836Seric if (*p != '\0') 24156836Seric *p++ = '\0'; 24256836Seric } 24356836Seric if (map->map_app != NULL) 24456836Seric map->map_app = newstr(map->map_app); 24556836Seric return TRUE; 24656836Seric } 24757402Seric /* 24857402Seric ** SETUPMAILERS -- initialize default mailers 24957402Seric */ 25056836Seric 25157402Seric setupmailers() 25257402Seric { 25357402Seric char buf[100]; 25457402Seric 25557403Seric strcpy(buf, "prog, P=/bin/sh, F=lsD, A=sh -c $u"); 25657403Seric makemailer(buf); 25757403Seric 25859883Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEu, A=FILE"); 25957402Seric makemailer(buf); 26057402Seric 26157402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 26257402Seric makemailer(buf); 26357402Seric } 26456836Seric /* 26560207Seric ** SETUPMAPS -- set up map classes 26660207Seric */ 26760207Seric 26860207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 26960207Seric { \ 27060207Seric extern bool parse __P((MAP *, char *)); \ 27160207Seric extern bool open __P((MAP *, int)); \ 27260207Seric extern void close __P((MAP *)); \ 27360207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 27460207Seric extern void store __P((MAP *, char *, char *)); \ 27560207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 27660207Seric s->s_mapclass.map_cname = name; \ 27760207Seric s->s_mapclass.map_ext = ext; \ 27860207Seric s->s_mapclass.map_cflags = flags; \ 27960207Seric s->s_mapclass.map_parse = parse; \ 28060207Seric s->s_mapclass.map_open = open; \ 28160207Seric s->s_mapclass.map_close = close; \ 28260207Seric s->s_mapclass.map_lookup = lookup; \ 28360207Seric s->s_mapclass.map_store = store; \ 28460207Seric } 28560207Seric 28660207Seric setupmaps() 28760207Seric { 28860207Seric register STAB *s; 28960207Seric 29060207Seric #ifdef NEWDB 29160207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29260207Seric map_parseargs, hash_map_open, db_map_close, 29360207Seric db_map_lookup, db_map_store); 29460207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29560207Seric map_parseargs, bt_map_open, db_map_close, 29660207Seric db_map_lookup, db_map_store); 29760207Seric #endif 29860207Seric 29960207Seric #ifdef NDBM 30060207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 30160207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 30260207Seric ndbm_map_lookup, ndbm_map_store); 30360207Seric #endif 30460207Seric 30560207Seric #ifdef NIS 30660207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 30760207Seric map_parseargs, nis_map_open, nis_map_close, 30860207Seric nis_map_lookup, nis_map_store); 30960207Seric #endif 31060207Seric 31160207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 31260207Seric map_parseargs, stab_map_open, stab_map_close, 31360207Seric stab_map_lookup, stab_map_store); 31460207Seric 31560207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 31660207Seric map_parseargs, impl_map_open, impl_map_close, 31760207Seric impl_map_lookup, impl_map_store); 31860207Seric 31960207Seric /* host DNS lookup */ 32060207Seric MAPDEF("host", NULL, 0, 32160207Seric host_map_init, null_map_open, null_map_close, 32260207Seric host_map_lookup, null_map_store); 32360207Seric 32460207Seric /* dequote map */ 32560207Seric MAPDEF("dequote", NULL, 0, 32660207Seric dequote_init, null_map_open, null_map_close, 32760207Seric dequote_map, null_map_store); 32860207Seric 32960207Seric #if 0 33060207Seric # ifdef USERDB 33160207Seric /* user database */ 33260207Seric MAPDEF("udb", ".db", 0, 33360207Seric udb_map_parse, null_map_open, null_map_close, 33460207Seric udb_map_lookup, null_map_store); 33560207Seric # endif 33660207Seric #endif 33760207Seric } 33860207Seric 33960207Seric #undef MAPDEF 34060207Seric /* 3419369Seric ** USERNAME -- return the user id of the logged in user. 3429369Seric ** 3439369Seric ** Parameters: 3449369Seric ** none. 3459369Seric ** 3469369Seric ** Returns: 3479369Seric ** The login name of the logged in user. 3489369Seric ** 3499369Seric ** Side Effects: 3509369Seric ** none. 3519369Seric ** 3529369Seric ** Notes: 3539369Seric ** The return value is statically allocated. 3549369Seric */ 3559369Seric 3569369Seric char * 3579369Seric username() 3589369Seric { 35917469Seric static char *myname = NULL; 3609369Seric extern char *getlogin(); 36119904Smiriam register struct passwd *pw; 3629369Seric 36317469Seric /* cache the result */ 36417469Seric if (myname == NULL) 36517469Seric { 36617469Seric myname = getlogin(); 36717469Seric if (myname == NULL || myname[0] == '\0') 36817469Seric { 36963787Seric pw = getpwuid(RealUid); 37017469Seric if (pw != NULL) 37140993Sbostic myname = newstr(pw->pw_name); 37217469Seric } 37319904Smiriam else 37419904Smiriam { 37563787Seric uid_t uid = RealUid; 37619873Smiriam 37740993Sbostic myname = newstr(myname); 37840993Sbostic if ((pw = getpwnam(myname)) == NULL || 37958736Seric (uid != 0 && uid != pw->pw_uid)) 38019904Smiriam { 38158736Seric pw = getpwuid(uid); 38224945Seric if (pw != NULL) 38340993Sbostic myname = newstr(pw->pw_name); 38419873Smiriam } 38519873Smiriam } 38617469Seric if (myname == NULL || myname[0] == '\0') 38717469Seric { 38858151Seric syserr("554 Who are you?"); 38917469Seric myname = "postmaster"; 39017469Seric } 39117469Seric } 39217469Seric 39317469Seric return (myname); 3949369Seric } 3959369Seric /* 3964190Seric ** TTYPATH -- Get the path of the user's tty 397294Seric ** 398294Seric ** Returns the pathname of the user's tty. Returns NULL if 399294Seric ** the user is not logged in or if s/he has write permission 400294Seric ** denied. 401294Seric ** 402294Seric ** Parameters: 403294Seric ** none 404294Seric ** 405294Seric ** Returns: 406294Seric ** pathname of the user's tty. 407294Seric ** NULL if not logged in or write permission denied. 408294Seric ** 409294Seric ** Side Effects: 410294Seric ** none. 411294Seric ** 412294Seric ** WARNING: 413294Seric ** Return value is in a local buffer. 414294Seric ** 415294Seric ** Called By: 416294Seric ** savemail 417294Seric */ 418294Seric 419294Seric char * 420294Seric ttypath() 421294Seric { 422294Seric struct stat stbuf; 423294Seric register char *pathn; 424294Seric extern char *ttyname(); 4254081Seric extern char *getlogin(); 426294Seric 427294Seric /* compute the pathname of the controlling tty */ 4289369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 4299369Seric (pathn = ttyname(0)) == NULL) 430294Seric { 431294Seric errno = 0; 432294Seric return (NULL); 433294Seric } 434294Seric 435294Seric /* see if we have write permission */ 4362967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 437294Seric { 438294Seric errno = 0; 439294Seric return (NULL); 440294Seric } 441294Seric 442294Seric /* see if the user is logged in */ 443294Seric if (getlogin() == NULL) 444294Seric return (NULL); 445294Seric 446294Seric /* looks good */ 447294Seric return (pathn); 448294Seric } 4492967Seric /* 4502967Seric ** CHECKCOMPAT -- check for From and To person compatible. 4512967Seric ** 4522967Seric ** This routine can be supplied on a per-installation basis 4532967Seric ** to determine whether a person is allowed to send a message. 4542967Seric ** This allows restriction of certain types of internet 4552967Seric ** forwarding or registration of users. 4562967Seric ** 4572967Seric ** If the hosts are found to be incompatible, an error 45857454Seric ** message should be given using "usrerr" and 0 should 4592967Seric ** be returned. 4602967Seric ** 461*67473Seric ** EF_NORETURN can be set in e->e_flags to suppress the return-to-sender 4624288Seric ** function; this should be done on huge messages. 4634288Seric ** 4642967Seric ** Parameters: 4652967Seric ** to -- the person being sent to. 4662967Seric ** 4672967Seric ** Returns: 46857459Seric ** an exit status 4692967Seric ** 4702967Seric ** Side Effects: 4712967Seric ** none (unless you include the usrerr stuff) 4722967Seric */ 4732967Seric 47455012Seric checkcompat(to, e) 4752967Seric register ADDRESS *to; 47655012Seric register ENVELOPE *e; 4772967Seric { 47812133Seric # ifdef lint 47912133Seric if (to == NULL) 48012133Seric to++; 48163834Seric # endif /* lint */ 48264402Seric 48364402Seric if (tTd(49, 1)) 48464402Seric printf("checkcompat(to=%s, from=%s)\n", 48564402Seric to->q_paddr, e->e_from.q_paddr); 48664402Seric 48710698Seric # ifdef EXAMPLE_CODE 48810698Seric /* this code is intended as an example only */ 4894437Seric register STAB *s; 4904437Seric 4914437Seric s = stab("arpa", ST_MAILER, ST_FIND); 492*67473Seric if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 && 4939369Seric to->q_mailer == s->s_mailer) 4944437Seric { 49558151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 496*67473Seric /* e->e_flags |= EF_NORETURN; to supress return copy */ 49757459Seric return (EX_UNAVAILABLE); 4984437Seric } 49956795Seric # endif /* EXAMPLE_CODE */ 50057459Seric return (EX_OK); 5012967Seric } 5029369Seric /* 50364035Seric ** SETSIGNAL -- set a signal handler 50464035Seric ** 50564035Seric ** This is essentially old BSD "signal(3)". 50664035Seric */ 50764035Seric 50864561Seric sigfunc_t 50964035Seric setsignal(sig, handler) 51064035Seric int sig; 51164561Seric sigfunc_t handler; 51264035Seric { 51364380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 51464035Seric return signal(sig, handler); 51564035Seric #else 51664035Seric struct sigaction n, o; 51764035Seric 51864035Seric bzero(&n, sizeof n); 51964035Seric n.sa_handler = handler; 52067159Seric # ifdef SA_RESTART 52167159Seric n.sa_flags = SA_RESTART; 52267159Seric # endif 52364035Seric if (sigaction(sig, &n, &o) < 0) 52464035Seric return SIG_ERR; 52564035Seric return o.sa_handler; 52664035Seric #endif 52764035Seric } 52864035Seric /* 5299369Seric ** HOLDSIGS -- arrange to hold all signals 5309369Seric ** 5319369Seric ** Parameters: 5329369Seric ** none. 5339369Seric ** 5349369Seric ** Returns: 5359369Seric ** none. 5369369Seric ** 5379369Seric ** Side Effects: 5389369Seric ** Arranges that signals are held. 5399369Seric */ 5409369Seric 5419369Seric holdsigs() 5429369Seric { 5439369Seric } 5449369Seric /* 5459369Seric ** RLSESIGS -- arrange to release all signals 5469369Seric ** 5479369Seric ** This undoes the effect of holdsigs. 5489369Seric ** 5499369Seric ** Parameters: 5509369Seric ** none. 5519369Seric ** 5529369Seric ** Returns: 5539369Seric ** none. 5549369Seric ** 5559369Seric ** Side Effects: 5569369Seric ** Arranges that signals are released. 5579369Seric */ 5589369Seric 5599369Seric rlsesigs() 5609369Seric { 5619369Seric } 56214872Seric /* 56364705Seric ** INIT_MD -- do machine dependent initializations 56464705Seric ** 56564705Seric ** Systems that have global modes that should be set should do 56664705Seric ** them here rather than in main. 56764705Seric */ 56864705Seric 56964705Seric #ifdef _AUX_SOURCE 57064705Seric # include <compat.h> 57164705Seric #endif 57264705Seric 57364825Seric init_md(argc, argv) 57464825Seric int argc; 57564825Seric char **argv; 57664705Seric { 57764705Seric #ifdef _AUX_SOURCE 57864705Seric setcompat(getcompat() | COMPAT_BSDPROT); 57964705Seric #endif 58064705Seric } 58164705Seric /* 58214872Seric ** GETLA -- get the current load average 58314872Seric ** 58414881Seric ** This code stolen from la.c. 58514881Seric ** 58614872Seric ** Parameters: 58714872Seric ** none. 58814872Seric ** 58914872Seric ** Returns: 59014872Seric ** The current load average as an integer. 59114872Seric ** 59214872Seric ** Side Effects: 59314872Seric ** none. 59414872Seric */ 59514872Seric 59651920Seric /* try to guess what style of load average we have */ 59751920Seric #define LA_ZERO 1 /* always return load average as zero */ 59864360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 59951920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 60051920Seric #define LA_SUBR 4 /* call getloadavg */ 60164295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 60264360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 60366301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 60414872Seric 60563962Seric /* do guesses based on general OS type */ 60651920Seric #ifndef LA_TYPE 60763962Seric # define LA_TYPE LA_ZERO 60851920Seric #endif 60951920Seric 61064360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 61151920Seric 61214872Seric #include <nlist.h> 61314872Seric 61451920Seric #ifndef LA_AVENRUN 61563962Seric # ifdef SYSTEM5 61663962Seric # define LA_AVENRUN "avenrun" 61763962Seric # else 61863962Seric # define LA_AVENRUN "_avenrun" 61963962Seric # endif 62051920Seric #endif 62151920Seric 62251920Seric /* _PATH_UNIX should be defined in <paths.h> */ 62351920Seric #ifndef _PATH_UNIX 62463962Seric # if defined(SYSTEM5) 62563962Seric # define _PATH_UNIX "/unix" 62663962Seric # else 62763962Seric # define _PATH_UNIX "/vmunix" 62863962Seric # endif 62951920Seric #endif 63051920Seric 63114872Seric struct nlist Nl[] = 63214872Seric { 63351920Seric { LA_AVENRUN }, 63414872Seric #define X_AVENRUN 0 63514872Seric { 0 }, 63614872Seric }; 63714872Seric 63860583Seric #ifndef FSHIFT 63960583Seric # if defined(unixpc) 64060583Seric # define FSHIFT 5 64160583Seric # endif 64257736Seric 64366763Seric # if defined(__alpha) || defined(IRIX) 64460583Seric # define FSHIFT 10 64560583Seric # endif 64666788Seric #endif 64757977Seric 64866788Seric #ifndef FSHIFT 64966788Seric # define FSHIFT 8 65057736Seric #endif 65160583Seric 65266788Seric #ifndef FSCALE 65366788Seric # define FSCALE (1 << FSHIFT) 65451920Seric #endif 65540930Srick 65614872Seric getla() 65714872Seric { 65814872Seric static int kmem = -1; 65951920Seric #if LA_TYPE == LA_INT 66024943Seric long avenrun[3]; 66151920Seric #else 66264360Seric # if LA_TYPE == LA_SHORT 66364360Seric short avenrun[3]; 66464360Seric # else 66551920Seric double avenrun[3]; 66664360Seric # endif 66751920Seric #endif 66825615Seric extern off_t lseek(); 66957736Seric extern int errno; 67014872Seric 67114872Seric if (kmem < 0) 67214872Seric { 67324945Seric kmem = open("/dev/kmem", 0, 0); 67414872Seric if (kmem < 0) 67557736Seric { 67657736Seric if (tTd(3, 1)) 67757736Seric printf("getla: open(/dev/kmem): %s\n", 67857736Seric errstring(errno)); 67914872Seric return (-1); 68057736Seric } 68151920Seric (void) fcntl(kmem, F_SETFD, 1); 68257736Seric if (nlist(_PATH_UNIX, Nl) < 0) 68357736Seric { 68457736Seric if (tTd(3, 1)) 68557736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 68657736Seric errstring(errno)); 68714872Seric return (-1); 68857736Seric } 68959253Seric if (Nl[X_AVENRUN].n_value == 0) 69059253Seric { 69159253Seric if (tTd(3, 1)) 69259253Seric printf("getla: nlist(%s, %s) ==> 0\n", 69359253Seric _PATH_UNIX, LA_AVENRUN); 69459253Seric return (-1); 69559253Seric } 69667435Seric #ifdef NAMELISTMASK 69767435Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 69866763Seric #endif 69914872Seric } 70057736Seric if (tTd(3, 20)) 70157736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 70224945Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 || 70323118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 70419967Seric { 70519967Seric /* thank you Ian */ 70657736Seric if (tTd(3, 1)) 70757736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 70819967Seric return (-1); 70919967Seric } 71064360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 71157736Seric if (tTd(3, 5)) 71257736Seric { 71357736Seric printf("getla: avenrun = %d", avenrun[0]); 71457736Seric if (tTd(3, 15)) 71557736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 71657736Seric printf("\n"); 71757736Seric } 71857736Seric if (tTd(3, 1)) 71957736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 72024943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 72151920Seric #else 72257736Seric if (tTd(3, 5)) 72357736Seric { 72457736Seric printf("getla: avenrun = %g", avenrun[0]); 72557736Seric if (tTd(3, 15)) 72657736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 72757736Seric printf("\n"); 72857736Seric } 72957736Seric if (tTd(3, 1)) 73057736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 73151920Seric return ((int) (avenrun[0] + 0.5)); 73251920Seric #endif 73314872Seric } 73414872Seric 73551773Seric #else 73651920Seric #if LA_TYPE == LA_SUBR 73751773Seric 73864718Seric #ifdef DGUX 73964718Seric 74064718Seric #include <sys/dg_sys_info.h> 74164718Seric 74264718Seric int getla() 74364718Seric { 74464718Seric struct dg_sys_info_load_info load_info; 74564718Seric 74664718Seric dg_sys_info((long *)&load_info, 74764718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 74864718Seric 74964718Seric return((int) (load_info.one_minute + 0.5)); 75064718Seric } 75164718Seric 75264718Seric #else 75364718Seric 75451773Seric getla() 75551773Seric { 75651920Seric double avenrun[3]; 75751920Seric 75851920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 75957736Seric { 76057736Seric if (tTd(3, 1)) 76157736Seric perror("getla: getloadavg failed:"); 76251920Seric return (-1); 76357736Seric } 76457736Seric if (tTd(3, 1)) 76557736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 76651920Seric return ((int) (avenrun[0] + 0.5)); 76751773Seric } 76851773Seric 76964718Seric #endif /* DGUX */ 77051773Seric #else 77164295Seric #if LA_TYPE == LA_MACH 77251773Seric 77364295Seric /* 77465173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 77564295Seric */ 77664295Seric 77765173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 77865173Seric # include <mach/mach.h> 77965173Seric #else 78065173Seric # include <mach.h> 78165173Seric #endif 78264295Seric 78351773Seric getla() 78451773Seric { 78564295Seric processor_set_t default_set; 78664295Seric kern_return_t error; 78764295Seric unsigned int info_count; 78864295Seric struct processor_set_basic_info info; 78964295Seric host_t host; 79064295Seric 79164295Seric error = processor_set_default(host_self(), &default_set); 79264295Seric if (error != KERN_SUCCESS) 79364295Seric return -1; 79464295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 79564295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 79664295Seric &host, (processor_set_info_t)&info, 79764295Seric &info_count) != KERN_SUCCESS) 79864295Seric { 79964295Seric return -1; 80064295Seric } 80164295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 80264295Seric } 80364295Seric 80464295Seric 80564295Seric #else 80666313Seric #if LA_TYPE == LA_PROCSTR 80764295Seric 80866301Seric /* 80966301Seric ** Read /proc/loadavg for the load average. This is assumed to be 81066301Seric ** in a format like "0.15 0.12 0.06". 81166301Seric ** 81266301Seric ** Initially intended for Linux. This has been in the kernel 81366301Seric ** since at least 0.99.15. 81466301Seric */ 81566301Seric 81666301Seric # ifndef _PATH_LOADAVG 81766301Seric # define _PATH_LOADAVG "/proc/loadavg" 81866301Seric # endif 81966301Seric 82066301Seric int 82164295Seric getla() 82264295Seric { 82366319Seric double avenrun; 82466301Seric register int result; 82566319Seric FILE *fp; 82666301Seric 82766319Seric fp = fopen(_PATH_LOADAVG, "r"); 82866319Seric if (fp == NULL) 82966301Seric { 83066319Seric if (tTd(3, 1)) 83166319Seric printf("getla: fopen(%s): %s\n", 83266319Seric _PATH_LOADAVG, errstring(errno)); 83366319Seric return -1; 83466301Seric } 83566301Seric result = fscanf(fp, "%lf", &avenrun); 83666319Seric fclose(fp); 83766301Seric if (result != 1) 83866301Seric { 83966310Seric if (tTd(3, 1)) 84066301Seric printf("getla: fscanf() = %d: %s\n", 84166301Seric result, errstring(errno)); 84266301Seric return -1; 84366301Seric } 84466301Seric 84557736Seric if (tTd(3, 1)) 84666301Seric printf("getla(): %.2f\n", avenrun); 84766301Seric 84866301Seric return ((int) (avenrun + 0.5)); 84966301Seric } 85066301Seric 85166301Seric #else 85266301Seric 85366301Seric getla() 85466301Seric { 85566301Seric if (tTd(3, 1)) 85657736Seric printf("getla: ZERO\n"); 85751920Seric return (0); 85851773Seric } 85951773Seric 86051773Seric #endif 86151773Seric #endif 86264295Seric #endif 86366301Seric #endif 86466045Seric 86566045Seric 86666045Seric /* 86766045Seric * Copyright 1989 Massachusetts Institute of Technology 86866045Seric * 86966045Seric * Permission to use, copy, modify, distribute, and sell this software and its 87066045Seric * documentation for any purpose is hereby granted without fee, provided that 87166045Seric * the above copyright notice appear in all copies and that both that 87266045Seric * copyright notice and this permission notice appear in supporting 87366045Seric * documentation, and that the name of M.I.T. not be used in advertising or 87466045Seric * publicity pertaining to distribution of the software without specific, 87566045Seric * written prior permission. M.I.T. makes no representations about the 87666045Seric * suitability of this software for any purpose. It is provided "as is" 87766045Seric * without express or implied warranty. 87866045Seric * 87966045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 88066045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 88166045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 88266045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 88366045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 88466045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 88566045Seric * 88666045Seric * Authors: Many and varied... 88766045Seric */ 88866045Seric 88966045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 89066045Seric #ifndef lint 89166045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 89266045Seric #endif /* !lint */ 89366045Seric 89466179Seric #ifdef apollo 89566045Seric # undef volatile 89666045Seric # include <apollo/base.h> 89766045Seric 89866045Seric /* ARGSUSED */ 89966045Seric int getloadavg( call_data ) 90066045Seric caddr_t call_data; /* pointer to (double) return value */ 90166045Seric { 90266045Seric double *avenrun = (double *) call_data; 90366045Seric int i; 90466045Seric status_$t st; 90566045Seric long loadav[3]; 90666045Seric proc1_$get_loadav(loadav, &st); 90766045Seric *avenrun = loadav[0] / (double) (1 << 16); 90866045Seric return(0); 90966045Seric } 91066045Seric # endif /* apollo */ 91124943Seric /* 91224943Seric ** SHOULDQUEUE -- should this message be queued or sent? 91324943Seric ** 91424943Seric ** Compares the message cost to the load average to decide. 91524943Seric ** 91624943Seric ** Parameters: 91724943Seric ** pri -- the priority of the message in question. 91857438Seric ** ctime -- the message creation time. 91924943Seric ** 92024943Seric ** Returns: 92124943Seric ** TRUE -- if this message should be queued up for the 92224943Seric ** time being. 92324943Seric ** FALSE -- if the load is low enough to send this message. 92424943Seric ** 92524943Seric ** Side Effects: 92624943Seric ** none. 92724943Seric */ 92824943Seric 92924943Seric bool 93057438Seric shouldqueue(pri, ctime) 93124943Seric long pri; 93257438Seric time_t ctime; 93324943Seric { 93451920Seric if (CurrentLA < QueueLA) 93524943Seric return (FALSE); 93658132Seric if (CurrentLA >= RefuseLA) 93758132Seric return (TRUE); 93851920Seric return (pri > (QueueFactor / (CurrentLA - QueueLA + 1))); 93924943Seric } 94024943Seric /* 94153037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 94253037Seric ** 94353037Seric ** Parameters: 94453037Seric ** none. 94553037Seric ** 94653037Seric ** Returns: 94753037Seric ** TRUE if incoming SMTP connections should be refused 94853037Seric ** (for now). 94953037Seric ** FALSE if we should accept new work. 95053037Seric ** 95153037Seric ** Side Effects: 95253037Seric ** none. 95353037Seric */ 95453037Seric 95553037Seric bool 95653037Seric refuseconnections() 95753037Seric { 95859156Seric #ifdef XLA 95959156Seric if (!xla_smtp_ok()) 96059156Seric return TRUE; 96159156Seric #endif 96259156Seric 96353037Seric /* this is probably too simplistic */ 96458132Seric return (CurrentLA >= RefuseLA); 96553037Seric } 96653037Seric /* 96724943Seric ** SETPROCTITLE -- set process title for ps 96824943Seric ** 96924943Seric ** Parameters: 97058674Seric ** fmt -- a printf style format string. 97158674Seric ** a, b, c -- possible parameters to fmt. 97224943Seric ** 97324943Seric ** Returns: 97424943Seric ** none. 97524943Seric ** 97624943Seric ** Side Effects: 97724943Seric ** Clobbers argv of our main procedure so ps(1) will 97824943Seric ** display the title. 97924943Seric */ 98024943Seric 98158689Seric #ifdef SETPROCTITLE 98266002Seric # ifdef HASSETPROCTITLE 98366002Seric *** ERROR *** Cannot have both SETPROCTITLE and HASSETPROCTITLE defined 98466002Seric # endif 98558689Seric # ifdef __hpux 98658689Seric # include <sys/pstat.h> 98758689Seric # endif 98859732Seric # ifdef BSD4_4 98959732Seric # include <machine/vmparam.h> 99059732Seric # include <sys/exec.h> 99164247Seric # ifdef __bsdi__ 99264247Seric # undef PS_STRINGS /* BSDI 1.0 doesn't do PS_STRINGS as we expect */ 99364843Seric # define PROCTITLEPAD '\0' 99464247Seric # endif 99559992Seric # ifdef PS_STRINGS 99659992Seric # define SETPROC_STATIC static 99759992Seric # endif 99859732Seric # endif 99959732Seric # ifndef SETPROC_STATIC 100059732Seric # define SETPROC_STATIC 100159732Seric # endif 100258689Seric #endif 100358689Seric 100464843Seric #ifndef PROCTITLEPAD 100564843Seric # define PROCTITLEPAD ' ' 100664843Seric #endif 100764843Seric 100865982Seric #ifndef HASSETPROCTITLE 100965982Seric 101024943Seric /*VARARGS1*/ 101157642Seric #ifdef __STDC__ 101257642Seric setproctitle(char *fmt, ...) 101357642Seric #else 101457642Seric setproctitle(fmt, va_alist) 101524943Seric char *fmt; 101657642Seric va_dcl 101757642Seric #endif 101824943Seric { 101924943Seric # ifdef SETPROCTITLE 102024943Seric register char *p; 102125049Seric register int i; 102259732Seric SETPROC_STATIC char buf[MAXLINE]; 102356852Seric VA_LOCAL_DECL 102458689Seric # ifdef __hpux 102558689Seric union pstun pst; 102658689Seric # endif 102724943Seric extern char **Argv; 102824943Seric extern char *LastArgv; 102924943Seric 103058674Seric p = buf; 103124943Seric 103258674Seric /* print sendmail: heading for grep */ 103358674Seric (void) strcpy(p, "sendmail: "); 103458674Seric p += strlen(p); 103524943Seric 103658674Seric /* print the argument string */ 103758674Seric VA_START(fmt); 103858674Seric (void) vsprintf(p, fmt, ap); 103956852Seric VA_END; 104054996Seric 104158674Seric i = strlen(buf); 104258689Seric 104358689Seric # ifdef __hpux 104458689Seric pst.pst_command = buf; 104558689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 104658689Seric # else 104759992Seric # ifdef PS_STRINGS 104859732Seric PS_STRINGS->ps_nargvstr = 1; 104959732Seric PS_STRINGS->ps_argvstr = buf; 105059732Seric # else 105154996Seric if (i > LastArgv - Argv[0] - 2) 105225049Seric { 105354996Seric i = LastArgv - Argv[0] - 2; 105458674Seric buf[i] = '\0'; 105525049Seric } 105658674Seric (void) strcpy(Argv[0], buf); 105754997Seric p = &Argv[0][i]; 105824943Seric while (p < LastArgv) 105964843Seric *p++ = PROCTITLEPAD; 106059732Seric # endif 106158689Seric # endif 106256795Seric # endif /* SETPROCTITLE */ 106324943Seric } 106465982Seric 106565982Seric #endif 106625698Seric /* 106725698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 106825698Seric ** 106925698Seric ** Parameters: 107025698Seric ** none. 107125698Seric ** 107225698Seric ** Returns: 107325698Seric ** none. 107425698Seric ** 107525698Seric ** Side Effects: 107625698Seric ** Picks up extant zombies. 107725698Seric */ 107825698Seric 107946928Sbostic void 108025698Seric reapchild() 108125698Seric { 108264738Seric int olderrno = errno; 108363966Seric # ifdef HASWAITPID 108460219Seric auto int status; 108560560Seric int count; 108660560Seric int pid; 108760219Seric 108860560Seric count = 0; 108960560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 109060560Seric { 109160560Seric if (count++ > 1000) 109260560Seric { 109366748Seric #ifdef LOG 109460560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 109560560Seric pid, status); 109666748Seric #endif 109760560Seric break; 109860560Seric } 109960560Seric } 110060219Seric # else 110125698Seric # ifdef WNOHANG 110225698Seric union wait status; 110325698Seric 110463753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 110525698Seric continue; 110656795Seric # else /* WNOHANG */ 110725698Seric auto int status; 110825698Seric 110960219Seric while (wait(&status) > 0) 111025698Seric continue; 111156795Seric # endif /* WNOHANG */ 111260219Seric # endif 111364035Seric # ifdef SYS5SIGNALS 111464035Seric (void) setsignal(SIGCHLD, reapchild); 111558061Seric # endif 111664738Seric errno = olderrno; 111725698Seric } 111855418Seric /* 111955418Seric ** UNSETENV -- remove a variable from the environment 112055418Seric ** 112155418Seric ** Not needed on newer systems. 112255418Seric ** 112355418Seric ** Parameters: 112455418Seric ** name -- the string name of the environment variable to be 112555418Seric ** deleted from the current environment. 112655418Seric ** 112755418Seric ** Returns: 112855418Seric ** none. 112955418Seric ** 113055418Seric ** Globals: 113155418Seric ** environ -- a pointer to the current environment. 113255418Seric ** 113355418Seric ** Side Effects: 113455418Seric ** Modifies environ. 113555418Seric */ 113655418Seric 113763962Seric #ifndef HASUNSETENV 113855418Seric 113955418Seric void 114055418Seric unsetenv(name) 114155418Seric char *name; 114255418Seric { 114355418Seric extern char **environ; 114455418Seric register char **pp; 114555418Seric int len = strlen(name); 114655418Seric 114755418Seric for (pp = environ; *pp != NULL; pp++) 114855418Seric { 114955418Seric if (strncmp(name, *pp, len) == 0 && 115055418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 115155418Seric break; 115255418Seric } 115355418Seric 115455418Seric for (; *pp != NULL; pp++) 115555418Seric *pp = pp[1]; 115655418Seric } 115755418Seric 115863962Seric #endif 115956215Seric /* 116056215Seric ** GETDTABLESIZE -- return number of file descriptors 116156215Seric ** 116256215Seric ** Only on non-BSD systems 116356215Seric ** 116456215Seric ** Parameters: 116556215Seric ** none 116656215Seric ** 116756215Seric ** Returns: 116856215Seric ** size of file descriptor table 116956215Seric ** 117056215Seric ** Side Effects: 117156215Seric ** none 117256215Seric */ 117356215Seric 117463787Seric #ifdef SOLARIS 117563787Seric # include <sys/resource.h> 117663787Seric #endif 117756215Seric 117856215Seric int 117963787Seric getdtsize() 118056215Seric { 118163787Seric #ifdef RLIMIT_NOFILE 118263787Seric struct rlimit rl; 118363787Seric 118463787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 118563787Seric return rl.rlim_cur; 118663787Seric #endif 118763787Seric 118864031Seric # ifdef HASGETDTABLESIZE 118964031Seric return getdtablesize(); 119064031Seric # else 119164031Seric # ifdef _SC_OPEN_MAX 119258689Seric return sysconf(_SC_OPEN_MAX); 119363787Seric # else 119456215Seric return NOFILE; 119563787Seric # endif 119658689Seric # endif 119756215Seric } 119857631Seric /* 119957631Seric ** UNAME -- get the UUCP name of this system. 120057631Seric */ 120157631Seric 120257943Seric #ifndef HASUNAME 120357631Seric 120457631Seric int 120557631Seric uname(name) 120657631Seric struct utsname *name; 120757631Seric { 120857631Seric FILE *file; 120957631Seric char *n; 121057631Seric 121157631Seric name->nodename[0] = '\0'; 121257631Seric 121357661Seric /* try /etc/whoami -- one line with the node name */ 121457631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 121557631Seric { 121657661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 121757631Seric (void) fclose(file); 121857661Seric n = strchr(name->nodename, '\n'); 121957631Seric if (n != NULL) 122057631Seric *n = '\0'; 122157631Seric if (name->nodename[0] != '\0') 122257631Seric return (0); 122357631Seric } 122457631Seric 122557661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 122657631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 122757631Seric { 122857631Seric char buf[MAXLINE]; 122957631Seric 123057631Seric while (fgets(buf, MAXLINE, file) != NULL) 123157631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 123257631Seric NODE_LENGTH, name->nodename) > 0) 123357631Seric break; 123457631Seric (void) fclose(file); 123557631Seric if (name->nodename[0] != '\0') 123657631Seric return (0); 123757631Seric } 123857631Seric 123957631Seric #ifdef TRUST_POPEN 124057631Seric /* 124157631Seric ** Popen is known to have security holes. 124257631Seric */ 124357631Seric 124457661Seric /* try uuname -l to return local name */ 124557631Seric if ((file = popen("uuname -l", "r")) != NULL) 124657631Seric { 124757661Seric (void) fgets(name, NODE_LENGTH + 1, file); 124857631Seric (void) pclose(file); 124957661Seric n = strchr(name, '\n'); 125057631Seric if (n != NULL) 125157631Seric *n = '\0'; 125257661Seric if (name->nodename[0] != '\0') 125357631Seric return (0); 125457631Seric } 125557631Seric #endif 125657631Seric 125757631Seric return (-1); 125857631Seric } 125957943Seric #endif /* HASUNAME */ 126058068Seric /* 126158068Seric ** INITGROUPS -- initialize groups 126258068Seric ** 126358068Seric ** Stub implementation for System V style systems 126458068Seric */ 126558068Seric 126658068Seric #ifndef HASINITGROUPS 126758068Seric 126858068Seric initgroups(name, basegid) 126958068Seric char *name; 127058068Seric int basegid; 127158068Seric { 127258068Seric return 0; 127358068Seric } 127458068Seric 127558068Seric #endif 127658082Seric /* 127759289Seric ** SETSID -- set session id (for non-POSIX systems) 127859289Seric */ 127959289Seric 128059289Seric #ifndef HASSETSID 128159289Seric 128263753Seric pid_t 128363753Seric setsid __P ((void)) 128459289Seric { 128564155Seric #ifdef TIOCNOTTY 128664155Seric int fd; 128764155Seric 128866743Seric fd = open("/dev/tty", O_RDWR, 0); 128964155Seric if (fd >= 0) 129064155Seric { 129164155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 129264155Seric (void) close(fd); 129364155Seric } 129464155Seric #endif /* TIOCNOTTY */ 129564705Seric # ifdef SYS5SETPGRP 129663753Seric return setpgrp(); 129763753Seric # else 129864155Seric return setpgid(0, getpid()); 129959289Seric # endif 130059289Seric } 130159289Seric 130259289Seric #endif 130359289Seric /* 130466748Seric ** FSYNC -- dummy fsync 130566748Seric */ 130666748Seric 130766748Seric #ifdef NEEDFSYNC 130866748Seric 130966748Seric fsync(fd) 131066748Seric int fd; 131166748Seric { 131266748Seric # ifdef O_SYNC 131366748Seric return fcntl(fd, F_SETFL, O_SYNC); 131466748Seric # else 131566748Seric /* nothing we can do */ 131666748Seric return 0; 131766748Seric # endif 131866748Seric } 131966748Seric 132066748Seric #endif 132166748Seric /* 132264718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 132364718Seric ** 132464718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 132567427Seric ** instead of a long. This patches things. Only needed on versions 132667427Seric ** prior to 5.4.3. 132764718Seric */ 132864718Seric 132967427Seric #ifdef DGUX_5_4_2 133064718Seric 133164718Seric #undef inet_addr 133264718Seric 133364718Seric long 133464718Seric dgux_inet_addr(host) 133564718Seric char *host; 133664718Seric { 133764718Seric struct in_addr haddr; 133864718Seric 133964718Seric haddr = inet_addr(host); 134064718Seric return haddr.s_addr; 134164718Seric } 134264718Seric 134364718Seric #endif 134464718Seric /* 134563969Seric ** GETOPT -- for old systems or systems with bogus implementations 134663969Seric */ 134763969Seric 134863969Seric #ifdef NEEDGETOPT 134963969Seric 135063969Seric /* 135163969Seric * Copyright (c) 1985 Regents of the University of California. 135263969Seric * All rights reserved. The Berkeley software License Agreement 135363969Seric * specifies the terms and conditions for redistribution. 135463969Seric */ 135563969Seric 135663969Seric 135763969Seric /* 135863969Seric ** this version hacked to add `atend' flag to allow state machine 135963969Seric ** to reset if invoked by the program to scan args for a 2nd time 136063969Seric */ 136163969Seric 136263969Seric #if defined(LIBC_SCCS) && !defined(lint) 136363969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 136464072Seric #endif /* LIBC_SCCS and not lint */ 136563969Seric 136663969Seric #include <stdio.h> 136763969Seric 136863969Seric /* 136963969Seric * get option letter from argument vector 137063969Seric */ 137165352Seric #ifdef _CONVEX_SOURCE 137265352Seric extern int optind, opterr; 137365352Seric #else 137465352Seric int opterr = 1; /* if error message should be printed */ 137565352Seric int optind = 1; /* index into parent argv vector */ 137665352Seric #endif 137765352Seric int optopt; /* character checked for validity */ 137863969Seric char *optarg; /* argument associated with option */ 137963969Seric 138063969Seric #define BADCH (int)'?' 138163969Seric #define EMSG "" 138263969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 138363969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 138463969Seric 138563969Seric getopt(nargc,nargv,ostr) 138665160Seric int nargc; 138765160Seric char *const *nargv; 138865160Seric const char *ostr; 138963969Seric { 139063969Seric static char *place = EMSG; /* option letter processing */ 139163969Seric static char atend = 0; 139263969Seric register char *oli; /* option letter list index */ 139363969Seric 139463969Seric if (atend) { 139563969Seric atend = 0; 139663969Seric place = EMSG; 139763969Seric } 139863969Seric if(!*place) { /* update scanning pointer */ 139964136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 140063969Seric atend++; 140163969Seric return(EOF); 140263969Seric } 140363969Seric if (*place == '-') { /* found "--" */ 140463969Seric ++optind; 140563969Seric atend++; 140663969Seric return(EOF); 140763969Seric } 140863969Seric } /* option letter okay? */ 140964125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 141064136Seric if (!*place) ++optind; 141163969Seric tell(": illegal option -- "); 141263969Seric } 141363969Seric if (*++oli != ':') { /* don't need argument */ 141463969Seric optarg = NULL; 141563969Seric if (!*place) ++optind; 141663969Seric } 141763969Seric else { /* need an argument */ 141863969Seric if (*place) optarg = place; /* no white space */ 141963969Seric else if (nargc <= ++optind) { /* no arg */ 142063969Seric place = EMSG; 142163969Seric tell(": option requires an argument -- "); 142263969Seric } 142363969Seric else optarg = nargv[optind]; /* white space */ 142463969Seric place = EMSG; 142563969Seric ++optind; 142663969Seric } 142763969Seric return(optopt); /* dump back option letter */ 142863969Seric } 142963969Seric 143063969Seric #endif 143163969Seric /* 143263969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 143363969Seric */ 143463969Seric 143563969Seric #ifdef NEEDVPRINTF 143663969Seric 143763969Seric #define MAXARG 16 143863969Seric 143963969Seric vfprintf(fp, fmt, ap) 144063969Seric FILE * fp; 144163969Seric char * fmt; 144263969Seric char ** ap; 144363969Seric { 144463969Seric char * bp[MAXARG]; 144563969Seric int i = 0; 144663969Seric 144763969Seric while (*ap && i < MAXARG) 144863969Seric bp[i++] = *ap++; 144963969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 145063969Seric bp[4], bp[5], bp[6], bp[7], 145163969Seric bp[8], bp[9], bp[10], bp[11], 145263969Seric bp[12], bp[13], bp[14], bp[15]); 145363969Seric } 145463969Seric 145563969Seric vsprintf(s, fmt, ap) 145663969Seric char * s; 145763969Seric char * fmt; 145863969Seric char ** ap; 145963969Seric { 146063969Seric char * bp[MAXARG]; 146163969Seric int i = 0; 146263969Seric 146363969Seric while (*ap && i < MAXARG) 146463969Seric bp[i++] = *ap++; 146563969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 146663969Seric bp[4], bp[5], bp[6], bp[7], 146763969Seric bp[8], bp[9], bp[10], bp[11], 146863969Seric bp[12], bp[13], bp[14], bp[15]); 146963969Seric } 147063969Seric 147163969Seric #endif 147263969Seric /* 147365220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 147465208Seric ** 147565211Seric ** Parameters: 147665211Seric ** shell -- the user's shell from /etc/passwd 147765211Seric ** 147865211Seric ** Returns: 147965211Seric ** TRUE -- if it is ok to use this for unrestricted access. 148065211Seric ** FALSE -- if the shell is restricted. 148165208Seric */ 148265208Seric 148365587Seric #if !HASGETUSERSHELL 148465587Seric 148565587Seric # ifndef _PATH_SHELLS 148665587Seric # define _PATH_SHELLS "/etc/shells" 148765587Seric # endif 148865587Seric 148965587Seric char *DefaultUserShells[] = 149065587Seric { 149165587Seric "/bin/sh", 149265587Seric "/usr/bin/sh", 149365587Seric "/bin/csh", 149465587Seric "/usr/bin/csh", 149565587Seric #ifdef __hpux 149665587Seric "/bin/rsh", 149765587Seric "/bin/ksh", 149865587Seric "/bin/rksh", 149965587Seric "/bin/pam", 150065587Seric "/usr/bin/keysh", 150165587Seric "/bin/posix/sh", 150265211Seric #endif 150365587Seric NULL 150465587Seric }; 150565587Seric 150665208Seric #endif 150765208Seric 150865747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 150965747Seric 151065211Seric bool 151165211Seric usershellok(shell) 151265211Seric char *shell; 151365208Seric { 151465211Seric #if HASGETUSERSHELL 151565211Seric register char *p; 151665211Seric extern char *getusershell(); 151765208Seric 151865211Seric setusershell(); 151965211Seric while ((p = getusershell()) != NULL) 152065747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 152165211Seric break; 152265211Seric endusershell(); 152365211Seric return p != NULL; 152465211Seric #else 152565211Seric register FILE *shellf; 152665211Seric char buf[MAXLINE]; 152765208Seric 152865211Seric shellf = fopen(_PATH_SHELLS, "r"); 152965211Seric if (shellf == NULL) 153065211Seric { 153165211Seric /* no /etc/shells; see if it is one of the std shells */ 153265587Seric char **d; 153365587Seric 153465587Seric for (d = DefaultUserShells; *d != NULL; d++) 153565587Seric { 153665587Seric if (strcmp(shell, *d) == 0) 153765587Seric return TRUE; 153865587Seric } 153965587Seric return FALSE; 154065211Seric } 154165208Seric 154265211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 154365211Seric { 154465211Seric register char *p, *q; 154565208Seric 154665211Seric p = buf; 154765211Seric while (*p != '\0' && *p != '#' && *p != '/') 154865211Seric p++; 154965211Seric if (*p == '#' || *p == '\0') 155065208Seric continue; 155165211Seric q = p; 155265211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 155365211Seric p++; 155465211Seric *p = '\0'; 155565747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 155665211Seric { 155765211Seric fclose(shellf); 155865211Seric return TRUE; 155965211Seric } 156065208Seric } 156165211Seric fclose(shellf); 156265211Seric return FALSE; 156365211Seric #endif 156465208Seric } 156565208Seric /* 156664311Seric ** FREESPACE -- see how much free space is on the queue filesystem 156758082Seric ** 156858082Seric ** Only implemented if you have statfs. 156958082Seric ** 157058082Seric ** Parameters: 157164311Seric ** dir -- the directory in question. 157264311Seric ** bsize -- a variable into which the filesystem 157364311Seric ** block size is stored. 157458082Seric ** 157558082Seric ** Returns: 157664311Seric ** The number of bytes free on the queue filesystem. 157764311Seric ** -1 if the statfs call fails. 157864311Seric ** 157964311Seric ** Side effects: 158064311Seric ** Puts the filesystem block size into bsize. 158158082Seric */ 158258082Seric 158365749Seric /* statfs types */ 158465749Seric #define SFS_NONE 0 /* no statfs implementation */ 158565749Seric #define SFS_USTAT 1 /* use ustat */ 158665749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 158765749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 158865749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 158965749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 159066752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 159165749Seric 159265749Seric #ifndef SFS_TYPE 159365749Seric # define SFS_TYPE SFS_NONE 159458157Seric #endif 159558157Seric 159665749Seric #if SFS_TYPE == SFS_USTAT 159758157Seric # include <ustat.h> 159858157Seric #endif 159965749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 160065749Seric # include <sys/statfs.h> 160158082Seric #endif 160265749Seric #if SFS_TYPE == SFS_VFS 160365749Seric # include <sys/vfs.h> 160465749Seric #endif 160565749Seric #if SFS_TYPE == SFS_MOUNT 160665749Seric # include <sys/mount.h> 160765749Seric #endif 160866752Seric #if SFS_TYPE == SFS_STATVFS 160966752Seric # include <sys/statvfs.h> 161066752Seric #endif 161158082Seric 161264311Seric long 161364311Seric freespace(dir, bsize) 161464311Seric char *dir; 161564311Seric long *bsize; 161658082Seric { 161765749Seric #if SFS_TYPE != SFS_NONE 161865749Seric # if SFS_TYPE == SFS_USTAT 161958153Seric struct ustat fs; 162058153Seric struct stat statbuf; 162158366Seric # define FSBLOCKSIZE DEV_BSIZE 162258157Seric # define f_bavail f_tfree 162358157Seric # else 162458157Seric # if defined(ultrix) 162558157Seric struct fs_data fs; 162658157Seric # define f_bavail fd_bfreen 162767467Seric # define FSBLOCKSIZE 1024L 162858153Seric # else 162966752Seric # if SFS_TYPE == SFS_STATVFS 163066752Seric struct statvfs fs; 163166893Seric # define FSBLOCKSIZE fs.f_bsize 163266752Seric # else 163358082Seric struct statfs fs; 163466752Seric # define FSBLOCKSIZE fs.f_bsize 163566752Seric # if defined(_SCO_unix_) || defined(IRIX) || defined(apollo) 163666752Seric # define f_bavail f_bfree 163766752Seric # endif 163863987Seric # endif 163958153Seric # endif 164058133Seric # endif 164158082Seric extern int errno; 164258082Seric 164365749Seric # if SFS_TYPE == SFS_USTAT 164464311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 164558157Seric # else 164665749Seric # if SFS_TYPE == SFS_4ARGS 164764311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 164858157Seric # else 164967159Seric # if SFS_TYPE == SFS_STATVFS 165067159Seric if (statvfs(dir, &fs) == 0) 165167159Seric # else 165267159Seric # if defined(ultrix) 165364311Seric if (statfs(dir, &fs) > 0) 165467159Seric # else 165564311Seric if (statfs(dir, &fs) == 0) 165667159Seric # endif 165758153Seric # endif 165858133Seric # endif 165958133Seric # endif 166058133Seric { 166164311Seric if (bsize != NULL) 166264311Seric *bsize = FSBLOCKSIZE; 166364311Seric return (fs.f_bavail); 166464311Seric } 166564337Seric #endif 166664311Seric return (-1); 166764311Seric } 166864311Seric /* 166964311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 167064311Seric ** 167164311Seric ** Only implemented if you have statfs. 167264311Seric ** 167364311Seric ** Parameters: 167464311Seric ** msize -- the size to check against. If zero, we don't yet 167564311Seric ** know how big the message will be, so just check for 167664311Seric ** a "reasonable" amount. 167764311Seric ** 167864311Seric ** Returns: 167964311Seric ** TRUE if there is enough space. 168064311Seric ** FALSE otherwise. 168164311Seric */ 168264311Seric 168364311Seric bool 168464311Seric enoughspace(msize) 168564311Seric long msize; 168664311Seric { 168764311Seric long bfree, bsize; 168864311Seric 168964311Seric if (MinBlocksFree <= 0 && msize <= 0) 169064311Seric { 169158133Seric if (tTd(4, 80)) 169264311Seric printf("enoughspace: no threshold\n"); 169364311Seric return TRUE; 169464311Seric } 169564311Seric 169664311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 169764311Seric { 169864311Seric if (tTd(4, 80)) 169958333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 170064311Seric bfree, msize); 170158333Seric 170258333Seric /* convert msize to block count */ 170364311Seric msize = msize / bsize + 1; 170458333Seric if (MinBlocksFree >= 0) 170558333Seric msize += MinBlocksFree; 170658333Seric 170764311Seric if (bfree < msize) 170858090Seric { 170958090Seric #ifdef LOG 171058090Seric if (LogLevel > 0) 171164254Seric syslog(LOG_ALERT, 171264254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 171364311Seric CurEnv->e_id, bfree, 171464254Seric CurHostName, msize, QueueDir); 171558090Seric #endif 171658082Seric return FALSE; 171758090Seric } 171858082Seric } 171958082Seric else if (tTd(4, 80)) 172058333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 172158333Seric MinBlocksFree, msize, errstring(errno)); 172258082Seric return TRUE; 172358082Seric } 172458542Seric /* 172558542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 172658542Seric ** 172758542Seric ** This looks at an errno value and tells if this is likely to 172858542Seric ** go away if retried later. 172958542Seric ** 173058542Seric ** Parameters: 173158542Seric ** err -- the errno code to classify. 173258542Seric ** 173358542Seric ** Returns: 173458542Seric ** TRUE if this is probably transient. 173558542Seric ** FALSE otherwise. 173658542Seric */ 173758542Seric 173858542Seric bool 173958542Seric transienterror(err) 174058542Seric int err; 174158542Seric { 174258542Seric switch (err) 174358542Seric { 174458542Seric case EIO: /* I/O error */ 174558542Seric case ENXIO: /* Device not configured */ 174658542Seric case EAGAIN: /* Resource temporarily unavailable */ 174758542Seric case ENOMEM: /* Cannot allocate memory */ 174858542Seric case ENODEV: /* Operation not supported by device */ 174958542Seric case ENFILE: /* Too many open files in system */ 175058542Seric case EMFILE: /* Too many open files */ 175158542Seric case ENOSPC: /* No space left on device */ 175258542Seric #ifdef ETIMEDOUT 175358542Seric case ETIMEDOUT: /* Connection timed out */ 175458542Seric #endif 175558542Seric #ifdef ESTALE 175658542Seric case ESTALE: /* Stale NFS file handle */ 175758542Seric #endif 175858542Seric #ifdef ENETDOWN 175958542Seric case ENETDOWN: /* Network is down */ 176058542Seric #endif 176158542Seric #ifdef ENETUNREACH 176258542Seric case ENETUNREACH: /* Network is unreachable */ 176358542Seric #endif 176458542Seric #ifdef ENETRESET 176558542Seric case ENETRESET: /* Network dropped connection on reset */ 176658542Seric #endif 176758542Seric #ifdef ECONNABORTED 176858542Seric case ECONNABORTED: /* Software caused connection abort */ 176958542Seric #endif 177058542Seric #ifdef ECONNRESET 177158542Seric case ECONNRESET: /* Connection reset by peer */ 177258542Seric #endif 177358542Seric #ifdef ENOBUFS 177458542Seric case ENOBUFS: /* No buffer space available */ 177558542Seric #endif 177658542Seric #ifdef ESHUTDOWN 177758542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 177858542Seric #endif 177958542Seric #ifdef ECONNREFUSED 178058542Seric case ECONNREFUSED: /* Connection refused */ 178158542Seric #endif 178258542Seric #ifdef EHOSTDOWN 178358542Seric case EHOSTDOWN: /* Host is down */ 178458542Seric #endif 178558542Seric #ifdef EHOSTUNREACH 178658542Seric case EHOSTUNREACH: /* No route to host */ 178758542Seric #endif 178858542Seric #ifdef EDQUOT 178958542Seric case EDQUOT: /* Disc quota exceeded */ 179058542Seric #endif 179158542Seric #ifdef EPROCLIM 179258542Seric case EPROCLIM: /* Too many processes */ 179358542Seric #endif 179458542Seric #ifdef EUSERS 179558542Seric case EUSERS: /* Too many users */ 179658542Seric #endif 179758542Seric #ifdef EDEADLK 179858542Seric case EDEADLK: /* Resource deadlock avoided */ 179958542Seric #endif 180058542Seric #ifdef EISCONN 180158542Seric case EISCONN: /* Socket already connected */ 180258542Seric #endif 180358542Seric #ifdef EINPROGRESS 180458542Seric case EINPROGRESS: /* Operation now in progress */ 180558542Seric #endif 180658542Seric #ifdef EALREADY 180758542Seric case EALREADY: /* Operation already in progress */ 180858542Seric #endif 180958542Seric #ifdef EADDRINUSE 181058542Seric case EADDRINUSE: /* Address already in use */ 181158542Seric #endif 181258542Seric #ifdef EADDRNOTAVAIL 181358542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 181458542Seric #endif 181566018Seric #ifdef ETXTBSY 181666017Seric case ETXTBSY: /* (Apollo) file locked */ 181766017Seric #endif 181863834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 181958542Seric case ENOSR: /* Out of streams resources */ 182058542Seric #endif 182158542Seric return TRUE; 182258542Seric } 182358542Seric 182458542Seric /* nope, must be permanent */ 182558542Seric return FALSE; 182658542Seric } 182758689Seric /* 182864035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 182958689Seric ** 183058689Seric ** Parameters: 183158689Seric ** fd -- the file descriptor of the file. 183258689Seric ** filename -- the file name (for error messages). 183364335Seric ** ext -- the filename extension. 183458689Seric ** type -- type of the lock. Bits can be: 183558689Seric ** LOCK_EX -- exclusive lock. 183658689Seric ** LOCK_NB -- non-blocking. 183758689Seric ** 183858689Seric ** Returns: 183958689Seric ** TRUE if the lock was acquired. 184058689Seric ** FALSE otherwise. 184158689Seric */ 184258689Seric 184358689Seric bool 184464335Seric lockfile(fd, filename, ext, type) 184558689Seric int fd; 184658689Seric char *filename; 184764335Seric char *ext; 184858689Seric int type; 184958689Seric { 185065830Seric # if !HASFLOCK 185158689Seric int action; 185258689Seric struct flock lfd; 185364335Seric 185464335Seric if (ext == NULL) 185564335Seric ext = ""; 185664035Seric 185763983Seric bzero(&lfd, sizeof lfd); 185859447Seric if (bitset(LOCK_UN, type)) 185959447Seric lfd.l_type = F_UNLCK; 186059447Seric else if (bitset(LOCK_EX, type)) 186158689Seric lfd.l_type = F_WRLCK; 186258689Seric else 186358689Seric lfd.l_type = F_RDLCK; 186458689Seric 186558689Seric if (bitset(LOCK_NB, type)) 186658689Seric action = F_SETLK; 186758689Seric else 186858689Seric action = F_SETLKW; 186958689Seric 187064264Seric if (tTd(55, 60)) 187164335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 187264335Seric filename, ext, action, lfd.l_type); 187364264Seric 187458689Seric if (fcntl(fd, action, &lfd) >= 0) 187564264Seric { 187664264Seric if (tTd(55, 60)) 187764264Seric printf("SUCCESS\n"); 187858689Seric return TRUE; 187964264Seric } 188058689Seric 188164264Seric if (tTd(55, 60)) 188264264Seric printf("(%s) ", errstring(errno)); 188364264Seric 188464136Seric /* 188564136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 188664136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 188764136Seric ** as type "tmp" (that is, served from swap space), the 188864136Seric ** previous fcntl will fail with "Invalid argument" errors. 188964136Seric ** Since this is fairly common during testing, we will assume 189064136Seric ** that this indicates that the lock is successfully grabbed. 189164136Seric */ 189264136Seric 189364136Seric if (errno == EINVAL) 189464264Seric { 189564264Seric if (tTd(55, 60)) 189664264Seric printf("SUCCESS\n"); 189764136Seric return TRUE; 189864264Seric } 189964136Seric 190058689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 190164378Seric { 190264378Seric int omode = -1; 190364378Seric # ifdef F_GETFL 190464378Seric int oerrno = errno; 190564378Seric 190664378Seric (void) fcntl(fd, F_GETFL, &omode); 190764378Seric errno = oerrno; 190864378Seric # endif 190964378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 191064378Seric filename, ext, fd, type, omode, geteuid()); 191164378Seric } 191258689Seric # else 191364335Seric if (ext == NULL) 191464335Seric ext = ""; 191564335Seric 191664264Seric if (tTd(55, 60)) 191764335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 191864264Seric 191958689Seric if (flock(fd, type) >= 0) 192064264Seric { 192164264Seric if (tTd(55, 60)) 192264264Seric printf("SUCCESS\n"); 192358689Seric return TRUE; 192464264Seric } 192558689Seric 192664264Seric if (tTd(55, 60)) 192764264Seric printf("(%s) ", errstring(errno)); 192864264Seric 192958689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 193064378Seric { 193164378Seric int omode = -1; 193264378Seric # ifdef F_GETFL 193364378Seric int oerrno = errno; 193464378Seric 193564378Seric (void) fcntl(fd, F_GETFL, &omode); 193664378Seric errno = oerrno; 193764378Seric # endif 193864378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 193964378Seric filename, ext, fd, type, omode, geteuid()); 194064386Seric } 194158689Seric # endif 194264264Seric if (tTd(55, 60)) 194364264Seric printf("FAIL\n"); 194458689Seric return FALSE; 194558689Seric } 194664556Seric /* 194765948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 194865948Seric ** 194965948Seric ** Parameters: 195065948Seric ** fd -- the file descriptor to check. 195165948Seric ** 195265948Seric ** Returns: 195365948Seric ** TRUE -- if only root can chown the file to an arbitrary 195465948Seric ** user. 195565948Seric ** FALSE -- if an arbitrary user can give away a file. 195665948Seric */ 195765948Seric 195865948Seric bool 195965948Seric chownsafe(fd) 196065948Seric int fd; 196165948Seric { 196265950Seric #ifdef __hpux 196365948Seric char *s; 196465948Seric int tfd; 196565948Seric uid_t o_uid, o_euid; 196665948Seric gid_t o_gid, o_egid; 196765948Seric bool rval; 196865948Seric struct stat stbuf; 196965948Seric 197065948Seric o_uid = getuid(); 197165948Seric o_euid = geteuid(); 197265948Seric o_gid = getgid(); 197365948Seric o_egid = getegid(); 197465959Seric fstat(fd, &stbuf); 197565948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 197665948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 197765948Seric s = tmpnam(NULL); 197865948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 197965974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 198065948Seric close(tfd); 198165948Seric unlink(s); 198265948Seric setreuid(o_uid, o_euid); 198365948Seric setresgid(o_gid, o_egid, -1); 198465948Seric return rval; 198565948Seric #else 198666088Seric # ifdef _POSIX_CHOWN_RESTRICTED 198766088Seric # if _POSIX_CHOWN_RESTRICTED == -1 198866088Seric return FALSE; 198966088Seric # else 199066088Seric return TRUE; 199166088Seric # endif 199266088Seric # else 199366088Seric # ifdef _PC_CHOWN_RESTRICTED 199467447Seric int rval; 199567447Seric 199667447Seric /* 199767447Seric ** Some systems (e.g., SunOS) seem to have the call and the 199867447Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 199967447Seric ** the call. This heuristic checks for that. 200067447Seric */ 200167447Seric 200267447Seric errno = 0; 200367447Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 200467447Seric if (errno == 0) 200567447Seric return rval > 0; 200667447Seric # endif 200767447Seric # ifdef BSD 200867447Seric return TRUE; 200966088Seric # else 201065948Seric return FALSE; 201165948Seric # endif 201265948Seric # endif 201365948Seric #endif 201465948Seric } 201565948Seric /* 201667430Seric ** RESETLIMITS -- reset system controlled resource limits 201767430Seric ** 201867430Seric ** This is to avoid denial-of-service attacks 201967430Seric ** 202067430Seric ** Parameters: 202167430Seric ** none 202267430Seric ** 202367430Seric ** Returns: 202467430Seric ** none 202567430Seric */ 202667430Seric 202767430Seric #if HASSETRLIMIT 202867430Seric # include <sys/resource.h> 202967430Seric #endif 203067430Seric 203167430Seric resetlimits() 203267430Seric { 203367430Seric #if HASSETRLIMIT 203467430Seric struct rlimit lim; 203567430Seric 203667430Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 203767430Seric (void) setrlimit(RLIMIT_CPU, &lim); 203867430Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 203967430Seric #else 204067430Seric # if HASULIMIT 204167430Seric (void) ulimit(2, 0x3fffff); 204267430Seric # endif 204367430Seric #endif 204467430Seric } 204567430Seric /* 204664556Seric ** GETCFNAME -- return the name of the .cf file. 204764556Seric ** 204864556Seric ** Some systems (e.g., NeXT) determine this dynamically. 204964556Seric */ 205064556Seric 205164556Seric char * 205264556Seric getcfname() 205364556Seric { 205464556Seric if (ConfFile != NULL) 205564556Seric return ConfFile; 205665214Seric #ifdef NETINFO 205765214Seric { 205865214Seric extern char *ni_propval(); 205965214Seric char *cflocation; 206065214Seric 206165214Seric cflocation = ni_propval("/locations/sendmail", "sendmail.cf"); 206265214Seric if (cflocation != NULL) 206365214Seric return cflocation; 206465214Seric } 206565214Seric #endif 206664556Seric return _PATH_SENDMAILCF; 206764556Seric } 206864718Seric /* 206964718Seric ** SETVENDOR -- process vendor code from V configuration line 207064718Seric ** 207164718Seric ** Parameters: 207264718Seric ** vendor -- string representation of vendor. 207364718Seric ** 207464718Seric ** Returns: 207564718Seric ** TRUE -- if ok. 207664718Seric ** FALSE -- if vendor code could not be processed. 207764926Seric ** 207864926Seric ** Side Effects: 207964926Seric ** It is reasonable to set mode flags here to tweak 208064926Seric ** processing in other parts of the code if necessary. 208164926Seric ** For example, if you are a vendor that uses $%y to 208264926Seric ** indicate YP lookups, you could enable that here. 208364718Seric */ 208464718Seric 208564718Seric bool 208664718Seric setvendor(vendor) 208764718Seric char *vendor; 208864718Seric { 208964926Seric if (strcasecmp(vendor, "Berkeley") == 0) 209064926Seric return TRUE; 209164926Seric 209264926Seric /* add vendor extensions here */ 209364926Seric 209464926Seric return FALSE; 209564718Seric } 209664816Seric /* 209764816Seric ** STRTOL -- convert string to long integer 209864816Seric ** 209964816Seric ** For systems that don't have it in the C library. 210066161Seric ** 210166161Seric ** This is taken verbatim from the 4.4-Lite C library. 210264816Seric */ 210364816Seric 210464816Seric #ifdef NEEDSTRTOL 210564816Seric 210666161Seric #if defined(LIBC_SCCS) && !defined(lint) 210766161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 210866161Seric #endif /* LIBC_SCCS and not lint */ 210966161Seric 211066161Seric #include <limits.h> 211166161Seric 211266161Seric /* 211366161Seric * Convert a string to a long integer. 211466161Seric * 211566161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 211666161Seric * alphabets and digits are each contiguous. 211766161Seric */ 211866161Seric 211964816Seric long 212066161Seric strtol(nptr, endptr, base) 212166161Seric const char *nptr; 212266161Seric char **endptr; 212366161Seric register int base; 212464816Seric { 212566161Seric register const char *s = nptr; 212666161Seric register unsigned long acc; 212766161Seric register int c; 212866161Seric register unsigned long cutoff; 212966161Seric register int neg = 0, any, cutlim; 213064816Seric 213166161Seric /* 213266161Seric * Skip white space and pick up leading +/- sign if any. 213366161Seric * If base is 0, allow 0x for hex and 0 for octal, else 213466161Seric * assume decimal; if base is already 16, allow 0x. 213566161Seric */ 213666161Seric do { 213766161Seric c = *s++; 213866161Seric } while (isspace(c)); 213966161Seric if (c == '-') { 214066161Seric neg = 1; 214166161Seric c = *s++; 214266161Seric } else if (c == '+') 214366161Seric c = *s++; 214466161Seric if ((base == 0 || base == 16) && 214566161Seric c == '0' && (*s == 'x' || *s == 'X')) { 214666161Seric c = s[1]; 214766161Seric s += 2; 214866161Seric base = 16; 214966161Seric } 215066161Seric if (base == 0) 215166161Seric base = c == '0' ? 8 : 10; 215264816Seric 215366161Seric /* 215466161Seric * Compute the cutoff value between legal numbers and illegal 215566161Seric * numbers. That is the largest legal value, divided by the 215666161Seric * base. An input number that is greater than this value, if 215766161Seric * followed by a legal input character, is too big. One that 215866161Seric * is equal to this value may be valid or not; the limit 215966161Seric * between valid and invalid numbers is then based on the last 216066161Seric * digit. For instance, if the range for longs is 216166161Seric * [-2147483648..2147483647] and the input base is 10, 216266161Seric * cutoff will be set to 214748364 and cutlim to either 216366161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 216466161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 216566161Seric * the number is too big, and we will return a range error. 216666161Seric * 216766161Seric * Set any if any `digits' consumed; make it negative to indicate 216866161Seric * overflow. 216966161Seric */ 217066161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 217166161Seric cutlim = cutoff % (unsigned long)base; 217266161Seric cutoff /= (unsigned long)base; 217366161Seric for (acc = 0, any = 0;; c = *s++) { 217466161Seric if (isdigit(c)) 217566161Seric c -= '0'; 217666161Seric else if (isalpha(c)) 217766161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 217866161Seric else 217966161Seric break; 218066161Seric if (c >= base) 218166161Seric break; 218266161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 218366161Seric any = -1; 218466161Seric else { 218566161Seric any = 1; 218666161Seric acc *= base; 218766161Seric acc += c; 218864816Seric } 218964816Seric } 219066161Seric if (any < 0) { 219166161Seric acc = neg ? LONG_MIN : LONG_MAX; 219266161Seric errno = ERANGE; 219366161Seric } else if (neg) 219466161Seric acc = -acc; 219566161Seric if (endptr != 0) 219666161Seric *endptr = (char *)(any ? s - 1 : nptr); 219766161Seric return (acc); 219864816Seric } 219964816Seric 220064816Seric #endif 220164841Seric /* 220264841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 220364841Seric ** 220467456Seric ** Solaris versions at least through 2.3 don't properly deliver a 220564841Seric ** canonical h_name field. This tries to work around it. 220664841Seric */ 220764841Seric 220864841Seric #ifdef SOLARIS 220964841Seric 221067456Seric extern int h_errno; 221167456Seric 221264841Seric struct hostent * 221364841Seric solaris_gethostbyname(name) 221465094Seric const char *name; 221564841Seric { 221664841Seric # ifdef SOLARIS_2_3 221764841Seric static struct hostent hp; 221864841Seric static char buf[1000]; 221964841Seric extern struct hostent *_switch_gethostbyname_r(); 222064841Seric 222164841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 222264841Seric # else 222365009Seric extern struct hostent *__switch_gethostbyname(); 222465009Seric 222564841Seric return __switch_gethostbyname(name); 222664841Seric # endif 222764841Seric } 222864841Seric 222964841Seric struct hostent * 223064841Seric solaris_gethostbyaddr(addr, len, type) 223165094Seric const char *addr; 223264841Seric int len; 223364841Seric int type; 223464841Seric { 223564841Seric # ifdef SOLARIS_2_3 223664841Seric static struct hostent hp; 223764841Seric static char buf[1000]; 223864841Seric extern struct hostent *_switch_gethostbyaddr_r(); 223964841Seric 224064841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 224164841Seric # else 224265009Seric extern struct hostent *__switch_gethostbyaddr(); 224365009Seric 224464841Seric return __switch_gethostbyaddr(addr, len, type); 224564841Seric # endif 224664841Seric } 224764841Seric 224864841Seric #endif 224965214Seric /* 225065214Seric ** NI_PROPVAL -- netinfo property value lookup routine 225165214Seric ** 225265214Seric ** Parameters: 225365214Seric ** directory -- the Netinfo directory name. 225465214Seric ** propname -- the Netinfo property name. 225565214Seric ** 225665214Seric ** Returns: 225765214Seric ** NULL -- if: 225865214Seric ** 1. the directory is not found 225965214Seric ** 2. the property name is not found 226065214Seric ** 3. the property contains multiple values 226165214Seric ** 4. some error occured 226265214Seric ** else -- the location of the config file. 226365214Seric ** 226465214Seric ** Notes: 226565214Seric ** Caller should free the return value of ni_proval 226665214Seric */ 226765214Seric 226865214Seric #ifdef NETINFO 226965214Seric 227065214Seric # include <netinfo/ni.h> 227165214Seric 227265214Seric # define LOCAL_NETINFO_DOMAIN "." 227365214Seric # define PARENT_NETINFO_DOMAIN ".." 227465214Seric # define MAX_NI_LEVELS 256 227565214Seric 227665214Seric char * 227765214Seric ni_propval(directory, propname) 227865214Seric char *directory; 227965214Seric char *propname; 228065214Seric { 228165820Seric char *propval = NULL; 228265214Seric int i; 228365214Seric void *ni = NULL; 228465214Seric void *lastni = NULL; 228565214Seric ni_status nis; 228665214Seric ni_id nid; 228765214Seric ni_namelist ninl; 228865214Seric 228965214Seric /* 229065214Seric ** If the passed directory and property name are found 229165214Seric ** in one of netinfo domains we need to search (starting 229265214Seric ** from the local domain moving all the way back to the 229365214Seric ** root domain) set propval to the property's value 229465214Seric ** and return it. 229565214Seric */ 229665214Seric 229765214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 229865214Seric { 229965214Seric if (i == 0) 230065214Seric { 230165214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 230265214Seric } 230365214Seric else 230465214Seric { 230565214Seric if (lastni != NULL) 230665214Seric ni_free(lastni); 230765214Seric lastni = ni; 230865214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 230965214Seric } 231065214Seric 231165214Seric /* 231265214Seric ** Don't bother if we didn't get a handle on a 231365214Seric ** proper domain. This is not necessarily an error. 231465214Seric ** We would get a positive ni_status if, for instance 231565214Seric ** we never found the directory or property and tried 231665214Seric ** to open the parent of the root domain! 231765214Seric */ 231865214Seric 231965214Seric if (nis != 0) 232065214Seric break; 232165214Seric 232265214Seric /* 232365214Seric ** Find the path to the server information. 232465214Seric */ 232565214Seric 232665214Seric if (ni_pathsearch(ni, &nid, directory) != 0) 232765214Seric continue; 232865214Seric 232965214Seric /* 233065214Seric ** Find "host" information. 233165214Seric */ 233265214Seric 233365214Seric if (ni_lookupprop(ni, &nid, propname, &ninl) != 0) 233465214Seric continue; 233565214Seric 233665214Seric /* 233765214Seric ** If there's only one name in 233865214Seric ** the list, assume we've got 233965214Seric ** what we want. 234065214Seric */ 234165214Seric 234265214Seric if (ninl.ni_namelist_len == 1) 234365214Seric { 234465214Seric propval = ni_name_dup(ninl.ni_namelist_val[0]); 234565214Seric break; 234665214Seric } 234765214Seric } 234865214Seric 234965214Seric /* 235065214Seric ** Clean up. 235165214Seric */ 235265214Seric 235365214Seric if (ni != NULL) 235465214Seric ni_free(ni); 235565214Seric if (lastni != NULL && ni != lastni) 235665214Seric ni_free(lastni); 235765214Seric 235865214Seric return propval; 235965214Seric } 236065214Seric 236165214Seric #endif /* NETINFO */ 236265504Seric /* 236365504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 236465504Seric ** 236565504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 236665504Seric ** syslog succeeds during interrupt handlers. 236765504Seric */ 236865504Seric 236965504Seric #ifdef __hpux 237065504Seric 237165504Seric # define MAXSYSLOGTRIES 100 237265504Seric # undef syslog 237365504Seric 237465504Seric # ifdef __STDC__ 237565504Seric hard_syslog(int pri, char *msg, ...) 237665504Seric # else 237765504Seric hard_syslog(pri, msg, va_alist) 237865504Seric int pri; 237965504Seric char *msg; 238065504Seric va_dcl 238165504Seric # endif 238265504Seric { 238365504Seric int i; 238465504Seric char buf[SYSLOG_BUFSIZE * 2]; 238565504Seric VA_LOCAL_DECL; 238665504Seric 238765504Seric VA_START(msg); 238865504Seric vsprintf(buf, msg, ap); 238965504Seric VA_END; 239065504Seric 239165504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 239265504Seric continue; 239365504Seric } 239465504Seric 239565504Seric #endif 2396