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