~ecryptfs/ecryptfs/trunk

400.1.1 by Michael Rooney
added python api
1
#!/usr/bin/env python
2
#
408 by Dustin Kirkland
* src/python/ecryptfsapi.py: added python api
3
#    ecryptfsapi.py, Copyright 2008, 2009 Michael Rooney <mrooney@ubuntu.com>
400.1.4 by Michael Rooney
minor typo/cleanup
4
#    Date: 2009-05-28
400.1.3 by Michael Rooney
stop the camel-casing
5
#    Version: 0.4
400.1.1 by Michael Rooney
added python api
6
#
400.1.3 by Michael Rooney
stop the camel-casing
7
#    This is a python API for interacting with ecryptfs-utils and its
8
#    encrypted directories.
400.1.1 by Michael Rooney
added python api
9
#
10
#    This program is free software: you can redistribute it and/or modify
11
#    it under the terms of the GNU General Public License as published by
12
#    the Free Software Foundation, either version 3 of the License, or
13
#    (at your option) any later version.
14
#
15
#    This program is distributed in the hope that it will be useful,
16
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
#    GNU General Public License for more details.
19
#
20
#    You should have received a copy of the GNU General Public License
21
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23
import commands, os
24
25
AUTOMOUNT_FILE = os.path.expanduser("~/.ecryptfs/auto-mount")
26
AUTOUMOUNT_FILE = os.path.expanduser("~/.ecryptfs/auto-umount")
27
PRIVATE_LOCATION_FILE = os.path.expanduser("~/.ecryptfs/Private.mnt")
28
PRIVATE_LOCATION = os.path.exists(PRIVATE_LOCATION_FILE) and open(PRIVATE_LOCATION_FILE).read().strip()
29
400.1.3 by Michael Rooney
stop the camel-casing
30
def set_automount(doAuto):
400.1.1 by Michael Rooney
added python api
31
    """Enable or disable automounting for this user."""
32
    if doAuto:
33
        command = "touch %s" % AUTOMOUNT_FILE
34
        #open(AUTOMOUNT_FILE, "w")
35
    else:
36
        command = "rm %s" % AUTOMOUNT_FILE
37
        #os.remove(AUTOMOUNT_FILE)
38
39
    return commands.getstatusoutput(command)
40
400.1.3 by Michael Rooney
stop the camel-casing
41
def get_automount():
400.1.1 by Michael Rooney
added python api
42
    """Return whether or not automounting is enabled for this user."""
43
    return os.path.exists(AUTOMOUNT_FILE)
44
400.1.3 by Michael Rooney
stop the camel-casing
45
def set_autounmount(doAuto):
400.1.1 by Michael Rooney
added python api
46
    """Enable or disable automounting for this user."""
47
    if doAuto:
48
        command = "touch %s" % AUTOUMOUNT_FILE
49
    else:
50
        command = "rm %s" % AUTOUMOUNT_FILE
51
52
    return commands.getstatusoutput(command)
53
400.1.3 by Michael Rooney
stop the camel-casing
54
def get_autounmount():
400.1.4 by Michael Rooney
minor typo/cleanup
55
    """Return whether or not autounmounting is enabled for this user."""
400.1.1 by Michael Rooney
added python api
56
    return os.path.exists(AUTOUMOUNT_FILE)
57
400.1.3 by Michael Rooney
stop the camel-casing
58
def set_mounted(doMount):
400.1.1 by Michael Rooney
added python api
59
    """Set the mounted (unencrypted) state of ~/Private."""
60
    if doMount:
408 by Dustin Kirkland
* src/python/ecryptfsapi.py: added python api
61
        command = "/sbin/mount.ecryptfs_private"
400.1.1 by Michael Rooney
added python api
62
    else:
408 by Dustin Kirkland
* src/python/ecryptfsapi.py: added python api
63
        command = "/sbin/umount.ecryptfs_private"
400.1.1 by Michael Rooney
added python api
64
65
    return commands.getstatusoutput(command)
66
400.1.3 by Michael Rooney
stop the camel-casing
67
def get_mounted():
400.1.1 by Michael Rooney
added python api
68
    """Return whether or not ~/Private is mounted (unencrypted)."""
69
    if PRIVATE_LOCATION:
70
        mounts = open("/proc/mounts").read()
71
        return PRIVATE_LOCATION in mounts
72
    else:
73
        return False
74
400.1.3 by Michael Rooney
stop the camel-casing
75
def needs_setup():
76
    """
400.1.4 by Michael Rooney
minor typo/cleanup
77
    Return whether or not an encrypted directory has been set up by ecryptfs
400.1.3 by Michael Rooney
stop the camel-casing
78
    for this user, either Home or Private.
79
    """
400.1.1 by Michael Rooney
added python api
80
    encryptedHome = False #TODO: implement
81
    encryptedPrivate = PRIVATE_LOCATION
82
    return not (encryptedHome or encryptedPrivate)