util.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  1. /* Copyright 2003 Roger Dingledine
  2. * Copyright 2004-2006 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char util_c_id[] = "$Id$";
  6. /**
  7. * \file util.c
  8. * \brief Common functions for strings, IO, network, data structures,
  9. * process control.
  10. **/
  11. /* This is required on rh7 to make strptime not complain.
  12. */
  13. #define _GNU_SOURCE
  14. #include "orconfig.h"
  15. #include "util.h"
  16. #include "log.h"
  17. #include "crypto.h"
  18. #include "torint.h"
  19. #include "container.h"
  20. #ifdef MS_WINDOWS
  21. #include <io.h>
  22. #include <direct.h>
  23. #else
  24. #include <dirent.h>
  25. #include <pwd.h>
  26. #endif
  27. #ifdef HAVE_CTYPE_H
  28. #include <ctype.h>
  29. #endif
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #ifdef HAVE_NETINET_IN_H
  35. #include <netinet/in.h>
  36. #endif
  37. #ifdef HAVE_ARPA_INET_H
  38. #include <arpa/inet.h>
  39. #endif
  40. #ifdef HAVE_ERRNO_H
  41. #include <errno.h>
  42. #endif
  43. #ifdef HAVE_SYS_SOCKET_H
  44. #include <sys/socket.h>
  45. #endif
  46. #ifdef HAVE_SYS_TIME_H
  47. #include <sys/time.h>
  48. #endif
  49. #ifdef HAVE_UNISTD_H
  50. #include <unistd.h>
  51. #endif
  52. #ifdef HAVE_SYS_STAT_H
  53. #include <sys/stat.h>
  54. #endif
  55. #ifdef HAVE_SYS_FCNTL_H
  56. #include <sys/fcntl.h>
  57. #endif
  58. #ifdef HAVE_FCNTL_H
  59. #include <fcntl.h>
  60. #endif
  61. #ifdef HAVE_TIME_H
  62. #include <time.h>
  63. #endif
  64. #ifndef O_BINARY
  65. #define O_BINARY 0
  66. #endif
  67. #ifndef O_TEXT
  68. #define O_TEXT 0
  69. #endif
  70. /* =====
  71. * Memory management
  72. * ===== */
  73. #ifdef USE_DMALLOC
  74. #include <dmalloc.h>
  75. #define DMALLOC_FN_ARGS , file, line
  76. #else
  77. #define dmalloc_strdup(file, line, string, xalloc_b) strdup(string)
  78. #define dmalloc_malloc(file, line, size, func_id, alignment, xalloc_b) \
  79. malloc(size)
  80. #define DMALLOC_FUNC_MALLOC 0
  81. #define dmalloc_realloc(file, line, old_pnt, new_size, func_id, xalloc_b) \
  82. realloc((old_pnt), (new_size))
  83. #define DMALLOC_FUNC_REALLOC 0
  84. #define DMALLOC_FN_ARGS
  85. #endif
  86. /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
  87. * result. On error, log and terminate the process. (Same as malloc(size),
  88. * but never returns NULL.)
  89. *
  90. * <b>file</b> and <b>line</b> are used if dmalloc is enabled, and
  91. * ignored otherwise.
  92. */
  93. void *
  94. _tor_malloc(size_t size DMALLOC_PARAMS)
  95. {
  96. void *result;
  97. /* Some libcs don't do the right thing on size==0. Override them. */
  98. if (size==0) {
  99. size=1;
  100. }
  101. result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
  102. if (!result) {
  103. log_err(LD_MM,"Out of memory. Dying.");
  104. /* If these functions die within a worker process, they won't call
  105. * spawn_exit, but that's ok, since the parent will run out of memory soon
  106. * anyway. */
  107. exit(1);
  108. }
  109. return result;
  110. }
  111. /* Allocate a chunk of <b>size</b> bytes of memory, fill the memory with
  112. * zero bytes, and return a pointer to the result. Log and terminate
  113. * the process on error. (Same as calloc(size,1), but never returns NULL.)
  114. */
  115. void *
  116. _tor_malloc_zero(size_t size DMALLOC_PARAMS)
  117. {
  118. void *result = _tor_malloc(size DMALLOC_FN_ARGS);
  119. memset(result, 0, size);
  120. return result;
  121. }
  122. /** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
  123. * bytes long; return the new memory block. On error, log and
  124. * terminate. (Like realloc(ptr,size), but never returns NULL.)
  125. */
  126. void *
  127. _tor_realloc(void *ptr, size_t size DMALLOC_PARAMS)
  128. {
  129. void *result;
  130. result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
  131. if (!result) {
  132. log_err(LD_MM,"Out of memory. Dying.");
  133. exit(1);
  134. }
  135. return result;
  136. }
  137. /** Return a newly allocated copy of the NUL-terminated string s. On
  138. * error, log and terminate. (Like strdup(s), but never returns
  139. * NULL.)
  140. */
  141. char *
  142. _tor_strdup(const char *s DMALLOC_PARAMS)
  143. {
  144. char *dup;
  145. tor_assert(s);
  146. dup = dmalloc_strdup(file, line, s, 0);
  147. if (!dup) {
  148. log_err(LD_MM,"Out of memory. Dying.");
  149. exit(1);
  150. }
  151. return dup;
  152. }
  153. /** Allocate and return a new string containing the first <b>n</b>
  154. * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
  155. * characters, only the first <b>n</b> are copied. The result is
  156. * always NUL-terminated. (Like strndup(s,n), but never returns
  157. * NULL.)
  158. */
  159. char *
  160. _tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
  161. {
  162. char *dup;
  163. tor_assert(s);
  164. dup = _tor_malloc((n+1) DMALLOC_FN_ARGS);
  165. /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
  166. * this function gets called a whole lot, and platform strncpy is
  167. * much faster than strlcpy when strlen(s) is much longer than n.
  168. */
  169. strncpy(dup, s, n);
  170. dup[n]='\0';
  171. return dup;
  172. }
  173. /** Allocate a chunk of <b>len</b> bytes, with the same contents starting at
  174. * <b>mem</b>. */
  175. void *
  176. _tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
  177. {
  178. char *dup;
  179. tor_assert(mem);
  180. dup = _tor_malloc(len DMALLOC_FN_ARGS);
  181. memcpy(dup, mem, len);
  182. return dup;
  183. }
  184. /* =====
  185. * String manipulation
  186. * ===== */
  187. /** Remove from the string <b>s</b> every character which appears in
  188. * <b>strip</b>. Return the number of characters removed. */
  189. int
  190. tor_strstrip(char *s, const char *strip)
  191. {
  192. char *read = s;
  193. while (*read) {
  194. if (strchr(strip, *read)) {
  195. ++read;
  196. } else {
  197. *s++ = *read++;
  198. }
  199. }
  200. *s = '\0';
  201. return read-s;
  202. }
  203. /** Set the <b>dest_len</b>-byte buffer <b>buf</b> to contain the
  204. * string <b>s</b>, with the string <b>insert</b> inserted after every
  205. * <b>n</b> characters. Return 0 on success, -1 on failure.
  206. *
  207. * If <b>rule</b> is ALWAYS_TERMINATE, then always end the string with
  208. * <b>insert</b>, even if its length is not a multiple of <b>n</b>. If
  209. * <b>rule</b> is NEVER_TERMINATE, then never end the string with
  210. * <b>insert</b>, even if its length <i>is</i> a multiple of <b>n</b>.
  211. * If <b>rule</b> is TERMINATE_IF_EVEN, then end the string with <b>insert</b>
  212. * exactly when its length <i>is</i> a multiple of <b>n</b>.
  213. */
  214. int
  215. tor_strpartition(char *dest, size_t dest_len,
  216. const char *s, const char *insert, size_t n,
  217. part_finish_rule_t rule)
  218. {
  219. char *destp;
  220. size_t len_in, len_out, len_ins;
  221. int is_even, remaining;
  222. tor_assert(s);
  223. tor_assert(insert);
  224. tor_assert(n > 0);
  225. tor_assert(n < SIZE_T_CEILING);
  226. tor_assert(dest_len < SIZE_T_CEILING);
  227. len_in = strlen(s);
  228. len_ins = strlen(insert);
  229. tor_assert(len_in < SIZE_T_CEILING);
  230. tor_assert(len_in/n < SIZE_T_CEILING/len_ins); /* avoid overflow */
  231. len_out = len_in + (len_in/n)*len_ins;
  232. is_even = (len_in%n) == 0;
  233. switch (rule)
  234. {
  235. case ALWAYS_TERMINATE:
  236. if (!is_even) len_out += len_ins;
  237. break;
  238. case NEVER_TERMINATE:
  239. if (is_even && len_in) len_out -= len_ins;
  240. break;
  241. case TERMINATE_IF_EVEN:
  242. break;
  243. }
  244. if (dest_len < len_out+1)
  245. return -1;
  246. destp = dest;
  247. remaining = len_in;
  248. while (remaining) {
  249. strncpy(destp, s, n);
  250. remaining -= n;
  251. if (remaining < 0) {
  252. if (rule == ALWAYS_TERMINATE)
  253. strcpy(destp+n+remaining,insert);
  254. break;
  255. } else if (remaining == 0 && rule == NEVER_TERMINATE) {
  256. *(destp+n) = '\0';
  257. break;
  258. }
  259. strcpy(destp+n, insert);
  260. s += n;
  261. destp += n+len_ins;
  262. }
  263. tor_assert(len_out == strlen(dest));
  264. return 0;
  265. }
  266. /** Return a pointer to a NUL-terminated hexadecimal string encoding
  267. * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
  268. * result does not need to be deallocated, but repeated calls to
  269. * hex_str will trash old results.
  270. */
  271. const char *
  272. hex_str(const char *from, size_t fromlen)
  273. {
  274. static char buf[65];
  275. if (fromlen>(sizeof(buf)-1)/2)
  276. fromlen = (sizeof(buf)-1)/2;
  277. base16_encode(buf,sizeof(buf),from,fromlen);
  278. return buf;
  279. }
  280. /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
  281. * lowercase. */
  282. void
  283. tor_strlower(char *s)
  284. {
  285. while (*s) {
  286. *s = TOR_TOLOWER(*s);
  287. ++s;
  288. }
  289. }
  290. /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
  291. * lowercase. */
  292. void
  293. tor_strupper(char *s)
  294. {
  295. while (*s) {
  296. *s = TOR_TOUPPER(*s);
  297. ++s;
  298. }
  299. }
  300. /** Return 1 if every character in <b>s</b> is printable, else return 0.
  301. */
  302. int
  303. tor_strisprint(const char *s)
  304. {
  305. while (*s) {
  306. if (!TOR_ISPRINT(*s))
  307. return 0;
  308. s++;
  309. }
  310. return 1;
  311. }
  312. /** Return 1 if no character in <b>s</b> is uppercase, else return 0.
  313. */
  314. int
  315. tor_strisnonupper(const char *s)
  316. {
  317. while (*s) {
  318. if (TOR_ISUPPER(*s))
  319. return 0;
  320. s++;
  321. }
  322. return 1;
  323. }
  324. /* Compares the first strlen(s2) characters of s1 with s2. Returns as for
  325. * strcmp.
  326. */
  327. int
  328. strcmpstart(const char *s1, const char *s2)
  329. {
  330. size_t n = strlen(s2);
  331. return strncmp(s1, s2, n);
  332. }
  333. /* Compares the first strlen(s2) characters of s1 with s2. Returns as for
  334. * strcasecmp.
  335. */
  336. int
  337. strcasecmpstart(const char *s1, const char *s2)
  338. {
  339. size_t n = strlen(s2);
  340. return strncasecmp(s1, s2, n);
  341. }
  342. /* Compares the last strlen(s2) characters of s1 with s2. Returns as for
  343. * strcmp.
  344. */
  345. int
  346. strcmpend(const char *s1, const char *s2)
  347. {
  348. size_t n1 = strlen(s1), n2 = strlen(s2);
  349. if (n2>n1)
  350. return strcmp(s1,s2);
  351. else
  352. return strncmp(s1+(n1-n2), s2, n2);
  353. }
  354. /* Compares the last strlen(s2) characters of s1 with s2. Returns as for
  355. * strcasecmp.
  356. */
  357. int
  358. strcasecmpend(const char *s1, const char *s2)
  359. {
  360. size_t n1 = strlen(s1), n2 = strlen(s2);
  361. if (n2>n1) /* then they can't be the same; figure out which is bigger */
  362. return strcasecmp(s1,s2);
  363. else
  364. return strncasecmp(s1+(n1-n2), s2, n2);
  365. }
  366. /** Return a pointer to the first char of s that is not whitespace and
  367. * not a comment, or to the terminating NUL if no such character exists.
  368. */
  369. const char *
  370. eat_whitespace(const char *s)
  371. {
  372. tor_assert(s);
  373. while (TOR_ISSPACE(*s) || *s == '#') {
  374. while (TOR_ISSPACE(*s))
  375. s++;
  376. if (*s == '#') { /* read to a \n or \0 */
  377. while (*s && *s != '\n')
  378. s++;
  379. if (!*s)
  380. return s;
  381. }
  382. }
  383. return s;
  384. }
  385. /** Return a pointer to the first char of s that is not a space or a tab,
  386. * or to the terminating NUL if no such character exists. */
  387. const char *
  388. eat_whitespace_no_nl(const char *s)
  389. {
  390. while (*s == ' ' || *s == '\t')
  391. ++s;
  392. return s;
  393. }
  394. /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
  395. * or to the terminating NUL if no such character exists.
  396. */
  397. const char *
  398. find_whitespace(const char *s)
  399. {
  400. /* tor_assert(s); */
  401. while (1) {
  402. switch (*s)
  403. {
  404. case '\0':
  405. case '#':
  406. case ' ':
  407. case '\r':
  408. case '\n':
  409. case '\t':
  410. return s;
  411. default:
  412. ++s;
  413. }
  414. }
  415. }
  416. /** Return true iff the 'len' bytes at 'mem' are all zero. */
  417. int
  418. tor_mem_is_zero(const char *mem, size_t len)
  419. {
  420. static const char ZERO[] = {
  421. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
  422. };
  423. while (len >= sizeof(ZERO)) {
  424. if (memcmp(mem, ZERO, sizeof(ZERO)))
  425. return 0;
  426. len -= sizeof(ZERO);
  427. mem += sizeof(ZERO);
  428. }
  429. /* Deal with leftover bytes. */
  430. if (len)
  431. return ! memcmp(mem, ZERO, len);
  432. return 1;
  433. }
  434. /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
  435. int
  436. tor_digest_is_zero(const char *digest)
  437. {
  438. return tor_mem_is_zero(digest, DIGEST_LEN);
  439. }
  440. #define CHECK_STRTOX_RESULT() \
  441. /* Was at least one character converted? */ \
  442. if (endptr == s) \
  443. goto err; \
  444. /* Were there unexpected unconverted characters? */ \
  445. if (!next && *endptr) \
  446. goto err; \
  447. /* Is r within limits? */ \
  448. if (r < min || r > max) \
  449. goto err; \
  450. if (ok) *ok = 1; \
  451. if (next) *next = endptr; \
  452. return r; \
  453. err: \
  454. if (ok) *ok = 0; \
  455. if (next) *next = endptr; \
  456. return 0
  457. /** Extract a long from the start of s, in the given numeric base. If
  458. * there is unconverted data and next is provided, set *next to the
  459. * first unconverted character. An error has occurred if no characters
  460. * are converted; or if there are unconverted characters and next is NULL; or
  461. * if the parsed value is not between min and max. When no error occurs,
  462. * return the parsed value and set *ok (if provided) to 1. When an error
  463. * occurs, return 0 and set *ok (if provided) to 0.
  464. */
  465. long
  466. tor_parse_long(const char *s, int base, long min, long max,
  467. int *ok, char **next)
  468. {
  469. char *endptr;
  470. long r;
  471. r = strtol(s, &endptr, base);
  472. CHECK_STRTOX_RESULT();
  473. }
  474. unsigned long
  475. tor_parse_ulong(const char *s, int base, unsigned long min,
  476. unsigned long max, int *ok, char **next)
  477. {
  478. char *endptr;
  479. unsigned long r;
  480. r = strtoul(s, &endptr, base);
  481. CHECK_STRTOX_RESULT();
  482. }
  483. /** Only base 10 is guaranteed to work for now. */
  484. uint64_t
  485. tor_parse_uint64(const char *s, int base, uint64_t min,
  486. uint64_t max, int *ok, char **next)
  487. {
  488. char *endptr;
  489. uint64_t r;
  490. #ifdef HAVE_STRTOULL
  491. r = (uint64_t)strtoull(s, &endptr, base);
  492. #elif defined(MS_WINDOWS)
  493. #if _MSC_VER < 1300
  494. tor_assert(base <= 10);
  495. r = (uint64_t)_atoi64(s);
  496. endptr = (char*)s;
  497. while (TOR_ISSPACE(*endptr)) endptr++;
  498. while (TOR_ISDIGIT(*endptr)) endptr++;
  499. #else
  500. r = (uint64_t)_strtoui64(s, &endptr, base);
  501. #endif
  502. #elif SIZEOF_LONG == 8
  503. r = (uint64_t)strtoul(s, &endptr, base);
  504. #else
  505. #error "I don't know how to parse 64-bit numbers."
  506. #endif
  507. CHECK_STRTOX_RESULT();
  508. }
  509. void
  510. base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
  511. {
  512. const char *end;
  513. char *cp;
  514. tor_assert(destlen >= srclen*2+1);
  515. tor_assert(destlen < SIZE_T_CEILING);
  516. cp = dest;
  517. end = src+srclen;
  518. while (src<end) {
  519. sprintf(cp,"%02X",*(const uint8_t*)src);
  520. ++src;
  521. cp += 2;
  522. }
  523. *cp = '\0';
  524. }
  525. static const char HEX_DIGITS[] = "0123456789ABCDEFabcdef";
  526. static INLINE int
  527. hex_decode_digit(char c)
  528. {
  529. const char *cp;
  530. int n;
  531. cp = strchr(HEX_DIGITS, c);
  532. if (!cp)
  533. return -1;
  534. n = cp-HEX_DIGITS;
  535. if (n<=15)
  536. return n; /* digit or uppercase */
  537. else
  538. return n-6; /* lowercase */
  539. }
  540. int
  541. base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
  542. {
  543. const char *end;
  544. int v1,v2;
  545. if ((srclen % 2) != 0)
  546. return -1;
  547. if (destlen < srclen/2 || destlen > SIZE_T_CEILING)
  548. return -1;
  549. end = src+srclen;
  550. while (src<end) {
  551. v1 = hex_decode_digit(*src);
  552. v2 = hex_decode_digit(*(src+1));
  553. if (v1<0||v2<0)
  554. return -1;
  555. *(uint8_t*)dest = (v1<<4)|v2;
  556. ++dest;
  557. src+=2;
  558. }
  559. return 0;
  560. }
  561. /** Allocate and return a new string representing the contents of <b>s</b>,
  562. * surrounded by quotes and using standard C escapes.
  563. *
  564. * Generally, we use this for logging values that come in over the network
  565. * to keep them from tricking users.
  566. *
  567. * We trust values from the resolver, OS, configuration file, and command line
  568. * to not be maliciously ill-formed. We validate incoming routerdescs and
  569. * SOCKS requests and addresses from BEGIN cells as they're parsed;
  570. * afterwards, we trust them as non-malicious.
  571. */
  572. char *
  573. esc_for_log(const char *s)
  574. {
  575. const char *cp;
  576. char *result, *outp;
  577. size_t len = 3;
  578. if (!s) {
  579. return tor_strdup("");
  580. }
  581. for (cp = s; *cp; ++cp) {
  582. switch (*cp) {
  583. case '\\':
  584. case '\"':
  585. case '\'':
  586. len += 2;
  587. break;
  588. default:
  589. if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
  590. ++len;
  591. else
  592. len += 4;
  593. break;
  594. }
  595. }
  596. result = outp = tor_malloc(len);
  597. *outp++ = '\"';
  598. for (cp = s; *cp; ++cp) {
  599. switch (*cp) {
  600. case '\\':
  601. case '\"':
  602. case '\'':
  603. *outp++ = '\\';
  604. *outp++ = *cp;
  605. break;
  606. case '\n':
  607. *outp++ = '\\';
  608. *outp++ = 'n';
  609. break;
  610. case '\t':
  611. *outp++ = '\\';
  612. *outp++ = 't';
  613. break;
  614. case '\r':
  615. *outp++ = '\\';
  616. *outp++ = 'r';
  617. break;
  618. default:
  619. if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
  620. *outp++ = *cp;
  621. } else {
  622. tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
  623. outp += 4;
  624. }
  625. break;
  626. }
  627. }
  628. *outp++ = '\"';
  629. *outp++ = 0;
  630. return result;
  631. }
  632. /** Allocate and return a new string representing the contents of <b>s</b>,
  633. * surrounded by quotes and using standard C escapes.
  634. *
  635. * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
  636. * thread. Also, each call invalidates the last-returned value, so don't
  637. * try log_warn(LD_GENERAL, "%s %s", escaped(a), escaped(b));
  638. */
  639. const char *
  640. escaped(const char *s)
  641. {
  642. static char *_escaped_val = NULL;
  643. if (_escaped_val)
  644. tor_free(_escaped_val);
  645. if (s)
  646. _escaped_val = esc_for_log(s);
  647. else
  648. _escaped_val = NULL;
  649. return _escaped_val;
  650. }
  651. /* =====
  652. * Time
  653. * ===== */
  654. /** Return the number of microseconds elapsed between *start and *end.
  655. */
  656. long
  657. tv_udiff(struct timeval *start, struct timeval *end)
  658. {
  659. long udiff;
  660. long secdiff = end->tv_sec - start->tv_sec;
  661. if (labs(secdiff+1) > LONG_MAX/1000000) {
  662. log_warn(LD_GENERAL, "comparing times too far apart.");
  663. return LONG_MAX;
  664. }
  665. udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
  666. return udiff;
  667. }
  668. /** Return -1 if *a \< *b, 0 if *a==*b, and 1 if *a \> *b.
  669. */
  670. int
  671. tv_cmp(struct timeval *a, struct timeval *b)
  672. {
  673. if (a->tv_sec > b->tv_sec)
  674. return 1;
  675. if (a->tv_sec < b->tv_sec)
  676. return -1;
  677. if (a->tv_usec > b->tv_usec)
  678. return 1;
  679. if (a->tv_usec < b->tv_usec)
  680. return -1;
  681. return 0;
  682. }
  683. /** Increment *a by the number of seconds and microseconds in *b.
  684. */
  685. void
  686. tv_add(struct timeval *a, struct timeval *b)
  687. {
  688. a->tv_usec += b->tv_usec;
  689. a->tv_sec += b->tv_sec + (a->tv_usec / 1000000);
  690. a->tv_usec %= 1000000;
  691. }
  692. /** Increment *a by <b>ms</b> milliseconds.
  693. */
  694. void
  695. tv_addms(struct timeval *a, long ms)
  696. {
  697. a->tv_usec += (ms * 1000) % 1000000;
  698. a->tv_sec += ((ms * 1000) / 1000000) + (a->tv_usec / 1000000);
  699. a->tv_usec %= 1000000;
  700. }
  701. #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
  702. static int
  703. n_leapdays(int y1, int y2)
  704. {
  705. --y1;
  706. --y2;
  707. return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
  708. }
  709. /** Number of days per month in non-leap year; used by tor_timegm. */
  710. static const int days_per_month[] =
  711. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  712. /** Return a time_t given a struct tm. The result is given in GMT, and
  713. * does not account for leap seconds.
  714. */
  715. time_t
  716. tor_timegm(struct tm *tm)
  717. {
  718. /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
  719. * It's way more brute-force than fiddling with tzset().
  720. */
  721. time_t ret;
  722. unsigned long year, days, hours, minutes;
  723. int i;
  724. year = tm->tm_year + 1900;
  725. if (year < 1970 || tm->tm_mon < 0 || tm->tm_mon > 11) {
  726. log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
  727. return -1;
  728. }
  729. days = 365 * (year-1970) + n_leapdays(1970,year);
  730. for (i = 0; i < tm->tm_mon; ++i)
  731. days += days_per_month[i];
  732. if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
  733. ++days;
  734. days += tm->tm_mday - 1;
  735. hours = days*24 + tm->tm_hour;
  736. minutes = hours*60 + tm->tm_min;
  737. ret = minutes*60 + tm->tm_sec;
  738. return ret;
  739. }
  740. /* strftime is locale-specific, so we need to replace those parts */
  741. static const char *WEEKDAY_NAMES[] =
  742. { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  743. static const char *MONTH_NAMES[] =
  744. { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  745. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  746. void
  747. format_rfc1123_time(char *buf, time_t t)
  748. {
  749. struct tm tm;
  750. tor_gmtime_r(&t, &tm);
  751. strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
  752. tor_assert(tm.tm_wday >= 0);
  753. tor_assert(tm.tm_wday <= 6);
  754. memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
  755. tor_assert(tm.tm_wday >= 0);
  756. tor_assert(tm.tm_mon <= 11);
  757. memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
  758. }
  759. int
  760. parse_rfc1123_time(const char *buf, time_t *t)
  761. {
  762. struct tm tm;
  763. char month[4];
  764. char weekday[4];
  765. int i, m;
  766. if (strlen(buf) != RFC1123_TIME_LEN)
  767. return -1;
  768. memset(&tm, 0, sizeof(tm));
  769. if (sscanf(buf, "%3s, %d %3s %d %d:%d:%d GMT", weekday,
  770. &tm.tm_mday, month, &tm.tm_year, &tm.tm_hour,
  771. &tm.tm_min, &tm.tm_sec) < 7) {
  772. char *esc = esc_for_log(buf);
  773. log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
  774. tor_free(esc);
  775. return -1;
  776. }
  777. m = -1;
  778. for (i = 0; i < 12; ++i) {
  779. if (!strcmp(month, MONTH_NAMES[i])) {
  780. m = i;
  781. break;
  782. }
  783. }
  784. if (m<0) {
  785. char *esc = esc_for_log(buf);
  786. log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
  787. tor_free(esc);
  788. return -1;
  789. }
  790. tm.tm_mon = m;
  791. if (tm.tm_year < 1970) {
  792. char *esc = esc_for_log(buf);
  793. log_warn(LD_GENERAL,
  794. "Got invalid RFC1123 time %s. (Before 1970)", esc);
  795. tor_free(esc);
  796. return -1;
  797. }
  798. tm.tm_year -= 1900;
  799. *t = tor_timegm(&tm);
  800. return 0;
  801. }
  802. void
  803. format_local_iso_time(char *buf, time_t t)
  804. {
  805. struct tm tm;
  806. strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
  807. }
  808. void
  809. format_iso_time(char *buf, time_t t)
  810. {
  811. struct tm tm;
  812. strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
  813. }
  814. int
  815. parse_iso_time(const char *cp, time_t *t)
  816. {
  817. struct tm st_tm;
  818. #ifdef HAVE_STRPTIME
  819. if (!strptime(cp, "%Y-%m-%d %H:%M:%S", &st_tm)) {
  820. log_warn(LD_GENERAL, "Published time was unparseable"); return -1;
  821. }
  822. #else
  823. unsigned int year=0, month=0, day=0, hour=100, minute=100, second=100;
  824. if (sscanf(cp, "%u-%u-%u %u:%u:%u", &year, &month,
  825. &day, &hour, &minute, &second) < 6) {
  826. log_warn(LD_GENERAL, "Published time was unparseable"); return -1;
  827. }
  828. if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
  829. hour > 23 || minute > 59 || second > 61) {
  830. log_warn(LD_GENERAL, "Published time was nonsensical"); return -1;
  831. }
  832. st_tm.tm_year = year-1900;
  833. st_tm.tm_mon = month-1;
  834. st_tm.tm_mday = day;
  835. st_tm.tm_hour = hour;
  836. st_tm.tm_min = minute;
  837. st_tm.tm_sec = second;
  838. #endif
  839. if (st_tm.tm_year < 70) {
  840. char *esc = esc_for_log(cp);
  841. log_warn(LD_GENERAL, "Got invalid ISO time %s. (Before 1970)", esc);
  842. tor_free(esc);
  843. return -1;
  844. }
  845. *t = tor_timegm(&st_tm);
  846. return 0;
  847. }
  848. /* =====
  849. * File helpers
  850. * ===== */
  851. /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
  852. * must be 1 if fd was returned by socket() or accept(), and 0 if fd
  853. * was returned by open(). Return the number of bytes written, or -1
  854. * on error. Only use if fd is a blocking fd. */
  855. int
  856. write_all(int fd, const char *buf, size_t count, int isSocket)
  857. {
  858. size_t written = 0;
  859. int result;
  860. while (written != count) {
  861. if (isSocket)
  862. result = tor_socket_send(fd, buf+written, count-written, 0);
  863. else
  864. result = write(fd, buf+written, count-written);
  865. if (result<0)
  866. return -1;
  867. written += result;
  868. }
  869. return count;
  870. }
  871. /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
  872. * or reach the end of the file. <b>isSocket</b> must be 1 if fd
  873. * was returned by socket() or accept(), and 0 if fd was returned by
  874. * open(). Return the number of bytes read, or -1 on error. Only use
  875. * if fd is a blocking fd. */
  876. int
  877. read_all(int fd, char *buf, size_t count, int isSocket)
  878. {
  879. size_t numread = 0;
  880. int result;
  881. if (count > SIZE_T_CEILING)
  882. return -1;
  883. while (numread != count) {
  884. if (isSocket)
  885. result = tor_socket_recv(fd, buf+numread, count-numread, 0);
  886. else
  887. result = read(fd, buf+numread, count-numread);
  888. if (result<0)
  889. return -1;
  890. else if (result == 0)
  891. break;
  892. numread += result;
  893. }
  894. return numread;
  895. }
  896. /*
  897. * Filesystem operations.
  898. */
  899. /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
  900. * we do nothing. On Windows, we remove a trailing slash, unless the path is
  901. * the root of a disk. */
  902. static void
  903. clean_name_for_stat(char *name)
  904. {
  905. #ifdef MS_WINDOWS
  906. size_t len = strlen(name);
  907. if (!len)
  908. return;
  909. if (name[len-1]=='\\' || name[len-1]=='/') {
  910. if (len == 1 || (len==3 && name[1]==':'))
  911. return;
  912. name[len-1]='\0';
  913. }
  914. #else
  915. (void)name;
  916. #endif
  917. }
  918. /** Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
  919. * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
  920. * directory. */
  921. file_status_t
  922. file_status(const char *fname)
  923. {
  924. struct stat st;
  925. char *f;
  926. int r;
  927. f = tor_strdup(fname);
  928. clean_name_for_stat(f);
  929. r = stat(f, &st);
  930. tor_free(f);
  931. if (r) {
  932. if (errno == ENOENT) {
  933. return FN_NOENT;
  934. }
  935. return FN_ERROR;
  936. }
  937. if (st.st_mode & S_IFDIR)
  938. return FN_DIR;
  939. else if (st.st_mode & S_IFREG)
  940. return FN_FILE;
  941. else
  942. return FN_ERROR;
  943. }
  944. /** Check whether dirname exists and is private. If yes return 0. If
  945. * it does not exist, and check==CPD_CREATE is set, try to create it
  946. * and return 0 on success. If it does not exist, and
  947. * check==CPD_CHECK, and we think we can create it, return 0. Else
  948. * return -1. */
  949. int
  950. check_private_dir(const char *dirname, cpd_check_t check)
  951. {
  952. int r;
  953. struct stat st;
  954. char *f;
  955. tor_assert(dirname);
  956. f = tor_strdup(dirname);
  957. clean_name_for_stat(f);
  958. r = stat(f, &st);
  959. tor_free(f);
  960. if (r) {
  961. if (errno != ENOENT) {
  962. log(LOG_WARN, LD_FS, "Directory %s cannot be read: %s", dirname,
  963. strerror(errno));
  964. return -1;
  965. }
  966. if (check == CPD_NONE) {
  967. log(LOG_WARN, LD_FS, "Directory %s does not exist.", dirname);
  968. return -1;
  969. } else if (check == CPD_CREATE) {
  970. log_info(LD_GENERAL, "Creating directory %s", dirname);
  971. #ifdef MS_WINDOWS
  972. r = mkdir(dirname);
  973. #else
  974. r = mkdir(dirname, 0700);
  975. #endif
  976. if (r) {
  977. log(LOG_WARN, LD_FS, "Error creating directory %s: %s", dirname,
  978. strerror(errno));
  979. return -1;
  980. }
  981. }
  982. /* XXXX In the case where check==CPD_CHECK, we should look at the
  983. * parent directory a little harder. */
  984. return 0;
  985. }
  986. if (!(st.st_mode & S_IFDIR)) {
  987. log(LOG_WARN, LD_FS, "%s is not a directory", dirname);
  988. return -1;
  989. }
  990. #ifndef MS_WINDOWS
  991. if (st.st_uid != getuid()) {
  992. struct passwd *pw = NULL;
  993. char *process_ownername = NULL;
  994. pw = getpwuid(getuid());
  995. process_ownername = pw ? tor_strdup(pw->pw_name) : tor_strdup("<unknown>");
  996. pw = getpwuid(st.st_uid);
  997. log(LOG_WARN, LD_FS, "%s is not owned by this user (%s, %d) but by "
  998. "%s (%d). Perhaps you are running Tor as the wrong user?",
  999. dirname, process_ownername, (int)getuid(),
  1000. pw ? pw->pw_name : "<unknown>", (int)st.st_uid);
  1001. tor_free(process_ownername);
  1002. return -1;
  1003. }
  1004. if (st.st_mode & 0077) {
  1005. log(LOG_WARN, LD_FS, "Fixing permissions on directory %s", dirname);
  1006. if (chmod(dirname, 0700)) {
  1007. log(LOG_WARN, LD_FS, "Could not chmod directory %s: %s", dirname,
  1008. strerror(errno));
  1009. return -1;
  1010. } else {
  1011. return 0;
  1012. }
  1013. }
  1014. #endif
  1015. return 0;
  1016. }
  1017. /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite
  1018. * the previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
  1019. *
  1020. * This function replaces the old file atomically, if possible.
  1021. */
  1022. int
  1023. write_str_to_file(const char *fname, const char *str, int bin)
  1024. {
  1025. #ifdef MS_WINDOWS
  1026. if (!bin && strchr(str, '\r')) {
  1027. log_warn(LD_BUG,
  1028. "Bug: we're writing a text string that already contains a CR.");
  1029. }
  1030. #endif
  1031. return write_bytes_to_file(fname, str, strlen(str), bin);
  1032. }
  1033. /* DOCDOC */
  1034. static int
  1035. write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
  1036. int open_flags)
  1037. {
  1038. size_t tempname_len;
  1039. char *tempname;
  1040. int fd;
  1041. int result;
  1042. tempname_len = strlen(fname)+16;
  1043. tor_assert(tempname_len > strlen(fname)); /*check for overflow*/
  1044. tempname = tor_malloc(tempname_len);
  1045. if (open_flags & O_APPEND) {
  1046. strlcpy(tempname, fname, tempname_len);
  1047. } else {
  1048. if (tor_snprintf(tempname, tempname_len, "%s.tmp", fname)<0) {
  1049. log(LOG_WARN, LD_GENERAL, "Failed to generate filename");
  1050. goto err;
  1051. }
  1052. }
  1053. if ((fd = open(tempname, open_flags, 0600))
  1054. < 0) {
  1055. log(LOG_WARN, LD_FS, "Couldn't open \"%s\" for writing: %s", tempname,
  1056. strerror(errno));
  1057. goto err;
  1058. }
  1059. SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
  1060. {
  1061. result = write_all(fd, chunk->bytes, chunk->len, 0);
  1062. if (result < 0 || (size_t)result != chunk->len) {
  1063. log(LOG_WARN, LD_FS, "Error writing to \"%s\": %s", tempname,
  1064. strerror(errno));
  1065. close(fd);
  1066. goto err;
  1067. }
  1068. });
  1069. if (close(fd)) {
  1070. log(LOG_WARN, LD_FS, "Error flushing to \"%s\": %s", tempname,
  1071. strerror(errno));
  1072. goto err;
  1073. }
  1074. if (!(open_flags & O_APPEND)) {
  1075. if (replace_file(tempname, fname)) {
  1076. log(LOG_WARN, LD_FS, "Error replacing \"%s\": %s", fname,
  1077. strerror(errno));
  1078. goto err;
  1079. }
  1080. }
  1081. tor_free(tempname);
  1082. return 0;
  1083. err:
  1084. tor_free(tempname);
  1085. return -1;
  1086. }
  1087. /* DOCDOC */
  1088. int
  1089. write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin)
  1090. {
  1091. int flags = O_WRONLY|O_CREAT|O_TRUNC|(bin?O_BINARY:O_TEXT);
  1092. return write_chunks_to_file_impl(fname, chunks, flags);
  1093. }
  1094. /** As write_str_to_file, but does not assume a NUL-terminated *
  1095. * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
  1096. int
  1097. write_bytes_to_file(const char *fname, const char *str, size_t len,
  1098. int bin)
  1099. {
  1100. int flags = O_WRONLY|O_CREAT|O_TRUNC|(bin?O_BINARY:O_TEXT);
  1101. int r;
  1102. sized_chunk_t c = { str, len };
  1103. smartlist_t *chunks = smartlist_create();
  1104. smartlist_add(chunks, &c);
  1105. r = write_chunks_to_file_impl(fname, chunks, flags);
  1106. smartlist_free(chunks);
  1107. return r;
  1108. }
  1109. /* DOCDOC */
  1110. int
  1111. append_bytes_to_file(const char *fname, const char *str, size_t len,
  1112. int bin)
  1113. {
  1114. int flags = O_WRONLY|O_CREAT|O_APPEND|(bin?O_BINARY:O_TEXT);
  1115. int r;
  1116. sized_chunk_t c = { str, len };
  1117. smartlist_t *chunks = smartlist_create();
  1118. smartlist_add(chunks, &c);
  1119. r = write_chunks_to_file_impl(fname, chunks, flags);
  1120. smartlist_free(chunks);
  1121. return r;
  1122. }
  1123. /** Read the contents of <b>filename</b> into a newly allocated
  1124. * string; return the string on success or NULL on failure.
  1125. */
  1126. /*
  1127. * This function <em>may</em> return an erroneous result if the file
  1128. * is modified while it is running, but must not crash or overflow.
  1129. * Right now, the error case occurs when the file length grows between
  1130. * the call to stat and the call to read_all: the resulting string will
  1131. * be truncated.
  1132. */
  1133. char *
  1134. read_file_to_str(const char *filename, int bin)
  1135. {
  1136. int fd; /* router file */
  1137. struct stat statbuf;
  1138. char *string, *f;
  1139. int r;
  1140. tor_assert(filename);
  1141. f = tor_strdup(filename);
  1142. clean_name_for_stat(f);
  1143. r = stat(f, &statbuf);
  1144. tor_free(f);
  1145. if (r < 0) {
  1146. log_info(LD_FS,"Could not stat \"%s\".",filename);
  1147. return NULL;
  1148. }
  1149. fd = open(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
  1150. if (fd<0) {
  1151. log_warn(LD_FS,"Could not open \"%s\".",filename);
  1152. return NULL;
  1153. }
  1154. string = tor_malloc(statbuf.st_size+1);
  1155. r = read_all(fd,string,statbuf.st_size,0);
  1156. if (r<0) {
  1157. log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
  1158. strerror(errno));
  1159. tor_free(string);
  1160. close(fd);
  1161. return NULL;
  1162. }
  1163. string[r] = '\0'; /* NUL-terminate the result. */
  1164. if (bin && r != statbuf.st_size) {
  1165. /* If we're in binary mode, then we'd better have an exact match for
  1166. * size. Otherwise, win32 encoding may throw us off, and that's okay. */
  1167. log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
  1168. r, (long)statbuf.st_size,filename);
  1169. tor_free(string);
  1170. close(fd);
  1171. return NULL;
  1172. }
  1173. #ifdef MS_WINDOWS
  1174. if (!bin && strchr(string, '\r')) {
  1175. log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
  1176. "when reading %s. Coping.",
  1177. filename);
  1178. tor_strstrip(string, "\r");
  1179. }
  1180. #endif
  1181. close(fd);
  1182. return string;
  1183. }
  1184. /** Given a string containing part of a configuration file or similar format,
  1185. * advance past comments and whitespace and try to parse a single line. If we
  1186. * parse a line successfully, set *<b>key_out</b> to the key portion and
  1187. * *<b>value_out</b> to the value portion of the line, and return a pointer to
  1188. * the start of the next line. If we run out of data, return a pointer to the
  1189. * end of the string. If we encounter an error, return NULL.
  1190. *
  1191. * NOTE: We modify <b>line</b> as we parse it, by inserting NULs to terminate
  1192. * the key and value.
  1193. */
  1194. char *
  1195. parse_line_from_str(char *line, char **key_out, char **value_out)
  1196. {
  1197. char *key, *val, *cp;
  1198. tor_assert(key_out);
  1199. tor_assert(value_out);
  1200. *key_out = *value_out = key = val = NULL;
  1201. /* Skip until the first keyword. */
  1202. while (1) {
  1203. while (TOR_ISSPACE(*line))
  1204. ++line;
  1205. if (*line == '#') {
  1206. while (*line && *line != '\n')
  1207. ++line;
  1208. } else {
  1209. break;
  1210. }
  1211. }
  1212. if (!*line) { /* End of string? */
  1213. *key_out = *value_out = NULL;
  1214. return line;
  1215. }
  1216. /* Skip until the next space. */
  1217. key = line;
  1218. while (*line && !TOR_ISSPACE(*line) && *line != '#')
  1219. ++line;
  1220. /* Skip until the value */
  1221. while (*line == ' ' || *line == '\t')
  1222. *line++ = '\0';
  1223. val = line;
  1224. /* Find the end of the line. */
  1225. while (*line && *line != '\n' && *line != '#')
  1226. ++line;
  1227. if (*line == '\n')
  1228. cp = line++;
  1229. else {
  1230. cp = line-1;
  1231. }
  1232. while (cp>=val && TOR_ISSPACE(*cp))
  1233. *cp-- = '\0';
  1234. if (*line == '#') {
  1235. do {
  1236. *line++ = '\0';
  1237. } while (*line && *line != '\n');
  1238. if (*line == '\n')
  1239. ++line;
  1240. }
  1241. *key_out = key;
  1242. *value_out = val;
  1243. return line;
  1244. }
  1245. /** Expand any homedir prefix on 'filename'; return a newly allocated
  1246. * string. */
  1247. char *
  1248. expand_filename(const char *filename)
  1249. {
  1250. tor_assert(filename);
  1251. if (*filename == '~') {
  1252. size_t len;
  1253. char *home, *result;
  1254. const char *rest;
  1255. if (filename[1] == '/' || filename[1] == '\0') {
  1256. home = getenv("HOME");
  1257. if (!home) {
  1258. log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
  1259. "expanding \"%s\"", filename);
  1260. return NULL;
  1261. }
  1262. home = tor_strdup(home);
  1263. rest = strlen(filename)>=2?(filename+2):NULL;
  1264. } else {
  1265. #ifdef HAVE_PWD_H
  1266. char *username, *slash;
  1267. slash = strchr(filename, '/');
  1268. if (slash)
  1269. username = tor_strndup(filename+1,slash-filename-1);
  1270. else
  1271. username = tor_strdup(filename+1);
  1272. if (!(home = get_user_homedir(username))) {
  1273. log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
  1274. tor_free(username);
  1275. return NULL;
  1276. }
  1277. tor_free(username);
  1278. rest = slash ? (slash+1) : NULL;
  1279. #else
  1280. log_warn(LD_CONFIG, "Couldn't expend homedir on system without pwd.h");
  1281. return tor_strdup(filename);
  1282. #endif
  1283. }
  1284. tor_assert(home);
  1285. /* Remove trailing slash. */
  1286. if (strlen(home)>1 && !strcmpend(home,"/")) {
  1287. home[strlen(home)-1] = '\0';
  1288. }
  1289. /* Plus one for /, plus one for NUL.
  1290. * Round up to 16 in case we can't do math. */
  1291. len = strlen(home)+strlen(rest)+16;
  1292. result = tor_malloc(len);
  1293. tor_snprintf(result,len,"%s/%s",home,rest?rest:"");
  1294. tor_free(home);
  1295. return result;
  1296. } else {
  1297. return tor_strdup(filename);
  1298. }
  1299. }
  1300. /** Return a new list containing the filenames in the directory <b>dirname</b>.
  1301. * Return NULL on error or if <b>dirname</b> is not a directory.
  1302. */
  1303. smartlist_t *
  1304. tor_listdir(const char *dirname)
  1305. {
  1306. smartlist_t *result;
  1307. #ifdef MS_WINDOWS
  1308. char *pattern;
  1309. HANDLE handle;
  1310. WIN32_FIND_DATA findData;
  1311. size_t pattern_len = strlen(dirname)+16;
  1312. pattern = tor_malloc(pattern_len);
  1313. tor_snprintf(pattern, pattern_len, "%s\\*", dirname);
  1314. if (!(handle = FindFirstFile(pattern, &findData))) {
  1315. tor_free(pattern);
  1316. return NULL;
  1317. }
  1318. result = smartlist_create();
  1319. while (1) {
  1320. if (strcmp(findData.cFileName, ".") &&
  1321. strcmp(findData.cFileName, "..")) {
  1322. smartlist_add(result, tor_strdup(findData.cFileName));
  1323. }
  1324. if (!FindNextFile(handle, &findData)) {
  1325. if (GetLastError() != ERROR_NO_MORE_FILES) {
  1326. log_warn(LD_FS, "Error reading directory.");
  1327. }
  1328. break;
  1329. }
  1330. }
  1331. FindClose(handle);
  1332. tor_free(pattern);
  1333. #else
  1334. DIR *d;
  1335. struct dirent *de;
  1336. if (!(d = opendir(dirname)))
  1337. return NULL;
  1338. result = smartlist_create();
  1339. while ((de = readdir(d))) {
  1340. if (!strcmp(de->d_name, ".") ||
  1341. !strcmp(de->d_name, ".."))
  1342. continue;
  1343. smartlist_add(result, tor_strdup(de->d_name));
  1344. }
  1345. closedir(d);
  1346. #endif
  1347. return result;
  1348. }
  1349. /** Return true iff <b>filename</b> is a relative path. (XXXX doesn't work on
  1350. * windows.) */
  1351. int
  1352. path_is_relative(const char *filename)
  1353. {
  1354. if (filename && filename[0] == '/')
  1355. return 0;
  1356. else
  1357. return 1;
  1358. }
  1359. /* =====
  1360. * Net helpers
  1361. * ===== */
  1362. /** Return true iff <b>ip</b> (in host order) is an IP reserved to localhost,
  1363. * or reserved for local networks by RFC 1918.
  1364. */
  1365. int
  1366. is_internal_IP(uint32_t ip, int for_listening)
  1367. {
  1368. if (for_listening && !ip) /* special case for binding to 0.0.0.0 */
  1369. return 0;
  1370. if (((ip & 0xff000000) == 0x0a000000) || /* 10/8 */
  1371. ((ip & 0xff000000) == 0x00000000) || /* 0/8 */
  1372. ((ip & 0xff000000) == 0x7f000000) || /* 127/8 */
  1373. ((ip & 0xffff0000) == 0xa9fe0000) || /* 169.254/16 */
  1374. ((ip & 0xfff00000) == 0xac100000) || /* 172.16/12 */
  1375. ((ip & 0xffff0000) == 0xc0a80000)) /* 192.168/16 */
  1376. return 1;
  1377. return 0;
  1378. }
  1379. /** Return true iff <b>ip</b> (in host order) is judged to be on the
  1380. * same network as us. For now, check if it's an internal IP.
  1381. *
  1382. * XXX Also check if it's on the same class C network as our public IP.
  1383. */
  1384. int
  1385. is_local_IP(uint32_t ip)
  1386. {
  1387. return is_internal_IP(ip, 0);
  1388. }
  1389. /** Parse a string of the form "host[:port]" from <b>addrport</b>. If
  1390. * <b>address</b> is provided, set *<b>address</b> to a copy of the
  1391. * host portion of the string. If <b>addr</b> is provided, try to
  1392. * resolve the host portion of the string and store it into
  1393. * *<b>addr</b> (in host byte order). If <b>port_out</b> is provided,
  1394. * store the port number into *<b>port_out</b>, or 0 if no port is given.
  1395. * If <b>port_out</b> is NULL, then there must be no port number in
  1396. * <b>addrport</b>.
  1397. * Return 0 on success, -1 on failure.
  1398. */
  1399. int
  1400. parse_addr_port(int severity, const char *addrport, char **address,
  1401. uint32_t *addr, uint16_t *port_out)
  1402. {
  1403. const char *colon;
  1404. char *_address = NULL;
  1405. int _port;
  1406. int ok = 1;
  1407. tor_assert(addrport);
  1408. colon = strchr(addrport, ':');
  1409. if (colon) {
  1410. _address = tor_strndup(addrport, colon-addrport);
  1411. _port = (int) tor_parse_long(colon+1,10,1,65535,NULL,NULL);
  1412. if (!_port) {
  1413. log_fn(severity, LD_GENERAL, "Port %s out of range", escaped(colon+1));
  1414. ok = 0;
  1415. }
  1416. if (!port_out) {
  1417. char *esc_addrport = esc_for_log(addrport);
  1418. log_fn(severity, LD_GENERAL,
  1419. "Port %s given on %s when not required",
  1420. escaped(colon+1), esc_addrport);
  1421. tor_free(esc_addrport);
  1422. ok = 0;
  1423. }
  1424. } else {
  1425. _address = tor_strdup(addrport);
  1426. _port = 0;
  1427. }
  1428. if (addr) {
  1429. /* There's an addr pointer, so we need to resolve the hostname. */
  1430. if (tor_lookup_hostname(_address,addr)) {
  1431. log_fn(severity, LD_NET, "Couldn't look up %s", escaped(_address));
  1432. ok = 0;
  1433. *addr = 0;
  1434. }
  1435. *addr = ntohl(*addr);
  1436. }
  1437. if (address && ok) {
  1438. *address = _address;
  1439. } else {
  1440. if (address)
  1441. *address = NULL;
  1442. tor_free(_address);
  1443. }
  1444. if (port_out)
  1445. *port_out = ok ? ((uint16_t) _port) : 0;
  1446. return ok ? 0 : -1;
  1447. }
  1448. /** If <b>mask</b> is an address mask for a bit-prefix, return the number of
  1449. * bits. Otherwise, return -1. */
  1450. int
  1451. addr_mask_get_bits(uint32_t mask)
  1452. {
  1453. int i;
  1454. if (mask == 0)
  1455. return 0;
  1456. if (mask == 0xFFFFFFFFu)
  1457. return 32;
  1458. for (i=0; i<=32; ++i) {
  1459. if (mask == (uint32_t) ~((1u<<(32-i))-1)) {
  1460. return i;
  1461. }
  1462. }
  1463. return -1;
  1464. }
  1465. /** Parse a string <b>s</b> in the format of (*|port(-maxport)?)?, setting the
  1466. * various *out pointers as appropriate. Return 0 on success, -1 on failure.
  1467. */
  1468. int
  1469. parse_port_range(const char *port, uint16_t *port_min_out,
  1470. uint16_t *port_max_out)
  1471. {
  1472. tor_assert(port_min_out);
  1473. tor_assert(port_max_out);
  1474. if (!port || *port == '\0' || strcmp(port, "*") == 0) {
  1475. *port_min_out = 1;
  1476. *port_max_out = 65535;
  1477. } else {
  1478. char *endptr = NULL;
  1479. *port_min_out = (uint16_t) tor_parse_long(port, 10, 1, 65535,
  1480. NULL, &endptr);
  1481. if (*endptr == '-') {
  1482. port = endptr+1;
  1483. endptr = NULL;
  1484. *port_max_out = (uint16_t) tor_parse_long(port, 10, 1, 65535, NULL,
  1485. &endptr);
  1486. if (*endptr || !*port_max_out) {
  1487. log_warn(LD_GENERAL,
  1488. "Malformed port %s on address range rejecting.",
  1489. escaped(port));
  1490. }
  1491. } else if (*endptr || !*port_min_out) {
  1492. log_warn(LD_GENERAL,
  1493. "Malformed port %s on address range; rejecting.",
  1494. escaped(port));
  1495. return -1;
  1496. } else {
  1497. *port_max_out = *port_min_out;
  1498. }
  1499. if (*port_min_out > *port_max_out) {
  1500. log_warn(LD_GENERAL, "Insane port range on address policy; rejecting.");
  1501. return -1;
  1502. }
  1503. }
  1504. return 0;
  1505. }
  1506. /** Parse a string <b>s</b> in the format of
  1507. * (IP(/mask|/mask-bits)?|*)(:*|port(-maxport)?)?, setting the various
  1508. * *out pointers as appropriate. Return 0 on success, -1 on failure.
  1509. */
  1510. int
  1511. parse_addr_and_port_range(const char *s, uint32_t *addr_out,
  1512. uint32_t *mask_out, uint16_t *port_min_out,
  1513. uint16_t *port_max_out)
  1514. {
  1515. char *address;
  1516. char *mask, *port, *endptr;
  1517. struct in_addr in;
  1518. int bits;
  1519. tor_assert(s);
  1520. tor_assert(addr_out);
  1521. tor_assert(mask_out);
  1522. tor_assert(port_min_out);
  1523. tor_assert(port_max_out);
  1524. address = tor_strdup(s);
  1525. /* Break 'address' into separate strings.
  1526. */
  1527. mask = strchr(address,'/');
  1528. port = strchr(mask?mask:address,':');
  1529. if (mask)
  1530. *mask++ = '\0';
  1531. if (port)
  1532. *port++ = '\0';
  1533. /* Now "address" is the IP|'*' part...
  1534. * "mask" is the Mask|Maskbits part...
  1535. * and "port" is the *|port|min-max part.
  1536. */
  1537. if (strcmp(address,"*")==0) {
  1538. *addr_out = 0;
  1539. } else if (tor_inet_aton(address, &in) != 0) {
  1540. *addr_out = ntohl(in.s_addr);
  1541. } else {
  1542. log_warn(LD_GENERAL, "Malformed IP %s in address pattern; rejecting.",
  1543. escaped(address));
  1544. goto err;
  1545. }
  1546. if (!mask) {
  1547. if (strcmp(address,"*")==0)
  1548. *mask_out = 0;
  1549. else
  1550. *mask_out = 0xFFFFFFFFu;
  1551. } else {
  1552. endptr = NULL;
  1553. bits = (int) strtol(mask, &endptr, 10);
  1554. if (!*endptr) {
  1555. /* strtol handled the whole mask. */
  1556. if (bits < 0 || bits > 32) {
  1557. log_warn(LD_GENERAL,
  1558. "Bad number of mask bits on address range; rejecting.");
  1559. goto err;
  1560. }
  1561. *mask_out = ~((1u<<(32-bits))-1);
  1562. } else if (tor_inet_aton(mask, &in) != 0) {
  1563. *mask_out = ntohl(in.s_addr);
  1564. } else {
  1565. log_warn(LD_GENERAL,
  1566. "Malformed mask %s on address range; rejecting.",
  1567. escaped(mask));
  1568. goto err;
  1569. }
  1570. }
  1571. if (parse_port_range(port, port_min_out, port_max_out)<0)
  1572. goto err;
  1573. tor_free(address);
  1574. return 0;
  1575. err:
  1576. tor_free(address);
  1577. return -1;
  1578. }
  1579. /** Given an IPv4 address <b>in</b> (in network order, as usual),
  1580. * write it as a string into the <b>buf_len</b>-byte buffer in
  1581. * <b>buf</b>.
  1582. */
  1583. int
  1584. tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len)
  1585. {
  1586. uint32_t a = ntohl(in->s_addr);
  1587. return tor_snprintf(buf, buf_len, "%d.%d.%d.%d",
  1588. (int)(uint8_t)((a>>24)&0xff),
  1589. (int)(uint8_t)((a>>16)&0xff),
  1590. (int)(uint8_t)((a>>8 )&0xff),
  1591. (int)(uint8_t)((a )&0xff));
  1592. }
  1593. /** Given a host-order <b>addr</b>, call tor_inet_ntoa() on it
  1594. * and return a strdup of the resulting address.
  1595. */
  1596. char *
  1597. tor_dup_addr(uint32_t addr)
  1598. {
  1599. char buf[INET_NTOA_BUF_LEN];
  1600. struct in_addr in;
  1601. in.s_addr = htonl(addr);
  1602. tor_inet_ntoa(&in, buf, sizeof(buf));
  1603. return tor_strdup(buf);
  1604. }
  1605. /* Return true iff <b>name</b> looks like it might be a hostname,
  1606. * nickname, key, or IP address of some kind, suitable for the
  1607. * controller's "mapaddress" command. */
  1608. int
  1609. is_plausible_address(const char *name)
  1610. {
  1611. // const char *cp;
  1612. tor_assert(name);
  1613. /* We could check better here. */
  1614. if (!*name)
  1615. return 0;
  1616. #if 0
  1617. for (cp=name; *cp; cp++) {
  1618. if (*cp != '.' && *cp != '-' && !TOR_ISALNUM(*cp))
  1619. return 0;
  1620. }
  1621. #endif
  1622. return 1;
  1623. }
  1624. /**
  1625. * Set *<b>addr</b> to the host-order IPv4 address (if any) of whatever
  1626. * interface connects to the internet. This address should only be used in
  1627. * checking whether our address has changed. Return 0 on success, -1 on
  1628. * failure.
  1629. */
  1630. int
  1631. get_interface_address(int severity, uint32_t *addr)
  1632. {
  1633. int sock=-1, r=-1;
  1634. struct sockaddr_in target_addr, my_addr;
  1635. socklen_t my_addr_len = sizeof(my_addr);
  1636. tor_assert(addr);
  1637. *addr = 0;
  1638. sock = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
  1639. if (sock < 0) {
  1640. int e = tor_socket_errno(-1);
  1641. log_fn(severity, LD_NET, "unable to create socket: %s",
  1642. tor_socket_strerror(e));
  1643. goto err;
  1644. }
  1645. memset(&target_addr, 0, sizeof(target_addr));
  1646. target_addr.sin_family = AF_INET;
  1647. /* discard port */
  1648. target_addr.sin_port = 9;
  1649. /* 18.0.0.1 (Don't worry: no packets are sent. We just need a real address
  1650. * on the internet.) */
  1651. target_addr.sin_addr.s_addr = htonl(0x12000001);
  1652. if (connect(sock,(struct sockaddr *)&target_addr,sizeof(target_addr))<0) {
  1653. int e = tor_socket_errno(sock);
  1654. log_fn(severity, LD_NET, "connect() failed: %s", tor_socket_strerror(e));
  1655. goto err;
  1656. }
  1657. /* XXXX Can this be right on IPv6 clients? */
  1658. if (getsockname(sock, (struct sockaddr*)&my_addr, &my_addr_len)) {
  1659. int e = tor_socket_errno(sock);
  1660. log_fn(severity, LD_NET, "getsockname() failed: %s",
  1661. tor_socket_strerror(e));
  1662. goto err;
  1663. }
  1664. *addr = ntohl(my_addr.sin_addr.s_addr);
  1665. r=0;
  1666. err:
  1667. if (sock >= 0)
  1668. tor_close_socket(sock);
  1669. return r;
  1670. }
  1671. /* =====
  1672. * Process helpers
  1673. * ===== */
  1674. #ifndef MS_WINDOWS
  1675. /* Based on code contributed by christian grothoff */
  1676. static int start_daemon_called = 0;
  1677. static int finish_daemon_called = 0;
  1678. static int daemon_filedes[2];
  1679. /** Start putting the process into daemon mode: fork and drop all resources
  1680. * except standard fds. The parent process never returns, but stays around
  1681. * until finish_daemon is called. (Note: it's safe to call this more
  1682. * than once: calls after the first are ignored.)
  1683. */
  1684. void
  1685. start_daemon(void)
  1686. {
  1687. pid_t pid;
  1688. if (start_daemon_called)
  1689. return;
  1690. start_daemon_called = 1;
  1691. pipe(daemon_filedes);
  1692. pid = fork();
  1693. if (pid < 0) {
  1694. log_err(LD_GENERAL,"fork failed. Exiting.");
  1695. exit(1);
  1696. }
  1697. if (pid) { /* Parent */
  1698. int ok;
  1699. char c;
  1700. close(daemon_filedes[1]); /* we only read */
  1701. ok = -1;
  1702. while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
  1703. if (c == '.')
  1704. ok = 1;
  1705. }
  1706. fflush(stdout);
  1707. if (ok == 1)
  1708. exit(0);
  1709. else
  1710. exit(1); /* child reported error */
  1711. } else { /* Child */
  1712. close(daemon_filedes[0]); /* we only write */
  1713. pid = setsid(); /* Detach from controlling terminal */
  1714. /*
  1715. * Fork one more time, so the parent (the session group leader) can exit.
  1716. * This means that we, as a non-session group leader, can never regain a
  1717. * controlling terminal. This part is recommended by Stevens's
  1718. * _Advanced Programming in the Unix Environment_.
  1719. */
  1720. if (fork() != 0) {
  1721. exit(0);
  1722. }
  1723. return;
  1724. }
  1725. }
  1726. /** Finish putting the process into daemon mode: drop standard fds, and tell
  1727. * the parent process to exit. (Note: it's safe to call this more than once:
  1728. * calls after the first are ignored. Calls start_daemon first if it hasn't
  1729. * been called already.)
  1730. */
  1731. void
  1732. finish_daemon(const char *desired_cwd)
  1733. {
  1734. int nullfd;
  1735. char c = '.';
  1736. if (finish_daemon_called)
  1737. return;
  1738. if (!start_daemon_called)
  1739. start_daemon();
  1740. finish_daemon_called = 1;
  1741. if (!desired_cwd)
  1742. desired_cwd = "/";
  1743. /* Don't hold the wrong FS mounted */
  1744. if (chdir(desired_cwd) < 0) {
  1745. log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
  1746. exit(1);
  1747. }
  1748. nullfd = open("/dev/null",
  1749. O_CREAT | O_RDWR | O_APPEND);
  1750. if (nullfd < 0) {
  1751. log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
  1752. exit(1);
  1753. }
  1754. /* close fds linking to invoking terminal, but
  1755. * close usual incoming fds, but redirect them somewhere
  1756. * useful so the fds don't get reallocated elsewhere.
  1757. */
  1758. if (dup2(nullfd,0) < 0 ||
  1759. dup2(nullfd,1) < 0 ||
  1760. dup2(nullfd,2) < 0) {
  1761. log_err(LD_GENERAL,"dup2 failed. Exiting.");
  1762. exit(1);
  1763. }
  1764. if (nullfd > 2)
  1765. close(nullfd);
  1766. write(daemon_filedes[1], &c, sizeof(char)); /* signal success */
  1767. close(daemon_filedes[1]);
  1768. }
  1769. #else
  1770. /* defined(MS_WINDOWS) */
  1771. void
  1772. start_daemon(void)
  1773. {
  1774. }
  1775. void
  1776. finish_daemon(const char *cp)
  1777. {
  1778. }
  1779. #endif
  1780. /** Write the current process ID, followed by NL, into <b>filename</b>.
  1781. */
  1782. void
  1783. write_pidfile(char *filename)
  1784. {
  1785. #ifndef MS_WINDOWS
  1786. FILE *pidfile;
  1787. if ((pidfile = fopen(filename, "w")) == NULL) {
  1788. log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
  1789. strerror(errno));
  1790. } else {
  1791. fprintf(pidfile, "%d\n", (int)getpid());
  1792. fclose(pidfile);
  1793. }
  1794. #endif
  1795. }