acme.sh 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  1. #!/usr/bin/env sh
  2. VER=2.2.5
  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.0.1-July-27-2015.pdf"
  8. DEFAULT_USER_AGENT="$PROJECT_ENTRY client: $PROJECT"
  9. STAGE_CA="https://acme-staging.api.letsencrypt.org"
  10. VTYPE_HTTP="http-01"
  11. VTYPE_DNS="dns-01"
  12. BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----"
  13. END_CSR="-----END CERTIFICATE REQUEST-----"
  14. BEGIN_CERT="-----BEGIN CERTIFICATE-----"
  15. END_CERT="-----END CERTIFICATE-----"
  16. if [ -z "$AGREEMENT" ] ; then
  17. AGREEMENT="$DEFAULT_AGREEMENT"
  18. fi
  19. _URGLY_PRINTF=""
  20. if [ "$(printf '\x41')" != 'A' ] ; then
  21. _URGLY_PRINTF=1
  22. fi
  23. _info() {
  24. if [ -z "$2" ] ; then
  25. echo "[$(date)] $1"
  26. else
  27. echo "[$(date)] $1='$2'"
  28. fi
  29. }
  30. _err() {
  31. _info "$@" >&2
  32. return 1
  33. }
  34. _debug() {
  35. if [ -z "$DEBUG" ] ; then
  36. return
  37. fi
  38. _err "$@"
  39. return 0
  40. }
  41. _debug2() {
  42. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
  43. _debug "$@"
  44. fi
  45. return
  46. }
  47. _startswith(){
  48. _str="$1"
  49. _sub="$2"
  50. echo "$_str" | grep "^$_sub" >/dev/null 2>&1
  51. }
  52. _contains(){
  53. _str="$1"
  54. _sub="$2"
  55. echo "$_str" | grep "$_sub" >/dev/null 2>&1
  56. }
  57. _hasfield() {
  58. _str="$1"
  59. _field="$2"
  60. _sep="$3"
  61. if [ -z "$_field" ] ; then
  62. _err "Usage: str field [sep]"
  63. return 1
  64. fi
  65. if [ -z "$_sep" ] ; then
  66. _sep=","
  67. fi
  68. for f in $(echo "$_str" | tr ',' ' ') ; do
  69. if [ "$f" = "$_field" ] ; then
  70. _debug "'$_str' contains '$_field'"
  71. return 0 #contains ok
  72. fi
  73. done
  74. _debug "'$_str' does not contain '$_field'"
  75. return 1 #not contains
  76. }
  77. _exists(){
  78. cmd="$1"
  79. if [ -z "$cmd" ] ; then
  80. _err "Usage: _exists cmd"
  81. return 1
  82. fi
  83. if type command >/dev/null 2>&1 ; then
  84. command -v "$cmd" >/dev/null 2>&1
  85. else
  86. type "$cmd" >/dev/null 2>&1
  87. fi
  88. ret="$?"
  89. _debug2 "$cmd exists=$ret"
  90. return $ret
  91. }
  92. #a + b
  93. _math(){
  94. expr "$@"
  95. }
  96. _h_char_2_dec() {
  97. _ch=$1
  98. case "${_ch}" in
  99. a|A)
  100. printf "10"
  101. ;;
  102. b|B)
  103. printf "11"
  104. ;;
  105. c|C)
  106. printf "12"
  107. ;;
  108. d|D)
  109. printf "13"
  110. ;;
  111. e|E)
  112. printf "14"
  113. ;;
  114. f|F)
  115. printf "15"
  116. ;;
  117. *)
  118. printf "%s" "$_ch"
  119. ;;
  120. esac
  121. }
  122. _h2b() {
  123. hex=$(cat)
  124. i=1
  125. j=2
  126. if _exists let ; then
  127. uselet="1"
  128. fi
  129. _debug uselet "$uselet"
  130. _debug _URGLY_PRINTF "$_URGLY_PRINTF"
  131. while true ; do
  132. if [ -z "$_URGLY_PRINTF" ] ; then
  133. h="$(printf $hex | cut -c $i-$j)"
  134. if [ -z "$h" ] ; then
  135. break;
  136. fi
  137. printf "\x$h"
  138. else
  139. ic="$(printf $hex | cut -c $i)"
  140. jc="$(printf $hex | cut -c $j)"
  141. if [ -z "$ic$jc" ] ; then
  142. break;
  143. fi
  144. ic="$(_h_char_2_dec "$ic")"
  145. jc="$(_h_char_2_dec "$jc")"
  146. printf '\'"$(printf %o "$(_math $ic \* 16 + $jc)")"
  147. fi
  148. if [ "$uselet" ] ; then
  149. let "i+=2" >/dev/null
  150. let "j+=2" >/dev/null
  151. else
  152. i="$(_math $i + 2)"
  153. j="$(_math $j + 2)"
  154. fi
  155. done
  156. }
  157. #options file
  158. _sed_i() {
  159. options="$1"
  160. filename="$2"
  161. if [ -z "$filename" ] ; then
  162. _err "Usage:_sed_i options filename"
  163. return 1
  164. fi
  165. _debug2 options "$options"
  166. if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then
  167. _debug "Using sed -i"
  168. sed -i "$options" "$filename"
  169. else
  170. _debug "No -i support in sed"
  171. text="$(cat "$filename")"
  172. echo "$text" | sed "$options" > "$filename"
  173. fi
  174. }
  175. #Usage: file startline endline
  176. _getfile() {
  177. filename="$1"
  178. startline="$2"
  179. endline="$3"
  180. if [ -z "$endline" ] ; then
  181. _err "Usage: file startline endline"
  182. return 1
  183. fi
  184. i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)"
  185. if [ -z "$i" ] ; then
  186. _err "Can not find start line: $startline"
  187. return 1
  188. fi
  189. i="$(_math "$i" + 1)"
  190. _debug i "$i"
  191. j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)"
  192. if [ -z "$j" ] ; then
  193. _err "Can not find end line: $endline"
  194. return 1
  195. fi
  196. j="$(_math "$j" - 1)"
  197. _debug j "$j"
  198. sed -n "$i,${j}p" "$filename"
  199. }
  200. #Usage: multiline
  201. _base64() {
  202. if [ "$1" ] ; then
  203. openssl base64 -e
  204. else
  205. openssl base64 -e | tr -d '\r\n'
  206. fi
  207. }
  208. #Usage: multiline
  209. _dbase64() {
  210. if [ "$1" ] ; then
  211. openssl base64 -d -A
  212. else
  213. openssl base64 -d
  214. fi
  215. }
  216. #Usage: hashalg
  217. #Output Base64-encoded digest
  218. _digest() {
  219. alg="$1"
  220. if [ -z "$alg" ] ; then
  221. _err "Usage: _digest hashalg"
  222. return 1
  223. fi
  224. if [ "$alg" = "sha256" ] ; then
  225. openssl dgst -sha256 -binary | _base64
  226. else
  227. _err "$alg is not supported yet"
  228. return 1
  229. fi
  230. }
  231. #Usage: keyfile hashalg
  232. #Output: Base64-encoded signature value
  233. _sign() {
  234. keyfile="$1"
  235. alg="$2"
  236. if [ -z "$alg" ] ; then
  237. _err "Usage: _sign keyfile hashalg"
  238. return 1
  239. fi
  240. if [ "$alg" = "sha256" ] ; then
  241. openssl dgst -sha256 -sign "$keyfile" | _base64
  242. else
  243. _err "$alg is not supported yet"
  244. return 1
  245. fi
  246. }
  247. _ss() {
  248. _port="$1"
  249. if _exists "ss" ; then
  250. _debug "Using: ss"
  251. ss -ntpl | grep ":$_port "
  252. return 0
  253. fi
  254. if _exists "netstat" ; then
  255. _debug "Using: netstat"
  256. if netstat -h 2>&1 | grep "\-p proto" >/dev/null ; then
  257. #for windows version netstat tool
  258. netstat -anb -p tcp | grep "LISTENING" | grep ":$_port "
  259. else
  260. if netstat -help 2>&1 | grep "\-p protocol" >/dev/null ; then
  261. netstat -an -p tcp | grep LISTEN | grep ":$_port "
  262. else
  263. netstat -ntpl | grep ":$_port "
  264. fi
  265. fi
  266. return 0
  267. fi
  268. return 1
  269. }
  270. toPkcs() {
  271. domain="$1"
  272. pfxPassword="$2"
  273. if [ -z "$domain" ] ; then
  274. echo "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]"
  275. return 1
  276. fi
  277. _initpath "$domain"
  278. if [ "$pfxPassword" ] ; then
  279. openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" -password "pass:$pfxPassword"
  280. else
  281. openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH"
  282. fi
  283. if [ "$?" = "0" ] ; then
  284. _info "Success, Pfx is exported to: $CERT_PFX_PATH"
  285. fi
  286. }
  287. #domain [2048]
  288. createAccountKey() {
  289. _info "Creating account key"
  290. if [ -z "$1" ] ; then
  291. echo Usage: $PROJECT_ENTRY --createAccountKey -d domain.com [--accountkeylength 2048]
  292. return
  293. fi
  294. account=$1
  295. length=$2
  296. _debug account "$account"
  297. _debug length "$length"
  298. if _startswith "$length" "ec-" ; then
  299. length=2048
  300. fi
  301. if [ -z "$2" ] || [ "$2" = "no" ] ; then
  302. _info "Use default length 2048"
  303. length=2048
  304. fi
  305. _initpath
  306. if [ -f "$ACCOUNT_KEY_PATH" ] ; then
  307. _info "Account key exists, skip"
  308. return
  309. else
  310. #generate account key
  311. openssl genrsa $length 2>/dev/null > "$ACCOUNT_KEY_PATH"
  312. fi
  313. }
  314. #domain length
  315. createDomainKey() {
  316. _info "Creating domain key"
  317. if [ -z "$1" ] ; then
  318. echo Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]
  319. return
  320. fi
  321. domain=$1
  322. length=$2
  323. isec=""
  324. if _startswith "$length" "ec-" ; then
  325. isec="1"
  326. length=$(printf $length | cut -d '-' -f 2-100)
  327. eccname="$length"
  328. fi
  329. if [ -z "$length" ] ; then
  330. if [ "$isec" ] ; then
  331. length=256
  332. else
  333. length=2048
  334. fi
  335. fi
  336. _info "Use length $length"
  337. if [ "$isec" ] ; then
  338. if [ "$length" = "256" ] ; then
  339. eccname="prime256v1"
  340. fi
  341. if [ "$length" = "384" ] ; then
  342. eccname="secp384r1"
  343. fi
  344. if [ "$length" = "521" ] ; then
  345. eccname="secp521r1"
  346. fi
  347. _info "Using ec name: $eccname"
  348. fi
  349. _initpath $domain
  350. if [ ! -f "$CERT_KEY_PATH" ] || ( [ "$FORCE" ] && ! [ "$IS_RENEW" ] ); then
  351. #generate account key
  352. if [ "$isec" ] ; then
  353. openssl ecparam -name $eccname -genkey 2>/dev/null > "$CERT_KEY_PATH"
  354. else
  355. openssl genrsa $length 2>/dev/null > "$CERT_KEY_PATH"
  356. fi
  357. else
  358. if [ "$IS_RENEW" ] ; then
  359. _info "Domain key exists, skip"
  360. return 0
  361. else
  362. _err "Domain key exists, do you want to overwrite the key?"
  363. _err "Add '--force', and try again."
  364. return 1
  365. fi
  366. fi
  367. }
  368. # domain domainlist
  369. createCSR() {
  370. _info "Creating csr"
  371. if [ -z "$1" ] ; then
  372. echo "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]"
  373. return
  374. fi
  375. domain=$1
  376. _initpath "$domain"
  377. domainlist=$2
  378. if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then
  379. _info "CSR exists, skip"
  380. return
  381. fi
  382. if [ -z "$domainlist" ] || [ "$domainlist" = "no" ]; then
  383. #single domain
  384. _info "Single domain" "$domain"
  385. printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n" > "$DOMAIN_SSL_CONF"
  386. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" -config "$DOMAIN_SSL_CONF" -out "$CSR_PATH"
  387. else
  388. alt="DNS:$(echo $domainlist | sed "s/,/,DNS:/g")"
  389. #multi
  390. _info "Multi domain" "$alt"
  391. printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n[SAN]\nsubjectAltName=$alt" > "$DOMAIN_SSL_CONF"
  392. openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" -reqexts SAN -config "$DOMAIN_SSL_CONF" -out "$CSR_PATH"
  393. fi
  394. }
  395. _urlencode() {
  396. __n=$(cat)
  397. echo $__n | tr '/+' '_-' | tr -d '= '
  398. }
  399. _time2str() {
  400. #BSD
  401. if date -u -d@$1 2>/dev/null ; then
  402. return
  403. fi
  404. #Linux
  405. if date -u -r $1 2>/dev/null ; then
  406. return
  407. fi
  408. }
  409. _stat() {
  410. #Linux
  411. if stat -c '%U:%G' "$1" 2>/dev/null ; then
  412. return
  413. fi
  414. #BSD
  415. if stat -f '%Su:%Sg' "$1" 2>/dev/null ; then
  416. return
  417. fi
  418. }
  419. #keyfile
  420. _calcjwk() {
  421. keyfile="$1"
  422. if [ -z "$keyfile" ] ; then
  423. _err "Usage: _calcjwk keyfile"
  424. return 1
  425. fi
  426. EC_SIGN=""
  427. if grep "BEGIN RSA PRIVATE KEY" "$keyfile" > /dev/null 2>&1 ; then
  428. _debug "RSA key"
  429. pub_exp=$(openssl rsa -in $keyfile -noout -text | grep "^publicExponent:"| cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1)
  430. if [ "${#pub_exp}" = "5" ] ; then
  431. pub_exp=0$pub_exp
  432. fi
  433. _debug2 pub_exp "$pub_exp"
  434. e=$(echo $pub_exp | _h2b | _base64)
  435. _debug2 e "$e"
  436. modulus=$(openssl rsa -in $keyfile -modulus -noout | cut -d '=' -f 2 )
  437. _debug2 modulus "$modulus"
  438. n="$(printf "%s" "$modulus"| _h2b | _base64 | _urlencode )"
  439. jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}'
  440. _debug2 jwk "$jwk"
  441. HEADER='{"alg": "RS256", "jwk": '$jwk'}'
  442. HEADERPLACE='{"nonce": "NONCE", "alg": "RS256", "jwk": '$jwk'}'
  443. elif grep "BEGIN EC PRIVATE KEY" "$keyfile" > /dev/null 2>&1 ; then
  444. _debug "EC key"
  445. EC_SIGN="1"
  446. crv="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")"
  447. _debug2 crv "$crv"
  448. pubi="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)"
  449. pubi=$(_math $pubi + 1)
  450. _debug2 pubi "$pubi"
  451. pubj="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)"
  452. pubj=$(_math $pubj + 1)
  453. _debug2 pubj "$pubj"
  454. pubtext="$(openssl ec -in $keyfile -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")"
  455. _debug2 pubtext "$pubtext"
  456. xlen="$(printf "$pubtext" | tr -d ':' | wc -c)"
  457. xlen=$(_math $xlen / 4)
  458. _debug2 xlen "$xlen"
  459. xend=$(_math "$xend" + 1)
  460. x="$(printf $pubtext | cut -d : -f 2-$xend)"
  461. _debug2 x "$x"
  462. x64="$(printf $x | tr -d : | _h2b | _base64 | _urlencode)"
  463. _debug2 x64 "$x64"
  464. xend=$(_math "$xend" + 1)
  465. y="$(printf $pubtext | cut -d : -f $xend-10000)"
  466. _debug2 y "$y"
  467. y64="$(printf $y | tr -d : | _h2b | _base64 | _urlencode)"
  468. _debug2 y64 "$y64"
  469. jwk='{"kty": "EC", "crv": "'$crv'", "x": "'$x64'", "y": "'$y64'"}'
  470. _debug2 jwk "$jwk"
  471. HEADER='{"alg": "ES256", "jwk": '$jwk'}'
  472. HEADERPLACE='{"nonce": "NONCE", "alg": "ES256", "jwk": '$jwk'}'
  473. else
  474. _err "Only RSA or EC key is supported."
  475. return 1
  476. fi
  477. _debug2 HEADER "$HEADER"
  478. }
  479. # body url [needbase64] [POST|PUT]
  480. _post() {
  481. body="$1"
  482. url="$2"
  483. needbase64="$3"
  484. httpmethod="$4"
  485. if [ -z "$httpmethod" ] ; then
  486. httpmethod="POST"
  487. fi
  488. _debug $httpmethod
  489. _debug "url" "$url"
  490. if _exists "curl" ; then
  491. _CURL="$CURL --dump-header $HTTP_HEADER "
  492. if [ "$needbase64" ] ; then
  493. response="$($_CURL -A "User-Agent: $USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" --data "$body" "$url" | _base64)"
  494. else
  495. response="$($_CURL -A "User-Agent: $USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" --data "$body" "$url" )"
  496. fi
  497. else
  498. if [ "$needbase64" ] ; then
  499. if [ "$httpmethod"="POST" ] ; then
  500. response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)"
  501. else
  502. response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)"
  503. fi
  504. else
  505. if [ "$httpmethod"="POST" ] ; then
  506. response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER")"
  507. else
  508. response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER")"
  509. fi
  510. fi
  511. _sed_i "s/^ *//g" "$HTTP_HEADER"
  512. fi
  513. printf "%s" "$response"
  514. }
  515. # url getheader
  516. _get() {
  517. _debug GET
  518. url="$1"
  519. onlyheader="$2"
  520. _debug url $url
  521. if _exists "curl" ; then
  522. if [ "$onlyheader" ] ; then
  523. $CURL -I -A "User-Agent: $USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" $url
  524. else
  525. $CURL -A "User-Agent: $USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" $url
  526. fi
  527. else
  528. _debug "WGET" "$WGET"
  529. if [ "$onlyheader" ] ; then
  530. $WGET --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O /dev/null $url 2>&1 | sed 's/^[ ]*//g'
  531. else
  532. $WGET --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - $url
  533. fi
  534. fi
  535. ret=$?
  536. return $ret
  537. }
  538. # url payload needbase64 keyfile
  539. _send_signed_request() {
  540. url=$1
  541. payload=$2
  542. needbase64=$3
  543. keyfile=$4
  544. if [ -z "$keyfile" ] ; then
  545. keyfile="$ACCOUNT_KEY_PATH"
  546. fi
  547. _debug url $url
  548. _debug payload "$payload"
  549. if ! _calcjwk "$keyfile" ; then
  550. return 1
  551. fi
  552. payload64=$(echo -n $payload | _base64 | _urlencode)
  553. _debug2 payload64 $payload64
  554. nonceurl="$API/directory"
  555. nonce="$(_get $nonceurl "onlyheader" | grep -o "Replay-Nonce:.*$" | head -1 | tr -d "\r\n" | cut -d ' ' -f 2)"
  556. _debug nonce "$nonce"
  557. protected="$(printf "$HEADERPLACE" | sed "s/NONCE/$nonce/" )"
  558. _debug2 protected "$protected"
  559. protected64="$(printf "$protected" | _base64 | _urlencode)"
  560. _debug2 protected64 "$protected64"
  561. sig=$(echo -n "$protected64.$payload64" | _sign "$keyfile" "sha256" | _urlencode)
  562. _debug2 sig "$sig"
  563. body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}"
  564. _debug2 body "$body"
  565. response="$(_post "$body" $url "$needbase64" | tr -d "\r\n ")"
  566. responseHeaders="$(cat $HTTP_HEADER)"
  567. _debug2 responseHeaders "$responseHeaders"
  568. _debug2 response "$response"
  569. code="$(grep "^HTTP" $HTTP_HEADER | tail -1 | cut -d " " -f 2 | tr -d "\r\n" )"
  570. _debug code $code
  571. }
  572. #setopt "file" "opt" "=" "value" [";"]
  573. _setopt() {
  574. __conf="$1"
  575. __opt="$2"
  576. __sep="$3"
  577. __val="$4"
  578. __end="$5"
  579. if [ -z "$__opt" ] ; then
  580. echo usage: _setopt '"file" "opt" "=" "value" [";"]'
  581. return
  582. fi
  583. if [ ! -f "$__conf" ] ; then
  584. touch "$__conf"
  585. fi
  586. if grep -H -n "^$__opt$__sep" "$__conf" > /dev/null ; then
  587. _debug2 OK
  588. if _contains "$__val" "&" ; then
  589. __val="$(echo $__val | sed 's/&/\\&/g')"
  590. fi
  591. text="$(cat $__conf)"
  592. echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf"
  593. elif grep -H -n "^#$__opt$__sep" "$__conf" > /dev/null ; then
  594. if _contains "$__val" "&" ; then
  595. __val="$(echo $__val | sed 's/&/\\&/g')"
  596. fi
  597. text="$(cat $__conf)"
  598. echo "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf"
  599. else
  600. _debug2 APP
  601. echo "$__opt$__sep$__val$__end" >> "$__conf"
  602. fi
  603. _debug "$(grep -H -n "^$__opt$__sep" $__conf)"
  604. }
  605. #_savedomainconf key value
  606. #save to domain.conf
  607. _savedomainconf() {
  608. key="$1"
  609. value="$2"
  610. if [ "$DOMAIN_CONF" ] ; then
  611. _setopt "$DOMAIN_CONF" "$key" "=" "\"$value\""
  612. else
  613. _err "DOMAIN_CONF is empty, can not save $key=$value"
  614. fi
  615. }
  616. #_cleardomainconf key
  617. _cleardomainconf() {
  618. key="$1"
  619. if [ "$DOMAIN_CONF" ] ; then
  620. _sed_i "s/^$key.*$//" "$DOMAIN_CONF"
  621. else
  622. _err "DOMAIN_CONF is empty, can not save $key=$value"
  623. fi
  624. }
  625. #_saveaccountconf key value
  626. _saveaccountconf() {
  627. key="$1"
  628. value="$2"
  629. if [ "$ACCOUNT_CONF_PATH" ] ; then
  630. _setopt "$ACCOUNT_CONF_PATH" "$key" "=" "\"$value\""
  631. else
  632. _err "ACCOUNT_CONF_PATH is empty, can not save $key=$value"
  633. fi
  634. }
  635. _startserver() {
  636. content="$1"
  637. _debug "startserver: $$"
  638. nchelp="$(nc -h 2>&1)"
  639. if echo "$nchelp" | grep "\-q[ ,]" >/dev/null ; then
  640. _NC="nc -q 1 -l"
  641. else
  642. if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null ; then
  643. _NC="nc -c -l"
  644. elif echo "$nchelp" | grep "\-N" |grep "Shutdown the network socket after EOF on stdin" >/dev/null ; then
  645. _NC="nc -N -l"
  646. else
  647. _NC="nc -l"
  648. fi
  649. fi
  650. _debug "_NC" "$_NC"
  651. # while true ; do
  652. if [ "$DEBUG" ] ; then
  653. if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort ; then
  654. printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort ;
  655. fi
  656. else
  657. if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then
  658. printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1
  659. fi
  660. fi
  661. if [ "$?" != "0" ] ; then
  662. _err "nc listen error."
  663. exit 1
  664. fi
  665. # done
  666. }
  667. _stopserver(){
  668. pid="$1"
  669. _debug "pid" "$pid"
  670. if [ -z "$pid" ] ; then
  671. return
  672. fi
  673. _get "http://localhost:$Le_HTTPPort" >/dev/null 2>&1
  674. }
  675. _initpath() {
  676. if [ -z "$LE_WORKING_DIR" ] ; then
  677. LE_WORKING_DIR=$HOME/.$PROJECT_NAME
  678. fi
  679. _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf"
  680. if [ -z "$ACCOUNT_CONF_PATH" ] ; then
  681. if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ] ; then
  682. . "$_DEFAULT_ACCOUNT_CONF_PATH"
  683. fi
  684. fi
  685. if [ -z "$ACCOUNT_CONF_PATH" ] ; then
  686. ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH"
  687. fi
  688. if [ -f "$ACCOUNT_CONF_PATH" ] ; then
  689. . "$ACCOUNT_CONF_PATH"
  690. fi
  691. if [ "$IN_CRON" ] ; then
  692. if [ ! "$_USER_PATH_EXPORTED" ] ; then
  693. _USER_PATH_EXPORTED=1
  694. export PATH="$USER_PATH:$PATH"
  695. fi
  696. fi
  697. if [ -z "$API" ] ; then
  698. if [ -z "$STAGE" ] ; then
  699. API="$DEFAULT_CA"
  700. else
  701. API="$STAGE_CA"
  702. _info "Using stage api:$API"
  703. fi
  704. fi
  705. if [ -z "$ACME_DIR" ] ; then
  706. ACME_DIR="/home/.acme"
  707. fi
  708. if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then
  709. APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR"
  710. fi
  711. if [ -z "$USER_AGENT" ] ; then
  712. USER_AGENT="$DEFAULT_USER_AGENT"
  713. fi
  714. HTTP_HEADER="$LE_WORKING_DIR/http.header"
  715. WGET="wget -q"
  716. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
  717. WGET="$WGET -d "
  718. fi
  719. dp="$LE_WORKING_DIR/curl.dump"
  720. CURL="curl -L --silent"
  721. if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then
  722. CURL="$CURL --trace-ascii $dp "
  723. fi
  724. _DEFAULT_ACCOUNT_KEY_PATH="$LE_WORKING_DIR/account.key"
  725. if [ -z "$ACCOUNT_KEY_PATH" ] ; then
  726. ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH"
  727. fi
  728. _DEFAULT_CERT_HOME="$LE_WORKING_DIR"
  729. if [ -z "$CERT_HOME" ] ; then
  730. CERT_HOME="$_DEFAULT_CERT_HOME"
  731. fi
  732. domain="$1"
  733. if [ -z "$domain" ] ; then
  734. return 0
  735. fi
  736. domainhome="$CERT_HOME/$domain"
  737. mkdir -p "$domainhome"
  738. if [ -z "$DOMAIN_PATH" ] ; then
  739. DOMAIN_PATH="$domainhome"
  740. fi
  741. if [ -z "$DOMAIN_CONF" ] ; then
  742. DOMAIN_CONF="$domainhome/$domain.conf"
  743. fi
  744. if [ -z "$DOMAIN_SSL_CONF" ] ; then
  745. DOMAIN_SSL_CONF="$domainhome/$domain.ssl.conf"
  746. fi
  747. if [ -z "$CSR_PATH" ] ; then
  748. CSR_PATH="$domainhome/$domain.csr"
  749. fi
  750. if [ -z "$CERT_KEY_PATH" ] ; then
  751. CERT_KEY_PATH="$domainhome/$domain.key"
  752. fi
  753. if [ -z "$CERT_PATH" ] ; then
  754. CERT_PATH="$domainhome/$domain.cer"
  755. fi
  756. if [ -z "$CA_CERT_PATH" ] ; then
  757. CA_CERT_PATH="$domainhome/ca.cer"
  758. fi
  759. if [ -z "$CERT_FULLCHAIN_PATH" ] ; then
  760. CERT_FULLCHAIN_PATH="$domainhome/fullchain.cer"
  761. fi
  762. if [ -z "$CERT_PFX_PATH" ] ; then
  763. CERT_PFX_PATH="$domainhome/$domain.pfx"
  764. fi
  765. }
  766. _apachePath() {
  767. if ! _exists apachectl ; then
  768. _err "'apachecrl not found. It seems that apache is not installed, or you are not root user.'"
  769. _err "Please use webroot mode to try again."
  770. return 1
  771. fi
  772. httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )"
  773. _debug httpdconfname "$httpdconfname"
  774. if _startswith "$httpdconfname" '/' ; then
  775. httpdconf="$httpdconfname"
  776. httpdconfname="$(basename $httpdconfname)"
  777. else
  778. httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )"
  779. _debug httpdroot "$httpdroot"
  780. httpdconf="$httpdroot/$httpdconfname"
  781. httpdconfname="$(basename $httpdconfname)"
  782. fi
  783. _debug httpdconf "$httpdconf"
  784. _debug httpdconfname "$httpdconfname"
  785. if [ ! -f "$httpdconf" ] ; then
  786. _err "Apache Config file not found" "$httpdconf"
  787. return 1
  788. fi
  789. return 0
  790. }
  791. _restoreApache() {
  792. if [ -z "$usingApache" ] ; then
  793. return 0
  794. fi
  795. _initpath
  796. if ! _apachePath ; then
  797. return 1
  798. fi
  799. if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then
  800. _debug "No config file to restore."
  801. return 0
  802. fi
  803. cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" > "$httpdconf"
  804. _debug "Restored: $httpdconf."
  805. if ! apachectl -t ; then
  806. _err "Sorry, restore apache config error, please contact me."
  807. return 1;
  808. fi
  809. _debug "Restored successfully."
  810. rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname"
  811. return 0
  812. }
  813. _setApache() {
  814. _initpath
  815. if ! _apachePath ; then
  816. return 1
  817. fi
  818. #backup the conf
  819. _debug "Backup apache config file" "$httpdconf"
  820. if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/" ; then
  821. _err "Can not backup apache config file, so abort. Don't worry, your apache config is not changed."
  822. _err "This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT"
  823. return 1
  824. fi
  825. _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname"
  826. _info "In case there is an error that can not be restored automatically, you may try restore it yourself."
  827. _info "The backup file will be deleted on sucess, just forget it."
  828. #add alias
  829. apacheVer="$(apachectl -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2 )"
  830. _debug "apacheVer" "$apacheVer"
  831. apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)"
  832. apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)"
  833. if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ] ; then
  834. echo "
  835. Alias /.well-known/acme-challenge $ACME_DIR
  836. <Directory $ACME_DIR >
  837. Require all granted
  838. </Directory>
  839. " >> "$httpdconf"
  840. else
  841. echo "
  842. Alias /.well-known/acme-challenge $ACME_DIR
  843. <Directory $ACME_DIR >
  844. Order allow,deny
  845. Allow from all
  846. </Directory>
  847. " >> "$httpdconf"
  848. fi
  849. if ! apachectl -t ; then
  850. _err "Sorry, apache config error, please contact me."
  851. _restoreApache
  852. return 1;
  853. fi
  854. if [ ! -d "$ACME_DIR" ] ; then
  855. mkdir -p "$ACME_DIR"
  856. chmod 755 "$ACME_DIR"
  857. fi
  858. if ! apachectl graceful ; then
  859. _err "Sorry, apachectl graceful error, please contact me."
  860. _restoreApache
  861. return 1;
  862. fi
  863. usingApache="1"
  864. return 0
  865. }
  866. _clearup() {
  867. _stopserver $serverproc
  868. serverproc=""
  869. _restoreApache
  870. }
  871. # webroot removelevel tokenfile
  872. _clearupwebbroot() {
  873. __webroot="$1"
  874. if [ -z "$__webroot" ] ; then
  875. _debug "no webroot specified, skip"
  876. return 0
  877. fi
  878. if [ "$2" = '1' ] ; then
  879. _debug "remove $__webroot/.well-known"
  880. rm -rf "$__webroot/.well-known"
  881. elif [ "$2" = '2' ] ; then
  882. _debug "remove $__webroot/.well-known/acme-challenge"
  883. rm -rf "$__webroot/.well-known/acme-challenge"
  884. elif [ "$2" = '3' ] ; then
  885. _debug "remove $__webroot/.well-known/acme-challenge/$3"
  886. rm -rf "$__webroot/.well-known/acme-challenge/$3"
  887. else
  888. _info "Skip for removelevel:$2"
  889. fi
  890. return 0
  891. }
  892. issue() {
  893. if [ -z "$2" ] ; then
  894. echo "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
  895. return 1
  896. fi
  897. Le_Webroot="$1"
  898. Le_Domain="$2"
  899. Le_Alt="$3"
  900. Le_Keylength="$4"
  901. Le_RealCertPath="$5"
  902. Le_RealKeyPath="$6"
  903. Le_RealCACertPath="$7"
  904. Le_ReloadCmd="$8"
  905. Le_RealFullChainPath="$9"
  906. #remove these later.
  907. if [ "$Le_Webroot" = "dns-cf" ] ; then
  908. Le_Webroot="dns_cf"
  909. fi
  910. if [ "$Le_Webroot" = "dns-dp" ] ; then
  911. Le_Webroot="dns_dp"
  912. fi
  913. if [ "$Le_Webroot" = "dns-cx" ] ; then
  914. Le_Webroot="dns_cx"
  915. fi
  916. _initpath $Le_Domain
  917. if [ -f "$DOMAIN_CONF" ] ; then
  918. Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2 | tr -d "'\"")
  919. _debug Le_NextRenewTime "$Le_NextRenewTime"
  920. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ $(date -u "+%s" ) -lt $Le_NextRenewTime ] ; then
  921. _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)"
  922. return 2
  923. fi
  924. fi
  925. _savedomainconf "Le_Domain" "$Le_Domain"
  926. _savedomainconf "Le_Alt" "$Le_Alt"
  927. _savedomainconf "Le_Webroot" "$Le_Webroot"
  928. _savedomainconf "Le_Keylength" "$Le_Keylength"
  929. if [ "$Le_Alt" = "no" ] ; then
  930. Le_Alt=""
  931. fi
  932. if [ "$Le_Keylength" = "no" ] ; then
  933. Le_Keylength=""
  934. fi
  935. if _hasfield "$Le_Webroot" "no" ; then
  936. _info "Standalone mode."
  937. if ! _exists "nc" ; then
  938. _err "Please install netcat(nc) tools first."
  939. return 1
  940. fi
  941. if [ -z "$Le_HTTPPort" ] ; then
  942. Le_HTTPPort=80
  943. fi
  944. _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
  945. netprc="$(_ss "$Le_HTTPPort" | grep "$Le_HTTPPort")"
  946. if [ "$netprc" ] ; then
  947. _err "$netprc"
  948. _err "tcp port $Le_HTTPPort is already used by $(echo "$netprc" | cut -d : -f 4)"
  949. _err "Please stop it first"
  950. return 1
  951. fi
  952. fi
  953. if _hasfield "$Le_Webroot" "apache" ; then
  954. if ! _setApache ; then
  955. _err "set up apache error. Report error to me."
  956. return 1
  957. fi
  958. else
  959. usingApache=""
  960. fi
  961. if [ ! -f "$ACCOUNT_KEY_PATH" ] ; then
  962. if ! createAccountKey $Le_Domain $Le_Keylength ; then
  963. _err "Create account key error."
  964. if [ "$usingApache" ] ; then
  965. _restoreApache
  966. fi
  967. return 1
  968. fi
  969. fi
  970. if ! _calcjwk "$ACCOUNT_KEY_PATH" ; then
  971. if [ "$usingApache" ] ; then
  972. _restoreApache
  973. fi
  974. return 1
  975. fi
  976. accountkey_json=$(echo -n "$jwk" | tr -d ' ' )
  977. thumbprint=$(echo -n "$accountkey_json" | _digest "sha256" | _urlencode)
  978. accountkeyhash="$(cat "$ACCOUNT_KEY_PATH" | _digest "sha256" )"
  979. accountkeyhash="$(echo $accountkeyhash$API | _digest "sha256" )"
  980. if [ "$accountkeyhash" != "$ACCOUNT_KEY_HASH" ] ; then
  981. _info "Registering account"
  982. regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}'
  983. if [ "$ACCOUNT_EMAIL" ] ; then
  984. regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}'
  985. fi
  986. _send_signed_request "$API/acme/new-reg" "$regjson"
  987. if [ "$code" = "" ] || [ "$code" = '201' ] ; then
  988. _info "Registered"
  989. echo $response > $LE_WORKING_DIR/account.json
  990. elif [ "$code" = '409' ] ; then
  991. _info "Already registered"
  992. else
  993. _err "Register account Error: $response"
  994. _clearup
  995. return 1
  996. fi
  997. ACCOUNT_KEY_HASH="$accountkeyhash"
  998. _saveaccountconf "ACCOUNT_KEY_HASH" "$ACCOUNT_KEY_HASH"
  999. else
  1000. _info "Skip register account key"
  1001. fi
  1002. if [ ! -f "$CERT_KEY_PATH" ] ; then
  1003. if ! createDomainKey $Le_Domain $Le_Keylength ; then
  1004. _err "Create domain key error."
  1005. _clearup
  1006. return 1
  1007. fi
  1008. fi
  1009. if ! createCSR $Le_Domain $Le_Alt ; then
  1010. _err "Create CSR error."
  1011. _clearup
  1012. return 1
  1013. fi
  1014. vlist="$Le_Vlist"
  1015. # verify each domain
  1016. _info "Verify each domain"
  1017. sep='#'
  1018. if [ -z "$vlist" ] ; then
  1019. alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' )
  1020. _index=1
  1021. _currentRoot=""
  1022. for d in $alldomains
  1023. do
  1024. _info "Getting webroot for domain" $d
  1025. _w="$(echo $Le_Webroot | cut -d , -f $_index)"
  1026. _debug _w "$_w"
  1027. if [ "$_w" ] ; then
  1028. _currentRoot="$_w"
  1029. fi
  1030. _debug "_currentRoot" "$_currentRoot"
  1031. _index=$(_math $_index + 1)
  1032. vtype="$VTYPE_HTTP"
  1033. if _startswith "$_currentRoot" "dns" ; then
  1034. vtype="$VTYPE_DNS"
  1035. fi
  1036. _info "Getting token for domain" $d
  1037. _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}"
  1038. if [ ! -z "$code" ] && [ ! "$code" = '201' ] ; then
  1039. _err "new-authz error: $response"
  1040. _clearup
  1041. return 1
  1042. fi
  1043. entry="$(printf "$response" | egrep -o '\{[^{]*"type":"'$vtype'"[^}]*')"
  1044. _debug entry "$entry"
  1045. if [ -z "$entry" ] ; then
  1046. _err "Error, can not get domain token $d"
  1047. _clearup
  1048. return 1
  1049. fi
  1050. token="$(printf "$entry" | egrep -o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  1051. _debug token $token
  1052. uri="$(printf "$entry" | egrep -o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )"
  1053. _debug uri $uri
  1054. keyauthorization="$token.$thumbprint"
  1055. _debug keyauthorization "$keyauthorization"
  1056. dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
  1057. _debug dvlist "$dvlist"
  1058. vlist="$vlist$dvlist,"
  1059. done
  1060. #add entry
  1061. dnsadded=""
  1062. ventries=$(echo "$vlist" | tr ',' ' ' )
  1063. for ventry in $ventries
  1064. do
  1065. d=$(echo $ventry | cut -d $sep -f 1)
  1066. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  1067. vtype=$(echo $ventry | cut -d $sep -f 4)
  1068. _currentRoot=$(echo $ventry | cut -d $sep -f 5)
  1069. if [ "$vtype" = "$VTYPE_DNS" ] ; then
  1070. dnsadded='0'
  1071. txtdomain="_acme-challenge.$d"
  1072. _debug txtdomain "$txtdomain"
  1073. txt="$(echo -n $keyauthorization | _digest "sha256" | _urlencode)"
  1074. _debug txt "$txt"
  1075. #dns
  1076. #1. check use api
  1077. d_api=""
  1078. if [ -f "$LE_WORKING_DIR/$d/$_currentRoot" ] ; then
  1079. d_api="$LE_WORKING_DIR/$d/$_currentRoot"
  1080. elif [ -f "$LE_WORKING_DIR/$d/$_currentRoot.sh" ] ; then
  1081. d_api="$LE_WORKING_DIR/$d/$_currentRoot.sh"
  1082. elif [ -f "$LE_WORKING_DIR/$_currentRoot" ] ; then
  1083. d_api="$LE_WORKING_DIR/$_currentRoot"
  1084. elif [ -f "$LE_WORKING_DIR/$_currentRoot.sh" ] ; then
  1085. d_api="$LE_WORKING_DIR/$_currentRoot.sh"
  1086. elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot" ] ; then
  1087. d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot"
  1088. elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" ] ; then
  1089. d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot.sh"
  1090. fi
  1091. _debug d_api "$d_api"
  1092. if [ "$d_api" ] ; then
  1093. _info "Found domain api file: $d_api"
  1094. else
  1095. _err "Add the following TXT record:"
  1096. _err "Domain: $txtdomain"
  1097. _err "TXT value: $txt"
  1098. _err "Please be aware that you prepend _acme-challenge. before your domain"
  1099. _err "so the resulting subdomain will be: $txtdomain"
  1100. continue
  1101. fi
  1102. (
  1103. if ! . $d_api ; then
  1104. _err "Load file $d_api error. Please check your api file and try again."
  1105. return 1
  1106. fi
  1107. addcommand="${_currentRoot}_add"
  1108. if ! _exists $addcommand ; then
  1109. _err "It seems that your api file is not correct, it must have a function named: $addcommand"
  1110. return 1
  1111. fi
  1112. if ! $addcommand $txtdomain $txt ; then
  1113. _err "Error add txt for domain:$txtdomain"
  1114. return 1
  1115. fi
  1116. )
  1117. if [ "$?" != "0" ] ; then
  1118. _clearup
  1119. return 1
  1120. fi
  1121. dnsadded='1'
  1122. fi
  1123. done
  1124. if [ "$dnsadded" = '0' ] ; then
  1125. _savedomainconf "Le_Vlist" "$vlist"
  1126. _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
  1127. _err "Please add the TXT records to the domains, and retry again."
  1128. _clearup
  1129. return 1
  1130. fi
  1131. fi
  1132. if [ "$dnsadded" = '1' ] ; then
  1133. _info "Sleep 60 seconds for the txt records to take effect"
  1134. sleep 60
  1135. fi
  1136. _debug "ok, let's start to verify"
  1137. ventries=$(echo "$vlist" | tr ',' ' ' )
  1138. for ventry in $ventries
  1139. do
  1140. d=$(echo $ventry | cut -d $sep -f 1)
  1141. keyauthorization=$(echo $ventry | cut -d $sep -f 2)
  1142. uri=$(echo $ventry | cut -d $sep -f 3)
  1143. vtype=$(echo $ventry | cut -d $sep -f 4)
  1144. _currentRoot=$(echo $ventry | cut -d $sep -f 5)
  1145. _info "Verifying:$d"
  1146. _debug "d" "$d"
  1147. _debug "keyauthorization" "$keyauthorization"
  1148. _debug "uri" "$uri"
  1149. removelevel=""
  1150. token=""
  1151. _debug "_currentRoot" "$_currentRoot"
  1152. if [ "$vtype" = "$VTYPE_HTTP" ] ; then
  1153. if [ "$_currentRoot" = "no" ] ; then
  1154. _info "Standalone mode server"
  1155. _startserver "$keyauthorization" &
  1156. if [ "$?" != "0" ] ; then
  1157. _clearup
  1158. return 1
  1159. fi
  1160. serverproc="$!"
  1161. sleep 2
  1162. _debug serverproc $serverproc
  1163. else
  1164. if [ "$_currentRoot" = "apache" ] ; then
  1165. wellknown_path="$ACME_DIR"
  1166. else
  1167. wellknown_path="$_currentRoot/.well-known/acme-challenge"
  1168. if [ ! -d "$_currentRoot/.well-known" ] ; then
  1169. removelevel='1'
  1170. elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ] ; then
  1171. removelevel='2'
  1172. else
  1173. removelevel='3'
  1174. fi
  1175. fi
  1176. _debug wellknown_path "$wellknown_path"
  1177. token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
  1178. _debug "writing token:$token to $wellknown_path/$token"
  1179. mkdir -p "$wellknown_path"
  1180. printf "%s" "$keyauthorization" > "$wellknown_path/$token"
  1181. if [ ! "$usingApache" ] ; then
  1182. webroot_owner=$(_stat $_currentRoot)
  1183. _debug "Changing owner/group of .well-known to $webroot_owner"
  1184. chown -R $webroot_owner "$_currentRoot/.well-known"
  1185. fi
  1186. fi
  1187. fi
  1188. _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}"
  1189. if [ ! -z "$code" ] && [ ! "$code" = '202' ] ; then
  1190. _err "$d:Challenge error: $response"
  1191. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1192. _clearup
  1193. return 1
  1194. fi
  1195. waittimes=0
  1196. if [ -z "$MAX_RETRY_TIMES" ] ; then
  1197. MAX_RETRY_TIMES=30
  1198. fi
  1199. while true ; do
  1200. waittimes=$(_math $waittimes + 1)
  1201. if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ] ; then
  1202. _err "$d:Timeout"
  1203. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1204. _clearup
  1205. return 1
  1206. fi
  1207. _debug "sleep 5 secs to verify"
  1208. sleep 5
  1209. _debug "checking"
  1210. response="$(_get $uri | tr -d "\r\n ")"
  1211. if [ "$?" != "0" ] ; then
  1212. _err "$d:Verify error:$response"
  1213. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1214. _clearup
  1215. return 1
  1216. fi
  1217. status=$(echo $response | egrep -o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
  1218. if [ "$status" = "valid" ] ; then
  1219. _info "Success"
  1220. _stopserver $serverproc
  1221. serverproc=""
  1222. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1223. break;
  1224. fi
  1225. if [ "$status" = "invalid" ] ; then
  1226. error=$(echo $response | egrep -o '"error":{[^}]*}' | grep -o '"detail":"[^"]*"' | cut -d '"' -f 4)
  1227. _err "$d:Verify error:$error"
  1228. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1229. _clearup
  1230. return 1;
  1231. fi
  1232. if [ "$status" = "pending" ] ; then
  1233. _info "Pending"
  1234. else
  1235. _err "$d:Verify error:$response"
  1236. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  1237. _clearup
  1238. return 1
  1239. fi
  1240. done
  1241. done
  1242. _clearup
  1243. _info "Verify finished, start to sign."
  1244. der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)"
  1245. _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64"
  1246. Le_LinkCert="$(grep -i -o '^Location.*$' $HTTP_HEADER | head -1 | tr -d "\r\n" | cut -d " " -f 2)"
  1247. _savedomainconf "Le_LinkCert" "$Le_LinkCert"
  1248. if [ "$Le_LinkCert" ] ; then
  1249. echo "$BEGIN_CERT" > "$CERT_PATH"
  1250. _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH"
  1251. echo "$END_CERT" >> "$CERT_PATH"
  1252. _info "Cert success."
  1253. cat "$CERT_PATH"
  1254. _info "Your cert is in $CERT_PATH"
  1255. cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
  1256. if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ] ; then
  1257. USER_PATH="$PATH"
  1258. _saveaccountconf "USER_PATH" "$USER_PATH"
  1259. fi
  1260. fi
  1261. if [ -z "$Le_LinkCert" ] ; then
  1262. response="$(echo $response | _dbase64 "multiline" )"
  1263. _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')"
  1264. return 1
  1265. fi
  1266. _cleardomainconf "Le_Vlist"
  1267. Le_LinkIssuer=$(grep -i '^Link' $HTTP_HEADER | head -1 | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' )
  1268. _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
  1269. if [ "$Le_LinkIssuer" ] ; then
  1270. echo "$BEGIN_CERT" > "$CA_CERT_PATH"
  1271. _get "$Le_LinkIssuer" | _base64 "multiline" >> "$CA_CERT_PATH"
  1272. echo "$END_CERT" >> "$CA_CERT_PATH"
  1273. _info "The intermediate CA cert is in $CA_CERT_PATH"
  1274. cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH"
  1275. _info "And the full chain certs is there: $CERT_FULLCHAIN_PATH"
  1276. fi
  1277. Le_CertCreateTime=$(date -u "+%s")
  1278. _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
  1279. Le_CertCreateTimeStr=$(date -u )
  1280. _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
  1281. if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "80" ] ; then
  1282. Le_RenewalDays=80
  1283. fi
  1284. _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
  1285. Le_NextRenewTime=$(_math $Le_CertCreateTime + $Le_RenewalDays \* 24 \* 60 \* 60)
  1286. _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
  1287. Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime )
  1288. _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
  1289. _output="$(installcert $Le_Domain "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" 2>&1)"
  1290. _ret="$?"
  1291. if [ "$_ret" = "9" ] ; then
  1292. #ignore the empty install error.
  1293. return 0
  1294. fi
  1295. if [ "$_ret" != "0" ] ; then
  1296. _err "$_output"
  1297. return 1
  1298. fi
  1299. }
  1300. renew() {
  1301. Le_Domain="$1"
  1302. if [ -z "$Le_Domain" ] ; then
  1303. _err "Usage: $PROJECT_ENTRY --renew -d domain.com"
  1304. return 1
  1305. fi
  1306. _initpath $Le_Domain
  1307. if [ ! -f "$DOMAIN_CONF" ] ; then
  1308. _info "$Le_Domain is not a issued domain, skip."
  1309. return 0;
  1310. fi
  1311. . "$DOMAIN_CONF"
  1312. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then
  1313. _info "Skip, Next renewal time is: $Le_NextRenewTimeStr"
  1314. return 2
  1315. fi
  1316. IS_RENEW="1"
  1317. issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath"
  1318. local res=$?
  1319. IS_RENEW=""
  1320. return $res
  1321. }
  1322. renewAll() {
  1323. _initpath
  1324. for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do
  1325. d=$(echo $d | cut -d '/' -f 1)
  1326. (
  1327. _info "Renew: $d"
  1328. renew "$d"
  1329. )
  1330. done
  1331. }
  1332. installcert() {
  1333. Le_Domain="$1"
  1334. if [ -z "$Le_Domain" ] ; then
  1335. echo "Usage: $PROJECT_ENTRY --installcert -d domain.com [--certpath cert-file-path] [--keypath key-file-path] [--capath ca-cert-file-path] [ --reloadCmd reloadCmd] [--fullchainpath fullchain-path]"
  1336. return 1
  1337. fi
  1338. Le_RealCertPath="$2"
  1339. Le_RealKeyPath="$3"
  1340. Le_RealCACertPath="$4"
  1341. Le_ReloadCmd="$5"
  1342. Le_RealFullChainPath="$6"
  1343. _initpath $Le_Domain
  1344. _savedomainconf "Le_RealCertPath" "$Le_RealCertPath"
  1345. _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath"
  1346. _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath"
  1347. _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd"
  1348. _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath"
  1349. if [ "$Le_RealCertPath" = "no" ] ; then
  1350. Le_RealCertPath=""
  1351. fi
  1352. if [ "$Le_RealKeyPath" = "no" ] ; then
  1353. Le_RealKeyPath=""
  1354. fi
  1355. if [ "$Le_RealCACertPath" = "no" ] ; then
  1356. Le_RealCACertPath=""
  1357. fi
  1358. if [ "$Le_ReloadCmd" = "no" ] ; then
  1359. Le_ReloadCmd=""
  1360. fi
  1361. if [ "$Le_RealFullChainPath" = "no" ] ; then
  1362. Le_RealFullChainPath=""
  1363. fi
  1364. _installed="0"
  1365. if [ "$Le_RealCertPath" ] ; then
  1366. _installed=1
  1367. _info "Installing cert to:$Le_RealCertPath"
  1368. if [ -f "$Le_RealCertPath" ] ; then
  1369. cp "$Le_RealCertPath" "$Le_RealCertPath".bak
  1370. fi
  1371. cat "$CERT_PATH" > "$Le_RealCertPath"
  1372. fi
  1373. if [ "$Le_RealCACertPath" ] ; then
  1374. _installed=1
  1375. _info "Installing CA to:$Le_RealCACertPath"
  1376. if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ] ; then
  1377. echo "" >> "$Le_RealCACertPath"
  1378. cat "$CA_CERT_PATH" >> "$Le_RealCACertPath"
  1379. else
  1380. if [ -f "$Le_RealCACertPath" ] ; then
  1381. cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak
  1382. fi
  1383. cat "$CA_CERT_PATH" > "$Le_RealCACertPath"
  1384. fi
  1385. fi
  1386. if [ "$Le_RealKeyPath" ] ; then
  1387. _installed=1
  1388. _info "Installing key to:$Le_RealKeyPath"
  1389. if [ -f "$Le_RealKeyPath" ] ; then
  1390. cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak
  1391. fi
  1392. cat "$CERT_KEY_PATH" > "$Le_RealKeyPath"
  1393. fi
  1394. if [ "$Le_RealFullChainPath" ] ; then
  1395. _installed=1
  1396. _info "Installing full chain to:$Le_RealFullChainPath"
  1397. if [ -f "$Le_RealFullChainPath" ] ; then
  1398. cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak
  1399. fi
  1400. cat "$CERT_FULLCHAIN_PATH" > "$Le_RealFullChainPath"
  1401. fi
  1402. if [ "$Le_ReloadCmd" ] ; then
  1403. _installed=1
  1404. _info "Run Le_ReloadCmd: $Le_ReloadCmd"
  1405. if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd") ; then
  1406. _info "Reload success."
  1407. else
  1408. _err "Reload error for :$Le_Domain"
  1409. fi
  1410. fi
  1411. if [ "$_installed" = "0" ] ; then
  1412. _err "Nothing to install. You don't specify any parameter."
  1413. return 9
  1414. fi
  1415. }
  1416. installcronjob() {
  1417. _initpath
  1418. if ! _exists "crontab" ; then
  1419. _err "crontab doesn't exist, so, we can not install cron jobs."
  1420. _err "All your certs will not be renewed automatically."
  1421. _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
  1422. return 1
  1423. fi
  1424. _info "Installing cron job"
  1425. if ! crontab -l | grep "$PROJECT_ENTRY --cron" ; then
  1426. if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ] ; then
  1427. lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
  1428. else
  1429. _err "Can not install cronjob, $PROJECT_ENTRY not found."
  1430. return 1
  1431. fi
  1432. crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab -
  1433. fi
  1434. if [ "$?" != "0" ] ; then
  1435. _err "Install cron job failed. You need to manually renew your certs."
  1436. _err "Or you can add cronjob by yourself:"
  1437. _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
  1438. return 1
  1439. fi
  1440. }
  1441. uninstallcronjob() {
  1442. if ! _exists "crontab" ; then
  1443. return
  1444. fi
  1445. _info "Removing cron job"
  1446. cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")"
  1447. if [ "$cr" ] ; then
  1448. crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -
  1449. LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
  1450. _info LE_WORKING_DIR "$LE_WORKING_DIR"
  1451. fi
  1452. _initpath
  1453. }
  1454. revoke() {
  1455. Le_Domain="$1"
  1456. if [ -z "$Le_Domain" ] ; then
  1457. echo "Usage: $PROJECT_ENTRY --revoke -d domain.com"
  1458. return 1
  1459. fi
  1460. _initpath $Le_Domain
  1461. if [ ! -f "$DOMAIN_CONF" ] ; then
  1462. _err "$Le_Domain is not a issued domain, skip."
  1463. return 1;
  1464. fi
  1465. if [ ! -f "$CERT_PATH" ] ; then
  1466. _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
  1467. return 1
  1468. fi
  1469. cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}"| tr -d "\r\n" | _urlencode)"
  1470. if [ -z "$cert" ] ; then
  1471. _err "Cert for $Le_Domain is empty found, skip."
  1472. return 1
  1473. fi
  1474. data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
  1475. uri="$API/acme/revoke-cert"
  1476. _info "Try domain key first."
  1477. if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then
  1478. if [ -z "$response" ] ; then
  1479. _info "Revoke success."
  1480. rm -f $CERT_PATH
  1481. return 0
  1482. else
  1483. _err "Revoke error by domain key."
  1484. _err "$resource"
  1485. fi
  1486. fi
  1487. _info "Then try account key."
  1488. if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH" ; then
  1489. if [ -z "$response" ] ; then
  1490. _info "Revoke success."
  1491. rm -f $CERT_PATH
  1492. return 0
  1493. else
  1494. _err "Revoke error."
  1495. _debug "$resource"
  1496. fi
  1497. fi
  1498. return 1
  1499. }
  1500. # Detect profile file if not specified as environment variable
  1501. _detect_profile() {
  1502. if [ -n "$PROFILE" -a -f "$PROFILE" ] ; then
  1503. echo "$PROFILE"
  1504. return
  1505. fi
  1506. local DETECTED_PROFILE
  1507. DETECTED_PROFILE=''
  1508. local SHELLTYPE
  1509. SHELLTYPE="$(basename "/$SHELL")"
  1510. if [ "$SHELLTYPE" = "bash" ] ; then
  1511. if [ -f "$HOME/.bashrc" ] ; then
  1512. DETECTED_PROFILE="$HOME/.bashrc"
  1513. elif [ -f "$HOME/.bash_profile" ] ; then
  1514. DETECTED_PROFILE="$HOME/.bash_profile"
  1515. fi
  1516. elif [ "$SHELLTYPE" = "zsh" ] ; then
  1517. DETECTED_PROFILE="$HOME/.zshrc"
  1518. fi
  1519. if [ -z "$DETECTED_PROFILE" ] ; then
  1520. if [ -f "$HOME/.profile" ] ; then
  1521. DETECTED_PROFILE="$HOME/.profile"
  1522. elif [ -f "$HOME/.bashrc" ] ; then
  1523. DETECTED_PROFILE="$HOME/.bashrc"
  1524. elif [ -f "$HOME/.bash_profile" ] ; then
  1525. DETECTED_PROFILE="$HOME/.bash_profile"
  1526. elif [ -f "$HOME/.zshrc" ] ; then
  1527. DETECTED_PROFILE="$HOME/.zshrc"
  1528. fi
  1529. fi
  1530. if [ ! -z "$DETECTED_PROFILE" ] ; then
  1531. echo "$DETECTED_PROFILE"
  1532. fi
  1533. }
  1534. _initconf() {
  1535. _initpath
  1536. if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
  1537. echo "#ACCOUNT_CONF_PATH=xxxx
  1538. #Account configurations:
  1539. #Here are the supported macros, uncomment them to make them take effect.
  1540. #[email protected] # the account email used to register account.
  1541. #ACCOUNT_KEY_PATH=\"/path/to/account.key\"
  1542. #CERT_HOME=\"/path/to/cert/home\"
  1543. #STAGE=1 # Use the staging api
  1544. #FORCE=1 # Force to issue cert
  1545. #DEBUG=1 # Debug mode
  1546. #ACCOUNT_KEY_HASH=account key hash
  1547. USER_AGENT=\"$USER_AGENT\"
  1548. #USER_PATH=""
  1549. #dns api
  1550. #######################
  1551. #Cloudflare:
  1552. #api key
  1553. #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\"
  1554. #account email
  1555. #CF_Email=\"[email protected]\"
  1556. #######################
  1557. #Dnspod.cn:
  1558. #api key id
  1559. #DP_Id=\"1234\"
  1560. #api key
  1561. #DP_Key=\"sADDsdasdgdsf\"
  1562. #######################
  1563. #Cloudxns.com:
  1564. #CX_Key=\"1234\"
  1565. #
  1566. #CX_Secret=\"sADDsdasdgdsf\"
  1567. " > $ACCOUNT_CONF_PATH
  1568. fi
  1569. }
  1570. _precheck() {
  1571. if ! _exists "curl" && ! _exists "wget"; then
  1572. _err "Please install curl or wget first, we need to access http resources."
  1573. return 1
  1574. fi
  1575. if ! _exists "crontab" ; then
  1576. _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
  1577. _err "We need to set cron job to renew the certs automatically."
  1578. _err "Otherwise, your certs will not be able to be renewed automatically."
  1579. if [ -z "$FORCE" ] ; then
  1580. _err "Please add '--force' and try install again to go without crontab."
  1581. _err "./$PROJECT_ENTRY --install --force"
  1582. return 1
  1583. fi
  1584. fi
  1585. if ! _exists "openssl" ; then
  1586. _err "Please install openssl first."
  1587. _err "We need openssl to generate keys."
  1588. return 1
  1589. fi
  1590. if ! _exists "nc" ; then
  1591. _err "It is recommended to install nc first, try to install 'nc' or 'netcat'."
  1592. _err "We use nc for standalone server if you use standalone mode."
  1593. _err "If you don't use standalone mode, just ignore this warning."
  1594. fi
  1595. return 0
  1596. }
  1597. _setShebang() {
  1598. _file="$1"
  1599. _shebang="$2"
  1600. if [ -z "$_shebang" ] ; then
  1601. _err "Usage: file shebang"
  1602. return 1
  1603. fi
  1604. cp "$_file" "$_file.tmp"
  1605. echo "$_shebang" > "$_file"
  1606. sed -n 2,99999p "$_file.tmp" >> "$_file"
  1607. rm -f "$_file.tmp"
  1608. }
  1609. _installalias() {
  1610. _initpath
  1611. _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  1612. if [ "$_upgrading" ] && [ "$_upgrading" = "1" ] ; then
  1613. echo "$(cat $_envfile)" | sed "s|^LE_WORKING_DIR.*$||" > "$_envfile"
  1614. echo "$(cat $_envfile)" | sed "s|^alias le.*$||" > "$_envfile"
  1615. echo "$(cat $_envfile)" | sed "s|^alias le.sh.*$||" > "$_envfile"
  1616. fi
  1617. _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
  1618. _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
  1619. _profile="$(_detect_profile)"
  1620. if [ "$_profile" ] ; then
  1621. _debug "Found profile: $_profile"
  1622. _setopt "$_profile" ". \"$_envfile\""
  1623. _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
  1624. else
  1625. _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
  1626. fi
  1627. #for csh
  1628. _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
  1629. _csh_profile="$HOME/.cshrc"
  1630. if [ -f "$_csh_profile" ] ; then
  1631. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  1632. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
  1633. _setopt "$_csh_profile" "source \"$_cshfile\""
  1634. fi
  1635. #for tcsh
  1636. _tcsh_profile="$HOME/.tcshrc"
  1637. if [ -f "$_tcsh_profile" ] ; then
  1638. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  1639. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\""
  1640. _setopt "$_tcsh_profile" "source \"$_cshfile\""
  1641. fi
  1642. }
  1643. install() {
  1644. if ! _initpath ; then
  1645. _err "Install failed."
  1646. return 1
  1647. fi
  1648. if ! _precheck ; then
  1649. _err "Pre-check failed, can not install."
  1650. return 1
  1651. fi
  1652. #convert from le
  1653. if [ -d "$HOME/.le" ] ; then
  1654. for envfile in "le.env" "le.sh.env"
  1655. do
  1656. if [ -f "$HOME/.le/$envfile" ] ; then
  1657. if grep "le.sh" "$HOME/.le/$envfile" >/dev/null ; then
  1658. _upgrading="1"
  1659. _info "You are upgrading from le.sh"
  1660. _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
  1661. mv "$HOME/.le" "$LE_WORKING_DIR"
  1662. mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  1663. break;
  1664. fi
  1665. fi
  1666. done
  1667. fi
  1668. _info "Installing to $LE_WORKING_DIR"
  1669. if ! mkdir -p "$LE_WORKING_DIR" ; then
  1670. _err "Can not craete working dir: $LE_WORKING_DIR"
  1671. return 1
  1672. fi
  1673. chmod 700 "$LE_WORKING_DIR"
  1674. cp $PROJECT_ENTRY "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
  1675. if [ "$?" != "0" ] ; then
  1676. _err "Install failed, can not copy $PROJECT_ENTRY"
  1677. return 1
  1678. fi
  1679. _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
  1680. _installalias
  1681. if [ -d "dnsapi" ] ; then
  1682. mkdir -p $LE_WORKING_DIR/dnsapi
  1683. cp dnsapi/* $LE_WORKING_DIR/dnsapi/
  1684. fi
  1685. if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then
  1686. _initconf
  1687. fi
  1688. if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ] ; then
  1689. _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
  1690. fi
  1691. if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ] ; then
  1692. _saveaccountconf "CERT_HOME" "$CERT_HOME"
  1693. fi
  1694. if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ] ; then
  1695. _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
  1696. fi
  1697. installcronjob
  1698. if [ -z "$NO_DETECT_SH" ] ; then
  1699. #Modify shebang
  1700. if _exists bash ; then
  1701. _info "Good, bash is installed, change the shebang to use bash as prefered."
  1702. _shebang='#!/usr/bin/env bash'
  1703. _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
  1704. if [ -d "$LE_WORKING_DIR/dnsapi" ] ; then
  1705. for _apifile in $(ls "$LE_WORKING_DIR/dnsapi/"*.sh) ; do
  1706. _setShebang "$_apifile" "$_shebang"
  1707. done
  1708. fi
  1709. fi
  1710. fi
  1711. _info OK
  1712. }
  1713. uninstall() {
  1714. uninstallcronjob
  1715. _initpath
  1716. _profile="$(_detect_profile)"
  1717. if [ "$_profile" ] ; then
  1718. text="$(cat $_profile)"
  1719. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" > "$_profile"
  1720. fi
  1721. _csh_profile="$HOME/.cshrc"
  1722. if [ -f "$_csh_profile" ] ; then
  1723. text="$(cat $_csh_profile)"
  1724. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_csh_profile"
  1725. fi
  1726. _tcsh_profile="$HOME/.tcshrc"
  1727. if [ -f "$_tcsh_profile" ] ; then
  1728. text="$(cat $_tcsh_profile)"
  1729. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_tcsh_profile"
  1730. fi
  1731. rm -f $LE_WORKING_DIR/$PROJECT_ENTRY
  1732. _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself."
  1733. }
  1734. cron() {
  1735. IN_CRON=1
  1736. renewAll
  1737. IN_CRON=""
  1738. }
  1739. version() {
  1740. echo "$PROJECT"
  1741. echo "v$VER"
  1742. }
  1743. showhelp() {
  1744. version
  1745. echo "Usage: $PROJECT_ENTRY command ...[parameters]....
  1746. Commands:
  1747. --help, -h Show this help message.
  1748. --version, -v Show version info.
  1749. --install Install $PROJECT_NAME to your system.
  1750. --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
  1751. --issue Issue a cert.
  1752. --installcert Install the issued cert to apache/nginx or any other server.
  1753. --renew, -r Renew a cert.
  1754. --renewAll Renew all the certs
  1755. --revoke Revoke a cert.
  1756. --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
  1757. --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
  1758. --cron Run cron job to renew all the certs.
  1759. --toPkcs Export the certificate and key to a pfx file.
  1760. --createAccountKey, -cak Create an account private key, professional use.
  1761. --createDomainKey, -cdk Create an domain private key, professional use.
  1762. --createCSR, -ccsr Create CSR , professional use.
  1763. Parameters:
  1764. --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
  1765. --force, -f Used to force to install or force to renew a cert immediately.
  1766. --staging, --test Use staging server, just for test.
  1767. --debug Output debug info.
  1768. --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
  1769. --standalone Use standalone mode.
  1770. --apache Use apache mode.
  1771. --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
  1772. --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
  1773. --accountkeylength, -ak [2048] Specifies the account key length.
  1774. These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
  1775. --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path.
  1776. --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path.
  1777. --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path.
  1778. --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path.
  1779. --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
  1780. --accountconf Specifies a customized account config file.
  1781. --home Specifies the home dir for $PROJECT_NAME .
  1782. --certhome Specifies the home dir to save all the certs.
  1783. --useragent Specifies the user agent string. it will be saved for future use too.
  1784. --accountemail Specifies the account email for registering, Only valid for the '--install' command.
  1785. --accountkey Specifies the account key path, Only valid for the '--install' command.
  1786. --days Specifies the days to renew the cert when using '--issue' command. The max value is 80 days.
  1787. "
  1788. }
  1789. _installOnline() {
  1790. _info "Installing from online archive."
  1791. if [ ! "$BRANCH" ] ; then
  1792. BRANCH="master"
  1793. fi
  1794. _initpath
  1795. target="$PROJECT/archive/$BRANCH.tar.gz"
  1796. _info "Downloading $target"
  1797. localname="$BRANCH.tar.gz"
  1798. if ! _get "$target" > $localname ; then
  1799. _debug "Download error."
  1800. return 1
  1801. fi
  1802. _info "Extracting $localname"
  1803. tar xzf $localname
  1804. cd "$PROJECT_NAME-$BRANCH"
  1805. chmod +x $PROJECT_ENTRY
  1806. if ./$PROJECT_ENTRY install ; then
  1807. _info "Install success!"
  1808. fi
  1809. cd ..
  1810. rm -rf "$PROJECT_NAME-$BRANCH"
  1811. rm -f "$localname"
  1812. }
  1813. _process() {
  1814. _CMD=""
  1815. _domain=""
  1816. _altdomains="no"
  1817. _webroot=""
  1818. _keylength="no"
  1819. _accountkeylength="no"
  1820. _certpath="no"
  1821. _keypath="no"
  1822. _capath="no"
  1823. _fullchainpath="no"
  1824. _reloadcmd=""
  1825. _password=""
  1826. _accountconf=""
  1827. _useragent=""
  1828. _accountemail=""
  1829. _accountkey=""
  1830. _certhome=""
  1831. while [ ${#} -gt 0 ] ; do
  1832. case "${1}" in
  1833. --help|-h)
  1834. showhelp
  1835. return
  1836. ;;
  1837. --version|-v)
  1838. version
  1839. return
  1840. ;;
  1841. --install)
  1842. _CMD="install"
  1843. ;;
  1844. --uninstall)
  1845. _CMD="uninstall"
  1846. ;;
  1847. --issue)
  1848. _CMD="issue"
  1849. ;;
  1850. --installcert|-i)
  1851. _CMD="installcert"
  1852. ;;
  1853. --renew|-r)
  1854. _CMD="renew"
  1855. ;;
  1856. --renewAll|--renewall)
  1857. _CMD="renewAll"
  1858. ;;
  1859. --revoke)
  1860. _CMD="revoke"
  1861. ;;
  1862. --installcronjob)
  1863. _CMD="installcronjob"
  1864. ;;
  1865. --uninstallcronjob)
  1866. _CMD="uninstallcronjob"
  1867. ;;
  1868. --cron)
  1869. _CMD="cron"
  1870. ;;
  1871. --toPkcs)
  1872. _CMD="toPkcs"
  1873. ;;
  1874. --createAccountKey|--createaccountkey|-cak)
  1875. _CMD="createAccountKey"
  1876. ;;
  1877. --createDomainKey|--createdomainkey|-cdk)
  1878. _CMD="createDomainKey"
  1879. ;;
  1880. --createCSR|--createcsr|-ccr)
  1881. _CMD="createCSR"
  1882. ;;
  1883. --domain|-d)
  1884. _dvalue="$2"
  1885. if [ -z "$_dvalue" ] || _startswith "$_dvalue" "-" ; then
  1886. _err "'$_dvalue' is not a valid domain for parameter '$1'"
  1887. return 1
  1888. fi
  1889. if [ -z "$_domain" ] ; then
  1890. _domain="$_dvalue"
  1891. else
  1892. if [ "$_altdomains" = "no" ] ; then
  1893. _altdomains="$_dvalue"
  1894. else
  1895. _altdomains="$_altdomains,$_dvalue"
  1896. fi
  1897. fi
  1898. shift
  1899. ;;
  1900. --force|-f)
  1901. FORCE="1"
  1902. ;;
  1903. --staging|--test)
  1904. STAGE="1"
  1905. ;;
  1906. --debug)
  1907. if [ -z "$2" ] || _startswith "$2" "-" ; then
  1908. DEBUG="1"
  1909. else
  1910. DEBUG="$2"
  1911. shift
  1912. fi
  1913. ;;
  1914. --webroot|-w)
  1915. wvalue="$2"
  1916. if [ -z "$_webroot" ] ; then
  1917. _webroot="$wvalue"
  1918. else
  1919. _webroot="$_webroot,$wvalue"
  1920. fi
  1921. shift
  1922. ;;
  1923. --standalone)
  1924. wvalue="no"
  1925. if [ -z "$_webroot" ] ; then
  1926. _webroot="$wvalue"
  1927. else
  1928. _webroot="$_webroot,$wvalue"
  1929. fi
  1930. ;;
  1931. --apache)
  1932. wvalue="apache"
  1933. if [ -z "$_webroot" ] ; then
  1934. _webroot="$wvalue"
  1935. else
  1936. _webroot="$_webroot,$wvalue"
  1937. fi
  1938. ;;
  1939. --dns)
  1940. wvalue="dns"
  1941. if ! _startswith "$2" "-" ; then
  1942. wvalue="$2"
  1943. shift
  1944. fi
  1945. if [ -z "$_webroot" ] ; then
  1946. _webroot="$wvalue"
  1947. else
  1948. _webroot="$_webroot,$wvalue"
  1949. fi
  1950. ;;
  1951. --keylength|-k)
  1952. _keylength="$2"
  1953. accountkeylength="$2"
  1954. shift
  1955. ;;
  1956. --accountkeylength|-ak)
  1957. accountkeylength="$2"
  1958. shift
  1959. ;;
  1960. --certpath)
  1961. _certpath="$2"
  1962. shift
  1963. ;;
  1964. --keypath)
  1965. _keypath="$2"
  1966. shift
  1967. ;;
  1968. --capath)
  1969. _capath="$2"
  1970. shift
  1971. ;;
  1972. --fullchainpath)
  1973. _fullchainpath="$2"
  1974. shift
  1975. ;;
  1976. --reloadcmd|--reloadCmd)
  1977. _reloadcmd="$2"
  1978. shift
  1979. ;;
  1980. --password)
  1981. _password="$2"
  1982. shift
  1983. ;;
  1984. --accountconf)
  1985. _accountconf="$2"
  1986. ACCOUNT_CONF_PATH="$_accountconf"
  1987. shift
  1988. ;;
  1989. --home)
  1990. LE_WORKING_DIR="$2"
  1991. shift
  1992. ;;
  1993. --certhome)
  1994. _certhome="$2"
  1995. CERT_HOME="$_certhome"
  1996. shift
  1997. ;;
  1998. --useragent)
  1999. _useragent="$2"
  2000. USER_AGENT="$_useragent"
  2001. shift
  2002. ;;
  2003. --accountemail )
  2004. _accountemail="$2"
  2005. ACCOUNT_EMAIL="$_accountemail"
  2006. shift
  2007. ;;
  2008. --accountkey )
  2009. _accountkey="$2"
  2010. ACCOUNT_KEY_PATH="$_accountkey"
  2011. shift
  2012. ;;
  2013. --days )
  2014. _days="$2"
  2015. Le_RenewalDays="$_days"
  2016. shift
  2017. ;;
  2018. *)
  2019. _err "Unknown parameter : $1"
  2020. return 1
  2021. ;;
  2022. esac
  2023. shift 1
  2024. done
  2025. case "${_CMD}" in
  2026. install) install ;;
  2027. uninstall) uninstall ;;
  2028. issue)
  2029. issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath"
  2030. ;;
  2031. installcert)
  2032. installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath"
  2033. ;;
  2034. renew)
  2035. renew "$_domain"
  2036. ;;
  2037. renewAll)
  2038. renewAll
  2039. ;;
  2040. revoke)
  2041. revoke "$_domain"
  2042. ;;
  2043. installcronjob) installcronjob ;;
  2044. uninstallcronjob) uninstallcronjob ;;
  2045. cron) cron ;;
  2046. toPkcs)
  2047. toPkcs "$_domain" "$_password"
  2048. ;;
  2049. createAccountKey)
  2050. createAccountKey "$_domain" "$_accountkeylength"
  2051. ;;
  2052. createDomainKey)
  2053. createDomainKey "$_domain" "$_keylength"
  2054. ;;
  2055. createCSR)
  2056. createCSR "$_domain" "$_altdomains"
  2057. ;;
  2058. *)
  2059. _err "Invalid command: $_CMD"
  2060. showhelp;
  2061. return 1
  2062. ;;
  2063. esac
  2064. _ret="$?"
  2065. if [ "$_ret" != "0" ] ; then
  2066. return $_ret
  2067. fi
  2068. if [ "$_useragent" ] ; then
  2069. _saveaccountconf "USER_AGENT" "$_useragent"
  2070. fi
  2071. if [ "$_accountemail" ] ; then
  2072. _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
  2073. fi
  2074. }
  2075. if [ "$INSTALLONLINE" ] ; then
  2076. INSTALLONLINE=""
  2077. _installOnline $BRANCH
  2078. exit
  2079. fi
  2080. if [ -z "$1" ] ; then
  2081. showhelp
  2082. else
  2083. if echo "$1" | grep "^-" >/dev/null 2>&1 ; then
  2084. _process "$@"
  2085. else
  2086. "$@"
  2087. fi
  2088. fi