12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868 |
- const char routerparse_c_id[] =
- "$Id$";
- #include "or.h"
- typedef enum {
- K_ACCEPT,
- K_DIRECTORY_SIGNATURE,
- K_RECOMMENDED_SOFTWARE,
- K_REJECT,
- K_ROUTER,
- K_SIGNED_DIRECTORY,
- K_SIGNING_KEY,
- K_ONION_KEY,
- K_ROUTER_SIGNATURE,
- K_PUBLISHED,
- K_RUNNING_ROUTERS,
- K_ROUTER_STATUS,
- K_PLATFORM,
- K_OPT,
- K_BANDWIDTH,
- K_PORTS,
- K_CONTACT,
- K_NETWORK_STATUS,
- K_UPTIME,
- K_DIR_SIGNING_KEY,
- K_FAMILY,
- K_FINGERPRINT,
- K_HIBERNATING,
- K_READ_HISTORY,
- K_WRITE_HISTORY,
- K_NETWORK_STATUS_VERSION,
- K_DIR_SOURCE,
- K_DIR_OPTIONS,
- K_CLIENT_VERSIONS,
- K_SERVER_VERSIONS,
- K_R,
- K_S,
- _UNRECOGNIZED,
- _ERR,
- _EOF,
- _NIL
- } directory_keyword;
- typedef struct directory_token_t {
- directory_keyword tp;
- int n_args;
- char **args;
- char *object_type;
- size_t object_size;
- char *object_body;
- crypto_pk_env_t *key;
- const char *error;
- } directory_token_t;
- typedef enum {
- NO_ARGS,
- ARGS,
- CONCAT_ARGS,
- } arg_syntax;
- typedef enum {
- NO_OBJ,
- NEED_OBJ,
- NEED_KEY,
- OBJ_OK,
- } obj_syntax;
- typedef enum {
- DIR = 1,
- RTR = 2,
- NETSTATUS = 4,
- ANYSIGNED = 7,
- RTRSTATUS = 8,
- ANY = 15,
- } where_syntax;
- static struct {
- const char *t; int v; arg_syntax s; obj_syntax os; int ws;
- } token_table[] = {
- { "accept", K_ACCEPT, ARGS, NO_OBJ, RTR },
- { "directory-signature", K_DIRECTORY_SIGNATURE, ARGS, NEED_OBJ,
- DIR|NETSTATUS},
- { "r", K_R, ARGS, NO_OBJ, RTRSTATUS },
- { "s", K_S, ARGS, NO_OBJ, RTRSTATUS },
- { "reject", K_REJECT, ARGS, NO_OBJ, RTR },
- { "router", K_ROUTER, ARGS, NO_OBJ, RTR },
- { "recommended-software",K_RECOMMENDED_SOFTWARE,ARGS, NO_OBJ, DIR },
- { "signed-directory", K_SIGNED_DIRECTORY, NO_ARGS, NO_OBJ, DIR },
- { "signing-key", K_SIGNING_KEY, NO_ARGS, NEED_KEY,RTR },
- { "onion-key", K_ONION_KEY, NO_ARGS, NEED_KEY,RTR },
- { "router-signature", K_ROUTER_SIGNATURE, NO_ARGS, NEED_OBJ,RTR },
- { "running-routers", K_RUNNING_ROUTERS, ARGS, NO_OBJ, DIR },
- { "router-status", K_ROUTER_STATUS, ARGS, NO_OBJ, DIR },
- { "ports", K_PORTS, ARGS, NO_OBJ, RTR },
- { "bandwidth", K_BANDWIDTH, ARGS, NO_OBJ, RTR },
- { "platform", K_PLATFORM, CONCAT_ARGS, NO_OBJ, RTR },
- { "published", K_PUBLISHED, CONCAT_ARGS, NO_OBJ, ANYSIGNED },
- { "opt", K_OPT, CONCAT_ARGS, OBJ_OK, ANY },
- { "contact", K_CONTACT, CONCAT_ARGS, NO_OBJ, ANYSIGNED },
- { "network-status", K_NETWORK_STATUS, NO_ARGS, NO_OBJ, DIR },
- { "uptime", K_UPTIME, ARGS, NO_OBJ, RTR },
- { "dir-signing-key", K_DIR_SIGNING_KEY, ARGS, OBJ_OK,
- DIR|NETSTATUS},
- { "family", K_FAMILY, ARGS, NO_OBJ, RTR },
- { "fingerprint", K_FINGERPRINT, CONCAT_ARGS, NO_OBJ, ANYSIGNED },
- { "hibernating", K_HIBERNATING, ARGS, NO_OBJ, RTR },
- { "read-history", K_READ_HISTORY, ARGS, NO_OBJ, RTR },
- { "write-history", K_WRITE_HISTORY, ARGS, NO_OBJ, RTR },
- { "network-status-version", K_NETWORK_STATUS_VERSION,
- ARGS, NO_OBJ, NETSTATUS },
- { "dir-source", K_DIR_SOURCE, ARGS, NO_OBJ, NETSTATUS },
- { "dir-options", K_DIR_OPTIONS, ARGS, NO_OBJ, NETSTATUS },
- { "client-versions", K_CLIENT_VERSIONS, ARGS, NO_OBJ, NETSTATUS },
- { "server-versions", K_SERVER_VERSIONS, ARGS, NO_OBJ, NETSTATUS },
- { NULL, -1, NO_ARGS, NO_OBJ, ANY }
- };
- static int router_add_exit_policy(routerinfo_t *router,directory_token_t *tok);
- static addr_policy_t *router_parse_addr_policy(directory_token_t *tok);
- static int router_get_hash_impl(const char *s, char *digest,
- const char *start_str, const char *end_str);
- static void token_free(directory_token_t *tok);
- static smartlist_t *find_all_exitpolicy(smartlist_t *s);
- static directory_token_t *find_first_by_keyword(smartlist_t *s,
- directory_keyword keyword);
- static int tokenize_string(const char *start, const char *end,
- smartlist_t *out, where_syntax where);
- static directory_token_t *get_next_token(const char **s, where_syntax where);
- static int check_directory_signature(const char *digest,
- directory_token_t *tok,
- crypto_pk_env_t *pkey,
- crypto_pk_env_t *declared_key,
- int check_authority);
- static crypto_pk_env_t *find_dir_signing_key(const char *str);
- static int tor_version_same_series(tor_version_t *a, tor_version_t *b);
- int
- router_get_dir_hash(const char *s, char *digest)
- {
- return router_get_hash_impl(s,digest,
- "signed-directory","\ndirectory-signature");
- }
- int
- router_get_router_hash(const char *s, char *digest)
- {
- return router_get_hash_impl(s,digest,
- "router ","\nrouter-signature");
- }
- int
- router_get_runningrouters_hash(const char *s, char *digest)
- {
- return router_get_hash_impl(s,digest,
- "network-status","\ndirectory-signature");
- }
- int
- router_get_networkstatus_v2_hash(const char *s, char *digest)
- {
- return router_get_hash_impl(s,digest,
- "network-status-version","\ndirectory-signature");
- }
- int
- router_append_dirobj_signature(char *buf, size_t buf_len, const char *digest,
- crypto_pk_env_t *private_key)
- {
- char signature[PK_BYTES];
- int i;
- if (crypto_pk_private_sign(private_key, signature, digest, DIGEST_LEN) < 0) {
- warn(LD_BUG,"Couldn't sign digest.");
- return -1;
- }
- if (strlcat(buf, "-----BEGIN SIGNATURE-----\n", buf_len) >= buf_len)
- goto truncated;
- i = strlen(buf);
- if (base64_encode(buf+i, buf_len-i, signature, 128) < 0) {
- warn(LD_BUG,"couldn't base64-encode signature");
- tor_free(buf);
- return -1;
- }
- if (strlcat(buf, "-----END SIGNATURE-----\n", buf_len) >= buf_len)
- goto truncated;
- return 0;
- truncated:
- warn(LD_BUG,"tried to exceed string length.");
- return -1;
- }
- version_status_t
- tor_version_is_obsolete(const char *myversion, const char *versionlist)
- {
- const char *vl;
- tor_version_t mine, other;
- int found_newer = 0, found_older = 0, found_newer_in_series = 0,
- found_any_in_series = 0, r, same;
- version_status_t ret = VS_UNRECOMMENDED;
- smartlist_t *version_sl;
- vl = versionlist;
- debug(LD_CONFIG,"Checking whether version '%s' is in '%s'",
- myversion, versionlist);
- if (tor_version_parse(myversion, &mine)) {
- err(LD_BUG,"I couldn't parse my own version (%s)", myversion);
- tor_assert(0);
- }
- version_sl = smartlist_create();
- smartlist_split_string(version_sl, versionlist, ",", SPLIT_SKIP_SPACE, 0);
- SMARTLIST_FOREACH(version_sl, const char *, cp, {
- if (!strcmpstart(cp, "Tor "))
- cp += 4;
- if (tor_version_parse(cp, &other)) {
-
- } else {
- same = tor_version_same_series(&mine, &other);
- if (same)
- found_any_in_series = 1;
- r = tor_version_compare(&mine, &other);
- if (r==0) {
- ret = VS_RECOMMENDED;
- goto done;
- } else if (r<0) {
- found_newer = 1;
- if (same)
- found_newer_in_series = 1;
- } else if (r>0) {
- found_older = 1;
- }
- }
- });
-
- if (found_any_in_series && !found_newer_in_series) {
- ret = VS_NEW_IN_SERIES;
- } else if (found_newer && !found_older) {
- ret = VS_OLD;
- } else if (found_older && !found_newer) {
- ret = VS_NEW;
- } else {
- ret = VS_UNRECOMMENDED;
- }
- done:
- SMARTLIST_FOREACH(version_sl, char *, version, tor_free(version));
- smartlist_free(version_sl);
- return ret;
- }
- version_status_t
- version_status_join(version_status_t a, version_status_t b)
- {
- if (a == b)
- return a;
- else if (a == VS_UNRECOMMENDED || b == VS_UNRECOMMENDED)
- return VS_UNRECOMMENDED;
- else if (a == VS_RECOMMENDED)
- return b;
- else if (b == VS_RECOMMENDED)
- return a;
-
- else if (a == VS_OLD || b == VS_OLD)
- return VS_UNRECOMMENDED;
-
- else
- return VS_NEW_IN_SERIES;
- }
- int
- router_parse_directory(const char *str)
- {
- directory_token_t *tok;
- char digest[DIGEST_LEN];
- time_t published_on;
- int r;
- const char *end, *cp;
- smartlist_t *tokens = NULL;
- crypto_pk_env_t *declared_key = NULL;
-
- if (router_get_dir_hash(str, digest)) {
- warn(LD_DIR, "Unable to compute digest of directory");
- goto err;
- }
- debug(LD_DIR,"Received directory hashes to %s",hex_str(digest,4));
-
- cp = str;
- while (cp && (end = strstr(cp+1, "\ndirectory-signature")))
- cp = end;
- if (cp == str || !cp) {
- warn(LD_DIR, "No signature found on directory."); goto err;
- }
- ++cp;
- tokens = smartlist_create();
- if (tokenize_string(cp,strchr(cp,'\0'),tokens,DIR)) {
- warn(LD_DIR, "Error tokenizing directory signature"); goto err;
- }
- if (smartlist_len(tokens) != 1) {
- warn(LD_DIR, "Unexpected number of tokens in signature"); goto err;
- }
- tok=smartlist_get(tokens,0);
- if (tok->tp != K_DIRECTORY_SIGNATURE) {
- warn(LD_DIR,"Expected a single directory signature"); goto err;
- }
- declared_key = find_dir_signing_key(str);
- if (check_directory_signature(digest, tok, NULL, declared_key, 1)<0)
- goto err;
- SMARTLIST_FOREACH(tokens, directory_token_t *, tok, token_free(tok));
- smartlist_free(tokens);
- tokens = NULL;
-
- if ((end = strstr(str,"\nrouter "))) {
- ++end;
- } else if ((end = strstr(str, "\ndirectory-signature"))) {
- ++end;
- } else {
- end = str + strlen(str);
- }
- tokens = smartlist_create();
- if (tokenize_string(str,end,tokens,DIR)) {
- warn(LD_DIR, "Error tokenizing directory"); goto err;
- }
- if (!(tok = find_first_by_keyword(tokens, K_PUBLISHED))) {
- warn(LD_DIR, "Missing published time on directory.");
- goto err;
- }
- tor_assert(tok->n_args == 1);
- if (parse_iso_time(tok->args[0], &published_on) < 0) {
- goto err;
- }
-
- if (get_options()->DirPort && !get_options()->V1AuthoritativeDir)
- dirserv_set_cached_directory(str, published_on, 0);
- r = 0;
- goto done;
- err:
- r = -1;
- done:
- if (declared_key) crypto_free_pk_env(declared_key);
- if (tokens) {
- SMARTLIST_FOREACH(tokens, directory_token_t *, tok, token_free(tok));
- smartlist_free(tokens);
- }
- return r;
- }
- int
- router_parse_runningrouters(const char *str)
- {
- char digest[DIGEST_LEN];
- directory_token_t *tok;
- time_t published_on;
- int r = -1;
- crypto_pk_env_t *declared_key = NULL;
- smartlist_t *tokens = NULL;
- if (router_get_runningrouters_hash(str, digest)) {
- warn(LD_DIR, "Unable to compute digest of directory");
- goto err;
- }
- tokens = smartlist_create();
- if (tokenize_string(str,str+strlen(str),tokens,DIR)) {
- warn(LD_DIR, "Error tokenizing directory"); goto err;
- }
- if ((tok = find_first_by_keyword(tokens, _UNRECOGNIZED))) {
- warn(LD_DIR, "Unrecognized keyword '%s'; can't parse running-routers",
- tok->args[0]);
- goto err;
- }
- tok = smartlist_get(tokens,0);
- if (tok->tp != K_NETWORK_STATUS) {
- warn(LD_DIR, "Network-status starts with wrong token");
- goto err;
- }
- if (!(tok = find_first_by_keyword(tokens, K_PUBLISHED))) {
- warn(LD_DIR, "Missing published time on directory.");
- goto err;
- }
- tor_assert(tok->n_args == 1);
- if (parse_iso_time(tok->args[0], &published_on) < 0) {
- goto err;
- }
- if (!(tok = find_first_by_keyword(tokens, K_DIRECTORY_SIGNATURE))) {
- warn(LD_DIR, "Missing signature on running-routers");
- goto err;
- }
- declared_key = find_dir_signing_key(str);
- if (check_directory_signature(digest, tok, NULL, declared_key, 1) < 0)
- goto err;
-
- if (get_options()->DirPort && !get_options()->V1AuthoritativeDir)
- dirserv_set_cached_directory(str, published_on, 1);
- r = 0;
- err:
- if (declared_key) crypto_free_pk_env(declared_key);
- if (tokens) {
- SMARTLIST_FOREACH(tokens, directory_token_t *, tok, token_free(tok));
- smartlist_free(tokens);
- }
- return r;
- }
- static crypto_pk_env_t *
- find_dir_signing_key(const char *str)
- {
- const char *cp;
- directory_token_t *tok;
- crypto_pk_env_t *key = NULL;
-
- cp = strstr(str, "\nopt dir-signing-key");
- if (!cp)
- cp = strstr(str, "\ndir-signing-key");
- if (!cp)
- return NULL;
- ++cp;
- tok = get_next_token(&cp, DIR);
- if (!tok) {
- warn(LD_DIR, "Unparseable dir-signing-key token");
- return NULL;
- }
- if (tok->tp != K_DIR_SIGNING_KEY) {
- warn(LD_DIR, "Dir-signing-key token did not parse as expected");
- return NULL;
- }
- if (tok->key) {
- key = tok->key;
- tok->key = NULL;
- } else if (tok->n_args >= 1) {
-
- key = crypto_pk_DER64_decode_public_key(tok->args[0]);
- if (!key) {
- warn(LD_DIR, "Unparseable dir-signing-key argument");
- return NULL;
- }
- } else {
- warn(LD_DIR, "Dir-signing-key token contained no key");
- return NULL;
- }
- token_free(tok);
- return key;
- }
- static int
- dir_signing_key_is_trusted(crypto_pk_env_t *key)
- {
- char digest[DIGEST_LEN];
- if (!key) return 0;
- if (crypto_pk_get_digest(key, digest) < 0) {
- warn(LD_DIR, "Error computing dir-signing-key digest");
- return 0;
- }
- if (!router_digest_is_trusted_dir(digest)) {
- warn(LD_DIR, "Listed dir-signing-key is not trusted");
- return 0;
- }
- return 1;
- }
- static int
- check_directory_signature(const char *digest,
- directory_token_t *tok,
- crypto_pk_env_t *pkey,
- crypto_pk_env_t *declared_key,
- int check_authority)
- {
- char signed_digest[PK_BYTES];
- crypto_pk_env_t *_pkey = NULL;
- if (tok->n_args != 1) {
- warn(LD_DIR, "Too many or too few arguments to directory-signature");
- return -1;
- }
- if (declared_key) {
- if (!check_authority || dir_signing_key_is_trusted(declared_key))
- _pkey = declared_key;
- }
- if (!_pkey && pkey) {
-
- _pkey = pkey;
- }
- if (!_pkey) {
- warn(LD_DIR, "Obsolete directory format (dir signing key not present) or "
- "signing key not trusted--rejecting.");
- return -1;
- }
- if (strcmp(tok->object_type, "SIGNATURE") || tok->object_size != 128) {
- warn(LD_DIR, "Bad object type or length on directory signature");
- return -1;
- }
- tor_assert(_pkey);
- if (crypto_pk_public_checksig(_pkey, signed_digest, tok->object_body, 128)
- != 20) {
- warn(LD_DIR, "Error reading directory: invalid signature.");
- return -1;
- }
- debug(LD_DIR,"Signed directory hash starts %s", hex_str(signed_digest,4));
- if (memcmp(digest, signed_digest, DIGEST_LEN)) {
- warn(LD_DIR, "Error reading directory: signature does not match.");
- return -1;
- }
- return 0;
- }
- int
- router_parse_list_from_string(const char **s, smartlist_t *dest)
- {
- routerinfo_t *router;
- const char *end, *cp;
- tor_assert(s);
- tor_assert(*s);
- tor_assert(dest);
- while (1) {
- *s = eat_whitespace(*s);
-
- if (strcmpstart(*s, "router ")!=0)
- break;
- if ((end = strstr(*s+1, "\nrouter "))) {
- cp = end;
- end++;
- } else if ((end = strstr(*s+1, "\ndirectory-signature"))) {
- cp = end;
- end++;
- } else {
- cp = end = *s+strlen(*s);
- }
- while (cp > *s && (!*cp || TOR_ISSPACE(*cp)))
- --cp;
-
- while (cp > *s && *cp != '\n')
- --cp;
-
- if (strcmpstart(cp, "\n-----END SIGNATURE-----\n")) {
- info(LD_DIR, "Ignoring truncated router descriptor.");
- *s = end;
- continue;
- }
- router = router_parse_entry_from_string(*s, end);
- *s = end;
- if (!router) {
- warn(LD_DIR, "Error reading router; skipping");
- continue;
- }
- smartlist_add(dest, router);
- }
- return 0;
- }
- routerinfo_t *
- router_parse_entry_from_string(const char *s, const char *end)
- {
- routerinfo_t *router = NULL;
- char signed_digest[128];
- char digest[128];
- smartlist_t *tokens = NULL, *exit_policy_tokens = NULL;
- directory_token_t *tok;
- int t;
- int ports_set, bw_set;
- struct in_addr in;
- if (!end) {
- end = s + strlen(s);
- }
-
- while (end > s+2 && *(end-1) == '\n' && *(end-2) == '\n')
- --end;
- if (router_get_router_hash(s, digest) < 0) {
- warn(LD_DIR, "Couldn't compute router hash.");
- return NULL;
- }
- tokens = smartlist_create();
- if (tokenize_string(s,end,tokens,RTR)) {
- warn(LD_DIR, "Error tokeninzing router descriptor.");
- goto err;
- }
- if (smartlist_len(tokens) < 2) {
- warn(LD_DIR, "Impossibly short router descriptor.");
- goto err;
- }
- if ((tok = find_first_by_keyword(tokens, _UNRECOGNIZED))) {
- warn(LD_DIR,
- "Unrecognized critical keyword '%s'; skipping descriptor. "
- "(It may be from another version of Tor.)",
- tok->args[0]);
- goto err;
- }
- tok = smartlist_get(tokens,0);
- if (tok->tp != K_ROUTER) {
- warn(LD_DIR,"Entry does not start with \"router\"");
- goto err;
- }
- router = tor_malloc_zero(sizeof(routerinfo_t));
- router->cache_info.signed_descriptor = tor_strndup(s, end-s);
- router->cache_info.signed_descriptor_len = end-s;
- memcpy(router->cache_info.signed_descriptor_digest, digest, DIGEST_LEN);
- ports_set = bw_set = 0;
- if (tok->n_args == 2 || tok->n_args == 5 || tok->n_args == 6) {
- router->nickname = tor_strdup(tok->args[0]);
- if (!is_legal_nickname(router->nickname)) {
- warn(LD_DIR,"Router nickname is invalid");
- goto err;
- }
- router->address = tor_strdup(tok->args[1]);
- if (!tor_inet_aton(router->address, &in)) {
- warn(LD_DIR,"Router address is not an IP.");
- goto err;
- }
- router->addr = ntohl(in.s_addr);
- if (tok->n_args >= 5) {
- router->or_port =
- (uint16_t) tor_parse_long(tok->args[2],10,0,65535,NULL,NULL);
- router->dir_port =
- (uint16_t) tor_parse_long(tok->args[4],10,0,65535,NULL,NULL);
- ports_set = 1;
- }
- } else {
- warn(LD_DIR,"Wrong # of arguments to \"router\" (%d)",tok->n_args);
- goto err;
- }
- tok = find_first_by_keyword(tokens, K_PORTS);
- if (tok && ports_set) {
- warn(LD_DIR,"Redundant ports line");
- goto err;
- } else if (tok) {
- if (tok->n_args != 3) {
- warn(LD_DIR,"Wrong # of arguments to \"ports\"");
- goto err;
- }
- router->or_port =
- (uint16_t) tor_parse_long(tok->args[0],10,0,65535,NULL,NULL);
- router->dir_port =
- (uint16_t) tor_parse_long(tok->args[2],10,0,65535,NULL,NULL);
- ports_set = 1;
- }
- tok = find_first_by_keyword(tokens, K_BANDWIDTH);
- if (tok && bw_set) {
- warn(LD_DIR,"Redundant bandwidth line");
- goto err;
- } else if (tok) {
- if (tok->n_args < 3) {
-
- warn(LD_DIR,"Not enough arguments to \"bandwidth\": must be an obsolete "
- " server. Rejecting one server (nickname '%s').", router->nickname);
- goto err;
- }
- router->bandwidthrate =
- tor_parse_long(tok->args[0],10,0,INT_MAX,NULL,NULL);
- router->bandwidthburst =
- tor_parse_long(tok->args[1],10,0,INT_MAX,NULL,NULL);
- router->bandwidthcapacity =
- tor_parse_long(tok->args[2],10,0,INT_MAX,NULL,NULL);
- bw_set = 1;
- }
- if ((tok = find_first_by_keyword(tokens, K_UPTIME))) {
- if (tok->n_args != 1) {
- warn(LD_DIR, "Unrecognized number of args on K_UPTIME; skipping.");
- } else {
- router->uptime = tor_parse_long(tok->args[0],10,0,LONG_MAX,NULL,NULL);
- }
- }
- if ((tok = find_first_by_keyword(tokens, K_HIBERNATING))) {
- if (tok->n_args < 1) {
- warn(LD_DIR, "Too few args on 'hibernating' keyword. Skipping.");
- } else {
- router->is_hibernating
- = (tor_parse_long(tok->args[0],10,0,LONG_MAX,NULL,NULL) != 0);
- }
- }
- if (!(tok = find_first_by_keyword(tokens, K_PUBLISHED))) {
- warn(LD_DIR, "Missing published time"); goto err;
- }
- tor_assert(tok->n_args == 1);
- if (parse_iso_time(tok->args[0], &router->cache_info.published_on) < 0)
- goto err;
- if (!(tok = find_first_by_keyword(tokens, K_ONION_KEY))) {
- warn(LD_DIR, "Missing onion key"); goto err;
- }
- if (crypto_pk_keysize(tok->key) != PK_BYTES) {
- warn(LD_DIR, "Wrong size on onion key: %d bits!",
- (int)crypto_pk_keysize(tok->key)*8);
- goto err;
- }
- router->onion_pkey = tok->key;
- tok->key = NULL;
- if (!(tok = find_first_by_keyword(tokens, K_SIGNING_KEY))) {
- warn(LD_DIR, "Missing identity key"); goto err;
- }
- if (crypto_pk_keysize(tok->key) != PK_BYTES) {
- warn(LD_DIR, "Wrong size on identity key: %d bits!",
- (int)crypto_pk_keysize(tok->key)*8);
- goto err;
- }
- router->identity_pkey = tok->key;
- tok->key = NULL;
- if (crypto_pk_get_digest(router->identity_pkey,
- router->cache_info.identity_digest)) {
- warn(LD_DIR, "Couldn't calculate key digest"); goto err;
- }
- if ((tok = find_first_by_keyword(tokens, K_FINGERPRINT))) {
-
- char d[DIGEST_LEN];
- if (tok->n_args < 1) {
- warn(LD_DIR, "Too few arguments to fingerprint");
- goto err;
- }
- tor_strstrip(tok->args[0], " ");
- if (base16_decode(d, DIGEST_LEN, tok->args[0], strlen(tok->args[0]))) {
- warn(LD_DIR, "Couldn't decode fingerprint '%s'", tok->args[0]);
- goto err;
- }
- if (memcmp(d,router->cache_info.identity_digest, DIGEST_LEN)!=0) {
- warn(LD_DIR, "Fingerprint '%s' does not match identity digest.",
- tok->args[0]);
- goto err;
- }
- }
- if ((tok = find_first_by_keyword(tokens, K_PLATFORM))) {
- router->platform = tor_strdup(tok->args[0]);
- }
- if ((tok = find_first_by_keyword(tokens, K_CONTACT))) {
- router->contact_info = tor_strdup(tok->args[0]);
- }
- exit_policy_tokens = find_all_exitpolicy(tokens);
- SMARTLIST_FOREACH(exit_policy_tokens, directory_token_t *, t,
- if (router_add_exit_policy(router,t)<0) {
- warn(LD_DIR,"Error in exit policy");
- goto err;
- });
- if ((tok = find_first_by_keyword(tokens, K_FAMILY)) && tok->n_args) {
- int i;
- router->declared_family = smartlist_create();
- for (i=0;i<tok->n_args;++i) {
- if (!is_legal_nickname_or_hexdigest(tok->args[i])) {
- warn(LD_DIR, "Illegal nickname '%s' in family line", tok->args[i]);
- goto err;
- }
- smartlist_add(router->declared_family, tor_strdup(tok->args[i]));
- }
- }
- if (!(tok = find_first_by_keyword(tokens, K_ROUTER_SIGNATURE))) {
- warn(LD_DIR, "Missing router signature");
- goto err;
- }
- if (strcmp(tok->object_type, "SIGNATURE") || tok->object_size != 128) {
- warn(LD_DIR, "Bad object type or length on router signature");
- goto err;
- }
- if ((t=crypto_pk_public_checksig(router->identity_pkey, signed_digest,
- tok->object_body, 128)) != 20) {
- warn(LD_DIR, "Invalid signature %d",t);
- goto err;
- }
- if (memcmp(digest, signed_digest, DIGEST_LEN)) {
- warn(LD_DIR, "Mismatched signature");
- goto err;
- }
- if (!ports_set) {
- warn(LD_DIR,"No ports declared; failing.");
- goto err;
- }
- if (!bw_set) {
- warn(LD_DIR,"No bandwidth declared; failing.");
- goto err;
- }
- if (!router->or_port) {
- warn(LD_DIR,"or_port unreadable or 0. Failing.");
- goto err;
- }
- if (!router->bandwidthrate) {
- warn(LD_DIR,"bandwidthrate unreadable or 0. Failing.");
- goto err;
- }
- if (!router->platform) {
- router->platform = tor_strdup("<unknown>");
- }
- goto done;
- err:
- routerinfo_free(router);
- router = NULL;
- done:
- if (tokens) {
- SMARTLIST_FOREACH(tokens, directory_token_t *, tok, token_free(tok));
- smartlist_free(tokens);
- }
- if (exit_policy_tokens) {
- smartlist_free(exit_policy_tokens);
- }
- return router;
- }
- static INLINE const char *
- find_start_of_next_routerstatus(const char *s)
- {
- const char *eos = strstr(s, "\nr ");
- if (!eos)
- eos = strstr(s, "\ndirectory-signature");
- if (eos)
- return eos+1;
- else
- return s + strlen(s);
- }
- static routerstatus_t *
- routerstatus_parse_entry_from_string(const char **s, smartlist_t *tokens)
- {
- const char *eos;
- routerstatus_t *rs = NULL;
- directory_token_t *tok;
- char timebuf[ISO_TIME_LEN+1];
- struct in_addr in;
- tor_assert(tokens);
- eos = find_start_of_next_routerstatus(*s);
- if (tokenize_string(*s, eos, tokens, RTRSTATUS)) {
- warn(LD_DIR, "Error tokenizing router status");
- goto err;
- }
- if (smartlist_len(tokens) < 1) {
- warn(LD_DIR, "Impossibly short router status");
- goto err;
- }
- if ((tok = find_first_by_keyword(tokens, _UNRECOGNIZED))) {
- warn(LD_DIR, "Unrecognized keyword \"%s\" in router status; skipping.",
- tok->args[0]);
- goto err;
- }
- if (!(tok = find_first_by_keyword(tokens, K_R))) {
- warn(LD_DIR, "Missing 'r' keywork in router status; skipping.");
- goto err;
- }
- if (tok->n_args < 8) {
- warn(LD_DIR,
- "Too few arguments to 'r' keywork in router status; skipping.");
- }
- rs = tor_malloc_zero(sizeof(routerstatus_t));
- if (!is_legal_nickname(tok->args[0])) {
- warn(LD_DIR,
- "Invalid nickname '%s' in router status; skipping.", tok->args[0]);
- goto err;
- }
- strlcpy(rs->nickname, tok->args[0], sizeof(rs->nickname));
- if (digest_from_base64(rs->identity_digest, tok->args[1])) {
- warn(LD_DIR, "Error decoding digest '%s'", tok->args[1]);
- goto err;
- }
- if (digest_from_base64(rs->descriptor_digest, tok->args[2])) {
- warn(LD_DIR, "Error decoding digest '%s'", tok->args[2]);
- goto err;
- }
- if (tor_snprintf(timebuf, sizeof(timebuf), "%s %s",
- tok->args[3], tok->args[4]) < 0 ||
- parse_iso_time(timebuf, &rs->published_on)<0) {
- warn(LD_DIR, "Error parsing time '%s %s'", tok->args[3], tok->args[4]);
- goto err;
- }
- if (tor_inet_aton(tok->args[5], &in) == 0) {
- warn(LD_DIR, "Error parsing address '%s'", tok->args[5]);
- goto err;
- }
- rs->addr = ntohl(in.s_addr);
- rs->or_port =(uint16_t) tor_parse_long(tok->args[6],10,0,65535,NULL,NULL);
- rs->dir_port = (uint16_t) tor_parse_long(tok->args[7],10,0,65535,NULL,NULL);
- if ((tok = find_first_by_keyword(tokens, K_S))) {
- int i;
- for (i=0; i < tok->n_args; ++i) {
- if (!strcmp(tok->args[i], "Exit"))
- rs->is_exit = 1;
- else if (!strcmp(tok->args[i], "Stable"))
- rs->is_stable = 1;
- else if (!strcmp(tok->args[i], "Fast"))
- rs->is_fast = 1;
- else if (!strcmp(tok->args[i], "Running"))
- rs->is_running = 1;
- else if (!strcmp(tok->args[i], "Named"))
- rs->is_named = 1;
- else if (!strcmp(tok->args[i], "Valid"))
- rs->is_valid = 1;
- else if (!strcmp(tok->args[i], "V2Dir"))
- rs->is_v2_dir = 1;
- }
- }
- goto done;
- err:
- if (rs)
- routerstatus_free(rs);
- rs = NULL;
- done:
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
- smartlist_clear(tokens);
- *s = eos;
- return rs;
- }
- static int
- _compare_routerstatus_entries(const void **_a, const void **_b)
- {
- const routerstatus_t *a = *_a, *b = *_b;
- return memcmp(a->identity_digest, b->identity_digest, DIGEST_LEN);
- }
- networkstatus_t *
- networkstatus_parse_from_string(const char *s)
- {
- const char *eos;
- smartlist_t *tokens = smartlist_create();
- networkstatus_t *ns = NULL;
- char ns_digest[DIGEST_LEN];
- char tmp_digest[DIGEST_LEN];
- struct in_addr in;
- directory_token_t *tok;
- int i;
- if (router_get_networkstatus_v2_hash(s, ns_digest)) {
- warn(LD_DIR, "Unable to compute digest of network-status");
- goto err;
- }
- eos = find_start_of_next_routerstatus(s);
- if (tokenize_string(s, eos, tokens, NETSTATUS)) {
- warn(LD_DIR, "Error tokenizing network-status header.");
- goto err;
- }
- if ((tok = find_first_by_keyword(tokens, _UNRECOGNIZED))) {
- warn(LD_DIR, "Unrecognized keyword '%s'; can't parse network-status",
- tok->args[0]);
- goto err;
- }
- ns = tor_malloc_zero(sizeof(networkstatus_t));
- memcpy(ns->networkstatus_digest, ns_digest, DIGEST_LEN);
- if (!(tok = find_first_by_keyword(tokens, K_NETWORK_STATUS_VERSION))) {
- warn(LD_DIR, "Couldn't find network-status-version keyword");
- goto err;
- }
- if (!(tok = find_first_by_keyword(tokens, K_DIR_SOURCE))) {
- warn(LD_DIR, "Couldn't find dir-source keyword");
- goto err;
- }
- if (tok->n_args < 3) {
- warn(LD_DIR, "Too few arguments to dir-source keyword");
- goto err;
- }
- ns->source_address = tok->args[0]; tok->args[0] = NULL;
- if (tor_inet_aton(tok->args[1], &in) == 0) {
- warn(LD_DIR, "Error parsing address '%s'", tok->args[1]);
- goto err;
- }
- ns->source_addr = ntohl(in.s_addr);
- ns->source_dirport =
- (uint16_t) tor_parse_long(tok->args[2],10,0,65535,NULL,NULL);
- if (ns->source_dirport == 0) {
- warn(LD_DIR, "Directory source without dirport; skipping.");
- goto err;
- }
- if (!(tok = find_first_by_keyword(tokens, K_FINGERPRINT))) {
- warn(LD_DIR, "Couldn't find fingerprint keyword");
- goto err;
- }
- if (tok->n_args < 1) {
- warn(LD_DIR, "Too few arguments to fingerprint");
- goto err;
- }
- if (base16_decode(ns->identity_digest, DIGEST_LEN, tok->args[0],
- strlen(tok->args[0]))) {
- warn(LD_DIR, "Couldn't decode fingerprint '%s'", tok->args[0]);
- goto err;
- }
- if ((tok = find_first_by_keyword(tokens, K_CONTACT)) && tok->n_args) {
- ns->contact = tok->args[0];
- tok->args[0] = NULL;
- }
- if (!(tok = find_first_by_keyword(tokens, K_DIR_SIGNING_KEY)) || !tok->key) {
- warn(LD_DIR, "Missing dir-signing-key");
- goto err;
- }
- ns->signing_key = tok->key;
- tok->key = NULL;
- if (crypto_pk_get_digest(ns->signing_key, tmp_digest)<0) {
- warn(LD_DIR, "Couldn't compute signing key digest");
- goto err;
- }
- if (memcmp(tmp_digest, ns->identity_digest, DIGEST_LEN)) {
- warn(LD_DIR, "network-status fingerprint did not match dir-signing-key");
- goto err;
- }
- if ((tok = find_first_by_keyword(tokens, K_DIR_OPTIONS))) {
- for (i=0; i < tok->n_args; ++i) {
- if (!strcmp(tok->args[i], "Names"))
- ns->binds_names = 1;
- if (!strcmp(tok->args[i], "Versions"))
- ns->recommends_versions = 1;
- }
- }
- if (ns->recommends_versions) {
- if (!(tok = find_first_by_keyword(tokens, K_CLIENT_VERSIONS)) ||
- tok->n_args<1) {
- warn(LD_DIR, "Missing client-versions");
- }
- ns->client_versions = tok->args[0];
- tok->args[0] = NULL;
- if (!(tok = find_first_by_keyword(tokens, K_SERVER_VERSIONS)) ||
- tok->n_args<1) {
- warn(LD_DIR, "Missing server-versions on versioning directory");
- goto err;
- }
- ns->server_versions = tok->args[0];
- tok->args[0] = NULL;
- }
- if (!(tok = find_first_by_keyword(tokens, K_PUBLISHED))) {
- warn(LD_DIR, "Missing published time on directory.");
- goto err;
- }
- tor_assert(tok->n_args == 1);
- if (parse_iso_time(tok->args[0], &ns->published_on) < 0) {
- goto err;
- }
- ns->entries = smartlist_create();
- s = eos;
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
- smartlist_clear(tokens);
- while (!strcmpstart(s, "r ")) {
- routerstatus_t *rs;
- if ((rs = routerstatus_parse_entry_from_string(&s, tokens)))
- smartlist_add(ns->entries, rs);
- }
- smartlist_sort(ns->entries, _compare_routerstatus_entries);
-
- for (i=0; i < smartlist_len(ns->entries)-1; ++i) {
- routerstatus_t *rs1 = smartlist_get(ns->entries, i);
- routerstatus_t *rs2 = smartlist_get(ns->entries, i+1);
- if (!memcmp(rs1->identity_digest,
- rs2->identity_digest, DIGEST_LEN)) {
- warn(LD_DIR,
- "Network-status has two entries for the same router. Dropping one.");
- smartlist_del_keeporder(ns->entries, i--);
- routerstatus_free(rs1);
- }
- }
- if (tokenize_string(s, NULL, tokens, NETSTATUS)) {
- warn(LD_DIR, "Error tokenizing network-status footer.");
- goto err;
- }
- if (smartlist_len(tokens) < 1) {
- warn(LD_DIR, "Too few items in network-status footer.");
- goto err;
- }
- tok = smartlist_get(tokens, smartlist_len(tokens)-1);
- if (tok->tp != K_DIRECTORY_SIGNATURE) {
- warn(LD_DIR, "Expected network-status footer to end with a signature.");
- goto err;
- }
- if (check_directory_signature(ns_digest, tok, NULL, ns->signing_key, 0))
- goto err;
- goto done;
- err:
- if (ns)
- networkstatus_free(ns);
- ns = NULL;
- done:
- SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
- smartlist_free(tokens);
- return ns;
- }
- addr_policy_t *
- router_parse_addr_policy_from_string(const char *s, int assume_action)
- {
- directory_token_t *tok = NULL;
- const char *cp;
- char *tmp;
- addr_policy_t *r;
- size_t len, idx;
-
- len = strlen(s);
- cp = tmp = tor_malloc(len+2);
- for (idx = 0; idx < len; ++idx) {
- tmp[idx] = tolower(s[idx]);
- }
- tmp[len]='\n';
- tmp[len+1]='\0';
- while (TOR_ISSPACE(*cp))
- ++cp;
- if ((*cp == '*' || TOR_ISDIGIT(*cp)) && assume_action >= 0) {
- char *new_str = tor_malloc(len+10);
- tor_snprintf(new_str, len+10, "%s %s\n",
- assume_action == ADDR_POLICY_ACCEPT?"accept":"reject", cp);
- tor_free(tmp);
- cp = tmp = new_str;
- }
- tok = get_next_token(&cp, RTR);
- if (tok->tp == _ERR) {
- warn(LD_DIR, "Error reading exit policy: %s", tok->error);
- goto err;
- }
- if (tok->tp != K_ACCEPT && tok->tp != K_REJECT) {
- warn(LD_DIR, "Expected 'accept' or 'reject'.");
- goto err;
- }
-
- r = router_parse_addr_policy(tok);
- goto done;
- err:
- r = NULL;
- done:
- tor_free(tmp);
- token_free(tok);
- return r;
- }
- static int
- router_add_exit_policy(routerinfo_t *router, directory_token_t *tok)
- {
- addr_policy_t *newe, **tmpe;
- newe = router_parse_addr_policy(tok);
- if (!newe)
- return -1;
- for (tmpe = &router->exit_policy; *tmpe; tmpe=&((*tmpe)->next))
- ;
- *tmpe = newe;
- return 0;
- }
- static addr_policy_t *
- router_parse_addr_policy(directory_token_t *tok)
- {
- addr_policy_t *newe;
- char *arg;
- tor_assert(tok->tp == K_REJECT || tok->tp == K_ACCEPT);
- if (tok->n_args != 1)
- return NULL;
- arg = tok->args[0];
- newe = tor_malloc_zero(sizeof(addr_policy_t));
- newe->string = tor_malloc(8+strlen(arg));
-
- tor_snprintf(newe->string, 8+strlen(arg), "%s %s",
- (tok->tp == K_REJECT) ? "reject" : "accept", arg);
- newe->policy_type = (tok->tp == K_REJECT) ? ADDR_POLICY_REJECT
- : ADDR_POLICY_ACCEPT;
- if (parse_addr_and_port_range(arg, &newe->addr, &newe->msk,
- &newe->prt_min, &newe->prt_max))
- goto policy_read_failed;
- return newe;
- policy_read_failed:
- tor_assert(newe->string);
- warn(LD_DIR,"Couldn't parse line '%s'. Dropping", newe->string);
- tor_free(newe->string);
- tor_free(newe);
- return NULL;
- }
- void
- assert_addr_policy_ok(addr_policy_t *t)
- {
- addr_policy_t *t2;
- while (t) {
- tor_assert(t->policy_type == ADDR_POLICY_REJECT ||
- t->policy_type == ADDR_POLICY_ACCEPT);
- tor_assert(t->prt_min <= t->prt_max);
- t2 = router_parse_addr_policy_from_string(t->string, -1);
- tor_assert(t2);
- tor_assert(t2->policy_type == t->policy_type);
- tor_assert(t2->addr == t->addr);
- tor_assert(t2->msk == t->msk);
- tor_assert(t2->prt_min == t->prt_min);
- tor_assert(t2->prt_max == t->prt_max);
- tor_assert(!strcmp(t2->string, t->string));
- tor_assert(t2->next == NULL);
- addr_policy_free(t2);
- t = t->next;
- }
- }
- static void
- token_free(directory_token_t *tok)
- {
- int i;
- tor_assert(tok);
- if (tok->args) {
- for (i = 0; i < tok->n_args; ++i) {
- tor_free(tok->args[i]);
- }
- tor_free(tok->args);
- }
- tor_free(tok->object_type);
- tor_free(tok->object_body);
- if (tok->key)
- crypto_free_pk_env(tok->key);
- tor_free(tok);
- }
- static directory_token_t *
- get_next_token(const char **s, where_syntax where)
- {
- const char *next, *obstart;
- int i, done, allocated, is_opt;
- directory_token_t *tok;
- arg_syntax a_syn;
- obj_syntax o_syn = NO_OBJ;
- #define RET_ERR(msg) \
- do { if (tok) token_free(tok); \
- tok = tor_malloc_zero(sizeof(directory_token_t));\
- tok->tp = _ERR; \
- tok->error = msg; \
- goto done_tokenizing; } while (0)
- tok = tor_malloc_zero(sizeof(directory_token_t));
- tok->tp = _ERR;
- *s = eat_whitespace(*s);
- if (!**s) {
- tok->tp = _EOF;
- return tok;
- }
- next = find_whitespace(*s);
- if (!next) {
- tok->error = "Unexpected EOF"; return tok;
- }
-
- is_opt = !strncmp("opt", *s, next-*s);
- if (is_opt) {
- *s = eat_whitespace(next);
- next = NULL;
- if (**s)
- next = find_whitespace(*s);
- if (!**s || !next) {
- RET_ERR("opt without keyword");
- }
- }
- for (i = 0; token_table[i].t ; ++i) {
- if (!strncmp(token_table[i].t, *s, next-*s)) {
-
- tok->tp = token_table[i].v;
- a_syn = token_table[i].s;
- o_syn = token_table[i].os;
- if (!(token_table[i].ws & where)) {
- if (where == DIR) {
- RET_ERR("Found an out-of-place token in a directory section");
- } else if (where == RTR) {
- RET_ERR("Found an out-of-place token in a router descriptor");
- } else if (where == NETSTATUS) {
- RET_ERR("Found an out-of-place token in a network-status header");
- } else {
- RET_ERR("Found an out-of-place token in a router status body");
- }
- }
- if (a_syn == ARGS) {
-
- i = 0;
- done = (*next == '\n');
- allocated = 32;
- tok->args = tor_malloc(sizeof(char*)*32);
- *s = eat_whitespace_no_nl(next);
- while (**s != '\n' && !done) {
- next = find_whitespace(*s);
- if (*next == '\n')
- done = 1;
- if (i == allocated) {
- allocated *= 2;
- tok->args = tor_realloc(tok->args,sizeof(char*)*allocated);
- }
- tok->args[i++] = tor_strndup(*s,next-*s);
- *s = eat_whitespace_no_nl(next+1);
- }
- tok->n_args = i;
- } else if (a_syn == CONCAT_ARGS) {
-
- *s = eat_whitespace_no_nl(next);
- next = strchr(*s, '\n');
- if (!next)
- RET_ERR("Unexpected EOF");
- tok->args = tor_malloc(sizeof(char*));
- tok->args[0] = tor_strndup(*s,next-*s);
- tok->n_args = 1;
- *s = eat_whitespace_no_nl(next+1);
- } else {
-
- tor_assert(a_syn == NO_ARGS);
- *s = eat_whitespace_no_nl(next);
- if (**s != '\n') {
- RET_ERR("Unexpected arguments");
- }
- tok->n_args = 0;
- *s = eat_whitespace_no_nl(*s+1);
- }
- break;
- }
- }
- if (tok->tp == _ERR) {
- if (is_opt) {
- tok->tp = K_OPT;
- *s = eat_whitespace_no_nl(next);
- next = strchr(*s,'\n');
- if (!next)
- RET_ERR("Unexpected EOF");
- tok->args = tor_malloc(sizeof(char*));
- tok->args[0] = tor_strndup(*s,next-*s);
- tok->n_args = 1;
- *s = eat_whitespace_no_nl(next+1);
- o_syn = OBJ_OK;
- } else {
- tok->tp = _UNRECOGNIZED;
- next = strchr(*s, '\n');
- if (!next) {
- RET_ERR("Unexpected EOF");
- }
- tok->args = tor_malloc(sizeof(char*));
- tok->args[0] = tor_strndup(*s,next-*s);
- tok->n_args = 1;
- *s = next+1;
- o_syn = OBJ_OK;
- }
- }
- *s = eat_whitespace(*s);
- if (strcmpstart(*s, "-----BEGIN ")) {
- goto done_tokenizing;
- }
- obstart = *s;
- *s += 11;
- next = strchr(*s, '\n');
- if (next-*s < 6 || strcmpstart(next-5, "-----\n")) {
- RET_ERR("Malformed object: bad begin line");
- }
- tok->object_type = tor_strndup(*s, next-*s-5);
- *s = next+1;
- next = strstr(*s, "-----END ");
- if (!next) {
- RET_ERR("Malformed object: missing end line");
- }
- if (!strcmp(tok->object_type, "RSA PUBLIC KEY")) {
- if (strcmpstart(next, "-----END RSA PUBLIC KEY-----\n"))
- RET_ERR("Malformed object: mismatched end line");
- next = strchr(next,'\n')+1;
- tok->key = crypto_new_pk_env();
- if (crypto_pk_read_public_key_from_string(tok->key, obstart, next-obstart))
- RET_ERR("Couldn't parse public key.");
- *s = next;
- } else {
- tok->object_body = tor_malloc(next-*s);
- i = base64_decode(tok->object_body, 256, *s, next-*s);
- if (i<0) {
- RET_ERR("Malformed object: bad base64-encoded data");
- }
- tok->object_size = i;
- *s = next + 9;
- i = strlen(tok->object_type);
- if (strncmp(*s, tok->object_type, i) || strcmpstart(*s+i, "-----\n")) {
- RET_ERR("Malformed object: mismatched end tag");
- }
- *s += i+6;
- }
- switch (o_syn)
- {
- case NO_OBJ:
- if (tok->object_body)
- RET_ERR("Unexpected object for keyword");
- if (tok->key)
- RET_ERR("Unexpected public key for keyword");
- break;
- case NEED_OBJ:
- if (!tok->object_body)
- RET_ERR("Missing object for keyword");
- break;
- case NEED_KEY:
- if (!tok->key)
- RET_ERR("Missing public key for keyword");
- break;
- case OBJ_OK:
- break;
- }
- done_tokenizing:
- return tok;
- #undef RET_ERR
- }
- static int
- tokenize_string(const char *start, const char *end, smartlist_t *out,
- where_syntax where)
- {
- const char **s;
- directory_token_t *tok = NULL;
- s = &start;
- if (!end)
- end = start+strlen(start);
- while (*s < end && (!tok || tok->tp != _EOF)) {
- tok = get_next_token(s, where);
- if (tok->tp == _ERR) {
- warn(LD_DIR, "parse error: %s", tok->error);
- return -1;
- }
- smartlist_add(out, tok);
- *s = eat_whitespace(*s);
- }
- return 0;
- }
- static directory_token_t *
- find_first_by_keyword(smartlist_t *s, directory_keyword keyword)
- {
- SMARTLIST_FOREACH(s, directory_token_t *, t, if (t->tp == keyword) return t);
- return NULL;
- }
- static smartlist_t *
- find_all_exitpolicy(smartlist_t *s)
- {
- smartlist_t *out = smartlist_create();
- SMARTLIST_FOREACH(s, directory_token_t *, t,
- if (t->tp == K_ACCEPT || t->tp == K_REJECT)
- smartlist_add(out,t));
- return out;
- }
- static int
- router_get_hash_impl(const char *s, char *digest,
- const char *start_str,
- const char *end_str)
- {
- char *start, *end;
- start = strstr(s, start_str);
- if (!start) {
- warn(LD_DIR,"couldn't find \"%s\"",start_str);
- return -1;
- }
- if (start != s && *(start-1) != '\n') {
- warn(LD_DIR, "first occurrence of \"%s\" is not at the start of a line",
- start_str);
- return -1;
- }
- end = strstr(start+strlen(start_str), end_str);
- if (!end) {
- warn(LD_DIR,"couldn't find \"%s\"",end_str);
- return -1;
- }
- end = strchr(end+strlen(end_str), '\n');
- if (!end) {
- warn(LD_DIR,"couldn't find EOL");
- return -1;
- }
- ++end;
- if (crypto_digest(digest, start, end-start)) {
- warn(LD_DIR,"couldn't compute digest");
- return -1;
- }
- return 0;
- }
- int
- tor_version_as_new_as(const char *platform, const char *cutoff)
- {
- tor_version_t cutoff_version, router_version;
- char *s, *start;
- char tmp[128];
- if (tor_version_parse(cutoff, &cutoff_version)<0) {
- warn(LD_DIR,"Bug: cutoff version '%s' unparseable.",cutoff);
- return 0;
- }
- if (strcmpstart(platform,"Tor "))
- return 1;
- start = (char *)eat_whitespace(platform+3);
- if (!*start) return 0;
- s = (char *)find_whitespace(start);
- if ((size_t)(s-start+1) >= sizeof(tmp))
- return 0;
- strlcpy(tmp, start, s-start+1);
- if (tor_version_parse(tmp, &router_version)<0) {
- info(LD_DIR,"Router version '%s' unparseable.",tmp);
- return 1;
- }
- return tor_version_compare(&router_version, &cutoff_version) >= 0;
- }
- int
- tor_version_parse(const char *s, tor_version_t *out)
- {
- char *eos=NULL, *cp=NULL;
-
- tor_assert(s);
- tor_assert(out);
- memset(out, 0, sizeof(tor_version_t));
- if (!strcasecmpstart(s, "Tor "))
- cp += 4;
-
- out->major = strtol(s,&eos,10);
- if (!eos || eos==s || *eos != '.') return -1;
- cp = eos+1;
-
- out->minor = strtol(cp,&eos,10);
- if (!eos || eos==cp || *eos != '.') return -1;
- cp = eos+1;
-
- out->micro = strtol(cp,&eos,10);
- if (!eos || eos==cp) return -1;
- if (!*eos) {
- out->status = VER_RELEASE;
- out->patchlevel = 0;
- out->cvs = IS_NOT_CVS;
- return 0;
- }
- cp = eos;
-
- if (*cp == '.') {
- out->status = VER_RELEASE;
- ++cp;
- } else if (0==strncmp(cp, "pre", 3)) {
- out->status = VER_PRE;
- cp += 3;
- } else if (0==strncmp(cp, "rc", 2)) {
- out->status = VER_RC;
- cp += 2;
- } else {
- return -1;
- }
-
- out->patchlevel = strtol(cp,&eos,10);
- if (!eos || eos==cp) return -1;
- cp = eos;
-
- if (*cp == '-' || *cp == '.')
- ++cp;
- strlcpy(out->status_tag, cp, sizeof(out->status_tag));
- if (0==strcmp(cp, "cvs")) {
- out->cvs = IS_CVS;
- } else {
- out->cvs = IS_NOT_CVS;
- }
- return 0;
- }
- int
- tor_version_compare(tor_version_t *a, tor_version_t *b)
- {
- int i;
- tor_assert(a);
- tor_assert(b);
- if ((i = a->major - b->major))
- return i;
- else if ((i = a->minor - b->minor))
- return i;
- else if ((i = a->micro - b->micro))
- return i;
- else if ((i = a->status - b->status))
- return i;
- else if ((i = a->patchlevel - b->patchlevel))
- return i;
- if (a->major > 0 || a->minor > 0) {
- return strcmp(a->status_tag, b->status_tag);
- } else {
- return (a->cvs - b->cvs);
- }
- }
- static int
- tor_version_same_series(tor_version_t *a, tor_version_t *b)
- {
- tor_assert(a);
- tor_assert(b);
- return ((a->major == b->major) &&
- (a->minor == b->minor) &&
- (a->micro == b->micro));
- }
|