acme.sh 162 KB

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