acme.sh 148 KB

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