acme.sh 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246
  1. #!/usr/bin/env sh
  2. VER=2.8.0
  3. PROJECT_NAME="acme.sh"
  4. PROJECT_ENTRY="acme.sh"
  5. PROJECT="https://github.com/Neilpang/$PROJECT_NAME"
  6. DEFAULT_INSTALL_HOME="$HOME/.$PROJECT_NAME"
  7. _SCRIPT_="$0"
  8. _SUB_FOLDERS="dnsapi deploy"
  9. LETSENCRYPT_CA_V1="https://acme-v01.api.letsencrypt.org/directory"
  10. LETSENCRYPT_STAGING_CA_V1="https://acme-staging.api.letsencrypt.org/directory"
  11. LETSENCRYPT_CA_V2="https://acme-v02.api.letsencrypt.org/directory"
  12. LETSENCRYPT_STAGING_CA_V2="https://acme-staging-v02.api.letsencrypt.org/directory"
  13. DEFAULT_CA=$LETSENCRYPT_CA_V1
  14. DEFAULT_STAGING_CA=$LETSENCRYPT_STAGING_CA_V1
  15. DEFAULT_USER_AGENT="$PROJECT_NAME/$VER ($PROJECT)"
  16. DEFAULT_ACCOUNT_EMAIL=""
  17. DEFAULT_ACCOUNT_KEY_LENGTH=2048
  18. DEFAULT_DOMAIN_KEY_LENGTH=2048
  19. DEFAULT_OPENSSL_BIN="openssl"
  20. _OLD_CA_HOST="https://acme-v01.api.letsencrypt.org"
  21. _OLD_STAGE_CA_HOST="https://acme-staging.api.letsencrypt.org"
  22. VTYPE_HTTP="http-01"
  23. VTYPE_DNS="dns-01"
  24. VTYPE_TLS="tls-sni-01"
  25. VTYPE_TLS2="tls-sni-02"
  26. LOCAL_ANY_ADDRESS="0.0.0.0"
  27. MAX_RENEW=60
  28. DEFAULT_DNS_SLEEP=120
  29. NO_VALUE="no"
  30. W_TLS="tls"
  31. W_DNS="dns"
  32. DNS_ALIAS_PREFIX="="
  33. MODE_STATELESS="stateless"
  34. STATE_VERIFIED="verified_ok"
  35. NGINX="nginx:"
  36. NGINX_START="#ACME_NGINX_START"
  37. NGINX_END="#ACME_NGINX_END"
  38. BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----"
  39. END_CSR="-----END CERTIFICATE REQUEST-----"
  40. BEGIN_CERT="-----BEGIN CERTIFICATE-----"
  41. END_CERT="-----END CERTIFICATE-----"
  42. CONTENT_TYPE_JSON="application/jose+json"
  43. RENEW_SKIP=2
  44. ECC_SEP="_"
  45. ECC_SUFFIX="${ECC_SEP}ecc"
  46. LOG_LEVEL_1=1
  47. LOG_LEVEL_2=2
  48. LOG_LEVEL_3=3
  49. DEFAULT_LOG_LEVEL="$LOG_LEVEL_1"
  50. DEBUG_LEVEL_1=1
  51. DEBUG_LEVEL_2=2
  52. DEBUG_LEVEL_3=3
  53. DEBUG_LEVEL_DEFAULT=$DEBUG_LEVEL_1
  54. DEBUG_LEVEL_NONE=0
  55. HIDDEN_VALUE="[hidden](please add '--output-insecure' to see this value)"
  56. SYSLOG_ERROR="user.error"
  57. SYSLOG_INFO="user.info"
  58. SYSLOG_DEBUG="user.debug"
  59. #error
  60. SYSLOG_LEVEL_ERROR=3
  61. #info
  62. SYSLOG_LEVEL_INFO=6
  63. #debug
  64. SYSLOG_LEVEL_DEBUG=7
  65. #debug2
  66. SYSLOG_LEVEL_DEBUG_2=8
  67. #debug3
  68. SYSLOG_LEVEL_DEBUG_3=9
  69. SYSLOG_LEVEL_DEFAULT=$SYSLOG_LEVEL_ERROR
  70. #none
  71. SYSLOG_LEVEL_NONE=0
  72. _DEBUG_WIKI="https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh"
  73. _PREPARE_LINK="https://github.com/Neilpang/acme.sh/wiki/Install-preparations"
  74. _STATELESS_WIKI="https://github.com/Neilpang/acme.sh/wiki/Stateless-Mode"
  75. _DNS_ALIAS_WIKI="https://github.com/Neilpang/acme.sh/wiki/DNS-alias-mode"
  76. _DNS_MANUAL_WIKI="https://github.com/Neilpang/acme.sh/wiki/dns-manual-mode"
  77. _DNS_MANUAL_ERR="The dns manual mode can not renew automatically, you must issue it again manually. You'd better use the other modes instead."
  78. _DNS_MANUAL_WARN="It seems that you are using dns manual mode. please take care: $_DNS_MANUAL_ERR"
  79. _DNS_MANUAL_ERROR="It seems that you are using dns manual mode. Read this link first: $_DNS_MANUAL_WIKI"
  80. __INTERACTIVE=""
  81. if [ -t 1 ]; then
  82. __INTERACTIVE="1"
  83. fi
  84. __green() {
  85. if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" -o "${ACME_FORCE_COLOR}" = "1" ]; then
  86. printf '\033[1;31;32m'
  87. fi
  88. printf -- "%b" "$1"
  89. if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" -o "${ACME_FORCE_COLOR}" = "1" ]; then
  90. printf '\033[0m'
  91. fi
  92. }
  93. __red() {
  94. if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" -o "${ACME_FORCE_COLOR}" = "1" ]; then
  95. printf '\033[1;31;40m'
  96. fi
  97. printf -- "%b" "$1"
  98. if [ "$__INTERACTIVE${ACME_NO_COLOR}" = "1" -o "${ACME_FORCE_COLOR}" = "1" ]; then
  99. printf '\033[0m'
  100. fi
  101. }
  102. _printargs() {
  103. if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then
  104. printf -- "%s" "[$(date)] "
  105. fi
  106. if [ -z "$2" ]; then
  107. printf -- "%s" "$1"
  108. else
  109. printf -- "%s" "$1='$2'"
  110. fi
  111. printf "\n"
  112. }
  113. _dlg_versions() {
  114. echo "Diagnosis versions: "
  115. echo "openssl:$ACME_OPENSSL_BIN"
  116. if _exists "${ACME_OPENSSL_BIN:-openssl}"; then
  117. ${ACME_OPENSSL_BIN:-openssl} version 2>&1
  118. else
  119. echo "$ACME_OPENSSL_BIN doesn't exists."
  120. fi
  121. echo "apache:"
  122. if [ "$_APACHECTL" ] && _exists "$_APACHECTL"; then
  123. $_APACHECTL -V 2>&1
  124. else
  125. echo "apache doesn't exists."
  126. fi
  127. echo "nginx:"
  128. if _exists "nginx"; then
  129. nginx -V 2>&1
  130. else
  131. echo "nginx doesn't exists."
  132. fi
  133. echo "socat:"
  134. if _exists "socat"; then
  135. socat -h 2>&1
  136. else
  137. _debug "socat doesn't exists."
  138. fi
  139. }
  140. #class
  141. _syslog() {
  142. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" = "$SYSLOG_LEVEL_NONE" ]; then
  143. return
  144. fi
  145. _logclass="$1"
  146. shift
  147. if [ -z "$__logger_i" ]; then
  148. if _contains "$(logger --help 2>&1)" "-i"; then
  149. __logger_i="logger -i"
  150. else
  151. __logger_i="logger"
  152. fi
  153. fi
  154. $__logger_i -t "$PROJECT_NAME" -p "$_logclass" "$(_printargs "$@")" >/dev/null 2>&1
  155. }
  156. _log() {
  157. [ -z "$LOG_FILE" ] && return
  158. _printargs "$@" >>"$LOG_FILE"
  159. }
  160. _info() {
  161. _log "$@"
  162. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_INFO" ]; then
  163. _syslog "$SYSLOG_INFO" "$@"
  164. fi
  165. _printargs "$@"
  166. }
  167. _err() {
  168. _syslog "$SYSLOG_ERROR" "$@"
  169. _log "$@"
  170. if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then
  171. printf -- "%s" "[$(date)] " >&2
  172. fi
  173. if [ -z "$2" ]; then
  174. __red "$1" >&2
  175. else
  176. __red "$1='$2'" >&2
  177. fi
  178. printf "\n" >&2
  179. return 1
  180. }
  181. _usage() {
  182. __red "$@" >&2
  183. printf "\n" >&2
  184. }
  185. _debug() {
  186. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
  187. _log "$@"
  188. fi
  189. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
  190. _syslog "$SYSLOG_DEBUG" "$@"
  191. fi
  192. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
  193. _printargs "$@" >&2
  194. fi
  195. }
  196. #output the sensitive messages
  197. _secure_debug() {
  198. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
  199. if [ "$OUTPUT_INSECURE" = "1" ]; then
  200. _log "$@"
  201. else
  202. _log "$1" "$HIDDEN_VALUE"
  203. fi
  204. fi
  205. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
  206. _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
  207. fi
  208. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
  209. if [ "$OUTPUT_INSECURE" = "1" ]; then
  210. _printargs "$@" >&2
  211. else
  212. _printargs "$1" "$HIDDEN_VALUE" >&2
  213. fi
  214. fi
  215. }
  216. _debug2() {
  217. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
  218. _log "$@"
  219. fi
  220. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
  221. _syslog "$SYSLOG_DEBUG" "$@"
  222. fi
  223. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
  224. _printargs "$@" >&2
  225. fi
  226. }
  227. _secure_debug2() {
  228. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
  229. if [ "$OUTPUT_INSECURE" = "1" ]; then
  230. _log "$@"
  231. else
  232. _log "$1" "$HIDDEN_VALUE"
  233. fi
  234. fi
  235. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
  236. _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
  237. fi
  238. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
  239. if [ "$OUTPUT_INSECURE" = "1" ]; then
  240. _printargs "$@" >&2
  241. else
  242. _printargs "$1" "$HIDDEN_VALUE" >&2
  243. fi
  244. fi
  245. }
  246. _debug3() {
  247. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
  248. _log "$@"
  249. fi
  250. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
  251. _syslog "$SYSLOG_DEBUG" "$@"
  252. fi
  253. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
  254. _printargs "$@" >&2
  255. fi
  256. }
  257. _secure_debug3() {
  258. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
  259. if [ "$OUTPUT_INSECURE" = "1" ]; then
  260. _log "$@"
  261. else
  262. _log "$1" "$HIDDEN_VALUE"
  263. fi
  264. fi
  265. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
  266. _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
  267. fi
  268. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
  269. if [ "$OUTPUT_INSECURE" = "1" ]; then
  270. _printargs "$@" >&2
  271. else
  272. _printargs "$1" "$HIDDEN_VALUE" >&2
  273. fi
  274. fi
  275. }
  276. _upper_case() {
  277. # shellcheck disable=SC2018,SC2019
  278. tr 'a-z' 'A-Z'
  279. }
  280. _lower_case() {
  281. # shellcheck disable=SC2018,SC2019
  282. tr 'A-Z' 'a-z'
  283. }
  284. _startswith() {
  285. _str="$1"
  286. _sub="$2"
  287. echo "$_str" | grep "^$_sub" >/dev/null 2>&1
  288. }
  289. _endswith() {
  290. _str="$1"
  291. _sub="$2"
  292. echo "$_str" | grep -- "$_sub\$" >/dev/null 2>&1
  293. }
  294. _contains() {
  295. _str="$1"
  296. _sub="$2"
  297. echo "$_str" | grep -- "$_sub" >/dev/null 2>&1
  298. }
  299. _hasfield() {
  300. _str="$1"
  301. _field="$2"
  302. _sep="$3"
  303. if [ -z "$_field" ]; then
  304. _usage "Usage: str field [sep]"
  305. return 1
  306. fi
  307. if [ -z "$_sep" ]; then
  308. _sep=","
  309. fi
  310. for f in $(echo "$_str" | tr "$_sep" ' '); do
  311. if [ "$f" = "$_field" ]; then
  312. _debug2 "'$_str' contains '$_field'"
  313. return 0 #contains ok
  314. fi
  315. done
  316. _debug2 "'$_str' does not contain '$_field'"
  317. return 1 #not contains
  318. }
  319. # str index [sep]
  320. _getfield() {
  321. _str="$1"
  322. _findex="$2"
  323. _sep="$3"
  324. if [ -z "$_findex" ]; then
  325. _usage "Usage: str field [sep]"
  326. return 1
  327. fi
  328. if [ -z "$_sep" ]; then
  329. _sep=","
  330. fi
  331. _ffi="$_findex"
  332. while [ "$_ffi" -gt "0" ]; do
  333. _fv="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
  334. if [ "$_fv" ]; then
  335. printf -- "%s" "$_fv"
  336. return 0
  337. fi
  338. _ffi="$(_math "$_ffi" - 1)"
  339. done
  340. printf -- "%s" "$_str"
  341. }
  342. _exists() {
  343. cmd="$1"
  344. if [ -z "$cmd" ]; then
  345. _usage "Usage: _exists cmd"
  346. return 1
  347. fi
  348. if eval type type >/dev/null 2>&1; then
  349. eval type "$cmd" >/dev/null 2>&1
  350. elif command >/dev/null 2>&1; then
  351. command -v "$cmd" >/dev/null 2>&1
  352. else
  353. which "$cmd" >/dev/null 2>&1
  354. fi
  355. ret="$?"
  356. _debug3 "$cmd exists=$ret"
  357. return $ret
  358. }
  359. #a + b
  360. _math() {
  361. _m_opts="$@"
  362. printf "%s" "$(($_m_opts))"
  363. }
  364. _h_char_2_dec() {
  365. _ch=$1
  366. case "${_ch}" in
  367. a | A)
  368. printf "10"
  369. ;;
  370. b | B)
  371. printf "11"
  372. ;;
  373. c | C)
  374. printf "12"
  375. ;;
  376. d | D)
  377. printf "13"
  378. ;;
  379. e | E)
  380. printf "14"
  381. ;;
  382. f | F)
  383. printf "15"
  384. ;;
  385. *)
  386. printf "%s" "$_ch"
  387. ;;
  388. esac
  389. }
  390. _URGLY_PRINTF=""
  391. if [ "$(printf '\x41')" != 'A' ]; then
  392. _URGLY_PRINTF=1
  393. fi
  394. _ESCAPE_XARGS=""
  395. if _exists xargs && [ "$(printf %s '\\x41' | xargs printf)" = 'A' ]; then
  396. _ESCAPE_XARGS=1
  397. fi
  398. _h2b() {
  399. if _exists xxd && xxd -r -p 2>/dev/null; then
  400. return
  401. fi
  402. hex=$(cat)
  403. ic=""
  404. jc=""
  405. _debug2 _URGLY_PRINTF "$_URGLY_PRINTF"
  406. if [ -z "$_URGLY_PRINTF" ]; then
  407. if [ "$_ESCAPE_XARGS" ] && _exists xargs; then
  408. _debug2 "xargs"
  409. echo "$hex" | _upper_case | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/g' | xargs printf
  410. else
  411. for h in $(echo "$hex" | _upper_case | sed 's/\([0-9A-F]\{2\}\)/ \1/g'); do
  412. if [ -z "$h" ]; then
  413. break
  414. fi
  415. printf "\x$h%s"
  416. done
  417. fi
  418. else
  419. for c in $(echo "$hex" | _upper_case | sed 's/\([0-9A-F]\)/ \1/g'); do
  420. if [ -z "$ic" ]; then
  421. ic=$c
  422. continue
  423. fi
  424. jc=$c
  425. ic="$(_h_char_2_dec "$ic")"
  426. jc="$(_h_char_2_dec "$jc")"
  427. printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s"
  428. ic=""
  429. jc=""
  430. done
  431. fi
  432. }
  433. _is_solaris() {
  434. _contains "${__OS__:=$(uname -a)}" "solaris" || _contains "${__OS__:=$(uname -a)}" "SunOS"
  435. }
  436. #_ascii_hex str
  437. #this can only process ascii chars, should only be used when od command is missing as a backup way.
  438. _ascii_hex() {
  439. _debug2 "Using _ascii_hex"
  440. _str="$1"
  441. _str_len=${#_str}
  442. _h_i=1
  443. while [ "$_h_i" -le "$_str_len" ]; do
  444. _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")"
  445. printf " %02x" "'$_str_c"
  446. _h_i="$(_math "$_h_i" + 1)"
  447. done
  448. }
  449. #stdin output hexstr splited by one space
  450. #input:"abc"
  451. #output: " 61 62 63"
  452. _hex_dump() {
  453. if _exists od; then
  454. od -A n -v -t x1 | tr -s " " | sed 's/ $//' | tr -d "\r\t\n"
  455. elif _exists hexdump; then
  456. _debug3 "using hexdump"
  457. hexdump -v -e '/1 ""' -e '/1 " %02x" ""'
  458. elif _exists xxd; then
  459. _debug3 "using xxd"
  460. xxd -ps -c 20 -i | sed "s/ 0x/ /g" | tr -d ",\n" | tr -s " "
  461. else
  462. _debug3 "using _ascii_hex"
  463. str=$(cat)
  464. _ascii_hex "$str"
  465. fi
  466. }
  467. #url encode, no-preserved chars
  468. #A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
  469. #41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a
  470. #a b c d e f g h i j k l m n o p q r s t u v w x y z
  471. #61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a
  472. #0 1 2 3 4 5 6 7 8 9 - _ . ~
  473. #30 31 32 33 34 35 36 37 38 39 2d 5f 2e 7e
  474. #stdin stdout
  475. _url_encode() {
  476. _hex_str=$(_hex_dump)
  477. _debug3 "_url_encode"
  478. _debug3 "_hex_str" "$_hex_str"
  479. for _hex_code in $_hex_str; do
  480. #upper case
  481. case "${_hex_code}" in
  482. "41")
  483. printf "%s" "A"
  484. ;;
  485. "42")
  486. printf "%s" "B"
  487. ;;
  488. "43")
  489. printf "%s" "C"
  490. ;;
  491. "44")
  492. printf "%s" "D"
  493. ;;
  494. "45")
  495. printf "%s" "E"
  496. ;;
  497. "46")
  498. printf "%s" "F"
  499. ;;
  500. "47")
  501. printf "%s" "G"
  502. ;;
  503. "48")
  504. printf "%s" "H"
  505. ;;
  506. "49")
  507. printf "%s" "I"
  508. ;;
  509. "4a")
  510. printf "%s" "J"
  511. ;;
  512. "4b")
  513. printf "%s" "K"
  514. ;;
  515. "4c")
  516. printf "%s" "L"
  517. ;;
  518. "4d")
  519. printf "%s" "M"
  520. ;;
  521. "4e")
  522. printf "%s" "N"
  523. ;;
  524. "4f")
  525. printf "%s" "O"
  526. ;;
  527. "50")
  528. printf "%s" "P"
  529. ;;
  530. "51")
  531. printf "%s" "Q"
  532. ;;
  533. "52")
  534. printf "%s" "R"
  535. ;;
  536. "53")
  537. printf "%s" "S"
  538. ;;
  539. "54")
  540. printf "%s" "T"
  541. ;;
  542. "55")
  543. printf "%s" "U"
  544. ;;
  545. "56")
  546. printf "%s" "V"
  547. ;;
  548. "57")
  549. printf "%s" "W"
  550. ;;
  551. "58")
  552. printf "%s" "X"
  553. ;;
  554. "59")
  555. printf "%s" "Y"
  556. ;;
  557. "5a")
  558. printf "%s" "Z"
  559. ;;
  560. #lower case
  561. "61")
  562. printf "%s" "a"
  563. ;;
  564. "62")
  565. printf "%s" "b"
  566. ;;
  567. "63")
  568. printf "%s" "c"
  569. ;;
  570. "64")
  571. printf "%s" "d"
  572. ;;
  573. "65")
  574. printf "%s" "e"
  575. ;;
  576. "66")
  577. printf "%s" "f"
  578. ;;
  579. "67")
  580. printf "%s" "g"
  581. ;;
  582. "68")
  583. printf "%s" "h"
  584. ;;
  585. "69")
  586. printf "%s" "i"
  587. ;;
  588. "6a")
  589. printf "%s" "j"
  590. ;;
  591. "6b")
  592. printf "%s" "k"
  593. ;;
  594. "6c")
  595. printf "%s" "l"
  596. ;;
  597. "6d")
  598. printf "%s" "m"
  599. ;;
  600. "6e")
  601. printf "%s" "n"
  602. ;;
  603. "6f")
  604. printf "%s" "o"
  605. ;;
  606. "70")
  607. printf "%s" "p"
  608. ;;
  609. "71")
  610. printf "%s" "q"
  611. ;;
  612. "72")
  613. printf "%s" "r"
  614. ;;
  615. "73")
  616. printf "%s" "s"
  617. ;;
  618. "74")
  619. printf "%s" "t"
  620. ;;
  621. "75")
  622. printf "%s" "u"
  623. ;;
  624. "76")
  625. printf "%s" "v"
  626. ;;
  627. "77")
  628. printf "%s" "w"
  629. ;;
  630. "78")
  631. printf "%s" "x"
  632. ;;
  633. "79")
  634. printf "%s" "y"
  635. ;;
  636. "7a")
  637. printf "%s" "z"
  638. ;;
  639. #numbers
  640. "30")
  641. printf "%s" "0"
  642. ;;
  643. "31")
  644. printf "%s" "1"
  645. ;;
  646. "32")
  647. printf "%s" "2"
  648. ;;
  649. "33")
  650. printf "%s" "3"
  651. ;;
  652. "34")
  653. printf "%s" "4"
  654. ;;
  655. "35")
  656. printf "%s" "5"
  657. ;;
  658. "36")
  659. printf "%s" "6"
  660. ;;
  661. "37")
  662. printf "%s" "7"
  663. ;;
  664. "38")
  665. printf "%s" "8"
  666. ;;
  667. "39")
  668. printf "%s" "9"
  669. ;;
  670. "2d")
  671. printf "%s" "-"
  672. ;;
  673. "5f")
  674. printf "%s" "_"
  675. ;;
  676. "2e")
  677. printf "%s" "."
  678. ;;
  679. "7e")
  680. printf "%s" "~"
  681. ;;
  682. #other hex
  683. *)
  684. printf '%%%s' "$_hex_code"
  685. ;;
  686. esac
  687. done
  688. }
  689. #options file
  690. _sed_i() {
  691. options="$1"
  692. filename="$2"
  693. if [ -z "$filename" ]; then
  694. _usage "Usage:_sed_i options filename"
  695. return 1
  696. fi
  697. _debug2 options "$options"
  698. if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then
  699. _debug "Using sed -i"
  700. sed -i "$options" "$filename"
  701. else
  702. _debug "No -i support in sed"
  703. text="$(cat "$filename")"
  704. echo "$text" | sed "$options" >"$filename"
  705. fi
  706. }
  707. _egrep_o() {
  708. if ! egrep -o "$1" 2>/dev/null; then
  709. sed -n 's/.*\('"$1"'\).*/\1/p'
  710. fi
  711. }
  712. #Usage: file startline endline
  713. _getfile() {
  714. filename="$1"
  715. startline="$2"
  716. endline="$3"
  717. if [ -z "$endline" ]; then
  718. _usage "Usage: file startline endline"
  719. return 1
  720. fi
  721. i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)"
  722. if [ -z "$i" ]; then
  723. _err "Can not find start line: $startline"
  724. return 1
  725. fi
  726. i="$(_math "$i" + 1)"
  727. _debug i "$i"
  728. j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)"
  729. if [ -z "$j" ]; then
  730. _err "Can not find end line: $endline"
  731. return 1
  732. fi
  733. j="$(_math "$j" - 1)"
  734. _debug j "$j"
  735. sed -n "$i,${j}p" "$filename"
  736. }
  737. #Usage: multiline
  738. _base64() {
  739. [ "" ] #urgly
  740. if [ "$1" ]; then
  741. _debug3 "base64 multiline:'$1'"
  742. ${ACME_OPENSSL_BIN:-openssl} base64 -e
  743. else
  744. _debug3 "base64 single line."
  745. ${ACME_OPENSSL_BIN:-openssl} base64 -e | tr -d '\r\n'
  746. fi
  747. }
  748. #Usage: multiline
  749. _dbase64() {
  750. if [ "$1" ]; then
  751. ${ACME_OPENSSL_BIN:-openssl} base64 -d -A
  752. else
  753. ${ACME_OPENSSL_BIN:-openssl} base64 -d
  754. fi
  755. }
  756. #file
  757. _checkcert() {
  758. _cf="$1"
  759. if [ "$DEBUG" ]; then
  760. openssl x509 -noout -text -in "$_cf"
  761. else
  762. openssl x509 -noout -text -in "$_cf" >/dev/null 2>&1
  763. fi
  764. }
  765. #Usage: hashalg [outputhex]
  766. #Output Base64-encoded digest
  767. _digest() {
  768. alg="$1"
  769. if [ -z "$alg" ]; then
  770. _usage "Usage: _digest hashalg"
  771. return 1
  772. fi
  773. outputhex="$2"
  774. if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ] || [ "$alg" = "md5" ]; then
  775. if [ "$outputhex" ]; then
  776. ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' '
  777. else
  778. ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -binary | _base64
  779. fi
  780. else
  781. _err "$alg is not supported yet"
  782. return 1
  783. fi
  784. }
  785. #Usage: hashalg secret_hex [outputhex]
  786. #Output binary hmac
  787. _hmac() {
  788. alg="$1"
  789. secret_hex="$2"
  790. outputhex="$3"
  791. if [ -z "$secret_hex" ]; then
  792. _usage "Usage: _hmac hashalg secret [outputhex]"
  793. return 1
  794. fi
  795. if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ]; then
  796. if [ "$outputhex" ]; then
  797. (${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -mac HMAC -macopt "hexkey:$secret_hex" 2>/dev/null || ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hmac "$(printf "%s" "$secret_hex" | _h2b)") | cut -d = -f 2 | tr -d ' '
  798. else
  799. ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -mac HMAC -macopt "hexkey:$secret_hex" -binary 2>/dev/null || ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hmac "$(printf "%s" "$secret_hex" | _h2b)" -binary
  800. fi
  801. else
  802. _err "$alg is not supported yet"
  803. return 1
  804. fi
  805. }
  806. #Usage: keyfile hashalg
  807. #Output: Base64-encoded signature value
  808. _sign() {
  809. keyfile="$1"
  810. alg="$2"
  811. if [ -z "$alg" ]; then
  812. _usage "Usage: _sign keyfile hashalg"
  813. return 1
  814. fi
  815. _sign_openssl="${ACME_OPENSSL_BIN:-openssl} dgst -sign $keyfile "
  816. if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
  817. $_sign_openssl -$alg | _base64
  818. elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
  819. if ! _signedECText="$($_sign_openssl -sha$__ECC_KEY_LEN | ${ACME_OPENSSL_BIN:-openssl} asn1parse -inform DER)"; then
  820. _err "Sign failed: $_sign_openssl"
  821. _err "Key file: $keyfile"
  822. _err "Key content:$(wc -l <"$keyfile") lines"
  823. return 1
  824. fi
  825. _debug3 "_signedECText" "$_signedECText"
  826. _ec_r="$(echo "$_signedECText" | _head_n 2 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")"
  827. _debug3 "_ec_r" "$_ec_r"
  828. _ec_s="$(echo "$_signedECText" | _head_n 3 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")"
  829. _debug3 "_ec_s" "$_ec_s"
  830. printf "%s" "$_ec_r$_ec_s" | _h2b | _base64
  831. else
  832. _err "Unknown key file format."
  833. return 1
  834. fi
  835. }
  836. #keylength or isEcc flag (empty str => not ecc)
  837. _isEccKey() {
  838. _length="$1"
  839. if [ -z "$_length" ]; then
  840. return 1
  841. fi
  842. [ "$_length" != "1024" ] \
  843. && [ "$_length" != "2048" ] \
  844. && [ "$_length" != "3072" ] \
  845. && [ "$_length" != "4096" ] \
  846. && [ "$_length" != "8192" ]
  847. }
  848. # _createkey 2048|ec-256 file
  849. _createkey() {
  850. length="$1"
  851. f="$2"
  852. _debug2 "_createkey for file:$f"
  853. eccname="$length"
  854. if _startswith "$length" "ec-"; then
  855. length=$(printf "%s" "$length" | cut -d '-' -f 2-100)
  856. if [ "$length" = "256" ]; then
  857. eccname="prime256v1"
  858. fi
  859. if [ "$length" = "384" ]; then
  860. eccname="secp384r1"
  861. fi
  862. if [ "$length" = "521" ]; then
  863. eccname="secp521r1"
  864. fi
  865. fi
  866. if [ -z "$length" ]; then
  867. length=2048
  868. fi
  869. _debug "Use length $length"
  870. if ! touch "$f" >/dev/null 2>&1; then
  871. _f_path="$(dirname "$f")"
  872. _debug _f_path "$_f_path"
  873. if ! mkdir -p "$_f_path"; then
  874. _err "Can not create path: $_f_path"
  875. return 1
  876. fi
  877. fi
  878. if _isEccKey "$length"; then
  879. _debug "Using ec name: $eccname"
  880. ${ACME_OPENSSL_BIN:-openssl} ecparam -name "$eccname" -genkey 2>/dev/null >"$f"
  881. else
  882. _debug "Using RSA: $length"
  883. ${ACME_OPENSSL_BIN:-openssl} genrsa "$length" 2>/dev/null >"$f"
  884. fi
  885. if [ "$?" != "0" ]; then
  886. _err "Create key error."
  887. return 1
  888. fi
  889. }
  890. #domain
  891. _is_idn() {
  892. _is_idn_d="$1"
  893. _debug2 _is_idn_d "$_is_idn_d"
  894. _idn_temp=$(printf "%s" "$_is_idn_d" | tr -d '0-9' | tr -d 'a-z' | tr -d 'A-Z' | tr -d '*.,-')
  895. _debug2 _idn_temp "$_idn_temp"
  896. [ "$_idn_temp" ]
  897. }
  898. #aa.com
  899. #aa.com,bb.com,cc.com
  900. _idn() {
  901. __idn_d="$1"
  902. if ! _is_idn "$__idn_d"; then
  903. printf "%s" "$__idn_d"
  904. return 0
  905. fi
  906. if _exists idn; then
  907. if _contains "$__idn_d" ','; then
  908. _i_first="1"
  909. for f in $(echo "$__idn_d" | tr ',' ' '); do
  910. [ -z "$f" ] && continue
  911. if [ -z "$_i_first" ]; then
  912. printf "%s" ","
  913. else
  914. _i_first=""
  915. fi
  916. idn --quiet "$f" | tr -d "\r\n"
  917. done
  918. else
  919. idn "$__idn_d" | tr -d "\r\n"
  920. fi
  921. else
  922. _err "Please install idn to process IDN names."
  923. fi
  924. }
  925. #_createcsr cn san_list keyfile csrfile conf
  926. _createcsr() {
  927. _debug _createcsr
  928. domain="$1"
  929. domainlist="$2"
  930. csrkey="$3"
  931. csr="$4"
  932. csrconf="$5"
  933. _debug2 domain "$domain"
  934. _debug2 domainlist "$domainlist"
  935. _debug2 csrkey "$csrkey"
  936. _debug2 csr "$csr"
  937. _debug2 csrconf "$csrconf"
  938. printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\nreq_extensions = v3_req\n[ v3_req ]\n\nkeyUsage = nonRepudiation, digitalSignature, keyEncipherment" >"$csrconf"
  939. if [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then
  940. #single domain
  941. _info "Single domain" "$domain"
  942. printf -- "\nsubjectAltName=DNS:$domain" >>"$csrconf"
  943. else
  944. domainlist="$(_idn "$domainlist")"
  945. _debug2 domainlist "$domainlist"
  946. if _contains "$domainlist" ","; then
  947. alt="DNS:$domain,DNS:$(echo "$domainlist" | sed "s/,,/,/g" | sed "s/,/,DNS:/g")"
  948. else
  949. alt="DNS:$domain,DNS:$domainlist"
  950. fi
  951. #multi
  952. _info "Multi domain" "$alt"
  953. printf -- "\nsubjectAltName=$alt" >>"$csrconf"
  954. fi
  955. if [ "$Le_OCSP_Staple" ] || [ "$Le_OCSP_Stable" ]; then
  956. _savedomainconf Le_OCSP_Staple "$Le_OCSP_Staple"
  957. _cleardomainconf Le_OCSP_Stable
  958. printf -- "\nbasicConstraints = CA:FALSE\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >>"$csrconf"
  959. fi
  960. _csr_cn="$(_idn "$domain")"
  961. _debug2 _csr_cn "$_csr_cn"
  962. if _contains "$(uname -a)" "MINGW"; then
  963. ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//CN=$_csr_cn" -config "$csrconf" -out "$csr"
  964. else
  965. ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/CN=$_csr_cn" -config "$csrconf" -out "$csr"
  966. fi
  967. }
  968. #_signcsr key csr conf cert
  969. _signcsr() {
  970. key="$1"
  971. csr="$2"
  972. conf="$3"
  973. cert="$4"
  974. _debug "_signcsr"
  975. _msg="$(${ACME_OPENSSL_BIN:-openssl} x509 -req -days 365 -in "$csr" -signkey "$key" -extensions v3_req -extfile "$conf" -out "$cert" 2>&1)"
  976. _ret="$?"
  977. _debug "$_msg"
  978. return $_ret
  979. }
  980. #_csrfile
  981. _readSubjectFromCSR() {
  982. _csrfile="$1"
  983. if [ -z "$_csrfile" ]; then
  984. _usage "_readSubjectFromCSR mycsr.csr"
  985. return 1
  986. fi
  987. ${ACME_OPENSSL_BIN:-openssl} req -noout -in "$_csrfile" -subject | tr ',' "\n" | _egrep_o "CN *=.*" | cut -d = -f 2 | cut -d / -f 1 | tr -d ' \n'
  988. }
  989. #_csrfile
  990. #echo comma separated domain list
  991. _readSubjectAltNamesFromCSR() {
  992. _csrfile="$1"
  993. if [ -z "$_csrfile" ]; then
  994. _usage "_readSubjectAltNamesFromCSR mycsr.csr"
  995. return 1
  996. fi
  997. _csrsubj="$(_readSubjectFromCSR "$_csrfile")"
  998. _debug _csrsubj "$_csrsubj"
  999. _dnsAltnames="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile" | grep "^ *DNS:.*" | tr -d ' \n')"
  1000. _debug _dnsAltnames "$_dnsAltnames"
  1001. if _contains "$_dnsAltnames," "DNS:$_csrsubj,"; then
  1002. _debug "AltNames contains subject"
  1003. _dnsAltnames="$(printf "%s" "$_dnsAltnames," | sed "s/DNS:$_csrsubj,//g")"
  1004. else
  1005. _debug "AltNames doesn't contain subject"
  1006. fi
  1007. printf "%s" "$_dnsAltnames" | sed "s/DNS://g"
  1008. }
  1009. #_csrfile
  1010. _readKeyLengthFromCSR() {
  1011. _csrfile="$1"
  1012. if [ -z "$_csrfile" ]; then
  1013. _usage "_readKeyLengthFromCSR mycsr.csr"
  1014. return 1
  1015. fi
  1016. _outcsr="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile")"
  1017. _debug2 _outcsr "$_outcsr"
  1018. if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then
  1019. _debug "ECC CSR"
  1020. echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' '
  1021. else
  1022. _debug "RSA CSR"
  1023. _rkl="$(echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1)"
  1024. if [ "$_rkl" ]; then
  1025. echo "$_rkl"
  1026. else
  1027. echo "$_outcsr" | tr "\t" " " | _egrep_o "RSA Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1
  1028. fi
  1029. fi
  1030. }
  1031. _ss() {
  1032. _port="$1"
  1033. if _exists "ss"; then
  1034. _debug "Using: ss"
  1035. ss -ntpl 2>/dev/null | grep ":$_port "
  1036. return 0
  1037. fi
  1038. if _exists "netstat"; then
  1039. _debug "Using: netstat"
  1040. if netstat -h 2>&1 | grep "\-p proto" >/dev/null; then
  1041. #for windows version netstat tool
  1042. netstat -an -p tcp | grep "LISTENING" | grep ":$_port "
  1043. else
  1044. if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then
  1045. netstat -an -p tcp | grep LISTEN | grep ":$_port "
  1046. elif netstat -help 2>&1 | grep -- '-P protocol' >/dev/null; then
  1047. #for solaris
  1048. netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
  1049. elif netstat -help 2>&1 | grep "\-p" >/dev/null; then
  1050. #for full linux
  1051. netstat -ntpl | grep ":$_port "
  1052. else
  1053. #for busybox (embedded linux; no pid support)
  1054. netstat -ntl 2>/dev/null | grep ":$_port "
  1055. fi
  1056. fi
  1057. return 0
  1058. fi
  1059. return 1
  1060. }
  1061. #outfile key cert cacert [password [name [caname]]]
  1062. _toPkcs() {
  1063. _cpfx="$1"
  1064. _ckey="$2"
  1065. _ccert="$3"
  1066. _cca="$4"
  1067. pfxPassword="$5"
  1068. pfxName="$6"
  1069. pfxCaname="$7"
  1070. if [ "$pfxCaname" ]; then
  1071. ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" -caname "$pfxCaname"
  1072. elif [ "$pfxName" ]; then
  1073. ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName"
  1074. elif [ "$pfxPassword" ]; then
  1075. ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword"
  1076. else
  1077. ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca"
  1078. fi
  1079. }
  1080. #domain [password] [isEcc]
  1081. toPkcs() {
  1082. domain="$1"
  1083. pfxPassword="$2"
  1084. if [ -z "$domain" ]; then
  1085. _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
  1086. return 1
  1087. fi
  1088. _isEcc="$3"
  1089. _initpath "$domain" "$_isEcc"
  1090. _toPkcs "$CERT_PFX_PATH" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$pfxPassword"
  1091. if [ "$?" = "0" ]; then
  1092. _info "Success, Pfx is exported to: $CERT_PFX_PATH"
  1093. fi
  1094. }
  1095. #domain [isEcc]
  1096. toPkcs8() {
  1097. domain="$1"
  1098. if [ -z "$domain" ]; then
  1099. _usage "Usage: $PROJECT_ENTRY --toPkcs8 -d domain [--ecc]"
  1100. return 1
  1101. fi
  1102. _isEcc="$2"
  1103. _initpath "$domain" "$_isEcc"
  1104. ${ACME_OPENSSL_BIN:-openssl} pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in "$CERT_KEY_PATH" -out "$CERT_PKCS8_PATH"
  1105. if [ "$?" = "0" ]; then
  1106. _info "Success, $CERT_PKCS8_PATH"
  1107. fi
  1108. }
  1109. #[2048]
  1110. createAccountKey() {
  1111. _info "Creating account key"
  1112. if [ -z "$1" ]; then
  1113. _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048"
  1114. return
  1115. fi
  1116. length=$1
  1117. _create_account_key "$length"
  1118. }
  1119. _create_account_key() {
  1120. length=$1
  1121. if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then
  1122. _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH"
  1123. length="$DEFAULT_ACCOUNT_KEY_LENGTH"
  1124. fi
  1125. _debug length "$length"
  1126. _initpath
  1127. mkdir -p "$CA_DIR"
  1128. if [ -f "$ACCOUNT_KEY_PATH" ]; then
  1129. _info "Account key exists, skip"
  1130. return
  1131. else
  1132. #generate account key
  1133. _createkey "$length" "$ACCOUNT_KEY_PATH"
  1134. chmod 600 "$ACCOUNT_KEY_PATH"
  1135. fi
  1136. }
  1137. #domain [length]
  1138. createDomainKey() {
  1139. _info "Creating domain key"
  1140. if [ -z "$1" ]; then
  1141. _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]"
  1142. return
  1143. fi
  1144. domain=$1
  1145. _cdl=$2
  1146. if [ -z "$_cdl" ]; then
  1147. _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH"
  1148. _cdl="$DEFAULT_DOMAIN_KEY_LENGTH"
  1149. fi
  1150. _initpath "$domain" "$_cdl"
  1151. if [ ! -f "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]) || [ "$Le_ForceNewDomainKey" = "1" ]; then
  1152. if _createkey "$_cdl" "$CERT_KEY_PATH"; then
  1153. _savedomainconf Le_Keylength "$_cdl"
  1154. _info "The domain key is here: $(__green $CERT_KEY_PATH)"
  1155. return 0
  1156. fi
  1157. else
  1158. if [ "$IS_RENEW" ]; then
  1159. _info "Domain key exists, skip"
  1160. return 0
  1161. else
  1162. _err "Domain key exists, do you want to overwrite the key?"
  1163. _err "Add '--force', and try again."
  1164. return 1
  1165. fi
  1166. fi
  1167. }
  1168. # domain domainlist isEcc
  1169. createCSR() {
  1170. _info "Creating csr"
  1171. if [ -z "$1" ]; then
  1172. _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
  1173. return
  1174. fi
  1175. domain="$1"
  1176. domainlist="$2"
  1177. _isEcc="$3"
  1178. _initpath "$domain" "$_isEcc"
  1179. if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
  1180. _info "CSR exists, skip"
  1181. return
  1182. fi
  1183. if [ ! -f "$CERT_KEY_PATH" ]; then
  1184. _err "The key file is not found: $CERT_KEY_PATH"
  1185. _err "Please create the key file first."
  1186. return 1
  1187. fi
  1188. _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
  1189. }
  1190. _url_replace() {
  1191. tr '/+' '_-' | tr -d '= '
  1192. }
  1193. _time2str() {
  1194. #BSD
  1195. if date -u -r "$1" 2>/dev/null; then
  1196. return
  1197. fi
  1198. #Linux
  1199. if date -u -d@"$1" 2>/dev/null; then
  1200. return
  1201. fi
  1202. #Solaris
  1203. if _exists adb; then
  1204. _t_s_a=$(echo "0t${1}=Y" | adb)
  1205. echo "$_t_s_a"
  1206. fi
  1207. #Busybox
  1208. if echo "$1" | awk '{ print strftime("%c", $0); }' 2>/dev/null; then
  1209. return
  1210. fi
  1211. }
  1212. _normalizeJson() {
  1213. sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
  1214. }
  1215. _stat() {
  1216. #Linux
  1217. if stat -c '%U:%G' "$1" 2>/dev/null; then
  1218. return
  1219. fi
  1220. #BSD
  1221. if stat -f '%Su:%Sg' "$1" 2>/dev/null; then
  1222. return
  1223. fi
  1224. return 1 #error, 'stat' not found
  1225. }
  1226. #keyfile
  1227. _calcjwk() {
  1228. keyfile="$1"
  1229. if [ -z "$keyfile" ]; then
  1230. _usage "Usage: _calcjwk keyfile"
  1231. return 1
  1232. fi
  1233. if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then
  1234. _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE"
  1235. return 0
  1236. fi
  1237. if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
  1238. _debug "RSA key"
  1239. pub_exp=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -noout -text | grep "^publicExponent:" | cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1)
  1240. if [ "${#pub_exp}" = "5" ]; then
  1241. pub_exp=0$pub_exp
  1242. fi
  1243. _debug3 pub_exp "$pub_exp"
  1244. e=$(echo "$pub_exp" | _h2b | _base64)
  1245. _debug3 e "$e"
  1246. modulus=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2)
  1247. _debug3 modulus "$modulus"
  1248. n="$(printf "%s" "$modulus" | _h2b | _base64 | _url_replace)"
  1249. _debug3 n "$n"
  1250. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  1251. _debug3 jwk "$jwk"
  1252. JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  1253. JWK_HEADERPLACE_PART1='{"nonce": "'
  1254. JWK_HEADERPLACE_PART2='", "alg": "RS256"'
  1255. elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then
  1256. _debug "EC key"
  1257. crv="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
  1258. _debug3 crv "$crv"
  1259. __ECC_KEY_LEN=$(echo "$crv" | cut -d "-" -f 2)
  1260. if [ "$__ECC_KEY_LEN" = "521" ]; then
  1261. __ECC_KEY_LEN=512
  1262. fi
  1263. _debug3 __ECC_KEY_LEN "$__ECC_KEY_LEN"
  1264. if [ -z "$crv" ]; then
  1265. _debug "Let's try ASN1 OID"
  1266. crv_oid="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^ASN1 OID:" | cut -d ":" -f 2 | tr -d " \r\n")"
  1267. _debug3 crv_oid "$crv_oid"
  1268. case "${crv_oid}" in
  1269. "prime256v1")
  1270. crv="P-256"
  1271. __ECC_KEY_LEN=256
  1272. ;;
  1273. "secp384r1")
  1274. crv="P-384"
  1275. __ECC_KEY_LEN=384
  1276. ;;
  1277. "secp521r1")
  1278. crv="P-521"
  1279. __ECC_KEY_LEN=512
  1280. ;;
  1281. *)
  1282. _err "ECC oid : $crv_oid"
  1283. return 1
  1284. ;;
  1285. esac
  1286. _debug3 crv "$crv"
  1287. fi
  1288. pubi="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)"
  1289. pubi=$(_math "$pubi" + 1)
  1290. _debug3 pubi "$pubi"
  1291. pubj="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)"
  1292. pubj=$(_math "$pubj" - 1)
  1293. _debug3 pubj "$pubj"
  1294. pubtext="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")"
  1295. _debug3 pubtext "$pubtext"
  1296. xlen="$(printf "%s" "$pubtext" | tr -d ':' | wc -c)"
  1297. xlen=$(_math "$xlen" / 4)
  1298. _debug3 xlen "$xlen"
  1299. xend=$(_math "$xlen" + 1)
  1300. x="$(printf "%s" "$pubtext" | cut -d : -f 2-"$xend")"
  1301. _debug3 x "$x"
  1302. x64="$(printf "%s" "$x" | tr -d : | _h2b | _base64 | _url_replace)"
  1303. _debug3 x64 "$x64"
  1304. xend=$(_math "$xend" + 1)
  1305. y="$(printf "%s" "$pubtext" | cut -d : -f "$xend"-10000)"
  1306. _debug3 y "$y"
  1307. y64="$(printf "%s" "$y" | tr -d : | _h2b | _base64 | _url_replace)"
  1308. _debug3 y64 "$y64"
  1309. jwk='{"crv": "'$crv'", "kty": "EC", "x": "'$x64'", "y": "'$y64'"}'
  1310. _debug3 jwk "$jwk"
  1311. JWK_HEADER='{"alg": "ES'$__ECC_KEY_LEN'", "jwk": '$jwk'}'
  1312. JWK_HEADERPLACE_PART1='{"nonce": "'
  1313. JWK_HEADERPLACE_PART2='", "alg": "ES'$__ECC_KEY_LEN'"'
  1314. else
  1315. _err "Only RSA or EC key is supported."
  1316. return 1
  1317. fi
  1318. _debug3 JWK_HEADER "$JWK_HEADER"
  1319. __CACHED_JWK_KEY_FILE="$keyfile"
  1320. }
  1321. _time() {
  1322. date -u "+%s"
  1323. }
  1324. _utc_date() {
  1325. date -u "+%Y-%m-%d %H:%M:%S"
  1326. }
  1327. _mktemp() {
  1328. if _exists mktemp; then
  1329. if mktemp 2>/dev/null; then
  1330. return 0
  1331. elif _contains "$(mktemp 2>&1)" "-t prefix" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then
  1332. #for Mac osx
  1333. return 0
  1334. fi
  1335. fi
  1336. if [ -d "/tmp" ]; then
  1337. echo "/tmp/${PROJECT_NAME}wefADf24sf.$(_time).tmp"
  1338. return 0
  1339. elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then
  1340. echo "/$LE_TEMP_DIR/wefADf24sf.$(_time).tmp"
  1341. return 0
  1342. fi
  1343. _err "Can not create temp file."
  1344. }
  1345. _inithttp() {
  1346. if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then
  1347. HTTP_HEADER="$(_mktemp)"
  1348. _debug2 HTTP_HEADER "$HTTP_HEADER"
  1349. fi
  1350. if [ "$__HTTP_INITIALIZED" ]; then
  1351. if [ "$_ACME_CURL$_ACME_WGET" ]; then
  1352. _debug2 "Http already initialized."
  1353. return 0
  1354. fi
  1355. fi
  1356. if [ -z "$_ACME_CURL" ] && _exists "curl"; then
  1357. _ACME_CURL="curl -L --silent --dump-header $HTTP_HEADER "
  1358. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  1359. _CURL_DUMP="$(_mktemp)"
  1360. _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP "
  1361. fi
  1362. if [ "$CA_PATH" ]; then
  1363. _ACME_CURL="$_ACME_CURL --capath $CA_PATH "
  1364. elif [ "$CA_BUNDLE" ]; then
  1365. _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE "
  1366. fi
  1367. if _contains "$(curl --help 2>&1)" "--globoff"; then
  1368. _ACME_CURL="$_ACME_CURL -g "
  1369. fi
  1370. fi
  1371. if [ -z "$_ACME_WGET" ] && _exists "wget"; then
  1372. _ACME_WGET="wget -q"
  1373. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  1374. _ACME_WGET="$_ACME_WGET -d "
  1375. fi
  1376. if [ "$CA_PATH" ]; then
  1377. _ACME_WGET="$_ACME_WGET --ca-directory=$CA_PATH "
  1378. elif [ "$CA_BUNDLE" ]; then
  1379. _ACME_WGET="$_ACME_WGET --ca-certificate=$CA_BUNDLE "
  1380. fi
  1381. fi
  1382. #from wget 1.14: do not skip body on 404 error
  1383. if [ "$_ACME_WGET" ] && _contains "$($_ACME_WGET --help 2>&1)" "--content-on-error"; then
  1384. _ACME_WGET="$_ACME_WGET --content-on-error "
  1385. fi
  1386. __HTTP_INITIALIZED=1
  1387. }
  1388. # body url [needbase64] [POST|PUT|DELETE] [ContentType]
  1389. _post() {
  1390. body="$1"
  1391. _post_url="$2"
  1392. needbase64="$3"
  1393. httpmethod="$4"
  1394. _postContentType="$5"
  1395. if [ -z "$httpmethod" ]; then
  1396. httpmethod="POST"
  1397. fi
  1398. _debug $httpmethod
  1399. _debug "_post_url" "$_post_url"
  1400. _debug2 "body" "$body"
  1401. _debug2 "_postContentType" "$_postContentType"
  1402. _inithttp
  1403. if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
  1404. _CURL="$_ACME_CURL"
  1405. if [ "$HTTPS_INSECURE" ]; then
  1406. _CURL="$_CURL --insecure "
  1407. fi
  1408. _debug "_CURL" "$_CURL"
  1409. if [ "$needbase64" ]; then
  1410. if [ "$_postContentType" ]; then
  1411. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)"
  1412. else
  1413. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)"
  1414. fi
  1415. else
  1416. if [ "$_postContentType" ]; then
  1417. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
  1418. else
  1419. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
  1420. fi
  1421. fi
  1422. _ret="$?"
  1423. if [ "$_ret" != "0" ]; then
  1424. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
  1425. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  1426. _err "Here is the curl dump log:"
  1427. _err "$(cat "$_CURL_DUMP")"
  1428. fi
  1429. fi
  1430. elif [ "$_ACME_WGET" ]; then
  1431. _WGET="$_ACME_WGET"
  1432. if [ "$HTTPS_INSECURE" ]; then
  1433. _WGET="$_WGET --no-check-certificate "
  1434. fi
  1435. _debug "_WGET" "$_WGET"
  1436. if [ "$needbase64" ]; then
  1437. if [ "$httpmethod" = "POST" ]; then
  1438. if [ "$_postContentType" ]; then
  1439. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)"
  1440. else
  1441. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)"
  1442. fi
  1443. else
  1444. if [ "$_postContentType" ]; then
  1445. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)"
  1446. else
  1447. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)"
  1448. fi
  1449. fi
  1450. else
  1451. if [ "$httpmethod" = "POST" ]; then
  1452. if [ "$_postContentType" ]; then
  1453. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
  1454. else
  1455. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
  1456. fi
  1457. else
  1458. if [ "$_postContentType" ]; then
  1459. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
  1460. else
  1461. response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
  1462. fi
  1463. fi
  1464. fi
  1465. _ret="$?"
  1466. if [ "$_ret" = "8" ]; then
  1467. _ret=0
  1468. _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later."
  1469. fi
  1470. if [ "$_ret" != "0" ]; then
  1471. _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
  1472. fi
  1473. _sed_i "s/^ *//g" "$HTTP_HEADER"
  1474. else
  1475. _ret="$?"
  1476. _err "Neither curl nor wget is found, can not do $httpmethod."
  1477. fi
  1478. _debug "_ret" "$_ret"
  1479. printf "%s" "$response"
  1480. return $_ret
  1481. }
  1482. # url getheader timeout
  1483. _get() {
  1484. _debug GET
  1485. url="$1"
  1486. onlyheader="$2"
  1487. t="$3"
  1488. _debug url "$url"
  1489. _debug "timeout=$t"
  1490. _inithttp
  1491. if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
  1492. _CURL="$_ACME_CURL"
  1493. if [ "$HTTPS_INSECURE" ]; then
  1494. _CURL="$_CURL --insecure "
  1495. fi
  1496. if [ "$t" ]; then
  1497. _CURL="$_CURL --connect-timeout $t"
  1498. fi
  1499. _debug "_CURL" "$_CURL"
  1500. if [ "$onlyheader" ]; then
  1501. $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
  1502. else
  1503. $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
  1504. fi
  1505. ret=$?
  1506. if [ "$ret" != "0" ]; then
  1507. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret"
  1508. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  1509. _err "Here is the curl dump log:"
  1510. _err "$(cat "$_CURL_DUMP")"
  1511. fi
  1512. fi
  1513. elif [ "$_ACME_WGET" ]; then
  1514. _WGET="$_ACME_WGET"
  1515. if [ "$HTTPS_INSECURE" ]; then
  1516. _WGET="$_WGET --no-check-certificate "
  1517. fi
  1518. if [ "$t" ]; then
  1519. _WGET="$_WGET --timeout=$t"
  1520. fi
  1521. _debug "_WGET" "$_WGET"
  1522. if [ "$onlyheader" ]; then
  1523. $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O /dev/null "$url" 2>&1 | sed 's/^[ ]*//g'
  1524. else
  1525. $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url"
  1526. fi
  1527. ret=$?
  1528. if [ "$ret" = "8" ]; then
  1529. ret=0
  1530. _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later."
  1531. fi
  1532. if [ "$ret" != "0" ]; then
  1533. _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $ret"
  1534. fi
  1535. else
  1536. ret=$?
  1537. _err "Neither curl nor wget is found, can not do GET."
  1538. fi
  1539. _debug "ret" "$ret"
  1540. return $ret
  1541. }
  1542. _head_n() {
  1543. head -n "$1"
  1544. }
  1545. _tail_n() {
  1546. if ! tail -n "$1" 2>/dev/null; then
  1547. #fix for solaris
  1548. tail -"$1"
  1549. fi
  1550. }
  1551. # url payload needbase64 keyfile
  1552. _send_signed_request() {
  1553. url=$1
  1554. payload=$2
  1555. needbase64=$3
  1556. keyfile=$4
  1557. if [ -z "$keyfile" ]; then
  1558. keyfile="$ACCOUNT_KEY_PATH"
  1559. fi
  1560. _debug url "$url"
  1561. _debug payload "$payload"
  1562. if ! _calcjwk "$keyfile"; then
  1563. return 1
  1564. fi
  1565. __request_conent_type="$CONTENT_TYPE_JSON"
  1566. payload64=$(printf "%s" "$payload" | _base64 | _url_replace)
  1567. _debug3 payload64 "$payload64"
  1568. MAX_REQUEST_RETRY_TIMES=20
  1569. _sleep_retry_sec=1
  1570. _request_retry_times=0
  1571. while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
  1572. _request_retry_times=$(_math "$_request_retry_times" + 1)
  1573. _debug3 _request_retry_times "$_request_retry_times"
  1574. if [ -z "$_CACHED_NONCE" ]; then
  1575. _headers=""
  1576. if [ "$ACME_NEW_NONCE" ]; then
  1577. _debug2 "Get nonce with HEAD. ACME_NEW_NONCE" "$ACME_NEW_NONCE"
  1578. nonceurl="$ACME_NEW_NONCE"
  1579. if _post "" "$nonceurl" "" "HEAD" "$__request_conent_type"; then
  1580. _headers="$(cat "$HTTP_HEADER")"
  1581. fi
  1582. fi
  1583. if [ -z "$_headers" ]; then
  1584. _debug2 "Get nonce with GET. ACME_DIRECTORY" "$ACME_DIRECTORY"
  1585. nonceurl="$ACME_DIRECTORY"
  1586. _headers="$(_get "$nonceurl" "onlyheader")"
  1587. fi
  1588. if [ "$?" != "0" ]; then
  1589. _err "Can not connect to $nonceurl to get nonce."
  1590. return 1
  1591. fi
  1592. _debug2 _headers "$_headers"
  1593. _CACHED_NONCE="$(echo "$_headers" | grep "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
  1594. _debug2 _CACHED_NONCE "$_CACHED_NONCE"
  1595. else
  1596. _debug2 "Use _CACHED_NONCE" "$_CACHED_NONCE"
  1597. fi
  1598. nonce="$_CACHED_NONCE"
  1599. _debug2 nonce "$nonce"
  1600. if [ -z "$nonce" ]; then
  1601. _info "Could not get nonce, let's try again."
  1602. _sleep 2
  1603. continue
  1604. fi
  1605. if [ "$ACME_VERSION" = "2" ]; then
  1606. if [ "$url" = "$ACME_NEW_ACCOUNT" ] || [ "$url" = "$ACME_REVOKE_CERT" ]; then
  1607. protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
  1608. else
  1609. protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"kid\": \"${ACCOUNT_URL}\""'}'
  1610. fi
  1611. else
  1612. protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
  1613. fi
  1614. _debug3 protected "$protected"
  1615. protected64="$(printf "%s" "$protected" | _base64 | _url_replace)"
  1616. _debug3 protected64 "$protected64"
  1617. if ! _sig_t="$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256")"; then
  1618. _err "Sign request failed."
  1619. return 1
  1620. fi
  1621. _debug3 _sig_t "$_sig_t"
  1622. sig="$(printf "%s" "$_sig_t" | _url_replace)"
  1623. _debug3 sig "$sig"
  1624. if [ "$ACME_VERSION" = "2" ]; then
  1625. body="{\"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  1626. else
  1627. body="{\"header\": $JWK_HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  1628. fi
  1629. _debug3 body "$body"
  1630. response="$(_post "$body" "$url" "$needbase64" "POST" "$__request_conent_type")"
  1631. _CACHED_NONCE=""
  1632. if [ "$?" != "0" ]; then
  1633. _err "Can not post to $url"
  1634. return 1
  1635. fi
  1636. _debug2 original "$response"
  1637. response="$(echo "$response" | _normalizeJson)"
  1638. responseHeaders="$(cat "$HTTP_HEADER")"
  1639. _debug2 responseHeaders "$responseHeaders"
  1640. _debug2 response "$response"
  1641. code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")"
  1642. _debug code "$code"
  1643. _CACHED_NONCE="$(echo "$responseHeaders" | grep "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
  1644. _body="$response"
  1645. if [ "$needbase64" ]; then
  1646. _body="$(echo "$_body" | _dbase64 | tr -d '\0')"
  1647. _debug3 _body "$_body"
  1648. fi
  1649. if _contains "$_body" "JWS has invalid anti-replay nonce" || _contains "$_body" "JWS has an invalid anti-replay nonce"; then
  1650. _info "It seems the CA server is busy now, let's wait and retry. Sleeping $_sleep_retry_sec seconds."
  1651. _CACHED_NONCE=""
  1652. _sleep $_sleep_retry_sec
  1653. continue
  1654. fi
  1655. break
  1656. done
  1657. }
  1658. #setopt "file" "opt" "=" "value" [";"]
  1659. _setopt() {
  1660. __conf="$1"
  1661. __opt="$2"
  1662. __sep="$3"
  1663. __val="$4"
  1664. __end="$5"
  1665. if [ -z "$__opt" ]; then
  1666. _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
  1667. return
  1668. fi
  1669. if [ ! -f "$__conf" ]; then
  1670. touch "$__conf"
  1671. fi
  1672. if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then
  1673. _debug3 OK
  1674. if _contains "$__val" "&"; then
  1675. __val="$(echo "$__val" | sed 's/&/\\&/g')"
  1676. fi
  1677. text="$(cat "$__conf")"
  1678. printf -- "%s\n" "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
  1679. elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then
  1680. if _contains "$__val" "&"; then
  1681. __val="$(echo "$__val" | sed 's/&/\\&/g')"
  1682. fi
  1683. text="$(cat "$__conf")"
  1684. printf -- "%s\n" "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
  1685. else
  1686. _debug3 APP
  1687. echo "$__opt$__sep$__val$__end" >>"$__conf"
  1688. fi
  1689. _debug3 "$(grep -n "^$__opt$__sep" "$__conf")"
  1690. }
  1691. #_save_conf file key value
  1692. #save to conf
  1693. _save_conf() {
  1694. _s_c_f="$1"
  1695. _sdkey="$2"
  1696. _sdvalue="$3"
  1697. if [ "$_s_c_f" ]; then
  1698. _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
  1699. else
  1700. _err "config file is empty, can not save $_sdkey=$_sdvalue"
  1701. fi
  1702. }
  1703. #_clear_conf file key
  1704. _clear_conf() {
  1705. _c_c_f="$1"
  1706. _sdkey="$2"
  1707. if [ "$_c_c_f" ]; then
  1708. _conf_data="$(cat "$_c_c_f")"
  1709. echo "$_conf_data" | sed "s/^$_sdkey *=.*$//" >"$_c_c_f"
  1710. else
  1711. _err "config file is empty, can not clear"
  1712. fi
  1713. }
  1714. #_read_conf file key
  1715. _read_conf() {
  1716. _r_c_f="$1"
  1717. _sdkey="$2"
  1718. if [ -f "$_r_c_f" ]; then
  1719. (
  1720. eval "$(grep "^$_sdkey *=" "$_r_c_f")"
  1721. eval "printf \"%s\" \"\$$_sdkey\""
  1722. )
  1723. else
  1724. _debug "config file is empty, can not read $_sdkey"
  1725. fi
  1726. }
  1727. #_savedomainconf key value
  1728. #save to domain.conf
  1729. _savedomainconf() {
  1730. _save_conf "$DOMAIN_CONF" "$1" "$2"
  1731. }
  1732. #_cleardomainconf key
  1733. _cleardomainconf() {
  1734. _clear_conf "$DOMAIN_CONF" "$1"
  1735. }
  1736. #_readdomainconf key
  1737. _readdomainconf() {
  1738. _read_conf "$DOMAIN_CONF" "$1"
  1739. }
  1740. #_saveaccountconf key value
  1741. _saveaccountconf() {
  1742. _save_conf "$ACCOUNT_CONF_PATH" "$1" "$2"
  1743. }
  1744. #key value
  1745. _saveaccountconf_mutable() {
  1746. _save_conf "$ACCOUNT_CONF_PATH" "SAVED_$1" "$2"
  1747. #remove later
  1748. _clearaccountconf "$1"
  1749. }
  1750. #key
  1751. _readaccountconf() {
  1752. _read_conf "$ACCOUNT_CONF_PATH" "$1"
  1753. }
  1754. #key
  1755. _readaccountconf_mutable() {
  1756. _rac_key="$1"
  1757. _readaccountconf "SAVED_$_rac_key"
  1758. }
  1759. #_clearaccountconf key
  1760. _clearaccountconf() {
  1761. _clear_conf "$ACCOUNT_CONF_PATH" "$1"
  1762. }
  1763. #_savecaconf key value
  1764. _savecaconf() {
  1765. _save_conf "$CA_CONF" "$1" "$2"
  1766. }
  1767. #_readcaconf key
  1768. _readcaconf() {
  1769. _read_conf "$CA_CONF" "$1"
  1770. }
  1771. #_clearaccountconf key
  1772. _clearcaconf() {
  1773. _clear_conf "$CA_CONF" "$1"
  1774. }
  1775. # content localaddress
  1776. _startserver() {
  1777. content="$1"
  1778. ncaddr="$2"
  1779. _debug "ncaddr" "$ncaddr"
  1780. _debug "startserver: $$"
  1781. _debug Le_HTTPPort "$Le_HTTPPort"
  1782. _debug Le_Listen_V4 "$Le_Listen_V4"
  1783. _debug Le_Listen_V6 "$Le_Listen_V6"
  1784. _NC="socat"
  1785. if [ "$Le_Listen_V4" ]; then
  1786. _NC="$_NC -4"
  1787. elif [ "$Le_Listen_V6" ]; then
  1788. _NC="$_NC -6"
  1789. fi
  1790. if [ "$DEBUG" ] && [ "$DEBUG" -gt "1" ]; then
  1791. _NC="$_NC -d -d -v"
  1792. fi
  1793. SOCAT_OPTIONS=TCP-LISTEN:$Le_HTTPPort,crlf,reuseaddr,fork
  1794. #Adding bind to local-address
  1795. if [ "$ncaddr" ]; then
  1796. SOCAT_OPTIONS="$SOCAT_OPTIONS,bind=${ncaddr}"
  1797. fi
  1798. _debug "_NC" "$_NC $SOCAT_OPTIONS"
  1799. $_NC $SOCAT_OPTIONS SYSTEM:"sleep 1; echo HTTP/1.0 200 OK; echo ; echo $content; echo;" &
  1800. serverproc="$!"
  1801. }
  1802. _stopserver() {
  1803. pid="$1"
  1804. _debug "pid" "$pid"
  1805. if [ -z "$pid" ]; then
  1806. return
  1807. fi
  1808. kill $pid
  1809. }
  1810. # sleep sec
  1811. _sleep() {
  1812. _sleep_sec="$1"
  1813. if [ "$__INTERACTIVE" ]; then
  1814. _sleep_c="$_sleep_sec"
  1815. while [ "$_sleep_c" -ge "0" ]; do
  1816. printf "\r \r"
  1817. __green "$_sleep_c"
  1818. _sleep_c="$(_math "$_sleep_c" - 1)"
  1819. sleep 1
  1820. done
  1821. printf "\r"
  1822. else
  1823. sleep "$_sleep_sec"
  1824. fi
  1825. }
  1826. # _starttlsserver san_a san_b port content _ncaddr
  1827. _starttlsserver() {
  1828. _info "Starting tls server."
  1829. san_a="$1"
  1830. san_b="$2"
  1831. port="$3"
  1832. content="$4"
  1833. opaddr="$5"
  1834. _debug san_a "$san_a"
  1835. _debug san_b "$san_b"
  1836. _debug port "$port"
  1837. #create key TLS_KEY
  1838. if ! _createkey "2048" "$TLS_KEY"; then
  1839. _err "Create tls validation key error."
  1840. return 1
  1841. fi
  1842. #create csr
  1843. alt="$san_a"
  1844. if [ "$san_b" ]; then
  1845. alt="$alt,$san_b"
  1846. fi
  1847. if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF"; then
  1848. _err "Create tls validation csr error."
  1849. return 1
  1850. fi
  1851. #self signed
  1852. if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
  1853. _err "Create tls validation cert error."
  1854. return 1
  1855. fi
  1856. __S_OPENSSL="${ACME_OPENSSL_BIN:-openssl} s_server -www -cert $TLS_CERT -key $TLS_KEY "
  1857. if [ "$opaddr" ]; then
  1858. __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port"
  1859. else
  1860. __S_OPENSSL="$__S_OPENSSL -accept $port"
  1861. fi
  1862. _debug Le_Listen_V4 "$Le_Listen_V4"
  1863. _debug Le_Listen_V6 "$Le_Listen_V6"
  1864. if [ "$Le_Listen_V4" ]; then
  1865. __S_OPENSSL="$__S_OPENSSL -4"
  1866. elif [ "$Le_Listen_V6" ]; then
  1867. __S_OPENSSL="$__S_OPENSSL -6"
  1868. fi
  1869. _debug "$__S_OPENSSL"
  1870. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  1871. $__S_OPENSSL -tlsextdebug &
  1872. else
  1873. $__S_OPENSSL >/dev/null 2>&1 &
  1874. fi
  1875. serverproc="$!"
  1876. sleep 1
  1877. _debug serverproc "$serverproc"
  1878. }
  1879. #file
  1880. _readlink() {
  1881. _rf="$1"
  1882. if ! readlink -f "$_rf" 2>/dev/null; then
  1883. if _startswith "$_rf" "/"; then
  1884. echo "$_rf"
  1885. return 0
  1886. fi
  1887. echo "$(pwd)/$_rf" | _conapath
  1888. fi
  1889. }
  1890. _conapath() {
  1891. sed "s#/\./#/#g"
  1892. }
  1893. __initHome() {
  1894. if [ -z "$_SCRIPT_HOME" ]; then
  1895. if _exists readlink && _exists dirname; then
  1896. _debug "Lets find script dir."
  1897. _debug "_SCRIPT_" "$_SCRIPT_"
  1898. _script="$(_readlink "$_SCRIPT_")"
  1899. _debug "_script" "$_script"
  1900. _script_home="$(dirname "$_script")"
  1901. _debug "_script_home" "$_script_home"
  1902. if [ -d "$_script_home" ]; then
  1903. _SCRIPT_HOME="$_script_home"
  1904. else
  1905. _err "It seems the script home is not correct:$_script_home"
  1906. fi
  1907. fi
  1908. fi
  1909. # if [ -z "$LE_WORKING_DIR" ]; then
  1910. # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
  1911. # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
  1912. # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
  1913. # else
  1914. # LE_WORKING_DIR="$_SCRIPT_HOME"
  1915. # fi
  1916. # fi
  1917. if [ -z "$LE_WORKING_DIR" ]; then
  1918. _debug "Using default home:$DEFAULT_INSTALL_HOME"
  1919. LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
  1920. fi
  1921. export LE_WORKING_DIR
  1922. if [ -z "$LE_CONFIG_HOME" ]; then
  1923. LE_CONFIG_HOME="$LE_WORKING_DIR"
  1924. fi
  1925. _debug "Using config home:$LE_CONFIG_HOME"
  1926. export LE_CONFIG_HOME
  1927. _DEFAULT_ACCOUNT_CONF_PATH="$LE_CONFIG_HOME/account.conf"
  1928. if [ -z "$ACCOUNT_CONF_PATH" ]; then
  1929. if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
  1930. . "$_DEFAULT_ACCOUNT_CONF_PATH"
  1931. fi
  1932. fi
  1933. if [ -z "$ACCOUNT_CONF_PATH" ]; then
  1934. ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
  1935. fi
  1936. DEFAULT_LOG_FILE="$LE_CONFIG_HOME/$PROJECT_NAME.log"
  1937. DEFAULT_CA_HOME="$LE_CONFIG_HOME/ca"
  1938. if [ -z "$LE_TEMP_DIR" ]; then
  1939. LE_TEMP_DIR="$LE_CONFIG_HOME/tmp"
  1940. fi
  1941. }
  1942. #server
  1943. _initAPI() {
  1944. _api_server="${1:-$ACME_DIRECTORY}"
  1945. _debug "_init api for server: $_api_server"
  1946. if [ -z "$ACME_NEW_ACCOUNT" ]; then
  1947. response=$(_get "$_api_server")
  1948. if [ "$?" != "0" ]; then
  1949. _debug2 "response" "$response"
  1950. _err "Can not init api."
  1951. return 1
  1952. fi
  1953. _debug2 "response" "$response"
  1954. ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'key-change" *: *"[^"]*"' | cut -d '"' -f 3)
  1955. if [ -z "$ACME_KEY_CHANGE" ]; then
  1956. ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'keyChange" *: *"[^"]*"' | cut -d '"' -f 3)
  1957. fi
  1958. export ACME_KEY_CHANGE
  1959. ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'new-authz" *: *"[^"]*"' | cut -d '"' -f 3)
  1960. if [ -z "$ACME_NEW_AUTHZ" ]; then
  1961. ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'newAuthz" *: *"[^"]*"' | cut -d '"' -f 3)
  1962. fi
  1963. export ACME_NEW_AUTHZ
  1964. ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-cert" *: *"[^"]*"' | cut -d '"' -f 3)
  1965. ACME_NEW_ORDER_RES="new-cert"
  1966. if [ -z "$ACME_NEW_ORDER" ]; then
  1967. ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-order" *: *"[^"]*"' | cut -d '"' -f 3)
  1968. ACME_NEW_ORDER_RES="new-order"
  1969. if [ -z "$ACME_NEW_ORDER" ]; then
  1970. ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'newOrder" *: *"[^"]*"' | cut -d '"' -f 3)
  1971. fi
  1972. fi
  1973. export ACME_NEW_ORDER
  1974. export ACME_NEW_ORDER_RES
  1975. ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-reg" *: *"[^"]*"' | cut -d '"' -f 3)
  1976. ACME_NEW_ACCOUNT_RES="new-reg"
  1977. if [ -z "$ACME_NEW_ACCOUNT" ]; then
  1978. ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-account" *: *"[^"]*"' | cut -d '"' -f 3)
  1979. ACME_NEW_ACCOUNT_RES="new-account"
  1980. if [ -z "$ACME_NEW_ACCOUNT" ]; then
  1981. ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'newAccount" *: *"[^"]*"' | cut -d '"' -f 3)
  1982. if [ "$ACME_NEW_ACCOUNT" ]; then
  1983. export ACME_VERSION=2
  1984. fi
  1985. fi
  1986. fi
  1987. export ACME_NEW_ACCOUNT
  1988. export ACME_NEW_ACCOUNT_RES
  1989. ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revoke-cert" *: *"[^"]*"' | cut -d '"' -f 3)
  1990. if [ -z "$ACME_REVOKE_CERT" ]; then
  1991. ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revokeCert" *: *"[^"]*"' | cut -d '"' -f 3)
  1992. fi
  1993. export ACME_REVOKE_CERT
  1994. ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'new-nonce" *: *"[^"]*"' | cut -d '"' -f 3)
  1995. if [ -z "$ACME_NEW_NONCE" ]; then
  1996. ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'newNonce" *: *"[^"]*"' | cut -d '"' -f 3)
  1997. fi
  1998. export ACME_NEW_NONCE
  1999. ACME_AGREEMENT=$(echo "$response" | _egrep_o 'terms-of-service" *: *"[^"]*"' | cut -d '"' -f 3)
  2000. if [ -z "$ACME_AGREEMENT" ]; then
  2001. ACME_AGREEMENT=$(echo "$response" | _egrep_o 'termsOfService" *: *"[^"]*"' | cut -d '"' -f 3)
  2002. fi
  2003. export ACME_AGREEMENT
  2004. _debug "ACME_KEY_CHANGE" "$ACME_KEY_CHANGE"
  2005. _debug "ACME_NEW_AUTHZ" "$ACME_NEW_AUTHZ"
  2006. _debug "ACME_NEW_ORDER" "$ACME_NEW_ORDER"
  2007. _debug "ACME_NEW_ACCOUNT" "$ACME_NEW_ACCOUNT"
  2008. _debug "ACME_REVOKE_CERT" "$ACME_REVOKE_CERT"
  2009. _debug "ACME_AGREEMENT" "$ACME_AGREEMENT"
  2010. _debug "ACME_NEW_NONCE" "$ACME_NEW_NONCE"
  2011. _debug "ACME_VERSION" "$ACME_VERSION"
  2012. fi
  2013. }
  2014. #[domain] [keylength or isEcc flag]
  2015. _initpath() {
  2016. domain="$1"
  2017. _ilength="$2"
  2018. __initHome
  2019. if [ -f "$ACCOUNT_CONF_PATH" ]; then
  2020. . "$ACCOUNT_CONF_PATH"
  2021. fi
  2022. if [ "$IN_CRON" ]; then
  2023. if [ ! "$_USER_PATH_EXPORTED" ]; then
  2024. _USER_PATH_EXPORTED=1
  2025. export PATH="$USER_PATH:$PATH"
  2026. fi
  2027. fi
  2028. if [ -z "$CA_HOME" ]; then
  2029. CA_HOME="$DEFAULT_CA_HOME"
  2030. fi
  2031. if [ "$ACME_VERSION" = "2" ]; then
  2032. DEFAULT_CA="$LETSENCRYPT_CA_V2"
  2033. DEFAULT_STAGING_CA="$LETSENCRYPT_STAGING_CA_V2"
  2034. fi
  2035. if [ -z "$ACME_DIRECTORY" ]; then
  2036. if [ -z "$STAGE" ]; then
  2037. ACME_DIRECTORY="$DEFAULT_CA"
  2038. else
  2039. ACME_DIRECTORY="$DEFAULT_STAGING_CA"
  2040. _info "Using stage ACME_DIRECTORY: $ACME_DIRECTORY"
  2041. fi
  2042. fi
  2043. _debug ACME_DIRECTORY "$ACME_DIRECTORY"
  2044. _ACME_SERVER_HOST="$(echo "$ACME_DIRECTORY" | cut -d : -f 2 | tr -s / | cut -d / -f 2)"
  2045. _debug2 "_ACME_SERVER_HOST" "$_ACME_SERVER_HOST"
  2046. CA_DIR="$CA_HOME/$_ACME_SERVER_HOST"
  2047. _DEFAULT_CA_CONF="$CA_DIR/ca.conf"
  2048. if [ -z "$CA_CONF" ]; then
  2049. CA_CONF="$_DEFAULT_CA_CONF"
  2050. fi
  2051. _debug3 CA_CONF "$CA_CONF"
  2052. if [ -f "$CA_CONF" ]; then
  2053. . "$CA_CONF"
  2054. fi
  2055. if [ -z "$ACME_DIR" ]; then
  2056. ACME_DIR="/home/.acme"
  2057. fi
  2058. if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
  2059. APACHE_CONF_BACKUP_DIR="$LE_CONFIG_HOME"
  2060. fi
  2061. if [ -z "$USER_AGENT" ]; then
  2062. USER_AGENT="$DEFAULT_USER_AGENT"
  2063. fi
  2064. if [ -z "$HTTP_HEADER" ]; then
  2065. HTTP_HEADER="$LE_CONFIG_HOME/http.header"
  2066. fi
  2067. _OLD_ACCOUNT_KEY="$LE_WORKING_DIR/account.key"
  2068. _OLD_ACCOUNT_JSON="$LE_WORKING_DIR/account.json"
  2069. _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key"
  2070. _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json"
  2071. if [ -z "$ACCOUNT_KEY_PATH" ]; then
  2072. ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
  2073. fi
  2074. if [ -z "$ACCOUNT_JSON_PATH" ]; then
  2075. ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH"
  2076. fi
  2077. _DEFAULT_CERT_HOME="$LE_CONFIG_HOME"
  2078. if [ -z "$CERT_HOME" ]; then
  2079. CERT_HOME="$_DEFAULT_CERT_HOME"
  2080. fi
  2081. if [ -z "$ACME_OPENSSL_BIN" ] || [ ! -f "$ACME_OPENSSL_BIN" ] || [ ! -x "$ACME_OPENSSL_BIN" ]; then
  2082. ACME_OPENSSL_BIN="$DEFAULT_OPENSSL_BIN"
  2083. fi
  2084. if [ -z "$domain" ]; then
  2085. return 0
  2086. fi
  2087. if [ -z "$DOMAIN_PATH" ]; then
  2088. domainhome="$CERT_HOME/$domain"
  2089. domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX"
  2090. DOMAIN_PATH="$domainhome"
  2091. if _isEccKey "$_ilength"; then
  2092. DOMAIN_PATH="$domainhomeecc"
  2093. else
  2094. if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
  2095. _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
  2096. fi
  2097. fi
  2098. _debug DOMAIN_PATH "$DOMAIN_PATH"
  2099. fi
  2100. if [ -z "$DOMAIN_BACKUP_PATH" ]; then
  2101. DOMAIN_BACKUP_PATH="$DOMAIN_PATH/backup"
  2102. fi
  2103. if [ -z "$DOMAIN_CONF" ]; then
  2104. DOMAIN_CONF="$DOMAIN_PATH/$domain.conf"
  2105. fi
  2106. if [ -z "$DOMAIN_SSL_CONF" ]; then
  2107. DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf"
  2108. fi
  2109. if [ -z "$CSR_PATH" ]; then
  2110. CSR_PATH="$DOMAIN_PATH/$domain.csr"
  2111. fi
  2112. if [ -z "$CERT_KEY_PATH" ]; then
  2113. CERT_KEY_PATH="$DOMAIN_PATH/$domain.key"
  2114. fi
  2115. if [ -z "$CERT_PATH" ]; then
  2116. CERT_PATH="$DOMAIN_PATH/$domain.cer"
  2117. fi
  2118. if [ -z "$CA_CERT_PATH" ]; then
  2119. CA_CERT_PATH="$DOMAIN_PATH/ca.cer"
  2120. fi
  2121. if [ -z "$CERT_FULLCHAIN_PATH" ]; then
  2122. CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer"
  2123. fi
  2124. if [ -z "$CERT_PFX_PATH" ]; then
  2125. CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx"
  2126. fi
  2127. if [ -z "$CERT_PKCS8_PATH" ]; then
  2128. CERT_PKCS8_PATH="$DOMAIN_PATH/$domain.pkcs8"
  2129. fi
  2130. if [ -z "$TLS_CONF" ]; then
  2131. TLS_CONF="$DOMAIN_PATH/tls.validation.conf"
  2132. fi
  2133. if [ -z "$TLS_CERT" ]; then
  2134. TLS_CERT="$DOMAIN_PATH/tls.validation.cert"
  2135. fi
  2136. if [ -z "$TLS_KEY" ]; then
  2137. TLS_KEY="$DOMAIN_PATH/tls.validation.key"
  2138. fi
  2139. if [ -z "$TLS_CSR" ]; then
  2140. TLS_CSR="$DOMAIN_PATH/tls.validation.csr"
  2141. fi
  2142. }
  2143. _exec() {
  2144. if [ -z "$_EXEC_TEMP_ERR" ]; then
  2145. _EXEC_TEMP_ERR="$(_mktemp)"
  2146. fi
  2147. if [ "$_EXEC_TEMP_ERR" ]; then
  2148. eval "$@ 2>>$_EXEC_TEMP_ERR"
  2149. else
  2150. eval "$@"
  2151. fi
  2152. }
  2153. _exec_err() {
  2154. [ "$_EXEC_TEMP_ERR" ] && _err "$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR"
  2155. }
  2156. _apachePath() {
  2157. _APACHECTL="apachectl"
  2158. if ! _exists apachectl; then
  2159. if _exists apache2ctl; then
  2160. _APACHECTL="apache2ctl"
  2161. else
  2162. _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'"
  2163. _err "Please use webroot mode to try again."
  2164. return 1
  2165. fi
  2166. fi
  2167. if ! _exec $_APACHECTL -V >/dev/null; then
  2168. _exec_err
  2169. return 1
  2170. fi
  2171. if [ "$APACHE_HTTPD_CONF" ]; then
  2172. _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF"
  2173. httpdconf="$APACHE_HTTPD_CONF"
  2174. httpdconfname="$(basename "$httpdconfname")"
  2175. else
  2176. httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
  2177. _debug httpdconfname "$httpdconfname"
  2178. if [ -z "$httpdconfname" ]; then
  2179. _err "Can not read apache config file."
  2180. return 1
  2181. fi
  2182. if _startswith "$httpdconfname" '/'; then
  2183. httpdconf="$httpdconfname"
  2184. httpdconfname="$(basename "$httpdconfname")"
  2185. else
  2186. httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
  2187. _debug httpdroot "$httpdroot"
  2188. httpdconf="$httpdroot/$httpdconfname"
  2189. httpdconfname="$(basename "$httpdconfname")"
  2190. fi
  2191. fi
  2192. _debug httpdconf "$httpdconf"
  2193. _debug httpdconfname "$httpdconfname"
  2194. if [ ! -f "$httpdconf" ]; then
  2195. _err "Apache Config file not found" "$httpdconf"
  2196. return 1
  2197. fi
  2198. return 0
  2199. }
  2200. _restoreApache() {
  2201. if [ -z "$usingApache" ]; then
  2202. return 0
  2203. fi
  2204. _initpath
  2205. if ! _apachePath; then
  2206. return 1
  2207. fi
  2208. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
  2209. _debug "No config file to restore."
  2210. return 0
  2211. fi
  2212. cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
  2213. _debug "Restored: $httpdconf."
  2214. if ! _exec $_APACHECTL -t; then
  2215. _exec_err
  2216. _err "Sorry, restore apache config error, please contact me."
  2217. return 1
  2218. fi
  2219. _debug "Restored successfully."
  2220. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  2221. return 0
  2222. }
  2223. _setApache() {
  2224. _initpath
  2225. if ! _apachePath; then
  2226. return 1
  2227. fi
  2228. #test the conf first
  2229. _info "Checking if there is an error in the apache config file before starting."
  2230. if ! _exec "$_APACHECTL" -t >/dev/null; then
  2231. _exec_err
  2232. _err "The apache config file has error, please fix it first, then try again."
  2233. _err "Don't worry, there is nothing changed to your system."
  2234. return 1
  2235. else
  2236. _info "OK"
  2237. fi
  2238. #backup the conf
  2239. _debug "Backup apache config file" "$httpdconf"
  2240. if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
  2241. _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
  2242. _err "This might be a bug of $PROJECT_NAME , please report issue: $PROJECT"
  2243. return 1
  2244. fi
  2245. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  2246. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  2247. _info "The backup file will be deleted on success, just forget it."
  2248. #add alias
  2249. apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
  2250. _debug "apacheVer" "$apacheVer"
  2251. apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
  2252. apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
  2253. if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ]; then
  2254. echo "
  2255. Alias /.well-known/acme-challenge $ACME_DIR
  2256. <Directory $ACME_DIR >
  2257. Require all granted
  2258. </Directory>
  2259. " >>"$httpdconf"
  2260. else
  2261. echo "
  2262. Alias /.well-known/acme-challenge $ACME_DIR
  2263. <Directory $ACME_DIR >
  2264. Order allow,deny
  2265. Allow from all
  2266. </Directory>
  2267. " >>"$httpdconf"
  2268. fi
  2269. _msg="$($_APACHECTL -t 2>&1)"
  2270. if [ "$?" != "0" ]; then
  2271. _err "Sorry, apache config error"
  2272. if _restoreApache; then
  2273. _err "The apache config file is restored."
  2274. else
  2275. _err "Sorry, The apache config file can not be restored, please report bug."
  2276. fi
  2277. return 1
  2278. fi
  2279. if [ ! -d "$ACME_DIR" ]; then
  2280. mkdir -p "$ACME_DIR"
  2281. chmod 755 "$ACME_DIR"
  2282. fi
  2283. if ! _exec "$_APACHECTL" graceful; then
  2284. _exec_err
  2285. _err "$_APACHECTL graceful error, please contact me."
  2286. _restoreApache
  2287. return 1
  2288. fi
  2289. usingApache="1"
  2290. return 0
  2291. }
  2292. #find the real nginx conf file
  2293. #backup
  2294. #set the nginx conf
  2295. #returns the real nginx conf file
  2296. _setNginx() {
  2297. _d="$1"
  2298. _croot="$2"
  2299. _thumbpt="$3"
  2300. FOUND_REAL_NGINX_CONF=""
  2301. FOUND_REAL_NGINX_CONF_LN=""
  2302. BACKUP_NGINX_CONF=""
  2303. _debug _croot "$_croot"
  2304. _start_f="$(echo "$_croot" | cut -d : -f 2)"
  2305. _debug _start_f "$_start_f"
  2306. if [ -z "$_start_f" ]; then
  2307. _debug "find start conf from nginx command"
  2308. if [ -z "$NGINX_CONF" ]; then
  2309. if ! _exists "nginx"; then
  2310. _err "nginx command is not found."
  2311. return 1
  2312. fi
  2313. NGINX_CONF="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^ ]* " | tr -d " ")"
  2314. _debug NGINX_CONF "$NGINX_CONF"
  2315. NGINX_CONF="$(echo "$NGINX_CONF" | cut -d = -f 2)"
  2316. _debug NGINX_CONF "$NGINX_CONF"
  2317. if [ ! -f "$NGINX_CONF" ]; then
  2318. _err "'$NGINX_CONF' doesn't exist."
  2319. NGINX_CONF=""
  2320. return 1
  2321. fi
  2322. _debug "Found nginx conf file:$NGINX_CONF"
  2323. fi
  2324. _start_f="$NGINX_CONF"
  2325. fi
  2326. _debug "Start detect nginx conf for $_d from:$_start_f"
  2327. if ! _checkConf "$_d" "$_start_f"; then
  2328. _err "Can not find conf file for domain $d"
  2329. return 1
  2330. fi
  2331. _info "Found conf file: $FOUND_REAL_NGINX_CONF"
  2332. _ln=$FOUND_REAL_NGINX_CONF_LN
  2333. _debug "_ln" "$_ln"
  2334. _lnn=$(_math $_ln + 1)
  2335. _debug _lnn "$_lnn"
  2336. _start_tag="$(sed -n "$_lnn,${_lnn}p" "$FOUND_REAL_NGINX_CONF")"
  2337. _debug "_start_tag" "$_start_tag"
  2338. if [ "$_start_tag" = "$NGINX_START" ]; then
  2339. _info "The domain $_d is already configured, skip"
  2340. FOUND_REAL_NGINX_CONF=""
  2341. return 0
  2342. fi
  2343. mkdir -p "$DOMAIN_BACKUP_PATH"
  2344. _backup_conf="$DOMAIN_BACKUP_PATH/$_d.nginx.conf"
  2345. _debug _backup_conf "$_backup_conf"
  2346. BACKUP_NGINX_CONF="$_backup_conf"
  2347. _info "Backup $FOUND_REAL_NGINX_CONF to $_backup_conf"
  2348. if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then
  2349. _err "backup error."
  2350. FOUND_REAL_NGINX_CONF=""
  2351. return 1
  2352. fi
  2353. if ! _exists "nginx"; then
  2354. _err "nginx command is not found."
  2355. return 1
  2356. fi
  2357. _info "Check the nginx conf before setting up."
  2358. if ! _exec "nginx -t" >/dev/null; then
  2359. _exec_err
  2360. return 1
  2361. fi
  2362. _info "OK, Set up nginx config file"
  2363. if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then
  2364. cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
  2365. _err "write nginx conf error, but don't worry, the file is restored to the original version."
  2366. return 1
  2367. fi
  2368. echo "$NGINX_START
  2369. location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" {
  2370. default_type text/plain;
  2371. return 200 \"\$1.$_thumbpt\";
  2372. }
  2373. #NGINX_START
  2374. " >>"$FOUND_REAL_NGINX_CONF"
  2375. if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then
  2376. cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
  2377. _err "write nginx conf error, but don't worry, the file is restored."
  2378. return 1
  2379. fi
  2380. _debug3 "Modified config:$(cat $FOUND_REAL_NGINX_CONF)"
  2381. _info "nginx conf is done, let's check it again."
  2382. if ! _exec "nginx -t" >/dev/null; then
  2383. _exec_err
  2384. _err "It seems that nginx conf was broken, let's restore."
  2385. cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
  2386. return 1
  2387. fi
  2388. _info "Reload nginx"
  2389. if ! _exec "nginx -s reload" >/dev/null; then
  2390. _exec_err
  2391. _err "It seems that nginx reload error, let's restore."
  2392. cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
  2393. return 1
  2394. fi
  2395. return 0
  2396. }
  2397. #d , conf
  2398. _checkConf() {
  2399. _d="$1"
  2400. _c_file="$2"
  2401. _debug "Start _checkConf from:$_c_file"
  2402. if [ ! -f "$2" ] && ! echo "$2" | grep '*$' >/dev/null && echo "$2" | grep '*' >/dev/null; then
  2403. _debug "wildcard"
  2404. for _w_f in $2; do
  2405. if [ -f "$_w_f" ] && _checkConf "$1" "$_w_f"; then
  2406. return 0
  2407. fi
  2408. done
  2409. #not found
  2410. return 1
  2411. elif [ -f "$2" ]; then
  2412. _debug "single"
  2413. if _isRealNginxConf "$1" "$2"; then
  2414. _debug "$2 is found."
  2415. FOUND_REAL_NGINX_CONF="$2"
  2416. return 0
  2417. fi
  2418. if cat "$2" | tr "\t" " " | grep "^ *include *.*;" >/dev/null; then
  2419. _debug "Try include files"
  2420. for included in $(cat "$2" | tr "\t" " " | grep "^ *include *.*;" | sed "s/include //" | tr -d " ;"); do
  2421. _debug "check included $included"
  2422. if _checkConf "$1" "$included"; then
  2423. return 0
  2424. fi
  2425. done
  2426. fi
  2427. return 1
  2428. else
  2429. _debug "$2 not found."
  2430. return 1
  2431. fi
  2432. return 1
  2433. }
  2434. #d , conf
  2435. _isRealNginxConf() {
  2436. _debug "_isRealNginxConf $1 $2"
  2437. if [ -f "$2" ]; then
  2438. for _fln in $(tr "\t" ' ' <"$2" | grep -n "^ *server_name.* $1" | cut -d : -f 1); do
  2439. _debug _fln "$_fln"
  2440. if [ "$_fln" ]; then
  2441. _start=$(tr "\t" ' ' <"$2" | _head_n "$_fln" | grep -n "^ *server *" | grep -v server_name | _tail_n 1)
  2442. _debug "_start" "$_start"
  2443. _start_n=$(echo "$_start" | cut -d : -f 1)
  2444. _start_nn=$(_math $_start_n + 1)
  2445. _debug "_start_n" "$_start_n"
  2446. _debug "_start_nn" "$_start_nn"
  2447. _left="$(sed -n "${_start_nn},99999p" "$2")"
  2448. _debug2 _left "$_left"
  2449. _end="$(echo "$_left" | tr "\t" ' ' | grep -n "^ *server *" | grep -v server_name | _head_n 1)"
  2450. _debug "_end" "$_end"
  2451. if [ "$_end" ]; then
  2452. _end_n=$(echo "$_end" | cut -d : -f 1)
  2453. _debug "_end_n" "$_end_n"
  2454. _seg_n=$(echo "$_left" | sed -n "1,${_end_n}p")
  2455. else
  2456. _seg_n="$_left"
  2457. fi
  2458. _debug "_seg_n" "$_seg_n"
  2459. _skip_ssl=1
  2460. for _listen_i in $(echo "$_seg_n" | tr "\t" ' ' | grep "^ *listen" | tr -d " "); do
  2461. if [ "$_listen_i" ]; then
  2462. if [ "$(echo "$_listen_i" | _egrep_o "listen.*ssl[ |;]")" ]; then
  2463. _debug2 "$_listen_i is ssl"
  2464. else
  2465. _debug2 "$_listen_i is plain text"
  2466. _skip_ssl=""
  2467. break
  2468. fi
  2469. fi
  2470. done
  2471. if [ "$_skip_ssl" = "1" ]; then
  2472. _debug "ssl on, skip"
  2473. else
  2474. FOUND_REAL_NGINX_CONF_LN=$_fln
  2475. _debug3 "found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN"
  2476. return 0
  2477. fi
  2478. fi
  2479. done
  2480. fi
  2481. return 1
  2482. }
  2483. #restore all the nginx conf
  2484. _restoreNginx() {
  2485. if [ -z "$NGINX_RESTORE_VLIST" ]; then
  2486. _debug "No need to restore nginx, skip."
  2487. return
  2488. fi
  2489. _debug "_restoreNginx"
  2490. _debug "NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
  2491. for ng_entry in $(echo "$NGINX_RESTORE_VLIST" | tr "$dvsep" ' '); do
  2492. _debug "ng_entry" "$ng_entry"
  2493. _nd=$(echo "$ng_entry" | cut -d "$sep" -f 1)
  2494. _ngconf=$(echo "$ng_entry" | cut -d "$sep" -f 2)
  2495. _ngbackupconf=$(echo "$ng_entry" | cut -d "$sep" -f 3)
  2496. _info "Restoring from $_ngbackupconf to $_ngconf"
  2497. cat "$_ngbackupconf" >"$_ngconf"
  2498. done
  2499. _info "Reload nginx"
  2500. if ! _exec "nginx -s reload" >/dev/null; then
  2501. _exec_err
  2502. _err "It seems that nginx reload error, please report bug."
  2503. return 1
  2504. fi
  2505. return 0
  2506. }
  2507. _clearup() {
  2508. _stopserver "$serverproc"
  2509. serverproc=""
  2510. _restoreApache
  2511. _restoreNginx
  2512. _clearupdns
  2513. if [ -z "$DEBUG" ]; then
  2514. rm -f "$TLS_CONF"
  2515. rm -f "$TLS_CERT"
  2516. rm -f "$TLS_KEY"
  2517. rm -f "$TLS_CSR"
  2518. fi
  2519. }
  2520. _clearupdns() {
  2521. _debug "_clearupdns"
  2522. if [ "$dnsadded" != 1 ] || [ -z "$vlist" ]; then
  2523. _debug "skip dns."
  2524. return
  2525. fi
  2526. _info "Removing DNS records."
  2527. ventries=$(echo "$vlist" | tr ',' ' ')
  2528. _alias_index=1
  2529. for ventry in $ventries; do
  2530. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  2531. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  2532. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  2533. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  2534. txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
  2535. _debug txt "$txt"
  2536. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  2537. _debug "$d is already verified, skip $vtype."
  2538. continue
  2539. fi
  2540. if [ "$vtype" != "$VTYPE_DNS" ]; then
  2541. _debug "Skip $d for $vtype"
  2542. continue
  2543. fi
  2544. d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
  2545. _debug d_api "$d_api"
  2546. if [ -z "$d_api" ]; then
  2547. _info "Not Found domain api file: $d_api"
  2548. continue
  2549. fi
  2550. (
  2551. if ! . "$d_api"; then
  2552. _err "Load file $d_api error. Please check your api file and try again."
  2553. return 1
  2554. fi
  2555. rmcommand="${_currentRoot}_rm"
  2556. if ! _exists "$rmcommand"; then
  2557. _err "It seems that your api file doesn't define $rmcommand"
  2558. return 1
  2559. fi
  2560. _dns_root_d="$d"
  2561. if _startswith "$_dns_root_d" "*."; then
  2562. _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')"
  2563. fi
  2564. _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
  2565. _alias_index="$(_math "$_alias_index" + 1)"
  2566. _debug "_d_alias" "$_d_alias"
  2567. if [ "$_d_alias" ]; then
  2568. if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
  2569. txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
  2570. else
  2571. txtdomain="_acme-challenge.$_d_alias"
  2572. fi
  2573. else
  2574. txtdomain="_acme-challenge.$_dns_root_d"
  2575. fi
  2576. if ! $rmcommand "$txtdomain" "$txt"; then
  2577. _err "Error removing txt for domain:$txtdomain"
  2578. return 1
  2579. fi
  2580. )
  2581. done
  2582. }
  2583. # webroot removelevel tokenfile
  2584. _clearupwebbroot() {
  2585. __webroot="$1"
  2586. if [ -z "$__webroot" ]; then
  2587. _debug "no webroot specified, skip"
  2588. return 0
  2589. fi
  2590. _rmpath=""
  2591. if [ "$2" = '1' ]; then
  2592. _rmpath="$__webroot/.well-known"
  2593. elif [ "$2" = '2' ]; then
  2594. _rmpath="$__webroot/.well-known/acme-challenge"
  2595. elif [ "$2" = '3' ]; then
  2596. _rmpath="$__webroot/.well-known/acme-challenge/$3"
  2597. else
  2598. _debug "Skip for removelevel:$2"
  2599. fi
  2600. if [ "$_rmpath" ]; then
  2601. if [ "$DEBUG" ]; then
  2602. _debug "Debugging, skip removing: $_rmpath"
  2603. else
  2604. rm -rf "$_rmpath"
  2605. fi
  2606. fi
  2607. return 0
  2608. }
  2609. _on_before_issue() {
  2610. _chk_web_roots="$1"
  2611. _chk_main_domain="$2"
  2612. _chk_alt_domains="$3"
  2613. _chk_pre_hook="$4"
  2614. _chk_local_addr="$5"
  2615. _debug _on_before_issue
  2616. _debug _chk_main_domain "$_chk_main_domain"
  2617. _debug _chk_alt_domains "$_chk_alt_domains"
  2618. #run pre hook
  2619. if [ "$_chk_pre_hook" ]; then
  2620. _info "Run pre hook:'$_chk_pre_hook'"
  2621. if ! (
  2622. cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
  2623. ); then
  2624. _err "Error when run pre hook."
  2625. return 1
  2626. fi
  2627. fi
  2628. if _hasfield "$_chk_web_roots" "$NO_VALUE"; then
  2629. if ! _exists "socat"; then
  2630. _err "Please install socat tools first."
  2631. return 1
  2632. fi
  2633. fi
  2634. _debug Le_LocalAddress "$_chk_local_addr"
  2635. _index=1
  2636. _currentRoot=""
  2637. _addrIndex=1
  2638. _w_index=1
  2639. while true; do
  2640. d="$(echo "$_chk_main_domain,$_chk_alt_domains," | cut -d , -f "$_w_index")"
  2641. _w_index="$(_math "$_w_index" + 1)"
  2642. _debug d "$d"
  2643. if [ -z "$d" ]; then
  2644. break
  2645. fi
  2646. _debug "Check for domain" "$d"
  2647. _currentRoot="$(_getfield "$_chk_web_roots" $_index)"
  2648. _debug "_currentRoot" "$_currentRoot"
  2649. _index=$(_math $_index + 1)
  2650. _checkport=""
  2651. if [ "$_currentRoot" = "$NO_VALUE" ]; then
  2652. _info "Standalone mode."
  2653. if [ -z "$Le_HTTPPort" ]; then
  2654. Le_HTTPPort=80
  2655. else
  2656. _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
  2657. fi
  2658. _checkport="$Le_HTTPPort"
  2659. elif [ "$_currentRoot" = "$W_TLS" ]; then
  2660. _info "Standalone tls mode."
  2661. if [ -z "$Le_TLSPort" ]; then
  2662. Le_TLSPort=443
  2663. else
  2664. _savedomainconf "Le_TLSPort" "$Le_TLSPort"
  2665. fi
  2666. _checkport="$Le_TLSPort"
  2667. fi
  2668. if [ "$_checkport" ]; then
  2669. _debug _checkport "$_checkport"
  2670. _checkaddr="$(_getfield "$_chk_local_addr" $_addrIndex)"
  2671. _debug _checkaddr "$_checkaddr"
  2672. _addrIndex="$(_math $_addrIndex + 1)"
  2673. _netprc="$(_ss "$_checkport" | grep "$_checkport")"
  2674. netprc="$(echo "$_netprc" | grep "$_checkaddr")"
  2675. if [ -z "$netprc" ]; then
  2676. netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
  2677. fi
  2678. if [ "$netprc" ]; then
  2679. _err "$netprc"
  2680. _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
  2681. _err "Please stop it first"
  2682. return 1
  2683. fi
  2684. fi
  2685. done
  2686. if _hasfield "$_chk_web_roots" "apache"; then
  2687. if ! _setApache; then
  2688. _err "set up apache error. Report error to me."
  2689. return 1
  2690. fi
  2691. else
  2692. usingApache=""
  2693. fi
  2694. }
  2695. _on_issue_err() {
  2696. _chk_post_hook="$1"
  2697. _chk_vlist="$2"
  2698. _debug _on_issue_err
  2699. if [ "$LOG_FILE" ]; then
  2700. _err "Please check log file for more details: $LOG_FILE"
  2701. else
  2702. _err "Please add '--debug' or '--log' to check more details."
  2703. _err "See: $_DEBUG_WIKI"
  2704. fi
  2705. #run the post hook
  2706. if [ "$_chk_post_hook" ]; then
  2707. _info "Run post hook:'$_chk_post_hook'"
  2708. if ! (
  2709. cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
  2710. ); then
  2711. _err "Error when run post hook."
  2712. return 1
  2713. fi
  2714. fi
  2715. #trigger the validation to flush the pending authz
  2716. _debug2 "_chk_vlist" "$_chk_vlist"
  2717. if [ "$_chk_vlist" ]; then
  2718. (
  2719. _debug2 "start to deactivate authz"
  2720. ventries=$(echo "$_chk_vlist" | tr "$dvsep" ' ')
  2721. for ventry in $ventries; do
  2722. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  2723. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  2724. uri=$(echo "$ventry" | cut -d "$sep" -f 3)
  2725. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  2726. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  2727. __trigger_validation "$uri" "$keyauthorization"
  2728. done
  2729. )
  2730. fi
  2731. if [ "$IS_RENEW" = "1" ] && _hasfield "$Le_Webroot" "$W_DNS"; then
  2732. _err "$_DNS_MANUAL_ERR"
  2733. fi
  2734. if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
  2735. _debug "$(_dlg_versions)"
  2736. fi
  2737. }
  2738. _on_issue_success() {
  2739. _chk_post_hook="$1"
  2740. _chk_renew_hook="$2"
  2741. _debug _on_issue_success
  2742. #run the post hook
  2743. if [ "$_chk_post_hook" ]; then
  2744. _info "Run post hook:'$_chk_post_hook'"
  2745. if ! (
  2746. cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
  2747. ); then
  2748. _err "Error when run post hook."
  2749. return 1
  2750. fi
  2751. fi
  2752. #run renew hook
  2753. if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
  2754. _info "Run renew hook:'$_chk_renew_hook'"
  2755. if ! (
  2756. cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
  2757. ); then
  2758. _err "Error when run renew hook."
  2759. return 1
  2760. fi
  2761. fi
  2762. if _hasfield "$Le_Webroot" "$W_DNS"; then
  2763. _err "$_DNS_MANUAL_WARN"
  2764. fi
  2765. }
  2766. updateaccount() {
  2767. _initpath
  2768. _regAccount
  2769. }
  2770. registeraccount() {
  2771. _reg_length="$1"
  2772. _initpath
  2773. _regAccount "$_reg_length"
  2774. }
  2775. __calcAccountKeyHash() {
  2776. [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH"
  2777. }
  2778. __calc_account_thumbprint() {
  2779. printf "%s" "$jwk" | tr -d ' ' | _digest "sha256" | _url_replace
  2780. }
  2781. #keylength
  2782. _regAccount() {
  2783. _initpath
  2784. _reg_length="$1"
  2785. _debug3 _regAccount "$_regAccount"
  2786. _initAPI
  2787. mkdir -p "$CA_DIR"
  2788. if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
  2789. _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
  2790. mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
  2791. fi
  2792. if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
  2793. _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
  2794. mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
  2795. fi
  2796. if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
  2797. if ! _create_account_key "$_reg_length"; then
  2798. _err "Create account key error."
  2799. return 1
  2800. fi
  2801. fi
  2802. if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
  2803. return 1
  2804. fi
  2805. if [ "$ACME_VERSION" = "2" ]; then
  2806. regjson='{"termsOfServiceAgreed": true}'
  2807. if [ "$ACCOUNT_EMAIL" ]; then
  2808. regjson='{"contact": ["mailto: '$ACCOUNT_EMAIL'"], "termsOfServiceAgreed": true}'
  2809. fi
  2810. else
  2811. _reg_res="$ACME_NEW_ACCOUNT_RES"
  2812. regjson='{"resource": "'$_reg_res'", "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
  2813. if [ "$ACCOUNT_EMAIL" ]; then
  2814. regjson='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
  2815. fi
  2816. fi
  2817. _info "Registering account"
  2818. if ! _send_signed_request "${ACME_NEW_ACCOUNT}" "$regjson"; then
  2819. _err "Register account Error: $response"
  2820. return 1
  2821. fi
  2822. if [ "$code" = "" ] || [ "$code" = '201' ]; then
  2823. echo "$response" >"$ACCOUNT_JSON_PATH"
  2824. _info "Registered"
  2825. elif [ "$code" = '409' ] || [ "$code" = '200' ]; then
  2826. _info "Already registered"
  2827. else
  2828. _err "Register account Error: $response"
  2829. return 1
  2830. fi
  2831. _debug2 responseHeaders "$responseHeaders"
  2832. _accUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
  2833. _debug "_accUri" "$_accUri"
  2834. if [ -z "$_accUri" ]; then
  2835. _err "Can not find account id url."
  2836. _err "$responseHeaders"
  2837. return 1
  2838. fi
  2839. _savecaconf "ACCOUNT_URL" "$_accUri"
  2840. export ACCOUNT_URL="$_accUri"
  2841. CA_KEY_HASH="$(__calcAccountKeyHash)"
  2842. _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH"
  2843. _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
  2844. if [ "$code" = '403' ]; then
  2845. _err "It seems that the account key is already deactivated, please use a new account key."
  2846. return 1
  2847. fi
  2848. ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)"
  2849. _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
  2850. }
  2851. #Implement deactivate account
  2852. deactivateaccount() {
  2853. _initpath
  2854. if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
  2855. _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
  2856. mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
  2857. fi
  2858. if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
  2859. _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
  2860. mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
  2861. fi
  2862. if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
  2863. _err "Account key is not found at: $ACCOUNT_KEY_PATH"
  2864. return 1
  2865. fi
  2866. _accUri=$(_readcaconf "ACCOUNT_URL")
  2867. _debug _accUri "$_accUri"
  2868. if [ -z "$_accUri" ]; then
  2869. _err "The account url is empty, please run '--update-account' first to update the account info first,"
  2870. _err "Then try again."
  2871. return 1
  2872. fi
  2873. if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
  2874. return 1
  2875. fi
  2876. _initAPI
  2877. if [ "$ACME_VERSION" = "2" ]; then
  2878. _djson="{\"status\":\"deactivated\"}"
  2879. else
  2880. _djson="{\"resource\": \"reg\", \"status\":\"deactivated\"}"
  2881. fi
  2882. if _send_signed_request "$_accUri" "$_djson" && _contains "$response" '"deactivated"'; then
  2883. _info "Deactivate account success for $_accUri."
  2884. _accid=$(echo "$response" | _egrep_o "\"id\" *: *[^,]*," | cut -d : -f 2 | tr -d ' ,')
  2885. elif [ "$code" = "403" ]; then
  2886. _info "The account is already deactivated."
  2887. _accid=$(_getfield "$_accUri" "999" "/")
  2888. else
  2889. _err "Deactivate: account failed for $_accUri."
  2890. return 1
  2891. fi
  2892. _debug "Account id: $_accid"
  2893. if [ "$_accid" ]; then
  2894. _deactivated_account_path="$CA_DIR/deactivated/$_accid"
  2895. _debug _deactivated_account_path "$_deactivated_account_path"
  2896. if mkdir -p "$_deactivated_account_path"; then
  2897. _info "Moving deactivated account info to $_deactivated_account_path/"
  2898. mv "$CA_CONF" "$_deactivated_account_path/"
  2899. mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/"
  2900. mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/"
  2901. else
  2902. _err "Can not create dir: $_deactivated_account_path, try to remove the deactivated account key."
  2903. rm -f "$CA_CONF"
  2904. rm -f "$ACCOUNT_JSON_PATH"
  2905. rm -f "$ACCOUNT_KEY_PATH"
  2906. fi
  2907. fi
  2908. }
  2909. # domain folder file
  2910. _findHook() {
  2911. _hookdomain="$1"
  2912. _hookcat="$2"
  2913. _hookname="$3"
  2914. if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
  2915. d_api="$_SCRIPT_HOME/$_hookcat/$_hookname"
  2916. elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
  2917. d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
  2918. elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
  2919. d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
  2920. elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
  2921. d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
  2922. elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
  2923. d_api="$LE_WORKING_DIR/$_hookname"
  2924. elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
  2925. d_api="$LE_WORKING_DIR/$_hookname.sh"
  2926. elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
  2927. d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
  2928. elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
  2929. d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
  2930. fi
  2931. printf "%s" "$d_api"
  2932. }
  2933. #domain
  2934. __get_domain_new_authz() {
  2935. _gdnd="$1"
  2936. _info "Getting new-authz for domain" "$_gdnd"
  2937. _initAPI
  2938. _Max_new_authz_retry_times=5
  2939. _authz_i=0
  2940. while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
  2941. _debug "Try new-authz for the $_authz_i time."
  2942. if ! _send_signed_request "${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
  2943. _err "Can not get domain new authz."
  2944. return 1
  2945. fi
  2946. if _contains "$response" "No registration exists matching provided key"; then
  2947. _err "It seems there is an error, but it's recovered now, please try again."
  2948. _err "If you see this message for a second time, please report bug: $(__green "$PROJECT")"
  2949. _clearcaconf "CA_KEY_HASH"
  2950. break
  2951. fi
  2952. if ! _contains "$response" "An error occurred while processing your request"; then
  2953. _info "The new-authz request is ok."
  2954. break
  2955. fi
  2956. _authz_i="$(_math "$_authz_i" + 1)"
  2957. _info "The server is busy, Sleep $_authz_i to retry."
  2958. _sleep "$_authz_i"
  2959. done
  2960. if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
  2961. _err "new-authz retry reach the max $_Max_new_authz_retry_times times."
  2962. fi
  2963. if [ "$code" ] && [ "$code" != '201' ]; then
  2964. _err "new-authz error: $response"
  2965. return 1
  2966. fi
  2967. }
  2968. #uri keyAuthorization
  2969. __trigger_validation() {
  2970. _debug2 "tigger domain validation."
  2971. _t_url="$1"
  2972. _debug2 _t_url "$_t_url"
  2973. _t_key_authz="$2"
  2974. _debug2 _t_key_authz "$_t_key_authz"
  2975. if [ "$ACME_VERSION" = "2" ]; then
  2976. _send_signed_request "$_t_url" "{\"keyAuthorization\": \"$_t_key_authz\"}"
  2977. else
  2978. _send_signed_request "$_t_url" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$_t_key_authz\"}"
  2979. fi
  2980. }
  2981. #webroot, domain domainlist keylength
  2982. issue() {
  2983. if [ -z "$2" ]; then
  2984. _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
  2985. return 1
  2986. fi
  2987. if [ -z "$1" ]; then
  2988. _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
  2989. return 1
  2990. fi
  2991. _web_roots="$1"
  2992. _main_domain="$2"
  2993. _alt_domains="$3"
  2994. if _contains "$_main_domain" ","; then
  2995. _main_domain=$(echo "$2,$3" | cut -d , -f 1)
  2996. _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//")
  2997. fi
  2998. _debug _main_domain "$_main_domain"
  2999. _debug _alt_domains "$_alt_domains"
  3000. _key_length="$4"
  3001. _real_cert="$5"
  3002. _real_key="$6"
  3003. _real_ca="$7"
  3004. _reload_cmd="$8"
  3005. _real_fullchain="$9"
  3006. _pre_hook="${10}"
  3007. _post_hook="${11}"
  3008. _renew_hook="${12}"
  3009. _local_addr="${13}"
  3010. _challenge_alias="${14}"
  3011. #remove these later.
  3012. if [ "$_web_roots" = "dns-cf" ]; then
  3013. _web_roots="dns_cf"
  3014. fi
  3015. if [ "$_web_roots" = "dns-dp" ]; then
  3016. _web_roots="dns_dp"
  3017. fi
  3018. if [ "$_web_roots" = "dns-cx" ]; then
  3019. _web_roots="dns_cx"
  3020. fi
  3021. if [ ! "$IS_RENEW" ]; then
  3022. _initpath "$_main_domain" "$_key_length"
  3023. mkdir -p "$DOMAIN_PATH"
  3024. fi
  3025. if _hasfield "$_web_roots" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then
  3026. _err "$_DNS_MANUAL_ERROR"
  3027. return 1
  3028. fi
  3029. _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY"
  3030. _initAPI
  3031. if [ -f "$DOMAIN_CONF" ]; then
  3032. Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
  3033. _debug Le_NextRenewTime "$Le_NextRenewTime"
  3034. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
  3035. _saved_domain=$(_readdomainconf Le_Domain)
  3036. _debug _saved_domain "$_saved_domain"
  3037. _saved_alt=$(_readdomainconf Le_Alt)
  3038. _debug _saved_alt "$_saved_alt"
  3039. if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
  3040. _info "Domains not changed."
  3041. _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
  3042. _info "Add '$(__red '--force')' to force to renew."
  3043. return $RENEW_SKIP
  3044. else
  3045. _info "Domains have changed."
  3046. fi
  3047. fi
  3048. fi
  3049. _savedomainconf "Le_Domain" "$_main_domain"
  3050. _savedomainconf "Le_Alt" "$_alt_domains"
  3051. _savedomainconf "Le_Webroot" "$_web_roots"
  3052. _savedomainconf "Le_PreHook" "$_pre_hook"
  3053. _savedomainconf "Le_PostHook" "$_post_hook"
  3054. _savedomainconf "Le_RenewHook" "$_renew_hook"
  3055. if [ "$_local_addr" ]; then
  3056. _savedomainconf "Le_LocalAddress" "$_local_addr"
  3057. else
  3058. _cleardomainconf "Le_LocalAddress"
  3059. fi
  3060. if [ "$_challenge_alias" ]; then
  3061. _savedomainconf "Le_ChallengeAlias" "$_challenge_alias"
  3062. else
  3063. _cleardomainconf "Le_ChallengeAlias"
  3064. fi
  3065. Le_API="$ACME_DIRECTORY"
  3066. _savedomainconf "Le_API" "$Le_API"
  3067. if [ "$_alt_domains" = "$NO_VALUE" ]; then
  3068. _alt_domains=""
  3069. fi
  3070. if [ "$_key_length" = "$NO_VALUE" ]; then
  3071. _key_length=""
  3072. fi
  3073. if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
  3074. _err "_on_before_issue."
  3075. return 1
  3076. fi
  3077. _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
  3078. _debug2 _saved_account_key_hash "$_saved_account_key_hash"
  3079. if [ -z "$ACCOUNT_URL" ] || [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
  3080. if ! _regAccount "$_accountkeylength"; then
  3081. _on_issue_err "$_post_hook"
  3082. return 1
  3083. fi
  3084. else
  3085. _debug "_saved_account_key_hash is not changed, skip register account."
  3086. fi
  3087. if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
  3088. _info "Signing from existing CSR."
  3089. else
  3090. _key=$(_readdomainconf Le_Keylength)
  3091. _debug "Read key length:$_key"
  3092. if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then
  3093. if ! createDomainKey "$_main_domain" "$_key_length"; then
  3094. _err "Create domain key error."
  3095. _clearup
  3096. _on_issue_err "$_post_hook"
  3097. return 1
  3098. fi
  3099. fi
  3100. if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
  3101. _err "Create CSR error."
  3102. _clearup
  3103. _on_issue_err "$_post_hook"
  3104. return 1
  3105. fi
  3106. fi
  3107. _savedomainconf "Le_Keylength" "$_key_length"
  3108. vlist="$Le_Vlist"
  3109. _info "Getting domain auth token for each domain"
  3110. sep='#'
  3111. dvsep=','
  3112. if [ -z "$vlist" ]; then
  3113. if [ "$ACME_VERSION" = "2" ]; then
  3114. #make new order request
  3115. _identifiers="{\"type\":\"dns\",\"value\":\"$_main_domain\"}"
  3116. _w_index=1
  3117. while true; do
  3118. d="$(echo "$_alt_domains," | cut -d , -f "$_w_index")"
  3119. _w_index="$(_math "$_w_index" + 1)"
  3120. _debug d "$d"
  3121. if [ -z "$d" ]; then
  3122. break
  3123. fi
  3124. _identifiers="$_identifiers,{\"type\":\"dns\",\"value\":\"$d\"}"
  3125. done
  3126. _debug2 _identifiers "$_identifiers"
  3127. if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
  3128. _err "Create new order error."
  3129. _clearup
  3130. _on_issue_err "$_post_hook"
  3131. return 1
  3132. fi
  3133. Le_OrderFinalize="$(echo "$response" | tr -d '\r\n' | _egrep_o '"finalize" *: *"[^"]*"' | cut -d '"' -f 4)"
  3134. _debug Le_OrderFinalize "$Le_OrderFinalize"
  3135. if [ -z "$Le_OrderFinalize" ]; then
  3136. _err "Create new order error. Le_OrderFinalize not found. $response"
  3137. _clearup
  3138. _on_issue_err "$_post_hook"
  3139. return 1
  3140. fi
  3141. #for dns manual mode
  3142. _savedomainconf "Le_OrderFinalize" "$Le_OrderFinalize"
  3143. _authorizations_seg="$(echo "$response" | tr -d '\r\n' | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
  3144. _debug2 _authorizations_seg "$_authorizations_seg"
  3145. if [ -z "$_authorizations_seg" ]; then
  3146. _err "_authorizations_seg not found."
  3147. _clearup
  3148. _on_issue_err "$_post_hook"
  3149. return 1
  3150. fi
  3151. #domain and authz map
  3152. _authorizations_map=""
  3153. for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do
  3154. _debug2 "_authz_url" "$_authz_url"
  3155. if ! response="$(_get "$_authz_url")"; then
  3156. _err "get to authz error."
  3157. _err "_authorizations_seg" "$_authorizations_seg"
  3158. _err "_authz_url" "$_authz_url"
  3159. _clearup
  3160. _on_issue_err "$_post_hook"
  3161. return 1
  3162. fi
  3163. response="$(echo "$response" | _normalizeJson)"
  3164. _debug2 response "$response"
  3165. _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2 | tr -d ' "')"
  3166. if _contains "$response" "\"wildcard\" *: *true"; then
  3167. _d="*.$_d"
  3168. fi
  3169. _debug2 _d "$_d"
  3170. _authorizations_map="$_d,$response
  3171. $_authorizations_map"
  3172. done
  3173. _debug2 _authorizations_map "$_authorizations_map"
  3174. fi
  3175. _index=0
  3176. _currentRoot=""
  3177. _w_index=1
  3178. while true; do
  3179. d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")"
  3180. _w_index="$(_math "$_w_index" + 1)"
  3181. _debug d "$d"
  3182. if [ -z "$d" ]; then
  3183. break
  3184. fi
  3185. _info "Getting webroot for domain" "$d"
  3186. _index=$(_math $_index + 1)
  3187. _w="$(echo $_web_roots | cut -d , -f $_index)"
  3188. _debug _w "$_w"
  3189. if [ "$_w" ]; then
  3190. _currentRoot="$_w"
  3191. fi
  3192. _debug "_currentRoot" "$_currentRoot"
  3193. vtype="$VTYPE_HTTP"
  3194. #todo, v2 wildcard force to use dns
  3195. if _startswith "$_currentRoot" "$W_DNS"; then
  3196. vtype="$VTYPE_DNS"
  3197. fi
  3198. if [ "$_currentRoot" = "$W_TLS" ]; then
  3199. if [ "$ACME_VERSION" = "2" ]; then
  3200. vtype="$VTYPE_TLS2"
  3201. else
  3202. vtype="$VTYPE_TLS"
  3203. fi
  3204. fi
  3205. if [ "$ACME_VERSION" = "2" ]; then
  3206. response="$(echo "$_authorizations_map" | grep "^$d," | sed "s/$d,//")"
  3207. _debug2 "response" "$response"
  3208. if [ -z "$response" ]; then
  3209. _err "get to authz error."
  3210. _err "_authorizations_map" "$_authorizations_map"
  3211. _clearup
  3212. _on_issue_err "$_post_hook"
  3213. return 1
  3214. fi
  3215. else
  3216. if ! __get_domain_new_authz "$d"; then
  3217. _clearup
  3218. _on_issue_err "$_post_hook"
  3219. return 1
  3220. fi
  3221. fi
  3222. if [ -z "$thumbprint" ]; then
  3223. thumbprint="$(__calc_account_thumbprint)"
  3224. fi
  3225. entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
  3226. _debug entry "$entry"
  3227. if [ -z "$entry" ]; then
  3228. _err "Error, can not get domain token entry $d"
  3229. _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')"
  3230. if [ "$_supported_vtypes" ]; then
  3231. _err "The supported validation types are: $_supported_vtypes, but you specified: $vtype"
  3232. fi
  3233. _clearup
  3234. _on_issue_err "$_post_hook"
  3235. return 1
  3236. fi
  3237. token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  3238. _debug token "$token"
  3239. if [ -z "$token" ]; then
  3240. _err "Error, can not get domain token $entry"
  3241. _clearup
  3242. _on_issue_err "$_post_hook"
  3243. return 1
  3244. fi
  3245. if [ "$ACME_VERSION" = "2" ]; then
  3246. uri="$(printf "%s\n" "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)"
  3247. else
  3248. uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d '"' -f 4)"
  3249. fi
  3250. _debug uri "$uri"
  3251. if [ -z "$uri" ]; then
  3252. _err "Error, can not get domain uri. $entry"
  3253. _clearup
  3254. _on_issue_err "$_post_hook"
  3255. return 1
  3256. fi
  3257. keyauthorization="$token.$thumbprint"
  3258. _debug keyauthorization "$keyauthorization"
  3259. if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
  3260. _debug "$d is already verified."
  3261. keyauthorization="$STATE_VERIFIED"
  3262. _debug keyauthorization "$keyauthorization"
  3263. fi
  3264. dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
  3265. _debug dvlist "$dvlist"
  3266. vlist="$vlist$dvlist$dvsep"
  3267. done
  3268. _debug vlist "$vlist"
  3269. #add entry
  3270. dnsadded=""
  3271. ventries=$(echo "$vlist" | tr "$dvsep" ' ')
  3272. _alias_index=1
  3273. for ventry in $ventries; do
  3274. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  3275. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  3276. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  3277. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  3278. _debug d "$d"
  3279. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  3280. _debug "$d is already verified, skip $vtype."
  3281. continue
  3282. fi
  3283. if [ "$vtype" = "$VTYPE_DNS" ]; then
  3284. dnsadded='0'
  3285. _dns_root_d="$d"
  3286. if _startswith "$_dns_root_d" "*."; then
  3287. _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')"
  3288. fi
  3289. _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
  3290. _alias_index="$(_math "$_alias_index" + 1)"
  3291. _debug "_d_alias" "$_d_alias"
  3292. if [ "$_d_alias" ]; then
  3293. if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
  3294. txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
  3295. else
  3296. txtdomain="_acme-challenge.$_d_alias"
  3297. fi
  3298. else
  3299. txtdomain="_acme-challenge.$_dns_root_d"
  3300. fi
  3301. _debug txtdomain "$txtdomain"
  3302. txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
  3303. _debug txt "$txt"
  3304. d_api="$(_findHook "$_dns_root_d" dnsapi "$_currentRoot")"
  3305. _debug d_api "$d_api"
  3306. if [ "$d_api" ]; then
  3307. _info "Found domain api file: $d_api"
  3308. else
  3309. if [ "$_currentRoot" != "$W_DNS" ]; then
  3310. _err "Can not find dns api hook for: $_currentRoot"
  3311. _info "You need to add the txt record manually."
  3312. fi
  3313. _info "$(__red "Add the following TXT record:")"
  3314. _info "$(__red "Domain: '$(__green "$txtdomain")'")"
  3315. _info "$(__red "TXT value: '$(__green "$txt")'")"
  3316. _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")"
  3317. _info "$(__red "so the resulting subdomain will be: $txtdomain")"
  3318. continue
  3319. fi
  3320. (
  3321. if ! . "$d_api"; then
  3322. _err "Load file $d_api error. Please check your api file and try again."
  3323. return 1
  3324. fi
  3325. addcommand="${_currentRoot}_add"
  3326. if ! _exists "$addcommand"; then
  3327. _err "It seems that your api file is not correct, it must have a function named: $addcommand"
  3328. return 1
  3329. fi
  3330. if ! $addcommand "$txtdomain" "$txt"; then
  3331. _err "Error add txt for domain:$txtdomain"
  3332. return 1
  3333. fi
  3334. )
  3335. if [ "$?" != "0" ]; then
  3336. _clearup
  3337. _on_issue_err "$_post_hook" "$vlist"
  3338. return 1
  3339. fi
  3340. dnsadded='1'
  3341. fi
  3342. done
  3343. if [ "$dnsadded" = '0' ]; then
  3344. _savedomainconf "Le_Vlist" "$vlist"
  3345. _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
  3346. _err "Please add the TXT records to the domains, and re-run with --renew."
  3347. _clearup
  3348. _on_issue_err "$_post_hook"
  3349. return 1
  3350. fi
  3351. fi
  3352. if [ "$dnsadded" = '1' ]; then
  3353. if [ -z "$Le_DNSSleep" ]; then
  3354. Le_DNSSleep="$DEFAULT_DNS_SLEEP"
  3355. else
  3356. _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
  3357. fi
  3358. _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
  3359. _sleep "$Le_DNSSleep"
  3360. fi
  3361. NGINX_RESTORE_VLIST=""
  3362. _debug "ok, let's start to verify"
  3363. _ncIndex=1
  3364. ventries=$(echo "$vlist" | tr "$dvsep" ' ')
  3365. for ventry in $ventries; do
  3366. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  3367. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  3368. uri=$(echo "$ventry" | cut -d "$sep" -f 3)
  3369. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  3370. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  3371. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  3372. _info "$d is already verified, skip $vtype."
  3373. continue
  3374. fi
  3375. _info "Verifying:$d"
  3376. _debug "d" "$d"
  3377. _debug "keyauthorization" "$keyauthorization"
  3378. _debug "uri" "$uri"
  3379. removelevel=""
  3380. token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
  3381. _debug "_currentRoot" "$_currentRoot"
  3382. if [ "$vtype" = "$VTYPE_HTTP" ]; then
  3383. if [ "$_currentRoot" = "$NO_VALUE" ]; then
  3384. _info "Standalone mode server"
  3385. _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
  3386. _ncIndex="$(_math $_ncIndex + 1)"
  3387. _startserver "$keyauthorization" "$_ncaddr"
  3388. if [ "$?" != "0" ]; then
  3389. _clearup
  3390. _on_issue_err "$_post_hook" "$vlist"
  3391. return 1
  3392. fi
  3393. sleep 1
  3394. _debug serverproc "$serverproc"
  3395. elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
  3396. _info "Stateless mode for domain:$d"
  3397. _sleep 1
  3398. elif _startswith "$_currentRoot" "$NGINX"; then
  3399. _info "Nginx mode for domain:$d"
  3400. #set up nginx server
  3401. FOUND_REAL_NGINX_CONF=""
  3402. BACKUP_NGINX_CONF=""
  3403. if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then
  3404. _clearup
  3405. _on_issue_err "$_post_hook" "$vlist"
  3406. return 1
  3407. fi
  3408. if [ "$FOUND_REAL_NGINX_CONF" ]; then
  3409. _realConf="$FOUND_REAL_NGINX_CONF"
  3410. _backup="$BACKUP_NGINX_CONF"
  3411. _debug _realConf "$_realConf"
  3412. NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
  3413. fi
  3414. _sleep 1
  3415. else
  3416. if [ "$_currentRoot" = "apache" ]; then
  3417. wellknown_path="$ACME_DIR"
  3418. else
  3419. wellknown_path="$_currentRoot/.well-known/acme-challenge"
  3420. if [ ! -d "$_currentRoot/.well-known" ]; then
  3421. removelevel='1'
  3422. elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
  3423. removelevel='2'
  3424. else
  3425. removelevel='3'
  3426. fi
  3427. fi
  3428. _debug wellknown_path "$wellknown_path"
  3429. _debug "writing token:$token to $wellknown_path/$token"
  3430. mkdir -p "$wellknown_path"
  3431. if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
  3432. _err "$d:Can not write token to file : $wellknown_path/$token"
  3433. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3434. _clearup
  3435. _on_issue_err "$_post_hook" "$vlist"
  3436. return 1
  3437. fi
  3438. if [ ! "$usingApache" ]; then
  3439. if webroot_owner=$(_stat "$_currentRoot"); then
  3440. _debug "Changing owner/group of .well-known to $webroot_owner"
  3441. if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then
  3442. _debug "$(cat "$_EXEC_TEMP_ERR")"
  3443. _exec_err >/dev/null 2>&1
  3444. fi
  3445. else
  3446. _debug "not changing owner/group of webroot"
  3447. fi
  3448. fi
  3449. fi
  3450. elif [ "$vtype" = "$VTYPE_TLS" ]; then
  3451. #create A
  3452. #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
  3453. #_debug2 _hash_A "$_hash_A"
  3454. #_x="$(echo $_hash_A | cut -c 1-32)"
  3455. #_debug2 _x "$_x"
  3456. #_y="$(echo $_hash_A | cut -c 33-64)"
  3457. #_debug2 _y "$_y"
  3458. #_SAN_A="$_x.$_y.token.acme.invalid"
  3459. #_debug2 _SAN_A "$_SAN_A"
  3460. #create B
  3461. _hash_B="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
  3462. _debug2 _hash_B "$_hash_B"
  3463. _x="$(echo "$_hash_B" | cut -c 1-32)"
  3464. _debug2 _x "$_x"
  3465. _y="$(echo "$_hash_B" | cut -c 33-64)"
  3466. _debug2 _y "$_y"
  3467. #_SAN_B="$_x.$_y.ka.acme.invalid"
  3468. _SAN_B="$_x.$_y.acme.invalid"
  3469. _debug2 _SAN_B "$_SAN_B"
  3470. _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
  3471. _ncIndex="$(_math "$_ncIndex" + 1)"
  3472. if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
  3473. _err "Start tls server error."
  3474. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3475. _clearup
  3476. _on_issue_err "$_post_hook" "$vlist"
  3477. return 1
  3478. fi
  3479. fi
  3480. if ! __trigger_validation "$uri" "$keyauthorization"; then
  3481. _err "$d:Can not get challenge: $response"
  3482. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3483. _clearup
  3484. _on_issue_err "$_post_hook" "$vlist"
  3485. return 1
  3486. fi
  3487. if [ "$code" ] && [ "$code" != '202' ]; then
  3488. if [ "$ACME_VERSION" = "2" ] && [ "$code" = '200' ]; then
  3489. _debug "trigger validation code: $code"
  3490. else
  3491. _err "$d:Challenge error: $response"
  3492. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3493. _clearup
  3494. _on_issue_err "$_post_hook" "$vlist"
  3495. return 1
  3496. fi
  3497. fi
  3498. waittimes=0
  3499. if [ -z "$MAX_RETRY_TIMES" ]; then
  3500. MAX_RETRY_TIMES=30
  3501. fi
  3502. while true; do
  3503. waittimes=$(_math "$waittimes" + 1)
  3504. if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
  3505. _err "$d:Timeout"
  3506. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3507. _clearup
  3508. _on_issue_err "$_post_hook" "$vlist"
  3509. return 1
  3510. fi
  3511. _debug "sleep 2 secs to verify"
  3512. sleep 2
  3513. _debug "checking"
  3514. response="$(_get "$uri")"
  3515. if [ "$?" != "0" ]; then
  3516. _err "$d:Verify error:$response"
  3517. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3518. _clearup
  3519. _on_issue_err "$_post_hook" "$vlist"
  3520. return 1
  3521. fi
  3522. _debug2 original "$response"
  3523. response="$(echo "$response" | _normalizeJson)"
  3524. _debug2 response "$response"
  3525. status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
  3526. if [ "$status" = "valid" ]; then
  3527. _info "$(__green Success)"
  3528. _stopserver "$serverproc"
  3529. serverproc=""
  3530. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3531. break
  3532. fi
  3533. if [ "$status" = "invalid" ]; then
  3534. error="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error":\{[^\}]*')"
  3535. _debug2 error "$error"
  3536. errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
  3537. _debug2 errordetail "$errordetail"
  3538. if [ "$errordetail" ]; then
  3539. _err "$d:Verify error:$errordetail"
  3540. else
  3541. _err "$d:Verify error:$error"
  3542. fi
  3543. if [ "$DEBUG" ]; then
  3544. if [ "$vtype" = "$VTYPE_HTTP" ]; then
  3545. _debug "Debug: get token url."
  3546. _get "http://$d/.well-known/acme-challenge/$token" "" 1
  3547. fi
  3548. fi
  3549. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3550. _clearup
  3551. _on_issue_err "$_post_hook" "$vlist"
  3552. return 1
  3553. fi
  3554. if [ "$status" = "pending" ]; then
  3555. _info "Pending"
  3556. else
  3557. _err "$d:Verify error:$response"
  3558. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3559. _clearup
  3560. _on_issue_err "$_post_hook" "$vlist"
  3561. return 1
  3562. fi
  3563. done
  3564. done
  3565. _clearup
  3566. _info "Verify finished, start to sign."
  3567. der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
  3568. if [ "$ACME_VERSION" = "2" ]; then
  3569. if ! _send_signed_request "${Le_OrderFinalize}" "{\"csr\": \"$der\"}"; then
  3570. _err "Sign failed."
  3571. _on_issue_err "$_post_hook"
  3572. return 1
  3573. fi
  3574. if [ "$code" != "200" ]; then
  3575. _err "Sign failed, code is not 200."
  3576. _err "$response"
  3577. _on_issue_err "$_post_hook"
  3578. return 1
  3579. fi
  3580. Le_LinkCert="$(echo "$response" | tr -d '\r\n' | _egrep_o '"certificate" *: *"[^"]*"' | cut -d '"' -f 4)"
  3581. if ! _get "$Le_LinkCert" >"$CERT_PATH"; then
  3582. _err "Sign failed, can not download cert:$Le_LinkCert."
  3583. _err "$response"
  3584. _on_issue_err "$_post_hook"
  3585. return 1
  3586. fi
  3587. if [ "$(grep -- "$BEGIN_CERT" "$CERT_PATH" | wc -l)" -gt "1" ]; then
  3588. _debug "Found cert chain"
  3589. cat "$CERT_PATH" >"$CERT_FULLCHAIN_PATH"
  3590. _end_n="$(grep -n -- "$END_CERT" "$CERT_FULLCHAIN_PATH" | _head_n 1 | cut -d : -f 1)"
  3591. _debug _end_n "$_end_n"
  3592. sed -n "1,${_end_n}p" "$CERT_FULLCHAIN_PATH" >"$CERT_PATH"
  3593. _end_n="$(_math $_end_n + 1)"
  3594. sed -n "${_end_n},9999p" "$CERT_FULLCHAIN_PATH" >"$CA_CERT_PATH"
  3595. fi
  3596. else
  3597. if ! _send_signed_request "${ACME_NEW_ORDER}" "{\"resource\": \"$ACME_NEW_ORDER_RES\", \"csr\": \"$der\"}" "needbase64"; then
  3598. _err "Sign failed. $response"
  3599. _on_issue_err "$_post_hook"
  3600. return 1
  3601. fi
  3602. _rcert="$response"
  3603. Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _tail_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
  3604. echo "$BEGIN_CERT" >"$CERT_PATH"
  3605. #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
  3606. # _debug "Get cert failed. Let's try last response."
  3607. # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
  3608. #fi
  3609. if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
  3610. _debug "Try cert link."
  3611. _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
  3612. fi
  3613. echo "$END_CERT" >>"$CERT_PATH"
  3614. fi
  3615. _debug "Le_LinkCert" "$Le_LinkCert"
  3616. _savedomainconf "Le_LinkCert" "$Le_LinkCert"
  3617. if [ -z "$Le_LinkCert" ] || ! _checkcert "$CERT_PATH"; then
  3618. response="$(echo "$response" | _dbase64 "multiline" | tr -d '\0' | _normalizeJson)"
  3619. _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
  3620. _on_issue_err "$_post_hook"
  3621. return 1
  3622. fi
  3623. if [ "$Le_LinkCert" ]; then
  3624. _info "$(__green "Cert success.")"
  3625. cat "$CERT_PATH"
  3626. _info "Your cert is in $(__green " $CERT_PATH ")"
  3627. if [ -f "$CERT_KEY_PATH" ]; then
  3628. _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
  3629. fi
  3630. if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
  3631. USER_PATH="$PATH"
  3632. _saveaccountconf "USER_PATH" "$USER_PATH"
  3633. fi
  3634. fi
  3635. _cleardomainconf "Le_Vlist"
  3636. if [ "$ACME_VERSION" = "2" ]; then
  3637. _debug "v2 chain."
  3638. else
  3639. cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
  3640. Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
  3641. if [ "$Le_LinkIssuer" ]; then
  3642. if ! _contains "$Le_LinkIssuer" ":"; then
  3643. _info "$(__red "Relative issuer link found.")"
  3644. Le_LinkIssuer="$_ACME_SERVER_HOST$Le_LinkIssuer"
  3645. fi
  3646. _debug Le_LinkIssuer "$Le_LinkIssuer"
  3647. _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
  3648. _link_issuer_retry=0
  3649. _MAX_ISSUER_RETRY=5
  3650. while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
  3651. _debug _link_issuer_retry "$_link_issuer_retry"
  3652. if [ "$ACME_VERSION" = "2" ]; then
  3653. if _get "$Le_LinkIssuer" >"$CA_CERT_PATH"; then
  3654. break
  3655. fi
  3656. else
  3657. if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then
  3658. echo "$BEGIN_CERT" >"$CA_CERT_PATH"
  3659. _base64 "multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH"
  3660. echo "$END_CERT" >>"$CA_CERT_PATH"
  3661. if ! _checkcert "$CA_CERT_PATH"; then
  3662. _err "Can not get the ca cert."
  3663. break
  3664. fi
  3665. cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
  3666. rm -f "$CA_CERT_PATH.der"
  3667. break
  3668. fi
  3669. fi
  3670. _link_issuer_retry=$(_math $_link_issuer_retry + 1)
  3671. _sleep "$_link_issuer_retry"
  3672. done
  3673. if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
  3674. _err "Max retry for issuer ca cert is reached."
  3675. fi
  3676. else
  3677. _debug "No Le_LinkIssuer header found."
  3678. fi
  3679. fi
  3680. [ -f "$CA_CERT_PATH" ] && _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
  3681. [ -f "$CERT_FULLCHAIN_PATH" ] && _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
  3682. Le_CertCreateTime=$(_time)
  3683. _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
  3684. Le_CertCreateTimeStr=$(date -u)
  3685. _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
  3686. if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
  3687. Le_RenewalDays="$MAX_RENEW"
  3688. else
  3689. _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
  3690. fi
  3691. if [ "$CA_BUNDLE" ]; then
  3692. _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
  3693. else
  3694. _clearaccountconf "CA_BUNDLE"
  3695. fi
  3696. if [ "$CA_PATH" ]; then
  3697. _saveaccountconf CA_PATH "$CA_PATH"
  3698. else
  3699. _clearaccountconf "CA_PATH"
  3700. fi
  3701. if [ "$HTTPS_INSECURE" ]; then
  3702. _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
  3703. else
  3704. _clearaccountconf "HTTPS_INSECURE"
  3705. fi
  3706. if [ "$Le_Listen_V4" ]; then
  3707. _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
  3708. _cleardomainconf Le_Listen_V6
  3709. elif [ "$Le_Listen_V6" ]; then
  3710. _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
  3711. _cleardomainconf Le_Listen_V4
  3712. fi
  3713. if [ "$Le_ForceNewDomainKey" = "1" ]; then
  3714. _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey"
  3715. else
  3716. _cleardomainconf Le_ForceNewDomainKey
  3717. fi
  3718. Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
  3719. Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
  3720. _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
  3721. Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
  3722. _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
  3723. if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
  3724. _savedomainconf "Le_RealCertPath" "$_real_cert"
  3725. _savedomainconf "Le_RealCACertPath" "$_real_ca"
  3726. _savedomainconf "Le_RealKeyPath" "$_real_key"
  3727. _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
  3728. _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
  3729. if ! _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"; then
  3730. return 1
  3731. fi
  3732. fi
  3733. if ! _on_issue_success "$_post_hook" "$_renew_hook"; then
  3734. _err "Call hook error."
  3735. return 1
  3736. fi
  3737. }
  3738. #domain [isEcc]
  3739. renew() {
  3740. Le_Domain="$1"
  3741. if [ -z "$Le_Domain" ]; then
  3742. _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
  3743. return 1
  3744. fi
  3745. _isEcc="$2"
  3746. _initpath "$Le_Domain" "$_isEcc"
  3747. _info "$(__green "Renew: '$Le_Domain'")"
  3748. if [ ! -f "$DOMAIN_CONF" ]; then
  3749. _info "'$Le_Domain' is not a issued domain, skip."
  3750. return 0
  3751. fi
  3752. if [ "$Le_RenewalDays" ]; then
  3753. _savedomainconf Le_RenewalDays "$Le_RenewalDays"
  3754. fi
  3755. . "$DOMAIN_CONF"
  3756. _debug Le_API "$Le_API"
  3757. if [ "$Le_API" ]; then
  3758. if [ "$_OLD_CA_HOST" = "$Le_API" ]; then
  3759. export Le_API="$DEFAULT_CA"
  3760. _savedomainconf Le_API "$Le_API"
  3761. fi
  3762. if [ "$_OLD_STAGE_CA_HOST" = "$Le_API" ]; then
  3763. export Le_API="$DEFAULT_STAGING_CA"
  3764. _savedomainconf Le_API "$Le_API"
  3765. fi
  3766. export ACME_DIRECTORY="$Le_API"
  3767. #reload ca configs
  3768. ACCOUNT_KEY_PATH=""
  3769. ACCOUNT_JSON_PATH=""
  3770. CA_CONF=""
  3771. _debug3 "initpath again."
  3772. _initpath "$Le_Domain" "$_isEcc"
  3773. fi
  3774. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
  3775. _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
  3776. _info "Add '$(__red '--force')' to force to renew."
  3777. return "$RENEW_SKIP"
  3778. fi
  3779. if [ "$IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then
  3780. _info "Skip invalid cert for: $Le_Domain"
  3781. return 0
  3782. fi
  3783. IS_RENEW="1"
  3784. issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" "$Le_PreHook" "$Le_PostHook" "$Le_RenewHook" "$Le_LocalAddress" "$Le_ChallengeAlias"
  3785. res="$?"
  3786. if [ "$res" != "0" ]; then
  3787. return "$res"
  3788. fi
  3789. if [ "$Le_DeployHook" ]; then
  3790. _deploy "$Le_Domain" "$Le_DeployHook"
  3791. res="$?"
  3792. fi
  3793. IS_RENEW=""
  3794. return "$res"
  3795. }
  3796. #renewAll [stopRenewOnError]
  3797. renewAll() {
  3798. _initpath
  3799. _stopRenewOnError="$1"
  3800. _debug "_stopRenewOnError" "$_stopRenewOnError"
  3801. _ret="0"
  3802. for di in "${CERT_HOME}"/*.*/; do
  3803. _debug di "$di"
  3804. if ! [ -d "$di" ]; then
  3805. _debug "Not directory, skip: $di"
  3806. continue
  3807. fi
  3808. d=$(basename "$di")
  3809. _debug d "$d"
  3810. (
  3811. if _endswith "$d" "$ECC_SUFFIX"; then
  3812. _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
  3813. d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
  3814. fi
  3815. renew "$d" "$_isEcc"
  3816. )
  3817. rc="$?"
  3818. _debug "Return code: $rc"
  3819. if [ "$rc" != "0" ]; then
  3820. if [ "$rc" = "$RENEW_SKIP" ]; then
  3821. _info "Skipped $d"
  3822. elif [ "$_stopRenewOnError" ]; then
  3823. _err "Error renew $d, stop now."
  3824. return "$rc"
  3825. else
  3826. _ret="$rc"
  3827. _err "Error renew $d."
  3828. fi
  3829. fi
  3830. done
  3831. return "$_ret"
  3832. }
  3833. #csr webroot
  3834. signcsr() {
  3835. _csrfile="$1"
  3836. _csrW="$2"
  3837. if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
  3838. _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
  3839. return 1
  3840. fi
  3841. _real_cert="$3"
  3842. _real_key="$4"
  3843. _real_ca="$5"
  3844. _reload_cmd="$6"
  3845. _real_fullchain="$7"
  3846. _pre_hook="${8}"
  3847. _post_hook="${9}"
  3848. _renew_hook="${10}"
  3849. _local_addr="${11}"
  3850. _challenge_alias="${12}"
  3851. _csrsubj=$(_readSubjectFromCSR "$_csrfile")
  3852. if [ "$?" != "0" ]; then
  3853. _err "Can not read subject from csr: $_csrfile"
  3854. return 1
  3855. fi
  3856. _debug _csrsubj "$_csrsubj"
  3857. if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then
  3858. _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it."
  3859. _csrsubj=""
  3860. fi
  3861. _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
  3862. if [ "$?" != "0" ]; then
  3863. _err "Can not read domain list from csr: $_csrfile"
  3864. return 1
  3865. fi
  3866. _debug "_csrdomainlist" "$_csrdomainlist"
  3867. if [ -z "$_csrsubj" ]; then
  3868. _csrsubj="$(_getfield "$_csrdomainlist" 1)"
  3869. _debug _csrsubj "$_csrsubj"
  3870. _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
  3871. _debug "_csrdomainlist" "$_csrdomainlist"
  3872. fi
  3873. if [ -z "$_csrsubj" ]; then
  3874. _err "Can not read subject from csr: $_csrfile"
  3875. return 1
  3876. fi
  3877. _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
  3878. if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
  3879. _err "Can not read key length from csr: $_csrfile"
  3880. return 1
  3881. fi
  3882. if [ -z "$ACME_VERSION" ] && _contains "$_csrsubj,$_csrdomainlist" "*."; then
  3883. export ACME_VERSION=2
  3884. fi
  3885. _initpath "$_csrsubj" "$_csrkeylength"
  3886. mkdir -p "$DOMAIN_PATH"
  3887. _info "Copy csr to: $CSR_PATH"
  3888. cp "$_csrfile" "$CSR_PATH"
  3889. issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength" "$_real_cert" "$_real_key" "$_real_ca" "$_reload_cmd" "$_real_fullchain" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_addr" "$_challenge_alias"
  3890. }
  3891. showcsr() {
  3892. _csrfile="$1"
  3893. _csrd="$2"
  3894. if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
  3895. _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
  3896. return 1
  3897. fi
  3898. _initpath
  3899. _csrsubj=$(_readSubjectFromCSR "$_csrfile")
  3900. if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
  3901. _err "Can not read subject from csr: $_csrfile"
  3902. return 1
  3903. fi
  3904. _info "Subject=$_csrsubj"
  3905. _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
  3906. if [ "$?" != "0" ]; then
  3907. _err "Can not read domain list from csr: $_csrfile"
  3908. return 1
  3909. fi
  3910. _debug "_csrdomainlist" "$_csrdomainlist"
  3911. _info "SubjectAltNames=$_csrdomainlist"
  3912. _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
  3913. if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
  3914. _err "Can not read key length from csr: $_csrfile"
  3915. return 1
  3916. fi
  3917. _info "KeyLength=$_csrkeylength"
  3918. }
  3919. list() {
  3920. _raw="$1"
  3921. _initpath
  3922. _sep="|"
  3923. if [ "$_raw" ]; then
  3924. printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
  3925. for di in "${CERT_HOME}"/*.*/; do
  3926. if ! [ -d "$di" ]; then
  3927. _debug "Not directory, skip: $di"
  3928. continue
  3929. fi
  3930. d=$(basename "$di")
  3931. _debug d "$d"
  3932. (
  3933. if _endswith "$d" "$ECC_SUFFIX"; then
  3934. _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
  3935. d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
  3936. fi
  3937. _initpath "$d" "$_isEcc"
  3938. if [ -f "$DOMAIN_CONF" ]; then
  3939. . "$DOMAIN_CONF"
  3940. printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
  3941. fi
  3942. )
  3943. done
  3944. else
  3945. if _exists column; then
  3946. list "raw" | column -t -s "$_sep"
  3947. else
  3948. list "raw" | tr "$_sep" '\t'
  3949. fi
  3950. fi
  3951. }
  3952. _deploy() {
  3953. _d="$1"
  3954. _hooks="$2"
  3955. for _d_api in $(echo "$_hooks" | tr ',' " "); do
  3956. _deployApi="$(_findHook "$_d" deploy "$_d_api")"
  3957. if [ -z "$_deployApi" ]; then
  3958. _err "The deploy hook $_d_api is not found."
  3959. return 1
  3960. fi
  3961. _debug _deployApi "$_deployApi"
  3962. if ! (
  3963. if ! . "$_deployApi"; then
  3964. _err "Load file $_deployApi error. Please check your api file and try again."
  3965. return 1
  3966. fi
  3967. d_command="${_d_api}_deploy"
  3968. if ! _exists "$d_command"; then
  3969. _err "It seems that your api file is not correct, it must have a function named: $d_command"
  3970. return 1
  3971. fi
  3972. if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
  3973. _err "Error deploy for domain:$_d"
  3974. return 1
  3975. fi
  3976. ); then
  3977. _err "Deploy error."
  3978. return 1
  3979. else
  3980. _info "$(__green Success)"
  3981. fi
  3982. done
  3983. }
  3984. #domain hooks
  3985. deploy() {
  3986. _d="$1"
  3987. _hooks="$2"
  3988. _isEcc="$3"
  3989. if [ -z "$_hooks" ]; then
  3990. _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
  3991. return 1
  3992. fi
  3993. _initpath "$_d" "$_isEcc"
  3994. if [ ! -d "$DOMAIN_PATH" ]; then
  3995. _err "The domain '$_d' is not a cert name. You must use the cert name to specify the cert to install."
  3996. _err "Can not find path:'$DOMAIN_PATH'"
  3997. return 1
  3998. fi
  3999. . "$DOMAIN_CONF"
  4000. _savedomainconf Le_DeployHook "$_hooks"
  4001. _deploy "$_d" "$_hooks"
  4002. }
  4003. installcert() {
  4004. _main_domain="$1"
  4005. if [ -z "$_main_domain" ]; then
  4006. _usage "Usage: $PROJECT_ENTRY --installcert -d domain.com [--ecc] [--cert-file cert-file-path] [--key-file key-file-path] [--ca-file ca-cert-file-path] [ --reloadCmd reloadCmd] [--fullchain-file fullchain-path]"
  4007. return 1
  4008. fi
  4009. _real_cert="$2"
  4010. _real_key="$3"
  4011. _real_ca="$4"
  4012. _reload_cmd="$5"
  4013. _real_fullchain="$6"
  4014. _isEcc="$7"
  4015. _initpath "$_main_domain" "$_isEcc"
  4016. if [ ! -d "$DOMAIN_PATH" ]; then
  4017. _err "The domain '$_main_domain' is not a cert name. You must use the cert name to specify the cert to install."
  4018. _err "Can not find path:'$DOMAIN_PATH'"
  4019. return 1
  4020. fi
  4021. _savedomainconf "Le_RealCertPath" "$_real_cert"
  4022. _savedomainconf "Le_RealCACertPath" "$_real_ca"
  4023. _savedomainconf "Le_RealKeyPath" "$_real_key"
  4024. _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
  4025. _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
  4026. _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
  4027. }
  4028. #domain cert key ca fullchain reloadcmd backup-prefix
  4029. _installcert() {
  4030. _main_domain="$1"
  4031. _real_cert="$2"
  4032. _real_key="$3"
  4033. _real_ca="$4"
  4034. _real_fullchain="$5"
  4035. _reload_cmd="$6"
  4036. _backup_prefix="$7"
  4037. if [ "$_real_cert" = "$NO_VALUE" ]; then
  4038. _real_cert=""
  4039. fi
  4040. if [ "$_real_key" = "$NO_VALUE" ]; then
  4041. _real_key=""
  4042. fi
  4043. if [ "$_real_ca" = "$NO_VALUE" ]; then
  4044. _real_ca=""
  4045. fi
  4046. if [ "$_reload_cmd" = "$NO_VALUE" ]; then
  4047. _reload_cmd=""
  4048. fi
  4049. if [ "$_real_fullchain" = "$NO_VALUE" ]; then
  4050. _real_fullchain=""
  4051. fi
  4052. _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
  4053. mkdir -p "$_backup_path"
  4054. if [ "$_real_cert" ]; then
  4055. _info "Installing cert to:$_real_cert"
  4056. if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
  4057. cp "$_real_cert" "$_backup_path/cert.bak"
  4058. fi
  4059. cat "$CERT_PATH" >"$_real_cert" || return 1
  4060. fi
  4061. if [ "$_real_ca" ]; then
  4062. _info "Installing CA to:$_real_ca"
  4063. if [ "$_real_ca" = "$_real_cert" ]; then
  4064. echo "" >>"$_real_ca"
  4065. cat "$CA_CERT_PATH" >>"$_real_ca" || return 1
  4066. else
  4067. if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
  4068. cp "$_real_ca" "$_backup_path/ca.bak"
  4069. fi
  4070. cat "$CA_CERT_PATH" >"$_real_ca" || return 1
  4071. fi
  4072. fi
  4073. if [ "$_real_key" ]; then
  4074. _info "Installing key to:$_real_key"
  4075. if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
  4076. cp "$_real_key" "$_backup_path/key.bak"
  4077. fi
  4078. if [ -f "$_real_key" ]; then
  4079. cat "$CERT_KEY_PATH" >"$_real_key" || return 1
  4080. else
  4081. cat "$CERT_KEY_PATH" >"$_real_key" || return 1
  4082. chmod 600 "$_real_key"
  4083. fi
  4084. fi
  4085. if [ "$_real_fullchain" ]; then
  4086. _info "Installing full chain to:$_real_fullchain"
  4087. if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
  4088. cp "$_real_fullchain" "$_backup_path/fullchain.bak"
  4089. fi
  4090. cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain" || return 1
  4091. fi
  4092. if [ "$_reload_cmd" ]; then
  4093. _info "Run reload cmd: $_reload_cmd"
  4094. if (
  4095. export CERT_PATH
  4096. export CERT_KEY_PATH
  4097. export CA_CERT_PATH
  4098. export CERT_FULLCHAIN_PATH
  4099. export Le_Domain
  4100. cd "$DOMAIN_PATH" && eval "$_reload_cmd"
  4101. ); then
  4102. _info "$(__green "Reload success")"
  4103. else
  4104. _err "Reload error for :$Le_Domain"
  4105. fi
  4106. fi
  4107. }
  4108. #confighome
  4109. installcronjob() {
  4110. _c_home="$1"
  4111. _initpath
  4112. _CRONTAB="crontab"
  4113. if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
  4114. _CRONTAB="fcrontab"
  4115. fi
  4116. if ! _exists "$_CRONTAB"; then
  4117. _err "crontab/fcrontab doesn't exist, so, we can not install cron jobs."
  4118. _err "All your certs will not be renewed automatically."
  4119. _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
  4120. return 1
  4121. fi
  4122. _info "Installing cron job"
  4123. if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then
  4124. if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
  4125. lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
  4126. else
  4127. _err "Can not install cronjob, $PROJECT_ENTRY not found."
  4128. return 1
  4129. fi
  4130. if [ "$_c_home" ]; then
  4131. _c_entry="--config-home \"$_c_home\" "
  4132. fi
  4133. _t=$(_time)
  4134. random_minute=$(_math $_t % 60)
  4135. if _exists uname && uname -a | grep SunOS >/dev/null; then
  4136. $_CRONTAB -l | {
  4137. cat
  4138. echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
  4139. } | $_CRONTAB --
  4140. else
  4141. $_CRONTAB -l | {
  4142. cat
  4143. echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
  4144. } | $_CRONTAB -
  4145. fi
  4146. fi
  4147. if [ "$?" != "0" ]; then
  4148. _err "Install cron job failed. You need to manually renew your certs."
  4149. _err "Or you can add cronjob by yourself:"
  4150. _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
  4151. return 1
  4152. fi
  4153. }
  4154. uninstallcronjob() {
  4155. _CRONTAB="crontab"
  4156. if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
  4157. _CRONTAB="fcrontab"
  4158. fi
  4159. if ! _exists "$_CRONTAB"; then
  4160. return
  4161. fi
  4162. _info "Removing cron job"
  4163. cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")"
  4164. if [ "$cr" ]; then
  4165. if _exists uname && uname -a | grep solaris >/dev/null; then
  4166. $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB --
  4167. else
  4168. $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -
  4169. fi
  4170. LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
  4171. _info LE_WORKING_DIR "$LE_WORKING_DIR"
  4172. if _contains "$cr" "--config-home"; then
  4173. LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
  4174. _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
  4175. fi
  4176. fi
  4177. _initpath
  4178. }
  4179. revoke() {
  4180. Le_Domain="$1"
  4181. if [ -z "$Le_Domain" ]; then
  4182. _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]"
  4183. return 1
  4184. fi
  4185. _isEcc="$2"
  4186. _initpath "$Le_Domain" "$_isEcc"
  4187. if [ ! -f "$DOMAIN_CONF" ]; then
  4188. _err "$Le_Domain is not a issued domain, skip."
  4189. return 1
  4190. fi
  4191. if [ ! -f "$CERT_PATH" ]; then
  4192. _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
  4193. return 1
  4194. fi
  4195. cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
  4196. if [ -z "$cert" ]; then
  4197. _err "Cert for $Le_Domain is empty found, skip."
  4198. return 1
  4199. fi
  4200. _initAPI
  4201. if [ "$ACME_VERSION" = "2" ]; then
  4202. data="{\"certificate\": \"$cert\"}"
  4203. else
  4204. data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
  4205. fi
  4206. uri="${ACME_REVOKE_CERT}"
  4207. if [ -f "$CERT_KEY_PATH" ]; then
  4208. _info "Try domain key first."
  4209. if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
  4210. if [ -z "$response" ]; then
  4211. _info "Revoke success."
  4212. rm -f "$CERT_PATH"
  4213. return 0
  4214. else
  4215. _err "Revoke error by domain key."
  4216. _err "$response"
  4217. fi
  4218. fi
  4219. else
  4220. _info "Domain key file doesn't exists."
  4221. fi
  4222. _info "Try account key."
  4223. if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
  4224. if [ -z "$response" ]; then
  4225. _info "Revoke success."
  4226. rm -f "$CERT_PATH"
  4227. return 0
  4228. else
  4229. _err "Revoke error."
  4230. _debug "$response"
  4231. fi
  4232. fi
  4233. return 1
  4234. }
  4235. #domain ecc
  4236. remove() {
  4237. Le_Domain="$1"
  4238. if [ -z "$Le_Domain" ]; then
  4239. _usage "Usage: $PROJECT_ENTRY --remove -d domain.com [--ecc]"
  4240. return 1
  4241. fi
  4242. _isEcc="$2"
  4243. _initpath "$Le_Domain" "$_isEcc"
  4244. _removed_conf="$DOMAIN_CONF.removed"
  4245. if [ ! -f "$DOMAIN_CONF" ]; then
  4246. if [ -f "$_removed_conf" ]; then
  4247. _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH"
  4248. else
  4249. _err "$Le_Domain is not a issued domain, skip."
  4250. fi
  4251. return 1
  4252. fi
  4253. if mv "$DOMAIN_CONF" "$_removed_conf"; then
  4254. _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)"
  4255. _info "You can remove them by yourself."
  4256. return 0
  4257. else
  4258. _err "Remove $Le_Domain failed."
  4259. return 1
  4260. fi
  4261. }
  4262. #domain vtype
  4263. _deactivate() {
  4264. _d_domain="$1"
  4265. _d_type="$2"
  4266. _initpath
  4267. if [ "$ACME_VERSION" = "2" ]; then
  4268. _identifiers="{\"type\":\"dns\",\"value\":\"$_d_domain\"}"
  4269. if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
  4270. _err "Can not get domain new order."
  4271. return 1
  4272. fi
  4273. _authorizations_seg="$(echo "$response" | tr -d '\r\n' | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
  4274. _debug2 _authorizations_seg "$_authorizations_seg"
  4275. if [ -z "$_authorizations_seg" ]; then
  4276. _err "_authorizations_seg not found."
  4277. _clearup
  4278. _on_issue_err "$_post_hook"
  4279. return 1
  4280. fi
  4281. authzUri="$_authorizations_seg"
  4282. _debug2 "authzUri" "$authzUri"
  4283. if ! response="$(_get "$authzUri")"; then
  4284. _err "get to authz error."
  4285. _err "_authorizations_seg" "$_authorizations_seg"
  4286. _err "authzUri" "$authzUri"
  4287. _clearup
  4288. _on_issue_err "$_post_hook"
  4289. return 1
  4290. fi
  4291. response="$(echo "$response" | _normalizeJson)"
  4292. _debug2 response "$response"
  4293. _URL_NAME="url"
  4294. else
  4295. if ! __get_domain_new_authz "$_d_domain"; then
  4296. _err "Can not get domain new authz token."
  4297. return 1
  4298. fi
  4299. authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
  4300. _debug "authzUri" "$authzUri"
  4301. if [ "$code" ] && [ ! "$code" = '201' ]; then
  4302. _err "new-authz error: $response"
  4303. return 1
  4304. fi
  4305. _URL_NAME="uri"
  4306. fi
  4307. entries="$(echo "$response" | _egrep_o "{ *\"type\":\"[^\"]*\", *\"status\": *\"valid\", *\"$_URL_NAME\"[^}]*")"
  4308. if [ -z "$entries" ]; then
  4309. _info "No valid entries found."
  4310. if [ -z "$thumbprint" ]; then
  4311. thumbprint="$(__calc_account_thumbprint)"
  4312. fi
  4313. _debug "Trigger validation."
  4314. vtype="$VTYPE_DNS"
  4315. entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
  4316. _debug entry "$entry"
  4317. if [ -z "$entry" ]; then
  4318. _err "Error, can not get domain token $d"
  4319. return 1
  4320. fi
  4321. token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  4322. _debug token "$token"
  4323. uri="$(printf "%s\n" "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
  4324. _debug uri "$uri"
  4325. keyauthorization="$token.$thumbprint"
  4326. _debug keyauthorization "$keyauthorization"
  4327. __trigger_validation "$uri" "$keyauthorization"
  4328. fi
  4329. _d_i=0
  4330. _d_max_retry=$(echo "$entries" | wc -l)
  4331. while [ "$_d_i" -lt "$_d_max_retry" ]; do
  4332. _info "Deactivate: $_d_domain"
  4333. _d_i="$(_math $_d_i + 1)"
  4334. entry="$(echo "$entries" | sed -n "${_d_i}p")"
  4335. _debug entry "$entry"
  4336. if [ -z "$entry" ]; then
  4337. _info "No more valid entry found."
  4338. break
  4339. fi
  4340. _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
  4341. _debug _vtype "$_vtype"
  4342. _info "Found $_vtype"
  4343. uri="$(printf "%s\n" "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
  4344. _debug uri "$uri"
  4345. if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
  4346. _info "Skip $_vtype"
  4347. continue
  4348. fi
  4349. _info "Deactivate: $_vtype"
  4350. if [ "$ACME_VERSION" = "2" ]; then
  4351. _djson="{\"status\":\"deactivated\"}"
  4352. else
  4353. _djson="{\"resource\": \"authz\", \"status\":\"deactivated\"}"
  4354. fi
  4355. if _send_signed_request "$authzUri" "$_djson" && _contains "$response" '"deactivated"'; then
  4356. _info "Deactivate: $_vtype success."
  4357. else
  4358. _err "Can not deactivate $_vtype."
  4359. break
  4360. fi
  4361. done
  4362. _debug "$_d_i"
  4363. if [ "$_d_i" -eq "$_d_max_retry" ]; then
  4364. _info "Deactivated success!"
  4365. else
  4366. _err "Deactivate failed."
  4367. fi
  4368. }
  4369. deactivate() {
  4370. _d_domain_list="$1"
  4371. _d_type="$2"
  4372. _initpath
  4373. _initAPI
  4374. _debug _d_domain_list "$_d_domain_list"
  4375. if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
  4376. _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
  4377. return 1
  4378. fi
  4379. for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
  4380. if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
  4381. continue
  4382. fi
  4383. if ! _deactivate "$_d_dm" "$_d_type"; then
  4384. return 1
  4385. fi
  4386. done
  4387. }
  4388. # Detect profile file if not specified as environment variable
  4389. _detect_profile() {
  4390. if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
  4391. echo "$PROFILE"
  4392. return
  4393. fi
  4394. DETECTED_PROFILE=''
  4395. SHELLTYPE="$(basename "/$SHELL")"
  4396. if [ "$SHELLTYPE" = "bash" ]; then
  4397. if [ -f "$HOME/.bashrc" ]; then
  4398. DETECTED_PROFILE="$HOME/.bashrc"
  4399. elif [ -f "$HOME/.bash_profile" ]; then
  4400. DETECTED_PROFILE="$HOME/.bash_profile"
  4401. fi
  4402. elif [ "$SHELLTYPE" = "zsh" ]; then
  4403. DETECTED_PROFILE="$HOME/.zshrc"
  4404. fi
  4405. if [ -z "$DETECTED_PROFILE" ]; then
  4406. if [ -f "$HOME/.profile" ]; then
  4407. DETECTED_PROFILE="$HOME/.profile"
  4408. elif [ -f "$HOME/.bashrc" ]; then
  4409. DETECTED_PROFILE="$HOME/.bashrc"
  4410. elif [ -f "$HOME/.bash_profile" ]; then
  4411. DETECTED_PROFILE="$HOME/.bash_profile"
  4412. elif [ -f "$HOME/.zshrc" ]; then
  4413. DETECTED_PROFILE="$HOME/.zshrc"
  4414. fi
  4415. fi
  4416. echo "$DETECTED_PROFILE"
  4417. }
  4418. _initconf() {
  4419. _initpath
  4420. if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
  4421. echo "
  4422. #LOG_FILE=\"$DEFAULT_LOG_FILE\"
  4423. #LOG_LEVEL=1
  4424. #AUTO_UPGRADE=\"1\"
  4425. #NO_TIMESTAMP=1
  4426. " >"$ACCOUNT_CONF_PATH"
  4427. fi
  4428. }
  4429. # nocron
  4430. _precheck() {
  4431. _nocron="$1"
  4432. if ! _exists "curl" && ! _exists "wget"; then
  4433. _err "Please install curl or wget first, we need to access http resources."
  4434. return 1
  4435. fi
  4436. if [ -z "$_nocron" ]; then
  4437. if ! _exists "crontab" && ! _exists "fcrontab"; then
  4438. _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
  4439. _err "We need to set cron job to renew the certs automatically."
  4440. _err "Otherwise, your certs will not be able to be renewed automatically."
  4441. if [ -z "$FORCE" ]; then
  4442. _err "Please add '--force' and try install again to go without crontab."
  4443. _err "./$PROJECT_ENTRY --install --force"
  4444. return 1
  4445. fi
  4446. fi
  4447. fi
  4448. if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
  4449. _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
  4450. _err "We need openssl to generate keys."
  4451. return 1
  4452. fi
  4453. if ! _exists "socat"; then
  4454. _err "It is recommended to install socat first."
  4455. _err "We use socat for standalone server if you use standalone mode."
  4456. _err "If you don't use standalone mode, just ignore this warning."
  4457. fi
  4458. return 0
  4459. }
  4460. _setShebang() {
  4461. _file="$1"
  4462. _shebang="$2"
  4463. if [ -z "$_shebang" ]; then
  4464. _usage "Usage: file shebang"
  4465. return 1
  4466. fi
  4467. cp "$_file" "$_file.tmp"
  4468. echo "$_shebang" >"$_file"
  4469. sed -n 2,99999p "$_file.tmp" >>"$_file"
  4470. rm -f "$_file.tmp"
  4471. }
  4472. #confighome
  4473. _installalias() {
  4474. _c_home="$1"
  4475. _initpath
  4476. _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  4477. if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
  4478. echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
  4479. echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
  4480. echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
  4481. fi
  4482. if [ "$_c_home" ]; then
  4483. _c_entry=" --config-home '$_c_home'"
  4484. fi
  4485. _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
  4486. if [ "$_c_home" ]; then
  4487. _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
  4488. else
  4489. _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile"
  4490. fi
  4491. _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
  4492. _profile="$(_detect_profile)"
  4493. if [ "$_profile" ]; then
  4494. _debug "Found profile: $_profile"
  4495. _info "Installing alias to '$_profile'"
  4496. _setopt "$_profile" ". \"$_envfile\""
  4497. _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
  4498. else
  4499. _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
  4500. fi
  4501. #for csh
  4502. _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
  4503. _csh_profile="$HOME/.cshrc"
  4504. if [ -f "$_csh_profile" ]; then
  4505. _info "Installing alias to '$_csh_profile'"
  4506. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  4507. if [ "$_c_home" ]; then
  4508. _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
  4509. else
  4510. _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile"
  4511. fi
  4512. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
  4513. _setopt "$_csh_profile" "source \"$_cshfile\""
  4514. fi
  4515. #for tcsh
  4516. _tcsh_profile="$HOME/.tcshrc"
  4517. if [ -f "$_tcsh_profile" ]; then
  4518. _info "Installing alias to '$_tcsh_profile'"
  4519. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  4520. if [ "$_c_home" ]; then
  4521. _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
  4522. fi
  4523. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
  4524. _setopt "$_tcsh_profile" "source \"$_cshfile\""
  4525. fi
  4526. }
  4527. # nocron confighome noprofile
  4528. install() {
  4529. if [ -z "$LE_WORKING_DIR" ]; then
  4530. LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
  4531. fi
  4532. _nocron="$1"
  4533. _c_home="$2"
  4534. _noprofile="$3"
  4535. if ! _initpath; then
  4536. _err "Install failed."
  4537. return 1
  4538. fi
  4539. if [ "$_nocron" ]; then
  4540. _debug "Skip install cron job"
  4541. fi
  4542. if [ "$IN_CRON" != "1" ]; then
  4543. if ! _precheck "$_nocron"; then
  4544. _err "Pre-check failed, can not install."
  4545. return 1
  4546. fi
  4547. fi
  4548. if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
  4549. _info "Using config home: $LE_CONFIG_HOME"
  4550. _c_home="$LE_CONFIG_HOME"
  4551. fi
  4552. #convert from le
  4553. if [ -d "$HOME/.le" ]; then
  4554. for envfile in "le.env" "le.sh.env"; do
  4555. if [ -f "$HOME/.le/$envfile" ]; then
  4556. if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
  4557. _upgrading="1"
  4558. _info "You are upgrading from le.sh"
  4559. _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
  4560. mv "$HOME/.le" "$LE_WORKING_DIR"
  4561. mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  4562. break
  4563. fi
  4564. fi
  4565. done
  4566. fi
  4567. _info "Installing to $LE_WORKING_DIR"
  4568. if [ ! -d "$LE_WORKING_DIR" ]; then
  4569. if ! mkdir -p "$LE_WORKING_DIR"; then
  4570. _err "Can not create working dir: $LE_WORKING_DIR"
  4571. return 1
  4572. fi
  4573. chmod 700 "$LE_WORKING_DIR"
  4574. fi
  4575. if [ ! -d "$LE_CONFIG_HOME" ]; then
  4576. if ! mkdir -p "$LE_CONFIG_HOME"; then
  4577. _err "Can not create config dir: $LE_CONFIG_HOME"
  4578. return 1
  4579. fi
  4580. chmod 700 "$LE_CONFIG_HOME"
  4581. fi
  4582. cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
  4583. if [ "$?" != "0" ]; then
  4584. _err "Install failed, can not copy $PROJECT_ENTRY"
  4585. return 1
  4586. fi
  4587. _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
  4588. if [ "$IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then
  4589. _installalias "$_c_home"
  4590. fi
  4591. for subf in $_SUB_FOLDERS; do
  4592. if [ -d "$subf" ]; then
  4593. mkdir -p "$LE_WORKING_DIR/$subf"
  4594. cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
  4595. fi
  4596. done
  4597. if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
  4598. _initconf
  4599. fi
  4600. if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
  4601. _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
  4602. fi
  4603. if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
  4604. _saveaccountconf "CERT_HOME" "$CERT_HOME"
  4605. fi
  4606. if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
  4607. _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
  4608. fi
  4609. if [ -z "$_nocron" ]; then
  4610. installcronjob "$_c_home"
  4611. fi
  4612. if [ -z "$NO_DETECT_SH" ]; then
  4613. #Modify shebang
  4614. if _exists bash; then
  4615. _bash_path="$(bash -c "command -v bash 2>/dev/null")"
  4616. if [ -z "$_bash_path" ]; then
  4617. _bash_path="$(bash -c 'echo $SHELL')"
  4618. fi
  4619. fi
  4620. if [ "$_bash_path" ]; then
  4621. _info "Good, bash is found, so change the shebang to use bash as preferred."
  4622. _shebang='#!'"$_bash_path"
  4623. _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
  4624. for subf in $_SUB_FOLDERS; do
  4625. if [ -d "$LE_WORKING_DIR/$subf" ]; then
  4626. for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
  4627. _setShebang "$_apifile" "$_shebang"
  4628. done
  4629. fi
  4630. done
  4631. fi
  4632. fi
  4633. _info OK
  4634. }
  4635. # nocron
  4636. uninstall() {
  4637. _nocron="$1"
  4638. if [ -z "$_nocron" ]; then
  4639. uninstallcronjob
  4640. fi
  4641. _initpath
  4642. _uninstallalias
  4643. rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
  4644. _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
  4645. }
  4646. _uninstallalias() {
  4647. _initpath
  4648. _profile="$(_detect_profile)"
  4649. if [ "$_profile" ]; then
  4650. _info "Uninstalling alias from: '$_profile'"
  4651. text="$(cat "$_profile")"
  4652. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
  4653. fi
  4654. _csh_profile="$HOME/.cshrc"
  4655. if [ -f "$_csh_profile" ]; then
  4656. _info "Uninstalling alias from: '$_csh_profile'"
  4657. text="$(cat "$_csh_profile")"
  4658. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
  4659. fi
  4660. _tcsh_profile="$HOME/.tcshrc"
  4661. if [ -f "$_tcsh_profile" ]; then
  4662. _info "Uninstalling alias from: '$_csh_profile'"
  4663. text="$(cat "$_tcsh_profile")"
  4664. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
  4665. fi
  4666. }
  4667. cron() {
  4668. export IN_CRON=1
  4669. _initpath
  4670. _info "$(__green "===Starting cron===")"
  4671. if [ "$AUTO_UPGRADE" = "1" ]; then
  4672. export LE_WORKING_DIR
  4673. (
  4674. if ! upgrade; then
  4675. _err "Cron:Upgrade failed!"
  4676. return 1
  4677. fi
  4678. )
  4679. . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
  4680. if [ -t 1 ]; then
  4681. __INTERACTIVE="1"
  4682. fi
  4683. _info "Auto upgraded to: $VER"
  4684. fi
  4685. renewAll
  4686. _ret="$?"
  4687. IN_CRON=""
  4688. _info "$(__green "===End cron===")"
  4689. exit $_ret
  4690. }
  4691. version() {
  4692. echo "$PROJECT"
  4693. echo "v$VER"
  4694. }
  4695. showhelp() {
  4696. _initpath
  4697. version
  4698. echo "Usage: $PROJECT_ENTRY command ...[parameters]....
  4699. Commands:
  4700. --help, -h Show this help message.
  4701. --version, -v Show version info.
  4702. --install Install $PROJECT_NAME to your system.
  4703. --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
  4704. --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
  4705. --issue Issue a cert.
  4706. --signcsr Issue a cert from an existing csr.
  4707. --deploy Deploy the cert to your server.
  4708. --install-cert Install the issued cert to apache/nginx or any other server.
  4709. --renew, -r Renew a cert.
  4710. --renew-all Renew all the certs.
  4711. --revoke Revoke a cert.
  4712. --remove Remove the cert from list of certs known to $PROJECT_NAME.
  4713. --list List all the certs.
  4714. --showcsr Show the content of a csr.
  4715. --install-cronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
  4716. --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
  4717. --cron Run cron job to renew all the certs.
  4718. --toPkcs Export the certificate and key to a pfx file.
  4719. --toPkcs8 Convert to pkcs8 format.
  4720. --update-account Update account info.
  4721. --register-account Register account key.
  4722. --deactivate-account Deactivate the account.
  4723. --create-account-key Create an account private key, professional use.
  4724. --create-domain-key Create an domain private key, professional use.
  4725. --createCSR, -ccsr Create CSR , professional use.
  4726. --deactivate Deactivate the domain authz, professional use.
  4727. Parameters:
  4728. --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
  4729. --challenge-alias domain.tld The challenge domain alias for DNS alias mode: $_DNS_ALIAS_WIKI
  4730. --domain-alias domain.tld The domain alias for DNS alias mode: $_DNS_ALIAS_WIKI
  4731. --force, -f Used to force to install or force to renew a cert immediately.
  4732. --staging, --test Use staging server, just for test.
  4733. --debug Output debug info.
  4734. --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
  4735. --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
  4736. --standalone Use standalone mode.
  4737. --stateless Use stateless mode, see: $_STATELESS_WIKI
  4738. --apache Use apache mode.
  4739. --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
  4740. --dnssleep [$DEFAULT_DNS_SLEEP] The time in seconds to wait for all the txt records to take effect in dns api mode. Default $DEFAULT_DNS_SLEEP seconds.
  4741. --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
  4742. --accountkeylength, -ak [2048] Specifies the account key length.
  4743. --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
  4744. --log-level 1|2 Specifies the log level, default is 1.
  4745. --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
  4746. These parameters are to install the cert to nginx/apache or any other server after issue/renew a cert:
  4747. --cert-file After issue/renew, the cert will be copied to this path.
  4748. --key-file After issue/renew, the key will be copied to this path.
  4749. --ca-file After issue/renew, the intermediate cert will be copied to this path.
  4750. --fullchain-file After issue/renew, the fullchain cert will be copied to this path.
  4751. --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
  4752. --server SERVER ACME Directory Resource URI. (default: https://acme-v01.api.letsencrypt.org/directory)
  4753. --accountconf Specifies a customized account config file.
  4754. --home Specifies the home dir for $PROJECT_NAME .
  4755. --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
  4756. --config-home Specifies the home dir to save all the configurations.
  4757. --useragent Specifies the user agent string. it will be saved for future use too.
  4758. --accountemail Specifies the account email, only valid for the '--install' and '--update-account' command.
  4759. --accountkey Specifies the account key path, only valid for the '--install' command.
  4760. --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
  4761. --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
  4762. --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
  4763. --listraw Only used for '--list' command, list the certs in raw format.
  4764. --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
  4765. --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
  4766. --ca-bundle Specifies the path to the CA certificate bundle to verify api server's certificate.
  4767. --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl.
  4768. --nocron Only valid for '--install' command, which means: do not install the default cron job. In this case, the certs will not be renewed automatically.
  4769. --no-color Do not output color text.
  4770. --force-color Force output of color text. Useful for non-interactive use with the aha tool for HTML E-Mails.
  4771. --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
  4772. --csr Specifies the input csr.
  4773. --pre-hook Command to be run before obtaining any certificates.
  4774. --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed.
  4775. --renew-hook Command to be run once for each successfully renewed certificate.
  4776. --deploy-hook The hook file to deploy cert
  4777. --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
  4778. --always-force-new-domain-key Generate new domain key when renewal. Otherwise, the domain key is not changed by default.
  4779. --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
  4780. --listen-v4 Force standalone/tls server to listen at ipv4.
  4781. --listen-v6 Force standalone/tls server to listen at ipv6.
  4782. --openssl-bin Specifies a custom openssl bin location.
  4783. --use-wget Force to use wget, if you have both curl and wget installed.
  4784. --yes-I-know-dns-manual-mode-enough-go-ahead-please Force to use dns manual mode: $_DNS_MANUAL_WIKI
  4785. --branch, -b Only valid for '--upgrade' command, specifies the branch name to upgrade to.
  4786. "
  4787. }
  4788. # nocron noprofile
  4789. _installOnline() {
  4790. _info "Installing from online archive."
  4791. _nocron="$1"
  4792. _noprofile="$2"
  4793. if [ ! "$BRANCH" ]; then
  4794. BRANCH="master"
  4795. fi
  4796. target="$PROJECT/archive/$BRANCH.tar.gz"
  4797. _info "Downloading $target"
  4798. localname="$BRANCH.tar.gz"
  4799. if ! _get "$target" >$localname; then
  4800. _err "Download error."
  4801. return 1
  4802. fi
  4803. (
  4804. _info "Extracting $localname"
  4805. if ! (tar xzf $localname || gtar xzf $localname); then
  4806. _err "Extraction error."
  4807. exit 1
  4808. fi
  4809. cd "$PROJECT_NAME-$BRANCH"
  4810. chmod +x $PROJECT_ENTRY
  4811. if ./$PROJECT_ENTRY install "$_nocron" "" "$_noprofile"; then
  4812. _info "Install success!"
  4813. fi
  4814. cd ..
  4815. rm -rf "$PROJECT_NAME-$BRANCH"
  4816. rm -f "$localname"
  4817. )
  4818. }
  4819. upgrade() {
  4820. if (
  4821. _initpath
  4822. export LE_WORKING_DIR
  4823. cd "$LE_WORKING_DIR"
  4824. _installOnline "nocron" "noprofile"
  4825. ); then
  4826. _info "Upgrade success!"
  4827. exit 0
  4828. else
  4829. _err "Upgrade failed!"
  4830. exit 1
  4831. fi
  4832. }
  4833. _processAccountConf() {
  4834. if [ "$_useragent" ]; then
  4835. _saveaccountconf "USER_AGENT" "$_useragent"
  4836. elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
  4837. _saveaccountconf "USER_AGENT" "$USER_AGENT"
  4838. fi
  4839. if [ "$_accountemail" ]; then
  4840. _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
  4841. elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
  4842. _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
  4843. fi
  4844. if [ "$_openssl_bin" ]; then
  4845. _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin"
  4846. elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
  4847. _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
  4848. fi
  4849. if [ "$_auto_upgrade" ]; then
  4850. _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
  4851. elif [ "$AUTO_UPGRADE" ]; then
  4852. _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
  4853. fi
  4854. if [ "$_use_wget" ]; then
  4855. _saveaccountconf "ACME_USE_WGET" "$_use_wget"
  4856. elif [ "$ACME_USE_WGET" ]; then
  4857. _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET"
  4858. fi
  4859. }
  4860. _process() {
  4861. _CMD=""
  4862. _domain=""
  4863. _altdomains="$NO_VALUE"
  4864. _webroot=""
  4865. _challenge_alias=""
  4866. _keylength=""
  4867. _accountkeylength=""
  4868. _cert_file=""
  4869. _key_file=""
  4870. _ca_file=""
  4871. _fullchain_file=""
  4872. _reloadcmd=""
  4873. _password=""
  4874. _accountconf=""
  4875. _useragent=""
  4876. _accountemail=""
  4877. _accountkey=""
  4878. _certhome=""
  4879. _confighome=""
  4880. _httpport=""
  4881. _tlsport=""
  4882. _dnssleep=""
  4883. _listraw=""
  4884. _stopRenewOnError=""
  4885. #_insecure=""
  4886. _ca_bundle=""
  4887. _ca_path=""
  4888. _nocron=""
  4889. _ecc=""
  4890. _csr=""
  4891. _pre_hook=""
  4892. _post_hook=""
  4893. _renew_hook=""
  4894. _deploy_hook=""
  4895. _logfile=""
  4896. _log=""
  4897. _local_address=""
  4898. _log_level=""
  4899. _auto_upgrade=""
  4900. _listen_v4=""
  4901. _listen_v6=""
  4902. _openssl_bin=""
  4903. _syslog=""
  4904. _use_wget=""
  4905. _server=""
  4906. while [ ${#} -gt 0 ]; do
  4907. case "${1}" in
  4908. --help | -h)
  4909. showhelp
  4910. return
  4911. ;;
  4912. --version | -v)
  4913. version
  4914. return
  4915. ;;
  4916. --install)
  4917. _CMD="install"
  4918. ;;
  4919. --uninstall)
  4920. _CMD="uninstall"
  4921. ;;
  4922. --upgrade)
  4923. _CMD="upgrade"
  4924. ;;
  4925. --issue)
  4926. _CMD="issue"
  4927. ;;
  4928. --deploy)
  4929. _CMD="deploy"
  4930. ;;
  4931. --signcsr)
  4932. _CMD="signcsr"
  4933. ;;
  4934. --showcsr)
  4935. _CMD="showcsr"
  4936. ;;
  4937. --installcert | -i | --install-cert)
  4938. _CMD="installcert"
  4939. ;;
  4940. --renew | -r)
  4941. _CMD="renew"
  4942. ;;
  4943. --renewAll | --renewall | --renew-all)
  4944. _CMD="renewAll"
  4945. ;;
  4946. --revoke)
  4947. _CMD="revoke"
  4948. ;;
  4949. --remove)
  4950. _CMD="remove"
  4951. ;;
  4952. --list)
  4953. _CMD="list"
  4954. ;;
  4955. --installcronjob | --install-cronjob)
  4956. _CMD="installcronjob"
  4957. ;;
  4958. --uninstallcronjob | --uninstall-cronjob)
  4959. _CMD="uninstallcronjob"
  4960. ;;
  4961. --cron)
  4962. _CMD="cron"
  4963. ;;
  4964. --toPkcs)
  4965. _CMD="toPkcs"
  4966. ;;
  4967. --toPkcs8)
  4968. _CMD="toPkcs8"
  4969. ;;
  4970. --createAccountKey | --createaccountkey | -cak | --create-account-key)
  4971. _CMD="createAccountKey"
  4972. ;;
  4973. --createDomainKey | --createdomainkey | -cdk | --create-domain-key)
  4974. _CMD="createDomainKey"
  4975. ;;
  4976. --createCSR | --createcsr | -ccr)
  4977. _CMD="createCSR"
  4978. ;;
  4979. --deactivate)
  4980. _CMD="deactivate"
  4981. ;;
  4982. --updateaccount | --update-account)
  4983. _CMD="updateaccount"
  4984. ;;
  4985. --registeraccount | --register-account)
  4986. _CMD="registeraccount"
  4987. ;;
  4988. --deactivate-account)
  4989. _CMD="deactivateaccount"
  4990. ;;
  4991. --domain | -d)
  4992. _dvalue="$2"
  4993. if [ "$_dvalue" ]; then
  4994. if _startswith "$_dvalue" "-"; then
  4995. _err "'$_dvalue' is not a valid domain for parameter '$1'"
  4996. return 1
  4997. fi
  4998. if _is_idn "$_dvalue" && ! _exists idn; then
  4999. _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
  5000. return 1
  5001. fi
  5002. if _startswith "$_dvalue" "*."; then
  5003. _debug "Wildcard domain"
  5004. export ACME_VERSION=2
  5005. fi
  5006. if [ -z "$_domain" ]; then
  5007. _domain="$_dvalue"
  5008. else
  5009. if [ "$_altdomains" = "$NO_VALUE" ]; then
  5010. _altdomains="$_dvalue"
  5011. else
  5012. _altdomains="$_altdomains,$_dvalue"
  5013. fi
  5014. fi
  5015. fi
  5016. shift
  5017. ;;
  5018. --force | -f)
  5019. FORCE="1"
  5020. ;;
  5021. --staging | --test)
  5022. STAGE="1"
  5023. ;;
  5024. --server)
  5025. ACME_DIRECTORY="$2"
  5026. _server="$ACME_DIRECTORY"
  5027. export ACME_DIRECTORY
  5028. shift
  5029. ;;
  5030. --debug)
  5031. if [ -z "$2" ] || _startswith "$2" "-"; then
  5032. DEBUG="$DEBUG_LEVEL_DEFAULT"
  5033. else
  5034. DEBUG="$2"
  5035. shift
  5036. fi
  5037. ;;
  5038. --output-insecure)
  5039. export OUTPUT_INSECURE=1
  5040. ;;
  5041. --webroot | -w)
  5042. wvalue="$2"
  5043. if [ -z "$_webroot" ]; then
  5044. _webroot="$wvalue"
  5045. else
  5046. _webroot="$_webroot,$wvalue"
  5047. fi
  5048. shift
  5049. ;;
  5050. --challenge-alias)
  5051. cvalue="$2"
  5052. _challenge_alias="$_challenge_alias$cvalue,"
  5053. shift
  5054. ;;
  5055. --domain-alias)
  5056. cvalue="$DNS_ALIAS_PREFIX$2"
  5057. _challenge_alias="$_challenge_alias$cvalue,"
  5058. shift
  5059. ;;
  5060. --standalone)
  5061. wvalue="$NO_VALUE"
  5062. if [ -z "$_webroot" ]; then
  5063. _webroot="$wvalue"
  5064. else
  5065. _webroot="$_webroot,$wvalue"
  5066. fi
  5067. ;;
  5068. --stateless)
  5069. wvalue="$MODE_STATELESS"
  5070. if [ -z "$_webroot" ]; then
  5071. _webroot="$wvalue"
  5072. else
  5073. _webroot="$_webroot,$wvalue"
  5074. fi
  5075. ;;
  5076. --local-address)
  5077. lvalue="$2"
  5078. _local_address="$_local_address$lvalue,"
  5079. shift
  5080. ;;
  5081. --apache)
  5082. wvalue="apache"
  5083. if [ -z "$_webroot" ]; then
  5084. _webroot="$wvalue"
  5085. else
  5086. _webroot="$_webroot,$wvalue"
  5087. fi
  5088. ;;
  5089. --nginx)
  5090. wvalue="$NGINX"
  5091. if [ -z "$_webroot" ]; then
  5092. _webroot="$wvalue"
  5093. else
  5094. _webroot="$_webroot,$wvalue"
  5095. fi
  5096. ;;
  5097. --dns)
  5098. wvalue="$W_DNS"
  5099. if [ "$2" ] && ! _startswith "$2" "-"; then
  5100. wvalue="$2"
  5101. shift
  5102. fi
  5103. if [ -z "$_webroot" ]; then
  5104. _webroot="$wvalue"
  5105. else
  5106. _webroot="$_webroot,$wvalue"
  5107. fi
  5108. ;;
  5109. --dnssleep)
  5110. _dnssleep="$2"
  5111. Le_DNSSleep="$_dnssleep"
  5112. shift
  5113. ;;
  5114. --keylength | -k)
  5115. _keylength="$2"
  5116. shift
  5117. ;;
  5118. --accountkeylength | -ak)
  5119. _accountkeylength="$2"
  5120. shift
  5121. ;;
  5122. --cert-file | --certpath)
  5123. _cert_file="$2"
  5124. shift
  5125. ;;
  5126. --key-file | --keypath)
  5127. _key_file="$2"
  5128. shift
  5129. ;;
  5130. --ca-file | --capath)
  5131. _ca_file="$2"
  5132. shift
  5133. ;;
  5134. --fullchain-file | --fullchainpath)
  5135. _fullchain_file="$2"
  5136. shift
  5137. ;;
  5138. --reloadcmd | --reloadCmd)
  5139. _reloadcmd="$2"
  5140. shift
  5141. ;;
  5142. --password)
  5143. _password="$2"
  5144. shift
  5145. ;;
  5146. --accountconf)
  5147. _accountconf="$2"
  5148. ACCOUNT_CONF_PATH="$_accountconf"
  5149. shift
  5150. ;;
  5151. --home)
  5152. LE_WORKING_DIR="$2"
  5153. shift
  5154. ;;
  5155. --certhome | --cert-home)
  5156. _certhome="$2"
  5157. CERT_HOME="$_certhome"
  5158. shift
  5159. ;;
  5160. --config-home)
  5161. _confighome="$2"
  5162. LE_CONFIG_HOME="$_confighome"
  5163. shift
  5164. ;;
  5165. --useragent)
  5166. _useragent="$2"
  5167. USER_AGENT="$_useragent"
  5168. shift
  5169. ;;
  5170. --accountemail)
  5171. _accountemail="$2"
  5172. ACCOUNT_EMAIL="$_accountemail"
  5173. shift
  5174. ;;
  5175. --accountkey)
  5176. _accountkey="$2"
  5177. ACCOUNT_KEY_PATH="$_accountkey"
  5178. shift
  5179. ;;
  5180. --days)
  5181. _days="$2"
  5182. Le_RenewalDays="$_days"
  5183. shift
  5184. ;;
  5185. --httpport)
  5186. _httpport="$2"
  5187. Le_HTTPPort="$_httpport"
  5188. shift
  5189. ;;
  5190. --listraw)
  5191. _listraw="raw"
  5192. ;;
  5193. --stopRenewOnError | --stoprenewonerror | -se)
  5194. _stopRenewOnError="1"
  5195. ;;
  5196. --insecure)
  5197. #_insecure="1"
  5198. HTTPS_INSECURE="1"
  5199. ;;
  5200. --ca-bundle)
  5201. _ca_bundle="$(_readlink "$2")"
  5202. CA_BUNDLE="$_ca_bundle"
  5203. shift
  5204. ;;
  5205. --ca-path)
  5206. _ca_path="$2"
  5207. CA_PATH="$_ca_path"
  5208. shift
  5209. ;;
  5210. --nocron)
  5211. _nocron="1"
  5212. ;;
  5213. --no-color)
  5214. export ACME_NO_COLOR=1
  5215. ;;
  5216. --force-color)
  5217. export ACME_FORCE_COLOR=1
  5218. ;;
  5219. --ecc)
  5220. _ecc="isEcc"
  5221. ;;
  5222. --csr)
  5223. _csr="$2"
  5224. shift
  5225. ;;
  5226. --pre-hook)
  5227. _pre_hook="$2"
  5228. shift
  5229. ;;
  5230. --post-hook)
  5231. _post_hook="$2"
  5232. shift
  5233. ;;
  5234. --renew-hook)
  5235. _renew_hook="$2"
  5236. shift
  5237. ;;
  5238. --deploy-hook)
  5239. if [ -z "$2" ] || _startswith "$2" "-"; then
  5240. _usage "Please specify a value for '--deploy-hook'"
  5241. return 1
  5242. fi
  5243. _deploy_hook="$_deploy_hook$2,"
  5244. shift
  5245. ;;
  5246. --ocsp-must-staple | --ocsp)
  5247. Le_OCSP_Staple="1"
  5248. ;;
  5249. --always-force-new-domain-key)
  5250. if [ -z "$2" ] || _startswith "$2" "-"; then
  5251. Le_ForceNewDomainKey=1
  5252. else
  5253. Le_ForceNewDomainKey="$2"
  5254. shift
  5255. fi
  5256. ;;
  5257. --yes-I-know-dns-manual-mode-enough-go-ahead-please)
  5258. export FORCE_DNS_MANUAL=1
  5259. ;;
  5260. --log | --logfile)
  5261. _log="1"
  5262. _logfile="$2"
  5263. if _startswith "$_logfile" '-'; then
  5264. _logfile=""
  5265. else
  5266. shift
  5267. fi
  5268. LOG_FILE="$_logfile"
  5269. if [ -z "$LOG_LEVEL" ]; then
  5270. LOG_LEVEL="$DEFAULT_LOG_LEVEL"
  5271. fi
  5272. ;;
  5273. --log-level)
  5274. _log_level="$2"
  5275. LOG_LEVEL="$_log_level"
  5276. shift
  5277. ;;
  5278. --syslog)
  5279. if ! _startswith "$2" '-'; then
  5280. _syslog="$2"
  5281. shift
  5282. fi
  5283. if [ -z "$_syslog" ]; then
  5284. _syslog="$SYSLOG_LEVEL_DEFAULT"
  5285. fi
  5286. ;;
  5287. --auto-upgrade)
  5288. _auto_upgrade="$2"
  5289. if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
  5290. _auto_upgrade="1"
  5291. else
  5292. shift
  5293. fi
  5294. AUTO_UPGRADE="$_auto_upgrade"
  5295. ;;
  5296. --listen-v4)
  5297. _listen_v4="1"
  5298. Le_Listen_V4="$_listen_v4"
  5299. ;;
  5300. --listen-v6)
  5301. _listen_v6="1"
  5302. Le_Listen_V6="$_listen_v6"
  5303. ;;
  5304. --openssl-bin)
  5305. _openssl_bin="$2"
  5306. ACME_OPENSSL_BIN="$_openssl_bin"
  5307. shift
  5308. ;;
  5309. --use-wget)
  5310. _use_wget="1"
  5311. ACME_USE_WGET="1"
  5312. ;;
  5313. --branch | -b)
  5314. export BRANCH="$2"
  5315. shift
  5316. ;;
  5317. *)
  5318. _err "Unknown parameter : $1"
  5319. return 1
  5320. ;;
  5321. esac
  5322. shift 1
  5323. done
  5324. if [ "${_CMD}" != "install" ]; then
  5325. __initHome
  5326. if [ "$_log" ]; then
  5327. if [ -z "$_logfile" ]; then
  5328. _logfile="$DEFAULT_LOG_FILE"
  5329. fi
  5330. fi
  5331. if [ "$_logfile" ]; then
  5332. _saveaccountconf "LOG_FILE" "$_logfile"
  5333. LOG_FILE="$_logfile"
  5334. fi
  5335. if [ "$_log_level" ]; then
  5336. _saveaccountconf "LOG_LEVEL" "$_log_level"
  5337. LOG_LEVEL="$_log_level"
  5338. fi
  5339. if [ "$_syslog" ]; then
  5340. if _exists logger; then
  5341. if [ "$_syslog" = "0" ]; then
  5342. _clearaccountconf "SYS_LOG"
  5343. else
  5344. _saveaccountconf "SYS_LOG" "$_syslog"
  5345. fi
  5346. SYS_LOG="$_syslog"
  5347. else
  5348. _err "The 'logger' command is not found, can not enable syslog."
  5349. _clearaccountconf "SYS_LOG"
  5350. SYS_LOG=""
  5351. fi
  5352. fi
  5353. _processAccountConf
  5354. fi
  5355. _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
  5356. if [ "$DEBUG" ]; then
  5357. version
  5358. if [ "$_server" ]; then
  5359. _debug "Using server: $_server"
  5360. fi
  5361. fi
  5362. case "${_CMD}" in
  5363. install) install "$_nocron" "$_confighome" ;;
  5364. uninstall) uninstall "$_nocron" ;;
  5365. upgrade) upgrade ;;
  5366. issue)
  5367. issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias"
  5368. ;;
  5369. deploy)
  5370. deploy "$_domain" "$_deploy_hook" "$_ecc"
  5371. ;;
  5372. signcsr)
  5373. signcsr "$_csr" "$_webroot" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias"
  5374. ;;
  5375. showcsr)
  5376. showcsr "$_csr" "$_domain"
  5377. ;;
  5378. installcert)
  5379. installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
  5380. ;;
  5381. renew)
  5382. renew "$_domain" "$_ecc"
  5383. ;;
  5384. renewAll)
  5385. renewAll "$_stopRenewOnError"
  5386. ;;
  5387. revoke)
  5388. revoke "$_domain" "$_ecc"
  5389. ;;
  5390. remove)
  5391. remove "$_domain" "$_ecc"
  5392. ;;
  5393. deactivate)
  5394. deactivate "$_domain,$_altdomains"
  5395. ;;
  5396. registeraccount)
  5397. registeraccount "$_accountkeylength"
  5398. ;;
  5399. updateaccount)
  5400. updateaccount
  5401. ;;
  5402. deactivateaccount)
  5403. deactivateaccount
  5404. ;;
  5405. list)
  5406. list "$_listraw"
  5407. ;;
  5408. installcronjob) installcronjob "$_confighome" ;;
  5409. uninstallcronjob) uninstallcronjob ;;
  5410. cron) cron ;;
  5411. toPkcs)
  5412. toPkcs "$_domain" "$_password" "$_ecc"
  5413. ;;
  5414. toPkcs8)
  5415. toPkcs8 "$_domain" "$_ecc"
  5416. ;;
  5417. createAccountKey)
  5418. createAccountKey "$_accountkeylength"
  5419. ;;
  5420. createDomainKey)
  5421. createDomainKey "$_domain" "$_keylength"
  5422. ;;
  5423. createCSR)
  5424. createCSR "$_domain" "$_altdomains" "$_ecc"
  5425. ;;
  5426. *)
  5427. if [ "$_CMD" ]; then
  5428. _err "Invalid command: $_CMD"
  5429. fi
  5430. showhelp
  5431. return 1
  5432. ;;
  5433. esac
  5434. _ret="$?"
  5435. if [ "$_ret" != "0" ]; then
  5436. return $_ret
  5437. fi
  5438. if [ "${_CMD}" = "install" ]; then
  5439. if [ "$_log" ]; then
  5440. if [ -z "$LOG_FILE" ]; then
  5441. LOG_FILE="$DEFAULT_LOG_FILE"
  5442. fi
  5443. _saveaccountconf "LOG_FILE" "$LOG_FILE"
  5444. fi
  5445. if [ "$_log_level" ]; then
  5446. _saveaccountconf "LOG_LEVEL" "$_log_level"
  5447. fi
  5448. if [ "$_syslog" ]; then
  5449. if _exists logger; then
  5450. if [ "$_syslog" = "0" ]; then
  5451. _clearaccountconf "SYS_LOG"
  5452. else
  5453. _saveaccountconf "SYS_LOG" "$_syslog"
  5454. fi
  5455. else
  5456. _err "The 'logger' command is not found, can not enable syslog."
  5457. _clearaccountconf "SYS_LOG"
  5458. SYS_LOG=""
  5459. fi
  5460. fi
  5461. _processAccountConf
  5462. fi
  5463. }
  5464. if [ "$INSTALLONLINE" ]; then
  5465. INSTALLONLINE=""
  5466. _installOnline
  5467. exit
  5468. fi
  5469. main() {
  5470. [ -z "$1" ] && showhelp && return
  5471. if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
  5472. }
  5473. main "$@"