Apply Craig Barratt's fixes to allow multiple exlusion files and patterns.
[Samba/gebeck_regimport.git] / source3 / rpc_client / cli_lsarpc.c
blob40b83c5c0c7e7818ad79b953a2a0bf735e79785b
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
4 Copyright (C) Tim Potter 2000-2001,
5 Copyright (C) Andrew Tridgell 1992-1997,2000,
6 Copyright (C) Luke Kenneth Casson Leighton 1996-1997,2000,
7 Copyright (C) Paul Ashton 1997,2000,
8 Copyright (C) Elrond 2000,
9 Copyright (C) Rafal Szczesniak 2002
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
28 /** @defgroup lsa LSA - Local Security Architecture
29 * @ingroup rpc_client
31 * @{
32 **/
34 /**
35 * @file cli_lsarpc.c
37 * RPC client routines for the LSA RPC pipe. LSA means "local
38 * security authority", which is half of a password database.
39 **/
41 /** Open a LSA policy handle
43 * @param cli Handle on an initialised SMB connection */
45 NTSTATUS cli_lsa_open_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
46 BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
48 prs_struct qbuf, rbuf;
49 LSA_Q_OPEN_POL q;
50 LSA_R_OPEN_POL r;
51 LSA_SEC_QOS qos;
52 NTSTATUS result;
54 ZERO_STRUCT(q);
55 ZERO_STRUCT(r);
57 /* Initialise parse structures */
59 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
60 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
62 /* Initialise input parameters */
64 if (sec_qos) {
65 init_lsa_sec_qos(&qos, 2, 1, 0);
66 init_q_open_pol(&q, '\\', 0, des_access, &qos);
67 } else {
68 init_q_open_pol(&q, '\\', 0, des_access, NULL);
71 /* Marshall data and send request */
73 if (!lsa_io_q_open_pol("", &q, &qbuf, 0) ||
74 !rpc_api_pipe_req(cli, LSA_OPENPOLICY, &qbuf, &rbuf)) {
75 result = NT_STATUS_UNSUCCESSFUL;
76 goto done;
79 /* Unmarshall response */
81 if (!lsa_io_r_open_pol("", &r, &rbuf, 0)) {
82 result = NT_STATUS_UNSUCCESSFUL;
83 goto done;
86 /* Return output parameters */
88 if (NT_STATUS_IS_OK(result = r.status)) {
89 *pol = r.pol;
90 #ifdef __INSURE__
91 pol->marker = malloc(1);
92 #endif
95 done:
96 prs_mem_free(&qbuf);
97 prs_mem_free(&rbuf);
99 return result;
102 /** Open a LSA policy handle
104 * @param cli Handle on an initialised SMB connection
107 NTSTATUS cli_lsa_open_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
108 BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
110 prs_struct qbuf, rbuf;
111 LSA_Q_OPEN_POL2 q;
112 LSA_R_OPEN_POL2 r;
113 LSA_SEC_QOS qos;
114 NTSTATUS result;
116 ZERO_STRUCT(q);
117 ZERO_STRUCT(r);
119 /* Initialise parse structures */
121 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
122 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
124 /* Initialise input parameters */
126 if (sec_qos) {
127 init_lsa_sec_qos(&qos, 2, 1, 0);
128 init_q_open_pol2(&q, cli->srv_name_slash, 0, des_access,
129 &qos);
130 } else {
131 init_q_open_pol2(&q, cli->srv_name_slash, 0, des_access,
132 NULL);
135 /* Marshall data and send request */
137 if (!lsa_io_q_open_pol2("", &q, &qbuf, 0) ||
138 !rpc_api_pipe_req(cli, LSA_OPENPOLICY2, &qbuf, &rbuf)) {
139 result = NT_STATUS_UNSUCCESSFUL;
140 goto done;
143 /* Unmarshall response */
145 if (!lsa_io_r_open_pol2("", &r, &rbuf, 0)) {
146 result = NT_STATUS_UNSUCCESSFUL;
147 goto done;
150 /* Return output parameters */
152 if (NT_STATUS_IS_OK(result = r.status)) {
153 *pol = r.pol;
154 #ifdef __INSURE__
155 pol->marker = (char *)malloc(1);
156 #endif
159 done:
160 prs_mem_free(&qbuf);
161 prs_mem_free(&rbuf);
163 return result;
166 /** Close a LSA policy handle */
168 NTSTATUS cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
169 POLICY_HND *pol)
171 prs_struct qbuf, rbuf;
172 LSA_Q_CLOSE q;
173 LSA_R_CLOSE r;
174 NTSTATUS result;
176 ZERO_STRUCT(q);
177 ZERO_STRUCT(r);
179 /* Initialise parse structures */
181 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
182 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
184 /* Marshall data and send request */
186 init_lsa_q_close(&q, pol);
188 if (!lsa_io_q_close("", &q, &qbuf, 0) ||
189 !rpc_api_pipe_req(cli, LSA_CLOSE, &qbuf, &rbuf)) {
190 result = NT_STATUS_UNSUCCESSFUL;
191 goto done;
194 /* Unmarshall response */
196 if (!lsa_io_r_close("", &r, &rbuf, 0)) {
197 result = NT_STATUS_UNSUCCESSFUL;
198 goto done;
201 /* Return output parameters */
203 if (NT_STATUS_IS_OK(result = r.status)) {
204 #ifdef __INSURE__
205 SAFE_FREE(pol->marker);
206 #endif
207 *pol = r.pol;
210 done:
211 prs_mem_free(&qbuf);
212 prs_mem_free(&rbuf);
214 return result;
217 /** Lookup a list of sids */
219 NTSTATUS cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
220 POLICY_HND *pol, int num_sids, const DOM_SID *sids,
221 char ***domains, char ***names, uint32 **types)
223 prs_struct qbuf, rbuf;
224 LSA_Q_LOOKUP_SIDS q;
225 LSA_R_LOOKUP_SIDS r;
226 DOM_R_REF ref;
227 LSA_TRANS_NAME_ENUM t_names;
228 NTSTATUS result;
229 int i;
231 ZERO_STRUCT(q);
232 ZERO_STRUCT(r);
234 /* Initialise parse structures */
236 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
237 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
239 /* Marshall data and send request */
241 init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
243 if (!lsa_io_q_lookup_sids("", &q, &qbuf, 0) ||
244 !rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &qbuf, &rbuf)) {
245 result = NT_STATUS_UNSUCCESSFUL;
246 goto done;
249 /* Unmarshall response */
251 ZERO_STRUCT(ref);
252 ZERO_STRUCT(t_names);
254 r.dom_ref = &ref;
255 r.names = &t_names;
257 if (!lsa_io_r_lookup_sids("", &r, &rbuf, 0)) {
258 result = NT_STATUS_UNSUCCESSFUL;
259 goto done;
262 result = r.status;
264 if (!NT_STATUS_IS_OK(result) &&
265 NT_STATUS_V(result) != NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
267 /* An actual error occured */
269 goto done;
272 /* Return output parameters */
274 if (r.mapped_count == 0) {
275 result = NT_STATUS_NONE_MAPPED;
276 goto done;
279 if (!((*domains) = (char **)talloc(mem_ctx, sizeof(char *) *
280 num_sids))) {
281 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
282 result = NT_STATUS_UNSUCCESSFUL;
283 goto done;
286 if (!((*names) = (char **)talloc(mem_ctx, sizeof(char *) *
287 num_sids))) {
288 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
289 result = NT_STATUS_UNSUCCESSFUL;
290 goto done;
293 if (!((*types) = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
294 num_sids))) {
295 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
296 result = NT_STATUS_UNSUCCESSFUL;
297 goto done;
300 for (i = 0; i < num_sids; i++) {
301 fstring name, dom_name;
302 uint32 dom_idx = t_names.name[i].domain_idx;
304 /* Translate optimised name through domain index array */
306 if (dom_idx != 0xffffffff) {
308 rpcstr_pull_unistr2_fstring(
309 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
310 rpcstr_pull_unistr2_fstring(
311 name, &t_names.uni_name[i]);
313 (*names)[i] = talloc_strdup(mem_ctx, name);
314 (*domains)[i] = talloc_strdup(mem_ctx, dom_name);
315 (*types)[i] = t_names.name[i].sid_name_use;
317 if (((*names)[i] == NULL) || ((*domains)[i] == NULL)) {
318 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
319 result = NT_STATUS_UNSUCCESSFUL;
320 goto done;
323 } else {
324 (*names)[i] = NULL;
325 (*domains)[i] = NULL;
326 (*types)[i] = SID_NAME_UNKNOWN;
330 done:
331 prs_mem_free(&qbuf);
332 prs_mem_free(&rbuf);
334 return result;
337 /** Lookup a list of names */
339 NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
340 POLICY_HND *pol, int num_names,
341 const char **names, DOM_SID **sids,
342 uint32 **types)
344 prs_struct qbuf, rbuf;
345 LSA_Q_LOOKUP_NAMES q;
346 LSA_R_LOOKUP_NAMES r;
347 DOM_R_REF ref;
348 NTSTATUS result;
349 int i;
351 ZERO_STRUCT(q);
352 ZERO_STRUCT(r);
354 /* Initialise parse structures */
356 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
357 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
359 /* Marshall data and send request */
361 init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
363 if (!lsa_io_q_lookup_names("", &q, &qbuf, 0) ||
364 !rpc_api_pipe_req(cli, LSA_LOOKUPNAMES, &qbuf, &rbuf)) {
365 result = NT_STATUS_UNSUCCESSFUL;
366 goto done;
369 /* Unmarshall response */
371 ZERO_STRUCT(ref);
372 r.dom_ref = &ref;
374 if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
375 result = NT_STATUS_UNSUCCESSFUL;
376 goto done;
379 result = r.status;
381 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
382 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
384 /* An actual error occured */
386 goto done;
389 /* Return output parameters */
391 if (r.mapped_count == 0) {
392 result = NT_STATUS_NONE_MAPPED;
393 goto done;
396 if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
397 num_names)))) {
398 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
399 result = NT_STATUS_UNSUCCESSFUL;
400 goto done;
403 if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
404 num_names)))) {
405 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
406 result = NT_STATUS_UNSUCCESSFUL;
407 goto done;
410 for (i = 0; i < num_names; i++) {
411 DOM_RID2 *t_rids = r.dom_rid;
412 uint32 dom_idx = t_rids[i].rid_idx;
413 uint32 dom_rid = t_rids[i].rid;
414 DOM_SID *sid = &(*sids)[i];
416 /* Translate optimised sid through domain index array */
418 if (dom_idx != 0xffffffff) {
420 sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
422 if (dom_rid != 0xffffffff) {
423 sid_append_rid(sid, dom_rid);
426 (*types)[i] = t_rids[i].type;
427 } else {
428 ZERO_STRUCTP(sid);
429 (*types)[i] = SID_NAME_UNKNOWN;
433 done:
434 prs_mem_free(&qbuf);
435 prs_mem_free(&rbuf);
437 return result;
440 /** Query info policy
442 * @param domain_sid - returned remote server's domain sid */
444 NTSTATUS cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
445 POLICY_HND *pol, uint16 info_class,
446 char **domain_name, DOM_SID **domain_sid)
448 prs_struct qbuf, rbuf;
449 LSA_Q_QUERY_INFO q;
450 LSA_R_QUERY_INFO r;
451 NTSTATUS result;
453 ZERO_STRUCT(q);
454 ZERO_STRUCT(r);
456 /* Initialise parse structures */
458 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
459 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
461 /* Marshall data and send request */
463 init_q_query(&q, pol, info_class);
465 if (!lsa_io_q_query("", &q, &qbuf, 0) ||
466 !rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &qbuf, &rbuf)) {
467 result = NT_STATUS_UNSUCCESSFUL;
468 goto done;
471 /* Unmarshall response */
473 if (!lsa_io_r_query("", &r, &rbuf, 0)) {
474 result = NT_STATUS_UNSUCCESSFUL;
475 goto done;
478 if (!NT_STATUS_IS_OK(result = r.status)) {
479 goto done;
482 /* Return output parameters */
484 switch (info_class) {
486 case 3:
487 if (domain_name && (r.dom.id3.buffer_dom_name != 0)) {
488 *domain_name = unistr2_tdup(mem_ctx,
489 &r.dom.id3.
490 uni_domain_name);
493 if (domain_sid && (r.dom.id3.buffer_dom_sid != 0)) {
494 *domain_sid = talloc(mem_ctx, sizeof(**domain_sid));
495 if (*domain_sid) {
496 sid_copy(*domain_sid, &r.dom.id3.dom_sid.sid);
500 break;
502 case 5:
504 if (domain_name && (r.dom.id5.buffer_dom_name != 0)) {
505 *domain_name = unistr2_tdup(mem_ctx,
506 &r.dom.id5.
507 uni_domain_name);
510 if (domain_sid && (r.dom.id5.buffer_dom_sid != 0)) {
511 *domain_sid = talloc(mem_ctx, sizeof(**domain_sid));
512 if (*domain_sid) {
513 sid_copy(*domain_sid, &r.dom.id5.dom_sid.sid);
516 break;
518 default:
519 DEBUG(3, ("unknown info class %d\n", info_class));
520 break;
523 done:
524 prs_mem_free(&qbuf);
525 prs_mem_free(&rbuf);
527 return result;
530 /** Query info policy2
532 * @param domain_name - returned remote server's domain name
533 * @param dns_name - returned remote server's dns domain name
534 * @param forest_name - returned remote server's forest name
535 * @param domain_guid - returned remote server's domain guid
536 * @param domain_sid - returned remote server's domain sid */
538 NTSTATUS cli_lsa_query_info_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
539 POLICY_HND *pol, uint16 info_class,
540 char **domain_name, char **dns_name,
541 char **forest_name, struct uuid **domain_guid,
542 DOM_SID **domain_sid)
544 prs_struct qbuf, rbuf;
545 LSA_Q_QUERY_INFO2 q;
546 LSA_R_QUERY_INFO2 r;
547 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
549 if (info_class != 12)
550 goto done;
552 ZERO_STRUCT(q);
553 ZERO_STRUCT(r);
555 /* Initialise parse structures */
557 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
558 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
560 /* Marshall data and send request */
562 init_q_query2(&q, pol, info_class);
564 if (!lsa_io_q_query_info2("", &q, &qbuf, 0) ||
565 !rpc_api_pipe_req(cli, LSA_QUERYINFO2, &qbuf, &rbuf)) {
566 result = NT_STATUS_UNSUCCESSFUL;
567 goto done;
570 /* Unmarshall response */
572 if (!lsa_io_r_query_info2("", &r, &rbuf, 0)) {
573 result = NT_STATUS_UNSUCCESSFUL;
574 goto done;
577 if (!NT_STATUS_IS_OK(result = r.status)) {
578 goto done;
581 /* Return output parameters */
583 ZERO_STRUCTP(domain_guid);
585 if (domain_name && r.info.dns_dom_info.hdr_nb_dom_name.buffer) {
586 *domain_name = unistr2_tdup(mem_ctx,
587 &r.info.dns_dom_info
588 .uni_nb_dom_name);
590 if (dns_name && r.info.dns_dom_info.hdr_dns_dom_name.buffer) {
591 *dns_name = unistr2_tdup(mem_ctx,
592 &r.info.dns_dom_info
593 .uni_dns_dom_name);
595 if (forest_name && r.info.dns_dom_info.hdr_forest_name.buffer) {
596 *forest_name = unistr2_tdup(mem_ctx,
597 &r.info.dns_dom_info
598 .uni_forest_name);
601 if (domain_guid) {
602 *domain_guid = talloc(mem_ctx, sizeof(**domain_guid));
603 memcpy(*domain_guid,
604 &r.info.dns_dom_info.dom_guid,
605 sizeof(struct uuid));
608 if (domain_sid && r.info.dns_dom_info.ptr_dom_sid != 0) {
609 *domain_sid = talloc(mem_ctx, sizeof(**domain_sid));
610 if (*domain_sid) {
611 sid_copy(*domain_sid,
612 &r.info.dns_dom_info.dom_sid.sid);
616 done:
617 prs_mem_free(&qbuf);
618 prs_mem_free(&rbuf);
620 return result;
624 * Enumerate list of trusted domains
626 * @param cli client state (cli_state) structure of the connection
627 * @param mem_ctx memory context
628 * @param pol opened lsa policy handle
629 * @param enum_ctx enumeration context ie. index of first returned domain entry
630 * @param pref_num_domains preferred max number of entries returned in one response
631 * @param num_domains total number of trusted domains returned by response
632 * @param domain_names returned trusted domain names
633 * @param domain_sids returned trusted domain sids
635 * @return nt status code of response
638 NTSTATUS cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
639 POLICY_HND *pol, uint32 *enum_ctx,
640 uint32 *num_domains,
641 char ***domain_names, DOM_SID **domain_sids)
643 prs_struct qbuf, rbuf;
644 LSA_Q_ENUM_TRUST_DOM q;
645 LSA_R_ENUM_TRUST_DOM r;
646 NTSTATUS result;
647 int i;
649 ZERO_STRUCT(q);
650 ZERO_STRUCT(r);
652 /* Initialise parse structures */
654 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
655 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
657 /* Marshall data and send request */
659 /* 64k is enough for about 2000 trusted domains */
660 init_q_enum_trust_dom(&q, pol, *enum_ctx, 0x10000);
662 if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
663 !rpc_api_pipe_req(cli, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
664 result = NT_STATUS_UNSUCCESSFUL;
665 goto done;
668 /* Unmarshall response */
670 if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
671 result = NT_STATUS_UNSUCCESSFUL;
672 goto done;
675 result = r.status;
677 if (!NT_STATUS_IS_OK(result) &&
678 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
679 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
681 /* An actual error ocured */
683 goto done;
686 /* Return output parameters */
688 if (r.num_domains) {
690 /* Allocate memory for trusted domain names and sids */
692 *domain_names = (char **)talloc(mem_ctx, sizeof(char *) *
693 r.num_domains);
695 if (!*domain_names) {
696 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
697 result = NT_STATUS_NO_MEMORY;
698 goto done;
701 *domain_sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
702 r.num_domains);
703 if (!domain_sids) {
704 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
705 result = NT_STATUS_NO_MEMORY;
706 goto done;
709 /* Copy across names and sids */
711 for (i = 0; i < r.num_domains; i++) {
712 fstring tmp;
714 unistr2_to_ascii(tmp, &r.uni_domain_name[i],
715 sizeof(tmp) - 1);
716 (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
717 sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
721 *num_domains = r.num_domains;
722 *enum_ctx = r.enum_context;
724 done:
725 prs_mem_free(&qbuf);
726 prs_mem_free(&rbuf);
728 return result;
732 /** Enumerate privileges*/
734 NTSTATUS cli_lsa_enum_privilege(struct cli_state *cli, TALLOC_CTX *mem_ctx,
735 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
736 uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
738 prs_struct qbuf, rbuf;
739 LSA_Q_ENUM_PRIVS q;
740 LSA_R_ENUM_PRIVS r;
741 NTSTATUS result;
742 int i;
744 ZERO_STRUCT(q);
745 ZERO_STRUCT(r);
747 /* Initialise parse structures */
749 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
750 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
752 /* Marshall data and send request */
754 init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
756 if (!lsa_io_q_enum_privs("", &q, &qbuf, 0) ||
757 !rpc_api_pipe_req(cli, LSA_ENUM_PRIVS, &qbuf, &rbuf)) {
758 result = NT_STATUS_UNSUCCESSFUL;
759 goto done;
762 /* Unmarshall response */
764 if (!lsa_io_r_enum_privs("", &r, &rbuf, 0)) {
765 result = NT_STATUS_UNSUCCESSFUL;
766 goto done;
769 if (!NT_STATUS_IS_OK(result = r.status)) {
770 goto done;
773 /* Return output parameters */
775 *enum_context = r.enum_context;
776 *count = r.count;
778 if (!((*privs_name = (char **)talloc(mem_ctx, sizeof(char *) * r.count)))) {
779 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
780 result = NT_STATUS_UNSUCCESSFUL;
781 goto done;
784 if (!((*privs_high = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.count)))) {
785 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
786 result = NT_STATUS_UNSUCCESSFUL;
787 goto done;
790 if (!((*privs_low = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.count)))) {
791 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
792 result = NT_STATUS_UNSUCCESSFUL;
793 goto done;
796 for (i = 0; i < r.count; i++) {
797 fstring name;
799 rpcstr_pull_unistr2_fstring( name, &r.privs[i].name);
801 (*privs_name)[i] = talloc_strdup(mem_ctx, name);
803 (*privs_high)[i] = r.privs[i].luid_high;
804 (*privs_low)[i] = r.privs[i].luid_low;
807 done:
808 prs_mem_free(&qbuf);
809 prs_mem_free(&rbuf);
811 return result;
814 /** Get privilege name */
816 NTSTATUS cli_lsa_get_dispname(struct cli_state *cli, TALLOC_CTX *mem_ctx,
817 POLICY_HND *pol, const char *name,
818 uint16 lang_id, uint16 lang_id_sys,
819 fstring description, uint16 *lang_id_desc)
821 prs_struct qbuf, rbuf;
822 LSA_Q_PRIV_GET_DISPNAME q;
823 LSA_R_PRIV_GET_DISPNAME r;
824 NTSTATUS result;
826 ZERO_STRUCT(q);
827 ZERO_STRUCT(r);
829 /* Initialise parse structures */
831 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
832 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
834 /* Marshall data and send request */
836 init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
838 if (!lsa_io_q_priv_get_dispname("", &q, &qbuf, 0) ||
839 !rpc_api_pipe_req(cli, LSA_PRIV_GET_DISPNAME, &qbuf, &rbuf)) {
840 result = NT_STATUS_UNSUCCESSFUL;
841 goto done;
844 /* Unmarshall response */
846 if (!lsa_io_r_priv_get_dispname("", &r, &rbuf, 0)) {
847 result = NT_STATUS_UNSUCCESSFUL;
848 goto done;
851 if (!NT_STATUS_IS_OK(result = r.status)) {
852 goto done;
855 /* Return output parameters */
857 rpcstr_pull_unistr2_fstring(description , &r.desc);
858 *lang_id_desc = r.lang_id;
860 done:
861 prs_mem_free(&qbuf);
862 prs_mem_free(&rbuf);
864 return result;
867 /** Enumerate list of SIDs */
869 NTSTATUS cli_lsa_enum_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
870 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length,
871 uint32 *num_sids, DOM_SID **sids)
873 prs_struct qbuf, rbuf;
874 LSA_Q_ENUM_ACCOUNTS q;
875 LSA_R_ENUM_ACCOUNTS r;
876 NTSTATUS result;
877 int i;
879 ZERO_STRUCT(q);
880 ZERO_STRUCT(r);
882 /* Initialise parse structures */
884 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
885 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
887 /* Marshall data and send request */
889 init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
891 if (!lsa_io_q_enum_accounts("", &q, &qbuf, 0) ||
892 !rpc_api_pipe_req(cli, LSA_ENUM_ACCOUNTS, &qbuf, &rbuf)) {
893 result = NT_STATUS_UNSUCCESSFUL;
894 goto done;
897 /* Unmarshall response */
899 if (!lsa_io_r_enum_accounts("", &r, &rbuf, 0)) {
900 result = NT_STATUS_UNSUCCESSFUL;
901 goto done;
904 result = r.status;
906 if (!NT_STATUS_IS_OK(result = r.status)) {
907 goto done;
910 if (r.sids.num_entries==0)
911 goto done;
913 /* Return output parameters */
915 *sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * r.sids.num_entries);
916 if (!*sids) {
917 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
918 result = NT_STATUS_UNSUCCESSFUL;
919 goto done;
922 /* Copy across names and sids */
924 for (i = 0; i < r.sids.num_entries; i++) {
925 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
928 *num_sids= r.sids.num_entries;
929 *enum_ctx = r.enum_context;
931 done:
932 prs_mem_free(&qbuf);
933 prs_mem_free(&rbuf);
935 return result;
938 /** Open a LSA user handle
940 * @param cli Handle on an initialised SMB connection */
942 NTSTATUS cli_lsa_open_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
943 POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access,
944 POLICY_HND *user_pol)
946 prs_struct qbuf, rbuf;
947 LSA_Q_OPENACCOUNT q;
948 LSA_R_OPENACCOUNT r;
949 NTSTATUS result;
951 ZERO_STRUCT(q);
952 ZERO_STRUCT(r);
954 /* Initialise parse structures */
956 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
957 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
959 /* Initialise input parameters */
961 init_lsa_q_open_account(&q, dom_pol, sid, des_access);
963 /* Marshall data and send request */
965 if (!lsa_io_q_open_account("", &q, &qbuf, 0) ||
966 !rpc_api_pipe_req(cli, LSA_OPENACCOUNT, &qbuf, &rbuf)) {
967 result = NT_STATUS_UNSUCCESSFUL;
968 goto done;
971 /* Unmarshall response */
973 if (!lsa_io_r_open_account("", &r, &rbuf, 0)) {
974 result = NT_STATUS_UNSUCCESSFUL;
975 goto done;
978 /* Return output parameters */
980 if (NT_STATUS_IS_OK(result = r.status)) {
981 *user_pol = r.pol;
984 done:
985 prs_mem_free(&qbuf);
986 prs_mem_free(&rbuf);
988 return result;
991 /** Enumerate user privileges
993 * @param cli Handle on an initialised SMB connection */
995 NTSTATUS cli_lsa_enum_privsaccount(struct cli_state *cli, TALLOC_CTX *mem_ctx,
996 POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
998 prs_struct qbuf, rbuf;
999 LSA_Q_ENUMPRIVSACCOUNT q;
1000 LSA_R_ENUMPRIVSACCOUNT r;
1001 NTSTATUS result;
1002 int i;
1004 ZERO_STRUCT(q);
1005 ZERO_STRUCT(r);
1007 /* Initialise parse structures */
1009 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1010 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1012 /* Initialise input parameters */
1014 init_lsa_q_enum_privsaccount(&q, pol);
1016 /* Marshall data and send request */
1018 if (!lsa_io_q_enum_privsaccount("", &q, &qbuf, 0) ||
1019 !rpc_api_pipe_req(cli, LSA_ENUMPRIVSACCOUNT, &qbuf, &rbuf)) {
1020 result = NT_STATUS_UNSUCCESSFUL;
1021 goto done;
1024 /* Unmarshall response */
1026 if (!lsa_io_r_enum_privsaccount("", &r, &rbuf, 0)) {
1027 result = NT_STATUS_UNSUCCESSFUL;
1028 goto done;
1031 /* Return output parameters */
1033 if (!NT_STATUS_IS_OK(result = r.status)) {
1034 goto done;
1037 if (r.count == 0)
1038 goto done;
1040 if (!((*set = (LUID_ATTR *)talloc(mem_ctx, sizeof(LUID_ATTR) * r.count)))) {
1041 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
1042 result = NT_STATUS_UNSUCCESSFUL;
1043 goto done;
1046 for (i=0; i<r.count; i++) {
1047 (*set)[i].luid.low = r.set->set[i].luid.low;
1048 (*set)[i].luid.high = r.set->set[i].luid.high;
1049 (*set)[i].attr = r.set->set[i].attr;
1052 *count=r.count;
1053 done:
1054 prs_mem_free(&qbuf);
1055 prs_mem_free(&rbuf);
1057 return result;
1060 /** Get a privilege value given its name */
1062 NTSTATUS cli_lsa_lookupprivvalue(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1063 POLICY_HND *pol, const char *name, LUID *luid)
1065 prs_struct qbuf, rbuf;
1066 LSA_Q_LOOKUPPRIVVALUE q;
1067 LSA_R_LOOKUPPRIVVALUE r;
1068 NTSTATUS result;
1070 ZERO_STRUCT(q);
1071 ZERO_STRUCT(r);
1073 /* Initialise parse structures */
1075 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1076 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1078 /* Marshall data and send request */
1080 init_lsa_q_lookupprivvalue(&q, pol, name);
1082 if (!lsa_io_q_lookupprivvalue("", &q, &qbuf, 0) ||
1083 !rpc_api_pipe_req(cli, LSA_LOOKUPPRIVVALUE, &qbuf, &rbuf)) {
1084 result = NT_STATUS_UNSUCCESSFUL;
1085 goto done;
1088 /* Unmarshall response */
1090 if (!lsa_io_r_lookupprivvalue("", &r, &rbuf, 0)) {
1091 result = NT_STATUS_UNSUCCESSFUL;
1092 goto done;
1095 if (!NT_STATUS_IS_OK(result = r.status)) {
1096 goto done;
1099 /* Return output parameters */
1101 (*luid).low=r.luid.low;
1102 (*luid).high=r.luid.high;
1104 done:
1105 prs_mem_free(&qbuf);
1106 prs_mem_free(&rbuf);
1108 return result;
1111 /** Query LSA security object */
1113 NTSTATUS cli_lsa_query_secobj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1114 POLICY_HND *pol, uint32 sec_info,
1115 SEC_DESC_BUF **psdb)
1117 prs_struct qbuf, rbuf;
1118 LSA_Q_QUERY_SEC_OBJ q;
1119 LSA_R_QUERY_SEC_OBJ r;
1120 NTSTATUS result;
1122 ZERO_STRUCT(q);
1123 ZERO_STRUCT(r);
1125 /* Initialise parse structures */
1127 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1128 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1130 /* Marshall data and send request */
1132 init_q_query_sec_obj(&q, pol, sec_info);
1134 if (!lsa_io_q_query_sec_obj("", &q, &qbuf, 0) ||
1135 !rpc_api_pipe_req(cli, LSA_QUERYSECOBJ, &qbuf, &rbuf)) {
1136 result = NT_STATUS_UNSUCCESSFUL;
1137 goto done;
1140 /* Unmarshall response */
1142 if (!lsa_io_r_query_sec_obj("", &r, &rbuf, 0)) {
1143 result = NT_STATUS_UNSUCCESSFUL;
1144 goto done;
1147 if (!NT_STATUS_IS_OK(result = r.status)) {
1148 goto done;
1151 /* Return output parameters */
1153 if (psdb)
1154 *psdb = r.buf;
1156 done:
1157 prs_mem_free(&qbuf);
1158 prs_mem_free(&rbuf);
1160 return result;
1164 /* Enumerate account rights This is similar to enum_privileges but
1165 takes a SID directly, avoiding the open_account call.
1168 NTSTATUS cli_lsa_enum_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1169 POLICY_HND *pol, DOM_SID sid,
1170 uint32 *count, char ***privs_name)
1172 prs_struct qbuf, rbuf;
1173 LSA_Q_ENUM_ACCT_RIGHTS q;
1174 LSA_R_ENUM_ACCT_RIGHTS r;
1175 NTSTATUS result;
1176 int i;
1178 ZERO_STRUCT(q);
1179 ZERO_STRUCT(r);
1181 /* Initialise parse structures */
1183 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1184 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1186 /* Marshall data and send request */
1187 init_q_enum_acct_rights(&q, pol, 2, &sid);
1189 if (!lsa_io_q_enum_acct_rights("", &q, &qbuf, 0) ||
1190 !rpc_api_pipe_req(cli, LSA_ENUMACCTRIGHTS, &qbuf, &rbuf)) {
1191 result = NT_STATUS_UNSUCCESSFUL;
1192 goto done;
1195 if (!lsa_io_r_enum_acct_rights("", &r, &rbuf, 0)) {
1196 result = NT_STATUS_UNSUCCESSFUL;
1197 goto done;
1200 if (!NT_STATUS_IS_OK(result = r.status)) {
1201 goto done;
1204 *count = r.count;
1205 if (! *count) {
1206 goto done;
1209 *privs_name = (char **)talloc(mem_ctx, (*count) * sizeof(char **));
1210 for (i=0;i<*count;i++) {
1211 pull_ucs2_talloc(mem_ctx, &(*privs_name)[i], r.rights.strings[i].string.buffer);
1214 done:
1216 return result;
1221 /* add account rights to an account. */
1223 NTSTATUS cli_lsa_add_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1224 POLICY_HND *pol, DOM_SID sid,
1225 uint32 count, const char **privs_name)
1227 prs_struct qbuf, rbuf;
1228 LSA_Q_ADD_ACCT_RIGHTS q;
1229 LSA_R_ADD_ACCT_RIGHTS r;
1230 NTSTATUS result;
1232 ZERO_STRUCT(q);
1234 /* Initialise parse structures */
1235 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1236 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1238 /* Marshall data and send request */
1239 init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
1241 if (!lsa_io_q_add_acct_rights("", &q, &qbuf, 0) ||
1242 !rpc_api_pipe_req(cli, LSA_ADDACCTRIGHTS, &qbuf, &rbuf)) {
1243 result = NT_STATUS_UNSUCCESSFUL;
1244 goto done;
1247 /* Unmarshall response */
1249 if (!lsa_io_r_add_acct_rights("", &r, &rbuf, 0)) {
1250 result = NT_STATUS_UNSUCCESSFUL;
1251 goto done;
1254 if (!NT_STATUS_IS_OK(result = r.status)) {
1255 goto done;
1257 done:
1259 return result;
1263 /* remove account rights for an account. */
1265 NTSTATUS cli_lsa_remove_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1266 POLICY_HND *pol, DOM_SID sid, BOOL removeall,
1267 uint32 count, const char **privs_name)
1269 prs_struct qbuf, rbuf;
1270 LSA_Q_REMOVE_ACCT_RIGHTS q;
1271 LSA_R_REMOVE_ACCT_RIGHTS r;
1272 NTSTATUS result;
1274 ZERO_STRUCT(q);
1276 /* Initialise parse structures */
1277 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1278 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1280 /* Marshall data and send request */
1281 init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
1283 if (!lsa_io_q_remove_acct_rights("", &q, &qbuf, 0) ||
1284 !rpc_api_pipe_req(cli, LSA_REMOVEACCTRIGHTS, &qbuf, &rbuf)) {
1285 result = NT_STATUS_UNSUCCESSFUL;
1286 goto done;
1289 /* Unmarshall response */
1291 if (!lsa_io_r_remove_acct_rights("", &r, &rbuf, 0)) {
1292 result = NT_STATUS_UNSUCCESSFUL;
1293 goto done;
1296 if (!NT_STATUS_IS_OK(result = r.status)) {
1297 goto done;
1299 done:
1301 return result;
1305 #if 0
1307 /** An example of how to use the routines in this file. Fetch a DOMAIN
1308 sid. Does complete cli setup / teardown anonymously. */
1310 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
1312 extern pstring global_myname;
1313 struct cli_state cli;
1314 NTSTATUS result;
1315 POLICY_HND lsa_pol;
1316 BOOL ret = False;
1318 ZERO_STRUCT(cli);
1319 if(cli_initialise(&cli) == False) {
1320 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1321 return False;
1324 if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
1325 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
1326 goto done;
1329 if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
1330 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1331 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1332 goto done;
1335 if (!attempt_netbios_session_request(&cli, global_myname, remote_machine, &cli.dest_ip)) {
1336 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
1337 remote_machine));
1338 goto done;
1341 cli.protocol = PROTOCOL_NT1;
1343 if (!cli_negprot(&cli)) {
1344 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1345 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1346 goto done;
1349 if (cli.protocol != PROTOCOL_NT1) {
1350 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1351 remote_machine));
1352 goto done;
1356 * Do an anonymous session setup.
1359 if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
1360 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1361 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1362 goto done;
1365 if (!(cli.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
1366 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1367 remote_machine));
1368 goto done;
1371 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
1372 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1373 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1374 goto done;
1377 /* Fetch domain sid */
1379 if (!cli_nt_session_open(&cli, PI_LSARPC)) {
1380 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1381 goto done;
1384 result = cli_lsa_open_policy(&cli, cli.mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
1385 if (!NT_STATUS_IS_OK(result)) {
1386 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1387 nt_errstr(result) ));
1388 goto done;
1391 result = cli_lsa_query_info_policy(&cli, cli.mem_ctx, &lsa_pol, 5, domain, psid);
1392 if (!NT_STATUS_IS_OK(result)) {
1393 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1394 nt_errstr(result) ));
1395 goto done;
1398 ret = True;
1400 done:
1402 cli_shutdown(&cli);
1403 return ret;
1406 #endif
1408 /** @} **/