Explicitly call tr_log_open when we're not being used as a library
[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
39 #include <gsscon.h>
40 #include <tr_debug.h>
41 #include <tid_internal.h>
42 #include <trust_router/tr_dh.h>
43
44 void static tidc_print_usage (const char *name)
45 {
46   printf("Usage: %s <server> <RP-realm> <target-realm> <community> [<port>]\n", name);
47 }
48
49 static void tidc_resp_handler (TIDC_INSTANCE * tidc, 
50                         TID_REQ *req,
51                         TID_RESP *resp, 
52                         void *cookie) 
53 {
54   int c_keylen = 0;
55   unsigned char *c_keybuf = NULL;
56   int i;
57
58   printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
59
60   /* Generate the client key -- TBD, handle more than one server */
61   if (TID_SUCCESS != resp->result) {
62     fprintf(stderr, "tidc_resp_handler: Response is an error.\n");
63     return;
64   }
65
66   if (!resp->servers) {
67     fprintf(stderr, "tidc_resp_handler: Response does not contain server info.\n");
68     return;
69   }
70   
71   if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf, 
72                                       resp->servers->aaa_server_dh->pub_key, 
73                                       req->tidc_dh))) {
74     
75     printf("tidc_resp_handler: Error computing client key.\n");
76     return;
77   }
78   
79   /* Print out the client key. */
80   printf("Client Key Generated (len = %d):\n", c_keylen);
81   for (i = 0; i < c_keylen; i++) {
82     printf("%.2x", c_keybuf[i]); 
83   }
84   printf("\n");
85
86   return;
87 }
88
89 int main (int argc, 
90           const char *argv[]) 
91 {
92   TIDC_INSTANCE *tidc;
93   char *server = NULL;
94   char *rp_realm = NULL;
95   char *realm = NULL;
96   char *coi = NULL;
97   int port = TID_PORT;
98   int conn = 0;
99   int rc;
100   gss_ctx_id_t gssctx;
101
102   /* Use standalone logging */
103   tr_log_open();
104
105   /* set logging levels */
106   talloc_set_log_stderr();
107   tr_log_threshold(LOG_CRIT);
108   tr_console_threshold(LOG_DEBUG);
109
110   /* Parse command-line arguments */ 
111   if (argc < 5 || argc > 6) {
112     tidc_print_usage(argv[0]);
113     exit(1);
114   }
115
116   /* TBD -- validity checking, dealing with quotes, etc. */
117   server = (char *)argv[1];
118   rp_realm = (char *) argv[2];
119   realm = (char *)argv[3];
120   coi = (char *)argv[4];
121
122   if (argc > 5) {
123     port = strtol(argv[5], NULL, 10);
124   }
125
126   printf("TIDC Client:\nServer = %s, rp_realm = %s, target_realm = %s, community = %s, port = %i\n", server, rp_realm, realm, coi, port);
127  
128   /* Create a TID client instance & the client DH */
129   tidc = tidc_create();
130   if (NULL == (tidc->client_dh = tr_create_dh_params(NULL, 0))) {
131     printf("Error creating client DH params.\n");
132     return 1;
133   }
134
135   /* Set-up TID connection */
136   if (-1 == (conn = tidc_open_connection(tidc, server, port, &gssctx))) {
137     /* Handle error */
138     printf("Error in tidc_open_connection.\n");
139     return 1;
140   };
141
142   /* Send a TID request */
143   if (0 > (rc = tidc_send_request(tidc, conn, gssctx, rp_realm, realm, coi, 
144                                   &tidc_resp_handler, NULL))) {
145     /* Handle error */
146     printf("Error in tidc_send_request, rc = %d.\n", rc);
147     return 1;
148   }
149     
150   /* Clean-up the TID client instance, and exit */
151   tidc_destroy(tidc);
152
153   return 0;
154 }
155