acme.sh 161 KB

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