2 Unix SMB/CIFS implementation.
4 lsa calls for file sharing connections
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 when dealing with ACLs the file sharing client code needs to
24 sometimes make LSA RPC calls. This code provides an easy interface
25 for doing those calls.
29 #include "libcli/raw/libcliraw.h"
30 #include "libcli/libcli.h"
31 #include "libcli/security/security.h"
32 #include "librpc/gen_ndr/ndr_lsa.h"
33 #include "librpc/gen_ndr/ndr_lsa_c.h"
34 #include "libcli/util/clilsa.h"
35 #include "param/param.h"
38 struct dcerpc_pipe *pipe;
39 struct smbcli_tree *ipc_tree;
40 struct policy_handle handle;
44 establish the lsa pipe connection
46 static NTSTATUS smblsa_connect(struct smbcli_state *cli)
48 struct smblsa_state *lsa;
50 struct lsa_OpenPolicy r;
51 uint16_t system_name = '\\';
53 struct lsa_ObjectAttribute attr;
54 struct lsa_QosInfo qos;
56 if (cli->lsa != NULL) {
60 lsa = talloc(cli, struct smblsa_state);
62 return NT_STATUS_NO_MEMORY;
65 lsa->ipc_tree = smbcli_tree_init(cli->session, lsa, false);
66 if (lsa->ipc_tree == NULL) {
67 return NT_STATUS_NO_MEMORY;
71 tcon.generic.level = RAW_TCON_TCONX;
72 tcon.tconx.in.flags = 0;
73 tcon.tconx.in.password = data_blob(NULL, 0);
74 tcon.tconx.in.path = "ipc$";
75 tcon.tconx.in.device = "IPC";
76 status = smb_raw_tcon(lsa->ipc_tree, lsa, &tcon);
77 if (!NT_STATUS_IS_OK(status)) {
81 lsa->ipc_tree->tid = tcon.tconx.out.tid;
83 lsa->pipe = dcerpc_pipe_init(lsa, cli->transport->socket->event.ctx,
84 lp_iconv_convenience(global_loadparm));
85 if (lsa->pipe == NULL) {
87 return NT_STATUS_NO_MEMORY;
90 /* open the LSA pipe */
91 status = dcerpc_pipe_open_smb(lsa->pipe, lsa->ipc_tree, NDR_LSARPC_NAME);
92 if (!NT_STATUS_IS_OK(status)) {
97 /* bind to the LSA pipe */
98 status = dcerpc_bind_auth_none(lsa->pipe, &ndr_table_lsarpc);
99 if (!NT_STATUS_IS_OK(status)) {
105 /* open a lsa policy handle */
107 qos.impersonation_level = 2;
108 qos.context_mode = 1;
109 qos.effective_only = 0;
112 attr.root_dir = NULL;
113 attr.object_name = NULL;
115 attr.sec_desc = NULL;
118 r.in.system_name = &system_name;
120 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
121 r.out.handle = &lsa->handle;
123 status = dcerpc_lsa_OpenPolicy(lsa->pipe, lsa, &r);
124 if (!NT_STATUS_IS_OK(status)) {
136 return the set of privileges for the given sid
138 NTSTATUS smblsa_sid_privileges(struct smbcli_state *cli, struct dom_sid *sid,
140 struct lsa_RightSet *rights)
143 struct lsa_EnumAccountRights r;
145 status = smblsa_connect(cli);
146 if (!NT_STATUS_IS_OK(status)) {
150 r.in.handle = &cli->lsa->handle;
152 r.out.rights = rights;
154 return dcerpc_lsa_EnumAccountRights(cli->lsa->pipe, mem_ctx, &r);
159 check if a named sid has a particular named privilege
161 NTSTATUS smblsa_sid_check_privilege(struct smbcli_state *cli,
163 const char *privilege)
165 struct lsa_RightSet rights;
167 TALLOC_CTX *mem_ctx = talloc_new(cli);
171 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
173 talloc_free(mem_ctx);
174 return NT_STATUS_INVALID_SID;
177 status = smblsa_sid_privileges(cli, sid, mem_ctx, &rights);
178 if (!NT_STATUS_IS_OK(status)) {
179 talloc_free(mem_ctx);
183 for (i=0;i<rights.count;i++) {
184 if (strcmp(rights.names[i].string, privilege) == 0) {
185 talloc_free(mem_ctx);
190 talloc_free(mem_ctx);
191 return NT_STATUS_NOT_FOUND;
196 lookup a SID, returning its name
198 NTSTATUS smblsa_lookup_sid(struct smbcli_state *cli,
203 struct lsa_LookupSids r;
204 struct lsa_TransNameArray names;
205 struct lsa_SidArray sids;
209 TALLOC_CTX *mem_ctx2 = talloc_new(mem_ctx);
211 status = smblsa_connect(cli);
212 if (!NT_STATUS_IS_OK(status)) {
216 sid = dom_sid_parse_talloc(mem_ctx2, sid_str);
218 return NT_STATUS_INVALID_SID;
225 sids.sids = talloc(mem_ctx2, struct lsa_SidPtr);
226 sids.sids[0].sid = sid;
228 r.in.handle = &cli->lsa->handle;
233 r.out.count = &count;
234 r.out.names = &names;
236 status = dcerpc_lsa_LookupSids(cli->lsa->pipe, mem_ctx2, &r);
237 if (!NT_STATUS_IS_OK(status)) {
238 talloc_free(mem_ctx2);
241 if (names.count != 1) {
242 talloc_free(mem_ctx2);
243 return NT_STATUS_UNSUCCESSFUL;
246 (*name) = talloc_asprintf(mem_ctx, "%s\\%s",
247 r.out.domains->domains[0].name.string,
248 names.names[0].name.string);
250 talloc_free(mem_ctx2);
256 lookup a name, returning its sid
258 NTSTATUS smblsa_lookup_name(struct smbcli_state *cli,
261 const char **sid_str)
263 struct lsa_LookupNames r;
264 struct lsa_TransSidArray sids;
265 struct lsa_String names;
269 TALLOC_CTX *mem_ctx2 = talloc_new(mem_ctx);
272 status = smblsa_connect(cli);
273 if (!NT_STATUS_IS_OK(status)) {
282 r.in.handle = &cli->lsa->handle;
288 r.out.count = &count;
291 status = dcerpc_lsa_LookupNames(cli->lsa->pipe, mem_ctx2, &r);
292 if (!NT_STATUS_IS_OK(status)) {
293 talloc_free(mem_ctx2);
296 if (sids.count != 1) {
297 talloc_free(mem_ctx2);
298 return NT_STATUS_UNSUCCESSFUL;
301 sid = r.out.domains->domains[0].sid;
302 rid = sids.sids[0].rid;
304 (*sid_str) = talloc_asprintf(mem_ctx, "%s-%u",
305 dom_sid_string(mem_ctx2, sid), rid);
307 talloc_free(mem_ctx2);
314 add a set of privileges to the given sid
316 NTSTATUS smblsa_sid_add_privileges(struct smbcli_state *cli, struct dom_sid *sid,
318 struct lsa_RightSet *rights)
321 struct lsa_AddAccountRights r;
323 status = smblsa_connect(cli);
324 if (!NT_STATUS_IS_OK(status)) {
328 r.in.handle = &cli->lsa->handle;
330 r.in.rights = rights;
332 return dcerpc_lsa_AddAccountRights(cli->lsa->pipe, mem_ctx, &r);
336 remove a set of privileges from the given sid
338 NTSTATUS smblsa_sid_del_privileges(struct smbcli_state *cli, struct dom_sid *sid,
340 struct lsa_RightSet *rights)
343 struct lsa_RemoveAccountRights r;
345 status = smblsa_connect(cli);
346 if (!NT_STATUS_IS_OK(status)) {
350 r.in.handle = &cli->lsa->handle;
353 r.in.rights = rights;
355 return dcerpc_lsa_RemoveAccountRights(cli->lsa->pipe, mem_ctx, &r);