summaryrefslogtreecommitdiff
path: root/python/samba/netcmd/domain/auth/silo/member.py
blob: 02e5cd53163df9357900234df16b74a374bcb73b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Unix SMB/CIFS implementation.
#
# authentication silos - silo member management
#
# Copyright (C) Catalyst.Net Ltd. 2023
#
# Written by Rob van der Linde <rob@catalyst.net.nz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import samba.getopt as options
from samba.domain.models import AuthenticationSilo, User
from samba.domain.models.exceptions import ModelError
from samba.netcmd import Command, CommandError, Option, SuperCommand


class cmd_domain_auth_silo_member_grant(Command):
    """Grant a member access to an authentication silo."""

    synopsis = "%prog -H <URL> [options]"

    takes_optiongroups = {
        "sambaopts": options.SambaOptions,
        "credopts": options.CredentialsOptions,
        "hostopts": options.HostOptions,
    }

    takes_options = [
        Option("--name",
               help="Name of authentication silo (required).",
               dest="name", action="store", type=str, required=True),
        Option("--member",
               help="Member to grant access to the silo (DN or account name).",
               dest="member", action="store", type=str, required=True),
    ]

    def run(self, hostopts=None, sambaopts=None, credopts=None,
            name=None, member=None):

        ldb = self.ldb_connect(hostopts, sambaopts, credopts)

        try:
            silo = AuthenticationSilo.get(ldb, cn=name)
        except ModelError as e:
            raise CommandError(e)

        # Check if authentication silo exists first.
        if silo is None:
            raise CommandError(f"Authentication silo {name} not found.")

        try:
            user = User.find(ldb, member)
        except ModelError as e:
            raise CommandError(e)

        # Ensure the user actually exists first.
        if user is None:
            raise CommandError(f"User {member} not found.")

        # Grant access to member.
        try:
            silo.grant(ldb, user)
        except ModelError as e:
            raise CommandError(e)

        # Display silo assigned status.
        if user.assigned_silo and user.assigned_silo == silo.dn:
            status = "assigned"
        else:
            status = "unassigned"

        print(f"User {user} granted access to the authentication silo {name} ({status}).",
              file=self.outf)


class cmd_domain_auth_silo_member_list(Command):
    """List all members in the authentication silo."""

    synopsis = "%prog -H <URL> [options]"

    takes_optiongroups = {
        "sambaopts": options.SambaOptions,
        "credopts": options.CredentialsOptions,
        "hostopts": options.HostOptions,
    }

    takes_options = [
        Option("--name",
               help="Name of authentication silo (required).",
               dest="name", action="store", type=str, required=True),
        Option("--json", help="Output results in JSON format.",
               dest="output_format", action="store_const", const="json"),
    ]

    def run(self, hostopts=None, sambaopts=None, credopts=None,
            name=None, output_format=None):

        ldb = self.ldb_connect(hostopts, sambaopts, credopts)

        try:
            silo = AuthenticationSilo.get(ldb, cn=name)
        except ModelError as e:
            raise CommandError(e)

        # Check if authentication silo exists first.
        if silo is None:
            raise CommandError(f"Authentication silo {name} not found.")

        # Fetch all members.
        try:
            members = [User.get(ldb, dn=dn) for dn in silo.members]
        except ModelError as e:
            raise CommandError(e)

        # Using json output format gives more detail.
        if output_format == "json":
            self.print_json([member.as_dict() for member in members])
        else:
            for member in members:
                print(member.dn, file=self.outf)


class cmd_domain_auth_silo_member_revoke(Command):
    """Revoke a member from an authentication silo."""

    synopsis = "%prog -H <URL> [options]"

    takes_optiongroups = {
        "sambaopts": options.SambaOptions,
        "credopts": options.CredentialsOptions,
        "hostopts": options.HostOptions,
    }

    takes_options = [
        Option("--name",
               help="Name of authentication silo (required).",
               dest="name", action="store", type=str, required=True),
        Option("--member",
               help="Member to revoke from the silo (DN or account name).",
               dest="member", action="store", type=str, required=True),
    ]

    def run(self, hostopts=None, sambaopts=None, credopts=None,
            name=None, member=None):

        ldb = self.ldb_connect(hostopts, sambaopts, credopts)

        try:
            silo = AuthenticationSilo.get(ldb, cn=name)
        except ModelError as e:
            raise CommandError(e)

        # Check if authentication silo exists first.
        if silo is None:
            raise CommandError(f"Authentication silo {name} not found.")

        try:
            user = User.find(ldb, member)
        except ModelError as e:
            raise CommandError(e)

        # Ensure the user actually exists first.
        if user is None:
            raise CommandError(f"User {member} not found.")

        # Revoke member access.
        try:
            silo.revoke(ldb, user)
        except ModelError as e:
            raise CommandError(e)

        # Display silo assigned status.
        if user.assigned_silo and user.assigned_silo == silo.dn:
            status = "assigned"
        else:
            status = "unassigned"

        print(f"User {user} revoked from the authentication silo {name} ({status}).",
              file=self.outf)


class cmd_domain_auth_silo_member(SuperCommand):
    """Manage members in an authentication silo."""

    subcommands = {
        "grant": cmd_domain_auth_silo_member_grant(),
        "list": cmd_domain_auth_silo_member_list(),
        "revoke": cmd_domain_auth_silo_member_revoke(),
    }