/* Unix SMB/Netbios implementation. Version 2.2 RPC pipe client Copyright (C) Tim Potter 2000-2001, Copyright (C) Andrew Tridgell 1992-1997,2000, Copyright (C) Luke Kenneth Casson Leighton 1996-1997,2000, Copyright (C) Paul Ashton 1997,2000, Copyright (C) Elrond 2000. 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 2 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, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "includes.h" /* Opens a SMB connection to the SAMR pipe */ struct cli_state *cli_samr_initialise(struct cli_state *cli, char *system_name, struct ntuser_creds *creds) { struct in_addr dest_ip; struct nmb_name calling, called; fstring dest_host; extern pstring global_myname; struct ntuser_creds anon; /* Initialise cli_state information */ if (!cli_initialise(cli)) { return NULL; } if (!creds) { ZERO_STRUCT(anon); anon.pwd.null_pwd = 1; creds = &anon; } cli_init_creds(cli, creds); /* Establish a SMB connection */ if (!resolve_srv_name(system_name, dest_host, &dest_ip)) { return NULL; } make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20); make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0); if (!cli_establish_connection(cli, dest_host, &dest_ip, &calling, &called, "IPC$", "IPC", False, True)) { return NULL; } /* Open a NT session thingy */ if (!cli_nt_session_open(cli, PIPE_SAMR)) { cli_shutdown(cli); return NULL; } return cli; } /* Shut down a SMB connection to the SAMR pipe */ void cli_samr_shutdown(struct cli_state *cli) { if (cli->fd != -1) cli_ulogoff(cli); cli_shutdown(cli); } /* Connect to SAMR database */ uint32 cli_samr_connect(struct cli_state *cli, TALLOC_CTX *mem_ctx, char *srv_name, uint32 access_mask, POLICY_HND *connect_pol) { prs_struct qbuf, rbuf; SAMR_Q_CONNECT q; SAMR_R_CONNECT r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_connect(&q, srv_name, access_mask); if (!samr_io_q_connect("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_CONNECT, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ if (!samr_io_r_connect("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) == NT_STATUS_NOPROBLEMO) { *connect_pol = r.connect_pol; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Close SAMR handle */ uint32 cli_samr_close(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *connect_pol) { prs_struct qbuf, rbuf; SAMR_Q_CLOSE_HND q; SAMR_R_CLOSE_HND r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_close_hnd(&q, connect_pol); if (!samr_io_q_close_hnd("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_CLOSE_HND, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ if (!samr_io_r_close_hnd("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) == NT_STATUS_NOPROBLEMO) { *connect_pol = r.pol; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Open handle on a domain */ uint32 cli_samr_open_domain(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *connect_pol, uint32 access_mask, DOM_SID *domain_sid, POLICY_HND *domain_pol) { prs_struct qbuf, rbuf; SAMR_Q_OPEN_DOMAIN q; SAMR_R_OPEN_DOMAIN r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_open_domain(&q, connect_pol, access_mask, domain_sid); if (!samr_io_q_open_domain("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_OPEN_DOMAIN, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ if (!samr_io_r_open_domain("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) == NT_STATUS_NOPROBLEMO) { *domain_pol = r.domain_pol; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Open handle on a user */ uint32 cli_samr_open_user(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *domain_pol, uint32 access_mask, uint32 user_rid, POLICY_HND *user_pol) { prs_struct qbuf, rbuf; SAMR_Q_OPEN_USER q; SAMR_R_OPEN_USER r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_open_user(&q, domain_pol, access_mask, user_rid); if (!samr_io_q_open_user("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_OPEN_USER, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ if (!samr_io_r_open_user("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) == NT_STATUS_NOPROBLEMO) { *user_pol = r.user_pol; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Open handle on a group */ uint32 cli_samr_open_group(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *domain_pol, uint32 access_mask, uint32 group_rid, POLICY_HND *group_pol) { prs_struct qbuf, rbuf; SAMR_Q_OPEN_GROUP q; SAMR_R_OPEN_GROUP r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_open_group(&q, domain_pol, access_mask, group_rid); if (!samr_io_q_open_group("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_OPEN_GROUP, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ if (!samr_io_r_open_group("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) == NT_STATUS_NOPROBLEMO) { *group_pol = r.pol; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Query user info */ uint32 cli_samr_query_userinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *user_pol, uint16 switch_value, SAM_USERINFO_CTR *ctr) { prs_struct qbuf, rbuf; SAMR_Q_QUERY_USERINFO q; SAMR_R_QUERY_USERINFO r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_query_userinfo(&q, user_pol, switch_value); if (!samr_io_q_query_userinfo("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_QUERY_USERINFO, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ r.ctr = ctr; if (!samr_io_r_query_userinfo("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ result = r.status; done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Query group info */ uint32 cli_samr_query_groupinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *group_pol, uint32 info_level, GROUP_INFO_CTR *ctr) { prs_struct qbuf, rbuf; SAMR_Q_QUERY_GROUPINFO q; SAMR_R_QUERY_GROUPINFO r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_query_groupinfo(&q, group_pol, info_level); if (!samr_io_q_query_groupinfo("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_QUERY_GROUPINFO, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ r.ctr = ctr; if (!samr_io_r_query_groupinfo("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ result = r.status; done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Query user groups */ uint32 cli_samr_query_usergroups(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *user_pol, uint32 *num_groups, DOM_GID **gid) { prs_struct qbuf, rbuf; SAMR_Q_QUERY_USERGROUPS q; SAMR_R_QUERY_USERGROUPS r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_query_usergroups(&q, user_pol); if (!samr_io_q_query_usergroups("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_QUERY_USERGROUPS, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ if (!samr_io_r_query_usergroups("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) == NT_STATUS_NOPROBLEMO) { *num_groups = r.num_entries; *gid = r.gid; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Query user groups */ uint32 cli_samr_query_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *group_pol, uint32 *num_mem, uint32 **rid, uint32 **attr) { prs_struct qbuf, rbuf; SAMR_Q_QUERY_GROUPMEM q; SAMR_R_QUERY_GROUPMEM r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_query_groupmem(&q, group_pol); if (!samr_io_q_query_groupmem("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_QUERY_GROUPMEM, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ if (!samr_io_r_query_groupmem("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) == NT_STATUS_NOPROBLEMO) { *num_mem = r.num_entries; *rid = r.rid; *attr = r.attr; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Enumerate domain groups */ uint32 cli_samr_enum_dom_groups(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *pol, uint32 *start_idx, uint32 size, struct acct_info **dom_groups, uint32 *num_dom_groups) { prs_struct qbuf, rbuf; SAMR_Q_ENUM_DOM_GROUPS q; SAMR_R_ENUM_DOM_GROUPS r; uint32 result = NT_STATUS_UNSUCCESSFUL, name_idx, i; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_enum_dom_groups(&q, pol, *start_idx, size); if (!samr_io_q_enum_dom_groups("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_ENUM_DOM_GROUPS, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ if (!samr_io_r_enum_dom_groups("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ result = r.status; if (result != NT_STATUS_NOPROBLEMO && result != STATUS_MORE_ENTRIES) { goto done; } *num_dom_groups = r.num_entries2; if (!((*dom_groups) = (struct acct_info *) talloc(mem_ctx, sizeof(struct acct_info) * *num_dom_groups))) { result = NT_STATUS_UNSUCCESSFUL; goto done; } memset(*dom_groups, 0, sizeof(struct acct_info) * *num_dom_groups); name_idx = 0; for (i = 0; i < *num_dom_groups; i++) { (*dom_groups)[i].rid = r.sam[i].rid; if (r.sam[i].hdr_name.buffer) { unistr2_to_ascii((*dom_groups)[i].acct_name, &r.uni_grp_name[name_idx], sizeof(fstring) - 1); name_idx++; } *start_idx = r.next_idx; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Query alias members */ uint32 cli_samr_query_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *alias_pol, uint32 *num_mem, DOM_SID **sids) { prs_struct qbuf, rbuf; SAMR_Q_QUERY_ALIASMEM q; SAMR_R_QUERY_ALIASMEM r; uint32 result = NT_STATUS_UNSUCCESSFUL, i; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_query_aliasmem(&q, alias_pol); if (!samr_io_q_query_aliasmem("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_QUERY_ALIASMEM, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ if (!samr_io_r_query_aliasmem("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) != NT_STATUS_NOPROBLEMO) { goto done; } *num_mem = r.num_sids; if (!(*sids = talloc(mem_ctx, sizeof(DOM_SID) * *num_mem))) { result = NT_STATUS_UNSUCCESSFUL; goto done; } for (i = 0; i < *num_mem; i++) { (*sids)[i] = r.sid[i].sid; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Open handle on an alias */ uint32 cli_samr_open_alias(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *domain_pol, uint32 access_mask, uint32 alias_rid, POLICY_HND *alias_pol) { prs_struct qbuf, rbuf; SAMR_Q_OPEN_ALIAS q; SAMR_R_OPEN_ALIAS r; uint32 result; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_open_alias(&q, domain_pol, access_mask, alias_rid); if (!samr_io_q_open_alias("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_OPEN_ALIAS, &qbuf, &rbuf)) { result = NT_STATUS_UNSUCCESSFUL; goto done; } /* Unmarshall response */ if (!samr_io_r_open_alias("", &r, &rbuf, 0)) { result = NT_STATUS_UNSUCCESSFUL; goto done; } /* Return output parameters */ if ((result = r.status) == NT_STATUS_NOPROBLEMO) { *alias_pol = r.pol; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Query domain info */ uint32 cli_samr_query_dom_info(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *domain_pol, uint16 switch_value, SAM_UNK_CTR *ctr) { prs_struct qbuf, rbuf; SAMR_Q_QUERY_DOMAIN_INFO q; SAMR_R_QUERY_DOMAIN_INFO r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_query_dom_info(&q, domain_pol, switch_value); if (!samr_io_q_query_dom_info("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_QUERY_DOMAIN_INFO, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ r.ctr = ctr; if (!samr_io_r_query_dom_info("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) != NT_STATUS_NOPROBLEMO) { goto done; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Query display info */ uint32 cli_samr_query_dispinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *domain_pol, uint32 *start_idx, uint16 switch_value, uint32 *num_entries, uint32 max_entries, SAM_DISPINFO_CTR *ctr) { prs_struct qbuf, rbuf; SAMR_Q_QUERY_DISPINFO q; SAMR_R_QUERY_DISPINFO r; uint32 result = NT_STATUS_UNSUCCESSFUL; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_query_dispinfo(&q, domain_pol, switch_value, *start_idx, max_entries); if (!samr_io_q_query_dispinfo("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_QUERY_DISPINFO, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ r.ctr = ctr; if (!samr_io_r_query_dispinfo("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) != NT_STATUS_NOPROBLEMO) { goto done; } *num_entries = r.num_entries; *start_idx += r.num_entries; /* No next_idx in this structure! */ done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; } /* Lookup rids */ uint32 cli_samr_lookup_rids(struct cli_state *cli, TALLOC_CTX *mem_ctx, POLICY_HND *domain_pol, uint32 flags, uint32 num_rids, uint32 *rids, uint32 *num_names, char ***names, uint32 **name_types) { prs_struct qbuf, rbuf; SAMR_Q_LOOKUP_RIDS q; SAMR_R_LOOKUP_RIDS r; uint32 result = NT_STATUS_UNSUCCESSFUL, i; ZERO_STRUCT(q); ZERO_STRUCT(r); /* Initialise parse structures */ prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL); prs_init(&rbuf, 0, mem_ctx, UNMARSHALL); /* Marshall data and send request */ init_samr_q_lookup_rids(mem_ctx, &q, domain_pol, flags, num_rids, rids); if (!samr_io_q_lookup_rids("", &q, &qbuf, 0) || !rpc_api_pipe_req(cli, SAMR_LOOKUP_RIDS, &qbuf, &rbuf)) { goto done; } /* Unmarshall response */ if (!samr_io_r_lookup_rids("", &r, &rbuf, 0)) { goto done; } /* Return output parameters */ if ((result = r.status) != NT_STATUS_NOPROBLEMO) { goto done; } if (r.num_names1 == 0) { *num_names = 0; *names = NULL; goto done; } *num_names = r.num_names1; *names = talloc(mem_ctx, sizeof(char *) * r.num_names1); *name_types = talloc(mem_ctx, sizeof(uint32) * r.num_names1); for (i = 0; i < r.num_names1; i++) { fstring tmp; unistr2_to_ascii(tmp, &r.uni_name[i], sizeof(tmp) - 1); (*names)[i] = strdup(tmp); (*name_types)[i] = r.type[i]; } done: prs_mem_free(&qbuf); prs_mem_free(&rbuf); return result; }