1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms are permitted 7 * provided that the above copyright notice and this paragraph are 8 * duplicated in all such forms and that any documentation, 9 * advertising materials, and other materials related to such 10 * distribution and use acknowledge that the software was developed 11 * by the University of California, Berkeley. The name of the 12 * University may not be used to endorse or promote products derived 13 * from this software without specific prior written permission. 14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 17 */ 18 19 #include <errno.h> 20 #include <sendmail.h> 21 22 #ifndef lint 23 #ifdef DAEMON 24 static char sccsid[] = "@(#)daemon.c 5.28 (Berkeley) 11/17/88 (with daemon mode)"; 25 #else 26 static char sccsid[] = "@(#)daemon.c 5.28 (Berkeley) 11/17/88 (without daemon mode)"; 27 #endif 28 #endif /* not lint */ 29 30 #ifdef DAEMON 31 32 # include <netdb.h> 33 # include <sys/signal.h> 34 # include <sys/wait.h> 35 # include <sys/time.h> 36 # include <sys/resource.h> 37 38 /* 39 ** DAEMON.C -- routines to use when running as a daemon. 40 ** 41 ** This entire file is highly dependent on the 4.2 BSD 42 ** interprocess communication primitives. No attempt has 43 ** been made to make this file portable to Version 7, 44 ** Version 6, MPX files, etc. If you should try such a 45 ** thing yourself, I recommend chucking the entire file 46 ** and starting from scratch. Basic semantics are: 47 ** 48 ** getrequests() 49 ** Opens a port and initiates a connection. 50 ** Returns in a child. Must set InChannel and 51 ** OutChannel appropriately. 52 ** clrdaemon() 53 ** Close any open files associated with getting 54 ** the connection; this is used when running the queue, 55 ** etc., to avoid having extra file descriptors during 56 ** the queue run and to avoid confusing the network 57 ** code (if it cares). 58 ** makeconnection(host, port, outfile, infile) 59 ** Make a connection to the named host on the given 60 ** port. Set *outfile and *infile to the files 61 ** appropriate for communication. Returns zero on 62 ** success, else an exit status describing the 63 ** error. 64 ** maphostname(hbuf, hbufsize) 65 ** Convert the entry in hbuf into a canonical form. It 66 ** may not be larger than hbufsize. 67 */ 68 /* 69 ** GETREQUESTS -- open mail IPC port and get requests. 70 ** 71 ** Parameters: 72 ** none. 73 ** 74 ** Returns: 75 ** none. 76 ** 77 ** Side Effects: 78 ** Waits until some interesting activity occurs. When 79 ** it does, a child is created to process it, and the 80 ** parent waits for completion. Return from this 81 ** routine is always in the child. The file pointers 82 ** "InChannel" and "OutChannel" should be set to point 83 ** to the communication channel. 84 */ 85 86 struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 87 88 int DaemonSocket = -1; /* fd describing socket */ 89 char *NetName; /* name of home (local?) network */ 90 91 getrequests() 92 { 93 int t; 94 register struct servent *sp; 95 int on = 1; 96 extern reapchild(); 97 98 /* 99 ** Set up the address for the mailer. 100 */ 101 102 sp = getservbyname("smtp", "tcp"); 103 if (sp == NULL) 104 { 105 syserr("server \"smtp\" unknown"); 106 goto severe; 107 } 108 SendmailAddress.sin_family = AF_INET; 109 SendmailAddress.sin_addr.s_addr = INADDR_ANY; 110 SendmailAddress.sin_port = sp->s_port; 111 112 /* 113 ** Try to actually open the connection. 114 */ 115 116 # ifdef DEBUG 117 if (tTd(15, 1)) 118 printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 119 # endif DEBUG 120 121 /* get a socket for the SMTP connection */ 122 DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 123 if (DaemonSocket < 0) 124 { 125 /* probably another daemon already */ 126 syserr("getrequests: can't create socket"); 127 severe: 128 # ifdef LOG 129 if (LogLevel > 0) 130 syslog(LOG_ALERT, "cannot get connection"); 131 # endif LOG 132 finis(); 133 } 134 135 #ifdef DEBUG 136 /* turn on network debugging? */ 137 if (tTd(15, 15)) 138 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 139 #endif DEBUG 140 141 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on); 142 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on); 143 144 if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 145 { 146 syserr("getrequests: cannot bind"); 147 (void) close(DaemonSocket); 148 goto severe; 149 } 150 if (listen(DaemonSocket, 10) < 0) 151 { 152 syserr("getrequests: cannot listen"); 153 (void) close(DaemonSocket); 154 goto severe; 155 } 156 157 (void) signal(SIGCHLD, reapchild); 158 159 # ifdef DEBUG 160 if (tTd(15, 1)) 161 printf("getrequests: %d\n", DaemonSocket); 162 # endif DEBUG 163 164 for (;;) 165 { 166 register int pid; 167 auto int lotherend; 168 extern int RefuseLA; 169 170 /* see if we are rejecting connections */ 171 while (getla() > RefuseLA) 172 sleep(5); 173 174 /* wait for a connection */ 175 do 176 { 177 errno = 0; 178 lotherend = sizeof RealHostAddr; 179 t = accept(DaemonSocket, &RealHostAddr, &lotherend); 180 } while (t < 0 && errno == EINTR); 181 if (t < 0) 182 { 183 syserr("getrequests: accept"); 184 sleep(5); 185 continue; 186 } 187 188 /* 189 ** Create a subprocess to process the mail. 190 */ 191 192 # ifdef DEBUG 193 if (tTd(15, 2)) 194 printf("getrequests: forking (fd = %d)\n", t); 195 # endif DEBUG 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 212 /* 213 ** CHILD -- return to caller. 214 ** Collect verified idea of sending host. 215 ** Verify calling user id if possible here. 216 */ 217 218 (void) signal(SIGCHLD, SIG_DFL); 219 220 /* determine host name */ 221 hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET); 222 if (hp != NULL) 223 (void) strcpy(buf, hp->h_name); 224 else 225 { 226 extern char *inet_ntoa(); 227 228 /* produce a dotted quad */ 229 (void) sprintf(buf, "[%s]", 230 inet_ntoa(RealHostAddr.sin_addr)); 231 } 232 233 /* should we check for illegal connection here? XXX */ 234 235 RealHostName = newstr(buf); 236 237 (void) close(DaemonSocket); 238 InChannel = fdopen(t, "r"); 239 OutChannel = fdopen(dup(t), "w"); 240 # ifdef DEBUG 241 if (tTd(15, 2)) 242 printf("getreq: returning\n"); 243 # endif DEBUG 244 # ifdef LOG 245 if (LogLevel > 11) 246 syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 247 # endif LOG 248 return; 249 } 250 251 /* close the port so that others will hang (for a while) */ 252 (void) close(t); 253 } 254 /*NOTREACHED*/ 255 } 256 /* 257 ** CLRDAEMON -- reset the daemon connection 258 ** 259 ** Parameters: 260 ** none. 261 ** 262 ** Returns: 263 ** none. 264 ** 265 ** Side Effects: 266 ** releases any resources used by the passive daemon. 267 */ 268 269 clrdaemon() 270 { 271 if (DaemonSocket >= 0) 272 (void) close(DaemonSocket); 273 DaemonSocket = -1; 274 } 275 /* 276 ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 277 ** 278 ** Parameters: 279 ** host -- the name of the host. 280 ** port -- the port number to connect to. 281 ** outfile -- a pointer to a place to put the outfile 282 ** descriptor. 283 ** infile -- ditto for infile. 284 ** 285 ** Returns: 286 ** An exit code telling whether the connection could be 287 ** made and if not why not. 288 ** 289 ** Side Effects: 290 ** none. 291 */ 292 293 makeconnection(host, port, outfile, infile) 294 char *host; 295 u_short port; 296 FILE **outfile; 297 FILE **infile; 298 { 299 register int i, s; 300 register struct hostent *hp = (struct hostent *)NULL; 301 extern char *inet_ntoa(); 302 int sav_errno; 303 #ifdef NAMED_BIND 304 extern int h_errno; 305 #endif 306 307 /* 308 ** Set up the address for the mailer. 309 ** Accept "[a.b.c.d]" syntax for host name. 310 */ 311 312 #ifdef NAMED_BIND 313 h_errno = 0; 314 #endif 315 errno = 0; 316 317 if (host[0] == '[') 318 { 319 long hid; 320 register char *p = index(host, ']'); 321 322 if (p != NULL) 323 { 324 *p = '\0'; 325 hid = inet_addr(&host[1]); 326 *p = ']'; 327 } 328 if (p == NULL || hid == -1) 329 { 330 usrerr("Invalid numeric domain spec \"%s\"", host); 331 return (EX_NOHOST); 332 } 333 SendmailAddress.sin_addr.s_addr = hid; 334 } 335 else 336 { 337 hp = gethostbyname(host); 338 if (hp == NULL) 339 { 340 #ifdef NAMED_BIND 341 if (errno == ETIMEDOUT || h_errno == TRY_AGAIN) 342 return (EX_TEMPFAIL); 343 344 /* if name server is specified, assume temp fail */ 345 if (errno == ECONNREFUSED && UseNameServer) 346 return (EX_TEMPFAIL); 347 #endif 348 349 /* 350 ** XXX Should look for mail forwarder record here 351 ** XXX if (h_errno == NO_ADDRESS). 352 */ 353 354 return (EX_NOHOST); 355 } 356 bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 357 i = 1; 358 } 359 360 /* 361 ** Determine the port number. 362 */ 363 364 if (port != 0) 365 SendmailAddress.sin_port = htons(port); 366 else 367 { 368 register struct servent *sp = getservbyname("smtp", "tcp"); 369 370 if (sp == NULL) 371 { 372 syserr("makeconnection: server \"smtp\" unknown"); 373 return (EX_OSFILE); 374 } 375 SendmailAddress.sin_port = sp->s_port; 376 } 377 378 /* 379 ** Try to actually open the connection. 380 */ 381 382 again: 383 # ifdef DEBUG 384 if (tTd(16, 1)) 385 printf("makeconnection (%s [%s])\n", host, 386 inet_ntoa(SendmailAddress.sin_addr.s_addr)); 387 # endif DEBUG 388 389 s = socket(AF_INET, SOCK_STREAM, 0); 390 if (s < 0) 391 { 392 syserr("makeconnection: no socket"); 393 sav_errno = errno; 394 goto failure; 395 } 396 397 # ifdef DEBUG 398 if (tTd(16, 1)) 399 printf("makeconnection: %d\n", s); 400 401 /* turn on network debugging? */ 402 if (tTd(16, 14)) 403 { 404 int on = 1; 405 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 406 } 407 # endif DEBUG 408 (void) fflush(CurEnv->e_xfp); /* for debugging */ 409 errno = 0; /* for debugging */ 410 SendmailAddress.sin_family = AF_INET; 411 if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0) 412 { 413 sav_errno = errno; 414 (void) close(s); 415 if (hp && hp->h_addr_list[i]) 416 { 417 bcopy(hp->h_addr_list[i++], 418 (char *)&SendmailAddress.sin_addr, hp->h_length); 419 goto again; 420 } 421 422 /* failure, decide if temporary or not */ 423 failure: 424 switch (sav_errno) 425 { 426 case EISCONN: 427 case ETIMEDOUT: 428 case EINPROGRESS: 429 case EALREADY: 430 case EADDRINUSE: 431 case EHOSTDOWN: 432 case ENETDOWN: 433 case ENETRESET: 434 case ENOBUFS: 435 case ECONNREFUSED: 436 case ECONNRESET: 437 case EHOSTUNREACH: 438 case ENETUNREACH: 439 /* there are others, I'm sure..... */ 440 return (EX_TEMPFAIL); 441 442 case EPERM: 443 /* why is this happening? */ 444 syserr("makeconnection: funny failure, addr=%lx, port=%x", 445 SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 446 return (EX_TEMPFAIL); 447 448 default: 449 message(Arpa_Info, "%s", errstring(sav_errno)); 450 return (EX_UNAVAILABLE); 451 } 452 } 453 454 /* connection ok, put it into canonical form */ 455 *outfile = fdopen(s, "w"); 456 *infile = fdopen(s, "r"); 457 458 return (EX_OK); 459 } 460 /* 461 ** MYHOSTNAME -- return the name of this host. 462 ** 463 ** Parameters: 464 ** hostbuf -- a place to return the name of this host. 465 ** size -- the size of hostbuf. 466 ** 467 ** Returns: 468 ** A list of aliases for this host. 469 ** 470 ** Side Effects: 471 ** none. 472 */ 473 474 char ** 475 myhostname(hostbuf, size) 476 char hostbuf[]; 477 int size; 478 { 479 extern struct hostent *gethostbyname(); 480 struct hostent *hp; 481 482 if (gethostname(hostbuf, size) < 0) 483 { 484 (void) strcpy(hostbuf, "localhost"); 485 } 486 hp = gethostbyname(hostbuf); 487 if (hp != NULL) 488 { 489 (void) strcpy(hostbuf, hp->h_name); 490 return (hp->h_aliases); 491 } 492 else 493 return (NULL); 494 } 495 496 /* 497 * MAPHOSTNAME -- turn a hostname into canonical form 498 * 499 * Parameters: 500 * hbuf -- a buffer containing a hostname. 501 * hbsize -- the size of hbuf. 502 * 503 * Returns: 504 * none. 505 * 506 * Side Effects: 507 * Looks up the host specified in hbuf. If it is not 508 * the canonical name for that host, replace it with 509 * the canonical name. If the name is unknown, or it 510 * is already the canonical name, leave it unchanged. 511 */ 512 maphostname(hbuf, hbsize) 513 char *hbuf; 514 int hbsize; 515 { 516 register struct hostent *hp; 517 u_long in_addr; 518 char ptr[256]; 519 struct hostent *gethostbyaddr(); 520 521 /* 522 * If first character is a bracket, then it is an address 523 * lookup. Address is copied into a temporary buffer to 524 * strip the brackets and to preserve hbuf if address is 525 * unknown. 526 */ 527 if (*hbuf != '[') { 528 getcanonname(hbuf, hbsize); 529 return; 530 } 531 *index(strcpy(ptr, hbuf), ']') = '\0'; 532 in_addr = inet_addr(&ptr[1]); 533 hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET); 534 if (hp == NULL) 535 return; 536 if (strlen(hp->h_name) >= hbsize) 537 hp->h_name[hbsize - 1] = '\0'; 538 (void)strcpy(hbuf, hp->h_name); 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 ** none. 578 ** 579 ** Side Effects: 580 ** Looks up the host specified in hbuf. If it is not 581 ** the canonical name for that host, replace it with 582 ** the canonical name. If the name is unknown, or it 583 ** is already the canonical name, leave it unchanged. 584 */ 585 586 /*ARGSUSED*/ 587 maphostname(hbuf, hbsize) 588 char *hbuf; 589 int hbsize; 590 { 591 return; 592 } 593 594 #endif DAEMON 595