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