acme.sh 80 KB

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