]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/filed.c
688a3e4ce568d1ad8b5b33c9a05f995f3cace6db
[bacula/bacula] / bacula / src / filed / filed.c
1 /*
2  *  Bacula File Daemon
3  *
4  *    Kern Sibbald, March MM
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2005 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "filed.h"
31
32 /* Imported Functions */
33 extern void *handle_client_request(void *dir_sock);
34
35 /* Imported Variables */
36 extern time_t watchdog_sleep_time;
37
38 /* Forward referenced functions */
39 void terminate_filed(int sig);
40 static int check_resources();
41
42 /* Exported variables */
43 CLIENT *me;                           /* my resource */
44 char OK_msg[]   = "2000 OK\n";
45 char TERM_msg[] = "2999 Terminate\n";
46 bool no_signals = false;
47
48 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
49 const int win32_client = 1;
50 #else
51 const int win32_client = 0;
52 #endif
53
54
55 #define CONFIG_FILE "./bacula-fd.conf" /* default config file */
56
57 static char *configfile = NULL;
58 static bool foreground = false;
59 static bool inetd_request = false;
60 static workq_t dir_workq;             /* queue of work from Director */
61 static pthread_t server_tid;
62
63
64 static void usage()
65 {
66    Pmsg0(-1, _(
67 "Copyright (C) 2000-2005 Kern Sibbald\n"
68 "\nVersion: " VERSION " (" BDATE ")\n\n"
69 "Usage: bacula-fd [-f -s] [-c config_file] [-d debug_level]\n"
70 "        -c <file>   use <file> as configuration file\n"
71 "        -dnn        set debug level to nn\n"
72 "        -f          run in foreground (for debugging)\n"
73 "        -g          groupid\n"
74 "        -i          inetd request\n"
75 "        -s          no signals (for debugging)\n"
76 "        -t          test configuration file and exit\n"
77 "        -u          userid\n"
78 "        -v          verbose user messages\n"
79 "        -?          print this message.\n"
80 "\n"));
81    exit(1);
82 }
83
84
85 /*********************************************************************
86  *
87  *  Main Bacula Unix Client Program
88  *
89  */
90 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
91 #define main BaculaMain
92 #endif
93
94 int main (int argc, char *argv[])
95 {
96    int ch;
97    bool test_config = false;
98    char *uid = NULL;
99    char *gid = NULL;
100
101    init_stack_dump();
102    my_name_is(argc, argv, "bacula-fd");
103    textdomain("bacula");
104    init_msg(NULL, NULL);
105    daemon_start_time = time(NULL);
106
107    while ((ch = getopt(argc, argv, "c:d:fg:istu:v?")) != -1) {
108       switch (ch) {
109       case 'c':                    /* configuration file */
110          if (configfile != NULL) {
111             free(configfile);
112          }
113          configfile = bstrdup(optarg);
114          break;
115
116       case 'd':                    /* debug level */
117          debug_level = atoi(optarg);
118          if (debug_level <= 0) {
119             debug_level = 1;
120          }
121          break;
122
123       case 'f':                    /* run in foreground */
124          foreground = true;
125          break;
126
127       case 'g':                    /* set group */
128          gid = optarg;
129          break;
130
131       case 'i':
132          inetd_request = true;
133          break;
134       case 's':
135          no_signals = true;
136          break;
137
138       case 't':
139          test_config = true;
140          break;
141
142       case 'u':                    /* set userid */
143          uid = optarg;
144          break;
145
146       case 'v':                    /* verbose */
147          verbose++;
148          break;
149
150       case '?':
151       default:
152          usage();
153
154       }
155    }
156    argc -= optind;
157    argv += optind;
158
159    if (argc) {
160       if (configfile != NULL)
161          free(configfile);
162       configfile = bstrdup(*argv);
163       argc--;
164       argv++;
165    }
166    if (argc) {
167       usage();
168    }
169
170    server_tid = pthread_self();
171    if (!no_signals) {
172       init_signals(terminate_filed);
173    } else {
174       /* This reduces the number of signals facilitating debugging */
175       watchdog_sleep_time = 120;      /* long timeout for debugging */
176    }
177
178    if (configfile == NULL) {
179       configfile = bstrdup(CONFIG_FILE);
180    }
181
182    parse_config(configfile);
183
184    if (init_tls() != 0) {
185       Emsg0(M_ERROR, 0, _("TLS library initialization failed.\n"));
186       terminate_filed(1);
187    }
188
189    if (!check_resources()) {
190       Emsg1(M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
191       terminate_filed(1);
192    }
193
194    set_working_directory(me->working_directory);
195
196    if (test_config) {
197       terminate_filed(0);
198    }
199
200    if (!foreground &&!inetd_request) {
201       daemon_start();
202       init_stack_dump();              /* set new pid */
203    }
204
205    /* Maximum 1 daemon at a time */
206    create_pid_file(me->pid_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
207    read_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
208
209    drop(uid, gid);
210
211 #ifdef BOMB
212    me += 1000000;
213 #endif
214
215    init_python_interpreter(me->hdr.name, me->scripts_directory, "FDStartUp");
216
217    set_thread_concurrency(10);
218
219    if (!no_signals) {
220       start_watchdog();               /* start watchdog thread */
221       init_jcr_subsystem();           /* start JCR watchdogs etc. */
222    }
223    server_tid = pthread_self();
224
225    if (inetd_request) {
226       /* Socket is on fd 0 */
227       struct sockaddr client_addr;
228       int port = -1;
229       socklen_t client_addr_len = sizeof(client_addr);
230       if (getsockname(0, &client_addr, &client_addr_len) == 0) {
231                 /* MA BUG 6 remove ifdefs */
232                 port = sockaddr_get_port_net_order(&client_addr);
233       }
234       BSOCK *bs = init_bsock(NULL, 0, "client", "unknown client", port, &client_addr);
235       handle_client_request((void *)bs);
236    } else {
237       /* Become server, and handle requests */
238       IPADDR *p;
239       foreach_dlist(p, me->FDaddrs) {
240          Dmsg1(10, "filed: listening on port %d\n", p->get_port_host_order());
241       }
242       bnet_thread_server(me->FDaddrs, me->MaxConcurrentJobs, &dir_workq, handle_client_request);
243    }
244
245    terminate_filed(0);
246    exit(0);                           /* should never get here */
247 }
248
249 void terminate_filed(int sig)
250 {
251    bnet_stop_thread_server(server_tid);
252    generate_daemon_event(NULL, "Exit");
253    write_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
254    delete_pid_file(me->pid_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
255
256    if (configfile != NULL) {
257       free(configfile);
258    }
259    if (debug_level > 5) {
260       print_memory_pool_stats();
261    }
262    free_config_resources();
263    term_msg();
264    stop_watchdog();
265 #ifdef HAVE_TLS
266    cleanup_tls();
267 #endif
268    close_memory_pool();               /* release free memory in pool */
269    sm_dump(false);                    /* dump orphaned buffers */
270    exit(sig);
271 }
272
273 /*
274 * Make a quick check to see that we have all the
275 * resources needed.
276 */
277 static int check_resources()
278 {
279    bool OK = true;
280    DIRRES *director;
281
282    LockRes();
283
284    me = (CLIENT *)GetNextRes(R_CLIENT, NULL);
285    if (!me) {
286       Emsg1(M_FATAL, 0, _("No File daemon resource defined in %s\n"
287             "Without that I don't know who I am :-(\n"), configfile);
288       OK = false;
289    } else {
290       if (GetNextRes(R_CLIENT, (RES *) me) != NULL) {
291          Emsg1(M_FATAL, 0, _("Only one Client resource permitted in %s\n"),
292               configfile);
293          OK = false;
294       }
295       my_name_is(0, NULL, me->hdr.name);
296       if (!me->messages) {
297          me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
298          if (!me->messages) {
299              Emsg1(M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
300              OK = false;
301          }
302       }
303 #ifdef HAVE_TLS
304       /* tls_require implies tls_enable */
305       if (me->tls_require) {
306          me->tls_enable = true;
307       }
308
309       if ((!me->tls_ca_certfile && !me->tls_ca_certdir) && me->tls_enable) {
310          Emsg1(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
311             " or \"TLS CA Certificate Dir\" are defined for File daemon in %s.\n"),
312                             configfile);
313         OK = false;
314       }
315
316       /* If everything is well, attempt to initialize our per-resource TLS context */
317       if (OK && (me->tls_enable || me->tls_require)) {
318          /* Initialize TLS context:
319           * Args: CA certfile, CA certdir, Certfile, Keyfile,
320           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
321          me->tls_ctx = new_tls_context(me->tls_ca_certfile,
322             me->tls_ca_certdir, me->tls_certfile, me->tls_keyfile,
323             NULL, NULL, NULL, true);
324
325          if (!me->tls_ctx) { 
326             Emsg2(M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
327                                 me->hdr.name, configfile);
328             OK = false;
329          }
330       }
331
332 #endif /* HAVE_TLS */
333    }
334
335
336    /* Verify that a director record exists */
337    LockRes();
338    director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
339    UnlockRes();
340    if (!director) {
341       Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"),
342             configfile);
343       OK = false;
344    }
345
346 #ifdef HAVE_TLS
347    foreach_res(director, R_DIRECTOR) { 
348       /* tls_require implies tls_enable */
349       if (director->tls_require) {
350          director->tls_enable = true;
351       }
352
353       if (!director->tls_certfile && director->tls_enable) {
354          Emsg2(M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
355                director->hdr.name, configfile);
356          OK = false;
357       }
358
359       if (!director->tls_keyfile && director->tls_enable) {
360          Emsg2(M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
361                director->hdr.name, configfile);
362          OK = false;
363       }
364
365       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable && director->tls_verify_peer) {
366          Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
367                              " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
368                              " At least one CA certificate store is required"
369                              " when using \"TLS Verify Peer\".\n"),
370                              director->hdr.name, configfile);
371          OK = false;
372       }
373
374       /* If everything is well, attempt to initialize our per-resource TLS context */
375       if (OK && (director->tls_enable || director->tls_require)) {
376          /* Initialize TLS context:
377           * Args: CA certfile, CA certdir, Certfile, Keyfile,
378           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
379          director->tls_ctx = new_tls_context(director->tls_ca_certfile,
380             director->tls_ca_certdir, director->tls_certfile,
381             director->tls_keyfile, NULL, NULL, director->tls_dhfile,
382             director->tls_verify_peer);
383
384          if (!director->tls_ctx) { 
385             Emsg2(M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
386                                 director->hdr.name, configfile);
387             OK = false;
388          }
389       }
390    }
391 #endif /* HAVE_TLS */
392
393    UnlockRes();
394
395    if (OK) {
396       close_msg(NULL);                /* close temp message handler */
397       init_msg(NULL, me->messages);   /* open user specified message handler */
398    }
399
400    return OK;
401 }
402