122698Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 362522Sbostic * Copyright (c) 1988, 1993 462522Sbostic * The Regents of the University of California. All rights reserved. 533728Sbostic * 642825Sbostic * %sccs.include.redist.c% 733728Sbostic */ 822698Sdist 922698Sdist #ifndef lint 10*68695Seric static char sccsid[] = "@(#)conf.c 8.144 (Berkeley) 03/31/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, 9558796Seric 969055Seric NULL, 0, 972897Seric }; 984166Seric 994166Seric 1004166Seric 1014282Seric /* 1024282Seric ** Location of system files/databases/etc. 1034282Seric */ 1044282Seric 10558082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1069039Seric 1079064Seric 1089064Seric 1099039Seric /* 11058082Seric ** Privacy values 11158082Seric */ 11258082Seric 11358082Seric struct prival PrivacyValues[] = 11458082Seric { 11558082Seric "public", PRIV_PUBLIC, 11658082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11758114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11858082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 11958082Seric "noexpn", PRIV_NOEXPN, 12058082Seric "novrfy", PRIV_NOVRFY, 12164333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12264333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12358789Seric "authwarnings", PRIV_AUTHWARNINGS, 12466783Seric "noreceipts", PRIV_NORECEIPTS, 12558082Seric "goaway", PRIV_GOAWAY, 12658789Seric NULL, 0, 12758082Seric }; 12858082Seric 12958082Seric 13058082Seric 13158082Seric /* 13224943Seric ** Miscellaneous stuff. 1339039Seric */ 1349039Seric 13524943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13624943Seric /* 13724943Seric ** SETDEFAULTS -- set default values 13824943Seric ** 13924943Seric ** Because of the way freezing is done, these must be initialized 14024943Seric ** using direct code. 14124943Seric ** 14224943Seric ** Parameters: 14358734Seric ** e -- the default envelope. 14424943Seric ** 14524943Seric ** Returns: 14624943Seric ** none. 14724943Seric ** 14824943Seric ** Side Effects: 14924943Seric ** Initializes a bunch of global variables to their 15024943Seric ** default values. 15124943Seric */ 15224943Seric 15358737Seric #define DAYS * 24 * 60 * 60 15458737Seric 15568508Seric void 15658734Seric setdefaults(e) 15758734Seric register ENVELOPE *e; 15824943Seric { 15968508Seric int i; 16068508Seric extern void inittimeouts(); 16168508Seric extern void setdefuser(); 16268508Seric extern void setupmaps(); 16368508Seric extern void setupmailers(); 16468508Seric 16557438Seric SpaceSub = ' '; /* option B */ 16657438Seric QueueLA = 8; /* option x */ 16757438Seric RefuseLA = 12; /* option X */ 16857438Seric WkRecipFact = 30000L; /* option y */ 16957438Seric WkClassFact = 1800L; /* option z */ 17057438Seric WkTimeFact = 90000L; /* option Z */ 17157438Seric QueueFactor = WkRecipFact * 20; /* option q */ 17263787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 17357438Seric /* option F */ 17457438Seric DefUid = 1; /* option u */ 17557438Seric DefGid = 1; /* option g */ 17657438Seric CheckpointInterval = 10; /* option C */ 17757438Seric MaxHopCount = 25; /* option h */ 17858734Seric e->e_sendmode = SM_FORK; /* option d */ 17958734Seric e->e_errormode = EM_PRINT; /* option e */ 18068508Seric SevenBitInput = FALSE; /* option 7 */ 18157438Seric MaxMciCache = 1; /* option k */ 18257438Seric MciCacheTimeout = 300; /* option K */ 18357438Seric LogLevel = 9; /* option L */ 18468508Seric inittimeouts(NULL); /* option r */ 18558853Seric PrivacyFlags = 0; /* option p */ 18668508Seric MimeMode = MM_CVTMIME|MM_PASS8BIT; /* option 8 */ 18768508Seric for (i = 0; i < MAXTOCLASS; i++) 18868508Seric { 18968508Seric TimeOuts.to_q_return[i] = 5 DAYS; /* option T */ 19068508Seric TimeOuts.to_q_warning[i] = 0; /* option T */ 19168508Seric } 19268508Seric ServiceSwitchFile = "/etc/service.switch"; 19340973Sbostic setdefuser(); 19453654Seric setupmaps(); 19557402Seric setupmailers(); 19624943Seric } 197294Seric 19840973Sbostic 1994326Seric /* 20040973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20140973Sbostic */ 20240973Sbostic 20368508Seric void 20440973Sbostic setdefuser() 20540973Sbostic { 20640973Sbostic struct passwd *defpwent; 20757386Seric static char defuserbuf[40]; 20840973Sbostic 20957386Seric DefUser = defuserbuf; 21068693Seric if ((defpwent = sm_getpwuid(DefUid)) != NULL) 21157386Seric strcpy(defuserbuf, defpwent->pw_name); 21240973Sbostic else 21357386Seric strcpy(defuserbuf, "nobody"); 21440973Sbostic } 21553654Seric /* 21656836Seric ** HOST_MAP_INIT -- initialize host class structures 21756836Seric */ 21856836Seric 21968508Seric bool host_map_init __P((MAP *map, char *args)); 22068508Seric 22156836Seric bool 22260219Seric host_map_init(map, args) 22356836Seric MAP *map; 22456836Seric char *args; 22556836Seric { 22656836Seric register char *p = args; 22756836Seric 22856836Seric for (;;) 22956836Seric { 23058050Seric while (isascii(*p) && isspace(*p)) 23156836Seric p++; 23256836Seric if (*p != '-') 23356836Seric break; 23456836Seric switch (*++p) 23556836Seric { 23656836Seric case 'a': 23756836Seric map->map_app = ++p; 23856836Seric break; 23956836Seric } 24058050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 24156836Seric p++; 24256836Seric if (*p != '\0') 24356836Seric *p++ = '\0'; 24456836Seric } 24556836Seric if (map->map_app != NULL) 24656836Seric map->map_app = newstr(map->map_app); 24756836Seric return TRUE; 24856836Seric } 24957402Seric /* 25057402Seric ** SETUPMAILERS -- initialize default mailers 25157402Seric */ 25256836Seric 25368508Seric void 25457402Seric setupmailers() 25557402Seric { 25657402Seric char buf[100]; 25768508Seric extern void makemailer(); 25857402Seric 25968508Seric strcpy(buf, "prog, P=/bin/sh, F=lsoD, A=sh -c $u"); 26057403Seric makemailer(buf); 26157403Seric 26268508Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEou, A=FILE"); 26357402Seric makemailer(buf); 26457402Seric 26557402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 26657402Seric makemailer(buf); 26757402Seric } 26856836Seric /* 26960207Seric ** SETUPMAPS -- set up map classes 27060207Seric */ 27160207Seric 27260207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 27360207Seric { \ 27460207Seric extern bool parse __P((MAP *, char *)); \ 27560207Seric extern bool open __P((MAP *, int)); \ 27660207Seric extern void close __P((MAP *)); \ 27760207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 27860207Seric extern void store __P((MAP *, char *, char *)); \ 27960207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 28060207Seric s->s_mapclass.map_cname = name; \ 28160207Seric s->s_mapclass.map_ext = ext; \ 28260207Seric s->s_mapclass.map_cflags = flags; \ 28360207Seric s->s_mapclass.map_parse = parse; \ 28460207Seric s->s_mapclass.map_open = open; \ 28560207Seric s->s_mapclass.map_close = close; \ 28660207Seric s->s_mapclass.map_lookup = lookup; \ 28760207Seric s->s_mapclass.map_store = store; \ 28860207Seric } 28960207Seric 29068508Seric void 29160207Seric setupmaps() 29260207Seric { 29360207Seric register STAB *s; 29460207Seric 29560207Seric #ifdef NEWDB 29660207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29760207Seric map_parseargs, hash_map_open, db_map_close, 29860207Seric db_map_lookup, db_map_store); 29968508Seric 30060207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 30160207Seric map_parseargs, bt_map_open, db_map_close, 30260207Seric db_map_lookup, db_map_store); 30360207Seric #endif 30460207Seric 30560207Seric #ifdef NDBM 30660207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 30760207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 30860207Seric ndbm_map_lookup, ndbm_map_store); 30960207Seric #endif 31060207Seric 31160207Seric #ifdef NIS 31260207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 31368508Seric map_parseargs, nis_map_open, null_map_close, 31468508Seric nis_map_lookup, null_map_store); 31560207Seric #endif 31660207Seric 31768508Seric #ifdef NISPLUS 31868508Seric MAPDEF("nisplus", NULL, MCF_ALIASOK, 31968508Seric map_parseargs, nisplus_map_open, null_map_close, 32068508Seric nisplus_map_lookup, null_map_store); 32168508Seric #endif 32268508Seric 32368508Seric #ifdef HESIOD 32468508Seric MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY, 32568508Seric map_parseargs, null_map_open, null_map_close, 32668508Seric hes_map_lookup, null_map_store); 32768508Seric #endif 32868508Seric 32968508Seric #ifdef NETINFO 33068508Seric MAPDEF("netinfo", NULL, MCF_ALIASOK, 33168508Seric map_parseargs, ni_map_open, null_map_close, 33268508Seric ni_map_lookup, null_map_store); 33368508Seric #endif 33468508Seric 33568508Seric #if 0 33668508Seric MAPDEF("dns", NULL, 0, 33768508Seric dns_map_init, null_map_open, null_map_close, 33868508Seric dns_map_lookup, null_map_store); 33968508Seric #endif 34068508Seric 34168508Seric #if NAMED_BIND 34268508Seric /* best MX DNS lookup */ 34368508Seric MAPDEF("bestmx", NULL, MCF_OPTFILE, 34468508Seric map_parseargs, null_map_open, null_map_close, 34568508Seric bestmx_map_lookup, null_map_store); 34668508Seric #endif 34768508Seric 34868508Seric MAPDEF("host", NULL, 0, 34968508Seric host_map_init, null_map_open, null_map_close, 35068508Seric host_map_lookup, null_map_store); 35168508Seric 35268508Seric MAPDEF("text", NULL, MCF_ALIASOK, 35368508Seric map_parseargs, text_map_open, null_map_close, 35468508Seric text_map_lookup, null_map_store); 35568508Seric 35660207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 35768508Seric map_parseargs, stab_map_open, null_map_close, 35860207Seric stab_map_lookup, stab_map_store); 35960207Seric 36060207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 36160207Seric map_parseargs, impl_map_open, impl_map_close, 36260207Seric impl_map_lookup, impl_map_store); 36360207Seric 36468508Seric /* access to system passwd file */ 36568508Seric MAPDEF("user", NULL, MCF_OPTFILE, 36668508Seric map_parseargs, user_map_open, null_map_close, 36768508Seric user_map_lookup, null_map_store); 36860207Seric 36960207Seric /* dequote map */ 37060207Seric MAPDEF("dequote", NULL, 0, 37160207Seric dequote_init, null_map_open, null_map_close, 37260207Seric dequote_map, null_map_store); 37360207Seric 37468521Seric #ifdef USERDB 37560207Seric /* user database */ 37668521Seric MAPDEF("userdb", ".db", 0, 37768521Seric map_parseargs, null_map_open, null_map_close, 37860207Seric udb_map_lookup, null_map_store); 37960207Seric #endif 38068508Seric 38168508Seric /* sequenced maps */ 38268508Seric MAPDEF("sequence", NULL, MCF_ALIASOK, 38368508Seric seq_map_parse, null_map_open, null_map_close, 38468508Seric seq_map_lookup, seq_map_store); 38568508Seric 38668508Seric /* switched interface to sequenced maps */ 38768508Seric MAPDEF("switch", NULL, MCF_ALIASOK, 38868508Seric map_parseargs, switch_map_open, null_map_close, 38968508Seric seq_map_lookup, seq_map_store); 39060207Seric } 39160207Seric 39260207Seric #undef MAPDEF 39360207Seric /* 39468508Seric ** INITHOSTMAPS -- initial host-dependent maps 39568508Seric ** 39668508Seric ** This should act as an interface to any local service switch 39768508Seric ** provided by the host operating system. 39868508Seric ** 39968508Seric ** Parameters: 40068508Seric ** none 40168508Seric ** 40268508Seric ** Returns: 40368508Seric ** none 40468508Seric ** 40568508Seric ** Side Effects: 40668508Seric ** Should define maps "host" and "users" as necessary 40768508Seric ** for this OS. If they are not defined, they will get 40868508Seric ** a default value later. It should check to make sure 40968508Seric ** they are not defined first, since it's possible that 41068508Seric ** the config file has provided an override. 41168508Seric */ 41268508Seric 41368508Seric void 41468508Seric inithostmaps() 41568508Seric { 41668508Seric register int i; 41768508Seric int nmaps; 41868508Seric char *maptype[MAXMAPSTACK]; 41968508Seric short mapreturn[MAXMAPACTIONS]; 42068508Seric char buf[MAXLINE]; 42168508Seric 42268508Seric /* 42368508Seric ** Set up default hosts maps. 42468508Seric */ 42568508Seric 42668508Seric #if 0 42768508Seric nmaps = switch_map_find("hosts", maptype, mapreturn); 42868508Seric for (i = 0; i < nmaps; i++) 42968508Seric { 43068508Seric if (strcmp(maptype[i], "files") == 0 && 43168508Seric stab("hosts.files", ST_MAP, ST_FIND) == NULL) 43268508Seric { 43368508Seric strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts"); 43468508Seric makemapentry(buf); 43568508Seric } 43668508Seric #if NAMED_BIND 43768508Seric else if (strcmp(maptype[i], "dns") == 0 && 43868508Seric stab("hosts.dns", ST_MAP, ST_FIND) == NULL) 43968508Seric { 44068508Seric strcpy(buf, "hosts.dns dns A"); 44168508Seric makemapentry(buf); 44268508Seric } 44368508Seric #endif 44468508Seric #ifdef NISPLUS 44568508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 44668508Seric stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL) 44768508Seric { 44868508Seric strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir"); 44968508Seric makemapentry(buf); 45068508Seric } 45168508Seric #endif 45268508Seric #ifdef NIS 45368508Seric else if (strcmp(maptype[i], "nis") == 0 && 45468508Seric stab("hosts.nis", ST_MAP, ST_FIND) == NULL) 45568508Seric { 45668508Seric strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname"); 45768508Seric makemapentry(buf); 45868508Seric } 45968508Seric #endif 46068508Seric } 46168508Seric #endif 46268508Seric 46368508Seric /* 46468508Seric ** Make sure we have a host map. 46568508Seric */ 46668508Seric 46768508Seric if (stab("host", ST_MAP, ST_FIND) == NULL) 46868508Seric { 46968508Seric /* user didn't initialize: set up host map */ 47068508Seric strcpy(buf, "host host"); 47168508Seric #if NAMED_BIND 47268508Seric if (ConfigLevel >= 2) 47368508Seric strcat(buf, " -a."); 47468508Seric #endif 47568508Seric makemapentry(buf); 47668508Seric } 47768508Seric 47868508Seric /* 47968508Seric ** Set up default aliases maps 48068508Seric */ 48168508Seric 48268508Seric nmaps = switch_map_find("aliases", maptype, mapreturn); 48368508Seric for (i = 0; i < nmaps; i++) 48468508Seric { 48568508Seric if (strcmp(maptype[i], "files") == 0 && 48668508Seric stab("aliases.files", ST_MAP, ST_FIND) == NULL) 48768508Seric { 48868508Seric strcpy(buf, "aliases.files implicit /etc/aliases"); 48968508Seric makemapentry(buf); 49068508Seric } 49168508Seric #ifdef NISPLUS 49268508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 49368508Seric stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL) 49468508Seric { 49568508Seric strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir"); 49668508Seric makemapentry(buf); 49768508Seric } 49868508Seric #endif 49968508Seric #ifdef NIS 50068508Seric else if (strcmp(maptype[i], "nis") == 0 && 50168508Seric stab("aliases.nis", ST_MAP, ST_FIND) == NULL) 50268508Seric { 50368508Seric strcpy(buf, "aliases.nis nis -d mail.aliases"); 50468508Seric makemapentry(buf); 50568508Seric } 50668508Seric #endif 50768508Seric } 50868508Seric if (stab("aliases", ST_MAP, ST_FIND) == NULL) 50968508Seric { 51068508Seric strcpy(buf, "aliases switch aliases"); 51168508Seric makemapentry(buf); 51268508Seric } 51368508Seric strcpy(buf, "switch:aliases"); 51468508Seric setalias(buf); 51568508Seric 51668508Seric #if 0 /* "user" map class is a better choice */ 51768508Seric /* 51868508Seric ** Set up default users maps. 51968508Seric */ 52068508Seric 52168508Seric nmaps = switch_map_find("passwd", maptype, mapreturn); 52268508Seric for (i = 0; i < nmaps; i++) 52368508Seric { 52468508Seric if (strcmp(maptype[i], "files") == 0 && 52568508Seric stab("users.files", ST_MAP, ST_FIND) == NULL) 52668508Seric { 52768508Seric strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd"); 52868508Seric makemapentry(buf); 52968508Seric } 53068508Seric #ifdef NISPLUS 53168508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 53268508Seric stab("users.nisplus", ST_MAP, ST_FIND) == NULL) 53368508Seric { 53468508Seric strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir"); 53568508Seric makemapentry(buf); 53668508Seric } 53768508Seric #endif 53868508Seric #ifdef NIS 53968508Seric else if (strcmp(maptype[i], "nis") == 0 && 54068508Seric stab("users.nis", ST_MAP, ST_FIND) == NULL) 54168508Seric { 54268508Seric strcpy(buf, "users.nis nis -m -d passwd.byname"); 54368508Seric makemapentry(buf); 54468508Seric } 54568508Seric #endif 54668508Seric #ifdef HESIOD 54768508Seric else if (strcmp(maptype[i], "hesiod") == 0) && 54868508Seric stab("users.hesiod", ST_MAP, ST_FIND) == NULL) 54968508Seric { 55068508Seric strcpy(buf, "users.hesiod hesiod"); 55168508Seric makemapentry(buf); 55268508Seric } 55368508Seric #endif 55468508Seric } 55568508Seric if (stab("users", ST_MAP, ST_FIND) == NULL) 55668508Seric { 55768508Seric strcpy(buf, "users switch -m passwd"); 55868508Seric makemapentry(buf); 55968508Seric } 56068508Seric #endif 56168508Seric } 56268508Seric /* 56368508Seric ** SWITCH_MAP_FIND -- find the list of types associated with a map 56468508Seric ** 56568508Seric ** This is the system-dependent interface to the service switch. 56668508Seric ** 56768508Seric ** Parameters: 56868508Seric ** service -- the name of the service of interest. 56968508Seric ** maptype -- an out-array of strings containing the types 57068508Seric ** of access to use for this service. There can 57168508Seric ** be at most MAXMAPSTACK types for a single service. 57268508Seric ** mapreturn -- an out-array of return information bitmaps 57368508Seric ** for the map. 57468508Seric ** 57568508Seric ** Returns: 57668508Seric ** The number of map types filled in, or -1 for failure. 57768508Seric */ 57868508Seric 57968508Seric #ifdef SOLARIS 58068508Seric # include <nsswitch.h> 58168508Seric #endif 58268508Seric 58368508Seric #if defined(ultrix) || defined(__osf__) 58468508Seric # include <sys/svcinfo.h> 58568508Seric #endif 58668508Seric 58768508Seric int 58868508Seric switch_map_find(service, maptype, mapreturn) 58968508Seric char *service; 59068508Seric char *maptype[MAXMAPSTACK]; 59168508Seric short mapreturn[MAXMAPACTIONS]; 59268508Seric { 59368508Seric register FILE *fp; 59468508Seric int svcno; 59568508Seric static char buf[MAXLINE]; 59668508Seric 59768508Seric #ifdef SOLARIS 59868508Seric struct __nsw_switchconfig *nsw_conf; 59968508Seric enum __nsw_parse_err pserr; 60068508Seric struct __nsw_lookup *lk; 60168508Seric int nsw_rc; 60268508Seric static struct __nsw_lookup lkp0 = 60368508Seric { "files", {1, 0, 0, 0}, NULL, NULL }; 60468508Seric static struct __nsw_switchconfig lkp_default = 60568508Seric { 0, "sendmail", 3, &lkp0 }; 60668508Seric 60768508Seric if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL) 60868508Seric lk = lkp_default.lookups; 60968508Seric else 61068508Seric lk = nsw_conf->lookups; 61168508Seric svcno = 0; 61268508Seric while (lk != NULL) 61368508Seric { 61468508Seric maptype[svcno] = lk->service_name; 61568508Seric if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN) 61668508Seric mapreturn[MA_NOTFOUND] |= 1 << svcno; 61768508Seric if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN) 61868508Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 61968508Seric if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN) 62068508Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 62168508Seric svcno++; 62268508Seric lk = lk->next; 62368508Seric } 62468508Seric return svcno; 62568508Seric #endif 62668508Seric 62768508Seric #if defined(ultrix) || defined(__osf__) 62868508Seric struct svcinfo *svcinfo; 62968508Seric int svc; 63068508Seric 63168508Seric svcinfo = getsvc(); 63268508Seric if (svcinfo == NULL) 63368508Seric goto punt; 63468508Seric if (strcmp(service, "hosts") == 0) 63568508Seric svc = SVC_HOSTS; 63668508Seric else if (strcmp(service, "aliases") == 0) 63768508Seric svc = SVC_ALIASES; 63868508Seric else if (strcmp(service, "passwd") == 0) 63968508Seric svc = SVC_PASSWD; 64068508Seric else 64168508Seric return -1; 64268508Seric for (svcno = 0; svcno < SVC_PATHSIZE; svcno++) 64368508Seric { 64468508Seric switch (svcinfo->svcpath[svc][svcno]) 64568508Seric { 64668508Seric case SVC_LOCAL: 64768508Seric maptype[svcno] = "files"; 64868508Seric break; 64968508Seric 65068508Seric case SVC_YP: 65168508Seric maptype[svcno] = "nis"; 65268508Seric break; 65368508Seric 65468508Seric case SVC_BIND: 65568508Seric maptype[svcno] = "dns"; 65668508Seric break; 65768508Seric 65868508Seric #ifdef SVC_HESIOD 65968508Seric case SVC_HESIOD: 66068508Seric maptype[svcno] = "hesiod"; 66168508Seric break; 66268508Seric #endif 66368508Seric 66468508Seric case SVC_LAST: 66568508Seric return svcno; 66668508Seric } 66768508Seric } 66868508Seric return svcno; 66968508Seric #endif 67068508Seric 67168508Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__) 67268508Seric /* 67368508Seric ** Fall-back mechanism. 67468508Seric */ 67568508Seric 67668508Seric svcno = 0; 67768508Seric fp = fopen(ServiceSwitchFile, "r"); 67868508Seric if (fp != NULL) 67968508Seric { 68068508Seric while (fgets(buf, sizeof buf, fp) != NULL) 68168508Seric { 68268508Seric register char *p; 68368508Seric 68468508Seric p = strpbrk(buf, "#\n"); 68568508Seric if (p != NULL) 68668508Seric *p = '\0'; 68768508Seric p = strpbrk(buf, " \t"); 68868508Seric if (p != NULL) 68968508Seric *p++ = '\0'; 69068508Seric if (strcmp(buf, service) != 0) 69168508Seric continue; 69268508Seric 69368508Seric /* got the right service -- extract data */ 69468508Seric do 69568508Seric { 69668508Seric while (isspace(*p)) 69768508Seric p++; 69868508Seric if (*p == '\0') 69968508Seric break; 70068508Seric maptype[svcno++] = p; 70168508Seric p = strpbrk(p, " \t"); 70268508Seric if (p != NULL) 70368508Seric *p++ = '\0'; 70468508Seric } while (p != NULL); 70568508Seric break; 70668508Seric } 70768508Seric fclose(fp); 70868508Seric return svcno; 70968508Seric } 71068508Seric #endif 71168508Seric 71268508Seric /* if the service file doesn't work, use an absolute fallback */ 71368508Seric punt: 71468508Seric if (strcmp(service, "aliases") == 0) 71568508Seric { 71668628Seric maptype[svcno++] = "files"; 71768628Seric #ifdef AUTO_NIS_ALIASES 71868628Seric # ifdef NISPLUS 71968628Seric maptype[svcno++] = "nisplus"; 72068628Seric # endif 72168628Seric # ifdef NIS 72268628Seric maptype[svcno++] = "nis"; 72368628Seric # endif 72468628Seric #endif 72568628Seric return svcno; 72668508Seric } 72768508Seric if (strcmp(service, "hosts") == 0) 72868508Seric { 72968508Seric # if NAMED_BIND 73068508Seric maptype[svcno++] = "dns"; 73168508Seric # else 73268508Seric # if defined(sun) && !defined(BSD) && !defined(SOLARIS) 73368508Seric /* SunOS */ 73468508Seric maptype[svcno++] = "nis"; 73568508Seric # endif 73668508Seric # endif 73768508Seric maptype[svcno++] = "files"; 73868508Seric return svcno; 73968508Seric } 74068508Seric return -1; 74168508Seric } 74268508Seric /* 7439369Seric ** USERNAME -- return the user id of the logged in user. 7449369Seric ** 7459369Seric ** Parameters: 7469369Seric ** none. 7479369Seric ** 7489369Seric ** Returns: 7499369Seric ** The login name of the logged in user. 7509369Seric ** 7519369Seric ** Side Effects: 7529369Seric ** none. 7539369Seric ** 7549369Seric ** Notes: 7559369Seric ** The return value is statically allocated. 7569369Seric */ 7579369Seric 7589369Seric char * 7599369Seric username() 7609369Seric { 76117469Seric static char *myname = NULL; 7629369Seric extern char *getlogin(); 76319904Smiriam register struct passwd *pw; 7649369Seric 76517469Seric /* cache the result */ 76617469Seric if (myname == NULL) 76717469Seric { 76817469Seric myname = getlogin(); 76917469Seric if (myname == NULL || myname[0] == '\0') 77017469Seric { 77168693Seric pw = sm_getpwuid(RealUid); 77217469Seric if (pw != NULL) 77340993Sbostic myname = newstr(pw->pw_name); 77417469Seric } 77519904Smiriam else 77619904Smiriam { 77763787Seric uid_t uid = RealUid; 77819873Smiriam 77940993Sbostic myname = newstr(myname); 78068693Seric if ((pw = sm_getpwnam(myname)) == NULL || 78158736Seric (uid != 0 && uid != pw->pw_uid)) 78219904Smiriam { 78368693Seric pw = sm_getpwuid(uid); 78424945Seric if (pw != NULL) 78540993Sbostic myname = newstr(pw->pw_name); 78619873Smiriam } 78719873Smiriam } 78817469Seric if (myname == NULL || myname[0] == '\0') 78917469Seric { 79058151Seric syserr("554 Who are you?"); 79117469Seric myname = "postmaster"; 79217469Seric } 79317469Seric } 79417469Seric 79517469Seric return (myname); 7969369Seric } 7979369Seric /* 7984190Seric ** TTYPATH -- Get the path of the user's tty 799294Seric ** 800294Seric ** Returns the pathname of the user's tty. Returns NULL if 801294Seric ** the user is not logged in or if s/he has write permission 802294Seric ** denied. 803294Seric ** 804294Seric ** Parameters: 805294Seric ** none 806294Seric ** 807294Seric ** Returns: 808294Seric ** pathname of the user's tty. 809294Seric ** NULL if not logged in or write permission denied. 810294Seric ** 811294Seric ** Side Effects: 812294Seric ** none. 813294Seric ** 814294Seric ** WARNING: 815294Seric ** Return value is in a local buffer. 816294Seric ** 817294Seric ** Called By: 818294Seric ** savemail 819294Seric */ 820294Seric 821294Seric char * 822294Seric ttypath() 823294Seric { 824294Seric struct stat stbuf; 825294Seric register char *pathn; 826294Seric extern char *ttyname(); 8274081Seric extern char *getlogin(); 828294Seric 829294Seric /* compute the pathname of the controlling tty */ 8309369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 8319369Seric (pathn = ttyname(0)) == NULL) 832294Seric { 833294Seric errno = 0; 834294Seric return (NULL); 835294Seric } 836294Seric 837294Seric /* see if we have write permission */ 8382967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 839294Seric { 840294Seric errno = 0; 841294Seric return (NULL); 842294Seric } 843294Seric 844294Seric /* see if the user is logged in */ 845294Seric if (getlogin() == NULL) 846294Seric return (NULL); 847294Seric 848294Seric /* looks good */ 849294Seric return (pathn); 850294Seric } 8512967Seric /* 8522967Seric ** CHECKCOMPAT -- check for From and To person compatible. 8532967Seric ** 8542967Seric ** This routine can be supplied on a per-installation basis 8552967Seric ** to determine whether a person is allowed to send a message. 8562967Seric ** This allows restriction of certain types of internet 8572967Seric ** forwarding or registration of users. 8582967Seric ** 8592967Seric ** If the hosts are found to be incompatible, an error 86057454Seric ** message should be given using "usrerr" and 0 should 8612967Seric ** be returned. 8622967Seric ** 86368559Seric ** EF_NO_BODY_RETN can be set in e->e_flags to suppress the 86468559Seric ** body during the return-to-sender function; this should be done 86568559Seric ** on huge messages. This bit may already be set by the ESMTP 86668559Seric ** protocol. 8674288Seric ** 8682967Seric ** Parameters: 8692967Seric ** to -- the person being sent to. 8702967Seric ** 8712967Seric ** Returns: 87257459Seric ** an exit status 8732967Seric ** 8742967Seric ** Side Effects: 8752967Seric ** none (unless you include the usrerr stuff) 8762967Seric */ 8772967Seric 87868508Seric int 87955012Seric checkcompat(to, e) 8802967Seric register ADDRESS *to; 88155012Seric register ENVELOPE *e; 8822967Seric { 88312133Seric # ifdef lint 88412133Seric if (to == NULL) 88512133Seric to++; 88663834Seric # endif /* lint */ 88764402Seric 88864402Seric if (tTd(49, 1)) 88964402Seric printf("checkcompat(to=%s, from=%s)\n", 89064402Seric to->q_paddr, e->e_from.q_paddr); 89164402Seric 89210698Seric # ifdef EXAMPLE_CODE 89310698Seric /* this code is intended as an example only */ 8944437Seric register STAB *s; 8954437Seric 8964437Seric s = stab("arpa", ST_MAILER, ST_FIND); 89768508Seric if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 && 8989369Seric to->q_mailer == s->s_mailer) 8994437Seric { 90058151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 90168559Seric /* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */ 90257459Seric return (EX_UNAVAILABLE); 9034437Seric } 90456795Seric # endif /* EXAMPLE_CODE */ 90557459Seric return (EX_OK); 9062967Seric } 9079369Seric /* 90864035Seric ** SETSIGNAL -- set a signal handler 90964035Seric ** 91064035Seric ** This is essentially old BSD "signal(3)". 91164035Seric */ 91264035Seric 91364561Seric sigfunc_t 91464035Seric setsignal(sig, handler) 91564035Seric int sig; 91664561Seric sigfunc_t handler; 91764035Seric { 91864380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 91964035Seric return signal(sig, handler); 92064035Seric #else 92164035Seric struct sigaction n, o; 92264035Seric 92364035Seric bzero(&n, sizeof n); 92464035Seric n.sa_handler = handler; 92568508Seric # ifdef SA_RESTART 92668508Seric n.sa_flags = SA_RESTART; 92768508Seric # endif 92864035Seric if (sigaction(sig, &n, &o) < 0) 92964035Seric return SIG_ERR; 93064035Seric return o.sa_handler; 93164035Seric #endif 93264035Seric } 93364035Seric /* 9349369Seric ** HOLDSIGS -- arrange to hold all signals 9359369Seric ** 9369369Seric ** Parameters: 9379369Seric ** none. 9389369Seric ** 9399369Seric ** Returns: 9409369Seric ** none. 9419369Seric ** 9429369Seric ** Side Effects: 9439369Seric ** Arranges that signals are held. 9449369Seric */ 9459369Seric 94668508Seric void 9479369Seric holdsigs() 9489369Seric { 9499369Seric } 9509369Seric /* 9519369Seric ** RLSESIGS -- arrange to release all signals 9529369Seric ** 9539369Seric ** This undoes the effect of holdsigs. 9549369Seric ** 9559369Seric ** Parameters: 9569369Seric ** none. 9579369Seric ** 9589369Seric ** Returns: 9599369Seric ** none. 9609369Seric ** 9619369Seric ** Side Effects: 9629369Seric ** Arranges that signals are released. 9639369Seric */ 9649369Seric 96568508Seric void 9669369Seric rlsesigs() 9679369Seric { 9689369Seric } 96914872Seric /* 97064705Seric ** INIT_MD -- do machine dependent initializations 97164705Seric ** 97264705Seric ** Systems that have global modes that should be set should do 97364705Seric ** them here rather than in main. 97464705Seric */ 97564705Seric 97664705Seric #ifdef _AUX_SOURCE 97764705Seric # include <compat.h> 97864705Seric #endif 97964705Seric 98068508Seric void 98164825Seric init_md(argc, argv) 98264825Seric int argc; 98364825Seric char **argv; 98464705Seric { 98564705Seric #ifdef _AUX_SOURCE 98664705Seric setcompat(getcompat() | COMPAT_BSDPROT); 98764705Seric #endif 98868508Seric 98968508Seric #ifdef VENDOR_DEFAULT 99068508Seric VendorCode = VENDOR_DEFAULT; 99168508Seric #else 99268508Seric VendorCode = VENDOR_BERKELEY; 99368508Seric #endif 99464705Seric } 99564705Seric /* 99668508Seric ** INIT_VENDOR_MACROS -- vendor-dependent macro initializations 99768508Seric ** 99868508Seric ** Called once, on startup. 99968508Seric ** 100068508Seric ** Parameters: 100168508Seric ** e -- the global envelope. 100268508Seric ** 100368508Seric ** Returns: 100468508Seric ** none. 100568508Seric ** 100668508Seric ** Side Effects: 100768508Seric ** vendor-dependent. 100868508Seric */ 100968508Seric 101068508Seric void 101168508Seric init_vendor_macros(e) 101268508Seric register ENVELOPE *e; 101368508Seric { 101468508Seric } 101568508Seric /* 101614872Seric ** GETLA -- get the current load average 101714872Seric ** 101814881Seric ** This code stolen from la.c. 101914881Seric ** 102014872Seric ** Parameters: 102114872Seric ** none. 102214872Seric ** 102314872Seric ** Returns: 102414872Seric ** The current load average as an integer. 102514872Seric ** 102614872Seric ** Side Effects: 102714872Seric ** none. 102814872Seric */ 102914872Seric 103051920Seric /* try to guess what style of load average we have */ 103151920Seric #define LA_ZERO 1 /* always return load average as zero */ 103264360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 103351920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 103451920Seric #define LA_SUBR 4 /* call getloadavg */ 103564295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 103664360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 103766301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 103814872Seric 103963962Seric /* do guesses based on general OS type */ 104051920Seric #ifndef LA_TYPE 104163962Seric # define LA_TYPE LA_ZERO 104251920Seric #endif 104351920Seric 104464360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 104551920Seric 104614872Seric #include <nlist.h> 104714872Seric 104868508Seric #ifdef IRIX64 104968508Seric # define nlist nlist64 105068508Seric #endif 105168508Seric 105251920Seric #ifndef LA_AVENRUN 105363962Seric # ifdef SYSTEM5 105463962Seric # define LA_AVENRUN "avenrun" 105563962Seric # else 105663962Seric # define LA_AVENRUN "_avenrun" 105763962Seric # endif 105851920Seric #endif 105951920Seric 106051920Seric /* _PATH_UNIX should be defined in <paths.h> */ 106151920Seric #ifndef _PATH_UNIX 106263962Seric # if defined(SYSTEM5) 106363962Seric # define _PATH_UNIX "/unix" 106463962Seric # else 106563962Seric # define _PATH_UNIX "/vmunix" 106663962Seric # endif 106751920Seric #endif 106851920Seric 106914872Seric struct nlist Nl[] = 107014872Seric { 107151920Seric { LA_AVENRUN }, 107214872Seric #define X_AVENRUN 0 107314872Seric { 0 }, 107414872Seric }; 107514872Seric 107660583Seric #ifndef FSHIFT 107760583Seric # if defined(unixpc) 107860583Seric # define FSHIFT 5 107960583Seric # endif 108057736Seric 108166763Seric # if defined(__alpha) || defined(IRIX) 108260583Seric # define FSHIFT 10 108360583Seric # endif 108468508Seric 108568508Seric # if defined(_AIX3) 108668508Seric # define FSHIFT 16 108768508Seric # endif 108866788Seric #endif 108957977Seric 109066788Seric #ifndef FSHIFT 109166788Seric # define FSHIFT 8 109257736Seric #endif 109360583Seric 109466788Seric #ifndef FSCALE 109566788Seric # define FSCALE (1 << FSHIFT) 109651920Seric #endif 109740930Srick 109814872Seric getla() 109914872Seric { 110014872Seric static int kmem = -1; 110151920Seric #if LA_TYPE == LA_INT 110224943Seric long avenrun[3]; 110351920Seric #else 110464360Seric # if LA_TYPE == LA_SHORT 110564360Seric short avenrun[3]; 110664360Seric # else 110751920Seric double avenrun[3]; 110864360Seric # endif 110951920Seric #endif 111025615Seric extern off_t lseek(); 111157736Seric extern int errno; 111214872Seric 111314872Seric if (kmem < 0) 111414872Seric { 111524945Seric kmem = open("/dev/kmem", 0, 0); 111614872Seric if (kmem < 0) 111757736Seric { 111857736Seric if (tTd(3, 1)) 111957736Seric printf("getla: open(/dev/kmem): %s\n", 112057736Seric errstring(errno)); 112114872Seric return (-1); 112257736Seric } 112351920Seric (void) fcntl(kmem, F_SETFD, 1); 112468508Seric #ifdef _AIX3 112568508Seric if (knlist(Nl, 1, sizeof Nl[0]) < 0) 112668508Seric #else 112757736Seric if (nlist(_PATH_UNIX, Nl) < 0) 112868508Seric #endif 112957736Seric { 113057736Seric if (tTd(3, 1)) 113157736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 113257736Seric errstring(errno)); 113314872Seric return (-1); 113457736Seric } 113559253Seric if (Nl[X_AVENRUN].n_value == 0) 113659253Seric { 113759253Seric if (tTd(3, 1)) 113859253Seric printf("getla: nlist(%s, %s) ==> 0\n", 113959253Seric _PATH_UNIX, LA_AVENRUN); 114059253Seric return (-1); 114159253Seric } 114268508Seric #ifdef NAMELISTMASK 114368508Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 114466763Seric #endif 114514872Seric } 114657736Seric if (tTd(3, 20)) 114757736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 114868508Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 || 114923118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 115019967Seric { 115119967Seric /* thank you Ian */ 115257736Seric if (tTd(3, 1)) 115357736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 115419967Seric return (-1); 115519967Seric } 115664360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 115757736Seric if (tTd(3, 5)) 115857736Seric { 115957736Seric printf("getla: avenrun = %d", avenrun[0]); 116057736Seric if (tTd(3, 15)) 116157736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 116257736Seric printf("\n"); 116357736Seric } 116457736Seric if (tTd(3, 1)) 116557736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 116624943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 116751920Seric #else 116857736Seric if (tTd(3, 5)) 116957736Seric { 117057736Seric printf("getla: avenrun = %g", avenrun[0]); 117157736Seric if (tTd(3, 15)) 117257736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 117357736Seric printf("\n"); 117457736Seric } 117557736Seric if (tTd(3, 1)) 117657736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 117751920Seric return ((int) (avenrun[0] + 0.5)); 117851920Seric #endif 117914872Seric } 118014872Seric 118151773Seric #else 118251920Seric #if LA_TYPE == LA_SUBR 118351773Seric 118464718Seric #ifdef DGUX 118564718Seric 118664718Seric #include <sys/dg_sys_info.h> 118764718Seric 118868508Seric int 118968508Seric getla() 119064718Seric { 119164718Seric struct dg_sys_info_load_info load_info; 119264718Seric 119364718Seric dg_sys_info((long *)&load_info, 119464718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 119564718Seric 119668508Seric if (tTd(3, 1)) 119768508Seric printf("getla: %d\n", (int) (load_info.one_minute + 0.5)); 119868508Seric 119964718Seric return((int) (load_info.one_minute + 0.5)); 120064718Seric } 120164718Seric 120264718Seric #else 120368508Seric # ifdef __hpux 120464718Seric 120568508Seric # include <sys/param.h> 120668508Seric # include <sys/pstat.h> 120768508Seric 120868508Seric int 120951773Seric getla() 121051773Seric { 121168508Seric struct pst_dynamic pstd; 121268508Seric 121368508Seric if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic), 121468508Seric (size_t) 1 ,0) == -1) 121568508Seric return 0; 121668508Seric 121768508Seric if (tTd(3, 1)) 121868508Seric printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5)); 121968508Seric 122068508Seric return (int) (pstd.psd_avg_1_min + 0.5); 122168508Seric } 122268508Seric 122368508Seric # else 122468508Seric 122568508Seric int 122668508Seric getla() 122768508Seric { 122851920Seric double avenrun[3]; 122951920Seric 123051920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 123157736Seric { 123257736Seric if (tTd(3, 1)) 123357736Seric perror("getla: getloadavg failed:"); 123451920Seric return (-1); 123557736Seric } 123657736Seric if (tTd(3, 1)) 123757736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 123851920Seric return ((int) (avenrun[0] + 0.5)); 123951773Seric } 124051773Seric 124168508Seric # endif /* __hpux */ 124264718Seric #endif /* DGUX */ 124351773Seric #else 124464295Seric #if LA_TYPE == LA_MACH 124551773Seric 124664295Seric /* 124765173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 124864295Seric */ 124964295Seric 125065173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 125165173Seric # include <mach/mach.h> 125265173Seric #else 125365173Seric # include <mach.h> 125465173Seric #endif 125564295Seric 125651773Seric getla() 125751773Seric { 125864295Seric processor_set_t default_set; 125964295Seric kern_return_t error; 126064295Seric unsigned int info_count; 126164295Seric struct processor_set_basic_info info; 126264295Seric host_t host; 126364295Seric 126464295Seric error = processor_set_default(host_self(), &default_set); 126564295Seric if (error != KERN_SUCCESS) 126664295Seric return -1; 126764295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 126864295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 126964295Seric &host, (processor_set_info_t)&info, 127064295Seric &info_count) != KERN_SUCCESS) 127164295Seric { 127264295Seric return -1; 127364295Seric } 127464295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 127564295Seric } 127664295Seric 127764295Seric 127864295Seric #else 127966313Seric #if LA_TYPE == LA_PROCSTR 128064295Seric 128166301Seric /* 128266301Seric ** Read /proc/loadavg for the load average. This is assumed to be 128366301Seric ** in a format like "0.15 0.12 0.06". 128466301Seric ** 128566301Seric ** Initially intended for Linux. This has been in the kernel 128666301Seric ** since at least 0.99.15. 128766301Seric */ 128866301Seric 128966301Seric # ifndef _PATH_LOADAVG 129066301Seric # define _PATH_LOADAVG "/proc/loadavg" 129166301Seric # endif 129266301Seric 129366301Seric int 129464295Seric getla() 129564295Seric { 129666319Seric double avenrun; 129766301Seric register int result; 129866319Seric FILE *fp; 129966301Seric 130066319Seric fp = fopen(_PATH_LOADAVG, "r"); 130166319Seric if (fp == NULL) 130266301Seric { 130366319Seric if (tTd(3, 1)) 130466319Seric printf("getla: fopen(%s): %s\n", 130566319Seric _PATH_LOADAVG, errstring(errno)); 130666319Seric return -1; 130766301Seric } 130866301Seric result = fscanf(fp, "%lf", &avenrun); 130966319Seric fclose(fp); 131066301Seric if (result != 1) 131166301Seric { 131266310Seric if (tTd(3, 1)) 131366301Seric printf("getla: fscanf() = %d: %s\n", 131466301Seric result, errstring(errno)); 131566301Seric return -1; 131666301Seric } 131766301Seric 131857736Seric if (tTd(3, 1)) 131966301Seric printf("getla(): %.2f\n", avenrun); 132066301Seric 132166301Seric return ((int) (avenrun + 0.5)); 132266301Seric } 132366301Seric 132466301Seric #else 132566301Seric 132666301Seric getla() 132766301Seric { 132866301Seric if (tTd(3, 1)) 132957736Seric printf("getla: ZERO\n"); 133051920Seric return (0); 133151773Seric } 133251773Seric 133351773Seric #endif 133451773Seric #endif 133564295Seric #endif 133666301Seric #endif 133766045Seric 133866045Seric 133966045Seric /* 134066045Seric * Copyright 1989 Massachusetts Institute of Technology 134166045Seric * 134266045Seric * Permission to use, copy, modify, distribute, and sell this software and its 134366045Seric * documentation for any purpose is hereby granted without fee, provided that 134466045Seric * the above copyright notice appear in all copies and that both that 134566045Seric * copyright notice and this permission notice appear in supporting 134666045Seric * documentation, and that the name of M.I.T. not be used in advertising or 134766045Seric * publicity pertaining to distribution of the software without specific, 134866045Seric * written prior permission. M.I.T. makes no representations about the 134966045Seric * suitability of this software for any purpose. It is provided "as is" 135066045Seric * without express or implied warranty. 135166045Seric * 135266045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 135366045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 135466045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 135566045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 135666045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 135766045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 135866045Seric * 135966045Seric * Authors: Many and varied... 136066045Seric */ 136166045Seric 136266045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 136366045Seric #ifndef lint 136466045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 136566045Seric #endif /* !lint */ 136666045Seric 136766179Seric #ifdef apollo 136866045Seric # undef volatile 136966045Seric # include <apollo/base.h> 137066045Seric 137166045Seric /* ARGSUSED */ 137266045Seric int getloadavg( call_data ) 137366045Seric caddr_t call_data; /* pointer to (double) return value */ 137466045Seric { 137566045Seric double *avenrun = (double *) call_data; 137666045Seric int i; 137766045Seric status_$t st; 137866045Seric long loadav[3]; 137966045Seric proc1_$get_loadav(loadav, &st); 138066045Seric *avenrun = loadav[0] / (double) (1 << 16); 138166045Seric return(0); 138266045Seric } 138366045Seric # endif /* apollo */ 138424943Seric /* 138524943Seric ** SHOULDQUEUE -- should this message be queued or sent? 138624943Seric ** 138724943Seric ** Compares the message cost to the load average to decide. 138824943Seric ** 138924943Seric ** Parameters: 139024943Seric ** pri -- the priority of the message in question. 139157438Seric ** ctime -- the message creation time. 139224943Seric ** 139324943Seric ** Returns: 139424943Seric ** TRUE -- if this message should be queued up for the 139524943Seric ** time being. 139624943Seric ** FALSE -- if the load is low enough to send this message. 139724943Seric ** 139824943Seric ** Side Effects: 139924943Seric ** none. 140024943Seric */ 140124943Seric 140224943Seric bool 140357438Seric shouldqueue(pri, ctime) 140424943Seric long pri; 140557438Seric time_t ctime; 140624943Seric { 140768508Seric bool rval; 140868508Seric 140968508Seric if (tTd(3, 30)) 141068508Seric printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri); 141151920Seric if (CurrentLA < QueueLA) 141268508Seric { 141368508Seric if (tTd(3, 30)) 141468508Seric printf("FALSE (CurrentLA < QueueLA)\n"); 141524943Seric return (FALSE); 141668508Seric } 141758132Seric if (CurrentLA >= RefuseLA) 141868508Seric { 141968508Seric if (tTd(3, 30)) 142068508Seric printf("TRUE (CurrentLA >= RefuseLA)\n"); 142158132Seric return (TRUE); 142268508Seric } 142368508Seric rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1)); 142468508Seric if (tTd(3, 30)) 142568508Seric printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE"); 142668508Seric return rval; 142724943Seric } 142824943Seric /* 142953037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 143053037Seric ** 143153037Seric ** Parameters: 143253037Seric ** none. 143353037Seric ** 143453037Seric ** Returns: 143553037Seric ** TRUE if incoming SMTP connections should be refused 143653037Seric ** (for now). 143753037Seric ** FALSE if we should accept new work. 143853037Seric ** 143953037Seric ** Side Effects: 144053037Seric ** none. 144153037Seric */ 144253037Seric 144353037Seric bool 144453037Seric refuseconnections() 144553037Seric { 144668508Seric extern bool enoughspace(); 144768508Seric 144859156Seric #ifdef XLA 144959156Seric if (!xla_smtp_ok()) 145059156Seric return TRUE; 145159156Seric #endif 145259156Seric 145353037Seric /* this is probably too simplistic */ 145468508Seric return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1); 145553037Seric } 145653037Seric /* 145724943Seric ** SETPROCTITLE -- set process title for ps 145824943Seric ** 145924943Seric ** Parameters: 146058674Seric ** fmt -- a printf style format string. 146158674Seric ** a, b, c -- possible parameters to fmt. 146224943Seric ** 146324943Seric ** Returns: 146424943Seric ** none. 146524943Seric ** 146624943Seric ** Side Effects: 146724943Seric ** Clobbers argv of our main procedure so ps(1) will 146824943Seric ** display the title. 146924943Seric */ 147024943Seric 147168508Seric #define SPT_NONE 0 /* don't use it at all */ 147268508Seric #define SPT_REUSEARGV 1 /* cover argv with title information */ 147368508Seric #define SPT_BUILTIN 2 /* use libc builtin */ 147468508Seric #define SPT_PSTAT 3 /* use pstat(PSTAT_SETCMD, ...) */ 147568508Seric #define SPT_PSSTRINGS 4 /* use PS_STRINGS->... */ 147668508Seric #define SPT_WRITEUDOT 5 /* write u. area in kmem */ 147768508Seric 147868508Seric #ifndef SPT_TYPE 147968508Seric # define SPT_TYPE SPT_REUSEARGV 148068508Seric #endif 148168508Seric 148268508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN 148368508Seric 148468508Seric # if SPT_TYPE == SPT_PSTAT 148558689Seric # include <sys/pstat.h> 148658689Seric # endif 148768508Seric # if SPT_TYPE == SPT_PSSTRINGS 148859732Seric # include <machine/vmparam.h> 148959732Seric # include <sys/exec.h> 149068508Seric # ifndef PS_STRINGS /* hmmmm.... apparently not available after all */ 149168508Seric # undef SPT_TYPE 149268508Seric # define SPT_TYPE SPT_REUSEARGV 149364247Seric # endif 149459732Seric # endif 149568508Seric 149668508Seric # if SPT_TYPE == SPT_PSSTRINGS 149768508Seric # define SETPROC_STATIC static 149868508Seric # else 149959732Seric # define SETPROC_STATIC 150059732Seric # endif 150167626Seric 150268508Seric # ifndef SPT_PADCHAR 150368508Seric # define SPT_PADCHAR ' ' 150468508Seric # endif 150558689Seric 150668508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */ 150764843Seric 150868508Seric #if SPT_TYPE != SPT_BUILTIN 150968508Seric 151024943Seric /*VARARGS1*/ 151168508Seric void 151268508Seric # ifdef __STDC__ 151357642Seric setproctitle(char *fmt, ...) 151468508Seric # else 151557642Seric setproctitle(fmt, va_alist) 151624943Seric char *fmt; 151757642Seric va_dcl 151868508Seric # endif 151924943Seric { 152068508Seric # if SPT_TYPE != SPT_NONE 152124943Seric register char *p; 152225049Seric register int i; 152359732Seric SETPROC_STATIC char buf[MAXLINE]; 152456852Seric VA_LOCAL_DECL 152568508Seric # if SPT_TYPE == SPT_PSTAT 152658689Seric union pstun pst; 152758689Seric # endif 152824943Seric extern char **Argv; 152924943Seric extern char *LastArgv; 153024943Seric 153158674Seric p = buf; 153224943Seric 153358674Seric /* print sendmail: heading for grep */ 153458674Seric (void) strcpy(p, "sendmail: "); 153558674Seric p += strlen(p); 153624943Seric 153758674Seric /* print the argument string */ 153858674Seric VA_START(fmt); 153958674Seric (void) vsprintf(p, fmt, ap); 154056852Seric VA_END; 154154996Seric 154258674Seric i = strlen(buf); 154358689Seric 154468508Seric # if SPT_TYPE == SPT_PSTAT 154558689Seric pst.pst_command = buf; 154658689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 154758689Seric # else 154868508Seric # if SPT_TYPE == SPT_PSSTRINGS 154959732Seric PS_STRINGS->ps_nargvstr = 1; 155059732Seric PS_STRINGS->ps_argvstr = buf; 155159732Seric # else 155254996Seric if (i > LastArgv - Argv[0] - 2) 155325049Seric { 155454996Seric i = LastArgv - Argv[0] - 2; 155558674Seric buf[i] = '\0'; 155625049Seric } 155758674Seric (void) strcpy(Argv[0], buf); 155854997Seric p = &Argv[0][i]; 155924943Seric while (p < LastArgv) 156068508Seric *p++ = SPT_PADCHAR; 156168508Seric Argv[1] = NULL; 156268508Seric # endif /* SPT_TYPE == SPT_PSSTRINGS */ 156368508Seric # endif /* SPT_TYPE == SPT_PSTAT */ 156468508Seric # endif /* SPT_TYPE != SPT_NONE */ 156524943Seric } 156665982Seric 156768508Seric #endif /* SPT_TYPE != SPT_BUILTIN */ 156825698Seric /* 156925698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 157025698Seric ** 157125698Seric ** Parameters: 157268508Seric ** sig -- the signal that got us here (unused). 157325698Seric ** 157425698Seric ** Returns: 157525698Seric ** none. 157625698Seric ** 157725698Seric ** Side Effects: 157825698Seric ** Picks up extant zombies. 157925698Seric */ 158025698Seric 158146928Sbostic void 158268508Seric reapchild(sig) 158368508Seric int sig; 158425698Seric { 158564738Seric int olderrno = errno; 158663966Seric # ifdef HASWAITPID 158760219Seric auto int status; 158860560Seric int count; 158960560Seric int pid; 159060219Seric 159160560Seric count = 0; 159260560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 159360560Seric { 159460560Seric if (count++ > 1000) 159560560Seric { 159666748Seric #ifdef LOG 159760560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 159860560Seric pid, status); 159966748Seric #endif 160060560Seric break; 160160560Seric } 160260560Seric } 160360219Seric # else 160425698Seric # ifdef WNOHANG 160525698Seric union wait status; 160625698Seric 160763753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 160825698Seric continue; 160956795Seric # else /* WNOHANG */ 161025698Seric auto int status; 161125698Seric 161260219Seric while (wait(&status) > 0) 161325698Seric continue; 161456795Seric # endif /* WNOHANG */ 161560219Seric # endif 161664035Seric # ifdef SYS5SIGNALS 161764035Seric (void) setsignal(SIGCHLD, reapchild); 161858061Seric # endif 161964738Seric errno = olderrno; 162025698Seric } 162155418Seric /* 162255418Seric ** UNSETENV -- remove a variable from the environment 162355418Seric ** 162455418Seric ** Not needed on newer systems. 162555418Seric ** 162655418Seric ** Parameters: 162755418Seric ** name -- the string name of the environment variable to be 162855418Seric ** deleted from the current environment. 162955418Seric ** 163055418Seric ** Returns: 163155418Seric ** none. 163255418Seric ** 163355418Seric ** Globals: 163455418Seric ** environ -- a pointer to the current environment. 163555418Seric ** 163655418Seric ** Side Effects: 163755418Seric ** Modifies environ. 163855418Seric */ 163955418Seric 164063962Seric #ifndef HASUNSETENV 164155418Seric 164255418Seric void 164355418Seric unsetenv(name) 164455418Seric char *name; 164555418Seric { 164655418Seric extern char **environ; 164755418Seric register char **pp; 164855418Seric int len = strlen(name); 164955418Seric 165055418Seric for (pp = environ; *pp != NULL; pp++) 165155418Seric { 165255418Seric if (strncmp(name, *pp, len) == 0 && 165355418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 165455418Seric break; 165555418Seric } 165655418Seric 165755418Seric for (; *pp != NULL; pp++) 165855418Seric *pp = pp[1]; 165955418Seric } 166055418Seric 166163962Seric #endif 166256215Seric /* 166356215Seric ** GETDTABLESIZE -- return number of file descriptors 166456215Seric ** 166556215Seric ** Only on non-BSD systems 166656215Seric ** 166756215Seric ** Parameters: 166856215Seric ** none 166956215Seric ** 167056215Seric ** Returns: 167156215Seric ** size of file descriptor table 167256215Seric ** 167356215Seric ** Side Effects: 167456215Seric ** none 167556215Seric */ 167656215Seric 167763787Seric #ifdef SOLARIS 167863787Seric # include <sys/resource.h> 167963787Seric #endif 168056215Seric 168156215Seric int 168263787Seric getdtsize() 168356215Seric { 168463787Seric #ifdef RLIMIT_NOFILE 168563787Seric struct rlimit rl; 168663787Seric 168763787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 168863787Seric return rl.rlim_cur; 168963787Seric #endif 169063787Seric 169164031Seric # ifdef HASGETDTABLESIZE 169264031Seric return getdtablesize(); 169364031Seric # else 169464031Seric # ifdef _SC_OPEN_MAX 169558689Seric return sysconf(_SC_OPEN_MAX); 169663787Seric # else 169756215Seric return NOFILE; 169863787Seric # endif 169958689Seric # endif 170056215Seric } 170157631Seric /* 170257631Seric ** UNAME -- get the UUCP name of this system. 170357631Seric */ 170457631Seric 170557943Seric #ifndef HASUNAME 170657631Seric 170757631Seric int 170857631Seric uname(name) 170957631Seric struct utsname *name; 171057631Seric { 171157631Seric FILE *file; 171257631Seric char *n; 171357631Seric 171457631Seric name->nodename[0] = '\0'; 171557631Seric 171657661Seric /* try /etc/whoami -- one line with the node name */ 171757631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 171857631Seric { 171957661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 172057631Seric (void) fclose(file); 172157661Seric n = strchr(name->nodename, '\n'); 172257631Seric if (n != NULL) 172357631Seric *n = '\0'; 172457631Seric if (name->nodename[0] != '\0') 172557631Seric return (0); 172657631Seric } 172757631Seric 172857661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 172957631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 173057631Seric { 173157631Seric char buf[MAXLINE]; 173257631Seric 173357631Seric while (fgets(buf, MAXLINE, file) != NULL) 173457631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 173557631Seric NODE_LENGTH, name->nodename) > 0) 173657631Seric break; 173757631Seric (void) fclose(file); 173857631Seric if (name->nodename[0] != '\0') 173957631Seric return (0); 174057631Seric } 174157631Seric 174257631Seric #ifdef TRUST_POPEN 174357631Seric /* 174457631Seric ** Popen is known to have security holes. 174557631Seric */ 174657631Seric 174757661Seric /* try uuname -l to return local name */ 174857631Seric if ((file = popen("uuname -l", "r")) != NULL) 174957631Seric { 175057661Seric (void) fgets(name, NODE_LENGTH + 1, file); 175157631Seric (void) pclose(file); 175257661Seric n = strchr(name, '\n'); 175357631Seric if (n != NULL) 175457631Seric *n = '\0'; 175557661Seric if (name->nodename[0] != '\0') 175657631Seric return (0); 175757631Seric } 175857631Seric #endif 175957631Seric 176057631Seric return (-1); 176157631Seric } 176257943Seric #endif /* HASUNAME */ 176358068Seric /* 176458068Seric ** INITGROUPS -- initialize groups 176558068Seric ** 176658068Seric ** Stub implementation for System V style systems 176758068Seric */ 176858068Seric 176958068Seric #ifndef HASINITGROUPS 177058068Seric 177158068Seric initgroups(name, basegid) 177258068Seric char *name; 177358068Seric int basegid; 177458068Seric { 177558068Seric return 0; 177658068Seric } 177758068Seric 177858068Seric #endif 177958082Seric /* 178059289Seric ** SETSID -- set session id (for non-POSIX systems) 178159289Seric */ 178259289Seric 178359289Seric #ifndef HASSETSID 178459289Seric 178563753Seric pid_t 178663753Seric setsid __P ((void)) 178759289Seric { 178864155Seric #ifdef TIOCNOTTY 178964155Seric int fd; 179064155Seric 179166743Seric fd = open("/dev/tty", O_RDWR, 0); 179264155Seric if (fd >= 0) 179364155Seric { 179464155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 179564155Seric (void) close(fd); 179664155Seric } 179764155Seric #endif /* TIOCNOTTY */ 179864705Seric # ifdef SYS5SETPGRP 179963753Seric return setpgrp(); 180063753Seric # else 180164155Seric return setpgid(0, getpid()); 180259289Seric # endif 180359289Seric } 180459289Seric 180559289Seric #endif 180659289Seric /* 180766748Seric ** FSYNC -- dummy fsync 180866748Seric */ 180966748Seric 181066748Seric #ifdef NEEDFSYNC 181166748Seric 181266748Seric fsync(fd) 181366748Seric int fd; 181466748Seric { 181566748Seric # ifdef O_SYNC 181666748Seric return fcntl(fd, F_SETFL, O_SYNC); 181766748Seric # else 181866748Seric /* nothing we can do */ 181966748Seric return 0; 182066748Seric # endif 182166748Seric } 182266748Seric 182366748Seric #endif 182466748Seric /* 182564718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 182664718Seric ** 182764718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 182868508Seric ** instead of a long. This patches things. Only needed on versions 182968508Seric ** prior to 5.4.3. 183064718Seric */ 183164718Seric 183268508Seric #ifdef DGUX_5_4_2 183364718Seric 183464718Seric #undef inet_addr 183564718Seric 183664718Seric long 183764718Seric dgux_inet_addr(host) 183864718Seric char *host; 183964718Seric { 184064718Seric struct in_addr haddr; 184164718Seric 184264718Seric haddr = inet_addr(host); 184364718Seric return haddr.s_addr; 184464718Seric } 184564718Seric 184664718Seric #endif 184764718Seric /* 184863969Seric ** GETOPT -- for old systems or systems with bogus implementations 184963969Seric */ 185063969Seric 185163969Seric #ifdef NEEDGETOPT 185263969Seric 185363969Seric /* 185463969Seric * Copyright (c) 1985 Regents of the University of California. 185563969Seric * All rights reserved. The Berkeley software License Agreement 185663969Seric * specifies the terms and conditions for redistribution. 185763969Seric */ 185863969Seric 185963969Seric 186063969Seric /* 186163969Seric ** this version hacked to add `atend' flag to allow state machine 186263969Seric ** to reset if invoked by the program to scan args for a 2nd time 186363969Seric */ 186463969Seric 186563969Seric #if defined(LIBC_SCCS) && !defined(lint) 186663969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 186764072Seric #endif /* LIBC_SCCS and not lint */ 186863969Seric 186963969Seric #include <stdio.h> 187063969Seric 187163969Seric /* 187263969Seric * get option letter from argument vector 187363969Seric */ 187465352Seric #ifdef _CONVEX_SOURCE 187568508Seric extern int optind, opterr, optopt; 187668508Seric extern char *optarg; 187765352Seric #else 187865352Seric int opterr = 1; /* if error message should be printed */ 187965352Seric int optind = 1; /* index into parent argv vector */ 188068470Seric int optopt = 0; /* character checked for validity */ 188168470Seric char *optarg = NULL; /* argument associated with option */ 188268508Seric #endif 188363969Seric 188463969Seric #define BADCH (int)'?' 188563969Seric #define EMSG "" 188663969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 188763969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 188863969Seric 188963969Seric getopt(nargc,nargv,ostr) 189065160Seric int nargc; 189165160Seric char *const *nargv; 189265160Seric const char *ostr; 189363969Seric { 189463969Seric static char *place = EMSG; /* option letter processing */ 189563969Seric static char atend = 0; 189663969Seric register char *oli; /* option letter list index */ 189763969Seric 189863969Seric if (atend) { 189963969Seric atend = 0; 190063969Seric place = EMSG; 190163969Seric } 190263969Seric if(!*place) { /* update scanning pointer */ 190364136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 190463969Seric atend++; 190563969Seric return(EOF); 190663969Seric } 190763969Seric if (*place == '-') { /* found "--" */ 190863969Seric ++optind; 190963969Seric atend++; 191063969Seric return(EOF); 191163969Seric } 191263969Seric } /* option letter okay? */ 191364125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 191464136Seric if (!*place) ++optind; 191563969Seric tell(": illegal option -- "); 191663969Seric } 191763969Seric if (*++oli != ':') { /* don't need argument */ 191863969Seric optarg = NULL; 191963969Seric if (!*place) ++optind; 192063969Seric } 192163969Seric else { /* need an argument */ 192263969Seric if (*place) optarg = place; /* no white space */ 192363969Seric else if (nargc <= ++optind) { /* no arg */ 192463969Seric place = EMSG; 192563969Seric tell(": option requires an argument -- "); 192663969Seric } 192763969Seric else optarg = nargv[optind]; /* white space */ 192863969Seric place = EMSG; 192963969Seric ++optind; 193063969Seric } 193163969Seric return(optopt); /* dump back option letter */ 193263969Seric } 193363969Seric 193463969Seric #endif 193563969Seric /* 193663969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 193763969Seric */ 193863969Seric 193963969Seric #ifdef NEEDVPRINTF 194063969Seric 194163969Seric #define MAXARG 16 194263969Seric 194363969Seric vfprintf(fp, fmt, ap) 194463969Seric FILE * fp; 194563969Seric char * fmt; 194663969Seric char ** ap; 194763969Seric { 194863969Seric char * bp[MAXARG]; 194963969Seric int i = 0; 195063969Seric 195163969Seric while (*ap && i < MAXARG) 195263969Seric bp[i++] = *ap++; 195363969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 195463969Seric bp[4], bp[5], bp[6], bp[7], 195563969Seric bp[8], bp[9], bp[10], bp[11], 195663969Seric bp[12], bp[13], bp[14], bp[15]); 195763969Seric } 195863969Seric 195963969Seric vsprintf(s, fmt, ap) 196063969Seric char * s; 196163969Seric char * fmt; 196263969Seric char ** ap; 196363969Seric { 196463969Seric char * bp[MAXARG]; 196563969Seric int i = 0; 196663969Seric 196763969Seric while (*ap && i < MAXARG) 196863969Seric bp[i++] = *ap++; 196963969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 197063969Seric bp[4], bp[5], bp[6], bp[7], 197163969Seric bp[8], bp[9], bp[10], bp[11], 197263969Seric bp[12], bp[13], bp[14], bp[15]); 197363969Seric } 197463969Seric 197563969Seric #endif 197663969Seric /* 197765220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 197865208Seric ** 197965211Seric ** Parameters: 198065211Seric ** shell -- the user's shell from /etc/passwd 198165211Seric ** 198265211Seric ** Returns: 198365211Seric ** TRUE -- if it is ok to use this for unrestricted access. 198465211Seric ** FALSE -- if the shell is restricted. 198565208Seric */ 198665208Seric 198765587Seric #if !HASGETUSERSHELL 198865587Seric 198965587Seric # ifndef _PATH_SHELLS 199065587Seric # define _PATH_SHELLS "/etc/shells" 199165587Seric # endif 199265587Seric 199365587Seric char *DefaultUserShells[] = 199465587Seric { 199568508Seric "/bin/sh", /* standard shell */ 199665587Seric "/usr/bin/sh", 199768508Seric "/bin/csh", /* C shell */ 199865587Seric "/usr/bin/csh", 199965587Seric #ifdef __hpux 200068508Seric "/bin/rsh", /* restricted Bourne shell */ 200168508Seric "/bin/ksh", /* Korn shell */ 200268508Seric "/bin/rksh", /* restricted Korn shell */ 200365587Seric "/bin/pam", 200468508Seric "/usr/bin/keysh", /* key shell (extended Korn shell) */ 200565587Seric "/bin/posix/sh", 200665211Seric #endif 200768508Seric #ifdef _AIX3 200868508Seric "/bin/ksh", /* Korn shell */ 200968508Seric "/usr/bin/ksh", 201068508Seric "/bin/tsh", /* trusted shell */ 201168508Seric "/usr/bin/tsh", 201268508Seric "/bin/bsh", /* Bourne shell */ 201368508Seric "/usr/bin/bsh", 201468508Seric #endif 201565587Seric NULL 201665587Seric }; 201765587Seric 201865208Seric #endif 201965208Seric 202065747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 202165747Seric 202265211Seric bool 202365211Seric usershellok(shell) 202465211Seric char *shell; 202565208Seric { 202665211Seric #if HASGETUSERSHELL 202765211Seric register char *p; 202865211Seric extern char *getusershell(); 202965208Seric 203068457Seric if (shell == NULL || shell[0] == '\0') 203168457Seric return TRUE; 203268457Seric 203365211Seric setusershell(); 203465211Seric while ((p = getusershell()) != NULL) 203565747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 203665211Seric break; 203765211Seric endusershell(); 203865211Seric return p != NULL; 203965211Seric #else 204065211Seric register FILE *shellf; 204165211Seric char buf[MAXLINE]; 204265208Seric 204368457Seric if (shell == NULL || shell[0] == '\0') 204468457Seric return TRUE; 204568457Seric 204665211Seric shellf = fopen(_PATH_SHELLS, "r"); 204765211Seric if (shellf == NULL) 204865211Seric { 204965211Seric /* no /etc/shells; see if it is one of the std shells */ 205065587Seric char **d; 205165587Seric 205265587Seric for (d = DefaultUserShells; *d != NULL; d++) 205365587Seric { 205465587Seric if (strcmp(shell, *d) == 0) 205565587Seric return TRUE; 205665587Seric } 205765587Seric return FALSE; 205865211Seric } 205965208Seric 206065211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 206165211Seric { 206265211Seric register char *p, *q; 206365208Seric 206465211Seric p = buf; 206565211Seric while (*p != '\0' && *p != '#' && *p != '/') 206665211Seric p++; 206765211Seric if (*p == '#' || *p == '\0') 206865208Seric continue; 206965211Seric q = p; 207065211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 207165211Seric p++; 207265211Seric *p = '\0'; 207365747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 207465211Seric { 207565211Seric fclose(shellf); 207665211Seric return TRUE; 207765211Seric } 207865208Seric } 207965211Seric fclose(shellf); 208065211Seric return FALSE; 208165211Seric #endif 208265208Seric } 208365208Seric /* 208464311Seric ** FREESPACE -- see how much free space is on the queue filesystem 208558082Seric ** 208658082Seric ** Only implemented if you have statfs. 208758082Seric ** 208858082Seric ** Parameters: 208964311Seric ** dir -- the directory in question. 209064311Seric ** bsize -- a variable into which the filesystem 209164311Seric ** block size is stored. 209258082Seric ** 209358082Seric ** Returns: 209464311Seric ** The number of bytes free on the queue filesystem. 209564311Seric ** -1 if the statfs call fails. 209664311Seric ** 209764311Seric ** Side effects: 209864311Seric ** Puts the filesystem block size into bsize. 209958082Seric */ 210058082Seric 210165749Seric /* statfs types */ 210265749Seric #define SFS_NONE 0 /* no statfs implementation */ 210365749Seric #define SFS_USTAT 1 /* use ustat */ 210465749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 210565749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 210665749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 210765749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 210866752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 210965749Seric 211065749Seric #ifndef SFS_TYPE 211165749Seric # define SFS_TYPE SFS_NONE 211258157Seric #endif 211358157Seric 211465749Seric #if SFS_TYPE == SFS_USTAT 211558157Seric # include <ustat.h> 211658157Seric #endif 211765749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 211865749Seric # include <sys/statfs.h> 211958082Seric #endif 212065749Seric #if SFS_TYPE == SFS_VFS 212165749Seric # include <sys/vfs.h> 212265749Seric #endif 212365749Seric #if SFS_TYPE == SFS_MOUNT 212465749Seric # include <sys/mount.h> 212565749Seric #endif 212666752Seric #if SFS_TYPE == SFS_STATVFS 212766752Seric # include <sys/statvfs.h> 212866752Seric #endif 212958082Seric 213064311Seric long 213164311Seric freespace(dir, bsize) 213264311Seric char *dir; 213364311Seric long *bsize; 213458082Seric { 213565749Seric #if SFS_TYPE != SFS_NONE 213665749Seric # if SFS_TYPE == SFS_USTAT 213758153Seric struct ustat fs; 213858153Seric struct stat statbuf; 213958366Seric # define FSBLOCKSIZE DEV_BSIZE 2140*68695Seric # define SFS_BAVAIL f_tfree 214158157Seric # else 214258157Seric # if defined(ultrix) 214358157Seric struct fs_data fs; 2144*68695Seric # define SFS_BAVAIL fd_bfreen 214568508Seric # define FSBLOCKSIZE 1024L 214658153Seric # else 214766752Seric # if SFS_TYPE == SFS_STATVFS 214866752Seric struct statvfs fs; 214968508Seric # define FSBLOCKSIZE fs.f_frsize 215066752Seric # else 215158082Seric struct statfs fs; 215266752Seric # define FSBLOCKSIZE fs.f_bsize 215363987Seric # endif 215458153Seric # endif 215558133Seric # endif 2156*68695Seric # ifndef SFS_BAVAIL 2157*68695Seric # define SFS_BAVAIL f_bavail 215868508Seric # endif 215958082Seric 216065749Seric # if SFS_TYPE == SFS_USTAT 216164311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 216258157Seric # else 216365749Seric # if SFS_TYPE == SFS_4ARGS 216464311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 216558157Seric # else 216668508Seric # if SFS_TYPE == SFS_STATVFS 216768508Seric if (statvfs(dir, &fs) == 0) 216868508Seric # else 216968508Seric # if defined(ultrix) 217068499Seric if (statfs(dir, &fs) > 0) 217168508Seric # else 217264311Seric if (statfs(dir, &fs) == 0) 217368508Seric # endif 217458153Seric # endif 217558133Seric # endif 217658133Seric # endif 217758133Seric { 217864311Seric if (bsize != NULL) 217964311Seric *bsize = FSBLOCKSIZE; 2180*68695Seric return (fs.SFS_BAVAIL); 218164311Seric } 218264337Seric #endif 218364311Seric return (-1); 218464311Seric } 218564311Seric /* 218664311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 218764311Seric ** 218864311Seric ** Only implemented if you have statfs. 218964311Seric ** 219064311Seric ** Parameters: 219164311Seric ** msize -- the size to check against. If zero, we don't yet 219264311Seric ** know how big the message will be, so just check for 219364311Seric ** a "reasonable" amount. 219464311Seric ** 219564311Seric ** Returns: 219664311Seric ** TRUE if there is enough space. 219764311Seric ** FALSE otherwise. 219864311Seric */ 219964311Seric 220064311Seric bool 220164311Seric enoughspace(msize) 220264311Seric long msize; 220364311Seric { 220464311Seric long bfree, bsize; 220564311Seric 220664311Seric if (MinBlocksFree <= 0 && msize <= 0) 220764311Seric { 220858133Seric if (tTd(4, 80)) 220964311Seric printf("enoughspace: no threshold\n"); 221064311Seric return TRUE; 221164311Seric } 221264311Seric 221364311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 221464311Seric { 221564311Seric if (tTd(4, 80)) 221658333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 221764311Seric bfree, msize); 221858333Seric 221958333Seric /* convert msize to block count */ 222064311Seric msize = msize / bsize + 1; 222158333Seric if (MinBlocksFree >= 0) 222258333Seric msize += MinBlocksFree; 222358333Seric 222464311Seric if (bfree < msize) 222558090Seric { 222658090Seric #ifdef LOG 222758090Seric if (LogLevel > 0) 222864254Seric syslog(LOG_ALERT, 222964254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 223068508Seric CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id, 223168508Seric bfree, 223268508Seric CurHostName == NULL ? "SMTP-DAEMON" : CurHostName, 223368508Seric msize, QueueDir); 223458090Seric #endif 223558082Seric return FALSE; 223658090Seric } 223758082Seric } 223858082Seric else if (tTd(4, 80)) 223958333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 224058333Seric MinBlocksFree, msize, errstring(errno)); 224158082Seric return TRUE; 224258082Seric } 224358542Seric /* 224458542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 224558542Seric ** 224658542Seric ** This looks at an errno value and tells if this is likely to 224758542Seric ** go away if retried later. 224858542Seric ** 224958542Seric ** Parameters: 225058542Seric ** err -- the errno code to classify. 225158542Seric ** 225258542Seric ** Returns: 225358542Seric ** TRUE if this is probably transient. 225458542Seric ** FALSE otherwise. 225558542Seric */ 225658542Seric 225758542Seric bool 225858542Seric transienterror(err) 225958542Seric int err; 226058542Seric { 226158542Seric switch (err) 226258542Seric { 226358542Seric case EIO: /* I/O error */ 226458542Seric case ENXIO: /* Device not configured */ 226558542Seric case EAGAIN: /* Resource temporarily unavailable */ 226658542Seric case ENOMEM: /* Cannot allocate memory */ 226758542Seric case ENODEV: /* Operation not supported by device */ 226858542Seric case ENFILE: /* Too many open files in system */ 226958542Seric case EMFILE: /* Too many open files */ 227058542Seric case ENOSPC: /* No space left on device */ 227158542Seric #ifdef ETIMEDOUT 227258542Seric case ETIMEDOUT: /* Connection timed out */ 227358542Seric #endif 227458542Seric #ifdef ESTALE 227558542Seric case ESTALE: /* Stale NFS file handle */ 227658542Seric #endif 227758542Seric #ifdef ENETDOWN 227858542Seric case ENETDOWN: /* Network is down */ 227958542Seric #endif 228058542Seric #ifdef ENETUNREACH 228158542Seric case ENETUNREACH: /* Network is unreachable */ 228258542Seric #endif 228358542Seric #ifdef ENETRESET 228458542Seric case ENETRESET: /* Network dropped connection on reset */ 228558542Seric #endif 228658542Seric #ifdef ECONNABORTED 228758542Seric case ECONNABORTED: /* Software caused connection abort */ 228858542Seric #endif 228958542Seric #ifdef ECONNRESET 229058542Seric case ECONNRESET: /* Connection reset by peer */ 229158542Seric #endif 229258542Seric #ifdef ENOBUFS 229358542Seric case ENOBUFS: /* No buffer space available */ 229458542Seric #endif 229558542Seric #ifdef ESHUTDOWN 229658542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 229758542Seric #endif 229858542Seric #ifdef ECONNREFUSED 229958542Seric case ECONNREFUSED: /* Connection refused */ 230058542Seric #endif 230158542Seric #ifdef EHOSTDOWN 230258542Seric case EHOSTDOWN: /* Host is down */ 230358542Seric #endif 230458542Seric #ifdef EHOSTUNREACH 230558542Seric case EHOSTUNREACH: /* No route to host */ 230658542Seric #endif 230758542Seric #ifdef EDQUOT 230858542Seric case EDQUOT: /* Disc quota exceeded */ 230958542Seric #endif 231058542Seric #ifdef EPROCLIM 231158542Seric case EPROCLIM: /* Too many processes */ 231258542Seric #endif 231358542Seric #ifdef EUSERS 231458542Seric case EUSERS: /* Too many users */ 231558542Seric #endif 231658542Seric #ifdef EDEADLK 231758542Seric case EDEADLK: /* Resource deadlock avoided */ 231858542Seric #endif 231958542Seric #ifdef EISCONN 232058542Seric case EISCONN: /* Socket already connected */ 232158542Seric #endif 232258542Seric #ifdef EINPROGRESS 232358542Seric case EINPROGRESS: /* Operation now in progress */ 232458542Seric #endif 232558542Seric #ifdef EALREADY 232658542Seric case EALREADY: /* Operation already in progress */ 232758542Seric #endif 232858542Seric #ifdef EADDRINUSE 232958542Seric case EADDRINUSE: /* Address already in use */ 233058542Seric #endif 233158542Seric #ifdef EADDRNOTAVAIL 233258542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 233358542Seric #endif 233466018Seric #ifdef ETXTBSY 233566017Seric case ETXTBSY: /* (Apollo) file locked */ 233666017Seric #endif 233763834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 233858542Seric case ENOSR: /* Out of streams resources */ 233958542Seric #endif 234058542Seric return TRUE; 234158542Seric } 234258542Seric 234358542Seric /* nope, must be permanent */ 234458542Seric return FALSE; 234558542Seric } 234658689Seric /* 234764035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 234858689Seric ** 234958689Seric ** Parameters: 235058689Seric ** fd -- the file descriptor of the file. 235158689Seric ** filename -- the file name (for error messages). 235264335Seric ** ext -- the filename extension. 235358689Seric ** type -- type of the lock. Bits can be: 235458689Seric ** LOCK_EX -- exclusive lock. 235558689Seric ** LOCK_NB -- non-blocking. 235658689Seric ** 235758689Seric ** Returns: 235858689Seric ** TRUE if the lock was acquired. 235958689Seric ** FALSE otherwise. 236058689Seric */ 236158689Seric 236258689Seric bool 236364335Seric lockfile(fd, filename, ext, type) 236458689Seric int fd; 236558689Seric char *filename; 236664335Seric char *ext; 236758689Seric int type; 236858689Seric { 236965830Seric # if !HASFLOCK 237058689Seric int action; 237158689Seric struct flock lfd; 237264335Seric 237364335Seric if (ext == NULL) 237464335Seric ext = ""; 237564035Seric 237663983Seric bzero(&lfd, sizeof lfd); 237759447Seric if (bitset(LOCK_UN, type)) 237859447Seric lfd.l_type = F_UNLCK; 237959447Seric else if (bitset(LOCK_EX, type)) 238058689Seric lfd.l_type = F_WRLCK; 238158689Seric else 238258689Seric lfd.l_type = F_RDLCK; 238358689Seric 238458689Seric if (bitset(LOCK_NB, type)) 238558689Seric action = F_SETLK; 238658689Seric else 238758689Seric action = F_SETLKW; 238858689Seric 238964264Seric if (tTd(55, 60)) 239064335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 239164335Seric filename, ext, action, lfd.l_type); 239264264Seric 239358689Seric if (fcntl(fd, action, &lfd) >= 0) 239464264Seric { 239564264Seric if (tTd(55, 60)) 239664264Seric printf("SUCCESS\n"); 239758689Seric return TRUE; 239864264Seric } 239958689Seric 240064264Seric if (tTd(55, 60)) 240164264Seric printf("(%s) ", errstring(errno)); 240264264Seric 240364136Seric /* 240464136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 240564136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 240664136Seric ** as type "tmp" (that is, served from swap space), the 240764136Seric ** previous fcntl will fail with "Invalid argument" errors. 240864136Seric ** Since this is fairly common during testing, we will assume 240964136Seric ** that this indicates that the lock is successfully grabbed. 241064136Seric */ 241164136Seric 241264136Seric if (errno == EINVAL) 241364264Seric { 241464264Seric if (tTd(55, 60)) 241564264Seric printf("SUCCESS\n"); 241664136Seric return TRUE; 241764264Seric } 241864136Seric 241958689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 242064378Seric { 242164378Seric int omode = -1; 242264378Seric # ifdef F_GETFL 242364378Seric int oerrno = errno; 242464378Seric 242564378Seric (void) fcntl(fd, F_GETFL, &omode); 242664378Seric errno = oerrno; 242764378Seric # endif 242864378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 242964378Seric filename, ext, fd, type, omode, geteuid()); 243064378Seric } 243158689Seric # else 243264335Seric if (ext == NULL) 243364335Seric ext = ""; 243464335Seric 243564264Seric if (tTd(55, 60)) 243664335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 243764264Seric 243858689Seric if (flock(fd, type) >= 0) 243964264Seric { 244064264Seric if (tTd(55, 60)) 244164264Seric printf("SUCCESS\n"); 244258689Seric return TRUE; 244364264Seric } 244458689Seric 244564264Seric if (tTd(55, 60)) 244664264Seric printf("(%s) ", errstring(errno)); 244764264Seric 244858689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 244964378Seric { 245064378Seric int omode = -1; 245164378Seric # ifdef F_GETFL 245264378Seric int oerrno = errno; 245364378Seric 245464378Seric (void) fcntl(fd, F_GETFL, &omode); 245564378Seric errno = oerrno; 245664378Seric # endif 245764378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 245864378Seric filename, ext, fd, type, omode, geteuid()); 245964386Seric } 246058689Seric # endif 246164264Seric if (tTd(55, 60)) 246264264Seric printf("FAIL\n"); 246358689Seric return FALSE; 246458689Seric } 246564556Seric /* 246665948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 246765948Seric ** 246865948Seric ** Parameters: 246965948Seric ** fd -- the file descriptor to check. 247065948Seric ** 247165948Seric ** Returns: 247265948Seric ** TRUE -- if only root can chown the file to an arbitrary 247365948Seric ** user. 247465948Seric ** FALSE -- if an arbitrary user can give away a file. 247565948Seric */ 247665948Seric 247765948Seric bool 247865948Seric chownsafe(fd) 247965948Seric int fd; 248065948Seric { 248165950Seric #ifdef __hpux 248265948Seric char *s; 248365948Seric int tfd; 248465948Seric uid_t o_uid, o_euid; 248565948Seric gid_t o_gid, o_egid; 248665948Seric bool rval; 248765948Seric struct stat stbuf; 248865948Seric 248965948Seric o_uid = getuid(); 249065948Seric o_euid = geteuid(); 249165948Seric o_gid = getgid(); 249265948Seric o_egid = getegid(); 249365959Seric fstat(fd, &stbuf); 249465948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 249565948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 249665948Seric s = tmpnam(NULL); 249765948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 249865974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 249965948Seric close(tfd); 250065948Seric unlink(s); 250165948Seric setreuid(o_uid, o_euid); 250265948Seric setresgid(o_gid, o_egid, -1); 250365948Seric return rval; 250465948Seric #else 250566088Seric # ifdef _POSIX_CHOWN_RESTRICTED 250666088Seric # if _POSIX_CHOWN_RESTRICTED == -1 250766088Seric return FALSE; 250866088Seric # else 250966088Seric return TRUE; 251066088Seric # endif 251166088Seric # else 251266088Seric # ifdef _PC_CHOWN_RESTRICTED 251368508Seric int rval; 251468508Seric 251568508Seric /* 251668508Seric ** Some systems (e.g., SunOS) seem to have the call and the 251768508Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 251868508Seric ** the call. This heuristic checks for that. 251968508Seric */ 252068508Seric 252168508Seric errno = 0; 252268508Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 252368508Seric if (errno == 0) 252468508Seric return rval > 0; 252568508Seric # endif 252668508Seric # ifdef BSD 252768508Seric return TRUE; 252868499Seric # else 252965948Seric return FALSE; 253065948Seric # endif 253165948Seric # endif 253265948Seric #endif 253365948Seric } 253465948Seric /* 253568508Seric ** RESETLIMITS -- reset system controlled resource limits 253668508Seric ** 253768508Seric ** This is to avoid denial-of-service attacks 253868508Seric ** 253968508Seric ** Parameters: 254068508Seric ** none 254168508Seric ** 254268508Seric ** Returns: 254368508Seric ** none 254468508Seric */ 254568508Seric 254668508Seric #if HASSETRLIMIT 254768508Seric # include <sys/resource.h> 254868508Seric #endif 254968508Seric 255068508Seric void 255168508Seric resetlimits() 255268508Seric { 255368508Seric #if HASSETRLIMIT 255468508Seric struct rlimit lim; 255568508Seric 255668508Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 255768508Seric (void) setrlimit(RLIMIT_CPU, &lim); 255868508Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 255968508Seric #else 256068508Seric # if HASULIMIT 256168508Seric (void) ulimit(2, 0x3fffff); 256268508Seric # endif 256368508Seric #endif 256468508Seric } 256568508Seric /* 256664556Seric ** GETCFNAME -- return the name of the .cf file. 256764556Seric ** 256864556Seric ** Some systems (e.g., NeXT) determine this dynamically. 256964556Seric */ 257064556Seric 257164556Seric char * 257264556Seric getcfname() 257364556Seric { 257464556Seric if (ConfFile != NULL) 257564556Seric return ConfFile; 257665214Seric #ifdef NETINFO 257765214Seric { 257865214Seric extern char *ni_propval(); 257965214Seric char *cflocation; 258065214Seric 258168508Seric cflocation = ni_propval("/locations", NULL, "sendmail", 258268508Seric "sendmail.cf", '\0'); 258365214Seric if (cflocation != NULL) 258465214Seric return cflocation; 258565214Seric } 258665214Seric #endif 258764556Seric return _PATH_SENDMAILCF; 258864556Seric } 258964718Seric /* 259064718Seric ** SETVENDOR -- process vendor code from V configuration line 259164718Seric ** 259264718Seric ** Parameters: 259364718Seric ** vendor -- string representation of vendor. 259464718Seric ** 259564718Seric ** Returns: 259664718Seric ** TRUE -- if ok. 259764718Seric ** FALSE -- if vendor code could not be processed. 259864926Seric ** 259964926Seric ** Side Effects: 260064926Seric ** It is reasonable to set mode flags here to tweak 260164926Seric ** processing in other parts of the code if necessary. 260264926Seric ** For example, if you are a vendor that uses $%y to 260364926Seric ** indicate YP lookups, you could enable that here. 260464718Seric */ 260564718Seric 260664718Seric bool 260764718Seric setvendor(vendor) 260864718Seric char *vendor; 260964718Seric { 261064926Seric if (strcasecmp(vendor, "Berkeley") == 0) 261168508Seric { 261268508Seric VendorCode = VENDOR_BERKELEY; 261364926Seric return TRUE; 261468508Seric } 261564926Seric 261664926Seric /* add vendor extensions here */ 261764926Seric 261868508Seric #ifdef SUN_EXTENSIONS 261968508Seric if (strcasecmp(vendor, "Sun") == 0) 262068508Seric { 262168508Seric VendorCode = VENDOR_SUN; 262268508Seric return TRUE; 262368508Seric } 262468508Seric #endif 262568508Seric 262664926Seric return FALSE; 262764718Seric } 262864816Seric /* 262964816Seric ** STRTOL -- convert string to long integer 263064816Seric ** 263164816Seric ** For systems that don't have it in the C library. 263266161Seric ** 263366161Seric ** This is taken verbatim from the 4.4-Lite C library. 263464816Seric */ 263564816Seric 263664816Seric #ifdef NEEDSTRTOL 263764816Seric 263866161Seric #if defined(LIBC_SCCS) && !defined(lint) 263966161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 264066161Seric #endif /* LIBC_SCCS and not lint */ 264166161Seric 264266161Seric #include <limits.h> 264366161Seric 264466161Seric /* 264566161Seric * Convert a string to a long integer. 264666161Seric * 264766161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 264866161Seric * alphabets and digits are each contiguous. 264966161Seric */ 265066161Seric 265164816Seric long 265266161Seric strtol(nptr, endptr, base) 265366161Seric const char *nptr; 265466161Seric char **endptr; 265566161Seric register int base; 265664816Seric { 265766161Seric register const char *s = nptr; 265866161Seric register unsigned long acc; 265966161Seric register int c; 266066161Seric register unsigned long cutoff; 266166161Seric register int neg = 0, any, cutlim; 266264816Seric 266366161Seric /* 266466161Seric * Skip white space and pick up leading +/- sign if any. 266566161Seric * If base is 0, allow 0x for hex and 0 for octal, else 266666161Seric * assume decimal; if base is already 16, allow 0x. 266766161Seric */ 266866161Seric do { 266966161Seric c = *s++; 267066161Seric } while (isspace(c)); 267166161Seric if (c == '-') { 267266161Seric neg = 1; 267366161Seric c = *s++; 267466161Seric } else if (c == '+') 267566161Seric c = *s++; 267666161Seric if ((base == 0 || base == 16) && 267766161Seric c == '0' && (*s == 'x' || *s == 'X')) { 267866161Seric c = s[1]; 267966161Seric s += 2; 268066161Seric base = 16; 268166161Seric } 268266161Seric if (base == 0) 268366161Seric base = c == '0' ? 8 : 10; 268464816Seric 268566161Seric /* 268666161Seric * Compute the cutoff value between legal numbers and illegal 268766161Seric * numbers. That is the largest legal value, divided by the 268866161Seric * base. An input number that is greater than this value, if 268966161Seric * followed by a legal input character, is too big. One that 269066161Seric * is equal to this value may be valid or not; the limit 269166161Seric * between valid and invalid numbers is then based on the last 269266161Seric * digit. For instance, if the range for longs is 269366161Seric * [-2147483648..2147483647] and the input base is 10, 269466161Seric * cutoff will be set to 214748364 and cutlim to either 269566161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 269666161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 269766161Seric * the number is too big, and we will return a range error. 269866161Seric * 269966161Seric * Set any if any `digits' consumed; make it negative to indicate 270066161Seric * overflow. 270166161Seric */ 270266161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 270366161Seric cutlim = cutoff % (unsigned long)base; 270466161Seric cutoff /= (unsigned long)base; 270566161Seric for (acc = 0, any = 0;; c = *s++) { 270666161Seric if (isdigit(c)) 270766161Seric c -= '0'; 270866161Seric else if (isalpha(c)) 270966161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 271066161Seric else 271166161Seric break; 271266161Seric if (c >= base) 271366161Seric break; 271466161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 271566161Seric any = -1; 271666161Seric else { 271766161Seric any = 1; 271866161Seric acc *= base; 271966161Seric acc += c; 272064816Seric } 272164816Seric } 272266161Seric if (any < 0) { 272366161Seric acc = neg ? LONG_MIN : LONG_MAX; 272466161Seric errno = ERANGE; 272566161Seric } else if (neg) 272666161Seric acc = -acc; 272766161Seric if (endptr != 0) 272866161Seric *endptr = (char *)(any ? s - 1 : nptr); 272966161Seric return (acc); 273064816Seric } 273164816Seric 273264816Seric #endif 273364841Seric /* 273468693Seric ** SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 273564841Seric ** 273668693Seric ** Some operating systems have wierd problems with the gethostbyXXX 273768693Seric ** routines. For example, Solaris versions at least through 2.3 273868693Seric ** don't properly deliver a canonical h_name field. This tries to 273968693Seric ** work around these problems. 274064841Seric */ 274164841Seric 274264841Seric struct hostent * 274368693Seric sm_gethostbyname(name) 274465094Seric const char *name; 274564841Seric { 274668693Seric #if defined(SOLARIS) && SOLARIS < 204 274768693Seric extern int h_errno; 274868693Seric 274968508Seric # if SOLARIS == 203 275064841Seric static struct hostent hp; 275164841Seric static char buf[1000]; 275264841Seric extern struct hostent *_switch_gethostbyname_r(); 275364841Seric 275464841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 275564841Seric # else 275665009Seric extern struct hostent *__switch_gethostbyname(); 275765009Seric 275864841Seric return __switch_gethostbyname(name); 275964841Seric # endif 276068693Seric #else 276168693Seric return gethostbyname(name); 276268693Seric #endif 276364841Seric } 276464841Seric 276564841Seric struct hostent * 276668693Seric sm_gethostbyaddr(addr, len, type) 276765094Seric const char *addr; 276864841Seric int len; 276964841Seric int type; 277064841Seric { 277168693Seric #if defined(SOLARIS) && SOLARIS < 204 277268693Seric extern int h_errno; 277368693Seric 277468508Seric # if SOLARIS == 203 277564841Seric static struct hostent hp; 277664841Seric static char buf[1000]; 277764841Seric extern struct hostent *_switch_gethostbyaddr_r(); 277864841Seric 277964841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 278064841Seric # else 278165009Seric extern struct hostent *__switch_gethostbyaddr(); 278265009Seric 278364841Seric return __switch_gethostbyaddr(addr, len, type); 278464841Seric # endif 278568693Seric #else 278668693Seric return gethostbyaddr(addr, len, type); 278768693Seric #endif 278864841Seric } 278968693Seric /* 279068693Seric ** SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid 279168693Seric */ 279264841Seric 279368693Seric struct passwd * 279468693Seric sm_getpwnam(user) 279568693Seric const char *user; 279668693Seric { 279768693Seric return getpwnam(user); 279868693Seric } 279968693Seric 280068693Seric struct passwd * 280168693Seric sm_getpwuid(uid) 280268693Seric uid_t uid; 280368693Seric { 280468693Seric return getpwuid(uid); 280568693Seric } 280665214Seric /* 280765214Seric ** NI_PROPVAL -- netinfo property value lookup routine 280865214Seric ** 280965214Seric ** Parameters: 281068508Seric ** keydir -- the Netinfo directory name in which to search 281168508Seric ** for the key. 281268508Seric ** keyprop -- the name of the property in which to find the 281368508Seric ** property we are interested. Defaults to "name". 281468508Seric ** keyval -- the value for which we are really searching. 281568508Seric ** valprop -- the property name for the value in which we 281668508Seric ** are interested. 281768508Seric ** sepchar -- if non-nil, this can be multiple-valued, and 281868508Seric ** we should return a string separated by this 281968508Seric ** character. 282065214Seric ** 282165214Seric ** Returns: 282265214Seric ** NULL -- if: 282365214Seric ** 1. the directory is not found 282465214Seric ** 2. the property name is not found 282565214Seric ** 3. the property contains multiple values 282665214Seric ** 4. some error occured 282765214Seric ** else -- the location of the config file. 282865214Seric ** 282968508Seric ** Example: 283068508Seric ** To search for an alias value, use: 283168508Seric ** ni_propval("/aliases", "name", aliasname, "members", ',') 283268508Seric ** 283365214Seric ** Notes: 283465214Seric ** Caller should free the return value of ni_proval 283565214Seric */ 283665214Seric 283765214Seric #ifdef NETINFO 283865214Seric 283965214Seric # include <netinfo/ni.h> 284065214Seric 284165214Seric # define LOCAL_NETINFO_DOMAIN "." 284265214Seric # define PARENT_NETINFO_DOMAIN ".." 284365214Seric # define MAX_NI_LEVELS 256 284465214Seric 284565214Seric char * 284668508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar) 284768508Seric char *keydir; 284868508Seric char *keyprop; 284968508Seric char *keyval; 285068508Seric char *valprop; 285168508Seric char sepchar; 285265214Seric { 285365820Seric char *propval = NULL; 285465214Seric int i; 285568508Seric int j, alen; 285665214Seric void *ni = NULL; 285765214Seric void *lastni = NULL; 285865214Seric ni_status nis; 285965214Seric ni_id nid; 286065214Seric ni_namelist ninl; 286168508Seric register char *p; 286268508Seric char keybuf[1024]; 286365214Seric 286465214Seric /* 286568508Seric ** Create the full key from the two parts. 286668508Seric ** 286768508Seric ** Note that directory can end with, e.g., "name=" to specify 286868508Seric ** an alternate search property. 286968508Seric */ 287068508Seric 287168508Seric i = strlen(keydir) + strlen(keyval) + 2; 287268508Seric if (keyprop != NULL) 287368508Seric i += strlen(keyprop) + 1; 287468508Seric if (i > sizeof keybuf) 287568508Seric return NULL; 287668508Seric strcpy(keybuf, keydir); 287768508Seric strcat(keybuf, "/"); 287868508Seric if (keyprop != NULL) 287968508Seric { 288068508Seric strcat(keybuf, keyprop); 288168508Seric strcat(keybuf, "="); 288268508Seric } 288368508Seric strcat(keybuf, keyval); 288468508Seric 288568508Seric /* 288665214Seric ** If the passed directory and property name are found 288765214Seric ** in one of netinfo domains we need to search (starting 288865214Seric ** from the local domain moving all the way back to the 288965214Seric ** root domain) set propval to the property's value 289065214Seric ** and return it. 289165214Seric */ 289265214Seric 289365214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 289465214Seric { 289565214Seric if (i == 0) 289665214Seric { 289765214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 289865214Seric } 289965214Seric else 290065214Seric { 290165214Seric if (lastni != NULL) 290265214Seric ni_free(lastni); 290365214Seric lastni = ni; 290465214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 290565214Seric } 290665214Seric 290765214Seric /* 290865214Seric ** Don't bother if we didn't get a handle on a 290965214Seric ** proper domain. This is not necessarily an error. 291065214Seric ** We would get a positive ni_status if, for instance 291165214Seric ** we never found the directory or property and tried 291265214Seric ** to open the parent of the root domain! 291365214Seric */ 291465214Seric 291565214Seric if (nis != 0) 291665214Seric break; 291765214Seric 291865214Seric /* 291965214Seric ** Find the path to the server information. 292065214Seric */ 292165214Seric 292268508Seric if (ni_pathsearch(ni, &nid, keybuf) != 0) 292365214Seric continue; 292465214Seric 292565214Seric /* 292668508Seric ** Find associated value information. 292765214Seric */ 292865214Seric 292968508Seric if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0) 293065214Seric continue; 293165214Seric 293265214Seric /* 293368508Seric ** See if we have an acceptable number of values. 293465214Seric */ 293565214Seric 293668508Seric if (ninl.ni_namelist_len <= 0) 293768508Seric continue; 293868508Seric 293968508Seric if (sepchar == '\0' && ninl.ni_namelist_len > 1) 294065214Seric { 294168508Seric ni_namelist_free(&ninl); 294268508Seric continue; 294365214Seric } 294468508Seric 294568508Seric /* 294668508Seric ** Calculate number of bytes needed and build result 294768508Seric */ 294868508Seric 294968508Seric alen = 1; 295068508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 295168508Seric alen += strlen(ninl.ni_namelist_val[j]) + 1; 295268508Seric propval = p = xalloc(alen); 295368508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 295468508Seric { 295568508Seric strcpy(p, ninl.ni_namelist_val[j]); 295668508Seric p += strlen(p); 295768508Seric *p++ = sepchar; 295868508Seric } 295968508Seric *--p = '\0'; 296068508Seric 296168508Seric ni_namelist_free(&ninl); 296265214Seric } 296365214Seric 296465214Seric /* 296565214Seric ** Clean up. 296665214Seric */ 296765214Seric 296865214Seric if (ni != NULL) 296965214Seric ni_free(ni); 297065214Seric if (lastni != NULL && ni != lastni) 297165214Seric ni_free(lastni); 297265214Seric 297365214Seric return propval; 297465214Seric } 297565214Seric 297665214Seric #endif /* NETINFO */ 297765504Seric /* 297865504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 297965504Seric ** 298065504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 298165504Seric ** syslog succeeds during interrupt handlers. 298265504Seric */ 298365504Seric 298465504Seric #ifdef __hpux 298565504Seric 298665504Seric # define MAXSYSLOGTRIES 100 298765504Seric # undef syslog 298865504Seric 298965504Seric # ifdef __STDC__ 299065504Seric hard_syslog(int pri, char *msg, ...) 299165504Seric # else 299265504Seric hard_syslog(pri, msg, va_alist) 299365504Seric int pri; 299465504Seric char *msg; 299565504Seric va_dcl 299665504Seric # endif 299765504Seric { 299865504Seric int i; 299965504Seric char buf[SYSLOG_BUFSIZE * 2]; 300065504Seric VA_LOCAL_DECL; 300165504Seric 300265504Seric VA_START(msg); 300365504Seric vsprintf(buf, msg, ap); 300465504Seric VA_END; 300565504Seric 300665504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 300765504Seric continue; 300865504Seric } 300965504Seric 301065504Seric #endif 3011