From 9bf1b5399b29520d343804aba50b3304d868160c Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Sun, 25 Feb 2018 23:10:44 -0500 Subject: [PATCH] config: ensure all paths are cleaned and expanded after parsing options In this commit, we ensure all paths to directories and files related to LND are cleand and expanded before attempting to use them. For example, in POSIX systems: - ~/lnd.conf would be expanded to /home/user/lnd.conf - $HOME/lnd.conf would be expanded to /home/user/lnd.conf --- config.go | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/config.go b/config.go index 5030b552..594f14fc 100644 --- a/config.go +++ b/config.go @@ -151,8 +151,8 @@ type config struct { LndDir string `long:"lnddir" description:"The base directory that contains lnd's data, logs, configuration file, etc."` ConfigFile string `long:"C" long:"configfile" description:"Path to configuration file"` DataDir string `short:"b" long:"datadir" description:"The directory to store lnd's data within"` - TLSCertPath string `long:"tlscertpath" description:"Path to TLS certificate for lnd's RPC and REST services"` - TLSKeyPath string `long:"tlskeypath" description:"Path to TLS private key for lnd's RPC and REST services"` + TLSCertPath string `long:"tlscertpath" description:"Path to write the TLS certificate for lnd's RPC and REST services"` + TLSKeyPath string `long:"tlskeypath" description:"Path to write the TLS private key for lnd's RPC and REST services"` TLSExtraIP string `long:"tlsextraip" description:"Adds an extra ip to the generated certificate"` NoMacaroons bool `long:"no-macaroons" description:"Disable macaroon authentication"` AdminMacPath string `long:"adminmacaroonpath" description:"Path to write the admin macaroon for lnd's RPC and REST services if it doesn't exist"` @@ -321,6 +321,19 @@ func loadConfig() (*config, error) { return nil, err } + // As soon as we're done parsing configuration options, ensure all paths + // to directories and files are cleaned and expanded before attempting + // to use them later on. + cfg.DataDir = cleanAndExpandPath(cfg.DataDir) + cfg.TLSCertPath = cleanAndExpandPath(cfg.TLSCertPath) + cfg.TLSKeyPath = cleanAndExpandPath(cfg.TLSKeyPath) + cfg.AdminMacPath = cleanAndExpandPath(cfg.AdminMacPath) + cfg.ReadMacPath = cleanAndExpandPath(cfg.ReadMacPath) + cfg.LogDir = cleanAndExpandPath(cfg.LogDir) + cfg.BtcdMode.Dir = cleanAndExpandPath(cfg.BtcdMode.Dir) + cfg.LtcdMode.Dir = cleanAndExpandPath(cfg.LtcdMode.Dir) + cfg.BitcoindMode.Dir = cleanAndExpandPath(cfg.BitcoindMode.Dir) + // Setup dial and DNS resolution functions depending on the specified // options. The default is to use the standard golang "net" package // functions. When Tor's proxy is specified, the dial function is set to @@ -510,28 +523,12 @@ func loadConfig() (*config, error) { cfg.ReadMacPath = filepath.Join(cfg.DataDir, defaultReadMacFilename) } - // Append the network type to the data directory so it is "namespaced" - // per network. In addition to the block database, there are other - // pieces of data that are saved to disk such as address manager state. - // All data is specific to a network, so namespacing the data directory - // means each individual piece of serialized data does not have to - // worry about changing names per network and such. - // TODO(roasbeef): when we go full multi-chain remove the additional - // namespacing on the target chain. - cfg.DataDir = cleanAndExpandPath(cfg.DataDir) - // Append the network type to the log directory so it is "namespaced" // per network in the same fashion as the data directory. - cfg.LogDir = cleanAndExpandPath(cfg.LogDir) cfg.LogDir = filepath.Join(cfg.LogDir, registeredChains.PrimaryChain().String(), normalizeNetwork(activeNetParams.Name)) - // Ensure that the paths to the TLS key and certificate files are - // expanded and cleaned. - cfg.TLSCertPath = cleanAndExpandPath(cfg.TLSCertPath) - cfg.TLSKeyPath = cleanAndExpandPath(cfg.TLSKeyPath) - // Initialize logging at the default logging level. initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename))