acme.sh 149 KB

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