acme.sh 149 KB

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