2 * Copyright (c) 2012, 2015, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
39 #include <event2/event.h>
45 #include <tr_filter.h>
46 #include <tid_internal.h>
47 #include <tr_config.h>
53 /* Structure to hold TR instance and original request in one cookie */
54 typedef struct tr_resp_cookie {
60 static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc,
65 tr_debug("tr_tidc_resp_handler: Response received (conn = %d)! Realm = %s, Community = %s.", ((TR_RESP_COOKIE *)resp_cookie)->orig_req->conn, resp->realm->buf, resp->comm->buf);
68 /* TBD -- handle concatentation of multiple responses to single req */
69 tids_send_response(((TR_RESP_COOKIE *)resp_cookie)->tr->tids,
70 ((TR_RESP_COOKIE *)resp_cookie)->orig_req,
76 static int tr_tids_req_handler (TIDS_INSTANCE *tids,
81 TIDC_INSTANCE *tidc = NULL;
82 TR_RESP_COOKIE resp_cookie;
83 TR_AAA_SERVER *aaa_servers = NULL;
85 TID_REQ *fwd_req = NULL;
86 TR_COMM *cfg_comm = NULL;
87 TR_COMM *cfg_apc = NULL;
88 TR_INSTANCE *tr = (TR_INSTANCE *) tr_in;
89 int oaction = TR_FILTER_ACTION_REJECT;
91 time_t expiration_interval;
93 if ((!tids) || (!orig_req) || (!resp) || (!tr)) {
94 tr_debug("tr_tids_req_handler: Bad parameters");
98 tr_debug("tr_tids_req_handler: Request received (conn = %d)! Realm = %s, Comm = %s", orig_req->conn,
99 orig_req->realm->buf, orig_req->comm->buf);
103 /* Duplicate the request, so we can modify and forward it */
104 if (NULL == (fwd_req = tid_dup_req(orig_req))) {
105 tr_debug("tr_tids_req_handler: Unable to duplicate request.");
109 if (NULL == (cfg_comm = tr_comm_lookup(tids->cookie, orig_req->comm))) {
110 tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", orig_req->comm->buf);
111 tids_send_err_response(tids, orig_req, "Unknown community");
115 /* Check that the rp_realm matches the filter for the GSS name that
118 if ((!(tr)->rp_gss) ||
119 (!(tr)->rp_gss->filter)) {
120 tr_notice("tr_tids_req_handler: No GSS name for incoming request.");
121 tids_send_err_response(tids, orig_req, "No GSS name for request");
125 if ((TR_FILTER_NO_MATCH == tr_filter_process_rp_permitted(orig_req->rp_realm, (tr)->rp_gss->filter, orig_req->cons, &fwd_req->cons, &oaction)) ||
126 (TR_FILTER_ACTION_REJECT == oaction)) {
127 tr_notice("tr_tids_req_handler: RP realm (%s) does not match RP Realm filter for GSS name", orig_req->rp_realm->buf);
128 tids_send_err_response(tids, orig_req, "RP Realm filter error");
131 /* Check that the rp_realm is a member of the community in the request */
132 if (NULL == (tr_find_comm_rp(cfg_comm, orig_req->rp_realm))) {
133 tr_notice("tr_tids_req_handler: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
134 tids_send_err_response(tids, orig_req, "RP COI membership error");
138 /* Map the comm in the request from a COI to an APC, if needed */
139 if (TR_COMM_COI == cfg_comm->type) {
140 tr_debug("tr_tids_req_handler: Community was a COI, switching.");
141 /* TBD -- In theory there can be more than one? How would that work? */
142 if ((!cfg_comm->apcs) || (!cfg_comm->apcs->id)) {
143 tr_notice("No valid APC for COI %s.", orig_req->comm->buf);
144 tids_send_err_response(tids, orig_req, "No valid APC for community");
147 apc = tr_dup_name(cfg_comm->apcs->id);
149 /* Check that the APC is configured */
150 if (NULL == (cfg_apc = tr_comm_lookup(tids->cookie, apc))) {
151 tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", apc->buf);
152 tids_send_err_response(tids, orig_req, "Unknown APC");
157 fwd_req->orig_coi = orig_req->comm;
159 /* Check that rp_realm is a member of this APC */
160 if (NULL == (tr_find_comm_rp(cfg_apc, orig_req->rp_realm))) {
161 tr_notice("tr_tids_req_hander: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf);
162 tids_send_err_response(tids, orig_req, "RP APC membership error");
167 /* Find the AAA server(s) for this request */
168 if (NULL == (aaa_servers = tr_idp_aaa_server_lookup((TR_INSTANCE *)tids->cookie,
171 tr_debug("tr_tids_req_handler: No AAA Servers for realm %s, defaulting.", orig_req->realm->buf);
172 if (NULL == (aaa_servers = tr_default_server_lookup ((TR_INSTANCE *)tids->cookie,
174 tr_notice("tr_tids_req_handler: No default AAA servers, discarded.");
175 tids_send_err_response(tids, orig_req, "No path to AAA Server(s) for realm");
179 /* if we aren't defaulting, check idp coi and apc membership */
180 if (NULL == (tr_find_comm_idp(cfg_comm, fwd_req->realm))) {
181 tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, orig_req->comm->buf);
182 tids_send_err_response(tids, orig_req, "IDP community membership error");
185 if ( cfg_apc && (NULL == (tr_find_comm_idp(cfg_apc, fwd_req->realm)))) {
186 tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, orig_req->comm->buf);
187 tids_send_err_response(tids, orig_req, "IDP APC membership error");
192 /* send a TID request to the AAA server(s), and get the answer(s) */
193 /* TBD -- Handle multiple servers */
196 expiration_interval = cfg_apc->expiration_interval;
197 else expiration_interval = cfg_comm->expiration_interval;
198 if (fwd_req->expiration_interval)
199 fwd_req->expiration_interval = (expiration_interval < fwd_req->expiration_interval) ? expiration_interval : fwd_req->expiration_interval;
200 else fwd_req->expiration_interval = expiration_interval;
201 /* Create a TID client instance */
202 if (NULL == (tidc = tidc_create())) {
203 tr_crit("tr_tids_req_hander: Unable to allocate TIDC instance.");
204 tids_send_err_response(tids, orig_req, "Memory allocation failure");
207 /* Use the DH parameters from the original request */
208 /* TBD -- this needs to be fixed when we handle more than one req per conn */
209 tidc->client_dh = orig_req->tidc_dh;
211 /* Save information about this request for the response */
213 resp_cookie.orig_req = orig_req;
215 /* Set-up TID connection */
216 if (-1 == (fwd_req->conn = tidc_open_connection(tidc,
217 aaa_servers->hostname->buf,
219 &(fwd_req->gssctx)))) {
220 tr_notice("tr_tids_req_handler: Error in tidc_open_connection.");
221 tids_send_err_response(tids, orig_req, "Can't open connection to next hop TIDS");
225 /* Send a TID request */
226 if (0 > (rc = tidc_fwd_request(tidc, fwd_req, &tr_tidc_resp_handler, (void *)&resp_cookie))) {
227 tr_notice("Error from tidc_fwd_request, rc = %d.", rc);
228 tids_send_err_response(tids, orig_req, "Can't forward request to next hop TIDS");
229 tid_req_free(orig_req);
233 tid_req_free(orig_req);
237 static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
241 TR_INSTANCE *tr = (TR_INSTANCE *) tr_in;
243 if ((!client_name) || (!gss_name) || (!tr)) {
244 tr_debug("tr_tidc_gss_handler: Bad parameters.");
248 /* look up the RP client matching the GSS name */
249 if ((NULL == (rp = tr_rp_client_lookup(tr, gss_name)))) {
250 tr_debug("tr_tids_gss_handler: Unknown GSS name %s", gss_name->buf);
254 /* Store the rp client in the TR_INSTANCE structure for now...
255 * TBD -- fix me for new tasking model. */
257 tr_debug("Client's GSS Name: %s", gss_name->buf);
264 /***** event loop management ****/
266 /* struct for hanging on to a socket listener event */
267 struct tr_socket_event {
268 int sock_fd; /* the fd for the socket */
269 struct event *ev; /* its event */
272 /* Allocate and set up the event base, return a pointer
273 * to the new event_base or NULL on failure.
274 * Does not currently enable thread-safe mode. */
275 static struct event_base *tr_event_loop_init(void)
277 struct event_base *base=NULL;
279 base=event_base_new();
281 tr_crit("Error initializing event loop.");
287 /* run the loop, does not normally return */
288 static int tr_event_loop_run(struct event_base *base)
290 return event_base_dispatch(base);
293 /* called when a connection to the TIDS port is received */
294 static void tr_tids_event_cb(int listener, short event, void *arg)
296 TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
298 if (0==(event & EV_READ))
299 tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
301 tids_accept(tids, listener);
304 /* Configure the tids instance and set up its event handler.
305 * Returns 0 on success, nonzero on failure. Fills in
306 * *tids_event (which should be allocated). */
307 static int tr_tids_event_init(struct event_base *base,
309 struct tr_socket_event *tids_ev)
311 if (tids_ev == NULL) {
312 tr_debug("tr_tids_event_init: Null tids_ev.");
316 /* get a tids listener */
317 tids_ev->sock_fd=tids_get_listener(tr->tids,
320 tr->active_cfg->internal->hostname,
321 tr->active_cfg->internal->tids_port,
323 if (tids_ev->sock_fd < 0) {
324 tr_crit("Error opening TID server socket.");
329 tids_ev->ev=event_new(base,
334 event_add(tids_ev->ev, NULL);
340 /***** config file watching *****/
344 struct timespec mtime;
347 struct tr_cfgwatch_data {
348 struct timeval poll_interval; /* how often should we check for updates? */
349 struct timeval settling_time; /* how long should we wait for changes to settle before updating? */
350 char *config_dir; /* what directory are we watching? */
351 struct tr_fstat *fstat_list; /* file names and mtimes */
352 int n_files; /* number of files in fstat_list */
353 int change_detected; /* have we detected a change? */
354 struct timeval last_change_detected; /* when did we last note a changed mtime? */
355 TALLOC_CTX *ctx; /* what context should own configuration talloc blocks? */
356 TR_INSTANCE *tr; /* what trust router are we updating? */
358 typedef struct tr_cfgwatch_data TR_CFGWATCH;
360 /* Initialize a new tr_cfgwatch_data struct. Free this with talloc. */
361 static TR_CFGWATCH *tr_cfgwatch_create(TALLOC_CTX *mem_ctx)
363 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
364 TR_CFGWATCH *new_cfg;
366 new_cfg=talloc(tmp_ctx, TR_CFGWATCH);
367 if (new_cfg == NULL) {
368 tr_debug("tr_cfgwatch_create: Allocation failed.");
370 timerclear(&new_cfg->poll_interval);
371 timerclear(&new_cfg->settling_time);
372 new_cfg->config_dir=NULL;
373 new_cfg->fstat_list=NULL;
375 new_cfg->change_detected=0;
376 timerclear(&new_cfg->last_change_detected);
381 talloc_steal(mem_ctx, new_cfg);
382 talloc_free(tmp_ctx);
386 /* Obtain the file modification time as seconds since epoch. Returns 0 on success. */
387 static int tr_get_mtime(const char *path, struct timespec *ts)
389 struct stat file_status;
391 if (stat(path, &file_status) != 0) {
394 (*ts)=file_status.st_mtim;
399 static char *tr_join_paths(TALLOC_CTX *mem_ctx, const char *p1, const char *p2)
401 return talloc_asprintf(mem_ctx, "%s/%s", p1, p2); /* returns NULL on a failure */
404 static int tr_fstat_namecmp(const void *p1_arg, const void *p2_arg)
406 struct tr_fstat *p1=(struct tr_fstat *) p1_arg;
407 struct tr_fstat *p2=(struct tr_fstat *) p2_arg;
409 return strcmp(p1->name, p2->name);
412 static int tr_fstat_mtimecmp(const void *p1_arg, const void *p2_arg)
414 struct tr_fstat *p1=(struct tr_fstat *) p1_arg;
415 struct tr_fstat *p2=(struct tr_fstat *) p2_arg;
417 if (p1->mtime.tv_sec == p2->mtime.tv_sec)
418 return (p1->mtime.tv_nsec) - (p2->mtime.tv_nsec);
420 return (p1->mtime.tv_sec) - (p2->mtime.tv_sec);
423 /* Get status of all files in cfg_files. Returns list, or NULL on error.
424 * Files are sorted by filename.
425 * After success, caller must eventually free result with talloc_free. */
426 static struct tr_fstat *tr_fstat_get_all(TALLOC_CTX *mem_ctx,
427 const char *config_path,
428 struct dirent **cfg_files,
431 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
432 struct tr_fstat *fstat_list=NULL;
435 /* create a new fstat list (may be discarded) */
436 fstat_list=talloc_array(tmp_ctx, struct tr_fstat, n_files);
437 if (fstat_list==NULL) {
438 tr_err("tr_fstat_get_all: Could not allocate fstat list.");
442 for (ii=0; ii<n_files; ii++) {
443 fstat_list[ii].name=talloc_strdup(fstat_list, cfg_files[ii]->d_name);
444 if (0 != tr_get_mtime(tr_join_paths(tmp_ctx, config_path, fstat_list[ii].name),
445 &(fstat_list[ii].mtime))) {
446 tr_warning("tr_fstat_get_all: Could not obtain mtime for file %s", fstat_list[ii].name);
451 qsort(fstat_list, n_files, sizeof(struct tr_fstat), tr_fstat_namecmp);
453 /* put list in the caller's context and return it */
454 talloc_steal(mem_ctx, fstat_list);
456 talloc_free(tmp_ctx);
460 /* must specify the ctx and tr in cfgwatch! */
461 static int tr_read_and_apply_config(TR_CFGWATCH *cfgwatch)
463 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
464 char *config_dir=cfgwatch->config_dir;
466 struct dirent **cfg_files=NULL;
467 TR_CFG_RC rc = TR_CFG_SUCCESS; /* presume success */
468 struct tr_fstat *new_fstat_list=NULL;
471 /* find the configuration files -- n.b., tr_find_config_files()
472 * allocates memory to cfg_files which we must later free */
473 tr_debug("Reading configuration files from %s/", config_dir);
474 n_files = tr_find_config_files(config_dir, &cfg_files);
476 tr_debug("tr_read_and_apply_config: No configuration files.");
477 retval=1; goto cleanup;
480 /* Get the list of update times.
481 * Do this before loading in case they change between obtaining their timestamp
482 * and reading the file---this way they will immediately reload if this happens. */
483 new_fstat_list=tr_fstat_get_all(tmp_ctx, config_dir, cfg_files, n_files);
484 if (new_fstat_list==NULL) {
485 tr_debug("tr_read_and_apply_config: Could not allocate config file status list.");
486 retval=1; goto cleanup;
489 if (TR_CFG_SUCCESS != tr_parse_config(cfgwatch->tr, config_dir, n_files, cfg_files)) {
490 tr_debug("tr_read_and_apply_config: Error decoding configuration information.");
491 retval=1; goto cleanup;
494 /* apply initial configuration */
495 if (TR_CFG_SUCCESS != (rc = tr_apply_new_config(cfgwatch->tr))) {
496 tr_debug("tr_read_and_apply_config: Error applying configuration, rc = %d.", rc);
497 retval=1; goto cleanup;
500 /* give ownership of the new_fstat_list to caller's context */
501 if (cfgwatch->fstat_list != NULL) {
502 /* free the old one */
503 talloc_free(cfgwatch->fstat_list);
505 cfgwatch->n_files=n_files;
506 cfgwatch->fstat_list=new_fstat_list;
507 talloc_steal(cfgwatch->ctx, new_fstat_list);
511 tr_free_config_file_list(n_files, &cfg_files);
512 talloc_free(tmp_ctx);
517 /* Checks whether any config files have appeared/disappeared/modified.
518 * Returns 1 if so, 0 otherwise. */
519 static int tr_cfgwatch_update_needed(TR_CFGWATCH *cfg_status)
521 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
522 struct tr_fstat *fstat_list=NULL;
525 struct dirent **cfg_files=NULL;
526 int update_needed=0; /* return value */
528 /* get the list, must free cfg_files later with tr_free_cfg_file_list */
529 n_files = tr_find_config_files(cfg_status->config_dir, &cfg_files);
531 tr_warning("tr_cfgwatch_update: configuration files disappeared, skipping update.");
535 /* create a new fstat list (will be discarded) */
536 fstat_list=tr_fstat_get_all(tmp_ctx, cfg_status->config_dir, cfg_files, n_files);
537 if (fstat_list==NULL) {
538 tr_err("tr_cfgwatch_update: Error getting fstat list.");
542 /* see if the number of files change, if so need to update */
543 if (n_files != cfg_status->n_files) {
544 tr_debug("tr_cfgwatch_update: Changed number of config files (was %d, now %d).",
548 talloc_free(cfg_status->fstat_list);
549 cfg_status->n_files=n_files;
550 cfg_status->fstat_list=fstat_list;
551 talloc_steal(cfg_status->ctx, fstat_list);
555 /* See if any files have a changed mtime. Both are sorted by name so this is easy. */
556 for (ii=0; ii<n_files; ii++) {
557 if ((0 != tr_fstat_mtimecmp(&fstat_list[ii], &cfg_status->fstat_list[ii]))
558 || (0 != tr_fstat_namecmp(&fstat_list[ii], &cfg_status->fstat_list[ii]))){
560 talloc_free(cfg_status->fstat_list);
561 cfg_status->n_files=n_files;
562 cfg_status->fstat_list=fstat_list;
563 talloc_steal(cfg_status->ctx, fstat_list);
569 tr_free_config_file_list(n_files, &cfg_files);
570 talloc_free(tmp_ctx);
571 return update_needed;
574 static void tr_cfgwatch_event_cb(int listener, short event, void *arg)
576 TR_CFGWATCH *cfg_status=(TR_CFGWATCH *) arg;
577 struct timeval now, diff;;
579 if (tr_cfgwatch_update_needed(cfg_status)) {
580 tr_notice("Configuration file change detected, waiting for changes to settle.");
581 /* if (!cfg_status->change_detected) {*/
582 cfg_status->change_detected=1;
584 if (0 != gettimeofday(&cfg_status->last_change_detected, NULL)) {
585 tr_err("tr_cfgwatch_event_cb: gettimeofday() failed (1).");
590 if (cfg_status->change_detected) {
591 if (0 != gettimeofday(&now, NULL)) {
592 tr_err("tr_cfgwatch_event_cb: gettimeofday() failed (2).");
594 timersub(&now, &cfg_status->last_change_detected, &diff);
595 if (!timercmp(&diff, &cfg_status->settling_time, <)) {
596 tr_notice("Configuration file change settled, updating configuration.");
597 if (0 != tr_read_and_apply_config(cfg_status))
598 tr_warning("Configuration file update failed. Using previous configuration.");
600 tr_notice("Configuration updated successfully.");
601 cfg_status->change_detected=0;
607 /* Configure the cfgwatch instance and set up its event handler.
608 * Returns 0 on success, nonzero on failure. Points
609 * *cfgwatch_ev to the event struct. */
610 static int tr_cfgwatch_event_init(struct event_base *base,
611 TR_CFGWATCH *cfg_status,
612 struct event **cfgwatch_ev)
614 if (cfgwatch_ev == NULL) {
615 tr_debug("tr_cfgwatch_event_init: Null cfgwatch_ev.");
619 /* zero out the change detection fields */
620 cfg_status->change_detected=0;
621 cfg_status->last_change_detected.tv_sec=0;
622 cfg_status->last_change_detected.tv_usec=0;
624 /* create the event and enable it */
625 *cfgwatch_ev=event_new(base, -1, EV_TIMEOUT|EV_PERSIST, tr_cfgwatch_event_cb, (void *)cfg_status);
626 event_add(*cfgwatch_ev, &(cfg_status->poll_interval));
628 tr_info("tr_cfgwatch_event_init: Added configuration file watcher with %0d.%06d second poll interval.",
629 cfg_status->poll_interval.tv_sec,
630 cfg_status->poll_interval.tv_usec);
635 /***** command-line option handling / setup *****/
637 /* Strip trailing / from a path name.*/
638 static void remove_trailing_slash(char *s) {
647 /* argp global parameters */
648 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
651 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router";
652 static const char arg_doc[]=""; /* string describing arguments, if any */
654 /* define the options here. Fields are:
655 * { long-name, short-name, variable name, options, help description } */
656 static const struct argp_option cmdline_options[] = {
657 { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
661 /* structure for communicating with option parser */
662 struct cmdline_args {
666 /* parser for individual options - fills in a struct cmdline_args */
667 static error_t parse_option(int key, char *arg, struct argp_state *state)
669 /* get a shorthand to the command line argument structure, part of state */
670 struct cmdline_args *arguments=state->input;
675 /* somehow we got called without an argument */
676 return ARGP_ERR_UNKNOWN;
678 arguments->config_dir=arg;
682 return ARGP_ERR_UNKNOWN;
685 return 0; /* success */
688 /* assemble the argp parser */
689 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
692 int main (int argc, char *argv[])
695 TALLOC_CTX *main_ctx=talloc_new(NULL);
697 TR_INSTANCE *tr = NULL;
698 struct cmdline_args opts;
699 struct event_base *ev_base;
700 struct tr_socket_event tids_ev;
701 struct event *cfgwatch_ev;
702 TR_CFGWATCH *cfgwatch; /* config file watcher status */
704 /* Use standalone logging */
707 /***** parse command-line arguments *****/
711 /* parse the command line*/
712 argp_parse(&argp, argc, argv, 0, 0, &opts);
714 /* process options */
715 remove_trailing_slash(opts.config_dir);
717 /* Get a configuration status object */
718 cfgwatch=tr_cfgwatch_create(main_ctx);
719 if (cfgwatch == NULL) {
720 tr_error("Unable to create configuration watcher object, exiting.");
724 /***** create a Trust Router instance *****/
725 if (NULL == (tr = tr_create())) {
726 tr_crit("Unable to create Trust Router instance, exiting.");
730 /***** process configuration *****/
731 cfgwatch->config_dir=opts.config_dir;
732 cfgwatch->ctx=main_ctx;
734 if (0 != tr_read_and_apply_config(cfgwatch)) {
735 tr_crit("Error reading configuration, exiting.");
739 /***** initialize the trust path query server instance *****/
740 if (0 == (tr->tids = tids_create ())) {
741 tr_crit("Error initializing Trust Path Query Server instance.");
745 /***** Set up the event loop *****/
746 ev_base=tr_event_loop_init(); /* Set up the event loop */
748 /* install configuration file watching events */
749 cfgwatch->poll_interval=(struct timeval) {1,0}; /* set poll interval in {sec, usec} */
750 cfgwatch->settling_time=(struct timeval) {5,0}; /* delay for changes to settle before updating */
752 /* already set config_dir, fstat_list and n_files earlier */
753 if (0 != tr_cfgwatch_event_init(ev_base, cfgwatch, &cfgwatch_ev)) {
754 tr_crit("Error initializing configuration file watcher.");
758 /*tr_status_event_init();*/ /* install status reporting events */
760 /* install TID server events */
761 if (0 != tr_tids_event_init(ev_base, tr, &tids_ev)) {
762 tr_crit("Error initializing Trust Path Query Server instance.");
766 /*tr_trp_event_init();*/ /* install TRP handler events */
768 fflush(stdout); fflush(stderr);
769 tr_event_loop_run(ev_base); /* does not return until we are done */
771 /* TODO: update the cleanup code */
772 tids_destroy(tr->tids);
775 talloc_free(main_ctx);