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.1 (Berkeley) 12/21/92 (with daemon mode)"; 15 #else 16 static char sccsid[] = "@(#)daemon.c 6.1 (Berkeley) 12/21/92 (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("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, "cannot get connection"); 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: %.2f", 157 (double)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 > 9) 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("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 358 /* 359 ** XXX Should look for mail forwarder record here 360 ** XXX if (h_errno == NO_ADDRESS). 361 */ 362 363 return (EX_NOHOST); 364 } 365 bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length); 366 i = 1; 367 } 368 369 /* 370 ** Determine the port number. 371 */ 372 373 if (port != 0) 374 addr.sin_port = htons(port); 375 else 376 { 377 register struct servent *sp = getservbyname("smtp", "tcp"); 378 379 if (sp == NULL) 380 { 381 syserr("makeconnection: server \"smtp\" unknown"); 382 return (EX_OSFILE); 383 } 384 addr.sin_port = sp->s_port; 385 } 386 387 /* 388 ** Try to actually open the connection. 389 */ 390 391 again: 392 if (tTd(16, 1)) 393 printf("makeconnection (%s [%s])\n", host, 394 inet_ntoa(addr.sin_addr)); 395 396 if (usesecureport) 397 { 398 int rport = IPPORT_RESERVED - 1; 399 400 s = rresvport(&rport); 401 } 402 else 403 { 404 s = socket(AF_INET, SOCK_STREAM, 0); 405 } 406 if (s < 0) 407 { 408 sav_errno = errno; 409 syserr("makeconnection: no socket"); 410 goto failure; 411 } 412 413 if (tTd(16, 1)) 414 printf("makeconnection: fd=%d\n", s); 415 416 /* turn on network debugging? */ 417 if (tTd(16, 101)) 418 { 419 int on = 1; 420 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 421 } 422 if (CurEnv->e_xfp != NULL) 423 (void) fflush(CurEnv->e_xfp); /* for debugging */ 424 errno = 0; /* for debugging */ 425 addr.sin_family = AF_INET; 426 if (connect(s, (struct sockaddr *) &addr, sizeof addr) < 0) 427 { 428 sav_errno = errno; 429 (void) close(s); 430 if (hp && hp->h_addr_list[i]) 431 { 432 bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr, 433 hp->h_length); 434 goto again; 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(Arpa_Info, "%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 ** none. 494 */ 495 496 char ** 497 myhostname(hostbuf, size) 498 char hostbuf[]; 499 int size; 500 { 501 extern struct hostent *gethostbyname(); 502 struct hostent *hp; 503 504 if (gethostname(hostbuf, size) < 0) 505 { 506 (void) strcpy(hostbuf, "localhost"); 507 } 508 hp = gethostbyname(hostbuf); 509 if (hp != NULL) 510 { 511 (void) strcpy(hostbuf, hp->h_name); 512 return (hp->h_aliases); 513 } 514 else 515 return (NULL); 516 } 517 /* 518 ** MAPHOSTNAME -- turn a hostname into canonical form 519 ** 520 ** Parameters: 521 ** map -- a pointer to this map (unused). 522 ** hbuf -- a buffer containing a hostname. 523 ** hbsize -- the size of hbuf. 524 ** avp -- unused -- for compatibility with other mapping 525 ** functions. 526 ** 527 ** Returns: 528 ** The mapping, if found. 529 ** NULL if no mapping found. 530 ** 531 ** Side Effects: 532 ** Looks up the host specified in hbuf. If it is not 533 ** the canonical name for that host, return the canonical 534 ** name. 535 */ 536 537 char * 538 maphostname(map, hbuf, hbsize, avp) 539 MAP *map; 540 char *hbuf; 541 int hbsize; 542 char **avp; 543 { 544 register struct hostent *hp; 545 u_long in_addr; 546 char *cp; 547 struct hostent *gethostbyaddr(); 548 549 /* allow room for null */ 550 hbsize--; 551 552 /* 553 * If first character is a bracket, then it is an address 554 * lookup. Address is copied into a temporary buffer to 555 * strip the brackets and to preserve hbuf if address is 556 * unknown. 557 */ 558 559 if (*hbuf != '[') 560 { 561 extern bool getcanonname(); 562 563 if (getcanonname(hbuf, hbsize)) 564 return hbuf; 565 else 566 return NULL; 567 } 568 if ((cp = strchr(hbuf, ']')) == NULL) 569 return (NULL); 570 *cp = '\0'; 571 in_addr = inet_addr(&hbuf[1]); 572 hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 573 if (hp == NULL) 574 return (NULL); 575 576 /* found a match -- copy and dot terminate */ 577 if (strlen(hp->h_name) > hbsize) 578 hp->h_name[hbsize] = '\0'; 579 (void) strcpy(hbuf, hp->h_name); 580 return hbuf; 581 } 582 583 # else /* DAEMON */ 584 /* code for systems without sophisticated networking */ 585 586 /* 587 ** MYHOSTNAME -- stub version for case of no daemon code. 588 ** 589 ** Can't convert to upper case here because might be a UUCP name. 590 ** 591 ** Mark, you can change this to be anything you want...... 592 */ 593 594 char ** 595 myhostname(hostbuf, size) 596 char hostbuf[]; 597 int size; 598 { 599 register FILE *f; 600 601 hostbuf[0] = '\0'; 602 f = fopen("/usr/include/whoami", "r"); 603 if (f != NULL) 604 { 605 (void) fgets(hostbuf, size, f); 606 fixcrlf(hostbuf, TRUE); 607 (void) fclose(f); 608 } 609 return (NULL); 610 } 611 /* 612 ** MAPHOSTNAME -- turn a hostname into canonical form 613 ** 614 ** Parameters: 615 ** map -- a pointer to the database map. 616 ** hbuf -- a buffer containing a hostname. 617 ** avp -- a pointer to a (cf file defined) argument vector. 618 ** 619 ** Returns: 620 ** mapped host name 621 ** FALSE otherwise. 622 ** 623 ** Side Effects: 624 ** Looks up the host specified in hbuf. If it is not 625 ** the canonical name for that host, replace it with 626 ** the canonical name. If the name is unknown, or it 627 ** is already the canonical name, leave it unchanged. 628 */ 629 630 /*ARGSUSED*/ 631 char * 632 maphostname(map, hbuf, hbsize, avp) 633 MAP *map; 634 char *hbuf; 635 int hbsize; 636 char **avp; 637 { 638 return NULL; 639 } 640 641 #endif /* DAEMON */ 642