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