acme.sh 111 KB

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