[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Refactor common file code into util.c; add published to des...



Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv18107/or

Modified Files:
	main.c or.h routers.c test.c 
Log Message:
Refactor common file code into util.c; add published to descriptors

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.103
retrieving revision 1.104
diff -u -d -r1.103 -r1.104
--- main.c	26 Sep 2003 10:03:50 -0000	1.103
+++ main.c	26 Sep 2003 18:27:35 -0000	1.104
@@ -433,25 +433,6 @@
   return (1000 - (now.tv_usec / 1000)); /* how many milliseconds til the next second? */
 }
 
-#define FN_ERROR -1
-#define FN_NOENT 0
-#define FN_FILE 1
-#define FN_DIR 2
-static int fn_exists(const char *fname)
-{
-  struct stat st;
-  if (stat(fname, &st)) {
-    if (errno == ENOENT) {
-      return FN_NOENT;
-    }
-    return FN_ERROR;
-  }
-  if (st.st_mode & S_IFDIR) 
-    return FN_DIR;
-  else
-    return FN_FILE;
-}
-
 static crypto_pk_env_t *init_key_from_file(const char *fname)
 {
   crypto_pk_env_t *prkey = NULL;
@@ -463,7 +444,7 @@
     goto error;
   }
 
-  switch(fn_exists(fname)) {
+  switch(file_status(fname)) {
   case FN_DIR:
   case FN_ERROR:
     log(LOG_ERR, "Can't read key from %s", fname);
@@ -479,22 +460,10 @@
       goto error;
     }
     log(LOG_INFO, "Generated key seems valid");
-    fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0400);
-    if (fd == -1) {
-      log(LOG_ERR, "Can't open %s for writing", fname);
-      goto error;
-    }
-    file = fdopen(fd, "w");
-    if (!file) {
-      log(LOG_ERR, "Can't fdopen %s for writing", fname);
-      goto error;
-    }
-    if (crypto_pk_write_private_key_to_file(prkey, file) < 0) {
-      log(LOG_ERR, "Can't write private key to %s", fname);
+    if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
+      log(LOG_ERR, "Couldn't write generated key to %s.", fname);
       goto error;
     }
