2 Copyright (C) 2000-2004 Kern Sibbald and John Walker
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.
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.
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,
21 * Originally written by Kern Sibbald for inclusion in apcupsd,
22 * but heavily modified for Bacula
29 #include <netinet/in.h>
30 #include <sys/socket.h>
31 #include <arpa/inet.h>
33 #ifdef HAVE_ARPA_NAMESER_H
34 #include <arpa/nameser.h>
41 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
45 int allow_severity = LOG_NOTICE;
46 int deny_severity = LOG_WARNING;
49 static bool quit = false;
52 bnet_stop_thread_server(pthread_t tid)
55 pthread_kill(tid, TIMEOUT_SIGNAL);
58 /* Become Threaded Network Server */
60 bnet_thread_server(char *bind_addr, int port, int max_clients, workq_t *client_wq,
61 void *handle_client_request(void *bsock))
63 int newsockfd, sockfd, stat;
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 */
72 struct request_info request;
78 for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
81 Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s. Retrying ...\n"), strerror(errno));
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));
94 * Bind our local address so that the client can send to us.
96 bind_ip.s_addr = htonl(INADDR_ANY);
97 if (bind_addr && bind_addr[0]) {
99 if (inet_pton(AF_INET, bind_addr, &bind_ip) <= 0) {
101 if (inet_aton(bind_addr, &bind_ip) <= 0) {
103 Emsg1(M_WARNING, 0, _("Invalid bind address: %s, using INADDR_ANY\n"),
105 bind_ip.s_addr = htonl(INADDR_ANY);
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);
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 ) {
116 tlog = 2*60; /* Complain every 2 minutes */
117 Emsg2(M_WARNING, 0, _("Cannot bind port %d: %s. Retrying ...\n"), port, strerror(errno));
121 Emsg2(M_ABORT, 0, _("Cannot bind port %d: %s.\n"), port, strerror(errno));
124 listen(sockfd, 5); /* tell system we are ready */
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));
132 * Wait for a connection from the client process.
137 FD_SET(sockfd, &sockset);
139 if ((stat = select(sockfd+1, &sockset, NULL, NULL, NULL)) < 0) {
140 if (errno == EINTR || errno == EAGAIN) {
145 Emsg1(M_FATAL, 0, _("Error in select: %s\n"), strerror(errno));
149 /* Got a connection, now accept it. */
151 clilen = sizeof(cli_addr);
152 newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
153 } while (newsockfd < 0 && (errno == EINTR || errno == EAGAIN));
160 P(mutex); /* hosts_access is not thread safe */
161 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
163 if (!hosts_access(&request)) {
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));
174 * Receive notification when connection dies.
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));
180 /* see who client is. i.e. who connected to us. */
182 caller = inet_ntoa(cli_addr.sin_addr); /* NOT thread safe, use mutex */
183 if (caller == NULL) {
184 caller = _("unknown client");
187 BSOCK *bs = init_bsock(NULL, newsockfd, "client", caller, port, &cli_addr);
189 Jmsg0(NULL, M_ABORT, 0, _("Could not create client BSOCK.\n"));
192 /* Queue client to be served */
193 if ((stat = workq_add(client_wq, (void *)bs, NULL, 0)) != 0) {
195 Jmsg1(NULL, M_ABORT, 0, _("Could not add job to client queue: ERR=%s\n"), strerror(stat));
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));
209 * Bind an address so that we may accept connections
216 struct sockaddr_in serv_addr; /* our address */
223 for (tlog=0; (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0; tlog -= 10 ) {
224 if (errno == EINTR || errno == EAGAIN) {
229 Emsg1(M_ERROR, 0, _("Cannot open stream socket: %s\n"), strerror(errno));
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));
242 * Bind our local address so that the client can send to us.
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);
249 for (tlog=0; bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0; tlog -= 5 ) {
250 if (errno == EINTR || errno == EAGAIN) {
255 Emsg2(M_WARNING, 0, _("Cannot bind port %d: %s: retrying ...\n"), port, strerror(errno));
259 listen(sockfd, 1); /* tell system we are ready */
260 return init_bsock(NULL, sockfd, _("Server socket"), _("client"), port, &serv_addr);
264 * Accept a single connection
267 bnet_accept(BSOCK *bsock, char *who)
269 fd_set ready, sockset;
270 int newsockfd, stat, len;
272 struct sockaddr_in cli_addr; /* client's address */
277 struct request_info request;
281 * Wait for a connection from the client process.
284 FD_SET(bsock->fd, &sockset);
288 * Wait for a connection from a client process.
291 if ((stat = select(bsock->fd+1, &ready, NULL, NULL, NULL)) < 0) {
292 if (errno == EINTR || errno = EAGAIN) {
296 Emsg1(M_FATAL, 0, _("Error in select: %s\n"), strerror(errno));
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) {
311 request_init(&request, RQ_DAEMON, my_name, RQ_FILE, newsockfd, 0);
313 if (!hosts_access(&request)) {
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));
324 * Receive notification when connection dies.
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));
330 /* see who client is. I.e. who connected to us.
331 * return it in the input message buffer.
333 if ((caller = inet_ntoa(cli_addr.sin_addr)) != NULL) {
334 pm_strcpy(&bsock->msg, caller);
338 bsock->msglen = strlen(bsock->msg);
341 Emsg2(M_FATAL, 0, _("Socket accept error for %s. ERR=%s\n"), who,
345 if (caller == NULL) {
348 len = strlen(caller) + strlen(who) + 3;
349 buf = (char *) malloc(len);
353 bs = init_bsock(NULL, newsockfd, "client", buf, bsock->port, &cli_addr);
355 return bs; /* return new BSOCK */