122700Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333780Sbostic * Copyright (c) 1988 Regents of the University of California. 433780Sbostic * All rights reserved. 533780Sbostic * 642825Sbostic * %sccs.include.redist.c% 733780Sbostic */ 822700Sdist 933932Sbostic #include <errno.h> 1040962Sbostic #include "sendmail.h" 114535Seric 1233780Sbostic #ifndef lint 1333780Sbostic #ifdef DAEMON 14*52106Seric static char sccsid[] = "@(#)daemon.c 5.42 (Berkeley) 01/04/92 (with daemon mode)"; 1533780Sbostic #else 16*52106Seric static char sccsid[] = "@(#)daemon.c 5.42 (Berkeley) 01/04/92 (without daemon mode)"; 1733780Sbostic #endif 1833780Sbostic #endif /* not lint */ 194535Seric 2033780Sbostic #ifdef DAEMON 2133780Sbostic 2223120Seric # include <netdb.h> 2324945Seric # include <sys/signal.h> 2423120Seric # include <sys/wait.h> 2523120Seric # include <sys/time.h> 2623120Seric # include <sys/resource.h> 275978Seric 284535Seric /* 294535Seric ** DAEMON.C -- routines to use when running as a daemon. 307556Seric ** 317556Seric ** This entire file is highly dependent on the 4.2 BSD 327556Seric ** interprocess communication primitives. No attempt has 337556Seric ** been made to make this file portable to Version 7, 347556Seric ** Version 6, MPX files, etc. If you should try such a 357556Seric ** thing yourself, I recommend chucking the entire file 367556Seric ** and starting from scratch. Basic semantics are: 377556Seric ** 387556Seric ** getrequests() 397556Seric ** Opens a port and initiates a connection. 407556Seric ** Returns in a child. Must set InChannel and 417556Seric ** OutChannel appropriately. 4210206Seric ** clrdaemon() 4310206Seric ** Close any open files associated with getting 4410206Seric ** the connection; this is used when running the queue, 4510206Seric ** etc., to avoid having extra file descriptors during 4610206Seric ** the queue run and to avoid confusing the network 4710206Seric ** code (if it cares). 48*52106Seric ** makeconnection(host, port, outfile, infile, usesecureport) 497556Seric ** Make a connection to the named host on the given 507556Seric ** port. Set *outfile and *infile to the files 517556Seric ** appropriate for communication. Returns zero on 527556Seric ** success, else an exit status describing the 537556Seric ** error. 5425699Seric ** maphostname(hbuf, hbufsize) 5525699Seric ** Convert the entry in hbuf into a canonical form. It 5625699Seric ** may not be larger than hbufsize. 574535Seric */ 584535Seric /* 594535Seric ** GETREQUESTS -- open mail IPC port and get requests. 604535Seric ** 614535Seric ** Parameters: 624535Seric ** none. 634535Seric ** 644535Seric ** Returns: 654535Seric ** none. 664535Seric ** 674535Seric ** Side Effects: 684535Seric ** Waits until some interesting activity occurs. When 694535Seric ** it does, a child is created to process it, and the 704535Seric ** parent waits for completion. Return from this 719886Seric ** routine is always in the child. The file pointers 729886Seric ** "InChannel" and "OutChannel" should be set to point 739886Seric ** to the communication channel. 744535Seric */ 754535Seric 7616144Seric int DaemonSocket = -1; /* fd describing socket */ 7716144Seric 784535Seric getrequests() 794535Seric { 809610Seric int t; 819610Seric register struct servent *sp; 8225027Seric int on = 1; 83*52106Seric struct sockaddr_in srvraddr; 8446928Sbostic extern void reapchild(); 857117Seric 869610Seric /* 879610Seric ** Set up the address for the mailer. 889610Seric */ 899610Seric 909610Seric sp = getservbyname("smtp", "tcp"); 919610Seric if (sp == NULL) 929610Seric { 939610Seric syserr("server \"smtp\" unknown"); 9410167Seric goto severe; 959610Seric } 96*52106Seric srvraddr.sin_family = AF_INET; 97*52106Seric srvraddr.sin_addr.s_addr = INADDR_ANY; 98*52106Seric srvraddr.sin_port = sp->s_port; 999610Seric 1009610Seric /* 1019610Seric ** Try to actually open the connection. 1029610Seric */ 1039610Seric 1049610Seric if (tTd(15, 1)) 105*52106Seric printf("getrequests: port 0x%x\n", srvraddr.sin_port); 1069610Seric 1079610Seric /* get a socket for the SMTP connection */ 10823120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 10910206Seric if (DaemonSocket < 0) 1109610Seric { 1119610Seric /* probably another daemon already */ 1129610Seric syserr("getrequests: can't create socket"); 1139610Seric severe: 1149610Seric # ifdef LOG 1159610Seric if (LogLevel > 0) 11624858Seric syslog(LOG_ALERT, "cannot get connection"); 1179610Seric # endif LOG 1189610Seric finis(); 1199610Seric } 12010347Seric 12110347Seric /* turn on network debugging? */ 12210347Seric if (tTd(15, 15)) 12324945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 12410347Seric 12525027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 12625027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 12725027Seric 128*52106Seric if (bind(DaemonSocket, (struct sockaddr *)&srvraddr, sizeof srvraddr) < 0) 1299610Seric { 1309610Seric syserr("getrequests: cannot bind"); 13110206Seric (void) close(DaemonSocket); 1329610Seric goto severe; 1339610Seric } 13423120Seric if (listen(DaemonSocket, 10) < 0) 13523120Seric { 13623120Seric syserr("getrequests: cannot listen"); 13723120Seric (void) close(DaemonSocket); 13823120Seric goto severe; 13923120Seric } 1409610Seric 14124955Seric (void) signal(SIGCHLD, reapchild); 14224945Seric 1439610Seric if (tTd(15, 1)) 14410206Seric printf("getrequests: %d\n", DaemonSocket); 1459610Seric 1464631Seric for (;;) 1474631Seric { 14814875Seric register int pid; 14911147Seric auto int lotherend; 15011147Seric 15114875Seric /* see if we are rejecting connections */ 15251920Seric while ((CurrentLA = getla()) > RefuseLA) 15336584Sbostic { 15451920Seric setproctitle("rejecting connections: load average: %.2f", (double)CurrentLA); 15514875Seric sleep(5); 15636584Sbostic } 15714875Seric 1589610Seric /* wait for a connection */ 15936584Sbostic setproctitle("accepting connections"); 1609610Seric do 1619610Seric { 1629610Seric errno = 0; 16336230Skarels lotherend = sizeof RealHostAddr; 16446928Sbostic t = accept(DaemonSocket, 16546928Sbostic (struct sockaddr *)&RealHostAddr, &lotherend); 1669610Seric } while (t < 0 && errno == EINTR); 1679610Seric if (t < 0) 1685978Seric { 1699610Seric syserr("getrequests: accept"); 1709610Seric sleep(5); 1719610Seric continue; 1725978Seric } 1734631Seric 1745978Seric /* 1755978Seric ** Create a subprocess to process the mail. 1765978Seric */ 1775978Seric 1787677Seric if (tTd(15, 2)) 1799610Seric printf("getrequests: forking (fd = %d)\n", t); 1805978Seric 1814636Seric pid = fork(); 1824636Seric if (pid < 0) 1834631Seric { 1844636Seric syserr("daemon: cannot fork"); 1854636Seric sleep(10); 1869610Seric (void) close(t); 1874636Seric continue; 1884631Seric } 1894631Seric 1904636Seric if (pid == 0) 1914631Seric { 19211147Seric extern struct hostent *gethostbyaddr(); 19311147Seric register struct hostent *hp; 19411147Seric char buf[MAXNAME]; 19511147Seric 1964636Seric /* 1974636Seric ** CHILD -- return to caller. 19811147Seric ** Collect verified idea of sending host. 1994636Seric ** Verify calling user id if possible here. 2004636Seric */ 2014631Seric 20224955Seric (void) signal(SIGCHLD, SIG_DFL); 20324950Seric 20411147Seric /* determine host name */ 20536230Skarels hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 20611147Seric if (hp != NULL) 20723104Seric (void) strcpy(buf, hp->h_name); 20811147Seric else 20916884Seric { 21016884Seric extern char *inet_ntoa(); 21116884Seric 21216884Seric /* produce a dotted quad */ 21316884Seric (void) sprintf(buf, "[%s]", 21436230Skarels inet_ntoa(RealHostAddr.sin_addr)); 21516884Seric } 21616884Seric 21716884Seric /* should we check for illegal connection here? XXX */ 21816884Seric 21911147Seric RealHostName = newstr(buf); 22011147Seric 22110206Seric (void) close(DaemonSocket); 2229610Seric InChannel = fdopen(t, "r"); 22321062Seric OutChannel = fdopen(dup(t), "w"); 2247677Seric if (tTd(15, 2)) 2255978Seric printf("getreq: returning\n"); 2267876Seric # ifdef LOG 2277876Seric if (LogLevel > 11) 2287876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2297876Seric # endif LOG 2304636Seric return; 2314631Seric } 2324631Seric 2337117Seric /* close the port so that others will hang (for a while) */ 2349610Seric (void) close(t); 2354631Seric } 2369886Seric /*NOTREACHED*/ 2374631Seric } 2385978Seric /* 23910206Seric ** CLRDAEMON -- reset the daemon connection 24010206Seric ** 24110206Seric ** Parameters: 24210206Seric ** none. 24310206Seric ** 24410206Seric ** Returns: 24510206Seric ** none. 24610206Seric ** 24710206Seric ** Side Effects: 24810206Seric ** releases any resources used by the passive daemon. 24910206Seric */ 25010206Seric 25110206Seric clrdaemon() 25210206Seric { 25310206Seric if (DaemonSocket >= 0) 25410206Seric (void) close(DaemonSocket); 25510206Seric DaemonSocket = -1; 25610206Seric } 25710206Seric /* 2586039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2596039Seric ** 2606039Seric ** Parameters: 2616039Seric ** host -- the name of the host. 2626633Seric ** port -- the port number to connect to. 2636039Seric ** outfile -- a pointer to a place to put the outfile 2646039Seric ** descriptor. 2656039Seric ** infile -- ditto for infile. 266*52106Seric ** usesecureport -- if set, use a low numbered (reserved) 267*52106Seric ** port to provide some rudimentary authentication. 2686039Seric ** 2696039Seric ** Returns: 2706039Seric ** An exit code telling whether the connection could be 2716039Seric ** made and if not why not. 2726039Seric ** 2736039Seric ** Side Effects: 2746039Seric ** none. 2756039Seric */ 2765978Seric 277*52106Seric makeconnection(host, port, outfile, infile, usesecureport) 2786039Seric char *host; 2797286Seric u_short port; 2806039Seric FILE **outfile; 2816039Seric FILE **infile; 282*52106Seric bool usesecureport; 2836039Seric { 28429430Sbloom register int i, s; 28529430Sbloom register struct hostent *hp = (struct hostent *)NULL; 286*52106Seric struct sockaddr_in addr; 287*52106Seric int sav_errno; 28829430Sbloom extern char *inet_ntoa(); 28935651Seric #ifdef NAMED_BIND 29035651Seric extern int h_errno; 29135651Seric #endif 2926039Seric 2936039Seric /* 2946039Seric ** Set up the address for the mailer. 2959308Seric ** Accept "[a.b.c.d]" syntax for host name. 2966039Seric */ 2976039Seric 29835651Seric #ifdef NAMED_BIND 29925475Smiriam h_errno = 0; 30035651Seric #endif 30125475Smiriam errno = 0; 30225475Smiriam 3039308Seric if (host[0] == '[') 3049308Seric { 30511147Seric long hid; 30611147Seric register char *p = index(host, ']'); 3079308Seric 30811147Seric if (p != NULL) 3099308Seric { 31011147Seric *p = '\0'; 31111147Seric hid = inet_addr(&host[1]); 31211147Seric *p = ']'; 3139308Seric } 31411147Seric if (p == NULL || hid == -1) 3159308Seric { 3169308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3179308Seric return (EX_NOHOST); 3189308Seric } 319*52106Seric addr.sin_addr.s_addr = hid; 3209308Seric } 3219610Seric else 3229610Seric { 32329430Sbloom hp = gethostbyname(host); 32425475Smiriam if (hp == NULL) 32524945Seric { 32635651Seric #ifdef NAMED_BIND 32725475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 32825475Smiriam return (EX_TEMPFAIL); 32925657Seric 33035651Seric /* if name server is specified, assume temp fail */ 33135651Seric if (errno == ECONNREFUSED && UseNameServer) 33235651Seric return (EX_TEMPFAIL); 33335651Seric #endif 33435651Seric 33525657Seric /* 33625657Seric ** XXX Should look for mail forwarder record here 33725657Seric ** XXX if (h_errno == NO_ADDRESS). 33825657Seric */ 33925657Seric 34025475Smiriam return (EX_NOHOST); 34124945Seric } 342*52106Seric bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length); 34329430Sbloom i = 1; 3449610Seric } 3459610Seric 3469610Seric /* 3479610Seric ** Determine the port number. 3489610Seric */ 3499610Seric 35010011Seric if (port != 0) 351*52106Seric addr.sin_port = htons(port); 35210011Seric else 3539610Seric { 3549610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3559610Seric 3569610Seric if (sp == NULL) 3579610Seric { 3589610Seric syserr("makeconnection: server \"smtp\" unknown"); 3599610Seric return (EX_OSFILE); 3609610Seric } 361*52106Seric addr.sin_port = sp->s_port; 3629610Seric } 3636039Seric 3646039Seric /* 3656039Seric ** Try to actually open the connection. 3666039Seric */ 3676039Seric 36829430Sbloom again: 3697677Seric if (tTd(16, 1)) 37029430Sbloom printf("makeconnection (%s [%s])\n", host, 371*52106Seric inet_ntoa(addr.sin_addr)); 3726039Seric 373*52106Seric if (usesecureport) 374*52106Seric { 375*52106Seric int rport = IPPORT_RESERVED - 1; 376*52106Seric 377*52106Seric s = rresvport(&rport); 378*52106Seric } 379*52106Seric else 380*52106Seric { 381*52106Seric s = socket(AF_INET, SOCK_STREAM, 0); 382*52106Seric } 3836039Seric if (s < 0) 3846039Seric { 385*52106Seric sav_errno = errno; 3866039Seric syserr("makeconnection: no socket"); 3876039Seric goto failure; 3886039Seric } 3896039Seric 3907677Seric if (tTd(16, 1)) 3916039Seric printf("makeconnection: %d\n", s); 39210347Seric 39310347Seric /* turn on network debugging? */ 39410347Seric if (tTd(16, 14)) 39524945Seric { 39624945Seric int on = 1; 39724945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 39824945Seric } 39940932Srick if (CurEnv->e_xfp != NULL) 40040932Srick (void) fflush(CurEnv->e_xfp); /* for debugging */ 40114383Seric errno = 0; /* for debugging */ 402*52106Seric addr.sin_family = AF_INET; 403*52106Seric if (connect(s, (struct sockaddr *) &addr, sizeof addr) < 0) 4046039Seric { 40527744Sbloom sav_errno = errno; 40627744Sbloom (void) close(s); 40729430Sbloom if (hp && hp->h_addr_list[i]) 40829430Sbloom { 409*52106Seric bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr, 410*52106Seric hp->h_length); 41129430Sbloom goto again; 41229430Sbloom } 41329430Sbloom 4146039Seric /* failure, decide if temporary or not */ 4156039Seric failure: 41627744Sbloom switch (sav_errno) 4176039Seric { 4186039Seric case EISCONN: 4196039Seric case ETIMEDOUT: 4206897Seric case EINPROGRESS: 4216897Seric case EALREADY: 4226897Seric case EADDRINUSE: 42310123Seric case EHOSTDOWN: 4246897Seric case ENETDOWN: 4256897Seric case ENETRESET: 4266897Seric case ENOBUFS: 4277204Seric case ECONNREFUSED: 42811546Seric case ECONNRESET: 42910081Seric case EHOSTUNREACH: 43010098Seric case ENETUNREACH: 43151995Seric #ifdef ENOSR 43251995Seric case ENOSR: 43351995Seric #endif 4346039Seric /* there are others, I'm sure..... */ 4356039Seric return (EX_TEMPFAIL); 4366039Seric 43711147Seric case EPERM: 43811147Seric /* why is this happening? */ 43911147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 440*52106Seric addr.sin_addr.s_addr, addr.sin_port); 44114383Seric return (EX_TEMPFAIL); 44211147Seric 4436039Seric default: 44440932Srick { 44540932Srick extern char *errstring(); 44640932Srick 44740932Srick message(Arpa_Info, "%s", errstring(sav_errno)); 44840932Srick return (EX_UNAVAILABLE); 44940932Srick } 4506039Seric } 4516039Seric } 4526039Seric 4536039Seric /* connection ok, put it into canonical form */ 4546039Seric *outfile = fdopen(s, "w"); 45540992Sbostic *infile = fdopen(dup(s), "r"); 4566039Seric 45710098Seric return (EX_OK); 4586039Seric } 45910758Seric /* 46010758Seric ** MYHOSTNAME -- return the name of this host. 46110758Seric ** 46210758Seric ** Parameters: 46310758Seric ** hostbuf -- a place to return the name of this host. 46412313Seric ** size -- the size of hostbuf. 46510758Seric ** 46610758Seric ** Returns: 46710758Seric ** A list of aliases for this host. 46810758Seric ** 46910758Seric ** Side Effects: 47010758Seric ** none. 47110758Seric */ 4726039Seric 47310758Seric char ** 47412313Seric myhostname(hostbuf, size) 47510758Seric char hostbuf[]; 47612313Seric int size; 47710758Seric { 47810758Seric extern struct hostent *gethostbyname(); 47911147Seric struct hostent *hp; 48010758Seric 48123120Seric if (gethostname(hostbuf, size) < 0) 48223120Seric { 48323120Seric (void) strcpy(hostbuf, "localhost"); 48423120Seric } 48511147Seric hp = gethostbyname(hostbuf); 48611147Seric if (hp != NULL) 48716877Seric { 48823104Seric (void) strcpy(hostbuf, hp->h_name); 48911147Seric return (hp->h_aliases); 49016877Seric } 49110758Seric else 49210758Seric return (NULL); 49310758Seric } 49451315Seric /* 49533932Sbostic * MAPHOSTNAME -- turn a hostname into canonical form 49633932Sbostic * 49733932Sbostic * Parameters: 49833932Sbostic * hbuf -- a buffer containing a hostname. 49933932Sbostic * hbsize -- the size of hbuf. 50033932Sbostic * 50133932Sbostic * Returns: 50251315Seric * TRUE if the host name was mapped. 50351315Seric * FALSE otherwise. 50433932Sbostic * 50533932Sbostic * Side Effects: 50633932Sbostic * Looks up the host specified in hbuf. If it is not 50733932Sbostic * the canonical name for that host, replace it with 50833932Sbostic * the canonical name. If the name is unknown, or it 50933932Sbostic * is already the canonical name, leave it unchanged. 51033932Sbostic */ 51151315Seric 51251315Seric bool 51316911Seric maphostname(hbuf, hbsize) 51416911Seric char *hbuf; 51516911Seric int hbsize; 51616911Seric { 51716911Seric register struct hostent *hp; 51833932Sbostic u_long in_addr; 51940994Sbostic char ptr[256], *cp; 52033932Sbostic struct hostent *gethostbyaddr(); 52116911Seric 52225574Smiriam /* 52333932Sbostic * If first character is a bracket, then it is an address 52433932Sbostic * lookup. Address is copied into a temporary buffer to 52533932Sbostic * strip the brackets and to preserve hbuf if address is 52633932Sbostic * unknown. 52733932Sbostic */ 52851315Seric if (*hbuf != '[') 52951315Seric return (getcanonname(hbuf, hbsize)); 53040994Sbostic if ((cp = index(strcpy(ptr, hbuf), ']')) == NULL) 53151315Seric return (FALSE); 53240994Sbostic *cp = '\0'; 53333932Sbostic in_addr = inet_addr(&ptr[1]); 53433932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 53533932Sbostic if (hp == NULL) 53651315Seric return (FALSE); 53733932Sbostic if (strlen(hp->h_name) >= hbsize) 53833932Sbostic hp->h_name[hbsize - 1] = '\0'; 53933932Sbostic (void)strcpy(hbuf, hp->h_name); 54051315Seric return (TRUE); 54133932Sbostic } 54216911Seric 54310758Seric # else DAEMON 54416911Seric /* code for systems without sophisticated networking */ 54510758Seric 54610758Seric /* 54710758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 54811297Seric ** 54911297Seric ** Can't convert to upper case here because might be a UUCP name. 55012313Seric ** 55112313Seric ** Mark, you can change this to be anything you want...... 55210758Seric */ 55310758Seric 55410758Seric char ** 55512313Seric myhostname(hostbuf, size) 55610758Seric char hostbuf[]; 55712313Seric int size; 55810758Seric { 55910758Seric register FILE *f; 56010758Seric 56110758Seric hostbuf[0] = '\0'; 56210758Seric f = fopen("/usr/include/whoami", "r"); 56310758Seric if (f != NULL) 56410758Seric { 56512313Seric (void) fgets(hostbuf, size, f); 56610758Seric fixcrlf(hostbuf, TRUE); 56710758Seric (void) fclose(f); 56810758Seric } 56910758Seric return (NULL); 57010758Seric } 57116911Seric /* 57216911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 57316911Seric ** 57416911Seric ** Parameters: 57516911Seric ** hbuf -- a buffer containing a hostname. 57616911Seric ** hbsize -- the size of hbuf. 57716911Seric ** 57816911Seric ** Returns: 57951315Seric ** TRUE if the hostname was mapped. 58051315Seric ** FALSE otherwise. 58116911Seric ** 58216911Seric ** Side Effects: 58316911Seric ** Looks up the host specified in hbuf. If it is not 58416911Seric ** the canonical name for that host, replace it with 58516911Seric ** the canonical name. If the name is unknown, or it 58616911Seric ** is already the canonical name, leave it unchanged. 58716911Seric */ 58810758Seric 58916911Seric /*ARGSUSED*/ 59051315Seric bool 59116911Seric maphostname(hbuf, hbsize) 59216911Seric char *hbuf; 59316911Seric int hbsize; 59416911Seric { 59551315Seric return (FALSE); 59616911Seric } 59716911Seric 5985978Seric #endif DAEMON 599