1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531 |
- 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();
- 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(mydesc, &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);
- if (!router_digest_is_trusted_dir(digest)) {
- add_trusted_dir_server(options->Nickname, NULL,
- (uint16_t)options->DirPort,
- (uint16_t)options->ORPort,
- digest,
- options->V1AuthoritativeDir,
- 1,
- options->HSAuthoritativeDir );
- }
- 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->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 ?
- " 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
- clique_mode(or_options_t *options)
- {
- return authdir_mode(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)
- return 0;
- if (!server_mode(options))
- return 0;
- if (options->AuthoritativeDir)
- 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)
- {
- const char *s;
- s = router_get_my_descriptor();
- if (!s) {
- log_info(LD_GENERAL, "No descriptor; skipping upload");
- return;
- }
- if (!get_options()->PublishServerDescriptor)
- return;
- if (!force && !desc_needs_upload)
- return;
- desc_needs_upload = 0;
- directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, s, strlen(s));
- }
- 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;
- ri->bandwidthburst = (int)options->BandwidthBurst;
- ri->bandwidthcapacity = hibernating ? 0 : rep_hist_bandwidth_assess();
- if (options->BandwidthRate > options->MaxAdvertisedBandwidth) {
- if (options->MaxAdvertisedBandwidth > ROUTER_MAX_DECLARED_BANDWIDTH) {
- ri->bandwidthrate = ROUTER_MAX_DECLARED_BANDWIDTH;
- } else {
- ri->bandwidthrate = (int)options->MaxAdvertisedBandwidth;
- }
- }
- 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->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;
- }
- void
- get_platform_str(char *platform, size_t len)
- {
- tor_snprintf(platform, len, "Tor %s on %s",
- VERSION, 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 *s = 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", s);
- tor_free(s);
- } else {
- family_line = tor_strdup("");
- }
- base16_encode(extra_info_digest, sizeof(extra_info_digest),
- router->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"
- "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,
- 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);
- }
- }
|