acme.sh 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185
  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. _mktemp() {
  602. if _exists mktemp ; then
  603. mktemp
  604. fi
  605. }
  606. _inithttp() {
  607. if [ -z "$HTTP_HEADER" ] || ! touch "HTTP_HEADER" ; then
  608. HTTP_HEADER="$(_mktemp)"
  609. _debug2 HTTP_HEADER "$HTTP_HEADER"
  610. fi
  611. if [ -z "$CURL" ] ; then
  612. CURL="curl -L --silent --dump-header $HTTP_HEADER "
  613. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
  614. _CURL_DUMP="$(_mktemp)"
  615. CURL="$CURL --trace-ascii $_CURL_DUMP "
  616. fi
  617. if [ "$HTTPS_INSECURE" ] ; then
  618. CURL="$CURL --insecure "
  619. fi
  620. fi
  621. if [ -z "$WGET" ] ; then
  622. WGET="wget -q"
  623. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
  624. WGET="$WGET -d "
  625. fi
  626. if [ "$HTTPS_INSECURE" ] ; then
  627. WGET="$WGET --no-check-certificate "
  628. fi
  629. fi
  630. }
  631. # body url [needbase64] [POST|PUT]
  632. _post() {
  633. body="$1"
  634. url="$2"
  635. needbase64="$3"
  636. httpmethod="$4"
  637. if [ -z "$httpmethod" ] ; then
  638. httpmethod="POST"
  639. fi
  640. _debug $httpmethod
  641. _debug "url" "$url"
  642. _debug2 "body" "$body"
  643. _inithttp
  644. if _exists "curl" ; then
  645. _CURL="$CURL"
  646. _debug "_CURL" "$_CURL"
  647. if [ "$needbase64" ] ; then
  648. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url" | _base64)"
  649. else
  650. response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url" )"
  651. fi
  652. _ret="$?"
  653. if [ "$_ret" != "0" ] ; then
  654. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
  655. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
  656. _err "Here is the curl dump log:"
  657. _err "$(cat "$_CURL_DUMP")"
  658. fi
  659. fi
  660. elif _exists "wget" ; then
  661. _debug "WGET" "$WGET"
  662. if [ "$needbase64" ] ; then
  663. if [ "$httpmethod"="POST" ] ; then
  664. 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)"
  665. else
  666. 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)"
  667. fi
  668. else
  669. if [ "$httpmethod"="POST" ] ; then
  670. 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")"
  671. else
  672. 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")"
  673. fi
  674. fi
  675. _ret="$?"
  676. if [ "$_ret" != "0" ] ; then
  677. _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
  678. fi
  679. _sed_i "s/^ *//g" "$HTTP_HEADER"
  680. else
  681. _ret="$?"
  682. _err "Neither curl nor wget is found, can not do $httpmethod."
  683. fi
  684. _debug "_ret" "$_ret"
  685. printf "%s" "$response"
  686. return $_ret
  687. }
  688. # url getheader timeout
  689. _get() {
  690. _debug GET
  691. url="$1"
  692. onlyheader="$2"
  693. t="$3"
  694. _debug url $url
  695. _debug "timeout" "$t"
  696. _inithttp
  697. if _exists "curl" ; then
  698. _CURL="$CURL"
  699. if [ "$t" ] ; then
  700. _CURL="$_CURL --connect-timeout $t"
  701. fi
  702. _debug "_CURL" "$_CURL"
  703. if [ "$onlyheader" ] ; then
  704. $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" $url
  705. else
  706. $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" $url
  707. fi
  708. ret=$?
  709. if [ "$ret" != "0" ] ; then
  710. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret"
  711. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
  712. _err "Here is the curl dump log:"
  713. _err "$(cat "$_CURL_DUMP")"
  714. fi
  715. fi
  716. elif _exists "wget" ; then
  717. _WGET="$WGET"
  718. if [ "$t" ] ; then
  719. _WGET="$_WGET --timeout=$t"
  720. fi
  721. _debug "_WGET" "$_WGET"
  722. if [ "$onlyheader" ] ; then
  723. $_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'
  724. else
  725. $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - $url
  726. fi
  727. ret=$?
  728. if [ "$ret" != "0" ] ; then
  729. _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $ret"
  730. fi
  731. else
  732. ret=$?
  733. _err "Neither curl nor wget is found, can not do GET."
  734. fi
  735. _debug "ret" "$ret"
  736. return $ret
  737. }
  738. # url payload needbase64 keyfile
  739. _send_signed_request() {
  740. url=$1
  741. payload=$2
  742. needbase64=$3
  743. keyfile=$4
  744. if [ -z "$keyfile" ] ; then
  745. keyfile="$ACCOUNT_KEY_PATH"
  746. fi
  747. _debug url $url
  748. _debug payload "$payload"
  749. if ! _calcjwk "$keyfile" ; then
  750. return 1
  751. fi
  752. payload64=$(printf "%s" "$payload" | _base64 | _urlencode)
  753. _debug3 payload64 $payload64
  754. nonceurl="$API/directory"
  755. _headers="$(_get $nonceurl "onlyheader")"
  756. if [ "$?" != "0" ] ; then
  757. _err "Can not connect to $nonceurl to get nonce."
  758. return 1
  759. fi
  760. _debug3 _headers "$_headers"
  761. nonce="$( echo "$_headers" | grep "Replay-Nonce:" | head -1 | tr -d "\r\n " | cut -d ':' -f 2)"
  762. _debug3 nonce "$nonce"
  763. protected="$HEADERPLACE_PART1$nonce$HEADERPLACE_PART2"
  764. _debug3 protected "$protected"
  765. protected64="$(printf "$protected" | _base64 | _urlencode)"
  766. _debug3 protected64 "$protected64"
  767. sig=$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256" | _urlencode)
  768. _debug3 sig "$sig"
  769. body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  770. _debug3 body "$body"
  771. response="$(_post "$body" $url "$needbase64")"
  772. if [ "$?" != "0" ] ; then
  773. _err "Can not post to $url."
  774. return 1
  775. fi
  776. _debug2 original "$response"
  777. response="$( echo "$response" | _normalizeJson )"
  778. responseHeaders="$(cat $HTTP_HEADER)"
  779. _debug2 responseHeaders "$responseHeaders"
  780. _debug2 response "$response"
  781. code="$(grep "^HTTP" $HTTP_HEADER | tail -1 | cut -d " " -f 2 | tr -d "\r\n" )"
  782. _debug code $code
  783. }
  784. #setopt "file" "opt" "=" "value" [";"]
  785. _setopt() {
  786. __conf="$1"
  787. __opt="$2"
  788. __sep="$3"
  789. __val="$4"
  790. __end="$5"
  791. if [ -z "$__opt" ] ; then
  792. _usage usage: _setopt '"file" "opt" "=" "value" [";"]'
  793. return
  794. fi
  795. if [ ! -f "$__conf" ] ; then
  796. touch "$__conf"
  797. fi
  798. if grep -n "^$__opt$__sep" "$__conf" > /dev/null ; then
  799. _debug3 OK
  800. if _contains "$__val" "&" ; then
  801. __val="$(echo $__val | sed 's/&/\\&/g')"
  802. fi
  803. text="$(cat $__conf)"
  804. echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf"
  805. elif grep -n "^#$__opt$__sep" "$__conf" > /dev/null ; then
  806. if _contains "$__val" "&" ; then
  807. __val="$(echo $__val | sed 's/&/\\&/g')"
  808. fi
  809. text="$(cat $__conf)"
  810. echo "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf"
  811. else
  812. _debug3 APP
  813. echo "$__opt$__sep$__val$__end" >> "$__conf"
  814. fi
  815. _debug2 "$(grep -n "^$__opt$__sep" $__conf)"
  816. }
  817. #_savedomainconf key value
  818. #save to domain.conf
  819. _savedomainconf() {
  820. key="$1"
  821. value="$2"
  822. if [ "$DOMAIN_CONF" ] ; then
  823. _setopt "$DOMAIN_CONF" "$key" "=" "\"$value\""
  824. else
  825. _err "DOMAIN_CONF is empty, can not save $key=$value"
  826. fi
  827. }
  828. #_cleardomainconf key
  829. _cleardomainconf() {
  830. key="$1"
  831. if [ "$DOMAIN_CONF" ] ; then
  832. _sed_i "s/^$key.*$//" "$DOMAIN_CONF"
  833. else
  834. _err "DOMAIN_CONF is empty, can not save $key=$value"
  835. fi
  836. }
  837. #_readdomainconf key
  838. _readdomainconf() {
  839. key="$1"
  840. if [ "$DOMAIN_CONF" ] ; then
  841. (
  842. eval $(grep "^$key *=" "$DOMAIN_CONF")
  843. eval "printf \"%s\" \"\$$key\""
  844. )
  845. else
  846. _err "DOMAIN_CONF is empty, can not read $key"
  847. fi
  848. }
  849. #_saveaccountconf key value
  850. _saveaccountconf() {
  851. key="$1"
  852. value="$2"
  853. if [ "$ACCOUNT_CONF_PATH" ] ; then
  854. _setopt "$ACCOUNT_CONF_PATH" "$key" "=" "\"$value\""
  855. else
  856. _err "ACCOUNT_CONF_PATH is empty, can not save $key=$value"
  857. fi
  858. }
  859. #_clearaccountconf key
  860. _clearaccountconf() {
  861. key="$1"
  862. if [ "$ACCOUNT_CONF_PATH" ] ; then
  863. _sed_i "s/^$key.*$//" "$ACCOUNT_CONF_PATH"
  864. else
  865. _err "ACCOUNT_CONF_PATH is empty, can not clear $key"
  866. fi
  867. }
  868. _startserver() {
  869. content="$1"
  870. _debug "startserver: $$"
  871. nchelp="$(nc -h 2>&1)"
  872. if echo "$nchelp" | grep "\-q[ ,]" >/dev/null ; then
  873. _NC="nc -q 1 -l"
  874. else
  875. if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null ; then
  876. _NC="nc -c -l"
  877. elif echo "$nchelp" | grep "\-N" |grep "Shutdown the network socket after EOF on stdin" >/dev/null ; then
  878. _NC="nc -N -l"
  879. else
  880. _NC="nc -l"
  881. fi
  882. fi
  883. _debug "_NC" "$_NC"
  884. _debug Le_HTTPPort "$Le_HTTPPort"
  885. # while true ; do
  886. if [ "$DEBUG" ] ; then
  887. if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort ; then
  888. printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort ;
  889. fi
  890. else
  891. if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then
  892. printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1
  893. fi
  894. fi
  895. if [ "$?" != "0" ] ; then
  896. _err "nc listen error."
  897. exit 1
  898. fi
  899. # done
  900. }
  901. _stopserver(){
  902. pid="$1"
  903. _debug "pid" "$pid"
  904. if [ -z "$pid" ] ; then
  905. return
  906. fi
  907. _debug2 "Le_HTTPPort" "$Le_HTTPPort"
  908. if [ "$Le_HTTPPort" ] ; then
  909. if [ "$DEBUG" ] ; then
  910. _get "http://localhost:$Le_HTTPPort" "" 1
  911. else
  912. _get "http://localhost:$Le_HTTPPort" "" 1 >/dev/null 2>&1
  913. fi
  914. fi
  915. _debug2 "Le_TLSPort" "$Le_TLSPort"
  916. if [ "$Le_TLSPort" ] ; then
  917. if [ "$DEBUG" ] ; then
  918. _get "https://localhost:$Le_TLSPort" "" 1
  919. _get "https://localhost:$Le_TLSPort" "" 1
  920. else
  921. _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1
  922. _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1
  923. fi
  924. fi
  925. }
  926. # _starttlsserver san_a san_b port content
  927. _starttlsserver() {
  928. _info "Starting tls server."
  929. san_a="$1"
  930. san_b="$2"
  931. port="$3"
  932. content="$4"
  933. _debug san_a "$san_a"
  934. _debug san_b "$san_b"
  935. _debug port "$port"
  936. #create key TLS_KEY
  937. if ! _createkey "2048" "$TLS_KEY" ; then
  938. _err "Create tls validation key error."
  939. return 1
  940. fi
  941. #create csr
  942. alt="$san_a"
  943. if [ "$san_b" ] ; then
  944. alt="$alt,$san_b"
  945. fi
  946. if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" ; then
  947. _err "Create tls validation csr error."
  948. return 1
  949. fi
  950. #self signed
  951. if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT" ; then
  952. _err "Create tls validation cert error."
  953. return 1
  954. fi
  955. #start openssl
  956. _debug "openssl s_server -cert \"$TLS_CERT\" -key \"$TLS_KEY\" -accept $port -naccept 1 -tlsextdebug"
  957. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
  958. (printf "HTTP/1.1 200 OK\r\n\r\n$content" | openssl s_server -cert "$TLS_CERT" -key "$TLS_KEY" -accept $port -tlsextdebug ) &
  959. else
  960. (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) &
  961. fi
  962. serverproc="$!"
  963. sleep 2
  964. _debug serverproc $serverproc
  965. }
  966. #[domain] [keylength]
  967. _initpath() {
  968. if [ -z "$LE_WORKING_DIR" ] ; then
  969. LE_WORKING_DIR=$HOME/.$PROJECT_NAME
  970. fi
  971. _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf"
  972. if [ -z "$ACCOUNT_CONF_PATH" ] ; then
  973. if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ] ; then
  974. . "$_DEFAULT_ACCOUNT_CONF_PATH"
  975. fi
  976. fi
  977. if [ -z "$ACCOUNT_CONF_PATH" ] ; then
  978. ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
  979. fi
  980. if [ -f "$ACCOUNT_CONF_PATH" ] ; then
  981. . "$ACCOUNT_CONF_PATH"
  982. fi
  983. if [ "$IN_CRON" ] ; then
  984. if [ ! "$_USER_PATH_EXPORTED" ] ; then
  985. _USER_PATH_EXPORTED=1
  986. export PATH="$USER_PATH:$PATH"
  987. fi
  988. fi
  989. if [ -z "$API" ] ; then
  990. if [ -z "$STAGE" ] ; then
  991. API="$DEFAULT_CA"
  992. else
  993. API="$STAGE_CA"
  994. _info "Using stage api:$API"
  995. fi
  996. fi
  997. if [ -z "$ACME_DIR" ] ; then
  998. ACME_DIR="/home/.acme"
  999. fi
  1000. if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then
  1001. APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR"
  1002. fi
  1003. if [ -z "$USER_AGENT" ] ; then
  1004. USER_AGENT="$DEFAULT_USER_AGENT"
  1005. fi
  1006. if [ -z "$HTTP_HEADER" ] ; then
  1007. HTTP_HEADER="$LE_WORKING_DIR/http.header"
  1008. fi
  1009. _DEFAULT_ACCOUNT_KEY_PATH="$LE_WORKING_DIR/account.key"
  1010. if [ -z "$ACCOUNT_KEY_PATH" ] ; then
  1011. ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
  1012. fi
  1013. _DEFAULT_CERT_HOME="$LE_WORKING_DIR"
  1014. if [ -z "$CERT_HOME" ] ; then
  1015. CERT_HOME="$_DEFAULT_CERT_HOME"
  1016. fi
  1017. if [ -z "$1" ] ; then
  1018. return 0
  1019. fi
  1020. domain="$1"
  1021. _ilength="$2"
  1022. if [ -z "$DOMAIN_PATH" ] ; then
  1023. domainhome="$CERT_HOME/$domain"
  1024. domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX"
  1025. DOMAIN_PATH="$domainhome"
  1026. if _isEccKey "$_ilength" ; then
  1027. DOMAIN_PATH="$domainhomeecc"
  1028. else
  1029. if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ] ; then
  1030. _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert."
  1031. fi
  1032. fi
  1033. _debug DOMAIN_PATH "$DOMAIN_PATH"
  1034. fi
  1035. if [ ! -d "$DOMAIN_PATH" ] ; then
  1036. if ! mkdir -p "$DOMAIN_PATH" ; then
  1037. _err "Can not create domain path: $DOMAIN_PATH"
  1038. return 1
  1039. fi
  1040. fi
  1041. if [ -z "$DOMAIN_CONF" ] ; then
  1042. DOMAIN_CONF="$DOMAIN_PATH/$domain.conf"
  1043. fi
  1044. if [ -z "$DOMAIN_SSL_CONF" ] ; then
  1045. DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.ssl.conf"
  1046. fi
  1047. if [ -z "$CSR_PATH" ] ; then
  1048. CSR_PATH="$DOMAIN_PATH/$domain.csr"
  1049. fi
  1050. if [ -z "$CERT_KEY_PATH" ] ; then
  1051. CERT_KEY_PATH="$DOMAIN_PATH/$domain.key"
  1052. fi
  1053. if [ -z "$CERT_PATH" ] ; then
  1054. CERT_PATH="$DOMAIN_PATH/$domain.cer"
  1055. fi
  1056. if [ -z "$CA_CERT_PATH" ] ; then
  1057. CA_CERT_PATH="$DOMAIN_PATH/ca.cer"
  1058. fi
  1059. if [ -z "$CERT_FULLCHAIN_PATH" ] ; then
  1060. CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer"
  1061. fi
  1062. if [ -z "$CERT_PFX_PATH" ] ; then
  1063. CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx"
  1064. fi
  1065. if [ -z "$TLS_CONF" ] ; then
  1066. TLS_CONF="$DOMAIN_PATH/tls.valdation.conf"
  1067. fi
  1068. if [ -z "$TLS_CERT" ] ; then
  1069. TLS_CERT="$DOMAIN_PATH/tls.valdation.cert"
  1070. fi
  1071. if [ -z "$TLS_KEY" ] ; then
  1072. TLS_KEY="$DOMAIN_PATH/tls.valdation.key"
  1073. fi
  1074. if [ -z "$TLS_CSR" ] ; then
  1075. TLS_CSR="$DOMAIN_PATH/tls.valdation.csr"
  1076. fi
  1077. }
  1078. _apachePath() {
  1079. _APACHECTL="apachectl"
  1080. if ! _exists apachectl ; then
  1081. if _exists apache2ctl ; then
  1082. _APACHECTL="apache2ctl"
  1083. else
  1084. _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'"
  1085. _err "Please use webroot mode to try again."
  1086. return 1
  1087. fi
  1088. fi
  1089. httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )"
  1090. _debug httpdconfname "$httpdconfname"
  1091. if _startswith "$httpdconfname" '/' ; then
  1092. httpdconf="$httpdconfname"
  1093. httpdconfname="$(basename $httpdconfname)"
  1094. else
  1095. httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )"
  1096. _debug httpdroot "$httpdroot"
  1097. httpdconf="$httpdroot/$httpdconfname"
  1098. httpdconfname="$(basename $httpdconfname)"
  1099. fi
  1100. _debug httpdconf "$httpdconf"
  1101. _debug httpdconfname "$httpdconfname"
  1102. if [ ! -f "$httpdconf" ] ; then
  1103. _err "Apache Config file not found" "$httpdconf"
  1104. return 1
  1105. fi
  1106. return 0
  1107. }
  1108. _restoreApache() {
  1109. if [ -z "$usingApache" ] ; then
  1110. return 0
  1111. fi
  1112. _initpath
  1113. if ! _apachePath ; then
  1114. return 1
  1115. fi
  1116. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then
  1117. _debug "No config file to restore."
  1118. return 0
  1119. fi
  1120. cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" > "$httpdconf"
  1121. _debug "Restored: $httpdconf."
  1122. if ! $_APACHECTL -t >/dev/null 2>&1 ; then
  1123. _err "Sorry, restore apache config error, please contact me."
  1124. return 1;
  1125. fi
  1126. _debug "Restored successfully."
  1127. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  1128. return 0
  1129. }
  1130. _setApache() {
  1131. _initpath
  1132. if ! _apachePath ; then
  1133. return 1
  1134. fi
  1135. #test the conf first
  1136. _info "Checking if there is an error in the apache config file before starting."
  1137. _msg="$($_APACHECTL -t 2>&1 )"
  1138. if [ "$?" != "0" ] ; then
  1139. _err "Sorry, apache config file has error, please fix it first, then try again."
  1140. _err "Don't worry, there is nothing changed to your system."
  1141. _err "$_msg"
  1142. return 1;
  1143. else
  1144. _info "OK"
  1145. fi
  1146. #backup the conf
  1147. _debug "Backup apache config file" "$httpdconf"
  1148. if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/" ; then
  1149. _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed."
  1150. _err "This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT"
  1151. return 1
  1152. fi
  1153. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  1154. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  1155. _info "The backup file will be deleted on sucess, just forget it."
  1156. #add alias
  1157. apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2 )"
  1158. _debug "apacheVer" "$apacheVer"
  1159. apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
  1160. apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
  1161. if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ] ; then
  1162. echo "
  1163. Alias /.well-known/acme-challenge $ACME_DIR
  1164. <Directory $ACME_DIR >
  1165. Require all granted
  1166. </Directory>
  1167. " >> "$httpdconf"
  1168. else
  1169. echo "
  1170. Alias /.well-known/acme-challenge $ACME_DIR
  1171. <Directory $ACME_DIR >
  1172. Order allow,deny
  1173. Allow from all
  1174. </Directory>
  1175. " >> "$httpdconf"
  1176. fi
  1177. _msg="$($_APACHECTL -t 2>&1 )"
  1178. if [ "$?" != "0" ] ; then
  1179. _err "Sorry, apache config error"
  1180. if _restoreApache ; then
  1181. _err "The apache config file is restored."
  1182. else
  1183. _err "Sorry, The apache config file can not be restored, please report bug."
  1184. fi
  1185. return 1;
  1186. fi
  1187. if [ ! -d "$ACME_DIR" ] ; then
  1188. mkdir -p "$ACME_DIR"
  1189. chmod 755 "$ACME_DIR"
  1190. fi
  1191. if ! $_APACHECTL graceful ; then
  1192. _err "Sorry, $_APACHECTL graceful error, please contact me."
  1193. _restoreApache
  1194. return 1;
  1195. fi
  1196. usingApache="1"
  1197. return 0
  1198. }
  1199. _clearup() {
  1200. _stopserver $serverproc
  1201. serverproc=""
  1202. _restoreApache
  1203. if [ -z "$DEBUG" ] ; then
  1204. rm -f "$TLS_CONF"
  1205. rm -f "$TLS_CERT"
  1206. rm -f "$TLS_KEY"
  1207. rm -f "$TLS_CSR"
  1208. fi
  1209. }
  1210. # webroot removelevel tokenfile
  1211. _clearupwebbroot() {
  1212. __webroot="$1"
  1213. if [ -z "$__webroot" ] ; then
  1214. _debug "no webroot specified, skip"
  1215. return 0
  1216. fi
  1217. _rmpath=""
  1218. if [ "$2" = '1' ] ; then
  1219. _rmpath="$__webroot/.well-known"
  1220. elif [ "$2" = '2' ] ; then
  1221. _rmpath="$__webroot/.well-known/acme-challenge"
  1222. elif [ "$2" = '3' ] ; then
  1223. _rmpath="$__webroot/.well-known/acme-challenge/$3"
  1224. else
  1225. _debug "Skip for removelevel:$2"
  1226. fi
  1227. if [ "$_rmpath" ] ; then
  1228. if [ "$DEBUG" ] ; then
  1229. _debug "Debugging, skip removing: $_rmpath"
  1230. else
  1231. rm -rf "$_rmpath"
  1232. fi
  1233. fi
  1234. return 0
  1235. }
  1236. issue() {
  1237. if [ -z "$2" ] ; then
  1238. _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
  1239. return 1
  1240. fi
  1241. Le_Webroot="$1"
  1242. Le_Domain="$2"
  1243. Le_Alt="$3"
  1244. Le_Keylength="$4"
  1245. Le_RealCertPath="$5"
  1246. Le_RealKeyPath="$6"
  1247. Le_RealCACertPath="$7"
  1248. Le_ReloadCmd="$8"
  1249. Le_RealFullChainPath="$9"
  1250. #remove these later.
  1251. if [ "$Le_Webroot" = "dns-cf" ] ; then
  1252. Le_Webroot="dns_cf"
  1253. fi
  1254. if [ "$Le_Webroot" = "dns-dp" ] ; then
  1255. Le_Webroot="dns_dp"
  1256. fi
  1257. if [ "$Le_Webroot" = "dns-cx" ] ; then
  1258. Le_Webroot="dns_cx"
  1259. fi
  1260. if [ ! "$IS_RENEW" ] ; then
  1261. _initpath $Le_Domain "$Le_Keylength"
  1262. mkdir -p "$DOMAIN_PATH"
  1263. fi
  1264. if [ -f "$DOMAIN_CONF" ] ; then
  1265. Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
  1266. _debug Le_NextRenewTime "$Le_NextRenewTime"
  1267. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ $(date -u "+%s" ) -lt $Le_NextRenewTime ] ; then
  1268. _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)"
  1269. return $RENEW_SKIP
  1270. fi
  1271. fi
  1272. _savedomainconf "Le_Domain" "$Le_Domain"
  1273. _savedomainconf "Le_Alt" "$Le_Alt"
  1274. _savedomainconf "Le_Webroot" "$Le_Webroot"
  1275. if [ "$Le_Alt" = "no" ] ; then
  1276. Le_Alt=""
  1277. fi
  1278. if _hasfield "$Le_Webroot" "no" ; then
  1279. _info "Standalone mode."
  1280. if ! _exists "nc" ; then
  1281. _err "Please install netcat(nc) tools first."
  1282. return 1
  1283. fi
  1284. if [ -z "$Le_HTTPPort" ] ; then
  1285. Le_HTTPPort=80
  1286. else
  1287. _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
  1288. fi
  1289. netprc="$(_ss "$Le_HTTPPort" | grep "$Le_HTTPPort")"
  1290. if [ "$netprc" ] ; then
  1291. _err "$netprc"
  1292. _err "tcp port $Le_HTTPPort is already used by $(echo "$netprc" | cut -d : -f 4)"
  1293. _err "Please stop it first"
  1294. return 1
  1295. fi
  1296. fi
  1297. if _hasfield "$Le_Webroot" "$W_TLS" ; then
  1298. _info "Standalone tls mode."
  1299. if [ -z "$Le_TLSPort" ] ; then
  1300. Le_TLSPort=443
  1301. else
  1302. _savedomainconf "Le_TLSPort" "$Le_TLSPort"
  1303. fi
  1304. netprc="$(_ss "$Le_TLSPort" | grep "$Le_TLSPort")"
  1305. if [ "$netprc" ] ; then
  1306. _err "$netprc"
  1307. _err "tcp port $Le_TLSPort 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" "apache" ; then
  1313. if ! _setApache ; then
  1314. _err "set up apache error. Report error to me."
  1315. return 1
  1316. fi
  1317. else
  1318. usingApache=""
  1319. fi
  1320. if [ ! -f "$ACCOUNT_KEY_PATH" ] ; then
  1321. _acck="no"
  1322. if [ "$Le_Keylength" ] ; then
  1323. _acck="$Le_Keylength"
  1324. fi
  1325. if ! createAccountKey "$_acck" ; then
  1326. _err "Create account key error."
  1327. if [ "$usingApache" ] ; then
  1328. _restoreApache
  1329. fi
  1330. return 1
  1331. fi
  1332. fi
  1333. if ! _calcjwk "$ACCOUNT_KEY_PATH" ; then
  1334. if [ "$usingApache" ] ; then
  1335. _restoreApache
  1336. fi
  1337. return 1
  1338. fi
  1339. accountkey_json=$(printf "%s" "$jwk" | tr -d ' ' )
  1340. thumbprint=$(printf "%s" "$accountkey_json" | _digest "sha256" | _urlencode)
  1341. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  1342. if [ "$ACCOUNT_EMAIL" ] ; then
  1343. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  1344. fi
  1345. accountkeyhash="$(cat "$ACCOUNT_KEY_PATH" | _digest "sha256" )"
  1346. accountkeyhash="$(echo $accountkeyhash$API$regjson | _digest "sha256" )"
  1347. if [ "$accountkeyhash" != "$ACCOUNT_KEY_HASH" ] ; then
  1348. _info "Registering account"
  1349. _send_signed_request "$API/acme/new-reg" "$regjson"
  1350. if [ "$code" = "" ] || [ "$code" = '201' ] ; then
  1351. _info "Registered"
  1352. echo "$response" > $LE_WORKING_DIR/account.json
  1353. elif [ "$code" = '409' ] ; then
  1354. _info "Already registered"
  1355. else
  1356. _err "Register account Error: $response"
  1357. _clearup
  1358. return 1
  1359. fi
  1360. ACCOUNT_KEY_HASH="$accountkeyhash"
  1361. _saveaccountconf "ACCOUNT_KEY_HASH" "$ACCOUNT_KEY_HASH"
  1362. else
  1363. _info "Skip register account key"
  1364. fi
  1365. if [ "$Le_Keylength" = "no" ] ; then
  1366. Le_Keylength=""
  1367. fi
  1368. _key=$(_readdomainconf Le_Keylength)
  1369. _debug "Read key length:$_key"
  1370. if [ ! -f "$CERT_KEY_PATH" ] || [ "$Le_Keylength" != "$_key" ] ; then
  1371. if ! createDomainKey $Le_Domain $Le_Keylength ; then
  1372. _err "Create domain key error."
  1373. _clearup
  1374. return 1
  1375. fi
  1376. fi
  1377. _savedomainconf "Le_Keylength" "$Le_Keylength"
  1378. if ! _createcsr "$Le_Domain" "$Le_Alt" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF" ; then
  1379. _err "Create CSR error."
  1380. _clearup
  1381. return 1
  1382. fi
  1383. vlist="$Le_Vlist"
  1384. # verify each domain
  1385. _info "Verify each domain"
  1386. sep='#'
  1387. if [ -z "$vlist" ] ; then
  1388. alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' )
  1389. _index=1
  1390. _currentRoot=""
  1391. for d in $alldomains
  1392. do
  1393. _info "Getting webroot for domain" $d
  1394. _w="$(echo $Le_Webroot | cut -d , -f $_index)"
  1395. _debug _w "$_w"
  1396. if [ "$_w" ] ; then
  1397. _currentRoot="$_w"
  1398. fi
  1399. _debug "_currentRoot" "$_currentRoot"
  1400. _index=$(_math $_index + 1)
  1401. vtype="$VTYPE_HTTP"
  1402. if _startswith "$_currentRoot" "dns" ; then
  1403. vtype="$VTYPE_DNS"
  1404. fi
  1405. if [ "$_currentRoot" = "$W_TLS" ] ; then
  1406. vtype="$VTYPE_TLS"
  1407. fi
  1408. _info "Getting token for domain" $d
  1409. if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}" ; then
  1410. _err "Can not get domain token."
  1411. _clearup
  1412. return 1
  1413. fi
  1414. if [ ! -z "$code" ] && [ ! "$code" = '201' ] ; then
  1415. _err "new-authz error: $response"
  1416. _clearup
  1417. return 1
  1418. fi
  1419. entry="$(printf "%s\n" "$response" | _egrep_o '[^{]*"type":"'$vtype'"[^}]*')"
  1420. _debug entry "$entry"
  1421. if [ -z "$entry" ] ; then
  1422. _err "Error, can not get domain token $d"
  1423. _clearup
  1424. return 1
  1425. fi
  1426. token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  1427. _debug token $token
  1428. uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )"
  1429. _debug uri $uri
  1430. keyauthorization="$token.$thumbprint"
  1431. _debug keyauthorization "$keyauthorization"
  1432. if printf "$response" | grep '"status":"valid"' >/dev/null 2>&1 ; then
  1433. _info "$d is already verified, skip."
  1434. keyauthorization=$STATE_VERIFIED
  1435. _debug keyauthorization "$keyauthorization"
  1436. fi
  1437. dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
  1438. _debug dvlist "$dvlist"
  1439. vlist="$vlist$dvlist,"
  1440. done
  1441. #add entry
  1442. dnsadded=""
  1443. ventries=$(echo "$vlist" | tr ',' ' ' )
  1444. for ventry in $ventries
  1445. do
  1446. d=$(echo $ventry | cut -d $sep -f 1)
  1447. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  1448. vtype=$(echo $ventry | cut -d $sep -f 4)
  1449. _currentRoot=$(echo $ventry | cut -d $sep -f 5)
  1450. if [ "$keyauthorization" = "$STATE_VERIFIED" ] ; then
  1451. _info "$d is already verified, skip $vtype."
  1452. continue
  1453. fi
  1454. if [ "$vtype" = "$VTYPE_DNS" ] ; then
  1455. dnsadded='0'
  1456. txtdomain="_acme-challenge.$d"
  1457. _debug txtdomain "$txtdomain"
  1458. txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _urlencode)"
  1459. _debug txt "$txt"
  1460. #dns
  1461. #1. check use api
  1462. d_api=""
  1463. if [ -f "$LE_WORKING_DIR/$d/$_currentRoot" ] ; then
  1464. d_api="$LE_WORKING_DIR/$d/$_currentRoot"
  1465. elif [ -f "$LE_WORKING_DIR/$d/$_currentRoot.sh" ] ; then
  1466. d_api="$LE_WORKING_DIR/$d/$_currentRoot.sh"
  1467. elif [ -f "$LE_WORKING_DIR/$_currentRoot" ] ; then
  1468. d_api="$LE_WORKING_DIR/$_currentRoot"
  1469. elif [ -f "$LE_WORKING_DIR/$_currentRoot.sh" ] ; then
  1470. d_api="$LE_WORKING_DIR/$_currentRoot.sh"
  1471. elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot" ] ; then
  1472. d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot"
  1473. elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" ] ; then
  1474. d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot.sh"
  1475. fi
  1476. _debug d_api "$d_api"
  1477. if [ "$d_api" ] ; then
  1478. _info "Found domain api file: $d_api"
  1479. else
  1480. _err "Add the following TXT record:"
  1481. _err "Domain: $txtdomain"
  1482. _err "TXT value: $txt"
  1483. _err "Please be aware that you prepend _acme-challenge. before your domain"
  1484. _err "so the resulting subdomain will be: $txtdomain"
  1485. continue
  1486. fi
  1487. (
  1488. if ! . $d_api ; then
  1489. _err "Load file $d_api error. Please check your api file and try again."
  1490. return 1
  1491. fi
  1492. addcommand="${_currentRoot}_add"
  1493. if ! _exists $addcommand ; then
  1494. _err "It seems that your api file is not correct, it must have a function named: $addcommand"
  1495. return 1
  1496. fi
  1497. if ! $addcommand $txtdomain $txt ; then
  1498. _err "Error add txt for domain:$txtdomain"
  1499. return 1
  1500. fi
  1501. )
  1502. if [ "$?" != "0" ] ; then
  1503. _clearup
  1504. return 1
  1505. fi
  1506. dnsadded='1'
  1507. fi
  1508. done
  1509. if [ "$dnsadded" = '0' ] ; then
  1510. _savedomainconf "Le_Vlist" "$vlist"
  1511. _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
  1512. _err "Please add the TXT records to the domains, and retry again."
  1513. _clearup
  1514. return 1
  1515. fi
  1516. fi
  1517. if [ "$dnsadded" = '1' ] ; then
  1518. if [ -z "$Le_DNSSleep" ] ; then
  1519. Le_DNSSleep=$DEFAULT_DNS_SLEEP
  1520. else
  1521. _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
  1522. fi
  1523. _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
  1524. sleep $Le_DNSSleep
  1525. fi
  1526. _debug "ok, let's start to verify"
  1527. ventries=$(echo "$vlist" | tr ',' ' ' )
  1528. for ventry in $ventries
  1529. do
  1530. d=$(echo $ventry | cut -d $sep -f 1)
  1531. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  1532. uri=$(echo $ventry | cut -d $sep -f 3)
  1533. vtype=$(echo $ventry | cut -d $sep -f 4)
  1534. _currentRoot=$(echo $ventry | cut -d $sep -f 5)
  1535. if [ "$keyauthorization" = "$STATE_VERIFIED" ] ; then
  1536. _info "$d is already verified, skip $vtype."
  1537. continue
  1538. fi
  1539. _info "Verifying:$d"
  1540. _debug "d" "$d"
  1541. _debug "keyauthorization" "$keyauthorization"
  1542. _debug "uri" "$uri"
  1543. removelevel=""
  1544. token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
  1545. _debug "_currentRoot" "$_currentRoot"
  1546. if [ "$vtype" = "$VTYPE_HTTP" ] ; then
  1547. if [ "$_currentRoot" = "no" ] ; then
  1548. _info "Standalone mode server"
  1549. _startserver "$keyauthorization" &
  1550. if [ "$?" != "0" ] ; then
  1551. _clearup
  1552. return 1
  1553. fi
  1554. serverproc="$!"
  1555. sleep 2
  1556. _debug serverproc $serverproc
  1557. else
  1558. if [ "$_currentRoot" = "apache" ] ; then
  1559. wellknown_path="$ACME_DIR"
  1560. else
  1561. wellknown_path="$_currentRoot/.well-known/acme-challenge"
  1562. if [ ! -d "$_currentRoot/.well-known" ] ; then
  1563. removelevel='1'
  1564. elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ] ; then
  1565. removelevel='2'
  1566. else
  1567. removelevel='3'
  1568. fi
  1569. fi
  1570. _debug wellknown_path "$wellknown_path"
  1571. _debug "writing token:$token to $wellknown_path/$token"
  1572. mkdir -p "$wellknown_path"
  1573. printf "%s" "$keyauthorization" > "$wellknown_path/$token"
  1574. if [ ! "$usingApache" ] ; then
  1575. if webroot_owner=$(_stat $_currentRoot) ; then
  1576. _debug "Changing owner/group of .well-known to $webroot_owner"
  1577. chown -R $webroot_owner "$_currentRoot/.well-known"
  1578. else
  1579. _debug "not chaning owner/group of webroot";
  1580. fi
  1581. fi
  1582. fi
  1583. elif [ "$vtype" = "$VTYPE_TLS" ] ; then
  1584. #create A
  1585. #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
  1586. #_debug2 _hash_A "$_hash_A"
  1587. #_x="$(echo $_hash_A | cut -c 1-32)"
  1588. #_debug2 _x "$_x"
  1589. #_y="$(echo $_hash_A | cut -c 33-64)"
  1590. #_debug2 _y "$_y"
  1591. #_SAN_A="$_x.$_y.token.acme.invalid"
  1592. #_debug2 _SAN_A "$_SAN_A"
  1593. #create B
  1594. _hash_B="$(printf "%s" $keyauthorization | _digest "sha256" "hex" )"
  1595. _debug2 _hash_B "$_hash_B"
  1596. _x="$(echo $_hash_B | cut -c 1-32)"
  1597. _debug2 _x "$_x"
  1598. _y="$(echo $_hash_B | cut -c 33-64)"
  1599. _debug2 _y "$_y"
  1600. #_SAN_B="$_x.$_y.ka.acme.invalid"
  1601. _SAN_B="$_x.$_y.acme.invalid"
  1602. _debug2 _SAN_B "$_SAN_B"
  1603. if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" ; then
  1604. _err "Start tls server error."
  1605. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1606. _clearup
  1607. return 1
  1608. fi
  1609. fi
  1610. if ! _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}" ; then
  1611. _err "$d:Can not get challenge: $response"
  1612. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1613. _clearup
  1614. return 1
  1615. fi
  1616. if [ ! -z "$code" ] && [ ! "$code" = '202' ] ; then
  1617. _err "$d:Challenge error: $response"
  1618. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1619. _clearup
  1620. return 1
  1621. fi
  1622. waittimes=0
  1623. if [ -z "$MAX_RETRY_TIMES" ] ; then
  1624. MAX_RETRY_TIMES=30
  1625. fi
  1626. while true ; do
  1627. waittimes=$(_math $waittimes + 1)
  1628. if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ] ; then
  1629. _err "$d:Timeout"
  1630. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1631. _clearup
  1632. return 1
  1633. fi
  1634. _debug "sleep 5 secs to verify"
  1635. sleep 5
  1636. _debug "checking"
  1637. response="$(_get $uri)"
  1638. if [ "$?" != "0" ] ; then
  1639. _err "$d:Verify error:$response"
  1640. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1641. _clearup
  1642. return 1
  1643. fi
  1644. _debug2 original "$response"
  1645. response="$(echo "$response" | _normalizeJson )"
  1646. _debug2 response "$response"
  1647. status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
  1648. if [ "$status" = "valid" ] ; then
  1649. _info "Success"
  1650. _stopserver $serverproc
  1651. serverproc=""
  1652. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1653. break;
  1654. fi
  1655. if [ "$status" = "invalid" ] ; then
  1656. error="$(echo "$response" | _egrep_o '"error":\{[^}]*}')"
  1657. _debug2 error "$error"
  1658. errordetail="$(echo $error | _egrep_o '"detail": *"[^"]*"' | cut -d '"' -f 4)"
  1659. _debug2 errordetail "$errordetail"
  1660. if [ "$errordetail" ] ; then
  1661. _err "$d:Verify error:$errordetail"
  1662. else
  1663. _err "$d:Verify error:$error"
  1664. fi
  1665. if [ "$DEBUG" ] ; then
  1666. if [ "$vtype" = "$VTYPE_HTTP" ] ; then
  1667. _get "http://$d/.well-known/acme-challenge/$token"
  1668. fi
  1669. fi
  1670. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1671. _clearup
  1672. return 1;
  1673. fi
  1674. if [ "$status" = "pending" ] ; then
  1675. _info "Pending"
  1676. else
  1677. _err "$d:Verify error:$response"
  1678. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1679. _clearup
  1680. return 1
  1681. fi
  1682. done
  1683. done
  1684. _clearup
  1685. _info "Verify finished, start to sign."
  1686. der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)"
  1687. if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64" ; then
  1688. _err "Sign failed."
  1689. return 1
  1690. fi
  1691. Le_LinkCert="$(grep -i '^Location.*$' $HTTP_HEADER | head -1 | tr -d "\r\n" | cut -d " " -f 2)"
  1692. _savedomainconf "Le_LinkCert" "$Le_LinkCert"
  1693. if [ "$Le_LinkCert" ] ; then
  1694. echo "$BEGIN_CERT" > "$CERT_PATH"
  1695. _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH"
  1696. echo "$END_CERT" >> "$CERT_PATH"
  1697. _info "$(__green "Cert success.")"
  1698. cat "$CERT_PATH"
  1699. _info "Your cert is in $( __green " $CERT_PATH ")"
  1700. _info "Your cert key is in $( __green " $CERT_KEY_PATH ")"
  1701. cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
  1702. if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ] ; then
  1703. USER_PATH="$PATH"
  1704. _saveaccountconf "USER_PATH" "$USER_PATH"
  1705. fi
  1706. fi
  1707. if [ -z "$Le_LinkCert" ] ; then
  1708. response="$(echo $response | _dbase64 "multiline" | _normalizeJson )"
  1709. _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
  1710. return 1
  1711. fi
  1712. _cleardomainconf "Le_Vlist"
  1713. Le_LinkIssuer=$(grep -i '^Link' $HTTP_HEADER | head -1 | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' )
  1714. if ! _contains "$Le_LinkIssuer" ":" ; then
  1715. Le_LinkIssuer="$API$Le_LinkIssuer"
  1716. fi
  1717. _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
  1718. if [ "$Le_LinkIssuer" ] ; then
  1719. echo "$BEGIN_CERT" > "$CA_CERT_PATH"
  1720. _get "$Le_LinkIssuer" | _base64 "multiline" >> "$CA_CERT_PATH"
  1721. echo "$END_CERT" >> "$CA_CERT_PATH"
  1722. _info "The intermediate CA cert is in $( __green " $CA_CERT_PATH ")"
  1723. cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH"
  1724. _info "And the full chain certs is there: $( __green " $CERT_FULLCHAIN_PATH ")"
  1725. fi
  1726. Le_CertCreateTime=$(date -u "+%s")
  1727. _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
  1728. Le_CertCreateTimeStr=$(date -u )
  1729. _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
  1730. if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ] ; then
  1731. Le_RenewalDays=$MAX_RENEW
  1732. else
  1733. _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
  1734. fi
  1735. if [ "$HTTPS_INSECURE" ] ; then
  1736. _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
  1737. else
  1738. _clearaccountconf "HTTPS_INSECURE"
  1739. fi
  1740. Le_NextRenewTime=$(_math $Le_CertCreateTime + $Le_RenewalDays \* 24 \* 60 \* 60)
  1741. _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
  1742. Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime )
  1743. _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
  1744. if [ "$Le_RealCertPath$Le_RealKeyPath$Le_RealCACertPath$Le_ReloadCmd$Le_RealFullChainPath" ] ; then
  1745. _installcert
  1746. fi
  1747. }
  1748. #domain [isEcc]
  1749. renew() {
  1750. Le_Domain="$1"
  1751. if [ -z "$Le_Domain" ] ; then
  1752. _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
  1753. return 1
  1754. fi
  1755. _isEcc="$2"
  1756. _initpath $Le_Domain "$_isEcc"
  1757. _info "Renew: '$Le_Domain'"
  1758. if [ ! -f "$DOMAIN_CONF" ] ; then
  1759. _info "'$Le_Domain' is not a issued domain, skip."
  1760. return 0;
  1761. fi
  1762. if [ "$Le_RenewalDays" ] ; then
  1763. _savedomainconf Le_RenewalDays "$Le_RenewalDays"
  1764. fi
  1765. . "$DOMAIN_CONF"
  1766. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  1767. _info "Skip, Next renewal time is: $Le_NextRenewTimeStr"
  1768. return $RENEW_SKIP
  1769. fi
  1770. IS_RENEW="1"
  1771. issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath"
  1772. res=$?
  1773. IS_RENEW=""
  1774. return $res
  1775. }
  1776. #renewAll [stopRenewOnError]
  1777. renewAll() {
  1778. _initpath
  1779. _stopRenewOnError="$1"
  1780. _debug "_stopRenewOnError" "$_stopRenewOnError"
  1781. _ret="0"
  1782. for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do
  1783. d=$(echo $d | cut -d '/' -f 1)
  1784. (
  1785. if _endswith $d "$ECC_SUFFIX" ; then
  1786. _isEcc=$(echo $d | cut -d "$ECC_SEP" -f 2)
  1787. d=$(echo $d | cut -d "$ECC_SEP" -f 1)
  1788. fi
  1789. renew "$d" "$_isEcc"
  1790. )
  1791. rc="$?"
  1792. _debug "Return code: $rc"
  1793. if [ "$rc" != "0" ] ; then
  1794. if [ "$rc" = "$RENEW_SKIP" ] ; then
  1795. _info "Skipped $d"
  1796. elif [ "$_stopRenewOnError" ] ; then
  1797. _err "Error renew $d, stop now."
  1798. return $rc
  1799. else
  1800. _ret="$rc"
  1801. _err "Error renew $d, Go ahead to next one."
  1802. fi
  1803. fi
  1804. done
  1805. return $_ret
  1806. }
  1807. list() {
  1808. _raw="$1"
  1809. _initpath
  1810. _sep="|"
  1811. if [ "$_raw" ] ; then
  1812. printf "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew\n"
  1813. for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do
  1814. d=$(echo $d | cut -d '/' -f 1)
  1815. (
  1816. if _endswith $d "$ECC_SUFFIX" ; then
  1817. _isEcc=$(echo $d | cut -d "$ECC_SEP" -f 2)
  1818. d=$(echo $d | cut -d "$ECC_SEP" -f 1)
  1819. fi
  1820. _initpath $d "$_isEcc"
  1821. if [ -f "$DOMAIN_CONF" ] ; then
  1822. . "$DOMAIN_CONF"
  1823. printf "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr\n"
  1824. fi
  1825. )
  1826. done
  1827. else
  1828. if _exists column ; then
  1829. list "raw" | column -t -s "$_sep"
  1830. else
  1831. list "raw" | tr "$_sep" '\t'
  1832. fi
  1833. fi
  1834. }
  1835. installcert() {
  1836. Le_Domain="$1"
  1837. if [ -z "$Le_Domain" ] ; then
  1838. _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]"
  1839. return 1
  1840. fi
  1841. Le_RealCertPath="$2"
  1842. Le_RealKeyPath="$3"
  1843. Le_RealCACertPath="$4"
  1844. Le_ReloadCmd="$5"
  1845. Le_RealFullChainPath="$6"
  1846. _isEcc="$7"
  1847. _initpath $Le_Domain "$_isEcc"
  1848. if [ ! -d "$DOMAIN_PATH" ] ; then
  1849. _err "Domain is not valid:'$Le_Domain'"
  1850. return 1
  1851. fi
  1852. _installcert
  1853. }
  1854. _installcert() {
  1855. _savedomainconf "Le_RealCertPath" "$Le_RealCertPath"
  1856. _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath"
  1857. _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath"
  1858. _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd"
  1859. _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath"
  1860. if [ "$Le_RealCertPath" = "no" ] ; then
  1861. Le_RealCertPath=""
  1862. fi
  1863. if [ "$Le_RealKeyPath" = "no" ] ; then
  1864. Le_RealKeyPath=""
  1865. fi
  1866. if [ "$Le_RealCACertPath" = "no" ] ; then
  1867. Le_RealCACertPath=""
  1868. fi
  1869. if [ "$Le_ReloadCmd" = "no" ] ; then
  1870. Le_ReloadCmd=""
  1871. fi
  1872. if [ "$Le_RealFullChainPath" = "no" ] ; then
  1873. Le_RealFullChainPath=""
  1874. fi
  1875. _installed="0"
  1876. if [ "$Le_RealCertPath" ] ; then
  1877. _installed=1
  1878. _info "Installing cert to:$Le_RealCertPath"
  1879. if [ -f "$Le_RealCertPath" ] && [ ! "$IS_RENEW" ] ; then
  1880. cp "$Le_RealCertPath" "$Le_RealCertPath".bak
  1881. fi
  1882. cat "$CERT_PATH" > "$Le_RealCertPath"
  1883. fi
  1884. if [ "$Le_RealCACertPath" ] ; then
  1885. _installed=1
  1886. _info "Installing CA to:$Le_RealCACertPath"
  1887. if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ] ; then
  1888. echo "" >> "$Le_RealCACertPath"
  1889. cat "$CA_CERT_PATH" >> "$Le_RealCACertPath"
  1890. else
  1891. if [ -f "$Le_RealCACertPath" ] && [ ! "$IS_RENEW" ] ; then
  1892. cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak
  1893. fi
  1894. cat "$CA_CERT_PATH" > "$Le_RealCACertPath"
  1895. fi
  1896. fi
  1897. if [ "$Le_RealKeyPath" ] ; then
  1898. _installed=1
  1899. _info "Installing key to:$Le_RealKeyPath"
  1900. if [ -f "$Le_RealKeyPath" ] && [ ! "$IS_RENEW" ] ; then
  1901. cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak
  1902. fi
  1903. cat "$CERT_KEY_PATH" > "$Le_RealKeyPath"
  1904. fi
  1905. if [ "$Le_RealFullChainPath" ] ; then
  1906. _installed=1
  1907. _info "Installing full chain to:$Le_RealFullChainPath"
  1908. if [ -f "$Le_RealFullChainPath" ] && [ ! "$IS_RENEW" ] ; then
  1909. cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak
  1910. fi
  1911. cat "$CERT_FULLCHAIN_PATH" > "$Le_RealFullChainPath"
  1912. fi
  1913. if [ "$Le_ReloadCmd" ] ; then
  1914. _installed=1
  1915. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  1916. if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd") ; then
  1917. _info "$(__green "Reload success")"
  1918. else
  1919. _err "Reload error for :$Le_Domain"
  1920. fi
  1921. fi
  1922. }
  1923. installcronjob() {
  1924. _initpath
  1925. if ! _exists "crontab" ; then
  1926. _err "crontab doesn't exist, so, we can not install cron jobs."
  1927. _err "All your certs will not be renewed automatically."
  1928. _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
  1929. return 1
  1930. fi
  1931. _info "Installing cron job"
  1932. if ! crontab -l | grep "$PROJECT_ENTRY --cron" ; then
  1933. if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ] ; then
  1934. lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
  1935. else
  1936. _err "Can not install cronjob, $PROJECT_ENTRY not found."
  1937. return 1
  1938. fi
  1939. if _exists uname && uname -a | grep solaris >/dev/null ; then
  1940. crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab --
  1941. else
  1942. crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab -
  1943. fi
  1944. fi
  1945. if [ "$?" != "0" ] ; then
  1946. _err "Install cron job failed. You need to manually renew your certs."
  1947. _err "Or you can add cronjob by yourself:"
  1948. _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
  1949. return 1
  1950. fi
  1951. }
  1952. uninstallcronjob() {
  1953. if ! _exists "crontab" ; then
  1954. return
  1955. fi
  1956. _info "Removing cron job"
  1957. cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
  1958. if [ "$cr" ] ; then
  1959. if _exists uname && uname -a | grep solaris >/dev/null ; then
  1960. crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab --
  1961. else
  1962. crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
  1963. fi
  1964. LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
  1965. _info LE_WORKING_DIR "$LE_WORKING_DIR"
  1966. fi
  1967. _initpath
  1968. }
  1969. revoke() {
  1970. Le_Domain="$1"
  1971. if [ -z "$Le_Domain" ] ; then
  1972. _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com"
  1973. return 1
  1974. fi
  1975. _isEcc="$2"
  1976. _initpath $Le_Domain "$_isEcc"
  1977. if [ ! -f "$DOMAIN_CONF" ] ; then
  1978. _err "$Le_Domain is not a issued domain, skip."
  1979. return 1;
  1980. fi
  1981. if [ ! -f "$CERT_PATH" ] ; then
  1982. _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
  1983. return 1
  1984. fi
  1985. cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}"| tr -d "\r\n" | _urlencode)"
  1986. if [ -z "$cert" ] ; then
  1987. _err "Cert for $Le_Domain is empty found, skip."
  1988. return 1
  1989. fi
  1990. data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
  1991. uri="$API/acme/revoke-cert"
  1992. _info "Try domain key first."
  1993. if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then
  1994. if [ -z "$response" ] ; then
  1995. _info "Revoke success."
  1996. rm -f $CERT_PATH
  1997. return 0
  1998. else
  1999. _err "Revoke error by domain key."
  2000. _err "$response"
  2001. fi
  2002. fi
  2003. _info "Then try account key."
  2004. if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH" ; then
  2005. if [ -z "$response" ] ; then
  2006. _info "Revoke success."
  2007. rm -f $CERT_PATH
  2008. return 0
  2009. else
  2010. _err "Revoke error."
  2011. _debug "$response"
  2012. fi
  2013. fi
  2014. return 1
  2015. }
  2016. # Detect profile file if not specified as environment variable
  2017. _detect_profile() {
  2018. if [ -n "$PROFILE" -a -f "$PROFILE" ] ; then
  2019. echo "$PROFILE"
  2020. return
  2021. fi
  2022. DETECTED_PROFILE=''
  2023. SHELLTYPE="$(basename "/$SHELL")"
  2024. if [ "$SHELLTYPE" = "bash" ] ; then
  2025. if [ -f "$HOME/.bashrc" ] ; then
  2026. DETECTED_PROFILE="$HOME/.bashrc"
  2027. elif [ -f "$HOME/.bash_profile" ] ; then
  2028. DETECTED_PROFILE="$HOME/.bash_profile"
  2029. fi
  2030. elif [ "$SHELLTYPE" = "zsh" ] ; then
  2031. DETECTED_PROFILE="$HOME/.zshrc"
  2032. fi
  2033. if [ -z "$DETECTED_PROFILE" ] ; then
  2034. if [ -f "$HOME/.profile" ] ; then
  2035. DETECTED_PROFILE="$HOME/.profile"
  2036. elif [ -f "$HOME/.bashrc" ] ; then
  2037. DETECTED_PROFILE="$HOME/.bashrc"
  2038. elif [ -f "$HOME/.bash_profile" ] ; then
  2039. DETECTED_PROFILE="$HOME/.bash_profile"
  2040. elif [ -f "$HOME/.zshrc" ] ; then
  2041. DETECTED_PROFILE="$HOME/.zshrc"
  2042. fi
  2043. fi
  2044. if [ ! -z "$DETECTED_PROFILE" ] ; then
  2045. echo "$DETECTED_PROFILE"
  2046. fi
  2047. }
  2048. _initconf() {
  2049. _initpath
  2050. if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
  2051. echo "#ACCOUNT_CONF_PATH=xxxx
  2052. #Account configurations:
  2053. #Here are the supported macros, uncomment them to make them take effect.
  2054. #ACCOUNT_EMAIL=aaa@aaa.com # the account email used to register account.
  2055. #ACCOUNT_KEY_PATH=\"/path/to/account.key\"
  2056. #CERT_HOME=\"/path/to/cert/home\"
  2057. #STAGE=1 # Use the staging api
  2058. #FORCE=1 # Force to issue cert
  2059. #DEBUG=1 # Debug mode
  2060. #ACCOUNT_KEY_HASH=account key hash
  2061. #USER_AGENT=\"$USER_AGENT\"
  2062. #USER_PATH=""
  2063. #dns api
  2064. #######################
  2065. #Cloudflare:
  2066. #api key
  2067. #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
  2068. #account email
  2069. #CF_Email=\"xxxx@sss.com\"
  2070. #######################
  2071. #Dnspod.cn:
  2072. #api key id
  2073. #DP_Id=\"1234\"
  2074. #api key
  2075. #DP_Key=\"sADDsdasdgdsf\"
  2076. #######################
  2077. #Cloudxns.com:
  2078. #CX_Key=\"1234\"
  2079. #
  2080. #CX_Secret=\"sADDsdasdgdsf\"
  2081. #######################
  2082. #Godaddy.com:
  2083. #GD_Key=\"sdfdsgdgdfdasfds\"
  2084. #
  2085. #GD_Secret=\"sADDsdasdfsdfdssdgdsf\"
  2086. " > $ACCOUNT_CONF_PATH
  2087. fi
  2088. }
  2089. # nocron
  2090. _precheck() {
  2091. _nocron="$1"
  2092. if ! _exists "curl" && ! _exists "wget"; then
  2093. _err "Please install curl or wget first, we need to access http resources."
  2094. return 1
  2095. fi
  2096. if [ -z "$_nocron" ] ; then
  2097. if ! _exists "crontab" ; then
  2098. _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
  2099. _err "We need to set cron job to renew the certs automatically."
  2100. _err "Otherwise, your certs will not be able to be renewed automatically."
  2101. if [ -z "$FORCE" ] ; then
  2102. _err "Please add '--force' and try install again to go without crontab."
  2103. _err "./$PROJECT_ENTRY --install --force"
  2104. return 1
  2105. fi
  2106. fi
  2107. fi
  2108. if ! _exists "openssl" ; then
  2109. _err "Please install openssl first."
  2110. _err "We need openssl to generate keys."
  2111. return 1
  2112. fi
  2113. if ! _exists "nc" ; then
  2114. _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
  2115. _err "We use nc for standalone server if you use standalone mode."
  2116. _err "If you don't use standalone mode, just ignore this warning."
  2117. fi
  2118. return 0
  2119. }
  2120. _setShebang() {
  2121. _file="$1"
  2122. _shebang="$2"
  2123. if [ -z "$_shebang" ] ; then
  2124. _usage "Usage: file shebang"
  2125. return 1
  2126. fi
  2127. cp "$_file" "$_file.tmp"
  2128. echo "$_shebang" > "$_file"
  2129. sed -n 2,99999p "$_file.tmp" >> "$_file"
  2130. rm -f "$_file.tmp"
  2131. }
  2132. _installalias() {
  2133. _initpath
  2134. _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  2135. if [ "$_upgrading" ] && [ "$_upgrading" = "1" ] ; then
  2136. echo "$(cat $_envfile)" | sed "s|^LE_WORKING_DIR.*$||" > "$_envfile"
  2137. echo "$(cat $_envfile)" | sed "s|^alias le.*$||" > "$_envfile"
  2138. echo "$(cat $_envfile)" | sed "s|^alias le.sh.*$||" > "$_envfile"
  2139. fi
  2140. _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
  2141. _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
  2142. _profile="$(_detect_profile)"
  2143. if [ "$_profile" ] ; then
  2144. _debug "Found profile: $_profile"
  2145. _setopt "$_profile" ". \"$_envfile\""
  2146. _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
  2147. else
  2148. _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
  2149. fi
  2150. #for csh
  2151. _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
  2152. _csh_profile="$HOME/.cshrc"
  2153. if [ -f "$_csh_profile" ] ; then
  2154. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  2155. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
  2156. _setopt "$_csh_profile" "source \"$_cshfile\""
  2157. fi
  2158. #for tcsh
  2159. _tcsh_profile="$HOME/.tcshrc"
  2160. if [ -f "$_tcsh_profile" ] ; then
  2161. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  2162. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
  2163. _setopt "$_tcsh_profile" "source \"$_cshfile\""
  2164. fi
  2165. }
  2166. # nocron
  2167. install() {
  2168. _nocron="$1"
  2169. if ! _initpath ; then
  2170. _err "Install failed."
  2171. return 1
  2172. fi
  2173. if [ "$_nocron" ] ; then
  2174. _debug "Skip install cron job"
  2175. fi
  2176. if ! _precheck "$_nocron" ; then
  2177. _err "Pre-check failed, can not install."
  2178. return 1
  2179. fi
  2180. #convert from le
  2181. if [ -d "$HOME/.le" ] ; then
  2182. for envfile in "le.env" "le.sh.env"
  2183. do
  2184. if [ -f "$HOME/.le/$envfile" ] ; then
  2185. if grep "le.sh" "$HOME/.le/$envfile" >/dev/null ; then
  2186. _upgrading="1"
  2187. _info "You are upgrading from le.sh"
  2188. _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
  2189. mv "$HOME/.le" "$LE_WORKING_DIR"
  2190. mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  2191. break;
  2192. fi
  2193. fi
  2194. done
  2195. fi
  2196. _info "Installing to $LE_WORKING_DIR"
  2197. if ! mkdir -p "$LE_WORKING_DIR" ; then
  2198. _err "Can not create working dir: $LE_WORKING_DIR"
  2199. return 1
  2200. fi
  2201. chmod 700 "$LE_WORKING_DIR"
  2202. cp $PROJECT_ENTRY "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
  2203. if [ "$?" != "0" ] ; then
  2204. _err "Install failed, can not copy $PROJECT_ENTRY"
  2205. return 1
  2206. fi
  2207. _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
  2208. _installalias
  2209. if [ -d "dnsapi" ] ; then
  2210. mkdir -p $LE_WORKING_DIR/dnsapi
  2211. cp dnsapi/* $LE_WORKING_DIR/dnsapi/
  2212. fi
  2213. if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
  2214. _initconf
  2215. fi
  2216. if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ] ; then
  2217. _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
  2218. fi
  2219. if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ] ; then
  2220. _saveaccountconf "CERT_HOME" "$CERT_HOME"
  2221. fi
  2222. if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ] ; then
  2223. _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
  2224. fi
  2225. if [ -z "$_nocron" ] ; then
  2226. installcronjob
  2227. fi
  2228. if [ -z "$NO_DETECT_SH" ] ; then
  2229. #Modify shebang
  2230. if _exists bash ; then
  2231. _info "Good, bash is installed, change the shebang to use bash as prefered."
  2232. _shebang='#!/usr/bin/env bash'
  2233. _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
  2234. if [ -d "$LE_WORKING_DIR/dnsapi" ] ; then
  2235. for _apifile in $(ls "$LE_WORKING_DIR/dnsapi/"*.sh) ; do
  2236. _setShebang "$_apifile" "$_shebang"
  2237. done
  2238. fi
  2239. fi
  2240. fi
  2241. _info OK
  2242. }
  2243. # nocron
  2244. uninstall() {
  2245. _nocron="$1"
  2246. if [ -z "$_nocron" ] ; then
  2247. uninstallcronjob
  2248. fi
  2249. _initpath
  2250. _profile="$(_detect_profile)"
  2251. if [ "$_profile" ] ; then
  2252. text="$(cat $_profile)"
  2253. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" > "$_profile"
  2254. fi
  2255. _csh_profile="$HOME/.cshrc"
  2256. if [ -f "$_csh_profile" ] ; then
  2257. text="$(cat $_csh_profile)"
  2258. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_csh_profile"
  2259. fi
  2260. _tcsh_profile="$HOME/.tcshrc"
  2261. if [ -f "$_tcsh_profile" ] ; then
  2262. text="$(cat $_tcsh_profile)"
  2263. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_tcsh_profile"
  2264. fi
  2265. rm -f $LE_WORKING_DIR/$PROJECT_ENTRY
  2266. _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
  2267. }
  2268. cron() {
  2269. IN_CRON=1
  2270. renewAll
  2271. _ret="$?"
  2272. IN_CRON=""
  2273. return $_ret
  2274. }
  2275. version() {
  2276. echo "$PROJECT"
  2277. echo "v$VER"
  2278. }
  2279. showhelp() {
  2280. version
  2281. echo "Usage: $PROJECT_ENTRY command ...[parameters]....
  2282. Commands:
  2283. --help, -h Show this help message.
  2284. --version, -v Show version info.
  2285. --install Install $PROJECT_NAME to your system.
  2286. --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
  2287. --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT
  2288. --issue Issue a cert.
  2289. --installcert Install the issued cert to apache/nginx or any other server.
  2290. --renew, -r Renew a cert.
  2291. --renewAll Renew all the certs
  2292. --revoke Revoke a cert.
  2293. --list List all the certs
  2294. --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
  2295. --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
  2296. --cron Run cron job to renew all the certs.
  2297. --toPkcs Export the certificate and key to a pfx file.
  2298. --createAccountKey, -cak Create an account private key, professional use.
  2299. --createDomainKey, -cdk Create an domain private key, professional use.
  2300. --createCSR, -ccsr Create CSR , professional use.
  2301. Parameters:
  2302. --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
  2303. --force, -f Used to force to install or force to renew a cert immediately.
  2304. --staging, --test Use staging server, just for test.
  2305. --debug Output debug info.
  2306. --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
  2307. --standalone Use standalone mode.
  2308. --tls Use standalone tls mode.
  2309. --apache Use apache mode.
  2310. --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
  2311. --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.
  2312. --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
  2313. --accountkeylength, -ak [2048] Specifies the account key length.
  2314. These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
  2315. --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path.
  2316. --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path.
  2317. --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path.
  2318. --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path.
  2319. --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
  2320. --accountconf Specifies a customized account config file.
  2321. --home Specifies the home dir for $PROJECT_NAME .
  2322. --certhome Specifies the home dir to save all the certs, only valid for '--install' command.
  2323. --useragent Specifies the user agent string. it will be saved for future use too.
  2324. --accountemail Specifies the account email for registering, Only valid for the '--install' command.
  2325. --accountkey Specifies the account key path, Only valid for the '--install' command.
  2326. --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
  2327. --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
  2328. --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
  2329. --listraw Only used for '--list' command, list the certs in raw format.
  2330. --stopRenewOnError, -se Only valid for '--renewall' command. Stop if one cert has error in renewal.
  2331. --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
  2332. --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.
  2333. --ecc Specifies to use the ECC cert. Valid for '--installcert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
  2334. "
  2335. }
  2336. # nocron
  2337. _installOnline() {
  2338. _info "Installing from online archive."
  2339. _nocron="$1"
  2340. if [ ! "$BRANCH" ] ; then
  2341. BRANCH="master"
  2342. fi
  2343. target="$PROJECT/archive/$BRANCH.tar.gz"
  2344. _info "Downloading $target"
  2345. localname="$BRANCH.tar.gz"
  2346. if ! _get "$target" > $localname ; then
  2347. _err "Download error."
  2348. return 1
  2349. fi
  2350. (
  2351. _info "Extracting $localname"
  2352. tar xzf $localname
  2353. cd "$PROJECT_NAME-$BRANCH"
  2354. chmod +x $PROJECT_ENTRY
  2355. if ./$PROJECT_ENTRY install "$_nocron" ; then
  2356. _info "Install success!"
  2357. fi
  2358. cd ..
  2359. rm -rf "$PROJECT_NAME-$BRANCH"
  2360. rm -f "$localname"
  2361. )
  2362. }
  2363. upgrade() {
  2364. if (
  2365. cd "$LE_WORKING_DIR"
  2366. _installOnline "nocron"
  2367. ) ; then
  2368. _info "Upgrade success!"
  2369. exit 0
  2370. else
  2371. _err "Upgrade failed!"
  2372. exit 1
  2373. fi
  2374. }
  2375. _process() {
  2376. _CMD=""
  2377. _domain=""
  2378. _altdomains="no"
  2379. _webroot=""
  2380. _keylength=""
  2381. _accountkeylength=""
  2382. _certpath=""
  2383. _keypath=""
  2384. _capath=""
  2385. _fullchainpath=""
  2386. _reloadcmd=""
  2387. _password=""
  2388. _accountconf=""
  2389. _useragent=""
  2390. _accountemail=""
  2391. _accountkey=""
  2392. _certhome=""
  2393. _httpport=""
  2394. _tlsport=""
  2395. _dnssleep=""
  2396. _listraw=""
  2397. _stopRenewOnError=""
  2398. _insecure=""
  2399. _nocron=""
  2400. _ecc=""
  2401. while [ ${#} -gt 0 ] ; do
  2402. case "${1}" in
  2403. --help|-h)
  2404. showhelp
  2405. return
  2406. ;;
  2407. --version|-v)
  2408. version
  2409. return
  2410. ;;
  2411. --install)
  2412. _CMD="install"
  2413. ;;
  2414. --uninstall)
  2415. _CMD="uninstall"
  2416. ;;
  2417. --upgrade)
  2418. _CMD="upgrade"
  2419. ;;
  2420. --issue)
  2421. _CMD="issue"
  2422. ;;
  2423. --installcert|-i)
  2424. _CMD="installcert"
  2425. ;;
  2426. --renew|-r)
  2427. _CMD="renew"
  2428. ;;
  2429. --renewAll|--renewall)
  2430. _CMD="renewAll"
  2431. ;;
  2432. --revoke)
  2433. _CMD="revoke"
  2434. ;;
  2435. --list)
  2436. _CMD="list"
  2437. ;;
  2438. --installcronjob)
  2439. _CMD="installcronjob"
  2440. ;;
  2441. --uninstallcronjob)
  2442. _CMD="uninstallcronjob"
  2443. ;;
  2444. --cron)
  2445. _CMD="cron"
  2446. ;;
  2447. --toPkcs)
  2448. _CMD="toPkcs"
  2449. ;;
  2450. --createAccountKey|--createaccountkey|-cak)
  2451. _CMD="createAccountKey"
  2452. ;;
  2453. --createDomainKey|--createdomainkey|-cdk)
  2454. _CMD="createDomainKey"
  2455. ;;
  2456. --createCSR|--createcsr|-ccr)
  2457. _CMD="createCSR"
  2458. ;;
  2459. --domain|-d)
  2460. _dvalue="$2"
  2461. if [ "$_dvalue" ] ; then
  2462. if _startswith "$_dvalue" "-" ; then
  2463. _err "'$_dvalue' is not a valid domain for parameter '$1'"
  2464. return 1
  2465. fi
  2466. if [ -z "$_domain" ] ; then
  2467. _domain="$_dvalue"
  2468. else
  2469. if [ "$_altdomains" = "no" ] ; then
  2470. _altdomains="$_dvalue"
  2471. else
  2472. _altdomains="$_altdomains,$_dvalue"
  2473. fi
  2474. fi
  2475. fi
  2476. shift
  2477. ;;
  2478. --force|-f)
  2479. FORCE="1"
  2480. ;;
  2481. --staging|--test)
  2482. STAGE="1"
  2483. ;;
  2484. --debug)
  2485. if [ -z "$2" ] || _startswith "$2" "-" ; then
  2486. DEBUG="1"
  2487. else
  2488. DEBUG="$2"
  2489. shift
  2490. fi
  2491. ;;
  2492. --webroot|-w)
  2493. wvalue="$2"
  2494. if [ -z "$_webroot" ] ; then
  2495. _webroot="$wvalue"
  2496. else
  2497. _webroot="$_webroot,$wvalue"
  2498. fi
  2499. shift
  2500. ;;
  2501. --standalone)
  2502. wvalue="no"
  2503. if [ -z "$_webroot" ] ; then
  2504. _webroot="$wvalue"
  2505. else
  2506. _webroot="$_webroot,$wvalue"
  2507. fi
  2508. ;;
  2509. --apache)
  2510. wvalue="apache"
  2511. if [ -z "$_webroot" ] ; then
  2512. _webroot="$wvalue"
  2513. else
  2514. _webroot="$_webroot,$wvalue"
  2515. fi
  2516. ;;
  2517. --tls)
  2518. wvalue="$W_TLS"
  2519. if [ -z "$_webroot" ] ; then
  2520. _webroot="$wvalue"
  2521. else
  2522. _webroot="$_webroot,$wvalue"
  2523. fi
  2524. ;;
  2525. --dns)
  2526. wvalue="dns"
  2527. if ! _startswith "$2" "-" ; then
  2528. wvalue="$2"
  2529. shift
  2530. fi
  2531. if [ -z "$_webroot" ] ; then
  2532. _webroot="$wvalue"
  2533. else
  2534. _webroot="$_webroot,$wvalue"
  2535. fi
  2536. ;;
  2537. --dnssleep)
  2538. _dnssleep="$2"
  2539. Le_DNSSleep="$_dnssleep"
  2540. shift
  2541. ;;
  2542. --keylength|-k)
  2543. _keylength="$2"
  2544. if [ "$_accountkeylength" = "no" ] ; then
  2545. _accountkeylength="$2"
  2546. fi
  2547. shift
  2548. ;;
  2549. --accountkeylength|-ak)
  2550. _accountkeylength="$2"
  2551. shift
  2552. ;;
  2553. --certpath)
  2554. _certpath="$2"
  2555. shift
  2556. ;;
  2557. --keypath)
  2558. _keypath="$2"
  2559. shift
  2560. ;;
  2561. --capath)
  2562. _capath="$2"
  2563. shift
  2564. ;;
  2565. --fullchainpath)
  2566. _fullchainpath="$2"
  2567. shift
  2568. ;;
  2569. --reloadcmd|--reloadCmd)
  2570. _reloadcmd="$2"
  2571. shift
  2572. ;;
  2573. --password)
  2574. _password="$2"
  2575. shift
  2576. ;;
  2577. --accountconf)
  2578. _accountconf="$2"
  2579. ACCOUNT_CONF_PATH="$_accountconf"
  2580. shift
  2581. ;;
  2582. --home)
  2583. LE_WORKING_DIR="$2"
  2584. shift
  2585. ;;
  2586. --certhome)
  2587. _certhome="$2"
  2588. CERT_HOME="$_certhome"
  2589. shift
  2590. ;;
  2591. --useragent)
  2592. _useragent="$2"
  2593. USER_AGENT="$_useragent"
  2594. shift
  2595. ;;
  2596. --accountemail )
  2597. _accountemail="$2"
  2598. ACCOUNT_EMAIL="$_accountemail"
  2599. shift
  2600. ;;
  2601. --accountkey )
  2602. _accountkey="$2"
  2603. ACCOUNT_KEY_PATH="$_accountkey"
  2604. shift
  2605. ;;
  2606. --days )
  2607. _days="$2"
  2608. Le_RenewalDays="$_days"
  2609. shift
  2610. ;;
  2611. --httpport )
  2612. _httpport="$2"
  2613. Le_HTTPPort="$_httpport"
  2614. shift
  2615. ;;
  2616. --tlsport )
  2617. _tlsport="$2"
  2618. Le_TLSPort="$_tlsport"
  2619. shift
  2620. ;;
  2621. --listraw )
  2622. _listraw="raw"
  2623. ;;
  2624. --stopRenewOnError|--stoprenewonerror|-se )
  2625. _stopRenewOnError="1"
  2626. ;;
  2627. --insecure)
  2628. _insecure="1"
  2629. HTTPS_INSECURE="1"
  2630. ;;
  2631. --nocron)
  2632. _nocron="1"
  2633. ;;
  2634. --ecc)
  2635. _ecc="isEcc"
  2636. ;;
  2637. *)
  2638. _err "Unknown parameter : $1"
  2639. return 1
  2640. ;;
  2641. esac
  2642. shift 1
  2643. done
  2644. if [ "$DEBUG" ] ; then
  2645. version
  2646. fi
  2647. case "${_CMD}" in
  2648. install) install "$_nocron" ;;
  2649. uninstall) uninstall "$_nocron" ;;
  2650. upgrade) upgrade ;;
  2651. issue)
  2652. issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath"
  2653. ;;
  2654. installcert)
  2655. installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_ecc"
  2656. ;;
  2657. renew)
  2658. renew "$_domain" "$_ecc"
  2659. ;;
  2660. renewAll)
  2661. renewAll "$_stopRenewOnError"
  2662. ;;
  2663. revoke)
  2664. revoke "$_domain" "$_ecc"
  2665. ;;
  2666. list)
  2667. list "$_listraw"
  2668. ;;
  2669. installcronjob) installcronjob ;;
  2670. uninstallcronjob) uninstallcronjob ;;
  2671. cron) cron ;;
  2672. toPkcs)
  2673. toPkcs "$_domain" "$_password" "$_ecc"
  2674. ;;
  2675. createAccountKey)
  2676. createAccountKey "$_accountkeylength"
  2677. ;;
  2678. createDomainKey)
  2679. createDomainKey "$_domain" "$_keylength"
  2680. ;;
  2681. createCSR)
  2682. createCSR "$_domain" "$_altdomains" "$_ecc"
  2683. ;;
  2684. *)
  2685. _err "Invalid command: $_CMD"
  2686. showhelp;
  2687. return 1
  2688. ;;
  2689. esac
  2690. _ret="$?"
  2691. if [ "$_ret" != "0" ] ; then
  2692. return $_ret
  2693. fi
  2694. if [ "$_useragent" ] ; then
  2695. _saveaccountconf "USER_AGENT" "$_useragent"
  2696. fi
  2697. if [ "$_accountemail" ] ; then
  2698. _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
  2699. fi
  2700. }
  2701. if [ "$INSTALLONLINE" ] ; then
  2702. INSTALLONLINE=""
  2703. _installOnline $BRANCH
  2704. exit
  2705. fi
  2706. if [ -z "$1" ] ; then
  2707. showhelp
  2708. else
  2709. if echo "$1" | grep "^-" >/dev/null 2>&1 ; then
  2710. _process "$@"
  2711. else
  2712. "$@"
  2713. fi
  2714. fi