summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/pwpolicy.py
blob: e7078f5e9bbb4a67c0e30006dd372d177bee0de8 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Authors:
#   Rob Crittenden <rcritten@redhat.com>
#   Pavel Zuna <pzuna@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

"""
Password policy
"""

from ipalib import api, errors
from ipalib import Command
from ipalib import Int

_fields = {
    'krbminpwdlife': 'Minimum lifetime (in hours)',
    'krbmaxpwdlife': 'Maximum lifetime (in days)',
    'krbpwdmindiffchars': 'Minimum number of characters classes',
    'krbpwdminlength': 'Minimum length',
    'krbpwdhistorylength': 'History size',
}

def _convert_time_for_output(entry_attrs):
    if 'krbmaxpwdlife' in entry_attrs:
        entry_attrs['krbmaxpwdlife'][0] = str(
            int(entry_attrs['krbmaxpwdlife'][0]) / 86400
        )
    if 'krbminpwdlife' in entry_attrs:
        entry_attrs['krbminpwdlife'][0] = str(
            int(entry_attrs['krbminpwdlife'][0]) / 3600
        )


class pwpolicy_mod(Command):
    """
    Modify password policy.
    """
    takes_options = (
        Int('krbmaxpwdlife?',
            cli_name='maxlife',
            doc='Max. Password Lifetime (days)',
            minvalue=0,
            attribute=True,
        ),
        Int('krbminpwdlife?',
            cli_name='minlife',
            doc='Min. Password Lifetime (hours)',
            minvalue=0,
            attribute=True,
        ),
        Int('krbpwdhistorylength?',
            cli_name='history',
            doc='Password History Size',
            minvalue=0,
            attribute=True,
        ),
        Int('krbpwdmindiffchars?',
            cli_name='minclasses',
            doc='Min. Number of Character Classes',
            minvalue=0,
            attribute=True,
        ),
        Int('krbpwdminlength?',
            cli_name='minlength',
            doc='Min. Length of Password',
            minvalue=0,
            attribute=True,
        ),
    )

    def execute(self, *args, **options):
        assert 'dn' not in options
        ldap = self.api.Backend.ldap2

        entry_attrs = self.args_options_2_entry(*args, **options)
        dn = self.api.env.container_accounts

        # Convert hours and days to seconds
        if 'krbmaxpwdlife' in entry_attrs:
            entry_attrs['krbmaxpwdlife'] = entry_attrs['krbmaxpwdlife'] * 86400
            del entry_attrs['krbmaxpwdlife']
        if 'krbminpwdlife' in entry_attrs:
            entry_attrs['krbminpwdlife'] = entry_attrs['krbminpwdlife'] * 3600
            del entry_attrs['krbminpwdlife']

        try:
            ldap.update_entry(dn, entry_attrs)
        except errors.EmptyModlist:
            pass

        (dn, entry_attrs) = ldap.get_entry(dn, entry_attrs.keys())

        _convert_time_for_output(entry_attrs)

        return (dn, entry_attrs)

    def output_for_cli(self, textui, result, *args, **options):
        (dn, entry_attrs) = result

        textui.print_name(self.name)
        textui.print_plain('Password policy:')
        for (k, v) in _fields.iteritems():
            if k in entry_attrs:
                textui.print_attribute(v, entry_attrs[k])
        textui.print_dashed('Modified password policy.')

api.register(pwpolicy_mod)


class pwpolicy_show(Command):
    """
    Display password policy.
    """
    def execute(self, *args, **options):
        ldap = self.api.Backend.ldap2

        dn = self.api.env.container_accounts
        (dn, entry_attrs) = ldap.get_entry(dn)

        _convert_time_for_output(entry_attrs)

        return (dn, entry_attrs)

    def output_for_cli(self, textui, result, *args, **options):
        (dn, entry_attrs) = result

        textui.print_name(self.name)
        textui.print_plain('Password policy:')
        for (k, v) in _fields.iteritems():
            if k in entry_attrs:
                textui.print_attribute(v, entry_attrs[k])

api.register(pwpolicy_show)