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