2 * Copyright (c) 2012, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
41 #include <sys/socket.h>
42 #include <netinet/in.h>
45 #include <trust_router/tid.h>
49 static TID_RESP *tids_create_response (TIDS_INSTANCE *tids, TID_REQ *req)
53 if ((NULL == (resp = calloc(sizeof(TID_RESP), 1)))) {
54 fprintf(stderr, "tids_create_response: Error allocating response structure.\n");
58 resp->result = TID_SUCCESS; /* presume success */
59 if ((NULL == (resp->rp_realm = tr_dup_name(req->rp_realm))) ||
60 (NULL == (resp->realm = tr_dup_name(req->realm))) ||
61 (NULL == (resp->comm = tr_dup_name(req->comm)))) {
62 fprintf(stderr, "tids_create_response: Error allocating fields in response.\n");
66 if (NULL == (resp->orig_coi = tr_dup_name(req->orig_coi))) {
67 fprintf(stderr, "tids_create_response: Error allocating fields in response.\n");
74 static void tids_destroy_response(TIDS_INSTANCE *tids, TID_RESP *resp)
78 tr_free_name(resp->err_msg);
80 tr_free_name(resp->rp_realm);
82 tr_free_name(resp->realm);
84 tr_free_name(resp->comm);
86 tr_free_name(resp->orig_coi);
91 static int tids_listen (TIDS_INSTANCE *tids, int port)
98 struct sockaddr_storage storage;
99 struct sockaddr_in in4;
102 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4;
104 saddr->sin_port = htons (port);
105 saddr->sin_family = AF_INET;
106 saddr->sin_addr.s_addr = INADDR_ANY;
108 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
111 setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
113 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
116 if (0 > (rc = listen(conn, 512)))
119 fprintf (stdout, "tids_listen: TID Server listening on port %d\n", port);
123 static int tids_auth_cb(gss_name_t clientName, gss_buffer_t displayName,
126 struct tids_instance *inst = (struct tids_instance *) data;
127 TR_NAME name ={(char *) displayName->value,
128 displayName->length};
129 return inst->auth_handler(clientName, &name, inst->cookie);
132 static int tids_auth_connection (struct tids_instance *inst,
133 int conn, gss_ctx_id_t *gssctx)
136 int auth, autherr = 0;
138 if (rc = gsscon_passive_authenticate(conn, gssctx, tids_auth_cb, inst)) {
139 fprintf(stderr, "tids_auth_connection: Error from gsscon_passive_authenticate(), rc = %d.\n", rc);
143 if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
144 fprintf(stderr, "tids_auth_connection: Error from gsscon_authorize, rc = %d, autherr = %d.\n",
150 fprintf(stdout, "tids_auth_connection: Connection authenticated, conn = %d.\n", conn);
152 fprintf(stderr, "tids_auth_connection: Authentication failed, conn %d.\n", conn);
157 static int tids_read_request (TIDS_INSTANCE *tids, int conn, gss_ctx_id_t *gssctx, TR_MSG **mreq)
163 if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
169 fprintf(stdout, "tids_read_request():Request Received, %u bytes.\n", (unsigned) buflen);
172 if (NULL == ((*mreq) = tr_msg_decode(buf, buflen))) {
173 fprintf(stderr, "tids_read_request():Error decoding request.\n");
178 /* If this isn't a TID Request, just drop it. */
179 if (TID_REQUEST != (*mreq)->msg_type) {
180 fprintf(stderr, "tids_read_request(): Not a TID Request, dropped.\n");
188 static int tids_handle_request (TIDS_INSTANCE *tids, TR_MSG *mreq, TID_RESP **resp)
192 /* Check that this is a valid TID Request. If not, send an error return. */
193 if ((!mreq->tid_req) ||
194 (!mreq->tid_req->rp_realm) ||
195 (!mreq->tid_req->realm) ||
196 (!mreq->tid_req->comm)) {
197 fprintf(stderr, "tids_handle_request():Not a valid TID Request.\n");
198 (*resp)->result = TID_ERROR;
199 (*resp)->err_msg = tr_new_name("Bad request format");
203 /* Call the caller's request handler */
204 /* TBD -- Handle different error returns/msgs */
205 if (0 > (rc = (*tids->req_handler)(tids, mreq->tid_req, &(*resp), tids->cookie))) {
206 /* set-up an error response */
207 (*resp)->result = TID_ERROR;
208 if (!(*resp)->err_msg) /* Use msg set by handler, if any */
209 (*resp)->err_msg = tr_new_name("Internal processing error");
212 /* set-up a success response */
213 (*resp)->result = TID_SUCCESS;
214 (*resp)->err_msg = NULL; /* No error msg on successful return */
220 int tids_send_err_response (TIDS_INSTANCE *tids, TID_REQ *req, const char *err_msg) {
221 TID_RESP *resp = NULL;
224 /* If we already sent a response, don't send another no matter what. */
228 if (NULL == (resp = tids_create_response(tids, req))) {
229 fprintf(stderr, "tids_send_err_response: Can't create response.\n");
233 /* mark this as an error response, and include the error message */
234 resp->result = TID_ERROR;
235 resp->err_msg = tr_new_name((char *)err_msg);
237 rc = tids_send_response(tids, req, resp);
239 tids_destroy_response(tids, resp);
243 int tids_send_response (TIDS_INSTANCE *tids, TID_REQ *req, TID_RESP *resp)
249 if ((!tids) || (!req) || (!resp))
250 fprintf (stderr, "tids_send_response: Invalid parameters.\n");
252 /* Never send a second response if we already sent one. */
256 mresp.msg_type = TID_RESPONSE;
257 mresp.tid_resp = resp;
259 if (NULL == (resp_buf = tr_msg_encode(&mresp))) {
260 fprintf(stderr, "tids_send_response: Error encoding json response.\n");
264 fprintf(stderr, "tids_send_response: Encoded response:\n%s\n", resp_buf);
266 /* Send the response over the connection */
267 if (err = gsscon_write_encrypted_token (req->conn, req->gssctx, resp_buf,
268 strlen(resp_buf) + 1)) {
269 fprintf(stderr, "tids_send_response: Error sending response over connection.\n");
273 /* indicate that a response has been sent for this request */
281 static void tids_handle_connection (TIDS_INSTANCE *tids, int conn)
284 TID_RESP *resp = NULL;
286 gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
288 if (tids_auth_connection(tids, conn, &gssctx)) {
289 fprintf(stderr, "tids_handle_connection: Error authorizing TID Server connection.\n");
294 fprintf(stdout, "tids_handle_connection: Connection authorized!\n");
296 while (1) { /* continue until an error breaks us out */
298 if (0 > (rc = tids_read_request(tids, conn, &gssctx, &mreq))) {
299 fprintf(stderr, "tids_handle_connection: Error from tids_read_request(), rc = %d.\n", rc);
301 } else if (0 == rc) {
305 /* Put connection information into the request structure */
306 mreq->tid_req->conn = conn;
307 mreq->tid_req->gssctx = gssctx;
309 /* Allocate a response structure and populate common fields */
310 if (NULL == (resp = tids_create_response (tids, mreq->tid_req))) {
311 fprintf(stderr, "tids_handle_connection: Error creating response structure.\n");
312 /* try to send an error */
313 tids_send_err_response(tids, mreq->tid_req, "Error creating response.\n");
317 if (0 > (rc = tids_handle_request(tids, mreq, &resp))) {
318 fprintf(stderr, "tids_handle_connection: Error from tids_handle_request(), rc = %d.\n", rc);
319 /* Fall through, to send the response, either way */
322 if (0 > (rc = tids_send_response(tids, mreq->tid_req, resp))) {
323 fprintf(stderr, "tids_handle_connection: Error from tids_send_response(), rc = %d.\n", rc);
324 /* if we didn't already send a response, try to send a generic error. */
325 if (!mreq->tid_req->resp_sent)
326 tids_send_err_response(tids, mreq->tid_req, "Error sending response.\n");
327 /* Fall through to free the response, either way. */
330 tids_destroy_response(tids, resp);
335 TIDS_INSTANCE *tids_create (void)
337 TIDS_INSTANCE *tids = NULL;
338 if (tids = malloc(sizeof(TIDS_INSTANCE)))
339 memset(tids, 0, sizeof(TIDS_INSTANCE));
343 int tids_start (TIDS_INSTANCE *tids,
344 TIDS_REQ_FUNC *req_handler,
345 tids_auth_func *auth_handler,
352 if (0 > (listen = tids_listen(tids, TID_PORT)))
353 perror ("Error from tids_listen()");
355 /* store the caller's request handler & cookie */
356 tids->req_handler = req_handler;
357 tids->auth_handler = auth_handler;
358 tids->cookie = cookie;
360 while(1) { /* accept incoming conns until we are stopped */
362 if (0 > (conn = accept(listen, NULL, NULL))) {
363 perror("Error from TIDS Server accept()");
367 if (0 > (pid = fork())) {
368 perror("Error on fork()");
374 tids_handle_connection(tids, conn);
382 return 1; /* should never get here */
385 void tids_destroy (TIDS_INSTANCE *tids)