samba-tools: more reasonable defaults for samba-tool commands
[samba.git] / source4 / scripting / python / samba / netcmd / group.py
1 #!/usr/bin/env python
2 #
3 # Adds a new user to 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 import samba.getopt as options
23 from samba.netcmd import Command, SuperCommand, CommandError, Option
24 import ldb
25
26 from getpass import getpass
27 from samba.auth import system_session
28 from samba.samdb import SamDB
29 from samba.dsdb import (
30     GTYPE_SECURITY_DOMAIN_LOCAL_GROUP,
31     GTYPE_SECURITY_GLOBAL_GROUP,
32     GTYPE_SECURITY_UNIVERSAL_GROUP,
33     GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP,
34     GTYPE_DISTRIBUTION_GLOBAL_GROUP,
35     GTYPE_DISTRIBUTION_UNIVERSAL_GROUP,
36 )
37
38 security_group = dict({"Domain": GTYPE_SECURITY_DOMAIN_LOCAL_GROUP, "Global": GTYPE_SECURITY_GLOBAL_GROUP, "Universal": GTYPE_SECURITY_UNIVERSAL_GROUP})
39 distribution_group = dict({"Domain": GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP, "Global": GTYPE_DISTRIBUTION_GLOBAL_GROUP, "Universal": GTYPE_DISTRIBUTION_UNIVERSAL_GROUP})
40
41
42 class cmd_group_add(Command):
43     """Creates a new group"""
44
45     synopsis = "%prog group add [options] <groupname>"
46
47     takes_optiongroups = {
48         "sambaopts": options.SambaOptions,
49         "versionopts": options.VersionOptions,
50         "credopts": options.CredentialsOptions,
51     }
52
53     takes_options = [
54         Option("-H", help="LDB URL for database or target server", type=str),
55         Option("--groupou",
56            help="Alternative location (without domainDN counterpart) to default CN=Users in which new user object will be created",
57            type=str),
58         Option("--group-scope", type="choice", choices=["Domain", "Global", "Universal"],
59             help="Group scope (Domain | Global | Universal)"),
60         Option("--group-type", type="choice", choices=["Security", "Distribution"],
61             help="Group type (Security | Distribution)"),
62         Option("--description", help="Group's description", type=str),
63         Option("--mail-address", help="Group's email address", type=str),
64         Option("--notes", help="Groups's notes", type=str),
65     ]
66
67     takes_args = ["groupname"]
68
69     def run(self, groupname, credopts=None, sambaopts=None,
70             versionopts=None, H=None, groupou=None, group_scope=None,
71             group_type=None, description=None, mail_address=None, notes=None):
72
73         if (group_type or "Security") == "Security":
74               gtype = security_group.get(group_scope, GTYPE_SECURITY_GLOBAL_GROUP)
75         else:
76               gtype = distribution_group.get(group_scope, GTYPE_DISTRIBUTION_GLOBAL_GROUP)
77
78         lp = sambaopts.get_loadparm()
79         creds = credopts.get_credentials(lp, fallback_machine=True)
80
81         try:
82             samdb = SamDB(url=H, session_info=system_session(),
83                           credentials=creds, lp=lp)
84             samdb.newgroup(groupname, groupou=groupou, grouptype = gtype,
85                           description=description, mailaddress=mail_address, notes=notes)
86         except Exception, e:
87             raise CommandError('Failed to create group "%s"' % groupname, e)
88
89
90 class cmd_group_delete(Command):
91     """Delete a group"""
92
93     synopsis = "%prog group delete <groupname>"
94
95     takes_optiongroups = {
96         "sambaopts": options.SambaOptions,
97         "versionopts": options.VersionOptions,
98         "credopts": options.CredentialsOptions,
99     }
100
101     takes_options = [
102         Option("-H", help="LDB URL for database or target server", type=str),
103     ]
104
105     takes_args = ["groupname"]
106
107     def run(self, groupname, credopts=None, sambaopts=None, versionopts=None, H=None):
108
109         lp = sambaopts.get_loadparm()
110         creds = credopts.get_credentials(lp, fallback_machine=True)
111
112         try:
113             samdb = SamDB(url=H, session_info=system_session(),
114                           credentials=creds, lp=lp)
115             samdb.deletegroup(groupname)
116         except Exception, e:
117             raise CommandError('Failed to remove group "%s"' % groupname, e)
118
119
120 class cmd_group_add_members(Command):
121     """Add (comma-separated list of) group members"""
122
123     synopsis = "%prog group addmembers <groupname> <listofmembers>"
124
125     takes_optiongroups = {
126         "sambaopts": options.SambaOptions,
127         "versionopts": options.VersionOptions,
128         "credopts": options.CredentialsOptions,
129     }
130
131     takes_options = [
132         Option("-H", help="LDB URL for database or target server", type=str),
133     ]
134
135     takes_args = ["groupname", "listofmembers"]
136
137     def run(self, groupname, listofmembers, credopts=None, sambaopts=None,
138             versionopts=None, H=None):
139
140         lp = sambaopts.get_loadparm()
141         creds = credopts.get_credentials(lp, fallback_machine=True)
142
143         try:
144             samdb = SamDB(url=H, session_info=system_session(),
145                           credentials=creds, lp=lp)
146             samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=True)
147         except Exception, e:
148             raise CommandError('Failed to add members "%s" to group "%s"' % (listofmembers, groupname), e)
149
150
151 class cmd_group_remove_members(Command):
152     """Remove (comma-separated list of) group members"""
153
154     synopsis = "%prog group removemembers <groupname> <listofmembers>"
155
156     takes_optiongroups = {
157         "sambaopts": options.SambaOptions,
158         "versionopts": options.VersionOptions,
159         "credopts": options.CredentialsOptions,
160     }
161
162     takes_options = [
163         Option("-H", help="LDB URL for database or target server", type=str),
164     ]
165
166     takes_args = ["groupname", "listofmembers"]
167
168     def run(self, groupname, listofmembers, credopts=None, sambaopts=None,
169             versionopts=None, H=None):
170
171         lp = sambaopts.get_loadparm()
172         creds = credopts.get_credentials(lp, fallback_machine=True)
173
174         try:
175             samdb = SamDB(url=H, session_info=system_session(),
176                           credentials=creds, lp=lp)
177             samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=False)
178         except Exception, e:
179             raise CommandError('Failed to remove members "%s" from group "%s"' % (listofmembers, groupname), e)
180
181
182 class cmd_group(SuperCommand):
183     """Group management"""
184
185     subcommands = {}
186     subcommands["add"] = cmd_group_add()
187     subcommands["delete"] = cmd_group_delete()
188     subcommands["addmembers"] = cmd_group_add_members()
189     subcommands["removemembers"] = cmd_group_remove_members()