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