acme.sh 111 KB

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