Internal changes to reflect merge of tpq and tid protocols.
[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 <tid.h>
45
46 static int tids_listen (int port) 
47 {
48     int rc = 0;
49     int conn = -1;
50     struct sockaddr_storage addr;
51     struct sockaddr_in *saddr = (struct sockaddr_in *) &addr;
52     
53     saddr->sin_port = htons (port);
54     saddr->sin_family = AF_INET;
55     saddr->sin_addr.s_addr = INADDR_ANY;
56
57     if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
58       return conn;
59         
60     if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
61       return rc;
62         
63     if (0 > (rc = listen(conn, 512)))
64       return rc;
65     
66     fprintf (stdout, "TID Server listening on port %d\n", port);
67     return conn; 
68 }
69
70 static int tids_auth_connection (int conn, gss_ctx_id_t *gssctx)
71 {
72   int rc = 0;
73   int auth, autherr = 0;
74
75   if (rc = gsscon_passive_authenticate(conn, gssctx)) {
76     fprintf(stderr, "Error from gsscon_passive_authenticate(), rc = %d.\n", rc);
77     return -1;
78   }
79
80   if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
81     fprintf(stderr, "Error from gsscon_authorize, rc = %d, autherr = %d.\n", 
82             rc, autherr);
83     return -1;
84   }
85
86   if (auth)
87     fprintf(stdout, "Connection authenticated, conn = %d.\n", conn);
88   else
89     fprintf(stderr, "Authentication failed, conn %d.\n", conn);
90
91   return auth;
92 }
93
94 static int tids_read_request (int conn, gss_ctx_id_t *gssctx, TID_REQ *req)
95 {
96   int err;
97   char *buf;
98   size_t buflen = 0;
99
100   if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
101     if (buf)
102       free(buf);
103     return -1;
104   }
105
106   fprintf(stdout, "Request Received, %d bytes.\n", buflen);
107
108   /* Parse request -- TBD */
109
110   if (buf)
111     free(buf);
112
113   return buflen;
114 }
115
116 static int tids_handle_request (TID_REQ *req, TID_RESP *resp) 
117 {
118   return 0;
119 }
120
121 static int tids_send_response (int conn, gss_ctx_id_t *gssctx, TID_RESP *resp)
122 {
123   json_t *jreq;
124   int err;
125   char *resp_buf;
126
127   /* Create a json TID response */
128   if (NULL == (jreq = json_object())) {
129     fprintf(stderr,"Error creating json object.\n");
130     return -1;
131   }
132
133   if (0 > (err = json_object_set_new(jreq, "type", json_string("tid_response")))) {
134     fprintf(stderr, "Error adding type to response.\n");
135     return -1;
136   }
137   if (0 > (err = json_object_set_new(jreq, "result", json_string("error")))) {
138     fprintf(stderr, "Error adding result to response.\n");
139     return -1;
140   }
141   if (0 > (err = json_object_set_new(jreq, "msg", json_string("No path to realm")))) {
142     fprintf(stderr, "Error adding msg to response.\n");
143     return -1;
144   }
145
146   /* Encode the json response */
147   if (NULL == (resp_buf = json_dumps(jreq, 0))) {
148     fprintf(stderr, "Error encoding json response.\n");
149     return -1;
150   }
151   
152   printf("Encoded response:\n%s\n", resp_buf);
153   
154   /* Send the request over the connection */
155   if (err = gsscon_write_encrypted_token (conn, *gssctx, resp_buf, 
156                                           strlen(resp_buf) + 1)) {
157     fprintf(stderr, "Error sending request over connection.\n");
158     return -1;
159   }
160
161   free(resp_buf);
162
163   return 0;
164
165 }
166
167 static void tids_handle_connection (int conn)
168 {
169   TID_REQ req;
170   TID_RESP resp;
171   int rc;
172   gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
173
174   if (!tids_auth_connection(conn, &gssctx)) {
175     fprintf(stderr, "Error authorizing TID Server connection, rc = %d.\n", rc);
176     close(conn);
177     return;
178   }
179
180   printf("Connection authorized!\n");
181
182   while (1) {   /* continue until an error breaks us out */
183
184     if (0 > (rc = tids_read_request(conn, &gssctx, &req))) {
185       fprintf(stderr, "Error from tids_read_request(), rc = %d.\n", rc);
186       return;
187     } else if (0 == rc) {
188       continue;
189     }
190
191     if (0 > (rc = tids_handle_request(&req, &resp))) {
192       fprintf(stderr, "Error from tids_handle_request(), rc = %d.\n", rc);
193       return;
194     }
195
196     if (0 > (rc = tids_send_response(conn, &gssctx, &resp))) {
197       fprintf(stderr, "Error from tids_send_response(), rc = %d.\n", rc);
198       return;
199     }
200   }  
201
202   return;
203 }
204
205 TIDS_INSTANCE *tids_create ()
206 {
207   TIDS_INSTANCE *tids = 0;
208   if (tids = malloc(sizeof(TIDS_INSTANCE)))
209     memset(tids, 0, sizeof(TIDS_INSTANCE));
210   return tids;
211 }
212
213 int tids_start (TIDS_INSTANCE *tids, 
214                 TIDS_REQ_FUNC *req_handler,
215                 void *cookie)
216 {
217   int listen = -1;
218   int conn = -1;
219   pid_t pid;
220
221   if (0 > (listen = tids_listen(TID_PORT)))
222     perror ("Error from tids_listen()");
223
224   while(1) {    /* accept incoming conns until we are stopped */
225
226     if (0 > (conn = accept(listen, NULL, NULL))) {
227       perror("Error from TIDS Server accept()");
228       return 1;
229     }
230
231     if (0 > (pid = fork())) {
232       perror("Error on fork()");
233       return 1;
234     }
235
236     if (pid == 0) {
237       close(listen);
238       tids_handle_connection(conn);
239       close(conn);
240       exit(0);
241     } else {
242       close(conn);
243     }
244   }
245
246   return 1;     /* should never get here */
247 }
248
249 void tids_destroy (TIDS_INSTANCE *tids)
250 {
251   free(tids);
252 }
253
254