acme.sh 149 KB

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