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*68559Seric static char sccsid[] = "@(#)conf.c 8.141 (Berkeley) 03/21/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 { 71868508Seric maptype[0] = "files"; 71968508Seric return 1; 72068508Seric } 72168508Seric if (strcmp(service, "hosts") == 0) 72268508Seric { 72368508Seric # if NAMED_BIND 72468508Seric maptype[svcno++] = "dns"; 72568508Seric # else 72668508Seric # if defined(sun) && !defined(BSD) && !defined(SOLARIS) 72768508Seric /* SunOS */ 72868508Seric maptype[svcno++] = "nis"; 72968508Seric # endif 73068508Seric # endif 73168508Seric maptype[svcno++] = "files"; 73268508Seric return svcno; 73368508Seric } 73468508Seric return -1; 73568508Seric } 73668508Seric /* 7379369Seric ** USERNAME -- return the user id of the logged in user. 7389369Seric ** 7399369Seric ** Parameters: 7409369Seric ** none. 7419369Seric ** 7429369Seric ** Returns: 7439369Seric ** The login name of the logged in user. 7449369Seric ** 7459369Seric ** Side Effects: 7469369Seric ** none. 7479369Seric ** 7489369Seric ** Notes: 7499369Seric ** The return value is statically allocated. 7509369Seric */ 7519369Seric 7529369Seric char * 7539369Seric username() 7549369Seric { 75517469Seric static char *myname = NULL; 7569369Seric extern char *getlogin(); 75719904Smiriam register struct passwd *pw; 7589369Seric 75917469Seric /* cache the result */ 76017469Seric if (myname == NULL) 76117469Seric { 76217469Seric myname = getlogin(); 76317469Seric if (myname == NULL || myname[0] == '\0') 76417469Seric { 76563787Seric pw = getpwuid(RealUid); 76617469Seric if (pw != NULL) 76740993Sbostic myname = newstr(pw->pw_name); 76817469Seric } 76919904Smiriam else 77019904Smiriam { 77163787Seric uid_t uid = RealUid; 77219873Smiriam 77340993Sbostic myname = newstr(myname); 77440993Sbostic if ((pw = getpwnam(myname)) == NULL || 77558736Seric (uid != 0 && uid != pw->pw_uid)) 77619904Smiriam { 77758736Seric pw = getpwuid(uid); 77824945Seric if (pw != NULL) 77940993Sbostic myname = newstr(pw->pw_name); 78019873Smiriam } 78119873Smiriam } 78217469Seric if (myname == NULL || myname[0] == '\0') 78317469Seric { 78458151Seric syserr("554 Who are you?"); 78517469Seric myname = "postmaster"; 78617469Seric } 78717469Seric } 78817469Seric 78917469Seric return (myname); 7909369Seric } 7919369Seric /* 7924190Seric ** TTYPATH -- Get the path of the user's tty 793294Seric ** 794294Seric ** Returns the pathname of the user's tty. Returns NULL if 795294Seric ** the user is not logged in or if s/he has write permission 796294Seric ** denied. 797294Seric ** 798294Seric ** Parameters: 799294Seric ** none 800294Seric ** 801294Seric ** Returns: 802294Seric ** pathname of the user's tty. 803294Seric ** NULL if not logged in or write permission denied. 804294Seric ** 805294Seric ** Side Effects: 806294Seric ** none. 807294Seric ** 808294Seric ** WARNING: 809294Seric ** Return value is in a local buffer. 810294Seric ** 811294Seric ** Called By: 812294Seric ** savemail 813294Seric */ 814294Seric 815294Seric char * 816294Seric ttypath() 817294Seric { 818294Seric struct stat stbuf; 819294Seric register char *pathn; 820294Seric extern char *ttyname(); 8214081Seric extern char *getlogin(); 822294Seric 823294Seric /* compute the pathname of the controlling tty */ 8249369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 8259369Seric (pathn = ttyname(0)) == NULL) 826294Seric { 827294Seric errno = 0; 828294Seric return (NULL); 829294Seric } 830294Seric 831294Seric /* see if we have write permission */ 8322967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 833294Seric { 834294Seric errno = 0; 835294Seric return (NULL); 836294Seric } 837294Seric 838294Seric /* see if the user is logged in */ 839294Seric if (getlogin() == NULL) 840294Seric return (NULL); 841294Seric 842294Seric /* looks good */ 843294Seric return (pathn); 844294Seric } 8452967Seric /* 8462967Seric ** CHECKCOMPAT -- check for From and To person compatible. 8472967Seric ** 8482967Seric ** This routine can be supplied on a per-installation basis 8492967Seric ** to determine whether a person is allowed to send a message. 8502967Seric ** This allows restriction of certain types of internet 8512967Seric ** forwarding or registration of users. 8522967Seric ** 8532967Seric ** If the hosts are found to be incompatible, an error 85457454Seric ** message should be given using "usrerr" and 0 should 8552967Seric ** be returned. 8562967Seric ** 857*68559Seric ** EF_NO_BODY_RETN can be set in e->e_flags to suppress the 858*68559Seric ** body during the return-to-sender function; this should be done 859*68559Seric ** on huge messages. This bit may already be set by the ESMTP 860*68559Seric ** protocol. 8614288Seric ** 8622967Seric ** Parameters: 8632967Seric ** to -- the person being sent to. 8642967Seric ** 8652967Seric ** Returns: 86657459Seric ** an exit status 8672967Seric ** 8682967Seric ** Side Effects: 8692967Seric ** none (unless you include the usrerr stuff) 8702967Seric */ 8712967Seric 87268508Seric int 87355012Seric checkcompat(to, e) 8742967Seric register ADDRESS *to; 87555012Seric register ENVELOPE *e; 8762967Seric { 87712133Seric # ifdef lint 87812133Seric if (to == NULL) 87912133Seric to++; 88063834Seric # endif /* lint */ 88164402Seric 88264402Seric if (tTd(49, 1)) 88364402Seric printf("checkcompat(to=%s, from=%s)\n", 88464402Seric to->q_paddr, e->e_from.q_paddr); 88564402Seric 88610698Seric # ifdef EXAMPLE_CODE 88710698Seric /* this code is intended as an example only */ 8884437Seric register STAB *s; 8894437Seric 8904437Seric s = stab("arpa", ST_MAILER, ST_FIND); 89168508Seric if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 && 8929369Seric to->q_mailer == s->s_mailer) 8934437Seric { 89458151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 895*68559Seric /* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */ 89657459Seric return (EX_UNAVAILABLE); 8974437Seric } 89856795Seric # endif /* EXAMPLE_CODE */ 89957459Seric return (EX_OK); 9002967Seric } 9019369Seric /* 90264035Seric ** SETSIGNAL -- set a signal handler 90364035Seric ** 90464035Seric ** This is essentially old BSD "signal(3)". 90564035Seric */ 90664035Seric 90764561Seric sigfunc_t 90864035Seric setsignal(sig, handler) 90964035Seric int sig; 91064561Seric sigfunc_t handler; 91164035Seric { 91264380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 91364035Seric return signal(sig, handler); 91464035Seric #else 91564035Seric struct sigaction n, o; 91664035Seric 91764035Seric bzero(&n, sizeof n); 91864035Seric n.sa_handler = handler; 91968508Seric # ifdef SA_RESTART 92068508Seric n.sa_flags = SA_RESTART; 92168508Seric # endif 92264035Seric if (sigaction(sig, &n, &o) < 0) 92364035Seric return SIG_ERR; 92464035Seric return o.sa_handler; 92564035Seric #endif 92664035Seric } 92764035Seric /* 9289369Seric ** HOLDSIGS -- arrange to hold all signals 9299369Seric ** 9309369Seric ** Parameters: 9319369Seric ** none. 9329369Seric ** 9339369Seric ** Returns: 9349369Seric ** none. 9359369Seric ** 9369369Seric ** Side Effects: 9379369Seric ** Arranges that signals are held. 9389369Seric */ 9399369Seric 94068508Seric void 9419369Seric holdsigs() 9429369Seric { 9439369Seric } 9449369Seric /* 9459369Seric ** RLSESIGS -- arrange to release all signals 9469369Seric ** 9479369Seric ** This undoes the effect of holdsigs. 9489369Seric ** 9499369Seric ** Parameters: 9509369Seric ** none. 9519369Seric ** 9529369Seric ** Returns: 9539369Seric ** none. 9549369Seric ** 9559369Seric ** Side Effects: 9569369Seric ** Arranges that signals are released. 9579369Seric */ 9589369Seric 95968508Seric void 9609369Seric rlsesigs() 9619369Seric { 9629369Seric } 96314872Seric /* 96464705Seric ** INIT_MD -- do machine dependent initializations 96564705Seric ** 96664705Seric ** Systems that have global modes that should be set should do 96764705Seric ** them here rather than in main. 96864705Seric */ 96964705Seric 97064705Seric #ifdef _AUX_SOURCE 97164705Seric # include <compat.h> 97264705Seric #endif 97364705Seric 97468508Seric void 97564825Seric init_md(argc, argv) 97664825Seric int argc; 97764825Seric char **argv; 97864705Seric { 97964705Seric #ifdef _AUX_SOURCE 98064705Seric setcompat(getcompat() | COMPAT_BSDPROT); 98164705Seric #endif 98268508Seric 98368508Seric #ifdef VENDOR_DEFAULT 98468508Seric VendorCode = VENDOR_DEFAULT; 98568508Seric #else 98668508Seric VendorCode = VENDOR_BERKELEY; 98768508Seric #endif 98864705Seric } 98964705Seric /* 99068508Seric ** INIT_VENDOR_MACROS -- vendor-dependent macro initializations 99168508Seric ** 99268508Seric ** Called once, on startup. 99368508Seric ** 99468508Seric ** Parameters: 99568508Seric ** e -- the global envelope. 99668508Seric ** 99768508Seric ** Returns: 99868508Seric ** none. 99968508Seric ** 100068508Seric ** Side Effects: 100168508Seric ** vendor-dependent. 100268508Seric */ 100368508Seric 100468508Seric void 100568508Seric init_vendor_macros(e) 100668508Seric register ENVELOPE *e; 100768508Seric { 100868508Seric } 100968508Seric /* 101014872Seric ** GETLA -- get the current load average 101114872Seric ** 101214881Seric ** This code stolen from la.c. 101314881Seric ** 101414872Seric ** Parameters: 101514872Seric ** none. 101614872Seric ** 101714872Seric ** Returns: 101814872Seric ** The current load average as an integer. 101914872Seric ** 102014872Seric ** Side Effects: 102114872Seric ** none. 102214872Seric */ 102314872Seric 102451920Seric /* try to guess what style of load average we have */ 102551920Seric #define LA_ZERO 1 /* always return load average as zero */ 102664360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 102751920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 102851920Seric #define LA_SUBR 4 /* call getloadavg */ 102964295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 103064360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 103166301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 103214872Seric 103363962Seric /* do guesses based on general OS type */ 103451920Seric #ifndef LA_TYPE 103563962Seric # define LA_TYPE LA_ZERO 103651920Seric #endif 103751920Seric 103864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 103951920Seric 104014872Seric #include <nlist.h> 104114872Seric 104268508Seric #ifdef IRIX64 104368508Seric # define nlist nlist64 104468508Seric #endif 104568508Seric 104651920Seric #ifndef LA_AVENRUN 104763962Seric # ifdef SYSTEM5 104863962Seric # define LA_AVENRUN "avenrun" 104963962Seric # else 105063962Seric # define LA_AVENRUN "_avenrun" 105163962Seric # endif 105251920Seric #endif 105351920Seric 105451920Seric /* _PATH_UNIX should be defined in <paths.h> */ 105551920Seric #ifndef _PATH_UNIX 105663962Seric # if defined(SYSTEM5) 105763962Seric # define _PATH_UNIX "/unix" 105863962Seric # else 105963962Seric # define _PATH_UNIX "/vmunix" 106063962Seric # endif 106151920Seric #endif 106251920Seric 106314872Seric struct nlist Nl[] = 106414872Seric { 106551920Seric { LA_AVENRUN }, 106614872Seric #define X_AVENRUN 0 106714872Seric { 0 }, 106814872Seric }; 106914872Seric 107060583Seric #ifndef FSHIFT 107160583Seric # if defined(unixpc) 107260583Seric # define FSHIFT 5 107360583Seric # endif 107457736Seric 107566763Seric # if defined(__alpha) || defined(IRIX) 107660583Seric # define FSHIFT 10 107760583Seric # endif 107868508Seric 107968508Seric # if defined(_AIX3) 108068508Seric # define FSHIFT 16 108168508Seric # endif 108266788Seric #endif 108357977Seric 108466788Seric #ifndef FSHIFT 108566788Seric # define FSHIFT 8 108657736Seric #endif 108760583Seric 108866788Seric #ifndef FSCALE 108966788Seric # define FSCALE (1 << FSHIFT) 109051920Seric #endif 109140930Srick 109214872Seric getla() 109314872Seric { 109414872Seric static int kmem = -1; 109551920Seric #if LA_TYPE == LA_INT 109624943Seric long avenrun[3]; 109751920Seric #else 109864360Seric # if LA_TYPE == LA_SHORT 109964360Seric short avenrun[3]; 110064360Seric # else 110151920Seric double avenrun[3]; 110264360Seric # endif 110351920Seric #endif 110425615Seric extern off_t lseek(); 110557736Seric extern int errno; 110614872Seric 110714872Seric if (kmem < 0) 110814872Seric { 110924945Seric kmem = open("/dev/kmem", 0, 0); 111014872Seric if (kmem < 0) 111157736Seric { 111257736Seric if (tTd(3, 1)) 111357736Seric printf("getla: open(/dev/kmem): %s\n", 111457736Seric errstring(errno)); 111514872Seric return (-1); 111657736Seric } 111751920Seric (void) fcntl(kmem, F_SETFD, 1); 111868508Seric #ifdef _AIX3 111968508Seric if (knlist(Nl, 1, sizeof Nl[0]) < 0) 112068508Seric #else 112157736Seric if (nlist(_PATH_UNIX, Nl) < 0) 112268508Seric #endif 112357736Seric { 112457736Seric if (tTd(3, 1)) 112557736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 112657736Seric errstring(errno)); 112714872Seric return (-1); 112857736Seric } 112959253Seric if (Nl[X_AVENRUN].n_value == 0) 113059253Seric { 113159253Seric if (tTd(3, 1)) 113259253Seric printf("getla: nlist(%s, %s) ==> 0\n", 113359253Seric _PATH_UNIX, LA_AVENRUN); 113459253Seric return (-1); 113559253Seric } 113668508Seric #ifdef NAMELISTMASK 113768508Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 113866763Seric #endif 113914872Seric } 114057736Seric if (tTd(3, 20)) 114157736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 114268508Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 || 114323118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 114419967Seric { 114519967Seric /* thank you Ian */ 114657736Seric if (tTd(3, 1)) 114757736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 114819967Seric return (-1); 114919967Seric } 115064360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 115157736Seric if (tTd(3, 5)) 115257736Seric { 115357736Seric printf("getla: avenrun = %d", avenrun[0]); 115457736Seric if (tTd(3, 15)) 115557736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 115657736Seric printf("\n"); 115757736Seric } 115857736Seric if (tTd(3, 1)) 115957736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 116024943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 116151920Seric #else 116257736Seric if (tTd(3, 5)) 116357736Seric { 116457736Seric printf("getla: avenrun = %g", avenrun[0]); 116557736Seric if (tTd(3, 15)) 116657736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 116757736Seric printf("\n"); 116857736Seric } 116957736Seric if (tTd(3, 1)) 117057736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 117151920Seric return ((int) (avenrun[0] + 0.5)); 117251920Seric #endif 117314872Seric } 117414872Seric 117551773Seric #else 117651920Seric #if LA_TYPE == LA_SUBR 117751773Seric 117864718Seric #ifdef DGUX 117964718Seric 118064718Seric #include <sys/dg_sys_info.h> 118164718Seric 118268508Seric int 118368508Seric getla() 118464718Seric { 118564718Seric struct dg_sys_info_load_info load_info; 118664718Seric 118764718Seric dg_sys_info((long *)&load_info, 118864718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 118964718Seric 119068508Seric if (tTd(3, 1)) 119168508Seric printf("getla: %d\n", (int) (load_info.one_minute + 0.5)); 119268508Seric 119364718Seric return((int) (load_info.one_minute + 0.5)); 119464718Seric } 119564718Seric 119664718Seric #else 119768508Seric # ifdef __hpux 119864718Seric 119968508Seric # include <sys/param.h> 120068508Seric # include <sys/pstat.h> 120168508Seric 120268508Seric int 120351773Seric getla() 120451773Seric { 120568508Seric struct pst_dynamic pstd; 120668508Seric 120768508Seric if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic), 120868508Seric (size_t) 1 ,0) == -1) 120968508Seric return 0; 121068508Seric 121168508Seric if (tTd(3, 1)) 121268508Seric printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5)); 121368508Seric 121468508Seric return (int) (pstd.psd_avg_1_min + 0.5); 121568508Seric } 121668508Seric 121768508Seric # else 121868508Seric 121968508Seric int 122068508Seric getla() 122168508Seric { 122251920Seric double avenrun[3]; 122351920Seric 122451920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 122557736Seric { 122657736Seric if (tTd(3, 1)) 122757736Seric perror("getla: getloadavg failed:"); 122851920Seric return (-1); 122957736Seric } 123057736Seric if (tTd(3, 1)) 123157736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 123251920Seric return ((int) (avenrun[0] + 0.5)); 123351773Seric } 123451773Seric 123568508Seric # endif /* __hpux */ 123664718Seric #endif /* DGUX */ 123751773Seric #else 123864295Seric #if LA_TYPE == LA_MACH 123951773Seric 124064295Seric /* 124165173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 124264295Seric */ 124364295Seric 124465173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 124565173Seric # include <mach/mach.h> 124665173Seric #else 124765173Seric # include <mach.h> 124865173Seric #endif 124964295Seric 125051773Seric getla() 125151773Seric { 125264295Seric processor_set_t default_set; 125364295Seric kern_return_t error; 125464295Seric unsigned int info_count; 125564295Seric struct processor_set_basic_info info; 125664295Seric host_t host; 125764295Seric 125864295Seric error = processor_set_default(host_self(), &default_set); 125964295Seric if (error != KERN_SUCCESS) 126064295Seric return -1; 126164295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 126264295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 126364295Seric &host, (processor_set_info_t)&info, 126464295Seric &info_count) != KERN_SUCCESS) 126564295Seric { 126664295Seric return -1; 126764295Seric } 126864295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 126964295Seric } 127064295Seric 127164295Seric 127264295Seric #else 127366313Seric #if LA_TYPE == LA_PROCSTR 127464295Seric 127566301Seric /* 127666301Seric ** Read /proc/loadavg for the load average. This is assumed to be 127766301Seric ** in a format like "0.15 0.12 0.06". 127866301Seric ** 127966301Seric ** Initially intended for Linux. This has been in the kernel 128066301Seric ** since at least 0.99.15. 128166301Seric */ 128266301Seric 128366301Seric # ifndef _PATH_LOADAVG 128466301Seric # define _PATH_LOADAVG "/proc/loadavg" 128566301Seric # endif 128666301Seric 128766301Seric int 128864295Seric getla() 128964295Seric { 129066319Seric double avenrun; 129166301Seric register int result; 129266319Seric FILE *fp; 129366301Seric 129466319Seric fp = fopen(_PATH_LOADAVG, "r"); 129566319Seric if (fp == NULL) 129666301Seric { 129766319Seric if (tTd(3, 1)) 129866319Seric printf("getla: fopen(%s): %s\n", 129966319Seric _PATH_LOADAVG, errstring(errno)); 130066319Seric return -1; 130166301Seric } 130266301Seric result = fscanf(fp, "%lf", &avenrun); 130366319Seric fclose(fp); 130466301Seric if (result != 1) 130566301Seric { 130666310Seric if (tTd(3, 1)) 130766301Seric printf("getla: fscanf() = %d: %s\n", 130866301Seric result, errstring(errno)); 130966301Seric return -1; 131066301Seric } 131166301Seric 131257736Seric if (tTd(3, 1)) 131366301Seric printf("getla(): %.2f\n", avenrun); 131466301Seric 131566301Seric return ((int) (avenrun + 0.5)); 131666301Seric } 131766301Seric 131866301Seric #else 131966301Seric 132066301Seric getla() 132166301Seric { 132266301Seric if (tTd(3, 1)) 132357736Seric printf("getla: ZERO\n"); 132451920Seric return (0); 132551773Seric } 132651773Seric 132751773Seric #endif 132851773Seric #endif 132964295Seric #endif 133066301Seric #endif 133166045Seric 133266045Seric 133366045Seric /* 133466045Seric * Copyright 1989 Massachusetts Institute of Technology 133566045Seric * 133666045Seric * Permission to use, copy, modify, distribute, and sell this software and its 133766045Seric * documentation for any purpose is hereby granted without fee, provided that 133866045Seric * the above copyright notice appear in all copies and that both that 133966045Seric * copyright notice and this permission notice appear in supporting 134066045Seric * documentation, and that the name of M.I.T. not be used in advertising or 134166045Seric * publicity pertaining to distribution of the software without specific, 134266045Seric * written prior permission. M.I.T. makes no representations about the 134366045Seric * suitability of this software for any purpose. It is provided "as is" 134466045Seric * without express or implied warranty. 134566045Seric * 134666045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 134766045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 134866045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 134966045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 135066045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 135166045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 135266045Seric * 135366045Seric * Authors: Many and varied... 135466045Seric */ 135566045Seric 135666045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 135766045Seric #ifndef lint 135866045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 135966045Seric #endif /* !lint */ 136066045Seric 136166179Seric #ifdef apollo 136266045Seric # undef volatile 136366045Seric # include <apollo/base.h> 136466045Seric 136566045Seric /* ARGSUSED */ 136666045Seric int getloadavg( call_data ) 136766045Seric caddr_t call_data; /* pointer to (double) return value */ 136866045Seric { 136966045Seric double *avenrun = (double *) call_data; 137066045Seric int i; 137166045Seric status_$t st; 137266045Seric long loadav[3]; 137366045Seric proc1_$get_loadav(loadav, &st); 137466045Seric *avenrun = loadav[0] / (double) (1 << 16); 137566045Seric return(0); 137666045Seric } 137766045Seric # endif /* apollo */ 137824943Seric /* 137924943Seric ** SHOULDQUEUE -- should this message be queued or sent? 138024943Seric ** 138124943Seric ** Compares the message cost to the load average to decide. 138224943Seric ** 138324943Seric ** Parameters: 138424943Seric ** pri -- the priority of the message in question. 138557438Seric ** ctime -- the message creation time. 138624943Seric ** 138724943Seric ** Returns: 138824943Seric ** TRUE -- if this message should be queued up for the 138924943Seric ** time being. 139024943Seric ** FALSE -- if the load is low enough to send this message. 139124943Seric ** 139224943Seric ** Side Effects: 139324943Seric ** none. 139424943Seric */ 139524943Seric 139624943Seric bool 139757438Seric shouldqueue(pri, ctime) 139824943Seric long pri; 139957438Seric time_t ctime; 140024943Seric { 140168508Seric bool rval; 140268508Seric 140368508Seric if (tTd(3, 30)) 140468508Seric printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri); 140551920Seric if (CurrentLA < QueueLA) 140668508Seric { 140768508Seric if (tTd(3, 30)) 140868508Seric printf("FALSE (CurrentLA < QueueLA)\n"); 140924943Seric return (FALSE); 141068508Seric } 141158132Seric if (CurrentLA >= RefuseLA) 141268508Seric { 141368508Seric if (tTd(3, 30)) 141468508Seric printf("TRUE (CurrentLA >= RefuseLA)\n"); 141558132Seric return (TRUE); 141668508Seric } 141768508Seric rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1)); 141868508Seric if (tTd(3, 30)) 141968508Seric printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE"); 142068508Seric return rval; 142124943Seric } 142224943Seric /* 142353037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 142453037Seric ** 142553037Seric ** Parameters: 142653037Seric ** none. 142753037Seric ** 142853037Seric ** Returns: 142953037Seric ** TRUE if incoming SMTP connections should be refused 143053037Seric ** (for now). 143153037Seric ** FALSE if we should accept new work. 143253037Seric ** 143353037Seric ** Side Effects: 143453037Seric ** none. 143553037Seric */ 143653037Seric 143753037Seric bool 143853037Seric refuseconnections() 143953037Seric { 144068508Seric extern bool enoughspace(); 144168508Seric 144259156Seric #ifdef XLA 144359156Seric if (!xla_smtp_ok()) 144459156Seric return TRUE; 144559156Seric #endif 144659156Seric 144753037Seric /* this is probably too simplistic */ 144868508Seric return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1); 144953037Seric } 145053037Seric /* 145124943Seric ** SETPROCTITLE -- set process title for ps 145224943Seric ** 145324943Seric ** Parameters: 145458674Seric ** fmt -- a printf style format string. 145558674Seric ** a, b, c -- possible parameters to fmt. 145624943Seric ** 145724943Seric ** Returns: 145824943Seric ** none. 145924943Seric ** 146024943Seric ** Side Effects: 146124943Seric ** Clobbers argv of our main procedure so ps(1) will 146224943Seric ** display the title. 146324943Seric */ 146424943Seric 146568508Seric #define SPT_NONE 0 /* don't use it at all */ 146668508Seric #define SPT_REUSEARGV 1 /* cover argv with title information */ 146768508Seric #define SPT_BUILTIN 2 /* use libc builtin */ 146868508Seric #define SPT_PSTAT 3 /* use pstat(PSTAT_SETCMD, ...) */ 146968508Seric #define SPT_PSSTRINGS 4 /* use PS_STRINGS->... */ 147068508Seric #define SPT_WRITEUDOT 5 /* write u. area in kmem */ 147168508Seric 147268508Seric #ifndef SPT_TYPE 147368508Seric # define SPT_TYPE SPT_REUSEARGV 147468508Seric #endif 147568508Seric 147668508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN 147768508Seric 147868508Seric # if SPT_TYPE == SPT_PSTAT 147958689Seric # include <sys/pstat.h> 148058689Seric # endif 148168508Seric # if SPT_TYPE == SPT_PSSTRINGS 148259732Seric # include <machine/vmparam.h> 148359732Seric # include <sys/exec.h> 148468508Seric # ifndef PS_STRINGS /* hmmmm.... apparently not available after all */ 148568508Seric # undef SPT_TYPE 148668508Seric # define SPT_TYPE SPT_REUSEARGV 148764247Seric # endif 148859732Seric # endif 148968508Seric 149068508Seric # if SPT_TYPE == SPT_PSSTRINGS 149168508Seric # define SETPROC_STATIC static 149268508Seric # else 149359732Seric # define SETPROC_STATIC 149459732Seric # endif 149567626Seric 149668508Seric # ifndef SPT_PADCHAR 149768508Seric # define SPT_PADCHAR ' ' 149868508Seric # endif 149958689Seric 150068508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */ 150164843Seric 150268508Seric #if SPT_TYPE != SPT_BUILTIN 150368508Seric 150424943Seric /*VARARGS1*/ 150568508Seric void 150668508Seric # ifdef __STDC__ 150757642Seric setproctitle(char *fmt, ...) 150868508Seric # else 150957642Seric setproctitle(fmt, va_alist) 151024943Seric char *fmt; 151157642Seric va_dcl 151268508Seric # endif 151324943Seric { 151468508Seric # if SPT_TYPE != SPT_NONE 151524943Seric register char *p; 151625049Seric register int i; 151759732Seric SETPROC_STATIC char buf[MAXLINE]; 151856852Seric VA_LOCAL_DECL 151968508Seric # if SPT_TYPE == SPT_PSTAT 152058689Seric union pstun pst; 152158689Seric # endif 152224943Seric extern char **Argv; 152324943Seric extern char *LastArgv; 152424943Seric 152558674Seric p = buf; 152624943Seric 152758674Seric /* print sendmail: heading for grep */ 152858674Seric (void) strcpy(p, "sendmail: "); 152958674Seric p += strlen(p); 153024943Seric 153158674Seric /* print the argument string */ 153258674Seric VA_START(fmt); 153358674Seric (void) vsprintf(p, fmt, ap); 153456852Seric VA_END; 153554996Seric 153658674Seric i = strlen(buf); 153758689Seric 153868508Seric # if SPT_TYPE == SPT_PSTAT 153958689Seric pst.pst_command = buf; 154058689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 154158689Seric # else 154268508Seric # if SPT_TYPE == SPT_PSSTRINGS 154359732Seric PS_STRINGS->ps_nargvstr = 1; 154459732Seric PS_STRINGS->ps_argvstr = buf; 154559732Seric # else 154654996Seric if (i > LastArgv - Argv[0] - 2) 154725049Seric { 154854996Seric i = LastArgv - Argv[0] - 2; 154958674Seric buf[i] = '\0'; 155025049Seric } 155158674Seric (void) strcpy(Argv[0], buf); 155254997Seric p = &Argv[0][i]; 155324943Seric while (p < LastArgv) 155468508Seric *p++ = SPT_PADCHAR; 155568508Seric Argv[1] = NULL; 155668508Seric # endif /* SPT_TYPE == SPT_PSSTRINGS */ 155768508Seric # endif /* SPT_TYPE == SPT_PSTAT */ 155868508Seric # endif /* SPT_TYPE != SPT_NONE */ 155924943Seric } 156065982Seric 156168508Seric #endif /* SPT_TYPE != SPT_BUILTIN */ 156225698Seric /* 156325698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 156425698Seric ** 156525698Seric ** Parameters: 156668508Seric ** sig -- the signal that got us here (unused). 156725698Seric ** 156825698Seric ** Returns: 156925698Seric ** none. 157025698Seric ** 157125698Seric ** Side Effects: 157225698Seric ** Picks up extant zombies. 157325698Seric */ 157425698Seric 157546928Sbostic void 157668508Seric reapchild(sig) 157768508Seric int sig; 157825698Seric { 157964738Seric int olderrno = errno; 158063966Seric # ifdef HASWAITPID 158160219Seric auto int status; 158260560Seric int count; 158360560Seric int pid; 158460219Seric 158560560Seric count = 0; 158660560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 158760560Seric { 158860560Seric if (count++ > 1000) 158960560Seric { 159066748Seric #ifdef LOG 159160560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 159260560Seric pid, status); 159366748Seric #endif 159460560Seric break; 159560560Seric } 159660560Seric } 159760219Seric # else 159825698Seric # ifdef WNOHANG 159925698Seric union wait status; 160025698Seric 160163753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 160225698Seric continue; 160356795Seric # else /* WNOHANG */ 160425698Seric auto int status; 160525698Seric 160660219Seric while (wait(&status) > 0) 160725698Seric continue; 160856795Seric # endif /* WNOHANG */ 160960219Seric # endif 161064035Seric # ifdef SYS5SIGNALS 161164035Seric (void) setsignal(SIGCHLD, reapchild); 161258061Seric # endif 161364738Seric errno = olderrno; 161425698Seric } 161555418Seric /* 161655418Seric ** UNSETENV -- remove a variable from the environment 161755418Seric ** 161855418Seric ** Not needed on newer systems. 161955418Seric ** 162055418Seric ** Parameters: 162155418Seric ** name -- the string name of the environment variable to be 162255418Seric ** deleted from the current environment. 162355418Seric ** 162455418Seric ** Returns: 162555418Seric ** none. 162655418Seric ** 162755418Seric ** Globals: 162855418Seric ** environ -- a pointer to the current environment. 162955418Seric ** 163055418Seric ** Side Effects: 163155418Seric ** Modifies environ. 163255418Seric */ 163355418Seric 163463962Seric #ifndef HASUNSETENV 163555418Seric 163655418Seric void 163755418Seric unsetenv(name) 163855418Seric char *name; 163955418Seric { 164055418Seric extern char **environ; 164155418Seric register char **pp; 164255418Seric int len = strlen(name); 164355418Seric 164455418Seric for (pp = environ; *pp != NULL; pp++) 164555418Seric { 164655418Seric if (strncmp(name, *pp, len) == 0 && 164755418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 164855418Seric break; 164955418Seric } 165055418Seric 165155418Seric for (; *pp != NULL; pp++) 165255418Seric *pp = pp[1]; 165355418Seric } 165455418Seric 165563962Seric #endif 165656215Seric /* 165756215Seric ** GETDTABLESIZE -- return number of file descriptors 165856215Seric ** 165956215Seric ** Only on non-BSD systems 166056215Seric ** 166156215Seric ** Parameters: 166256215Seric ** none 166356215Seric ** 166456215Seric ** Returns: 166556215Seric ** size of file descriptor table 166656215Seric ** 166756215Seric ** Side Effects: 166856215Seric ** none 166956215Seric */ 167056215Seric 167163787Seric #ifdef SOLARIS 167263787Seric # include <sys/resource.h> 167363787Seric #endif 167456215Seric 167556215Seric int 167663787Seric getdtsize() 167756215Seric { 167863787Seric #ifdef RLIMIT_NOFILE 167963787Seric struct rlimit rl; 168063787Seric 168163787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 168263787Seric return rl.rlim_cur; 168363787Seric #endif 168463787Seric 168564031Seric # ifdef HASGETDTABLESIZE 168664031Seric return getdtablesize(); 168764031Seric # else 168864031Seric # ifdef _SC_OPEN_MAX 168958689Seric return sysconf(_SC_OPEN_MAX); 169063787Seric # else 169156215Seric return NOFILE; 169263787Seric # endif 169358689Seric # endif 169456215Seric } 169557631Seric /* 169657631Seric ** UNAME -- get the UUCP name of this system. 169757631Seric */ 169857631Seric 169957943Seric #ifndef HASUNAME 170057631Seric 170157631Seric int 170257631Seric uname(name) 170357631Seric struct utsname *name; 170457631Seric { 170557631Seric FILE *file; 170657631Seric char *n; 170757631Seric 170857631Seric name->nodename[0] = '\0'; 170957631Seric 171057661Seric /* try /etc/whoami -- one line with the node name */ 171157631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 171257631Seric { 171357661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 171457631Seric (void) fclose(file); 171557661Seric n = strchr(name->nodename, '\n'); 171657631Seric if (n != NULL) 171757631Seric *n = '\0'; 171857631Seric if (name->nodename[0] != '\0') 171957631Seric return (0); 172057631Seric } 172157631Seric 172257661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 172357631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 172457631Seric { 172557631Seric char buf[MAXLINE]; 172657631Seric 172757631Seric while (fgets(buf, MAXLINE, file) != NULL) 172857631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 172957631Seric NODE_LENGTH, name->nodename) > 0) 173057631Seric break; 173157631Seric (void) fclose(file); 173257631Seric if (name->nodename[0] != '\0') 173357631Seric return (0); 173457631Seric } 173557631Seric 173657631Seric #ifdef TRUST_POPEN 173757631Seric /* 173857631Seric ** Popen is known to have security holes. 173957631Seric */ 174057631Seric 174157661Seric /* try uuname -l to return local name */ 174257631Seric if ((file = popen("uuname -l", "r")) != NULL) 174357631Seric { 174457661Seric (void) fgets(name, NODE_LENGTH + 1, file); 174557631Seric (void) pclose(file); 174657661Seric n = strchr(name, '\n'); 174757631Seric if (n != NULL) 174857631Seric *n = '\0'; 174957661Seric if (name->nodename[0] != '\0') 175057631Seric return (0); 175157631Seric } 175257631Seric #endif 175357631Seric 175457631Seric return (-1); 175557631Seric } 175657943Seric #endif /* HASUNAME */ 175758068Seric /* 175858068Seric ** INITGROUPS -- initialize groups 175958068Seric ** 176058068Seric ** Stub implementation for System V style systems 176158068Seric */ 176258068Seric 176358068Seric #ifndef HASINITGROUPS 176458068Seric 176558068Seric initgroups(name, basegid) 176658068Seric char *name; 176758068Seric int basegid; 176858068Seric { 176958068Seric return 0; 177058068Seric } 177158068Seric 177258068Seric #endif 177358082Seric /* 177459289Seric ** SETSID -- set session id (for non-POSIX systems) 177559289Seric */ 177659289Seric 177759289Seric #ifndef HASSETSID 177859289Seric 177963753Seric pid_t 178063753Seric setsid __P ((void)) 178159289Seric { 178264155Seric #ifdef TIOCNOTTY 178364155Seric int fd; 178464155Seric 178566743Seric fd = open("/dev/tty", O_RDWR, 0); 178664155Seric if (fd >= 0) 178764155Seric { 178864155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 178964155Seric (void) close(fd); 179064155Seric } 179164155Seric #endif /* TIOCNOTTY */ 179264705Seric # ifdef SYS5SETPGRP 179363753Seric return setpgrp(); 179463753Seric # else 179564155Seric return setpgid(0, getpid()); 179659289Seric # endif 179759289Seric } 179859289Seric 179959289Seric #endif 180059289Seric /* 180166748Seric ** FSYNC -- dummy fsync 180266748Seric */ 180366748Seric 180466748Seric #ifdef NEEDFSYNC 180566748Seric 180666748Seric fsync(fd) 180766748Seric int fd; 180866748Seric { 180966748Seric # ifdef O_SYNC 181066748Seric return fcntl(fd, F_SETFL, O_SYNC); 181166748Seric # else 181266748Seric /* nothing we can do */ 181366748Seric return 0; 181466748Seric # endif 181566748Seric } 181666748Seric 181766748Seric #endif 181866748Seric /* 181964718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 182064718Seric ** 182164718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 182268508Seric ** instead of a long. This patches things. Only needed on versions 182368508Seric ** prior to 5.4.3. 182464718Seric */ 182564718Seric 182668508Seric #ifdef DGUX_5_4_2 182764718Seric 182864718Seric #undef inet_addr 182964718Seric 183064718Seric long 183164718Seric dgux_inet_addr(host) 183264718Seric char *host; 183364718Seric { 183464718Seric struct in_addr haddr; 183564718Seric 183664718Seric haddr = inet_addr(host); 183764718Seric return haddr.s_addr; 183864718Seric } 183964718Seric 184064718Seric #endif 184164718Seric /* 184263969Seric ** GETOPT -- for old systems or systems with bogus implementations 184363969Seric */ 184463969Seric 184563969Seric #ifdef NEEDGETOPT 184663969Seric 184763969Seric /* 184863969Seric * Copyright (c) 1985 Regents of the University of California. 184963969Seric * All rights reserved. The Berkeley software License Agreement 185063969Seric * specifies the terms and conditions for redistribution. 185163969Seric */ 185263969Seric 185363969Seric 185463969Seric /* 185563969Seric ** this version hacked to add `atend' flag to allow state machine 185663969Seric ** to reset if invoked by the program to scan args for a 2nd time 185763969Seric */ 185863969Seric 185963969Seric #if defined(LIBC_SCCS) && !defined(lint) 186063969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 186164072Seric #endif /* LIBC_SCCS and not lint */ 186263969Seric 186363969Seric #include <stdio.h> 186463969Seric 186563969Seric /* 186663969Seric * get option letter from argument vector 186763969Seric */ 186865352Seric #ifdef _CONVEX_SOURCE 186968508Seric extern int optind, opterr, optopt; 187068508Seric extern char *optarg; 187165352Seric #else 187265352Seric int opterr = 1; /* if error message should be printed */ 187365352Seric int optind = 1; /* index into parent argv vector */ 187468470Seric int optopt = 0; /* character checked for validity */ 187568470Seric char *optarg = NULL; /* argument associated with option */ 187668508Seric #endif 187763969Seric 187863969Seric #define BADCH (int)'?' 187963969Seric #define EMSG "" 188063969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 188163969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 188263969Seric 188363969Seric getopt(nargc,nargv,ostr) 188465160Seric int nargc; 188565160Seric char *const *nargv; 188665160Seric const char *ostr; 188763969Seric { 188863969Seric static char *place = EMSG; /* option letter processing */ 188963969Seric static char atend = 0; 189063969Seric register char *oli; /* option letter list index */ 189163969Seric 189263969Seric if (atend) { 189363969Seric atend = 0; 189463969Seric place = EMSG; 189563969Seric } 189663969Seric if(!*place) { /* update scanning pointer */ 189764136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 189863969Seric atend++; 189963969Seric return(EOF); 190063969Seric } 190163969Seric if (*place == '-') { /* found "--" */ 190263969Seric ++optind; 190363969Seric atend++; 190463969Seric return(EOF); 190563969Seric } 190663969Seric } /* option letter okay? */ 190764125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 190864136Seric if (!*place) ++optind; 190963969Seric tell(": illegal option -- "); 191063969Seric } 191163969Seric if (*++oli != ':') { /* don't need argument */ 191263969Seric optarg = NULL; 191363969Seric if (!*place) ++optind; 191463969Seric } 191563969Seric else { /* need an argument */ 191663969Seric if (*place) optarg = place; /* no white space */ 191763969Seric else if (nargc <= ++optind) { /* no arg */ 191863969Seric place = EMSG; 191963969Seric tell(": option requires an argument -- "); 192063969Seric } 192163969Seric else optarg = nargv[optind]; /* white space */ 192263969Seric place = EMSG; 192363969Seric ++optind; 192463969Seric } 192563969Seric return(optopt); /* dump back option letter */ 192663969Seric } 192763969Seric 192863969Seric #endif 192963969Seric /* 193063969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 193163969Seric */ 193263969Seric 193363969Seric #ifdef NEEDVPRINTF 193463969Seric 193563969Seric #define MAXARG 16 193663969Seric 193763969Seric vfprintf(fp, fmt, ap) 193863969Seric FILE * fp; 193963969Seric char * fmt; 194063969Seric char ** ap; 194163969Seric { 194263969Seric char * bp[MAXARG]; 194363969Seric int i = 0; 194463969Seric 194563969Seric while (*ap && i < MAXARG) 194663969Seric bp[i++] = *ap++; 194763969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 194863969Seric bp[4], bp[5], bp[6], bp[7], 194963969Seric bp[8], bp[9], bp[10], bp[11], 195063969Seric bp[12], bp[13], bp[14], bp[15]); 195163969Seric } 195263969Seric 195363969Seric vsprintf(s, fmt, ap) 195463969Seric char * s; 195563969Seric char * fmt; 195663969Seric char ** ap; 195763969Seric { 195863969Seric char * bp[MAXARG]; 195963969Seric int i = 0; 196063969Seric 196163969Seric while (*ap && i < MAXARG) 196263969Seric bp[i++] = *ap++; 196363969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 196463969Seric bp[4], bp[5], bp[6], bp[7], 196563969Seric bp[8], bp[9], bp[10], bp[11], 196663969Seric bp[12], bp[13], bp[14], bp[15]); 196763969Seric } 196863969Seric 196963969Seric #endif 197063969Seric /* 197165220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 197265208Seric ** 197365211Seric ** Parameters: 197465211Seric ** shell -- the user's shell from /etc/passwd 197565211Seric ** 197665211Seric ** Returns: 197765211Seric ** TRUE -- if it is ok to use this for unrestricted access. 197865211Seric ** FALSE -- if the shell is restricted. 197965208Seric */ 198065208Seric 198165587Seric #if !HASGETUSERSHELL 198265587Seric 198365587Seric # ifndef _PATH_SHELLS 198465587Seric # define _PATH_SHELLS "/etc/shells" 198565587Seric # endif 198665587Seric 198765587Seric char *DefaultUserShells[] = 198865587Seric { 198968508Seric "/bin/sh", /* standard shell */ 199065587Seric "/usr/bin/sh", 199168508Seric "/bin/csh", /* C shell */ 199265587Seric "/usr/bin/csh", 199365587Seric #ifdef __hpux 199468508Seric "/bin/rsh", /* restricted Bourne shell */ 199568508Seric "/bin/ksh", /* Korn shell */ 199668508Seric "/bin/rksh", /* restricted Korn shell */ 199765587Seric "/bin/pam", 199868508Seric "/usr/bin/keysh", /* key shell (extended Korn shell) */ 199965587Seric "/bin/posix/sh", 200065211Seric #endif 200168508Seric #ifdef _AIX3 200268508Seric "/bin/ksh", /* Korn shell */ 200368508Seric "/usr/bin/ksh", 200468508Seric "/bin/tsh", /* trusted shell */ 200568508Seric "/usr/bin/tsh", 200668508Seric "/bin/bsh", /* Bourne shell */ 200768508Seric "/usr/bin/bsh", 200868508Seric #endif 200965587Seric NULL 201065587Seric }; 201165587Seric 201265208Seric #endif 201365208Seric 201465747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 201565747Seric 201665211Seric bool 201765211Seric usershellok(shell) 201865211Seric char *shell; 201965208Seric { 202065211Seric #if HASGETUSERSHELL 202165211Seric register char *p; 202265211Seric extern char *getusershell(); 202365208Seric 202468457Seric if (shell == NULL || shell[0] == '\0') 202568457Seric return TRUE; 202668457Seric 202765211Seric setusershell(); 202865211Seric while ((p = getusershell()) != NULL) 202965747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 203065211Seric break; 203165211Seric endusershell(); 203265211Seric return p != NULL; 203365211Seric #else 203465211Seric register FILE *shellf; 203565211Seric char buf[MAXLINE]; 203665208Seric 203768457Seric if (shell == NULL || shell[0] == '\0') 203868457Seric return TRUE; 203968457Seric 204065211Seric shellf = fopen(_PATH_SHELLS, "r"); 204165211Seric if (shellf == NULL) 204265211Seric { 204365211Seric /* no /etc/shells; see if it is one of the std shells */ 204465587Seric char **d; 204565587Seric 204665587Seric for (d = DefaultUserShells; *d != NULL; d++) 204765587Seric { 204865587Seric if (strcmp(shell, *d) == 0) 204965587Seric return TRUE; 205065587Seric } 205165587Seric return FALSE; 205265211Seric } 205365208Seric 205465211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 205565211Seric { 205665211Seric register char *p, *q; 205765208Seric 205865211Seric p = buf; 205965211Seric while (*p != '\0' && *p != '#' && *p != '/') 206065211Seric p++; 206165211Seric if (*p == '#' || *p == '\0') 206265208Seric continue; 206365211Seric q = p; 206465211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 206565211Seric p++; 206665211Seric *p = '\0'; 206765747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 206865211Seric { 206965211Seric fclose(shellf); 207065211Seric return TRUE; 207165211Seric } 207265208Seric } 207365211Seric fclose(shellf); 207465211Seric return FALSE; 207565211Seric #endif 207665208Seric } 207765208Seric /* 207864311Seric ** FREESPACE -- see how much free space is on the queue filesystem 207958082Seric ** 208058082Seric ** Only implemented if you have statfs. 208158082Seric ** 208258082Seric ** Parameters: 208364311Seric ** dir -- the directory in question. 208464311Seric ** bsize -- a variable into which the filesystem 208564311Seric ** block size is stored. 208658082Seric ** 208758082Seric ** Returns: 208864311Seric ** The number of bytes free on the queue filesystem. 208964311Seric ** -1 if the statfs call fails. 209064311Seric ** 209164311Seric ** Side effects: 209264311Seric ** Puts the filesystem block size into bsize. 209358082Seric */ 209458082Seric 209565749Seric /* statfs types */ 209665749Seric #define SFS_NONE 0 /* no statfs implementation */ 209765749Seric #define SFS_USTAT 1 /* use ustat */ 209865749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 209965749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 210065749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 210165749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 210266752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 210365749Seric 210465749Seric #ifndef SFS_TYPE 210565749Seric # define SFS_TYPE SFS_NONE 210658157Seric #endif 210758157Seric 210865749Seric #if SFS_TYPE == SFS_USTAT 210958157Seric # include <ustat.h> 211058157Seric #endif 211165749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 211265749Seric # include <sys/statfs.h> 211358082Seric #endif 211465749Seric #if SFS_TYPE == SFS_VFS 211565749Seric # include <sys/vfs.h> 211665749Seric #endif 211765749Seric #if SFS_TYPE == SFS_MOUNT 211865749Seric # include <sys/mount.h> 211965749Seric #endif 212066752Seric #if SFS_TYPE == SFS_STATVFS 212166752Seric # include <sys/statvfs.h> 212266752Seric #endif 212358082Seric 212464311Seric long 212564311Seric freespace(dir, bsize) 212664311Seric char *dir; 212764311Seric long *bsize; 212858082Seric { 212965749Seric #if SFS_TYPE != SFS_NONE 213065749Seric # if SFS_TYPE == SFS_USTAT 213158153Seric struct ustat fs; 213258153Seric struct stat statbuf; 213358366Seric # define FSBLOCKSIZE DEV_BSIZE 213468508Seric # define FSF_BAVAIL f_tfree 213558157Seric # else 213658157Seric # if defined(ultrix) 213758157Seric struct fs_data fs; 213868508Seric # define FSF_BAVAIL fd_bfreen 213968508Seric # define FSBLOCKSIZE 1024L 214058153Seric # else 214166752Seric # if SFS_TYPE == SFS_STATVFS 214266752Seric struct statvfs fs; 214368508Seric # define FSBLOCKSIZE fs.f_frsize 214466752Seric # else 214558082Seric struct statfs fs; 214666752Seric # define FSBLOCKSIZE fs.f_bsize 214763987Seric # endif 214858153Seric # endif 214958133Seric # endif 215068508Seric # ifndef FSF_BAVAIL 215168508Seric # define FSF_BAVAIL f_bavail 215268508Seric # endif 215358082Seric 215465749Seric # if SFS_TYPE == SFS_USTAT 215564311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 215658157Seric # else 215765749Seric # if SFS_TYPE == SFS_4ARGS 215864311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 215958157Seric # else 216068508Seric # if SFS_TYPE == SFS_STATVFS 216168508Seric if (statvfs(dir, &fs) == 0) 216268508Seric # else 216368508Seric # if defined(ultrix) 216468499Seric if (statfs(dir, &fs) > 0) 216568508Seric # else 216664311Seric if (statfs(dir, &fs) == 0) 216768508Seric # endif 216858153Seric # endif 216958133Seric # endif 217058133Seric # endif 217158133Seric { 217264311Seric if (bsize != NULL) 217364311Seric *bsize = FSBLOCKSIZE; 217468508Seric return (fs.FSF_BAVAIL); 217564311Seric } 217664337Seric #endif 217764311Seric return (-1); 217864311Seric } 217964311Seric /* 218064311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 218164311Seric ** 218264311Seric ** Only implemented if you have statfs. 218364311Seric ** 218464311Seric ** Parameters: 218564311Seric ** msize -- the size to check against. If zero, we don't yet 218664311Seric ** know how big the message will be, so just check for 218764311Seric ** a "reasonable" amount. 218864311Seric ** 218964311Seric ** Returns: 219064311Seric ** TRUE if there is enough space. 219164311Seric ** FALSE otherwise. 219264311Seric */ 219364311Seric 219464311Seric bool 219564311Seric enoughspace(msize) 219664311Seric long msize; 219764311Seric { 219864311Seric long bfree, bsize; 219964311Seric 220064311Seric if (MinBlocksFree <= 0 && msize <= 0) 220164311Seric { 220258133Seric if (tTd(4, 80)) 220364311Seric printf("enoughspace: no threshold\n"); 220464311Seric return TRUE; 220564311Seric } 220664311Seric 220764311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 220864311Seric { 220964311Seric if (tTd(4, 80)) 221058333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 221164311Seric bfree, msize); 221258333Seric 221358333Seric /* convert msize to block count */ 221464311Seric msize = msize / bsize + 1; 221558333Seric if (MinBlocksFree >= 0) 221658333Seric msize += MinBlocksFree; 221758333Seric 221864311Seric if (bfree < msize) 221958090Seric { 222058090Seric #ifdef LOG 222158090Seric if (LogLevel > 0) 222264254Seric syslog(LOG_ALERT, 222364254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 222468508Seric CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id, 222568508Seric bfree, 222668508Seric CurHostName == NULL ? "SMTP-DAEMON" : CurHostName, 222768508Seric msize, QueueDir); 222858090Seric #endif 222958082Seric return FALSE; 223058090Seric } 223158082Seric } 223258082Seric else if (tTd(4, 80)) 223358333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 223458333Seric MinBlocksFree, msize, errstring(errno)); 223558082Seric return TRUE; 223658082Seric } 223758542Seric /* 223858542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 223958542Seric ** 224058542Seric ** This looks at an errno value and tells if this is likely to 224158542Seric ** go away if retried later. 224258542Seric ** 224358542Seric ** Parameters: 224458542Seric ** err -- the errno code to classify. 224558542Seric ** 224658542Seric ** Returns: 224758542Seric ** TRUE if this is probably transient. 224858542Seric ** FALSE otherwise. 224958542Seric */ 225058542Seric 225158542Seric bool 225258542Seric transienterror(err) 225358542Seric int err; 225458542Seric { 225558542Seric switch (err) 225658542Seric { 225758542Seric case EIO: /* I/O error */ 225858542Seric case ENXIO: /* Device not configured */ 225958542Seric case EAGAIN: /* Resource temporarily unavailable */ 226058542Seric case ENOMEM: /* Cannot allocate memory */ 226158542Seric case ENODEV: /* Operation not supported by device */ 226258542Seric case ENFILE: /* Too many open files in system */ 226358542Seric case EMFILE: /* Too many open files */ 226458542Seric case ENOSPC: /* No space left on device */ 226558542Seric #ifdef ETIMEDOUT 226658542Seric case ETIMEDOUT: /* Connection timed out */ 226758542Seric #endif 226858542Seric #ifdef ESTALE 226958542Seric case ESTALE: /* Stale NFS file handle */ 227058542Seric #endif 227158542Seric #ifdef ENETDOWN 227258542Seric case ENETDOWN: /* Network is down */ 227358542Seric #endif 227458542Seric #ifdef ENETUNREACH 227558542Seric case ENETUNREACH: /* Network is unreachable */ 227658542Seric #endif 227758542Seric #ifdef ENETRESET 227858542Seric case ENETRESET: /* Network dropped connection on reset */ 227958542Seric #endif 228058542Seric #ifdef ECONNABORTED 228158542Seric case ECONNABORTED: /* Software caused connection abort */ 228258542Seric #endif 228358542Seric #ifdef ECONNRESET 228458542Seric case ECONNRESET: /* Connection reset by peer */ 228558542Seric #endif 228658542Seric #ifdef ENOBUFS 228758542Seric case ENOBUFS: /* No buffer space available */ 228858542Seric #endif 228958542Seric #ifdef ESHUTDOWN 229058542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 229158542Seric #endif 229258542Seric #ifdef ECONNREFUSED 229358542Seric case ECONNREFUSED: /* Connection refused */ 229458542Seric #endif 229558542Seric #ifdef EHOSTDOWN 229658542Seric case EHOSTDOWN: /* Host is down */ 229758542Seric #endif 229858542Seric #ifdef EHOSTUNREACH 229958542Seric case EHOSTUNREACH: /* No route to host */ 230058542Seric #endif 230158542Seric #ifdef EDQUOT 230258542Seric case EDQUOT: /* Disc quota exceeded */ 230358542Seric #endif 230458542Seric #ifdef EPROCLIM 230558542Seric case EPROCLIM: /* Too many processes */ 230658542Seric #endif 230758542Seric #ifdef EUSERS 230858542Seric case EUSERS: /* Too many users */ 230958542Seric #endif 231058542Seric #ifdef EDEADLK 231158542Seric case EDEADLK: /* Resource deadlock avoided */ 231258542Seric #endif 231358542Seric #ifdef EISCONN 231458542Seric case EISCONN: /* Socket already connected */ 231558542Seric #endif 231658542Seric #ifdef EINPROGRESS 231758542Seric case EINPROGRESS: /* Operation now in progress */ 231858542Seric #endif 231958542Seric #ifdef EALREADY 232058542Seric case EALREADY: /* Operation already in progress */ 232158542Seric #endif 232258542Seric #ifdef EADDRINUSE 232358542Seric case EADDRINUSE: /* Address already in use */ 232458542Seric #endif 232558542Seric #ifdef EADDRNOTAVAIL 232658542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 232758542Seric #endif 232866018Seric #ifdef ETXTBSY 232966017Seric case ETXTBSY: /* (Apollo) file locked */ 233066017Seric #endif 233163834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 233258542Seric case ENOSR: /* Out of streams resources */ 233358542Seric #endif 233458542Seric return TRUE; 233558542Seric } 233658542Seric 233758542Seric /* nope, must be permanent */ 233858542Seric return FALSE; 233958542Seric } 234058689Seric /* 234164035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 234258689Seric ** 234358689Seric ** Parameters: 234458689Seric ** fd -- the file descriptor of the file. 234558689Seric ** filename -- the file name (for error messages). 234664335Seric ** ext -- the filename extension. 234758689Seric ** type -- type of the lock. Bits can be: 234858689Seric ** LOCK_EX -- exclusive lock. 234958689Seric ** LOCK_NB -- non-blocking. 235058689Seric ** 235158689Seric ** Returns: 235258689Seric ** TRUE if the lock was acquired. 235358689Seric ** FALSE otherwise. 235458689Seric */ 235558689Seric 235658689Seric bool 235764335Seric lockfile(fd, filename, ext, type) 235858689Seric int fd; 235958689Seric char *filename; 236064335Seric char *ext; 236158689Seric int type; 236258689Seric { 236365830Seric # if !HASFLOCK 236458689Seric int action; 236558689Seric struct flock lfd; 236664335Seric 236764335Seric if (ext == NULL) 236864335Seric ext = ""; 236964035Seric 237063983Seric bzero(&lfd, sizeof lfd); 237159447Seric if (bitset(LOCK_UN, type)) 237259447Seric lfd.l_type = F_UNLCK; 237359447Seric else if (bitset(LOCK_EX, type)) 237458689Seric lfd.l_type = F_WRLCK; 237558689Seric else 237658689Seric lfd.l_type = F_RDLCK; 237758689Seric 237858689Seric if (bitset(LOCK_NB, type)) 237958689Seric action = F_SETLK; 238058689Seric else 238158689Seric action = F_SETLKW; 238258689Seric 238364264Seric if (tTd(55, 60)) 238464335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 238564335Seric filename, ext, action, lfd.l_type); 238664264Seric 238758689Seric if (fcntl(fd, action, &lfd) >= 0) 238864264Seric { 238964264Seric if (tTd(55, 60)) 239064264Seric printf("SUCCESS\n"); 239158689Seric return TRUE; 239264264Seric } 239358689Seric 239464264Seric if (tTd(55, 60)) 239564264Seric printf("(%s) ", errstring(errno)); 239664264Seric 239764136Seric /* 239864136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 239964136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 240064136Seric ** as type "tmp" (that is, served from swap space), the 240164136Seric ** previous fcntl will fail with "Invalid argument" errors. 240264136Seric ** Since this is fairly common during testing, we will assume 240364136Seric ** that this indicates that the lock is successfully grabbed. 240464136Seric */ 240564136Seric 240664136Seric if (errno == EINVAL) 240764264Seric { 240864264Seric if (tTd(55, 60)) 240964264Seric printf("SUCCESS\n"); 241064136Seric return TRUE; 241164264Seric } 241264136Seric 241358689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 241464378Seric { 241564378Seric int omode = -1; 241664378Seric # ifdef F_GETFL 241764378Seric int oerrno = errno; 241864378Seric 241964378Seric (void) fcntl(fd, F_GETFL, &omode); 242064378Seric errno = oerrno; 242164378Seric # endif 242264378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 242364378Seric filename, ext, fd, type, omode, geteuid()); 242464378Seric } 242558689Seric # else 242664335Seric if (ext == NULL) 242764335Seric ext = ""; 242864335Seric 242964264Seric if (tTd(55, 60)) 243064335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 243164264Seric 243258689Seric if (flock(fd, type) >= 0) 243364264Seric { 243464264Seric if (tTd(55, 60)) 243564264Seric printf("SUCCESS\n"); 243658689Seric return TRUE; 243764264Seric } 243858689Seric 243964264Seric if (tTd(55, 60)) 244064264Seric printf("(%s) ", errstring(errno)); 244164264Seric 244258689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 244364378Seric { 244464378Seric int omode = -1; 244564378Seric # ifdef F_GETFL 244664378Seric int oerrno = errno; 244764378Seric 244864378Seric (void) fcntl(fd, F_GETFL, &omode); 244964378Seric errno = oerrno; 245064378Seric # endif 245164378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 245264378Seric filename, ext, fd, type, omode, geteuid()); 245364386Seric } 245458689Seric # endif 245564264Seric if (tTd(55, 60)) 245664264Seric printf("FAIL\n"); 245758689Seric return FALSE; 245858689Seric } 245964556Seric /* 246065948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 246165948Seric ** 246265948Seric ** Parameters: 246365948Seric ** fd -- the file descriptor to check. 246465948Seric ** 246565948Seric ** Returns: 246665948Seric ** TRUE -- if only root can chown the file to an arbitrary 246765948Seric ** user. 246865948Seric ** FALSE -- if an arbitrary user can give away a file. 246965948Seric */ 247065948Seric 247165948Seric bool 247265948Seric chownsafe(fd) 247365948Seric int fd; 247465948Seric { 247565950Seric #ifdef __hpux 247665948Seric char *s; 247765948Seric int tfd; 247865948Seric uid_t o_uid, o_euid; 247965948Seric gid_t o_gid, o_egid; 248065948Seric bool rval; 248165948Seric struct stat stbuf; 248265948Seric 248365948Seric o_uid = getuid(); 248465948Seric o_euid = geteuid(); 248565948Seric o_gid = getgid(); 248665948Seric o_egid = getegid(); 248765959Seric fstat(fd, &stbuf); 248865948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 248965948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 249065948Seric s = tmpnam(NULL); 249165948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 249265974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 249365948Seric close(tfd); 249465948Seric unlink(s); 249565948Seric setreuid(o_uid, o_euid); 249665948Seric setresgid(o_gid, o_egid, -1); 249765948Seric return rval; 249865948Seric #else 249966088Seric # ifdef _POSIX_CHOWN_RESTRICTED 250066088Seric # if _POSIX_CHOWN_RESTRICTED == -1 250166088Seric return FALSE; 250266088Seric # else 250366088Seric return TRUE; 250466088Seric # endif 250566088Seric # else 250666088Seric # ifdef _PC_CHOWN_RESTRICTED 250768508Seric int rval; 250868508Seric 250968508Seric /* 251068508Seric ** Some systems (e.g., SunOS) seem to have the call and the 251168508Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 251268508Seric ** the call. This heuristic checks for that. 251368508Seric */ 251468508Seric 251568508Seric errno = 0; 251668508Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 251768508Seric if (errno == 0) 251868508Seric return rval > 0; 251968508Seric # endif 252068508Seric # ifdef BSD 252168508Seric return TRUE; 252268499Seric # else 252365948Seric return FALSE; 252465948Seric # endif 252565948Seric # endif 252665948Seric #endif 252765948Seric } 252865948Seric /* 252968508Seric ** RESETLIMITS -- reset system controlled resource limits 253068508Seric ** 253168508Seric ** This is to avoid denial-of-service attacks 253268508Seric ** 253368508Seric ** Parameters: 253468508Seric ** none 253568508Seric ** 253668508Seric ** Returns: 253768508Seric ** none 253868508Seric */ 253968508Seric 254068508Seric #if HASSETRLIMIT 254168508Seric # include <sys/resource.h> 254268508Seric #endif 254368508Seric 254468508Seric void 254568508Seric resetlimits() 254668508Seric { 254768508Seric #if HASSETRLIMIT 254868508Seric struct rlimit lim; 254968508Seric 255068508Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 255168508Seric (void) setrlimit(RLIMIT_CPU, &lim); 255268508Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 255368508Seric #else 255468508Seric # if HASULIMIT 255568508Seric (void) ulimit(2, 0x3fffff); 255668508Seric # endif 255768508Seric #endif 255868508Seric } 255968508Seric /* 256064556Seric ** GETCFNAME -- return the name of the .cf file. 256164556Seric ** 256264556Seric ** Some systems (e.g., NeXT) determine this dynamically. 256364556Seric */ 256464556Seric 256564556Seric char * 256664556Seric getcfname() 256764556Seric { 256864556Seric if (ConfFile != NULL) 256964556Seric return ConfFile; 257065214Seric #ifdef NETINFO 257165214Seric { 257265214Seric extern char *ni_propval(); 257365214Seric char *cflocation; 257465214Seric 257568508Seric cflocation = ni_propval("/locations", NULL, "sendmail", 257668508Seric "sendmail.cf", '\0'); 257765214Seric if (cflocation != NULL) 257865214Seric return cflocation; 257965214Seric } 258065214Seric #endif 258164556Seric return _PATH_SENDMAILCF; 258264556Seric } 258364718Seric /* 258464718Seric ** SETVENDOR -- process vendor code from V configuration line 258564718Seric ** 258664718Seric ** Parameters: 258764718Seric ** vendor -- string representation of vendor. 258864718Seric ** 258964718Seric ** Returns: 259064718Seric ** TRUE -- if ok. 259164718Seric ** FALSE -- if vendor code could not be processed. 259264926Seric ** 259364926Seric ** Side Effects: 259464926Seric ** It is reasonable to set mode flags here to tweak 259564926Seric ** processing in other parts of the code if necessary. 259664926Seric ** For example, if you are a vendor that uses $%y to 259764926Seric ** indicate YP lookups, you could enable that here. 259864718Seric */ 259964718Seric 260064718Seric bool 260164718Seric setvendor(vendor) 260264718Seric char *vendor; 260364718Seric { 260464926Seric if (strcasecmp(vendor, "Berkeley") == 0) 260568508Seric { 260668508Seric VendorCode = VENDOR_BERKELEY; 260764926Seric return TRUE; 260868508Seric } 260964926Seric 261064926Seric /* add vendor extensions here */ 261164926Seric 261268508Seric #ifdef SUN_EXTENSIONS 261368508Seric if (strcasecmp(vendor, "Sun") == 0) 261468508Seric { 261568508Seric VendorCode = VENDOR_SUN; 261668508Seric return TRUE; 261768508Seric } 261868508Seric #endif 261968508Seric 262064926Seric return FALSE; 262164718Seric } 262264816Seric /* 262364816Seric ** STRTOL -- convert string to long integer 262464816Seric ** 262564816Seric ** For systems that don't have it in the C library. 262666161Seric ** 262766161Seric ** This is taken verbatim from the 4.4-Lite C library. 262864816Seric */ 262964816Seric 263064816Seric #ifdef NEEDSTRTOL 263164816Seric 263266161Seric #if defined(LIBC_SCCS) && !defined(lint) 263366161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 263466161Seric #endif /* LIBC_SCCS and not lint */ 263566161Seric 263666161Seric #include <limits.h> 263766161Seric 263866161Seric /* 263966161Seric * Convert a string to a long integer. 264066161Seric * 264166161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 264266161Seric * alphabets and digits are each contiguous. 264366161Seric */ 264466161Seric 264564816Seric long 264666161Seric strtol(nptr, endptr, base) 264766161Seric const char *nptr; 264866161Seric char **endptr; 264966161Seric register int base; 265064816Seric { 265166161Seric register const char *s = nptr; 265266161Seric register unsigned long acc; 265366161Seric register int c; 265466161Seric register unsigned long cutoff; 265566161Seric register int neg = 0, any, cutlim; 265664816Seric 265766161Seric /* 265866161Seric * Skip white space and pick up leading +/- sign if any. 265966161Seric * If base is 0, allow 0x for hex and 0 for octal, else 266066161Seric * assume decimal; if base is already 16, allow 0x. 266166161Seric */ 266266161Seric do { 266366161Seric c = *s++; 266466161Seric } while (isspace(c)); 266566161Seric if (c == '-') { 266666161Seric neg = 1; 266766161Seric c = *s++; 266866161Seric } else if (c == '+') 266966161Seric c = *s++; 267066161Seric if ((base == 0 || base == 16) && 267166161Seric c == '0' && (*s == 'x' || *s == 'X')) { 267266161Seric c = s[1]; 267366161Seric s += 2; 267466161Seric base = 16; 267566161Seric } 267666161Seric if (base == 0) 267766161Seric base = c == '0' ? 8 : 10; 267864816Seric 267966161Seric /* 268066161Seric * Compute the cutoff value between legal numbers and illegal 268166161Seric * numbers. That is the largest legal value, divided by the 268266161Seric * base. An input number that is greater than this value, if 268366161Seric * followed by a legal input character, is too big. One that 268466161Seric * is equal to this value may be valid or not; the limit 268566161Seric * between valid and invalid numbers is then based on the last 268666161Seric * digit. For instance, if the range for longs is 268766161Seric * [-2147483648..2147483647] and the input base is 10, 268866161Seric * cutoff will be set to 214748364 and cutlim to either 268966161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 269066161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 269166161Seric * the number is too big, and we will return a range error. 269266161Seric * 269366161Seric * Set any if any `digits' consumed; make it negative to indicate 269466161Seric * overflow. 269566161Seric */ 269666161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 269766161Seric cutlim = cutoff % (unsigned long)base; 269866161Seric cutoff /= (unsigned long)base; 269966161Seric for (acc = 0, any = 0;; c = *s++) { 270066161Seric if (isdigit(c)) 270166161Seric c -= '0'; 270266161Seric else if (isalpha(c)) 270366161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 270466161Seric else 270566161Seric break; 270666161Seric if (c >= base) 270766161Seric break; 270866161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 270966161Seric any = -1; 271066161Seric else { 271166161Seric any = 1; 271266161Seric acc *= base; 271366161Seric acc += c; 271464816Seric } 271564816Seric } 271666161Seric if (any < 0) { 271766161Seric acc = neg ? LONG_MIN : LONG_MAX; 271866161Seric errno = ERANGE; 271966161Seric } else if (neg) 272066161Seric acc = -acc; 272166161Seric if (endptr != 0) 272266161Seric *endptr = (char *)(any ? s - 1 : nptr); 272366161Seric return (acc); 272464816Seric } 272564816Seric 272664816Seric #endif 272764841Seric /* 272864841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 272964841Seric ** 273068508Seric ** Solaris versions at least through 2.3 don't properly deliver a 273164841Seric ** canonical h_name field. This tries to work around it. 273264841Seric */ 273364841Seric 273468508Seric #if defined(SOLARIS) && SOLARIS < 204 273564841Seric 273668482Seric extern int h_errno; 273768482Seric 273864841Seric struct hostent * 273964841Seric solaris_gethostbyname(name) 274065094Seric const char *name; 274164841Seric { 274268508Seric # if SOLARIS == 203 274364841Seric static struct hostent hp; 274464841Seric static char buf[1000]; 274564841Seric extern struct hostent *_switch_gethostbyname_r(); 274664841Seric 274764841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 274864841Seric # else 274965009Seric extern struct hostent *__switch_gethostbyname(); 275065009Seric 275164841Seric return __switch_gethostbyname(name); 275264841Seric # endif 275364841Seric } 275464841Seric 275564841Seric struct hostent * 275664841Seric solaris_gethostbyaddr(addr, len, type) 275765094Seric const char *addr; 275864841Seric int len; 275964841Seric int type; 276064841Seric { 276168508Seric # if SOLARIS == 203 276264841Seric static struct hostent hp; 276364841Seric static char buf[1000]; 276464841Seric extern struct hostent *_switch_gethostbyaddr_r(); 276564841Seric 276664841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 276764841Seric # else 276865009Seric extern struct hostent *__switch_gethostbyaddr(); 276965009Seric 277064841Seric return __switch_gethostbyaddr(addr, len, type); 277164841Seric # endif 277264841Seric } 277364841Seric 277464841Seric #endif 277565214Seric /* 277665214Seric ** NI_PROPVAL -- netinfo property value lookup routine 277765214Seric ** 277865214Seric ** Parameters: 277968508Seric ** keydir -- the Netinfo directory name in which to search 278068508Seric ** for the key. 278168508Seric ** keyprop -- the name of the property in which to find the 278268508Seric ** property we are interested. Defaults to "name". 278368508Seric ** keyval -- the value for which we are really searching. 278468508Seric ** valprop -- the property name for the value in which we 278568508Seric ** are interested. 278668508Seric ** sepchar -- if non-nil, this can be multiple-valued, and 278768508Seric ** we should return a string separated by this 278868508Seric ** character. 278965214Seric ** 279065214Seric ** Returns: 279165214Seric ** NULL -- if: 279265214Seric ** 1. the directory is not found 279365214Seric ** 2. the property name is not found 279465214Seric ** 3. the property contains multiple values 279565214Seric ** 4. some error occured 279665214Seric ** else -- the location of the config file. 279765214Seric ** 279868508Seric ** Example: 279968508Seric ** To search for an alias value, use: 280068508Seric ** ni_propval("/aliases", "name", aliasname, "members", ',') 280168508Seric ** 280265214Seric ** Notes: 280365214Seric ** Caller should free the return value of ni_proval 280465214Seric */ 280565214Seric 280665214Seric #ifdef NETINFO 280765214Seric 280865214Seric # include <netinfo/ni.h> 280965214Seric 281065214Seric # define LOCAL_NETINFO_DOMAIN "." 281165214Seric # define PARENT_NETINFO_DOMAIN ".." 281265214Seric # define MAX_NI_LEVELS 256 281365214Seric 281465214Seric char * 281568508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar) 281668508Seric char *keydir; 281768508Seric char *keyprop; 281868508Seric char *keyval; 281968508Seric char *valprop; 282068508Seric char sepchar; 282165214Seric { 282265820Seric char *propval = NULL; 282365214Seric int i; 282468508Seric int j, alen; 282565214Seric void *ni = NULL; 282665214Seric void *lastni = NULL; 282765214Seric ni_status nis; 282865214Seric ni_id nid; 282965214Seric ni_namelist ninl; 283068508Seric register char *p; 283168508Seric char keybuf[1024]; 283265214Seric 283365214Seric /* 283468508Seric ** Create the full key from the two parts. 283568508Seric ** 283668508Seric ** Note that directory can end with, e.g., "name=" to specify 283768508Seric ** an alternate search property. 283868508Seric */ 283968508Seric 284068508Seric i = strlen(keydir) + strlen(keyval) + 2; 284168508Seric if (keyprop != NULL) 284268508Seric i += strlen(keyprop) + 1; 284368508Seric if (i > sizeof keybuf) 284468508Seric return NULL; 284568508Seric strcpy(keybuf, keydir); 284668508Seric strcat(keybuf, "/"); 284768508Seric if (keyprop != NULL) 284868508Seric { 284968508Seric strcat(keybuf, keyprop); 285068508Seric strcat(keybuf, "="); 285168508Seric } 285268508Seric strcat(keybuf, keyval); 285368508Seric 285468508Seric /* 285565214Seric ** If the passed directory and property name are found 285665214Seric ** in one of netinfo domains we need to search (starting 285765214Seric ** from the local domain moving all the way back to the 285865214Seric ** root domain) set propval to the property's value 285965214Seric ** and return it. 286065214Seric */ 286165214Seric 286265214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 286365214Seric { 286465214Seric if (i == 0) 286565214Seric { 286665214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 286765214Seric } 286865214Seric else 286965214Seric { 287065214Seric if (lastni != NULL) 287165214Seric ni_free(lastni); 287265214Seric lastni = ni; 287365214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 287465214Seric } 287565214Seric 287665214Seric /* 287765214Seric ** Don't bother if we didn't get a handle on a 287865214Seric ** proper domain. This is not necessarily an error. 287965214Seric ** We would get a positive ni_status if, for instance 288065214Seric ** we never found the directory or property and tried 288165214Seric ** to open the parent of the root domain! 288265214Seric */ 288365214Seric 288465214Seric if (nis != 0) 288565214Seric break; 288665214Seric 288765214Seric /* 288865214Seric ** Find the path to the server information. 288965214Seric */ 289065214Seric 289168508Seric if (ni_pathsearch(ni, &nid, keybuf) != 0) 289265214Seric continue; 289365214Seric 289465214Seric /* 289568508Seric ** Find associated value information. 289665214Seric */ 289765214Seric 289868508Seric if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0) 289965214Seric continue; 290065214Seric 290165214Seric /* 290268508Seric ** See if we have an acceptable number of values. 290365214Seric */ 290465214Seric 290568508Seric if (ninl.ni_namelist_len <= 0) 290668508Seric continue; 290768508Seric 290868508Seric if (sepchar == '\0' && ninl.ni_namelist_len > 1) 290965214Seric { 291068508Seric ni_namelist_free(&ninl); 291168508Seric continue; 291265214Seric } 291368508Seric 291468508Seric /* 291568508Seric ** Calculate number of bytes needed and build result 291668508Seric */ 291768508Seric 291868508Seric alen = 1; 291968508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 292068508Seric alen += strlen(ninl.ni_namelist_val[j]) + 1; 292168508Seric propval = p = xalloc(alen); 292268508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 292368508Seric { 292468508Seric strcpy(p, ninl.ni_namelist_val[j]); 292568508Seric p += strlen(p); 292668508Seric *p++ = sepchar; 292768508Seric } 292868508Seric *--p = '\0'; 292968508Seric 293068508Seric ni_namelist_free(&ninl); 293165214Seric } 293265214Seric 293365214Seric /* 293465214Seric ** Clean up. 293565214Seric */ 293665214Seric 293765214Seric if (ni != NULL) 293865214Seric ni_free(ni); 293965214Seric if (lastni != NULL && ni != lastni) 294065214Seric ni_free(lastni); 294165214Seric 294265214Seric return propval; 294365214Seric } 294465214Seric 294565214Seric #endif /* NETINFO */ 294665504Seric /* 294765504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 294865504Seric ** 294965504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 295065504Seric ** syslog succeeds during interrupt handlers. 295165504Seric */ 295265504Seric 295365504Seric #ifdef __hpux 295465504Seric 295565504Seric # define MAXSYSLOGTRIES 100 295665504Seric # undef syslog 295765504Seric 295865504Seric # ifdef __STDC__ 295965504Seric hard_syslog(int pri, char *msg, ...) 296065504Seric # else 296165504Seric hard_syslog(pri, msg, va_alist) 296265504Seric int pri; 296365504Seric char *msg; 296465504Seric va_dcl 296565504Seric # endif 296665504Seric { 296765504Seric int i; 296865504Seric char buf[SYSLOG_BUFSIZE * 2]; 296965504Seric VA_LOCAL_DECL; 297065504Seric 297165504Seric VA_START(msg); 297265504Seric vsprintf(buf, msg, ap); 297365504Seric VA_END; 297465504Seric 297565504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 297665504Seric continue; 297765504Seric } 297865504Seric 297965504Seric #endif 2980