122698Sdist /* 268839Seric * Copyright (c) 1983, 1995 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*69919Seric static char sccsid[] = "@(#)conf.c 8.200 (Berkeley) 06/18/95"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1363937Seric # include "sendmail.h" 1463937Seric # include "pathnames.h" 1514881Seric # include <sys/ioctl.h> 1658082Seric # include <sys/param.h> 17404Seric 18294Seric /* 193309Seric ** CONF.C -- Sendmail Configuration Tables. 20294Seric ** 21294Seric ** Defines the configuration of this installation. 22294Seric ** 231388Seric ** Configuration Variables: 242897Seric ** HdrInfo -- a table describing well-known header fields. 252897Seric ** Each entry has the field name and some flags, 264147Seric ** which are described in sendmail.h. 274093Seric ** 284093Seric ** Notes: 294093Seric ** I have tried to put almost all the reasonable 304093Seric ** configuration information into the configuration 314093Seric ** file read at runtime. My intent is that anything 324093Seric ** here is a function of the version of UNIX you 334093Seric ** are running, or is really static -- for example 344093Seric ** the headers are a superset of widely used 354093Seric ** protocols. If you find yourself playing with 364093Seric ** this file too much, you may be making a mistake! 37294Seric */ 38294Seric 39294Seric 40294Seric 41294Seric 424437Seric /* 432897Seric ** Header info table 443057Seric ** Final (null) entry contains the flags used for any other field. 454147Seric ** 464147Seric ** Not all of these are actually handled specially by sendmail 474147Seric ** at this time. They are included as placeholders, to let 484147Seric ** you know that "someday" I intend to have sendmail do 494147Seric ** something with them. 502897Seric */ 512897Seric 522897Seric struct hdrinfo HdrInfo[] = 532897Seric { 548060Seric /* originator fields, most to least significant */ 5568508Seric "resent-sender", H_FROM|H_RESENT, 5668508Seric "resent-from", H_FROM|H_RESENT, 5768508Seric "resent-reply-to", H_FROM|H_RESENT, 5868508Seric "sender", H_FROM, 5968508Seric "from", H_FROM, 6068508Seric "reply-to", H_FROM, 6168508Seric "full-name", H_ACHECK, 6268508Seric "return-receipt-to", H_FROM|H_RECEIPTTO, 6368508Seric "errors-to", H_FROM|H_ERRORSTO, 6458796Seric 658060Seric /* destination fields */ 6668508Seric "to", H_RCPT, 6768508Seric "resent-to", H_RCPT|H_RESENT, 6868508Seric "cc", H_RCPT, 6968508Seric "resent-cc", H_RCPT|H_RESENT, 7068508Seric "bcc", H_RCPT|H_STRIPVAL, 7168508Seric "resent-bcc", H_RCPT|H_STRIPVAL|H_RESENT, 7268508Seric "apparently-to", H_RCPT, 7358796Seric 748060Seric /* message identification and control */ 7568508Seric "message-id", 0, 7668508Seric "resent-message-id", H_RESENT, 7768508Seric "message", H_EOH, 7868508Seric "text", H_EOH, 7958796Seric 8011417Seric /* date fields */ 8168508Seric "date", 0, 8268508Seric "resent-date", H_RESENT, 8358796Seric 848060Seric /* trace fields */ 8568508Seric "received", H_TRACE|H_FORCE, 8668508Seric "x400-received", H_TRACE|H_FORCE, 8768508Seric "via", H_TRACE|H_FORCE, 8868508Seric "mail-from", H_TRACE|H_FORCE, 898060Seric 9058796Seric /* miscellaneous fields */ 9168508Seric "comments", H_FORCE, 9268508Seric "return-path", H_FORCE|H_ACHECK, 9368508Seric "content-transfer-encoding", H_CTE, 9468508Seric "content-type", H_CTYPE, 9568706Seric "content-length", H_ACHECK, 9658796Seric 979055Seric NULL, 0, 982897Seric }; 994166Seric 1004166Seric 1014166Seric 1024282Seric /* 1034282Seric ** Location of system files/databases/etc. 1044282Seric */ 1054282Seric 10658082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1079039Seric 1089064Seric 1099064Seric 1109039Seric /* 11158082Seric ** Privacy values 11258082Seric */ 11358082Seric 11458082Seric struct prival PrivacyValues[] = 11558082Seric { 11658082Seric "public", PRIV_PUBLIC, 11758082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11858114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11958082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 12058082Seric "noexpn", PRIV_NOEXPN, 12158082Seric "novrfy", PRIV_NOVRFY, 12264333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12364333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12458789Seric "authwarnings", PRIV_AUTHWARNINGS, 12566783Seric "noreceipts", PRIV_NORECEIPTS, 12658082Seric "goaway", PRIV_GOAWAY, 12758789Seric NULL, 0, 12858082Seric }; 12958082Seric 13058082Seric 13158082Seric 13258082Seric /* 13324943Seric ** Miscellaneous stuff. 1349039Seric */ 1359039Seric 13624943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13724943Seric /* 13824943Seric ** SETDEFAULTS -- set default values 13924943Seric ** 14024943Seric ** Because of the way freezing is done, these must be initialized 14124943Seric ** using direct code. 14224943Seric ** 14324943Seric ** Parameters: 14458734Seric ** e -- the default envelope. 14524943Seric ** 14624943Seric ** Returns: 14724943Seric ** none. 14824943Seric ** 14924943Seric ** Side Effects: 15024943Seric ** Initializes a bunch of global variables to their 15124943Seric ** default values. 15224943Seric */ 15324943Seric 15458737Seric #define DAYS * 24 * 60 * 60 15558737Seric 15668508Seric void 15758734Seric setdefaults(e) 15858734Seric register ENVELOPE *e; 15924943Seric { 16068508Seric int i; 16168508Seric extern void inittimeouts(); 16268508Seric extern void setdefuser(); 16368508Seric extern void setupmaps(); 16468508Seric extern void setupmailers(); 16568508Seric 16657438Seric SpaceSub = ' '; /* option B */ 16757438Seric QueueLA = 8; /* option x */ 16857438Seric RefuseLA = 12; /* option X */ 16957438Seric WkRecipFact = 30000L; /* option y */ 17057438Seric WkClassFact = 1800L; /* option z */ 17157438Seric WkTimeFact = 90000L; /* option Z */ 17257438Seric QueueFactor = WkRecipFact * 20; /* option q */ 17363787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 17457438Seric /* option F */ 17557438Seric DefUid = 1; /* option u */ 17657438Seric DefGid = 1; /* option g */ 17757438Seric CheckpointInterval = 10; /* option C */ 17857438Seric MaxHopCount = 25; /* option h */ 17958734Seric e->e_sendmode = SM_FORK; /* option d */ 18058734Seric e->e_errormode = EM_PRINT; /* option e */ 18168508Seric SevenBitInput = FALSE; /* option 7 */ 18257438Seric MaxMciCache = 1; /* option k */ 18357438Seric MciCacheTimeout = 300; /* option K */ 18457438Seric LogLevel = 9; /* option L */ 18568508Seric inittimeouts(NULL); /* option r */ 18658853Seric PrivacyFlags = 0; /* option p */ 18769480Seric #if MIME8TO7 18868508Seric MimeMode = MM_CVTMIME|MM_PASS8BIT; /* option 8 */ 18969480Seric #else 19069480Seric MimeMode = MM_PASS8BIT; 19169480Seric #endif 19268508Seric for (i = 0; i < MAXTOCLASS; i++) 19368508Seric { 19468508Seric TimeOuts.to_q_return[i] = 5 DAYS; /* option T */ 19568508Seric TimeOuts.to_q_warning[i] = 0; /* option T */ 19668508Seric } 19768508Seric ServiceSwitchFile = "/etc/service.switch"; 19869401Seric HostsFile = _PATH_HOSTS; 19940973Sbostic setdefuser(); 20053654Seric setupmaps(); 20157402Seric setupmailers(); 20224943Seric } 203294Seric 20440973Sbostic 2054326Seric /* 20640973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20740973Sbostic */ 20840973Sbostic 20968508Seric void 21040973Sbostic setdefuser() 21140973Sbostic { 21240973Sbostic struct passwd *defpwent; 21357386Seric static char defuserbuf[40]; 21440973Sbostic 21557386Seric DefUser = defuserbuf; 21668693Seric if ((defpwent = sm_getpwuid(DefUid)) != NULL) 21757386Seric strcpy(defuserbuf, defpwent->pw_name); 21840973Sbostic else 21957386Seric strcpy(defuserbuf, "nobody"); 22040973Sbostic } 22153654Seric /* 22256836Seric ** HOST_MAP_INIT -- initialize host class structures 22356836Seric */ 22456836Seric 22568508Seric bool host_map_init __P((MAP *map, char *args)); 22668508Seric 22756836Seric bool 22860219Seric host_map_init(map, args) 22956836Seric MAP *map; 23056836Seric char *args; 23156836Seric { 23256836Seric register char *p = args; 23356836Seric 23456836Seric for (;;) 23556836Seric { 23658050Seric while (isascii(*p) && isspace(*p)) 23756836Seric p++; 23856836Seric if (*p != '-') 23956836Seric break; 24056836Seric switch (*++p) 24156836Seric { 24256836Seric case 'a': 24356836Seric map->map_app = ++p; 24456836Seric break; 24556836Seric } 24658050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 24756836Seric p++; 24856836Seric if (*p != '\0') 24956836Seric *p++ = '\0'; 25056836Seric } 25156836Seric if (map->map_app != NULL) 25256836Seric map->map_app = newstr(map->map_app); 25356836Seric return TRUE; 25456836Seric } 25557402Seric /* 25657402Seric ** SETUPMAILERS -- initialize default mailers 25757402Seric */ 25856836Seric 25968508Seric void 26057402Seric setupmailers() 26157402Seric { 26257402Seric char buf[100]; 26368508Seric extern void makemailer(); 26457402Seric 26569793Seric strcpy(buf, "prog, P=/bin/sh, F=lsoD, T=DNS/RFC822/X-Unix, A=sh -c \201u"); 26657403Seric makemailer(buf); 26757403Seric 26869721Seric strcpy(buf, "*file*, P=[FILE], F=lsDFMPEou, T=DNS/RFC822/X-Unix, A=FILE"); 26957402Seric makemailer(buf); 27057402Seric 27157402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 27257402Seric makemailer(buf); 27357402Seric } 27456836Seric /* 27560207Seric ** SETUPMAPS -- set up map classes 27660207Seric */ 27760207Seric 27860207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 27960207Seric { \ 28060207Seric extern bool parse __P((MAP *, char *)); \ 28160207Seric extern bool open __P((MAP *, int)); \ 28260207Seric extern void close __P((MAP *)); \ 28360207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 28460207Seric extern void store __P((MAP *, char *, char *)); \ 28560207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 28660207Seric s->s_mapclass.map_cname = name; \ 28760207Seric s->s_mapclass.map_ext = ext; \ 28860207Seric s->s_mapclass.map_cflags = flags; \ 28960207Seric s->s_mapclass.map_parse = parse; \ 29060207Seric s->s_mapclass.map_open = open; \ 29160207Seric s->s_mapclass.map_close = close; \ 29260207Seric s->s_mapclass.map_lookup = lookup; \ 29360207Seric s->s_mapclass.map_store = store; \ 29460207Seric } 29560207Seric 29668508Seric void 29760207Seric setupmaps() 29860207Seric { 29960207Seric register STAB *s; 30060207Seric 30160207Seric #ifdef NEWDB 30260207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 30360207Seric map_parseargs, hash_map_open, db_map_close, 30460207Seric db_map_lookup, db_map_store); 30568508Seric 30660207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 30760207Seric map_parseargs, bt_map_open, db_map_close, 30860207Seric db_map_lookup, db_map_store); 30960207Seric #endif 31060207Seric 31160207Seric #ifdef NDBM 31260207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 31360207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 31460207Seric ndbm_map_lookup, ndbm_map_store); 31560207Seric #endif 31660207Seric 31760207Seric #ifdef NIS 31860207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 31968508Seric map_parseargs, nis_map_open, null_map_close, 32068508Seric nis_map_lookup, null_map_store); 32160207Seric #endif 32260207Seric 32368508Seric #ifdef NISPLUS 32468508Seric MAPDEF("nisplus", NULL, MCF_ALIASOK, 32568508Seric map_parseargs, nisplus_map_open, null_map_close, 32668508Seric nisplus_map_lookup, null_map_store); 32768508Seric #endif 32868508Seric 32968508Seric #ifdef HESIOD 33068508Seric MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY, 33168508Seric map_parseargs, null_map_open, null_map_close, 33268508Seric hes_map_lookup, null_map_store); 33368508Seric #endif 33468508Seric 33569881Seric #if NETINFO 33668508Seric MAPDEF("netinfo", NULL, MCF_ALIASOK, 33768508Seric map_parseargs, ni_map_open, null_map_close, 33868508Seric ni_map_lookup, null_map_store); 33968508Seric #endif 34068508Seric 34168508Seric #if 0 34268508Seric MAPDEF("dns", NULL, 0, 34368508Seric dns_map_init, null_map_open, null_map_close, 34468508Seric dns_map_lookup, null_map_store); 34568508Seric #endif 34668508Seric 34768508Seric #if NAMED_BIND 34868508Seric /* best MX DNS lookup */ 34968508Seric MAPDEF("bestmx", NULL, MCF_OPTFILE, 35068508Seric map_parseargs, null_map_open, null_map_close, 35168508Seric bestmx_map_lookup, null_map_store); 35268508Seric #endif 35368508Seric 35468508Seric MAPDEF("host", NULL, 0, 35568508Seric host_map_init, null_map_open, null_map_close, 35668508Seric host_map_lookup, null_map_store); 35768508Seric 35868508Seric MAPDEF("text", NULL, MCF_ALIASOK, 35968508Seric map_parseargs, text_map_open, null_map_close, 36068508Seric text_map_lookup, null_map_store); 36168508Seric 36260207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 36368508Seric map_parseargs, stab_map_open, null_map_close, 36460207Seric stab_map_lookup, stab_map_store); 36560207Seric 36660207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 36760207Seric map_parseargs, impl_map_open, impl_map_close, 36860207Seric impl_map_lookup, impl_map_store); 36960207Seric 37068508Seric /* access to system passwd file */ 37168508Seric MAPDEF("user", NULL, MCF_OPTFILE, 37268508Seric map_parseargs, user_map_open, null_map_close, 37368508Seric user_map_lookup, null_map_store); 37460207Seric 37560207Seric /* dequote map */ 37660207Seric MAPDEF("dequote", NULL, 0, 37760207Seric dequote_init, null_map_open, null_map_close, 37860207Seric dequote_map, null_map_store); 37960207Seric 38069717Seric #if USERDB 38160207Seric /* user database */ 38268521Seric MAPDEF("userdb", ".db", 0, 38368521Seric map_parseargs, null_map_open, null_map_close, 38460207Seric udb_map_lookup, null_map_store); 38560207Seric #endif 38668508Seric 38769453Seric /* arbitrary programs */ 38869499Seric MAPDEF("program", NULL, MCF_ALIASOK, 38969453Seric map_parseargs, null_map_open, null_map_close, 39069453Seric prog_map_lookup, null_map_store); 39169453Seric 39268508Seric /* sequenced maps */ 39368508Seric MAPDEF("sequence", NULL, MCF_ALIASOK, 39468508Seric seq_map_parse, null_map_open, null_map_close, 39568508Seric seq_map_lookup, seq_map_store); 39668508Seric 39768508Seric /* switched interface to sequenced maps */ 39868508Seric MAPDEF("switch", NULL, MCF_ALIASOK, 39968508Seric map_parseargs, switch_map_open, null_map_close, 40068508Seric seq_map_lookup, seq_map_store); 40160207Seric } 40260207Seric 40360207Seric #undef MAPDEF 40460207Seric /* 40568508Seric ** INITHOSTMAPS -- initial host-dependent maps 40668508Seric ** 40768508Seric ** This should act as an interface to any local service switch 40868508Seric ** provided by the host operating system. 40968508Seric ** 41068508Seric ** Parameters: 41168508Seric ** none 41268508Seric ** 41368508Seric ** Returns: 41468508Seric ** none 41568508Seric ** 41668508Seric ** Side Effects: 41768508Seric ** Should define maps "host" and "users" as necessary 41868508Seric ** for this OS. If they are not defined, they will get 41968508Seric ** a default value later. It should check to make sure 42068508Seric ** they are not defined first, since it's possible that 42168508Seric ** the config file has provided an override. 42268508Seric */ 42368508Seric 42468508Seric void 42568508Seric inithostmaps() 42668508Seric { 42768508Seric register int i; 42868508Seric int nmaps; 42968508Seric char *maptype[MAXMAPSTACK]; 43068508Seric short mapreturn[MAXMAPACTIONS]; 43168508Seric char buf[MAXLINE]; 43268508Seric 43368508Seric /* 43468508Seric ** Set up default hosts maps. 43568508Seric */ 43668508Seric 43768508Seric #if 0 43868508Seric nmaps = switch_map_find("hosts", maptype, mapreturn); 43968508Seric for (i = 0; i < nmaps; i++) 44068508Seric { 44168508Seric if (strcmp(maptype[i], "files") == 0 && 44268508Seric stab("hosts.files", ST_MAP, ST_FIND) == NULL) 44368508Seric { 44468508Seric strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts"); 44569774Seric (void) makemapentry(buf); 44668508Seric } 44768508Seric #if NAMED_BIND 44868508Seric else if (strcmp(maptype[i], "dns") == 0 && 44968508Seric stab("hosts.dns", ST_MAP, ST_FIND) == NULL) 45068508Seric { 45168508Seric strcpy(buf, "hosts.dns dns A"); 45269774Seric (void) makemapentry(buf); 45368508Seric } 45468508Seric #endif 45568508Seric #ifdef NISPLUS 45668508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 45768508Seric stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL) 45868508Seric { 45968508Seric strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir"); 46069774Seric (void) makemapentry(buf); 46168508Seric } 46268508Seric #endif 46368508Seric #ifdef NIS 46468508Seric else if (strcmp(maptype[i], "nis") == 0 && 46568508Seric stab("hosts.nis", ST_MAP, ST_FIND) == NULL) 46668508Seric { 46768508Seric strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname"); 46869774Seric (void) makemapentry(buf); 46968508Seric } 47068508Seric #endif 47168508Seric } 47268508Seric #endif 47368508Seric 47468508Seric /* 47568508Seric ** Make sure we have a host map. 47668508Seric */ 47768508Seric 47868508Seric if (stab("host", ST_MAP, ST_FIND) == NULL) 47968508Seric { 48068508Seric /* user didn't initialize: set up host map */ 48168508Seric strcpy(buf, "host host"); 48268508Seric #if NAMED_BIND 48368508Seric if (ConfigLevel >= 2) 48468508Seric strcat(buf, " -a."); 48568508Seric #endif 48669774Seric (void) makemapentry(buf); 48768508Seric } 48868508Seric 48968508Seric /* 49068508Seric ** Set up default aliases maps 49168508Seric */ 49268508Seric 49368508Seric nmaps = switch_map_find("aliases", maptype, mapreturn); 49468508Seric for (i = 0; i < nmaps; i++) 49568508Seric { 49668508Seric if (strcmp(maptype[i], "files") == 0 && 49768508Seric stab("aliases.files", ST_MAP, ST_FIND) == NULL) 49868508Seric { 49968508Seric strcpy(buf, "aliases.files implicit /etc/aliases"); 50069774Seric (void) makemapentry(buf); 50168508Seric } 50268508Seric #ifdef NISPLUS 50368508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 50468508Seric stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL) 50568508Seric { 50668508Seric strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir"); 50769774Seric (void) makemapentry(buf); 50868508Seric } 50968508Seric #endif 51068508Seric #ifdef NIS 51168508Seric else if (strcmp(maptype[i], "nis") == 0 && 51268508Seric stab("aliases.nis", ST_MAP, ST_FIND) == NULL) 51368508Seric { 51468508Seric strcpy(buf, "aliases.nis nis -d mail.aliases"); 51569774Seric (void) makemapentry(buf); 51668508Seric } 51768508Seric #endif 51869774Seric #ifdef HESIOD 51969774Seric else if (strcmp(maptype[i], "hesiod") == 0 && 52069774Seric stab("aliases.hesiod", ST_MAP, ST_FIND) == NULL) 52169774Seric { 52269774Seric strcpy(buf, "aliases.hesiod hesiod aliases"); 52369774Seric (void) makemapentry(buf); 52469774Seric } 52569774Seric #endif 52668508Seric } 52769774Seric if (stab("aliases", ST_MAP, ST_FIND) == NULL) 52868508Seric { 52968508Seric strcpy(buf, "aliases switch aliases"); 53069774Seric (void) makemapentry(buf); 53168508Seric } 53268508Seric 53368508Seric #if 0 /* "user" map class is a better choice */ 53468508Seric /* 53568508Seric ** Set up default users maps. 53668508Seric */ 53768508Seric 53868508Seric nmaps = switch_map_find("passwd", maptype, mapreturn); 53968508Seric for (i = 0; i < nmaps; i++) 54068508Seric { 54168508Seric if (strcmp(maptype[i], "files") == 0 && 54268508Seric stab("users.files", ST_MAP, ST_FIND) == NULL) 54368508Seric { 54468508Seric strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd"); 54569774Seric (void) makemapentry(buf); 54668508Seric } 54768508Seric #ifdef NISPLUS 54868508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 54968508Seric stab("users.nisplus", ST_MAP, ST_FIND) == NULL) 55068508Seric { 55168508Seric strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir"); 55269774Seric (void) makemapentry(buf); 55368508Seric } 55468508Seric #endif 55568508Seric #ifdef NIS 55668508Seric else if (strcmp(maptype[i], "nis") == 0 && 55768508Seric stab("users.nis", ST_MAP, ST_FIND) == NULL) 55868508Seric { 55968508Seric strcpy(buf, "users.nis nis -m -d passwd.byname"); 56069774Seric (void) makemapentry(buf); 56168508Seric } 56268508Seric #endif 56368508Seric #ifdef HESIOD 56468508Seric else if (strcmp(maptype[i], "hesiod") == 0) && 56568508Seric stab("users.hesiod", ST_MAP, ST_FIND) == NULL) 56668508Seric { 56768508Seric strcpy(buf, "users.hesiod hesiod"); 56869774Seric (void) makemapentry(buf); 56968508Seric } 57068508Seric #endif 57168508Seric } 57268508Seric if (stab("users", ST_MAP, ST_FIND) == NULL) 57368508Seric { 57468508Seric strcpy(buf, "users switch -m passwd"); 57569774Seric (void) makemapentry(buf); 57668508Seric } 57768508Seric #endif 57868508Seric } 57968508Seric /* 58068508Seric ** SWITCH_MAP_FIND -- find the list of types associated with a map 58168508Seric ** 58268508Seric ** This is the system-dependent interface to the service switch. 58368508Seric ** 58468508Seric ** Parameters: 58568508Seric ** service -- the name of the service of interest. 58668508Seric ** maptype -- an out-array of strings containing the types 58768508Seric ** of access to use for this service. There can 58868508Seric ** be at most MAXMAPSTACK types for a single service. 58968508Seric ** mapreturn -- an out-array of return information bitmaps 59068508Seric ** for the map. 59168508Seric ** 59268508Seric ** Returns: 59368508Seric ** The number of map types filled in, or -1 for failure. 59468508Seric */ 59568508Seric 59668508Seric #ifdef SOLARIS 59768508Seric # include <nsswitch.h> 59868508Seric #endif 59968508Seric 60069815Seric #if defined(ultrix) || (defined(__osf__) && defined(__alpha)) 60169815Seric # define _USE_DEC_SVC_CONF_ 60269815Seric #endif 60369815Seric 60469815Seric #ifdef _USE_DEC_SVC_CONF_ 60568508Seric # include <sys/svcinfo.h> 60668508Seric #endif 60768508Seric 60868508Seric int 60968508Seric switch_map_find(service, maptype, mapreturn) 61068508Seric char *service; 61168508Seric char *maptype[MAXMAPSTACK]; 61268508Seric short mapreturn[MAXMAPACTIONS]; 61368508Seric { 61468508Seric register FILE *fp; 61568508Seric int svcno; 61668508Seric static char buf[MAXLINE]; 61768508Seric 61868508Seric #ifdef SOLARIS 61968508Seric struct __nsw_switchconfig *nsw_conf; 62068508Seric enum __nsw_parse_err pserr; 62168508Seric struct __nsw_lookup *lk; 62268508Seric int nsw_rc; 62368508Seric static struct __nsw_lookup lkp0 = 62468508Seric { "files", {1, 0, 0, 0}, NULL, NULL }; 62568508Seric static struct __nsw_switchconfig lkp_default = 62668508Seric { 0, "sendmail", 3, &lkp0 }; 62768508Seric 62868508Seric if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL) 62968508Seric lk = lkp_default.lookups; 63068508Seric else 63168508Seric lk = nsw_conf->lookups; 63268508Seric svcno = 0; 63368508Seric while (lk != NULL) 63468508Seric { 63568508Seric maptype[svcno] = lk->service_name; 63668508Seric if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN) 63768508Seric mapreturn[MA_NOTFOUND] |= 1 << svcno; 63868508Seric if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN) 63968508Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 64068508Seric if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN) 64168508Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 64268508Seric svcno++; 64368508Seric lk = lk->next; 64468508Seric } 64568508Seric return svcno; 64668508Seric #endif 64768508Seric 64869815Seric #ifdef _USE_DEC_SVC_CONF_ 64968508Seric struct svcinfo *svcinfo; 65068508Seric int svc; 65168508Seric 65269401Seric for (svcno = 0; svcno < MAXMAPACTIONS; svcno++) 65369401Seric mapreturn[svcno] = 0; 65469401Seric 65568508Seric svcinfo = getsvc(); 65668508Seric if (svcinfo == NULL) 65768508Seric goto punt; 65868508Seric if (strcmp(service, "hosts") == 0) 65968508Seric svc = SVC_HOSTS; 66068508Seric else if (strcmp(service, "aliases") == 0) 66168508Seric svc = SVC_ALIASES; 66268508Seric else if (strcmp(service, "passwd") == 0) 66368508Seric svc = SVC_PASSWD; 66468508Seric else 66568508Seric return -1; 66668508Seric for (svcno = 0; svcno < SVC_PATHSIZE; svcno++) 66768508Seric { 66868508Seric switch (svcinfo->svcpath[svc][svcno]) 66968508Seric { 67068508Seric case SVC_LOCAL: 67168508Seric maptype[svcno] = "files"; 67268508Seric break; 67368508Seric 67468508Seric case SVC_YP: 67568508Seric maptype[svcno] = "nis"; 67668508Seric break; 67768508Seric 67868508Seric case SVC_BIND: 67968508Seric maptype[svcno] = "dns"; 68068508Seric break; 68168508Seric 68268508Seric #ifdef SVC_HESIOD 68368508Seric case SVC_HESIOD: 68468508Seric maptype[svcno] = "hesiod"; 68568508Seric break; 68668508Seric #endif 68768508Seric 68868508Seric case SVC_LAST: 68968508Seric return svcno; 69068508Seric } 69168508Seric } 69268508Seric return svcno; 69368508Seric #endif 69468508Seric 69569815Seric #if !defined(SOLARIS) && !defined(_USE_DEC_SVC_CONF_) 69668508Seric /* 69768508Seric ** Fall-back mechanism. 69868508Seric */ 69968508Seric 70069401Seric for (svcno = 0; svcno < MAXMAPACTIONS; svcno++) 70169401Seric mapreturn[svcno] = 0; 70269401Seric 70368508Seric svcno = 0; 70468508Seric fp = fopen(ServiceSwitchFile, "r"); 70568508Seric if (fp != NULL) 70668508Seric { 70768508Seric while (fgets(buf, sizeof buf, fp) != NULL) 70868508Seric { 70968508Seric register char *p; 71068508Seric 71168508Seric p = strpbrk(buf, "#\n"); 71268508Seric if (p != NULL) 71368508Seric *p = '\0'; 71468508Seric p = strpbrk(buf, " \t"); 71568508Seric if (p != NULL) 71668508Seric *p++ = '\0'; 71768508Seric if (strcmp(buf, service) != 0) 71868508Seric continue; 71968508Seric 72068508Seric /* got the right service -- extract data */ 72168508Seric do 72268508Seric { 72368508Seric while (isspace(*p)) 72468508Seric p++; 72568508Seric if (*p == '\0') 72668508Seric break; 72768508Seric maptype[svcno++] = p; 72868508Seric p = strpbrk(p, " \t"); 72968508Seric if (p != NULL) 73068508Seric *p++ = '\0'; 73168508Seric } while (p != NULL); 73269830Seric fclose(fp); 73369830Seric return svcno; 73468508Seric } 73569830Seric 73669830Seric /* service was not found -- use compiled in default */ 73768508Seric fclose(fp); 73868508Seric } 73968508Seric #endif 74068508Seric 74168508Seric /* if the service file doesn't work, use an absolute fallback */ 74268508Seric punt: 74369401Seric for (svcno = 0; svcno < MAXMAPACTIONS; svcno++) 74469401Seric mapreturn[svcno] = 0; 74569401Seric svcno = 0; 74668508Seric if (strcmp(service, "aliases") == 0) 74768508Seric { 74868628Seric maptype[svcno++] = "files"; 74968628Seric #ifdef AUTO_NIS_ALIASES 75068628Seric # ifdef NISPLUS 75168628Seric maptype[svcno++] = "nisplus"; 75268628Seric # endif 75368628Seric # ifdef NIS 75468628Seric maptype[svcno++] = "nis"; 75568628Seric # endif 75668628Seric #endif 75768628Seric return svcno; 75868508Seric } 75968508Seric if (strcmp(service, "hosts") == 0) 76068508Seric { 76168508Seric # if NAMED_BIND 76268508Seric maptype[svcno++] = "dns"; 76368508Seric # else 76468508Seric # if defined(sun) && !defined(BSD) && !defined(SOLARIS) 76568508Seric /* SunOS */ 76668508Seric maptype[svcno++] = "nis"; 76768508Seric # endif 76868508Seric # endif 76968508Seric maptype[svcno++] = "files"; 77068508Seric return svcno; 77168508Seric } 77268508Seric return -1; 77368508Seric } 77468508Seric /* 7759369Seric ** USERNAME -- return the user id of the logged in user. 7769369Seric ** 7779369Seric ** Parameters: 7789369Seric ** none. 7799369Seric ** 7809369Seric ** Returns: 7819369Seric ** The login name of the logged in user. 7829369Seric ** 7839369Seric ** Side Effects: 7849369Seric ** none. 7859369Seric ** 7869369Seric ** Notes: 7879369Seric ** The return value is statically allocated. 7889369Seric */ 7899369Seric 7909369Seric char * 7919369Seric username() 7929369Seric { 79317469Seric static char *myname = NULL; 7949369Seric extern char *getlogin(); 79519904Smiriam register struct passwd *pw; 7969369Seric 79717469Seric /* cache the result */ 79817469Seric if (myname == NULL) 79917469Seric { 80017469Seric myname = getlogin(); 80117469Seric if (myname == NULL || myname[0] == '\0') 80217469Seric { 80368693Seric pw = sm_getpwuid(RealUid); 80417469Seric if (pw != NULL) 80540993Sbostic myname = newstr(pw->pw_name); 80617469Seric } 80719904Smiriam else 80819904Smiriam { 80963787Seric uid_t uid = RealUid; 81019873Smiriam 81140993Sbostic myname = newstr(myname); 81268693Seric if ((pw = sm_getpwnam(myname)) == NULL || 81358736Seric (uid != 0 && uid != pw->pw_uid)) 81419904Smiriam { 81568693Seric pw = sm_getpwuid(uid); 81624945Seric if (pw != NULL) 81740993Sbostic myname = newstr(pw->pw_name); 81819873Smiriam } 81919873Smiriam } 82017469Seric if (myname == NULL || myname[0] == '\0') 82117469Seric { 82258151Seric syserr("554 Who are you?"); 82317469Seric myname = "postmaster"; 82417469Seric } 82517469Seric } 82617469Seric 82717469Seric return (myname); 8289369Seric } 8299369Seric /* 8304190Seric ** TTYPATH -- Get the path of the user's tty 831294Seric ** 832294Seric ** Returns the pathname of the user's tty. Returns NULL if 833294Seric ** the user is not logged in or if s/he has write permission 834294Seric ** denied. 835294Seric ** 836294Seric ** Parameters: 837294Seric ** none 838294Seric ** 839294Seric ** Returns: 840294Seric ** pathname of the user's tty. 841294Seric ** NULL if not logged in or write permission denied. 842294Seric ** 843294Seric ** Side Effects: 844294Seric ** none. 845294Seric ** 846294Seric ** WARNING: 847294Seric ** Return value is in a local buffer. 848294Seric ** 849294Seric ** Called By: 850294Seric ** savemail 851294Seric */ 852294Seric 853294Seric char * 854294Seric ttypath() 855294Seric { 856294Seric struct stat stbuf; 857294Seric register char *pathn; 858294Seric extern char *ttyname(); 8594081Seric extern char *getlogin(); 860294Seric 861294Seric /* compute the pathname of the controlling tty */ 8629369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 8639369Seric (pathn = ttyname(0)) == NULL) 864294Seric { 865294Seric errno = 0; 866294Seric return (NULL); 867294Seric } 868294Seric 869294Seric /* see if we have write permission */ 8702967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 871294Seric { 872294Seric errno = 0; 873294Seric return (NULL); 874294Seric } 875294Seric 876294Seric /* see if the user is logged in */ 877294Seric if (getlogin() == NULL) 878294Seric return (NULL); 879294Seric 880294Seric /* looks good */ 881294Seric return (pathn); 882294Seric } 8832967Seric /* 8842967Seric ** CHECKCOMPAT -- check for From and To person compatible. 8852967Seric ** 8862967Seric ** This routine can be supplied on a per-installation basis 8872967Seric ** to determine whether a person is allowed to send a message. 8882967Seric ** This allows restriction of certain types of internet 8892967Seric ** forwarding or registration of users. 8902967Seric ** 8912967Seric ** If the hosts are found to be incompatible, an error 89268857Seric ** message should be given using "usrerr" and an EX_ code 89368857Seric ** should be returned. You can also set to->q_status to 89468857Seric ** a DSN-style status code. 8952967Seric ** 89668559Seric ** EF_NO_BODY_RETN can be set in e->e_flags to suppress the 89768559Seric ** body during the return-to-sender function; this should be done 89868559Seric ** on huge messages. This bit may already be set by the ESMTP 89968559Seric ** protocol. 9004288Seric ** 9012967Seric ** Parameters: 9022967Seric ** to -- the person being sent to. 9032967Seric ** 9042967Seric ** Returns: 90557459Seric ** an exit status 9062967Seric ** 9072967Seric ** Side Effects: 9082967Seric ** none (unless you include the usrerr stuff) 9092967Seric */ 9102967Seric 91168508Seric int 91255012Seric checkcompat(to, e) 9132967Seric register ADDRESS *to; 91455012Seric register ENVELOPE *e; 9152967Seric { 91612133Seric # ifdef lint 91712133Seric if (to == NULL) 91812133Seric to++; 91963834Seric # endif /* lint */ 92064402Seric 92164402Seric if (tTd(49, 1)) 92264402Seric printf("checkcompat(to=%s, from=%s)\n", 92364402Seric to->q_paddr, e->e_from.q_paddr); 92464402Seric 92510698Seric # ifdef EXAMPLE_CODE 92610698Seric /* this code is intended as an example only */ 9274437Seric register STAB *s; 9284437Seric 9294437Seric s = stab("arpa", ST_MAILER, ST_FIND); 93068508Seric if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 && 9319369Seric to->q_mailer == s->s_mailer) 9324437Seric { 93358151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 93468559Seric /* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */ 93568857Seric to->q_status = "5.7.1"; 93657459Seric return (EX_UNAVAILABLE); 9374437Seric } 93856795Seric # endif /* EXAMPLE_CODE */ 93957459Seric return (EX_OK); 9402967Seric } 9419369Seric /* 94264035Seric ** SETSIGNAL -- set a signal handler 94364035Seric ** 94464035Seric ** This is essentially old BSD "signal(3)". 94564035Seric */ 94664035Seric 94764561Seric sigfunc_t 94864035Seric setsignal(sig, handler) 94964035Seric int sig; 95064561Seric sigfunc_t handler; 95164035Seric { 95269892Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) 95364035Seric return signal(sig, handler); 95464035Seric #else 95564035Seric struct sigaction n, o; 95664035Seric 95764035Seric bzero(&n, sizeof n); 95864035Seric n.sa_handler = handler; 95968508Seric # ifdef SA_RESTART 96068508Seric n.sa_flags = SA_RESTART; 96168508Seric # endif 96264035Seric if (sigaction(sig, &n, &o) < 0) 96364035Seric return SIG_ERR; 96464035Seric return o.sa_handler; 96564035Seric #endif 96664035Seric } 96764035Seric /* 9689369Seric ** HOLDSIGS -- arrange to hold all signals 9699369Seric ** 9709369Seric ** Parameters: 9719369Seric ** none. 9729369Seric ** 9739369Seric ** Returns: 9749369Seric ** none. 9759369Seric ** 9769369Seric ** Side Effects: 9779369Seric ** Arranges that signals are held. 9789369Seric */ 9799369Seric 98068508Seric void 9819369Seric holdsigs() 9829369Seric { 9839369Seric } 9849369Seric /* 9859369Seric ** RLSESIGS -- arrange to release all signals 9869369Seric ** 9879369Seric ** This undoes the effect of holdsigs. 9889369Seric ** 9899369Seric ** Parameters: 9909369Seric ** none. 9919369Seric ** 9929369Seric ** Returns: 9939369Seric ** none. 9949369Seric ** 9959369Seric ** Side Effects: 9969369Seric ** Arranges that signals are released. 9979369Seric */ 9989369Seric 99968508Seric void 10009369Seric rlsesigs() 10019369Seric { 10029369Seric } 100314872Seric /* 100464705Seric ** INIT_MD -- do machine dependent initializations 100564705Seric ** 100664705Seric ** Systems that have global modes that should be set should do 100764705Seric ** them here rather than in main. 100864705Seric */ 100964705Seric 101064705Seric #ifdef _AUX_SOURCE 101164705Seric # include <compat.h> 101264705Seric #endif 101364705Seric 101468508Seric void 101564825Seric init_md(argc, argv) 101664825Seric int argc; 101764825Seric char **argv; 101864705Seric { 101964705Seric #ifdef _AUX_SOURCE 102064705Seric setcompat(getcompat() | COMPAT_BSDPROT); 102164705Seric #endif 102268508Seric 102368508Seric #ifdef VENDOR_DEFAULT 102468508Seric VendorCode = VENDOR_DEFAULT; 102568508Seric #else 102668508Seric VendorCode = VENDOR_BERKELEY; 102768508Seric #endif 102864705Seric } 102964705Seric /* 103068508Seric ** INIT_VENDOR_MACROS -- vendor-dependent macro initializations 103168508Seric ** 103268508Seric ** Called once, on startup. 103368508Seric ** 103468508Seric ** Parameters: 103568508Seric ** e -- the global envelope. 103668508Seric ** 103768508Seric ** Returns: 103868508Seric ** none. 103968508Seric ** 104068508Seric ** Side Effects: 104168508Seric ** vendor-dependent. 104268508Seric */ 104368508Seric 104468508Seric void 104568508Seric init_vendor_macros(e) 104668508Seric register ENVELOPE *e; 104768508Seric { 104868508Seric } 104968508Seric /* 105014872Seric ** GETLA -- get the current load average 105114872Seric ** 105214881Seric ** This code stolen from la.c. 105314881Seric ** 105414872Seric ** Parameters: 105514872Seric ** none. 105614872Seric ** 105714872Seric ** Returns: 105814872Seric ** The current load average as an integer. 105914872Seric ** 106014872Seric ** Side Effects: 106114872Seric ** none. 106214872Seric */ 106314872Seric 106451920Seric /* try to guess what style of load average we have */ 106551920Seric #define LA_ZERO 1 /* always return load average as zero */ 106664360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 106751920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 106851920Seric #define LA_SUBR 4 /* call getloadavg */ 106964295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 107064360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 107166301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 107269462Seric #define LA_READKSYM 8 /* SVR4: use MIOC_READKSYM ioctl call */ 107369462Seric #define LA_DGUX 9 /* special DGUX implementation */ 107469462Seric #define LA_HPUX 10 /* special HPUX implementation */ 107514872Seric 107663962Seric /* do guesses based on general OS type */ 107751920Seric #ifndef LA_TYPE 107863962Seric # define LA_TYPE LA_ZERO 107951920Seric #endif 108051920Seric 108169462Seric #ifndef FSHIFT 108269462Seric # if defined(unixpc) 108369462Seric # define FSHIFT 5 108469462Seric # endif 108569462Seric 108669462Seric # if defined(__alpha) || defined(IRIX) 108769462Seric # define FSHIFT 10 108869462Seric # endif 108969462Seric 109069462Seric # if defined(_AIX3) 109169462Seric # define FSHIFT 16 109269462Seric # endif 109369462Seric #endif 109469462Seric 109569462Seric #ifndef FSHIFT 109669462Seric # define FSHIFT 8 109769462Seric #endif 109869462Seric 109969462Seric #ifndef FSCALE 110069462Seric # define FSCALE (1 << FSHIFT) 110169462Seric #endif 110269462Seric 110369654Seric #ifndef LA_AVENRUN 110469654Seric # ifdef SYSTEM5 110569654Seric # define LA_AVENRUN "avenrun" 110669654Seric # else 110769654Seric # define LA_AVENRUN "_avenrun" 110869654Seric # endif 110969654Seric #endif 111069654Seric 111164360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 111251920Seric 111314872Seric #include <nlist.h> 111414872Seric 111568508Seric #ifdef IRIX64 111668508Seric # define nlist nlist64 111768508Seric #endif 111868508Seric 111951920Seric /* _PATH_UNIX should be defined in <paths.h> */ 112051920Seric #ifndef _PATH_UNIX 112163962Seric # if defined(SYSTEM5) 112263962Seric # define _PATH_UNIX "/unix" 112363962Seric # else 112463962Seric # define _PATH_UNIX "/vmunix" 112563962Seric # endif 112651920Seric #endif 112751920Seric 112869892Seric #ifdef _AUX_SOURCE 112969892Seric struct nlist Nl[2]; 113069892Seric #else 113169462Seric struct nlist Nl[] = 113214872Seric { 113351920Seric { LA_AVENRUN }, 113414872Seric { 0 }, 113514872Seric }; 113669892Seric #endif 113769892Seric #define X_AVENRUN 0 113814872Seric 113914872Seric getla() 114014872Seric { 114114872Seric static int kmem = -1; 114251920Seric #if LA_TYPE == LA_INT 114324943Seric long avenrun[3]; 114451920Seric #else 114564360Seric # if LA_TYPE == LA_SHORT 114664360Seric short avenrun[3]; 114764360Seric # else 114851920Seric double avenrun[3]; 114964360Seric # endif 115051920Seric #endif 115169462Seric extern int errno; 115225615Seric extern off_t lseek(); 115314872Seric 115414872Seric if (kmem < 0) 115514872Seric { 115624945Seric kmem = open("/dev/kmem", 0, 0); 115714872Seric if (kmem < 0) 115857736Seric { 115957736Seric if (tTd(3, 1)) 116057736Seric printf("getla: open(/dev/kmem): %s\n", 116157736Seric errstring(errno)); 116214872Seric return (-1); 116357736Seric } 116451920Seric (void) fcntl(kmem, F_SETFD, 1); 116569462Seric 116669892Seric #ifdef _AUX_SOURCE 116769892Seric strcpy(Nl[X_AVENRUN].n_name, LA_AVENRUN); 116869892Seric #endif 116969892Seric 117068508Seric #ifdef _AIX3 117168508Seric if (knlist(Nl, 1, sizeof Nl[0]) < 0) 117268508Seric #else 117357736Seric if (nlist(_PATH_UNIX, Nl) < 0) 117468508Seric #endif 117557736Seric { 117657736Seric if (tTd(3, 1)) 117757736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 117857736Seric errstring(errno)); 117914872Seric return (-1); 118057736Seric } 118159253Seric if (Nl[X_AVENRUN].n_value == 0) 118259253Seric { 118359253Seric if (tTd(3, 1)) 118459253Seric printf("getla: nlist(%s, %s) ==> 0\n", 118559253Seric _PATH_UNIX, LA_AVENRUN); 118659253Seric return (-1); 118759253Seric } 118868508Seric #ifdef NAMELISTMASK 118968508Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 119066763Seric #endif 119114872Seric } 119257736Seric if (tTd(3, 20)) 119357736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 119468508Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 || 119523118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 119619967Seric { 119719967Seric /* thank you Ian */ 119857736Seric if (tTd(3, 1)) 119957736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 120019967Seric return (-1); 120119967Seric } 120269462Seric # if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 120357736Seric if (tTd(3, 5)) 120457736Seric { 120557736Seric printf("getla: avenrun = %d", avenrun[0]); 120657736Seric if (tTd(3, 15)) 120757736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 120857736Seric printf("\n"); 120957736Seric } 121057736Seric if (tTd(3, 1)) 121157736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 121224943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 121369462Seric # else /* LA_TYPE == LA_FLOAT */ 121457736Seric if (tTd(3, 5)) 121557736Seric { 121657736Seric printf("getla: avenrun = %g", avenrun[0]); 121757736Seric if (tTd(3, 15)) 121857736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 121957736Seric printf("\n"); 122057736Seric } 122157736Seric if (tTd(3, 1)) 122257736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 122351920Seric return ((int) (avenrun[0] + 0.5)); 122469462Seric # endif 122514872Seric } 122614872Seric 122769462Seric #endif /* LA_TYPE == LA_INT or LA_SHORT or LA_FLOAT */ 122851773Seric 122969462Seric #if LA_TYPE == LA_READKSYM 123064718Seric 123169473Seric # include <sys/ksym.h> 123269473Seric 123369462Seric getla() 123469462Seric { 123569462Seric static int kmem = -1; 123669462Seric long avenrun[3]; 123769462Seric extern int errno; 123869462Seric struct mioc_rksym mirk; 123964718Seric 124069462Seric if (kmem < 0) 124169462Seric { 124269462Seric kmem = open("/dev/kmem", 0, 0); 124369462Seric if (kmem < 0) 124469462Seric { 124569462Seric if (tTd(3, 1)) 124669462Seric printf("getla: open(/dev/kmem): %s\n", 124769462Seric errstring(errno)); 124869462Seric return (-1); 124969462Seric } 125069462Seric (void) fcntl(kmem, F_SETFD, 1); 125169462Seric } 125269462Seric mirk.mirk_symname = LA_AVENRUN; 125369462Seric mirk.mirk_buf = avenrun; 125469462Seric mirk.mirk_buflen = sizeof(avenrun); 125569462Seric if (ioctl(kmem, MIOC_READKSYM, &mirk) < 0) 125669462Seric { 125769462Seric if (tTd(3, 1)) 125869462Seric printf("getla: ioctl(MIOC_READKSYM) failed: %s\n", 125969462Seric errstring(errno)); 126069462Seric return -1; 126169462Seric } 126269462Seric if (tTd(3, 5)) 126369462Seric { 126469462Seric printf("getla: avenrun = %d", avenrun[0]); 126569462Seric if (tTd(3, 15)) 126669462Seric printf(", %d, %d", avenrun[1], avenrun[2]); 126769462Seric printf("\n"); 126869462Seric } 126969462Seric if (tTd(3, 1)) 127069462Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 127169462Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 127269462Seric } 127369462Seric 127469462Seric #endif /* LA_TYPE == LA_READKSYM */ 127569462Seric 127669462Seric #if LA_TYPE == LA_DGUX 127769462Seric 127869462Seric # include <sys/dg_sys_info.h> 127969462Seric 128068508Seric int 128168508Seric getla() 128264718Seric { 128364718Seric struct dg_sys_info_load_info load_info; 128464718Seric 128564718Seric dg_sys_info((long *)&load_info, 128664718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 128764718Seric 128868508Seric if (tTd(3, 1)) 128968508Seric printf("getla: %d\n", (int) (load_info.one_minute + 0.5)); 129068508Seric 129164718Seric return((int) (load_info.one_minute + 0.5)); 129264718Seric } 129364718Seric 129469462Seric #endif /* LA_TYPE == LA_DGUX */ 129564718Seric 129669462Seric #if LA_TYPE == LA_HPUX 129769462Seric 129869563Seric /* forward declarations to keep gcc from complaining */ 129968869Seric struct pst_dynamic; 130069563Seric struct pst_status; 130169563Seric struct pst_static; 130269563Seric struct pst_vminfo; 130369563Seric struct pst_diskinfo; 130469563Seric struct pst_processor; 130569563Seric struct pst_lv; 130669563Seric struct pst_swapinfo; 130768869Seric 130869462Seric # include <sys/param.h> 130969462Seric # include <sys/pstat.h> 131068508Seric 131168508Seric int 131251773Seric getla() 131351773Seric { 131468508Seric struct pst_dynamic pstd; 131568508Seric 131668508Seric if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic), 131768869Seric (size_t) 1, 0) == -1) 131868508Seric return 0; 131968508Seric 132068508Seric if (tTd(3, 1)) 132168508Seric printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5)); 132268508Seric 132368508Seric return (int) (pstd.psd_avg_1_min + 0.5); 132468508Seric } 132568508Seric 132669462Seric #endif /* LA_TYPE == LA_HPUX */ 132768508Seric 132869462Seric #if LA_TYPE == LA_SUBR 132969462Seric 133068508Seric int 133168508Seric getla() 133268508Seric { 133351920Seric double avenrun[3]; 133451920Seric 133551920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 133657736Seric { 133757736Seric if (tTd(3, 1)) 133857736Seric perror("getla: getloadavg failed:"); 133951920Seric return (-1); 134057736Seric } 134157736Seric if (tTd(3, 1)) 134257736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 134351920Seric return ((int) (avenrun[0] + 0.5)); 134451773Seric } 134551773Seric 134669462Seric #endif /* LA_TYPE == LA_SUBR */ 134769462Seric 134864295Seric #if LA_TYPE == LA_MACH 134951773Seric 135064295Seric /* 135165173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 135264295Seric */ 135364295Seric 135465173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 135565173Seric # include <mach/mach.h> 135665173Seric #else 135765173Seric # include <mach.h> 135865173Seric #endif 135964295Seric 136051773Seric getla() 136151773Seric { 136264295Seric processor_set_t default_set; 136364295Seric kern_return_t error; 136464295Seric unsigned int info_count; 136564295Seric struct processor_set_basic_info info; 136664295Seric host_t host; 136764295Seric 136864295Seric error = processor_set_default(host_self(), &default_set); 136964295Seric if (error != KERN_SUCCESS) 137064295Seric return -1; 137164295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 137264295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 137364295Seric &host, (processor_set_info_t)&info, 137464295Seric &info_count) != KERN_SUCCESS) 137564295Seric { 137664295Seric return -1; 137764295Seric } 137864295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 137964295Seric } 138064295Seric 138169462Seric #endif /* LA_TYPE == LA_MACH */ 138264295Seric 138366313Seric #if LA_TYPE == LA_PROCSTR 138464295Seric 138566301Seric /* 138666301Seric ** Read /proc/loadavg for the load average. This is assumed to be 138766301Seric ** in a format like "0.15 0.12 0.06". 138866301Seric ** 138966301Seric ** Initially intended for Linux. This has been in the kernel 139066301Seric ** since at least 0.99.15. 139166301Seric */ 139266301Seric 139366301Seric # ifndef _PATH_LOADAVG 139466301Seric # define _PATH_LOADAVG "/proc/loadavg" 139566301Seric # endif 139666301Seric 139766301Seric int 139864295Seric getla() 139964295Seric { 140066319Seric double avenrun; 140166301Seric register int result; 140266319Seric FILE *fp; 140366301Seric 140466319Seric fp = fopen(_PATH_LOADAVG, "r"); 140569472Seric if (fp == NULL) 140666301Seric { 140766319Seric if (tTd(3, 1)) 140866319Seric printf("getla: fopen(%s): %s\n", 140966319Seric _PATH_LOADAVG, errstring(errno)); 141066319Seric return -1; 141166301Seric } 141266301Seric result = fscanf(fp, "%lf", &avenrun); 141366319Seric fclose(fp); 141466301Seric if (result != 1) 141566301Seric { 141666310Seric if (tTd(3, 1)) 141766301Seric printf("getla: fscanf() = %d: %s\n", 141866301Seric result, errstring(errno)); 141966301Seric return -1; 142066301Seric } 142166301Seric 142257736Seric if (tTd(3, 1)) 142366301Seric printf("getla(): %.2f\n", avenrun); 142466301Seric 142566301Seric return ((int) (avenrun + 0.5)); 142666301Seric } 142766301Seric 142869462Seric #endif /* LA_TYPE == LA_PROCSTR */ 142966301Seric 143069462Seric #if LA_TYPE == LA_ZERO 143169462Seric 143266301Seric getla() 143366301Seric { 143466301Seric if (tTd(3, 1)) 143557736Seric printf("getla: ZERO\n"); 143651920Seric return (0); 143751773Seric } 143851773Seric 143969462Seric #endif /* LA_TYPE == LA_ZERO */ 144066045Seric 144166045Seric 144266045Seric /* 144366045Seric * Copyright 1989 Massachusetts Institute of Technology 144466045Seric * 144566045Seric * Permission to use, copy, modify, distribute, and sell this software and its 144666045Seric * documentation for any purpose is hereby granted without fee, provided that 144766045Seric * the above copyright notice appear in all copies and that both that 144866045Seric * copyright notice and this permission notice appear in supporting 144966045Seric * documentation, and that the name of M.I.T. not be used in advertising or 145066045Seric * publicity pertaining to distribution of the software without specific, 145166045Seric * written prior permission. M.I.T. makes no representations about the 145266045Seric * suitability of this software for any purpose. It is provided "as is" 145366045Seric * without express or implied warranty. 145466045Seric * 145566045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 145666045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 145766045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 145866045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 145966045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 146066045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 146166045Seric * 146266045Seric * Authors: Many and varied... 146366045Seric */ 146466045Seric 146566045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 146666045Seric #ifndef lint 146766045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 146866045Seric #endif /* !lint */ 146966045Seric 147066179Seric #ifdef apollo 147166045Seric # undef volatile 147266045Seric # include <apollo/base.h> 147366045Seric 147466045Seric /* ARGSUSED */ 147566045Seric int getloadavg( call_data ) 147666045Seric caddr_t call_data; /* pointer to (double) return value */ 147766045Seric { 147866045Seric double *avenrun = (double *) call_data; 147966045Seric int i; 148066045Seric status_$t st; 148166045Seric long loadav[3]; 148266045Seric proc1_$get_loadav(loadav, &st); 148366045Seric *avenrun = loadav[0] / (double) (1 << 16); 148466045Seric return(0); 148566045Seric } 148666045Seric # endif /* apollo */ 148724943Seric /* 148824943Seric ** SHOULDQUEUE -- should this message be queued or sent? 148924943Seric ** 149024943Seric ** Compares the message cost to the load average to decide. 149124943Seric ** 149224943Seric ** Parameters: 149324943Seric ** pri -- the priority of the message in question. 149457438Seric ** ctime -- the message creation time. 149524943Seric ** 149624943Seric ** Returns: 149724943Seric ** TRUE -- if this message should be queued up for the 149824943Seric ** time being. 149924943Seric ** FALSE -- if the load is low enough to send this message. 150024943Seric ** 150124943Seric ** Side Effects: 150224943Seric ** none. 150324943Seric */ 150424943Seric 150524943Seric bool 150657438Seric shouldqueue(pri, ctime) 150724943Seric long pri; 150857438Seric time_t ctime; 150924943Seric { 151068508Seric bool rval; 151168508Seric 151268508Seric if (tTd(3, 30)) 151368508Seric printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri); 151451920Seric if (CurrentLA < QueueLA) 151568508Seric { 151668508Seric if (tTd(3, 30)) 151768508Seric printf("FALSE (CurrentLA < QueueLA)\n"); 151824943Seric return (FALSE); 151968508Seric } 152058132Seric if (CurrentLA >= RefuseLA) 152168508Seric { 152268508Seric if (tTd(3, 30)) 152368508Seric printf("TRUE (CurrentLA >= RefuseLA)\n"); 152458132Seric return (TRUE); 152568508Seric } 152668508Seric rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1)); 152768508Seric if (tTd(3, 30)) 152868508Seric printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE"); 152968508Seric return rval; 153024943Seric } 153124943Seric /* 153253037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 153353037Seric ** 153453037Seric ** Parameters: 153553037Seric ** none. 153653037Seric ** 153753037Seric ** Returns: 153853037Seric ** TRUE if incoming SMTP connections should be refused 153953037Seric ** (for now). 154053037Seric ** FALSE if we should accept new work. 154153037Seric ** 154253037Seric ** Side Effects: 154369832Seric ** Sets process title when it is rejecting connections. 154453037Seric */ 154553037Seric 154653037Seric bool 154753037Seric refuseconnections() 154853037Seric { 154969801Seric extern bool enoughdiskspace(); 155069847Seric extern void setproctitle __P((char *, ...)); 155168508Seric 155259156Seric #ifdef XLA 155359156Seric if (!xla_smtp_ok()) 155459156Seric return TRUE; 155559156Seric #endif 155659156Seric 155769832Seric if (CurrentLA >= RefuseLA) 155869832Seric { 155969832Seric setproctitle("rejecting connections: load average: %d", 156069832Seric CurrentLA); 156169832Seric } 156269832Seric else if (!enoughdiskspace(MinBlocksFree + 1)) 156369832Seric { 156469832Seric setproctitle("rejecting connections: min free: %d", 156569832Seric MinBlocksFree); 156669832Seric } 156769832Seric else if (MaxChildren > 0 && CurChildren >= MaxChildren) 156869832Seric { 156969832Seric setproctitle("rejecting connections: maximum children: %d", 157069832Seric CurChildren); 157169832Seric } 157269832Seric else 157369832Seric return FALSE; 157469832Seric return TRUE; 157553037Seric } 157653037Seric /* 157724943Seric ** SETPROCTITLE -- set process title for ps 157824943Seric ** 157924943Seric ** Parameters: 158058674Seric ** fmt -- a printf style format string. 158158674Seric ** a, b, c -- possible parameters to fmt. 158224943Seric ** 158324943Seric ** Returns: 158424943Seric ** none. 158524943Seric ** 158624943Seric ** Side Effects: 158724943Seric ** Clobbers argv of our main procedure so ps(1) will 158824943Seric ** display the title. 158924943Seric */ 159024943Seric 159168508Seric #define SPT_NONE 0 /* don't use it at all */ 159268508Seric #define SPT_REUSEARGV 1 /* cover argv with title information */ 159368508Seric #define SPT_BUILTIN 2 /* use libc builtin */ 159468508Seric #define SPT_PSTAT 3 /* use pstat(PSTAT_SETCMD, ...) */ 159568508Seric #define SPT_PSSTRINGS 4 /* use PS_STRINGS->... */ 159669654Seric #define SPT_SYSMIPS 5 /* use sysmips() supported by NEWS-OS 6 */ 159768508Seric 159868508Seric #ifndef SPT_TYPE 159968508Seric # define SPT_TYPE SPT_REUSEARGV 160068508Seric #endif 160168508Seric 160268508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN 160368508Seric 160468508Seric # if SPT_TYPE == SPT_PSTAT 160558689Seric # include <sys/pstat.h> 160658689Seric # endif 160768508Seric # if SPT_TYPE == SPT_PSSTRINGS 160859732Seric # include <machine/vmparam.h> 160959732Seric # include <sys/exec.h> 161068508Seric # ifndef PS_STRINGS /* hmmmm.... apparently not available after all */ 161168508Seric # undef SPT_TYPE 161268508Seric # define SPT_TYPE SPT_REUSEARGV 161368734Seric # else 161468734Seric # ifndef NKPDE /* FreeBSD 2.0 */ 161568734Seric # define NKPDE 63 161668734Seric typedef unsigned int *pt_entry_t; 161768734Seric # endif 161864247Seric # endif 161959732Seric # endif 162068508Seric 162168508Seric # if SPT_TYPE == SPT_PSSTRINGS 162268508Seric # define SETPROC_STATIC static 162368508Seric # else 162459732Seric # define SETPROC_STATIC 162559732Seric # endif 162667626Seric 162769654Seric # if SPT_TYPE == SPT_SYSMIPS 162869654Seric # include <sys/sysmips.h> 162969654Seric # include <sys/sysnews.h> 163069654Seric # endif 163169654Seric 163268508Seric # ifndef SPT_PADCHAR 163368508Seric # define SPT_PADCHAR ' ' 163468508Seric # endif 163558689Seric 163668508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */ 163764843Seric 163868508Seric #if SPT_TYPE != SPT_BUILTIN 163968508Seric 164024943Seric /*VARARGS1*/ 164168508Seric void 164268508Seric # ifdef __STDC__ 164357642Seric setproctitle(char *fmt, ...) 164468508Seric # else 164557642Seric setproctitle(fmt, va_alist) 164624943Seric char *fmt; 164757642Seric va_dcl 164868508Seric # endif 164924943Seric { 165068508Seric # if SPT_TYPE != SPT_NONE 165124943Seric register char *p; 165225049Seric register int i; 165359732Seric SETPROC_STATIC char buf[MAXLINE]; 165456852Seric VA_LOCAL_DECL 165568508Seric # if SPT_TYPE == SPT_PSTAT 165658689Seric union pstun pst; 165758689Seric # endif 165869748Seric # if SPT_TYPE == SPT_REUSEARGV 165924943Seric extern char **Argv; 166024943Seric extern char *LastArgv; 166169748Seric # endif 166224943Seric 166358674Seric p = buf; 166424943Seric 166558674Seric /* print sendmail: heading for grep */ 166658674Seric (void) strcpy(p, "sendmail: "); 166758674Seric p += strlen(p); 166824943Seric 166958674Seric /* print the argument string */ 167058674Seric VA_START(fmt); 167158674Seric (void) vsprintf(p, fmt, ap); 167256852Seric VA_END; 167354996Seric 167458674Seric i = strlen(buf); 167558689Seric 167668508Seric # if SPT_TYPE == SPT_PSTAT 167758689Seric pst.pst_command = buf; 167858689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 167969654Seric # endif 168069654Seric # if SPT_TYPE == SPT_PSSTRINGS 168159732Seric PS_STRINGS->ps_nargvstr = 1; 168259732Seric PS_STRINGS->ps_argvstr = buf; 168369654Seric # endif 168469654Seric # if SPT_TYPE == SPT_SYSMIPS 168569654Seric sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf); 168669654Seric # endif 168769654Seric # if SPT_TYPE == SPT_REUSEARGV 168854996Seric if (i > LastArgv - Argv[0] - 2) 168925049Seric { 169054996Seric i = LastArgv - Argv[0] - 2; 169158674Seric buf[i] = '\0'; 169225049Seric } 169358674Seric (void) strcpy(Argv[0], buf); 169454997Seric p = &Argv[0][i]; 169524943Seric while (p < LastArgv) 169668508Seric *p++ = SPT_PADCHAR; 169768508Seric Argv[1] = NULL; 169869654Seric # endif 169968508Seric # endif /* SPT_TYPE != SPT_NONE */ 170024943Seric } 170165982Seric 170268508Seric #endif /* SPT_TYPE != SPT_BUILTIN */ 170325698Seric /* 170425698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 170525698Seric ** 170625698Seric ** Parameters: 170768508Seric ** sig -- the signal that got us here (unused). 170825698Seric ** 170925698Seric ** Returns: 171025698Seric ** none. 171125698Seric ** 171225698Seric ** Side Effects: 171325698Seric ** Picks up extant zombies. 171425698Seric */ 171525698Seric 171646928Sbostic void 171768508Seric reapchild(sig) 171868508Seric int sig; 171925698Seric { 172064738Seric int olderrno = errno; 172163966Seric # ifdef HASWAITPID 172260219Seric auto int status; 172360560Seric int count; 172460560Seric int pid; 172560219Seric 172660560Seric count = 0; 172760560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 172860560Seric { 172960560Seric if (count++ > 1000) 173060560Seric { 173166748Seric #ifdef LOG 173260560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 173360560Seric pid, status); 173466748Seric #endif 173560560Seric break; 173660560Seric } 173769832Seric CurChildren--; 173860560Seric } 173960219Seric # else 174025698Seric # ifdef WNOHANG 174125698Seric union wait status; 174225698Seric 174363753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 174469832Seric CurChildren--; 174556795Seric # else /* WNOHANG */ 174625698Seric auto int status; 174725698Seric 174860219Seric while (wait(&status) > 0) 174969832Seric CurChildren--; 175056795Seric # endif /* WNOHANG */ 175160219Seric # endif 175264035Seric # ifdef SYS5SIGNALS 175364035Seric (void) setsignal(SIGCHLD, reapchild); 175458061Seric # endif 175564738Seric errno = olderrno; 175625698Seric } 175755418Seric /* 175869654Seric ** PUTENV -- emulation of putenv() in terms of setenv() 175969654Seric ** 176069654Seric ** Not needed on Posix-compliant systems. 176169654Seric ** This doesn't have full Posix semantics, but it's good enough 176269654Seric ** for sendmail. 176369654Seric ** 176469654Seric ** Parameter: 176569654Seric ** env -- the environment to put. 176669654Seric ** 176769654Seric ** Returns: 176869654Seric ** none. 176969654Seric */ 177069654Seric 177169654Seric #ifdef NEEDPUTENV 177269654Seric 177369717Seric # if NEEDPUTENV == 2 /* no setenv(3) call available */ 177469717Seric 177569717Seric int 177669717Seric putenv(str) 177769717Seric char *str; 177869717Seric { 177969717Seric char **current; 178069717Seric int matchlen, envlen=0; 178169717Seric char *tmp; 178269717Seric char **newenv; 178369717Seric static int first=1; 178469717Seric extern char **environ; 178569717Seric 178669717Seric /* 178769717Seric * find out how much of str to match when searching 178869717Seric * for a string to replace. 178969717Seric */ 179069717Seric if ((tmp = index(str, '=')) == NULL || tmp == str) 179169717Seric matchlen = strlen(str); 179269717Seric else 179369717Seric matchlen = (int) (tmp - str); 179469717Seric ++matchlen; 179569717Seric 179669717Seric /* 179769717Seric * Search for an existing string in the environment and find the 179869717Seric * length of environ. If found, replace and exit. 179969717Seric */ 180069717Seric for (current=environ; *current; current++) { 180169717Seric ++envlen; 180269717Seric 180369717Seric if (strncmp(str, *current, matchlen) == 0) { 180469717Seric /* found it, now insert the new version */ 180569717Seric *current = (char *)str; 180669717Seric return(0); 180769717Seric } 180869717Seric } 180969717Seric 181069717Seric /* 181169717Seric * There wasn't already a slot so add space for a new slot. 181269717Seric * If this is our first time through, use malloc(), else realloc(). 181369717Seric */ 181469717Seric if (first) { 181569717Seric newenv = (char **) malloc(sizeof(char *) * (envlen + 2)); 181669717Seric if (newenv == NULL) 181769717Seric return(-1); 181869717Seric 181969717Seric first=0; 182069717Seric (void) memcpy(newenv, environ, sizeof(char *) * envlen); 182169717Seric } else { 182269717Seric newenv = (char **) realloc((char *)environ, sizeof(char *) * (envlen + 2)); 182369717Seric if (newenv == NULL) 182469717Seric return(-1); 182569717Seric } 182669717Seric 182769717Seric /* actually add in the new entry */ 182869717Seric environ = newenv; 182969717Seric environ[envlen] = (char *)str; 183069717Seric environ[envlen+1] = NULL; 183169717Seric 183269717Seric return(0); 183369717Seric } 183469717Seric 183569717Seric #else /* implement putenv() in terms of setenv() */ 183669717Seric 183769717Seric int 183869654Seric putenv(env) 183969654Seric char *env; 184069654Seric { 184169654Seric char *p; 184269654Seric int l; 184369654Seric char nbuf[100]; 184469654Seric 184569654Seric p = strchr(env, '='); 184669654Seric if (p == NULL) 184769717Seric return 0; 184869654Seric l = p - env; 184969654Seric if (l > sizeof nbuf - 1) 185069654Seric l = sizeof nbuf - 1; 185169654Seric bcopy(env, nbuf, l); 185269654Seric nbuf[l] = '\0'; 185369717Seric return setenv(nbuf, ++p, 1); 185469654Seric } 185569654Seric 185669717Seric # endif 185769654Seric #endif 185869654Seric /* 185955418Seric ** UNSETENV -- remove a variable from the environment 186055418Seric ** 186155418Seric ** Not needed on newer systems. 186255418Seric ** 186355418Seric ** Parameters: 186455418Seric ** name -- the string name of the environment variable to be 186555418Seric ** deleted from the current environment. 186655418Seric ** 186755418Seric ** Returns: 186855418Seric ** none. 186955418Seric ** 187055418Seric ** Globals: 187155418Seric ** environ -- a pointer to the current environment. 187255418Seric ** 187355418Seric ** Side Effects: 187455418Seric ** Modifies environ. 187555418Seric */ 187655418Seric 187763962Seric #ifndef HASUNSETENV 187855418Seric 187955418Seric void 188055418Seric unsetenv(name) 188155418Seric char *name; 188255418Seric { 188355418Seric extern char **environ; 188455418Seric register char **pp; 188555418Seric int len = strlen(name); 188655418Seric 188755418Seric for (pp = environ; *pp != NULL; pp++) 188855418Seric { 188955418Seric if (strncmp(name, *pp, len) == 0 && 189055418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 189155418Seric break; 189255418Seric } 189355418Seric 189455418Seric for (; *pp != NULL; pp++) 189555418Seric *pp = pp[1]; 189655418Seric } 189755418Seric 189863962Seric #endif 189956215Seric /* 190056215Seric ** GETDTABLESIZE -- return number of file descriptors 190156215Seric ** 190256215Seric ** Only on non-BSD systems 190356215Seric ** 190456215Seric ** Parameters: 190556215Seric ** none 190656215Seric ** 190756215Seric ** Returns: 190856215Seric ** size of file descriptor table 190956215Seric ** 191056215Seric ** Side Effects: 191156215Seric ** none 191256215Seric */ 191356215Seric 191463787Seric #ifdef SOLARIS 191563787Seric # include <sys/resource.h> 191663787Seric #endif 191756215Seric 191856215Seric int 191963787Seric getdtsize() 192056215Seric { 192163787Seric #ifdef RLIMIT_NOFILE 192263787Seric struct rlimit rl; 192363787Seric 192463787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 192563787Seric return rl.rlim_cur; 192663787Seric #endif 192763787Seric 192864031Seric # ifdef HASGETDTABLESIZE 192964031Seric return getdtablesize(); 193064031Seric # else 193164031Seric # ifdef _SC_OPEN_MAX 193258689Seric return sysconf(_SC_OPEN_MAX); 193363787Seric # else 193456215Seric return NOFILE; 193563787Seric # endif 193658689Seric # endif 193756215Seric } 193857631Seric /* 193957631Seric ** UNAME -- get the UUCP name of this system. 194057631Seric */ 194157631Seric 194257943Seric #ifndef HASUNAME 194357631Seric 194457631Seric int 194557631Seric uname(name) 194657631Seric struct utsname *name; 194757631Seric { 194857631Seric FILE *file; 194957631Seric char *n; 195057631Seric 195157631Seric name->nodename[0] = '\0'; 195257631Seric 195357661Seric /* try /etc/whoami -- one line with the node name */ 195457631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 195557631Seric { 195657661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 195757631Seric (void) fclose(file); 195857661Seric n = strchr(name->nodename, '\n'); 195957631Seric if (n != NULL) 196057631Seric *n = '\0'; 196157631Seric if (name->nodename[0] != '\0') 196257631Seric return (0); 196357631Seric } 196457631Seric 196557661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 196657631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 196757631Seric { 196857631Seric char buf[MAXLINE]; 196957631Seric 197057631Seric while (fgets(buf, MAXLINE, file) != NULL) 197157631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 197257631Seric NODE_LENGTH, name->nodename) > 0) 197357631Seric break; 197457631Seric (void) fclose(file); 197557631Seric if (name->nodename[0] != '\0') 197657631Seric return (0); 197757631Seric } 197857631Seric 197957631Seric #ifdef TRUST_POPEN 198057631Seric /* 198157631Seric ** Popen is known to have security holes. 198257631Seric */ 198357631Seric 198457661Seric /* try uuname -l to return local name */ 198557631Seric if ((file = popen("uuname -l", "r")) != NULL) 198657631Seric { 198757661Seric (void) fgets(name, NODE_LENGTH + 1, file); 198857631Seric (void) pclose(file); 198957661Seric n = strchr(name, '\n'); 199057631Seric if (n != NULL) 199157631Seric *n = '\0'; 199257661Seric if (name->nodename[0] != '\0') 199357631Seric return (0); 199457631Seric } 199557631Seric #endif 199669472Seric 199757631Seric return (-1); 199857631Seric } 199957943Seric #endif /* HASUNAME */ 200058068Seric /* 200158068Seric ** INITGROUPS -- initialize groups 200258068Seric ** 200358068Seric ** Stub implementation for System V style systems 200458068Seric */ 200558068Seric 200658068Seric #ifndef HASINITGROUPS 200758068Seric 200858068Seric initgroups(name, basegid) 200958068Seric char *name; 201058068Seric int basegid; 201158068Seric { 201258068Seric return 0; 201358068Seric } 201458068Seric 201558068Seric #endif 201658082Seric /* 201759289Seric ** SETSID -- set session id (for non-POSIX systems) 201859289Seric */ 201959289Seric 202059289Seric #ifndef HASSETSID 202159289Seric 202263753Seric pid_t 202363753Seric setsid __P ((void)) 202459289Seric { 202564155Seric #ifdef TIOCNOTTY 202664155Seric int fd; 202764155Seric 202866743Seric fd = open("/dev/tty", O_RDWR, 0); 202964155Seric if (fd >= 0) 203064155Seric { 203164155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 203264155Seric (void) close(fd); 203364155Seric } 203464155Seric #endif /* TIOCNOTTY */ 203564705Seric # ifdef SYS5SETPGRP 203663753Seric return setpgrp(); 203763753Seric # else 203864155Seric return setpgid(0, getpid()); 203959289Seric # endif 204059289Seric } 204159289Seric 204259289Seric #endif 204359289Seric /* 204466748Seric ** FSYNC -- dummy fsync 204566748Seric */ 204666748Seric 204766748Seric #ifdef NEEDFSYNC 204866748Seric 204966748Seric fsync(fd) 205066748Seric int fd; 205166748Seric { 205266748Seric # ifdef O_SYNC 205366748Seric return fcntl(fd, F_SETFL, O_SYNC); 205466748Seric # else 205566748Seric /* nothing we can do */ 205666748Seric return 0; 205766748Seric # endif 205866748Seric } 205966748Seric 206066748Seric #endif 206166748Seric /* 206264718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 206364718Seric ** 206464718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 206568508Seric ** instead of a long. This patches things. Only needed on versions 206668508Seric ** prior to 5.4.3. 206764718Seric */ 206864718Seric 206968508Seric #ifdef DGUX_5_4_2 207064718Seric 207164718Seric #undef inet_addr 207264718Seric 207364718Seric long 207464718Seric dgux_inet_addr(host) 207564718Seric char *host; 207664718Seric { 207764718Seric struct in_addr haddr; 207864718Seric 207964718Seric haddr = inet_addr(host); 208064718Seric return haddr.s_addr; 208164718Seric } 208264718Seric 208364718Seric #endif 208464718Seric /* 208563969Seric ** GETOPT -- for old systems or systems with bogus implementations 208663969Seric */ 208763969Seric 208863969Seric #ifdef NEEDGETOPT 208963969Seric 209063969Seric /* 209163969Seric * Copyright (c) 1985 Regents of the University of California. 209263969Seric * All rights reserved. The Berkeley software License Agreement 209363969Seric * specifies the terms and conditions for redistribution. 209463969Seric */ 209563969Seric 209663969Seric 209763969Seric /* 209863969Seric ** this version hacked to add `atend' flag to allow state machine 209963969Seric ** to reset if invoked by the program to scan args for a 2nd time 210063969Seric */ 210163969Seric 210263969Seric #if defined(LIBC_SCCS) && !defined(lint) 210363969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 210464072Seric #endif /* LIBC_SCCS and not lint */ 210563969Seric 210663969Seric #include <stdio.h> 210763969Seric 210863969Seric /* 210963969Seric * get option letter from argument vector 211063969Seric */ 211165352Seric #ifdef _CONVEX_SOURCE 211268508Seric extern int optind, opterr, optopt; 211368508Seric extern char *optarg; 211465352Seric #else 211565352Seric int opterr = 1; /* if error message should be printed */ 211665352Seric int optind = 1; /* index into parent argv vector */ 211768470Seric int optopt = 0; /* character checked for validity */ 211868470Seric char *optarg = NULL; /* argument associated with option */ 211968508Seric #endif 212063969Seric 212163969Seric #define BADCH (int)'?' 212263969Seric #define EMSG "" 212363969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 212463969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 212563969Seric 212663969Seric getopt(nargc,nargv,ostr) 212765160Seric int nargc; 212865160Seric char *const *nargv; 212965160Seric const char *ostr; 213063969Seric { 213163969Seric static char *place = EMSG; /* option letter processing */ 213263969Seric static char atend = 0; 213363969Seric register char *oli; /* option letter list index */ 213463969Seric 213563969Seric if (atend) { 213663969Seric atend = 0; 213763969Seric place = EMSG; 213863969Seric } 213963969Seric if(!*place) { /* update scanning pointer */ 214064136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 214163969Seric atend++; 214263969Seric return(EOF); 214363969Seric } 214463969Seric if (*place == '-') { /* found "--" */ 214563969Seric ++optind; 214663969Seric atend++; 214763969Seric return(EOF); 214863969Seric } 214963969Seric } /* option letter okay? */ 215064125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 215164136Seric if (!*place) ++optind; 215263969Seric tell(": illegal option -- "); 215363969Seric } 215463969Seric if (*++oli != ':') { /* don't need argument */ 215563969Seric optarg = NULL; 215663969Seric if (!*place) ++optind; 215763969Seric } 215863969Seric else { /* need an argument */ 215963969Seric if (*place) optarg = place; /* no white space */ 216063969Seric else if (nargc <= ++optind) { /* no arg */ 216163969Seric place = EMSG; 216263969Seric tell(": option requires an argument -- "); 216363969Seric } 216463969Seric else optarg = nargv[optind]; /* white space */ 216563969Seric place = EMSG; 216663969Seric ++optind; 216763969Seric } 216863969Seric return(optopt); /* dump back option letter */ 216963969Seric } 217063969Seric 217163969Seric #endif 217263969Seric /* 217363969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 217463969Seric */ 217563969Seric 217663969Seric #ifdef NEEDVPRINTF 217763969Seric 217863969Seric #define MAXARG 16 217963969Seric 218063969Seric vfprintf(fp, fmt, ap) 218163969Seric FILE * fp; 218263969Seric char * fmt; 218363969Seric char ** ap; 218463969Seric { 218563969Seric char * bp[MAXARG]; 218663969Seric int i = 0; 218763969Seric 218863969Seric while (*ap && i < MAXARG) 218963969Seric bp[i++] = *ap++; 219063969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 219163969Seric bp[4], bp[5], bp[6], bp[7], 219263969Seric bp[8], bp[9], bp[10], bp[11], 219363969Seric bp[12], bp[13], bp[14], bp[15]); 219463969Seric } 219563969Seric 219663969Seric vsprintf(s, fmt, ap) 219763969Seric char * s; 219863969Seric char * fmt; 219963969Seric char ** ap; 220063969Seric { 220163969Seric char * bp[MAXARG]; 220263969Seric int i = 0; 220363969Seric 220463969Seric while (*ap && i < MAXARG) 220563969Seric bp[i++] = *ap++; 220663969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 220763969Seric bp[4], bp[5], bp[6], bp[7], 220863969Seric bp[8], bp[9], bp[10], bp[11], 220963969Seric bp[12], bp[13], bp[14], bp[15]); 221063969Seric } 221163969Seric 221263969Seric #endif 221363969Seric /* 221465220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 221565208Seric ** 221665211Seric ** Parameters: 221769894Seric ** user -- the name of the user we are checking. 221865211Seric ** shell -- the user's shell from /etc/passwd 221965211Seric ** 222065211Seric ** Returns: 222165211Seric ** TRUE -- if it is ok to use this for unrestricted access. 222265211Seric ** FALSE -- if the shell is restricted. 222365208Seric */ 222465208Seric 222565587Seric #if !HASGETUSERSHELL 222665587Seric 222765587Seric # ifndef _PATH_SHELLS 222865587Seric # define _PATH_SHELLS "/etc/shells" 222965587Seric # endif 223065587Seric 223169762Seric # ifdef _AIX3 223269762Seric # include <userconf.h> 223369762Seric # include <usersec.h> 223469762Seric # endif 223569762Seric 223665587Seric char *DefaultUserShells[] = 223765587Seric { 223868508Seric "/bin/sh", /* standard shell */ 223965587Seric "/usr/bin/sh", 224068508Seric "/bin/csh", /* C shell */ 224165587Seric "/usr/bin/csh", 224265587Seric #ifdef __hpux 224368889Seric # ifdef V4FS 224468889Seric "/usr/bin/rsh", /* restricted Bourne shell */ 224568889Seric "/usr/bin/ksh", /* Korn shell */ 224668889Seric "/usr/bin/rksh", /* restricted Korn shell */ 224768889Seric "/usr/bin/pam", 224868889Seric "/usr/bin/keysh", /* key shell (extended Korn shell) */ 224968889Seric "/usr/bin/posix/sh", 225068889Seric # else 225168508Seric "/bin/rsh", /* restricted Bourne shell */ 225268508Seric "/bin/ksh", /* Korn shell */ 225368508Seric "/bin/rksh", /* restricted Korn shell */ 225465587Seric "/bin/pam", 225568508Seric "/usr/bin/keysh", /* key shell (extended Korn shell) */ 225665587Seric "/bin/posix/sh", 225768889Seric # endif 225865211Seric #endif 225968508Seric #ifdef _AIX3 226068508Seric "/bin/ksh", /* Korn shell */ 226168508Seric "/usr/bin/ksh", 226268508Seric "/bin/tsh", /* trusted shell */ 226368508Seric "/usr/bin/tsh", 226468508Seric "/bin/bsh", /* Bourne shell */ 226568508Seric "/usr/bin/bsh", 226668508Seric #endif 226765587Seric NULL 226865587Seric }; 226965587Seric 227065208Seric #endif 227165208Seric 227265747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 227365747Seric 227465211Seric bool 227569894Seric usershellok(user, shell) 227669894Seric char *user; 227765211Seric char *shell; 227865208Seric { 227965211Seric #if HASGETUSERSHELL 228065211Seric register char *p; 228165211Seric extern char *getusershell(); 228265208Seric 228369894Seric if (shell == NULL || shell[0] == '\0' || wordinclass(user, 't') || 228469894Seric ConfigLevel <= 1) 228568457Seric return TRUE; 228668457Seric 228765211Seric setusershell(); 228865211Seric while ((p = getusershell()) != NULL) 228965747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 229065211Seric break; 229165211Seric endusershell(); 229265211Seric return p != NULL; 229365211Seric #else 229469769Seric # if USEGETCONFATTR 229569762Seric auto char *v; 229669762Seric # endif 229765211Seric register FILE *shellf; 229865211Seric char buf[MAXLINE]; 229965208Seric 230069894Seric if (shell == NULL || shell[0] == '\0' || wordinclass(user, 't')) 230168457Seric return TRUE; 230268457Seric 230369769Seric # if USEGETCONFATTR 230469769Seric /* 230569769Seric ** Naturally IBM has a "better" idea..... 230669769Seric ** 230769769Seric ** What a crock. This interface isn't documented, it is 230869769Seric ** considered part of the security library (-ls), and it 230969769Seric ** only works if you are running as root (since the list 231069769Seric ** of valid shells is obviously a source of great concern). 231169769Seric ** I recommend that you do NOT define USEGETCONFATTR, 231269769Seric ** especially since you are going to have to set up an 231369769Seric ** /etc/shells anyhow to handle the cases where getconfattr 231469769Seric ** fails. 231569769Seric */ 231669769Seric 231769769Seric if (getconfattr(SC_SYS_LOGIN, SC_SHELLS, &v, SEC_LIST) == 0 && v != NULL) 231869762Seric { 231969769Seric while (*v != '\0') 232069762Seric { 232169762Seric if (strcmp(v, shell) == 0 || strcmp(v, WILDCARD_SHELL) == 0) 232269762Seric return TRUE; 232369762Seric v += strlen(v) + 1; 232469762Seric } 232569769Seric return FALSE; 232669762Seric } 232769762Seric # endif 232869762Seric 232965211Seric shellf = fopen(_PATH_SHELLS, "r"); 233065211Seric if (shellf == NULL) 233165211Seric { 233265211Seric /* no /etc/shells; see if it is one of the std shells */ 233365587Seric char **d; 233465587Seric 233565587Seric for (d = DefaultUserShells; *d != NULL; d++) 233665587Seric { 233765587Seric if (strcmp(shell, *d) == 0) 233865587Seric return TRUE; 233965587Seric } 234065587Seric return FALSE; 234165211Seric } 234265208Seric 234365211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 234465211Seric { 234565211Seric register char *p, *q; 234665208Seric 234765211Seric p = buf; 234865211Seric while (*p != '\0' && *p != '#' && *p != '/') 234965211Seric p++; 235065211Seric if (*p == '#' || *p == '\0') 235165208Seric continue; 235265211Seric q = p; 235365211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 235465211Seric p++; 235565211Seric *p = '\0'; 235665747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 235765211Seric { 235865211Seric fclose(shellf); 235965211Seric return TRUE; 236065211Seric } 236165208Seric } 236265211Seric fclose(shellf); 236365211Seric return FALSE; 236465211Seric #endif 236565208Seric } 236665208Seric /* 236769801Seric ** FREEDISKSPACE -- see how much free space is on the queue filesystem 236858082Seric ** 236958082Seric ** Only implemented if you have statfs. 237058082Seric ** 237158082Seric ** Parameters: 237264311Seric ** dir -- the directory in question. 237364311Seric ** bsize -- a variable into which the filesystem 237464311Seric ** block size is stored. 237558082Seric ** 237658082Seric ** Returns: 237764311Seric ** The number of bytes free on the queue filesystem. 237864311Seric ** -1 if the statfs call fails. 237964311Seric ** 238064311Seric ** Side effects: 238164311Seric ** Puts the filesystem block size into bsize. 238258082Seric */ 238358082Seric 238465749Seric /* statfs types */ 238565749Seric #define SFS_NONE 0 /* no statfs implementation */ 238665749Seric #define SFS_USTAT 1 /* use ustat */ 238765749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 238865749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 238965749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 239065749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 239166752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 239265749Seric 239365749Seric #ifndef SFS_TYPE 239465749Seric # define SFS_TYPE SFS_NONE 239558157Seric #endif 239658157Seric 239765749Seric #if SFS_TYPE == SFS_USTAT 239858157Seric # include <ustat.h> 239958157Seric #endif 240065749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 240165749Seric # include <sys/statfs.h> 240258082Seric #endif 240365749Seric #if SFS_TYPE == SFS_VFS 240465749Seric # include <sys/vfs.h> 240565749Seric #endif 240665749Seric #if SFS_TYPE == SFS_MOUNT 240765749Seric # include <sys/mount.h> 240865749Seric #endif 240966752Seric #if SFS_TYPE == SFS_STATVFS 241066752Seric # include <sys/statvfs.h> 241166752Seric #endif 241258082Seric 241364311Seric long 241469801Seric freediskspace(dir, bsize) 241564311Seric char *dir; 241664311Seric long *bsize; 241758082Seric { 241865749Seric #if SFS_TYPE != SFS_NONE 241965749Seric # if SFS_TYPE == SFS_USTAT 242058153Seric struct ustat fs; 242158153Seric struct stat statbuf; 242258366Seric # define FSBLOCKSIZE DEV_BSIZE 242368695Seric # define SFS_BAVAIL f_tfree 242458157Seric # else 242558157Seric # if defined(ultrix) 242658157Seric struct fs_data fs; 242768695Seric # define SFS_BAVAIL fd_bfreen 242868508Seric # define FSBLOCKSIZE 1024L 242958153Seric # else 243066752Seric # if SFS_TYPE == SFS_STATVFS 243166752Seric struct statvfs fs; 243268508Seric # define FSBLOCKSIZE fs.f_frsize 243366752Seric # else 243458082Seric struct statfs fs; 243566752Seric # define FSBLOCKSIZE fs.f_bsize 243663987Seric # endif 243758153Seric # endif 243858133Seric # endif 243968695Seric # ifndef SFS_BAVAIL 244068695Seric # define SFS_BAVAIL f_bavail 244168508Seric # endif 244258082Seric 244365749Seric # if SFS_TYPE == SFS_USTAT 244464311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 244558157Seric # else 244665749Seric # if SFS_TYPE == SFS_4ARGS 244764311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 244858157Seric # else 244968508Seric # if SFS_TYPE == SFS_STATVFS 245068508Seric if (statvfs(dir, &fs) == 0) 245168508Seric # else 245268508Seric # if defined(ultrix) 245368499Seric if (statfs(dir, &fs) > 0) 245468508Seric # else 245564311Seric if (statfs(dir, &fs) == 0) 245668508Seric # endif 245758153Seric # endif 245858133Seric # endif 245958133Seric # endif 246058133Seric { 246164311Seric if (bsize != NULL) 246264311Seric *bsize = FSBLOCKSIZE; 246368695Seric return (fs.SFS_BAVAIL); 246464311Seric } 246564337Seric #endif 246664311Seric return (-1); 246764311Seric } 246864311Seric /* 246969801Seric ** ENOUGHDISKSPACE -- is there enough free space on the queue fs? 247064311Seric ** 247164311Seric ** Only implemented if you have statfs. 247264311Seric ** 247364311Seric ** Parameters: 247464311Seric ** msize -- the size to check against. If zero, we don't yet 247564311Seric ** know how big the message will be, so just check for 247664311Seric ** a "reasonable" amount. 247764311Seric ** 247864311Seric ** Returns: 247964311Seric ** TRUE if there is enough space. 248064311Seric ** FALSE otherwise. 248164311Seric */ 248264311Seric 248364311Seric bool 248469801Seric enoughdiskspace(msize) 248564311Seric long msize; 248664311Seric { 248764311Seric long bfree, bsize; 248864311Seric 248964311Seric if (MinBlocksFree <= 0 && msize <= 0) 249064311Seric { 249158133Seric if (tTd(4, 80)) 249269801Seric printf("enoughdiskspace: no threshold\n"); 249364311Seric return TRUE; 249464311Seric } 249564311Seric 249669801Seric if ((bfree = freediskspace(QueueDir, &bsize)) >= 0) 249764311Seric { 249864311Seric if (tTd(4, 80)) 249969801Seric printf("enoughdiskspace: bavail=%ld, need=%ld\n", 250064311Seric bfree, msize); 250158333Seric 250258333Seric /* convert msize to block count */ 250364311Seric msize = msize / bsize + 1; 250458333Seric if (MinBlocksFree >= 0) 250558333Seric msize += MinBlocksFree; 250658333Seric 250764311Seric if (bfree < msize) 250858090Seric { 250958090Seric #ifdef LOG 251058090Seric if (LogLevel > 0) 251164254Seric syslog(LOG_ALERT, 251264254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 251368508Seric CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id, 251468508Seric bfree, 251568508Seric CurHostName == NULL ? "SMTP-DAEMON" : CurHostName, 251668508Seric msize, QueueDir); 251758090Seric #endif 251858082Seric return FALSE; 251958090Seric } 252058082Seric } 252158082Seric else if (tTd(4, 80)) 252269801Seric printf("enoughdiskspace failure: min=%ld, need=%ld: %s\n", 252358333Seric MinBlocksFree, msize, errstring(errno)); 252458082Seric return TRUE; 252558082Seric } 252658542Seric /* 252758542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 252858542Seric ** 252958542Seric ** This looks at an errno value and tells if this is likely to 253058542Seric ** go away if retried later. 253158542Seric ** 253258542Seric ** Parameters: 253358542Seric ** err -- the errno code to classify. 253458542Seric ** 253558542Seric ** Returns: 253658542Seric ** TRUE if this is probably transient. 253758542Seric ** FALSE otherwise. 253858542Seric */ 253958542Seric 254058542Seric bool 254158542Seric transienterror(err) 254258542Seric int err; 254358542Seric { 254458542Seric switch (err) 254558542Seric { 254658542Seric case EIO: /* I/O error */ 254758542Seric case ENXIO: /* Device not configured */ 254858542Seric case EAGAIN: /* Resource temporarily unavailable */ 254958542Seric case ENOMEM: /* Cannot allocate memory */ 255058542Seric case ENODEV: /* Operation not supported by device */ 255158542Seric case ENFILE: /* Too many open files in system */ 255258542Seric case EMFILE: /* Too many open files */ 255358542Seric case ENOSPC: /* No space left on device */ 255458542Seric #ifdef ETIMEDOUT 255558542Seric case ETIMEDOUT: /* Connection timed out */ 255658542Seric #endif 255758542Seric #ifdef ESTALE 255858542Seric case ESTALE: /* Stale NFS file handle */ 255958542Seric #endif 256058542Seric #ifdef ENETDOWN 256158542Seric case ENETDOWN: /* Network is down */ 256258542Seric #endif 256358542Seric #ifdef ENETUNREACH 256458542Seric case ENETUNREACH: /* Network is unreachable */ 256558542Seric #endif 256658542Seric #ifdef ENETRESET 256758542Seric case ENETRESET: /* Network dropped connection on reset */ 256858542Seric #endif 256958542Seric #ifdef ECONNABORTED 257058542Seric case ECONNABORTED: /* Software caused connection abort */ 257158542Seric #endif 257258542Seric #ifdef ECONNRESET 257358542Seric case ECONNRESET: /* Connection reset by peer */ 257458542Seric #endif 257558542Seric #ifdef ENOBUFS 257658542Seric case ENOBUFS: /* No buffer space available */ 257758542Seric #endif 257858542Seric #ifdef ESHUTDOWN 257958542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 258058542Seric #endif 258158542Seric #ifdef ECONNREFUSED 258258542Seric case ECONNREFUSED: /* Connection refused */ 258358542Seric #endif 258458542Seric #ifdef EHOSTDOWN 258558542Seric case EHOSTDOWN: /* Host is down */ 258658542Seric #endif 258758542Seric #ifdef EHOSTUNREACH 258858542Seric case EHOSTUNREACH: /* No route to host */ 258958542Seric #endif 259058542Seric #ifdef EDQUOT 259158542Seric case EDQUOT: /* Disc quota exceeded */ 259258542Seric #endif 259358542Seric #ifdef EPROCLIM 259458542Seric case EPROCLIM: /* Too many processes */ 259558542Seric #endif 259658542Seric #ifdef EUSERS 259758542Seric case EUSERS: /* Too many users */ 259858542Seric #endif 259958542Seric #ifdef EDEADLK 260058542Seric case EDEADLK: /* Resource deadlock avoided */ 260158542Seric #endif 260258542Seric #ifdef EISCONN 260358542Seric case EISCONN: /* Socket already connected */ 260458542Seric #endif 260558542Seric #ifdef EINPROGRESS 260658542Seric case EINPROGRESS: /* Operation now in progress */ 260758542Seric #endif 260858542Seric #ifdef EALREADY 260958542Seric case EALREADY: /* Operation already in progress */ 261058542Seric #endif 261158542Seric #ifdef EADDRINUSE 261258542Seric case EADDRINUSE: /* Address already in use */ 261358542Seric #endif 261458542Seric #ifdef EADDRNOTAVAIL 261558542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 261658542Seric #endif 261766018Seric #ifdef ETXTBSY 261866017Seric case ETXTBSY: /* (Apollo) file locked */ 261966017Seric #endif 262063834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 262158542Seric case ENOSR: /* Out of streams resources */ 262258542Seric #endif 262358542Seric return TRUE; 262458542Seric } 262558542Seric 262658542Seric /* nope, must be permanent */ 262758542Seric return FALSE; 262858542Seric } 262958689Seric /* 263064035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 263158689Seric ** 263258689Seric ** Parameters: 263358689Seric ** fd -- the file descriptor of the file. 263458689Seric ** filename -- the file name (for error messages). 263564335Seric ** ext -- the filename extension. 263658689Seric ** type -- type of the lock. Bits can be: 263758689Seric ** LOCK_EX -- exclusive lock. 263858689Seric ** LOCK_NB -- non-blocking. 263958689Seric ** 264058689Seric ** Returns: 264158689Seric ** TRUE if the lock was acquired. 264258689Seric ** FALSE otherwise. 264358689Seric */ 264458689Seric 264558689Seric bool 264664335Seric lockfile(fd, filename, ext, type) 264758689Seric int fd; 264858689Seric char *filename; 264964335Seric char *ext; 265058689Seric int type; 265158689Seric { 265265830Seric # if !HASFLOCK 265358689Seric int action; 265458689Seric struct flock lfd; 265564335Seric 265664335Seric if (ext == NULL) 265764335Seric ext = ""; 265869472Seric 265963983Seric bzero(&lfd, sizeof lfd); 266059447Seric if (bitset(LOCK_UN, type)) 266159447Seric lfd.l_type = F_UNLCK; 266259447Seric else if (bitset(LOCK_EX, type)) 266358689Seric lfd.l_type = F_WRLCK; 266458689Seric else 266558689Seric lfd.l_type = F_RDLCK; 266658689Seric 266758689Seric if (bitset(LOCK_NB, type)) 266858689Seric action = F_SETLK; 266958689Seric else 267058689Seric action = F_SETLKW; 267158689Seric 267264264Seric if (tTd(55, 60)) 267364335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 267464335Seric filename, ext, action, lfd.l_type); 267564264Seric 267658689Seric if (fcntl(fd, action, &lfd) >= 0) 267764264Seric { 267864264Seric if (tTd(55, 60)) 267964264Seric printf("SUCCESS\n"); 268058689Seric return TRUE; 268164264Seric } 268258689Seric 268364264Seric if (tTd(55, 60)) 268464264Seric printf("(%s) ", errstring(errno)); 268564264Seric 268664136Seric /* 268764136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 268864136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 268964136Seric ** as type "tmp" (that is, served from swap space), the 269064136Seric ** previous fcntl will fail with "Invalid argument" errors. 269164136Seric ** Since this is fairly common during testing, we will assume 269264136Seric ** that this indicates that the lock is successfully grabbed. 269364136Seric */ 269464136Seric 269564136Seric if (errno == EINVAL) 269664264Seric { 269764264Seric if (tTd(55, 60)) 269864264Seric printf("SUCCESS\n"); 269964136Seric return TRUE; 270064264Seric } 270164136Seric 270258689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 270364378Seric { 270464378Seric int omode = -1; 270564378Seric # ifdef F_GETFL 270664378Seric int oerrno = errno; 270764378Seric 270864378Seric (void) fcntl(fd, F_GETFL, &omode); 270964378Seric errno = oerrno; 271064378Seric # endif 271164378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 271264378Seric filename, ext, fd, type, omode, geteuid()); 271364378Seric } 271458689Seric # else 271564335Seric if (ext == NULL) 271664335Seric ext = ""; 271764335Seric 271864264Seric if (tTd(55, 60)) 271964335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 272064264Seric 272158689Seric if (flock(fd, type) >= 0) 272264264Seric { 272364264Seric if (tTd(55, 60)) 272464264Seric printf("SUCCESS\n"); 272558689Seric return TRUE; 272664264Seric } 272758689Seric 272864264Seric if (tTd(55, 60)) 272964264Seric printf("(%s) ", errstring(errno)); 273064264Seric 273158689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 273264378Seric { 273364378Seric int omode = -1; 273464378Seric # ifdef F_GETFL 273564378Seric int oerrno = errno; 273664378Seric 273764378Seric (void) fcntl(fd, F_GETFL, &omode); 273864378Seric errno = oerrno; 273964378Seric # endif 274064378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 274164378Seric filename, ext, fd, type, omode, geteuid()); 274264386Seric } 274358689Seric # endif 274464264Seric if (tTd(55, 60)) 274564264Seric printf("FAIL\n"); 274658689Seric return FALSE; 274758689Seric } 274864556Seric /* 274965948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 275065948Seric ** 275165948Seric ** Parameters: 275265948Seric ** fd -- the file descriptor to check. 275365948Seric ** 275465948Seric ** Returns: 275565948Seric ** TRUE -- if only root can chown the file to an arbitrary 275665948Seric ** user. 275765948Seric ** FALSE -- if an arbitrary user can give away a file. 275865948Seric */ 275965948Seric 276065948Seric bool 276165948Seric chownsafe(fd) 276265948Seric int fd; 276365948Seric { 276465950Seric #ifdef __hpux 276565948Seric char *s; 276665948Seric int tfd; 276765948Seric uid_t o_uid, o_euid; 276865948Seric gid_t o_gid, o_egid; 276965948Seric bool rval; 277065948Seric struct stat stbuf; 277165948Seric 277265948Seric o_uid = getuid(); 277365948Seric o_euid = geteuid(); 277465948Seric o_gid = getgid(); 277565948Seric o_egid = getegid(); 277665959Seric fstat(fd, &stbuf); 277765948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 277865948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 277965948Seric s = tmpnam(NULL); 278065948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 278165974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 278265948Seric close(tfd); 278365948Seric unlink(s); 278469638Seric setresuid(o_uid, o_euid, -1); 278565948Seric setresgid(o_gid, o_egid, -1); 278665948Seric return rval; 278765948Seric #else 278866088Seric # ifdef _POSIX_CHOWN_RESTRICTED 278966088Seric # if _POSIX_CHOWN_RESTRICTED == -1 279066088Seric return FALSE; 279166088Seric # else 279266088Seric return TRUE; 279366088Seric # endif 279466088Seric # else 279566088Seric # ifdef _PC_CHOWN_RESTRICTED 279668508Seric int rval; 279768508Seric 279868508Seric /* 279968508Seric ** Some systems (e.g., SunOS) seem to have the call and the 280068508Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 280168508Seric ** the call. This heuristic checks for that. 280268508Seric */ 280368508Seric 280468508Seric errno = 0; 280568508Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 280668508Seric if (errno == 0) 280768508Seric return rval > 0; 280868508Seric # endif 280968508Seric # ifdef BSD 281068508Seric return TRUE; 281168499Seric # else 281265948Seric return FALSE; 281365948Seric # endif 281465948Seric # endif 281565948Seric #endif 281665948Seric } 281765948Seric /* 281868508Seric ** RESETLIMITS -- reset system controlled resource limits 281968508Seric ** 282068508Seric ** This is to avoid denial-of-service attacks 282168508Seric ** 282268508Seric ** Parameters: 282368508Seric ** none 282468508Seric ** 282568508Seric ** Returns: 282668508Seric ** none 282768508Seric */ 282868508Seric 282968508Seric #if HASSETRLIMIT 283068508Seric # include <sys/resource.h> 283168508Seric #endif 283268508Seric 283368508Seric void 283468508Seric resetlimits() 283568508Seric { 283668508Seric #if HASSETRLIMIT 283768508Seric struct rlimit lim; 283868508Seric 283968508Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 284068508Seric (void) setrlimit(RLIMIT_CPU, &lim); 284168508Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 284268508Seric #else 284368508Seric # if HASULIMIT 284468508Seric (void) ulimit(2, 0x3fffff); 284568508Seric # endif 284668508Seric #endif 284768508Seric } 284868508Seric /* 284964556Seric ** GETCFNAME -- return the name of the .cf file. 285064556Seric ** 285164556Seric ** Some systems (e.g., NeXT) determine this dynamically. 285264556Seric */ 285364556Seric 285464556Seric char * 285564556Seric getcfname() 285664556Seric { 285769909Seric struct stat st; 285868842Seric 285964556Seric if (ConfFile != NULL) 286064556Seric return ConfFile; 286169881Seric #if NETINFO 286265214Seric { 286365214Seric extern char *ni_propval(); 286465214Seric char *cflocation; 286565214Seric 286668508Seric cflocation = ni_propval("/locations", NULL, "sendmail", 286768508Seric "sendmail.cf", '\0'); 286865214Seric if (cflocation != NULL) 286965214Seric return cflocation; 287065214Seric } 287165214Seric #endif 287268842Seric 287369909Seric if (stat("/etc/sendmail.cf", &st) >= 0) 287469909Seric return "/etc/sendmail.cf"; 287564556Seric return _PATH_SENDMAILCF; 287664556Seric } 287764718Seric /* 287864718Seric ** SETVENDOR -- process vendor code from V configuration line 287964718Seric ** 288064718Seric ** Parameters: 288164718Seric ** vendor -- string representation of vendor. 288264718Seric ** 288364718Seric ** Returns: 288464718Seric ** TRUE -- if ok. 288564718Seric ** FALSE -- if vendor code could not be processed. 288664926Seric ** 288764926Seric ** Side Effects: 288864926Seric ** It is reasonable to set mode flags here to tweak 288964926Seric ** processing in other parts of the code if necessary. 289064926Seric ** For example, if you are a vendor that uses $%y to 289164926Seric ** indicate YP lookups, you could enable that here. 289264718Seric */ 289364718Seric 289464718Seric bool 289564718Seric setvendor(vendor) 289664718Seric char *vendor; 289764718Seric { 289864926Seric if (strcasecmp(vendor, "Berkeley") == 0) 289968508Seric { 290068508Seric VendorCode = VENDOR_BERKELEY; 290164926Seric return TRUE; 290268508Seric } 290364926Seric 290464926Seric /* add vendor extensions here */ 290564926Seric 290668508Seric #ifdef SUN_EXTENSIONS 290768508Seric if (strcasecmp(vendor, "Sun") == 0) 290868508Seric { 290968508Seric VendorCode = VENDOR_SUN; 291068508Seric return TRUE; 291168508Seric } 291268508Seric #endif 291368508Seric 291464926Seric return FALSE; 291564718Seric } 291664816Seric /* 291769510Seric ** VENDOR_PRE_DEFAULTS, VENDOR_POST_DEFAULTS -- set vendor-specific defaults 291869510Seric ** 291969510Seric ** Vendor_pre_defaults is called before reading the configuration 292069510Seric ** file; vendor_post_defaults is called immediately after. 292169510Seric ** 292269510Seric ** Parameters: 292369510Seric ** e -- the global environment to initialize. 292469510Seric ** 292569510Seric ** Returns: 292669510Seric ** none. 292769510Seric */ 292869510Seric 292969510Seric void 293069510Seric vendor_pre_defaults(e) 293169510Seric ENVELOPE *e; 293269510Seric { 293369510Seric } 293469510Seric 293569510Seric 293669510Seric void 293769510Seric vendor_post_defaults(e) 293869510Seric ENVELOPE *e; 293969510Seric { 294069510Seric } 294169510Seric /* 294264816Seric ** STRTOL -- convert string to long integer 294364816Seric ** 294464816Seric ** For systems that don't have it in the C library. 294566161Seric ** 294666161Seric ** This is taken verbatim from the 4.4-Lite C library. 294764816Seric */ 294864816Seric 294964816Seric #ifdef NEEDSTRTOL 295064816Seric 295166161Seric #if defined(LIBC_SCCS) && !defined(lint) 295266161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 295366161Seric #endif /* LIBC_SCCS and not lint */ 295466161Seric 295566161Seric #include <limits.h> 295666161Seric 295766161Seric /* 295866161Seric * Convert a string to a long integer. 295966161Seric * 296066161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 296166161Seric * alphabets and digits are each contiguous. 296266161Seric */ 296366161Seric 296464816Seric long 296566161Seric strtol(nptr, endptr, base) 296666161Seric const char *nptr; 296766161Seric char **endptr; 296866161Seric register int base; 296964816Seric { 297066161Seric register const char *s = nptr; 297166161Seric register unsigned long acc; 297266161Seric register int c; 297366161Seric register unsigned long cutoff; 297466161Seric register int neg = 0, any, cutlim; 297564816Seric 297666161Seric /* 297766161Seric * Skip white space and pick up leading +/- sign if any. 297866161Seric * If base is 0, allow 0x for hex and 0 for octal, else 297966161Seric * assume decimal; if base is already 16, allow 0x. 298066161Seric */ 298166161Seric do { 298266161Seric c = *s++; 298366161Seric } while (isspace(c)); 298466161Seric if (c == '-') { 298566161Seric neg = 1; 298666161Seric c = *s++; 298766161Seric } else if (c == '+') 298866161Seric c = *s++; 298966161Seric if ((base == 0 || base == 16) && 299066161Seric c == '0' && (*s == 'x' || *s == 'X')) { 299166161Seric c = s[1]; 299266161Seric s += 2; 299366161Seric base = 16; 299466161Seric } 299566161Seric if (base == 0) 299666161Seric base = c == '0' ? 8 : 10; 299764816Seric 299866161Seric /* 299966161Seric * Compute the cutoff value between legal numbers and illegal 300066161Seric * numbers. That is the largest legal value, divided by the 300166161Seric * base. An input number that is greater than this value, if 300266161Seric * followed by a legal input character, is too big. One that 300366161Seric * is equal to this value may be valid or not; the limit 300466161Seric * between valid and invalid numbers is then based on the last 300566161Seric * digit. For instance, if the range for longs is 300666161Seric * [-2147483648..2147483647] and the input base is 10, 300766161Seric * cutoff will be set to 214748364 and cutlim to either 300866161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 300966161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 301066161Seric * the number is too big, and we will return a range error. 301166161Seric * 301266161Seric * Set any if any `digits' consumed; make it negative to indicate 301366161Seric * overflow. 301466161Seric */ 301566161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 301666161Seric cutlim = cutoff % (unsigned long)base; 301766161Seric cutoff /= (unsigned long)base; 301866161Seric for (acc = 0, any = 0;; c = *s++) { 301966161Seric if (isdigit(c)) 302066161Seric c -= '0'; 302166161Seric else if (isalpha(c)) 302266161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 302366161Seric else 302466161Seric break; 302566161Seric if (c >= base) 302666161Seric break; 302766161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 302866161Seric any = -1; 302966161Seric else { 303066161Seric any = 1; 303166161Seric acc *= base; 303266161Seric acc += c; 303364816Seric } 303464816Seric } 303566161Seric if (any < 0) { 303666161Seric acc = neg ? LONG_MIN : LONG_MAX; 303766161Seric errno = ERANGE; 303866161Seric } else if (neg) 303966161Seric acc = -acc; 304066161Seric if (endptr != 0) 304166161Seric *endptr = (char *)(any ? s - 1 : nptr); 304266161Seric return (acc); 304364816Seric } 304464816Seric 304564816Seric #endif 304664841Seric /* 304769679Seric ** STRSTR -- find first substring in string 304869679Seric ** 304969679Seric ** Parameters: 305069679Seric ** big -- the big (full) string. 305169679Seric ** little -- the little (sub) string. 305269679Seric ** 305369679Seric ** Returns: 305469679Seric ** A pointer to the first instance of little in big. 305569679Seric ** big if little is the null string. 305669679Seric ** NULL if little is not contained in big. 305769679Seric */ 305869679Seric 305969679Seric #ifdef NEEDSTRSTR 306069679Seric 306169679Seric char * 306269679Seric strstr(big, little) 306369679Seric char *big; 306469679Seric char *little; 306569679Seric { 306669679Seric register char *p = big; 306769679Seric int l; 306869679Seric 306969679Seric if (*little == '\0') 307069679Seric return big; 307169679Seric l = strlen(little); 307269679Seric 307369679Seric while ((p = strchr(p, *little)) != NULL) 307469679Seric { 307569679Seric if (strncmp(p, little, l) == 0) 307669679Seric return p; 307769679Seric p++; 307869679Seric } 307969679Seric return NULL; 308069679Seric } 308169679Seric 308269679Seric #endif 308369679Seric /* 308468693Seric ** SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 308564841Seric ** 308668693Seric ** Some operating systems have wierd problems with the gethostbyXXX 308768693Seric ** routines. For example, Solaris versions at least through 2.3 308868693Seric ** don't properly deliver a canonical h_name field. This tries to 308968693Seric ** work around these problems. 309064841Seric */ 309164841Seric 309264841Seric struct hostent * 309368693Seric sm_gethostbyname(name) 309468707Seric char *name; 309564841Seric { 309669777Seric struct hostent *h; 309769654Seric #if defined(SOLARIS) && SOLARIS < 204 || defined(sony_news) && defined(__svr4) 309868693Seric extern int h_errno; 309968693Seric 310068508Seric # if SOLARIS == 203 310164841Seric static struct hostent hp; 310264841Seric static char buf[1000]; 310364841Seric extern struct hostent *_switch_gethostbyname_r(); 310464841Seric 310569777Seric if (tTd(61, 10)) 310669777Seric printf("_switch_gethostbyname_r(%s)... ", name); 310769777Seric h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 310864841Seric # else 310965009Seric extern struct hostent *__switch_gethostbyname(); 311065009Seric 311169777Seric if (tTd(61, 10)) 311269777Seric printf("__switch_gethostbyname(%s)... ", name); 311369777Seric h = __switch_gethostbyname(name); 311464841Seric # endif 311568693Seric #else 311669687Seric int nmaps; 311769687Seric char *maptype[MAXMAPSTACK]; 311869687Seric short mapreturn[MAXMAPACTIONS]; 311969687Seric char hbuf[MAXNAME]; 312069687Seric 312169777Seric if (tTd(61, 10)) 312269777Seric printf("gethostbyname(%s)... ", name); 312369687Seric h = gethostbyname(name); 312469777Seric if (h == NULL) 312569687Seric { 312669777Seric if (tTd(61, 10)) 312769777Seric printf("failure\n"); 312869687Seric 312969777Seric nmaps = switch_map_find("hosts", maptype, mapreturn); 313069777Seric while (--nmaps >= 0) 3131*69919Seric if (strcmp(maptype[nmaps], "nis") == 0 || 3132*69919Seric strcmp(maptype[nmaps], "files") == 0) 313369777Seric break; 313469777Seric if (nmaps >= 0) 313569777Seric { 313669777Seric /* try short name */ 313769777Seric if (strlen(name) > sizeof hbuf - 1) 313869777Seric return NULL; 313969777Seric strcpy(hbuf, name); 314069777Seric shorten_hostname(hbuf); 314169777Seric 314269777Seric /* if it hasn't been shortened, there's no point */ 314369777Seric if (strcmp(hbuf, name) != 0) 314469777Seric { 314569777Seric if (tTd(61, 10)) 314669777Seric printf("gethostbyname(%s)... ", hbuf); 314769777Seric h = gethostbyname(hbuf); 314869777Seric } 314969777Seric } 315069687Seric } 315169777Seric #endif 315269777Seric if (tTd(61, 10)) 315369777Seric { 315469777Seric if (h == NULL) 315569777Seric printf("failure\n"); 315669777Seric else 315769777Seric printf("%s\n", h->h_name); 315869777Seric } 315969687Seric return h; 316064841Seric } 316164841Seric 316264841Seric struct hostent * 316368693Seric sm_gethostbyaddr(addr, len, type) 316468707Seric char *addr; 316564841Seric int len; 316664841Seric int type; 316764841Seric { 316868693Seric #if defined(SOLARIS) && SOLARIS < 204 316968693Seric extern int h_errno; 317068693Seric 317168508Seric # if SOLARIS == 203 317264841Seric static struct hostent hp; 317364841Seric static char buf[1000]; 317464841Seric extern struct hostent *_switch_gethostbyaddr_r(); 317564841Seric 317664841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 317764841Seric # else 317865009Seric extern struct hostent *__switch_gethostbyaddr(); 317965009Seric 318064841Seric return __switch_gethostbyaddr(addr, len, type); 318164841Seric # endif 318268693Seric #else 318368693Seric return gethostbyaddr(addr, len, type); 318468693Seric #endif 318564841Seric } 318668693Seric /* 318768693Seric ** SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid 318868693Seric */ 318964841Seric 319068693Seric struct passwd * 319168693Seric sm_getpwnam(user) 319268704Seric char *user; 319368693Seric { 319468693Seric return getpwnam(user); 319568693Seric } 319668693Seric 319768693Seric struct passwd * 319868693Seric sm_getpwuid(uid) 319969865Seric UID_T uid; 320068693Seric { 320168693Seric return getpwuid(uid); 320268693Seric } 320365214Seric /* 320469472Seric ** LOAD_IF_NAMES -- load interface-specific names into $=w 320569472Seric ** 320669472Seric ** Parameters: 320769472Seric ** none. 320869472Seric ** 320969472Seric ** Returns: 321069472Seric ** none. 321169472Seric ** 321269472Seric ** Side Effects: 321369472Seric ** Loads $=w with the names of all the interfaces. 321469472Seric */ 321569472Seric 321669472Seric #ifdef SIOCGIFCONF 321769565Seric struct rtentry; 321869565Seric struct mbuf; 321969472Seric # include <arpa/inet.h> 322069562Seric # include <sys/time.h> 322169472Seric # include <net/if.h> 322269472Seric #endif 322369472Seric 322469472Seric void 322569472Seric load_if_names() 322669472Seric { 322769472Seric #ifdef SIOCGIFCONF 322869472Seric int s; 322969516Seric int i; 323069472Seric struct ifconf ifc; 323169472Seric char interfacebuf[1024]; 323269472Seric 323369472Seric s = socket(AF_INET, SOCK_DGRAM, 0); 323469472Seric if (s == -1) 323569472Seric return; 323669472Seric 323769472Seric /* get the list of known IP address from the kernel */ 323869472Seric ifc.ifc_buf = interfacebuf; 323969516Seric ifc.ifc_len = sizeof interfacebuf; 324069472Seric if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) 324169472Seric { 324269472Seric if (tTd(0, 4)) 324369472Seric printf("SIOGIFCONF failed: %s\n", errstring(errno)); 324469516Seric close(s); 324569472Seric return; 324669472Seric } 324769516Seric close(s); 324869472Seric 324969472Seric /* scan the list of IP address */ 325069865Seric if (tTd(0, 40)) 325169472Seric printf("scanning for interface specific names, ifc_len=%d\n", 325269472Seric ifc.ifc_len); 325369472Seric 325469516Seric for (i = 0; i < ifc.ifc_len; ) 325569516Seric { 325669516Seric struct ifreq *ifr = (struct ifreq *) &ifc.ifc_buf[i]; 325769516Seric struct sockaddr *sa = &ifr->ifr_addr; 325869472Seric struct in_addr ia; 325969516Seric struct hostent *hp; 326069472Seric char ip_addr[256]; 326169516Seric extern char *inet_ntoa(); 326269516Seric extern struct hostent *gethostbyaddr(); 326369472Seric 326469516Seric #ifdef BSD4_4_SOCKADDR 326569516Seric if (sa->sa_len > sizeof ifr->ifr_addr) 326669516Seric i += sizeof ifr->ifr_name + sa->sa_len; 326769516Seric else 326869516Seric #endif 326969516Seric i += sizeof *ifr; 327069516Seric 327169472Seric if (tTd(0, 20)) 327269516Seric printf("%s\n", anynet_ntoa((SOCKADDR *) sa)); 327369472Seric 327469472Seric if (ifr->ifr_addr.sa_family != AF_INET) 327569472Seric continue; 327669472Seric 327769472Seric /* extract IP address from the list*/ 327869516Seric ia = (((struct sockaddr_in *) sa)->sin_addr); 327969472Seric 328069472Seric /* save IP address in text from */ 328169472Seric (void) sprintf(ip_addr, "[%s]", 328269516Seric inet_ntoa(((struct sockaddr_in *) sa)->sin_addr)); 328369472Seric if (!wordinclass(ip_addr, 'w')) 328469472Seric { 328569472Seric setclass('w', ip_addr); 328669472Seric if (tTd(0, 4)) 328769472Seric printf("\ta.k.a.: %s\n", ip_addr); 328869472Seric } 328969472Seric 329069472Seric /* skip "loopback" interface "lo" */ 329169472Seric if (strcmp("lo0", ifr->ifr_name) == 0) 329269472Seric continue; 329369472Seric 329469472Seric /* lookup name with IP address */ 329569472Seric hp = sm_gethostbyaddr((char *) &ia, sizeof(ia), AF_INET); 329669472Seric if (hp == NULL) 329769472Seric { 329869472Seric syslog(LOG_CRIT, "gethostbyaddr() failed for %s\n", 329969472Seric inet_ntoa(ia)); 330069472Seric continue; 330169472Seric } 330269472Seric 330369472Seric /* save its cname */ 330469472Seric if (!wordinclass(hp->h_name, 'w')) 330569472Seric { 330669472Seric setclass('w', hp->h_name); 330769472Seric if (tTd(0, 4)) 330869472Seric printf("\ta.k.a.: %s\n", hp->h_name); 330969472Seric } 331069472Seric 331169472Seric /* save all it aliases name */ 331269472Seric while (*hp->h_aliases) 331369472Seric { 331469472Seric if (!wordinclass(*hp->h_aliases, 'w')) 331569472Seric { 331669472Seric setclass('w', *hp->h_aliases); 331769472Seric if (tTd(0, 4)) 331869472Seric printf("\ta.k.a.: %s\n", *hp->h_aliases); 331969472Seric } 332069472Seric hp->h_aliases++; 332169472Seric } 332269472Seric } 332369472Seric #endif 332469472Seric } 332569472Seric /* 332665214Seric ** NI_PROPVAL -- netinfo property value lookup routine 332765214Seric ** 332865214Seric ** Parameters: 332968508Seric ** keydir -- the Netinfo directory name in which to search 333068508Seric ** for the key. 333168508Seric ** keyprop -- the name of the property in which to find the 333268508Seric ** property we are interested. Defaults to "name". 333368508Seric ** keyval -- the value for which we are really searching. 333468508Seric ** valprop -- the property name for the value in which we 333568508Seric ** are interested. 333668508Seric ** sepchar -- if non-nil, this can be multiple-valued, and 333768508Seric ** we should return a string separated by this 333868508Seric ** character. 333965214Seric ** 334065214Seric ** Returns: 334165214Seric ** NULL -- if: 334265214Seric ** 1. the directory is not found 334365214Seric ** 2. the property name is not found 334465214Seric ** 3. the property contains multiple values 334565214Seric ** 4. some error occured 334665214Seric ** else -- the location of the config file. 334765214Seric ** 334868508Seric ** Example: 334968508Seric ** To search for an alias value, use: 335068508Seric ** ni_propval("/aliases", "name", aliasname, "members", ',') 335168508Seric ** 335265214Seric ** Notes: 335365214Seric ** Caller should free the return value of ni_proval 335465214Seric */ 335565214Seric 335669881Seric #if NETINFO 335765214Seric 335865214Seric # include <netinfo/ni.h> 335965214Seric 336065214Seric # define LOCAL_NETINFO_DOMAIN "." 336165214Seric # define PARENT_NETINFO_DOMAIN ".." 336265214Seric # define MAX_NI_LEVELS 256 336365214Seric 336465214Seric char * 336568508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar) 336668508Seric char *keydir; 336768508Seric char *keyprop; 336868508Seric char *keyval; 336968508Seric char *valprop; 337068508Seric char sepchar; 337165214Seric { 337265820Seric char *propval = NULL; 337365214Seric int i; 337468508Seric int j, alen; 337565214Seric void *ni = NULL; 337665214Seric void *lastni = NULL; 337765214Seric ni_status nis; 337865214Seric ni_id nid; 337965214Seric ni_namelist ninl; 338068508Seric register char *p; 338168508Seric char keybuf[1024]; 338265214Seric 338365214Seric /* 338468508Seric ** Create the full key from the two parts. 338568508Seric ** 338668508Seric ** Note that directory can end with, e.g., "name=" to specify 338768508Seric ** an alternate search property. 338868508Seric */ 338968508Seric 339068508Seric i = strlen(keydir) + strlen(keyval) + 2; 339168508Seric if (keyprop != NULL) 339268508Seric i += strlen(keyprop) + 1; 339368508Seric if (i > sizeof keybuf) 339468508Seric return NULL; 339568508Seric strcpy(keybuf, keydir); 339668508Seric strcat(keybuf, "/"); 339768508Seric if (keyprop != NULL) 339868508Seric { 339968508Seric strcat(keybuf, keyprop); 340068508Seric strcat(keybuf, "="); 340168508Seric } 340268508Seric strcat(keybuf, keyval); 340368508Seric 340468508Seric /* 340565214Seric ** If the passed directory and property name are found 340665214Seric ** in one of netinfo domains we need to search (starting 340765214Seric ** from the local domain moving all the way back to the 340865214Seric ** root domain) set propval to the property's value 340965214Seric ** and return it. 341065214Seric */ 341165214Seric 341265214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 341365214Seric { 341465214Seric if (i == 0) 341565214Seric { 341665214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 341765214Seric } 341865214Seric else 341965214Seric { 342065214Seric if (lastni != NULL) 342165214Seric ni_free(lastni); 342265214Seric lastni = ni; 342365214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 342465214Seric } 342565214Seric 342665214Seric /* 342765214Seric ** Don't bother if we didn't get a handle on a 342865214Seric ** proper domain. This is not necessarily an error. 342965214Seric ** We would get a positive ni_status if, for instance 343065214Seric ** we never found the directory or property and tried 343165214Seric ** to open the parent of the root domain! 343265214Seric */ 343365214Seric 343465214Seric if (nis != 0) 343565214Seric break; 343665214Seric 343765214Seric /* 343865214Seric ** Find the path to the server information. 343965214Seric */ 344065214Seric 344168508Seric if (ni_pathsearch(ni, &nid, keybuf) != 0) 344265214Seric continue; 344365214Seric 344465214Seric /* 344568508Seric ** Find associated value information. 344665214Seric */ 344765214Seric 344868508Seric if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0) 344965214Seric continue; 345065214Seric 345165214Seric /* 345268508Seric ** See if we have an acceptable number of values. 345365214Seric */ 345465214Seric 345568508Seric if (ninl.ni_namelist_len <= 0) 345668508Seric continue; 345768508Seric 345868508Seric if (sepchar == '\0' && ninl.ni_namelist_len > 1) 345965214Seric { 346068508Seric ni_namelist_free(&ninl); 346168508Seric continue; 346265214Seric } 346368508Seric 346469472Seric /* 346568508Seric ** Calculate number of bytes needed and build result 346668508Seric */ 346768508Seric 346868508Seric alen = 1; 346968508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 347068508Seric alen += strlen(ninl.ni_namelist_val[j]) + 1; 347168508Seric propval = p = xalloc(alen); 347268508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 347368508Seric { 347468508Seric strcpy(p, ninl.ni_namelist_val[j]); 347568508Seric p += strlen(p); 347668508Seric *p++ = sepchar; 347769472Seric } 347868508Seric *--p = '\0'; 347968508Seric 348068508Seric ni_namelist_free(&ninl); 348165214Seric } 348265214Seric 348365214Seric /* 348465214Seric ** Clean up. 348565214Seric */ 348665214Seric 348765214Seric if (ni != NULL) 348865214Seric ni_free(ni); 348965214Seric if (lastni != NULL && ni != lastni) 349065214Seric ni_free(lastni); 349165214Seric 349265214Seric return propval; 349365214Seric } 349465214Seric 349565214Seric #endif /* NETINFO */ 349665504Seric /* 349765504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 349865504Seric ** 349965504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 350065504Seric ** syslog succeeds during interrupt handlers. 350165504Seric */ 350265504Seric 350365504Seric #ifdef __hpux 350465504Seric 350565504Seric # define MAXSYSLOGTRIES 100 350665504Seric # undef syslog 350765504Seric 350869758Seric # ifdef __STDC__ 350969748Seric void 351065504Seric hard_syslog(int pri, char *msg, ...) 351165504Seric # else 351265504Seric hard_syslog(pri, msg, va_alist) 351365504Seric int pri; 351465504Seric char *msg; 351565504Seric va_dcl 351665504Seric # endif 351765504Seric { 351865504Seric int i; 351965504Seric char buf[SYSLOG_BUFSIZE * 2]; 352065504Seric VA_LOCAL_DECL; 352165504Seric 352265504Seric VA_START(msg); 352365504Seric vsprintf(buf, msg, ap); 352465504Seric VA_END; 352565504Seric 352665504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 352765504Seric continue; 352865504Seric } 352965504Seric 353065504Seric #endif 353168696Seric /* 353268696Seric ** LOCAL_HOSTNAME_LENGTH 353368696Seric ** 353468696Seric ** This is required to get sendmail to compile against BIND 4.9.x 353568696Seric ** on Ultrix. 353668696Seric */ 353768696Seric 353868696Seric #if defined(ultrix) && NAMED_BIND 353968696Seric 354068696Seric # include <resolv.h> 354168696Seric # if __RES >= 19931104 354268696Seric 354368696Seric int 354468696Seric local_hostname_length(hostname) 354568696Seric char *hostname; 354668696Seric { 354768696Seric int len_host, len_domain; 354868696Seric 354968696Seric if (!*_res.defdname) 355068696Seric res_init(); 355168696Seric len_host = strlen(hostname); 355268696Seric len_domain = strlen(_res.defdname); 355368696Seric if (len_host > len_domain && 355468696Seric (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) && 355568696Seric hostname[len_host - len_domain - 1] == '.') 355668696Seric return len_host - len_domain - 1; 355768696Seric else 355868696Seric return 0; 355968696Seric } 356068696Seric 356168696Seric # endif 356268696Seric #endif 356368741Seric /* 356468741Seric ** Compile-Time options 356568741Seric */ 356668741Seric 356768741Seric char *CompileOptions[] = 356868741Seric { 356968741Seric #if HESIOD 357068741Seric "HESIOD", 357168741Seric #endif 357269883Seric #ifdef LOG 357368741Seric "LOG", 357468741Seric #endif 357568741Seric #if MATCHGECOS 357668741Seric "MATCHGECOS", 357768741Seric #endif 357868741Seric #if NAMED_BIND 357968741Seric "NAMED_BIND", 358068741Seric #endif 358168741Seric #if NDBM 358268741Seric "NDBM", 358368741Seric #endif 358468741Seric #if NETINET 358568741Seric "NETINET", 358668741Seric #endif 358768741Seric #if NETINFO 358868741Seric "NETINFO", 358968741Seric #endif 359068741Seric #if NETISO 359168741Seric "NETISO", 359268741Seric #endif 359368741Seric #if NETNS 359468741Seric "NETNS", 359568741Seric #endif 359668741Seric #if NETUNIX 359768741Seric "NETUNIX", 359868741Seric #endif 359968741Seric #if NETX25 360068741Seric "NETX25", 360168741Seric #endif 360268741Seric #if NEWDB 360368741Seric "NEWDB", 360468741Seric #endif 360568741Seric #if NIS 360668741Seric "NIS", 360768741Seric #endif 360869518Seric #if NISPLUS 360969518Seric "NISPLUS", 361069518Seric #endif 361168741Seric #if SCANF 361268741Seric "SCANF", 361368741Seric #endif 361468741Seric #if SUID_ROOT_FILES_OK 361568741Seric "SUID_ROOT_FILES_OK", 361668741Seric #endif 361768741Seric #if USERDB 361868741Seric "USERDB", 361968741Seric #endif 362068741Seric #if XDEBUG 362168741Seric "XDEBUG", 362268741Seric #endif 362368741Seric #if XLA 362468741Seric "XLA", 362568741Seric #endif 362668741Seric NULL 362768741Seric }; 362869817Seric 362969817Seric 363069817Seric /* 363169817Seric ** OS compile options. 363269817Seric */ 363369817Seric 363469817Seric char *OsCompileOptions[] = 363569817Seric { 363669817Seric #if HASFLOCK 363769817Seric "HASFLOCK", 363869817Seric #endif 363969817Seric #if HASGETUSERSHELL 364069817Seric "HASGETUSERSHELL", 364169817Seric #endif 364269817Seric #if HASINITGROUPS 364369817Seric "HASINITGROUPS", 364469817Seric #endif 364569817Seric #if HASLSTAT 364669817Seric "HASLSTAT", 364769817Seric #endif 364869817Seric #if HASSETREUID 364969817Seric "HASSETREUID", 365069817Seric #endif 365169817Seric #if HASSETSID 365269817Seric "HASSETSID", 365369817Seric #endif 365469817Seric #if HASSETVBUF 365569817Seric "HASSETVBUF", 365669817Seric #endif 365769817Seric #if HASUNAME 365869817Seric "HASUNAME", 365969817Seric #endif 366069817Seric #if IDENTPROTO 366169817Seric "IDENTPROTO", 366269817Seric #endif 366369817Seric #if IP_SRCROUTE 366469817Seric "IP_SRCROUTE", 366569817Seric #endif 366669817Seric #if SYS5SETPGRP 366769817Seric "SYS5SETPGRP", 366869817Seric #endif 366969817Seric #if SYSTEM5 367069817Seric "SYSTEM5", 367169817Seric #endif 367269817Seric #if USESETEUID 367369817Seric "USESETEUID", 367469817Seric #endif 367569817Seric NULL 367669817Seric }; 3677