acme.sh 96 KB

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