acme.sh 133 KB

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