acme.sh 122 KB

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