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*68734Seric static char sccsid[] = "@(#)conf.c 8.149 (Berkeley) 04/05/95"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1363937Seric # include "sendmail.h" 1463937Seric # include "pathnames.h" 1514881Seric # include <sys/ioctl.h> 1658082Seric # include <sys/param.h> 17404Seric 18294Seric /* 193309Seric ** CONF.C -- Sendmail Configuration Tables. 20294Seric ** 21294Seric ** Defines the configuration of this installation. 22294Seric ** 231388Seric ** Configuration Variables: 242897Seric ** HdrInfo -- a table describing well-known header fields. 252897Seric ** Each entry has the field name and some flags, 264147Seric ** which are described in sendmail.h. 274093Seric ** 284093Seric ** Notes: 294093Seric ** I have tried to put almost all the reasonable 304093Seric ** configuration information into the configuration 314093Seric ** file read at runtime. My intent is that anything 324093Seric ** here is a function of the version of UNIX you 334093Seric ** are running, or is really static -- for example 344093Seric ** the headers are a superset of widely used 354093Seric ** protocols. If you find yourself playing with 364093Seric ** this file too much, you may be making a mistake! 37294Seric */ 38294Seric 39294Seric 40294Seric 41294Seric 424437Seric /* 432897Seric ** Header info table 443057Seric ** Final (null) entry contains the flags used for any other field. 454147Seric ** 464147Seric ** Not all of these are actually handled specially by sendmail 474147Seric ** at this time. They are included as placeholders, to let 484147Seric ** you know that "someday" I intend to have sendmail do 494147Seric ** something with them. 502897Seric */ 512897Seric 522897Seric struct hdrinfo HdrInfo[] = 532897Seric { 548060Seric /* originator fields, most to least significant */ 5568508Seric "resent-sender", H_FROM|H_RESENT, 5668508Seric "resent-from", H_FROM|H_RESENT, 5768508Seric "resent-reply-to", H_FROM|H_RESENT, 5868508Seric "sender", H_FROM, 5968508Seric "from", H_FROM, 6068508Seric "reply-to", H_FROM, 6168508Seric "full-name", H_ACHECK, 6268508Seric "return-receipt-to", H_FROM|H_RECEIPTTO, 6368508Seric "errors-to", H_FROM|H_ERRORSTO, 6458796Seric 658060Seric /* destination fields */ 6668508Seric "to", H_RCPT, 6768508Seric "resent-to", H_RCPT|H_RESENT, 6868508Seric "cc", H_RCPT, 6968508Seric "resent-cc", H_RCPT|H_RESENT, 7068508Seric "bcc", H_RCPT|H_STRIPVAL, 7168508Seric "resent-bcc", H_RCPT|H_STRIPVAL|H_RESENT, 7268508Seric "apparently-to", H_RCPT, 7358796Seric 748060Seric /* message identification and control */ 7568508Seric "message-id", 0, 7668508Seric "resent-message-id", H_RESENT, 7768508Seric "message", H_EOH, 7868508Seric "text", H_EOH, 7958796Seric 8011417Seric /* date fields */ 8168508Seric "date", 0, 8268508Seric "resent-date", H_RESENT, 8358796Seric 848060Seric /* trace fields */ 8568508Seric "received", H_TRACE|H_FORCE, 8668508Seric "x400-received", H_TRACE|H_FORCE, 8768508Seric "via", H_TRACE|H_FORCE, 8868508Seric "mail-from", H_TRACE|H_FORCE, 898060Seric 9058796Seric /* miscellaneous fields */ 9168508Seric "comments", H_FORCE, 9268508Seric "return-path", H_FORCE|H_ACHECK, 9368508Seric "content-transfer-encoding", H_CTE, 9468508Seric "content-type", H_CTYPE, 9568706Seric "content-length", H_ACHECK, 9658796Seric 979055Seric NULL, 0, 982897Seric }; 994166Seric 1004166Seric 1014166Seric 1024282Seric /* 1034282Seric ** Location of system files/databases/etc. 1044282Seric */ 1054282Seric 10658082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1079039Seric 1089064Seric 1099064Seric 1109039Seric /* 11158082Seric ** Privacy values 11258082Seric */ 11358082Seric 11458082Seric struct prival PrivacyValues[] = 11558082Seric { 11658082Seric "public", PRIV_PUBLIC, 11758082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11858114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 11958082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 12058082Seric "noexpn", PRIV_NOEXPN, 12158082Seric "novrfy", PRIV_NOVRFY, 12264333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12364333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12458789Seric "authwarnings", PRIV_AUTHWARNINGS, 12566783Seric "noreceipts", PRIV_NORECEIPTS, 12658082Seric "goaway", PRIV_GOAWAY, 12758789Seric NULL, 0, 12858082Seric }; 12958082Seric 13058082Seric 13158082Seric 13258082Seric /* 13324943Seric ** Miscellaneous stuff. 1349039Seric */ 1359039Seric 13624943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13724943Seric /* 13824943Seric ** SETDEFAULTS -- set default values 13924943Seric ** 14024943Seric ** Because of the way freezing is done, these must be initialized 14124943Seric ** using direct code. 14224943Seric ** 14324943Seric ** Parameters: 14458734Seric ** e -- the default envelope. 14524943Seric ** 14624943Seric ** Returns: 14724943Seric ** none. 14824943Seric ** 14924943Seric ** Side Effects: 15024943Seric ** Initializes a bunch of global variables to their 15124943Seric ** default values. 15224943Seric */ 15324943Seric 15458737Seric #define DAYS * 24 * 60 * 60 15558737Seric 15668508Seric void 15758734Seric setdefaults(e) 15858734Seric register ENVELOPE *e; 15924943Seric { 16068508Seric int i; 16168508Seric extern void inittimeouts(); 16268508Seric extern void setdefuser(); 16368508Seric extern void setupmaps(); 16468508Seric extern void setupmailers(); 16568508Seric 16657438Seric SpaceSub = ' '; /* option B */ 16757438Seric QueueLA = 8; /* option x */ 16857438Seric RefuseLA = 12; /* option X */ 16957438Seric WkRecipFact = 30000L; /* option y */ 17057438Seric WkClassFact = 1800L; /* option z */ 17157438Seric WkTimeFact = 90000L; /* option Z */ 17257438Seric QueueFactor = WkRecipFact * 20; /* option q */ 17363787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 17457438Seric /* option F */ 17557438Seric DefUid = 1; /* option u */ 17657438Seric DefGid = 1; /* option g */ 17757438Seric CheckpointInterval = 10; /* option C */ 17857438Seric MaxHopCount = 25; /* option h */ 17958734Seric e->e_sendmode = SM_FORK; /* option d */ 18058734Seric e->e_errormode = EM_PRINT; /* option e */ 18168508Seric SevenBitInput = FALSE; /* option 7 */ 18257438Seric MaxMciCache = 1; /* option k */ 18357438Seric MciCacheTimeout = 300; /* option K */ 18457438Seric LogLevel = 9; /* option L */ 18568508Seric inittimeouts(NULL); /* option r */ 18658853Seric PrivacyFlags = 0; /* option p */ 18768508Seric MimeMode = MM_CVTMIME|MM_PASS8BIT; /* option 8 */ 18868508Seric for (i = 0; i < MAXTOCLASS; i++) 18968508Seric { 19068508Seric TimeOuts.to_q_return[i] = 5 DAYS; /* option T */ 19168508Seric TimeOuts.to_q_warning[i] = 0; /* option T */ 19268508Seric } 19368508Seric ServiceSwitchFile = "/etc/service.switch"; 19440973Sbostic setdefuser(); 19553654Seric setupmaps(); 19657402Seric setupmailers(); 19724943Seric } 198294Seric 19940973Sbostic 2004326Seric /* 20140973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20240973Sbostic */ 20340973Sbostic 20468508Seric void 20540973Sbostic setdefuser() 20640973Sbostic { 20740973Sbostic struct passwd *defpwent; 20857386Seric static char defuserbuf[40]; 20940973Sbostic 21057386Seric DefUser = defuserbuf; 21168693Seric if ((defpwent = sm_getpwuid(DefUid)) != NULL) 21257386Seric strcpy(defuserbuf, defpwent->pw_name); 21340973Sbostic else 21457386Seric strcpy(defuserbuf, "nobody"); 21540973Sbostic } 21653654Seric /* 21756836Seric ** HOST_MAP_INIT -- initialize host class structures 21856836Seric */ 21956836Seric 22068508Seric bool host_map_init __P((MAP *map, char *args)); 22168508Seric 22256836Seric bool 22360219Seric host_map_init(map, args) 22456836Seric MAP *map; 22556836Seric char *args; 22656836Seric { 22756836Seric register char *p = args; 22856836Seric 22956836Seric for (;;) 23056836Seric { 23158050Seric while (isascii(*p) && isspace(*p)) 23256836Seric p++; 23356836Seric if (*p != '-') 23456836Seric break; 23556836Seric switch (*++p) 23656836Seric { 23756836Seric case 'a': 23856836Seric map->map_app = ++p; 23956836Seric break; 24056836Seric } 24158050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 24256836Seric p++; 24356836Seric if (*p != '\0') 24456836Seric *p++ = '\0'; 24556836Seric } 24656836Seric if (map->map_app != NULL) 24756836Seric map->map_app = newstr(map->map_app); 24856836Seric return TRUE; 24956836Seric } 25057402Seric /* 25157402Seric ** SETUPMAILERS -- initialize default mailers 25257402Seric */ 25356836Seric 25468508Seric void 25557402Seric setupmailers() 25657402Seric { 25757402Seric char buf[100]; 25868508Seric extern void makemailer(); 25957402Seric 26068508Seric strcpy(buf, "prog, P=/bin/sh, F=lsoD, A=sh -c $u"); 26157403Seric makemailer(buf); 26257403Seric 26368508Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEou, A=FILE"); 26457402Seric makemailer(buf); 26557402Seric 26657402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 26757402Seric makemailer(buf); 26857402Seric } 26956836Seric /* 27060207Seric ** SETUPMAPS -- set up map classes 27160207Seric */ 27260207Seric 27360207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 27460207Seric { \ 27560207Seric extern bool parse __P((MAP *, char *)); \ 27660207Seric extern bool open __P((MAP *, int)); \ 27760207Seric extern void close __P((MAP *)); \ 27860207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 27960207Seric extern void store __P((MAP *, char *, char *)); \ 28060207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 28160207Seric s->s_mapclass.map_cname = name; \ 28260207Seric s->s_mapclass.map_ext = ext; \ 28360207Seric s->s_mapclass.map_cflags = flags; \ 28460207Seric s->s_mapclass.map_parse = parse; \ 28560207Seric s->s_mapclass.map_open = open; \ 28660207Seric s->s_mapclass.map_close = close; \ 28760207Seric s->s_mapclass.map_lookup = lookup; \ 28860207Seric s->s_mapclass.map_store = store; \ 28960207Seric } 29060207Seric 29168508Seric void 29260207Seric setupmaps() 29360207Seric { 29460207Seric register STAB *s; 29560207Seric 29660207Seric #ifdef NEWDB 29760207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29860207Seric map_parseargs, hash_map_open, db_map_close, 29960207Seric db_map_lookup, db_map_store); 30068508Seric 30160207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 30260207Seric map_parseargs, bt_map_open, db_map_close, 30360207Seric db_map_lookup, db_map_store); 30460207Seric #endif 30560207Seric 30660207Seric #ifdef NDBM 30760207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 30860207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 30960207Seric ndbm_map_lookup, ndbm_map_store); 31060207Seric #endif 31160207Seric 31260207Seric #ifdef NIS 31360207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 31468508Seric map_parseargs, nis_map_open, null_map_close, 31568508Seric nis_map_lookup, null_map_store); 31660207Seric #endif 31760207Seric 31868508Seric #ifdef NISPLUS 31968508Seric MAPDEF("nisplus", NULL, MCF_ALIASOK, 32068508Seric map_parseargs, nisplus_map_open, null_map_close, 32168508Seric nisplus_map_lookup, null_map_store); 32268508Seric #endif 32368508Seric 32468508Seric #ifdef HESIOD 32568508Seric MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY, 32668508Seric map_parseargs, null_map_open, null_map_close, 32768508Seric hes_map_lookup, null_map_store); 32868508Seric #endif 32968508Seric 33068508Seric #ifdef NETINFO 33168508Seric MAPDEF("netinfo", NULL, MCF_ALIASOK, 33268508Seric map_parseargs, ni_map_open, null_map_close, 33368508Seric ni_map_lookup, null_map_store); 33468508Seric #endif 33568508Seric 33668508Seric #if 0 33768508Seric MAPDEF("dns", NULL, 0, 33868508Seric dns_map_init, null_map_open, null_map_close, 33968508Seric dns_map_lookup, null_map_store); 34068508Seric #endif 34168508Seric 34268508Seric #if NAMED_BIND 34368508Seric /* best MX DNS lookup */ 34468508Seric MAPDEF("bestmx", NULL, MCF_OPTFILE, 34568508Seric map_parseargs, null_map_open, null_map_close, 34668508Seric bestmx_map_lookup, null_map_store); 34768508Seric #endif 34868508Seric 34968508Seric MAPDEF("host", NULL, 0, 35068508Seric host_map_init, null_map_open, null_map_close, 35168508Seric host_map_lookup, null_map_store); 35268508Seric 35368508Seric MAPDEF("text", NULL, MCF_ALIASOK, 35468508Seric map_parseargs, text_map_open, null_map_close, 35568508Seric text_map_lookup, null_map_store); 35668508Seric 35760207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 35868508Seric map_parseargs, stab_map_open, null_map_close, 35960207Seric stab_map_lookup, stab_map_store); 36060207Seric 36160207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 36260207Seric map_parseargs, impl_map_open, impl_map_close, 36360207Seric impl_map_lookup, impl_map_store); 36460207Seric 36568508Seric /* access to system passwd file */ 36668508Seric MAPDEF("user", NULL, MCF_OPTFILE, 36768508Seric map_parseargs, user_map_open, null_map_close, 36868508Seric user_map_lookup, null_map_store); 36960207Seric 37060207Seric /* dequote map */ 37160207Seric MAPDEF("dequote", NULL, 0, 37260207Seric dequote_init, null_map_open, null_map_close, 37360207Seric dequote_map, null_map_store); 37460207Seric 37568521Seric #ifdef USERDB 37660207Seric /* user database */ 37768521Seric MAPDEF("userdb", ".db", 0, 37868521Seric map_parseargs, null_map_open, null_map_close, 37960207Seric udb_map_lookup, null_map_store); 38060207Seric #endif 38168508Seric 38268508Seric /* sequenced maps */ 38368508Seric MAPDEF("sequence", NULL, MCF_ALIASOK, 38468508Seric seq_map_parse, null_map_open, null_map_close, 38568508Seric seq_map_lookup, seq_map_store); 38668508Seric 38768508Seric /* switched interface to sequenced maps */ 38868508Seric MAPDEF("switch", NULL, MCF_ALIASOK, 38968508Seric map_parseargs, switch_map_open, null_map_close, 39068508Seric seq_map_lookup, seq_map_store); 39160207Seric } 39260207Seric 39360207Seric #undef MAPDEF 39460207Seric /* 39568508Seric ** INITHOSTMAPS -- initial host-dependent maps 39668508Seric ** 39768508Seric ** This should act as an interface to any local service switch 39868508Seric ** provided by the host operating system. 39968508Seric ** 40068508Seric ** Parameters: 40168508Seric ** none 40268508Seric ** 40368508Seric ** Returns: 40468508Seric ** none 40568508Seric ** 40668508Seric ** Side Effects: 40768508Seric ** Should define maps "host" and "users" as necessary 40868508Seric ** for this OS. If they are not defined, they will get 40968508Seric ** a default value later. It should check to make sure 41068508Seric ** they are not defined first, since it's possible that 41168508Seric ** the config file has provided an override. 41268508Seric */ 41368508Seric 41468508Seric void 41568508Seric inithostmaps() 41668508Seric { 41768508Seric register int i; 41868508Seric int nmaps; 41968508Seric char *maptype[MAXMAPSTACK]; 42068508Seric short mapreturn[MAXMAPACTIONS]; 42168508Seric char buf[MAXLINE]; 42268508Seric 42368508Seric /* 42468508Seric ** Set up default hosts maps. 42568508Seric */ 42668508Seric 42768508Seric #if 0 42868508Seric nmaps = switch_map_find("hosts", maptype, mapreturn); 42968508Seric for (i = 0; i < nmaps; i++) 43068508Seric { 43168508Seric if (strcmp(maptype[i], "files") == 0 && 43268508Seric stab("hosts.files", ST_MAP, ST_FIND) == NULL) 43368508Seric { 43468508Seric strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts"); 43568508Seric makemapentry(buf); 43668508Seric } 43768508Seric #if NAMED_BIND 43868508Seric else if (strcmp(maptype[i], "dns") == 0 && 43968508Seric stab("hosts.dns", ST_MAP, ST_FIND) == NULL) 44068508Seric { 44168508Seric strcpy(buf, "hosts.dns dns A"); 44268508Seric makemapentry(buf); 44368508Seric } 44468508Seric #endif 44568508Seric #ifdef NISPLUS 44668508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 44768508Seric stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL) 44868508Seric { 44968508Seric strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir"); 45068508Seric makemapentry(buf); 45168508Seric } 45268508Seric #endif 45368508Seric #ifdef NIS 45468508Seric else if (strcmp(maptype[i], "nis") == 0 && 45568508Seric stab("hosts.nis", ST_MAP, ST_FIND) == NULL) 45668508Seric { 45768508Seric strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname"); 45868508Seric makemapentry(buf); 45968508Seric } 46068508Seric #endif 46168508Seric } 46268508Seric #endif 46368508Seric 46468508Seric /* 46568508Seric ** Make sure we have a host map. 46668508Seric */ 46768508Seric 46868508Seric if (stab("host", ST_MAP, ST_FIND) == NULL) 46968508Seric { 47068508Seric /* user didn't initialize: set up host map */ 47168508Seric strcpy(buf, "host host"); 47268508Seric #if NAMED_BIND 47368508Seric if (ConfigLevel >= 2) 47468508Seric strcat(buf, " -a."); 47568508Seric #endif 47668508Seric makemapentry(buf); 47768508Seric } 47868508Seric 47968508Seric /* 48068508Seric ** Set up default aliases maps 48168508Seric */ 48268508Seric 48368508Seric nmaps = switch_map_find("aliases", maptype, mapreturn); 48468508Seric for (i = 0; i < nmaps; i++) 48568508Seric { 48668508Seric if (strcmp(maptype[i], "files") == 0 && 48768508Seric stab("aliases.files", ST_MAP, ST_FIND) == NULL) 48868508Seric { 48968508Seric strcpy(buf, "aliases.files implicit /etc/aliases"); 49068508Seric makemapentry(buf); 49168508Seric } 49268508Seric #ifdef NISPLUS 49368508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 49468508Seric stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL) 49568508Seric { 49668508Seric strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir"); 49768508Seric makemapentry(buf); 49868508Seric } 49968508Seric #endif 50068508Seric #ifdef NIS 50168508Seric else if (strcmp(maptype[i], "nis") == 0 && 50268508Seric stab("aliases.nis", ST_MAP, ST_FIND) == NULL) 50368508Seric { 50468508Seric strcpy(buf, "aliases.nis nis -d mail.aliases"); 50568508Seric makemapentry(buf); 50668508Seric } 50768508Seric #endif 50868508Seric } 50968508Seric if (stab("aliases", ST_MAP, ST_FIND) == NULL) 51068508Seric { 51168508Seric strcpy(buf, "aliases switch aliases"); 51268508Seric makemapentry(buf); 51368508Seric } 51468508Seric strcpy(buf, "switch:aliases"); 51568508Seric setalias(buf); 51668508Seric 51768508Seric #if 0 /* "user" map class is a better choice */ 51868508Seric /* 51968508Seric ** Set up default users maps. 52068508Seric */ 52168508Seric 52268508Seric nmaps = switch_map_find("passwd", maptype, mapreturn); 52368508Seric for (i = 0; i < nmaps; i++) 52468508Seric { 52568508Seric if (strcmp(maptype[i], "files") == 0 && 52668508Seric stab("users.files", ST_MAP, ST_FIND) == NULL) 52768508Seric { 52868508Seric strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd"); 52968508Seric makemapentry(buf); 53068508Seric } 53168508Seric #ifdef NISPLUS 53268508Seric else if (strcmp(maptype[i], "nisplus") == 0 && 53368508Seric stab("users.nisplus", ST_MAP, ST_FIND) == NULL) 53468508Seric { 53568508Seric strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir"); 53668508Seric makemapentry(buf); 53768508Seric } 53868508Seric #endif 53968508Seric #ifdef NIS 54068508Seric else if (strcmp(maptype[i], "nis") == 0 && 54168508Seric stab("users.nis", ST_MAP, ST_FIND) == NULL) 54268508Seric { 54368508Seric strcpy(buf, "users.nis nis -m -d passwd.byname"); 54468508Seric makemapentry(buf); 54568508Seric } 54668508Seric #endif 54768508Seric #ifdef HESIOD 54868508Seric else if (strcmp(maptype[i], "hesiod") == 0) && 54968508Seric stab("users.hesiod", ST_MAP, ST_FIND) == NULL) 55068508Seric { 55168508Seric strcpy(buf, "users.hesiod hesiod"); 55268508Seric makemapentry(buf); 55368508Seric } 55468508Seric #endif 55568508Seric } 55668508Seric if (stab("users", ST_MAP, ST_FIND) == NULL) 55768508Seric { 55868508Seric strcpy(buf, "users switch -m passwd"); 55968508Seric makemapentry(buf); 56068508Seric } 56168508Seric #endif 56268508Seric } 56368508Seric /* 56468508Seric ** SWITCH_MAP_FIND -- find the list of types associated with a map 56568508Seric ** 56668508Seric ** This is the system-dependent interface to the service switch. 56768508Seric ** 56868508Seric ** Parameters: 56968508Seric ** service -- the name of the service of interest. 57068508Seric ** maptype -- an out-array of strings containing the types 57168508Seric ** of access to use for this service. There can 57268508Seric ** be at most MAXMAPSTACK types for a single service. 57368508Seric ** mapreturn -- an out-array of return information bitmaps 57468508Seric ** for the map. 57568508Seric ** 57668508Seric ** Returns: 57768508Seric ** The number of map types filled in, or -1 for failure. 57868508Seric */ 57968508Seric 58068508Seric #ifdef SOLARIS 58168508Seric # include <nsswitch.h> 58268508Seric #endif 58368508Seric 58468508Seric #if defined(ultrix) || defined(__osf__) 58568508Seric # include <sys/svcinfo.h> 58668508Seric #endif 58768508Seric 58868508Seric int 58968508Seric switch_map_find(service, maptype, mapreturn) 59068508Seric char *service; 59168508Seric char *maptype[MAXMAPSTACK]; 59268508Seric short mapreturn[MAXMAPACTIONS]; 59368508Seric { 59468508Seric register FILE *fp; 59568508Seric int svcno; 59668508Seric static char buf[MAXLINE]; 59768508Seric 59868508Seric #ifdef SOLARIS 59968508Seric struct __nsw_switchconfig *nsw_conf; 60068508Seric enum __nsw_parse_err pserr; 60168508Seric struct __nsw_lookup *lk; 60268508Seric int nsw_rc; 60368508Seric static struct __nsw_lookup lkp0 = 60468508Seric { "files", {1, 0, 0, 0}, NULL, NULL }; 60568508Seric static struct __nsw_switchconfig lkp_default = 60668508Seric { 0, "sendmail", 3, &lkp0 }; 60768508Seric 60868508Seric if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL) 60968508Seric lk = lkp_default.lookups; 61068508Seric else 61168508Seric lk = nsw_conf->lookups; 61268508Seric svcno = 0; 61368508Seric while (lk != NULL) 61468508Seric { 61568508Seric maptype[svcno] = lk->service_name; 61668508Seric if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN) 61768508Seric mapreturn[MA_NOTFOUND] |= 1 << svcno; 61868508Seric if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN) 61968508Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 62068508Seric if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN) 62168508Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 62268508Seric svcno++; 62368508Seric lk = lk->next; 62468508Seric } 62568508Seric return svcno; 62668508Seric #endif 62768508Seric 62868508Seric #if defined(ultrix) || defined(__osf__) 62968508Seric struct svcinfo *svcinfo; 63068508Seric int svc; 63168508Seric 63268508Seric svcinfo = getsvc(); 63368508Seric if (svcinfo == NULL) 63468508Seric goto punt; 63568508Seric if (strcmp(service, "hosts") == 0) 63668508Seric svc = SVC_HOSTS; 63768508Seric else if (strcmp(service, "aliases") == 0) 63868508Seric svc = SVC_ALIASES; 63968508Seric else if (strcmp(service, "passwd") == 0) 64068508Seric svc = SVC_PASSWD; 64168508Seric else 64268508Seric return -1; 64368508Seric for (svcno = 0; svcno < SVC_PATHSIZE; svcno++) 64468508Seric { 64568508Seric switch (svcinfo->svcpath[svc][svcno]) 64668508Seric { 64768508Seric case SVC_LOCAL: 64868508Seric maptype[svcno] = "files"; 64968508Seric break; 65068508Seric 65168508Seric case SVC_YP: 65268508Seric maptype[svcno] = "nis"; 65368508Seric break; 65468508Seric 65568508Seric case SVC_BIND: 65668508Seric maptype[svcno] = "dns"; 65768508Seric break; 65868508Seric 65968508Seric #ifdef SVC_HESIOD 66068508Seric case SVC_HESIOD: 66168508Seric maptype[svcno] = "hesiod"; 66268508Seric break; 66368508Seric #endif 66468508Seric 66568508Seric case SVC_LAST: 66668508Seric return svcno; 66768508Seric } 66868508Seric } 66968508Seric return svcno; 67068508Seric #endif 67168508Seric 67268508Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__) 67368508Seric /* 67468508Seric ** Fall-back mechanism. 67568508Seric */ 67668508Seric 67768508Seric svcno = 0; 67868508Seric fp = fopen(ServiceSwitchFile, "r"); 67968508Seric if (fp != NULL) 68068508Seric { 68168508Seric while (fgets(buf, sizeof buf, fp) != NULL) 68268508Seric { 68368508Seric register char *p; 68468508Seric 68568508Seric p = strpbrk(buf, "#\n"); 68668508Seric if (p != NULL) 68768508Seric *p = '\0'; 68868508Seric p = strpbrk(buf, " \t"); 68968508Seric if (p != NULL) 69068508Seric *p++ = '\0'; 69168508Seric if (strcmp(buf, service) != 0) 69268508Seric continue; 69368508Seric 69468508Seric /* got the right service -- extract data */ 69568508Seric do 69668508Seric { 69768508Seric while (isspace(*p)) 69868508Seric p++; 69968508Seric if (*p == '\0') 70068508Seric break; 70168508Seric maptype[svcno++] = p; 70268508Seric p = strpbrk(p, " \t"); 70368508Seric if (p != NULL) 70468508Seric *p++ = '\0'; 70568508Seric } while (p != NULL); 70668508Seric break; 70768508Seric } 70868508Seric fclose(fp); 70968508Seric return svcno; 71068508Seric } 71168508Seric #endif 71268508Seric 71368508Seric /* if the service file doesn't work, use an absolute fallback */ 71468508Seric punt: 71568508Seric if (strcmp(service, "aliases") == 0) 71668508Seric { 71768628Seric maptype[svcno++] = "files"; 71868628Seric #ifdef AUTO_NIS_ALIASES 71968628Seric # ifdef NISPLUS 72068628Seric maptype[svcno++] = "nisplus"; 72168628Seric # endif 72268628Seric # ifdef NIS 72368628Seric maptype[svcno++] = "nis"; 72468628Seric # endif 72568628Seric #endif 72668628Seric return svcno; 72768508Seric } 72868508Seric if (strcmp(service, "hosts") == 0) 72968508Seric { 73068508Seric # if NAMED_BIND 73168508Seric maptype[svcno++] = "dns"; 73268508Seric # else 73368508Seric # if defined(sun) && !defined(BSD) && !defined(SOLARIS) 73468508Seric /* SunOS */ 73568508Seric maptype[svcno++] = "nis"; 73668508Seric # endif 73768508Seric # endif 73868508Seric maptype[svcno++] = "files"; 73968508Seric return svcno; 74068508Seric } 74168508Seric return -1; 74268508Seric } 74368508Seric /* 7449369Seric ** USERNAME -- return the user id of the logged in user. 7459369Seric ** 7469369Seric ** Parameters: 7479369Seric ** none. 7489369Seric ** 7499369Seric ** Returns: 7509369Seric ** The login name of the logged in user. 7519369Seric ** 7529369Seric ** Side Effects: 7539369Seric ** none. 7549369Seric ** 7559369Seric ** Notes: 7569369Seric ** The return value is statically allocated. 7579369Seric */ 7589369Seric 7599369Seric char * 7609369Seric username() 7619369Seric { 76217469Seric static char *myname = NULL; 7639369Seric extern char *getlogin(); 76419904Smiriam register struct passwd *pw; 7659369Seric 76617469Seric /* cache the result */ 76717469Seric if (myname == NULL) 76817469Seric { 76917469Seric myname = getlogin(); 77017469Seric if (myname == NULL || myname[0] == '\0') 77117469Seric { 77268693Seric pw = sm_getpwuid(RealUid); 77317469Seric if (pw != NULL) 77440993Sbostic myname = newstr(pw->pw_name); 77517469Seric } 77619904Smiriam else 77719904Smiriam { 77863787Seric uid_t uid = RealUid; 77919873Smiriam 78040993Sbostic myname = newstr(myname); 78168693Seric if ((pw = sm_getpwnam(myname)) == NULL || 78258736Seric (uid != 0 && uid != pw->pw_uid)) 78319904Smiriam { 78468693Seric pw = sm_getpwuid(uid); 78524945Seric if (pw != NULL) 78640993Sbostic myname = newstr(pw->pw_name); 78719873Smiriam } 78819873Smiriam } 78917469Seric if (myname == NULL || myname[0] == '\0') 79017469Seric { 79158151Seric syserr("554 Who are you?"); 79217469Seric myname = "postmaster"; 79317469Seric } 79417469Seric } 79517469Seric 79617469Seric return (myname); 7979369Seric } 7989369Seric /* 7994190Seric ** TTYPATH -- Get the path of the user's tty 800294Seric ** 801294Seric ** Returns the pathname of the user's tty. Returns NULL if 802294Seric ** the user is not logged in or if s/he has write permission 803294Seric ** denied. 804294Seric ** 805294Seric ** Parameters: 806294Seric ** none 807294Seric ** 808294Seric ** Returns: 809294Seric ** pathname of the user's tty. 810294Seric ** NULL if not logged in or write permission denied. 811294Seric ** 812294Seric ** Side Effects: 813294Seric ** none. 814294Seric ** 815294Seric ** WARNING: 816294Seric ** Return value is in a local buffer. 817294Seric ** 818294Seric ** Called By: 819294Seric ** savemail 820294Seric */ 821294Seric 822294Seric char * 823294Seric ttypath() 824294Seric { 825294Seric struct stat stbuf; 826294Seric register char *pathn; 827294Seric extern char *ttyname(); 8284081Seric extern char *getlogin(); 829294Seric 830294Seric /* compute the pathname of the controlling tty */ 8319369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 8329369Seric (pathn = ttyname(0)) == NULL) 833294Seric { 834294Seric errno = 0; 835294Seric return (NULL); 836294Seric } 837294Seric 838294Seric /* see if we have write permission */ 8392967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 840294Seric { 841294Seric errno = 0; 842294Seric return (NULL); 843294Seric } 844294Seric 845294Seric /* see if the user is logged in */ 846294Seric if (getlogin() == NULL) 847294Seric return (NULL); 848294Seric 849294Seric /* looks good */ 850294Seric return (pathn); 851294Seric } 8522967Seric /* 8532967Seric ** CHECKCOMPAT -- check for From and To person compatible. 8542967Seric ** 8552967Seric ** This routine can be supplied on a per-installation basis 8562967Seric ** to determine whether a person is allowed to send a message. 8572967Seric ** This allows restriction of certain types of internet 8582967Seric ** forwarding or registration of users. 8592967Seric ** 8602967Seric ** If the hosts are found to be incompatible, an error 86157454Seric ** message should be given using "usrerr" and 0 should 8622967Seric ** be returned. 8632967Seric ** 86468559Seric ** EF_NO_BODY_RETN can be set in e->e_flags to suppress the 86568559Seric ** body during the return-to-sender function; this should be done 86668559Seric ** on huge messages. This bit may already be set by the ESMTP 86768559Seric ** protocol. 8684288Seric ** 8692967Seric ** Parameters: 8702967Seric ** to -- the person being sent to. 8712967Seric ** 8722967Seric ** Returns: 87357459Seric ** an exit status 8742967Seric ** 8752967Seric ** Side Effects: 8762967Seric ** none (unless you include the usrerr stuff) 8772967Seric */ 8782967Seric 87968508Seric int 88055012Seric checkcompat(to, e) 8812967Seric register ADDRESS *to; 88255012Seric register ENVELOPE *e; 8832967Seric { 88412133Seric # ifdef lint 88512133Seric if (to == NULL) 88612133Seric to++; 88763834Seric # endif /* lint */ 88864402Seric 88964402Seric if (tTd(49, 1)) 89064402Seric printf("checkcompat(to=%s, from=%s)\n", 89164402Seric to->q_paddr, e->e_from.q_paddr); 89264402Seric 89310698Seric # ifdef EXAMPLE_CODE 89410698Seric /* this code is intended as an example only */ 8954437Seric register STAB *s; 8964437Seric 8974437Seric s = stab("arpa", ST_MAILER, ST_FIND); 89868508Seric if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 && 8999369Seric to->q_mailer == s->s_mailer) 9004437Seric { 90158151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 90268559Seric /* e->e_flags |= EF_NO_BODY_RETN; to supress body on return */ 90357459Seric return (EX_UNAVAILABLE); 9044437Seric } 90556795Seric # endif /* EXAMPLE_CODE */ 90657459Seric return (EX_OK); 9072967Seric } 9089369Seric /* 90964035Seric ** SETSIGNAL -- set a signal handler 91064035Seric ** 91164035Seric ** This is essentially old BSD "signal(3)". 91264035Seric */ 91364035Seric 91464561Seric sigfunc_t 91564035Seric setsignal(sig, handler) 91664035Seric int sig; 91764561Seric sigfunc_t handler; 91864035Seric { 91964380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 92064035Seric return signal(sig, handler); 92164035Seric #else 92264035Seric struct sigaction n, o; 92364035Seric 92464035Seric bzero(&n, sizeof n); 92564035Seric n.sa_handler = handler; 92668508Seric # ifdef SA_RESTART 92768508Seric n.sa_flags = SA_RESTART; 92868508Seric # endif 92964035Seric if (sigaction(sig, &n, &o) < 0) 93064035Seric return SIG_ERR; 93164035Seric return o.sa_handler; 93264035Seric #endif 93364035Seric } 93464035Seric /* 9359369Seric ** HOLDSIGS -- arrange to hold all signals 9369369Seric ** 9379369Seric ** Parameters: 9389369Seric ** none. 9399369Seric ** 9409369Seric ** Returns: 9419369Seric ** none. 9429369Seric ** 9439369Seric ** Side Effects: 9449369Seric ** Arranges that signals are held. 9459369Seric */ 9469369Seric 94768508Seric void 9489369Seric holdsigs() 9499369Seric { 9509369Seric } 9519369Seric /* 9529369Seric ** RLSESIGS -- arrange to release all signals 9539369Seric ** 9549369Seric ** This undoes the effect of holdsigs. 9559369Seric ** 9569369Seric ** Parameters: 9579369Seric ** none. 9589369Seric ** 9599369Seric ** Returns: 9609369Seric ** none. 9619369Seric ** 9629369Seric ** Side Effects: 9639369Seric ** Arranges that signals are released. 9649369Seric */ 9659369Seric 96668508Seric void 9679369Seric rlsesigs() 9689369Seric { 9699369Seric } 97014872Seric /* 97164705Seric ** INIT_MD -- do machine dependent initializations 97264705Seric ** 97364705Seric ** Systems that have global modes that should be set should do 97464705Seric ** them here rather than in main. 97564705Seric */ 97664705Seric 97764705Seric #ifdef _AUX_SOURCE 97864705Seric # include <compat.h> 97964705Seric #endif 98064705Seric 98168508Seric void 98264825Seric init_md(argc, argv) 98364825Seric int argc; 98464825Seric char **argv; 98564705Seric { 98664705Seric #ifdef _AUX_SOURCE 98764705Seric setcompat(getcompat() | COMPAT_BSDPROT); 98864705Seric #endif 98968508Seric 99068508Seric #ifdef VENDOR_DEFAULT 99168508Seric VendorCode = VENDOR_DEFAULT; 99268508Seric #else 99368508Seric VendorCode = VENDOR_BERKELEY; 99468508Seric #endif 99564705Seric } 99664705Seric /* 99768508Seric ** INIT_VENDOR_MACROS -- vendor-dependent macro initializations 99868508Seric ** 99968508Seric ** Called once, on startup. 100068508Seric ** 100168508Seric ** Parameters: 100268508Seric ** e -- the global envelope. 100368508Seric ** 100468508Seric ** Returns: 100568508Seric ** none. 100668508Seric ** 100768508Seric ** Side Effects: 100868508Seric ** vendor-dependent. 100968508Seric */ 101068508Seric 101168508Seric void 101268508Seric init_vendor_macros(e) 101368508Seric register ENVELOPE *e; 101468508Seric { 101568508Seric } 101668508Seric /* 101714872Seric ** GETLA -- get the current load average 101814872Seric ** 101914881Seric ** This code stolen from la.c. 102014881Seric ** 102114872Seric ** Parameters: 102214872Seric ** none. 102314872Seric ** 102414872Seric ** Returns: 102514872Seric ** The current load average as an integer. 102614872Seric ** 102714872Seric ** Side Effects: 102814872Seric ** none. 102914872Seric */ 103014872Seric 103151920Seric /* try to guess what style of load average we have */ 103251920Seric #define LA_ZERO 1 /* always return load average as zero */ 103364360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 103451920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 103551920Seric #define LA_SUBR 4 /* call getloadavg */ 103664295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 103764360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 103866301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 103914872Seric 104063962Seric /* do guesses based on general OS type */ 104151920Seric #ifndef LA_TYPE 104263962Seric # define LA_TYPE LA_ZERO 104351920Seric #endif 104451920Seric 104564360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 104651920Seric 104714872Seric #include <nlist.h> 104814872Seric 104968508Seric #ifdef IRIX64 105068508Seric # define nlist nlist64 105168508Seric #endif 105268508Seric 105351920Seric #ifndef LA_AVENRUN 105463962Seric # ifdef SYSTEM5 105563962Seric # define LA_AVENRUN "avenrun" 105663962Seric # else 105763962Seric # define LA_AVENRUN "_avenrun" 105863962Seric # endif 105951920Seric #endif 106051920Seric 106151920Seric /* _PATH_UNIX should be defined in <paths.h> */ 106251920Seric #ifndef _PATH_UNIX 106363962Seric # if defined(SYSTEM5) 106463962Seric # define _PATH_UNIX "/unix" 106563962Seric # else 106663962Seric # define _PATH_UNIX "/vmunix" 106763962Seric # endif 106851920Seric #endif 106951920Seric 107014872Seric struct nlist Nl[] = 107114872Seric { 107251920Seric { LA_AVENRUN }, 107314872Seric #define X_AVENRUN 0 107414872Seric { 0 }, 107514872Seric }; 107614872Seric 107760583Seric #ifndef FSHIFT 107860583Seric # if defined(unixpc) 107960583Seric # define FSHIFT 5 108060583Seric # endif 108157736Seric 108266763Seric # if defined(__alpha) || defined(IRIX) 108360583Seric # define FSHIFT 10 108460583Seric # endif 108568508Seric 108668508Seric # if defined(_AIX3) 108768508Seric # define FSHIFT 16 108868508Seric # endif 108966788Seric #endif 109057977Seric 109166788Seric #ifndef FSHIFT 109266788Seric # define FSHIFT 8 109357736Seric #endif 109460583Seric 109566788Seric #ifndef FSCALE 109666788Seric # define FSCALE (1 << FSHIFT) 109751920Seric #endif 109840930Srick 109914872Seric getla() 110014872Seric { 110114872Seric static int kmem = -1; 110251920Seric #if LA_TYPE == LA_INT 110324943Seric long avenrun[3]; 110451920Seric #else 110564360Seric # if LA_TYPE == LA_SHORT 110664360Seric short avenrun[3]; 110764360Seric # else 110851920Seric double avenrun[3]; 110964360Seric # endif 111051920Seric #endif 111125615Seric extern off_t lseek(); 111257736Seric extern int errno; 111314872Seric 111414872Seric if (kmem < 0) 111514872Seric { 111624945Seric kmem = open("/dev/kmem", 0, 0); 111714872Seric if (kmem < 0) 111857736Seric { 111957736Seric if (tTd(3, 1)) 112057736Seric printf("getla: open(/dev/kmem): %s\n", 112157736Seric errstring(errno)); 112214872Seric return (-1); 112357736Seric } 112451920Seric (void) fcntl(kmem, F_SETFD, 1); 112568508Seric #ifdef _AIX3 112668508Seric if (knlist(Nl, 1, sizeof Nl[0]) < 0) 112768508Seric #else 112857736Seric if (nlist(_PATH_UNIX, Nl) < 0) 112968508Seric #endif 113057736Seric { 113157736Seric if (tTd(3, 1)) 113257736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 113357736Seric errstring(errno)); 113414872Seric return (-1); 113557736Seric } 113659253Seric if (Nl[X_AVENRUN].n_value == 0) 113759253Seric { 113859253Seric if (tTd(3, 1)) 113959253Seric printf("getla: nlist(%s, %s) ==> 0\n", 114059253Seric _PATH_UNIX, LA_AVENRUN); 114159253Seric return (-1); 114259253Seric } 114368508Seric #ifdef NAMELISTMASK 114468508Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 114566763Seric #endif 114614872Seric } 114757736Seric if (tTd(3, 20)) 114857736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 114968508Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 || 115023118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 115119967Seric { 115219967Seric /* thank you Ian */ 115357736Seric if (tTd(3, 1)) 115457736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 115519967Seric return (-1); 115619967Seric } 115764360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 115857736Seric if (tTd(3, 5)) 115957736Seric { 116057736Seric printf("getla: avenrun = %d", avenrun[0]); 116157736Seric if (tTd(3, 15)) 116257736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 116357736Seric printf("\n"); 116457736Seric } 116557736Seric if (tTd(3, 1)) 116657736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 116724943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 116851920Seric #else 116957736Seric if (tTd(3, 5)) 117057736Seric { 117157736Seric printf("getla: avenrun = %g", avenrun[0]); 117257736Seric if (tTd(3, 15)) 117357736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 117457736Seric printf("\n"); 117557736Seric } 117657736Seric if (tTd(3, 1)) 117757736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 117851920Seric return ((int) (avenrun[0] + 0.5)); 117951920Seric #endif 118014872Seric } 118114872Seric 118251773Seric #else 118351920Seric #if LA_TYPE == LA_SUBR 118451773Seric 118564718Seric #ifdef DGUX 118664718Seric 118764718Seric #include <sys/dg_sys_info.h> 118864718Seric 118968508Seric int 119068508Seric getla() 119164718Seric { 119264718Seric struct dg_sys_info_load_info load_info; 119364718Seric 119464718Seric dg_sys_info((long *)&load_info, 119564718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 119664718Seric 119768508Seric if (tTd(3, 1)) 119868508Seric printf("getla: %d\n", (int) (load_info.one_minute + 0.5)); 119968508Seric 120064718Seric return((int) (load_info.one_minute + 0.5)); 120164718Seric } 120264718Seric 120364718Seric #else 120468508Seric # ifdef __hpux 120564718Seric 120668508Seric # include <sys/param.h> 120768508Seric # include <sys/pstat.h> 120868508Seric 120968508Seric int 121051773Seric getla() 121151773Seric { 121268508Seric struct pst_dynamic pstd; 121368508Seric 121468508Seric if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic), 121568508Seric (size_t) 1 ,0) == -1) 121668508Seric return 0; 121768508Seric 121868508Seric if (tTd(3, 1)) 121968508Seric printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5)); 122068508Seric 122168508Seric return (int) (pstd.psd_avg_1_min + 0.5); 122268508Seric } 122368508Seric 122468508Seric # else 122568508Seric 122668508Seric int 122768508Seric getla() 122868508Seric { 122951920Seric double avenrun[3]; 123051920Seric 123151920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 123257736Seric { 123357736Seric if (tTd(3, 1)) 123457736Seric perror("getla: getloadavg failed:"); 123551920Seric return (-1); 123657736Seric } 123757736Seric if (tTd(3, 1)) 123857736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 123951920Seric return ((int) (avenrun[0] + 0.5)); 124051773Seric } 124151773Seric 124268508Seric # endif /* __hpux */ 124364718Seric #endif /* DGUX */ 124451773Seric #else 124564295Seric #if LA_TYPE == LA_MACH 124651773Seric 124764295Seric /* 124865173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 124964295Seric */ 125064295Seric 125165173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 125265173Seric # include <mach/mach.h> 125365173Seric #else 125465173Seric # include <mach.h> 125565173Seric #endif 125664295Seric 125751773Seric getla() 125851773Seric { 125964295Seric processor_set_t default_set; 126064295Seric kern_return_t error; 126164295Seric unsigned int info_count; 126264295Seric struct processor_set_basic_info info; 126364295Seric host_t host; 126464295Seric 126564295Seric error = processor_set_default(host_self(), &default_set); 126664295Seric if (error != KERN_SUCCESS) 126764295Seric return -1; 126864295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 126964295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 127064295Seric &host, (processor_set_info_t)&info, 127164295Seric &info_count) != KERN_SUCCESS) 127264295Seric { 127364295Seric return -1; 127464295Seric } 127564295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 127664295Seric } 127764295Seric 127864295Seric 127964295Seric #else 128066313Seric #if LA_TYPE == LA_PROCSTR 128164295Seric 128266301Seric /* 128366301Seric ** Read /proc/loadavg for the load average. This is assumed to be 128466301Seric ** in a format like "0.15 0.12 0.06". 128566301Seric ** 128666301Seric ** Initially intended for Linux. This has been in the kernel 128766301Seric ** since at least 0.99.15. 128866301Seric */ 128966301Seric 129066301Seric # ifndef _PATH_LOADAVG 129166301Seric # define _PATH_LOADAVG "/proc/loadavg" 129266301Seric # endif 129366301Seric 129466301Seric int 129564295Seric getla() 129664295Seric { 129766319Seric double avenrun; 129866301Seric register int result; 129966319Seric FILE *fp; 130066301Seric 130166319Seric fp = fopen(_PATH_LOADAVG, "r"); 130266319Seric if (fp == NULL) 130366301Seric { 130466319Seric if (tTd(3, 1)) 130566319Seric printf("getla: fopen(%s): %s\n", 130666319Seric _PATH_LOADAVG, errstring(errno)); 130766319Seric return -1; 130866301Seric } 130966301Seric result = fscanf(fp, "%lf", &avenrun); 131066319Seric fclose(fp); 131166301Seric if (result != 1) 131266301Seric { 131366310Seric if (tTd(3, 1)) 131466301Seric printf("getla: fscanf() = %d: %s\n", 131566301Seric result, errstring(errno)); 131666301Seric return -1; 131766301Seric } 131866301Seric 131957736Seric if (tTd(3, 1)) 132066301Seric printf("getla(): %.2f\n", avenrun); 132166301Seric 132266301Seric return ((int) (avenrun + 0.5)); 132366301Seric } 132466301Seric 132566301Seric #else 132666301Seric 132766301Seric getla() 132866301Seric { 132966301Seric if (tTd(3, 1)) 133057736Seric printf("getla: ZERO\n"); 133151920Seric return (0); 133251773Seric } 133351773Seric 133451773Seric #endif 133551773Seric #endif 133664295Seric #endif 133766301Seric #endif 133866045Seric 133966045Seric 134066045Seric /* 134166045Seric * Copyright 1989 Massachusetts Institute of Technology 134266045Seric * 134366045Seric * Permission to use, copy, modify, distribute, and sell this software and its 134466045Seric * documentation for any purpose is hereby granted without fee, provided that 134566045Seric * the above copyright notice appear in all copies and that both that 134666045Seric * copyright notice and this permission notice appear in supporting 134766045Seric * documentation, and that the name of M.I.T. not be used in advertising or 134866045Seric * publicity pertaining to distribution of the software without specific, 134966045Seric * written prior permission. M.I.T. makes no representations about the 135066045Seric * suitability of this software for any purpose. It is provided "as is" 135166045Seric * without express or implied warranty. 135266045Seric * 135366045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 135466045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 135566045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 135666045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 135766045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 135866045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 135966045Seric * 136066045Seric * Authors: Many and varied... 136166045Seric */ 136266045Seric 136366045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 136466045Seric #ifndef lint 136566045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 136666045Seric #endif /* !lint */ 136766045Seric 136866179Seric #ifdef apollo 136966045Seric # undef volatile 137066045Seric # include <apollo/base.h> 137166045Seric 137266045Seric /* ARGSUSED */ 137366045Seric int getloadavg( call_data ) 137466045Seric caddr_t call_data; /* pointer to (double) return value */ 137566045Seric { 137666045Seric double *avenrun = (double *) call_data; 137766045Seric int i; 137866045Seric status_$t st; 137966045Seric long loadav[3]; 138066045Seric proc1_$get_loadav(loadav, &st); 138166045Seric *avenrun = loadav[0] / (double) (1 << 16); 138266045Seric return(0); 138366045Seric } 138466045Seric # endif /* apollo */ 138524943Seric /* 138624943Seric ** SHOULDQUEUE -- should this message be queued or sent? 138724943Seric ** 138824943Seric ** Compares the message cost to the load average to decide. 138924943Seric ** 139024943Seric ** Parameters: 139124943Seric ** pri -- the priority of the message in question. 139257438Seric ** ctime -- the message creation time. 139324943Seric ** 139424943Seric ** Returns: 139524943Seric ** TRUE -- if this message should be queued up for the 139624943Seric ** time being. 139724943Seric ** FALSE -- if the load is low enough to send this message. 139824943Seric ** 139924943Seric ** Side Effects: 140024943Seric ** none. 140124943Seric */ 140224943Seric 140324943Seric bool 140457438Seric shouldqueue(pri, ctime) 140524943Seric long pri; 140657438Seric time_t ctime; 140724943Seric { 140868508Seric bool rval; 140968508Seric 141068508Seric if (tTd(3, 30)) 141168508Seric printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri); 141251920Seric if (CurrentLA < QueueLA) 141368508Seric { 141468508Seric if (tTd(3, 30)) 141568508Seric printf("FALSE (CurrentLA < QueueLA)\n"); 141624943Seric return (FALSE); 141768508Seric } 141858132Seric if (CurrentLA >= RefuseLA) 141968508Seric { 142068508Seric if (tTd(3, 30)) 142168508Seric printf("TRUE (CurrentLA >= RefuseLA)\n"); 142258132Seric return (TRUE); 142368508Seric } 142468508Seric rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1)); 142568508Seric if (tTd(3, 30)) 142668508Seric printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE"); 142768508Seric return rval; 142824943Seric } 142924943Seric /* 143053037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 143153037Seric ** 143253037Seric ** Parameters: 143353037Seric ** none. 143453037Seric ** 143553037Seric ** Returns: 143653037Seric ** TRUE if incoming SMTP connections should be refused 143753037Seric ** (for now). 143853037Seric ** FALSE if we should accept new work. 143953037Seric ** 144053037Seric ** Side Effects: 144153037Seric ** none. 144253037Seric */ 144353037Seric 144453037Seric bool 144553037Seric refuseconnections() 144653037Seric { 144768508Seric extern bool enoughspace(); 144868508Seric 144959156Seric #ifdef XLA 145059156Seric if (!xla_smtp_ok()) 145159156Seric return TRUE; 145259156Seric #endif 145359156Seric 145453037Seric /* this is probably too simplistic */ 145568508Seric return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1); 145653037Seric } 145753037Seric /* 145824943Seric ** SETPROCTITLE -- set process title for ps 145924943Seric ** 146024943Seric ** Parameters: 146158674Seric ** fmt -- a printf style format string. 146258674Seric ** a, b, c -- possible parameters to fmt. 146324943Seric ** 146424943Seric ** Returns: 146524943Seric ** none. 146624943Seric ** 146724943Seric ** Side Effects: 146824943Seric ** Clobbers argv of our main procedure so ps(1) will 146924943Seric ** display the title. 147024943Seric */ 147124943Seric 147268508Seric #define SPT_NONE 0 /* don't use it at all */ 147368508Seric #define SPT_REUSEARGV 1 /* cover argv with title information */ 147468508Seric #define SPT_BUILTIN 2 /* use libc builtin */ 147568508Seric #define SPT_PSTAT 3 /* use pstat(PSTAT_SETCMD, ...) */ 147668508Seric #define SPT_PSSTRINGS 4 /* use PS_STRINGS->... */ 147768508Seric #define SPT_WRITEUDOT 5 /* write u. area in kmem */ 147868508Seric 147968508Seric #ifndef SPT_TYPE 148068508Seric # define SPT_TYPE SPT_REUSEARGV 148168508Seric #endif 148268508Seric 148368508Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN 148468508Seric 148568508Seric # if SPT_TYPE == SPT_PSTAT 148658689Seric # include <sys/pstat.h> 148758689Seric # endif 148868508Seric # if SPT_TYPE == SPT_PSSTRINGS 148959732Seric # include <machine/vmparam.h> 149059732Seric # include <sys/exec.h> 149168508Seric # ifndef PS_STRINGS /* hmmmm.... apparently not available after all */ 149268508Seric # undef SPT_TYPE 149368508Seric # define SPT_TYPE SPT_REUSEARGV 1494*68734Seric # else 1495*68734Seric # ifndef NKPDE /* FreeBSD 2.0 */ 1496*68734Seric # define NKPDE 63 1497*68734Seric typedef unsigned int *pt_entry_t; 1498*68734Seric # endif 149964247Seric # endif 150059732Seric # endif 150168508Seric 150268508Seric # if SPT_TYPE == SPT_PSSTRINGS 150368508Seric # define SETPROC_STATIC static 150468508Seric # else 150559732Seric # define SETPROC_STATIC 150659732Seric # endif 150767626Seric 150868508Seric # ifndef SPT_PADCHAR 150968508Seric # define SPT_PADCHAR ' ' 151068508Seric # endif 151158689Seric 151268508Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */ 151364843Seric 151468508Seric #if SPT_TYPE != SPT_BUILTIN 151568508Seric 151624943Seric /*VARARGS1*/ 151768508Seric void 151868508Seric # ifdef __STDC__ 151957642Seric setproctitle(char *fmt, ...) 152068508Seric # else 152157642Seric setproctitle(fmt, va_alist) 152224943Seric char *fmt; 152357642Seric va_dcl 152468508Seric # endif 152524943Seric { 152668508Seric # if SPT_TYPE != SPT_NONE 152724943Seric register char *p; 152825049Seric register int i; 152959732Seric SETPROC_STATIC char buf[MAXLINE]; 153056852Seric VA_LOCAL_DECL 153168508Seric # if SPT_TYPE == SPT_PSTAT 153258689Seric union pstun pst; 153358689Seric # endif 153424943Seric extern char **Argv; 153524943Seric extern char *LastArgv; 153624943Seric 153758674Seric p = buf; 153824943Seric 153958674Seric /* print sendmail: heading for grep */ 154058674Seric (void) strcpy(p, "sendmail: "); 154158674Seric p += strlen(p); 154224943Seric 154358674Seric /* print the argument string */ 154458674Seric VA_START(fmt); 154558674Seric (void) vsprintf(p, fmt, ap); 154656852Seric VA_END; 154754996Seric 154858674Seric i = strlen(buf); 154958689Seric 155068508Seric # if SPT_TYPE == SPT_PSTAT 155158689Seric pst.pst_command = buf; 155258689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 155358689Seric # else 155468508Seric # if SPT_TYPE == SPT_PSSTRINGS 155559732Seric PS_STRINGS->ps_nargvstr = 1; 155659732Seric PS_STRINGS->ps_argvstr = buf; 155759732Seric # else 155854996Seric if (i > LastArgv - Argv[0] - 2) 155925049Seric { 156054996Seric i = LastArgv - Argv[0] - 2; 156158674Seric buf[i] = '\0'; 156225049Seric } 156358674Seric (void) strcpy(Argv[0], buf); 156454997Seric p = &Argv[0][i]; 156524943Seric while (p < LastArgv) 156668508Seric *p++ = SPT_PADCHAR; 156768508Seric Argv[1] = NULL; 156868508Seric # endif /* SPT_TYPE == SPT_PSSTRINGS */ 156968508Seric # endif /* SPT_TYPE == SPT_PSTAT */ 157068508Seric # endif /* SPT_TYPE != SPT_NONE */ 157124943Seric } 157265982Seric 157368508Seric #endif /* SPT_TYPE != SPT_BUILTIN */ 157425698Seric /* 157525698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 157625698Seric ** 157725698Seric ** Parameters: 157868508Seric ** sig -- the signal that got us here (unused). 157925698Seric ** 158025698Seric ** Returns: 158125698Seric ** none. 158225698Seric ** 158325698Seric ** Side Effects: 158425698Seric ** Picks up extant zombies. 158525698Seric */ 158625698Seric 158746928Sbostic void 158868508Seric reapchild(sig) 158968508Seric int sig; 159025698Seric { 159164738Seric int olderrno = errno; 159263966Seric # ifdef HASWAITPID 159360219Seric auto int status; 159460560Seric int count; 159560560Seric int pid; 159660219Seric 159760560Seric count = 0; 159860560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 159960560Seric { 160060560Seric if (count++ > 1000) 160160560Seric { 160266748Seric #ifdef LOG 160360560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 160460560Seric pid, status); 160566748Seric #endif 160660560Seric break; 160760560Seric } 160860560Seric } 160960219Seric # else 161025698Seric # ifdef WNOHANG 161125698Seric union wait status; 161225698Seric 161363753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 161425698Seric continue; 161556795Seric # else /* WNOHANG */ 161625698Seric auto int status; 161725698Seric 161860219Seric while (wait(&status) > 0) 161925698Seric continue; 162056795Seric # endif /* WNOHANG */ 162160219Seric # endif 162264035Seric # ifdef SYS5SIGNALS 162364035Seric (void) setsignal(SIGCHLD, reapchild); 162458061Seric # endif 162564738Seric errno = olderrno; 162625698Seric } 162755418Seric /* 162855418Seric ** UNSETENV -- remove a variable from the environment 162955418Seric ** 163055418Seric ** Not needed on newer systems. 163155418Seric ** 163255418Seric ** Parameters: 163355418Seric ** name -- the string name of the environment variable to be 163455418Seric ** deleted from the current environment. 163555418Seric ** 163655418Seric ** Returns: 163755418Seric ** none. 163855418Seric ** 163955418Seric ** Globals: 164055418Seric ** environ -- a pointer to the current environment. 164155418Seric ** 164255418Seric ** Side Effects: 164355418Seric ** Modifies environ. 164455418Seric */ 164555418Seric 164663962Seric #ifndef HASUNSETENV 164755418Seric 164855418Seric void 164955418Seric unsetenv(name) 165055418Seric char *name; 165155418Seric { 165255418Seric extern char **environ; 165355418Seric register char **pp; 165455418Seric int len = strlen(name); 165555418Seric 165655418Seric for (pp = environ; *pp != NULL; pp++) 165755418Seric { 165855418Seric if (strncmp(name, *pp, len) == 0 && 165955418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 166055418Seric break; 166155418Seric } 166255418Seric 166355418Seric for (; *pp != NULL; pp++) 166455418Seric *pp = pp[1]; 166555418Seric } 166655418Seric 166763962Seric #endif 166856215Seric /* 166956215Seric ** GETDTABLESIZE -- return number of file descriptors 167056215Seric ** 167156215Seric ** Only on non-BSD systems 167256215Seric ** 167356215Seric ** Parameters: 167456215Seric ** none 167556215Seric ** 167656215Seric ** Returns: 167756215Seric ** size of file descriptor table 167856215Seric ** 167956215Seric ** Side Effects: 168056215Seric ** none 168156215Seric */ 168256215Seric 168363787Seric #ifdef SOLARIS 168463787Seric # include <sys/resource.h> 168563787Seric #endif 168656215Seric 168756215Seric int 168863787Seric getdtsize() 168956215Seric { 169063787Seric #ifdef RLIMIT_NOFILE 169163787Seric struct rlimit rl; 169263787Seric 169363787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 169463787Seric return rl.rlim_cur; 169563787Seric #endif 169663787Seric 169764031Seric # ifdef HASGETDTABLESIZE 169864031Seric return getdtablesize(); 169964031Seric # else 170064031Seric # ifdef _SC_OPEN_MAX 170158689Seric return sysconf(_SC_OPEN_MAX); 170263787Seric # else 170356215Seric return NOFILE; 170463787Seric # endif 170558689Seric # endif 170656215Seric } 170757631Seric /* 170857631Seric ** UNAME -- get the UUCP name of this system. 170957631Seric */ 171057631Seric 171157943Seric #ifndef HASUNAME 171257631Seric 171357631Seric int 171457631Seric uname(name) 171557631Seric struct utsname *name; 171657631Seric { 171757631Seric FILE *file; 171857631Seric char *n; 171957631Seric 172057631Seric name->nodename[0] = '\0'; 172157631Seric 172257661Seric /* try /etc/whoami -- one line with the node name */ 172357631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 172457631Seric { 172557661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 172657631Seric (void) fclose(file); 172757661Seric n = strchr(name->nodename, '\n'); 172857631Seric if (n != NULL) 172957631Seric *n = '\0'; 173057631Seric if (name->nodename[0] != '\0') 173157631Seric return (0); 173257631Seric } 173357631Seric 173457661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 173557631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 173657631Seric { 173757631Seric char buf[MAXLINE]; 173857631Seric 173957631Seric while (fgets(buf, MAXLINE, file) != NULL) 174057631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 174157631Seric NODE_LENGTH, name->nodename) > 0) 174257631Seric break; 174357631Seric (void) fclose(file); 174457631Seric if (name->nodename[0] != '\0') 174557631Seric return (0); 174657631Seric } 174757631Seric 174857631Seric #ifdef TRUST_POPEN 174957631Seric /* 175057631Seric ** Popen is known to have security holes. 175157631Seric */ 175257631Seric 175357661Seric /* try uuname -l to return local name */ 175457631Seric if ((file = popen("uuname -l", "r")) != NULL) 175557631Seric { 175657661Seric (void) fgets(name, NODE_LENGTH + 1, file); 175757631Seric (void) pclose(file); 175857661Seric n = strchr(name, '\n'); 175957631Seric if (n != NULL) 176057631Seric *n = '\0'; 176157661Seric if (name->nodename[0] != '\0') 176257631Seric return (0); 176357631Seric } 176457631Seric #endif 176557631Seric 176657631Seric return (-1); 176757631Seric } 176857943Seric #endif /* HASUNAME */ 176958068Seric /* 177058068Seric ** INITGROUPS -- initialize groups 177158068Seric ** 177258068Seric ** Stub implementation for System V style systems 177358068Seric */ 177458068Seric 177558068Seric #ifndef HASINITGROUPS 177658068Seric 177758068Seric initgroups(name, basegid) 177858068Seric char *name; 177958068Seric int basegid; 178058068Seric { 178158068Seric return 0; 178258068Seric } 178358068Seric 178458068Seric #endif 178558082Seric /* 178659289Seric ** SETSID -- set session id (for non-POSIX systems) 178759289Seric */ 178859289Seric 178959289Seric #ifndef HASSETSID 179059289Seric 179163753Seric pid_t 179263753Seric setsid __P ((void)) 179359289Seric { 179464155Seric #ifdef TIOCNOTTY 179564155Seric int fd; 179664155Seric 179766743Seric fd = open("/dev/tty", O_RDWR, 0); 179864155Seric if (fd >= 0) 179964155Seric { 180064155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 180164155Seric (void) close(fd); 180264155Seric } 180364155Seric #endif /* TIOCNOTTY */ 180464705Seric # ifdef SYS5SETPGRP 180563753Seric return setpgrp(); 180663753Seric # else 180764155Seric return setpgid(0, getpid()); 180859289Seric # endif 180959289Seric } 181059289Seric 181159289Seric #endif 181259289Seric /* 181366748Seric ** FSYNC -- dummy fsync 181466748Seric */ 181566748Seric 181666748Seric #ifdef NEEDFSYNC 181766748Seric 181866748Seric fsync(fd) 181966748Seric int fd; 182066748Seric { 182166748Seric # ifdef O_SYNC 182266748Seric return fcntl(fd, F_SETFL, O_SYNC); 182366748Seric # else 182466748Seric /* nothing we can do */ 182566748Seric return 0; 182666748Seric # endif 182766748Seric } 182866748Seric 182966748Seric #endif 183066748Seric /* 183164718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 183264718Seric ** 183364718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 183468508Seric ** instead of a long. This patches things. Only needed on versions 183568508Seric ** prior to 5.4.3. 183664718Seric */ 183764718Seric 183868508Seric #ifdef DGUX_5_4_2 183964718Seric 184064718Seric #undef inet_addr 184164718Seric 184264718Seric long 184364718Seric dgux_inet_addr(host) 184464718Seric char *host; 184564718Seric { 184664718Seric struct in_addr haddr; 184764718Seric 184864718Seric haddr = inet_addr(host); 184964718Seric return haddr.s_addr; 185064718Seric } 185164718Seric 185264718Seric #endif 185364718Seric /* 185463969Seric ** GETOPT -- for old systems or systems with bogus implementations 185563969Seric */ 185663969Seric 185763969Seric #ifdef NEEDGETOPT 185863969Seric 185963969Seric /* 186063969Seric * Copyright (c) 1985 Regents of the University of California. 186163969Seric * All rights reserved. The Berkeley software License Agreement 186263969Seric * specifies the terms and conditions for redistribution. 186363969Seric */ 186463969Seric 186563969Seric 186663969Seric /* 186763969Seric ** this version hacked to add `atend' flag to allow state machine 186863969Seric ** to reset if invoked by the program to scan args for a 2nd time 186963969Seric */ 187063969Seric 187163969Seric #if defined(LIBC_SCCS) && !defined(lint) 187263969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 187364072Seric #endif /* LIBC_SCCS and not lint */ 187463969Seric 187563969Seric #include <stdio.h> 187663969Seric 187763969Seric /* 187863969Seric * get option letter from argument vector 187963969Seric */ 188065352Seric #ifdef _CONVEX_SOURCE 188168508Seric extern int optind, opterr, optopt; 188268508Seric extern char *optarg; 188365352Seric #else 188465352Seric int opterr = 1; /* if error message should be printed */ 188565352Seric int optind = 1; /* index into parent argv vector */ 188668470Seric int optopt = 0; /* character checked for validity */ 188768470Seric char *optarg = NULL; /* argument associated with option */ 188868508Seric #endif 188963969Seric 189063969Seric #define BADCH (int)'?' 189163969Seric #define EMSG "" 189263969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 189363969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 189463969Seric 189563969Seric getopt(nargc,nargv,ostr) 189665160Seric int nargc; 189765160Seric char *const *nargv; 189865160Seric const char *ostr; 189963969Seric { 190063969Seric static char *place = EMSG; /* option letter processing */ 190163969Seric static char atend = 0; 190263969Seric register char *oli; /* option letter list index */ 190363969Seric 190463969Seric if (atend) { 190563969Seric atend = 0; 190663969Seric place = EMSG; 190763969Seric } 190863969Seric if(!*place) { /* update scanning pointer */ 190964136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 191063969Seric atend++; 191163969Seric return(EOF); 191263969Seric } 191363969Seric if (*place == '-') { /* found "--" */ 191463969Seric ++optind; 191563969Seric atend++; 191663969Seric return(EOF); 191763969Seric } 191863969Seric } /* option letter okay? */ 191964125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 192064136Seric if (!*place) ++optind; 192163969Seric tell(": illegal option -- "); 192263969Seric } 192363969Seric if (*++oli != ':') { /* don't need argument */ 192463969Seric optarg = NULL; 192563969Seric if (!*place) ++optind; 192663969Seric } 192763969Seric else { /* need an argument */ 192863969Seric if (*place) optarg = place; /* no white space */ 192963969Seric else if (nargc <= ++optind) { /* no arg */ 193063969Seric place = EMSG; 193163969Seric tell(": option requires an argument -- "); 193263969Seric } 193363969Seric else optarg = nargv[optind]; /* white space */ 193463969Seric place = EMSG; 193563969Seric ++optind; 193663969Seric } 193763969Seric return(optopt); /* dump back option letter */ 193863969Seric } 193963969Seric 194063969Seric #endif 194163969Seric /* 194263969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 194363969Seric */ 194463969Seric 194563969Seric #ifdef NEEDVPRINTF 194663969Seric 194763969Seric #define MAXARG 16 194863969Seric 194963969Seric vfprintf(fp, fmt, ap) 195063969Seric FILE * fp; 195163969Seric char * fmt; 195263969Seric char ** ap; 195363969Seric { 195463969Seric char * bp[MAXARG]; 195563969Seric int i = 0; 195663969Seric 195763969Seric while (*ap && i < MAXARG) 195863969Seric bp[i++] = *ap++; 195963969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 196063969Seric bp[4], bp[5], bp[6], bp[7], 196163969Seric bp[8], bp[9], bp[10], bp[11], 196263969Seric bp[12], bp[13], bp[14], bp[15]); 196363969Seric } 196463969Seric 196563969Seric vsprintf(s, fmt, ap) 196663969Seric char * s; 196763969Seric char * fmt; 196863969Seric char ** ap; 196963969Seric { 197063969Seric char * bp[MAXARG]; 197163969Seric int i = 0; 197263969Seric 197363969Seric while (*ap && i < MAXARG) 197463969Seric bp[i++] = *ap++; 197563969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 197663969Seric bp[4], bp[5], bp[6], bp[7], 197763969Seric bp[8], bp[9], bp[10], bp[11], 197863969Seric bp[12], bp[13], bp[14], bp[15]); 197963969Seric } 198063969Seric 198163969Seric #endif 198263969Seric /* 198365220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 198465208Seric ** 198565211Seric ** Parameters: 198665211Seric ** shell -- the user's shell from /etc/passwd 198765211Seric ** 198865211Seric ** Returns: 198965211Seric ** TRUE -- if it is ok to use this for unrestricted access. 199065211Seric ** FALSE -- if the shell is restricted. 199165208Seric */ 199265208Seric 199365587Seric #if !HASGETUSERSHELL 199465587Seric 199565587Seric # ifndef _PATH_SHELLS 199665587Seric # define _PATH_SHELLS "/etc/shells" 199765587Seric # endif 199865587Seric 199965587Seric char *DefaultUserShells[] = 200065587Seric { 200168508Seric "/bin/sh", /* standard shell */ 200265587Seric "/usr/bin/sh", 200368508Seric "/bin/csh", /* C shell */ 200465587Seric "/usr/bin/csh", 200565587Seric #ifdef __hpux 200668508Seric "/bin/rsh", /* restricted Bourne shell */ 200768508Seric "/bin/ksh", /* Korn shell */ 200868508Seric "/bin/rksh", /* restricted Korn shell */ 200965587Seric "/bin/pam", 201068508Seric "/usr/bin/keysh", /* key shell (extended Korn shell) */ 201165587Seric "/bin/posix/sh", 201265211Seric #endif 201368508Seric #ifdef _AIX3 201468508Seric "/bin/ksh", /* Korn shell */ 201568508Seric "/usr/bin/ksh", 201668508Seric "/bin/tsh", /* trusted shell */ 201768508Seric "/usr/bin/tsh", 201868508Seric "/bin/bsh", /* Bourne shell */ 201968508Seric "/usr/bin/bsh", 202068508Seric #endif 202165587Seric NULL 202265587Seric }; 202365587Seric 202465208Seric #endif 202565208Seric 202665747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 202765747Seric 202865211Seric bool 202965211Seric usershellok(shell) 203065211Seric char *shell; 203165208Seric { 203265211Seric #if HASGETUSERSHELL 203365211Seric register char *p; 203465211Seric extern char *getusershell(); 203565208Seric 203668457Seric if (shell == NULL || shell[0] == '\0') 203768457Seric return TRUE; 203868457Seric 203965211Seric setusershell(); 204065211Seric while ((p = getusershell()) != NULL) 204165747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 204265211Seric break; 204365211Seric endusershell(); 204465211Seric return p != NULL; 204565211Seric #else 204665211Seric register FILE *shellf; 204765211Seric char buf[MAXLINE]; 204865208Seric 204968457Seric if (shell == NULL || shell[0] == '\0') 205068457Seric return TRUE; 205168457Seric 205265211Seric shellf = fopen(_PATH_SHELLS, "r"); 205365211Seric if (shellf == NULL) 205465211Seric { 205565211Seric /* no /etc/shells; see if it is one of the std shells */ 205665587Seric char **d; 205765587Seric 205865587Seric for (d = DefaultUserShells; *d != NULL; d++) 205965587Seric { 206065587Seric if (strcmp(shell, *d) == 0) 206165587Seric return TRUE; 206265587Seric } 206365587Seric return FALSE; 206465211Seric } 206565208Seric 206665211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 206765211Seric { 206865211Seric register char *p, *q; 206965208Seric 207065211Seric p = buf; 207165211Seric while (*p != '\0' && *p != '#' && *p != '/') 207265211Seric p++; 207365211Seric if (*p == '#' || *p == '\0') 207465208Seric continue; 207565211Seric q = p; 207665211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 207765211Seric p++; 207865211Seric *p = '\0'; 207965747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 208065211Seric { 208165211Seric fclose(shellf); 208265211Seric return TRUE; 208365211Seric } 208465208Seric } 208565211Seric fclose(shellf); 208665211Seric return FALSE; 208765211Seric #endif 208865208Seric } 208965208Seric /* 209064311Seric ** FREESPACE -- see how much free space is on the queue filesystem 209158082Seric ** 209258082Seric ** Only implemented if you have statfs. 209358082Seric ** 209458082Seric ** Parameters: 209564311Seric ** dir -- the directory in question. 209664311Seric ** bsize -- a variable into which the filesystem 209764311Seric ** block size is stored. 209858082Seric ** 209958082Seric ** Returns: 210064311Seric ** The number of bytes free on the queue filesystem. 210164311Seric ** -1 if the statfs call fails. 210264311Seric ** 210364311Seric ** Side effects: 210464311Seric ** Puts the filesystem block size into bsize. 210558082Seric */ 210658082Seric 210765749Seric /* statfs types */ 210865749Seric #define SFS_NONE 0 /* no statfs implementation */ 210965749Seric #define SFS_USTAT 1 /* use ustat */ 211065749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 211165749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 211265749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 211365749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 211466752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 211565749Seric 211665749Seric #ifndef SFS_TYPE 211765749Seric # define SFS_TYPE SFS_NONE 211858157Seric #endif 211958157Seric 212065749Seric #if SFS_TYPE == SFS_USTAT 212158157Seric # include <ustat.h> 212258157Seric #endif 212365749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 212465749Seric # include <sys/statfs.h> 212558082Seric #endif 212665749Seric #if SFS_TYPE == SFS_VFS 212765749Seric # include <sys/vfs.h> 212865749Seric #endif 212965749Seric #if SFS_TYPE == SFS_MOUNT 213065749Seric # include <sys/mount.h> 213165749Seric #endif 213266752Seric #if SFS_TYPE == SFS_STATVFS 213366752Seric # include <sys/statvfs.h> 213466752Seric #endif 213558082Seric 213664311Seric long 213764311Seric freespace(dir, bsize) 213864311Seric char *dir; 213964311Seric long *bsize; 214058082Seric { 214165749Seric #if SFS_TYPE != SFS_NONE 214265749Seric # if SFS_TYPE == SFS_USTAT 214358153Seric struct ustat fs; 214458153Seric struct stat statbuf; 214558366Seric # define FSBLOCKSIZE DEV_BSIZE 214668695Seric # define SFS_BAVAIL f_tfree 214758157Seric # else 214858157Seric # if defined(ultrix) 214958157Seric struct fs_data fs; 215068695Seric # define SFS_BAVAIL fd_bfreen 215168508Seric # define FSBLOCKSIZE 1024L 215258153Seric # else 215366752Seric # if SFS_TYPE == SFS_STATVFS 215466752Seric struct statvfs fs; 215568508Seric # define FSBLOCKSIZE fs.f_frsize 215666752Seric # else 215758082Seric struct statfs fs; 215866752Seric # define FSBLOCKSIZE fs.f_bsize 215963987Seric # endif 216058153Seric # endif 216158133Seric # endif 216268695Seric # ifndef SFS_BAVAIL 216368695Seric # define SFS_BAVAIL f_bavail 216468508Seric # endif 216558082Seric 216665749Seric # if SFS_TYPE == SFS_USTAT 216764311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 216858157Seric # else 216965749Seric # if SFS_TYPE == SFS_4ARGS 217064311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 217158157Seric # else 217268508Seric # if SFS_TYPE == SFS_STATVFS 217368508Seric if (statvfs(dir, &fs) == 0) 217468508Seric # else 217568508Seric # if defined(ultrix) 217668499Seric if (statfs(dir, &fs) > 0) 217768508Seric # else 217864311Seric if (statfs(dir, &fs) == 0) 217968508Seric # endif 218058153Seric # endif 218158133Seric # endif 218258133Seric # endif 218358133Seric { 218464311Seric if (bsize != NULL) 218564311Seric *bsize = FSBLOCKSIZE; 218668695Seric return (fs.SFS_BAVAIL); 218764311Seric } 218864337Seric #endif 218964311Seric return (-1); 219064311Seric } 219164311Seric /* 219264311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 219364311Seric ** 219464311Seric ** Only implemented if you have statfs. 219564311Seric ** 219664311Seric ** Parameters: 219764311Seric ** msize -- the size to check against. If zero, we don't yet 219864311Seric ** know how big the message will be, so just check for 219964311Seric ** a "reasonable" amount. 220064311Seric ** 220164311Seric ** Returns: 220264311Seric ** TRUE if there is enough space. 220364311Seric ** FALSE otherwise. 220464311Seric */ 220564311Seric 220664311Seric bool 220764311Seric enoughspace(msize) 220864311Seric long msize; 220964311Seric { 221064311Seric long bfree, bsize; 221164311Seric 221264311Seric if (MinBlocksFree <= 0 && msize <= 0) 221364311Seric { 221458133Seric if (tTd(4, 80)) 221564311Seric printf("enoughspace: no threshold\n"); 221664311Seric return TRUE; 221764311Seric } 221864311Seric 221964311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 222064311Seric { 222164311Seric if (tTd(4, 80)) 222258333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 222364311Seric bfree, msize); 222458333Seric 222558333Seric /* convert msize to block count */ 222664311Seric msize = msize / bsize + 1; 222758333Seric if (MinBlocksFree >= 0) 222858333Seric msize += MinBlocksFree; 222958333Seric 223064311Seric if (bfree < msize) 223158090Seric { 223258090Seric #ifdef LOG 223358090Seric if (LogLevel > 0) 223464254Seric syslog(LOG_ALERT, 223564254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 223668508Seric CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id, 223768508Seric bfree, 223868508Seric CurHostName == NULL ? "SMTP-DAEMON" : CurHostName, 223968508Seric msize, QueueDir); 224058090Seric #endif 224158082Seric return FALSE; 224258090Seric } 224358082Seric } 224458082Seric else if (tTd(4, 80)) 224558333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 224658333Seric MinBlocksFree, msize, errstring(errno)); 224758082Seric return TRUE; 224858082Seric } 224958542Seric /* 225058542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 225158542Seric ** 225258542Seric ** This looks at an errno value and tells if this is likely to 225358542Seric ** go away if retried later. 225458542Seric ** 225558542Seric ** Parameters: 225658542Seric ** err -- the errno code to classify. 225758542Seric ** 225858542Seric ** Returns: 225958542Seric ** TRUE if this is probably transient. 226058542Seric ** FALSE otherwise. 226158542Seric */ 226258542Seric 226358542Seric bool 226458542Seric transienterror(err) 226558542Seric int err; 226658542Seric { 226758542Seric switch (err) 226858542Seric { 226958542Seric case EIO: /* I/O error */ 227058542Seric case ENXIO: /* Device not configured */ 227158542Seric case EAGAIN: /* Resource temporarily unavailable */ 227258542Seric case ENOMEM: /* Cannot allocate memory */ 227358542Seric case ENODEV: /* Operation not supported by device */ 227458542Seric case ENFILE: /* Too many open files in system */ 227558542Seric case EMFILE: /* Too many open files */ 227658542Seric case ENOSPC: /* No space left on device */ 227758542Seric #ifdef ETIMEDOUT 227858542Seric case ETIMEDOUT: /* Connection timed out */ 227958542Seric #endif 228058542Seric #ifdef ESTALE 228158542Seric case ESTALE: /* Stale NFS file handle */ 228258542Seric #endif 228358542Seric #ifdef ENETDOWN 228458542Seric case ENETDOWN: /* Network is down */ 228558542Seric #endif 228658542Seric #ifdef ENETUNREACH 228758542Seric case ENETUNREACH: /* Network is unreachable */ 228858542Seric #endif 228958542Seric #ifdef ENETRESET 229058542Seric case ENETRESET: /* Network dropped connection on reset */ 229158542Seric #endif 229258542Seric #ifdef ECONNABORTED 229358542Seric case ECONNABORTED: /* Software caused connection abort */ 229458542Seric #endif 229558542Seric #ifdef ECONNRESET 229658542Seric case ECONNRESET: /* Connection reset by peer */ 229758542Seric #endif 229858542Seric #ifdef ENOBUFS 229958542Seric case ENOBUFS: /* No buffer space available */ 230058542Seric #endif 230158542Seric #ifdef ESHUTDOWN 230258542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 230358542Seric #endif 230458542Seric #ifdef ECONNREFUSED 230558542Seric case ECONNREFUSED: /* Connection refused */ 230658542Seric #endif 230758542Seric #ifdef EHOSTDOWN 230858542Seric case EHOSTDOWN: /* Host is down */ 230958542Seric #endif 231058542Seric #ifdef EHOSTUNREACH 231158542Seric case EHOSTUNREACH: /* No route to host */ 231258542Seric #endif 231358542Seric #ifdef EDQUOT 231458542Seric case EDQUOT: /* Disc quota exceeded */ 231558542Seric #endif 231658542Seric #ifdef EPROCLIM 231758542Seric case EPROCLIM: /* Too many processes */ 231858542Seric #endif 231958542Seric #ifdef EUSERS 232058542Seric case EUSERS: /* Too many users */ 232158542Seric #endif 232258542Seric #ifdef EDEADLK 232358542Seric case EDEADLK: /* Resource deadlock avoided */ 232458542Seric #endif 232558542Seric #ifdef EISCONN 232658542Seric case EISCONN: /* Socket already connected */ 232758542Seric #endif 232858542Seric #ifdef EINPROGRESS 232958542Seric case EINPROGRESS: /* Operation now in progress */ 233058542Seric #endif 233158542Seric #ifdef EALREADY 233258542Seric case EALREADY: /* Operation already in progress */ 233358542Seric #endif 233458542Seric #ifdef EADDRINUSE 233558542Seric case EADDRINUSE: /* Address already in use */ 233658542Seric #endif 233758542Seric #ifdef EADDRNOTAVAIL 233858542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 233958542Seric #endif 234066018Seric #ifdef ETXTBSY 234166017Seric case ETXTBSY: /* (Apollo) file locked */ 234266017Seric #endif 234363834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 234458542Seric case ENOSR: /* Out of streams resources */ 234558542Seric #endif 234658542Seric return TRUE; 234758542Seric } 234858542Seric 234958542Seric /* nope, must be permanent */ 235058542Seric return FALSE; 235158542Seric } 235258689Seric /* 235364035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 235458689Seric ** 235558689Seric ** Parameters: 235658689Seric ** fd -- the file descriptor of the file. 235758689Seric ** filename -- the file name (for error messages). 235864335Seric ** ext -- the filename extension. 235958689Seric ** type -- type of the lock. Bits can be: 236058689Seric ** LOCK_EX -- exclusive lock. 236158689Seric ** LOCK_NB -- non-blocking. 236258689Seric ** 236358689Seric ** Returns: 236458689Seric ** TRUE if the lock was acquired. 236558689Seric ** FALSE otherwise. 236658689Seric */ 236758689Seric 236858689Seric bool 236964335Seric lockfile(fd, filename, ext, type) 237058689Seric int fd; 237158689Seric char *filename; 237264335Seric char *ext; 237358689Seric int type; 237458689Seric { 237565830Seric # if !HASFLOCK 237658689Seric int action; 237758689Seric struct flock lfd; 237864335Seric 237964335Seric if (ext == NULL) 238064335Seric ext = ""; 238164035Seric 238263983Seric bzero(&lfd, sizeof lfd); 238359447Seric if (bitset(LOCK_UN, type)) 238459447Seric lfd.l_type = F_UNLCK; 238559447Seric else if (bitset(LOCK_EX, type)) 238658689Seric lfd.l_type = F_WRLCK; 238758689Seric else 238858689Seric lfd.l_type = F_RDLCK; 238958689Seric 239058689Seric if (bitset(LOCK_NB, type)) 239158689Seric action = F_SETLK; 239258689Seric else 239358689Seric action = F_SETLKW; 239458689Seric 239564264Seric if (tTd(55, 60)) 239664335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 239764335Seric filename, ext, action, lfd.l_type); 239864264Seric 239958689Seric if (fcntl(fd, action, &lfd) >= 0) 240064264Seric { 240164264Seric if (tTd(55, 60)) 240264264Seric printf("SUCCESS\n"); 240358689Seric return TRUE; 240464264Seric } 240558689Seric 240664264Seric if (tTd(55, 60)) 240764264Seric printf("(%s) ", errstring(errno)); 240864264Seric 240964136Seric /* 241064136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 241164136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 241264136Seric ** as type "tmp" (that is, served from swap space), the 241364136Seric ** previous fcntl will fail with "Invalid argument" errors. 241464136Seric ** Since this is fairly common during testing, we will assume 241564136Seric ** that this indicates that the lock is successfully grabbed. 241664136Seric */ 241764136Seric 241864136Seric if (errno == EINVAL) 241964264Seric { 242064264Seric if (tTd(55, 60)) 242164264Seric printf("SUCCESS\n"); 242264136Seric return TRUE; 242364264Seric } 242464136Seric 242558689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 242664378Seric { 242764378Seric int omode = -1; 242864378Seric # ifdef F_GETFL 242964378Seric int oerrno = errno; 243064378Seric 243164378Seric (void) fcntl(fd, F_GETFL, &omode); 243264378Seric errno = oerrno; 243364378Seric # endif 243464378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 243564378Seric filename, ext, fd, type, omode, geteuid()); 243664378Seric } 243758689Seric # else 243864335Seric if (ext == NULL) 243964335Seric ext = ""; 244064335Seric 244164264Seric if (tTd(55, 60)) 244264335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 244364264Seric 244458689Seric if (flock(fd, type) >= 0) 244564264Seric { 244664264Seric if (tTd(55, 60)) 244764264Seric printf("SUCCESS\n"); 244858689Seric return TRUE; 244964264Seric } 245058689Seric 245164264Seric if (tTd(55, 60)) 245264264Seric printf("(%s) ", errstring(errno)); 245364264Seric 245458689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 245564378Seric { 245664378Seric int omode = -1; 245764378Seric # ifdef F_GETFL 245864378Seric int oerrno = errno; 245964378Seric 246064378Seric (void) fcntl(fd, F_GETFL, &omode); 246164378Seric errno = oerrno; 246264378Seric # endif 246364378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 246464378Seric filename, ext, fd, type, omode, geteuid()); 246564386Seric } 246658689Seric # endif 246764264Seric if (tTd(55, 60)) 246864264Seric printf("FAIL\n"); 246958689Seric return FALSE; 247058689Seric } 247164556Seric /* 247265948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 247365948Seric ** 247465948Seric ** Parameters: 247565948Seric ** fd -- the file descriptor to check. 247665948Seric ** 247765948Seric ** Returns: 247865948Seric ** TRUE -- if only root can chown the file to an arbitrary 247965948Seric ** user. 248065948Seric ** FALSE -- if an arbitrary user can give away a file. 248165948Seric */ 248265948Seric 248365948Seric bool 248465948Seric chownsafe(fd) 248565948Seric int fd; 248665948Seric { 248765950Seric #ifdef __hpux 248865948Seric char *s; 248965948Seric int tfd; 249065948Seric uid_t o_uid, o_euid; 249165948Seric gid_t o_gid, o_egid; 249265948Seric bool rval; 249365948Seric struct stat stbuf; 249465948Seric 249565948Seric o_uid = getuid(); 249665948Seric o_euid = geteuid(); 249765948Seric o_gid = getgid(); 249865948Seric o_egid = getegid(); 249965959Seric fstat(fd, &stbuf); 250065948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 250165948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 250265948Seric s = tmpnam(NULL); 250365948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 250465974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 250565948Seric close(tfd); 250665948Seric unlink(s); 250765948Seric setreuid(o_uid, o_euid); 250865948Seric setresgid(o_gid, o_egid, -1); 250965948Seric return rval; 251065948Seric #else 251166088Seric # ifdef _POSIX_CHOWN_RESTRICTED 251266088Seric # if _POSIX_CHOWN_RESTRICTED == -1 251366088Seric return FALSE; 251466088Seric # else 251566088Seric return TRUE; 251666088Seric # endif 251766088Seric # else 251866088Seric # ifdef _PC_CHOWN_RESTRICTED 251968508Seric int rval; 252068508Seric 252168508Seric /* 252268508Seric ** Some systems (e.g., SunOS) seem to have the call and the 252368508Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 252468508Seric ** the call. This heuristic checks for that. 252568508Seric */ 252668508Seric 252768508Seric errno = 0; 252868508Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 252968508Seric if (errno == 0) 253068508Seric return rval > 0; 253168508Seric # endif 253268508Seric # ifdef BSD 253368508Seric return TRUE; 253468499Seric # else 253565948Seric return FALSE; 253665948Seric # endif 253765948Seric # endif 253865948Seric #endif 253965948Seric } 254065948Seric /* 254168508Seric ** RESETLIMITS -- reset system controlled resource limits 254268508Seric ** 254368508Seric ** This is to avoid denial-of-service attacks 254468508Seric ** 254568508Seric ** Parameters: 254668508Seric ** none 254768508Seric ** 254868508Seric ** Returns: 254968508Seric ** none 255068508Seric */ 255168508Seric 255268508Seric #if HASSETRLIMIT 255368508Seric # include <sys/resource.h> 255468508Seric #endif 255568508Seric 255668508Seric void 255768508Seric resetlimits() 255868508Seric { 255968508Seric #if HASSETRLIMIT 256068508Seric struct rlimit lim; 256168508Seric 256268508Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 256368508Seric (void) setrlimit(RLIMIT_CPU, &lim); 256468508Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 256568508Seric #else 256668508Seric # if HASULIMIT 256768508Seric (void) ulimit(2, 0x3fffff); 256868508Seric # endif 256968508Seric #endif 257068508Seric } 257168508Seric /* 257264556Seric ** GETCFNAME -- return the name of the .cf file. 257364556Seric ** 257464556Seric ** Some systems (e.g., NeXT) determine this dynamically. 257564556Seric */ 257664556Seric 257764556Seric char * 257864556Seric getcfname() 257964556Seric { 258064556Seric if (ConfFile != NULL) 258164556Seric return ConfFile; 258265214Seric #ifdef NETINFO 258365214Seric { 258465214Seric extern char *ni_propval(); 258565214Seric char *cflocation; 258665214Seric 258768508Seric cflocation = ni_propval("/locations", NULL, "sendmail", 258868508Seric "sendmail.cf", '\0'); 258965214Seric if (cflocation != NULL) 259065214Seric return cflocation; 259165214Seric } 259265214Seric #endif 259364556Seric return _PATH_SENDMAILCF; 259464556Seric } 259564718Seric /* 259664718Seric ** SETVENDOR -- process vendor code from V configuration line 259764718Seric ** 259864718Seric ** Parameters: 259964718Seric ** vendor -- string representation of vendor. 260064718Seric ** 260164718Seric ** Returns: 260264718Seric ** TRUE -- if ok. 260364718Seric ** FALSE -- if vendor code could not be processed. 260464926Seric ** 260564926Seric ** Side Effects: 260664926Seric ** It is reasonable to set mode flags here to tweak 260764926Seric ** processing in other parts of the code if necessary. 260864926Seric ** For example, if you are a vendor that uses $%y to 260964926Seric ** indicate YP lookups, you could enable that here. 261064718Seric */ 261164718Seric 261264718Seric bool 261364718Seric setvendor(vendor) 261464718Seric char *vendor; 261564718Seric { 261664926Seric if (strcasecmp(vendor, "Berkeley") == 0) 261768508Seric { 261868508Seric VendorCode = VENDOR_BERKELEY; 261964926Seric return TRUE; 262068508Seric } 262164926Seric 262264926Seric /* add vendor extensions here */ 262364926Seric 262468508Seric #ifdef SUN_EXTENSIONS 262568508Seric if (strcasecmp(vendor, "Sun") == 0) 262668508Seric { 262768508Seric VendorCode = VENDOR_SUN; 262868508Seric return TRUE; 262968508Seric } 263068508Seric #endif 263168508Seric 263264926Seric return FALSE; 263364718Seric } 263464816Seric /* 263564816Seric ** STRTOL -- convert string to long integer 263664816Seric ** 263764816Seric ** For systems that don't have it in the C library. 263866161Seric ** 263966161Seric ** This is taken verbatim from the 4.4-Lite C library. 264064816Seric */ 264164816Seric 264264816Seric #ifdef NEEDSTRTOL 264364816Seric 264466161Seric #if defined(LIBC_SCCS) && !defined(lint) 264566161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 264666161Seric #endif /* LIBC_SCCS and not lint */ 264766161Seric 264866161Seric #include <limits.h> 264966161Seric 265066161Seric /* 265166161Seric * Convert a string to a long integer. 265266161Seric * 265366161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 265466161Seric * alphabets and digits are each contiguous. 265566161Seric */ 265666161Seric 265764816Seric long 265866161Seric strtol(nptr, endptr, base) 265966161Seric const char *nptr; 266066161Seric char **endptr; 266166161Seric register int base; 266264816Seric { 266366161Seric register const char *s = nptr; 266466161Seric register unsigned long acc; 266566161Seric register int c; 266666161Seric register unsigned long cutoff; 266766161Seric register int neg = 0, any, cutlim; 266864816Seric 266966161Seric /* 267066161Seric * Skip white space and pick up leading +/- sign if any. 267166161Seric * If base is 0, allow 0x for hex and 0 for octal, else 267266161Seric * assume decimal; if base is already 16, allow 0x. 267366161Seric */ 267466161Seric do { 267566161Seric c = *s++; 267666161Seric } while (isspace(c)); 267766161Seric if (c == '-') { 267866161Seric neg = 1; 267966161Seric c = *s++; 268066161Seric } else if (c == '+') 268166161Seric c = *s++; 268266161Seric if ((base == 0 || base == 16) && 268366161Seric c == '0' && (*s == 'x' || *s == 'X')) { 268466161Seric c = s[1]; 268566161Seric s += 2; 268666161Seric base = 16; 268766161Seric } 268866161Seric if (base == 0) 268966161Seric base = c == '0' ? 8 : 10; 269064816Seric 269166161Seric /* 269266161Seric * Compute the cutoff value between legal numbers and illegal 269366161Seric * numbers. That is the largest legal value, divided by the 269466161Seric * base. An input number that is greater than this value, if 269566161Seric * followed by a legal input character, is too big. One that 269666161Seric * is equal to this value may be valid or not; the limit 269766161Seric * between valid and invalid numbers is then based on the last 269866161Seric * digit. For instance, if the range for longs is 269966161Seric * [-2147483648..2147483647] and the input base is 10, 270066161Seric * cutoff will be set to 214748364 and cutlim to either 270166161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 270266161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 270366161Seric * the number is too big, and we will return a range error. 270466161Seric * 270566161Seric * Set any if any `digits' consumed; make it negative to indicate 270666161Seric * overflow. 270766161Seric */ 270866161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 270966161Seric cutlim = cutoff % (unsigned long)base; 271066161Seric cutoff /= (unsigned long)base; 271166161Seric for (acc = 0, any = 0;; c = *s++) { 271266161Seric if (isdigit(c)) 271366161Seric c -= '0'; 271466161Seric else if (isalpha(c)) 271566161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 271666161Seric else 271766161Seric break; 271866161Seric if (c >= base) 271966161Seric break; 272066161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 272166161Seric any = -1; 272266161Seric else { 272366161Seric any = 1; 272466161Seric acc *= base; 272566161Seric acc += c; 272664816Seric } 272764816Seric } 272866161Seric if (any < 0) { 272966161Seric acc = neg ? LONG_MIN : LONG_MAX; 273066161Seric errno = ERANGE; 273166161Seric } else if (neg) 273266161Seric acc = -acc; 273366161Seric if (endptr != 0) 273466161Seric *endptr = (char *)(any ? s - 1 : nptr); 273566161Seric return (acc); 273664816Seric } 273764816Seric 273864816Seric #endif 273964841Seric /* 274068693Seric ** SM_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 274164841Seric ** 274268693Seric ** Some operating systems have wierd problems with the gethostbyXXX 274368693Seric ** routines. For example, Solaris versions at least through 2.3 274468693Seric ** don't properly deliver a canonical h_name field. This tries to 274568693Seric ** work around these problems. 274664841Seric */ 274764841Seric 274864841Seric struct hostent * 274968693Seric sm_gethostbyname(name) 275068707Seric char *name; 275164841Seric { 275268693Seric #if defined(SOLARIS) && SOLARIS < 204 275368693Seric extern int h_errno; 275468693Seric 275568508Seric # if SOLARIS == 203 275664841Seric static struct hostent hp; 275764841Seric static char buf[1000]; 275864841Seric extern struct hostent *_switch_gethostbyname_r(); 275964841Seric 276064841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 276164841Seric # else 276265009Seric extern struct hostent *__switch_gethostbyname(); 276365009Seric 276464841Seric return __switch_gethostbyname(name); 276564841Seric # endif 276668693Seric #else 276768693Seric return gethostbyname(name); 276868693Seric #endif 276964841Seric } 277064841Seric 277164841Seric struct hostent * 277268693Seric sm_gethostbyaddr(addr, len, type) 277368707Seric char *addr; 277464841Seric int len; 277564841Seric int type; 277664841Seric { 277768693Seric #if defined(SOLARIS) && SOLARIS < 204 277868693Seric extern int h_errno; 277968693Seric 278068508Seric # if SOLARIS == 203 278164841Seric static struct hostent hp; 278264841Seric static char buf[1000]; 278364841Seric extern struct hostent *_switch_gethostbyaddr_r(); 278464841Seric 278564841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 278664841Seric # else 278765009Seric extern struct hostent *__switch_gethostbyaddr(); 278865009Seric 278964841Seric return __switch_gethostbyaddr(addr, len, type); 279064841Seric # endif 279168693Seric #else 279268693Seric return gethostbyaddr(addr, len, type); 279368693Seric #endif 279464841Seric } 279568693Seric /* 279668693Seric ** SM_GETPW{NAM,UID} -- wrapper for getpwnam and getpwuid 279768693Seric */ 279864841Seric 279968693Seric struct passwd * 280068693Seric sm_getpwnam(user) 280168704Seric char *user; 280268693Seric { 280368693Seric return getpwnam(user); 280468693Seric } 280568693Seric 280668693Seric struct passwd * 280768693Seric sm_getpwuid(uid) 280868693Seric uid_t uid; 280968693Seric { 281068693Seric return getpwuid(uid); 281168693Seric } 281265214Seric /* 281365214Seric ** NI_PROPVAL -- netinfo property value lookup routine 281465214Seric ** 281565214Seric ** Parameters: 281668508Seric ** keydir -- the Netinfo directory name in which to search 281768508Seric ** for the key. 281868508Seric ** keyprop -- the name of the property in which to find the 281968508Seric ** property we are interested. Defaults to "name". 282068508Seric ** keyval -- the value for which we are really searching. 282168508Seric ** valprop -- the property name for the value in which we 282268508Seric ** are interested. 282368508Seric ** sepchar -- if non-nil, this can be multiple-valued, and 282468508Seric ** we should return a string separated by this 282568508Seric ** character. 282665214Seric ** 282765214Seric ** Returns: 282865214Seric ** NULL -- if: 282965214Seric ** 1. the directory is not found 283065214Seric ** 2. the property name is not found 283165214Seric ** 3. the property contains multiple values 283265214Seric ** 4. some error occured 283365214Seric ** else -- the location of the config file. 283465214Seric ** 283568508Seric ** Example: 283668508Seric ** To search for an alias value, use: 283768508Seric ** ni_propval("/aliases", "name", aliasname, "members", ',') 283868508Seric ** 283965214Seric ** Notes: 284065214Seric ** Caller should free the return value of ni_proval 284165214Seric */ 284265214Seric 284365214Seric #ifdef NETINFO 284465214Seric 284565214Seric # include <netinfo/ni.h> 284665214Seric 284765214Seric # define LOCAL_NETINFO_DOMAIN "." 284865214Seric # define PARENT_NETINFO_DOMAIN ".." 284965214Seric # define MAX_NI_LEVELS 256 285065214Seric 285165214Seric char * 285268508Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar) 285368508Seric char *keydir; 285468508Seric char *keyprop; 285568508Seric char *keyval; 285668508Seric char *valprop; 285768508Seric char sepchar; 285865214Seric { 285965820Seric char *propval = NULL; 286065214Seric int i; 286168508Seric int j, alen; 286265214Seric void *ni = NULL; 286365214Seric void *lastni = NULL; 286465214Seric ni_status nis; 286565214Seric ni_id nid; 286665214Seric ni_namelist ninl; 286768508Seric register char *p; 286868508Seric char keybuf[1024]; 286965214Seric 287065214Seric /* 287168508Seric ** Create the full key from the two parts. 287268508Seric ** 287368508Seric ** Note that directory can end with, e.g., "name=" to specify 287468508Seric ** an alternate search property. 287568508Seric */ 287668508Seric 287768508Seric i = strlen(keydir) + strlen(keyval) + 2; 287868508Seric if (keyprop != NULL) 287968508Seric i += strlen(keyprop) + 1; 288068508Seric if (i > sizeof keybuf) 288168508Seric return NULL; 288268508Seric strcpy(keybuf, keydir); 288368508Seric strcat(keybuf, "/"); 288468508Seric if (keyprop != NULL) 288568508Seric { 288668508Seric strcat(keybuf, keyprop); 288768508Seric strcat(keybuf, "="); 288868508Seric } 288968508Seric strcat(keybuf, keyval); 289068508Seric 289168508Seric /* 289265214Seric ** If the passed directory and property name are found 289365214Seric ** in one of netinfo domains we need to search (starting 289465214Seric ** from the local domain moving all the way back to the 289565214Seric ** root domain) set propval to the property's value 289665214Seric ** and return it. 289765214Seric */ 289865214Seric 289965214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 290065214Seric { 290165214Seric if (i == 0) 290265214Seric { 290365214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 290465214Seric } 290565214Seric else 290665214Seric { 290765214Seric if (lastni != NULL) 290865214Seric ni_free(lastni); 290965214Seric lastni = ni; 291065214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 291165214Seric } 291265214Seric 291365214Seric /* 291465214Seric ** Don't bother if we didn't get a handle on a 291565214Seric ** proper domain. This is not necessarily an error. 291665214Seric ** We would get a positive ni_status if, for instance 291765214Seric ** we never found the directory or property and tried 291865214Seric ** to open the parent of the root domain! 291965214Seric */ 292065214Seric 292165214Seric if (nis != 0) 292265214Seric break; 292365214Seric 292465214Seric /* 292565214Seric ** Find the path to the server information. 292665214Seric */ 292765214Seric 292868508Seric if (ni_pathsearch(ni, &nid, keybuf) != 0) 292965214Seric continue; 293065214Seric 293165214Seric /* 293268508Seric ** Find associated value information. 293365214Seric */ 293465214Seric 293568508Seric if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0) 293665214Seric continue; 293765214Seric 293865214Seric /* 293968508Seric ** See if we have an acceptable number of values. 294065214Seric */ 294165214Seric 294268508Seric if (ninl.ni_namelist_len <= 0) 294368508Seric continue; 294468508Seric 294568508Seric if (sepchar == '\0' && ninl.ni_namelist_len > 1) 294665214Seric { 294768508Seric ni_namelist_free(&ninl); 294868508Seric continue; 294965214Seric } 295068508Seric 295168508Seric /* 295268508Seric ** Calculate number of bytes needed and build result 295368508Seric */ 295468508Seric 295568508Seric alen = 1; 295668508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 295768508Seric alen += strlen(ninl.ni_namelist_val[j]) + 1; 295868508Seric propval = p = xalloc(alen); 295968508Seric for (j = 0; j < ninl.ni_namelist_len; j++) 296068508Seric { 296168508Seric strcpy(p, ninl.ni_namelist_val[j]); 296268508Seric p += strlen(p); 296368508Seric *p++ = sepchar; 296468508Seric } 296568508Seric *--p = '\0'; 296668508Seric 296768508Seric ni_namelist_free(&ninl); 296865214Seric } 296965214Seric 297065214Seric /* 297165214Seric ** Clean up. 297265214Seric */ 297365214Seric 297465214Seric if (ni != NULL) 297565214Seric ni_free(ni); 297665214Seric if (lastni != NULL && ni != lastni) 297765214Seric ni_free(lastni); 297865214Seric 297965214Seric return propval; 298065214Seric } 298165214Seric 298265214Seric #endif /* NETINFO */ 298365504Seric /* 298465504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 298565504Seric ** 298665504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 298765504Seric ** syslog succeeds during interrupt handlers. 298865504Seric */ 298965504Seric 299065504Seric #ifdef __hpux 299165504Seric 299265504Seric # define MAXSYSLOGTRIES 100 299365504Seric # undef syslog 299465504Seric 299565504Seric # ifdef __STDC__ 299665504Seric hard_syslog(int pri, char *msg, ...) 299765504Seric # else 299865504Seric hard_syslog(pri, msg, va_alist) 299965504Seric int pri; 300065504Seric char *msg; 300165504Seric va_dcl 300265504Seric # endif 300365504Seric { 300465504Seric int i; 300565504Seric char buf[SYSLOG_BUFSIZE * 2]; 300665504Seric VA_LOCAL_DECL; 300765504Seric 300865504Seric VA_START(msg); 300965504Seric vsprintf(buf, msg, ap); 301065504Seric VA_END; 301165504Seric 301265504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 301365504Seric continue; 301465504Seric } 301565504Seric 301665504Seric #endif 301768696Seric /* 301868696Seric ** LOCAL_HOSTNAME_LENGTH 301968696Seric ** 302068696Seric ** This is required to get sendmail to compile against BIND 4.9.x 302168696Seric ** on Ultrix. 302268696Seric */ 302368696Seric 302468696Seric #if defined(ultrix) && NAMED_BIND 302568696Seric 302668696Seric # include <resolv.h> 302768696Seric # if __RES >= 19931104 302868696Seric 302968696Seric int 303068696Seric local_hostname_length(hostname) 303168696Seric char *hostname; 303268696Seric { 303368696Seric int len_host, len_domain; 303468696Seric 303568696Seric if (!*_res.defdname) 303668696Seric res_init(); 303768696Seric len_host = strlen(hostname); 303868696Seric len_domain = strlen(_res.defdname); 303968696Seric if (len_host > len_domain && 304068696Seric (strcasecmp(hostname + len_host - len_domain,_res.defdname) == 0) && 304168696Seric hostname[len_host - len_domain - 1] == '.') 304268696Seric return len_host - len_domain - 1; 304368696Seric else 304468696Seric return 0; 304568696Seric } 304668696Seric 304768696Seric # endif 304868696Seric #endif 3049