util.c 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util.c
  7. * \brief Common functions for strings, IO, network, data structures,
  8. * process control.
  9. **/
  10. /* This is required on rh7 to make strptime not complain.
  11. */
  12. #define _GNU_SOURCE
  13. #include "orconfig.h"
  14. #ifdef HAVE_FCNTL_H
  15. #include <fcntl.h>
  16. #endif
  17. #define UTIL_PRIVATE
  18. #include "util.h"
  19. #include "torlog.h"
  20. #include "crypto.h"
  21. #include "torint.h"
  22. #include "container.h"
  23. #include "address.h"
  24. #include "sandbox.h"
  25. #include "backtrace.h"
  26. #include "util_process.h"
  27. #ifdef _WIN32
  28. #include <io.h>
  29. #include <direct.h>
  30. #include <process.h>
  31. #include <tchar.h>
  32. #include <winbase.h>
  33. #else
  34. #include <dirent.h>
  35. #include <pwd.h>
  36. #include <grp.h>
  37. #endif
  38. /* math.h needs this on Linux */
  39. #ifndef _USE_ISOC99_
  40. #define _USE_ISOC99_ 1
  41. #endif
  42. #include <math.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <assert.h>
  47. #include <signal.h>
  48. #ifdef HAVE_NETINET_IN_H
  49. #include <netinet/in.h>
  50. #endif
  51. #ifdef HAVE_ARPA_INET_H
  52. #include <arpa/inet.h>
  53. #endif
  54. #ifdef HAVE_ERRNO_H
  55. #include <errno.h>
  56. #endif
  57. #ifdef HAVE_SYS_SOCKET_H
  58. #include <sys/socket.h>
  59. #endif
  60. #ifdef HAVE_SYS_TIME_H
  61. #include <sys/time.h>
  62. #endif
  63. #ifdef HAVE_UNISTD_H
  64. #include <unistd.h>
  65. #endif
  66. #ifdef HAVE_SYS_STAT_H
  67. #include <sys/stat.h>
  68. #endif
  69. #ifdef HAVE_SYS_FCNTL_H
  70. #include <sys/fcntl.h>
  71. #endif
  72. #ifdef HAVE_TIME_H
  73. #include <time.h>
  74. #endif
  75. #ifdef HAVE_MALLOC_MALLOC_H
  76. #include <malloc/malloc.h>
  77. #endif
  78. #ifdef HAVE_MALLOC_H
  79. #if !defined(OPENBSD) && !defined(__FreeBSD__)
  80. /* OpenBSD has a malloc.h, but for our purposes, it only exists in order to
  81. * scold us for being so stupid as to autodetect its presence. To be fair,
  82. * they've done this since 1996, when autoconf was only 5 years old. */
  83. #include <malloc.h>
  84. #endif
  85. #endif
  86. #ifdef HAVE_MALLOC_NP_H
  87. #include <malloc_np.h>
  88. #endif
  89. #ifdef HAVE_SYS_WAIT_H
  90. #include <sys/wait.h>
  91. #endif
  92. #ifdef __clang_analyzer__
  93. #undef MALLOC_ZERO_WORKS
  94. #endif
  95. /* =====
  96. * Assertion helper.
  97. * ===== */
  98. /** Helper for tor_assert: report the assertion failure. */
  99. void
  100. tor_assertion_failed_(const char *fname, unsigned int line,
  101. const char *func, const char *expr)
  102. {
  103. char buf[256];
  104. log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
  105. fname, line, func, expr);
  106. tor_snprintf(buf, sizeof(buf),
  107. "Assertion %s failed in %s at %s:%u",
  108. expr, func, fname, line);
  109. log_backtrace(LOG_ERR, LD_BUG, buf);
  110. }
  111. /* =====
  112. * Memory management
  113. * ===== */
  114. #ifdef USE_DMALLOC
  115. #undef strndup
  116. #include <dmalloc.h>
  117. /* Macro to pass the extra dmalloc args to another function. */
  118. #define DMALLOC_FN_ARGS , file, line
  119. #if defined(HAVE_DMALLOC_STRDUP)
  120. /* the dmalloc_strdup should be fine as defined */
  121. #elif defined(HAVE_DMALLOC_STRNDUP)
  122. #define dmalloc_strdup(file, line, string, xalloc_b) \
  123. dmalloc_strndup(file, line, (string), -1, xalloc_b)
  124. #else
  125. #error "No dmalloc_strdup or equivalent"
  126. #endif
  127. #else /* not using dmalloc */
  128. #define DMALLOC_FN_ARGS
  129. #endif
  130. /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
  131. * result. On error, log and terminate the process. (Same as malloc(size),
  132. * but never returns NULL.)
  133. *
  134. * <b>file</b> and <b>line</b> are used if dmalloc is enabled, and
  135. * ignored otherwise.
  136. */
  137. void *
  138. tor_malloc_(size_t size DMALLOC_PARAMS)
  139. {
  140. void *result;
  141. tor_assert(size < SIZE_T_CEILING);
  142. #ifndef MALLOC_ZERO_WORKS
  143. /* Some libc mallocs don't work when size==0. Override them. */
  144. if (size==0) {
  145. size=1;
  146. }
  147. #endif
  148. #ifdef USE_DMALLOC
  149. result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
  150. #else
  151. result = malloc(size);
  152. #endif
  153. if (PREDICT_UNLIKELY(result == NULL)) {
  154. log_err(LD_MM,"Out of memory on malloc(). Dying.");
  155. /* If these functions die within a worker process, they won't call
  156. * spawn_exit, but that's ok, since the parent will run out of memory soon
  157. * anyway. */
  158. exit(1);
  159. }
  160. return result;
  161. }
  162. /** Allocate a chunk of <b>size</b> bytes of memory, fill the memory with
  163. * zero bytes, and return a pointer to the result. Log and terminate
  164. * the process on error. (Same as calloc(size,1), but never returns NULL.)
  165. */
  166. void *
  167. tor_malloc_zero_(size_t size DMALLOC_PARAMS)
  168. {
  169. /* You may ask yourself, "wouldn't it be smart to use calloc instead of
  170. * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
  171. * we don't!" Indeed it does, but its optimizations are only a big win when
  172. * we're allocating something very big (it knows if it just got the memory
  173. * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
  174. * for big stuff, so we don't bother with calloc. */
  175. void *result = tor_malloc_(size DMALLOC_FN_ARGS);
  176. memset(result, 0, size);
  177. return result;
  178. }
  179. /* The square root of SIZE_MAX + 1. If a is less than this, and b is less
  180. * than this, then a*b is less than SIZE_MAX. (For example, if size_t is
  181. * 32 bits, then SIZE_MAX is 0xffffffff and this value is 0x10000. If a and
  182. * b are less than this, then their product is at most (65535*65535) ==
  183. * 0xfffe0001. */
  184. #define SQRT_SIZE_MAX_P1 (((size_t)1) << (sizeof(size_t)*4))
  185. /** Return non-zero if and only if the product of the arguments is exact. */
  186. static INLINE int
  187. size_mul_check(const size_t x, const size_t y)
  188. {
  189. /* This first check is equivalent to
  190. (x < SQRT_SIZE_MAX_P1 && y < SQRT_SIZE_MAX_P1)
  191. Rationale: if either one of x or y is >= SQRT_SIZE_MAX_P1, then it
  192. will have some bit set in its most significant half.
  193. */
  194. return ((x|y) < SQRT_SIZE_MAX_P1 ||
  195. y == 0 ||
  196. x <= SIZE_MAX / y);
  197. }
  198. /** Allocate a chunk of <b>nmemb</b>*<b>size</b> bytes of memory, fill
  199. * the memory with zero bytes, and return a pointer to the result.
  200. * Log and terminate the process on error. (Same as
  201. * calloc(<b>nmemb</b>,<b>size</b>), but never returns NULL.)
  202. * The second argument (<b>size</b>) should preferably be non-zero
  203. * and a compile-time constant.
  204. */
  205. void *
  206. tor_calloc_(size_t nmemb, size_t size DMALLOC_PARAMS)
  207. {
  208. tor_assert(size_mul_check(nmemb, size));
  209. return tor_malloc_zero_((nmemb * size) DMALLOC_FN_ARGS);
  210. }
  211. /** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
  212. * bytes long; return the new memory block. On error, log and
  213. * terminate. (Like realloc(ptr,size), but never returns NULL.)
  214. */
  215. void *
  216. tor_realloc_(void *ptr, size_t size DMALLOC_PARAMS)
  217. {
  218. void *result;
  219. tor_assert(size < SIZE_T_CEILING);
  220. #ifndef MALLOC_ZERO_WORKS
  221. /* Some libc mallocs don't work when size==0. Override them. */
  222. if (size==0) {
  223. size=1;
  224. }
  225. #endif
  226. #ifdef USE_DMALLOC
  227. result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
  228. #else
  229. result = realloc(ptr, size);
  230. #endif
  231. if (PREDICT_UNLIKELY(result == NULL)) {
  232. log_err(LD_MM,"Out of memory on realloc(). Dying.");
  233. exit(1);
  234. }
  235. return result;
  236. }
  237. /**
  238. * Try to realloc <b>ptr</b> so that it takes up sz1 * sz2 bytes. Check for
  239. * overflow. Unlike other allocation functions, return NULL on overflow.
  240. */
  241. void *
  242. tor_reallocarray_(void *ptr, size_t sz1, size_t sz2 DMALLOC_PARAMS)
  243. {
  244. /* XXXX we can make this return 0, but we would need to check all the
  245. * reallocarray users. */
  246. tor_assert(size_mul_check(sz1, sz2));
  247. return tor_realloc(ptr, (sz1 * sz2) DMALLOC_FN_ARGS);
  248. }
  249. /** Return a newly allocated copy of the NUL-terminated string s. On
  250. * error, log and terminate. (Like strdup(s), but never returns
  251. * NULL.)
  252. */
  253. char *
  254. tor_strdup_(const char *s DMALLOC_PARAMS)
  255. {
  256. char *dup;
  257. tor_assert(s);
  258. #ifdef USE_DMALLOC
  259. dup = dmalloc_strdup(file, line, s, 0);
  260. #else
  261. dup = strdup(s);
  262. #endif
  263. if (PREDICT_UNLIKELY(dup == NULL)) {
  264. log_err(LD_MM,"Out of memory on strdup(). Dying.");
  265. exit(1);
  266. }
  267. return dup;
  268. }
  269. /** Allocate and return a new string containing the first <b>n</b>
  270. * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
  271. * characters, only the first <b>n</b> are copied. The result is
  272. * always NUL-terminated. (Like strndup(s,n), but never returns
  273. * NULL.)
  274. */
  275. char *
  276. tor_strndup_(const char *s, size_t n DMALLOC_PARAMS)
  277. {
  278. char *dup;
  279. tor_assert(s);
  280. tor_assert(n < SIZE_T_CEILING);
  281. dup = tor_malloc_((n+1) DMALLOC_FN_ARGS);
  282. /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
  283. * this function gets called a whole lot, and platform strncpy is
  284. * much faster than strlcpy when strlen(s) is much longer than n.
  285. */
  286. strncpy(dup, s, n);
  287. dup[n]='\0';
  288. return dup;
  289. }
  290. /** Allocate a chunk of <b>len</b> bytes, with the same contents as the
  291. * <b>len</b> bytes starting at <b>mem</b>. */
  292. void *
  293. tor_memdup_(const void *mem, size_t len DMALLOC_PARAMS)
  294. {
  295. char *dup;
  296. tor_assert(len < SIZE_T_CEILING);
  297. tor_assert(mem);
  298. dup = tor_malloc_(len DMALLOC_FN_ARGS);
  299. memcpy(dup, mem, len);
  300. return dup;
  301. }
  302. /** As tor_memdup(), but add an extra 0 byte at the end of the resulting
  303. * memory. */
  304. void *
  305. tor_memdup_nulterm_(const void *mem, size_t len DMALLOC_PARAMS)
  306. {
  307. char *dup;
  308. tor_assert(len < SIZE_T_CEILING+1);
  309. tor_assert(mem);
  310. dup = tor_malloc_(len+1 DMALLOC_FN_ARGS);
  311. memcpy(dup, mem, len);
  312. dup[len] = '\0';
  313. return dup;
  314. }
  315. /** Helper for places that need to take a function pointer to the right
  316. * spelling of "free()". */
  317. void
  318. tor_free_(void *mem)
  319. {
  320. tor_free(mem);
  321. }
  322. /** Call the platform malloc info function, and dump the results to the log at
  323. * level <b>severity</b>. If no such function exists, do nothing. */
  324. void
  325. tor_log_mallinfo(int severity)
  326. {
  327. #ifdef HAVE_MALLINFO
  328. struct mallinfo mi;
  329. memset(&mi, 0, sizeof(mi));
  330. mi = mallinfo();
  331. tor_log(severity, LD_MM,
  332. "mallinfo() said: arena=%d, ordblks=%d, smblks=%d, hblks=%d, "
  333. "hblkhd=%d, usmblks=%d, fsmblks=%d, uordblks=%d, fordblks=%d, "
  334. "keepcost=%d",
  335. mi.arena, mi.ordblks, mi.smblks, mi.hblks,
  336. mi.hblkhd, mi.usmblks, mi.fsmblks, mi.uordblks, mi.fordblks,
  337. mi.keepcost);
  338. #else
  339. (void)severity;
  340. #endif
  341. #ifdef USE_DMALLOC
  342. dmalloc_log_changed(0, /* Since the program started. */
  343. 1, /* Log info about non-freed pointers. */
  344. 0, /* Do not log info about freed pointers. */
  345. 0 /* Do not log individual pointers. */
  346. );
  347. #endif
  348. }
  349. /* =====
  350. * Math
  351. * ===== */
  352. /**
  353. * Returns the natural logarithm of d base e. We defined this wrapper here so
  354. * to avoid conflicts with old versions of tor_log(), which were named log().
  355. */
  356. double
  357. tor_mathlog(double d)
  358. {
  359. return log(d);
  360. }
  361. /** Return the long integer closest to <b>d</b>. We define this wrapper
  362. * here so that not all users of math.h need to use the right incantations
  363. * to get the c99 functions. */
  364. long
  365. tor_lround(double d)
  366. {
  367. #if defined(HAVE_LROUND)
  368. return lround(d);
  369. #elif defined(HAVE_RINT)
  370. return (long)rint(d);
  371. #else
  372. return (long)(d > 0 ? d + 0.5 : ceil(d - 0.5));
  373. #endif
  374. }
  375. /** Return the 64-bit integer closest to d. We define this wrapper here so
  376. * that not all users of math.h need to use the right incantations to get the
  377. * c99 functions. */
  378. int64_t
  379. tor_llround(double d)
  380. {
  381. #if defined(HAVE_LLROUND)
  382. return (int64_t)llround(d);
  383. #elif defined(HAVE_RINT)
  384. return (int64_t)rint(d);
  385. #else
  386. return (int64_t)(d > 0 ? d + 0.5 : ceil(d - 0.5));
  387. #endif
  388. }
  389. /** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */
  390. int
  391. tor_log2(uint64_t u64)
  392. {
  393. int r = 0;
  394. if (u64 >= (U64_LITERAL(1)<<32)) {
  395. u64 >>= 32;
  396. r = 32;
  397. }
  398. if (u64 >= (U64_LITERAL(1)<<16)) {
  399. u64 >>= 16;
  400. r += 16;
  401. }
  402. if (u64 >= (U64_LITERAL(1)<<8)) {
  403. u64 >>= 8;
  404. r += 8;
  405. }
  406. if (u64 >= (U64_LITERAL(1)<<4)) {
  407. u64 >>= 4;
  408. r += 4;
  409. }
  410. if (u64 >= (U64_LITERAL(1)<<2)) {
  411. u64 >>= 2;
  412. r += 2;
  413. }
  414. if (u64 >= (U64_LITERAL(1)<<1)) {
  415. u64 >>= 1;
  416. r += 1;
  417. }
  418. return r;
  419. }
  420. /** Return the power of 2 in range [1,UINT64_MAX] closest to <b>u64</b>. If
  421. * there are two powers of 2 equally close, round down. */
  422. uint64_t
  423. round_to_power_of_2(uint64_t u64)
  424. {
  425. int lg2;
  426. uint64_t low;
  427. uint64_t high;
  428. if (u64 == 0)
  429. return 1;
  430. lg2 = tor_log2(u64);
  431. low = U64_LITERAL(1) << lg2;
  432. if (lg2 == 63)
  433. return low;
  434. high = U64_LITERAL(1) << (lg2+1);
  435. if (high - u64 < u64 - low)
  436. return high;
  437. else
  438. return low;
  439. }
  440. /** Return the lowest x such that x is at least <b>number</b>, and x modulo
  441. * <b>divisor</b> == 0. */
  442. unsigned
  443. round_to_next_multiple_of(unsigned number, unsigned divisor)
  444. {
  445. number += divisor - 1;
  446. number -= number % divisor;
  447. return number;
  448. }
  449. /** Return the lowest x such that x is at least <b>number</b>, and x modulo
  450. * <b>divisor</b> == 0. */
  451. uint32_t
  452. round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor)
  453. {
  454. number += divisor - 1;
  455. number -= number % divisor;
  456. return number;
  457. }
  458. /** Return the lowest x such that x is at least <b>number</b>, and x modulo
  459. * <b>divisor</b> == 0. */
  460. uint64_t
  461. round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor)
  462. {
  463. number += divisor - 1;
  464. number -= number % divisor;
  465. return number;
  466. }
  467. /** Return the lowest x in [INT64_MIN, INT64_MAX] such that x is at least
  468. * <b>number</b>, and x modulo <b>divisor</b> == 0. */
  469. int64_t
  470. round_int64_to_next_multiple_of(int64_t number, int64_t divisor)
  471. {
  472. tor_assert(divisor > 0);
  473. if (number >= 0 && INT64_MAX - divisor + 1 >= number)
  474. number += divisor - 1;
  475. number -= number % divisor;
  476. return number;
  477. }
  478. /** Transform a random value <b>p</b> from the uniform distribution in
  479. * [0.0, 1.0[ into a Laplace distributed value with location parameter
  480. * <b>mu</b> and scale parameter <b>b</b> in [-Inf, Inf[. */
  481. double
  482. sample_laplace_distribution(double mu, double b, double p)
  483. {
  484. tor_assert(p >= 0.0 && p < 1.0);
  485. /* This is the "inverse cumulative distribution function" from:
  486. * http://en.wikipedia.org/wiki/Laplace_distribution */
  487. return mu - b * (p > 0.5 ? 1.0 : -1.0)
  488. * tor_mathlog(1.0 - 2.0 * fabs(p - 0.5));
  489. }
  490. /** Add random noise between INT64_MIN and INT64_MAX coming from a
  491. * Laplace distribution with mu = 0 and b = <b>delta_f</b>/<b>epsilon</b>
  492. * to <b>signal</b> based on the provided <b>random</b> value in
  493. * [0.0, 1.0[. */
  494. int64_t
  495. add_laplace_noise(int64_t signal, double random, double delta_f,
  496. double epsilon)
  497. {
  498. /* cast to int64_t intended */
  499. int64_t noise = sample_laplace_distribution(
  500. 0.0, /* just add noise, no further signal */
  501. delta_f / epsilon, random);
  502. if (noise > 0 && INT64_MAX - noise < signal)
  503. return INT64_MAX;
  504. else if (noise < 0 && INT64_MIN - noise > signal)
  505. return INT64_MIN;
  506. else
  507. return signal + noise;
  508. }
  509. /** Return the number of bits set in <b>v</b>. */
  510. int
  511. n_bits_set_u8(uint8_t v)
  512. {
  513. static const int nybble_table[] = {
  514. 0, /* 0000 */
  515. 1, /* 0001 */
  516. 1, /* 0010 */
  517. 2, /* 0011 */
  518. 1, /* 0100 */
  519. 2, /* 0101 */
  520. 2, /* 0110 */
  521. 3, /* 0111 */
  522. 1, /* 1000 */
  523. 2, /* 1001 */
  524. 2, /* 1010 */
  525. 3, /* 1011 */
  526. 2, /* 1100 */
  527. 3, /* 1101 */
  528. 3, /* 1110 */
  529. 4, /* 1111 */
  530. };
  531. return nybble_table[v & 15] + nybble_table[v>>4];
  532. }
  533. /* =====
  534. * String manipulation
  535. * ===== */
  536. /** Remove from the string <b>s</b> every character which appears in
  537. * <b>strip</b>. */
  538. void
  539. tor_strstrip(char *s, const char *strip)
  540. {
  541. char *read = s;
  542. while (*read) {
  543. if (strchr(strip, *read)) {
  544. ++read;
  545. } else {
  546. *s++ = *read++;
  547. }
  548. }
  549. *s = '\0';
  550. }
  551. /** Return a pointer to a NUL-terminated hexadecimal string encoding
  552. * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
  553. * result does not need to be deallocated, but repeated calls to
  554. * hex_str will trash old results.
  555. */
  556. const char *
  557. hex_str(const char *from, size_t fromlen)
  558. {
  559. static char buf[65];
  560. if (fromlen>(sizeof(buf)-1)/2)
  561. fromlen = (sizeof(buf)-1)/2;
  562. base16_encode(buf,sizeof(buf),from,fromlen);
  563. return buf;
  564. }
  565. /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
  566. * lowercase. */
  567. void
  568. tor_strlower(char *s)
  569. {
  570. while (*s) {
  571. *s = TOR_TOLOWER(*s);
  572. ++s;
  573. }
  574. }
  575. /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
  576. * lowercase. */
  577. void
  578. tor_strupper(char *s)
  579. {
  580. while (*s) {
  581. *s = TOR_TOUPPER(*s);
  582. ++s;
  583. }
  584. }
  585. /** Return 1 if every character in <b>s</b> is printable, else return 0.
  586. */
  587. int
  588. tor_strisprint(const char *s)
  589. {
  590. while (*s) {
  591. if (!TOR_ISPRINT(*s))
  592. return 0;
  593. s++;
  594. }
  595. return 1;
  596. }
  597. /** Return 1 if no character in <b>s</b> is uppercase, else return 0.
  598. */
  599. int
  600. tor_strisnonupper(const char *s)
  601. {
  602. while (*s) {
  603. if (TOR_ISUPPER(*s))
  604. return 0;
  605. s++;
  606. }
  607. return 1;
  608. }
  609. /** As strcmp, except that either string may be NULL. The NULL string is
  610. * considered to be before any non-NULL string. */
  611. int
  612. strcmp_opt(const char *s1, const char *s2)
  613. {
  614. if (!s1) {
  615. if (!s2)
  616. return 0;
  617. else
  618. return -1;
  619. } else if (!s2) {
  620. return 1;
  621. } else {
  622. return strcmp(s1, s2);
  623. }
  624. }
  625. /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
  626. * strcmp.
  627. */
  628. int
  629. strcmpstart(const char *s1, const char *s2)
  630. {
  631. size_t n = strlen(s2);
  632. return strncmp(s1, s2, n);
  633. }
  634. /** Compare the s1_len-byte string <b>s1</b> with <b>s2</b>,
  635. * without depending on a terminating nul in s1. Sorting order is first by
  636. * length, then lexically; return values are as for strcmp.
  637. */
  638. int
  639. strcmp_len(const char *s1, const char *s2, size_t s1_len)
  640. {
  641. size_t s2_len = strlen(s2);
  642. if (s1_len < s2_len)
  643. return -1;
  644. if (s1_len > s2_len)
  645. return 1;
  646. return fast_memcmp(s1, s2, s2_len);
  647. }
  648. /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
  649. * strcasecmp.
  650. */
  651. int
  652. strcasecmpstart(const char *s1, const char *s2)
  653. {
  654. size_t n = strlen(s2);
  655. return strncasecmp(s1, s2, n);
  656. }
  657. /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
  658. * strcmp.
  659. */
  660. int
  661. strcmpend(const char *s1, const char *s2)
  662. {
  663. size_t n1 = strlen(s1), n2 = strlen(s2);
  664. if (n2>n1)
  665. return strcmp(s1,s2);
  666. else
  667. return strncmp(s1+(n1-n2), s2, n2);
  668. }
  669. /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
  670. * strcasecmp.
  671. */
  672. int
  673. strcasecmpend(const char *s1, const char *s2)
  674. {
  675. size_t n1 = strlen(s1), n2 = strlen(s2);
  676. if (n2>n1) /* then they can't be the same; figure out which is bigger */
  677. return strcasecmp(s1,s2);
  678. else
  679. return strncasecmp(s1+(n1-n2), s2, n2);
  680. }
  681. /** Compare the value of the string <b>prefix</b> with the start of the
  682. * <b>memlen</b>-byte memory chunk at <b>mem</b>. Return as for strcmp.
  683. *
  684. * [As fast_memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is
  685. * less than strlen(prefix).]
  686. */
  687. int
  688. fast_memcmpstart(const void *mem, size_t memlen,
  689. const char *prefix)
  690. {
  691. size_t plen = strlen(prefix);
  692. if (memlen < plen)
  693. return -1;
  694. return fast_memcmp(mem, prefix, plen);
  695. }
  696. /** Given a nul-terminated string s, set every character before the nul
  697. * to zero. */
  698. void
  699. tor_strclear(char *s)
  700. {
  701. while (*s) {
  702. *s++ = '\0';
  703. }
  704. }
  705. /** Return a pointer to the first char of s that is not whitespace and
  706. * not a comment, or to the terminating NUL if no such character exists.
  707. */
  708. const char *
  709. eat_whitespace(const char *s)
  710. {
  711. tor_assert(s);
  712. while (1) {
  713. switch (*s) {
  714. case '\0':
  715. default:
  716. return s;
  717. case ' ':
  718. case '\t':
  719. case '\n':
  720. case '\r':
  721. ++s;
  722. break;
  723. case '#':
  724. ++s;
  725. while (*s && *s != '\n')
  726. ++s;
  727. }
  728. }
  729. }
  730. /** Return a pointer to the first char of s that is not whitespace and
  731. * not a comment, or to the terminating NUL if no such character exists.
  732. */
  733. const char *
  734. eat_whitespace_eos(const char *s, const char *eos)
  735. {
  736. tor_assert(s);
  737. tor_assert(eos && s <= eos);
  738. while (s < eos) {
  739. switch (*s) {
  740. case '\0':
  741. default:
  742. return s;
  743. case ' ':
  744. case '\t':
  745. case '\n':
  746. case '\r':
  747. ++s;
  748. break;
  749. case '#':
  750. ++s;
  751. while (s < eos && *s && *s != '\n')
  752. ++s;
  753. }
  754. }
  755. return s;
  756. }
  757. /** Return a pointer to the first char of s that is not a space or a tab
  758. * or a \\r, or to the terminating NUL if no such character exists. */
  759. const char *
  760. eat_whitespace_no_nl(const char *s)
  761. {
  762. while (*s == ' ' || *s == '\t' || *s == '\r')
  763. ++s;
  764. return s;
  765. }
  766. /** As eat_whitespace_no_nl, but stop at <b>eos</b> whether we have
  767. * found a non-whitespace character or not. */
  768. const char *
  769. eat_whitespace_eos_no_nl(const char *s, const char *eos)
  770. {
  771. while (s < eos && (*s == ' ' || *s == '\t' || *s == '\r'))
  772. ++s;
  773. return s;
  774. }
  775. /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
  776. * or to the terminating NUL if no such character exists.
  777. */
  778. const char *
  779. find_whitespace(const char *s)
  780. {
  781. /* tor_assert(s); */
  782. while (1) {
  783. switch (*s)
  784. {
  785. case '\0':
  786. case '#':
  787. case ' ':
  788. case '\r':
  789. case '\n':
  790. case '\t':
  791. return s;
  792. default:
  793. ++s;
  794. }
  795. }
  796. }
  797. /** As find_whitespace, but stop at <b>eos</b> whether we have found a
  798. * whitespace or not. */
  799. const char *
  800. find_whitespace_eos(const char *s, const char *eos)
  801. {
  802. /* tor_assert(s); */
  803. while (s < eos) {
  804. switch (*s)
  805. {
  806. case '\0':
  807. case '#':
  808. case ' ':
  809. case '\r':
  810. case '\n':
  811. case '\t':
  812. return s;
  813. default:
  814. ++s;
  815. }
  816. }
  817. return s;
  818. }
  819. /** Return the first occurrence of <b>needle</b> in <b>haystack</b> that
  820. * occurs at the start of a line (that is, at the beginning of <b>haystack</b>
  821. * or immediately after a newline). Return NULL if no such string is found.
  822. */
  823. const char *
  824. find_str_at_start_of_line(const char *haystack, const char *needle)
  825. {
  826. size_t needle_len = strlen(needle);
  827. do {
  828. if (!strncmp(haystack, needle, needle_len))
  829. return haystack;
  830. haystack = strchr(haystack, '\n');
  831. if (!haystack)
  832. return NULL;
  833. else
  834. ++haystack;
  835. } while (*haystack);
  836. return NULL;
  837. }
  838. /** Returns true if <b>string</b> could be a C identifier.
  839. A C identifier must begin with a letter or an underscore and the
  840. rest of its characters can be letters, numbers or underscores. No
  841. length limit is imposed. */
  842. int
  843. string_is_C_identifier(const char *string)
  844. {
  845. size_t iter;
  846. size_t length = strlen(string);
  847. if (!length)
  848. return 0;
  849. for (iter = 0; iter < length ; iter++) {
  850. if (iter == 0) {
  851. if (!(TOR_ISALPHA(string[iter]) ||
  852. string[iter] == '_'))
  853. return 0;
  854. } else {
  855. if (!(TOR_ISALPHA(string[iter]) ||
  856. TOR_ISDIGIT(string[iter]) ||
  857. string[iter] == '_'))
  858. return 0;
  859. }
  860. }
  861. return 1;
  862. }
  863. /** Return true iff the 'len' bytes at 'mem' are all zero. */
  864. int
  865. tor_mem_is_zero(const char *mem, size_t len)
  866. {
  867. static const char ZERO[] = {
  868. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
  869. };
  870. while (len >= sizeof(ZERO)) {
  871. /* It's safe to use fast_memcmp here, since the very worst thing an
  872. * attacker could learn is how many initial bytes of a secret were zero */
  873. if (fast_memcmp(mem, ZERO, sizeof(ZERO)))
  874. return 0;
  875. len -= sizeof(ZERO);
  876. mem += sizeof(ZERO);
  877. }
  878. /* Deal with leftover bytes. */
  879. if (len)
  880. return fast_memeq(mem, ZERO, len);
  881. return 1;
  882. }
  883. /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
  884. int
  885. tor_digest_is_zero(const char *digest)
  886. {
  887. static const uint8_t ZERO_DIGEST[] = {
  888. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
  889. };
  890. return tor_memeq(digest, ZERO_DIGEST, DIGEST_LEN);
  891. }
  892. /** Return true if <b>string</b> is a valid 'key=[value]' string.
  893. * "value" is optional, to indicate the empty string. Log at logging
  894. * <b>severity</b> if something ugly happens. */
  895. int
  896. string_is_key_value(int severity, const char *string)
  897. {
  898. /* position of equal sign in string */
  899. const char *equal_sign_pos = NULL;
  900. tor_assert(string);
  901. if (strlen(string) < 2) { /* "x=" is shortest args string */
  902. tor_log(severity, LD_GENERAL, "'%s' is too short to be a k=v value.",
  903. escaped(string));
  904. return 0;
  905. }
  906. equal_sign_pos = strchr(string, '=');
  907. if (!equal_sign_pos) {
  908. tor_log(severity, LD_GENERAL, "'%s' is not a k=v value.", escaped(string));
  909. return 0;
  910. }
  911. /* validate that the '=' is not in the beginning of the string. */
  912. if (equal_sign_pos == string) {
  913. tor_log(severity, LD_GENERAL, "'%s' is not a valid k=v value.",
  914. escaped(string));
  915. return 0;
  916. }
  917. return 1;
  918. }
  919. /** Return true if <b>string</b> represents a valid IPv4 adddress in
  920. * 'a.b.c.d' form.
  921. */
  922. int
  923. string_is_valid_ipv4_address(const char *string)
  924. {
  925. struct in_addr addr;
  926. return (tor_inet_pton(AF_INET,string,&addr) == 1);
  927. }
  928. /** Return true if <b>string</b> represents a valid IPv6 address in
  929. * a form that inet_pton() can parse.
  930. */
  931. int
  932. string_is_valid_ipv6_address(const char *string)
  933. {
  934. struct in6_addr addr;
  935. return (tor_inet_pton(AF_INET6,string,&addr) == 1);
  936. }
  937. /** Return true iff <b>string</b> matches a pattern of DNS names
  938. * that we allow Tor clients to connect to.
  939. */
  940. int
  941. string_is_valid_hostname(const char *string)
  942. {
  943. int result = 1;
  944. smartlist_t *components;
  945. components = smartlist_new();
  946. smartlist_split_string(components,string,".",0,0);
  947. SMARTLIST_FOREACH_BEGIN(components, char *, c) {
  948. if (c[0] == '-') {
  949. result = 0;
  950. break;
  951. }
  952. do {
  953. if ((*c >= 'a' && *c <= 'z') ||
  954. (*c >= 'A' && *c <= 'Z') ||
  955. (*c >= '0' && *c <= '9') ||
  956. (*c == '-'))
  957. c++;
  958. else
  959. result = 0;
  960. } while (result && *c);
  961. } SMARTLIST_FOREACH_END(c);
  962. SMARTLIST_FOREACH_BEGIN(components, char *, c) {
  963. tor_free(c);
  964. } SMARTLIST_FOREACH_END(c);
  965. smartlist_free(components);
  966. return result;
  967. }
  968. /** Return true iff the DIGEST256_LEN bytes in digest are all zero. */
  969. int
  970. tor_digest256_is_zero(const char *digest)
  971. {
  972. return tor_mem_is_zero(digest, DIGEST256_LEN);
  973. }
  974. /* Helper: common code to check whether the result of a strtol or strtoul or
  975. * strtoll is correct. */
  976. #define CHECK_STRTOX_RESULT() \
  977. /* Did an overflow occur? */ \
  978. if (errno == ERANGE) \
  979. goto err; \
  980. /* Was at least one character converted? */ \
  981. if (endptr == s) \
  982. goto err; \
  983. /* Were there unexpected unconverted characters? */ \
  984. if (!next && *endptr) \
  985. goto err; \
  986. /* Is r within limits? */ \
  987. if (r < min || r > max) \
  988. goto err; \
  989. if (ok) *ok = 1; \
  990. if (next) *next = endptr; \
  991. return r; \
  992. err: \
  993. if (ok) *ok = 0; \
  994. if (next) *next = endptr; \
  995. return 0
  996. /** Extract a long from the start of <b>s</b>, in the given numeric
  997. * <b>base</b>. If <b>base</b> is 0, <b>s</b> is parsed as a decimal,
  998. * octal, or hex number in the syntax of a C integer literal. If
  999. * there is unconverted data and <b>next</b> is provided, set
  1000. * *<b>next</b> to the first unconverted character. An error has
  1001. * occurred if no characters are converted; or if there are
  1002. * unconverted characters and <b>next</b> is NULL; or if the parsed
  1003. * value is not between <b>min</b> and <b>max</b>. When no error
  1004. * occurs, return the parsed value and set *<b>ok</b> (if provided) to
  1005. * 1. When an error occurs, return 0 and set *<b>ok</b> (if provided)
  1006. * to 0.
  1007. */
  1008. long
  1009. tor_parse_long(const char *s, int base, long min, long max,
  1010. int *ok, char **next)
  1011. {
  1012. char *endptr;
  1013. long r;
  1014. if (base < 0) {
  1015. if (ok)
  1016. *ok = 0;
  1017. return 0;
  1018. }
  1019. errno = 0;
  1020. r = strtol(s, &endptr, base);
  1021. CHECK_STRTOX_RESULT();
  1022. }
  1023. /** As tor_parse_long(), but return an unsigned long. */
  1024. unsigned long
  1025. tor_parse_ulong(const char *s, int base, unsigned long min,
  1026. unsigned long max, int *ok, char **next)
  1027. {
  1028. char *endptr;
  1029. unsigned long r;
  1030. if (base < 0) {
  1031. if (ok)
  1032. *ok = 0;
  1033. return 0;
  1034. }
  1035. errno = 0;
  1036. r = strtoul(s, &endptr, base);
  1037. CHECK_STRTOX_RESULT();
  1038. }
  1039. /** As tor_parse_long(), but return a double. */
  1040. double
  1041. tor_parse_double(const char *s, double min, double max, int *ok, char **next)
  1042. {
  1043. char *endptr;
  1044. double r;
  1045. errno = 0;
  1046. r = strtod(s, &endptr);
  1047. CHECK_STRTOX_RESULT();
  1048. }
  1049. /** As tor_parse_long, but return a uint64_t. Only base 10 is guaranteed to
  1050. * work for now. */
  1051. uint64_t
  1052. tor_parse_uint64(const char *s, int base, uint64_t min,
  1053. uint64_t max, int *ok, char **next)
  1054. {
  1055. char *endptr;
  1056. uint64_t r;
  1057. if (base < 0) {
  1058. if (ok)
  1059. *ok = 0;
  1060. return 0;
  1061. }
  1062. errno = 0;
  1063. #ifdef HAVE_STRTOULL
  1064. r = (uint64_t)strtoull(s, &endptr, base);
  1065. #elif defined(_WIN32)
  1066. #if defined(_MSC_VER) && _MSC_VER < 1300
  1067. tor_assert(base <= 10);
  1068. r = (uint64_t)_atoi64(s);
  1069. endptr = (char*)s;
  1070. while (TOR_ISSPACE(*endptr)) endptr++;
  1071. while (TOR_ISDIGIT(*endptr)) endptr++;
  1072. #else
  1073. r = (uint64_t)_strtoui64(s, &endptr, base);
  1074. #endif
  1075. #elif SIZEOF_LONG == 8
  1076. r = (uint64_t)strtoul(s, &endptr, base);
  1077. #else
  1078. #error "I don't know how to parse 64-bit numbers."
  1079. #endif
  1080. CHECK_STRTOX_RESULT();
  1081. }
  1082. /** Encode the <b>srclen</b> bytes at <b>src</b> in a NUL-terminated,
  1083. * uppercase hexadecimal string; store it in the <b>destlen</b>-byte buffer
  1084. * <b>dest</b>.
  1085. */
  1086. void
  1087. base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
  1088. {
  1089. const char *end;
  1090. char *cp;
  1091. tor_assert(destlen >= srclen*2+1);
  1092. tor_assert(destlen < SIZE_T_CEILING);
  1093. cp = dest;
  1094. end = src+srclen;
  1095. while (src<end) {
  1096. *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) >> 4 ];
  1097. *cp++ = "0123456789ABCDEF"[ (*(const uint8_t*)src) & 0xf ];
  1098. ++src;
  1099. }
  1100. *cp = '\0';
  1101. }
  1102. /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
  1103. static INLINE int
  1104. hex_decode_digit_(char c)
  1105. {
  1106. switch (c) {
  1107. case '0': return 0;
  1108. case '1': return 1;
  1109. case '2': return 2;
  1110. case '3': return 3;
  1111. case '4': return 4;
  1112. case '5': return 5;
  1113. case '6': return 6;
  1114. case '7': return 7;
  1115. case '8': return 8;
  1116. case '9': return 9;
  1117. case 'A': case 'a': return 10;
  1118. case 'B': case 'b': return 11;
  1119. case 'C': case 'c': return 12;
  1120. case 'D': case 'd': return 13;
  1121. case 'E': case 'e': return 14;
  1122. case 'F': case 'f': return 15;
  1123. default:
  1124. return -1;
  1125. }
  1126. }
  1127. /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
  1128. int
  1129. hex_decode_digit(char c)
  1130. {
  1131. return hex_decode_digit_(c);
  1132. }
  1133. /** Given a hexadecimal string of <b>srclen</b> bytes in <b>src</b>, decode it
  1134. * and store the result in the <b>destlen</b>-byte buffer at <b>dest</b>.
  1135. * Return 0 on success, -1 on failure. */
  1136. int
  1137. base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
  1138. {
  1139. const char *end;
  1140. int v1,v2;
  1141. if ((srclen % 2) != 0)
  1142. return -1;
  1143. if (destlen < srclen/2 || destlen > SIZE_T_CEILING)
  1144. return -1;
  1145. memset(dest, 0, destlen);
  1146. end = src+srclen;
  1147. while (src<end) {
  1148. v1 = hex_decode_digit_(*src);
  1149. v2 = hex_decode_digit_(*(src+1));
  1150. if (v1<0||v2<0)
  1151. return -1;
  1152. *(uint8_t*)dest = (v1<<4)|v2;
  1153. ++dest;
  1154. src+=2;
  1155. }
  1156. return 0;
  1157. }
  1158. /** Allocate and return a new string representing the contents of <b>s</b>,
  1159. * surrounded by quotes and using standard C escapes.
  1160. *
  1161. * Generally, we use this for logging values that come in over the network to
  1162. * keep them from tricking users, and for sending certain values to the
  1163. * controller.
  1164. *
  1165. * We trust values from the resolver, OS, configuration file, and command line
  1166. * to not be maliciously ill-formed. We validate incoming routerdescs and
  1167. * SOCKS requests and addresses from BEGIN cells as they're parsed;
  1168. * afterwards, we trust them as non-malicious.
  1169. */
  1170. char *
  1171. esc_for_log(const char *s)
  1172. {
  1173. const char *cp;
  1174. char *result, *outp;
  1175. size_t len = 3;
  1176. if (!s) {
  1177. return tor_strdup("(null)");
  1178. }
  1179. for (cp = s; *cp; ++cp) {
  1180. switch (*cp) {
  1181. case '\\':
  1182. case '\"':
  1183. case '\'':
  1184. case '\r':
  1185. case '\n':
  1186. case '\t':
  1187. len += 2;
  1188. break;
  1189. default:
  1190. if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
  1191. ++len;
  1192. else
  1193. len += 4;
  1194. break;
  1195. }
  1196. }
  1197. tor_assert(len <= SSIZE_MAX);
  1198. result = outp = tor_malloc(len);
  1199. *outp++ = '\"';
  1200. for (cp = s; *cp; ++cp) {
  1201. /* This assertion should always succeed, since we will write at least
  1202. * one char here, and two chars for closing quote and nul later */
  1203. tor_assert((outp-result) < (ssize_t)len-2);
  1204. switch (*cp) {
  1205. case '\\':
  1206. case '\"':
  1207. case '\'':
  1208. *outp++ = '\\';
  1209. *outp++ = *cp;
  1210. break;
  1211. case '\n':
  1212. *outp++ = '\\';
  1213. *outp++ = 'n';
  1214. break;
  1215. case '\t':
  1216. *outp++ = '\\';
  1217. *outp++ = 't';
  1218. break;
  1219. case '\r':
  1220. *outp++ = '\\';
  1221. *outp++ = 'r';
  1222. break;
  1223. default:
  1224. if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
  1225. *outp++ = *cp;
  1226. } else {
  1227. tor_assert((outp-result) < (ssize_t)len-4);
  1228. tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
  1229. outp += 4;
  1230. }
  1231. break;
  1232. }
  1233. }
  1234. tor_assert((outp-result) <= (ssize_t)len-2);
  1235. *outp++ = '\"';
  1236. *outp++ = 0;
  1237. return result;
  1238. }
  1239. /** Allocate and return a new string representing the contents of <b>s</b>,
  1240. * surrounded by quotes and using standard C escapes.
  1241. *
  1242. * THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
  1243. * thread. Also, each call invalidates the last-returned value, so don't
  1244. * try log_warn(LD_GENERAL, "%s %s", escaped(a), escaped(b));
  1245. */
  1246. const char *
  1247. escaped(const char *s)
  1248. {
  1249. static char *escaped_val_ = NULL;
  1250. tor_free(escaped_val_);
  1251. if (s)
  1252. escaped_val_ = esc_for_log(s);
  1253. else
  1254. escaped_val_ = NULL;
  1255. return escaped_val_;
  1256. }
  1257. /** Return a newly allocated string equal to <b>string</b>, except that every
  1258. * character in <b>chars_to_escape</b> is preceded by a backslash. */
  1259. char *
  1260. tor_escape_str_for_pt_args(const char *string, const char *chars_to_escape)
  1261. {
  1262. char *new_string = NULL;
  1263. char *new_cp = NULL;
  1264. size_t length, new_length;
  1265. tor_assert(string);
  1266. length = strlen(string);
  1267. if (!length) /* If we were given the empty string, return the same. */
  1268. return tor_strdup("");
  1269. /* (new_length > SIZE_MAX) => ((length * 2) + 1 > SIZE_MAX) =>
  1270. (length*2 > SIZE_MAX - 1) => (length > (SIZE_MAX - 1)/2) */
  1271. if (length > (SIZE_MAX - 1)/2) /* check for overflow */
  1272. return NULL;
  1273. /* this should be enough even if all characters must be escaped */
  1274. new_length = (length * 2) + 1;
  1275. new_string = new_cp = tor_malloc(new_length);
  1276. while (*string) {
  1277. if (strchr(chars_to_escape, *string))
  1278. *new_cp++ = '\\';
  1279. *new_cp++ = *string++;
  1280. }
  1281. *new_cp = '\0'; /* NUL-terminate the new string */
  1282. return new_string;
  1283. }
  1284. /* =====
  1285. * Time
  1286. * ===== */
  1287. /** Return the number of microseconds elapsed between *start and *end.
  1288. */
  1289. long
  1290. tv_udiff(const struct timeval *start, const struct timeval *end)
  1291. {
  1292. long udiff;
  1293. long secdiff = end->tv_sec - start->tv_sec;
  1294. if (labs(secdiff+1) > LONG_MAX/1000000) {
  1295. log_warn(LD_GENERAL, "comparing times on microsecond detail too far "
  1296. "apart: %ld seconds", secdiff);
  1297. return LONG_MAX;
  1298. }
  1299. udiff = secdiff*1000000L + (end->tv_usec - start->tv_usec);
  1300. return udiff;
  1301. }
  1302. /** Return the number of milliseconds elapsed between *start and *end.
  1303. */
  1304. long
  1305. tv_mdiff(const struct timeval *start, const struct timeval *end)
  1306. {
  1307. long mdiff;
  1308. long secdiff = end->tv_sec - start->tv_sec;
  1309. if (labs(secdiff+1) > LONG_MAX/1000) {
  1310. log_warn(LD_GENERAL, "comparing times on millisecond detail too far "
  1311. "apart: %ld seconds", secdiff);
  1312. return LONG_MAX;
  1313. }
  1314. /* Subtract and round */
  1315. mdiff = secdiff*1000L +
  1316. ((long)end->tv_usec - (long)start->tv_usec + 500L) / 1000L;
  1317. return mdiff;
  1318. }
  1319. /**
  1320. * Converts timeval to milliseconds.
  1321. */
  1322. int64_t
  1323. tv_to_msec(const struct timeval *tv)
  1324. {
  1325. int64_t conv = ((int64_t)tv->tv_sec)*1000L;
  1326. /* Round ghetto-style */
  1327. conv += ((int64_t)tv->tv_usec+500)/1000L;
  1328. return conv;
  1329. }
  1330. /** Yield true iff <b>y</b> is a leap-year. */
  1331. #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
  1332. /** Helper: Return the number of leap-days between Jan 1, y1 and Jan 1, y2. */
  1333. static int
  1334. n_leapdays(int y1, int y2)
  1335. {
  1336. --y1;
  1337. --y2;
  1338. return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
  1339. }
  1340. /** Number of days per month in non-leap year; used by tor_timegm and
  1341. * parse_rfc1123_time. */
  1342. static const int days_per_month[] =
  1343. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  1344. /** Compute a time_t given a struct tm. The result is given in UTC, and
  1345. * does not account for leap seconds. Return 0 on success, -1 on failure.
  1346. */
  1347. int
  1348. tor_timegm(const struct tm *tm, time_t *time_out)
  1349. {
  1350. /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
  1351. * It's way more brute-force than fiddling with tzset().
  1352. */
  1353. time_t year, days, hours, minutes, seconds;
  1354. int i, invalid_year, dpm;
  1355. /* avoid int overflow on addition */
  1356. if (tm->tm_year < INT32_MAX-1900) {
  1357. year = tm->tm_year + 1900;
  1358. } else {
  1359. /* clamp year */
  1360. year = INT32_MAX;
  1361. }
  1362. invalid_year = (year < 1970 || tm->tm_year >= INT32_MAX-1900);
  1363. if (tm->tm_mon >= 0 && tm->tm_mon <= 11) {
  1364. dpm = days_per_month[tm->tm_mon];
  1365. if (tm->tm_mon == 1 && !invalid_year && IS_LEAPYEAR(tm->tm_year)) {
  1366. dpm = 29;
  1367. }
  1368. } else {
  1369. /* invalid month - default to 0 days per month */
  1370. dpm = 0;
  1371. }
  1372. if (invalid_year ||
  1373. tm->tm_mon < 0 || tm->tm_mon > 11 ||
  1374. tm->tm_mday < 1 || tm->tm_mday > dpm ||
  1375. tm->tm_hour < 0 || tm->tm_hour > 23 ||
  1376. tm->tm_min < 0 || tm->tm_min > 59 ||
  1377. tm->tm_sec < 0 || tm->tm_sec > 60) {
  1378. log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
  1379. return -1;
  1380. }
  1381. days = 365 * (year-1970) + n_leapdays(1970,(int)year);
  1382. for (i = 0; i < tm->tm_mon; ++i)
  1383. days += days_per_month[i];
  1384. if (tm->tm_mon > 1 && IS_LEAPYEAR(year))
  1385. ++days;
  1386. days += tm->tm_mday - 1;
  1387. hours = days*24 + tm->tm_hour;
  1388. minutes = hours*60 + tm->tm_min;
  1389. seconds = minutes*60 + tm->tm_sec;
  1390. *time_out = seconds;
  1391. return 0;
  1392. }
  1393. /* strftime is locale-specific, so we need to replace those parts */
  1394. /** A c-locale array of 3-letter names of weekdays, starting with Sun. */
  1395. static const char *WEEKDAY_NAMES[] =
  1396. { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  1397. /** A c-locale array of 3-letter names of months, starting with Jan. */
  1398. static const char *MONTH_NAMES[] =
  1399. { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  1400. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  1401. /** Set <b>buf</b> to the RFC1123 encoding of the UTC value of <b>t</b>.
  1402. * The buffer must be at least RFC1123_TIME_LEN+1 bytes long.
  1403. *
  1404. * (RFC1123 format is "Fri, 29 Sep 2006 15:54:20 GMT". Note the "GMT"
  1405. * rather than "UTC".)
  1406. */
  1407. void
  1408. format_rfc1123_time(char *buf, time_t t)
  1409. {
  1410. struct tm tm;
  1411. tor_gmtime_r(&t, &tm);
  1412. strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
  1413. tor_assert(tm.tm_wday >= 0);
  1414. tor_assert(tm.tm_wday <= 6);
  1415. memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
  1416. tor_assert(tm.tm_mon >= 0);
  1417. tor_assert(tm.tm_mon <= 11);
  1418. memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
  1419. }
  1420. /** Parse the (a subset of) the RFC1123 encoding of some time (in UTC) from
  1421. * <b>buf</b>, and store the result in *<b>t</b>.
  1422. *
  1423. * Note that we only accept the subset generated by format_rfc1123_time above,
  1424. * not the full range of formats suggested by RFC 1123.
  1425. *
  1426. * Return 0 on success, -1 on failure.
  1427. */
  1428. int
  1429. parse_rfc1123_time(const char *buf, time_t *t)
  1430. {
  1431. struct tm tm;
  1432. char month[4];
  1433. char weekday[4];
  1434. int i, m, invalid_year;
  1435. unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
  1436. unsigned dpm;
  1437. if (strlen(buf) != RFC1123_TIME_LEN)
  1438. return -1;
  1439. memset(&tm, 0, sizeof(tm));
  1440. if (tor_sscanf(buf, "%3s, %2u %3s %u %2u:%2u:%2u GMT", weekday,
  1441. &tm_mday, month, &tm_year, &tm_hour,
  1442. &tm_min, &tm_sec) < 7) {
  1443. char *esc = esc_for_log(buf);
  1444. log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
  1445. tor_free(esc);
  1446. return -1;
  1447. }
  1448. m = -1;
  1449. for (i = 0; i < 12; ++i) {
  1450. if (!strcmp(month, MONTH_NAMES[i])) {
  1451. m = i;
  1452. break;
  1453. }
  1454. }
  1455. if (m<0) {
  1456. char *esc = esc_for_log(buf);
  1457. log_warn(LD_GENERAL, "Got invalid RFC1123 time %s: No such month", esc);
  1458. tor_free(esc);
  1459. return -1;
  1460. }
  1461. tm.tm_mon = m;
  1462. invalid_year = (tm_year >= INT32_MAX || tm_year < 1970);
  1463. tor_assert(m >= 0 && m <= 11);
  1464. dpm = days_per_month[m];
  1465. if (m == 1 && !invalid_year && IS_LEAPYEAR(tm_year)) {
  1466. dpm = 29;
  1467. }
  1468. if (invalid_year || tm_mday < 1 || tm_mday > dpm ||
  1469. tm_hour > 23 || tm_min > 59 || tm_sec > 60) {
  1470. char *esc = esc_for_log(buf);
  1471. log_warn(LD_GENERAL, "Got invalid RFC1123 time %s", esc);
  1472. tor_free(esc);
  1473. return -1;
  1474. }
  1475. tm.tm_mday = (int)tm_mday;
  1476. tm.tm_year = (int)tm_year;
  1477. tm.tm_hour = (int)tm_hour;
  1478. tm.tm_min = (int)tm_min;
  1479. tm.tm_sec = (int)tm_sec;
  1480. if (tm.tm_year < 1970) {
  1481. char *esc = esc_for_log(buf);
  1482. log_warn(LD_GENERAL,
  1483. "Got invalid RFC1123 time %s. (Before 1970)", esc);
  1484. tor_free(esc);
  1485. return -1;
  1486. }
  1487. tm.tm_year -= 1900;
  1488. return tor_timegm(&tm, t);
  1489. }
  1490. /** Set <b>buf</b> to the ISO8601 encoding of the local value of <b>t</b>.
  1491. * The buffer must be at least ISO_TIME_LEN+1 bytes long.
  1492. *
  1493. * (ISO8601 format is 2006-10-29 10:57:20)
  1494. */
  1495. void
  1496. format_local_iso_time(char *buf, time_t t)
  1497. {
  1498. struct tm tm;
  1499. strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
  1500. }
  1501. /** Set <b>buf</b> to the ISO8601 encoding of the GMT value of <b>t</b>.
  1502. * The buffer must be at least ISO_TIME_LEN+1 bytes long.
  1503. */
  1504. void
  1505. format_iso_time(char *buf, time_t t)
  1506. {
  1507. struct tm tm;
  1508. strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
  1509. }
  1510. /** As format_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid
  1511. * embedding an internal space. */
  1512. void
  1513. format_iso_time_nospace(char *buf, time_t t)
  1514. {
  1515. format_iso_time(buf, t);
  1516. buf[10] = 'T';
  1517. }
  1518. /** As format_iso_time_nospace, but include microseconds in decimal
  1519. * fixed-point format. Requires that buf be at least ISO_TIME_USEC_LEN+1
  1520. * bytes long. */
  1521. void
  1522. format_iso_time_nospace_usec(char *buf, const struct timeval *tv)
  1523. {
  1524. tor_assert(tv);
  1525. format_iso_time_nospace(buf, (time_t)tv->tv_sec);
  1526. tor_snprintf(buf+ISO_TIME_LEN, 8, ".%06d", (int)tv->tv_usec);
  1527. }
  1528. /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
  1529. * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
  1530. * failure. Ignore extraneous stuff in <b>cp</b> after the end of the time
  1531. * string, unless <b>strict</b> is set. */
  1532. int
  1533. parse_iso_time_(const char *cp, time_t *t, int strict)
  1534. {
  1535. struct tm st_tm;
  1536. unsigned int year=0, month=0, day=0, hour=0, minute=0, second=0;
  1537. int n_fields;
  1538. char extra_char;
  1539. n_fields = tor_sscanf(cp, "%u-%2u-%2u %2u:%2u:%2u%c", &year, &month,
  1540. &day, &hour, &minute, &second, &extra_char);
  1541. if (strict ? (n_fields != 6) : (n_fields < 6)) {
  1542. char *esc = esc_for_log(cp);
  1543. log_warn(LD_GENERAL, "ISO time %s was unparseable", esc);
  1544. tor_free(esc);
  1545. return -1;
  1546. }
  1547. if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
  1548. hour > 23 || minute > 59 || second > 60 || year >= INT32_MAX) {
  1549. char *esc = esc_for_log(cp);
  1550. log_warn(LD_GENERAL, "ISO time %s was nonsensical", esc);
  1551. tor_free(esc);
  1552. return -1;
  1553. }
  1554. st_tm.tm_year = (int)year-1900;
  1555. st_tm.tm_mon = month-1;
  1556. st_tm.tm_mday = day;
  1557. st_tm.tm_hour = hour;
  1558. st_tm.tm_min = minute;
  1559. st_tm.tm_sec = second;
  1560. if (st_tm.tm_year < 70) {
  1561. char *esc = esc_for_log(cp);
  1562. log_warn(LD_GENERAL, "Got invalid ISO time %s. (Before 1970)", esc);
  1563. tor_free(esc);
  1564. return -1;
  1565. }
  1566. return tor_timegm(&st_tm, t);
  1567. }
  1568. /** Given an ISO-formatted UTC time value (after the epoch) in <b>cp</b>,
  1569. * parse it and store its value in *<b>t</b>. Return 0 on success, -1 on
  1570. * failure. Reject the string if any characters are present after the time.
  1571. */
  1572. int
  1573. parse_iso_time(const char *cp, time_t *t)
  1574. {
  1575. return parse_iso_time_(cp, t, 1);
  1576. }
  1577. /** Given a <b>date</b> in one of the three formats allowed by HTTP (ugh),
  1578. * parse it into <b>tm</b>. Return 0 on success, negative on failure. */
  1579. int
  1580. parse_http_time(const char *date, struct tm *tm)
  1581. {
  1582. const char *cp;
  1583. char month[4];
  1584. char wkday[4];
  1585. int i;
  1586. unsigned tm_mday, tm_year, tm_hour, tm_min, tm_sec;
  1587. tor_assert(tm);
  1588. memset(tm, 0, sizeof(*tm));
  1589. /* First, try RFC1123 or RFC850 format: skip the weekday. */
  1590. if ((cp = strchr(date, ','))) {
  1591. ++cp;
  1592. if (*cp != ' ')
  1593. return -1;
  1594. ++cp;
  1595. if (tor_sscanf(cp, "%2u %3s %4u %2u:%2u:%2u GMT",
  1596. &tm_mday, month, &tm_year,
  1597. &tm_hour, &tm_min, &tm_sec) == 6) {
  1598. /* rfc1123-date */
  1599. tm_year -= 1900;
  1600. } else if (tor_sscanf(cp, "%2u-%3s-%2u %2u:%2u:%2u GMT",
  1601. &tm_mday, month, &tm_year,
  1602. &tm_hour, &tm_min, &tm_sec) == 6) {
  1603. /* rfc850-date */
  1604. } else {
  1605. return -1;
  1606. }
  1607. } else {
  1608. /* No comma; possibly asctime() format. */
  1609. if (tor_sscanf(date, "%3s %3s %2u %2u:%2u:%2u %4u",
  1610. wkday, month, &tm_mday,
  1611. &tm_hour, &tm_min, &tm_sec, &tm_year) == 7) {
  1612. tm_year -= 1900;
  1613. } else {
  1614. return -1;
  1615. }
  1616. }
  1617. tm->tm_mday = (int)tm_mday;
  1618. tm->tm_year = (int)tm_year;
  1619. tm->tm_hour = (int)tm_hour;
  1620. tm->tm_min = (int)tm_min;
  1621. tm->tm_sec = (int)tm_sec;
  1622. month[3] = '\0';
  1623. /* Okay, now decode the month. */
  1624. /* set tm->tm_mon to dummy value so the check below fails. */
  1625. tm->tm_mon = -1;
  1626. for (i = 0; i < 12; ++i) {
  1627. if (!strcasecmp(MONTH_NAMES[i], month)) {
  1628. tm->tm_mon = i;
  1629. }
  1630. }
  1631. if (tm->tm_year < 0 ||
  1632. tm->tm_mon < 0 || tm->tm_mon > 11 ||
  1633. tm->tm_mday < 1 || tm->tm_mday > 31 ||
  1634. tm->tm_hour < 0 || tm->tm_hour > 23 ||
  1635. tm->tm_min < 0 || tm->tm_min > 59 ||
  1636. tm->tm_sec < 0 || tm->tm_sec > 60)
  1637. return -1; /* Out of range, or bad month. */
  1638. return 0;
  1639. }
  1640. /** Given an <b>interval</b> in seconds, try to write it to the
  1641. * <b>out_len</b>-byte buffer in <b>out</b> in a human-readable form.
  1642. * Return 0 on success, -1 on failure.
  1643. */
  1644. int
  1645. format_time_interval(char *out, size_t out_len, long interval)
  1646. {
  1647. /* We only report seconds if there's no hours. */
  1648. long sec = 0, min = 0, hour = 0, day = 0;
  1649. /* -LONG_MIN is LONG_MAX + 1, which causes signed overflow */
  1650. if (interval < -LONG_MAX)
  1651. interval = LONG_MAX;
  1652. else if (interval < 0)
  1653. interval = -interval;
  1654. if (interval >= 86400) {
  1655. day = interval / 86400;
  1656. interval %= 86400;
  1657. }
  1658. if (interval >= 3600) {
  1659. hour = interval / 3600;
  1660. interval %= 3600;
  1661. }
  1662. if (interval >= 60) {
  1663. min = interval / 60;
  1664. interval %= 60;
  1665. }
  1666. sec = interval;
  1667. if (day) {
  1668. return tor_snprintf(out, out_len, "%ld days, %ld hours, %ld minutes",
  1669. day, hour, min);
  1670. } else if (hour) {
  1671. return tor_snprintf(out, out_len, "%ld hours, %ld minutes", hour, min);
  1672. } else if (min) {
  1673. return tor_snprintf(out, out_len, "%ld minutes, %ld seconds", min, sec);
  1674. } else {
  1675. return tor_snprintf(out, out_len, "%ld seconds", sec);
  1676. }
  1677. }
  1678. /* =====
  1679. * Cached time
  1680. * ===== */
  1681. #ifndef TIME_IS_FAST
  1682. /** Cached estimate of the current time. Updated around once per second;
  1683. * may be a few seconds off if we are really busy. This is a hack to avoid
  1684. * calling time(NULL) (which not everybody has optimized) on critical paths.
  1685. */
  1686. static time_t cached_approx_time = 0;
  1687. /** Return a cached estimate of the current time from when
  1688. * update_approx_time() was last called. This is a hack to avoid calling
  1689. * time(NULL) on critical paths: please do not even think of calling it
  1690. * anywhere else. */
  1691. time_t
  1692. approx_time(void)
  1693. {
  1694. return cached_approx_time;
  1695. }
  1696. /** Update the cached estimate of the current time. This function SHOULD be
  1697. * called once per second, and MUST be called before the first call to
  1698. * get_approx_time. */
  1699. void
  1700. update_approx_time(time_t now)
  1701. {
  1702. cached_approx_time = now;
  1703. }
  1704. #endif
  1705. /* =====
  1706. * Rate limiting
  1707. * ===== */
  1708. /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return the number
  1709. * of calls to rate_limit_is_ready (including this one!) since the last time
  1710. * rate_limit_is_ready returned nonzero. Otherwise return 0. */
  1711. static int
  1712. rate_limit_is_ready(ratelim_t *lim, time_t now)
  1713. {
  1714. if (lim->rate + lim->last_allowed <= now) {
  1715. int res = lim->n_calls_since_last_time + 1;
  1716. lim->last_allowed = now;
  1717. lim->n_calls_since_last_time = 0;
  1718. return res;
  1719. } else {
  1720. ++lim->n_calls_since_last_time;
  1721. return 0;
  1722. }
  1723. }
  1724. /** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return a newly
  1725. * allocated string indicating how many messages were suppressed, suitable to
  1726. * append to a log message. Otherwise return NULL. */
  1727. char *
  1728. rate_limit_log(ratelim_t *lim, time_t now)
  1729. {
  1730. int n;
  1731. if ((n = rate_limit_is_ready(lim, now))) {
  1732. if (n == 1) {
  1733. return tor_strdup("");
  1734. } else {
  1735. char *cp=NULL;
  1736. tor_asprintf(&cp,
  1737. " [%d similar message(s) suppressed in last %d seconds]",
  1738. n-1, lim->rate);
  1739. return cp;
  1740. }
  1741. } else {
  1742. return NULL;
  1743. }
  1744. }
  1745. /* =====
  1746. * File helpers
  1747. * ===== */
  1748. /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
  1749. * must be 1 if fd was returned by socket() or accept(), and 0 if fd
  1750. * was returned by open(). Return the number of bytes written, or -1
  1751. * on error. Only use if fd is a blocking fd. */
  1752. ssize_t
  1753. write_all(tor_socket_t fd, const char *buf, size_t count, int isSocket)
  1754. {
  1755. size_t written = 0;
  1756. ssize_t result;
  1757. tor_assert(count < SSIZE_MAX);
  1758. while (written != count) {
  1759. if (isSocket)
  1760. result = tor_socket_send(fd, buf+written, count-written, 0);
  1761. else
  1762. result = write((int)fd, buf+written, count-written);
  1763. if (result<0)
  1764. return -1;
  1765. written += result;
  1766. }
  1767. return (ssize_t)count;
  1768. }
  1769. /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
  1770. * or reach the end of the file. <b>isSocket</b> must be 1 if fd
  1771. * was returned by socket() or accept(), and 0 if fd was returned by
  1772. * open(). Return the number of bytes read, or -1 on error. Only use
  1773. * if fd is a blocking fd. */
  1774. ssize_t
  1775. read_all(tor_socket_t fd, char *buf, size_t count, int isSocket)
  1776. {
  1777. size_t numread = 0;
  1778. ssize_t result;
  1779. if (count > SIZE_T_CEILING || count > SSIZE_MAX)
  1780. return -1;
  1781. while (numread != count) {
  1782. if (isSocket)
  1783. result = tor_socket_recv(fd, buf+numread, count-numread, 0);
  1784. else
  1785. result = read((int)fd, buf+numread, count-numread);
  1786. if (result<0)
  1787. return -1;
  1788. else if (result == 0)
  1789. break;
  1790. numread += result;
  1791. }
  1792. return (ssize_t)numread;
  1793. }
  1794. /*
  1795. * Filesystem operations.
  1796. */
  1797. /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
  1798. * we do nothing. On Windows, we remove a trailing slash, unless the path is
  1799. * the root of a disk. */
  1800. static void
  1801. clean_name_for_stat(char *name)
  1802. {
  1803. #ifdef _WIN32
  1804. size_t len = strlen(name);
  1805. if (!len)
  1806. return;
  1807. if (name[len-1]=='\\' || name[len-1]=='/') {
  1808. if (len == 1 || (len==3 && name[1]==':'))
  1809. return;
  1810. name[len-1]='\0';
  1811. }
  1812. #else
  1813. (void)name;
  1814. #endif
  1815. }
  1816. /** Return:
  1817. * FN_ERROR if filename can't be read, is NULL, or is zero-length,
  1818. * FN_NOENT if it doesn't exist,
  1819. * FN_FILE if it is a non-empty regular file, or a FIFO on unix-like systems,
  1820. * FN_EMPTY for zero-byte regular files,
  1821. * FN_DIR if it's a directory, and
  1822. * FN_ERROR for any other file type.
  1823. * On FN_ERROR and FN_NOENT, sets errno. (errno is not set when FN_ERROR
  1824. * is returned due to an unhandled file type.) */
  1825. file_status_t
  1826. file_status(const char *fname)
  1827. {
  1828. struct stat st;
  1829. char *f;
  1830. int r;
  1831. if (!fname || strlen(fname) == 0) {
  1832. return FN_ERROR;
  1833. }
  1834. f = tor_strdup(fname);
  1835. clean_name_for_stat(f);
  1836. log_debug(LD_FS, "stat()ing %s", f);
  1837. r = stat(sandbox_intern_string(f), &st);
  1838. tor_free(f);
  1839. if (r) {
  1840. if (errno == ENOENT) {
  1841. return FN_NOENT;
  1842. }
  1843. return FN_ERROR;
  1844. }
  1845. if (st.st_mode & S_IFDIR) {
  1846. return FN_DIR;
  1847. } else if (st.st_mode & S_IFREG) {
  1848. if (st.st_size > 0) {
  1849. return FN_FILE;
  1850. } else if (st.st_size == 0) {
  1851. return FN_EMPTY;
  1852. } else {
  1853. return FN_ERROR;
  1854. }
  1855. #ifndef _WIN32
  1856. } else if (st.st_mode & S_IFIFO) {
  1857. return FN_FILE;
  1858. #endif
  1859. } else {
  1860. return FN_ERROR;
  1861. }
  1862. }
  1863. /** Check whether <b>dirname</b> exists and is private. If yes return 0. If
  1864. * it does not exist, and <b>check</b>&CPD_CREATE is set, try to create it
  1865. * and return 0 on success. If it does not exist, and
  1866. * <b>check</b>&CPD_CHECK, and we think we can create it, return 0. Else
  1867. * return -1. If CPD_GROUP_OK is set, then it's okay if the directory
  1868. * is group-readable, but in all cases we create the directory mode 0700.
  1869. * If CPD_GROUP_READ is set, existing directory behaves as CPD_GROUP_OK and
  1870. * if the directory is created it will use mode 0750 with group read
  1871. * permission. Group read privileges also assume execute permission
  1872. * as norm for directories. If CPD_CHECK_MODE_ONLY is set, then we don't
  1873. * alter the directory permissions if they are too permissive:
  1874. * we just return -1.
  1875. * When effective_user is not NULL, check permissions against the given user
  1876. * and its primary group.
  1877. */
  1878. int
  1879. check_private_dir(const char *dirname, cpd_check_t check,
  1880. const char *effective_user)
  1881. {
  1882. int r;
  1883. struct stat st;
  1884. char *f;
  1885. #ifndef _WIN32
  1886. unsigned unwanted_bits = 0;
  1887. const struct passwd *pw = NULL;
  1888. uid_t running_uid;
  1889. gid_t running_gid;
  1890. #else
  1891. (void)effective_user;
  1892. #endif
  1893. tor_assert(dirname);
  1894. f = tor_strdup(dirname);
  1895. clean_name_for_stat(f);
  1896. log_debug(LD_FS, "stat()ing %s", f);
  1897. r = stat(sandbox_intern_string(f), &st);
  1898. tor_free(f);
  1899. if (r) {
  1900. if (errno != ENOENT) {
  1901. log_warn(LD_FS, "Directory %s cannot be read: %s", dirname,
  1902. strerror(errno));
  1903. return -1;
  1904. }
  1905. if (check & CPD_CREATE) {
  1906. log_info(LD_GENERAL, "Creating directory %s", dirname);
  1907. #if defined (_WIN32)
  1908. r = mkdir(dirname);
  1909. #else
  1910. if (check & CPD_GROUP_READ) {
  1911. r = mkdir(dirname, 0750);
  1912. } else {
  1913. r = mkdir(dirname, 0700);
  1914. }
  1915. #endif
  1916. if (r) {
  1917. log_warn(LD_FS, "Error creating directory %s: %s", dirname,
  1918. strerror(errno));
  1919. return -1;
  1920. }
  1921. } else if (!(check & CPD_CHECK)) {
  1922. log_warn(LD_FS, "Directory %s does not exist.", dirname);
  1923. return -1;
  1924. }
  1925. /* XXXX In the case where check==CPD_CHECK, we should look at the
  1926. * parent directory a little harder. */
  1927. return 0;
  1928. }
  1929. if (!(st.st_mode & S_IFDIR)) {
  1930. log_warn(LD_FS, "%s is not a directory", dirname);
  1931. return -1;
  1932. }
  1933. #ifndef _WIN32
  1934. if (effective_user) {
  1935. /* Look up the user and group information.
  1936. * If we have a problem, bail out. */
  1937. pw = tor_getpwnam(effective_user);
  1938. if (pw == NULL) {
  1939. log_warn(LD_CONFIG, "Error setting configured user: %s not found",
  1940. effective_user);
  1941. return -1;
  1942. }
  1943. running_uid = pw->pw_uid;
  1944. running_gid = pw->pw_gid;
  1945. } else {
  1946. running_uid = getuid();
  1947. running_gid = getgid();
  1948. }
  1949. if (st.st_uid != running_uid) {
  1950. const struct passwd *pw = NULL;
  1951. char *process_ownername = NULL;
  1952. pw = tor_getpwuid(running_uid);
  1953. process_ownername = pw ? tor_strdup(pw->pw_name) : tor_strdup("<unknown>");
  1954. pw = tor_getpwuid(st.st_uid);
  1955. log_warn(LD_FS, "%s is not owned by this user (%s, %d) but by "
  1956. "%s (%d). Perhaps you are running Tor as the wrong user?",
  1957. dirname, process_ownername, (int)running_uid,
  1958. pw ? pw->pw_name : "<unknown>", (int)st.st_uid);
  1959. tor_free(process_ownername);
  1960. return -1;
  1961. }
  1962. if ( (check & (CPD_GROUP_OK|CPD_GROUP_READ))
  1963. && (st.st_gid != running_gid) ) {
  1964. struct group *gr;
  1965. char *process_groupname = NULL;
  1966. gr = getgrgid(running_gid);
  1967. process_groupname = gr ? tor_strdup(gr->gr_name) : tor_strdup("<unknown>");
  1968. gr = getgrgid(st.st_gid);
  1969. log_warn(LD_FS, "%s is not owned by this group (%s, %d) but by group "
  1970. "%s (%d). Are you running Tor as the wrong user?",
  1971. dirname, process_groupname, (int)running_gid,
  1972. gr ? gr->gr_name : "<unknown>", (int)st.st_gid);
  1973. tor_free(process_groupname);
  1974. return -1;
  1975. }
  1976. if (check & (CPD_GROUP_OK|CPD_GROUP_READ)) {
  1977. unwanted_bits = 0027;
  1978. } else {
  1979. unwanted_bits = 0077;
  1980. }
  1981. if ((st.st_mode & unwanted_bits) != 0) {
  1982. unsigned new_mode;
  1983. if (check & CPD_CHECK_MODE_ONLY) {
  1984. log_warn(LD_FS, "Permissions on directory %s are too permissive.",
  1985. dirname);
  1986. return -1;
  1987. }
  1988. log_warn(LD_FS, "Fixing permissions on directory %s", dirname);
  1989. new_mode = st.st_mode;
  1990. new_mode |= 0700; /* Owner should have rwx */
  1991. if (check & CPD_GROUP_READ) {
  1992. new_mode |= 0050; /* Group should have rx */
  1993. }
  1994. new_mode &= ~unwanted_bits; /* Clear the bits that we didn't want set...*/
  1995. if (chmod(dirname, new_mode)) {
  1996. log_warn(LD_FS, "Could not chmod directory %s: %s", dirname,
  1997. strerror(errno));
  1998. return -1;
  1999. } else {
  2000. return 0;
  2001. }
  2002. }
  2003. #endif
  2004. return 0;
  2005. }
  2006. /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite
  2007. * the previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
  2008. *
  2009. * This function replaces the old file atomically, if possible. This
  2010. * function, and all other functions in util.c that create files, create them
  2011. * with mode 0600.
  2012. */
  2013. int
  2014. write_str_to_file(const char *fname, const char *str, int bin)
  2015. {
  2016. #ifdef _WIN32
  2017. if (!bin && strchr(str, '\r')) {
  2018. log_warn(LD_BUG,
  2019. "We're writing a text string that already contains a CR to %s",
  2020. escaped(fname));
  2021. }
  2022. #endif
  2023. return write_bytes_to_file(fname, str, strlen(str), bin);
  2024. }
  2025. /** Represents a file that we're writing to, with support for atomic commit:
  2026. * we can write into a temporary file, and either remove the file on
  2027. * failure, or replace the original file on success. */
  2028. struct open_file_t {
  2029. char *tempname; /**< Name of the temporary file. */
  2030. char *filename; /**< Name of the original file. */
  2031. unsigned rename_on_close:1; /**< Are we using the temporary file or not? */
  2032. unsigned binary:1; /**< Did we open in binary mode? */
  2033. int fd; /**< fd for the open file. */
  2034. FILE *stdio_file; /**< stdio wrapper for <b>fd</b>. */
  2035. };
  2036. /** Try to start writing to the file in <b>fname</b>, passing the flags
  2037. * <b>open_flags</b> to the open() syscall, creating the file (if needed) with
  2038. * access value <b>mode</b>. If the O_APPEND flag is set, we append to the
  2039. * original file. Otherwise, we open a new temporary file in the same
  2040. * directory, and either replace the original or remove the temporary file
  2041. * when we're done.
  2042. *
  2043. * Return the fd for the newly opened file, and store working data in
  2044. * *<b>data_out</b>. The caller should not close the fd manually:
  2045. * instead, call finish_writing_to_file() or abort_writing_to_file().
  2046. * Returns -1 on failure.
  2047. *
  2048. * NOTE: When not appending, the flags O_CREAT and O_TRUNC are treated
  2049. * as true and the flag O_EXCL is treated as false.
  2050. *
  2051. * NOTE: Ordinarily, O_APPEND means "seek to the end of the file before each
  2052. * write()". We don't do that.
  2053. */
  2054. int
  2055. start_writing_to_file(const char *fname, int open_flags, int mode,
  2056. open_file_t **data_out)
  2057. {
  2058. open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
  2059. const char *open_name;
  2060. int append = 0;
  2061. tor_assert(fname);
  2062. tor_assert(data_out);
  2063. #if (O_BINARY != 0 && O_TEXT != 0)
  2064. tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
  2065. #endif
  2066. new_file->fd = -1;
  2067. new_file->filename = tor_strdup(fname);
  2068. if (open_flags & O_APPEND) {
  2069. open_name = fname;
  2070. new_file->rename_on_close = 0;
  2071. append = 1;
  2072. open_flags &= ~O_APPEND;
  2073. } else {
  2074. tor_asprintf(&new_file->tempname, "%s.tmp", fname);
  2075. open_name = new_file->tempname;
  2076. /* We always replace an existing temporary file if there is one. */
  2077. open_flags |= O_CREAT|O_TRUNC;
  2078. open_flags &= ~O_EXCL;
  2079. new_file->rename_on_close = 1;
  2080. }
  2081. #if O_BINARY != 0
  2082. if (open_flags & O_BINARY)
  2083. new_file->binary = 1;
  2084. #endif
  2085. new_file->fd = tor_open_cloexec(open_name, open_flags, mode);
  2086. if (new_file->fd < 0) {
  2087. log_warn(LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
  2088. open_name, fname, strerror(errno));
  2089. goto err;
  2090. }
  2091. if (append) {
  2092. if (tor_fd_seekend(new_file->fd) < 0) {
  2093. log_warn(LD_FS, "Couldn't seek to end of file \"%s\": %s", open_name,
  2094. strerror(errno));
  2095. goto err;
  2096. }
  2097. }
  2098. *data_out = new_file;
  2099. return new_file->fd;
  2100. err:
  2101. if (new_file->fd >= 0)
  2102. close(new_file->fd);
  2103. *data_out = NULL;
  2104. tor_free(new_file->filename);
  2105. tor_free(new_file->tempname);
  2106. tor_free(new_file);
  2107. return -1;
  2108. }
  2109. /** Given <b>file_data</b> from start_writing_to_file(), return a stdio FILE*
  2110. * that can be used to write to the same file. The caller should not mix
  2111. * stdio calls with non-stdio calls. */
  2112. FILE *
  2113. fdopen_file(open_file_t *file_data)
  2114. {
  2115. tor_assert(file_data);
  2116. if (file_data->stdio_file)
  2117. return file_data->stdio_file;
  2118. tor_assert(file_data->fd >= 0);
  2119. if (!(file_data->stdio_file = fdopen(file_data->fd,
  2120. file_data->binary?"ab":"a"))) {
  2121. log_warn(LD_FS, "Couldn't fdopen \"%s\" [%d]: %s", file_data->filename,
  2122. file_data->fd, strerror(errno));
  2123. }
  2124. return file_data->stdio_file;
  2125. }
  2126. /** Combines start_writing_to_file with fdopen_file(): arguments are as
  2127. * for start_writing_to_file, but */
  2128. FILE *
  2129. start_writing_to_stdio_file(const char *fname, int open_flags, int mode,
  2130. open_file_t **data_out)
  2131. {
  2132. FILE *res;
  2133. if (start_writing_to_file(fname, open_flags, mode, data_out)<0)
  2134. return NULL;
  2135. if (!(res = fdopen_file(*data_out))) {
  2136. abort_writing_to_file(*data_out);
  2137. *data_out = NULL;
  2138. }
  2139. return res;
  2140. }
  2141. /** Helper function: close and free the underlying file and memory in
  2142. * <b>file_data</b>. If we were writing into a temporary file, then delete
  2143. * that file (if abort_write is true) or replaces the target file with
  2144. * the temporary file (if abort_write is false). */
  2145. static int
  2146. finish_writing_to_file_impl(open_file_t *file_data, int abort_write)
  2147. {
  2148. int r = 0;
  2149. tor_assert(file_data && file_data->filename);
  2150. if (file_data->stdio_file) {
  2151. if (fclose(file_data->stdio_file)) {
  2152. log_warn(LD_FS, "Error closing \"%s\": %s", file_data->filename,
  2153. strerror(errno));
  2154. abort_write = r = -1;
  2155. }
  2156. } else if (file_data->fd >= 0 && close(file_data->fd) < 0) {
  2157. log_warn(LD_FS, "Error flushing \"%s\": %s", file_data->filename,
  2158. strerror(errno));
  2159. abort_write = r = -1;
  2160. }
  2161. if (file_data->rename_on_close) {
  2162. tor_assert(file_data->tempname && file_data->filename);
  2163. if (abort_write) {
  2164. int res = unlink(file_data->tempname);
  2165. if (res != 0) {
  2166. /* We couldn't unlink and we'll leave a mess behind */
  2167. log_warn(LD_FS, "Failed to unlink %s: %s",
  2168. file_data->tempname, strerror(errno));
  2169. r = -1;
  2170. }
  2171. } else {
  2172. tor_assert(strcmp(file_data->filename, file_data->tempname));
  2173. if (replace_file(file_data->tempname, file_data->filename)) {
  2174. log_warn(LD_FS, "Error replacing \"%s\": %s", file_data->filename,
  2175. strerror(errno));
  2176. r = -1;
  2177. }
  2178. }
  2179. }
  2180. tor_free(file_data->filename);
  2181. tor_free(file_data->tempname);
  2182. tor_free(file_data);
  2183. return r;
  2184. }
  2185. /** Finish writing to <b>file_data</b>: close the file handle, free memory as
  2186. * needed, and if using a temporary file, replace the original file with
  2187. * the temporary file. */
  2188. int
  2189. finish_writing_to_file(open_file_t *file_data)
  2190. {
  2191. return finish_writing_to_file_impl(file_data, 0);
  2192. }
  2193. /** Finish writing to <b>file_data</b>: close the file handle, free memory as
  2194. * needed, and if using a temporary file, delete it. */
  2195. int
  2196. abort_writing_to_file(open_file_t *file_data)
  2197. {
  2198. return finish_writing_to_file_impl(file_data, 1);
  2199. }
  2200. /** Helper: given a set of flags as passed to open(2), open the file
  2201. * <b>fname</b> and write all the sized_chunk_t structs in <b>chunks</b> to
  2202. * the file. Do so as atomically as possible e.g. by opening temp files and
  2203. * renaming. */
  2204. static int
  2205. write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
  2206. int open_flags)
  2207. {
  2208. open_file_t *file = NULL;
  2209. int fd;
  2210. ssize_t result;
  2211. fd = start_writing_to_file(fname, open_flags, 0600, &file);
  2212. if (fd<0)
  2213. return -1;
  2214. SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
  2215. {
  2216. result = write_all(fd, chunk->bytes, chunk->len, 0);
  2217. if (result < 0) {
  2218. log_warn(LD_FS, "Error writing to \"%s\": %s", fname,
  2219. strerror(errno));
  2220. goto err;
  2221. }
  2222. tor_assert((size_t)result == chunk->len);
  2223. });
  2224. return finish_writing_to_file(file);
  2225. err:
  2226. abort_writing_to_file(file);
  2227. return -1;
  2228. }
  2229. /** Given a smartlist of sized_chunk_t, write them to a file
  2230. * <b>fname</b>, overwriting or creating the file as necessary.
  2231. * If <b>no_tempfile</b> is 0 then the file will be written
  2232. * atomically. */
  2233. int
  2234. write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin,
  2235. int no_tempfile)
  2236. {
  2237. int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
  2238. if (no_tempfile) {
  2239. /* O_APPEND stops write_chunks_to_file from using tempfiles */
  2240. flags |= O_APPEND;
  2241. }
  2242. return write_chunks_to_file_impl(fname, chunks, flags);
  2243. }
  2244. /** Write <b>len</b> bytes, starting at <b>str</b>, to <b>fname</b>
  2245. using the open() flags passed in <b>flags</b>. */
  2246. static int
  2247. write_bytes_to_file_impl(const char *fname, const char *str, size_t len,
  2248. int flags)
  2249. {
  2250. int r;
  2251. sized_chunk_t c = { str, len };
  2252. smartlist_t *chunks = smartlist_new();
  2253. smartlist_add(chunks, &c);
  2254. r = write_chunks_to_file_impl(fname, chunks, flags);
  2255. smartlist_free(chunks);
  2256. return r;
  2257. }
  2258. /** As write_str_to_file, but does not assume a NUL-terminated
  2259. * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
  2260. MOCK_IMPL(int,
  2261. write_bytes_to_file,(const char *fname, const char *str, size_t len,
  2262. int bin))
  2263. {
  2264. return write_bytes_to_file_impl(fname, str, len,
  2265. OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT));
  2266. }
  2267. /** As write_bytes_to_file, but if the file already exists, append the bytes
  2268. * to the end of the file instead of overwriting it. */
  2269. int
  2270. append_bytes_to_file(const char *fname, const char *str, size_t len,
  2271. int bin)
  2272. {
  2273. return write_bytes_to_file_impl(fname, str, len,
  2274. OPEN_FLAGS_APPEND|(bin?O_BINARY:O_TEXT));
  2275. }
  2276. /** Like write_str_to_file(), but also return -1 if there was a file
  2277. already residing in <b>fname</b>. */
  2278. int
  2279. write_bytes_to_new_file(const char *fname, const char *str, size_t len,
  2280. int bin)
  2281. {
  2282. return write_bytes_to_file_impl(fname, str, len,
  2283. OPEN_FLAGS_DONT_REPLACE|
  2284. (bin?O_BINARY:O_TEXT));
  2285. }
  2286. /**
  2287. * Read the contents of the open file <b>fd</b> presuming it is a FIFO
  2288. * (or similar) file descriptor for which the size of the file isn't
  2289. * known ahead of time. Return NULL on failure, and a NUL-terminated
  2290. * string on success. On success, set <b>sz_out</b> to the number of
  2291. * bytes read.
  2292. */
  2293. char *
  2294. read_file_to_str_until_eof(int fd, size_t max_bytes_to_read, size_t *sz_out)
  2295. {
  2296. ssize_t r;
  2297. size_t pos = 0;
  2298. char *string = NULL;
  2299. size_t string_max = 0;
  2300. if (max_bytes_to_read+1 >= SIZE_T_CEILING)
  2301. return NULL;
  2302. do {
  2303. /* XXXX This "add 1K" approach is a little goofy; if we care about
  2304. * performance here, we should be doubling. But in practice we shouldn't
  2305. * be using this function on big files anyway. */
  2306. string_max = pos + 1024;
  2307. if (string_max > max_bytes_to_read)
  2308. string_max = max_bytes_to_read + 1;
  2309. string = tor_realloc(string, string_max);
  2310. r = read(fd, string + pos, string_max - pos - 1);
  2311. if (r < 0) {
  2312. tor_free(string);
  2313. return NULL;
  2314. }
  2315. pos += r;
  2316. } while (r > 0 && pos < max_bytes_to_read);
  2317. tor_assert(pos < string_max);
  2318. *sz_out = pos;
  2319. string[pos] = '\0';
  2320. return string;
  2321. }
  2322. /** Read the contents of <b>filename</b> into a newly allocated
  2323. * string; return the string on success or NULL on failure.
  2324. *
  2325. * If <b>stat_out</b> is provided, store the result of stat()ing the
  2326. * file into <b>stat_out</b>.
  2327. *
  2328. * If <b>flags</b> &amp; RFTS_BIN, open the file in binary mode.
  2329. * If <b>flags</b> &amp; RFTS_IGNORE_MISSING, don't warn if the file
  2330. * doesn't exist.
  2331. */
  2332. /*
  2333. * This function <em>may</em> return an erroneous result if the file
  2334. * is modified while it is running, but must not crash or overflow.
  2335. * Right now, the error case occurs when the file length grows between
  2336. * the call to stat and the call to read_all: the resulting string will
  2337. * be truncated.
  2338. */
  2339. char *
  2340. read_file_to_str(const char *filename, int flags, struct stat *stat_out)
  2341. {
  2342. int fd; /* router file */
  2343. struct stat statbuf;
  2344. char *string;
  2345. ssize_t r;
  2346. int bin = flags & RFTS_BIN;
  2347. tor_assert(filename);
  2348. fd = tor_open_cloexec(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
  2349. if (fd<0) {
  2350. int severity = LOG_WARN;
  2351. int save_errno = errno;
  2352. if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
  2353. severity = LOG_INFO;
  2354. log_fn(severity, LD_FS,"Could not open \"%s\": %s",filename,
  2355. strerror(errno));
  2356. errno = save_errno;
  2357. return NULL;
  2358. }
  2359. if (fstat(fd, &statbuf)<0) {
  2360. int save_errno = errno;
  2361. close(fd);
  2362. log_warn(LD_FS,"Could not fstat \"%s\".",filename);
  2363. errno = save_errno;
  2364. return NULL;
  2365. }
  2366. #ifndef _WIN32
  2367. /** When we detect that we're reading from a FIFO, don't read more than
  2368. * this many bytes. It's insane overkill for most uses. */
  2369. #define FIFO_READ_MAX (1024*1024)
  2370. if (S_ISFIFO(statbuf.st_mode)) {
  2371. size_t sz = 0;
  2372. string = read_file_to_str_until_eof(fd, FIFO_READ_MAX, &sz);
  2373. if (string && stat_out) {
  2374. statbuf.st_size = sz;
  2375. memcpy(stat_out, &statbuf, sizeof(struct stat));
  2376. }
  2377. close(fd);
  2378. return string;
  2379. }
  2380. #endif
  2381. if ((uint64_t)(statbuf.st_size)+1 >= SIZE_T_CEILING) {
  2382. close(fd);
  2383. return NULL;
  2384. }
  2385. string = tor_malloc((size_t)(statbuf.st_size+1));
  2386. r = read_all(fd,string,(size_t)statbuf.st_size,0);
  2387. if (r<0) {
  2388. int save_errno = errno;
  2389. log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
  2390. strerror(errno));
  2391. tor_free(string);
  2392. close(fd);
  2393. errno = save_errno;
  2394. return NULL;
  2395. }
  2396. string[r] = '\0'; /* NUL-terminate the result. */
  2397. #if defined(_WIN32) || defined(__CYGWIN__)
  2398. if (!bin && strchr(string, '\r')) {
  2399. log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
  2400. "when reading %s. Coping.",
  2401. filename);
  2402. tor_strstrip(string, "\r");
  2403. r = strlen(string);
  2404. }
  2405. if (!bin) {
  2406. statbuf.st_size = (size_t) r;
  2407. } else
  2408. #endif
  2409. if (r != statbuf.st_size) {
  2410. /* Unless we're using text mode on win32, we'd better have an exact
  2411. * match for size. */
  2412. int save_errno = errno;
  2413. log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
  2414. (int)r, (long)statbuf.st_size,filename);
  2415. tor_free(string);
  2416. close(fd);
  2417. errno = save_errno;
  2418. return NULL;
  2419. }
  2420. close(fd);
  2421. if (stat_out) {
  2422. memcpy(stat_out, &statbuf, sizeof(struct stat));
  2423. }
  2424. return string;
  2425. }
  2426. #define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
  2427. /** Given a c-style double-quoted escaped string in <b>s</b>, extract and
  2428. * decode its contents into a newly allocated string. On success, assign this
  2429. * string to *<b>result</b>, assign its length to <b>size_out</b> (if
  2430. * provided), and return a pointer to the position in <b>s</b> immediately
  2431. * after the string. On failure, return NULL.
  2432. */
  2433. static const char *
  2434. unescape_string(const char *s, char **result, size_t *size_out)
  2435. {
  2436. const char *cp;
  2437. char *out;
  2438. if (s[0] != '\"')
  2439. return NULL;
  2440. cp = s+1;
  2441. while (1) {
  2442. switch (*cp) {
  2443. case '\0':
  2444. case '\n':
  2445. return NULL;
  2446. case '\"':
  2447. goto end_of_loop;
  2448. case '\\':
  2449. if (cp[1] == 'x' || cp[1] == 'X') {
  2450. if (!(TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])))
  2451. return NULL;
  2452. cp += 4;
  2453. } else if (TOR_ISODIGIT(cp[1])) {
  2454. cp += 2;
  2455. if (TOR_ISODIGIT(*cp)) ++cp;
  2456. if (TOR_ISODIGIT(*cp)) ++cp;
  2457. } else if (cp[1] == 'n' || cp[1] == 'r' || cp[1] == 't' || cp[1] == '"'
  2458. || cp[1] == '\\' || cp[1] == '\'') {
  2459. cp += 2;
  2460. } else {
  2461. return NULL;
  2462. }
  2463. break;
  2464. default:
  2465. ++cp;
  2466. break;
  2467. }
  2468. }
  2469. end_of_loop:
  2470. out = *result = tor_malloc(cp-s + 1);
  2471. cp = s+1;
  2472. while (1) {
  2473. switch (*cp)
  2474. {
  2475. case '\"':
  2476. *out = '\0';
  2477. if (size_out) *size_out = out - *result;
  2478. return cp+1;
  2479. case '\0':
  2480. tor_fragile_assert();
  2481. tor_free(*result);
  2482. return NULL;
  2483. case '\\':
  2484. switch (cp[1])
  2485. {
  2486. case 'n': *out++ = '\n'; cp += 2; break;
  2487. case 'r': *out++ = '\r'; cp += 2; break;
  2488. case 't': *out++ = '\t'; cp += 2; break;
  2489. case 'x': case 'X':
  2490. {
  2491. int x1, x2;
  2492. x1 = hex_decode_digit(cp[2]);
  2493. x2 = hex_decode_digit(cp[3]);
  2494. if (x1 == -1 || x2 == -1) {
  2495. tor_free(*result);
  2496. return NULL;
  2497. }
  2498. *out++ = ((x1<<4) + x2);
  2499. cp += 4;
  2500. }
  2501. break;
  2502. case '0': case '1': case '2': case '3': case '4': case '5':
  2503. case '6': case '7':
  2504. {
  2505. int n = cp[1]-'0';
  2506. cp += 2;
  2507. if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
  2508. if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
  2509. if (n > 255) { tor_free(*result); return NULL; }
  2510. *out++ = (char)n;
  2511. }
  2512. break;
  2513. case '\'':
  2514. case '\"':
  2515. case '\\':
  2516. case '\?':
  2517. *out++ = cp[1];
  2518. cp += 2;
  2519. break;
  2520. default:
  2521. tor_free(*result); return NULL;
  2522. }
  2523. break;
  2524. default:
  2525. *out++ = *cp++;
  2526. }
  2527. }
  2528. }
  2529. /** Given a string containing part of a configuration file or similar format,
  2530. * advance past comments and whitespace and try to parse a single line. If we
  2531. * parse a line successfully, set *<b>key_out</b> to a new string holding the
  2532. * key portion and *<b>value_out</b> to a new string holding the value portion
  2533. * of the line, and return a pointer to the start of the next line. If we run
  2534. * out of data, return a pointer to the end of the string. If we encounter an
  2535. * error, return NULL and set *<b>err_out</b> (if provided) to an error
  2536. * message.
  2537. */
  2538. const char *
  2539. parse_config_line_from_str_verbose(const char *line, char **key_out,
  2540. char **value_out,
  2541. const char **err_out)
  2542. {
  2543. /* I believe the file format here is supposed to be:
  2544. FILE = (EMPTYLINE | LINE)* (EMPTYLASTLINE | LASTLINE)?
  2545. EMPTYLASTLINE = SPACE* | COMMENT
  2546. EMPTYLINE = EMPTYLASTLINE NL
  2547. SPACE = ' ' | '\r' | '\t'
  2548. COMMENT = '#' NOT-NL*
  2549. NOT-NL = Any character except '\n'
  2550. NL = '\n'
  2551. LASTLINE = SPACE* KEY SPACE* VALUES
  2552. LINE = LASTLINE NL
  2553. KEY = KEYCHAR+
  2554. KEYCHAR = Any character except ' ', '\r', '\n', '\t', '#', "\"
  2555. VALUES = QUOTEDVALUE | NORMALVALUE
  2556. QUOTEDVALUE = QUOTE QVCHAR* QUOTE EOLSPACE?
  2557. QUOTE = '"'
  2558. QVCHAR = KEYCHAR | ESC ('n' | 't' | 'r' | '"' | ESC |'\'' | OCTAL | HEX)
  2559. ESC = "\\"
  2560. OCTAL = ODIGIT (ODIGIT ODIGIT?)?
  2561. HEX = ('x' | 'X') HEXDIGIT HEXDIGIT
  2562. ODIGIT = '0' .. '7'
  2563. HEXDIGIT = '0'..'9' | 'a' .. 'f' | 'A' .. 'F'
  2564. EOLSPACE = SPACE* COMMENT?
  2565. NORMALVALUE = (VALCHAR | ESC ESC_IGNORE | CONTINUATION)* EOLSPACE?
  2566. VALCHAR = Any character except ESC, '#', and '\n'
  2567. ESC_IGNORE = Any character except '#' or '\n'
  2568. CONTINUATION = ESC NL ( COMMENT NL )*
  2569. */
  2570. const char *key, *val, *cp;
  2571. int continuation = 0;
  2572. tor_assert(key_out);
  2573. tor_assert(value_out);
  2574. *key_out = *value_out = NULL;
  2575. key = val = NULL;
  2576. /* Skip until the first keyword. */
  2577. while (1) {
  2578. while (TOR_ISSPACE(*line))
  2579. ++line;
  2580. if (*line == '#') {
  2581. while (*line && *line != '\n')
  2582. ++line;
  2583. } else {
  2584. break;
  2585. }
  2586. }
  2587. if (!*line) { /* End of string? */
  2588. *key_out = *value_out = NULL;
  2589. return line;
  2590. }
  2591. /* Skip until the next space or \ followed by newline. */
  2592. key = line;
  2593. while (*line && !TOR_ISSPACE(*line) && *line != '#' &&
  2594. ! (line[0] == '\\' && line[1] == '\n'))
  2595. ++line;
  2596. *key_out = tor_strndup(key, line-key);
  2597. /* Skip until the value. */
  2598. while (*line == ' ' || *line == '\t')
  2599. ++line;
  2600. val = line;
  2601. /* Find the end of the line. */
  2602. if (*line == '\"') { // XXX No continuation handling is done here
  2603. if (!(line = unescape_string(line, value_out, NULL))) {
  2604. if (err_out)
  2605. *err_out = "Invalid escape sequence in quoted string";
  2606. return NULL;
  2607. }
  2608. while (*line == ' ' || *line == '\t')
  2609. ++line;
  2610. if (*line && *line != '#' && *line != '\n') {
  2611. if (err_out)
  2612. *err_out = "Excess data after quoted string";
  2613. return NULL;
  2614. }
  2615. } else {
  2616. /* Look for the end of the line. */
  2617. while (*line && *line != '\n' && (*line != '#' || continuation)) {
  2618. if (*line == '\\' && line[1] == '\n') {
  2619. continuation = 1;
  2620. line += 2;
  2621. } else if (*line == '#') {
  2622. do {
  2623. ++line;
  2624. } while (*line && *line != '\n');
  2625. if (*line == '\n')
  2626. ++line;
  2627. } else {
  2628. ++line;
  2629. }
  2630. }
  2631. if (*line == '\n') {
  2632. cp = line++;
  2633. } else {
  2634. cp = line;
  2635. }
  2636. /* Now back cp up to be the last nonspace character */
  2637. while (cp>val && TOR_ISSPACE(*(cp-1)))
  2638. --cp;
  2639. tor_assert(cp >= val);
  2640. /* Now copy out and decode the value. */
  2641. *value_out = tor_strndup(val, cp-val);
  2642. if (continuation) {
  2643. char *v_out, *v_in;
  2644. v_out = v_in = *value_out;
  2645. while (*v_in) {
  2646. if (*v_in == '#') {
  2647. do {
  2648. ++v_in;
  2649. } while (*v_in && *v_in != '\n');
  2650. if (*v_in == '\n')
  2651. ++v_in;
  2652. } else if (v_in[0] == '\\' && v_in[1] == '\n') {
  2653. v_in += 2;
  2654. } else {
  2655. *v_out++ = *v_in++;
  2656. }
  2657. }
  2658. *v_out = '\0';
  2659. }
  2660. }
  2661. if (*line == '#') {
  2662. do {
  2663. ++line;
  2664. } while (*line && *line != '\n');
  2665. }
  2666. while (TOR_ISSPACE(*line)) ++line;
  2667. return line;
  2668. }
  2669. /** Expand any homedir prefix on <b>filename</b>; return a newly allocated
  2670. * string. */
  2671. char *
  2672. expand_filename(const char *filename)
  2673. {
  2674. tor_assert(filename);
  2675. #ifdef _WIN32
  2676. return tor_strdup(filename);
  2677. #else
  2678. if (*filename == '~') {
  2679. char *home, *result=NULL;
  2680. const char *rest;
  2681. if (filename[1] == '/' || filename[1] == '\0') {
  2682. home = getenv("HOME");
  2683. if (!home) {
  2684. log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
  2685. "expanding \"%s\"; defaulting to \"\".", filename);
  2686. home = tor_strdup("");
  2687. } else {
  2688. home = tor_strdup(home);
  2689. }
  2690. rest = strlen(filename)>=2?(filename+2):"";
  2691. } else {
  2692. #ifdef HAVE_PWD_H
  2693. char *username, *slash;
  2694. slash = strchr(filename, '/');
  2695. if (slash)
  2696. username = tor_strndup(filename+1,slash-filename-1);
  2697. else
  2698. username = tor_strdup(filename+1);
  2699. if (!(home = get_user_homedir(username))) {
  2700. log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
  2701. tor_free(username);
  2702. return NULL;
  2703. }
  2704. tor_free(username);
  2705. rest = slash ? (slash+1) : "";
  2706. #else
  2707. log_warn(LD_CONFIG, "Couldn't expand homedir on system without pwd.h");
  2708. return tor_strdup(filename);
  2709. #endif
  2710. }
  2711. tor_assert(home);
  2712. /* Remove trailing slash. */
  2713. if (strlen(home)>1 && !strcmpend(home,PATH_SEPARATOR)) {
  2714. home[strlen(home)-1] = '\0';
  2715. }
  2716. tor_asprintf(&result,"%s"PATH_SEPARATOR"%s",home,rest);
  2717. tor_free(home);
  2718. return result;
  2719. } else {
  2720. return tor_strdup(filename);
  2721. }
  2722. #endif
  2723. }
  2724. #define MAX_SCANF_WIDTH 9999
  2725. /** Helper: given an ASCII-encoded decimal digit, return its numeric value.
  2726. * NOTE: requires that its input be in-bounds. */
  2727. static int
  2728. digit_to_num(char d)
  2729. {
  2730. int num = ((int)d) - (int)'0';
  2731. tor_assert(num <= 9 && num >= 0);
  2732. return num;
  2733. }
  2734. /** Helper: Read an unsigned int from *<b>bufp</b> of up to <b>width</b>
  2735. * characters. (Handle arbitrary width if <b>width</b> is less than 0.) On
  2736. * success, store the result in <b>out</b>, advance bufp to the next
  2737. * character, and return 0. On failure, return -1. */
  2738. static int
  2739. scan_unsigned(const char **bufp, unsigned long *out, int width, int base)
  2740. {
  2741. unsigned long result = 0;
  2742. int scanned_so_far = 0;
  2743. const int hex = base==16;
  2744. tor_assert(base == 10 || base == 16);
  2745. if (!bufp || !*bufp || !out)
  2746. return -1;
  2747. if (width<0)
  2748. width=MAX_SCANF_WIDTH;
  2749. while (**bufp && (hex?TOR_ISXDIGIT(**bufp):TOR_ISDIGIT(**bufp))
  2750. && scanned_so_far < width) {
  2751. int digit = hex?hex_decode_digit(*(*bufp)++):digit_to_num(*(*bufp)++);
  2752. // Check for overflow beforehand, without actually causing any overflow
  2753. // This preserves functionality on compilers that don't wrap overflow
  2754. // (i.e. that trap or optimise away overflow)
  2755. // result * base + digit > ULONG_MAX
  2756. // result * base > ULONG_MAX - digit
  2757. if (result > (ULONG_MAX - digit)/base)
  2758. return -1; /* Processing this digit would overflow */
  2759. result = result * base + digit;
  2760. ++scanned_so_far;
  2761. }
  2762. if (!scanned_so_far) /* No actual digits scanned */
  2763. return -1;
  2764. *out = result;
  2765. return 0;
  2766. }
  2767. /** Helper: Read an signed int from *<b>bufp</b> of up to <b>width</b>
  2768. * characters. (Handle arbitrary width if <b>width</b> is less than 0.) On
  2769. * success, store the result in <b>out</b>, advance bufp to the next
  2770. * character, and return 0. On failure, return -1. */
  2771. static int
  2772. scan_signed(const char **bufp, long *out, int width)
  2773. {
  2774. int neg = 0;
  2775. unsigned long result = 0;
  2776. if (!bufp || !*bufp || !out)
  2777. return -1;
  2778. if (width<0)
  2779. width=MAX_SCANF_WIDTH;
  2780. if (**bufp == '-') {
  2781. neg = 1;
  2782. ++*bufp;
  2783. --width;
  2784. }
  2785. if (scan_unsigned(bufp, &result, width, 10) < 0)
  2786. return -1;
  2787. if (neg && result > 0) {
  2788. if (result > ((unsigned long)LONG_MAX) + 1)
  2789. return -1; /* Underflow */
  2790. // Avoid overflow on the cast to signed long when result is LONG_MIN
  2791. // by subtracting 1 from the unsigned long positive value,
  2792. // then, after it has been cast to signed and negated,
  2793. // subtracting the original 1 (the double-subtraction is intentional).
  2794. // Otherwise, the cast to signed could cause a temporary long
  2795. // to equal LONG_MAX + 1, which is undefined.
  2796. // We avoid underflow on the subtraction by treating -0 as positive.
  2797. *out = (-(long)(result - 1)) - 1;
  2798. } else {
  2799. if (result > LONG_MAX)
  2800. return -1; /* Overflow */
  2801. *out = (long)result;
  2802. }
  2803. return 0;
  2804. }
  2805. /** Helper: Read a decimal-formatted double from *<b>bufp</b> of up to
  2806. * <b>width</b> characters. (Handle arbitrary width if <b>width</b> is less
  2807. * than 0.) On success, store the result in <b>out</b>, advance bufp to the
  2808. * next character, and return 0. On failure, return -1. */
  2809. static int
  2810. scan_double(const char **bufp, double *out, int width)
  2811. {
  2812. int neg = 0;
  2813. double result = 0;
  2814. int scanned_so_far = 0;
  2815. if (!bufp || !*bufp || !out)
  2816. return -1;
  2817. if (width<0)
  2818. width=MAX_SCANF_WIDTH;
  2819. if (**bufp == '-') {
  2820. neg = 1;
  2821. ++*bufp;
  2822. }
  2823. while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
  2824. const int digit = digit_to_num(*(*bufp)++);
  2825. result = result * 10 + digit;
  2826. ++scanned_so_far;
  2827. }
  2828. if (**bufp == '.') {
  2829. double fracval = 0, denominator = 1;
  2830. ++*bufp;
  2831. ++scanned_so_far;
  2832. while (**bufp && TOR_ISDIGIT(**bufp) && scanned_so_far < width) {
  2833. const int digit = digit_to_num(*(*bufp)++);
  2834. fracval = fracval * 10 + digit;
  2835. denominator *= 10;
  2836. ++scanned_so_far;
  2837. }
  2838. result += fracval / denominator;
  2839. }
  2840. if (!scanned_so_far) /* No actual digits scanned */
  2841. return -1;
  2842. *out = neg ? -result : result;
  2843. return 0;
  2844. }
  2845. /** Helper: copy up to <b>width</b> non-space characters from <b>bufp</b> to
  2846. * <b>out</b>. Make sure <b>out</b> is nul-terminated. Advance <b>bufp</b>
  2847. * to the next non-space character or the EOS. */
  2848. static int
  2849. scan_string(const char **bufp, char *out, int width)
  2850. {
  2851. int scanned_so_far = 0;
  2852. if (!bufp || !out || width < 0)
  2853. return -1;
  2854. while (**bufp && ! TOR_ISSPACE(**bufp) && scanned_so_far < width) {
  2855. *out++ = *(*bufp)++;
  2856. ++scanned_so_far;
  2857. }
  2858. *out = '\0';
  2859. return 0;
  2860. }
  2861. /** Locale-independent, minimal, no-surprises scanf variant, accepting only a
  2862. * restricted pattern format. For more info on what it supports, see
  2863. * tor_sscanf() documentation. */
  2864. int
  2865. tor_vsscanf(const char *buf, const char *pattern, va_list ap)
  2866. {
  2867. int n_matched = 0;
  2868. while (*pattern) {
  2869. if (*pattern != '%') {
  2870. if (*buf == *pattern) {
  2871. ++buf;
  2872. ++pattern;
  2873. continue;
  2874. } else {
  2875. return n_matched;
  2876. }
  2877. } else {
  2878. int width = -1;
  2879. int longmod = 0;
  2880. ++pattern;
  2881. if (TOR_ISDIGIT(*pattern)) {
  2882. width = digit_to_num(*pattern++);
  2883. while (TOR_ISDIGIT(*pattern)) {
  2884. width *= 10;
  2885. width += digit_to_num(*pattern++);
  2886. if (width > MAX_SCANF_WIDTH)
  2887. return -1;
  2888. }
  2889. if (!width) /* No zero-width things. */
  2890. return -1;
  2891. }
  2892. if (*pattern == 'l') {
  2893. longmod = 1;
  2894. ++pattern;
  2895. }
  2896. if (*pattern == 'u' || *pattern == 'x') {
  2897. unsigned long u;
  2898. const int base = (*pattern == 'u') ? 10 : 16;
  2899. if (!*buf)
  2900. return n_matched;
  2901. if (scan_unsigned(&buf, &u, width, base)<0)
  2902. return n_matched;
  2903. if (longmod) {
  2904. unsigned long *out = va_arg(ap, unsigned long *);
  2905. *out = u;
  2906. } else {
  2907. unsigned *out = va_arg(ap, unsigned *);
  2908. if (u > UINT_MAX)
  2909. return n_matched;
  2910. *out = (unsigned) u;
  2911. }
  2912. ++pattern;
  2913. ++n_matched;
  2914. } else if (*pattern == 'f') {
  2915. double *d = va_arg(ap, double *);
  2916. if (!longmod)
  2917. return -1; /* float not supported */
  2918. if (!*buf)
  2919. return n_matched;
  2920. if (scan_double(&buf, d, width)<0)
  2921. return n_matched;
  2922. ++pattern;
  2923. ++n_matched;
  2924. } else if (*pattern == 'd') {
  2925. long lng=0;
  2926. if (scan_signed(&buf, &lng, width)<0)
  2927. return n_matched;
  2928. if (longmod) {
  2929. long *out = va_arg(ap, long *);
  2930. *out = lng;
  2931. } else {
  2932. int *out = va_arg(ap, int *);
  2933. if (lng < INT_MIN || lng > INT_MAX)
  2934. return n_matched;
  2935. *out = (int)lng;
  2936. }
  2937. ++pattern;
  2938. ++n_matched;
  2939. } else if (*pattern == 's') {
  2940. char *s = va_arg(ap, char *);
  2941. if (longmod)
  2942. return -1;
  2943. if (width < 0)
  2944. return -1;
  2945. if (scan_string(&buf, s, width)<0)
  2946. return n_matched;
  2947. ++pattern;
  2948. ++n_matched;
  2949. } else if (*pattern == 'c') {
  2950. char *ch = va_arg(ap, char *);
  2951. if (longmod)
  2952. return -1;
  2953. if (width != -1)
  2954. return -1;
  2955. if (!*buf)
  2956. return n_matched;
  2957. *ch = *buf++;
  2958. ++pattern;
  2959. ++n_matched;
  2960. } else if (*pattern == '%') {
  2961. if (*buf != '%')
  2962. return n_matched;
  2963. if (longmod)
  2964. return -1;
  2965. ++buf;
  2966. ++pattern;
  2967. } else {
  2968. return -1; /* Unrecognized pattern component. */
  2969. }
  2970. }
  2971. }
  2972. return n_matched;
  2973. }
  2974. /** Minimal sscanf replacement: parse <b>buf</b> according to <b>pattern</b>
  2975. * and store the results in the corresponding argument fields. Differs from
  2976. * sscanf in that:
  2977. * <ul><li>It only handles %u, %lu, %x, %lx, %[NUM]s, %d, %ld, %lf, and %c.
  2978. * <li>It only handles decimal inputs for %lf. (12.3, not 1.23e1)
  2979. * <li>It does not handle arbitrarily long widths.
  2980. * <li>Numbers do not consume any space characters.
  2981. * <li>It is locale-independent.
  2982. * <li>%u and %x do not consume any space.
  2983. * <li>It returns -1 on malformed patterns.</ul>
  2984. *
  2985. * (As with other locale-independent functions, we need this to parse data that
  2986. * is in ASCII without worrying that the C library's locale-handling will make
  2987. * miscellaneous characters look like numbers, spaces, and so on.)
  2988. */
  2989. int
  2990. tor_sscanf(const char *buf, const char *pattern, ...)
  2991. {
  2992. int r;
  2993. va_list ap;
  2994. va_start(ap, pattern);
  2995. r = tor_vsscanf(buf, pattern, ap);
  2996. va_end(ap);
  2997. return r;
  2998. }
  2999. /** Append the string produced by tor_asprintf(<b>pattern</b>, <b>...</b>)
  3000. * to <b>sl</b>. */
  3001. void
  3002. smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
  3003. {
  3004. va_list ap;
  3005. va_start(ap, pattern);
  3006. smartlist_add_vasprintf(sl, pattern, ap);
  3007. va_end(ap);
  3008. }
  3009. /** va_list-based backend of smartlist_add_asprintf. */
  3010. void
  3011. smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
  3012. va_list args)
  3013. {
  3014. char *str = NULL;
  3015. tor_vasprintf(&str, pattern, args);
  3016. tor_assert(str != NULL);
  3017. smartlist_add(sl, str);
  3018. }
  3019. /** Return a new list containing the filenames in the directory <b>dirname</b>.
  3020. * Return NULL on error or if <b>dirname</b> is not a directory.
  3021. */
  3022. smartlist_t *
  3023. tor_listdir(const char *dirname)
  3024. {
  3025. smartlist_t *result;
  3026. #ifdef _WIN32
  3027. char *pattern=NULL;
  3028. TCHAR tpattern[MAX_PATH] = {0};
  3029. char name[MAX_PATH*2+1] = {0};
  3030. HANDLE handle;
  3031. WIN32_FIND_DATA findData;
  3032. tor_asprintf(&pattern, "%s\\*", dirname);
  3033. #ifdef UNICODE
  3034. mbstowcs(tpattern,pattern,MAX_PATH);
  3035. #else
  3036. strlcpy(tpattern, pattern, MAX_PATH);
  3037. #endif
  3038. if (INVALID_HANDLE_VALUE == (handle = FindFirstFile(tpattern, &findData))) {
  3039. tor_free(pattern);
  3040. return NULL;
  3041. }
  3042. result = smartlist_new();
  3043. while (1) {
  3044. #ifdef UNICODE
  3045. wcstombs(name,findData.cFileName,MAX_PATH);
  3046. name[sizeof(name)-1] = '\0';
  3047. #else
  3048. strlcpy(name,findData.cFileName,sizeof(name));
  3049. #endif
  3050. if (strcmp(name, ".") &&
  3051. strcmp(name, "..")) {
  3052. smartlist_add(result, tor_strdup(name));
  3053. }
  3054. if (!FindNextFile(handle, &findData)) {
  3055. DWORD err;
  3056. if ((err = GetLastError()) != ERROR_NO_MORE_FILES) {
  3057. char *errstr = format_win32_error(err);
  3058. log_warn(LD_FS, "Error reading directory '%s': %s", dirname, errstr);
  3059. tor_free(errstr);
  3060. }
  3061. break;
  3062. }
  3063. }
  3064. FindClose(handle);
  3065. tor_free(pattern);
  3066. #else
  3067. const char *prot_dname = sandbox_intern_string(dirname);
  3068. DIR *d;
  3069. struct dirent *de;
  3070. if (!(d = opendir(prot_dname)))
  3071. return NULL;
  3072. result = smartlist_new();
  3073. while ((de = readdir(d))) {
  3074. if (!strcmp(de->d_name, ".") ||
  3075. !strcmp(de->d_name, ".."))
  3076. continue;
  3077. smartlist_add(result, tor_strdup(de->d_name));
  3078. }
  3079. closedir(d);
  3080. #endif
  3081. return result;
  3082. }
  3083. /** Return true iff <b>filename</b> is a relative path. */
  3084. int
  3085. path_is_relative(const char *filename)
  3086. {
  3087. if (filename && filename[0] == '/')
  3088. return 0;
  3089. #ifdef _WIN32
  3090. else if (filename && filename[0] == '\\')
  3091. return 0;
  3092. else if (filename && strlen(filename)>3 && TOR_ISALPHA(filename[0]) &&
  3093. filename[1] == ':' && filename[2] == '\\')
  3094. return 0;
  3095. #endif
  3096. else
  3097. return 1;
  3098. }
  3099. /* =====
  3100. * Process helpers
  3101. * ===== */
  3102. #ifndef _WIN32
  3103. /* Based on code contributed by christian grothoff */
  3104. /** True iff we've called start_daemon(). */
  3105. static int start_daemon_called = 0;
  3106. /** True iff we've called finish_daemon(). */
  3107. static int finish_daemon_called = 0;
  3108. /** Socketpair used to communicate between parent and child process while
  3109. * daemonizing. */
  3110. static int daemon_filedes[2];
  3111. /** Start putting the process into daemon mode: fork and drop all resources
  3112. * except standard fds. The parent process never returns, but stays around
  3113. * until finish_daemon is called. (Note: it's safe to call this more
  3114. * than once: calls after the first are ignored.)
  3115. */
  3116. void
  3117. start_daemon(void)
  3118. {
  3119. pid_t pid;
  3120. if (start_daemon_called)
  3121. return;
  3122. start_daemon_called = 1;
  3123. if (pipe(daemon_filedes)) {
  3124. log_err(LD_GENERAL,"pipe failed; exiting. Error was %s", strerror(errno));
  3125. exit(1);
  3126. }
  3127. pid = fork();
  3128. if (pid < 0) {
  3129. log_err(LD_GENERAL,"fork failed. Exiting.");
  3130. exit(1);
  3131. }
  3132. if (pid) { /* Parent */
  3133. int ok;
  3134. char c;
  3135. close(daemon_filedes[1]); /* we only read */
  3136. ok = -1;
  3137. while (0 < read(daemon_filedes[0], &c, sizeof(char))) {
  3138. if (c == '.')
  3139. ok = 1;
  3140. }
  3141. fflush(stdout);
  3142. if (ok == 1)
  3143. exit(0);
  3144. else
  3145. exit(1); /* child reported error */
  3146. } else { /* Child */
  3147. close(daemon_filedes[0]); /* we only write */
  3148. pid = setsid(); /* Detach from controlling terminal */
  3149. /*
  3150. * Fork one more time, so the parent (the session group leader) can exit.
  3151. * This means that we, as a non-session group leader, can never regain a
  3152. * controlling terminal. This part is recommended by Stevens's
  3153. * _Advanced Programming in the Unix Environment_.
  3154. */
  3155. if (fork() != 0) {
  3156. exit(0);
  3157. }
  3158. set_main_thread(); /* We are now the main thread. */
  3159. return;
  3160. }
  3161. }
  3162. /** Finish putting the process into daemon mode: drop standard fds, and tell
  3163. * the parent process to exit. (Note: it's safe to call this more than once:
  3164. * calls after the first are ignored. Calls start_daemon first if it hasn't
  3165. * been called already.)
  3166. */
  3167. void
  3168. finish_daemon(const char *desired_cwd)
  3169. {
  3170. int nullfd;
  3171. char c = '.';
  3172. if (finish_daemon_called)
  3173. return;
  3174. if (!start_daemon_called)
  3175. start_daemon();
  3176. finish_daemon_called = 1;
  3177. if (!desired_cwd)
  3178. desired_cwd = "/";
  3179. /* Don't hold the wrong FS mounted */
  3180. if (chdir(desired_cwd) < 0) {
  3181. log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
  3182. exit(1);
  3183. }
  3184. nullfd = tor_open_cloexec("/dev/null", O_RDWR, 0);
  3185. if (nullfd < 0) {
  3186. log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
  3187. exit(1);
  3188. }
  3189. /* close fds linking to invoking terminal, but
  3190. * close usual incoming fds, but redirect them somewhere
  3191. * useful so the fds don't get reallocated elsewhere.
  3192. */
  3193. if (dup2(nullfd,0) < 0 ||
  3194. dup2(nullfd,1) < 0 ||
  3195. dup2(nullfd,2) < 0) {
  3196. log_err(LD_GENERAL,"dup2 failed. Exiting.");
  3197. exit(1);
  3198. }
  3199. if (nullfd > 2)
  3200. close(nullfd);
  3201. /* signal success */
  3202. if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
  3203. log_err(LD_GENERAL,"write failed. Exiting.");
  3204. }
  3205. close(daemon_filedes[1]);
  3206. }
  3207. #else
  3208. /* defined(_WIN32) */
  3209. void
  3210. start_daemon(void)
  3211. {
  3212. }
  3213. void
  3214. finish_daemon(const char *cp)
  3215. {
  3216. (void)cp;
  3217. }
  3218. #endif
  3219. /** Write the current process ID, followed by NL, into <b>filename</b>.
  3220. */
  3221. void
  3222. write_pidfile(char *filename)
  3223. {
  3224. FILE *pidfile;
  3225. if ((pidfile = fopen(filename, "w")) == NULL) {
  3226. log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
  3227. strerror(errno));
  3228. } else {
  3229. #ifdef _WIN32
  3230. fprintf(pidfile, "%d\n", (int)_getpid());
  3231. #else
  3232. fprintf(pidfile, "%d\n", (int)getpid());
  3233. #endif
  3234. fclose(pidfile);
  3235. }
  3236. }
  3237. #ifdef _WIN32
  3238. HANDLE
  3239. load_windows_system_library(const TCHAR *library_name)
  3240. {
  3241. TCHAR path[MAX_PATH];
  3242. unsigned n;
  3243. n = GetSystemDirectory(path, MAX_PATH);
  3244. if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH)
  3245. return 0;
  3246. _tcscat(path, TEXT("\\"));
  3247. _tcscat(path, library_name);
  3248. return LoadLibrary(path);
  3249. }
  3250. #endif
  3251. /** Format a single argument for being put on a Windows command line.
  3252. * Returns a newly allocated string */
  3253. static char *
  3254. format_win_cmdline_argument(const char *arg)
  3255. {
  3256. char *formatted_arg;
  3257. char need_quotes;
  3258. const char *c;
  3259. int i;
  3260. int bs_counter = 0;
  3261. /* Backslash we can point to when one is inserted into the string */
  3262. const char backslash = '\\';
  3263. /* Smartlist of *char */
  3264. smartlist_t *arg_chars;
  3265. arg_chars = smartlist_new();
  3266. /* Quote string if it contains whitespace or is empty */
  3267. need_quotes = (strchr(arg, ' ') || strchr(arg, '\t') || '\0' == arg[0]);
  3268. /* Build up smartlist of *chars */
  3269. for (c=arg; *c != '\0'; c++) {
  3270. if ('"' == *c) {
  3271. /* Double up backslashes preceding a quote */
  3272. for (i=0; i<(bs_counter*2); i++)
  3273. smartlist_add(arg_chars, (void*)&backslash);
  3274. bs_counter = 0;
  3275. /* Escape the quote */
  3276. smartlist_add(arg_chars, (void*)&backslash);
  3277. smartlist_add(arg_chars, (void*)c);
  3278. } else if ('\\' == *c) {
  3279. /* Count backslashes until we know whether to double up */
  3280. bs_counter++;
  3281. } else {
  3282. /* Don't double up slashes preceding a non-quote */
  3283. for (i=0; i<bs_counter; i++)
  3284. smartlist_add(arg_chars, (void*)&backslash);
  3285. bs_counter = 0;
  3286. smartlist_add(arg_chars, (void*)c);
  3287. }
  3288. }
  3289. /* Don't double up trailing backslashes */
  3290. for (i=0; i<bs_counter; i++)
  3291. smartlist_add(arg_chars, (void*)&backslash);
  3292. /* Allocate space for argument, quotes (if needed), and terminator */
  3293. const size_t formatted_arg_len = smartlist_len(arg_chars) +
  3294. (need_quotes ? 2 : 0) + 1;
  3295. formatted_arg = tor_malloc_zero(formatted_arg_len);
  3296. /* Add leading quote */
  3297. i=0;
  3298. if (need_quotes)
  3299. formatted_arg[i++] = '"';
  3300. /* Add characters */
  3301. SMARTLIST_FOREACH(arg_chars, char*, c,
  3302. {
  3303. formatted_arg[i++] = *c;
  3304. });
  3305. /* Add trailing quote */
  3306. if (need_quotes)
  3307. formatted_arg[i++] = '"';
  3308. formatted_arg[i] = '\0';
  3309. smartlist_free(arg_chars);
  3310. return formatted_arg;
  3311. }
  3312. /** Format a command line for use on Windows, which takes the command as a
  3313. * string rather than string array. Follows the rules from "Parsing C++
  3314. * Command-Line Arguments" in MSDN. Algorithm based on list2cmdline in the
  3315. * Python subprocess module. Returns a newly allocated string */
  3316. char *
  3317. tor_join_win_cmdline(const char *argv[])
  3318. {
  3319. smartlist_t *argv_list;
  3320. char *joined_argv;
  3321. int i;
  3322. /* Format each argument and put the result in a smartlist */
  3323. argv_list = smartlist_new();
  3324. for (i=0; argv[i] != NULL; i++) {
  3325. smartlist_add(argv_list, (void *)format_win_cmdline_argument(argv[i]));
  3326. }
  3327. /* Join the arguments with whitespace */
  3328. joined_argv = smartlist_join_strings(argv_list, " ", 0, NULL);
  3329. /* Free the newly allocated arguments, and the smartlist */
  3330. SMARTLIST_FOREACH(argv_list, char *, arg,
  3331. {
  3332. tor_free(arg);
  3333. });
  3334. smartlist_free(argv_list);
  3335. return joined_argv;
  3336. }
  3337. /* As format_{hex,dex}_number_sigsafe, but takes a <b>radix</b> argument
  3338. * in range 2..16 inclusive. */
  3339. static int
  3340. format_number_sigsafe(unsigned long x, char *buf, int buf_len,
  3341. unsigned int radix)
  3342. {
  3343. unsigned long tmp;
  3344. int len;
  3345. char *cp;
  3346. /* NOT tor_assert. This needs to be safe to run from within a signal handler,
  3347. * and from within the 'tor_assert() has failed' code. */
  3348. if (radix < 2 || radix > 16)
  3349. return 0;
  3350. /* Count how many digits we need. */
  3351. tmp = x;
  3352. len = 1;
  3353. while (tmp >= radix) {
  3354. tmp /= radix;
  3355. ++len;
  3356. }
  3357. /* Not long enough */
  3358. if (!buf || len >= buf_len)
  3359. return 0;
  3360. cp = buf + len;
  3361. *cp = '\0';
  3362. do {
  3363. unsigned digit = (unsigned) (x % radix);
  3364. tor_assert(cp > buf);
  3365. --cp;
  3366. *cp = "0123456789ABCDEF"[digit];
  3367. x /= radix;
  3368. } while (x);
  3369. /* NOT tor_assert; see above. */
  3370. if (cp != buf) {
  3371. abort();
  3372. }
  3373. return len;
  3374. }
  3375. /**
  3376. * Helper function to output hex numbers from within a signal handler.
  3377. *
  3378. * Writes the nul-terminated hexadecimal digits of <b>x</b> into a buffer
  3379. * <b>buf</b> of size <b>buf_len</b>, and return the actual number of digits
  3380. * written, not counting the terminal NUL.
  3381. *
  3382. * If there is insufficient space, write nothing and return 0.
  3383. *
  3384. * This accepts an unsigned int because format_helper_exit_status() needs to
  3385. * call it with a signed int and an unsigned char, and since the C standard
  3386. * does not guarantee that an int is wider than a char (an int must be at
  3387. * least 16 bits but it is permitted for a char to be that wide as well), we
  3388. * can't assume a signed int is sufficient to accomodate an unsigned char.
  3389. * Thus, format_helper_exit_status() will still need to emit any require '-'
  3390. * on its own.
  3391. *
  3392. * For most purposes, you'd want to use tor_snprintf("%x") instead of this
  3393. * function; it's designed to be used in code paths where you can't call
  3394. * arbitrary C functions.
  3395. */
  3396. int
  3397. format_hex_number_sigsafe(unsigned long x, char *buf, int buf_len)
  3398. {
  3399. return format_number_sigsafe(x, buf, buf_len, 16);
  3400. }
  3401. /** As format_hex_number_sigsafe, but format the number in base 10. */
  3402. int
  3403. format_dec_number_sigsafe(unsigned long x, char *buf, int buf_len)
  3404. {
  3405. return format_number_sigsafe(x, buf, buf_len, 10);
  3406. }
  3407. #ifndef _WIN32
  3408. /** Format <b>child_state</b> and <b>saved_errno</b> as a hex string placed in
  3409. * <b>hex_errno</b>. Called between fork and _exit, so must be signal-handler
  3410. * safe.
  3411. *
  3412. * <b>hex_errno</b> must have at least HEX_ERRNO_SIZE+1 bytes available.
  3413. *
  3414. * The format of <b>hex_errno</b> is: "CHILD_STATE/ERRNO\n", left-padded
  3415. * with spaces. CHILD_STATE indicates where
  3416. * in the processs of starting the child process did the failure occur (see
  3417. * CHILD_STATE_* macros for definition), and SAVED_ERRNO is the value of
  3418. * errno when the failure occurred.
  3419. *
  3420. * On success return the number of characters added to hex_errno, not counting
  3421. * the terminating NUL; return -1 on error.
  3422. */
  3423. STATIC int
  3424. format_helper_exit_status(unsigned char child_state, int saved_errno,
  3425. char *hex_errno)
  3426. {
  3427. unsigned int unsigned_errno;
  3428. int written, left;
  3429. char *cur;
  3430. size_t i;
  3431. int res = -1;
  3432. /* Fill hex_errno with spaces, and a trailing newline (memset may
  3433. not be signal handler safe, so we can't use it) */
  3434. for (i = 0; i < (HEX_ERRNO_SIZE - 1); i++)
  3435. hex_errno[i] = ' ';
  3436. hex_errno[HEX_ERRNO_SIZE - 1] = '\n';
  3437. /* Convert errno to be unsigned for hex conversion */
  3438. if (saved_errno < 0) {
  3439. // Avoid overflow on the cast to unsigned int when result is INT_MIN
  3440. // by adding 1 to the signed int negative value,
  3441. // then, after it has been negated and cast to unsigned,
  3442. // adding the original 1 back (the double-addition is intentional).
  3443. // Otherwise, the cast to signed could cause a temporary int
  3444. // to equal INT_MAX + 1, which is undefined.
  3445. unsigned_errno = ((unsigned int) -(saved_errno + 1)) + 1;
  3446. } else {
  3447. unsigned_errno = (unsigned int) saved_errno;
  3448. }
  3449. /*
  3450. * Count how many chars of space we have left, and keep a pointer into the
  3451. * current point in the buffer.
  3452. */
  3453. left = HEX_ERRNO_SIZE+1;
  3454. cur = hex_errno;
  3455. /* Emit child_state */
  3456. written = format_hex_number_sigsafe(child_state, cur, left);
  3457. if (written <= 0)
  3458. goto err;
  3459. /* Adjust left and cur */
  3460. left -= written;
  3461. cur += written;
  3462. if (left <= 0)
  3463. goto err;
  3464. /* Now the '/' */
  3465. *cur = '/';
  3466. /* Adjust left and cur */
  3467. ++cur;
  3468. --left;
  3469. if (left <= 0)
  3470. goto err;
  3471. /* Need minus? */
  3472. if (saved_errno < 0) {
  3473. *cur = '-';
  3474. ++cur;
  3475. --left;
  3476. if (left <= 0)
  3477. goto err;
  3478. }
  3479. /* Emit unsigned_errno */
  3480. written = format_hex_number_sigsafe(unsigned_errno, cur, left);
  3481. if (written <= 0)
  3482. goto err;
  3483. /* Adjust left and cur */
  3484. left -= written;
  3485. cur += written;
  3486. /* Check that we have enough space left for a newline and a NUL */
  3487. if (left <= 1)
  3488. goto err;
  3489. /* Emit the newline and NUL */
  3490. *cur++ = '\n';
  3491. *cur++ = '\0';
  3492. res = (int)(cur - hex_errno - 1);
  3493. goto done;
  3494. err:
  3495. /*
  3496. * In error exit, just write a '\0' in the first char so whatever called
  3497. * this at least won't fall off the end.
  3498. */
  3499. *hex_errno = '\0';
  3500. done:
  3501. return res;
  3502. }
  3503. #endif
  3504. /* Maximum number of file descriptors, if we cannot get it via sysconf() */
  3505. #define DEFAULT_MAX_FD 256
  3506. /** Terminate the process of <b>process_handle</b>.
  3507. * Code borrowed from Python's os.kill. */
  3508. int
  3509. tor_terminate_process(process_handle_t *process_handle)
  3510. {
  3511. #ifdef _WIN32
  3512. if (tor_get_exit_code(process_handle, 0, NULL) == PROCESS_EXIT_RUNNING) {
  3513. HANDLE handle = process_handle->pid.hProcess;
  3514. if (!TerminateProcess(handle, 0))
  3515. return -1;
  3516. else
  3517. return 0;
  3518. }
  3519. #else /* Unix */
  3520. if (process_handle->waitpid_cb) {
  3521. /* We haven't got a waitpid yet, so we can just kill off the process. */
  3522. return kill(process_handle->pid, SIGTERM);
  3523. }
  3524. #endif
  3525. return -1;
  3526. }
  3527. /** Return the Process ID of <b>process_handle</b>. */
  3528. int
  3529. tor_process_get_pid(process_handle_t *process_handle)
  3530. {
  3531. #ifdef _WIN32
  3532. return (int) process_handle->pid.dwProcessId;
  3533. #else
  3534. return (int) process_handle->pid;
  3535. #endif
  3536. }
  3537. #ifdef _WIN32
  3538. HANDLE
  3539. tor_process_get_stdout_pipe(process_handle_t *process_handle)
  3540. {
  3541. return process_handle->stdout_pipe;
  3542. }
  3543. #else
  3544. /* DOCDOC tor_process_get_stdout_pipe */
  3545. FILE *
  3546. tor_process_get_stdout_pipe(process_handle_t *process_handle)
  3547. {
  3548. return process_handle->stdout_handle;
  3549. }
  3550. #endif
  3551. /* DOCDOC process_handle_new */
  3552. static process_handle_t *
  3553. process_handle_new(void)
  3554. {
  3555. process_handle_t *out = tor_malloc_zero(sizeof(process_handle_t));
  3556. #ifdef _WIN32
  3557. out->stdout_pipe = INVALID_HANDLE_VALUE;
  3558. out->stderr_pipe = INVALID_HANDLE_VALUE;
  3559. #else
  3560. out->stdout_pipe = -1;
  3561. out->stderr_pipe = -1;
  3562. #endif
  3563. return out;
  3564. }
  3565. #ifndef _WIN32
  3566. /** Invoked when a process that we've launched via tor_spawn_background() has
  3567. * been found to have terminated.
  3568. */
  3569. static void
  3570. process_handle_waitpid_cb(int status, void *arg)
  3571. {
  3572. process_handle_t *process_handle = arg;
  3573. process_handle->waitpid_exit_status = status;
  3574. clear_waitpid_callback(process_handle->waitpid_cb);
  3575. if (process_handle->status == PROCESS_STATUS_RUNNING)
  3576. process_handle->status = PROCESS_STATUS_NOTRUNNING;
  3577. process_handle->waitpid_cb = 0;
  3578. }
  3579. #endif
  3580. /**
  3581. * @name child-process states
  3582. *
  3583. * Each of these values represents a possible state that a child process can
  3584. * be in. They're used to determine what to say when telling the parent how
  3585. * far along we were before failure.
  3586. *
  3587. * @{
  3588. */
  3589. #define CHILD_STATE_INIT 0
  3590. #define CHILD_STATE_PIPE 1
  3591. #define CHILD_STATE_MAXFD 2
  3592. #define CHILD_STATE_FORK 3
  3593. #define CHILD_STATE_DUPOUT 4
  3594. #define CHILD_STATE_DUPERR 5
  3595. #define CHILD_STATE_REDIRECT 6
  3596. #define CHILD_STATE_CLOSEFD 7
  3597. #define CHILD_STATE_EXEC 8
  3598. #define CHILD_STATE_FAILEXEC 9
  3599. /** @} */
  3600. /** Start a program in the background. If <b>filename</b> contains a '/', then
  3601. * it will be treated as an absolute or relative path. Otherwise, on
  3602. * non-Windows systems, the system path will be searched for <b>filename</b>.
  3603. * On Windows, only the current directory will be searched. Here, to search the
  3604. * system path (as well as the application directory, current working
  3605. * directory, and system directories), set filename to NULL.
  3606. *
  3607. * The strings in <b>argv</b> will be passed as the command line arguments of
  3608. * the child program (following convention, argv[0] should normally be the
  3609. * filename of the executable, and this must be the case if <b>filename</b> is
  3610. * NULL). The last element of argv must be NULL. A handle to the child process
  3611. * will be returned in process_handle (which must be non-NULL). Read
  3612. * process_handle.status to find out if the process was successfully launched.
  3613. * For convenience, process_handle.status is returned by this function.
  3614. *
  3615. * Some parts of this code are based on the POSIX subprocess module from
  3616. * Python, and example code from
  3617. * http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx.
  3618. */
  3619. int
  3620. tor_spawn_background(const char *const filename, const char **argv,
  3621. process_environment_t *env,
  3622. process_handle_t **process_handle_out)
  3623. {
  3624. #ifdef _WIN32
  3625. HANDLE stdout_pipe_read = NULL;
  3626. HANDLE stdout_pipe_write = NULL;
  3627. HANDLE stderr_pipe_read = NULL;
  3628. HANDLE stderr_pipe_write = NULL;
  3629. process_handle_t *process_handle;
  3630. int status;
  3631. STARTUPINFOA siStartInfo;
  3632. BOOL retval = FALSE;
  3633. SECURITY_ATTRIBUTES saAttr;
  3634. char *joined_argv;
  3635. saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
  3636. saAttr.bInheritHandle = TRUE;
  3637. /* TODO: should we set explicit security attributes? (#2046, comment 5) */
  3638. saAttr.lpSecurityDescriptor = NULL;
  3639. /* Assume failure to start process */
  3640. status = PROCESS_STATUS_ERROR;
  3641. /* Set up pipe for stdout */
  3642. if (!CreatePipe(&stdout_pipe_read, &stdout_pipe_write, &saAttr, 0)) {
  3643. log_warn(LD_GENERAL,
  3644. "Failed to create pipe for stdout communication with child process: %s",
  3645. format_win32_error(GetLastError()));
  3646. return status;
  3647. }
  3648. if (!SetHandleInformation(stdout_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
  3649. log_warn(LD_GENERAL,
  3650. "Failed to configure pipe for stdout communication with child "
  3651. "process: %s", format_win32_error(GetLastError()));
  3652. return status;
  3653. }
  3654. /* Set up pipe for stderr */
  3655. if (!CreatePipe(&stderr_pipe_read, &stderr_pipe_write, &saAttr, 0)) {
  3656. log_warn(LD_GENERAL,
  3657. "Failed to create pipe for stderr communication with child process: %s",
  3658. format_win32_error(GetLastError()));
  3659. return status;
  3660. }
  3661. if (!SetHandleInformation(stderr_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
  3662. log_warn(LD_GENERAL,
  3663. "Failed to configure pipe for stderr communication with child "
  3664. "process: %s", format_win32_error(GetLastError()));
  3665. return status;
  3666. }
  3667. /* Create the child process */
  3668. /* Windows expects argv to be a whitespace delimited string, so join argv up
  3669. */
  3670. joined_argv = tor_join_win_cmdline(argv);
  3671. process_handle = process_handle_new();
  3672. process_handle->status = status;
  3673. ZeroMemory(&(process_handle->pid), sizeof(PROCESS_INFORMATION));
  3674. ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
  3675. siStartInfo.cb = sizeof(STARTUPINFO);
  3676. siStartInfo.hStdError = stderr_pipe_write;
  3677. siStartInfo.hStdOutput = stdout_pipe_write;
  3678. siStartInfo.hStdInput = NULL;
  3679. siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
  3680. /* Create the child process */
  3681. retval = CreateProcessA(filename, // module name
  3682. joined_argv, // command line
  3683. /* TODO: should we set explicit security attributes? (#2046, comment 5) */
  3684. NULL, // process security attributes
  3685. NULL, // primary thread security attributes
  3686. TRUE, // handles are inherited
  3687. /*(TODO: set CREATE_NEW CONSOLE/PROCESS_GROUP to make GetExitCodeProcess()
  3688. * work?) */
  3689. CREATE_NO_WINDOW, // creation flags
  3690. (env==NULL) ? NULL : env->windows_environment_block,
  3691. NULL, // use parent's current directory
  3692. &siStartInfo, // STARTUPINFO pointer
  3693. &(process_handle->pid)); // receives PROCESS_INFORMATION
  3694. tor_free(joined_argv);
  3695. if (!retval) {
  3696. log_warn(LD_GENERAL,
  3697. "Failed to create child process %s: %s", filename?filename:argv[0],
  3698. format_win32_error(GetLastError()));
  3699. tor_free(process_handle);
  3700. } else {
  3701. /* TODO: Close hProcess and hThread in process_handle->pid? */
  3702. process_handle->stdout_pipe = stdout_pipe_read;
  3703. process_handle->stderr_pipe = stderr_pipe_read;
  3704. status = process_handle->status = PROCESS_STATUS_RUNNING;
  3705. }
  3706. /* TODO: Close pipes on exit */
  3707. *process_handle_out = process_handle;
  3708. return status;
  3709. #else // _WIN32
  3710. pid_t pid;
  3711. int stdout_pipe[2];
  3712. int stderr_pipe[2];
  3713. int fd, retval;
  3714. ssize_t nbytes;
  3715. process_handle_t *process_handle;
  3716. int status;
  3717. const char *error_message = SPAWN_ERROR_MESSAGE;
  3718. size_t error_message_length;
  3719. /* Represents where in the process of spawning the program is;
  3720. this is used for printing out the error message */
  3721. unsigned char child_state = CHILD_STATE_INIT;
  3722. char hex_errno[HEX_ERRNO_SIZE + 2]; /* + 1 should be sufficient actually */
  3723. static int max_fd = -1;
  3724. status = PROCESS_STATUS_ERROR;
  3725. /* We do the strlen here because strlen() is not signal handler safe,
  3726. and we are not allowed to use unsafe functions between fork and exec */
  3727. error_message_length = strlen(error_message);
  3728. child_state = CHILD_STATE_PIPE;
  3729. /* Set up pipe for redirecting stdout and stderr of child */
  3730. retval = pipe(stdout_pipe);
  3731. if (-1 == retval) {
  3732. log_warn(LD_GENERAL,
  3733. "Failed to set up pipe for stdout communication with child process: %s",
  3734. strerror(errno));
  3735. return status;
  3736. }
  3737. retval = pipe(stderr_pipe);
  3738. if (-1 == retval) {
  3739. log_warn(LD_GENERAL,
  3740. "Failed to set up pipe for stderr communication with child process: %s",
  3741. strerror(errno));
  3742. close(stdout_pipe[0]);
  3743. close(stdout_pipe[1]);
  3744. return status;
  3745. }
  3746. child_state = CHILD_STATE_MAXFD;
  3747. #ifdef _SC_OPEN_MAX
  3748. if (-1 == max_fd) {
  3749. max_fd = (int) sysconf(_SC_OPEN_MAX);
  3750. if (max_fd == -1) {
  3751. max_fd = DEFAULT_MAX_FD;
  3752. log_warn(LD_GENERAL,
  3753. "Cannot find maximum file descriptor, assuming %d", max_fd);
  3754. }
  3755. }
  3756. #else
  3757. max_fd = DEFAULT_MAX_FD;
  3758. #endif
  3759. child_state = CHILD_STATE_FORK;
  3760. pid = fork();
  3761. if (0 == pid) {
  3762. /* In child */
  3763. child_state = CHILD_STATE_DUPOUT;
  3764. /* Link child stdout to the write end of the pipe */
  3765. retval = dup2(stdout_pipe[1], STDOUT_FILENO);
  3766. if (-1 == retval)
  3767. goto error;
  3768. child_state = CHILD_STATE_DUPERR;
  3769. /* Link child stderr to the write end of the pipe */
  3770. retval = dup2(stderr_pipe[1], STDERR_FILENO);
  3771. if (-1 == retval)
  3772. goto error;
  3773. child_state = CHILD_STATE_REDIRECT;
  3774. /* Link stdin to /dev/null */
  3775. fd = open("/dev/null", O_RDONLY); /* NOT cloexec, obviously. */
  3776. if (fd != -1)
  3777. dup2(fd, STDIN_FILENO);
  3778. else
  3779. goto error;
  3780. child_state = CHILD_STATE_CLOSEFD;
  3781. close(stderr_pipe[0]);
  3782. close(stderr_pipe[1]);
  3783. close(stdout_pipe[0]);
  3784. close(stdout_pipe[1]);
  3785. close(fd);
  3786. /* Close all other fds, including the read end of the pipe */
  3787. /* XXX: We should now be doing enough FD_CLOEXEC setting to make
  3788. * this needless. */
  3789. for (fd = STDERR_FILENO + 1; fd < max_fd; fd++) {
  3790. close(fd);
  3791. }
  3792. child_state = CHILD_STATE_EXEC;
  3793. /* Call the requested program. We need the cast because
  3794. execvp doesn't define argv as const, even though it
  3795. does not modify the arguments */
  3796. if (env)
  3797. execve(filename, (char *const *) argv, env->unixoid_environment_block);
  3798. else
  3799. execvp(filename, (char *const *) argv);
  3800. /* If we got here, the exec or open(/dev/null) failed */
  3801. child_state = CHILD_STATE_FAILEXEC;
  3802. error:
  3803. {
  3804. /* XXX: are we leaking fds from the pipe? */
  3805. int n;
  3806. n = format_helper_exit_status(child_state, errno, hex_errno);
  3807. if (n >= 0) {
  3808. /* Write the error message. GCC requires that we check the return
  3809. value, but there is nothing we can do if it fails */
  3810. /* TODO: Don't use STDOUT, use a pipe set up just for this purpose */
  3811. nbytes = write(STDOUT_FILENO, error_message, error_message_length);
  3812. nbytes = write(STDOUT_FILENO, hex_errno, n);
  3813. }
  3814. }
  3815. (void) nbytes;
  3816. _exit(255);
  3817. /* Never reached, but avoids compiler warning */
  3818. return status;
  3819. }
  3820. /* In parent */
  3821. if (-1 == pid) {
  3822. log_warn(LD_GENERAL, "Failed to fork child process: %s", strerror(errno));
  3823. close(stdout_pipe[0]);
  3824. close(stdout_pipe[1]);
  3825. close(stderr_pipe[0]);
  3826. close(stderr_pipe[1]);
  3827. return status;
  3828. }
  3829. process_handle = process_handle_new();
  3830. process_handle->status = status;
  3831. process_handle->pid = pid;
  3832. /* TODO: If the child process forked but failed to exec, waitpid it */
  3833. /* Return read end of the pipes to caller, and close write end */
  3834. process_handle->stdout_pipe = stdout_pipe[0];
  3835. retval = close(stdout_pipe[1]);
  3836. if (-1 == retval) {
  3837. log_warn(LD_GENERAL,
  3838. "Failed to close write end of stdout pipe in parent process: %s",
  3839. strerror(errno));
  3840. }
  3841. process_handle->waitpid_cb = set_waitpid_callback(pid,
  3842. process_handle_waitpid_cb,
  3843. process_handle);
  3844. process_handle->stderr_pipe = stderr_pipe[0];
  3845. retval = close(stderr_pipe[1]);
  3846. if (-1 == retval) {
  3847. log_warn(LD_GENERAL,
  3848. "Failed to close write end of stderr pipe in parent process: %s",
  3849. strerror(errno));
  3850. }
  3851. status = process_handle->status = PROCESS_STATUS_RUNNING;
  3852. /* Set stdout/stderr pipes to be non-blocking */
  3853. if (fcntl(process_handle->stdout_pipe, F_SETFL, O_NONBLOCK) < 0 ||
  3854. fcntl(process_handle->stderr_pipe, F_SETFL, O_NONBLOCK) < 0) {
  3855. log_warn(LD_GENERAL, "Failed to set stderror/stdout pipes nonblocking "
  3856. "in parent process: %s", strerror(errno));
  3857. }
  3858. /* Open the buffered IO streams */
  3859. process_handle->stdout_handle = fdopen(process_handle->stdout_pipe, "r");
  3860. process_handle->stderr_handle = fdopen(process_handle->stderr_pipe, "r");
  3861. *process_handle_out = process_handle;
  3862. return process_handle->status;
  3863. #endif // _WIN32
  3864. }
  3865. /** Destroy all resources allocated by the process handle in
  3866. * <b>process_handle</b>.
  3867. * If <b>also_terminate_process</b> is true, also terminate the
  3868. * process of the process handle. */
  3869. MOCK_IMPL(void,
  3870. tor_process_handle_destroy,(process_handle_t *process_handle,
  3871. int also_terminate_process))
  3872. {
  3873. if (!process_handle)
  3874. return;
  3875. if (also_terminate_process) {
  3876. if (tor_terminate_process(process_handle) < 0) {
  3877. const char *errstr =
  3878. #ifdef _WIN32
  3879. format_win32_error(GetLastError());
  3880. #else
  3881. strerror(errno);
  3882. #endif
  3883. log_notice(LD_GENERAL, "Failed to terminate process with "
  3884. "PID '%d' ('%s').", tor_process_get_pid(process_handle),
  3885. errstr);
  3886. } else {
  3887. log_info(LD_GENERAL, "Terminated process with PID '%d'.",
  3888. tor_process_get_pid(process_handle));
  3889. }
  3890. }
  3891. process_handle->status = PROCESS_STATUS_NOTRUNNING;
  3892. #ifdef _WIN32
  3893. if (process_handle->stdout_pipe)
  3894. CloseHandle(process_handle->stdout_pipe);
  3895. if (process_handle->stderr_pipe)
  3896. CloseHandle(process_handle->stderr_pipe);
  3897. #else
  3898. if (process_handle->stdout_handle)
  3899. fclose(process_handle->stdout_handle);
  3900. if (process_handle->stderr_handle)
  3901. fclose(process_handle->stderr_handle);
  3902. clear_waitpid_callback(process_handle->waitpid_cb);
  3903. #endif
  3904. memset(process_handle, 0x0f, sizeof(process_handle_t));
  3905. tor_free(process_handle);
  3906. }
  3907. /** Get the exit code of a process specified by <b>process_handle</b> and store
  3908. * it in <b>exit_code</b>, if set to a non-NULL value. If <b>block</b> is set
  3909. * to true, the call will block until the process has exited. Otherwise if
  3910. * the process is still running, the function will return
  3911. * PROCESS_EXIT_RUNNING, and exit_code will be left unchanged. Returns
  3912. * PROCESS_EXIT_EXITED if the process did exit. If there is a failure,
  3913. * PROCESS_EXIT_ERROR will be returned and the contents of exit_code (if
  3914. * non-NULL) will be undefined. N.B. Under *nix operating systems, this will
  3915. * probably not work in Tor, because waitpid() is called in main.c to reap any
  3916. * terminated child processes.*/
  3917. int
  3918. tor_get_exit_code(process_handle_t *process_handle,
  3919. int block, int *exit_code)
  3920. {
  3921. #ifdef _WIN32
  3922. DWORD retval;
  3923. BOOL success;
  3924. if (block) {
  3925. /* Wait for the process to exit */
  3926. retval = WaitForSingleObject(process_handle->pid.hProcess, INFINITE);
  3927. if (retval != WAIT_OBJECT_0) {
  3928. log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
  3929. (int)retval, format_win32_error(GetLastError()));
  3930. return PROCESS_EXIT_ERROR;
  3931. }
  3932. } else {
  3933. retval = WaitForSingleObject(process_handle->pid.hProcess, 0);
  3934. if (WAIT_TIMEOUT == retval) {
  3935. /* Process has not exited */
  3936. return PROCESS_EXIT_RUNNING;
  3937. } else if (retval != WAIT_OBJECT_0) {
  3938. log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
  3939. (int)retval, format_win32_error(GetLastError()));
  3940. return PROCESS_EXIT_ERROR;
  3941. }
  3942. }
  3943. if (exit_code != NULL) {
  3944. success = GetExitCodeProcess(process_handle->pid.hProcess,
  3945. (PDWORD)exit_code);
  3946. if (!success) {
  3947. log_warn(LD_GENERAL, "GetExitCodeProcess() failed: %s",
  3948. format_win32_error(GetLastError()));
  3949. return PROCESS_EXIT_ERROR;
  3950. }
  3951. }
  3952. #else
  3953. int stat_loc;
  3954. int retval;
  3955. if (process_handle->waitpid_cb) {
  3956. /* We haven't processed a SIGCHLD yet. */
  3957. retval = waitpid(process_handle->pid, &stat_loc, block?0:WNOHANG);
  3958. if (retval == process_handle->pid) {
  3959. clear_waitpid_callback(process_handle->waitpid_cb);
  3960. process_handle->waitpid_cb = NULL;
  3961. process_handle->waitpid_exit_status = stat_loc;
  3962. }
  3963. } else {
  3964. /* We already got a SIGCHLD for this process, and handled it. */
  3965. retval = process_handle->pid;
  3966. stat_loc = process_handle->waitpid_exit_status;
  3967. }
  3968. if (!block && 0 == retval) {
  3969. /* Process has not exited */
  3970. return PROCESS_EXIT_RUNNING;
  3971. } else if (retval != process_handle->pid) {
  3972. log_warn(LD_GENERAL, "waitpid() failed for PID %d: %s",
  3973. process_handle->pid, strerror(errno));
  3974. return PROCESS_EXIT_ERROR;
  3975. }
  3976. if (!WIFEXITED(stat_loc)) {
  3977. log_warn(LD_GENERAL, "Process %d did not exit normally",
  3978. process_handle->pid);
  3979. return PROCESS_EXIT_ERROR;
  3980. }
  3981. if (exit_code != NULL)
  3982. *exit_code = WEXITSTATUS(stat_loc);
  3983. #endif // _WIN32
  3984. return PROCESS_EXIT_EXITED;
  3985. }
  3986. /** Helper: return the number of characters in <b>s</b> preceding the first
  3987. * occurrence of <b>ch</b>. If <b>ch</b> does not occur in <b>s</b>, return
  3988. * the length of <b>s</b>. Should be equivalent to strspn(s, "ch"). */
  3989. static INLINE size_t
  3990. str_num_before(const char *s, char ch)
  3991. {
  3992. const char *cp = strchr(s, ch);
  3993. if (cp)
  3994. return cp - s;
  3995. else
  3996. return strlen(s);
  3997. }
  3998. /** Return non-zero iff getenv would consider <b>s1</b> and <b>s2</b>
  3999. * to have the same name as strings in a process's environment. */
  4000. int
  4001. environment_variable_names_equal(const char *s1, const char *s2)
  4002. {
  4003. size_t s1_name_len = str_num_before(s1, '=');
  4004. size_t s2_name_len = str_num_before(s2, '=');
  4005. return (s1_name_len == s2_name_len &&
  4006. tor_memeq(s1, s2, s1_name_len));
  4007. }
  4008. /** Free <b>env</b> (assuming it was produced by
  4009. * process_environment_make). */
  4010. void
  4011. process_environment_free(process_environment_t *env)
  4012. {
  4013. if (env == NULL) return;
  4014. /* As both an optimization hack to reduce consing on Unixoid systems
  4015. * and a nice way to ensure that some otherwise-Windows-specific
  4016. * code will always get tested before changes to it get merged, the
  4017. * strings which env->unixoid_environment_block points to are packed
  4018. * into env->windows_environment_block. */
  4019. tor_free(env->unixoid_environment_block);
  4020. tor_free(env->windows_environment_block);
  4021. tor_free(env);
  4022. }
  4023. /** Make a process_environment_t containing the environment variables
  4024. * specified in <b>env_vars</b> (as C strings of the form
  4025. * "NAME=VALUE"). */
  4026. process_environment_t *
  4027. process_environment_make(struct smartlist_t *env_vars)
  4028. {
  4029. process_environment_t *env = tor_malloc_zero(sizeof(process_environment_t));
  4030. size_t n_env_vars = smartlist_len(env_vars);
  4031. size_t i;
  4032. size_t total_env_length;
  4033. smartlist_t *env_vars_sorted;
  4034. tor_assert(n_env_vars + 1 != 0);
  4035. env->unixoid_environment_block = tor_calloc(n_env_vars + 1, sizeof(char *));
  4036. /* env->unixoid_environment_block is already NULL-terminated,
  4037. * because we assume that NULL == 0 (and check that during compilation). */
  4038. total_env_length = 1; /* terminating NUL of terminating empty string */
  4039. for (i = 0; i < n_env_vars; ++i) {
  4040. const char *s = smartlist_get(env_vars, i);
  4041. size_t slen = strlen(s);
  4042. tor_assert(slen + 1 != 0);
  4043. tor_assert(slen + 1 < SIZE_MAX - total_env_length);
  4044. total_env_length += slen + 1;
  4045. }
  4046. env->windows_environment_block = tor_malloc_zero(total_env_length);
  4047. /* env->windows_environment_block is already
  4048. * (NUL-terminated-empty-string)-terminated. */
  4049. /* Some versions of Windows supposedly require that environment
  4050. * blocks be sorted. Or maybe some Windows programs (or their
  4051. * runtime libraries) fail to look up strings in non-sorted
  4052. * environment blocks.
  4053. *
  4054. * Also, sorting strings makes it easy to find duplicate environment
  4055. * variables and environment-variable strings without an '=' on all
  4056. * OSes, and they can cause badness. Let's complain about those. */
  4057. env_vars_sorted = smartlist_new();
  4058. smartlist_add_all(env_vars_sorted, env_vars);
  4059. smartlist_sort_strings(env_vars_sorted);
  4060. /* Now copy the strings into the environment blocks. */
  4061. {
  4062. char *cp = env->windows_environment_block;
  4063. const char *prev_env_var = NULL;
  4064. for (i = 0; i < n_env_vars; ++i) {
  4065. const char *s = smartlist_get(env_vars_sorted, i);
  4066. size_t slen = strlen(s);
  4067. size_t s_name_len = str_num_before(s, '=');
  4068. if (s_name_len == slen) {
  4069. log_warn(LD_GENERAL,
  4070. "Preparing an environment containing a variable "
  4071. "without a value: %s",
  4072. s);
  4073. }
  4074. if (prev_env_var != NULL &&
  4075. environment_variable_names_equal(s, prev_env_var)) {
  4076. log_warn(LD_GENERAL,
  4077. "Preparing an environment containing two variables "
  4078. "with the same name: %s and %s",
  4079. prev_env_var, s);
  4080. }
  4081. prev_env_var = s;
  4082. /* Actually copy the string into the environment. */
  4083. memcpy(cp, s, slen+1);
  4084. env->unixoid_environment_block[i] = cp;
  4085. cp += slen+1;
  4086. }
  4087. tor_assert(cp == env->windows_environment_block + total_env_length - 1);
  4088. }
  4089. smartlist_free(env_vars_sorted);
  4090. return env;
  4091. }
  4092. /** Return a newly allocated smartlist containing every variable in
  4093. * this process's environment, as a NUL-terminated string of the form
  4094. * "NAME=VALUE". Note that on some/many/most/all OSes, the parent
  4095. * process can put strings not of that form in our environment;
  4096. * callers should try to not get crashed by that.
  4097. *
  4098. * The returned strings are heap-allocated, and must be freed by the
  4099. * caller. */
  4100. struct smartlist_t *
  4101. get_current_process_environment_variables(void)
  4102. {
  4103. smartlist_t *sl = smartlist_new();
  4104. char **environ_tmp; /* Not const char ** ? Really? */
  4105. for (environ_tmp = get_environment(); *environ_tmp; ++environ_tmp) {
  4106. smartlist_add(sl, tor_strdup(*environ_tmp));
  4107. }
  4108. return sl;
  4109. }
  4110. /** For each string s in <b>env_vars</b> such that
  4111. * environment_variable_names_equal(s, <b>new_var</b>), remove it; if
  4112. * <b>free_p</b> is non-zero, call <b>free_old</b>(s). If
  4113. * <b>new_var</b> contains '=', insert it into <b>env_vars</b>. */
  4114. void
  4115. set_environment_variable_in_smartlist(struct smartlist_t *env_vars,
  4116. const char *new_var,
  4117. void (*free_old)(void*),
  4118. int free_p)
  4119. {
  4120. SMARTLIST_FOREACH_BEGIN(env_vars, const char *, s) {
  4121. if (environment_variable_names_equal(s, new_var)) {
  4122. SMARTLIST_DEL_CURRENT(env_vars, s);
  4123. if (free_p) {
  4124. free_old((void *)s);
  4125. }
  4126. }
  4127. } SMARTLIST_FOREACH_END(s);
  4128. if (strchr(new_var, '=') != NULL) {
  4129. smartlist_add(env_vars, (void *)new_var);
  4130. }
  4131. }
  4132. #ifdef _WIN32
  4133. /** Read from a handle <b>h</b> into <b>buf</b>, up to <b>count</b> bytes. If
  4134. * <b>hProcess</b> is NULL, the function will return immediately if there is
  4135. * nothing more to read. Otherwise <b>hProcess</b> should be set to the handle
  4136. * to the process owning the <b>h</b>. In this case, the function will exit
  4137. * only once the process has exited, or <b>count</b> bytes are read. Returns
  4138. * the number of bytes read, or -1 on error. */
  4139. ssize_t
  4140. tor_read_all_handle(HANDLE h, char *buf, size_t count,
  4141. const process_handle_t *process)
  4142. {
  4143. size_t numread = 0;
  4144. BOOL retval;
  4145. DWORD byte_count;
  4146. BOOL process_exited = FALSE;
  4147. if (count > SIZE_T_CEILING || count > SSIZE_MAX)
  4148. return -1;
  4149. while (numread != count) {
  4150. /* Check if there is anything to read */
  4151. retval = PeekNamedPipe(h, NULL, 0, NULL, &byte_count, NULL);
  4152. if (!retval) {
  4153. log_warn(LD_GENERAL,
  4154. "Failed to peek from handle: %s",
  4155. format_win32_error(GetLastError()));
  4156. return -1;
  4157. } else if (0 == byte_count) {
  4158. /* Nothing available: process exited or it is busy */
  4159. /* Exit if we don't know whether the process is running */
  4160. if (NULL == process)
  4161. break;
  4162. /* The process exited and there's nothing left to read from it */
  4163. if (process_exited)
  4164. break;
  4165. /* If process is not running, check for output one more time in case
  4166. it wrote something after the peek was performed. Otherwise keep on
  4167. waiting for output */
  4168. tor_assert(process != NULL);
  4169. byte_count = WaitForSingleObject(process->pid.hProcess, 0);
  4170. if (WAIT_TIMEOUT != byte_count)
  4171. process_exited = TRUE;
  4172. continue;
  4173. }
  4174. /* There is data to read; read it */
  4175. retval = ReadFile(h, buf+numread, count-numread, &byte_count, NULL);
  4176. tor_assert(byte_count + numread <= count);
  4177. if (!retval) {
  4178. log_warn(LD_GENERAL, "Failed to read from handle: %s",
  4179. format_win32_error(GetLastError()));
  4180. return -1;
  4181. } else if (0 == byte_count) {
  4182. /* End of file */
  4183. break;
  4184. }
  4185. numread += byte_count;
  4186. }
  4187. return (ssize_t)numread;
  4188. }
  4189. #else
  4190. /** Read from a handle <b>h</b> into <b>buf</b>, up to <b>count</b> bytes. If
  4191. * <b>process</b> is NULL, the function will return immediately if there is
  4192. * nothing more to read. Otherwise data will be read until end of file, or
  4193. * <b>count</b> bytes are read. Returns the number of bytes read, or -1 on
  4194. * error. Sets <b>eof</b> to true if <b>eof</b> is not NULL and the end of the
  4195. * file has been reached. */
  4196. ssize_t
  4197. tor_read_all_handle(FILE *h, char *buf, size_t count,
  4198. const process_handle_t *process,
  4199. int *eof)
  4200. {
  4201. size_t numread = 0;
  4202. char *retval;
  4203. if (eof)
  4204. *eof = 0;
  4205. if (count > SIZE_T_CEILING || count > SSIZE_MAX)
  4206. return -1;
  4207. while (numread != count) {
  4208. /* Use fgets because that is what we use in log_from_pipe() */
  4209. retval = fgets(buf+numread, (int)(count-numread), h);
  4210. if (NULL == retval) {
  4211. if (feof(h)) {
  4212. log_debug(LD_GENERAL, "fgets() reached end of file");
  4213. if (eof)
  4214. *eof = 1;
  4215. break;
  4216. } else {
  4217. if (EAGAIN == errno) {
  4218. if (process)
  4219. continue;
  4220. else
  4221. break;
  4222. } else {
  4223. log_warn(LD_GENERAL, "fgets() from handle failed: %s",
  4224. strerror(errno));
  4225. return -1;
  4226. }
  4227. }
  4228. }
  4229. tor_assert(retval != NULL);
  4230. tor_assert(strlen(retval) + numread <= count);
  4231. numread += strlen(retval);
  4232. }
  4233. log_debug(LD_GENERAL, "fgets() read %d bytes from handle", (int)numread);
  4234. return (ssize_t)numread;
  4235. }
  4236. #endif
  4237. /** Read from stdout of a process until the process exits. */
  4238. ssize_t
  4239. tor_read_all_from_process_stdout(const process_handle_t *process_handle,
  4240. char *buf, size_t count)
  4241. {
  4242. #ifdef _WIN32
  4243. return tor_read_all_handle(process_handle->stdout_pipe, buf, count,
  4244. process_handle);
  4245. #else
  4246. return tor_read_all_handle(process_handle->stdout_handle, buf, count,
  4247. process_handle, NULL);
  4248. #endif
  4249. }
  4250. /** Read from stdout of a process until the process exits. */
  4251. ssize_t
  4252. tor_read_all_from_process_stderr(const process_handle_t *process_handle,
  4253. char *buf, size_t count)
  4254. {
  4255. #ifdef _WIN32
  4256. return tor_read_all_handle(process_handle->stderr_pipe, buf, count,
  4257. process_handle);
  4258. #else
  4259. return tor_read_all_handle(process_handle->stderr_handle, buf, count,
  4260. process_handle, NULL);
  4261. #endif
  4262. }
  4263. /** Split buf into lines, and add to smartlist. The buffer <b>buf</b> will be
  4264. * modified. The resulting smartlist will consist of pointers to buf, so there
  4265. * is no need to free the contents of sl. <b>buf</b> must be a NUL-terminated
  4266. * string. <b>len</b> should be set to the length of the buffer excluding the
  4267. * NUL. Non-printable characters (including NUL) will be replaced with "." */
  4268. int
  4269. tor_split_lines(smartlist_t *sl, char *buf, int len)
  4270. {
  4271. /* Index in buf of the start of the current line */
  4272. int start = 0;
  4273. /* Index in buf of the current character being processed */
  4274. int cur = 0;
  4275. /* Are we currently in a line */
  4276. char in_line = 0;
  4277. /* Loop over string */
  4278. while (cur < len) {
  4279. /* Loop until end of line or end of string */
  4280. for (; cur < len; cur++) {
  4281. if (in_line) {
  4282. if ('\r' == buf[cur] || '\n' == buf[cur]) {
  4283. /* End of line */
  4284. buf[cur] = '\0';
  4285. /* Point cur to the next line */
  4286. cur++;
  4287. /* Line starts at start and ends with a nul */
  4288. break;
  4289. } else {
  4290. if (!TOR_ISPRINT(buf[cur]))
  4291. buf[cur] = '.';
  4292. }
  4293. } else {
  4294. if ('\r' == buf[cur] || '\n' == buf[cur]) {
  4295. /* Skip leading vertical space */
  4296. ;
  4297. } else {
  4298. in_line = 1;
  4299. start = cur;
  4300. if (!TOR_ISPRINT(buf[cur]))
  4301. buf[cur] = '.';
  4302. }
  4303. }
  4304. }
  4305. /* We are at the end of the line or end of string. If in_line is true there
  4306. * is a line which starts at buf+start and ends at a NUL. cur points to
  4307. * the character after the NUL. */
  4308. if (in_line)
  4309. smartlist_add(sl, (void *)(buf+start));
  4310. in_line = 0;
  4311. }
  4312. return smartlist_len(sl);
  4313. }
  4314. /** Return a string corresponding to <b>stream_status</b>. */
  4315. const char *
  4316. stream_status_to_string(enum stream_status stream_status)
  4317. {
  4318. switch (stream_status) {
  4319. case IO_STREAM_OKAY:
  4320. return "okay";
  4321. case IO_STREAM_EAGAIN:
  4322. return "temporarily unavailable";
  4323. case IO_STREAM_TERM:
  4324. return "terminated";
  4325. case IO_STREAM_CLOSED:
  4326. return "closed";
  4327. default:
  4328. tor_fragile_assert();
  4329. return "unknown";
  4330. }
  4331. }
  4332. /* DOCDOC */
  4333. static void
  4334. log_portfw_spawn_error_message(const char *buf,
  4335. const char *executable, int *child_status)
  4336. {
  4337. /* Parse error message */
  4338. int retval, child_state, saved_errno;
  4339. retval = tor_sscanf(buf, SPAWN_ERROR_MESSAGE "%x/%x",
  4340. &child_state, &saved_errno);
  4341. if (retval == 2) {
  4342. log_warn(LD_GENERAL,
  4343. "Failed to start child process \"%s\" in state %d: %s",
  4344. executable, child_state, strerror(saved_errno));
  4345. if (child_status)
  4346. *child_status = 1;
  4347. } else {
  4348. /* Failed to parse message from child process, log it as a
  4349. warning */
  4350. log_warn(LD_GENERAL,
  4351. "Unexpected message from port forwarding helper \"%s\": %s",
  4352. executable, buf);
  4353. }
  4354. }
  4355. #ifdef _WIN32
  4356. /** Return a smartlist containing lines outputted from
  4357. * <b>handle</b>. Return NULL on error, and set
  4358. * <b>stream_status_out</b> appropriately. */
  4359. MOCK_IMPL(smartlist_t *,
  4360. tor_get_lines_from_handle, (HANDLE *handle,
  4361. enum stream_status *stream_status_out))
  4362. {
  4363. int pos;
  4364. char stdout_buf[600] = {0};
  4365. smartlist_t *lines = NULL;
  4366. tor_assert(stream_status_out);
  4367. *stream_status_out = IO_STREAM_TERM;
  4368. pos = tor_read_all_handle(handle, stdout_buf, sizeof(stdout_buf) - 1, NULL);
  4369. if (pos < 0) {
  4370. *stream_status_out = IO_STREAM_TERM;
  4371. return NULL;
  4372. }
  4373. if (pos == 0) {
  4374. *stream_status_out = IO_STREAM_EAGAIN;
  4375. return NULL;
  4376. }
  4377. /* End with a null even if there isn't a \r\n at the end */
  4378. /* TODO: What if this is a partial line? */
  4379. stdout_buf[pos] = '\0';
  4380. /* Split up the buffer */
  4381. lines = smartlist_new();
  4382. tor_split_lines(lines, stdout_buf, pos);
  4383. /* Currently 'lines' is populated with strings residing on the
  4384. stack. Replace them with their exact copies on the heap: */
  4385. SMARTLIST_FOREACH(lines, char *, line,
  4386. SMARTLIST_REPLACE_CURRENT(lines, line, tor_strdup(line)));
  4387. *stream_status_out = IO_STREAM_OKAY;
  4388. return lines;
  4389. }
  4390. /** Read from stream, and send lines to log at the specified log level.
  4391. * Returns -1 if there is a error reading, and 0 otherwise.
  4392. * If the generated stream is flushed more often than on new lines, or
  4393. * a read exceeds 256 bytes, lines will be truncated. This should be fixed,
  4394. * along with the corresponding problem on *nix (see bug #2045).
  4395. */
  4396. static int
  4397. log_from_handle(HANDLE *pipe, int severity)
  4398. {
  4399. char buf[256];
  4400. int pos;
  4401. smartlist_t *lines;
  4402. pos = tor_read_all_handle(pipe, buf, sizeof(buf) - 1, NULL);
  4403. if (pos < 0) {
  4404. /* Error */
  4405. log_warn(LD_GENERAL, "Failed to read data from subprocess");
  4406. return -1;
  4407. }
  4408. if (0 == pos) {
  4409. /* There's nothing to read (process is busy or has exited) */
  4410. log_debug(LD_GENERAL, "Subprocess had nothing to say");
  4411. return 0;
  4412. }
  4413. /* End with a null even if there isn't a \r\n at the end */
  4414. /* TODO: What if this is a partial line? */
  4415. buf[pos] = '\0';
  4416. log_debug(LD_GENERAL, "Subprocess had %d bytes to say", pos);
  4417. /* Split up the buffer */
  4418. lines = smartlist_new();
  4419. tor_split_lines(lines, buf, pos);
  4420. /* Log each line */
  4421. SMARTLIST_FOREACH(lines, char *, line,
  4422. {
  4423. log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", line);
  4424. });
  4425. smartlist_free(lines);
  4426. return 0;
  4427. }
  4428. #else
  4429. /** Return a smartlist containing lines outputted from
  4430. * <b>handle</b>. Return NULL on error, and set
  4431. * <b>stream_status_out</b> appropriately. */
  4432. MOCK_IMPL(smartlist_t *,
  4433. tor_get_lines_from_handle, (FILE *handle,
  4434. enum stream_status *stream_status_out))
  4435. {
  4436. enum stream_status stream_status;
  4437. char stdout_buf[400];
  4438. smartlist_t *lines = NULL;
  4439. while (1) {
  4440. memset(stdout_buf, 0, sizeof(stdout_buf));
  4441. stream_status = get_string_from_pipe(handle,
  4442. stdout_buf, sizeof(stdout_buf) - 1);
  4443. if (stream_status != IO_STREAM_OKAY)
  4444. goto done;
  4445. if (!lines) lines = smartlist_new();
  4446. smartlist_add(lines, tor_strdup(stdout_buf));
  4447. }
  4448. done:
  4449. *stream_status_out = stream_status;
  4450. return lines;
  4451. }
  4452. /** Read from stream, and send lines to log at the specified log level.
  4453. * Returns 1 if stream is closed normally, -1 if there is a error reading, and
  4454. * 0 otherwise. Handles lines from tor-fw-helper and
  4455. * tor_spawn_background() specially.
  4456. */
  4457. static int
  4458. log_from_pipe(FILE *stream, int severity, const char *executable,
  4459. int *child_status)
  4460. {
  4461. char buf[256];
  4462. enum stream_status r;
  4463. for (;;) {
  4464. r = get_string_from_pipe(stream, buf, sizeof(buf) - 1);
  4465. if (r == IO_STREAM_CLOSED) {
  4466. return 1;
  4467. } else if (r == IO_STREAM_EAGAIN) {
  4468. return 0;
  4469. } else if (r == IO_STREAM_TERM) {
  4470. return -1;
  4471. }
  4472. tor_assert(r == IO_STREAM_OKAY);
  4473. /* Check if buf starts with SPAWN_ERROR_MESSAGE */
  4474. if (strcmpstart(buf, SPAWN_ERROR_MESSAGE) == 0) {
  4475. log_portfw_spawn_error_message(buf, executable, child_status);
  4476. } else {
  4477. log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", buf);
  4478. }
  4479. }
  4480. /* We should never get here */
  4481. return -1;
  4482. }
  4483. #endif
  4484. /** Reads from <b>stream</b> and stores input in <b>buf_out</b> making
  4485. * sure it's below <b>count</b> bytes.
  4486. * If the string has a trailing newline, we strip it off.
  4487. *
  4488. * This function is specifically created to handle input from managed
  4489. * proxies, according to the pluggable transports spec. Make sure it
  4490. * fits your needs before using it.
  4491. *
  4492. * Returns:
  4493. * IO_STREAM_CLOSED: If the stream is closed.
  4494. * IO_STREAM_EAGAIN: If there is nothing to read and we should check back
  4495. * later.
  4496. * IO_STREAM_TERM: If something is wrong with the stream.
  4497. * IO_STREAM_OKAY: If everything went okay and we got a string
  4498. * in <b>buf_out</b>. */
  4499. enum stream_status
  4500. get_string_from_pipe(FILE *stream, char *buf_out, size_t count)
  4501. {
  4502. char *retval;
  4503. size_t len;
  4504. tor_assert(count <= INT_MAX);
  4505. retval = fgets(buf_out, (int)count, stream);
  4506. if (!retval) {
  4507. if (feof(stream)) {
  4508. /* Program has closed stream (probably it exited) */
  4509. /* TODO: check error */
  4510. return IO_STREAM_CLOSED;
  4511. } else {
  4512. if (EAGAIN == errno) {
  4513. /* Nothing more to read, try again next time */
  4514. return IO_STREAM_EAGAIN;
  4515. } else {
  4516. /* There was a problem, abandon this child process */
  4517. return IO_STREAM_TERM;
  4518. }
  4519. }
  4520. } else {
  4521. len = strlen(buf_out);
  4522. if (len == 0) {
  4523. /* this probably means we got a NUL at the start of the string. */
  4524. return IO_STREAM_EAGAIN;
  4525. }
  4526. if (buf_out[len - 1] == '\n') {
  4527. /* Remove the trailing newline */
  4528. buf_out[len - 1] = '\0';
  4529. } else {
  4530. /* No newline; check whether we overflowed the buffer */
  4531. if (!feof(stream))
  4532. log_info(LD_GENERAL,
  4533. "Line from stream was truncated: %s", buf_out);
  4534. /* TODO: What to do with this error? */
  4535. }
  4536. return IO_STREAM_OKAY;
  4537. }
  4538. /* We should never get here */
  4539. return IO_STREAM_TERM;
  4540. }
  4541. /** Parse a <b>line</b> from tor-fw-helper and issue an appropriate
  4542. * log message to our user. */
  4543. static void
  4544. handle_fw_helper_line(const char *executable, const char *line)
  4545. {
  4546. smartlist_t *tokens = smartlist_new();
  4547. char *message = NULL;
  4548. char *message_for_log = NULL;
  4549. const char *external_port = NULL;
  4550. const char *internal_port = NULL;
  4551. const char *result = NULL;
  4552. int port = 0;
  4553. int success = 0;
  4554. if (strcmpstart(line, SPAWN_ERROR_MESSAGE) == 0) {
  4555. /* We need to check for SPAWN_ERROR_MESSAGE again here, since it's
  4556. * possible that it got sent after we tried to read it in log_from_pipe.
  4557. *
  4558. * XXX Ideally, we should be using one of stdout/stderr for the real
  4559. * output, and one for the output of the startup code. We used to do that
  4560. * before cd05f35d2c.
  4561. */
  4562. int child_status;
  4563. log_portfw_spawn_error_message(line, executable, &child_status);
  4564. goto done;
  4565. }
  4566. smartlist_split_string(tokens, line, NULL,
  4567. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  4568. if (smartlist_len(tokens) < 5)
  4569. goto err;
  4570. if (strcmp(smartlist_get(tokens, 0), "tor-fw-helper") ||
  4571. strcmp(smartlist_get(tokens, 1), "tcp-forward"))
  4572. goto err;
  4573. external_port = smartlist_get(tokens, 2);
  4574. internal_port = smartlist_get(tokens, 3);
  4575. result = smartlist_get(tokens, 4);
  4576. if (smartlist_len(tokens) > 5) {
  4577. /* If there are more than 5 tokens, they are part of [<message>].
  4578. Let's use a second smartlist to form the whole message;
  4579. strncat loops suck. */
  4580. int i;
  4581. int message_words_n = smartlist_len(tokens) - 5;
  4582. smartlist_t *message_sl = smartlist_new();
  4583. for (i = 0; i < message_words_n; i++)
  4584. smartlist_add(message_sl, smartlist_get(tokens, 5+i));
  4585. tor_assert(smartlist_len(message_sl) > 0);
  4586. message = smartlist_join_strings(message_sl, " ", 0, NULL);
  4587. /* wrap the message in log-friendly wrapping */
  4588. tor_asprintf(&message_for_log, " ('%s')", message);
  4589. smartlist_free(message_sl);
  4590. }
  4591. port = atoi(external_port);
  4592. if (port < 1 || port > 65535)
  4593. goto err;
  4594. port = atoi(internal_port);
  4595. if (port < 1 || port > 65535)
  4596. goto err;
  4597. if (!strcmp(result, "SUCCESS"))
  4598. success = 1;
  4599. else if (!strcmp(result, "FAIL"))
  4600. success = 0;
  4601. else
  4602. goto err;
  4603. if (!success) {
  4604. log_warn(LD_GENERAL, "Tor was unable to forward TCP port '%s' to '%s'%s. "
  4605. "Please make sure that your router supports port "
  4606. "forwarding protocols (like NAT-PMP). Note that if '%s' is "
  4607. "your ORPort, your relay will be unable to receive inbound "
  4608. "traffic.", external_port, internal_port,
  4609. message_for_log ? message_for_log : "",
  4610. internal_port);
  4611. } else {
  4612. log_info(LD_GENERAL,
  4613. "Tor successfully forwarded TCP port '%s' to '%s'%s.",
  4614. external_port, internal_port,
  4615. message_for_log ? message_for_log : "");
  4616. }
  4617. goto done;
  4618. err:
  4619. log_warn(LD_GENERAL, "tor-fw-helper sent us a string we could not "
  4620. "parse (%s).", line);
  4621. done:
  4622. SMARTLIST_FOREACH(tokens, char *, cp, tor_free(cp));
  4623. smartlist_free(tokens);
  4624. tor_free(message);
  4625. tor_free(message_for_log);
  4626. }
  4627. /** Read what tor-fw-helper has to say in its stdout and handle it
  4628. * appropriately */
  4629. static int
  4630. handle_fw_helper_output(const char *executable,
  4631. process_handle_t *process_handle)
  4632. {
  4633. smartlist_t *fw_helper_output = NULL;
  4634. enum stream_status stream_status = 0;
  4635. fw_helper_output =
  4636. tor_get_lines_from_handle(tor_process_get_stdout_pipe(process_handle),
  4637. &stream_status);
  4638. if (!fw_helper_output) { /* didn't get any output from tor-fw-helper */
  4639. /* if EAGAIN we should retry in the future */
  4640. return (stream_status == IO_STREAM_EAGAIN) ? 0 : -1;
  4641. }
  4642. /* Handle the lines we got: */
  4643. SMARTLIST_FOREACH_BEGIN(fw_helper_output, char *, line) {
  4644. handle_fw_helper_line(executable, line);
  4645. tor_free(line);
  4646. } SMARTLIST_FOREACH_END(line);
  4647. smartlist_free(fw_helper_output);
  4648. return 0;
  4649. }
  4650. /** Spawn tor-fw-helper and ask it to forward the ports in
  4651. * <b>ports_to_forward</b>. <b>ports_to_forward</b> contains strings
  4652. * of the form "<external port>:<internal port>", which is the format
  4653. * that tor-fw-helper expects. */
  4654. void
  4655. tor_check_port_forwarding(const char *filename,
  4656. smartlist_t *ports_to_forward,
  4657. time_t now)
  4658. {
  4659. /* When fw-helper succeeds, how long do we wait until running it again */
  4660. #define TIME_TO_EXEC_FWHELPER_SUCCESS 300
  4661. /* When fw-helper failed to start, how long do we wait until running it again
  4662. */
  4663. #define TIME_TO_EXEC_FWHELPER_FAIL 60
  4664. /* Static variables are initialized to zero, so child_handle.status=0
  4665. * which corresponds to it not running on startup */
  4666. static process_handle_t *child_handle=NULL;
  4667. static time_t time_to_run_helper = 0;
  4668. int stderr_status, retval;
  4669. int stdout_status = 0;
  4670. tor_assert(filename);
  4671. /* Start the child, if it is not already running */
  4672. if ((!child_handle || child_handle->status != PROCESS_STATUS_RUNNING) &&
  4673. time_to_run_helper < now) {
  4674. /*tor-fw-helper cli looks like this: tor_fw_helper -p :5555 -p 4555:1111 */
  4675. const char **argv; /* cli arguments */
  4676. int args_n, status;
  4677. int argv_index = 0; /* index inside 'argv' */
  4678. tor_assert(smartlist_len(ports_to_forward) > 0);
  4679. /* check for overflow during 'argv' allocation:
  4680. (len(ports_to_forward)*2 + 2)*sizeof(char*) > SIZE_MAX ==
  4681. len(ports_to_forward) > (((SIZE_MAX/sizeof(char*)) - 2)/2) */
  4682. if ((size_t) smartlist_len(ports_to_forward) >
  4683. (((SIZE_MAX/sizeof(char*)) - 2)/2)) {
  4684. log_warn(LD_GENERAL,
  4685. "Overflow during argv allocation. This shouldn't happen.");
  4686. return;
  4687. }
  4688. /* check for overflow during 'argv_index' increase:
  4689. ((len(ports_to_forward)*2 + 2) > INT_MAX) ==
  4690. len(ports_to_forward) > (INT_MAX - 2)/2 */
  4691. if (smartlist_len(ports_to_forward) > (INT_MAX - 2)/2) {
  4692. log_warn(LD_GENERAL,
  4693. "Overflow during argv_index increase. This shouldn't happen.");
  4694. return;
  4695. }
  4696. /* Calculate number of cli arguments: one for the filename, two
  4697. for each smartlist element (one for "-p" and one for the
  4698. ports), and one for the final NULL. */
  4699. args_n = 1 + 2*smartlist_len(ports_to_forward) + 1;
  4700. argv = tor_calloc(args_n, sizeof(char *));
  4701. argv[argv_index++] = filename;
  4702. SMARTLIST_FOREACH_BEGIN(ports_to_forward, const char *, port) {
  4703. argv[argv_index++] = "-p";
  4704. argv[argv_index++] = port;
  4705. } SMARTLIST_FOREACH_END(port);
  4706. argv[argv_index] = NULL;
  4707. /* Assume tor-fw-helper will succeed, start it later*/
  4708. time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_SUCCESS;
  4709. if (child_handle) {
  4710. tor_process_handle_destroy(child_handle, 1);
  4711. child_handle = NULL;
  4712. }
  4713. #ifdef _WIN32
  4714. /* Passing NULL as lpApplicationName makes Windows search for the .exe */
  4715. status = tor_spawn_background(NULL, argv, NULL, &child_handle);
  4716. #else
  4717. status = tor_spawn_background(filename, argv, NULL, &child_handle);
  4718. #endif
  4719. tor_free_((void*)argv);
  4720. argv=NULL;
  4721. if (PROCESS_STATUS_ERROR == status) {
  4722. log_warn(LD_GENERAL, "Failed to start port forwarding helper %s",
  4723. filename);
  4724. time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_FAIL;
  4725. return;
  4726. }
  4727. log_info(LD_GENERAL,
  4728. "Started port forwarding helper (%s) with pid '%d'",
  4729. filename, tor_process_get_pid(child_handle));
  4730. }
  4731. /* If child is running, read from its stdout and stderr) */
  4732. if (child_handle && PROCESS_STATUS_RUNNING == child_handle->status) {
  4733. /* Read from stdout/stderr and log result */
  4734. retval = 0;
  4735. #ifdef _WIN32
  4736. stderr_status = log_from_handle(child_handle->stderr_pipe, LOG_INFO);
  4737. #else
  4738. stderr_status = log_from_pipe(child_handle->stderr_handle,
  4739. LOG_INFO, filename, &retval);
  4740. #endif
  4741. if (handle_fw_helper_output(filename, child_handle) < 0) {
  4742. log_warn(LD_GENERAL, "Failed to handle fw helper output.");
  4743. stdout_status = -1;
  4744. retval = -1;
  4745. }
  4746. if (retval) {
  4747. /* There was a problem in the child process */
  4748. time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_FAIL;
  4749. }
  4750. /* Combine the two statuses in order of severity */
  4751. if (-1 == stdout_status || -1 == stderr_status)
  4752. /* There was a failure */
  4753. retval = -1;
  4754. #ifdef _WIN32
  4755. else if (!child_handle || tor_get_exit_code(child_handle, 0, NULL) !=
  4756. PROCESS_EXIT_RUNNING) {
  4757. /* process has exited or there was an error */
  4758. /* TODO: Do something with the process return value */
  4759. /* TODO: What if the process output something since
  4760. * between log_from_handle and tor_get_exit_code? */
  4761. retval = 1;
  4762. }
  4763. #else
  4764. else if (1 == stdout_status || 1 == stderr_status)
  4765. /* stdout or stderr was closed, the process probably
  4766. * exited. It will be reaped by waitpid() in main.c */
  4767. /* TODO: Do something with the process return value */
  4768. retval = 1;
  4769. #endif
  4770. else
  4771. /* Both are fine */
  4772. retval = 0;
  4773. /* If either pipe indicates a failure, act on it */
  4774. if (0 != retval) {
  4775. if (1 == retval) {
  4776. log_info(LD_GENERAL, "Port forwarding helper terminated");
  4777. child_handle->status = PROCESS_STATUS_NOTRUNNING;
  4778. } else {
  4779. log_warn(LD_GENERAL, "Failed to read from port forwarding helper");
  4780. child_handle->status = PROCESS_STATUS_ERROR;
  4781. }
  4782. /* TODO: The child might not actually be finished (maybe it failed or
  4783. closed stdout/stderr), so maybe we shouldn't start another? */
  4784. }
  4785. }
  4786. }
  4787. /** Initialize the insecure RNG <b>rng</b> from a seed value <b>seed</b>. */
  4788. void
  4789. tor_init_weak_random(tor_weak_rng_t *rng, unsigned seed)
  4790. {
  4791. rng->state = (uint32_t)(seed & 0x7fffffff);
  4792. }
  4793. /** Return a randomly chosen value in the range 0..TOR_WEAK_RANDOM_MAX based
  4794. * on the RNG state of <b>rng</b>. This entropy will not be cryptographically
  4795. * strong; do not rely on it for anything an adversary should not be able to
  4796. * predict. */
  4797. int32_t
  4798. tor_weak_random(tor_weak_rng_t *rng)
  4799. {
  4800. /* Here's a linear congruential generator. OpenBSD and glibc use these
  4801. * parameters; they aren't too bad, and should have maximal period over the
  4802. * range 0..INT32_MAX. We don't want to use the platform rand() or random(),
  4803. * since some platforms have bad weak RNGs that only return values in the
  4804. * range 0..INT16_MAX, which just isn't enough. */
  4805. rng->state = (rng->state * 1103515245 + 12345) & 0x7fffffff;
  4806. return (int32_t) rng->state;
  4807. }
  4808. /** Return a random number in the range [0 , <b>top</b>). {That is, the range
  4809. * of integers i such that 0 <= i < top.} Chooses uniformly. Requires that
  4810. * top is greater than 0. This randomness is not cryptographically strong; do
  4811. * not rely on it for anything an adversary should not be able to predict. */
  4812. int32_t
  4813. tor_weak_random_range(tor_weak_rng_t *rng, int32_t top)
  4814. {
  4815. /* We don't want to just do tor_weak_random() % top, since random() is often
  4816. * implemented with an LCG whose modulus is a power of 2, and those are
  4817. * cyclic in their low-order bits. */
  4818. int divisor, result;
  4819. tor_assert(top > 0);
  4820. divisor = TOR_WEAK_RANDOM_MAX / top;
  4821. do {
  4822. result = (int32_t)(tor_weak_random(rng) / divisor);
  4823. } while (result >= top);
  4824. return result;
  4825. }