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