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.10 (Berkeley) 09/19/85 (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.10 (Berkeley) 09/19/85 (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 ** 58 ** The semantics of both of these should be clean. 59 */ 60 /* 61 ** GETREQUESTS -- open mail IPC port and get requests. 62 ** 63 ** Parameters: 64 ** none. 65 ** 66 ** Returns: 67 ** none. 68 ** 69 ** Side Effects: 70 ** Waits until some interesting activity occurs. When 71 ** it does, a child is created to process it, and the 72 ** parent waits for completion. Return from this 73 ** routine is always in the child. The file pointers 74 ** "InChannel" and "OutChannel" should be set to point 75 ** to the communication channel. 76 */ 77 78 struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 79 80 int DaemonSocket = -1; /* fd describing socket */ 81 char *NetName; /* name of home (local?) network */ 82 83 getrequests() 84 { 85 int t; 86 register struct servent *sp; 87 extern reapchild(); 88 89 /* 90 ** Set up the address for the mailer. 91 */ 92 93 sp = getservbyname("smtp", "tcp"); 94 if (sp == NULL) 95 { 96 syserr("server \"smtp\" unknown"); 97 goto severe; 98 } 99 SendmailAddress.sin_family = AF_INET; 100 SendmailAddress.sin_addr.s_addr = INADDR_ANY; 101 SendmailAddress.sin_port = sp->s_port; 102 103 /* 104 ** Try to actually open the connection. 105 */ 106 107 # ifdef DEBUG 108 if (tTd(15, 1)) 109 printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 110 # endif DEBUG 111 112 /* get a socket for the SMTP connection */ 113 DaemonSocket = socket(AF_INET, SOCK_STREAM, 0); 114 if (DaemonSocket < 0) 115 { 116 /* probably another daemon already */ 117 syserr("getrequests: can't create socket"); 118 severe: 119 # ifdef LOG 120 if (LogLevel > 0) 121 syslog(LOG_ALERT, "cannot get connection"); 122 # endif LOG 123 finis(); 124 } 125 126 #ifdef DEBUG 127 /* turn on network debugging? */ 128 if (tTd(15, 15)) 129 { 130 int on = 1; 131 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 132 } 133 #endif DEBUG 134 135 if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0) 136 { 137 syserr("getrequests: cannot bind"); 138 (void) close(DaemonSocket); 139 goto severe; 140 } 141 if (listen(DaemonSocket, 10) < 0) 142 { 143 syserr("getrequests: cannot listen"); 144 (void) close(DaemonSocket); 145 goto severe; 146 } 147 148 signal(SIGCHLD, reapchild); 149 150 # ifdef DEBUG 151 if (tTd(15, 1)) 152 printf("getrequests: %d\n", DaemonSocket); 153 # endif DEBUG 154 155 for (;;) 156 { 157 register int pid; 158 auto int lotherend; 159 struct sockaddr_in otherend; 160 extern int RefuseLA; 161 162 /* see if we are rejecting connections */ 163 while (getla() > RefuseLA) 164 sleep(5); 165 166 /* wait for a connection */ 167 do 168 { 169 errno = 0; 170 lotherend = sizeof otherend; 171 t = accept(DaemonSocket, &otherend, &lotherend); 172 } while (t < 0 && errno == EINTR); 173 if (t < 0) 174 { 175 syserr("getrequests: accept"); 176 sleep(5); 177 continue; 178 } 179 180 /* 181 ** Create a subprocess to process the mail. 182 */ 183 184 # ifdef DEBUG 185 if (tTd(15, 2)) 186 printf("getrequests: forking (fd = %d)\n", t); 187 # endif DEBUG 188 189 pid = fork(); 190 if (pid < 0) 191 { 192 syserr("daemon: cannot fork"); 193 sleep(10); 194 (void) close(t); 195 continue; 196 } 197 198 if (pid == 0) 199 { 200 extern struct hostent *gethostbyaddr(); 201 register struct hostent *hp; 202 char buf[MAXNAME]; 203 204 /* 205 ** CHILD -- return to caller. 206 ** Collect verified idea of sending host. 207 ** Verify calling user id if possible here. 208 */ 209 210 signal(SIGCHLD, SIG_DFL); 211 212 /* determine host name */ 213 hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET); 214 if (hp != NULL) 215 { 216 (void) strcpy(buf, hp->h_name); 217 if (NetName != NULL && NetName[0] != '\0' && 218 index(hp->h_name, '.') == NULL) 219 { 220 (void) strcat(buf, "."); 221 (void) strcat(buf, NetName); 222 } 223 } 224 else 225 { 226 extern char *inet_ntoa(); 227 228 /* produce a dotted quad */ 229 (void) sprintf(buf, "[%s]", 230 inet_ntoa(otherend.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 ** REAPCHILD -- pick up the body of my child, lest it become a zombie 258 ** 259 ** Parameters: 260 ** none. 261 ** 262 ** Returns: 263 ** none. 264 ** 265 ** Side Effects: 266 ** Picks up zombies. 267 */ 268 269 reapchild() 270 { 271 union wait status; 272 273 while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0) 274 continue; 275 } 276 /* 277 ** CLRDAEMON -- reset the daemon connection 278 ** 279 ** Parameters: 280 ** none. 281 ** 282 ** Returns: 283 ** none. 284 ** 285 ** Side Effects: 286 ** releases any resources used by the passive daemon. 287 */ 288 289 clrdaemon() 290 { 291 if (DaemonSocket >= 0) 292 (void) close(DaemonSocket); 293 DaemonSocket = -1; 294 } 295 /* 296 ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 297 ** 298 ** Parameters: 299 ** host -- the name of the host. 300 ** port -- the port number to connect to. 301 ** outfile -- a pointer to a place to put the outfile 302 ** descriptor. 303 ** infile -- ditto for infile. 304 ** 305 ** Returns: 306 ** An exit code telling whether the connection could be 307 ** made and if not why not. 308 ** 309 ** Side Effects: 310 ** none. 311 */ 312 313 makeconnection(host, port, outfile, infile) 314 char *host; 315 u_short port; 316 FILE **outfile; 317 FILE **infile; 318 { 319 register int s; 320 321 /* 322 ** Set up the address for the mailer. 323 ** Accept "[a.b.c.d]" syntax for host name. 324 */ 325 326 if (host[0] == '[') 327 { 328 long hid; 329 register char *p = index(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 SendmailAddress.sin_addr.s_addr = hid; 343 } 344 else 345 { 346 register struct hostent *hp = gethostbyname(host); 347 348 if (errno == ETIMEDOUT) 349 { 350 CurEnv->e_flags &= ~EF_FATALERRS; 351 return (EX_TEMPFAIL); 352 } 353 if (hp == NULL) 354 return (EX_NOHOST); 355 bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 356 } 357 358 /* 359 ** Determine the port number. 360 */ 361 362 if (port != 0) 363 SendmailAddress.sin_port = htons(port); 364 else 365 { 366 register struct servent *sp = getservbyname("smtp", "tcp"); 367 368 if (sp == NULL) 369 { 370 syserr("makeconnection: server \"smtp\" unknown"); 371 return (EX_OSFILE); 372 } 373 SendmailAddress.sin_port = sp->s_port; 374 } 375 376 /* 377 ** Try to actually open the connection. 378 */ 379 380 # ifdef DEBUG 381 if (tTd(16, 1)) 382 printf("makeconnection (%s)\n", host); 383 # endif DEBUG 384 385 s = socket(AF_INET, SOCK_STREAM, 0); 386 if (s < 0) 387 { 388 syserr("makeconnection: no socket"); 389 goto failure; 390 } 391 392 # ifdef DEBUG 393 if (tTd(16, 1)) 394 printf("makeconnection: %d\n", s); 395 396 /* turn on network debugging? */ 397 if (tTd(16, 14)) 398 { 399 int on = 1; 400 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on); 401 } 402 # endif DEBUG 403 (void) fflush(CurEnv->e_xfp); /* for debugging */ 404 errno = 0; /* for debugging */ 405 SendmailAddress.sin_family = AF_INET; 406 if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0) 407 { 408 /* failure, decide if temporary or not */ 409 failure: 410 switch (errno) 411 { 412 case EISCONN: 413 case ETIMEDOUT: 414 case EINPROGRESS: 415 case EALREADY: 416 case EADDRINUSE: 417 case EHOSTDOWN: 418 case ENETDOWN: 419 case ENETRESET: 420 case ENOBUFS: 421 case ECONNREFUSED: 422 case ECONNRESET: 423 case EHOSTUNREACH: 424 case ENETUNREACH: 425 /* there are others, I'm sure..... */ 426 CurEnv->e_flags &= ~EF_FATALERRS; 427 return (EX_TEMPFAIL); 428 429 case EPERM: 430 /* why is this happening? */ 431 syserr("makeconnection: funny failure, addr=%lx, port=%x", 432 SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port); 433 return (EX_TEMPFAIL); 434 435 default: 436 return (EX_UNAVAILABLE); 437 } 438 } 439 440 /* connection ok, put it into canonical form */ 441 *outfile = fdopen(s, "w"); 442 *infile = fdopen(s, "r"); 443 444 return (EX_OK); 445 } 446 /* 447 ** MYHOSTNAME -- return the name of this host. 448 ** 449 ** Parameters: 450 ** hostbuf -- a place to return the name of this host. 451 ** size -- the size of hostbuf. 452 ** 453 ** Returns: 454 ** A list of aliases for this host. 455 ** 456 ** Side Effects: 457 ** none. 458 */ 459 460 char ** 461 myhostname(hostbuf, size) 462 char hostbuf[]; 463 int size; 464 { 465 extern struct hostent *gethostbyname(); 466 struct hostent *hp; 467 468 if (gethostname(hostbuf, size) < 0) 469 { 470 (void) strcpy(hostbuf, "localhost"); 471 } 472 hp = gethostbyname(hostbuf); 473 if (hp != NULL) 474 { 475 (void) strcpy(hostbuf, hp->h_name); 476 return (hp->h_aliases); 477 } 478 else 479 return (NULL); 480 } 481 /* 482 ** MAPHOSTNAME -- turn a hostname into canonical form 483 ** 484 ** Parameters: 485 ** hbuf -- a buffer containing a hostname. 486 ** hbsize -- the size of hbuf. 487 ** 488 ** Returns: 489 ** none. 490 ** 491 ** Side Effects: 492 ** Looks up the host specified in hbuf. If it is not 493 ** the canonical name for that host, replace it with 494 ** the canonical name. If the name is unknown, or it 495 ** is already the canonical name, leave it unchanged. 496 */ 497 498 maphostname(hbuf, hbsize) 499 char *hbuf; 500 int hbsize; 501 { 502 register struct hostent *hp; 503 extern struct hostent *gethostbyname(); 504 505 makelower(hbuf); 506 hp = gethostbyname(hbuf); 507 if (hp != NULL) 508 { 509 int i = strlen(hp->h_name); 510 511 if (i >= hbsize) 512 hp->h_name[--i] = '\0'; 513 (void) strcpy(hbuf, hp->h_name); 514 } 515 } 516 517 # else DAEMON 518 /* code for systems without sophisticated networking */ 519 520 /* 521 ** MYHOSTNAME -- stub version for case of no daemon code. 522 ** 523 ** Can't convert to upper case here because might be a UUCP name. 524 ** 525 ** Mark, you can change this to be anything you want...... 526 */ 527 528 char ** 529 myhostname(hostbuf, size) 530 char hostbuf[]; 531 int size; 532 { 533 register FILE *f; 534 535 hostbuf[0] = '\0'; 536 f = fopen("/usr/include/whoami", "r"); 537 if (f != NULL) 538 { 539 (void) fgets(hostbuf, size, f); 540 fixcrlf(hostbuf, TRUE); 541 (void) fclose(f); 542 } 543 return (NULL); 544 } 545 /* 546 ** MAPHOSTNAME -- turn a hostname into canonical form 547 ** 548 ** Parameters: 549 ** hbuf -- a buffer containing a hostname. 550 ** hbsize -- the size of hbuf. 551 ** 552 ** Returns: 553 ** none. 554 ** 555 ** Side Effects: 556 ** Looks up the host specified in hbuf. If it is not 557 ** the canonical name for that host, replace it with 558 ** the canonical name. If the name is unknown, or it 559 ** is already the canonical name, leave it unchanged. 560 */ 561 562 /*ARGSUSED*/ 563 maphostname(hbuf, hbsize) 564 char *hbuf; 565 int hbsize; 566 { 567 return; 568 } 569 570 571 #endif DAEMON 572