Merge pull request #99 from painless-security/jennifer/count_failed_reqs
authorJennifer Richards <jennifer@painless-security.com>
Fri, 1 Jun 2018 19:58:49 +0000 (15:58 -0400)
committerGitHub <noreply@github.com>
Fri, 1 Jun 2018 19:58:49 +0000 (15:58 -0400)
Return separate counts of TID reqs that succeed and result in error

gsscon/gsscon_common.c
tid/example/tidc_main.c
trp/trps.c

index b8d4fcf..9d6ad3c 100755 (executable)
@@ -53,6 +53,8 @@
  */
 
 #include <gsscon.h>
+#include <fcntl.h>
+#include <poll.h>
 
 /* --------------------------------------------------------------------------- */
 /* Display the contents of the buffer in hex and ascii                         */
@@ -86,6 +88,7 @@ static void PrintBuffer (const char *inBuffer,
 /* --------------------------------------------------------------------------- */
 /* Standard network read loop, accounting for EINTR, EOF and incomplete reads  */
 
+#define READBUFFER_TIMEOUT_SECONDS (60 * 1000)
 static int ReadBuffer (int     inSocket, 
                        size_t  inBufferLength, 
                        char   *ioBuffer)
@@ -94,13 +97,37 @@ static int ReadBuffer (int     inSocket,
     ssize_t bytesRead = 0;
     
     if (!ioBuffer) { err = EINVAL; }
-    
+
+    /* Read in non-blocking mode */
+    if (!err) {
+        err = fcntl(inSocket, F_SETFL, O_NONBLOCK);
+    }
+
     if (!err) {
         char *ptr = ioBuffer;
         do {
-            ssize_t count = read (inSocket, ptr, inBufferLength - bytesRead);
+            ssize_t count;
+            struct pollfd fds = {inSocket, POLLIN, 0}; /* poll for data ready on the socket */
+            int poll_rc = 0;
+
+            poll_rc = poll(&fds, 1, READBUFFER_TIMEOUT_SECONDS);
+            if (poll_rc == 0) {
+                /* timed out */
+                err = ETIMEDOUT;
+                continue;
+            } else if (poll_rc < 0) {
+                /* try again if we were interrupted, otherwise exit */
+                if (errno != EINTR) {
+                    err = errno;
+                }
+                continue;
+            }
+
+            /* Data should be ready to read */
+            count = read (inSocket, ptr, inBufferLength - bytesRead);
             if (count < 0) {
-                /* Try again on EINTR */
+                /* Try again on EINTR (if we get EAGAIN or EWOULDBLOCK, something is wrong because
+                 * we just polled the fd) */
                 if (errno != EINTR) { err = errno; }
             } else if (count == 0) {
                 err = ECONNRESET; /* EOF and we expected data */
@@ -109,8 +136,8 @@ static int ReadBuffer (int     inSocket,
                 bytesRead += count;
             }
         } while (!err && (bytesRead < inBufferLength));
-    } 
-    
+    }
+
     if (err) { gsscon_print_error (err, "ReadBuffer failed"); }
 
     return err;
index f1dfce5..8ae56ca 100644 (file)
 #include <trust_router/tid.h>
 #include <tr_inet_util.h>
 
+struct tidc_resp_cookie {
+  int succeeded;
+};
+
 static void tidc_resp_handler (TIDC_INSTANCE * tidc, 
                        TID_REQ *req,
                        TID_RESP *resp, 
@@ -53,9 +57,12 @@ static void tidc_resp_handler (TIDC_INSTANCE * tidc,
   unsigned char *c_keybuf = NULL;
   int i;
   struct timeval tv;
+  struct tidc_resp_cookie *data = talloc_get_type_abort(cookie, struct tidc_resp_cookie);
 
   printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
 
+  data->succeeded = 0;
+
   /* Generate the client key -- TBD, handle more than one server */
   if (TID_SUCCESS != resp->result) {
     fprintf(stderr, "tidc_resp_handler: Response is an error.\n");
@@ -87,6 +94,7 @@ static void tidc_resp_handler (TIDC_INSTANCE * tidc,
   }
   printf("\n");
 
+  data->succeeded = 1;
   return;
 }
 
@@ -186,6 +194,11 @@ static error_t parse_option(int key, char *arg, struct argp_state *state)
   return 0; /* success */
 }
 
+/* Exit values */
+#define EXIT_OK 0
+#define EXIT_REQ_FAILED 2
+#define EXIT_ERROR 1
+
 /* assemble the argp parser */
 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
 
@@ -197,6 +210,7 @@ int main (int argc,
   int rc;
   gss_ctx_id_t gssctx;
   struct cmdline_args opts;
+  struct tidc_resp_cookie cookie = {0};
 
   /* parse the command line*/
   /* set defaults */
@@ -226,27 +240,30 @@ int main (int argc,
   tidc_set_dh(tidc, tr_create_dh_params(NULL, 0));
   if (tidc_get_dh(tidc) == NULL) {
     printf("Error creating client DH params.\n");
-    return 1;
+    return EXIT_ERROR;
   }
 
   /* Set-up TID connection */
   if (-1 == (conn = tidc_open_connection(tidc, opts.server, opts.port, &gssctx))) {
     /* Handle error */
     printf("Error in tidc_open_connection.\n");
-    return 1;
+    return EXIT_ERROR;
   };
 
   /* Send a TID request */
   if (0 > (rc = tidc_send_request(tidc, conn, gssctx, opts.rp_realm, opts.target_realm, opts.community, 
-                                 &tidc_resp_handler, NULL))) {
+                                 &tidc_resp_handler, &cookie))) {
     /* Handle error */
     printf("Error in tidc_send_request, rc = %d.\n", rc);
-    return 1;
+    return EXIT_ERROR;
   }
     
   /* Clean-up the TID client instance, and exit */
   tidc_destroy(tidc);
 
-  return 0;
+  if (cookie.succeeded)
+    return EXIT_OK;
+  else
+    return EXIT_REQ_FAILED;
 }
 
index a489133..b4051e1 100644 (file)
@@ -430,11 +430,11 @@ TRP_RC trps_authorize_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
 {
   /* try to establish a GSS context */
   if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
-    tr_notice("trps_authorize_connection: failed to authorize connection");
+    tr_debug("trps_authorize_connection: failed to authorize connection");
     trp_connection_close(conn);
     return TRP_ERROR;
   }
-  tr_notice("trps_authorize_connection: authorized connection");
+  tr_debug("trps_authorize_connection: authorized connection");
   return TRP_SUCCESS;
 }