12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835 |
- 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 crypto_pk_env_t *authority_signing_key = NULL;
- static authority_cert_t *authority_key_certificate = NULL;
- 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;
- }
- authority_cert_t *
- get_my_v3_authority_cert(void)
- {
- return authority_key_certificate;
- }
- crypto_pk_env_t *
- get_my_v3_authority_signing_key(void)
- {
- return authority_signing_key;
- }
- 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, int generate, int severity)
- {
- crypto_pk_env_t *prkey = NULL;
- FILE *file = NULL;
- if (!(prkey = crypto_new_pk_env())) {
- log(severity, LD_GENERAL,"Error constructing key");
- goto error;
- }
- switch (file_status(fname)) {
- case FN_DIR:
- case FN_ERROR:
- log(severity, LD_FS,"Can't read key from \"%s\"", fname);
- goto error;
- case FN_NOENT:
- if (generate) {
- log_info(LD_GENERAL, "No key found in \"%s\"; generating fresh key.",
- fname);
- if (crypto_pk_generate_key(prkey)) {
- log(severity, LD_GENERAL,"Error generating onion key");
- goto error;
- }
- if (crypto_pk_check_key(prkey) <= 0) {
- log(severity, 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(severity, LD_FS,
- "Couldn't write generated key to \"%s\".", fname);
- goto error;
- }
- } else {
- log_info(LD_GENERAL, "No key found in \"%s\"", fname);
- }
- return prkey;
- case FN_FILE:
- if (crypto_pk_read_private_key_from_filename(prkey, fname)) {
- log(severity, 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;
- }
- static void
- init_v3_authority_keys(const char *keydir)
- {
- char *fname = NULL, *cert = NULL;
- const char *eos = NULL;
- size_t fname_len = strlen(keydir) + 64;
- crypto_pk_env_t *signing_key = NULL;
- authority_cert_t *parsed = NULL;
- fname = tor_malloc(fname_len);
- tor_snprintf(fname, fname_len, "%s"PATH_SEPARATOR"authority_signing_key",
- keydir);
- signing_key = init_key_from_file(fname, 0, LOG_INFO);
- if (!signing_key) {
- log_warn(LD_DIR, "No version 3 directory key found in %s", fname);
- goto done;
- }
- tor_snprintf(fname, fname_len, "%s"PATH_SEPARATOR"authority_certificate",
- keydir);
- cert = read_file_to_str(fname, 0, NULL);
- if (!cert) {
- log_warn(LD_DIR, "Signing key found, but no certificate found in %s",
- fname);
- goto done;
- }
- parsed = authority_cert_parse_from_string(cert, &eos);
- if (!parsed) {
- log_warn(LD_DIR, "Unable to parse certificate in %s", fname);
- goto done;
- }
- if (crypto_pk_cmp_keys(signing_key, parsed->signing_key) != 0) {
- log_warn(LD_DIR, "Stored signing key does not match signing key in "
- "certificate");
- goto done;
- }
- parsed->cache_info.signed_descriptor_body = cert;
- parsed->cache_info.signed_descriptor_len = eos-cert;
- cert = NULL;
-
- if (authority_key_certificate)
- authority_cert_free(authority_key_certificate);
- if (authority_signing_key)
- crypto_free_pk_env(authority_signing_key);
-
- authority_key_certificate = parsed;
- authority_signing_key = signing_key;
- parsed = NULL;
- signing_key = NULL;
- done:
- tor_free(fname);
- tor_free(cert);
- if (signing_key)
- crypto_free_pk_env(signing_key);
- if (parsed)
- authority_cert_free(parsed);
- }
- void
- v3_authority_check_key_expiry(void)
- {
- time_t now, expires;
- static time_t last_warned = 0;
- int badness, time_left, warn_interval;
- if (!authdir_mode_v3(get_options()) || !authority_key_certificate)
- return;
- now = time(NULL);
- expires = authority_key_certificate->expires;
- time_left = expires - now;
- if (time_left <= 0) {
- badness = LOG_ERR;
- warn_interval = 60*60;
- } else if (time_left <= 24*60*60) {
- badness = LOG_WARN;
- warn_interval = 60*60;
- } else if (time_left <= 24*60*60*7) {
- badness = LOG_WARN;
- warn_interval = 24*60*60;
- } else if (time_left <= 24*60*60*30) {
- badness = LOG_WARN;
- warn_interval = 24*60*60*5;
- } else {
- return;
- }
- if (last_warned + warn_interval > now)
- return;
- if (time_left <= 0) {
- log(badness, LD_DIR, "Your v3 authority certificate has expired."
- " Generate a new one NOW.");
- } else if (time_left <= 24*60*60) {
- log(badness, LD_DIR, "Your v3 authority certificate expires in %d hours;"
- " Generate a new one NOW.", time_left/(60*60));
- } else {
- log(badness, LD_DIR, "Your v3 authority certificate expires in %d days;"
- " Generate a new one soon.", time_left/(24*60*60));
- }
- last_warned = now;
- }
- 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 v3_digest[20];
- char *cp;
- or_options_t *options = get_options();
- authority_type_t type;
- time_t now = time(NULL);
- 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;
- }
-
- memset(v3_digest, 0, sizeof(v3_digest));
- if (authdir_mode_v3(options)) {
- init_v3_authority_keys(keydir);
- if (get_my_v3_authority_cert()) {
- crypto_pk_get_digest(get_my_v3_authority_cert()->identity_key,
- v3_digest);
- }
- }
-
- 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, 1, LOG_ERR);
- 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, 1, LOG_ERR);
- if (!prkey) return -1;
- set_onion_key(prkey);
- if (options->command == CMD_RUN_TOR) {
-
- or_state_t *state = get_or_state();
- if (state->LastRotatedOnionKey > 100 && state->LastRotatedOnionKey < now) {
-
- onionkey_set_at = state->LastRotatedOnionKey;
- } else {
-
- state->LastRotatedOnionKey = onionkey_set_at = now;
- 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, 1, LOG_ERR);
- 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;
- routerinfo_t *ri;
-
- 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) {
- ri = router_parse_entry_from_string(mydesc, NULL, 1, 0, NULL);
- if (!ri) {
- log_err(LD_GENERAL,"Generated a routerinfo we couldn't parse.");
- return -1;
- }
- if (dirserv_add_descriptor(ri, &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->V3AuthoritativeDir ? V3_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,
- v3_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";
- #define MIN_BW_TO_ADVERTISE_DIRPORT 51200
- } else if (options->BandwidthRate < MIN_BW_TO_ADVERTISE_DIRPORT ||
- (options->RelayBandwidthRate > 0 &&
- options->RelayBandwidthRate < MIN_BW_TO_ADVERTISE_DIRPORT)) {
-
- 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,
- ROUTER_PURPOSE_GENERAL,
- 1, "authority.z", 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->or_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_v3(or_options_t *options)
- {
- return authdir_mode(options) && options->V3AuthoritativeDir != 0;
- }
- int
- authdir_mode_any_nonbridge(or_options_t *options)
- {
- return authdir_mode(options) &&
- (options->V1AuthoritativeDir ||
- options->V2AuthoritativeDir ||
- options->V3AuthoritativeDir);
- }
- int
- authdir_mode_handles_descs(or_options_t *options)
- {
- return authdir_mode_any_nonbridge(options) ||
- authdir_mode_bridge(options);
- }
- int
- authdir_mode_publishes_statuses(or_options_t *options)
- {
- if (authdir_mode_bridge(options))
- return 0;
- return authdir_mode_any_nonbridge(options);
- }
- int
- authdir_mode_tests_reachability(or_options_t *options)
- {
- return authdir_mode_any_nonbridge(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_tests_reachability(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 & BRIDGE_AUTHORITY) ?
- ROUTER_PURPOSE_BRIDGE :
- ROUTER_PURPOSE_GENERAL,
- 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));
- ei->cache_info.is_extrainfo = 1;
- 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);
- router_get_router_hash(ri->cache_info.signed_descriptor_body,
- ri->cache_info.signed_descriptor_digest);
- tor_assert(! routerinfo_incompatible_with_extrainfo(ri, ei, NULL));
- 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)
- {
- tor_snprintf(platform, len, "Tor %s on %s", get_version(), get_uname());
- }
- #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 *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);
- 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",
- 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,
- we_are_hibernating() ? "opt hibernating 1\n" : "");
- tor_free(family_line);
- tor_free(onion_pkey);
- tor_free(identity_pkey);
- 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, 0, NULL);
- 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);
- }
- }
- const char *
- router_purpose_to_string(uint8_t p)
- {
- switch (p)
- {
- case ROUTER_PURPOSE_GENERAL: return "general";
- case ROUTER_PURPOSE_BRIDGE: return "bridge";
- case ROUTER_PURPOSE_CONTROLLER: return "controller";
- default:
- tor_assert(0);
- }
- return NULL;
- }
- uint8_t
- router_purpose_from_string(const char *s)
- {
- if (!strcmp(s, "general"))
- return ROUTER_PURPOSE_GENERAL;
- else if (!strcmp(s, "bridge"))
- return ROUTER_PURPOSE_BRIDGE;
- else if (!strcmp(s, "controller"))
- return ROUTER_PURPOSE_CONTROLLER;
- else
- return ROUTER_PURPOSE_UNKNOWN;
- }
- 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 (authority_signing_key)
- crypto_free_pk_env(authority_signing_key);
- if (authority_key_certificate)
- authority_cert_free(authority_key_certificate);
- if (warned_nonexistent_family) {
- SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
- smartlist_free(warned_nonexistent_family);
- }
- }
|