示例#1
0
int main(int argc, const char *argv[])
{
  TALLOC_CTX *main_ctx=talloc_new(NULL);
  FILE *f;
  char *buf;
  size_t buflen;
  TR_MSG *msg;
  
  if (argc != 2) {
    printf("Usage: %s <input file>\n\n", argv[0]);
    exit(-1);
  }

  buf=malloc(MAX_MSG_LEN);
  if (!buf) {
    printf("Allocation error.\n\n");
    exit(-1);
  }

  f=fopen(argv[1], "r");
  if (!f) {
    printf("Error opening %s for reading.\n\n", argv[1]);
    exit(-1);
  }

  printf("Reading from %s...\n", argv[1]);

  buflen=fread(buf, sizeof(char), MAX_MSG_LEN, f);
  if (buflen==0) {
    printf("File empty.\n\n");
    exit(0);
  }

  if (buflen>=MAX_MSG_LEN)
    printf("Warning: file may exceed maximum message length (%d bytes).\n", MAX_MSG_LEN);

  msg= tr_msg_decode(NULL, buf, buflen);

/*  if (rc==TRP_SUCCESS)
    trp_msg_print(msg);*/

  printf("\nEncoding...\n");

  printf("Result: \n%s\n\n", tr_msg_encode(NULL, msg));

  talloc_report_full(main_ctx, stdout);

  return 0;
}
示例#2
0
/**
 * Produces a JSON-encoded msg containing the TID response
 *
 * @param mem_ctx talloc context for the return value
 * @param resp outgoing response
 * @return JSON-encoded message containing the TID response
 */
static char *tids_encode_response(TALLOC_CTX *mem_ctx, TID_RESP *resp)
{
  TR_MSG mresp;
  char *resp_buf = NULL;

  /* Construct the response message */
  tid_set_tr_msg_resp(&mresp, resp);

  /* Encode the message to JSON */
  resp_buf = tr_msg_encode(mem_ctx, &mresp);
  if (resp_buf == NULL) {
    tr_err("tids_encode_response: Error encoding json response.");
    return NULL;
  }
  tr_debug("tids_encode_response: Encoded response: %s", resp_buf);

  /* Success */
  return resp_buf;
}
示例#3
0
/**
 * Handle a request/response connection
 *
 * Authorizes/authenticates the connection, then reads a response, passes that to a
 * callback to get a response, sends that, then returns.
 *
 * @param conn connection file descriptor
 * @param acceptor_service acceptor name to present
 * @param acceptor_hostname acceptor hostname to present
 * @param auth_cb callback for authorization
 * @param auth_cookie cookie for the auth_cb
 * @param req_cb callback to handle the request and produce the response
 * @param req_cookie cookie for the req_cb
 */
