088a2d641d50e2b407cc3f6616edc20e3267c112
[trust_router.git] / tr / trpc_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 <tr_trp.h>
43 #include <trust_router/tr_dh.h>
44
45 #if 0
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
55   printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
56
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");
60     return;
61   }
62
63   if (!resp->servers) {
64     fprintf(stderr, "tidc_resp_handler: Response does not contain server info.\n");
65     return;
66   }
67   
68   if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf, 
69                                       resp->servers->aaa_server_dh->pub_key, 
70                                       req->tidc_dh))) {
71     
72     printf("tidc_resp_handler: Error computing client key.\n");
73     return;
74   }
75   
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]); 
80   }
81   printf("\n");
82
83   return;
84 }
85 #endif
86
87 /* command-line option setup */
88
89 /* argp global parameters */
90 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
91
92 /* doc strings */
93 static const char doc[]=PACKAGE_NAME " - TRP Client";
94 static const char arg_doc[]="<message> <server> [<port>]"; /* string describing arguments, if any */
95
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[] = {
99   { NULL }
100 };
101
102 /* structure for communicating with option parser */
103 struct cmdline_args {
104   char *msg;
105   char *server;
106   int port; /* optional */
107 };
108
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)
111 {
112   /* get a shorthand to the command line argument structure, part of state */
113   struct cmdline_args *arguments=state->input;
114
115   switch (key) {
116   case ARGP_KEY_ARG: /* handle argument (not option) */
117     switch (state->arg_num) {
118     case 0:
119       arguments->msg=arg;
120       break;
121
122     case 1:
123       arguments->server=arg;
124       break;
125
126     case 2:
127       arguments->port=strtol(arg, NULL, 10); /* optional */
128       break;
129
130     default:
131       /* too many arguments */
132       argp_usage(state);
133     }
134     break;
135
136   case ARGP_KEY_END: /* no more arguments */
137     if (state->arg_num < 2) {
138       /* not enough arguments encountered */
139       argp_usage(state);
140     }
141     break;
142
143   default:
144     return ARGP_ERR_UNKNOWN;
145   }
146
147   return 0; /* success */
148 }
149
150 /* assemble the argp parser */
151 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
152
153 int main (int argc, 
154           char *argv[]) 
155 {
156   TALLOC_CTX *main_ctx=talloc_new(NULL);
157   TRPC_INSTANCE *trpc=NULL;
158   int conn = 0;
159   int rc;
160   gss_ctx_id_t gssctx;
161   struct cmdline_args opts;
162
163   /* parse the command line*/
164   /* set defaults */
165   opts.msg=NULL;
166   opts.server=NULL;
167   opts.port=TRP_PORT;
168
169   argp_parse(&argp, argc, argv, 0, 0, &opts);
170   /* TBD -- validity checking, dealing with quotes, etc. */
171
172   /* Use standalone logging */
173   tr_log_open();
174
175   /* set logging levels */
176   talloc_set_log_stderr();
177   tr_log_threshold(LOG_CRIT);
178   tr_console_threshold(LOG_DEBUG);
179
180   printf("TRPC Client:\nServer = %s, port = %i\n", opts.server, opts.port);
181  
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");
186     return 1;
187   }
188
189   /* Set-up TRP connection */
190   if (-1 == (conn = trpc_open_connection(trpc, opts.server, opts.port, &gssctx))) {
191     /* Handle error */
192     printf("Error in trpc_open_connection.\n");
193     return 1;
194   };
195
196   /* Send a TRP message */
197   if (0 > (rc = trpc_send_msg(trpc, conn, gssctx, opts.msg, NULL, NULL))) {
198     /* Handle error */
199     printf("Error in trpc_send_request, rc = %d.\n", rc);
200     return 1;
201   }
202     
203   /* Clean-up the TRP client instance, and exit */
204   trpc_destroy(trpc);
205
206   return 0;
207 }
208