acme.sh 110 KB

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