3e567cef2a4ecf7cba2e63c584cbc8854cd1bfae
[trust_router.git] / mon / monc.c
1 /*
2  * Copyright (c) 2012, 2014-2018, 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 <stdio.h>
36 #include <jansson.h>
37 #include <talloc.h>
38
39 #include <trust_router/tr_dh.h>
40 #include <mon_internal.h>
41 #include <tr_msg.h>
42 #include <gsscon.h>
43 #include <tr_debug.h>
44
45
46 static int monc_destructor(void *obj)
47 {
48   MONC_INSTANCE *monc=talloc_get_type_abort(obj, MONC_INSTANCE);
49   if (NULL!=monc) {
50     if (NULL!=monc->client_dh)
51       tr_destroy_dh_params(monc->client_dh);
52   }
53   return 0;
54 }
55
56 /* creates struct in talloc null context */
57 MONC_INSTANCE *monc_create(void)
58 {
59   MONC_INSTANCE *monc=talloc(NULL, MONC_INSTANCE);
60   if (monc!=NULL) {
61     monc->client_dh=NULL;
62     talloc_set_destructor((void *)monc, monc_destructor);
63   }
64   return monc;
65 }
66
67 void monc_destroy(MONC_INSTANCE *monc)
68 {
69   talloc_free(monc);
70 }
71
72 int monc_open_connection (MONC_INSTANCE *monc,
73                           const char *server,
74                           unsigned int port,
75                           gss_ctx_id_t *gssctx)
76 {
77   int err = 0;
78   int conn = -1;
79
80   tr_debug("monc_open_connection: opening monc connection to %s:%d", server, port);
81   err = gsscon_connect(server, port, "trustmonitor", &conn, gssctx);
82
83   if (!err)
84     return conn;
85   else
86     return -1;
87 }
88
89 int monc_send_request (MONC_INSTANCE *monc,
90                        int conn,
91                        gss_ctx_id_t gssctx,
92                        MONC_RESP_FUNC *resp_handler,
93                        void *cookie)
94 {
95   MON_REQ *mon_req = NULL;
96   int rc;
97
98   /* Create and populate a MON req structure */
99   if (!(mon_req = mon_req_new(NULL, MON_CMD_SHOW))) // TODO accept command as a parameter
100     goto error;
101
102   rc = monc_fwd_request(monc, conn, gssctx, mon_req, resp_handler, cookie);
103   goto cleanup;
104 error:
105   rc = -1;
106 cleanup:
107   mon_req_free(mon_req);
108   return rc;
109 }
110
111 int monc_fwd_request(MONC_INSTANCE *monc,
112                      int conn,
113                      gss_ctx_id_t gssctx,
114                      MON_REQ *mon_req,
115                      MONC_RESP_FUNC *resp_handler,
116                      void *cookie)
117 {
118   char *req_buf = NULL;
119   char *resp_buf = NULL;
120   size_t resp_buflen = 0;
121   TR_MSG *msg = NULL;
122   TR_MSG *resp_msg = NULL;
123   int err;
124   int rc = 0;
125
126   /* Create and populate a MON msg structure */
127   if (!(msg = talloc_zero(mon_req, TR_MSG)))
128     goto error;
129
130   msg->msg_type = MON_REQUEST;
131   tr_msg_set_mon_req(msg, mon_req);
132
133   /* store the response function and cookie */
134   // mon_req->resp_func = resp_handler;
135   // mon_req->cookie = cookie;
136
137
138   /* Encode the request into a json string */
139   if (!(req_buf = tr_msg_encode(NULL, msg))) {
140     tr_err("monc_fwd_request: Error encoding MON request.\n");
141     goto error;
142   }
143
144   tr_debug( "monc_fwd_request: Sending MON request:\n");
145   tr_debug( "%s\n", req_buf);
146
147   /* Send the request over the connection */
148   err = gsscon_write_encrypted_token (conn, gssctx, req_buf, strlen(req_buf));
149   if (err) {
150     tr_err( "monc_fwd_request: Error sending request over connection.\n");
151     goto error;
152   }
153
154   /* TBD -- queue request on instance, read resps in separate thread */
155
156   /* Read the response from the connection */
157   /* TBD -- timeout? */
158   if (err = gsscon_read_encrypted_token(conn, gssctx, &resp_buf, &resp_buflen)) {
159     if (resp_buf)
160       free(resp_buf);
161     goto error;
162   }
163
164   tr_debug( "monc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
165   tr_debug( "%s\n", resp_buf);
166
167 //  if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
168 //    tr_err( "monc_fwd_request: Error decoding response.\n");
169 //    goto error;
170 //  }
171 //
172 //  /* TBD -- Check if this is actually a valid response */
173 //  if (MON_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
174 //    tr_err( "monc_fwd_request: Error, no response in the response!\n");
175 //    goto error;
176 //  }
177 //
178 //  if (resp_handler) {
179 //    /* Call the caller's response function. It must copy any data it needs before returning. */
180 //    tr_debug("monc_fwd_request: calling response callback function.");
181 //    (*resp_handler)(monc, mon_req, tr_msg_get_resp(resp_msg), cookie);
182 //  }
183
184   goto cleanup;
185
186 error:
187   rc = -1;
188 cleanup:
189   if (msg)
190     talloc_free(msg);
191   if (req_buf)
192     free(req_buf);
193   if (resp_buf)
194     free(resp_buf);
195   if (resp_msg)
196     tr_msg_free_decoded(resp_msg);
197   return rc;
198 }
199
200
201 DH * monc_get_dh(MONC_INSTANCE *inst)
202 {
203   return inst->client_dh;
204 }
205
206 DH *monc_set_dh(MONC_INSTANCE *inst, DH *dh)
207 {
208   inst->client_dh = dh;
209   return dh;
210 }