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.
43 #include <trust_router/tr_dh.h>
46 static void tidc_resp_handler (TIDC_INSTANCE * tidc,
52 unsigned char *c_keybuf = NULL;
55 printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
57 /* Generate the client key -- TBD, handle more than one server */
58 if (TID_SUCCESS != resp->result) {
59 fprintf(stderr, "tidc_resp_handler: Response is an error.\n");
64 fprintf(stderr, "tidc_resp_handler: Response does not contain server info.\n");
68 if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf,
69 resp->servers->aaa_server_dh->pub_key,
72 printf("tidc_resp_handler: Error computing client key.\n");
76 /* Print out the client key. */
77 printf("Client Key Generated (len = %d):\n", c_keylen);
78 for (i = 0; i < c_keylen; i++) {
79 printf("%.2x", c_keybuf[i]);
87 /* command-line option setup */
89 /* argp global parameters */
90 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
93 static const char doc[]=PACKAGE_NAME " - TRP Client";
94 static const char arg_doc[]="<message> <server> [<port>]"; /* string describing arguments, if any */
96 /* define the options here. Fields are:
97 * { long-name, short-name, variable name, options, help description } */
98 static const struct argp_option cmdline_options[] = {
102 /* structure for communicating with option parser */
103 struct cmdline_args {
106 int port; /* optional */
109 /* parser for individual options - fills in a struct cmdline_args */
110 static error_t parse_option(int key, char *arg, struct argp_state *state)
112 /* get a shorthand to the command line argument structure, part of state */
113 struct cmdline_args *arguments=state->input;
116 case ARGP_KEY_ARG: /* handle argument (not option) */
117 switch (state->arg_num) {
123 arguments->server=arg;
127 arguments->port=strtol(arg, NULL, 10); /* optional */
131 /* too many arguments */
136 case ARGP_KEY_END: /* no more arguments */
137 if (state->arg_num < 2) {
138 /* not enough arguments encountered */
144 return ARGP_ERR_UNKNOWN;
147 return 0; /* success */
150 /* assemble the argp parser */
151 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
156 TALLOC_CTX *main_ctx=talloc_new(NULL);
157 TRPC_INSTANCE *trpc=NULL;
161 struct cmdline_args opts;
163 /* parse the command line*/
169 argp_parse(&argp, argc, argv, 0, 0, &opts);
170 /* TBD -- validity checking, dealing with quotes, etc. */
172 /* Use standalone logging */
175 /* set logging levels */
176 talloc_set_log_stderr();
177 tr_log_threshold(LOG_CRIT);
178 tr_console_threshold(LOG_DEBUG);
180 printf("TRPC Client:\nServer = %s, port = %i\n", opts.server, opts.port);
182 /* Create a TRP client instance & the client DH */
183 trpc = trpc_create(main_ctx);
184 if (NULL == (trpc->client_dh = tr_create_dh_params(NULL, 0))) {
185 printf("Error creating client DH params.\n");
189 /* Set-up TRP connection */
190 if (-1 == (conn = trpc_open_connection(trpc, opts.server, opts.port, &gssctx))) {
192 printf("Error in trpc_open_connection.\n");
196 /* Send a TRP message */
197 if (0 > (rc = trpc_send_msg(trpc, conn, gssctx, opts.msg, NULL, NULL))) {
199 printf("Error in trpc_send_request, rc = %d.\n", rc);
203 /* Clean-up the TRP client instance, and exit */