acme.sh 121 KB

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