41506bb2056b9cd216ff0bf949eb19194c324fcb
[samba.git] / source4 / scripting / python / samba / netcmd / group.py
1 #!/usr/bin/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)
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 ldb.LdbError, (num, msg):
87             raise CommandError('Failed to create group "%s" : %s' % (
88                 groupname, msg))
89
90
91 class cmd_group_delete(Command):
92     """Delete a group"""
93
94     synopsis = "%prog group delete <groupname>"
95
96     takes_optiongroups = {
97         "sambaopts": options.SambaOptions,
98         "versionopts": options.VersionOptions,
99         "credopts": options.CredentialsOptions,
100     }
101
102     takes_options = [
103         Option("-H", help="LDB URL for database or target server", type=str),
104     ]
105
106     takes_args = ["groupname"]
107
108     def run(self, groupname, credopts=None, sambaopts=None, versionopts=None, H=None):
109
110         lp = sambaopts.get_loadparm()
111         creds = credopts.get_credentials(lp)
112
113         try:
114             samdb = SamDB(url=H, session_info=system_session(),
115                           credentials=creds, lp=lp)
116             samdb.deletegroup(groupname)
117         except ldb.LdbError, (num, msg):
118             raise CommandError('Failed to remove group "%s": %s' % (
119                 groupname , msg))
120
121
122 class cmd_group_add_members(Command):
123     """Add (comma-separated list of) group members"""
124
125     synopsis = "%prog group addmembers <groupname> <listofmembers>"
126
127     takes_optiongroups = {
128         "sambaopts": options.SambaOptions,
129         "versionopts": options.VersionOptions,
130         "credopts": options.CredentialsOptions,
131     }
132
133     takes_options = [
134         Option("-H", help="LDB URL for database or target server", type=str),
135     ]
136
137     takes_args = ["groupname", "listofmembers"]
138
139     def run(self, groupname, listofmembers, credopts=None, sambaopts=None,
140             versionopts=None, H=None):
141
142         lp = sambaopts.get_loadparm()
143         creds = credopts.get_credentials(lp)
144
145         try:
146             samdb = SamDB(url=H, session_info=system_session(),
147                           credentials=creds, lp=lp)
148             samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=True)
149         except ldb.LdbError, (num, msg):
150             raise CommandError('Failed to add members "%s" to group "%s": %s' % (
151                 listofmembers, groupname , msg))
152
153
154 class cmd_group_remove_members(Command):
155     """Remove (comma-separated list of) group members"""
156
157     synopsis = "%prog group removemembers <groupname> <listofmembers>"
158
159     takes_optiongroups = {
160         "sambaopts": options.SambaOptions,
161         "versionopts": options.VersionOptions,
162         "credopts": options.CredentialsOptions,
163     }
164
165     takes_options = [
166         Option("-H", help="LDB URL for database or target server", type=str),
167     ]
168
169     takes_args = ["groupname", "listofmembers"]
170
171     def run(self, groupname, listofmembers, credopts=None, sambaopts=None,
172             versionopts=None, H=None):
173
174         lp = sambaopts.get_loadparm()
175         creds = credopts.get_credentials(lp)
176
177         try:
178             samdb = SamDB(url=H, session_info=system_session(),
179                           credentials=creds, lp=lp)
180             samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=False)
181         except ldb.LdbError, (num, msg):
182             raise CommandError('Failed to remove members "%s" from group "%s": %s' % (
183                 listofmembers, groupname , msg))
184
185
186 class cmd_group(SuperCommand):
187     """Group management"""
188
189     subcommands = {}
190     subcommands["add"] = cmd_group_add()
191     subcommands["delete"] = cmd_group_delete()
192     subcommands["addmembers"] = cmd_group_add_members()
193     subcommands["removemembers"] = cmd_group_remove_members()