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