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>
44 #include <trust_router/tid.h>
45 #include <tr_inet_util.h>
47 struct tidc_resp_cookie {
51 static void tidc_resp_handler (TIDC_INSTANCE * tidc,
57 unsigned char *c_keybuf = NULL;
60 struct tidc_resp_cookie *data = (struct tidc_resp_cookie *) cookie;
62 printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
66 /* Generate the client key -- TBD, handle more than one server */
67 if (TID_SUCCESS != resp->result) {
68 fprintf(stderr, "tidc_resp_handler: Response is an error.\n");
73 fprintf(stderr, "tidc_resp_handler: Response does not contain server info.\n");
76 if (tid_srvr_get_key_expiration(tid_resp_get_server(resp, 0), &tv))
77 printf("Error reading key expiration\n");
79 printf("Key expiration: %s", ctime(&tv.tv_sec));
82 if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf,
83 resp->servers->aaa_server_dh->pub_key,
86 printf("tidc_resp_handler: Error computing client key.\n");
90 /* Print out the client key. */
91 printf("Client Key Generated (len = %d):\n", c_keylen);
92 for (i = 0; i < c_keylen; i++) {
93 printf("%.2x", c_keybuf[i]);
102 static void print_version_info(void)
104 printf("Moonshot TID Client %s\n\n", PACKAGE_VERSION);
107 /* command-line option setup */
109 /* argp global parameters */
110 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
113 static const char doc[]=PACKAGE_NAME " - Moonshot TID Client " PACKAGE_VERSION;
114 static const char arg_doc[]="<server> <RP-realm> <target-realm> <community> [<port>]"; /* string describing arguments, if any */
116 /* define the options here. Fields are:
117 * { long-name, short-name, variable name, options, help description } */
118 static const struct argp_option cmdline_options[] = {
119 { "version", 'v', NULL, 0, "Print version information and exit"},
123 /* structure for communicating with option parser */
124 struct cmdline_args {
129 int port; /* optional */
132 /* parser for individual options - fills in a struct cmdline_args */
133 static error_t parse_option(int key, char *arg, struct argp_state *state)
135 /* get a shorthand to the command line argument structure, part of state */
136 struct cmdline_args *arguments=state->input;
139 case ARGP_KEY_ARG: /* handle argument (not option) */
140 switch (state->arg_num) {
142 arguments->server=arg;
146 arguments->rp_realm=arg;
150 arguments->target_realm=arg;
154 arguments->community=arg;
158 arguments->port=tr_parse_port(arg); /* optional */
159 if (arguments->port < 0) {
160 switch(-(arguments->port)) {
162 printf("\nError parsing port (%s): port must be an integer in the range 1 - 65535\n\n", arg);
166 printf("\nError parsing port (%s): %s\n\n", arg, strerror(-arguments->port));
174 /* too many arguments */
179 case ARGP_KEY_END: /* no more arguments */
180 if (state->arg_num < 4) {
181 /* not enough arguments encountered */
187 print_version_info();
191 return ARGP_ERR_UNKNOWN;
194 return 0; /* success */
199 #define EXIT_REQ_FAILED 2
202 /* assemble the argp parser */
203 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
212 struct cmdline_args opts;
213 struct tidc_resp_cookie cookie = {0};
215 /* parse the command line*/
219 opts.target_realm=NULL;
223 argp_parse(&argp, argc, argv, 0, 0, &opts);
224 /* TBD -- validity checking, dealing with quotes, etc. */
226 print_version_info();
228 /* Use standalone logging */
231 /* set logging levels */
232 talloc_set_log_stderr();
233 tr_log_threshold(LOG_CRIT);
234 tr_console_threshold(LOG_DEBUG);
236 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);
238 /* Create a TID client instance & the client DH */
239 tidc = tidc_create();
240 tidc_set_dh(tidc, tr_create_dh_params(NULL, 0));
241 if (tidc_get_dh(tidc) == NULL) {
242 printf("Error creating client DH params.\n");
246 /* Set-up TID connection */
247 if (-1 == (conn = tidc_open_connection(tidc, opts.server, opts.port, &gssctx))) {
249 printf("Error in tidc_open_connection.\n");
253 /* Send a TID request */
254 if (0 > (rc = tidc_send_request(tidc, conn, gssctx, opts.rp_realm, opts.target_realm, opts.community,
255 &tidc_resp_handler, &cookie))) {
257 printf("Error in tidc_send_request, rc = %d.\n", rc);
261 /* Clean-up the TID client instance, and exit */
264 if (cookie.succeeded)
267 return EXIT_REQ_FAILED;