acme.sh 120 KB

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