2 Unix SMB/CIFS implementation.
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.
28 /** @defgroup lsa LSA - Local Security Architecture
37 * RPC client routines for the LSA RPC pipe. LSA means "local
38 * security authority", which is half of a password database.
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;
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 */
65 init_lsa_sec_qos(&qos, 2, 1, 0);
66 init_q_open_pol(&q, '\\', 0, des_access, &qos);
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;
79 /* Unmarshall response */
81 if (!lsa_io_r_open_pol("", &r, &rbuf, 0)) {
82 result = NT_STATUS_UNSUCCESSFUL;
86 /* Return output parameters */
88 if (NT_STATUS_IS_OK(result = r.status)) {
99 /** Open a LSA policy handle
101 * @param cli Handle on an initialised SMB connection
104 NTSTATUS cli_lsa_open_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
105 BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
107 prs_struct qbuf, rbuf;
116 /* Initialise parse structures */
118 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
119 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
121 /* Initialise input parameters */
124 init_lsa_sec_qos(&qos, 2, 1, 0);
125 init_q_open_pol2(&q, cli->srv_name_slash, 0, des_access,
128 init_q_open_pol2(&q, cli->srv_name_slash, 0, des_access,
132 /* Marshall data and send request */
134 if (!lsa_io_q_open_pol2("", &q, &qbuf, 0) ||
135 !rpc_api_pipe_req(cli, LSA_OPENPOLICY2, &qbuf, &rbuf)) {
136 result = NT_STATUS_UNSUCCESSFUL;
140 /* Unmarshall response */
142 if (!lsa_io_r_open_pol2("", &r, &rbuf, 0)) {
143 result = NT_STATUS_UNSUCCESSFUL;
147 /* Return output parameters */
149 if (NT_STATUS_IS_OK(result = r.status)) {
160 /** Close a LSA policy handle */
162 NTSTATUS cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
165 prs_struct qbuf, rbuf;
173 /* Initialise parse structures */
175 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
176 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
178 /* Marshall data and send request */
180 init_lsa_q_close(&q, pol);
182 if (!lsa_io_q_close("", &q, &qbuf, 0) ||
183 !rpc_api_pipe_req(cli, LSA_CLOSE, &qbuf, &rbuf)) {
184 result = NT_STATUS_UNSUCCESSFUL;
188 /* Unmarshall response */
190 if (!lsa_io_r_close("", &r, &rbuf, 0)) {
191 result = NT_STATUS_UNSUCCESSFUL;
195 /* Return output parameters */
197 if (NT_STATUS_IS_OK(result = r.status)) {
208 /** Lookup a list of sids */
210 NTSTATUS cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
211 POLICY_HND *pol, int num_sids, DOM_SID *sids,
212 char ***domains, char ***names, uint32 **types)
214 prs_struct qbuf, rbuf;
218 LSA_TRANS_NAME_ENUM t_names;
225 /* Initialise parse structures */
227 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
228 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
230 /* Marshall data and send request */
232 init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
234 if (!lsa_io_q_lookup_sids("", &q, &qbuf, 0) ||
235 !rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &qbuf, &rbuf)) {
236 result = NT_STATUS_UNSUCCESSFUL;
240 /* Unmarshall response */
243 ZERO_STRUCT(t_names);
248 if (!lsa_io_r_lookup_sids("", &r, &rbuf, 0)) {
249 result = NT_STATUS_UNSUCCESSFUL;
255 if (!NT_STATUS_IS_OK(result) &&
256 NT_STATUS_V(result) != NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
258 /* An actual error occured */
263 /* Return output parameters */
265 if (r.mapped_count == 0) {
266 result = NT_STATUS_NONE_MAPPED;
270 if (!((*domains) = (char **)talloc(mem_ctx, sizeof(char *) *
272 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
273 result = NT_STATUS_UNSUCCESSFUL;
277 if (!((*names) = (char **)talloc(mem_ctx, sizeof(char *) *
279 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
280 result = NT_STATUS_UNSUCCESSFUL;
284 if (!((*types) = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
286 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
287 result = NT_STATUS_UNSUCCESSFUL;
291 for (i = 0; i < num_sids; i++) {
292 fstring name, dom_name;
293 uint32 dom_idx = t_names.name[i].domain_idx;
295 /* Translate optimised name through domain index array */
297 if (dom_idx != 0xffffffff) {
299 rpcstr_pull_unistr2_fstring(
300 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
301 rpcstr_pull_unistr2_fstring(
302 name, &t_names.uni_name[i]);
304 (*names)[i] = talloc_strdup(mem_ctx, name);
305 (*domains)[i] = talloc_strdup(mem_ctx, dom_name);
306 (*types)[i] = t_names.name[i].sid_name_use;
308 if (((*names)[i] == NULL) || ((*domains)[i] == NULL)) {
309 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
310 result = NT_STATUS_UNSUCCESSFUL;
316 (*types)[i] = SID_NAME_UNKNOWN;
327 /** Lookup a list of names */
329 NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
330 POLICY_HND *pol, int num_names,
331 const char **names, DOM_SID **sids,
334 prs_struct qbuf, rbuf;
335 LSA_Q_LOOKUP_NAMES q;
336 LSA_R_LOOKUP_NAMES r;
344 /* Initialise parse structures */
346 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
347 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
349 /* Marshall data and send request */
351 init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
353 if (!lsa_io_q_lookup_names("", &q, &qbuf, 0) ||
354 !rpc_api_pipe_req(cli, LSA_LOOKUPNAMES, &qbuf, &rbuf)) {
355 result = NT_STATUS_UNSUCCESSFUL;
359 /* Unmarshall response */
364 if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
365 result = NT_STATUS_UNSUCCESSFUL;
371 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
372 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
374 /* An actual error occured */
379 /* Return output parameters */
381 if (r.mapped_count == 0) {
382 result = NT_STATUS_NONE_MAPPED;
386 if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
388 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
389 result = NT_STATUS_UNSUCCESSFUL;
393 if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
395 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
396 result = NT_STATUS_UNSUCCESSFUL;
400 for (i = 0; i < num_names; i++) {
401 DOM_RID2 *t_rids = r.dom_rid;
402 uint32 dom_idx = t_rids[i].rid_idx;
403 uint32 dom_rid = t_rids[i].rid;
404 DOM_SID *sid = &(*sids)[i];
406 /* Translate optimised sid through domain index array */
408 if (dom_idx != 0xffffffff) {
410 sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
412 if (dom_rid != 0xffffffff) {
413 sid_append_rid(sid, dom_rid);
416 (*types)[i] = t_rids[i].type;
419 (*types)[i] = SID_NAME_UNKNOWN;
430 /** Query info policy
432 * @param domain_sid - returned remote server's domain sid */
434 NTSTATUS cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
435 POLICY_HND *pol, uint16 info_class,
436 fstring domain_name, DOM_SID *domain_sid)
438 prs_struct qbuf, rbuf;
446 /* Initialise parse structures */
448 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
449 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
451 /* Marshall data and send request */
453 init_q_query(&q, pol, info_class);
455 if (!lsa_io_q_query("", &q, &qbuf, 0) ||
456 !rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &qbuf, &rbuf)) {
457 result = NT_STATUS_UNSUCCESSFUL;
461 /* Unmarshall response */
463 if (!lsa_io_r_query("", &r, &rbuf, 0)) {
464 result = NT_STATUS_UNSUCCESSFUL;
468 if (!NT_STATUS_IS_OK(result = r.status)) {
472 /* Return output parameters */
474 ZERO_STRUCTP(domain_sid);
475 domain_name[0] = '\0';
477 switch (info_class) {
480 if (r.dom.id3.buffer_dom_name != 0) {
481 unistr2_to_ascii(domain_name,
484 sizeof (fstring) - 1);
487 if (r.dom.id3.buffer_dom_sid != 0) {
488 *domain_sid = r.dom.id3.dom_sid.sid;
495 if (r.dom.id5.buffer_dom_name != 0) {
496 unistr2_to_ascii(domain_name, &r.dom.id5.
498 sizeof (fstring) - 1);
501 if (r.dom.id5.buffer_dom_sid != 0) {
502 *domain_sid = r.dom.id5.dom_sid.sid;
508 DEBUG(3, ("unknown info class %d\n", info_class));
519 /** Query info policy2
521 * @param domain_name - returned remote server's domain name
522 * @param dns_name - returned remote server's dns domain name
523 * @param forest_name - returned remote server's forest name
524 * @param domain_guid - returned remote server's domain guid
525 * @param domain_sid - returned remote server's domain sid */
527 NTSTATUS cli_lsa_query_info_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
528 POLICY_HND *pol, uint16 info_class,
529 fstring domain_name, fstring dns_name,
530 fstring forest_name, GUID *domain_guid,
533 prs_struct qbuf, rbuf;
536 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
538 if (info_class != 12)
544 /* Initialise parse structures */
546 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
547 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
549 /* Marshall data and send request */
551 init_q_query2(&q, pol, info_class);
553 if (!lsa_io_q_query_info2("", &q, &qbuf, 0) ||
554 !rpc_api_pipe_req(cli, LSA_QUERYINFO2, &qbuf, &rbuf)) {
555 result = NT_STATUS_UNSUCCESSFUL;
559 /* Unmarshall response */
561 if (!lsa_io_r_query_info2("", &r, &rbuf, 0)) {
562 result = NT_STATUS_UNSUCCESSFUL;
566 if (!NT_STATUS_IS_OK(result = r.status)) {
570 /* Return output parameters */
572 ZERO_STRUCTP(domain_sid);
573 ZERO_STRUCTP(domain_guid);
574 domain_name[0] = '\0';
576 if (r.info.dns_dom_info.hdr_nb_dom_name.buffer) {
577 unistr2_to_ascii(domain_name,
578 &r.info.dns_dom_info.uni_nb_dom_name,
579 sizeof(fstring) - 1);
581 if (r.info.dns_dom_info.hdr_dns_dom_name.buffer) {
582 unistr2_to_ascii(dns_name,
583 &r.info.dns_dom_info.uni_dns_dom_name,
584 sizeof(fstring) - 1);
586 if (r.info.dns_dom_info.hdr_forest_name.buffer) {
587 unistr2_to_ascii(forest_name,
588 &r.info.dns_dom_info.uni_forest_name,
589 sizeof(fstring) - 1);
592 memcpy(domain_guid, &r.info.dns_dom_info.dom_guid, sizeof(GUID));
594 if (r.info.dns_dom_info.ptr_dom_sid != 0) {
595 *domain_sid = r.info.dns_dom_info.dom_sid.sid;
606 * Enumerate list of trusted domains
608 * @param cli client state (cli_state) structure of the connection
609 * @param mem_ctx memory context
610 * @param pol opened lsa policy handle
611 * @param enum_ctx enumeration context ie. index of first returned domain entry
612 * @param pref_num_domains preferred max number of entries returned in one response
613 * @param num_domains total number of trusted domains returned by response
614 * @param domain_names returned trusted domain names
615 * @param domain_sids returned trusted domain sids
617 * @return nt status code of response
620 NTSTATUS cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
621 POLICY_HND *pol, uint32 *enum_ctx,
623 char ***domain_names, DOM_SID **domain_sids)
625 prs_struct qbuf, rbuf;
626 LSA_Q_ENUM_TRUST_DOM q;
627 LSA_R_ENUM_TRUST_DOM r;
634 /* Initialise parse structures */
636 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
637 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
639 /* Marshall data and send request */
641 /* 64k is enough for about 2000 trusted domains */
642 init_q_enum_trust_dom(&q, pol, *enum_ctx, 0x10000);
644 if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
645 !rpc_api_pipe_req(cli, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
646 result = NT_STATUS_UNSUCCESSFUL;
650 /* Unmarshall response */
652 if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
653 result = NT_STATUS_UNSUCCESSFUL;
659 if (!NT_STATUS_IS_OK(result) &&
660 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
661 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
663 /* An actual error ocured */
668 /* Return output parameters */
672 /* Allocate memory for trusted domain names and sids */
674 *domain_names = (char **)talloc(mem_ctx, sizeof(char *) *
677 if (!*domain_names) {
678 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
679 result = NT_STATUS_NO_MEMORY;
683 *domain_sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
686 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
687 result = NT_STATUS_NO_MEMORY;
691 /* Copy across names and sids */
693 for (i = 0; i < r.num_domains; i++) {
696 unistr2_to_ascii(tmp, &r.uni_domain_name[i],
698 (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
699 sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
703 *num_domains = r.num_domains;
704 *enum_ctx = r.enum_context;
714 /** Enumerate privileges*/
716 NTSTATUS cli_lsa_enum_privilege(struct cli_state *cli, TALLOC_CTX *mem_ctx,
717 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
718 uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
720 prs_struct qbuf, rbuf;
729 /* Initialise parse structures */
731 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
732 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
734 /* Marshall data and send request */
736 init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
738 if (!lsa_io_q_enum_privs("", &q, &qbuf, 0) ||
739 !rpc_api_pipe_req(cli, LSA_ENUM_PRIVS, &qbuf, &rbuf)) {
740 result = NT_STATUS_UNSUCCESSFUL;
744 /* Unmarshall response */
746 if (!lsa_io_r_enum_privs("", &r, &rbuf, 0)) {
747 result = NT_STATUS_UNSUCCESSFUL;
751 if (!NT_STATUS_IS_OK(result = r.status)) {
755 /* Return output parameters */
757 *enum_context = r.enum_context;
760 if (!((*privs_name = (char **)talloc(mem_ctx, sizeof(char *) * r.count)))) {
761 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
762 result = NT_STATUS_UNSUCCESSFUL;
766 if (!((*privs_high = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.count)))) {
767 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
768 result = NT_STATUS_UNSUCCESSFUL;
772 if (!((*privs_low = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.count)))) {
773 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
774 result = NT_STATUS_UNSUCCESSFUL;
778 for (i = 0; i < r.count; i++) {
781 rpcstr_pull_unistr2_fstring( name, &r.privs[i].name);
783 (*privs_name)[i] = talloc_strdup(mem_ctx, name);
785 (*privs_high)[i] = r.privs[i].luid_high;
786 (*privs_low)[i] = r.privs[i].luid_low;
796 /** Get privilege name */
798 NTSTATUS cli_lsa_get_dispname(struct cli_state *cli, TALLOC_CTX *mem_ctx,
799 POLICY_HND *pol, const char *name,
800 uint16 lang_id, uint16 lang_id_sys,
801 fstring description, uint16 *lang_id_desc)
803 prs_struct qbuf, rbuf;
804 LSA_Q_PRIV_GET_DISPNAME q;
805 LSA_R_PRIV_GET_DISPNAME r;
811 /* Initialise parse structures */
813 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
814 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
816 /* Marshall data and send request */
818 init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
820 if (!lsa_io_q_priv_get_dispname("", &q, &qbuf, 0) ||
821 !rpc_api_pipe_req(cli, LSA_PRIV_GET_DISPNAME, &qbuf, &rbuf)) {
822 result = NT_STATUS_UNSUCCESSFUL;
826 /* Unmarshall response */
828 if (!lsa_io_r_priv_get_dispname("", &r, &rbuf, 0)) {
829 result = NT_STATUS_UNSUCCESSFUL;
833 if (!NT_STATUS_IS_OK(result = r.status)) {
837 /* Return output parameters */
839 rpcstr_pull_unistr2_fstring(description , &r.desc);
840 *lang_id_desc = r.lang_id;
849 /** Enumerate list of SIDs */
851 NTSTATUS cli_lsa_enum_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
852 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length,
853 uint32 *num_sids, DOM_SID **sids)
855 prs_struct qbuf, rbuf;
856 LSA_Q_ENUM_ACCOUNTS q;
857 LSA_R_ENUM_ACCOUNTS r;
864 /* Initialise parse structures */
866 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
867 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
869 /* Marshall data and send request */
871 init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
873 if (!lsa_io_q_enum_accounts("", &q, &qbuf, 0) ||
874 !rpc_api_pipe_req(cli, LSA_ENUM_ACCOUNTS, &qbuf, &rbuf)) {
875 result = NT_STATUS_UNSUCCESSFUL;
879 /* Unmarshall response */
881 if (!lsa_io_r_enum_accounts("", &r, &rbuf, 0)) {
882 result = NT_STATUS_UNSUCCESSFUL;
888 if (!NT_STATUS_IS_OK(result = r.status)) {
892 if (r.sids.num_entries==0)
895 /* Return output parameters */
897 *sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * r.sids.num_entries);
899 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
900 result = NT_STATUS_UNSUCCESSFUL;
904 /* Copy across names and sids */
906 for (i = 0; i < r.sids.num_entries; i++) {
907 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
910 *num_sids= r.sids.num_entries;
911 *enum_ctx = r.enum_context;
920 /** Open a LSA user handle
922 * @param cli Handle on an initialised SMB connection */
924 NTSTATUS cli_lsa_open_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
925 POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access,
926 POLICY_HND *user_pol)
928 prs_struct qbuf, rbuf;
936 /* Initialise parse structures */
938 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
939 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
941 /* Initialise input parameters */
943 init_lsa_q_open_account(&q, dom_pol, sid, des_access);
945 /* Marshall data and send request */
947 if (!lsa_io_q_open_account("", &q, &qbuf, 0) ||
948 !rpc_api_pipe_req(cli, LSA_OPENACCOUNT, &qbuf, &rbuf)) {
949 result = NT_STATUS_UNSUCCESSFUL;
953 /* Unmarshall response */
955 if (!lsa_io_r_open_account("", &r, &rbuf, 0)) {
956 result = NT_STATUS_UNSUCCESSFUL;
960 /* Return output parameters */
962 if (NT_STATUS_IS_OK(result = r.status)) {
973 /** Enumerate user privileges
975 * @param cli Handle on an initialised SMB connection */
977 NTSTATUS cli_lsa_enum_privsaccount(struct cli_state *cli, TALLOC_CTX *mem_ctx,
978 POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
980 prs_struct qbuf, rbuf;
981 LSA_Q_ENUMPRIVSACCOUNT q;
982 LSA_R_ENUMPRIVSACCOUNT r;
989 /* Initialise parse structures */
991 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
992 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
994 /* Initialise input parameters */
996 init_lsa_q_enum_privsaccount(&q, pol);
998 /* Marshall data and send request */
1000 if (!lsa_io_q_enum_privsaccount("", &q, &qbuf, 0) ||
1001 !rpc_api_pipe_req(cli, LSA_ENUMPRIVSACCOUNT, &qbuf, &rbuf)) {
1002 result = NT_STATUS_UNSUCCESSFUL;
1006 /* Unmarshall response */
1008 if (!lsa_io_r_enum_privsaccount("", &r, &rbuf, 0)) {
1009 result = NT_STATUS_UNSUCCESSFUL;
1013 /* Return output parameters */
1015 if (!NT_STATUS_IS_OK(result = r.status)) {
1022 if (!((*set = (LUID_ATTR *)talloc(mem_ctx, sizeof(LUID_ATTR) * r.count)))) {
1023 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
1024 result = NT_STATUS_UNSUCCESSFUL;
1028 for (i=0; i<r.count; i++) {
1029 (*set)[i].luid.low = r.set.set[i].luid.low;
1030 (*set)[i].luid.high = r.set.set[i].luid.high;
1031 (*set)[i].attr = r.set.set[i].attr;
1036 prs_mem_free(&qbuf);
1037 prs_mem_free(&rbuf);
1042 /** Get a privilege value given its name */
1044 NTSTATUS cli_lsa_lookupprivvalue(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1045 POLICY_HND *pol, const char *name, LUID *luid)
1047 prs_struct qbuf, rbuf;
1048 LSA_Q_LOOKUPPRIVVALUE q;
1049 LSA_R_LOOKUPPRIVVALUE r;
1055 /* Initialise parse structures */
1057 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1058 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1060 /* Marshall data and send request */
1062 init_lsa_q_lookupprivvalue(&q, pol, name);
1064 if (!lsa_io_q_lookupprivvalue("", &q, &qbuf, 0) ||
1065 !rpc_api_pipe_req(cli, LSA_LOOKUPPRIVVALUE, &qbuf, &rbuf)) {
1066 result = NT_STATUS_UNSUCCESSFUL;
1070 /* Unmarshall response */
1072 if (!lsa_io_r_lookupprivvalue("", &r, &rbuf, 0)) {
1073 result = NT_STATUS_UNSUCCESSFUL;
1077 if (!NT_STATUS_IS_OK(result = r.status)) {
1081 /* Return output parameters */
1083 (*luid).low=r.luid.low;
1084 (*luid).high=r.luid.high;
1087 prs_mem_free(&qbuf);
1088 prs_mem_free(&rbuf);
1093 /** Query LSA security object */
1095 NTSTATUS cli_lsa_query_secobj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1096 POLICY_HND *pol, uint32 sec_info,
1097 SEC_DESC_BUF **psdb)
1099 prs_struct qbuf, rbuf;
1100 LSA_Q_QUERY_SEC_OBJ q;
1101 LSA_R_QUERY_SEC_OBJ r;
1107 /* Initialise parse structures */
1109 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1110 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1112 /* Marshall data and send request */
1114 init_q_query_sec_obj(&q, pol, sec_info);
1116 if (!lsa_io_q_query_sec_obj("", &q, &qbuf, 0) ||
1117 !rpc_api_pipe_req(cli, LSA_QUERYSECOBJ, &qbuf, &rbuf)) {
1118 result = NT_STATUS_UNSUCCESSFUL;
1122 /* Unmarshall response */
1124 if (!lsa_io_r_query_sec_obj("", &r, &rbuf, 0)) {
1125 result = NT_STATUS_UNSUCCESSFUL;
1129 if (!NT_STATUS_IS_OK(result = r.status)) {
1133 /* Return output parameters */
1139 prs_mem_free(&qbuf);
1140 prs_mem_free(&rbuf);
1146 /* Enumerate account rights This is similar to enum_privileges but
1147 takes a SID directly, avoiding the open_account call.
1150 NTSTATUS cli_lsa_enum_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1151 POLICY_HND *pol, DOM_SID sid,
1152 uint32 *count, char ***privs_name)
1154 prs_struct qbuf, rbuf;
1155 LSA_Q_ENUM_ACCT_RIGHTS q;
1156 LSA_R_ENUM_ACCT_RIGHTS r;
1163 /* Initialise parse structures */
1165 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1166 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1168 /* Marshall data and send request */
1169 init_q_enum_acct_rights(&q, pol, 2, &sid);
1171 if (!lsa_io_q_enum_acct_rights("", &q, &qbuf, 0) ||
1172 !rpc_api_pipe_req(cli, LSA_ENUMACCTRIGHTS, &qbuf, &rbuf)) {
1173 result = NT_STATUS_UNSUCCESSFUL;
1177 if (!lsa_io_r_enum_acct_rights("", &r, &rbuf, 0)) {
1178 result = NT_STATUS_UNSUCCESSFUL;
1182 if (!NT_STATUS_IS_OK(result = r.status)) {
1191 *privs_name = (char **)talloc(mem_ctx, (*count) * sizeof(char **));
1192 for (i=0;i<*count;i++) {
1193 (*privs_name)[i] = unistr2_tdup(mem_ctx, &r.rights.strings[i].string);
1203 /* add account rights to an account. */
1205 NTSTATUS cli_lsa_add_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1206 POLICY_HND *pol, DOM_SID sid,
1207 uint32 count, const char **privs_name)
1209 prs_struct qbuf, rbuf;
1210 LSA_Q_ADD_ACCT_RIGHTS q;
1211 LSA_R_ADD_ACCT_RIGHTS r;
1216 /* Initialise parse structures */
1217 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1218 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1220 /* Marshall data and send request */
1221 init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
1223 if (!lsa_io_q_add_acct_rights("", &q, &qbuf, 0) ||
1224 !rpc_api_pipe_req(cli, LSA_ADDACCTRIGHTS, &qbuf, &rbuf)) {
1225 result = NT_STATUS_UNSUCCESSFUL;
1229 /* Unmarshall response */
1231 if (!lsa_io_r_add_acct_rights("", &r, &rbuf, 0)) {
1232 result = NT_STATUS_UNSUCCESSFUL;
1236 if (!NT_STATUS_IS_OK(result = r.status)) {
1245 /* remove account rights for an account. */
1247 NTSTATUS cli_lsa_remove_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1248 POLICY_HND *pol, DOM_SID sid, BOOL removeall,
1249 uint32 count, const char **privs_name)
1251 prs_struct qbuf, rbuf;
1252 LSA_Q_REMOVE_ACCT_RIGHTS q;
1253 LSA_R_REMOVE_ACCT_RIGHTS r;
1258 /* Initialise parse structures */
1259 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1260 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1262 /* Marshall data and send request */
1263 init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
1265 if (!lsa_io_q_remove_acct_rights("", &q, &qbuf, 0) ||
1266 !rpc_api_pipe_req(cli, LSA_REMOVEACCTRIGHTS, &qbuf, &rbuf)) {
1267 result = NT_STATUS_UNSUCCESSFUL;
1271 /* Unmarshall response */
1273 if (!lsa_io_r_remove_acct_rights("", &r, &rbuf, 0)) {
1274 result = NT_STATUS_UNSUCCESSFUL;
1278 if (!NT_STATUS_IS_OK(result = r.status)) {
1287 /* list account SIDs that have the specified right */
1289 NTSTATUS cli_lsa_enum_account_with_right(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1290 POLICY_HND *pol, const char *right,
1291 uint32 *count, DOM_SID **sids)
1293 prs_struct qbuf, rbuf;
1294 LSA_Q_ENUM_ACCT_WITH_RIGHT q;
1295 LSA_R_ENUM_ACCT_WITH_RIGHT r;
1300 /* Initialise parse structures */
1301 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1302 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1304 /* Marshall data and send request */
1305 init_q_enum_acct_with_right(&q, pol, right);
1307 if (!lsa_io_q_enum_acct_with_right("", &q, &qbuf, 0) ||
1308 !rpc_api_pipe_req(cli, LSA_ENUMACCTWITHRIGHT, &qbuf, &rbuf)) {
1309 result = NT_STATUS_UNSUCCESSFUL;
1313 /* Unmarshall response */
1315 if (!lsa_io_r_enum_acct_with_right("", &r, &rbuf, 0)) {
1316 result = NT_STATUS_UNSUCCESSFUL;
1322 if (!NT_STATUS_IS_OK(result = r.status)) {
1328 (*sids) = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * (*count));
1329 for (i=0; i<*count; i++) {
1330 sid_copy(&(*sids)[i], &r.sids.sids[i].sid.sid);
1341 /** An example of how to use the routines in this file. Fetch a DOMAIN
1342 sid. Does complete cli setup / teardown anonymously. */
1344 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
1346 struct cli_state cli;
1352 if(cli_initialise(&cli) == False) {
1353 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1357 if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
1358 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
1362 if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
1363 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1364 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1368 if (!attempt_netbios_session_request(&cli, lp_netbios_name(), remote_machine, &cli.dest_ip)) {
1369 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
1374 cli.protocol = PROTOCOL_NT1;
1376 if (!cli_negprot(&cli)) {
1377 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1378 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1382 if (cli.protocol != PROTOCOL_NT1) {
1383 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1389 * Do an anonymous session setup.
1392 if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
1393 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1394 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1398 if (!(cli.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
1399 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1404 if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
1405 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1406 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1410 /* Fetch domain sid */
1412 if (!cli_nt_session_open(&cli, PI_LSARPC)) {
1413 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1417 result = cli_lsa_open_policy(&cli, cli.mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
1418 if (!NT_STATUS_IS_OK(result)) {
1419 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1420 nt_errstr(result) ));
1424 result = cli_lsa_query_info_policy(&cli, cli.mem_ctx, &lsa_pol, 5, domain, psid);
1425 if (!NT_STATUS_IS_OK(result)) {
1426 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1427 nt_errstr(result) ));