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