Fixed warnings, fixed bugs in key generation code.
[trust_router.git] / tid / tids.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 <unistd.h>
37 #include <stdio.h>
38 #include <errno.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <jansson.h>
42
43 #include <gsscon.h>
44 #include <tr_msg.h>
45 #include <trust_router/tid.h>
46
47 static int tids_listen (TIDS_INSTANCE *tids, int port) 
48 {
49     int rc = 0;
50     int conn = -1;
51     struct sockaddr_storage addr;
52     struct sockaddr_in *saddr = (struct sockaddr_in *) &addr;
53     
54     saddr->sin_port = htons (port);
55     saddr->sin_family = AF_INET;
56     saddr->sin_addr.s_addr = INADDR_ANY;
57
58     if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
59       return conn;
60         
61     if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
62       return rc;
63         
64     if (0 > (rc = listen(conn, 512)))
65       return rc;
66     
67     fprintf (stdout, "TID Server listening on port %d\n", port);
68     return conn; 
69 }
70
71 static int tids_auth_connection (int conn, gss_ctx_id_t *gssctx)
72 {
73   int rc = 0;
74   int auth, autherr = 0;
75
76   if (rc = gsscon_passive_authenticate(conn, gssctx)) {
77     fprintf(stderr, "Error from gsscon_passive_authenticate(), rc = %d.\n", rc);
78     return -1;
79   }
80
81   if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
82     fprintf(stderr, "Error from gsscon_authorize, rc = %d, autherr = %d.\n", 
83             rc, autherr);
84     return -1;
85   }
86
87   if (auth)
88     fprintf(stdout, "Connection authenticated, conn = %d.\n", conn);
89   else
90     fprintf(stderr, "Authentication failed, conn %d.\n", conn);
91
92   return auth;
93 }
94
95 static int tids_read_request (TIDS_INSTANCE *tids, int conn, gss_ctx_id_t *gssctx, TR_MSG **mreq)
96 {
97   int err;
98   char *buf;
99   size_t buflen = 0;
100
101   if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
102     if (buf)
103       free(buf);
104     return -1;
105   }
106
107   fprintf(stdout, "tids_read_request():Request Received, %d bytes.\n", buflen);
108
109   /* Parse request */
110   if (NULL == ((*mreq) = tr_msg_decode(buf, buflen))) {
111     printf("tids_read_request():Error decoding request.\n");
112     free (buf);
113     return -1;
114   }
115
116   /* If this isn't a TID Request, just drop it. */
117   if (TID_REQUEST != (*mreq)->msg_type) {
118     printf("tids_read_request(): Not a TID Request, dropped.\n");
119     return -1;
120   }
121
122   free (buf);
123   return buflen;
124 }
125
126 static int tids_handle_request (TIDS_INSTANCE *tids, TR_MSG *mreq, TR_MSG **mresp) 
127 {
128   int rc;
129   TID_RESP *resp;
130
131   /* Check that this is a valid TID Request.  If not, send an error return. */
132   if ((!mreq->tid_req) ||
133       (!mreq->tid_req->rp_realm) ||
134       (!mreq->tid_req->realm) ||
135       (!mreq->tid_req->comm)) {
136     printf("tids_handle_request():Not a valid TID Request.\n");
137     (*mresp)->tid_resp->result = TID_ERROR;
138     (*mresp)->tid_resp->err_msg = tr_new_name("Bad request format");
139     return -1;
140   }
141
142   /* Call the caller's request handler */
143   /* TBD -- Handle different error returns/msgs */
144   resp = (*mresp)->tid_resp;
145   if (0 > (rc = (*tids->req_handler)(tids, mreq->tid_req, &resp, tids->cookie))) {
146     /* set-up an error response */
147     (*mresp)->tid_resp->result = TID_ERROR;
148     if (!(*mresp)->tid_resp->err_msg)   /* Use msg set by handler, if any */
149       (*mresp)->tid_resp->err_msg = tr_new_name("Internal processing error");
150   }
151   else {
152     /* set-up a success response */
153     (*mresp)->tid_resp->result = TID_SUCCESS;
154     (*mresp)->tid_resp->err_msg = NULL; /* No msg on successful return */
155   }
156     
157   return rc;
158 }
159
160 static int tids_send_response (TIDS_INSTANCE *tids, int conn, gss_ctx_id_t *gssctx, TR_MSG *mresp)
161 {
162   int err;
163   char *resp_buf;
164
165   if (NULL == (resp_buf = tr_msg_encode(mresp))) {
166     fprintf(stderr, "Error decoding json response.\n");
167     return -1;
168   }
169
170   printf("Encoded response:\n%s\n", resp_buf);
171   
172   /* Send the response over the connection */
173   if (err = gsscon_write_encrypted_token (conn, *gssctx, resp_buf, 
174                                           strlen(resp_buf) + 1)) {
175     fprintf(stderr, "Error sending response over connection.\n");
176     return -1;
177   }
178
179   free(resp_buf);
180
181   return 0;
182 }
183
184 static void tids_handle_connection (TIDS_INSTANCE *tids, int conn)
185 {
186   TR_MSG *mreq = NULL;
187   TR_MSG *mresp = NULL;
188   int rc = 0;
189   gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
190
191   if (!tids_auth_connection(conn, &gssctx)) {
192     fprintf(stderr, "Error authorizing TID Server connection, rc = %d.\n", rc);
193     close(conn);
194     return;
195   }
196
197   printf("Connection authorized!\n");
198
199   while (1) {   /* continue until an error breaks us out */
200
201     if (0 > (rc = tids_read_request(tids, conn, &gssctx, &mreq))) {
202       fprintf(stderr, "Error from tids_read_request(), rc = %d.\n", rc);
203       return;
204     } else if (0 == rc) {
205       continue;
206     }
207
208     /* Allocate a response structure and populate common fields */
209     if ((NULL == (mresp = malloc(sizeof(TR_MSG)))) ||
210         (NULL == (mresp->tid_resp = malloc(sizeof(TID_RESP))))) {
211       fprintf(stderr, "Error allocating response structure.\n");
212       return;
213     }
214
215     mresp->msg_type = TID_RESPONSE;
216     memset(mresp->tid_resp, 0, sizeof(TID_RESP));
217
218     /* TBD -- handle errors */
219     mresp->tid_resp->result = TID_SUCCESS; /* presume success */
220     mresp->tid_resp->rp_realm = tr_dup_name(mreq->tid_req->rp_realm);
221     mresp->tid_resp->realm = tr_dup_name(mreq->tid_req->realm);
222     mresp->tid_resp->comm = tr_dup_name(mreq->tid_req->comm);
223     if (mreq->tid_req->orig_coi)
224       mresp->tid_resp->orig_coi = tr_dup_name(mreq->tid_req->orig_coi);
225
226     if (0 > (rc = tids_handle_request(tids, mreq, &mresp))) {
227       fprintf(stderr, "Error from tids_handle_request(), rc = %d.\n", rc);
228       return;
229     }
230
231     if (0 > (rc = tids_send_response(tids, conn, &gssctx, mresp))) {
232       fprintf(stderr, "Error from tids_send_response(), rc = %d.\n", rc);
233       return;
234     }
235   }  
236
237   return;
238 }
239
240 TIDS_INSTANCE *tids_create (void)
241 {
242   TIDS_INSTANCE *tids = 0;
243   if (tids = malloc(sizeof(TIDS_INSTANCE)))
244     memset(tids, 0, sizeof(TIDS_INSTANCE));
245   return tids;
246 }
247
248 int tids_start (TIDS_INSTANCE *tids, 
249                 TIDS_REQ_FUNC *req_handler,
250                 void *cookie)
251 {
252   int listen = -1;
253   int conn = -1;
254   pid_t pid;
255   int optval = 1;
256
257   if (0 > (listen = tids_listen(tids, TID_PORT)))
258     perror ("Error from tids_listen()");
259
260   setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
261
262   /* store the caller's request handler & cookie */
263   tids->req_handler = req_handler;
264   tids->cookie = cookie;
265
266   while(1) {    /* accept incoming conns until we are stopped */
267
268     if (0 > (conn = accept(listen, NULL, NULL))) {
269       perror("Error from TIDS Server accept()");
270       return 1;
271     }
272
273     if (0 > (pid = fork())) {
274       perror("Error on fork()");
275       return 1;
276     }
277
278     if (pid == 0) {
279       close(listen);
280       tids_handle_connection(tids, conn);
281       close(conn);
282       exit(0);
283     } else {
284       close(conn);
285     }
286   }
287
288   return 1;     /* should never get here */
289 }
290
291 void tids_destroy (TIDS_INSTANCE *tids)
292 {
293   free(tids);
294 }
295
296