2016-09-20 19:45:28 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2016-10-09 20:55:33 +02:00
|
|
|
"fmt"
|
2016-12-10 14:54:06 +01:00
|
|
|
"net"
|
2016-09-20 19:45:28 +02:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2016-10-09 19:38:49 +02:00
|
|
|
"strings"
|
2016-09-20 19:45:28 +02:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/configfile"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/prefer_openssl"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
|
|
|
)
|
|
|
|
|
2016-09-20 19:59:08 +02:00
|
|
|
// argContainer stores the parsed CLI options and arguments
|
2016-09-20 19:45:28 +02:00
|
|
|
type argContainer struct {
|
2016-11-01 18:59:34 +01:00
|
|
|
debug, init, zerokey, fusedebug, openssl, passwd, fg, version,
|
2016-09-20 19:45:28 +02:00
|
|
|
plaintextnames, quiet, nosyslog, wpanic,
|
2016-11-24 22:36:04 +01:00
|
|
|
longnames, allow_other, ro, reverse, aessiv, nonempty, raw64,
|
|
|
|
noprealloc bool
|
2016-09-20 19:59:08 +02:00
|
|
|
masterkey, mountpoint, cipherdir, cpuprofile, extpass,
|
2017-01-26 22:13:57 +01:00
|
|
|
memprofile, ko, passfile, ctlsock, fsname string
|
2016-09-20 19:59:08 +02:00
|
|
|
// Configuration file name override
|
|
|
|
config string
|
2016-09-20 19:45:28 +02:00
|
|
|
notifypid, scryptn int
|
2016-12-10 14:54:06 +01:00
|
|
|
// Helper variables that are NOT cli options all start with an underscore
|
2016-10-08 20:57:38 +02:00
|
|
|
// _configCustom is true when the user sets a custom config file name.
|
|
|
|
_configCustom bool
|
2016-12-10 14:54:06 +01:00
|
|
|
// _ctlsockFd stores the control socket file descriptor (ctlsock stores the path)
|
|
|
|
_ctlsockFd net.Listener
|
2016-09-20 19:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var flagSet *flag.FlagSet
|
|
|
|
|
2016-10-09 19:38:49 +02:00
|
|
|
// prefixOArgs transform options passed via "-o foo,bar" into regular options
|
|
|
|
// like "-foo -bar" and prefixes them to the command line.
|
|
|
|
func prefixOArgs(osArgs []string) []string {
|
|
|
|
// Need at least 3, example: gocryptfs -o foo,bar
|
2016-10-10 19:44:34 +02:00
|
|
|
if len(osArgs) < 3 {
|
2016-10-09 19:38:49 +02:00
|
|
|
return osArgs
|
|
|
|
}
|
2016-10-10 19:44:34 +02:00
|
|
|
// Find and extract "-o foo,bar"
|
|
|
|
var otherArgs, oOpts []string
|
|
|
|
for i := 1; i < len(osArgs); i++ {
|
|
|
|
if osArgs[i] == "-o" {
|
|
|
|
// Last argument?
|
|
|
|
if i+1 >= len(osArgs) {
|
|
|
|
tlog.Fatal.Printf("The \"-o\" option requires an argument")
|
|
|
|
os.Exit(ErrExitUsage)
|
|
|
|
}
|
|
|
|
oOpts = strings.Split(osArgs[i+1], ",")
|
|
|
|
// Skip over the arguments to "-o"
|
|
|
|
i++
|
|
|
|
} else if strings.HasPrefix(osArgs[i], "-o=") {
|
|
|
|
oOpts = strings.Split(osArgs[i][3:], ",")
|
|
|
|
} else {
|
|
|
|
otherArgs = append(otherArgs, osArgs[i])
|
|
|
|
}
|
2016-10-09 19:38:49 +02:00
|
|
|
}
|
2016-10-10 19:44:34 +02:00
|
|
|
// Start with program name
|
2016-10-09 19:38:49 +02:00
|
|
|
newArgs := []string{osArgs[0]}
|
|
|
|
// Add options from "-o"
|
2016-10-10 19:44:34 +02:00
|
|
|
for _, o := range oOpts {
|
|
|
|
if o == "" {
|
|
|
|
continue
|
|
|
|
}
|
2016-10-10 20:21:52 +02:00
|
|
|
if o == "o" || o == "-o" {
|
|
|
|
tlog.Fatal.Printf("You can't pass \"-o\" to \"-o\"")
|
|
|
|
os.Exit(ErrExitUsage)
|
|
|
|
}
|
2016-10-10 19:44:34 +02:00
|
|
|
newArgs = append(newArgs, "-"+o)
|
2016-10-09 19:38:49 +02:00
|
|
|
}
|
2016-10-10 19:44:34 +02:00
|
|
|
// Add other arguments
|
|
|
|
newArgs = append(newArgs, otherArgs...)
|
2016-10-09 19:38:49 +02:00
|
|
|
return newArgs
|
|
|
|
}
|
|
|
|
|
2016-09-20 19:59:08 +02:00
|
|
|
// parseCliOpts - parse command line options (i.e. arguments that start with "-")
|
|
|
|
func parseCliOpts() (args argContainer) {
|
2016-10-09 19:38:49 +02:00
|
|
|
os.Args = prefixOArgs(os.Args)
|
|
|
|
|
2016-09-20 19:45:28 +02:00
|
|
|
var err error
|
|
|
|
var opensslAuto string
|
|
|
|
|
2016-10-09 20:55:33 +02:00
|
|
|
flagSet = flag.NewFlagSet(tlog.ProgramName, flag.ContinueOnError)
|
2016-09-20 19:45:28 +02:00
|
|
|
flagSet.Usage = usageText
|
|
|
|
flagSet.BoolVar(&args.debug, "d", false, "")
|
|
|
|
flagSet.BoolVar(&args.debug, "debug", false, "Enable debug output")
|
|
|
|
flagSet.BoolVar(&args.fusedebug, "fusedebug", false, "Enable fuse library debug output")
|
|
|
|
flagSet.BoolVar(&args.init, "init", false, "Initialize encrypted directory")
|
|
|
|
flagSet.BoolVar(&args.zerokey, "zerokey", false, "Use all-zero dummy master key")
|
|
|
|
// Tri-state true/false/auto
|
|
|
|
flagSet.StringVar(&opensslAuto, "openssl", "auto", "Use OpenSSL instead of built-in Go crypto")
|
|
|
|
flagSet.BoolVar(&args.passwd, "passwd", false, "Change password")
|
2016-11-01 18:59:34 +01:00
|
|
|
flagSet.BoolVar(&args.fg, "f", false, "")
|
|
|
|
flagSet.BoolVar(&args.fg, "fg", false, "Stay in the foreground")
|
2016-09-20 19:45:28 +02:00
|
|
|
flagSet.BoolVar(&args.version, "version", false, "Print version and exit")
|
|
|
|
flagSet.BoolVar(&args.plaintextnames, "plaintextnames", false, "Do not encrypt file names")
|
|
|
|
flagSet.BoolVar(&args.quiet, "q", false, "")
|
|
|
|
flagSet.BoolVar(&args.quiet, "quiet", false, "Quiet - silence informational messages")
|
|
|
|
flagSet.BoolVar(&args.nosyslog, "nosyslog", false, "Do not redirect output to syslog when running in the background")
|
|
|
|
flagSet.BoolVar(&args.wpanic, "wpanic", false, "When encountering a warning, panic and exit immediately")
|
|
|
|
flagSet.BoolVar(&args.longnames, "longnames", true, "Store names longer than 176 bytes in extra files")
|
|
|
|
flagSet.BoolVar(&args.allow_other, "allow_other", false, "Allow other users to access the filesystem. "+
|
|
|
|
"Only works if user_allow_other is set in /etc/fuse.conf.")
|
|
|
|
flagSet.BoolVar(&args.ro, "ro", false, "Mount the filesystem read-only")
|
|
|
|
flagSet.BoolVar(&args.reverse, "reverse", false, "Reverse mode")
|
2016-09-26 23:25:13 +02:00
|
|
|
flagSet.BoolVar(&args.aessiv, "aessiv", false, "AES-SIV encryption")
|
2016-10-06 22:41:13 +02:00
|
|
|
flagSet.BoolVar(&args.nonempty, "nonempty", false, "Allow mounting over non-empty directories")
|
2016-11-01 18:43:22 +01:00
|
|
|
flagSet.BoolVar(&args.raw64, "raw64", false, "Use unpadded base64 for file names")
|
2016-11-24 22:36:04 +01:00
|
|
|
flagSet.BoolVar(&args.noprealloc, "noprealloc", false, "Disable preallocation before writing")
|
2016-09-20 19:45:28 +02:00
|
|
|
flagSet.StringVar(&args.masterkey, "masterkey", "", "Mount with explicit master key")
|
|
|
|
flagSet.StringVar(&args.cpuprofile, "cpuprofile", "", "Write cpu profile to specified file")
|
|
|
|
flagSet.StringVar(&args.memprofile, "memprofile", "", "Write memory profile to specified file")
|
|
|
|
flagSet.StringVar(&args.config, "config", "", "Use specified config file instead of CIPHERDIR/gocryptfs.conf")
|
|
|
|
flagSet.StringVar(&args.extpass, "extpass", "", "Use external program for the password prompt")
|
2016-10-09 20:08:10 +02:00
|
|
|
flagSet.StringVar(&args.passfile, "passfile", "", "Read password from file")
|
2016-10-09 19:32:55 +02:00
|
|
|
flagSet.StringVar(&args.ko, "ko", "", "Pass additional options directly to the kernel, comma-separated list")
|
2016-11-10 00:27:08 +01:00
|
|
|
flagSet.StringVar(&args.ctlsock, "ctlsock", "", "Create control socket at specified path")
|
2017-01-26 22:13:57 +01:00
|
|
|
flagSet.StringVar(&args.fsname, "fsname", "", "Override the filesystem name")
|
2016-09-20 19:45:28 +02:00
|
|
|
flagSet.IntVar(&args.notifypid, "notifypid", 0, "Send USR1 to the specified process after "+
|
|
|
|
"successful mount - used internally for daemonization")
|
|
|
|
flagSet.IntVar(&args.scryptn, "scryptn", configfile.ScryptDefaultLogN, "scrypt cost parameter logN. "+
|
2016-11-24 22:36:04 +01:00
|
|
|
"A lower value speeds up mounting but makes the password susceptible to brute-force attacks")
|
2016-10-09 20:06:23 +02:00
|
|
|
// Ignored otions
|
2016-10-10 20:21:52 +02:00
|
|
|
var dummyBool bool
|
2016-10-24 19:18:13 +02:00
|
|
|
ignoreText := "(ignored for compatibility)"
|
2016-10-10 20:21:52 +02:00
|
|
|
flagSet.BoolVar(&dummyBool, "rw", false, ignoreText)
|
|
|
|
flagSet.BoolVar(&dummyBool, "nosuid", false, ignoreText)
|
|
|
|
flagSet.BoolVar(&dummyBool, "nodev", false, ignoreText)
|
|
|
|
var dummyString string
|
2016-10-24 19:18:13 +02:00
|
|
|
flagSet.StringVar(&dummyString, "o", "", "For compatibility, all options can be also passed as a comma-separated list to -o.")
|
2016-10-09 20:06:23 +02:00
|
|
|
// Actual parsing
|
2016-10-09 20:55:33 +02:00
|
|
|
err = flagSet.Parse(os.Args[1:])
|
2016-10-10 20:21:52 +02:00
|
|
|
if err == flag.ErrHelp {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2016-10-09 20:55:33 +02:00
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("You passed: %s", prettyArgs())
|
|
|
|
tlog.Fatal.Printf("%v", err)
|
2016-10-10 20:21:52 +02:00
|
|
|
os.Exit(ErrExitUsage)
|
2016-10-09 20:55:33 +02:00
|
|
|
}
|
2016-09-20 19:45:28 +02:00
|
|
|
// "-openssl" needs some post-processing
|
|
|
|
if opensslAuto == "auto" {
|
|
|
|
args.openssl = prefer_openssl.PreferOpenSSL()
|
|
|
|
} else {
|
|
|
|
args.openssl, err = strconv.ParseBool(opensslAuto)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Printf("Invalid \"-openssl\" setting: %v", err)
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitUsage)
|
2016-09-20 19:45:28 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-29 00:34:12 +01:00
|
|
|
// '-passfile FILE' is a shortcut for -extpass='/bin/cat -- FILE'
|
2016-10-09 20:08:10 +02:00
|
|
|
if args.passfile != "" {
|
2017-01-29 00:34:12 +01:00
|
|
|
args.extpass = "/bin/cat -- " + args.passfile
|
2016-10-09 20:08:10 +02:00
|
|
|
}
|
2016-10-16 16:50:23 +02:00
|
|
|
if args.extpass != "" && args.masterkey != "" {
|
|
|
|
tlog.Fatal.Printf("The options -extpass and -masterkey cannot be used at the same time")
|
|
|
|
os.Exit(ErrExitUsage)
|
|
|
|
}
|
2016-09-20 19:45:28 +02:00
|
|
|
return args
|
|
|
|
}
|
2016-10-09 20:55:33 +02:00
|
|
|
|
|
|
|
// prettyArgs pretty-prints the command-line arguments.
|
|
|
|
func prettyArgs() string {
|
|
|
|
pa := fmt.Sprintf("%q", os.Args[1:])
|
|
|
|
// Get rid of "[" and "]"
|
|
|
|
pa = pa[1 : len(pa)-1]
|
|
|
|
return pa
|
|
|
|
}
|