122700Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 333780Sbostic * Copyright (c) 1988 Regents of the University of California. 433780Sbostic * All rights reserved. 533780Sbostic * 633780Sbostic * Redistribution and use in source and binary forms are permitted 734920Sbostic * provided that the above copyright notice and this paragraph are 834920Sbostic * duplicated in all such forms and that any documentation, 934920Sbostic * advertising materials, and other materials related to such 1034920Sbostic * distribution and use acknowledge that the software was developed 1134920Sbostic * by the University of California, Berkeley. The name of the 1234920Sbostic * University may not be used to endorse or promote products derived 1334920Sbostic * from this software without specific prior written permission. 1434920Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1534920Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1634920Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1733780Sbostic */ 1822700Sdist 1933932Sbostic #include <errno.h> 2033932Sbostic #include <sendmail.h> 214535Seric 2233780Sbostic #ifndef lint 2333780Sbostic #ifdef DAEMON 24*36230Skarels static char sccsid[] = "@(#)daemon.c 5.28 (Berkeley) 11/17/88 (with daemon mode)"; 2533780Sbostic #else 26*36230Skarels static char sccsid[] = "@(#)daemon.c 5.28 (Berkeley) 11/17/88 (without daemon mode)"; 2733780Sbostic #endif 2833780Sbostic #endif /* not lint */ 294535Seric 3033780Sbostic #ifdef DAEMON 3133780Sbostic 3223120Seric # include <netdb.h> 3324945Seric # include <sys/signal.h> 3423120Seric # include <sys/wait.h> 3523120Seric # include <sys/time.h> 3623120Seric # include <sys/resource.h> 375978Seric 384535Seric /* 394535Seric ** DAEMON.C -- routines to use when running as a daemon. 407556Seric ** 417556Seric ** This entire file is highly dependent on the 4.2 BSD 427556Seric ** interprocess communication primitives. No attempt has 437556Seric ** been made to make this file portable to Version 7, 447556Seric ** Version 6, MPX files, etc. If you should try such a 457556Seric ** thing yourself, I recommend chucking the entire file 467556Seric ** and starting from scratch. Basic semantics are: 477556Seric ** 487556Seric ** getrequests() 497556Seric ** Opens a port and initiates a connection. 507556Seric ** Returns in a child. Must set InChannel and 517556Seric ** OutChannel appropriately. 5210206Seric ** clrdaemon() 5310206Seric ** Close any open files associated with getting 5410206Seric ** the connection; this is used when running the queue, 5510206Seric ** etc., to avoid having extra file descriptors during 5610206Seric ** the queue run and to avoid confusing the network 5710206Seric ** code (if it cares). 587556Seric ** makeconnection(host, port, outfile, infile) 597556Seric ** Make a connection to the named host on the given 607556Seric ** port. Set *outfile and *infile to the files 617556Seric ** appropriate for communication. Returns zero on 627556Seric ** success, else an exit status describing the 637556Seric ** error. 6425699Seric ** maphostname(hbuf, hbufsize) 6525699Seric ** Convert the entry in hbuf into a canonical form. It 6625699Seric ** may not be larger than hbufsize. 674535Seric */ 684535Seric /* 694535Seric ** GETREQUESTS -- open mail IPC port and get requests. 704535Seric ** 714535Seric ** Parameters: 724535Seric ** none. 734535Seric ** 744535Seric ** Returns: 754535Seric ** none. 764535Seric ** 774535Seric ** Side Effects: 784535Seric ** Waits until some interesting activity occurs. When 794535Seric ** it does, a child is created to process it, and the 804535Seric ** parent waits for completion. Return from this 819886Seric ** routine is always in the child. The file pointers 829886Seric ** "InChannel" and "OutChannel" should be set to point 839886Seric ** to the communication channel. 844535Seric */ 854535Seric 8610206Seric struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 879610Seric 8816144Seric int DaemonSocket = -1; /* fd describing socket */ 8916890Seric char *NetName; /* name of home (local?) network */ 9016144Seric 914535Seric getrequests() 924535Seric { 939610Seric int t; 949610Seric register struct servent *sp; 9525027Seric int on = 1; 9624945Seric extern reapchild(); 977117Seric 989610Seric /* 999610Seric ** Set up the address for the mailer. 1009610Seric */ 1019610Seric 1029610Seric sp = getservbyname("smtp", "tcp"); 1039610Seric if (sp == NULL) 1049610Seric { 1059610Seric syserr("server \"smtp\" unknown"); 10610167Seric goto severe; 1079610Seric } 1089610Seric SendmailAddress.sin_family = AF_INET; 1099610Seric SendmailAddress.sin_addr.s_addr = INADDR_ANY; 1109740Ssam SendmailAddress.sin_port = sp->s_port; 1119610Seric 1129610Seric /* 1139610Seric ** Try to actually open the connection. 1149610Seric */ 1159610Seric 1169610Seric # ifdef DEBUG 1179610Seric if (tTd(15, 1)) 1189610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 1199610Seric # endif DEBUG 1209610Seric 1219610Seric /* get a socket for the SMTP connection */ 12223120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 12310206Seric if (DaemonSocket < 0) 1249610Seric { 1259610Seric /* probably another daemon already */ 1269610Seric syserr("getrequests: can't create socket"); 1279610Seric severe: 1289610Seric # ifdef LOG 1299610Seric if (LogLevel > 0) 13024858Seric syslog(LOG_ALERT, "cannot get connection"); 1319610Seric # endif LOG 1329610Seric finis(); 1339610Seric } 13410347Seric 13510347Seric #ifdef DEBUG 13610347Seric /* turn on network debugging? */ 13710347Seric if (tTd(15, 15)) 13824945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 13910347Seric #endif DEBUG 14010347Seric 14125027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 14225027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 14325027Seric 14423120Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 1459610Seric { 1469610Seric syserr("getrequests: cannot bind"); 14710206Seric (void) close(DaemonSocket); 1489610Seric goto severe; 1499610Seric } 15023120Seric if (listen(DaemonSocket, 10) < 0) 15123120Seric { 15223120Seric syserr("getrequests: cannot listen"); 15323120Seric (void) close(DaemonSocket); 15423120Seric goto severe; 15523120Seric } 1569610Seric 15724955Seric (void) signal(SIGCHLD, reapchild); 15824945Seric 1599610Seric # ifdef DEBUG 1609610Seric if (tTd(15, 1)) 16110206Seric printf("getrequests: %d\n", DaemonSocket); 1629610Seric # endif DEBUG 1639610Seric 1644631Seric for (;;) 1654631Seric { 16614875Seric register int pid; 16711147Seric auto int lotherend; 16814875Seric extern int RefuseLA; 16911147Seric 17014875Seric /* see if we are rejecting connections */ 17114875Seric while (getla() > RefuseLA) 17214875Seric sleep(5); 17314875Seric 1749610Seric /* wait for a connection */ 1759610Seric do 1769610Seric { 1779610Seric errno = 0; 178*36230Skarels lotherend = sizeof RealHostAddr; 179*36230Skarels t = accept(DaemonSocket, &RealHostAddr, &lotherend); 1809610Seric } while (t < 0 && errno == EINTR); 1819610Seric if (t < 0) 1825978Seric { 1839610Seric syserr("getrequests: accept"); 1849610Seric sleep(5); 1859610Seric continue; 1865978Seric } 1874631Seric 1885978Seric /* 1895978Seric ** Create a subprocess to process the mail. 1905978Seric */ 1915978Seric 1925978Seric # ifdef DEBUG 1937677Seric if (tTd(15, 2)) 1949610Seric printf("getrequests: forking (fd = %d)\n", t); 1955978Seric # endif DEBUG 1965978Seric 1974636Seric pid = fork(); 1984636Seric if (pid < 0) 1994631Seric { 2004636Seric syserr("daemon: cannot fork"); 2014636Seric sleep(10); 2029610Seric (void) close(t); 2034636Seric continue; 2044631Seric } 2054631Seric 2064636Seric if (pid == 0) 2074631Seric { 20811147Seric extern struct hostent *gethostbyaddr(); 20911147Seric register struct hostent *hp; 21011147Seric char buf[MAXNAME]; 21111147Seric 2124636Seric /* 2134636Seric ** CHILD -- return to caller. 21411147Seric ** Collect verified idea of sending host. 2154636Seric ** Verify calling user id if possible here. 2164636Seric */ 2174631Seric 21824955Seric (void) signal(SIGCHLD, SIG_DFL); 21924950Seric 22011147Seric /* determine host name */ 221*36230Skarels hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 22211147Seric if (hp != NULL) 22323104Seric (void) strcpy(buf, hp->h_name); 22411147Seric else 22516884Seric { 22616884Seric extern char *inet_ntoa(); 22716884Seric 22816884Seric /* produce a dotted quad */ 22916884Seric (void) sprintf(buf, "[%s]", 230*36230Skarels inet_ntoa(RealHostAddr.sin_addr)); 23116884Seric } 23216884Seric 23316884Seric /* should we check for illegal connection here? XXX */ 23416884Seric 23511147Seric RealHostName = newstr(buf); 23611147Seric 23710206Seric (void) close(DaemonSocket); 2389610Seric InChannel = fdopen(t, "r"); 23921062Seric OutChannel = fdopen(dup(t), "w"); 2405978Seric # ifdef DEBUG 2417677Seric if (tTd(15, 2)) 2425978Seric printf("getreq: returning\n"); 2435978Seric # endif DEBUG 2447876Seric # ifdef LOG 2457876Seric if (LogLevel > 11) 2467876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2477876Seric # endif LOG 2484636Seric return; 2494631Seric } 2504631Seric 2517117Seric /* close the port so that others will hang (for a while) */ 2529610Seric (void) close(t); 2534631Seric } 2549886Seric /*NOTREACHED*/ 2554631Seric } 2565978Seric /* 25710206Seric ** CLRDAEMON -- reset the daemon connection 25810206Seric ** 25910206Seric ** Parameters: 26010206Seric ** none. 26110206Seric ** 26210206Seric ** Returns: 26310206Seric ** none. 26410206Seric ** 26510206Seric ** Side Effects: 26610206Seric ** releases any resources used by the passive daemon. 26710206Seric */ 26810206Seric 26910206Seric clrdaemon() 27010206Seric { 27110206Seric if (DaemonSocket >= 0) 27210206Seric (void) close(DaemonSocket); 27310206Seric DaemonSocket = -1; 27410206Seric } 27510206Seric /* 2766039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2776039Seric ** 2786039Seric ** Parameters: 2796039Seric ** host -- the name of the host. 2806633Seric ** port -- the port number to connect to. 2816039Seric ** outfile -- a pointer to a place to put the outfile 2826039Seric ** descriptor. 2836039Seric ** infile -- ditto for infile. 2846039Seric ** 2856039Seric ** Returns: 2866039Seric ** An exit code telling whether the connection could be 2876039Seric ** made and if not why not. 2886039Seric ** 2896039Seric ** Side Effects: 2906039Seric ** none. 2916039Seric */ 2925978Seric 2936633Seric makeconnection(host, port, outfile, infile) 2946039Seric char *host; 2957286Seric u_short port; 2966039Seric FILE **outfile; 2976039Seric FILE **infile; 2986039Seric { 29929430Sbloom register int i, s; 30029430Sbloom register struct hostent *hp = (struct hostent *)NULL; 30129430Sbloom extern char *inet_ntoa(); 30227744Sbloom int sav_errno; 30335651Seric #ifdef NAMED_BIND 30435651Seric extern int h_errno; 30535651Seric #endif 3066039Seric 3076039Seric /* 3086039Seric ** Set up the address for the mailer. 3099308Seric ** Accept "[a.b.c.d]" syntax for host name. 3106039Seric */ 3116039Seric 31235651Seric #ifdef NAMED_BIND 31325475Smiriam h_errno = 0; 31435651Seric #endif 31525475Smiriam errno = 0; 31625475Smiriam 3179308Seric if (host[0] == '[') 3189308Seric { 31911147Seric long hid; 32011147Seric register char *p = index(host, ']'); 3219308Seric 32211147Seric if (p != NULL) 3239308Seric { 32411147Seric *p = '\0'; 32511147Seric hid = inet_addr(&host[1]); 32611147Seric *p = ']'; 3279308Seric } 32811147Seric if (p == NULL || hid == -1) 3299308Seric { 3309308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3319308Seric return (EX_NOHOST); 3329308Seric } 3339308Seric SendmailAddress.sin_addr.s_addr = hid; 3349308Seric } 3359610Seric else 3369610Seric { 33729430Sbloom hp = gethostbyname(host); 33825475Smiriam if (hp == NULL) 33924945Seric { 34035651Seric #ifdef NAMED_BIND 34125475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 34225475Smiriam return (EX_TEMPFAIL); 34325657Seric 34435651Seric /* if name server is specified, assume temp fail */ 34535651Seric if (errno == ECONNREFUSED && UseNameServer) 34635651Seric return (EX_TEMPFAIL); 34735651Seric #endif 34835651Seric 34925657Seric /* 35025657Seric ** XXX Should look for mail forwarder record here 35125657Seric ** XXX if (h_errno == NO_ADDRESS). 35225657Seric */ 35325657Seric 35425475Smiriam return (EX_NOHOST); 35524945Seric } 35616884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 35729430Sbloom i = 1; 3589610Seric } 3599610Seric 3609610Seric /* 3619610Seric ** Determine the port number. 3629610Seric */ 3639610Seric 36410011Seric if (port != 0) 36510011Seric SendmailAddress.sin_port = htons(port); 36610011Seric else 3679610Seric { 3689610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3699610Seric 3709610Seric if (sp == NULL) 3719610Seric { 3729610Seric syserr("makeconnection: server \"smtp\" unknown"); 3739610Seric return (EX_OSFILE); 3749610Seric } 37510011Seric SendmailAddress.sin_port = sp->s_port; 3769610Seric } 3776039Seric 3786039Seric /* 3796039Seric ** Try to actually open the connection. 3806039Seric */ 3816039Seric 38229430Sbloom again: 3836039Seric # ifdef DEBUG 3847677Seric if (tTd(16, 1)) 38529430Sbloom printf("makeconnection (%s [%s])\n", host, 38629430Sbloom inet_ntoa(SendmailAddress.sin_addr.s_addr)); 3876039Seric # endif DEBUG 3886039Seric 38923120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3906039Seric if (s < 0) 3916039Seric { 3926039Seric syserr("makeconnection: no socket"); 39327744Sbloom sav_errno = errno; 3946039Seric goto failure; 3956039Seric } 3966039Seric 3976039Seric # ifdef DEBUG 3987677Seric if (tTd(16, 1)) 3996039Seric printf("makeconnection: %d\n", s); 40010347Seric 40110347Seric /* turn on network debugging? */ 40210347Seric if (tTd(16, 14)) 40324945Seric { 40424945Seric int on = 1; 40524945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 40624945Seric } 4076039Seric # endif DEBUG 4089546Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 40914383Seric errno = 0; /* for debugging */ 4109610Seric SendmailAddress.sin_family = AF_INET; 41123120Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0) 4126039Seric { 41327744Sbloom sav_errno = errno; 41427744Sbloom (void) close(s); 41529430Sbloom if (hp && hp->h_addr_list[i]) 41629430Sbloom { 41729430Sbloom bcopy(hp->h_addr_list[i++], 41829430Sbloom (char *)&SendmailAddress.sin_addr, hp->h_length); 41929430Sbloom goto again; 42029430Sbloom } 42129430Sbloom 4226039Seric /* failure, decide if temporary or not */ 4236039Seric failure: 42427744Sbloom switch (sav_errno) 4256039Seric { 4266039Seric case EISCONN: 4276039Seric case ETIMEDOUT: 4286897Seric case EINPROGRESS: 4296897Seric case EALREADY: 4306897Seric case EADDRINUSE: 43110123Seric case EHOSTDOWN: 4326897Seric case ENETDOWN: 4336897Seric case ENETRESET: 4346897Seric case ENOBUFS: 4357204Seric case ECONNREFUSED: 43611546Seric case ECONNRESET: 43710081Seric case EHOSTUNREACH: 43810098Seric case ENETUNREACH: 4396039Seric /* there are others, I'm sure..... */ 4406039Seric return (EX_TEMPFAIL); 4416039Seric 44211147Seric case EPERM: 44311147Seric /* why is this happening? */ 44411147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 44511147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 44614383Seric return (EX_TEMPFAIL); 44711147Seric 4486039Seric default: 44931953Sbostic message(Arpa_Info, "%s", errstring(sav_errno)); 4506039Seric return (EX_UNAVAILABLE); 4516039Seric } 4526039Seric } 4536039Seric 4546039Seric /* connection ok, put it into canonical form */ 4556039Seric *outfile = fdopen(s, "w"); 4566039Seric *infile = fdopen(s, "r"); 4576039Seric 45810098Seric return (EX_OK); 4596039Seric } 46010758Seric /* 46110758Seric ** MYHOSTNAME -- return the name of this host. 46210758Seric ** 46310758Seric ** Parameters: 46410758Seric ** hostbuf -- a place to return the name of this host. 46512313Seric ** size -- the size of hostbuf. 46610758Seric ** 46710758Seric ** Returns: 46810758Seric ** A list of aliases for this host. 46910758Seric ** 47010758Seric ** Side Effects: 47110758Seric ** none. 47210758Seric */ 4736039Seric 47410758Seric char ** 47512313Seric myhostname(hostbuf, size) 47610758Seric char hostbuf[]; 47712313Seric int size; 47810758Seric { 47910758Seric extern struct hostent *gethostbyname(); 48011147Seric struct hostent *hp; 48110758Seric 48223120Seric if (gethostname(hostbuf, size) < 0) 48323120Seric { 48423120Seric (void) strcpy(hostbuf, "localhost"); 48523120Seric } 48611147Seric hp = gethostbyname(hostbuf); 48711147Seric if (hp != NULL) 48816877Seric { 48923104Seric (void) strcpy(hostbuf, hp->h_name); 49011147Seric return (hp->h_aliases); 49116877Seric } 49210758Seric else 49310758Seric return (NULL); 49410758Seric } 49510758Seric 49633932Sbostic /* 49733932Sbostic * MAPHOSTNAME -- turn a hostname into canonical form 49833932Sbostic * 49933932Sbostic * Parameters: 50033932Sbostic * hbuf -- a buffer containing a hostname. 50133932Sbostic * hbsize -- the size of hbuf. 50233932Sbostic * 50333932Sbostic * Returns: 50433932Sbostic * none. 50533932Sbostic * 50633932Sbostic * Side Effects: 50733932Sbostic * Looks up the host specified in hbuf. If it is not 50833932Sbostic * the canonical name for that host, replace it with 50933932Sbostic * the canonical name. If the name is unknown, or it 51033932Sbostic * is already the canonical name, leave it unchanged. 51133932Sbostic */ 51216911Seric maphostname(hbuf, hbsize) 51316911Seric char *hbuf; 51416911Seric int hbsize; 51516911Seric { 51616911Seric register struct hostent *hp; 51733932Sbostic u_long in_addr; 51833932Sbostic char ptr[256]; 51933932Sbostic struct hostent *gethostbyaddr(); 52016911Seric 52125574Smiriam /* 52233932Sbostic * If first character is a bracket, then it is an address 52333932Sbostic * lookup. Address is copied into a temporary buffer to 52433932Sbostic * strip the brackets and to preserve hbuf if address is 52533932Sbostic * unknown. 52633932Sbostic */ 52733932Sbostic if (*hbuf != '[') { 52829654Sbloom getcanonname(hbuf, hbsize); 52929654Sbloom return; 53025574Smiriam } 53133932Sbostic *index(strcpy(ptr, hbuf), ']') = '\0'; 53233932Sbostic in_addr = inet_addr(&ptr[1]); 53333932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 53433932Sbostic if (hp == NULL) 53533932Sbostic return; 53633932Sbostic if (strlen(hp->h_name) >= hbsize) 53733932Sbostic hp->h_name[hbsize - 1] = '\0'; 53833932Sbostic (void)strcpy(hbuf, hp->h_name); 53933932Sbostic } 54016911Seric 54110758Seric # else DAEMON 54216911Seric /* code for systems without sophisticated networking */ 54310758Seric 54410758Seric /* 54510758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 54611297Seric ** 54711297Seric ** Can't convert to upper case here because might be a UUCP name. 54812313Seric ** 54912313Seric ** Mark, you can change this to be anything you want...... 55010758Seric */ 55110758Seric 55210758Seric char ** 55312313Seric myhostname(hostbuf, size) 55410758Seric char hostbuf[]; 55512313Seric int size; 55610758Seric { 55710758Seric register FILE *f; 55810758Seric 55910758Seric hostbuf[0] = '\0'; 56010758Seric f = fopen("/usr/include/whoami", "r"); 56110758Seric if (f != NULL) 56210758Seric { 56312313Seric (void) fgets(hostbuf, size, f); 56410758Seric fixcrlf(hostbuf, TRUE); 56510758Seric (void) fclose(f); 56610758Seric } 56710758Seric return (NULL); 56810758Seric } 56916911Seric /* 57016911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 57116911Seric ** 57216911Seric ** Parameters: 57316911Seric ** hbuf -- a buffer containing a hostname. 57416911Seric ** hbsize -- the size of hbuf. 57516911Seric ** 57616911Seric ** Returns: 57716911Seric ** none. 57816911Seric ** 57916911Seric ** Side Effects: 58016911Seric ** Looks up the host specified in hbuf. If it is not 58116911Seric ** the canonical name for that host, replace it with 58216911Seric ** the canonical name. If the name is unknown, or it 58316911Seric ** is already the canonical name, leave it unchanged. 58416911Seric */ 58510758Seric 58616911Seric /*ARGSUSED*/ 58716911Seric maphostname(hbuf, hbsize) 58816911Seric char *hbuf; 58916911Seric int hbsize; 59016911Seric { 59116911Seric return; 59216911Seric } 59316911Seric 5945978Seric #endif DAEMON 595