acme.sh 122 KB

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