acme.sh 112 KB

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