Release alpha15.
[ira/wip.git] / source4 / scripting / python / samba / netcmd / setexpiry.py
1 #!/usr/bin/env python
2 #
3 # Sets the user password expiry on a Samba4 server
4 # Copyright Jelmer Vernooij 2008
5 #
6 # Based on the original in EJS:
7 # Copyright Andrew Tridgell 2005
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23 from samba.netcmd import Command, CommandError, Option
24
25 import samba.getopt as options
26
27 from samba.auth import system_session
28 from samba.samdb import SamDB
29
30 class cmd_setexpiry(Command):
31     """Sets the expiration of a user account"""
32
33     synopsis = "setexpiry [username] [options]"
34
35     takes_optiongroups = {
36         "sambaopts": options.SambaOptions,
37         "versionopts": options.VersionOptions,
38         "credopts": options.CredentialsOptions,
39     }
40
41     takes_options = [
42         Option("-H", help="LDB URL for database or target server", type=str),
43         Option("--filter", help="LDAP Filter to set password on", type=str),
44         Option("--days", help="Days to expiry", type=int),
45         Option("--noexpiry", help="Password does never expire", action="store_true"),
46     ]
47
48     takes_args = ["username?"]
49
50     def run(self, username=None, sambaopts=None, credopts=None,
51             versionopts=None, H=None, filter=None, days=None, noexpiry=None):
52         if username is None and filter is None:
53             raise CommandError("Either the username or '--filter' must be specified!")
54
55         if filter is None:
56             filter = "(&(objectClass=user)(sAMAccountName=%s))" % (username)
57
58         lp = sambaopts.get_loadparm()
59         creds = credopts.get_credentials(lp)
60
61         if days is None:
62             days = 0
63
64         samdb = SamDB(url=H, session_info=system_session(),
65             credentials=creds, lp=lp)
66
67         samdb.setexpiry(filter, days*24*3600, no_expiry_req=noexpiry)