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