acme.sh 148 KB

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