1 # include <errno.h> 2 # include "sendmail.h" 3 4 #ifndef DAEMON 5 SCCSID(@(#)daemon.c 3.46 01/16/83 (w/o daemon mode)); 6 #else 7 8 #include <sys/socket.h> 9 #include <netinet/in.h> 10 #include <netdb.h> 11 #include <wait.h> 12 13 SCCSID(@(#)daemon.c 3.46 01/16/83 (with daemon mode)); 14 15 /* 16 ** DAEMON.C -- routines to use when running as a daemon. 17 ** 18 ** This entire file is highly dependent on the 4.2 BSD 19 ** interprocess communication primitives. No attempt has 20 ** been made to make this file portable to Version 7, 21 ** Version 6, MPX files, etc. If you should try such a 22 ** thing yourself, I recommend chucking the entire file 23 ** and starting from scratch. Basic semantics are: 24 ** 25 ** getrequests() 26 ** Opens a port and initiates a connection. 27 ** Returns in a child. Must set InChannel and 28 ** OutChannel appropriately. 29 ** clrdaemon() 30 ** Close any open files associated with getting 31 ** the connection; this is used when running the queue, 32 ** etc., to avoid having extra file descriptors during 33 ** the queue run and to avoid confusing the network 34 ** code (if it cares). 35 ** makeconnection(host, port, outfile, infile) 36 ** Make a connection to the named host on the given 37 ** port. Set *outfile and *infile to the files 38 ** appropriate for communication. Returns zero on 39 ** success, else an exit status describing the 40 ** error. 41 ** 42 ** The semantics of both of these should be clean. 43 */ 44 /* 45 ** GETREQUESTS -- open mail IPC port and get requests. 46 ** 47 ** Parameters: 48 ** none. 49 ** 50 ** Returns: 51 ** none. 52 ** 53 ** Side Effects: 54 ** Waits until some interesting activity occurs. When 55 ** it does, a child is created to process it, and the 56 ** parent waits for completion. Return from this 57 ** routine is always in the child. The file pointers 58 ** "InChannel" and "OutChannel" should be set to point 59 ** to the communication channel. 60 */ 61 62 struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 63 int DaemonSocket = -1; /* fd describing socket */ 64 int MaxConnections = 4; /* maximum simultaneous sendmails */ 65 66 getrequests() 67 { 68 int t; 69 union wait status; 70 int numconnections = 0; 71 register struct servent *sp; 72 73 /* 74 ** Set up the address for the mailer. 75 */ 76 77 sp = getservbyname("smtp", "tcp"); 78 if (sp == NULL) 79 { 80 syserr("server \"smtp\" unknown"); 81 goto severe; 82 } 83 SendmailAddress.sin_family = AF_INET; 84 SendmailAddress.sin_addr.s_addr = INADDR_ANY; 85 SendmailAddress.sin_port = sp->s_port; 86 87 /* 88 ** Try to actually open the connection. 89 */ 90 91 # ifdef DEBUG 92 if (tTd(15, 1)) 93 printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 94 # endif DEBUG 95 96 /* get a socket for the SMTP connection */ 97 DaemonSocket = socket(AF_INET, SOCK_STREAM, 0, 0); 98 if (DaemonSocket < 0) 99 { 100 /* probably another daemon already */ 101 syserr("getrequests: can't create socket"); 102 severe: 103 # ifdef LOG 104 if (LogLevel > 0) 105 syslog(LOG_SALERT, "cannot get connection"); 106 # endif LOG 107 finis(); 108 } 109 110 #ifdef DEBUG 111 /* turn on network debugging? */ 112 if (tTd(15, 15)) 113 (void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 0, 0); 114 #endif DEBUG 115 116 if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 117 { 118 syserr("getrequests: cannot bind"); 119 (void) close(DaemonSocket); 120 goto severe; 121 } 122 listen(DaemonSocket, 10); 123 124 # ifdef DEBUG 125 if (tTd(15, 1)) 126 printf("getrequests: %d\n", DaemonSocket); 127 # endif DEBUG 128 129 for (;;) 130 { 131 /* wait for a connection */ 132 register int pid; 133 134 do 135 { 136 auto int lotherend; 137 struct sockaddr otherend; 138 139 errno = 0; 140 lotherend = sizeof otherend; 141 t = accept(DaemonSocket, &otherend, &lotherend, 0); 142 } while (t < 0 && errno == EINTR); 143 if (t < 0) 144 { 145 syserr("getrequests: accept"); 146 sleep(5); 147 continue; 148 } 149 150 /* 151 ** Create a subprocess to process the mail. 152 */ 153 154 # ifdef DEBUG 155 if (tTd(15, 2)) 156 printf("getrequests: forking (fd = %d)\n", t); 157 # endif DEBUG 158 159 pid = fork(); 160 if (pid < 0) 161 { 162 syserr("daemon: cannot fork"); 163 sleep(10); 164 (void) close(t); 165 continue; 166 } 167 168 if (pid == 0) 169 { 170 /* 171 ** CHILD -- return to caller. 172 ** Verify calling user id if possible here. 173 */ 174 175 (void) close(DaemonSocket); 176 InChannel = fdopen(t, "r"); 177 OutChannel = fdopen(t, "w"); 178 # ifdef DEBUG 179 if (tTd(15, 2)) 180 printf("getreq: returning\n"); 181 # endif DEBUG 182 # ifdef LOG 183 if (LogLevel > 11) 184 syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 185 # endif LOG 186 return; 187 } 188 189 /* 190 ** PARENT -- wait for child to terminate. 191 ** Perhaps we should allow concurrent processing? 192 */ 193 194 # ifdef DEBUG 195 if (tTd(15, 2)) 196 { 197 sleep(2); 198 printf("getreq: parent waiting\n"); 199 } 200 # endif DEBUG 201 202 /* close the port so that others will hang (for a while) */ 203 (void) close(t); 204 205 /* pick up old zombies; implement load limiting */ 206 numconnections++; 207 while (wait3(&status, numconnections < MaxConnections ? WNOHANG : 0, 0) > 0) 208 numconnections--; 209 } 210 /*NOTREACHED*/ 211 } 212 /* 213 ** CLRDAEMON -- reset the daemon connection 214 ** 215 ** Parameters: 216 ** none. 217 ** 218 ** Returns: 219 ** none. 220 ** 221 ** Side Effects: 222 ** releases any resources used by the passive daemon. 223 */ 224 225 clrdaemon() 226 { 227 if (DaemonSocket >= 0) 228 (void) close(DaemonSocket); 229 DaemonSocket = -1; 230 } 231 /* 232 ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 233 ** 234 ** Parameters: 235 ** host -- the name of the host. 236 ** port -- the port number to connect to. 237 ** outfile -- a pointer to a place to put the outfile 238 ** descriptor. 239 ** infile -- ditto for infile. 240 ** 241 ** Returns: 242 ** An exit code telling whether the connection could be 243 ** made and if not why not. 244 ** 245 ** Side Effects: 246 ** none. 247 */ 248 249 makeconnection(host, port, outfile, infile) 250 char *host; 251 u_short port; 252 FILE **outfile; 253 FILE **infile; 254 { 255 register int s; 256 257 /* 258 ** Set up the address for the mailer. 259 ** Accept "[a.b.c.d]" syntax for host name. 260 */ 261 262 if (host[0] == '[') 263 { 264 long hid = 0; 265 int i, j; 266 register char *p = host; 267 268 for (i = 3; i >= 0 && *p != ']' && *p != '\0'; i--) 269 { 270 j = 0; 271 while (isdigit(*++p)) 272 j = j * 10 + (*p - '0'); 273 if (*p != (i == 0 ? ']' : '.') || j > 255 || j < 0) 274 break; 275 hid |= j << ((3 - i) * 8); 276 } 277 if (i >= 0 || *p != ']' || *++p != '\0') 278 { 279 usrerr("Invalid numeric domain spec \"%s\"", host); 280 return (EX_NOHOST); 281 } 282 SendmailAddress.sin_addr.s_addr = hid; 283 } 284 else 285 { 286 register struct hostent *hp = gethostbyname(host); 287 288 if (hp == 0) 289 return (EX_NOHOST); 290 bmove(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 291 } 292 293 /* 294 ** Determine the port number. 295 */ 296 297 if (port != 0) 298 SendmailAddress.sin_port = htons(port); 299 else 300 { 301 register struct servent *sp = getservbyname("smtp", "tcp"); 302 303 if (sp == NULL) 304 { 305 syserr("makeconnection: server \"smtp\" unknown"); 306 return (EX_OSFILE); 307 } 308 SendmailAddress.sin_port = sp->s_port; 309 } 310 311 /* 312 ** Try to actually open the connection. 313 */ 314 315 # ifdef DEBUG 316 if (tTd(16, 1)) 317 printf("makeconnection (%s)\n", host); 318 # endif DEBUG 319 320 s = socket(AF_INET, SOCK_STREAM, 0, 0); 321 if (s < 0) 322 { 323 syserr("makeconnection: no socket"); 324 goto failure; 325 } 326 327 # ifdef DEBUG 328 if (tTd(16, 1)) 329 printf("makeconnection: %d\n", s); 330 331 /* turn on network debugging? */ 332 if (tTd(16, 14)) 333 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0); 334 # endif DEBUG 335 (void) fflush(CurEnv->e_xfp); /* for debugging */ 336 SendmailAddress.sin_family = AF_INET; 337 bind(s, &SendmailAddress, sizeof SendmailAddress, 0); 338 if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 339 { 340 /* failure, decide if temporary or not */ 341 failure: 342 switch (errno) 343 { 344 case EISCONN: 345 case ETIMEDOUT: 346 case EINPROGRESS: 347 case EALREADY: 348 case EADDRINUSE: 349 case EHOSTDOWN: 350 case ENETDOWN: 351 case ENETRESET: 352 case ENOBUFS: 353 case ECONNREFUSED: 354 case EHOSTUNREACH: 355 case ENETUNREACH: 356 /* there are others, I'm sure..... */ 357 return (EX_TEMPFAIL); 358 359 default: 360 return (EX_UNAVAILABLE); 361 } 362 } 363 364 /* connection ok, put it into canonical form */ 365 *outfile = fdopen(s, "w"); 366 *infile = fdopen(s, "r"); 367 368 return (EX_OK); 369 } 370 371 #endif DAEMON 372