Send TPQ resoonse from server, receive on client.
[trust_router.git] / tpq / tpqc.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 <jansson.h>
37
38 #include <gsscon.h>
39 #include <tpq.h>
40
41 TPQC_INSTANCE *tpqc_create ()
42 {
43   TPQC_INSTANCE *tpqc = NULL;
44
45   if (tpqc = malloc(sizeof(TPQC_INSTANCE)))
46     memset(tpqc, 0, sizeof(TPQC_INSTANCE));
47
48   return tpqc;
49 }
50
51 int tpqc_open_connection (TPQC_INSTANCE *tpqc, 
52                           char *server,
53                           gss_ctx_id_t *gssctx)
54 {
55   int err = 0;
56   int conn = -1;
57
58   err = gsscon_connect(server, TPQ_PORT, &conn);
59
60   if (!err)
61     err = gsscon_active_authenticate(conn, NULL, "trustquery", gssctx);
62
63   if (!err)
64     return conn;
65   else
66     return -1;
67 }
68
69 int tpqc_send_request (TPQC_INSTANCE *tpqc, 
70                        int conn, 
71                        gss_ctx_id_t gssctx,
72                        char *realm, 
73                        char *coi,
74                        TPQC_RESP_FUNC *resp_handler,
75                        void *cookie)
76
77 {
78   json_t *jreq;
79   int err;
80   char *req_buf;
81   char *resp_buf;
82   size_t resp_buflen = 0;
83
84   /* Create a json TPQ request */
85   if (NULL == (jreq = json_object())) {
86     fprintf(stderr,"Error creating json object.\n");
87     return -1;
88   }
89
90   if (0 > (err = json_object_set_new(jreq, "type", json_string("tpq_request")))) {
91     fprintf(stderr, "Error adding type to request.\n");
92     return -1;
93   }
94
95   /* Insert realm and coi into the json request */
96   if (0 > (err = json_object_set_new(jreq, "realm", json_string(realm)))) {
97     fprintf(stderr, "Error adding realm to request.\n");
98     return -1;
99   }
100   if (0 > (err = json_object_set_new(jreq, "coi", json_string(coi)))) {
101     fprintf(stderr, "Error adding coi to request.\n");
102     return -1;
103   }
104
105   /* Generate half of a D-H exchange -- TBD */
106   /* Insert D-H information into the request -- TBD */
107
108   /* Encode the json request */
109   if (NULL == (req_buf = json_dumps(jreq, 0))) {
110     fprintf(stderr, "Error encoding json request.\n");
111     return -1;
112   }
113   
114   printf("Encoded request:\n%s\n", req_buf);
115   
116   /* Send the request over the connection */
117   if (err = gsscon_write_encrypted_token (conn, gssctx, req_buf, 
118                                           strlen(req_buf) + 1)) {
119     fprintf(stderr, "Error sending request over connection.\n");
120     return -1;
121   }
122
123   free(req_buf);
124
125   /* read the response from the connection */
126
127   if (err = gsscon_read_encrypted_token(conn, gssctx, &resp_buf, &resp_buflen)) {
128     if (resp_buf)
129       free(resp_buf);
130     return -1;
131   }
132
133   fprintf(stdout, "Response Received, %d bytes.\n", resp_buflen);
134
135   /* Parse response -- TBD */
136
137   /* Call the caller's response function */
138   (*resp_handler)(tpqc, NULL, cookie);
139
140   if (resp_buf)
141     free(resp_buf);
142
143   return 0;
144 }
145
146 void tpqc_destroy (TPQC_INSTANCE *tpqc)
147 {
148   if (tpqc)
149     free(tpqc);
150 }
151
152
153
154