1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #include <errno.h> 10 #include "sendmail.h" 11 12 #ifndef lint 13 #ifdef DAEMON 14 static char sccsid[] = "@(#)daemon.c 6.8 (Berkeley) 02/23/93 (with daemon mode)"; 15 #else 16 static char sccsid[] = "@(#)daemon.c 6.8 (Berkeley) 02/23/93 (without daemon mode)"; 17 #endif 18 #endif /* not lint */ 19 20 #ifdef DAEMON 21 22 # include <netdb.h> 23 # include <sys/signal.h> 24 # include <sys/wait.h> 25 # include <sys/time.h> 26 # include <sys/resource.h> 27 28 /* 29 ** DAEMON.C -- routines to use when running as a daemon. 30 ** 31 ** This entire file is highly dependent on the 4.2 BSD 32 ** interprocess communication primitives. No attempt has 33 ** been made to make this file portable to Version 7, 34 ** Version 6, MPX files, etc. If you should try such a 35 ** thing yourself, I recommend chucking the entire file 36 ** and starting from scratch. Basic semantics are: 37 ** 38 ** getrequests() 39 ** Opens a port and initiates a connection. 40 ** Returns in a child. Must set InChannel and 41 ** OutChannel appropriately. 42 ** clrdaemon() 43 ** Close any open files associated with getting 44 ** the connection; this is used when running the queue, 45 ** etc., to avoid having extra file descriptors during 46 ** the queue run and to avoid confusing the network 47 ** code (if it cares). 48 ** makeconnection(host, port, outfile, infile, usesecureport) 49 ** Make a connection to the named host on the given 50 ** port. Set *outfile and *infile to the files 51 ** appropriate for communication. Returns zero on 52 ** success, else an exit status describing the 53 ** error. 54 ** maphostname(map, hbuf, hbufsiz, avp) 55 ** Convert the entry in hbuf into a canonical form. 56 */ 57 /* 58 ** GETREQUESTS -- open mail IPC port and get requests. 59 ** 60 ** Parameters: 61 ** none. 62 ** 63 ** Returns: 64 ** none. 65 ** 66 ** Side Effects: 67 ** Waits until some interesting activity occurs. When 68 ** it does, a child is created to process it, and the 69 ** parent waits for completion. Return from this 70 ** routine is always in the child. The file pointers 71 ** "InChannel" and "OutChannel" should be set to point 72 ** to the communication channel. 73 */ 74 75 int DaemonSocket = -1; /* fd describing socket */ 76 77 getrequests() 78 { 79 int t; 80 register struct servent *sp; 81 int on = 1; 82 bool refusingconnections = TRUE; 83 struct sockaddr_in srvraddr; 84 extern void reapchild(); 85 86 /* 87 ** Set up the address for the mailer. 88 */ 89 90 sp = getservbyname("smtp", "tcp"); 91 if (sp == NULL) 92 { 93 syserr("554 server \"smtp\" unknown"); 94 goto severe; 95 } 96 srvraddr.sin_family = AF_INET; 97 srvraddr.sin_addr.s_addr = INADDR_ANY; 98 srvraddr.sin_port = sp->s_port; 99 100 /* 101 ** Try to actually open the connection. 102 */ 103 104 if (tTd(15, 1)) 105 printf("getrequests: port 0x%x\n", srvraddr.sin_port); 106 107 /* get a socket for the SMTP connection */ 108 DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 109 if (DaemonSocket < 0) 110 { 111 /* probably another daemon already */ 112 syserr("getrequests: can't create socket"); 113 severe: 114 # ifdef LOG 115 if (LogLevel > 0) 116 syslog(LOG_ALERT, "problem creating SMTP socket"); 117 # endif /* LOG */ 118 finis(); 119 } 120 121 /* turn on network debugging? */ 122 if (tTd(15, 101)) 123 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 124 125 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 126 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 127 128 if (bind(DaemonSocket, (struct sockaddr *)&srvraddr, sizeof srvraddr) < 0) 129 { 130 syserr("getrequests: cannot bind"); 131 (void) close(DaemonSocket); 132 goto severe; 133 } 134 135 (void) signal(SIGCHLD, reapchild); 136 137 if (tTd(15, 1)) 138 printf("getrequests: %d\n", DaemonSocket); 139 140 for (;;) 141 { 142 register int pid; 143 auto int lotherend; 144 extern bool refuseconnections(); 145 146 /* see if we are rejecting connections */ 147 CurrentLA = getla(); 148 if (refuseconnections()) 149 { 150 if (!refusingconnections) 151 { 152 /* don't queue so peer will fail quickly */ 153 (void) listen(DaemonSocket, 0); 154 refusingconnections = TRUE; 155 } 156 setproctitle("rejecting connections: load average: %d", 157 CurrentLA); 158 sleep(5); 159 continue; 160 } 161 162 if (refusingconnections) 163 { 164 /* start listening again */ 165 if (listen(DaemonSocket, 10) < 0) 166 { 167 syserr("getrequests: cannot listen"); 168 (void) close(DaemonSocket); 169 goto severe; 170 } 171 setproctitle("accepting connections"); 172 refusingconnections = FALSE; 173 } 174 175 /* wait for a connection */ 176 do 177 { 178 errno = 0; 179 lotherend = sizeof RealHostAddr; 180 t = accept(DaemonSocket, 181 (struct sockaddr *)&RealHostAddr, &lotherend); 182 } while (t < 0 && errno == EINTR); 183 if (t < 0) 184 { 185 syserr("getrequests: accept"); 186 sleep(5); 187 continue; 188 } 189 190 /* 191 ** Create a subprocess to process the mail. 192 */ 193 194 if (tTd(15, 2)) 195 printf("getrequests: forking (fd = %d)\n", t); 196 197 pid = fork(); 198 if (pid < 0) 199 { 200 syserr("daemon: cannot fork"); 201 sleep(10); 202 (void) close(t); 203 continue; 204 } 205 206 if (pid == 0) 207 { 208 extern struct hostent *gethostbyaddr(); 209 register struct hostent *hp; 210 char buf[MAXNAME]; 211 extern char *inet_ntoa(); 212 213 /* 214 ** CHILD -- return to caller. 215 ** Collect verified idea of sending host. 216 ** Verify calling user id if possible here. 217 */ 218 219 (void) signal(SIGCHLD, SIG_DFL); 220 221 /* determine host name */ 222 hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 223 if (hp != NULL) 224 (void) strcpy(buf, hp->h_name); 225 else 226 { 227 /* produce a dotted quad */ 228 (void) sprintf(buf, "[%s]", 229 inet_ntoa(RealHostAddr.sin_addr)); 230 } 231 232 #ifdef LOG 233 if (LogLevel > 10) 234 { 235 /* log connection information */ 236 syslog(LOG_INFO, "connect from %s (%s)", 237 buf, inet_ntoa(RealHostAddr.sin_addr)); 238 } 239 #endif 240 241 /* should we check for illegal connection here? XXX */ 242 243 RealHostName = newstr(buf); 244 245 (void) close(DaemonSocket); 246 InChannel = fdopen(t, "r"); 247 OutChannel = fdopen(dup(t), "w"); 248 if (tTd(15, 2)) 249 printf("getreq: returning\n"); 250 # ifdef LOG 251 if (LogLevel > 11) 252 syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 253 # endif /* LOG */ 254 return; 255 } 256 257 /* close the port so that others will hang (for a while) */ 258 (void) close(t); 259 } 260 /*NOTREACHED*/ 261 } 262 /* 263 ** CLRDAEMON -- reset the daemon connection 264 ** 265 ** Parameters: 266 ** none. 267 ** 268 ** Returns: 269 ** none. 270 ** 271 ** Side Effects: 272 ** releases any resources used by the passive daemon. 273 */ 274 275 clrdaemon() 276 { 277 if (DaemonSocket >= 0) 278 (void) close(DaemonSocket); 279 DaemonSocket = -1; 280 } 281 /* 282 ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 283 ** 284 ** Parameters: 285 ** host -- the name of the host. 286 ** port -- the port number to connect to. 287 ** mci -- a pointer to the mail connection information 288 ** structure to be filled in. 289 ** usesecureport -- if set, use a low numbered (reserved) 290 ** port to provide some rudimentary authentication. 291 ** 292 ** Returns: 293 ** An exit code telling whether the connection could be 294 ** made and if not why not. 295 ** 296 ** Side Effects: 297 ** none. 298 */ 299 300 int 301 makeconnection(host, port, mci, usesecureport) 302 char *host; 303 u_short port; 304 register MCI *mci; 305 bool usesecureport; 306 { 307 register int i, s; 308 register struct hostent *hp = (struct hostent *)NULL; 309 struct sockaddr_in addr; 310 int sav_errno; 311 extern char *inet_ntoa(); 312 #ifdef NAMED_BIND 313 extern int h_errno; 314 #endif 315 316 /* 317 ** Set up the address for the mailer. 318 ** Accept "[a.b.c.d]" syntax for host name. 319 */ 320 321 #ifdef NAMED_BIND 322 h_errno = 0; 323 #endif 324 errno = 0; 325 326 if (host[0] == '[') 327 { 328 long hid; 329 register char *p = strchr(host, ']'); 330 331 if (p != NULL) 332 { 333 *p = '\0'; 334 hid = inet_addr(&host[1]); 335 *p = ']'; 336 } 337 if (p == NULL || hid == -1) 338 { 339 usrerr("553 Invalid numeric domain spec \"%s\"", host); 340 return (EX_NOHOST); 341 } 342 addr.sin_addr.s_addr = hid; 343 } 344 else 345 { 346 hp = gethostbyname(host); 347 if (hp == NULL) 348 { 349 #ifdef NAMED_BIND 350 if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 351 return (EX_TEMPFAIL); 352 353 /* if name server is specified, assume temp fail */ 354 if (errno == ECONNREFUSED && UseNameServer) 355 return (EX_TEMPFAIL); 356 #endif 357 return (EX_NOHOST); 358 } 359 bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length); 360 i = 1; 361 } 362 363 /* 364 ** Determine the port number. 365 */ 366 367 if (port != 0) 368 addr.sin_port = htons(port); 369 else 370 { 371 register struct servent *sp = getservbyname("smtp", "tcp"); 372 373 if (sp == NULL) 374 { 375 syserr("554 makeconnection: server \"smtp\" unknown"); 376 return (EX_OSERR); 377 } 378 addr.sin_port = sp->s_port; 379 } 380 381 /* 382 ** Try to actually open the connection. 383 */ 384 385 for (;;) 386 { 387 if (tTd(16, 1)) 388 printf("makeconnection (%s [%s])\n", host, 389 inet_ntoa(addr.sin_addr)); 390 391 if (usesecureport) 392 { 393 int rport = IPPORT_RESERVED - 1; 394 395 s = rresvport(&rport); 396 } 397 else 398 { 399 s = socket(AF_INET, SOCK_STREAM, 0); 400 } 401 if (s < 0) 402 { 403 sav_errno = errno; 404 syserr("makeconnection: no socket"); 405 goto failure; 406 } 407 408 if (tTd(16, 1)) 409 printf("makeconnection: fd=%d\n", s); 410 411 /* turn on network debugging? */ 412 if (tTd(16, 101)) 413 { 414 int on = 1; 415 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 416 (char *)&on, sizeof on); 417 } 418 if (CurEnv->e_xfp != NULL) 419 (void) fflush(CurEnv->e_xfp); /* for debugging */ 420 errno = 0; /* for debugging */ 421 addr.sin_family = AF_INET; 422 if (connect(s, (struct sockaddr *) &addr, sizeof addr) >= 0) 423 break; 424 425 /* couldn't connect.... figure out why */ 426 sav_errno = errno; 427 (void) close(s); 428 if (hp && hp->h_addr_list[i]) 429 { 430 if (tTd(16, 1)) 431 printf("Connect failed; trying new address....\n"); 432 bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr, 433 hp->h_length); 434 continue; 435 } 436 437 /* failure, decide if temporary or not */ 438 failure: 439 switch (sav_errno) 440 { 441 case EISCONN: 442 case ETIMEDOUT: 443 case EINPROGRESS: 444 case EALREADY: 445 case EADDRINUSE: 446 case EHOSTDOWN: 447 case ENETDOWN: 448 case ENETRESET: 449 case ENOBUFS: 450 case ECONNREFUSED: 451 case ECONNRESET: 452 case EHOSTUNREACH: 453 case ENETUNREACH: 454 #ifdef ENOSR 455 case ENOSR: 456 #endif 457 /* there are others, I'm sure..... */ 458 return (EX_TEMPFAIL); 459 460 case EPERM: 461 /* why is this happening? */ 462 syserr("makeconnection: funny failure, addr=%lx, port=%x", 463 addr.sin_addr.s_addr, addr.sin_port); 464 return (EX_TEMPFAIL); 465 466 default: 467 { 468 extern char *errstring(); 469 470 message("%s", errstring(sav_errno)); 471 return (EX_UNAVAILABLE); 472 } 473 } 474 } 475 476 /* connection ok, put it into canonical form */ 477 mci->mci_out = fdopen(s, "w"); 478 mci->mci_in = fdopen(dup(s), "r"); 479 480 return (EX_OK); 481 } 482 /* 483 ** MYHOSTNAME -- return the name of this host. 484 ** 485 ** Parameters: 486 ** hostbuf -- a place to return the name of this host. 487 ** size -- the size of hostbuf. 488 ** 489 ** Returns: 490 ** A list of aliases for this host. 491 ** 492 ** Side Effects: 493 ** Sets the MyIpAddrs buffer to a list of my IP addresses. 494 */ 495 496 struct in_addr MyIpAddrs[MAXIPADDR + 1]; 497 498 char ** 499 myhostname(hostbuf, size) 500 char hostbuf[]; 501 int size; 502 { 503 register struct hostent *hp; 504 extern struct hostent *gethostbyname(); 505 506 if (gethostname(hostbuf, size) < 0) 507 { 508 (void) strcpy(hostbuf, "localhost"); 509 } 510 hp = gethostbyname(hostbuf); 511 if (hp != NULL) 512 { 513 (void) strncpy(hostbuf, hp->h_name, size - 1); 514 hostbuf[size - 1] = '\0'; 515 516 if (hp->h_addrtype == AF_INET && hp->h_length == 4) 517 { 518 register int i; 519 520 for (i = 0; i < MAXIPADDR; i++) 521 { 522 if (hp->h_addr_list[i] == NULL) 523 break; 524 MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i]; 525 } 526 MyIpAddrs[i].s_addr = 0; 527 } 528 529 return (hp->h_aliases); 530 } 531 else 532 return (NULL); 533 } 534 /* 535 ** MAPHOSTNAME -- turn a hostname into canonical form 536 ** 537 ** Parameters: 538 ** map -- a pointer to this map (unused). 539 ** hbuf -- a buffer containing a hostname. 540 ** hbsize -- the size of hbuf. 541 ** avp -- unused -- for compatibility with other mapping 542 ** functions. 543 ** 544 ** Returns: 545 ** The mapping, if found. 546 ** NULL if no mapping found. 547 ** 548 ** Side Effects: 549 ** Looks up the host specified in hbuf. If it is not 550 ** the canonical name for that host, return the canonical 551 ** name. 552 */ 553 554 char * 555 maphostname(map, hbuf, hbsize, avp) 556 MAP *map; 557 char *hbuf; 558 int hbsize; 559 char **avp; 560 { 561 register struct hostent *hp; 562 u_long in_addr; 563 char *cp; 564 int i; 565 struct hostent *gethostbyaddr(); 566 567 /* allow room for null */ 568 hbsize--; 569 570 /* 571 * If first character is a bracket, then it is an address 572 * lookup. Address is copied into a temporary buffer to 573 * strip the brackets and to preserve hbuf if address is 574 * unknown. 575 */ 576 577 if (*hbuf != '[') 578 { 579 extern bool getcanonname(); 580 581 if (getcanonname(hbuf, hbsize)) 582 return hbuf; 583 else 584 return NULL; 585 } 586 if ((cp = strchr(hbuf, ']')) == NULL) 587 return (NULL); 588 *cp = '\0'; 589 in_addr = inet_addr(&hbuf[1]); 590 591 /* check to see if this is one of our addresses */ 592 for (i = 0; MyIpAddrs[i].s_addr != 0; i++) 593 { 594 if (MyIpAddrs[i].s_addr == in_addr) 595 { 596 strncpy(hbuf, MyHostName, hbsize); 597 hbuf[hbsize] = '\0'; 598 return hbuf; 599 } 600 } 601 602 /* nope -- ask the name server */ 603 hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 604 if (hp == NULL) 605 return (NULL); 606 607 /* found a match -- copy out */ 608 if (strlen(hp->h_name) > hbsize) 609 hp->h_name[hbsize] = '\0'; 610 (void) strcpy(hbuf, hp->h_name); 611 return hbuf; 612 } 613 614 # else /* DAEMON */ 615 /* code for systems without sophisticated networking */ 616 617 /* 618 ** MYHOSTNAME -- stub version for case of no daemon code. 619 ** 620 ** Can't convert to upper case here because might be a UUCP name. 621 ** 622 ** Mark, you can change this to be anything you want...... 623 */ 624 625 char ** 626 myhostname(hostbuf, size) 627 char hostbuf[]; 628 int size; 629 { 630 register FILE *f; 631 632 hostbuf[0] = '\0'; 633 f = fopen("/usr/include/whoami", "r"); 634 if (f != NULL) 635 { 636 (void) fgets(hostbuf, size, f); 637 fixcrlf(hostbuf, TRUE); 638 (void) fclose(f); 639 } 640 return (NULL); 641 } 642 /* 643 ** MAPHOSTNAME -- turn a hostname into canonical form 644 ** 645 ** Parameters: 646 ** map -- a pointer to the database map. 647 ** hbuf -- a buffer containing a hostname. 648 ** avp -- a pointer to a (cf file defined) argument vector. 649 ** 650 ** Returns: 651 ** mapped host name 652 ** FALSE otherwise. 653 ** 654 ** Side Effects: 655 ** Looks up the host specified in hbuf. If it is not 656 ** the canonical name for that host, replace it with 657 ** the canonical name. If the name is unknown, or it 658 ** is already the canonical name, leave it unchanged. 659 */ 660 661 /*ARGSUSED*/ 662 char * 663 maphostname(map, hbuf, hbsize, avp) 664 MAP *map; 665 char *hbuf; 666 int hbsize; 667 char **avp; 668 { 669 return NULL; 670 } 671 672 #endif /* DAEMON */ 673