acme.sh 121 KB

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