123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605 |
- const char router_c_id[] =
- "$Id$";
- #define ROUTER_PRIVATE
- #include "or.h"
- extern long stats_n_seconds_working;
- static tor_mutex_t *key_lock=NULL;
- static time_t onionkey_set_at=0;
- static crypto_pk_env_t *onionkey=NULL;
- static crypto_pk_env_t *lastonionkey=NULL;
- static crypto_pk_env_t *identitykey=NULL;
- static char identitykey_digest[DIGEST_LEN];
- static void
- set_onion_key(crypto_pk_env_t *k)
- {
- tor_mutex_acquire(key_lock);
- onionkey = k;
- onionkey_set_at = time(NULL);
- tor_mutex_release(key_lock);
- mark_my_descriptor_dirty();
- }
- crypto_pk_env_t *
- get_onion_key(void)
- {
- tor_assert(onionkey);
- return onionkey;
- }
- void
- dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last)
- {
- tor_assert(key);
- tor_assert(last);
- tor_mutex_acquire(key_lock);
- tor_assert(onionkey);
- *key = crypto_pk_dup_key(onionkey);
- if (lastonionkey)
- *last = crypto_pk_dup_key(lastonionkey);
- else
- *last = NULL;
- tor_mutex_release(key_lock);
- }
- time_t
- get_onion_key_set_at(void)
- {
- return onionkey_set_at;
- }
- void
- set_identity_key(crypto_pk_env_t *k)
- {
- if (identitykey)
- crypto_free_pk_env(identitykey);
- identitykey = k;
- crypto_pk_get_digest(identitykey, identitykey_digest);
- }
- crypto_pk_env_t *
- get_identity_key(void)
- {
- tor_assert(identitykey);
- return identitykey;
- }
- int
- identity_key_is_set(void)
- {
- return identitykey != NULL;
- }
- void
- rotate_onion_key(void)
- {
- char fname[512];
- char fname_prev[512];
- crypto_pk_env_t *prkey;
- or_state_t *state = get_or_state();
- time_t now;
- tor_snprintf(fname,sizeof(fname),
- "%s"PATH_SEPARATOR"keys"PATH_SEPARATOR"secret_onion_key",
- get_options()->DataDirectory);
- tor_snprintf(fname_prev,sizeof(fname_prev),
- "%s"PATH_SEPARATOR"keys"PATH_SEPARATOR"secret_onion_key.old",
- get_options()->DataDirectory);
- if (!(prkey = crypto_new_pk_env())) {
- log_err(LD_GENERAL,"Error constructing rotated onion key");
- goto error;
- }
- if (crypto_pk_generate_key(prkey)) {
- log_err(LD_BUG,"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_err(LD_FS,"Couldn't write generated onion key to \"%s\".", fname);
- goto error;
- }
- log_info(LD_GENERAL, "Rotating onion key");
- tor_mutex_acquire(key_lock);
- if (lastonionkey)
- crypto_free_pk_env(lastonionkey);
- lastonionkey = onionkey;
- onionkey = prkey;
- now = time(NULL);
- state->LastRotatedOnionKey = onionkey_set_at = now;
- tor_mutex_release(key_lock);
- mark_my_descriptor_dirty();
- or_state_mark_dirty(state, get_options()->AvoidDiskWrites ? now+3600 : 0);
- return;
- error:
- log_warn(LD_GENERAL, "Couldn't rotate onion key.");
- }
- crypto_pk_env_t *
- init_key_from_file(const char *fname)
- {
- crypto_pk_env_t *prkey = NULL;
- FILE *file = NULL;
- if (!(prkey = crypto_new_pk_env())) {
- log_err(LD_GENERAL,"Error constructing key");
- goto error;
- }
- switch (file_status(fname)) {
- case FN_DIR:
- case FN_ERROR:
- log_err(LD_FS,"Can't read key from \"%s\"", fname);
- goto error;
- case FN_NOENT:
- log_info(LD_GENERAL, "No key found in \"%s\"; generating fresh key.",
- fname);
- if (crypto_pk_generate_key(prkey)) {
- log_err(LD_GENERAL,"Error generating onion key");
- goto error;
- }
- if (crypto_pk_check_key(prkey) <= 0) {
- log_err(LD_GENERAL,"Generated key seems invalid");
- goto error;
- }
- log_info(LD_GENERAL, "Generated key seems valid");
- if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
- log_err(LD_FS,"Couldn't write generated key to \"%s\".", fname);
- goto error;
- }
- return prkey;
- case FN_FILE:
- if (crypto_pk_read_private_key_from_filename(prkey, fname)) {
- log_err(LD_GENERAL,"Error loading private key.");
- goto error;
- }
- return prkey;
- default:
- tor_assert(0);
- }
- error:
- if (prkey)
- crypto_free_pk_env(prkey);
- if (file)
- fclose(file);
- return NULL;
- }
- int
- init_keys(void)
- {
- char keydir[512];
- char fingerprint[FINGERPRINT_LEN+1];
-
- char fingerprint_line[MAX_NICKNAME_LEN+FINGERPRINT_LEN+3];
- const char *mydesc, *datadir;
- crypto_pk_env_t *prkey;
- char digest[20];
- char *cp;
- or_options_t *options = get_options();
- or_state_t *state = get_or_state();
- authority_type_t type;
- if (!key_lock)
- key_lock = tor_mutex_new();
-
- if (!server_mode(options)) {
- if (!(prkey = crypto_new_pk_env()))
- return -1;
- if (crypto_pk_generate_key(prkey))
- return -1;
- set_identity_key(prkey);
-
- if (tor_tls_context_new(get_identity_key(),
- options->Nickname ? options->Nickname : "client",
- MAX_SSL_KEY_LIFETIME) < 0) {
- log_err(LD_GENERAL,"Error creating TLS context for Tor client.");
- return -1;
- }
- return 0;
- }
-
- datadir = options->DataDirectory;
- if (check_private_dir(datadir, CPD_CREATE)) {
- return -1;
- }
-
- tor_snprintf(keydir,sizeof(keydir),"%s"PATH_SEPARATOR"keys", datadir);
- if (check_private_dir(keydir, CPD_CREATE)) {
- return -1;
- }
-
- tor_snprintf(keydir,sizeof(keydir),
- "%s"PATH_SEPARATOR"keys"PATH_SEPARATOR"secret_id_key",datadir);
- log_info(LD_GENERAL,"Reading/making identity key \"%s\"...",keydir);
- prkey = init_key_from_file(keydir);
- if (!prkey) return -1;
- set_identity_key(prkey);
-
- tor_snprintf(keydir,sizeof(keydir),
- "%s"PATH_SEPARATOR"keys"PATH_SEPARATOR"secret_onion_key",datadir);
- log_info(LD_GENERAL,"Reading/making onion key \"%s\"...",keydir);
- prkey = init_key_from_file(keydir);
- if (!prkey) return -1;
- set_onion_key(prkey);
- if (state->LastRotatedOnionKey > 100) {
- onionkey_set_at = state->LastRotatedOnionKey;
- } else {
-
- state->LastRotatedOnionKey = onionkey_set_at = time(NULL);
- or_state_mark_dirty(state, options->AvoidDiskWrites ? time(NULL)+3600 : 0);
- }
- tor_snprintf(keydir,sizeof(keydir),
- "%s"PATH_SEPARATOR"keys"PATH_SEPARATOR"secret_onion_key.old",datadir);
- if (file_status(keydir) == FN_FILE) {
- prkey = init_key_from_file(keydir);
- if (prkey)
- lastonionkey = prkey;
- }
-
- if (tor_tls_context_new(get_identity_key(), options->Nickname,
- MAX_SSL_KEY_LIFETIME) < 0) {
- log_err(LD_GENERAL,"Error initializing TLS context");
- return -1;
- }
-
-
- mydesc = router_get_my_descriptor();
- if (authdir_mode(options)) {
- const char *m;
-
- if (dirserv_add_own_fingerprint(options->Nickname, get_identity_key())) {
- log_err(LD_GENERAL,"Error adding own fingerprint to approved set");
- return -1;
- }
- if (!mydesc) {
- log_err(LD_GENERAL,"Error initializing descriptor.");
- return -1;
- }
- if (dirserv_add_descriptor(router_get_my_routerinfo(), &m) < 0) {
- log_err(LD_GENERAL,"Unable to add own descriptor to directory: %s",
- m?m:"<unknown error>");
- return -1;
- }
- }
-
- tor_snprintf(keydir,sizeof(keydir),"%s"PATH_SEPARATOR"fingerprint", datadir);
- log_info(LD_GENERAL,"Dumping fingerprint to \"%s\"...",keydir);
- if (crypto_pk_get_fingerprint(get_identity_key(), fingerprint, 1)<0) {
- log_err(LD_GENERAL,"Error computing fingerprint");
- return -1;
- }
- tor_assert(strlen(options->Nickname) <= MAX_NICKNAME_LEN);
- if (tor_snprintf(fingerprint_line, sizeof(fingerprint_line),
- "%s %s\n",options->Nickname, fingerprint) < 0) {
- log_err(LD_GENERAL,"Error writing fingerprint line");
- return -1;
- }
-
- cp = NULL;
- if (file_status(keydir) == FN_FILE)
- cp = read_file_to_str(keydir, 0, NULL);
- if (!cp || strcmp(cp, fingerprint_line)) {
- if (write_str_to_file(keydir, fingerprint_line, 0)) {
- log_err(LD_FS, "Error writing fingerprint line to file");
- return -1;
- }
- }
- tor_free(cp);
- log(LOG_NOTICE, LD_GENERAL,
- "Your Tor server's identity key fingerprint is '%s %s'",
- options->Nickname, fingerprint);
- if (!authdir_mode(options))
- return 0;
-
- if (dirserv_load_fingerprint_file() < 0) {
- log_err(LD_GENERAL,"Error loading fingerprints");
- return -1;
- }
-
- crypto_pk_get_digest(get_identity_key(), digest);
- type = ((options->V1AuthoritativeDir ? V1_AUTHORITY : 0) |
- (options->V2AuthoritativeDir ? V2_AUTHORITY : 0) |
- (options->BridgeAuthoritativeDir ? BRIDGE_AUTHORITY : 0) |
- (options->HSAuthoritativeDir ? HIDSERV_AUTHORITY : 0));
- if (!router_digest_is_trusted_dir(digest)) {
- add_trusted_dir_server(options->Nickname, NULL,
- (uint16_t)options->DirPort,
- (uint16_t)options->ORPort,
- digest,
- type);
- }
- return 0;
- }
- static int can_reach_or_port = 0;
- static int can_reach_dir_port = 0;
- void
- router_reset_reachability(void)
- {
- can_reach_or_port = can_reach_dir_port = 0;
- }
- int
- check_whether_orport_reachable(void)
- {
- or_options_t *options = get_options();
- return options->AssumeReachable ||
- can_reach_or_port;
- }
- int
- check_whether_dirport_reachable(void)
- {
- or_options_t *options = get_options();
- return !options->DirPort ||
- options->AssumeReachable ||
- we_are_hibernating() ||
- can_reach_dir_port;
- }
- static int
- decide_to_advertise_dirport(or_options_t *options, routerinfo_t *router)
- {
- static int advertising=1;
- int new_choice=1;
- const char *reason = NULL;
-
- if (!router->dir_port)
- return 0;
- if (authdir_mode(options))
- return router->dir_port;
- if (we_are_hibernating())
- return 0;
- if (!check_whether_dirport_reachable())
- return 0;
-
- if (accounting_is_enabled(options)) {
-
- new_choice = 0;
- reason = "AccountingMax enabled";
- } else if (router->bandwidthrate < 51200) {
-
- new_choice = 0;
- reason = "BandwidthRate under 50KB";
- }
- if (advertising != new_choice) {
- if (new_choice == 1) {
- log(LOG_NOTICE, LD_DIR, "Advertising DirPort as %d", router->dir_port);
- } else {
- tor_assert(reason);
- log(LOG_NOTICE, LD_DIR, "Not advertising DirPort (Reason: %s)", reason);
- }
- advertising = new_choice;
- }
- return advertising ? router->dir_port : 0;
- }
- void
- consider_testing_reachability(int test_or, int test_dir)
- {
- routerinfo_t *me = router_get_my_routerinfo();
- int orport_reachable = check_whether_orport_reachable();
- if (!me)
- return;
- if (test_or && (!orport_reachable || !circuit_enough_testing_circs())) {
- log_info(LD_CIRC, "Testing %s of my ORPort: %s:%d.",
- !orport_reachable ? "reachability" : "bandwidth",
- me->address, me->or_port);
- circuit_launch_by_router(CIRCUIT_PURPOSE_TESTING, 0, me, 0, 1, 1);
- control_event_server_status(LOG_NOTICE,
- "CHECKING_REACHABILITY ORADDRESS=%s:%d",
- me->address, me->or_port);
- }
- if (test_dir && !check_whether_dirport_reachable() &&
- !connection_get_by_type_addr_port_purpose(
- CONN_TYPE_DIR, me->addr, me->dir_port,
- DIR_PURPOSE_FETCH_SERVERDESC)) {
-
- directory_initiate_command(me->address, me->addr,
- me->or_port, me->dir_port,
- 0, me->cache_info.identity_digest,
- DIR_PURPOSE_FETCH_SERVERDESC,
- 1, "authority", NULL, 0);
- control_event_server_status(LOG_NOTICE,
- "CHECKING_REACHABILITY DIRADDRESS=%s:%d",
- me->address, me->dir_port);
- }
- }
- void
- router_orport_found_reachable(void)
- {
- if (!can_reach_or_port) {
- routerinfo_t *me = router_get_my_routerinfo();
- log_notice(LD_OR,"Self-testing indicates your ORPort is reachable from "
- "the outside. Excellent.%s",
- get_options()->_PublishServerDescriptor != NO_AUTHORITY ?
- " Publishing server descriptor." : "");
- can_reach_or_port = 1;
- mark_my_descriptor_dirty();
- if (!me)
- return;
- control_event_server_status(LOG_NOTICE,
- "REACHABILITY_SUCCEEDED ORADDRESS=%s:%d",
- me->address, me->dir_port);
- }
- }
- void
- router_dirport_found_reachable(void)
- {
- if (!can_reach_dir_port) {
- routerinfo_t *me = router_get_my_routerinfo();
- log_notice(LD_DIRSERV,"Self-testing indicates your DirPort is reachable "
- "from the outside. Excellent.");
- can_reach_dir_port = 1;
- mark_my_descriptor_dirty();
- if (!me)
- return;
- control_event_server_status(LOG_NOTICE,
- "REACHABILITY_SUCCEEDED DIRADDRESS=%s:%d",
- me->address, me->dir_port);
- }
- }
- void
- router_perform_bandwidth_test(int num_circs, time_t now)
- {
- int num_cells = (int)(get_options()->BandwidthRate * 10 / CELL_NETWORK_SIZE);
- int max_cells = num_cells < CIRCWINDOW_START ?
- num_cells : CIRCWINDOW_START;
- int cells_per_circuit = max_cells / num_circs;
- origin_circuit_t *circ = NULL;
- log_notice(LD_OR,"Performing bandwidth self-test...done.");
- while ((circ = circuit_get_next_by_pk_and_purpose(circ, NULL,
- CIRCUIT_PURPOSE_TESTING))) {
-
- int i = cells_per_circuit;
- if (circ->_base.state != CIRCUIT_STATE_OPEN)
- continue;
- circ->_base.timestamp_dirty = now;
- while (i-- > 0) {
- if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
- RELAY_COMMAND_DROP,
- NULL, 0, circ->cpath->prev)<0) {
- return;
- }
- }
- }
- }
- int
- authdir_mode(or_options_t *options)
- {
- return options->AuthoritativeDir != 0;
- }
- int
- authdir_mode_v1(or_options_t *options)
- {
- return authdir_mode(options) && options->V1AuthoritativeDir != 0;
- }
- int
- authdir_mode_v2(or_options_t *options)
- {
- return authdir_mode(options) && options->V2AuthoritativeDir != 0;
- }
- int
- authdir_mode_handles_descs(or_options_t *options)
- {
- return authdir_mode_v1(options) || authdir_mode_v2(options);
- }
- int
- authdir_mode_bridge(or_options_t *options)
- {
- return authdir_mode(options) && options->BridgeAuthoritativeDir != 0;
- }
- int
- clique_mode(or_options_t *options)
- {
- return authdir_mode_handles_descs(options);
- }
- int
- server_mode(or_options_t *options)
- {
- if (options->ClientOnly) return 0;
- return (options->ORPort != 0 || options->ORListenAddress);
- }
- static int server_is_advertised=0;
- int
- advertised_server_mode(void)
- {
- return server_is_advertised;
- }
- static void
- set_server_advertised(int s)
- {
- server_is_advertised = s;
- }
- int
- proxy_mode(or_options_t *options)
- {
- return (options->SocksPort != 0 || options->SocksListenAddress);
- }
- static int
- decide_if_publishable_server(void)
- {
- or_options_t *options = get_options();
- if (options->ClientOnly)
- return 0;
- if (options->_PublishServerDescriptor == NO_AUTHORITY)
- return 0;
- if (!server_mode(options))
- return 0;
- if (authdir_mode(options))
- return 1;
- return check_whether_orport_reachable();
- }
- void
- consider_publishable_server(int force)
- {
- int rebuilt;
- if (!server_mode(get_options()))
- return;
- rebuilt = router_rebuild_descriptor(0);
- if (decide_if_publishable_server()) {
- set_server_advertised(1);
- if (rebuilt == 0)
- router_upload_dir_desc_to_dirservers(force);
- } else {
- set_server_advertised(0);
- }
- }
- int
- router_is_clique_mode(routerinfo_t *router)
- {
- if (router_digest_is_trusted_dir(router->cache_info.identity_digest))
- return 1;
- return 0;
- }
- static routerinfo_t *desc_routerinfo = NULL;
- static extrainfo_t *desc_extrainfo = NULL;
- static time_t desc_clean_since = 0;
- static int desc_needs_upload = 0;
- void
- router_upload_dir_desc_to_dirservers(int force)
- {
- routerinfo_t *ri;
- extrainfo_t *ei;
- char *msg;
- size_t desc_len, extra_len = 0, total_len;
- authority_type_t auth = get_options()->_PublishServerDescriptor;
- ri = router_get_my_routerinfo();
- if (!ri) {
- log_info(LD_GENERAL, "No descriptor; skipping upload");
- return;
- }
- ei = router_get_my_extrainfo();
- if (auth == NO_AUTHORITY)
- return;
- if (!force && !desc_needs_upload)
- return;
- desc_needs_upload = 0;
- desc_len = ri->cache_info.signed_descriptor_len;
- extra_len = ei ? ei->cache_info.signed_descriptor_len : 0;
- total_len = desc_len + extra_len + 1;
- msg = tor_malloc(total_len);
- memcpy(msg, ri->cache_info.signed_descriptor_body, desc_len);
- if (ei) {
- memcpy(msg+desc_len, ei->cache_info.signed_descriptor_body, extra_len);
- }
- msg[desc_len+extra_len] = 0;
- directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, auth,
- msg, desc_len, extra_len);
- tor_free(msg);
- }
- int
- router_compare_to_my_exit_policy(edge_connection_t *conn)
- {
- if (!router_get_my_routerinfo())
- return -1;
-
- if (!conn->_base.addr)
- return -1;
- return compare_addr_to_addr_policy(conn->_base.addr, conn->_base.port,
- desc_routerinfo->exit_policy) != ADDR_POLICY_ACCEPTED;
- }
- int
- router_digest_is_me(const char *digest)
- {
- return identitykey && !memcmp(identitykey_digest, digest, DIGEST_LEN);
- }
- int
- router_is_me(routerinfo_t *router)
- {
- return router_digest_is_me(router->cache_info.identity_digest);
- }
- int
- router_fingerprint_is_me(const char *fp)
- {
- char digest[DIGEST_LEN];
- if (strlen(fp) == HEX_DIGEST_LEN &&
- base16_decode(digest, sizeof(digest), fp, HEX_DIGEST_LEN) == 0)
- return router_digest_is_me(digest);
- return 0;
- }
- routerinfo_t *
- router_get_my_routerinfo(void)
- {
- if (!server_mode(get_options()))
- return NULL;
- if (router_rebuild_descriptor(0))
- return NULL;
- return desc_routerinfo;
- }
- const char *
- router_get_my_descriptor(void)
- {
- const char *body;
- if (!router_get_my_routerinfo())
- return NULL;
-
- tor_assert(desc_routerinfo->cache_info.saved_location == SAVED_NOWHERE);
- body = signed_descriptor_get_body(&desc_routerinfo->cache_info);
- tor_assert(!body[desc_routerinfo->cache_info.signed_descriptor_len]);
- log_debug(LD_GENERAL,"my desc is '%s'", body);
- return body;
- }
- extrainfo_t *
- router_get_my_extrainfo(void)
- {
- if (!server_mode(get_options()))
- return NULL;
- if (router_rebuild_descriptor(0))
- return NULL;
- return desc_extrainfo;
- }
- static smartlist_t *warned_nonexistent_family = NULL;
- static int router_guess_address_from_dir_headers(uint32_t *guess);
- int
- router_pick_published_address(or_options_t *options, uint32_t *addr)
- {
- if (resolve_my_address(LOG_INFO, options, addr, NULL) < 0) {
- log_info(LD_CONFIG, "Could not determine our address locally. "
- "Checking if directory headers provide any hints.");
- if (router_guess_address_from_dir_headers(addr) < 0) {
- log_info(LD_CONFIG, "No hints from directory headers either. "
- "Will try again later.");
- return -1;
- }
- }
- return 0;
- }
- int
- router_rebuild_descriptor(int force)
- {
- routerinfo_t *ri;
- extrainfo_t *ei;
- uint32_t addr;
- char platform[256];
- int hibernating = we_are_hibernating();
- or_options_t *options = get_options();
- if (desc_clean_since && !force)
- return 0;
- if (router_pick_published_address(options, &addr) < 0) {
-
- desc_clean_since = time(NULL);
- return -1;
- }
- ri = tor_malloc_zero(sizeof(routerinfo_t));
- ri->routerlist_index = -1;
- ri->address = tor_dup_addr(addr);
- ri->nickname = tor_strdup(options->Nickname);
- ri->addr = addr;
- ri->or_port = options->ORPort;
- ri->dir_port = options->DirPort;
- ri->cache_info.published_on = time(NULL);
- ri->onion_pkey = crypto_pk_dup_key(get_onion_key());
- ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
- if (crypto_pk_get_digest(ri->identity_pkey,
- ri->cache_info.identity_digest)<0) {
- routerinfo_free(ri);
- return -1;
- }
- get_platform_str(platform, sizeof(platform));
- ri->platform = tor_strdup(platform);
-
- ri->bandwidthrate = (int)options->BandwidthRate;
- if (ri->bandwidthrate > options->MaxAdvertisedBandwidth)
- ri->bandwidthrate = (int)options->MaxAdvertisedBandwidth;
- if (options->RelayBandwidthRate > 0 &&
- ri->bandwidthrate > options->RelayBandwidthRate)
- ri->bandwidthrate = (int)options->RelayBandwidthRate;
-
- ri->bandwidthburst = (int)options->BandwidthBurst;
- if (options->RelayBandwidthBurst > 0 &&
- ri->bandwidthburst > options->RelayBandwidthBurst)
- ri->bandwidthburst = (int)options->RelayBandwidthBurst;
- ri->bandwidthcapacity = hibernating ? 0 : rep_hist_bandwidth_assess();
- policies_parse_exit_policy(options->ExitPolicy, &ri->exit_policy,
- options->ExitPolicyRejectPrivate);
- if (desc_routerinfo) {
- ri->is_valid = desc_routerinfo->is_valid;
- ri->is_running = desc_routerinfo->is_running;
- ri->is_named = desc_routerinfo->is_named;
- }
- if (authdir_mode(options))
- ri->is_valid = ri->is_named = 1;
- if (options->MyFamily) {
- smartlist_t *family;
- if (!warned_nonexistent_family)
- warned_nonexistent_family = smartlist_create();
- family = smartlist_create();
- ri->declared_family = smartlist_create();
- smartlist_split_string(family, options->MyFamily, ",",
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
- SMARTLIST_FOREACH(family, char *, name,
- {
- routerinfo_t *member;
- if (!strcasecmp(name, options->Nickname))
- member = ri;
- else
- member = router_get_by_nickname(name, 1);
- if (!member) {
- if (!smartlist_string_isin(warned_nonexistent_family, name) &&
- !is_legal_hexdigest(name)) {
- log_warn(LD_CONFIG,
- "I have no descriptor for the router named \"%s\" "
- "in my declared family; I'll use the nickname as is, but "
- "this may confuse clients.", name);
- smartlist_add(warned_nonexistent_family, tor_strdup(name));
- }
- smartlist_add(ri->declared_family, name);
- name = NULL;
- } else if (router_is_me(member)) {
-
- } else {
- char *fp = tor_malloc(HEX_DIGEST_LEN+2);
- fp[0] = '$';
- base16_encode(fp+1,HEX_DIGEST_LEN+1,
- member->cache_info.identity_digest, DIGEST_LEN);
- smartlist_add(ri->declared_family, fp);
- if (smartlist_string_isin(warned_nonexistent_family, name))
- smartlist_string_remove(warned_nonexistent_family, name);
- }
- tor_free(name);
- });
-
- smartlist_sort_strings(ri->declared_family);
- smartlist_uniq_strings(ri->declared_family);
- smartlist_free(family);
- }
-
- ei = tor_malloc_zero(sizeof(extrainfo_t));
- strlcpy(ei->nickname, get_options()->Nickname, sizeof(ei->nickname));
- ei->cache_info.published_on = ri->cache_info.published_on;
- memcpy(ei->cache_info.identity_digest, ri->cache_info.identity_digest,
- DIGEST_LEN);
- ei->cache_info.signed_descriptor_body = tor_malloc(8192);
- if (extrainfo_dump_to_string(ei->cache_info.signed_descriptor_body, 8192,
- ei, get_identity_key()) < 0) {
- log_warn(LD_BUG, "Couldn't generate extra-info descriptor.");
- return -1;
- }
- ei->cache_info.signed_descriptor_len =
- strlen(ei->cache_info.signed_descriptor_body);
- router_get_extrainfo_hash(ei->cache_info.signed_descriptor_body,
- ei->cache_info.signed_descriptor_digest);
-
- memcpy(ri->cache_info.extra_info_digest,
- ei->cache_info.signed_descriptor_digest,
- DIGEST_LEN);
- ri->cache_info.signed_descriptor_body = tor_malloc(8192);
- if (router_dump_router_to_string(ri->cache_info.signed_descriptor_body, 8192,
- ri, get_identity_key())<0) {
- log_warn(LD_BUG, "Couldn't generate router descriptor.");
- return -1;
- }
- ri->cache_info.signed_descriptor_len =
- strlen(ri->cache_info.signed_descriptor_body);
-
- crypto_digest(ri->cache_info.signed_descriptor_digest,
- ri->cache_info.signed_descriptor_body,
- ri->cache_info.signed_descriptor_len);
- tor_assert(! routerinfo_incompatible_with_extrainfo(ri, ei));
- if (desc_routerinfo)
- routerinfo_free(desc_routerinfo);
- desc_routerinfo = ri;
- if (desc_extrainfo)
- extrainfo_free(desc_extrainfo);
- desc_extrainfo = ei;
- desc_clean_since = time(NULL);
- desc_needs_upload = 1;
- control_event_my_descriptor_changed();
- return 0;
- }
- void
- mark_my_descriptor_dirty_if_older_than(time_t when)
- {
- if (desc_clean_since < when)
- mark_my_descriptor_dirty();
- }
- void
- mark_my_descriptor_dirty(void)
- {
- desc_clean_since = 0;
- }
- #define MAX_BANDWIDTH_CHANGE_FREQ (20*60)
- void
- check_descriptor_bandwidth_changed(time_t now)
- {
- static time_t last_changed = 0;
- uint64_t prev, cur;
- if (!desc_routerinfo)
- return;
- prev = desc_routerinfo->bandwidthcapacity;
- cur = we_are_hibernating() ? 0 : rep_hist_bandwidth_assess();
- if ((prev != cur && (!prev || !cur)) ||
- cur > prev*2 ||
- cur < prev/2) {
- if (last_changed+MAX_BANDWIDTH_CHANGE_FREQ < now) {
- log_info(LD_GENERAL,
- "Measured bandwidth has changed; rebuilding descriptor.");
- mark_my_descriptor_dirty();
- last_changed = now;
- }
- }
- }
- static void
- log_addr_has_changed(int severity, uint32_t prev, uint32_t cur)
- {
- char addrbuf_prev[INET_NTOA_BUF_LEN];
- char addrbuf_cur[INET_NTOA_BUF_LEN];
- struct in_addr in_prev;
- struct in_addr in_cur;
- in_prev.s_addr = htonl(prev);
- tor_inet_ntoa(&in_prev, addrbuf_prev, sizeof(addrbuf_prev));
- in_cur.s_addr = htonl(cur);
- tor_inet_ntoa(&in_cur, addrbuf_cur, sizeof(addrbuf_cur));
- if (prev)
- log_fn(severity, LD_GENERAL,
- "Our IP Address has changed from %s to %s; "
- "rebuilding descriptor.",
- addrbuf_prev, addrbuf_cur);
- else
- log_notice(LD_GENERAL,
- "Guessed our IP address as %s.",
- addrbuf_cur);
- }
- void
- check_descriptor_ipaddress_changed(time_t now)
- {
- uint32_t prev, cur;
- or_options_t *options = get_options();
- (void) now;
- if (!desc_routerinfo)
- return;
- prev = desc_routerinfo->addr;
- if (resolve_my_address(LOG_INFO, options, &cur, NULL) < 0) {
- log_info(LD_CONFIG,"options->Address didn't resolve into an IP.");
- return;
- }
- if (prev != cur) {
- log_addr_has_changed(LOG_INFO, prev, cur);
- ip_address_changed(0);
- }
- }
- static uint32_t last_guessed_ip = 0;
- void
- router_new_address_suggestion(const char *suggestion)
- {
- uint32_t addr, cur = 0;
- struct in_addr in;
- or_options_t *options = get_options();
-
- if (!tor_inet_aton(suggestion, &in)) {
- log_debug(LD_DIR, "Malformed X-Your-Address-Is header %s. Ignoring.",
- escaped(suggestion));
- return;
- }
- addr = ntohl(in.s_addr);
- log_debug(LD_DIR, "Got X-Your-Address-Is: %s.", suggestion);
- if (!server_mode(options)) {
- last_guessed_ip = addr;
- return;
- }
- if (resolve_my_address(LOG_INFO, options, &cur, NULL) >= 0) {
-
- last_guessed_ip = cur;
- return;
- }
- if (is_internal_IP(addr, 0)) {
-
- return;
- }
-
- if (last_guessed_ip != addr) {
- control_event_server_status(LOG_NOTICE,
- "EXTERNAL_ADDRESS ADDRESS=%s METHOD=DIRSERV",
- suggestion);
- log_addr_has_changed(LOG_NOTICE, last_guessed_ip, addr);
- ip_address_changed(0);
- last_guessed_ip = addr;
- }
- }
- static int
- router_guess_address_from_dir_headers(uint32_t *guess)
- {
- if (last_guessed_ip) {
- *guess = last_guessed_ip;
- return 0;
- }
- return -1;
- }
- extern const char tor_svn_revision[];
- void
- get_platform_str(char *platform, size_t len)
- {
- char svn_version_buf[128];
- if (!strcmpend(VERSION, "-dev") &&
- tor_svn_revision != NULL && strlen(tor_svn_revision)) {
- tor_snprintf(svn_version_buf, sizeof(svn_version_buf), " (r%s)",
- tor_svn_revision);
- } else {
- svn_version_buf[0] = 0;
- }
- tor_snprintf(platform, len, "Tor %s%s on %s",
- VERSION, svn_version_buf, get_uname());
- return;
- }
- #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
- int
- router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
- crypto_pk_env_t *ident_key)
- {
- char *onion_pkey;
- char *identity_pkey;
- char digest[DIGEST_LEN];
- char published[ISO_TIME_LEN+1];
- char fingerprint[FINGERPRINT_LEN+1];
- char extra_info_digest[HEX_DIGEST_LEN+1];
- size_t onion_pkeylen, identity_pkeylen;
- size_t written;
- int result=0;
- addr_policy_t *tmpe;
- char *bandwidth_usage;
- char *family_line;
- or_options_t *options = get_options();
-
- if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
- log_warn(LD_BUG,"Tried to sign a router with a private key that didn't "
- "match router's public key!");
- return -1;
- }
-
- if (crypto_pk_get_fingerprint(router->identity_pkey, fingerprint, 1)<0) {
- log_err(LD_BUG,"Error computing fingerprint");
- return -1;
- }
-
- if (crypto_pk_write_public_key_to_string(router->onion_pkey,
- &onion_pkey,&onion_pkeylen)<0) {
- log_warn(LD_BUG,"write onion_pkey to string failed!");
- return -1;
- }
-
- if (crypto_pk_write_public_key_to_string(router->identity_pkey,
- &identity_pkey,&identity_pkeylen)<0) {
- log_warn(LD_BUG,"write identity_pkey to string failed!");
- tor_free(onion_pkey);
- return -1;
- }
-
- format_iso_time(published, router->cache_info.published_on);
-
- bandwidth_usage = rep_hist_get_bandwidth_lines(0);
- if (router->declared_family && smartlist_len(router->declared_family)) {
- size_t n;
- char *family = smartlist_join_strings(router->declared_family, " ", 0, &n);
- n += strlen("family ") + 2;
- family_line = tor_malloc(n);
- tor_snprintf(family_line, n, "family %s\n", family);
- tor_free(family);
- } else {
- family_line = tor_strdup("");
- }
- base16_encode(extra_info_digest, sizeof(extra_info_digest),
- router->cache_info.extra_info_digest, DIGEST_LEN);
-
- result = tor_snprintf(s, maxlen,
- "router %s %s %d 0 %d\n"
- "platform %s\n"
- "published %s\n"
- "opt fingerprint %s\n"
- "uptime %ld\n"
- "bandwidth %d %d %d\n"
- "opt extra-info-digest %s\n%s"
- "onion-key\n%s"
- "signing-key\n%s"
- "%s%s%s",
- router->nickname,
- router->address,
- router->or_port,
- decide_to_advertise_dirport(options, router),
- router->platform,
- published,
- fingerprint,
- stats_n_seconds_working,
- (int) router->bandwidthrate,
- (int) router->bandwidthburst,
- (int) router->bandwidthcapacity,
- extra_info_digest,
- options->DownloadExtraInfo ? "opt caches-extra-info\n" : "",
- onion_pkey, identity_pkey,
- family_line, bandwidth_usage,
- we_are_hibernating() ? "opt hibernating 1\n" : "");
- tor_free(family_line);
- tor_free(onion_pkey);
- tor_free(identity_pkey);
- tor_free(bandwidth_usage);
- if (result < 0) {
- log_warn(LD_BUG,"descriptor snprintf #1 ran out of room!");
- return -1;
- }
-
- written = result;
- if (options->ContactInfo && strlen(options->ContactInfo)) {
- result = tor_snprintf(s+written,maxlen-written, "contact %s\n",
- options->ContactInfo);
- if (result<0) {
- log_warn(LD_BUG,"descriptor snprintf #2 ran out of room!");
- return -1;
- }
- written += result;
- }
-
- tmpe = router->exit_policy;
- if (dns_seems_to_be_broken()) {
-
- strlcat(s+written, "reject *:*\n", maxlen-written);
- written += strlen("reject *:*\n");
- tmpe = NULL;
- }
- for ( ; tmpe; tmpe=tmpe->next) {
- result = policy_write_item(s+written, maxlen-written, tmpe);
- if (result < 0) {
- log_warn(LD_BUG,"descriptor policy_write_item ran out of room!");
- return -1;
- }
- tor_assert(result == (int)strlen(s+written));
- written += result;
- if (written+2 > maxlen) {
- log_warn(LD_BUG,"descriptor policy_write_item ran out of room (2)!");
- return -1;
- }
- s[written++] = '\n';
- }
- if (written+256 > maxlen) {
- log_warn(LD_BUG,"not enough room left in descriptor for signature!");
- return -1;
- }
-
- strlcpy(s+written, "router-signature\n", maxlen-written);
- written += strlen(s+written);
- s[written] = '\0';
- if (router_get_router_hash(s, digest) < 0) {
- return -1;
- }
- note_crypto_pk_op(SIGN_RTR);
- if (router_append_dirobj_signature(s+written,maxlen-written,
- digest,ident_key)<0) {
- log_warn(LD_BUG, "Couldn't sign router descriptor");
- return -1;
- }
- written += strlen(s+written);
- if (written+2 > maxlen) {
- log_warn(LD_BUG,"Not enough room to finish descriptor.");
- return -1;
- }
-
- s[written] = '\n';
- s[written+1] = 0;
- #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
- {
- char *s_dup;
- const char *cp;
- routerinfo_t *ri_tmp;
- cp = s_dup = tor_strdup(s);
- ri_tmp = router_parse_entry_from_string(cp, NULL, 1);
- if (!ri_tmp) {
- log_err(LD_BUG,
- "We just generated a router descriptor we can't parse.");
- log_err(LD_BUG, "Descriptor was: <<%s>>", s);
- return -1;
- }
- tor_free(s_dup);
- routerinfo_free(ri_tmp);
- }
- #endif
- return written+1;
- }
- int
- extrainfo_dump_to_string(char *s, size_t maxlen, extrainfo_t *extrainfo,
- crypto_pk_env_t *ident_key)
- {
- char identity[HEX_DIGEST_LEN+1];
- char published[ISO_TIME_LEN+1];
- char digest[DIGEST_LEN];
- char *bandwidth_usage;
- int result;
- size_t len;
- base16_encode(identity, sizeof(identity),
- extrainfo->cache_info.identity_digest, DIGEST_LEN);
- format_iso_time(published, extrainfo->cache_info.published_on);
- bandwidth_usage = rep_hist_get_bandwidth_lines(1);
- result = tor_snprintf(s, maxlen,
- "extra-info %s %s\n"
- "published %s\n%s"
- "router-signature\n",
- extrainfo->nickname, identity,
- published, bandwidth_usage);
- tor_free(bandwidth_usage);
- if (result<0)
- return -1;
- len = strlen(s);
- if (router_get_extrainfo_hash(s, digest)<0)
- return -1;
- if (router_append_dirobj_signature(s+len, maxlen-len, digest, ident_key)<0)
- return -1;
- #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
- {
- char *cp, *s_dup;
- extrainfo_t *ei_tmp;
- cp = s_dup = tor_strdup(s);
- ei_tmp = extrainfo_parse_entry_from_string(cp, NULL, 1, NULL);
- if (!ei_tmp) {
- log_err(LD_BUG,
- "We just generated an extrainfo descriptor we can't parse.");
- log_err(LD_BUG, "Descriptor was: <<%s>>", s);
- return -1;
- }
- tor_free(s_dup);
- extrainfo_free(ei_tmp);
- }
- #endif
- return strlen(s)+1;
- }
- int
- is_legal_nickname(const char *s)
- {
- size_t len;
- tor_assert(s);
- len = strlen(s);
- return len > 0 && len <= MAX_NICKNAME_LEN &&
- strspn(s,LEGAL_NICKNAME_CHARACTERS) == len;
- }
- int
- is_legal_nickname_or_hexdigest(const char *s)
- {
- if (*s!='$')
- return is_legal_nickname(s);
- else
- return is_legal_hexdigest(s);
- }
- int
- is_legal_hexdigest(const char *s)
- {
- size_t len;
- tor_assert(s);
- if (s[0] == '$') s++;
- len = strlen(s);
- if (len > HEX_DIGEST_LEN) {
- if (s[HEX_DIGEST_LEN] == '=' ||
- s[HEX_DIGEST_LEN] == '~') {
- if (!is_legal_nickname(s+HEX_DIGEST_LEN+1))
- return 0;
- } else {
- return 0;
- }
- }
- return (len >= HEX_DIGEST_LEN &&
- strspn(s,HEX_CHARACTERS)==HEX_DIGEST_LEN);
- }
- void
- router_get_verbose_nickname(char *buf, routerinfo_t *router)
- {
- buf[0] = '$';
- base16_encode(buf+1, HEX_DIGEST_LEN+1, router->cache_info.identity_digest,
- DIGEST_LEN);
- buf[1+HEX_DIGEST_LEN] = router->is_named ? '=' : '~';
- strlcpy(buf+1+HEX_DIGEST_LEN+1, router->nickname, MAX_NICKNAME_LEN+1);
- }
- void
- router_reset_warnings(void)
- {
- if (warned_nonexistent_family) {
- SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
- smartlist_clear(warned_nonexistent_family);
- }
- }
- void
- router_free_all(void)
- {
- if (onionkey)
- crypto_free_pk_env(onionkey);
- if (lastonionkey)
- crypto_free_pk_env(lastonionkey);
- if (identitykey)
- crypto_free_pk_env(identitykey);
- if (key_lock)
- tor_mutex_free(key_lock);
- if (desc_routerinfo)
- routerinfo_free(desc_routerinfo);
- if (desc_extrainfo)
- extrainfo_free(desc_extrainfo);
- if (warned_nonexistent_family) {
- SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
- smartlist_free(warned_nonexistent_family);
- }
- }
|