]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bnet_server.c
First cut AutoPrune
[bacula/bacula] / bacula / src / lib / bnet_server.c
1 /*
2    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of
7    the License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public
15    License along with this program; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17    MA 02111-1307, USA.
18
19  */
20  /* 
21   * Originally written by Kern Sibbald for inclusion in apcupsd,
22   *  but heavily modified for Bacula
23   *
24   *   Version $Id$
25   */
26
27 #include "bacula.h"
28 #include <netinet/in.h>
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
31 #include <netdb.h>
32
33 #ifdef HAVE_LIBWRAP
34 #include "tcpd.h"
35 int allow_severity = LOG_NOTICE;
36 int deny_severity = LOG_WARNING;
37 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
38 #endif
39
40 /* Become Threaded Network Server */
41 void
42 bnet_thread_server(int port, int max_clients, workq_t *client_wq, 
43                    void handle_client_request(void *bsock))
44 {
45    int newsockfd, sockfd, stat;
46    socklen_t clilen;
47    struct sockaddr_in cli_addr;       /* client's address */
48    struct sockaddr_in serv_addr;      /* our address */
49    int tlog;
50    fd_set ready, sockset;
51    int turnon = 1;
52    char *caller;
53 #ifdef HAVE_LIBWRAP
54    struct request_info request;
55 #endif
56
57    /*
58     * Open a TCP socket  
59     */
60    for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
61       if (tlog <= 0) {
62          tlog = 60; 
63          Emsg1(M_ERROR, 0, "Cannot open stream socket: %s. Retrying ...\n", strerror(errno));
64       }
65       sleep(10);
66    }
67
68    /*
69     * Reuse old sockets 
70     */
71    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
72       Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
73    }
74
75    /* 
76     * Bind our local address so that the client can send to us.
77     */
78    bzero((char *) &serv_addr, sizeof(serv_addr));
79    serv_addr.sin_family = AF_INET;
80    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
81    serv_addr.sin_port = htons(port);
82
83    int tmax = 30 * (60 / 5);          /* wait 30 minutes max */
84    for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
85       if (tlog <= 0) {
86          tlog = 2*60;                 /* Complain every 2 minutes */
87          Emsg2(M_WARNING, 0, "Cannot bind port %d: %s. Retrying ...\n", port, strerror(errno));
88       }
89       sleep(5);
90       if (--tmax <= 0) {
91          Emsg2(M_ABORT, 0, "Cannot bind port %d: %s.\n", port, strerror(errno));
92       }
93    }
94    listen(sockfd, 5);                 /* tell system we are ready */
95
96    FD_ZERO(&sockset);
97    FD_SET(sockfd, &sockset);
98
99    /* Start work queue thread */
100    if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
101       Emsg1(M_ABORT, 0, "Could not init client queue: ERR=%s\n", strerror(stat));
102    }
103
104    for (;;) {
105       /* 
106        * Wait for a connection from a client process.
107        */
108       ready = sockset;
109       if ((stat = select(sockfd+1, &ready, NULL, NULL, NULL)) < 0) {
110          if (errno == EINTR || errno == EAGAIN) {
111             errno = 0;
112             continue;
113          }
114          close(sockfd);
115          Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
116          break;
117       }
118       clilen = sizeof(cli_addr);
119       newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
120
121 #ifdef HAVE_LIBWRAP
122       P(mutex);                       /* hosts_access is not thread safe */
123       request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
124       fromhost(&request);
125       if (!hosts_access(&request)) {
126          V(mutex);
127          Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
128                inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
129          close(newsockfd);
130          continue;
131       }
132       V(mutex);
133 #endif
134
135       /*
136        * Receive notification when connection dies.
137        */
138       if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
139          Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
140       }
141
142       /* see who client is. i.e. who connected to us. */
143       caller = inet_ntoa(cli_addr.sin_addr);
144       if (caller == NULL) {
145          caller = "unknown client";
146       }
147
148       /* Queue client to be served */
149       if ((stat = workq_add(client_wq, 
150             (void *)init_bsock(newsockfd, "client", caller, port))) != 0) {
151          Emsg1(M_ABORT, 0, "Could not add job to client queue: ERR=%s\n", strerror(stat));
152       }
153    }
154 }   
155
156
157 #ifdef REALLY_USED   
158 /*
159  * Bind an address so that we may accept connections
160  * one at a time.
161  */
162 BSOCK *
163 bnet_bind(int port)
164 {
165    int sockfd;
166    struct sockaddr_in serv_addr;      /* our address */
167    int tlog;
168    int turnon = 1;
169
170    /*
171     * Open a TCP socket  
172     */
173    for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
174       if (tlog <= 0) {
175          tlog = 2*60; 
176          Emsg1(M_ERROR, 0, "Cannot open stream socket: %s\n", strerror(errno));
177       }
178       sleep(60);
179    }
180
181    /*
182     * Reuse old sockets 
183     */
184    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
185       Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
186    }
187
188    /* 
189     * Bind our local address so that the client can send to us.
190     */
191    bzero((char *) &serv_addr, sizeof(serv_addr));
192    serv_addr.sin_family = AF_INET;
193    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
194    serv_addr.sin_port = htons(port);
195
196    for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
197       if (tlog <= 0) {
198          tlog = 2*60;
199          Emsg2(M_WARNING, 0, "Cannot bind port %d: %s: retrying ...\n", port, strerror(errno));
200       }
201       sleep(5);
202    }
203    listen(sockfd, 1);                 /* tell system we are ready */
204    return init_bsock(sockfd, "Server socket", "client", port);
205 }
206
207 /*
208  * Accept a single connection 
209  */
210 BSOCK *
211 bnet_accept(BSOCK *bsock, char *who)
212 {
213    fd_set ready, sockset;
214    int newsockfd, stat, len;
215    socklen_t clilen;
216    struct sockaddr_in cli_addr;       /* client's address */
217    char *caller, *buf;
218    BSOCK *bs;
219    int turnon = 1;
220 #ifdef HAVE_LIBWRAP
221    struct request_info request;
222 #endif
223
224    /* 
225     * Wait for a connection from the client process.
226     */
227    FD_ZERO(&sockset);
228    FD_SET(bsock->fd, &sockset);
229
230    for (;;) {
231       /* 
232        * Wait for a connection from a client process.
233        */
234       ready = sockset;
235       if ((stat = select(bsock->fd+1, &ready, NULL, NULL, NULL)) < 0) {
236          if (errno == EINTR || errno == EAGAIN) {
237             errno = 0;
238             continue;
239          }
240          Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
241          newsockfd = -1;
242          break;
243       }
244       clilen = sizeof(cli_addr);
245       newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
246       break;
247    }
248
249 #ifdef HAVE_LIBWRAP
250    P(mutex);
251    request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
252    fromhost(&request);
253    if (!hosts_access(&request)) {
254       V(mutex);
255       Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
256             inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
257       close(newsockfd);
258       return NULL;
259    }
260    V(mutex);
261 #endif
262
263    /*
264     * Receive notification when connection dies.
265     */
266    if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
267       Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
268    }
269
270    /* see who client is. I.e. who connected to us.
271     * return it in the input message buffer.
272     */
273    if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
274       strcpy(bsock->msg, caller);
275    } else {
276       bsock->msg[0] = 0;
277    }
278    bsock->msglen = strlen(bsock->msg);
279
280    if (newsockfd < 0) {
281       Emsg2(M_FATAL, 0, "Socket accept error for %s. ERR=%s\n", who,
282             strerror(errno));
283       return NULL;
284    } else {
285       if (caller == NULL) {
286          caller = "unknown";
287       }
288       len = strlen(caller) + strlen(who) + 3;
289       buf = (char *) malloc(len);
290       strcpy(buf, who);
291       strcat(buf, ": ");
292       strcat(buf, caller);
293       bs = init_bsock(newsockfd, "client", buf, bsock->port);
294       free(buf);
295       return bs;                      /* return new BSOCK */
296    }
297 }   
298
299 #endif
300
301
302
303 /*
304  * The following code will soon be deleted, don't attempt
305  * to use it.
306  */
307 #ifdef dont_have_threads
308
309 /*
310  * This routine is called by the main process to
311  * track its children. On CYGWIN, the child processes
312  * don't always exit when the other end of the socket
313  * hangs up. Thus they remain hung on a read(). After
314  * 30 seconds, we send them a SIGTERM signal, which 
315  * causes them to wake up to the reality of the situation.
316  *
317  * Also, on certain Unix systems such as SunOS, we must
318  * explicitly do the wait, otherwise, the child that dies
319  * remains a zombie potentially remaining bound to the
320  * port.
321  */
322
323 #ifdef HAVE_SUN_OS
324
325 static void reap_children(int childpid)
326 {
327    static int pids[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
328    static int times[10];
329    int i;
330    time_t now;
331
332    time(&now);
333    for (i=0; i<10; i++) {
334       if (pids[i] && (waitpid(pids[i], NULL, WNOHANG) == pids[i])) {
335          pids[i] = 0;
336       } else {
337          if (pids[i] && ((now - times[i]) > 30))
338             kill(pids[i], SIGTERM);
339       }
340    }
341    for (i=0; i<10; i++) {
342       if (pids[i] && (waitpid(pids[i], NULL, WNOHANG) == pids[i])) {
343          pids[i] = 0;
344       }
345       if (childpid && (pids[i] == 0)) {
346          pids[i] = childpid;
347          times[i] = now;
348          childpid = 0;
349       }
350    }
351 }
352
353 #endif /* HAVE_SUN_OS */
354
355 /* Become network server */
356 void
357 bnet_server(int port, void handle_client_request(BSOCK *bsock))
358 {
359    int newsockfd, sockfd, clilen, childpid, stat;
360    struct sockaddr_in cli_addr;       /* client's address */
361    struct sockaddr_in serv_addr;      /* our address */
362    int tlog;
363    fd_set ready, sockset;
364    int turnon = 1;
365    char *caller;
366 #ifdef HAVE_LIBWRAP
367    struct request_info request;
368 #endif
369
370    /* **** FIXME **** handle BSD too */
371    signal(SIGCHLD, SIG_IGN);
372    
373    /*
374     * Open a TCP socket  
375     */
376    for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
377       if (tlog <= 0) {
378          tlog = 60; 
379          Emsg1(M_ERROR, 0, "Cannot open stream socket: %s. Retrying ...\n", strerror(errno));
380       }
381       sleep(10);
382    }
383
384    /*
385     * Reuse old sockets 
386     */
387    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &turnon, sizeof(turnon)) < 0) {
388       Emsg1(M_WARNING, 0, "Cannot set SO_REUSEADDR on socket: %s\n" , strerror(errno));
389    }
390
391    /*
392     * Receive notification when connection dies.
393     */
394    if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
395       Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
396    }
397
398    /* 
399     * Bind our local address so that the client can send to us.
400     */
401    bzero((char *) &serv_addr, sizeof(serv_addr));
402    serv_addr.sin_family = AF_INET;
403    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
404    serv_addr.sin_port = htons(port);
405
406    for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
407       if (tlog <= 0) {
408          tlog = 2*60;                 /* Complain every 2 minutes */
409          Emsg2(M_WARNING, 0, "Cannot bind port %d: %s. Retrying ...\n", port, strerror(errno));
410       }
411       sleep(5);
412    }
413    listen(sockfd, 5);                 /* tell system we are ready */
414
415    FD_ZERO(&sockset);
416    FD_SET(sockfd, &sockset);
417
418    for (;;) {
419       /* 
420        * Wait for a connection from a client process.
421        */
422       ready = sockset;
423       if ((stat = select(sockfd+1, &ready, NULL, NULL, NULL)) < 0) {
424          if (errno == EINTR || errno == EAGAIN) {
425             errno = 0;
426             continue;
427          }
428          close(sockfd);
429          Emsg1(M_FATAL, 0, "Error in select: %s\n", strerror(errno));
430          break;
431       }
432       clilen = sizeof(cli_addr);
433       newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
434
435 #ifdef HAVE_LIBWRAP
436       request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
437       fromhost(&request);
438       if (!hosts_access(&request)) {
439          Emsg2(M_WARNING, 0, "Connection from %s:%d refused by hosts.access",
440                inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
441          close(newsockfd);
442          continue;
443       }
444 #endif
445
446       /*
447        * Receive notification when connection dies.
448        */
449       if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, &turnon, sizeof(turnon)) < 0) {
450          Emsg1(M_WARNING, 0, "Cannot set SO_KEEPALIVE on socket: %s\n" , strerror(errno));
451       }
452
453
454       /* see who client is. i.e. who connected to us. */
455       caller = inet_ntoa(cli_addr.sin_addr);
456       if (caller == NULL) {
457          caller = "client";
458       }
459
460 #ifdef HAVE_CYGWIN
461       childpid = 0;
462       handle_client_request(init_bsock(newsockfd, "client", caller, port));
463 #else
464       /* fork to provide the response */
465       for (tlog=0; (childpid = fork()) < 0; tlog -= 5*60 ) {
466          if (tlog <= 0) {
467             tlog = 60*60; 
468             Emsg1(M_FATAL, 0, "Fork error: %s\n", strerror(errno));
469          }
470          sleep(5*60);
471       } 
472       if (childpid == 0) {      /* child process */
473          close(sockfd);              /* close original socket */
474          handle_client_request(init_bsock(newsockfd, "client", caller, port));    /* process the request */
475          Dmsg0(99, "back from handle request\n");
476          close(newsockfd);
477          exit(0);
478       }
479 #endif
480
481       close(newsockfd);              /* parent process */
482 #ifdef HAVE_SUN_OS
483       reap_children(childpid);
484 #endif /* HAVE_SUN_OS */
485     }
486 }   
487
488 #endif /* no threads -- not supported any more sorry! */