acme.sh 111 KB

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