X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=tid%2Fexample%2Ftidc_main.c;h=02cd1db58faf5fbb08c2fbbbd3834b785cbb702e;hb=6491f2a660c3183b210b968a939eaa18f1b44a09;hp=d5361ea80d2b4715898d6ad8ed1dc5236350a1e8;hpb=5831eccf8362e9323b0e353d400d70c4ecb80883;p=trust_router.git diff --git a/tid/example/tidc_main.c b/tid/example/tidc_main.c index d5361ea..02cd1db 100644 --- a/tid/example/tidc_main.c +++ b/tid/example/tidc_main.c @@ -34,76 +34,203 @@ #include #include +#include +#include #include +#include +#include +#include #include -static int tidc_response_received = 0; - -void static tidc_print_usage (const char *name) -{ - printf("Usage: %s \n", name); -} - static void tidc_resp_handler (TIDC_INSTANCE * tidc, TID_REQ *req, TID_RESP *resp, void *cookie) { + int c_keylen = 0; + unsigned char *c_keybuf = NULL; + int i; + struct timeval tv; + printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf); - tidc_response_received = 1; + /* Generate the client key -- TBD, handle more than one server */ + if (TID_SUCCESS != resp->result) { + fprintf(stderr, "tidc_resp_handler: Response is an error.\n"); + return; + } + + if (!resp->servers) { + fprintf(stderr, "tidc_resp_handler: Response does not contain server info.\n"); + return; + } + if (tid_srvr_get_key_expiration(tid_resp_get_server(resp, 0), &tv)) + printf("Error reading key expiration\n"); + else + printf("Key expiration: %s", ctime(&tv.tv_sec)); + + + if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf, + resp->servers->aaa_server_dh->pub_key, + req->tidc_dh))) { + + printf("tidc_resp_handler: Error computing client key.\n"); + return; + } + /* Print out the client key. */ + printf("Client Key Generated (len = %d):\n", c_keylen); + for (i = 0; i < c_keylen; i++) { + printf("%.2x", c_keybuf[i]); + } + printf("\n"); + return; } + +static void print_version_info(void) +{ + printf("Moonshot TID Client %s\n\n", PACKAGE_VERSION); +} + +/* command-line option setup */ + +/* argp global parameters */ +const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */ + +/* doc strings */ +static const char doc[]=PACKAGE_NAME " - Moonshot TID Client " PACKAGE_VERSION; +static const char arg_doc[]=" []"; /* string describing arguments, if any */ + +/* define the options here. Fields are: + * { long-name, short-name, variable name, options, help description } */ +static const struct argp_option cmdline_options[] = { + { "version", 'v', NULL, 0, "Print version information and exit"}, + { NULL } +}; + +/* structure for communicating with option parser */ +struct cmdline_args { + char *server; + char *rp_realm; + char *target_realm; + char *community; + int port; /* optional */ +}; + +/* parser for individual options - fills in a struct cmdline_args */ +static error_t parse_option(int key, char *arg, struct argp_state *state) +{ + /* get a shorthand to the command line argument structure, part of state */ + struct cmdline_args *arguments=state->input; + + switch (key) { + case ARGP_KEY_ARG: /* handle argument (not option) */ + switch (state->arg_num) { + case 0: + arguments->server=arg; + break; + + case 1: + arguments->rp_realm=arg; + break; + + case 2: + arguments->target_realm=arg; + break; + + case 3: + arguments->community=arg; + break; + + case 4: + arguments->port=strtol(arg, NULL, 10); /* optional */ + break; + + default: + /* too many arguments */ + argp_usage(state); + } + break; + + case ARGP_KEY_END: /* no more arguments */ + if (state->arg_num < 4) { + /* not enough arguments encountered */ + argp_usage(state); + } + break; + + case 'v': + print_version_info(); + exit(0); + + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; /* success */ +} + +/* assemble the argp parser */ +static struct argp argp = {cmdline_options, parse_option, arg_doc, doc}; + int main (int argc, - const char *argv[]) + char *argv[]) { TIDC_INSTANCE *tidc; - char *server = NULL; - char *rp_realm = NULL; - char *realm = NULL; - char *coi = NULL; int conn = 0; int rc; gss_ctx_id_t gssctx; + struct cmdline_args opts; - /* Parse command-line arguments */ - if (argc != 5) { - tidc_print_usage(argv[0]); - exit(1); - } + /* parse the command line*/ + /* set defaults */ + opts.server=NULL; + opts.rp_realm=NULL; + opts.target_realm=NULL; + opts.community=NULL; + opts.port=TID_PORT; + argp_parse(&argp, argc, argv, 0, 0, &opts); /* TBD -- validity checking, dealing with quotes, etc. */ - server = (char *)argv[1]; - rp_realm = (char *) argv[2]; - realm = (char *)argv[3]; - coi = (char *)argv[4]; - printf("TIDC Client:\nServer = %s, rp_realm = %s, target_realm = %s, community = %s\n", server, rp_realm, realm, coi); + print_version_info(); + + /* Use standalone logging */ + tr_log_open(); + + /* set logging levels */ + talloc_set_log_stderr(); + tr_log_threshold(LOG_CRIT); + tr_console_threshold(LOG_DEBUG); + + printf("TIDC Client:\nServer = %s, rp_realm = %s, target_realm = %s, community = %s, port = %i\n", opts.server, opts.rp_realm, opts.target_realm, opts.community, opts.port); - /* Create a TID client instance */ + /* Create a TID client instance & the client DH */ tidc = tidc_create(); + tidc_set_dh(tidc, tr_create_dh_params(NULL, 0)); + if (tidc_get_dh(tidc) == NULL) { + printf("Error creating client DH params.\n"); + return 1; + } /* Set-up TID connection */ - if (-1 == (conn = tidc_open_connection(tidc, server, &gssctx))) { + if (-1 == (conn = tidc_open_connection(tidc, opts.server, opts.port, &gssctx))) { /* Handle error */ printf("Error in tidc_open_connection.\n"); return 1; }; /* Send a TID request */ - if (0 > (rc = tidc_send_request(tidc, conn, gssctx, rp_realm, realm, coi, + if (0 > (rc = tidc_send_request(tidc, conn, gssctx, opts.rp_realm, opts.target_realm, opts.community, &tidc_resp_handler, NULL))) { /* Handle error */ printf("Error in tidc_send_request, rc = %d.\n", rc); return 1; } - /* Wait for a response */ - while (!tidc_response_received); - /* Clean-up the TID client instance, and exit */ tidc_destroy(tidc);