#include <gsscon.h>
#include <tr_msg.h>
+static TID_RESP *tids_create_response (TIDS_INSTANCE *tids, TID_REQ *req)
+{
+ TID_RESP *resp;
+
+ if ((NULL == (resp = calloc(sizeof(TID_RESP), 1)))) {
+ fprintf(stderr, "tids_create_response: Error allocating response structure.\n");
+ return NULL;
+ }
+
+ resp->result = TID_SUCCESS; /* presume success */
+ if ((NULL == (resp->rp_realm = tr_dup_name(req->rp_realm))) ||
+ (NULL == (resp->realm = tr_dup_name(req->realm))) ||
+ (NULL == (resp->comm = tr_dup_name(req->comm)))) {
+ fprintf(stderr, "tids_create_response: Error allocating fields in response.\n");
+ return NULL;
+ }
+ if (req->orig_coi) {
+ if (NULL == (resp->orig_coi = tr_dup_name(req->orig_coi))) {
+ fprintf(stderr, "tids_create_response: Error allocating fields in response.\n");
+ return NULL;
+ }
+ }
+ return resp;
+}
+
+static void tids_destroy_response(TIDS_INSTANCE *tids, TID_RESP *resp)
+{
+ if (resp) {
+ if (resp->err_msg)
+ tr_free_name(resp->err_msg);
+ if (resp->rp_realm)
+ tr_free_name(resp->rp_realm);
+ if (resp->realm)
+ tr_free_name(resp->realm);
+ if (resp->comm)
+ tr_free_name(resp->comm);
+ if (resp->orig_coi)
+ tr_free_name(resp->orig_coi);
+ free (resp);
+ }
+}
+
static int tids_listen (TIDS_INSTANCE *tids, int port)
{
int rc = 0;
if (0 > (rc = listen(conn, 512)))
return rc;
- fprintf (stdout, "TID Server listening on port %d\n", port);
+ fprintf (stdout, "tids_listen: TID Server listening on port %d\n", port);
return conn;
}
int auth, autherr = 0;
if (rc = gsscon_passive_authenticate(conn, gssctx)) {
- fprintf(stderr, "Error from gsscon_passive_authenticate(), rc = %d.\n", rc);
+ fprintf(stderr, "tids_auth_connection: Error from gsscon_passive_authenticate(), rc = %d.\n", rc);
return -1;
}
if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
- fprintf(stderr, "Error from gsscon_authorize, rc = %d, autherr = %d.\n",
+ fprintf(stderr, "tids_auth_connection: Error from gsscon_authorize, rc = %d, autherr = %d.\n",
rc, autherr);
return -1;
}
if (auth)
- fprintf(stdout, "Connection authenticated, conn = %d.\n", conn);
+ fprintf(stdout, "tids_auth_connection: Connection authenticated, conn = %d.\n", conn);
else
- fprintf(stderr, "Authentication failed, conn %d.\n", conn);
+ fprintf(stderr, "tids_auth_connection: Authentication failed, conn %d.\n", conn);
return !auth;
}
/* Parse request */
if (NULL == ((*mreq) = tr_msg_decode(buf, buflen))) {
- printf("tids_read_request():Error decoding request.\n");
+ fprintf(stderr, "tids_read_request():Error decoding request.\n");
free (buf);
return -1;
}
/* If this isn't a TID Request, just drop it. */
if (TID_REQUEST != (*mreq)->msg_type) {
- printf("tids_read_request(): Not a TID Request, dropped.\n");
+ fprintf(stderr, "tids_read_request(): Not a TID Request, dropped.\n");
return -1;
}
(!mreq->tid_req->rp_realm) ||
(!mreq->tid_req->realm) ||
(!mreq->tid_req->comm)) {
- printf("tids_handle_request():Not a valid TID Request.\n");
+ fprintf(stderr, "tids_handle_request():Not a valid TID Request.\n");
(*resp)->result = TID_ERROR;
(*resp)->err_msg = tr_new_name("Bad request format");
return -1;
else {
/* set-up a success response */
(*resp)->result = TID_SUCCESS;
- (*resp)->err_msg = NULL; /* No msg on successful return */
+ (*resp)->err_msg = NULL; /* No error msg on successful return */
}
return rc;
}
-int tids_send_response (TIDS_INSTANCE *tids, int conn, gss_ctx_id_t *gssctx, TID_RESP *resp)
+int tids_send_err_response (TIDS_INSTANCE *tids, TID_REQ *req, const char *err_msg) {
+ TID_RESP *resp = NULL;
+ int rc = 0;
+
+ /* If we already sent a response, don't send another no matter what. */
+ if (req->resp_sent)
+ return 0;
+
+ if (NULL == (resp = tids_create_response(tids, req))) {
+ fprintf(stderr, "tids_send_err_response: Can't create response.\n");
+ return -1;
+ }
+
+ /* mark this as an error response, and include the error message */
+ resp->result = TID_ERROR;
+ resp->err_msg = tr_new_name((char *)err_msg);
+
+ rc = tids_send_response(tids, req, resp);
+
+ tids_destroy_response(tids, resp);
+ return rc;
+}
+
+int tids_send_response (TIDS_INSTANCE *tids, TID_REQ *req, TID_RESP *resp)
{
int err;
TR_MSG mresp;
char *resp_buf;
+ if ((!tids) || (!req) || (!resp))
+ fprintf (stderr, "tids_send_response: Invalid parameters.\n");
+
+ /* Never send a second response if we already sent one. */
+ if (req->resp_sent)
+ return -1;
+
mresp.msg_type = TID_RESPONSE;
mresp.tid_resp = resp;
if (NULL == (resp_buf = tr_msg_encode(&mresp))) {
- fprintf(stderr, "Error decoding json response.\n");
+ fprintf(stderr, "tids_send_response: Error encoding json response.\n");
return -1;
}
- printf("Encoded response:\n%s\n", resp_buf);
+ fprintf(stderr, "tids_send_response: Encoded response:\n%s\n", resp_buf);
/* Send the response over the connection */
- if (err = gsscon_write_encrypted_token (conn, *gssctx, resp_buf,
+ if (err = gsscon_write_encrypted_token (req->conn, req->gssctx, resp_buf,
strlen(resp_buf) + 1)) {
- fprintf(stderr, "Error sending response over connection.\n");
+ fprintf(stderr, "tids_send_response: Error sending response over connection.\n");
return -1;
}
+ /* indicate that a response has been sent for this request */
+ req->resp_sent = 1;
+
free(resp_buf);
return 0;
gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
if (tids_auth_connection(conn, &gssctx)) {
- fprintf(stderr, "Error authorizing TID Server connection.\n");
+ fprintf(stderr, "tids_handle_connection: Error authorizing TID Server connection.\n");
close(conn);
return;
}
- printf("Connection authorized!\n");
+ fprintf(stdout, "tids_handle_connection: Connection authorized!\n");
while (1) { /* continue until an error breaks us out */
if (0 > (rc = tids_read_request(tids, conn, &gssctx, &mreq))) {
- fprintf(stderr, "Error from tids_read_request(), rc = %d.\n", rc);
+ fprintf(stderr, "tids_handle_connection: Error from tids_read_request(), rc = %d.\n", rc);
return;
} else if (0 == rc) {
continue;
mreq->tid_req->gssctx = gssctx;
/* Allocate a response structure and populate common fields */
- if ((NULL == (resp = calloc(sizeof(TID_RESP), 1)))) {
- fprintf(stderr, "Error allocating response structure.\n");
+ if (NULL == (resp = tids_create_response (tids, mreq->tid_req))) {
+ fprintf(stderr, "tids_handle_connection: Error creating response structure.\n");
+ /* try to send an error */
+ tids_send_err_response(tids, mreq->tid_req, "Error creating response.\n");
return;
}
- /* TBD -- handle errors */
- resp->result = TID_SUCCESS; /* presume success */
- resp->rp_realm = tr_dup_name(mreq->tid_req->rp_realm);
- resp->realm = tr_dup_name(mreq->tid_req->realm);
- resp->comm = tr_dup_name(mreq->tid_req->comm);
- if (mreq->tid_req->orig_coi)
- resp->orig_coi = tr_dup_name(mreq->tid_req->orig_coi);
-
if (0 > (rc = tids_handle_request(tids, mreq, &resp))) {
- fprintf(stderr, "Error from tids_handle_request(), rc = %d.\n", rc);
- return;
+ fprintf(stderr, "tids_handle_connection: Error from tids_handle_request(), rc = %d.\n", rc);
+ /* Fall through, to send the response, either way */
}
- if (0 > (rc = tids_send_response(tids, conn, &gssctx, resp))) {
- fprintf(stderr, "Error from tids_send_response(), rc = %d.\n", rc);
- return;
+ if (0 > (rc = tids_send_response(tids, mreq->tid_req, resp))) {
+ fprintf(stderr, "tids_handle_connection: Error from tids_send_response(), rc = %d.\n", rc);
+ /* if we didn't already send a response, try to send a generic error. */
+ if (!mreq->tid_req->resp_sent)
+ tids_send_err_response(tids, mreq->tid_req, "Error sending response.\n");
+ /* Fall through to free the response, either way. */
}
- }
-
- return;
+
+ tids_destroy_response(tids, resp);
+ return;
+ }
}
TIDS_INSTANCE *tids_create (void)
void tids_destroy (TIDS_INSTANCE *tids)
{
- free(tids);
+ if (tids)
+ free(tids);
}