[or-cvs] Fix bug in saving onion keys to disk when rotating; also su...

Nick Mathewson nickm at seul.org
Mon Aug 9 04:27:15 UTC 2004


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

Modified Files:
	router.c 
Log Message:
Fix bug in saving onion keys to disk when rotating; also survive short-term shutdowns better

Index: router.c
===================================================================
RCS file: /home/or/cvsroot/src/or/router.c,v
retrieving revision 1.81
retrieving revision 1.82
diff -u -d -r1.81 -r1.82
--- router.c	8 Aug 2004 05:26:13 -0000	1.81
+++ router.c	9 Aug 2004 04:27:13 -0000	1.82
@@ -98,8 +98,11 @@
 void rotate_onion_key(void)
 {
   char fname[512];
+  char fname_prev[512];
   crypto_pk_env_t *prkey;
-  sprintf(fname,"%s/keys/onion.key",get_data_directory(&options));
+  sprintf(fname,"%s/keys/secret_onion_key",get_data_directory(&options));
+  sprintf(fname_prev,"%s/keys/secret_onion_key.old",
+          get_data_directory(&options));
   if (!(prkey = crypto_new_pk_env())) {
     log(LOG_ERR, "Error creating crypto environment.");
     goto error;
@@ -108,6 +111,10 @@
     log(LOG_ERR, "Error generating onion key");
     goto error;
   }
+  if (file_status(fname) == FN_FILE) {
+    if (replace_file(fname, fname_prev))
+      goto error;
+  }
   if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
     log(LOG_ERR, "Couldn't write generated key to %s.", fname);
     goto error;
@@ -144,15 +151,9 @@
 init_key_from_file_name_changed(const char *fname_old,
                                 const char *fname_new)
 {
-  int fs;
 
-  fs = file_status(fname_new);
-  if (fs == FN_FILE)
-    /* The new filename is there. */
-    return init_key_from_file(fname_new);
-  fs = file_status(fname_old);
-  if (fs != FN_FILE)
-    /* There is no key under either name. */
+  if (file_status(fname_new) == FN_FILE || file_status(fname_old) != FN_FILE)
+    /* The new filename is there, or both are, or neither is. */
     return init_key_from_file(fname_new);
 
   /* The old filename exists, and the new one doesn't.  Rename and load. */
@@ -284,6 +285,12 @@
   prkey = init_key_from_file_name_changed(keydir,keydir2);
   if (!prkey) return -1;
   set_onion_key(prkey);
+  sprintf(keydir,"%s/keys/secret_onion_key.old",datadir);
+  if (file_status(keydir) == FN_FILE) {
+    prkey = init_key_from_file(keydir);
+    if (prkey)
+      lastonionkey = prkey;
+  }
 
   /* 3. Initialize link key and TLS context. */
   if (tor_tls_context_new(get_identity_key(), 1, options.Nickname,
@@ -620,7 +627,7 @@
 
   /* How busy have we been? */
   bandwidth_usage = rep_hist_get_bandwidth_lines();
-  
+
   /* Generate the easy portion of the router descriptor. */
   result = snprintf(s, maxlen,
                     "router %s %s %d %d %d\n"



More information about the tor-commits mailing list