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*68628Seric static char sccsid[] = "@(#)conf.c 8.142 (Berkeley) 03/29/95"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1363937Seric # include "sendmail.h" 1463937Seric # include "pathnames.h" 1514881Seric # include <sys/ioctl.h> 1658082Seric # include <sys/param.h> 1765018Seric # include <netdb.h> 1836928Sbostic # include <pwd.h> 19404Seric 20294Seric /* 213309Seric ** CONF.C -- Sendmail Configuration Tables. 22294Seric ** 23294Seric ** Defines the configuration of this installation. 24294Seric ** 251388Seric ** Configuration Variables: 262897Seric ** HdrInfo -- a table describing well-known header fields. 272897Seric ** Each entry has the field name and some flags, 284147Seric ** which are described in sendmail.h. 294093Seric ** 304093Seric ** Notes: 314093Seric ** I have tried to put almost all the reasonable 324093Seric ** configuration information into the configuration 334093Seric ** file read at runtime. My intent is that anything 344093Seric ** here is a function of the version of UNIX you 354093Seric ** are running, or is really static -- for example 364093Seric ** the headers are a superset of widely used 374093Seric ** protocols. If you find yourself playing with 384093Seric ** this file too much, you may be making a mistake! 39294Seric */ 40294Seric 41294Seric 42294Seric 43294Seric 444437Seric /* 452897Seric ** Header info table 463057Seric ** Final (null) entry contains the flags used for any other field. 474147Seric ** 484147Seric ** Not all of these are actually handled specially by sendmail 494147Seric ** at this time. They are included as placeholders, to let 504147Seric ** you know that "someday" I intend to have sendmail do 514147Seric ** something with them. 522897Seric */ 532897Seric 542897Seric struct hdrinfo HdrInfo[] = 552897Seric { 568060Seric /* originator fields, most to least significant */ 5768508Seric "resent-sender", H_FROM|H_RESENT, 5868508Seric "resent-from", H_FROM|H_RESENT, 5968508Seric "resent-reply-to", H_FROM|H_RESENT, 6068508Seric "sender", H_FROM, 6168508Seric "from", H_FROM, 6268508Seric "reply-to", H_FROM, 6368508Seric "full-name", H_ACHECK, 6468508Seric "return-receipt-to", H_FROM|H_RECEIPTTO, 6568508Seric "errors-to", H_FROM|H_ERRORSTO, 6658796Seric 678060Seric /* destination fields */ 6868508Seric "to", H_RCPT, 6968508Seric "resent-to", H_RCPT|H_RESENT, 7068508Seric "cc", H_RCPT, 7168508Seric "resent-cc", H_RCPT|H_RESENT, 7268508Seric "bcc", H_RCPT|H_STRIPVAL, 7368508Seric "resent-bcc", H_RCPT|H_STRIPVAL|H_RESENT, 7468508Seric "apparently-to", H_RCPT, 7558796Seric 768060Seric /* message identification and control */ 7768508Seric "message-id", 0, 7868508Seric "resent-message-id", H_RESENT, 7968508Seric "message", H_EOH, 8068508Seric "text", H_EOH, 8158796Seric 8211417Seric /* date fields */ 8368508Seric "date", 0, 8468508Seric "resent-date", H_RESENT, 8558796Seric 868060Seric /* trace fields */ 8768508Seric "received", H_TRACE|H_FORCE, 8868508Seric "x400-received", H_TRACE|H_FORCE, 8968508Seric "via", H_TRACE|H_FORCE, 9068508Seric "mail-from", H_TRACE|H_FORCE, 918060Seric 9258796Seric /* miscellaneous fields */ 9368508Seric "comments", H_FORCE, 9468508Seric "return-path", H_FORCE|H_ACHECK, 9568508Seric "content-transfer-encoding", H_CTE, 9668508Seric "content-type", H_CTYPE, 9758796Seric 989055Seric NULL, 0, 992897Seric }; 1004166Seric 1014166Seric 1024166Seric 1034282Seric /* 1044282Seric ** Location of system files/databases/etc. 1054282Seric */ 1064282Seric 10758082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1089039Seric 1099064Seric 1109064Seric 1119039Seric /* 11258082Seric ** Privacy values 11358082Seric */ 11458082Seric 11558082Seric struct prival PrivacyValues[] = 11658082Seric { 11758082Seric "public", PRIV_PUBLIC, 11858082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11958114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 12058082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 12158082Seric "noexpn", PRIV_NOEXPN, 12258082Seric "novrfy", PRIV_NOVRFY, 12364333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12464333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12558789Seric "authwarnings", PRIV_AUTHWARNINGS, 12666783Seric "noreceipts", PRIV_NORECEIPTS, 12758082Seric "goaway", PRIV_GOAWAY, 12858789Seric NULL, 0, 12958082Seric }; 13058082Seric 13158082Seric 13258082Seric 13358082Seric /* 13424943Seric ** Miscellaneous stuff. 1359039Seric */ 1369039Seric 13724943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13824943Seric /* 13924943Seric ** SETDEFAULTS -- set default values 14024943Seric ** 14124943Seric ** Because of the way freezing is done, these must be initialized 14224943Seric ** using direct code. 14324943Seric ** 14424943Seric ** Parameters: 14558734Seric ** e -- the default envelope. 14624943Seric ** 14724943Seric ** Returns: 14824943Seric ** none. 14924943Seric ** 15024943Seric ** Side Effects: 15124943Seric ** Initializes a bunch of global variables to their 15224943Seric ** default values. 15324943Seric */ 15424943Seric 15558737Seric #define DAYS * 24 * 60 * 60 15658737Seric 15768508Seric void 15858734Seric setdefaults(e) 15958734Seric register ENVELOPE *e; 16024943Seric { 16168508Seric int i; 16268508Seric extern void inittimeouts(); 16368508Seric extern void setdefuser(); 16468508Seric extern void setupmaps(); 16568508Seric extern void setupmailers(); 16668508Seric 16757438Seric SpaceSub = ' '; /* option B */ 16857438Seric QueueLA = 8; /* option x */ 16957438Seric RefuseLA = 12; /* option X */ 17057438Seric WkRecipFact = 30000L; /* option y */ 17157438Seric WkClassFact = 1800L; /* option z */ 17257438Seric WkTimeFact = 90000L; /* option Z */ 17357438Seric QueueFactor = WkRecipFact * 20; /* option q */ 17463787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 17557438Seric /* option F */ 17657438Seric DefUid = 1; /* option u */ 17757438Seric DefGid = 1; /* option g */ 17857438Seric CheckpointInterval = 10; /* option C */ 17957438Seric MaxHopCount = 25; /* option h */ 18058734Seric e->e_sendmode = SM_FORK; /* option d */ 18158734Seric e->e_errormode = EM_PRINT; /* option e */ 18268508Seric SevenBitInput = FALSE; /* option 7 */ 18357438Seric MaxMciCache = 1; /* option k */ 18457438Seric MciCacheTimeout = 300; /* option K */ 18557438Seric LogLevel = 9; /* option L */ 18668508Seric inittimeouts(NULL); /* option r */ 18758853Seric PrivacyFlags = 0; /* option p */ 18868508Seric MimeMode = MM_CVTMIME|MM_PASS8BIT; /* option 8 */ 18968508Seric for (i = 0; i < MAXTOCLASS; i++) 19068508Seric { 19168508Seric TimeOuts.to_q_return[i] = 5 DAYS; /* option T */ 19268508Seric TimeOuts.to_q_warning[i] = 0; /* option T */ 19368508Seric } 19468508Seric ServiceSwitchFile = "/etc/service.switch"; 19540973Sbostic setdefuser(); 19653654Seric setupmaps(); 19757402Seric setupmailers(); 19824943Seric } 199294Seric 20040973Sbostic 2014326Seric /* 20240973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20340973Sbostic */ 20440973Sbostic 20568508Seric void 20640973Sbostic setdefuser() 20740973Sbostic { 20840973Sbostic struct passwd *defpwent; 20957386Seric static char defuserbuf[40]; 21040973Sbostic 21157386Seric DefUser = defuserbuf; 21240973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 21357386Seric strcpy(defuserbuf, defpwent->pw_name); 21440973Sbostic else 21557386Seric strcpy(defuserbuf, "nobody"); 21640973Sbostic } 21753654Seric /* 21856836Seric ** HOST_MAP_INIT -- initialize host class structures 21956836Seric */ 22056836Seric 22168508Seric bool host_map_init __P((MAP *map, char *args)); 22268508Seric 22356836Seric bool 22460219Seric host_map_init(map, args) 22556836Seric MAP *map; 22656836Seric char *args; 22756836Seric { 22856836Seric register char *p = args; 22956836Seric 23056836Seric for (;;) 23156836Seric { 23258050Seric while (isascii(*p) && isspace(*p)) 23356836Seric p++; 23456836Seric if (*p != '-') 23556836Seric break; 23656836Seric switch (*++p) 23756836Seric { 23856836Seric case 'a': 23956836Seric map->map_app = ++p; 24056836Seric break; 24156836Seric } 24258050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 24356836Seric p++; 24456836Seric if (*p != '\0') 24556836Seric *p++ = '\0'; 24656836Seric } 24756836Seric if (map->map_app != NULL) 24856836Seric map->map_app = newstr(map->map_app); 24956836Seric return TRUE; 25056836Seric } 25157402Seric /* 25257402Seric ** SETUPMAILERS -- initialize default mailers 25357402Seric */ 25456836Seric 25568508Seric void 25657402Seric setupmailers() 25757402Seric { 25857402Seric char buf[100]; 25968508Seric extern void makemailer(); 26057402Seric 26168508Seric strcpy(buf, "prog, P=/bin/sh, F=lsoD, A=sh -c $u"); 26257403Seric makemailer(buf); 26357403Seric 26468508Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEou, A=FILE"); 26557402Seric makemailer(buf); 26657402Seric 26757402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 26857402Seric makemailer(buf); 26957402Seric } 27056836Seric /* 27160207Seric ** SETUPMAPS -- set up map classes 27260207Seric */ 27360207Seric 27460207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 27560207Seric { \ 27660207Seric extern bool parse __P((MAP *, char *)); \ 27760207Seric extern bool open __P((MAP *, int)); \ 27860207Seric extern void close __P((MAP *)); \ 27960207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 28060207Seric extern void store __P((MAP *, char *, char *)); \ 28160207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 28260207Seric s->s_mapclass.map_cname = name; \ 28360207Seric s->s_mapclass.map_ext = ext; \ 28460207Seric s->s_mapclass.map_cflags = flags; \ 28560207Seric s->s_mapclass.map_parse = parse; \ 28660207Seric s->s_mapclass.map_open = open; \ 28760207Seric s->s_mapclass.map_close = close; \ 28860207Seric s->s_mapclass.map_lookup = lookup; \ 28960207Seric s->s_mapclass.map_store = store; \ 29060207Seric } 29160207Seric 29268508Seric void 29360207Seric setupmaps() 29460207Seric { 29560207Seric register STAB *s; 29660207Seric 29760207Seric #ifdef NEWDB 29860207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29960207Seric map_parseargs, hash_map_open, db_map_close, 30060207Seric db_map_lookup, db_map_store); 30168508Seric 30260207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 30360207Seric map_parseargs, bt_map_open, db_map_close, 30460207Seric db_map_lookup, db_map_store); 30560207Seric #endif 30660207Seric 30760207Seric #ifdef NDBM 30860207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 30960207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 31060207Seric ndbm_map_lookup, ndbm_map_store); 31160207Seric #endif 31260207Seric 31360207Seric #ifdef NIS 31460207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 31568508Seric map_parseargs, nis_map_open, null_map_close, 31668508Seric nis_map_lookup, null_map_store); 31760207Seric #endif 31860207Seric 31968508Seric #ifdef NISPLUS 32068508Seric MAPDEF("nisplus", NULL, MCF_ALIASOK, 32168508Seric map_parseargs, nisplus_map_open, null_map_close, 32268508Seric nisplus_map_lookup, null_map_store); 32368508Seric #endif 32468508Seric 32568508Seric #ifdef HESIOD 32668508Seric MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY, 32768508Seric map_parseargs, null_map_open, null_map_close, 32868508Seric hes_map_lookup, null_map_store); 32968508Seric #endif 33068508Seric 33168508Seric #ifdef NETINFO 33268508Seric MAPDEF("netinfo", NULL, MCF_ALIASOK, 33368508Seric map_parseargs, ni_map_open, null_map_close, 33468508Seric ni_map_lookup, null_map_store); 33568508Seric #endif 33668508Seric 33768508Seric #if 0 33868508Seric MAPDEF("dns", NULL, 0, 33968508Seric dns_map_init, null_map_open, null_map_close, 34068508Seric dns_map_lookup, null_map_store); 34168508Seric #endif 34268508Seric 34368508Seric #if NAMED_BIND 34468508Seric /* best MX DNS lookup */ 34568508Seric MAPDEF("bestmx", NULL, MCF_OPTFILE, 34668508Seric map_parseargs, null_map_open, null_map_close, 34768508Seric bestmx_map_lookup, null_map_store); 34868508Seric #endif 34968508Seric 35068508Seric MAPDEF("host", NULL, 0, 35168508Seric host_map_init, null_map_open, null_map_close, 35268508Seric host_map_lookup, null_map_store); 35368508Seric 35468508Seric MAPDEF("text", NULL, MCF_ALIASOK, 35568508Seric map_parseargs, text_map_open, null_map_close, 35668508Seric text_map_lookup, null_map_store); 35768508Seric 35860207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 35968508Seric map_parseargs, stab_map_open, null_map_close, 36060207Seric stab_map_lookup, stab_map_store); 36160207Seric 36260207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 36360207Seric map_parseargs, impl_map_open, impl_map_close, 36460207Seric impl_map_lookup, impl_map_store); 36560207Seric 36668508Seric /* access to system passwd file */ 36768508Seric MAPDEF("user", NULL, MCF_OPTFILE, 36868508Seric map_parseargs, user_map_open, null_map_close, 36968508Seric user_map_lookup, null_map_store); 37060207Seric 37160207Seric /* dequote map */ 37260207Seric MAPDEF("dequote", NULL, 0, 37360207Seric dequote_init, null_map_open, null_map_close, 37460207Seric dequote_map, null_map_store); 37560207Seric 37668521Seric #ifdef USERDB 37760207Seric /* user database */ 37868521Seric MAPDEF("userdb", ".db", 0, 37968521Seric map_parseargs, null_map_open, null_map_close, 38060207Seric udb_map_lookup, null_map_store); 38160207Seric #endif 38268508Seric 38368508Seric /* sequenced maps */ 38468508Seric MAPDEF("sequence", NULL, MCF_ALIASOK, 38568508Seric seq_map_parse, null_map_open, null_map_close, 38668508Seric seq_map_lookup, seq_map_store); 38768508Seric 38868508Seric /* switched interface to sequenced maps */ 38968508Seric MAPDEF("switch", NULL, MCF_ALIASOK, 39068508Seric map_parseargs, switch_map_open, null_map_close, 39168508Seric seq_map_lookup, seq_map_store); 39260207Seric } 39360207Seric 39460207Seric #undef MAPDEF 39560207Seric /* 39668508Seric ** INITHOSTMAPS -- initial host-dependent maps 39768508Seric ** 39868508Seric ** This should act as an interface to any local service switch 39968508Seric ** provided by the host operating system. 40068508Seric ** 40168508Seric ** Parameters: 40268508Seric ** none 40368508Seric ** 40468508Seric ** Returns: 40568508Seric ** none 40668508Seric ** 40768508Seric ** Side Effects: 40868508Seric ** Should define maps "host" and "users" as necessary 40968508Seric ** for this OS. If they are not defined, they will get 41068508Seric ** a default value later. It should check to make sure 41168508Seric ** they are not defined first, since it's possible that 41268508Seric ** the config file has provided an override. 41368508Seric */ 41468508Seric 41568508Seric void 41668508Seric inithostmaps() 41768508Seric { 41868508Seric register int i; 41968508Seric int nmaps; 42068508Seric char *maptype[MAXMAPSTACK]; 42168508Seric short mapreturn[MAXMAPACTIONS]; 42268508Seric char buf[MAXLINE]; 42368508Seric 42468508Seric /* 42568508Seric ** Set up default hosts maps. 42668508Seric */ 42768508Seric 42868508Seric #if 0 42968508Seric nmaps = switch_map_find("hosts", maptype, mapreturn); 43068508Seric for (i = 0; i < nmaps; i++) 43168508Seric { 43268508Seric if (strcmp(maptype[i], "files") == 0 && 43368508Seric stab("hosts.files", ST_MAP, ST_FIND) == NULL) 43468508Seric { 43568508Seric strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts"); 43668508Seric makemapentry(buf); 43768508Seric } 43868508Seric #if NAMED_BIND 43968508Seric else if (strcmp(maptype[i], "dns") == 0 && 44068508Seric stab("hosts.dns", ST_MAP, ST_FIND) == NULL) 44168508Seric { 44268508Seric strcpy(buf, "hosts.dns dns A"); 44368508Seric makemapentry(buf); 44468508Seric } 44568508Seric #endif 44668508Seric #ifdef NISPLUS 44768508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 44868508Seric stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL) 44968508Seric { 45068508Seric strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir"); 45168508Seric makemapentry(buf); 45268508Seric } 45368508Seric #endif 45468508Seric #ifdef NIS 45568508Seric else if (strcmp(maptype[i], "nis") == 0 && 45668508Seric stab("hosts.nis", ST_MAP, ST_FIND) == NULL) 45768508Seric { 45868508Seric strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname"); 45968508Seric makemapentry(buf); 46068508Seric } 46168508Seric #endif 46268508Seric } 46368508Seric #endif 46468508Seric 46568508Seric /* 46668508Seric ** Make sure we have a host map. 46768508Seric */ 46868508Seric 46968508Seric if (stab("host", ST_MAP, ST_FIND) == NULL) 47068508Seric { 47168508Seric /* user didn't initialize: set up host map */ 47268508Seric strcpy(buf, "host host"); 47368508Seric #if NAMED_BIND 47468508Seric if (ConfigLevel >= 2) 47568508Seric strcat(buf, " -a."); 47668508Seric #endif 47768508Seric makemapentry(buf); 47868508Seric } 47968508Seric 48068508Seric /* 48168508Seric ** Set up default aliases maps 48268508Seric */ 48368508Seric 48468508Seric nmaps = switch_map_find("aliases", maptype, mapreturn); 48568508Seric for (i = 0; i < nmaps; i++) 48668508Seric { 48768508Seric if (strcmp(maptype[i], "files") == 0 && 48868508Seric stab("aliases.files", ST_MAP, ST_FIND) == NULL) 48968508Seric { 49068508Seric strcpy(buf, "aliases.files implicit /etc/aliases"); 49168508Seric makemapentry(buf); 49268508Seric } 49368508Seric #ifdef NISPLUS 49468508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 49568508Seric stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL) 49668508Seric { 49768508Seric strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir"); 49868508Seric makemapentry(buf); 49968508Seric } 50068508Seric #endif 50168508Seric #ifdef NIS 50268508Seric else if (strcmp(maptype[i], "nis") == 0 && 50368508Seric stab("aliases.nis", ST_MAP, ST_FIND) == NULL) 50468508Seric { 50568508Seric strcpy(buf, "aliases.nis nis -d mail.aliases"); 50668508Seric makemapentry(buf); 50768508Seric } 50868508Seric #endif 50968508Seric } 51068508Seric if (stab("aliases", ST_MAP, ST_FIND) == NULL) 51168508Seric { 51268508Seric strcpy(buf, "aliases switch aliases"); 51368508Seric makemapentry(buf); 51468508Seric } 51568508Seric strcpy(buf, "switch:aliases"); 51668508Seric setalias(buf); 51768508Seric 51868508Seric #if 0 /* "user" map class is a better choice */ 51968508Seric /* 52068508Seric ** Set up default users maps. 52168508Seric */ 52268508Seric 52368508Seric nmaps = switch_map_find("passwd", maptype, mapreturn); 52468508Seric for (i = 0; i < nmaps; i++) 52568508Seric { 52668508Seric if (strcmp(maptype[i], "files") == 0 && 52768508Seric stab("users.files", ST_MAP, ST_FIND) == NULL) 52868508Seric { 52968508Seric strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd"); 53068508Seric makemapentry(buf); 53168508Seric } 53268508Seric #ifdef NISPLUS 53368508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 53468508Seric stab("users.nisplus", ST_MAP, ST_FIND) == NULL) 53568508Seric { 53668508Seric strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir"); 53768508Seric makemapentry(buf); 53868508Seric } 53968508Seric #endif 54068508Seric #ifdef NIS 54168508Seric else if (strcmp(maptype[i], "nis") == 0 && 54268508Seric stab("users.nis", ST_MAP, ST_FIND) == NULL) 54368508Seric { 54468508Seric strcpy(buf, "users.nis nis -m -d passwd.byname"); 54568508Seric makemapentry(buf); 54668508Seric } 54768508Seric #endif 54868508Seric #ifdef HESIOD 54968508Seric else if (strcmp(maptype[i], "hesiod") == 0) && 55068508Seric stab("users.hesiod", ST_MAP, ST_FIND) == NULL) 55168508Seric { 55268508Seric strcpy(buf, "users.hesiod hesiod"); 55368508Seric makemapentry(buf); 55468508Seric } 55568508Seric #endif 55668508Seric } 55768508Seric if (stab("users", ST_MAP, ST_FIND) == NULL) 55868508Seric { 55968508Seric strcpy(buf, "users switch -m passwd"); 56068508Seric makemapentry(buf); 56168508Seric } 56268508Seric #endif 56368508Seric } 56468508Seric /* 56568508Seric ** SWITCH_MAP_FIND -- find the list of types associated with a map 56668508Seric ** 56768508Seric ** This is the system-dependent interface to the service switch. 56868508Seric ** 56968508Seric ** Parameters: 57068508Seric ** service -- the name of the service of interest. 57168508Seric ** maptype -- an out-array of strings containing the types 57268508Seric ** of access to use for this service. There can 57368508Seric ** be at most MAXMAPSTACK types for a single service. 57468508Seric ** mapreturn -- an out-array of return information bitmaps 57568508Seric ** for the map. 57668508Seric ** 57768508Seric ** Returns: 57868508Seric ** The number of map types filled in, or -1 for failure. 57968508Seric */ 58068508Seric 58168508Seric #ifdef SOLARIS 58268508Seric # include <nsswitch.h> 58368508Seric #endif 58468508Seric 58568508Seric #if defined(ultrix) || defined(__osf__) 58668508Seric # include <sys/svcinfo.h> 58768508Seric #endif 58868508Seric 58968508Seric int 59068508Seric switch_map_find(service, maptype, mapreturn) 59168508Seric char *service; 59268508Seric char *maptype[MAXMAPSTACK]; 59368508Seric short mapreturn[MAXMAPACTIONS]; 59468508Seric { 59568508Seric register FILE *fp; 59668508Seric int svcno; 59768508Seric static char buf[MAXLINE]; 59868508Seric 59968508Seric #ifdef SOLARIS 60068508Seric struct __nsw_switchconfig *nsw_conf; 60168508Seric enum __nsw_parse_err pserr; 60268508Seric struct __nsw_lookup *lk; 60368508Seric int nsw_rc; 60468508Seric static struct __nsw_lookup lkp0 = 60568508Seric { "files", {1, 0, 0, 0}, NULL, NULL }; 60668508Seric static struct __nsw_switchconfig lkp_default = 60768508Seric { 0, "sendmail", 3, &lkp0 }; 60868508Seric 60968508Seric if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL) 61068508Seric lk = lkp_default.lookups; 61168508Seric else 61268508Seric lk = nsw_conf->lookups; 61368508Seric svcno = 0; 61468508Seric while (lk != NULL) 61568508Seric { 61668508Seric maptype[svcno] = lk->service_name; 61768508Seric if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN) 61868508Seric mapreturn[MA_NOTFOUND] |= 1 << svcno; 61968508Seric if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN) 62068508Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 62168508Seric if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN) 62268508Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 62368508Seric svcno++; 62468508Seric lk = lk->next; 62568508Seric } 62668508Seric return svcno; 62768508Seric #endif 62868508Seric 62968508Seric #if defined(ultrix) || defined(__osf__) 63068508Seric struct svcinfo *svcinfo; 63168508Seric int svc; 63268508Seric 63368508Seric svcinfo = getsvc(); 63468508Seric if (svcinfo == NULL) 63568508Seric goto punt; 63668508Seric if (strcmp(service, "hosts") == 0) 63768508Seric svc = SVC_HOSTS; 63868508Seric else if (strcmp(service, "aliases") == 0) 63968508Seric svc = SVC_ALIASES; 64068508Seric else if (strcmp(service, "passwd") == 0) 64168508Seric svc = SVC_PASSWD; 64268508Seric else 64368508Seric return -1; 64468508Seric for (svcno = 0; svcno < SVC_PATHSIZE; svcno++) 64568508Seric { 64668508Seric switch (svcinfo->svcpath[svc][svcno]) 64768508Seric { 64868508Seric case SVC_LOCAL: 64968508Seric maptype[svcno] = "files"; 65068508Seric break; 65168508Seric 65268508Seric case SVC_YP: 65368508Seric maptype[svcno] = "nis"; 65468508Seric break; 65568508Seric 65668508Seric case SVC_BIND: 65768508Seric maptype[svcno] = "dns"; 65868508Seric break; 65968508Seric 66068508Seric #ifdef SVC_HESIOD 66168508Seric case SVC_HESIOD: 66268508Seric maptype[svcno] = "hesiod"; 66368508Seric break; 66468508Seric #endif 66568508Seric 66668508Seric case SVC_LAST: 66768508Seric return svcno; 66868508Seric } 66968508Seric } 67068508Seric return svcno; 67168508Seric #endif 67268508Seric 67368508Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__) 67468508Seric /* 67568508Seric ** Fall-back mechanism. 67668508Seric */ 67768508Seric 67868508Seric svcno = 0; 67968508Seric fp = fopen(ServiceSwitchFile, "r"); 68068508Seric if (fp != NULL) 68168508Seric { 68268508Seric while (fgets(buf, sizeof buf, fp) != NULL) 68368508Seric { 68468508Seric register char *p; 68568508Seric 68668508Seric p = strpbrk(buf, "#\n"); 68768508Seric if (p != NULL) 68868508Seric *p = '\0'; 68968508Seric p = strpbrk(buf, " \t"); 69068508Seric if (p != NULL) 69168508Seric *p++ = '\0'; 69268508Seric if (strcmp(buf, service) != 0) 69368508Seric continue; 69468508Seric 69568508Seric /* got the right service -- extract data */ 69668508Seric do 69768508Seric { 69868508Seric while (isspace(*p)) 69968508Seric p++; 70068508Seric if (*p == '\0') 70168508Seric break; 70268508Seric maptype[svcno++] = p; 70368508Seric p = strpbrk(p, " \t"); 70468508Seric if (p != NULL) 70568508Seric *p++ = '\0'; 70668508Seric } while (p != NULL); 70768508Seric break; 70868508Seric } 70968508Seric fclose(fp); 71068508Seric return svcno; 71168508Seric } 71268508Seric #endif 71368508Seric 71468508Seric /* if the service file doesn't work, use an absolute fallback */ 71568508Seric punt: 71668508Seric if (strcmp(service, "aliases") == 0) 71768508Seric { 718*68628Seric maptype[svcno++] = "files"; 719*68628Seric #ifdef AUTO_NIS_ALIASES 720*68628Seric # ifdef NISPLUS 721*68628Seric maptype[svcno++] = "nisplus"; 722*68628Seric # endif 723*68628Seric # ifdef NIS 724*68628Seric maptype[svcno++] = "nis"; 725*68628Seric # endif 726*68628Seric #endif 727*68628Seric return svcno; 72868508Seric } 72968508Seric if (strcmp(service, "hosts") == 0) 73068508Seric { 73168508Seric # if NAMED_BIND 73268508Seric maptype[svcno++] = "dns"; 73368508Seric # else 73468508Seric # if defined(sun) && !defined(BSD) && !defined(SOLARIS) 73568508Seric /* SunOS */ 73668508Seric maptype[svcno++] = "nis"; 73768508Seric # endif 73868508Seric # endif 73968508Seric maptype[svcno++] = "files"; 74068508Seric return svcno; 74168508Seric } 74268508Seric return -1; 74368508Seric } 74468508Seric /* 7459369Seric ** USERNAME -- return the user id of the logged in user. 7469369Seric ** 7479369Seric ** Parameters: 7489369Seric ** none. 7499369Seric ** 7509369Seric ** Returns: 7519369Seric ** The login name of the logged in user. 7529369Seric ** 7539369Seric ** Side Effects: 7549369Seric ** none. 7559369Seric ** 7569369Seric ** Notes: 7579369Seric ** The return value is statically allocated. 7589369Seric */ 7599369Seric 7609369Seric char * 7619369Seric username() 7629369Seric { 76317469Seric static char *myname = NULL; 7649369Seric extern char *getlogin(); 76519904Smiriam register struct passwd *pw; 7669369Seric 76717469Seric /* cache the result */ 76817469Seric if (myname == NULL) 76917469Seric { 77017469Seric myname = getlogin(); 77117469Seric if (myname == NULL || myname[0] == '\0') 77217469Seric { 77363787Seric pw = getpwuid(RealUid); 77417469Seric if (pw != NULL) 77540993Sbostic myname = newstr(pw->pw_name); 77617469Seric } 77719904Smiriam else 77819904Smiriam { 77963787Seric uid_t uid = RealUid; 78019873Smiriam 78140993Sbostic myname = newstr(myname); 78240993Sbostic if ((pw = getpwnam(myname)) == NULL || 78358736Seric (uid != 0 && uid != pw->pw_uid)) 78419904Smiriam { 78558736Seric pw = getpwuid(uid); 78624945Seric if (pw != NULL) 78740993Sbostic myname = newstr(pw->pw_name); 78819873Smiriam } 78919873Smiriam } 79017469Seric if (myname == NULL || myname[0] == '\0') 79117469Seric { 79258151Seric syserr("554 Who are you?"); 79317469Seric myname = "postmaster"; 79417469Seric } 79517469Seric } 79617469Seric 79717469Seric return (myname); 7989369Seric } 7999369Seric /* 8004190Seric ** TTYPATH -- Get the path of the user's tty 801294Seric ** 802294Seric ** Returns the pathname of the user's tty. Returns NULL if 803294Seric ** the user is not logged in or if s/he has write permission 804294Seric ** denied. 805294Seric ** 806294Seric ** Parameters: 807294Seric ** none 808294Seric ** 809294Seric ** Returns: 810294Seric ** pathname of the user's tty. 811294Seric ** NULL if not logged in or write permission denied. 812294Seric ** 813294Seric ** Side Effects: 814294Seric ** none. 815294Seric ** 816294Seric ** WARNING: 817294Seric ** Return value is in a local buffer. 818294Seric ** 819294Seric ** Called By: 820294Seric ** savemail 821294Seric */ 822294Seric 823294Seric char * 824294Seric ttypath() 825294Seric { 826294Seric struct stat stbuf; 827294Seric register char *pathn; 828294Seric extern char *ttyname(); 8294081Seric extern char *getlogin(); 830294Seric 831294Seric /* compute the pathname of the controlling tty */ 8329369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 8339369Seric (pathn = ttyname(0)) == NULL) 834294Seric { 835294Seric errno = 0; 836294Seric return (NULL); 837294Seric } 838294Seric 839294Seric /* see if we have write permission */ 8402967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 841294Seric { 842294Seric errno = 0; 843294Seric return (NULL); 844294Seric } 845294Seric 846294Seric /* see if the user is logged in */ 847294Seric if (getlogin() == NULL) 848294Seric return (NULL); 849294Seric 850294Seric /* looks good */ 851294Seric return (pathn); 852294Seric } 8532967Seric /* 8542967Seric ** CHECKCOMPAT -- check for From and To person compatible. 8552967Seric ** 8562967Seric ** This routine can be supplied on a per-installation basis 8572967Seric ** to determine whether a person is allowed to send a message. 8582967Seric ** This allows restriction of certain types of internet 8592967Seric ** forwarding or registration of users. 8602967Seric ** 8612967Seric ** If the hosts are found to be incompatible, an error 86257454Seric ** message should be given using "usrerr" and 0 should 8632967Seric ** be returned. 8642967Seric ** 86568559Seric ** EF_NO_BODY_RETN can be set in e->e_flags to suppress the 86668559Seric ** body during the return-to-sender function; this should be done 86768559Seric ** on huge messages. This bit may already be set by the ESMTP 86868559Seric ** protocol. 8694288Seric ** 8702967Seric ** Parameters: 8712967Seric ** to -- the person being sent to. 8722967Seric ** 8732967Seric ** Returns: 87457459Seric ** an exit status 8752967Seric ** 8762967Seric ** Side Effects: 8772967Seric ** none (unless you include the usrerr stuff) 8782967Seric */ 8792967Seric 88068508Seric int 88155012Seric checkcompat(to, e) 8822967Seric register ADDRESS *to; 88355012Seric register ENVELOPE *e; 8842967Seric { 88512133Seric # ifdef lint 88612133Seric if (to == NULL) 88712133Seric to++; 88863834Seric # endif /* lint */ 88964402Seric 89064402Seric if (tTd(49, 1)) 89164402Seric printf("checkcompat(to=%s, from=%s)\n", 89264402Seric to->q_paddr, e->e_from.q_paddr); 89364402Seric 89410698Seric # ifdef EXAMPLE_CODE 89510698Seric /* this code is intended as an example only */ 8964437Seric register STAB *s; 8974437Seric 8984437Seric s = stab("arpa", ST_MAILER, ST_FIND); 89968508Seric if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 && 9009369Seric to->q_mailer == s->s_mailer) 9014437Seric { 90258151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 90368559Seric /* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */ 90457459Seric return (EX_UNAVAILABLE); 9054437Seric } 90656795Seric # endif /* EXAMPLE_CODE */ 90757459Seric return (EX_OK); 9082967Seric } 9099369Seric /* 91064035Seric ** SETSIGNAL -- set a signal handler 91164035Seric ** 91264035Seric ** This is essentially old BSD "signal(3)". 91364035Seric */ 91464035Seric 91564561Seric sigfunc_t 91664035Seric setsignal(sig, handler) 91764035Seric int sig; 91864561Seric sigfunc_t handler; 91964035Seric { 92064380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 92164035Seric return signal(sig, handler); 92264035Seric #else 92364035Seric struct sigaction n, o; 92464035Seric 92564035Seric bzero(&n, sizeof n); 92664035Seric n.sa_handler = handler; 92768508Seric # ifdef SA_RESTART 92868508Seric n.sa_flags = SA_RESTART; 92968508Seric # endif 93064035Seric if (sigaction(sig, &n, &o) < 0) 93164035Seric return SIG_ERR; 93264035Seric return o.sa_handler; 93364035Seric #endif 93464035Seric } 93564035Seric /* 9369369Seric ** HOLDSIGS -- arrange to hold all signals 9379369Seric ** 9389369Seric ** Parameters: 9399369Seric ** none. 9409369Seric ** 9419369Seric ** Returns: 9429369Seric ** none. 9439369Seric ** 9449369Seric ** Side Effects: 9459369Seric ** Arranges that signals are held. 9469369Seric */ 9479369Seric 94868508Seric void 9499369Seric holdsigs() 9509369Seric { 9519369Seric } 9529369Seric /* 9539369Seric ** RLSESIGS -- arrange to release all signals 9549369Seric ** 9559369Seric ** This undoes the effect of holdsigs. 9569369Seric ** 9579369Seric ** Parameters: 9589369Seric ** none. 9599369Seric ** 9609369Seric ** Returns: 9619369Seric ** none. 9629369Seric ** 9639369Seric ** Side Effects: 9649369Seric ** Arranges that signals are released. 9659369Seric */ 9669369Seric 96768508Seric void 9689369Seric rlsesigs() 9699369Seric { 9709369Seric } 97114872Seric /* 97264705Seric ** INIT_MD -- do machine dependent initializations 97364705Seric ** 97464705Seric ** Systems that have global modes that should be set should do 97564705Seric ** them here rather than in main. 97664705Seric */ 97764705Seric 97864705Seric #ifdef _AUX_SOURCE 97964705Seric # include <compat.h> 98064705Seric #endif 98164705Seric 98268508Seric void 98364825Seric init_md(argc, argv) 98464825Seric int argc; 98564825Seric char **argv; 98664705Seric { 98764705Seric #ifdef _AUX_SOURCE 98864705Seric setcompat(getcompat() | COMPAT_BSDPROT); 98964705Seric #endif 99068508Seric 99168508Seric #ifdef VENDOR_DEFAULT 99268508Seric VendorCode = VENDOR_DEFAULT; 99368508Seric #else 99468508Seric VendorCode = VENDOR_BERKELEY; 99568508Seric #endif 99664705Seric } 99764705Seric /* 99868508Seric ** INIT_VENDOR_MACROS -- vendor-dependent macro initializations 99968508Seric ** 100068508Seric ** Called once, on startup. 100168508Seric ** 100268508Seric ** Parameters: 100368508Seric ** e -- the global envelope. 100468508Seric ** 100568508Seric ** Returns: 100668508Seric ** none. 100768508Seric ** 100868508Seric ** Side Effects: 100968508Seric ** vendor-dependent. 101068508Seric */ 101168508Seric 101268508Seric void 101368508Seric init_vendor_macros(e) 101468508Seric register ENVELOPE *e; 101568508Seric { 101668508Seric } 101768508Seric /* 101814872Seric ** GETLA -- get the current load average 101914872Seric ** 102014881Seric ** This code stolen from la.c. 102114881Seric ** 102214872Seric ** Parameters: 102314872Seric ** none. 102414872Seric ** 102514872Seric ** Returns: 102614872Seric ** The current load average as an integer. 102714872Seric ** 102814872Seric ** Side Effects: 102914872Seric ** none. 103014872Seric */ 103114872Seric 103251920Seric /* try to guess what style of load average we have */ 103351920Seric #define LA_ZERO 1 /* always return load average as zero */ 103464360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 103551920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 103651920Seric #define LA_SUBR 4 /* call getloadavg */ 103764295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 103864360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 103966301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 104014872Seric 104163962Seric /* do guesses based on general OS type */ 104251920Seric #ifndef LA_TYPE 104363962Seric # define LA_TYPE LA_ZERO 104451920Seric #endif 104551920Seric 104664360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 104751920Seric 104814872Seric #include <nlist.h> 104914872Seric 105068508Seric #ifdef IRIX64 105168508Seric # define nlist nlist64 105268508Seric #endif 105368508Seric 105451920Seric #ifndef LA_AVENRUN 105563962Seric # ifdef SYSTEM5 105663962Seric # define LA_AVENRUN "avenrun" 105763962Seric # else 105863962Seric # define LA_AVENRUN "_avenrun" 105963962Seric # endif 106051920Seric #endif 106151920Seric 106251920Seric /* _PATH_UNIX should be defined in <paths.h> */ 106351920Seric #ifndef _PATH_UNIX 106463962Seric # if defined(SYSTEM5) 106563962Seric # define _PATH_UNIX "/unix" 106663962Seric # else 106763962Seric # define _PATH_UNIX "/vmunix" 106863962Seric # endif 106951920Seric #endif 107051920Seric 107114872Seric struct nlist Nl[] = 107214872Seric { 107351920Seric { LA_AVENRUN }, 107414872Seric #define X_AVENRUN 0 107514872Seric { 0 }, 107614872Seric }; 107714872Seric 107860583Seric #ifndef FSHIFT 107960583Seric # if defined(unixpc) 108060583Seric # define FSHIFT 5 108160583Seric # endif 108257736Seric 108366763Seric # if defined(__alpha) || defined(IRIX) 108460583Seric # define FSHIFT 10 108560583Seric # endif 108668508Seric 108768508Seric # if defined(_AIX3) 108868508Seric # define FSHIFT 16 108968508Seric # endif 109066788Seric #endif 109157977Seric 109266788Seric #ifndef FSHIFT 109366788Seric # define FSHIFT 8 109457736Seric #endif 109560583Seric 109666788Seric #ifndef FSCALE 109766788Seric # define FSCALE (1 << FSHIFT) 109851920Seric #endif 109940930Srick 110014872Seric getla() 110114872Seric { 110214872Seric static int kmem = -1; 110351920Seric #if LA_TYPE == LA_INT 110424943Seric long avenrun[3]; 110551920Seric #else 110664360Seric # if LA_TYPE == LA_SHORT 110764360Seric short avenrun[3]; 110864360Seric # else 110951920Seric double avenrun[3]; 111064360Seric # endif 111151920Seric #endif 111225615Seric extern off_t lseek(); 111357736Seric extern int errno; 111414872Seric 111514872Seric if (kmem < 0) 111614872Seric { 111724945Seric kmem = open("/dev/kmem", 0, 0); 111814872Seric if (kmem < 0) 111957736Seric { 112057736Seric if (tTd(3, 1)) 112157736Seric printf("getla: open(/dev/kmem): %s\n", 112257736Seric errstring(errno)); 112314872Seric return (-1); 112457736Seric } 112551920Seric (void) fcntl(kmem, F_SETFD, 1); 112668508Seric #ifdef _AIX3 112768508Seric if (knlist(Nl, 1, sizeof Nl[0]) < 0) 112868508Seric #else 112957736Seric if (nlist(_PATH_UNIX, Nl) < 0) 113068508Seric #endif 113157736Seric { 113257736Seric if (tTd(3, 1)) 113357736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 113457736Seric errstring(errno)); 113514872Seric return (-1); 113657736Seric } 113759253Seric if (Nl[X_AVENRUN].n_value == 0) 113859253Seric { 113959253Seric if (tTd(3, 1)) 114059253Seric printf("getla: nlist(%s, %s) ==> 0\n", 114159253Seric _PATH_UNIX, LA_AVENRUN); 114259253Seric return (-1); 114359253Seric } 114468508Seric #ifdef NAMELISTMASK 114568508Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 114666763Seric #endif 114714872Seric } 114857736Seric if (tTd(3, 20)) 114957736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 115068508Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 || 115123118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 115219967Seric { 115319967Seric /* thank you Ian */ 115457736Seric if (tTd(3, 1)) 115557736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 115619967Seric return (-1); 115719967Seric } 115864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 115957736Seric if (tTd(3, 5)) 116057736Seric { 116157736Seric printf("getla: avenrun = %d", avenrun[0]); 116257736Seric if (tTd(3, 15)) 116357736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 116457736Seric printf("\n"); 116557736Seric } 116657736Seric if (tTd(3, 1)) 116757736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 116824943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 116951920Seric #else 117057736Seric if (tTd(3, 5)) 117157736Seric { 117257736Seric printf("getla: avenrun = %g", avenrun[0]); 117357736Seric if (tTd(3, 15)) 117457736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 117557736Seric printf("\n"); 117657736Seric } 117757736Seric if (tTd(3, 1)) 117857736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 117951920Seric return ((int) (avenrun[0] + 0.5)); 118051920Seric #endif 118114872Seric } 118214872Seric 118351773Seric #else 118451920Seric #if LA_TYPE == LA_SUBR 118551773Seric 118664718Seric #ifdef DGUX 118764718Seric 118864718Seric #include <sys/dg_sys_info.h> 118964718Seric 119068508Seric int 119168508Seric getla() 119264718Seric { 119364718Seric struct dg_sys_info_load_info load_info; 119464718Seric 119564718Seric dg_sys_info((long *)&load_info, 119664718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 119764718Seric 119868508Seric if (tTd(3, 1)) 119968508Seric printf("getla: %d\n", (int) (load_info.one_minute + 0.5)); 120068508Seric 120164718Seric return((int) (load_info.one_minute + 0.5)); 120264718Seric } 120364718Seric 120464718Seric #else 120568508Seric # ifdef __hpux 120664718Seric 120768508Seric # include <sys/param.h> 120868508Seric # include <sys/pstat.h> 120968508Seric 121068508Seric int 121151773Seric getla() 121251773Seric { 121368508Seric struct pst_dynamic pstd; 121468508Seric 121568508Seric if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic), 121668508Seric (size_t) 1 ,0) == -1) 121768508Seric return 0; 121868508Seric 121968508Seric if (tTd(3, 1)) 122068508Seric printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5)); 122168508Seric 122268508Seric return (int) (pstd.psd_avg_1_min + 0.5); 122368508Seric } 122468508Seric 122568508Seric # else 122668508Seric 122768508Seric int 122868508Seric getla() 122968508Seric { 123051920Seric double avenrun[3]; 123151920Seric 123251920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 123357736Seric { 123457736Seric if (tTd(3, 1)) 123557736Seric perror("getla: getloadavg failed:"); 123651920Seric return (-1); 123757736Seric } 123857736Seric if (tTd(3, 1)) 123957736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 124051920Seric return ((int) (avenrun[0] + 0.5)); 124151773Seric } 124251773Seric 124368508Seric # endif /* __hpux */ 124464718Seric #endif /* DGUX */ 124551773Seric #else 124664295Seric #if LA_TYPE == LA_MACH 124751773Seric 124864295Seric /* 124965173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 125064295Seric */ 125164295Seric 125265173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 125365173Seric # include <mach/mach.h> 125465173Seric #else 125565173Seric # include <mach.h> 125665173Seric #endif 125764295Seric 125851773Seric getla() 125951773Seric { 126064295Seric processor_set_t default_set; 126164295Seric kern_return_t error; 126264295Seric unsigned int info_count; 126364295Seric struct processor_set_basic_info info; 126464295Seric host_t host; 126564295Seric 126664295Seric error = processor_set_default(host_self(), &default_set); 126764295Seric if (error != KERN_SUCCESS) 126864295Seric return -1; 126964295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 127064295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 127164295Seric &host, (processor_set_info_t)&info, 127264295Seric &info_count) != KERN_SUCCESS) 127364295Seric { 127464295Seric return -1; 127564295Seric } 127664295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 127764295Seric } 127864295Seric 127964295Seric 128064295Seric #else 128166313Seric #if LA_TYPE == LA_PROCSTR 128264295Seric 128366301Seric /* 128466301Seric ** Read /proc/loadavg for the load average. This is assumed to be 128566301Seric ** in a format like "0.15 0.12 0.06". 128666301Seric ** 128766301Seric ** Initially intended for Linux. This has been in the kernel 128866301Seric ** since at least 0.99.15. 128966301Seric */ 129066301Seric 129166301Seric # ifndef _PATH_LOADAVG 129266301Seric # define _PATH_LOADAVG "/proc/loadavg" 129366301Seric # endif 129466301Seric 129566301Seric int 129664295Seric getla() 129764295Seric { 129866319Seric double avenrun; 129966301Seric register int result; 130066319Seric FILE *fp; 130166301Seric 130266319Seric fp = fopen(_PATH_LOADAVG, "r"); 130366319Seric if (fp == NULL) 130466301Seric { 130566319Seric if (tTd(3, 1)) 130666319Seric printf("getla: fopen(%s): %s\n", 130766319Seric _PATH_LOADAVG, errstring(errno)); 130866319Seric return -1; 130966301Seric } 131066301Seric result = fscanf(fp, "%lf", &avenrun); 131166319Seric fclose(fp); 131266301Seric if (result != 1) 131366301Seric { 131466310Seric if (tTd(3, 1)) 131566301Seric printf("getla: fscanf() = %d: %s\n", 131666301Seric result, errstring(errno)); 131766301Seric return -1; 131866301Seric } 131966301Seric 132057736Seric if (tTd(3, 1)) 132166301Seric printf("getla(): %.2f\n", avenrun); 132266301Seric 132366301Seric return ((int) (avenrun + 0.5)); 132466301Seric } 132566301Seric 132666301Seric #else 132766301Seric 132866301Seric getla() 132966301Seric { 133066301Seric if (tTd(3, 1)) 133157736Seric printf("getla: ZERO\n"); 133251920Seric return (0); 133351773Seric } 133451773Seric 133551773Seric #endif 133651773Seric #endif 133764295Seric #endif 133866301Seric #endif 133966045Seric 134066045Seric 134166045Seric /* 134266045Seric * Copyright 1989 Massachusetts Institute of Technology 134366045Seric * 134466045Seric * Permission to use, copy, modify, distribute, and sell this software and its 134566045Seric * documentation for any purpose is hereby granted without fee, provided that 134666045Seric * the above copyright notice appear in all copies and that both that 134766045Seric * copyright notice and this permission notice appear in supporting 134866045Seric * documentation, and that the name of M.I.T. not be used in advertising or 134966045Seric * publicity pertaining to distribution of the software without specific, 135066045Seric * written prior permission. M.I.T. makes no representations about the 135166045Seric * suitability of this software for any purpose. It is provided "as is" 135266045Seric * without express or implied warranty. 135366045Seric * 135466045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 135566045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 135666045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 135766045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 135866045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 135966045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 136066045Seric * 136166045Seric * Authors: Many and varied... 136266045Seric */ 136366045Seric 136466045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 136566045Seric #ifndef lint 136666045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 136766045Seric #endif /* !lint */ 136866045Seric 136966179Seric #ifdef apollo 137066045Seric # undef volatile 137166045Seric # include <apollo/base.h> 137266045Seric 137366045Seric /* ARGSUSED */ 137466045Seric int getloadavg( call_data ) 137566045Seric caddr_t call_data; /* pointer to (double) return value */ 137666045Seric { 137766045Seric double *avenrun = (double *) call_data; 137866045Seric int i; 137966045Seric status_$t st; 138066045Seric long loadav[3]; 138166045Seric proc1_$get_loadav(loadav, &st); 138266045Seric *avenrun = loadav[0] / (double) (1 << 16); 138366045Seric return(0); 138466045Seric } 138566045Seric # endif /* apollo */ 138624943Seric /* 138724943Seric ** SHOULDQUEUE -- should this message be queued or sent? 138824943Seric ** 138924943Seric ** Compares the message cost to the load average to decide. 139024943Seric ** 139124943Seric ** Parameters: 139224943Seric ** pri -- the priority of the message in question. 139357438Seric ** ctime -- the message creation time. 139424943Seric ** 139524943Seric ** Returns: 139624943Seric ** TRUE -- if this message should be queued up for the 139724943Seric ** time being. 139824943Seric ** FALSE -- if the load is low enough to send this message. 139924943Seric ** 140024943Seric ** Side Effects: 140124943Seric ** none. 140224943Seric */ 140324943Seric 140424943Seric bool 140557438Seric shouldqueue(pri, ctime) 140624943Seric long pri; 140757438Seric time_t ctime; 140824943Seric { 140968508Seric bool rval; 141068508Seric 141168508Seric if (tTd(3, 30)) 141268508Seric printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri); 141351920Seric if (CurrentLA < QueueLA) 141468508Seric { 141568508Seric if (tTd(3, 30)) 141668508Seric printf("FALSE (CurrentLA < QueueLA)\n"); 141724943Seric return (FALSE); 141868508Seric } 141958132Seric if (CurrentLA >= RefuseLA) 142068508Seric { 142168508Seric if (tTd(3, 30)) 142268508Seric printf("TRUE (CurrentLA >= RefuseLA)\n"); 142358132Seric return (TRUE); 142468508Seric } 142568508Seric rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1)); 142668508Seric if (tTd(3, 30)) 142768508Seric printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE"); 142868508Seric return rval; 142924943Seric } 143024943Seric /* 143153037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 143253037Seric ** 143353037Seric ** Parameters: 143453037Seric ** none. 143553037Seric ** 143653037Seric ** Returns: 143753037Seric ** TRUE if incoming SMTP connections should be refused 143853037Seric ** (for now). 143953037Seric ** FALSE if we should accept new work. 144053037Seric ** 144153037Seric ** Side Effects: 144253037Seric ** none. 144353037Seric */ 144453037Seric 144553037Seric bool 144653037Seric refuseconnections() 144753037Seric { 144868508Seric extern bool enoughspace(); 144968508Seric 145059156Seric #ifdef XLA 145159156Seric if (!xla_smtp_ok()) 145259156Seric return TRUE; 145359156Seric #endif 145459156Seric 145553037Seric /* this is probably too simplistic */ 145668508Seric return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1); 145753037Seric } 145853037Seric /* 145924943Seric ** SETPROCTITLE -- set process title for ps 146024943Seric ** 146124943Seric ** Parameters: 146258674Seric ** fmt -- a printf style format string. 146358674Seric ** a, b, c -- possible parameters to fmt. 146424943Seric ** 146524943Seric ** Returns: 146624943Seric ** none. 146724943Seric ** 146824943Seric ** Side Effects: 146924943Seric ** Clobbers argv of our main procedure so ps(1) will 147024943Seric ** display the title. 147124943Seric */ 147224943Seric 147368508Seric #define SPT_NONE 0 /* don't use it at all */ 147468508Seric #define SPT_REUSEARGV 1 /* cover argv with title information */ 147568508Seric #define SPT_BUILTIN 2 /* use libc builtin */ 147668508Seric #define SPT_PSTAT 3 /* use pstat(PSTAT_SETCMD, ...) */ 147768508Seric #define SPT_PSSTRINGS 4 /* use PS_STRINGS->... */ 147868508Seric #define SPT_WRITEUDOT 5 /* write u. area in kmem */ 147968508Seric 148068508Seric #ifndef SPT_TYPE 148168508Seric # define SPT_TYPE SPT_REUSEARGV 148268508Seric #endif 148368508Seric 148468508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN 148568508Seric 148668508Seric # if SPT_TYPE == SPT_PSTAT 148758689Seric # include <sys/pstat.h> 148858689Seric # endif 148968508Seric # if SPT_TYPE == SPT_PSSTRINGS 149059732Seric # include <machine/vmparam.h> 149159732Seric # include <sys/exec.h> 149268508Seric # ifndef PS_STRINGS /* hmmmm.... apparently not available after all */ 149368508Seric # undef SPT_TYPE 149468508Seric # define SPT_TYPE SPT_REUSEARGV 149564247Seric # endif 149659732Seric # endif 149768508Seric 149868508Seric # if SPT_TYPE == SPT_PSSTRINGS 149968508Seric # define SETPROC_STATIC static 150068508Seric # else 150159732Seric # define SETPROC_STATIC 150259732Seric # endif 150367626Seric 150468508Seric # ifndef SPT_PADCHAR 150568508Seric # define SPT_PADCHAR ' ' 150668508Seric # endif 150758689Seric 150868508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */ 150964843Seric 151068508Seric #if SPT_TYPE != SPT_BUILTIN 151168508Seric 151224943Seric /*VARARGS1*/ 151368508Seric void 151468508Seric # ifdef __STDC__ 151557642Seric setproctitle(char *fmt, ...) 151668508Seric # else 151757642Seric setproctitle(fmt, va_alist) 151824943Seric char *fmt; 151957642Seric va_dcl 152068508Seric # endif 152124943Seric { 152268508Seric # if SPT_TYPE != SPT_NONE 152324943Seric register char *p; 152425049Seric register int i; 152559732Seric SETPROC_STATIC char buf[MAXLINE]; 152656852Seric VA_LOCAL_DECL 152768508Seric # if SPT_TYPE == SPT_PSTAT 152858689Seric union pstun pst; 152958689Seric # endif 153024943Seric extern char **Argv; 153124943Seric extern char *LastArgv; 153224943Seric 153358674Seric p = buf; 153424943Seric 153558674Seric /* print sendmail: heading for grep */ 153658674Seric (void) strcpy(p, "sendmail: "); 153758674Seric p += strlen(p); 153824943Seric 153958674Seric /* print the argument string */ 154058674Seric VA_START(fmt); 154158674Seric (void) vsprintf(p, fmt, ap); 154256852Seric VA_END; 154354996Seric 154458674Seric i = strlen(buf); 154558689Seric 154668508Seric # if SPT_TYPE == SPT_PSTAT 154758689Seric pst.pst_command = buf; 154858689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 154958689Seric # else 155068508Seric # if SPT_TYPE == SPT_PSSTRINGS 155159732Seric PS_STRINGS->ps_nargvstr = 1; 155259732Seric PS_STRINGS->ps_argvstr = buf; 155359732Seric # else 155454996Seric if (i > LastArgv - Argv[0] - 2) 155525049Seric { 155654996Seric i = LastArgv - Argv[0] - 2; 155758674Seric buf[i] = '\0'; 155825049Seric } 155958674Seric (void) strcpy(Argv[0], buf); 156054997Seric p = &Argv[0][i]; 156124943Seric while (p < LastArgv) 156268508Seric *p++ = SPT_PADCHAR; 156368508Seric Argv[1] = NULL; 156468508Seric # endif /* SPT_TYPE == SPT_PSSTRINGS */ 156568508Seric # endif /* SPT_TYPE == SPT_PSTAT */ 156668508Seric # endif /* SPT_TYPE != SPT_NONE */ 156724943Seric } 156865982Seric 156968508Seric #endif /* SPT_TYPE != SPT_BUILTIN */ 157025698Seric /* 157125698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 157225698Seric ** 157325698Seric ** Parameters: 157468508Seric ** sig -- the signal that got us here (unused). 157525698Seric ** 157625698Seric ** Returns: 157725698Seric ** none. 157825698Seric ** 157925698Seric ** Side Effects: 158025698Seric ** Picks up extant zombies. 158125698Seric */ 158225698Seric 158346928Sbostic void 158468508Seric reapchild(sig) 158568508Seric int sig; 158625698Seric { 158764738Seric int olderrno = errno; 158863966Seric # ifdef HASWAITPID 158960219Seric auto int status; 159060560Seric int count; 159160560Seric int pid; 159260219Seric 159360560Seric count = 0; 159460560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 159560560Seric { 159660560Seric if (count++ > 1000) 159760560Seric { 159866748Seric #ifdef LOG 159960560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 160060560Seric pid, status); 160166748Seric #endif 160260560Seric break; 160360560Seric } 160460560Seric } 160560219Seric # else 160625698Seric # ifdef WNOHANG 160725698Seric union wait status; 160825698Seric 160963753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 161025698Seric continue; 161156795Seric # else /* WNOHANG */ 161225698Seric auto int status; 161325698Seric 161460219Seric while (wait(&status) > 0) 161525698Seric continue; 161656795Seric # endif /* WNOHANG */ 161760219Seric # endif 161864035Seric # ifdef SYS5SIGNALS 161964035Seric (void) setsignal(SIGCHLD, reapchild); 162058061Seric # endif 162164738Seric errno = olderrno; 162225698Seric } 162355418Seric /* 162455418Seric ** UNSETENV -- remove a variable from the environment 162555418Seric ** 162655418Seric ** Not needed on newer systems. 162755418Seric ** 162855418Seric ** Parameters: 162955418Seric ** name -- the string name of the environment variable to be 163055418Seric ** deleted from the current environment. 163155418Seric ** 163255418Seric ** Returns: 163355418Seric ** none. 163455418Seric ** 163555418Seric ** Globals: 163655418Seric ** environ -- a pointer to the current environment. 163755418Seric ** 163855418Seric ** Side Effects: 163955418Seric ** Modifies environ. 164055418Seric */ 164155418Seric 164263962Seric #ifndef HASUNSETENV 164355418Seric 164455418Seric void 164555418Seric unsetenv(name) 164655418Seric char *name; 164755418Seric { 164855418Seric extern char **environ; 164955418Seric register char **pp; 165055418Seric int len = strlen(name); 165155418Seric 165255418Seric for (pp = environ; *pp != NULL; pp++) 165355418Seric { 165455418Seric if (strncmp(name, *pp, len) == 0 && 165555418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 165655418Seric break; 165755418Seric } 165855418Seric 165955418Seric for (; *pp != NULL; pp++) 166055418Seric *pp = pp[1]; 166155418Seric } 166255418Seric 166363962Seric #endif 166456215Seric /* 166556215Seric ** GETDTABLESIZE -- return number of file descriptors 166656215Seric ** 166756215Seric ** Only on non-BSD systems 166856215Seric ** 166956215Seric ** Parameters: 167056215Seric ** none 167156215Seric ** 167256215Seric ** Returns: 167356215Seric ** size of file descriptor table 167456215Seric ** 167556215Seric ** Side Effects: 167656215Seric ** none 167756215Seric */ 167856215Seric 167963787Seric #ifdef SOLARIS 168063787Seric # include <sys/resource.h> 168163787Seric #endif 168256215Seric 168356215Seric int 168463787Seric getdtsize() 168556215Seric { 168663787Seric #ifdef RLIMIT_NOFILE 168763787Seric struct rlimit rl; 168863787Seric 168963787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 169063787Seric return rl.rlim_cur; 169163787Seric #endif 169263787Seric 169364031Seric # ifdef HASGETDTABLESIZE 169464031Seric return getdtablesize(); 169564031Seric # else 169664031Seric # ifdef _SC_OPEN_MAX 169758689Seric return sysconf(_SC_OPEN_MAX); 169863787Seric # else 169956215Seric return NOFILE; 170063787Seric # endif 170158689Seric # endif 170256215Seric } 170357631Seric /* 170457631Seric ** UNAME -- get the UUCP name of this system. 170557631Seric */ 170657631Seric 170757943Seric #ifndef HASUNAME 170857631Seric 170957631Seric int 171057631Seric uname(name) 171157631Seric struct utsname *name; 171257631Seric { 171357631Seric FILE *file; 171457631Seric char *n; 171557631Seric 171657631Seric name->nodename[0] = '\0'; 171757631Seric 171857661Seric /* try /etc/whoami -- one line with the node name */ 171957631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 172057631Seric { 172157661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 172257631Seric (void) fclose(file); 172357661Seric n = strchr(name->nodename, '\n'); 172457631Seric if (n != NULL) 172557631Seric *n = '\0'; 172657631Seric if (name->nodename[0] != '\0') 172757631Seric return (0); 172857631Seric } 172957631Seric 173057661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 173157631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 173257631Seric { 173357631Seric char buf[MAXLINE]; 173457631Seric 173557631Seric while (fgets(buf, MAXLINE, file) != NULL) 173657631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 173757631Seric NODE_LENGTH, name->nodename) > 0) 173857631Seric break; 173957631Seric (void) fclose(file); 174057631Seric if (name->nodename[0] != '\0') 174157631Seric return (0); 174257631Seric } 174357631Seric 174457631Seric #ifdef TRUST_POPEN 174557631Seric /* 174657631Seric ** Popen is known to have security holes. 174757631Seric */ 174857631Seric 174957661Seric /* try uuname -l to return local name */ 175057631Seric if ((file = popen("uuname -l", "r")) != NULL) 175157631Seric { 175257661Seric (void) fgets(name, NODE_LENGTH + 1, file); 175357631Seric (void) pclose(file); 175457661Seric n = strchr(name, '\n'); 175557631Seric if (n != NULL) 175657631Seric *n = '\0'; 175757661Seric if (name->nodename[0] != '\0') 175857631Seric return (0); 175957631Seric } 176057631Seric #endif 176157631Seric 176257631Seric return (-1); 176357631Seric } 176457943Seric #endif /* HASUNAME */ 176558068Seric /* 176658068Seric ** INITGROUPS -- initialize groups 176758068Seric ** 176858068Seric ** Stub implementation for System V style systems 176958068Seric */ 177058068Seric 177158068Seric #ifndef HASINITGROUPS 177258068Seric 177358068Seric initgroups(name, basegid) 177458068Seric char *name; 177558068Seric int basegid; 177658068Seric { 177758068Seric return 0; 177858068Seric } 177958068Seric 178058068Seric #endif 178158082Seric /* 178259289Seric ** SETSID -- set session id (for non-POSIX systems) 178359289Seric */ 178459289Seric 178559289Seric #ifndef HASSETSID 178659289Seric 178763753Seric pid_t 178863753Seric setsid __P ((void)) 178959289Seric { 179064155Seric #ifdef TIOCNOTTY 179164155Seric int fd; 179264155Seric 179366743Seric fd = open("/dev/tty", O_RDWR, 0); 179464155Seric if (fd >= 0) 179564155Seric { 179664155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 179764155Seric (void) close(fd); 179864155Seric } 179964155Seric #endif /* TIOCNOTTY */ 180064705Seric # ifdef SYS5SETPGRP 180163753Seric return setpgrp(); 180263753Seric # else 180364155Seric return setpgid(0, getpid()); 180459289Seric # endif 180559289Seric } 180659289Seric 180759289Seric #endif 180859289Seric /* 180966748Seric ** FSYNC -- dummy fsync 181066748Seric */ 181166748Seric 181266748Seric #ifdef NEEDFSYNC 181366748Seric 181466748Seric fsync(fd) 181566748Seric int fd; 181666748Seric { 181766748Seric # ifdef O_SYNC 181866748Seric return fcntl(fd, F_SETFL, O_SYNC); 181966748Seric # else 182066748Seric /* nothing we can do */ 182166748Seric return 0; 182266748Seric # endif 182366748Seric } 182466748Seric 182566748Seric #endif 182666748Seric /* 182764718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 182864718Seric ** 182964718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 183068508Seric ** instead of a long. This patches things. Only needed on versions 183168508Seric ** prior to 5.4.3. 183264718Seric */ 183364718Seric 183468508Seric #ifdef DGUX_5_4_2 183564718Seric 183664718Seric #undef inet_addr 183764718Seric 183864718Seric long 183964718Seric dgux_inet_addr(host) 184064718Seric char *host; 184164718Seric { 184264718Seric struct in_addr haddr; 184364718Seric 184464718Seric haddr = inet_addr(host); 184564718Seric return haddr.s_addr; 184664718Seric } 184764718Seric 184864718Seric #endif 184964718Seric /* 185063969Seric ** GETOPT -- for old systems or systems with bogus implementations 185163969Seric */ 185263969Seric 185363969Seric #ifdef NEEDGETOPT 185463969Seric 185563969Seric /* 185663969Seric * Copyright (c) 1985 Regents of the University of California. 185763969Seric * All rights reserved. The Berkeley software License Agreement 185863969Seric * specifies the terms and conditions for redistribution. 185963969Seric */ 186063969Seric 186163969Seric 186263969Seric /* 186363969Seric ** this version hacked to add `atend' flag to allow state machine 186463969Seric ** to reset if invoked by the program to scan args for a 2nd time 186563969Seric */ 186663969Seric 186763969Seric #if defined(LIBC_SCCS) && !defined(lint) 186863969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 186964072Seric #endif /* LIBC_SCCS and not lint */ 187063969Seric 187163969Seric #include <stdio.h> 187263969Seric 187363969Seric /* 187463969Seric * get option letter from argument vector 187563969Seric */ 187665352Seric #ifdef _CONVEX_SOURCE 187768508Seric extern int optind, opterr, optopt; 187868508Seric extern char *optarg; 187965352Seric #else 188065352Seric int opterr = 1; /* if error message should be printed */ 188165352Seric int optind = 1; /* index into parent argv vector */ 188268470Seric int optopt = 0; /* character checked for validity */ 188368470Seric char *optarg = NULL; /* argument associated with option */ 188468508Seric #endif 188563969Seric 188663969Seric #define BADCH (int)'?' 188763969Seric #define EMSG "" 188863969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 188963969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 189063969Seric 189163969Seric getopt(nargc,nargv,ostr) 189265160Seric int nargc; 189365160Seric char *const *nargv; 189465160Seric const char *ostr; 189563969Seric { 189663969Seric static char *place = EMSG; /* option letter processing */ 189763969Seric static char atend = 0; 189863969Seric register char *oli; /* option letter list index */ 189963969Seric 190063969Seric if (atend) { 190163969Seric atend = 0; 190263969Seric place = EMSG; 190363969Seric } 190463969Seric if(!*place) { /* update scanning pointer */ 190564136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 190663969Seric atend++; 190763969Seric return(EOF); 190863969Seric } 190963969Seric if (*place == '-') { /* found "--" */ 191063969Seric ++optind; 191163969Seric atend++; 191263969Seric return(EOF); 191363969Seric } 191463969Seric } /* option letter okay? */ 191564125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 191664136Seric if (!*place) ++optind; 191763969Seric tell(": illegal option -- "); 191863969Seric } 191963969Seric if (*++oli != ':') { /* don't need argument */ 192063969Seric optarg = NULL; 192163969Seric if (!*place) ++optind; 192263969Seric } 192363969Seric else { /* need an argument */ 192463969Seric if (*place) optarg = place; /* no white space */ 192563969Seric else if (nargc <= ++optind) { /* no arg */ 192663969Seric place = EMSG; 192763969Seric tell(": option requires an argument -- "); 192863969Seric } 192963969Seric else optarg = nargv[optind]; /* white space */ 193063969Seric place = EMSG; 193163969Seric ++optind; 193263969Seric } 193363969Seric return(optopt); /* dump back option letter */ 193463969Seric } 193563969Seric 193663969Seric #endif 193763969Seric /* 193863969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 193963969Seric */ 194063969Seric 194163969Seric #ifdef NEEDVPRINTF 194263969Seric 194363969Seric #define MAXARG 16 194463969Seric 194563969Seric vfprintf(fp, fmt, ap) 194663969Seric FILE * fp; 194763969Seric char * fmt; 194863969Seric char ** ap; 194963969Seric { 195063969Seric char * bp[MAXARG]; 195163969Seric int i = 0; 195263969Seric 195363969Seric while (*ap && i < MAXARG) 195463969Seric bp[i++] = *ap++; 195563969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 195663969Seric bp[4], bp[5], bp[6], bp[7], 195763969Seric bp[8], bp[9], bp[10], bp[11], 195863969Seric bp[12], bp[13], bp[14], bp[15]); 195963969Seric } 196063969Seric 196163969Seric vsprintf(s, fmt, ap) 196263969Seric char * s; 196363969Seric char * fmt; 196463969Seric char ** ap; 196563969Seric { 196663969Seric char * bp[MAXARG]; 196763969Seric int i = 0; 196863969Seric 196963969Seric while (*ap && i < MAXARG) 197063969Seric bp[i++] = *ap++; 197163969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 197263969Seric bp[4], bp[5], bp[6], bp[7], 197363969Seric bp[8], bp[9], bp[10], bp[11], 197463969Seric bp[12], bp[13], bp[14], bp[15]); 197563969Seric } 197663969Seric 197763969Seric #endif 197863969Seric /* 197965220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 198065208Seric ** 198165211Seric ** Parameters: 198265211Seric ** shell -- the user's shell from /etc/passwd 198365211Seric ** 198465211Seric ** Returns: 198565211Seric ** TRUE -- if it is ok to use this for unrestricted access. 198665211Seric ** FALSE -- if the shell is restricted. 198765208Seric */ 198865208Seric 198965587Seric #if !HASGETUSERSHELL 199065587Seric 199165587Seric # ifndef _PATH_SHELLS 199265587Seric # define _PATH_SHELLS "/etc/shells" 199365587Seric # endif 199465587Seric 199565587Seric char *DefaultUserShells[] = 199665587Seric { 199768508Seric "/bin/sh", /* standard shell */ 199865587Seric "/usr/bin/sh", 199968508Seric "/bin/csh", /* C shell */ 200065587Seric "/usr/bin/csh", 200165587Seric #ifdef __hpux 200268508Seric "/bin/rsh", /* restricted Bourne shell */ 200368508Seric "/bin/ksh", /* Korn shell */ 200468508Seric "/bin/rksh", /* restricted Korn shell */ 200565587Seric "/bin/pam", 200668508Seric "/usr/bin/keysh", /* key shell (extended Korn shell) */ 200765587Seric "/bin/posix/sh", 200865211Seric #endif 200968508Seric #ifdef _AIX3 201068508Seric "/bin/ksh", /* Korn shell */ 201168508Seric "/usr/bin/ksh", 201268508Seric "/bin/tsh", /* trusted shell */ 201368508Seric "/usr/bin/tsh", 201468508Seric "/bin/bsh", /* Bourne shell */ 201568508Seric "/usr/bin/bsh", 201668508Seric #endif 201765587Seric NULL 201865587Seric }; 201965587Seric 202065208Seric #endif 202165208Seric 202265747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 202365747Seric 202465211Seric bool 202565211Seric usershellok(shell) 202665211Seric char *shell; 202765208Seric { 202865211Seric #if HASGETUSERSHELL 202965211Seric register char *p; 203065211Seric extern char *getusershell(); 203165208Seric 203268457Seric if (shell == NULL || shell[0] == '\0') 203368457Seric return TRUE; 203468457Seric 203565211Seric setusershell(); 203665211Seric while ((p = getusershell()) != NULL) 203765747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 203865211Seric break; 203965211Seric endusershell(); 204065211Seric return p != NULL; 204165211Seric #else 204265211Seric register FILE *shellf; 204365211Seric char buf[MAXLINE]; 204465208Seric 204568457Seric if (shell == NULL || shell[0] == '\0') 204668457Seric return TRUE; 204768457Seric 204865211Seric shellf = fopen(_PATH_SHELLS, "r"); 204965211Seric if (shellf == NULL) 205065211Seric { 205165211Seric /* no /etc/shells; see if it is one of the std shells */ 205265587Seric char **d; 205365587Seric 205465587Seric for (d = DefaultUserShells; *d != NULL; d++) 205565587Seric { 205665587Seric if (strcmp(shell, *d) == 0) 205765587Seric return TRUE; 205865587Seric } 205965587Seric return FALSE; 206065211Seric } 206165208Seric 206265211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 206365211Seric { 206465211Seric register char *p, *q; 206565208Seric 206665211Seric p = buf; 206765211Seric while (*p != '\0' && *p != '#' && *p != '/') 206865211Seric p++; 206965211Seric if (*p == '#' || *p == '\0') 207065208Seric continue; 207165211Seric q = p; 207265211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 207365211Seric p++; 207465211Seric *p = '\0'; 207565747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 207665211Seric { 207765211Seric fclose(shellf); 207865211Seric return TRUE; 207965211Seric } 208065208Seric } 208165211Seric fclose(shellf); 208265211Seric return FALSE; 208365211Seric #endif 208465208Seric } 208565208Seric /* 208664311Seric ** FREESPACE -- see how much free space is on the queue filesystem 208758082Seric ** 208858082Seric ** Only implemented if you have statfs. 208958082Seric ** 209058082Seric ** Parameters: 209164311Seric ** dir -- the directory in question. 209264311Seric ** bsize -- a variable into which the filesystem 209364311Seric ** block size is stored. 209458082Seric ** 209558082Seric ** Returns: 209664311Seric ** The number of bytes free on the queue filesystem. 209764311Seric ** -1 if the statfs call fails. 209864311Seric ** 209964311Seric ** Side effects: 210064311Seric ** Puts the filesystem block size into bsize. 210158082Seric */ 210258082Seric 210365749Seric /* statfs types */ 210465749Seric #define SFS_NONE 0 /* no statfs implementation */ 210565749Seric #define SFS_USTAT 1 /* use ustat */ 210665749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 210765749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 210865749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 210965749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 211066752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 211165749Seric 211265749Seric #ifndef SFS_TYPE 211365749Seric # define SFS_TYPE SFS_NONE 211458157Seric #endif 211558157Seric 211665749Seric #if SFS_TYPE == SFS_USTAT 211758157Seric # include <ustat.h> 211858157Seric #endif 211965749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 212065749Seric # include <sys/statfs.h> 212158082Seric #endif 212265749Seric #if SFS_TYPE == SFS_VFS 212365749Seric # include <sys/vfs.h> 212465749Seric #endif 212565749Seric #if SFS_TYPE == SFS_MOUNT 212665749Seric # include <sys/mount.h> 212765749Seric #endif 212866752Seric #if SFS_TYPE == SFS_STATVFS 212966752Seric # include <sys/statvfs.h> 213066752Seric #endif 213158082Seric 213264311Seric long 213364311Seric freespace(dir, bsize) 213464311Seric char *dir; 213564311Seric long *bsize; 213658082Seric { 213765749Seric #if SFS_TYPE != SFS_NONE 213865749Seric # if SFS_TYPE == SFS_USTAT 213958153Seric struct ustat fs; 214058153Seric struct stat statbuf; 214158366Seric # define FSBLOCKSIZE DEV_BSIZE 214268508Seric # define FSF_BAVAIL f_tfree 214358157Seric # else 214458157Seric # if defined(ultrix) 214558157Seric struct fs_data fs; 214668508Seric # define FSF_BAVAIL fd_bfreen 214768508Seric # define FSBLOCKSIZE 1024L 214858153Seric # else 214966752Seric # if SFS_TYPE == SFS_STATVFS 215066752Seric struct statvfs fs; 215168508Seric # define FSBLOCKSIZE fs.f_frsize 215266752Seric # else 215358082Seric struct statfs fs; 215466752Seric # define FSBLOCKSIZE fs.f_bsize 215563987Seric # endif 215658153Seric # endif 215758133Seric # endif 215868508Seric # ifndef FSF_BAVAIL 215968508Seric # define FSF_BAVAIL f_bavail 216068508Seric # endif 216158082Seric 216265749Seric # if SFS_TYPE == SFS_USTAT 216364311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 216458157Seric # else 216565749Seric # if SFS_TYPE == SFS_4ARGS 216664311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 216758157Seric # else 216868508Seric # if SFS_TYPE == SFS_STATVFS 216968508Seric if (statvfs(dir, &fs) == 0) 217068508Seric # else 217168508Seric # if defined(ultrix) 217268499Seric if (statfs(dir, &fs) > 0) 217368508Seric # else 217464311Seric if (statfs(dir, &fs) == 0) 217568508Seric # endif 217658153Seric # endif 217758133Seric # endif 217858133Seric # endif 217958133Seric { 218064311Seric if (bsize != NULL) 218164311Seric *bsize = FSBLOCKSIZE; 218268508Seric return (fs.FSF_BAVAIL); 218364311Seric } 218464337Seric #endif 218564311Seric return (-1); 218664311Seric } 218764311Seric /* 218864311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 218964311Seric ** 219064311Seric ** Only implemented if you have statfs. 219164311Seric ** 219264311Seric ** Parameters: 219364311Seric ** msize -- the size to check against. If zero, we don't yet 219464311Seric ** know how big the message will be, so just check for 219564311Seric ** a "reasonable" amount. 219664311Seric ** 219764311Seric ** Returns: 219864311Seric ** TRUE if there is enough space. 219964311Seric ** FALSE otherwise. 220064311Seric */ 220164311Seric 220264311Seric bool 220364311Seric enoughspace(msize) 220464311Seric long msize; 220564311Seric { 220664311Seric long bfree, bsize; 220764311Seric 220864311Seric if (MinBlocksFree <= 0 && msize <= 0) 220964311Seric { 221058133Seric if (tTd(4, 80)) 221164311Seric printf("enoughspace: no threshold\n"); 221264311Seric return TRUE; 221364311Seric } 221464311Seric 221564311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 221664311Seric { 221764311Seric if (tTd(4, 80)) 221858333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 221964311Seric bfree, msize); 222058333Seric 222158333Seric /* convert msize to block count */ 222264311Seric msize = msize / bsize + 1; 222358333Seric if (MinBlocksFree >= 0) 222458333Seric msize += MinBlocksFree; 222558333Seric 222664311Seric if (bfree < msize) 222758090Seric { 222858090Seric #ifdef LOG 222958090Seric if (LogLevel > 0) 223064254Seric syslog(LOG_ALERT, 223164254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 223268508Seric CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id, 223368508Seric bfree, 223468508Seric CurHostName == NULL ? "SMTP-DAEMON" : CurHostName, 223568508Seric msize, QueueDir); 223658090Seric #endif 223758082Seric return FALSE; 223858090Seric } 223958082Seric } 224058082Seric else if (tTd(4, 80)) 224158333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 224258333Seric MinBlocksFree, msize, errstring(errno)); 224358082Seric return TRUE; 224458082Seric } 224558542Seric /* 224658542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 224758542Seric ** 224858542Seric ** This looks at an errno value and tells if this is likely to 224958542Seric ** go away if retried later. 225058542Seric ** 225158542Seric ** Parameters: 225258542Seric ** err -- the errno code to classify. 225358542Seric ** 225458542Seric ** Returns: 225558542Seric ** TRUE if this is probably transient. 225658542Seric ** FALSE otherwise. 225758542Seric */ 225858542Seric 225958542Seric bool 226058542Seric transienterror(err) 226158542Seric int err; 226258542Seric { 226358542Seric switch (err) 226458542Seric { 226558542Seric case EIO: /* I/O error */ 226658542Seric case ENXIO: /* Device not configured */ 226758542Seric case EAGAIN: /* Resource temporarily unavailable */ 226858542Seric case ENOMEM: /* Cannot allocate memory */ 226958542Seric case ENODEV: /* Operation not supported by device */ 227058542Seric case ENFILE: /* Too many open files in system */ 227158542Seric case EMFILE: /* Too many open files */ 227258542Seric case ENOSPC: /* No space left on device */ 227358542Seric #ifdef ETIMEDOUT 227458542Seric case ETIMEDOUT: /* Connection timed out */ 227558542Seric #endif 227658542Seric #ifdef ESTALE 227758542Seric case ESTALE: /* Stale NFS file handle */ 227858542Seric #endif 227958542Seric #ifdef ENETDOWN 228058542Seric case ENETDOWN: /* Network is down */ 228158542Seric #endif 228258542Seric #ifdef ENETUNREACH 228358542Seric case ENETUNREACH: /* Network is unreachable */ 228458542Seric #endif 228558542Seric #ifdef ENETRESET 228658542Seric case ENETRESET: /* Network dropped connection on reset */ 228758542Seric #endif 228858542Seric #ifdef ECONNABORTED 228958542Seric case ECONNABORTED: /* Software caused connection abort */ 229058542Seric #endif 229158542Seric #ifdef ECONNRESET 229258542Seric case ECONNRESET: /* Connection reset by peer */ 229358542Seric #endif 229458542Seric #ifdef ENOBUFS 229558542Seric case ENOBUFS: /* No buffer space available */ 229658542Seric #endif 229758542Seric #ifdef ESHUTDOWN 229858542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 229958542Seric #endif 230058542Seric #ifdef ECONNREFUSED 230158542Seric case ECONNREFUSED: /* Connection refused */ 230258542Seric #endif 230358542Seric #ifdef EHOSTDOWN 230458542Seric case EHOSTDOWN: /* Host is down */ 230558542Seric #endif 230658542Seric #ifdef EHOSTUNREACH 230758542Seric case EHOSTUNREACH: /* No route to host */ 230858542Seric #endif 230958542Seric #ifdef EDQUOT 231058542Seric case EDQUOT: /* Disc quota exceeded */ 231158542Seric #endif 231258542Seric #ifdef EPROCLIM 231358542Seric case EPROCLIM: /* Too many processes */ 231458542Seric #endif 231558542Seric #ifdef EUSERS 231658542Seric case EUSERS: /* Too many users */ 231758542Seric #endif 231858542Seric #ifdef EDEADLK 231958542Seric case EDEADLK: /* Resource deadlock avoided */ 232058542Seric #endif 232158542Seric #ifdef EISCONN 232258542Seric case EISCONN: /* Socket already connected */ 232358542Seric #endif 232458542Seric #ifdef EINPROGRESS 232558542Seric case EINPROGRESS: /* Operation now in progress */ 232658542Seric #endif 232758542Seric #ifdef EALREADY 232858542Seric case EALREADY: /* Operation already in progress */ 232958542Seric #endif 233058542Seric #ifdef EADDRINUSE 233158542Seric case EADDRINUSE: /* Address already in use */ 233258542Seric #endif 233358542Seric #ifdef EADDRNOTAVAIL 233458542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 233558542Seric #endif 233666018Seric #ifdef ETXTBSY 233766017Seric case ETXTBSY: /* (Apollo) file locked */ 233866017Seric #endif 233963834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 234058542Seric case ENOSR: /* Out of streams resources */ 234158542Seric #endif 234258542Seric return TRUE; 234358542Seric } 234458542Seric 234558542Seric /* nope, must be permanent */ 234658542Seric return FALSE; 234758542Seric } 234858689Seric /* 234964035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 235058689Seric ** 235158689Seric ** Parameters: 235258689Seric ** fd -- the file descriptor of the file. 235358689Seric ** filename -- the file name (for error messages). 235464335Seric ** ext -- the filename extension. 235558689Seric ** type -- type of the lock. Bits can be: 235658689Seric ** LOCK_EX -- exclusive lock. 235758689Seric ** LOCK_NB -- non-blocking. 235858689Seric ** 235958689Seric ** Returns: 236058689Seric ** TRUE if the lock was acquired. 236158689Seric ** FALSE otherwise. 236258689Seric */ 236358689Seric 236458689Seric bool 236564335Seric lockfile(fd, filename, ext, type) 236658689Seric int fd; 236758689Seric char *filename; 236864335Seric char *ext; 236958689Seric int type; 237058689Seric { 237165830Seric # if !HASFLOCK 237258689Seric int action; 237358689Seric struct flock lfd; 237464335Seric 237564335Seric if (ext == NULL) 237664335Seric ext = ""; 237764035Seric 237863983Seric bzero(&lfd, sizeof lfd); 237959447Seric if (bitset(LOCK_UN, type)) 238059447Seric lfd.l_type = F_UNLCK; 238159447Seric else if (bitset(LOCK_EX, type)) 238258689Seric lfd.l_type = F_WRLCK; 238358689Seric else 238458689Seric lfd.l_type = F_RDLCK; 238558689Seric 238658689Seric if (bitset(LOCK_NB, type)) 238758689Seric action = F_SETLK; 238858689Seric else 238958689Seric action = F_SETLKW; 239058689Seric 239164264Seric if (tTd(55, 60)) 239264335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 239364335Seric filename, ext, action, lfd.l_type); 239464264Seric 239558689Seric if (fcntl(fd, action, &lfd) >= 0) 239664264Seric { 239764264Seric if (tTd(55, 60)) 239864264Seric printf("SUCCESS\n"); 239958689Seric return TRUE; 240064264Seric } 240158689Seric 240264264Seric if (tTd(55, 60)) 240364264Seric printf("(%s) ", errstring(errno)); 240464264Seric 240564136Seric /* 240664136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 240764136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 240864136Seric ** as type "tmp" (that is, served from swap space), the 240964136Seric ** previous fcntl will fail with "Invalid argument" errors. 241064136Seric ** Since this is fairly common during testing, we will assume 241164136Seric ** that this indicates that the lock is successfully grabbed. 241264136Seric */ 241364136Seric 241464136Seric if (errno == EINVAL) 241564264Seric { 241664264Seric if (tTd(55, 60)) 241764264Seric printf("SUCCESS\n"); 241864136Seric return TRUE; 241964264Seric } 242064136Seric 242158689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 242264378Seric { 242364378Seric int omode = -1; 242464378Seric # ifdef F_GETFL 242564378Seric int oerrno = errno; 242664378Seric 242764378Seric (void) fcntl(fd, F_GETFL, &omode); 242864378Seric errno = oerrno; 242964378Seric # endif 243064378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 243164378Seric filename, ext, fd, type, omode, geteuid()); 243264378Seric } 243358689Seric # else 243464335Seric if (ext == NULL) 243564335Seric ext = ""; 243664335Seric 243764264Seric if (tTd(55, 60)) 243864335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 243964264Seric 244058689Seric if (flock(fd, type) >= 0) 244164264Seric { 244264264Seric if (tTd(55, 60)) 244364264Seric printf("SUCCESS\n"); 244458689Seric return TRUE; 244564264Seric } 244658689Seric 244764264Seric if (tTd(55, 60)) 244864264Seric printf("(%s) ", errstring(errno)); 244964264Seric 245058689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 245164378Seric { 245264378Seric int omode = -1; 245364378Seric # ifdef F_GETFL 245464378Seric int oerrno = errno; 245564378Seric 245664378Seric (void) fcntl(fd, F_GETFL, &omode); 245764378Seric errno = oerrno; 245864378Seric # endif 245964378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 246064378Seric filename, ext, fd, type, omode, geteuid()); 246164386Seric } 246258689Seric # endif 246364264Seric if (tTd(55, 60)) 246464264Seric printf("FAIL\n"); 246558689Seric return FALSE; 246658689Seric } 246764556Seric /* 246865948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 246965948Seric ** 247065948Seric ** Parameters: 247165948Seric ** fd -- the file descriptor to check. 247265948Seric ** 247365948Seric ** Returns: 247465948Seric ** TRUE -- if only root can chown the file to an arbitrary 247565948Seric ** user. 247665948Seric ** FALSE -- if an arbitrary user can give away a file. 247765948Seric */ 247865948Seric 247965948Seric bool 248065948Seric chownsafe(fd) 248165948Seric int fd; 248265948Seric { 248365950Seric #ifdef __hpux 248465948Seric char *s; 248565948Seric int tfd; 248665948Seric uid_t o_uid, o_euid; 248765948Seric gid_t o_gid, o_egid; 248865948Seric bool rval; 248965948Seric struct stat stbuf; 249065948Seric 249165948Seric o_uid = getuid(); 249265948Seric o_euid = geteuid(); 249365948Seric o_gid = getgid(); 249465948Seric o_egid = getegid(); 249565959Seric fstat(fd, &stbuf); 249665948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 249765948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 249865948Seric s = tmpnam(NULL); 249965948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 250065974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 250165948Seric close(tfd); 250265948Seric unlink(s); 250365948Seric setreuid(o_uid, o_euid); 250465948Seric setresgid(o_gid, o_egid, -1); 250565948Seric return rval; 250665948Seric #else 250766088Seric # ifdef _POSIX_CHOWN_RESTRICTED 250866088Seric # if _POSIX_CHOWN_RESTRICTED == -1 250966088Seric return FALSE; 251066088Seric # else 251166088Seric return TRUE; 251266088Seric # endif 251366088Seric # else 251466088Seric # ifdef _PC_CHOWN_RESTRICTED 251568508Seric int rval; 251668508Seric 251768508Seric /* 251868508Seric ** Some systems (e.g., SunOS) seem to have the call and the 251968508Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 252068508Seric ** the call. This heuristic checks for that. 252168508Seric */ 252268508Seric 252368508Seric errno = 0; 252468508Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 252568508Seric if (errno == 0) 252668508Seric return rval > 0; 252768508Seric # endif 252868508Seric # ifdef BSD 252968508Seric return TRUE; 253068499Seric # else 253165948Seric return FALSE; 253265948Seric # endif 253365948Seric # endif 253465948Seric #endif 253565948Seric } 253665948Seric /* 253768508Seric ** RESETLIMITS -- reset system controlled resource limits 253868508Seric ** 253968508Seric ** This is to avoid denial-of-service attacks 254068508Seric ** 254168508Seric ** Parameters: 254268508Seric ** none 254368508Seric ** 254468508Seric ** Returns: 254568508Seric ** none 254668508Seric */ 254768508Seric 254868508Seric #if HASSETRLIMIT 254968508Seric # include <sys/resource.h> 255068508Seric #endif 255168508Seric 255268508Seric void 255368508Seric resetlimits() 255468508Seric { 255568508Seric #if HASSETRLIMIT 255668508Seric struct rlimit lim; 255768508Seric 255868508Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 255968508Seric (void) setrlimit(RLIMIT_CPU, &lim); 256068508Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 256168508Seric #else 256268508Seric # if HASULIMIT 256368508Seric (void) ulimit(2, 0x3fffff); 256468508Seric # endif 256568508Seric #endif 256668508Seric } 256768508Seric /* 256864556Seric ** GETCFNAME -- return the name of the .cf file. 256964556Seric ** 257064556Seric ** Some systems (e.g., NeXT) determine this dynamically. 257164556Seric */ 257264556Seric 257364556Seric char * 257464556Seric getcfname() 257564556Seric { 257664556Seric if (ConfFile != NULL) 257764556Seric return ConfFile; 257865214Seric #ifdef NETINFO 257965214Seric { 258065214Seric extern char *ni_propval(); 258165214Seric char *cflocation; 258265214Seric 258368508Seric cflocation = ni_propval("/locations", NULL, "sendmail", 258468508Seric "sendmail.cf", '\0'); 258565214Seric if (cflocation != NULL) 258665214Seric return cflocation; 258765214Seric } 258865214Seric #endif 258964556Seric return _PATH_SENDMAILCF; 259064556Seric } 259164718Seric /* 259264718Seric ** SETVENDOR -- process vendor code from V configuration line 259364718Seric ** 259464718Seric ** Parameters: 259564718Seric ** vendor -- string representation of vendor. 259664718Seric ** 259764718Seric ** Returns: 259864718Seric ** TRUE -- if ok. 259964718Seric ** FALSE -- if vendor code could not be processed. 260064926Seric ** 260164926Seric ** Side Effects: 260264926Seric ** It is reasonable to set mode flags here to tweak 260364926Seric ** processing in other parts of the code if necessary. 260464926Seric ** For example, if you are a vendor that uses $%y to 260564926Seric ** indicate YP lookups, you could enable that here. 260664718Seric */ 260764718Seric 260864718Seric bool 260964718Seric setvendor(vendor) 261064718Seric char *vendor; 261164718Seric { 261264926Seric if (strcasecmp(vendor, "Berkeley") == 0) 261368508Seric { 261468508Seric VendorCode = VENDOR_BERKELEY; 261564926Seric return TRUE; 261668508Seric } 261764926Seric 261864926Seric /* add vendor extensions here */ 261964926Seric 262068508Seric #ifdef SUN_EXTENSIONS 262168508Seric if (strcasecmp(vendor, "Sun") == 0) 262268508Seric { 262368508Seric VendorCode = VENDOR_SUN; 262468508Seric return TRUE; 262568508Seric } 262668508Seric #endif 262768508Seric 262864926Seric return FALSE; 262964718Seric } 263064816Seric /* 263164816Seric ** STRTOL -- convert string to long integer 263264816Seric ** 263364816Seric ** For systems that don't have it in the C library. 263466161Seric ** 263566161Seric ** This is taken verbatim from the 4.4-Lite C library. 263664816Seric */ 263764816Seric 263864816Seric #ifdef NEEDSTRTOL 263964816Seric 264066161Seric #if defined(LIBC_SCCS) && !defined(lint) 264166161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 264266161Seric #endif /* LIBC_SCCS and not lint */ 264366161Seric 264466161Seric #include <limits.h> 264566161Seric 264666161Seric /* 264766161Seric * Convert a string to a long integer. 264866161Seric * 264966161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 265066161Seric * alphabets and digits are each contiguous. 265166161Seric */ 265266161Seric 265364816Seric long 265466161Seric strtol(nptr, endptr, base) 265566161Seric const char *nptr; 265666161Seric char **endptr; 265766161Seric register int base; 265864816Seric { 265966161Seric register const char *s = nptr; 266066161Seric register unsigned long acc; 266166161Seric register int c; 266266161Seric register unsigned long cutoff; 266366161Seric register int neg = 0, any, cutlim; 266464816Seric 266566161Seric /* 266666161Seric * Skip white space and pick up leading +/- sign if any. 266766161Seric * If base is 0, allow 0x for hex and 0 for octal, else 266866161Seric * assume decimal; if base is already 16, allow 0x. 266966161Seric */ 267066161Seric do { 267166161Seric c = *s++; 267266161Seric } while (isspace(c)); 267366161Seric if (c == '-') { 267466161Seric neg = 1; 267566161Seric c = *s++; 267666161Seric } else if (c == '+') 267766161Seric c = *s++; 267866161Seric if ((base == 0 || base == 16) && 267966161Seric c == '0' && (*s == 'x' || *s == 'X')) { 268066161Seric c = s[1]; 268166161Seric s += 2; 268266161Seric base = 16; 268366161Seric } 268466161Seric if (base == 0) 268566161Seric base = c == '0' ? 8 : 10; 268664816Seric 268766161Seric /* 268866161Seric * Compute the cutoff value between legal numbers and illegal 268966161Seric * numbers. That is the largest legal value, divided by the 269066161Seric * base. An input number that is greater than this value, if 269166161Seric * followed by a legal input character, is too big. One that 269266161Seric * is equal to this value may be valid or not; the limit 269366161Seric * between valid and invalid numbers is then based on the last 269466161Seric * digit. For instance, if the range for longs is 269566161Seric * [-2147483648..2147483647] and the input base is 10, 269666161Seric * cutoff will be set to 214748364 and cutlim to either 269766161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 269866161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 269966161Seric * the number is too big, and we will return a range error. 270066161Seric * 270166161Seric * Set any if any `digits' consumed; make it negative to indicate 270266161Seric * overflow. 270366161Seric */ 270466161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 270566161Seric cutlim = cutoff % (unsigned long)base; 270666161Seric cutoff /= (unsigned long)base; 270766161Seric for (acc = 0, any = 0;; c = *s++) { 270866161Seric if (isdigit(c)) 270966161Seric c -= '0'; 271066161Seric else if (isalpha(c)) 271166161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 271266161Seric else 271366161Seric break; 271466161Seric if (c >= base) 271566161Seric break; 271666161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 271766161Seric any = -1; 271866161Seric else { 271966161Seric any = 1; 272066161Seric acc *= base; 272166161Seric acc += c; 272264816Seric } 272364816Seric } 272466161Seric if (any < 0) { 272566161Seric acc = neg ? LONG_MIN : LONG_MAX; 272666161Seric errno = ERANGE; 272766161Seric } else if (neg) 272866161Seric acc = -acc; 272966161Seric if (endptr != 0) 273066161Seric *endptr = (char *)(any ? s - 1 : nptr); 273166161Seric return (acc); 273264816Seric } 273364816Seric 273464816Seric #endif 273564841Seric /* 273664841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 273764841Seric ** 273868508Seric ** Solaris versions at least through 2.3 don't properly deliver a 273964841Seric ** canonical h_name field. This tries to work around it. 274064841Seric */ 274164841Seric 274268508Seric #if defined(SOLARIS) && SOLARIS < 204 274364841Seric 274468482Seric extern int h_errno; 274568482Seric 274664841Seric struct hostent * 274764841Seric solaris_gethostbyname(name) 274865094Seric const char *name; 274964841Seric { 275068508Seric # if SOLARIS == 203 275164841Seric static struct hostent hp; 275264841Seric static char buf[1000]; 275364841Seric extern struct hostent *_switch_gethostbyname_r(); 275464841Seric 275564841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 275664841Seric # else 275765009Seric extern struct hostent *__switch_gethostbyname(); 275865009Seric 275964841Seric return __switch_gethostbyname(name); 276064841Seric # endif 276164841Seric } 276264841Seric 276364841Seric struct hostent * 276464841Seric solaris_gethostbyaddr(addr, len, type) 276565094Seric const char *addr; 276664841Seric int len; 276764841Seric int type; 276864841Seric { 276968508Seric # if SOLARIS == 203 277064841Seric static struct hostent hp; 277164841Seric static char buf[1000]; 277264841Seric extern struct hostent *_switch_gethostbyaddr_r(); 277364841Seric 277464841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 277564841Seric # else 277665009Seric extern struct hostent *__switch_gethostbyaddr(); 277765009Seric 277864841Seric return __switch_gethostbyaddr(addr, len, type); 277964841Seric # endif 278064841Seric } 278164841Seric 278264841Seric #endif 278365214Seric /* 278465214Seric ** NI_PROPVAL -- netinfo property value lookup routine 278565214Seric ** 278665214Seric ** Parameters: 278768508Seric ** keydir -- the Netinfo directory name in which to search 278868508Seric ** for the key. 278968508Seric ** keyprop -- the name of the property in which to find the 279068508Seric ** property we are interested. Defaults to "name". 279168508Seric ** keyval -- the value for which we are really searching. 279268508Seric ** valprop -- the property name for the value in which we 279368508Seric ** are interested. 279468508Seric ** sepchar -- if non-nil, this can be multiple-valued, and 279568508Seric ** we should return a string separated by this 279668508Seric ** character. 279765214Seric ** 279865214Seric ** Returns: 279965214Seric ** NULL -- if: 280065214Seric ** 1. the directory is not found 280165214Seric ** 2. the property name is not found 280265214Seric ** 3. the property contains multiple values 280365214Seric ** 4. some error occured 280465214Seric ** else -- the location of the config file. 280565214Seric ** 280668508Seric ** Example: 280768508Seric ** To search for an alias value, use: 280868508Seric ** ni_propval("/aliases", "name", aliasname, "members", ',') 280968508Seric ** 281065214Seric ** Notes: 281165214Seric ** Caller should free the return value of ni_proval 281265214Seric */ 281365214Seric 281465214Seric #ifdef NETINFO 281565214Seric 281665214Seric # include <netinfo/ni.h> 281765214Seric 281865214Seric # define LOCAL_NETINFO_DOMAIN "." 281965214Seric # define PARENT_NETINFO_DOMAIN ".." 282065214Seric # define MAX_NI_LEVELS 256 282165214Seric 282265214Seric char * 282368508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar) 282468508Seric char *keydir; 282568508Seric char *keyprop; 282668508Seric char *keyval; 282768508Seric char *valprop; 282868508Seric char sepchar; 282965214Seric { 283065820Seric char *propval = NULL; 283165214Seric int i; 283268508Seric int j, alen; 283365214Seric void *ni = NULL; 283465214Seric void *lastni = NULL; 283565214Seric ni_status nis; 283665214Seric ni_id nid; 283765214Seric ni_namelist ninl; 283868508Seric register char *p; 283968508Seric char keybuf[1024]; 284065214Seric 284165214Seric /* 284268508Seric ** Create the full key from the two parts. 284368508Seric ** 284468508Seric ** Note that directory can end with, e.g., "name=" to specify 284568508Seric ** an alternate search property. 284668508Seric */ 284768508Seric 284868508Seric i = strlen(keydir) + strlen(keyval) + 2; 284968508Seric if (keyprop != NULL) 285068508Seric i += strlen(keyprop) + 1; 285168508Seric if (i > sizeof keybuf) 285268508Seric return NULL; 285368508Seric strcpy(keybuf, keydir); 285468508Seric strcat(keybuf, "/"); 285568508Seric if (keyprop != NULL) 285668508Seric { 285768508Seric strcat(keybuf, keyprop); 285868508Seric strcat(keybuf, "="); 285968508Seric } 286068508Seric strcat(keybuf, keyval); 286168508Seric 286268508Seric /* 286365214Seric ** If the passed directory and property name are found 286465214Seric ** in one of netinfo domains we need to search (starting 286565214Seric ** from the local domain moving all the way back to the 286665214Seric ** root domain) set propval to the property's value 286765214Seric ** and return it. 286865214Seric */ 286965214Seric 287065214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 287165214Seric { 287265214Seric if (i == 0) 287365214Seric { 287465214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 287565214Seric } 287665214Seric else 287765214Seric { 287865214Seric if (lastni != NULL) 287965214Seric ni_free(lastni); 288065214Seric lastni = ni; 288165214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 288265214Seric } 288365214Seric 288465214Seric /* 288565214Seric ** Don't bother if we didn't get a handle on a 288665214Seric ** proper domain. This is not necessarily an error. 288765214Seric ** We would get a positive ni_status if, for instance 288865214Seric ** we never found the directory or property and tried 288965214Seric ** to open the parent of the root domain! 289065214Seric */ 289165214Seric 289265214Seric if (nis != 0) 289365214Seric break; 289465214Seric 289565214Seric /* 289665214Seric ** Find the path to the server information. 289765214Seric */ 289865214Seric 289968508Seric if (ni_pathsearch(ni, &nid, keybuf) != 0) 290065214Seric continue; 290165214Seric 290265214Seric /* 290368508Seric ** Find associated value information. 290465214Seric */ 290565214Seric 290668508Seric if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0) 290765214Seric continue; 290865214Seric 290965214Seric /* 291068508Seric ** See if we have an acceptable number of values. 291165214Seric */ 291265214Seric 291368508Seric if (ninl.ni_namelist_len <= 0) 291468508Seric continue; 291568508Seric 291668508Seric if (sepchar == '\0' && ninl.ni_namelist_len > 1) 291765214Seric { 291868508Seric ni_namelist_free(&ninl); 291968508Seric continue; 292065214Seric } 292168508Seric 292268508Seric /* 292368508Seric ** Calculate number of bytes needed and build result 292468508Seric */ 292568508Seric 292668508Seric alen = 1; 292768508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 292868508Seric alen += strlen(ninl.ni_namelist_val[j]) + 1; 292968508Seric propval = p = xalloc(alen); 293068508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 293168508Seric { 293268508Seric strcpy(p, ninl.ni_namelist_val[j]); 293368508Seric p += strlen(p); 293468508Seric *p++ = sepchar; 293568508Seric } 293668508Seric *--p = '\0'; 293768508Seric 293868508Seric ni_namelist_free(&ninl); 293965214Seric } 294065214Seric 294165214Seric /* 294265214Seric ** Clean up. 294365214Seric */ 294465214Seric 294565214Seric if (ni != NULL) 294665214Seric ni_free(ni); 294765214Seric if (lastni != NULL && ni != lastni) 294865214Seric ni_free(lastni); 294965214Seric 295065214Seric return propval; 295165214Seric } 295265214Seric 295365214Seric #endif /* NETINFO */ 295465504Seric /* 295565504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 295665504Seric ** 295765504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 295865504Seric ** syslog succeeds during interrupt handlers. 295965504Seric */ 296065504Seric 296165504Seric #ifdef __hpux 296265504Seric 296365504Seric # define MAXSYSLOGTRIES 100 296465504Seric # undef syslog 296565504Seric 296665504Seric # ifdef __STDC__ 296765504Seric hard_syslog(int pri, char *msg, ...) 296865504Seric # else 296965504Seric hard_syslog(pri, msg, va_alist) 297065504Seric int pri; 297165504Seric char *msg; 297265504Seric va_dcl 297365504Seric # endif 297465504Seric { 297565504Seric int i; 297665504Seric char buf[SYSLOG_BUFSIZE * 2]; 297765504Seric VA_LOCAL_DECL; 297865504Seric 297965504Seric VA_START(msg); 298065504Seric vsprintf(buf, msg, ap); 298165504Seric VA_END; 298265504Seric 298365504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 298465504Seric continue; 298565504Seric } 298665504Seric 298765504Seric #endif 2988