acme.sh 167 KB

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