acme.sh 113 KB

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