2 Unix SMB/Netbios implementation.
5 Copyright (C) Tim Potter 2000-2001,
6 Copyright (C) Andrew Tridgell 1992-1997,2000,
7 Copyright (C) Luke Kenneth Casson Leighton 1996-1997,2000,
8 Copyright (C) Paul Ashton 1997,2000,
9 Copyright (C) Elrond 2000.
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 /* Opens a SMB connection to the SAMR pipe */
30 struct cli_state *cli_samr_initialise(struct cli_state *cli, char *system_name,
31 struct ntuser_creds *creds)
33 struct in_addr dest_ip;
34 struct nmb_name calling, called;
36 extern pstring global_myname;
37 struct ntuser_creds anon;
39 /* Initialise cli_state information */
41 if (!cli_initialise(cli)) {
47 anon.pwd.null_pwd = 1;
51 cli_init_creds(cli, creds);
53 /* Establish a SMB connection */
55 if (!resolve_srv_name(system_name, dest_host, &dest_ip)) {
59 make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
60 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
62 if (!cli_establish_connection(cli, dest_host, &dest_ip, &calling,
63 &called, "IPC$", "IPC", False, True)) {
67 /* Open a NT session thingy */
69 if (!cli_nt_session_open(cli, PIPE_SAMR)) {
77 /* Shut down a SMB connection to the SAMR pipe */
79 void cli_samr_shutdown(struct cli_state *cli)
81 if (cli->fd != -1) cli_ulogoff(cli);
85 /* Connect to SAMR database */
87 uint32 cli_samr_connect(struct cli_state *cli, TALLOC_CTX *mem_ctx,
88 char *srv_name, uint32 access_mask,
89 POLICY_HND *connect_pol)
91 prs_struct qbuf, rbuf;
94 uint32 result = NT_STATUS_UNSUCCESSFUL;
99 /* Initialise parse structures */
101 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
102 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
104 /* Marshall data and send request */
106 init_samr_q_connect(&q, srv_name, access_mask);
108 if (!samr_io_q_connect("", &q, &qbuf, 0) ||
109 !rpc_api_pipe_req(cli, SAMR_CONNECT, &qbuf, &rbuf)) {
113 /* Unmarshall response */
115 if (!samr_io_r_connect("", &r, &rbuf, 0)) {
119 /* Return output parameters */
121 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
122 *connect_pol = r.connect_pol;
132 /* Close SAMR handle */
134 uint32 cli_samr_close(struct cli_state *cli, TALLOC_CTX *mem_ctx,
135 POLICY_HND *connect_pol)
137 prs_struct qbuf, rbuf;
140 uint32 result = NT_STATUS_UNSUCCESSFUL;
145 /* Initialise parse structures */
147 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
148 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
150 /* Marshall data and send request */
152 init_samr_q_close_hnd(&q, connect_pol);
154 if (!samr_io_q_close_hnd("", &q, &qbuf, 0) ||
155 !rpc_api_pipe_req(cli, SAMR_CLOSE_HND, &qbuf, &rbuf)) {
159 /* Unmarshall response */
161 if (!samr_io_r_close_hnd("", &r, &rbuf, 0)) {
165 /* Return output parameters */
167 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
168 *connect_pol = r.pol;
178 /* Open handle on a domain */
180 uint32 cli_samr_open_domain(struct cli_state *cli, TALLOC_CTX *mem_ctx,
181 POLICY_HND *connect_pol, uint32 access_mask,
182 DOM_SID *domain_sid, POLICY_HND *domain_pol)
184 prs_struct qbuf, rbuf;
185 SAMR_Q_OPEN_DOMAIN q;
186 SAMR_R_OPEN_DOMAIN r;
187 uint32 result = NT_STATUS_UNSUCCESSFUL;
192 /* Initialise parse structures */
194 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
195 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
197 /* Marshall data and send request */
199 init_samr_q_open_domain(&q, connect_pol, access_mask, domain_sid);
201 if (!samr_io_q_open_domain("", &q, &qbuf, 0) ||
202 !rpc_api_pipe_req(cli, SAMR_OPEN_DOMAIN, &qbuf, &rbuf)) {
206 /* Unmarshall response */
208 if (!samr_io_r_open_domain("", &r, &rbuf, 0)) {
212 /* Return output parameters */
214 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
215 *domain_pol = r.domain_pol;
225 /* Open handle on a user */
227 uint32 cli_samr_open_user(struct cli_state *cli, TALLOC_CTX *mem_ctx,
228 POLICY_HND *domain_pol, uint32 access_mask,
229 uint32 user_rid, POLICY_HND *user_pol)
231 prs_struct qbuf, rbuf;
234 uint32 result = NT_STATUS_UNSUCCESSFUL;
239 /* Initialise parse structures */
241 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
242 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
244 /* Marshall data and send request */
246 init_samr_q_open_user(&q, domain_pol, access_mask, user_rid);
248 if (!samr_io_q_open_user("", &q, &qbuf, 0) ||
249 !rpc_api_pipe_req(cli, SAMR_OPEN_USER, &qbuf, &rbuf)) {
253 /* Unmarshall response */
255 if (!samr_io_r_open_user("", &r, &rbuf, 0)) {
259 /* Return output parameters */
261 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
262 *user_pol = r.user_pol;
272 /* Open handle on a group */
274 uint32 cli_samr_open_group(struct cli_state *cli, TALLOC_CTX *mem_ctx,
275 POLICY_HND *domain_pol, uint32 access_mask,
276 uint32 group_rid, POLICY_HND *group_pol)
278 prs_struct qbuf, rbuf;
281 uint32 result = NT_STATUS_UNSUCCESSFUL;
286 /* Initialise parse structures */
288 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
289 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
291 /* Marshall data and send request */
293 init_samr_q_open_group(&q, domain_pol, access_mask, group_rid);
295 if (!samr_io_q_open_group("", &q, &qbuf, 0) ||
296 !rpc_api_pipe_req(cli, SAMR_OPEN_GROUP, &qbuf, &rbuf)) {
300 /* Unmarshall response */
302 if (!samr_io_r_open_group("", &r, &rbuf, 0)) {
306 /* Return output parameters */
308 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
319 /* Query user info */
321 uint32 cli_samr_query_userinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
322 POLICY_HND *user_pol, uint16 switch_value,
323 SAM_USERINFO_CTR *ctr)
325 prs_struct qbuf, rbuf;
326 SAMR_Q_QUERY_USERINFO q;
327 SAMR_R_QUERY_USERINFO r;
328 uint32 result = NT_STATUS_UNSUCCESSFUL;
333 /* Initialise parse structures */
335 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
336 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
338 /* Marshall data and send request */
340 init_samr_q_query_userinfo(&q, user_pol, switch_value);
342 if (!samr_io_q_query_userinfo("", &q, &qbuf, 0) ||
343 !rpc_api_pipe_req(cli, SAMR_QUERY_USERINFO, &qbuf, &rbuf)) {
347 /* Unmarshall response */
351 if (!samr_io_r_query_userinfo("", &r, &rbuf, 0)) {
355 /* Return output parameters */
366 /* Query group info */
368 uint32 cli_samr_query_groupinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
369 POLICY_HND *group_pol, uint32 info_level,
372 prs_struct qbuf, rbuf;
373 SAMR_Q_QUERY_GROUPINFO q;
374 SAMR_R_QUERY_GROUPINFO r;
375 uint32 result = NT_STATUS_UNSUCCESSFUL;
380 /* Initialise parse structures */
382 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
383 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
385 /* Marshall data and send request */
387 init_samr_q_query_groupinfo(&q, group_pol, info_level);
389 if (!samr_io_q_query_groupinfo("", &q, &qbuf, 0) ||
390 !rpc_api_pipe_req(cli, SAMR_QUERY_GROUPINFO, &qbuf, &rbuf)) {
394 /* Unmarshall response */
398 if (!samr_io_r_query_groupinfo("", &r, &rbuf, 0)) {
402 /* Return output parameters */
413 /* Query user groups */
415 uint32 cli_samr_query_usergroups(struct cli_state *cli, TALLOC_CTX *mem_ctx,
416 POLICY_HND *user_pol, uint32 *num_groups,
419 prs_struct qbuf, rbuf;
420 SAMR_Q_QUERY_USERGROUPS q;
421 SAMR_R_QUERY_USERGROUPS r;
422 uint32 result = NT_STATUS_UNSUCCESSFUL;
427 /* Initialise parse structures */
429 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
430 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
432 /* Marshall data and send request */
434 init_samr_q_query_usergroups(&q, user_pol);
436 if (!samr_io_q_query_usergroups("", &q, &qbuf, 0) ||
437 !rpc_api_pipe_req(cli, SAMR_QUERY_USERGROUPS, &qbuf, &rbuf)) {
441 /* Unmarshall response */
443 if (!samr_io_r_query_usergroups("", &r, &rbuf, 0)) {
447 /* Return output parameters */
449 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
450 *num_groups = r.num_entries;
461 /* Query user groups */
463 uint32 cli_samr_query_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
464 POLICY_HND *group_pol, uint32 *num_mem,
465 uint32 **rid, uint32 **attr)
467 prs_struct qbuf, rbuf;
468 SAMR_Q_QUERY_GROUPMEM q;
469 SAMR_R_QUERY_GROUPMEM r;
470 uint32 result = NT_STATUS_UNSUCCESSFUL;
475 /* Initialise parse structures */
477 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
478 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
480 /* Marshall data and send request */
482 init_samr_q_query_groupmem(&q, group_pol);
484 if (!samr_io_q_query_groupmem("", &q, &qbuf, 0) ||
485 !rpc_api_pipe_req(cli, SAMR_QUERY_GROUPMEM, &qbuf, &rbuf)) {
489 /* Unmarshall response */
491 if (!samr_io_r_query_groupmem("", &r, &rbuf, 0)) {
495 /* Return output parameters */
497 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
498 *num_mem = r.num_entries;
510 /* Enumerate domain groups */
512 uint32 cli_samr_enum_dom_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx,
513 POLICY_HND *pol, uint32 *start_idx,
514 uint32 size, struct acct_info **dom_groups,
515 uint32 *num_dom_groups)
517 prs_struct qbuf, rbuf;
518 SAMR_Q_ENUM_DOM_GROUPS q;
519 SAMR_R_ENUM_DOM_GROUPS r;
520 uint32 result = NT_STATUS_UNSUCCESSFUL, name_idx, i;
525 /* Initialise parse structures */
527 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
528 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
530 /* Marshall data and send request */
532 init_samr_q_enum_dom_groups(&q, pol, *start_idx, size);
534 if (!samr_io_q_enum_dom_groups("", &q, &qbuf, 0) ||
535 !rpc_api_pipe_req(cli, SAMR_ENUM_DOM_GROUPS, &qbuf, &rbuf)) {
539 /* Unmarshall response */
541 if (!samr_io_r_enum_dom_groups("", &r, &rbuf, 0)) {
545 /* Return output parameters */
549 if (result != NT_STATUS_NOPROBLEMO &&
550 result != STATUS_MORE_ENTRIES) {
554 *num_dom_groups = r.num_entries2;
556 if (!((*dom_groups) = (struct acct_info *)
557 talloc(mem_ctx, sizeof(struct acct_info) * *num_dom_groups))) {
558 result = NT_STATUS_UNSUCCESSFUL;
562 memset(*dom_groups, 0, sizeof(struct acct_info) * *num_dom_groups);
566 for (i = 0; i < *num_dom_groups; i++) {
568 (*dom_groups)[i].rid = r.sam[i].rid;
570 if (r.sam[i].hdr_name.buffer) {
571 unistr2_to_ascii((*dom_groups)[i].acct_name,
572 &r.uni_grp_name[name_idx],
573 sizeof(fstring) - 1);
577 *start_idx = r.next_idx;
587 /* Query alias members */
589 uint32 cli_samr_query_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
590 POLICY_HND *alias_pol, uint32 *num_mem,
593 prs_struct qbuf, rbuf;
594 SAMR_Q_QUERY_ALIASMEM q;
595 SAMR_R_QUERY_ALIASMEM r;
596 uint32 result = NT_STATUS_UNSUCCESSFUL, i;
601 /* Initialise parse structures */
603 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
604 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
606 /* Marshall data and send request */
608 init_samr_q_query_aliasmem(&q, alias_pol);
610 if (!samr_io_q_query_aliasmem("", &q, &qbuf, 0) ||
611 !rpc_api_pipe_req(cli, SAMR_QUERY_ALIASMEM, &qbuf, &rbuf)) {
615 /* Unmarshall response */
617 if (!samr_io_r_query_aliasmem("", &r, &rbuf, 0)) {
621 /* Return output parameters */
623 if ((result = r.status) != NT_STATUS_NOPROBLEMO) {
627 *num_mem = r.num_sids;
629 if (!(*sids = talloc(mem_ctx, sizeof(DOM_SID) * *num_mem))) {
630 result = NT_STATUS_UNSUCCESSFUL;
634 for (i = 0; i < *num_mem; i++) {
635 (*sids)[i] = r.sid[i].sid;
645 /* Open handle on an alias */
647 uint32 cli_samr_open_alias(struct cli_state *cli, TALLOC_CTX *mem_ctx,
648 POLICY_HND *domain_pol, uint32 access_mask,
649 uint32 alias_rid, POLICY_HND *alias_pol)
651 prs_struct qbuf, rbuf;
659 /* Initialise parse structures */
661 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
662 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
664 /* Marshall data and send request */
666 init_samr_q_open_alias(&q, domain_pol, access_mask, alias_rid);
668 if (!samr_io_q_open_alias("", &q, &qbuf, 0) ||
669 !rpc_api_pipe_req(cli, SAMR_OPEN_ALIAS, &qbuf, &rbuf)) {
670 result = NT_STATUS_UNSUCCESSFUL;
674 /* Unmarshall response */
676 if (!samr_io_r_open_alias("", &r, &rbuf, 0)) {
677 result = NT_STATUS_UNSUCCESSFUL;
681 /* Return output parameters */
683 if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
694 /* Query domain info */
696 uint32 cli_samr_query_dom_info(struct cli_state *cli, TALLOC_CTX *mem_ctx,
697 POLICY_HND *domain_pol, uint16 switch_value,
700 prs_struct qbuf, rbuf;
701 SAMR_Q_QUERY_DOMAIN_INFO q;
702 SAMR_R_QUERY_DOMAIN_INFO r;
703 uint32 result = NT_STATUS_UNSUCCESSFUL;
708 /* Initialise parse structures */
710 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
711 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
713 /* Marshall data and send request */
715 init_samr_q_query_dom_info(&q, domain_pol, switch_value);
717 if (!samr_io_q_query_dom_info("", &q, &qbuf, 0) ||
718 !rpc_api_pipe_req(cli, SAMR_QUERY_DOMAIN_INFO, &qbuf, &rbuf)) {
722 /* Unmarshall response */
726 if (!samr_io_r_query_dom_info("", &r, &rbuf, 0)) {
730 /* Return output parameters */
732 if ((result = r.status) != NT_STATUS_NOPROBLEMO) {
743 /* Query display info */
745 uint32 cli_samr_query_dispinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
746 POLICY_HND *domain_pol, uint32 *start_idx,
747 uint16 switch_value, uint32 *num_entries,
748 uint32 max_entries, SAM_DISPINFO_CTR *ctr)
750 prs_struct qbuf, rbuf;
751 SAMR_Q_QUERY_DISPINFO q;
752 SAMR_R_QUERY_DISPINFO r;
753 uint32 result = NT_STATUS_UNSUCCESSFUL;
758 /* Initialise parse structures */
760 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
761 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
763 /* Marshall data and send request */
765 init_samr_q_query_dispinfo(&q, domain_pol, switch_value,
766 *start_idx, max_entries);
768 if (!samr_io_q_query_dispinfo("", &q, &qbuf, 0) ||
769 !rpc_api_pipe_req(cli, SAMR_QUERY_DISPINFO, &qbuf, &rbuf)) {
773 /* Unmarshall response */
777 if (!samr_io_r_query_dispinfo("", &r, &rbuf, 0)) {
781 /* Return output parameters */
783 if ((result = r.status) != NT_STATUS_NOPROBLEMO) {
787 *num_entries = r.num_entries;
788 *start_idx += r.num_entries; /* No next_idx in this structure! */
799 uint32 cli_samr_lookup_rids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
800 POLICY_HND *domain_pol, uint32 flags,
801 uint32 num_rids, uint32 *rids,
802 uint32 *num_names, char ***names,
805 prs_struct qbuf, rbuf;
806 SAMR_Q_LOOKUP_RIDS q;
807 SAMR_R_LOOKUP_RIDS r;
808 uint32 result = NT_STATUS_UNSUCCESSFUL, i;
813 /* Initialise parse structures */
815 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
816 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
818 /* Marshall data and send request */
820 init_samr_q_lookup_rids(mem_ctx, &q, domain_pol, flags,
823 if (!samr_io_q_lookup_rids("", &q, &qbuf, 0) ||
824 !rpc_api_pipe_req(cli, SAMR_LOOKUP_RIDS, &qbuf, &rbuf)) {
828 /* Unmarshall response */
830 if (!samr_io_r_lookup_rids("", &r, &rbuf, 0)) {
834 /* Return output parameters */
836 if ((result = r.status) != NT_STATUS_NOPROBLEMO) {
840 if (r.num_names1 == 0) {
846 *num_names = r.num_names1;
847 *names = talloc(mem_ctx, sizeof(char *) * r.num_names1);
848 *name_types = talloc(mem_ctx, sizeof(uint32) * r.num_names1);
850 for (i = 0; i < r.num_names1; i++) {
853 unistr2_to_ascii(tmp, &r.uni_name[i], sizeof(tmp) - 1);
854 (*names)[i] = talloc_strdup(mem_ctx, tmp);
855 (*name_types)[i] = r.type[i];