acme.sh 149 KB

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