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