acme.sh 185 KB

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