acme.sh 111 KB

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