2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation plus additions
11 that are listed in the file LICENSE.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Originally written by Kern Sibbald for inclusion in apcupsd,
30 * but heavily modified for Bacula
36 #include <netinet/in.h>
37 #include <sys/socket.h>
39 #include <arpa/inet.h>
41 #ifdef HAVE_ARPA_NAMESER_H
42 #include <arpa/nameser.h>
49 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
53 int allow_severity = LOG_NOTICE;
54 int deny_severity = LOG_WARNING;
57 static bool quit = false;
59 void bnet_stop_thread_server(pthread_t tid)
62 if (!pthread_equal(tid, pthread_self())) {
63 pthread_kill(tid, TIMEOUT_SIGNAL);
68 Become Threaded Network Server
69 This function is able to handle multiple server ips in
70 ipv4 and ipv6 style. The Addresse are give in a comma
71 seperated string in bind_addr
72 In the moment it is inpossible to bind different ports.
75 bnet_thread_server(dlist *addrs, int max_clients, workq_t *client_wq,
76 void *handle_client_request(void *bsock))
80 struct sockaddr cli_addr; /* client's address */
84 struct request_info request;
88 dlink link; /* this MUST be the first item */
95 char allbuf[256 * 10];
96 Dmsg1(100, "Addresses %s\n", build_addresses_str(addrs, allbuf, sizeof(allbuf)));
98 foreach_dlist(p, addrs) {
99 /* Allocate on stack frame -- no need to free */
100 fd_ptr = (s_sockfd *)alloca(sizeof(s_sockfd));
101 fd_ptr->port = p->get_port_net_order();
105 for (tlog= 60; (fd_ptr->fd=socket(p->get_family(), SOCK_STREAM, 0)) < 0; tlog -= 10) {
109 Emsg3(M_ABORT, 0, _("Cannot open stream socket. ERR=%s. Current %s All %s\n"),
111 p->build_address_str(curbuf, sizeof(curbuf)),
112 build_addresses_str(addrs, allbuf, sizeof(allbuf)));
119 if (setsockopt(fd_ptr->fd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon,
120 sizeof(turnon)) < 0) {
122 Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"),
126 int tmax = 30 * (60 / 5); /* wait 30 minutes max */
127 for (tlog = 0; bind(fd_ptr->fd, p->get_sockaddr(), p->get_sockaddr_len()) < 0; tlog -= 5) {
130 tlog = 2 * 60; /* Complain every 2 minutes */
131 Emsg2(M_WARNING, 0, _("Cannot bind port %d: ERR=%s: Retrying ...\n"),
132 ntohs(fd_ptr->port), be.bstrerror());
136 Emsg2(M_ABORT, 0, _("Cannot bind port %d: ERR=%s.\n"), ntohs(fd_ptr->port),
140 listen(fd_ptr->fd, 5); /* tell system we are ready */
141 sockfds.append(fd_ptr);
143 /* Start work queue thread */
144 if ((stat = workq_init(client_wq, max_clients, handle_client_request)) != 0) {
147 Emsg1(M_ABORT, 0, _("Could not init client queue: ERR=%s\n"), be.bstrerror());
150 * Wait for a connection from the client process.
153 unsigned int maxfd = 0;
156 foreach_dlist(fd_ptr, &sockfds) {
157 FD_SET((unsigned)fd_ptr->fd, &sockset);
158 maxfd = maxfd > (unsigned)fd_ptr->fd ? maxfd : fd_ptr->fd;
161 if ((stat = select(maxfd + 1, &sockset, NULL, NULL, NULL)) < 0) {
162 berrno be; /* capture errno */
163 if (errno == EINTR) {
167 foreach_dlist(fd_ptr, &sockfds) {
170 Emsg1(M_FATAL, 0, _("Error in select: %s\n"), be.bstrerror());
174 foreach_dlist(fd_ptr, &sockfds) {
175 if (FD_ISSET(fd_ptr->fd, &sockset)) {
176 /* Got a connection, now accept it. */
178 clilen = sizeof(cli_addr);
179 newsockfd = accept(fd_ptr->fd, &cli_addr, &clilen);
180 } while (newsockfd < 0 && errno == EINTR);
185 P(mutex); /* hosts_access is not thread safe */
186 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
188 if (!hosts_access(&request)) {
190 Jmsg2(NULL, M_SECURITY, 0,
191 _("Connection from %s:%d refused by hosts.access\n"),
192 sockaddr_to_ascii(&cli_addr, buf, sizeof(buf)),
193 sockaddr_get_port(&cli_addr));
201 * Receive notification when connection dies.
203 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon,
204 sizeof(turnon)) < 0) {
206 Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
210 /* see who client is. i.e. who connected to us. */
212 sockaddr_to_ascii(&cli_addr, buf, sizeof(buf));
215 bs = init_bsock(NULL, newsockfd, "client", buf, fd_ptr->port, &cli_addr);
217 Jmsg0(NULL, M_ABORT, 0, _("Could not create client BSOCK.\n"));
220 /* Queue client to be served */
221 if ((stat = workq_add(client_wq, (void *)bs, NULL, 0)) != 0) {
224 Jmsg1(NULL, M_ABORT, 0, _("Could not add job to client queue: ERR=%s\n"),
231 /* Stop work queue thread */
232 if ((stat = workq_destroy(client_wq)) != 0) {
235 Emsg1(M_FATAL, 0, _("Could not destroy client queue: ERR=%s\n"),
243 * Bind an address so that we may accept connections
246 BSOCK *bnet_bind(int port)
249 struct sockaddr_in serv_addr; /* our address */
256 for (tlog = 0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10) {
258 if (errno == EINTR || errno == EAGAIN) {
263 Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s\n"), be.bstrerror());
271 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
273 Emsg1(M_WARNING, 0, _("Cannot set SO_REUSEADDR on socket: %s\n"),
278 * Bind our local address so that the client can send to us.
280 bzero((char *)&serv_addr, sizeof(serv_addr));
281 serv_addr.sin_family = AF_INET;
282 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
283 serv_addr.sin_port = htons(port);
285 for (tlog = 0; bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0;
288 if (errno == EINTR || errno == EAGAIN) {
293 Emsg2(M_WARNING, 0, _("Cannot bind port %d: ERR=%s: retrying ...\n"), port,
298 listen(sockfd, 1); /* tell system we are ready */
299 return init_bsock(NULL, sockfd, _("Server socket"), _("client"), port,
304 * Accept a single connection
306 BSOCK *bnet_accept(BSOCK * bsock, char *who)
308 fd_set ready, sockset;
309 int newsockfd, stat, len;
311 struct sockaddr_in cli_addr; /* client's address */
316 struct request_info request;
320 * Wait for a connection from the client process.
323 FD_SET((unsigned)bsock->fd, &sockset);
327 * Wait for a connection from a client process.
330 if ((stat = select(bsock->fd + 1, &ready, NULL, NULL, NULL)) < 0) {
332 if (errno == EINTR || errno = EAGAIN) {
336 Emsg1(M_FATAL, 0, _("Error in select: %s\n"), be.bstrerror());
341 clilen = sizeof(cli_addr);
342 newsockfd = accept(bsock->fd, (struct sockaddr *)&cli_addr, &clilen);
343 } while (newsockfd < 0 && (errno == EINTR || errno = EAGAIN));
344 if (newsockfd >= 0) {
351 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
353 if (!hosts_access(&request)) {
355 Emsg2(M_SECURITY, 0, _("Connection from %s:%d refused by hosts.access\n"),
356 inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
364 * Receive notification when connection dies.
366 if (setsockopt(newsockfd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
368 Emsg1(M_WARNING, 0, _("Cannot set SO_KEEPALIVE on socket: %s\n"),
372 /* see who client is. I.e. who connected to us.
373 * return it in the input message buffer.
375 if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
376 pm_strcpy(&bsock->msg, caller);
380 bsock->msglen = strlen(bsock->msg);
384 Emsg2(M_FATAL, 0, _("Socket accept error for %s. ERR=%s\n"), who,
388 if (caller == NULL) {
389 caller = _("unknown");
391 len = strlen(caller) + strlen(who) + 3;
392 buf = (char *)malloc(len);
393 bstrncpy(buf, len, who);
394 bstrncat(buf, len, ": ");
395 bstrncat(buf, len, caller);
396 bs = init_bsock(NULL, newsockfd, _("client"), buf, bsock->port, &cli_addr);
398 return bs; /* return new BSOCK */