TPQ_NAME *tpq_new_name (char *name);
TPQ_NAME *tpq_dup_name (TPQ_NAME *from);
+char *tpq_req_encode(TPQ_REQ *req);
+TPQ_REQ *tpq_req_decode(char *jreq);
+char *tpq_resp_encode(TPQ_REQ *resp);
+TPQ_RESP *tpq_resp_decode(char *jresp);
+
TPQC_INSTANCE *tpqc_create (void);
int tpqc_open_connection (TPQC_INSTANCE *tpqc, char *server, gss_ctx_id_t *gssctx);
int tpqc_send_request (TPQC_INSTANCE *tpqc, int conn, gss_ctx_id_t gssctx, char *realm, char *coi, TPQC_RESP_FUNC *resp_handler, void *cookie);
TPQ_RESP *resp,
void *cookie)
{
- printf ("Response received! Realm = %s, COI = %s.\n", resp->realm->buf,
- resp->coi->buf);
+ // printf ("Response received! Realm = %s, COI = %s.\n", resp->realm->buf,
+ // resp->coi->buf);
+ printf ("Response received at handler!\n");
tpqc_response_received = 1;
+ return;
}
int main (int argc,
};
/* Send a TPQ request */
- if (rc = tpqc_send_request(tpqc, conn, gssctx, realm, coi,
- &tpqc_resp_handler, NULL)) {
+ if (0 > (rc = tpqc_send_request(tpqc, conn, gssctx, realm, coi,
+ &tpqc_resp_handler, NULL))) {
/* Handle error */
printf("Error in tpqc_send_request, rc = %d.\n", rc);
return 1;
*/
#include <stdlib.h>
+#include <jansson.h>
#include <gsscon.h>
#include <tpq.h>
}
int tpqc_open_connection (TPQC_INSTANCE *tpqc,
- char *server)
+ char *server,
+ gss_ctx_id_t *gssctx)
{
int err = 0;
int conn = -1;
- gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
-
err = gsscon_connect(server, TPQ_PORT, &conn);
+
if (!err)
- err = gsscon_active_authenticate(conn, NULL, "trustquery", &gssContext);
+ err = gsscon_active_authenticate(conn, NULL, "trustquery", gssctx);
if (!err)
return conn;
int tpqc_send_request (TPQC_INSTANCE *tpqc,
int conn,
+ gss_ctx_id_t gssctx,
char *realm,
char *coi,
TPQC_RESP_FUNC *resp_handler,
void *cookie)
{
+ json_t *jreq;
+ int err;
+ char *req_buf;
+ char *resp_buf;
+ size_t resp_buflen = 0;
+
+ /* Create a json TPQ request */
+ if (NULL == (jreq = json_object())) {
+ fprintf(stderr,"Error creating json object.\n");
+ return -1;
+ }
+
+ if (0 > (err = json_object_set_new(jreq, "type", json_string("tpq_request")))) {
+ fprintf(stderr, "Error adding type to request.\n");
+ return -1;
+ }
+
+ /* Insert realm and coi into the json request */
+ if (0 > (err = json_object_set_new(jreq, "realm", json_string(realm)))) {
+ fprintf(stderr, "Error adding realm to request.\n");
+ return -1;
+ }
+ if (0 > (err = json_object_set_new(jreq, "coi", json_string(coi)))) {
+ fprintf(stderr, "Error adding coi to request.\n");
+ return -1;
+ }
+
+ /* Generate half of a D-H exchange -- TBD */
+ /* Insert D-H information into the request -- TBD */
+
+ /* Encode the json request */
+ if (NULL == (req_buf = json_dumps(jreq, 0))) {
+ fprintf(stderr, "Error encoding json request.\n");
+ return -1;
+ }
+
+ printf("Encoded request:\n%s\n", req_buf);
+
+ /* Send the request over the connection */
+ if (err = gsscon_write_encrypted_token (conn, gssctx, req_buf,
+ strlen(req_buf) + 1)) {
+ fprintf(stderr, "Error sending request over connection.\n");
+ return -1;
+ }
+
+ free(req_buf);
+
+ /* read the response from the connection */
+
+ if (err = gsscon_read_encrypted_token(conn, gssctx, &resp_buf, &resp_buflen)) {
+ if (resp_buf)
+ free(resp_buf);
+ return -1;
+ }
+
+ fprintf(stdout, "Response Received, %d bytes.\n", resp_buflen);
+
+ /* Parse response -- TBD */
+
+ /* Call the caller's response function */
+ (*resp_handler)(tpqc, NULL, cookie);
+
+ if (resp_buf)
+ free(resp_buf);
+ return 0;
}
void tpqc_destroy (TPQC_INSTANCE *tpqc)
static int tpqs_read_request (int conn, gss_ctx_id_t *gssctx, TPQ_REQ *req)
{
- return -1;
+ int err;
+ char *buf;
+ size_t buflen = 0;
+
+ if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
+ if (buf)
+ free(buf);
+ return -1;
+ }
+
+ fprintf(stdout, "Request Received, %d bytes.\n", buflen);
+
+ /* Parse request -- TBD */
+
+ if (buf)
+ free(buf);
+
+ return buflen;
}
static int tpqs_handle_request (TPQ_REQ *req, TPQ_RESP *resp)
{
- return -1;
+ return 0;
}
static int tpqs_send_response (int conn, gss_ctx_id_t *gssctx, TPQ_RESP *resp)
{
- return -1;
+ json_t *jreq;
+ int err;
+ char *resp_buf;
+
+ /* Create a json TPQ response */
+ if (NULL == (jreq = json_object())) {
+ fprintf(stderr,"Error creating json object.\n");
+ return -1;
+ }
+
+ if (0 > (err = json_object_set_new(jreq, "type", json_string("tpq_response")))) {
+ fprintf(stderr, "Error adding type to response.\n");
+ return -1;
+ }
+ if (0 > (err = json_object_set_new(jreq, "result", json_string("error")))) {
+ fprintf(stderr, "Error adding result to response.\n");
+ return -1;
+ }
+ if (0 > (err = json_object_set_new(jreq, "msg", json_string("No path to realm")))) {
+ fprintf(stderr, "Error adding msg to response.\n");
+ return -1;
+ }
+
+ /* Encode the json response */
+ if (NULL == (resp_buf = json_dumps(jreq, 0))) {
+ fprintf(stderr, "Error encoding json response.\n");
+ return -1;
+ }
+
+ printf("Encoded response:\n%s\n", resp_buf);
+
+ /* Send the request over the connection */
+ if (err = gsscon_write_encrypted_token (conn, *gssctx, resp_buf,
+ strlen(resp_buf) + 1)) {
+ fprintf(stderr, "Error sending request over connection.\n");
+ return -1;
+ }
+
+ free(resp_buf);
+
+ return 0;
+
}
static void tpqs_handle_connection (int conn)
if (pid == 0) {
close(listen);
tpqs_handle_connection(conn);
+ close(conn);
exit(0);
} else {
close(conn);