acme.sh 171 KB

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