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.
39 #include <sys/socket.h>
40 #include <netinet/in.h>
45 #include <trust_router/tid.h>
47 static int tids_listen (TIDS_INSTANCE *tids, int port)
51 struct sockaddr_storage addr;
52 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr;
54 saddr->sin_port = htons (port);
55 saddr->sin_family = AF_INET;
56 saddr->sin_addr.s_addr = INADDR_ANY;
58 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
61 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
64 if (0 > (rc = listen(conn, 512)))
67 fprintf (stdout, "TID Server listening on port %d\n", port);
71 static int tids_auth_connection (int conn, gss_ctx_id_t *gssctx)
74 int auth, autherr = 0;
76 if (rc = gsscon_passive_authenticate(conn, gssctx)) {
77 fprintf(stderr, "Error from gsscon_passive_authenticate(), rc = %d.\n", rc);
81 if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
82 fprintf(stderr, "Error from gsscon_authorize, rc = %d, autherr = %d.\n",
88 fprintf(stdout, "Connection authenticated, conn = %d.\n", conn);
90 fprintf(stderr, "Authentication failed, conn %d.\n", conn);
95 static int tids_read_request (TIDS_INSTANCE *tids, int conn, gss_ctx_id_t *gssctx, TR_MSG **mreq)
101 if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
107 fprintf(stdout, "tids_read_request():Request Received, %d bytes.\n", buflen);
110 if (NULL == ((*mreq) = tr_msg_decode(buf, buflen))) {
111 printf("tids_read_request():Error decoding request.\n");
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");
126 static int tids_handle_request (TIDS_INSTANCE *tids, TR_MSG *mreq, TR_MSG **mresp)
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");
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");
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 */
160 static int tids_send_response (TIDS_INSTANCE *tids, int conn, gss_ctx_id_t *gssctx, TR_MSG *mresp)
165 if (NULL == (resp_buf = tr_msg_encode(mresp))) {
166 fprintf(stderr, "Error decoding json response.\n");
170 printf("Encoded response:\n%s\n", resp_buf);
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");
184 static void tids_handle_connection (TIDS_INSTANCE *tids, int conn)
187 TR_MSG *mresp = NULL;
189 gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
191 if (!tids_auth_connection(conn, &gssctx)) {
192 fprintf(stderr, "Error authorizing TID Server connection, rc = %d.\n", rc);
197 printf("Connection authorized!\n");
199 while (1) { /* continue until an error breaks us out */
201 if (0 > (rc = tids_read_request(tids, conn, &gssctx, &mreq))) {
202 fprintf(stderr, "Error from tids_read_request(), rc = %d.\n", rc);
204 } else if (0 == rc) {
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");
215 mresp->msg_type = TID_RESPONSE;
216 memset(mresp->tid_resp, 0, sizeof(TID_RESP));
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);
226 if (0 > (rc = tids_handle_request(tids, mreq, &mresp))) {
227 fprintf(stderr, "Error from tids_handle_request(), rc = %d.\n", rc);
231 if (0 > (rc = tids_send_response(tids, conn, &gssctx, mresp))) {
232 fprintf(stderr, "Error from tids_send_response(), rc = %d.\n", rc);
240 TIDS_INSTANCE *tids_create (void)
242 TIDS_INSTANCE *tids = 0;
243 if (tids = malloc(sizeof(TIDS_INSTANCE)))
244 memset(tids, 0, sizeof(TIDS_INSTANCE));
248 int tids_start (TIDS_INSTANCE *tids,
249 TIDS_REQ_FUNC *req_handler,
257 if (0 > (listen = tids_listen(tids, TID_PORT)))
258 perror ("Error from tids_listen()");
260 setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
262 /* store the caller's request handler & cookie */
263 tids->req_handler = req_handler;
264 tids->cookie = cookie;
266 while(1) { /* accept incoming conns until we are stopped */
268 if (0 > (conn = accept(listen, NULL, NULL))) {
269 perror("Error from TIDS Server accept()");
273 if (0 > (pid = fork())) {
274 perror("Error on fork()");
280 tids_handle_connection(tids, conn);
288 return 1; /* should never get here */
291 void tids_destroy (TIDS_INSTANCE *tids)