acme.sh 223 KB

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