acme.sh 224 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163
  1. #!/usr/bin/env sh
  2. VER=3.1.2
  3. PROJECT_NAME="acme.sh"
  4. PROJECT_ENTRY="acme.sh"
  5. PROJECT="https://github.com/acmesh-official/$PROJECT_NAME"
  6. DEFAULT_INSTALL_HOME="$HOME/.$PROJECT_NAME"
  7. _WINDOWS_SCHEDULER_NAME="$PROJECT_NAME.cron"
  8. _SCRIPT_="$0"
  9. _SUB_FOLDER_NOTIFY="notify"
  10. _SUB_FOLDER_DNSAPI="dnsapi"
  11. _SUB_FOLDER_DEPLOY="deploy"
  12. _SUB_FOLDERS="$_SUB_FOLDER_DNSAPI $_SUB_FOLDER_DEPLOY $_SUB_FOLDER_NOTIFY"
  13. CA_LETSENCRYPT_V2="https://acme-v02.api.letsencrypt.org/directory"
  14. CA_LETSENCRYPT_V2_TEST="https://acme-staging-v02.api.letsencrypt.org/directory"
  15. CA_ZEROSSL="https://acme.zerossl.com/v2/DV90"
  16. _ZERO_EAB_ENDPOINT="https://api.zerossl.com/acme/eab-credentials-email"
  17. CA_SSLCOM_RSA="https://acme.ssl.com/sslcom-dv-rsa"
  18. CA_SSLCOM_ECC="https://acme.ssl.com/sslcom-dv-ecc"
  19. CA_GOOGLE="https://dv.acme-v02.api.pki.goog/directory"
  20. CA_GOOGLE_TEST="https://dv.acme-v02.test-api.pki.goog/directory"
  21. CA_ACTALIS="https://acme-api.actalis.com/acme/directory"
  22. DEFAULT_CA=$CA_ZEROSSL
  23. DEFAULT_STAGING_CA=$CA_LETSENCRYPT_V2_TEST
  24. CA_NAMES="
  25. ZeroSSL.com,zerossl
  26. LetsEncrypt.org,letsencrypt
  27. LetsEncrypt.org_test,letsencrypt_test,letsencrypttest
  28. SSL.com,sslcom
  29. Google.com,google
  30. Google.com_test,googletest,google_test
  31. Actalis.com,actalis.com,actalis
  32. "
  33. CA_SERVERS="$CA_ZEROSSL,$CA_LETSENCRYPT_V2,$CA_LETSENCRYPT_V2_TEST,$CA_SSLCOM_RSA,$CA_GOOGLE,$CA_GOOGLE_TEST,$CA_ACTALIS"
  34. DEFAULT_USER_AGENT="$PROJECT_NAME/$VER ($PROJECT)"
  35. DEFAULT_ACCOUNT_KEY_LENGTH=ec-256
  36. DEFAULT_DOMAIN_KEY_LENGTH=ec-256
  37. DEFAULT_OPENSSL_BIN="openssl"
  38. VTYPE_HTTP="http-01"
  39. VTYPE_DNS="dns-01"
  40. VTYPE_ALPN="tls-alpn-01"
  41. ID_TYPE_DNS="dns"
  42. ID_TYPE_IP="ip"
  43. LOCAL_ANY_ADDRESS="0.0.0.0"
  44. DEFAULT_RENEW=60
  45. NO_VALUE="no"
  46. W_DNS="dns"
  47. W_ALPN="alpn"
  48. DNS_ALIAS_PREFIX="="
  49. MODE_STATELESS="stateless"
  50. STATE_VERIFIED="verified_ok"
  51. NGINX="nginx:"
  52. NGINX_START="#ACME_NGINX_START"
  53. NGINX_END="#ACME_NGINX_END"
  54. BEGIN_CSR="-----BEGIN [NEW ]\{0,4\}CERTIFICATE REQUEST-----"
  55. END_CSR="-----END [NEW ]\{0,4\}CERTIFICATE REQUEST-----"
  56. BEGIN_CERT="-----BEGIN CERTIFICATE-----"
  57. END_CERT="-----END CERTIFICATE-----"
  58. CONTENT_TYPE_JSON="application/jose+json"
  59. RENEW_SKIP=2
  60. CODE_DNS_MANUAL=3
  61. B64CONF_START="__ACME_BASE64__START_"
  62. B64CONF_END="__ACME_BASE64__END_"
  63. ECC_SEP="_"
  64. ECC_SUFFIX="${ECC_SEP}ecc"
  65. LOG_LEVEL_1=1
  66. LOG_LEVEL_2=2
  67. LOG_LEVEL_3=3
  68. DEFAULT_LOG_LEVEL="$LOG_LEVEL_2"
  69. DEBUG_LEVEL_1=1
  70. DEBUG_LEVEL_2=2
  71. DEBUG_LEVEL_3=3
  72. DEBUG_LEVEL_DEFAULT=$DEBUG_LEVEL_2
  73. DEBUG_LEVEL_NONE=0
  74. DOH_CLOUDFLARE=1
  75. DOH_GOOGLE=2
  76. DOH_ALI=3
  77. DOH_DP=4
  78. HIDDEN_VALUE="[hidden](please add '--output-insecure' to see this value)"
  79. SYSLOG_ERROR="user.error"
  80. SYSLOG_INFO="user.info"
  81. SYSLOG_DEBUG="user.debug"
  82. #error
  83. SYSLOG_LEVEL_ERROR=3
  84. #info
  85. SYSLOG_LEVEL_INFO=6
  86. #debug
  87. SYSLOG_LEVEL_DEBUG=7
  88. #debug2
  89. SYSLOG_LEVEL_DEBUG_2=8
  90. #debug3
  91. SYSLOG_LEVEL_DEBUG_3=9
  92. SYSLOG_LEVEL_DEFAULT=$SYSLOG_LEVEL_ERROR
  93. #none
  94. SYSLOG_LEVEL_NONE=0
  95. NOTIFY_LEVEL_DISABLE=0
  96. NOTIFY_LEVEL_ERROR=1
  97. NOTIFY_LEVEL_RENEW=2
  98. NOTIFY_LEVEL_SKIP=3
  99. NOTIFY_LEVEL_DEFAULT=$NOTIFY_LEVEL_RENEW
  100. NOTIFY_MODE_BULK=0
  101. NOTIFY_MODE_CERT=1
  102. NOTIFY_MODE_DEFAULT=$NOTIFY_MODE_BULK
  103. _BASE64_ENCODED_CFGS="Le_PreHook Le_PostHook Le_RenewHook Le_Preferred_Chain Le_ReloadCmd"
  104. _DEBUG_WIKI="https://github.com/acmesh-official/acme.sh/wiki/How-to-debug-acme.sh"
  105. _PREPARE_LINK="https://github.com/acmesh-official/acme.sh/wiki/Install-preparations"
  106. _STATELESS_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Stateless-Mode"
  107. _DNS_ALIAS_WIKI="https://github.com/acmesh-official/acme.sh/wiki/DNS-alias-mode"
  108. _DNS_MANUAL_WIKI="https://github.com/acmesh-official/acme.sh/wiki/dns-manual-mode"
  109. _DNS_API_WIKI="https://github.com/acmesh-official/acme.sh/wiki/dnsapi"
  110. _NOTIFY_WIKI="https://github.com/acmesh-official/acme.sh/wiki/notify"
  111. _SUDO_WIKI="https://github.com/acmesh-official/acme.sh/wiki/sudo"
  112. _REVOKE_WIKI="https://github.com/acmesh-official/acme.sh/wiki/revokecert"
  113. _ZEROSSL_WIKI="https://github.com/acmesh-official/acme.sh/wiki/ZeroSSL.com-CA"
  114. _SSLCOM_WIKI="https://github.com/acmesh-official/acme.sh/wiki/SSL.com-CA"
  115. _SERVER_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Server"
  116. _PREFERRED_CHAIN_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Preferred-Chain"
  117. _VALIDITY_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Validity"
  118. _DNSCHECK_WIKI="https://github.com/acmesh-official/acme.sh/wiki/dnscheck"
  119. _PROFILESELECTION_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Profile-selection"
  120. _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."
  121. _DNS_MANUAL_WARN="It seems that you are using dns manual mode. please take care: $_DNS_MANUAL_ERR"
  122. _DNS_MANUAL_ERROR="It seems that you are using dns manual mode. Read this link first: $_DNS_MANUAL_WIKI"
  123. __INTERACTIVE=""
  124. if [ -t 1 ]; then
  125. __INTERACTIVE="1"
  126. fi
  127. __green() {
  128. if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then
  129. printf '\33[1;32m%b\33[0m' "$1"
  130. return
  131. fi
  132. printf -- "%b" "$1"
  133. }
  134. __red() {
  135. if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then
  136. printf '\33[1;31m%b\33[0m' "$1"
  137. return
  138. fi
  139. printf -- "%b" "$1"
  140. }
  141. _printargs() {
  142. _exitstatus="$?"
  143. if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then
  144. printf -- "%s" "[$(date)] "
  145. fi
  146. if [ -z "$2" ]; then
  147. printf -- "%s" "$1"
  148. else
  149. printf -- "%s" "$1='$2'"
  150. fi
  151. printf "\n"
  152. # return the saved exit status
  153. return "$_exitstatus"
  154. }
  155. _dlg_versions() {
  156. echo "Diagnosis versions: "
  157. echo "openssl:$ACME_OPENSSL_BIN"
  158. if _exists "${ACME_OPENSSL_BIN:-openssl}"; then
  159. ${ACME_OPENSSL_BIN:-openssl} version 2>&1
  160. else
  161. echo "$ACME_OPENSSL_BIN doesn't exist."
  162. fi
  163. echo "Apache:"
  164. if [ "$_APACHECTL" ] && _exists "$_APACHECTL"; then
  165. $_APACHECTL -V 2>&1
  166. else
  167. echo "Apache doesn't exist."
  168. fi
  169. echo "nginx:"
  170. if _exists "nginx"; then
  171. nginx -V 2>&1
  172. else
  173. echo "nginx doesn't exist."
  174. fi
  175. echo "socat:"
  176. if _exists "socat"; then
  177. socat -V 2>&1
  178. else
  179. _debug "socat doesn't exist."
  180. fi
  181. }
  182. #class
  183. _syslog() {
  184. _exitstatus="$?"
  185. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" = "$SYSLOG_LEVEL_NONE" ]; then
  186. return
  187. fi
  188. _logclass="$1"
  189. shift
  190. if [ -z "$__logger_i" ]; then
  191. if _contains "$(logger --help 2>&1)" "-i"; then
  192. __logger_i="logger -i"
  193. else
  194. __logger_i="logger"
  195. fi
  196. fi
  197. $__logger_i -t "$PROJECT_NAME" -p "$_logclass" "$(_printargs "$@")" >/dev/null 2>&1
  198. return "$_exitstatus"
  199. }
  200. _log() {
  201. [ -z "$LOG_FILE" ] && return
  202. _printargs "$@" >>"$LOG_FILE"
  203. }
  204. _info() {
  205. _log "$@"
  206. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_INFO" ]; then
  207. _syslog "$SYSLOG_INFO" "$@"
  208. fi
  209. _printargs "$@"
  210. }
  211. _err() {
  212. _syslog "$SYSLOG_ERROR" "$@"
  213. _log "$@"
  214. if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then
  215. printf -- "%s" "[$(date)] " >&2
  216. fi
  217. if [ -z "$2" ]; then
  218. __red "$1" >&2
  219. else
  220. __red "$1='$2'" >&2
  221. fi
  222. printf "\n" >&2
  223. return 1
  224. }
  225. _usage() {
  226. __red "$@" >&2
  227. printf "\n" >&2
  228. }
  229. __debug_bash_helper() {
  230. # At this point only do for --debug 3
  231. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -lt "$DEBUG_LEVEL_3" ]; then
  232. return
  233. fi
  234. # Return extra debug info when running with bash, otherwise return empty
  235. # string.
  236. if [ -z "${BASH_VERSION}" ]; then
  237. return
  238. fi
  239. # We are a bash shell at this point, return the filename, function name, and
  240. # line number as a string
  241. _dbh_saveIFS=$IFS
  242. IFS=" "
  243. # Must use eval or syntax error happens under dash. The eval should use
  244. # single quotes as older versions of busybox had a bug with double quotes and
  245. # eval.
  246. # Use 'caller 1' as we want one level up the stack as we should be called
  247. # by one of the _debug* functions
  248. eval '_dbh_called=($(caller 1))'
  249. IFS=$_dbh_saveIFS
  250. eval '_dbh_file=${_dbh_called[2]}'
  251. if [ -n "${_script_home}" ]; then
  252. # Trim off the _script_home directory name
  253. eval '_dbh_file=${_dbh_file#$_script_home/}'
  254. fi
  255. eval '_dbh_function=${_dbh_called[1]}'
  256. eval '_dbh_lineno=${_dbh_called[0]}'
  257. printf "%-40s " "$_dbh_file:${_dbh_function}:${_dbh_lineno}"
  258. }
  259. _debug() {
  260. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
  261. _log "$@"
  262. fi
  263. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
  264. _syslog "$SYSLOG_DEBUG" "$@"
  265. fi
  266. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
  267. _bash_debug=$(__debug_bash_helper)
  268. _printargs "${_bash_debug}$@" >&2
  269. fi
  270. }
  271. #output the sensitive messages
  272. _secure_debug() {
  273. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then
  274. if [ "$OUTPUT_INSECURE" = "1" ]; then
  275. _log "$@"
  276. else
  277. _log "$1" "$HIDDEN_VALUE"
  278. fi
  279. fi
  280. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then
  281. _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
  282. fi
  283. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then
  284. if [ "$OUTPUT_INSECURE" = "1" ]; then
  285. _printargs "$@" >&2
  286. else
  287. _printargs "$1" "$HIDDEN_VALUE" >&2
  288. fi
  289. fi
  290. }
  291. _debug2() {
  292. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
  293. _log "$@"
  294. fi
  295. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
  296. _syslog "$SYSLOG_DEBUG" "$@"
  297. fi
  298. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
  299. _bash_debug=$(__debug_bash_helper)
  300. _printargs "${_bash_debug}$@" >&2
  301. fi
  302. }
  303. _secure_debug2() {
  304. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then
  305. if [ "$OUTPUT_INSECURE" = "1" ]; then
  306. _log "$@"
  307. else
  308. _log "$1" "$HIDDEN_VALUE"
  309. fi
  310. fi
  311. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then
  312. _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
  313. fi
  314. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then
  315. if [ "$OUTPUT_INSECURE" = "1" ]; then
  316. _printargs "$@" >&2
  317. else
  318. _printargs "$1" "$HIDDEN_VALUE" >&2
  319. fi
  320. fi
  321. }
  322. _debug3() {
  323. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
  324. _log "$@"
  325. fi
  326. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
  327. _syslog "$SYSLOG_DEBUG" "$@"
  328. fi
  329. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
  330. _bash_debug=$(__debug_bash_helper)
  331. _printargs "${_bash_debug}$@" >&2
  332. fi
  333. }
  334. _secure_debug3() {
  335. if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then
  336. if [ "$OUTPUT_INSECURE" = "1" ]; then
  337. _log "$@"
  338. else
  339. _log "$1" "$HIDDEN_VALUE"
  340. fi
  341. fi
  342. if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then
  343. _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE"
  344. fi
  345. if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then
  346. if [ "$OUTPUT_INSECURE" = "1" ]; then
  347. _printargs "$@" >&2
  348. else
  349. _printargs "$1" "$HIDDEN_VALUE" >&2
  350. fi
  351. fi
  352. }
  353. __USE_TR_TAG=""
  354. if [ "$(echo "abc" | LANG=C tr a-z A-Z 2>/dev/null)" != "ABC" ]; then
  355. __USE_TR_TAG="1"
  356. fi
  357. export __USE_TR_TAG
  358. _upper_case() {
  359. if [ "$__USE_TR_TAG" ]; then
  360. LANG=C tr '[:lower:]' '[:upper:]'
  361. else
  362. # shellcheck disable=SC2018,SC2019
  363. LANG=C tr '[a-z]' '[A-Z]'
  364. fi
  365. }
  366. _lower_case() {
  367. if [ "$__USE_TR_TAG" ]; then
  368. LANG=C tr '[:upper:]' '[:lower:]'
  369. else
  370. # shellcheck disable=SC2018,SC2019
  371. LANG=C tr '[A-Z]' '[a-z]'
  372. fi
  373. }
  374. _startswith() {
  375. _str="$1"
  376. _sub="$2"
  377. echo "$_str" | grep -- "^$_sub" >/dev/null 2>&1
  378. }
  379. _endswith() {
  380. _str="$1"
  381. _sub="$2"
  382. echo "$_str" | grep -- "$_sub\$" >/dev/null 2>&1
  383. }
  384. _contains() {
  385. _str="$1"
  386. _sub="$2"
  387. echo "$_str" | grep -- "$_sub" >/dev/null 2>&1
  388. }
  389. _hasfield() {
  390. _str="$1"
  391. _field="$2"
  392. _sep="$3"
  393. if [ -z "$_field" ]; then
  394. _usage "Usage: str field [sep]"
  395. return 1
  396. fi
  397. if [ -z "$_sep" ]; then
  398. _sep=","
  399. fi
  400. for f in $(echo "$_str" | tr "$_sep" ' '); do
  401. if [ "$f" = "$_field" ]; then
  402. _debug2 "'$_str' contains '$_field'"
  403. return 0 #contains ok
  404. fi
  405. done
  406. _debug2 "'$_str' does not contain '$_field'"
  407. return 1 #not contains
  408. }
  409. # str index [sep]
  410. _getfield() {
  411. _str="$1"
  412. _findex="$2"
  413. _sep="$3"
  414. if [ -z "$_findex" ]; then
  415. _usage "Usage: str field [sep]"
  416. return 1
  417. fi
  418. if [ -z "$_sep" ]; then
  419. _sep=","
  420. fi
  421. _ffi="$_findex"
  422. while [ "$_ffi" -gt "0" ]; do
  423. _fv="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")"
  424. if [ "$_fv" ]; then
  425. printf -- "%s" "$_fv"
  426. return 0
  427. fi
  428. _ffi="$(_math "$_ffi" - 1)"
  429. done
  430. printf -- "%s" "$_str"
  431. }
  432. _exists() {
  433. cmd="$1"
  434. if [ -z "$cmd" ]; then
  435. _usage "Usage: _exists cmd"
  436. return 1
  437. fi
  438. if eval type type >/dev/null 2>&1; then
  439. eval type "$cmd" >/dev/null 2>&1
  440. elif command >/dev/null 2>&1; then
  441. command -v "$cmd" >/dev/null 2>&1
  442. else
  443. which "$cmd" >/dev/null 2>&1
  444. fi
  445. ret="$?"
  446. _debug3 "$cmd exists=$ret"
  447. return $ret
  448. }
  449. #a + b
  450. _math() {
  451. _m_opts="$@"
  452. printf "%s" "$(($_m_opts))"
  453. }
  454. _h_char_2_dec() {
  455. _ch=$1
  456. case "${_ch}" in
  457. a | A)
  458. printf "10"
  459. ;;
  460. b | B)
  461. printf "11"
  462. ;;
  463. c | C)
  464. printf "12"
  465. ;;
  466. d | D)
  467. printf "13"
  468. ;;
  469. e | E)
  470. printf "14"
  471. ;;
  472. f | F)
  473. printf "15"
  474. ;;
  475. *)
  476. printf "%s" "$_ch"
  477. ;;
  478. esac
  479. }
  480. _URGLY_PRINTF=""
  481. if [ "$(printf '\x41')" != 'A' ]; then
  482. _URGLY_PRINTF=1
  483. fi
  484. _ESCAPE_XARGS=""
  485. if _exists xargs && [ "$(printf %s '\\x41' | xargs printf)" = 'A' ]; then
  486. _ESCAPE_XARGS=1
  487. fi
  488. _h2b() {
  489. if _exists xxd; then
  490. if _contains "$(xxd --help 2>&1)" "assumes -c30"; then
  491. if xxd -r -p -c 9999 2>/dev/null; then
  492. return
  493. fi
  494. else
  495. if xxd -r -p 2>/dev/null; then
  496. return
  497. fi
  498. fi
  499. fi
  500. hex=$(cat)
  501. ic=""
  502. jc=""
  503. _debug2 _URGLY_PRINTF "$_URGLY_PRINTF"
  504. if [ -z "$_URGLY_PRINTF" ]; then
  505. if [ "$_ESCAPE_XARGS" ] && _exists xargs; then
  506. _debug2 "xargs"
  507. echo "$hex" | _upper_case | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/g' | xargs printf
  508. else
  509. for h in $(echo "$hex" | _upper_case | sed 's/\([0-9A-F]\{2\}\)/ \1/g'); do
  510. if [ -z "$h" ]; then
  511. break
  512. fi
  513. printf "\x$h%s"
  514. done
  515. fi
  516. else
  517. for c in $(echo "$hex" | _upper_case | sed 's/\([0-9A-F]\)/ \1/g'); do
  518. if [ -z "$ic" ]; then
  519. ic=$c
  520. continue
  521. fi
  522. jc=$c
  523. ic="$(_h_char_2_dec "$ic")"
  524. jc="$(_h_char_2_dec "$jc")"
  525. printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s"
  526. ic=""
  527. jc=""
  528. done
  529. fi
  530. }
  531. _is_solaris() {
  532. _contains "${__OS__:=$(uname -a)}" "solaris" || _contains "${__OS__:=$(uname -a)}" "SunOS"
  533. }
  534. #_ascii_hex str
  535. #this can only process ascii chars, should only be used when od command is missing as a backup way.
  536. _ascii_hex() {
  537. _debug2 "Using _ascii_hex"
  538. _str="$1"
  539. _str_len=${#_str}
  540. _h_i=1
  541. while [ "$_h_i" -le "$_str_len" ]; do
  542. _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")"
  543. printf " %02x" "'$_str_c"
  544. _h_i="$(_math "$_h_i" + 1)"
  545. done
  546. }
  547. #stdin output hexstr splited by one space
  548. #input:"abc"
  549. #output: " 61 62 63"
  550. _hex_dump() {
  551. if _exists od; then
  552. od -A n -v -t x1 | tr -s " " | sed 's/ $//' | tr -d "\r\t\n"
  553. elif _exists hexdump; then
  554. _debug3 "using hexdump"
  555. hexdump -v -e '/1 ""' -e '/1 " %02x" ""'
  556. elif _exists xxd; then
  557. _debug3 "using xxd"
  558. xxd -ps -c 20 -i | sed "s/ 0x/ /g" | tr -d ",\n" | tr -s " "
  559. else
  560. _debug3 "using _ascii_hex"
  561. str=$(cat)
  562. _ascii_hex "$str"
  563. fi
  564. }
  565. #url encode, no-preserved chars
  566. #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
  567. #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
  568. #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
  569. #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
  570. #0 1 2 3 4 5 6 7 8 9 - _ . ~
  571. #30 31 32 33 34 35 36 37 38 39 2d 5f 2e 7e
  572. #_url_encode [upper-hex] the encoded hex will be upper-case if the argument upper-hex is followed
  573. #stdin stdout
  574. _url_encode() {
  575. _upper_hex=$1
  576. _hex_str=$(_hex_dump)
  577. _debug3 "_url_encode"
  578. _debug3 "_hex_str" "$_hex_str"
  579. for _hex_code in $_hex_str; do
  580. #upper case
  581. case "${_hex_code}" in
  582. "41")
  583. printf "%s" "A"
  584. ;;
  585. "42")
  586. printf "%s" "B"
  587. ;;
  588. "43")
  589. printf "%s" "C"
  590. ;;
  591. "44")
  592. printf "%s" "D"
  593. ;;
  594. "45")
  595. printf "%s" "E"
  596. ;;
  597. "46")
  598. printf "%s" "F"
  599. ;;
  600. "47")
  601. printf "%s" "G"
  602. ;;
  603. "48")
  604. printf "%s" "H"
  605. ;;
  606. "49")
  607. printf "%s" "I"
  608. ;;
  609. "4a")
  610. printf "%s" "J"
  611. ;;
  612. "4b")
  613. printf "%s" "K"
  614. ;;
  615. "4c")
  616. printf "%s" "L"
  617. ;;
  618. "4d")
  619. printf "%s" "M"
  620. ;;
  621. "4e")
  622. printf "%s" "N"
  623. ;;
  624. "4f")
  625. printf "%s" "O"
  626. ;;
  627. "50")
  628. printf "%s" "P"
  629. ;;
  630. "51")
  631. printf "%s" "Q"
  632. ;;
  633. "52")
  634. printf "%s" "R"
  635. ;;
  636. "53")
  637. printf "%s" "S"
  638. ;;
  639. "54")
  640. printf "%s" "T"
  641. ;;
  642. "55")
  643. printf "%s" "U"
  644. ;;
  645. "56")
  646. printf "%s" "V"
  647. ;;
  648. "57")
  649. printf "%s" "W"
  650. ;;
  651. "58")
  652. printf "%s" "X"
  653. ;;
  654. "59")
  655. printf "%s" "Y"
  656. ;;
  657. "5a")
  658. printf "%s" "Z"
  659. ;;
  660. #lower case
  661. "61")
  662. printf "%s" "a"
  663. ;;
  664. "62")
  665. printf "%s" "b"
  666. ;;
  667. "63")
  668. printf "%s" "c"
  669. ;;
  670. "64")
  671. printf "%s" "d"
  672. ;;
  673. "65")
  674. printf "%s" "e"
  675. ;;
  676. "66")
  677. printf "%s" "f"
  678. ;;
  679. "67")
  680. printf "%s" "g"
  681. ;;
  682. "68")
  683. printf "%s" "h"
  684. ;;
  685. "69")
  686. printf "%s" "i"
  687. ;;
  688. "6a")
  689. printf "%s" "j"
  690. ;;
  691. "6b")
  692. printf "%s" "k"
  693. ;;
  694. "6c")
  695. printf "%s" "l"
  696. ;;
  697. "6d")
  698. printf "%s" "m"
  699. ;;
  700. "6e")
  701. printf "%s" "n"
  702. ;;
  703. "6f")
  704. printf "%s" "o"
  705. ;;
  706. "70")
  707. printf "%s" "p"
  708. ;;
  709. "71")
  710. printf "%s" "q"
  711. ;;
  712. "72")
  713. printf "%s" "r"
  714. ;;
  715. "73")
  716. printf "%s" "s"
  717. ;;
  718. "74")
  719. printf "%s" "t"
  720. ;;
  721. "75")
  722. printf "%s" "u"
  723. ;;
  724. "76")
  725. printf "%s" "v"
  726. ;;
  727. "77")
  728. printf "%s" "w"
  729. ;;
  730. "78")
  731. printf "%s" "x"
  732. ;;
  733. "79")
  734. printf "%s" "y"
  735. ;;
  736. "7a")
  737. printf "%s" "z"
  738. ;;
  739. #numbers
  740. "30")
  741. printf "%s" "0"
  742. ;;
  743. "31")
  744. printf "%s" "1"
  745. ;;
  746. "32")
  747. printf "%s" "2"
  748. ;;
  749. "33")
  750. printf "%s" "3"
  751. ;;
  752. "34")
  753. printf "%s" "4"
  754. ;;
  755. "35")
  756. printf "%s" "5"
  757. ;;
  758. "36")
  759. printf "%s" "6"
  760. ;;
  761. "37")
  762. printf "%s" "7"
  763. ;;
  764. "38")
  765. printf "%s" "8"
  766. ;;
  767. "39")
  768. printf "%s" "9"
  769. ;;
  770. "2d")
  771. printf "%s" "-"
  772. ;;
  773. "5f")
  774. printf "%s" "_"
  775. ;;
  776. "2e")
  777. printf "%s" "."
  778. ;;
  779. "7e")
  780. printf "%s" "~"
  781. ;;
  782. #other hex
  783. *)
  784. if [ "$_upper_hex" = "upper-hex" ]; then
  785. _hex_code=$(printf "%s" "$_hex_code" | _upper_case)
  786. fi
  787. printf '%%%s' "$_hex_code"
  788. ;;
  789. esac
  790. done
  791. }
  792. _json_encode() {
  793. _j_str="$(sed 's/"/\\"/g' | sed "s/\r/\\r/g")"
  794. _debug3 "_json_encode"
  795. _debug3 "_j_str" "$_j_str"
  796. echo "$_j_str" | _hex_dump | _lower_case | sed 's/0a/5c 6e/g' | tr -d ' ' | _h2b | tr -d "\r\n"
  797. }
  798. #from: http:\/\/ to http://
  799. _json_decode() {
  800. _j_str="$(sed 's#\\/#/#g')"
  801. _debug3 "_json_decode"
  802. _debug3 "_j_str" "$_j_str"
  803. echo "$_j_str"
  804. }
  805. #options file
  806. _sed_i() {
  807. options="$1"
  808. filename="$2"
  809. if [ -z "$filename" ]; then
  810. _usage "Usage:_sed_i options filename"
  811. return 1
  812. fi
  813. _debug2 options "$options"
  814. if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then
  815. _debug "Using sed -i"
  816. sed -i "$options" "$filename"
  817. elif sed -h 2>&1 | grep "\-i extension" >/dev/null 2>&1; then
  818. _debug "Using FreeBSD sed -i"
  819. sed -i "" "$options" "$filename"
  820. else
  821. _debug "No -i support in sed"
  822. text="$(cat "$filename")"
  823. echo "$text" | sed "$options" >"$filename"
  824. fi
  825. }
  826. if [ "$(echo abc | egrep -o b 2>/dev/null)" = "b" ]; then
  827. __USE_EGREP=1
  828. else
  829. __USE_EGREP=""
  830. fi
  831. _egrep_o() {
  832. if [ "$__USE_EGREP" ]; then
  833. egrep -o -- "$1" 2>/dev/null
  834. else
  835. sed -n 's/.*\('"$1"'\).*/\1/p'
  836. fi
  837. }
  838. #Usage: file startline endline
  839. _getfile() {
  840. filename="$1"
  841. startline="$2"
  842. endline="$3"
  843. if [ -z "$endline" ]; then
  844. _usage "Usage: file startline endline"
  845. return 1
  846. fi
  847. i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)"
  848. if [ -z "$i" ]; then
  849. _err "Cannot find start line: $startline"
  850. return 1
  851. fi
  852. i="$(_math "$i" + 1)"
  853. _debug i "$i"
  854. j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)"
  855. if [ -z "$j" ]; then
  856. _err "Cannot find end line: $endline"
  857. return 1
  858. fi
  859. j="$(_math "$j" - 1)"
  860. _debug j "$j"
  861. sed -n "$i,${j}p" "$filename"
  862. }
  863. #Usage: multiline
  864. _base64() {
  865. [ "" ] #urgly
  866. if [ "$1" ]; then
  867. _debug3 "base64 multiline:'$1'"
  868. ${ACME_OPENSSL_BIN:-openssl} base64 -e
  869. else
  870. _debug3 "base64 single line."
  871. ${ACME_OPENSSL_BIN:-openssl} base64 -e | tr -d '\r\n'
  872. fi
  873. }
  874. #Usage: multiline
  875. _dbase64() {
  876. if [ "$1" ]; then
  877. ${ACME_OPENSSL_BIN:-openssl} base64 -d
  878. else
  879. ${ACME_OPENSSL_BIN:-openssl} base64 -d -A
  880. fi
  881. }
  882. #file
  883. _checkcert() {
  884. _cf="$1"
  885. if [ "$DEBUG" ]; then
  886. ${ACME_OPENSSL_BIN:-openssl} x509 -noout -text -in "$_cf"
  887. else
  888. ${ACME_OPENSSL_BIN:-openssl} x509 -noout -text -in "$_cf" >/dev/null 2>&1
  889. fi
  890. }
  891. #Usage: hashalg [outputhex]
  892. #Output Base64-encoded digest
  893. _digest() {
  894. alg="$1"
  895. if [ -z "$alg" ]; then
  896. _usage "Usage: _digest hashalg"
  897. return 1
  898. fi
  899. outputhex="$2"
  900. if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ] || [ "$alg" = "md5" ]; then
  901. if [ "$outputhex" ]; then
  902. ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' '
  903. else
  904. ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -binary | _base64
  905. fi
  906. else
  907. _err "$alg is not supported yet"
  908. return 1
  909. fi
  910. }
  911. #Usage: hashalg secret_hex [outputhex]
  912. #Output binary hmac
  913. _hmac() {
  914. alg="$1"
  915. secret_hex="$2"
  916. outputhex="$3"
  917. if [ -z "$secret_hex" ]; then
  918. _usage "Usage: _hmac hashalg secret [outputhex]"
  919. return 1
  920. fi
  921. if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ]; then
  922. if [ "$outputhex" ]; then
  923. (${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 ' '
  924. else
  925. ${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
  926. fi
  927. else
  928. _err "$alg is not supported yet"
  929. return 1
  930. fi
  931. }
  932. #Usage: keyfile hashalg
  933. #Output: Base64-encoded signature value
  934. _sign() {
  935. keyfile="$1"
  936. alg="$2"
  937. if [ -z "$alg" ]; then
  938. _usage "Usage: _sign keyfile hashalg"
  939. return 1
  940. fi
  941. _sign_openssl="${ACME_OPENSSL_BIN:-openssl} dgst -sign $keyfile "
  942. if _isRSA "$keyfile" >/dev/null 2>&1; then
  943. $_sign_openssl -$alg | _base64
  944. elif _isEcc "$keyfile" >/dev/null 2>&1; then
  945. if ! _signedECText="$($_sign_openssl -sha$__ECC_KEY_LEN | ${ACME_OPENSSL_BIN:-openssl} asn1parse -inform DER)"; then
  946. _err "Sign failed: $_sign_openssl"
  947. _err "Key file: $keyfile"
  948. _err "Key content: $(wc -l <"$keyfile") lines"
  949. return 1
  950. fi
  951. _debug3 "_signedECText" "$_signedECText"
  952. _ec_r="$(echo "$_signedECText" | _head_n 2 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")"
  953. _ec_s="$(echo "$_signedECText" | _head_n 3 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")"
  954. if [ "$__ECC_KEY_LEN" -eq "256" ]; then
  955. while [ "${#_ec_r}" -lt "64" ]; do
  956. _ec_r="0${_ec_r}"
  957. done
  958. while [ "${#_ec_s}" -lt "64" ]; do
  959. _ec_s="0${_ec_s}"
  960. done
  961. fi
  962. if [ "$__ECC_KEY_LEN" -eq "384" ]; then
  963. while [ "${#_ec_r}" -lt "96" ]; do
  964. _ec_r="0${_ec_r}"
  965. done
  966. while [ "${#_ec_s}" -lt "96" ]; do
  967. _ec_s="0${_ec_s}"
  968. done
  969. fi
  970. if [ "$__ECC_KEY_LEN" -eq "512" ]; then
  971. while [ "${#_ec_r}" -lt "132" ]; do
  972. _ec_r="0${_ec_r}"
  973. done
  974. while [ "${#_ec_s}" -lt "132" ]; do
  975. _ec_s="0${_ec_s}"
  976. done
  977. fi
  978. _debug3 "_ec_r" "$_ec_r"
  979. _debug3 "_ec_s" "$_ec_s"
  980. printf "%s" "$_ec_r$_ec_s" | _h2b | _base64
  981. else
  982. _err "Unknown key file format."
  983. return 1
  984. fi
  985. }
  986. #keylength or isEcc flag (empty str => not ecc)
  987. _isEccKey() {
  988. _length="$1"
  989. if [ -z "$_length" ]; then
  990. return 1
  991. fi
  992. [ "$_length" != "1024" ] &&
  993. [ "$_length" != "2048" ] &&
  994. [ "$_length" != "3072" ] &&
  995. [ "$_length" != "4096" ] &&
  996. [ "$_length" != "8192" ]
  997. }
  998. # _createkey 2048|ec-256 file
  999. _createkey() {
  1000. length="$1"
  1001. f="$2"
  1002. _debug2 "_createkey for file:$f"
  1003. eccname="$length"
  1004. if _startswith "$length" "ec-"; then
  1005. length=$(printf "%s" "$length" | cut -d '-' -f 2-100)
  1006. if [ "$length" = "256" ]; then
  1007. eccname="prime256v1"
  1008. fi
  1009. if [ "$length" = "384" ]; then
  1010. eccname="secp384r1"
  1011. fi
  1012. if [ "$length" = "521" ]; then
  1013. eccname="secp521r1"
  1014. fi
  1015. fi
  1016. if [ -z "$length" ]; then
  1017. length=2048
  1018. fi
  1019. _debug "Using length $length"
  1020. if ! [ -e "$f" ]; then
  1021. if ! touch "$f" >/dev/null 2>&1; then
  1022. _f_path="$(dirname "$f")"
  1023. _debug _f_path "$_f_path"
  1024. if ! mkdir -p "$_f_path"; then
  1025. _err "Cannot create path: $_f_path"
  1026. return 1
  1027. fi
  1028. fi
  1029. if ! touch "$f" >/dev/null 2>&1; then
  1030. return 1
  1031. fi
  1032. chmod 600 "$f"
  1033. fi
  1034. if _isEccKey "$length"; then
  1035. _debug "Using EC name: $eccname"
  1036. if _opkey="$(${ACME_OPENSSL_BIN:-openssl} ecparam -name "$eccname" -noout -genkey 2>/dev/null)"; then
  1037. echo "$_opkey" >"$f"
  1038. else
  1039. _err "Error encountered for ECC key named $eccname"
  1040. return 1
  1041. fi
  1042. else
  1043. _debug "Using RSA: $length"
  1044. __traditional=""
  1045. if _contains "$(${ACME_OPENSSL_BIN:-openssl} help genrsa 2>&1)" "-traditional"; then
  1046. __traditional="-traditional"
  1047. fi
  1048. if _opkey="$(${ACME_OPENSSL_BIN:-openssl} genrsa $__traditional "$length" 2>/dev/null)"; then
  1049. echo "$_opkey" >"$f"
  1050. else
  1051. _err "Error encountered for RSA key of length $length"
  1052. return 1
  1053. fi
  1054. fi
  1055. if [ "$?" != "0" ]; then
  1056. _err "Key creation error."
  1057. return 1
  1058. fi
  1059. }
  1060. #domain
  1061. _is_idn() {
  1062. _is_idn_d="$1"
  1063. _debug2 _is_idn_d "$_is_idn_d"
  1064. _idn_temp=$(printf "%s" "$_is_idn_d" | tr -d '[0-9]' | tr -d '[a-z]' | tr -d '[A-Z]' | tr -d '*.,-_')
  1065. _debug2 _idn_temp "$_idn_temp"
  1066. [ "$_idn_temp" ]
  1067. }
  1068. #aa.com
  1069. #aa.com,bb.com,cc.com
  1070. _idn() {
  1071. __idn_d="$1"
  1072. if ! _is_idn "$__idn_d"; then
  1073. printf "%s" "$__idn_d"
  1074. return 0
  1075. fi
  1076. if _exists idn; then
  1077. if _contains "$__idn_d" ','; then
  1078. _i_first="1"
  1079. for f in $(echo "$__idn_d" | tr ',' ' '); do
  1080. [ -z "$f" ] && continue
  1081. if [ -z "$_i_first" ]; then
  1082. printf "%s" ","
  1083. else
  1084. _i_first=""
  1085. fi
  1086. idn --quiet "$f" | tr -d "\r\n"
  1087. done
  1088. else
  1089. idn "$__idn_d" | tr -d "\r\n"
  1090. fi
  1091. else
  1092. _err "Please install idn to process IDN names."
  1093. fi
  1094. }
  1095. #_createcsr cn san_list keyfile csrfile conf acmeValidationv1
  1096. _createcsr() {
  1097. _debug _createcsr
  1098. domain="$1"
  1099. domainlist="$2"
  1100. csrkey="$3"
  1101. csr="$4"
  1102. csrconf="$5"
  1103. acmeValidationv1="$6"
  1104. _debug2 domain "$domain"
  1105. _debug2 domainlist "$domainlist"
  1106. _debug2 csrkey "$csrkey"
  1107. _debug2 csr "$csr"
  1108. _debug2 csrconf "$csrconf"
  1109. printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\nreq_extensions = v3_req\n[ v3_req ]" >"$csrconf"
  1110. if [ "$Le_ExtKeyUse" ]; then
  1111. _savedomainconf Le_ExtKeyUse "$Le_ExtKeyUse"
  1112. printf "\nextendedKeyUsage=$Le_ExtKeyUse\n" >>"$csrconf"
  1113. else
  1114. printf "\nextendedKeyUsage=serverAuth,clientAuth\n" >>"$csrconf"
  1115. fi
  1116. if [ "$acmeValidationv1" ]; then
  1117. domainlist="$(_idn "$domainlist")"
  1118. _debug2 domainlist "$domainlist"
  1119. alt=""
  1120. for dl in $(echo "$domainlist" | tr "," ' '); do
  1121. if [ "$alt" ]; then
  1122. alt="$alt,$(_getIdType "$dl" | _upper_case):$dl"
  1123. else
  1124. alt="$(_getIdType "$dl" | _upper_case):$dl"
  1125. fi
  1126. done
  1127. printf -- "\nsubjectAltName=$alt" >>"$csrconf"
  1128. elif [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then
  1129. #single domain
  1130. _info "Single domain" "$domain"
  1131. printf -- "\nsubjectAltName=$(_getIdType "$domain" | _upper_case):$(_idn "$domain")" >>"$csrconf"
  1132. else
  1133. domainlist="$(_idn "$domainlist")"
  1134. _debug2 domainlist "$domainlist"
  1135. alt="$(_getIdType "$domain" | _upper_case):$(_idn "$domain")"
  1136. for dl in $(echo "'$domainlist'" | sed "s/,/' '/g"); do
  1137. dl=$(echo "$dl" | tr -d "'")
  1138. alt="$alt,$(_getIdType "$dl" | _upper_case):$dl"
  1139. done
  1140. #multi
  1141. _info "Multi domain" "$alt"
  1142. printf -- "\nsubjectAltName=$alt" >>"$csrconf"
  1143. fi
  1144. if [ "$Le_OCSP_Staple" = "1" ]; then
  1145. _savedomainconf Le_OCSP_Staple "$Le_OCSP_Staple"
  1146. printf -- "\nbasicConstraints = CA:FALSE\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >>"$csrconf"
  1147. fi
  1148. if [ "$acmeValidationv1" ]; then
  1149. printf "\n1.3.6.1.5.5.7.1.31=critical,DER:04:20:${acmeValidationv1}" >>"${csrconf}"
  1150. fi
  1151. _csr_cn="$(_idn "$domain")"
  1152. _debug2 _csr_cn "$_csr_cn"
  1153. if _contains "$(uname -a)" "MINGW"; then
  1154. if _isIP "$_csr_cn"; then
  1155. ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//O=$PROJECT_NAME" -config "$csrconf" -out "$csr"
  1156. else
  1157. ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//CN=$_csr_cn" -config "$csrconf" -out "$csr"
  1158. fi
  1159. else
  1160. if _isIP "$_csr_cn"; then
  1161. ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/O=$PROJECT_NAME" -config "$csrconf" -out "$csr"
  1162. else
  1163. ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/CN=$_csr_cn" -config "$csrconf" -out "$csr"
  1164. fi
  1165. fi
  1166. }
  1167. #_signcsr key csr conf cert
  1168. _signcsr() {
  1169. key="$1"
  1170. csr="$2"
  1171. conf="$3"
  1172. cert="$4"
  1173. _debug "_signcsr"
  1174. _msg="$(${ACME_OPENSSL_BIN:-openssl} x509 -req -days 365 -in "$csr" -signkey "$key" -extensions v3_req -extfile "$conf" -out "$cert" 2>&1)"
  1175. _ret="$?"
  1176. _debug "$_msg"
  1177. return $_ret
  1178. }
  1179. #_csrfile
  1180. _readSubjectFromCSR() {
  1181. _csrfile="$1"
  1182. if [ -z "$_csrfile" ]; then
  1183. _usage "_readSubjectFromCSR mycsr.csr"
  1184. return 1
  1185. fi
  1186. ${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'
  1187. }
  1188. #_csrfile
  1189. #echo comma separated domain list
  1190. _readSubjectAltNamesFromCSR() {
  1191. _csrfile="$1"
  1192. if [ -z "$_csrfile" ]; then
  1193. _usage "_readSubjectAltNamesFromCSR mycsr.csr"
  1194. return 1
  1195. fi
  1196. _csrsubj="$(_readSubjectFromCSR "$_csrfile")"
  1197. _debug _csrsubj "$_csrsubj"
  1198. _dnsAltnames="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile" | grep "^ *DNS:.*" | tr -d ' \n')"
  1199. _debug _dnsAltnames "$_dnsAltnames"
  1200. if _contains "$_dnsAltnames," "DNS:$_csrsubj,"; then
  1201. _debug "AltNames contains subject"
  1202. _excapedAlgnames="$(echo "$_dnsAltnames" | tr '*' '#')"
  1203. _debug _excapedAlgnames "$_excapedAlgnames"
  1204. _escapedSubject="$(echo "$_csrsubj" | tr '*' '#')"
  1205. _debug _escapedSubject "$_escapedSubject"
  1206. _dnsAltnames="$(echo "$_excapedAlgnames," | sed "s/DNS:$_escapedSubject,//g" | tr '#' '*' | sed "s/,\$//g")"
  1207. _debug _dnsAltnames "$_dnsAltnames"
  1208. else
  1209. _debug "AltNames doesn't contain subject"
  1210. fi
  1211. echo "$_dnsAltnames" | sed "s/DNS://g"
  1212. }
  1213. #_csrfile
  1214. _readKeyLengthFromCSR() {
  1215. _csrfile="$1"
  1216. if [ -z "$_csrfile" ]; then
  1217. _usage "_readKeyLengthFromCSR mycsr.csr"
  1218. return 1
  1219. fi
  1220. _outcsr="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile")"
  1221. _debug2 _outcsr "$_outcsr"
  1222. if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then
  1223. _debug "ECC CSR"
  1224. echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' '
  1225. else
  1226. _debug "RSA CSR"
  1227. _rkl="$(echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1)"
  1228. if [ "$_rkl" ]; then
  1229. echo "$_rkl"
  1230. else
  1231. echo "$_outcsr" | tr "\t" " " | _egrep_o "RSA Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1
  1232. fi
  1233. fi
  1234. }
  1235. _ss() {
  1236. _port="$1"
  1237. if _exists "ss"; then
  1238. _debug "Using: ss"
  1239. ss -ntpl 2>/dev/null | grep ":$_port "
  1240. return 0
  1241. fi
  1242. if [ "$(uname)" = "AIX" ]; then
  1243. _debug "Using: AIX netstat"
  1244. netstat -an | grep "^tcp" | grep "LISTEN" | grep "\.$_port "
  1245. return 0
  1246. fi
  1247. if _exists "netstat"; then
  1248. _debug "Using: netstat"
  1249. if netstat -help 2>&1 | grep "\-p proto" >/dev/null; then
  1250. #for windows version netstat tool
  1251. netstat -an -p tcp | grep "LISTENING" | grep ":$_port "
  1252. else
  1253. if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then
  1254. netstat -an -p tcp | grep LISTEN | grep ":$_port "
  1255. elif netstat -help 2>&1 | grep -- '-P protocol' >/dev/null; then
  1256. #for solaris
  1257. netstat -an -P tcp | grep "\.$_port " | grep "LISTEN"
  1258. elif netstat -help 2>&1 | grep "\-p" >/dev/null; then
  1259. #for full linux
  1260. netstat -ntpl | grep ":$_port "
  1261. else
  1262. #for busybox (embedded linux; no pid support)
  1263. netstat -ntl 2>/dev/null | grep ":$_port "
  1264. fi
  1265. fi
  1266. return 0
  1267. fi
  1268. return 1
  1269. }
  1270. #outfile key cert cacert [password [name [caname]]]
  1271. _toPkcs() {
  1272. _cpfx="$1"
  1273. _ckey="$2"
  1274. _ccert="$3"
  1275. _cca="$4"
  1276. pfxPassword="$5"
  1277. pfxName="$6"
  1278. pfxCaname="$7"
  1279. if [ "$pfxCaname" ]; then
  1280. ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" -caname "$pfxCaname"
  1281. elif [ "$pfxName" ]; then
  1282. ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName"
  1283. elif [ "$pfxPassword" ]; then
  1284. ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword"
  1285. else
  1286. ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca"
  1287. fi
  1288. if [ "$?" = "0" ]; then
  1289. _savedomainconf "Le_PFXPassword" "$pfxPassword"
  1290. fi
  1291. }
  1292. #domain [password] [isEcc]
  1293. toPkcs() {
  1294. domain="$1"
  1295. pfxPassword="$2"
  1296. if [ -z "$domain" ]; then
  1297. _usage "Usage: $PROJECT_ENTRY --to-pkcs12 --domain <domain.tld> [--password <password>] [--ecc]"
  1298. return 1
  1299. fi
  1300. _isEcc="$3"
  1301. _initpath "$domain" "$_isEcc"
  1302. _toPkcs "$CERT_PFX_PATH" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$pfxPassword"
  1303. if [ "$?" = "0" ]; then
  1304. _info "Success, PFX has been exported to: $CERT_PFX_PATH"
  1305. fi
  1306. }
  1307. #domain [isEcc]
  1308. toPkcs8() {
  1309. domain="$1"
  1310. if [ -z "$domain" ]; then
  1311. _usage "Usage: $PROJECT_ENTRY --to-pkcs8 --domain <domain.tld> [--ecc]"
  1312. return 1
  1313. fi
  1314. _isEcc="$2"
  1315. _initpath "$domain" "$_isEcc"
  1316. ${ACME_OPENSSL_BIN:-openssl} pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in "$CERT_KEY_PATH" -out "$CERT_PKCS8_PATH"
  1317. if [ "$?" = "0" ]; then
  1318. _info "Success, $CERT_PKCS8_PATH"
  1319. fi
  1320. }
  1321. #[2048]
  1322. createAccountKey() {
  1323. _info "Creating account key"
  1324. if [ -z "$1" ]; then
  1325. _usage "Usage: $PROJECT_ENTRY --create-account-key [--accountkeylength <bits>]"
  1326. return
  1327. fi
  1328. length=$1
  1329. _create_account_key "$length"
  1330. }
  1331. _create_account_key() {
  1332. length=$1
  1333. if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then
  1334. _debug "Using default length $DEFAULT_ACCOUNT_KEY_LENGTH"
  1335. length="$DEFAULT_ACCOUNT_KEY_LENGTH"
  1336. fi
  1337. _debug length "$length"
  1338. _initpath
  1339. mkdir -p "$CA_DIR"
  1340. if [ -s "$ACCOUNT_KEY_PATH" ]; then
  1341. _info "Account key exists, skipping"
  1342. return 0
  1343. else
  1344. #generate account key
  1345. if _createkey "$length" "$ACCOUNT_KEY_PATH"; then
  1346. _info "Account key creation OK."
  1347. return 0
  1348. else
  1349. _err "Account key creation error."
  1350. return 1
  1351. fi
  1352. fi
  1353. }
  1354. #domain [length]
  1355. createDomainKey() {
  1356. _info "Creating domain key"
  1357. if [ -z "$1" ]; then
  1358. _usage "Usage: $PROJECT_ENTRY --create-domain-key --domain <domain.tld> [--keylength <bits>]"
  1359. return
  1360. fi
  1361. domain=$1
  1362. _cdl=$2
  1363. if [ -z "$_cdl" ]; then
  1364. _debug "Using DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH"
  1365. _cdl="$DEFAULT_DOMAIN_KEY_LENGTH"
  1366. fi
  1367. _initpath "$domain" "$_cdl"
  1368. if [ ! -f "$CERT_KEY_PATH" ] || [ ! -s "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$_ACME_IS_RENEW" ]) || [ "$Le_ForceNewDomainKey" = "1" ]; then
  1369. if _createkey "$_cdl" "$CERT_KEY_PATH"; then
  1370. _savedomainconf Le_Keylength "$_cdl"
  1371. _info "The domain key is here: $(__green $CERT_KEY_PATH)"
  1372. return 0
  1373. else
  1374. _err "Cannot create domain key"
  1375. return 1
  1376. fi
  1377. else
  1378. if [ "$_ACME_IS_RENEW" ]; then
  1379. _info "Domain key exists, skipping"
  1380. return 0
  1381. else
  1382. _err "Domain key exists, do you want to overwrite it?"
  1383. _err "If so, add '--force' and try again."
  1384. return 1
  1385. fi
  1386. fi
  1387. }
  1388. # domain domainlist isEcc
  1389. createCSR() {
  1390. _info "Creating CSR"
  1391. if [ -z "$1" ]; then
  1392. _usage "Usage: $PROJECT_ENTRY --create-csr --domain <domain.tld> [--domain <domain2.tld> ...] [--ecc]"
  1393. return
  1394. fi
  1395. domain="$1"
  1396. domainlist="$2"
  1397. _isEcc="$3"
  1398. _initpath "$domain" "$_isEcc"
  1399. if [ -f "$CSR_PATH" ] && [ "$_ACME_IS_RENEW" ] && [ -z "$FORCE" ]; then
  1400. _info "CSR exists, skipping"
  1401. return
  1402. fi
  1403. if [ ! -f "$CERT_KEY_PATH" ]; then
  1404. _err "This key file was not found: $CERT_KEY_PATH"
  1405. _err "Please create it first."
  1406. return 1
  1407. fi
  1408. _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"
  1409. }
  1410. _url_replace() {
  1411. tr '/+' '_-' | tr -d '= '
  1412. }
  1413. #base64 string
  1414. _durl_replace_base64() {
  1415. _l=$((${#1} % 4))
  1416. if [ $_l -eq 2 ]; then
  1417. _s="$1"'=='
  1418. elif [ $_l -eq 3 ]; then
  1419. _s="$1"'='
  1420. else
  1421. _s="$1"
  1422. fi
  1423. echo "$_s" | tr '_-' '/+'
  1424. }
  1425. _time2str() {
  1426. #BSD
  1427. if date -u -r "$1" -j "+%Y-%m-%dT%H:%M:%SZ" 2>/dev/null; then
  1428. return
  1429. fi
  1430. #Linux
  1431. if date -u --date=@"$1" "+%Y-%m-%dT%H:%M:%SZ" 2>/dev/null; then
  1432. return
  1433. fi
  1434. #Omnios
  1435. if date -u -r "$1" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null; then
  1436. return
  1437. fi
  1438. #Solaris
  1439. if printf "%(%Y-%m-%dT%H:%M:%SZ)T\n" $1 2>/dev/null; then
  1440. return
  1441. fi
  1442. #Busybox
  1443. if echo "$1" | awk '{ print strftime("%Y-%m-%dT%H:%M:%SZ", $0); }' 2>/dev/null; then
  1444. return
  1445. fi
  1446. }
  1447. _normalizeJson() {
  1448. sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n"
  1449. }
  1450. _stat() {
  1451. #Linux
  1452. if stat -c '%U:%G' "$1" 2>/dev/null; then
  1453. return
  1454. fi
  1455. #BSD
  1456. if stat -f '%Su:%Sg' "$1" 2>/dev/null; then
  1457. return
  1458. fi
  1459. return 1 #error, 'stat' not found
  1460. }
  1461. #keyfile
  1462. _isRSA() {
  1463. keyfile=$1
  1464. if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1 || ${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -noout -text 2>&1 | grep "^publicExponent:" 2>&1 >/dev/null; then
  1465. return 0
  1466. fi
  1467. return 1
  1468. }
  1469. #keyfile
  1470. _isEcc() {
  1471. keyfile=$1
  1472. if grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1 || ${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" 2>&1 >/dev/null; then
  1473. return 0
  1474. fi
  1475. return 1
  1476. }
  1477. #keyfile
  1478. _calcjwk() {
  1479. keyfile="$1"
  1480. if [ -z "$keyfile" ]; then
  1481. _usage "Usage: _calcjwk keyfile"
  1482. return 1
  1483. fi
  1484. if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then
  1485. _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE"
  1486. return 0
  1487. fi
  1488. if _isRSA "$keyfile"; then
  1489. _debug "RSA key"
  1490. 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)
  1491. if [ "${#pub_exp}" = "5" ]; then
  1492. pub_exp=0$pub_exp
  1493. fi
  1494. _debug3 pub_exp "$pub_exp"
  1495. e=$(echo "$pub_exp" | _h2b | _base64)
  1496. _debug3 e "$e"
  1497. modulus=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2)
  1498. _debug3 modulus "$modulus"
  1499. n="$(printf "%s" "$modulus" | _h2b | _base64 | _url_replace)"
  1500. _debug3 n "$n"
  1501. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  1502. _debug3 jwk "$jwk"
  1503. JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  1504. JWK_HEADERPLACE_PART1='{"nonce": "'
  1505. JWK_HEADERPLACE_PART2='", "alg": "RS256"'
  1506. elif _isEcc "$keyfile"; then
  1507. _debug "EC key"
  1508. crv="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
  1509. _debug3 crv "$crv"
  1510. __ECC_KEY_LEN=$(echo "$crv" | cut -d "-" -f 2)
  1511. if [ "$__ECC_KEY_LEN" = "521" ]; then
  1512. __ECC_KEY_LEN=512
  1513. fi
  1514. _debug3 __ECC_KEY_LEN "$__ECC_KEY_LEN"
  1515. if [ -z "$crv" ]; then
  1516. _debug "Let's try ASN1 OID"
  1517. 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")"
  1518. _debug3 crv_oid "$crv_oid"
  1519. case "${crv_oid}" in
  1520. "prime256v1")
  1521. crv="P-256"
  1522. __ECC_KEY_LEN=256
  1523. ;;
  1524. "secp384r1")
  1525. crv="P-384"
  1526. __ECC_KEY_LEN=384
  1527. ;;
  1528. "secp521r1")
  1529. crv="P-521"
  1530. __ECC_KEY_LEN=512
  1531. ;;
  1532. *)
  1533. _err "ECC oid: $crv_oid"
  1534. return 1
  1535. ;;
  1536. esac
  1537. _debug3 crv "$crv"
  1538. fi
  1539. pubi="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)"
  1540. pubi=$(_math "$pubi" + 1)
  1541. _debug3 pubi "$pubi"
  1542. pubj="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)"
  1543. pubj=$(_math "$pubj" - 1)
  1544. _debug3 pubj "$pubj"
  1545. pubtext="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")"
  1546. _debug3 pubtext "$pubtext"
  1547. xlen="$(printf "%s" "$pubtext" | tr -d ':' | wc -c)"
  1548. xlen=$(_math "$xlen" / 4)
  1549. _debug3 xlen "$xlen"
  1550. xend=$(_math "$xlen" + 1)
  1551. x="$(printf "%s" "$pubtext" | cut -d : -f 2-"$xend")"
  1552. _debug3 x "$x"
  1553. x64="$(printf "%s" "$x" | tr -d : | _h2b | _base64 | _url_replace)"
  1554. _debug3 x64 "$x64"
  1555. xend=$(_math "$xend" + 1)
  1556. y="$(printf "%s" "$pubtext" | cut -d : -f "$xend"-2048)"
  1557. _debug3 y "$y"
  1558. y64="$(printf "%s" "$y" | tr -d : | _h2b | _base64 | _url_replace)"
  1559. _debug3 y64 "$y64"
  1560. jwk='{"crv": "'$crv'", "kty": "EC", "x": "'$x64'", "y": "'$y64'"}'
  1561. _debug3 jwk "$jwk"
  1562. JWK_HEADER='{"alg": "ES'$__ECC_KEY_LEN'", "jwk": '$jwk'}'
  1563. JWK_HEADERPLACE_PART1='{"nonce": "'
  1564. JWK_HEADERPLACE_PART2='", "alg": "ES'$__ECC_KEY_LEN'"'
  1565. else
  1566. _err "Only RSA or EC keys are supported. keyfile=$keyfile"
  1567. _debug2 "$(cat "$keyfile")"
  1568. return 1
  1569. fi
  1570. _debug3 JWK_HEADER "$JWK_HEADER"
  1571. __CACHED_JWK_KEY_FILE="$keyfile"
  1572. }
  1573. _time() {
  1574. date -u "+%s"
  1575. }
  1576. #support 2 formats:
  1577. # 2022-04-01 08:10:33 to 1648800633
  1578. #or 2022-04-01T08:10:33Z to 1648800633
  1579. _date2time() {
  1580. #Mac/BSD
  1581. if date -u -j -f "%Y-%m-%d %H:%M:%S" "$(echo "$1" | tr -d "Z" | tr "T" ' ')" +"%s" 2>/dev/null; then
  1582. return
  1583. fi
  1584. #Linux
  1585. if date -u -d "$(echo "$1" | tr -d "Z" | tr "T" ' ')" +"%s" 2>/dev/null; then
  1586. return
  1587. fi
  1588. #Solaris
  1589. if gdate -u -d "$(echo "$1" | tr -d "Z" | tr "T" ' ')" +"%s" 2>/dev/null; then
  1590. return
  1591. fi
  1592. #Omnios
  1593. if python3 -c "import datetime; print(int(datetime.datetime.strptime(\"$1\", \"%Y-%m-%d %H:%M:%S\").replace(tzinfo=datetime.timezone.utc).timestamp()))" 2>/dev/null; then
  1594. return
  1595. fi
  1596. #Omnios
  1597. if python3 -c "import datetime; print(int(datetime.datetime.strptime(\"$1\", \"%Y-%m-%dT%H:%M:%SZ\").replace(tzinfo=datetime.timezone.utc).timestamp()))" 2>/dev/null; then
  1598. return
  1599. fi
  1600. _err "Cannot parse _date2time $1"
  1601. return 1
  1602. }
  1603. _utc_date() {
  1604. date -u "+%Y-%m-%d %H:%M:%S"
  1605. }
  1606. _mktemp() {
  1607. if _exists mktemp; then
  1608. if mktemp 2>/dev/null; then
  1609. return 0
  1610. elif _contains "$(mktemp 2>&1)" "-t prefix" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then
  1611. #for Mac osx
  1612. return 0
  1613. fi
  1614. fi
  1615. if [ -d "/tmp" ]; then
  1616. echo "/tmp/${PROJECT_NAME}wefADf24sf.$(_time).tmp"
  1617. return 0
  1618. elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then
  1619. echo "/$LE_TEMP_DIR/wefADf24sf.$(_time).tmp"
  1620. return 0
  1621. fi
  1622. _err "Cannot create temp file."
  1623. }
  1624. #clear all the https envs to cause _inithttp() to run next time.
  1625. _resethttp() {
  1626. __HTTP_INITIALIZED=""
  1627. _ACME_CURL=""
  1628. _ACME_WGET=""
  1629. ACME_HTTP_NO_REDIRECTS=""
  1630. }
  1631. _inithttp() {
  1632. if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then
  1633. HTTP_HEADER="$(_mktemp)"
  1634. _debug2 HTTP_HEADER "$HTTP_HEADER"
  1635. fi
  1636. if [ "$__HTTP_INITIALIZED" ]; then
  1637. if [ "$_ACME_CURL$_ACME_WGET" ]; then
  1638. _debug2 "Http already initialized."
  1639. return 0
  1640. fi
  1641. fi
  1642. if [ -z "$_ACME_CURL" ] && _exists "curl"; then
  1643. _ACME_CURL="curl --silent --dump-header $HTTP_HEADER "
  1644. if [ -z "$ACME_HTTP_NO_REDIRECTS" ]; then
  1645. _ACME_CURL="$_ACME_CURL -L "
  1646. fi
  1647. if [ "$DEBUG" ] && [ "$DEBUG" -ge 2 ]; then
  1648. _CURL_DUMP="$(_mktemp)"
  1649. _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP "
  1650. fi
  1651. if [ "$CA_PATH" ]; then
  1652. _ACME_CURL="$_ACME_CURL --capath $CA_PATH "
  1653. elif [ "$CA_BUNDLE" ]; then
  1654. _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE "
  1655. fi
  1656. if _contains "$(curl --help 2>&1)" "--globoff" || _contains "$(curl --help curl 2>&1)" "--globoff"; then
  1657. _ACME_CURL="$_ACME_CURL -g "
  1658. fi
  1659. #don't use --fail-with-body
  1660. ##from curl 7.76: return fail on HTTP errors but keep the body
  1661. #if _contains "$(curl --help http 2>&1)" "--fail-with-body"; then
  1662. # _ACME_CURL="$_ACME_CURL --fail-with-body "
  1663. #fi
  1664. fi
  1665. if [ -z "$_ACME_WGET" ] && _exists "wget"; then
  1666. _ACME_WGET="wget -q"
  1667. if [ "$ACME_HTTP_NO_REDIRECTS" ]; then
  1668. _ACME_WGET="$_ACME_WGET --max-redirect 0 "
  1669. fi
  1670. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  1671. if [ "$_ACME_WGET" ] && _contains "$($_ACME_WGET --help 2>&1)" "--debug"; then
  1672. _ACME_WGET="$_ACME_WGET -d "
  1673. fi
  1674. fi
  1675. if [ "$CA_PATH" ]; then
  1676. _ACME_WGET="$_ACME_WGET --ca-directory=$CA_PATH "
  1677. elif [ "$CA_BUNDLE" ]; then
  1678. _ACME_WGET="$_ACME_WGET --ca-certificate=$CA_BUNDLE "
  1679. fi
  1680. #from wget 1.14: do not skip body on 404 error
  1681. if _contains "$(wget --help 2>&1)" "--content-on-error"; then
  1682. _ACME_WGET="$_ACME_WGET --content-on-error "
  1683. fi
  1684. fi
  1685. __HTTP_INITIALIZED=1
  1686. }
  1687. # body url [needbase64] [POST|PUT|DELETE] [ContentType]
  1688. _post() {
  1689. body="$1"
  1690. _post_url="$2"
  1691. needbase64="$3"
  1692. httpmethod="$4"
  1693. _postContentType="$5"
  1694. if [ -z "$httpmethod" ]; then
  1695. httpmethod="POST"
  1696. fi
  1697. _debug $httpmethod
  1698. _debug "_post_url" "$_post_url"
  1699. _debug2 "body" "$body"
  1700. _debug2 "_postContentType" "$_postContentType"
  1701. _inithttp
  1702. if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
  1703. _CURL="$_ACME_CURL"
  1704. if [ "$HTTPS_INSECURE" ]; then
  1705. _CURL="$_CURL --insecure "
  1706. fi
  1707. if [ "$httpmethod" = "HEAD" ]; then
  1708. _CURL="$_CURL -I "
  1709. fi
  1710. _debug "_CURL" "$_CURL"
  1711. if [ "$needbase64" ]; then
  1712. if [ "$body" ]; then
  1713. if [ "$_postContentType" ]; then
  1714. 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)"
  1715. else
  1716. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)"
  1717. fi
  1718. else
  1719. if [ "$_postContentType" ]; then
  1720. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url" | _base64)"
  1721. else
  1722. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url" | _base64)"
  1723. fi
  1724. fi
  1725. else
  1726. if [ "$body" ]; then
  1727. if [ "$_postContentType" ]; then
  1728. 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")"
  1729. else
  1730. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")"
  1731. fi
  1732. else
  1733. if [ "$_postContentType" ]; then
  1734. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url")"
  1735. else
  1736. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url")"
  1737. fi
  1738. fi
  1739. fi
  1740. _ret="$?"
  1741. if [ "$_ret" != "0" ]; then
  1742. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
  1743. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  1744. _err "Here is the curl dump log:"
  1745. _err "$(cat "$_CURL_DUMP")"
  1746. fi
  1747. fi
  1748. elif [ "$_ACME_WGET" ]; then
  1749. _WGET="$_ACME_WGET"
  1750. if [ "$HTTPS_INSECURE" ]; then
  1751. _WGET="$_WGET --no-check-certificate "
  1752. fi
  1753. if [ "$httpmethod" = "HEAD" ]; then
  1754. _WGET="$_WGET --read-timeout=3.0 --tries=2 "
  1755. fi
  1756. _debug "_WGET" "$_WGET"
  1757. if [ "$needbase64" ]; then
  1758. if [ "$httpmethod" = "POST" ]; then
  1759. if [ "$_postContentType" ]; then
  1760. 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)"
  1761. else
  1762. 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)"
  1763. fi
  1764. else
  1765. if [ "$_postContentType" ]; then
  1766. 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)"
  1767. else
  1768. 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)"
  1769. fi
  1770. fi
  1771. else
  1772. if [ "$httpmethod" = "POST" ]; then
  1773. if [ "$_postContentType" ]; then
  1774. 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")"
  1775. else
  1776. 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")"
  1777. fi
  1778. elif [ "$httpmethod" = "HEAD" ]; then
  1779. if [ "$_postContentType" ]; then
  1780. response="$($_WGET --spider -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")"
  1781. else
  1782. response="$($_WGET --spider -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")"
  1783. fi
  1784. else
  1785. if [ "$_postContentType" ]; then
  1786. 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")"
  1787. else
  1788. 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")"
  1789. fi
  1790. fi
  1791. fi
  1792. _ret="$?"
  1793. if [ "$_ret" = "8" ]; then
  1794. _ret=0
  1795. _debug "wget returned 8 as the server returned a 'Bad Request' response. Let's process the response later."
  1796. fi
  1797. if [ "$_ret" != "0" ]; then
  1798. _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
  1799. fi
  1800. if _contains "$_WGET" " -d "; then
  1801. # Demultiplex wget debug output
  1802. cat "$HTTP_HEADER" >&2
  1803. _sed_i '/^[^ ][^ ]/d; /^ *$/d' "$HTTP_HEADER"
  1804. fi
  1805. # remove leading whitespaces from header to match curl format
  1806. _sed_i 's/^ //g' "$HTTP_HEADER"
  1807. else
  1808. _ret="$?"
  1809. _err "Neither curl nor wget have been found, cannot make $httpmethod request."
  1810. fi
  1811. _debug "_ret" "$_ret"
  1812. printf "%s" "$response"
  1813. return $_ret
  1814. }
  1815. # url getheader timeout
  1816. _get() {
  1817. _debug GET
  1818. url="$1"
  1819. onlyheader="$2"
  1820. t="$3"
  1821. _debug url "$url"
  1822. _debug "timeout=$t"
  1823. _inithttp
  1824. if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then
  1825. _CURL="$_ACME_CURL"
  1826. if [ "$HTTPS_INSECURE" ]; then
  1827. _CURL="$_CURL --insecure "
  1828. fi
  1829. if [ "$t" ]; then
  1830. _CURL="$_CURL --connect-timeout $t"
  1831. fi
  1832. _debug "_CURL" "$_CURL"
  1833. if [ "$onlyheader" ]; then
  1834. $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
  1835. else
  1836. $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url"
  1837. fi
  1838. ret=$?
  1839. if [ "$ret" != "0" ]; then
  1840. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret"
  1841. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  1842. _err "Here is the curl dump log:"
  1843. _err "$(cat "$_CURL_DUMP")"
  1844. fi
  1845. fi
  1846. elif [ "$_ACME_WGET" ]; then
  1847. _WGET="$_ACME_WGET"
  1848. if [ "$HTTPS_INSECURE" ]; then
  1849. _WGET="$_WGET --no-check-certificate "
  1850. fi
  1851. if [ "$t" ]; then
  1852. _WGET="$_WGET --timeout=$t"
  1853. fi
  1854. _debug "_WGET" "$_WGET"
  1855. if [ "$onlyheader" ]; then
  1856. _wget_out="$($_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O /dev/null "$url" 2>&1)"
  1857. if _contains "$_WGET" " -d "; then
  1858. # Demultiplex wget debug output
  1859. echo "$_wget_out" >&2
  1860. echo "$_wget_out" | sed '/^[^ ][^ ]/d; /^ *$/d; s/^ //g' -
  1861. fi
  1862. else
  1863. $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O - "$url" 2>"$HTTP_HEADER"
  1864. if _contains "$_WGET" " -d "; then
  1865. # Demultiplex wget debug output
  1866. cat "$HTTP_HEADER" >&2
  1867. _sed_i '/^[^ ][^ ]/d; /^ *$/d' "$HTTP_HEADER"
  1868. fi
  1869. # remove leading whitespaces from header to match curl format
  1870. _sed_i 's/^ //g' "$HTTP_HEADER"
  1871. fi
  1872. ret=$?
  1873. if [ "$ret" = "8" ]; then
  1874. ret=0
  1875. _debug "wget returned 8 as the server returned a 'Bad Request' response. Let's process the response later."
  1876. fi
  1877. if [ "$ret" != "0" ]; then
  1878. _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $ret"
  1879. fi
  1880. else
  1881. ret=$?
  1882. _err "Neither curl nor wget have been found, cannot make GET request."
  1883. fi
  1884. _debug "ret" "$ret"
  1885. return $ret
  1886. }
  1887. _head_n() {
  1888. head -n "$1"
  1889. }
  1890. _tail_n() {
  1891. if _is_solaris; then
  1892. #fix for solaris
  1893. tail -"$1"
  1894. else
  1895. tail -n "$1"
  1896. fi
  1897. }
  1898. _tail_c() {
  1899. tail -c "$1" 2>/dev/null || tail -"$1"c
  1900. }
  1901. # url payload needbase64 keyfile
  1902. _send_signed_request() {
  1903. url=$1
  1904. payload=$2
  1905. needbase64=$3
  1906. keyfile=$4
  1907. if [ -z "$keyfile" ]; then
  1908. keyfile="$ACCOUNT_KEY_PATH"
  1909. fi
  1910. _debug "=======Sending Signed Request======="
  1911. _debug url "$url"
  1912. _debug payload "$payload"
  1913. if ! _calcjwk "$keyfile"; then
  1914. return 1
  1915. fi
  1916. __request_conent_type="$CONTENT_TYPE_JSON"
  1917. payload64=$(printf "%s" "$payload" | _base64 | _url_replace)
  1918. _debug3 payload64 "$payload64"
  1919. MAX_REQUEST_RETRY_TIMES=20
  1920. _sleep_retry_sec=1
  1921. _request_retry_times=0
  1922. while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
  1923. _request_retry_times=$(_math "$_request_retry_times" + 1)
  1924. _debug3 _request_retry_times "$_request_retry_times"
  1925. if [ -z "$_CACHED_NONCE" ]; then
  1926. _headers=""
  1927. if [ "$ACME_NEW_NONCE" ]; then
  1928. _debug2 "Get nonce with HEAD. ACME_NEW_NONCE" "$ACME_NEW_NONCE"
  1929. nonceurl="$ACME_NEW_NONCE"
  1930. if _post "" "$nonceurl" "" "HEAD" "$__request_conent_type" >/dev/null; then
  1931. _headers="$(cat "$HTTP_HEADER")"
  1932. _debug2 _headers "$_headers"
  1933. _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2 | cut -d , -f 1)"
  1934. fi
  1935. fi
  1936. if [ -z "$_CACHED_NONCE" ]; then
  1937. _debug2 "Get nonce with GET. ACME_DIRECTORY" "$ACME_DIRECTORY"
  1938. nonceurl="$ACME_DIRECTORY"
  1939. _headers="$(_get "$nonceurl" "onlyheader")"
  1940. _debug2 _headers "$_headers"
  1941. _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
  1942. fi
  1943. if [ -z "$_CACHED_NONCE" ] && [ "$ACME_NEW_NONCE" ]; then
  1944. _debug2 "Get nonce with GET. ACME_NEW_NONCE" "$ACME_NEW_NONCE"
  1945. nonceurl="$ACME_NEW_NONCE"
  1946. _headers="$(_get "$nonceurl" "onlyheader")"
  1947. _debug2 _headers "$_headers"
  1948. _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)"
  1949. fi
  1950. if [ "$?" != "0" ]; then
  1951. _err "Cannot connect to $nonceurl to get nonce."
  1952. return 1
  1953. fi
  1954. else
  1955. _debug2 "Use _CACHED_NONCE" "$_CACHED_NONCE"
  1956. fi
  1957. nonce="$_CACHED_NONCE"
  1958. _debug2 nonce "$nonce"
  1959. if [ -z "$nonce" ]; then
  1960. _info "Could not get nonce, let's try again."
  1961. _sleep 2
  1962. continue
  1963. fi
  1964. if [ "$url" = "$ACME_NEW_ACCOUNT" ]; then
  1965. protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
  1966. elif [ "$url" = "$ACME_REVOKE_CERT" ] && [ "$keyfile" != "$ACCOUNT_KEY_PATH" ]; then
  1967. protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}'
  1968. else
  1969. protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"kid\": \"${ACCOUNT_URL}\""'}'
  1970. fi
  1971. _debug3 protected "$protected"
  1972. protected64="$(printf "%s" "$protected" | _base64 | _url_replace)"
  1973. _debug3 protected64 "$protected64"
  1974. if ! _sig_t="$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256")"; then
  1975. _err "Sign request failed."
  1976. return 1
  1977. fi
  1978. _debug3 _sig_t "$_sig_t"
  1979. sig="$(printf "%s" "$_sig_t" | _url_replace)"
  1980. _debug3 sig "$sig"
  1981. body="{\"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  1982. _debug3 body "$body"
  1983. response="$(_post "$body" "$url" "$needbase64" "POST" "$__request_conent_type")"
  1984. _CACHED_NONCE=""
  1985. if [ "$?" != "0" ]; then
  1986. _err "Cannot make POST request to $url"
  1987. return 1
  1988. fi
  1989. responseHeaders="$(cat "$HTTP_HEADER")"
  1990. _debug2 responseHeaders "$responseHeaders"
  1991. code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")"
  1992. _debug code "$code"
  1993. _debug2 original "$response"
  1994. if echo "$responseHeaders" | grep -i "Content-Type: *application/json" >/dev/null 2>&1; then
  1995. response="$(echo "$response" | _json_decode | _normalizeJson)"
  1996. fi
  1997. _debug2 response "$response"
  1998. _CACHED_NONCE="$(echo "$responseHeaders" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2 | cut -d , -f 1)"
  1999. if ! _startswith "$code" "2"; then
  2000. _body="$response"
  2001. if [ "$needbase64" ]; then
  2002. _body="$(echo "$_body" | _dbase64 multiline)"
  2003. _debug3 _body "$_body"
  2004. fi
  2005. _retryafter=$(echo "$responseHeaders" | grep -i "^Retry-After *: *[0-9]\+ *" | cut -d : -f 2 | tr -d ' ' | tr -d '\r')
  2006. if [ "$code" = '503' ]; then
  2007. _sleep_overload_retry_sec=$_retryafter
  2008. if [ -z "$_sleep_overload_retry_sec" ]; then
  2009. _sleep_overload_retry_sec=5
  2010. fi
  2011. if [ $_sleep_overload_retry_sec -le 600 ]; then
  2012. _info "It seems the CA server is currently overloaded, let's wait and retry. Sleeping for $_sleep_overload_retry_sec seconds."
  2013. _sleep $_sleep_overload_retry_sec
  2014. continue
  2015. else
  2016. _info "The retryafter=$_retryafter value is too large (> 600), will not retry anymore."
  2017. fi
  2018. fi
  2019. if _contains "$_body" "JWS has invalid anti-replay nonce" || _contains "$_body" "JWS has an invalid anti-replay nonce"; then
  2020. _info "It seems the CA server is busy now, let's wait and retry. Sleeping for $_sleep_retry_sec seconds."
  2021. _CACHED_NONCE=""
  2022. _sleep $_sleep_retry_sec
  2023. continue
  2024. fi
  2025. if _contains "$_body" "The Replay Nonce is not recognized"; then
  2026. _info "The replay nonce is not valid, let's get a new one. Sleeping for $_sleep_retry_sec seconds."
  2027. _CACHED_NONCE=""
  2028. _sleep $_sleep_retry_sec
  2029. continue
  2030. fi
  2031. fi
  2032. return 0
  2033. done
  2034. _info "Giving up sending to CA server after $MAX_REQUEST_RETRY_TIMES retries."
  2035. return 1
  2036. }
  2037. #setopt "file" "opt" "=" "value" [";"]
  2038. _setopt() {
  2039. __conf="$1"
  2040. __opt="$2"
  2041. __sep="$3"
  2042. __val="$4"
  2043. __end="$5"
  2044. if [ -z "$__opt" ]; then
  2045. _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
  2046. return
  2047. fi
  2048. if [ ! -f "$__conf" ]; then
  2049. touch "$__conf"
  2050. fi
  2051. if [ -n "$(_tail_c 1 <"$__conf")" ]; then
  2052. echo >>"$__conf"
  2053. fi
  2054. if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then
  2055. _debug3 OK
  2056. if _contains "$__val" "&"; then
  2057. __val="$(echo "$__val" | sed 's/&/\\&/g')"
  2058. fi
  2059. if _contains "$__val" "|"; then
  2060. __val="$(echo "$__val" | sed 's/|/\\|/g')"
  2061. fi
  2062. text="$(cat "$__conf")"
  2063. printf -- "%s\n" "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
  2064. elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then
  2065. if _contains "$__val" "&"; then
  2066. __val="$(echo "$__val" | sed 's/&/\\&/g')"
  2067. fi
  2068. if _contains "$__val" "|"; then
  2069. __val="$(echo "$__val" | sed 's/|/\\|/g')"
  2070. fi
  2071. text="$(cat "$__conf")"
  2072. printf -- "%s\n" "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf"
  2073. else
  2074. _debug3 APP
  2075. echo "$__opt$__sep$__val$__end" >>"$__conf"
  2076. fi
  2077. _debug3 "$(grep -n "^$__opt$__sep" "$__conf")"
  2078. }
  2079. #_save_conf file key value base64encode
  2080. #save to conf
  2081. _save_conf() {
  2082. _s_c_f="$1"
  2083. _sdkey="$2"
  2084. _sdvalue="$3"
  2085. _b64encode="$4"
  2086. if [ "$_sdvalue" ] && [ "$_b64encode" ]; then
  2087. _sdvalue="${B64CONF_START}$(printf "%s" "${_sdvalue}" | _base64)${B64CONF_END}"
  2088. fi
  2089. if [ "$_s_c_f" ]; then
  2090. _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'"
  2091. else
  2092. _err "Config file is empty, cannot save $_sdkey=$_sdvalue"
  2093. fi
  2094. }
  2095. #_clear_conf file key
  2096. _clear_conf() {
  2097. _c_c_f="$1"
  2098. _sdkey="$2"
  2099. if [ "$_c_c_f" ]; then
  2100. _conf_data="$(cat "$_c_c_f")"
  2101. echo "$_conf_data" | sed "/^$_sdkey *=.*$/d" >"$_c_c_f"
  2102. else
  2103. _err "Config file is empty, cannot clear"
  2104. fi
  2105. }
  2106. #_read_conf file key
  2107. _read_conf() {
  2108. _r_c_f="$1"
  2109. _sdkey="$2"
  2110. if [ -f "$_r_c_f" ]; then
  2111. _sdv="$(
  2112. eval "$(grep "^$_sdkey *=" "$_r_c_f")"
  2113. eval "printf \"%s\" \"\$$_sdkey\""
  2114. )"
  2115. if _startswith "$_sdv" "${B64CONF_START}" && _endswith "$_sdv" "${B64CONF_END}"; then
  2116. _sdv="$(echo "$_sdv" | sed "s/${B64CONF_START}//" | sed "s/${B64CONF_END}//" | _dbase64)"
  2117. fi
  2118. printf "%s" "$_sdv"
  2119. else
  2120. _debug "Config file is empty, cannot read $_sdkey"
  2121. fi
  2122. }
  2123. #_savedomainconf key value base64encode
  2124. #save to domain.conf
  2125. _savedomainconf() {
  2126. _save_conf "$DOMAIN_CONF" "$@"
  2127. }
  2128. #_cleardomainconf key
  2129. _cleardomainconf() {
  2130. _clear_conf "$DOMAIN_CONF" "$1"
  2131. }
  2132. #_readdomainconf key
  2133. _readdomainconf() {
  2134. _read_conf "$DOMAIN_CONF" "$1"
  2135. }
  2136. #_migratedomainconf oldkey newkey base64encode
  2137. _migratedomainconf() {
  2138. _old_key="$1"
  2139. _new_key="$2"
  2140. _b64encode="$3"
  2141. _old_value=$(_readdomainconf "$_old_key")
  2142. _cleardomainconf "$_old_key"
  2143. if [ -z "$_old_value" ]; then
  2144. return 1 # migrated failed: old value is empty
  2145. fi
  2146. _new_value=$(_readdomainconf "$_new_key")
  2147. if [ -n "$_new_value" ]; then
  2148. _debug "Domain config new key exists, old key $_old_key='$_old_value' has been removed."
  2149. return 1 # migrated failed: old value replaced by new value
  2150. fi
  2151. _savedomainconf "$_new_key" "$_old_value" "$_b64encode"
  2152. _debug "Domain config $_old_key has been migrated to $_new_key."
  2153. }
  2154. #_migratedeployconf oldkey newkey base64encode
  2155. _migratedeployconf() {
  2156. _migratedomainconf "$1" "SAVED_$2" "$3" ||
  2157. _migratedomainconf "SAVED_$1" "SAVED_$2" "$3" # try only when oldkey itself is not found
  2158. }
  2159. #key value base64encode
  2160. _savedeployconf() {
  2161. _savedomainconf "SAVED_$1" "$2" "$3"
  2162. #remove later
  2163. _cleardomainconf "$1"
  2164. }
  2165. #key
  2166. _getdeployconf() {
  2167. _rac_key="$1"
  2168. _rac_value="$(eval echo \$"$_rac_key")"
  2169. if [ "$_rac_value" ]; then
  2170. if _startswith "$_rac_value" '"' && _endswith "$_rac_value" '"'; then
  2171. _debug2 "trim quotation marks"
  2172. eval $_rac_key=$_rac_value
  2173. export $_rac_key
  2174. fi
  2175. return 0 # do nothing
  2176. fi
  2177. _saved="$(_readdomainconf "SAVED_$_rac_key")"
  2178. eval $_rac_key=\$_saved
  2179. export $_rac_key
  2180. }
  2181. #_saveaccountconf key value base64encode
  2182. _saveaccountconf() {
  2183. _save_conf "$ACCOUNT_CONF_PATH" "$@"
  2184. }
  2185. #key value base64encode
  2186. _saveaccountconf_mutable() {
  2187. _save_conf "$ACCOUNT_CONF_PATH" "SAVED_$1" "$2" "$3"
  2188. #remove later
  2189. _clearaccountconf "$1"
  2190. }
  2191. #key
  2192. _readaccountconf() {
  2193. _read_conf "$ACCOUNT_CONF_PATH" "$1"
  2194. }
  2195. #key
  2196. _readaccountconf_mutable() {
  2197. _rac_key="$1"
  2198. _readaccountconf "SAVED_$_rac_key"
  2199. }
  2200. #_clearaccountconf key
  2201. _clearaccountconf() {
  2202. _clear_conf "$ACCOUNT_CONF_PATH" "$1"
  2203. }
  2204. #key
  2205. _clearaccountconf_mutable() {
  2206. _clearaccountconf "SAVED_$1"
  2207. #remove later
  2208. _clearaccountconf "$1"
  2209. }
  2210. #_savecaconf key value
  2211. _savecaconf() {
  2212. _save_conf "$CA_CONF" "$1" "$2"
  2213. }
  2214. #_readcaconf key
  2215. _readcaconf() {
  2216. _read_conf "$CA_CONF" "$1"
  2217. }
  2218. #_clearaccountconf key
  2219. _clearcaconf() {
  2220. _clear_conf "$CA_CONF" "$1"
  2221. }
  2222. # content localaddress
  2223. _startserver() {
  2224. content="$1"
  2225. ncaddr="$2"
  2226. _debug "content" "$content"
  2227. _debug "ncaddr" "$ncaddr"
  2228. _debug "startserver: $$"
  2229. _debug Le_HTTPPort "$Le_HTTPPort"
  2230. _debug Le_Listen_V4 "$Le_Listen_V4"
  2231. _debug Le_Listen_V6 "$Le_Listen_V6"
  2232. _NC="socat"
  2233. if [ "$Le_Listen_V6" ]; then
  2234. _NC="$_NC -6"
  2235. SOCAT_OPTIONS=TCP6-LISTEN
  2236. elif [ "$Le_Listen_V4" ]; then
  2237. _NC="$_NC -4"
  2238. SOCAT_OPTIONS=TCP4-LISTEN
  2239. else
  2240. SOCAT_OPTIONS=TCP-LISTEN
  2241. fi
  2242. if [ "$DEBUG" ] && [ "$DEBUG" -gt "1" ]; then
  2243. _NC="$_NC -d -d -v"
  2244. fi
  2245. SOCAT_OPTIONS=$SOCAT_OPTIONS:$Le_HTTPPort,crlf,reuseaddr,fork
  2246. #Adding bind to local-address
  2247. if [ "$ncaddr" ]; then
  2248. SOCAT_OPTIONS="$SOCAT_OPTIONS,bind=${ncaddr}"
  2249. fi
  2250. _content_len="$(printf "%s" "$content" | wc -c)"
  2251. _debug _content_len "$_content_len"
  2252. _debug "_NC" "$_NC $SOCAT_OPTIONS"
  2253. export _SOCAT_ERR="$(_mktemp)"
  2254. $_NC $SOCAT_OPTIONS SYSTEM:"sleep 1; \
  2255. echo 'HTTP/1.0 200 OK'; \
  2256. echo 'Content-Length\: $_content_len'; \
  2257. echo ''; \
  2258. printf '%s' '$content';" 2>"$_SOCAT_ERR" &
  2259. serverproc="$!"
  2260. if [ -f "$_SOCAT_ERR" ]; then
  2261. if grep "Permission denied" "$_SOCAT_ERR" >/dev/null; then
  2262. _err "socat: $(cat $_SOCAT_ERR)"
  2263. _err "Can not listen for user: $(whoami)"
  2264. _err "Maybe try with root again?"
  2265. rm -f "$_SOCAT_ERR"
  2266. return 1
  2267. fi
  2268. fi
  2269. }
  2270. _stopserver() {
  2271. pid="$1"
  2272. _debug "pid" "$pid"
  2273. if [ -z "$pid" ]; then
  2274. rm -f "$_SOCAT_ERR"
  2275. return
  2276. fi
  2277. kill $pid
  2278. rm -f "$_SOCAT_ERR"
  2279. }
  2280. # sleep sec
  2281. _sleep() {
  2282. _sleep_sec="$1"
  2283. if [ "$__INTERACTIVE" ]; then
  2284. _sleep_c="$_sleep_sec"
  2285. while [ "$_sleep_c" -ge "0" ]; do
  2286. printf "\r \r"
  2287. __green "$_sleep_c"
  2288. _sleep_c="$(_math "$_sleep_c" - 1)"
  2289. sleep 1
  2290. done
  2291. printf "\r"
  2292. else
  2293. sleep "$_sleep_sec"
  2294. fi
  2295. }
  2296. # _starttlsserver san_a san_b port content _ncaddr acmeValidationv1
  2297. _starttlsserver() {
  2298. _info "Starting tls server."
  2299. san_a="$1"
  2300. san_b="$2"
  2301. port="$3"
  2302. content="$4"
  2303. opaddr="$5"
  2304. acmeValidationv1="$6"
  2305. _debug san_a "$san_a"
  2306. _debug san_b "$san_b"
  2307. _debug port "$port"
  2308. _debug acmeValidationv1 "$acmeValidationv1"
  2309. #create key TLS_KEY
  2310. if ! _createkey "2048" "$TLS_KEY"; then
  2311. _err "Error creating TLS validation key."
  2312. return 1
  2313. fi
  2314. #create csr
  2315. alt="$san_a"
  2316. if [ "$san_b" ]; then
  2317. alt="$alt,$san_b"
  2318. fi
  2319. if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$acmeValidationv1"; then
  2320. _err "Error creating TLS validation CSR."
  2321. return 1
  2322. fi
  2323. #self signed
  2324. if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then
  2325. _err "Error creating TLS validation cert."
  2326. return 1
  2327. fi
  2328. __S_OPENSSL="${ACME_OPENSSL_BIN:-openssl} s_server -www -cert $TLS_CERT -key $TLS_KEY "
  2329. if [ "$opaddr" ]; then
  2330. __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port"
  2331. else
  2332. __S_OPENSSL="$__S_OPENSSL -accept $port"
  2333. fi
  2334. _debug Le_Listen_V4 "$Le_Listen_V4"
  2335. _debug Le_Listen_V6 "$Le_Listen_V6"
  2336. if [ "$Le_Listen_V4" ]; then
  2337. __S_OPENSSL="$__S_OPENSSL -4"
  2338. elif [ "$Le_Listen_V6" ]; then
  2339. __S_OPENSSL="$__S_OPENSSL -6"
  2340. fi
  2341. if [ "$acmeValidationv1" ]; then
  2342. __S_OPENSSL="$__S_OPENSSL -alpn acme-tls/1"
  2343. fi
  2344. _debug "$__S_OPENSSL"
  2345. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then
  2346. $__S_OPENSSL -tlsextdebug &
  2347. else
  2348. $__S_OPENSSL >/dev/null 2>&1 &
  2349. fi
  2350. serverproc="$!"
  2351. sleep 1
  2352. _debug serverproc "$serverproc"
  2353. }
  2354. #file
  2355. _readlink() {
  2356. _rf="$1"
  2357. if ! readlink -f "$_rf" 2>/dev/null; then
  2358. if _startswith "$_rf" "/"; then
  2359. echo "$_rf"
  2360. return 0
  2361. fi
  2362. echo "$(pwd)/$_rf" | _conapath
  2363. fi
  2364. }
  2365. _conapath() {
  2366. sed "s#/\./#/#g"
  2367. }
  2368. __initHome() {
  2369. if [ -z "$_SCRIPT_HOME" ]; then
  2370. if _exists readlink && _exists dirname; then
  2371. _debug "Let's find the script directory."
  2372. _debug "_SCRIPT_" "$_SCRIPT_"
  2373. _script="$(_readlink "$_SCRIPT_")"
  2374. _debug "_script" "$_script"
  2375. _script_home="$(dirname "$_script")"
  2376. _debug "_script_home" "$_script_home"
  2377. if [ -d "$_script_home" ]; then
  2378. export _SCRIPT_HOME="$_script_home"
  2379. else
  2380. _err "It seems the script home is not correct: $_script_home"
  2381. fi
  2382. fi
  2383. fi
  2384. # if [ -z "$LE_WORKING_DIR" ]; then
  2385. # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then
  2386. # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME"
  2387. # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
  2388. # else
  2389. # LE_WORKING_DIR="$_SCRIPT_HOME"
  2390. # fi
  2391. # fi
  2392. if [ -z "$LE_WORKING_DIR" ]; then
  2393. _debug "Using default home: $DEFAULT_INSTALL_HOME"
  2394. LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
  2395. fi
  2396. export LE_WORKING_DIR
  2397. if [ -z "$LE_CONFIG_HOME" ]; then
  2398. LE_CONFIG_HOME="$LE_WORKING_DIR"
  2399. fi
  2400. _debug "Using config home: $LE_CONFIG_HOME"
  2401. export LE_CONFIG_HOME
  2402. _DEFAULT_ACCOUNT_CONF_PATH="$LE_CONFIG_HOME/account.conf"
  2403. if [ -z "$ACCOUNT_CONF_PATH" ]; then
  2404. if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then
  2405. . "$_DEFAULT_ACCOUNT_CONF_PATH"
  2406. fi
  2407. fi
  2408. if [ -z "$ACCOUNT_CONF_PATH" ]; then
  2409. ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
  2410. fi
  2411. _debug3 ACCOUNT_CONF_PATH "$ACCOUNT_CONF_PATH"
  2412. DEFAULT_LOG_FILE="$LE_CONFIG_HOME/$PROJECT_NAME.log"
  2413. DEFAULT_CA_HOME="$LE_CONFIG_HOME/ca"
  2414. if [ -z "$LE_TEMP_DIR" ]; then
  2415. LE_TEMP_DIR="$LE_CONFIG_HOME/tmp"
  2416. fi
  2417. }
  2418. _clearAPI() {
  2419. ACME_NEW_ACCOUNT=""
  2420. ACME_KEY_CHANGE=""
  2421. ACME_NEW_AUTHZ=""
  2422. ACME_NEW_ORDER=""
  2423. ACME_REVOKE_CERT=""
  2424. ACME_NEW_NONCE=""
  2425. ACME_AGREEMENT=""
  2426. }
  2427. #server
  2428. _initAPI() {
  2429. _api_server="${1:-$ACME_DIRECTORY}"
  2430. _debug "_init API for server: $_api_server"
  2431. MAX_API_RETRY_TIMES=10
  2432. _sleep_retry_sec=10
  2433. _request_retry_times=0
  2434. while [ -z "$ACME_NEW_ACCOUNT" ] && [ "${_request_retry_times}" -lt "$MAX_API_RETRY_TIMES" ]; do
  2435. _request_retry_times=$(_math "$_request_retry_times" + 1)
  2436. response=$(_get "$_api_server" "" 10)
  2437. if [ "$?" != "0" ]; then
  2438. _debug2 "response" "$response"
  2439. _info "Cannot init API for: $_api_server."
  2440. _info "Sleeping for $_sleep_retry_sec seconds and retrying."
  2441. _sleep "$_sleep_retry_sec"
  2442. continue
  2443. fi
  2444. response=$(echo "$response" | _json_decode)
  2445. _debug2 "response" "$response"
  2446. ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'keyChange" *: *"[^"]*"' | cut -d '"' -f 3)
  2447. export ACME_KEY_CHANGE
  2448. ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'newAuthz" *: *"[^"]*"' | cut -d '"' -f 3)
  2449. export ACME_NEW_AUTHZ
  2450. ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'newOrder" *: *"[^"]*"' | cut -d '"' -f 3)
  2451. export ACME_NEW_ORDER
  2452. ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'newAccount" *: *"[^"]*"' | cut -d '"' -f 3)
  2453. export ACME_NEW_ACCOUNT
  2454. ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revokeCert" *: *"[^"]*"' | cut -d '"' -f 3)
  2455. export ACME_REVOKE_CERT
  2456. ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'newNonce" *: *"[^"]*"' | cut -d '"' -f 3)
  2457. export ACME_NEW_NONCE
  2458. ACME_AGREEMENT=$(echo "$response" | _egrep_o 'termsOfService" *: *"[^"]*"' | cut -d '"' -f 3)
  2459. export ACME_AGREEMENT
  2460. _debug "ACME_KEY_CHANGE" "$ACME_KEY_CHANGE"
  2461. _debug "ACME_NEW_AUTHZ" "$ACME_NEW_AUTHZ"
  2462. _debug "ACME_NEW_ORDER" "$ACME_NEW_ORDER"
  2463. _debug "ACME_NEW_ACCOUNT" "$ACME_NEW_ACCOUNT"
  2464. _debug "ACME_REVOKE_CERT" "$ACME_REVOKE_CERT"
  2465. _debug "ACME_AGREEMENT" "$ACME_AGREEMENT"
  2466. _debug "ACME_NEW_NONCE" "$ACME_NEW_NONCE"
  2467. if [ "$ACME_NEW_ACCOUNT" ] && [ "$ACME_NEW_ORDER" ]; then
  2468. return 0
  2469. fi
  2470. _info "Sleeping for $_sleep_retry_sec seconds and retrying."
  2471. _sleep "$_sleep_retry_sec"
  2472. done
  2473. if [ "$ACME_NEW_ACCOUNT" ] && [ "$ACME_NEW_ORDER" ]; then
  2474. return 0
  2475. fi
  2476. _err "Cannot init API for $_api_server"
  2477. return 1
  2478. }
  2479. _clearCA() {
  2480. export CA_CONF=
  2481. export ACCOUNT_KEY_PATH=
  2482. export ACCOUNT_JSON_PATH=
  2483. }
  2484. #[domain] [keylength or isEcc flag]
  2485. _initpath() {
  2486. domain="$1"
  2487. _ilength="$2"
  2488. __initHome
  2489. if [ -f "$ACCOUNT_CONF_PATH" ]; then
  2490. . "$ACCOUNT_CONF_PATH"
  2491. fi
  2492. if [ "$_ACME_IN_CRON" ]; then
  2493. if [ ! "$_USER_PATH_EXPORTED" ]; then
  2494. _USER_PATH_EXPORTED=1
  2495. export PATH="$USER_PATH:$PATH"
  2496. fi
  2497. fi
  2498. if [ -z "$CA_HOME" ]; then
  2499. CA_HOME="$DEFAULT_CA_HOME"
  2500. fi
  2501. if [ -z "$ACME_DIRECTORY" ]; then
  2502. if [ "$STAGE" ]; then
  2503. ACME_DIRECTORY="$DEFAULT_STAGING_CA"
  2504. _info "Using ACME_DIRECTORY: $ACME_DIRECTORY"
  2505. else
  2506. default_acme_server=$(_readaccountconf "DEFAULT_ACME_SERVER")
  2507. _debug default_acme_server "$default_acme_server"
  2508. if [ "$default_acme_server" ]; then
  2509. ACME_DIRECTORY="$default_acme_server"
  2510. else
  2511. ACME_DIRECTORY="$DEFAULT_CA"
  2512. fi
  2513. fi
  2514. fi
  2515. _debug ACME_DIRECTORY "$ACME_DIRECTORY"
  2516. _ACME_SERVER_HOST="$(echo "$ACME_DIRECTORY" | cut -d : -f 2 | tr -s / | cut -d / -f 2)"
  2517. _debug2 "_ACME_SERVER_HOST" "$_ACME_SERVER_HOST"
  2518. _ACME_SERVER_PATH="$(echo "$ACME_DIRECTORY" | cut -d : -f 2- | tr -s / | cut -d / -f 3-)"
  2519. _debug2 "_ACME_SERVER_PATH" "$_ACME_SERVER_PATH"
  2520. CA_DIR="$CA_HOME/$_ACME_SERVER_HOST/$_ACME_SERVER_PATH"
  2521. _DEFAULT_CA_CONF="$CA_DIR/ca.conf"
  2522. if [ -z "$CA_CONF" ]; then
  2523. CA_CONF="$_DEFAULT_CA_CONF"
  2524. fi
  2525. _debug3 CA_CONF "$CA_CONF"
  2526. _OLD_CADIR="$CA_HOME/$_ACME_SERVER_HOST"
  2527. _OLD_ACCOUNT_KEY="$_OLD_CADIR/account.key"
  2528. _OLD_ACCOUNT_JSON="$_OLD_CADIR/account.json"
  2529. _OLD_CA_CONF="$_OLD_CADIR/ca.conf"
  2530. _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key"
  2531. _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json"
  2532. if [ -z "$ACCOUNT_KEY_PATH" ]; then
  2533. ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
  2534. if [ -f "$_OLD_ACCOUNT_KEY" ] && ! [ -f "$ACCOUNT_KEY_PATH" ]; then
  2535. mkdir -p "$CA_DIR"
  2536. mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
  2537. fi
  2538. fi
  2539. if [ -z "$ACCOUNT_JSON_PATH" ]; then
  2540. ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH"
  2541. if [ -f "$_OLD_ACCOUNT_JSON" ] && ! [ -f "$ACCOUNT_JSON_PATH" ]; then
  2542. mkdir -p "$CA_DIR"
  2543. mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
  2544. fi
  2545. fi
  2546. if [ -f "$_OLD_CA_CONF" ] && ! [ -f "$CA_CONF" ]; then
  2547. mkdir -p "$CA_DIR"
  2548. mv "$_OLD_CA_CONF" "$CA_CONF"
  2549. fi
  2550. if [ -f "$CA_CONF" ]; then
  2551. . "$CA_CONF"
  2552. fi
  2553. if [ -z "$ACME_DIR" ]; then
  2554. ACME_DIR="/home/.acme"
  2555. fi
  2556. if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then
  2557. APACHE_CONF_BACKUP_DIR="$LE_CONFIG_HOME"
  2558. fi
  2559. if [ -z "$USER_AGENT" ]; then
  2560. USER_AGENT="$DEFAULT_USER_AGENT"
  2561. fi
  2562. if [ -z "$HTTP_HEADER" ]; then
  2563. HTTP_HEADER="$LE_CONFIG_HOME/http.header"
  2564. fi
  2565. _DEFAULT_CERT_HOME="$LE_CONFIG_HOME"
  2566. if [ -z "$CERT_HOME" ]; then
  2567. CERT_HOME="$_DEFAULT_CERT_HOME"
  2568. fi
  2569. if [ -z "$ACME_OPENSSL_BIN" ] || [ ! -f "$ACME_OPENSSL_BIN" ] || [ ! -x "$ACME_OPENSSL_BIN" ]; then
  2570. ACME_OPENSSL_BIN="$DEFAULT_OPENSSL_BIN"
  2571. fi
  2572. if [ -z "$domain" ]; then
  2573. return 0
  2574. fi
  2575. if [ -z "$DOMAIN_PATH" ]; then
  2576. domainhome="$CERT_HOME/$domain"
  2577. domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX"
  2578. DOMAIN_PATH="$domainhome"
  2579. if _isEccKey "$_ilength"; then
  2580. DOMAIN_PATH="$domainhomeecc"
  2581. elif [ -z "$__SELECTED_RSA_KEY" ]; then
  2582. if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then
  2583. _info "The domain '$domain' seems to already have an ECC cert, let's use it."
  2584. DOMAIN_PATH="$domainhomeecc"
  2585. fi
  2586. fi
  2587. _debug DOMAIN_PATH "$DOMAIN_PATH"
  2588. export DOMAIN_PATH
  2589. fi
  2590. if [ -z "$DOMAIN_BACKUP_PATH" ]; then
  2591. DOMAIN_BACKUP_PATH="$DOMAIN_PATH/backup"
  2592. fi
  2593. if [ -z "$DOMAIN_CONF" ]; then
  2594. DOMAIN_CONF="$DOMAIN_PATH/$domain.conf"
  2595. fi
  2596. if [ -z "$DOMAIN_SSL_CONF" ]; then
  2597. DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf"
  2598. fi
  2599. if [ -z "$CSR_PATH" ]; then
  2600. CSR_PATH="$DOMAIN_PATH/$domain.csr"
  2601. fi
  2602. if [ -z "$CERT_KEY_PATH" ]; then
  2603. CERT_KEY_PATH="$DOMAIN_PATH/$domain.key"
  2604. fi
  2605. if [ -z "$CERT_PATH" ]; then
  2606. CERT_PATH="$DOMAIN_PATH/$domain.cer"
  2607. fi
  2608. if [ -z "$CA_CERT_PATH" ]; then
  2609. CA_CERT_PATH="$DOMAIN_PATH/ca.cer"
  2610. fi
  2611. if [ -z "$CERT_FULLCHAIN_PATH" ]; then
  2612. CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer"
  2613. fi
  2614. if [ -z "$CERT_PFX_PATH" ]; then
  2615. CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx"
  2616. fi
  2617. if [ -z "$CERT_PKCS8_PATH" ]; then
  2618. CERT_PKCS8_PATH="$DOMAIN_PATH/$domain.pkcs8"
  2619. fi
  2620. if [ -z "$TLS_CONF" ]; then
  2621. TLS_CONF="$DOMAIN_PATH/tls.validation.conf"
  2622. fi
  2623. if [ -z "$TLS_CERT" ]; then
  2624. TLS_CERT="$DOMAIN_PATH/tls.validation.cert"
  2625. fi
  2626. if [ -z "$TLS_KEY" ]; then
  2627. TLS_KEY="$DOMAIN_PATH/tls.validation.key"
  2628. fi
  2629. if [ -z "$TLS_CSR" ]; then
  2630. TLS_CSR="$DOMAIN_PATH/tls.validation.csr"
  2631. fi
  2632. }
  2633. _apachePath() {
  2634. _APACHECTL="apachectl"
  2635. if ! _exists apachectl; then
  2636. if _exists apache2ctl; then
  2637. _APACHECTL="apache2ctl"
  2638. else
  2639. _err "'apachectl not found. It seems that Apache is not installed or you are not root.'"
  2640. _err "Please use webroot mode to try again."
  2641. return 1
  2642. fi
  2643. fi
  2644. if ! $_APACHECTL -V >/dev/null; then
  2645. return 1
  2646. fi
  2647. if [ "$APACHE_HTTPD_CONF" ]; then
  2648. _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF"
  2649. httpdconf="$APACHE_HTTPD_CONF"
  2650. httpdconfname="$(basename "$httpdconfname")"
  2651. else
  2652. httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')"
  2653. _debug httpdconfname "$httpdconfname"
  2654. if [ -z "$httpdconfname" ]; then
  2655. _err "Cannot read Apache config file."
  2656. return 1
  2657. fi
  2658. if _startswith "$httpdconfname" '/'; then
  2659. httpdconf="$httpdconfname"
  2660. httpdconfname="$(basename "$httpdconfname")"
  2661. else
  2662. httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')"
  2663. _debug httpdroot "$httpdroot"
  2664. httpdconf="$httpdroot/$httpdconfname"
  2665. httpdconfname="$(basename "$httpdconfname")"
  2666. fi
  2667. fi
  2668. _debug httpdconf "$httpdconf"
  2669. _debug httpdconfname "$httpdconfname"
  2670. if [ ! -f "$httpdconf" ]; then
  2671. _err "Apache config file not found" "$httpdconf"
  2672. return 1
  2673. fi
  2674. return 0
  2675. }
  2676. _restoreApache() {
  2677. if [ -z "$usingApache" ]; then
  2678. return 0
  2679. fi
  2680. _initpath
  2681. if ! _apachePath; then
  2682. return 1
  2683. fi
  2684. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then
  2685. _debug "No config file to restore."
  2686. return 0
  2687. fi
  2688. cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf"
  2689. _debug "Restored: $httpdconf."
  2690. if ! $_APACHECTL -t; then
  2691. _err "Sorry, there's been an error restoring the Apache config. Please ask for support on $PROJECT."
  2692. return 1
  2693. fi
  2694. _debug "Restored successfully."
  2695. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  2696. return 0
  2697. }
  2698. _setApache() {
  2699. _initpath
  2700. if ! _apachePath; then
  2701. return 1
  2702. fi
  2703. #test the conf first
  2704. _info "Checking if there is an error in the Apache config file before starting."
  2705. if ! $_APACHECTL -t >/dev/null; then
  2706. _err "The Apache config file has errors, please fix them first then try again."
  2707. _err "Don't worry, no changes to your system have been made."
  2708. return 1
  2709. else
  2710. _info "OK"
  2711. fi
  2712. #backup the conf
  2713. _debug "Backing up Apache config file" "$httpdconf"
  2714. if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then
  2715. _err "Cannot backup Apache config file, aborting. Don't worry, the Apache config has not been changed."
  2716. _err "This might be an $PROJECT_NAME bug, please open an issue on $PROJECT"
  2717. return 1
  2718. fi
  2719. _info "Config file $httpdconf has been backed up to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  2720. _info "In case an error causes it to not be restored automatically, you can restore it yourself."
  2721. _info "You do not need to do anything on success, as the backup file will automatically be deleted."
  2722. #add alias
  2723. apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)"
  2724. _debug "apacheVer" "$apacheVer"
  2725. apacheMajor="$(echo "$apacheVer" | cut -d . -f 1)"
  2726. apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
  2727. if [ "$apacheVer" ] && [ "$apacheMajor$apacheMinor" -ge "24" ]; then
  2728. echo "
  2729. Alias /.well-known/acme-challenge $ACME_DIR
  2730. <Directory $ACME_DIR >
  2731. Require all granted
  2732. </Directory>
  2733. " >>"$httpdconf"
  2734. else
  2735. echo "
  2736. Alias /.well-known/acme-challenge $ACME_DIR
  2737. <Directory $ACME_DIR >
  2738. Order allow,deny
  2739. Allow from all
  2740. </Directory>
  2741. " >>"$httpdconf"
  2742. fi
  2743. _msg="$($_APACHECTL -t 2>&1)"
  2744. if [ "$?" != "0" ]; then
  2745. _err "Sorry, an Apache config error has occurred"
  2746. if _restoreApache; then
  2747. _err "The Apache config file has been restored."
  2748. else
  2749. _err "Sorry, the Apache config file cannot be restored, please open an issue on $PROJECT."
  2750. fi
  2751. return 1
  2752. fi
  2753. if [ ! -d "$ACME_DIR" ]; then
  2754. mkdir -p "$ACME_DIR"
  2755. chmod 755 "$ACME_DIR"
  2756. fi
  2757. if ! $_APACHECTL graceful; then
  2758. _err "$_APACHECTL graceful error, please open an issue on $PROJECT."
  2759. _restoreApache
  2760. return 1
  2761. fi
  2762. usingApache="1"
  2763. return 0
  2764. }
  2765. #find the real nginx conf file
  2766. #backup
  2767. #set the nginx conf
  2768. #returns the real nginx conf file
  2769. _setNginx() {
  2770. _d="$1"
  2771. _croot="$2"
  2772. _thumbpt="$3"
  2773. FOUND_REAL_NGINX_CONF=""
  2774. FOUND_REAL_NGINX_CONF_LN=""
  2775. BACKUP_NGINX_CONF=""
  2776. _debug _croot "$_croot"
  2777. _start_f="$(echo "$_croot" | cut -d : -f 2)"
  2778. _debug _start_f "$_start_f"
  2779. if [ -z "$_start_f" ]; then
  2780. _debug "Finding config using the nginx command"
  2781. if [ -z "$NGINX_CONF" ]; then
  2782. if ! _exists "nginx"; then
  2783. _err "nginx command not found."
  2784. return 1
  2785. fi
  2786. NGINX_CONF="$(nginx -V 2>&1 | _egrep_o "\-\-conf-path=[^ ]* " | tr -d " ")"
  2787. _debug NGINX_CONF "$NGINX_CONF"
  2788. NGINX_CONF="$(echo "$NGINX_CONF" | cut -d = -f 2)"
  2789. _debug NGINX_CONF "$NGINX_CONF"
  2790. if [ -z "$NGINX_CONF" ]; then
  2791. _err "Cannot find nginx config."
  2792. NGINX_CONF=""
  2793. return 1
  2794. fi
  2795. if [ ! -f "$NGINX_CONF" ]; then
  2796. _err "'$NGINX_CONF' doesn't exist."
  2797. NGINX_CONF=""
  2798. return 1
  2799. fi
  2800. _debug "Found nginx config file: $NGINX_CONF"
  2801. fi
  2802. _start_f="$NGINX_CONF"
  2803. fi
  2804. _debug "Detecting nginx conf for $_d from: $_start_f"
  2805. if ! _checkConf "$_d" "$_start_f"; then
  2806. _err "Cannot find config file for domain $d"
  2807. return 1
  2808. fi
  2809. _info "Found config file: $FOUND_REAL_NGINX_CONF"
  2810. _ln=$FOUND_REAL_NGINX_CONF_LN
  2811. _debug "_ln" "$_ln"
  2812. _lnn=$(_math $_ln + 1)
  2813. _debug _lnn "$_lnn"
  2814. _start_tag="$(sed -n "$_lnn,${_lnn}p" "$FOUND_REAL_NGINX_CONF")"
  2815. _debug "_start_tag" "$_start_tag"
  2816. if [ "$_start_tag" = "$NGINX_START" ]; then
  2817. _info "The domain $_d is already configured, skipping"
  2818. FOUND_REAL_NGINX_CONF=""
  2819. return 0
  2820. fi
  2821. mkdir -p "$DOMAIN_BACKUP_PATH"
  2822. _backup_conf="$DOMAIN_BACKUP_PATH/$_d.nginx.conf"
  2823. _debug _backup_conf "$_backup_conf"
  2824. BACKUP_NGINX_CONF="$_backup_conf"
  2825. _info "Backing $FOUND_REAL_NGINX_CONF up to $_backup_conf"
  2826. if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then
  2827. _err "Backup error."
  2828. FOUND_REAL_NGINX_CONF=""
  2829. return 1
  2830. fi
  2831. if ! _exists "nginx"; then
  2832. _err "nginx command not found."
  2833. return 1
  2834. fi
  2835. _info "Checking the nginx config before setting up."
  2836. if ! nginx -t >/dev/null 2>&1; then
  2837. _err "It seems that the nginx config is not correct, cannot continue."
  2838. return 1
  2839. fi
  2840. _info "OK, setting up the nginx config file"
  2841. if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then
  2842. cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
  2843. _err "Error writing nginx config. Restoring it to its original version."
  2844. return 1
  2845. fi
  2846. echo "$NGINX_START
  2847. location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" {
  2848. default_type text/plain;
  2849. return 200 \"\$1.$_thumbpt\";
  2850. }
  2851. #NGINX_START
  2852. " >>"$FOUND_REAL_NGINX_CONF"
  2853. if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then
  2854. cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
  2855. _err "Error writing nginx config. Restoring it to its original version."
  2856. return 1
  2857. fi
  2858. _debug3 "Modified config:$(cat $FOUND_REAL_NGINX_CONF)"
  2859. _info "nginx config has been written, let's check it again."
  2860. if ! nginx -t >/dev/null 2>&1; then
  2861. _err "There seems to be a problem with the nginx config, let's restore it to its original version."
  2862. cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
  2863. return 1
  2864. fi
  2865. _info "Reloading nginx"
  2866. if ! nginx -s reload >/dev/null 2>&1; then
  2867. _err "There seems to be a problem with the nginx config, let's restore it to its original version."
  2868. cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"
  2869. return 1
  2870. fi
  2871. return 0
  2872. }
  2873. #d , conf
  2874. _checkConf() {
  2875. _d="$1"
  2876. _c_file="$2"
  2877. _debug "Starting _checkConf from: $_c_file"
  2878. if [ ! -f "$2" ] && ! echo "$2" | grep '*$' >/dev/null && echo "$2" | grep '*' >/dev/null; then
  2879. _debug "wildcard"
  2880. for _w_f in $2; do
  2881. if [ -f "$_w_f" ] && _checkConf "$1" "$_w_f"; then
  2882. return 0
  2883. fi
  2884. done
  2885. #not found
  2886. return 1
  2887. elif [ -f "$2" ]; then
  2888. _debug "single"
  2889. if _isRealNginxConf "$1" "$2"; then
  2890. _debug "$2 found."
  2891. FOUND_REAL_NGINX_CONF="$2"
  2892. return 0
  2893. fi
  2894. if cat "$2" | tr "\t" " " | grep "^ *include *.*;" >/dev/null; then
  2895. _debug "Trying include files"
  2896. for included in $(cat "$2" | tr "\t" " " | grep "^ *include *.*;" | sed "s/include //" | tr -d " ;"); do
  2897. _debug "Checking included $included"
  2898. if ! _startswith "$included" "/" && _exists dirname; then
  2899. _relpath="$(dirname "$2")"
  2900. _debug "_relpath" "$_relpath"
  2901. included="$_relpath/$included"
  2902. fi
  2903. if _checkConf "$1" "$included"; then
  2904. return 0
  2905. fi
  2906. done
  2907. fi
  2908. return 1
  2909. else
  2910. _debug "$2 not found."
  2911. return 1
  2912. fi
  2913. return 1
  2914. }
  2915. #d , conf
  2916. _isRealNginxConf() {
  2917. _debug "_isRealNginxConf $1 $2"
  2918. if [ -f "$2" ]; then
  2919. for _fln in $(tr "\t" ' ' <"$2" | grep -n "^ *server_name.* $1" | cut -d : -f 1); do
  2920. _debug _fln "$_fln"
  2921. if [ "$_fln" ]; then
  2922. _start=$(tr "\t" ' ' <"$2" | _head_n "$_fln" | grep -n "^ *server *" | grep -v server_name | _tail_n 1)
  2923. _debug "_start" "$_start"
  2924. _start_n=$(echo "$_start" | cut -d : -f 1)
  2925. _start_nn=$(_math $_start_n + 1)
  2926. _debug "_start_n" "$_start_n"
  2927. _debug "_start_nn" "$_start_nn"
  2928. _left="$(sed -n "${_start_nn},99999p" "$2")"
  2929. _debug2 _left "$_left"
  2930. _end="$(echo "$_left" | tr "\t" ' ' | grep -n "^ *server *" | grep -v server_name | _head_n 1)"
  2931. _debug "_end" "$_end"
  2932. if [ "$_end" ]; then
  2933. _end_n=$(echo "$_end" | cut -d : -f 1)
  2934. _debug "_end_n" "$_end_n"
  2935. _seg_n=$(echo "$_left" | sed -n "1,${_end_n}p")
  2936. else
  2937. _seg_n="$_left"
  2938. fi
  2939. _debug "_seg_n" "$_seg_n"
  2940. _skip_ssl=1
  2941. for _listen_i in $(echo "$_seg_n" | tr "\t" ' ' | grep "^ *listen" | tr -d " "); do
  2942. if [ "$_listen_i" ]; then
  2943. if [ "$(echo "$_listen_i" | _egrep_o "listen.*ssl")" ]; then
  2944. _debug2 "$_listen_i is ssl"
  2945. else
  2946. _debug2 "$_listen_i is plain text"
  2947. _skip_ssl=""
  2948. break
  2949. fi
  2950. fi
  2951. done
  2952. if [ "$_skip_ssl" = "1" ]; then
  2953. _debug "ssl on, skip"
  2954. else
  2955. FOUND_REAL_NGINX_CONF_LN=$_fln
  2956. _debug3 "found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN"
  2957. return 0
  2958. fi
  2959. fi
  2960. done
  2961. fi
  2962. return 1
  2963. }
  2964. #restore all the nginx conf
  2965. _restoreNginx() {
  2966. if [ -z "$NGINX_RESTORE_VLIST" ]; then
  2967. _debug "No need to restore nginx config, skipping."
  2968. return
  2969. fi
  2970. _debug "_restoreNginx"
  2971. _debug "NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
  2972. for ng_entry in $(echo "$NGINX_RESTORE_VLIST" | tr "$dvsep" ' '); do
  2973. _debug "ng_entry" "$ng_entry"
  2974. _nd=$(echo "$ng_entry" | cut -d "$sep" -f 1)
  2975. _ngconf=$(echo "$ng_entry" | cut -d "$sep" -f 2)
  2976. _ngbackupconf=$(echo "$ng_entry" | cut -d "$sep" -f 3)
  2977. _info "Restoring from $_ngbackupconf to $_ngconf"
  2978. cat "$_ngbackupconf" >"$_ngconf"
  2979. done
  2980. _info "Reloading nginx"
  2981. if ! nginx -s reload >/dev/null; then
  2982. _err "An error occurred while reloading nginx, please open an issue on $PROJECT."
  2983. return 1
  2984. fi
  2985. return 0
  2986. }
  2987. _clearup() {
  2988. _stopserver "$serverproc"
  2989. serverproc=""
  2990. _restoreApache
  2991. _restoreNginx
  2992. _clearupdns
  2993. if [ -z "$DEBUG" ]; then
  2994. rm -f "$TLS_CONF"
  2995. rm -f "$TLS_CERT"
  2996. rm -f "$TLS_KEY"
  2997. rm -f "$TLS_CSR"
  2998. fi
  2999. }
  3000. _clearupdns() {
  3001. _debug "_clearupdns"
  3002. _debug "dns_entries" "$dns_entries"
  3003. if [ -z "$dns_entries" ]; then
  3004. _debug "Skipping dns."
  3005. return
  3006. fi
  3007. _info "Removing DNS records."
  3008. for entry in $dns_entries; do
  3009. d=$(_getfield "$entry" 1)
  3010. txtdomain=$(_getfield "$entry" 2)
  3011. aliasDomain=$(_getfield "$entry" 3)
  3012. _currentRoot=$(_getfield "$entry" 4)
  3013. txt=$(_getfield "$entry" 5)
  3014. d_api=$(_getfield "$entry" 6)
  3015. _debug "d" "$d"
  3016. _debug "txtdomain" "$txtdomain"
  3017. _debug "aliasDomain" "$aliasDomain"
  3018. _debug "_currentRoot" "$_currentRoot"
  3019. _debug "txt" "$txt"
  3020. _debug "d_api" "$d_api"
  3021. if [ "$d_api" = "$txt" ]; then
  3022. d_api=""
  3023. fi
  3024. if [ -z "$d_api" ]; then
  3025. _info "Domain API file was not found: $d_api"
  3026. continue
  3027. fi
  3028. if [ "$aliasDomain" ]; then
  3029. txtdomain="$aliasDomain"
  3030. fi
  3031. (
  3032. if ! . "$d_api"; then
  3033. _err "Error loading file $d_api. Please check your API file and try again."
  3034. return 1
  3035. fi
  3036. rmcommand="${_currentRoot}_rm"
  3037. if ! _exists "$rmcommand"; then
  3038. _err "It seems that your API file doesn't define $rmcommand"
  3039. return 1
  3040. fi
  3041. _info "Removing txt: $txt for domain: $txtdomain"
  3042. if ! $rmcommand "$txtdomain" "$txt"; then
  3043. _err "Error removing txt for domain: $txtdomain"
  3044. return 1
  3045. fi
  3046. _info "Successfully removed"
  3047. )
  3048. done
  3049. }
  3050. # webroot removelevel tokenfile
  3051. _clearupwebbroot() {
  3052. __webroot="$1"
  3053. if [ -z "$__webroot" ]; then
  3054. _debug "No webroot specified, skipping"
  3055. return 0
  3056. fi
  3057. _rmpath=""
  3058. if [ "$2" = '1' ]; then
  3059. _rmpath="$__webroot/.well-known"
  3060. elif [ "$2" = '2' ]; then
  3061. _rmpath="$__webroot/.well-known/acme-challenge"
  3062. elif [ "$2" = '3' ]; then
  3063. _rmpath="$__webroot/.well-known/acme-challenge/$3"
  3064. else
  3065. _debug "Skipping for removelevel: $2"
  3066. fi
  3067. if [ "$_rmpath" ]; then
  3068. if [ "$DEBUG" ]; then
  3069. _debug "Debugging, not removing: $_rmpath"
  3070. else
  3071. rm -rf "$_rmpath"
  3072. fi
  3073. fi
  3074. return 0
  3075. }
  3076. _on_before_issue() {
  3077. _chk_web_roots="$1"
  3078. _chk_main_domain="$2"
  3079. _chk_alt_domains="$3"
  3080. _chk_pre_hook="$4"
  3081. _chk_local_addr="$5"
  3082. _debug _on_before_issue
  3083. _debug _chk_main_domain "$_chk_main_domain"
  3084. _debug _chk_alt_domains "$_chk_alt_domains"
  3085. #run pre hook
  3086. if [ "$_chk_pre_hook" ]; then
  3087. _info "Running pre hook:'$_chk_pre_hook'"
  3088. if ! (
  3089. export Le_Domain="$_chk_main_domain"
  3090. export Le_Alt="$_chk_alt_domains"
  3091. cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
  3092. ); then
  3093. _err "Error occurred when running pre hook."
  3094. return 1
  3095. fi
  3096. fi
  3097. if _hasfield "$_chk_web_roots" "$NO_VALUE"; then
  3098. if ! _exists "socat"; then
  3099. _err "Please install socat tools first."
  3100. return 1
  3101. fi
  3102. fi
  3103. _debug Le_LocalAddress "$_chk_local_addr"
  3104. _index=1
  3105. _currentRoot=""
  3106. _addrIndex=1
  3107. _w_index=1
  3108. while true; do
  3109. d="$(echo "$_chk_main_domain,$_chk_alt_domains," | cut -d , -f "$_w_index")"
  3110. _w_index="$(_math "$_w_index" + 1)"
  3111. _debug d "$d"
  3112. if [ -z "$d" ]; then
  3113. break
  3114. fi
  3115. _debug "Checking for domain" "$d"
  3116. _currentRoot="$(_getfield "$_chk_web_roots" $_index)"
  3117. _debug "_currentRoot" "$_currentRoot"
  3118. _index=$(_math $_index + 1)
  3119. _checkport=""
  3120. if [ "$_currentRoot" = "$NO_VALUE" ]; then
  3121. _info "Standalone mode."
  3122. if [ -z "$Le_HTTPPort" ]; then
  3123. Le_HTTPPort=80
  3124. _cleardomainconf "Le_HTTPPort"
  3125. else
  3126. _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
  3127. fi
  3128. _checkport="$Le_HTTPPort"
  3129. elif [ "$_currentRoot" = "$W_ALPN" ]; then
  3130. _info "Standalone alpn mode."
  3131. if [ -z "$Le_TLSPort" ]; then
  3132. Le_TLSPort=443
  3133. else
  3134. _savedomainconf "Le_TLSPort" "$Le_TLSPort"
  3135. fi
  3136. _checkport="$Le_TLSPort"
  3137. fi
  3138. if [ "$_checkport" ]; then
  3139. _debug _checkport "$_checkport"
  3140. _checkaddr="$(_getfield "$_chk_local_addr" $_addrIndex)"
  3141. _debug _checkaddr "$_checkaddr"
  3142. _addrIndex="$(_math $_addrIndex + 1)"
  3143. _netprc="$(_ss "$_checkport" | grep "$_checkport")"
  3144. netprc="$(echo "$_netprc" | grep "$_checkaddr")"
  3145. if [ -z "$netprc" ]; then
  3146. netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS:$_checkport")"
  3147. fi
  3148. if [ "$netprc" ]; then
  3149. _err "$netprc"
  3150. _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
  3151. _err "Please stop it first"
  3152. return 1
  3153. fi
  3154. fi
  3155. done
  3156. if _hasfield "$_chk_web_roots" "apache"; then
  3157. if ! _setApache; then
  3158. _err "Error setting up Apache. Please open an issue on $PROJECT."
  3159. return 1
  3160. fi
  3161. else
  3162. usingApache=""
  3163. fi
  3164. }
  3165. _on_issue_err() {
  3166. _chk_post_hook="$1"
  3167. _chk_vlist="$2"
  3168. _debug _on_issue_err
  3169. if [ "$LOG_FILE" ]; then
  3170. _err "Please check log file for more details: $LOG_FILE"
  3171. else
  3172. _err "Please add '--debug' or '--log' to see more information."
  3173. _err "See: $_DEBUG_WIKI"
  3174. fi
  3175. #run the post hook
  3176. if [ "$_chk_post_hook" ]; then
  3177. _info "Running post hook: '$_chk_post_hook'"
  3178. if ! (
  3179. cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
  3180. ); then
  3181. _err "Error encountered while running post hook."
  3182. return 1
  3183. fi
  3184. fi
  3185. #trigger the validation to flush the pending authz
  3186. _debug2 "_chk_vlist" "$_chk_vlist"
  3187. if [ "$_chk_vlist" ]; then
  3188. (
  3189. _debug2 "start to deactivate authz"
  3190. ventries=$(echo "$_chk_vlist" | tr "$dvsep" ' ')
  3191. for ventry in $ventries; do
  3192. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  3193. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  3194. uri=$(echo "$ventry" | cut -d "$sep" -f 3)
  3195. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  3196. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  3197. __trigger_validation "$uri" "$keyauthorization"
  3198. done
  3199. )
  3200. fi
  3201. if [ "$_ACME_IS_RENEW" = "1" ] && _hasfield "$Le_Webroot" "$W_DNS"; then
  3202. _err "$_DNS_MANUAL_ERR"
  3203. fi
  3204. if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
  3205. _debug "$(_dlg_versions)"
  3206. fi
  3207. }
  3208. _on_issue_success() {
  3209. _chk_post_hook="$1"
  3210. _chk_renew_hook="$2"
  3211. _debug _on_issue_success
  3212. #run the post hook
  3213. if [ "$_chk_post_hook" ]; then
  3214. _info "Running post hook:'$_chk_post_hook'"
  3215. if ! (
  3216. export CERT_PATH
  3217. export CERT_KEY_PATH
  3218. export CA_CERT_PATH
  3219. export CERT_FULLCHAIN_PATH
  3220. export Le_Domain="$_main_domain"
  3221. cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
  3222. ); then
  3223. _err "Error encountered while running post hook."
  3224. return 1
  3225. fi
  3226. fi
  3227. #run renew hook
  3228. if [ "$_ACME_IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
  3229. _info "Running renew hook: '$_chk_renew_hook'"
  3230. if ! (
  3231. export CERT_PATH
  3232. export CERT_KEY_PATH
  3233. export CA_CERT_PATH
  3234. export CERT_FULLCHAIN_PATH
  3235. export Le_Domain="$_main_domain"
  3236. cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
  3237. ); then
  3238. _err "Error encountered while running renew hook."
  3239. return 1
  3240. fi
  3241. fi
  3242. if _hasfield "$Le_Webroot" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then
  3243. _err "$_DNS_MANUAL_WARN"
  3244. fi
  3245. }
  3246. #account_key_length eab-kid eab-hmac-key
  3247. registeraccount() {
  3248. _account_key_length="$1"
  3249. _eab_id="$2"
  3250. _eab_hmac_key="$3"
  3251. _initpath
  3252. _regAccount "$_account_key_length" "$_eab_id" "$_eab_hmac_key"
  3253. }
  3254. __calcAccountKeyHash() {
  3255. [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH"
  3256. }
  3257. __calc_account_thumbprint() {
  3258. printf "%s" "$jwk" | tr -d ' ' | _digest "sha256" | _url_replace
  3259. }
  3260. _getAccountEmail() {
  3261. if [ "$ACCOUNT_EMAIL" ]; then
  3262. echo "$ACCOUNT_EMAIL"
  3263. return 0
  3264. fi
  3265. if [ -z "$CA_EMAIL" ]; then
  3266. CA_EMAIL="$(_readcaconf CA_EMAIL)"
  3267. fi
  3268. if [ "$CA_EMAIL" ]; then
  3269. echo "$CA_EMAIL"
  3270. return 0
  3271. fi
  3272. _readaccountconf "ACCOUNT_EMAIL"
  3273. }
  3274. #keylength
  3275. _regAccount() {
  3276. _initpath
  3277. _reg_length="$1"
  3278. _eab_id="$2"
  3279. _eab_hmac_key="$3"
  3280. _debug3 _regAccount "$_regAccount"
  3281. _initAPI
  3282. mkdir -p "$CA_DIR"
  3283. if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
  3284. if ! _create_account_key "$_reg_length"; then
  3285. _err "Error creating account key."
  3286. return 1
  3287. fi
  3288. fi
  3289. if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
  3290. return 1
  3291. fi
  3292. if [ "$_eab_id" ] && [ "$_eab_hmac_key" ]; then
  3293. _savecaconf CA_EAB_KEY_ID "$_eab_id"
  3294. _savecaconf CA_EAB_HMAC_KEY "$_eab_hmac_key"
  3295. fi
  3296. _eab_id=$(_readcaconf "CA_EAB_KEY_ID")
  3297. _eab_hmac_key=$(_readcaconf "CA_EAB_HMAC_KEY")
  3298. _secure_debug3 _eab_id "$_eab_id"
  3299. _secure_debug3 _eab_hmac_key "$_eab_hmac_key"
  3300. _email="$(_getAccountEmail)"
  3301. if [ "$_email" ]; then
  3302. _savecaconf "CA_EMAIL" "$_email"
  3303. fi
  3304. if [ "$ACME_DIRECTORY" = "$CA_ZEROSSL" ]; then
  3305. if [ -z "$_eab_id" ] || [ -z "$_eab_hmac_key" ]; then
  3306. _info "No EAB credentials found for ZeroSSL, let's obtain them"
  3307. if [ -z "$_email" ]; then
  3308. _info "$(__green "$PROJECT_NAME is using ZeroSSL as default CA now.")"
  3309. _info "$(__green "Please update your account with an email address first.")"
  3310. _info "$(__green "$PROJECT_ENTRY --register-account -m [email protected]")"
  3311. _info "See: $(__green "$_ZEROSSL_WIKI")"
  3312. return 1
  3313. fi
  3314. _eabresp=$(_post "email=$_email" $_ZERO_EAB_ENDPOINT)
  3315. if [ "$?" != "0" ]; then
  3316. _debug2 "$_eabresp"
  3317. _err "Cannot get EAB credentials from ZeroSSL."
  3318. return 1
  3319. fi
  3320. _secure_debug2 _eabresp "$_eabresp"
  3321. _eab_id="$(echo "$_eabresp" | tr ',}' '\n\n' | grep '"eab_kid"' | cut -d : -f 2 | tr -d '"')"
  3322. _secure_debug2 _eab_id "$_eab_id"
  3323. if [ -z "$_eab_id" ]; then
  3324. _err "Cannot resolve _eab_id"
  3325. return 1
  3326. fi
  3327. _eab_hmac_key="$(echo "$_eabresp" | tr ',}' '\n\n' | grep '"eab_hmac_key"' | cut -d : -f 2 | tr -d '"')"
  3328. _secure_debug2 _eab_hmac_key "$_eab_hmac_key"
  3329. if [ -z "$_eab_hmac_key" ]; then
  3330. _err "Cannot resolve _eab_hmac_key"
  3331. return 1
  3332. fi
  3333. _savecaconf CA_EAB_KEY_ID "$_eab_id"
  3334. _savecaconf CA_EAB_HMAC_KEY "$_eab_hmac_key"
  3335. fi
  3336. fi
  3337. if [ "$_eab_id" ] && [ "$_eab_hmac_key" ]; then
  3338. eab_protected="{\"alg\":\"HS256\",\"kid\":\"$_eab_id\",\"url\":\"${ACME_NEW_ACCOUNT}\"}"
  3339. _debug3 eab_protected "$eab_protected"
  3340. eab_protected64=$(printf "%s" "$eab_protected" | _base64 | _url_replace)
  3341. _debug3 eab_protected64 "$eab_protected64"
  3342. eab_payload64=$(printf "%s" "$jwk" | _base64 | _url_replace)
  3343. _debug3 eab_payload64 "$eab_payload64"
  3344. eab_sign_t="$eab_protected64.$eab_payload64"
  3345. _debug3 eab_sign_t "$eab_sign_t"
  3346. key_hex="$(_durl_replace_base64 "$_eab_hmac_key" | _dbase64 | _hex_dump | tr -d ' ')"
  3347. _debug3 key_hex "$key_hex"
  3348. eab_signature=$(printf "%s" "$eab_sign_t" | _hmac sha256 $key_hex | _base64 | _url_replace)
  3349. _debug3 eab_signature "$eab_signature"
  3350. externalBinding=",\"externalAccountBinding\":{\"protected\":\"$eab_protected64\", \"payload\":\"$eab_payload64\", \"signature\":\"$eab_signature\"}"
  3351. _debug3 externalBinding "$externalBinding"
  3352. fi
  3353. if [ "$_email" ]; then
  3354. email_sg="\"contact\": [\"mailto:$_email\"], "
  3355. fi
  3356. regjson="{$email_sg\"termsOfServiceAgreed\": true$externalBinding}"
  3357. _info "Registering account: $ACME_DIRECTORY"
  3358. if ! _send_signed_request "${ACME_NEW_ACCOUNT}" "$regjson"; then
  3359. _err "Error registering account: $response"
  3360. return 1
  3361. fi
  3362. _eabAlreadyBound=""
  3363. if [ "$code" = "" ] || [ "$code" = '201' ]; then
  3364. echo "$response" >"$ACCOUNT_JSON_PATH"
  3365. _info "Registered"
  3366. elif [ "$code" = '409' ] || [ "$code" = '200' ]; then
  3367. _info "Already registered"
  3368. elif [ "$code" = '400' ] && _contains "$response" 'The account is not awaiting external account binding'; then
  3369. _info "EAB already registered"
  3370. _eabAlreadyBound=1
  3371. else
  3372. _err "Account registration error: $response"
  3373. return 1
  3374. fi
  3375. if [ -z "$_eabAlreadyBound" ]; then
  3376. _debug2 responseHeaders "$responseHeaders"
  3377. _accUri="$(echo "$responseHeaders" | grep -i "^Location:" | _head_n 1 | cut -d ':' -f 2- | tr -d "\r\n ")"
  3378. _debug "_accUri" "$_accUri"
  3379. if [ -z "$_accUri" ]; then
  3380. _err "Cannot find account id url."
  3381. _err "$responseHeaders"
  3382. return 1
  3383. fi
  3384. _savecaconf "ACCOUNT_URL" "$_accUri"
  3385. else
  3386. ACCOUNT_URL="$(_readcaconf ACCOUNT_URL)"
  3387. fi
  3388. export ACCOUNT_URL="$_accUri"
  3389. CA_KEY_HASH="$(__calcAccountKeyHash)"
  3390. _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH"
  3391. _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
  3392. if [ "$code" = '403' ]; then
  3393. _err "It seems that the account key has been deactivated, please use a new account key."
  3394. return 1
  3395. fi
  3396. ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)"
  3397. _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
  3398. }
  3399. #implement updateaccount
  3400. updateaccount() {
  3401. _initpath
  3402. if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
  3403. _err "Account key not found at: $ACCOUNT_KEY_PATH"
  3404. return 1
  3405. fi
  3406. _accUri=$(_readcaconf "ACCOUNT_URL")
  3407. _debug _accUri "$_accUri"
  3408. if [ -z "$_accUri" ]; then
  3409. _err "The account URL is empty, please run '--update-account' first to update the account info, then try again."
  3410. return 1
  3411. fi
  3412. if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
  3413. return 1
  3414. fi
  3415. _initAPI
  3416. _email="$(_getAccountEmail)"
  3417. if [ "$_email" ]; then
  3418. updjson='{"contact": ["mailto:'$_email'"]}'
  3419. else
  3420. updjson='{"contact": []}'
  3421. fi
  3422. _send_signed_request "$_accUri" "$updjson"
  3423. if [ "$code" = '200' ]; then
  3424. echo "$response" >"$ACCOUNT_JSON_PATH"
  3425. _info "Account update success for $_accUri."
  3426. ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)"
  3427. _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
  3428. else
  3429. _info "An error occurred and the account was not updated."
  3430. return 1
  3431. fi
  3432. }
  3433. #Implement deactivate account
  3434. deactivateaccount() {
  3435. _initpath
  3436. if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
  3437. _err "Account key not found at: $ACCOUNT_KEY_PATH"
  3438. return 1
  3439. fi
  3440. _accUri=$(_readcaconf "ACCOUNT_URL")
  3441. _debug _accUri "$_accUri"
  3442. if [ -z "$_accUri" ]; then
  3443. _err "The account URL is empty, please run '--update-account' first to update the account info, then try again."
  3444. return 1
  3445. fi
  3446. if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
  3447. return 1
  3448. fi
  3449. _initAPI
  3450. _djson="{\"status\":\"deactivated\"}"
  3451. if _send_signed_request "$_accUri" "$_djson" && _contains "$response" '"deactivated"'; then
  3452. _info "Successfully deactivated account $_accUri."
  3453. _accid=$(echo "$response" | _egrep_o "\"id\" *: *[^,]*," | cut -d : -f 2 | tr -d ' ,')
  3454. elif [ "$code" = "403" ]; then
  3455. _info "The account is already deactivated."
  3456. _accid=$(_getfield "$_accUri" "999" "/")
  3457. else
  3458. _err "Account deactivation failed for $_accUri."
  3459. return 1
  3460. fi
  3461. _debug "Account id: $_accid"
  3462. if [ "$_accid" ]; then
  3463. _deactivated_account_path="$CA_DIR/deactivated/$_accid"
  3464. _debug _deactivated_account_path "$_deactivated_account_path"
  3465. if mkdir -p "$_deactivated_account_path"; then
  3466. _info "Moving deactivated account info to $_deactivated_account_path/"
  3467. mv "$CA_CONF" "$_deactivated_account_path/"
  3468. mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/"
  3469. mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/"
  3470. else
  3471. _err "Cannot create dir: $_deactivated_account_path, try to remove the deactivated account key."
  3472. rm -f "$CA_CONF"
  3473. rm -f "$ACCOUNT_JSON_PATH"
  3474. rm -f "$ACCOUNT_KEY_PATH"
  3475. fi
  3476. fi
  3477. }
  3478. # domain folder file
  3479. _findHook() {
  3480. _hookdomain="$1"
  3481. _hookcat="$2"
  3482. _hookname="$3"
  3483. if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
  3484. d_api="$_SCRIPT_HOME/$_hookcat/$_hookname"
  3485. elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
  3486. d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
  3487. elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
  3488. d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
  3489. elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
  3490. d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
  3491. elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
  3492. d_api="$LE_WORKING_DIR/$_hookname"
  3493. elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
  3494. d_api="$LE_WORKING_DIR/$_hookname.sh"
  3495. elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
  3496. d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
  3497. elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
  3498. d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
  3499. fi
  3500. printf "%s" "$d_api"
  3501. }
  3502. #domain
  3503. __get_domain_new_authz() {
  3504. _gdnd="$1"
  3505. _info "Getting new-authz for domain" "$_gdnd"
  3506. _initAPI
  3507. _Max_new_authz_retry_times=5
  3508. _authz_i=0
  3509. while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
  3510. _debug "Trying new-authz, attempt number $_authz_i."
  3511. if ! _send_signed_request "${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
  3512. _err "Cannot get new authz for domain."
  3513. return 1
  3514. fi
  3515. if _contains "$response" "No registration exists matching provided key"; then
  3516. _err "There has been an error, but it might now be resolved, please try again."
  3517. _err "If you see this message for a second time, please report this as a bug: $(__green "$PROJECT")"
  3518. _clearcaconf "CA_KEY_HASH"
  3519. break
  3520. fi
  3521. if ! _contains "$response" "An error occurred while processing your request"; then
  3522. _info "new-authz request successful."
  3523. break
  3524. fi
  3525. _authz_i="$(_math "$_authz_i" + 1)"
  3526. _info "The server is busy, sleeping for $_authz_i seconds and retrying."
  3527. _sleep "$_authz_i"
  3528. done
  3529. if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
  3530. _err "new-authz has been retried $_Max_new_authz_retry_times times, stopping."
  3531. fi
  3532. if [ "$code" ] && [ "$code" != '201' ]; then
  3533. _err "new-authz error: $response"
  3534. return 1
  3535. fi
  3536. }
  3537. #uri keyAuthorization
  3538. __trigger_validation() {
  3539. _debug2 "Trigger domain validation."
  3540. _t_url="$1"
  3541. _debug2 _t_url "$_t_url"
  3542. _t_key_authz="$2"
  3543. _debug2 _t_key_authz "$_t_key_authz"
  3544. _t_vtype="$3"
  3545. _debug2 _t_vtype "$_t_vtype"
  3546. _send_signed_request "$_t_url" "{}"
  3547. }
  3548. #endpoint domain type
  3549. _ns_lookup_impl() {
  3550. _ns_ep="$1"
  3551. _ns_domain="$2"
  3552. _ns_type="$3"
  3553. _debug2 "_ns_ep" "$_ns_ep"
  3554. _debug2 "_ns_domain" "$_ns_domain"
  3555. _debug2 "_ns_type" "$_ns_type"
  3556. response="$(_H1="accept: application/dns-json" _get "$_ns_ep?name=$_ns_domain&type=$_ns_type")"
  3557. _ret=$?
  3558. _debug2 "response" "$response"
  3559. if [ "$_ret" != "0" ]; then
  3560. return $_ret
  3561. fi
  3562. _answers="$(echo "$response" | tr '{}' '<>' | _egrep_o '"Answer":\[[^]]*]' | tr '<>' '\n\n')"
  3563. _debug2 "_answers" "$_answers"
  3564. echo "$_answers"
  3565. }
  3566. #domain, type
  3567. _ns_lookup_cf() {
  3568. _cf_ld="$1"
  3569. _cf_ld_type="$2"
  3570. _cf_ep="https://cloudflare-dns.com/dns-query"
  3571. _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type"
  3572. }
  3573. #domain, type
  3574. _ns_purge_cf() {
  3575. _cf_d="$1"
  3576. _cf_d_type="$2"
  3577. _debug "Purging Cloudflare $_cf_d_type record for domain $_cf_d"
  3578. _cf_purl="https://cloudflare-dns.com/api/v1/purge?domain=$_cf_d&type=$_cf_d_type"
  3579. response="$(_post "" "$_cf_purl")"
  3580. _debug2 response "$response"
  3581. }
  3582. #checks if cf server is available
  3583. _ns_is_available_cf() {
  3584. if _get "https://cloudflare-dns.com" "" 10 >/dev/null; then
  3585. return 0
  3586. else
  3587. return 1
  3588. fi
  3589. }
  3590. _ns_is_available_google() {
  3591. if _get "https://dns.google" "" 10 >/dev/null; then
  3592. return 0
  3593. else
  3594. return 1
  3595. fi
  3596. }
  3597. #domain, type
  3598. _ns_lookup_google() {
  3599. _cf_ld="$1"
  3600. _cf_ld_type="$2"
  3601. _cf_ep="https://dns.google/resolve"
  3602. _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type"
  3603. }
  3604. _ns_is_available_ali() {
  3605. if _get "https://dns.alidns.com" "" 10 >/dev/null; then
  3606. return 0
  3607. else
  3608. return 1
  3609. fi
  3610. }
  3611. #domain, type
  3612. _ns_lookup_ali() {
  3613. _cf_ld="$1"
  3614. _cf_ld_type="$2"
  3615. _cf_ep="https://dns.alidns.com/resolve"
  3616. _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type"
  3617. }
  3618. _ns_is_available_dp() {
  3619. if _get "https://doh.pub" "" 10 >/dev/null; then
  3620. return 0
  3621. else
  3622. return 1
  3623. fi
  3624. }
  3625. #dnspod
  3626. _ns_lookup_dp() {
  3627. _cf_ld="$1"
  3628. _cf_ld_type="$2"
  3629. _cf_ep="https://doh.pub/dns-query"
  3630. _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type"
  3631. }
  3632. _ns_select_doh() {
  3633. if [ -z "$DOH_USE" ]; then
  3634. _debug "Detecting DNS server first."
  3635. if _ns_is_available_cf; then
  3636. _debug "Using Cloudflare doh server"
  3637. export DOH_USE=$DOH_CLOUDFLARE
  3638. elif _ns_is_available_google; then
  3639. _debug "Using Google DOH server"
  3640. export DOH_USE=$DOH_GOOGLE
  3641. elif _ns_is_available_ali; then
  3642. _debug "Using Aliyun DOH server"
  3643. export DOH_USE=$DOH_ALI
  3644. elif _ns_is_available_dp; then
  3645. _debug "Using DNS POD DOH server"
  3646. export DOH_USE=$DOH_DP
  3647. else
  3648. _err "No DOH"
  3649. fi
  3650. fi
  3651. }
  3652. #domain, type
  3653. _ns_lookup() {
  3654. _ns_select_doh
  3655. if [ "$DOH_USE" = "$DOH_CLOUDFLARE" ] || [ -z "$DOH_USE" ]; then
  3656. _ns_lookup_cf "$@"
  3657. elif [ "$DOH_USE" = "$DOH_GOOGLE" ]; then
  3658. _ns_lookup_google "$@"
  3659. elif [ "$DOH_USE" = "$DOH_ALI" ]; then
  3660. _ns_lookup_ali "$@"
  3661. elif [ "$DOH_USE" = "$DOH_DP" ]; then
  3662. _ns_lookup_dp "$@"
  3663. else
  3664. _err "Unknown DOH provider: DOH_USE=$DOH_USE"
  3665. fi
  3666. }
  3667. #txtdomain, alias, txt
  3668. __check_txt() {
  3669. _c_txtdomain="$1"
  3670. _c_aliasdomain="$2"
  3671. _c_txt="$3"
  3672. _debug "_c_txtdomain" "$_c_txtdomain"
  3673. _debug "_c_aliasdomain" "$_c_aliasdomain"
  3674. _debug "_c_txt" "$_c_txt"
  3675. _ns_select_doh
  3676. _answers="$(_ns_lookup "$_c_aliasdomain" TXT)"
  3677. _contains "$_answers" "$_c_txt"
  3678. }
  3679. #txtdomain
  3680. __purge_txt() {
  3681. _p_txtdomain="$1"
  3682. _debug _p_txtdomain "$_p_txtdomain"
  3683. if [ "$DOH_USE" = "$DOH_CLOUDFLARE" ] || [ -z "$DOH_USE" ]; then
  3684. _ns_purge_cf "$_p_txtdomain" "TXT"
  3685. else
  3686. _debug "No purge API for this DOH API, just sleeping for 5 seconds"
  3687. _sleep 5
  3688. fi
  3689. }
  3690. #wait and check each dns entries
  3691. _check_dns_entries() {
  3692. _success_txt=","
  3693. _end_time="$(_time)"
  3694. _end_time="$(_math "$_end_time" + 1200)" #let's check no more than 20 minutes.
  3695. while [ "$(_time)" -le "$_end_time" ]; do
  3696. _info "You can use '--dnssleep' to disable public dns checks."
  3697. _info "See: $_DNSCHECK_WIKI"
  3698. _left=""
  3699. for entry in $dns_entries; do
  3700. d=$(_getfield "$entry" 1)
  3701. txtdomain=$(_getfield "$entry" 2)
  3702. txtdomain=$(_idn "$txtdomain")
  3703. aliasDomain=$(_getfield "$entry" 3)
  3704. aliasDomain=$(_idn "$aliasDomain")
  3705. txt=$(_getfield "$entry" 5)
  3706. d_api=$(_getfield "$entry" 6)
  3707. _debug "d" "$d"
  3708. _debug "txtdomain" "$txtdomain"
  3709. _debug "aliasDomain" "$aliasDomain"
  3710. _debug "txt" "$txt"
  3711. _debug "d_api" "$d_api"
  3712. _info "Checking $d for $aliasDomain"
  3713. if _contains "$_success_txt" ",$txt,"; then
  3714. _info "Already succeeded, continuing."
  3715. continue
  3716. fi
  3717. if __check_txt "$txtdomain" "$aliasDomain" "$txt"; then
  3718. _info "Success for domain $d '$aliasDomain'."
  3719. _success_txt="$_success_txt,$txt,"
  3720. continue
  3721. fi
  3722. _left=1
  3723. _info "Not valid yet, let's wait for 10 seconds then check the next one."
  3724. __purge_txt "$txtdomain"
  3725. if [ "$txtdomain" != "$aliasDomain" ]; then
  3726. __purge_txt "$aliasDomain"
  3727. fi
  3728. _sleep 10
  3729. done
  3730. if [ "$_left" ]; then
  3731. _info "Let's wait for 10 seconds and check again".
  3732. _sleep 10
  3733. else
  3734. _info "All checks succeeded"
  3735. return 0
  3736. fi
  3737. done
  3738. _info "Timed out waiting for DNS."
  3739. return 1
  3740. }
  3741. #file
  3742. _get_chain_issuers() {
  3743. _cfile="$1"
  3744. if _contains "$(${ACME_OPENSSL_BIN:-openssl} help crl2pkcs7 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -help 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 help 2>&1)" "unknown option help"; then
  3745. ${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -nocrl -certfile $_cfile | ${ACME_OPENSSL_BIN:-openssl} pkcs7 -print_certs -text -noout | grep -i 'Issuer:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2
  3746. else
  3747. _cindex=1
  3748. for _startn in $(grep -n -- "$BEGIN_CERT" "$_cfile" | cut -d : -f 1); do
  3749. _endn="$(grep -n -- "$END_CERT" "$_cfile" | cut -d : -f 1 | _head_n $_cindex | _tail_n 1)"
  3750. _debug2 "_startn" "$_startn"
  3751. _debug2 "_endn" "$_endn"
  3752. if [ "$DEBUG" ]; then
  3753. _debug2 "cert$_cindex" "$(sed -n "$_startn,${_endn}p" "$_cfile")"
  3754. fi
  3755. sed -n "$_startn,${_endn}p" "$_cfile" | ${ACME_OPENSSL_BIN:-openssl} x509 -text -noout | grep 'Issuer:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | sed "s/ *\(.*\)/\1/"
  3756. _cindex=$(_math $_cindex + 1)
  3757. done
  3758. fi
  3759. }
  3760. #
  3761. _get_chain_subjects() {
  3762. _cfile="$1"
  3763. if _contains "$(${ACME_OPENSSL_BIN:-openssl} help crl2pkcs7 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -help 2>&1)" "Usage: crl2pkcs7" || _contains "$(${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 help 2>&1)" "unknown option help"; then
  3764. ${ACME_OPENSSL_BIN:-openssl} crl2pkcs7 -nocrl -certfile $_cfile | ${ACME_OPENSSL_BIN:-openssl} pkcs7 -print_certs -text -noout | grep -i 'Subject:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2
  3765. else
  3766. _cindex=1
  3767. for _startn in $(grep -n -- "$BEGIN_CERT" "$_cfile" | cut -d : -f 1); do
  3768. _endn="$(grep -n -- "$END_CERT" "$_cfile" | cut -d : -f 1 | _head_n $_cindex | _tail_n 1)"
  3769. _debug2 "_startn" "$_startn"
  3770. _debug2 "_endn" "$_endn"
  3771. if [ "$DEBUG" ]; then
  3772. _debug2 "cert$_cindex" "$(sed -n "$_startn,${_endn}p" "$_cfile")"
  3773. fi
  3774. sed -n "$_startn,${_endn}p" "$_cfile" | ${ACME_OPENSSL_BIN:-openssl} x509 -text -noout | grep -i 'Subject:' | _egrep_o "CN *=[^,]*" | cut -d = -f 2 | sed "s/ *\(.*\)/\1/"
  3775. _cindex=$(_math $_cindex + 1)
  3776. done
  3777. fi
  3778. }
  3779. #cert issuer
  3780. _match_issuer() {
  3781. _cfile="$1"
  3782. _missuer="$2"
  3783. _fissuers="$(_get_chain_issuers $_cfile)"
  3784. _debug2 _fissuers "$_fissuers"
  3785. _rootissuer="$(echo "$_fissuers" | _lower_case | _tail_n 1)"
  3786. _debug2 _rootissuer "$_rootissuer"
  3787. _missuer="$(echo "$_missuer" | _lower_case)"
  3788. _contains "$_rootissuer" "$_missuer"
  3789. }
  3790. #ip
  3791. _isIPv4() {
  3792. for seg in $(echo "$1" | tr '.' ' '); do
  3793. _debug2 seg "$seg"
  3794. if [ "$(echo "$seg" | tr -d '[0-9]')" ]; then
  3795. #not all number
  3796. return 1
  3797. fi
  3798. if [ $seg -ge 0 ] && [ $seg -lt 256 ]; then
  3799. continue
  3800. fi
  3801. return 1
  3802. done
  3803. return 0
  3804. }
  3805. #ip6
  3806. _isIPv6() {
  3807. _contains "$1" ":"
  3808. }
  3809. #ip
  3810. _isIP() {
  3811. _isIPv4 "$1" || _isIPv6 "$1"
  3812. }
  3813. #identifier
  3814. _getIdType() {
  3815. if _isIP "$1"; then
  3816. echo "$ID_TYPE_IP"
  3817. else
  3818. echo "$ID_TYPE_DNS"
  3819. fi
  3820. }
  3821. # beginTime dateTo
  3822. # beginTime is full string format("2022-04-01T08:10:33Z"), beginTime can be empty, to use current time
  3823. # dateTo can be ether in full string format("2022-04-01T08:10:33Z") or in delta format(+5d or +20h)
  3824. _convertValidaty() {
  3825. _beginTime="$1"
  3826. _dateTo="$2"
  3827. _debug2 "_beginTime" "$_beginTime"
  3828. _debug2 "_dateTo" "$_dateTo"
  3829. if _startswith "$_dateTo" "+"; then
  3830. _v_begin=$(_time)
  3831. if [ "$_beginTime" ]; then
  3832. _v_begin="$(_date2time "$_beginTime")"
  3833. fi
  3834. _debug2 "_v_begin" "$_v_begin"
  3835. if _endswith "$_dateTo" "h"; then
  3836. _v_end=$(_math "$_v_begin + 60 * 60 * $(echo "$_dateTo" | tr -d '+h')")
  3837. elif _endswith "$_dateTo" "d"; then
  3838. _v_end=$(_math "$_v_begin + 60 * 60 * 24 * $(echo "$_dateTo" | tr -d '+d')")
  3839. else
  3840. _err "Unrecognized format for _dateTo: $_dateTo"
  3841. return 1
  3842. fi
  3843. _debug2 "_v_end" "$_v_end"
  3844. _time2str "$_v_end"
  3845. else
  3846. if [ "$(_time)" -gt "$(_date2time "$_dateTo")" ]; then
  3847. _err "The validity end date is in the past: _dateTo = $_dateTo"
  3848. return 1
  3849. fi
  3850. echo "$_dateTo"
  3851. fi
  3852. }
  3853. #webroot, domain domainlist keylength
  3854. issue() {
  3855. if [ -z "$2" ]; then
  3856. _usage "Usage: $PROJECT_ENTRY --issue --domain <domain.tld> --webroot <directory>"
  3857. return 1
  3858. fi
  3859. if [ -z "$1" ]; then
  3860. _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
  3861. return 1
  3862. fi
  3863. _web_roots="$1"
  3864. _main_domain="$2"
  3865. _alt_domains="$3"
  3866. if _contains "$_main_domain" ","; then
  3867. _main_domain=$(echo "$2,$3" | cut -d , -f 1)
  3868. _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//")
  3869. fi
  3870. _debug _main_domain "$_main_domain"
  3871. _debug _alt_domains "$_alt_domains"
  3872. _key_length="$4"
  3873. _real_cert="$5"
  3874. _real_key="$6"
  3875. _real_ca="$7"
  3876. _reload_cmd="$8"
  3877. _real_fullchain="$9"
  3878. _pre_hook="${10}"
  3879. _post_hook="${11}"
  3880. _renew_hook="${12}"
  3881. _local_addr="${13}"
  3882. _challenge_alias="${14}"
  3883. _preferred_chain="${15}"
  3884. _valid_from="${16}"
  3885. _valid_to="${17}"
  3886. _certificate_profile="${18}"
  3887. if [ -z "$_ACME_IS_RENEW" ]; then
  3888. _initpath "$_main_domain" "$_key_length"
  3889. mkdir -p "$DOMAIN_PATH"
  3890. elif ! _hasfield "$_web_roots" "$W_DNS"; then
  3891. Le_OrderFinalize=""
  3892. Le_LinkOrder=""
  3893. Le_LinkCert=""
  3894. fi
  3895. if _hasfield "$_web_roots" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then
  3896. _err "$_DNS_MANUAL_ERROR"
  3897. return 1
  3898. fi
  3899. if [ -f "$DOMAIN_CONF" ]; then
  3900. Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
  3901. _debug Le_NextRenewTime "$Le_NextRenewTime"
  3902. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
  3903. _valid_to_saved=$(_readdomainconf Le_Valid_to)
  3904. if [ "$_valid_to_saved" ] && ! _startswith "$_valid_to_saved" "+"; then
  3905. _info "The domain is set to be valid to: $_valid_to_saved"
  3906. _info "It cannot be renewed automatically"
  3907. _info "See: $_VALIDITY_WIKI"
  3908. return $RENEW_SKIP
  3909. fi
  3910. _saved_domain=$(_readdomainconf Le_Domain)
  3911. _debug _saved_domain "$_saved_domain"
  3912. _saved_alt=$(_readdomainconf Le_Alt)
  3913. _debug _saved_alt "$_saved_alt"
  3914. _normized_saved_domains="$(echo "$_saved_domain,$_saved_alt" | tr "," "\n" | sort | tr '\n' ',')"
  3915. _debug _normized_saved_domains "$_normized_saved_domains"
  3916. _normized_domains="$(echo "$_main_domain,$_alt_domains" | tr "," "\n" | sort | tr '\n' ',')"
  3917. _debug _normized_domains "$_normized_domains"
  3918. if [ "$_normized_saved_domains" = "$_normized_domains" ]; then
  3919. _info "Domains not changed."
  3920. _info "Skipping. Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
  3921. _info "Add '$(__red '--force')' to force renewal."
  3922. return $RENEW_SKIP
  3923. else
  3924. _info "Domains have changed."
  3925. fi
  3926. fi
  3927. fi
  3928. _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY"
  3929. if ! _initAPI; then
  3930. return 1
  3931. fi
  3932. _savedomainconf "Le_Domain" "$_main_domain"
  3933. _savedomainconf "Le_Alt" "$_alt_domains"
  3934. _savedomainconf "Le_Webroot" "$_web_roots"
  3935. _savedomainconf "Le_PreHook" "$_pre_hook" "base64"
  3936. _savedomainconf "Le_PostHook" "$_post_hook" "base64"
  3937. _savedomainconf "Le_RenewHook" "$_renew_hook" "base64"
  3938. if [ "$_local_addr" ]; then
  3939. _savedomainconf "Le_LocalAddress" "$_local_addr"
  3940. else
  3941. _cleardomainconf "Le_LocalAddress"
  3942. fi
  3943. if [ "$_challenge_alias" ]; then
  3944. _savedomainconf "Le_ChallengeAlias" "$_challenge_alias"
  3945. else
  3946. _cleardomainconf "Le_ChallengeAlias"
  3947. fi
  3948. if [ "$_preferred_chain" ]; then
  3949. _savedomainconf "Le_Preferred_Chain" "$_preferred_chain" "base64"
  3950. else
  3951. _cleardomainconf "Le_Preferred_Chain"
  3952. fi
  3953. if [ "$_certificate_profile" ]; then
  3954. _savedomainconf "Le_Certificate_Profile" "$_certificate_profile"
  3955. else
  3956. _cleardomainconf "Le_Certificate_Profile"
  3957. fi
  3958. Le_API="$ACME_DIRECTORY"
  3959. _savedomainconf "Le_API" "$Le_API"
  3960. _info "Using CA: $ACME_DIRECTORY"
  3961. if [ "$_alt_domains" = "$NO_VALUE" ]; then
  3962. _alt_domains=""
  3963. fi
  3964. if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
  3965. _err "_on_before_issue."
  3966. _on_issue_err "$_post_hook"
  3967. return 1
  3968. fi
  3969. _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
  3970. _debug2 _saved_account_key_hash "$_saved_account_key_hash"
  3971. if [ -z "$ACCOUNT_URL" ] || [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
  3972. if ! _regAccount "$_accountkeylength"; then
  3973. _on_issue_err "$_post_hook"
  3974. return 1
  3975. fi
  3976. else
  3977. _debug "_saved_account_key_hash was not changed, skipping account registration."
  3978. fi
  3979. export Le_Next_Domain_Key="$CERT_KEY_PATH.next"
  3980. if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
  3981. _info "Signing from existing CSR."
  3982. else
  3983. # When renewing from an old version, the empty Le_Keylength means 2048.
  3984. # Note, do not use DEFAULT_DOMAIN_KEY_LENGTH as that value may change over
  3985. # time but an empty value implies 2048 specifically.
  3986. _key=$(_readdomainconf Le_Keylength)
  3987. if [ -z "$_key" ]; then
  3988. _key=2048
  3989. fi
  3990. _debug "Read key length: $_key"
  3991. if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then
  3992. if [ "$Le_ForceNewDomainKey" = "1" ] && [ -f "$Le_Next_Domain_Key" ]; then
  3993. _info "Using pre-generated key: $Le_Next_Domain_Key"
  3994. cat "$Le_Next_Domain_Key" >"$CERT_KEY_PATH"
  3995. echo "" >"$Le_Next_Domain_Key"
  3996. else
  3997. if ! createDomainKey "$_main_domain" "$_key_length"; then
  3998. _err "Error creating domain key."
  3999. _clearup
  4000. _on_issue_err "$_post_hook"
  4001. return 1
  4002. fi
  4003. fi
  4004. fi
  4005. if [ "$Le_ForceNewDomainKey" ]; then
  4006. _info "Generating next pre-generate key."
  4007. if [ ! -e "$Le_Next_Domain_Key" ]; then
  4008. touch "$Le_Next_Domain_Key"
  4009. chmod 600 "$Le_Next_Domain_Key"
  4010. fi
  4011. if ! _createkey "$_key_length" "$Le_Next_Domain_Key"; then
  4012. _err "Cannot pre-generate domain key"
  4013. return 1
  4014. fi
  4015. fi
  4016. if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
  4017. _err "Error creating CSR."
  4018. _clearup
  4019. _on_issue_err "$_post_hook"
  4020. return 1
  4021. fi
  4022. fi
  4023. _savedomainconf "Le_Keylength" "$_key_length"
  4024. vlist="$Le_Vlist"
  4025. _cleardomainconf "Le_Vlist"
  4026. _debug "Getting domain auth token for each domain"
  4027. sep='#'
  4028. dvsep=','
  4029. if [ -z "$vlist" ]; then
  4030. #make new order request
  4031. _identifiers="{\"type\":\"$(_getIdType "$_main_domain")\",\"value\":\"$(_idn "$_main_domain")\"}"
  4032. _w_index=1
  4033. while true; do
  4034. d="$(echo "$_alt_domains," | cut -d , -f "$_w_index")"
  4035. _w_index="$(_math "$_w_index" + 1)"
  4036. _debug d "$d"
  4037. if [ -z "$d" ]; then
  4038. break
  4039. fi
  4040. _identifiers="$_identifiers,{\"type\":\"$(_getIdType "$d")\",\"value\":\"$(_idn "$d")\"}"
  4041. done
  4042. _debug2 _identifiers "$_identifiers"
  4043. _notBefore=""
  4044. _notAfter=""
  4045. if [ "$_valid_from" ]; then
  4046. _savedomainconf "Le_Valid_From" "$_valid_from"
  4047. _debug2 "_valid_from" "$_valid_from"
  4048. _notBefore="$(_convertValidaty "" "$_valid_from")"
  4049. if [ "$?" != "0" ]; then
  4050. _err "Cannot parse _valid_from: $_valid_from"
  4051. return 1
  4052. fi
  4053. if [ "$(_time)" -gt "$(_date2time "$_notBefore")" ]; then
  4054. _notBefore=""
  4055. fi
  4056. else
  4057. _cleardomainconf "Le_Valid_From"
  4058. fi
  4059. _debug2 _notBefore "$_notBefore"
  4060. if [ "$_valid_to" ]; then
  4061. _debug2 "_valid_to" "$_valid_to"
  4062. _savedomainconf "Le_Valid_To" "$_valid_to"
  4063. _notAfter="$(_convertValidaty "$_notBefore" "$_valid_to")"
  4064. if [ "$?" != "0" ]; then
  4065. _err "Cannot parse _valid_to: $_valid_to"
  4066. return 1
  4067. fi
  4068. else
  4069. _cleardomainconf "Le_Valid_To"
  4070. fi
  4071. _debug2 "_notAfter" "$_notAfter"
  4072. _newOrderObj="{\"identifiers\": [$_identifiers]"
  4073. if [ "$_notBefore" ]; then
  4074. _newOrderObj="$_newOrderObj,\"notBefore\": \"$_notBefore\""
  4075. fi
  4076. if [ "$_notAfter" ]; then
  4077. _newOrderObj="$_newOrderObj,\"notAfter\": \"$_notAfter\""
  4078. fi
  4079. if [ "$_certificate_profile" ]; then
  4080. _newOrderObj="$_newOrderObj,\"profile\": \"$_certificate_profile\""
  4081. fi
  4082. _debug "STEP 1, Ordering a Certificate"
  4083. if ! _send_signed_request "$ACME_NEW_ORDER" "$_newOrderObj}"; then
  4084. _err "Error creating new order."
  4085. _clearup
  4086. _on_issue_err "$_post_hook"
  4087. return 1
  4088. fi
  4089. if _contains "$response" "invalid"; then
  4090. if echo "$response" | _normalizeJson | grep '"status":"invalid"' >/dev/null 2>&1; then
  4091. _err "Create new order with invalid status."
  4092. _err "$response"
  4093. _clearup
  4094. _on_issue_err "$_post_hook"
  4095. return 1
  4096. fi
  4097. fi
  4098. Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n " | cut -d ":" -f 2-)"
  4099. _debug Le_LinkOrder "$Le_LinkOrder"
  4100. Le_OrderFinalize="$(echo "$response" | _egrep_o '"finalize" *: *"[^"]*"' | cut -d '"' -f 4)"
  4101. _debug Le_OrderFinalize "$Le_OrderFinalize"
  4102. if [ -z "$Le_OrderFinalize" ]; then
  4103. _err "Error creating new order. Le_OrderFinalize not found. $response"
  4104. _clearup
  4105. _on_issue_err "$_post_hook"
  4106. return 1
  4107. fi
  4108. #for dns manual mode
  4109. _savedomainconf "Le_OrderFinalize" "$Le_OrderFinalize"
  4110. _authorizations_seg="$(echo "$response" | _json_decode | _egrep_o '"authorizations" *: *\[[^\[]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
  4111. _debug2 _authorizations_seg "$_authorizations_seg"
  4112. if [ -z "$_authorizations_seg" ]; then
  4113. _err "_authorizations_seg not found."
  4114. _clearup
  4115. _on_issue_err "$_post_hook"
  4116. return 1
  4117. fi
  4118. _debug "STEP 2, Get the authorizations of each domain"
  4119. #domain and authz map
  4120. _authorizations_map=""
  4121. for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do
  4122. _debug2 "_authz_url" "$_authz_url"
  4123. if ! _send_signed_request "$_authz_url"; then
  4124. _err "Error getting authz."
  4125. _err "_authorizations_seg" "$_authorizations_seg"
  4126. _err "_authz_url" "$_authz_url"
  4127. _err "$response"
  4128. _clearup
  4129. _on_issue_err "$_post_hook"
  4130. return 1
  4131. fi
  4132. response="$(echo "$response" | _normalizeJson)"
  4133. _debug2 response "$response"
  4134. if echo "$response" | grep '"status":"invalid"' >/dev/null 2>&1; then
  4135. _err "get authz objec with invalid status, please try again later."
  4136. _err "_authorizations_seg" "$_authorizations_seg"
  4137. _err "$response"
  4138. _clearup
  4139. _on_issue_err "$_post_hook"
  4140. return 1
  4141. fi
  4142. _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2- | tr -d ' "')"
  4143. if _contains "$response" "\"wildcard\" *: *true"; then
  4144. _d="*.$_d"
  4145. fi
  4146. _debug2 _d "$_d"
  4147. _authorizations_map="$_d,$response#$_authz_url
  4148. $_authorizations_map"
  4149. done
  4150. _debug2 _authorizations_map "$_authorizations_map"
  4151. _index=0
  4152. _currentRoot=""
  4153. _w_index=1
  4154. while true; do
  4155. d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")"
  4156. _w_index="$(_math "$_w_index" + 1)"
  4157. _debug d "$d"
  4158. if [ -z "$d" ]; then
  4159. break
  4160. fi
  4161. _info "Getting webroot for domain" "$d"
  4162. _index=$(_math $_index + 1)
  4163. _w="$(echo $_web_roots | cut -d , -f $_index)"
  4164. _debug _w "$_w"
  4165. if [ "$_w" ]; then
  4166. _currentRoot="$_w"
  4167. fi
  4168. _debug "_currentRoot" "$_currentRoot"
  4169. vtype="$VTYPE_HTTP"
  4170. #todo, v2 wildcard force to use dns
  4171. if _startswith "$_currentRoot" "$W_DNS"; then
  4172. vtype="$VTYPE_DNS"
  4173. fi
  4174. if [ "$_currentRoot" = "$W_ALPN" ]; then
  4175. vtype="$VTYPE_ALPN"
  4176. fi
  4177. _idn_d="$(_idn "$d")"
  4178. _candidates="$(echo "$_authorizations_map" | grep -i "^$_idn_d,")"
  4179. _debug2 _candidates "$_candidates"
  4180. if [ "$(echo "$_candidates" | wc -l)" -gt 1 ]; then
  4181. for _can in $_candidates; do
  4182. if _startswith "$(echo "$_can" | tr '.' '|')" "$(echo "$_idn_d" | tr '.' '|'),"; then
  4183. _candidates="$_can"
  4184. break
  4185. fi
  4186. done
  4187. fi
  4188. response="$(echo "$_candidates" | sed "s/$_idn_d,//")"
  4189. _debug2 "response" "$response"
  4190. if [ -z "$response" ]; then
  4191. _err "Error getting authz."
  4192. _err "_authorizations_map" "$_authorizations_map"
  4193. _clearup
  4194. _on_issue_err "$_post_hook"
  4195. return 1
  4196. fi
  4197. _authz_url="$(echo "$_candidates" | sed "s/$_idn_d,//" | _egrep_o "#.*" | sed "s/^#//")"
  4198. _debug _authz_url "$_authz_url"
  4199. if [ -z "$thumbprint" ]; then
  4200. thumbprint="$(__calc_account_thumbprint)"
  4201. fi
  4202. keyauthorization=""
  4203. if echo "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
  4204. _debug "$d is already valid."
  4205. keyauthorization="$STATE_VERIFIED"
  4206. _debug keyauthorization "$keyauthorization"
  4207. fi
  4208. # Fix for empty error objects in response which mess up the original code, adapted from fix suggested here: https://github.com/acmesh-official/acme.sh/issues/4933#issuecomment-1870499018
  4209. entry="$(echo "$response" | sed s/'"error":{}'/'"error":null'/ | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
  4210. _debug entry "$entry"
  4211. if [ -z "$keyauthorization" -a -z "$entry" ]; then
  4212. _err "Cannot get domain token entry $d for $vtype"
  4213. _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')"
  4214. if [ "$_supported_vtypes" ]; then
  4215. _err "Supported validation types are: $_supported_vtypes, but you specified: $vtype"
  4216. fi
  4217. _clearup
  4218. _on_issue_err "$_post_hook"
  4219. return 1
  4220. fi
  4221. if [ -z "$keyauthorization" ]; then
  4222. token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  4223. _debug token "$token"
  4224. if [ -z "$token" ]; then
  4225. _err "Cannot get domain token $entry"
  4226. _clearup
  4227. _on_issue_err "$_post_hook"
  4228. return 1
  4229. fi
  4230. uri="$(echo "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)"
  4231. _debug uri "$uri"
  4232. if [ -z "$uri" ]; then
  4233. _err "Cannot get domain URI $entry"
  4234. _clearup
  4235. _on_issue_err "$_post_hook"
  4236. return 1
  4237. fi
  4238. keyauthorization="$token.$thumbprint"
  4239. _debug keyauthorization "$keyauthorization"
  4240. fi
  4241. dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot$sep$_authz_url"
  4242. _debug dvlist "$dvlist"
  4243. vlist="$vlist$dvlist$dvsep"
  4244. done
  4245. _debug vlist "$vlist"
  4246. #add entry
  4247. dns_entries=""
  4248. dnsadded=""
  4249. ventries=$(echo "$vlist" | tr "$dvsep" ' ')
  4250. _alias_index=1
  4251. for ventry in $ventries; do
  4252. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  4253. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  4254. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  4255. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  4256. _authz_url=$(echo "$ventry" | cut -d "$sep" -f 6)
  4257. _debug d "$d"
  4258. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  4259. _debug "$d has already been verified, skipping $vtype."
  4260. _alias_index="$(_math "$_alias_index" + 1)"
  4261. continue
  4262. fi
  4263. if [ "$vtype" = "$VTYPE_DNS" ]; then
  4264. dnsadded='0'
  4265. _dns_root_d="$d"
  4266. if _startswith "$_dns_root_d" "*."; then
  4267. _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')"
  4268. fi
  4269. _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")"
  4270. test "$_d_alias" = "$NO_VALUE" && _d_alias=""
  4271. _alias_index="$(_math "$_alias_index" + 1)"
  4272. _debug "_d_alias" "$_d_alias"
  4273. if [ "$_d_alias" ]; then
  4274. if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then
  4275. txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")"
  4276. else
  4277. txtdomain="_acme-challenge.$_d_alias"
  4278. fi
  4279. dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$txtdomain$dvsep$_currentRoot"
  4280. else
  4281. txtdomain="_acme-challenge.$_dns_root_d"
  4282. dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$dvsep$_currentRoot"
  4283. fi
  4284. _debug txtdomain "$txtdomain"
  4285. txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
  4286. _debug txt "$txt"
  4287. d_api="$(_findHook "$_dns_root_d" $_SUB_FOLDER_DNSAPI "$_currentRoot")"
  4288. _debug d_api "$d_api"
  4289. dns_entry="$dns_entry$dvsep$txt${dvsep}$d_api"
  4290. _debug2 dns_entry "$dns_entry"
  4291. if [ "$d_api" ]; then
  4292. _debug "Found domain API file: $d_api"
  4293. else
  4294. if [ "$_currentRoot" != "$W_DNS" ]; then
  4295. _err "Cannot find DNS API hook for: $_currentRoot"
  4296. _info "You need to add the TXT record manually."
  4297. fi
  4298. _info "$(__red "Add the following TXT record:")"
  4299. _info "$(__red "Domain: '$(__green "$txtdomain")'")"
  4300. _info "$(__red "TXT value: '$(__green "$txt")'")"
  4301. _info "$(__red "Please make sure to prepend '_acme-challenge.' to your domain")"
  4302. _info "$(__red "so that the resulting subdomain is: $txtdomain")"
  4303. continue
  4304. fi
  4305. (
  4306. if ! . "$d_api"; then
  4307. _err "Error loading file $d_api. Please check your API file and try again."
  4308. return 1
  4309. fi
  4310. addcommand="${_currentRoot}_add"
  4311. if ! _exists "$addcommand"; then
  4312. _err "It seems that your API file is incorrect. Make sure it has a function named: $addcommand"
  4313. return 1
  4314. fi
  4315. _info "Adding TXT value: $txt for domain: $txtdomain"
  4316. if ! $addcommand "$txtdomain" "$txt"; then
  4317. _err "Error adding TXT record to domain: $txtdomain"
  4318. return 1
  4319. fi
  4320. _info "The TXT record has been successfully added."
  4321. )
  4322. if [ "$?" != "0" ]; then
  4323. _on_issue_err "$_post_hook" "$vlist"
  4324. _clearup
  4325. return 1
  4326. fi
  4327. dns_entries="$dns_entries$dns_entry
  4328. "
  4329. _debug2 "$dns_entries"
  4330. dnsadded='1'
  4331. fi
  4332. done
  4333. if [ "$dnsadded" = '0' ]; then
  4334. _savedomainconf "Le_Vlist" "$vlist"
  4335. _debug "DNS record not yet added. Will save to $DOMAIN_CONF and exit."
  4336. _err "Please add the TXT records to the domains, and re-run with --renew."
  4337. _on_issue_err "$_post_hook"
  4338. _clearup
  4339. # If asked to be in manual DNS mode, flag this exit with a separate
  4340. # error so it can be distinguished from other failures.
  4341. return $CODE_DNS_MANUAL
  4342. fi
  4343. fi
  4344. if [ "$dns_entries" ]; then
  4345. if [ -z "$Le_DNSSleep" ]; then
  4346. _info "Let's check each DNS record now. Sleeping for 20 seconds first."
  4347. _sleep 20
  4348. if ! _check_dns_entries; then
  4349. _err "Error checking DNS."
  4350. _on_issue_err "$_post_hook"
  4351. _clearup
  4352. return 1
  4353. fi
  4354. else
  4355. _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
  4356. _info "Sleeping for $(__green $Le_DNSSleep) seconds to wait for the the TXT records to take effect"
  4357. _sleep "$Le_DNSSleep"
  4358. fi
  4359. fi
  4360. NGINX_RESTORE_VLIST=""
  4361. _debug "OK, let's start verification"
  4362. _ncIndex=1
  4363. ventries=$(echo "$vlist" | tr "$dvsep" ' ')
  4364. for ventry in $ventries; do
  4365. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  4366. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  4367. uri=$(echo "$ventry" | cut -d "$sep" -f 3)
  4368. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  4369. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  4370. _authz_url=$(echo "$ventry" | cut -d "$sep" -f 6)
  4371. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  4372. _info "$d is already verified, skipping $vtype."
  4373. continue
  4374. fi
  4375. _info "Verifying: $d"
  4376. _debug "d" "$d"
  4377. _debug "keyauthorization" "$keyauthorization"
  4378. _debug "uri" "$uri"
  4379. _debug "_authz_url" "$_authz_url"
  4380. removelevel=""
  4381. token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
  4382. _debug "_currentRoot" "$_currentRoot"
  4383. if [ "$vtype" = "$VTYPE_HTTP" ]; then
  4384. if [ "$_currentRoot" = "$NO_VALUE" ]; then
  4385. _info "Standalone mode server"
  4386. _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
  4387. _ncIndex="$(_math $_ncIndex + 1)"
  4388. _startserver "$keyauthorization" "$_ncaddr"
  4389. if [ "$?" != "0" ]; then
  4390. _clearup
  4391. _on_issue_err "$_post_hook" "$vlist"
  4392. return 1
  4393. fi
  4394. sleep 1
  4395. _debug serverproc "$serverproc"
  4396. elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
  4397. _info "Stateless mode for domain: $d"
  4398. _sleep 1
  4399. elif _startswith "$_currentRoot" "$NGINX"; then
  4400. _info "Nginx mode for domain: $d"
  4401. #set up nginx server
  4402. FOUND_REAL_NGINX_CONF=""
  4403. BACKUP_NGINX_CONF=""
  4404. if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then
  4405. _clearup
  4406. _on_issue_err "$_post_hook" "$vlist"
  4407. return 1
  4408. fi
  4409. if [ "$FOUND_REAL_NGINX_CONF" ]; then
  4410. _realConf="$FOUND_REAL_NGINX_CONF"
  4411. _backup="$BACKUP_NGINX_CONF"
  4412. _debug _realConf "$_realConf"
  4413. NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
  4414. fi
  4415. _sleep 1
  4416. else
  4417. if [ "$_currentRoot" = "apache" ]; then
  4418. wellknown_path="$ACME_DIR"
  4419. else
  4420. wellknown_path="$_currentRoot/.well-known/acme-challenge"
  4421. if [ ! -d "$_currentRoot/.well-known" ]; then
  4422. removelevel='1'
  4423. elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
  4424. removelevel='2'
  4425. else
  4426. removelevel='3'
  4427. fi
  4428. fi
  4429. _debug wellknown_path "$wellknown_path"
  4430. _debug "Writing token: $token to $wellknown_path/$token"
  4431. # Ensure .well-known is visible to web server user/group
  4432. # https://github.com/Neilpang/acme.sh/pull/32
  4433. if ! (umask ugo+rx &&
  4434. mkdir -p "$wellknown_path" &&
  4435. printf "%s" "$keyauthorization" >"$wellknown_path/$token"); then
  4436. _err "$d: Cannot write token to file: $wellknown_path/$token"
  4437. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  4438. _clearup
  4439. _on_issue_err "$_post_hook" "$vlist"
  4440. return 1
  4441. fi
  4442. if ! chmod a+r "$wellknown_path/$token"; then
  4443. _debug "chmod failed, will just continue."
  4444. fi
  4445. fi
  4446. elif [ "$vtype" = "$VTYPE_ALPN" ]; then
  4447. acmevalidationv1="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
  4448. _debug acmevalidationv1 "$acmevalidationv1"
  4449. if ! _starttlsserver "$d" "" "$Le_TLSPort" "$keyauthorization" "$_ncaddr" "$acmevalidationv1"; then
  4450. _err "Error starting TLS server."
  4451. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  4452. _clearup
  4453. _on_issue_err "$_post_hook" "$vlist"
  4454. return 1
  4455. fi
  4456. fi
  4457. if ! __trigger_validation "$uri" "$keyauthorization" "$vtype"; then
  4458. _err "$d: Cannot get challenge: $response"
  4459. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  4460. _clearup
  4461. _on_issue_err "$_post_hook" "$vlist"
  4462. return 1
  4463. fi
  4464. if [ "$code" ] && [ "$code" != '202' ]; then
  4465. if [ "$code" = '200' ]; then
  4466. _debug "Trigger validation code: $code"
  4467. else
  4468. _err "$d: Challenge error: $response"
  4469. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  4470. _clearup
  4471. _on_issue_err "$_post_hook" "$vlist"
  4472. return 1
  4473. fi
  4474. fi
  4475. waittimes=0
  4476. if [ -z "$MAX_RETRY_TIMES" ]; then
  4477. MAX_RETRY_TIMES=30
  4478. fi
  4479. _debug "Let's check the authz status"
  4480. while true; do
  4481. waittimes=$(_math "$waittimes" + 1)
  4482. if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
  4483. _err "$d: Timeout"
  4484. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  4485. _clearup
  4486. _on_issue_err "$_post_hook" "$vlist"
  4487. return 1
  4488. fi
  4489. _debug2 original "$response"
  4490. response="$(echo "$response" | _normalizeJson)"
  4491. _debug2 response "$response"
  4492. status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
  4493. _debug2 status "$status"
  4494. if _contains "$status" "invalid"; then
  4495. error="$(echo "$response" | _egrep_o '"error":\{[^\}]*')"
  4496. _debug2 error "$error"
  4497. errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
  4498. _debug2 errordetail "$errordetail"
  4499. if [ "$errordetail" ]; then
  4500. _err "$d: Invalid status. Verification error details: $errordetail"
  4501. else
  4502. _err "$d: Invalid status, Verification error: $error"
  4503. fi
  4504. if [ "$DEBUG" ]; then
  4505. if [ "$vtype" = "$VTYPE_HTTP" ]; then
  4506. _debug "Debug: GET token URL."
  4507. _get "http://$d/.well-known/acme-challenge/$token" "" 1
  4508. fi
  4509. fi
  4510. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  4511. _clearup
  4512. _on_issue_err "$_post_hook" "$vlist"
  4513. return 1
  4514. fi
  4515. if _contains "$status" "valid"; then
  4516. _info "$(__green Success)"
  4517. _stopserver "$serverproc"
  4518. serverproc=""
  4519. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  4520. break
  4521. fi
  4522. if _contains "$status" "pending"; then
  4523. _info "Pending. The CA is processing your order, please wait. ($waittimes/$MAX_RETRY_TIMES)"
  4524. elif _contains "$status" "processing"; then
  4525. _info "Processing. The CA is processing your order, please wait. ($waittimes/$MAX_RETRY_TIMES)"
  4526. else
  4527. _err "$d: Unknown status: $status. Verification error: $response"
  4528. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  4529. _clearup
  4530. _on_issue_err "$_post_hook" "$vlist"
  4531. return 1
  4532. fi
  4533. _debug "Sleep 2 seconds before verifying again"
  4534. _sleep 2
  4535. _debug "Checking"
  4536. _send_signed_request "$_authz_url"
  4537. if [ "$?" != "0" ]; then
  4538. _err "$d: Invalid code. Verification error: $response"
  4539. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  4540. _clearup
  4541. _on_issue_err "$_post_hook" "$vlist"
  4542. return 1
  4543. fi
  4544. _retryafter=$(echo "$responseHeaders" | grep -i "^Retry-After *: *[0-9]\+ *" | cut -d : -f 2 | tr -d ' ' | tr -d '\r')
  4545. _sleep_overload_retry_sec=$_retryafter
  4546. if [ "$_sleep_overload_retry_sec" ]; then
  4547. if [ $_sleep_overload_retry_sec -le 600 ]; then
  4548. _sleep $_sleep_overload_retry_sec
  4549. else
  4550. _info "The retryafter=$_retryafter value is too large (> 600), will not retry anymore."
  4551. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  4552. _clearup
  4553. _on_issue_err "$_post_hook" "$vlist"
  4554. return 1
  4555. fi
  4556. fi
  4557. done
  4558. done
  4559. _clearup
  4560. _info "Verification finished, beginning signing."
  4561. der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
  4562. _info "Let's finalize the order."
  4563. _info "Le_OrderFinalize" "$Le_OrderFinalize"
  4564. if ! _send_signed_request "${Le_OrderFinalize}" "{\"csr\": \"$der\"}"; then
  4565. _err "Signing failed."
  4566. _on_issue_err "$_post_hook"
  4567. return 1
  4568. fi
  4569. if [ "$code" != "200" ]; then
  4570. _err "Signing failed. Finalize code was not 200."
  4571. _err "$response"
  4572. _on_issue_err "$_post_hook"
  4573. return 1
  4574. fi
  4575. if [ -z "$Le_LinkOrder" ]; then
  4576. Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n \t" | cut -d ":" -f 2-)"
  4577. fi
  4578. _savedomainconf "Le_LinkOrder" "$Le_LinkOrder"
  4579. _link_cert_retry=0
  4580. _MAX_CERT_RETRY=30
  4581. while [ "$_link_cert_retry" -lt "$_MAX_CERT_RETRY" ]; do
  4582. if _contains "$response" "\"status\":\"valid\""; then
  4583. _debug "Order status is valid."
  4584. Le_LinkCert="$(echo "$response" | _egrep_o '"certificate" *: *"[^"]*"' | cut -d '"' -f 4)"
  4585. _debug Le_LinkCert "$Le_LinkCert"
  4586. if [ -z "$Le_LinkCert" ]; then
  4587. _err "A signing error occurred: could not find Le_LinkCert"
  4588. _err "$response"
  4589. _on_issue_err "$_post_hook"
  4590. return 1
  4591. fi
  4592. break
  4593. elif _contains "$response" "\"processing\""; then
  4594. _info "Order status is 'processing', let's sleep and retry."
  4595. _retryafter=$(echo "$responseHeaders" | grep -i "^Retry-After *:" | cut -d : -f 2 | tr -d ' ' | tr -d '\r')
  4596. _debug "_retryafter" "$_retryafter"
  4597. if [ "$_retryafter" ]; then
  4598. _info "Sleeping for $_retryafter seconds then retrying"
  4599. _sleep $_retryafter
  4600. else
  4601. _sleep 2
  4602. fi
  4603. else
  4604. _err "Signing error: wrong status"
  4605. _err "$response"
  4606. _on_issue_err "$_post_hook"
  4607. return 1
  4608. fi
  4609. #the order is processing, so we are going to poll order status
  4610. if [ -z "$Le_LinkOrder" ]; then
  4611. _err "Signing error: could not get order link location header"
  4612. _err "responseHeaders" "$responseHeaders"
  4613. _on_issue_err "$_post_hook"
  4614. return 1
  4615. fi
  4616. _info "Polling order status: $Le_LinkOrder"
  4617. if ! _send_signed_request "$Le_LinkOrder"; then
  4618. _err "Signing failed. Could not make POST request to Le_LinkOrder for cert: $Le_LinkOrder."
  4619. _err "$response"
  4620. _on_issue_err "$_post_hook"
  4621. return 1
  4622. fi
  4623. _link_cert_retry="$(_math $_link_cert_retry + 1)"
  4624. done
  4625. if [ -z "$Le_LinkCert" ]; then
  4626. _err "Signing failed. Could not get Le_LinkCert, and stopped retrying after reaching the retry limit."
  4627. _err "$response"
  4628. _on_issue_err "$_post_hook"
  4629. return 1
  4630. fi
  4631. _info "Downloading cert."
  4632. _info "Le_LinkCert" "$Le_LinkCert"
  4633. if ! _send_signed_request "$Le_LinkCert"; then
  4634. _err "Signing failed. Could not download cert: $Le_LinkCert."
  4635. _err "$response"
  4636. _on_issue_err "$_post_hook"
  4637. return 1
  4638. fi
  4639. echo "$response" >"$CERT_PATH"
  4640. _split_cert_chain "$CERT_PATH" "$CERT_FULLCHAIN_PATH" "$CA_CERT_PATH"
  4641. if [ -z "$_preferred_chain" ]; then
  4642. _preferred_chain=$(_readcaconf DEFAULT_PREFERRED_CHAIN)
  4643. fi
  4644. if [ "$_preferred_chain" ] && [ -f "$CERT_FULLCHAIN_PATH" ]; then
  4645. if [ "$DEBUG" ]; then
  4646. _debug "Default chain issuers: " "$(_get_chain_issuers "$CERT_FULLCHAIN_PATH")"
  4647. fi
  4648. if ! _match_issuer "$CERT_FULLCHAIN_PATH" "$_preferred_chain"; then
  4649. rels="$(echo "$responseHeaders" | tr -d ' <>' | grep -i "^link:" | grep -i 'rel="alternate"' | cut -d : -f 2- | cut -d ';' -f 1)"
  4650. _debug2 "rels" "$rels"
  4651. for rel in $rels; do
  4652. _info "Trying rel: $rel"
  4653. if ! _send_signed_request "$rel"; then
  4654. _err "Signing failed, could not download cert: $rel"
  4655. _err "$response"
  4656. continue
  4657. fi
  4658. _relcert="$CERT_PATH.alt"
  4659. _relfullchain="$CERT_FULLCHAIN_PATH.alt"
  4660. _relca="$CA_CERT_PATH.alt"
  4661. echo "$response" >"$_relcert"
  4662. _split_cert_chain "$_relcert" "$_relfullchain" "$_relca"
  4663. if [ "$DEBUG" ]; then
  4664. _debug "rel chain issuers: " "$(_get_chain_issuers "$_relfullchain")"
  4665. fi
  4666. if _match_issuer "$_relfullchain" "$_preferred_chain"; then
  4667. _info "Matched issuer in: $rel"
  4668. cat $_relcert >"$CERT_PATH"
  4669. cat $_relfullchain >"$CERT_FULLCHAIN_PATH"
  4670. cat $_relca >"$CA_CERT_PATH"
  4671. rm -f "$_relcert"
  4672. rm -f "$_relfullchain"
  4673. rm -f "$_relca"
  4674. break
  4675. fi
  4676. rm -f "$_relcert"
  4677. rm -f "$_relfullchain"
  4678. rm -f "$_relca"
  4679. done
  4680. fi
  4681. fi
  4682. _debug "Le_LinkCert" "$Le_LinkCert"
  4683. _savedomainconf "Le_LinkCert" "$Le_LinkCert"
  4684. if [ -z "$Le_LinkCert" ] || ! _checkcert "$CERT_PATH"; then
  4685. response="$(echo "$response" | _dbase64 "multiline" | tr -d '\0' | _normalizeJson)"
  4686. _err "Signing failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
  4687. _on_issue_err "$_post_hook"
  4688. return 1
  4689. fi
  4690. if [ "$Le_LinkCert" ]; then
  4691. _info "$(__green "Cert success.")"
  4692. cat "$CERT_PATH"
  4693. _info "Your cert is in: $(__green "$CERT_PATH")"
  4694. if [ -f "$CERT_KEY_PATH" ]; then
  4695. _info "Your cert key is in: $(__green "$CERT_KEY_PATH")"
  4696. fi
  4697. if [ ! "$USER_PATH" ] || [ ! "$_ACME_IN_CRON" ]; then
  4698. USER_PATH="$PATH"
  4699. _saveaccountconf "USER_PATH" "$USER_PATH"
  4700. fi
  4701. fi
  4702. [ -f "$CA_CERT_PATH" ] && _info "The intermediate CA cert is in: $(__green "$CA_CERT_PATH")"
  4703. [ -f "$CERT_FULLCHAIN_PATH" ] && _info "And the full-chain cert is in: $(__green "$CERT_FULLCHAIN_PATH")"
  4704. if [ "$Le_ForceNewDomainKey" ] && [ -e "$Le_Next_Domain_Key" ]; then
  4705. _info "Your pre-generated key for future cert key changes is in: $(__green "$Le_Next_Domain_Key")"
  4706. fi
  4707. Le_CertCreateTime=$(_time)
  4708. _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
  4709. Le_CertCreateTimeStr=$(_time2str "$Le_CertCreateTime")
  4710. _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
  4711. if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ]; then
  4712. Le_RenewalDays="$DEFAULT_RENEW"
  4713. else
  4714. _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
  4715. fi
  4716. if [ "$CA_BUNDLE" ]; then
  4717. _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
  4718. else
  4719. _clearaccountconf "CA_BUNDLE"
  4720. fi
  4721. if [ "$CA_PATH" ]; then
  4722. _saveaccountconf CA_PATH "$CA_PATH"
  4723. else
  4724. _clearaccountconf "CA_PATH"
  4725. fi
  4726. if [ "$HTTPS_INSECURE" ]; then
  4727. _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
  4728. else
  4729. _clearaccountconf "HTTPS_INSECURE"
  4730. fi
  4731. if [ "$Le_Listen_V4" ]; then
  4732. _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
  4733. _cleardomainconf Le_Listen_V6
  4734. elif [ "$Le_Listen_V6" ]; then
  4735. _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
  4736. _cleardomainconf Le_Listen_V4
  4737. fi
  4738. if [ "$Le_ForceNewDomainKey" = "1" ]; then
  4739. _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey"
  4740. else
  4741. _cleardomainconf Le_ForceNewDomainKey
  4742. fi
  4743. if [ "$_notAfter" ]; then
  4744. Le_NextRenewTime=$(_date2time "$_notAfter")
  4745. Le_NextRenewTimeStr="$_notAfter"
  4746. if [ "$_valid_to" ] && ! _startswith "$_valid_to" "+"; then
  4747. _info "The domain is set to be valid until: $_valid_to"
  4748. _info "It cannot be renewed automatically"
  4749. _info "See: $_VALIDITY_WIKI"
  4750. else
  4751. _now=$(_time)
  4752. _debug2 "_now" "$_now"
  4753. _lifetime=$(_math $Le_NextRenewTime - $_now)
  4754. _debug2 "_lifetime" "$_lifetime"
  4755. if [ $_lifetime -gt 86400 ]; then
  4756. #if lifetime is logner than one day, it will renew one day before
  4757. Le_NextRenewTime=$(_math $Le_NextRenewTime - 86400)
  4758. Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
  4759. else
  4760. #if lifetime is less than 24 hours, it will renew one hour before
  4761. Le_NextRenewTime=$(_math $Le_NextRenewTime - 3600)
  4762. Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
  4763. fi
  4764. fi
  4765. else
  4766. Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
  4767. Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
  4768. Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
  4769. fi
  4770. _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
  4771. _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
  4772. #convert to pkcs12
  4773. if [ "$Le_PFXPassword" ]; then
  4774. _toPkcs "$CERT_PFX_PATH" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$Le_PFXPassword"
  4775. fi
  4776. export CERT_PFX_PATH
  4777. if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
  4778. _savedomainconf "Le_RealCertPath" "$_real_cert"
  4779. _savedomainconf "Le_RealCACertPath" "$_real_ca"
  4780. _savedomainconf "Le_RealKeyPath" "$_real_key"
  4781. _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64"
  4782. _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
  4783. if ! _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"; then
  4784. return 1
  4785. fi
  4786. fi
  4787. if ! _on_issue_success "$_post_hook" "$_renew_hook"; then
  4788. _err "Error calling hook."
  4789. return 1
  4790. fi
  4791. }
  4792. #in_out_cert out_fullchain out_ca
  4793. _split_cert_chain() {
  4794. _certf="$1"
  4795. _fullchainf="$2"
  4796. _caf="$3"
  4797. if [ "$(grep -- "$BEGIN_CERT" "$_certf" | wc -l)" -gt "1" ]; then
  4798. _debug "Found cert chain"
  4799. cat "$_certf" >"$_fullchainf"
  4800. _end_n="$(grep -n -- "$END_CERT" "$_fullchainf" | _head_n 1 | cut -d : -f 1)"
  4801. _debug _end_n "$_end_n"
  4802. sed -n "1,${_end_n}p" "$_fullchainf" >"$_certf"
  4803. _end_n="$(_math $_end_n + 1)"
  4804. sed -n "${_end_n},9999p" "$_fullchainf" >"$_caf"
  4805. fi
  4806. }
  4807. #domain [isEcc] [server]
  4808. renew() {
  4809. Le_Domain="$1"
  4810. if [ -z "$Le_Domain" ]; then
  4811. _usage "Usage: $PROJECT_ENTRY --renew --domain <domain.tld> [--ecc] [--server server]"
  4812. return 1
  4813. fi
  4814. _isEcc="$2"
  4815. _renewServer="$3"
  4816. _debug "_renewServer" "$_renewServer"
  4817. _initpath "$Le_Domain" "$_isEcc"
  4818. _set_level=${NOTIFY_LEVEL:-$NOTIFY_LEVEL_DEFAULT}
  4819. _info "$(__green "Renewing: '$Le_Domain'")"
  4820. if [ ! -f "$DOMAIN_CONF" ]; then
  4821. _info "'$Le_Domain' is not an issued domain, skipping."
  4822. return $RENEW_SKIP
  4823. fi
  4824. if [ "$Le_RenewalDays" ]; then
  4825. _savedomainconf Le_RenewalDays "$Le_RenewalDays"
  4826. fi
  4827. . "$DOMAIN_CONF"
  4828. _debug Le_API "$Le_API"
  4829. case "$Le_API" in
  4830. "$CA_LETSENCRYPT_V2_TEST")
  4831. _info "Switching back to $CA_LETSENCRYPT_V2"
  4832. Le_API="$CA_LETSENCRYPT_V2"
  4833. ;;
  4834. "$CA_GOOGLE_TEST")
  4835. _info "Switching back to $CA_GOOGLE"
  4836. Le_API="$CA_GOOGLE"
  4837. ;;
  4838. esac
  4839. if [ "$_server" ]; then
  4840. Le_API="$_server"
  4841. fi
  4842. _info "Renewing using Le_API=$Le_API"
  4843. _clearAPI
  4844. _clearCA
  4845. export ACME_DIRECTORY="$Le_API"
  4846. #reload ca configs
  4847. _debug2 "initpath again."
  4848. _initpath "$Le_Domain" "$_isEcc"
  4849. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
  4850. _info "Skipping. Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
  4851. _info "Add '$(__red '--force')' to force renewal."
  4852. if [ -z "$_ACME_IN_RENEWALL" ]; then
  4853. if [ $_set_level -ge $NOTIFY_LEVEL_SKIP ]; then
  4854. _send_notify "Renew $Le_Domain skipped" "Good, the cert is skipped." "$NOTIFY_HOOK" "$RENEW_SKIP"
  4855. fi
  4856. fi
  4857. return "$RENEW_SKIP"
  4858. fi
  4859. if [ "$_ACME_IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then
  4860. _info "Skipping invalid cert for: $Le_Domain"
  4861. return $RENEW_SKIP
  4862. fi
  4863. _ACME_IS_RENEW="1"
  4864. Le_ReloadCmd="$(_readdomainconf Le_ReloadCmd)"
  4865. Le_PreHook="$(_readdomainconf Le_PreHook)"
  4866. Le_PostHook="$(_readdomainconf Le_PostHook)"
  4867. Le_RenewHook="$(_readdomainconf Le_RenewHook)"
  4868. Le_Preferred_Chain="$(_readdomainconf Le_Preferred_Chain)"
  4869. Le_Certificate_Profile="$(_readdomainconf Le_Certificate_Profile)"
  4870. # When renewing from an old version, the empty Le_Keylength means 2048.
  4871. # Note, do not use DEFAULT_DOMAIN_KEY_LENGTH as that value may change over
  4872. # time but an empty value implies 2048 specifically.
  4873. Le_Keylength="$(_readdomainconf Le_Keylength)"
  4874. if [ -z "$Le_Keylength" ]; then
  4875. Le_Keylength=2048
  4876. fi
  4877. if [ "$CA_LETSENCRYPT_V2" = "$Le_API" ]; then
  4878. #letsencrypt doesn't support ocsp anymore
  4879. if [ "$Le_OCSP_Staple" ]; then
  4880. export Le_OCSP_Staple=""
  4881. _cleardomainconf Le_OCSP_Staple
  4882. fi
  4883. fi
  4884. 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" "$Le_Preferred_Chain" "$Le_Valid_From" "$Le_Valid_To" "$Le_Certificate_Profile"
  4885. res="$?"
  4886. if [ "$res" != "0" ]; then
  4887. return "$res"
  4888. fi
  4889. if [ "$Le_DeployHook" ]; then
  4890. _deploy "$Le_Domain" "$Le_DeployHook"
  4891. res="$?"
  4892. fi
  4893. _ACME_IS_RENEW=""
  4894. if [ -z "$_ACME_IN_RENEWALL" ]; then
  4895. if [ "$res" = "0" ]; then
  4896. if [ $_set_level -ge $NOTIFY_LEVEL_RENEW ]; then
  4897. _send_notify "Renew $d success" "Good, the cert is renewed." "$NOTIFY_HOOK" 0
  4898. fi
  4899. else
  4900. if [ $_set_level -ge $NOTIFY_LEVEL_ERROR ]; then
  4901. _send_notify "Renew $d error" "There is an error." "$NOTIFY_HOOK" 1
  4902. fi
  4903. fi
  4904. fi
  4905. return "$res"
  4906. }
  4907. #renewAll [stopRenewOnError] [server]
  4908. renewAll() {
  4909. _initpath
  4910. _clearCA
  4911. _stopRenewOnError="$1"
  4912. _debug "_stopRenewOnError" "$_stopRenewOnError"
  4913. _server="$2"
  4914. _debug "_server" "$_server"
  4915. _ret="0"
  4916. _success_msg=""
  4917. _error_msg=""
  4918. _skipped_msg=""
  4919. _error_level=$NOTIFY_LEVEL_SKIP
  4920. _notify_code=$RENEW_SKIP
  4921. _set_level=${NOTIFY_LEVEL:-$NOTIFY_LEVEL_DEFAULT}
  4922. _debug "_set_level" "$_set_level"
  4923. export _ACME_IN_RENEWALL=1
  4924. for di in "${CERT_HOME}"/*.*/; do
  4925. _debug di "$di"
  4926. if ! [ -d "$di" ]; then
  4927. _debug "Not a directory, skipping: $di"
  4928. continue
  4929. fi
  4930. d=$(basename "$di")
  4931. _debug d "$d"
  4932. (
  4933. if _endswith "$d" "$ECC_SUFFIX"; then
  4934. _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
  4935. d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
  4936. fi
  4937. renew "$d" "$_isEcc" "$_server"
  4938. )
  4939. rc="$?"
  4940. _debug "Return code: $rc"
  4941. if [ "$rc" = "0" ]; then
  4942. if [ $_error_level -gt $NOTIFY_LEVEL_RENEW ]; then
  4943. _error_level="$NOTIFY_LEVEL_RENEW"
  4944. _notify_code=0
  4945. fi
  4946. if [ $_set_level -ge $NOTIFY_LEVEL_RENEW ]; then
  4947. if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
  4948. _send_notify "Renew $d success" "Good, the cert is renewed." "$NOTIFY_HOOK" 0
  4949. fi
  4950. fi
  4951. _success_msg="${_success_msg} $d
  4952. "
  4953. elif [ "$rc" = "$RENEW_SKIP" ]; then
  4954. if [ $_error_level -gt $NOTIFY_LEVEL_SKIP ]; then
  4955. _error_level="$NOTIFY_LEVEL_SKIP"
  4956. _notify_code=$RENEW_SKIP
  4957. fi
  4958. if [ $_set_level -ge $NOTIFY_LEVEL_SKIP ]; then
  4959. if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
  4960. _send_notify "Renew $d skipped" "Good, the cert is skipped." "$NOTIFY_HOOK" "$RENEW_SKIP"
  4961. fi
  4962. fi
  4963. _info "Skipped $d"
  4964. _skipped_msg="${_skipped_msg} $d
  4965. "
  4966. else
  4967. if [ $_error_level -gt $NOTIFY_LEVEL_ERROR ]; then
  4968. _error_level="$NOTIFY_LEVEL_ERROR"
  4969. _notify_code=1
  4970. fi
  4971. if [ $_set_level -ge $NOTIFY_LEVEL_ERROR ]; then
  4972. if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then
  4973. _send_notify "Renew $d error" "There is an error." "$NOTIFY_HOOK" 1
  4974. fi
  4975. fi
  4976. _error_msg="${_error_msg} $d
  4977. "
  4978. if [ "$_stopRenewOnError" ]; then
  4979. _err "Error renewing $d, stopping."
  4980. _ret="$rc"
  4981. break
  4982. else
  4983. _ret="$rc"
  4984. _err "Error renewing $d."
  4985. fi
  4986. fi
  4987. done
  4988. _debug _error_level "$_error_level"
  4989. _debug _set_level "$_set_level"
  4990. if [ $_error_level -le $_set_level ]; then
  4991. if [ -z "$NOTIFY_MODE" ] || [ "$NOTIFY_MODE" = "$NOTIFY_MODE_BULK" ]; then
  4992. _msg_subject="Renew"
  4993. if [ "$_error_msg" ]; then
  4994. _msg_subject="${_msg_subject} Error"
  4995. _msg_data="Errored certs:
  4996. ${_error_msg}
  4997. "
  4998. fi
  4999. if [ "$_success_msg" ]; then
  5000. _msg_subject="${_msg_subject} Success"
  5001. _msg_data="${_msg_data}Successful certs:
  5002. ${_success_msg}
  5003. "
  5004. fi
  5005. if [ "$_skipped_msg" ]; then
  5006. _msg_subject="${_msg_subject} Skipped"
  5007. _msg_data="${_msg_data}Skipped certs:
  5008. ${_skipped_msg}
  5009. "
  5010. fi
  5011. _send_notify "$_msg_subject" "$_msg_data" "$NOTIFY_HOOK" "$_notify_code"
  5012. fi
  5013. fi
  5014. return "$_ret"
  5015. }
  5016. #csr webroot
  5017. signcsr() {
  5018. _csrfile="$1"
  5019. _csrW="$2"
  5020. if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
  5021. _usage "Usage: $PROJECT_ENTRY --sign-csr --csr <csr-file> --webroot <directory>"
  5022. return 1
  5023. fi
  5024. _real_cert="$3"
  5025. _real_key="$4"
  5026. _real_ca="$5"
  5027. _reload_cmd="$6"
  5028. _real_fullchain="$7"
  5029. _pre_hook="${8}"
  5030. _post_hook="${9}"
  5031. _renew_hook="${10}"
  5032. _local_addr="${11}"
  5033. _challenge_alias="${12}"
  5034. _preferred_chain="${13}"
  5035. _csrsubj=$(_readSubjectFromCSR "$_csrfile")
  5036. if [ "$?" != "0" ]; then
  5037. _err "Cannot read subject from CSR: $_csrfile"
  5038. return 1
  5039. fi
  5040. _debug _csrsubj "$_csrsubj"
  5041. if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then
  5042. _info "It seems that the subject $_csrsubj is not a valid domain name. Dropping it."
  5043. _csrsubj=""
  5044. fi
  5045. _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
  5046. if [ "$?" != "0" ]; then
  5047. _err "Cannot read domain list from CSR: $_csrfile"
  5048. return 1
  5049. fi
  5050. _debug "_csrdomainlist" "$_csrdomainlist"
  5051. if [ -z "$_csrsubj" ]; then
  5052. _csrsubj="$(_getfield "$_csrdomainlist" 1)"
  5053. _debug _csrsubj "$_csrsubj"
  5054. _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
  5055. _debug "_csrdomainlist" "$_csrdomainlist"
  5056. fi
  5057. if [ -z "$_csrsubj" ]; then
  5058. _err "Cannot read subject from CSR: $_csrfile"
  5059. return 1
  5060. fi
  5061. _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
  5062. if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
  5063. _err "Cannot read key length from CSR: $_csrfile"
  5064. return 1
  5065. fi
  5066. _initpath "$_csrsubj" "$_csrkeylength"
  5067. mkdir -p "$DOMAIN_PATH"
  5068. _info "Copying CSR to: $CSR_PATH"
  5069. cp "$_csrfile" "$CSR_PATH"
  5070. 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" "$_preferred_chain"
  5071. }
  5072. showcsr() {
  5073. _csrfile="$1"
  5074. _csrd="$2"
  5075. if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
  5076. _usage "Usage: $PROJECT_ENTRY --show-csr --csr <csr-file>"
  5077. return 1
  5078. fi
  5079. _initpath
  5080. _csrsubj=$(_readSubjectFromCSR "$_csrfile")
  5081. if [ "$?" != "0" ]; then
  5082. _err "Cannot read subject from CSR: $_csrfile"
  5083. return 1
  5084. fi
  5085. if [ -z "$_csrsubj" ]; then
  5086. _info "The subject is empty"
  5087. fi
  5088. _info "Subject=$_csrsubj"
  5089. _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
  5090. if [ "$?" != "0" ]; then
  5091. _err "Cannot read domain list from CSR: $_csrfile"
  5092. return 1
  5093. fi
  5094. _debug "_csrdomainlist" "$_csrdomainlist"
  5095. _info "SubjectAltNames=$_csrdomainlist"
  5096. _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
  5097. if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
  5098. _err "Cannot read key length from CSR: $_csrfile"
  5099. return 1
  5100. fi
  5101. _info "KeyLength=$_csrkeylength"
  5102. }
  5103. #listraw domain
  5104. list() {
  5105. _raw="$1"
  5106. _domain="$2"
  5107. _initpath
  5108. _sep="|"
  5109. if [ "$_raw" ]; then
  5110. if [ -z "$_domain" ]; then
  5111. printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Profile${_sep}CA${_sep}Created${_sep}Renew"
  5112. fi
  5113. for di in "${CERT_HOME}"/*.*/; do
  5114. d=$(basename "$di")
  5115. _debug d "$d"
  5116. (
  5117. if _endswith "$d" "$ECC_SUFFIX"; then
  5118. _isEcc="ecc"
  5119. d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
  5120. fi
  5121. DOMAIN_CONF="$di/$d.conf"
  5122. if [ -f "$DOMAIN_CONF" ]; then
  5123. . "$DOMAIN_CONF"
  5124. _ca="$(_getCAShortName "$Le_API")"
  5125. if [ -z "$_domain" ]; then
  5126. printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_Certificate_Profile${_sep}$_ca${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
  5127. else
  5128. if [ "$_domain" = "$d" ]; then
  5129. cat "$DOMAIN_CONF"
  5130. fi
  5131. fi
  5132. fi
  5133. )
  5134. done
  5135. else
  5136. if _exists column; then
  5137. list "raw" "$_domain" | column -t -s "$_sep"
  5138. else
  5139. list "raw" "$_domain" | tr "$_sep" '\t'
  5140. fi
  5141. fi
  5142. }
  5143. list_profiles() {
  5144. _initpath
  5145. _initAPI
  5146. _l_server_url="$ACME_DIRECTORY"
  5147. _l_server_name="$(_getCAShortName "$_l_server_url")"
  5148. _info "Fetching profiles from $_l_server_name ($_l_server_url)..."
  5149. response=$(_get "$_l_server_url" "" 10)
  5150. if [ "$?" != "0" ]; then
  5151. _err "Failed to connect to CA directory: $_l_server_url"
  5152. return 1
  5153. fi
  5154. normalized_response=$(echo "$response" | _normalizeJson)
  5155. profiles_json=$(echo "$normalized_response" | _egrep_o '"profiles" *: *\{[^\}]*\}')
  5156. if [ -z "$profiles_json" ]; then
  5157. _info "The CA '$_l_server_name' does not publish certificate profiles via its directory endpoint."
  5158. return 0
  5159. fi
  5160. # Strip the outer layer to get the key-value pairs
  5161. profiles_kv=$(echo "$profiles_json" | sed 's/"profiles" *: *{//' | sed 's/}$//' | tr ',' '\n')
  5162. printf "\n%-15s %s\n" "name" "info"
  5163. printf -- "--------------------------------------------------------------------\n"
  5164. _old_IFS="$IFS"
  5165. IFS='
  5166. '
  5167. for pair in $profiles_kv; do
  5168. # Trim quotes and whitespace
  5169. _name=$(echo "$pair" | cut -d: -f1 | tr -d '" \t')
  5170. _info_url=$(echo "$pair" | cut -d: -f2- | sed 's/^ *//' | tr -d '"')
  5171. printf "%-15s %s\n" "$_name" "$_info_url"
  5172. done
  5173. IFS="$_old_IFS"
  5174. return 0
  5175. }
  5176. _deploy() {
  5177. _d="$1"
  5178. _hooks="$2"
  5179. for _d_api in $(echo "$_hooks" | tr ',' " "); do
  5180. _deployApi="$(_findHook "$_d" $_SUB_FOLDER_DEPLOY "$_d_api")"
  5181. if [ -z "$_deployApi" ]; then
  5182. _err "The deploy hook $_d_api was not found."
  5183. return 1
  5184. fi
  5185. _debug _deployApi "$_deployApi"
  5186. if ! (
  5187. if ! . "$_deployApi"; then
  5188. _err "Error loading file $_deployApi. Please check your API file and try again."
  5189. return 1
  5190. fi
  5191. d_command="${_d_api}_deploy"
  5192. if ! _exists "$d_command"; then
  5193. _err "It seems that your API file is not correct. Make sure it has a function named: $d_command"
  5194. return 1
  5195. fi
  5196. if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH" "$CERT_PFX_PATH"; then
  5197. _err "Error deploying for domain: $_d"
  5198. return 1
  5199. fi
  5200. ); then
  5201. _err "Error encountered while deploying."
  5202. return 1
  5203. else
  5204. _info "$(__green Success)"
  5205. fi
  5206. done
  5207. }
  5208. #domain hooks
  5209. deploy() {
  5210. _d="$1"
  5211. _hooks="$2"
  5212. _isEcc="$3"
  5213. if [ -z "$_hooks" ]; then
  5214. _usage "Usage: $PROJECT_ENTRY --deploy --domain <domain.tld> --deploy-hook <hookname> [--ecc] "
  5215. return 1
  5216. fi
  5217. _initpath "$_d" "$_isEcc"
  5218. if [ ! -d "$DOMAIN_PATH" ]; then
  5219. _err "The domain '$_d' is not a cert name. You must use the cert name to specify the cert to install."
  5220. _err "Cannot find path: '$DOMAIN_PATH'"
  5221. return 1
  5222. fi
  5223. _debug2 DOMAIN_CONF "$DOMAIN_CONF"
  5224. . "$DOMAIN_CONF"
  5225. _savedomainconf Le_DeployHook "$_hooks"
  5226. _deploy "$_d" "$_hooks"
  5227. }
  5228. installcert() {
  5229. _main_domain="$1"
  5230. if [ -z "$_main_domain" ]; then
  5231. _usage "Usage: $PROJECT_ENTRY --install-cert --domain <domain.tld> [--ecc] [--cert-file <file>] [--key-file <file>] [--ca-file <file>] [ --reloadcmd <command>] [--fullchain-file <file>]"
  5232. return 1
  5233. fi
  5234. _real_cert="$2"
  5235. _real_key="$3"
  5236. _real_ca="$4"
  5237. _reload_cmd="$5"
  5238. _real_fullchain="$6"
  5239. _isEcc="$7"
  5240. _initpath "$_main_domain" "$_isEcc"
  5241. if [ ! -d "$DOMAIN_PATH" ]; then
  5242. _err "The domain '$_main_domain' is not a cert name. You must use the cert name to specify the cert to install."
  5243. _err "Cannot find path: '$DOMAIN_PATH'"
  5244. return 1
  5245. fi
  5246. _savedomainconf "Le_RealCertPath" "$_real_cert"
  5247. _savedomainconf "Le_RealCACertPath" "$_real_ca"
  5248. _savedomainconf "Le_RealKeyPath" "$_real_key"
  5249. _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64"
  5250. _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
  5251. export Le_ForceNewDomainKey="$(_readdomainconf Le_ForceNewDomainKey)"
  5252. export Le_Next_Domain_Key
  5253. _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
  5254. }
  5255. #domain cert key ca fullchain reloadcmd backup-prefix
  5256. _installcert() {
  5257. _main_domain="$1"
  5258. _real_cert="$2"
  5259. _real_key="$3"
  5260. _real_ca="$4"
  5261. _real_fullchain="$5"
  5262. _reload_cmd="$6"
  5263. _backup_prefix="$7"
  5264. if [ "$_real_cert" = "$NO_VALUE" ]; then
  5265. _real_cert=""
  5266. fi
  5267. if [ "$_real_key" = "$NO_VALUE" ]; then
  5268. _real_key=""
  5269. fi
  5270. if [ "$_real_ca" = "$NO_VALUE" ]; then
  5271. _real_ca=""
  5272. fi
  5273. if [ "$_reload_cmd" = "$NO_VALUE" ]; then
  5274. _reload_cmd=""
  5275. fi
  5276. if [ "$_real_fullchain" = "$NO_VALUE" ]; then
  5277. _real_fullchain=""
  5278. fi
  5279. _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
  5280. mkdir -p "$_backup_path"
  5281. if [ "$_real_cert" ]; then
  5282. _info "Installing cert to: $_real_cert"
  5283. if [ -f "$_real_cert" ] && [ ! "$_ACME_IS_RENEW" ]; then
  5284. cp "$_real_cert" "$_backup_path/cert.bak"
  5285. fi
  5286. if [ "$CERT_PATH" != "$_real_cert" ]; then
  5287. cat "$CERT_PATH" >"$_real_cert" || return 1
  5288. fi
  5289. fi
  5290. if [ "$_real_ca" ]; then
  5291. _info "Installing CA to: $_real_ca"
  5292. if [ "$_real_ca" = "$_real_cert" ]; then
  5293. echo "" >>"$_real_ca"
  5294. cat "$CA_CERT_PATH" >>"$_real_ca" || return 1
  5295. else
  5296. if [ -f "$_real_ca" ] && [ ! "$_ACME_IS_RENEW" ]; then
  5297. cp "$_real_ca" "$_backup_path/ca.bak"
  5298. fi
  5299. if [ "$CA_CERT_PATH" != "$_real_ca" ]; then
  5300. cat "$CA_CERT_PATH" >"$_real_ca" || return 1
  5301. fi
  5302. fi
  5303. fi
  5304. if [ "$_real_key" ]; then
  5305. _info "Installing key to: $_real_key"
  5306. if [ -f "$_real_key" ] && [ ! "$_ACME_IS_RENEW" ]; then
  5307. cp "$_real_key" "$_backup_path/key.bak"
  5308. fi
  5309. if [ "$CERT_KEY_PATH" != "$_real_key" ]; then
  5310. if [ -f "$_real_key" ]; then
  5311. cat "$CERT_KEY_PATH" >"$_real_key" || return 1
  5312. else
  5313. touch "$_real_key" || return 1
  5314. chmod 600 "$_real_key"
  5315. cat "$CERT_KEY_PATH" >"$_real_key" || return 1
  5316. fi
  5317. fi
  5318. fi
  5319. if [ "$_real_fullchain" ]; then
  5320. _info "Installing full chain to: $_real_fullchain"
  5321. if [ -f "$_real_fullchain" ] && [ ! "$_ACME_IS_RENEW" ]; then
  5322. cp "$_real_fullchain" "$_backup_path/fullchain.bak"
  5323. fi
  5324. if [ "$_real_fullchain" != "$CERT_FULLCHAIN_PATH" ]; then
  5325. cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain" || return 1
  5326. fi
  5327. fi
  5328. if [ "$_reload_cmd" ]; then
  5329. _info "Running reload cmd: $_reload_cmd"
  5330. if (
  5331. export CERT_PATH
  5332. export CERT_KEY_PATH
  5333. export CA_CERT_PATH
  5334. export CERT_FULLCHAIN_PATH
  5335. export Le_Domain="$_main_domain"
  5336. export Le_ForceNewDomainKey
  5337. export Le_Next_Domain_Key
  5338. cd "$DOMAIN_PATH" && eval "$_reload_cmd"
  5339. ); then
  5340. _info "$(__green "Reload successful")"
  5341. else
  5342. _err "Reload error for: $_main_domain"
  5343. fi
  5344. fi
  5345. }
  5346. __read_password() {
  5347. unset _pp
  5348. prompt="Enter Password:"
  5349. while IFS= read -p "$prompt" -r -s -n 1 char; do
  5350. if [ "$char" = $'\0' ]; then
  5351. break
  5352. fi
  5353. prompt='*'
  5354. _pp="$_pp$char"
  5355. done
  5356. echo "$_pp"
  5357. }
  5358. _install_win_taskscheduler() {
  5359. _lesh="$1"
  5360. _centry="$2"
  5361. _randomminute="$3"
  5362. if ! _exists cygpath; then
  5363. _err "cygpath not found"
  5364. return 1
  5365. fi
  5366. if ! _exists schtasks; then
  5367. _err "schtasks.exe was not found, are you on Windows?"
  5368. return 1
  5369. fi
  5370. _winbash="$(cygpath -w $(which bash))"
  5371. _debug _winbash "$_winbash"
  5372. if [ -z "$_winbash" ]; then
  5373. _err "Cannot find bash path"
  5374. return 1
  5375. fi
  5376. _myname="$(whoami)"
  5377. _debug "_myname" "$_myname"
  5378. if [ -z "$_myname" ]; then
  5379. _err "Can not find own username"
  5380. return 1
  5381. fi
  5382. _debug "_lesh" "$_lesh"
  5383. _info "To install the scheduler task to your Windows account, you must input your Windows password."
  5384. _info "$PROJECT_NAME will not save your password."
  5385. _info "Please input your Windows password for: $(__green "$_myname")"
  5386. _password="$(__read_password)"
  5387. #SCHTASKS.exe '/create' '/SC' 'DAILY' '/TN' "$_WINDOWS_SCHEDULER_NAME" '/F' '/ST' "00:$_randomminute" '/RU' "$_myname" '/RP' "$_password" '/TR' "$_winbash -l -c '$_lesh --cron --home \"$LE_WORKING_DIR\" $_centry'" >/dev/null
  5388. echo SCHTASKS.exe '/create' '/SC' 'DAILY' '/TN' "$_WINDOWS_SCHEDULER_NAME" '/F' '/ST' "00:$_randomminute" '/RU' "$_myname" '/RP' "$_password" '/TR' "\"$_winbash -l -c '$_lesh --cron --home \"$LE_WORKING_DIR\" $_centry'\"" | cmd.exe >/dev/null
  5389. echo
  5390. }
  5391. _uninstall_win_taskscheduler() {
  5392. if ! _exists schtasks; then
  5393. _err "schtasks.exe was not found, are you on Windows?"
  5394. return 1
  5395. fi
  5396. if ! echo SCHTASKS /query /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null; then
  5397. _debug "scheduler $_WINDOWS_SCHEDULER_NAME was not found."
  5398. else
  5399. _info "Removing $_WINDOWS_SCHEDULER_NAME"
  5400. echo SCHTASKS /delete /f /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null
  5401. fi
  5402. }
  5403. #confighome
  5404. installcronjob() {
  5405. _c_home="$1"
  5406. _initpath
  5407. _CRONTAB="crontab"
  5408. if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
  5409. lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
  5410. else
  5411. _debug "_SCRIPT_" "$_SCRIPT_"
  5412. _script="$(_readlink "$_SCRIPT_")"
  5413. _debug _script "$_script"
  5414. if [ -f "$_script" ]; then
  5415. _info "Using the current script from: $_script"
  5416. lesh="$_script"
  5417. else
  5418. _err "Cannot install cronjob, $PROJECT_ENTRY not found."
  5419. return 1
  5420. fi
  5421. fi
  5422. if [ "$_c_home" ]; then
  5423. _c_entry="--config-home \"$_c_home\" "
  5424. fi
  5425. _t=$(_time)
  5426. random_minute=$(_math $_t % 60)
  5427. random_hour=$(_math $_t / 60 % 24)
  5428. if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
  5429. _CRONTAB="fcrontab"
  5430. fi
  5431. if ! _exists "$_CRONTAB"; then
  5432. if _exists cygpath && _exists schtasks.exe; then
  5433. _info "It seems you are on Windows, let's install the Windows scheduler task."
  5434. if _install_win_taskscheduler "$lesh" "$_c_entry" "$random_minute"; then
  5435. _info "Successfully installed Windows scheduler task."
  5436. return 0
  5437. else
  5438. _err "Failed to install Windows scheduler task."
  5439. return 1
  5440. fi
  5441. fi
  5442. _err "crontab/fcrontab doesn't exist, so we cannot install cron jobs."
  5443. _err "Your certs will not be renewed automatically."
  5444. _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' every day."
  5445. return 1
  5446. fi
  5447. _info "Installing cron job"
  5448. if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then
  5449. if _exists uname && uname -a | grep SunOS >/dev/null; then
  5450. _CRONTAB_STDIN="$_CRONTAB --"
  5451. else
  5452. _CRONTAB_STDIN="$_CRONTAB -"
  5453. fi
  5454. $_CRONTAB -l | {
  5455. cat
  5456. echo "$random_minute $random_hour * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
  5457. } | $_CRONTAB_STDIN
  5458. fi
  5459. if [ "$?" != "0" ]; then
  5460. _err "Failed to install cron job. You need to manually renew your certs."
  5461. _err "Alternatively, you can add a cron job by yourself:"
  5462. _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
  5463. return 1
  5464. fi
  5465. }
  5466. uninstallcronjob() {
  5467. _CRONTAB="crontab"
  5468. if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
  5469. _CRONTAB="fcrontab"
  5470. fi
  5471. if ! _exists "$_CRONTAB"; then
  5472. if _exists cygpath && _exists schtasks.exe; then
  5473. _info "It seems you are on Windows, let's uninstall the Windows scheduler task."
  5474. if _uninstall_win_taskscheduler; then
  5475. _info "Successfully uninstalled Windows scheduler task."
  5476. return 0
  5477. else
  5478. _err "Failed to uninstall Windows scheduler task."
  5479. return 1
  5480. fi
  5481. fi
  5482. return
  5483. fi
  5484. _info "Removing cron job"
  5485. cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")"
  5486. if [ "$cr" ]; then
  5487. if _exists uname && uname -a | grep SunOS >/dev/null; then
  5488. $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB --
  5489. else
  5490. $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -
  5491. fi
  5492. LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
  5493. _info LE_WORKING_DIR "$LE_WORKING_DIR"
  5494. if _contains "$cr" "--config-home"; then
  5495. LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
  5496. _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
  5497. fi
  5498. fi
  5499. _initpath
  5500. }
  5501. #domain isECC revokeReason
  5502. revoke() {
  5503. Le_Domain="$1"
  5504. if [ -z "$Le_Domain" ]; then
  5505. _usage "Usage: $PROJECT_ENTRY --revoke --domain <domain.tld> [--ecc]"
  5506. return 1
  5507. fi
  5508. _isEcc="$2"
  5509. _reason="$3"
  5510. if [ -z "$_reason" ]; then
  5511. _reason="0"
  5512. fi
  5513. _initpath "$Le_Domain" "$_isEcc"
  5514. if [ ! -f "$DOMAIN_CONF" ]; then
  5515. _err "$Le_Domain is not an issued domain, skipping."
  5516. return 1
  5517. fi
  5518. if [ ! -f "$CERT_PATH" ]; then
  5519. _err "Cert for $Le_Domain $CERT_PATH was not found, skipping."
  5520. return 1
  5521. fi
  5522. . "$DOMAIN_CONF"
  5523. _debug Le_API "$Le_API"
  5524. if [ "$Le_API" ]; then
  5525. if [ "$Le_API" != "$ACME_DIRECTORY" ]; then
  5526. _clearAPI
  5527. fi
  5528. export ACME_DIRECTORY="$Le_API"
  5529. #reload ca configs
  5530. ACCOUNT_KEY_PATH=""
  5531. ACCOUNT_JSON_PATH=""
  5532. CA_CONF=""
  5533. _debug3 "initpath again."
  5534. _initpath "$Le_Domain" "$_isEcc"
  5535. _initAPI
  5536. fi
  5537. cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
  5538. if [ -z "$cert" ]; then
  5539. _err "Cert for $Le_Domain is empty, skipping."
  5540. return 1
  5541. fi
  5542. _initAPI
  5543. data="{\"certificate\": \"$cert\",\"reason\":$_reason}"
  5544. uri="${ACME_REVOKE_CERT}"
  5545. _info "Trying account key first."
  5546. if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
  5547. if [ -z "$response" ]; then
  5548. _info "Successfully revoked."
  5549. rm -f "$CERT_PATH"
  5550. cat "$CERT_KEY_PATH" >"$CERT_KEY_PATH.revoked"
  5551. cat "$CSR_PATH" >"$CSR_PATH.revoked"
  5552. return 0
  5553. else
  5554. _err "Error revoking."
  5555. _debug "$response"
  5556. fi
  5557. fi
  5558. if [ -f "$CERT_KEY_PATH" ]; then
  5559. _info "Trying domain key."
  5560. if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
  5561. if [ -z "$response" ]; then
  5562. _info "Successfully revoked."
  5563. rm -f "$CERT_PATH"
  5564. cat "$CERT_KEY_PATH" >"$CERT_KEY_PATH.revoked"
  5565. cat "$CSR_PATH" >"$CSR_PATH.revoked"
  5566. return 0
  5567. else
  5568. _err "Error revoking using domain key."
  5569. _err "$response"
  5570. fi
  5571. fi
  5572. else
  5573. _info "Domain key file doesn't exist."
  5574. fi
  5575. return 1
  5576. }
  5577. #domain ecc
  5578. remove() {
  5579. Le_Domain="$1"
  5580. if [ -z "$Le_Domain" ]; then
  5581. _usage "Usage: $PROJECT_ENTRY --remove --domain <domain.tld> [--ecc]"
  5582. return 1
  5583. fi
  5584. _isEcc="$2"
  5585. _initpath "$Le_Domain" "$_isEcc"
  5586. _removed_conf="$DOMAIN_CONF.removed"
  5587. if [ ! -f "$DOMAIN_CONF" ]; then
  5588. if [ -f "$_removed_conf" ]; then
  5589. _err "$Le_Domain has already been removed. You can remove the folder by yourself: $DOMAIN_PATH"
  5590. else
  5591. _err "$Le_Domain is not an issued domain, skipping."
  5592. fi
  5593. return 1
  5594. fi
  5595. if mv "$DOMAIN_CONF" "$_removed_conf"; then
  5596. _info "$Le_Domain has been removed. The key and cert files are in $(__green $DOMAIN_PATH)"
  5597. _info "You can remove them by yourself."
  5598. return 0
  5599. else
  5600. _err "Failed to remove $Le_Domain."
  5601. return 1
  5602. fi
  5603. }
  5604. #domain vtype
  5605. _deactivate() {
  5606. _d_domain="$1"
  5607. _d_type="$2"
  5608. _initpath "$_d_domain" "$_d_type"
  5609. . "$DOMAIN_CONF"
  5610. _debug Le_API "$Le_API"
  5611. if [ "$Le_API" ]; then
  5612. if [ "$Le_API" != "$ACME_DIRECTORY" ]; then
  5613. _clearAPI
  5614. fi
  5615. export ACME_DIRECTORY="$Le_API"
  5616. #reload ca configs
  5617. ACCOUNT_KEY_PATH=""
  5618. ACCOUNT_JSON_PATH=""
  5619. CA_CONF=""
  5620. _debug3 "initpath again."
  5621. _initpath "$Le_Domain" "$_d_type"
  5622. _initAPI
  5623. fi
  5624. _identifiers="{\"type\":\"$(_getIdType "$_d_domain")\",\"value\":\"$_d_domain\"}"
  5625. if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then
  5626. _err "Cannot get new order for domain."
  5627. return 1
  5628. fi
  5629. _authorizations_seg="$(echo "$response" | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')"
  5630. _debug2 _authorizations_seg "$_authorizations_seg"
  5631. if [ -z "$_authorizations_seg" ]; then
  5632. _err "_authorizations_seg not found."
  5633. _clearup
  5634. _on_issue_err "$_post_hook"
  5635. return 1
  5636. fi
  5637. authzUri="$_authorizations_seg"
  5638. _debug2 "authzUri" "$authzUri"
  5639. if ! _send_signed_request "$authzUri"; then
  5640. _err "Error making GET request for authz."
  5641. _err "_authorizations_seg" "$_authorizations_seg"
  5642. _err "authzUri" "$authzUri"
  5643. _clearup
  5644. _on_issue_err "$_post_hook"
  5645. return 1
  5646. fi
  5647. response="$(echo "$response" | _normalizeJson)"
  5648. _debug2 response "$response"
  5649. _URL_NAME="url"
  5650. entries="$(echo "$response" | tr '][' '==' | _egrep_o "challenges\": *=[^=]*=" | tr '}{' '\n\n' | grep "\"status\": *\"valid\"")"
  5651. if [ -z "$entries" ]; then
  5652. _info "No valid entries found."
  5653. if [ -z "$thumbprint" ]; then
  5654. thumbprint="$(__calc_account_thumbprint)"
  5655. fi
  5656. _debug "Trigger validation."
  5657. vtype="$(_getIdType "$_d_domain")"
  5658. # Fix for empty error objects in response which mess up the original code, adapted from fix suggested here: https://github.com/acmesh-official/acme.sh/issues/4933#issuecomment-1870499018
  5659. entry="$(echo "$response" | sed s/'"error":{}'/'"error":null'/ | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
  5660. _debug entry "$entry"
  5661. if [ -z "$entry" ]; then
  5662. _err "$d: Cannot get domain token"
  5663. return 1
  5664. fi
  5665. token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  5666. _debug token "$token"
  5667. uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')"
  5668. _debug uri "$uri"
  5669. keyauthorization="$token.$thumbprint"
  5670. _debug keyauthorization "$keyauthorization"
  5671. __trigger_validation "$uri" "$keyauthorization"
  5672. fi
  5673. _d_i=0
  5674. _d_max_retry=$(echo "$entries" | wc -l)
  5675. while [ "$_d_i" -lt "$_d_max_retry" ]; do
  5676. _info "Deactivating $_d_domain"
  5677. _d_i="$(_math $_d_i + 1)"
  5678. entry="$(echo "$entries" | sed -n "${_d_i}p")"
  5679. _debug entry "$entry"
  5680. if [ -z "$entry" ]; then
  5681. _info "No more valid entries found."
  5682. break
  5683. fi
  5684. _vtype="$(echo "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
  5685. _debug _vtype "$_vtype"
  5686. _info "Found $_vtype"
  5687. uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*\"" | tr -d '" ' | cut -d : -f 2-)"
  5688. _debug uri "$uri"
  5689. if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
  5690. _info "Skipping $_vtype"
  5691. continue
  5692. fi
  5693. _info "Deactivating $_vtype"
  5694. _djson="{\"status\":\"deactivated\"}"
  5695. if _send_signed_request "$authzUri" "$_djson" && _contains "$response" '"deactivated"'; then
  5696. _info "Successfully deactivated $_vtype."
  5697. else
  5698. _err "Could not deactivate $_vtype."
  5699. break
  5700. fi
  5701. done
  5702. _debug "$_d_i"
  5703. if [ "$_d_i" -eq "$_d_max_retry" ]; then
  5704. _info "Successfully deactivated!"
  5705. else
  5706. _err "Deactivation failed."
  5707. fi
  5708. }
  5709. deactivate() {
  5710. _d_domain_list="$1"
  5711. _d_type="$2"
  5712. _initpath
  5713. _initAPI
  5714. _debug _d_domain_list "$_d_domain_list"
  5715. if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
  5716. _usage "Usage: $PROJECT_ENTRY --deactivate --domain <domain.tld> [--domain <domain2.tld> ...]"
  5717. return 1
  5718. fi
  5719. for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
  5720. if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
  5721. continue
  5722. fi
  5723. if ! _deactivate "$_d_dm" "$_d_type"; then
  5724. return 1
  5725. fi
  5726. done
  5727. }
  5728. # Detect profile file if not specified as environment variable
  5729. _detect_profile() {
  5730. if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
  5731. echo "$PROFILE"
  5732. return
  5733. fi
  5734. DETECTED_PROFILE=''
  5735. SHELLTYPE="$(basename "/$SHELL")"
  5736. if [ "$SHELLTYPE" = "bash" ]; then
  5737. if [ -f "$HOME/.bashrc" ]; then
  5738. DETECTED_PROFILE="$HOME/.bashrc"
  5739. elif [ -f "$HOME/.bash_profile" ]; then
  5740. DETECTED_PROFILE="$HOME/.bash_profile"
  5741. fi
  5742. elif [ "$SHELLTYPE" = "zsh" ]; then
  5743. DETECTED_PROFILE="$HOME/.zshrc"
  5744. fi
  5745. if [ -z "$DETECTED_PROFILE" ]; then
  5746. if [ -f "$HOME/.profile" ]; then
  5747. DETECTED_PROFILE="$HOME/.profile"
  5748. elif [ -f "$HOME/.bashrc" ]; then
  5749. DETECTED_PROFILE="$HOME/.bashrc"
  5750. elif [ -f "$HOME/.bash_profile" ]; then
  5751. DETECTED_PROFILE="$HOME/.bash_profile"
  5752. elif [ -f "$HOME/.zshrc" ]; then
  5753. DETECTED_PROFILE="$HOME/.zshrc"
  5754. fi
  5755. fi
  5756. echo "$DETECTED_PROFILE"
  5757. }
  5758. _initconf() {
  5759. _initpath
  5760. if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
  5761. echo "
  5762. #LOG_FILE=\"$DEFAULT_LOG_FILE\"
  5763. #LOG_LEVEL=1
  5764. #AUTO_UPGRADE=\"1\"
  5765. #NO_TIMESTAMP=1
  5766. " >"$ACCOUNT_CONF_PATH"
  5767. fi
  5768. }
  5769. # nocron
  5770. _precheck() {
  5771. _nocron="$1"
  5772. if ! _exists "curl" && ! _exists "wget"; then
  5773. _err "Please install curl or wget first to enable access to HTTP resources."
  5774. return 1
  5775. fi
  5776. if [ -z "$_nocron" ]; then
  5777. if ! _exists "crontab" && ! _exists "fcrontab"; then
  5778. if _exists cygpath && _exists schtasks.exe; then
  5779. _info "It seems you are on Windows, we will install the Windows scheduler task."
  5780. else
  5781. _err "It is recommended to install crontab first. Try to install 'cron', 'crontab', 'crontabs' or 'vixie-cron'."
  5782. _err "We need to set a cron job to renew the certs automatically."
  5783. _err "Otherwise, your certs will not be able to be renewed automatically."
  5784. if [ -z "$FORCE" ]; then
  5785. _err "Please add '--force' and try install again to go without crontab."
  5786. _err "./$PROJECT_ENTRY --install --force"
  5787. return 1
  5788. fi
  5789. fi
  5790. fi
  5791. fi
  5792. if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
  5793. _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
  5794. _err "We need openssl to generate keys."
  5795. return 1
  5796. fi
  5797. if ! _exists "socat"; then
  5798. _err "It is recommended to install socat first."
  5799. _err "We use socat for the standalone server, which is used for standalone mode."
  5800. _err "If you don't want to use standalone mode, you may ignore this warning."
  5801. fi
  5802. return 0
  5803. }
  5804. _setShebang() {
  5805. _file="$1"
  5806. _shebang="$2"
  5807. if [ -z "$_shebang" ]; then
  5808. _usage "Usage: file shebang"
  5809. return 1
  5810. fi
  5811. cp "$_file" "$_file.tmp"
  5812. echo "$_shebang" >"$_file"
  5813. sed -n 2,99999p "$_file.tmp" >>"$_file"
  5814. rm -f "$_file.tmp"
  5815. }
  5816. #confighome
  5817. _installalias() {
  5818. _c_home="$1"
  5819. _initpath
  5820. _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  5821. if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
  5822. echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
  5823. echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
  5824. echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
  5825. fi
  5826. if [ "$_c_home" ]; then
  5827. _c_entry=" --config-home '$_c_home'"
  5828. fi
  5829. _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
  5830. if [ "$_c_home" ]; then
  5831. _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
  5832. else
  5833. _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile"
  5834. fi
  5835. _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
  5836. _profile="$(_detect_profile)"
  5837. if [ "$_profile" ]; then
  5838. _debug "Found profile: $_profile"
  5839. _info "Installing alias to '$_profile'"
  5840. _setopt "$_profile" ". \"$_envfile\""
  5841. _info "Close and reopen your terminal to start using $PROJECT_NAME"
  5842. else
  5843. _info "No profile has been found, you will need to change your working directory to $LE_WORKING_DIR to use $PROJECT_NAME"
  5844. fi
  5845. #for csh
  5846. _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
  5847. _csh_profile="$HOME/.cshrc"
  5848. if [ -f "$_csh_profile" ]; then
  5849. _info "Installing alias to '$_csh_profile'"
  5850. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  5851. if [ "$_c_home" ]; then
  5852. _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
  5853. else
  5854. _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile"
  5855. fi
  5856. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
  5857. _setopt "$_csh_profile" "source \"$_cshfile\""
  5858. fi
  5859. #for tcsh
  5860. _tcsh_profile="$HOME/.tcshrc"
  5861. if [ -f "$_tcsh_profile" ]; then
  5862. _info "Installing alias to '$_tcsh_profile'"
  5863. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  5864. if [ "$_c_home" ]; then
  5865. _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
  5866. fi
  5867. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
  5868. _setopt "$_tcsh_profile" "source \"$_cshfile\""
  5869. fi
  5870. }
  5871. # nocron confighome noprofile accountemail
  5872. install() {
  5873. if [ -z "$LE_WORKING_DIR" ]; then
  5874. LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
  5875. fi
  5876. _nocron="$1"
  5877. _c_home="$2"
  5878. _noprofile="$3"
  5879. _accountemail="$4"
  5880. if ! _initpath; then
  5881. _err "Install failed."
  5882. return 1
  5883. fi
  5884. if [ "$_nocron" ]; then
  5885. _debug "Skipping cron job installation"
  5886. fi
  5887. if [ "$_ACME_IN_CRON" != "1" ]; then
  5888. if ! _precheck "$_nocron"; then
  5889. _err "Pre-check failed, cannot install."
  5890. return 1
  5891. fi
  5892. fi
  5893. if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
  5894. _info "Using config home: $LE_CONFIG_HOME"
  5895. _c_home="$LE_CONFIG_HOME"
  5896. fi
  5897. #convert from le
  5898. if [ -d "$HOME/.le" ]; then
  5899. for envfile in "le.env" "le.sh.env"; do
  5900. if [ -f "$HOME/.le/$envfile" ]; then
  5901. if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
  5902. _upgrading="1"
  5903. _info "You are upgrading from le.sh"
  5904. _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
  5905. mv "$HOME/.le" "$LE_WORKING_DIR"
  5906. mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  5907. break
  5908. fi
  5909. fi
  5910. done
  5911. fi
  5912. _info "Installing to $LE_WORKING_DIR"
  5913. if [ ! -d "$LE_WORKING_DIR" ]; then
  5914. if ! mkdir -p "$LE_WORKING_DIR"; then
  5915. _err "Cannot create working dir: $LE_WORKING_DIR"
  5916. return 1
  5917. fi
  5918. chmod 700 "$LE_WORKING_DIR"
  5919. fi
  5920. if [ ! -d "$LE_CONFIG_HOME" ]; then
  5921. if ! mkdir -p "$LE_CONFIG_HOME"; then
  5922. _err "Cannot create config dir: $LE_CONFIG_HOME"
  5923. return 1
  5924. fi
  5925. chmod 700 "$LE_CONFIG_HOME"
  5926. fi
  5927. cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
  5928. if [ "$?" != "0" ]; then
  5929. _err "Installation failed, cannot copy $PROJECT_ENTRY"
  5930. return 1
  5931. fi
  5932. _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
  5933. if [ "$_ACME_IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then
  5934. _installalias "$_c_home"
  5935. fi
  5936. for subf in $_SUB_FOLDERS; do
  5937. if [ -d "$subf" ]; then
  5938. mkdir -p "$LE_WORKING_DIR/$subf"
  5939. cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
  5940. fi
  5941. done
  5942. if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
  5943. _initconf
  5944. fi
  5945. if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
  5946. _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
  5947. fi
  5948. if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
  5949. _saveaccountconf "CERT_HOME" "$CERT_HOME"
  5950. fi
  5951. if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
  5952. _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
  5953. fi
  5954. if [ -z "$_nocron" ]; then
  5955. installcronjob "$_c_home"
  5956. fi
  5957. if [ -z "$NO_DETECT_SH" ]; then
  5958. #Modify shebang
  5959. if _exists bash; then
  5960. _bash_path="$(bash -c "command -v bash 2>/dev/null")"
  5961. if [ -z "$_bash_path" ]; then
  5962. _bash_path="$(bash -c 'echo $SHELL')"
  5963. fi
  5964. fi
  5965. if [ "$_bash_path" ]; then
  5966. _info "bash has been found. Changing the shebang to use bash as preferred."
  5967. _shebang='#!'"$_bash_path"
  5968. _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
  5969. for subf in $_SUB_FOLDERS; do
  5970. if [ -d "$LE_WORKING_DIR/$subf" ]; then
  5971. for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
  5972. _setShebang "$_apifile" "$_shebang"
  5973. done
  5974. fi
  5975. done
  5976. fi
  5977. fi
  5978. if [ "$_accountemail" ]; then
  5979. _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
  5980. fi
  5981. _saveaccountconf "UPGRADE_HASH" "$(_getUpgradeHash)"
  5982. _info OK
  5983. }
  5984. # nocron
  5985. uninstall() {
  5986. _nocron="$1"
  5987. if [ -z "$_nocron" ]; then
  5988. uninstallcronjob
  5989. fi
  5990. _initpath
  5991. _uninstallalias
  5992. rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
  5993. _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\". You can remove them by yourself."
  5994. }
  5995. _uninstallalias() {
  5996. _initpath
  5997. _profile="$(_detect_profile)"
  5998. if [ "$_profile" ]; then
  5999. _info "Uninstalling alias from: '$_profile'"
  6000. text="$(cat "$_profile")"
  6001. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
  6002. fi
  6003. _csh_profile="$HOME/.cshrc"
  6004. if [ -f "$_csh_profile" ]; then
  6005. _info "Uninstalling alias from: '$_csh_profile'"
  6006. text="$(cat "$_csh_profile")"
  6007. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
  6008. fi
  6009. _tcsh_profile="$HOME/.tcshrc"
  6010. if [ -f "$_tcsh_profile" ]; then
  6011. _info "Uninstalling alias from: '$_csh_profile'"
  6012. text="$(cat "$_tcsh_profile")"
  6013. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
  6014. fi
  6015. }
  6016. cron() {
  6017. export _ACME_IN_CRON=1
  6018. _initpath
  6019. _info "$(__green "===Starting cron===")"
  6020. if [ "$AUTO_UPGRADE" = "1" ]; then
  6021. export LE_WORKING_DIR
  6022. (
  6023. if ! upgrade; then
  6024. _err "Cron: Upgrade failed!"
  6025. return 1
  6026. fi
  6027. )
  6028. . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
  6029. if [ -t 1 ]; then
  6030. __INTERACTIVE="1"
  6031. fi
  6032. _info "Automatically upgraded to: $VER"
  6033. fi
  6034. renewAll
  6035. _ret="$?"
  6036. _ACME_IN_CRON=""
  6037. _info "$(__green "===End cron===")"
  6038. exit $_ret
  6039. }
  6040. version() {
  6041. echo "$PROJECT"
  6042. echo "v$VER"
  6043. }
  6044. # subject content hooks code
  6045. _send_notify() {
  6046. _nsubject="$1"
  6047. _ncontent="$2"
  6048. _nhooks="$3"
  6049. _nerror="$4"
  6050. if [ "$NOTIFY_LEVEL" = "$NOTIFY_LEVEL_DISABLE" ]; then
  6051. _debug "The NOTIFY_LEVEL is $NOTIFY_LEVEL, which means it's disabled, so will just return."
  6052. return 0
  6053. fi
  6054. if [ -z "$_nhooks" ]; then
  6055. _debug "The NOTIFY_HOOK is empty, will just return."
  6056. return 0
  6057. fi
  6058. _nsource="$NOTIFY_SOURCE"
  6059. if [ -z "$_nsource" ]; then
  6060. _nsource="$(uname -n)"
  6061. fi
  6062. _nsubject="$_nsubject by $_nsource"
  6063. _send_err=0
  6064. for _n_hook in $(echo "$_nhooks" | tr ',' " "); do
  6065. _n_hook_file="$(_findHook "" $_SUB_FOLDER_NOTIFY "$_n_hook")"
  6066. _info "Sending via: $_n_hook"
  6067. _debug "Found $_n_hook_file for $_n_hook"
  6068. if [ -z "$_n_hook_file" ]; then
  6069. _err "Cannot find the hook file for $_n_hook"
  6070. continue
  6071. fi
  6072. if ! (
  6073. if ! . "$_n_hook_file"; then
  6074. _err "Error loading file $_n_hook_file. Please check your API file and try again."
  6075. return 1
  6076. fi
  6077. d_command="${_n_hook}_send"
  6078. if ! _exists "$d_command"; then
  6079. _err "It seems that your API file is not correct. Make sure it has a function named: $d_command"
  6080. return 1
  6081. fi
  6082. if ! $d_command "$_nsubject" "$_ncontent" "$_nerror"; then
  6083. _err "Error sending message using $d_command"
  6084. return 1
  6085. fi
  6086. return 0
  6087. ); then
  6088. _err "Error setting $_n_hook_file."
  6089. _send_err=1
  6090. else
  6091. _info "$_n_hook $(__green Success)"
  6092. fi
  6093. done
  6094. return $_send_err
  6095. }
  6096. # hook
  6097. _set_notify_hook() {
  6098. _nhooks="$1"
  6099. _test_subject="Hello, this is a notification from $PROJECT_NAME"
  6100. _test_content="If you receive this message, your notification works."
  6101. _send_notify "$_test_subject" "$_test_content" "$_nhooks" 0
  6102. }
  6103. #[hook] [level] [mode]
  6104. setnotify() {
  6105. _nhook="$1"
  6106. _nlevel="$2"
  6107. _nmode="$3"
  6108. _nsource="$4"
  6109. _initpath
  6110. if [ -z "$_nhook$_nlevel$_nmode$_nsource" ]; then
  6111. _usage "Usage: $PROJECT_ENTRY --set-notify [--notify-hook <hookname>] [--notify-level <0|1|2|3>] [--notify-mode <0|1>] [--notify-source <hostname>]"
  6112. _usage "$_NOTIFY_WIKI"
  6113. return 1
  6114. fi
  6115. if [ "$_nlevel" ]; then
  6116. _info "Set notify level to: $_nlevel"
  6117. export "NOTIFY_LEVEL=$_nlevel"
  6118. _saveaccountconf "NOTIFY_LEVEL" "$NOTIFY_LEVEL"
  6119. fi
  6120. if [ "$_nmode" ]; then
  6121. _info "Set notify mode to: $_nmode"
  6122. export "NOTIFY_MODE=$_nmode"
  6123. _saveaccountconf "NOTIFY_MODE" "$NOTIFY_MODE"
  6124. fi
  6125. if [ "$_nsource" ]; then
  6126. _info "Set notify source to: $_nsource"
  6127. export "NOTIFY_SOURCE=$_nsource"
  6128. _saveaccountconf "NOTIFY_SOURCE" "$NOTIFY_SOURCE"
  6129. fi
  6130. if [ "$_nhook" ]; then
  6131. _info "Set notify hook to: $_nhook"
  6132. if [ "$_nhook" = "$NO_VALUE" ]; then
  6133. _info "Clearing notify hook"
  6134. _clearaccountconf "NOTIFY_HOOK"
  6135. else
  6136. if _set_notify_hook "$_nhook"; then
  6137. export NOTIFY_HOOK="$_nhook"
  6138. _saveaccountconf "NOTIFY_HOOK" "$NOTIFY_HOOK"
  6139. return 0
  6140. else
  6141. _err "Cannot set notify hook to: $_nhook"
  6142. return 1
  6143. fi
  6144. fi
  6145. fi
  6146. }
  6147. showhelp() {
  6148. _initpath
  6149. version
  6150. echo "Usage: $PROJECT_ENTRY <command> ... [parameters ...]
  6151. Commands:
  6152. -h, --help Show this help message.
  6153. -v, --version Show version info.
  6154. --install Install $PROJECT_NAME to your system.
  6155. --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
  6156. --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
  6157. --issue Issue a cert.
  6158. --deploy Deploy the cert to your server.
  6159. -i, --install-cert Install the issued cert to Apache/nginx or any other server.
  6160. -r, --renew Renew a cert.
  6161. --renew-all Renew all the certs.
  6162. --revoke Revoke a cert.
  6163. --remove Remove the cert from list of certs known to $PROJECT_NAME.
  6164. --list List all the certs.
  6165. --info Show the $PROJECT_NAME configs, or the configs for a domain with [-d domain] parameter.
  6166. --to-pkcs12 Export the certificate and key to a pfx file.
  6167. --to-pkcs8 Convert to pkcs8 format.
  6168. --sign-csr Issue a cert from an existing csr.
  6169. --show-csr Show the content of a csr.
  6170. -ccr, --create-csr Create CSR, professional use.
  6171. --create-domain-key Create an domain private key, professional use.
  6172. --update-account Update account info.
  6173. --register-account Register account key.
  6174. --deactivate-account Deactivate the account.
  6175. --create-account-key Create an account private key, professional use.
  6176. --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.
  6177. --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
  6178. --cron Run cron job to renew all the certs.
  6179. --set-notify Set the cron notification hook, level or mode.
  6180. --deactivate Deactivate the domain authz, professional use.
  6181. --set-default-ca Used with '--server', Set the default CA to use.
  6182. See: $_SERVER_WIKI
  6183. --set-default-chain Set the default preferred chain for a CA.
  6184. See: $_PREFERRED_CHAIN_WIKI
  6185. Parameters:
  6186. -d, --domain <domain.tld> Specifies a domain, used to issue, renew or revoke etc.
  6187. --challenge-alias <domain.tld> The challenge domain alias for DNS alias mode.
  6188. See: $_DNS_ALIAS_WIKI
  6189. --domain-alias <domain.tld> The domain alias for DNS alias mode.
  6190. See: $_DNS_ALIAS_WIKI
  6191. --preferred-chain <chain> If the CA offers multiple certificate chains, prefer the chain with an issuer matching this Subject Common Name.
  6192. If no match, the default offered chain will be used. (default: empty)
  6193. See: $_PREFERRED_CHAIN_WIKI
  6194. --cert-profile, --certificate-profile <profile> If the CA offers profiles, select the desired profile
  6195. See: $_PROFILESELECTION_WIKI
  6196. --valid-to <date-time> Request the NotAfter field of the cert.
  6197. See: $_VALIDITY_WIKI
  6198. --valid-from <date-time> Request the NotBefore field of the cert.
  6199. See: $_VALIDITY_WIKI
  6200. -f, --force Force install, force cert renewal or override sudo restrictions.
  6201. --staging, --test Use staging server, for testing.
  6202. --debug [0|1|2|3] Output debug info. Defaults to $DEBUG_LEVEL_DEFAULT if argument is omitted.
  6203. --output-insecure Output all the sensitive messages.
  6204. By default all the credentials/sensitive messages are hidden from the output/debug/log for security.
  6205. -w, --webroot <directory> Specifies the web root folder for web root mode.
  6206. --standalone Use standalone mode.
  6207. --alpn Use standalone alpn mode.
  6208. --stateless Use stateless mode.
  6209. See: $_STATELESS_WIKI
  6210. --apache Use Apache mode.
  6211. --dns [dns_hook] Use dns manual mode or dns api. Defaults to manual mode when argument is omitted.
  6212. See: $_DNS_API_WIKI
  6213. --dnssleep <seconds> The time in seconds to wait for all the txt records to propagate in dns api mode.
  6214. It's not necessary to use this by default, $PROJECT_NAME polls dns status by DOH automatically.
  6215. -k, --keylength <bits> Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384, ec-521.
  6216. -ak, --accountkeylength <bits> Specifies the account key length: 2048, 3072, 4096
  6217. --log [file] Specifies the log file. Defaults to \"$DEFAULT_LOG_FILE\" if argument is omitted.
  6218. --log-level <1|2> Specifies the log level, default is $DEFAULT_LOG_LEVEL.
  6219. --syslog <0|3|6|7> Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
  6220. --eab-kid <eab_key_id> Key Identifier for External Account Binding.
  6221. --eab-hmac-key <eab_hmac_key> HMAC key for External Account Binding.
  6222. These parameters are to install the cert to nginx/Apache or any other server after issue/renew a cert:
  6223. --cert-file <file> Path to copy the cert file to after issue/renew.
  6224. --key-file <file> Path to copy the key file to after issue/renew.
  6225. --ca-file <file> Path to copy the intermediate cert file to after issue/renew.
  6226. --fullchain-file <file> Path to copy the fullchain cert file to after issue/renew.
  6227. --reloadcmd <command> Command to execute after issue/renew to reload the server.
  6228. --server <server_uri> ACME Directory Resource URI. (default: $DEFAULT_CA)
  6229. See: $_SERVER_WIKI
  6230. --accountconf <file> Specifies a customized account config file.
  6231. --home <directory> Specifies the home dir for $PROJECT_NAME.
  6232. --cert-home <directory> Specifies the home dir to save all the certs.
  6233. --config-home <directory> Specifies the home dir to save all the configurations.
  6234. --useragent <string> Specifies the user agent string. it will be saved for future use too.
  6235. -m, --email <email> Specifies the account email, only valid for the '--install' and '--update-account' command.
  6236. --accountkey <file> Specifies the account key path, only valid for the '--install' command.
  6237. --days <ndays> Specifies the days to renew the cert when using '--issue' command. The default value is $DEFAULT_RENEW days.
  6238. --httpport <port> Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
  6239. --tlsport <port> Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
  6240. --local-address <ip> Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
  6241. --listraw Only used for '--list' command, list the certs in raw format.
  6242. -se, --stop-renew-on-error Only valid for '--renew-all' command. Stop if one cert has error in renewal.
  6243. --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
  6244. --ca-bundle <file> Specifies the path to the CA certificate bundle to verify api server's certificate.
  6245. --ca-path <directory> Specifies directory containing CA certificates in PEM format, used by wget or curl.
  6246. --no-cron Only valid for '--install' command, which means: do not install the default cron job.
  6247. In this case, the certs will not be renewed automatically.
  6248. --no-profile Only valid for '--install' command, which means: do not install aliases to user profile.
  6249. --no-color Do not output color text.
  6250. --force-color Force output of color text. Useful for non-interactive use with the aha tool for HTML E-Mails.
  6251. --ecc Specifies use of the ECC cert. Only valid for '--install-cert', '--renew', '--remove ', '--revoke',
  6252. '--deploy', '--to-pkcs8', '--to-pkcs12' and '--create-csr'.
  6253. --csr <file> Specifies the input csr.
  6254. --pre-hook <command> Command to be run before obtaining any certificates.
  6255. --post-hook <command> Command to be run after attempting to obtain/renew certificates. Runs regardless of whether obtain/renew succeeded or failed.
  6256. --renew-hook <command> Command to be run after each successfully renewed certificate.
  6257. --deploy-hook <hookname> The hook file to deploy cert
  6258. --extended-key-usage <string> Manually define the CSR extended key usage value. The default is serverAuth,clientAuth.
  6259. --ocsp, --ocsp-must-staple Generate OCSP-Must-Staple extension.
  6260. --always-force-new-domain-key Generate new domain key on renewal. Otherwise, the domain key is not changed by default.
  6261. --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future. Defaults to 1 if argument is omitted.
  6262. --listen-v4 Force standalone/tls server to listen at ipv4.
  6263. --listen-v6 Force standalone/tls server to listen at ipv6.
  6264. --openssl-bin <file> Specifies a custom openssl bin location.
  6265. --use-wget Force to use wget, if you have both curl and wget installed.
  6266. --yes-I-know-dns-manual-mode-enough-go-ahead-please Force use of dns manual mode.
  6267. See: $_DNS_MANUAL_WIKI
  6268. -b, --branch <branch> Only valid for '--upgrade' command, specifies the branch name to upgrade to.
  6269. --notify-level <0|1|2|3> Set the notification level: Default value is $NOTIFY_LEVEL_DEFAULT.
  6270. 0: disabled, no notification will be sent.
  6271. 1: send notifications only when there is an error.
  6272. 2: send notifications when a cert is successfully renewed, or there is an error.
  6273. 3: send notifications when a cert is skipped, renewed, or error.
  6274. --notify-mode <0|1> Set notification mode. Default value is $NOTIFY_MODE_DEFAULT.
  6275. 0: Bulk mode. Send all the domain's notifications in one message(mail).
  6276. 1: Cert mode. Send a message for every single cert.
  6277. --notify-hook <hookname> Set the notify hook
  6278. --notify-source <server name> Set the server name in the notification message
  6279. --revoke-reason <0-10> The reason for revocation, can be used in conjunction with the '--revoke' command.
  6280. See: $_REVOKE_WIKI
  6281. --password <password> Add a password to exported pfx file. Use with --to-pkcs12.
  6282. "
  6283. }
  6284. installOnline() {
  6285. _info "Installing from online archive."
  6286. _branch="$BRANCH"
  6287. if [ -z "$_branch" ]; then
  6288. _branch="master"
  6289. fi
  6290. target="$PROJECT/archive/$_branch.tar.gz"
  6291. _info "Downloading $target"
  6292. localname="$_branch.tar.gz"
  6293. if ! _get "$target" >$localname; then
  6294. _err "Download error."
  6295. return 1
  6296. fi
  6297. (
  6298. _info "Extracting $localname"
  6299. if ! (tar xzf $localname || gtar xzf $localname); then
  6300. _err "Extraction error."
  6301. exit 1
  6302. fi
  6303. cd "$PROJECT_NAME-$_branch"
  6304. chmod +x $PROJECT_ENTRY
  6305. if ./$PROJECT_ENTRY --install "$@"; then
  6306. _info "Install success!"
  6307. fi
  6308. cd ..
  6309. rm -rf "$PROJECT_NAME-$_branch"
  6310. rm -f "$localname"
  6311. )
  6312. }
  6313. _getRepoHash() {
  6314. _hash_path=$1
  6315. shift
  6316. _hash_url="${PROJECT_API:-https://api.github.com/repos/acmesh-official}/$PROJECT_NAME/git/refs/$_hash_path"
  6317. _get "$_hash_url" "" 30 | tr -d "\r\n" | tr '{},' '\n\n\n' | grep '"sha":' | cut -d '"' -f 4
  6318. }
  6319. _getUpgradeHash() {
  6320. _b="$BRANCH"
  6321. if [ -z "$_b" ]; then
  6322. _b="master"
  6323. fi
  6324. _hash=$(_getRepoHash "heads/$_b")
  6325. if [ -z "$_hash" ]; then _hash=$(_getRepoHash "tags/$_b"); fi
  6326. echo $_hash
  6327. }
  6328. upgrade() {
  6329. if (
  6330. _initpath
  6331. [ -z "$FORCE" ] && [ "$(_getUpgradeHash)" = "$(_readaccountconf "UPGRADE_HASH")" ] && _info "Already up to date!" && exit 0
  6332. export LE_WORKING_DIR
  6333. cd "$LE_WORKING_DIR"
  6334. installOnline "--nocron" "--noprofile"
  6335. ); then
  6336. _info "Upgrade successful!"
  6337. exit 0
  6338. else
  6339. _err "Upgrade failed!"
  6340. exit 1
  6341. fi
  6342. }
  6343. _processAccountConf() {
  6344. if [ "$_useragent" ]; then
  6345. _saveaccountconf "USER_AGENT" "$_useragent"
  6346. elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
  6347. _saveaccountconf "USER_AGENT" "$USER_AGENT"
  6348. fi
  6349. if [ "$_openssl_bin" ]; then
  6350. _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin"
  6351. elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
  6352. _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
  6353. fi
  6354. if [ "$_auto_upgrade" ]; then
  6355. _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
  6356. elif [ "$AUTO_UPGRADE" ]; then
  6357. _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
  6358. fi
  6359. if [ "$_use_wget" ]; then
  6360. _saveaccountconf "ACME_USE_WGET" "$_use_wget"
  6361. elif [ "$ACME_USE_WGET" ]; then
  6362. _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET"
  6363. fi
  6364. }
  6365. _checkSudo() {
  6366. if [ -z "$__INTERACTIVE" ]; then
  6367. #don't check if it's not in an interactive shell
  6368. return 0
  6369. fi
  6370. if [ "$SUDO_GID" ] && [ "$SUDO_COMMAND" ] && [ "$SUDO_USER" ] && [ "$SUDO_UID" ]; then
  6371. if [ "$SUDO_USER" = "root" ] && [ "$SUDO_UID" = "0" ]; then
  6372. #it's root using sudo, no matter it's using sudo or not, just fine
  6373. return 0
  6374. fi
  6375. if [ -n "$SUDO_COMMAND" ]; then
  6376. #it's a normal user doing "sudo su", or `sudo -i` or `sudo -s`, or `sudo su acmeuser1`
  6377. _endswith "$SUDO_COMMAND" /bin/su || _contains "$SUDO_COMMAND" "/bin/su " || grep "^$SUDO_COMMAND\$" /etc/shells >/dev/null 2>&1
  6378. return $?
  6379. fi
  6380. #otherwise
  6381. return 1
  6382. fi
  6383. return 0
  6384. }
  6385. #server #keylength
  6386. _selectServer() {
  6387. _server="$1"
  6388. _skeylength="$2"
  6389. _server_lower="$(echo "$_server" | _lower_case)"
  6390. _sindex=0
  6391. for snames in $CA_NAMES; do
  6392. snames="$(echo "$snames" | _lower_case)"
  6393. _sindex="$(_math $_sindex + 1)"
  6394. _debug2 "_selectServer try snames" "$snames"
  6395. for sname in $(echo "$snames" | tr ',' ' '); do
  6396. if [ "$_server_lower" = "$sname" ]; then
  6397. _debug2 "_selectServer match $sname"
  6398. _serverdir="$(_getfield "$CA_SERVERS" $_sindex)"
  6399. if [ "$_serverdir" = "$CA_SSLCOM_RSA" ] && _isEccKey "$_skeylength"; then
  6400. _serverdir="$CA_SSLCOM_ECC"
  6401. fi
  6402. _debug "Selected server: $_serverdir"
  6403. ACME_DIRECTORY="$_serverdir"
  6404. export ACME_DIRECTORY
  6405. return
  6406. fi
  6407. done
  6408. done
  6409. ACME_DIRECTORY="$_server"
  6410. export ACME_DIRECTORY
  6411. }
  6412. #url
  6413. _getCAShortName() {
  6414. caurl="$1"
  6415. if [ -z "$caurl" ]; then
  6416. #use letsencrypt as default value if the Le_API is empty
  6417. #this case can only come from the old upgrading.
  6418. caurl="$CA_LETSENCRYPT_V2"
  6419. fi
  6420. if [ "$CA_SSLCOM_ECC" = "$caurl" ]; then
  6421. caurl="$CA_SSLCOM_RSA" #just hack to get the short name
  6422. fi
  6423. caurl_lower="$(echo $caurl | _lower_case)"
  6424. _sindex=0
  6425. for surl in $(echo "$CA_SERVERS" | _lower_case | tr , ' '); do
  6426. _sindex="$(_math $_sindex + 1)"
  6427. if [ "$caurl_lower" = "$surl" ]; then
  6428. _nindex=0
  6429. for snames in $CA_NAMES; do
  6430. _nindex="$(_math $_nindex + 1)"
  6431. if [ $_nindex -ge $_sindex ]; then
  6432. _getfield "$snames" 1
  6433. return
  6434. fi
  6435. done
  6436. fi
  6437. done
  6438. echo "$caurl"
  6439. }
  6440. #set default ca to $ACME_DIRECTORY
  6441. setdefaultca() {
  6442. if [ -z "$ACME_DIRECTORY" ]; then
  6443. _err "Please provide a --server parameter."
  6444. return 1
  6445. fi
  6446. _saveaccountconf "DEFAULT_ACME_SERVER" "$ACME_DIRECTORY"
  6447. _info "Changed default CA to: $(__green "$ACME_DIRECTORY")"
  6448. }
  6449. #preferred-chain
  6450. setdefaultchain() {
  6451. _initpath
  6452. _preferred_chain="$1"
  6453. if [ -z "$_preferred_chain" ]; then
  6454. _err "Please provide a value for '--preferred-chain'."
  6455. return 1
  6456. fi
  6457. mkdir -p "$CA_DIR"
  6458. _savecaconf "DEFAULT_PREFERRED_CHAIN" "$_preferred_chain"
  6459. }
  6460. #domain ecc
  6461. info() {
  6462. _domain="$1"
  6463. _ecc="$2"
  6464. _initpath
  6465. if [ -z "$_domain" ]; then
  6466. _debug "Show global configs"
  6467. echo "LE_WORKING_DIR=$LE_WORKING_DIR"
  6468. echo "LE_CONFIG_HOME=$LE_CONFIG_HOME"
  6469. cat "$ACCOUNT_CONF_PATH"
  6470. else
  6471. _debug "Show domain configs"
  6472. (
  6473. _initpath "$_domain" "$_ecc"
  6474. echo "DOMAIN_CONF=$DOMAIN_CONF"
  6475. for seg in $(cat $DOMAIN_CONF | cut -d = -f 1); do
  6476. echo "$seg=$(_readdomainconf "$seg")"
  6477. done
  6478. )
  6479. fi
  6480. }
  6481. _process() {
  6482. _CMD=""
  6483. _domain=""
  6484. _altdomains="$NO_VALUE"
  6485. _webroot=""
  6486. _challenge_alias=""
  6487. _keylength="$DEFAULT_DOMAIN_KEY_LENGTH"
  6488. _accountkeylength="$DEFAULT_ACCOUNT_KEY_LENGTH"
  6489. _cert_file=""
  6490. _key_file=""
  6491. _ca_file=""
  6492. _fullchain_file=""
  6493. _reloadcmd=""
  6494. _password=""
  6495. _accountconf=""
  6496. _useragent=""
  6497. _accountemail=""
  6498. _accountkey=""
  6499. _certhome=""
  6500. _confighome=""
  6501. _httpport=""
  6502. _tlsport=""
  6503. _dnssleep=""
  6504. _listraw=""
  6505. _stopRenewOnError=""
  6506. #_insecure=""
  6507. _ca_bundle=""
  6508. _ca_path=""
  6509. _nocron=""
  6510. _noprofile=""
  6511. _ecc=""
  6512. _csr=""
  6513. _pre_hook=""
  6514. _post_hook=""
  6515. _renew_hook=""
  6516. _deploy_hook=""
  6517. _logfile=""
  6518. _log=""
  6519. _local_address=""
  6520. _log_level=""
  6521. _auto_upgrade=""
  6522. _listen_v4=""
  6523. _listen_v6=""
  6524. _openssl_bin=""
  6525. _syslog=""
  6526. _use_wget=""
  6527. _server=""
  6528. _notify_hook=""
  6529. _notify_level=""
  6530. _notify_mode=""
  6531. _notify_source=""
  6532. _revoke_reason=""
  6533. _eab_kid=""
  6534. _eab_hmac_key=""
  6535. _preferred_chain=""
  6536. _valid_from=""
  6537. _valid_to=""
  6538. _certificate_profile=""
  6539. while [ ${#} -gt 0 ]; do
  6540. case "${1}" in
  6541. --help | -h)
  6542. showhelp
  6543. return
  6544. ;;
  6545. --version | -v)
  6546. version
  6547. return
  6548. ;;
  6549. --install)
  6550. _CMD="install"
  6551. ;;
  6552. --install-online)
  6553. shift
  6554. installOnline "$@"
  6555. return
  6556. ;;
  6557. --uninstall)
  6558. _CMD="uninstall"
  6559. ;;
  6560. --upgrade)
  6561. _CMD="upgrade"
  6562. ;;
  6563. --issue)
  6564. _CMD="issue"
  6565. ;;
  6566. --deploy)
  6567. _CMD="deploy"
  6568. ;;
  6569. --sign-csr | --signcsr)
  6570. _CMD="signcsr"
  6571. ;;
  6572. --show-csr | --showcsr)
  6573. _CMD="showcsr"
  6574. ;;
  6575. -i | --install-cert | --installcert)
  6576. _CMD="installcert"
  6577. ;;
  6578. --renew | -r)
  6579. _CMD="renew"
  6580. ;;
  6581. --renew-all | --renewAll | --renewall)
  6582. _CMD="renewAll"
  6583. ;;
  6584. --revoke)
  6585. _CMD="revoke"
  6586. ;;
  6587. --remove)
  6588. _CMD="remove"
  6589. ;;
  6590. --list)
  6591. _CMD="list"
  6592. ;;
  6593. --info)
  6594. _CMD="info"
  6595. ;;
  6596. --install-cronjob | --installcronjob)
  6597. _CMD="installcronjob"
  6598. ;;
  6599. --uninstall-cronjob | --uninstallcronjob)
  6600. _CMD="uninstallcronjob"
  6601. ;;
  6602. --cron)
  6603. _CMD="cron"
  6604. ;;
  6605. --to-pkcs12 | --to-pkcs | --toPkcs)
  6606. _CMD="toPkcs"
  6607. ;;
  6608. --to-pkcs8 | --toPkcs8)
  6609. _CMD="toPkcs8"
  6610. ;;
  6611. --create-account-key | --createAccountKey | --createaccountkey | -cak)
  6612. _CMD="createAccountKey"
  6613. ;;
  6614. --create-domain-key | --createDomainKey | --createdomainkey | -cdk)
  6615. _CMD="createDomainKey"
  6616. ;;
  6617. -ccr | --create-csr | --createCSR | --createcsr)
  6618. _CMD="createCSR"
  6619. ;;
  6620. --deactivate)
  6621. _CMD="deactivate"
  6622. ;;
  6623. --update-account | --updateaccount)
  6624. _CMD="updateaccount"
  6625. ;;
  6626. --register-account | --registeraccount)
  6627. _CMD="registeraccount"
  6628. ;;
  6629. --deactivate-account)
  6630. _CMD="deactivateaccount"
  6631. ;;
  6632. --set-notify)
  6633. _CMD="setnotify"
  6634. ;;
  6635. --set-default-ca)
  6636. _CMD="setdefaultca"
  6637. ;;
  6638. --set-default-chain)
  6639. _CMD="setdefaultchain"
  6640. ;;
  6641. --list-profiles)
  6642. _CMD="list_profiles"
  6643. ;;
  6644. -d | --domain)
  6645. _dvalue="$2"
  6646. if [ "$_dvalue" ]; then
  6647. if _startswith "$_dvalue" "-"; then
  6648. _err "'$_dvalue' is not a valid domain for parameter '$1'"
  6649. return 1
  6650. fi
  6651. if _is_idn "$_dvalue" && ! _exists idn; then
  6652. _err "It seems that $_dvalue is an IDN (Internationalized Domain Names), please install the 'idn' command first."
  6653. return 1
  6654. fi
  6655. if [ -z "$_domain" ]; then
  6656. _domain="$_dvalue"
  6657. else
  6658. if [ "$_altdomains" = "$NO_VALUE" ]; then
  6659. _altdomains="$_dvalue"
  6660. else
  6661. _altdomains="$_altdomains,$_dvalue"
  6662. fi
  6663. fi
  6664. fi
  6665. shift
  6666. ;;
  6667. -f | --force)
  6668. FORCE="1"
  6669. ;;
  6670. --staging | --test)
  6671. STAGE="1"
  6672. ;;
  6673. --server)
  6674. _server="$2"
  6675. shift
  6676. ;;
  6677. --debug)
  6678. if [ -z "$2" ] || _startswith "$2" "-"; then
  6679. DEBUG="$DEBUG_LEVEL_DEFAULT"
  6680. else
  6681. DEBUG="$2"
  6682. shift
  6683. fi
  6684. ;;
  6685. --output-insecure)
  6686. export OUTPUT_INSECURE=1
  6687. ;;
  6688. -w | --webroot)
  6689. wvalue="$2"
  6690. if [ -z "$_webroot" ]; then
  6691. _webroot="$wvalue"
  6692. else
  6693. _webroot="$_webroot,$wvalue"
  6694. fi
  6695. shift
  6696. ;;
  6697. --challenge-alias)
  6698. cvalue="$2"
  6699. _challenge_alias="$_challenge_alias$cvalue,"
  6700. shift
  6701. ;;
  6702. --domain-alias)
  6703. cvalue="$DNS_ALIAS_PREFIX$2"
  6704. _challenge_alias="$_challenge_alias$cvalue,"
  6705. shift
  6706. ;;
  6707. --standalone)
  6708. wvalue="$NO_VALUE"
  6709. if [ -z "$_webroot" ]; then
  6710. _webroot="$wvalue"
  6711. else
  6712. _webroot="$_webroot,$wvalue"
  6713. fi
  6714. ;;
  6715. --alpn)
  6716. wvalue="$W_ALPN"
  6717. if [ -z "$_webroot" ]; then
  6718. _webroot="$wvalue"
  6719. else
  6720. _webroot="$_webroot,$wvalue"
  6721. fi
  6722. ;;
  6723. --stateless)
  6724. wvalue="$MODE_STATELESS"
  6725. if [ -z "$_webroot" ]; then
  6726. _webroot="$wvalue"
  6727. else
  6728. _webroot="$_webroot,$wvalue"
  6729. fi
  6730. ;;
  6731. --local-address)
  6732. lvalue="$2"
  6733. _local_address="$_local_address$lvalue,"
  6734. shift
  6735. ;;
  6736. --apache)
  6737. wvalue="apache"
  6738. if [ -z "$_webroot" ]; then
  6739. _webroot="$wvalue"
  6740. else
  6741. _webroot="$_webroot,$wvalue"
  6742. fi
  6743. ;;
  6744. --nginx)
  6745. wvalue="$NGINX"
  6746. if [ "$2" ] && ! _startswith "$2" "-"; then
  6747. wvalue="$NGINX$2"
  6748. shift
  6749. fi
  6750. if [ -z "$_webroot" ]; then
  6751. _webroot="$wvalue"
  6752. else
  6753. _webroot="$_webroot,$wvalue"
  6754. fi
  6755. ;;
  6756. --dns)
  6757. wvalue="$W_DNS"
  6758. if [ "$2" ] && ! _startswith "$2" "-"; then
  6759. wvalue="$2"
  6760. shift
  6761. fi
  6762. if [ -z "$_webroot" ]; then
  6763. _webroot="$wvalue"
  6764. else
  6765. _webroot="$_webroot,$wvalue"
  6766. fi
  6767. ;;
  6768. --dnssleep)
  6769. _dnssleep="$2"
  6770. Le_DNSSleep="$_dnssleep"
  6771. shift
  6772. ;;
  6773. --keylength | -k)
  6774. _keylength="$2"
  6775. shift
  6776. if [ "$_keylength" ] && ! _isEccKey "$_keylength"; then
  6777. export __SELECTED_RSA_KEY=1
  6778. fi
  6779. ;;
  6780. -ak | --accountkeylength)
  6781. _accountkeylength="$2"
  6782. shift
  6783. ;;
  6784. --cert-file | --certpath)
  6785. _cert_file="$2"
  6786. shift
  6787. ;;
  6788. --key-file | --keypath)
  6789. _key_file="$2"
  6790. shift
  6791. ;;
  6792. --ca-file | --capath)
  6793. _ca_file="$2"
  6794. shift
  6795. ;;
  6796. --fullchain-file | --fullchainpath)
  6797. _fullchain_file="$2"
  6798. shift
  6799. ;;
  6800. --reloadcmd | --reloadCmd)
  6801. _reloadcmd="$2"
  6802. shift
  6803. ;;
  6804. --password)
  6805. _password="$2"
  6806. shift
  6807. ;;
  6808. --accountconf)
  6809. _accountconf="$2"
  6810. ACCOUNT_CONF_PATH="$_accountconf"
  6811. shift
  6812. ;;
  6813. --home)
  6814. export LE_WORKING_DIR="$(echo "$2" | sed 's|/$||')"
  6815. shift
  6816. ;;
  6817. --cert-home | --certhome)
  6818. _certhome="$2"
  6819. export CERT_HOME="$_certhome"
  6820. shift
  6821. ;;
  6822. --config-home)
  6823. _confighome="$2"
  6824. export LE_CONFIG_HOME="$_confighome"
  6825. shift
  6826. ;;
  6827. --useragent)
  6828. _useragent="$2"
  6829. USER_AGENT="$_useragent"
  6830. shift
  6831. ;;
  6832. -m | --email | --accountemail)
  6833. _accountemail="$2"
  6834. export ACCOUNT_EMAIL="$_accountemail"
  6835. shift
  6836. ;;
  6837. --accountkey)
  6838. _accountkey="$2"
  6839. ACCOUNT_KEY_PATH="$_accountkey"
  6840. shift
  6841. ;;
  6842. --days)
  6843. _days="$2"
  6844. Le_RenewalDays="$_days"
  6845. shift
  6846. ;;
  6847. --valid-from)
  6848. _valid_from="$2"
  6849. shift
  6850. ;;
  6851. --valid-to)
  6852. _valid_to="$2"
  6853. shift
  6854. ;;
  6855. --certificate-profile | --cert-profile)
  6856. _certificate_profile="$2"
  6857. shift
  6858. ;;
  6859. --httpport)
  6860. _httpport="$2"
  6861. Le_HTTPPort="$_httpport"
  6862. shift
  6863. ;;
  6864. --tlsport)
  6865. _tlsport="$2"
  6866. Le_TLSPort="$_tlsport"
  6867. shift
  6868. ;;
  6869. --listraw)
  6870. _listraw="raw"
  6871. ;;
  6872. -se | --stop-renew-on-error | --stopRenewOnError | --stoprenewonerror)
  6873. _stopRenewOnError="1"
  6874. ;;
  6875. --insecure)
  6876. #_insecure="1"
  6877. HTTPS_INSECURE="1"
  6878. ;;
  6879. --ca-bundle)
  6880. _ca_bundle="$(_readlink "$2")"
  6881. CA_BUNDLE="$_ca_bundle"
  6882. shift
  6883. ;;
  6884. --ca-path)
  6885. _ca_path="$2"
  6886. CA_PATH="$_ca_path"
  6887. shift
  6888. ;;
  6889. --no-cron | --nocron)
  6890. _nocron="1"
  6891. ;;
  6892. --no-profile | --noprofile)
  6893. _noprofile="1"
  6894. ;;
  6895. --no-color)
  6896. export ACME_NO_COLOR=1
  6897. ;;
  6898. --force-color)
  6899. export ACME_FORCE_COLOR=1
  6900. ;;
  6901. --ecc)
  6902. _ecc="isEcc"
  6903. ;;
  6904. --csr)
  6905. _csr="$2"
  6906. shift
  6907. ;;
  6908. --pre-hook)
  6909. _pre_hook="$2"
  6910. shift
  6911. ;;
  6912. --post-hook)
  6913. _post_hook="$2"
  6914. shift
  6915. ;;
  6916. --renew-hook)
  6917. _renew_hook="$2"
  6918. shift
  6919. ;;
  6920. --deploy-hook)
  6921. if [ -z "$2" ] || _startswith "$2" "-"; then
  6922. _usage "Please specify a value for '--deploy-hook'"
  6923. return 1
  6924. fi
  6925. _deploy_hook="$_deploy_hook$2,"
  6926. shift
  6927. ;;
  6928. --extended-key-usage)
  6929. Le_ExtKeyUse="$2"
  6930. shift
  6931. ;;
  6932. --ocsp-must-staple | --ocsp)
  6933. Le_OCSP_Staple="1"
  6934. ;;
  6935. --always-force-new-domain-key)
  6936. if [ -z "$2" ] || _startswith "$2" "-"; then
  6937. Le_ForceNewDomainKey=1
  6938. else
  6939. Le_ForceNewDomainKey="$2"
  6940. shift
  6941. fi
  6942. ;;
  6943. --yes-I-know-dns-manual-mode-enough-go-ahead-please)
  6944. export FORCE_DNS_MANUAL=1
  6945. ;;
  6946. --log | --logfile)
  6947. _log="1"
  6948. _logfile="$2"
  6949. if _startswith "$_logfile" '-'; then
  6950. _logfile=""
  6951. else
  6952. shift
  6953. fi
  6954. LOG_FILE="$_logfile"
  6955. if [ -z "$LOG_LEVEL" ]; then
  6956. LOG_LEVEL="$DEFAULT_LOG_LEVEL"
  6957. fi
  6958. ;;
  6959. --log-level)
  6960. _log_level="$2"
  6961. LOG_LEVEL="$_log_level"
  6962. shift
  6963. ;;
  6964. --syslog)
  6965. if ! _startswith "$2" '-'; then
  6966. _syslog="$2"
  6967. shift
  6968. fi
  6969. if [ -z "$_syslog" ]; then
  6970. _syslog="$SYSLOG_LEVEL_DEFAULT"
  6971. fi
  6972. ;;
  6973. --auto-upgrade)
  6974. _auto_upgrade="$2"
  6975. if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
  6976. _auto_upgrade="1"
  6977. else
  6978. shift
  6979. fi
  6980. AUTO_UPGRADE="$_auto_upgrade"
  6981. ;;
  6982. --listen-v4)
  6983. _listen_v4="1"
  6984. Le_Listen_V4="$_listen_v4"
  6985. ;;
  6986. --listen-v6)
  6987. _listen_v6="1"
  6988. Le_Listen_V6="$_listen_v6"
  6989. ;;
  6990. --openssl-bin)
  6991. _openssl_bin="$2"
  6992. ACME_OPENSSL_BIN="$_openssl_bin"
  6993. shift
  6994. ;;
  6995. --use-wget)
  6996. _use_wget="1"
  6997. ACME_USE_WGET="1"
  6998. ;;
  6999. --branch | -b)
  7000. export BRANCH="$2"
  7001. shift
  7002. ;;
  7003. --notify-hook)
  7004. _nhook="$2"
  7005. if _startswith "$_nhook" "-"; then
  7006. _err "'$_nhook' is not a hook name for '$1'"
  7007. return 1
  7008. fi
  7009. if [ "$_notify_hook" ]; then
  7010. _notify_hook="$_notify_hook,$_nhook"
  7011. else
  7012. _notify_hook="$_nhook"
  7013. fi
  7014. shift
  7015. ;;
  7016. --notify-level)
  7017. _nlevel="$2"
  7018. if _startswith "$_nlevel" "-"; then
  7019. _err "'$_nlevel' is not an integer for '$1'"
  7020. return 1
  7021. fi
  7022. _notify_level="$_nlevel"
  7023. shift
  7024. ;;
  7025. --notify-mode)
  7026. _nmode="$2"
  7027. if _startswith "$_nmode" "-"; then
  7028. _err "'$_nmode' is not an integer for '$1'"
  7029. return 1
  7030. fi
  7031. _notify_mode="$_nmode"
  7032. shift
  7033. ;;
  7034. --notify-source)
  7035. _nsource="$2"
  7036. if _startswith "$_nsource" "-"; then
  7037. _err "'$_nsource' is not a valid host name for '$1'"
  7038. return 1
  7039. fi
  7040. _notify_source="$_nsource"
  7041. shift
  7042. ;;
  7043. --revoke-reason)
  7044. _revoke_reason="$2"
  7045. if _startswith "$_revoke_reason" "-"; then
  7046. _err "'$_revoke_reason' is not an integer for '$1'"
  7047. return 1
  7048. fi
  7049. shift
  7050. ;;
  7051. --eab-kid)
  7052. _eab_kid="$2"
  7053. shift
  7054. ;;
  7055. --eab-hmac-key)
  7056. _eab_hmac_key="$2"
  7057. shift
  7058. ;;
  7059. --preferred-chain)
  7060. _preferred_chain="$2"
  7061. shift
  7062. ;;
  7063. *)
  7064. _err "Unknown parameter: $1"
  7065. return 1
  7066. ;;
  7067. esac
  7068. shift 1
  7069. done
  7070. if [ "$_server" ]; then
  7071. _selectServer "$_server" "${_ecc:-$_keylength}"
  7072. _server="$ACME_DIRECTORY"
  7073. fi
  7074. if [ "${_CMD}" != "install" ]; then
  7075. if [ "$__INTERACTIVE" ] && ! _checkSudo; then
  7076. if [ -z "$FORCE" ]; then
  7077. #Use "echo" here, instead of _info. it's too early
  7078. echo "It seems that you are using sudo, please read this page first:"
  7079. echo "$_SUDO_WIKI"
  7080. return 1
  7081. fi
  7082. fi
  7083. __initHome
  7084. if [ "$_log" ]; then
  7085. if [ -z "$_logfile" ]; then
  7086. _logfile="$DEFAULT_LOG_FILE"
  7087. fi
  7088. fi
  7089. if [ "$_logfile" ]; then
  7090. _saveaccountconf "LOG_FILE" "$_logfile"
  7091. LOG_FILE="$_logfile"
  7092. fi
  7093. if [ "$_log_level" ]; then
  7094. _saveaccountconf "LOG_LEVEL" "$_log_level"
  7095. LOG_LEVEL="$_log_level"
  7096. fi
  7097. if [ "$_syslog" ]; then
  7098. if _exists logger; then
  7099. if [ "$_syslog" = "0" ]; then
  7100. _clearaccountconf "SYS_LOG"
  7101. else
  7102. _saveaccountconf "SYS_LOG" "$_syslog"
  7103. fi
  7104. SYS_LOG="$_syslog"
  7105. else
  7106. _err "The 'logger' command was not found, cannot enable syslog."
  7107. _clearaccountconf "SYS_LOG"
  7108. SYS_LOG=""
  7109. fi
  7110. fi
  7111. _processAccountConf
  7112. fi
  7113. _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
  7114. if [ "$DEBUG" ]; then
  7115. version
  7116. if [ "$_server" ]; then
  7117. _debug "Using server: $_server"
  7118. fi
  7119. fi
  7120. _debug "Running cmd: ${_CMD}"
  7121. case "${_CMD}" in
  7122. install) install "$_nocron" "$_confighome" "$_noprofile" "$_accountemail" ;;
  7123. uninstall) uninstall "$_nocron" ;;
  7124. upgrade) upgrade ;;
  7125. issue)
  7126. issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias" "$_preferred_chain" "$_valid_from" "$_valid_to" "$_certificate_profile"
  7127. ;;
  7128. deploy)
  7129. deploy "$_domain" "$_deploy_hook" "$_ecc"
  7130. ;;
  7131. signcsr)
  7132. signcsr "$_csr" "$_webroot" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias" "$_preferred_chain"
  7133. ;;
  7134. showcsr)
  7135. showcsr "$_csr" "$_domain"
  7136. ;;
  7137. installcert)
  7138. installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
  7139. ;;
  7140. renew)
  7141. renew "$_domain" "$_ecc" "$_server"
  7142. ;;
  7143. renewAll)
  7144. renewAll "$_stopRenewOnError" "$_server"
  7145. ;;
  7146. revoke)
  7147. revoke "$_domain" "$_ecc" "$_revoke_reason"
  7148. ;;
  7149. remove)
  7150. remove "$_domain" "$_ecc"
  7151. ;;
  7152. deactivate)
  7153. deactivate "$_domain,$_altdomains"
  7154. ;;
  7155. registeraccount)
  7156. registeraccount "$_accountkeylength" "$_eab_kid" "$_eab_hmac_key"
  7157. ;;
  7158. updateaccount)
  7159. updateaccount
  7160. ;;
  7161. deactivateaccount)
  7162. deactivateaccount
  7163. ;;
  7164. list)
  7165. list "$_listraw" "$_domain"
  7166. ;;
  7167. info)
  7168. info "$_domain" "$_ecc"
  7169. ;;
  7170. installcronjob) installcronjob "$_confighome" ;;
  7171. uninstallcronjob) uninstallcronjob ;;
  7172. cron) cron ;;
  7173. toPkcs)
  7174. toPkcs "$_domain" "$_password" "$_ecc"
  7175. ;;
  7176. toPkcs8)
  7177. toPkcs8 "$_domain" "$_ecc"
  7178. ;;
  7179. createAccountKey)
  7180. createAccountKey "$_accountkeylength"
  7181. ;;
  7182. createDomainKey)
  7183. createDomainKey "$_domain" "$_keylength"
  7184. ;;
  7185. createCSR)
  7186. createCSR "$_domain" "$_altdomains" "$_ecc"
  7187. ;;
  7188. setnotify)
  7189. setnotify "$_notify_hook" "$_notify_level" "$_notify_mode" "$_notify_source"
  7190. ;;
  7191. setdefaultca)
  7192. setdefaultca
  7193. ;;
  7194. setdefaultchain)
  7195. setdefaultchain "$_preferred_chain"
  7196. ;;
  7197. list_profiles)
  7198. list_profiles
  7199. ;;
  7200. *)
  7201. if [ "$_CMD" ]; then
  7202. _err "Invalid command: $_CMD"
  7203. fi
  7204. showhelp
  7205. return 1
  7206. ;;
  7207. esac
  7208. _ret="$?"
  7209. if [ "$_ret" != "0" ]; then
  7210. return $_ret
  7211. fi
  7212. if [ "${_CMD}" = "install" ]; then
  7213. if [ "$_log" ]; then
  7214. if [ -z "$LOG_FILE" ]; then
  7215. LOG_FILE="$DEFAULT_LOG_FILE"
  7216. fi
  7217. _saveaccountconf "LOG_FILE" "$LOG_FILE"
  7218. fi
  7219. if [ "$_log_level" ]; then
  7220. _saveaccountconf "LOG_LEVEL" "$_log_level"
  7221. fi
  7222. if [ "$_syslog" ]; then
  7223. if _exists logger; then
  7224. if [ "$_syslog" = "0" ]; then
  7225. _clearaccountconf "SYS_LOG"
  7226. else
  7227. _saveaccountconf "SYS_LOG" "$_syslog"
  7228. fi
  7229. else
  7230. _err "The 'logger' command was not found, cannot enable syslog."
  7231. _clearaccountconf "SYS_LOG"
  7232. SYS_LOG=""
  7233. fi
  7234. fi
  7235. _processAccountConf
  7236. fi
  7237. }
  7238. main() {
  7239. [ -z "$1" ] && showhelp && return
  7240. if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
  7241. }
  7242. main "$@"