]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bnet_server.c
Final changes
[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 #undef DEV_BSIZE
29 #include <netinet/in.h>
30 #include <sys/socket.h>
31 #include <arpa/inet.h>
32 #include <netdb.h>
33 #ifdef HAVE_ARPA_NAMESER_H
34 #include <arpa/nameser.h>
35 #endif
36 #ifdef HAVE_RESOLV_H
37 #include <resolv.h>
38 #endif
39
40
41 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
42
43 #ifdef HAVE_LIBWRAP
44 #include "tcpd.h"
45 int allow_severity = LOG_NOTICE;
46 int deny_severity = LOG_WARNING;
47 #endif
48
49 /* Become Threaded Network Server */
50 void
51 bnet_thread_server(char *bind_addr, int port, int max_clients, workq_t *client_wq, 
52                    void *handle_client_request(void *bsock))
53 {
54    int newsockfd, sockfd, stat;
55    socklen_t clilen;
56    struct sockaddr_in cli_addr;       /* client's address */
57    struct sockaddr_in serv_addr;      /* our address */
58    struct in_addr bind_ip;            /* address to bind to */
59    int tlog;
60    fd_set ready, sockset;
61    int turnon = 1;
62    char *caller;
63 #ifdef HAVE_LIBWRAP
64    struct request_info request;
65 #endif
66
67    /*
68     * Open a TCP socket  
69     */
70    for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
71       if (tlog <= 0) {
72          tlog = 60; 
73          Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s. Retrying ...\n"), strerror(errno));
74       }
75       bmicrosleep(10, 0);
76    }
77
78    /*
79     * Reuse old sockets 
80     */
81    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
82       Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"), strerror(errno));
83    }
84
85    /* 
86     * Bind our local address so that the client can send to us.
87     */
88    bind_ip.s_addr = htonl(INADDR_ANY);
89    if (bind_addr && bind_addr[0]) {
90 #ifdef HAVE_INET_PTON
91       if (inet_pton(AF_INET, bind_addr, &bind_ip) <= 0) {
92 #else
93       if (inet_aton(bind_addr, &bind_ip) <= 0) {
94 #endif
95          Emsg1(M_WARNING, 0, _("Invalid bind address: %s, using INADDR_ANY\n"),
96             bind_addr);
97          bind_ip.s_addr = htonl(INADDR_ANY);
98       }
99    }
100    memset((char *) &serv_addr, 0, sizeof(serv_addr));
101    serv_addr.sin_family = AF_INET;
102    serv_addr.sin_addr.s_addr = bind_ip.s_addr;
103    serv_addr.sin_port = htons(port);
104
105    int tmax = 30 * (60 / 5);          /* wait 30 minutes max */
106    for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
107       if (tlog <= 0) {
108          tlog = 2*60;                 /* Complain every 2 minutes */
109          Emsg2(M_WARNING, 0, _("Cannot bind port %d: %s. Retrying ...\n"), port, strerror(errno));
110       }
111       bmicrosleep(5, 0);
112       if (--tmax <= 0) {
113          Emsg2(M_ABORT, 0, _("Cannot bind port %d: %s.\n"), port, strerror(errno));
114       }
115    }
116    listen(sockfd, 5);                 /* tell system we are ready */
117
118    FD_ZERO(&sockset);
119    FD_SET(sockfd, &sockset);
120
121    /* Start work queue thread */
122    if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
123       Emsg1(M_ABORT, 0, _("Could not init client queue: ERR=%s\n"), strerror(stat));
124    }
125
126    for (;;) {
127       /* 
128        * Wait for a connection from a client process.
129        */
130       ready = sockset;
131       do {
132          errno = 0;
133          stat = select(sockfd+1, &ready, NULL, NULL, NULL);       
134       } while(stat == -1 && (errno == EINTR || errno == EAGAIN));
135       if (stat < 0) {
136          close(sockfd);
137          Emsg1(M_FATAL, 0, _("Error in select: %s\n"), strerror(errno));
138          break;
139       }
140       do {
141          clilen = sizeof(cli_addr);
142          errno = 0;
143          newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
144       } while (newsockfd < 0 && (errno == EINTR || errno == EAGAIN));
145       if (newsockfd < 0) {
146          continue;
147       }
148
149 #ifdef HAVE_LIBWRAP
150       P(mutex);                       /* hosts_access is not thread safe */
151       request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
152       fromhost(&request);
153       if (!hosts_access(&request)) {
154          V(mutex);
155          Jmsg2(NULL, M_WARNING, 0, _("Connection from %s:%d refused by hosts.access"),
156                inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
157          close(newsockfd);
158          continue;
159       }
160       V(mutex);
161 #endif
162
163       /*
164        * Receive notification when connection dies.
165        */
166       if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
167          Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n") , strerror(errno));
168       }
169
170       /* see who client is. i.e. who connected to us. */
171       P(mutex);
172       caller = inet_ntoa(cli_addr.sin_addr);  /* NOT thread safe, use mutex */
173       if (caller == NULL) {
174          caller = "unknown client";
175       }
176
177       BSOCK *bs = init_bsock(NULL, newsockfd, "client", caller, port);
178       if (bs == NULL) {
179          Jmsg0(NULL, M_ABORT, 0, _("Could not create client BSOCK.\n"));
180       }
181
182       /* Queue client to be served */
183       if ((stat = workq_add(client_wq, (void *)bs, NULL, 0)) != 0) {
184          V(mutex);
185          Jmsg1(NULL, M_ABORT, 0, _("Could not add job to client queue: ERR=%s\n"), strerror(stat));
186       }
187       V(mutex);
188    }
189 }   
190
191
192 #ifdef REALLY_USED   
193 /*
194  * Bind an address so that we may accept connections
195  * one at a time.
196  */
197 BSOCK *
198 bnet_bind(int port)
199 {
200    int sockfd;
201    struct sockaddr_in serv_addr;      /* our address */
202    int tlog;
203    int turnon = 1;
204
205    /*
206     * Open a TCP socket  
207     */
208    for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
209       if (errno == EINTR) {
210          continue;
211       }
212       if (tlog <= 0) {
213          tlog = 2*60; 
214          Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s\n"), strerror(errno));
215       }
216       bmicrosleep(60, 0);
217    }
218
219    /*
220     * Reuse old sockets 
221     */
222    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
223       Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n") , strerror(errno));
224    }
225
226    /* 
227     * Bind our local address so that the client can send to us.
228     */
229    bzero((char *) &serv_addr, sizeof(serv_addr));
230    serv_addr.sin_family = AF_INET;
231    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
232    serv_addr.sin_port = htons(port);
233
234    for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
235       if (errno == EINTR) {
236          continue;
237       }
238       if (tlog <= 0) {
239          tlog = 2*60;
240          Emsg2(M_WARNING, 0, _("Cannot bind port %d: %s: retrying ...\n"), port, strerror(errno));
241       }
242       bmicrosleep(5, 0);
243    }
244    listen(sockfd, 1);                 /* tell system we are ready */
245    return init_bsock(NULL, sockfd, _("Server socket"), _("client"), port);
246 }
247
248 /*
249  * Accept a single connection 
250  */
251 BSOCK *
252 bnet_accept(BSOCK *bsock, char *who)
253 {
254    fd_set ready, sockset;
255    int newsockfd, stat, len;
256    socklen_t clilen;
257    struct sockaddr_in cli_addr;       /* client's address */
258    char *caller, *buf;
259    BSOCK *bs;
260    int turnon = 1;
261 #ifdef HAVE_LIBWRAP
262    struct request_info request;
263 #endif
264
265    /* 
266     * Wait for a connection from the client process.
267     */
268    FD_ZERO(&sockset);
269    FD_SET(bsock->fd, &sockset);
270
271    for (;;) {
272       /* 
273        * Wait for a connection from a client process.
274        */
275       ready = sockset;
276       if ((stat = select(bsock->fd+1, &ready, NULL, NULL, NULL)) < 0) {
277          if (errno == EINTR || errno == EAGAIN) {
278             errno = 0;
279             continue;
280          }
281          Emsg1(M_FATAL, 0, _("Error in select: %s\n"), strerror(errno));
282          newsockfd = -1;
283          break;
284       }
285       do {
286          clilen = sizeof(cli_addr);
287          newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
288       } while (newsockfd < 0 && errno == EINTR);
289       if (newsockfd >= 0) {
290          break;
291       }
292    }
293
294 #ifdef HAVE_LIBWRAP
295    P(mutex);
296    request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
297    fromhost(&request);
298    if (!hosts_access(&request)) {
299       V(mutex);
300       Emsg2(M_WARNING, 0, _("Connection from %s:%d refused by hosts.access"),
301             inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
302       close(newsockfd);
303       return NULL;
304    }
305    V(mutex);
306 #endif
307
308    /*
309     * Receive notification when connection dies.
310     */
311    if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
312       Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"), strerror(errno));
313    }
314
315    /* see who client is. I.e. who connected to us.
316     * return it in the input message buffer.
317     */
318    if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
319       pm_strcpy(&bsock->msg, caller);
320    } else {
321       bsock->msg[0] = 0;
322    }
323    bsock->msglen = strlen(bsock->msg);
324
325    if (newsockfd < 0) {
326       Emsg2(M_FATAL, 0, _("Socket accept error for %s. ERR=%s\n"), who,
327             strerror(errno));
328       return NULL;
329    } else {
330       if (caller == NULL) {
331          caller = "unknown";
332       }
333       len = strlen(caller) + strlen(who) + 3;
334       buf = (char *) malloc(len);
335       strcpy(buf, who);
336       strcat(buf, ": ");
337       strcat(buf, caller);
338       bs = init_bsock(NULL, newsockfd, "client", buf, bsock->port);
339       free(buf);
340       return bs;                      /* return new BSOCK */
341    }
342 }   
343
344 #endif