122700Sdist /* 233780Sbostic * Copyright (c) 1988 Regents of the University of California. 333780Sbostic * All rights reserved. 433780Sbostic * 533780Sbostic * Redistribution and use in source and binary forms are permitted 633780Sbostic * provided that this notice is preserved and that due credit is given 733780Sbostic * to the University of California at Berkeley. The name of the University 833780Sbostic * may not be used to endorse or promote products derived from this 933780Sbostic * software without specific prior written permission. This software 1033780Sbostic * is provided ``as is'' without express or implied warranty. 1133780Sbostic * 1233780Sbostic * Sendmail 1333780Sbostic * Copyright (c) 1983 Eric P. Allman 1433780Sbostic * Berkeley, California 1533780Sbostic */ 1622700Sdist 17*33932Sbostic #include <errno.h> 18*33932Sbostic #include <sendmail.h> 194535Seric 2033780Sbostic #ifndef lint 2133780Sbostic #ifdef DAEMON 22*33932Sbostic static char sccsid[] = "@(#)daemon.c 5.25 (Berkeley) 04/01/88 (with daemon mode)"; 2333780Sbostic #else 24*33932Sbostic static char sccsid[] = "@(#)daemon.c 5.25 (Berkeley) 04/01/88 (without daemon mode)"; 2533780Sbostic #endif 2633780Sbostic #endif /* not lint */ 274535Seric 2833780Sbostic #ifdef DAEMON 2933780Sbostic 3023120Seric # include <netdb.h> 3124945Seric # include <sys/signal.h> 3223120Seric # include <sys/wait.h> 3323120Seric # include <sys/time.h> 3423120Seric # include <sys/resource.h> 355978Seric 364535Seric /* 374535Seric ** DAEMON.C -- routines to use when running as a daemon. 387556Seric ** 397556Seric ** This entire file is highly dependent on the 4.2 BSD 407556Seric ** interprocess communication primitives. No attempt has 417556Seric ** been made to make this file portable to Version 7, 427556Seric ** Version 6, MPX files, etc. If you should try such a 437556Seric ** thing yourself, I recommend chucking the entire file 447556Seric ** and starting from scratch. Basic semantics are: 457556Seric ** 467556Seric ** getrequests() 477556Seric ** Opens a port and initiates a connection. 487556Seric ** Returns in a child. Must set InChannel and 497556Seric ** OutChannel appropriately. 5010206Seric ** clrdaemon() 5110206Seric ** Close any open files associated with getting 5210206Seric ** the connection; this is used when running the queue, 5310206Seric ** etc., to avoid having extra file descriptors during 5410206Seric ** the queue run and to avoid confusing the network 5510206Seric ** code (if it cares). 567556Seric ** makeconnection(host, port, outfile, infile) 577556Seric ** Make a connection to the named host on the given 587556Seric ** port. Set *outfile and *infile to the files 597556Seric ** appropriate for communication. Returns zero on 607556Seric ** success, else an exit status describing the 617556Seric ** error. 6225699Seric ** maphostname(hbuf, hbufsize) 6325699Seric ** Convert the entry in hbuf into a canonical form. It 6425699Seric ** may not be larger than hbufsize. 654535Seric */ 664535Seric /* 674535Seric ** GETREQUESTS -- open mail IPC port and get requests. 684535Seric ** 694535Seric ** Parameters: 704535Seric ** none. 714535Seric ** 724535Seric ** Returns: 734535Seric ** none. 744535Seric ** 754535Seric ** Side Effects: 764535Seric ** Waits until some interesting activity occurs. When 774535Seric ** it does, a child is created to process it, and the 784535Seric ** parent waits for completion. Return from this 799886Seric ** routine is always in the child. The file pointers 809886Seric ** "InChannel" and "OutChannel" should be set to point 819886Seric ** to the communication channel. 824535Seric */ 834535Seric 8410206Seric struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 859610Seric 8616144Seric int DaemonSocket = -1; /* fd describing socket */ 8716890Seric char *NetName; /* name of home (local?) network */ 8816144Seric 894535Seric getrequests() 904535Seric { 919610Seric int t; 929610Seric register struct servent *sp; 9325027Seric int on = 1; 9424945Seric extern reapchild(); 957117Seric 969610Seric /* 979610Seric ** Set up the address for the mailer. 989610Seric */ 999610Seric 1009610Seric sp = getservbyname("smtp", "tcp"); 1019610Seric if (sp == NULL) 1029610Seric { 1039610Seric syserr("server \"smtp\" unknown"); 10410167Seric goto severe; 1059610Seric } 1069610Seric SendmailAddress.sin_family = AF_INET; 1079610Seric SendmailAddress.sin_addr.s_addr = INADDR_ANY; 1089740Ssam SendmailAddress.sin_port = sp->s_port; 1099610Seric 1109610Seric /* 1119610Seric ** Try to actually open the connection. 1129610Seric */ 1139610Seric 1149610Seric # ifdef DEBUG 1159610Seric if (tTd(15, 1)) 1169610Seric printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 1179610Seric # endif DEBUG 1189610Seric 1199610Seric /* get a socket for the SMTP connection */ 12023120Seric DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 12110206Seric if (DaemonSocket < 0) 1229610Seric { 1239610Seric /* probably another daemon already */ 1249610Seric syserr("getrequests: can't create socket"); 1259610Seric severe: 1269610Seric # ifdef LOG 1279610Seric if (LogLevel > 0) 12824858Seric syslog(LOG_ALERT, "cannot get connection"); 1299610Seric # endif LOG 1309610Seric finis(); 1319610Seric } 13210347Seric 13310347Seric #ifdef DEBUG 13410347Seric /* turn on network debugging? */ 13510347Seric if (tTd(15, 15)) 13624945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 13710347Seric #endif DEBUG 13810347Seric 13925027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 14025027Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 14125027Seric 14223120Seric if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 1439610Seric { 1449610Seric syserr("getrequests: cannot bind"); 14510206Seric (void) close(DaemonSocket); 1469610Seric goto severe; 1479610Seric } 14823120Seric if (listen(DaemonSocket, 10) < 0) 14923120Seric { 15023120Seric syserr("getrequests: cannot listen"); 15123120Seric (void) close(DaemonSocket); 15223120Seric goto severe; 15323120Seric } 1549610Seric 15524955Seric (void) signal(SIGCHLD, reapchild); 15624945Seric 1579610Seric # ifdef DEBUG 1589610Seric if (tTd(15, 1)) 15910206Seric printf("getrequests: %d\n", DaemonSocket); 1609610Seric # endif DEBUG 1619610Seric 1624631Seric for (;;) 1634631Seric { 16414875Seric register int pid; 16511147Seric auto int lotherend; 16611147Seric struct sockaddr_in otherend; 16714875Seric extern int RefuseLA; 16811147Seric 16914875Seric /* see if we are rejecting connections */ 17014875Seric while (getla() > RefuseLA) 17114875Seric sleep(5); 17214875Seric 1739610Seric /* wait for a connection */ 1749610Seric do 1759610Seric { 1769610Seric errno = 0; 1779610Seric lotherend = sizeof otherend; 17823120Seric t = accept(DaemonSocket, &otherend, &lotherend); 1799610Seric } while (t < 0 && errno == EINTR); 1809610Seric if (t < 0) 1815978Seric { 1829610Seric syserr("getrequests: accept"); 1839610Seric sleep(5); 1849610Seric continue; 1855978Seric } 1864631Seric 1875978Seric /* 1885978Seric ** Create a subprocess to process the mail. 1895978Seric */ 1905978Seric 1915978Seric # ifdef DEBUG 1927677Seric if (tTd(15, 2)) 1939610Seric printf("getrequests: forking (fd = %d)\n", t); 1945978Seric # endif DEBUG 1955978Seric 1964636Seric pid = fork(); 1974636Seric if (pid < 0) 1984631Seric { 1994636Seric syserr("daemon: cannot fork"); 2004636Seric sleep(10); 2019610Seric (void) close(t); 2024636Seric continue; 2034631Seric } 2044631Seric 2054636Seric if (pid == 0) 2064631Seric { 20711147Seric extern struct hostent *gethostbyaddr(); 20811147Seric register struct hostent *hp; 20911147Seric char buf[MAXNAME]; 21011147Seric 2114636Seric /* 2124636Seric ** CHILD -- return to caller. 21311147Seric ** Collect verified idea of sending host. 2144636Seric ** Verify calling user id if possible here. 2154636Seric */ 2164631Seric 21724955Seric (void) signal(SIGCHLD, SIG_DFL); 21824950Seric 21911147Seric /* determine host name */ 22025616Seric hp = gethostbyaddr((char *) &otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 22111147Seric if (hp != NULL) 22216890Seric { 22323104Seric (void) strcpy(buf, hp->h_name); 22416890Seric if (NetName != NULL && NetName[0] != '\0' && 22516894Seric index(hp->h_name, '.') == NULL) 22616890Seric { 22723104Seric (void) strcat(buf, "."); 22823104Seric (void) strcat(buf, NetName); 22916890Seric } 23016890Seric } 23111147Seric else 23216884Seric { 23316884Seric extern char *inet_ntoa(); 23416884Seric 23516884Seric /* produce a dotted quad */ 23616884Seric (void) sprintf(buf, "[%s]", 23716884Seric inet_ntoa(otherend.sin_addr)); 23816884Seric } 23916884Seric 24016884Seric /* should we check for illegal connection here? XXX */ 24116884Seric 24211147Seric RealHostName = newstr(buf); 24311147Seric 24410206Seric (void) close(DaemonSocket); 2459610Seric InChannel = fdopen(t, "r"); 24621062Seric OutChannel = fdopen(dup(t), "w"); 2475978Seric # ifdef DEBUG 2487677Seric if (tTd(15, 2)) 2495978Seric printf("getreq: returning\n"); 2505978Seric # endif DEBUG 2517876Seric # ifdef LOG 2527876Seric if (LogLevel > 11) 2537876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 2547876Seric # endif LOG 2554636Seric return; 2564631Seric } 2574631Seric 2587117Seric /* close the port so that others will hang (for a while) */ 2599610Seric (void) close(t); 2604631Seric } 2619886Seric /*NOTREACHED*/ 2624631Seric } 2635978Seric /* 26410206Seric ** CLRDAEMON -- reset the daemon connection 26510206Seric ** 26610206Seric ** Parameters: 26710206Seric ** none. 26810206Seric ** 26910206Seric ** Returns: 27010206Seric ** none. 27110206Seric ** 27210206Seric ** Side Effects: 27310206Seric ** releases any resources used by the passive daemon. 27410206Seric */ 27510206Seric 27610206Seric clrdaemon() 27710206Seric { 27810206Seric if (DaemonSocket >= 0) 27910206Seric (void) close(DaemonSocket); 28010206Seric DaemonSocket = -1; 28110206Seric } 28210206Seric /* 2836039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2846039Seric ** 2856039Seric ** Parameters: 2866039Seric ** host -- the name of the host. 2876633Seric ** port -- the port number to connect to. 2886039Seric ** outfile -- a pointer to a place to put the outfile 2896039Seric ** descriptor. 2906039Seric ** infile -- ditto for infile. 2916039Seric ** 2926039Seric ** Returns: 2936039Seric ** An exit code telling whether the connection could be 2946039Seric ** made and if not why not. 2956039Seric ** 2966039Seric ** Side Effects: 2976039Seric ** none. 2986039Seric */ 2995978Seric 30025475Smiriam int h_errno; /*this will go away when code implemented*/ 30125475Smiriam 3026633Seric makeconnection(host, port, outfile, infile) 3036039Seric char *host; 3047286Seric u_short port; 3056039Seric FILE **outfile; 3066039Seric FILE **infile; 3076039Seric { 30829430Sbloom register int i, s; 30929430Sbloom register struct hostent *hp = (struct hostent *)NULL; 31029430Sbloom extern char *inet_ntoa(); 31127744Sbloom int sav_errno; 3126039Seric 3136039Seric /* 3146039Seric ** Set up the address for the mailer. 3159308Seric ** Accept "[a.b.c.d]" syntax for host name. 3166039Seric */ 3176039Seric 31825475Smiriam h_errno = 0; 31925475Smiriam errno = 0; 32025475Smiriam 3219308Seric if (host[0] == '[') 3229308Seric { 32311147Seric long hid; 32411147Seric register char *p = index(host, ']'); 3259308Seric 32611147Seric if (p != NULL) 3279308Seric { 32811147Seric *p = '\0'; 32911147Seric hid = inet_addr(&host[1]); 33011147Seric *p = ']'; 3319308Seric } 33211147Seric if (p == NULL || hid == -1) 3339308Seric { 3349308Seric usrerr("Invalid numeric domain spec \"%s\"", host); 3359308Seric return (EX_NOHOST); 3369308Seric } 3379308Seric SendmailAddress.sin_addr.s_addr = hid; 3389308Seric } 3399610Seric else 3409610Seric { 34129430Sbloom hp = gethostbyname(host); 34225475Smiriam if (hp == NULL) 34324945Seric { 34425475Smiriam if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 34525475Smiriam return (EX_TEMPFAIL); 34625657Seric 34725657Seric /* 34825657Seric ** XXX Should look for mail forwarder record here 34925657Seric ** XXX if (h_errno == NO_ADDRESS). 35025657Seric */ 35125657Seric 35225475Smiriam return (EX_NOHOST); 35324945Seric } 35416884Seric bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 35529430Sbloom i = 1; 3569610Seric } 3579610Seric 3589610Seric /* 3599610Seric ** Determine the port number. 3609610Seric */ 3619610Seric 36210011Seric if (port != 0) 36310011Seric SendmailAddress.sin_port = htons(port); 36410011Seric else 3659610Seric { 3669610Seric register struct servent *sp = getservbyname("smtp", "tcp"); 3679610Seric 3689610Seric if (sp == NULL) 3699610Seric { 3709610Seric syserr("makeconnection: server \"smtp\" unknown"); 3719610Seric return (EX_OSFILE); 3729610Seric } 37310011Seric SendmailAddress.sin_port = sp->s_port; 3749610Seric } 3756039Seric 3766039Seric /* 3776039Seric ** Try to actually open the connection. 3786039Seric */ 3796039Seric 38029430Sbloom again: 3816039Seric # ifdef DEBUG 3827677Seric if (tTd(16, 1)) 38329430Sbloom printf("makeconnection (%s [%s])\n", host, 38429430Sbloom inet_ntoa(SendmailAddress.sin_addr.s_addr)); 3856039Seric # endif DEBUG 3866039Seric 38723120Seric s = socket(AF_INET, SOCK_STREAM, 0); 3886039Seric if (s < 0) 3896039Seric { 3906039Seric syserr("makeconnection: no socket"); 39127744Sbloom sav_errno = errno; 3926039Seric goto failure; 3936039Seric } 3946039Seric 3956039Seric # ifdef DEBUG 3967677Seric if (tTd(16, 1)) 3976039Seric printf("makeconnection: %d\n", s); 39810347Seric 39910347Seric /* turn on network debugging? */ 40010347Seric if (tTd(16, 14)) 40124945Seric { 40224945Seric int on = 1; 40324945Seric (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 40424945Seric } 4056039Seric # endif DEBUG 4069546Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 40714383Seric errno = 0; /* for debugging */ 4089610Seric SendmailAddress.sin_family = AF_INET; 40923120Seric if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0) 4106039Seric { 41127744Sbloom sav_errno = errno; 41227744Sbloom (void) close(s); 41329430Sbloom if (hp && hp->h_addr_list[i]) 41429430Sbloom { 41529430Sbloom bcopy(hp->h_addr_list[i++], 41629430Sbloom (char *)&SendmailAddress.sin_addr, hp->h_length); 41729430Sbloom goto again; 41829430Sbloom } 41929430Sbloom 4206039Seric /* failure, decide if temporary or not */ 4216039Seric failure: 42227744Sbloom switch (sav_errno) 4236039Seric { 4246039Seric case EISCONN: 4256039Seric case ETIMEDOUT: 4266897Seric case EINPROGRESS: 4276897Seric case EALREADY: 4286897Seric case EADDRINUSE: 42910123Seric case EHOSTDOWN: 4306897Seric case ENETDOWN: 4316897Seric case ENETRESET: 4326897Seric case ENOBUFS: 4337204Seric case ECONNREFUSED: 43411546Seric case ECONNRESET: 43510081Seric case EHOSTUNREACH: 43610098Seric case ENETUNREACH: 4376039Seric /* there are others, I'm sure..... */ 4386039Seric return (EX_TEMPFAIL); 4396039Seric 44011147Seric case EPERM: 44111147Seric /* why is this happening? */ 44211147Seric syserr("makeconnection: funny failure, addr=%lx, port=%x", 44311147Seric SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 44414383Seric return (EX_TEMPFAIL); 44511147Seric 4466039Seric default: 44731953Sbostic message(Arpa_Info, "%s", errstring(sav_errno)); 4486039Seric return (EX_UNAVAILABLE); 4496039Seric } 4506039Seric } 4516039Seric 4526039Seric /* connection ok, put it into canonical form */ 4536039Seric *outfile = fdopen(s, "w"); 4546039Seric *infile = fdopen(s, "r"); 4556039Seric 45610098Seric return (EX_OK); 4576039Seric } 45810758Seric /* 45910758Seric ** MYHOSTNAME -- return the name of this host. 46010758Seric ** 46110758Seric ** Parameters: 46210758Seric ** hostbuf -- a place to return the name of this host. 46312313Seric ** size -- the size of hostbuf. 46410758Seric ** 46510758Seric ** Returns: 46610758Seric ** A list of aliases for this host. 46710758Seric ** 46810758Seric ** Side Effects: 46910758Seric ** none. 47010758Seric */ 4716039Seric 47210758Seric char ** 47312313Seric myhostname(hostbuf, size) 47410758Seric char hostbuf[]; 47512313Seric int size; 47610758Seric { 47710758Seric extern struct hostent *gethostbyname(); 47811147Seric struct hostent *hp; 47910758Seric 48023120Seric if (gethostname(hostbuf, size) < 0) 48123120Seric { 48223120Seric (void) strcpy(hostbuf, "localhost"); 48323120Seric } 48411147Seric hp = gethostbyname(hostbuf); 48511147Seric if (hp != NULL) 48616877Seric { 48723104Seric (void) strcpy(hostbuf, hp->h_name); 48811147Seric return (hp->h_aliases); 48916877Seric } 49010758Seric else 49110758Seric return (NULL); 49210758Seric } 49310758Seric 494*33932Sbostic /* 495*33932Sbostic * MAPHOSTNAME -- turn a hostname into canonical form 496*33932Sbostic * 497*33932Sbostic * Parameters: 498*33932Sbostic * hbuf -- a buffer containing a hostname. 499*33932Sbostic * hbsize -- the size of hbuf. 500*33932Sbostic * 501*33932Sbostic * Returns: 502*33932Sbostic * none. 503*33932Sbostic * 504*33932Sbostic * Side Effects: 505*33932Sbostic * Looks up the host specified in hbuf. If it is not 506*33932Sbostic * the canonical name for that host, replace it with 507*33932Sbostic * the canonical name. If the name is unknown, or it 508*33932Sbostic * is already the canonical name, leave it unchanged. 509*33932Sbostic */ 51016911Seric maphostname(hbuf, hbsize) 51116911Seric char *hbuf; 51216911Seric int hbsize; 51316911Seric { 51416911Seric register struct hostent *hp; 515*33932Sbostic u_long in_addr; 516*33932Sbostic char ptr[256]; 517*33932Sbostic struct hostent *gethostbyaddr(); 51816911Seric 51925574Smiriam /* 520*33932Sbostic * If first character is a bracket, then it is an address 521*33932Sbostic * lookup. Address is copied into a temporary buffer to 522*33932Sbostic * strip the brackets and to preserve hbuf if address is 523*33932Sbostic * unknown. 524*33932Sbostic */ 525*33932Sbostic if (*hbuf != '[') { 52629654Sbloom getcanonname(hbuf, hbsize); 52729654Sbloom return; 52825574Smiriam } 529*33932Sbostic *index(strcpy(ptr, hbuf), ']') = '\0'; 530*33932Sbostic in_addr = inet_addr(&ptr[1]); 531*33932Sbostic hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 532*33932Sbostic if (hp == NULL) 533*33932Sbostic return; 534*33932Sbostic if (strlen(hp->h_name) >= hbsize) 535*33932Sbostic hp->h_name[hbsize - 1] = '\0'; 536*33932Sbostic (void)strcpy(hbuf, hp->h_name); 537*33932Sbostic } 53816911Seric 53910758Seric # else DAEMON 54016911Seric /* code for systems without sophisticated networking */ 54110758Seric 54210758Seric /* 54310758Seric ** MYHOSTNAME -- stub version for case of no daemon code. 54411297Seric ** 54511297Seric ** Can't convert to upper case here because might be a UUCP name. 54612313Seric ** 54712313Seric ** Mark, you can change this to be anything you want...... 54810758Seric */ 54910758Seric 55010758Seric char ** 55112313Seric myhostname(hostbuf, size) 55210758Seric char hostbuf[]; 55312313Seric int size; 55410758Seric { 55510758Seric register FILE *f; 55610758Seric 55710758Seric hostbuf[0] = '\0'; 55810758Seric f = fopen("/usr/include/whoami", "r"); 55910758Seric if (f != NULL) 56010758Seric { 56112313Seric (void) fgets(hostbuf, size, f); 56210758Seric fixcrlf(hostbuf, TRUE); 56310758Seric (void) fclose(f); 56410758Seric } 56510758Seric return (NULL); 56610758Seric } 56716911Seric /* 56816911Seric ** MAPHOSTNAME -- turn a hostname into canonical form 56916911Seric ** 57016911Seric ** Parameters: 57116911Seric ** hbuf -- a buffer containing a hostname. 57216911Seric ** hbsize -- the size of hbuf. 57316911Seric ** 57416911Seric ** Returns: 57516911Seric ** none. 57616911Seric ** 57716911Seric ** Side Effects: 57816911Seric ** Looks up the host specified in hbuf. If it is not 57916911Seric ** the canonical name for that host, replace it with 58016911Seric ** the canonical name. If the name is unknown, or it 58116911Seric ** is already the canonical name, leave it unchanged. 58216911Seric */ 58310758Seric 58416911Seric /*ARGSUSED*/ 58516911Seric maphostname(hbuf, hbsize) 58616911Seric char *hbuf; 58716911Seric int hbsize; 58816911Seric { 58916911Seric return; 59016911Seric } 59116911Seric 5925978Seric #endif DAEMON 593