acme.sh 111 KB

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