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