util.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2008, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util.c
  7. * \brief Common functions for strings, IO, network, data structures,
  8. * process control.
  9. **/
  10. /* This is required on rh7 to make strptime not complain.
  11. */
  12. #define _GNU_SOURCE
  13. #include "orconfig.h"
  14. #include "util.h"
  15. #include "log.h"
  16. #include "crypto.h"
  17. #include "torint.h"
  18. #include "container.h"
  19. #include "address.h"
  20. #ifdef MS_WINDOWS
  21. #include <io.h>
  22. #include <direct.h>
  23. #include <process.h>
  24. #else
  25. #include <dirent.h>
  26. #include <pwd.h>
  27. #endif
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #ifdef HAVE_NETINET_IN_H
  33. #include <netinet/in.h>
  34. #endif
  35. #ifdef HAVE_ARPA_INET_H
  36. #include <arpa/inet.h>
  37. #endif
  38. #ifdef HAVE_ERRNO_H
  39. #include <errno.h>
  40. #endif
  41. #ifdef HAVE_SYS_SOCKET_H
  42. #include <sys/socket.h>
  43. #endif
  44. #ifdef HAVE_SYS_TIME_H
  45. #include <sys/time.h>
  46. #endif
  47. #ifdef HAVE_UNISTD_H
  48. #include <unistd.h>
  49. #endif
  50. #ifdef HAVE_SYS_STAT_H
  51. #include <sys/stat.h>
  52. #endif
  53. #ifdef HAVE_SYS_FCNTL_H
  54. #include <sys/fcntl.h>
  55. #endif
  56. #ifdef HAVE_FCNTL_H
  57. #include <fcntl.h>
  58. #endif
  59. #ifdef HAVE_TIME_H
  60. #include <time.h>
  61. #endif
  62. #ifdef HAVE_MALLOC_MALLOC_H
  63. #include <malloc/malloc.h>
  64. #endif
  65. #ifdef HAVE_MALLOC_H
  66. #ifndef OPENBSD
  67. /* OpenBSD has a malloc.h, but for our purposes, it only exists in order to
  68. * scold us for being so stupid as to autodetect its presence. To be fair,
  69. * they've done this since 1996, when autoconf was only 5 years old. */
  70. #include <malloc.h>
  71. #endif
  72. #endif
  73. #ifdef HAVE_MALLOC_NP_H
  74. #include <malloc_np.h>
  75. #endif
  76. /* =====
  77. * Memory management
  78. * ===== */
  79. #ifdef USE_DMALLOC
  80. #undef strndup
  81. #include <dmalloc.h>
  82. /* Macro to pass the extra dmalloc args to another function. */
  83. #define DMALLOC_FN_ARGS , file, line
  84. #if defined(HAVE_DMALLOC_STRDUP)
  85. /* the dmalloc_strdup should be fine as defined */
  86. #elif defined(HAVE_DMALLOC_STRNDUP)
  87. #define dmalloc_strdup(file, line, string, xalloc_b) \
  88. dmalloc_strndup(file, line, (string), -1, xalloc_b)
  89. #else
  90. #error "No dmalloc_strdup or equivalent"
  91. #endif
  92. #else /* not using dmalloc */
  93. #define DMALLOC_FN_ARGS
  94. #endif
  95. /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
  96. * result. On error, log and terminate the process. (Same as malloc(size),
  97. * but never returns NULL.)
  98. *
  99. * <b>file</b> and <b>line</b> are used if dmalloc is enabled, and
  100. * ignored otherwise.
  101. */
  102. void *
  103. _tor_malloc(size_t size DMALLOC_PARAMS)
  104. {
  105. void *result;
  106. #ifndef MALLOC_ZERO_WORKS
  107. /* Some libc mallocs don't work when size==0. Override them. */
  108. if (size==0) {
  109. size=1;
  110. }
  111. #endif
  112. #ifdef USE_DMALLOC
  113. result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
  114. #else
  115. result = malloc(size);
  116. #endif
  117. if (PREDICT_UNLIKELY(result == NULL)) {
  118. log_err(LD_MM,"Out of memory on malloc(). Dying.");
  119. /* If these functions die within a worker process, they won't call
  120. * spawn_exit, but that's ok, since the parent will run out of memory soon
  121. * anyway. */
  122. exit(1);
  123. }
  124. return result;
  125. }
  126. /** Allocate a chunk of <b>size</b> bytes of memory, fill the memory with
  127. * zero bytes, and return a pointer to the result. Log and terminate
  128. * the process on error. (Same as calloc(size,1), but never returns NULL.)
  129. */
  130. void *
  131. _tor_malloc_zero(size_t size DMALLOC_PARAMS)
  132. {
  133. /* You may ask yourself, "wouldn't it be smart to use calloc instead of
  134. * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
  135. * we don't!" Indeed it does, but its optimizations are only a big win when
  136. * we're allocating something very big (it knows if it just got the memory
  137. * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
  138. * for big stuff, so we don't bother with calloc. */
  139. void *result = _tor_malloc(size DMALLOC_FN_ARGS);
  140. memset(result, 0, size);
  141. return result;
  142. }
  143. /** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
  144. * bytes long; return the new memory block. On error, log and
  145. * terminate. (Like realloc(ptr,size), but never returns NULL.)
  146. */
  147. void *
  148. _tor_realloc(void *ptr, size_t size DMALLOC_PARAMS)
  149. {
  150. void *result;
  151. #ifdef USE_DMALLOC
  152. result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
  153. #else
  154. result = realloc(ptr, size);
  155. #endif
  156. if (PREDICT_UNLIKELY(result == NULL)) {
  157. log_err(LD_MM,"Out of memory on realloc(). Dying.");
  158. exit(1);
  159. }
  160. return result;
  161. }
  162. /** Return a newly allocated copy of the NUL-terminated string s. On
  163. * error, log and terminate. (Like strdup(s), but never returns
  164. * NULL.)
  165. */
  166. char *
  167. _tor_strdup(const char *s DMALLOC_PARAMS)
  168. {
  169. char *dup;
  170. tor_assert(s);
  171. #ifdef USE_DMALLOC
  172. dup = dmalloc_strdup(file, line, s, 0);
  173. #else
  174. dup = strdup(s);
  175. #endif
  176. if (PREDICT_UNLIKELY(dup == NULL)) {
  177. log_err(LD_MM,"Out of memory on strdup(). Dying.");
  178. exit(1);
  179. }
  180. return dup;
  181. }
  182. /** Allocate and return a new string containing the first <b>n</b>
  183. * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
  184. * characters, only the first <b>n</b> are copied. The result is
  185. * always NUL-terminated. (Like strndup(s,n), but never returns
  186. * NULL.)
  187. */
  188. char *
  189. _tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
  190. {
  191. char *dup;
  192. tor_assert(s);
  193. dup = _tor_malloc((n+1) DMALLOC_FN_ARGS);
  194. /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
  195. * this function gets called a whole lot, and platform strncpy is
  196. * much faster than strlcpy when strlen(s) is much longer than n.
  197. */
  198. strncpy(dup, s, n);
  199. dup[n]='\0';
  200. return dup;
  201. }
  202. /** Allocate a chunk of <b>len</b> bytes, with the same contents as the
  203. * <b>len</b> bytes starting at <b>mem</b>. */
  204. void *
  205. _tor_memdup(const void *mem, size_t len DMALLOC_PARAMS)
  206. {
  207. char *dup;
  208. tor_assert(mem);
  209. dup = _tor_malloc(len DMALLOC_FN_ARGS);
  210. memcpy(dup, mem, len);
  211. return dup;
  212. }
  213. /** Helper for places that need to take a function pointer to the right
  214. * spelling of "free()". */
  215. void
  216. _tor_free(void *mem)
  217. {
  218. tor_free(mem);
  219. }
  220. #if defined(HAVE_MALLOC_GOOD_SIZE) && !defined(HAVE_MALLOC_GOOD_SIZE_PROTOTYPE)
  221. /* Some version of Mac OSX have malloc_good_size in their libc, but not
  222. * actually defined in malloc/malloc.h. We detect this and work around it by
  223. * prototyping.
  224. */
  225. extern size_t malloc_good_size(size_t size);
  226. #endif
  227. /** Allocate and return a chunk of memory of size at least *<b>size</b>, using
  228. * the same resources we would use to malloc *<b>sizep</b>. Set *<b>sizep</b>
  229. * to the number of usable bytes in the chunk of memory. */
  230. void *
  231. _tor_malloc_roundup(size_t *sizep DMALLOC_PARAMS)
  232. {
  233. #ifdef HAVE_MALLOC_GOOD_SIZE
  234. *sizep = malloc_good_size(*sizep);
  235. return _tor_malloc(*sizep DMALLOC_FN_ARGS);
  236. #elif defined(HAVE_MALLOC_USABLE_SIZE) && !defined(USE_DMALLOC)
  237. void *result = _tor_malloc(*sizep DMALLOC_FN_ARGS);
  238. *sizep = malloc_usable_size(result);
  239. return result;
  240. #else
  241. return _tor_malloc(*sizep DMALLOC_FN_ARGS);
  242. #endif
  243. }
  244. /** Call the platform malloc info function, and dump the results to the log at
  245. * level <b>severity</b>. If no such function exists, do nothing. */
  246. void
  247. tor_log_mallinfo(int severity)
  248. {
  249. #ifdef HAVE_MALLINFO
  250. struct mallinfo mi;
  251. memset(&mi, 0, sizeof(mi));
  252. mi = mallinfo();
  253. log(severity, LD_MM,
  254. "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
  255. "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
  256. "keepcost=%d",
  257. mi.arena, mi.ordblks, mi.smblks, mi.hblks,
  258. mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
  259. mi.keepcost);
  260. #else
  261. (void)severity;
  262. #endif
  263. #ifdef USE_DMALLOC
  264. dmalloc_log_changed(0, /* Since the program started. */
  265. 1, /* Log info about non-freed pointers. */
  266. 0, /* Do not log info about freed pointers. */
  267. 0 /* Do not log individual pointers. */
  268. );
  269. #endif
  270. }
  271. /* =====
  272. * Math
  273. * ===== */
  274. /** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */
  275. int
  276. tor_log2(uint64_t u64)
  277. {
  278. int r = 0;
  279. if (u64 >= (U64_LITERAL(1)<<32)) {
  280. u64 >>= 32;
  281. r = 32;
  282. }
  283. if (u64 >= (U64_LITERAL(1)<<16)) {
  284. u64 >>= 16;
  285. r += 16;
  286. }
  287. if (u64 >= (U64_LITERAL(1)<<8)) {
  288. u64 >>= 8;
  289. r += 8;
  290. }
  291. if (u64 >= (U64_LITERAL(1)<<4)) {
  292. u64 >>= 4;
  293. r += 4;
  294. }
  295. if (u64 >= (U64_LITERAL(1)<<2)) {
  296. u64 >>= 2;
  297. r += 2;
  298. }
  299. if (u64 >= (U64_LITERAL(1)<<1)) {
  300. u64 >>= 1;
  301. r += 1;
  302. }
  303. return r;
  304. }
  305. /** Return the power of 2 closest to <b>u64</b>. */
  306. uint64_t
  307. round_to_power_of_2(uint64_t u64)
  308. {
  309. int lg2 = tor_log2(u64);
  310. uint64_t low = U64_LITERAL(1) << lg2, high = U64_LITERAL(1) << (lg2+1);
  311. if (high - u64 < u64 - low)
  312. return high;
  313. else
  314. return low;
  315. }
  316. /* =====
  317. * String manipulation
  318. * ===== */
  319. /** Remove from the string <b>s</b> every character which appears in
  320. * <b>strip</b>. */
  321. void
  322. tor_strstrip(char *s, const char *strip)
  323. {
  324. char *read = s;
  325. while (*read) {
  326. if (strchr(strip, *read)) {
  327. ++read;
  328. } else {
  329. *s++ = *read++;
  330. }
  331. }
  332. *s = '\0';
  333. }
  334. /** Return a pointer to a NUL-terminated hexadecimal string encoding
  335. * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
  336. * result does not need to be deallocated, but repeated calls to
  337. * hex_str will trash old results.
  338. */
  339. const char *
  340. hex_str(const char *from, size_t fromlen)
  341. {
  342. static char buf[65];
  343. if (fromlen>(sizeof(buf)-1)/2)
  344. fromlen = (sizeof(buf)-1)/2;
  345. base16_encode(buf,sizeof(buf),from,fromlen);
  346. return buf;
  347. }
  348. /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
  349. * lowercase. */
  350. void
  351. tor_strlower(char *s)
  352. {
  353. while (*s) {
  354. *s = TOR_TOLOWER(*s);
  355. ++s;
  356. }
  357. }
  358. /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
  359. * lowercase. */
  360. void
  361. tor_strupper(char *s)
  362. {
  363. while (*s) {
  364. *s = TOR_TOUPPER(*s);
  365. ++s;
  366. }
  367. }
  368. /** Return 1 if every character in <b>s</b> is printable, else return 0.
  369. */
  370. int
  371. tor_strisprint(const char *s)
  372. {
  373. while (*s) {
  374. if (!TOR_ISPRINT(*s))
  375. return 0;
  376. s++;
  377. }
  378. return 1;
  379. }
  380. /** Return 1 if no character in <b>s</b> is uppercase, else return 0.
  381. */
  382. int
  383. tor_strisnonupper(const char *s)
  384. {
  385. while (*s) {
  386. if (TOR_ISUPPER(*s))
  387. return 0;
  388. s++;
  389. }
  390. return 1;
  391. }
  392. /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
  393. * strcmp.
  394. */
  395. int
  396. strcmpstart(const char *s1, const char *s2)
  397. {
  398. size_t n = strlen(s2);
  399. return strncmp(s1, s2, n);
  400. }
  401. /** Compare the s1_len-byte string <b>s1</b> with <b>s2</b>,
  402. * without depending on a terminating nul in s1. Sorting order is first by
  403. * length, then lexically; return values are as for strcmp.
  404. */
  405. int
  406. strcmp_len(const char *s1, const char *s2, size_t s1_len)
  407. {
  408. size_t s2_len = strlen(s2);
  409. if (s1_len < s2_len)
  410. return -1;
  411. if (s1_len > s2_len)
  412. return 1;
  413. return memcmp(s1, s2, s2_len);
  414. }
  415. /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
  416. * strcasecmp.
  417. */
  418. int
  419. strcasecmpstart(const char *s1, const char *s2)
  420. {
  421. size_t n = strlen(s2);
  422. return strncasecmp(s1, s2, n);
  423. }
  424. /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
  425. * strcmp.
  426. */
  427. int
  428. strcmpend(const char *s1, const char *s2)
  429. {
  430. size_t n1 = strlen(s1), n2 = strlen(s2);
  431. if (n2>n1)
  432. return strcmp(s1,s2);
  433. else
  434. return strncmp(s1+(n1-n2), s2, n2);
  435. }
  436. /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
  437. * strcasecmp.
  438. */
  439. int
  440. strcasecmpend(const char *s1, const char *s2)
  441. {
  442. size_t n1 = strlen(s1), n2 = strlen(s2);
  443. if (n2>n1) /* then they can't be the same; figure out which is bigger */
  444. return strcasecmp(s1,s2);
  445. else
  446. return strncasecmp(s1+(n1-n2), s2, n2);
  447. }
  448. /** Compare the value of the string <b>prefix</b> with the start of the
  449. * <b>memlen</b>-byte memory chunk at <b>mem</b>. Return as for strcmp.
  450. *
  451. * [As memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is less
  452. * than strlen(prefix).]
  453. */
  454. int
  455. memcmpstart(const void *mem, size_t memlen,
  456. const char *prefix)
  457. {
  458. size_t plen = strlen(prefix);
  459. if (memlen < plen)
  460. return -1;
  461. return memcmp(mem, prefix, plen);
  462. }
  463. /** Return a pointer to the first char of s that is not whitespace and
  464. * not a comment, or to the terminating NUL if no such character exists.
  465. */
  466. const char *
  467. eat_whitespace(const char *s)
  468. {
  469. tor_assert(s);
  470. while (1) {
  471. switch (*s) {
  472. case '\0':
  473. default:
  474. return s;
  475. case ' ':
  476. case '\t':
  477. case '\n':
  478. case '\r':
  479. ++s;
  480. break;
  481. case '#':
  482. ++s;
  483. while (*s && *s != '\n')
  484. ++s;
  485. }
  486. }
  487. }
  488. /** Return a pointer to the first char of s that is not whitespace and
  489. * not a comment, or to the terminating NUL if no such character exists.
  490. */
  491. const char *
  492. eat_whitespace_eos(const char *s, const char *eos)
  493. {
  494. tor_assert(s);
  495. tor_assert(eos && s <= eos);
  496. while (s < eos) {
  497. switch (*s) {
  498. case '\0':
  499. default:
  500. return s;
  501. case ' ':
  502. case '\t':
  503. case '\n':
  504. case '\r':
  505. ++s;
  506. break;
  507. case '#':
  508. ++s;
  509. while (s < eos && *s && *s != '\n')
  510. ++s;
  511. }
  512. }
  513. return s;
  514. }
  515. /** Return a pointer to the first char of s that is not a space or a tab
  516. * or a \\r, or to the terminating NUL if no such character exists. */
  517. const char *
  518. eat_whitespace_no_nl(const char *s)
  519. {
  520. while (*s == ' ' || *s == '\t' || *s == '\r')
  521. ++s;
  522. return s;
  523. }
  524. /** As eat_whitespace_no_nl, but stop at <b>eos</b> whether we have
  525. * found a non-whitespace character or not. */
  526. const char *
  527. eat_whitespace_eos_no_nl(const char *s, const char *eos)
  528. {
  529. while (s < eos && (*s == ' ' || *s == '\t' || *s == '\r'))
  530. ++s;
  531. return s;
  532. }
  533. /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
  534. * or to the terminating NUL if no such character exists.
  535. */
  536. const char *
  537. find_whitespace(const char *s)
  538. {
  539. /* tor_assert(s); */
  540. while (1) {
  541. switch (*s)
  542. {
  543. case '\0':
  544. case '#':
  545. case ' ':
  546. case '\r':
  547. case '\n':
  548. case '\t':
  549. return s;
  550. default:
  551. ++s;
  552. }
  553. }
  554. }
  555. /** As find_whitespace, but stop at <b>eos</b> whether we have found a
  556. * whitespace or not. */
  557. const char *
  558. find_whitespace_eos(const char *s, const char *eos)
  559. {
  560. /* tor_assert(s); */
  561. while (s < eos) {
  562. switch (*s)
  563. {
  564. case '\0':
  565. case '#':
  566. case ' ':
  567. case '\r':
  568. case '\n':
  569. case '\t':
  570. return s;
  571. default:
  572. ++s;
  573. }
  574. }
  575. return s;
  576. }
  577. /** Return true iff the 'len' bytes at 'mem' are all zero. */
  578. int
  579. tor_mem_is_zero(const char *mem, size_t len)
  580. {
  581. static const char ZERO[] = {
  582. 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,
  583. };
  584. while (len >= sizeof(ZERO)) {
  585. if (memcmp(mem, ZERO, sizeof(ZERO)))
  586. return 0;
  587. len -= sizeof(ZERO);
  588. mem += sizeof(ZERO);
  589. }
  590. /* Deal with leftover bytes. */
  591. if (len)
  592. return ! memcmp(mem, ZERO, len);
  593. return 1;
  594. }
  595. /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
  596. int
  597. tor_digest_is_zero(const char *digest)
  598. {
  599. return tor_mem_is_zero(digest, DIGEST_LEN);
  600. }
  601. /* Helper: common code to check whether the result of a strtol or strtoul or
  602. * strtoll is correct. */
  603. #define CHECK_STRTOX_RESULT() \
  604. /* Was at least one character converted? */ \
  605. if (endptr == s) \
  606. goto err; \
  607. /* Were there unexpected unconverted characters? */ \
  608. if (!next && *endptr) \
  609. goto err; \
  610. /* Is r within limits? */ \
  611. if (r < min || r > max) \
  612. goto err; \
  613. if (ok) *ok = 1; \
  614. if (next) *next = endptr; \
  615. return r; \
  616. err: \
  617. if (ok) *ok = 0; \
  618. if (next) *next = endptr; \
  619. return 0
  620. /** Extract a long from the start of s, in the given numeric base. If
  621. * there is unconverted data and next is provided, set *next to the
  622. * first unconverted character. An error has occurred if no characters
  623. * are converted; or if there are unconverted characters and next is NULL; or
  624. * if the parsed value is not between min and max. When no error occurs,
  625. * return the parsed value and set *ok (if provided) to 1. When an error
  626. * occurs, return 0 and set *ok (if provided) to 0.
  627. */
  628. long
  629. tor_parse_long(const char *s, int base, long min, long max,
  630. int *ok, char **next)
  631. {
  632. char *endptr;
  633. long r;
  634. r = strtol(s, &endptr, base);
  635. CHECK_STRTOX_RESULT();
  636. }
  637. /** As tor_parse_long(), but return an unsigned long. */
  638. unsigned long
  639. tor_parse_ulong(const char *s, int base, unsigned long min,
  640. unsigned long max, int *ok, char **next)
  641. {
  642. char *endptr;
  643. unsigned long r;
  644. r = strtoul(s, &endptr, base);
  645. CHECK_STRTOX_RESULT();
  646. }
  647. /** As tor_parse_log, but return a unit64_t. Only base 10 is guaranteed to
  648. * work for now. */
  649. uint64_t
  650. tor_parse_uint64(const char *s, int base, uint64_t min,
  651. uint64_t max, int *ok, char **next)
  652. {
  653. char *endptr;
  654. uint64_t r;
  655. #ifdef HAVE_STRTOULL
  656. r = (uint64_t)strtoull(s, &endptr, base);
  657. #elif defined(MS_WINDOWS)
  658. #if defined(_MSC_VER) && _MSC_VER < 1300
  659. tor_assert(base <= 10);
  660. r = (uint64_t)_atoi64(s);
  661. endptr = (char*)s;
  662. while (TOR_ISSPACE(*endptr)) endptr++;
  663. while (TOR_ISDIGIT(*endptr)) endptr++;
  664. #else
  665. r = (uint64_t)_strtoui64(s, &endptr, base);
  666. #endif
  667. #elif SIZEOF_LONG == 8
  668. r = (uint64_t)strtoul(s, &endptr, base);
  669. #else
  670. #error "I don't know how to parse 64-bit numbers."
  671. #endif
  672. CHECK_STRTOX_RESULT();
  673. }
  674. /** Encode the <b>srclen</b> bytes at <b>src</b> in a NUL-terminated,
  675. * uppercase hexadecimal string; store it in the <b>destlen</b>-byte buffer
  676. * <b>dest</b>.
  677. */
  678. void
  679. base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
  680. {
  681. const char *end;
  682. char *cp;
  683. tor_assert(destlen >= srclen*2+1);
  684. tor_assert(destlen < SIZE_T_CEILING);
  685. cp = dest;
  686. end = src+srclen;
  687. while (src<end) {
  688. *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) >> 4 ];
  689. *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) & 0xf ];
  690. ++src;
  691. }
  692. *cp = '\0';
  693. }
  694. /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
  695. static INLINE int
  696. _hex_decode_digit(char c)
  697. {
  698. switch (c) {
  699. case '0': return 0;
  700. case '1': return 1;
  701. case '2': return 2;
  702. case '3': return 3;
  703. case '4': return 4;
  704. case '5': return 5;
  705. case '6': return 6;
  706. case '7': return 7;
  707. case '8': return 8;
  708. case '9': return 9;
  709. case 'A': case 'a': return 10;
  710. case 'B': case 'b': return 11;
  711. case 'C': case 'c': return 12;
  712. case 'D': case 'd': return 13;
  713. case 'E': case 'e': return 14;
  714. case 'F': case 'f': return 15;
  715. default:
  716. return -1;
  717. }
  718. }
  719. /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
  720. int
  721. hex_decode_digit(char c)
  722. {
  723. return _hex_decode_digit(c);
  724. }
  725. /** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode it
  726. * and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>.
  727. * Return 0 on success, -1 on failure. */
  728. int
  729. base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
  730. {
  731. const char *end;
  732. int v1,v2;
  733. if ((srclen % 2) != 0)
  734. return -1;
  735. if (destlen < srclen/2 || destlen > SIZE_T_CEILING)
  736. return -1;
  737. end = src+srclen;
  738. while (src<end) {
  739. v1 = _hex_decode_digit(*src);
  740. v2 = _hex_decode_digit(*(src+1));
  741. if (v1<0||v2<0)
  742. return -1;
  743. *(uint8_t*)dest = (v1<<4)|v2;
  744. ++dest;
  745. src+=2;
  746. }
  747. return 0;
  748. }
  749. /** Allocate and return a new string representing the contents of <b>s</b>,
  750. * surrounded by quotes and using standard C escapes.
  751. *
  752. * Generally, we use this for logging values that come in over the network to
  753. * keep them from tricking users, and for sending certain values to the
  754. * controller.
  755. *
  756. * We trust values from the resolver, OS, configuration file, and command line
  757. * to not be maliciously ill-formed. We validate incoming routerdescs and
  758. * SOCKS requests and addresses from BEGIN cells as they're parsed;
  759. * afterwards, we trust them as non-malicious.
  760. */
  761. char *
  762. esc_for_log(const char *s)
  763. {
  764. const char *cp;
  765. char *result, *outp;
  766. size_t len = 3;
  767. if (!s) {
  768. return tor_strdup("");
  769. }
  770. for (cp = s; *cp; ++cp) {
  771. switch (*cp) {
  772. case '\\':
  773. case '\"':
  774. case '\'':
  775. len += 2;
  776. break;
  777. default:
  778. if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
  779. ++len;
  780. else
  781. len += 4;
  782. break;
  783. }
  784. }
  785. result = outp = tor_malloc(len);
  786. *outp++ = '\"';
  787. for (cp = s; *cp; ++cp) {
  788. switch (*cp) {
  789. case '\\':
  790. case '\"':
  791. case '\'':
  792. *outp++ = '\\';
  793. *outp++ = *cp;
  794. break;
  795. case '\n':
  796. *outp++ = '\\';
  797. *outp++ = 'n';
  798. break;
  799. case '\t':
  800. *outp++ = '\\';
  801. *outp++ = 't';
  802. break;
  803. case '\r':
  804. *outp++ = '\\';
  805. *outp++ = 'r';
  806. break;
  807. default:
  808. if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
  809. *outp++ = *cp;
  810. } else {
  811. tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
  812. outp += 4;
  813. }
  814. break;
  815. }
  816. }
  817. *outp++ = '\"';
  818. *outp++ = 0;
  819. return result;
  820. }
  821. /** Allocate and return a new string representing the contents of <b>s</b>,
  822. * surrounded by quotes and using standard C escapes.
  823. *
  824. * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
  825. * thread. Also, each call invalidates the last-returned value, so don't
  826. * try log_warn(LD_GENERAL, "%s %s", escaped(a), escaped(b));
  827. */
  828. const char *
  829. escaped(const char *s)
  830. {
  831. static char *_escaped_val = NULL;
  832. if (_escaped_val)
  833. tor_free(_escaped_val);
  834. if (s)
  835. _escaped_val = esc_for_log(s);
  836. else
  837. _escaped_val = NULL;
  838. return _escaped_val;
  839. }
  840. /** Rudimentary string wrapping code: given a un-wrapped <b>string</b> (no
  841. * newlines!), break the string into newline-terminated lines of no more than
  842. * <b>width</b> characters long (not counting newline) and insert them into
  843. * <b>out</b> in order. Precede the first line with prefix0, and subsequent
  844. * lines with prefixRest.
  845. */
  846. /* This uses a stupid greedy wrapping algorithm right now:
  847. * - For each line:
  848. * - Try to fit as much stuff as possible, but break on a space.
  849. * - If the first "word" of the line will extend beyond the allowable
  850. * width, break the word at the end of the width.
  851. */
  852. void
  853. wrap_string(smartlist_t *out, const char *string, size_t width,
  854. const char *prefix0, const char *prefixRest)
  855. {
  856. size_t p0Len, pRestLen, pCurLen;
  857. const char *eos, *prefixCur;
  858. tor_assert(out);
  859. tor_assert(string);
  860. tor_assert(width);
  861. if (!prefix0)
  862. prefix0 = "";
  863. if (!prefixRest)
  864. prefixRest = "";
  865. p0Len = strlen(prefix0);
  866. pRestLen = strlen(prefixRest);
  867. tor_assert(width > p0Len && width > pRestLen);
  868. eos = strchr(string, '\0');
  869. tor_assert(eos);
  870. pCurLen = p0Len;
  871. prefixCur = prefix0;
  872. while ((eos-string)+pCurLen > width) {
  873. const char *eol = string + width - pCurLen;
  874. while (eol > string && *eol != ' ')
  875. --eol;
  876. /* eol is now the last space that can fit, or the start of the string. */
  877. if (eol > string) {
  878. size_t line_len = (eol-string) + pCurLen + 2;
  879. char *line = tor_malloc(line_len);
  880. memcpy(line, prefixCur, pCurLen);
  881. memcpy(line+pCurLen, string, eol-string);
  882. line[line_len-2] = '\n';
  883. line[line_len-1] = '\0';
  884. smartlist_add(out, line);
  885. string = eol + 1;
  886. } else {
  887. size_t line_len = width + 2;
  888. char *line = tor_malloc(line_len);
  889. memcpy(line, prefixCur, pCurLen);
  890. memcpy(line+pCurLen, string, width - pCurLen);
  891. line[line_len-2] = '\n';
  892. line[line_len-1] = '\0';
  893. smartlist_add(out, line);
  894. string += width-pCurLen;
  895. }
  896. prefixCur = prefixRest;
  897. pCurLen = pRestLen;
  898. }
  899. if (string < eos) {
  900. size_t line_len = (eos-string) + pCurLen + 2;
  901. char *line = tor_malloc(line_len);
  902. memcpy(line, prefixCur, pCurLen);
  903. memcpy(line+pCurLen, string, eos-string);
  904. line[line_len-2] = '\n';
  905. line[line_len-1] = '\0';
  906. smartlist_add(out, line);
  907. }
  908. }
  909. /* =====
  910. * Time
  911. * ===== */
  912. /** Return the number of microseconds elapsed between *start and *end.
  913. */
  914. long
  915. tv_udiff(const struct timeval *start, const struct timeval *end)
  916. {
  917. long udiff;
  918. long secdiff = end->tv_sec - start->tv_sec;
  919. if (labs(secdiff+1) > LONG_MAX/1000000) {
  920. log_warn(LD_GENERAL, "comparing times too far apart.");
  921. return LONG_MAX;
  922. }
  923. udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
  924. return udiff;
  925. }
  926. /** Yield true iff <b>y</b> is a leap-year. */
  927. #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
  928. /** Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2. */
  929. static int
  930. n_leapdays(int y1, int y2)
  931. {
  932. --y1;
  933. --y2;
  934. return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
  935. }
  936. /** Number of days per month in non-leap year; used by tor_timegm. */
  937. static const int days_per_month[] =
  938. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  939. /** Return a time_t given a struct tm. The result is given in GMT, and
  940. * does not account for leap seconds.
  941. */
  942. time_t
  943. tor_timegm(struct tm *tm)
  944. {
  945. /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
  946. * It's way more brute-force than fiddling with tzset().
  947. */
  948. time_t year, days, hours, minutes, seconds;
  949. int i;
  950. year = tm->tm_year + 1900;
  951. if (year < 1970 || tm->tm_mon < 0 || tm->tm_mon > 11) {
  952. log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
  953. return -1;
  954. }
  955. tor_assert(year < INT_MAX);
  956. days = 365 * (year-1970) + n_leapdays(1970,(int)year);
  957. for (i = 0; i < tm->tm_mon; ++i)
  958. days += days_per_month[i];
  959. if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
  960. ++days;
  961. days += tm->tm_mday - 1;
  962. hours = days*24 + tm->tm_hour;
  963. minutes = hours*60 + tm->tm_min;
  964. seconds = minutes*60 + tm->tm_sec;
  965. return seconds;
  966. }
  967. /* strftime is locale-specific, so we need to replace those parts */
  968. /** A c-locale array of 3-letter names of weekdays, starting with Sun. */
  969. static const char *WEEKDAY_NAMES[] =
  970. { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  971. /** A c-locale array of 3-letter names of months, starting with Jan. */
  972. static const char *MONTH_NAMES[] =
  973. { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  974. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  975. /** Set <b>buf</b> to the RFC1123 encoding of the GMT value of <b>t</b>.
  976. * The buffer must be at least RFC1123_TIME_LEN+1 bytes long.
  977. *
  978. * (RFC1123 format is Fri, 29 Sep 2006 15:54:20 GMT)
  979. */
  980. void
  981. format_rfc1123_time(char *buf, time_t t)
  982. {
  983. struct tm tm;
  984. tor_gmtime_r(&t, &tm);
  985. strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
  986. tor_assert(tm.tm_wday >= 0);
  987. tor_assert(tm.tm_wday <= 6);
  988. memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
  989. tor_assert(tm.tm_wday >= 0);
  990. tor_assert(tm.tm_mon <= 11);
  991. memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
  992. }
  993. /** Parse the the RFC1123 encoding of some time (in GMT) from <b>buf</b>,
  994. * and store the result in *<b>t</b>.
  995. *
  996. * Return 0 on succcess, -1 on failure.
  997. */
  998. int
  999. parse_rfc1123_time(const char *buf, time_t *t)
  1000. {
  1001. struct tm tm;
  1002. char month[4];
  1003. char weekday[4];
  1004. int i, m;
  1005. if (strlen(buf) != RFC1123_TIME_LEN)
  1006. return -1;
  1007. memset(&tm, 0, sizeof(tm));
  1008. if (sscanf(buf, "%3s, %d %3s %d %d:%d:%d GMT", weekday,
  1009. &tm.tm_mday, month, &tm.tm_year, &tm.tm_hour,
  1010. &tm.tm_min, &tm.tm_sec) < 7) {
  1011. char *esc = esc_for_log(buf);
  1012. log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
  1013. tor_free(esc);
  1014. return -1;
  1015. }
  1016. m = -1;
  1017. for (i = 0; i < 12; ++i) {
  1018. if (!strcmp(month, MONTH_NAMES[i])) {
  1019. m = i;
  1020. break;
  1021. }
  1022. }
  1023. if (m<0) {
  1024. char *esc = esc_for_log(buf);
  1025. log_warn(LD_GENERAL, "Got invalid RFC1123 time %s: No such month", esc);
  1026. tor_free(esc);
  1027. return -1;
  1028. }
  1029. tm.tm_mon = m;
  1030. if (tm.tm_year < 1970) {
  1031. char *esc = esc_for_log(buf);
  1032. log_warn(LD_GENERAL,
  1033. "Got invalid RFC1123 time %s. (Before 1970)", esc);
  1034. tor_free(esc);
  1035. return -1;
  1036. }
  1037. tm.tm_year -= 1900;
  1038. *t = tor_timegm(&tm);
  1039. return 0;
  1040. }
  1041. /** Set <b>buf</b> to the ISO8601 encoding of the local value of <b>t</b>.
  1042. * The buffer must be at least ISO_TIME_LEN+1 bytes long.
  1043. *
  1044. * (ISO8601 format is 2006-10-29 10:57:20)
  1045. */
  1046. void
  1047. format_local_iso_time(char *buf, time_t t)
  1048. {
  1049. struct tm tm;
  1050. strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
  1051. }
  1052. /** Set <b>buf</b> to the ISO8601 encoding of the GMT value of <b>t</b>.
  1053. * The buffer must be at least ISO_TIME_LEN+1 bytes long.
  1054. */
  1055. void
  1056. format_iso_time(char *buf, time_t t)
  1057. {
  1058. struct tm tm;
  1059. strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
  1060. }
  1061. /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
  1062. * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
  1063. * failure. Ignore extraneous stuff in <b>cp</b> separated by whitespace from
  1064. * the end of the time string. */
  1065. int
  1066. parse_iso_time(const char *cp, time_t *t)
  1067. {
  1068. struct tm st_tm;
  1069. #ifdef HAVE_STRPTIME
  1070. if (!strptime(cp, "%Y-%m-%d %H:%M:%S", &st_tm)) {
  1071. log_warn(LD_GENERAL, "ISO time was unparseable by strptime"); return -1;
  1072. }
  1073. #else
  1074. unsigned int year=0, month=0, day=0, hour=100, minute=100, second=100;
  1075. if (sscanf(cp, "%u-%u-%u %u:%u:%u", &year, &month,
  1076. &day, &hour, &minute, &second) < 6) {
  1077. log_warn(LD_GENERAL, "ISO time was unparseable"); return -1;
  1078. }
  1079. if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
  1080. hour > 23 || minute > 59 || second > 61) {
  1081. log_warn(LD_GENERAL, "ISO time was nonsensical"); return -1;
  1082. }
  1083. st_tm.tm_year = year-1900;
  1084. st_tm.tm_mon = month-1;
  1085. st_tm.tm_mday = day;
  1086. st_tm.tm_hour = hour;
  1087. st_tm.tm_min = minute;
  1088. st_tm.tm_sec = second;
  1089. #endif
  1090. if (st_tm.tm_year < 70) {
  1091. char *esc = esc_for_log(cp);
  1092. log_warn(LD_GENERAL, "Got invalid ISO time %s. (Before 1970)", esc);
  1093. tor_free(esc);
  1094. return -1;
  1095. }
  1096. *t = tor_timegm(&st_tm);
  1097. return 0;
  1098. }
  1099. /** Given a <b>date</b> in one of the three formats allowed by HTTP (ugh),
  1100. * parse it into <b>tm</b>. Return 0 on success, negative on failure. */
  1101. int
  1102. parse_http_time(const char *date, struct tm *tm)
  1103. {
  1104. const char *cp;
  1105. char month[4];
  1106. char wkday[4];
  1107. int i;
  1108. tor_assert(tm);
  1109. memset(tm, 0, sizeof(*tm));
  1110. /* First, try RFC1123 or RFC850 format: skip the weekday. */
  1111. if ((cp = strchr(date, ','))) {
  1112. ++cp;
  1113. if (sscanf(date, "%2d %3s %4d %2d:%2d:%2d GMT",
  1114. &tm->tm_mday, month, &tm->tm_year,
  1115. &tm->tm_hour, &tm->tm_min, &tm->tm_sec) == 6) {
  1116. /* rfc1123-date */
  1117. tm->tm_year -= 1900;
  1118. } else if (sscanf(date, "%2d-%3s-%2d %2d:%2d:%2d GMT",
  1119. &tm->tm_mday, month, &tm->tm_year,
  1120. &tm->tm_hour, &tm->tm_min, &tm->tm_sec) == 6) {
  1121. /* rfc850-date */
  1122. } else {
  1123. return -1;
  1124. }
  1125. } else {
  1126. /* No comma; possibly asctime() format. */
  1127. if (sscanf(date, "%3s %3s %2d %2d:%2d:%2d %4d",
  1128. wkday, month, &tm->tm_mday,
  1129. &tm->tm_hour, &tm->tm_min, &tm->tm_sec, &tm->tm_year) == 7) {
  1130. tm->tm_year -= 1900;
  1131. } else {
  1132. return -1;
  1133. }
  1134. }
  1135. month[3] = '\0';
  1136. /* Okay, now decode the month. */
  1137. for (i = 0; i < 12; ++i) {
  1138. if (!strcasecmp(MONTH_NAMES[i], month)) {
  1139. tm->tm_mon = i+1;
  1140. }
  1141. }
  1142. if (tm->tm_year < 0 ||
  1143. tm->tm_mon < 1 || tm->tm_mon > 12 ||
  1144. tm->tm_mday < 0 || tm->tm_mday > 31 ||
  1145. tm->tm_hour < 0 || tm->tm_hour > 23 ||
  1146. tm->tm_min < 0 || tm->tm_min > 59 ||
  1147. tm->tm_sec < 0 || tm->tm_sec > 61)
  1148. return -1; /* Out of range, or bad month. */
  1149. return 0;
  1150. }
  1151. /** Given an <b>interval</b> in seconds, try to write it to the
  1152. * <b>out_len</b>-byte buffer in <b>out</b> in a human-readable form.
  1153. * Return 0 on success, -1 on failure.
  1154. */
  1155. int
  1156. format_time_interval(char *out, size_t out_len, long interval)
  1157. {
  1158. /* We only report seconds if there's no hours. */
  1159. long sec = 0, min = 0, hour = 0, day = 0;
  1160. if (interval < 0)
  1161. interval = -interval;
  1162. if (interval >= 86400) {
  1163. day = interval / 86400;
  1164. interval %= 86400;
  1165. }
  1166. if (interval >= 3600) {
  1167. hour = interval / 3600;
  1168. interval %= 3600;
  1169. }
  1170. if (interval >= 60) {
  1171. min = interval / 60;
  1172. interval %= 60;
  1173. }
  1174. sec = interval;
  1175. if (day) {
  1176. return tor_snprintf(out, out_len, "%ld days, %ld hours, %ld minutes",
  1177. day, hour, min);
  1178. } else if (hour) {
  1179. return tor_snprintf(out, out_len, "%ld hours, %ld minutes", hour, min);
  1180. } else if (min) {
  1181. return tor_snprintf(out, out_len, "%ld minutes, %ld seconds", min, sec);
  1182. } else {
  1183. return tor_snprintf(out, out_len, "%ld seconds", sec);
  1184. }
  1185. }
  1186. /* =====
  1187. * Cached time
  1188. * ===== */
  1189. #ifndef TIME_IS_FAST
  1190. /** Cached estimate of the currrent time. Updated around once per second;
  1191. * may be a few seconds off if we are really busy. This is a hack to avoid
  1192. * calling time(NULL) (which not everybody has optimized) on critical paths.
  1193. */
  1194. static time_t cached_approx_time = 0;
  1195. /** Return a cached estimate of the current time from when
  1196. * update_approx_time() was last called. This is a hack to avoid calling
  1197. * time(NULL) on critical paths: please do not even think of calling it
  1198. * anywhere else. */
  1199. time_t
  1200. approx_time(void)
  1201. {
  1202. return cached_approx_time;
  1203. }
  1204. /** Update the cached estimate of the current time. This function SHOULD be
  1205. * called once per second, and MUST be called before the first call to
  1206. * get_approx_time. */
  1207. void
  1208. update_approx_time(time_t now)
  1209. {
  1210. cached_approx_time = now;
  1211. }
  1212. #endif
  1213. /* =====
  1214. * Fuzzy time
  1215. * XXXX022 Use this consistently or rip most of it out.
  1216. * ===== */
  1217. /* In a perfect world, everybody would run ntp, and ntp would be perfect, so
  1218. * if we wanted to know "Is the current time before time X?" we could just say
  1219. * "time(NULL) < X".
  1220. *
  1221. * But unfortunately, many users are running Tor in an imperfect world, on
  1222. * even more imperfect computers. Hence, we need to track time oddly. We
  1223. * model the user's computer as being "skewed" from accurate time by
  1224. * -<b>ftime_skew</b> seconds, such that our best guess of the current time is
  1225. * time(NULL)+ftime_skew. We also assume that our measurements of time may
  1226. * have up to <b>ftime_slop</b> seconds of inaccuracy; IOW, our window of
  1227. * estimate for the current time is now + ftime_skew +/- ftime_slop.
  1228. */
  1229. /** Our current estimate of our skew, such that we think the current time is
  1230. * closest to time(NULL)+ftime_skew. */
  1231. static int ftime_skew = 0;
  1232. /** Tolerance during time comparisons, in seconds. */
  1233. static int ftime_slop = 60;
  1234. /** Set the largest amount of sloppiness we'll allow in fuzzy time
  1235. * comparisons. */
  1236. void
  1237. ftime_set_maximum_sloppiness(int seconds)
  1238. {
  1239. tor_assert(seconds >= 0);
  1240. ftime_slop = seconds;
  1241. }
  1242. /** Set the amount by which we believe our system clock to differ from
  1243. * real time. */
  1244. void
  1245. ftime_set_estimated_skew(int seconds)
  1246. {
  1247. ftime_skew = seconds;
  1248. }
  1249. #if 0
  1250. void
  1251. ftime_get_window(time_t now, ftime_t *ft_out)
  1252. {
  1253. ft_out->earliest = now + ftime_skew - ftime_slop;
  1254. ft_out->latest = now + ftime_skew + ftime_slop;
  1255. }
  1256. #endif
  1257. /** Return true iff we think that <b>now</b> might be after <b>when</b>. */
  1258. int
  1259. ftime_maybe_after(time_t now, time_t when)
  1260. {
  1261. /* It may be after when iff the latest possible current time is after when */
  1262. return (now + ftime_skew + ftime_slop) >= when;
  1263. }
  1264. /** Return true iff we think that <b>now</b> might be before <b>when</b>. */
  1265. int
  1266. ftime_maybe_before(time_t now, time_t when)
  1267. {
  1268. /* It may be before when iff the earliest possible current time is before */
  1269. return (now + ftime_skew - ftime_slop) < when;
  1270. }
  1271. /** Return true if we think that <b>now</b> is definitely after <b>when</b>. */
  1272. int
  1273. ftime_definitely_after(time_t now, time_t when)
  1274. {
  1275. /* It is definitely after when if the earliest time it could be is still
  1276. * after when. */
  1277. return (now + ftime_skew - ftime_slop) >= when;
  1278. }
  1279. /** Return true if we think that <b>now</b> is definitely before <b>when</b>.
  1280. */
  1281. int
  1282. ftime_definitely_before(time_t now, time_t when)
  1283. {
  1284. /* It is definitely before when if the latest time it could be is still
  1285. * before when. */
  1286. return (now + ftime_skew + ftime_slop) < when;
  1287. }
  1288. /* =====
  1289. * File helpers
  1290. * ===== */
  1291. /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
  1292. * must be 1 if fd was returned by socket() or accept(), and 0 if fd
  1293. * was returned by open(). Return the number of bytes written, or -1
  1294. * on error. Only use if fd is a blocking fd. */
  1295. ssize_t
  1296. write_all(int fd, const char *buf, size_t count, int isSocket)
  1297. {
  1298. size_t written = 0;
  1299. ssize_t result;
  1300. tor_assert(count < SSIZE_T_MAX);
  1301. while (written != count) {
  1302. if (isSocket)
  1303. result = tor_socket_send(fd, buf+written, count-written, 0);
  1304. else
  1305. result = write(fd, buf+written, count-written);
  1306. if (result<0)
  1307. return -1;
  1308. written += result;
  1309. }
  1310. return (ssize_t)count;
  1311. }
  1312. /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
  1313. * or reach the end of the file. <b>isSocket</b> must be 1 if fd
  1314. * was returned by socket() or accept(), and 0 if fd was returned by
  1315. * open(). Return the number of bytes read, or -1 on error. Only use
  1316. * if fd is a blocking fd. */
  1317. ssize_t
  1318. read_all(int fd, char *buf, size_t count, int isSocket)
  1319. {
  1320. size_t numread = 0;
  1321. ssize_t result;
  1322. if (count > SIZE_T_CEILING || count > SSIZE_T_MAX)
  1323. return -1;
  1324. while (numread != count) {
  1325. if (isSocket)
  1326. result = tor_socket_recv(fd, buf+numread, count-numread, 0);
  1327. else
  1328. result = read(fd, buf+numread, count-numread);
  1329. if (result<0)
  1330. return -1;
  1331. else if (result == 0)
  1332. break;
  1333. numread += result;
  1334. }
  1335. return (ssize_t)numread;
  1336. }
  1337. /*
  1338. * Filesystem operations.
  1339. */
  1340. /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
  1341. * we do nothing. On Windows, we remove a trailing slash, unless the path is
  1342. * the root of a disk. */
  1343. static void
  1344. clean_name_for_stat(char *name)
  1345. {
  1346. #ifdef MS_WINDOWS
  1347. size_t len = strlen(name);
  1348. if (!len)
  1349. return;
  1350. if (name[len-1]=='\\' || name[len-1]=='/') {
  1351. if (len == 1 || (len==3 && name[1]==':'))
  1352. return;
  1353. name[len-1]='\0';
  1354. }
  1355. #else
  1356. (void)name;
  1357. #endif
  1358. }
  1359. /** Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
  1360. * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
  1361. * directory. On FN_ERROR, sets errno. */
  1362. file_status_t
  1363. file_status(const char *fname)
  1364. {
  1365. struct stat st;
  1366. char *f;
  1367. int r;
  1368. f = tor_strdup(fname);
  1369. clean_name_for_stat(f);
  1370. r = stat(f, &st);
  1371. tor_free(f);
  1372. if (r) {
  1373. if (errno == ENOENT) {
  1374. return FN_NOENT;
  1375. }
  1376. return FN_ERROR;
  1377. }
  1378. if (st.st_mode & S_IFDIR)
  1379. return FN_DIR;
  1380. else if (st.st_mode & S_IFREG)
  1381. return FN_FILE;
  1382. else
  1383. return FN_ERROR;
  1384. }
  1385. /** Check whether dirname exists and is private. If yes return 0. If
  1386. * it does not exist, and check==CPD_CREATE is set, try to create it
  1387. * and return 0 on success. If it does not exist, and
  1388. * check==CPD_CHECK, and we think we can create it, return 0. Else
  1389. * return -1. */
  1390. int
  1391. check_private_dir(const char *dirname, cpd_check_t check)
  1392. {
  1393. int r;
  1394. struct stat st;
  1395. char *f;
  1396. tor_assert(dirname);
  1397. f = tor_strdup(dirname);
  1398. clean_name_for_stat(f);
  1399. r = stat(f, &st);
  1400. tor_free(f);
  1401. if (r) {
  1402. if (errno != ENOENT) {
  1403. log(LOG_WARN, LD_FS, "Directory %s cannot be read: %s", dirname,
  1404. strerror(errno));
  1405. return -1;
  1406. }
  1407. if (check == CPD_NONE) {
  1408. log(LOG_WARN, LD_FS, "Directory %s does not exist.", dirname);
  1409. return -1;
  1410. } else if (check == CPD_CREATE) {
  1411. log_info(LD_GENERAL, "Creating directory %s", dirname);
  1412. #ifdef MS_WINDOWS
  1413. r = mkdir(dirname);
  1414. #else
  1415. r = mkdir(dirname, 0700);
  1416. #endif
  1417. if (r) {
  1418. log(LOG_WARN, LD_FS, "Error creating directory %s: %s", dirname,
  1419. strerror(errno));
  1420. return -1;
  1421. }
  1422. }
  1423. /* XXXX In the case where check==CPD_CHECK, we should look at the
  1424. * parent directory a little harder. */
  1425. return 0;
  1426. }
  1427. if (!(st.st_mode & S_IFDIR)) {
  1428. log(LOG_WARN, LD_FS, "%s is not a directory", dirname);
  1429. return -1;
  1430. }
  1431. #ifndef MS_WINDOWS
  1432. if (st.st_uid != getuid()) {
  1433. struct passwd *pw = NULL;
  1434. char *process_ownername = NULL;
  1435. pw = getpwuid(getuid());
  1436. process_ownername = pw ? tor_strdup(pw->pw_name) : tor_strdup("<unknown>");
  1437. pw = getpwuid(st.st_uid);
  1438. log(LOG_WARN, LD_FS, "%s is not owned by this user (%s, %d) but by "
  1439. "%s (%d). Perhaps you are running Tor as the wrong user?",
  1440. dirname, process_ownername, (int)getuid(),
  1441. pw ? pw->pw_name : "<unknown>", (int)st.st_uid);
  1442. tor_free(process_ownername);
  1443. return -1;
  1444. }
  1445. if (st.st_mode & 0077) {
  1446. log(LOG_WARN, LD_FS, "Fixing permissions on directory %s", dirname);
  1447. if (chmod(dirname, 0700)) {
  1448. log(LOG_WARN, LD_FS, "Could not chmod directory %s: %s", dirname,
  1449. strerror(errno));
  1450. return -1;
  1451. } else {
  1452. return 0;
  1453. }
  1454. }
  1455. #endif
  1456. return 0;
  1457. }
  1458. /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite
  1459. * the previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
  1460. *
  1461. * This function replaces the old file atomically, if possible. This
  1462. * function, and all other functions in util.c that create files, create them
  1463. * with mode 0600.
  1464. */
  1465. int
  1466. write_str_to_file(const char *fname, const char *str, int bin)
  1467. {
  1468. #ifdef MS_WINDOWS
  1469. if (!bin && strchr(str, '\r')) {
  1470. log_warn(LD_BUG,
  1471. "We're writing a text string that already contains a CR.");
  1472. }
  1473. #endif
  1474. return write_bytes_to_file(fname, str, strlen(str), bin);
  1475. }
  1476. /** Represents a file that we're writing to, with support for atomic commit:
  1477. * we can write into a a temporary file, and either remove the file on
  1478. * failure, or replace the original file on success. */
  1479. struct open_file_t {
  1480. char *tempname; /**< Name of the temporary file. */
  1481. char *filename; /**< Name of the original file. */
  1482. int rename_on_close; /**< Are we using the temporary file or not? */
  1483. int fd; /**< fd for the open file. */
  1484. FILE *stdio_file; /**< stdio wrapper for <b>fd</b>. */
  1485. };
  1486. /** Try to start writing to the file in <b>fname</b>, passing the flags
  1487. * <b>open_flags</b> to the open() syscall, creating the file (if needed) with
  1488. * access value <b>mode</b>. If the O_APPEND flag is set, we append to the
  1489. * original file. Otherwise, we open a new temporary file in the same
  1490. * directory, and either replace the original or remove the temporary file
  1491. * when we're done.
  1492. *
  1493. * Return the fd for the newly opened file, and store working data in
  1494. * *<b>data_out</b>. The caller should not close the fd manually:
  1495. * instead, call finish_writing_to_file() or abort_writing_to_file().
  1496. * Returns -1 on failure.
  1497. *
  1498. * NOTE: When not appending, the flags O_CREAT and O_TRUNC are treated
  1499. * as true and the flag O_EXCL is treated as false.
  1500. *
  1501. * NOTE: Ordinarily, O_APPEND means "seek to the end of the file before each
  1502. * write()". We don't do that.
  1503. */
  1504. int
  1505. start_writing_to_file(const char *fname, int open_flags, int mode,
  1506. open_file_t **data_out)
  1507. {
  1508. size_t tempname_len = strlen(fname)+16;
  1509. open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
  1510. const char *open_name;
  1511. int append = 0;
  1512. tor_assert(fname);
  1513. tor_assert(data_out);
  1514. #if (O_BINARY != 0 && O_TEXT != 0)
  1515. tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
  1516. #endif
  1517. new_file->fd = -1;
  1518. tor_assert(tempname_len > strlen(fname)); /*check for overflow*/
  1519. new_file->filename = tor_strdup(fname);
  1520. if (open_flags & O_APPEND) {
  1521. open_name = fname;
  1522. new_file->rename_on_close = 0;
  1523. append = 1;
  1524. open_flags &= ~O_APPEND;
  1525. } else {
  1526. open_name = new_file->tempname = tor_malloc(tempname_len);
  1527. if (tor_snprintf(new_file->tempname, tempname_len, "%s.tmp", fname)<0) {
  1528. log(LOG_WARN, LD_GENERAL, "Failed to generate filename");
  1529. goto err;
  1530. }
  1531. /* We always replace an existing temporary file if there is one. */
  1532. open_flags |= O_CREAT|O_TRUNC;
  1533. open_flags &= ~O_EXCL;
  1534. new_file->rename_on_close = 1;
  1535. }
  1536. if ((new_file->fd = open(open_name, open_flags, mode)) < 0) {
  1537. log(LOG_WARN, LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
  1538. open_name, fname, strerror(errno));
  1539. goto err;
  1540. }
  1541. if (append) {
  1542. if (tor_fd_seekend(new_file->fd) < 0) {
  1543. log_warn(LD_FS, "Couldn't seek to end of file \"%s\": %s", open_name,
  1544. strerror(errno));
  1545. goto err;
  1546. }
  1547. }
  1548. *data_out = new_file;
  1549. return new_file->fd;
  1550. err:
  1551. if (new_file->fd >= 0)
  1552. close(new_file->fd);
  1553. *data_out = NULL;
  1554. tor_free(new_file->filename);
  1555. tor_free(new_file->tempname);
  1556. tor_free(new_file);
  1557. return -1;
  1558. }
  1559. /** Given <b>file_data</b> from start_writing_to_file(), return a stdio FILE*
  1560. * that can be used to write to the same file. The caller should not mix
  1561. * stdio calls with non-stdio calls. */
  1562. FILE *
  1563. fdopen_file(open_file_t *file_data)
  1564. {
  1565. tor_assert(file_data);
  1566. if (file_data->stdio_file)
  1567. return file_data->stdio_file;
  1568. tor_assert(file_data->fd >= 0);
  1569. if (!(file_data->stdio_file = fdopen(file_data->fd, "a"))) {
  1570. log_warn(LD_FS, "Couldn't fdopen \"%s\" [%d]: %s", file_data->filename,
  1571. file_data->fd, strerror(errno));
  1572. }
  1573. return file_data->stdio_file;
  1574. }
  1575. /** Combines start_writing_to_file with fdopen_file(): arguments are as
  1576. * for start_writing_to_file, but */
  1577. FILE *
  1578. start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
  1579. open_file_t **data_out)
  1580. {
  1581. FILE *res;
  1582. if (start_writing_to_file(fname, open_flags, mode, data_out)<0)
  1583. return NULL;
  1584. if (!(res = fdopen_file(*data_out))) {
  1585. abort_writing_to_file(*data_out);
  1586. *data_out = NULL;
  1587. }
  1588. return res;
  1589. }
  1590. /** Helper function: close and free the underlying file and memory in
  1591. * <b>file_data</b>. If we were writing into a temporary file, then delete
  1592. * that file (if abort_write is true) or replaces the target file with
  1593. * the temporary file (if abort_write is false). */
  1594. static int
  1595. finish_writing_to_file_impl(open_file_t *file_data, int abort_write)
  1596. {
  1597. int r = 0;
  1598. tor_assert(file_data && file_data->filename);
  1599. if (file_data->stdio_file) {
  1600. if (fclose(file_data->stdio_file)) {
  1601. log_warn(LD_FS, "Error closing \"%s\": %s", file_data->filename,
  1602. strerror(errno));
  1603. abort_write = r = -1;
  1604. }
  1605. } else if (file_data->fd >= 0 && close(file_data->fd) < 0) {
  1606. log_warn(LD_FS, "Error flushing \"%s\": %s", file_data->filename,
  1607. strerror(errno));
  1608. abort_write = r = -1;
  1609. }
  1610. if (file_data->rename_on_close) {
  1611. tor_assert(file_data->tempname && file_data->filename);
  1612. if (abort_write) {
  1613. unlink(file_data->tempname);
  1614. } else {
  1615. tor_assert(strcmp(file_data->filename, file_data->tempname));
  1616. if (replace_file(file_data->tempname, file_data->filename)) {
  1617. log_warn(LD_FS, "Error replacing \"%s\": %s", file_data->filename,
  1618. strerror(errno));
  1619. r = -1;
  1620. }
  1621. }
  1622. }
  1623. tor_free(file_data->filename);
  1624. tor_free(file_data->tempname);
  1625. tor_free(file_data);
  1626. return r;
  1627. }
  1628. /** Finish writing to <b>file_data</b>: close the file handle, free memory as
  1629. * needed, and if using a temporary file, replace the original file with
  1630. * the temporary file. */
  1631. int
  1632. finish_writing_to_file(open_file_t *file_data)
  1633. {
  1634. return finish_writing_to_file_impl(file_data, 0);
  1635. }
  1636. /** Finish writing to <b>file_data</b>: close the file handle, free memory as
  1637. * needed, and if using a temporary file, delete it. */
  1638. int
  1639. abort_writing_to_file(open_file_t *file_data)
  1640. {
  1641. return finish_writing_to_file_impl(file_data, 1);
  1642. }
  1643. /** Helper: given a set of flags as passed to open(2), open the file
  1644. * <b>fname</b> and write all the sized_chunk_t structs in <b>chunks</b> to
  1645. * the file. Do so as atomically as possible e.g. by opening temp files and
  1646. * renaming. */
  1647. static int
  1648. write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
  1649. int open_flags)
  1650. {
  1651. open_file_t *file = NULL;
  1652. int fd, result;
  1653. fd = start_writing_to_file(fname, open_flags, 0600, &file);
  1654. if (fd<0)
  1655. return -1;
  1656. SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
  1657. {
  1658. result = write_all(fd, chunk->bytes, chunk->len, 0);
  1659. if (result < 0) {
  1660. log(LOG_WARN, LD_FS, "Error writing to \"%s\": %s", fname,
  1661. strerror(errno));
  1662. goto err;
  1663. }
  1664. tor_assert((size_t)result == chunk->len);
  1665. });
  1666. return finish_writing_to_file(file);
  1667. err:
  1668. abort_writing_to_file(file);
  1669. return -1;
  1670. }
  1671. /** Given a smartlist of sized_chunk_t, write them atomically to a file
  1672. * <b>fname</b>, overwriting or creating the file as necessary. */
  1673. int
  1674. write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin)
  1675. {
  1676. int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
  1677. return write_chunks_to_file_impl(fname, chunks, flags);
  1678. }
  1679. /** As write_str_to_file, but does not assume a NUL-terminated
  1680. * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
  1681. int
  1682. write_bytes_to_file(const char *fname, const char *str, size_t len,
  1683. int bin)
  1684. {
  1685. int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
  1686. int r;
  1687. sized_chunk_t c = { str, len };
  1688. smartlist_t *chunks = smartlist_create();
  1689. smartlist_add(chunks, &c);
  1690. r = write_chunks_to_file_impl(fname, chunks, flags);
  1691. smartlist_free(chunks);
  1692. return r;
  1693. }
  1694. /** As write_bytes_to_file, but if the file already exists, append the bytes
  1695. * to the end of the file instead of overwriting it. */
  1696. int
  1697. append_bytes_to_file(const char *fname, const char *str, size_t len,
  1698. int bin)
  1699. {
  1700. int flags = OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT);
  1701. int r;
  1702. sized_chunk_t c = { str, len };
  1703. smartlist_t *chunks = smartlist_create();
  1704. smartlist_add(chunks, &c);
  1705. r = write_chunks_to_file_impl(fname, chunks, flags);
  1706. smartlist_free(chunks);
  1707. return r;
  1708. }
  1709. /** Read the contents of <b>filename</b> into a newly allocated
  1710. * string; return the string on success or NULL on failure.
  1711. *
  1712. * If <b>stat_out</b> is provided, store the result of stat()ing the
  1713. * file into <b>stat_out</b>.
  1714. *
  1715. * If <b>flags</b> &amp; RFTS_BIN, open the file in binary mode.
  1716. * If <b>flags</b> &amp; RFTS_IGNORE_MISSING, don't warn if the file
  1717. * doesn't exist.
  1718. */
  1719. /*
  1720. * This function <em>may</em> return an erroneous result if the file
  1721. * is modified while it is running, but must not crash or overflow.
  1722. * Right now, the error case occurs when the file length grows between
  1723. * the call to stat and the call to read_all: the resulting string will
  1724. * be truncated.
  1725. */
  1726. char *
  1727. read_file_to_str(const char *filename, int flags, struct stat *stat_out)
  1728. {
  1729. int fd; /* router file */
  1730. struct stat statbuf;
  1731. char *string;
  1732. int r;
  1733. int bin = flags & RFTS_BIN;
  1734. tor_assert(filename);
  1735. fd = open(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
  1736. if (fd<0) {
  1737. int severity = LOG_WARN;
  1738. int save_errno = errno;
  1739. if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
  1740. severity = LOG_INFO;
  1741. log_fn(severity, LD_FS,"Could not open \"%s\": %s ",filename,
  1742. strerror(errno));
  1743. errno = save_errno;
  1744. return NULL;
  1745. }
  1746. if (fstat(fd, &statbuf)<0) {
  1747. int save_errno = errno;
  1748. close(fd);
  1749. log_warn(LD_FS,"Could not fstat \"%s\".",filename);
  1750. errno = save_errno;
  1751. return NULL;
  1752. }
  1753. if ((uint64_t)(statbuf.st_size)+1 > SIZE_T_MAX)
  1754. return NULL;
  1755. string = tor_malloc((size_t)(statbuf.st_size+1));
  1756. r = read_all(fd,string,(size_t)statbuf.st_size,0);
  1757. if (r<0) {
  1758. int save_errno = errno;
  1759. log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
  1760. strerror(errno));
  1761. tor_free(string);
  1762. close(fd);
  1763. errno = save_errno;
  1764. return NULL;
  1765. }
  1766. string[r] = '\0'; /* NUL-terminate the result. */
  1767. #ifdef MS_WINDOWS
  1768. if (!bin && strchr(string, '\r')) {
  1769. log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
  1770. "when reading %s. Coping.",
  1771. filename);
  1772. tor_strstrip(string, "\r");
  1773. r = strlen(string);
  1774. }
  1775. if (!bin) {
  1776. statbuf.st_size = (size_t) r;
  1777. } else
  1778. #endif
  1779. if (r != statbuf.st_size) {
  1780. /* Unless we're using text mode on win32, we'd better have an exact
  1781. * match for size. */
  1782. int save_errno = errno;
  1783. log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
  1784. r, (long)statbuf.st_size,filename);
  1785. tor_free(string);
  1786. close(fd);
  1787. errno = save_errno;
  1788. return NULL;
  1789. }
  1790. close(fd);
  1791. if (stat_out) {
  1792. memcpy(stat_out, &statbuf, sizeof(struct stat));
  1793. }
  1794. return string;
  1795. }
  1796. #define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
  1797. /** Given a c-style double-quoted escaped string in <b>s</b>, extract and
  1798. * decode its contents into a newly allocated string. On success, assign this
  1799. * string to *<b>result</b>, assign its length to <b>size_out</b> (if
  1800. * provided), and return a pointer to the position in <b>s</b> immediately
  1801. * after the string. On failure, return NULL.
  1802. */
  1803. static const char *
  1804. unescape_string(const char *s, char **result, size_t *size_out)
  1805. {
  1806. const char *cp;
  1807. char *out;
  1808. if (s[0] != '\"')
  1809. return NULL;
  1810. cp = s+1;
  1811. while (1) {
  1812. switch (*cp) {
  1813. case '\0':
  1814. case '\n':
  1815. return NULL;
  1816. case '\"':
  1817. goto end_of_loop;
  1818. case '\\':
  1819. if ((cp[1] == 'x' || cp[1] == 'X')
  1820. && TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])) {
  1821. cp += 4;
  1822. } else if (TOR_ISODIGIT(cp[1])) {
  1823. cp += 2;
  1824. if (TOR_ISODIGIT(*cp)) ++cp;
  1825. if (TOR_ISODIGIT(*cp)) ++cp;
  1826. } else if (cp[1]) {
  1827. cp += 2;
  1828. } else {
  1829. return NULL;
  1830. }
  1831. break;
  1832. default:
  1833. ++cp;
  1834. break;
  1835. }
  1836. }
  1837. end_of_loop:
  1838. out = *result = tor_malloc(cp-s + 1);
  1839. cp = s+1;
  1840. while (1) {
  1841. switch (*cp)
  1842. {
  1843. case '\"':
  1844. *out = '\0';
  1845. if (size_out) *size_out = out - *result;
  1846. return cp+1;
  1847. case '\0':
  1848. tor_fragile_assert();
  1849. tor_free(*result);
  1850. return NULL;
  1851. case '\\':
  1852. switch (cp[1])
  1853. {
  1854. case 'n': *out++ = '\n'; cp += 2; break;
  1855. case 'r': *out++ = '\r'; cp += 2; break;
  1856. case 't': *out++ = '\t'; cp += 2; break;
  1857. case 'x': case 'X':
  1858. *out++ = ((hex_decode_digit(cp[2])<<4) +
  1859. hex_decode_digit(cp[3]));
  1860. cp += 4;
  1861. break;
  1862. case '0': case '1': case '2': case '3': case '4': case '5':
  1863. case '6': case '7':
  1864. {
  1865. int n = cp[1]-'0';
  1866. cp += 2;
  1867. if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
  1868. if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
  1869. if (n > 255) { tor_free(*result); return NULL; }
  1870. *out++ = (char)n;
  1871. }
  1872. break;
  1873. case '\'':
  1874. case '\"':
  1875. case '\\':
  1876. case '\?':
  1877. *out++ = cp[1];
  1878. cp += 2;
  1879. break;
  1880. default:
  1881. tor_free(*result); return NULL;
  1882. }
  1883. break;
  1884. default:
  1885. *out++ = *cp++;
  1886. }
  1887. }
  1888. }
  1889. /** Given a string containing part of a configuration file or similar format,
  1890. * advance past comments and whitespace and try to parse a single line. If we
  1891. * parse a line successfully, set *<b>key_out</b> to a new string holding the
  1892. * key portion and *<b>value_out</b> to a new string holding the value portion
  1893. * of the line, and return a pointer to the start of the next line. If we run
  1894. * out of data, return a pointer to the end of the string. If we encounter an
  1895. * error, return NULL.
  1896. */
  1897. const char *
  1898. parse_config_line_from_str(const char *line, char **key_out, char **value_out)
  1899. {
  1900. const char *key, *val, *cp;
  1901. tor_assert(key_out);
  1902. tor_assert(value_out);
  1903. *key_out = *value_out = NULL;
  1904. key = val = NULL;
  1905. /* Skip until the first keyword. */
  1906. while (1) {
  1907. while (TOR_ISSPACE(*line))
  1908. ++line;
  1909. if (*line == '#') {
  1910. while (*line && *line != '\n')
  1911. ++line;
  1912. } else {
  1913. break;
  1914. }
  1915. }
  1916. if (!*line) { /* End of string? */
  1917. *key_out = *value_out = NULL;
  1918. return line;
  1919. }
  1920. /* Skip until the next space. */
  1921. key = line;
  1922. while (*line && !TOR_ISSPACE(*line) && *line != '#')
  1923. ++line;
  1924. *key_out = tor_strndup(key, line-key);
  1925. /* Skip until the value. */
  1926. while (*line == ' ' || *line == '\t')
  1927. ++line;
  1928. val = line;
  1929. /* Find the end of the line. */
  1930. if (*line == '\"') {
  1931. if (!(line = unescape_string(line, value_out, NULL)))
  1932. return NULL;
  1933. while (*line == ' ' || *line == '\t')
  1934. ++line;
  1935. if (*line && *line != '#' && *line != '\n')
  1936. return NULL;
  1937. } else {
  1938. while (*line && *line != '\n' && *line != '#')
  1939. ++line;
  1940. if (*line == '\n') {
  1941. cp = line++;
  1942. } else {
  1943. cp = line;
  1944. }
  1945. while (cp>val && TOR_ISSPACE(*(cp-1)))
  1946. --cp;
  1947. tor_assert(cp >= val);
  1948. *value_out = tor_strndup(val, cp-val);
  1949. }
  1950. if (*line == '#') {
  1951. do {
  1952. ++line;
  1953. } while (*line && *line != '\n');
  1954. }
  1955. while (TOR_ISSPACE(*line)) ++line;
  1956. return line;
  1957. }
  1958. /** Expand any homedir prefix on <b>filename</b>; return a newly allocated
  1959. * string. */
  1960. char *
  1961. expand_filename(const char *filename)
  1962. {
  1963. tor_assert(filename);
  1964. if (*filename == '~') {
  1965. size_t len;
  1966. char *home, *result;
  1967. const char *rest;
  1968. if (filename[1] == '/' || filename[1] == '\0') {
  1969. home = getenv("HOME");
  1970. if (!home) {
  1971. log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
  1972. "expanding \"%s\"", filename);
  1973. return NULL;
  1974. }
  1975. home = tor_strdup(home);
  1976. rest = strlen(filename)>=2?(filename+2):"";
  1977. } else {
  1978. #ifdef HAVE_PWD_H
  1979. char *username, *slash;
  1980. slash = strchr(filename, '/');
  1981. if (slash)
  1982. username = tor_strndup(filename+1,slash-filename-1);
  1983. else
  1984. username = tor_strdup(filename+1);
  1985. if (!(home = get_user_homedir(username))) {
  1986. log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
  1987. tor_free(username);
  1988. return NULL;
  1989. }
  1990. tor_free(username);
  1991. rest = slash ? (slash+1) : "";
  1992. #else
  1993. log_warn(LD_CONFIG, "Couldn't expend homedir on system without pwd.h");
  1994. return tor_strdup(filename);
  1995. #endif
  1996. }
  1997. tor_assert(home);
  1998. /* Remove trailing slash. */
  1999. if (strlen(home)>1 && !strcmpend(home,PATH_SEPARATOR)) {
  2000. home[strlen(home)-1] = '\0';
  2001. }
  2002. /* Plus one for /, plus one for NUL.
  2003. * Round up to 16 in case we can't do math. */
  2004. len = strlen(home)+strlen(rest)+16;
  2005. result = tor_malloc(len);
  2006. tor_snprintf(result,len,"%s"PATH_SEPARATOR"%s",home,rest);
  2007. tor_free(home);
  2008. return result;
  2009. } else {
  2010. return tor_strdup(filename);
  2011. }
  2012. }
  2013. /** Return a new list containing the filenames in the directory <b>dirname</b>.
  2014. * Return NULL on error or if <b>dirname</b> is not a directory.
  2015. */
  2016. smartlist_t *
  2017. tor_listdir(const char *dirname)
  2018. {
  2019. smartlist_t *result;
  2020. #ifdef MS_WINDOWS
  2021. char *pattern;
  2022. HANDLE handle;
  2023. WIN32_FIND_DATA findData;
  2024. size_t pattern_len = strlen(dirname)+16;
  2025. pattern = tor_malloc(pattern_len);
  2026. tor_snprintf(pattern, pattern_len, "%s\\*", dirname);
  2027. if (INVALID_HANDLE_VALUE == (handle = FindFirstFile(pattern, &findData))) {
  2028. tor_free(pattern);
  2029. return NULL;
  2030. }
  2031. result = smartlist_create();
  2032. while (1) {
  2033. if (strcmp(findData.cFileName, ".") &&
  2034. strcmp(findData.cFileName, "..")) {
  2035. smartlist_add(result, tor_strdup(findData.cFileName));
  2036. }
  2037. if (!FindNextFile(handle, &findData)) {
  2038. DWORD err;
  2039. if ((err = GetLastError()) != ERROR_NO_MORE_FILES) {
  2040. char *errstr = format_win32_error(err);
  2041. log_warn(LD_FS, "Error reading directory '%s': %s", dirname, errstr);
  2042. tor_free(errstr);
  2043. }
  2044. break;
  2045. }
  2046. }
  2047. FindClose(handle);
  2048. tor_free(pattern);
  2049. #else
  2050. DIR *d;
  2051. struct dirent *de;
  2052. if (!(d = opendir(dirname)))
  2053. return NULL;
  2054. result = smartlist_create();
  2055. while ((de = readdir(d))) {
  2056. if (!strcmp(de->d_name, ".") ||
  2057. !strcmp(de->d_name, ".."))
  2058. continue;
  2059. smartlist_add(result, tor_strdup(de->d_name));
  2060. }
  2061. closedir(d);
  2062. #endif
  2063. return result;
  2064. }
  2065. /** Return true iff <b>filename</b> is a relative path. */
  2066. int
  2067. path_is_relative(const char *filename)
  2068. {
  2069. if (filename && filename[0] == '/')
  2070. return 0;
  2071. #ifdef MS_WINDOWS
  2072. else if (filename && filename[0] == '\\')
  2073. return 0;
  2074. else if (filename && strlen(filename)>3 && TOR_ISALPHA(filename[0]) &&
  2075. filename[1] == ':' && filename[2] == '\\')
  2076. return 0;
  2077. #endif
  2078. else
  2079. return 1;
  2080. }
  2081. /* =====
  2082. * Process helpers
  2083. * ===== */
  2084. #ifndef MS_WINDOWS
  2085. /* Based on code contributed by christian grothoff */
  2086. /** True iff we've called start_daemon(). */
  2087. static int start_daemon_called = 0;
  2088. /** True iff we've called finish_daemon(). */
  2089. static int finish_daemon_called = 0;
  2090. /** Socketpair used to communicate between parent and child process while
  2091. * daemonizing. */
  2092. static int daemon_filedes[2];
  2093. /** Start putting the process into daemon mode: fork and drop all resources
  2094. * except standard fds. The parent process never returns, but stays around
  2095. * until finish_daemon is called. (Note: it's safe to call this more
  2096. * than once: calls after the first are ignored.)
  2097. */
  2098. void
  2099. start_daemon(void)
  2100. {
  2101. pid_t pid;
  2102. if (start_daemon_called)
  2103. return;
  2104. start_daemon_called = 1;
  2105. if (pipe(daemon_filedes)) {
  2106. log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
  2107. exit(1);
  2108. }
  2109. pid = fork();
  2110. if (pid < 0) {
  2111. log_err(LD_GENERAL,"fork failed. Exiting.");
  2112. exit(1);
  2113. }
  2114. if (pid) { /* Parent */
  2115. int ok;
  2116. char c;
  2117. close(daemon_filedes[1]); /* we only read */
  2118. ok = -1;
  2119. while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
  2120. if (c == '.')
  2121. ok = 1;
  2122. }
  2123. fflush(stdout);
  2124. if (ok == 1)
  2125. exit(0);
  2126. else
  2127. exit(1); /* child reported error */
  2128. } else { /* Child */
  2129. close(daemon_filedes[0]); /* we only write */
  2130. pid = setsid(); /* Detach from controlling terminal */
  2131. /*
  2132. * Fork one more time, so the parent (the session group leader) can exit.
  2133. * This means that we, as a non-session group leader, can never regain a
  2134. * controlling terminal. This part is recommended by Stevens's
  2135. * _Advanced Programming in the Unix Environment_.
  2136. */
  2137. if (fork() != 0) {
  2138. exit(0);
  2139. }
  2140. return;
  2141. }
  2142. }
  2143. /** Finish putting the process into daemon mode: drop standard fds, and tell
  2144. * the parent process to exit. (Note: it's safe to call this more than once:
  2145. * calls after the first are ignored. Calls start_daemon first if it hasn't
  2146. * been called already.)
  2147. */
  2148. void
  2149. finish_daemon(const char *desired_cwd)
  2150. {
  2151. int nullfd;
  2152. char c = '.';
  2153. if (finish_daemon_called)
  2154. return;
  2155. if (!start_daemon_called)
  2156. start_daemon();
  2157. finish_daemon_called = 1;
  2158. if (!desired_cwd)
  2159. desired_cwd = "/";
  2160. /* Don't hold the wrong FS mounted */
  2161. if (chdir(desired_cwd) < 0) {
  2162. log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
  2163. exit(1);
  2164. }
  2165. nullfd = open("/dev/null", O_RDWR);
  2166. if (nullfd < 0) {
  2167. log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
  2168. exit(1);
  2169. }
  2170. /* close fds linking to invoking terminal, but
  2171. * close usual incoming fds, but redirect them somewhere
  2172. * useful so the fds don't get reallocated elsewhere.
  2173. */
  2174. if (dup2(nullfd,0) < 0 ||
  2175. dup2(nullfd,1) < 0 ||
  2176. dup2(nullfd,2) < 0) {
  2177. log_err(LD_GENERAL,"dup2 failed. Exiting.");
  2178. exit(1);
  2179. }
  2180. if (nullfd > 2)
  2181. close(nullfd);
  2182. /* signal success */
  2183. if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
  2184. log_err(LD_GENERAL,"write failed. Exiting.");
  2185. }
  2186. close(daemon_filedes[1]);
  2187. }
  2188. #else
  2189. /* defined(MS_WINDOWS) */
  2190. void
  2191. start_daemon(void)
  2192. {
  2193. }
  2194. void
  2195. finish_daemon(const char *cp)
  2196. {
  2197. (void)cp;
  2198. }
  2199. #endif
  2200. /** Write the current process ID, followed by NL, into <b>filename</b>.
  2201. */
  2202. void
  2203. write_pidfile(char *filename)
  2204. {
  2205. FILE *pidfile;
  2206. if ((pidfile = fopen(filename, "w")) == NULL) {
  2207. log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
  2208. strerror(errno));
  2209. } else {
  2210. #ifdef MS_WINDOWS
  2211. fprintf(pidfile, "%d\n", (int)_getpid());
  2212. #else
  2213. fprintf(pidfile, "%d\n", (int)getpid());
  2214. #endif
  2215. fclose(pidfile);
  2216. }
  2217. }