Enable warnings; fix many of them
[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 <trust_router/tid.h>
45
46
47 static int tids_listen (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 (int conn, gss_ctx_id_t *gssctx, TID_REQ *req)
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, "Request Received, %u bytes.\n", (unsigned) buflen);
108
109   /* Parse request -- TBD */
110
111   if (buf)
112     free(buf);
113
114   return buflen;
115 }
116
117 static int tids_handle_request (TID_REQ *req, TID_RESP *resp) 
118 {
119   return 0;
120 }
121
122 static int tids_send_response (int conn, gss_ctx_id_t *gssctx, TID_RESP *resp)
123 {
124   json_t *jreq;
125   int err;
126   char *resp_buf;
127
128   /* Create a json TID response */
129   if (NULL == (jreq = json_object())) {
130     fprintf(stderr,"Error creating json object.\n");
131     return -1;
132   }
133
134   if (0 > (err = json_object_set_new(jreq, "type", json_string("tid_response")))) {
135     fprintf(stderr, "Error adding type to response.\n");
136     return -1;
137   }
138   if (0 > (err = json_object_set_new(jreq, "result", json_string("error")))) {
139     fprintf(stderr, "Error adding result to response.\n");
140     return -1;
141   }
142   if (0 > (err = json_object_set_new(jreq, "msg", json_string("No path to realm")))) {
143     fprintf(stderr, "Error adding msg to response.\n");
144     return -1;
145   }
146
147   /* Encode the json response */
148   if (NULL == (resp_buf = json_dumps(jreq, 0))) {
149     fprintf(stderr, "Error encoding json response.\n");
150     return -1;
151   }
152   
153   printf("Encoded response:\n%s\n", resp_buf);
154   
155   /* Send the request over the connection */
156   if (err = gsscon_write_encrypted_token (conn, *gssctx, resp_buf, 
157                                           strlen(resp_buf) + 1)) {
158     fprintf(stderr, "Error sending request over connection.\n");
159     return -1;
160   }
161
162   free(resp_buf);
163
164   return 0;
165
166 }
167
168 static void tids_handle_connection (int conn)
169 {
170   TID_REQ req;
171   TID_RESP resp;
172   int rc;
173   gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
174
175   if (!tids_auth_connection(conn, &gssctx)) {
176     fprintf(stderr, "Error authorizing TID Server connection, rc = %d.\n", rc);
177     close(conn);
178     return;
179   }
180
181   printf("Connection authorized!\n");
182
183   while (1) {   /* continue until an error breaks us out */
184
185     if (0 > (rc = tids_read_request(conn, &gssctx, &req))) {
186       fprintf(stderr, "Error from tids_read_request(), rc = %d.\n", rc);
187       return;
188     } else if (0 == rc) {
189       continue;
190     }
191
192     if (0 > (rc = tids_handle_request(&req, &resp))) {
193       fprintf(stderr, "Error from tids_handle_request(), rc = %d.\n", rc);
194       return;
195     }
196
197     if (0 > (rc = tids_send_response(conn, &gssctx, &resp))) {
198       fprintf(stderr, "Error from tids_send_response(), rc = %d.\n", rc);
199       return;
200     }
201   }  
202
203   return;
204 }
205
206 TIDS_INSTANCE *tids_create (void)
207 {
208   TIDS_INSTANCE *tids = 0;
209   if (tids = malloc(sizeof(TIDS_INSTANCE)))
210     memset(tids, 0, sizeof(TIDS_INSTANCE));
211   return tids;
212 }
213
214 int tids_start (TIDS_INSTANCE *tids, 
215                 TIDS_REQ_FUNC *req_handler,
216                 void *cookie)
217 {
218   int listen = -1;
219   int conn = -1;
220   pid_t pid;
221
222   if (0 > (listen = tids_listen(TID_PORT)))
223     perror ("Error from tids_listen()");
224
225   while(1) {    /* accept incoming conns until we are stopped */
226
227     if (0 > (conn = accept(listen, NULL, NULL))) {
228       perror("Error from TIDS Server accept()");
229       return 1;
230     }
231
232     if (0 > (pid = fork())) {
233       perror("Error on fork()");
234       return 1;
235     }
236
237     if (pid == 0) {
238       close(listen);
239       tids_handle_connection(conn);
240       close(conn);
241       exit(0);
242     } else {
243       close(conn);
244     }
245   }
246
247   return 1;     /* should never get here */
248 }
249
250 void tids_destroy (TIDS_INSTANCE *tids)
251 {
252   free(tids);
253 }
254
255