[or-cvs] tor --list-fingerprint to print fingerprint and exit

Nick Mathewson nickm at seul.org
Sat Oct 30 19:18:40 UTC 2004


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

Modified Files:
	or.h main.c config.c 
Log Message:
tor --list-fingerprint to print fingerprint and exit

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.448
retrieving revision 1.449
diff -u -d -r1.448 -r1.449
--- or.h	28 Oct 2004 18:37:52 -0000	1.448
+++ or.h	30 Oct 2004 19:18:37 -0000	1.449
@@ -832,6 +832,10 @@
 
 /** Configuration options for a Tor process */
 typedef struct {
+  /** What should the tor process actually do? */
+  enum {
+    CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT
+  } command;
   struct config_line_t *LogOptions; /**< List of configuration lines
                                      * for logfiles */
 

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.343
retrieving revision 1.344
diff -u -d -r1.343 -r1.344
--- main.c	28 Oct 2004 18:37:52 -0000	1.343
+++ main.c	30 Oct 2004 19:18:37 -0000	1.344
@@ -706,6 +706,11 @@
     return -1;
   }
 
+  /* Bail out at this point if we're not going to be a server: we want
+   * to not fork, and to log stuff to stderr. */
+  if (options.command != CMD_RUN_TOR)
+    return 0;
+
   /* Configure the log(s) */
   if (config_init_logs(&options)<0)
     return -1;
@@ -1095,11 +1100,31 @@
 void tor_cleanup(void) {
   /* Remove our pid file. We don't care if there was an error when we
    * unlink, nothing we could do about it anyways. */
-  if(options.PidFile)
+  if(options.PidFile && options.command == CMD_RUN_TOR)
     unlink(options.PidFile);
   crypto_global_cleanup();
 }
 
+/** Read/create keys as needed, and echo our fingerprint to stdout. */
+void do_list_fingerprint(void)
+{
+  char buf[FINGERPRINT_LEN+1];
+  crypto_pk_env_t *k;
+  if (init_keys() < 0) {
+    log_fn(LOG_ERR,"Error initializing keys; exiting");
+    return;
+  }
+  if (!(k = get_identity_key())) {
+    log_fn(LOG_ERR,"Error: missing identity key.");
+    return;
+  }    
+  if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
+    log_fn(LOG_ERR, "Error computing fingerprint");
+    return;
+  }
+  printf("%s %s\n", options.Nickname, buf);
+}
+
 #ifdef MS_WINDOWS_SERVICE
 void nt_service_control(DWORD request)
 {
@@ -1169,7 +1194,17 @@
 #else
   if (tor_init(argc, argv)<0)
     return -1;
-  do_main_loop();
+  switch (options.command) {
+  case CMD_RUN_TOR:
+    do_main_loop();
+    break;
+  case CMD_LIST_FINGERPRINT:
+    do_list_fingerprint();
+    break;
+  default:
+    log_fn(LOG_ERR, "Illegal command number %d: internal error.",
+           options.command);
+  }
   tor_cleanup();
   return -1;
 #endif

Index: config.c
===================================================================
RCS file: /home/or/cvsroot/src/or/config.c,v
retrieving revision 1.192
retrieving revision 1.193
diff -u -d -r1.192 -r1.193
--- config.c	27 Oct 2004 21:14:10 -0000	1.192
+++ config.c	30 Oct 2004 19:18:37 -0000	1.193
@@ -184,6 +184,8 @@
 //      log(LOG_DEBUG,"Commandline: skipping over -f.");
       i += 2; /* this is the config file option. ignore it. */
       continue;
+    } else if (!strcmp(argv[i],"--list-fingerprint")) {
+      i += 1; /* command-line option. ignore it. */
     }
 
     new = tor_malloc(sizeof(struct config_line_t));
@@ -726,19 +728,27 @@
     exit(0);
   }
 
-/* learn config file name, get config lines, assign them */
-  i = 1;
-  while (i < argc-1 && strcmp(argv[i],"-f")) {
-    i++;
+  /* learn config file name, get config lines, assign them */
+  fname = NULL;
+  using_default_torrc = 1;
+  options->command = CMD_RUN_TOR;
+  for (i = 1; i < argc; ++i) {
+    if (i < argc-1 && !strcmp(argv[i],"-f")) {
+      if (fname) {
+        log(LOG_WARN, "Duplicate -f options on command line.");
+        tor_free(fname);
+      }
+      fname = tor_strdup(argv[i+1]);
+      using_default_torrc = 0;
+      ++i;
+    } else if (!strcmp(argv[i],"--list-fingerprint")) {
+      options->command = CMD_LIST_FINGERPRINT;
+    }      
   }
 
-  if (i < argc-1) { /* we found one */
-    fname = tor_strdup(argv[i+1]);
-    using_default_torrc = 0;
-  } else {
+  if (using_default_torrc) {
     /* didn't find one, try CONFDIR */
     char *fn;
-    using_default_torrc = 1;
     fn = get_default_conf_file();
     if (fn && file_status(fn) == FN_FILE) {
       fname = fn;



More information about the tor-commits mailing list