summaryrefslogtreecommitdiff
path: root/src/tools/sss_obfuscate
blob: faa2d981a367b390e90a9a6d9e00e77f7e1aaf6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python

import sys
from optparse import OptionParser

import pysss
import SSSDConfig
import getpass

def parse_options():
    parser = OptionParser()
    parser.set_description("sss_obfuscate converts a given password into \
                            human-unreadable format and places it into \
                            appropriate domain section of the SSSD config \
                            file. The password can be passed in by stdin, \
                            specified on the command-line or entered \
                            interactively")
    parser.add_option("-s", "--stdin", action="store_true",
                      dest="stdin", default=False,
                      help="Read the password from stdin.")
    parser.add_option("-d", "--domain",
                      dest="domain", default=None,
                      help="The domain to use the password in (mandatory)",
                      metavar="DOMNAME")
    parser.add_option("-f", "--file",
                      dest="filename", default=None,
                      help="Set input file to FILE (default: Use system default, usually /etc/sssd/sssd.conf)",
                      metavar="FILE")
    parser.add_option("-p", "--password",
                      dest="password", default=None,
                      help="Password to obfuscate.",
                      metavar="PASSWORD")
    (options, args) = parser.parse_args()

    return options, args

def main():
    options, args = parse_options()
    if not options:
        print >> sys.stderr, "Cannot parse options"
        return 1

    if not options.domain:
        print >> sys.stderr, "No domain specified"
        return 1

    if not options.stdin and not options.password:
        pprompt = lambda: (getpass.getpass("Enter password: "), getpass.getpass("Re-enter password: "))
        p1, p2 = pprompt()
        while p1 != p2:
            print('Passwords do not match. Try again')
            p1, p2 = pprompt()
        password = p1

    else:
        try:
            password = sys.stdin.read()
        except KeyboardInterrupt:
            return 1

    # Obfuscate the password
    obfobj = pysss.password()
    obfpwd = obfobj.encrypt(password, obfobj.AES_256)

    # Save the obfuscated password into the domain
    sssdconfig = SSSDConfig.SSSDConfig()
    try:
        sssdconfig.import_config(options.filename)
    except IOError:
        print "Cannot open config file %s" % options.filename
        return 1

    try:
        domain = sssdconfig.get_domain(options.domain)
    except SSSDConfig.NoDomainError:
        print "No such domain %s" % options.domain
        return 1

    try:
        domain.set_option('ldap_default_authtok_type', 'obfuscated_password')
        domain.set_option('ldap_default_authtok', obfpwd)
    except SSSDConfig.NoOptionError:
        print "The domain %s does not seem to support the required options" % \
              options.domain
        return 1


    sssdconfig.save_domain(domain)
    sssdconfig.write()
    return 0

if __name__ == "__main__":
    ret = main()
    sys.exit(ret)