summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/pwpolicy.py
blob: f18160b6562483a45fa7297e7aee339f2e3c9b90 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Authors:
#   Rob Crittenden <rcritten@redhat.com>
#
# Copyright (C) 2008  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
Frontend plugins for password policy.
"""

from ipalib import api
from ipalib import Command  # Plugin base classes
from ipalib import Int  # Parameter types


class pwpolicy_mod(Command):
    'Edit existing password policy.'
    takes_options = (
        Int('krbmaxpwdlife?',
            cli_name='maxlife',
            doc='Max. Password Lifetime (days)',
            minvalue=0,
        ),
        Int('krbminpwdlife?',
            cli_name='minlife',
            doc='Min. Password Lifetime (hours)',
            minvalue=0,
        ),
        Int('krbpwdhistorylength?',
            cli_name='history',
            doc='Password History Size',
            minvalue=0,
        ),
        Int('krbpwdmindiffchars?',
            cli_name='minclasses',
            doc='Min. Number of Character Classes',
            minvalue=0,
        ),
        Int('krbpwdminlength?',
            cli_name='minlength',
            doc='Min. Length of Password',
            minvalue=0,
        ),
    )
    def execute(self, *args, **kw):
        """
        Execute the pwpolicy-mod operation.

        The dn should not be passed as a keyword argument as it is constructed
        by this method.

        Returns the entry

        :param args: This function takes no positional arguments
        :param kw: Keyword arguments for the other LDAP attributes.
        """
        assert 'dn' not in kw
        ldap = self.api.Backend.ldap
        dn = ldap.find_entry_dn("cn", "accounts", "krbPwdPolicy")

        # The LDAP routines want strings, not ints, so convert a few
        # things. Otherwise it sees a string -> int conversion as a change.
        for k in kw.iterkeys():
            if k.startswith("krb", 0, 3) and type(kw[k]) is int:
                kw[k] = str(kw[k])

        # Convert hours and days to seconds
        if kw.get('krbmaxpwdlife'):
            kw['krbmaxpwdlife'] = str(int(kw.get('krbmaxpwdlife')) * 86400)
        if kw.get('krbminpwdlife'):
            kw['krbminpwdlife'] = str(int(kw.get('krbminpwdlife')) * 3600)

        return ldap.update(dn, **kw)

    def output_for_cli(self, textui, result, *args, **options):
        textui.print_plain("Policy modified")

api.register(pwpolicy_mod)


class pwpolicy_show(Command):
    'Retrieve current password policy'
    def execute(self, *args, **kw):
        """
        Execute the pwpolicy-show operation.

        The dn should not be passed as a keyword argument as it is constructed
        by this method.

        Returns the entry

        :param args: Not used.
        :param kw: Not used.
        """
        ldap = self.api.Backend.ldap
        dn = ldap.find_entry_dn("cn", "accounts", "krbPwdPolicy")

        policy = ldap.retrieve(dn)

        # convert some values for display purposes
        policy['krbmaxpwdlife'] = str(int(policy.get('krbmaxpwdlife')) / 86400)
        policy['krbminpwdlife'] = str(int(policy.get('krbminpwdlife')) / 3600)

        return policy

    def output_for_cli(self, textui, result, *args, **options):
        textui.print_plain("Password Policy")
        textui.print_plain("Min. Password Lifetime (hours): %s" % result.get('krbminpwdlife'))
        textui.print_plain("Max. Password Lifetime (days): %s" % result.get('krbmaxpwdlife'))
        textui.print_plain("Min. Number of Character Classes: %s" % result.get('krbpwdmindiffchars'))
        textui.print_plain("Min. Length of Password: %s" % result.get('krbpwdminlength'))
        textui.print_plain("Password History Size: %s" % result.get('krbpwdhistorylength'))

api.register(pwpolicy_show)