Merge pull request #50 from painless-security/jennifer/refactoring_tids
[trust_router.git] / tr / tr_main.c
1 /*
2  * Copyright (c) 2012, 2015, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <argp.h>
38 #include <event2/event.h>
39 #include <talloc.h>
40 #include <signal.h>
41
42 #include <tid_internal.h>
43 #include <mon_internal.h>
44 #include <tr_mon.h>
45 #include <tr_tid.h>
46 #include <tr_trp.h>
47 #include <tr_config.h>
48 #include <tr_event.h>
49 #include <tr_cfgwatch.h>
50 #include <tr.h>
51 #include <tr_debug.h>
52
53 #define TALLOC_DEBUG_ENABLE 1
54
55 /***** command-line option handling / setup *****/
56
57 static void print_version_info(void)
58 {
59   printf("Moonshot Trust Router %s\n\n", PACKAGE_VERSION);
60 }
61
62 /* Strip trailing / from a path name.*/
63 static void remove_trailing_slash(char *s) {
64   size_t n;
65
66   n=strlen(s);
67   if(s[n-1]=='/') {
68     s[n-1]='\0';
69   }
70 }
71
72 /* argp global parameters */
73 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
74
75 /* doc strings */
76 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router " PACKAGE_VERSION;
77 static const char arg_doc[]=""; /* string describing arguments, if any */
78
79 /* define the options here. Fields are:
80  * { long-name, short-name, variable name, options, help description } */
81 static const struct argp_option cmdline_options[] = {
82     { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
83     { "config-validate", 'C', NULL, 0, "Validate configuration files and exit"},
84     { "version", 1, NULL, 0, "Print version information and exit"},
85     { NULL }
86 };
87
88 /* structure for communicating with option parser */
89 struct cmdline_args {
90     int version_requested;
91     int validate_config_and_exit;
92     char *config_dir;
93 };
94
95 /* parser for individual options - fills in a struct cmdline_args */
96 static error_t parse_option(int key, char *arg, struct argp_state *state)
97 {
98   /* get a shorthand to the command line argument structure, part of state */
99   struct cmdline_args *arguments=state->input;
100
101   switch (key) {
102     case 'c':
103       if (arg == NULL) {
104         /* somehow we got called without an argument */
105         return ARGP_ERR_UNKNOWN;
106       }
107       arguments->config_dir=arg;
108       break;
109
110     case 1:
111       arguments->version_requested=1;
112       break;
113
114     case 'C':
115       arguments->validate_config_and_exit=1;
116       break;
117
118     default:
119       return ARGP_ERR_UNKNOWN;
120   }
121
122   return 0; /* success */
123 }
124
125 /* assemble the argp parser */
126 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
127
128
129 /***** talloc error handling *****/
130 /* called when talloc tries to abort */
131 static void tr_abort(const char *reason)
132 {
133   tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
134   abort();
135 }
136
137 #if TALLOC_DEBUG_ENABLE
138 static void tr_talloc_log(const char *msg)
139 {
140   tr_debug("talloc: %s", msg);
141 }
142 #endif /* TALLOC_DEBUG_ENABLE */
143
144 static void configure_signals(void)
145 {
146   sigset_t signals;
147   /* ignore SIGPIPE */
148   sigemptyset(&signals);
149   sigaddset(&signals, SIGPIPE);
150   pthread_sigmask(SIG_BLOCK, &signals, NULL);
151 }
152
153 int main(int argc, char *argv[])
154 {
155   TALLOC_CTX *main_ctx=NULL;
156
157   TR_INSTANCE *tr = NULL;
158   struct cmdline_args opts;
159   struct event_base *ev_base;
160   struct tr_socket_event tids_ev = {0};
161   struct tr_socket_event mon_ev = {0};
162   struct event *cfgwatch_ev;
163
164   configure_signals();
165
166   /* we're going to be multithreaded, so disable null context tracking */
167   talloc_set_abort_fn(tr_abort);
168   talloc_disable_null_tracking();
169 #if TALLOC_DEBUG_ENABLE
170   talloc_set_log_fn(tr_talloc_log);
171 #endif /* TALLOC_DEBUG_ENABLE */
172   main_ctx=talloc_new(NULL);
173
174   /* Use standalone logging */
175   tr_log_open();
176
177   /***** parse command-line arguments *****/
178   /* set defaults */
179   opts.version_requested=0;
180   opts.validate_config_and_exit=0;
181   opts.config_dir=".";
182
183   /* parse the command line*/
184   argp_parse(&argp, argc, argv, 0, 0, &opts);
185
186   /* process options */
187   remove_trailing_slash(opts.config_dir);
188
189
190   /***** Print version info *****/
191   print_version_info();
192   if (opts.version_requested)
193     return 0; /* requested that we print version and exit */
194
195   /***** create a Trust Router instance *****/
196   if (NULL == (tr = tr_create(main_ctx))) {
197     tr_crit("Unable to create Trust Router instance, exiting.");
198     return 1;
199   }
200
201   /***** initialize the trust path query server instance *****/
202   if (NULL == (tr->tids = tids_new(tr))) {
203     tr_crit("Error initializing Trust Path Query Server instance.");
204     return 1;
205   }
206
207   /***** initialize the trust router protocol server instance *****/
208   if (NULL == (tr->trps = trps_new(tr))) {
209     tr_crit("Error initializing Trust Router Protocol Server instance.");
210     return 1;
211   }
212
213   /***** initialize the monitoring interface instance *****/
214   if (NULL == (tr->mons = mons_new(tr))) {
215     tr_crit("Error initializing monitoring interface instance.");
216     return 1;
217   }
218   /* Monitor our tids/trps instances */
219   tr->mons->tids = tr->tids;
220   tr->mons->trps = tr->trps;
221
222   /***** process configuration *****/
223   tr->cfgwatch=tr_cfgwatch_create(tr);
224   if (tr->cfgwatch == NULL) {
225     tr_crit("Unable to create configuration watcher object, exiting.");
226     return 1;
227   }
228   tr->cfgwatch->config_dir=opts.config_dir;
229   tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
230   tr->cfgwatch->update_cb=tr_config_changed; /* handle configuration changes */
231   tr->cfgwatch->update_cookie=(void *)tr;
232   if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
233     tr_crit("Error reading configuration, exiting.");
234     return 1;
235   }
236
237   /***** Exit here if we are just validating our configuration *****/
238   if (opts.validate_config_and_exit) {
239     printf("Valid configuration found in %s.\n", opts.config_dir);
240     return 0;
241   }
242   /***** Set up the event loop *****/
243   ev_base=tr_event_loop_init(); /* Set up the event loop */
244   if (ev_base==NULL) {
245     tr_crit("Error initializing event loop.");
246     return 1;
247   }
248
249   /* already set config_dir, fstat_list and n_files earlier */
250   if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
251     tr_crit("Error initializing configuration file watcher.");
252     return 1;
253   }
254
255   /* install monitoring interface events */
256   tr_debug("Initializing monitoring interface events.");
257   if (0 != tr_mons_event_init(ev_base, tr->mons, tr->cfg_mgr, &mon_ev)) {
258     tr_crit("Error initializing monitoring interface.");
259     return 1;
260   }
261
262   /* install TID server events */
263   tr_debug("Initializing TID server events.");
264   if (0 != tr_tids_event_init(ev_base,
265                               tr->tids,
266                               tr->cfg_mgr,
267                               tr->trps,
268                              &tids_ev)) {
269     tr_crit("Error initializing Trust Path Query Server instance.");
270     return 1;
271   }
272
273   /* install TRP handler events */
274   tr_debug("Initializing Dynamic Trust Router Protocol events.");
275   if (TRP_SUCCESS != tr_trps_event_init(ev_base, tr)) {
276     tr_crit("Error initializing Trust Path Query Server instance.");
277     return 1;
278   }
279
280   tr_debug("Starting event loop.");
281   tr_event_loop_run(ev_base); /* does not return until we are done */
282
283   tr_destroy(tr); /* thanks to talloc, should destroy everything */
284
285   talloc_free(main_ctx);
286   return 0;
287 }