acme.sh 109 KB

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