Commits

Oben Sonne  committed bdfa378

Tune integration of encfs config file option

- shorter naming
- version independent naming (if there are different config file
versions to consider - other than 6 - we can detect them
automatically)
- config file set in interactive mode, similar to other options
- adjust tests (though a test case for teh new feature is still missing)

  • Participants
  • Parent commits d262007

Comments (0)

Files changed (3)

     proceed = None
     password = None
     epath = None
+    econfig = None
     mpoint = None
     amount = None
 
                 "Use option --list to show available items or "
                 "option --add to add items.")
 
+MSG_NO_ENCFS_PATH = ("no EncFS at given path (or the EncFS config file "
+                     "location is invalid)")
+
 USAGE = """Usage: %prog --list
        %prog --mount [ENCFS-PATH-or-MOUNT-POINT]
        %prog --add ENCFS-PATH MOUNT-POINT
                   help="Input for mount point edit")
     og.add_option("", "--amount", default=None,
                   help="Input for auto mount question")
-    og.add_option("", "--encfs6config", default=None,
-                  help="Set an encfs6 config file to use (instead of using the one in the encfs path)")
+    og.add_option("", "--econfig", default=None,
+                  help="Input for encfs config file question")
+
     op.add_option_group(og)
 
     opts, args = op.parse_args()
     preset.epath = opts.epath
     preset.mpoint = opts.mpoint
     preset.amount = opts.amount
-    preset.encfs6config = opts.encfs6config
+    preset.econfig = opts.econfig
 
     return opts
 
 def _proceed(msg):
     print("Warning: %s" % msg)
     proceed = preset.proceed or raw_input("Proceed [y/N]: ")
-    if proceed.strip()[0].lower() != "y":
+    if not proceed or proceed.strip()[0].lower() != "y":
         _exit(2)
 
 def _pathify(path):
     path = os.path.realpath(path)
     return path
 
-def _env_with_encfs6config(encfs6config):
+def _encfs_env(econfig):
+    """Get environment for encfs commands."""
     env = os.environ.copy()
-    if encfs6config != "":
-        env["ENCFS6_CONFIG"] = encfs6config
+    if econfig != "-":
+        env["ENCFS6_CONFIG"] = econfig
     return env
 
 def _is_mounted(mpoint):
     points = [os.path.abspath(p) for p in points]
     return os.path.abspath(mpoint) in points
 
-def _is_encfs(epath, encfs6config):
+def _is_encfs(epath, econfig):
     """Check if 'epath' points to an EncFS directory."""
 
     p = subprocess.Popen(["encfsctl", "info", epath], stdout=subprocess.PIPE,
-                         stderr=subprocess.PIPE, env = _env_with_encfs6config(encfs6config))
+        stderr=subprocess.PIPE, env=_encfs_env(econfig))
     p.communicate()
     return p.returncode == 0
 
         epath = item.attributes["encfs-path"]
         mpoint = item.attributes["mount-point"]
         amount = item.attributes["auto-mount"]
-        encfs6config = item.attributes.get("encfs6config", "[in encfs path]")
+        econfig = item.attributes.get("encfs-config", "-")
         print("* encfs path     : %s" % epath)
         print("  mount point    : %s" % mpoint)
         print("  mount at login : %s" % (amount == "y" and "yes" or "no"))
-        print("  encfs6config   : %s" % encfs6config)
+        print("  encfs config   : %s" % econfig)
 
     return RC_OK
 
 def add_item(epath, mpoint):
     """Add new EncFS item to keyring."""
 
-    encfs6config = preset.encfs6config or ""
-
-    if not _is_encfs(epath, encfs6config):
-        _proceed("no EncFS at given path (maybe you want to specify an external encfs configfile using --encfsconfig)")
     if not os.path.isdir(mpoint):
         _proceed("mount point is not a directory")
     if _get_items(mpoint=mpoint):
         _proceed("mount point already in keyring")
 
+    econfig = preset.econfig or raw_input("EncFS config file [-]: ") or "-"
+
+    if not _is_encfs(epath, econfig):
+        _proceed(MSG_NO_ENCFS_PATH)
+
     secret = preset.password or getpass.getpass("EncFS password: ")
     amount = preset.amount or raw_input("Mount at login [Y/n]: ") or "y"
     amount = amount.strip()[0].lower() == "y" and "y" or "n"
-    attr = {"encfs-path": epath, "mount-point": mpoint, "auto-mount": amount, "encfs6config": encfs6config}
+    attr = {"encfs-path": epath, "mount-point": mpoint, "auto-mount": amount,
+        "encfs-config": econfig}
     attr.update(GENCFS_ATTR)
     name = "EncFS mount at %s" % mpoint
     gk.item_create_sync(KEYRING, ITYPE, name, attr, secret, False)
         epath = item.attributes["encfs-path"]
         mpoint = item.attributes["mount-point"]
         amount = item.attributes["auto-mount"]
-        encfs6config = item.attributes.get("encfs6config","")
+        econfig = item.attributes.get("encfs-config", "-")
         epath = preset.epath or raw_input("EncFS path [%s]: " % epath) or epath
+        econfig = preset.econfig or raw_input("EncFS config file [%s] (`-`: default): " % econfig) or econfig
         mpoint = preset.mpoint or raw_input("Mount point [%s]: " % mpoint) or mpoint
         secret = preset.password or getpass.getpass("Password [**current**]: ") or item.secret
         hint = amount == "y" and "Y/n" or "y/N"
         amount = preset.amount or raw_input("Mount at login [%s]: " % hint) or amount
-        encfs6config = preset.encfs6config or encfs6config
         amount = amount.strip()[0].lower() == "y" and "y" or "n"
         mpoint = _pathify(mpoint)
         epath = _pathify(epath)
         for other in [i for i in items if i.item_id != item.item_id]:
             if other.attributes["mount-point"] == mpoint:
                 _proceed("mount point already in use")
-        if not _is_encfs(epath, encfs6config):
-            _proceed("no EncFS at given path")
+        if not _is_encfs(epath, econfig):
+            _proceed(MSG_NO_ENCFS_PATH)
         if not os.path.isdir(mpoint):
             _proceed("mount point is not a directory")
 
         attributes["encfs-path"] = epath
         attributes["mount-point"] = mpoint
         attributes["auto-mount"] = amount
-        attributes["encfs6config"] = encfs6config
+        attributes["encfs-config"] = econfig
         gk.item_set_attributes_sync(KEYRING, item.item_id, attributes)
         info = gk.item_get_info_sync(KEYRING, item.item_id)
         info.set_secret(secret)
     for item in items:
         epath = item.attributes["encfs-path"]
         mpoint = item.attributes["mount-point"]
-        encfs6config = item.attributes["encfs6config"]
+        econfig = item.attributes["encfs-config"]
         msg = "Mounting %s at %s: " % (epath, mpoint)
         if _is_mounted(mpoint):
             msg += "mount point already in use"
             rc |= RC_INVALID_PATH
         else:
             cmd = ["encfs", "-o", "nonempty", "-S", epath, mpoint]
-            p = subprocess.Popen(cmd, stdin=subprocess.PIPE, env = _env_with_encfs6config(encfs6config))
+            p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
+                env=_encfs_env(econfig))
             p.communicate(input="%s\n" % item.secret)
             msg += p.returncode and "FAILED" or "OK"
             rc |= 0 if p.returncode == os.EX_OK else RC_MOUNT_FAILED

File tests/test.exp

 * encfs path     : ./tenv/e1
   mount point    : ./tenv/m1
   mount at login : yes
+  encfs config   : -
 # EXPECT: failing add - mount point in use
 Warning: mount point already in keyring
 # EXPECT: 1 listed item (1)
 * encfs path     : ./tenv/e1
   mount point    : ./tenv/m1
   mount at login : yes
+  encfs config   : -
 # EXPECT: succeeding add (2)
 # EXPECT: 2 listed items (1,2)
 * encfs path     : ./tenv/e1
   mount point    : ./tenv/m1
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e2
   mount point    : ./tenv/m2
   mount at login : yes
+  encfs config   : -
 # EXPECT: succeeding add (3a)
 # EXPECT: 3 listed items (1,2,3a)
 * encfs path     : ./tenv/e1
   mount point    : ./tenv/m1
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e2
   mount point    : ./tenv/m2
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e3
   mount point    : ./tenv/m3a
   mount at login : yes
+  encfs config   : -
 # EXPECT: succeeding add (3b)
 # EXPECT: 4 listed items (1,2,3a,3b)
 * encfs path     : ./tenv/e1
   mount point    : ./tenv/m1
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e2
   mount point    : ./tenv/m2
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e3
   mount point    : ./tenv/m3a
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e3
   mount point    : ./tenv/m3b
   mount at login : yes
+  encfs config   : -
 # EXPECT: 2 succeeding mounts (3a,3b)
 Mounting ./tenv/m3a: OK
 Mounting ./tenv/m3b: OK
 # EXPECT: 2 mounted paths (3a,3b)
-encfs on ./tenv/m3a type fuse.encfs (rw,nosuid,nodev,default_permissions,)
-encfs on ./tenv/m3b type fuse.encfs (rw,nosuid,nodev,default_permissions,)
+encfs on ./tenv/m3a type fuse.encfs
+encfs on ./tenv/m3b type fuse.encfs
 # EXPECT: no mounted paths - all unmounted
 # EXPECT: 4 succeeding mounts (1,2,3a,3b)
 Mounting ./tenv/m1: OK
 Mounting ./tenv/m3a: OK
 Mounting ./tenv/m3b: OK
 # EXPECT: 4 mounted paths (1,2,3a,3b)
-encfs on ./tenv/m1 type fuse.encfs (rw,nosuid,nodev,default_permissions,)
-encfs on ./tenv/m2 type fuse.encfs (rw,nosuid,nodev,default_permissions,)
-encfs on ./tenv/m3a type fuse.encfs (rw,nosuid,nodev,default_permissions,)
-encfs on ./tenv/m3b type fuse.encfs (rw,nosuid,nodev,default_permissions,)
+encfs on ./tenv/m1 type fuse.encfs
+encfs on ./tenv/m2 type fuse.encfs
+encfs on ./tenv/m3a type fuse.encfs
+encfs on ./tenv/m3b type fuse.encfs
 # EXPECT: 4 failing mounts - already mounted
 Mounting ./tenv/m1: mount point already in use
 Mounting ./tenv/m2: mount point already in use
 * encfs path     : ./tenv/e1
   mount point    : ./tenv/m1
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e2
   mount point    : ./tenv/m2
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e3
   mount point    : ./tenv/m3b
   mount at login : yes
+  encfs config   : -
 # EXPECT: 3 items (1,2,3a)
 * encfs path     : ./tenv/e1
   mount point    : ./tenv/m1
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e2
   mount point    : ./tenv/m2
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e3
   mount point    : ./tenv/m3a
   mount at login : yes
+  encfs config   : -
 # EXPECT: 3 items (1,2,3a)
 * encfs path     : ./tenv/e1
   mount point    : ./tenv/m1
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e2
   mount point    : ./tenv/m2
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e3
   mount point    : ./tenv/m3a
   mount at login : yes
+  encfs config   : -
 # EXPECT: 1 failing mount (3a) - wrong password
 Error decoding volume key, password incorrect
 Mounting ./tenv/m3a: FAILED
 * encfs path     : ./tenv/e1
   mount point    : ./tenv/m1
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e2
   mount point    : ./tenv/m2
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e3
   mount point    : ./tenv/m3b
   mount at login : yes
+  encfs config   : -
 # EXPECT: 1 succeeding mount (3b)
 Mounting ./tenv/m3b: OK
 # EXPECT: 1 mounted path (3b)
-encfs on ./tenv/m3b type fuse.encfs (rw,nosuid,nodev,default_permissions,)
+encfs on ./tenv/m3b type fuse.encfs
 # EXPECT: no mounted paths - all unmounted
 # EXPECT: failing edit (3b->2) - mount point in use
 Warning: mount point already in use
 * encfs path     : ./tenv/e1
   mount point    : ./tenv/m1
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e2
   mount point    : ./tenv/m2
   mount at login : yes
+  encfs config   : -
 * encfs path     : ./tenv/e3
   mount point    : ./tenv/m3b
   mount at login : yes
+  encfs config   : -
 # EXPECT: autostart on
 autostart on
 # EXPECT: 2 succeeding edits

File tests/test.sh

 	echo "# EXPECT: $1"
 }
 
+mounts() {
+    mount | grep "/tenv/m[0-9]" | sed -r -e "s/ \([^ ]+\)//"
+}
+
 if [ ! -e ./tenv.tar ] ; then
     echo "Abort. Not in testing environment."
     exit 1
 $GENCFS -l
 
 expect "succeeding add (1)"
-$GENCFS -a $TENV/e1 $TENV/m1 --password p1 --proceed n --amount y
+$GENCFS -a $TENV/e1 $TENV/m1 --econfig "-" --password p1 --proceed n --amount y
 expect "1 listed item (1)"
 $GENCFS -l
 
 expect "failing add - mount point in use"
-$GENCFS -a ./tenv/e1 ./tenv/m1 --password p1 --proceed n --amount y
+$GENCFS -a ./tenv/e1 ./tenv/m1 --econfig "-" --password p1 --proceed n --amount y
 expect "1 listed item (1)"
 $GENCFS -l
 
 expect "succeeding add (2)"
-$GENCFS -a $TENV/e2 $TENV/m2 --password p2 --proceed n --amount y
+$GENCFS -a $TENV/e2 $TENV/m2 --econfig "-" --password p2 --proceed n --amount y
 expect "2 listed items (1,2)"
 $GENCFS -l
 
 expect "succeeding add (3a)"
-$GENCFS -a $TENV/e3 $TENV/m3a --password p3 --proceed n --amount y
+$GENCFS -a $TENV/e3 $TENV/m3a --econfig "-" --password p3 --proceed n --amount y
 expect "3 listed items (1,2,3a)"
 $GENCFS -l
 
 expect "succeeding add (3b)"
-$GENCFS -a $TENV/e3 $TENV/m3b --password p3 --proceed n --amount y
+$GENCFS -a $TENV/e3 $TENV/m3b --econfig "-" --password p3 --proceed n --amount y
 expect "4 listed items (1,2,3a,3b)"
 $GENCFS -l
 
 expect "2 succeeding mounts (3a,3b)"
 $GENCFS -m ./tenv/e3
 expect "2 mounted paths (3a,3b)"
-mount | grep "/tenv/m[0-9]" | sed -e "s/user=\w\+//"
+mounts
 
 for MPOINT in $TENV/m3* ; do
     fusermount -u $MPOINT 2>&1
 done
 expect "no mounted paths - all unmounted"
-mount | grep "/tenv/m[0-9]"
+mounts
 
 expect "4 succeeding mounts (1,2,3a,3b)"
 $GENCFS -m
 expect "4 mounted paths (1,2,3a,3b)"
-mount | grep "/tenv/m[0-9]" | sed -e "s/user=\w\+//"
+mounts
 
 expect "4 failing mounts - already mounted"
 $GENCFS -m
 	fusermount -u $MPOINT 2>&1
 done
 expect "no mounted paths - all unmounted"
-mount | grep "/tenv/m[0-9]" | sed -e "s/user=\w\+//"
+mounts
 
 $GENCFS -r $TENV/m3a
 expect "3 items (1,2,3b)"
 $GENCFS -l
 
-$GENCFS -e $TENV/m3b --password p3 --epath $TENV/e3 --mpoint $TENV/m3a --amount y
+$GENCFS -e $TENV/m3b --econfig "-" --password p3 --epath $TENV/e3 --mpoint $TENV/m3a --amount y
 expect "3 items (1,2,3a)"
 $GENCFS -l
 
-$GENCFS -e $TENV/m3a --password px --epath $TENV/e3 --mpoint $TENV/m3a --amount y
+$GENCFS -e $TENV/m3a --econfig "-" --password px --epath $TENV/e3 --mpoint $TENV/m3a --amount y
 expect "3 items (1,2,3a)"
 $GENCFS -l
 
 expect "1 failing mount (3a) - wrong password"
 $GENCFS -m $TENV/m3a
 expect "no mounted paths"
-mount | grep "/tenv/m[0-9]" | sed -e "s/user=\w\+//"
+mounts
 
-$GENCFS -e $TENV/m3a --password p3 --epath $TENV/e3 --mpoint $TENV/m3b --amount y
+$GENCFS -e $TENV/m3a --econfig "-" --password p3 --epath $TENV/e3 --mpoint $TENV/m3b --amount y
 expect "3 items (1,2,3b)"
 $GENCFS -l
 
 expect "1 succeeding mount (3b)"
 $GENCFS -m $TENV/m3b
 expect "1 mounted path (3b)"
-mount | grep "/tenv/m[0-9]" | sed -e "s/user=\w\+//"
+mounts
 
 fusermount -u $TENV/m3b 2>&1
 
 expect "no mounted paths - all unmounted"
-mount | grep "/tenv/m[0-9]" | sed -e "s/user=\w\+//"
+mounts
 
 expect "failing edit (3b->2) - mount point in use"
-$GENCFS -e $TENV/m3b --password p3 --epath $TENV/e3 --mpoint $TENV/m2 --proceed n --amount y
+$GENCFS -e $TENV/m3b --econfig "-" --password p3 --epath $TENV/e3 --mpoint $TENV/m2 --proceed n --amount y
 expect "3 items (1,2,3b)"
 $GENCFS -l
 expect "autostart on"
 test -e autostart.desktop && echo "autostart on" ||  echo "autostart off"
 expect "2 succeeding edits"
-$GENCFS -e $TENV/m1 --password p1 --epath $TENV/e1 --mpoint $TENV/m1 --proceed n --amount n
-$GENCFS -e $TENV/m2 --password p2 --epath $TENV/e2 --mpoint $TENV/m2 --proceed n --amount n
+$GENCFS -e $TENV/m1 --econfig "-" --password p1 --epath $TENV/e1 --mpoint $TENV/m1 --proceed n --amount n
+$GENCFS -e $TENV/m2 --econfig "-" --password p2 --epath $TENV/e2 --mpoint $TENV/m2 --proceed n --amount n
 expect "autostart on"
 test -e autostart.desktop && echo "autostart on" ||  echo "autostart off"
 expect "autostart content"
 cat autostart.desktop
 expect "1 succeeding edits"
-$GENCFS -e $TENV/m3b --password p3 --epath $TENV/e3 --mpoint $TENV/m3b --proceed n --amount n
+$GENCFS -e $TENV/m3b --econfig "-" --password p3 --epath $TENV/e3 --mpoint $TENV/m3b --proceed n --amount n
 expect "autostart off"
 test -e autostart.desktop && echo "autostart on" ||  echo "autostart off"