acme.sh 112 KB

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