config.c 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char config_c_id[] = "$Id$";
  7. /**
  8. * \file config.c
  9. * \brief Code to parse and interpret configuration files.
  10. **/
  11. #include "or.h"
  12. #ifdef MS_WINDOWS
  13. #include <shlobj.h>
  14. #endif
  15. #include "../common/aes.h"
  16. /** Enumeration of types which option values can take */
  17. typedef enum config_type_t {
  18. CONFIG_TYPE_STRING = 0, /**< An arbitrary string. */
  19. CONFIG_TYPE_UINT, /**< A non-negative integer less than MAX_INT */
  20. CONFIG_TYPE_INTERVAL, /**< A number of seconds, with optional units*/
  21. CONFIG_TYPE_MEMUNIT, /**< A number of bytes, with optional units*/
  22. CONFIG_TYPE_DOUBLE, /**< A floating-point value */
  23. CONFIG_TYPE_BOOL, /**< A boolean value, expressed as 0 or 1. */
  24. CONFIG_TYPE_ISOTIME, /**< An ISO-formated time relative to GMT. */
  25. CONFIG_TYPE_CSV, /**< A list of strings, separated by commas and optional
  26. * whitespace. */
  27. CONFIG_TYPE_LINELIST, /**< Uninterpreted config lines */
  28. CONFIG_TYPE_LINELIST_S, /**< Uninterpreted, context-sensitive config lines,
  29. * mixed with other keywords. */
  30. CONFIG_TYPE_LINELIST_V, /**< Catch-all "virtual" option to summarize
  31. * context-sensitive config lines when fetching.
  32. */
  33. CONFIG_TYPE_OBSOLETE, /**< Obsolete (ignored) option. */
  34. } config_type_t;
  35. /* An abbreviation for a configuration option allowed on the command line */
  36. typedef struct config_abbrev_t {
  37. const char *abbreviated;
  38. const char *full;
  39. int commandline_only;
  40. } config_abbrev_t;
  41. /* Handy macro for declaring "In the config file or on the command line,
  42. * you can abbreviate <b>tok</b>s as <b>tok</b>". */
  43. #define PLURAL(tok) { #tok, #tok "s", 0 }
  44. /* A list of command-line abbreviations. */
  45. static config_abbrev_t _option_abbrevs[] = {
  46. PLURAL(ExitNode),
  47. PLURAL(EntryNode),
  48. PLURAL(ExcludeNode),
  49. PLURAL(FirewallPort),
  50. PLURAL(LongLivedPort),
  51. PLURAL(HiddenServiceNode),
  52. PLURAL(HiddenServiceExcludeNode),
  53. PLURAL(NumCpu),
  54. PLURAL(RendNode),
  55. PLURAL(RendExcludeNode),
  56. PLURAL(StrictEntryNode),
  57. PLURAL(StrictExitNode),
  58. { "l", "Log", 1},
  59. { "BandwidthRateBytes", "BandwidthRate", 0},
  60. { "BandwidthBurstBytes", "BandwidthBurst", 0},
  61. { "DirFetchPostPeriod", "StatusFetchPeriod", 0},
  62. { "MaxConn", "ConnLimit", 0},
  63. { NULL, NULL , 0},
  64. };
  65. #undef PLURAL
  66. /** A variable allowed in the configuration file or on the command line. */
  67. typedef struct config_var_t {
  68. const char *name; /**< The full keyword (case insensitive). */
  69. config_type_t type; /**< How to interpret the type and turn it into a value. */
  70. off_t var_offset; /**< Offset of the corresponding member of or_options_t. */
  71. const char *initvalue; /**< String (or null) describing initial value. */
  72. const char *description;
  73. } config_var_t;
  74. /** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
  75. #define STRUCT_OFFSET(tp, member) ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
  76. /** An entry for config_vars: "The option <b>name</b> has type
  77. * CONFIG_TYPE_<b>conftype</b>, and corresponds to
  78. * or_options_t.<b>member</b>"
  79. */
  80. #define VAR(name,conftype,member,initvalue) \
  81. { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(or_options_t, member), initvalue, NULL }
  82. /** An entry for config_vars: "The option <b>name</b> is obsolete." */
  83. #define OBSOLETE(name) { name, CONFIG_TYPE_OBSOLETE, 0, NULL, NULL }
  84. /** Array of configuration options. Until we disallow nonstandard
  85. * abbreviations, order is significant, since the first matching option will
  86. * be chosen first.
  87. */
  88. static config_var_t _option_vars[] = {
  89. VAR("AccountingMax", MEMUNIT, AccountingMax, "0 bytes"),
  90. VAR("AccountingMaxKB", UINT, _AccountingMaxKB, "0"),
  91. VAR("AccountingStart", STRING, AccountingStart, NULL),
  92. VAR("Address", STRING, Address, NULL),
  93. VAR("AllowUnverifiedNodes",CSV, AllowUnverifiedNodes, "middle,rendezvous"),
  94. VAR("AssumeReachable", BOOL, AssumeReachable, "0"),
  95. VAR("AuthoritativeDirectory",BOOL, AuthoritativeDir, "0"),
  96. /* XXXX 011 change this default on 0.1.1.x */
  97. VAR("V1AuthoritativeDirectory",BOOL, V1AuthoritativeDir, "1"),
  98. VAR("BandwidthBurst", MEMUNIT, BandwidthBurst, "5 MB"),
  99. VAR("BandwidthRate", MEMUNIT, BandwidthRate, "2 MB"),
  100. VAR("ClientOnly", BOOL, ClientOnly, "0"),
  101. VAR("ConnLimit", UINT, ConnLimit, "1024"),
  102. VAR("ContactInfo", STRING, ContactInfo, NULL),
  103. VAR("ControlPort", UINT, ControlPort, "0"),
  104. VAR("CookieAuthentication",BOOL, CookieAuthentication, "0"),
  105. VAR("DataDirectory", STRING, DataDirectory, NULL),
  106. VAR("DebugLogFile", STRING, DebugLogFile, NULL),
  107. VAR("DirAllowPrivateAddresses",BOOL, DirAllowPrivateAddresses, NULL),
  108. VAR("DirBindAddress", LINELIST, DirBindAddress, NULL),
  109. VAR("DirFetchPeriod", INTERVAL, DirFetchPeriod, "0 seconds"), /** DOCDOC **/
  110. VAR("DirPolicy", LINELIST, DirPolicy, NULL),
  111. VAR("DirPort", UINT, DirPort, "0"),
  112. OBSOLETE("DirPostPeriod"),
  113. VAR("DirServer", LINELIST, DirServers, NULL),
  114. VAR("EntryNodes", STRING, EntryNodes, NULL),
  115. VAR("ExcludeNodes", STRING, ExcludeNodes, NULL),
  116. VAR("ExitNodes", STRING, ExitNodes, NULL),
  117. VAR("ExitPolicy", LINELIST, ExitPolicy, NULL),
  118. VAR("FascistFirewall", BOOL, FascistFirewall, "0"),
  119. VAR("FirewallPorts", CSV, FirewallPorts, ""),
  120. VAR("Group", STRING, Group, NULL),
  121. VAR("HardwareAccel", BOOL, HardwareAccel, "1"),
  122. VAR("HashedControlPassword",STRING, HashedControlPassword, NULL),
  123. VAR("HiddenServiceDir", LINELIST_S, RendConfigLines, NULL),
  124. VAR("HiddenServiceExcludeNodes", LINELIST_S, RendConfigLines, NULL),
  125. VAR("HiddenServiceNodes", LINELIST_S, RendConfigLines, NULL),
  126. VAR("HiddenServiceOptions",LINELIST_V, RendConfigLines, NULL),
  127. VAR("HiddenServicePort", LINELIST_S, RendConfigLines, NULL),
  128. VAR("HttpProxy", STRING, HttpProxy, NULL),
  129. VAR("HttpProxyAuthenticator",STRING, HttpProxyAuthenticator,NULL),
  130. VAR("HttpsProxy", STRING, HttpsProxy, NULL),
  131. VAR("HttpsProxyAuthenticator",STRING,HttpsProxyAuthenticator,NULL),
  132. OBSOLETE("IgnoreVersion"),
  133. VAR("KeepalivePeriod", INTERVAL, KeepalivePeriod, "5 minutes"),
  134. VAR("Log", LINELIST, Logs, NULL),
  135. OBSOLETE("LinkPadding"),
  136. VAR("LogFile", LINELIST_S, OldLogOptions, NULL),
  137. VAR("LogLevel", LINELIST_S, OldLogOptions, NULL),
  138. VAR("LongLivedPorts", CSV, LongLivedPorts, "21,22,706,1863,5050,5190,5222,5223,6667,8300,8888"),
  139. VAR("MapAddress", LINELIST, AddressMap, NULL),
  140. VAR("MaxAdvertisedBandwidth",MEMUNIT,MaxAdvertisedBandwidth,"128 TB"),
  141. VAR("MaxCircuitDirtiness", INTERVAL, MaxCircuitDirtiness, "10 minutes"),
  142. VAR("MaxOnionsPending", UINT, MaxOnionsPending, "100"),
  143. OBSOLETE("MonthlyAccountingStart"),
  144. VAR("MyFamily", STRING, MyFamily, NULL),
  145. VAR("NewCircuitPeriod", INTERVAL, NewCircuitPeriod, "30 seconds"),
  146. VAR("Nickname", STRING, Nickname, NULL),
  147. VAR("NoPublish", BOOL, NoPublish, "0"),
  148. VAR("NodeFamily", LINELIST, NodeFamilies, NULL),
  149. VAR("NumCpus", UINT, NumCpus, "1"),
  150. VAR("NumHelperNodes", UINT, NumHelperNodes, "3"),
  151. VAR("ORBindAddress", LINELIST, ORBindAddress, NULL),
  152. VAR("ORPort", UINT, ORPort, "0"),
  153. VAR("OutboundBindAddress", STRING, OutboundBindAddress, NULL),
  154. VAR("PathlenCoinWeight", DOUBLE, PathlenCoinWeight, "0.3"),
  155. VAR("PidFile", STRING, PidFile, NULL),
  156. VAR("ReachableAddresses", LINELIST, ReachableAddresses, NULL),
  157. VAR("RecommendedVersions", LINELIST, RecommendedVersions, NULL),
  158. VAR("RecommendedClientVersions", LINELIST, RecommendedClientVersions, NULL),
  159. VAR("RecommendedServerVersions", LINELIST, RecommendedServerVersions, NULL),
  160. VAR("RedirectExit", LINELIST, RedirectExit, NULL),
  161. VAR("RendExcludeNodes", STRING, RendExcludeNodes, NULL),
  162. VAR("RendNodes", STRING, RendNodes, NULL),
  163. VAR("RendPostPeriod", INTERVAL, RendPostPeriod, "20 minutes"),
  164. VAR("RephistTrackTime", INTERVAL, RephistTrackTime, "24 hours"),
  165. OBSOLETE("RouterFile"),
  166. VAR("RunAsDaemon", BOOL, RunAsDaemon, "0"),
  167. VAR("RunTesting", BOOL, RunTesting, "0"),
  168. VAR("SafeLogging", BOOL, SafeLogging, "1"),
  169. VAR("ShutdownWaitLength", INTERVAL, ShutdownWaitLength, "30 seconds"),
  170. VAR("SocksBindAddress", LINELIST, SocksBindAddress, NULL),
  171. VAR("SocksPolicy", LINELIST, SocksPolicy, NULL),
  172. VAR("SocksPort", UINT, SocksPort, "9050"),
  173. VAR("StatusFetchPeriod", INTERVAL, StatusFetchPeriod, "0 seconds"), /** DOCDOC */
  174. VAR("StrictEntryNodes", BOOL, StrictEntryNodes, "0"),
  175. VAR("StrictExitNodes", BOOL, StrictExitNodes, "0"),
  176. VAR("SysLog", LINELIST_S, OldLogOptions, NULL),
  177. VAR("TrackHostExits", CSV, TrackHostExits, NULL),
  178. VAR("TrackHostExitsExpire",INTERVAL, TrackHostExitsExpire, "30 minutes"),
  179. OBSOLETE("TrafficShaping"),
  180. VAR("UseHelperNodes", BOOL, UseHelperNodes, "0"),
  181. VAR("User", STRING, User, NULL),
  182. VAR("__LeaveStreamsUnattached", BOOL,LeaveStreamsUnattached, "0"),
  183. { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL, NULL }
  184. };
  185. #undef VAR
  186. #define VAR(name,conftype,member,initvalue) \
  187. { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(or_state_t, member), initvalue, NULL }
  188. static config_var_t _state_vars[] = {
  189. VAR("AccountingBytesReadInterval", MEMUNIT, AccountingBytesReadInInterval,NULL),
  190. VAR("AccountingBytesWrittenInInterval", MEMUNIT,
  191. AccountingBytesWrittenInInterval, NULL),
  192. VAR("AccountingExpectedUsage", MEMUNIT, AccountingExpectedUsage, NULL),
  193. VAR("AccountingIntervalStart", ISOTIME, AccountingIntervalStart, NULL),
  194. VAR("AccountingSecondsActive", INTERVAL, AccountingSecondsActive, NULL),
  195. VAR("HelperNode", LINELIST_S, HelperNodes, NULL),
  196. VAR("HelperNodeDownSince", LINELIST_S, HelperNodes, NULL),
  197. VAR("HelperNodeUnlistedSince", LINELIST_S, HelperNodes, NULL),
  198. VAR("HelperNodes", LINELIST_V, HelperNodes, NULL),
  199. VAR("LastWritten", ISOTIME, LastWritten, NULL),
  200. { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL, NULL }
  201. };
  202. #undef VAR
  203. #undef OBSOLETE
  204. /** DOCDOC*/
  205. typedef struct config_var_description_t {
  206. const char *name;
  207. const char *description;
  208. } config_var_description_t;
  209. static config_var_description_t options_description[] = {
  210. { "Address", "The advertised (external) address we should use." },
  211. // { "AccountingStart", ""},
  212. { NULL, NULL },
  213. };
  214. static config_var_description_t state_description[] = {
  215. { "HelperNode", "One of the nodes we have chosen as a fixed entry" },
  216. { "HelperNodeDownSince",
  217. "The last helper node has been down since this time." },
  218. { "HelperNodeUnlistedSince",
  219. "The last helper node has been unlisted since this time." },
  220. { NULL, NULL },
  221. };
  222. typedef int (*validate_fn_t)(void*);
  223. typedef struct {
  224. size_t size;
  225. uint32_t magic;
  226. off_t magic_offset;
  227. config_abbrev_t *abbrevs;
  228. config_var_t *vars;
  229. validate_fn_t validate_fn;
  230. config_var_description_t *descriptions;
  231. } config_format_t;
  232. #define CHECK(fmt, cfg) do { \
  233. tor_assert(fmt && cfg); \
  234. tor_assert((fmt)->magic == *(uint32_t*)(((char*)(cfg))+fmt->magic_offset)); \
  235. } while (0)
  236. /** Largest allowed config line */
  237. #define CONFIG_LINE_T_MAXLEN 4096
  238. static void config_line_append(config_line_t **lst,
  239. const char *key, const char *val);
  240. static void option_clear(config_format_t *fmt, or_options_t *options,
  241. config_var_t *var);
  242. static void option_reset(config_format_t *fmt, or_options_t *options,
  243. config_var_t *var);
  244. static void config_free(config_format_t *fmt, void *options);
  245. static int option_is_same(config_format_t *fmt,
  246. or_options_t *o1, or_options_t *o2,const char *name);
  247. static or_options_t *options_dup(config_format_t *fmt, or_options_t *old);
  248. static int options_validate(or_options_t *options);
  249. static int options_act(or_options_t *old_options);
  250. static int options_transition_allowed(or_options_t *old, or_options_t *new);
  251. static int options_transition_affects_workers(or_options_t *old_options,
  252. or_options_t *new_options);
  253. static int options_transition_affects_descriptor(or_options_t *old_options,
  254. or_options_t *new_options);
  255. static int check_nickname_list(const char *lst, const char *name);
  256. static void config_register_addressmaps(or_options_t *options);
  257. static int parse_dir_server_line(const char *line, int validate_only);
  258. static int parse_redirect_line(smartlist_t *result,
  259. config_line_t *line);
  260. static int parse_log_severity_range(const char *range, int *min_out,
  261. int *max_out);
  262. static int convert_log_option(or_options_t *options,
  263. config_line_t *level_opt,
  264. config_line_t *file_opt, int isDaemon);
  265. static int add_single_log_option(or_options_t *options, int minSeverity,
  266. int maxSeverity,
  267. const char *type, const char *fname);
  268. static int normalize_log_options(or_options_t *options);
  269. static int validate_data_directory(or_options_t *options);
  270. static int write_configuration_file(const char *fname, or_options_t *options);
  271. static config_line_t *get_assigned_option(config_format_t *fmt,
  272. or_options_t *options, const char *key);
  273. static void config_init(config_format_t *fmt, void *options);
  274. static int or_state_validate(or_state_t *options);
  275. static uint64_t config_parse_memunit(const char *s, int *ok);
  276. static int config_parse_interval(const char *s, int *ok);
  277. static void print_cvs_version(void);
  278. static void parse_reachable_addresses(void);
  279. static int init_libevent(void);
  280. #if defined(HAVE_EVENT_GET_VERSION) && defined(HAVE_EVENT_GET_METHOD)
  281. static void check_libevent_version(const char *m, const char *v, int server);
  282. #endif
  283. /*static*/ or_options_t *options_new(void);
  284. #define OR_OPTIONS_MAGIC 9090909
  285. static config_format_t options_format = {
  286. sizeof(or_options_t),
  287. OR_OPTIONS_MAGIC,
  288. STRUCT_OFFSET(or_options_t, _magic),
  289. _option_abbrevs,
  290. _option_vars,
  291. (validate_fn_t)options_validate,
  292. options_description
  293. };
  294. #define OR_STATE_MAGIC 0x57A73f57
  295. static config_format_t state_format = {
  296. sizeof(or_state_t),
  297. OR_STATE_MAGIC,
  298. STRUCT_OFFSET(or_state_t, _magic),
  299. NULL,
  300. _state_vars,
  301. (validate_fn_t)or_state_validate,
  302. state_description
  303. };
  304. /*
  305. * Functions to read and write the global options pointer.
  306. */
  307. /** Command-line and config-file options. */
  308. static or_options_t *global_options = NULL;
  309. /** Name of most recently read torrc file. */
  310. static char *torrc_fname = NULL;
  311. /** Persistant serialized state. */
  312. static or_state_t *global_state = NULL;
  313. /** DOCDOC */
  314. static addr_policy_t *reachable_addr_policy = NULL;
  315. static void *
  316. config_alloc(config_format_t *fmt)
  317. {
  318. void *opts = opts = tor_malloc_zero(fmt->size);
  319. *(uint32_t*)(((char*)opts)+fmt->magic_offset) = fmt->magic;
  320. CHECK(fmt, opts);
  321. return opts;
  322. }
  323. /** Return the currently configured options. */
  324. or_options_t *
  325. get_options(void)
  326. {
  327. tor_assert(global_options);
  328. return global_options;
  329. }
  330. /** Change the current global options to contain <b>new_val</b> instead of
  331. * their current value; take action based on the new value; free the old value
  332. * as necessary.
  333. */
  334. void
  335. set_options(or_options_t *new_val)
  336. {
  337. or_options_t *old_options = global_options;
  338. global_options = new_val;
  339. /* Note that we pass the *old* options below, for comparison. It
  340. * pulls the new options directly out of global_options. */
  341. if (options_act(old_options) < 0) { /* acting on the options failed. die. */
  342. log_fn(LOG_ERR,"Acting on config options left us in a broken state. Dying.");
  343. exit(1);
  344. }
  345. if (old_options)
  346. config_free(&options_format, old_options);
  347. }
  348. void
  349. config_free_all(void)
  350. {
  351. config_free(&options_format, global_options);
  352. tor_free(torrc_fname);
  353. addr_policy_free(reachable_addr_policy);
  354. reachable_addr_policy = NULL;
  355. }
  356. /** If options->SafeLogging is on, return a not very useful string,
  357. * else return address.
  358. */
  359. const char *
  360. safe_str(const char *address)
  361. {
  362. if (get_options()->SafeLogging)
  363. return "[scrubbed]";
  364. else
  365. return address;
  366. }
  367. /** Fetch the active option list, and take actions based on it. All of the
  368. * things we do should survive being done repeatedly. If present,
  369. * <b>old_options</b> contains the previous value of the options.
  370. *
  371. * Return 0 if all goes well, return -1 if it's time to die.
  372. *
  373. * Note 2: We haven't moved all the "act on new configuration" logic
  374. * here yet. Some is still in do_hup() and other places.
  375. */
  376. static int
  377. options_act(or_options_t *old_options)
  378. {
  379. config_line_t *cl;
  380. char *fn;
  381. size_t len;
  382. or_options_t *options = get_options();
  383. int running_tor = options->command == CMD_RUN_TOR;
  384. static int libevent_initialized = 0;
  385. if (running_tor && options->RunAsDaemon) {
  386. start_daemon();
  387. }
  388. clear_trusted_dir_servers();
  389. for (cl = options->DirServers; cl; cl = cl->next) {
  390. if (parse_dir_server_line(cl->value, 0)<0) {
  391. log_fn(LOG_ERR,
  392. "Bug: Previously validated DirServer line could not be added!");
  393. return -1;
  394. }
  395. }
  396. if (running_tor && rend_config_services(options, 0)<0) {
  397. log_fn(LOG_ERR,
  398. "Bug: Previously validated hidden services line could not be added!");
  399. return -1;
  400. }
  401. if (options->EntryNodes && strlen(options->EntryNodes))
  402. options->UseHelperNodes = 0;
  403. /* Setuid/setgid as appropriate */
  404. if (options->User || options->Group) {
  405. if (switch_id(options->User, options->Group) != 0) {
  406. return -1;
  407. }
  408. }
  409. /* Ensure data directory is private; create if possible. */
  410. if (check_private_dir(options->DataDirectory, CPD_CREATE) != 0) {
  411. log_fn(LOG_ERR, "Couldn't access/create private data directory \"%s\"",
  412. options->DataDirectory);
  413. return -1;
  414. }
  415. if (running_tor) {
  416. len = strlen(options->DataDirectory)+32;
  417. fn = tor_malloc(len);
  418. tor_snprintf(fn, len, "%s/cached-status", options->DataDirectory);
  419. if (check_private_dir(fn, CPD_CREATE) != 0) {
  420. log_fn(LOG_ERR, "Couldn't access/create private data directory \"%s\"",
  421. fn);
  422. tor_free(fn);
  423. return -1;
  424. }
  425. tor_free(fn);
  426. }
  427. /* Bail out at this point if we're not going to be a client or server:
  428. * we want to not fork, and to log stuff to stderr. */
  429. if (options->command != CMD_RUN_TOR)
  430. return 0;
  431. mark_logs_temp(); /* Close current logs once new logs are open. */
  432. if (options_init_logs(options, 0)<0) /* Configure the log(s) */
  433. return -1;
  434. /* Close the temporary log we used while starting up, if it isn't already
  435. * gone. */
  436. close_temp_logs();
  437. add_callback_log(LOG_ERR, LOG_ERR, control_event_logmsg);
  438. control_adjust_event_log_severity();
  439. /* Set up libevent. */
  440. if (running_tor && !libevent_initialized) {
  441. if (init_libevent())
  442. return -1;
  443. libevent_initialized = 1;
  444. }
  445. /* Load state */
  446. if (! global_state)
  447. if (or_state_load())
  448. return -1;
  449. options->_ConnLimit =
  450. set_max_file_descriptors((unsigned)options->ConnLimit, MAXCONNECTIONS);
  451. if (options->_ConnLimit < 0)
  452. return -1;
  453. {
  454. smartlist_t *sl = smartlist_create();
  455. for (cl = options->RedirectExit; cl; cl = cl->next) {
  456. if (parse_redirect_line(sl, cl)<0)
  457. return -1;
  458. }
  459. set_exit_redirects(sl);
  460. }
  461. /* Finish backgrounding the process */
  462. if (running_tor && options->RunAsDaemon) {
  463. /* We may be calling this for the n'th time (on SIGHUP), but it's safe. */
  464. finish_daemon(options->DataDirectory);
  465. }
  466. /* Write our pid to the pid file. If we do not have write permissions we
  467. * will log a warning */
  468. if (running_tor && options->PidFile)
  469. write_pidfile(options->PidFile);
  470. /* Register addressmap directives */
  471. config_register_addressmaps(options);
  472. /* Update address policies. */
  473. parse_socks_policy();
  474. parse_dir_policy();
  475. parse_reachable_addresses();
  476. init_cookie_authentication(options->CookieAuthentication);
  477. /* reload keys as needed for rendezvous services. */
  478. if (rend_service_load_keys()<0) {
  479. log_fn(LOG_ERR,"Error loading rendezvous service keys");
  480. return -1;
  481. }
  482. /* Set up accounting */
  483. if (accounting_parse_options(options, 0)<0) {
  484. log_fn(LOG_ERR,"Error in accounting options");
  485. return -1;
  486. }
  487. if (accounting_is_enabled(options))
  488. configure_accounting(time(NULL));
  489. if (!running_tor)
  490. return 0;
  491. if (!we_are_hibernating() && retry_all_listeners(0) < 0) {
  492. log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
  493. return -1;
  494. }
  495. /* Check for transitions that need action. */
  496. if (old_options) {
  497. if (options_transition_affects_workers(old_options, options)) {
  498. log_fn(LOG_INFO,"Worker-related options changed. Rotating workers.");
  499. cpuworkers_rotate();
  500. dnsworkers_rotate();
  501. }
  502. }
  503. /* Since our options changed, we might need to regenerate and upload our
  504. * server descriptor.
  505. */
  506. if (!old_options || options_transition_affects_descriptor(old_options, options))
  507. mark_my_descriptor_dirty();
  508. return 0;
  509. }
  510. /*
  511. * Functions to parse config options
  512. */
  513. /** If <b>option</b> is an official abbreviation for a longer option,
  514. * return the longer option. Otherwise return <b>option</b>.
  515. * If <b>command_line</b> is set, apply all abbreviations. Otherwise, only
  516. * apply abbreviations that work for the config file and the command line. */
  517. static const char *
  518. expand_abbrev(config_format_t *fmt, const char *option, int command_line)
  519. {
  520. int i;
  521. if (! fmt->abbrevs)
  522. return option;
  523. for (i=0; fmt->abbrevs[i].abbreviated; ++i) {
  524. /* Abbreviations aren't casei. */
  525. if (!strcasecmp(option,fmt->abbrevs[i].abbreviated) &&
  526. (command_line || !fmt->abbrevs[i].commandline_only)) {
  527. return fmt->abbrevs[i].full;
  528. }
  529. }
  530. return option;
  531. }
  532. /** Helper: Read a list of configuration options from the command line. */
  533. static config_line_t *
  534. config_get_commandlines(int argc, char **argv)
  535. {
  536. config_line_t *front = NULL;
  537. config_line_t **new = &front;
  538. char *s;
  539. int i = 1;
  540. while (i < argc-1) {
  541. if (!strcmp(argv[i],"-f") ||
  542. !strcmp(argv[i],"--hash-password")) {
  543. i += 2; /* command-line option with argument. ignore them. */
  544. continue;
  545. } else if (!strcmp(argv[i],"--list-fingerprint")) {
  546. i += 1; /* command-line option. ignore it. */
  547. continue;
  548. } else if (!strcmp(argv[i],"--nt-service")) {
  549. i += 1;
  550. continue;
  551. }
  552. *new = tor_malloc_zero(sizeof(config_line_t));
  553. s = argv[i];
  554. while (*s == '-')
  555. s++;
  556. (*new)->key = tor_strdup(expand_abbrev(&options_format, s, 1));
  557. (*new)->value = tor_strdup(argv[i+1]);
  558. (*new)->next = NULL;
  559. log(LOG_DEBUG,"Commandline: parsed keyword '%s', value '%s'",
  560. (*new)->key, (*new)->value);
  561. new = &((*new)->next);
  562. i += 2;
  563. }
  564. return front;
  565. }
  566. /** Helper: allocate a new configuration option mapping 'key' to 'val',
  567. * append it to *<b>lst</b>. */
  568. static void
  569. config_line_append(config_line_t **lst,
  570. const char *key,
  571. const char *val)
  572. {
  573. config_line_t *newline;
  574. newline = tor_malloc(sizeof(config_line_t));
  575. newline->key = tor_strdup(key);
  576. newline->value = tor_strdup(val);
  577. newline->next = NULL;
  578. while (*lst)
  579. lst = &((*lst)->next);
  580. (*lst) = newline;
  581. }
  582. /** Helper: parse the config string and strdup into key/value
  583. * strings. Set *result to the list, or NULL if parsing the string
  584. * failed. Return 0 on success, -1 on failure. Warn and ignore any
  585. * misformatted lines. */
  586. int
  587. config_get_lines(char *string, config_line_t **result)
  588. {
  589. config_line_t *list = NULL, **next;
  590. char *k, *v;
  591. next = &list;
  592. do {
  593. string = parse_line_from_str(string, &k, &v);
  594. if (!string) {
  595. config_free_lines(list);
  596. return -1;
  597. }
  598. if (k && v) {
  599. /* This list can get long, so we keep a pointer to the end of it
  600. * rather than using config_line_append over and over and getting n^2
  601. * performance. This is the only really long list. */
  602. *next = tor_malloc(sizeof(config_line_t));
  603. (*next)->key = tor_strdup(k);
  604. (*next)->value = tor_strdup(v);
  605. (*next)->next = NULL;
  606. next = &((*next)->next);
  607. }
  608. } while (*string);
  609. *result = list;
  610. return 0;
  611. }
  612. /**
  613. * Free all the configuration lines on the linked list <b>front</b>.
  614. */
  615. void
  616. config_free_lines(config_line_t *front)
  617. {
  618. config_line_t *tmp;
  619. while (front) {
  620. tmp = front;
  621. front = tmp->next;
  622. tor_free(tmp->key);
  623. tor_free(tmp->value);
  624. tor_free(tmp);
  625. }
  626. }
  627. /** DOCDOC */
  628. static const char *
  629. config_find_description(config_format_t *fmt, const char *name)
  630. {
  631. int i;
  632. for (i=0; fmt->descriptions[i].name; ++i) {
  633. if (!strcasecmp(name, fmt->descriptions[i].name))
  634. return fmt->descriptions[i].description;
  635. }
  636. return NULL;
  637. }
  638. /** If <b>key</b> is a configuration option, return the corresponding
  639. * config_var_t. Otherwise, if <b>key</b> is a non-standard abbreviation,
  640. * warn, and return the corresponding config_var_t. Otherwise return NULL.
  641. */
  642. static config_var_t *
  643. config_find_option(config_format_t *fmt, const char *key)
  644. {
  645. int i;
  646. size_t keylen = strlen(key);
  647. if (!keylen)
  648. return NULL; /* if they say "--" on the commandline, it's not an option */
  649. /* First, check for an exact (case-insensitive) match */
  650. for (i=0; fmt->vars[i].name; ++i) {
  651. if (!strcasecmp(key, fmt->vars[i].name)) {
  652. return &fmt->vars[i];
  653. }
  654. }
  655. /* If none, check for an abbreviated match */
  656. for (i=0; fmt->vars[i].name; ++i) {
  657. if (!strncasecmp(key, fmt->vars[i].name, keylen)) {
  658. log_fn(LOG_WARN, "The abbreviation '%s' is deprecated. "
  659. "Please use '%s' instead",
  660. key, fmt->vars[i].name);
  661. return &fmt->vars[i];
  662. }
  663. }
  664. /* Okay, unrecognized options */
  665. return NULL;
  666. }
  667. /** <b>c</b>-\>key is known to be a real key. Update <b>options</b>
  668. * with <b>c</b>-\>value and return 0, or return -1 if bad value.
  669. */
  670. static int
  671. config_assign_value(config_format_t *fmt, or_options_t *options,
  672. config_line_t *c)
  673. {
  674. int i, ok;
  675. config_var_t *var;
  676. void *lvalue;
  677. CHECK(fmt, options);
  678. var = config_find_option(fmt, c->key);
  679. tor_assert(var);
  680. lvalue = ((char*)options) + var->var_offset;
  681. switch (var->type) {
  682. case CONFIG_TYPE_UINT:
  683. i = tor_parse_long(c->value, 10, 0, INT_MAX, &ok, NULL);
  684. if (!ok) {
  685. log(LOG_WARN, "Int keyword '%s %s' is malformed or out of bounds.",
  686. c->key,c->value);
  687. return -1;
  688. }
  689. *(int *)lvalue = i;
  690. break;
  691. case CONFIG_TYPE_INTERVAL: {
  692. i = config_parse_interval(c->value, &ok);
  693. if (!ok) {
  694. return -1;
  695. }
  696. *(int *)lvalue = i;
  697. break;
  698. }
  699. case CONFIG_TYPE_MEMUNIT: {
  700. uint64_t u64 = config_parse_memunit(c->value, &ok);
  701. if (!ok) {
  702. return -1;
  703. }
  704. *(uint64_t *)lvalue = u64;
  705. break;
  706. }
  707. case CONFIG_TYPE_BOOL:
  708. i = tor_parse_long(c->value, 10, 0, 1, &ok, NULL);
  709. if (!ok) {
  710. log(LOG_WARN, "Boolean keyword '%s' expects 0 or 1.", c->key);
  711. return -1;
  712. }
  713. *(int *)lvalue = i;
  714. break;
  715. case CONFIG_TYPE_STRING:
  716. tor_free(*(char **)lvalue);
  717. *(char **)lvalue = tor_strdup(c->value);
  718. break;
  719. case CONFIG_TYPE_DOUBLE:
  720. *(double *)lvalue = atof(c->value);
  721. break;
  722. case CONFIG_TYPE_ISOTIME:
  723. if (parse_iso_time(c->value, (time_t *)lvalue)) {
  724. log(LOG_WARN, "Invalid time '%s' for keyword '%s'", c->value, c->key);
  725. return -1;
  726. }
  727. break;
  728. case CONFIG_TYPE_CSV:
  729. if (*(smartlist_t**)lvalue) {
  730. SMARTLIST_FOREACH(*(smartlist_t**)lvalue, char *, cp, tor_free(cp));
  731. smartlist_clear(*(smartlist_t**)lvalue);
  732. } else {
  733. *(smartlist_t**)lvalue = smartlist_create();
  734. }
  735. smartlist_split_string(*(smartlist_t**)lvalue, c->value, ",",
  736. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  737. break;
  738. case CONFIG_TYPE_LINELIST:
  739. case CONFIG_TYPE_LINELIST_S:
  740. config_line_append((config_line_t**)lvalue, c->key, c->value);
  741. break;
  742. case CONFIG_TYPE_OBSOLETE:
  743. log_fn(LOG_WARN, "Skipping obsolete configuration option '%s'", c->key);
  744. break;
  745. case CONFIG_TYPE_LINELIST_V:
  746. log_fn(LOG_WARN, "Can't provide value for virtual option '%s'", c->key);
  747. return -1;
  748. default:
  749. tor_assert(0);
  750. break;
  751. }
  752. return 0;
  753. }
  754. /** If <b>c</b> is a syntactically valid configuration line, update
  755. * <b>options</b> with its value and return 0. Otherwise return -1 for bad key,
  756. * -2 for bad value.
  757. *
  758. * If 'reset' is set, and we get a line containing no value, restore the
  759. * option to its default value.
  760. */
  761. static int
  762. config_assign_line(config_format_t *fmt, or_options_t *options,
  763. config_line_t *c, int reset)
  764. {
  765. config_var_t *var;
  766. CHECK(fmt, options);
  767. var = config_find_option(fmt, c->key);
  768. if (!var) {
  769. log_fn(LOG_WARN, "Unknown option '%s'. Failing.", c->key);
  770. return -1;
  771. }
  772. /* Put keyword into canonical case. */
  773. if (strcmp(var->name, c->key)) {
  774. tor_free(c->key);
  775. c->key = tor_strdup(var->name);
  776. }
  777. if (!strlen(c->value)) { /* reset or clear it, then return */
  778. if (reset)
  779. option_reset(fmt, options, var);
  780. else
  781. option_clear(fmt, options, var);
  782. return 0;
  783. }
  784. if (config_assign_value(fmt, options, c) < 0)
  785. return -2;
  786. return 0;
  787. }
  788. /** restore the option named <b>key</b> in options to its default value. */
  789. static void
  790. config_reset_line(config_format_t *fmt, or_options_t *options, const char *key)
  791. {
  792. config_var_t *var;
  793. CHECK(fmt, options);
  794. var = config_find_option(fmt, key);
  795. if (!var)
  796. return; /* give error on next pass. */
  797. option_reset(fmt, options, var);
  798. }
  799. /** Return true iff key is a valid configuration option. */
  800. int
  801. option_is_recognized(const char *key)
  802. {
  803. config_var_t *var = config_find_option(&options_format, key);
  804. return (var != NULL);
  805. }
  806. /** Return the canonical name of a configuration option. */
  807. const char *
  808. option_get_canonical_name(const char *key)
  809. {
  810. config_var_t *var = config_find_option(&options_format, key);
  811. return var->name;
  812. }
  813. /** Return a canonicalized list of the options assigned for key.
  814. */
  815. config_line_t *
  816. option_get_assignment(or_options_t *options, const char *key)
  817. {
  818. return get_assigned_option(&options_format, options, key);
  819. }
  820. static config_line_t *
  821. config_lines_dup(const config_line_t *inp)
  822. {
  823. config_line_t *result = NULL;
  824. config_line_t **next_out = &result;
  825. while (inp) {
  826. *next_out = tor_malloc(sizeof(config_line_t));
  827. (*next_out)->key = tor_strdup(inp->key);
  828. (*next_out)->value = tor_strdup(inp->value);
  829. inp = inp->next;
  830. next_out = &((*next_out)->next);
  831. }
  832. (*next_out) = NULL;
  833. return result;
  834. }
  835. static config_line_t *
  836. get_assigned_option(config_format_t *fmt, or_options_t *options, const char *key)
  837. {
  838. config_var_t *var;
  839. const void *value;
  840. char buf[32];
  841. config_line_t *result;
  842. tor_assert(options && key);
  843. CHECK(fmt, options);
  844. var = config_find_option(fmt, key);
  845. if (!var) {
  846. log_fn(LOG_WARN, "Unknown option '%s'. Failing.", key);
  847. return NULL;
  848. } else if (var->type == CONFIG_TYPE_LINELIST_S) {
  849. log_fn(LOG_WARN, "Can't return context-sensitive '%s' on its own", key);
  850. return NULL;
  851. }
  852. value = ((char*)options) + var->var_offset;
  853. if (var->type == CONFIG_TYPE_LINELIST ||
  854. var->type == CONFIG_TYPE_LINELIST_V) {
  855. /* Linelist requires special handling: we just copy and return it. */
  856. return config_lines_dup(*(const config_line_t**)value);
  857. }
  858. result = tor_malloc_zero(sizeof(config_line_t));
  859. result->key = tor_strdup(var->name);
  860. switch (var->type)
  861. {
  862. case CONFIG_TYPE_STRING:
  863. if (*(char**)value) {
  864. result->value = tor_strdup(*(char**)value);
  865. } else {
  866. tor_free(result->key);
  867. tor_free(result);
  868. return NULL;
  869. }
  870. break;
  871. case CONFIG_TYPE_ISOTIME:
  872. if (*(time_t*)value) {
  873. result->value = tor_malloc(ISO_TIME_LEN+1);
  874. format_iso_time(result->value, *(time_t*)value);
  875. } else {
  876. tor_free(result->key);
  877. tor_free(result);
  878. }
  879. break;
  880. case CONFIG_TYPE_INTERVAL:
  881. case CONFIG_TYPE_UINT:
  882. /* This means every or_options_t uint or bool element
  883. * needs to be an int. Not, say, a uint16_t or char. */
  884. tor_snprintf(buf, sizeof(buf), "%d", *(int*)value);
  885. result->value = tor_strdup(buf);
  886. break;
  887. case CONFIG_TYPE_MEMUNIT:
  888. tor_snprintf(buf, sizeof(buf), U64_FORMAT,
  889. U64_PRINTF_ARG(*(uint64_t*)value));
  890. result->value = tor_strdup(buf);
  891. break;
  892. case CONFIG_TYPE_DOUBLE:
  893. tor_snprintf(buf, sizeof(buf), "%f", *(double*)value);
  894. result->value = tor_strdup(buf);
  895. break;
  896. case CONFIG_TYPE_BOOL:
  897. result->value = tor_strdup(*(int*)value ? "1" : "0");
  898. break;
  899. case CONFIG_TYPE_CSV:
  900. if (*(smartlist_t**)value)
  901. result->value = smartlist_join_strings(*(smartlist_t**)value,",",0,NULL);
  902. else
  903. result->value = tor_strdup("");
  904. break;
  905. case CONFIG_TYPE_OBSOLETE:
  906. log_fn(LOG_WARN,"You asked me for the value of an obsolete config option '%s'.", key);
  907. tor_free(result->key);
  908. tor_free(result);
  909. return NULL;
  910. default:
  911. tor_free(result->key);
  912. tor_free(result);
  913. log_fn(LOG_WARN,"Bug: unknown type %d for known key '%s'", var->type, key);
  914. return NULL;
  915. }
  916. return result;
  917. }
  918. /** Iterate through the linked list of requested options <b>list</b>.
  919. * For each item, convert as appropriate and assign to <b>options</b>.
  920. * If an item is unrecognized, return -1 immediately,
  921. * else return 0 for success.
  922. *
  923. * If <b>reset</b>, then interpret empty lines as meaning "restore to
  924. * default value", and interpret LINELIST* options as replacing (not
  925. * extending) their previous values. Otherwise, interpret empty lines
  926. * as meaning "make the value 0 or null".
  927. *
  928. * Return 0 on success, -1 on bad key, -2 on bad value.
  929. */
  930. static int
  931. config_assign(config_format_t *fmt,
  932. void *options, config_line_t *list, int reset)
  933. {
  934. config_line_t *p;
  935. CHECK(fmt, options);
  936. /* pass 1: normalize keys */
  937. for (p = list; p; p = p->next) {
  938. const char *full = expand_abbrev(fmt, p->key, 0);
  939. if (strcmp(full,p->key)) {
  940. tor_free(p->key);
  941. p->key = tor_strdup(full);
  942. }
  943. }
  944. /* pass 2: if we're reading from a resetting source, clear all mentioned
  945. * linelists. */
  946. if (reset) {
  947. for (p = list; p; p = p->next)
  948. config_reset_line(fmt, options, p->key);
  949. }
  950. /* pass 3: assign. */
  951. while (list) {
  952. int r;
  953. if ((r=config_assign_line(fmt, options, list, reset)))
  954. return r;
  955. list = list->next;
  956. }
  957. return 0;
  958. }
  959. /** Try assigning <b>list</b> to the global options. You do this by duping
  960. * options, assigning list to the new one, then validating it. If it's
  961. * ok, then throw out the old one and stick with the new one. Else,
  962. * revert to old and return failure. Return 0 on success, -1 on bad
  963. * keys, -2 on bad values, -3 on bad transition.
  964. */
  965. int
  966. options_trial_assign(config_line_t *list, int reset)
  967. {
  968. int r;
  969. or_options_t *trial_options = options_dup(&options_format, get_options());
  970. if ((r=config_assign(&options_format, trial_options, list, reset)) < 0) {
  971. config_free(&options_format, trial_options);
  972. return r;
  973. }
  974. if (options_validate(trial_options) < 0) {
  975. config_free(&options_format, trial_options);
  976. return -2;
  977. }
  978. if (options_transition_allowed(get_options(), trial_options) < 0) {
  979. config_free(&options_format, trial_options);
  980. return -3;
  981. }
  982. set_options(trial_options); /* we liked it. put it in place. */
  983. return 0;
  984. }
  985. /** Reset config option <b>var</b> to 0, 0.0, "", or the equivalent. */
  986. static void
  987. option_clear(config_format_t *fmt, or_options_t *options, config_var_t *var)
  988. {
  989. void *lvalue = ((char*)options) + var->var_offset;
  990. switch (var->type) {
  991. case CONFIG_TYPE_STRING:
  992. tor_free(*(char**)lvalue);
  993. break;
  994. case CONFIG_TYPE_DOUBLE:
  995. *(double*)lvalue = 0.0;
  996. break;
  997. case CONFIG_TYPE_ISOTIME:
  998. *(time_t*)lvalue = 0;
  999. case CONFIG_TYPE_INTERVAL:
  1000. case CONFIG_TYPE_UINT:
  1001. case CONFIG_TYPE_BOOL:
  1002. *(int*)lvalue = 0;
  1003. break;
  1004. case CONFIG_TYPE_MEMUNIT:
  1005. *(uint64_t*)lvalue = 0;
  1006. break;
  1007. case CONFIG_TYPE_CSV:
  1008. if (*(smartlist_t**)lvalue) {
  1009. SMARTLIST_FOREACH(*(smartlist_t **)lvalue, char *, cp, tor_free(cp));
  1010. smartlist_free(*(smartlist_t **)lvalue);
  1011. *(smartlist_t **)lvalue = NULL;
  1012. }
  1013. break;
  1014. case CONFIG_TYPE_LINELIST:
  1015. case CONFIG_TYPE_LINELIST_S:
  1016. config_free_lines(*(config_line_t **)lvalue);
  1017. *(config_line_t **)lvalue = NULL;
  1018. break;
  1019. case CONFIG_TYPE_LINELIST_V:
  1020. /* handled by linelist_s. */
  1021. break;
  1022. case CONFIG_TYPE_OBSOLETE:
  1023. break;
  1024. }
  1025. }
  1026. /** Replace the option indexed by <b>var</b> in <b>options</b> with its
  1027. * default value. */
  1028. static void
  1029. option_reset(config_format_t *fmt, or_options_t *options, config_var_t *var)
  1030. {
  1031. config_line_t *c;
  1032. void *lvalue;
  1033. CHECK(fmt, options);
  1034. option_clear(fmt, options, var); /* clear it first */
  1035. lvalue = ((char*)options) + var->var_offset;
  1036. if (var->initvalue) {
  1037. c = tor_malloc_zero(sizeof(config_line_t));
  1038. c->key = tor_strdup(var->name);
  1039. c->value = tor_strdup(var->initvalue);
  1040. config_assign_value(fmt, options, c);
  1041. config_free_lines(c);
  1042. }
  1043. }
  1044. /** Set <b>options</b>-&gt;DirServers to contain the default directory
  1045. * servers. */
  1046. static void
  1047. add_default_trusted_dirservers(or_options_t *options)
  1048. {
  1049. /* moria1 */
  1050. config_line_append(&options->DirServers, "DirServer",
  1051. "v1 18.244.0.188:9031 FFCB 46DB 1339 DA84 674C 70D7 CB58 6434 C437 0441");
  1052. /* moria2 */
  1053. config_line_append(&options->DirServers, "DirServer",
  1054. "v1 18.244.0.114:80 719B E45D E224 B607 C537 07D0 E214 3E2D 423E 74CF");
  1055. /* tor26 */
  1056. config_line_append(&options->DirServers, "DirServer",
  1057. "v1 86.59.5.130:80 847B 1F85 0344 D787 6491 A548 92F9 0493 4E4E B85D");
  1058. // "tor.noreply.org:9030 847B 1F85 0344 D787 6491 A548 92F9 0493 4E4E B85D");
  1059. }
  1060. /** Print a usage message for tor. */
  1061. static void
  1062. print_usage(void)
  1063. {
  1064. printf(
  1065. "Copyright 2001-2005 Roger Dingledine, Nick Mathewson.\n\n"
  1066. "tor -f <torrc> [args]\n"
  1067. "See man page for options, or http://tor.eff.org/ for documentation.\n");
  1068. }
  1069. /**
  1070. * Based on <b>options-\>Address</b>, guess our public IP address and put it
  1071. * (in host order) into *<b>addr_out</b>. If <b>hostname_out</b> is provided, set
  1072. * *<b>hostname_out</b> to a new string holding the hostname we used to get
  1073. * the address. Return 0 if all is well, or -1 if we can't find a suitable
  1074. * public IP address.
  1075. */
  1076. int
  1077. resolve_my_address(or_options_t *options, uint32_t *addr_out, char **hostname_out)
  1078. {
  1079. struct in_addr in;
  1080. struct hostent *rent;
  1081. char hostname[256];
  1082. int explicit_ip=1;
  1083. char tmpbuf[INET_NTOA_BUF_LEN];
  1084. static uint32_t old_addr=0;
  1085. const char *address = options->Address;
  1086. tor_assert(addr_out);
  1087. if (address && *address) {
  1088. strlcpy(hostname, address, sizeof(hostname));
  1089. } else { /* then we need to guess our address */
  1090. explicit_ip = 0; /* it's implicit */
  1091. if (gethostname(hostname, sizeof(hostname)) < 0) {
  1092. log_fn(LOG_WARN,"Error obtaining local hostname");
  1093. return -1;
  1094. }
  1095. log_fn(LOG_DEBUG,"Guessed local host name as '%s'",hostname);
  1096. }
  1097. /* now we know hostname. resolve it and keep only the IP */
  1098. if (tor_inet_aton(hostname, &in) == 0) {
  1099. /* then we have to resolve it */
  1100. explicit_ip = 0;
  1101. rent = (struct hostent *)gethostbyname(hostname);
  1102. if (!rent) {
  1103. log_fn(LOG_WARN,"Could not resolve local Address '%s'. Failing.", hostname);
  1104. return -1;
  1105. }
  1106. tor_assert(rent->h_length == 4);
  1107. memcpy(&in.s_addr, rent->h_addr, rent->h_length);
  1108. }
  1109. tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
  1110. if (is_internal_IP(htonl(in.s_addr)) && !options->NoPublish) {
  1111. /* make sure we're ok with publishing an internal IP */
  1112. if (!options->DirServers) {
  1113. /* if they are using the default dirservers, disallow internal IPs always. */
  1114. log_fn(LOG_WARN,"Address '%s' resolves to private IP '%s'. "
  1115. "Tor servers that use the default DirServers must have public IP addresses.",
  1116. hostname, tmpbuf);
  1117. return -1;
  1118. }
  1119. if (!explicit_ip) {
  1120. /* even if they've set their own dirservers, require an explicit IP if
  1121. * they're using an internal address. */
  1122. log_fn(LOG_WARN,"Address '%s' resolves to private IP '%s'. "
  1123. "Please set the Address config option to be the IP you want to use.",
  1124. hostname, tmpbuf);
  1125. return -1;
  1126. }
  1127. }
  1128. log_fn(LOG_DEBUG, "Resolved Address to '%s'.", tmpbuf);
  1129. *addr_out = ntohl(in.s_addr);
  1130. if (old_addr && old_addr != *addr_out) {
  1131. log_fn(LOG_NOTICE,"Your IP seems to have changed. Updating.");
  1132. server_has_changed_ip();
  1133. }
  1134. old_addr = *addr_out;
  1135. if (hostname_out)
  1136. *hostname_out = tor_strdup(hostname);
  1137. return 0;
  1138. }
  1139. /** Called when we don't have a nickname set. Try to guess a good
  1140. * nickname based on the hostname, and return it in a newly allocated string. */
  1141. static char *
  1142. get_default_nickname(void)
  1143. {
  1144. char localhostname[256];
  1145. char *cp, *out, *outp;
  1146. if (gethostname(localhostname, sizeof(localhostname)) < 0) {
  1147. log_fn(LOG_WARN,"Error obtaining local hostname");
  1148. return NULL;
  1149. }
  1150. /* Put it in lowercase; stop at the first dot. */
  1151. for (cp = localhostname; *cp; ++cp) {
  1152. if (*cp == '.') {
  1153. *cp = '\0';
  1154. break;
  1155. }
  1156. *cp = tolower(*cp);
  1157. }
  1158. /* Strip invalid characters. */
  1159. cp = localhostname;
  1160. out = outp = tor_malloc(strlen(localhostname) + 1);
  1161. while (*cp) {
  1162. if (strchr(LEGAL_NICKNAME_CHARACTERS, *cp))
  1163. *outp++ = *cp++;
  1164. else
  1165. cp++;
  1166. }
  1167. *outp = '\0';
  1168. /* Enforce length. */
  1169. if (strlen(out) > MAX_NICKNAME_LEN)
  1170. out[MAX_NICKNAME_LEN]='\0';
  1171. return out;
  1172. }
  1173. /** Release storage held by <b>options</b> */
  1174. static void
  1175. config_free(config_format_t *fmt, void *options)
  1176. {
  1177. int i;
  1178. void *lvalue;
  1179. tor_assert(options);
  1180. for (i=0; fmt->vars[i].name; ++i) {
  1181. lvalue = ((char*)options) + fmt->vars[i].var_offset;
  1182. switch (fmt->vars[i].type) {
  1183. case CONFIG_TYPE_MEMUNIT:
  1184. case CONFIG_TYPE_INTERVAL:
  1185. case CONFIG_TYPE_UINT:
  1186. case CONFIG_TYPE_BOOL:
  1187. case CONFIG_TYPE_DOUBLE:
  1188. case CONFIG_TYPE_ISOTIME:
  1189. case CONFIG_TYPE_OBSOLETE:
  1190. break; /* nothing to free for these config types */
  1191. case CONFIG_TYPE_STRING:
  1192. tor_free(*(char **)lvalue);
  1193. break;
  1194. case CONFIG_TYPE_LINELIST:
  1195. case CONFIG_TYPE_LINELIST_V:
  1196. config_free_lines(*(config_line_t**)lvalue);
  1197. *(config_line_t**)lvalue = NULL;
  1198. break;
  1199. case CONFIG_TYPE_CSV:
  1200. if (*(smartlist_t**)lvalue) {
  1201. SMARTLIST_FOREACH(*(smartlist_t**)lvalue, char *, cp, tor_free(cp));
  1202. smartlist_free(*(smartlist_t**)lvalue);
  1203. *(smartlist_t**)lvalue = NULL;
  1204. }
  1205. break;
  1206. case CONFIG_TYPE_LINELIST_S:
  1207. /* will be freed by corresponding LINELIST_V. */
  1208. break;
  1209. }
  1210. }
  1211. tor_free(options);
  1212. }
  1213. /** Return true iff the option <b>var</b> has the same value in <b>o1</b>
  1214. * and <b>o2</b>. Must not be called for LINELIST_S or OBSOLETE options.
  1215. */
  1216. static int
  1217. option_is_same(config_format_t *fmt,
  1218. or_options_t *o1, or_options_t *o2, const char *name)
  1219. {
  1220. config_line_t *c1, *c2;
  1221. int r = 1;
  1222. CHECK(fmt, o1);
  1223. CHECK(fmt, o2);
  1224. c1 = get_assigned_option(fmt, o1, name);
  1225. c2 = get_assigned_option(fmt, o2, name);
  1226. while (c1 && c2) {
  1227. if (strcasecmp(c1->key, c2->key) ||
  1228. strcmp(c1->value, c2->value)) {
  1229. r = 0;
  1230. break;
  1231. }
  1232. c1 = c1->next;
  1233. c2 = c2->next;
  1234. }
  1235. if (r && (c1 || c2)) {
  1236. r = 0;
  1237. }
  1238. config_free_lines(c1);
  1239. config_free_lines(c2);
  1240. return r;
  1241. }
  1242. /** Copy storage held by <b>old</b> into a new or_options_t and return it. */
  1243. static or_options_t *
  1244. options_dup(config_format_t *fmt, or_options_t *old)
  1245. {
  1246. or_options_t *newopts;
  1247. int i;
  1248. config_line_t *line;
  1249. newopts = config_alloc(fmt);
  1250. for (i=0; fmt->vars[i].name; ++i) {
  1251. if (fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  1252. continue;
  1253. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE)
  1254. continue;
  1255. line = get_assigned_option(fmt, old, fmt->vars[i].name);
  1256. if (line) {
  1257. if (config_assign(fmt, newopts, line, 0) < 0) {
  1258. log_fn(LOG_WARN,"Bug: config_get_assigned_option() generated "
  1259. "something we couldn't config_assign().");
  1260. tor_assert(0);
  1261. }
  1262. }
  1263. config_free_lines(line);
  1264. }
  1265. return newopts;
  1266. }
  1267. /** Return a new empty or_options_t. Used for testing. */
  1268. or_options_t *
  1269. options_new(void)
  1270. {
  1271. return config_alloc(&options_format);
  1272. }
  1273. /** Set <b>options</b> to hold reasonable defaults for most options.
  1274. * Each option defaults to zero. */
  1275. void
  1276. options_init(or_options_t *options)
  1277. {
  1278. config_init(&options_format, options);
  1279. }
  1280. /* DOCDOC */
  1281. static void
  1282. config_init(config_format_t *fmt, void *options)
  1283. {
  1284. int i;
  1285. config_var_t *var;
  1286. CHECK(fmt, options);
  1287. for (i=0; fmt->vars[i].name; ++i) {
  1288. var = &fmt->vars[i];
  1289. if (!var->initvalue)
  1290. continue; /* defaults to NULL or 0 */
  1291. option_reset(fmt, options, var);
  1292. }
  1293. }
  1294. /* DOCDOC */
  1295. static char *
  1296. config_dump(config_format_t *fmt, void *options, int minimal)
  1297. {
  1298. smartlist_t *elements;
  1299. or_options_t *defaults;
  1300. config_line_t *line;
  1301. char *result;
  1302. int i;
  1303. const char *desc;
  1304. defaults = config_alloc(fmt);
  1305. config_init(fmt, defaults);
  1306. fmt->validate_fn(defaults);
  1307. elements = smartlist_create();
  1308. for (i=0; fmt->vars[i].name; ++i) {
  1309. if (fmt->vars[i].type == CONFIG_TYPE_OBSOLETE ||
  1310. fmt->vars[i].type == CONFIG_TYPE_LINELIST_S)
  1311. continue;
  1312. /* Don't save 'hidden' control variables. */
  1313. if (!strcmpstart(fmt->vars[i].name, "__"))
  1314. continue;
  1315. if (minimal && option_is_same(fmt, options, defaults, fmt->vars[i].name))
  1316. continue;
  1317. desc = config_find_description(fmt, fmt->vars[i].name);
  1318. if (desc) {
  1319. size_t len = strlen(desc)+8;
  1320. char *tmp = tor_malloc(len);
  1321. tor_snprintf(tmp, len, "# %s\n",desc);
  1322. smartlist_add(elements, tmp);
  1323. }
  1324. line = get_assigned_option(fmt, options, fmt->vars[i].name);
  1325. for (; line; line = line->next) {
  1326. size_t len = strlen(line->key) + strlen(line->value) + 3;
  1327. char *tmp;
  1328. tmp = tor_malloc(len);
  1329. if (tor_snprintf(tmp, len, "%s %s\n", line->key, line->value)<0) {
  1330. log_fn(LOG_ERR, "Internal error writing log option");
  1331. tor_assert(0);
  1332. }
  1333. smartlist_add(elements, tmp);
  1334. }
  1335. config_free_lines(line);
  1336. }
  1337. result = smartlist_join_strings(elements, "", 0, NULL);
  1338. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  1339. smartlist_free(elements);
  1340. return result;
  1341. }
  1342. /** Return a string containing a possible configuration file that would give
  1343. * the configuration in <b>options</b>. If <b>minimal</b> is true, do not
  1344. * include options that are the same as Tor's defaults.
  1345. */
  1346. char *
  1347. options_dump(or_options_t *options, int minimal)
  1348. {
  1349. return config_dump(&options_format, options, minimal);
  1350. }
  1351. /* Return 0 if every element of sl is a string holding a decimal
  1352. * representation of a port number, or if sl is NULL.
  1353. * Otherwise return -1. */
  1354. static int
  1355. validate_ports_csv(smartlist_t *sl, const char *name)
  1356. {
  1357. int i;
  1358. int result = 0;
  1359. tor_assert(name);
  1360. if (!sl)
  1361. return 0;
  1362. SMARTLIST_FOREACH(sl, const char *, cp,
  1363. {
  1364. i = atoi(cp);
  1365. if (i < 1 || i > 65535) {
  1366. log(LOG_WARN, "Port '%s' out of range in %s", cp, name);
  1367. result=-1;
  1368. }
  1369. });
  1370. return result;
  1371. }
  1372. /** DOCDOC */
  1373. static void
  1374. parse_reachable_addresses(void)
  1375. {
  1376. or_options_t *options = get_options();
  1377. addr_policy_free(reachable_addr_policy);
  1378. reachable_addr_policy = NULL;
  1379. if (config_parse_addr_policy(options->ReachableAddresses,
  1380. &reachable_addr_policy,
  1381. ADDR_POLICY_ACCEPT)) {
  1382. log_fn(LOG_WARN, "Error in ReachableAddresses entry; ignoring.");
  1383. return;
  1384. }
  1385. }
  1386. /** Return true iff the firewall options might block any address:port
  1387. * combination
  1388. */
  1389. int
  1390. firewall_is_fascist(void)
  1391. {
  1392. return reachable_addr_policy ? 1 : 0;
  1393. }
  1394. /** Return true iff we are configured to think that the local fascist
  1395. * firewall (if any) will allow a connection to <b>addr</b>:<b>port</b> */
  1396. int
  1397. fascist_firewall_allows_address(uint32_t addr, uint16_t port)
  1398. {
  1399. addr_policy_result_t p = router_compare_addr_to_addr_policy(
  1400. addr, port, reachable_addr_policy);
  1401. switch (p) {
  1402. case ADDR_POLICY_PROBABLY_ACCEPTED:
  1403. case ADDR_POLICY_ACCEPTED:
  1404. return 1;
  1405. case ADDR_POLICY_PROBABLY_REJECTED:
  1406. case ADDR_POLICY_REJECTED:
  1407. return 0;
  1408. default:
  1409. log_fn(LOG_WARN, "Unexpected result: %d", (int)p);
  1410. return 0;
  1411. }
  1412. }
  1413. /** Return 0 if every setting in <b>options</b> is reasonable. Else
  1414. * warn and return -1. Should have no side effects, except for
  1415. * normalizing the contents of <b>options</b>. */
  1416. static int
  1417. options_validate(or_options_t *options)
  1418. {
  1419. int result = 0;
  1420. config_line_t *cl;
  1421. addr_policy_t *addr_policy=NULL;
  1422. if (options->ORPort < 0 || options->ORPort > 65535) {
  1423. log(LOG_WARN, "ORPort option out of bounds.");
  1424. result = -1;
  1425. }
  1426. if (options->ORPort == 0 && options->ORBindAddress != NULL) {
  1427. log(LOG_WARN, "ORPort must be defined if ORBindAddress is defined.");
  1428. result = -1;
  1429. }
  1430. if (options->DirPort == 0 && options->DirBindAddress != NULL) {
  1431. log(LOG_WARN, "DirPort must be defined if DirBindAddress is defined.");
  1432. result = -1;
  1433. }
  1434. if (options->SocksPort == 0 && options->SocksBindAddress != NULL) {
  1435. log(LOG_WARN, "SocksPort must be defined if SocksBindAddress is defined.");
  1436. result = -1;
  1437. }
  1438. if (validate_data_directory(options)<0) {
  1439. log(LOG_WARN, "Invalid DataDirectory");
  1440. result = -1;
  1441. }
  1442. if (options->Nickname == NULL) {
  1443. if (server_mode(options)) {
  1444. if (!(options->Nickname = get_default_nickname()))
  1445. return -1;
  1446. log_fn(LOG_NOTICE, "Choosing default nickname '%s'", options->Nickname);
  1447. }
  1448. } else {
  1449. if (!is_legal_nickname(options->Nickname)) {
  1450. log_fn(LOG_WARN, "Nickname '%s' is wrong length or contains illegal characters.", options->Nickname);
  1451. result = -1;
  1452. }
  1453. }
  1454. if (server_mode(options) && !options->ContactInfo)
  1455. log_fn(LOG_NOTICE,"Your ContactInfo config option is not set. Please consider setting it, so we can contact you if your server is misconfigured or something else goes wrong.");
  1456. if (normalize_log_options(options))
  1457. return -1;
  1458. /* Special case if no options are given. */
  1459. if (!options->Logs) {
  1460. config_line_append(&options->Logs, "Log", "notice stdout");
  1461. }
  1462. if (options_init_logs(options, 1)<0) /* Validate the log(s) */
  1463. return -1;
  1464. if (server_mode(options)) {
  1465. /* confirm that our address isn't broken, so we can complain now */
  1466. uint32_t tmp;
  1467. if (resolve_my_address(options, &tmp, NULL) < 0)
  1468. result = -1;
  1469. }
  1470. if (options->SocksPort < 0 || options->SocksPort > 65535) {
  1471. log(LOG_WARN, "SocksPort option out of bounds.");
  1472. result = -1;
  1473. }
  1474. if (options->SocksPort == 0 && options->ORPort == 0) {
  1475. log(LOG_WARN, "SocksPort and ORPort are both undefined? Quitting.");
  1476. result = -1;
  1477. }
  1478. if (options->ControlPort < 0 || options->ControlPort > 65535) {
  1479. log(LOG_WARN, "ControlPort option out of bounds.");
  1480. result = -1;
  1481. }
  1482. if (options->DirPort < 0 || options->DirPort > 65535) {
  1483. log(LOG_WARN, "DirPort option out of bounds.");
  1484. result = -1;
  1485. }
  1486. if (options->StrictExitNodes &&
  1487. (!options->ExitNodes || !strlen(options->ExitNodes))) {
  1488. log(LOG_WARN, "StrictExitNodes set, but no ExitNodes listed.");
  1489. }
  1490. if (options->StrictEntryNodes &&
  1491. (!options->EntryNodes || !strlen(options->EntryNodes))) {
  1492. log(LOG_WARN, "StrictEntryNodes set, but no EntryNodes listed.");
  1493. }
  1494. if (options->AuthoritativeDir) {
  1495. if (!options->ContactInfo) {
  1496. log(LOG_WARN, "Authoritative directory servers must set ContactInfo");
  1497. result = -1;
  1498. }
  1499. if (!options->RecommendedVersions) {
  1500. log(LOG_WARN, "Authoritative directory servers must configure RecommendedVersions.");
  1501. result = -1;
  1502. }
  1503. if (!options->RecommendedClientVersions)
  1504. options->RecommendedClientVersions =
  1505. config_lines_dup(options->RecommendedVersions);
  1506. if (!options->RecommendedServerVersions)
  1507. options->RecommendedServerVersions =
  1508. config_lines_dup(options->RecommendedVersions);
  1509. }
  1510. if (options->AuthoritativeDir && !options->DirPort) {
  1511. log(LOG_WARN, "Running as authoritative directory, but no DirPort set.");
  1512. result = -1;
  1513. }
  1514. if (options->AuthoritativeDir && !options->ORPort) {
  1515. log(LOG_WARN, "Running as authoritative directory, but no ORPort set.");
  1516. result = -1;
  1517. }
  1518. if (options->AuthoritativeDir && options->ClientOnly) {
  1519. log(LOG_WARN, "Running as authoritative directory, but ClientOnly also set.");
  1520. result = -1;
  1521. }
  1522. if (options->AuthoritativeDir && options->NoPublish) {
  1523. log(LOG_WARN, "You cannot set both AuthoritativeDir and NoPublish.");
  1524. result = -1;
  1525. }
  1526. if (options->ConnLimit <= 0) {
  1527. log(LOG_WARN, "ConnLimit must be greater than 0, but was set to %d",
  1528. options->ConnLimit);
  1529. result = -1;
  1530. }
  1531. if (options->_AccountingMaxKB) {
  1532. log(LOG_WARN, "AccountingMaxKB is deprecated. Say 'AccountingMax %d KB' instead.", options->_AccountingMaxKB);
  1533. options->AccountingMax = U64_LITERAL(1024)*options->_AccountingMaxKB;
  1534. options->_AccountingMaxKB = 0;
  1535. }
  1536. if (validate_ports_csv(options->FirewallPorts,
  1537. "FirewallPorts") < 0)
  1538. result = -1;
  1539. if (validate_ports_csv(options->LongLivedPorts,
  1540. "LongLivedPorts") < 0)
  1541. result = -1;
  1542. if (options->FascistFirewall) {
  1543. smartlist_t *instead = smartlist_create();
  1544. config_line_t *new_line = tor_malloc_zero(sizeof(config_line_t));
  1545. new_line->key = tor_strdup("ReachableAddresses");
  1546. /* If we're configured with the old format, we need to prepend some
  1547. * open ports. */
  1548. if (!smartlist_len(options->FirewallPorts)) {
  1549. smartlist_add(options->FirewallPorts, tor_strdup("80"));
  1550. smartlist_add(options->FirewallPorts, tor_strdup("443"));
  1551. }
  1552. SMARTLIST_FOREACH(options->FirewallPorts, const char *, portno,
  1553. {
  1554. int p = atoi(portno);
  1555. char *s;
  1556. if (p<0) continue;
  1557. s = tor_malloc(16);
  1558. tor_snprintf(s, 16, "*:%d", p);
  1559. smartlist_add(instead, s);
  1560. });
  1561. new_line->value = smartlist_join_strings(instead,",",0,NULL);
  1562. /* These have been deprecated since 0.1.1.5-alpha-cvs */
  1563. log_fn(LOG_WARN, "FascistFirewall and FirewallPorts are deprecated. Instead, use \"ReachableAddresses %s\"", new_line->value);
  1564. new_line->next = options->ReachableAddresses;
  1565. options->ReachableAddresses = new_line;
  1566. SMARTLIST_FOREACH(instead, char *, cp, tor_free(cp));
  1567. smartlist_free(instead);
  1568. }
  1569. if (options->FascistFirewall || options->ReachableAddresses) {
  1570. /* We need to end with a reject *:*, not an implicit accept *:* */
  1571. config_line_t **linep = &options->ReachableAddresses;
  1572. while (*linep) {
  1573. linep = &((*linep)->next);
  1574. }
  1575. *linep = tor_malloc_zero(sizeof(config_line_t));
  1576. (*linep)->key = tor_strdup("ReachableAddresses");
  1577. (*linep)->value = tor_strdup("reject *:*");
  1578. }
  1579. options->_AllowUnverified = 0;
  1580. if (options->AllowUnverifiedNodes) {
  1581. SMARTLIST_FOREACH(options->AllowUnverifiedNodes, const char *, cp, {
  1582. if (!strcasecmp(cp, "entry"))
  1583. options->_AllowUnverified |= ALLOW_UNVERIFIED_ENTRY;
  1584. else if (!strcasecmp(cp, "exit"))
  1585. options->_AllowUnverified |= ALLOW_UNVERIFIED_EXIT;
  1586. else if (!strcasecmp(cp, "middle"))
  1587. options->_AllowUnverified |= ALLOW_UNVERIFIED_MIDDLE;
  1588. else if (!strcasecmp(cp, "introduction"))
  1589. options->_AllowUnverified |= ALLOW_UNVERIFIED_INTRODUCTION;
  1590. else if (!strcasecmp(cp, "rendezvous"))
  1591. options->_AllowUnverified |= ALLOW_UNVERIFIED_RENDEZVOUS;
  1592. else {
  1593. log(LOG_WARN, "Unrecognized value '%s' in AllowUnverifiedNodes",
  1594. cp);
  1595. result = -1;
  1596. }
  1597. });
  1598. }
  1599. if (options->SocksPort >= 1 &&
  1600. (options->PathlenCoinWeight < 0.0 || options->PathlenCoinWeight >= 1.0)) {
  1601. log(LOG_WARN, "PathlenCoinWeight option must be >=0.0 and <1.0.");
  1602. result = -1;
  1603. }
  1604. #define MIN_DIR_FETCH_PERIOD 600
  1605. #define MIN_REND_POST_PERIOD 300
  1606. #define MIN_STATUS_FETCH_PERIOD 60
  1607. #define MAX_DIR_PERIOD (MIN_ONION_KEY_LIFETIME/2)
  1608. #define MAX_CACHE_DIR_FETCH_PERIOD 3600
  1609. #define MAX_CACHE_STATUS_FETCH_PERIOD 900
  1610. if (options->DirFetchPeriod &&
  1611. options->DirFetchPeriod < MIN_DIR_FETCH_PERIOD) {
  1612. log(LOG_WARN, "DirFetchPeriod option must be at least %d seconds. Clipping.", MIN_DIR_FETCH_PERIOD);
  1613. options->DirFetchPeriod = MIN_DIR_FETCH_PERIOD;
  1614. }
  1615. if (options->StatusFetchPeriod &&
  1616. options->StatusFetchPeriod < MIN_STATUS_FETCH_PERIOD) {
  1617. log(LOG_WARN, "StatusFetchPeriod option must be at least %d seconds. Clipping.", MIN_STATUS_FETCH_PERIOD);
  1618. options->StatusFetchPeriod = MIN_STATUS_FETCH_PERIOD;
  1619. }
  1620. if (options->RendPostPeriod < MIN_REND_POST_PERIOD) {
  1621. log(LOG_WARN,"RendPostPeriod option must be at least %d seconds. Clipping.",
  1622. MIN_REND_POST_PERIOD);
  1623. options->RendPostPeriod = MIN_REND_POST_PERIOD;
  1624. }
  1625. if (options->DirPort && ! options->AuthoritativeDir) {
  1626. if (options->DirFetchPeriod > MAX_CACHE_DIR_FETCH_PERIOD) {
  1627. log(LOG_WARN, "Caching directory servers must have DirFetchPeriod less than %d seconds. Clipping.", MAX_CACHE_DIR_FETCH_PERIOD);
  1628. options->DirFetchPeriod = MAX_CACHE_DIR_FETCH_PERIOD;
  1629. }
  1630. if (options->StatusFetchPeriod > MAX_CACHE_STATUS_FETCH_PERIOD) {
  1631. log(LOG_WARN, "Caching directory servers must have StatusFetchPeriod less than %d seconds. Clipping.", MAX_CACHE_STATUS_FETCH_PERIOD);
  1632. options->StatusFetchPeriod = MAX_CACHE_STATUS_FETCH_PERIOD;
  1633. }
  1634. }
  1635. if (options->DirFetchPeriod > MAX_DIR_PERIOD) {
  1636. log(LOG_WARN, "DirFetchPeriod is too large; clipping to %ds.", MAX_DIR_PERIOD);
  1637. options->DirFetchPeriod = MAX_DIR_PERIOD;
  1638. }
  1639. if (options->StatusFetchPeriod > MAX_DIR_PERIOD) {
  1640. log(LOG_WARN, "StatusFetchPeriod is too large; clipping to %ds.", MAX_DIR_PERIOD);
  1641. options->StatusFetchPeriod = MAX_DIR_PERIOD;
  1642. }
  1643. if (options->RendPostPeriod > MAX_DIR_PERIOD) {
  1644. log(LOG_WARN, "RendPostPeriod is too large; clipping to %ds.", MAX_DIR_PERIOD);
  1645. options->RendPostPeriod = MAX_DIR_PERIOD;
  1646. }
  1647. if (options->KeepalivePeriod < 1) {
  1648. log(LOG_WARN,"KeepalivePeriod option must be positive.");
  1649. result = -1;
  1650. }
  1651. if (options->BandwidthRate > INT_MAX) {
  1652. log(LOG_WARN,"BandwidthRate must be less than %d",INT_MAX);
  1653. result = -1;
  1654. }
  1655. if (options->BandwidthBurst > INT_MAX) {
  1656. log(LOG_WARN,"BandwidthBurst must be less than %d",INT_MAX);
  1657. result = -1;
  1658. }
  1659. if (server_mode(options) &&
  1660. options->BandwidthRate < ROUTER_REQUIRED_MIN_BANDWIDTH*2) {
  1661. log(LOG_WARN,"BandwidthRate is set to %d bytes/second. For servers, it must be at least %d.", (int)options->BandwidthRate, ROUTER_REQUIRED_MIN_BANDWIDTH*2);
  1662. result = -1;
  1663. }
  1664. if (options->BandwidthRate > options->BandwidthBurst) {
  1665. log(LOG_WARN,"BandwidthBurst must be at least equal to BandwidthRate.");
  1666. result = -1;
  1667. }
  1668. if (accounting_parse_options(options, 1)<0) {
  1669. result = -1;
  1670. }
  1671. if (options->HttpProxy) { /* parse it now */
  1672. if (parse_addr_port(options->HttpProxy, NULL,
  1673. &options->HttpProxyAddr, &options->HttpProxyPort) < 0) {
  1674. log(LOG_WARN,"HttpProxy failed to parse or resolve. Please fix.");
  1675. result = -1;
  1676. }
  1677. if (options->HttpProxyPort == 0) { /* give it a default */
  1678. options->HttpProxyPort = 80;
  1679. }
  1680. }
  1681. if (options->HttpProxyAuthenticator) {
  1682. if (strlen(options->HttpProxyAuthenticator) >= 48) {
  1683. log(LOG_WARN, "HttpProxyAuthenticator is too long (>= 48 chars).");
  1684. result = -1;
  1685. }
  1686. }
  1687. if (options->HttpsProxy) { /* parse it now */
  1688. if (parse_addr_port(options->HttpsProxy, NULL,
  1689. &options->HttpsProxyAddr, &options->HttpsProxyPort) < 0) {
  1690. log(LOG_WARN,"HttpsProxy failed to parse or resolve. Please fix.");
  1691. result = -1;
  1692. }
  1693. if (options->HttpsProxyPort == 0) { /* give it a default */
  1694. options->HttpsProxyPort = 443;
  1695. }
  1696. }
  1697. if (options->HttpsProxyAuthenticator) {
  1698. if (strlen(options->HttpsProxyAuthenticator) >= 48) {
  1699. log(LOG_WARN, "HttpsProxyAuthenticator is too long (>= 48 chars).");
  1700. result = -1;
  1701. }
  1702. }
  1703. if (options->HashedControlPassword) {
  1704. if (decode_hashed_password(NULL, options->HashedControlPassword)<0) {
  1705. log_fn(LOG_WARN,"Bad HashedControlPassword: wrong length or bad encoding");
  1706. result = -1;
  1707. }
  1708. }
  1709. if (options->HashedControlPassword && options->CookieAuthentication) {
  1710. log_fn(LOG_WARN,"Cannot enable both HashedControlPassword and CookieAuthentication");
  1711. result = -1;
  1712. }
  1713. if (options->UseHelperNodes && ! options->NumHelperNodes) {
  1714. log_fn(LOG_WARN, "Cannot enable UseHelperNodes with NumHelperNodes set to 0");
  1715. result = -1;
  1716. }
  1717. if (check_nickname_list(options->ExitNodes, "ExitNodes"))
  1718. result = -1;
  1719. if (check_nickname_list(options->EntryNodes, "EntryNodes"))
  1720. result = -1;
  1721. if (check_nickname_list(options->ExcludeNodes, "ExcludeNodes"))
  1722. result = -1;
  1723. if (check_nickname_list(options->RendNodes, "RendNodes"))
  1724. result = -1;
  1725. if (check_nickname_list(options->RendNodes, "RendExcludeNodes"))
  1726. result = -1;
  1727. if (check_nickname_list(options->MyFamily, "MyFamily"))
  1728. result = -1;
  1729. for (cl = options->NodeFamilies; cl; cl = cl->next) {
  1730. if (check_nickname_list(cl->value, "NodeFamily"))
  1731. result = -1;
  1732. }
  1733. if (config_parse_addr_policy(options->ExitPolicy, &addr_policy, -1)) {
  1734. log_fn(LOG_WARN, "Error in Exit Policy entry.");
  1735. result = -1;
  1736. }
  1737. options_append_default_exit_policy(&addr_policy);
  1738. if (server_mode(options)) {
  1739. exit_policy_implicitly_allows_local_networks(addr_policy, 1);
  1740. }
  1741. /* The rest of these calls *append* to addr_policy. So don't actually
  1742. * use the results for anything other than checking if they parse! */
  1743. if (config_parse_addr_policy(options->DirPolicy, &addr_policy, -1)) {
  1744. log_fn(LOG_WARN, "Error in DirPolicy entry.");
  1745. result = -1;
  1746. }
  1747. if (config_parse_addr_policy(options->SocksPolicy, &addr_policy, -1)) {
  1748. log_fn(LOG_WARN, "Error in SocksPolicy entry.");
  1749. result = -1;
  1750. }
  1751. if (config_parse_addr_policy(options->ReachableAddresses, &addr_policy,
  1752. ADDR_POLICY_ACCEPT)) {
  1753. log_fn(LOG_WARN, "Error in ReachableAddresses entry.");
  1754. result = -1;
  1755. }
  1756. addr_policy_free(addr_policy);
  1757. for (cl = options->RedirectExit; cl; cl = cl->next) {
  1758. if (parse_redirect_line(NULL, cl)<0)
  1759. result = -1;
  1760. }
  1761. if (!options->DirServers) {
  1762. add_default_trusted_dirservers(options);
  1763. } else {
  1764. for (cl = options->DirServers; cl; cl = cl->next) {
  1765. if (parse_dir_server_line(cl->value, 1)<0)
  1766. result = -1;
  1767. }
  1768. }
  1769. if (rend_config_services(options, 1) < 0)
  1770. result = -1;
  1771. return result;
  1772. }
  1773. /** Helper: return true iff s1 and s2 are both NULL, or both non-NULL
  1774. * equal strings. */
  1775. static int
  1776. opt_streq(const char *s1, const char *s2)
  1777. {
  1778. if (!s1 && !s2)
  1779. return 1;
  1780. else if (s1 && s2 && !strcmp(s1,s2))
  1781. return 1;
  1782. else
  1783. return 0;
  1784. }
  1785. /** Check if any of the previous options have changed but aren't allowed to. */
  1786. static int
  1787. options_transition_allowed(or_options_t *old, or_options_t *new_val)
  1788. {
  1789. if (!old)
  1790. return 0;
  1791. if (!opt_streq(old->PidFile, new_val->PidFile)) {
  1792. log_fn(LOG_WARN,"PidFile is not allowed to change. Failing.");
  1793. return -1;
  1794. }
  1795. if (old->RunAsDaemon != new_val->RunAsDaemon) {
  1796. log_fn(LOG_WARN,"While Tor is running, changing RunAsDaemon is not allowed. Failing.");
  1797. return -1;
  1798. }
  1799. if (strcmp(old->DataDirectory,new_val->DataDirectory)!=0) {
  1800. log_fn(LOG_WARN,"While Tor is running, changing DataDirectory (\"%s\"->\"%s\") is not allowed. Failing.", old->DataDirectory, new_val->DataDirectory);
  1801. return -1;
  1802. }
  1803. if (!opt_streq(old->User, new_val->User)) {
  1804. log_fn(LOG_WARN,"While Tor is running, changing User is not allowed. Failing.");
  1805. return -1;
  1806. }
  1807. if (!opt_streq(old->Group, new_val->Group)) {
  1808. log_fn(LOG_WARN,"While Tor is running, changing Group is not allowed. Failing.");
  1809. return -1;
  1810. }
  1811. if (old->HardwareAccel != new_val->HardwareAccel) {
  1812. log_fn(LOG_WARN,"While Tor is running, changing HardwareAccel is not allowed. Failing.");
  1813. return -1;
  1814. }
  1815. return 0;
  1816. }
  1817. /** Return true iff a and b contain identical keys and values in identical
  1818. * order. */
  1819. static int
  1820. config_lines_eq(config_line_t *a, config_line_t *b)
  1821. {
  1822. while (a && b) {
  1823. if (strcasecmp(a->key, b->key) || strcmp(a->value, b->value))
  1824. return 0;
  1825. a = a->next;
  1826. b = b->next;
  1827. }
  1828. if (a || b)
  1829. return 0;
  1830. return 1;
  1831. }
  1832. /** Return 1 if any change from <b>old_options</b> to <b>new_options</b>
  1833. * will require us to rotate the cpu and dns workers; else return 0. */
  1834. static int
  1835. options_transition_affects_workers(or_options_t *old_options,
  1836. or_options_t *new_options)
  1837. {
  1838. if (!opt_streq(old_options->DataDirectory, new_options->DataDirectory) ||
  1839. old_options->NumCpus != new_options->NumCpus ||
  1840. old_options->ORPort != new_options->ORPort ||
  1841. old_options->SafeLogging != new_options->SafeLogging ||
  1842. !config_lines_eq(old_options->Logs, new_options->Logs))
  1843. return 1;
  1844. /* Check whether log options match. */
  1845. /* Nothing that changed matters. */
  1846. return 0;
  1847. }
  1848. /** Return 1 if any change from <b>old_options</b> to <b>new_options</b>
  1849. * will require us to generate a new descriptor; else return 0. */
  1850. static int
  1851. options_transition_affects_descriptor(or_options_t *old_options,
  1852. or_options_t *new_options)
  1853. {
  1854. if (!opt_streq(old_options->DataDirectory, new_options->DataDirectory) ||
  1855. !opt_streq(old_options->Nickname,new_options->Nickname) ||
  1856. !opt_streq(old_options->Address,new_options->Address) ||
  1857. !config_lines_eq(old_options->ExitPolicy,new_options->ExitPolicy) ||
  1858. old_options->ORPort != new_options->ORPort ||
  1859. old_options->DirPort != new_options->DirPort ||
  1860. old_options->ClientOnly != new_options->ClientOnly ||
  1861. old_options->NoPublish != new_options->NoPublish ||
  1862. old_options->BandwidthRate != new_options->BandwidthRate ||
  1863. old_options->BandwidthBurst != new_options->BandwidthBurst ||
  1864. !opt_streq(old_options->ContactInfo, new_options->ContactInfo) ||
  1865. !opt_streq(old_options->MyFamily, new_options->MyFamily) ||
  1866. !opt_streq(old_options->AccountingStart, new_options->AccountingStart) ||
  1867. old_options->AccountingMax != new_options->AccountingMax)
  1868. return 1;
  1869. return 0;
  1870. }
  1871. #ifdef MS_WINDOWS
  1872. /** Return the directory on windows where we expect to find our application
  1873. * data. */
  1874. static char *
  1875. get_windows_conf_root(void)
  1876. {
  1877. static int is_set = 0;
  1878. static char path[MAX_PATH+1];
  1879. LPITEMIDLIST idl;
  1880. IMalloc *m;
  1881. HRESULT result;
  1882. if (is_set)
  1883. return path;
  1884. /* Find X:\documents and settings\username\application data\ .
  1885. * We would use SHGetSpecialFolder path, but that wasn't added until IE4.
  1886. */
  1887. if (!SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA,
  1888. &idl))) {
  1889. GetCurrentDirectory(MAX_PATH, path);
  1890. is_set = 1;
  1891. log_fn(LOG_WARN, "I couldn't find your application data folder: are you running an ancient version of Windows 95? Defaulting to \"%s\"", path);
  1892. return path;
  1893. }
  1894. /* Convert the path from an "ID List" (whatever that is!) to a path. */
  1895. result = SHGetPathFromIDList(idl, path);
  1896. /* Now we need to free the */
  1897. SHGetMalloc(&m);
  1898. if (m) {
  1899. m->lpVtbl->Free(m, idl);
  1900. m->lpVtbl->Release(m);
  1901. }
  1902. if (!SUCCEEDED(result)) {
  1903. return NULL;
  1904. }
  1905. strlcat(path,"\\tor",MAX_PATH);
  1906. is_set = 1;
  1907. return path;
  1908. }
  1909. #endif
  1910. /** Return the default location for our torrc file. */
  1911. static const char *
  1912. get_default_conf_file(void)
  1913. {
  1914. #ifdef MS_WINDOWS
  1915. static char path[MAX_PATH+1];
  1916. strlcpy(path, get_windows_conf_root(), MAX_PATH);
  1917. strlcat(path,"\\torrc",MAX_PATH);
  1918. return path;
  1919. #else
  1920. return (CONFDIR "/torrc");
  1921. #endif
  1922. }
  1923. /** Verify whether lst is a string containing valid-looking space-separated
  1924. * nicknames, or NULL. Return 0 on success. Warn and return -1 on failure.
  1925. */
  1926. static int
  1927. check_nickname_list(const char *lst, const char *name)
  1928. {
  1929. int r = 0;
  1930. smartlist_t *sl;
  1931. if (!lst)
  1932. return 0;
  1933. sl = smartlist_create();
  1934. smartlist_split_string(sl, lst, ",", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1935. SMARTLIST_FOREACH(sl, const char *, s,
  1936. {
  1937. if (!is_legal_nickname_or_hexdigest(s)) {
  1938. log_fn(LOG_WARN, "Invalid nickname '%s' in %s line", s, name);
  1939. r = -1;
  1940. }
  1941. });
  1942. SMARTLIST_FOREACH(sl, char *, s, tor_free(s));
  1943. smartlist_free(sl);
  1944. return r;
  1945. }
  1946. /** Read a configuration file into <b>options</b>, finding the configuration
  1947. * file location based on the command line. After loading the options,
  1948. * validate them for consistency, then take actions based on them.
  1949. * Return 0 if success, -1 if failure. */
  1950. int
  1951. options_init_from_torrc(int argc, char **argv)
  1952. {
  1953. or_options_t *oldoptions, *newoptions;
  1954. config_line_t *cl;
  1955. char *cf=NULL, *fname=NULL;
  1956. int i, retval;
  1957. int using_default_torrc;
  1958. static char **backup_argv;
  1959. static int backup_argc;
  1960. if (argv) { /* first time we're called. save commandline args */
  1961. backup_argv = argv;
  1962. backup_argc = argc;
  1963. oldoptions = NULL;
  1964. } else { /* we're reloading. need to clean up old options first. */
  1965. argv = backup_argv;
  1966. argc = backup_argc;
  1967. oldoptions = get_options();
  1968. }
  1969. if (argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1],"--help"))) {
  1970. print_usage();
  1971. exit(0);
  1972. }
  1973. if (argc > 1 && (!strcmp(argv[1],"--version"))) {
  1974. printf("Tor version %s.\n",VERSION);
  1975. if (argc > 2 && (!strcmp(argv[2],"--version"))) {
  1976. print_cvs_version();
  1977. }
  1978. exit(0);
  1979. }
  1980. newoptions = tor_malloc_zero(sizeof(or_options_t));
  1981. newoptions->_magic = OR_OPTIONS_MAGIC;
  1982. options_init(newoptions);
  1983. /* learn config file name, get config lines, assign them */
  1984. fname = NULL;
  1985. using_default_torrc = 1;
  1986. newoptions->command = CMD_RUN_TOR;
  1987. for (i = 1; i < argc; ++i) {
  1988. if (i < argc-1 && !strcmp(argv[i],"-f")) {
  1989. if (fname) {
  1990. log(LOG_WARN, "Duplicate -f options on command line.");
  1991. tor_free(fname);
  1992. }
  1993. fname = tor_strdup(argv[i+1]);
  1994. using_default_torrc = 0;
  1995. ++i;
  1996. } else if (!strcmp(argv[i],"--list-fingerprint")) {
  1997. newoptions->command = CMD_LIST_FINGERPRINT;
  1998. } else if (!strcmp(argv[i],"--hash-password")) {
  1999. newoptions->command = CMD_HASH_PASSWORD;
  2000. newoptions->command_arg = tor_strdup( (i < argc-1) ? argv[i+1] : "");
  2001. ++i;
  2002. } else if (!strcmp(argv[i],"--verify-config")) {
  2003. newoptions->command = CMD_VERIFY_CONFIG;
  2004. }
  2005. }
  2006. if (using_default_torrc) {
  2007. /* didn't find one, try CONFDIR */
  2008. const char *dflt = get_default_conf_file();
  2009. char *fn = NULL;
  2010. if (dflt && file_status(dflt) == FN_FILE) {
  2011. fname = tor_strdup(dflt);
  2012. } else {
  2013. #ifndef MS_WINDOWS
  2014. fn = expand_filename("~/.torrc");
  2015. if (fn && file_status(fn) == FN_FILE) {
  2016. fname = fn;
  2017. } else {
  2018. tor_free(fn);
  2019. fname = tor_strdup(dflt);
  2020. }
  2021. #else
  2022. fname = tor_strdup(dflt);
  2023. #endif
  2024. }
  2025. }
  2026. tor_assert(fname);
  2027. log(LOG_DEBUG, "Opening config file \"%s\"", fname);
  2028. if (file_status(fname) != FN_FILE ||
  2029. !(cf = read_file_to_str(fname,0))) {
  2030. if (using_default_torrc == 1) {
  2031. log(LOG_NOTICE, "Configuration file \"%s\" not present, "
  2032. "using reasonable defaults.", fname);
  2033. tor_free(fname); /* sets fname to NULL */
  2034. } else {
  2035. log(LOG_WARN, "Unable to open configuration file \"%s\".", fname);
  2036. tor_free(fname);
  2037. goto err;
  2038. }
  2039. } else { /* it opened successfully. use it. */
  2040. retval = config_get_lines(cf, &cl);
  2041. tor_free(cf);
  2042. if (retval < 0)
  2043. goto err;
  2044. retval = config_assign(&options_format, newoptions, cl, 0);
  2045. config_free_lines(cl);
  2046. if (retval < 0)
  2047. goto err;
  2048. }
  2049. /* Go through command-line variables too */
  2050. cl = config_get_commandlines(argc, argv);
  2051. retval = config_assign(&options_format, newoptions, cl, 0);
  2052. config_free_lines(cl);
  2053. if (retval < 0)
  2054. goto err;
  2055. /* Validate newoptions */
  2056. if (options_validate(newoptions) < 0)
  2057. goto err;
  2058. if (options_transition_allowed(oldoptions, newoptions) < 0)
  2059. goto err;
  2060. set_options(newoptions); /* frees and replaces old options */
  2061. tor_free(torrc_fname);
  2062. torrc_fname = fname;
  2063. return 0;
  2064. err:
  2065. tor_free(fname);
  2066. config_free(&options_format, newoptions);
  2067. return -1;
  2068. }
  2069. /** Return the location for our configuration file.
  2070. */
  2071. const char *
  2072. get_torrc_fname(void)
  2073. {
  2074. if (torrc_fname)
  2075. return torrc_fname;
  2076. else
  2077. return get_default_conf_file();
  2078. }
  2079. /** Adjust the address map mased on the MapAddress elements in the
  2080. * configuration <b>options</b>
  2081. */
  2082. static void
  2083. config_register_addressmaps(or_options_t *options)
  2084. {
  2085. smartlist_t *elts;
  2086. config_line_t *opt;
  2087. char *from, *to;
  2088. addressmap_clear_configured();
  2089. elts = smartlist_create();
  2090. for (opt = options->AddressMap; opt; opt = opt->next) {
  2091. smartlist_split_string(elts, opt->value, NULL,
  2092. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  2093. if (smartlist_len(elts) >= 2) {
  2094. from = smartlist_get(elts,0);
  2095. to = smartlist_get(elts,1);
  2096. if (!is_plausible_address(from)) {
  2097. log_fn(LOG_WARN,"Skipping invalid argument '%s' to MapAddress",from);
  2098. } else if (!is_plausible_address(to)) {
  2099. log_fn(LOG_WARN,"Skipping invalid argument '%s' to MapAddress",to);
  2100. } else {
  2101. addressmap_register(from, tor_strdup(to), 0);
  2102. if (smartlist_len(elts)>2) {
  2103. log_fn(LOG_WARN,"Ignoring extra arguments to MapAddress.");
  2104. }
  2105. }
  2106. } else {
  2107. log_fn(LOG_WARN,"MapAddress '%s' has too few arguments. Ignoring.", opt->value);
  2108. }
  2109. SMARTLIST_FOREACH(elts, char*, cp, tor_free(cp));
  2110. smartlist_clear(elts);
  2111. }
  2112. smartlist_free(elts);
  2113. }
  2114. /** If <b>range</b> is of the form MIN-MAX, for MIN and MAX both
  2115. * recognized log severity levels, set *<b>min_out</b> to MIN and
  2116. * *<b>max_out</b> to MAX and return 0. Else, if <b>range</b> is of
  2117. * the form MIN, act as if MIN-err had been specified. Else, warn and
  2118. * return -1.
  2119. */
  2120. static int
  2121. parse_log_severity_range(const char *range, int *min_out, int *max_out)
  2122. {
  2123. int levelMin, levelMax;
  2124. const char *cp;
  2125. cp = strchr(range, '-');
  2126. if (cp) {
  2127. if (cp == range) {
  2128. levelMin = LOG_DEBUG;
  2129. } else {
  2130. char *tmp_sev = tor_strndup(range, cp - range);
  2131. levelMin = parse_log_level(tmp_sev);
  2132. if (levelMin < 0) {
  2133. log_fn(LOG_WARN, "Unrecognized log severity '%s': must be one of "
  2134. "err|warn|notice|info|debug", tmp_sev);
  2135. tor_free(tmp_sev);
  2136. return -1;
  2137. }
  2138. tor_free(tmp_sev);
  2139. }
  2140. if (!*(cp+1)) {
  2141. levelMax = LOG_ERR;
  2142. } else {
  2143. levelMax = parse_log_level(cp+1);
  2144. if (levelMax < 0) {
  2145. log_fn(LOG_WARN, "Unrecognized log severity '%s': must be one of "
  2146. "err|warn|notice|info|debug", cp+1);
  2147. return -1;
  2148. }
  2149. }
  2150. } else {
  2151. levelMin = parse_log_level(range);
  2152. if (levelMin < 0) {
  2153. log_fn(LOG_WARN, "Unrecognized log severity '%s': must be one of "
  2154. "err|warn|notice|info|debug", range);
  2155. return -1;
  2156. }
  2157. levelMax = LOG_ERR;
  2158. }
  2159. *min_out = levelMin;
  2160. *max_out = levelMax;
  2161. return 0;
  2162. }
  2163. /** Try to convert a pair of old-style logging options [LogLevel, and
  2164. * (LogFile/Syslog)] to a new-style option, and add the new option to
  2165. * options->Logs. */
  2166. static int
  2167. convert_log_option(or_options_t *options, config_line_t *level_opt,
  2168. config_line_t *file_opt, int isDaemon)
  2169. {
  2170. int levelMin = -1, levelMax = -1;
  2171. if (level_opt) {
  2172. if (parse_log_severity_range(level_opt->value, &levelMin, &levelMax))
  2173. return -1;
  2174. }
  2175. if (levelMin < 0 && levelMax < 0) {
  2176. levelMin = LOG_NOTICE;
  2177. levelMax = LOG_ERR;
  2178. } else if (levelMin < 0) {
  2179. levelMin = levelMax;
  2180. } else {
  2181. levelMax = LOG_ERR;
  2182. }
  2183. if (file_opt && !strcasecmp(file_opt->key, "LogFile")) {
  2184. if (add_single_log_option(options, levelMin, levelMax, "file", file_opt->value) < 0) {
  2185. log_fn(LOG_WARN, "Cannot write to LogFile \"%s\": %s.", file_opt->value,
  2186. strerror(errno));
  2187. return -1;
  2188. }
  2189. } else if (file_opt && !strcasecmp(file_opt->key, "SysLog")) {
  2190. if (add_single_log_option(options, levelMin, levelMax, "syslog", NULL) < 0)
  2191. return -1;
  2192. } else if (!isDaemon) {
  2193. add_single_log_option(options, levelMin, levelMax, "stdout", NULL);
  2194. }
  2195. return 0;
  2196. }
  2197. /**
  2198. * Initialize the logs based on the configuration file.
  2199. */
  2200. int
  2201. options_init_logs(or_options_t *options, int validate_only)
  2202. {
  2203. config_line_t *opt;
  2204. int ok;
  2205. smartlist_t *elts;
  2206. ok = 1;
  2207. elts = smartlist_create();
  2208. for (opt = options->Logs; opt; opt = opt->next) {
  2209. int levelMin=LOG_DEBUG, levelMax=LOG_ERR;
  2210. smartlist_split_string(elts, opt->value, NULL,
  2211. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
  2212. if (smartlist_len(elts) == 0) {
  2213. log_fn(LOG_WARN, "Bad syntax on Log option 'Log %s'", opt->value);
  2214. ok = 0; goto cleanup;
  2215. }
  2216. if (parse_log_severity_range(smartlist_get(elts,0), &levelMin, &levelMax)) {
  2217. ok = 0; goto cleanup;
  2218. }
  2219. if (smartlist_len(elts) < 2) { /* only loglevels were provided */
  2220. if (!validate_only)
  2221. add_stream_log(levelMin, levelMax, "<stdout>", stdout);
  2222. goto cleanup;
  2223. }
  2224. if (!strcasecmp(smartlist_get(elts,1), "file")) {
  2225. if (smartlist_len(elts) != 3) {
  2226. log_fn(LOG_WARN, "Bad syntax on Log option 'Log %s'", opt->value);
  2227. ok = 0; goto cleanup;
  2228. }
  2229. if (!validate_only)
  2230. add_file_log(levelMin, levelMax, smartlist_get(elts, 2));
  2231. goto cleanup;
  2232. }
  2233. if (smartlist_len(elts) != 2) {
  2234. log_fn(LOG_WARN, "Bad syntax on Log option 'Log %s'", opt->value);
  2235. ok = 0; goto cleanup;
  2236. }
  2237. if (!strcasecmp(smartlist_get(elts,1), "stdout")) {
  2238. if (!validate_only) {
  2239. add_stream_log(levelMin, levelMax, "<stdout>", stdout);
  2240. close_temp_logs();
  2241. }
  2242. } else if (!strcasecmp(smartlist_get(elts,1), "stderr")) {
  2243. if (!validate_only) {
  2244. add_stream_log(levelMin, levelMax, "<stderr>", stderr);
  2245. close_temp_logs();
  2246. }
  2247. } else if (!strcasecmp(smartlist_get(elts,1), "syslog")) {
  2248. #ifdef HAVE_SYSLOG_H
  2249. if (!validate_only)
  2250. add_syslog_log(levelMin, levelMax);
  2251. #else
  2252. log_fn(LOG_WARN, "Syslog is not supported in this compilation.");
  2253. #endif
  2254. } else {
  2255. log_fn(LOG_WARN, "Unrecognized log type %s",
  2256. (const char*)smartlist_get(elts,1));
  2257. if (strchr(smartlist_get(elts,1), '/')) {
  2258. log_fn(LOG_WARN, "Did you mean to say 'Log file %s' ?",
  2259. (const char *)smartlist_get(elts,1));
  2260. }
  2261. ok = 0; goto cleanup;
  2262. }
  2263. cleanup:
  2264. SMARTLIST_FOREACH(elts, char*, cp, tor_free(cp));
  2265. smartlist_clear(elts);
  2266. }
  2267. smartlist_free(elts);
  2268. if (!validate_only)
  2269. close_temp_logs();
  2270. return ok?0:-1;
  2271. }
  2272. /** Add a single option of the form Log min-max \<type\> [fname] to options. */
  2273. static int
  2274. add_single_log_option(or_options_t *options, int minSeverity, int maxSeverity,
  2275. const char *type, const char *fname)
  2276. {
  2277. char buf[512];
  2278. int n;
  2279. n = tor_snprintf(buf, sizeof(buf), "%s%s%s %s%s%s",
  2280. log_level_to_string(minSeverity),
  2281. maxSeverity == LOG_ERR ? "" : "-",
  2282. maxSeverity == LOG_ERR ? "" : log_level_to_string(maxSeverity),
  2283. type, fname?" ":"", fname?fname:"");
  2284. if (n<0) {
  2285. log_fn(LOG_WARN, "Normalized log option too long.");
  2286. return -1;
  2287. }
  2288. log(LOG_WARN, "The old LogLevel/LogFile/DebugLogFile/SysLog options are deprecated, and will go away soon. Your new torrc line should be: 'Log %s'", buf);
  2289. config_line_append(&options->Logs, "Log", buf);
  2290. return 0;
  2291. }
  2292. /** Convert all old-style logging options to new-style Log options. Return 0
  2293. * on success, -1 on failure. */
  2294. static int
  2295. normalize_log_options(or_options_t *options)
  2296. {
  2297. /* The order of options is: Level? (File Level?)+
  2298. */
  2299. config_line_t *opt = options->OldLogOptions;
  2300. /* Special case for if first option is LogLevel. */
  2301. if (opt && !strcasecmp(opt->key, "LogLevel")) {
  2302. if (opt->next && (!strcasecmp(opt->next->key, "LogFile") ||
  2303. !strcasecmp(opt->next->key, "SysLog"))) {
  2304. if (convert_log_option(options, opt, opt->next, options->RunAsDaemon) < 0)
  2305. return -1;
  2306. opt = opt->next->next;
  2307. } else if (!opt->next) {
  2308. if (convert_log_option(options, opt, NULL, options->RunAsDaemon) < 0)
  2309. return -1;
  2310. opt = opt->next;
  2311. } else {
  2312. ; /* give warning below */
  2313. }
  2314. }
  2315. while (opt) {
  2316. if (!strcasecmp(opt->key, "LogLevel")) {
  2317. log_fn(LOG_WARN, "Two LogLevel options in a row without intervening LogFile or SysLog");
  2318. opt = opt->next;
  2319. } else {
  2320. tor_assert(!strcasecmp(opt->key, "LogFile") ||
  2321. !strcasecmp(opt->key, "SysLog"));
  2322. if (opt->next && !strcasecmp(opt->next->key, "LogLevel")) {
  2323. /* LogFile/SysLog followed by LogLevel */
  2324. if (convert_log_option(options,opt->next,opt, options->RunAsDaemon) < 0)
  2325. return -1;
  2326. opt = opt->next->next;
  2327. } else {
  2328. /* LogFile/SysLog followed by LogFile/SysLog or end of list. */
  2329. if (convert_log_option(options,NULL, opt, options->RunAsDaemon) < 0)
  2330. return -1;
  2331. opt = opt->next;
  2332. }
  2333. }
  2334. }
  2335. if (options->DebugLogFile) {
  2336. if (add_single_log_option(options, LOG_DEBUG, LOG_ERR, "file", options->DebugLogFile) < 0)
  2337. return -1;
  2338. }
  2339. tor_free(options->DebugLogFile);
  2340. config_free_lines(options->OldLogOptions);
  2341. options->OldLogOptions = NULL;
  2342. return 0;
  2343. }
  2344. #define DEFAULT_EXIT_POLICY "reject 0.0.0.0/8,reject 169.254.0.0/16,reject 127.0.0.0/8,reject 192.168.0.0/16,reject 10.0.0.0/8,reject 172.16.0.0/12,reject *:25,reject *:119,reject *:135-139,reject *:445,reject *:465,reject *:587,reject *:1214,reject *:4661-4666,reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
  2345. /** Add the default exit policy entries to <b>policy</b>
  2346. */
  2347. void
  2348. options_append_default_exit_policy(addr_policy_t **policy)
  2349. {
  2350. config_line_t tmp;
  2351. addr_policy_t *ap;
  2352. tmp.key = NULL;
  2353. tmp.value = (char*)DEFAULT_EXIT_POLICY;
  2354. tmp.next = NULL;
  2355. config_parse_addr_policy(&tmp, policy, -1);
  2356. /* Remove redundant parts, if any. */
  2357. for (ap=*policy; ap; ap=ap->next) {
  2358. if (ap->msk == 0 && ap->prt_min <= 1 && ap->prt_max >= 65535) {
  2359. if (ap->next) {
  2360. addr_policy_free(ap->next);
  2361. ap->next = NULL;
  2362. }
  2363. return;
  2364. }
  2365. }
  2366. }
  2367. /**
  2368. * Given a linked list of config lines containing "allow" and "deny" tokens,
  2369. * parse them and append the result to <b>dest</b>. Return -1 if any tokens
  2370. * are malformed, else return 0.
  2371. */
  2372. int
  2373. config_parse_addr_policy(config_line_t *cfg,
  2374. addr_policy_t **dest,
  2375. int assume_action)
  2376. {
  2377. addr_policy_t **nextp;
  2378. smartlist_t *entries;
  2379. int r = 0;
  2380. if (!cfg)
  2381. return 0;
  2382. nextp = dest;
  2383. while (*nextp)
  2384. nextp = &((*nextp)->next);
  2385. entries = smartlist_create();
  2386. for (; cfg; cfg = cfg->next) {
  2387. smartlist_split_string(entries, cfg->value, ",", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  2388. SMARTLIST_FOREACH(entries, const char *, ent,
  2389. {
  2390. log_fn(LOG_DEBUG,"Adding new entry '%s'",ent);
  2391. *nextp = router_parse_addr_policy_from_string(ent, assume_action);
  2392. if (*nextp) {
  2393. nextp = &((*nextp)->next);
  2394. } else {
  2395. log_fn(LOG_WARN,"Malformed policy '%s'.", ent);
  2396. r = -1;
  2397. }
  2398. });
  2399. SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
  2400. smartlist_clear(entries);
  2401. }
  2402. smartlist_free(entries);
  2403. return r;
  2404. }
  2405. /** Release all storage held by <b>p</b> */
  2406. void
  2407. addr_policy_free(addr_policy_t *p)
  2408. {
  2409. addr_policy_t *e;
  2410. while (p) {
  2411. e = p;
  2412. p = p->next;
  2413. tor_free(e->string);
  2414. tor_free(e);
  2415. }
  2416. }
  2417. /** Parse a single RedirectExit line's contents from <b>line</b>. If
  2418. * they are valid, and <b>result</b> is not NULL, add an element to
  2419. * <b>result</b> and return 0. Else if they are valid, return 0.
  2420. * Else return -1. */
  2421. static int
  2422. parse_redirect_line(smartlist_t *result, config_line_t *line)
  2423. {
  2424. smartlist_t *elements = NULL;
  2425. exit_redirect_t *r;
  2426. tor_assert(line);
  2427. r = tor_malloc_zero(sizeof(exit_redirect_t));
  2428. elements = smartlist_create();
  2429. smartlist_split_string(elements, line->value, NULL,
  2430. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  2431. if (smartlist_len(elements) != 2) {
  2432. log_fn(LOG_WARN, "Wrong number of elements in RedirectExit line");
  2433. goto err;
  2434. }
  2435. if (parse_addr_and_port_range(smartlist_get(elements,0),&r->addr,&r->mask,
  2436. &r->port_min,&r->port_max)) {
  2437. log_fn(LOG_WARN, "Error parsing source address in RedirectExit line");
  2438. goto err;
  2439. }
  2440. if (0==strcasecmp(smartlist_get(elements,1), "pass")) {
  2441. r->is_redirect = 0;
  2442. } else {
  2443. if (parse_addr_port(smartlist_get(elements,1),NULL,&r->addr_dest,
  2444. &r->port_dest)) {
  2445. log_fn(LOG_WARN, "Error parsing dest address in RedirectExit line");
  2446. goto err;
  2447. }
  2448. r->is_redirect = 1;
  2449. }
  2450. goto done;
  2451. err:
  2452. tor_free(r);
  2453. done:
  2454. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  2455. smartlist_free(elements);
  2456. if (r) {
  2457. if (result)
  2458. smartlist_add(result, r);
  2459. else
  2460. tor_free(r);
  2461. return 0;
  2462. } else {
  2463. return -1;
  2464. }
  2465. }
  2466. /** Read the contents of a DirServer line from <b>line</b>. Return 0
  2467. * if the line is well-formed, and -1 if it isn't. If
  2468. * <b>validate_only</b> is 0, and the line is well-formed, then add
  2469. * the dirserver described in the line as a valid server. */
  2470. static int
  2471. parse_dir_server_line(const char *line, int validate_only)
  2472. {
  2473. smartlist_t *items = NULL;
  2474. int r;
  2475. char *addrport, *address=NULL;
  2476. uint16_t port;
  2477. char digest[DIGEST_LEN];
  2478. int supports_v1 = 1; /*XXXX011 change default when clients support v2. */
  2479. while (TOR_ISSPACE(*line))
  2480. ++line;
  2481. if (!strcmpstart(line, "v1 ")) {
  2482. line += 3;
  2483. supports_v1 = 1;
  2484. }
  2485. items = smartlist_create();
  2486. smartlist_split_string(items, line, NULL,
  2487. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  2488. if (smartlist_len(items) < 2) {
  2489. log_fn(LOG_WARN, "Too few arguments to DirServer line.");
  2490. goto err;
  2491. }
  2492. addrport = smartlist_get(items, 0);
  2493. if (parse_addr_port(addrport, &address, NULL, &port)<0) {
  2494. log_fn(LOG_WARN, "Error parsing DirServer address '%s'", addrport);
  2495. goto err;
  2496. }
  2497. if (!port) {
  2498. log_fn(LOG_WARN, "Missing port in DirServer address '%s'",addrport);
  2499. goto err;
  2500. }
  2501. tor_strstrip(smartlist_get(items, 1), " ");
  2502. if (strlen(smartlist_get(items, 1)) != HEX_DIGEST_LEN) {
  2503. log_fn(LOG_WARN, "Key digest for DirServer is wrong length.");
  2504. goto err;
  2505. }
  2506. if (base16_decode(digest, DIGEST_LEN,
  2507. smartlist_get(items,1), HEX_DIGEST_LEN)<0) {
  2508. log_fn(LOG_WARN, "Unable to decode DirServer key digest.");
  2509. goto err;
  2510. }
  2511. if (!validate_only) {
  2512. log_fn(LOG_DEBUG, "Trusted dirserver at %s:%d (%s)", address, (int)port,
  2513. (char*)smartlist_get(items,1));
  2514. add_trusted_dir_server(address, port, digest, supports_v1);
  2515. }
  2516. r = 0;
  2517. goto done;
  2518. err:
  2519. r = -1;
  2520. done:
  2521. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  2522. smartlist_free(items);
  2523. tor_free(address);
  2524. return r;
  2525. }
  2526. /** Adjust the value of options->DataDirectory, or fill it in if it's
  2527. * absent. Return 0 on success, -1 on failure. */
  2528. static int
  2529. normalize_data_directory(or_options_t *options)
  2530. {
  2531. #ifdef MS_WINDOWS
  2532. char *p;
  2533. if (options->DataDirectory)
  2534. return 0; /* all set */
  2535. p = tor_malloc(MAX_PATH);
  2536. strlcpy(p,get_windows_conf_root(),MAX_PATH);
  2537. options->DataDirectory = p;
  2538. return 0;
  2539. #else
  2540. const char *d = options->DataDirectory;
  2541. if (!d)
  2542. d = "~/.tor";
  2543. if (strncmp(d,"~/",2) == 0) {
  2544. char *fn = expand_filename(d);
  2545. if (!fn) {
  2546. log_fn(LOG_ERR,"Failed to expand filename \"%s\".", d);
  2547. return -1;
  2548. }
  2549. if (!options->DataDirectory && !strcmp(fn,"/.tor")) {
  2550. /* If our homedir is /, we probably don't want to use it. */
  2551. /* XXXX Default to /var/lib/tor? */
  2552. log_fn(LOG_WARN, "Default DataDirectory is \"~/.tor\". This expands to \"%s\", which is probably not what you want. Using \"%s/tor\" instead", fn, LOCALSTATEDIR);
  2553. tor_free(fn);
  2554. fn = tor_strdup(LOCALSTATEDIR"/tor");
  2555. }
  2556. tor_free(options->DataDirectory);
  2557. options->DataDirectory = fn;
  2558. }
  2559. return 0;
  2560. #endif
  2561. }
  2562. /** Check and normalize the value of options->DataDirectory; return 0 if it
  2563. * sane, -1 otherwise. */
  2564. static int
  2565. validate_data_directory(or_options_t *options)
  2566. {
  2567. if (normalize_data_directory(options) < 0)
  2568. return -1;
  2569. tor_assert(options->DataDirectory);
  2570. if (strlen(options->DataDirectory) > (512-128)) {
  2571. log_fn(LOG_ERR, "DataDirectory is too long.");
  2572. return -1;
  2573. }
  2574. return 0;
  2575. }
  2576. #define GENERATED_FILE_PREFIX "# This file was generated by Tor; if you edit it, comments will not be preserved"
  2577. /** Save a configuration file for the configuration in <b>options</b>
  2578. * into the file <b>fname</b>. If the file already exists, and
  2579. * doesn't begin with GENERATED_FILE_PREFIX, rename it. Otherwise
  2580. * replace it. Return 0 on success, -1 on failure. */
  2581. static int
  2582. write_configuration_file(const char *fname, or_options_t *options)
  2583. {
  2584. char *old_val=NULL, *new_val=NULL, *new_conf=NULL;
  2585. int rename_old = 0, r;
  2586. size_t len;
  2587. if (fname) {
  2588. switch (file_status(fname)) {
  2589. case FN_FILE:
  2590. old_val = read_file_to_str(fname, 0);
  2591. if (strcmpstart(old_val, GENERATED_FILE_PREFIX)) {
  2592. rename_old = 1;
  2593. }
  2594. tor_free(old_val);
  2595. break;
  2596. case FN_NOENT:
  2597. break;
  2598. default:
  2599. log_fn(LOG_WARN,"Config file \"%s\" is not a file? Failing.", fname);
  2600. return -1;
  2601. }
  2602. }
  2603. if (!(new_conf = options_dump(options, 1))) {
  2604. log_fn(LOG_WARN, "Couldn't get configuration string");
  2605. goto err;
  2606. }
  2607. len = strlen(new_conf)+128;
  2608. new_val = tor_malloc(len);
  2609. tor_snprintf(new_val, len, "%s\n\n%s", GENERATED_FILE_PREFIX, new_conf);
  2610. if (rename_old) {
  2611. int i = 1;
  2612. size_t fn_tmp_len = strlen(fname)+32;
  2613. char *fn_tmp;
  2614. tor_assert(fn_tmp_len > strlen(fname)); /*check for overflow*/
  2615. fn_tmp = tor_malloc(fn_tmp_len);
  2616. while (1) {
  2617. if (tor_snprintf(fn_tmp, fn_tmp_len, "%s.orig.%d", fname, i)<0) {
  2618. log_fn(LOG_WARN, "tor_snprintf failed inexplicably");
  2619. tor_free(fn_tmp);
  2620. goto err;
  2621. }
  2622. if (file_status(fn_tmp) == FN_NOENT)
  2623. break;
  2624. ++i;
  2625. }
  2626. log_fn(LOG_NOTICE, "Renaming old configuration file to \"%s\"", fn_tmp);
  2627. rename(fname, fn_tmp);
  2628. tor_free(fn_tmp);
  2629. }
  2630. write_str_to_file(fname, new_val, 0);
  2631. r = 0;
  2632. goto done;
  2633. err:
  2634. r = -1;
  2635. done:
  2636. tor_free(new_val);
  2637. tor_free(new_conf);
  2638. return r;
  2639. }
  2640. /**
  2641. * Save the current configuration file value to disk. Return 0 on
  2642. * success, -1 on failure.
  2643. **/
  2644. int
  2645. options_save_current(void)
  2646. {
  2647. if (torrc_fname) {
  2648. /* XXX This fails if we can't write to our configuration file.
  2649. * Arguably, we should try falling back to datadirectory or something.
  2650. * But just as arguably, we shouldn't. */
  2651. return write_configuration_file(torrc_fname, get_options());
  2652. }
  2653. return write_configuration_file(get_default_conf_file(), get_options());
  2654. }
  2655. struct unit_table_t {
  2656. const char *unit;
  2657. uint64_t multiplier;
  2658. };
  2659. static struct unit_table_t memory_units[] = {
  2660. { "", 1 },
  2661. { "b", 1<< 0 },
  2662. { "byte", 1<< 0 },
  2663. { "bytes", 1<< 0 },
  2664. { "kb", 1<<10 },
  2665. { "kilobyte", 1<<10 },
  2666. { "kilobytes", 1<<10 },
  2667. { "m", 1<<20 },
  2668. { "mb", 1<<20 },
  2669. { "megabyte", 1<<20 },
  2670. { "megabytes", 1<<20 },
  2671. { "gb", 1<<30 },
  2672. { "gigabyte", 1<<30 },
  2673. { "gigabytes", 1<<30 },
  2674. { "tb", U64_LITERAL(1)<<40 },
  2675. { "terabyte", U64_LITERAL(1)<<40 },
  2676. { "terabytes", U64_LITERAL(1)<<40 },
  2677. { NULL, 0 },
  2678. };
  2679. static struct unit_table_t time_units[] = {
  2680. { "", 1 },
  2681. { "second", 1 },
  2682. { "seconds", 1 },
  2683. { "minute", 60 },
  2684. { "minutes", 60 },
  2685. { "hour", 60*60 },
  2686. { "hours", 60*60 },
  2687. { "day", 24*60*60 },
  2688. { "days", 24*60*60 },
  2689. { "week", 7*24*60*60 },
  2690. { "weeks", 7*24*60*60 },
  2691. { NULL, 0 },
  2692. };
  2693. /** Parse a string <b>val</b> containing a number, zero or more
  2694. * spaces, and an optional unit string. If the unit appears in the
  2695. * table <b>u</b>, then multiply the number by the unit multiplier.
  2696. * On success, set *<b>ok</b> to 1 and return this product.
  2697. * Otherwise, set *<b>ok</b> to 0.
  2698. */
  2699. static uint64_t
  2700. config_parse_units(const char *val, struct unit_table_t *u, int *ok)
  2701. {
  2702. uint64_t v;
  2703. char *cp;
  2704. tor_assert(ok);
  2705. v = tor_parse_uint64(val, 10, 0, UINT64_MAX, ok, &cp);
  2706. if (!*ok)
  2707. return 0;
  2708. if (!cp) {
  2709. *ok = 1;
  2710. return v;
  2711. }
  2712. while (TOR_ISSPACE(*cp))
  2713. ++cp;
  2714. for ( ;u->unit;++u) {
  2715. if (!strcasecmp(u->unit, cp)) {
  2716. v *= u->multiplier;
  2717. *ok = 1;
  2718. return v;
  2719. }
  2720. }
  2721. log_fn(LOG_WARN, "Unknown unit '%s'.", cp);
  2722. *ok = 0;
  2723. return 0;
  2724. }
  2725. /** Parse a string in the format "number unit", where unit is a unit of
  2726. * information (byte, KB, M, etc). On success, set *<b>ok</b> to true
  2727. * and return the number of bytes specified. Otherwise, set
  2728. * *<b>ok</b> to false and return 0. */
  2729. static uint64_t
  2730. config_parse_memunit(const char *s, int *ok) {
  2731. return config_parse_units(s, memory_units, ok);
  2732. }
  2733. /** Parse a string in the format "number unit", where unit is a unit of time.
  2734. * On success, set *<b>ok</b> to true and return the number of seconds in
  2735. * the provided interval. Otherwise, set *<b>ok</b> to 0 and return -1.
  2736. */
  2737. static int
  2738. config_parse_interval(const char *s, int *ok) {
  2739. uint64_t r;
  2740. r = config_parse_units(s, time_units, ok);
  2741. if (!ok)
  2742. return -1;
  2743. if (r > INT_MAX) {
  2744. log_fn(LOG_WARN, "Interval '%s' is too long", s);
  2745. *ok = 0;
  2746. return -1;
  2747. }
  2748. return (int)r;
  2749. }
  2750. /**
  2751. * Initialize the libevent library.
  2752. */
  2753. static int
  2754. init_libevent(void)
  2755. {
  2756. configure_libevent_logging();
  2757. /* If the kernel complains that some method (say, epoll) doesn't
  2758. * exist, we don't care about it, since libevent will cope.
  2759. */
  2760. suppress_libevent_log_msg("Function not implemented");
  2761. #ifdef __APPLE__
  2762. putenv("EVENT_NOKQUEUE=1");
  2763. #endif
  2764. event_init();
  2765. suppress_libevent_log_msg(NULL);
  2766. #if defined(HAVE_EVENT_GET_VERSION) && defined(HAVE_EVENT_GET_METHOD)
  2767. /* Making this a NOTICE for now so we can link bugs to a libevent versions
  2768. * or methods better. */
  2769. log(LOG_NOTICE, "Initialized libevent version %s using method %s. Good.",
  2770. event_get_version(), event_get_method());
  2771. check_libevent_version(event_get_method(), event_get_version(),
  2772. get_options()->ORPort != 0);
  2773. #else
  2774. log(LOG_NOTICE, "Initialized old libevent (version 1.0b or earlier).");
  2775. log(LOG_WARN, "You have a very old version of libevent. It is likely to be buggy; please consider building Tor with a more recent version.");
  2776. #endif
  2777. return 0;
  2778. }
  2779. #if defined(HAVE_EVENT_GET_VERSION) && defined(HAVE_EVENT_GET_METHOD)
  2780. /**
  2781. * Compare the given libevent method and version to a list of versions
  2782. * which are known not to work. Warn the user as appropriate.
  2783. *
  2784. */
  2785. static void
  2786. check_libevent_version(const char *m, const char *v, int server)
  2787. {
  2788. int buggy = 0, iffy = 0, slow = 0;
  2789. tor_assert(m && v);
  2790. if (!strcmp(m, "kqueue")) {
  2791. if (!strcmp(v, "1.0c") || !strcmp(v, "1.0d") || !strcmp(v, "1.0e") ||
  2792. !strcmp(v, "1.1")) {
  2793. buggy = 1;
  2794. }
  2795. } else if (!strcmp(m, "epoll")) {
  2796. if (!strcmp(v, "1.0c") || !strcmp(v, "1.0d") || !strcmp(v, "1.0e"))
  2797. iffy = 1;
  2798. } else if (!strcmp(m, "poll")) {
  2799. if (!strcmp(v, "1.0c") || !strcmp(v, "1.0d"))
  2800. buggy = 1;
  2801. else if (!strcmp(v, "1.0e"))
  2802. slow = 1;
  2803. } else if (!strcmp(m, "poll")) {
  2804. if (!strcmp(v, "1.0c") || !strcmp(v, "1.0d") || !strcmp(v, "1.0e"))
  2805. slow = 1;
  2806. }
  2807. if (buggy) {
  2808. log(LOG_WARN,
  2809. "There are known bugs in using %s with libevent %s. "
  2810. "Please use the latest version of libevent.", m, v);
  2811. } else if (iffy) {
  2812. log(LOG_WARN,
  2813. "There are minor bugs in using %s with libevent %s. "
  2814. "You may want to use the latest version of libevent.", m, v);
  2815. } else if (slow && server) {
  2816. log(LOG_WARN,
  2817. "libevent %s can be very slow with %s. "
  2818. "When running a server, please use the latest version of libevent.",v,m);
  2819. }
  2820. }
  2821. #endif
  2822. /* Versioning issues and state: we want to be able to understand old state
  2823. * files, and not choke on new ones.
  2824. *
  2825. * We could preserve all unrecognized variables across invocations, but we could
  2826. * screw up order, if their order is significant with respect to existing
  2827. * options.
  2828. *
  2829. * We could just dump unrecognized variables if you downgrade.
  2830. *
  2831. * This needs thought. XXXX NM
  2832. */
  2833. /** DOCDOC */
  2834. or_state_t *
  2835. get_or_state(void)
  2836. {
  2837. return global_state;
  2838. }
  2839. /** DOCDOC */
  2840. static char *
  2841. get_or_state_fname(void)
  2842. {
  2843. char *fname = NULL;
  2844. or_options_t *options = get_options();
  2845. size_t len = strlen(options->DataDirectory) + 16;
  2846. fname = tor_malloc(len);
  2847. tor_snprintf(fname, len, "%s/state", options->DataDirectory);
  2848. return fname;
  2849. }
  2850. /** DOCDOC */
  2851. static int
  2852. or_state_validate(or_state_t *state)
  2853. {
  2854. const char *err;
  2855. if (helper_nodes_parse_state(state, 0, &err)<0) {
  2856. log_fn(LOG_WARN, "Unable to parse helper nodes: %s", err);
  2857. return -1;
  2858. }
  2859. return 0;
  2860. }
  2861. /** DOCDOC */
  2862. static void
  2863. or_state_set(or_state_t *new_state)
  2864. {
  2865. const char *err;
  2866. tor_assert(new_state);
  2867. if (global_state)
  2868. config_free(&state_format, global_state);
  2869. global_state = new_state;
  2870. if (helper_nodes_parse_state(global_state, 1, &err)<0)
  2871. log_fn(LOG_WARN,"Unparseable helper nodes state: %s",err);
  2872. }
  2873. /* DOCDOC */
  2874. int
  2875. or_state_load(void)
  2876. {
  2877. or_state_t *new_state = NULL;
  2878. char *contents = NULL, *fname;
  2879. int r = -1;
  2880. fname = get_or_state_fname();
  2881. switch (file_status(fname)) {
  2882. case FN_FILE:
  2883. if (!(contents = read_file_to_str(fname, 0))) {
  2884. log_fn(LOG_WARN, "Unable to read state file \"%s\"", fname);
  2885. goto done;
  2886. }
  2887. break;
  2888. case FN_NOENT:
  2889. break;
  2890. default:
  2891. log_fn(LOG_WARN,"State file \"%s\" is not a file? Failing.", fname);
  2892. goto done;
  2893. }
  2894. new_state = tor_malloc_zero(sizeof(or_state_t));
  2895. new_state->_magic = OR_STATE_MAGIC;
  2896. config_init(&state_format, new_state);
  2897. if (contents) {
  2898. config_line_t *lines=NULL;
  2899. int assign_retval;
  2900. if (config_get_lines(contents, &lines)<0)
  2901. goto done;
  2902. assign_retval = config_assign(&state_format, new_state, lines, 0);
  2903. config_free_lines(lines);
  2904. if (assign_retval<0)
  2905. goto done;
  2906. }
  2907. if (or_state_validate(new_state) < 0)
  2908. goto done;
  2909. if (contents)
  2910. log_fn(LOG_INFO, "Loaded state from \"%s\"", fname);
  2911. else
  2912. log_fn(LOG_INFO, "Initialized state");
  2913. or_state_set(new_state);
  2914. new_state = NULL;
  2915. if (!contents) {
  2916. global_state->dirty = 1;
  2917. or_state_save();
  2918. }
  2919. r = 0;
  2920. done:
  2921. tor_free(fname);
  2922. tor_free(contents);
  2923. if (new_state)
  2924. config_free(&state_format, new_state);
  2925. return r;
  2926. }
  2927. /** DOCDOC */
  2928. int
  2929. or_state_save(void)
  2930. {
  2931. char *state, *contents;
  2932. char tbuf[ISO_TIME_LEN+1];
  2933. size_t len;
  2934. char *fname;
  2935. helper_nodes_update_state(global_state);
  2936. if (!global_state->dirty)
  2937. return 0;
  2938. global_state->LastWritten = time(NULL);
  2939. state = config_dump(&state_format, global_state, 0);
  2940. len = strlen(state)+128;
  2941. contents = tor_malloc(len);
  2942. format_local_iso_time(tbuf, time(NULL));
  2943. tor_snprintf(contents, len,
  2944. "# Tor state file last generated on %s\n"
  2945. "# You *do not* need to edit this file.\n\n%s",
  2946. tbuf, state);
  2947. tor_free(state);
  2948. fname = get_or_state_fname();
  2949. if (write_str_to_file(fname, contents, 0)<0) {
  2950. log_fn(LOG_WARN, "Unable to write state to file \"%s\"", fname);
  2951. tor_free(fname);
  2952. tor_free(contents);
  2953. return -1;
  2954. }
  2955. log_fn(LOG_INFO, "Saved state to \"%s\"", fname);
  2956. tor_free(fname);
  2957. tor_free(contents);
  2958. global_state->dirty = 0;
  2959. return 0;
  2960. }
  2961. /** DOCDOC */
  2962. int
  2963. config_getinfo_helper(const char *question, char **answer)
  2964. {
  2965. if (!strcmp(question, "config/names")) {
  2966. smartlist_t *sl = smartlist_create();
  2967. int i;
  2968. for (i = 0; _option_vars[i].name; ++i) {
  2969. config_var_t *var = &_option_vars[i];
  2970. const char *type, *desc;
  2971. char *line;
  2972. size_t len;
  2973. desc = config_find_description(&options_format, var->name);
  2974. switch (var->type) {
  2975. case CONFIG_TYPE_STRING: type = "String"; break;
  2976. case CONFIG_TYPE_UINT: type = "Integer"; break;
  2977. case CONFIG_TYPE_INTERVAL: type = "TimeInterval"; break;
  2978. case CONFIG_TYPE_MEMUNIT: type = "DataSize"; break;
  2979. case CONFIG_TYPE_DOUBLE: type = "Float"; break;
  2980. case CONFIG_TYPE_BOOL: type = "Boolean"; break;
  2981. case CONFIG_TYPE_ISOTIME: type = "Time"; break;
  2982. case CONFIG_TYPE_CSV: type = "CommaList"; break;
  2983. case CONFIG_TYPE_LINELIST: type = "LineList"; break;
  2984. case CONFIG_TYPE_LINELIST_S: type = "Dependant"; break;
  2985. case CONFIG_TYPE_LINELIST_V: type = "Virtual"; break;
  2986. default:
  2987. case CONFIG_TYPE_OBSOLETE:
  2988. type = NULL; break;
  2989. }
  2990. if (!type)
  2991. continue;
  2992. len = strlen(var->name)+strlen(type)+16;
  2993. if (desc)
  2994. len += strlen(desc);
  2995. line = tor_malloc(len);
  2996. if (desc)
  2997. tor_snprintf(line, len, "%s %s %s\n",var->name,type,desc);
  2998. else
  2999. tor_snprintf(line, len, "%s %s\n",var->name,type);
  3000. smartlist_add(sl, line);
  3001. }
  3002. *answer = smartlist_join_strings(sl, "", 0, NULL);
  3003. SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
  3004. smartlist_free(sl);
  3005. }
  3006. return 0;
  3007. }
  3008. /** Dump the version of every file to the log. */
  3009. static void
  3010. print_cvs_version(void)
  3011. {
  3012. extern const char aes_c_id[];
  3013. extern const char compat_c_id[];
  3014. extern const char container_c_id[];
  3015. extern const char crypto_c_id[];
  3016. extern const char log_c_id[];
  3017. extern const char torgzip_c_id[];
  3018. extern const char tortls_c_id[];
  3019. extern const char util_c_id[];
  3020. extern const char buffers_c_id[];
  3021. extern const char circuitbuild_c_id[];
  3022. extern const char circuitlist_c_id[];
  3023. extern const char circuituse_c_id[];
  3024. extern const char command_c_id[];
  3025. // extern const char config_c_id[];
  3026. extern const char connection_c_id[];
  3027. extern const char connection_edge_c_id[];
  3028. extern const char connection_or_c_id[];
  3029. extern const char control_c_id[];
  3030. extern const char cpuworker_c_id[];
  3031. extern const char directory_c_id[];
  3032. extern const char dirserv_c_id[];
  3033. extern const char dns_c_id[];
  3034. extern const char hibernate_c_id[];
  3035. extern const char main_c_id[];
  3036. extern const char onion_c_id[];
  3037. extern const char relay_c_id[];
  3038. extern const char rendclient_c_id[];
  3039. extern const char rendcommon_c_id[];
  3040. extern const char rendmid_c_id[];
  3041. extern const char rendservice_c_id[];
  3042. extern const char rephist_c_id[];
  3043. extern const char router_c_id[];
  3044. extern const char routerlist_c_id[];
  3045. extern const char routerparse_c_id[];
  3046. puts(AES_H_ID);
  3047. puts(COMPAT_H_ID);
  3048. puts(CONTAINER_H_ID);
  3049. puts(CRYPTO_H_ID);
  3050. puts(LOG_H_ID);
  3051. puts(TORGZIP_H_ID);
  3052. puts(TORINT_H_ID);
  3053. puts(TORTLS_H_ID);
  3054. puts(UTIL_H_ID);
  3055. puts(aes_c_id);
  3056. puts(compat_c_id);
  3057. puts(container_c_id);
  3058. puts(crypto_c_id);
  3059. puts(log_c_id);
  3060. puts(torgzip_c_id);
  3061. puts(tortls_c_id);
  3062. puts(util_c_id);
  3063. puts(OR_H_ID);
  3064. puts(buffers_c_id);
  3065. puts(circuitbuild_c_id);
  3066. puts(circuitlist_c_id);
  3067. puts(circuituse_c_id);
  3068. puts(command_c_id);
  3069. puts(config_c_id);
  3070. puts(connection_c_id);
  3071. puts(connection_edge_c_id);
  3072. puts(connection_or_c_id);
  3073. puts(control_c_id);
  3074. puts(cpuworker_c_id);
  3075. puts(directory_c_id);
  3076. puts(dirserv_c_id);
  3077. puts(dns_c_id);
  3078. puts(hibernate_c_id);
  3079. puts(main_c_id);
  3080. puts(onion_c_id);
  3081. puts(relay_c_id);
  3082. puts(rendclient_c_id);
  3083. puts(rendcommon_c_id);
  3084. puts(rendmid_c_id);
  3085. puts(rendservice_c_id);
  3086. puts(rephist_c_id);
  3087. puts(router_c_id);
  3088. puts(routerlist_c_id);
  3089. puts(routerparse_c_id);
  3090. }