acme.sh 139 KB

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