2 * Copyright (c) 2012, 2015, 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>
43 #include <netinet/in.h>
46 #include <tid_internal.h>
51 static TID_RESP *tids_create_response (TIDS_INSTANCE *tids, TID_REQ *req)
55 if ((NULL == (resp = talloc_zero(req, TID_RESP)))) {
56 tr_crit("tids_create_response: Error allocating response structure.");
60 resp->result = TID_SUCCESS; /* presume success */
61 if ((NULL == (resp->rp_realm = tr_dup_name(req->rp_realm))) ||
62 (NULL == (resp->realm = tr_dup_name(req->realm))) ||
63 (NULL == (resp->comm = tr_dup_name(req->comm)))) {
64 tr_crit("tids_create_response: Error allocating fields in response.");
68 if (NULL == (resp->orig_coi = tr_dup_name(req->orig_coi))) {
69 tr_crit("tids_create_response: Error allocating fields in response.");
76 static void tids_destroy_response(TIDS_INSTANCE *tids, TID_RESP *resp)
80 tr_free_name(resp->err_msg);
82 tr_free_name(resp->rp_realm);
84 tr_free_name(resp->realm);
86 tr_free_name(resp->comm);
88 tr_free_name(resp->orig_coi);
93 static int tids_listen (TIDS_INSTANCE *tids, int port)
100 struct sockaddr_storage storage;
101 struct sockaddr_in in4;
104 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4;
106 saddr->sin_port = htons (port);
107 saddr->sin_family = AF_INET;
108 saddr->sin_addr.s_addr = INADDR_ANY;
110 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
113 setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
115 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
118 if (0 > (rc = listen(conn, 512)))
121 tr_debug("tids_listen: TID Server listening on port %d", port);
125 static int tids_auth_cb(gss_name_t clientName, gss_buffer_t displayName,
128 struct tids_instance *inst = (struct tids_instance *) data;
129 TR_NAME name ={(char *) displayName->value,
130 displayName->length};
131 return inst->auth_handler(clientName, &name, inst->cookie);
134 static int tids_auth_connection (struct tids_instance *inst,
135 int conn, gss_ctx_id_t *gssctx)
138 int auth, autherr = 0;
139 gss_buffer_desc nameBuffer = {0, NULL};
143 nameLen = asprintf(&name, "trustidentity@%s", inst->hostname);
144 nameBuffer.length = nameLen;
145 nameBuffer.value = name;
147 if (rc = gsscon_passive_authenticate(conn, nameBuffer, gssctx, tids_auth_cb, inst)) {
148 tr_debug("tids_auth_connection: Error from gsscon_passive_authenticate(), rc = %d.", rc);
152 if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
153 tr_debug("tids_auth_connection: Error from gsscon_authorize, rc = %d, autherr = %d.",
159 tr_debug("tids_auth_connection: Connection authenticated, conn = %d.", conn);
161 tr_debug("tids_auth_connection: Authentication failed, conn %d.", conn);
166 static int tids_read_request (TIDS_INSTANCE *tids, int conn, gss_ctx_id_t *gssctx, TR_MSG **mreq)
172 if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
178 tr_debug("tids_read_request():Request Received, %u bytes.", (unsigned) buflen);
181 if (NULL == ((*mreq) = tr_msg_decode(buf, buflen))) {
182 tr_debug("tids_read_request():Error decoding request.");
187 /* If this isn't a TID Request, just drop it. */
188 if (TID_REQUEST != (*mreq)->msg_type) {
189 tr_debug("tids_read_request(): Not a TID Request, dropped.");
197 static int tids_handle_request (TIDS_INSTANCE *tids, TR_MSG *mreq, TID_RESP *resp)
201 /* Check that this is a valid TID Request. If not, send an error return. */
202 if ((!tr_msg_get_req(mreq)) ||
203 (!tr_msg_get_req(mreq)->rp_realm) ||
204 (!tr_msg_get_req(mreq)->realm) ||
205 (!tr_msg_get_req(mreq)->comm)) {
206 tr_notice("tids_handle_request(): Not a valid TID Request.");
207 resp->result = TID_ERROR;
208 resp->err_msg = tr_new_name("Bad request format");
212 tid_req_add_path(tr_msg_get_req(mreq), tids->hostname, tids->tids_port);
214 /* Call the caller's request handler */
215 /* TBD -- Handle different error returns/msgs */
216 if (0 > (rc = (*tids->req_handler)(tids, tr_msg_get_req(mreq), resp, tids->cookie))) {
217 /* set-up an error response */
218 resp->result = TID_ERROR;
219 if (!resp->err_msg) /* Use msg set by handler, if any */
220 resp->err_msg = tr_new_name("Internal processing error");
223 /* set-up a success response */
224 resp->result = TID_SUCCESS;
225 resp->err_msg = NULL; /* No error msg on successful return */
231 int tids_send_err_response (TIDS_INSTANCE *tids, TID_REQ *req, const char *err_msg) {
232 TID_RESP *resp = NULL;
235 /* If we already sent a response, don't send another no matter what. */
239 if (NULL == (resp = tids_create_response(tids, req))) {
240 tr_crit("tids_send_err_response: Can't create response.");
245 /* mark this as an error response, and include the error message */
246 resp->result = TID_ERROR;
247 resp->err_msg = tr_new_name((char *)err_msg);
248 resp->error_path = req->path;
250 rc = tids_send_response(tids, req, resp);
252 tids_destroy_response(tids, resp);
256 int tids_send_response (TIDS_INSTANCE *tids, TID_REQ *req, TID_RESP *resp)
262 if ((!tids) || (!req) || (!resp))
263 tr_debug("tids_send_response: Invalid parameters.");
265 /* Never send a second response if we already sent one. */
269 mresp.msg_type = TID_RESPONSE;
270 tr_msg_set_resp(&mresp, resp);
272 if (NULL == (resp_buf = tr_msg_encode(&mresp))) {
274 fprintf(stderr, "tids_send_response: Error encoding json response.\n");
280 tr_debug("tids_send_response: Encoded response: %s", resp_buf);
282 /* If external logging is enabled, fire off a message */
283 /* TODO Can be moved to end once segfault in gsscon_write_encrypted_token fixed */
286 /* Send the response over the connection */
287 if (err = gsscon_write_encrypted_token (req->conn, req->gssctx, resp_buf,
288 strlen(resp_buf) + 1)) {
289 tr_notice("tids_send_response: Error sending response over connection.");
296 /* indicate that a response has been sent for this request */
304 static void tids_handle_connection (TIDS_INSTANCE *tids, int conn)
307 TID_RESP *resp = NULL;
309 gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
311 if (tids_auth_connection(tids, conn, &gssctx)) {
312 tr_notice("tids_handle_connection: Error authorizing TID Server connection.");
317 tr_debug("tids_handle_connection: Connection authorized!");
319 while (1) { /* continue until an error breaks us out */
321 if (0 > (rc = tids_read_request(tids, conn, &gssctx, &mreq))) {
322 tr_debug("tids_handle_connection: Error from tids_read_request(), rc = %d.", rc);
324 } else if (0 == rc) {
328 /* Put connection information into the request structure */
329 tr_msg_get_req(mreq)->conn = conn;
330 tr_msg_get_req(mreq)->gssctx = gssctx;
332 /* Allocate a response structure and populate common fields */
333 if (NULL == (resp = tids_create_response (tids, tr_msg_get_req(mreq)))) {
334 tr_crit("tids_handle_connection: Error creating response structure.");
335 /* try to send an error */
336 tids_send_err_response(tids, tr_msg_get_req(mreq), "Error creating response.");
340 if (0 > (rc = tids_handle_request(tids, mreq, resp))) {
341 tr_debug("tids_handle_connection: Error from tids_handle_request(), rc = %d.", rc);
342 /* Fall through, to send the response, either way */
345 if (0 > (rc = tids_send_response(tids, tr_msg_get_req(mreq), resp))) {
346 tr_debug("tids_handle_connection: Error from tids_send_response(), rc = %d.", rc);
347 /* if we didn't already send a response, try to send a generic error. */
348 if (!tr_msg_get_req(mreq)->resp_sent)
349 tids_send_err_response(tids, tr_msg_get_req(mreq), "Error sending response.");
350 /* Fall through to free the response, either way. */
353 tids_destroy_response(tids, resp);
358 TIDS_INSTANCE *tids_create (void)
360 TIDS_INSTANCE *tids = NULL;
361 if (tids = malloc(sizeof(TIDS_INSTANCE)))
362 memset(tids, 0, sizeof(TIDS_INSTANCE));
366 int tids_start (TIDS_INSTANCE *tids,
367 TIDS_REQ_FUNC *req_handler,
368 tids_auth_func *auth_handler,
369 const char *hostname,
377 tids->tids_port = port;
378 if (0 > (listen = tids_listen(tids, port)))
379 perror ("Error from tids_listen()");
381 /* store the caller's request handler & cookie */
382 tids->req_handler = req_handler;
383 tids->auth_handler = auth_handler;
384 tids->hostname = hostname;
385 tids->cookie = cookie;
387 tr_info("Trust Path Query Server starting on host %s:%d.", hostname, port);
389 while(1) { /* accept incoming conns until we are stopped */
391 if (0 > (conn = accept(listen, NULL, NULL))) {
392 perror("Error from TIDS Server accept()");
396 if (0 > (pid = fork())) {
397 perror("Error on fork()");
403 tids_handle_connection(tids, conn);
410 /* clean up any processes that have completed */
411 while (waitpid(-1, 0, WNOHANG) > 0);
414 return 1; /* should never get here, loops "forever" */
417 void tids_destroy (TIDS_INSTANCE *tids)
419 /* clean up logfiles */