Authenticate GSS context in separate thread. (Not fully working yet.)
[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
46 /* command-line option setup */
47
48 /* argp global parameters */
49 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
50
51 /* doc strings */
52 static const char doc[]=PACKAGE_NAME " - TRP Client";
53 static const char arg_doc[]="<message> <server> [<port>]"; /* string describing arguments, if any */
54
55 /* define the options here. Fields are:
56  * { long-name, short-name, variable name, options, help description } */
57 static const struct argp_option cmdline_options[] = {
58   { NULL }
59 };
60
61 /* structure for communicating with option parser */
62 struct cmdline_args {
63   char *msg;
64   char *server;
65   int port; /* optional */
66 };
67
68 /* parser for individual options - fills in a struct cmdline_args */
69 static error_t parse_option(int key, char *arg, struct argp_state *state)
70 {
71   /* get a shorthand to the command line argument structure, part of state */
72   struct cmdline_args *arguments=state->input;
73
74   switch (key) {
75   case ARGP_KEY_ARG: /* handle argument (not option) */
76     switch (state->arg_num) {
77     case 0:
78       arguments->msg=arg;
79       break;
80
81     case 1:
82       arguments->server=arg;
83       break;
84
85     case 2:
86       arguments->port=strtol(arg, NULL, 10); /* optional */
87       break;
88
89     default:
90       /* too many arguments */
91       argp_usage(state);
92     }
93     break;
94
95   case ARGP_KEY_END: /* no more arguments */
96     if (state->arg_num < 2) {
97       /* not enough arguments encountered */
98       argp_usage(state);
99     }
100     break;
101
102   default:
103     return ARGP_ERR_UNKNOWN;
104   }
105
106   return 0; /* success */
107 }
108
109 /* assemble the argp parser */
110 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
111
112 int main (int argc, 
113           char *argv[]) 
114 {
115   TALLOC_CTX *main_ctx=talloc_new(NULL);
116   TRPC_INSTANCE *trpc=NULL;
117   int conn = 0;
118   int rc;
119   gss_ctx_id_t gssctx;
120   struct cmdline_args opts;
121
122   /* parse the command line*/
123   /* set defaults */
124   opts.msg=NULL;
125   opts.server=NULL;
126   opts.port=TRP_PORT;
127
128   argp_parse(&argp, argc, argv, 0, 0, &opts);
129   /* TBD -- validity checking, dealing with quotes, etc. */
130
131   /* Use standalone logging */
132   tr_log_open();
133
134   /* set logging levels */
135   talloc_set_log_stderr();
136   tr_log_threshold(LOG_CRIT);
137   tr_console_threshold(LOG_DEBUG);
138
139   printf("TRPC Client:\nServer = %s, port = %i\n", opts.server, opts.port);
140  
141   /* Create a TRP client instance & the client DH */
142   trpc = trpc_new(main_ctx);
143   if (NULL == (trpc->client_dh = tr_create_dh_params(NULL, 0))) {
144     printf("Error creating client DH params.\n");
145     return 1;
146   }
147
148   /* Set-up TRP connection */
149   if (-1 == (conn = trpc_open_connection(trpc, opts.server, opts.port, &gssctx))) {
150     /* Handle error */
151     printf("Error in trpc_open_connection.\n");
152     return 1;
153   };
154
155   /* Send a TRP message */
156   if (0 > (rc = trpc_send_msg(trpc, conn, gssctx, opts.msg, NULL, NULL))) {
157     /* Handle error */
158     printf("Error in trpc_send_request, rc = %d.\n", rc);
159     return 1;
160   }
161     
162   /* Clean-up the TRP client instance, and exit */
163   trpc_free(trpc);
164
165   return 0;
166 }
167