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.
40 #include <sys/socket.h>
41 #include <netinet/in.h>
44 #include <trust_router/tid.h>
48 static int tids_listen (TIDS_INSTANCE *tids, int port)
52 struct sockaddr_storage addr;
53 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr;
55 saddr->sin_port = htons (port);
56 saddr->sin_family = AF_INET;
57 saddr->sin_addr.s_addr = INADDR_ANY;
59 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
62 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
65 if (0 > (rc = listen(conn, 512)))
68 fprintf (stdout, "TID Server listening on port %d\n", port);
72 static int tids_auth_connection (int conn, gss_ctx_id_t *gssctx)
75 int auth, autherr = 0;
77 if (rc = gsscon_passive_authenticate(conn, gssctx)) {
78 fprintf(stderr, "Error from gsscon_passive_authenticate(), rc = %d.\n", rc);
82 if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
83 fprintf(stderr, "Error from gsscon_authorize, rc = %d, autherr = %d.\n",
89 fprintf(stdout, "Connection authenticated, conn = %d.\n", conn);
91 fprintf(stderr, "Authentication failed, conn %d.\n", conn);
96 static int tids_read_request (TIDS_INSTANCE *tids, int conn, gss_ctx_id_t *gssctx, TR_MSG **mreq)
102 if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
108 fprintf(stdout, "tids_read_request():Request Received, %d bytes.\n", buflen);
111 if (NULL == ((*mreq) = tr_msg_decode(buf, buflen))) {
112 printf("tids_read_request():Error decoding request.\n");
117 /* If this isn't a TID Request, just drop it. */
118 if (TID_REQUEST != (*mreq)->msg_type) {
119 printf("tids_read_request(): Not a TID Request, dropped.\n");
127 static int tids_handle_request (TIDS_INSTANCE *tids, TR_MSG *mreq, TID_RESP **resp)
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 (*resp)->result = TID_ERROR;
138 (*resp)->err_msg = tr_new_name("Bad request format");
142 /* Call the caller's request handler */
143 /* TBD -- Handle different error returns/msgs */
144 if (0 > (rc = (*tids->req_handler)(tids, mreq->tid_req, &(*resp), tids->cookie))) {
145 /* set-up an error response */
146 (*resp)->result = TID_ERROR;
147 if (!(*resp)->err_msg) /* Use msg set by handler, if any */
148 (*resp)->err_msg = tr_new_name("Internal processing error");
151 /* set-up a success response */
152 (*resp)->result = TID_SUCCESS;
153 (*resp)->err_msg = NULL; /* No msg on successful return */
159 int tids_send_response (TIDS_INSTANCE *tids, int conn, gss_ctx_id_t *gssctx, TID_RESP *resp)
165 mresp.msg_type = TID_RESPONSE;
166 mresp.tid_resp = resp;
168 if (NULL == (resp_buf = tr_msg_encode(&mresp))) {
169 fprintf(stderr, "Error decoding json response.\n");
173 printf("Encoded response:\n%s\n", resp_buf);
175 /* Send the response over the connection */
176 if (err = gsscon_write_encrypted_token (conn, *gssctx, resp_buf,
177 strlen(resp_buf) + 1)) {
178 fprintf(stderr, "Error sending response over connection.\n");
187 static void tids_handle_connection (TIDS_INSTANCE *tids, int conn)
190 TID_RESP *resp = NULL;
192 gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
194 if (!tids_auth_connection(conn, &gssctx)) {
195 fprintf(stderr, "Error authorizing TID Server connection.\n");
200 printf("Connection authorized!\n");
202 while (1) { /* continue until an error breaks us out */
204 if (0 > (rc = tids_read_request(tids, conn, &gssctx, &mreq))) {
205 fprintf(stderr, "Error from tids_read_request(), rc = %d.\n", rc);
207 } else if (0 == rc) {
211 /* Allocate a response structure and populate common fields */
212 if ((NULL == (resp = malloc(sizeof(TID_RESP))))) {
213 fprintf(stderr, "Error allocating response structure.\n");
217 /* TBD -- handle errors */
218 resp->result = TID_SUCCESS; /* presume success */
219 resp->rp_realm = tr_dup_name(mreq->tid_req->rp_realm);
220 resp->realm = tr_dup_name(mreq->tid_req->realm);
221 resp->comm = tr_dup_name(mreq->tid_req->comm);
222 if (mreq->tid_req->orig_coi)
223 resp->orig_coi = tr_dup_name(mreq->tid_req->orig_coi);
225 if (0 > (rc = tids_handle_request(tids, mreq, &resp))) {
226 fprintf(stderr, "Error from tids_handle_request(), rc = %d.\n", rc);
230 if (0 > (rc = tids_send_response(tids, conn, &gssctx, resp))) {
231 fprintf(stderr, "Error from tids_send_response(), rc = %d.\n", rc);
239 TIDS_INSTANCE *tids_create (void)
241 TIDS_INSTANCE *tids = NULL;
242 if (tids = malloc(sizeof(TIDS_INSTANCE)))
243 memset(tids, 0, sizeof(TIDS_INSTANCE));
247 int tids_start (TIDS_INSTANCE *tids,
248 TIDS_REQ_FUNC *req_handler,
256 if (0 > (listen = tids_listen(tids, TID_PORT)))
257 perror ("Error from tids_listen()");
259 setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
261 /* store the caller's request handler & cookie */
262 tids->req_handler = req_handler;
263 tids->cookie = cookie;
265 while(1) { /* accept incoming conns until we are stopped */
267 if (0 > (conn = accept(listen, NULL, NULL))) {
268 perror("Error from TIDS Server accept()");
272 if (0 > (pid = fork())) {
273 perror("Error on fork()");
279 tids_handle_connection(tids, conn);
287 return 1; /* should never get here */
290 void tids_destroy (TIDS_INSTANCE *tids)