net: Move setexpiry to 'net setexpiry'
[metze/samba/wip.git] / source4 / scripting / python / samba / netcmd / setexpiry.py
1 #!/usr/bin/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
32     synopsis = "setexpiry [username] [options]"
33
34     takes_optiongroups = {
35         "sambaopts": options.SambaOptions,
36         "versionopts": options.VersionOptions,
37         "credopts": options.CredentialsOptions,
38     }
39
40     takes_options = [
41         Option("-H", help="LDB URL for database or target server", type=str),
42         Option("--filter", help="LDAP Filter to set password on", type=str),
43         Option("--days", help="Days to expiry", type=int),
44         Option("--noexpiry", help="Password does never expire", action="store_true"),
45     ]
46
47     takes_args = ["username?"]
48
49     def run(self, username=None, sambaopts=None, credopts=None,
50             versionopts=None, H=None, filter=None, days=None, noexpiry=None):
51         if username is None and filter is None:
52             raise CommandError("Either the username or '--filter' must be specified!")
53
54         if filter is None:
55             filter = "(&(objectClass=user)(sAMAccountName=%s))" % (username)
56
57         lp = sambaopts.get_loadparm()
58         creds = credopts.get_credentials(lp)
59
60         if days is None:
61             days = 0
62
63         if H is not None:
64             url = H
65         else:
66             url = lp.get("sam database")
67
68         samdb = SamDB(url=url, session_info=system_session(),
69             credentials=creds, lp=lp)
70
71         samdb.setexpiry(filter, days*24*3600, no_expiry_req=noexpiry)