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*68521Seric static char sccsid[] = "@(#)conf.c 8.140 (Berkeley) 03/12/95"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1363937Seric # include "sendmail.h" 1463937Seric # include "pathnames.h" 1514881Seric # include <sys/ioctl.h> 1658082Seric # include <sys/param.h> 1765018Seric # include <netdb.h> 1836928Sbostic # include <pwd.h> 19404Seric 20294Seric /* 213309Seric ** CONF.C -- Sendmail Configuration Tables. 22294Seric ** 23294Seric ** Defines the configuration of this installation. 24294Seric ** 251388Seric ** Configuration Variables: 262897Seric ** HdrInfo -- a table describing well-known header fields. 272897Seric ** Each entry has the field name and some flags, 284147Seric ** which are described in sendmail.h. 294093Seric ** 304093Seric ** Notes: 314093Seric ** I have tried to put almost all the reasonable 324093Seric ** configuration information into the configuration 334093Seric ** file read at runtime. My intent is that anything 344093Seric ** here is a function of the version of UNIX you 354093Seric ** are running, or is really static -- for example 364093Seric ** the headers are a superset of widely used 374093Seric ** protocols. If you find yourself playing with 384093Seric ** this file too much, you may be making a mistake! 39294Seric */ 40294Seric 41294Seric 42294Seric 43294Seric 444437Seric /* 452897Seric ** Header info table 463057Seric ** Final (null) entry contains the flags used for any other field. 474147Seric ** 484147Seric ** Not all of these are actually handled specially by sendmail 494147Seric ** at this time. They are included as placeholders, to let 504147Seric ** you know that "someday" I intend to have sendmail do 514147Seric ** something with them. 522897Seric */ 532897Seric 542897Seric struct hdrinfo HdrInfo[] = 552897Seric { 568060Seric /* originator fields, most to least significant */ 5768508Seric "resent-sender", H_FROM|H_RESENT, 5868508Seric "resent-from", H_FROM|H_RESENT, 5968508Seric "resent-reply-to", H_FROM|H_RESENT, 6068508Seric "sender", H_FROM, 6168508Seric "from", H_FROM, 6268508Seric "reply-to", H_FROM, 6368508Seric "full-name", H_ACHECK, 6468508Seric "return-receipt-to", H_FROM|H_RECEIPTTO, 6568508Seric "errors-to", H_FROM|H_ERRORSTO, 6658796Seric 678060Seric /* destination fields */ 6868508Seric "to", H_RCPT, 6968508Seric "resent-to", H_RCPT|H_RESENT, 7068508Seric "cc", H_RCPT, 7168508Seric "resent-cc", H_RCPT|H_RESENT, 7268508Seric "bcc", H_RCPT|H_STRIPVAL, 7368508Seric "resent-bcc", H_RCPT|H_STRIPVAL|H_RESENT, 7468508Seric "apparently-to", H_RCPT, 7558796Seric 768060Seric /* message identification and control */ 7768508Seric "message-id", 0, 7868508Seric "resent-message-id", H_RESENT, 7968508Seric "message", H_EOH, 8068508Seric "text", H_EOH, 8158796Seric 8211417Seric /* date fields */ 8368508Seric "date", 0, 8468508Seric "resent-date", H_RESENT, 8558796Seric 868060Seric /* trace fields */ 8768508Seric "received", H_TRACE|H_FORCE, 8868508Seric "x400-received", H_TRACE|H_FORCE, 8968508Seric "via", H_TRACE|H_FORCE, 9068508Seric "mail-from", H_TRACE|H_FORCE, 918060Seric 9258796Seric /* miscellaneous fields */ 9368508Seric "comments", H_FORCE, 9468508Seric "return-path", H_FORCE|H_ACHECK, 9568508Seric "content-transfer-encoding", H_CTE, 9668508Seric "content-type", H_CTYPE, 9758796Seric 989055Seric NULL, 0, 992897Seric }; 1004166Seric 1014166Seric 1024166Seric 1034282Seric /* 1044282Seric ** Location of system files/databases/etc. 1054282Seric */ 1064282Seric 10758082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1089039Seric 1099064Seric 1109064Seric 1119039Seric /* 11258082Seric ** Privacy values 11358082Seric */ 11458082Seric 11558082Seric struct prival PrivacyValues[] = 11658082Seric { 11758082Seric "public", PRIV_PUBLIC, 11858082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11958114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 12058082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 12158082Seric "noexpn", PRIV_NOEXPN, 12258082Seric "novrfy", PRIV_NOVRFY, 12364333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12464333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12558789Seric "authwarnings", PRIV_AUTHWARNINGS, 12666783Seric "noreceipts", PRIV_NORECEIPTS, 12758082Seric "goaway", PRIV_GOAWAY, 12858789Seric NULL, 0, 12958082Seric }; 13058082Seric 13158082Seric 13258082Seric 13358082Seric /* 13424943Seric ** Miscellaneous stuff. 1359039Seric */ 1369039Seric 13724943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13824943Seric /* 13924943Seric ** SETDEFAULTS -- set default values 14024943Seric ** 14124943Seric ** Because of the way freezing is done, these must be initialized 14224943Seric ** using direct code. 14324943Seric ** 14424943Seric ** Parameters: 14558734Seric ** e -- the default envelope. 14624943Seric ** 14724943Seric ** Returns: 14824943Seric ** none. 14924943Seric ** 15024943Seric ** Side Effects: 15124943Seric ** Initializes a bunch of global variables to their 15224943Seric ** default values. 15324943Seric */ 15424943Seric 15558737Seric #define DAYS * 24 * 60 * 60 15658737Seric 15768508Seric void 15858734Seric setdefaults(e) 15958734Seric register ENVELOPE *e; 16024943Seric { 16168508Seric int i; 16268508Seric extern void inittimeouts(); 16368508Seric extern void setdefuser(); 16468508Seric extern void setupmaps(); 16568508Seric extern void setupmailers(); 16668508Seric 16757438Seric SpaceSub = ' '; /* option B */ 16857438Seric QueueLA = 8; /* option x */ 16957438Seric RefuseLA = 12; /* option X */ 17057438Seric WkRecipFact = 30000L; /* option y */ 17157438Seric WkClassFact = 1800L; /* option z */ 17257438Seric WkTimeFact = 90000L; /* option Z */ 17357438Seric QueueFactor = WkRecipFact * 20; /* option q */ 17463787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 17557438Seric /* option F */ 17657438Seric DefUid = 1; /* option u */ 17757438Seric DefGid = 1; /* option g */ 17857438Seric CheckpointInterval = 10; /* option C */ 17957438Seric MaxHopCount = 25; /* option h */ 18058734Seric e->e_sendmode = SM_FORK; /* option d */ 18158734Seric e->e_errormode = EM_PRINT; /* option e */ 18268508Seric SevenBitInput = FALSE; /* option 7 */ 18357438Seric MaxMciCache = 1; /* option k */ 18457438Seric MciCacheTimeout = 300; /* option K */ 18557438Seric LogLevel = 9; /* option L */ 18668508Seric inittimeouts(NULL); /* option r */ 18758853Seric PrivacyFlags = 0; /* option p */ 18868508Seric MimeMode = MM_CVTMIME|MM_PASS8BIT; /* option 8 */ 18968508Seric for (i = 0; i < MAXTOCLASS; i++) 19068508Seric { 19168508Seric TimeOuts.to_q_return[i] = 5 DAYS; /* option T */ 19268508Seric TimeOuts.to_q_warning[i] = 0; /* option T */ 19368508Seric } 19468508Seric ServiceSwitchFile = "/etc/service.switch"; 19540973Sbostic setdefuser(); 19653654Seric setupmaps(); 19757402Seric setupmailers(); 19824943Seric } 199294Seric 20040973Sbostic 2014326Seric /* 20240973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20340973Sbostic */ 20440973Sbostic 20568508Seric void 20640973Sbostic setdefuser() 20740973Sbostic { 20840973Sbostic struct passwd *defpwent; 20957386Seric static char defuserbuf[40]; 21040973Sbostic 21157386Seric DefUser = defuserbuf; 21240973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 21357386Seric strcpy(defuserbuf, defpwent->pw_name); 21440973Sbostic else 21557386Seric strcpy(defuserbuf, "nobody"); 21640973Sbostic } 21753654Seric /* 21856836Seric ** HOST_MAP_INIT -- initialize host class structures 21956836Seric */ 22056836Seric 22168508Seric bool host_map_init __P((MAP *map, char *args)); 22268508Seric 22356836Seric bool 22460219Seric host_map_init(map, args) 22556836Seric MAP *map; 22656836Seric char *args; 22756836Seric { 22856836Seric register char *p = args; 22956836Seric 23056836Seric for (;;) 23156836Seric { 23258050Seric while (isascii(*p) && isspace(*p)) 23356836Seric p++; 23456836Seric if (*p != '-') 23556836Seric break; 23656836Seric switch (*++p) 23756836Seric { 23856836Seric case 'a': 23956836Seric map->map_app = ++p; 24056836Seric break; 24156836Seric } 24258050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 24356836Seric p++; 24456836Seric if (*p != '\0') 24556836Seric *p++ = '\0'; 24656836Seric } 24756836Seric if (map->map_app != NULL) 24856836Seric map->map_app = newstr(map->map_app); 24956836Seric return TRUE; 25056836Seric } 25157402Seric /* 25257402Seric ** SETUPMAILERS -- initialize default mailers 25357402Seric */ 25456836Seric 25568508Seric void 25657402Seric setupmailers() 25757402Seric { 25857402Seric char buf[100]; 25968508Seric extern void makemailer(); 26057402Seric 26168508Seric strcpy(buf, "prog, P=/bin/sh, F=lsoD, A=sh -c $u"); 26257403Seric makemailer(buf); 26357403Seric 26468508Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEou, A=FILE"); 26557402Seric makemailer(buf); 26657402Seric 26757402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 26857402Seric makemailer(buf); 26957402Seric } 27056836Seric /* 27160207Seric ** SETUPMAPS -- set up map classes 27260207Seric */ 27360207Seric 27460207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 27560207Seric { \ 27660207Seric extern bool parse __P((MAP *, char *)); \ 27760207Seric extern bool open __P((MAP *, int)); \ 27860207Seric extern void close __P((MAP *)); \ 27960207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 28060207Seric extern void store __P((MAP *, char *, char *)); \ 28160207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 28260207Seric s->s_mapclass.map_cname = name; \ 28360207Seric s->s_mapclass.map_ext = ext; \ 28460207Seric s->s_mapclass.map_cflags = flags; \ 28560207Seric s->s_mapclass.map_parse = parse; \ 28660207Seric s->s_mapclass.map_open = open; \ 28760207Seric s->s_mapclass.map_close = close; \ 28860207Seric s->s_mapclass.map_lookup = lookup; \ 28960207Seric s->s_mapclass.map_store = store; \ 29060207Seric } 29160207Seric 29268508Seric void 29360207Seric setupmaps() 29460207Seric { 29560207Seric register STAB *s; 29660207Seric 29760207Seric #ifdef NEWDB 29860207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29960207Seric map_parseargs, hash_map_open, db_map_close, 30060207Seric db_map_lookup, db_map_store); 30168508Seric 30260207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 30360207Seric map_parseargs, bt_map_open, db_map_close, 30460207Seric db_map_lookup, db_map_store); 30560207Seric #endif 30660207Seric 30760207Seric #ifdef NDBM 30860207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 30960207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 31060207Seric ndbm_map_lookup, ndbm_map_store); 31160207Seric #endif 31260207Seric 31360207Seric #ifdef NIS 31460207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 31568508Seric map_parseargs, nis_map_open, null_map_close, 31668508Seric nis_map_lookup, null_map_store); 31760207Seric #endif 31860207Seric 31968508Seric #ifdef NISPLUS 32068508Seric MAPDEF("nisplus", NULL, MCF_ALIASOK, 32168508Seric map_parseargs, nisplus_map_open, null_map_close, 32268508Seric nisplus_map_lookup, null_map_store); 32368508Seric #endif 32468508Seric 32568508Seric #ifdef HESIOD 32668508Seric MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY, 32768508Seric map_parseargs, null_map_open, null_map_close, 32868508Seric hes_map_lookup, null_map_store); 32968508Seric #endif 33068508Seric 33168508Seric #ifdef NETINFO 33268508Seric MAPDEF("netinfo", NULL, MCF_ALIASOK, 33368508Seric map_parseargs, ni_map_open, null_map_close, 33468508Seric ni_map_lookup, null_map_store); 33568508Seric #endif 33668508Seric 33768508Seric #if 0 33868508Seric MAPDEF("dns", NULL, 0, 33968508Seric dns_map_init, null_map_open, null_map_close, 34068508Seric dns_map_lookup, null_map_store); 34168508Seric #endif 34268508Seric 34368508Seric #if NAMED_BIND 34468508Seric /* best MX DNS lookup */ 34568508Seric MAPDEF("bestmx", NULL, MCF_OPTFILE, 34668508Seric map_parseargs, null_map_open, null_map_close, 34768508Seric bestmx_map_lookup, null_map_store); 34868508Seric #endif 34968508Seric 35068508Seric MAPDEF("host", NULL, 0, 35168508Seric host_map_init, null_map_open, null_map_close, 35268508Seric host_map_lookup, null_map_store); 35368508Seric 35468508Seric MAPDEF("text", NULL, MCF_ALIASOK, 35568508Seric map_parseargs, text_map_open, null_map_close, 35668508Seric text_map_lookup, null_map_store); 35768508Seric 35860207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 35968508Seric map_parseargs, stab_map_open, null_map_close, 36060207Seric stab_map_lookup, stab_map_store); 36160207Seric 36260207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 36360207Seric map_parseargs, impl_map_open, impl_map_close, 36460207Seric impl_map_lookup, impl_map_store); 36560207Seric 36668508Seric /* access to system passwd file */ 36768508Seric MAPDEF("user", NULL, MCF_OPTFILE, 36868508Seric map_parseargs, user_map_open, null_map_close, 36968508Seric user_map_lookup, null_map_store); 37060207Seric 37160207Seric /* dequote map */ 37260207Seric MAPDEF("dequote", NULL, 0, 37360207Seric dequote_init, null_map_open, null_map_close, 37460207Seric dequote_map, null_map_store); 37560207Seric 376*68521Seric #ifdef USERDB 37760207Seric /* user database */ 378*68521Seric MAPDEF("userdb", ".db", 0, 379*68521Seric map_parseargs, null_map_open, null_map_close, 38060207Seric udb_map_lookup, null_map_store); 38160207Seric #endif 38268508Seric 38368508Seric /* sequenced maps */ 38468508Seric MAPDEF("sequence", NULL, MCF_ALIASOK, 38568508Seric seq_map_parse, null_map_open, null_map_close, 38668508Seric seq_map_lookup, seq_map_store); 38768508Seric 38868508Seric /* switched interface to sequenced maps */ 38968508Seric MAPDEF("switch", NULL, MCF_ALIASOK, 39068508Seric map_parseargs, switch_map_open, null_map_close, 39168508Seric seq_map_lookup, seq_map_store); 39260207Seric } 39360207Seric 39460207Seric #undef MAPDEF 39560207Seric /* 39668508Seric ** INITHOSTMAPS -- initial host-dependent maps 39768508Seric ** 39868508Seric ** This should act as an interface to any local service switch 39968508Seric ** provided by the host operating system. 40068508Seric ** 40168508Seric ** Parameters: 40268508Seric ** none 40368508Seric ** 40468508Seric ** Returns: 40568508Seric ** none 40668508Seric ** 40768508Seric ** Side Effects: 40868508Seric ** Should define maps "host" and "users" as necessary 40968508Seric ** for this OS. If they are not defined, they will get 41068508Seric ** a default value later. It should check to make sure 41168508Seric ** they are not defined first, since it's possible that 41268508Seric ** the config file has provided an override. 41368508Seric */ 41468508Seric 41568508Seric void 41668508Seric inithostmaps() 41768508Seric { 41868508Seric register int i; 41968508Seric int nmaps; 42068508Seric char *maptype[MAXMAPSTACK]; 42168508Seric short mapreturn[MAXMAPACTIONS]; 42268508Seric char buf[MAXLINE]; 42368508Seric 42468508Seric /* 42568508Seric ** Set up default hosts maps. 42668508Seric */ 42768508Seric 42868508Seric #if 0 42968508Seric nmaps = switch_map_find("hosts", maptype, mapreturn); 43068508Seric for (i = 0; i < nmaps; i++) 43168508Seric { 43268508Seric if (strcmp(maptype[i], "files") == 0 && 43368508Seric stab("hosts.files", ST_MAP, ST_FIND) == NULL) 43468508Seric { 43568508Seric strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts"); 43668508Seric makemapentry(buf); 43768508Seric } 43868508Seric #if NAMED_BIND 43968508Seric else if (strcmp(maptype[i], "dns") == 0 && 44068508Seric stab("hosts.dns", ST_MAP, ST_FIND) == NULL) 44168508Seric { 44268508Seric strcpy(buf, "hosts.dns dns A"); 44368508Seric makemapentry(buf); 44468508Seric } 44568508Seric #endif 44668508Seric #ifdef NISPLUS 44768508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 44868508Seric stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL) 44968508Seric { 45068508Seric strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir"); 45168508Seric makemapentry(buf); 45268508Seric } 45368508Seric #endif 45468508Seric #ifdef NIS 45568508Seric else if (strcmp(maptype[i], "nis") == 0 && 45668508Seric stab("hosts.nis", ST_MAP, ST_FIND) == NULL) 45768508Seric { 45868508Seric strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname"); 45968508Seric makemapentry(buf); 46068508Seric } 46168508Seric #endif 46268508Seric } 46368508Seric #endif 46468508Seric 46568508Seric /* 46668508Seric ** Make sure we have a host map. 46768508Seric */ 46868508Seric 46968508Seric if (stab("host", ST_MAP, ST_FIND) == NULL) 47068508Seric { 47168508Seric /* user didn't initialize: set up host map */ 47268508Seric strcpy(buf, "host host"); 47368508Seric #if NAMED_BIND 47468508Seric if (ConfigLevel >= 2) 47568508Seric strcat(buf, " -a."); 47668508Seric #endif 47768508Seric makemapentry(buf); 47868508Seric } 47968508Seric 48068508Seric /* 48168508Seric ** Set up default aliases maps 48268508Seric */ 48368508Seric 48468508Seric nmaps = switch_map_find("aliases", maptype, mapreturn); 48568508Seric for (i = 0; i < nmaps; i++) 48668508Seric { 48768508Seric if (strcmp(maptype[i], "files") == 0 && 48868508Seric stab("aliases.files", ST_MAP, ST_FIND) == NULL) 48968508Seric { 49068508Seric strcpy(buf, "aliases.files implicit /etc/aliases"); 49168508Seric makemapentry(buf); 49268508Seric } 49368508Seric #ifdef NISPLUS 49468508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 49568508Seric stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL) 49668508Seric { 49768508Seric strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir"); 49868508Seric makemapentry(buf); 49968508Seric } 50068508Seric #endif 50168508Seric #ifdef NIS 50268508Seric else if (strcmp(maptype[i], "nis") == 0 && 50368508Seric stab("aliases.nis", ST_MAP, ST_FIND) == NULL) 50468508Seric { 50568508Seric strcpy(buf, "aliases.nis nis -d mail.aliases"); 50668508Seric makemapentry(buf); 50768508Seric } 50868508Seric #endif 50968508Seric } 51068508Seric if (stab("aliases", ST_MAP, ST_FIND) == NULL) 51168508Seric { 51268508Seric strcpy(buf, "aliases switch aliases"); 51368508Seric makemapentry(buf); 51468508Seric } 51568508Seric strcpy(buf, "switch:aliases"); 51668508Seric setalias(buf); 51768508Seric 51868508Seric #if 0 /* "user" map class is a better choice */ 51968508Seric /* 52068508Seric ** Set up default users maps. 52168508Seric */ 52268508Seric 52368508Seric nmaps = switch_map_find("passwd", maptype, mapreturn); 52468508Seric for (i = 0; i < nmaps; i++) 52568508Seric { 52668508Seric if (strcmp(maptype[i], "files") == 0 && 52768508Seric stab("users.files", ST_MAP, ST_FIND) == NULL) 52868508Seric { 52968508Seric strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd"); 53068508Seric makemapentry(buf); 53168508Seric } 53268508Seric #ifdef NISPLUS 53368508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 53468508Seric stab("users.nisplus", ST_MAP, ST_FIND) == NULL) 53568508Seric { 53668508Seric strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir"); 53768508Seric makemapentry(buf); 53868508Seric } 53968508Seric #endif 54068508Seric #ifdef NIS 54168508Seric else if (strcmp(maptype[i], "nis") == 0 && 54268508Seric stab("users.nis", ST_MAP, ST_FIND) == NULL) 54368508Seric { 54468508Seric strcpy(buf, "users.nis nis -m -d passwd.byname"); 54568508Seric makemapentry(buf); 54668508Seric } 54768508Seric #endif 54868508Seric #ifdef HESIOD 54968508Seric else if (strcmp(maptype[i], "hesiod") == 0) && 55068508Seric stab("users.hesiod", ST_MAP, ST_FIND) == NULL) 55168508Seric { 55268508Seric strcpy(buf, "users.hesiod hesiod"); 55368508Seric makemapentry(buf); 55468508Seric } 55568508Seric #endif 55668508Seric } 55768508Seric if (stab("users", ST_MAP, ST_FIND) == NULL) 55868508Seric { 55968508Seric strcpy(buf, "users switch -m passwd"); 56068508Seric makemapentry(buf); 56168508Seric } 56268508Seric #endif 56368508Seric } 56468508Seric /* 56568508Seric ** SWITCH_MAP_FIND -- find the list of types associated with a map 56668508Seric ** 56768508Seric ** This is the system-dependent interface to the service switch. 56868508Seric ** 56968508Seric ** Parameters: 57068508Seric ** service -- the name of the service of interest. 57168508Seric ** maptype -- an out-array of strings containing the types 57268508Seric ** of access to use for this service. There can 57368508Seric ** be at most MAXMAPSTACK types for a single service. 57468508Seric ** mapreturn -- an out-array of return information bitmaps 57568508Seric ** for the map. 57668508Seric ** 57768508Seric ** Returns: 57868508Seric ** The number of map types filled in, or -1 for failure. 57968508Seric */ 58068508Seric 58168508Seric #ifdef SOLARIS 58268508Seric # include <nsswitch.h> 58368508Seric #endif 58468508Seric 58568508Seric #if defined(ultrix) || defined(__osf__) 58668508Seric # include <sys/svcinfo.h> 58768508Seric #endif 58868508Seric 58968508Seric int 59068508Seric switch_map_find(service, maptype, mapreturn) 59168508Seric char *service; 59268508Seric char *maptype[MAXMAPSTACK]; 59368508Seric short mapreturn[MAXMAPACTIONS]; 59468508Seric { 59568508Seric register FILE *fp; 59668508Seric int svcno; 59768508Seric static char buf[MAXLINE]; 59868508Seric 59968508Seric #ifdef SOLARIS 60068508Seric struct __nsw_switchconfig *nsw_conf; 60168508Seric enum __nsw_parse_err pserr; 60268508Seric struct __nsw_lookup *lk; 60368508Seric int nsw_rc; 60468508Seric static struct __nsw_lookup lkp0 = 60568508Seric { "files", {1, 0, 0, 0}, NULL, NULL }; 60668508Seric static struct __nsw_switchconfig lkp_default = 60768508Seric { 0, "sendmail", 3, &lkp0 }; 60868508Seric 60968508Seric if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL) 61068508Seric lk = lkp_default.lookups; 61168508Seric else 61268508Seric lk = nsw_conf->lookups; 61368508Seric svcno = 0; 61468508Seric while (lk != NULL) 61568508Seric { 61668508Seric maptype[svcno] = lk->service_name; 61768508Seric if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN) 61868508Seric mapreturn[MA_NOTFOUND] |= 1 << svcno; 61968508Seric if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN) 62068508Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 62168508Seric if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN) 62268508Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 62368508Seric svcno++; 62468508Seric lk = lk->next; 62568508Seric } 62668508Seric return svcno; 62768508Seric #endif 62868508Seric 62968508Seric #if defined(ultrix) || defined(__osf__) 63068508Seric struct svcinfo *svcinfo; 63168508Seric int svc; 63268508Seric 63368508Seric svcinfo = getsvc(); 63468508Seric if (svcinfo == NULL) 63568508Seric goto punt; 63668508Seric if (strcmp(service, "hosts") == 0) 63768508Seric svc = SVC_HOSTS; 63868508Seric else if (strcmp(service, "aliases") == 0) 63968508Seric svc = SVC_ALIASES; 64068508Seric else if (strcmp(service, "passwd") == 0) 64168508Seric svc = SVC_PASSWD; 64268508Seric else 64368508Seric return -1; 64468508Seric for (svcno = 0; svcno < SVC_PATHSIZE; svcno++) 64568508Seric { 64668508Seric switch (svcinfo->svcpath[svc][svcno]) 64768508Seric { 64868508Seric case SVC_LOCAL: 64968508Seric maptype[svcno] = "files"; 65068508Seric break; 65168508Seric 65268508Seric case SVC_YP: 65368508Seric maptype[svcno] = "nis"; 65468508Seric break; 65568508Seric 65668508Seric case SVC_BIND: 65768508Seric maptype[svcno] = "dns"; 65868508Seric break; 65968508Seric 66068508Seric #ifdef SVC_HESIOD 66168508Seric case SVC_HESIOD: 66268508Seric maptype[svcno] = "hesiod"; 66368508Seric break; 66468508Seric #endif 66568508Seric 66668508Seric case SVC_LAST: 66768508Seric return svcno; 66868508Seric } 66968508Seric } 67068508Seric return svcno; 67168508Seric #endif 67268508Seric 67368508Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__) 67468508Seric /* 67568508Seric ** Fall-back mechanism. 67668508Seric */ 67768508Seric 67868508Seric svcno = 0; 67968508Seric fp = fopen(ServiceSwitchFile, "r"); 68068508Seric if (fp != NULL) 68168508Seric { 68268508Seric while (fgets(buf, sizeof buf, fp) != NULL) 68368508Seric { 68468508Seric register char *p; 68568508Seric 68668508Seric p = strpbrk(buf, "#\n"); 68768508Seric if (p != NULL) 68868508Seric *p = '\0'; 68968508Seric p = strpbrk(buf, " \t"); 69068508Seric if (p != NULL) 69168508Seric *p++ = '\0'; 69268508Seric if (strcmp(buf, service) != 0) 69368508Seric continue; 69468508Seric 69568508Seric /* got the right service -- extract data */ 69668508Seric do 69768508Seric { 69868508Seric while (isspace(*p)) 69968508Seric p++; 70068508Seric if (*p == '\0') 70168508Seric break; 70268508Seric maptype[svcno++] = p; 70368508Seric p = strpbrk(p, " \t"); 70468508Seric if (p != NULL) 70568508Seric *p++ = '\0'; 70668508Seric } while (p != NULL); 70768508Seric break; 70868508Seric } 70968508Seric fclose(fp); 71068508Seric return svcno; 71168508Seric } 71268508Seric #endif 71368508Seric 71468508Seric /* if the service file doesn't work, use an absolute fallback */ 71568508Seric punt: 71668508Seric if (strcmp(service, "aliases") == 0) 71768508Seric { 71868508Seric maptype[0] = "files"; 71968508Seric return 1; 72068508Seric } 72168508Seric if (strcmp(service, "hosts") == 0) 72268508Seric { 72368508Seric # if NAMED_BIND 72468508Seric maptype[svcno++] = "dns"; 72568508Seric # else 72668508Seric # if defined(sun) && !defined(BSD) && !defined(SOLARIS) 72768508Seric /* SunOS */ 72868508Seric maptype[svcno++] = "nis"; 72968508Seric # endif 73068508Seric # endif 73168508Seric maptype[svcno++] = "files"; 73268508Seric return svcno; 73368508Seric } 73468508Seric return -1; 73568508Seric } 73668508Seric /* 7379369Seric ** USERNAME -- return the user id of the logged in user. 7389369Seric ** 7399369Seric ** Parameters: 7409369Seric ** none. 7419369Seric ** 7429369Seric ** Returns: 7439369Seric ** The login name of the logged in user. 7449369Seric ** 7459369Seric ** Side Effects: 7469369Seric ** none. 7479369Seric ** 7489369Seric ** Notes: 7499369Seric ** The return value is statically allocated. 7509369Seric */ 7519369Seric 7529369Seric char * 7539369Seric username() 7549369Seric { 75517469Seric static char *myname = NULL; 7569369Seric extern char *getlogin(); 75719904Smiriam register struct passwd *pw; 7589369Seric 75917469Seric /* cache the result */ 76017469Seric if (myname == NULL) 76117469Seric { 76217469Seric myname = getlogin(); 76317469Seric if (myname == NULL || myname[0] == '\0') 76417469Seric { 76563787Seric pw = getpwuid(RealUid); 76617469Seric if (pw != NULL) 76740993Sbostic myname = newstr(pw->pw_name); 76817469Seric } 76919904Smiriam else 77019904Smiriam { 77163787Seric uid_t uid = RealUid; 77219873Smiriam 77340993Sbostic myname = newstr(myname); 77440993Sbostic if ((pw = getpwnam(myname)) == NULL || 77558736Seric (uid != 0 && uid != pw->pw_uid)) 77619904Smiriam { 77758736Seric pw = getpwuid(uid); 77824945Seric if (pw != NULL) 77940993Sbostic myname = newstr(pw->pw_name); 78019873Smiriam } 78119873Smiriam } 78217469Seric if (myname == NULL || myname[0] == '\0') 78317469Seric { 78458151Seric syserr("554 Who are you?"); 78517469Seric myname = "postmaster"; 78617469Seric } 78717469Seric } 78817469Seric 78917469Seric return (myname); 7909369Seric } 7919369Seric /* 7924190Seric ** TTYPATH -- Get the path of the user's tty 793294Seric ** 794294Seric ** Returns the pathname of the user's tty. Returns NULL if 795294Seric ** the user is not logged in or if s/he has write permission 796294Seric ** denied. 797294Seric ** 798294Seric ** Parameters: 799294Seric ** none 800294Seric ** 801294Seric ** Returns: 802294Seric ** pathname of the user's tty. 803294Seric ** NULL if not logged in or write permission denied. 804294Seric ** 805294Seric ** Side Effects: 806294Seric ** none. 807294Seric ** 808294Seric ** WARNING: 809294Seric ** Return value is in a local buffer. 810294Seric ** 811294Seric ** Called By: 812294Seric ** savemail 813294Seric */ 814294Seric 815294Seric char * 816294Seric ttypath() 817294Seric { 818294Seric struct stat stbuf; 819294Seric register char *pathn; 820294Seric extern char *ttyname(); 8214081Seric extern char *getlogin(); 822294Seric 823294Seric /* compute the pathname of the controlling tty */ 8249369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 8259369Seric (pathn = ttyname(0)) == NULL) 826294Seric { 827294Seric errno = 0; 828294Seric return (NULL); 829294Seric } 830294Seric 831294Seric /* see if we have write permission */ 8322967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 833294Seric { 834294Seric errno = 0; 835294Seric return (NULL); 836294Seric } 837294Seric 838294Seric /* see if the user is logged in */ 839294Seric if (getlogin() == NULL) 840294Seric return (NULL); 841294Seric 842294Seric /* looks good */ 843294Seric return (pathn); 844294Seric } 8452967Seric /* 8462967Seric ** CHECKCOMPAT -- check for From and To person compatible. 8472967Seric ** 8482967Seric ** This routine can be supplied on a per-installation basis 8492967Seric ** to determine whether a person is allowed to send a message. 8502967Seric ** This allows restriction of certain types of internet 8512967Seric ** forwarding or registration of users. 8522967Seric ** 8532967Seric ** If the hosts are found to be incompatible, an error 85457454Seric ** message should be given using "usrerr" and 0 should 8552967Seric ** be returned. 8562967Seric ** 85768508Seric ** EF_NORETURN can be set in e->e_flags to suppress the return-to-sender 8584288Seric ** function; this should be done on huge messages. 8594288Seric ** 8602967Seric ** Parameters: 8612967Seric ** to -- the person being sent to. 8622967Seric ** 8632967Seric ** Returns: 86457459Seric ** an exit status 8652967Seric ** 8662967Seric ** Side Effects: 8672967Seric ** none (unless you include the usrerr stuff) 8682967Seric */ 8692967Seric 87068508Seric int 87155012Seric checkcompat(to, e) 8722967Seric register ADDRESS *to; 87355012Seric register ENVELOPE *e; 8742967Seric { 87512133Seric # ifdef lint 87612133Seric if (to == NULL) 87712133Seric to++; 87863834Seric # endif /* lint */ 87964402Seric 88064402Seric if (tTd(49, 1)) 88164402Seric printf("checkcompat(to=%s, from=%s)\n", 88264402Seric to->q_paddr, e->e_from.q_paddr); 88364402Seric 88410698Seric # ifdef EXAMPLE_CODE 88510698Seric /* this code is intended as an example only */ 8864437Seric register STAB *s; 8874437Seric 8884437Seric s = stab("arpa", ST_MAILER, ST_FIND); 88968508Seric if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 && 8909369Seric to->q_mailer == s->s_mailer) 8914437Seric { 89258151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 89368508Seric /* e->e_flags |= EF_NORETURN; to supress return copy */ 89457459Seric return (EX_UNAVAILABLE); 8954437Seric } 89656795Seric # endif /* EXAMPLE_CODE */ 89757459Seric return (EX_OK); 8982967Seric } 8999369Seric /* 90064035Seric ** SETSIGNAL -- set a signal handler 90164035Seric ** 90264035Seric ** This is essentially old BSD "signal(3)". 90364035Seric */ 90464035Seric 90564561Seric sigfunc_t 90664035Seric setsignal(sig, handler) 90764035Seric int sig; 90864561Seric sigfunc_t handler; 90964035Seric { 91064380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 91164035Seric return signal(sig, handler); 91264035Seric #else 91364035Seric struct sigaction n, o; 91464035Seric 91564035Seric bzero(&n, sizeof n); 91664035Seric n.sa_handler = handler; 91768508Seric # ifdef SA_RESTART 91868508Seric n.sa_flags = SA_RESTART; 91968508Seric # endif 92064035Seric if (sigaction(sig, &n, &o) < 0) 92164035Seric return SIG_ERR; 92264035Seric return o.sa_handler; 92364035Seric #endif 92464035Seric } 92564035Seric /* 9269369Seric ** HOLDSIGS -- arrange to hold all signals 9279369Seric ** 9289369Seric ** Parameters: 9299369Seric ** none. 9309369Seric ** 9319369Seric ** Returns: 9329369Seric ** none. 9339369Seric ** 9349369Seric ** Side Effects: 9359369Seric ** Arranges that signals are held. 9369369Seric */ 9379369Seric 93868508Seric void 9399369Seric holdsigs() 9409369Seric { 9419369Seric } 9429369Seric /* 9439369Seric ** RLSESIGS -- arrange to release all signals 9449369Seric ** 9459369Seric ** This undoes the effect of holdsigs. 9469369Seric ** 9479369Seric ** Parameters: 9489369Seric ** none. 9499369Seric ** 9509369Seric ** Returns: 9519369Seric ** none. 9529369Seric ** 9539369Seric ** Side Effects: 9549369Seric ** Arranges that signals are released. 9559369Seric */ 9569369Seric 95768508Seric void 9589369Seric rlsesigs() 9599369Seric { 9609369Seric } 96114872Seric /* 96264705Seric ** INIT_MD -- do machine dependent initializations 96364705Seric ** 96464705Seric ** Systems that have global modes that should be set should do 96564705Seric ** them here rather than in main. 96664705Seric */ 96764705Seric 96864705Seric #ifdef _AUX_SOURCE 96964705Seric # include <compat.h> 97064705Seric #endif 97164705Seric 97268508Seric void 97364825Seric init_md(argc, argv) 97464825Seric int argc; 97564825Seric char **argv; 97664705Seric { 97764705Seric #ifdef _AUX_SOURCE 97864705Seric setcompat(getcompat() | COMPAT_BSDPROT); 97964705Seric #endif 98068508Seric 98168508Seric #ifdef VENDOR_DEFAULT 98268508Seric VendorCode = VENDOR_DEFAULT; 98368508Seric #else 98468508Seric VendorCode = VENDOR_BERKELEY; 98568508Seric #endif 98664705Seric } 98764705Seric /* 98868508Seric ** INIT_VENDOR_MACROS -- vendor-dependent macro initializations 98968508Seric ** 99068508Seric ** Called once, on startup. 99168508Seric ** 99268508Seric ** Parameters: 99368508Seric ** e -- the global envelope. 99468508Seric ** 99568508Seric ** Returns: 99668508Seric ** none. 99768508Seric ** 99868508Seric ** Side Effects: 99968508Seric ** vendor-dependent. 100068508Seric */ 100168508Seric 100268508Seric void 100368508Seric init_vendor_macros(e) 100468508Seric register ENVELOPE *e; 100568508Seric { 100668508Seric } 100768508Seric /* 100814872Seric ** GETLA -- get the current load average 100914872Seric ** 101014881Seric ** This code stolen from la.c. 101114881Seric ** 101214872Seric ** Parameters: 101314872Seric ** none. 101414872Seric ** 101514872Seric ** Returns: 101614872Seric ** The current load average as an integer. 101714872Seric ** 101814872Seric ** Side Effects: 101914872Seric ** none. 102014872Seric */ 102114872Seric 102251920Seric /* try to guess what style of load average we have */ 102351920Seric #define LA_ZERO 1 /* always return load average as zero */ 102464360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 102551920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 102651920Seric #define LA_SUBR 4 /* call getloadavg */ 102764295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 102864360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 102966301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 103014872Seric 103163962Seric /* do guesses based on general OS type */ 103251920Seric #ifndef LA_TYPE 103363962Seric # define LA_TYPE LA_ZERO 103451920Seric #endif 103551920Seric 103664360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 103751920Seric 103814872Seric #include <nlist.h> 103914872Seric 104068508Seric #ifdef IRIX64 104168508Seric # define nlist nlist64 104268508Seric #endif 104368508Seric 104451920Seric #ifndef LA_AVENRUN 104563962Seric # ifdef SYSTEM5 104663962Seric # define LA_AVENRUN "avenrun" 104763962Seric # else 104863962Seric # define LA_AVENRUN "_avenrun" 104963962Seric # endif 105051920Seric #endif 105151920Seric 105251920Seric /* _PATH_UNIX should be defined in <paths.h> */ 105351920Seric #ifndef _PATH_UNIX 105463962Seric # if defined(SYSTEM5) 105563962Seric # define _PATH_UNIX "/unix" 105663962Seric # else 105763962Seric # define _PATH_UNIX "/vmunix" 105863962Seric # endif 105951920Seric #endif 106051920Seric 106114872Seric struct nlist Nl[] = 106214872Seric { 106351920Seric { LA_AVENRUN }, 106414872Seric #define X_AVENRUN 0 106514872Seric { 0 }, 106614872Seric }; 106714872Seric 106860583Seric #ifndef FSHIFT 106960583Seric # if defined(unixpc) 107060583Seric # define FSHIFT 5 107160583Seric # endif 107257736Seric 107366763Seric # if defined(__alpha) || defined(IRIX) 107460583Seric # define FSHIFT 10 107560583Seric # endif 107668508Seric 107768508Seric # if defined(_AIX3) 107868508Seric # define FSHIFT 16 107968508Seric # endif 108066788Seric #endif 108157977Seric 108266788Seric #ifndef FSHIFT 108366788Seric # define FSHIFT 8 108457736Seric #endif 108560583Seric 108666788Seric #ifndef FSCALE 108766788Seric # define FSCALE (1 << FSHIFT) 108851920Seric #endif 108940930Srick 109014872Seric getla() 109114872Seric { 109214872Seric static int kmem = -1; 109351920Seric #if LA_TYPE == LA_INT 109424943Seric long avenrun[3]; 109551920Seric #else 109664360Seric # if LA_TYPE == LA_SHORT 109764360Seric short avenrun[3]; 109864360Seric # else 109951920Seric double avenrun[3]; 110064360Seric # endif 110151920Seric #endif 110225615Seric extern off_t lseek(); 110357736Seric extern int errno; 110414872Seric 110514872Seric if (kmem < 0) 110614872Seric { 110724945Seric kmem = open("/dev/kmem", 0, 0); 110814872Seric if (kmem < 0) 110957736Seric { 111057736Seric if (tTd(3, 1)) 111157736Seric printf("getla: open(/dev/kmem): %s\n", 111257736Seric errstring(errno)); 111314872Seric return (-1); 111457736Seric } 111551920Seric (void) fcntl(kmem, F_SETFD, 1); 111668508Seric #ifdef _AIX3 111768508Seric if (knlist(Nl, 1, sizeof Nl[0]) < 0) 111868508Seric #else 111957736Seric if (nlist(_PATH_UNIX, Nl) < 0) 112068508Seric #endif 112157736Seric { 112257736Seric if (tTd(3, 1)) 112357736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 112457736Seric errstring(errno)); 112514872Seric return (-1); 112657736Seric } 112759253Seric if (Nl[X_AVENRUN].n_value == 0) 112859253Seric { 112959253Seric if (tTd(3, 1)) 113059253Seric printf("getla: nlist(%s, %s) ==> 0\n", 113159253Seric _PATH_UNIX, LA_AVENRUN); 113259253Seric return (-1); 113359253Seric } 113468508Seric #ifdef NAMELISTMASK 113568508Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 113666763Seric #endif 113714872Seric } 113857736Seric if (tTd(3, 20)) 113957736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 114068508Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 || 114123118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 114219967Seric { 114319967Seric /* thank you Ian */ 114457736Seric if (tTd(3, 1)) 114557736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 114619967Seric return (-1); 114719967Seric } 114864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 114957736Seric if (tTd(3, 5)) 115057736Seric { 115157736Seric printf("getla: avenrun = %d", avenrun[0]); 115257736Seric if (tTd(3, 15)) 115357736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 115457736Seric printf("\n"); 115557736Seric } 115657736Seric if (tTd(3, 1)) 115757736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 115824943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 115951920Seric #else 116057736Seric if (tTd(3, 5)) 116157736Seric { 116257736Seric printf("getla: avenrun = %g", avenrun[0]); 116357736Seric if (tTd(3, 15)) 116457736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 116557736Seric printf("\n"); 116657736Seric } 116757736Seric if (tTd(3, 1)) 116857736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 116951920Seric return ((int) (avenrun[0] + 0.5)); 117051920Seric #endif 117114872Seric } 117214872Seric 117351773Seric #else 117451920Seric #if LA_TYPE == LA_SUBR 117551773Seric 117664718Seric #ifdef DGUX 117764718Seric 117864718Seric #include <sys/dg_sys_info.h> 117964718Seric 118068508Seric int 118168508Seric getla() 118264718Seric { 118364718Seric struct dg_sys_info_load_info load_info; 118464718Seric 118564718Seric dg_sys_info((long *)&load_info, 118664718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 118764718Seric 118868508Seric if (tTd(3, 1)) 118968508Seric printf("getla: %d\n", (int) (load_info.one_minute + 0.5)); 119068508Seric 119164718Seric return((int) (load_info.one_minute + 0.5)); 119264718Seric } 119364718Seric 119464718Seric #else 119568508Seric # ifdef __hpux 119664718Seric 119768508Seric # include <sys/param.h> 119868508Seric # include <sys/pstat.h> 119968508Seric 120068508Seric int 120151773Seric getla() 120251773Seric { 120368508Seric struct pst_dynamic pstd; 120468508Seric 120568508Seric if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic), 120668508Seric (size_t) 1 ,0) == -1) 120768508Seric return 0; 120868508Seric 120968508Seric if (tTd(3, 1)) 121068508Seric printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5)); 121168508Seric 121268508Seric return (int) (pstd.psd_avg_1_min + 0.5); 121368508Seric } 121468508Seric 121568508Seric # else 121668508Seric 121768508Seric int 121868508Seric getla() 121968508Seric { 122051920Seric double avenrun[3]; 122151920Seric 122251920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 122357736Seric { 122457736Seric if (tTd(3, 1)) 122557736Seric perror("getla: getloadavg failed:"); 122651920Seric return (-1); 122757736Seric } 122857736Seric if (tTd(3, 1)) 122957736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 123051920Seric return ((int) (avenrun[0] + 0.5)); 123151773Seric } 123251773Seric 123368508Seric # endif /* __hpux */ 123464718Seric #endif /* DGUX */ 123551773Seric #else 123664295Seric #if LA_TYPE == LA_MACH 123751773Seric 123864295Seric /* 123965173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 124064295Seric */ 124164295Seric 124265173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 124365173Seric # include <mach/mach.h> 124465173Seric #else 124565173Seric # include <mach.h> 124665173Seric #endif 124764295Seric 124851773Seric getla() 124951773Seric { 125064295Seric processor_set_t default_set; 125164295Seric kern_return_t error; 125264295Seric unsigned int info_count; 125364295Seric struct processor_set_basic_info info; 125464295Seric host_t host; 125564295Seric 125664295Seric error = processor_set_default(host_self(), &default_set); 125764295Seric if (error != KERN_SUCCESS) 125864295Seric return -1; 125964295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 126064295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 126164295Seric &host, (processor_set_info_t)&info, 126264295Seric &info_count) != KERN_SUCCESS) 126364295Seric { 126464295Seric return -1; 126564295Seric } 126664295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 126764295Seric } 126864295Seric 126964295Seric 127064295Seric #else 127166313Seric #if LA_TYPE == LA_PROCSTR 127264295Seric 127366301Seric /* 127466301Seric ** Read /proc/loadavg for the load average. This is assumed to be 127566301Seric ** in a format like "0.15 0.12 0.06". 127666301Seric ** 127766301Seric ** Initially intended for Linux. This has been in the kernel 127866301Seric ** since at least 0.99.15. 127966301Seric */ 128066301Seric 128166301Seric # ifndef _PATH_LOADAVG 128266301Seric # define _PATH_LOADAVG "/proc/loadavg" 128366301Seric # endif 128466301Seric 128566301Seric int 128664295Seric getla() 128764295Seric { 128866319Seric double avenrun; 128966301Seric register int result; 129066319Seric FILE *fp; 129166301Seric 129266319Seric fp = fopen(_PATH_LOADAVG, "r"); 129366319Seric if (fp == NULL) 129466301Seric { 129566319Seric if (tTd(3, 1)) 129666319Seric printf("getla: fopen(%s): %s\n", 129766319Seric _PATH_LOADAVG, errstring(errno)); 129866319Seric return -1; 129966301Seric } 130066301Seric result = fscanf(fp, "%lf", &avenrun); 130166319Seric fclose(fp); 130266301Seric if (result != 1) 130366301Seric { 130466310Seric if (tTd(3, 1)) 130566301Seric printf("getla: fscanf() = %d: %s\n", 130666301Seric result, errstring(errno)); 130766301Seric return -1; 130866301Seric } 130966301Seric 131057736Seric if (tTd(3, 1)) 131166301Seric printf("getla(): %.2f\n", avenrun); 131266301Seric 131366301Seric return ((int) (avenrun + 0.5)); 131466301Seric } 131566301Seric 131666301Seric #else 131766301Seric 131866301Seric getla() 131966301Seric { 132066301Seric if (tTd(3, 1)) 132157736Seric printf("getla: ZERO\n"); 132251920Seric return (0); 132351773Seric } 132451773Seric 132551773Seric #endif 132651773Seric #endif 132764295Seric #endif 132866301Seric #endif 132966045Seric 133066045Seric 133166045Seric /* 133266045Seric * Copyright 1989 Massachusetts Institute of Technology 133366045Seric * 133466045Seric * Permission to use, copy, modify, distribute, and sell this software and its 133566045Seric * documentation for any purpose is hereby granted without fee, provided that 133666045Seric * the above copyright notice appear in all copies and that both that 133766045Seric * copyright notice and this permission notice appear in supporting 133866045Seric * documentation, and that the name of M.I.T. not be used in advertising or 133966045Seric * publicity pertaining to distribution of the software without specific, 134066045Seric * written prior permission. M.I.T. makes no representations about the 134166045Seric * suitability of this software for any purpose. It is provided "as is" 134266045Seric * without express or implied warranty. 134366045Seric * 134466045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 134566045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 134666045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 134766045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 134866045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 134966045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 135066045Seric * 135166045Seric * Authors: Many and varied... 135266045Seric */ 135366045Seric 135466045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 135566045Seric #ifndef lint 135666045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 135766045Seric #endif /* !lint */ 135866045Seric 135966179Seric #ifdef apollo 136066045Seric # undef volatile 136166045Seric # include <apollo/base.h> 136266045Seric 136366045Seric /* ARGSUSED */ 136466045Seric int getloadavg( call_data ) 136566045Seric caddr_t call_data; /* pointer to (double) return value */ 136666045Seric { 136766045Seric double *avenrun = (double *) call_data; 136866045Seric int i; 136966045Seric status_$t st; 137066045Seric long loadav[3]; 137166045Seric proc1_$get_loadav(loadav, &st); 137266045Seric *avenrun = loadav[0] / (double) (1 << 16); 137366045Seric return(0); 137466045Seric } 137566045Seric # endif /* apollo */ 137624943Seric /* 137724943Seric ** SHOULDQUEUE -- should this message be queued or sent? 137824943Seric ** 137924943Seric ** Compares the message cost to the load average to decide. 138024943Seric ** 138124943Seric ** Parameters: 138224943Seric ** pri -- the priority of the message in question. 138357438Seric ** ctime -- the message creation time. 138424943Seric ** 138524943Seric ** Returns: 138624943Seric ** TRUE -- if this message should be queued up for the 138724943Seric ** time being. 138824943Seric ** FALSE -- if the load is low enough to send this message. 138924943Seric ** 139024943Seric ** Side Effects: 139124943Seric ** none. 139224943Seric */ 139324943Seric 139424943Seric bool 139557438Seric shouldqueue(pri, ctime) 139624943Seric long pri; 139757438Seric time_t ctime; 139824943Seric { 139968508Seric bool rval; 140068508Seric 140168508Seric if (tTd(3, 30)) 140268508Seric printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri); 140351920Seric if (CurrentLA < QueueLA) 140468508Seric { 140568508Seric if (tTd(3, 30)) 140668508Seric printf("FALSE (CurrentLA < QueueLA)\n"); 140724943Seric return (FALSE); 140868508Seric } 140958132Seric if (CurrentLA >= RefuseLA) 141068508Seric { 141168508Seric if (tTd(3, 30)) 141268508Seric printf("TRUE (CurrentLA >= RefuseLA)\n"); 141358132Seric return (TRUE); 141468508Seric } 141568508Seric rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1)); 141668508Seric if (tTd(3, 30)) 141768508Seric printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE"); 141868508Seric return rval; 141924943Seric } 142024943Seric /* 142153037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 142253037Seric ** 142353037Seric ** Parameters: 142453037Seric ** none. 142553037Seric ** 142653037Seric ** Returns: 142753037Seric ** TRUE if incoming SMTP connections should be refused 142853037Seric ** (for now). 142953037Seric ** FALSE if we should accept new work. 143053037Seric ** 143153037Seric ** Side Effects: 143253037Seric ** none. 143353037Seric */ 143453037Seric 143553037Seric bool 143653037Seric refuseconnections() 143753037Seric { 143868508Seric extern bool enoughspace(); 143968508Seric 144059156Seric #ifdef XLA 144159156Seric if (!xla_smtp_ok()) 144259156Seric return TRUE; 144359156Seric #endif 144459156Seric 144553037Seric /* this is probably too simplistic */ 144668508Seric return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1); 144753037Seric } 144853037Seric /* 144924943Seric ** SETPROCTITLE -- set process title for ps 145024943Seric ** 145124943Seric ** Parameters: 145258674Seric ** fmt -- a printf style format string. 145358674Seric ** a, b, c -- possible parameters to fmt. 145424943Seric ** 145524943Seric ** Returns: 145624943Seric ** none. 145724943Seric ** 145824943Seric ** Side Effects: 145924943Seric ** Clobbers argv of our main procedure so ps(1) will 146024943Seric ** display the title. 146124943Seric */ 146224943Seric 146368508Seric #define SPT_NONE 0 /* don't use it at all */ 146468508Seric #define SPT_REUSEARGV 1 /* cover argv with title information */ 146568508Seric #define SPT_BUILTIN 2 /* use libc builtin */ 146668508Seric #define SPT_PSTAT 3 /* use pstat(PSTAT_SETCMD, ...) */ 146768508Seric #define SPT_PSSTRINGS 4 /* use PS_STRINGS->... */ 146868508Seric #define SPT_WRITEUDOT 5 /* write u. area in kmem */ 146968508Seric 147068508Seric #ifndef SPT_TYPE 147168508Seric # define SPT_TYPE SPT_REUSEARGV 147268508Seric #endif 147368508Seric 147468508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN 147568508Seric 147668508Seric # if SPT_TYPE == SPT_PSTAT 147758689Seric # include <sys/pstat.h> 147858689Seric # endif 147968508Seric # if SPT_TYPE == SPT_PSSTRINGS 148059732Seric # include <machine/vmparam.h> 148159732Seric # include <sys/exec.h> 148268508Seric # ifndef PS_STRINGS /* hmmmm.... apparently not available after all */ 148368508Seric # undef SPT_TYPE 148468508Seric # define SPT_TYPE SPT_REUSEARGV 148564247Seric # endif 148659732Seric # endif 148768508Seric 148868508Seric # if SPT_TYPE == SPT_PSSTRINGS 148968508Seric # define SETPROC_STATIC static 149068508Seric # else 149159732Seric # define SETPROC_STATIC 149259732Seric # endif 149367626Seric 149468508Seric # ifndef SPT_PADCHAR 149568508Seric # define SPT_PADCHAR ' ' 149668508Seric # endif 149758689Seric 149868508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */ 149964843Seric 150068508Seric #if SPT_TYPE != SPT_BUILTIN 150168508Seric 150224943Seric /*VARARGS1*/ 150368508Seric void 150468508Seric # ifdef __STDC__ 150557642Seric setproctitle(char *fmt, ...) 150668508Seric # else 150757642Seric setproctitle(fmt, va_alist) 150824943Seric char *fmt; 150957642Seric va_dcl 151068508Seric # endif 151124943Seric { 151268508Seric # if SPT_TYPE != SPT_NONE 151324943Seric register char *p; 151425049Seric register int i; 151559732Seric SETPROC_STATIC char buf[MAXLINE]; 151656852Seric VA_LOCAL_DECL 151768508Seric # if SPT_TYPE == SPT_PSTAT 151858689Seric union pstun pst; 151958689Seric # endif 152024943Seric extern char **Argv; 152124943Seric extern char *LastArgv; 152224943Seric 152358674Seric p = buf; 152424943Seric 152558674Seric /* print sendmail: heading for grep */ 152658674Seric (void) strcpy(p, "sendmail: "); 152758674Seric p += strlen(p); 152824943Seric 152958674Seric /* print the argument string */ 153058674Seric VA_START(fmt); 153158674Seric (void) vsprintf(p, fmt, ap); 153256852Seric VA_END; 153354996Seric 153458674Seric i = strlen(buf); 153558689Seric 153668508Seric # if SPT_TYPE == SPT_PSTAT 153758689Seric pst.pst_command = buf; 153858689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 153958689Seric # else 154068508Seric # if SPT_TYPE == SPT_PSSTRINGS 154159732Seric PS_STRINGS->ps_nargvstr = 1; 154259732Seric PS_STRINGS->ps_argvstr = buf; 154359732Seric # else 154454996Seric if (i > LastArgv - Argv[0] - 2) 154525049Seric { 154654996Seric i = LastArgv - Argv[0] - 2; 154758674Seric buf[i] = '\0'; 154825049Seric } 154958674Seric (void) strcpy(Argv[0], buf); 155054997Seric p = &Argv[0][i]; 155124943Seric while (p < LastArgv) 155268508Seric *p++ = SPT_PADCHAR; 155368508Seric Argv[1] = NULL; 155468508Seric # endif /* SPT_TYPE == SPT_PSSTRINGS */ 155568508Seric # endif /* SPT_TYPE == SPT_PSTAT */ 155668508Seric # endif /* SPT_TYPE != SPT_NONE */ 155724943Seric } 155865982Seric 155968508Seric #endif /* SPT_TYPE != SPT_BUILTIN */ 156025698Seric /* 156125698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 156225698Seric ** 156325698Seric ** Parameters: 156468508Seric ** sig -- the signal that got us here (unused). 156525698Seric ** 156625698Seric ** Returns: 156725698Seric ** none. 156825698Seric ** 156925698Seric ** Side Effects: 157025698Seric ** Picks up extant zombies. 157125698Seric */ 157225698Seric 157346928Sbostic void 157468508Seric reapchild(sig) 157568508Seric int sig; 157625698Seric { 157764738Seric int olderrno = errno; 157863966Seric # ifdef HASWAITPID 157960219Seric auto int status; 158060560Seric int count; 158160560Seric int pid; 158260219Seric 158360560Seric count = 0; 158460560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 158560560Seric { 158660560Seric if (count++ > 1000) 158760560Seric { 158866748Seric #ifdef LOG 158960560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 159060560Seric pid, status); 159166748Seric #endif 159260560Seric break; 159360560Seric } 159460560Seric } 159560219Seric # else 159625698Seric # ifdef WNOHANG 159725698Seric union wait status; 159825698Seric 159963753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 160025698Seric continue; 160156795Seric # else /* WNOHANG */ 160225698Seric auto int status; 160325698Seric 160460219Seric while (wait(&status) > 0) 160525698Seric continue; 160656795Seric # endif /* WNOHANG */ 160760219Seric # endif 160864035Seric # ifdef SYS5SIGNALS 160964035Seric (void) setsignal(SIGCHLD, reapchild); 161058061Seric # endif 161164738Seric errno = olderrno; 161225698Seric } 161355418Seric /* 161455418Seric ** UNSETENV -- remove a variable from the environment 161555418Seric ** 161655418Seric ** Not needed on newer systems. 161755418Seric ** 161855418Seric ** Parameters: 161955418Seric ** name -- the string name of the environment variable to be 162055418Seric ** deleted from the current environment. 162155418Seric ** 162255418Seric ** Returns: 162355418Seric ** none. 162455418Seric ** 162555418Seric ** Globals: 162655418Seric ** environ -- a pointer to the current environment. 162755418Seric ** 162855418Seric ** Side Effects: 162955418Seric ** Modifies environ. 163055418Seric */ 163155418Seric 163263962Seric #ifndef HASUNSETENV 163355418Seric 163455418Seric void 163555418Seric unsetenv(name) 163655418Seric char *name; 163755418Seric { 163855418Seric extern char **environ; 163955418Seric register char **pp; 164055418Seric int len = strlen(name); 164155418Seric 164255418Seric for (pp = environ; *pp != NULL; pp++) 164355418Seric { 164455418Seric if (strncmp(name, *pp, len) == 0 && 164555418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 164655418Seric break; 164755418Seric } 164855418Seric 164955418Seric for (; *pp != NULL; pp++) 165055418Seric *pp = pp[1]; 165155418Seric } 165255418Seric 165363962Seric #endif 165456215Seric /* 165556215Seric ** GETDTABLESIZE -- return number of file descriptors 165656215Seric ** 165756215Seric ** Only on non-BSD systems 165856215Seric ** 165956215Seric ** Parameters: 166056215Seric ** none 166156215Seric ** 166256215Seric ** Returns: 166356215Seric ** size of file descriptor table 166456215Seric ** 166556215Seric ** Side Effects: 166656215Seric ** none 166756215Seric */ 166856215Seric 166963787Seric #ifdef SOLARIS 167063787Seric # include <sys/resource.h> 167163787Seric #endif 167256215Seric 167356215Seric int 167463787Seric getdtsize() 167556215Seric { 167663787Seric #ifdef RLIMIT_NOFILE 167763787Seric struct rlimit rl; 167863787Seric 167963787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 168063787Seric return rl.rlim_cur; 168163787Seric #endif 168263787Seric 168364031Seric # ifdef HASGETDTABLESIZE 168464031Seric return getdtablesize(); 168564031Seric # else 168664031Seric # ifdef _SC_OPEN_MAX 168758689Seric return sysconf(_SC_OPEN_MAX); 168863787Seric # else 168956215Seric return NOFILE; 169063787Seric # endif 169158689Seric # endif 169256215Seric } 169357631Seric /* 169457631Seric ** UNAME -- get the UUCP name of this system. 169557631Seric */ 169657631Seric 169757943Seric #ifndef HASUNAME 169857631Seric 169957631Seric int 170057631Seric uname(name) 170157631Seric struct utsname *name; 170257631Seric { 170357631Seric FILE *file; 170457631Seric char *n; 170557631Seric 170657631Seric name->nodename[0] = '\0'; 170757631Seric 170857661Seric /* try /etc/whoami -- one line with the node name */ 170957631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 171057631Seric { 171157661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 171257631Seric (void) fclose(file); 171357661Seric n = strchr(name->nodename, '\n'); 171457631Seric if (n != NULL) 171557631Seric *n = '\0'; 171657631Seric if (name->nodename[0] != '\0') 171757631Seric return (0); 171857631Seric } 171957631Seric 172057661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 172157631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 172257631Seric { 172357631Seric char buf[MAXLINE]; 172457631Seric 172557631Seric while (fgets(buf, MAXLINE, file) != NULL) 172657631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 172757631Seric NODE_LENGTH, name->nodename) > 0) 172857631Seric break; 172957631Seric (void) fclose(file); 173057631Seric if (name->nodename[0] != '\0') 173157631Seric return (0); 173257631Seric } 173357631Seric 173457631Seric #ifdef TRUST_POPEN 173557631Seric /* 173657631Seric ** Popen is known to have security holes. 173757631Seric */ 173857631Seric 173957661Seric /* try uuname -l to return local name */ 174057631Seric if ((file = popen("uuname -l", "r")) != NULL) 174157631Seric { 174257661Seric (void) fgets(name, NODE_LENGTH + 1, file); 174357631Seric (void) pclose(file); 174457661Seric n = strchr(name, '\n'); 174557631Seric if (n != NULL) 174657631Seric *n = '\0'; 174757661Seric if (name->nodename[0] != '\0') 174857631Seric return (0); 174957631Seric } 175057631Seric #endif 175157631Seric 175257631Seric return (-1); 175357631Seric } 175457943Seric #endif /* HASUNAME */ 175558068Seric /* 175658068Seric ** INITGROUPS -- initialize groups 175758068Seric ** 175858068Seric ** Stub implementation for System V style systems 175958068Seric */ 176058068Seric 176158068Seric #ifndef HASINITGROUPS 176258068Seric 176358068Seric initgroups(name, basegid) 176458068Seric char *name; 176558068Seric int basegid; 176658068Seric { 176758068Seric return 0; 176858068Seric } 176958068Seric 177058068Seric #endif 177158082Seric /* 177259289Seric ** SETSID -- set session id (for non-POSIX systems) 177359289Seric */ 177459289Seric 177559289Seric #ifndef HASSETSID 177659289Seric 177763753Seric pid_t 177863753Seric setsid __P ((void)) 177959289Seric { 178064155Seric #ifdef TIOCNOTTY 178164155Seric int fd; 178264155Seric 178366743Seric fd = open("/dev/tty", O_RDWR, 0); 178464155Seric if (fd >= 0) 178564155Seric { 178664155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 178764155Seric (void) close(fd); 178864155Seric } 178964155Seric #endif /* TIOCNOTTY */ 179064705Seric # ifdef SYS5SETPGRP 179163753Seric return setpgrp(); 179263753Seric # else 179364155Seric return setpgid(0, getpid()); 179459289Seric # endif 179559289Seric } 179659289Seric 179759289Seric #endif 179859289Seric /* 179966748Seric ** FSYNC -- dummy fsync 180066748Seric */ 180166748Seric 180266748Seric #ifdef NEEDFSYNC 180366748Seric 180466748Seric fsync(fd) 180566748Seric int fd; 180666748Seric { 180766748Seric # ifdef O_SYNC 180866748Seric return fcntl(fd, F_SETFL, O_SYNC); 180966748Seric # else 181066748Seric /* nothing we can do */ 181166748Seric return 0; 181266748Seric # endif 181366748Seric } 181466748Seric 181566748Seric #endif 181666748Seric /* 181764718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 181864718Seric ** 181964718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 182068508Seric ** instead of a long. This patches things. Only needed on versions 182168508Seric ** prior to 5.4.3. 182264718Seric */ 182364718Seric 182468508Seric #ifdef DGUX_5_4_2 182564718Seric 182664718Seric #undef inet_addr 182764718Seric 182864718Seric long 182964718Seric dgux_inet_addr(host) 183064718Seric char *host; 183164718Seric { 183264718Seric struct in_addr haddr; 183364718Seric 183464718Seric haddr = inet_addr(host); 183564718Seric return haddr.s_addr; 183664718Seric } 183764718Seric 183864718Seric #endif 183964718Seric /* 184063969Seric ** GETOPT -- for old systems or systems with bogus implementations 184163969Seric */ 184263969Seric 184363969Seric #ifdef NEEDGETOPT 184463969Seric 184563969Seric /* 184663969Seric * Copyright (c) 1985 Regents of the University of California. 184763969Seric * All rights reserved. The Berkeley software License Agreement 184863969Seric * specifies the terms and conditions for redistribution. 184963969Seric */ 185063969Seric 185163969Seric 185263969Seric /* 185363969Seric ** this version hacked to add `atend' flag to allow state machine 185463969Seric ** to reset if invoked by the program to scan args for a 2nd time 185563969Seric */ 185663969Seric 185763969Seric #if defined(LIBC_SCCS) && !defined(lint) 185863969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 185964072Seric #endif /* LIBC_SCCS and not lint */ 186063969Seric 186163969Seric #include <stdio.h> 186263969Seric 186363969Seric /* 186463969Seric * get option letter from argument vector 186563969Seric */ 186665352Seric #ifdef _CONVEX_SOURCE 186768508Seric extern int optind, opterr, optopt; 186868508Seric extern char *optarg; 186965352Seric #else 187065352Seric int opterr = 1; /* if error message should be printed */ 187165352Seric int optind = 1; /* index into parent argv vector */ 187268470Seric int optopt = 0; /* character checked for validity */ 187368470Seric char *optarg = NULL; /* argument associated with option */ 187468508Seric #endif 187563969Seric 187663969Seric #define BADCH (int)'?' 187763969Seric #define EMSG "" 187863969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 187963969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 188063969Seric 188163969Seric getopt(nargc,nargv,ostr) 188265160Seric int nargc; 188365160Seric char *const *nargv; 188465160Seric const char *ostr; 188563969Seric { 188663969Seric static char *place = EMSG; /* option letter processing */ 188763969Seric static char atend = 0; 188863969Seric register char *oli; /* option letter list index */ 188963969Seric 189063969Seric if (atend) { 189163969Seric atend = 0; 189263969Seric place = EMSG; 189363969Seric } 189463969Seric if(!*place) { /* update scanning pointer */ 189564136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 189663969Seric atend++; 189763969Seric return(EOF); 189863969Seric } 189963969Seric if (*place == '-') { /* found "--" */ 190063969Seric ++optind; 190163969Seric atend++; 190263969Seric return(EOF); 190363969Seric } 190463969Seric } /* option letter okay? */ 190564125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 190664136Seric if (!*place) ++optind; 190763969Seric tell(": illegal option -- "); 190863969Seric } 190963969Seric if (*++oli != ':') { /* don't need argument */ 191063969Seric optarg = NULL; 191163969Seric if (!*place) ++optind; 191263969Seric } 191363969Seric else { /* need an argument */ 191463969Seric if (*place) optarg = place; /* no white space */ 191563969Seric else if (nargc <= ++optind) { /* no arg */ 191663969Seric place = EMSG; 191763969Seric tell(": option requires an argument -- "); 191863969Seric } 191963969Seric else optarg = nargv[optind]; /* white space */ 192063969Seric place = EMSG; 192163969Seric ++optind; 192263969Seric } 192363969Seric return(optopt); /* dump back option letter */ 192463969Seric } 192563969Seric 192663969Seric #endif 192763969Seric /* 192863969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 192963969Seric */ 193063969Seric 193163969Seric #ifdef NEEDVPRINTF 193263969Seric 193363969Seric #define MAXARG 16 193463969Seric 193563969Seric vfprintf(fp, fmt, ap) 193663969Seric FILE * fp; 193763969Seric char * fmt; 193863969Seric char ** ap; 193963969Seric { 194063969Seric char * bp[MAXARG]; 194163969Seric int i = 0; 194263969Seric 194363969Seric while (*ap && i < MAXARG) 194463969Seric bp[i++] = *ap++; 194563969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 194663969Seric bp[4], bp[5], bp[6], bp[7], 194763969Seric bp[8], bp[9], bp[10], bp[11], 194863969Seric bp[12], bp[13], bp[14], bp[15]); 194963969Seric } 195063969Seric 195163969Seric vsprintf(s, fmt, ap) 195263969Seric char * s; 195363969Seric char * fmt; 195463969Seric char ** ap; 195563969Seric { 195663969Seric char * bp[MAXARG]; 195763969Seric int i = 0; 195863969Seric 195963969Seric while (*ap && i < MAXARG) 196063969Seric bp[i++] = *ap++; 196163969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 196263969Seric bp[4], bp[5], bp[6], bp[7], 196363969Seric bp[8], bp[9], bp[10], bp[11], 196463969Seric bp[12], bp[13], bp[14], bp[15]); 196563969Seric } 196663969Seric 196763969Seric #endif 196863969Seric /* 196965220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 197065208Seric ** 197165211Seric ** Parameters: 197265211Seric ** shell -- the user's shell from /etc/passwd 197365211Seric ** 197465211Seric ** Returns: 197565211Seric ** TRUE -- if it is ok to use this for unrestricted access. 197665211Seric ** FALSE -- if the shell is restricted. 197765208Seric */ 197865208Seric 197965587Seric #if !HASGETUSERSHELL 198065587Seric 198165587Seric # ifndef _PATH_SHELLS 198265587Seric # define _PATH_SHELLS "/etc/shells" 198365587Seric # endif 198465587Seric 198565587Seric char *DefaultUserShells[] = 198665587Seric { 198768508Seric "/bin/sh", /* standard shell */ 198865587Seric "/usr/bin/sh", 198968508Seric "/bin/csh", /* C shell */ 199065587Seric "/usr/bin/csh", 199165587Seric #ifdef __hpux 199268508Seric "/bin/rsh", /* restricted Bourne shell */ 199368508Seric "/bin/ksh", /* Korn shell */ 199468508Seric "/bin/rksh", /* restricted Korn shell */ 199565587Seric "/bin/pam", 199668508Seric "/usr/bin/keysh", /* key shell (extended Korn shell) */ 199765587Seric "/bin/posix/sh", 199865211Seric #endif 199968508Seric #ifdef _AIX3 200068508Seric "/bin/ksh", /* Korn shell */ 200168508Seric "/usr/bin/ksh", 200268508Seric "/bin/tsh", /* trusted shell */ 200368508Seric "/usr/bin/tsh", 200468508Seric "/bin/bsh", /* Bourne shell */ 200568508Seric "/usr/bin/bsh", 200668508Seric #endif 200765587Seric NULL 200865587Seric }; 200965587Seric 201065208Seric #endif 201165208Seric 201265747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 201365747Seric 201465211Seric bool 201565211Seric usershellok(shell) 201665211Seric char *shell; 201765208Seric { 201865211Seric #if HASGETUSERSHELL 201965211Seric register char *p; 202065211Seric extern char *getusershell(); 202165208Seric 202268457Seric if (shell == NULL || shell[0] == '\0') 202368457Seric return TRUE; 202468457Seric 202565211Seric setusershell(); 202665211Seric while ((p = getusershell()) != NULL) 202765747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 202865211Seric break; 202965211Seric endusershell(); 203065211Seric return p != NULL; 203165211Seric #else 203265211Seric register FILE *shellf; 203365211Seric char buf[MAXLINE]; 203465208Seric 203568457Seric if (shell == NULL || shell[0] == '\0') 203668457Seric return TRUE; 203768457Seric 203865211Seric shellf = fopen(_PATH_SHELLS, "r"); 203965211Seric if (shellf == NULL) 204065211Seric { 204165211Seric /* no /etc/shells; see if it is one of the std shells */ 204265587Seric char **d; 204365587Seric 204465587Seric for (d = DefaultUserShells; *d != NULL; d++) 204565587Seric { 204665587Seric if (strcmp(shell, *d) == 0) 204765587Seric return TRUE; 204865587Seric } 204965587Seric return FALSE; 205065211Seric } 205165208Seric 205265211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 205365211Seric { 205465211Seric register char *p, *q; 205565208Seric 205665211Seric p = buf; 205765211Seric while (*p != '\0' && *p != '#' && *p != '/') 205865211Seric p++; 205965211Seric if (*p == '#' || *p == '\0') 206065208Seric continue; 206165211Seric q = p; 206265211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 206365211Seric p++; 206465211Seric *p = '\0'; 206565747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 206665211Seric { 206765211Seric fclose(shellf); 206865211Seric return TRUE; 206965211Seric } 207065208Seric } 207165211Seric fclose(shellf); 207265211Seric return FALSE; 207365211Seric #endif 207465208Seric } 207565208Seric /* 207664311Seric ** FREESPACE -- see how much free space is on the queue filesystem 207758082Seric ** 207858082Seric ** Only implemented if you have statfs. 207958082Seric ** 208058082Seric ** Parameters: 208164311Seric ** dir -- the directory in question. 208264311Seric ** bsize -- a variable into which the filesystem 208364311Seric ** block size is stored. 208458082Seric ** 208558082Seric ** Returns: 208664311Seric ** The number of bytes free on the queue filesystem. 208764311Seric ** -1 if the statfs call fails. 208864311Seric ** 208964311Seric ** Side effects: 209064311Seric ** Puts the filesystem block size into bsize. 209158082Seric */ 209258082Seric 209365749Seric /* statfs types */ 209465749Seric #define SFS_NONE 0 /* no statfs implementation */ 209565749Seric #define SFS_USTAT 1 /* use ustat */ 209665749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 209765749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 209865749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 209965749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 210066752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 210165749Seric 210265749Seric #ifndef SFS_TYPE 210365749Seric # define SFS_TYPE SFS_NONE 210458157Seric #endif 210558157Seric 210665749Seric #if SFS_TYPE == SFS_USTAT 210758157Seric # include <ustat.h> 210858157Seric #endif 210965749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 211065749Seric # include <sys/statfs.h> 211158082Seric #endif 211265749Seric #if SFS_TYPE == SFS_VFS 211365749Seric # include <sys/vfs.h> 211465749Seric #endif 211565749Seric #if SFS_TYPE == SFS_MOUNT 211665749Seric # include <sys/mount.h> 211765749Seric #endif 211866752Seric #if SFS_TYPE == SFS_STATVFS 211966752Seric # include <sys/statvfs.h> 212066752Seric #endif 212158082Seric 212264311Seric long 212364311Seric freespace(dir, bsize) 212464311Seric char *dir; 212564311Seric long *bsize; 212658082Seric { 212765749Seric #if SFS_TYPE != SFS_NONE 212865749Seric # if SFS_TYPE == SFS_USTAT 212958153Seric struct ustat fs; 213058153Seric struct stat statbuf; 213158366Seric # define FSBLOCKSIZE DEV_BSIZE 213268508Seric # define FSF_BAVAIL f_tfree 213358157Seric # else 213458157Seric # if defined(ultrix) 213558157Seric struct fs_data fs; 213668508Seric # define FSF_BAVAIL fd_bfreen 213768508Seric # define FSBLOCKSIZE 1024L 213858153Seric # else 213966752Seric # if SFS_TYPE == SFS_STATVFS 214066752Seric struct statvfs fs; 214168508Seric # define FSBLOCKSIZE fs.f_frsize 214266752Seric # else 214358082Seric struct statfs fs; 214466752Seric # define FSBLOCKSIZE fs.f_bsize 214563987Seric # endif 214658153Seric # endif 214758133Seric # endif 214868508Seric # ifndef FSF_BAVAIL 214968508Seric # define FSF_BAVAIL f_bavail 215068508Seric # endif 215158082Seric 215265749Seric # if SFS_TYPE == SFS_USTAT 215364311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 215458157Seric # else 215565749Seric # if SFS_TYPE == SFS_4ARGS 215664311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 215758157Seric # else 215868508Seric # if SFS_TYPE == SFS_STATVFS 215968508Seric if (statvfs(dir, &fs) == 0) 216068508Seric # else 216168508Seric # if defined(ultrix) 216268499Seric if (statfs(dir, &fs) > 0) 216368508Seric # else 216464311Seric if (statfs(dir, &fs) == 0) 216568508Seric # endif 216658153Seric # endif 216758133Seric # endif 216858133Seric # endif 216958133Seric { 217064311Seric if (bsize != NULL) 217164311Seric *bsize = FSBLOCKSIZE; 217268508Seric return (fs.FSF_BAVAIL); 217364311Seric } 217464337Seric #endif 217564311Seric return (-1); 217664311Seric } 217764311Seric /* 217864311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 217964311Seric ** 218064311Seric ** Only implemented if you have statfs. 218164311Seric ** 218264311Seric ** Parameters: 218364311Seric ** msize -- the size to check against. If zero, we don't yet 218464311Seric ** know how big the message will be, so just check for 218564311Seric ** a "reasonable" amount. 218664311Seric ** 218764311Seric ** Returns: 218864311Seric ** TRUE if there is enough space. 218964311Seric ** FALSE otherwise. 219064311Seric */ 219164311Seric 219264311Seric bool 219364311Seric enoughspace(msize) 219464311Seric long msize; 219564311Seric { 219664311Seric long bfree, bsize; 219764311Seric 219864311Seric if (MinBlocksFree <= 0 && msize <= 0) 219964311Seric { 220058133Seric if (tTd(4, 80)) 220164311Seric printf("enoughspace: no threshold\n"); 220264311Seric return TRUE; 220364311Seric } 220464311Seric 220564311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 220664311Seric { 220764311Seric if (tTd(4, 80)) 220858333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 220964311Seric bfree, msize); 221058333Seric 221158333Seric /* convert msize to block count */ 221264311Seric msize = msize / bsize + 1; 221358333Seric if (MinBlocksFree >= 0) 221458333Seric msize += MinBlocksFree; 221558333Seric 221664311Seric if (bfree < msize) 221758090Seric { 221858090Seric #ifdef LOG 221958090Seric if (LogLevel > 0) 222064254Seric syslog(LOG_ALERT, 222164254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 222268508Seric CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id, 222368508Seric bfree, 222468508Seric CurHostName == NULL ? "SMTP-DAEMON" : CurHostName, 222568508Seric msize, QueueDir); 222658090Seric #endif 222758082Seric return FALSE; 222858090Seric } 222958082Seric } 223058082Seric else if (tTd(4, 80)) 223158333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 223258333Seric MinBlocksFree, msize, errstring(errno)); 223358082Seric return TRUE; 223458082Seric } 223558542Seric /* 223658542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 223758542Seric ** 223858542Seric ** This looks at an errno value and tells if this is likely to 223958542Seric ** go away if retried later. 224058542Seric ** 224158542Seric ** Parameters: 224258542Seric ** err -- the errno code to classify. 224358542Seric ** 224458542Seric ** Returns: 224558542Seric ** TRUE if this is probably transient. 224658542Seric ** FALSE otherwise. 224758542Seric */ 224858542Seric 224958542Seric bool 225058542Seric transienterror(err) 225158542Seric int err; 225258542Seric { 225358542Seric switch (err) 225458542Seric { 225558542Seric case EIO: /* I/O error */ 225658542Seric case ENXIO: /* Device not configured */ 225758542Seric case EAGAIN: /* Resource temporarily unavailable */ 225858542Seric case ENOMEM: /* Cannot allocate memory */ 225958542Seric case ENODEV: /* Operation not supported by device */ 226058542Seric case ENFILE: /* Too many open files in system */ 226158542Seric case EMFILE: /* Too many open files */ 226258542Seric case ENOSPC: /* No space left on device */ 226358542Seric #ifdef ETIMEDOUT 226458542Seric case ETIMEDOUT: /* Connection timed out */ 226558542Seric #endif 226658542Seric #ifdef ESTALE 226758542Seric case ESTALE: /* Stale NFS file handle */ 226858542Seric #endif 226958542Seric #ifdef ENETDOWN 227058542Seric case ENETDOWN: /* Network is down */ 227158542Seric #endif 227258542Seric #ifdef ENETUNREACH 227358542Seric case ENETUNREACH: /* Network is unreachable */ 227458542Seric #endif 227558542Seric #ifdef ENETRESET 227658542Seric case ENETRESET: /* Network dropped connection on reset */ 227758542Seric #endif 227858542Seric #ifdef ECONNABORTED 227958542Seric case ECONNABORTED: /* Software caused connection abort */ 228058542Seric #endif 228158542Seric #ifdef ECONNRESET 228258542Seric case ECONNRESET: /* Connection reset by peer */ 228358542Seric #endif 228458542Seric #ifdef ENOBUFS 228558542Seric case ENOBUFS: /* No buffer space available */ 228658542Seric #endif 228758542Seric #ifdef ESHUTDOWN 228858542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 228958542Seric #endif 229058542Seric #ifdef ECONNREFUSED 229158542Seric case ECONNREFUSED: /* Connection refused */ 229258542Seric #endif 229358542Seric #ifdef EHOSTDOWN 229458542Seric case EHOSTDOWN: /* Host is down */ 229558542Seric #endif 229658542Seric #ifdef EHOSTUNREACH 229758542Seric case EHOSTUNREACH: /* No route to host */ 229858542Seric #endif 229958542Seric #ifdef EDQUOT 230058542Seric case EDQUOT: /* Disc quota exceeded */ 230158542Seric #endif 230258542Seric #ifdef EPROCLIM 230358542Seric case EPROCLIM: /* Too many processes */ 230458542Seric #endif 230558542Seric #ifdef EUSERS 230658542Seric case EUSERS: /* Too many users */ 230758542Seric #endif 230858542Seric #ifdef EDEADLK 230958542Seric case EDEADLK: /* Resource deadlock avoided */ 231058542Seric #endif 231158542Seric #ifdef EISCONN 231258542Seric case EISCONN: /* Socket already connected */ 231358542Seric #endif 231458542Seric #ifdef EINPROGRESS 231558542Seric case EINPROGRESS: /* Operation now in progress */ 231658542Seric #endif 231758542Seric #ifdef EALREADY 231858542Seric case EALREADY: /* Operation already in progress */ 231958542Seric #endif 232058542Seric #ifdef EADDRINUSE 232158542Seric case EADDRINUSE: /* Address already in use */ 232258542Seric #endif 232358542Seric #ifdef EADDRNOTAVAIL 232458542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 232558542Seric #endif 232666018Seric #ifdef ETXTBSY 232766017Seric case ETXTBSY: /* (Apollo) file locked */ 232866017Seric #endif 232963834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 233058542Seric case ENOSR: /* Out of streams resources */ 233158542Seric #endif 233258542Seric return TRUE; 233358542Seric } 233458542Seric 233558542Seric /* nope, must be permanent */ 233658542Seric return FALSE; 233758542Seric } 233858689Seric /* 233964035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 234058689Seric ** 234158689Seric ** Parameters: 234258689Seric ** fd -- the file descriptor of the file. 234358689Seric ** filename -- the file name (for error messages). 234464335Seric ** ext -- the filename extension. 234558689Seric ** type -- type of the lock. Bits can be: 234658689Seric ** LOCK_EX -- exclusive lock. 234758689Seric ** LOCK_NB -- non-blocking. 234858689Seric ** 234958689Seric ** Returns: 235058689Seric ** TRUE if the lock was acquired. 235158689Seric ** FALSE otherwise. 235258689Seric */ 235358689Seric 235458689Seric bool 235564335Seric lockfile(fd, filename, ext, type) 235658689Seric int fd; 235758689Seric char *filename; 235864335Seric char *ext; 235958689Seric int type; 236058689Seric { 236165830Seric # if !HASFLOCK 236258689Seric int action; 236358689Seric struct flock lfd; 236464335Seric 236564335Seric if (ext == NULL) 236664335Seric ext = ""; 236764035Seric 236863983Seric bzero(&lfd, sizeof lfd); 236959447Seric if (bitset(LOCK_UN, type)) 237059447Seric lfd.l_type = F_UNLCK; 237159447Seric else if (bitset(LOCK_EX, type)) 237258689Seric lfd.l_type = F_WRLCK; 237358689Seric else 237458689Seric lfd.l_type = F_RDLCK; 237558689Seric 237658689Seric if (bitset(LOCK_NB, type)) 237758689Seric action = F_SETLK; 237858689Seric else 237958689Seric action = F_SETLKW; 238058689Seric 238164264Seric if (tTd(55, 60)) 238264335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 238364335Seric filename, ext, action, lfd.l_type); 238464264Seric 238558689Seric if (fcntl(fd, action, &lfd) >= 0) 238664264Seric { 238764264Seric if (tTd(55, 60)) 238864264Seric printf("SUCCESS\n"); 238958689Seric return TRUE; 239064264Seric } 239158689Seric 239264264Seric if (tTd(55, 60)) 239364264Seric printf("(%s) ", errstring(errno)); 239464264Seric 239564136Seric /* 239664136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 239764136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 239864136Seric ** as type "tmp" (that is, served from swap space), the 239964136Seric ** previous fcntl will fail with "Invalid argument" errors. 240064136Seric ** Since this is fairly common during testing, we will assume 240164136Seric ** that this indicates that the lock is successfully grabbed. 240264136Seric */ 240364136Seric 240464136Seric if (errno == EINVAL) 240564264Seric { 240664264Seric if (tTd(55, 60)) 240764264Seric printf("SUCCESS\n"); 240864136Seric return TRUE; 240964264Seric } 241064136Seric 241158689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 241264378Seric { 241364378Seric int omode = -1; 241464378Seric # ifdef F_GETFL 241564378Seric int oerrno = errno; 241664378Seric 241764378Seric (void) fcntl(fd, F_GETFL, &omode); 241864378Seric errno = oerrno; 241964378Seric # endif 242064378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 242164378Seric filename, ext, fd, type, omode, geteuid()); 242264378Seric } 242358689Seric # else 242464335Seric if (ext == NULL) 242564335Seric ext = ""; 242664335Seric 242764264Seric if (tTd(55, 60)) 242864335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 242964264Seric 243058689Seric if (flock(fd, type) >= 0) 243164264Seric { 243264264Seric if (tTd(55, 60)) 243364264Seric printf("SUCCESS\n"); 243458689Seric return TRUE; 243564264Seric } 243658689Seric 243764264Seric if (tTd(55, 60)) 243864264Seric printf("(%s) ", errstring(errno)); 243964264Seric 244058689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 244164378Seric { 244264378Seric int omode = -1; 244364378Seric # ifdef F_GETFL 244464378Seric int oerrno = errno; 244564378Seric 244664378Seric (void) fcntl(fd, F_GETFL, &omode); 244764378Seric errno = oerrno; 244864378Seric # endif 244964378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 245064378Seric filename, ext, fd, type, omode, geteuid()); 245164386Seric } 245258689Seric # endif 245364264Seric if (tTd(55, 60)) 245464264Seric printf("FAIL\n"); 245558689Seric return FALSE; 245658689Seric } 245764556Seric /* 245865948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 245965948Seric ** 246065948Seric ** Parameters: 246165948Seric ** fd -- the file descriptor to check. 246265948Seric ** 246365948Seric ** Returns: 246465948Seric ** TRUE -- if only root can chown the file to an arbitrary 246565948Seric ** user. 246665948Seric ** FALSE -- if an arbitrary user can give away a file. 246765948Seric */ 246865948Seric 246965948Seric bool 247065948Seric chownsafe(fd) 247165948Seric int fd; 247265948Seric { 247365950Seric #ifdef __hpux 247465948Seric char *s; 247565948Seric int tfd; 247665948Seric uid_t o_uid, o_euid; 247765948Seric gid_t o_gid, o_egid; 247865948Seric bool rval; 247965948Seric struct stat stbuf; 248065948Seric 248165948Seric o_uid = getuid(); 248265948Seric o_euid = geteuid(); 248365948Seric o_gid = getgid(); 248465948Seric o_egid = getegid(); 248565959Seric fstat(fd, &stbuf); 248665948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 248765948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 248865948Seric s = tmpnam(NULL); 248965948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 249065974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 249165948Seric close(tfd); 249265948Seric unlink(s); 249365948Seric setreuid(o_uid, o_euid); 249465948Seric setresgid(o_gid, o_egid, -1); 249565948Seric return rval; 249665948Seric #else 249766088Seric # ifdef _POSIX_CHOWN_RESTRICTED 249866088Seric # if _POSIX_CHOWN_RESTRICTED == -1 249966088Seric return FALSE; 250066088Seric # else 250166088Seric return TRUE; 250266088Seric # endif 250366088Seric # else 250466088Seric # ifdef _PC_CHOWN_RESTRICTED 250568508Seric int rval; 250668508Seric 250768508Seric /* 250868508Seric ** Some systems (e.g., SunOS) seem to have the call and the 250968508Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 251068508Seric ** the call. This heuristic checks for that. 251168508Seric */ 251268508Seric 251368508Seric errno = 0; 251468508Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 251568508Seric if (errno == 0) 251668508Seric return rval > 0; 251768508Seric # endif 251868508Seric # ifdef BSD 251968508Seric return TRUE; 252068499Seric # else 252165948Seric return FALSE; 252265948Seric # endif 252365948Seric # endif 252465948Seric #endif 252565948Seric } 252665948Seric /* 252768508Seric ** RESETLIMITS -- reset system controlled resource limits 252868508Seric ** 252968508Seric ** This is to avoid denial-of-service attacks 253068508Seric ** 253168508Seric ** Parameters: 253268508Seric ** none 253368508Seric ** 253468508Seric ** Returns: 253568508Seric ** none 253668508Seric */ 253768508Seric 253868508Seric #if HASSETRLIMIT 253968508Seric # include <sys/resource.h> 254068508Seric #endif 254168508Seric 254268508Seric void 254368508Seric resetlimits() 254468508Seric { 254568508Seric #if HASSETRLIMIT 254668508Seric struct rlimit lim; 254768508Seric 254868508Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 254968508Seric (void) setrlimit(RLIMIT_CPU, &lim); 255068508Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 255168508Seric #else 255268508Seric # if HASULIMIT 255368508Seric (void) ulimit(2, 0x3fffff); 255468508Seric # endif 255568508Seric #endif 255668508Seric } 255768508Seric /* 255864556Seric ** GETCFNAME -- return the name of the .cf file. 255964556Seric ** 256064556Seric ** Some systems (e.g., NeXT) determine this dynamically. 256164556Seric */ 256264556Seric 256364556Seric char * 256464556Seric getcfname() 256564556Seric { 256664556Seric if (ConfFile != NULL) 256764556Seric return ConfFile; 256865214Seric #ifdef NETINFO 256965214Seric { 257065214Seric extern char *ni_propval(); 257165214Seric char *cflocation; 257265214Seric 257368508Seric cflocation = ni_propval("/locations", NULL, "sendmail", 257468508Seric "sendmail.cf", '\0'); 257565214Seric if (cflocation != NULL) 257665214Seric return cflocation; 257765214Seric } 257865214Seric #endif 257964556Seric return _PATH_SENDMAILCF; 258064556Seric } 258164718Seric /* 258264718Seric ** SETVENDOR -- process vendor code from V configuration line 258364718Seric ** 258464718Seric ** Parameters: 258564718Seric ** vendor -- string representation of vendor. 258664718Seric ** 258764718Seric ** Returns: 258864718Seric ** TRUE -- if ok. 258964718Seric ** FALSE -- if vendor code could not be processed. 259064926Seric ** 259164926Seric ** Side Effects: 259264926Seric ** It is reasonable to set mode flags here to tweak 259364926Seric ** processing in other parts of the code if necessary. 259464926Seric ** For example, if you are a vendor that uses $%y to 259564926Seric ** indicate YP lookups, you could enable that here. 259664718Seric */ 259764718Seric 259864718Seric bool 259964718Seric setvendor(vendor) 260064718Seric char *vendor; 260164718Seric { 260264926Seric if (strcasecmp(vendor, "Berkeley") == 0) 260368508Seric { 260468508Seric VendorCode = VENDOR_BERKELEY; 260564926Seric return TRUE; 260668508Seric } 260764926Seric 260864926Seric /* add vendor extensions here */ 260964926Seric 261068508Seric #ifdef SUN_EXTENSIONS 261168508Seric if (strcasecmp(vendor, "Sun") == 0) 261268508Seric { 261368508Seric VendorCode = VENDOR_SUN; 261468508Seric return TRUE; 261568508Seric } 261668508Seric #endif 261768508Seric 261864926Seric return FALSE; 261964718Seric } 262064816Seric /* 262164816Seric ** STRTOL -- convert string to long integer 262264816Seric ** 262364816Seric ** For systems that don't have it in the C library. 262466161Seric ** 262566161Seric ** This is taken verbatim from the 4.4-Lite C library. 262664816Seric */ 262764816Seric 262864816Seric #ifdef NEEDSTRTOL 262964816Seric 263066161Seric #if defined(LIBC_SCCS) && !defined(lint) 263166161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 263266161Seric #endif /* LIBC_SCCS and not lint */ 263366161Seric 263466161Seric #include <limits.h> 263566161Seric 263666161Seric /* 263766161Seric * Convert a string to a long integer. 263866161Seric * 263966161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 264066161Seric * alphabets and digits are each contiguous. 264166161Seric */ 264266161Seric 264364816Seric long 264466161Seric strtol(nptr, endptr, base) 264566161Seric const char *nptr; 264666161Seric char **endptr; 264766161Seric register int base; 264864816Seric { 264966161Seric register const char *s = nptr; 265066161Seric register unsigned long acc; 265166161Seric register int c; 265266161Seric register unsigned long cutoff; 265366161Seric register int neg = 0, any, cutlim; 265464816Seric 265566161Seric /* 265666161Seric * Skip white space and pick up leading +/- sign if any. 265766161Seric * If base is 0, allow 0x for hex and 0 for octal, else 265866161Seric * assume decimal; if base is already 16, allow 0x. 265966161Seric */ 266066161Seric do { 266166161Seric c = *s++; 266266161Seric } while (isspace(c)); 266366161Seric if (c == '-') { 266466161Seric neg = 1; 266566161Seric c = *s++; 266666161Seric } else if (c == '+') 266766161Seric c = *s++; 266866161Seric if ((base == 0 || base == 16) && 266966161Seric c == '0' && (*s == 'x' || *s == 'X')) { 267066161Seric c = s[1]; 267166161Seric s += 2; 267266161Seric base = 16; 267366161Seric } 267466161Seric if (base == 0) 267566161Seric base = c == '0' ? 8 : 10; 267664816Seric 267766161Seric /* 267866161Seric * Compute the cutoff value between legal numbers and illegal 267966161Seric * numbers. That is the largest legal value, divided by the 268066161Seric * base. An input number that is greater than this value, if 268166161Seric * followed by a legal input character, is too big. One that 268266161Seric * is equal to this value may be valid or not; the limit 268366161Seric * between valid and invalid numbers is then based on the last 268466161Seric * digit. For instance, if the range for longs is 268566161Seric * [-2147483648..2147483647] and the input base is 10, 268666161Seric * cutoff will be set to 214748364 and cutlim to either 268766161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 268866161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 268966161Seric * the number is too big, and we will return a range error. 269066161Seric * 269166161Seric * Set any if any `digits' consumed; make it negative to indicate 269266161Seric * overflow. 269366161Seric */ 269466161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 269566161Seric cutlim = cutoff % (unsigned long)base; 269666161Seric cutoff /= (unsigned long)base; 269766161Seric for (acc = 0, any = 0;; c = *s++) { 269866161Seric if (isdigit(c)) 269966161Seric c -= '0'; 270066161Seric else if (isalpha(c)) 270166161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 270266161Seric else 270366161Seric break; 270466161Seric if (c >= base) 270566161Seric break; 270666161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 270766161Seric any = -1; 270866161Seric else { 270966161Seric any = 1; 271066161Seric acc *= base; 271166161Seric acc += c; 271264816Seric } 271364816Seric } 271466161Seric if (any < 0) { 271566161Seric acc = neg ? LONG_MIN : LONG_MAX; 271666161Seric errno = ERANGE; 271766161Seric } else if (neg) 271866161Seric acc = -acc; 271966161Seric if (endptr != 0) 272066161Seric *endptr = (char *)(any ? s - 1 : nptr); 272166161Seric return (acc); 272264816Seric } 272364816Seric 272464816Seric #endif 272564841Seric /* 272664841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 272764841Seric ** 272868508Seric ** Solaris versions at least through 2.3 don't properly deliver a 272964841Seric ** canonical h_name field. This tries to work around it. 273064841Seric */ 273164841Seric 273268508Seric #if defined(SOLARIS) && SOLARIS < 204 273364841Seric 273468482Seric extern int h_errno; 273568482Seric 273664841Seric struct hostent * 273764841Seric solaris_gethostbyname(name) 273865094Seric const char *name; 273964841Seric { 274068508Seric # if SOLARIS == 203 274164841Seric static struct hostent hp; 274264841Seric static char buf[1000]; 274364841Seric extern struct hostent *_switch_gethostbyname_r(); 274464841Seric 274564841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 274664841Seric # else 274765009Seric extern struct hostent *__switch_gethostbyname(); 274865009Seric 274964841Seric return __switch_gethostbyname(name); 275064841Seric # endif 275164841Seric } 275264841Seric 275364841Seric struct hostent * 275464841Seric solaris_gethostbyaddr(addr, len, type) 275565094Seric const char *addr; 275664841Seric int len; 275764841Seric int type; 275864841Seric { 275968508Seric # if SOLARIS == 203 276064841Seric static struct hostent hp; 276164841Seric static char buf[1000]; 276264841Seric extern struct hostent *_switch_gethostbyaddr_r(); 276364841Seric 276464841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 276564841Seric # else 276665009Seric extern struct hostent *__switch_gethostbyaddr(); 276765009Seric 276864841Seric return __switch_gethostbyaddr(addr, len, type); 276964841Seric # endif 277064841Seric } 277164841Seric 277264841Seric #endif 277365214Seric /* 277465214Seric ** NI_PROPVAL -- netinfo property value lookup routine 277565214Seric ** 277665214Seric ** Parameters: 277768508Seric ** keydir -- the Netinfo directory name in which to search 277868508Seric ** for the key. 277968508Seric ** keyprop -- the name of the property in which to find the 278068508Seric ** property we are interested. Defaults to "name". 278168508Seric ** keyval -- the value for which we are really searching. 278268508Seric ** valprop -- the property name for the value in which we 278368508Seric ** are interested. 278468508Seric ** sepchar -- if non-nil, this can be multiple-valued, and 278568508Seric ** we should return a string separated by this 278668508Seric ** character. 278765214Seric ** 278865214Seric ** Returns: 278965214Seric ** NULL -- if: 279065214Seric ** 1. the directory is not found 279165214Seric ** 2. the property name is not found 279265214Seric ** 3. the property contains multiple values 279365214Seric ** 4. some error occured 279465214Seric ** else -- the location of the config file. 279565214Seric ** 279668508Seric ** Example: 279768508Seric ** To search for an alias value, use: 279868508Seric ** ni_propval("/aliases", "name", aliasname, "members", ',') 279968508Seric ** 280065214Seric ** Notes: 280165214Seric ** Caller should free the return value of ni_proval 280265214Seric */ 280365214Seric 280465214Seric #ifdef NETINFO 280565214Seric 280665214Seric # include <netinfo/ni.h> 280765214Seric 280865214Seric # define LOCAL_NETINFO_DOMAIN "." 280965214Seric # define PARENT_NETINFO_DOMAIN ".." 281065214Seric # define MAX_NI_LEVELS 256 281165214Seric 281265214Seric char * 281368508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar) 281468508Seric char *keydir; 281568508Seric char *keyprop; 281668508Seric char *keyval; 281768508Seric char *valprop; 281868508Seric char sepchar; 281965214Seric { 282065820Seric char *propval = NULL; 282165214Seric int i; 282268508Seric int j, alen; 282365214Seric void *ni = NULL; 282465214Seric void *lastni = NULL; 282565214Seric ni_status nis; 282665214Seric ni_id nid; 282765214Seric ni_namelist ninl; 282868508Seric register char *p; 282968508Seric char keybuf[1024]; 283065214Seric 283165214Seric /* 283268508Seric ** Create the full key from the two parts. 283368508Seric ** 283468508Seric ** Note that directory can end with, e.g., "name=" to specify 283568508Seric ** an alternate search property. 283668508Seric */ 283768508Seric 283868508Seric i = strlen(keydir) + strlen(keyval) + 2; 283968508Seric if (keyprop != NULL) 284068508Seric i += strlen(keyprop) + 1; 284168508Seric if (i > sizeof keybuf) 284268508Seric return NULL; 284368508Seric strcpy(keybuf, keydir); 284468508Seric strcat(keybuf, "/"); 284568508Seric if (keyprop != NULL) 284668508Seric { 284768508Seric strcat(keybuf, keyprop); 284868508Seric strcat(keybuf, "="); 284968508Seric } 285068508Seric strcat(keybuf, keyval); 285168508Seric 285268508Seric /* 285365214Seric ** If the passed directory and property name are found 285465214Seric ** in one of netinfo domains we need to search (starting 285565214Seric ** from the local domain moving all the way back to the 285665214Seric ** root domain) set propval to the property's value 285765214Seric ** and return it. 285865214Seric */ 285965214Seric 286065214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 286165214Seric { 286265214Seric if (i == 0) 286365214Seric { 286465214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 286565214Seric } 286665214Seric else 286765214Seric { 286865214Seric if (lastni != NULL) 286965214Seric ni_free(lastni); 287065214Seric lastni = ni; 287165214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 287265214Seric } 287365214Seric 287465214Seric /* 287565214Seric ** Don't bother if we didn't get a handle on a 287665214Seric ** proper domain. This is not necessarily an error. 287765214Seric ** We would get a positive ni_status if, for instance 287865214Seric ** we never found the directory or property and tried 287965214Seric ** to open the parent of the root domain! 288065214Seric */ 288165214Seric 288265214Seric if (nis != 0) 288365214Seric break; 288465214Seric 288565214Seric /* 288665214Seric ** Find the path to the server information. 288765214Seric */ 288865214Seric 288968508Seric if (ni_pathsearch(ni, &nid, keybuf) != 0) 289065214Seric continue; 289165214Seric 289265214Seric /* 289368508Seric ** Find associated value information. 289465214Seric */ 289565214Seric 289668508Seric if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0) 289765214Seric continue; 289865214Seric 289965214Seric /* 290068508Seric ** See if we have an acceptable number of values. 290165214Seric */ 290265214Seric 290368508Seric if (ninl.ni_namelist_len <= 0) 290468508Seric continue; 290568508Seric 290668508Seric if (sepchar == '\0' && ninl.ni_namelist_len > 1) 290765214Seric { 290868508Seric ni_namelist_free(&ninl); 290968508Seric continue; 291065214Seric } 291168508Seric 291268508Seric /* 291368508Seric ** Calculate number of bytes needed and build result 291468508Seric */ 291568508Seric 291668508Seric alen = 1; 291768508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 291868508Seric alen += strlen(ninl.ni_namelist_val[j]) + 1; 291968508Seric propval = p = xalloc(alen); 292068508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 292168508Seric { 292268508Seric strcpy(p, ninl.ni_namelist_val[j]); 292368508Seric p += strlen(p); 292468508Seric *p++ = sepchar; 292568508Seric } 292668508Seric *--p = '\0'; 292768508Seric 292868508Seric ni_namelist_free(&ninl); 292965214Seric } 293065214Seric 293165214Seric /* 293265214Seric ** Clean up. 293365214Seric */ 293465214Seric 293565214Seric if (ni != NULL) 293665214Seric ni_free(ni); 293765214Seric if (lastni != NULL && ni != lastni) 293865214Seric ni_free(lastni); 293965214Seric 294065214Seric return propval; 294165214Seric } 294265214Seric 294365214Seric #endif /* NETINFO */ 294465504Seric /* 294565504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 294665504Seric ** 294765504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 294865504Seric ** syslog succeeds during interrupt handlers. 294965504Seric */ 295065504Seric 295165504Seric #ifdef __hpux 295265504Seric 295365504Seric # define MAXSYSLOGTRIES 100 295465504Seric # undef syslog 295565504Seric 295665504Seric # ifdef __STDC__ 295765504Seric hard_syslog(int pri, char *msg, ...) 295865504Seric # else 295965504Seric hard_syslog(pri, msg, va_alist) 296065504Seric int pri; 296165504Seric char *msg; 296265504Seric va_dcl 296365504Seric # endif 296465504Seric { 296565504Seric int i; 296665504Seric char buf[SYSLOG_BUFSIZE * 2]; 296765504Seric VA_LOCAL_DECL; 296865504Seric 296965504Seric VA_START(msg); 297065504Seric vsprintf(buf, msg, ap); 297165504Seric VA_END; 297265504Seric 297365504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 297465504Seric continue; 297565504Seric } 297665504Seric 297765504Seric #endif 2978