Output key expiration time on a successful request
[trust_router.git] / tid / example / tidc_main.c
1 /*
2  * Copyright (c) 2012, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <talloc.h>
38 #include <argp.h>
39
40 #include <gsscon.h>
41 #include <tr_debug.h>
42 #include <tid_internal.h>
43 #include <trust_router/tr_dh.h>
44 #include <trust_router/tid.h>
45
46 static void tidc_resp_handler (TIDC_INSTANCE * tidc, 
47                         TID_REQ *req,
48                         TID_RESP *resp, 
49                         void *cookie) 
50 {
51   int c_keylen = 0;
52   unsigned char *c_keybuf = NULL;
53   int i;
54   struct timeval tv;
55
56   printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
57
58   /* Generate the client key -- TBD, handle more than one server */
59   if (TID_SUCCESS != resp->result) {
60     fprintf(stderr, "tidc_resp_handler: Response is an error.\n");
61     return;
62   }
63
64   if (!resp->servers) {
65     fprintf(stderr, "tidc_resp_handler: Response does not contain server info.\n");
66     return;
67   }
68   if (tid_srvr_get_key_expiration(tid_resp_get_server(resp, 0), &tv))
69     printf("Error reading key expiration\n");
70   else
71     printf("Key expiration: %s", ctime(&tv.tv_sec));
72
73
74   if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf, 
75                                       resp->servers->aaa_server_dh->pub_key, 
76                                       req->tidc_dh))) {
77     
78     printf("tidc_resp_handler: Error computing client key.\n");
79     return;
80   }
81   
82   /* Print out the client key. */
83   printf("Client Key Generated (len = %d):\n", c_keylen);
84   for (i = 0; i < c_keylen; i++) {
85     printf("%.2x", c_keybuf[i]); 
86   }
87   printf("\n");
88
89   return;
90 }
91
92
93 static void print_version_info(void)
94 {
95   printf("Moonshot TID Client %s\n\n", PACKAGE_VERSION);
96 }
97
98 /* command-line option setup */
99
100 /* argp global parameters */
101 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
102
103 /* doc strings */
104 static const char doc[]=PACKAGE_NAME " - Moonshot TID Client " PACKAGE_VERSION;
105 static const char arg_doc[]="<server> <RP-realm> <target-realm> <community> [<port>]"; /* string describing arguments, if any */
106
107 /* define the options here. Fields are:
108  * { long-name, short-name, variable name, options, help description } */
109 static const struct argp_option cmdline_options[] = {
110     { "version", 'v', NULL, 0, "Print version information and exit"},
111     { NULL }
112 };
113
114 /* structure for communicating with option parser */
115 struct cmdline_args {
116   char *server;
117   char *rp_realm;
118   char *target_realm;
119   char *community;
120   int port; /* optional */
121 };
122
123 /* parser for individual options - fills in a struct cmdline_args */
124 static error_t parse_option(int key, char *arg, struct argp_state *state)
125 {
126   /* get a shorthand to the command line argument structure, part of state */
127   struct cmdline_args *arguments=state->input;
128
129   switch (key) {
130   case ARGP_KEY_ARG: /* handle argument (not option) */
131     switch (state->arg_num) {
132     case 0:
133       arguments->server=arg;
134       break;
135
136     case 1:
137       arguments->rp_realm=arg;
138       break;
139
140     case 2:
141       arguments->target_realm=arg;
142       break;
143
144     case 3:
145       arguments->community=arg;
146       break;
147
148     case 4:
149       arguments->port=strtol(arg, NULL, 10); /* optional */
150       break;
151
152     default:
153       /* too many arguments */
154       argp_usage(state);
155     }
156     break;
157
158   case ARGP_KEY_END: /* no more arguments */
159     if (state->arg_num < 4) {
160       /* not enough arguments encountered */
161       argp_usage(state);
162     }
163     break;
164
165   case 'v':
166     print_version_info();
167     exit(0);
168
169   default:
170     return ARGP_ERR_UNKNOWN;
171   }
172
173   return 0; /* success */
174 }
175
176 /* assemble the argp parser */
177 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
178
179 int main (int argc, 
180           char *argv[]) 
181 {
182   TIDC_INSTANCE *tidc;
183   int conn = 0;
184   int rc;
185   gss_ctx_id_t gssctx;
186   struct cmdline_args opts;
187
188   /* parse the command line*/
189   /* set defaults */
190   opts.server=NULL;
191   opts.rp_realm=NULL;
192   opts.target_realm=NULL;
193   opts.community=NULL;
194   opts.port=TID_PORT;
195
196   argp_parse(&argp, argc, argv, 0, 0, &opts);
197   /* TBD -- validity checking, dealing with quotes, etc. */
198
199   print_version_info();
200
201   /* Use standalone logging */
202   tr_log_open();
203
204   /* set logging levels */
205   talloc_set_log_stderr();
206   tr_log_threshold(LOG_CRIT);
207   tr_console_threshold(LOG_DEBUG);
208
209   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);
210  
211   /* Create a TID client instance & the client DH */
212   tidc = tidc_create();
213   if (NULL == (tidc->client_dh = tr_create_dh_params(NULL, 0))) {
214     printf("Error creating client DH params.\n");
215     return 1;
216   }
217
218   /* Set-up TID connection */
219   if (-1 == (conn = tidc_open_connection(tidc, opts.server, opts.port, &gssctx))) {
220     /* Handle error */
221     printf("Error in tidc_open_connection.\n");
222     return 1;
223   };
224
225   /* Send a TID request */
226   if (0 > (rc = tidc_send_request(tidc, conn, gssctx, opts.rp_realm, opts.target_realm, opts.community, 
227                                   &tidc_resp_handler, NULL))) {
228     /* Handle error */
229     printf("Error in tidc_send_request, rc = %d.\n", rc);
230     return 1;
231   }
232     
233   /* Clean-up the TID client instance, and exit */
234   tidc_destroy(tidc);
235
236   return 0;
237 }
238