acme.sh 92 KB

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