acme.sh 184 KB

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