acme.sh 119 KB

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