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