acme.sh 140 KB

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