acme.sh 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809
  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 *" | grep -v server_name | _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. _skip_ssl=1
  2352. for _listen_i in $(echo "$_seg_n" | tr "\t" ' ' | grep "^ *listen" | tr -d " "); do
  2353. if [ "$_listen_i" ]; then
  2354. if [ "$(echo "$_listen_i" | _egrep_o "listen.*ssl[ |;]")" ]; then
  2355. _debug2 "$_listen_i is ssl"
  2356. else
  2357. _debug2 "$_listen_i is plain text"
  2358. _skip_ssl=""
  2359. break
  2360. fi
  2361. fi
  2362. done
  2363. if [ "$_skip_ssl" = "1" ]; then
  2364. _debug "ssl on, skip"
  2365. else
  2366. FOUND_REAL_NGINX_CONF_LN=$_fln
  2367. _debug3 "found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN"
  2368. return 0
  2369. fi
  2370. fi
  2371. done
  2372. fi
  2373. return 1
  2374. }
  2375. #restore all the nginx conf
  2376. _restoreNginx() {
  2377. if [ -z "$NGINX_RESTORE_VLIST" ]; then
  2378. _debug "No need to restore nginx, skip."
  2379. return
  2380. fi
  2381. _debug "_restoreNginx"
  2382. _debug "NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST"
  2383. for ng_entry in $(echo "$NGINX_RESTORE_VLIST" | tr "$dvsep" ' '); do
  2384. _debug "ng_entry" "$ng_entry"
  2385. _nd=$(echo "$ng_entry" | cut -d "$sep" -f 1)
  2386. _ngconf=$(echo "$ng_entry" | cut -d "$sep" -f 2)
  2387. _ngbackupconf=$(echo "$ng_entry" | cut -d "$sep" -f 3)
  2388. _info "Restoring from $_ngbackupconf to $_ngconf"
  2389. cat "$_ngbackupconf" >"$_ngconf"
  2390. done
  2391. _info "Reload nginx"
  2392. if ! _exec "nginx -s reload" >/dev/null; then
  2393. _exec_err
  2394. _err "It seems that nginx reload error, please report bug."
  2395. return 1
  2396. fi
  2397. return 0
  2398. }
  2399. _clearup() {
  2400. _stopserver "$serverproc"
  2401. serverproc=""
  2402. _restoreApache
  2403. _restoreNginx
  2404. _clearupdns
  2405. if [ -z "$DEBUG" ]; then
  2406. rm -f "$TLS_CONF"
  2407. rm -f "$TLS_CERT"
  2408. rm -f "$TLS_KEY"
  2409. rm -f "$TLS_CSR"
  2410. fi
  2411. }
  2412. _clearupdns() {
  2413. _debug "_clearupdns"
  2414. if [ "$dnsadded" != 1 ] || [ -z "$vlist" ]; then
  2415. _debug "skip dns."
  2416. return
  2417. fi
  2418. ventries=$(echo "$vlist" | tr ',' ' ')
  2419. for ventry in $ventries; do
  2420. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  2421. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  2422. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  2423. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  2424. txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
  2425. _debug txt "$txt"
  2426. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  2427. _debug "$d is already verified, skip $vtype."
  2428. continue
  2429. fi
  2430. if [ "$vtype" != "$VTYPE_DNS" ]; then
  2431. _info "Skip $d for $vtype"
  2432. continue
  2433. fi
  2434. d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
  2435. _debug d_api "$d_api"
  2436. if [ -z "$d_api" ]; then
  2437. _info "Not Found domain api file: $d_api"
  2438. continue
  2439. fi
  2440. (
  2441. if ! . "$d_api"; then
  2442. _err "Load file $d_api error. Please check your api file and try again."
  2443. return 1
  2444. fi
  2445. rmcommand="${_currentRoot}_rm"
  2446. if ! _exists "$rmcommand"; then
  2447. _err "It seems that your api file doesn't define $rmcommand"
  2448. return 1
  2449. fi
  2450. txtdomain="_acme-challenge.$d"
  2451. if ! $rmcommand "$txtdomain" "$txt"; then
  2452. _err "Error removing txt for domain:$txtdomain"
  2453. return 1
  2454. fi
  2455. )
  2456. done
  2457. }
  2458. # webroot removelevel tokenfile
  2459. _clearupwebbroot() {
  2460. __webroot="$1"
  2461. if [ -z "$__webroot" ]; then
  2462. _debug "no webroot specified, skip"
  2463. return 0
  2464. fi
  2465. _rmpath=""
  2466. if [ "$2" = '1' ]; then
  2467. _rmpath="$__webroot/.well-known"
  2468. elif [ "$2" = '2' ]; then
  2469. _rmpath="$__webroot/.well-known/acme-challenge"
  2470. elif [ "$2" = '3' ]; then
  2471. _rmpath="$__webroot/.well-known/acme-challenge/$3"
  2472. else
  2473. _debug "Skip for removelevel:$2"
  2474. fi
  2475. if [ "$_rmpath" ]; then
  2476. if [ "$DEBUG" ]; then
  2477. _debug "Debugging, skip removing: $_rmpath"
  2478. else
  2479. rm -rf "$_rmpath"
  2480. fi
  2481. fi
  2482. return 0
  2483. }
  2484. _on_before_issue() {
  2485. _chk_web_roots="$1"
  2486. _chk_main_domain="$2"
  2487. _chk_alt_domains="$3"
  2488. _chk_pre_hook="$4"
  2489. _chk_local_addr="$5"
  2490. _debug _on_before_issue
  2491. #run pre hook
  2492. if [ "$_chk_pre_hook" ]; then
  2493. _info "Run pre hook:'$_chk_pre_hook'"
  2494. if ! (
  2495. cd "$DOMAIN_PATH" && eval "$_chk_pre_hook"
  2496. ); then
  2497. _err "Error when run pre hook."
  2498. return 1
  2499. fi
  2500. fi
  2501. if _hasfield "$_chk_web_roots" "$NO_VALUE"; then
  2502. if ! _exists "socat"; then
  2503. _err "Please install socat tools first."
  2504. return 1
  2505. fi
  2506. fi
  2507. _debug Le_LocalAddress "$_chk_local_addr"
  2508. alldomains=$(echo "$_chk_main_domain,$_chk_alt_domains" | tr ',' ' ')
  2509. _index=1
  2510. _currentRoot=""
  2511. _addrIndex=1
  2512. for d in $alldomains; do
  2513. _debug "Check for domain" "$d"
  2514. _currentRoot="$(_getfield "$_chk_web_roots" $_index)"
  2515. _debug "_currentRoot" "$_currentRoot"
  2516. _index=$(_math $_index + 1)
  2517. _checkport=""
  2518. if [ "$_currentRoot" = "$NO_VALUE" ]; then
  2519. _info "Standalone mode."
  2520. if [ -z "$Le_HTTPPort" ]; then
  2521. Le_HTTPPort=80
  2522. else
  2523. _savedomainconf "Le_HTTPPort" "$Le_HTTPPort"
  2524. fi
  2525. _checkport="$Le_HTTPPort"
  2526. elif [ "$_currentRoot" = "$W_TLS" ]; then
  2527. _info "Standalone tls mode."
  2528. if [ -z "$Le_TLSPort" ]; then
  2529. Le_TLSPort=443
  2530. else
  2531. _savedomainconf "Le_TLSPort" "$Le_TLSPort"
  2532. fi
  2533. _checkport="$Le_TLSPort"
  2534. fi
  2535. if [ "$_checkport" ]; then
  2536. _debug _checkport "$_checkport"
  2537. _checkaddr="$(_getfield "$_chk_local_addr" $_addrIndex)"
  2538. _debug _checkaddr "$_checkaddr"
  2539. _addrIndex="$(_math $_addrIndex + 1)"
  2540. _netprc="$(_ss "$_checkport" | grep "$_checkport")"
  2541. netprc="$(echo "$_netprc" | grep "$_checkaddr")"
  2542. if [ -z "$netprc" ]; then
  2543. netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")"
  2544. fi
  2545. if [ "$netprc" ]; then
  2546. _err "$netprc"
  2547. _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)"
  2548. _err "Please stop it first"
  2549. return 1
  2550. fi
  2551. fi
  2552. done
  2553. if _hasfield "$_chk_web_roots" "apache"; then
  2554. if ! _setApache; then
  2555. _err "set up apache error. Report error to me."
  2556. return 1
  2557. fi
  2558. else
  2559. usingApache=""
  2560. fi
  2561. }
  2562. _on_issue_err() {
  2563. _chk_post_hook="$1"
  2564. _chk_vlist="$2"
  2565. _debug _on_issue_err
  2566. if [ "$LOG_FILE" ]; then
  2567. _err "Please check log file for more details: $LOG_FILE"
  2568. else
  2569. _err "Please add '--debug' or '--log' to check more details."
  2570. _err "See: $_DEBUG_WIKI"
  2571. fi
  2572. #run the post hook
  2573. if [ "$_chk_post_hook" ]; then
  2574. _info "Run post hook:'$_chk_post_hook'"
  2575. if ! (
  2576. cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
  2577. ); then
  2578. _err "Error when run post hook."
  2579. return 1
  2580. fi
  2581. fi
  2582. #trigger the validation to flush the pending authz
  2583. _debug2 "_chk_vlist" "$_chk_vlist"
  2584. if [ "$_chk_vlist" ]; then
  2585. (
  2586. _debug2 "start to deactivate authz"
  2587. ventries=$(echo "$_chk_vlist" | tr "$dvsep" ' ')
  2588. for ventry in $ventries; do
  2589. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  2590. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  2591. uri=$(echo "$ventry" | cut -d "$sep" -f 3)
  2592. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  2593. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  2594. __trigger_validation "$uri" "$keyauthorization"
  2595. done
  2596. )
  2597. fi
  2598. if [ "$IS_RENEW" = "1" ] && _hasfield "$Le_Webroot" "dns"; then
  2599. _err "$_DNS_MANUAL_ERR"
  2600. fi
  2601. if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then
  2602. _debug "$(_dlg_versions)"
  2603. fi
  2604. }
  2605. _on_issue_success() {
  2606. _chk_post_hook="$1"
  2607. _chk_renew_hook="$2"
  2608. _debug _on_issue_success
  2609. #run the post hook
  2610. if [ "$_chk_post_hook" ]; then
  2611. _info "Run post hook:'$_chk_post_hook'"
  2612. if ! (
  2613. cd "$DOMAIN_PATH" && eval "$_chk_post_hook"
  2614. ); then
  2615. _err "Error when run post hook."
  2616. return 1
  2617. fi
  2618. fi
  2619. #run renew hook
  2620. if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then
  2621. _info "Run renew hook:'$_chk_renew_hook'"
  2622. if ! (
  2623. cd "$DOMAIN_PATH" && eval "$_chk_renew_hook"
  2624. ); then
  2625. _err "Error when run renew hook."
  2626. return 1
  2627. fi
  2628. fi
  2629. if _hasfield "$Le_Webroot" "dns"; then
  2630. _err "$_DNS_MANUAL_WARN"
  2631. fi
  2632. }
  2633. updateaccount() {
  2634. _initpath
  2635. _regAccount
  2636. }
  2637. registeraccount() {
  2638. _reg_length="$1"
  2639. _initpath
  2640. _regAccount "$_reg_length"
  2641. }
  2642. __calcAccountKeyHash() {
  2643. [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH"
  2644. }
  2645. __calc_account_thumbprint() {
  2646. printf "%s" "$jwk" | tr -d ' ' | _digest "sha256" | _url_replace
  2647. }
  2648. #keylength
  2649. _regAccount() {
  2650. _initpath
  2651. _reg_length="$1"
  2652. _debug3 _regAccount "$_regAccount"
  2653. mkdir -p "$CA_DIR"
  2654. if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
  2655. _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
  2656. mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
  2657. fi
  2658. if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
  2659. _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
  2660. mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
  2661. fi
  2662. if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
  2663. if ! _create_account_key "$_reg_length"; then
  2664. _err "Create account key error."
  2665. return 1
  2666. fi
  2667. fi
  2668. if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
  2669. return 1
  2670. fi
  2671. _initAPI
  2672. _reg_res="$ACME_NEW_ACCOUNT_RES"
  2673. regjson='{"resource": "'$_reg_res'", "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
  2674. if [ "$ACCOUNT_EMAIL" ]; then
  2675. regjson='{"resource": "'$_reg_res'", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}'
  2676. fi
  2677. _info "Registering account"
  2678. if ! _send_signed_request "${ACME_NEW_ACCOUNT}" "$regjson"; then
  2679. _err "Register account Error: $response"
  2680. return 1
  2681. fi
  2682. if [ "$code" = "" ] || [ "$code" = '201' ]; then
  2683. echo "$response" >"$ACCOUNT_JSON_PATH"
  2684. _info "Registered"
  2685. elif [ "$code" = '409' ]; then
  2686. _info "Already registered"
  2687. else
  2688. _err "Register account Error: $response"
  2689. return 1
  2690. fi
  2691. _accUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
  2692. _debug "_accUri" "$_accUri"
  2693. _savecaconf "ACCOUNT_URL" "$_accUri"
  2694. echo "$response" >"$ACCOUNT_JSON_PATH"
  2695. CA_KEY_HASH="$(__calcAccountKeyHash)"
  2696. _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH"
  2697. _savecaconf CA_KEY_HASH "$CA_KEY_HASH"
  2698. if [ "$code" = '403' ]; then
  2699. _err "It seems that the account key is already deactivated, please use a new account key."
  2700. return 1
  2701. fi
  2702. ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)"
  2703. _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT"
  2704. }
  2705. #Implement deactivate account
  2706. deactivateaccount() {
  2707. _initpath
  2708. if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then
  2709. _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH"
  2710. mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH"
  2711. fi
  2712. if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then
  2713. _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH"
  2714. mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH"
  2715. fi
  2716. if [ ! -f "$ACCOUNT_KEY_PATH" ]; then
  2717. _err "Account key is not found at: $ACCOUNT_KEY_PATH"
  2718. return 1
  2719. fi
  2720. _accUri=$(_readcaconf "ACCOUNT_URL")
  2721. _debug _accUri "$_accUri"
  2722. if [ -z "$_accUri" ]; then
  2723. _err "The account url is empty, please run '--update-account' first to update the account info first,"
  2724. _err "Then try again."
  2725. return 1
  2726. fi
  2727. if ! _calcjwk "$ACCOUNT_KEY_PATH"; then
  2728. return 1
  2729. fi
  2730. _initAPI
  2731. if _send_signed_request "$_accUri" "{\"resource\": \"reg\", \"status\":\"deactivated\"}" && _contains "$response" '"deactivated"'; then
  2732. _info "Deactivate account success for $_accUri."
  2733. _accid=$(echo "$response" | _egrep_o "\"id\" *: *[^,]*," | cut -d : -f 2 | tr -d ' ,')
  2734. elif [ "$code" = "403" ]; then
  2735. _info "The account is already deactivated."
  2736. _accid=$(_getfield "$_accUri" "999" "/")
  2737. else
  2738. _err "Deactivate: account failed for $_accUri."
  2739. return 1
  2740. fi
  2741. _debug "Account id: $_accid"
  2742. if [ "$_accid" ]; then
  2743. _deactivated_account_path="$CA_DIR/deactivated/$_accid"
  2744. _debug _deactivated_account_path "$_deactivated_account_path"
  2745. if mkdir -p "$_deactivated_account_path"; then
  2746. _info "Moving deactivated account info to $_deactivated_account_path/"
  2747. mv "$CA_CONF" "$_deactivated_account_path/"
  2748. mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/"
  2749. mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/"
  2750. else
  2751. _err "Can not create dir: $_deactivated_account_path, try to remove the deactivated account key."
  2752. rm -f "$CA_CONF"
  2753. rm -f "$ACCOUNT_JSON_PATH"
  2754. rm -f "$ACCOUNT_KEY_PATH"
  2755. fi
  2756. fi
  2757. }
  2758. # domain folder file
  2759. _findHook() {
  2760. _hookdomain="$1"
  2761. _hookcat="$2"
  2762. _hookname="$3"
  2763. if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then
  2764. d_api="$_SCRIPT_HOME/$_hookcat/$_hookname"
  2765. elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then
  2766. d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh"
  2767. elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then
  2768. d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname"
  2769. elif [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then
  2770. d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh"
  2771. elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then
  2772. d_api="$LE_WORKING_DIR/$_hookname"
  2773. elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then
  2774. d_api="$LE_WORKING_DIR/$_hookname.sh"
  2775. elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then
  2776. d_api="$LE_WORKING_DIR/$_hookcat/$_hookname"
  2777. elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then
  2778. d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh"
  2779. fi
  2780. printf "%s" "$d_api"
  2781. }
  2782. #domain
  2783. __get_domain_new_authz() {
  2784. _gdnd="$1"
  2785. _info "Getting new-authz for domain" "$_gdnd"
  2786. _initAPI
  2787. _Max_new_authz_retry_times=5
  2788. _authz_i=0
  2789. while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do
  2790. _debug "Try new-authz for the $_authz_i time."
  2791. if ! _send_signed_request "${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then
  2792. _err "Can not get domain new authz."
  2793. return 1
  2794. fi
  2795. if _contains "$response" "No registration exists matching provided key"; then
  2796. _err "It seems there is an error, but it's recovered now, please try again."
  2797. _err "If you see this message for a second time, please report bug: $(__green "$PROJECT")"
  2798. _clearcaconf "CA_KEY_HASH"
  2799. break
  2800. fi
  2801. if ! _contains "$response" "An error occurred while processing your request"; then
  2802. _info "The new-authz request is ok."
  2803. break
  2804. fi
  2805. _authz_i="$(_math "$_authz_i" + 1)"
  2806. _info "The server is busy, Sleep $_authz_i to retry."
  2807. _sleep "$_authz_i"
  2808. done
  2809. if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then
  2810. _err "new-authz retry reach the max $_Max_new_authz_retry_times times."
  2811. fi
  2812. if [ ! -z "$code" ] && [ ! "$code" = '201' ]; then
  2813. _err "new-authz error: $response"
  2814. return 1
  2815. fi
  2816. }
  2817. #uri keyAuthorization
  2818. __trigger_validation() {
  2819. _debug2 "tigger domain validation."
  2820. _t_url="$1"
  2821. _debug2 _t_url "$_t_url"
  2822. _t_key_authz="$2"
  2823. _debug2 _t_key_authz "$_t_key_authz"
  2824. _send_signed_request "$_t_url" "{\"resource\": \"challenge\", \"keyAuthorization\": \"$_t_key_authz\"}"
  2825. }
  2826. #webroot, domain domainlist keylength
  2827. issue() {
  2828. if [ -z "$2" ]; then
  2829. _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ "
  2830. return 1
  2831. fi
  2832. if [ -z "$1" ]; then
  2833. _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc."
  2834. return 1
  2835. fi
  2836. _web_roots="$1"
  2837. _main_domain="$2"
  2838. _alt_domains="$3"
  2839. if _contains "$_main_domain" ","; then
  2840. _main_domain=$(echo "$2,$3" | cut -d , -f 1)
  2841. _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//")
  2842. fi
  2843. _key_length="$4"
  2844. _real_cert="$5"
  2845. _real_key="$6"
  2846. _real_ca="$7"
  2847. _reload_cmd="$8"
  2848. _real_fullchain="$9"
  2849. _pre_hook="${10}"
  2850. _post_hook="${11}"
  2851. _renew_hook="${12}"
  2852. _local_addr="${13}"
  2853. #remove these later.
  2854. if [ "$_web_roots" = "dns-cf" ]; then
  2855. _web_roots="dns_cf"
  2856. fi
  2857. if [ "$_web_roots" = "dns-dp" ]; then
  2858. _web_roots="dns_dp"
  2859. fi
  2860. if [ "$_web_roots" = "dns-cx" ]; then
  2861. _web_roots="dns_cx"
  2862. fi
  2863. if [ ! "$IS_RENEW" ]; then
  2864. _initpath "$_main_domain" "$_key_length"
  2865. mkdir -p "$DOMAIN_PATH"
  2866. fi
  2867. _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY"
  2868. _initAPI
  2869. if [ -f "$DOMAIN_CONF" ]; then
  2870. Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime)
  2871. _debug Le_NextRenewTime "$Le_NextRenewTime"
  2872. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
  2873. _saved_domain=$(_readdomainconf Le_Domain)
  2874. _debug _saved_domain "$_saved_domain"
  2875. _saved_alt=$(_readdomainconf Le_Alt)
  2876. _debug _saved_alt "$_saved_alt"
  2877. if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then
  2878. _info "Domains not changed."
  2879. _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")"
  2880. _info "Add '$(__red '--force')' to force to renew."
  2881. return $RENEW_SKIP
  2882. else
  2883. _info "Domains have changed."
  2884. fi
  2885. fi
  2886. fi
  2887. _savedomainconf "Le_Domain" "$_main_domain"
  2888. _savedomainconf "Le_Alt" "$_alt_domains"
  2889. _savedomainconf "Le_Webroot" "$_web_roots"
  2890. _savedomainconf "Le_PreHook" "$_pre_hook"
  2891. _savedomainconf "Le_PostHook" "$_post_hook"
  2892. _savedomainconf "Le_RenewHook" "$_renew_hook"
  2893. if [ "$_local_addr" ]; then
  2894. _savedomainconf "Le_LocalAddress" "$_local_addr"
  2895. else
  2896. _cleardomainconf "Le_LocalAddress"
  2897. fi
  2898. Le_API="$ACME_DIRECTORY"
  2899. _savedomainconf "Le_API" "$Le_API"
  2900. if [ "$_alt_domains" = "$NO_VALUE" ]; then
  2901. _alt_domains=""
  2902. fi
  2903. if [ "$_key_length" = "$NO_VALUE" ]; then
  2904. _key_length=""
  2905. fi
  2906. if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then
  2907. _err "_on_before_issue."
  2908. return 1
  2909. fi
  2910. _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")"
  2911. _debug2 _saved_account_key_hash "$_saved_account_key_hash"
  2912. if [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then
  2913. if ! _regAccount "$_accountkeylength"; then
  2914. _on_issue_err "$_post_hook"
  2915. return 1
  2916. fi
  2917. else
  2918. _debug "_saved_account_key_hash is not changed, skip register account."
  2919. fi
  2920. if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then
  2921. _info "Signing from existing CSR."
  2922. else
  2923. _key=$(_readdomainconf Le_Keylength)
  2924. _debug "Read key length:$_key"
  2925. if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then
  2926. if ! createDomainKey "$_main_domain" "$_key_length"; then
  2927. _err "Create domain key error."
  2928. _clearup
  2929. _on_issue_err "$_post_hook"
  2930. return 1
  2931. fi
  2932. fi
  2933. if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then
  2934. _err "Create CSR error."
  2935. _clearup
  2936. _on_issue_err "$_post_hook"
  2937. return 1
  2938. fi
  2939. fi
  2940. _savedomainconf "Le_Keylength" "$_key_length"
  2941. vlist="$Le_Vlist"
  2942. _info "Getting domain auth token for each domain"
  2943. sep='#'
  2944. dvsep=','
  2945. if [ -z "$vlist" ]; then
  2946. alldomains=$(echo "$_main_domain,$_alt_domains" | tr ',' ' ')
  2947. _index=1
  2948. _currentRoot=""
  2949. for d in $alldomains; do
  2950. _info "Getting webroot for domain" "$d"
  2951. _w="$(echo $_web_roots | cut -d , -f $_index)"
  2952. _debug _w "$_w"
  2953. if [ "$_w" ]; then
  2954. _currentRoot="$_w"
  2955. fi
  2956. _debug "_currentRoot" "$_currentRoot"
  2957. _index=$(_math $_index + 1)
  2958. vtype="$VTYPE_HTTP"
  2959. if _startswith "$_currentRoot" "dns"; then
  2960. vtype="$VTYPE_DNS"
  2961. fi
  2962. if [ "$_currentRoot" = "$W_TLS" ]; then
  2963. vtype="$VTYPE_TLS"
  2964. fi
  2965. if ! __get_domain_new_authz "$d"; then
  2966. _clearup
  2967. _on_issue_err "$_post_hook"
  2968. return 1
  2969. fi
  2970. if [ -z "$thumbprint" ]; then
  2971. thumbprint="$(__calc_account_thumbprint)"
  2972. fi
  2973. entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
  2974. _debug entry "$entry"
  2975. if [ -z "$entry" ]; then
  2976. _err "Error, can not get domain token $d"
  2977. _clearup
  2978. _on_issue_err "$_post_hook"
  2979. return 1
  2980. fi
  2981. token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  2982. _debug token "$token"
  2983. uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d '"' -f 4)"
  2984. _debug uri "$uri"
  2985. keyauthorization="$token.$thumbprint"
  2986. _debug keyauthorization "$keyauthorization"
  2987. if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then
  2988. _debug "$d is already verified, skip."
  2989. keyauthorization="$STATE_VERIFIED"
  2990. _debug keyauthorization "$keyauthorization"
  2991. fi
  2992. dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot"
  2993. _debug dvlist "$dvlist"
  2994. vlist="$vlist$dvlist$dvsep"
  2995. done
  2996. _debug vlist "$vlist"
  2997. #add entry
  2998. dnsadded=""
  2999. ventries=$(echo "$vlist" | tr "$dvsep" ' ')
  3000. for ventry in $ventries; do
  3001. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  3002. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  3003. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  3004. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  3005. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  3006. _debug "$d is already verified, skip $vtype."
  3007. continue
  3008. fi
  3009. if [ "$vtype" = "$VTYPE_DNS" ]; then
  3010. dnsadded='0'
  3011. txtdomain="_acme-challenge.$d"
  3012. _debug txtdomain "$txtdomain"
  3013. txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)"
  3014. _debug txt "$txt"
  3015. d_api="$(_findHook "$d" dnsapi "$_currentRoot")"
  3016. _debug d_api "$d_api"
  3017. if [ "$d_api" ]; then
  3018. _info "Found domain api file: $d_api"
  3019. else
  3020. _info "$(__red "Add the following TXT record:")"
  3021. _info "$(__red "Domain: '$(__green "$txtdomain")'")"
  3022. _info "$(__red "TXT value: '$(__green "$txt")'")"
  3023. _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")"
  3024. _info "$(__red "so the resulting subdomain will be: $txtdomain")"
  3025. continue
  3026. fi
  3027. (
  3028. if ! . "$d_api"; then
  3029. _err "Load file $d_api error. Please check your api file and try again."
  3030. return 1
  3031. fi
  3032. addcommand="${_currentRoot}_add"
  3033. if ! _exists "$addcommand"; then
  3034. _err "It seems that your api file is not correct, it must have a function named: $addcommand"
  3035. return 1
  3036. fi
  3037. if ! $addcommand "$txtdomain" "$txt"; then
  3038. _err "Error add txt for domain:$txtdomain"
  3039. return 1
  3040. fi
  3041. )
  3042. if [ "$?" != "0" ]; then
  3043. _clearup
  3044. _on_issue_err "$_post_hook" "$vlist"
  3045. return 1
  3046. fi
  3047. dnsadded='1'
  3048. fi
  3049. done
  3050. if [ "$dnsadded" = '0' ]; then
  3051. _savedomainconf "Le_Vlist" "$vlist"
  3052. _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit."
  3053. _err "Please add the TXT records to the domains, and retry again."
  3054. _clearup
  3055. _on_issue_err "$_post_hook"
  3056. return 1
  3057. fi
  3058. fi
  3059. if [ "$dnsadded" = '1' ]; then
  3060. if [ -z "$Le_DNSSleep" ]; then
  3061. Le_DNSSleep="$DEFAULT_DNS_SLEEP"
  3062. else
  3063. _savedomainconf "Le_DNSSleep" "$Le_DNSSleep"
  3064. fi
  3065. _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect"
  3066. _sleep "$Le_DNSSleep"
  3067. fi
  3068. NGINX_RESTORE_VLIST=""
  3069. _debug "ok, let's start to verify"
  3070. _ncIndex=1
  3071. ventries=$(echo "$vlist" | tr "$dvsep" ' ')
  3072. for ventry in $ventries; do
  3073. d=$(echo "$ventry" | cut -d "$sep" -f 1)
  3074. keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2)
  3075. uri=$(echo "$ventry" | cut -d "$sep" -f 3)
  3076. vtype=$(echo "$ventry" | cut -d "$sep" -f 4)
  3077. _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5)
  3078. if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then
  3079. _info "$d is already verified, skip $vtype."
  3080. continue
  3081. fi
  3082. _info "Verifying:$d"
  3083. _debug "d" "$d"
  3084. _debug "keyauthorization" "$keyauthorization"
  3085. _debug "uri" "$uri"
  3086. removelevel=""
  3087. token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)"
  3088. _debug "_currentRoot" "$_currentRoot"
  3089. if [ "$vtype" = "$VTYPE_HTTP" ]; then
  3090. if [ "$_currentRoot" = "$NO_VALUE" ]; then
  3091. _info "Standalone mode server"
  3092. _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
  3093. _ncIndex="$(_math $_ncIndex + 1)"
  3094. _startserver "$keyauthorization" "$_ncaddr"
  3095. if [ "$?" != "0" ]; then
  3096. _clearup
  3097. _on_issue_err "$_post_hook" "$vlist"
  3098. return 1
  3099. fi
  3100. sleep 1
  3101. _debug serverproc "$serverproc"
  3102. elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then
  3103. _info "Stateless mode for domain:$d"
  3104. _sleep 1
  3105. elif _startswith "$_currentRoot" "$NGINX"; then
  3106. _info "Nginx mode for domain:$d"
  3107. #set up nginx server
  3108. FOUND_REAL_NGINX_CONF=""
  3109. BACKUP_NGINX_CONF=""
  3110. if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then
  3111. _clearup
  3112. _on_issue_err "$_post_hook" "$vlist"
  3113. return 1
  3114. fi
  3115. if [ "$FOUND_REAL_NGINX_CONF" ]; then
  3116. _realConf="$FOUND_REAL_NGINX_CONF"
  3117. _backup="$BACKUP_NGINX_CONF"
  3118. _debug _realConf "$_realConf"
  3119. NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST"
  3120. fi
  3121. _sleep 1
  3122. else
  3123. if [ "$_currentRoot" = "apache" ]; then
  3124. wellknown_path="$ACME_DIR"
  3125. else
  3126. wellknown_path="$_currentRoot/.well-known/acme-challenge"
  3127. if [ ! -d "$_currentRoot/.well-known" ]; then
  3128. removelevel='1'
  3129. elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then
  3130. removelevel='2'
  3131. else
  3132. removelevel='3'
  3133. fi
  3134. fi
  3135. _debug wellknown_path "$wellknown_path"
  3136. _debug "writing token:$token to $wellknown_path/$token"
  3137. mkdir -p "$wellknown_path"
  3138. if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then
  3139. _err "$d:Can not write token to file : $wellknown_path/$token"
  3140. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3141. _clearup
  3142. _on_issue_err "$_post_hook" "$vlist"
  3143. return 1
  3144. fi
  3145. if [ ! "$usingApache" ]; then
  3146. if webroot_owner=$(_stat "$_currentRoot"); then
  3147. _debug "Changing owner/group of .well-known to $webroot_owner"
  3148. if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then
  3149. _debug "$(cat "$_EXEC_TEMP_ERR")"
  3150. _exec_err >/dev/null 2>&1
  3151. fi
  3152. else
  3153. _debug "not changing owner/group of webroot"
  3154. fi
  3155. fi
  3156. fi
  3157. elif [ "$vtype" = "$VTYPE_TLS" ]; then
  3158. #create A
  3159. #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )"
  3160. #_debug2 _hash_A "$_hash_A"
  3161. #_x="$(echo $_hash_A | cut -c 1-32)"
  3162. #_debug2 _x "$_x"
  3163. #_y="$(echo $_hash_A | cut -c 33-64)"
  3164. #_debug2 _y "$_y"
  3165. #_SAN_A="$_x.$_y.token.acme.invalid"
  3166. #_debug2 _SAN_A "$_SAN_A"
  3167. #create B
  3168. _hash_B="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")"
  3169. _debug2 _hash_B "$_hash_B"
  3170. _x="$(echo "$_hash_B" | cut -c 1-32)"
  3171. _debug2 _x "$_x"
  3172. _y="$(echo "$_hash_B" | cut -c 33-64)"
  3173. _debug2 _y "$_y"
  3174. #_SAN_B="$_x.$_y.ka.acme.invalid"
  3175. _SAN_B="$_x.$_y.acme.invalid"
  3176. _debug2 _SAN_B "$_SAN_B"
  3177. _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")"
  3178. _ncIndex="$(_math "$_ncIndex" + 1)"
  3179. if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" "$_ncaddr"; then
  3180. _err "Start tls server error."
  3181. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3182. _clearup
  3183. _on_issue_err "$_post_hook" "$vlist"
  3184. return 1
  3185. fi
  3186. fi
  3187. if ! __trigger_validation "$uri" "$keyauthorization"; then
  3188. _err "$d:Can not get challenge: $response"
  3189. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3190. _clearup
  3191. _on_issue_err "$_post_hook" "$vlist"
  3192. return 1
  3193. fi
  3194. if [ ! -z "$code" ] && [ ! "$code" = '202' ]; then
  3195. _err "$d:Challenge error: $response"
  3196. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3197. _clearup
  3198. _on_issue_err "$_post_hook" "$vlist"
  3199. return 1
  3200. fi
  3201. waittimes=0
  3202. if [ -z "$MAX_RETRY_TIMES" ]; then
  3203. MAX_RETRY_TIMES=30
  3204. fi
  3205. while true; do
  3206. waittimes=$(_math "$waittimes" + 1)
  3207. if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then
  3208. _err "$d:Timeout"
  3209. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3210. _clearup
  3211. _on_issue_err "$_post_hook" "$vlist"
  3212. return 1
  3213. fi
  3214. _debug "sleep 2 secs to verify"
  3215. sleep 2
  3216. _debug "checking"
  3217. response="$(_get "$uri")"
  3218. if [ "$?" != "0" ]; then
  3219. _err "$d:Verify error:$response"
  3220. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3221. _clearup
  3222. _on_issue_err "$_post_hook" "$vlist"
  3223. return 1
  3224. fi
  3225. _debug2 original "$response"
  3226. response="$(echo "$response" | _normalizeJson)"
  3227. _debug2 response "$response"
  3228. status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"')
  3229. if [ "$status" = "valid" ]; then
  3230. _info "$(__green Success)"
  3231. _stopserver "$serverproc"
  3232. serverproc=""
  3233. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3234. break
  3235. fi
  3236. if [ "$status" = "invalid" ]; then
  3237. error="$(echo "$response" | tr -d "\r\n" | _egrep_o '"error":\{[^\}]*')"
  3238. _debug2 error "$error"
  3239. errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)"
  3240. _debug2 errordetail "$errordetail"
  3241. if [ "$errordetail" ]; then
  3242. _err "$d:Verify error:$errordetail"
  3243. else
  3244. _err "$d:Verify error:$error"
  3245. fi
  3246. if [ "$DEBUG" ]; then
  3247. if [ "$vtype" = "$VTYPE_HTTP" ]; then
  3248. _debug "Debug: get token url."
  3249. _get "http://$d/.well-known/acme-challenge/$token" "" 1
  3250. fi
  3251. fi
  3252. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3253. _clearup
  3254. _on_issue_err "$_post_hook" "$vlist"
  3255. return 1
  3256. fi
  3257. if [ "$status" = "pending" ]; then
  3258. _info "Pending"
  3259. else
  3260. _err "$d:Verify error:$response"
  3261. _clearupwebbroot "$_currentRoot" "$removelevel" "$token"
  3262. _clearup
  3263. _on_issue_err "$_post_hook" "$vlist"
  3264. return 1
  3265. fi
  3266. done
  3267. done
  3268. _clearup
  3269. _info "Verify finished, start to sign."
  3270. der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)"
  3271. if ! _send_signed_request "${ACME_NEW_ORDER}" "{\"resource\": \"$ACME_NEW_ORDER_RES\", \"csr\": \"$der\"}" "needbase64"; then
  3272. _err "Sign failed."
  3273. _on_issue_err "$_post_hook"
  3274. return 1
  3275. fi
  3276. _rcert="$response"
  3277. Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
  3278. _debug "Le_LinkCert" "$Le_LinkCert"
  3279. _savedomainconf "Le_LinkCert" "$Le_LinkCert"
  3280. if [ "$Le_LinkCert" ]; then
  3281. echo "$BEGIN_CERT" >"$CERT_PATH"
  3282. #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then
  3283. # _debug "Get cert failed. Let's try last response."
  3284. # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH"
  3285. #fi
  3286. if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then
  3287. _debug "Try cert link."
  3288. _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH"
  3289. fi
  3290. echo "$END_CERT" >>"$CERT_PATH"
  3291. _info "$(__green "Cert success.")"
  3292. cat "$CERT_PATH"
  3293. _info "Your cert is in $(__green " $CERT_PATH ")"
  3294. if [ -f "$CERT_KEY_PATH" ]; then
  3295. _info "Your cert key is in $(__green " $CERT_KEY_PATH ")"
  3296. fi
  3297. cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH"
  3298. if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ]; then
  3299. USER_PATH="$PATH"
  3300. _saveaccountconf "USER_PATH" "$USER_PATH"
  3301. fi
  3302. fi
  3303. if [ -z "$Le_LinkCert" ]; then
  3304. response="$(echo "$response" | _dbase64 "multiline" | _normalizeJson)"
  3305. _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')"
  3306. _on_issue_err "$_post_hook"
  3307. return 1
  3308. fi
  3309. _cleardomainconf "Le_Vlist"
  3310. Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>')
  3311. if ! _contains "$Le_LinkIssuer" ":"; then
  3312. _info "$(__red "Relative issuer link found.")"
  3313. Le_LinkIssuer="$_ACME_SERVER_HOST$Le_LinkIssuer"
  3314. fi
  3315. _debug Le_LinkIssuer "$Le_LinkIssuer"
  3316. _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer"
  3317. if [ "$Le_LinkIssuer" ]; then
  3318. _link_issuer_retry=0
  3319. _MAX_ISSUER_RETRY=5
  3320. while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do
  3321. _debug _link_issuer_retry "$_link_issuer_retry"
  3322. if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then
  3323. echo "$BEGIN_CERT" >"$CA_CERT_PATH"
  3324. _base64 "multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH"
  3325. echo "$END_CERT" >>"$CA_CERT_PATH"
  3326. _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")"
  3327. cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH"
  3328. _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")"
  3329. rm -f "$CA_CERT_PATH.der"
  3330. break
  3331. fi
  3332. _link_issuer_retry=$(_math $_link_issuer_retry + 1)
  3333. _sleep "$_link_issuer_retry"
  3334. done
  3335. if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then
  3336. _err "Max retry for issuer ca cert is reached."
  3337. fi
  3338. else
  3339. _debug "No Le_LinkIssuer header found."
  3340. fi
  3341. Le_CertCreateTime=$(_time)
  3342. _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime"
  3343. Le_CertCreateTimeStr=$(date -u)
  3344. _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr"
  3345. if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ]; then
  3346. Le_RenewalDays="$MAX_RENEW"
  3347. else
  3348. _savedomainconf "Le_RenewalDays" "$Le_RenewalDays"
  3349. fi
  3350. if [ "$CA_BUNDLE" ]; then
  3351. _saveaccountconf CA_BUNDLE "$CA_BUNDLE"
  3352. else
  3353. _clearaccountconf "CA_BUNDLE"
  3354. fi
  3355. if [ "$CA_PATH" ]; then
  3356. _saveaccountconf CA_PATH "$CA_PATH"
  3357. else
  3358. _clearaccountconf "CA_PATH"
  3359. fi
  3360. if [ "$HTTPS_INSECURE" ]; then
  3361. _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE"
  3362. else
  3363. _clearaccountconf "HTTPS_INSECURE"
  3364. fi
  3365. if [ "$Le_Listen_V4" ]; then
  3366. _savedomainconf "Le_Listen_V4" "$Le_Listen_V4"
  3367. _cleardomainconf Le_Listen_V6
  3368. elif [ "$Le_Listen_V6" ]; then
  3369. _savedomainconf "Le_Listen_V6" "$Le_Listen_V6"
  3370. _cleardomainconf Le_Listen_V4
  3371. fi
  3372. if [ "$Le_ForceNewDomainKey" = "1" ]; then
  3373. _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey"
  3374. else
  3375. _cleardomainconf Le_ForceNewDomainKey
  3376. fi
  3377. Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60)
  3378. Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime")
  3379. _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr"
  3380. Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400)
  3381. _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime"
  3382. if ! _on_issue_success "$_post_hook" "$_renew_hook"; then
  3383. _err "Call hook error."
  3384. return 1
  3385. fi
  3386. if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then
  3387. _savedomainconf "Le_RealCertPath" "$_real_cert"
  3388. _savedomainconf "Le_RealCACertPath" "$_real_ca"
  3389. _savedomainconf "Le_RealKeyPath" "$_real_key"
  3390. _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
  3391. _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
  3392. _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
  3393. fi
  3394. }
  3395. #domain [isEcc]
  3396. renew() {
  3397. Le_Domain="$1"
  3398. if [ -z "$Le_Domain" ]; then
  3399. _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]"
  3400. return 1
  3401. fi
  3402. _isEcc="$2"
  3403. _initpath "$Le_Domain" "$_isEcc"
  3404. _info "$(__green "Renew: '$Le_Domain'")"
  3405. if [ ! -f "$DOMAIN_CONF" ]; then
  3406. _info "'$Le_Domain' is not a issued domain, skip."
  3407. return 0
  3408. fi
  3409. if [ "$Le_RenewalDays" ]; then
  3410. _savedomainconf Le_RenewalDays "$Le_RenewalDays"
  3411. fi
  3412. . "$DOMAIN_CONF"
  3413. if [ "$Le_API" ]; then
  3414. if [ "$_OLD_CA_HOST" = "$Le_API" ]; then
  3415. export Le_API="$DEFAULT_CA"
  3416. _savedomainconf Le_API "$Le_API"
  3417. fi
  3418. if [ "$_OLD_STAGE_CA_HOST" = "$Le_API" ]; then
  3419. export Le_API="$STAGE_CA"
  3420. _savedomainconf Le_API "$Le_API"
  3421. fi
  3422. export ACME_DIRECTORY="$Le_API"
  3423. #reload ca configs
  3424. ACCOUNT_KEY_PATH=""
  3425. ACCOUNT_JSON_PATH=""
  3426. CA_CONF=""
  3427. _debug3 "initpath again."
  3428. _initpath "$Le_Domain" "$_isEcc"
  3429. fi
  3430. if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then
  3431. _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")"
  3432. _info "Add '$(__red '--force')' to force to renew."
  3433. return "$RENEW_SKIP"
  3434. fi
  3435. if [ "$IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then
  3436. _info "Skip invalid cert for: $Le_Domain"
  3437. return 0
  3438. fi
  3439. IS_RENEW="1"
  3440. 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"
  3441. res="$?"
  3442. if [ "$res" != "0" ]; then
  3443. return "$res"
  3444. fi
  3445. if [ "$Le_DeployHook" ]; then
  3446. _deploy "$Le_Domain" "$Le_DeployHook"
  3447. res="$?"
  3448. fi
  3449. IS_RENEW=""
  3450. return "$res"
  3451. }
  3452. #renewAll [stopRenewOnError]
  3453. renewAll() {
  3454. _initpath
  3455. _stopRenewOnError="$1"
  3456. _debug "_stopRenewOnError" "$_stopRenewOnError"
  3457. _ret="0"
  3458. for di in "${CERT_HOME}"/*.*/; do
  3459. _debug di "$di"
  3460. if ! [ -d "$di" ]; then
  3461. _debug "Not directory, skip: $di"
  3462. continue
  3463. fi
  3464. d=$(basename "$di")
  3465. _debug d "$d"
  3466. (
  3467. if _endswith "$d" "$ECC_SUFFIX"; then
  3468. _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
  3469. d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
  3470. fi
  3471. renew "$d" "$_isEcc"
  3472. )
  3473. rc="$?"
  3474. _debug "Return code: $rc"
  3475. if [ "$rc" != "0" ]; then
  3476. if [ "$rc" = "$RENEW_SKIP" ]; then
  3477. _info "Skipped $d"
  3478. elif [ "$_stopRenewOnError" ]; then
  3479. _err "Error renew $d, stop now."
  3480. return "$rc"
  3481. else
  3482. _ret="$rc"
  3483. _err "Error renew $d."
  3484. fi
  3485. fi
  3486. done
  3487. return "$_ret"
  3488. }
  3489. #csr webroot
  3490. signcsr() {
  3491. _csrfile="$1"
  3492. _csrW="$2"
  3493. if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then
  3494. _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ "
  3495. return 1
  3496. fi
  3497. _initpath
  3498. _csrsubj=$(_readSubjectFromCSR "$_csrfile")
  3499. if [ "$?" != "0" ]; then
  3500. _err "Can not read subject from csr: $_csrfile"
  3501. return 1
  3502. fi
  3503. _debug _csrsubj "$_csrsubj"
  3504. if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then
  3505. _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it."
  3506. _csrsubj=""
  3507. fi
  3508. _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
  3509. if [ "$?" != "0" ]; then
  3510. _err "Can not read domain list from csr: $_csrfile"
  3511. return 1
  3512. fi
  3513. _debug "_csrdomainlist" "$_csrdomainlist"
  3514. if [ -z "$_csrsubj" ]; then
  3515. _csrsubj="$(_getfield "$_csrdomainlist" 1)"
  3516. _debug _csrsubj "$_csrsubj"
  3517. _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)"
  3518. _debug "_csrdomainlist" "$_csrdomainlist"
  3519. fi
  3520. if [ -z "$_csrsubj" ]; then
  3521. _err "Can not read subject from csr: $_csrfile"
  3522. return 1
  3523. fi
  3524. _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
  3525. if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
  3526. _err "Can not read key length from csr: $_csrfile"
  3527. return 1
  3528. fi
  3529. _initpath "$_csrsubj" "$_csrkeylength"
  3530. mkdir -p "$DOMAIN_PATH"
  3531. _info "Copy csr to: $CSR_PATH"
  3532. cp "$_csrfile" "$CSR_PATH"
  3533. issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength"
  3534. }
  3535. showcsr() {
  3536. _csrfile="$1"
  3537. _csrd="$2"
  3538. if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then
  3539. _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr"
  3540. return 1
  3541. fi
  3542. _initpath
  3543. _csrsubj=$(_readSubjectFromCSR "$_csrfile")
  3544. if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then
  3545. _err "Can not read subject from csr: $_csrfile"
  3546. return 1
  3547. fi
  3548. _info "Subject=$_csrsubj"
  3549. _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile")
  3550. if [ "$?" != "0" ]; then
  3551. _err "Can not read domain list from csr: $_csrfile"
  3552. return 1
  3553. fi
  3554. _debug "_csrdomainlist" "$_csrdomainlist"
  3555. _info "SubjectAltNames=$_csrdomainlist"
  3556. _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile")
  3557. if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then
  3558. _err "Can not read key length from csr: $_csrfile"
  3559. return 1
  3560. fi
  3561. _info "KeyLength=$_csrkeylength"
  3562. }
  3563. list() {
  3564. _raw="$1"
  3565. _initpath
  3566. _sep="|"
  3567. if [ "$_raw" ]; then
  3568. printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew"
  3569. for di in "${CERT_HOME}"/*.*/; do
  3570. if ! [ -d "$di" ]; then
  3571. _debug "Not directory, skip: $di"
  3572. continue
  3573. fi
  3574. d=$(basename "$di")
  3575. _debug d "$d"
  3576. (
  3577. if _endswith "$d" "$ECC_SUFFIX"; then
  3578. _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2)
  3579. d=$(echo "$d" | cut -d "$ECC_SEP" -f 1)
  3580. fi
  3581. _initpath "$d" "$_isEcc"
  3582. if [ -f "$DOMAIN_CONF" ]; then
  3583. . "$DOMAIN_CONF"
  3584. printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr"
  3585. fi
  3586. )
  3587. done
  3588. else
  3589. if _exists column; then
  3590. list "raw" | column -t -s "$_sep"
  3591. else
  3592. list "raw" | tr "$_sep" '\t'
  3593. fi
  3594. fi
  3595. }
  3596. _deploy() {
  3597. _d="$1"
  3598. _hooks="$2"
  3599. for _d_api in $(echo "$_hooks" | tr ',' " "); do
  3600. _deployApi="$(_findHook "$_d" deploy "$_d_api")"
  3601. if [ -z "$_deployApi" ]; then
  3602. _err "The deploy hook $_d_api is not found."
  3603. return 1
  3604. fi
  3605. _debug _deployApi "$_deployApi"
  3606. if ! (
  3607. if ! . "$_deployApi"; then
  3608. _err "Load file $_deployApi error. Please check your api file and try again."
  3609. return 1
  3610. fi
  3611. d_command="${_d_api}_deploy"
  3612. if ! _exists "$d_command"; then
  3613. _err "It seems that your api file is not correct, it must have a function named: $d_command"
  3614. return 1
  3615. fi
  3616. if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then
  3617. _err "Error deploy for domain:$_d"
  3618. return 1
  3619. fi
  3620. ); then
  3621. _err "Deploy error."
  3622. return 1
  3623. else
  3624. _info "$(__green Success)"
  3625. fi
  3626. done
  3627. }
  3628. #domain hooks
  3629. deploy() {
  3630. _d="$1"
  3631. _hooks="$2"
  3632. _isEcc="$3"
  3633. if [ -z "$_hooks" ]; then
  3634. _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] "
  3635. return 1
  3636. fi
  3637. _initpath "$_d" "$_isEcc"
  3638. if [ ! -d "$DOMAIN_PATH" ]; then
  3639. _err "Domain is not valid:'$_d'"
  3640. return 1
  3641. fi
  3642. . "$DOMAIN_CONF"
  3643. _savedomainconf Le_DeployHook "$_hooks"
  3644. _deploy "$_d" "$_hooks"
  3645. }
  3646. installcert() {
  3647. _main_domain="$1"
  3648. if [ -z "$_main_domain" ]; then
  3649. _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]"
  3650. return 1
  3651. fi
  3652. _real_cert="$2"
  3653. _real_key="$3"
  3654. _real_ca="$4"
  3655. _reload_cmd="$5"
  3656. _real_fullchain="$6"
  3657. _isEcc="$7"
  3658. _initpath "$_main_domain" "$_isEcc"
  3659. if [ ! -d "$DOMAIN_PATH" ]; then
  3660. _err "Domain is not valid:'$_main_domain'"
  3661. return 1
  3662. fi
  3663. _savedomainconf "Le_RealCertPath" "$_real_cert"
  3664. _savedomainconf "Le_RealCACertPath" "$_real_ca"
  3665. _savedomainconf "Le_RealKeyPath" "$_real_key"
  3666. _savedomainconf "Le_ReloadCmd" "$_reload_cmd"
  3667. _savedomainconf "Le_RealFullChainPath" "$_real_fullchain"
  3668. _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"
  3669. }
  3670. #domain cert key ca fullchain reloadcmd backup-prefix
  3671. _installcert() {
  3672. _main_domain="$1"
  3673. _real_cert="$2"
  3674. _real_key="$3"
  3675. _real_ca="$4"
  3676. _real_fullchain="$5"
  3677. _reload_cmd="$6"
  3678. _backup_prefix="$7"
  3679. if [ "$_real_cert" = "$NO_VALUE" ]; then
  3680. _real_cert=""
  3681. fi
  3682. if [ "$_real_key" = "$NO_VALUE" ]; then
  3683. _real_key=""
  3684. fi
  3685. if [ "$_real_ca" = "$NO_VALUE" ]; then
  3686. _real_ca=""
  3687. fi
  3688. if [ "$_reload_cmd" = "$NO_VALUE" ]; then
  3689. _reload_cmd=""
  3690. fi
  3691. if [ "$_real_fullchain" = "$NO_VALUE" ]; then
  3692. _real_fullchain=""
  3693. fi
  3694. _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix"
  3695. mkdir -p "$_backup_path"
  3696. if [ "$_real_cert" ]; then
  3697. _info "Installing cert to:$_real_cert"
  3698. if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then
  3699. cp "$_real_cert" "$_backup_path/cert.bak"
  3700. fi
  3701. cat "$CERT_PATH" >"$_real_cert"
  3702. fi
  3703. if [ "$_real_ca" ]; then
  3704. _info "Installing CA to:$_real_ca"
  3705. if [ "$_real_ca" = "$_real_cert" ]; then
  3706. echo "" >>"$_real_ca"
  3707. cat "$CA_CERT_PATH" >>"$_real_ca"
  3708. else
  3709. if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then
  3710. cp "$_real_ca" "$_backup_path/ca.bak"
  3711. fi
  3712. cat "$CA_CERT_PATH" >"$_real_ca"
  3713. fi
  3714. fi
  3715. if [ "$_real_key" ]; then
  3716. _info "Installing key to:$_real_key"
  3717. if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then
  3718. cp "$_real_key" "$_backup_path/key.bak"
  3719. fi
  3720. if [ -f "$_real_key" ]; then
  3721. cat "$CERT_KEY_PATH" >"$_real_key"
  3722. else
  3723. cat "$CERT_KEY_PATH" >"$_real_key"
  3724. chmod 700 "$_real_key"
  3725. fi
  3726. fi
  3727. if [ "$_real_fullchain" ]; then
  3728. _info "Installing full chain to:$_real_fullchain"
  3729. if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then
  3730. cp "$_real_fullchain" "$_backup_path/fullchain.bak"
  3731. fi
  3732. cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain"
  3733. fi
  3734. if [ "$_reload_cmd" ]; then
  3735. _info "Run reload cmd: $_reload_cmd"
  3736. if (
  3737. export CERT_PATH
  3738. export CERT_KEY_PATH
  3739. export CA_CERT_PATH
  3740. export CERT_FULLCHAIN_PATH
  3741. export Le_Domain
  3742. cd "$DOMAIN_PATH" && eval "$_reload_cmd"
  3743. ); then
  3744. _info "$(__green "Reload success")"
  3745. else
  3746. _err "Reload error for :$Le_Domain"
  3747. fi
  3748. fi
  3749. }
  3750. #confighome
  3751. installcronjob() {
  3752. _c_home="$1"
  3753. _initpath
  3754. _CRONTAB="crontab"
  3755. if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
  3756. _CRONTAB="fcrontab"
  3757. fi
  3758. if ! _exists "$_CRONTAB"; then
  3759. _err "crontab/fcrontab doesn't exist, so, we can not install cron jobs."
  3760. _err "All your certs will not be renewed automatically."
  3761. _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday."
  3762. return 1
  3763. fi
  3764. _info "Installing cron job"
  3765. if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then
  3766. if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then
  3767. lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY"
  3768. else
  3769. _err "Can not install cronjob, $PROJECT_ENTRY not found."
  3770. return 1
  3771. fi
  3772. if [ "$_c_home" ]; then
  3773. _c_entry="--config-home \"$_c_home\" "
  3774. fi
  3775. _t=$(_time)
  3776. random_minute=$(_math $_t % 60)
  3777. if _exists uname && uname -a | grep SunOS >/dev/null; then
  3778. $_CRONTAB -l | {
  3779. cat
  3780. echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
  3781. } | $_CRONTAB --
  3782. else
  3783. $_CRONTAB -l | {
  3784. cat
  3785. echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null"
  3786. } | $_CRONTAB -
  3787. fi
  3788. fi
  3789. if [ "$?" != "0" ]; then
  3790. _err "Install cron job failed. You need to manually renew your certs."
  3791. _err "Or you can add cronjob by yourself:"
  3792. _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"
  3793. return 1
  3794. fi
  3795. }
  3796. uninstallcronjob() {
  3797. _CRONTAB="crontab"
  3798. if ! _exists "$_CRONTAB" && _exists "fcrontab"; then
  3799. _CRONTAB="fcrontab"
  3800. fi
  3801. if ! _exists "$_CRONTAB"; then
  3802. return
  3803. fi
  3804. _info "Removing cron job"
  3805. cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")"
  3806. if [ "$cr" ]; then
  3807. if _exists uname && uname -a | grep solaris >/dev/null; then
  3808. $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB --
  3809. else
  3810. $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -
  3811. fi
  3812. LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')"
  3813. _info LE_WORKING_DIR "$LE_WORKING_DIR"
  3814. if _contains "$cr" "--config-home"; then
  3815. LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')"
  3816. _debug LE_CONFIG_HOME "$LE_CONFIG_HOME"
  3817. fi
  3818. fi
  3819. _initpath
  3820. }
  3821. revoke() {
  3822. Le_Domain="$1"
  3823. if [ -z "$Le_Domain" ]; then
  3824. _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]"
  3825. return 1
  3826. fi
  3827. _isEcc="$2"
  3828. _initpath "$Le_Domain" "$_isEcc"
  3829. if [ ! -f "$DOMAIN_CONF" ]; then
  3830. _err "$Le_Domain is not a issued domain, skip."
  3831. return 1
  3832. fi
  3833. if [ ! -f "$CERT_PATH" ]; then
  3834. _err "Cert for $Le_Domain $CERT_PATH is not found, skip."
  3835. return 1
  3836. fi
  3837. cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)"
  3838. if [ -z "$cert" ]; then
  3839. _err "Cert for $Le_Domain is empty found, skip."
  3840. return 1
  3841. fi
  3842. _initAPI
  3843. data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}"
  3844. uri="${ACME_REVOKE_CERT}"
  3845. if [ -f "$CERT_KEY_PATH" ]; then
  3846. _info "Try domain key first."
  3847. if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then
  3848. if [ -z "$response" ]; then
  3849. _info "Revoke success."
  3850. rm -f "$CERT_PATH"
  3851. return 0
  3852. else
  3853. _err "Revoke error by domain key."
  3854. _err "$response"
  3855. fi
  3856. fi
  3857. else
  3858. _info "Domain key file doesn't exists."
  3859. fi
  3860. _info "Try account key."
  3861. if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then
  3862. if [ -z "$response" ]; then
  3863. _info "Revoke success."
  3864. rm -f "$CERT_PATH"
  3865. return 0
  3866. else
  3867. _err "Revoke error."
  3868. _debug "$response"
  3869. fi
  3870. fi
  3871. return 1
  3872. }
  3873. #domain ecc
  3874. remove() {
  3875. Le_Domain="$1"
  3876. if [ -z "$Le_Domain" ]; then
  3877. _usage "Usage: $PROJECT_ENTRY --remove -d domain.com [--ecc]"
  3878. return 1
  3879. fi
  3880. _isEcc="$2"
  3881. _initpath "$Le_Domain" "$_isEcc"
  3882. _removed_conf="$DOMAIN_CONF.removed"
  3883. if [ ! -f "$DOMAIN_CONF" ]; then
  3884. if [ -f "$_removed_conf" ]; then
  3885. _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH"
  3886. else
  3887. _err "$Le_Domain is not a issued domain, skip."
  3888. fi
  3889. return 1
  3890. fi
  3891. if mv "$DOMAIN_CONF" "$_removed_conf"; then
  3892. _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)"
  3893. _info "You can remove them by yourself."
  3894. return 0
  3895. else
  3896. _err "Remove $Le_Domain failed."
  3897. return 1
  3898. fi
  3899. }
  3900. #domain vtype
  3901. _deactivate() {
  3902. _d_domain="$1"
  3903. _d_type="$2"
  3904. _initpath
  3905. if ! __get_domain_new_authz "$_d_domain"; then
  3906. _err "Can not get domain new authz token."
  3907. return 1
  3908. fi
  3909. authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ' ' -f 2 | tr -d "\r\n")"
  3910. _debug "authzUri" "$authzUri"
  3911. if [ "$code" ] && [ ! "$code" = '201' ]; then
  3912. _err "new-authz error: $response"
  3913. return 1
  3914. fi
  3915. entries="$(echo "$response" | _egrep_o '{ *"type":"[^"]*", *"status": *"valid", *"uri"[^}]*')"
  3916. if [ -z "$entries" ]; then
  3917. _info "No valid entries found."
  3918. if [ -z "$thumbprint" ]; then
  3919. thumbprint="$(__calc_account_thumbprint)"
  3920. fi
  3921. _debug "Trigger validation."
  3922. vtype="$VTYPE_HTTP"
  3923. entry="$(printf "%s\n" "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')"
  3924. _debug entry "$entry"
  3925. if [ -z "$entry" ]; then
  3926. _err "Error, can not get domain token $d"
  3927. return 1
  3928. fi
  3929. token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')"
  3930. _debug token "$token"
  3931. uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
  3932. _debug uri "$uri"
  3933. keyauthorization="$token.$thumbprint"
  3934. _debug keyauthorization "$keyauthorization"
  3935. __trigger_validation "$uri" "$keyauthorization"
  3936. fi
  3937. _d_i=0
  3938. _d_max_retry=$(echo "$entries" | wc -l)
  3939. while [ "$_d_i" -lt "$_d_max_retry" ]; do
  3940. _info "Deactivate: $_d_domain"
  3941. _d_i="$(_math $_d_i + 1)"
  3942. entry="$(echo "$entries" | sed -n "${_d_i}p")"
  3943. _debug entry "$entry"
  3944. if [ -z "$entry" ]; then
  3945. _info "No more valid entry found."
  3946. break
  3947. fi
  3948. _vtype="$(printf "%s\n" "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')"
  3949. _debug _vtype "$_vtype"
  3950. _info "Found $_vtype"
  3951. uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*' | cut -d : -f 2,3 | tr -d '"')"
  3952. _debug uri "$uri"
  3953. if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then
  3954. _info "Skip $_vtype"
  3955. continue
  3956. fi
  3957. _info "Deactivate: $_vtype"
  3958. if _send_signed_request "$authzUri" "{\"resource\": \"authz\", \"status\":\"deactivated\"}" && _contains "$response" '"deactivated"'; then
  3959. _info "Deactivate: $_vtype success."
  3960. else
  3961. _err "Can not deactivate $_vtype."
  3962. break
  3963. fi
  3964. done
  3965. _debug "$_d_i"
  3966. if [ "$_d_i" -eq "$_d_max_retry" ]; then
  3967. _info "Deactivated success!"
  3968. else
  3969. _err "Deactivate failed."
  3970. fi
  3971. }
  3972. deactivate() {
  3973. _d_domain_list="$1"
  3974. _d_type="$2"
  3975. _initpath
  3976. _initAPI
  3977. _debug _d_domain_list "$_d_domain_list"
  3978. if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then
  3979. _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]"
  3980. return 1
  3981. fi
  3982. for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do
  3983. if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then
  3984. continue
  3985. fi
  3986. if ! _deactivate "$_d_dm" "$_d_type"; then
  3987. return 1
  3988. fi
  3989. done
  3990. }
  3991. # Detect profile file if not specified as environment variable
  3992. _detect_profile() {
  3993. if [ -n "$PROFILE" -a -f "$PROFILE" ]; then
  3994. echo "$PROFILE"
  3995. return
  3996. fi
  3997. DETECTED_PROFILE=''
  3998. SHELLTYPE="$(basename "/$SHELL")"
  3999. if [ "$SHELLTYPE" = "bash" ]; then
  4000. if [ -f "$HOME/.bashrc" ]; then
  4001. DETECTED_PROFILE="$HOME/.bashrc"
  4002. elif [ -f "$HOME/.bash_profile" ]; then
  4003. DETECTED_PROFILE="$HOME/.bash_profile"
  4004. fi
  4005. elif [ "$SHELLTYPE" = "zsh" ]; then
  4006. DETECTED_PROFILE="$HOME/.zshrc"
  4007. fi
  4008. if [ -z "$DETECTED_PROFILE" ]; then
  4009. if [ -f "$HOME/.profile" ]; then
  4010. DETECTED_PROFILE="$HOME/.profile"
  4011. elif [ -f "$HOME/.bashrc" ]; then
  4012. DETECTED_PROFILE="$HOME/.bashrc"
  4013. elif [ -f "$HOME/.bash_profile" ]; then
  4014. DETECTED_PROFILE="$HOME/.bash_profile"
  4015. elif [ -f "$HOME/.zshrc" ]; then
  4016. DETECTED_PROFILE="$HOME/.zshrc"
  4017. fi
  4018. fi
  4019. echo "$DETECTED_PROFILE"
  4020. }
  4021. _initconf() {
  4022. _initpath
  4023. if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
  4024. echo "
  4025. #LOG_FILE=\"$DEFAULT_LOG_FILE\"
  4026. #LOG_LEVEL=1
  4027. #AUTO_UPGRADE=\"1\"
  4028. #NO_TIMESTAMP=1
  4029. " >"$ACCOUNT_CONF_PATH"
  4030. fi
  4031. }
  4032. # nocron
  4033. _precheck() {
  4034. _nocron="$1"
  4035. if ! _exists "curl" && ! _exists "wget"; then
  4036. _err "Please install curl or wget first, we need to access http resources."
  4037. return 1
  4038. fi
  4039. if [ -z "$_nocron" ]; then
  4040. if ! _exists "crontab" && ! _exists "fcrontab"; then
  4041. _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'."
  4042. _err "We need to set cron job to renew the certs automatically."
  4043. _err "Otherwise, your certs will not be able to be renewed automatically."
  4044. if [ -z "$FORCE" ]; then
  4045. _err "Please add '--force' and try install again to go without crontab."
  4046. _err "./$PROJECT_ENTRY --install --force"
  4047. return 1
  4048. fi
  4049. fi
  4050. fi
  4051. if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then
  4052. _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN"
  4053. _err "We need openssl to generate keys."
  4054. return 1
  4055. fi
  4056. if ! _exists "socat"; then
  4057. _err "It is recommended to install socat first."
  4058. _err "We use socat for standalone server if you use standalone mode."
  4059. _err "If you don't use standalone mode, just ignore this warning."
  4060. fi
  4061. return 0
  4062. }
  4063. _setShebang() {
  4064. _file="$1"
  4065. _shebang="$2"
  4066. if [ -z "$_shebang" ]; then
  4067. _usage "Usage: file shebang"
  4068. return 1
  4069. fi
  4070. cp "$_file" "$_file.tmp"
  4071. echo "$_shebang" >"$_file"
  4072. sed -n 2,99999p "$_file.tmp" >>"$_file"
  4073. rm -f "$_file.tmp"
  4074. }
  4075. #confighome
  4076. _installalias() {
  4077. _c_home="$1"
  4078. _initpath
  4079. _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  4080. if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then
  4081. echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile"
  4082. echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile"
  4083. echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile"
  4084. fi
  4085. if [ "$_c_home" ]; then
  4086. _c_entry=" --config-home '$_c_home'"
  4087. fi
  4088. _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\""
  4089. if [ "$_c_home" ]; then
  4090. _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\""
  4091. else
  4092. _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile"
  4093. fi
  4094. _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
  4095. _profile="$(_detect_profile)"
  4096. if [ "$_profile" ]; then
  4097. _debug "Found profile: $_profile"
  4098. _info "Installing alias to '$_profile'"
  4099. _setopt "$_profile" ". \"$_envfile\""
  4100. _info "OK, Close and reopen your terminal to start using $PROJECT_NAME"
  4101. else
  4102. _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME"
  4103. fi
  4104. #for csh
  4105. _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh"
  4106. _csh_profile="$HOME/.cshrc"
  4107. if [ -f "$_csh_profile" ]; then
  4108. _info "Installing alias to '$_csh_profile'"
  4109. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  4110. if [ "$_c_home" ]; then
  4111. _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
  4112. else
  4113. _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile"
  4114. fi
  4115. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
  4116. _setopt "$_csh_profile" "source \"$_cshfile\""
  4117. fi
  4118. #for tcsh
  4119. _tcsh_profile="$HOME/.tcshrc"
  4120. if [ -f "$_tcsh_profile" ]; then
  4121. _info "Installing alias to '$_tcsh_profile'"
  4122. _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\""
  4123. if [ "$_c_home" ]; then
  4124. _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\""
  4125. fi
  4126. _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\""
  4127. _setopt "$_tcsh_profile" "source \"$_cshfile\""
  4128. fi
  4129. }
  4130. # nocron confighome
  4131. install() {
  4132. if [ -z "$LE_WORKING_DIR" ]; then
  4133. LE_WORKING_DIR="$DEFAULT_INSTALL_HOME"
  4134. fi
  4135. _nocron="$1"
  4136. _c_home="$2"
  4137. if ! _initpath; then
  4138. _err "Install failed."
  4139. return 1
  4140. fi
  4141. if [ "$_nocron" ]; then
  4142. _debug "Skip install cron job"
  4143. fi
  4144. if [ "$IN_CRON" != "1" ]; then
  4145. if ! _precheck "$_nocron"; then
  4146. _err "Pre-check failed, can not install."
  4147. return 1
  4148. fi
  4149. fi
  4150. if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then
  4151. _info "Using config home: $LE_CONFIG_HOME"
  4152. _c_home="$LE_CONFIG_HOME"
  4153. fi
  4154. #convert from le
  4155. if [ -d "$HOME/.le" ]; then
  4156. for envfile in "le.env" "le.sh.env"; do
  4157. if [ -f "$HOME/.le/$envfile" ]; then
  4158. if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then
  4159. _upgrading="1"
  4160. _info "You are upgrading from le.sh"
  4161. _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR"
  4162. mv "$HOME/.le" "$LE_WORKING_DIR"
  4163. mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env"
  4164. break
  4165. fi
  4166. fi
  4167. done
  4168. fi
  4169. _info "Installing to $LE_WORKING_DIR"
  4170. if [ ! -d "$LE_WORKING_DIR" ]; then
  4171. if ! mkdir -p "$LE_WORKING_DIR"; then
  4172. _err "Can not create working dir: $LE_WORKING_DIR"
  4173. return 1
  4174. fi
  4175. chmod 700 "$LE_WORKING_DIR"
  4176. fi
  4177. if [ ! -d "$LE_CONFIG_HOME" ]; then
  4178. if ! mkdir -p "$LE_CONFIG_HOME"; then
  4179. _err "Can not create config dir: $LE_CONFIG_HOME"
  4180. return 1
  4181. fi
  4182. chmod 700 "$LE_CONFIG_HOME"
  4183. fi
  4184. cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY"
  4185. if [ "$?" != "0" ]; then
  4186. _err "Install failed, can not copy $PROJECT_ENTRY"
  4187. return 1
  4188. fi
  4189. _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY"
  4190. if [ "$IN_CRON" != "1" ]; then
  4191. _installalias "$_c_home"
  4192. fi
  4193. for subf in $_SUB_FOLDERS; do
  4194. if [ -d "$subf" ]; then
  4195. mkdir -p "$LE_WORKING_DIR/$subf"
  4196. cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/
  4197. fi
  4198. done
  4199. if [ ! -f "$ACCOUNT_CONF_PATH" ]; then
  4200. _initconf
  4201. fi
  4202. if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then
  4203. _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\""
  4204. fi
  4205. if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then
  4206. _saveaccountconf "CERT_HOME" "$CERT_HOME"
  4207. fi
  4208. if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then
  4209. _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH"
  4210. fi
  4211. if [ -z "$_nocron" ]; then
  4212. installcronjob "$_c_home"
  4213. fi
  4214. if [ -z "$NO_DETECT_SH" ]; then
  4215. #Modify shebang
  4216. if _exists bash; then
  4217. _info "Good, bash is found, so change the shebang to use bash as preferred."
  4218. _shebang='#!'"$(env bash -c "command -v bash")"
  4219. _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang"
  4220. for subf in $_SUB_FOLDERS; do
  4221. if [ -d "$LE_WORKING_DIR/$subf" ]; then
  4222. for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do
  4223. _setShebang "$_apifile" "$_shebang"
  4224. done
  4225. fi
  4226. done
  4227. fi
  4228. fi
  4229. _info OK
  4230. }
  4231. # nocron
  4232. uninstall() {
  4233. _nocron="$1"
  4234. if [ -z "$_nocron" ]; then
  4235. uninstallcronjob
  4236. fi
  4237. _initpath
  4238. _uninstallalias
  4239. rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY"
  4240. _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself."
  4241. }
  4242. _uninstallalias() {
  4243. _initpath
  4244. _profile="$(_detect_profile)"
  4245. if [ "$_profile" ]; then
  4246. _info "Uninstalling alias from: '$_profile'"
  4247. text="$(cat "$_profile")"
  4248. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile"
  4249. fi
  4250. _csh_profile="$HOME/.cshrc"
  4251. if [ -f "$_csh_profile" ]; then
  4252. _info "Uninstalling alias from: '$_csh_profile'"
  4253. text="$(cat "$_csh_profile")"
  4254. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile"
  4255. fi
  4256. _tcsh_profile="$HOME/.tcshrc"
  4257. if [ -f "$_tcsh_profile" ]; then
  4258. _info "Uninstalling alias from: '$_csh_profile'"
  4259. text="$(cat "$_tcsh_profile")"
  4260. echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile"
  4261. fi
  4262. }
  4263. cron() {
  4264. export IN_CRON=1
  4265. _initpath
  4266. _info "$(__green "===Starting cron===")"
  4267. if [ "$AUTO_UPGRADE" = "1" ]; then
  4268. export LE_WORKING_DIR
  4269. (
  4270. if ! upgrade; then
  4271. _err "Cron:Upgrade failed!"
  4272. return 1
  4273. fi
  4274. )
  4275. . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null
  4276. if [ -t 1 ]; then
  4277. __INTERACTIVE="1"
  4278. fi
  4279. _info "Auto upgraded to: $VER"
  4280. fi
  4281. renewAll
  4282. _ret="$?"
  4283. IN_CRON=""
  4284. _info "$(__green "===End cron===")"
  4285. exit $_ret
  4286. }
  4287. version() {
  4288. echo "$PROJECT"
  4289. echo "v$VER"
  4290. }
  4291. showhelp() {
  4292. _initpath
  4293. version
  4294. echo "Usage: $PROJECT_ENTRY command ...[parameters]....
  4295. Commands:
  4296. --help, -h Show this help message.
  4297. --version, -v Show version info.
  4298. --install Install $PROJECT_NAME to your system.
  4299. --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job.
  4300. --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT.
  4301. --issue Issue a cert.
  4302. --signcsr Issue a cert from an existing csr.
  4303. --deploy Deploy the cert to your server.
  4304. --install-cert Install the issued cert to apache/nginx or any other server.
  4305. --renew, -r Renew a cert.
  4306. --renew-all Renew all the certs.
  4307. --revoke Revoke a cert.
  4308. --remove Remove the cert from $PROJECT
  4309. --list List all the certs.
  4310. --showcsr Show the content of a csr.
  4311. --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.
  4312. --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically.
  4313. --cron Run cron job to renew all the certs.
  4314. --toPkcs Export the certificate and key to a pfx file.
  4315. --toPkcs8 Convert to pkcs8 format.
  4316. --update-account Update account info.
  4317. --register-account Register account key.
  4318. --deactivate-account Deactivate the account.
  4319. --create-account-key Create an account private key, professional use.
  4320. --create-domain-key Create an domain private key, professional use.
  4321. --createCSR, -ccsr Create CSR , professional use.
  4322. --deactivate Deactivate the domain authz, professional use.
  4323. Parameters:
  4324. --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc.
  4325. --force, -f Used to force to install or force to renew a cert immediately.
  4326. --staging, --test Use staging server, just for test.
  4327. --debug Output debug info.
  4328. --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for secure.
  4329. --webroot, -w /path/to/webroot Specifies the web root folder for web root mode.
  4330. --standalone Use standalone mode.
  4331. --stateless Use stateless mode, see: $_STATELESS_WIKI
  4332. --tls Use standalone tls mode.
  4333. --apache Use apache mode.
  4334. --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api.
  4335. --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.
  4336. --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384.
  4337. --accountkeylength, -ak [2048] Specifies the account key length.
  4338. --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here.
  4339. --log-level 1|2 Specifies the log level, default is 1.
  4340. --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug.
  4341. These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert:
  4342. --cert-file After issue/renew, the cert will be copied to this path.
  4343. --key-file After issue/renew, the key will be copied to this path.
  4344. --ca-file After issue/renew, the intermediate cert will be copied to this path.
  4345. --fullchain-file After issue/renew, the fullchain cert will be copied to this path.
  4346. --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server.
  4347. --server SERVER ACME Directory Resource URI. (default: https://acme-v01.api.letsencrypt.org/directory)
  4348. --accountconf Specifies a customized account config file.
  4349. --home Specifies the home dir for $PROJECT_NAME .
  4350. --cert-home Specifies the home dir to save all the certs, only valid for '--install' command.
  4351. --config-home Specifies the home dir to save all the configurations.
  4352. --useragent Specifies the user agent string. it will be saved for future use too.
  4353. --accountemail Specifies the account email for registering, Only valid for the '--install' command.
  4354. --accountkey Specifies the account key path, Only valid for the '--install' command.
  4355. --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days.
  4356. --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer.
  4357. --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer.
  4358. --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses.
  4359. --listraw Only used for '--list' command, list the certs in raw format.
  4360. --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal.
  4361. --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted.
  4362. --ca-bundle Specifies the path to the CA certificate bundle to verify api server's certificate.
  4363. --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl.
  4364. --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.
  4365. --no-color Do not output color text.
  4366. --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR'
  4367. --csr Specifies the input csr.
  4368. --pre-hook Command to be run before obtaining any certificates.
  4369. --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed.
  4370. --renew-hook Command to be run once for each successfully renewed certificate.
  4371. --deploy-hook The hook file to deploy cert
  4372. --ocsp-must-staple, --ocsp Generate ocsp must Staple extension.
  4373. --always-force-new-domain-key Generate new domain key when renewal. Otherwise, the domain key is not changed by default.
  4374. --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future.
  4375. --listen-v4 Force standalone/tls server to listen at ipv4.
  4376. --listen-v6 Force standalone/tls server to listen at ipv6.
  4377. --openssl-bin Specifies a custom openssl bin location.
  4378. --use-wget Force to use wget, if you have both curl and wget installed.
  4379. "
  4380. }
  4381. # nocron
  4382. _installOnline() {
  4383. _info "Installing from online archive."
  4384. _nocron="$1"
  4385. if [ ! "$BRANCH" ]; then
  4386. BRANCH="master"
  4387. fi
  4388. target="$PROJECT/archive/$BRANCH.tar.gz"
  4389. _info "Downloading $target"
  4390. localname="$BRANCH.tar.gz"
  4391. if ! _get "$target" >$localname; then
  4392. _err "Download error."
  4393. return 1
  4394. fi
  4395. (
  4396. _info "Extracting $localname"
  4397. if ! (tar xzf $localname || gtar xzf $localname); then
  4398. _err "Extraction error."
  4399. exit 1
  4400. fi
  4401. cd "$PROJECT_NAME-$BRANCH"
  4402. chmod +x $PROJECT_ENTRY
  4403. if ./$PROJECT_ENTRY install "$_nocron"; then
  4404. _info "Install success!"
  4405. fi
  4406. cd ..
  4407. rm -rf "$PROJECT_NAME-$BRANCH"
  4408. rm -f "$localname"
  4409. )
  4410. }
  4411. upgrade() {
  4412. if (
  4413. _initpath
  4414. export LE_WORKING_DIR
  4415. cd "$LE_WORKING_DIR"
  4416. _installOnline "nocron"
  4417. ); then
  4418. _info "Upgrade success!"
  4419. exit 0
  4420. else
  4421. _err "Upgrade failed!"
  4422. exit 1
  4423. fi
  4424. }
  4425. _processAccountConf() {
  4426. if [ "$_useragent" ]; then
  4427. _saveaccountconf "USER_AGENT" "$_useragent"
  4428. elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then
  4429. _saveaccountconf "USER_AGENT" "$USER_AGENT"
  4430. fi
  4431. if [ "$_accountemail" ]; then
  4432. _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail"
  4433. elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then
  4434. _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL"
  4435. fi
  4436. if [ "$_openssl_bin" ]; then
  4437. _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin"
  4438. elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then
  4439. _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN"
  4440. fi
  4441. if [ "$_auto_upgrade" ]; then
  4442. _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade"
  4443. elif [ "$AUTO_UPGRADE" ]; then
  4444. _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE"
  4445. fi
  4446. if [ "$_use_wget" ]; then
  4447. _saveaccountconf "ACME_USE_WGET" "$_use_wget"
  4448. elif [ "$ACME_USE_WGET" ]; then
  4449. _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET"
  4450. fi
  4451. }
  4452. _process() {
  4453. _CMD=""
  4454. _domain=""
  4455. _altdomains="$NO_VALUE"
  4456. _webroot=""
  4457. _keylength=""
  4458. _accountkeylength=""
  4459. _cert_file=""
  4460. _key_file=""
  4461. _ca_file=""
  4462. _fullchain_file=""
  4463. _reloadcmd=""
  4464. _password=""
  4465. _accountconf=""
  4466. _useragent=""
  4467. _accountemail=""
  4468. _accountkey=""
  4469. _certhome=""
  4470. _confighome=""
  4471. _httpport=""
  4472. _tlsport=""
  4473. _dnssleep=""
  4474. _listraw=""
  4475. _stopRenewOnError=""
  4476. #_insecure=""
  4477. _ca_bundle=""
  4478. _ca_path=""
  4479. _nocron=""
  4480. _ecc=""
  4481. _csr=""
  4482. _pre_hook=""
  4483. _post_hook=""
  4484. _renew_hook=""
  4485. _deploy_hook=""
  4486. _logfile=""
  4487. _log=""
  4488. _local_address=""
  4489. _log_level=""
  4490. _auto_upgrade=""
  4491. _listen_v4=""
  4492. _listen_v6=""
  4493. _openssl_bin=""
  4494. _syslog=""
  4495. _use_wget=""
  4496. _server=""
  4497. while [ ${#} -gt 0 ]; do
  4498. case "${1}" in
  4499. --help | -h)
  4500. showhelp
  4501. return
  4502. ;;
  4503. --version | -v)
  4504. version
  4505. return
  4506. ;;
  4507. --install)
  4508. _CMD="install"
  4509. ;;
  4510. --uninstall)
  4511. _CMD="uninstall"
  4512. ;;
  4513. --upgrade)
  4514. _CMD="upgrade"
  4515. ;;
  4516. --issue)
  4517. _CMD="issue"
  4518. ;;
  4519. --deploy)
  4520. _CMD="deploy"
  4521. ;;
  4522. --signcsr)
  4523. _CMD="signcsr"
  4524. ;;
  4525. --showcsr)
  4526. _CMD="showcsr"
  4527. ;;
  4528. --installcert | -i | --install-cert)
  4529. _CMD="installcert"
  4530. ;;
  4531. --renew | -r)
  4532. _CMD="renew"
  4533. ;;
  4534. --renewAll | --renewall | --renew-all)
  4535. _CMD="renewAll"
  4536. ;;
  4537. --revoke)
  4538. _CMD="revoke"
  4539. ;;
  4540. --remove)
  4541. _CMD="remove"
  4542. ;;
  4543. --list)
  4544. _CMD="list"
  4545. ;;
  4546. --installcronjob | --install-cronjob)
  4547. _CMD="installcronjob"
  4548. ;;
  4549. --uninstallcronjob | --uninstall-cronjob)
  4550. _CMD="uninstallcronjob"
  4551. ;;
  4552. --cron)
  4553. _CMD="cron"
  4554. ;;
  4555. --toPkcs)
  4556. _CMD="toPkcs"
  4557. ;;
  4558. --toPkcs8)
  4559. _CMD="toPkcs8"
  4560. ;;
  4561. --createAccountKey | --createaccountkey | -cak | --create-account-key)
  4562. _CMD="createAccountKey"
  4563. ;;
  4564. --createDomainKey | --createdomainkey | -cdk | --create-domain-key)
  4565. _CMD="createDomainKey"
  4566. ;;
  4567. --createCSR | --createcsr | -ccr)
  4568. _CMD="createCSR"
  4569. ;;
  4570. --deactivate)
  4571. _CMD="deactivate"
  4572. ;;
  4573. --updateaccount | --update-account)
  4574. _CMD="updateaccount"
  4575. ;;
  4576. --registeraccount | --register-account)
  4577. _CMD="registeraccount"
  4578. ;;
  4579. --deactivate-account)
  4580. _CMD="deactivateaccount"
  4581. ;;
  4582. --domain | -d)
  4583. _dvalue="$2"
  4584. if [ "$_dvalue" ]; then
  4585. if _startswith "$_dvalue" "-"; then
  4586. _err "'$_dvalue' is not a valid domain for parameter '$1'"
  4587. return 1
  4588. fi
  4589. if _is_idn "$_dvalue" && ! _exists idn; then
  4590. _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first."
  4591. return 1
  4592. fi
  4593. if [ -z "$_domain" ]; then
  4594. _domain="$_dvalue"
  4595. else
  4596. if [ "$_altdomains" = "$NO_VALUE" ]; then
  4597. _altdomains="$_dvalue"
  4598. else
  4599. _altdomains="$_altdomains,$_dvalue"
  4600. fi
  4601. fi
  4602. fi
  4603. shift
  4604. ;;
  4605. --force | -f)
  4606. FORCE="1"
  4607. ;;
  4608. --staging | --test)
  4609. STAGE="1"
  4610. ;;
  4611. --server)
  4612. ACME_DIRECTORY="$2"
  4613. _server="$ACME_DIRECTORY"
  4614. export ACME_DIRECTORY
  4615. shift
  4616. ;;
  4617. --debug)
  4618. if [ -z "$2" ] || _startswith "$2" "-"; then
  4619. DEBUG="$DEBUG_LEVEL_DEFAULT"
  4620. else
  4621. DEBUG="$2"
  4622. shift
  4623. fi
  4624. ;;
  4625. --output-insecure)
  4626. export OUTPUT_INSECURE=1
  4627. ;;
  4628. --webroot | -w)
  4629. wvalue="$2"
  4630. if [ -z "$_webroot" ]; then
  4631. _webroot="$wvalue"
  4632. else
  4633. _webroot="$_webroot,$wvalue"
  4634. fi
  4635. shift
  4636. ;;
  4637. --standalone)
  4638. wvalue="$NO_VALUE"
  4639. if [ -z "$_webroot" ]; then
  4640. _webroot="$wvalue"
  4641. else
  4642. _webroot="$_webroot,$wvalue"
  4643. fi
  4644. ;;
  4645. --stateless)
  4646. wvalue="$MODE_STATELESS"
  4647. if [ -z "$_webroot" ]; then
  4648. _webroot="$wvalue"
  4649. else
  4650. _webroot="$_webroot,$wvalue"
  4651. fi
  4652. ;;
  4653. --local-address)
  4654. lvalue="$2"
  4655. _local_address="$_local_address$lvalue,"
  4656. shift
  4657. ;;
  4658. --apache)
  4659. wvalue="apache"
  4660. if [ -z "$_webroot" ]; then
  4661. _webroot="$wvalue"
  4662. else
  4663. _webroot="$_webroot,$wvalue"
  4664. fi
  4665. ;;
  4666. --nginx)
  4667. wvalue="$NGINX"
  4668. if [ -z "$_webroot" ]; then
  4669. _webroot="$wvalue"
  4670. else
  4671. _webroot="$_webroot,$wvalue"
  4672. fi
  4673. ;;
  4674. --tls)
  4675. wvalue="$W_TLS"
  4676. if [ -z "$_webroot" ]; then
  4677. _webroot="$wvalue"
  4678. else
  4679. _webroot="$_webroot,$wvalue"
  4680. fi
  4681. ;;
  4682. --dns)
  4683. wvalue="dns"
  4684. if [ "$2" ] && ! _startswith "$2" "-"; then
  4685. wvalue="$2"
  4686. shift
  4687. fi
  4688. if [ -z "$_webroot" ]; then
  4689. _webroot="$wvalue"
  4690. else
  4691. _webroot="$_webroot,$wvalue"
  4692. fi
  4693. ;;
  4694. --dnssleep)
  4695. _dnssleep="$2"
  4696. Le_DNSSleep="$_dnssleep"
  4697. shift
  4698. ;;
  4699. --keylength | -k)
  4700. _keylength="$2"
  4701. shift
  4702. ;;
  4703. --accountkeylength | -ak)
  4704. _accountkeylength="$2"
  4705. shift
  4706. ;;
  4707. --cert-file | --certpath)
  4708. _cert_file="$2"
  4709. shift
  4710. ;;
  4711. --key-file | --keypath)
  4712. _key_file="$2"
  4713. shift
  4714. ;;
  4715. --ca-file | --capath)
  4716. _ca_file="$2"
  4717. shift
  4718. ;;
  4719. --fullchain-file | --fullchainpath)
  4720. _fullchain_file="$2"
  4721. shift
  4722. ;;
  4723. --reloadcmd | --reloadCmd)
  4724. _reloadcmd="$2"
  4725. shift
  4726. ;;
  4727. --password)
  4728. _password="$2"
  4729. shift
  4730. ;;
  4731. --accountconf)
  4732. _accountconf="$2"
  4733. ACCOUNT_CONF_PATH="$_accountconf"
  4734. shift
  4735. ;;
  4736. --home)
  4737. LE_WORKING_DIR="$2"
  4738. shift
  4739. ;;
  4740. --certhome | --cert-home)
  4741. _certhome="$2"
  4742. CERT_HOME="$_certhome"
  4743. shift
  4744. ;;
  4745. --config-home)
  4746. _confighome="$2"
  4747. LE_CONFIG_HOME="$_confighome"
  4748. shift
  4749. ;;
  4750. --useragent)
  4751. _useragent="$2"
  4752. USER_AGENT="$_useragent"
  4753. shift
  4754. ;;
  4755. --accountemail)
  4756. _accountemail="$2"
  4757. ACCOUNT_EMAIL="$_accountemail"
  4758. shift
  4759. ;;
  4760. --accountkey)
  4761. _accountkey="$2"
  4762. ACCOUNT_KEY_PATH="$_accountkey"
  4763. shift
  4764. ;;
  4765. --days)
  4766. _days="$2"
  4767. Le_RenewalDays="$_days"
  4768. shift
  4769. ;;
  4770. --httpport)
  4771. _httpport="$2"
  4772. Le_HTTPPort="$_httpport"
  4773. shift
  4774. ;;
  4775. --tlsport)
  4776. _tlsport="$2"
  4777. Le_TLSPort="$_tlsport"
  4778. shift
  4779. ;;
  4780. --listraw)
  4781. _listraw="raw"
  4782. ;;
  4783. --stopRenewOnError | --stoprenewonerror | -se)
  4784. _stopRenewOnError="1"
  4785. ;;
  4786. --insecure)
  4787. #_insecure="1"
  4788. HTTPS_INSECURE="1"
  4789. ;;
  4790. --ca-bundle)
  4791. _ca_bundle="$(_readlink "$2")"
  4792. CA_BUNDLE="$_ca_bundle"
  4793. shift
  4794. ;;
  4795. --ca-path)
  4796. _ca_path="$2"
  4797. CA_PATH="$_ca_path"
  4798. shift
  4799. ;;
  4800. --nocron)
  4801. _nocron="1"
  4802. ;;
  4803. --no-color)
  4804. export ACME_NO_COLOR=1
  4805. ;;
  4806. --ecc)
  4807. _ecc="isEcc"
  4808. ;;
  4809. --csr)
  4810. _csr="$2"
  4811. shift
  4812. ;;
  4813. --pre-hook)
  4814. _pre_hook="$2"
  4815. shift
  4816. ;;
  4817. --post-hook)
  4818. _post_hook="$2"
  4819. shift
  4820. ;;
  4821. --renew-hook)
  4822. _renew_hook="$2"
  4823. shift
  4824. ;;
  4825. --deploy-hook)
  4826. if [ -z "$2" ] || _startswith "$2" "-"; then
  4827. _usage "Please specify a value for '--deploy-hook'"
  4828. return 1
  4829. fi
  4830. _deploy_hook="$_deploy_hook$2,"
  4831. shift
  4832. ;;
  4833. --ocsp-must-staple | --ocsp)
  4834. Le_OCSP_Staple="1"
  4835. ;;
  4836. --always-force-new-domain-key)
  4837. if [ -z "$2" ] || _startswith "$2" "-"; then
  4838. Le_ForceNewDomainKey=1
  4839. else
  4840. Le_ForceNewDomainKey="$2"
  4841. shift
  4842. fi
  4843. ;;
  4844. --log | --logfile)
  4845. _log="1"
  4846. _logfile="$2"
  4847. if _startswith "$_logfile" '-'; then
  4848. _logfile=""
  4849. else
  4850. shift
  4851. fi
  4852. LOG_FILE="$_logfile"
  4853. if [ -z "$LOG_LEVEL" ]; then
  4854. LOG_LEVEL="$DEFAULT_LOG_LEVEL"
  4855. fi
  4856. ;;
  4857. --log-level)
  4858. _log_level="$2"
  4859. LOG_LEVEL="$_log_level"
  4860. shift
  4861. ;;
  4862. --syslog)
  4863. if ! _startswith "$2" '-'; then
  4864. _syslog="$2"
  4865. shift
  4866. fi
  4867. if [ -z "$_syslog" ]; then
  4868. _syslog="$SYSLOG_LEVEL_DEFAULT"
  4869. fi
  4870. ;;
  4871. --auto-upgrade)
  4872. _auto_upgrade="$2"
  4873. if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then
  4874. _auto_upgrade="1"
  4875. else
  4876. shift
  4877. fi
  4878. AUTO_UPGRADE="$_auto_upgrade"
  4879. ;;
  4880. --listen-v4)
  4881. _listen_v4="1"
  4882. Le_Listen_V4="$_listen_v4"
  4883. ;;
  4884. --listen-v6)
  4885. _listen_v6="1"
  4886. Le_Listen_V6="$_listen_v6"
  4887. ;;
  4888. --openssl-bin)
  4889. _openssl_bin="$2"
  4890. ACME_OPENSSL_BIN="$_openssl_bin"
  4891. shift
  4892. ;;
  4893. --use-wget)
  4894. _use_wget="1"
  4895. ACME_USE_WGET="1"
  4896. ;;
  4897. *)
  4898. _err "Unknown parameter : $1"
  4899. return 1
  4900. ;;
  4901. esac
  4902. shift 1
  4903. done
  4904. if [ "${_CMD}" != "install" ]; then
  4905. __initHome
  4906. if [ "$_log" ]; then
  4907. if [ -z "$_logfile" ]; then
  4908. _logfile="$DEFAULT_LOG_FILE"
  4909. fi
  4910. fi
  4911. if [ "$_logfile" ]; then
  4912. _saveaccountconf "LOG_FILE" "$_logfile"
  4913. LOG_FILE="$_logfile"
  4914. fi
  4915. if [ "$_log_level" ]; then
  4916. _saveaccountconf "LOG_LEVEL" "$_log_level"
  4917. LOG_LEVEL="$_log_level"
  4918. fi
  4919. if [ "$_syslog" ]; then
  4920. if _exists logger; then
  4921. if [ "$_syslog" = "0" ]; then
  4922. _clearaccountconf "SYS_LOG"
  4923. else
  4924. _saveaccountconf "SYS_LOG" "$_syslog"
  4925. fi
  4926. SYS_LOG="$_syslog"
  4927. else
  4928. _err "The 'logger' command is not found, can not enable syslog."
  4929. _clearaccountconf "SYS_LOG"
  4930. SYS_LOG=""
  4931. fi
  4932. fi
  4933. _processAccountConf
  4934. fi
  4935. _debug2 LE_WORKING_DIR "$LE_WORKING_DIR"
  4936. if [ "$DEBUG" ]; then
  4937. version
  4938. if [ "$_server" ]; then
  4939. _debug "Using server: $_server"
  4940. fi
  4941. fi
  4942. case "${_CMD}" in
  4943. install) install "$_nocron" "$_confighome" ;;
  4944. uninstall) uninstall "$_nocron" ;;
  4945. upgrade) upgrade ;;
  4946. issue)
  4947. issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address"
  4948. ;;
  4949. deploy)
  4950. deploy "$_domain" "$_deploy_hook" "$_ecc"
  4951. ;;
  4952. signcsr)
  4953. signcsr "$_csr" "$_webroot"
  4954. ;;
  4955. showcsr)
  4956. showcsr "$_csr" "$_domain"
  4957. ;;
  4958. installcert)
  4959. installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc"
  4960. ;;
  4961. renew)
  4962. renew "$_domain" "$_ecc"
  4963. ;;
  4964. renewAll)
  4965. renewAll "$_stopRenewOnError"
  4966. ;;
  4967. revoke)
  4968. revoke "$_domain" "$_ecc"
  4969. ;;
  4970. remove)
  4971. remove "$_domain" "$_ecc"
  4972. ;;
  4973. deactivate)
  4974. deactivate "$_domain,$_altdomains"
  4975. ;;
  4976. registeraccount)
  4977. registeraccount "$_accountkeylength"
  4978. ;;
  4979. updateaccount)
  4980. updateaccount
  4981. ;;
  4982. deactivateaccount)
  4983. deactivateaccount
  4984. ;;
  4985. list)
  4986. list "$_listraw"
  4987. ;;
  4988. installcronjob) installcronjob "$_confighome" ;;
  4989. uninstallcronjob) uninstallcronjob ;;
  4990. cron) cron ;;
  4991. toPkcs)
  4992. toPkcs "$_domain" "$_password" "$_ecc"
  4993. ;;
  4994. toPkcs8)
  4995. toPkcs8 "$_domain" "$_ecc"
  4996. ;;
  4997. createAccountKey)
  4998. createAccountKey "$_accountkeylength"
  4999. ;;
  5000. createDomainKey)
  5001. createDomainKey "$_domain" "$_keylength"
  5002. ;;
  5003. createCSR)
  5004. createCSR "$_domain" "$_altdomains" "$_ecc"
  5005. ;;
  5006. *)
  5007. if [ "$_CMD" ]; then
  5008. _err "Invalid command: $_CMD"
  5009. fi
  5010. showhelp
  5011. return 1
  5012. ;;
  5013. esac
  5014. _ret="$?"
  5015. if [ "$_ret" != "0" ]; then
  5016. return $_ret
  5017. fi
  5018. if [ "${_CMD}" = "install" ]; then
  5019. if [ "$_log" ]; then
  5020. if [ -z "$LOG_FILE" ]; then
  5021. LOG_FILE="$DEFAULT_LOG_FILE"
  5022. fi
  5023. _saveaccountconf "LOG_FILE" "$LOG_FILE"
  5024. fi
  5025. if [ "$_log_level" ]; then
  5026. _saveaccountconf "LOG_LEVEL" "$_log_level"
  5027. fi
  5028. if [ "$_syslog" ]; then
  5029. if _exists logger; then
  5030. if [ "$_syslog" = "0" ]; then
  5031. _clearaccountconf "SYS_LOG"
  5032. else
  5033. _saveaccountconf "SYS_LOG" "$_syslog"
  5034. fi
  5035. else
  5036. _err "The 'logger' command is not found, can not enable syslog."
  5037. _clearaccountconf "SYS_LOG"
  5038. SYS_LOG=""
  5039. fi
  5040. fi
  5041. _processAccountConf
  5042. fi
  5043. }
  5044. if [ "$INSTALLONLINE" ]; then
  5045. INSTALLONLINE=""
  5046. _installOnline
  5047. exit
  5048. fi
  5049. main() {
  5050. [ -z "$1" ] && showhelp && return
  5051. if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
  5052. }
  5053. main "$@"