3 # Adds a new user to a Samba4 server
4 # Copyright Jelmer Vernooij 2008
6 # Based on the original in EJS:
7 # Copyright Andrew Tridgell 2005
8 # Copyright Giampaolo Lauria 2011 <lauria2@yahoo.com>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 import samba.getopt as options
24 from samba.netcmd import Command, SuperCommand, CommandError, Option
27 from getpass import getpass
28 from samba.auth import system_session
29 from samba.samdb import SamDB
30 from samba.dsdb import (
31 GTYPE_SECURITY_DOMAIN_LOCAL_GROUP,
32 GTYPE_SECURITY_GLOBAL_GROUP,
33 GTYPE_SECURITY_UNIVERSAL_GROUP,
34 GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP,
35 GTYPE_DISTRIBUTION_GLOBAL_GROUP,
36 GTYPE_DISTRIBUTION_UNIVERSAL_GROUP,
39 security_group = dict({"Domain": GTYPE_SECURITY_DOMAIN_LOCAL_GROUP, "Global": GTYPE_SECURITY_GLOBAL_GROUP, "Universal": GTYPE_SECURITY_UNIVERSAL_GROUP})
40 distribution_group = dict({"Domain": GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP, "Global": GTYPE_DISTRIBUTION_GLOBAL_GROUP, "Universal": GTYPE_DISTRIBUTION_UNIVERSAL_GROUP})
43 class cmd_group_add(Command):
44 """Creates a new group"""
46 synopsis = "%prog group add [options] <groupname>"
49 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
50 metavar="URL", dest="H"),
52 help="Alternative location (without domainDN counterpart) to default CN=Users in which new user object will be created",
54 Option("--group-scope", type="choice", choices=["Domain", "Global", "Universal"],
55 help="Group scope (Domain | Global | Universal)"),
56 Option("--group-type", type="choice", choices=["Security", "Distribution"],
57 help="Group type (Security | Distribution)"),
58 Option("--description", help="Group's description", type=str),
59 Option("--mail-address", help="Group's email address", type=str),
60 Option("--notes", help="Groups's notes", type=str),
63 takes_args = ["groupname"]
65 def run(self, groupname, credopts=None, sambaopts=None,
66 versionopts=None, H=None, groupou=None, group_scope=None,
67 group_type=None, description=None, mail_address=None, notes=None):
69 if (group_type or "Security") == "Security":
70 gtype = security_group.get(group_scope, GTYPE_SECURITY_GLOBAL_GROUP)
72 gtype = distribution_group.get(group_scope, GTYPE_DISTRIBUTION_GLOBAL_GROUP)
74 lp = sambaopts.get_loadparm()
75 creds = credopts.get_credentials(lp, fallback_machine=True)
78 samdb = SamDB(url=H, session_info=system_session(),
79 credentials=creds, lp=lp)
80 samdb.newgroup(groupname, groupou=groupou, grouptype = gtype,
81 description=description, mailaddress=mail_address, notes=notes)
83 raise CommandError('Failed to create group "%s"' % groupname, e)
84 print("Added group %s" % groupname)
87 class cmd_group_delete(Command):
90 synopsis = "%prog group delete <groupname>"
93 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
94 metavar="URL", dest="H"),
97 takes_args = ["groupname"]
99 def run(self, groupname, credopts=None, sambaopts=None, versionopts=None, H=None):
101 lp = sambaopts.get_loadparm()
102 creds = credopts.get_credentials(lp, fallback_machine=True)
105 samdb = SamDB(url=H, session_info=system_session(),
106 credentials=creds, lp=lp)
107 samdb.deletegroup(groupname)
109 raise CommandError('Failed to remove group "%s"' % groupname, e)
110 print("Deleted group %s" % groupname)
113 class cmd_group_add_members(Command):
114 """Add (comma-separated list of) group members"""
116 synopsis = "%prog group addmembers <groupname> <listofmembers>"
119 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
120 metavar="URL", dest="H"),
123 takes_args = ["groupname", "listofmembers"]
125 def run(self, groupname, listofmembers, credopts=None, sambaopts=None,
126 versionopts=None, H=None):
128 lp = sambaopts.get_loadparm()
129 creds = credopts.get_credentials(lp, fallback_machine=True)
132 samdb = SamDB(url=H, session_info=system_session(),
133 credentials=creds, lp=lp)
134 samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=True)
136 raise CommandError('Failed to add members "%s" to group "%s"' % (listofmembers, groupname), e)
137 print("Added members to group %s" % groupname)
140 class cmd_group_remove_members(Command):
141 """Remove (comma-separated list of) group members"""
143 synopsis = "%prog group removemembers <groupname> <listofmembers>"
146 Option("-H", "--URL", help="LDB URL for database or target server", type=str,
147 metavar="URL", dest="H"),
150 takes_args = ["groupname", "listofmembers"]
152 def run(self, groupname, listofmembers, credopts=None, sambaopts=None,
153 versionopts=None, H=None):
155 lp = sambaopts.get_loadparm()
156 creds = credopts.get_credentials(lp, fallback_machine=True)
159 samdb = SamDB(url=H, session_info=system_session(),
160 credentials=creds, lp=lp)
161 samdb.add_remove_group_members(groupname, listofmembers, add_members_operation=False)
163 raise CommandError('Failed to remove members "%s" from group "%s"' % (listofmembers, groupname), e)
164 print("Removed members from group %s" % groupname)
167 class cmd_group(SuperCommand):
168 """Group management"""
171 subcommands["add"] = cmd_group_add()
172 subcommands["delete"] = cmd_group_delete()
173 subcommands["addmembers"] = cmd_group_add_members()
174 subcommands["removemembers"] = cmd_group_remove_members()