TR_GSS_RC tr_gss_handle_connection(int conn,
                                   const char *acceptor_service,
                                   const char *acceptor_hostname,
                                   TR_GSS_AUTH_FN auth_cb,
                                   void *auth_cookie,
                                   TR_GSS_HANDLE_REQ_FN req_cb,
                                   void *req_cookie)
{
  TALLOC_CTX *tmp_ctx = talloc_new(NULL);
  gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
  char *req_str = NULL;
  size_t req_len = 0;
  TR_MSG *req_msg = NULL;
  TR_MSG *resp_msg = NULL;
  char *resp_str = NULL;
  TR_GSS_RC rc = TR_GSS_ERROR;

  tr_debug("tr_gss_handle_connection: Attempting to accept %s connection on fd %d.",
           acceptor_service, conn);

  if (tr_gss_auth_connection(conn,
                             acceptor_service,
                             acceptor_hostname,
                             &gssctx,
                             auth_cb,
                             auth_cookie)) {
    tr_notice("tr_gss_handle_connection: Error authorizing connection.");
    goto cleanup;
  }

  tr_debug("tr_gss_handle_connection: Connection authorized");

  // TODO: should there be a timeout on this?
  do {
    /* continue until an error breaks us out */
    // try to read a request
    req_str = tr_gss_read_req(tmp_ctx, conn, gssctx);

    if (req_str == NULL) {
      // an error occurred, give up
      tr_notice("tr_gss_handle_connection: Error reading request");
      goto cleanup;
    }

    req_len = strlen(req_str);

    /* If we got no characters, we will loop again. Free the empty response for the next loop. */
    if (req_len == 0)
      talloc_free(req_str);

  } while (req_len == 0);

  /* Decode the request */
  req_msg = tr_msg_decode(tmp_ctx, req_str, req_len);
  if (req_msg == NULL) {
    tr_notice("tr_gss_handle_connection: Error decoding response");
    goto cleanup;
  }

  /* Hand off the request for processing and get the response */
  rc = req_cb(tmp_ctx, req_msg, &resp_msg, req_cookie);

  if (resp_msg == NULL) {
    // no response, clean up
    goto cleanup;
  }

  /* Encode the response */
  resp_str = tr_msg_encode(tmp_ctx, resp_msg);
  if (resp_str == NULL) {
    /* We apparently can't encode a response, so just return */
    tr_err("tr_gss_handle_connection: Error encoding response");
    goto cleanup;
  }

  // send the response
  if (tr_gss_write_resp(conn, gssctx, resp_str)) {
    tr_err("tr_gss_handle_connection: Error writing response");
    goto cleanup;
  }

  /* we successfully sent a response */
  rc = TR_GSS_SUCCESS;

cleanup:
  talloc_free(tmp_ctx);
  return rc;
}
示例#4
0
int tidc_fwd_request(TIDC_INSTANCE *tidc,
                     TID_REQ *tid_req,
		     TIDC_RESP_FUNC *resp_handler,
                     void *cookie)
{
  char *req_buf = NULL;
  char *resp_buf = NULL;
  size_t resp_buflen = 0;
  TR_MSG *msg = NULL;
  TR_MSG *resp_msg = NULL;
  int err;
  int rc = 0;

  /* Create and populate a TID msg structure */
  if (!(msg = talloc_zero(tid_req, TR_MSG)))
    goto error;

  msg->msg_type = TID_REQUEST;
  tr_msg_set_req(msg, tid_req);

  /* store the response function and cookie */
  // tid_req->resp_func = resp_handler;
  // tid_req->cookie = cookie;


  /* Encode the request into a json string */
  if (!(req_buf = tr_msg_encode(msg))) {
    tr_err("tidc_fwd_request: Error encoding TID request.\n");
    goto error;
  }

  tr_debug( "tidc_fwd_request: Sending TID request:\n");
  tr_debug( "%s\n", req_buf);

  /* Send the request over the connection */
  if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf,
					  strlen(req_buf))) {
    tr_err( "tidc_fwd_request: Error sending request over connection.\n");
    goto error;
  }

  /* TBD -- queue request on instance, read resps in separate thread */

  /* Read the response from the connection */
  /* TBD -- timeout? */
  if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
    if (resp_buf)
      free(resp_buf);
    goto error;
  }

  tr_debug( "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
  tr_debug( "%s\n", resp_buf);

  if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
    tr_err( "tidc_fwd_request: Error decoding response.\n");
    goto error;
  }

  /* TBD -- Check if this is actually a valid response */
  if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
    tr_err( "tidc_fwd_request: Error, no response in the response!\n");
    goto error;
  }

  if (resp_handler) {
    /* Call the caller's response function. It must copy any data it needs before returning. */
    tr_debug("tidc_fwd_request: calling response callback function.");
    (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
  }

  goto cleanup;

 error:
  rc = -1;
 cleanup:
  if (msg)
    talloc_free(msg);
  if (req_buf)
    free(req_buf);
  if (resp_buf)
    free(resp_buf);
  if (resp_msg)
    tr_msg_free_decoded(resp_msg);
  return rc;
}