96e1d073bcf2c80f991d21459bcdea80c7055dca
[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
45 static void tidc_resp_handler (TIDC_INSTANCE * tidc, 
46                         TID_REQ *req,
47                         TID_RESP *resp, 
48                         void *cookie) 
49 {
50   int c_keylen = 0;
51   unsigned char *c_keybuf = NULL;
52   int i;
53
54   printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
55
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");
59     return;
60   }
61
62   if (!resp->servers) {
63     fprintf(stderr, "tidc_resp_handler: Response does not contain server info.\n");
64     return;
65   }
66   
67   if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf, 
68                                       resp->servers->aaa_server_dh->pub_key, 
69                                       req->tidc_dh))) {
70     
71     printf("tidc_resp_handler: Error computing client key.\n");
72     return;
73   }
74   
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]); 
79   }
80   printf("\n");
81
82   return;
83 }
84
85
86 /* command-line option setup */
87
88 /* argp global parameters */
89 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
90
91 /* doc strings */
92 static const char doc[]=PACKAGE_NAME " - TID Client";
93 static const char arg_doc[]="<server> <RP-realm> <target-realm> <community> [<port>]"; /* string describing arguments, if any */
94
95 /* define the options here. Fields are:
96  * { long-name, short-name, variable name, options, help description } */
97 static const struct argp_option cmdline_options[] = {
98   { NULL }
99 };
100
101 /* structure for communicating with option parser */
102 struct cmdline_args {
103   char *server;
104   char *rp_realm;
105   char *target_realm;
106   char *community;
107   int port; /* optional */
108 };
109
110 /* parser for individual options - fills in a struct cmdline_args */
111 static error_t parse_option(int key, char *arg, struct argp_state *state)
112 {
113   /* get a shorthand to the command line argument structure, part of state */
114   struct cmdline_args *arguments=state->input;
115
116   switch (key) {
117   case ARGP_KEY_ARG: /* handle argument (not option) */
118     switch (state->arg_num) {
119     case 0:
120       arguments->server=arg;
121       break;
122
123     case 1:
124       arguments->rp_realm=arg;
125       break;
126
127     case 2:
128       arguments->target_realm=arg;
129       break;
130
131     case 3:
132       arguments->community=arg;
133       break;
134
135     case 4:
136       arguments->port=strtol(arg, NULL, 10); /* optional */
137       break;
138
139     default:
140       /* too many arguments */
141       argp_usage(state);
142     }
143     break;
144
145   case ARGP_KEY_END: /* no more arguments */
146     if (state->arg_num < 4) {
147       /* not enough arguments encountered */
148       argp_usage(state);
149     }
150     break;
151
152   default:
153     return ARGP_ERR_UNKNOWN;
154   }
155
156   return 0; /* success */
157 }
158
159 /* assemble the argp parser */
160 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
161
162 int main (int argc, 
163           char *argv[]) 
164 {
165   TIDC_INSTANCE *tidc;
166   int conn = 0;
167   int rc;
168   gss_ctx_id_t gssctx;
169   struct cmdline_args opts;
170
171   /* parse the command line*/
172   /* set defaults */
173   opts.server=NULL;
174   opts.rp_realm=NULL;
175   opts.target_realm=NULL;
176   opts.community=NULL;
177   opts.port=TID_PORT;
178
179   argp_parse(&argp, argc, argv, 0, 0, &opts);
180   /* TBD -- validity checking, dealing with quotes, etc. */
181
182   /* Use standalone logging */
183   tr_log_open();
184
185   /* set logging levels */
186   talloc_set_log_stderr();
187   tr_log_threshold(LOG_CRIT);
188   tr_console_threshold(LOG_DEBUG);
189
190   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);
191  
192   /* Create a TID client instance & the client DH */
193   tidc = tidc_create();
194   if (NULL == (tidc->client_dh = tr_create_dh_params(NULL, 0))) {
195     printf("Error creating client DH params.\n");
196     return 1;
197   }
198
199   /* Set-up TID connection */
200   if (-1 == (conn = tidc_open_connection(tidc, opts.server, opts.port, &gssctx))) {
201     /* Handle error */
202     printf("Error in tidc_open_connection.\n");
203     return 1;
204   };
205
206   /* Send a TID request */
207   if (0 > (rc = tidc_send_request(tidc, conn, gssctx, opts.rp_realm, opts.target_realm, opts.community, 
208                                   &tidc_resp_handler, NULL))) {
209     /* Handle error */
210     printf("Error in tidc_send_request, rc = %d.\n", rc);
211     return 1;
212   }
213     
214   /* Clean-up the TID client instance, and exit */
215   tidc_destroy(tidc);
216
217   return 0;
218 }
219