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*68461Seric static char sccsid[] = "@(#)conf.c 8.135 (Berkeley) 02/28/95"; 1133728Sbostic #endif /* not lint */ 1222698Sdist 1363937Seric # include "sendmail.h" 1463937Seric # include "pathnames.h" 1514881Seric # include <sys/ioctl.h> 1658082Seric # include <sys/param.h> 1765018Seric # include <netdb.h> 1836928Sbostic # include <pwd.h> 19404Seric 20294Seric /* 213309Seric ** CONF.C -- Sendmail Configuration Tables. 22294Seric ** 23294Seric ** Defines the configuration of this installation. 24294Seric ** 251388Seric ** Configuration Variables: 262897Seric ** HdrInfo -- a table describing well-known header fields. 272897Seric ** Each entry has the field name and some flags, 284147Seric ** which are described in sendmail.h. 294093Seric ** 304093Seric ** Notes: 314093Seric ** I have tried to put almost all the reasonable 324093Seric ** configuration information into the configuration 334093Seric ** file read at runtime. My intent is that anything 344093Seric ** here is a function of the version of UNIX you 354093Seric ** are running, or is really static -- for example 364093Seric ** the headers are a superset of widely used 374093Seric ** protocols. If you find yourself playing with 384093Seric ** this file too much, you may be making a mistake! 39294Seric */ 40294Seric 41294Seric 42294Seric 43294Seric 444437Seric /* 452897Seric ** Header info table 463057Seric ** Final (null) entry contains the flags used for any other field. 474147Seric ** 484147Seric ** Not all of these are actually handled specially by sendmail 494147Seric ** at this time. They are included as placeholders, to let 504147Seric ** you know that "someday" I intend to have sendmail do 514147Seric ** something with them. 522897Seric */ 532897Seric 542897Seric struct hdrinfo HdrInfo[] = 552897Seric { 568060Seric /* originator fields, most to least significant */ 57*68461Seric "resent-sender", H_FROM|H_RESENT, 58*68461Seric "resent-from", H_FROM|H_RESENT, 59*68461Seric "resent-reply-to", H_FROM|H_RESENT, 60*68461Seric "sender", H_FROM, 61*68461Seric "from", H_FROM, 62*68461Seric "reply-to", H_FROM, 63*68461Seric "full-name", H_ACHECK, 64*68461Seric "return-receipt-to", H_FROM|H_RECEIPTTO, 65*68461Seric "errors-to", H_FROM|H_ERRORSTO, 6658796Seric 678060Seric /* destination fields */ 68*68461Seric "to", H_RCPT, 69*68461Seric "resent-to", H_RCPT|H_RESENT, 70*68461Seric "cc", H_RCPT, 71*68461Seric "resent-cc", H_RCPT|H_RESENT, 72*68461Seric "bcc", H_RCPT|H_STRIPVAL, 73*68461Seric "resent-bcc", H_RCPT|H_STRIPVAL|H_RESENT, 74*68461Seric "apparently-to", H_RCPT, 7558796Seric 768060Seric /* message identification and control */ 77*68461Seric "message-id", 0, 78*68461Seric "resent-message-id", H_RESENT, 79*68461Seric "message", H_EOH, 80*68461Seric "text", H_EOH, 8158796Seric 8211417Seric /* date fields */ 83*68461Seric "date", 0, 84*68461Seric "resent-date", H_RESENT, 8558796Seric 868060Seric /* trace fields */ 87*68461Seric "received", H_TRACE|H_FORCE, 88*68461Seric "x400-received", H_TRACE|H_FORCE, 89*68461Seric "via", H_TRACE|H_FORCE, 90*68461Seric "mail-from", H_TRACE|H_FORCE, 918060Seric 9258796Seric /* miscellaneous fields */ 93*68461Seric "comments", H_FORCE, 94*68461Seric "return-path", H_FORCE|H_ACHECK, 95*68461Seric "content-transfer-encoding", H_CTE, 96*68461Seric "content-type", H_CTYPE, 9758796Seric 989055Seric NULL, 0, 992897Seric }; 1004166Seric 1014166Seric 1024166Seric 1034282Seric /* 1044282Seric ** Location of system files/databases/etc. 1054282Seric */ 1064282Seric 10758082Seric char *PidFile = _PATH_SENDMAILPID; /* stores daemon proc id */ 1089039Seric 1099064Seric 1109064Seric 1119039Seric /* 11258082Seric ** Privacy values 11358082Seric */ 11458082Seric 11558082Seric struct prival PrivacyValues[] = 11658082Seric { 11758082Seric "public", PRIV_PUBLIC, 11858082Seric "needmailhelo", PRIV_NEEDMAILHELO, 11958114Seric "needexpnhelo", PRIV_NEEDEXPNHELO, 12058082Seric "needvrfyhelo", PRIV_NEEDVRFYHELO, 12158082Seric "noexpn", PRIV_NOEXPN, 12258082Seric "novrfy", PRIV_NOVRFY, 12364333Seric "restrictmailq", PRIV_RESTRICTMAILQ, 12464333Seric "restrictqrun", PRIV_RESTRICTQRUN, 12558789Seric "authwarnings", PRIV_AUTHWARNINGS, 12666783Seric "noreceipts", PRIV_NORECEIPTS, 12758082Seric "goaway", PRIV_GOAWAY, 12858789Seric NULL, 0, 12958082Seric }; 13058082Seric 13158082Seric 13258082Seric 13358082Seric /* 13424943Seric ** Miscellaneous stuff. 1359039Seric */ 1369039Seric 13724943Seric int DtableSize = 50; /* max open files; reset in 4.2bsd */ 13824943Seric /* 13924943Seric ** SETDEFAULTS -- set default values 14024943Seric ** 14124943Seric ** Because of the way freezing is done, these must be initialized 14224943Seric ** using direct code. 14324943Seric ** 14424943Seric ** Parameters: 14558734Seric ** e -- the default envelope. 14624943Seric ** 14724943Seric ** Returns: 14824943Seric ** none. 14924943Seric ** 15024943Seric ** Side Effects: 15124943Seric ** Initializes a bunch of global variables to their 15224943Seric ** default values. 15324943Seric */ 15424943Seric 15558737Seric #define DAYS * 24 * 60 * 60 15658737Seric 157*68461Seric void 15858734Seric setdefaults(e) 15958734Seric register ENVELOPE *e; 16024943Seric { 161*68461Seric int i; 162*68461Seric extern void inittimeouts(); 163*68461Seric extern void setdefuser(); 164*68461Seric extern void setupmaps(); 165*68461Seric extern void setupmailers(); 166*68461Seric 16757438Seric SpaceSub = ' '; /* option B */ 16857438Seric QueueLA = 8; /* option x */ 16957438Seric RefuseLA = 12; /* option X */ 17057438Seric WkRecipFact = 30000L; /* option y */ 17157438Seric WkClassFact = 1800L; /* option z */ 17257438Seric WkTimeFact = 90000L; /* option Z */ 17357438Seric QueueFactor = WkRecipFact * 20; /* option q */ 17463787Seric FileMode = (RealUid != geteuid()) ? 0644 : 0600; 17557438Seric /* option F */ 17657438Seric DefUid = 1; /* option u */ 17757438Seric DefGid = 1; /* option g */ 17857438Seric CheckpointInterval = 10; /* option C */ 17957438Seric MaxHopCount = 25; /* option h */ 18058734Seric e->e_sendmode = SM_FORK; /* option d */ 18158734Seric e->e_errormode = EM_PRINT; /* option e */ 182*68461Seric SevenBitInput = FALSE; /* option 7 */ 18357438Seric MaxMciCache = 1; /* option k */ 18457438Seric MciCacheTimeout = 300; /* option K */ 18557438Seric LogLevel = 9; /* option L */ 186*68461Seric inittimeouts(NULL); /* option r */ 18758853Seric PrivacyFlags = 0; /* option p */ 188*68461Seric MimeMode = MM_CVTMIME|MM_PASS8BIT; /* option 8 */ 189*68461Seric for (i = 0; i < MAXTOCLASS; i++) 190*68461Seric { 191*68461Seric TimeOuts.to_q_return[i] = 5 DAYS; /* option T */ 192*68461Seric TimeOuts.to_q_warning[i] = 0; /* option T */ 193*68461Seric } 194*68461Seric ServiceSwitchFile = "/etc/service.switch"; 19540973Sbostic setdefuser(); 19653654Seric setupmaps(); 19757402Seric setupmailers(); 19824943Seric } 199294Seric 20040973Sbostic 2014326Seric /* 20240973Sbostic ** SETDEFUSER -- set/reset DefUser using DefUid (for initgroups()) 20340973Sbostic */ 20440973Sbostic 205*68461Seric void 20640973Sbostic setdefuser() 20740973Sbostic { 20840973Sbostic struct passwd *defpwent; 20957386Seric static char defuserbuf[40]; 21040973Sbostic 21157386Seric DefUser = defuserbuf; 21240973Sbostic if ((defpwent = getpwuid(DefUid)) != NULL) 21357386Seric strcpy(defuserbuf, defpwent->pw_name); 21440973Sbostic else 21557386Seric strcpy(defuserbuf, "nobody"); 21640973Sbostic } 21753654Seric /* 21856836Seric ** HOST_MAP_INIT -- initialize host class structures 21956836Seric */ 22056836Seric 221*68461Seric bool host_map_init __P((MAP *map, char *args)); 222*68461Seric 22356836Seric bool 22460219Seric host_map_init(map, args) 22556836Seric MAP *map; 22656836Seric char *args; 22756836Seric { 22856836Seric register char *p = args; 22956836Seric 23056836Seric for (;;) 23156836Seric { 23258050Seric while (isascii(*p) && isspace(*p)) 23356836Seric p++; 23456836Seric if (*p != '-') 23556836Seric break; 23656836Seric switch (*++p) 23756836Seric { 23856836Seric case 'a': 23956836Seric map->map_app = ++p; 24056836Seric break; 24156836Seric } 24258050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 24356836Seric p++; 24456836Seric if (*p != '\0') 24556836Seric *p++ = '\0'; 24656836Seric } 24756836Seric if (map->map_app != NULL) 24856836Seric map->map_app = newstr(map->map_app); 24956836Seric return TRUE; 25056836Seric } 25157402Seric /* 25257402Seric ** SETUPMAILERS -- initialize default mailers 25357402Seric */ 25456836Seric 255*68461Seric void 25657402Seric setupmailers() 25757402Seric { 25857402Seric char buf[100]; 259*68461Seric extern void makemailer(); 26057402Seric 261*68461Seric strcpy(buf, "prog, P=/bin/sh, F=lsoD, A=sh -c $u"); 26257403Seric makemailer(buf); 26357403Seric 264*68461Seric strcpy(buf, "*file*, P=/dev/null, F=lsDFMPEou, A=FILE"); 26557402Seric makemailer(buf); 26657402Seric 26757402Seric strcpy(buf, "*include*, P=/dev/null, F=su, A=INCLUDE"); 26857402Seric makemailer(buf); 26957402Seric } 27056836Seric /* 27160207Seric ** SETUPMAPS -- set up map classes 27260207Seric */ 27360207Seric 27460207Seric #define MAPDEF(name, ext, flags, parse, open, close, lookup, store) \ 27560207Seric { \ 27660207Seric extern bool parse __P((MAP *, char *)); \ 27760207Seric extern bool open __P((MAP *, int)); \ 27860207Seric extern void close __P((MAP *)); \ 27960207Seric extern char *lookup __P((MAP *, char *, char **, int *)); \ 28060207Seric extern void store __P((MAP *, char *, char *)); \ 28160207Seric s = stab(name, ST_MAPCLASS, ST_ENTER); \ 28260207Seric s->s_mapclass.map_cname = name; \ 28360207Seric s->s_mapclass.map_ext = ext; \ 28460207Seric s->s_mapclass.map_cflags = flags; \ 28560207Seric s->s_mapclass.map_parse = parse; \ 28660207Seric s->s_mapclass.map_open = open; \ 28760207Seric s->s_mapclass.map_close = close; \ 28860207Seric s->s_mapclass.map_lookup = lookup; \ 28960207Seric s->s_mapclass.map_store = store; \ 29060207Seric } 29160207Seric 292*68461Seric void 29360207Seric setupmaps() 29460207Seric { 29560207Seric register STAB *s; 29660207Seric 29760207Seric #ifdef NEWDB 29860207Seric MAPDEF("hash", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 29960207Seric map_parseargs, hash_map_open, db_map_close, 30060207Seric db_map_lookup, db_map_store); 301*68461Seric 30260207Seric MAPDEF("btree", ".db", MCF_ALIASOK|MCF_REBUILDABLE, 30360207Seric map_parseargs, bt_map_open, db_map_close, 30460207Seric db_map_lookup, db_map_store); 30560207Seric #endif 30660207Seric 30760207Seric #ifdef NDBM 30860207Seric MAPDEF("dbm", ".dir", MCF_ALIASOK|MCF_REBUILDABLE, 30960207Seric map_parseargs, ndbm_map_open, ndbm_map_close, 31060207Seric ndbm_map_lookup, ndbm_map_store); 31160207Seric #endif 31260207Seric 31360207Seric #ifdef NIS 31460207Seric MAPDEF("nis", NULL, MCF_ALIASOK, 315*68461Seric map_parseargs, nis_map_open, null_map_close, 316*68461Seric nis_map_lookup, null_map_store); 31760207Seric #endif 31860207Seric 319*68461Seric #ifdef NISPLUS 320*68461Seric MAPDEF("nisplus", NULL, MCF_ALIASOK, 321*68461Seric map_parseargs, nisplus_map_open, null_map_close, 322*68461Seric nisplus_map_lookup, null_map_store); 323*68461Seric #endif 324*68461Seric 325*68461Seric #ifdef HESIOD 326*68461Seric MAPDEF("hesiod", NULL, MCF_ALIASOK|MCF_ALIASONLY, 327*68461Seric map_parseargs, null_map_open, null_map_close, 328*68461Seric hes_map_lookup, null_map_store); 329*68461Seric #endif 330*68461Seric 331*68461Seric #ifdef NETINFO 332*68461Seric MAPDEF("netinfo", NULL, MCF_ALIASOK, 333*68461Seric map_parseargs, ni_map_open, null_map_close, 334*68461Seric ni_map_lookup, null_map_store); 335*68461Seric #endif 336*68461Seric 337*68461Seric #if 0 338*68461Seric MAPDEF("dns", NULL, 0, 339*68461Seric dns_map_init, null_map_open, null_map_close, 340*68461Seric dns_map_lookup, null_map_store); 341*68461Seric #endif 342*68461Seric 343*68461Seric #if NAMED_BIND 344*68461Seric /* best MX DNS lookup */ 345*68461Seric MAPDEF("bestmx", NULL, MCF_OPTFILE, 346*68461Seric map_parseargs, null_map_open, null_map_close, 347*68461Seric bestmx_map_lookup, null_map_store); 348*68461Seric #endif 349*68461Seric 350*68461Seric MAPDEF("host", NULL, 0, 351*68461Seric host_map_init, null_map_open, null_map_close, 352*68461Seric host_map_lookup, null_map_store); 353*68461Seric 354*68461Seric MAPDEF("text", NULL, MCF_ALIASOK, 355*68461Seric map_parseargs, text_map_open, null_map_close, 356*68461Seric text_map_lookup, null_map_store); 357*68461Seric 35860207Seric MAPDEF("stab", NULL, MCF_ALIASOK|MCF_ALIASONLY, 359*68461Seric map_parseargs, stab_map_open, null_map_close, 36060207Seric stab_map_lookup, stab_map_store); 36160207Seric 36260207Seric MAPDEF("implicit", NULL, MCF_ALIASOK|MCF_ALIASONLY|MCF_REBUILDABLE, 36360207Seric map_parseargs, impl_map_open, impl_map_close, 36460207Seric impl_map_lookup, impl_map_store); 36560207Seric 366*68461Seric /* access to system passwd file */ 367*68461Seric MAPDEF("user", NULL, MCF_OPTFILE, 368*68461Seric map_parseargs, user_map_open, null_map_close, 369*68461Seric user_map_lookup, null_map_store); 37060207Seric 37160207Seric /* dequote map */ 37260207Seric MAPDEF("dequote", NULL, 0, 37360207Seric dequote_init, null_map_open, null_map_close, 37460207Seric dequote_map, null_map_store); 37560207Seric 37660207Seric #if 0 37760207Seric # ifdef USERDB 37860207Seric /* user database */ 37960207Seric MAPDEF("udb", ".db", 0, 38060207Seric udb_map_parse, null_map_open, null_map_close, 38160207Seric udb_map_lookup, null_map_store); 38260207Seric # endif 38360207Seric #endif 384*68461Seric 385*68461Seric /* sequenced maps */ 386*68461Seric MAPDEF("sequence", NULL, MCF_ALIASOK, 387*68461Seric seq_map_parse, null_map_open, null_map_close, 388*68461Seric seq_map_lookup, seq_map_store); 389*68461Seric 390*68461Seric /* switched interface to sequenced maps */ 391*68461Seric MAPDEF("switch", NULL, MCF_ALIASOK, 392*68461Seric map_parseargs, switch_map_open, null_map_close, 393*68461Seric seq_map_lookup, seq_map_store); 39460207Seric } 39560207Seric 39660207Seric #undef MAPDEF 39760207Seric /* 398*68461Seric ** INITHOSTMAPS -- initial host-dependent maps 399*68461Seric ** 400*68461Seric ** This should act as an interface to any local service switch 401*68461Seric ** provided by the host operating system. 402*68461Seric ** 403*68461Seric ** Parameters: 404*68461Seric ** none 405*68461Seric ** 406*68461Seric ** Returns: 407*68461Seric ** none 408*68461Seric ** 409*68461Seric ** Side Effects: 410*68461Seric ** Should define maps "host" and "users" as necessary 411*68461Seric ** for this OS. If they are not defined, they will get 412*68461Seric ** a default value later. It should check to make sure 413*68461Seric ** they are not defined first, since it's possible that 414*68461Seric ** the config file has provided an override. 415*68461Seric */ 416*68461Seric 417*68461Seric void 418*68461Seric inithostmaps() 419*68461Seric { 420*68461Seric register int i; 421*68461Seric int nmaps; 422*68461Seric char *maptype[MAXMAPSTACK]; 423*68461Seric short mapreturn[MAXMAPACTIONS]; 424*68461Seric char buf[MAXLINE]; 425*68461Seric 426*68461Seric /* 427*68461Seric ** Set up default hosts maps. 428*68461Seric */ 429*68461Seric 430*68461Seric #if 0 431*68461Seric nmaps = switch_map_find("hosts", maptype, mapreturn); 432*68461Seric for (i = 0; i < nmaps; i++) 433*68461Seric { 434*68461Seric if (strcmp(maptype[i], "files") == 0 && 435*68461Seric stab("hosts.files", ST_MAP, ST_FIND) == NULL) 436*68461Seric { 437*68461Seric strcpy(buf, "hosts.files text -k 0 -v 1 /etc/hosts"); 438*68461Seric makemapentry(buf); 439*68461Seric } 440*68461Seric #if NAMED_BIND 441*68461Seric else if (strcmp(maptype[i], "dns") == 0 && 442*68461Seric stab("hosts.dns", ST_MAP, ST_FIND) == NULL) 443*68461Seric { 444*68461Seric strcpy(buf, "hosts.dns dns A"); 445*68461Seric makemapentry(buf); 446*68461Seric } 447*68461Seric #endif 448*68461Seric #ifdef NISPLUS 449*68461Seric else if (strcmp(maptype[i], "nisplus") == 0 && 450*68461Seric stab("hosts.nisplus", ST_MAP, ST_FIND) == NULL) 451*68461Seric { 452*68461Seric strcpy(buf, "hosts.nisplus nisplus -k name -v address -d hosts.org_dir"); 453*68461Seric makemapentry(buf); 454*68461Seric } 455*68461Seric #endif 456*68461Seric #ifdef NIS 457*68461Seric else if (strcmp(maptype[i], "nis") == 0 && 458*68461Seric stab("hosts.nis", ST_MAP, ST_FIND) == NULL) 459*68461Seric { 460*68461Seric strcpy(buf, "hosts.nis nis -d -k 0 -v 1 hosts.byname"); 461*68461Seric makemapentry(buf); 462*68461Seric } 463*68461Seric #endif 464*68461Seric } 465*68461Seric #endif 466*68461Seric 467*68461Seric /* 468*68461Seric ** Make sure we have a host map. 469*68461Seric */ 470*68461Seric 471*68461Seric if (stab("host", ST_MAP, ST_FIND) == NULL) 472*68461Seric { 473*68461Seric /* user didn't initialize: set up host map */ 474*68461Seric strcpy(buf, "host host"); 475*68461Seric #if NAMED_BIND 476*68461Seric if (ConfigLevel >= 2) 477*68461Seric strcat(buf, " -a."); 478*68461Seric #endif 479*68461Seric makemapentry(buf); 480*68461Seric } 481*68461Seric 482*68461Seric /* 483*68461Seric ** Set up default aliases maps 484*68461Seric */ 485*68461Seric 486*68461Seric nmaps = switch_map_find("aliases", maptype, mapreturn); 487*68461Seric for (i = 0; i < nmaps; i++) 488*68461Seric { 489*68461Seric if (strcmp(maptype[i], "files") == 0 && 490*68461Seric stab("aliases.files", ST_MAP, ST_FIND) == NULL) 491*68461Seric { 492*68461Seric strcpy(buf, "aliases.files implicit /etc/aliases"); 493*68461Seric makemapentry(buf); 494*68461Seric } 495*68461Seric #ifdef NISPLUS 496*68461Seric else if (strcmp(maptype[i], "nisplus") == 0 && 497*68461Seric stab("aliases.nisplus", ST_MAP, ST_FIND) == NULL) 498*68461Seric { 499*68461Seric strcpy(buf, "aliases.nisplus nisplus -kalias -vexpansion -d mail_aliases.org_dir"); 500*68461Seric makemapentry(buf); 501*68461Seric } 502*68461Seric #endif 503*68461Seric #ifdef NIS 504*68461Seric else if (strcmp(maptype[i], "nis") == 0 && 505*68461Seric stab("aliases.nis", ST_MAP, ST_FIND) == NULL) 506*68461Seric { 507*68461Seric strcpy(buf, "aliases.nis nis -d mail.aliases"); 508*68461Seric makemapentry(buf); 509*68461Seric } 510*68461Seric #endif 511*68461Seric } 512*68461Seric if (stab("aliases", ST_MAP, ST_FIND) == NULL) 513*68461Seric { 514*68461Seric strcpy(buf, "aliases switch aliases"); 515*68461Seric makemapentry(buf); 516*68461Seric } 517*68461Seric strcpy(buf, "switch:aliases"); 518*68461Seric setalias(buf); 519*68461Seric 520*68461Seric #if 0 /* "user" map class is a better choice */ 521*68461Seric /* 522*68461Seric ** Set up default users maps. 523*68461Seric */ 524*68461Seric 525*68461Seric nmaps = switch_map_find("passwd", maptype, mapreturn); 526*68461Seric for (i = 0; i < nmaps; i++) 527*68461Seric { 528*68461Seric if (strcmp(maptype[i], "files") == 0 && 529*68461Seric stab("users.files", ST_MAP, ST_FIND) == NULL) 530*68461Seric { 531*68461Seric strcpy(buf, "users.files text -m -z: -k0 -v6 /etc/passwd"); 532*68461Seric makemapentry(buf); 533*68461Seric } 534*68461Seric #ifdef NISPLUS 535*68461Seric else if (strcmp(maptype[i], "nisplus") == 0 && 536*68461Seric stab("users.nisplus", ST_MAP, ST_FIND) == NULL) 537*68461Seric { 538*68461Seric strcpy(buf, "users.nisplus nisplus -m -kname -vhome -d passwd.org_dir"); 539*68461Seric makemapentry(buf); 540*68461Seric } 541*68461Seric #endif 542*68461Seric #ifdef NIS 543*68461Seric else if (strcmp(maptype[i], "nis") == 0 && 544*68461Seric stab("users.nis", ST_MAP, ST_FIND) == NULL) 545*68461Seric { 546*68461Seric strcpy(buf, "users.nis nis -m -d passwd.byname"); 547*68461Seric makemapentry(buf); 548*68461Seric } 549*68461Seric #endif 550*68461Seric #ifdef HESIOD 551*68461Seric else if (strcmp(maptype[i], "hesiod") == 0) && 552*68461Seric stab("users.hesiod", ST_MAP, ST_FIND) == NULL) 553*68461Seric { 554*68461Seric strcpy(buf, "users.hesiod hesiod"); 555*68461Seric makemapentry(buf); 556*68461Seric } 557*68461Seric #endif 558*68461Seric } 559*68461Seric if (stab("users", ST_MAP, ST_FIND) == NULL) 560*68461Seric { 561*68461Seric strcpy(buf, "users switch -m passwd"); 562*68461Seric makemapentry(buf); 563*68461Seric } 564*68461Seric #endif 565*68461Seric } 566*68461Seric /* 567*68461Seric ** SWITCH_MAP_FIND -- find the list of types associated with a map 568*68461Seric ** 569*68461Seric ** This is the system-dependent interface to the service switch. 570*68461Seric ** 571*68461Seric ** Parameters: 572*68461Seric ** service -- the name of the service of interest. 573*68461Seric ** maptype -- an out-array of strings containing the types 574*68461Seric ** of access to use for this service. There can 575*68461Seric ** be at most MAXMAPSTACK types for a single service. 576*68461Seric ** mapreturn -- an out-array of return information bitmaps 577*68461Seric ** for the map. 578*68461Seric ** 579*68461Seric ** Returns: 580*68461Seric ** The number of map types filled in, or -1 for failure. 581*68461Seric */ 582*68461Seric 583*68461Seric #ifdef SOLARIS 584*68461Seric # include <nsswitch.h> 585*68461Seric #endif 586*68461Seric 587*68461Seric #if defined(ultrix) || defined(__osf__) 588*68461Seric # include <sys/svcinfo.h> 589*68461Seric #endif 590*68461Seric 591*68461Seric int 592*68461Seric switch_map_find(service, maptype, mapreturn) 593*68461Seric char *service; 594*68461Seric char *maptype[MAXMAPSTACK]; 595*68461Seric short mapreturn[MAXMAPACTIONS]; 596*68461Seric { 597*68461Seric register FILE *fp; 598*68461Seric int svcno; 599*68461Seric static char buf[MAXLINE]; 600*68461Seric 601*68461Seric #ifdef SOLARIS 602*68461Seric struct __nsw_switchconfig *nsw_conf; 603*68461Seric enum __nsw_parse_err pserr; 604*68461Seric struct __nsw_lookup *lk; 605*68461Seric int nsw_rc; 606*68461Seric static struct __nsw_lookup lkp0 = 607*68461Seric { "files", {1, 0, 0, 0}, NULL, NULL }; 608*68461Seric static struct __nsw_switchconfig lkp_default = 609*68461Seric { 0, "sendmail", 3, &lkp0 }; 610*68461Seric 611*68461Seric if ((nsw_conf = __nsw_getconfig(service, &pserr)) == NULL) 612*68461Seric lk = lkp_default.lookups; 613*68461Seric else 614*68461Seric lk = nsw_conf->lookups; 615*68461Seric svcno = 0; 616*68461Seric while (lk != NULL) 617*68461Seric { 618*68461Seric maptype[svcno] = lk->service_name; 619*68461Seric if (lk->actions[__NSW_NOTFOUND] == __NSW_RETURN) 620*68461Seric mapreturn[MA_NOTFOUND] |= 1 << svcno; 621*68461Seric if (lk->actions[__NSW_TRYAGAIN] == __NSW_RETURN) 622*68461Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 623*68461Seric if (lk->actions[__NSW_UNAVAIL] == __NSW_RETURN) 624*68461Seric mapreturn[MA_TRYAGAIN] |= 1 << svcno; 625*68461Seric svcno++; 626*68461Seric lk = lk->next; 627*68461Seric } 628*68461Seric return svcno; 629*68461Seric #endif 630*68461Seric 631*68461Seric #if defined(ultrix) || defined(__osf__) 632*68461Seric struct svcinfo *svcinfo; 633*68461Seric int svc; 634*68461Seric 635*68461Seric svcinfo = getsvc(); 636*68461Seric if (svcinfo == NULL) 637*68461Seric goto punt; 638*68461Seric if (strcmp(service, "hosts") == 0) 639*68461Seric svc = SVC_HOSTS; 640*68461Seric else if (strcmp(service, "aliases") == 0) 641*68461Seric svc = SVC_ALIASES; 642*68461Seric else if (strcmp(service, "passwd") == 0) 643*68461Seric svc = SVC_PASSWD; 644*68461Seric else 645*68461Seric return -1; 646*68461Seric for (svcno = 0; svcno < SVC_PATHSIZE; svcno++) 647*68461Seric { 648*68461Seric switch (svcinfo->svcpath[svc][svcno]) 649*68461Seric { 650*68461Seric case SVC_LOCAL: 651*68461Seric maptype[svcno] = "files"; 652*68461Seric break; 653*68461Seric 654*68461Seric case SVC_YP: 655*68461Seric maptype[svcno] = "nis"; 656*68461Seric break; 657*68461Seric 658*68461Seric case SVC_BIND: 659*68461Seric maptype[svcno] = "dns"; 660*68461Seric break; 661*68461Seric 662*68461Seric #ifdef SVC_HESIOD 663*68461Seric case SVC_HESIOD: 664*68461Seric maptype[svcno] = "hesiod"; 665*68461Seric break; 666*68461Seric #endif 667*68461Seric 668*68461Seric case SVC_LAST: 669*68461Seric return svcno; 670*68461Seric } 671*68461Seric } 672*68461Seric return svcno; 673*68461Seric #endif 674*68461Seric 675*68461Seric #if !defined(SOLARIS) && !defined(ultrix) && !defined(__osf__) 676*68461Seric /* 677*68461Seric ** Fall-back mechanism. 678*68461Seric */ 679*68461Seric 680*68461Seric svcno = 0; 681*68461Seric fp = fopen(ServiceSwitchFile, "r"); 682*68461Seric if (fp != NULL) 683*68461Seric { 684*68461Seric while (fgets(buf, sizeof buf, fp) != NULL) 685*68461Seric { 686*68461Seric register char *p; 687*68461Seric 688*68461Seric p = strpbrk(buf, "#\n"); 689*68461Seric if (p != NULL) 690*68461Seric *p = '\0'; 691*68461Seric p = strpbrk(buf, " \t"); 692*68461Seric if (p != NULL) 693*68461Seric *p++ = '\0'; 694*68461Seric if (strcmp(buf, service) != 0) 695*68461Seric continue; 696*68461Seric 697*68461Seric /* got the right service -- extract data */ 698*68461Seric do 699*68461Seric { 700*68461Seric while (isspace(*p)) 701*68461Seric p++; 702*68461Seric if (*p == '\0') 703*68461Seric break; 704*68461Seric maptype[svcno++] = p; 705*68461Seric p = strpbrk(p, " \t"); 706*68461Seric if (p != NULL) 707*68461Seric *p++ = '\0'; 708*68461Seric } while (p != NULL); 709*68461Seric break; 710*68461Seric } 711*68461Seric fclose(fp); 712*68461Seric return svcno; 713*68461Seric } 714*68461Seric #endif 715*68461Seric 716*68461Seric /* if the service file doesn't work, use an absolute fallback */ 717*68461Seric punt: 718*68461Seric if (strcmp(service, "aliases") == 0) 719*68461Seric { 720*68461Seric maptype[0] = "files"; 721*68461Seric return 1; 722*68461Seric } 723*68461Seric if (strcmp(service, "hosts") == 0) 724*68461Seric { 725*68461Seric # if NAMED_BIND 726*68461Seric maptype[svcno++] = "dns"; 727*68461Seric # else 728*68461Seric # if defined(sun) && !defined(BSD) && !defined(SOLARIS) 729*68461Seric /* SunOS */ 730*68461Seric maptype[svcno++] = "nis"; 731*68461Seric # endif 732*68461Seric # endif 733*68461Seric maptype[svcno++] = "files"; 734*68461Seric return svcno; 735*68461Seric } 736*68461Seric return -1; 737*68461Seric } 738*68461Seric /* 7399369Seric ** USERNAME -- return the user id of the logged in user. 7409369Seric ** 7419369Seric ** Parameters: 7429369Seric ** none. 7439369Seric ** 7449369Seric ** Returns: 7459369Seric ** The login name of the logged in user. 7469369Seric ** 7479369Seric ** Side Effects: 7489369Seric ** none. 7499369Seric ** 7509369Seric ** Notes: 7519369Seric ** The return value is statically allocated. 7529369Seric */ 7539369Seric 7549369Seric char * 7559369Seric username() 7569369Seric { 75717469Seric static char *myname = NULL; 7589369Seric extern char *getlogin(); 75919904Smiriam register struct passwd *pw; 7609369Seric 76117469Seric /* cache the result */ 76217469Seric if (myname == NULL) 76317469Seric { 76417469Seric myname = getlogin(); 76517469Seric if (myname == NULL || myname[0] == '\0') 76617469Seric { 76763787Seric pw = getpwuid(RealUid); 76817469Seric if (pw != NULL) 76940993Sbostic myname = newstr(pw->pw_name); 77017469Seric } 77119904Smiriam else 77219904Smiriam { 77363787Seric uid_t uid = RealUid; 77419873Smiriam 77540993Sbostic myname = newstr(myname); 77640993Sbostic if ((pw = getpwnam(myname)) == NULL || 77758736Seric (uid != 0 && uid != pw->pw_uid)) 77819904Smiriam { 77958736Seric pw = getpwuid(uid); 78024945Seric if (pw != NULL) 78140993Sbostic myname = newstr(pw->pw_name); 78219873Smiriam } 78319873Smiriam } 78417469Seric if (myname == NULL || myname[0] == '\0') 78517469Seric { 78658151Seric syserr("554 Who are you?"); 78717469Seric myname = "postmaster"; 78817469Seric } 78917469Seric } 79017469Seric 79117469Seric return (myname); 7929369Seric } 7939369Seric /* 7944190Seric ** TTYPATH -- Get the path of the user's tty 795294Seric ** 796294Seric ** Returns the pathname of the user's tty. Returns NULL if 797294Seric ** the user is not logged in or if s/he has write permission 798294Seric ** denied. 799294Seric ** 800294Seric ** Parameters: 801294Seric ** none 802294Seric ** 803294Seric ** Returns: 804294Seric ** pathname of the user's tty. 805294Seric ** NULL if not logged in or write permission denied. 806294Seric ** 807294Seric ** Side Effects: 808294Seric ** none. 809294Seric ** 810294Seric ** WARNING: 811294Seric ** Return value is in a local buffer. 812294Seric ** 813294Seric ** Called By: 814294Seric ** savemail 815294Seric */ 816294Seric 817294Seric char * 818294Seric ttypath() 819294Seric { 820294Seric struct stat stbuf; 821294Seric register char *pathn; 822294Seric extern char *ttyname(); 8234081Seric extern char *getlogin(); 824294Seric 825294Seric /* compute the pathname of the controlling tty */ 8269369Seric if ((pathn = ttyname(2)) == NULL && (pathn = ttyname(1)) == NULL && 8279369Seric (pathn = ttyname(0)) == NULL) 828294Seric { 829294Seric errno = 0; 830294Seric return (NULL); 831294Seric } 832294Seric 833294Seric /* see if we have write permission */ 8342967Seric if (stat(pathn, &stbuf) < 0 || !bitset(02, stbuf.st_mode)) 835294Seric { 836294Seric errno = 0; 837294Seric return (NULL); 838294Seric } 839294Seric 840294Seric /* see if the user is logged in */ 841294Seric if (getlogin() == NULL) 842294Seric return (NULL); 843294Seric 844294Seric /* looks good */ 845294Seric return (pathn); 846294Seric } 8472967Seric /* 8482967Seric ** CHECKCOMPAT -- check for From and To person compatible. 8492967Seric ** 8502967Seric ** This routine can be supplied on a per-installation basis 8512967Seric ** to determine whether a person is allowed to send a message. 8522967Seric ** This allows restriction of certain types of internet 8532967Seric ** forwarding or registration of users. 8542967Seric ** 8552967Seric ** If the hosts are found to be incompatible, an error 85657454Seric ** message should be given using "usrerr" and 0 should 8572967Seric ** be returned. 8582967Seric ** 859*68461Seric ** EF_NORETURN can be set in e->e_flags to suppress the return-to-sender 8604288Seric ** function; this should be done on huge messages. 8614288Seric ** 8622967Seric ** Parameters: 8632967Seric ** to -- the person being sent to. 8642967Seric ** 8652967Seric ** Returns: 86657459Seric ** an exit status 8672967Seric ** 8682967Seric ** Side Effects: 8692967Seric ** none (unless you include the usrerr stuff) 8702967Seric */ 8712967Seric 872*68461Seric int 87355012Seric checkcompat(to, e) 8742967Seric register ADDRESS *to; 87555012Seric register ENVELOPE *e; 8762967Seric { 87712133Seric # ifdef lint 87812133Seric if (to == NULL) 87912133Seric to++; 88063834Seric # endif /* lint */ 88164402Seric 88264402Seric if (tTd(49, 1)) 88364402Seric printf("checkcompat(to=%s, from=%s)\n", 88464402Seric to->q_paddr, e->e_from.q_paddr); 88564402Seric 88610698Seric # ifdef EXAMPLE_CODE 88710698Seric /* this code is intended as an example only */ 8884437Seric register STAB *s; 8894437Seric 8904437Seric s = stab("arpa", ST_MAILER, ST_FIND); 891*68461Seric if (s != NULL && strcmp(e->e_from.q_mailer->m_name, "local") != 0 && 8929369Seric to->q_mailer == s->s_mailer) 8934437Seric { 89458151Seric usrerr("553 No ARPA mail through this machine: see your system administration"); 895*68461Seric /* e->e_flags |= EF_NORETURN; to supress return copy */ 89657459Seric return (EX_UNAVAILABLE); 8974437Seric } 89856795Seric # endif /* EXAMPLE_CODE */ 89957459Seric return (EX_OK); 9002967Seric } 9019369Seric /* 90264035Seric ** SETSIGNAL -- set a signal handler 90364035Seric ** 90464035Seric ** This is essentially old BSD "signal(3)". 90564035Seric */ 90664035Seric 90764561Seric sigfunc_t 90864035Seric setsignal(sig, handler) 90964035Seric int sig; 91064561Seric sigfunc_t handler; 91164035Seric { 91264380Seric #if defined(SYS5SIGNALS) || defined(BSD4_3) || defined(_AUX_SOURCE) 91364035Seric return signal(sig, handler); 91464035Seric #else 91564035Seric struct sigaction n, o; 91664035Seric 91764035Seric bzero(&n, sizeof n); 91864035Seric n.sa_handler = handler; 919*68461Seric # ifdef SA_RESTART 920*68461Seric n.sa_flags = SA_RESTART; 921*68461Seric # endif 92264035Seric if (sigaction(sig, &n, &o) < 0) 92364035Seric return SIG_ERR; 92464035Seric return o.sa_handler; 92564035Seric #endif 92664035Seric } 92764035Seric /* 9289369Seric ** HOLDSIGS -- arrange to hold all signals 9299369Seric ** 9309369Seric ** Parameters: 9319369Seric ** none. 9329369Seric ** 9339369Seric ** Returns: 9349369Seric ** none. 9359369Seric ** 9369369Seric ** Side Effects: 9379369Seric ** Arranges that signals are held. 9389369Seric */ 9399369Seric 940*68461Seric void 9419369Seric holdsigs() 9429369Seric { 9439369Seric } 9449369Seric /* 9459369Seric ** RLSESIGS -- arrange to release all signals 9469369Seric ** 9479369Seric ** This undoes the effect of holdsigs. 9489369Seric ** 9499369Seric ** Parameters: 9509369Seric ** none. 9519369Seric ** 9529369Seric ** Returns: 9539369Seric ** none. 9549369Seric ** 9559369Seric ** Side Effects: 9569369Seric ** Arranges that signals are released. 9579369Seric */ 9589369Seric 959*68461Seric void 9609369Seric rlsesigs() 9619369Seric { 9629369Seric } 96314872Seric /* 96464705Seric ** INIT_MD -- do machine dependent initializations 96564705Seric ** 96664705Seric ** Systems that have global modes that should be set should do 96764705Seric ** them here rather than in main. 96864705Seric */ 96964705Seric 97064705Seric #ifdef _AUX_SOURCE 97164705Seric # include <compat.h> 97264705Seric #endif 97364705Seric 974*68461Seric void 97564825Seric init_md(argc, argv) 97664825Seric int argc; 97764825Seric char **argv; 97864705Seric { 97964705Seric #ifdef _AUX_SOURCE 98064705Seric setcompat(getcompat() | COMPAT_BSDPROT); 98164705Seric #endif 982*68461Seric 983*68461Seric #ifdef VENDOR_DEFAULT 984*68461Seric VendorCode = VENDOR_DEFAULT; 985*68461Seric #else 986*68461Seric VendorCode = VENDOR_BERKELEY; 987*68461Seric #endif 98864705Seric } 98964705Seric /* 99014872Seric ** GETLA -- get the current load average 99114872Seric ** 99214881Seric ** This code stolen from la.c. 99314881Seric ** 99414872Seric ** Parameters: 99514872Seric ** none. 99614872Seric ** 99714872Seric ** Returns: 99814872Seric ** The current load average as an integer. 99914872Seric ** 100014872Seric ** Side Effects: 100114872Seric ** none. 100214872Seric */ 100314872Seric 100451920Seric /* try to guess what style of load average we have */ 100551920Seric #define LA_ZERO 1 /* always return load average as zero */ 100664360Seric #define LA_INT 2 /* read kmem for avenrun; interpret as long */ 100751920Seric #define LA_FLOAT 3 /* read kmem for avenrun; interpret as float */ 100851920Seric #define LA_SUBR 4 /* call getloadavg */ 100964295Seric #define LA_MACH 5 /* MACH load averages (as on NeXT boxes) */ 101064360Seric #define LA_SHORT 6 /* read kmem for avenrun; interpret as short */ 101166301Seric #define LA_PROCSTR 7 /* read string ("1.17") from /proc/loadavg */ 101214872Seric 101363962Seric /* do guesses based on general OS type */ 101451920Seric #ifndef LA_TYPE 101563962Seric # define LA_TYPE LA_ZERO 101651920Seric #endif 101751920Seric 101864360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_FLOAT) || (LA_TYPE == LA_SHORT) 101951920Seric 102014872Seric #include <nlist.h> 102114872Seric 1022*68461Seric #ifdef IRIX64 1023*68461Seric # define nlist nlist64 1024*68461Seric #endif 1025*68461Seric 102651920Seric #ifndef LA_AVENRUN 102763962Seric # ifdef SYSTEM5 102863962Seric # define LA_AVENRUN "avenrun" 102963962Seric # else 103063962Seric # define LA_AVENRUN "_avenrun" 103163962Seric # endif 103251920Seric #endif 103351920Seric 103451920Seric /* _PATH_UNIX should be defined in <paths.h> */ 103551920Seric #ifndef _PATH_UNIX 103663962Seric # if defined(SYSTEM5) 103763962Seric # define _PATH_UNIX "/unix" 103863962Seric # else 103963962Seric # define _PATH_UNIX "/vmunix" 104063962Seric # endif 104151920Seric #endif 104251920Seric 104314872Seric struct nlist Nl[] = 104414872Seric { 104551920Seric { LA_AVENRUN }, 104614872Seric #define X_AVENRUN 0 104714872Seric { 0 }, 104814872Seric }; 104914872Seric 105060583Seric #ifndef FSHIFT 105160583Seric # if defined(unixpc) 105260583Seric # define FSHIFT 5 105360583Seric # endif 105457736Seric 105566763Seric # if defined(__alpha) || defined(IRIX) 105660583Seric # define FSHIFT 10 105760583Seric # endif 1058*68461Seric 1059*68461Seric # if defined(_AIX3) 1060*68461Seric # define FSHIFT 16 1061*68461Seric # endif 106266788Seric #endif 106357977Seric 106466788Seric #ifndef FSHIFT 106566788Seric # define FSHIFT 8 106657736Seric #endif 106760583Seric 106866788Seric #ifndef FSCALE 106966788Seric # define FSCALE (1 << FSHIFT) 107051920Seric #endif 107140930Srick 107214872Seric getla() 107314872Seric { 107414872Seric static int kmem = -1; 107551920Seric #if LA_TYPE == LA_INT 107624943Seric long avenrun[3]; 107751920Seric #else 107864360Seric # if LA_TYPE == LA_SHORT 107964360Seric short avenrun[3]; 108064360Seric # else 108151920Seric double avenrun[3]; 108264360Seric # endif 108351920Seric #endif 108425615Seric extern off_t lseek(); 108557736Seric extern int errno; 108614872Seric 108714872Seric if (kmem < 0) 108814872Seric { 108924945Seric kmem = open("/dev/kmem", 0, 0); 109014872Seric if (kmem < 0) 109157736Seric { 109257736Seric if (tTd(3, 1)) 109357736Seric printf("getla: open(/dev/kmem): %s\n", 109457736Seric errstring(errno)); 109514872Seric return (-1); 109657736Seric } 109751920Seric (void) fcntl(kmem, F_SETFD, 1); 1098*68461Seric #ifdef _AIX3 1099*68461Seric if (knlist(Nl, 1, sizeof Nl[0]) < 0) 1100*68461Seric #else 110157736Seric if (nlist(_PATH_UNIX, Nl) < 0) 1102*68461Seric #endif 110357736Seric { 110457736Seric if (tTd(3, 1)) 110557736Seric printf("getla: nlist(%s): %s\n", _PATH_UNIX, 110657736Seric errstring(errno)); 110714872Seric return (-1); 110857736Seric } 110959253Seric if (Nl[X_AVENRUN].n_value == 0) 111059253Seric { 111159253Seric if (tTd(3, 1)) 111259253Seric printf("getla: nlist(%s, %s) ==> 0\n", 111359253Seric _PATH_UNIX, LA_AVENRUN); 111459253Seric return (-1); 111559253Seric } 1116*68461Seric #ifdef NAMELISTMASK 1117*68461Seric Nl[X_AVENRUN].n_value &= NAMELISTMASK; 111866763Seric #endif 111914872Seric } 112057736Seric if (tTd(3, 20)) 112157736Seric printf("getla: symbol address = %#x\n", Nl[X_AVENRUN].n_value); 1122*68461Seric if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, SEEK_SET) == -1 || 112323118Seric read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun)) 112419967Seric { 112519967Seric /* thank you Ian */ 112657736Seric if (tTd(3, 1)) 112757736Seric printf("getla: lseek or read: %s\n", errstring(errno)); 112819967Seric return (-1); 112919967Seric } 113064360Seric #if (LA_TYPE == LA_INT) || (LA_TYPE == LA_SHORT) 113157736Seric if (tTd(3, 5)) 113257736Seric { 113357736Seric printf("getla: avenrun = %d", avenrun[0]); 113457736Seric if (tTd(3, 15)) 113557736Seric printf(", %d, %d", avenrun[1], avenrun[2]); 113657736Seric printf("\n"); 113757736Seric } 113857736Seric if (tTd(3, 1)) 113957736Seric printf("getla: %d\n", (int) (avenrun[0] + FSCALE/2) >> FSHIFT); 114024943Seric return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT); 114151920Seric #else 114257736Seric if (tTd(3, 5)) 114357736Seric { 114457736Seric printf("getla: avenrun = %g", avenrun[0]); 114557736Seric if (tTd(3, 15)) 114657736Seric printf(", %g, %g", avenrun[1], avenrun[2]); 114757736Seric printf("\n"); 114857736Seric } 114957736Seric if (tTd(3, 1)) 115057736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 115151920Seric return ((int) (avenrun[0] + 0.5)); 115251920Seric #endif 115314872Seric } 115414872Seric 115551773Seric #else 115651920Seric #if LA_TYPE == LA_SUBR 115751773Seric 115864718Seric #ifdef DGUX 115964718Seric 116064718Seric #include <sys/dg_sys_info.h> 116164718Seric 1162*68461Seric int 1163*68461Seric getla() 116464718Seric { 116564718Seric struct dg_sys_info_load_info load_info; 116664718Seric 116764718Seric dg_sys_info((long *)&load_info, 116864718Seric DG_SYS_INFO_LOAD_INFO_TYPE, DG_SYS_INFO_LOAD_VERSION_0); 116964718Seric 1170*68461Seric if (tTd(3, 1)) 1171*68461Seric printf("getla: %d\n", (int) (load_info.one_minute + 0.5)); 1172*68461Seric 117364718Seric return((int) (load_info.one_minute + 0.5)); 117464718Seric } 117564718Seric 117664718Seric #else 1177*68461Seric # ifdef __hpux 117864718Seric 1179*68461Seric # include <sys/param.h> 1180*68461Seric # include <sys/pstat.h> 1181*68461Seric 1182*68461Seric int 118351773Seric getla() 118451773Seric { 1185*68461Seric struct pst_dynamic pstd; 1186*68461Seric 1187*68461Seric if (pstat_getdynamic(&pstd, sizeof(struct pst_dynamic), 1188*68461Seric (size_t) 1 ,0) == -1) 1189*68461Seric return 0; 1190*68461Seric 1191*68461Seric if (tTd(3, 1)) 1192*68461Seric printf("getla: %d\n", (int) (pstd.psd_avg_1_min + 0.5)); 1193*68461Seric 1194*68461Seric return (int) (pstd.psd_avg_1_min + 0.5); 1195*68461Seric } 1196*68461Seric 1197*68461Seric # else 1198*68461Seric 1199*68461Seric int 1200*68461Seric getla() 1201*68461Seric { 120251920Seric double avenrun[3]; 120351920Seric 120451920Seric if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) < 0) 120557736Seric { 120657736Seric if (tTd(3, 1)) 120757736Seric perror("getla: getloadavg failed:"); 120851920Seric return (-1); 120957736Seric } 121057736Seric if (tTd(3, 1)) 121157736Seric printf("getla: %d\n", (int) (avenrun[0] +0.5)); 121251920Seric return ((int) (avenrun[0] + 0.5)); 121351773Seric } 121451773Seric 1215*68461Seric # endif /* __hpux */ 121664718Seric #endif /* DGUX */ 121751773Seric #else 121864295Seric #if LA_TYPE == LA_MACH 121951773Seric 122064295Seric /* 122165173Seric ** This has been tested on NEXTSTEP release 2.1/3.X. 122264295Seric */ 122364295Seric 122465173Seric #if defined(NX_CURRENT_COMPILER_RELEASE) && NX_CURRENT_COMPILER_RELEASE > NX_COMPILER_RELEASE_3_0 122565173Seric # include <mach/mach.h> 122665173Seric #else 122765173Seric # include <mach.h> 122865173Seric #endif 122964295Seric 123051773Seric getla() 123151773Seric { 123264295Seric processor_set_t default_set; 123364295Seric kern_return_t error; 123464295Seric unsigned int info_count; 123564295Seric struct processor_set_basic_info info; 123664295Seric host_t host; 123764295Seric 123864295Seric error = processor_set_default(host_self(), &default_set); 123964295Seric if (error != KERN_SUCCESS) 124064295Seric return -1; 124164295Seric info_count = PROCESSOR_SET_BASIC_INFO_COUNT; 124264295Seric if (processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, 124364295Seric &host, (processor_set_info_t)&info, 124464295Seric &info_count) != KERN_SUCCESS) 124564295Seric { 124664295Seric return -1; 124764295Seric } 124864295Seric return (int) (info.load_average + (LOAD_SCALE / 2)) / LOAD_SCALE; 124964295Seric } 125064295Seric 125164295Seric 125264295Seric #else 125366313Seric #if LA_TYPE == LA_PROCSTR 125464295Seric 125566301Seric /* 125666301Seric ** Read /proc/loadavg for the load average. This is assumed to be 125766301Seric ** in a format like "0.15 0.12 0.06". 125866301Seric ** 125966301Seric ** Initially intended for Linux. This has been in the kernel 126066301Seric ** since at least 0.99.15. 126166301Seric */ 126266301Seric 126366301Seric # ifndef _PATH_LOADAVG 126466301Seric # define _PATH_LOADAVG "/proc/loadavg" 126566301Seric # endif 126666301Seric 126766301Seric int 126864295Seric getla() 126964295Seric { 127066319Seric double avenrun; 127166301Seric register int result; 127266319Seric FILE *fp; 127366301Seric 127466319Seric fp = fopen(_PATH_LOADAVG, "r"); 127566319Seric if (fp == NULL) 127666301Seric { 127766319Seric if (tTd(3, 1)) 127866319Seric printf("getla: fopen(%s): %s\n", 127966319Seric _PATH_LOADAVG, errstring(errno)); 128066319Seric return -1; 128166301Seric } 128266301Seric result = fscanf(fp, "%lf", &avenrun); 128366319Seric fclose(fp); 128466301Seric if (result != 1) 128566301Seric { 128666310Seric if (tTd(3, 1)) 128766301Seric printf("getla: fscanf() = %d: %s\n", 128866301Seric result, errstring(errno)); 128966301Seric return -1; 129066301Seric } 129166301Seric 129257736Seric if (tTd(3, 1)) 129366301Seric printf("getla(): %.2f\n", avenrun); 129466301Seric 129566301Seric return ((int) (avenrun + 0.5)); 129666301Seric } 129766301Seric 129866301Seric #else 129966301Seric 130066301Seric getla() 130166301Seric { 130266301Seric if (tTd(3, 1)) 130357736Seric printf("getla: ZERO\n"); 130451920Seric return (0); 130551773Seric } 130651773Seric 130751773Seric #endif 130851773Seric #endif 130964295Seric #endif 131066301Seric #endif 131166045Seric 131266045Seric 131366045Seric /* 131466045Seric * Copyright 1989 Massachusetts Institute of Technology 131566045Seric * 131666045Seric * Permission to use, copy, modify, distribute, and sell this software and its 131766045Seric * documentation for any purpose is hereby granted without fee, provided that 131866045Seric * the above copyright notice appear in all copies and that both that 131966045Seric * copyright notice and this permission notice appear in supporting 132066045Seric * documentation, and that the name of M.I.T. not be used in advertising or 132166045Seric * publicity pertaining to distribution of the software without specific, 132266045Seric * written prior permission. M.I.T. makes no representations about the 132366045Seric * suitability of this software for any purpose. It is provided "as is" 132466045Seric * without express or implied warranty. 132566045Seric * 132666045Seric * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 132766045Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T. 132866045Seric * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 132966045Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 133066045Seric * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 133166045Seric * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 133266045Seric * 133366045Seric * Authors: Many and varied... 133466045Seric */ 133566045Seric 133666045Seric /* Non Apollo stuff removed by Don Lewis 11/15/93 */ 133766045Seric #ifndef lint 133866045Seric static char rcsid[] = "@(#)$Id: getloadavg.c,v 1.16 1991/06/21 12:51:15 paul Exp $"; 133966045Seric #endif /* !lint */ 134066045Seric 134166179Seric #ifdef apollo 134266045Seric # undef volatile 134366045Seric # include <apollo/base.h> 134466045Seric 134566045Seric /* ARGSUSED */ 134666045Seric int getloadavg( call_data ) 134766045Seric caddr_t call_data; /* pointer to (double) return value */ 134866045Seric { 134966045Seric double *avenrun = (double *) call_data; 135066045Seric int i; 135166045Seric status_$t st; 135266045Seric long loadav[3]; 135366045Seric proc1_$get_loadav(loadav, &st); 135466045Seric *avenrun = loadav[0] / (double) (1 << 16); 135566045Seric return(0); 135666045Seric } 135766045Seric # endif /* apollo */ 135824943Seric /* 135924943Seric ** SHOULDQUEUE -- should this message be queued or sent? 136024943Seric ** 136124943Seric ** Compares the message cost to the load average to decide. 136224943Seric ** 136324943Seric ** Parameters: 136424943Seric ** pri -- the priority of the message in question. 136557438Seric ** ctime -- the message creation time. 136624943Seric ** 136724943Seric ** Returns: 136824943Seric ** TRUE -- if this message should be queued up for the 136924943Seric ** time being. 137024943Seric ** FALSE -- if the load is low enough to send this message. 137124943Seric ** 137224943Seric ** Side Effects: 137324943Seric ** none. 137424943Seric */ 137524943Seric 137624943Seric bool 137757438Seric shouldqueue(pri, ctime) 137824943Seric long pri; 137957438Seric time_t ctime; 138024943Seric { 1381*68461Seric bool rval; 1382*68461Seric 1383*68461Seric if (tTd(3, 30)) 1384*68461Seric printf("shouldqueue: CurrentLA=%d, pri=%d: ", CurrentLA, pri); 138551920Seric if (CurrentLA < QueueLA) 1386*68461Seric { 1387*68461Seric if (tTd(3, 30)) 1388*68461Seric printf("FALSE (CurrentLA < QueueLA)\n"); 138924943Seric return (FALSE); 1390*68461Seric } 139158132Seric if (CurrentLA >= RefuseLA) 1392*68461Seric { 1393*68461Seric if (tTd(3, 30)) 1394*68461Seric printf("TRUE (CurrentLA >= RefuseLA)\n"); 139558132Seric return (TRUE); 1396*68461Seric } 1397*68461Seric rval = pri > (QueueFactor / (CurrentLA - QueueLA + 1)); 1398*68461Seric if (tTd(3, 30)) 1399*68461Seric printf("%s (by calculation)\n", rval ? "TRUE" : "FALSE"); 1400*68461Seric return rval; 140124943Seric } 140224943Seric /* 140353037Seric ** REFUSECONNECTIONS -- decide if connections should be refused 140453037Seric ** 140553037Seric ** Parameters: 140653037Seric ** none. 140753037Seric ** 140853037Seric ** Returns: 140953037Seric ** TRUE if incoming SMTP connections should be refused 141053037Seric ** (for now). 141153037Seric ** FALSE if we should accept new work. 141253037Seric ** 141353037Seric ** Side Effects: 141453037Seric ** none. 141553037Seric */ 141653037Seric 141753037Seric bool 141853037Seric refuseconnections() 141953037Seric { 1420*68461Seric extern bool enoughspace(); 1421*68461Seric 142259156Seric #ifdef XLA 142359156Seric if (!xla_smtp_ok()) 142459156Seric return TRUE; 142559156Seric #endif 142659156Seric 142753037Seric /* this is probably too simplistic */ 1428*68461Seric return CurrentLA >= RefuseLA || !enoughspace(MinBlocksFree + 1); 142953037Seric } 143053037Seric /* 143124943Seric ** SETPROCTITLE -- set process title for ps 143224943Seric ** 143324943Seric ** Parameters: 143458674Seric ** fmt -- a printf style format string. 143558674Seric ** a, b, c -- possible parameters to fmt. 143624943Seric ** 143724943Seric ** Returns: 143824943Seric ** none. 143924943Seric ** 144024943Seric ** Side Effects: 144124943Seric ** Clobbers argv of our main procedure so ps(1) will 144224943Seric ** display the title. 144324943Seric */ 144424943Seric 1445*68461Seric #define SPT_NONE 0 /* don't use it at all */ 1446*68461Seric #define SPT_REUSEARGV 1 /* cover argv with title information */ 1447*68461Seric #define SPT_BUILTIN 2 /* use libc builtin */ 1448*68461Seric #define SPT_PSTAT 3 /* use pstat(PSTAT_SETCMD, ...) */ 1449*68461Seric #define SPT_PSSTRINGS 4 /* use PS_STRINGS->... */ 1450*68461Seric #define SPT_WRITEUDOT 5 /* write u. area in kmem */ 1451*68461Seric 1452*68461Seric #ifndef SPT_TYPE 1453*68461Seric # define SPT_TYPE SPT_REUSEARGV 1454*68461Seric #endif 1455*68461Seric 1456*68461Seric #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN 1457*68461Seric 1458*68461Seric # if SPT_TYPE == SPT_PSTAT 145958689Seric # include <sys/pstat.h> 146058689Seric # endif 1461*68461Seric # if SPT_TYPE == SPT_PSSTRINGS 146259732Seric # include <machine/vmparam.h> 146359732Seric # include <sys/exec.h> 1464*68461Seric # ifndef PS_STRINGS /* hmmmm.... apparently not available after all */ 1465*68461Seric # undef SPT_TYPE 1466*68461Seric # define SPT_TYPE SPT_REUSEARGV 146764247Seric # endif 146859732Seric # endif 1469*68461Seric 1470*68461Seric # if SPT_TYPE == SPT_PSSTRINGS 1471*68461Seric # define SETPROC_STATIC static 1472*68461Seric # else 147359732Seric # define SETPROC_STATIC 147459732Seric # endif 147567626Seric 1476*68461Seric # ifndef SPT_PADCHAR 1477*68461Seric # define SPT_PADCHAR ' ' 1478*68461Seric # endif 147958689Seric 1480*68461Seric #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */ 148164843Seric 1482*68461Seric #if SPT_TYPE != SPT_BUILTIN 1483*68461Seric 148424943Seric /*VARARGS1*/ 1485*68461Seric void 1486*68461Seric # ifdef __STDC__ 148757642Seric setproctitle(char *fmt, ...) 1488*68461Seric # else 148957642Seric setproctitle(fmt, va_alist) 149024943Seric char *fmt; 149157642Seric va_dcl 1492*68461Seric # endif 149324943Seric { 1494*68461Seric # if SPT_TYPE != SPT_NONE 149524943Seric register char *p; 149625049Seric register int i; 149759732Seric SETPROC_STATIC char buf[MAXLINE]; 149856852Seric VA_LOCAL_DECL 1499*68461Seric # if SPT_TYPE == SPT_PSTAT 150058689Seric union pstun pst; 150158689Seric # endif 150224943Seric extern char **Argv; 150324943Seric extern char *LastArgv; 150424943Seric 150558674Seric p = buf; 150624943Seric 150758674Seric /* print sendmail: heading for grep */ 150858674Seric (void) strcpy(p, "sendmail: "); 150958674Seric p += strlen(p); 151024943Seric 151158674Seric /* print the argument string */ 151258674Seric VA_START(fmt); 151358674Seric (void) vsprintf(p, fmt, ap); 151456852Seric VA_END; 151554996Seric 151658674Seric i = strlen(buf); 151758689Seric 1518*68461Seric # if SPT_TYPE == SPT_PSTAT 151958689Seric pst.pst_command = buf; 152058689Seric pstat(PSTAT_SETCMD, pst, i, 0, 0); 152158689Seric # else 1522*68461Seric # if SPT_TYPE == SPT_PSSTRINGS 152359732Seric PS_STRINGS->ps_nargvstr = 1; 152459732Seric PS_STRINGS->ps_argvstr = buf; 152559732Seric # else 152654996Seric if (i > LastArgv - Argv[0] - 2) 152725049Seric { 152854996Seric i = LastArgv - Argv[0] - 2; 152958674Seric buf[i] = '\0'; 153025049Seric } 153158674Seric (void) strcpy(Argv[0], buf); 153254997Seric p = &Argv[0][i]; 153324943Seric while (p < LastArgv) 1534*68461Seric *p++ = SPT_PADCHAR; 1535*68461Seric Argv[1] = NULL; 1536*68461Seric # endif /* SPT_TYPE == SPT_PSSTRINGS */ 1537*68461Seric # endif /* SPT_TYPE == SPT_PSTAT */ 1538*68461Seric # endif /* SPT_TYPE != SPT_NONE */ 153924943Seric } 154065982Seric 1541*68461Seric #endif /* SPT_TYPE != SPT_BUILTIN */ 154225698Seric /* 154325698Seric ** REAPCHILD -- pick up the body of my child, lest it become a zombie 154425698Seric ** 154525698Seric ** Parameters: 1546*68461Seric ** sig -- the signal that got us here (unused). 154725698Seric ** 154825698Seric ** Returns: 154925698Seric ** none. 155025698Seric ** 155125698Seric ** Side Effects: 155225698Seric ** Picks up extant zombies. 155325698Seric */ 155425698Seric 155546928Sbostic void 1556*68461Seric reapchild(sig) 1557*68461Seric int sig; 155825698Seric { 155964738Seric int olderrno = errno; 156063966Seric # ifdef HASWAITPID 156160219Seric auto int status; 156260560Seric int count; 156360560Seric int pid; 156460219Seric 156560560Seric count = 0; 156660560Seric while ((pid = waitpid(-1, &status, WNOHANG)) > 0) 156760560Seric { 156860560Seric if (count++ > 1000) 156960560Seric { 157066748Seric #ifdef LOG 157160560Seric syslog(LOG_ALERT, "reapchild: waitpid loop: pid=%d, status=%x", 157260560Seric pid, status); 157366748Seric #endif 157460560Seric break; 157560560Seric } 157660560Seric } 157760219Seric # else 157825698Seric # ifdef WNOHANG 157925698Seric union wait status; 158025698Seric 158163753Seric while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 158225698Seric continue; 158356795Seric # else /* WNOHANG */ 158425698Seric auto int status; 158525698Seric 158660219Seric while (wait(&status) > 0) 158725698Seric continue; 158856795Seric # endif /* WNOHANG */ 158960219Seric # endif 159064035Seric # ifdef SYS5SIGNALS 159164035Seric (void) setsignal(SIGCHLD, reapchild); 159258061Seric # endif 159364738Seric errno = olderrno; 159425698Seric } 159555418Seric /* 159655418Seric ** UNSETENV -- remove a variable from the environment 159755418Seric ** 159855418Seric ** Not needed on newer systems. 159955418Seric ** 160055418Seric ** Parameters: 160155418Seric ** name -- the string name of the environment variable to be 160255418Seric ** deleted from the current environment. 160355418Seric ** 160455418Seric ** Returns: 160555418Seric ** none. 160655418Seric ** 160755418Seric ** Globals: 160855418Seric ** environ -- a pointer to the current environment. 160955418Seric ** 161055418Seric ** Side Effects: 161155418Seric ** Modifies environ. 161255418Seric */ 161355418Seric 161463962Seric #ifndef HASUNSETENV 161555418Seric 161655418Seric void 161755418Seric unsetenv(name) 161855418Seric char *name; 161955418Seric { 162055418Seric extern char **environ; 162155418Seric register char **pp; 162255418Seric int len = strlen(name); 162355418Seric 162455418Seric for (pp = environ; *pp != NULL; pp++) 162555418Seric { 162655418Seric if (strncmp(name, *pp, len) == 0 && 162755418Seric ((*pp)[len] == '=' || (*pp)[len] == '\0')) 162855418Seric break; 162955418Seric } 163055418Seric 163155418Seric for (; *pp != NULL; pp++) 163255418Seric *pp = pp[1]; 163355418Seric } 163455418Seric 163563962Seric #endif 163656215Seric /* 163756215Seric ** GETDTABLESIZE -- return number of file descriptors 163856215Seric ** 163956215Seric ** Only on non-BSD systems 164056215Seric ** 164156215Seric ** Parameters: 164256215Seric ** none 164356215Seric ** 164456215Seric ** Returns: 164556215Seric ** size of file descriptor table 164656215Seric ** 164756215Seric ** Side Effects: 164856215Seric ** none 164956215Seric */ 165056215Seric 165163787Seric #ifdef SOLARIS 165263787Seric # include <sys/resource.h> 165363787Seric #endif 165456215Seric 165556215Seric int 165663787Seric getdtsize() 165756215Seric { 165863787Seric #ifdef RLIMIT_NOFILE 165963787Seric struct rlimit rl; 166063787Seric 166163787Seric if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) 166263787Seric return rl.rlim_cur; 166363787Seric #endif 166463787Seric 166564031Seric # ifdef HASGETDTABLESIZE 166664031Seric return getdtablesize(); 166764031Seric # else 166864031Seric # ifdef _SC_OPEN_MAX 166958689Seric return sysconf(_SC_OPEN_MAX); 167063787Seric # else 167156215Seric return NOFILE; 167263787Seric # endif 167358689Seric # endif 167456215Seric } 167557631Seric /* 167657631Seric ** UNAME -- get the UUCP name of this system. 167757631Seric */ 167857631Seric 167957943Seric #ifndef HASUNAME 168057631Seric 168157631Seric int 168257631Seric uname(name) 168357631Seric struct utsname *name; 168457631Seric { 168557631Seric FILE *file; 168657631Seric char *n; 168757631Seric 168857631Seric name->nodename[0] = '\0'; 168957631Seric 169057661Seric /* try /etc/whoami -- one line with the node name */ 169157631Seric if ((file = fopen("/etc/whoami", "r")) != NULL) 169257631Seric { 169357661Seric (void) fgets(name->nodename, NODE_LENGTH + 1, file); 169457631Seric (void) fclose(file); 169557661Seric n = strchr(name->nodename, '\n'); 169657631Seric if (n != NULL) 169757631Seric *n = '\0'; 169857631Seric if (name->nodename[0] != '\0') 169957631Seric return (0); 170057631Seric } 170157631Seric 170257661Seric /* try /usr/include/whoami.h -- has a #define somewhere */ 170357631Seric if ((file = fopen("/usr/include/whoami.h", "r")) != NULL) 170457631Seric { 170557631Seric char buf[MAXLINE]; 170657631Seric 170757631Seric while (fgets(buf, MAXLINE, file) != NULL) 170857631Seric if (sscanf(buf, "#define sysname \"%*[^\"]\"", 170957631Seric NODE_LENGTH, name->nodename) > 0) 171057631Seric break; 171157631Seric (void) fclose(file); 171257631Seric if (name->nodename[0] != '\0') 171357631Seric return (0); 171457631Seric } 171557631Seric 171657631Seric #ifdef TRUST_POPEN 171757631Seric /* 171857631Seric ** Popen is known to have security holes. 171957631Seric */ 172057631Seric 172157661Seric /* try uuname -l to return local name */ 172257631Seric if ((file = popen("uuname -l", "r")) != NULL) 172357631Seric { 172457661Seric (void) fgets(name, NODE_LENGTH + 1, file); 172557631Seric (void) pclose(file); 172657661Seric n = strchr(name, '\n'); 172757631Seric if (n != NULL) 172857631Seric *n = '\0'; 172957661Seric if (name->nodename[0] != '\0') 173057631Seric return (0); 173157631Seric } 173257631Seric #endif 173357631Seric 173457631Seric return (-1); 173557631Seric } 173657943Seric #endif /* HASUNAME */ 173758068Seric /* 173858068Seric ** INITGROUPS -- initialize groups 173958068Seric ** 174058068Seric ** Stub implementation for System V style systems 174158068Seric */ 174258068Seric 174358068Seric #ifndef HASINITGROUPS 174458068Seric 174558068Seric initgroups(name, basegid) 174658068Seric char *name; 174758068Seric int basegid; 174858068Seric { 174958068Seric return 0; 175058068Seric } 175158068Seric 175258068Seric #endif 175358082Seric /* 175459289Seric ** SETSID -- set session id (for non-POSIX systems) 175559289Seric */ 175659289Seric 175759289Seric #ifndef HASSETSID 175859289Seric 175963753Seric pid_t 176063753Seric setsid __P ((void)) 176159289Seric { 176264155Seric #ifdef TIOCNOTTY 176364155Seric int fd; 176464155Seric 176566743Seric fd = open("/dev/tty", O_RDWR, 0); 176664155Seric if (fd >= 0) 176764155Seric { 176864155Seric (void) ioctl(fd, (int) TIOCNOTTY, (char *) 0); 176964155Seric (void) close(fd); 177064155Seric } 177164155Seric #endif /* TIOCNOTTY */ 177264705Seric # ifdef SYS5SETPGRP 177363753Seric return setpgrp(); 177463753Seric # else 177564155Seric return setpgid(0, getpid()); 177659289Seric # endif 177759289Seric } 177859289Seric 177959289Seric #endif 178059289Seric /* 178166748Seric ** FSYNC -- dummy fsync 178266748Seric */ 178366748Seric 178466748Seric #ifdef NEEDFSYNC 178566748Seric 178666748Seric fsync(fd) 178766748Seric int fd; 178866748Seric { 178966748Seric # ifdef O_SYNC 179066748Seric return fcntl(fd, F_SETFL, O_SYNC); 179166748Seric # else 179266748Seric /* nothing we can do */ 179366748Seric return 0; 179466748Seric # endif 179566748Seric } 179666748Seric 179766748Seric #endif 179866748Seric /* 179964718Seric ** DGUX_INET_ADDR -- inet_addr for DG/UX 180064718Seric ** 180164718Seric ** Data General DG/UX version of inet_addr returns a struct in_addr 1802*68461Seric ** instead of a long. This patches things. Only needed on versions 1803*68461Seric ** prior to 5.4.3. 180464718Seric */ 180564718Seric 1806*68461Seric #ifdef DGUX_5_4_2 180764718Seric 180864718Seric #undef inet_addr 180964718Seric 181064718Seric long 181164718Seric dgux_inet_addr(host) 181264718Seric char *host; 181364718Seric { 181464718Seric struct in_addr haddr; 181564718Seric 181664718Seric haddr = inet_addr(host); 181764718Seric return haddr.s_addr; 181864718Seric } 181964718Seric 182064718Seric #endif 182164718Seric /* 182263969Seric ** GETOPT -- for old systems or systems with bogus implementations 182363969Seric */ 182463969Seric 182563969Seric #ifdef NEEDGETOPT 182663969Seric 182763969Seric /* 182863969Seric * Copyright (c) 1985 Regents of the University of California. 182963969Seric * All rights reserved. The Berkeley software License Agreement 183063969Seric * specifies the terms and conditions for redistribution. 183163969Seric */ 183263969Seric 183363969Seric 183463969Seric /* 183563969Seric ** this version hacked to add `atend' flag to allow state machine 183663969Seric ** to reset if invoked by the program to scan args for a 2nd time 183763969Seric */ 183863969Seric 183963969Seric #if defined(LIBC_SCCS) && !defined(lint) 184063969Seric static char sccsid[] = "@(#)getopt.c 4.3 (Berkeley) 3/9/86"; 184164072Seric #endif /* LIBC_SCCS and not lint */ 184263969Seric 184363969Seric #include <stdio.h> 184463969Seric 184563969Seric /* 184663969Seric * get option letter from argument vector 184763969Seric */ 184865352Seric #ifdef _CONVEX_SOURCE 184965352Seric extern int optind, opterr; 185065352Seric #else 185165352Seric int opterr = 1; /* if error message should be printed */ 185265352Seric int optind = 1; /* index into parent argv vector */ 185365352Seric #endif 185465352Seric int optopt; /* character checked for validity */ 185563969Seric char *optarg; /* argument associated with option */ 185663969Seric 185763969Seric #define BADCH (int)'?' 185863969Seric #define EMSG "" 185963969Seric #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \ 186063969Seric fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);} 186163969Seric 186263969Seric getopt(nargc,nargv,ostr) 186365160Seric int nargc; 186465160Seric char *const *nargv; 186565160Seric const char *ostr; 186663969Seric { 186763969Seric static char *place = EMSG; /* option letter processing */ 186863969Seric static char atend = 0; 186963969Seric register char *oli; /* option letter list index */ 187063969Seric 187163969Seric if (atend) { 187263969Seric atend = 0; 187363969Seric place = EMSG; 187463969Seric } 187563969Seric if(!*place) { /* update scanning pointer */ 187664136Seric if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) { 187763969Seric atend++; 187863969Seric return(EOF); 187963969Seric } 188063969Seric if (*place == '-') { /* found "--" */ 188163969Seric ++optind; 188263969Seric atend++; 188363969Seric return(EOF); 188463969Seric } 188563969Seric } /* option letter okay? */ 188664125Seric if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) { 188764136Seric if (!*place) ++optind; 188863969Seric tell(": illegal option -- "); 188963969Seric } 189063969Seric if (*++oli != ':') { /* don't need argument */ 189163969Seric optarg = NULL; 189263969Seric if (!*place) ++optind; 189363969Seric } 189463969Seric else { /* need an argument */ 189563969Seric if (*place) optarg = place; /* no white space */ 189663969Seric else if (nargc <= ++optind) { /* no arg */ 189763969Seric place = EMSG; 189863969Seric tell(": option requires an argument -- "); 189963969Seric } 190063969Seric else optarg = nargv[optind]; /* white space */ 190163969Seric place = EMSG; 190263969Seric ++optind; 190363969Seric } 190463969Seric return(optopt); /* dump back option letter */ 190563969Seric } 190663969Seric 190763969Seric #endif 190863969Seric /* 190963969Seric ** VFPRINTF, VSPRINTF -- for old 4.3 BSD systems missing a real version 191063969Seric */ 191163969Seric 191263969Seric #ifdef NEEDVPRINTF 191363969Seric 191463969Seric #define MAXARG 16 191563969Seric 191663969Seric vfprintf(fp, fmt, ap) 191763969Seric FILE * fp; 191863969Seric char * fmt; 191963969Seric char ** ap; 192063969Seric { 192163969Seric char * bp[MAXARG]; 192263969Seric int i = 0; 192363969Seric 192463969Seric while (*ap && i < MAXARG) 192563969Seric bp[i++] = *ap++; 192663969Seric fprintf(fp, fmt, bp[0], bp[1], bp[2], bp[3], 192763969Seric bp[4], bp[5], bp[6], bp[7], 192863969Seric bp[8], bp[9], bp[10], bp[11], 192963969Seric bp[12], bp[13], bp[14], bp[15]); 193063969Seric } 193163969Seric 193263969Seric vsprintf(s, fmt, ap) 193363969Seric char * s; 193463969Seric char * fmt; 193563969Seric char ** ap; 193663969Seric { 193763969Seric char * bp[MAXARG]; 193863969Seric int i = 0; 193963969Seric 194063969Seric while (*ap && i < MAXARG) 194163969Seric bp[i++] = *ap++; 194263969Seric sprintf(s, fmt, bp[0], bp[1], bp[2], bp[3], 194363969Seric bp[4], bp[5], bp[6], bp[7], 194463969Seric bp[8], bp[9], bp[10], bp[11], 194563969Seric bp[12], bp[13], bp[14], bp[15]); 194663969Seric } 194763969Seric 194863969Seric #endif 194963969Seric /* 195065220Seric ** USERSHELLOK -- tell if a user's shell is ok for unrestricted use 195165208Seric ** 195265211Seric ** Parameters: 195365211Seric ** shell -- the user's shell from /etc/passwd 195465211Seric ** 195565211Seric ** Returns: 195665211Seric ** TRUE -- if it is ok to use this for unrestricted access. 195765211Seric ** FALSE -- if the shell is restricted. 195865208Seric */ 195965208Seric 196065587Seric #if !HASGETUSERSHELL 196165587Seric 196265587Seric # ifndef _PATH_SHELLS 196365587Seric # define _PATH_SHELLS "/etc/shells" 196465587Seric # endif 196565587Seric 196665587Seric char *DefaultUserShells[] = 196765587Seric { 1968*68461Seric "/bin/sh", /* standard shell */ 196965587Seric "/usr/bin/sh", 1970*68461Seric "/bin/csh", /* C shell */ 197165587Seric "/usr/bin/csh", 197265587Seric #ifdef __hpux 1973*68461Seric "/bin/rsh", /* restricted Bourne shell */ 1974*68461Seric "/bin/ksh", /* Korn shell */ 1975*68461Seric "/bin/rksh", /* restricted Korn shell */ 197665587Seric "/bin/pam", 1977*68461Seric "/usr/bin/keysh", /* key shell (extended Korn shell) */ 197865587Seric "/bin/posix/sh", 197965211Seric #endif 1980*68461Seric #ifdef _AIX3 1981*68461Seric "/bin/ksh", /* Korn shell */ 1982*68461Seric "/usr/bin/ksh", 1983*68461Seric "/bin/tsh", /* trusted shell */ 1984*68461Seric "/usr/bin/tsh", 1985*68461Seric "/bin/bsh", /* Bourne shell */ 1986*68461Seric "/usr/bin/bsh", 1987*68461Seric #endif 198865587Seric NULL 198965587Seric }; 199065587Seric 199165208Seric #endif 199265208Seric 199365747Seric #define WILDCARD_SHELL "/SENDMAIL/ANY/SHELL/" 199465747Seric 199565211Seric bool 199665211Seric usershellok(shell) 199765211Seric char *shell; 199865208Seric { 199965211Seric #if HASGETUSERSHELL 200065211Seric register char *p; 200165211Seric extern char *getusershell(); 200265208Seric 200368457Seric if (shell == NULL || shell[0] == '\0') 200468457Seric return TRUE; 200568457Seric 200665211Seric setusershell(); 200765211Seric while ((p = getusershell()) != NULL) 200865747Seric if (strcmp(p, shell) == 0 || strcmp(p, WILDCARD_SHELL) == 0) 200965211Seric break; 201065211Seric endusershell(); 201165211Seric return p != NULL; 201265211Seric #else 201365211Seric register FILE *shellf; 201465211Seric char buf[MAXLINE]; 201565208Seric 201668457Seric if (shell == NULL || shell[0] == '\0') 201768457Seric return TRUE; 201868457Seric 201965211Seric shellf = fopen(_PATH_SHELLS, "r"); 202065211Seric if (shellf == NULL) 202165211Seric { 202265211Seric /* no /etc/shells; see if it is one of the std shells */ 202365587Seric char **d; 202465587Seric 202565587Seric for (d = DefaultUserShells; *d != NULL; d++) 202665587Seric { 202765587Seric if (strcmp(shell, *d) == 0) 202865587Seric return TRUE; 202965587Seric } 203065587Seric return FALSE; 203165211Seric } 203265208Seric 203365211Seric while (fgets(buf, sizeof buf, shellf) != NULL) 203465211Seric { 203565211Seric register char *p, *q; 203665208Seric 203765211Seric p = buf; 203865211Seric while (*p != '\0' && *p != '#' && *p != '/') 203965211Seric p++; 204065211Seric if (*p == '#' || *p == '\0') 204165208Seric continue; 204265211Seric q = p; 204365211Seric while (*p != '\0' && *p != '#' && !isspace(*p)) 204465211Seric p++; 204565211Seric *p = '\0'; 204665747Seric if (strcmp(shell, q) == 0 || strcmp(WILDCARD_SHELL, q) == 0) 204765211Seric { 204865211Seric fclose(shellf); 204965211Seric return TRUE; 205065211Seric } 205165208Seric } 205265211Seric fclose(shellf); 205365211Seric return FALSE; 205465211Seric #endif 205565208Seric } 205665208Seric /* 205764311Seric ** FREESPACE -- see how much free space is on the queue filesystem 205858082Seric ** 205958082Seric ** Only implemented if you have statfs. 206058082Seric ** 206158082Seric ** Parameters: 206264311Seric ** dir -- the directory in question. 206364311Seric ** bsize -- a variable into which the filesystem 206464311Seric ** block size is stored. 206558082Seric ** 206658082Seric ** Returns: 206764311Seric ** The number of bytes free on the queue filesystem. 206864311Seric ** -1 if the statfs call fails. 206964311Seric ** 207064311Seric ** Side effects: 207164311Seric ** Puts the filesystem block size into bsize. 207258082Seric */ 207358082Seric 207465749Seric /* statfs types */ 207565749Seric #define SFS_NONE 0 /* no statfs implementation */ 207665749Seric #define SFS_USTAT 1 /* use ustat */ 207765749Seric #define SFS_4ARGS 2 /* use four-argument statfs call */ 207865749Seric #define SFS_VFS 3 /* use <sys/vfs.h> implementation */ 207965749Seric #define SFS_MOUNT 4 /* use <sys/mount.h> implementation */ 208065749Seric #define SFS_STATFS 5 /* use <sys/statfs.h> implementation */ 208166752Seric #define SFS_STATVFS 6 /* use <sys/statvfs.h> implementation */ 208265749Seric 208365749Seric #ifndef SFS_TYPE 208465749Seric # define SFS_TYPE SFS_NONE 208558157Seric #endif 208658157Seric 208765749Seric #if SFS_TYPE == SFS_USTAT 208858157Seric # include <ustat.h> 208958157Seric #endif 209065749Seric #if SFS_TYPE == SFS_4ARGS || SFS_TYPE == SFS_STATFS 209165749Seric # include <sys/statfs.h> 209258082Seric #endif 209365749Seric #if SFS_TYPE == SFS_VFS 209465749Seric # include <sys/vfs.h> 209565749Seric #endif 209665749Seric #if SFS_TYPE == SFS_MOUNT 209765749Seric # include <sys/mount.h> 209865749Seric #endif 209966752Seric #if SFS_TYPE == SFS_STATVFS 210066752Seric # include <sys/statvfs.h> 210166752Seric #endif 210258082Seric 210364311Seric long 210464311Seric freespace(dir, bsize) 210564311Seric char *dir; 210664311Seric long *bsize; 210758082Seric { 210865749Seric #if SFS_TYPE != SFS_NONE 210965749Seric # if SFS_TYPE == SFS_USTAT 211058153Seric struct ustat fs; 211158153Seric struct stat statbuf; 211258366Seric # define FSBLOCKSIZE DEV_BSIZE 2113*68461Seric # define FSF_BAVAIL f_tfree 211458157Seric # else 211558157Seric # if defined(ultrix) 211658157Seric struct fs_data fs; 2117*68461Seric # define FSF_BAVAIL fd_bfreen 2118*68461Seric # define FSBLOCKSIZE 1024L 211958153Seric # else 212066752Seric # if SFS_TYPE == SFS_STATVFS 212166752Seric struct statvfs fs; 2122*68461Seric # define FSBLOCKSIZE fs.f_frsize 212366752Seric # else 212458082Seric struct statfs fs; 212566752Seric # define FSBLOCKSIZE fs.f_bsize 212663987Seric # endif 212758153Seric # endif 212858133Seric # endif 2129*68461Seric # ifndef FSF_BAVAIL 2130*68461Seric # define FSF_BAVAIL f_bavail 2131*68461Seric # endif 213258082Seric 213365749Seric # if SFS_TYPE == SFS_USTAT 213464311Seric if (stat(dir, &statbuf) == 0 && ustat(statbuf.st_dev, &fs) == 0) 213558157Seric # else 213665749Seric # if SFS_TYPE == SFS_4ARGS 213764311Seric if (statfs(dir, &fs, sizeof fs, 0) == 0) 213858157Seric # else 2139*68461Seric # if SFS_TYPE == SFS_STATVFS 2140*68461Seric if (statvfs(dir, &fs) == 0) 2141*68461Seric # else 2142*68461Seric # if defined(ultrix) 214368457Seric if (statfs(dir, &fs) > 0) 2144*68461Seric # else 214564311Seric if (statfs(dir, &fs) == 0) 2146*68461Seric # endif 214758153Seric # endif 214858133Seric # endif 214958133Seric # endif 215058133Seric { 215164311Seric if (bsize != NULL) 215264311Seric *bsize = FSBLOCKSIZE; 2153*68461Seric return (fs.FSF_BAVAIL); 215464311Seric } 215564337Seric #endif 215664311Seric return (-1); 215764311Seric } 215864311Seric /* 215964311Seric ** ENOUGHSPACE -- check to see if there is enough free space on the queue fs 216064311Seric ** 216164311Seric ** Only implemented if you have statfs. 216264311Seric ** 216364311Seric ** Parameters: 216464311Seric ** msize -- the size to check against. If zero, we don't yet 216564311Seric ** know how big the message will be, so just check for 216664311Seric ** a "reasonable" amount. 216764311Seric ** 216864311Seric ** Returns: 216964311Seric ** TRUE if there is enough space. 217064311Seric ** FALSE otherwise. 217164311Seric */ 217264311Seric 217364311Seric bool 217464311Seric enoughspace(msize) 217564311Seric long msize; 217664311Seric { 217764311Seric long bfree, bsize; 217864311Seric 217964311Seric if (MinBlocksFree <= 0 && msize <= 0) 218064311Seric { 218158133Seric if (tTd(4, 80)) 218264311Seric printf("enoughspace: no threshold\n"); 218364311Seric return TRUE; 218464311Seric } 218564311Seric 218664311Seric if ((bfree = freespace(QueueDir, &bsize)) >= 0) 218764311Seric { 218864311Seric if (tTd(4, 80)) 218958333Seric printf("enoughspace: bavail=%ld, need=%ld\n", 219064311Seric bfree, msize); 219158333Seric 219258333Seric /* convert msize to block count */ 219364311Seric msize = msize / bsize + 1; 219458333Seric if (MinBlocksFree >= 0) 219558333Seric msize += MinBlocksFree; 219658333Seric 219764311Seric if (bfree < msize) 219858090Seric { 219958090Seric #ifdef LOG 220058090Seric if (LogLevel > 0) 220164254Seric syslog(LOG_ALERT, 220264254Seric "%s: low on space (have %ld, %s needs %ld in %s)", 2203*68461Seric CurEnv->e_id == NULL ? "[NOQUEUE]" : CurEnv->e_id, 2204*68461Seric bfree, 2205*68461Seric CurHostName == NULL ? "SMTP-DAEMON" : CurHostName, 2206*68461Seric msize, QueueDir); 220758090Seric #endif 220858082Seric return FALSE; 220958090Seric } 221058082Seric } 221158082Seric else if (tTd(4, 80)) 221258333Seric printf("enoughspace failure: min=%ld, need=%ld: %s\n", 221358333Seric MinBlocksFree, msize, errstring(errno)); 221458082Seric return TRUE; 221558082Seric } 221658542Seric /* 221758542Seric ** TRANSIENTERROR -- tell if an error code indicates a transient failure 221858542Seric ** 221958542Seric ** This looks at an errno value and tells if this is likely to 222058542Seric ** go away if retried later. 222158542Seric ** 222258542Seric ** Parameters: 222358542Seric ** err -- the errno code to classify. 222458542Seric ** 222558542Seric ** Returns: 222658542Seric ** TRUE if this is probably transient. 222758542Seric ** FALSE otherwise. 222858542Seric */ 222958542Seric 223058542Seric bool 223158542Seric transienterror(err) 223258542Seric int err; 223358542Seric { 223458542Seric switch (err) 223558542Seric { 223658542Seric case EIO: /* I/O error */ 223758542Seric case ENXIO: /* Device not configured */ 223858542Seric case EAGAIN: /* Resource temporarily unavailable */ 223958542Seric case ENOMEM: /* Cannot allocate memory */ 224058542Seric case ENODEV: /* Operation not supported by device */ 224158542Seric case ENFILE: /* Too many open files in system */ 224258542Seric case EMFILE: /* Too many open files */ 224358542Seric case ENOSPC: /* No space left on device */ 224458542Seric #ifdef ETIMEDOUT 224558542Seric case ETIMEDOUT: /* Connection timed out */ 224658542Seric #endif 224758542Seric #ifdef ESTALE 224858542Seric case ESTALE: /* Stale NFS file handle */ 224958542Seric #endif 225058542Seric #ifdef ENETDOWN 225158542Seric case ENETDOWN: /* Network is down */ 225258542Seric #endif 225358542Seric #ifdef ENETUNREACH 225458542Seric case ENETUNREACH: /* Network is unreachable */ 225558542Seric #endif 225658542Seric #ifdef ENETRESET 225758542Seric case ENETRESET: /* Network dropped connection on reset */ 225858542Seric #endif 225958542Seric #ifdef ECONNABORTED 226058542Seric case ECONNABORTED: /* Software caused connection abort */ 226158542Seric #endif 226258542Seric #ifdef ECONNRESET 226358542Seric case ECONNRESET: /* Connection reset by peer */ 226458542Seric #endif 226558542Seric #ifdef ENOBUFS 226658542Seric case ENOBUFS: /* No buffer space available */ 226758542Seric #endif 226858542Seric #ifdef ESHUTDOWN 226958542Seric case ESHUTDOWN: /* Can't send after socket shutdown */ 227058542Seric #endif 227158542Seric #ifdef ECONNREFUSED 227258542Seric case ECONNREFUSED: /* Connection refused */ 227358542Seric #endif 227458542Seric #ifdef EHOSTDOWN 227558542Seric case EHOSTDOWN: /* Host is down */ 227658542Seric #endif 227758542Seric #ifdef EHOSTUNREACH 227858542Seric case EHOSTUNREACH: /* No route to host */ 227958542Seric #endif 228058542Seric #ifdef EDQUOT 228158542Seric case EDQUOT: /* Disc quota exceeded */ 228258542Seric #endif 228358542Seric #ifdef EPROCLIM 228458542Seric case EPROCLIM: /* Too many processes */ 228558542Seric #endif 228658542Seric #ifdef EUSERS 228758542Seric case EUSERS: /* Too many users */ 228858542Seric #endif 228958542Seric #ifdef EDEADLK 229058542Seric case EDEADLK: /* Resource deadlock avoided */ 229158542Seric #endif 229258542Seric #ifdef EISCONN 229358542Seric case EISCONN: /* Socket already connected */ 229458542Seric #endif 229558542Seric #ifdef EINPROGRESS 229658542Seric case EINPROGRESS: /* Operation now in progress */ 229758542Seric #endif 229858542Seric #ifdef EALREADY 229958542Seric case EALREADY: /* Operation already in progress */ 230058542Seric #endif 230158542Seric #ifdef EADDRINUSE 230258542Seric case EADDRINUSE: /* Address already in use */ 230358542Seric #endif 230458542Seric #ifdef EADDRNOTAVAIL 230558542Seric case EADDRNOTAVAIL: /* Can't assign requested address */ 230658542Seric #endif 230766018Seric #ifdef ETXTBSY 230866017Seric case ETXTBSY: /* (Apollo) file locked */ 230966017Seric #endif 231063834Seric #if defined(ENOSR) && (!defined(ENOBUFS) || (ENOBUFS != ENOSR)) 231158542Seric case ENOSR: /* Out of streams resources */ 231258542Seric #endif 231358542Seric return TRUE; 231458542Seric } 231558542Seric 231658542Seric /* nope, must be permanent */ 231758542Seric return FALSE; 231858542Seric } 231958689Seric /* 232064035Seric ** LOCKFILE -- lock a file using flock or (shudder) fcntl locking 232158689Seric ** 232258689Seric ** Parameters: 232358689Seric ** fd -- the file descriptor of the file. 232458689Seric ** filename -- the file name (for error messages). 232564335Seric ** ext -- the filename extension. 232658689Seric ** type -- type of the lock. Bits can be: 232758689Seric ** LOCK_EX -- exclusive lock. 232858689Seric ** LOCK_NB -- non-blocking. 232958689Seric ** 233058689Seric ** Returns: 233158689Seric ** TRUE if the lock was acquired. 233258689Seric ** FALSE otherwise. 233358689Seric */ 233458689Seric 233558689Seric bool 233664335Seric lockfile(fd, filename, ext, type) 233758689Seric int fd; 233858689Seric char *filename; 233964335Seric char *ext; 234058689Seric int type; 234158689Seric { 234265830Seric # if !HASFLOCK 234358689Seric int action; 234458689Seric struct flock lfd; 234564335Seric 234664335Seric if (ext == NULL) 234764335Seric ext = ""; 234864035Seric 234963983Seric bzero(&lfd, sizeof lfd); 235059447Seric if (bitset(LOCK_UN, type)) 235159447Seric lfd.l_type = F_UNLCK; 235259447Seric else if (bitset(LOCK_EX, type)) 235358689Seric lfd.l_type = F_WRLCK; 235458689Seric else 235558689Seric lfd.l_type = F_RDLCK; 235658689Seric 235758689Seric if (bitset(LOCK_NB, type)) 235858689Seric action = F_SETLK; 235958689Seric else 236058689Seric action = F_SETLKW; 236158689Seric 236264264Seric if (tTd(55, 60)) 236364335Seric printf("lockfile(%s%s, action=%d, type=%d): ", 236464335Seric filename, ext, action, lfd.l_type); 236564264Seric 236658689Seric if (fcntl(fd, action, &lfd) >= 0) 236764264Seric { 236864264Seric if (tTd(55, 60)) 236964264Seric printf("SUCCESS\n"); 237058689Seric return TRUE; 237164264Seric } 237258689Seric 237364264Seric if (tTd(55, 60)) 237464264Seric printf("(%s) ", errstring(errno)); 237564264Seric 237664136Seric /* 237764136Seric ** On SunOS, if you are testing using -oQ/tmp/mqueue or 237864136Seric ** -oA/tmp/aliases or anything like that, and /tmp is mounted 237964136Seric ** as type "tmp" (that is, served from swap space), the 238064136Seric ** previous fcntl will fail with "Invalid argument" errors. 238164136Seric ** Since this is fairly common during testing, we will assume 238264136Seric ** that this indicates that the lock is successfully grabbed. 238364136Seric */ 238464136Seric 238564136Seric if (errno == EINVAL) 238664264Seric { 238764264Seric if (tTd(55, 60)) 238864264Seric printf("SUCCESS\n"); 238964136Seric return TRUE; 239064264Seric } 239164136Seric 239258689Seric if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN)) 239364378Seric { 239464378Seric int omode = -1; 239564378Seric # ifdef F_GETFL 239664378Seric int oerrno = errno; 239764378Seric 239864378Seric (void) fcntl(fd, F_GETFL, &omode); 239964378Seric errno = oerrno; 240064378Seric # endif 240164378Seric syserr("cannot lockf(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 240264378Seric filename, ext, fd, type, omode, geteuid()); 240364378Seric } 240458689Seric # else 240564335Seric if (ext == NULL) 240664335Seric ext = ""; 240764335Seric 240864264Seric if (tTd(55, 60)) 240964335Seric printf("lockfile(%s%s, type=%o): ", filename, ext, type); 241064264Seric 241158689Seric if (flock(fd, type) >= 0) 241264264Seric { 241364264Seric if (tTd(55, 60)) 241464264Seric printf("SUCCESS\n"); 241558689Seric return TRUE; 241664264Seric } 241758689Seric 241864264Seric if (tTd(55, 60)) 241964264Seric printf("(%s) ", errstring(errno)); 242064264Seric 242158689Seric if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK) 242264378Seric { 242364378Seric int omode = -1; 242464378Seric # ifdef F_GETFL 242564378Seric int oerrno = errno; 242664378Seric 242764378Seric (void) fcntl(fd, F_GETFL, &omode); 242864378Seric errno = oerrno; 242964378Seric # endif 243064378Seric syserr("cannot flock(%s%s, fd=%d, type=%o, omode=%o, euid=%d)", 243164378Seric filename, ext, fd, type, omode, geteuid()); 243264386Seric } 243358689Seric # endif 243464264Seric if (tTd(55, 60)) 243564264Seric printf("FAIL\n"); 243658689Seric return FALSE; 243758689Seric } 243864556Seric /* 243965948Seric ** CHOWNSAFE -- tell if chown is "safe" (executable only by root) 244065948Seric ** 244165948Seric ** Parameters: 244265948Seric ** fd -- the file descriptor to check. 244365948Seric ** 244465948Seric ** Returns: 244565948Seric ** TRUE -- if only root can chown the file to an arbitrary 244665948Seric ** user. 244765948Seric ** FALSE -- if an arbitrary user can give away a file. 244865948Seric */ 244965948Seric 245065948Seric bool 245165948Seric chownsafe(fd) 245265948Seric int fd; 245365948Seric { 245465950Seric #ifdef __hpux 245565948Seric char *s; 245665948Seric int tfd; 245765948Seric uid_t o_uid, o_euid; 245865948Seric gid_t o_gid, o_egid; 245965948Seric bool rval; 246065948Seric struct stat stbuf; 246165948Seric 246265948Seric o_uid = getuid(); 246365948Seric o_euid = geteuid(); 246465948Seric o_gid = getgid(); 246565948Seric o_egid = getegid(); 246665959Seric fstat(fd, &stbuf); 246765948Seric setresuid(stbuf.st_uid, stbuf.st_uid, -1); 246865948Seric setresgid(stbuf.st_gid, stbuf.st_gid, -1); 246965948Seric s = tmpnam(NULL); 247065948Seric tfd = open(s, O_RDONLY|O_CREAT, 0600); 247165974Seric rval = fchown(tfd, DefUid, DefGid) != 0; 247265948Seric close(tfd); 247365948Seric unlink(s); 247465948Seric setreuid(o_uid, o_euid); 247565948Seric setresgid(o_gid, o_egid, -1); 247665948Seric return rval; 247765948Seric #else 247866088Seric # ifdef _POSIX_CHOWN_RESTRICTED 247966088Seric # if _POSIX_CHOWN_RESTRICTED == -1 248066088Seric return FALSE; 248166088Seric # else 248266088Seric return TRUE; 248366088Seric # endif 248466088Seric # else 248566088Seric # ifdef _PC_CHOWN_RESTRICTED 2486*68461Seric int rval; 2487*68461Seric 2488*68461Seric /* 2489*68461Seric ** Some systems (e.g., SunOS) seem to have the call and the 2490*68461Seric ** #define _PC_CHOWN_RESTRICTED, but don't actually implement 2491*68461Seric ** the call. This heuristic checks for that. 2492*68461Seric */ 2493*68461Seric 2494*68461Seric errno = 0; 2495*68461Seric rval = fpathconf(fd, _PC_CHOWN_RESTRICTED); 2496*68461Seric if (errno == 0) 2497*68461Seric return rval > 0; 2498*68461Seric # endif 2499*68461Seric # ifdef BSD 2500*68461Seric return TRUE; 250168457Seric # else 250265948Seric return FALSE; 250365948Seric # endif 250465948Seric # endif 250565948Seric #endif 250665948Seric } 250765948Seric /* 2508*68461Seric ** RESETLIMITS -- reset system controlled resource limits 2509*68461Seric ** 2510*68461Seric ** This is to avoid denial-of-service attacks 2511*68461Seric ** 2512*68461Seric ** Parameters: 2513*68461Seric ** none 2514*68461Seric ** 2515*68461Seric ** Returns: 2516*68461Seric ** none 2517*68461Seric */ 2518*68461Seric 2519*68461Seric #if HASSETRLIMIT 2520*68461Seric # include <sys/resource.h> 2521*68461Seric #endif 2522*68461Seric 2523*68461Seric void 2524*68461Seric resetlimits() 2525*68461Seric { 2526*68461Seric #if HASSETRLIMIT 2527*68461Seric struct rlimit lim; 2528*68461Seric 2529*68461Seric lim.rlim_cur = lim.rlim_max = RLIM_INFINITY; 2530*68461Seric (void) setrlimit(RLIMIT_CPU, &lim); 2531*68461Seric (void) setrlimit(RLIMIT_FSIZE, &lim); 2532*68461Seric #else 2533*68461Seric # if HASULIMIT 2534*68461Seric (void) ulimit(2, 0x3fffff); 2535*68461Seric # endif 2536*68461Seric #endif 2537*68461Seric } 2538*68461Seric /* 253964556Seric ** GETCFNAME -- return the name of the .cf file. 254064556Seric ** 254164556Seric ** Some systems (e.g., NeXT) determine this dynamically. 254264556Seric */ 254364556Seric 254464556Seric char * 254564556Seric getcfname() 254664556Seric { 254764556Seric if (ConfFile != NULL) 254864556Seric return ConfFile; 254965214Seric #ifdef NETINFO 255065214Seric { 255165214Seric extern char *ni_propval(); 255265214Seric char *cflocation; 255365214Seric 2554*68461Seric cflocation = ni_propval("/locations", NULL, "sendmail", 2555*68461Seric "sendmail.cf", '\0'); 255665214Seric if (cflocation != NULL) 255765214Seric return cflocation; 255865214Seric } 255965214Seric #endif 256064556Seric return _PATH_SENDMAILCF; 256164556Seric } 256264718Seric /* 256364718Seric ** SETVENDOR -- process vendor code from V configuration line 256464718Seric ** 256564718Seric ** Parameters: 256664718Seric ** vendor -- string representation of vendor. 256764718Seric ** 256864718Seric ** Returns: 256964718Seric ** TRUE -- if ok. 257064718Seric ** FALSE -- if vendor code could not be processed. 257164926Seric ** 257264926Seric ** Side Effects: 257364926Seric ** It is reasonable to set mode flags here to tweak 257464926Seric ** processing in other parts of the code if necessary. 257564926Seric ** For example, if you are a vendor that uses $%y to 257664926Seric ** indicate YP lookups, you could enable that here. 257764718Seric */ 257864718Seric 257964718Seric bool 258064718Seric setvendor(vendor) 258164718Seric char *vendor; 258264718Seric { 258364926Seric if (strcasecmp(vendor, "Berkeley") == 0) 2584*68461Seric { 2585*68461Seric VendorCode = VENDOR_BERKELEY; 258664926Seric return TRUE; 2587*68461Seric } 258864926Seric 258964926Seric /* add vendor extensions here */ 259064926Seric 2591*68461Seric #ifdef SUN_EXTENSIONS 2592*68461Seric if (strcasecmp(vendor, "Sun") == 0) 2593*68461Seric { 2594*68461Seric VendorCode = VENDOR_SUN; 2595*68461Seric return TRUE; 2596*68461Seric } 2597*68461Seric #endif 2598*68461Seric 259964926Seric return FALSE; 260064718Seric } 260164816Seric /* 260264816Seric ** STRTOL -- convert string to long integer 260364816Seric ** 260464816Seric ** For systems that don't have it in the C library. 260566161Seric ** 260666161Seric ** This is taken verbatim from the 4.4-Lite C library. 260764816Seric */ 260864816Seric 260964816Seric #ifdef NEEDSTRTOL 261064816Seric 261166161Seric #if defined(LIBC_SCCS) && !defined(lint) 261266161Seric static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; 261366161Seric #endif /* LIBC_SCCS and not lint */ 261466161Seric 261566161Seric #include <limits.h> 261666161Seric 261766161Seric /* 261866161Seric * Convert a string to a long integer. 261966161Seric * 262066161Seric * Ignores `locale' stuff. Assumes that the upper and lower case 262166161Seric * alphabets and digits are each contiguous. 262266161Seric */ 262366161Seric 262464816Seric long 262566161Seric strtol(nptr, endptr, base) 262666161Seric const char *nptr; 262766161Seric char **endptr; 262866161Seric register int base; 262964816Seric { 263066161Seric register const char *s = nptr; 263166161Seric register unsigned long acc; 263266161Seric register int c; 263366161Seric register unsigned long cutoff; 263466161Seric register int neg = 0, any, cutlim; 263564816Seric 263666161Seric /* 263766161Seric * Skip white space and pick up leading +/- sign if any. 263866161Seric * If base is 0, allow 0x for hex and 0 for octal, else 263966161Seric * assume decimal; if base is already 16, allow 0x. 264066161Seric */ 264166161Seric do { 264266161Seric c = *s++; 264366161Seric } while (isspace(c)); 264466161Seric if (c == '-') { 264566161Seric neg = 1; 264666161Seric c = *s++; 264766161Seric } else if (c == '+') 264866161Seric c = *s++; 264966161Seric if ((base == 0 || base == 16) && 265066161Seric c == '0' && (*s == 'x' || *s == 'X')) { 265166161Seric c = s[1]; 265266161Seric s += 2; 265366161Seric base = 16; 265466161Seric } 265566161Seric if (base == 0) 265666161Seric base = c == '0' ? 8 : 10; 265764816Seric 265866161Seric /* 265966161Seric * Compute the cutoff value between legal numbers and illegal 266066161Seric * numbers. That is the largest legal value, divided by the 266166161Seric * base. An input number that is greater than this value, if 266266161Seric * followed by a legal input character, is too big. One that 266366161Seric * is equal to this value may be valid or not; the limit 266466161Seric * between valid and invalid numbers is then based on the last 266566161Seric * digit. For instance, if the range for longs is 266666161Seric * [-2147483648..2147483647] and the input base is 10, 266766161Seric * cutoff will be set to 214748364 and cutlim to either 266866161Seric * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated 266966161Seric * a value > 214748364, or equal but the next digit is > 7 (or 8), 267066161Seric * the number is too big, and we will return a range error. 267166161Seric * 267266161Seric * Set any if any `digits' consumed; make it negative to indicate 267366161Seric * overflow. 267466161Seric */ 267566161Seric cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 267666161Seric cutlim = cutoff % (unsigned long)base; 267766161Seric cutoff /= (unsigned long)base; 267866161Seric for (acc = 0, any = 0;; c = *s++) { 267966161Seric if (isdigit(c)) 268066161Seric c -= '0'; 268166161Seric else if (isalpha(c)) 268266161Seric c -= isupper(c) ? 'A' - 10 : 'a' - 10; 268366161Seric else 268466161Seric break; 268566161Seric if (c >= base) 268666161Seric break; 268766161Seric if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 268866161Seric any = -1; 268966161Seric else { 269066161Seric any = 1; 269166161Seric acc *= base; 269266161Seric acc += c; 269364816Seric } 269464816Seric } 269566161Seric if (any < 0) { 269666161Seric acc = neg ? LONG_MIN : LONG_MAX; 269766161Seric errno = ERANGE; 269866161Seric } else if (neg) 269966161Seric acc = -acc; 270066161Seric if (endptr != 0) 270166161Seric *endptr = (char *)(any ? s - 1 : nptr); 270266161Seric return (acc); 270364816Seric } 270464816Seric 270564816Seric #endif 270664841Seric /* 270764841Seric ** SOLARIS_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 270864841Seric ** 2709*68461Seric ** Solaris versions at least through 2.3 don't properly deliver a 271064841Seric ** canonical h_name field. This tries to work around it. 271164841Seric */ 271264841Seric 271364841Seric #ifdef SOLARIS 271464841Seric 2715*68461Seric extern int h_errno; 2716*68461Seric 271764841Seric struct hostent * 271864841Seric solaris_gethostbyname(name) 271965094Seric const char *name; 272064841Seric { 272164841Seric # ifdef SOLARIS_2_3 272264841Seric static struct hostent hp; 272364841Seric static char buf[1000]; 272464841Seric extern struct hostent *_switch_gethostbyname_r(); 272564841Seric 272664841Seric return _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 272764841Seric # else 272865009Seric extern struct hostent *__switch_gethostbyname(); 272965009Seric 273064841Seric return __switch_gethostbyname(name); 273164841Seric # endif 273264841Seric } 273364841Seric 273464841Seric struct hostent * 273564841Seric solaris_gethostbyaddr(addr, len, type) 273665094Seric const char *addr; 273764841Seric int len; 273864841Seric int type; 273964841Seric { 274064841Seric # ifdef SOLARIS_2_3 274164841Seric static struct hostent hp; 274264841Seric static char buf[1000]; 274364841Seric extern struct hostent *_switch_gethostbyaddr_r(); 274464841Seric 274564841Seric return _switch_gethostbyaddr_r(addr, len, type, &hp, buf, sizeof(buf), &h_errno); 274664841Seric # else 274765009Seric extern struct hostent *__switch_gethostbyaddr(); 274865009Seric 274964841Seric return __switch_gethostbyaddr(addr, len, type); 275064841Seric # endif 275164841Seric } 275264841Seric 275364841Seric #endif 275465214Seric /* 275565214Seric ** NI_PROPVAL -- netinfo property value lookup routine 275665214Seric ** 275765214Seric ** Parameters: 2758*68461Seric ** keydir -- the Netinfo directory name in which to search 2759*68461Seric ** for the key. 2760*68461Seric ** keyprop -- the name of the property in which to find the 2761*68461Seric ** property we are interested. Defaults to "name". 2762*68461Seric ** keyval -- the value for which we are really searching. 2763*68461Seric ** valprop -- the property name for the value in which we 2764*68461Seric ** are interested. 2765*68461Seric ** sepchar -- if non-nil, this can be multiple-valued, and 2766*68461Seric ** we should return a string separated by this 2767*68461Seric ** character. 276865214Seric ** 276965214Seric ** Returns: 277065214Seric ** NULL -- if: 277165214Seric ** 1. the directory is not found 277265214Seric ** 2. the property name is not found 277365214Seric ** 3. the property contains multiple values 277465214Seric ** 4. some error occured 277565214Seric ** else -- the location of the config file. 277665214Seric ** 2777*68461Seric ** Example: 2778*68461Seric ** To search for an alias value, use: 2779*68461Seric ** ni_propval("/aliases", "name", aliasname, "members", ',') 2780*68461Seric ** 278165214Seric ** Notes: 278265214Seric ** Caller should free the return value of ni_proval 278365214Seric */ 278465214Seric 278565214Seric #ifdef NETINFO 278665214Seric 278765214Seric # include <netinfo/ni.h> 278865214Seric 278965214Seric # define LOCAL_NETINFO_DOMAIN "." 279065214Seric # define PARENT_NETINFO_DOMAIN ".." 279165214Seric # define MAX_NI_LEVELS 256 279265214Seric 279365214Seric char * 2794*68461Seric ni_propval(keydir, keyprop, keyval, valprop, sepchar) 2795*68461Seric char *keydir; 2796*68461Seric char *keyprop; 2797*68461Seric char *keyval; 2798*68461Seric char *valprop; 2799*68461Seric char sepchar; 280065214Seric { 280165820Seric char *propval = NULL; 280265214Seric int i; 2803*68461Seric int j, alen; 280465214Seric void *ni = NULL; 280565214Seric void *lastni = NULL; 280665214Seric ni_status nis; 280765214Seric ni_id nid; 280865214Seric ni_namelist ninl; 2809*68461Seric register char *p; 2810*68461Seric char keybuf[1024]; 281165214Seric 281265214Seric /* 2813*68461Seric ** Create the full key from the two parts. 2814*68461Seric ** 2815*68461Seric ** Note that directory can end with, e.g., "name=" to specify 2816*68461Seric ** an alternate search property. 2817*68461Seric */ 2818*68461Seric 2819*68461Seric i = strlen(keydir) + strlen(keyval) + 2; 2820*68461Seric if (keyprop != NULL) 2821*68461Seric i += strlen(keyprop) + 1; 2822*68461Seric if (i > sizeof keybuf) 2823*68461Seric return NULL; 2824*68461Seric strcpy(keybuf, keydir); 2825*68461Seric strcat(keybuf, "/"); 2826*68461Seric if (keyprop != NULL) 2827*68461Seric { 2828*68461Seric strcat(keybuf, keyprop); 2829*68461Seric strcat(keybuf, "="); 2830*68461Seric } 2831*68461Seric strcat(keybuf, keyval); 2832*68461Seric 2833*68461Seric /* 283465214Seric ** If the passed directory and property name are found 283565214Seric ** in one of netinfo domains we need to search (starting 283665214Seric ** from the local domain moving all the way back to the 283765214Seric ** root domain) set propval to the property's value 283865214Seric ** and return it. 283965214Seric */ 284065214Seric 284165214Seric for (i = 0; i < MAX_NI_LEVELS; ++i) 284265214Seric { 284365214Seric if (i == 0) 284465214Seric { 284565214Seric nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni); 284665214Seric } 284765214Seric else 284865214Seric { 284965214Seric if (lastni != NULL) 285065214Seric ni_free(lastni); 285165214Seric lastni = ni; 285265214Seric nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni); 285365214Seric } 285465214Seric 285565214Seric /* 285665214Seric ** Don't bother if we didn't get a handle on a 285765214Seric ** proper domain. This is not necessarily an error. 285865214Seric ** We would get a positive ni_status if, for instance 285965214Seric ** we never found the directory or property and tried 286065214Seric ** to open the parent of the root domain! 286165214Seric */ 286265214Seric 286365214Seric if (nis != 0) 286465214Seric break; 286565214Seric 286665214Seric /* 286765214Seric ** Find the path to the server information. 286865214Seric */ 286965214Seric 2870*68461Seric if (ni_pathsearch(ni, &nid, keybuf) != 0) 287165214Seric continue; 287265214Seric 287365214Seric /* 2874*68461Seric ** Find associated value information. 287565214Seric */ 287665214Seric 2877*68461Seric if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0) 287865214Seric continue; 287965214Seric 288065214Seric /* 2881*68461Seric ** See if we have an acceptable number of values. 288265214Seric */ 288365214Seric 2884*68461Seric if (ninl.ni_namelist_len <= 0) 2885*68461Seric continue; 2886*68461Seric 2887*68461Seric if (sepchar == '\0' && ninl.ni_namelist_len > 1) 288865214Seric { 2889*68461Seric ni_namelist_free(&ninl); 2890*68461Seric continue; 289165214Seric } 2892*68461Seric 2893*68461Seric /* 2894*68461Seric ** Calculate number of bytes needed and build result 2895*68461Seric */ 2896*68461Seric 2897*68461Seric alen = 1; 2898*68461Seric for (j = 0; j < ninl.ni_namelist_len; j++) 2899*68461Seric alen += strlen(ninl.ni_namelist_val[j]) + 1; 2900*68461Seric propval = p = xalloc(alen); 2901*68461Seric for (j = 0; j < ninl.ni_namelist_len; j++) 2902*68461Seric { 2903*68461Seric strcpy(p, ninl.ni_namelist_val[j]); 2904*68461Seric p += strlen(p); 2905*68461Seric *p++ = sepchar; 2906*68461Seric } 2907*68461Seric *--p = '\0'; 2908*68461Seric 2909*68461Seric ni_namelist_free(&ninl); 291065214Seric } 291165214Seric 291265214Seric /* 291365214Seric ** Clean up. 291465214Seric */ 291565214Seric 291665214Seric if (ni != NULL) 291765214Seric ni_free(ni); 291865214Seric if (lastni != NULL && ni != lastni) 291965214Seric ni_free(lastni); 292065214Seric 292165214Seric return propval; 292265214Seric } 292365214Seric 292465214Seric #endif /* NETINFO */ 292565504Seric /* 292665504Seric ** HARD_SYSLOG -- call syslog repeatedly until it works 292765504Seric ** 292865504Seric ** Needed on HP-UX, which apparently doesn't guarantee that 292965504Seric ** syslog succeeds during interrupt handlers. 293065504Seric */ 293165504Seric 293265504Seric #ifdef __hpux 293365504Seric 293465504Seric # define MAXSYSLOGTRIES 100 293565504Seric # undef syslog 293665504Seric 293765504Seric # ifdef __STDC__ 293865504Seric hard_syslog(int pri, char *msg, ...) 293965504Seric # else 294065504Seric hard_syslog(pri, msg, va_alist) 294165504Seric int pri; 294265504Seric char *msg; 294365504Seric va_dcl 294465504Seric # endif 294565504Seric { 294665504Seric int i; 294765504Seric char buf[SYSLOG_BUFSIZE * 2]; 294865504Seric VA_LOCAL_DECL; 294965504Seric 295065504Seric VA_START(msg); 295165504Seric vsprintf(buf, msg, ap); 295265504Seric VA_END; 295365504Seric 295465504Seric for (i = MAXSYSLOGTRIES; --i >= 0 && syslog(pri, "%s", buf) < 0; ) 295565504Seric continue; 295665504Seric } 295765504Seric 295865504Seric #endif 2959