routers.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. /* Copyright 2001-2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #define OR_PUBLICKEY_BEGIN_TAG "-----BEGIN RSA PUBLIC KEY-----\n"
  5. #define OR_PUBLICKEY_END_TAG "-----END RSA PUBLIC KEY-----\n"
  6. #define OR_SIGNATURE_BEGIN_TAG "-----BEGIN SIGNATURE-----\n"
  7. #define OR_SIGNATURE_END_TAG "-----END SIGNATURE-----\n"
  8. #define _GNU_SOURCE
  9. /* XXX this is required on rh7 to make strptime not complain. how bad
  10. * is this for portability?
  11. */
  12. #include "or.h"
  13. /****************************************************************************/
  14. static directory_t *directory = NULL; /* router array */
  15. static routerinfo_t *desc_routerinfo = NULL; /* my descriptor */
  16. static char descriptor[8192]; /* string representation of my descriptor */
  17. extern or_options_t options; /* command-line and config-file options */
  18. /****************************************************************************/
  19. struct directory_token;
  20. typedef struct directory_token directory_token_t;
  21. /* static function prototypes */
  22. void routerlist_free(routerinfo_t *list);
  23. static char *eat_whitespace(char *s);
  24. static char *eat_whitespace_no_nl(char *s);
  25. static char *find_whitespace(char *s);
  26. static void router_free_exit_policy(routerinfo_t *router);
  27. static int router_add_exit_policy_from_string(routerinfo_t *router, char *s);
  28. static int router_add_exit_policy(routerinfo_t *router,
  29. directory_token_t *tok);
  30. static int router_resolve_directory(directory_t *dir);
  31. /****************************************************************************/
  32. void router_retry_connections(void) {
  33. int i;
  34. routerinfo_t *router;
  35. for (i=0;i<directory->n_routers;i++) {
  36. router = directory->routers[i];
  37. if(!connection_exact_get_by_addr_port(router->addr,router->or_port)) { /* not in the list */
  38. log_fn(LOG_DEBUG,"connecting to OR %s:%u.",router->address,router->or_port);
  39. connection_or_connect(router);
  40. }
  41. }
  42. }
  43. routerinfo_t *router_pick_directory_server(void) {
  44. /* pick the first running router with a positive dir_port */
  45. int i;
  46. routerinfo_t *router;
  47. if(!directory)
  48. return NULL;
  49. for(i=0;i<directory->n_routers;i++) {
  50. router = directory->routers[i];
  51. if(router->dir_port > 0 && router->is_running)
  52. return router;
  53. }
  54. return NULL;
  55. }
  56. void router_upload_desc_to_dirservers(void) {
  57. int i;
  58. routerinfo_t *router;
  59. if(!directory)
  60. return;
  61. if (!router_get_my_descriptor()) {
  62. log_fn(LOG_WARN, "No descriptor; skipping upload");
  63. return;
  64. }
  65. for(i=0;i<directory->n_routers;i++) {
  66. router = directory->routers[i];
  67. if(router->dir_port > 0)
  68. directory_initiate_command(router, DIR_CONN_STATE_CONNECTING_UPLOAD);
  69. }
  70. }
  71. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  72. int i;
  73. routerinfo_t *router;
  74. assert(directory);
  75. for(i=0;i<directory->n_routers;i++) {
  76. router = directory->routers[i];
  77. if ((router->addr == addr) && (router->or_port == port))
  78. return router;
  79. }
  80. return NULL;
  81. }
  82. routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk)
  83. {
  84. int i;
  85. routerinfo_t *router;
  86. assert(directory);
  87. for(i=0;i<directory->n_routers;i++) {
  88. router = directory->routers[i];
  89. if (0 == crypto_pk_cmp_keys(router->link_pkey, pk))
  90. return router;
  91. }
  92. return NULL;
  93. }
  94. routerinfo_t *router_get_by_nickname(char *nickname)
  95. {
  96. int i;
  97. routerinfo_t *router;
  98. assert(directory);
  99. for(i=0;i<directory->n_routers;i++) {
  100. router = directory->routers[i];
  101. if (0 == strcmp(router->nickname, nickname))
  102. return router;
  103. }
  104. return NULL;
  105. }
  106. void router_get_directory(directory_t **pdirectory) {
  107. *pdirectory = directory;
  108. }
  109. /* delete a list of routers from memory */
  110. void routerinfo_free(routerinfo_t *router)
  111. {
  112. struct exit_policy_t *e = NULL, *etmp = NULL;
  113. if (!router)
  114. return;
  115. if (router->address)
  116. free(router->address);
  117. if (router->onion_pkey)
  118. crypto_free_pk_env(router->onion_pkey);
  119. if (router->link_pkey)
  120. crypto_free_pk_env(router->link_pkey);
  121. if (router->identity_pkey)
  122. crypto_free_pk_env(router->identity_pkey);
  123. e = router->exit_policy;
  124. while (e) {
  125. etmp = e->next;
  126. if (e->string) free(e->string);
  127. if (e->address) free(e->address);
  128. if (e->port) free(e->port);
  129. free(e);
  130. e = etmp;
  131. }
  132. free(router);
  133. }
  134. void directory_free(directory_t *directory)
  135. {
  136. int i;
  137. for (i = 0; i < directory->n_routers; ++i)
  138. routerinfo_free(directory->routers[i]);
  139. if (directory->routers)
  140. free(directory->routers);
  141. if(directory->software_versions)
  142. free(directory->software_versions);
  143. free(directory);
  144. }
  145. void router_mark_as_down(char *nickname) {
  146. routerinfo_t *router = router_get_by_nickname(nickname);
  147. if(!router) /* we don't seem to know about him in the first place */
  148. return;
  149. log_fn(LOG_DEBUG,"Marking %s as down.",router->nickname);
  150. router->is_running = 0;
  151. }
  152. /* load the router list */
  153. int router_get_list_from_file(char *routerfile)
  154. {
  155. char *string;
  156. string = read_file_to_str(routerfile);
  157. if(!string) {
  158. log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
  159. return -1;
  160. }
  161. if(router_get_list_from_string(string) < 0) {
  162. log_fn(LOG_WARN,"The routerfile itself was corrupt.");
  163. free(string);
  164. return -1;
  165. }
  166. free(string);
  167. return 0;
  168. }
  169. typedef enum {
  170. K_ACCEPT,
  171. K_DIRECTORY_SIGNATURE,
  172. K_RECOMMENDED_SOFTWARE,
  173. K_REJECT,
  174. K_ROUTER,
  175. K_SIGNED_DIRECTORY,
  176. K_SIGNING_KEY,
  177. K_ONION_KEY,
  178. K_LINK_KEY,
  179. K_ROUTER_SIGNATURE,
  180. K_PUBLISHED,
  181. K_RUNNING_ROUTERS,
  182. K_PLATFORM,
  183. _SIGNATURE,
  184. _PUBLIC_KEY,
  185. _ERR,
  186. _EOF
  187. } directory_keyword;
  188. struct token_table_ent { char *t; int v; };
  189. static struct token_table_ent token_table[] = {
  190. { "accept", K_ACCEPT },
  191. { "directory-signature", K_DIRECTORY_SIGNATURE },
  192. { "reject", K_REJECT },
  193. { "router", K_ROUTER },
  194. { "recommended-software", K_RECOMMENDED_SOFTWARE },
  195. { "signed-directory", K_SIGNED_DIRECTORY },
  196. { "signing-key", K_SIGNING_KEY },
  197. { "onion-key", K_ONION_KEY },
  198. { "link-key", K_LINK_KEY },
  199. { "router-signature", K_ROUTER_SIGNATURE },
  200. { "published", K_PUBLISHED },
  201. { "running-routers", K_RUNNING_ROUTERS },
  202. { "platform", K_PLATFORM },
  203. { NULL, -1 }
  204. };
  205. #define MAX_ARGS 1024
  206. struct directory_token {
  207. directory_keyword tp;
  208. union {
  209. struct {
  210. char *args[MAX_ARGS+1];
  211. int n_args;
  212. } cmd;
  213. char *signature;
  214. char *error;
  215. crypto_pk_env_t *public_key;
  216. } val;
  217. };
  218. /* Free any malloced resources allocated for a token. Don't call this if
  219. you inherit the reference to those resources.
  220. */
  221. static void
  222. router_release_token(directory_token_t *tok)
  223. {
  224. switch (tok->tp)
  225. {
  226. case _SIGNATURE:
  227. free(tok->val.signature);
  228. break;
  229. case _PUBLIC_KEY:
  230. crypto_free_pk_env(tok->val.public_key);
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. static int
  237. _router_get_next_token(char **s, directory_token_t *tok) {
  238. char *next;
  239. crypto_pk_env_t *pkey = NULL;
  240. char *signature = NULL;
  241. int i, done;
  242. tok->tp = _ERR;
  243. tok->val.error = "";
  244. *s = eat_whitespace(*s);
  245. if (!**s) {
  246. tok->tp = _EOF;
  247. return 0;
  248. } else if (**s == '-') {
  249. next = strchr(*s, '\n');
  250. if (! next) { tok->val.error = "No newline at EOF"; return -1; }
  251. ++next;
  252. if (! strncmp(*s, OR_PUBLICKEY_BEGIN_TAG, next-*s)) {
  253. next = strstr(*s, OR_PUBLICKEY_END_TAG);
  254. if (!next) { tok->val.error = "No public key end tag found"; return -1; }
  255. next = strchr(next, '\n'); /* Part of OR_PUBLICKEY_END_TAG; can't fail.*/
  256. ++next;
  257. if (!(pkey = crypto_new_pk_env(CRYPTO_PK_RSA)))
  258. return -1;
  259. if (crypto_pk_read_public_key_from_string(pkey, *s, next-*s)) {
  260. crypto_free_pk_env(pkey);
  261. tok->val.error = "Couldn't parse public key.";
  262. return -1;
  263. }
  264. tok->tp = _PUBLIC_KEY;
  265. tok->val.public_key = pkey;
  266. *s = next;
  267. return 0;
  268. } else if (! strncmp(*s, OR_SIGNATURE_BEGIN_TAG, next-*s)) {
  269. /* Advance past newline; can't fail. */
  270. *s = strchr(*s, '\n');
  271. ++*s;
  272. /* Find end of base64'd data */
  273. next = strstr(*s, OR_SIGNATURE_END_TAG);
  274. if (!next) { tok->val.error = "No signature end tag found"; return -1; }
  275. signature = tor_malloc(256);
  276. i = base64_decode(signature, 256, *s, next-*s);
  277. if (i<0) {
  278. free(signature);
  279. tok->val.error = "Error decoding signature."; return -1;
  280. } else if (i != 128) {
  281. free(signature);
  282. tok->val.error = "Bad length on decoded signature."; return -1;
  283. }
  284. tok->tp = _SIGNATURE;
  285. tok->val.signature = signature;
  286. next = strchr(next, '\n'); /* Part of OR_SIGNATURE_END_TAG; can't fail.*/
  287. *s = next+1;
  288. return 0;
  289. } else {
  290. tok->val.error = "Unrecognized begin line"; return -1;
  291. }
  292. } else {
  293. next = find_whitespace(*s);
  294. if (!next) {
  295. tok->val.error = "Unexpected EOF"; return -1;
  296. }
  297. for (i = 0 ; token_table[i].t ; ++i) {
  298. if (!strncmp(token_table[i].t, *s, next-*s)) {
  299. tok->tp = token_table[i].v;
  300. i = 0;
  301. done = (*next == '\n');
  302. *s = eat_whitespace_no_nl(next);
  303. while (**s != '\n' && i <= MAX_ARGS && !done) {
  304. next = find_whitespace(*s);
  305. if (*next == '\n')
  306. done = 1;
  307. *next = 0;
  308. tok->val.cmd.args[i++] = *s;
  309. *s = eat_whitespace_no_nl(next+1);
  310. };
  311. tok->val.cmd.n_args = i;
  312. if (i > MAX_ARGS) {
  313. tok->tp = _ERR;
  314. tok->val.error = "Too many arguments"; return -1;
  315. }
  316. return 0;
  317. }
  318. }
  319. tok->val.error = "Unrecognized command"; return -1;
  320. }
  321. }
  322. #ifdef DEBUG_ROUTER_TOKENS
  323. static void
  324. router_dump_token(directory_token_t *tok) {
  325. int i;
  326. switch(tok->tp)
  327. {
  328. case _SIGNATURE:
  329. puts("(signature)");
  330. return;
  331. case _PUBLIC_KEY:
  332. puts("(public key)");
  333. return;
  334. case _ERR:
  335. printf("(Error: %s\n)", tok->val.error);
  336. return;
  337. case _EOF:
  338. puts("EOF");
  339. return;
  340. case K_ACCEPT: printf("Accept"); break;
  341. case K_DIRECTORY_SIGNATURE: printf("Directory-Signature"); break;
  342. case K_REJECT: printf("Reject"); break;
  343. case K_RECOMMENDED_SOFTWARE: printf("Server-Software"); break;
  344. case K_ROUTER: printf("Router"); break;
  345. case K_SIGNED_DIRECTORY: printf("Signed-Directory"); break;
  346. case K_SIGNING_KEY: printf("Signing-Key"); break;
  347. case K_ONION_KEY: printf("Onion-key"); break;
  348. case K_LINK_KEY: printf("Link-key"); break;
  349. case K_ROUTER_SIGNATURE: printf("Router-signature"); break;
  350. case K_PUBLISHED: printf("Published"); break;
  351. case K_RUNNING_ROUTERS: printf("Running-routers"); break;
  352. case K_PLATFORM: printf("Platform"); break;
  353. default:
  354. printf("?????? %d\n", tok->tp); return;
  355. }
  356. for (i = 0; i < tok->val.cmd.n_args; ++i) {
  357. printf(" \"%s\"", tok->val.cmd.args[i]);
  358. }
  359. printf("\n");
  360. return;
  361. }
  362. static int
  363. router_get_next_token(char **s, directory_token_t *tok) {
  364. int i;
  365. i = _router_get_next_token(s, tok);
  366. router_dump_token(tok);
  367. return i;
  368. }
  369. #else
  370. #define router_get_next_token _router_get_next_token
  371. #endif
  372. /* return the first char of s that is not whitespace and not a comment */
  373. static char *eat_whitespace(char *s) {
  374. assert(s);
  375. while(isspace(*s) || *s == '#') {
  376. while(isspace(*s))
  377. s++;
  378. if(*s == '#') { /* read to a \n or \0 */
  379. while(*s && *s != '\n')
  380. s++;
  381. if(!*s)
  382. return s;
  383. }
  384. }
  385. return s;
  386. }
  387. static char *eat_whitespace_no_nl(char *s) {
  388. while(*s == ' ' || *s == '\t')
  389. ++s;
  390. return s;
  391. }
  392. /* return the first char of s that is whitespace or '#' or '\0 */
  393. static char *find_whitespace(char *s) {
  394. assert(s);
  395. while(*s && !isspace(*s) && *s != '#')
  396. s++;
  397. return s;
  398. }
  399. int router_get_list_from_string(char *s)
  400. {
  401. if (router_get_list_from_string_impl(&s, &directory, -1, NULL)) {
  402. log(LOG_WARN, "Error parsing router file");
  403. return -1;
  404. }
  405. if (router_resolve_directory(directory)) {
  406. log(LOG_WARN, "Error resolving directory");
  407. return -1;
  408. }
  409. return 0;
  410. }
  411. static int router_get_hash_impl(char *s, char *digest, const char *start_str,
  412. const char *end_str)
  413. {
  414. char *start, *end;
  415. start = strstr(s, start_str);
  416. if (!start) {
  417. log_fn(LOG_WARN,"couldn't find \"%s\"",start_str);
  418. return -1;
  419. }
  420. end = strstr(start+strlen(start_str), end_str);
  421. if (!end) {
  422. log_fn(LOG_WARN,"couldn't find \"%s\"",end_str);
  423. return -1;
  424. }
  425. end = strchr(end, '\n');
  426. if (!end) {
  427. log_fn(LOG_WARN,"couldn't find EOL");
  428. return -1;
  429. }
  430. ++end;
  431. if (crypto_SHA_digest(start, end-start, digest)) {
  432. log_fn(LOG_WARN,"couldn't compute digest");
  433. return -1;
  434. }
  435. return 0;
  436. }
  437. int router_get_dir_hash(char *s, char *digest)
  438. {
  439. return router_get_hash_impl(s,digest,
  440. "signed-directory","directory-signature");
  441. }
  442. int router_get_router_hash(char *s, char *digest)
  443. {
  444. return router_get_hash_impl(s,digest,
  445. "router ","router-signature");
  446. }
  447. /* return 0 if myversion is in start. Else return -1. */
  448. int compare_recommended_versions(char *myversion, char *start) {
  449. int len_myversion = strlen(myversion);
  450. char *comma;
  451. char *end = start + strlen(start);
  452. log_fn(LOG_DEBUG,"checking '%s' in '%s'.", myversion, start);
  453. for(;;) {
  454. comma = strchr(start, ',');
  455. if( ((comma ? comma : end) - start == len_myversion) &&
  456. !strncmp(start, myversion, len_myversion)) /* only do strncmp if the length matches */
  457. return 0; /* success, it's there */
  458. if(!comma)
  459. return -1; /* nope */
  460. start = comma+1;
  461. }
  462. }
  463. int router_get_dir_from_string(char *s, crypto_pk_env_t *pkey)
  464. {
  465. if (router_get_dir_from_string_impl(s, &directory, pkey)) {
  466. log_fn(LOG_WARN, "Couldn't parse directory.");
  467. return -1;
  468. }
  469. if (router_resolve_directory(directory)) {
  470. log_fn(LOG_WARN, "Error resolving directory");
  471. return -1;
  472. }
  473. if (compare_recommended_versions(VERSION, directory->software_versions) < 0) {
  474. log(LOG_WARN, "You are running tor version %s, which is no longer supported.\nPlease upgrade to one of %s.", VERSION, directory->software_versions);
  475. if(options.IgnoreVersion) {
  476. log(LOG_WARN, "IgnoreVersion is set. If it breaks, we told you so.");
  477. } else {
  478. log(LOG_ERR,"Set IgnoreVersion config variable if you want to proceed.");
  479. fflush(0);
  480. exit(0);
  481. }
  482. }
  483. return 0;
  484. }
  485. int router_get_dir_from_string_impl(char *s, directory_t **dest,
  486. crypto_pk_env_t *pkey)
  487. {
  488. directory_token_t tok;
  489. char digest[20];
  490. char signed_digest[128];
  491. directory_t *new_dir = NULL;
  492. char *versions;
  493. struct tm published;
  494. time_t published_on;
  495. const char *good_nickname_lst[1024];
  496. int n_good_nicknames;
  497. #define NEXT_TOK() \
  498. do { \
  499. if (router_get_next_token(&s, &tok)) { \
  500. log_fn(LOG_WARN, "Error reading directory: %s", tok.val.error);\
  501. return -1; \
  502. } } while (0)
  503. #define TOK_IS(type,name) \
  504. do { \
  505. if (tok.tp != type) { \
  506. router_release_token(&tok); \
  507. log_fn(LOG_WARN, "Error reading directory: expected %s", name);\
  508. return -1; \
  509. } } while(0)
  510. if (router_get_dir_hash(s, digest)) {
  511. log_fn(LOG_WARN, "Unable to compute digest of directory");
  512. goto err;
  513. }
  514. log(LOG_DEBUG,"Received directory hashes to %02x:%02x:%02x:%02x",
  515. ((int)digest[0])&0xff,((int)digest[1])&0xff,
  516. ((int)digest[2])&0xff,((int)digest[3])&0xff);
  517. NEXT_TOK();
  518. TOK_IS(K_SIGNED_DIRECTORY, "signed-directory");
  519. NEXT_TOK();
  520. TOK_IS(K_PUBLISHED, "published");
  521. if (tok.val.cmd.n_args != 2) {
  522. log_fn(LOG_WARN, "Invalid published line");
  523. goto err;
  524. }
  525. tok.val.cmd.args[1][-1] = ' ';
  526. if (!strptime(tok.val.cmd.args[0], "%Y-%m-%d %H:%M:%S", &published)) {
  527. log_fn(LOG_WARN, "Published time was unparseable"); goto err;
  528. }
  529. published_on = timegm(&published);
  530. NEXT_TOK();
  531. TOK_IS(K_RECOMMENDED_SOFTWARE, "recommended-software");
  532. if (tok.val.cmd.n_args != 1) {
  533. log_fn(LOG_WARN, "Invalid recommended-software line");
  534. goto err;
  535. }
  536. versions = tor_strdup(tok.val.cmd.args[0]);
  537. NEXT_TOK();
  538. TOK_IS(K_RUNNING_ROUTERS, "running-routers");
  539. n_good_nicknames = tok.val.cmd.n_args;
  540. memcpy(good_nickname_lst, tok.val.cmd.args, n_good_nicknames*sizeof(char *));
  541. if (router_get_list_from_string_impl(&s, &new_dir,
  542. n_good_nicknames, good_nickname_lst)) {
  543. log_fn(LOG_WARN, "Error reading routers from directory");
  544. goto err;
  545. }
  546. new_dir->software_versions = versions;
  547. new_dir->published_on = published_on;
  548. NEXT_TOK();
  549. TOK_IS(K_DIRECTORY_SIGNATURE, "directory-signature");
  550. NEXT_TOK();
  551. TOK_IS(_SIGNATURE, "signature");
  552. if (pkey) {
  553. if (crypto_pk_public_checksig(pkey, tok.val.signature, 128, signed_digest)
  554. != 20) {
  555. log_fn(LOG_WARN, "Error reading directory: invalid signature.");
  556. free(tok.val.signature);
  557. goto err;
  558. }
  559. log(LOG_DEBUG,"Signed directory hash starts %02x:%02x:%02x:%02x",
  560. ((int)signed_digest[0])&0xff,((int)signed_digest[1])&0xff,
  561. ((int)signed_digest[2])&0xff,((int)signed_digest[3])&0xff);
  562. if (memcmp(digest, signed_digest, 20)) {
  563. log_fn(LOG_WARN, "Error reading directory: signature does not match.");
  564. free(tok.val.signature);
  565. goto err;
  566. }
  567. }
  568. free(tok.val.signature);
  569. NEXT_TOK();
  570. TOK_IS(_EOF, "end of directory");
  571. if (*dest)
  572. directory_free(*dest);
  573. *dest = new_dir;
  574. return 0;
  575. err:
  576. if (new_dir)
  577. directory_free(new_dir);
  578. return -1;
  579. #undef NEXT_TOK
  580. #undef TOK_IS
  581. }
  582. int router_get_list_from_string_impl(char **s, directory_t **dest,
  583. int n_good_nicknames,
  584. const char **good_nickname_lst)
  585. {
  586. routerinfo_t *router;
  587. routerinfo_t **rarray;
  588. int rarray_len = 0;
  589. int i;
  590. assert(s && *s);
  591. rarray = (routerinfo_t **)tor_malloc((sizeof(routerinfo_t *))*MAX_ROUTERS_IN_DIR);
  592. while (1) {
  593. *s = eat_whitespace(*s);
  594. if (strncmp(*s, "router ", 7)!=0)
  595. break;
  596. router = router_get_entry_from_string(s);
  597. if (!router) {
  598. log_fn(LOG_WARN, "Error reading router");
  599. return -1;
  600. }
  601. if (rarray_len >= MAX_ROUTERS_IN_DIR) {
  602. log_fn(LOG_WARN, "too many routers");
  603. routerinfo_free(router);
  604. continue;
  605. }
  606. if (n_good_nicknames>=0) {
  607. router->is_running = 0;
  608. for (i = 0; i < n_good_nicknames; ++i) {
  609. if (0==strcasecmp(good_nickname_lst[i], router->nickname)) {
  610. router->is_running = 1;
  611. break;
  612. }
  613. }
  614. } else {
  615. router->is_running = 1; /* start out assuming all dirservers are up */
  616. }
  617. rarray[rarray_len++] = router;
  618. log_fn(LOG_DEBUG,"just added router #%d.",rarray_len);
  619. }
  620. if (*dest)
  621. directory_free(*dest);
  622. *dest = (directory_t *)tor_malloc(sizeof(directory_t));
  623. (*dest)->routers = rarray;
  624. (*dest)->n_routers = rarray_len;
  625. (*dest)->software_versions = NULL;
  626. return 0;
  627. }
  628. static int
  629. router_resolve(routerinfo_t *router)
  630. {
  631. struct hostent *rent;
  632. rent = (struct hostent *)gethostbyname(router->address);
  633. if (!rent) {
  634. log_fn(LOG_WARN,"Could not get address for router %s.",router->address);
  635. return -1;
  636. }
  637. assert(rent->h_length == 4);
  638. memcpy(&router->addr, rent->h_addr,rent->h_length);
  639. router->addr = ntohl(router->addr); /* get it back into host order */
  640. return 0;
  641. }
  642. static int
  643. router_resolve_directory(directory_t *dir)
  644. {
  645. int i, max, remove;
  646. if (!dir)
  647. dir = directory;
  648. max = dir->n_routers;
  649. for (i = 0; i < max; ++i) {
  650. remove = 0;
  651. if (router_resolve(dir->routers[i])) {
  652. log_fn(LOG_WARN, "Couldn't resolve router %s; removing",
  653. dir->routers[i]->address);
  654. remove = 1;
  655. routerinfo_free(dir->routers[i]);
  656. } else if (options.Nickname && !strcmp(dir->routers[i]->nickname, options.Nickname)) {
  657. remove = 1;
  658. }
  659. if (remove) {
  660. dir->routers[i] = dir->routers[--max];
  661. --dir->n_routers;
  662. --i;
  663. }
  664. }
  665. return 0;
  666. }
  667. /* reads a single router entry from s.
  668. * updates s so it points to after the router it just read.
  669. * mallocs a new router, returns it if all goes well, else returns NULL.
  670. */
  671. routerinfo_t *router_get_entry_from_string(char**s) {
  672. routerinfo_t *router = NULL;
  673. char signed_digest[128];
  674. char digest[128];
  675. directory_token_t _tok;
  676. directory_token_t *tok = &_tok;
  677. struct tm published;
  678. int t;
  679. #define NEXT_TOKEN() \
  680. do { if (router_get_next_token(s, tok)) { \
  681. log_fn(LOG_WARN, "Error reading directory: %s", tok->val.error);\
  682. goto err; \
  683. } } while(0)
  684. #define ARGS tok->val.cmd.args
  685. if (router_get_router_hash(*s, digest) < 0) {
  686. log_fn(LOG_WARN, "Couldn't compute router hash.");
  687. return NULL;
  688. }
  689. NEXT_TOKEN();
  690. if (tok->tp != K_ROUTER) {
  691. router_release_token(tok);
  692. log_fn(LOG_WARN,"Entry does not start with \"router\"");
  693. return NULL;
  694. }
  695. router = tor_malloc(sizeof(routerinfo_t));
  696. memset(router,0,sizeof(routerinfo_t)); /* zero it out first */
  697. /* C doesn't guarantee that NULL is represented by 0 bytes. You'll
  698. thank me for this someday. */
  699. router->onion_pkey = router->identity_pkey = router->link_pkey = NULL;
  700. if (tok->val.cmd.n_args != 6) {
  701. log_fn(LOG_WARN,"Wrong # of arguments to \"router\"");
  702. goto err;
  703. }
  704. router->nickname = tor_strdup(ARGS[0]);
  705. if (strlen(router->nickname) > MAX_NICKNAME_LEN) {
  706. log_fn(LOG_WARN,"Router nickname too long.");
  707. goto err;
  708. }
  709. if (strspn(router->nickname, LEGAL_NICKNAME_CHARACTERS) !=
  710. strlen(router->nickname)) {
  711. log_fn(LOG_WARN, "Router nickname contains illegal characters.");
  712. goto err;
  713. }
  714. /* read router.address */
  715. router->address = tor_strdup(ARGS[1]);
  716. router->addr = 0;
  717. /* Read router->or_port */
  718. router->or_port = atoi(ARGS[2]);
  719. if(!router->or_port) {
  720. log_fn(LOG_WARN,"or_port unreadable or 0. Failing.");
  721. goto err;
  722. }
  723. /* Router->ap_port */
  724. router->ap_port = atoi(ARGS[3]);
  725. /* Router->dir_port */
  726. router->dir_port = atoi(ARGS[4]);
  727. /* Router->bandwidth */
  728. router->bandwidth = atoi(ARGS[5]);
  729. if (!router->bandwidth) {
  730. log_fn(LOG_WARN,"bandwidth unreadable or 0. Failing.");
  731. goto err;
  732. }
  733. log_fn(LOG_DEBUG,"or_port %d, ap_port %d, dir_port %d, bandwidth %d.",
  734. router->or_port, router->ap_port, router->dir_port, router->bandwidth);
  735. /* XXX Later, require platform before published. */
  736. NEXT_TOKEN();
  737. if (tok->tp == K_PLATFORM) {
  738. NEXT_TOKEN();
  739. }
  740. if (tok->tp != K_PUBLISHED) {
  741. log_fn(LOG_WARN, "Missing published time"); goto err;
  742. }
  743. if (tok->val.cmd.n_args != 2) {
  744. log_fn(LOG_WARN, "Wrong number of arguments to published"); goto err;
  745. }
  746. ARGS[1][-1] = ' '; /* Re-insert space. */
  747. if (!strptime(ARGS[0], "%Y-%m-%d %H:%M:%S", &published)) {
  748. log_fn(LOG_WARN, "Published time was unparseable"); goto err;
  749. }
  750. router->published_on = timegm(&published);
  751. NEXT_TOKEN();
  752. if (tok->tp != K_ONION_KEY) {
  753. log_fn(LOG_WARN, "Missing onion-key"); goto err;
  754. }
  755. NEXT_TOKEN();
  756. if (tok->tp != _PUBLIC_KEY) {
  757. log_fn(LOG_WARN, "Missing onion key"); goto err;
  758. } /* XXX Check key length */
  759. router->onion_pkey = tok->val.public_key;
  760. NEXT_TOKEN();
  761. if (tok->tp != K_LINK_KEY) {
  762. log_fn(LOG_WARN, "Missing link-key"); goto err;
  763. }
  764. NEXT_TOKEN();
  765. if (tok->tp != _PUBLIC_KEY) {
  766. log_fn(LOG_WARN, "Missing link key"); goto err;
  767. } /* XXX Check key length */
  768. router->link_pkey = tok->val.public_key;
  769. NEXT_TOKEN();
  770. if (tok->tp != K_SIGNING_KEY) {
  771. log_fn(LOG_WARN, "Missing signing-key"); goto err;
  772. }
  773. NEXT_TOKEN();
  774. if (tok->tp != _PUBLIC_KEY) {
  775. log_fn(LOG_WARN, "Missing signing key"); goto err;
  776. }
  777. router->identity_pkey = tok->val.public_key;
  778. NEXT_TOKEN();
  779. while (tok->tp == K_ACCEPT || tok->tp == K_REJECT) {
  780. router_add_exit_policy(router, tok);
  781. NEXT_TOKEN();
  782. }
  783. if (tok->tp != K_ROUTER_SIGNATURE) {
  784. log_fn(LOG_WARN,"Missing router signature");
  785. goto err;
  786. }
  787. NEXT_TOKEN();
  788. if (tok->tp != _SIGNATURE) {
  789. log_fn(LOG_WARN,"Missing router signature");
  790. goto err;
  791. }
  792. assert (router->identity_pkey);
  793. if ((t=crypto_pk_public_checksig(router->identity_pkey, tok->val.signature,
  794. 128, signed_digest)) != 20) {
  795. log_fn(LOG_WARN, "Invalid signature %d",t);
  796. goto err;
  797. }
  798. if (memcmp(digest, signed_digest, 20)) {
  799. log_fn(LOG_WARN, "Mismatched signature");
  800. goto err;
  801. }
  802. return router;
  803. err:
  804. router_release_token(tok);
  805. if(router->address)
  806. free(router->address);
  807. if(router->link_pkey)
  808. crypto_free_pk_env(router->link_pkey);
  809. if(router->onion_pkey)
  810. crypto_free_pk_env(router->onion_pkey);
  811. if(router->identity_pkey)
  812. crypto_free_pk_env(router->identity_pkey);
  813. router_free_exit_policy(router);
  814. free(router);
  815. return NULL;
  816. #undef ARGS
  817. #undef NEXT_TOKEN
  818. }
  819. static void router_free_exit_policy(routerinfo_t *router) {
  820. struct exit_policy_t *tmpe;
  821. while(router->exit_policy) {
  822. tmpe = router->exit_policy;
  823. router->exit_policy = tmpe->next;
  824. free(tmpe->string);
  825. free(tmpe->address);
  826. free(tmpe->port);
  827. free(tmpe);
  828. }
  829. }
  830. void router_add_exit_policy_from_config(routerinfo_t *router) {
  831. char *s = options.ExitPolicy, *e;
  832. int last=0;
  833. char line[1024];
  834. if(!s) {
  835. log_fn(LOG_INFO,"No exit policy configured. Ok.");
  836. return; /* nothing to see here */
  837. }
  838. if(!*s) {
  839. log_fn(LOG_INFO,"Exit policy is empty. Ok.");
  840. return; /* nothing to see here */
  841. }
  842. for(;;) {
  843. e = strchr(s,',');
  844. if(!e) {
  845. last = 1;
  846. strcpy(line,s);
  847. } else {
  848. memcpy(line,s,e-s);
  849. line[e-s] = 0;
  850. }
  851. log_fn(LOG_DEBUG,"Adding new entry '%s'",line);
  852. if(router_add_exit_policy_from_string(router,line) < 0)
  853. log_fn(LOG_WARN,"Malformed exit policy %s; skipping.", line);
  854. if(last)
  855. return;
  856. s = e+1;
  857. }
  858. }
  859. static int
  860. router_add_exit_policy_from_string(routerinfo_t *router,
  861. char *s)
  862. {
  863. directory_token_t tok;
  864. char *tmp, *cp;
  865. int r;
  866. int len, idx;
  867. len = strlen(s);
  868. tmp = cp = tor_malloc(len+2);
  869. for (idx = 0; idx < len; ++idx) {
  870. tmp[idx] = tolower(s[idx]);
  871. }
  872. tmp[len]='\n';
  873. tmp[len+1]='\0';
  874. if (router_get_next_token(&cp, &tok)) {
  875. log_fn(LOG_WARN, "Error reading exit policy: %s", tok.val.error);
  876. free(tmp);
  877. return -1;
  878. }
  879. if (tok.tp != K_ACCEPT && tok.tp != K_REJECT) {
  880. log_fn(LOG_WARN, "Expected 'accept' or 'reject'.");
  881. free(tmp);
  882. return -1;
  883. }
  884. r = router_add_exit_policy(router, &tok);
  885. free(tmp);
  886. return r;
  887. }
  888. static int router_add_exit_policy(routerinfo_t *router,
  889. directory_token_t *tok) {
  890. struct exit_policy_t *tmpe, *newe;
  891. char *arg, *colon;
  892. if (tok->val.cmd.n_args != 1)
  893. return -1;
  894. arg = tok->val.cmd.args[0];
  895. newe = tor_malloc(sizeof(struct exit_policy_t));
  896. memset(newe,0,sizeof(struct exit_policy_t));
  897. newe->string = tor_malloc(8+strlen(arg));
  898. if (tok->tp == K_REJECT) {
  899. strcpy(newe->string, "reject ");
  900. newe->policy_type = EXIT_POLICY_REJECT;
  901. } else {
  902. assert(tok->tp == K_ACCEPT);
  903. strcpy(newe->string, "accept ");
  904. newe->policy_type = EXIT_POLICY_ACCEPT;
  905. }
  906. strcat(newe->string, arg);
  907. colon = strchr(arg,':');
  908. if(!colon)
  909. goto policy_read_failed;
  910. *colon = 0;
  911. newe->address = tor_strdup(arg);
  912. newe->port = tor_strdup(colon+1);
  913. log_fn(LOG_DEBUG,"%s %s:%s",
  914. newe->policy_type == EXIT_POLICY_REJECT ? "reject" : "accept",
  915. newe->address, newe->port);
  916. /* now link newe onto the end of exit_policy */
  917. if(!router->exit_policy) {
  918. router->exit_policy = newe;
  919. return 0;
  920. }
  921. for(tmpe=router->exit_policy; tmpe->next; tmpe=tmpe->next) ;
  922. tmpe->next = newe;
  923. return 0;
  924. policy_read_failed:
  925. assert(newe->string);
  926. log_fn(LOG_WARN,"Couldn't parse line '%s'. Dropping", newe->string);
  927. if(newe->string)
  928. free(newe->string);
  929. if(newe->address)
  930. free(newe->address);
  931. if(newe->port)
  932. free(newe->port);
  933. free(newe);
  934. return -1;
  935. }
  936. /* Return 0 if my exit policy says to allow connection to conn.
  937. * Else return -1.
  938. */
  939. int router_compare_to_exit_policy(connection_t *conn) {
  940. struct exit_policy_t *tmpe;
  941. struct in_addr in;
  942. assert(desc_routerinfo);
  943. for(tmpe=desc_routerinfo->exit_policy; tmpe; tmpe=tmpe->next) {
  944. assert(tmpe->address);
  945. assert(tmpe->port);
  946. log_fn(LOG_DEBUG,"Considering exit policy %s:%s",tmpe->address, tmpe->port);
  947. if(strcmp(tmpe->address,"*") &&
  948. inet_aton(tmpe->address,&in) == 0) { /* malformed IP. reject. */
  949. log_fn(LOG_WARN,"Malformed IP %s in exit policy. Rejecting.",tmpe->address);
  950. return -1;
  951. }
  952. if((!strcmp(tmpe->address,"*") || conn->addr == ntohl(in.s_addr)) &&
  953. (!strcmp(tmpe->port,"*") || atoi(tmpe->port) == conn->port)) {
  954. log_fn(LOG_INFO,"Address '%s' matches '%s' and port '%s' matches '%d'. %s.",
  955. tmpe->address, conn->address,
  956. tmpe->port, conn->port,
  957. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "Accepting" : "Rejecting");
  958. if(tmpe->policy_type == EXIT_POLICY_ACCEPT)
  959. return 0;
  960. else
  961. return -1;
  962. }
  963. }
  964. return 0; /* accept all by default. */
  965. }
  966. const char *router_get_my_descriptor(void) {
  967. if (!desc_routerinfo) {
  968. if (router_rebuild_descriptor())
  969. return NULL;
  970. }
  971. log_fn(LOG_DEBUG,"my desc is '%s'",descriptor);
  972. return descriptor;
  973. }
  974. const routerinfo_t *router_get_desc_routerinfo(void) {
  975. if (!desc_routerinfo) {
  976. if (router_rebuild_descriptor())
  977. return NULL;
  978. }
  979. return desc_routerinfo;
  980. }
  981. int router_rebuild_descriptor(void) {
  982. routerinfo_t *ri;
  983. char localhostname[256];
  984. char *address = options.Address;
  985. if(!address) { /* if not specified in config, we find a default */
  986. if(gethostname(localhostname,sizeof(localhostname)) < 0) {
  987. log_fn(LOG_WARN,"Error obtaining local hostname");
  988. return -1;
  989. }
  990. address = localhostname;
  991. if(!strchr(address,'.')) {
  992. log_fn(LOG_WARN,"fqdn '%s' has only one element. Misconfigured machine?",address);
  993. return -1;
  994. }
  995. }
  996. ri = tor_malloc(sizeof(routerinfo_t));
  997. ri->address = tor_strdup(address);
  998. ri->nickname = tor_strdup(options.Nickname);
  999. /* No need to set addr. */
  1000. ri->or_port = options.ORPort;
  1001. ri->ap_port = options.APPort;
  1002. ri->dir_port = options.DirPort;
  1003. ri->published_on = time(NULL);
  1004. ri->onion_pkey = crypto_pk_dup_key(get_onion_key());
  1005. ri->link_pkey = crypto_pk_dup_key(get_link_key());
  1006. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  1007. ri->bandwidth = options.TotalBandwidth;
  1008. ri->exit_policy = NULL; /* zero it out first */
  1009. router_add_exit_policy_from_config(ri);
  1010. if (desc_routerinfo)
  1011. routerinfo_free(desc_routerinfo);
  1012. desc_routerinfo = ri;
  1013. if (router_dump_router_to_string(descriptor, 8192, ri, get_identity_key())<0) {
  1014. log_fn(LOG_WARN, "Couldn't dump router to string.");
  1015. return -1;
  1016. }
  1017. return 0;
  1018. }
  1019. static void get_platform_str(char *platform, int len)
  1020. {
  1021. snprintf(platform, len-1, "Tor %s on %s", VERSION, get_uname());
  1022. platform[len-1] = '\0';
  1023. return;
  1024. }
  1025. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1026. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  1027. crypto_pk_env_t *ident_key) {
  1028. char *onion_pkey;
  1029. char *link_pkey;
  1030. char *identity_pkey;
  1031. char platform[256];
  1032. char digest[20];
  1033. char signature[128];
  1034. char published[32];
  1035. int onion_pkeylen, link_pkeylen, identity_pkeylen;
  1036. int written;
  1037. int result=0;
  1038. struct exit_policy_t *tmpe;
  1039. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1040. char *s_tmp, *s_dup;
  1041. routerinfo_t *ri_tmp;
  1042. #endif
  1043. get_platform_str(platform, sizeof(platform));
  1044. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  1045. log_fn(LOG_WARN,"Tried to sign a router with a private key that didn't match router's public key!");
  1046. return -1;
  1047. }
  1048. if(crypto_pk_write_public_key_to_string(router->onion_pkey,
  1049. &onion_pkey,&onion_pkeylen)<0) {
  1050. log_fn(LOG_WARN,"write onion_pkey to string failed!");
  1051. return -1;
  1052. }
  1053. if(crypto_pk_write_public_key_to_string(router->identity_pkey,
  1054. &identity_pkey,&identity_pkeylen)<0) {
  1055. log_fn(LOG_WARN,"write identity_pkey to string failed!");
  1056. return -1;
  1057. }
  1058. if(crypto_pk_write_public_key_to_string(router->link_pkey,
  1059. &link_pkey,&link_pkeylen)<0) {
  1060. log_fn(LOG_WARN,"write link_pkey to string failed!");
  1061. return -1;
  1062. }
  1063. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&router->published_on));
  1064. result = snprintf(s, maxlen,
  1065. "router %s %s %d %d %d %d\n"
  1066. "platform %s\n"
  1067. "published %s\n"
  1068. "onion-key\n%s"
  1069. "link-key\n%s"
  1070. "signing-key\n%s",
  1071. router->nickname,
  1072. router->address,
  1073. router->or_port,
  1074. router->ap_port,
  1075. router->dir_port,
  1076. router->bandwidth,
  1077. platform,
  1078. published,
  1079. onion_pkey, link_pkey, identity_pkey);
  1080. free(onion_pkey);
  1081. free(link_pkey);
  1082. free(identity_pkey);
  1083. if(result < 0 || result >= maxlen) {
  1084. /* apparently different glibcs do different things on snprintf error.. so check both */
  1085. return -1;
  1086. }
  1087. written = result;
  1088. for(tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  1089. result = snprintf(s+written, maxlen-written, "%s %s:%s\n",
  1090. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "accept" : "reject",
  1091. tmpe->address, tmpe->port);
  1092. if(result < 0 || result+written > maxlen) {
  1093. /* apparently different glibcs do different things on snprintf error.. so check both */
  1094. return -1;
  1095. }
  1096. written += result;
  1097. }
  1098. if (written > maxlen-256) /* Not enough room for signature. */
  1099. return -1;
  1100. strcat(s+written, "router-signature\n");
  1101. written += strlen(s+written);
  1102. s[written] = '\0';
  1103. if (router_get_router_hash(s, digest) < 0)
  1104. return -1;
  1105. if (crypto_pk_private_sign(ident_key, digest, 20, signature) < 0) {
  1106. log_fn(LOG_WARN, "Error signing digest");
  1107. return -1;
  1108. }
  1109. strcat(s+written, "-----BEGIN SIGNATURE-----\n");
  1110. written += strlen(s+written);
  1111. if (base64_encode(s+written, maxlen-written, signature, 128) < 0) {
  1112. log_fn(LOG_WARN, "Couldn't base64-encode signature");
  1113. return -1;
  1114. }
  1115. written += strlen(s+written);
  1116. strcat(s+written, "-----END SIGNATURE-----\n");
  1117. written += strlen(s+written);
  1118. if (written > maxlen-2)
  1119. return -1;
  1120. /* include a last '\n' */
  1121. s[written] = '\n';
  1122. s[written+1] = 0;
  1123. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1124. s_tmp = s_dup = tor_strdup(s);
  1125. ri_tmp = router_get_entry_from_string(&s_tmp);
  1126. if (!ri_tmp) {
  1127. log_fn(LOG_ERR, "We just generated a router descriptor we can't parse: <<%s>>",
  1128. s);
  1129. return -1;
  1130. }
  1131. free(s_dup);
  1132. routerinfo_free(ri_tmp);
  1133. #endif
  1134. return written+1;
  1135. }
  1136. /*
  1137. Local Variables:
  1138. mode:c
  1139. indent-tabs-mode:nil
  1140. c-basic-offset:2
  1141. End:
  1142. */