From: Adam Bishop Date: Tue, 9 Dec 2014 13:27:13 +0000 (+0000) Subject: Replace calls to fprintf with new tr_* macros X-Git-Tag: 1.5~14^2~4 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=trust_router.git;a=commitdiff_plain;h=45c8d1e8259cae6eaaee3b4a66660a9aa92b1d5b Replace calls to fprintf with new tr_* macros --- diff --git a/common/tr_comm.c b/common/tr_comm.c index e032ea8..08d6638 100644 --- a/common/tr_comm.c +++ b/common/tr_comm.c @@ -37,6 +37,7 @@ #include #include #include +#include TR_IDP_REALM *tr_find_comm_idp (TR_COMM *comm, TR_NAME *idp_realm) { @@ -48,7 +49,7 @@ TR_IDP_REALM *tr_find_comm_idp (TR_COMM *comm, TR_NAME *idp_realm) for (idp = comm->idp_realms; NULL != idp; idp = idp->next) { if (!tr_name_cmp (idp_realm, idp->realm_id)) { - fprintf(stderr, "tr_find_comm_idp: Found %s.\n", idp_realm->buf); + tr_debug("tr_find_comm_idp: Found %s.", idp_realm->buf); return idp; } } @@ -66,7 +67,7 @@ TR_RP_REALM *tr_find_comm_rp (TR_COMM *comm, TR_NAME *rp_realm) for (rp = comm->rp_realms; NULL != rp; rp = rp->next) { if (!tr_name_cmp (rp_realm, rp->realm_name)) { - fprintf(stderr, "tr_find_comm_idp: Found %s.\n", rp_realm->buf); + tr_debug("tr_find_comm_idp: Found %s.", rp_realm->buf); return rp; } } diff --git a/common/tr_dh.c b/common/tr_dh.c index 41bac2a..b1d1e17 100644 --- a/common/tr_dh.c +++ b/common/tr_dh.c @@ -39,6 +39,7 @@ #include #include #include +#include unsigned char tr_2048_dhprime[2048/8] = { @@ -102,23 +103,23 @@ DH *tr_create_dh_params(unsigned char *priv_key, DH_check(dh, &dh_err); if (0 != dh_err) { - fprintf(stderr, "Warning: dh_check failed with %d", dh_err); + tr_warning("Warning: dh_check failed with %d", dh_err); if (dh_err & DH_CHECK_P_NOT_PRIME) - fprintf(stderr, ": p value is not prime\n"); + tr_warning(": p value is not prime"); else if (dh_err & DH_CHECK_P_NOT_SAFE_PRIME) - fprintf(stderr, ": p value is not a safe prime\n"); + tr_warning(": p value is not a safe prime"); else if (dh_err & DH_UNABLE_TO_CHECK_GENERATOR) - fprintf(stderr, ": unable to check the generator value\n"); + tr_warning(": unable to check the generator value"); else if (dh_err & DH_NOT_SUITABLE_GENERATOR) - fprintf (stderr, ": the g value is not a generator\n"); - else - fprintf(stderr, "\n"); + tr_warning(": the g value is not a generator"); + else + tr_warning("unhandled error %i", dh_err); } - + return(dh); } -DH *tr_create_matching_dh (unsigned char *priv_key, +DH *tr_create_matching_dh (unsigned char *priv_key, size_t keylen, DH *in_dh) { DH *dh = NULL; @@ -128,14 +129,14 @@ DH *tr_create_matching_dh (unsigned char *priv_key, return NULL; if (NULL == (dh = DH_new())) { - fprintf(stderr, "Unable to allocate new DH structure.\n"); + tr_crit("tr_create_matching_dh: unable to allocate new DH structure."); return NULL; } if ((NULL == (dh->g = BN_dup(in_dh->g))) || (NULL == (dh->p = BN_dup(in_dh->p)))) { DH_free(dh); - fprintf(stderr, "Invalid dh parameter values, can't be duped.\n"); + tr_debug("tr_create_matching_dh: Invalid dh parameter values, can't be duped."); return NULL; } @@ -146,19 +147,19 @@ DH *tr_create_matching_dh (unsigned char *priv_key, DH_generate_key(dh); /* generates the public key */ DH_check(dh, &dh_err); if (0 != dh_err) { - fprintf(stderr, "Warning: dh_check failed with %d", dh_err); + tr_warning("Warning: dh_check failed with %d", dh_err); if (dh_err & DH_CHECK_P_NOT_PRIME) - fprintf(stderr, ": p value is not prime\n"); + tr_warning(": p value is not prime"); else if (dh_err & DH_CHECK_P_NOT_SAFE_PRIME) - fprintf(stderr, ": p value is not a safe prime\n"); + tr_warning(": p value is not a safe prime"); else if (dh_err & DH_UNABLE_TO_CHECK_GENERATOR) - fprintf(stderr, ": unable to check the generator value\n"); + tr_warning(": unable to check the generator value"); else if (dh_err & DH_NOT_SUITABLE_GENERATOR) - fprintf (stderr, ": the g value is not a generator\n"); - else - fprintf(stderr, "\n"); + tr_warning(": the g value is not a generator"); + else + tr_warning("unhandled error %i", dh_err); } - + return(dh); } @@ -179,14 +180,14 @@ int tr_compute_dh_key(unsigned char **pbuf, if ((!pbuf) || (!pub_key) || (!priv_dh)) { - fprintf(stderr, "tr_compute_dh_key(): Invalid parameters.\n"); + tr_debug("tr_compute_dh_key: Invalid parameters."); return(-1); } *pbuf = NULL; buflen = DH_size(priv_dh); buf = malloc(buflen); if (buf == NULL) { - fprintf(stderr, "out of memory\n"); + tr_crit("tr_compute_dh_key: out of memory"); return -1; } diff --git a/common/tr_msg.c b/common/tr_msg.c index 85221f5..841e894 100644 --- a/common/tr_msg.c +++ b/common/tr_msg.c @@ -113,7 +113,7 @@ static DH *tr_msg_decode_dh(json_t *jdh) json_t *jpub_key = NULL; if (!(dh = malloc(sizeof(DH)))) { - fprintf (stderr, "tr_msg_decode_dh(): Error allocating DH structure.\n"); + tr_crit("tr_msg_decode_dh(): Error allocating DH structure."); return NULL; } @@ -123,7 +123,7 @@ static DH *tr_msg_decode_dh(json_t *jdh) if ((NULL == (jp = json_object_get(jdh, "dh_p"))) || (NULL == (jg = json_object_get(jdh, "dh_g"))) || (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key")))) { - fprintf (stderr, "tr_msg_decode_dh(): Error parsing dh_info.\n"); + tr_debug("tr_msg_decode_dh(): Error parsing dh_info."); free(dh); return NULL; } @@ -177,7 +177,7 @@ static TID_REQ *tr_msg_decode_tidreq(json_t *jreq) json_t *jdh = NULL; if (!(treq =tid_req_new())) { - fprintf (stderr, "tr_msg_decode_tidreq(): Error allocating TID_REQ structure.\n"); + tr_crit("tr_msg_decode_tidreq(): Error allocating TID_REQ structure."); return NULL; } @@ -185,7 +185,7 @@ static TID_REQ *tr_msg_decode_tidreq(json_t *jreq) if ((NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) || (NULL == (jrealm = json_object_get(jreq, "target_realm"))) || (NULL == (jcomm = json_object_get(jreq, "community")))) { - fprintf (stderr, "tr_msg_decode(): Error parsing required fields.\n"); + tr_debug("tr_msg_decode(): Error parsing required fields."); tid_req_free(treq); return NULL; } @@ -196,7 +196,7 @@ static TID_REQ *tr_msg_decode_tidreq(json_t *jreq) /* Get DH Info from the request */ if (NULL == (jdh = json_object_get(jreq, "dh_info"))) { - fprintf (stderr, "tr_msg_decode(): Error parsing dh_info.\n"); + tr_debug("tr_msg_decode(): Error parsing dh_info."); tid_req_free(treq); return NULL; } @@ -210,7 +210,7 @@ static TID_REQ *tr_msg_decode_tidreq(json_t *jreq) treq->cons = (TR_CONSTRAINT_SET *) json_object_get(jreq, "constraints"); if (treq->cons) { if (!tr_constraint_set_validate(treq->cons)) { - tr_debug("Constraint set validation failed\n"); + tr_debug("Constraint set validation failed"); tid_req_free(treq); return NULL; } @@ -225,7 +225,7 @@ static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr) json_t *jsrvr = NULL; json_t *jstr = NULL; - fprintf(stderr, "Encoding one server.\n"); + tr_debug("Encoding one server."); jsrvr = json_object(); @@ -238,8 +238,8 @@ static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr) json_object_set_new(jsrvr, "key_name", jstr); json_object_set_new(jsrvr, "server_dh", tr_msg_encode_dh(srvr->aaa_server_dh)); - // fprintf(stderr,"tr_msg_encode_one_server(): jsrvr contains:\n"); - // fprintf(stderr,"%s\n", json_dumps(jsrvr, 0)); + // tr_debug("tr_msg_encode_one_server(): jsrvr contains:"); + // tr_debug("%s", json_dumps(jsrvr, 0)); return jsrvr; } @@ -256,7 +256,7 @@ static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr) if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) || (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) || (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) { - tr_debug("tr_msg_decode_one_server(): Error parsing required fields.\n"); + tr_debug("tr_msg_decode_one_server(): Error parsing required fields."); return -1; } @@ -283,8 +283,8 @@ static json_t *tr_msg_encode_servers(TID_RESP *resp) } } - // fprintf(stderr,"tr_msg_encode_servers(): servers contains:\n"); - // fprintf(stderr,"%s\n", json_dumps(jservers, 0)); + // tr_debug("tr_msg_encode_servers(): servers contains:"); + // tr_debug("%s", json_dumps(jservers, 0)); return jservers; } @@ -295,13 +295,13 @@ static TID_SRVR_BLK *tr_msg_decode_servers(void * ctx, json_t *jservers, size_t size_t i, num_servers; num_servers = json_array_size(jservers); - fprintf(stderr, "tr_msg_decode_servers(): Number of servers = %u.\n", (unsigned) num_servers); + tr_debug("tr_msg_decode_servers(): Number of servers = %u.", (unsigned) num_servers); if (0 == num_servers) { - fprintf(stderr, "tr_msg_decode_servers(): Server array is empty.\n"); + tr_debug("tr_msg_decode_servers(): Server array is empty."); return NULL; } - servers = talloc_zero_array(ctx, TID_SRVR_BLK, num_servers); + servers = talloc_zero_array(ctx, TID_SRVR_BLK, num_servers); for (i = 0; i < num_servers; i++) { jsrvr = json_array_get(jservers, i); @@ -355,7 +355,7 @@ static json_t * tr_msg_encode_tidresp(TID_RESP *resp) } if (NULL == resp->servers) { - fprintf(stderr, "tr_msg_encode_tidresp(): No servers to encode.\n"); + tr_debug("tr_msg_encode_tidresp(): No servers to encode."); return jresp; } jservers = tr_msg_encode_servers(resp); @@ -376,7 +376,7 @@ static TID_RESP *tr_msg_decode_tidresp(json_t *jresp) json_t *jerr_msg = NULL; if (!(tresp = talloc_zero(NULL, TID_RESP))) { - fprintf (stderr, "tr_msg_decode_tidresp(): Error allocating TID_RESP structure.\n"); + tr_crit("tr_msg_decode_tidresp(): Error allocating TID_RESP structure."); return NULL; } @@ -390,13 +390,13 @@ static TID_RESP *tr_msg_decode_tidresp(json_t *jresp) (!json_is_string(jrealm)) || (NULL == (jcomm = json_object_get(jresp, "comm"))) || (!json_is_string(jcomm))) { - fprintf (stderr, "tr_msg_decode_tidresp(): Error parsing response.\n"); + tr_debug("tr_msg_decode_tidresp(): Error parsing response."); talloc_free(tresp); return NULL; } if (0 == (strcmp(json_string_value(jresult), "success"))) { - fprintf(stderr, "tr_msg_decode_tidresp(): Success! result = %s.\n", json_string_value(jresult)); + tr_debug("tr_msg_decode_tidresp(): Success! result = %s.", json_string_value(jresult)); if ((NULL != (jservers = json_object_get(jresp, "servers"))) || (!json_is_array(jservers))) { tresp->servers = tr_msg_decode_servers(tresp, jservers, &tresp->num_servers); @@ -409,7 +409,7 @@ static TID_RESP *tr_msg_decode_tidresp(json_t *jresp) } else { tresp->result = TID_ERROR; - fprintf(stderr, "tr_msg_decode_tidresp(): Error! result = %s.\n", json_string_value(jresult)); + tr_debug("tr_msg_decode_tidresp(): Error! result = %s.", json_string_value(jresult)); if ((NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) || (!json_is_string(jerr_msg))) { tresp->err_msg = tr_new_name((char *)json_string_value(jerr_msg)); @@ -471,12 +471,12 @@ TR_MSG *tr_msg_decode(char *jbuf, size_t buflen) const char *mtype = NULL; if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) { - fprintf (stderr, "tr_msg_decode(): error loading object\n"); + tr_debug("tr_msg_decode(): error loading object"); return NULL; } if (!(msg = malloc(sizeof(TR_MSG)))) { - fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n"); + tr_debug("tr_msg_decode(): Error allocating TR_MSG structure."); json_decref(jmsg); return NULL; } @@ -485,7 +485,7 @@ TR_MSG *tr_msg_decode(char *jbuf, size_t buflen) if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) || (NULL == (jbody = json_object_get(jmsg, "msg_body")))) { - fprintf (stderr, "tr_msg_decode(): Error parsing message header.\n"); + tr_debug("tr_msg_decode(): Error parsing message header."); json_decref(jmsg); tr_msg_free_decoded(msg); return NULL; diff --git a/common/tr_rp.c b/common/tr_rp.c index 9711fbb..7244cbd 100644 --- a/common/tr_rp.c +++ b/common/tr_rp.c @@ -36,13 +36,14 @@ #include #include #include +#include TR_RP_CLIENT *tr_rp_client_lookup(TR_INSTANCE *tr, TR_NAME *gss_name) { TR_RP_CLIENT *rp = NULL; int i = 0; if ((!tr) || (!tr->active_cfg) || (!gss_name)) { - fprintf(stderr, "tr_rp_client_lookup: Bad parameters.\n"); + tr_debug("tr_rp_client_lookup: Bad parameters."); return NULL; } diff --git a/tid/example/tidc_main.c b/tid/example/tidc_main.c index 3088e6b..9f27018 100644 --- a/tid/example/tidc_main.c +++ b/tid/example/tidc_main.c @@ -37,6 +37,7 @@ #include #include +#include #include #include @@ -98,7 +99,11 @@ int main (int argc, int rc; gss_ctx_id_t gssctx; + /* set logging levels */ talloc_set_log_stderr(); + tr_log_threshold(LOG_CRIT); + tr_console_threshold(LOG_DEBUG); + /* Parse command-line arguments */ if (argc < 5 || argc > 6) { tidc_print_usage(argv[0]); diff --git a/tid/example/tids_main.c b/tid/example/tids_main.c index 29a43b8..b322de4 100644 --- a/tid/example/tids_main.c +++ b/tid/example/tids_main.c @@ -104,7 +104,7 @@ static int handle_authorizations(TID_REQ *req, const unsigned char *dh_hash, int sqlite3_result; if (!req->cons) { - tr_debug("Request has no constraints, so no authorizations.\n"); + tr_debug("Request has no constraints, so no authorizations."); return 0; } intersected = tr_constraint_set_intersect(req, req->cons); @@ -118,17 +118,17 @@ static int handle_authorizations(TID_REQ *req, const unsigned char *dh_hash, intersected, "realm", &realm_wc, &realm_len)) return -1; - tr_debug(" %u domain constraint matches and %u realm constraint matches\n", + tr_debug(" %u domain constraint matches and %u realm constraint matches", (unsigned) domain_len, (unsigned) realm_len); if (0 != sqlify_wc(req, domain_wc, domain_len, &error)) { - tr_debug("Processing domain constraints: %s\n", error); + tr_debug("Processing domain constraints: %s", error); return -1; }else if (0 != sqlify_wc(req, realm_wc, realm_len, &error)) { - tr_debug("Processing realm constraints: %s\n", error); + tr_debug("Processing realm constraints: %s", error); return -1; } if (!authorization_insert) { - tr_debug( " No database, no authorizations inserted\n"); + tr_debug( " No database, no authorizations inserted"); return 0; } for (domain_index = 0; domain_index < domain_len; domain_index++) @@ -143,7 +143,7 @@ static int handle_authorizations(TID_REQ *req, const unsigned char *dh_hash, sqlite3_bind_text(authorization_insert, 5, req->comm->buf, req->comm->len, SQLITE_TRANSIENT); sqlite3_result = sqlite3_step(authorization_insert); if (SQLITE_DONE != sqlite3_result) - printf("sqlite3: failed to write to database\n"); + tr_crit("sqlite3: failed to write to database"); sqlite3_reset(authorization_insert); } return 0; @@ -162,18 +162,18 @@ static int tids_req_handler (TIDS_INSTANCE *tids, size_t pub_digest_len; - fprintf(stdout, "tids_req_handler: Request received! target_realm = %s, community = %s\n", req->realm->buf, req->comm->buf); + tr_debug("tids_req_handler: Request received! target_realm = %s, community = %s", req->realm->buf, req->comm->buf); if (tids) tids->req_count++; if (!(resp) || !resp) { - fprintf(stderr, "tids_req_handler: No response structure.\n"); + tr_debug("tids_req_handler: No response structure."); return -1; } /* Allocate a new server block */ if (NULL == (resp->servers = malloc(sizeof(TID_SRVR_BLK)))){ - fprintf(stderr, "tids_req_handler(): malloc failed.\n"); + tr_crit("tids_req_handler(): malloc failed."); return -1; } memset(resp->servers, 0, sizeof(TID_SRVR_BLK)); @@ -182,12 +182,12 @@ static int tids_req_handler (TIDS_INSTANCE *tids, /* TBD -- Set up the server IP Address */ if (!(req) || !(req->tidc_dh)) { - fprintf(stderr, "tids_req_handler(): No client DH info.\n"); + tr_debug("tids_req_handler(): No client DH info."); return -1; } if ((!req->tidc_dh->p) || (!req->tidc_dh->g)) { - fprintf(stderr, "tids_req_handler(): NULL dh values.\n"); + tr_debug("tids_req_handler: NULL dh values."); return -1; } @@ -196,12 +196,12 @@ static int tids_req_handler (TIDS_INSTANCE *tids, // fprintf(stderr, "...from client DH block, dh_g = %s, dh_p = %s.\n", BN_bn2hex(req->tidc_dh->g), BN_bn2hex(req->tidc_dh->p)); if (NULL == (resp->servers->aaa_server_dh = tr_create_matching_dh(NULL, 0, req->tidc_dh))) { - fprintf(stderr, "tids_req_handler(): Can't create server DH params.\n"); + tr_debug("tids_req_handler: Can't create server DH params."); return -1; } if (0 == inet_aton(tids->ipaddr, &(resp->servers->aaa_server_addr))) { - fprintf(stderr, "tids_req_handler(): inet_aton() failed.\n"); + tr_debug("tids_req_handler: inet_aton() failed."); return -1; } @@ -216,12 +216,12 @@ static int tids_req_handler (TIDS_INSTANCE *tids, if (0 > (s_keylen = tr_compute_dh_key(&s_keybuf, req->tidc_dh->pub_key, resp->servers->aaa_server_dh))) { - fprintf(stderr, "tids_req_handler(): Key computation failed."); + tr_debug("tids_req_handler: Key computation failed."); return -1; } if (0 != tr_dh_pub_hash(req, &pub_digest, &pub_digest_len)) { - tr_debug("Unable to digest client public key\n"); + tr_debug("tids_req_handler: Unable to digest client public key"); return -1; } if (0 != handle_authorizations(req, pub_digest, pub_digest_len)) @@ -233,7 +233,7 @@ static int tids_req_handler (TIDS_INSTANCE *tids, sqlite3_bind_blob(insert_stmt, 3, pub_digest, pub_digest_len, SQLITE_TRANSIENT); sqlite3_result = sqlite3_step(insert_stmt); if (SQLITE_DONE != sqlite3_result) - printf("sqlite3: failed to write to database\n"); + tr_crit("sqlite3: failed to write to database"); sqlite3_reset(insert_stmt); } @@ -269,11 +269,16 @@ int main (int argc, fprintf(stdout, "Usage: %s \n", argv[0]); exit(1); } + + /* set logging levels */ + tr_log_threshold(LOG_CRIT); + tr_console_threshold(LOG_DEBUG); + ipaddr = (char *)argv[1]; gssname = tr_new_name((char *) argv[2]); hostname = argv[3]; if (SQLITE_OK != sqlite3_open(argv[4], &db)) { - fprintf(stdout, "Error opening database %s\n", argv[4]); + tr_crit("Error opening database %s", argv[4]); exit(1); } sqlite3_busy_timeout( db, 1000); @@ -284,7 +289,7 @@ int main (int argc, /* Create a TID server instance */ if (NULL == (tids = tids_create())) { - fprintf(stdout, "Unable to create TIDS instance, exiting.\n"); + tr_crit("Unable to create TIDS instance, exiting."); return 1; } @@ -293,7 +298,7 @@ int main (int argc, /* Start-up the server, won't return unless there is an error. */ rc = tids_start(tids, &tids_req_handler , auth_handler, hostname, TID_PORT, gssname); - fprintf(stdout, "Error in tids_start(), rc = %d. Exiting.\n", rc); + tr_crit("Error in tids_start(), rc = %d. Exiting.", rc); /* Clean-up the TID server instance */ tids_destroy(tids); diff --git a/tid/tid_req.c b/tid/tid_req.c index 23599ad..7575352 100644 --- a/tid/tid_req.c +++ b/tid/tid_req.c @@ -38,6 +38,8 @@ #include #include +#include + #include static int destroy_tid_req(TID_REQ *req) @@ -174,7 +176,7 @@ TID_REQ *tid_dup_req (TID_REQ *orig_req) TID_REQ *new_req = NULL; if (NULL == (new_req = malloc(sizeof(TID_REQ)))) { - fprintf(stderr, "tid_dup_req: Can't allocated duplicate request.\n"); + tr_crit("tid_dup_req: Can't allocated duplicate request."); return NULL; } @@ -185,12 +187,12 @@ TID_REQ *tid_dup_req (TID_REQ *orig_req) if ((NULL == (new_req->rp_realm = tr_dup_name(orig_req->rp_realm))) || (NULL == (new_req->realm = tr_dup_name(orig_req->realm))) || (NULL == (new_req->comm = tr_dup_name(orig_req->comm)))) { - fprintf(stderr, "tid_dup_req: Can't duplicate request (names).\n"); + tr_crit("tid_dup_req: Can't duplicate request (names)."); } if (orig_req->orig_coi) { if (NULL == (new_req->orig_coi = tr_dup_name(orig_req->orig_coi))) { - fprintf(stderr, "tid_dup_req: Can't duplicate request (orig_coi).\n"); + tr_crit("tid_dup_req: Can't duplicate request (orig_coi)."); } } diff --git a/tid/tids.c b/tid/tids.c index ed525d8..ada82a6 100644 --- a/tid/tids.c +++ b/tid/tids.c @@ -53,7 +53,7 @@ static TID_RESP *tids_create_response (TIDS_INSTANCE *tids, TID_REQ *req) TID_RESP *resp; if ((NULL == (resp = talloc_zero(req, TID_RESP)))) { - fprintf(stderr, "tids_create_response: Error allocating response structure.\n"); + tr_crit("tids_create_response: Error allocating response structure."); return NULL; } @@ -61,12 +61,12 @@ static TID_RESP *tids_create_response (TIDS_INSTANCE *tids, TID_REQ *req) 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"); + tr_crit("tids_create_response: Error allocating fields in response."); 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"); + tr_crit("tids_create_response: Error allocating fields in response."); return NULL; } } @@ -102,24 +102,24 @@ static int tids_listen (TIDS_INSTANCE *tids, int port) } addr; struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4; - + saddr->sin_port = htons (port); saddr->sin_family = AF_INET; saddr->sin_addr.s_addr = INADDR_ANY; if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0))) return conn; - + setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in)))) return rc; - + if (0 > (rc = listen(conn, 512))) return rc; - - fprintf (stdout, "tids_listen: TID Server listening on port %d\n", port); - return conn; + + tr_debug("tids_listen: TID Server listening on port %d", port); + return conn; } static int tids_auth_cb(gss_name_t clientName, gss_buffer_t displayName, @@ -145,20 +145,20 @@ static int tids_auth_connection (struct tids_instance *inst, nameBuffer.value = name; if (rc = gsscon_passive_authenticate(conn, nameBuffer, gssctx, tids_auth_cb, inst)) { - fprintf(stderr, "tids_auth_connection: Error from gsscon_passive_authenticate(), rc = %d.\n", rc); + tr_debug("tids_auth_connection: Error from gsscon_passive_authenticate(), rc = %d.", rc); return -1; } if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) { - fprintf(stderr, "tids_auth_connection: Error from gsscon_authorize, rc = %d, autherr = %d.\n", + tr_debug("tids_auth_connection: Error from gsscon_authorize, rc = %d, autherr = %d.", rc, autherr); return -1; } if (auth) - fprintf(stdout, "tids_auth_connection: Connection authenticated, conn = %d.\n", conn); + tr_debug("tids_auth_connection: Connection authenticated, conn = %d.", conn); else - fprintf(stderr, "tids_auth_connection: Authentication failed, conn %d.\n", conn); + tr_debug("tids_auth_connection: Authentication failed, conn %d.", conn); return !auth; } @@ -175,18 +175,18 @@ static int tids_read_request (TIDS_INSTANCE *tids, int conn, gss_ctx_id_t *gssct return -1; } - fprintf(stdout, "tids_read_request():Request Received, %u bytes.\n", (unsigned) buflen); + tr_debug("tids_read_request():Request Received, %u bytes.", (unsigned) buflen); /* Parse request */ if (NULL == ((*mreq) = tr_msg_decode(buf, buflen))) { - fprintf(stderr, "tids_read_request():Error decoding request.\n"); + tr_debug("tids_read_request():Error decoding request."); free (buf); return -1; } /* If this isn't a TID Request, just drop it. */ if (TID_REQUEST != (*mreq)->msg_type) { - fprintf(stderr, "tids_read_request(): Not a TID Request, dropped.\n"); + tr_debug("tids_read_request(): Not a TID Request, dropped."); return -1; } @@ -203,7 +203,7 @@ static int tids_handle_request (TIDS_INSTANCE *tids, TR_MSG *mreq, TID_RESP *res (!tr_msg_get_req(mreq)->rp_realm) || (!tr_msg_get_req(mreq)->realm) || (!tr_msg_get_req(mreq)->comm)) { - fprintf(stderr, "tids_handle_request():Not a valid TID Request.\n"); + tr_notice("tids_handle_request(): Not a valid TID Request."); resp->result = TID_ERROR; resp->err_msg = tr_new_name("Bad request format"); return -1; @@ -235,7 +235,7 @@ int tids_send_err_response (TIDS_INSTANCE *tids, TID_REQ *req, const char *err_m return 0; if (NULL == (resp = tids_create_response(tids, req))) { - fprintf(stderr, "tids_send_err_response: Can't create response.\n"); + tr_crit("tids_send_err_response: Can't create response."); return -1; } @@ -256,7 +256,7 @@ int tids_send_response (TIDS_INSTANCE *tids, TID_REQ *req, TID_RESP *resp) char *resp_buf; if ((!tids) || (!req) || (!resp)) - fprintf (stderr, "tids_send_response: Invalid parameters.\n"); + tr_debug("tids_send_response: Invalid parameters."); /* Never send a second response if we already sent one. */ if (req->resp_sent) @@ -264,16 +264,17 @@ int tids_send_response (TIDS_INSTANCE *tids, TID_REQ *req, TID_RESP *resp) mresp.msg_type = TID_RESPONSE; tr_msg_set_resp(&mresp, resp); - + if (NULL == (resp_buf = tr_msg_encode(&mresp))) { + fprintf(stderr, "tids_send_response: Error encoding json response.\n"); tr_audit_req(req); return -1; } - fprintf(stderr, "tids_send_response: Encoded response:\n%s\n", resp_buf); - + tr_debug("tids_send_response: Encoded response: %s", resp_buf); + /* If external logging is enabled, fire off a message */ /* TODO Can be moved to end once segfault in gsscon_write_encrypted_token fixed */ tr_audit_resp(resp); @@ -281,7 +282,7 @@ int tids_send_response (TIDS_INSTANCE *tids, TID_REQ *req, TID_RESP *resp) /* Send the response over the connection */ if (err = gsscon_write_encrypted_token (req->conn, req->gssctx, resp_buf, strlen(resp_buf) + 1)) { - fprintf(stderr, "tids_send_response: Error sending response over connection.\n"); + tr_notice("tids_send_response: Error sending response over connection."); tr_audit_req(req); @@ -304,17 +305,17 @@ static void tids_handle_connection (TIDS_INSTANCE *tids, int conn) gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT; if (tids_auth_connection(tids, conn, &gssctx)) { - fprintf(stderr, "tids_handle_connection: Error authorizing TID Server connection.\n"); + tr_notice("tids_handle_connection: Error authorizing TID Server connection."); close(conn); return; } - fprintf(stdout, "tids_handle_connection: Connection authorized!\n"); + tr_debug("tids_handle_connection: Connection authorized!"); while (1) { /* continue until an error breaks us out */ if (0 > (rc = tids_read_request(tids, conn, &gssctx, &mreq))) { - fprintf(stderr, "tids_handle_connection: Error from tids_read_request(), rc = %d.\n", rc); + tr_debug("tids_handle_connection: Error from tids_read_request(), rc = %d.", rc); return; } else if (0 == rc) { continue; @@ -326,22 +327,22 @@ static void tids_handle_connection (TIDS_INSTANCE *tids, int conn) /* Allocate a response structure and populate common fields */ if (NULL == (resp = tids_create_response (tids, tr_msg_get_req(mreq)))) { - fprintf(stderr, "tids_handle_connection: Error creating response structure.\n"); + tr_crit("tids_handle_connection: Error creating response structure."); /* try to send an error */ - tids_send_err_response(tids, tr_msg_get_req(mreq), "Error creating response.\n"); + tids_send_err_response(tids, tr_msg_get_req(mreq), "Error creating response."); return; } if (0 > (rc = tids_handle_request(tids, mreq, resp))) { - fprintf(stderr, "tids_handle_connection: Error from tids_handle_request(), rc = %d.\n", rc); + tr_debug("tids_handle_connection: Error from tids_handle_request(), rc = %d.", rc); /* Fall through, to send the response, either way */ } if (0 > (rc = tids_send_response(tids, tr_msg_get_req(mreq), resp))) { - fprintf(stderr, "tids_handle_connection: Error from tids_send_response(), rc = %d.\n", rc); + tr_debug("tids_handle_connection: Error from tids_send_response(), rc = %d.", rc); /* if we didn't already send a response, try to send a generic error. */ if (!tr_msg_get_req(mreq)->resp_sent) - tids_send_err_response(tids, tr_msg_get_req(mreq), "Error sending response.\n"); + tids_send_err_response(tids, tr_msg_get_req(mreq), "Error sending response."); /* Fall through to free the response, either way. */ } @@ -410,11 +411,9 @@ int tids_start (TIDS_INSTANCE *tids, void tids_destroy (TIDS_INSTANCE *tids) { - /* close syslog connection if syslog is enabled */ + /* clean up logfiles */ tr_log_close(); if (tids) free(tids); } - - diff --git a/tr/tr_main.c b/tr/tr_main.c index 8777354..9c8f909 100644 --- a/tr/tr_main.c +++ b/tr/tr_main.c @@ -42,6 +42,7 @@ #include #include #include +#include /* Structure to hold TR instance and original request in one cookie */ typedef struct tr_resp_cookie { @@ -55,7 +56,7 @@ static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc, TID_RESP *resp, void *resp_cookie) { - fprintf(stderr, "tr_tidc_resp_handler: Response received (conn = %d)! Realm = %s, Community = %s.\n", ((TR_RESP_COOKIE *)resp_cookie)->orig_req->conn, resp->realm->buf, resp->comm->buf); + tr_debug("tr_tidc_resp_handler: Response received (conn = %d)! Realm = %s, Community = %s.", ((TR_RESP_COOKIE *)resp_cookie)->orig_req->conn, resp->realm->buf, resp->comm->buf); req->resp_rcvd = 1; /* TBD -- handle concatentation of multiple responses to single req */ @@ -82,23 +83,23 @@ static int tr_tids_req_handler (TIDS_INSTANCE *tids, int rc = 0; if ((!tids) || (!orig_req) || (!resp) || (!tr)) { - fprintf(stderr, "tids_req_handler: Bad parameters\n"); + tr_debug("tr_tids_req_handler: Bad parameters"); return -1; } - fprintf(stdout, "tr_tids_req_handler: Request received (conn = %d)! Realm = %s, Comm = %s\n", orig_req->conn, + tr_debug("tr_tids_req_handler: Request received (conn = %d)! Realm = %s, Comm = %s", orig_req->conn, orig_req->realm->buf, orig_req->comm->buf); if (tids) tids->req_count++; /* Duplicate the request, so we can modify and forward it */ if (NULL == (fwd_req = tid_dup_req(orig_req))) { - fprintf(stderr, "tr_tids_req_handler: Unable to duplicate request.\n"); + tr_debug("tr_tids_req_handler: Unable to duplicate request."); return -1; } if (NULL == (cfg_comm = tr_comm_lookup((TR_INSTANCE *)tids->cookie, orig_req->comm))) { - fprintf(stderr, "tr_tids_req_hander: Request for unknown comm: %s.\n", orig_req->comm->buf); + tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", orig_req->comm->buf); tids_send_err_response(tids, orig_req, "Unknown community"); return -1; } @@ -108,30 +109,30 @@ static int tr_tids_req_handler (TIDS_INSTANCE *tids, if ((!((TR_INSTANCE *)tr)->rp_gss) || (!((TR_INSTANCE *)tr)->rp_gss->filter)) { - fprintf(stderr, "tr_tids_req_handler: No GSS name for incoming request.\n"); + tr_notice("tr_tids_req_handler: No GSS name for incoming request."); tids_send_err_response(tids, orig_req, "No GSS name for request"); return -1; } if ((TR_FILTER_NO_MATCH == tr_filter_process_rp_permitted(orig_req->rp_realm, ((TR_INSTANCE *)tr)->rp_gss->filter, orig_req->cons, &fwd_req->cons, &oaction)) || (TR_FILTER_ACTION_REJECT == oaction)) { - fprintf(stderr, "tr_tids_req_handler: RP realm (%s) does not match RP Realm filter for GSS name\n", orig_req->rp_realm->buf); + tr_notice("tr_tids_req_handler: RP realm (%s) does not match RP Realm filter for GSS name", orig_req->rp_realm->buf); tids_send_err_response(tids, orig_req, "RP Realm filter error"); return -1; } /* Check that the rp_realm is a member of the community in the request */ if (NULL == (tr_find_comm_rp(cfg_comm, orig_req->rp_realm))) { - fprintf(stderr, "tr_tids_req_handler: RP Realm (%s) not member of community (%s).\n", orig_req->rp_realm->buf, orig_req->comm->buf); + tr_notice("tr_tids_req_handler: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf); tids_send_err_response(tids, orig_req, "RP COI membership error"); return -1; } /* Map the comm in the request from a COI to an APC, if needed */ if (TR_COMM_COI == cfg_comm->type) { - fprintf(stderr, "tr_tids_req_handler: Community was a COI, switching.\n"); + tr_debug("tr_tids_req_handler: Community was a COI, switching."); /* TBD -- In theory there can be more than one? How would that work? */ if ((!cfg_comm->apcs) || (!cfg_comm->apcs->id)) { - fprintf(stderr, "No valid APC for COI %s.\n", orig_req->comm->buf); + tr_notice("No valid APC for COI %s.", orig_req->comm->buf); tids_send_err_response(tids, orig_req, "No valid APC for community"); return -1; } @@ -139,7 +140,7 @@ static int tr_tids_req_handler (TIDS_INSTANCE *tids, /* Check that the APC is configured */ if (NULL == (cfg_apc = tr_comm_lookup((TR_INSTANCE *)tids->cookie, apc))) { - fprintf(stderr, "tr_tids_req_hander: Request for unknown comm: %s.\n", apc->buf); + tr_notice("tr_tids_req_hander: Request for unknown comm: %s.", apc->buf); tids_send_err_response(tids, orig_req, "Unknown APC"); return -1; } @@ -149,7 +150,7 @@ static int tr_tids_req_handler (TIDS_INSTANCE *tids, /* Check that rp_realm is a member of this APC */ if (NULL == (tr_find_comm_rp(cfg_apc, orig_req->rp_realm))) { - fprintf(stderr, "tr_tids_req_hander: RP Realm (%s) not member of community (%s).\n", orig_req->rp_realm->buf, orig_req->comm->buf); + tr_notice("tr_tids_req_hander: RP Realm (%s) not member of community (%s).", orig_req->rp_realm->buf, orig_req->comm->buf); tids_send_err_response(tids, orig_req, "RP APC membership error"); return -1; } @@ -159,22 +160,22 @@ static int tr_tids_req_handler (TIDS_INSTANCE *tids, if (NULL == (aaa_servers = tr_idp_aaa_server_lookup((TR_INSTANCE *)tids->cookie, orig_req->realm, orig_req->comm))) { - fprintf(stderr, "tr_tids_req_handler: No AAA Servers for realm %s, defaulting.\n", orig_req->realm->buf); + tr_debug("tr_tids_req_handler: No AAA Servers for realm %s, defaulting.", orig_req->realm->buf); if (NULL == (aaa_servers = tr_default_server_lookup ((TR_INSTANCE *)tids->cookie, orig_req->comm))) { - fprintf(stderr, "tr_tids_req_handler: No default AAA servers, discarded.\n"); - tids_send_err_response(tids, orig_req, "No path to AAA Server(s) for realm"); - return -1; + tr_notice("tr_tids_req_handler: No default AAA servers, discarded."); + tids_send_err_response(tids, orig_req, "No path to AAA Server(s) for realm"); + return -1; } } else { /* if we aren't defaulting, check idp coi and apc membership */ if (NULL == (tr_find_comm_idp(cfg_comm, fwd_req->realm))) { - fprintf(stderr, "tr_tids_req_handler: IDP Realm (%s) not member of community (%s).\n", orig_req->realm->buf, orig_req->comm->buf); + tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of community (%s).", orig_req->realm->buf, orig_req->comm->buf); tids_send_err_response(tids, orig_req, "IDP community membership error"); return -1; } if ( cfg_apc && (NULL == (tr_find_comm_idp(cfg_apc, fwd_req->realm)))) { - fprintf(stderr, "tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).\n", orig_req->realm->buf, orig_req->comm->buf); + tr_notice("tr_tids_req_handler: IDP Realm (%s) not member of APC (%s).", orig_req->realm->buf, orig_req->comm->buf); tids_send_err_response(tids, orig_req, "IDP APC membership error"); return -1; } @@ -185,7 +186,7 @@ static int tr_tids_req_handler (TIDS_INSTANCE *tids, /* Create a TID client instance */ if (NULL == (tidc = tidc_create())) { - fprintf(stderr, "tr_tids_req_hander: Unable to allocate TIDC instance.\n"); + tr_crit("tr_tids_req_hander: Unable to allocate TIDC instance."); tids_send_err_response(tids, orig_req, "Memory allocation failure"); return -1; } @@ -203,14 +204,14 @@ static int tr_tids_req_handler (TIDS_INSTANCE *tids, aaa_servers->hostname->buf, TID_PORT, &(fwd_req->gssctx)))) { - fprintf(stderr, "tr_tids_req_handler: Error in tidc_open_connection.\n"); + tr_notice("tr_tids_req_handler: Error in tidc_open_connection."); tids_send_err_response(tids, orig_req, "Can't open connection to next hop TIDS"); return -1; }; /* Send a TID request */ if (0 > (rc = tidc_fwd_request(tidc, fwd_req, &tr_tidc_resp_handler, (void *)&resp_cookie))) { - fprintf(stderr, "Error from tidc_fwd_request, rc = %d.\n", rc); + tr_notice("Error from tidc_fwd_request, rc = %d.", rc); tids_send_err_response(tids, orig_req, "Can't forward request to next hop TIDS"); return -1; } @@ -224,20 +225,20 @@ static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name, TR_RP_CLIENT *rp; if ((!client_name) || (!gss_name) || (!tr)) { - fprintf(stderr, "tr_tidc_gss_handler: Bad parameters.\n"); + tr_debug("tr_tidc_gss_handler: Bad parameters."); return -1; } /* look up the RP client matching the GSS name */ if ((NULL == (rp = tr_rp_client_lookup(tr, gss_name)))) { - fprintf(stderr, "tr_tids_gss_handler: Unknown GSS name %s\n", gss_name->buf); + tr_debug("tr_tids_gss_handler: Unknown GSS name %s", gss_name->buf); return -1; } /* Store the rp client in the TR_INSTANCE structure for now... * TBD -- fix me for new tasking model. */ ((TR_INSTANCE *)tr)->rp_gss = rp; - fprintf( stderr, "Client's GSS Name: %s\n", gss_name->buf); + tr_debug("Client's GSS Name: %s", gss_name->buf); return 0; } @@ -253,36 +254,36 @@ int main (int argc, const char *argv[]) /* create a Trust Router instance */ if (NULL == (tr = tr_create())) { - fprintf(stderr, "Unable to create Trust Router instance, exiting.\n"); + tr_crit("Unable to create Trust Router instance, exiting."); return 1; } /* find the configuration files */ if (0 == (n = tr_find_config_files(&cfg_files))) { - fprintf (stderr, "Can't locate configuration files, exiting.\n"); + tr_crit("Can't locate configuration files, exiting."); exit(1); } if (TR_CFG_SUCCESS != tr_parse_config(tr, n, cfg_files)) { - fprintf (stderr, "Error decoding configuration information, exiting.\n"); + tr_crit("Error decoding configuration information, exiting."); exit(1); } /* apply initial configuration */ if (TR_CFG_SUCCESS != (rc = tr_apply_new_config(tr))) { - fprintf (stderr, "Error applying configuration, rc = %d.\n", rc); + tr_crit("Error applying configuration, rc = %d.", rc); exit(1); } /* initialize the trust path query server instance */ if (0 == (tr->tids = tids_create ())) { - fprintf (stderr, "Error initializing Trust Path Query Server instance.\n"); + tr_crit("Error initializing Trust Path Query Server instance."); exit(1); } /* start the trust path query server, won't return unless fatal error. */ if (0 != (err = tids_start(tr->tids, &tr_tids_req_handler, &tr_tids_gss_handler, tr->active_cfg->internal->hostname, tr->active_cfg->internal->tids_port, (void *)tr))) { - fprintf (stderr, "Error from Trust Path Query Server, err = %d.\n", err); + tr_crit("Error from Trust Path Query Server, err = %d.", err); exit(err); }