acme.sh 136 KB

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