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 5.42.1.2 (Berkeley) 05/29/92 (with daemon mode)"; 15 #else 16 static char sccsid[] = "@(#)daemon.c 5.42.1.2 (Berkeley) 05/29/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(hbuf, hbufsize) 55 ** Convert the entry in hbuf into a canonical form. It 56 ** may not be larger than hbufsize. 57 */ 58 /* 59 ** GETREQUESTS -- open mail IPC port and get requests. 60 ** 61 ** Parameters: 62 ** none. 63 ** 64 ** Returns: 65 ** none. 66 ** 67 ** Side Effects: 68 ** Waits until some interesting activity occurs. When 69 ** it does, a child is created to process it, and the 70 ** parent waits for completion. Return from this 71 ** routine is always in the child. The file pointers 72 ** "InChannel" and "OutChannel" should be set to point 73 ** to the communication channel. 74 */ 75 76 int DaemonSocket = -1; /* fd describing socket */ 77 78 getrequests() 79 { 80 int t; 81 register struct servent *sp; 82 int on = 1; 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, 15)) 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 if (listen(DaemonSocket, 10) < 0) 135 { 136 syserr("getrequests: cannot listen"); 137 (void) close(DaemonSocket); 138 goto severe; 139 } 140 141 (void) signal(SIGCHLD, reapchild); 142 143 if (tTd(15, 1)) 144 printf("getrequests: %d\n", DaemonSocket); 145 146 for (;;) 147 { 148 register int pid; 149 auto int lotherend; 150 151 /* see if we are rejecting connections */ 152 while ((CurrentLA = getla()) > RefuseLA) 153 { 154 setproctitle("rejecting connections: load average: %.2f", (double)CurrentLA); 155 sleep(5); 156 } 157 158 /* wait for a connection */ 159 setproctitle("accepting connections"); 160 do 161 { 162 errno = 0; 163 lotherend = sizeof RealHostAddr; 164 t = accept(DaemonSocket, 165 (struct sockaddr *)&RealHostAddr, &lotherend); 166 } while (t < 0 && errno == EINTR); 167 if (t < 0) 168 { 169 syserr("getrequests: accept"); 170 sleep(5); 171 continue; 172 } 173 174 /* 175 ** Create a subprocess to process the mail. 176 */ 177 178 if (tTd(15, 2)) 179 printf("getrequests: forking (fd = %d)\n", t); 180 181 pid = fork(); 182 if (pid < 0) 183 { 184 syserr("daemon: cannot fork"); 185 sleep(10); 186 (void) close(t); 187 continue; 188 } 189 190 if (pid == 0) 191 { 192 extern struct hostent *gethostbyaddr(); 193 register struct hostent *hp; 194 char buf[MAXNAME]; 195 196 /* 197 ** CHILD -- return to caller. 198 ** Collect verified idea of sending host. 199 ** Verify calling user id if possible here. 200 */ 201 202 (void) signal(SIGCHLD, SIG_DFL); 203 204 /* determine host name */ 205 hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 206 if (hp != NULL) 207 (void) strcpy(buf, hp->h_name); 208 else 209 { 210 extern char *inet_ntoa(); 211 212 /* produce a dotted quad */ 213 (void) sprintf(buf, "[%s]", 214 inet_ntoa(RealHostAddr.sin_addr)); 215 } 216 217 /* should we check for illegal connection here? XXX */ 218 219 RealHostName = newstr(buf); 220 221 (void) close(DaemonSocket); 222 InChannel = fdopen(t, "r"); 223 OutChannel = fdopen(dup(t), "w"); 224 if (tTd(15, 2)) 225 printf("getreq: returning\n"); 226 # ifdef LOG 227 if (LogLevel > 11) 228 syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 229 # endif LOG 230 return; 231 } 232 233 /* close the port so that others will hang (for a while) */ 234 (void) close(t); 235 } 236 /*NOTREACHED*/ 237 } 238 /* 239 ** CLRDAEMON -- reset the daemon connection 240 ** 241 ** Parameters: 242 ** none. 243 ** 244 ** Returns: 245 ** none. 246 ** 247 ** Side Effects: 248 ** releases any resources used by the passive daemon. 249 */ 250 251 clrdaemon() 252 { 253 if (DaemonSocket >= 0) 254 (void) close(DaemonSocket); 255 DaemonSocket = -1; 256 } 257 /* 258 ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 259 ** 260 ** Parameters: 261 ** host -- the name of the host. 262 ** port -- the port number to connect to. 263 ** mci -- a pointer to the mail connection information 264 ** structure to be filled in. 265 ** usesecureport -- if set, use a low numbered (reserved) 266 ** port to provide some rudimentary authentication. 267 ** 268 ** Returns: 269 ** An exit code telling whether the connection could be 270 ** made and if not why not. 271 ** 272 ** Side Effects: 273 ** none. 274 */ 275 276 makeconnection(host, port, mci, usesecureport) 277 char *host; 278 u_short port; 279 register MCONINFO *mci; 280 bool usesecureport; 281 { 282 register int i, s; 283 register struct hostent *hp = (struct hostent *)NULL; 284 struct sockaddr_in addr; 285 int sav_errno; 286 extern char *inet_ntoa(); 287 #ifdef NAMED_BIND 288 extern int h_errno; 289 #endif 290 291 /* 292 ** Set up the address for the mailer. 293 ** Accept "[a.b.c.d]" syntax for host name. 294 */ 295 296 #ifdef NAMED_BIND 297 h_errno = 0; 298 #endif 299 errno = 0; 300 301 if (host[0] == '[') 302 { 303 long hid; 304 register char *p = index(host, ']'); 305 306 if (p != NULL) 307 { 308 *p = '\0'; 309 hid = inet_addr(&host[1]); 310 *p = ']'; 311 } 312 if (p == NULL || hid == -1) 313 { 314 usrerr("Invalid numeric domain spec \"%s\"", host); 315 return (EX_NOHOST); 316 } 317 addr.sin_addr.s_addr = hid; 318 } 319 else 320 { 321 hp = gethostbyname(host); 322 if (hp == NULL) 323 { 324 #ifdef NAMED_BIND 325 if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 326 return (EX_TEMPFAIL); 327 328 /* if name server is specified, assume temp fail */ 329 if (errno == ECONNREFUSED && UseNameServer) 330 return (EX_TEMPFAIL); 331 #endif 332 333 /* 334 ** XXX Should look for mail forwarder record here 335 ** XXX if (h_errno == NO_ADDRESS). 336 */ 337 338 return (EX_NOHOST); 339 } 340 bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length); 341 i = 1; 342 } 343 344 /* 345 ** Determine the port number. 346 */ 347 348 if (port != 0) 349 addr.sin_port = htons(port); 350 else 351 { 352 register struct servent *sp = getservbyname("smtp", "tcp"); 353 354 if (sp == NULL) 355 { 356 syserr("makeconnection: server \"smtp\" unknown"); 357 return (EX_OSFILE); 358 } 359 addr.sin_port = sp->s_port; 360 } 361 362 /* 363 ** Try to actually open the connection. 364 */ 365 366 again: 367 if (tTd(16, 1)) 368 printf("makeconnection (%s [%s])\n", host, 369 inet_ntoa(addr.sin_addr)); 370 371 if (usesecureport) 372 { 373 int rport = IPPORT_RESERVED - 1; 374 375 s = rresvport(&rport); 376 } 377 else 378 { 379 s = socket(AF_INET, SOCK_STREAM, 0); 380 } 381 if (s < 0) 382 { 383 sav_errno = errno; 384 syserr("makeconnection: no socket"); 385 goto failure; 386 } 387 388 if (tTd(16, 1)) 389 printf("makeconnection: %d\n", s); 390 391 /* turn on network debugging? */ 392 if (tTd(16, 14)) 393 { 394 int on = 1; 395 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 396 } 397 if (CurEnv->e_xfp != NULL) 398 (void) fflush(CurEnv->e_xfp); /* for debugging */ 399 errno = 0; /* for debugging */ 400 addr.sin_family = AF_INET; 401 if (connect(s, (struct sockaddr *) &addr, sizeof addr) < 0) 402 { 403 sav_errno = errno; 404 (void) close(s); 405 if (hp && hp->h_addr_list[i]) 406 { 407 bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr, 408 hp->h_length); 409 goto again; 410 } 411 412 /* failure, decide if temporary or not */ 413 failure: 414 switch (sav_errno) 415 { 416 case EISCONN: 417 case ETIMEDOUT: 418 case EINPROGRESS: 419 case EALREADY: 420 case EADDRINUSE: 421 case EHOSTDOWN: 422 case ENETDOWN: 423 case ENETRESET: 424 case ENOBUFS: 425 case ECONNREFUSED: 426 case ECONNRESET: 427 case EHOSTUNREACH: 428 case ENETUNREACH: 429 #ifdef ENOSR 430 case ENOSR: 431 #endif 432 /* there are others, I'm sure..... */ 433 return (EX_TEMPFAIL); 434 435 case EPERM: 436 /* why is this happening? */ 437 syserr("makeconnection: funny failure, addr=%lx, port=%x", 438 addr.sin_addr.s_addr, addr.sin_port); 439 return (EX_TEMPFAIL); 440 441 default: 442 { 443 extern char *errstring(); 444 445 message(Arpa_Info, "%s", errstring(sav_errno)); 446 return (EX_UNAVAILABLE); 447 } 448 } 449 } 450 451 /* connection ok, put it into canonical form */ 452 mci->mci_out = fdopen(s, "w"); 453 mci->mci_in = fdopen(dup(s), "r"); 454 455 return (EX_OK); 456 } 457 /* 458 ** MYHOSTNAME -- return the name of this host. 459 ** 460 ** Parameters: 461 ** hostbuf -- a place to return the name of this host. 462 ** size -- the size of hostbuf. 463 ** 464 ** Returns: 465 ** A list of aliases for this host. 466 ** 467 ** Side Effects: 468 ** none. 469 */ 470 471 char ** 472 myhostname(hostbuf, size) 473 char hostbuf[]; 474 int size; 475 { 476 extern struct hostent *gethostbyname(); 477 struct hostent *hp; 478 479 if (gethostname(hostbuf, size) < 0) 480 { 481 (void) strcpy(hostbuf, "localhost"); 482 } 483 hp = gethostbyname(hostbuf); 484 if (hp != NULL) 485 { 486 (void) strcpy(hostbuf, hp->h_name); 487 return (hp->h_aliases); 488 } 489 else 490 return (NULL); 491 } 492 /* 493 * MAPHOSTNAME -- turn a hostname into canonical form 494 * 495 * Parameters: 496 * hbuf -- a buffer containing a hostname. 497 * hbsize -- the size of hbuf. 498 * 499 * Returns: 500 * TRUE if the host name was mapped. 501 * FALSE otherwise. 502 * 503 * Side Effects: 504 * Looks up the host specified in hbuf. If it is not 505 * the canonical name for that host, replace it with 506 * the canonical name. If the name is unknown, or it 507 * is already the canonical name, leave it unchanged. 508 */ 509 510 bool 511 maphostname(hbuf, hbsize) 512 char *hbuf; 513 int hbsize; 514 { 515 register struct hostent *hp; 516 u_long in_addr; 517 char ptr[256], *cp; 518 struct hostent *gethostbyaddr(); 519 520 /* 521 * If first character is a bracket, then it is an address 522 * lookup. Address is copied into a temporary buffer to 523 * strip the brackets and to preserve hbuf if address is 524 * unknown. 525 */ 526 if (*hbuf != '[') 527 return (getcanonname(hbuf, hbsize)); 528 if ((cp = index(strcpy(ptr, hbuf), ']')) == NULL) 529 return (FALSE); 530 *cp = '\0'; 531 in_addr = inet_addr(&ptr[1]); 532 hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 533 if (hp == NULL) 534 return (FALSE); 535 if (strlen(hp->h_name) >= hbsize) 536 hp->h_name[hbsize - 1] = '\0'; 537 (void)strcpy(hbuf, hp->h_name); 538 return (TRUE); 539 } 540 541 # else DAEMON 542 /* code for systems without sophisticated networking */ 543 544 /* 545 ** MYHOSTNAME -- stub version for case of no daemon code. 546 ** 547 ** Can't convert to upper case here because might be a UUCP name. 548 ** 549 ** Mark, you can change this to be anything you want...... 550 */ 551 552 char ** 553 myhostname(hostbuf, size) 554 char hostbuf[]; 555 int size; 556 { 557 register FILE *f; 558 559 hostbuf[0] = '\0'; 560 f = fopen("/usr/include/whoami", "r"); 561 if (f != NULL) 562 { 563 (void) fgets(hostbuf, size, f); 564 fixcrlf(hostbuf, TRUE); 565 (void) fclose(f); 566 } 567 return (NULL); 568 } 569 /* 570 ** MAPHOSTNAME -- turn a hostname into canonical form 571 ** 572 ** Parameters: 573 ** hbuf -- a buffer containing a hostname. 574 ** hbsize -- the size of hbuf. 575 ** 576 ** Returns: 577 ** TRUE if the hostname was mapped. 578 ** FALSE otherwise. 579 ** 580 ** Side Effects: 581 ** Looks up the host specified in hbuf. If it is not 582 ** the canonical name for that host, replace it with 583 ** the canonical name. If the name is unknown, or it 584 ** is already the canonical name, leave it unchanged. 585 */ 586 587 /*ARGSUSED*/ 588 bool 589 maphostname(hbuf, hbsize) 590 char *hbuf; 591 int hbsize; 592 { 593 return (FALSE); 594 } 595 596 #endif DAEMON 597