-    fclose(file);
-    /* XXX fingerprint */
     return prkey;
   case FN_FILE:
     if (crypto_pk_read_private_key_from_filename(prkey, fname)) {
@@ -519,10 +488,9 @@
 static int init_keys(void)
 {
   char keydir[512];
-  char fingerprint[FINGERPRINT_LEN+1];
+  char fingerprint[FINGERPRINT_LEN+MAX_NICKNAME_LEN+3]; 
   char *cp;
   crypto_pk_env_t *prkey;
-  FILE *file;
 
   /* OP's don't need keys.  Just initialize the TLS context.*/
   if (!options.OnionRouter && !options.DirPort) {
@@ -538,34 +506,12 @@
     return -1;
   }
   strcpy(keydir, options.DataDirectory);
-  switch (fn_exists(keydir)) {
-  case FN_NOENT:
-    log_fn(LOG_ERR, "DataDirectory does not exist");
-    return -1;
-  case FN_ERROR:
-    log_fn(LOG_ERR, "DataDirectory can't be read");
-    return -1;
-  case FN_FILE:
-    log_fn(LOG_ERR, "DataDirectory is not a directory.");
+  if (check_private_dir(keydir, 1)) {
     return -1;
   }
   strcat(keydir, "/keys");
-  switch (fn_exists(keydir)) {
-  case FN_NOENT:
-    if (mkdir(keydir, 0700)) {
-      log_fn(LOG_ERR, "Error making key directory.");
-      return -1;
-    }
-    break;
-  case FN_ERROR:
-    log_fn(LOG_ERR, "Error reading key directory.");
-    return -1;
-  case FN_FILE:
-    log_fn(LOG_ERR, "Key directory is not a directory.");
+  if (check_private_dir(keydir, 1)) {
     return -1;
-  case FN_DIR:
-    chmod(keydir, 0700);
-    break;
   }
   cp = keydir + strlen(keydir); /* End of string. */
   assert(!*cp);
@@ -600,28 +546,23 @@
   }
   strcpy(keydir, options.DataDirectory);
   strcat(keydir, "/router.desc");
-  file = fopen(keydir, "w");
-  if (!file) {
-    log_fn(LOG_ERR, "Error opening %s for writing", keydir);
+  if (write_str_to_file(keydir, router_get_my_descriptor())) {
     return -1;
   }
-  fputs(router_get_my_descriptor(), file);
-  fclose(file);
   /* 5. Dump fingerprint to 'fingerprint' */
   strcpy(keydir, options.DataDirectory);
   strcat(keydir, "/fingerprint");
-  file = fopen(keydir, "w");
-  if (!file) {
-    log_fn(LOG_ERR, "Error opening %s for writing", keydir);
-    return -1;
-  }
-  if (crypto_pk_get_fingerprint(get_identity_key(), fingerprint)<0) {
+  assert(strlen(options.Nickname) <= MAX_NICKNAME_LEN);
+  strcpy(fingerprint, options.Nickname);
+  strcat(fingerprint, " ");
+  if (crypto_pk_get_fingerprint(get_identity_key(),
+                                fingerprint+strlen(fingerprint))<0) {
     log_fn(LOG_ERR, "Error computing fingerprint");
     return -1;
   }
-  fprintf(file, "%s %s\n", options.Nickname, fingerprint);
-  fclose(file);
-
+  strcat(fingerprint, "\n");
+  if (write_str_to_file(keydir, fingerprint))
+    return -1;
   return 0;
 }
 
@@ -774,6 +715,7 @@
   char *identity_pkey;
   char digest[20];
   char signature[128];
+  char published[32];
   int onion_pkeylen, link_pkeylen, identity_pkeylen;
   int written;
   int result=0;
@@ -796,9 +738,12 @@
     log_fn(LOG_WARNING,"write link_pkey to string failed!");
     return -1;
   }
+  strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&router->published_on));
   
   result = snprintf(s, maxlen, 
-                    "router %s %d %d %d %d\nonion-key\n%s"
+                    "router %s %d %d %d %d\n"
+                    "published %s\n"
+                    "onion-key\n%s"
                     "link-key\n%s"
                     "signing-key\n%s",
     router->address,
@@ -806,6 +751,7 @@
     router->ap_port,
     router->dir_port,
     router->bandwidth,
+    published,
     onion_pkey, link_pkey, identity_pkey);
 
   free(onion_pkey);
@@ -1005,6 +951,7 @@
   ri->or_port = options.ORPort;
   ri->ap_port = options.APPort;
   ri->dir_port = options.DirPort;
+  ri->published_on = time(NULL);
   ri->onion_pkey = crypto_pk_dup_key(get_onion_key());
   ri->link_pkey = crypto_pk_dup_key(get_link_key());
   ri->identity_pkey = crypto_pk_dup_key(get_identity_key());

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.139
retrieving revision 1.140
diff -u -d -r1.139 -r1.140
--- or.h	26 Sep 2003 10:03:50 -0000	1.139
+++ or.h	26 Sep 2003 18:27:35 -0000	1.140
@@ -101,6 +101,7 @@
 
 #define MAX_BUF_SIZE (640*1024)
 #define DEFAULT_BANDWIDTH_OP (1024 * 1000)
+#define MAX_NICKNAME_LEN 32
 
 #define ACI_TYPE_LOWER 0
 #define ACI_TYPE_HIGHER 1
@@ -326,6 +327,8 @@
   uint16_t or_port;
   uint16_t ap_port;
   uint16_t dir_port;
+
+  time_t published_on;
  
   crypto_pk_env_t *onion_pkey; /* public RSA key for onions */
   crypto_pk_env_t *link_pkey;  /* public RSA key for TLS */

Index: routers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routers.c,v
retrieving revision 1.52
retrieving revision 1.53
diff -u -d -r1.52 -r1.53
--- routers.c	26 Sep 2003 10:03:50 -0000	1.52
+++ routers.c	26 Sep 2003 18:27:35 -0000	1.53
@@ -318,6 +318,7 @@
   K_ONION_KEY,
   K_LINK_KEY,
   K_ROUTER_SIGNATURE,
+  K_PUBLISHED,
   _SIGNATURE, 
   _PUBLIC_KEY, 
   _ERR, 
@@ -337,6 +338,7 @@
   { "onion-key", K_ONION_KEY },
   { "link-key", K_LINK_KEY },
   { "router-signature", K_ROUTER_SIGNATURE },
+  { "published", K_PUBLISHED },
   { NULL, -1 }
 };
 
