2 * Copyright (c) 2012, 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.
42 #include <tid_internal.h>
43 #include <trust_router/tr_dh.h>
45 static void tidc_resp_handler (TIDC_INSTANCE * tidc,
51 unsigned char *c_keybuf = NULL;
54 printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
56 /* Generate the client key -- TBD, handle more than one server */
57 if (TID_SUCCESS != resp->result) {
58 fprintf(stderr, "tidc_resp_handler: Response is an error.\n");
63 fprintf(stderr, "tidc_resp_handler: Response does not contain server info.\n");
67 if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf,
68 resp->servers->aaa_server_dh->pub_key,
71 printf("tidc_resp_handler: Error computing client key.\n");
75 /* Print out the client key. */
76 printf("Client Key Generated (len = %d):\n", c_keylen);
77 for (i = 0; i < c_keylen; i++) {
78 printf("%.2x", c_keybuf[i]);
86 static void print_version_info(void)
88 printf("Moonshot TID Client %s\n\n", PACKAGE_VERSION);
91 /* command-line option setup */
93 /* argp global parameters */
94 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
97 static const char doc[]=PACKAGE_NAME " - Moonshot TID Client " PACKAGE_VERSION;
98 static const char arg_doc[]="<server> <RP-realm> <target-realm> <community> [<port>]"; /* string describing arguments, if any */
100 /* define the options here. Fields are:
101 * { long-name, short-name, variable name, options, help description } */
102 static const struct argp_option cmdline_options[] = {
103 { "version", 'v', NULL, 0, "Print version information and exit"},
107 /* structure for communicating with option parser */
108 struct cmdline_args {
113 int port; /* optional */
116 /* parser for individual options - fills in a struct cmdline_args */
117 static error_t parse_option(int key, char *arg, struct argp_state *state)
119 /* get a shorthand to the command line argument structure, part of state */
120 struct cmdline_args *arguments=state->input;
123 case ARGP_KEY_ARG: /* handle argument (not option) */
124 switch (state->arg_num) {
126 arguments->server=arg;
130 arguments->rp_realm=arg;
134 arguments->target_realm=arg;
138 arguments->community=arg;
142 arguments->port=strtol(arg, NULL, 10); /* optional */
146 /* too many arguments */
151 case ARGP_KEY_END: /* no more arguments */
152 if (state->arg_num < 4) {
153 /* not enough arguments encountered */
159 print_version_info();
163 return ARGP_ERR_UNKNOWN;
166 return 0; /* success */
169 /* assemble the argp parser */
170 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
179 struct cmdline_args opts;
181 /* parse the command line*/
185 opts.target_realm=NULL;
189 argp_parse(&argp, argc, argv, 0, 0, &opts);
190 /* TBD -- validity checking, dealing with quotes, etc. */
192 print_version_info();
194 /* Use standalone logging */
197 /* set logging levels */
198 talloc_set_log_stderr();
199 tr_log_threshold(LOG_CRIT);
200 tr_console_threshold(LOG_DEBUG);
202 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);
204 /* Create a TID client instance & the client DH */
205 tidc = tidc_create();
206 if (NULL == (tidc->client_dh = tr_create_dh_params(NULL, 0))) {
207 printf("Error creating client DH params.\n");
211 /* Set-up TID connection */
212 if (-1 == (conn = tidc_open_connection(tidc, opts.server, opts.port, &gssctx))) {
214 printf("Error in tidc_open_connection.\n");
218 /* Send a TID request */
219 if (0 > (rc = tidc_send_request(tidc, conn, gssctx, opts.rp_realm, opts.target_realm, opts.community,
220 &tidc_resp_handler, NULL))) {
222 printf("Error in tidc_send_request, rc = %d.\n", rc);
226 /* Clean-up the TID client instance, and exit */