labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pque...@apache.org
Subject svn commit: r744596 - in /labs/orthrus/trunk: include/orthrus.h src/ortcalc.c
Date Sun, 15 Feb 2009 02:02:03 GMT
Author: pquerna
Date: Sun Feb 15 02:02:03 2009
New Revision: 744596

URL: http://svn.apache.org/viewvc?rev=744596&view=rev
Log:
Flesh out what I think the public API should look like, and how I think ortcalc should consume
it.

Modified:
    labs/orthrus/trunk/include/orthrus.h
    labs/orthrus/trunk/src/ortcalc.c

Modified: labs/orthrus/trunk/include/orthrus.h
URL: http://svn.apache.org/viewvc/labs/orthrus/trunk/include/orthrus.h?rev=744596&r1=744595&r2=744596&view=diff
==============================================================================
--- labs/orthrus/trunk/include/orthrus.h (original)
+++ labs/orthrus/trunk/include/orthrus.h Sun Feb 15 02:02:03 2009
@@ -24,11 +24,28 @@
 extern "C" {
 #endif
 
-/* Opaque Structure repersenting an Orthrus Structure */
+/* Opaque Structure repersenting an Orthrus Context */
 typedef struct orthrus_t orthrus_t;
 
+/* Opaque Structure repersenting an OTP Response */
+typedef struct orthrus_response_t orthrus_response_t;
+
 orthrus_error_t* orthrus_create(apr_pool_t *pool, orthrus_t **ort);
 
+orthrus_error_t* orthrus_calculate(orthrus_t *ort,
+                                   orthrus_response_t **reply,
+                                   apr_uint64_t num,
+                                   const char *seed,
+                                   const char *pw,
+                                   apr_size_t pwlen,
+                                   apr_pool_t *pool);
+
+void orthrus_response_format_hex(orthrus_response_t *reply,
+                                 const char **output);
+
+void orthrus_response_format_words(orthrus_response_t *reply,
+                                   const char **output);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */

Modified: labs/orthrus/trunk/src/ortcalc.c
URL: http://svn.apache.org/viewvc/labs/orthrus/trunk/src/ortcalc.c?rev=744596&r1=744595&r2=744596&view=diff
==============================================================================
--- labs/orthrus/trunk/src/ortcalc.c (original)
+++ labs/orthrus/trunk/src/ortcalc.c Sun Feb 15 02:02:03 2009
@@ -16,6 +16,7 @@
 
 #include "orthrus.h"
 #include "apr_file_io.h"
+#include "apr_lib.h"
 
 #if APR_HAVE_STDLIB_H
 #include <stdlib.h> /* for atexit() */
@@ -26,33 +27,125 @@
 #define NL APR_EOL_STR
 #endif
 
-int main(int argc, const char * const argv[])
-{
+#ifndef PW_MAX_LEN
+#define PW_MAX_LEN 1024
+#endif
+
+typedef struct ortcalc_t {
   apr_pool_t *pool;
-  apr_status_t rv = APR_SUCCESS;
   apr_file_t *errfile;
+  apr_file_t *outfile;
+  const char *shortname;
   orthrus_t *ort;
+  char pwin[PW_MAX_LEN];
+  apr_uint64_t num;
+  const char *seed;
+  int showhex;
+} ortcalc_t;
+
+
+static orthrus_error_t* acquire_password(ortcalc_t *oc)
+{
+  apr_status_t rv;
+  apr_size_t bufsize = sizeof(oc->pwin);
+
+  rv = apr_password_get("Password: ", oc->pwin, &bufsize);
+  
+  if (rv) {
+    return orthrus_error_create(rv,  "Failed to read password");
+  }
+
+  return ORTHRUS_SUCCESS;
+}
+
+static void usage(ortcalc_t *oc)
+{
+  apr_file_printf(oc->errfile,
+    "%s -- Program to calculate OTP responses" NL
+    "Usage: %s [-V] [challenge]"
+    ""NL,
+    oc->shortname,
+    oc->shortname);
+}
+
+int main(int argc, const char * const argv[])
+{
+  orthrus_response_t *reply;
+  ortcalc_t oc;
+  apr_status_t rv = APR_SUCCESS;
   orthrus_error_t *err;
 
   apr_app_initialize(&argc, &argv, NULL);
   atexit(apr_terminate);
 
-  apr_pool_create(&pool, NULL);
+  memset(&oc, 0, sizeof(ortcalc_t));
 
-  rv = apr_file_open_stderr(&errfile, pool);
+  apr_pool_create(&oc.pool, NULL);
+
+  rv = apr_file_open_stderr(&oc.errfile, oc.pool);
   if (rv) {
     fprintf(stderr, "Failed to open stderr: %d", rv);
     return rv;
   }
+
+  rv = apr_file_open_stdout(&oc.outfile, oc.pool);
+  if (rv) {
+    apr_file_printf(oc.errfile, "failed to open stdout: (%d)"NL,
+                    rv);
+    return 1;
+  }
+  
+  if (argc) {
+    oc.shortname = apr_filepath_name_get(argv[0]);
+  }
+  else {
+    oc.shortname = "ortcalc";
+  }
   
-  err = orthrus_create(pool, &ort);
+  err = orthrus_create(oc.pool, &oc.ort);
 
   if (err) {
-    apr_file_printf(errfile, "[%s:%d] Failed to create orthrus instance: %s (%d)"NL,
+    apr_file_printf(oc.errfile, "[%s:%d] Failed to create orthrus instance: %s (%d)"NL,
                     err->file, err->line, err->msg, err->err);
     return 1;
   }
 
+  /* TODO: Parse Argv in more detail */
+  if (argc != 3) {
+    usage(&oc);
+    return 1;
+  }
+
+  err = acquire_password(&oc);
+  if (err) {
+    apr_file_printf(oc.errfile, "[%s:%d] acquire_password: %s (%d)"NL,
+                    err->file, err->line, err->msg, err->err);
+    return 1;
+  }
+
+  err = orthrus_calculate(oc.ort, &reply, oc.num, oc.seed,
+                          oc.pwin, strlen(oc.pwin),
+                          oc.pool);
+
+  if (err) {
+    apr_file_printf(oc.errfile, "[%s:%d] Failed to calculate OTP: %s (%d)"NL,
+                    err->file, err->line, err->msg, err->err);
+    return 1;
+  }
+  else {
+    const char *output;
+
+    if (oc.showhex) {
+      orthrus_response_format_hex(reply, &output);
+    }
+    else {
+      orthrus_response_format_words(reply, &output);
+    }
+
+    apr_file_printf(oc.outfile, "Reply: %s"NL, output);
+  }
+
+
   return 0;
 }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message