acme.sh 138 KB

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