acme.sh 133 KB

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