acme.sh 165 KB

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