You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

961 lines
24 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/bin/bash
  2. VER=1.1.0
  3. PROJECT="https://github.com/Neilpang/le"
  4. DEFAULT_CA="https://acme-v01.api.letsencrypt.org"
  5. DEFAULT_AGREEMENT="https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf"
  6. STAGE_CA="https://acme-staging.api.letsencrypt.org"
  7. VTYPE_HTTP="http-01"
  8. VTYPE_DNS="dns-01"
  9. if [ -z "$AGREEMENT" ] ; then
  10. AGREEMENT="$DEFAULT_AGREEMENT"
  11. fi
  12. _debug() {
  13. if [ -z "$DEBUG" ] ; then
  14. return
  15. fi
  16. if [ -z "$2" ] ; then
  17. echo $1
  18. else
  19. echo "$1"="$2"
  20. fi
  21. }
  22. _info() {
  23. if [ -z "$2" ] ; then
  24. echo "$1"
  25. else
  26. echo "$1"="$2"
  27. fi
  28. }
  29. _err() {
  30. if [ -z "$2" ] ; then
  31. echo "$1" >&2
  32. else
  33. echo "$1"="$2" >&2
  34. fi
  35. }
  36. #domain [2048]
  37. createAccountKey() {
  38. if [ -z "$1" ] ; then
  39. echo Usage: $0 account-domain [2048]
  40. return
  41. fi
  42. account=$1
  43. length=$2
  44. if [ -z "$2" ] ; then
  45. _info "Use default length 2048"
  46. length=2048
  47. fi
  48. _initpath
  49. if [ -f "$ACCOUNT_KEY_PATH" ] ; then
  50. _info "Account key exists, skip"
  51. return
  52. else
  53. #generate account key
  54. openssl genrsa $length > "$ACCOUNT_KEY_PATH"
  55. fi
  56. }
  57. #domain length
  58. createDomainKey() {
  59. if [ -z "$1" ] ; then
  60. echo Usage: $0 domain [2048]
  61. return
  62. fi
  63. domain=$1
  64. length=$2
  65. if [ -z "$2" ] ; then
  66. _info "Use default length 2048"
  67. length=2048
  68. fi
  69. _initpath $domain
  70. if [ -f "$CERT_KEY_PATH" ] && ! [ "$FORCE" ] ; then
  71. if [ "$IS_RENEW" ] ; then
  72. _info "Domain key exists, skip"
  73. return 0
  74. else
  75. _err "Domain key exists, do you want to overwrite the key?"
  76. _err "Set FORCE=1, and try again."
  77. return 1
  78. fi
  79. else
  80. #generate account key
  81. openssl genrsa $length > "$CERT_KEY_PATH"
  82. fi
  83. }
  84. # domain domainlist
  85. createCSR() {
  86. if [ -z "$1" ] ; then
  87. echo Usage: $0 domain [domainlist]
  88. return
  89. fi
  90. domain=$1
  91. _initpath $domain
  92. domainlist=$2
  93. if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && ! [ "$FORCE" ]; then
  94. _info "CSR exists, skip"
  95. return
  96. fi
  97. if [ -z "$domainlist" ] ; then
  98. #single domain
  99. _info "Single domain" $domain
  100. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" > "$CSR_PATH"
  101. else
  102. alt="DNS:$(echo $domainlist | sed "s/,/,DNS:/g")"
  103. #multi
  104. _info "Multi domain" "$alt"
  105. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" -reqexts SAN -config <(printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n[SAN]\nsubjectAltName=$alt") -out "$CSR_PATH"
  106. fi
  107. }
  108. _b64() {
  109. __n=$(cat)
  110. echo $__n | tr '/+' '_-' | tr -d '= '
  111. }
  112. _send_signed_request() {
  113. url=$1
  114. payload=$2
  115. needbase64=$3
  116. _debug url $url
  117. _debug payload "$payload"
  118. CURL_HEADER="$WORKING_DIR/curl.header"
  119. dp="$WORKING_DIR/curl.dump"
  120. CURL="curl --silent --dump-header $CURL_HEADER "
  121. if [ "$DEBUG" ] ; then
  122. CURL="$CURL --trace-ascii $dp "
  123. fi
  124. payload64=$(echo -n $payload | base64 -w 0 | _b64)
  125. _debug payload64 $payload64
  126. nonceurl="$API/directory"
  127. nonce=$($CURL -I $nonceurl | grep "^Replay-Nonce:" | sed s/\\r//|sed s/\\n//| cut -d ' ' -f 2)
  128. _debug nonce $nonce
  129. protected=$(echo -n "$HEADERPLACE" | sed "s/NONCE/$nonce/" )
  130. _debug protected "$protected"
  131. protected64=$( echo -n $protected | base64 -w 0 | _b64)
  132. _debug protected64 "$protected64"
  133. sig=$(echo -n "$protected64.$payload64" | openssl dgst -sha256 -sign $ACCOUNT_KEY_PATH | base64 -w 0 | _b64)
  134. _debug sig "$sig"
  135. body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  136. _debug body "$body"
  137. if [ "$needbase64" ] ; then
  138. response="$($CURL -X POST --data "$body" $url | base64 -w 0)"
  139. else
  140. response="$($CURL -X POST --data "$body" $url)"
  141. fi
  142. responseHeaders="$(sed 's/\r//g' $CURL_HEADER)"
  143. _debug responseHeaders "$responseHeaders"
  144. _debug response "$response"
  145. code="$(grep ^HTTP $CURL_HEADER | tail -1 | cut -d " " -f 2)"
  146. _debug code $code
  147. }
  148. _get() {
  149. url="$1"
  150. _debug url $url
  151. response="$(curl --silent $url)"
  152. ret=$?
  153. _debug response "$response"
  154. code="$(echo $response | grep -o '"status":[0-9]\+' | cut -d : -f 2)"
  155. _debug code $code
  156. return $ret
  157. }
  158. #setopt "file" "opt" "=" "value" [";"]
  159. _setopt() {
  160. __conf="$1"
  161. __opt="$2"
  162. __sep="$3"
  163. __val="$4"
  164. __end="$5"
  165. if [ -z "$__opt" ] ; then
  166. echo usage: $0 '"file" "opt" "=" "value" [";"]'
  167. return
  168. fi
  169. if [ ! -f "$__conf" ] ; then
  170. touch "$__conf"
  171. fi
  172. if grep -H -n "^$__opt$__sep" "$__conf" > /dev/null ; then
  173. _debug OK
  174. if [[ "$__val" == *"&"* ]] ; then
  175. __val="$(echo $__val | sed 's/&/\\&/g')"
  176. fi
  177. sed -i "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" "$__conf"
  178. else
  179. _debug APP
  180. echo "$__opt$__sep$__val$__end" >> "$__conf"
  181. fi
  182. _debug "$(grep -H -n "^$__opt$__sep" $__conf)"
  183. }
  184. _startserver() {
  185. content="$1"
  186. # while true ; do
  187. if [ "$DEBUG" ] ; then
  188. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | nc -q 1 -l -p 80
  189. else
  190. echo -e -n "HTTP/1.1 200 OK\r\n\r\n$content" | nc -q 1 -l -p 80 > /dev/null
  191. fi
  192. # done
  193. }
  194. _stopserver() {
  195. pid="$1"
  196. }
  197. _initpath() {
  198. if [ -z "$API" ] ; then
  199. if [ -z "$STAGE" ] ; then
  200. API="$DEFAULT_CA"
  201. else
  202. API="$STAGE_CA"
  203. _info "Using stage api:$API"
  204. fi
  205. fi
  206. if [ -z "$WORKING_DIR" ]; then
  207. WORKING_DIR=$HOME/.le
  208. fi
  209. if [ -z "$ACME_DIR" ] ; then
  210. ACME_DIR="/home/.acme"
  211. fi
  212. if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then
  213. APACHE_CONF_BACKUP_DIR="$WORKING_DIR/"
  214. fi
  215. domain="$1"
  216. mkdir -p "$WORKING_DIR"
  217. if [ -z "$ACCOUNT_KEY_PATH" ] ; then
  218. ACCOUNT_KEY_PATH="$WORKING_DIR/account.acc"
  219. fi
  220. if [ -z "$domain" ] ; then
  221. return 0
  222. fi
  223. mkdir -p "$WORKING_DIR/$domain"
  224. if [ -z "$DOMAIN_CONF" ] ; then
  225. DOMAIN_CONF="$WORKING_DIR/$domain/$Le_Domain.conf"
  226. fi
  227. if [ -z "$CSR_PATH" ] ; then
  228. CSR_PATH="$WORKING_DIR/$domain/$domain.csr"
  229. fi
  230. if [ -z "$CERT_KEY_PATH" ] ; then
  231. CERT_KEY_PATH="$WORKING_DIR/$domain/$domain.key"
  232. fi
  233. if [ -z "$CERT_PATH" ] ; then
  234. CERT_PATH="$WORKING_DIR/$domain/$domain.cer"
  235. fi
  236. if [ -z "$CA_CERT_PATH" ] ; then
  237. CA_CERT_PATH="$WORKING_DIR/$domain/ca.cer"
  238. fi
  239. }
  240. _apachePath() {
  241. httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | sed s/\"//g)"
  242. httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | sed s/\"//g)"
  243. httpdconf="$httpdroot/$httpdconfname"
  244. if [ ! -f $httpdconf ] ; then
  245. _err "Apache Config file not found" $httpdconf
  246. return 1
  247. fi
  248. return 0
  249. }
  250. _restoreApache() {
  251. if [ -z "$usingApache" ] ; then
  252. return 0
  253. fi
  254. _initpath
  255. if ! _apachePath ; then
  256. return 1
  257. fi
  258. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then
  259. _debug "No config file to restore."
  260. return 0
  261. fi
  262. cp -p "$APACHE_CONF_BACKUP_DIR/$httpdconfname" "$httpdconf"
  263. if ! apachectl -t ; then
  264. _err "Sorry, restore apache config error, please contact me."
  265. return 1;
  266. fi
  267. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  268. return 0
  269. }
  270. _setApache() {
  271. _initpath
  272. if ! _apachePath ; then
  273. return 1
  274. fi
  275. #backup the conf
  276. _debug "Backup apache config file" $httpdconf
  277. cp -p $httpdconf $APACHE_CONF_BACKUP_DIR/
  278. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  279. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  280. _info "The backup file will be deleted on sucess, just forget it."
  281. #add alias
  282. echo "
  283. Alias /.well-known/acme-challenge $ACME_DIR
  284. <Directory $ACME_DIR >
  285. Require all granted
  286. </Directory>
  287. " >> $httpdconf
  288. if ! apachectl -t ; then
  289. _err "Sorry, apache config error, please contact me."
  290. _restoreApache
  291. return 1;
  292. fi
  293. if [ ! -d "$ACME_DIR" ] ; then
  294. mkdir -p "$ACME_DIR"
  295. chmod 755 "$ACME_DIR"
  296. fi
  297. if ! apachectl graceful ; then
  298. _err "Sorry, apachectl graceful error, please contact me."
  299. _restoreApache
  300. return 1;
  301. fi
  302. usingApache="1"
  303. return 0
  304. }
  305. _clearup () {
  306. _stopserver $serverproc
  307. serverproc=""
  308. _restoreApache
  309. }
  310. # webroot removelevel tokenfile
  311. _clearupwebbroot() {
  312. __webroot="$1"
  313. if [ -z "$__webroot" ] ; then
  314. _debug "no webroot specified, skip"
  315. return 0
  316. fi
  317. if [ "$2" == '1' ] ; then
  318. _debug "remove $__webroot/.well-known"
  319. rm -rf "$__webroot/.well-known"
  320. elif [ "$2" == '2' ] ; then
  321. _debug "remove $__webroot/.well-known/acme-challenge"
  322. rm -rf "$__webroot/.well-known/acme-challenge"
  323. elif [ "$2" == '3' ] ; then
  324. _debug "remove $__webroot/.well-known/acme-challenge/$3"
  325. rm -rf "$__webroot/.well-known/acme-challenge/$3"
  326. else
  327. _err "removelevel invalid: $2"
  328. return 1
  329. fi
  330. return 0
  331. }
  332. issue() {
  333. if [ -z "$2" ] ; then
  334. _err "Usage: le issue webroot|no|apache|dns a.com [www.a.com,b.com,c.com]|no [key-length]|no [cert-file-path]|no [key-file-path]|no [ca-cert-file-path]|no [reloadCmd]|no"
  335. return 1
  336. fi
  337. Le_Webroot="$1"
  338. Le_Domain="$2"
  339. Le_Alt="$3"
  340. Le_Keylength="$4"
  341. Le_RealCertPath="$5"
  342. Le_RealKeyPath="$6"
  343. Le_RealCACertPath="$7"
  344. Le_ReloadCmd="$8"
  345. _initpath $Le_Domain
  346. if [ -f "$DOMAIN_CONF" ] ; then
  347. Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2)
  348. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  349. _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)"
  350. return 2
  351. fi
  352. fi
  353. if [ "$Le_Alt" == "no" ] ; then
  354. Le_Alt=""
  355. fi
  356. if [ "$Le_Keylength" == "no" ] ; then
  357. Le_Keylength=""
  358. fi
  359. if [ "$Le_RealCertPath" == "no" ] ; then
  360. Le_RealCertPath=""
  361. fi
  362. if [ "$Le_RealKeyPath" == "no" ] ; then
  363. Le_RealKeyPath=""
  364. fi
  365. if [ "$Le_RealCACertPath" == "no" ] ; then
  366. Le_RealCACertPath=""
  367. fi
  368. if [ "$Le_ReloadCmd" == "no" ] ; then
  369. Le_ReloadCmd=""
  370. fi
  371. _setopt "$DOMAIN_CONF" "Le_Domain" "=" "$Le_Domain"
  372. _setopt "$DOMAIN_CONF" "Le_Alt" "=" "$Le_Alt"
  373. _setopt "$DOMAIN_CONF" "Le_Webroot" "=" "$Le_Webroot"
  374. _setopt "$DOMAIN_CONF" "Le_Keylength" "=" "$Le_Keylength"
  375. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  376. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  377. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  378. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  379. if [ "$Le_Webroot" == "no" ] ; then
  380. _info "Standalone mode."
  381. if ! command -v "nc" > /dev/null ; then
  382. _err "Please install netcat(nc) tools first."
  383. return 1
  384. fi
  385. netprc="$(ss -ntpl | grep ':80 ')"
  386. if [ "$netprc" ] ; then
  387. _err "$netprc"
  388. _err "tcp port 80 is already used by $(echo "$netprc" | cut -d : -f 4)"
  389. _err "Please stop it first"
  390. return 1
  391. fi
  392. fi
  393. if [ "$Le_Webroot" == "apache" ] ; then
  394. if ! _setApache ; then
  395. _err "set up apache error. Report error to me."
  396. return 1
  397. fi
  398. wellknown_path="$ACME_DIR"
  399. else
  400. usingApache=""
  401. fi
  402. createAccountKey $Le_Domain $Le_Keylength
  403. if ! createDomainKey $Le_Domain $Le_Keylength ; then
  404. _err "Create domain key error."
  405. return 1
  406. fi
  407. if ! createCSR $Le_Domain $Le_Alt ; then
  408. _err "Create CSR error."
  409. return 1
  410. fi
  411. pub_exp=$(openssl rsa -in $ACCOUNT_KEY_PATH -noout -text | grep "^publicExponent:"| cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1)
  412. if [ "${#pub_exp}" == "5" ] ; then
  413. pub_exp=0$pub_exp
  414. fi
  415. _debug pub_exp "$pub_exp"
  416. e=$(echo $pub_exp | xxd -r -p | base64)
  417. _debug e "$e"
  418. modulus=$(openssl rsa -in $ACCOUNT_KEY_PATH -modulus -noout | cut -d '=' -f 2 )
  419. n=$(echo $modulus| xxd -r -p | base64 -w 0 | _b64 )
  420. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  421. HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  422. HEADERPLACE='{"nonce": "NONCE", "alg": "RS256", "jwk": '$jwk'}'
  423. _debug HEADER "$HEADER"
  424. accountkey_json=$(echo -n "$jwk" | sed "s/ //g")
  425. thumbprint=$(echo -n "$accountkey_json" | sha256sum | xxd -r -p | base64 -w 0 | _b64)
  426. _info "Registering account"
  427. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  428. if [ "$ACCOUNT_EMAIL" ] ; then
  429. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  430. fi
  431. _send_signed_request "$API/acme/new-reg" "$regjson"
  432. if [ "$code" == "" ] || [ "$code" == '201' ] ; then
  433. _info "Registered"
  434. echo $response > $WORKING_DIR/account.json
  435. elif [ "$code" == '409' ] ; then
  436. _info "Already registered"
  437. else
  438. _err "Register account Error."
  439. _clearup
  440. return 1
  441. fi
  442. vtype="$VTYPE_HTTP"
  443. if [[ "$Le_Webroot" == "dns"* ]] ; then
  444. vtype="$VTYPE_DNS"
  445. fi
  446. vlist="$Le_Vlist"
  447. # verify each domain
  448. _info "Verify each domain"
  449. sep='#'
  450. if [ -z "$vlist" ] ; then
  451. alldomains=$(echo "$Le_Domain,$Le_Alt" | sed "s/,/ /g")
  452. for d in $alldomains
  453. do
  454. _info "Geting token for domain" $d
  455. _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}"
  456. if [ ! -z "$code" ] && [ ! "$code" == '201' ] ; then
  457. _err "new-authz error: $response"
  458. _clearup
  459. return 1
  460. fi
  461. entry=$(echo $response | egrep -o '{[^{]*"type":"'$vtype'"[^}]*')
  462. _debug entry "$entry"
  463. token=$(echo "$entry" | sed 's/,/\n'/g| grep '"token":'| cut -d : -f 2|sed 's/"//g')
  464. _debug token $token
  465. uri=$(echo "$entry" | sed 's/,/\n'/g| grep '"uri":'| cut -d : -f 2,3|sed 's/"//g')
  466. _debug uri $uri
  467. keyauthorization="$token.$thumbprint"
  468. _debug keyauthorization "$keyauthorization"
  469. dvlist="$d$sep$keyauthorization$sep$uri"
  470. _debug dvlist "$dvlist"
  471. vlist="$vlist$dvlist,"
  472. done
  473. #add entry
  474. dnsadded=""
  475. ventries=$(echo "$vlist" | sed "s/,/ /g")
  476. for ventry in $ventries
  477. do
  478. d=$(echo $ventry | cut -d $sep -f 1)
  479. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  480. if [ "$vtype" == "$VTYPE_DNS" ] ; then
  481. dnsadded='0'
  482. txtdomain="_acme-challenge.$d"
  483. _debug txtdomain "$txtdomain"
  484. txt="$(echo -e -n $keyauthorization | sha256sum | xxd -r -p | base64 -w 0 | _b64)"
  485. _debug txt "$txt"
  486. #dns
  487. #1. check use api
  488. _err "Add the following txt record:"
  489. _err "Domain:$txtdomain"
  490. _err "Txt value:$txt"
  491. #dnsadded='1'
  492. fi
  493. done
  494. if [ "$dnsadded" == '0' ] ; then
  495. _setopt "$DOMAIN_CONF" "Le_Vlist" "=" "\"$vlist\""
  496. _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
  497. _err "Please add the txt records to the domains, and retry again."
  498. return 1
  499. fi
  500. fi
  501. _debug "ok, let's start to verify"
  502. ventries=$(echo "$vlist" | sed "s/,/ /g")
  503. for ventry in $ventries
  504. do
  505. d=$(echo $ventry | cut -d $sep -f 1)
  506. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  507. uri=$(echo $ventry | cut -d $sep -f 3)
  508. _info "Verifying:$d"
  509. _debug "d" "$d"
  510. _debug "keyauthorization" "$keyauthorization"
  511. _debug "uri" "$uri"
  512. removelevel= ""
  513. token=""
  514. if [ "$vtype" == "$VTYPE_HTTP" ] ; then
  515. if [ "$Le_Webroot" == "no" ] ; then
  516. _info "Standalone mode server"
  517. _startserver "$keyauthorization" &
  518. serverproc="$!"
  519. sleep 2
  520. _debug serverproc $serverproc
  521. else
  522. if [ -z "$wellknown_path" ] ; then
  523. wellknown_path="$Le_Webroot/.well-known/acme-challenge"
  524. fi
  525. _debug wellknown_path "$wellknown_path"
  526. if [ ! -d "$Le_Webroot/.well-known" ] ; then
  527. removelevel='1'
  528. elif [ ! -d "$Le_Webroot/.well-known/acme-challenge" ] ; then
  529. removelevel='2'
  530. else
  531. removelevel='3'
  532. fi
  533. token="$(echo -e -n "$keyauthorization" | cut -d '.' -f 1)"
  534. _debug "writing token:$token to $wellknown_path/$token"
  535. mkdir -p "$wellknown_path"
  536. echo -n "$keyauthorization" > "$wellknown_path/$token"
  537. webroot_owner=$(stat -c '%U:%G' $Le_Webroot)
  538. _debug "Changing owner/group of .well-known to $webroot_owner"
  539. chown -R $webroot_owner "$Le_Webroot/.well-known"
  540. fi
  541. fi
  542. _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"
  543. if [ ! -z "$code" ] && [ ! "$code" == '202' ] ; then
  544. _err "$d:Challenge error: $resource"
  545. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  546. _clearup
  547. return 1
  548. fi
  549. while [ "1" ] ; do
  550. _debug "sleep 5 secs to verify"
  551. sleep 5
  552. _debug "checking"
  553. if ! _get $uri ; then
  554. _err "$d:Verify error:$resource"
  555. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  556. _clearup
  557. return 1
  558. fi
  559. status=$(echo $response | egrep -o '"status":"[^"]+"' | cut -d : -f 2 | sed 's/"//g')
  560. if [ "$status" == "valid" ] ; then
  561. _info "Success"
  562. _stopserver $serverproc
  563. serverproc=""
  564. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  565. break;
  566. fi
  567. if [ "$status" == "invalid" ] ; then
  568. error=$(echo $response | egrep -o '"error":{[^}]*}' | grep -o '"detail":"[^"]*"' | cut -d '"' -f 4)
  569. _err "$d:Verify error:$error"
  570. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  571. _clearup
  572. return 1;
  573. fi
  574. if [ "$status" == "pending" ] ; then
  575. _info "Pending"
  576. else
  577. _err "$d:Verify error:$response"
  578. _clearupwebbroot "$Le_Webroot" "$removelevel" "$token"
  579. _clearup
  580. return 1
  581. fi
  582. done
  583. done
  584. _clearup
  585. _info "Verify finished, start to sign."
  586. der="$(openssl req -in $CSR_PATH -outform DER | base64 -w 0 | _b64)"
  587. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  588. Le_LinkCert="$(grep -i -o '^Location.*' $CURL_HEADER |sed 's/\r//g'| cut -d " " -f 2)"
  589. _setopt "$DOMAIN_CONF" "Le_LinkCert" "=" "$Le_LinkCert"
  590. if [ "$Le_LinkCert" ] ; then
  591. echo -----BEGIN CERTIFICATE----- > "$CERT_PATH"
  592. curl --silent "$Le_LinkCert" | base64 >> "$CERT_PATH"
  593. echo -----END CERTIFICATE----- >> "$CERT_PATH"
  594. _info "Cert success."
  595. cat "$CERT_PATH"
  596. _info "Your cert is in $CERT_PATH"
  597. fi
  598. if [ -z "$Le_LinkCert" ] ; then
  599. response="$(echo $response | base64 -d)"
  600. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  601. return 1
  602. fi
  603. _setopt "$DOMAIN_CONF" 'Le_Vlist' '=' "\"\""
  604. Le_LinkIssuer=$(grep -i '^Link' $CURL_HEADER | cut -d " " -f 2| cut -d ';' -f 1 | sed 's/<//g' | sed 's/>//g')
  605. _setopt "$DOMAIN_CONF" "Le_LinkIssuer" "=" "$Le_LinkIssuer"
  606. if [ "$Le_LinkIssuer" ] ; then
  607. echo -----BEGIN CERTIFICATE----- > "$CA_CERT_PATH"
  608. curl --silent "$Le_LinkIssuer" | base64 >> "$CA_CERT_PATH"
  609. echo -----END CERTIFICATE----- >> "$CA_CERT_PATH"
  610. _info "The intermediate CA cert is in $CA_CERT_PATH"
  611. fi
  612. Le_CertCreateTime=$(date -u "+%s")
  613. _setopt "$DOMAIN_CONF" "Le_CertCreateTime" "=" "$Le_CertCreateTime"
  614. Le_CertCreateTimeStr=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
  615. _setopt "$DOMAIN_CONF" "Le_CertCreateTimeStr" "=" "\"$Le_CertCreateTimeStr\""
  616. if [ ! "$Le_RenewalDays" ] ; then
  617. Le_RenewalDays=80
  618. fi
  619. _setopt "$DOMAIN_CONF" "Le_RenewalDays" "=" "$Le_RenewalDays"
  620. Le_NextRenewTime=$(date -u -d "+$Le_RenewalDays day" "+%s")
  621. _setopt "$DOMAIN_CONF" "Le_NextRenewTime" "=" "$Le_NextRenewTime"
  622. Le_NextRenewTimeStr=$(date -u -d "+$Le_RenewalDays day" "+%Y-%m-%d %H:%M:%S UTC")
  623. _setopt "$DOMAIN_CONF" "Le_NextRenewTimeStr" "=" "\"$Le_NextRenewTimeStr\""
  624. installcert $Le_Domain "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd"
  625. }
  626. renew() {
  627. Le_Domain="$1"
  628. if [ -z "$Le_Domain" ] ; then
  629. _err "Usage: $0 domain.com"
  630. return 1
  631. fi
  632. _initpath $Le_Domain
  633. if [ -f "$DOMAIN_CONF" ] ; then
  634. source "$DOMAIN_CONF"
  635. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  636. _info "Skip, Next renewal time is: $Le_NextRenewTimeStr"
  637. return 2
  638. fi
  639. fi
  640. IS_RENEW="1"
  641. issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd"
  642. IS_RENEW=""
  643. }
  644. renewAll() {
  645. _initpath
  646. _info "renewAll"
  647. for d in $(ls -F $WORKING_DIR | grep '/$') ; do
  648. d=$(echo $d | cut -d '/' -f 1)
  649. _info "renew $d"
  650. Le_LinkCert=""
  651. Le_Domain=""
  652. Le_Alt=""
  653. Le_Webroot=""
  654. Le_Keylength=""
  655. Le_LinkIssuer=""
  656. Le_CertCreateTime=""
  657. Le_CertCreateTimeStr=""
  658. Le_RenewalDays=""
  659. Le_NextRenewTime=""
  660. Le_NextRenewTimeStr=""
  661. Le_RealCertPath=""
  662. Le_RealKeyPath=""
  663. Le_RealCACertPath=""
  664. Le_ReloadCmd=""
  665. DOMAIN_CONF=""
  666. CSR_PATH=""
  667. CERT_KEY_PATH=""
  668. CERT_PATH=""
  669. CA_CERT_PATH=""
  670. ACCOUNT_KEY_PATH=""
  671. wellknown_path=""
  672. renew "$d"
  673. done
  674. }
  675. installcert() {
  676. Le_Domain="$1"
  677. if [ -z "$Le_Domain" ] ; then
  678. _err "Usage: $0 domain.com [cert-file-path]|no [key-file-path]|no [ca-cert-file-path]|no [reloadCmd]|no"
  679. return 1
  680. fi
  681. Le_RealCertPath="$2"
  682. Le_RealKeyPath="$3"
  683. Le_RealCACertPath="$4"
  684. Le_ReloadCmd="$5"
  685. _initpath $Le_Domain
  686. _setopt "$DOMAIN_CONF" "Le_RealCertPath" "=" "\"$Le_RealCertPath\""
  687. _setopt "$DOMAIN_CONF" "Le_RealCACertPath" "=" "\"$Le_RealCACertPath\""
  688. _setopt "$DOMAIN_CONF" "Le_RealKeyPath" "=" "\"$Le_RealKeyPath\""
  689. _setopt "$DOMAIN_CONF" "Le_ReloadCmd" "=" "\"$Le_ReloadCmd\""
  690. if [ "$Le_RealCACertPath" ] ; then
  691. if [ -f "$Le_RealCACertPath" ] ; then
  692. cp -p "$Le_RealCACertPath" "$Le_RealCACertPath".bak
  693. fi
  694. cat "$CA_CERT_PATH" > "$Le_RealCACertPath"
  695. fi
  696. if [ "$Le_RealKeyPath" ] ; then
  697. if [ -f "$Le_RealKeyPath" ] ; then
  698. cp -p "$Le_RealKeyPath" "$Le_RealKeyPath".bak
  699. fi
  700. cat "$CERT_KEY_PATH" > "$Le_RealKeyPath"
  701. fi
  702. if [ "$Le_ReloadCmd" ] ; then
  703. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  704. $Le_ReloadCmd
  705. fi
  706. }
  707. install() {
  708. _initpath
  709. if ! command -v "curl" > /dev/null ; then
  710. _err "Please install curl first."
  711. _err "Ubuntu: sudo apt-get install curl"
  712. _err "CentOS: yum install curl"
  713. return 1
  714. fi
  715. if ! command -v "crontab" > /dev/null ; then
  716. _err "Please install crontab first."
  717. _err "CentOs: yum -y install crontabs"
  718. return 1
  719. fi
  720. if ! command -v "openssl" > /dev/null ; then
  721. _err "Please install openssl first."
  722. _err "CentOs: yum -y install openssl"
  723. return 1
  724. fi
  725. if ! command -v "xxd" > /dev/null ; then
  726. _err "Please install xxd first."
  727. _err "CentOs: yum install vim-common"
  728. return 1
  729. fi
  730. _info "Installing to $WORKING_DIR"
  731. if [ ! -f /bin/le.sh ] ; then
  732. cp le.sh "/bin/"
  733. chmod +x "/bin/le.sh"
  734. ln -s "/bin/le.sh" /bin/le
  735. fi
  736. _info "Installing cron job"
  737. if command -v sudo > /dev/null ; then
  738. if [ "$(sudo -n uptime 2>&1|grep "load"|wc -l)" != "0" ] ; then
  739. SUDO=sudo
  740. fi
  741. fi
  742. if ! crontab -l | grep 'le renewAll' ; then
  743. crontab -l | { cat; echo "0 0 * * * $SUDO le renewAll > /dev/null"; } | crontab -
  744. if command -v crond > /dev/null ; then
  745. service crond reload >/dev/null
  746. else
  747. service cron reload >/dev/null
  748. fi
  749. fi
  750. _info OK
  751. }
  752. uninstall() {
  753. _initpath
  754. _info "Removing cron job"
  755. if crontab -l | grep 'le.*renewAll' ; then
  756. crontab -l | sed "/le.*renewAll/d" | crontab -
  757. if command -v crond > /dev/null ; then
  758. service crond reload >/dev/null
  759. else
  760. service cron reload >/dev/null
  761. fi
  762. fi
  763. _info "Removing /bin/le.sh"
  764. rm -f /bin/le
  765. rm -f /bin/le.sh
  766. _info "The keys and certs are in $WORKING_DIR, you can remove them by yourself."
  767. }
  768. version() {
  769. _info "$PROJECT"
  770. _info "v$VER"
  771. }
  772. showhelp() {
  773. version
  774. echo "Usage: issue|installcert|renew|renewAll|createAccountKey|createDomainKey|createCSR|install|uninstall|version"
  775. }
  776. if [ -z "$1" ] ; then
  777. showhelp
  778. else
  779. "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
  780. fi