acme.sh 148 KB

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