@@ -492,6 +494,7 @@
     case K_ONION_KEY: printf("Onion-key"); break;
     case K_LINK_KEY: printf("Link-key"); break;
     case K_ROUTER_SIGNATURE: printf("Router-signature"); break;
+    case K_PUBLISHED: printf("Published"); break;
     default:
       printf("?????? %d\n", tok->tp); return;
     }
@@ -513,7 +516,6 @@
 #endif
 
 
-
 /* return the first char of s that is not whitespace and not a comment */
 static char *eat_whitespace(char *s) {
   assert(s);
@@ -817,6 +819,7 @@
   char digest[128];
   directory_token_t _tok;
   directory_token_t *tok = &_tok;
+  struct tm published;
 
 #define NEXT_TOKEN()                                                     \
   do { if (router_get_next_token(s, tok)) {                              \
@@ -874,6 +877,19 @@
   
   log_fn(LOG_DEBUG,"or_port %d, ap_port %d, dir_port %d, bandwidth %d.",
     router->or_port, router->ap_port, router->dir_port, router->bandwidth);
+
+  NEXT_TOKEN();
+  if (tok->tp != K_PUBLISHED) {
+    log_fn(LOG_WARNING, "Missing published time"); goto err;
+  }
+  if (tok->val.cmd.n_args != 2) {
+    log_fn(LOG_WARNING, "Wrong number of arguments to published"); goto err;
+  }
+  tok->val.cmd.args[1][-1] = ' '; /* Re-insert space. */
+  if (!strptime(tok->val.cmd.args[0], "%Y-%m-%d %H:%M:%S", &published)) {
+    log_fn(LOG_WARNING, "Published time was unparseable"); goto err;
+  }
+  router->published_on = timegm(&published);
 
   NEXT_TOKEN();
   if (tok->tp != K_ONION_KEY) {

Index: test.c
===================================================================
RCS file: /home/or/cvsroot/src/or/test.c,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -d -r1.38 -r1.39
--- test.c	25 Sep 2003 05:17:11 -0000	1.38
+++ test.c	26 Sep 2003 18:27:35 -0000	1.39
@@ -519,6 +519,7 @@
   
   r1.address = "testaddr1.foo.bar";
   r1.addr = 0xc0a80001u; /* 192.168.0.1 */
+  r1.published_on = 0;
   r1.or_port = 9000;
   r1.ap_port = 9002;
   r1.dir_port = 9003;
@@ -539,6 +540,7 @@
   ex2.next = NULL;
   r2.address = "tor.tor.tor";
   r2.addr = 0x0a030201u; /* 10.3.2.1 */
+  r2.published_on = 5;
   r2.or_port = 9005;
   r2.ap_port = 0;
   r2.dir_port = 0;
@@ -555,7 +557,9 @@
   test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str, 
                                                     &pk3_str_len));
   
-  strcpy(buf2, "router testaddr1.foo.bar 9000 9002 9003 1000\nonion-key\n");
+  strcpy(buf2, "router testaddr1.foo.bar 9000 9002 9003 1000\n"
+         "published 1970-01-01 00:00:00\n"
+         "onion-key\n");
   strcat(buf2, pk1_str);
   strcat(buf2, "link-key\n");
   strcat(buf2, pk3_str);