1 # include <errno.h> 2 # include "sendmail.h" 3 4 #ifndef DAEMON 5 SCCSID(@(#)daemon.c 3.44 01/08/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.44 01/08/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 # define MAXCONNS 4 /* maximum simultaneous sendmails */ 63 64 struct sockaddr_in SendmailAddress;/* internet address of sendmail */ 65 int DaemonSocket = -1; /* fd describing socket */ 66 67 getrequests() 68 { 69 int t; 70 union wait status; 71 int numconnections = 0; 72 register struct servent *sp; 73 74 /* 75 ** Set up the address for the mailer. 76 */ 77 78 sp = getservbyname("smtp", "tcp"); 79 if (sp == NULL) 80 { 81 syserr("server \"smtp\" unknown"); 82 goto severe; 83 } 84 SendmailAddress.sin_family = AF_INET; 85 SendmailAddress.sin_addr.s_addr = INADDR_ANY; 86 SendmailAddress.sin_port = sp->s_port; 87 88 /* 89 ** Try to actually open the connection. 90 */ 91 92 # ifdef DEBUG 93 if (tTd(15, 1)) 94 printf("getrequests: port 0x%x\n", SendmailAddress.sin_port); 95 # endif DEBUG 96 97 /* get a socket for the SMTP connection */ 98 DaemonSocket = socket(AF_INET, SOCK_STREAM, 0, 0); 99 if (DaemonSocket < 0) 100 { 101 /* probably another daemon already */ 102 syserr("getrequests: can't create socket"); 103 severe: 104 # ifdef LOG 105 if (LogLevel > 0) 106 syslog(LOG_SALERT, "cannot get connection"); 107 # endif LOG 108 finis(); 109 } 110 if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 111 { 112 syserr("getrequests: cannot bind"); 113 (void) close(DaemonSocket); 114 goto severe; 115 } 116 listen(DaemonSocket, 10); 117 118 # ifdef DEBUG 119 if (tTd(15, 1)) 120 printf("getrequests: %d\n", DaemonSocket); 121 # endif DEBUG 122 123 for (;;) 124 { 125 /* wait for a connection */ 126 register int pid; 127 128 do 129 { 130 auto int lotherend; 131 struct sockaddr otherend; 132 133 errno = 0; 134 lotherend = sizeof otherend; 135 t = accept(DaemonSocket, &otherend, &lotherend, 0); 136 } while (t < 0 && errno == EINTR); 137 if (t < 0) 138 { 139 syserr("getrequests: accept"); 140 sleep(5); 141 continue; 142 } 143 144 /* 145 ** Create a subprocess to process the mail. 146 */ 147 148 # ifdef DEBUG 149 if (tTd(15, 2)) 150 printf("getrequests: forking (fd = %d)\n", t); 151 # endif DEBUG 152 153 pid = fork(); 154 if (pid < 0) 155 { 156 syserr("daemon: cannot fork"); 157 sleep(10); 158 (void) close(t); 159 continue; 160 } 161 162 if (pid == 0) 163 { 164 /* 165 ** CHILD -- return to caller. 166 ** Verify calling user id if possible here. 167 */ 168 169 (void) close(DaemonSocket); 170 InChannel = fdopen(t, "r"); 171 OutChannel = fdopen(t, "w"); 172 # ifdef DEBUG 173 if (tTd(15, 2)) 174 printf("getreq: returning\n"); 175 # endif DEBUG 176 # ifdef LOG 177 if (LogLevel > 11) 178 syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 179 # endif LOG 180 return; 181 } 182 183 /* 184 ** PARENT -- wait for child to terminate. 185 ** Perhaps we should allow concurrent processing? 186 */ 187 188 # ifdef DEBUG 189 if (tTd(15, 2)) 190 { 191 sleep(2); 192 printf("getreq: parent waiting\n"); 193 } 194 # endif DEBUG 195 196 /* close the port so that others will hang (for a while) */ 197 (void) close(t); 198 199 /* pick up old zombies; implement load limiting */ 200 numconnections++; 201 while (wait3(&status, numconnections < MAXCONNS ? WNOHANG : 0, 0) > 0) 202 numconnections--; 203 } 204 /*NOTREACHED*/ 205 } 206 /* 207 ** CLRDAEMON -- reset the daemon connection 208 ** 209 ** Parameters: 210 ** none. 211 ** 212 ** Returns: 213 ** none. 214 ** 215 ** Side Effects: 216 ** releases any resources used by the passive daemon. 217 */ 218 219 clrdaemon() 220 { 221 if (DaemonSocket >= 0) 222 (void) close(DaemonSocket); 223 DaemonSocket = -1; 224 } 225 /* 226 ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 227 ** 228 ** Parameters: 229 ** host -- the name of the host. 230 ** port -- the port number to connect to. 231 ** outfile -- a pointer to a place to put the outfile 232 ** descriptor. 233 ** infile -- ditto for infile. 234 ** 235 ** Returns: 236 ** An exit code telling whether the connection could be 237 ** made and if not why not. 238 ** 239 ** Side Effects: 240 ** none. 241 */ 242 243 makeconnection(host, port, outfile, infile) 244 char *host; 245 u_short port; 246 FILE **outfile; 247 FILE **infile; 248 { 249 register int s; 250 251 /* 252 ** Set up the address for the mailer. 253 ** Accept "[a.b.c.d]" syntax for host name. 254 */ 255 256 if (host[0] == '[') 257 { 258 long hid = 0; 259 int i, j; 260 register char *p = host; 261 262 for (i = 3; i >= 0 && *p != ']' && *p != '\0'; i--) 263 { 264 j = 0; 265 while (isdigit(*++p)) 266 j = j * 10 + (*p - '0'); 267 if (*p != (i == 0 ? ']' : '.') || j > 255 || j < 0) 268 break; 269 hid |= j << ((3 - i) * 8); 270 } 271 if (i >= 0 || *p != ']' || *++p != '\0') 272 { 273 usrerr("Invalid numeric domain spec \"%s\"", host); 274 return (EX_NOHOST); 275 } 276 SendmailAddress.sin_addr.s_addr = hid; 277 } 278 else 279 { 280 register struct hostent *hp = gethostbyname(host); 281 282 if (hp == 0) 283 return (EX_NOHOST); 284 bmove(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length); 285 } 286 287 /* 288 ** Determine the port number. 289 */ 290 291 if (port != 0) 292 SendmailAddress.sin_port = htons(port); 293 else 294 { 295 register struct servent *sp = getservbyname("smtp", "tcp"); 296 297 if (sp == NULL) 298 { 299 syserr("makeconnection: server \"smtp\" unknown"); 300 return (EX_OSFILE); 301 } 302 SendmailAddress.sin_port = sp->s_port; 303 } 304 305 /* 306 ** Try to actually open the connection. 307 */ 308 309 # ifdef DEBUG 310 if (tTd(16, 1)) 311 printf("makeconnection (%s)\n", host); 312 # endif DEBUG 313 314 s = socket(AF_INET, SOCK_STREAM, 0, 0); 315 if (s < 0) 316 { 317 syserr("makeconnection: no socket"); 318 goto failure; 319 } 320 321 # ifdef DEBUG 322 if (tTd(16, 1)) 323 printf("makeconnection: %d\n", s); 324 # endif DEBUG 325 (void) fflush(CurEnv->e_xfp); /* for debugging */ 326 SendmailAddress.sin_family = AF_INET; 327 bind(s, &SendmailAddress, sizeof SendmailAddress, 0); 328 if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0) 329 { 330 /* failure, decide if temporary or not */ 331 failure: 332 switch (errno) 333 { 334 case EISCONN: 335 case ETIMEDOUT: 336 case EINPROGRESS: 337 case EALREADY: 338 case EADDRINUSE: 339 case EHOSTDOWN: 340 case ENETDOWN: 341 case ENETRESET: 342 case ENOBUFS: 343 case ECONNREFUSED: 344 case EHOSTUNREACH: 345 case ENETUNREACH: 346 /* there are others, I'm sure..... */ 347 return (EX_TEMPFAIL); 348 349 default: 350 return (EX_UNAVAILABLE); 351 } 352 } 353 354 /* connection ok, put it into canonical form */ 355 *outfile = fdopen(s, "w"); 356 *infile = fdopen(s, "r"); 357 358 return (EX_OK); 359 } 360 361 #endif DAEMON 362