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>
44 #include <trust_router/tid.h>
47 static int tids_listen (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 (int conn, gss_ctx_id_t *gssctx, TID_REQ *req)
101 if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
107 fprintf(stdout, "Request Received, %u bytes.\n", (unsigned) buflen);
109 /* Parse request -- TBD */
117 static int tids_handle_request (TID_REQ *req, TID_RESP *resp)
122 static int tids_send_response (int conn, gss_ctx_id_t *gssctx, TID_RESP *resp)
128 /* Create a json TID response */
129 if (NULL == (jreq = json_object())) {
130 fprintf(stderr,"Error creating json object.\n");
134 if (0 > (err = json_object_set_new(jreq, "type", json_string("tid_response")))) {
135 fprintf(stderr, "Error adding type to response.\n");
138 if (0 > (err = json_object_set_new(jreq, "result", json_string("error")))) {
139 fprintf(stderr, "Error adding result to response.\n");
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");
147 /* Encode the json response */
148 if (NULL == (resp_buf = json_dumps(jreq, 0))) {
149 fprintf(stderr, "Error encoding json response.\n");
153 printf("Encoded response:\n%s\n", resp_buf);
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");
168 static void tids_handle_connection (int conn)
173 gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
175 if (!tids_auth_connection(conn, &gssctx)) {
176 fprintf(stderr, "Error authorizing TID Server connection, rc = %d.\n", rc);
181 printf("Connection authorized!\n");
183 while (1) { /* continue until an error breaks us out */
185 if (0 > (rc = tids_read_request(conn, &gssctx, &req))) {
186 fprintf(stderr, "Error from tids_read_request(), rc = %d.\n", rc);
188 } else if (0 == rc) {
192 if (0 > (rc = tids_handle_request(&req, &resp))) {
193 fprintf(stderr, "Error from tids_handle_request(), rc = %d.\n", rc);
197 if (0 > (rc = tids_send_response(conn, &gssctx, &resp))) {
198 fprintf(stderr, "Error from tids_send_response(), rc = %d.\n", rc);
206 TIDS_INSTANCE *tids_create (void)
208 TIDS_INSTANCE *tids = 0;
209 if (tids = malloc(sizeof(TIDS_INSTANCE)))
210 memset(tids, 0, sizeof(TIDS_INSTANCE));
214 int tids_start (TIDS_INSTANCE *tids,
215 TIDS_REQ_FUNC *req_handler,
222 if (0 > (listen = tids_listen(TID_PORT)))
223 perror ("Error from tids_listen()");
225 while(1) { /* accept incoming conns until we are stopped */
227 if (0 > (conn = accept(listen, NULL, NULL))) {
228 perror("Error from TIDS Server accept()");
232 if (0 > (pid = fork())) {
233 perror("Error on fork()");
239 tids_handle_connection(conn);
247 return 1; /* should never get here */
250 void tids_destroy (TIDS_INSTANCE *tids)