util.c 76 KB

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