util.c 152 KB

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