Authenticate GSS context in separate thread. (Not fully working yet.)
[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 <jansson.h>
38 #include <argp.h>
39 #include <event2/event.h>
40 #include <event2/thread.h>
41 #include <talloc.h>
42 #include <sys/time.h>
43
44 #include <tr.h>
45 #include <tid_internal.h>
46 #include <tr_tid.h>
47 #include <tr_trp.h>
48 #include <tr_config.h>
49 #include <tr_event.h>
50 #include <tr_cfgwatch.h>
51 #include <tr_debug.h>
52
53 #define TALLOC_DEBUG_ENABLE 1
54
55 /***** command-line option handling / setup *****/
56
57 /* Strip trailing / from a path name.*/
58 static void remove_trailing_slash(char *s) {
59   size_t n;
60
61   n=strlen(s);
62   if(s[n-1]=='/') {
63     s[n-1]='\0';
64   }
65 }
66
67 /* argp global parameters */
68 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
69
70 /* doc strings */
71 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router";
72 static const char arg_doc[]=""; /* string describing arguments, if any */
73
74 /* define the options here. Fields are:
75  * { long-name, short-name, variable name, options, help description } */
76 static const struct argp_option cmdline_options[] = {
77     { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
78     { NULL }
79 };
80
81 /* structure for communicating with option parser */
82 struct cmdline_args {
83   char *config_dir;
84 };
85
86 /* parser for individual options - fills in a struct cmdline_args */
87 static error_t parse_option(int key, char *arg, struct argp_state *state)
88 {
89   /* get a shorthand to the command line argument structure, part of state */
90   struct cmdline_args *arguments=state->input;
91
92   switch (key) {
93   case 'c':
94     if (arg == NULL) {
95       /* somehow we got called without an argument */
96       return ARGP_ERR_UNKNOWN;
97     }
98     arguments->config_dir=arg;
99     break;
100
101   default:
102     return ARGP_ERR_UNKNOWN;
103   }
104
105   return 0; /* success */
106 }
107
108 /* assemble the argp parser */
109 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
110
111
112 /***** talloc error handling *****/
113 /* called when talloc tries to abort */
114 static void tr_abort(const char *reason)
115 {
116   tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
117   abort();
118 }
119
120 #if TALLOC_DEBUG_ENABLE
121 static void tr_talloc_log(const char *msg)
122 {
123   tr_debug("talloc: %s", msg);
124 }
125 #endif /* TALLOC_DEBUG_ENABLE */
126
127 int main(int argc, char *argv[])
128 {
129   TALLOC_CTX *main_ctx=NULL;
130
131   TR_INSTANCE *tr = NULL;
132   struct cmdline_args opts;
133   struct event_base *ev_base;
134   struct tr_socket_event tids_ev, trps_ev;
135   struct event *cfgwatch_ev;
136
137   /* we're going to be multithreaded, so disable null context tracking */
138   talloc_set_abort_fn(tr_abort);
139   talloc_disable_null_tracking();
140 #if TALLOC_DEBUG_ENABLE
141   talloc_set_log_fn(tr_talloc_log);
142 #endif /* TALLOC_DEBUG_ENABLE */
143   main_ctx=talloc_new(NULL);
144
145   /* Use standalone logging */
146   tr_log_open();
147
148   /***** parse command-line arguments *****/
149   /* set defaults */
150   opts.config_dir=".";
151
152   /* parse the command line*/
153   argp_parse(&argp, argc, argv, 0, 0, &opts);
154
155   /* process options */
156   remove_trailing_slash(opts.config_dir);
157
158   /***** create a Trust Router instance *****/
159   if (NULL == (tr = tr_create(main_ctx))) {
160     tr_crit("Unable to create Trust Router instance, exiting.");
161     return 1;
162   }
163
164   /***** process configuration *****/
165   tr->cfgwatch=tr_cfgwatch_create(tr);
166   if (tr->cfgwatch == NULL) {
167     tr_crit("Unable to create configuration watcher object, exiting.");
168     return 1;
169   }
170   tr->cfgwatch->config_dir=opts.config_dir;
171   tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
172   if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
173     tr_crit("Error reading configuration, exiting.");
174     return 1;
175   }
176
177   /***** initialize the trust path query server instance *****/
178   if (NULL == (tr->tids = tids_create (tr))) {
179     tr_crit("Error initializing Trust Path Query Server instance.");
180     return 1;
181   }
182
183   /***** initialize the trust router protocol server instance *****/
184   if (NULL == (tr->trps = trps_new(tr))) {
185     tr_crit("Error initializing Trust Router Protocol Server instance.");
186     return 1;
187   }
188
189   /***** Set up the event loop *****/
190   evthread_use_pthreads(); /* enable pthreads support */
191   ev_base=tr_event_loop_init(); /* Set up the event loop */
192   if (ev_base==NULL) {
193     tr_crit("Error initializing event loop.");
194     return 1;
195   }
196
197   /* install configuration file watching events */
198   tr->cfgwatch->poll_interval=(struct timeval) {1,0}; /* set poll interval in {sec, usec} */
199   tr->cfgwatch->settling_time=(struct timeval) {5,0}; /* delay for changes to settle before updating */
200   /* TODO: pull these settings out of the configuration files */
201
202   /* already set config_dir, fstat_list and n_files earlier */
203   if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
204     tr_crit("Error initializing configuration file watcher.");
205     return 1;
206   }
207
208   /*tr_status_event_init();*/ /* install status reporting events */
209
210   /* install TID server events */
211   if (0 != tr_tids_event_init(ev_base,
212                               tr->tids,
213                               tr->cfg_mgr,
214                              &tids_ev)) {
215     tr_crit("Error initializing Trust Path Query Server instance.");
216     return 1;
217   }
218
219   /* install TRP handler events */
220   if (0 != tr_trps_event_init(ev_base,
221                               tr->trps,
222                               tr->cfg_mgr,
223                              &trps_ev)) {
224     tr_crit("Error initializing Trust Path Query Server instance.");
225     return 1;
226   }
227
228   tr_event_loop_run(ev_base); /* does not return until we are done */
229
230   /* TODO: ensure talloc is properly used so this actually works */
231   tr_destroy(tr); /* thanks to talloc, should destroy everything */
232
233   talloc_free(main_ctx);
234   return 0;
235 }