util.c 74 KB

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