routers.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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_WARNING, "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_WARNING,"Failed to load routerfile %s.",routerfile);
  159. return -1;
  160. }
  161. if(router_get_list_from_string(string) < 0) {
  162. log_fn(LOG_WARNING,"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_WARNING, "Error parsing router file");
  403. return -1;
  404. }
  405. if (router_resolve_directory(directory)) {
  406. log(LOG_WARNING, "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_WARNING,"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_WARNING,"couldn't find \"%s\"",end_str);
  423. return -1;
  424. }
  425. end = strchr(end, '\n');
  426. if (!end) {
  427. log_fn(LOG_WARNING,"couldn't find EOL");
  428. return -1;
  429. }
  430. ++end;
  431. if (crypto_SHA_digest(start, end-start, digest)) {
  432. log_fn(LOG_WARNING,"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_WARNING, "Couldn't parse directory.");
  467. return -1;
  468. }
  469. if (router_resolve_directory(directory)) {
  470. log_fn(LOG_WARNING, "Error resolving directory");
  471. return -1;
  472. }
  473. if (compare_recommended_versions(VERSION, directory->software_versions) < 0) {
  474. log(LOG_WARNING, "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_WARNING, "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_WARNING, "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_WARNING, "Error reading directory: expected %s", name);\
  508. return -1; \
  509. } } while(0)
  510. if (router_get_dir_hash(s, digest)) {
  511. log_fn(LOG_WARNING, "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_WARNING, "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_WARNING, "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_WARNING, "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_WARNING, "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_WARNING, "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_WARNING, "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_WARNING, "Error reading router");
  599. return -1;
  600. }
  601. if (rarray_len >= MAX_ROUTERS_IN_DIR) {
  602. log_fn(LOG_WARNING, "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_WARNING,"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_WARNING, "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_WARNING, "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_WARNING, "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_WARNING,"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_WARNING,"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_WARNING,"Router nickname too long.");
  707. goto err;
  708. }
  709. if (strspn(router->nickname, LEGAL_NICKNAME_CHARACTERS) !=
  710. strlen(router->nickname)) {
  711. log_fn(LOG_WARNING, "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_WARNING,"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_WARNING,"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_WARNING, "Missing published time"); goto err;
  742. }
  743. if (tok->val.cmd.n_args != 2) {
  744. log_fn(LOG_WARNING, "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_WARNING, "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_WARNING, "Missing onion-key"); goto err;
  754. }
  755. NEXT_TOKEN();
  756. if (tok->tp != _PUBLIC_KEY) {
  757. log_fn(LOG_WARNING, "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_WARNING, "Missing link-key"); goto err;
  763. }
  764. NEXT_TOKEN();
  765. if (tok->tp != _PUBLIC_KEY) {
  766. log_fn(LOG_WARNING, "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_WARNING, "Missing signing-key"); goto err;
  772. }
  773. NEXT_TOKEN();
  774. if (tok->tp != _PUBLIC_KEY) {
  775. log_fn(LOG_WARNING, "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_WARNING,"Missing router signature");
  785. goto err;
  786. }
  787. NEXT_TOKEN();
  788. if (tok->tp != _SIGNATURE) {
  789. log_fn(LOG_WARNING,"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_WARNING, "Invalid signature %d",t);
  796. goto err;
  797. }
  798. if (memcmp(digest, signed_digest, 20)) {
  799. log_fn(LOG_WARNING, "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_WARNING,"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_WARNING, "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_WARNING, "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_WARNING,"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_WARNING,"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_WARNING,"Error obtaining local hostname");
  988. return -1;
  989. }
  990. address = localhostname;
  991. }
  992. ri = tor_malloc(sizeof(routerinfo_t));
  993. ri->address = tor_strdup(address);
  994. ri->nickname = tor_strdup(options.Nickname);
  995. /* No need to set addr. */
  996. ri->or_port = options.ORPort;
  997. ri->ap_port = options.APPort;
  998. ri->dir_port = options.DirPort;
  999. ri->published_on = time(NULL);
  1000. ri->onion_pkey = crypto_pk_dup_key(get_onion_key());
  1001. ri->link_pkey = crypto_pk_dup_key(get_link_key());
  1002. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  1003. ri->bandwidth = options.TotalBandwidth;
  1004. ri->exit_policy = NULL; /* zero it out first */
  1005. router_add_exit_policy_from_config(ri);
  1006. if (desc_routerinfo)
  1007. routerinfo_free(desc_routerinfo);
  1008. desc_routerinfo = ri;
  1009. if (router_dump_router_to_string(descriptor, 8192, ri, get_identity_key())<0) {
  1010. log_fn(LOG_WARNING, "Couldn't dump router to string.");
  1011. return -1;
  1012. }
  1013. return 0;
  1014. }
  1015. static void get_platform_str(char *platform, int len)
  1016. {
  1017. snprintf(platform, len-1, "Tor %s on %s", VERSION, get_uname());
  1018. platform[len-1] = '\0';
  1019. return;
  1020. }
  1021. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1022. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  1023. crypto_pk_env_t *ident_key) {
  1024. char *onion_pkey;
  1025. char *link_pkey;
  1026. char *identity_pkey;
  1027. char platform[256];
  1028. char digest[20];
  1029. char signature[128];
  1030. char published[32];
  1031. int onion_pkeylen, link_pkeylen, identity_pkeylen;
  1032. int written;
  1033. int result=0;
  1034. struct exit_policy_t *tmpe;
  1035. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1036. char *s_tmp, *s_dup;
  1037. routerinfo_t *ri_tmp;
  1038. #endif
  1039. get_platform_str(platform, sizeof(platform));
  1040. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  1041. log_fn(LOG_WARNING,"Tried to sign a router with a private key that didn't match router's public key!");
  1042. return -1;
  1043. }
  1044. if(crypto_pk_write_public_key_to_string(router->onion_pkey,
  1045. &onion_pkey,&onion_pkeylen)<0) {
  1046. log_fn(LOG_WARNING,"write onion_pkey to string failed!");
  1047. return -1;
  1048. }
  1049. if(crypto_pk_write_public_key_to_string(router->identity_pkey,
  1050. &identity_pkey,&identity_pkeylen)<0) {
  1051. log_fn(LOG_WARNING,"write identity_pkey to string failed!");
  1052. return -1;
  1053. }
  1054. if(crypto_pk_write_public_key_to_string(router->link_pkey,
  1055. &link_pkey,&link_pkeylen)<0) {
  1056. log_fn(LOG_WARNING,"write link_pkey to string failed!");
  1057. return -1;
  1058. }
  1059. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&router->published_on));
  1060. result = snprintf(s, maxlen,
  1061. "router %s %s %d %d %d %d\n"
  1062. "platform %s\n"
  1063. "published %s\n"
  1064. "onion-key\n%s"
  1065. "link-key\n%s"
  1066. "signing-key\n%s",
  1067. router->nickname,
  1068. router->address,
  1069. router->or_port,
  1070. router->ap_port,
  1071. router->dir_port,
  1072. router->bandwidth,
  1073. platform,
  1074. published,
  1075. onion_pkey, link_pkey, identity_pkey);
  1076. free(onion_pkey);
  1077. free(link_pkey);
  1078. free(identity_pkey);
  1079. if(result < 0 || result >= maxlen) {
  1080. /* apparently different glibcs do different things on snprintf error.. so check both */
  1081. return -1;
  1082. }
  1083. written = result;
  1084. for(tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  1085. result = snprintf(s+written, maxlen-written, "%s %s:%s\n",
  1086. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "accept" : "reject",
  1087. tmpe->address, tmpe->port);
  1088. if(result < 0 || result+written > maxlen) {
  1089. /* apparently different glibcs do different things on snprintf error.. so check both */
  1090. return -1;
  1091. }
  1092. written += result;
  1093. }
  1094. if (written > maxlen-256) /* Not enough room for signature. */
  1095. return -1;
  1096. strcat(s+written, "router-signature\n");
  1097. written += strlen(s+written);
  1098. s[written] = '\0';
  1099. if (router_get_router_hash(s, digest) < 0)
  1100. return -1;
  1101. if (crypto_pk_private_sign(ident_key, digest, 20, signature) < 0) {
  1102. log_fn(LOG_WARNING, "Error signing digest");
  1103. return -1;
  1104. }
  1105. strcat(s+written, "-----BEGIN SIGNATURE-----\n");
  1106. written += strlen(s+written);
  1107. if (base64_encode(s+written, maxlen-written, signature, 128) < 0) {
  1108. log_fn(LOG_WARNING, "Couldn't base64-encode signature");
  1109. return -1;
  1110. }
  1111. written += strlen(s+written);
  1112. strcat(s+written, "-----END SIGNATURE-----\n");
  1113. written += strlen(s+written);
  1114. if (written > maxlen-2)
  1115. return -1;
  1116. /* include a last '\n' */
  1117. s[written] = '\n';
  1118. s[written+1] = 0;
  1119. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1120. s_tmp = s_dup = tor_strdup(s);
  1121. ri_tmp = router_get_entry_from_string(&s_tmp);
  1122. if (!ri_tmp) {
  1123. log_fn(LOG_ERR, "We just generated a router descriptor we can't parse: <<%s>>",
  1124. s);
  1125. return -1;
  1126. }
  1127. free(s_dup);
  1128. routerinfo_free(ri_tmp);
  1129. #endif
  1130. return written+1;
  1131. }
  1132. /*
  1133. Local Variables:
  1134. mode:c
  1135. indent-tabs-mode:nil
  1136. c-basic-offset:2
  1137. End:
  1138. */