diff options
Diffstat (limited to 'source3/rpcclient')
-rw-r--r-- | source3/rpcclient/cmd_dfs.c | 237 | ||||
-rw-r--r-- | source3/rpcclient/cmd_lsarpc.c | 509 | ||||
-rw-r--r-- | source3/rpcclient/cmd_netlogon.c | 334 | ||||
-rw-r--r-- | source3/rpcclient/cmd_reg.c | 1010 | ||||
-rw-r--r-- | source3/rpcclient/cmd_samr.c | 1288 | ||||
-rw-r--r-- | source3/rpcclient/cmd_spoolss.c | 1710 | ||||
-rw-r--r-- | source3/rpcclient/cmd_srvsvc.c | 233 | ||||
-rw-r--r-- | source3/rpcclient/cmd_wkssvc.c | 84 | ||||
-rw-r--r-- | source3/rpcclient/display.c | 1338 | ||||
-rw-r--r-- | source3/rpcclient/display_sec.c | 144 | ||||
-rw-r--r-- | source3/rpcclient/display_spool.c | 927 | ||||
-rw-r--r-- | source3/rpcclient/rpcclient.c | 811 | ||||
-rw-r--r-- | source3/rpcclient/rpcclient.h | 34 | ||||
-rw-r--r-- | source3/rpcclient/samsync.c | 619 |
14 files changed, 9278 insertions, 0 deletions
diff --git a/source3/rpcclient/cmd_dfs.c b/source3/rpcclient/cmd_dfs.c new file mode 100644 index 0000000000..8a3c3e9db3 --- /dev/null +++ b/source3/rpcclient/cmd_dfs.c @@ -0,0 +1,237 @@ +/* + Unix SMB/CIFS implementation. + RPC pipe client + + Copyright (C) Tim Potter 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" +#include "rpcclient.h" + +/* Check DFS is supported by the remote server */ + +static NTSTATUS cmd_dfs_exist(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + BOOL dfs_exists; + NTSTATUS result; + + if (argc != 1) { + printf("Usage: %s\n", argv[0]); + return NT_STATUS_OK; + } + + result = cli_dfs_exist(cli, mem_ctx, &dfs_exists); + + if (NT_STATUS_IS_OK(result)) + printf("dfs is %spresent\n", dfs_exists ? "" : "not "); + + return result; +} + +static NTSTATUS cmd_dfs_add(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + NTSTATUS result; + char *entrypath, *servername, *sharename, *comment; + uint32 flags = 0; + + if (argc != 5) { + printf("Usage: %s entrypath servername sharename comment\n", + argv[0]); + return NT_STATUS_OK; + } + + entrypath = argv[1]; + servername = argv[2]; + sharename = argv[3]; + comment = argv[4]; + + result = cli_dfs_add(cli, mem_ctx, entrypath, servername, + sharename, comment, flags); + + return result; +} + +static NTSTATUS cmd_dfs_remove(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + NTSTATUS result; + char *entrypath, *servername, *sharename; + + if (argc != 4) { + printf("Usage: %s entrypath servername sharename\n", argv[0]); + return NT_STATUS_OK; + } + + entrypath = argv[1]; + servername = argv[2]; + sharename = argv[3]; + + result = cli_dfs_remove(cli, mem_ctx, entrypath, servername, + sharename); + + return result; +} + +/* Display a DFS_INFO_1 structure */ + +static void display_dfs_info_1(DFS_INFO_1 *info1) +{ + fstring temp; + + unistr2_to_ascii(temp, &info1->entrypath, sizeof(temp) - 1); + printf("entrypath: %s\n", temp); +} + +/* Display a DFS_INFO_2 structure */ + +static void display_dfs_info_2(DFS_INFO_2 *info2) +{ + fstring temp; + + unistr2_to_ascii(temp, &info2->entrypath, sizeof(temp) - 1); + printf("entrypath: %s\n", temp); + + unistr2_to_ascii(temp, &info2->comment, sizeof(temp) - 1); + printf("\tcomment: %s\n", temp); + + printf("\tstate: %d\n", info2->state); + printf("\tnum_storages: %d\n", info2->num_storages); +} + +/* Display a DFS_INFO_3 structure */ + +static void display_dfs_info_3(DFS_INFO_3 *info3) +{ + fstring temp; + int i; + + unistr2_to_ascii(temp, &info3->entrypath, sizeof(temp) - 1); + printf("entrypath: %s\n", temp); + + unistr2_to_ascii(temp, &info3->comment, sizeof(temp) - 1); + printf("\tcomment: %s\n", temp); + + printf("\tstate: %d\n", info3->state); + printf("\tnum_storages: %d\n", info3->num_storages); + + for (i = 0; i < info3->num_storages; i++) { + DFS_STORAGE_INFO *dsi = &info3->storages[i]; + + unistr2_to_ascii(temp, &dsi->servername, sizeof(temp) - 1); + printf("\t\tstorage[%d] servername: %s\n", i, temp); + + unistr2_to_ascii(temp, &dsi->sharename, sizeof(temp) - 1); + printf("\t\tstorage[%d] sharename: %s\n", i, temp); + } +} + +/* Display a DFS_INFO_CTR structure */ + +static void display_dfs_info_ctr(DFS_INFO_CTR *ctr) +{ + int i; + + for (i = 0; i < ctr->num_entries; i++) { + switch (ctr->switch_value) { + case 0x01: + display_dfs_info_1(&ctr->dfs.info1[i]); + break; + case 0x02: + display_dfs_info_2(&ctr->dfs.info2[i]); + break; + case 0x03: + display_dfs_info_3(&ctr->dfs.info3[i]); + break; + default: + printf("unsupported info level %d\n", + ctr->switch_value); + break; + } + } +} + +/* Enumerate dfs shares */ + +static NTSTATUS cmd_dfs_enum(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + DFS_INFO_CTR ctr; + NTSTATUS result; + uint32 info_level = 1; + + if (argc > 2) { + printf("Usage: %s [info_level]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc == 2) + info_level = atoi(argv[1]); + + result = cli_dfs_enum(cli, mem_ctx, info_level, &ctr); + + if (NT_STATUS_IS_OK(result)) + display_dfs_info_ctr(&ctr); + + return result; +} + +static NTSTATUS cmd_dfs_getinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + NTSTATUS result; + char *entrypath, *servername, *sharename; + uint32 info_level = 1; + DFS_INFO_CTR ctr; + + if (argc < 4 || argc > 5) { + printf("Usage: %s entrypath servername sharename " + "[info_level]\n", argv[0]); + return NT_STATUS_OK; + } + + entrypath = argv[1]; + servername = argv[2]; + sharename = argv[3]; + + if (argc == 5) + info_level = atoi(argv[4]); + + result = cli_dfs_get_info(cli, mem_ctx, entrypath, servername, + sharename, info_level, &ctr); + + if (NT_STATUS_IS_OK(result)) + display_dfs_info_ctr(&ctr); + + return result; +} + +/* List of commands exported by this module */ + +struct cmd_set dfs_commands[] = { + + { "DFS" }, + + { "dfsexist", cmd_dfs_exist, PIPE_NETDFS, "Query DFS support", "" }, + { "dfsadd", cmd_dfs_add, PIPE_NETDFS, "Add a DFS share", "" }, + { "dfsremove", cmd_dfs_remove, PIPE_NETDFS, "Remove a DFS share", "" }, + { "dfsgetinfo", cmd_dfs_getinfo, PIPE_NETDFS, "Query DFS share info", "" }, + { "dfsenum", cmd_dfs_enum, PIPE_NETDFS, "Enumerate dfs shares", "" }, + + { NULL } +}; diff --git a/source3/rpcclient/cmd_lsarpc.c b/source3/rpcclient/cmd_lsarpc.c new file mode 100644 index 0000000000..99f1fbc3ce --- /dev/null +++ b/source3/rpcclient/cmd_lsarpc.c @@ -0,0 +1,509 @@ +/* + Unix SMB/CIFS implementation. + RPC pipe client + + Copyright (C) Tim Potter 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" +#include "rpcclient.h" + +/* Look up domain related information on a remote host */ + +static NTSTATUS cmd_lsa_query_info_policy(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + DOM_SID dom_sid; + fstring sid_str, domain_name; + uint32 info_class = 3; + + if (argc > 2) { + printf("Usage: %s [info_class]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc == 2) + info_class = atoi(argv[1]); + + result = cli_lsa_open_policy(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Lookup info policy */ + + result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class, + domain_name, &dom_sid); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + sid_to_string(sid_str, &dom_sid); + + if (domain_name[0]) + printf("domain %s has sid %s\n", domain_name, sid_str); + else + printf("could not query info for level %d\n", info_class); + + done: + return result; +} + +/* Resolve a list of names to a list of sids */ + +static NTSTATUS cmd_lsa_lookup_names(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + DOM_SID *sids; + uint32 *types; + int num_names, i; + + if (argc == 1) { + printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]); + return NT_STATUS_OK; + } + + result = cli_lsa_open_policy(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1, + (const char**)(argv + 1), &sids, + &types, &num_names); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Print results */ + + for (i = 0; i < num_names; i++) { + fstring sid_str; + + sid_to_string(sid_str, &sids[i]); + printf("%s %s (%d)\n", argv[i + 1], sid_str, + types[i]); + } + + done: + return result; +} + +/* Resolve a list of SIDs to a list of names */ + +static NTSTATUS cmd_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + DOM_SID *sids; + char **domains; + char **names; + uint32 *types; + int num_names, i; + + if (argc == 1) { + printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]); + return NT_STATUS_OK; + } + + result = cli_lsa_open_policy(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Convert arguments to sids */ + + sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * (argc - 1)); + + if (!sids) { + printf("could not allocate memory for %d sids\n", argc - 1); + goto done; + } + + for (i = 0; i < argc - 1; i++) + string_to_sid(&sids[i], argv[i + 1]); + + /* Lookup the SIDs */ + + result = cli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids, + &domains, &names, &types, &num_names); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Print results */ + + for (i = 0; i < num_names; i++) { + fstring sid_str; + + sid_to_string(sid_str, &sids[i]); + printf("%s [%s]\\[%s] (%d)\n", sid_str, + domains[i] ? domains[i] : "*unknown*", + names[i] ? names[i] : "*unknown*", types[i]); + } + + done: + return result; +} + +/* Enumerate list of trusted domains */ + +static NTSTATUS cmd_lsa_enum_trust_dom(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + DOM_SID *domain_sids; + char **domain_names; + uint32 enum_ctx = 0; + uint32 num_domains; + int i; + + if (argc != 1) { + printf("Usage: %s\n", argv[0]); + return NT_STATUS_OK; + } + + result = cli_lsa_open_policy(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Lookup list of trusted domains */ + + result = cli_lsa_enum_trust_dom(cli, mem_ctx, &pol, &enum_ctx, + &num_domains, &domain_names, + &domain_sids); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Print results */ + + for (i = 0; i < num_domains; i++) { + fstring sid_str; + + sid_to_string(sid_str, &domain_sids[i]); + printf("%s %s\n", domain_names[i] ? domain_names[i] : + "*unknown*", sid_str); + } + + done: + return result; +} + +/* Enumerates privileges */ + +static NTSTATUS cmd_lsa_enum_privilege(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + + uint32 enum_context=0; + uint32 pref_max_length=0x1000; + uint32 count=0; + char **privs_name; + uint32 *privs_high; + uint32 *privs_low; + int i; + + if (argc > 3) { + printf("Usage: %s [enum context] [max length]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc>=2) + enum_context=atoi(argv[1]); + + if (argc==3) + pref_max_length=atoi(argv[2]); + + result = cli_lsa_open_policy(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_lsa_enum_privilege(cli, mem_ctx, &pol, &enum_context, pref_max_length, + &count, &privs_name, &privs_high, &privs_low); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Print results */ + printf("found %d privileges\n\n", count); + + for (i = 0; i < count; i++) { + printf("%s \t\t%d:%d (0x%x:0x%x)\n", privs_name[i] ? privs_name[i] : "*unknown*", + privs_high[i], privs_low[i], privs_high[i], privs_low[i]); + } + + done: + return result; +} + +/* Get privilege name */ + +static NTSTATUS cmd_lsa_get_dispname(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + + uint16 lang_id=0; + uint16 lang_id_sys=0; + uint16 lang_id_desc; + fstring description; + + if (argc != 2) { + printf("Usage: %s privilege name\n", argv[0]); + return NT_STATUS_OK; + } + + result = cli_lsa_open_policy(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_lsa_get_dispname(cli, mem_ctx, &pol, argv[1], lang_id, lang_id_sys, description, &lang_id_desc); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Print results */ + printf("%s -> %s (language: 0x%x)\n", argv[1], description, lang_id_desc); + + done: + return result; +} + +/* Enumerate the LSA SIDS */ + +static NTSTATUS cmd_lsa_enum_sids(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + + uint32 enum_context=0; + uint32 pref_max_length=0x1000; + DOM_SID *sids; + uint32 count=0; + int i; + + if (argc > 3) { + printf("Usage: %s [enum context] [max length]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc>=2) + enum_context=atoi(argv[1]); + + if (argc==3) + pref_max_length=atoi(argv[2]); + + result = cli_lsa_open_policy(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_lsa_enum_sids(cli, mem_ctx, &pol, &enum_context, pref_max_length, + &count, &sids); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Print results */ + printf("found %d SIDs\n\n", count); + + for (i = 0; i < count; i++) { + fstring sid_str; + + sid_to_string(sid_str, &sids[i]); + printf("%s\n", sid_str); + } + + done: + return result; +} + +/* Enumerate the privileges of an SID */ + +static NTSTATUS cmd_lsa_enum_privsaccounts(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND dom_pol; + POLICY_HND user_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 access_desired = 0x000f000f; + + DOM_SID sid; + uint32 count=0; + LUID_ATTR *set; + int i; + + if (argc != 2 ) { + printf("Usage: %s SID\n", argv[0]); + return NT_STATUS_OK; + } + + string_to_sid(&sid, argv[1]); + + result = cli_lsa_open_policy2(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &dom_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_lsa_open_account(cli, mem_ctx, &dom_pol, &sid, access_desired, &user_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_lsa_enum_privsaccount(cli, mem_ctx, &user_pol, &count, &set); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Print results */ + printf("found %d privileges for SID %s\n\n", count, argv[1]); + printf("high\tlow\tattribute\n"); + + for (i = 0; i < count; i++) { + printf("%u\t%u\t%u\n", set[i].luid.high, set[i].luid.low, set[i].attr); + } + + done: + return result; +} + +/* Get a privilege value given its name */ + +static NTSTATUS cmd_lsa_lookupprivvalue(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + LUID luid; + + if (argc != 2 ) { + printf("Usage: %s name\n", argv[0]); + return NT_STATUS_OK; + } + + result = cli_lsa_open_policy2(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_lsa_lookupprivvalue(cli, mem_ctx, &pol, argv[1], &luid); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Print results */ + printf("%u:%u (0x%x:0x%x)\n", luid.high, luid.low, luid.high, luid.low); + + done: + return result; +} + +/* Query LSA security object */ + +static NTSTATUS cmd_lsa_query_secobj(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + SEC_DESC_BUF *sdb; + uint32 sec_info = 0x00000004; /* ??? */ + + if (argc != 1 ) { + printf("Usage: %s\n", argv[0]); + return NT_STATUS_OK; + } + + result = cli_lsa_open_policy2(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_lsa_query_secobj(cli, mem_ctx, &pol, sec_info, &sdb); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Print results */ + + display_sec_desc(sdb->sec); + + done: + return result; +} + +/* List of commands exported by this module */ + +struct cmd_set lsarpc_commands[] = { + + { "LSARPC" }, + + { "lsaquery", cmd_lsa_query_info_policy, PIPE_LSARPC, "Query info policy", "" }, + { "lookupsids", cmd_lsa_lookup_sids, PIPE_LSARPC, "Convert SIDs to names", "" }, + { "lookupnames", cmd_lsa_lookup_names, PIPE_LSARPC, "Convert names to SIDs", "" }, + { "enumtrust", cmd_lsa_enum_trust_dom, PIPE_LSARPC, "Enumerate trusted domains", "" }, + { "enumprivs", cmd_lsa_enum_privilege, PIPE_LSARPC, "Enumerate privileges", "" }, + { "getdispname", cmd_lsa_get_dispname, PIPE_LSARPC, "Get the privilege name", "" }, + { "lsaenumsid", cmd_lsa_enum_sids, PIPE_LSARPC, "Enumerate the LSA SIDS", "" }, + { "lsaenumprivsaccount", cmd_lsa_enum_privsaccounts, PIPE_LSARPC, "Enumerate the privileges of an SID", "" }, + { "lsalookupprivvalue", cmd_lsa_lookupprivvalue, PIPE_LSARPC, "Get a privilege value given its name", "" }, + { "lsaquerysecobj", cmd_lsa_query_secobj, PIPE_LSARPC, "Query LSA security object", "" }, + + { NULL } +}; diff --git a/source3/rpcclient/cmd_netlogon.c b/source3/rpcclient/cmd_netlogon.c new file mode 100644 index 0000000000..4d67cba1b5 --- /dev/null +++ b/source3/rpcclient/cmd_netlogon.c @@ -0,0 +1,334 @@ +/* + Unix SMB/CIFS implementation. + RPC pipe client + + Copyright (C) Tim Potter 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" +#include "rpcclient.h" + +static NTSTATUS cmd_netlogon_logon_ctrl2(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + uint32 query_level = 1; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + + if (argc > 1) { + fprintf(stderr, "Usage: %s\n", argv[0]); + return NT_STATUS_OK; + } + + result = cli_netlogon_logon_ctrl2(cli, mem_ctx, query_level); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Display results */ + + done: + return result; +} + +static NTSTATUS cmd_netlogon_logon_ctrl(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ +#if 0 + uint32 query_level = 1; +#endif + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + + if (argc > 1) { + fprintf(stderr, "Usage: %s\n", argv[0]); + return NT_STATUS_OK; + } + +#if 0 + result = cli_netlogon_logon_ctrl(cli, mem_ctx, query_level); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } +#endif + + /* Display results */ + + return result; +} + +/* Display sam synchronisation information */ + +static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas, + SAM_DELTA_CTR *deltas) +{ + fstring name; + uint32 i, j; + + for (i = 0; i < num_deltas; i++) { + switch (hdr_deltas[i].type) { + case SAM_DELTA_DOMAIN_INFO: + unistr2_to_ascii(name, + &deltas[i].domain_info.uni_dom_name, + sizeof(name) - 1); + printf("Domain: %s\n", name); + break; + case SAM_DELTA_GROUP_INFO: + unistr2_to_ascii(name, + &deltas[i].group_info.uni_grp_name, + sizeof(name) - 1); + printf("Group: %s\n", name); + break; + case SAM_DELTA_ACCOUNT_INFO: + unistr2_to_ascii(name, + &deltas[i].account_info.uni_acct_name, + sizeof(name) - 1); + printf("Account: %s\n", name); + break; + case SAM_DELTA_ALIAS_INFO: + unistr2_to_ascii(name, + &deltas[i].alias_info.uni_als_name, + sizeof(name) - 1); + printf("Alias: %s\n", name); + break; + case SAM_DELTA_ALIAS_MEM: { + SAM_ALIAS_MEM_INFO *alias = &deltas[i].als_mem_info; + + for (j = 0; j < alias->num_members; j++) { + fstring sid_str; + + sid_to_string(sid_str, &alias->sids[j].sid); + + printf("%s\n", sid_str); + } + break; + } + case SAM_DELTA_GROUP_MEM: { + SAM_GROUP_MEM_INFO *group = &deltas[i].grp_mem_info; + + for (j = 0; j < group->num_members; j++) + printf("rid 0x%x, attrib 0x%08x\n", + group->rids[j], group->attribs[j]); + break; + } + case SAM_DELTA_SAM_STAMP: { + SAM_DELTA_STAMP *stamp = &deltas[i].stamp; + + printf("sam sequence update: 0x%04x\n", + stamp->seqnum); + break; + } + default: + printf("unknown delta type 0x%02x\n", + hdr_deltas[i].type); + break; + } + } +} + +/* Perform sam synchronisation */ + +static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + unsigned char trust_passwd[16]; + uint32 database_id = 0, num_deltas; + SAM_DELTA_HDR *hdr_deltas; + SAM_DELTA_CTR *deltas; + DOM_CRED ret_creds; + + if (argc > 2) { + fprintf(stderr, "Usage: %s [database_id]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc == 2) + database_id = atoi(argv[1]); + + if (!secrets_init()) { + fprintf(stderr, "Unable to initialise secrets database\n"); + return result; + } + + /* Initialise session credentials */ + + if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd, + NULL)) { + fprintf(stderr, "could not fetch trust account password\n"); + goto done; + } + + result = cli_nt_setup_creds(cli, trust_passwd); + + if (!NT_STATUS_IS_OK(result)) { + fprintf(stderr, "Error initialising session creds\n"); + goto done; + } + + /* on first call the returnAuthenticator is empty */ + memset(&ret_creds, 0, sizeof(ret_creds)); + + /* Synchronise sam database */ + + result = cli_netlogon_sam_sync(cli, mem_ctx, &ret_creds, database_id, + &num_deltas, &hdr_deltas, &deltas); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Display results */ + + display_sam_sync(num_deltas, hdr_deltas, deltas); + + done: + return result; +} + +/* Perform sam delta synchronisation */ + +static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + unsigned char trust_passwd[16]; + uint32 database_id, num_deltas, tmp; + SAM_DELTA_HDR *hdr_deltas; + SAM_DELTA_CTR *deltas; + UINT64_S seqnum; + + if (argc != 3) { + fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]); + return NT_STATUS_OK; + } + + database_id = atoi(argv[1]); + tmp = atoi(argv[2]); + + seqnum.low = tmp & 0xffff; + seqnum.high = 0; + + if (!secrets_init()) { + fprintf(stderr, "Unable to initialise secrets database\n"); + goto done; + } + + /* Initialise session credentials */ + + if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd, + NULL)) { + fprintf(stderr, "could not fetch trust account password\n"); + goto done; + } + + result = cli_nt_setup_creds(cli, trust_passwd); + + if (!NT_STATUS_IS_OK(result)) { + fprintf(stderr, "Error initialising session creds\n"); + goto done; + } + + /* Synchronise sam database */ + + result = cli_netlogon_sam_deltas(cli, mem_ctx, database_id, + seqnum, &num_deltas, + &hdr_deltas, &deltas); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Display results */ + + display_sam_sync(num_deltas, hdr_deltas, deltas); + + done: + return result; +} + +/* Log on a domain user */ + +static NTSTATUS cmd_netlogon_sam_logon(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + unsigned char trust_passwd[16]; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + int logon_type = NET_LOGON_TYPE; + char *username, *password; + + /* Check arguments */ + + if (argc < 3 || argc > 4) { + fprintf(stderr, "Usage: samlogon <username> <password> " + "[logon_type]\n"); + return NT_STATUS_OK; + } + + username = argv[1]; + password = argv[2]; + + if (argc == 4) + sscanf(argv[3], "%i", &logon_type); + + /* Authenticate ourselves with the domain controller */ + + if (!secrets_init()) { + fprintf(stderr, "Unable to initialise secrets database\n"); + return result; + } + + if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd, + NULL)) { + fprintf(stderr, "could not fetch trust account password\n"); + goto done; + } + + result = cli_nt_setup_creds(cli, trust_passwd); + + if (!NT_STATUS_IS_OK(result)) { + fprintf(stderr, "Error initialising session creds\n"); + goto done; + } + + /* Perform the sam logon */ + + result = cli_netlogon_sam_logon(cli, mem_ctx, username, password, + logon_type); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + done: + return result; +} + +/* List of commands exported by this module */ + +struct cmd_set netlogon_commands[] = { + + { "NETLOGON" }, + + { "logonctrl2", cmd_netlogon_logon_ctrl2, PIPE_NETLOGON, "Logon Control 2", "" }, + { "logonctrl", cmd_netlogon_logon_ctrl, PIPE_NETLOGON, "Logon Control", "" }, + { "samsync", cmd_netlogon_sam_sync, PIPE_NETLOGON, "Sam Synchronisation", "" }, + { "samdeltas", cmd_netlogon_sam_deltas, PIPE_NETLOGON, "Query Sam Deltas", "" }, + { "samlogon", cmd_netlogon_sam_logon, PIPE_NETLOGON, "Sam Logon", "" }, + + { NULL } +}; diff --git a/source3/rpcclient/cmd_reg.c b/source3/rpcclient/cmd_reg.c new file mode 100644 index 0000000000..c089917f9b --- /dev/null +++ b/source3/rpcclient/cmd_reg.c @@ -0,0 +1,1010 @@ +/* + Unix SMB/CIFS implementation. + NT Domain Authentication SMB / MSRPC client + Copyright (C) Andrew Tridgell 1994-1997 + Copyright (C) Luke Kenneth Casson Leighton 1996-1997 + Copyright (C) Simo Sorce 2001 + + 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" +#include "rpcclient.h" + +/* + * keys. of the form: + * ---- + * + * [HKLM]|[HKU]\[parent_keyname_components]\[subkey]|[value] + * + * reg_getsubkey() splits this down into: + * [HKLM]|[HKU]\[parent_keyname_components] and [subkey]|[value] + * + * do_reg_connect() splits the left side down further into: + * [HKLM]|[HKU] and [parent_keyname_components]. + * + * HKLM is short for HKEY_LOCAL_MACHINE + * HKU is short for HKEY_USERS + * + * oh, and HKEY stands for "Hive Key". + * + */ + +#if 0 /* Simo: reg functions need to be updated to the new cmd interface */ + +/**************************************************************************** +nt registry enum +****************************************************************************/ +static void cmd_reg_enum(struct client_info *info) +{ + BOOL res = True; + BOOL res1 = True; + BOOL res2 = True; + int i; + + POLICY_HND key_pol; + fstring full_keyname; + fstring key_name; + + /* + * query key info + */ + + fstring key_class; + uint32 max_class_len = 0; + uint32 num_subkeys; + uint32 max_subkeylen; + uint32 max_subkeysize; + uint32 num_values; + uint32 max_valnamelen; + uint32 max_valbufsize; + uint32 sec_desc; + NTTIME mod_time; + + /* + * unknown 0x1a request + */ + + uint32 unk_1a_response; + + DEBUG(5, ("cmd_reg_enum: smb_cli->fd:%d\n", smb_cli->fd)); + + if (!next_token_nr(NULL, full_keyname, NULL, sizeof(full_keyname))) + { + fprintf(out_hnd, "regenum <key_name>\n"); + return; + } + + /* open WINREG session. */ + res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False; + + /* open registry receive a policy handle */ + res = res ? do_reg_connect(smb_cli, full_keyname, key_name, + &info->dom.reg_pol_connect) : False; + + if ((*key_name) != 0) + { + /* open an entry */ + res1 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect, + key_name, 0x02000000, &key_pol) : False; + } + else + { + memcpy(&key_pol, &info->dom.reg_pol_connect, sizeof(key_pol)); + } + + res1 = res1 ? do_reg_query_key(smb_cli, + &key_pol, + key_class, &max_class_len, + &num_subkeys, &max_subkeylen, &max_subkeysize, + &num_values, &max_valnamelen, &max_valbufsize, + &sec_desc, &mod_time) : False; + + if (res1 && num_subkeys > 0) + { + fprintf(out_hnd,"Subkeys\n"); + fprintf(out_hnd,"-------\n"); + } + + for (i = 0; i < num_subkeys; i++) + { + /* + * enumerate key + */ + + fstring enum_name; + uint32 enum_unk1; + uint32 enum_unk2; + time_t key_mod_time; + + /* unknown 1a it */ + res2 = res1 ? do_reg_unknown_1a(smb_cli, &key_pol, + &unk_1a_response) : False; + + if (res2 && unk_1a_response != 5) + { + fprintf(out_hnd,"Unknown 1a response: %x\n", unk_1a_response); + } + + /* enum key */ + res2 = res2 ? do_reg_enum_key(smb_cli, &key_pol, + i, enum_name, + &enum_unk1, &enum_unk2, + &key_mod_time) : False; + + if (res2) + { + display_reg_key_info(out_hnd, ACTION_HEADER , enum_name, key_mod_time); + display_reg_key_info(out_hnd, ACTION_ENUMERATE, enum_name, key_mod_time); + display_reg_key_info(out_hnd, ACTION_FOOTER , enum_name, key_mod_time); + } + + } + + if (num_values > 0) + { + fprintf(out_hnd,"Key Values\n"); + fprintf(out_hnd,"----------\n"); + } + + for (i = 0; i < num_values; i++) + { + /* + * enumerate key + */ + + uint32 val_type; + BUFFER2 value; + fstring val_name; + + /* unknown 1a it */ + res2 = res1 ? do_reg_unknown_1a(smb_cli, &key_pol, + &unk_1a_response) : False; + + if (res2 && unk_1a_response != 5) + { + fprintf(out_hnd,"Unknown 1a response: %x\n", unk_1a_response); + } + + /* enum key */ + res2 = res2 ? do_reg_enum_val(smb_cli, &key_pol, + i, max_valnamelen, max_valbufsize, + val_name, &val_type, &value) : False; + + if (res2) + { + display_reg_value_info(out_hnd, ACTION_HEADER , val_name, val_type, &value); + display_reg_value_info(out_hnd, ACTION_ENUMERATE, val_name, val_type, &value); + display_reg_value_info(out_hnd, ACTION_FOOTER , val_name, val_type, &value); + } + } + + /* close the handles */ + if ((*key_name) != 0) + { + res1 = res1 ? do_reg_close(smb_cli, &key_pol) : False; + } + res = res ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False; + + /* close the session */ + cli_nt_session_close(smb_cli); + + if (res && res1 && res2) + { + DEBUG(5,("cmd_reg_enum: query succeeded\n")); + } + else + { + DEBUG(5,("cmd_reg_enum: query failed\n")); + } +} + +/**************************************************************************** +nt registry query key +****************************************************************************/ +static void cmd_reg_query_key(struct client_info *info) +{ + BOOL res = True; + BOOL res1 = True; + + POLICY_HND key_pol; + fstring full_keyname; + fstring key_name; + + /* + * query key info + */ + + fstring key_class; + uint32 key_class_len = 0; + uint32 num_subkeys; + uint32 max_subkeylen; + uint32 max_subkeysize; + uint32 num_values; + uint32 max_valnamelen; + uint32 max_valbufsize; + uint32 sec_desc; + NTTIME mod_time; + + DEBUG(5, ("cmd_reg_enum: smb_cli->fd:%d\n", smb_cli->fd)); + + if (!next_token_nr(NULL, full_keyname, NULL, sizeof(full_keyname))) + { + fprintf(out_hnd, "regquery key_name\n"); + return; + } + + /* open WINREG session. */ + res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False; + + /* open registry receive a policy handle */ + res = res ? do_reg_connect(smb_cli, full_keyname, key_name, + &info->dom.reg_pol_connect) : False; + + if ((*key_name) != 0) + { + /* open an entry */ + res1 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect, + key_name, 0x02000000, &key_pol) : False; + } + else + { + memcpy(&key_pol, &info->dom.reg_pol_connect, sizeof(key_pol)); + } + + res1 = res1 ? do_reg_query_key(smb_cli, + &key_pol, + key_class, &key_class_len, + &num_subkeys, &max_subkeylen, &max_subkeysize, + &num_values, &max_valnamelen, &max_valbufsize, + &sec_desc, &mod_time) : False; + + if (res1 && key_class_len != 0) + { + res1 = res1 ? do_reg_query_key(smb_cli, + &key_pol, + key_class, &key_class_len, + &num_subkeys, &max_subkeylen, &max_subkeysize, + &num_values, &max_valnamelen, &max_valbufsize, + &sec_desc, &mod_time) : False; + } + + if (res1) + { + fprintf(out_hnd,"Registry Query Info Key\n"); + fprintf(out_hnd,"key class: %s\n", key_class); + fprintf(out_hnd,"subkeys, max_len, max_size: %d %d %d\n", num_subkeys, max_subkeylen, max_subkeysize); + fprintf(out_hnd,"vals, max_len, max_size: 0x%x 0x%x 0x%x\n", num_values, max_valnamelen, max_valbufsize); + fprintf(out_hnd,"sec desc: 0x%x\n", sec_desc); + fprintf(out_hnd,"mod time: %s\n", http_timestring(nt_time_to_unix(&mod_time))); + } + + /* close the handles */ + if ((*key_name) != 0) + { + res1 = res1 ? do_reg_close(smb_cli, &key_pol) : False; + } + res = res ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False; + + /* close the session */ + cli_nt_session_close(smb_cli); + + if (res && res1) + { + DEBUG(5,("cmd_reg_query: query succeeded\n")); + } + else + { + DEBUG(5,("cmd_reg_query: query failed\n")); + } +} + +/**************************************************************************** +nt registry create value +****************************************************************************/ +static void cmd_reg_create_val(struct client_info *info) +{ + BOOL res = True; + BOOL res3 = True; + BOOL res4 = True; + + POLICY_HND parent_pol; + fstring full_keyname; + fstring keyname; + fstring parent_name; + fstring val_name; + fstring tmp; + uint32 val_type; + BUFFER3 value; + +#if 0 + uint32 unk_0; + uint32 unk_1; + /* query it */ + res1 = res1 ? do_reg_query_info(smb_cli, &val_pol, + val_name, *val_type) : False; +#endif + + DEBUG(5, ("cmd_reg_create_val: smb_cli->fd:%d\n", smb_cli->fd)); + + if (!next_token_nr(NULL, full_keyname, NULL, sizeof(full_keyname))) + { + fprintf(out_hnd, "regcreate <val_name> <val_type> <val>\n"); + return; + } + + reg_get_subkey(full_keyname, keyname, val_name); + + if (keyname[0] == 0 || val_name[0] == 0) + { + fprintf(out_hnd, "invalid key name\n"); + return; + } + + if (!next_token_nr(NULL, tmp, NULL, sizeof(tmp))) + { + fprintf(out_hnd, "regcreate <val_name> <val_type (1|4)> <val>\n"); + return; + } + + val_type = atoi(tmp); + + if (val_type != 1 && val_type != 3 && val_type != 4) + { + fprintf(out_hnd, "val_type 1=UNISTR, 3=BYTES, 4=DWORD supported\n"); + return; + } + + if (!next_token_nr(NULL, tmp, NULL, sizeof(tmp))) + { + fprintf(out_hnd, "regcreate <val_name> <val_type (1|4)> <val>\n"); + return; + } + + switch (val_type) + { + case 0x01: /* UNISTR */ + { + init_buffer3_str(&value, tmp, strlen(tmp)+1); + break; + } + case 0x03: /* BYTES */ + { + init_buffer3_hex(&value, tmp); + break; + } + case 0x04: /* DWORD */ + { + uint32 tmp_val; + if (strnequal(tmp, "0x", 2)) + { + tmp_val = strtol(tmp, (char**)NULL, 16); + } + else + { + tmp_val = strtol(tmp, (char**)NULL, 10); + } + init_buffer3_uint32(&value, tmp_val); + break; + } + default: + { + fprintf(out_hnd, "i told you i only deal with UNISTR, DWORD and BYTES!\n"); + return; + } + } + + DEBUG(10,("key data:\n")); + dump_data(10, (char *)value.buffer, value.buf_len); + + /* open WINREG session. */ + res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False; + + /* open registry receive a policy handle */ + res = res ? do_reg_connect(smb_cli, keyname, parent_name, + &info->dom.reg_pol_connect) : False; + + if ((*val_name) != 0) + { + /* open an entry */ + res3 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect, + parent_name, 0x02000000, &parent_pol) : False; + } + else + { + memcpy(&parent_pol, &info->dom.reg_pol_connect, sizeof(parent_pol)); + } + + /* create an entry */ + res4 = res3 ? do_reg_create_val(smb_cli, &parent_pol, + val_name, val_type, &value) : False; + + /* flush the modified key */ + res4 = res4 ? do_reg_flush_key(smb_cli, &parent_pol) : False; + + /* close the val handle */ + if ((*val_name) != 0) + { + res3 = res3 ? do_reg_close(smb_cli, &parent_pol) : False; + } + + /* close the registry handles */ + res = res ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False; + + /* close the session */ + cli_nt_session_close(smb_cli); + + if (res && res3 && res4) + { + DEBUG(5,("cmd_reg_create_val: query succeeded\n")); + fprintf(out_hnd,"OK\n"); + } + else + { + DEBUG(5,("cmd_reg_create_val: query failed\n")); + } +} + +/**************************************************************************** +nt registry delete value +****************************************************************************/ +static void cmd_reg_delete_val(struct client_info *info) +{ + BOOL res = True; + BOOL res3 = True; + BOOL res4 = True; + + POLICY_HND parent_pol; + fstring full_keyname; + fstring keyname; + fstring parent_name; + fstring val_name; + + DEBUG(5, ("cmd_reg_delete_val: smb_cli->fd:%d\n", smb_cli->fd)); + + if (!next_token_nr(NULL, full_keyname, NULL, sizeof(full_keyname))) + { + fprintf(out_hnd, "regdelete <val_name>\n"); + return; + } + + reg_get_subkey(full_keyname, keyname, val_name); + + if (keyname[0] == 0 || val_name[0] == 0) + { + fprintf(out_hnd, "invalid key name\n"); + return; + } + + /* open WINREG session. */ + res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False; + + /* open registry receive a policy handle */ + res = res ? do_reg_connect(smb_cli, keyname, parent_name, + &info->dom.reg_pol_connect) : False; + + if ((*val_name) != 0) + { + /* open an entry */ + res3 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect, + parent_name, 0x02000000, &parent_pol) : False; + } + else + { + memcpy(&parent_pol, &info->dom.reg_pol_connect, sizeof(parent_pol)); + } + + /* delete an entry */ + res4 = res3 ? do_reg_delete_val(smb_cli, &parent_pol, val_name) : False; + + /* flush the modified key */ + res4 = res4 ? do_reg_flush_key(smb_cli, &parent_pol) : False; + + /* close the key handle */ + res3 = res3 ? do_reg_close(smb_cli, &parent_pol) : False; + + /* close the registry handles */ + res = res ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False; + + /* close the session */ + cli_nt_session_close(smb_cli); + + if (res && res3 && res4) + { + DEBUG(5,("cmd_reg_delete_val: query succeeded\n")); + fprintf(out_hnd,"OK\n"); + } + else + { + DEBUG(5,("cmd_reg_delete_val: query failed\n")); + } +} + +/**************************************************************************** +nt registry delete key +****************************************************************************/ +static void cmd_reg_delete_key(struct client_info *info) +{ + BOOL res = True; + BOOL res3 = True; + BOOL res4 = True; + + POLICY_HND parent_pol; + fstring full_keyname; + fstring parent_name; + fstring key_name; + fstring subkey_name; + + DEBUG(5, ("cmd_reg_delete_key: smb_cli->fd:%d\n", smb_cli->fd)); + + if (!next_token_nr(NULL, full_keyname, NULL, sizeof(full_keyname))) + { + fprintf(out_hnd, "regdeletekey <key_name>\n"); + return; + } + + reg_get_subkey(full_keyname, parent_name, subkey_name); + + if (parent_name[0] == 0 || subkey_name[0] == 0) + { + fprintf(out_hnd, "invalid key name\n"); + return; + } + + /* open WINREG session. */ + res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False; + + /* open registry receive a policy handle */ + res = res ? do_reg_connect(smb_cli, parent_name, key_name, + &info->dom.reg_pol_connect) : False; + + if ((*key_name) != 0) + { + /* open an entry */ + res3 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect, + key_name, 0x02000000, &parent_pol) : False; + } + else + { + memcpy(&parent_pol, &info->dom.reg_pol_connect, sizeof(parent_pol)); + } + + /* create an entry */ + res4 = res3 ? do_reg_delete_key(smb_cli, &parent_pol, subkey_name) : False; + + /* flush the modified key */ + res4 = res4 ? do_reg_flush_key(smb_cli, &parent_pol) : False; + + /* close the key handle */ + if ((*key_name) != 0) + { + res3 = res3 ? do_reg_close(smb_cli, &parent_pol) : False; + } + + /* close the registry handles */ + res = res ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False; + + /* close the session */ + cli_nt_session_close(smb_cli); + + if (res && res3 && res4) + { + DEBUG(5,("cmd_reg_delete_key: query succeeded\n")); + fprintf(out_hnd,"OK\n"); + } + else + { + DEBUG(5,("cmd_reg_delete_key: query failed\n")); + } +} + +/**************************************************************************** +nt registry create key +****************************************************************************/ +static void cmd_reg_create_key(struct client_info *info) +{ + BOOL res = True; + BOOL res3 = True; + BOOL res4 = True; + + POLICY_HND parent_pol; + POLICY_HND key_pol; + fstring full_keyname; + fstring parent_key; + fstring parent_name; + fstring key_name; + fstring key_class; + SEC_ACCESS sam_access; + + DEBUG(5, ("cmd_reg_create_key: smb_cli->fd:%d\n", smb_cli->fd)); + + if (!next_token_nr(NULL, full_keyname, NULL, sizeof(full_keyname))) + { + fprintf(out_hnd, "regcreate <key_name> [key_class]\n"); + return; + } + + reg_get_subkey(full_keyname, parent_key, key_name); + + if (parent_key[0] == 0 || key_name[0] == 0) + { + fprintf(out_hnd, "invalid key name\n"); + return; + } + + if (!next_token_nr(NULL, key_class, NULL, sizeof(key_class))) + { + memset(key_class, 0, sizeof(key_class)); + } + + /* set access permissions */ + sam_access.mask = SEC_RIGHTS_READ; + + /* open WINREG session. */ + res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False; + + /* open registry receive a policy handle */ + res = res ? do_reg_connect(smb_cli, parent_key, parent_name, + &info->dom.reg_pol_connect) : False; + + if ((*parent_name) != 0) + { + /* open an entry */ + res3 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect, + parent_name, 0x02000000, &parent_pol) : False; + } + else + { + memcpy(&parent_pol, &info->dom.reg_pol_connect, sizeof(parent_pol)); + } + + /* create an entry */ + res4 = res3 ? do_reg_create_key(smb_cli, &parent_pol, + key_name, key_class, &sam_access, &key_pol) : False; + + /* flush the modified key */ + res4 = res4 ? do_reg_flush_key(smb_cli, &parent_pol) : False; + + /* close the key handle */ + res4 = res4 ? do_reg_close(smb_cli, &key_pol) : False; + + /* close the key handle */ + if ((*parent_name) != 0) + { + res3 = res3 ? do_reg_close(smb_cli, &parent_pol) : False; + } + + /* close the registry handles */ + res = res ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False; + + /* close the session */ + cli_nt_session_close(smb_cli); + + if (res && res3 && res4) + { + DEBUG(5,("cmd_reg_create_key: query succeeded\n")); + fprintf(out_hnd,"OK\n"); + } + else + { + DEBUG(5,("cmd_reg_create_key: query failed\n")); + } +} + +/**************************************************************************** +nt registry security info +****************************************************************************/ +static void cmd_reg_test_key_sec(struct client_info *info) +{ + BOOL res = True; + BOOL res3 = True; + BOOL res4 = True; + + POLICY_HND key_pol; + fstring full_keyname; + fstring key_name; + + /* + * security info + */ + + uint32 sec_buf_size; + SEC_DESC_BUF *psdb; + + DEBUG(5, ("cmd_reg_get_key_sec: smb_cli->fd:%d\n", smb_cli->fd)); + + if (!next_token_nr(NULL, full_keyname, NULL, sizeof(full_keyname))) + { + fprintf(out_hnd, "reggetsec <key_name>\n"); + return; + } + + /* open WINREG session. */ + res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False; + + /* open registry receive a policy handle */ + res = res ? do_reg_connect(smb_cli, full_keyname, key_name, + &info->dom.reg_pol_connect) : False; + + if ((*key_name) != 0) + { + /* open an entry */ + res3 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect, + key_name, 0x02000000, &key_pol) : False; + } + else + { + memcpy(&key_pol, &info->dom.reg_pol_connect, sizeof(key_pol)); + } + + /* open an entry */ + res3 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect, + key_name, 0x02000000, &key_pol) : False; + + /* query key sec info. first call sets sec_buf_size. */ + + sec_buf_size = 0; + res4 = res3 ? do_reg_get_key_sec(smb_cli, &key_pol, + &sec_buf_size, &psdb) : False; + + free_sec_desc_buf(&psdb); + + res4 = res4 ? do_reg_get_key_sec(smb_cli, &key_pol, + &sec_buf_size, &psdb) : False; + + if (res4 && psdb->len > 0 && psdb->sec != NULL) + { + display_sec_desc(out_hnd, ACTION_HEADER , psdb->sec); + display_sec_desc(out_hnd, ACTION_ENUMERATE, psdb->sec); + display_sec_desc(out_hnd, ACTION_FOOTER , psdb->sec); + + res4 = res4 ? do_reg_set_key_sec(smb_cli, &key_pol, psdb) : False; + } + + free_sec_desc_buf(&psdb); + + /* close the key handle */ + if ((*key_name) != 0) + { + res3 = res3 ? do_reg_close(smb_cli, &key_pol) : False; + } + + /* close the registry handles */ + res = res ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False; + + /* close the session */ + cli_nt_session_close(smb_cli); + + if (res && res3 && res4) + { + DEBUG(5,("cmd_reg_test2: query succeeded\n")); + fprintf(out_hnd,"Registry Test2\n"); + } + else + { + DEBUG(5,("cmd_reg_test2: query failed\n")); + } +} + +/**************************************************************************** +nt registry security info +****************************************************************************/ +static void cmd_reg_get_key_sec(struct client_info *info) +{ + BOOL res = True; + BOOL res3 = True; + BOOL res4 = True; + + POLICY_HND key_pol; + fstring full_keyname; + fstring key_name; + + /* + * security info + */ + + uint32 sec_buf_size; + SEC_DESC_BUF *psdb; + + DEBUG(5, ("cmd_reg_get_key_sec: smb_cli->fd:%d\n", smb_cli->fd)); + + if (!next_token_nr(NULL, full_keyname, NULL, sizeof(full_keyname))) + { + fprintf(out_hnd, "reggetsec <key_name>\n"); + return; + } + + /* open WINREG session. */ + res = res ? cli_nt_session_open(smb_cli, PIPE_WINREG) : False; + + /* open registry receive a policy handle */ + res = res ? do_reg_connect(smb_cli, full_keyname, key_name, + &info->dom.reg_pol_connect) : False; + + if ((*key_name) != 0) + { + /* open an entry */ + res3 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect, + key_name, 0x02000000, &key_pol) : False; + } + else + { + memcpy(&key_pol, &info->dom.reg_pol_connect, sizeof(key_pol)); + } + + /* open an entry */ + res3 = res ? do_reg_open_entry(smb_cli, &info->dom.reg_pol_connect, + key_name, 0x02000000, &key_pol) : False; + + /* Get the size. */ + sec_buf_size = 0; + res4 = res3 ? do_reg_get_key_sec(smb_cli, &key_pol, + &sec_buf_size, &psdb) : False; + + free_sec_desc_buf(&psdb); + + res4 = res4 ? do_reg_get_key_sec(smb_cli, &key_pol, + &sec_buf_size, &psdb) : False; + + if (res4 && psdb->len > 0 && psdb->sec != NULL) + { + display_sec_desc(out_hnd, ACTION_HEADER , psdb->sec); + display_sec_desc(out_hnd, ACTION_ENUMERATE, psdb->sec); + display_sec_desc(out_hnd, ACTION_FOOTER , psdb->sec); + } + + free_sec_desc_buf(&psdb); + + /* close the key handle */ + if ((*key_name) != 0) + { + res3 = res3 ? do_reg_close(smb_cli, &key_pol) : False; + } + + /* close the registry handles */ + res = res ? do_reg_close(smb_cli, &info->dom.reg_pol_connect) : False; + + /* close the session */ + cli_nt_session_close(smb_cli); + + if (res && res3 && res4) + { + DEBUG(5,("cmd_reg_get_key_sec: query succeeded\n")); + } + else + { + DEBUG(5,("cmd_reg_get_key_sec: query failed\n")); + } +} + +#endif /* 0 */ + +/**************************************************************************** +nt registry shutdown +****************************************************************************/ +static NTSTATUS cmd_reg_shutdown(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + fstring msg; + uint32 timeout = 20; + uint16 flgs = 0; + int opt; + + *msg = 0; + optind = 0; /* TODO: test if this hack works on other systems too --simo */ + + while ((opt = getopt(argc, argv, "m:t:rf")) != EOF) + { + fprintf (stderr, "[%s]\n", argv[argc-1]); + + switch (opt) + { + case 'm': + { + safe_strcpy(msg, optarg, sizeof(msg)-1); + fprintf (stderr, "[%s|%s]\n", optarg, msg); + break; + } + case 't': + { + timeout = atoi(optarg); + fprintf (stderr, "[%s|%d]\n", optarg, timeout); + break; + } + case 'r': + { + flgs |= 0x100; + break; + } + case 'f': + { + flgs |= 0x001; + break; + } + } + } + + /* create an entry */ + result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, flgs); + + if (NT_STATUS_IS_OK(result)) + DEBUG(5,("cmd_reg_shutdown: query succeeded\n")); + else + DEBUG(5,("cmd_reg_shutdown: query failed\n")); + + return result; +} + +/**************************************************************************** +abort a shutdown +****************************************************************************/ +static NTSTATUS cmd_reg_abort_shutdown(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + + result = cli_reg_abort_shutdown(cli, mem_ctx); + + if (NT_STATUS_IS_OK(result)) + DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n")); + else + DEBUG(5,("cmd_reg_abort_shutdown: query failed\n")); + + return result; +} + + +/* List of commands exported by this module */ +struct cmd_set reg_commands[] = { + + { "REG" }, + + { "shutdown", cmd_reg_shutdown, PIPE_WINREG, "Remote Shutdown", + "[-m message] [-t timeout] [-r] [-f] (-r == reboot, -f == force)" }, + + { "abortshutdown", cmd_reg_abort_shutdown, PIPE_WINREG, "Abort Shutdown", + "" }, +/* + { "regenum", cmd_reg_enum, "Registry Enumeration", + "<keyname>" }, + + { "regdeletekey", cmd_reg_delete_key, "Registry Key Delete", + "<keyname>" }, + + { "regcreatekey", cmd_reg_create_key, "Registry Key Create", + "<keyname> [keyclass]" }, + + { "regqueryval", cmd_reg_query_info, "Registry Value Query", + "<valname>" }, + + { "regquerykey", cmd_reg_query_key, "Registry Key Query", + "<keyname>" }, + + { "regdeleteval", cmd_reg_delete_val, "Registry Value Delete", + "<valname>" }, + + { "regcreateval", cmd_reg_create_val, "Registry Key Create", + "<valname> <valtype> <value>" }, + + { "reggetsec", cmd_reg_get_key_sec, "Registry Key Security", + "<keyname>" }, + + { "regtestsec", cmd_reg_test_key_sec, "Test Registry Key Security", + "<keyname>" }, +*/ + { NULL } +}; diff --git a/source3/rpcclient/cmd_samr.c b/source3/rpcclient/cmd_samr.c new file mode 100644 index 0000000000..ee46fc64a5 --- /dev/null +++ b/source3/rpcclient/cmd_samr.c @@ -0,0 +1,1288 @@ +/* + Unix SMB/CIFS implementation. + RPC pipe client + + Copyright (C) Andrew Tridgell 1992-2000, + Copyright (C) Luke Kenneth Casson Leighton 1996-2000, + Copyright (C) Elrond 2000, + Copyright (C) Tim Potter 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" +#include "rpcclient.h" + +extern DOM_SID domain_sid; + +/**************************************************************************** + display sam_user_info_21 structure + ****************************************************************************/ +static void display_sam_user_info_21(SAM_USER_INFO_21 *usr) +{ + fstring temp; + + unistr2_to_ascii(temp, &usr->uni_user_name, sizeof(temp)-1); + printf("\tUser Name :\t%s\n", temp); + + unistr2_to_ascii(temp, &usr->uni_full_name, sizeof(temp)-1); + printf("\tFull Name :\t%s\n", temp); + + unistr2_to_ascii(temp, &usr->uni_home_dir, sizeof(temp)-1); + printf("\tHome Drive :\t%s\n", temp); + + unistr2_to_ascii(temp, &usr->uni_dir_drive, sizeof(temp)-1); + printf("\tDir Drive :\t%s\n", temp); + + unistr2_to_ascii(temp, &usr->uni_profile_path, sizeof(temp)-1); + printf("\tProfile Path:\t%s\n", temp); + + unistr2_to_ascii(temp, &usr->uni_logon_script, sizeof(temp)-1); + printf("\tLogon Script:\t%s\n", temp); + + unistr2_to_ascii(temp, &usr->uni_acct_desc, sizeof(temp)-1); + printf("\tDescription :\t%s\n", temp); + + unistr2_to_ascii(temp, &usr->uni_workstations, sizeof(temp)-1); + printf("\tWorkstations:\t%s\n", temp); + + unistr2_to_ascii(temp, &usr->uni_unknown_str, sizeof(temp)-1); + printf("\tUnknown Str :\t%s\n", temp); + + unistr2_to_ascii(temp, &usr->uni_munged_dial, sizeof(temp)-1); + printf("\tRemote Dial :\t%s\n", temp); + + printf("\tLogon Time :\t%s\n", + http_timestring(nt_time_to_unix(&usr->logon_time))); + printf("\tLogoff Time :\t%s\n", + http_timestring(nt_time_to_unix(&usr->logoff_time))); + printf("\tKickoff Time :\t%s\n", + http_timestring(nt_time_to_unix(&usr->kickoff_time))); + printf("\tPassword last set Time :\t%s\n", + http_timestring(nt_time_to_unix(&usr->pass_last_set_time))); + printf("\tPassword can change Time :\t%s\n", + http_timestring(nt_time_to_unix(&usr->pass_can_change_time))); + printf("\tPassword must change Time:\t%s\n", + http_timestring(nt_time_to_unix(&usr->pass_must_change_time))); + + printf("\tunknown_2[0..31]...\n"); /* user passwords? */ + + printf("\tuser_rid :\t%x\n" , usr->user_rid ); /* User ID */ + printf("\tgroup_rid:\t%x\n" , usr->group_rid); /* Group ID */ + printf("\tacb_info :\t%04x\n", usr->acb_info ); /* Account Control Info */ + + printf("\tunknown_3:\t%08x\n", usr->unknown_3); /* 0x00ff ffff */ + printf("\tlogon_divs:\t%d\n", usr->logon_divs); /* 0x0000 00a8 which is 168 which is num hrs in a week */ + printf("\tunknown_5:\t%08x\n", usr->unknown_5); /* 0x0002 0000 */ + + printf("\tpadding1[0..7]...\n"); + + if (usr->ptr_logon_hrs) { + printf("\tlogon_hrs[0..%d]...\n", usr->logon_hrs.len); + } +} + +static char *display_time(NTTIME nttime) +{ + static fstring string; + + float high; + float low; + int sec; + int days, hours, mins, secs; + + if (nttime.high==0 && nttime.low==0) + return "Now"; + + if (nttime.high==0x80000000 && nttime.low==0) + return "Never"; + + high = 65536; + high = high/10000; + high = high*65536; + high = high/1000; + high = high * (~nttime.high); + + low = ~nttime.low; + low = low/(1000*1000*10); + + sec=high+low; + + days=sec/(60*60*24); + hours=(sec - (days*60*60*24)) / (60*60); + mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60; + secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60); + + snprintf(string, sizeof(string)-1, "%u days, %u hours, %u minutes, %u seconds", days, hours, mins, secs); + return (string); +} + +static void display_sam_unk_info_1(SAM_UNK_INFO_1 *info1) +{ + + printf("Minimum password length: %d\n", info1->min_length_password); + printf("Password uniqueness (remember x passwords): %d\n", info1->password_history); + printf("flag: "); + if(info1->flag&&2==2) printf("users must open a session to change password "); + printf("\n"); + + printf("password expire in: %s\n", display_time(info1->expire)); + printf("Min password age (allow changing in x days): %s\n", display_time(info1->min_passwordage)); +} + +static void display_sam_unk_info_2(SAM_UNK_INFO_2 *info2) +{ + fstring name; + + unistr2_to_ascii(name, &info2->uni_domain, sizeof(name) - 1); + printf("Domain:\t%s\n", name); + + unistr2_to_ascii(name, &info2->uni_server, sizeof(name) - 1); + printf("Server:\t%s\n", name); + + printf("Total Users:\t%d\n", info2->num_domain_usrs); + printf("Total Groups:\t%d\n", info2->num_domain_grps); + printf("Total Aliases:\t%d\n", info2->num_local_grps); + + printf("Sequence No:\t%d\n", info2->seq_num); + + printf("Unknown 0:\t0x%x\n", info2->unknown_0); + printf("Unknown 1:\t0x%x\n", info2->unknown_1); + printf("Unknown 2:\t0x%x\n", info2->unknown_2); + printf("Unknown 3:\t0x%x\n", info2->unknown_3); + printf("Unknown 4:\t0x%x\n", info2->unknown_4); + printf("Unknown 5:\t0x%x\n", info2->unknown_5); + printf("Unknown 6:\t0x%x\n", info2->unknown_6); +} + +static void display_sam_info_1(SAM_ENTRY1 *e1, SAM_STR1 *s1) +{ + fstring tmp; + + printf("index: 0x%x ", e1->user_idx); + printf("RID: 0x%x ", e1->rid_user); + printf("acb: 0x%x ", e1->acb_info); + + unistr2_to_ascii(tmp, &s1->uni_acct_name, sizeof(tmp)-1); + printf("Account: %s\t", tmp); + + unistr2_to_ascii(tmp, &s1->uni_full_name, sizeof(tmp)-1); + printf("Name: %s\t", tmp); + + unistr2_to_ascii(tmp, &s1->uni_acct_desc, sizeof(tmp)-1); + printf("Desc: %s\n", tmp); +} + +static void display_sam_info_2(SAM_ENTRY2 *e2, SAM_STR2 *s2) +{ + fstring tmp; + + printf("index: 0x%x ", e2->user_idx); + printf("RID: 0x%x ", e2->rid_user); + printf("acb: 0x%x ", e2->acb_info); + + unistr2_to_ascii(tmp, &s2->uni_srv_name, sizeof(tmp)-1); + printf("Account: %s\t", tmp); + + unistr2_to_ascii(tmp, &s2->uni_srv_desc, sizeof(tmp)-1); + printf("Name: %s\n", tmp); + +} + +static void display_sam_info_3(SAM_ENTRY3 *e3, SAM_STR3 *s3) +{ + fstring tmp; + + printf("index: 0x%x ", e3->grp_idx); + printf("RID: 0x%x ", e3->rid_grp); + printf("attr: 0x%x ", e3->attr); + + unistr2_to_ascii(tmp, &s3->uni_grp_name, sizeof(tmp)-1); + printf("Account: %s\t", tmp); + + unistr2_to_ascii(tmp, &s3->uni_grp_desc, sizeof(tmp)-1); + printf("Name: %s\n", tmp); + +} + +static void display_sam_info_4(SAM_ENTRY4 *e4, SAM_STR4 *s4) +{ + int i; + + printf("index: %d ", e4->user_idx); + + printf("Account: "); + for (i=0; i<s4->acct_name.str_str_len; i++) + printf("%c", s4->acct_name.buffer[i]); + printf("\n"); + +} + +static void display_sam_info_5(SAM_ENTRY5 *e5, SAM_STR5 *s5) +{ + int i; + + printf("index: 0x%x ", e5->grp_idx); + + printf("Account: "); + for (i=0; i<s5->grp_name.str_str_len; i++) + printf("%c", s5->grp_name.buffer[i]); + printf("\n"); + +} + +/********************************************************************** + * Query user information + */ +static NTSTATUS cmd_samr_query_user(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol, user_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 info_level = 21; + SAM_USERINFO_CTR *user_ctr; + fstring server; + uint32 user_rid; + + if (argc != 2) { + printf("Usage: %s rid\n", argv[0]); + return NT_STATUS_OK; + } + + sscanf(argv[1], "%i", &user_rid); + + slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (server); + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_samr_open_user(cli, mem_ctx, &domain_pol, + MAXIMUM_ALLOWED_ACCESS, + user_rid, &user_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + ZERO_STRUCT(user_ctr); + + result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol, + info_level, &user_ctr); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + display_sam_user_info_21(user_ctr->info.id21); + +done: + return result; +} + +/**************************************************************************** + display group info + ****************************************************************************/ +static void display_group_info1(GROUP_INFO1 *info1) +{ + fstring temp; + + unistr2_to_ascii(temp, &info1->uni_acct_name, sizeof(temp)-1); + printf("\tGroup Name:\t%s\n", temp); + unistr2_to_ascii(temp, &info1->uni_acct_desc, sizeof(temp)-1); + printf("\tDescription:\t%s\n", temp); + printf("\tunk1:%d\n", info1->unknown_1); + printf("\tNum Members:%d\n", info1->num_members); +} + +/**************************************************************************** + display group info + ****************************************************************************/ +static void display_group_info4(GROUP_INFO4 *info4) +{ + fstring desc; + + unistr2_to_ascii(desc, &info4->uni_acct_desc, sizeof(desc)-1); + printf("\tGroup Description:%s\n", desc); +} + +/**************************************************************************** + display sam sync structure + ****************************************************************************/ +static void display_group_info_ctr(GROUP_INFO_CTR *ctr) +{ + switch (ctr->switch_value1) { + case 1: { + display_group_info1(&ctr->group.info1); + break; + } + case 4: { + display_group_info4(&ctr->group.info4); + break; + } + } +} + +/*********************************************************************** + * Query group information + */ +static NTSTATUS cmd_samr_query_group(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol, group_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 info_level = 1; + GROUP_INFO_CTR group_ctr; + fstring server; + uint32 group_rid; + + if (argc != 2) { + printf("Usage: %s rid\n", argv[0]); + return NT_STATUS_OK; + } + + sscanf(argv[1], "%i", &group_rid); + + slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (server); + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + result = cli_samr_open_group(cli, mem_ctx, &domain_pol, + MAXIMUM_ALLOWED_ACCESS, + group_rid, &group_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + ZERO_STRUCT(group_ctr); + + result = cli_samr_query_groupinfo(cli, mem_ctx, &group_pol, + info_level, &group_ctr); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + display_group_info_ctr(&group_ctr); + +done: + return result; +} + +/* Query groups a user is a member of */ + +static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, + domain_pol, + user_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 num_groups, + user_rid; + DOM_GID *user_gids; + int i; + fstring server; + + if (argc != 2) { + printf("Usage: %s rid\n", argv[0]); + return NT_STATUS_OK; + } + + sscanf(argv[1], "%i", &user_rid); + + slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (server); + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + result = cli_samr_open_user(cli, mem_ctx, &domain_pol, + MAXIMUM_ALLOWED_ACCESS, + user_rid, &user_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol, + &num_groups, &user_gids); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + for (i = 0; i < num_groups; i++) { + printf("\tgroup rid:[0x%x] attr:[0x%x]\n", + user_gids[i].g_rid, user_gids[i].attr); + } + + done: + return result; +} + +/* Query aliases a user is a member of */ + +static NTSTATUS cmd_samr_query_useraliases(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 user_rid, num_aliases, *alias_rids; + int i; + fstring server; + DOM_SID tmp_sid; + DOM_SID2 sid; + DOM_SID global_sid_Builtin; + + string_to_sid(&global_sid_Builtin, "S-1-5-32"); + + if (argc != 3) { + printf("Usage: %s builtin|domain rid\n", argv[0]); + return NT_STATUS_OK; + } + + sscanf(argv[2], "%i", &user_rid); + + slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (server); + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + if (StrCaseCmp(argv[1], "domain")==0) + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + else if (StrCaseCmp(argv[1], "builtin")==0) + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &global_sid_Builtin, &domain_pol); + else + return NT_STATUS_OK; + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + sid_copy(&tmp_sid, &domain_sid); + sid_append_rid(&tmp_sid, user_rid); + init_dom_sid2(&sid, &tmp_sid); + + result = cli_samr_query_useraliases(cli, mem_ctx, &domain_pol, 1, &sid, &num_aliases, &alias_rids); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + for (i = 0; i < num_aliases; i++) { + printf("\tgroup rid:[0x%x]\n", alias_rids[i]); + } + + done: + return result; +} + +/* Query members of a group */ + +static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol, group_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 num_members, *group_rids, *group_attrs, group_rid; + int i; + fstring server; + + if (argc != 2) { + printf("Usage: %s rid\n", argv[0]); + return NT_STATUS_OK; + } + + sscanf(argv[1], "%i", &group_rid); + + slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (server); + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + result = cli_samr_open_group(cli, mem_ctx, &domain_pol, + MAXIMUM_ALLOWED_ACCESS, + group_rid, &group_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol, + &num_members, &group_rids, + &group_attrs); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + for (i = 0; i < num_members; i++) { + printf("\trid:[0x%x] attr:[0x%x]\n", group_rids[i], + group_attrs[i]); + } + + done: + return result; +} + +/* Enumerate domain groups */ + +static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 start_idx, size, num_dom_groups, i; + struct acct_info *dom_groups; + + if (argc != 1) { + printf("Usage: %s\n", argv[0]); + return NT_STATUS_OK; + } + + /* Get sam policy handle */ + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Get domain policy handle */ + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Enumerate domain groups */ + + start_idx = 0; + size = 0xffff; + + result = cli_samr_enum_dom_groups(cli, mem_ctx, &domain_pol, + &start_idx, size, + &dom_groups, &num_dom_groups); + + for (i = 0; i < num_dom_groups; i++) + printf("group:[%s] rid:[0x%x]\n", dom_groups[i].acct_name, + dom_groups[i].rid); + + done: + return result; +} + +/* Enumerate domain groups */ + +static NTSTATUS cmd_samr_enum_als_groups(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 start_idx, size, num_dom_groups, i; + struct acct_info *dom_groups; + DOM_SID global_sid_Builtin; + + string_to_sid(&global_sid_Builtin, "S-1-5-32"); + + if (argc != 2) { + printf("Usage: %s builtin|domain\n", argv[0]); + return NT_STATUS_OK; + } + + /* Get sam policy handle */ + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Get domain policy handle */ + + if (StrCaseCmp(argv[1], "domain")==0) + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + else if (StrCaseCmp(argv[1], "builtin")==0) + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &global_sid_Builtin, &domain_pol); + else + return NT_STATUS_OK; + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Enumerate domain groups */ + + start_idx = 0; + size = 0xffff; + + result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol, + &start_idx, size, + &dom_groups, &num_dom_groups); + + for (i = 0; i < num_dom_groups; i++) + printf("group:[%s] rid:[0x%x]\n", dom_groups[i].acct_name, + dom_groups[i].rid); + + done: + return result; +} + +/* Query alias membership */ + +static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol, alias_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 alias_rid, num_members, i; + DOM_SID *alias_sids; + + if (argc != 2) { + printf("Usage: %s rid\n", argv[0]); + return NT_STATUS_OK; + } + + sscanf(argv[1], "%i", &alias_rid); + + /* Open SAMR handle */ + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Open handle on domain */ + + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Open handle on alias */ + + result = cli_samr_open_alias(cli, mem_ctx, &domain_pol, + MAXIMUM_ALLOWED_ACCESS, + alias_rid, &alias_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol, + &num_members, &alias_sids); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + for (i = 0; i < num_members; i++) { + fstring sid_str; + + sid_to_string(sid_str, &alias_sids[i]); + printf("\tsid:[%s]\n", sid_str); + } + + done: + return result; +} + +/* Query display info */ + +static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 start_idx=0, max_entries=250, num_entries, i; + int info_level = 1; + SAM_DISPINFO_CTR ctr; + SAM_DISPINFO_1 info1; + SAM_DISPINFO_2 info2; + SAM_DISPINFO_3 info3; + SAM_DISPINFO_4 info4; + SAM_DISPINFO_5 info5; + + if (argc > 4) { + printf("Usage: %s [info level] [start index] [max entries]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc >= 2) + sscanf(argv[1], "%i", &info_level); + + if (argc >= 3) + sscanf(argv[2], "%i", &start_idx); + + if (argc >= 4) + sscanf(argv[3], "%i", &max_entries); + + /* Get sam policy handle */ + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, &connect_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Get domain policy handle */ + + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Query display info */ + + ZERO_STRUCT(ctr); + ZERO_STRUCT(info1); + + switch (info_level) { + case 1: + ZERO_STRUCT(info1); + ctr.sam.info1 = &info1; + break; + case 2: + ZERO_STRUCT(info2); + ctr.sam.info2 = &info2; + break; + case 3: + ZERO_STRUCT(info3); + ctr.sam.info3 = &info3; + break; + case 4: + ZERO_STRUCT(info4); + ctr.sam.info4 = &info4; + break; + case 5: + ZERO_STRUCT(info5); + ctr.sam.info5 = &info5; + break; + } + + + do { + result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol, + &start_idx, info_level, + &num_entries, max_entries, &ctr); + + for (i = 0; i < num_entries; i++) { + switch (info_level) { + case 1: + display_sam_info_1(&ctr.sam.info1->sam[i], &ctr.sam.info1->str[i]); + break; + case 2: + display_sam_info_2(&ctr.sam.info2->sam[i], &ctr.sam.info2->str[i]); + break; + case 3: + display_sam_info_3(&ctr.sam.info3->sam[i], &ctr.sam.info3->str[i]); + break; + case 4: + display_sam_info_4(&ctr.sam.info4->sam[i], &ctr.sam.info4->str[i]); + break; + case 5: + display_sam_info_5(&ctr.sam.info5->sam[i], &ctr.sam.info5->str[i]); + break; + } + } + } while (!NT_STATUS_IS_OK(result)); + done: + return result; +} + +/* Query domain info */ + +static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + int switch_value = 2; + SAM_UNK_CTR ctr; + + if (argc > 2) { + printf("Usage: %s [infolevel]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc == 2) + sscanf(argv[1], "%i", &switch_value); + + /* Get sam policy handle */ + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Get domain policy handle */ + + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Query domain info */ + + result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol, + switch_value, &ctr); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Display domain info */ + + switch (switch_value) { + case 1: + display_sam_unk_info_1(&ctr.info.inf1); + break; + case 2: + display_sam_unk_info_2(&ctr.info.inf2); + break; + default: + printf("cannot display domain info for switch value %d\n", + switch_value); + break; + } + + done: + + cli_samr_close(cli, mem_ctx, &domain_pol); + cli_samr_close(cli, mem_ctx, &connect_pol); + return result; +} + +/* Create domain user */ + +static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol, user_pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + char *acct_name; + uint16 acb_info; + uint32 unknown, user_rid; + + if (argc != 2) { + printf("Usage: %s username\n", argv[0]); + return NT_STATUS_OK; + } + + acct_name = argv[1]; + + /* Get sam policy handle */ + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Get domain policy handle */ + + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Create domain user */ + + acb_info = ACB_NORMAL; + unknown = 0xe005000b; /* No idea what this is - a permission mask? */ + + result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol, + acct_name, acb_info, unknown, + &user_pol, &user_rid); + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + done: + return result; +} + +/* Lookup sam names */ + +static NTSTATUS cmd_samr_lookup_names(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + POLICY_HND connect_pol, domain_pol; + uint32 flags = 0x000003e8; /* Unknown */ + uint32 num_rids, num_names, *name_types, *rids; + char **names; + int i; + DOM_SID global_sid_Builtin; + + string_to_sid(&global_sid_Builtin, "S-1-5-32"); + + if (argc < 3) { + printf("Usage: %s domain|builtin name1 [name2 [name3] [...]]\n", argv[0]); + printf("check on the domain SID: S-1-5-21-x-y-z\n"); + printf("or check on the builtin SID: S-1-5-32\n"); + return NT_STATUS_OK; + } + + /* Get sam policy and domain handles */ + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + if (StrCaseCmp(argv[1], "domain")==0) + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + else if (StrCaseCmp(argv[1], "builtin")==0) + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &global_sid_Builtin, &domain_pol); + else + return NT_STATUS_OK; + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Look up names */ + + num_names = argc - 2; + names = (char **)talloc(mem_ctx, sizeof(char *) * num_names); + + for (i = 0; i < argc - 2; i++) + names[i] = argv[i + 2]; + + result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, + flags, num_names, names, + &num_rids, &rids, &name_types); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Display results */ + + for (i = 0; i < num_names; i++) + printf("name %s: 0x%x (%d)\n", names[i], rids[i], + name_types[i]); + + done: + return result; +} + +/* Lookup sam rids */ + +static NTSTATUS cmd_samr_lookup_rids(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + POLICY_HND connect_pol, domain_pol; + uint32 flags = 0x000003e8; /* Unknown */ + uint32 num_rids, num_names, *rids, *name_types; + char **names; + int i; + + if (argc < 2) { + printf("Usage: %s rid1 [rid2 [rid3] [...]]\n", argv[0]); + return NT_STATUS_OK; + } + + /* Get sam policy and domain handles */ + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Look up rids */ + + num_rids = argc - 1; + rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids); + + for (i = 0; i < argc - 1; i++) + sscanf(argv[i + 1], "%i", &rids[i]); + + result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol, + flags, num_rids, rids, + &num_names, &names, &name_types); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Display results */ + + for (i = 0; i < num_names; i++) + printf("rid 0x%x: %s (%d)\n", rids[i], names[i], name_types[i]); + + done: + return result; +} + +/* Delete domain user */ + +static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + POLICY_HND connect_pol, domain_pol, user_pol; + + if (argc != 2) { + printf("Usage: %s username\n", argv[0]); + return NT_STATUS_OK; + } + + /* Get sam policy and domain handles */ + + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Get handle on user */ + + { + uint32 *user_rids, num_rids, *name_types; + uint32 flags = 0x000003e8; /* Unknown */ + + result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, + flags, 1, &argv[1], + &num_rids, &user_rids, + &name_types); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + result = cli_samr_open_user(cli, mem_ctx, &domain_pol, + MAXIMUM_ALLOWED_ACCESS, + user_rids[0], &user_pol); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + } + + /* Delete user */ + + result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Display results */ + + done: + return result; +} + +/********************************************************************** + * Query user security object + */ +static NTSTATUS cmd_samr_query_sec_obj(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND connect_pol, domain_pol, user_pol, *pol; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + uint32 info_level = 4; + fstring server; + uint32 user_rid = 0; + TALLOC_CTX *ctx = NULL; + SEC_DESC_BUF *sec_desc_buf=NULL; + BOOL domain = False; + + ctx=talloc_init(); + + if (argc > 2) { + printf("Usage: %s [rid|-d]\n", argv[0]); + printf("\tSpecify rid for security on user, -d for security on domain\n"); + return NT_STATUS_OK; + } + + if (argc == 2) { + if (strcmp(argv[1], "-d") == 0) + domain = True; + else + sscanf(argv[1], "%i", &user_rid); + } + + slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (server); + result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, + &connect_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + if (domain || user_rid) + result = cli_samr_open_domain(cli, mem_ctx, &connect_pol, + MAXIMUM_ALLOWED_ACCESS, + &domain_sid, &domain_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + if (user_rid) + result = cli_samr_open_user(cli, mem_ctx, &domain_pol, + MAXIMUM_ALLOWED_ACCESS, + user_rid, &user_pol); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* Pick which query pol to use */ + + pol = &connect_pol; + + if (domain) + pol = &domain_pol; + + if (user_rid) + pol = &user_pol; + + /* Query SAM security object */ + + result = cli_samr_query_sec_obj(cli, mem_ctx, pol, info_level, ctx, + &sec_desc_buf); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + display_sec_desc(sec_desc_buf->sec); + +done: + talloc_destroy(ctx); + return result; +} + +/* List of commands exported by this module */ + +struct cmd_set samr_commands[] = { + + { "SAMR" }, + + { "queryuser", cmd_samr_query_user, PIPE_SAMR, "Query user info", "" }, + { "querygroup", cmd_samr_query_group, PIPE_SAMR, "Query group info", "" }, + { "queryusergroups", cmd_samr_query_usergroups, PIPE_SAMR, "Query user groups", "" }, + { "queryuseraliases", cmd_samr_query_useraliases, PIPE_SAMR, "Query user aliases", "" }, + { "querygroupmem", cmd_samr_query_groupmem, PIPE_SAMR, "Query group membership", "" }, + { "queryaliasmem", cmd_samr_query_aliasmem, PIPE_SAMR, "Query alias membership", "" }, + { "querydispinfo", cmd_samr_query_dispinfo, PIPE_SAMR, "Query display info", "" }, + { "querydominfo", cmd_samr_query_dominfo, PIPE_SAMR, "Query domain info", "" }, + { "enumdomgroups", cmd_samr_enum_dom_groups, PIPE_SAMR, "Enumerate domain groups", "" }, + { "enumalsgroups", cmd_samr_enum_als_groups, PIPE_SAMR, "Enumerate alias groups", "" }, + + { "createdomuser", cmd_samr_create_dom_user, PIPE_SAMR, "Create domain user", "" }, + { "samlookupnames", cmd_samr_lookup_names, PIPE_SAMR, "Look up names", "" }, + { "samlookuprids", cmd_samr_lookup_rids, PIPE_SAMR, "Look up names", "" }, + { "deletedomuser", cmd_samr_delete_dom_user, PIPE_SAMR, "Delete domain user", "" }, + { "samquerysecobj", cmd_samr_query_sec_obj, PIPE_SAMR, "Query SAMR security object", "" }, + + { NULL } +}; diff --git a/source3/rpcclient/cmd_spoolss.c b/source3/rpcclient/cmd_spoolss.c new file mode 100644 index 0000000000..64a84e25df --- /dev/null +++ b/source3/rpcclient/cmd_spoolss.c @@ -0,0 +1,1710 @@ +/* + Unix SMB/CIFS implementation. + RPC pipe client + + Copyright (C) Gerald Carter 2001 + Copyright (C) Tim Potter 2000 + Copyright (C) Andrew Tridgell 1992-1999 + Copyright (C) Luke Kenneth Casson Leighton 1996-1999 + + 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" +#include "rpcclient.h" + +struct table_node { + char *long_archi; + char *short_archi; + int version; +}; + +struct table_node archi_table[]= { + + {"Windows 4.0", "WIN40", 0 }, + {"Windows NT x86", "W32X86", 2 }, + {"Windows NT R4000", "W32MIPS", 2 }, + {"Windows NT Alpha_AXP", "W32ALPHA", 2 }, + {"Windows NT PowerPC", "W32PPC", 2 }, + {NULL, "", -1 } +}; + +/**************************************************************************** +function to do the mapping between the long architecture name and +the short one. +****************************************************************************/ +BOOL get_short_archi(char *short_archi, char *long_archi) +{ + int i=-1; + + DEBUG(107,("Getting architecture dependant directory\n")); + do { + i++; + } while ( (archi_table[i].long_archi!=NULL ) && + StrCaseCmp(long_archi, archi_table[i].long_archi) ); + + if (archi_table[i].long_archi==NULL) { + DEBUGADD(10,("Unknown architecture [%s] !\n", long_archi)); + return False; + } + + StrnCpy (short_archi, archi_table[i].short_archi, strlen(archi_table[i].short_archi)); + + DEBUGADD(108,("index: [%d]\n", i)); + DEBUGADD(108,("long architecture: [%s]\n", long_archi)); + DEBUGADD(108,("short architecture: [%s]\n", short_archi)); + + return True; +} + + +/********************************************************************** + * dummy function -- placeholder + */ +static NTSTATUS cmd_spoolss_not_implemented(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + printf ("(*) This command is not currently implemented.\n"); + return NT_STATUS_OK; +} + +/*********************************************************************** + * Get printer information + */ +static NTSTATUS cmd_spoolss_open_printer_ex(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + WERROR werror; + pstring printername; + fstring servername, user; + POLICY_HND hnd; + + if (argc != 2) { + printf("Usage: %s <printername>\n", argv[0]); + return NT_STATUS_OK; + } + + if (!cli) + return NT_STATUS_UNSUCCESSFUL; + + slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (servername); + fstrcpy (user, cli->user_name); + fstrcpy (printername, argv[1]); + + /* Open the printer handle */ + + werror = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, + "", MAXIMUM_ALLOWED_ACCESS, + servername, user, &hnd); + + if (W_ERROR_IS_OK(werror)) { + printf("Printer %s opened successfully\n", printername); + werror = cli_spoolss_close_printer(cli, mem_ctx, &hnd); + + if (!W_ERROR_IS_OK(werror)) { + printf("Error closing printer handle! (%s)\n", + get_dos_error_msg(werror)); + } + } + + return W_ERROR_IS_OK(werror) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + + +/**************************************************************************** +printer info level 0 display function +****************************************************************************/ +static void display_print_info_0(PRINTER_INFO_0 *i0) +{ + fstring name = ""; + fstring servername = ""; + + if (!i0) + return; + + if (i0->printername.buffer) + rpcstr_pull(name, i0->printername.buffer, sizeof(name), 0, STR_TERMINATE); + + if (i0->servername.buffer) + rpcstr_pull(servername, i0->servername.buffer, sizeof(servername), 0,STR_TERMINATE); + + printf("\tprintername:[%s]\n", name); + printf("\tservername:[%s]\n", servername); + printf("\tcjobs:[0x%x]\n", i0->cjobs); + printf("\ttotal_jobs:[0x%x]\n", i0->total_jobs); + + printf("\t:date: [%d]-[%d]-[%d] (%d)\n", i0->year, i0->month, + i0->day, i0->dayofweek); + printf("\t:time: [%d]-[%d]-[%d]-[%d]\n", i0->hour, i0->minute, + i0->second, i0->milliseconds); + + printf("\tglobal_counter:[0x%x]\n", i0->global_counter); + printf("\ttotal_pages:[0x%x]\n", i0->total_pages); + + printf("\tmajorversion:[0x%x]\n", i0->major_version); + printf("\tbuildversion:[0x%x]\n", i0->build_version); + + printf("\tunknown7:[0x%x]\n", i0->unknown7); + printf("\tunknown8:[0x%x]\n", i0->unknown8); + printf("\tunknown9:[0x%x]\n", i0->unknown9); + printf("\tsession_counter:[0x%x]\n", i0->session_counter); + printf("\tunknown11:[0x%x]\n", i0->unknown11); + printf("\tprinter_errors:[0x%x]\n", i0->printer_errors); + printf("\tunknown13:[0x%x]\n", i0->unknown13); + printf("\tunknown14:[0x%x]\n", i0->unknown14); + printf("\tunknown15:[0x%x]\n", i0->unknown15); + printf("\tunknown16:[0x%x]\n", i0->unknown16); + printf("\tchange_id:[0x%x]\n", i0->change_id); + printf("\tunknown18:[0x%x]\n", i0->unknown18); + printf("\tstatus:[0x%x]\n", i0->status); + printf("\tunknown20:[0x%x]\n", i0->unknown20); + printf("\tc_setprinter:[0x%x]\n", i0->c_setprinter); + printf("\tunknown22:[0x%x]\n", i0->unknown22); + printf("\tunknown23:[0x%x]\n", i0->unknown23); + printf("\tunknown24:[0x%x]\n", i0->unknown24); + printf("\tunknown25:[0x%x]\n", i0->unknown25); + printf("\tunknown26:[0x%x]\n", i0->unknown26); + printf("\tunknown27:[0x%x]\n", i0->unknown27); + printf("\tunknown28:[0x%x]\n", i0->unknown28); + printf("\tunknown29:[0x%x]\n", i0->unknown29); + + printf("\n"); +} + +/**************************************************************************** +printer info level 1 display function +****************************************************************************/ +static void display_print_info_1(PRINTER_INFO_1 *i1) +{ + fstring desc = ""; + fstring name = ""; + fstring comm = ""; + + if (i1->description.buffer) + rpcstr_pull(desc, i1->description.buffer, sizeof(desc), 0, + STR_TERMINATE); + + if (i1->name.buffer) + rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, + STR_TERMINATE); + + if (i1->comment.buffer) + rpcstr_pull(comm, i1->comment.buffer, sizeof(comm), 0, + STR_TERMINATE); + + printf("\tflags:[0x%x]\n", i1->flags); + printf("\tname:[%s]\n", name); + printf("\tdescription:[%s]\n", desc); + printf("\tcomment:[%s]\n", comm); + + printf("\n"); +} + +/**************************************************************************** +printer info level 2 display function +****************************************************************************/ +static void display_print_info_2(PRINTER_INFO_2 *i2) +{ + fstring servername = ""; + fstring printername = ""; + fstring sharename = ""; + fstring portname = ""; + fstring drivername = ""; + fstring comment = ""; + fstring location = ""; + fstring sepfile = ""; + fstring printprocessor = ""; + fstring datatype = ""; + fstring parameters = ""; + + if (i2->servername.buffer) + rpcstr_pull(servername, i2->servername.buffer,sizeof(servername), 0, STR_TERMINATE); + + if (i2->printername.buffer) + rpcstr_pull(printername, i2->printername.buffer,sizeof(printername), 0, STR_TERMINATE); + + if (i2->sharename.buffer) + rpcstr_pull(sharename, i2->sharename.buffer,sizeof(sharename), 0, STR_TERMINATE); + + if (i2->portname.buffer) + rpcstr_pull(portname, i2->portname.buffer,sizeof(portname), 0, STR_TERMINATE); + + if (i2->drivername.buffer) + rpcstr_pull(drivername, i2->drivername.buffer,sizeof(drivername), 0, STR_TERMINATE); + + if (i2->comment.buffer) + rpcstr_pull(comment, i2->comment.buffer,sizeof(comment), 0, STR_TERMINATE); + + if (i2->location.buffer) + rpcstr_pull(location, i2->location.buffer,sizeof(location), 0, STR_TERMINATE); + + if (i2->sepfile.buffer) + rpcstr_pull(sepfile, i2->sepfile.buffer,sizeof(sepfile), 0, STR_TERMINATE); + + if (i2->printprocessor.buffer) + rpcstr_pull(printprocessor, i2->printprocessor.buffer,sizeof(printprocessor), 0, STR_TERMINATE); + + if (i2->datatype.buffer) + rpcstr_pull(datatype, i2->datatype.buffer,sizeof(datatype), 0, STR_TERMINATE); + + if (i2->parameters.buffer) + rpcstr_pull(parameters, i2->parameters.buffer,sizeof(parameters), 0, STR_TERMINATE); + + printf("\tservername:[%s]\n", servername); + printf("\tprintername:[%s]\n", printername); + printf("\tsharename:[%s]\n", sharename); + printf("\tportname:[%s]\n", portname); + printf("\tdrivername:[%s]\n", drivername); + printf("\tcomment:[%s]\n", comment); + printf("\tlocation:[%s]\n", location); + printf("\tsepfile:[%s]\n", sepfile); + printf("\tprintprocessor:[%s]\n", printprocessor); + printf("\tdatatype:[%s]\n", datatype); + printf("\tparameters:[%s]\n", parameters); + printf("\tattributes:[0x%x]\n", i2->attributes); + printf("\tpriority:[0x%x]\n", i2->priority); + printf("\tdefaultpriority:[0x%x]\n", i2->defaultpriority); + printf("\tstarttime:[0x%x]\n", i2->starttime); + printf("\tuntiltime:[0x%x]\n", i2->untiltime); + printf("\tstatus:[0x%x]\n", i2->status); + printf("\tcjobs:[0x%x]\n", i2->cjobs); + printf("\taverageppm:[0x%x]\n", i2->averageppm); + + if (i2->secdesc) + display_sec_desc(i2->secdesc); + + printf("\n"); +} + +/**************************************************************************** +printer info level 3 display function +****************************************************************************/ +static void display_print_info_3(PRINTER_INFO_3 *i3) +{ + printf("\tflags:[0x%x]\n", i3->flags); + + display_sec_desc(i3->secdesc); + + printf("\n"); +} + +/* Enumerate printers */ + +static NTSTATUS cmd_spoolss_enum_printers(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + WERROR result; + uint32 info_level = 1; + PRINTER_INFO_CTR ctr; + uint32 i = 0, num_printers, needed; + + if (argc > 2) + { + printf("Usage: %s [level]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc == 2) { + info_level = atoi(argv[1]); + } + + /* Enumerate printers -- Should we enumerate types other + than PRINTER_ENUM_LOCAL? Maybe accept as a parameter? --jerry */ + + ZERO_STRUCT(ctr); + + result = cli_spoolss_enum_printers( + cli, mem_ctx, 0, &needed, PRINTER_ENUM_LOCAL, + info_level, &num_printers, &ctr); + + if (W_ERROR_V(result) == ERRinsufficientbuffer) + result = cli_spoolss_enum_printers( + cli, mem_ctx, needed, NULL, PRINTER_ENUM_LOCAL, + info_level, &num_printers, &ctr); + + if (W_ERROR_IS_OK(result)) { + + if (!num_printers) { + printf ("No printers returned.\n"); + goto done; + } + + for (i = 0; i < num_printers; i++) { + switch(info_level) { + case 0: + display_print_info_0(&ctr.printers_0[i]); + break; + case 1: + display_print_info_1(&ctr.printers_1[i]); + break; + case 2: + display_print_info_2(&ctr.printers_2[i]); + break; + case 3: + display_print_info_3(&ctr.printers_3[i]); + break; + default: + printf("unknown info level %d\n", info_level); + goto done; + } + } + } + done: + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/**************************************************************************** +port info level 1 display function +****************************************************************************/ +static void display_port_info_1(PORT_INFO_1 *i1) +{ + fstring buffer; + + rpcstr_pull(buffer, i1->port_name.buffer, sizeof(buffer), 0, STR_TERMINATE); + printf("\tPort Name:\t[%s]\n", buffer); +} + +/**************************************************************************** +port info level 2 display function +****************************************************************************/ +static void display_port_info_2(PORT_INFO_2 *i2) +{ + fstring buffer; + + rpcstr_pull(buffer, i2->port_name.buffer, sizeof(buffer), 0, STR_TERMINATE); + printf("\tPort Name:\t[%s]\n", buffer); + rpcstr_pull(buffer, i2->monitor_name.buffer, sizeof(buffer), 0, STR_TERMINATE); + + printf("\tMonitor Name:\t[%s]\n", buffer); + rpcstr_pull(buffer, i2->description.buffer, sizeof(buffer), 0, STR_TERMINATE); + + printf("\tDescription:\t[%s]\n", buffer); + printf("\tPort Type:\t[%d]\n", i2->port_type); + printf("\tReserved:\t[%d]\n", i2->reserved); + printf("\n"); +} + +/* Enumerate ports */ + +static NTSTATUS cmd_spoolss_enum_ports(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + WERROR result; + uint32 needed, info_level = 1; + PORT_INFO_CTR ctr; + int returned; + + if (argc > 2) { + printf("Usage: %s [level]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc == 2) + info_level = atoi(argv[1]); + + /* Enumerate ports */ + + ZERO_STRUCT(ctr); + + result = cli_spoolss_enum_ports(cli, mem_ctx, 0, &needed, info_level, + &returned, &ctr); + + if (W_ERROR_V(result) == ERRinsufficientbuffer) + result = cli_spoolss_enum_ports(cli, mem_ctx, needed, NULL, + info_level, &returned, &ctr); + + if (W_ERROR_IS_OK(result)) { + int i; + + for (i = 0; i < returned; i++) { + switch (info_level) { + case 1: + display_port_info_1(&ctr.port.info_1[i]); + break; + case 2: + display_port_info_2(&ctr.port.info_2[i]); + break; + default: + printf("unknown info level %d\n", info_level); + break; + } + } + } + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/*********************************************************************** + * Set printer comment - use a level2 set. + */ +static NTSTATUS cmd_spoolss_setprinter(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND pol; + WERROR result; + uint32 needed; + uint32 info_level = 2; + BOOL opened_hnd = False; + PRINTER_INFO_CTR ctr; + fstring printername, + servername, + user, + comment; + + if (argc == 1 || argc > 3) { + printf("Usage: %s printername comment\n", argv[0]); + + return NT_STATUS_OK; + } + + /* Open a printer handle */ + if (argc == 3) { + fstrcpy(comment, argv[2]); + } + + slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (servername); + fstrcpy (printername, argv[1]); + fstrcpy (user, cli->user_name); + + /* get a printer handle */ + result = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, "", + MAXIMUM_ALLOWED_ACCESS, servername, + user, &pol); + + if (!W_ERROR_IS_OK(result)) + goto done; + + opened_hnd = True; + + /* Get printer info */ + result = cli_spoolss_getprinter(cli, mem_ctx, 0, &needed, &pol, info_level, &ctr); + + if (W_ERROR_V(result) == ERRinsufficientbuffer) + result = cli_spoolss_getprinter(cli, mem_ctx, needed, NULL, &pol, info_level, &ctr); + + if (!W_ERROR_IS_OK(result)) + goto done; + + + /* Modify the comment. */ + init_unistr(&ctr.printers_2->comment, comment); + ctr.printers_2->devmode = NULL; + ctr.printers_2->secdesc = NULL; + + result = cli_spoolss_setprinter(cli, mem_ctx, &pol, info_level, &ctr, 0); + if (W_ERROR_IS_OK(result)) + printf("Success in setting comment.\n"); + + done: + if (opened_hnd) + cli_spoolss_close_printer(cli, mem_ctx, &pol); + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/*********************************************************************** + * Get printer information + */ +static NTSTATUS cmd_spoolss_getprinter(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND pol; + WERROR result; + uint32 info_level = 1; + BOOL opened_hnd = False; + PRINTER_INFO_CTR ctr; + fstring printername, + servername, + user; + uint32 needed; + + if (argc == 1 || argc > 3) { + printf("Usage: %s <printername> [level]\n", argv[0]); + return NT_STATUS_OK; + } + + /* Open a printer handle */ + if (argc == 3) { + info_level = atoi(argv[2]); + } + + slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (servername); + slprintf (printername, sizeof(fstring)-1, "%s\\%s", servername, argv[1]); + fstrcpy (user, cli->user_name); + + /* get a printer handle */ + + result = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, + "", MAXIMUM_ALLOWED_ACCESS, + servername, user, &pol); + + if (!W_ERROR_IS_OK(result)) + goto done; + + opened_hnd = True; + + /* Get printer info */ + + result = cli_spoolss_getprinter(cli, mem_ctx, 0, &needed, + &pol, info_level, &ctr); + + if (W_ERROR_V(result) == ERRinsufficientbuffer) + result = cli_spoolss_getprinter( + cli, mem_ctx, needed, NULL, &pol, info_level, &ctr); + + if (!W_ERROR_IS_OK(result)) + goto done; + + /* Display printer info */ + + switch (info_level) { + case 0: + display_print_info_0(ctr.printers_0); + break; + case 1: + display_print_info_1(ctr.printers_1); + break; + case 2: + display_print_info_2(ctr.printers_2); + break; + case 3: + display_print_info_3(ctr.printers_3); + break; + default: + printf("unknown info level %d\n", info_level); + break; + } + + done: + if (opened_hnd) + cli_spoolss_close_printer(cli, mem_ctx, &pol); + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/**************************************************************************** +printer info level 0 display function +****************************************************************************/ +static void display_print_driver_1(DRIVER_INFO_1 *i1) +{ + fstring name; + if (i1 == NULL) + return; + + rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE); + + printf ("Printer Driver Info 1:\n"); + printf ("\tDriver Name: [%s]\n\n", name); + + return; +} + +/**************************************************************************** +printer info level 1 display function +****************************************************************************/ +static void display_print_driver_2(DRIVER_INFO_2 *i1) +{ + fstring name; + fstring architecture; + fstring driverpath; + fstring datafile; + fstring configfile; + if (i1 == NULL) + return; + + rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE); + rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), 0, STR_TERMINATE); + rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), 0, STR_TERMINATE); + rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), 0, STR_TERMINATE); + rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), 0, STR_TERMINATE); + + printf ("Printer Driver Info 2:\n"); + printf ("\tVersion: [%x]\n", i1->version); + printf ("\tDriver Name: [%s]\n", name); + printf ("\tArchitecture: [%s]\n", architecture); + printf ("\tDriver Path: [%s]\n", driverpath); + printf ("\tDatafile: [%s]\n", datafile); + printf ("\tConfigfile: [%s]\n\n", configfile); + + return; +} + +/**************************************************************************** +printer info level 2 display function +****************************************************************************/ +static void display_print_driver_3(DRIVER_INFO_3 *i1) +{ + fstring name; + fstring architecture; + fstring driverpath; + fstring datafile; + fstring configfile; + fstring helpfile; + fstring dependentfiles; + fstring monitorname; + fstring defaultdatatype; + + int length=0; + BOOL valid = True; + + if (i1 == NULL) + return; + + rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE); + rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), 0, STR_TERMINATE); + rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), 0, STR_TERMINATE); + rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), 0, STR_TERMINATE); + rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), 0, STR_TERMINATE); + rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), 0, STR_TERMINATE); + rpcstr_pull(monitorname, i1->monitorname.buffer, sizeof(monitorname), 0, STR_TERMINATE); + rpcstr_pull(defaultdatatype, i1->defaultdatatype.buffer, sizeof(defaultdatatype), 0, STR_TERMINATE); + + printf ("Printer Driver Info 3:\n"); + printf ("\tVersion: [%x]\n", i1->version); + printf ("\tDriver Name: [%s]\n",name); + printf ("\tArchitecture: [%s]\n", architecture); + printf ("\tDriver Path: [%s]\n", driverpath); + printf ("\tDatafile: [%s]\n", datafile); + printf ("\tConfigfile: [%s]\n", configfile); + printf ("\tHelpfile: [%s]\n\n", helpfile); + + while (valid) + { + rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), 0, STR_TERMINATE); + + length+=strlen(dependentfiles)+1; + + if (strlen(dependentfiles) > 0) + { + printf ("\tDependentfiles: [%s]\n", dependentfiles); + } + else + { + valid = False; + } + } + + printf ("\n"); + + printf ("\tMonitorname: [%s]\n", monitorname); + printf ("\tDefaultdatatype: [%s]\n\n", defaultdatatype); + + return; +} + +/*********************************************************************** + * Get printer information + */ +static NTSTATUS cmd_spoolss_getdriver(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND pol; + WERROR werror; + NTSTATUS result; + uint32 info_level = 3; + BOOL opened_hnd = False; + PRINTER_DRIVER_CTR ctr; + fstring printername, + servername, + user; + uint32 i; + + if ((argc == 1) || (argc > 3)) + { + printf("Usage: %s <printername> [level]\n", argv[0]); + return NT_STATUS_OK; + } + + /* get the arguments need to open the printer handle */ + slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (servername); + fstrcpy (user, cli->user_name); + fstrcpy (printername, argv[1]); + if (argc == 3) + info_level = atoi(argv[2]); + + /* Open a printer handle */ + + werror = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, "", + PRINTER_ACCESS_USE, + servername, user, &pol); + + result = W_ERROR_IS_OK(werror) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; + + if (!NT_STATUS_IS_OK(result)) { + printf("Error opening printer handle for %s!\n", printername); + return result; + } + + opened_hnd = True; + + /* loop through and print driver info level for each architecture */ + + for (i=0; archi_table[i].long_archi!=NULL; i++) { + uint32 needed; + + werror = cli_spoolss_getprinterdriver( + cli, mem_ctx, 0, &needed, &pol, info_level, + archi_table[i].long_archi, &ctr); + + if (W_ERROR_V(werror) == ERRinsufficientbuffer) + werror = cli_spoolss_getprinterdriver( + cli, mem_ctx, needed, NULL, &pol, info_level, + archi_table[i].long_archi, &ctr); + + if (!W_ERROR_IS_OK(werror)) + continue; + + printf ("\n[%s]\n", archi_table[i].long_archi); + + switch (info_level) { + case 1: + display_print_driver_1 (ctr.info1); + break; + case 2: + display_print_driver_2 (ctr.info2); + break; + case 3: + display_print_driver_3 (ctr.info3); + break; + default: + printf("unknown info level %d\n", info_level); + break; + } + } + + /* Cleanup */ + + if (opened_hnd) + cli_spoolss_close_printer (cli, mem_ctx, &pol); + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/*********************************************************************** + * Get printer information + */ +static NTSTATUS cmd_spoolss_enum_drivers(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + WERROR werror; + uint32 info_level = 1; + PRINTER_DRIVER_CTR ctr; + uint32 i, j, + returned; + + if (argc > 2) + { + printf("Usage: enumdrivers [level]\n"); + return NT_STATUS_OK; + } + + if (argc == 2) + info_level = atoi(argv[1]); + + + /* loop through and print driver info level for each architecture */ + for (i=0; archi_table[i].long_archi!=NULL; i++) + { + uint32 needed; + + werror = cli_spoolss_enumprinterdrivers( + cli, mem_ctx, 0, &needed, info_level, + archi_table[i].long_archi, &returned, &ctr); + + if (W_ERROR_V(werror) == ERRinsufficientbuffer) + werror = cli_spoolss_enumprinterdrivers( + cli, mem_ctx, needed, NULL, info_level, + archi_table[i].long_archi, &returned, &ctr); + + if (returned == 0) + continue; + + if (!W_ERROR_IS_OK(werror)) { + printf ("Error getting driver for environment [%s] - %d\n", + archi_table[i].long_archi, W_ERROR_V(werror)); + continue; + } + + printf ("\n[%s]\n", archi_table[i].long_archi); + switch (info_level) + { + + case 1: + for (j=0; j < returned; j++) { + display_print_driver_1 (&(ctr.info1[j])); + } + break; + case 2: + for (j=0; j < returned; j++) { + display_print_driver_2 (&(ctr.info2[j])); + } + break; + case 3: + for (j=0; j < returned; j++) { + display_print_driver_3 (&(ctr.info3[j])); + } + break; + default: + printf("unknown info level %d\n", info_level); + break; + } + } + + return W_ERROR_IS_OK(werror) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/**************************************************************************** +printer info level 1 display function +****************************************************************************/ +static void display_printdriverdir_1(DRIVER_DIRECTORY_1 *i1) +{ + fstring name; + if (i1 == NULL) + return; + + rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE); + + printf ("\tDirectory Name:[%s]\n", name); +} + +/*********************************************************************** + * Get printer driver directory information + */ +static NTSTATUS cmd_spoolss_getdriverdir(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + WERROR result; + fstring env; + DRIVER_DIRECTORY_CTR ctr; + uint32 needed; + + if (argc > 2) { + printf("Usage: %s [environment]\n", argv[0]); + return NT_STATUS_OK; + } + + /* Get the arguments need to open the printer handle */ + + if (argc == 2) + fstrcpy (env, argv[1]); + else + fstrcpy (env, "Windows NT x86"); + + /* Get the directory. Only use Info level 1 */ + + result = cli_spoolss_getprinterdriverdir( + cli, mem_ctx, 0, &needed, 1, env, &ctr); + + if (W_ERROR_V(result) == ERRinsufficientbuffer) + result = cli_spoolss_getprinterdriverdir( + cli, mem_ctx, needed, NULL, 1, env, &ctr); + + if (W_ERROR_IS_OK(result)) + display_printdriverdir_1(ctr.info1); + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/******************************************************************************* + set the version and environment fields of a DRIVER_INFO_3 struct + ******************************************************************************/ +void set_drv_info_3_env (DRIVER_INFO_3 *info, const char *arch) +{ + + int i; + + for (i=0; archi_table[i].long_archi != NULL; i++) + { + if (strcmp(arch, archi_table[i].short_archi) == 0) + { + info->version = archi_table[i].version; + init_unistr (&info->architecture, archi_table[i].long_archi); + break; + } + } + + if (archi_table[i].long_archi == NULL) + { + DEBUG(0, ("set_drv_info_3_env: Unknown arch [%s]\n", arch)); + } + + return; +} + + +/************************************************************************** + wrapper for strtok to get the next parameter from a delimited list. + Needed to handle the empty parameter string denoted by "NULL" + *************************************************************************/ +static char* get_driver_3_param (char* str, char* delim, UNISTR* dest) +{ + char *ptr; + + /* get the next token */ + ptr = strtok(str, delim); + + /* a string of 'NULL' is used to represent an empty + parameter because two consecutive delimiters + will not return an empty string. See man strtok(3) + for details */ + if (StrCaseCmp(ptr, "NULL") == 0) + ptr = NULL; + + if (dest != NULL) + init_unistr(dest, ptr); + + return ptr; +} + +/******************************************************************************** + fill in the members of a DRIVER_INFO_3 struct using a character + string in the form of + <Long Printer Name>:<Driver File Name>:<Data File Name>:\ + <Config File Name>:<Help File Name>:<Language Monitor Name>:\ + <Default Data Type>:<Comma Separated list of Files> + *******************************************************************************/ +static BOOL init_drv_info_3_members ( + TALLOC_CTX *mem_ctx, + DRIVER_INFO_3 *info, + char *args +) +{ + char *str, *str2; + uint32 len, i; + + /* fill in the UNISTR fields */ + str = get_driver_3_param (args, ":", &info->name); + str = get_driver_3_param (NULL, ":", &info->driverpath); + str = get_driver_3_param (NULL, ":", &info->datafile); + str = get_driver_3_param (NULL, ":", &info->configfile); + str = get_driver_3_param (NULL, ":", &info->helpfile); + str = get_driver_3_param (NULL, ":", &info->monitorname); + str = get_driver_3_param (NULL, ":", &info->defaultdatatype); + + /* <Comma Separated List of Dependent Files> */ + str2 = get_driver_3_param (NULL, ":", NULL); /* save the beginning of the string */ + str = str2; + + /* begin to strip out each filename */ + str = strtok(str, ","); + len = 0; + while (str != NULL) + { + /* keep a cumlative count of the str lengths */ + len += strlen(str)+1; + str = strtok(NULL, ","); + } + + /* allocate the space; add one extra slot for a terminating NULL. + Each filename is NULL terminated and the end contains a double + NULL */ + if ((info->dependentfiles=(uint16*)talloc(mem_ctx, (len+1)*sizeof(uint16))) == NULL) + { + DEBUG(0,("init_drv_info_3_members: Unable to malloc memory for dependenfiles\n")); + return False; + } + for (i=0; i<len; i++) + { + info->dependentfiles[i] = SSVAL(&info->dependentfiles[i], 0, str2[i]); + } + info->dependentfiles[len] = '\0'; + + return True; +} + + +static NTSTATUS cmd_spoolss_addprinterdriver(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + WERROR result; + uint32 level = 3; + PRINTER_DRIVER_CTR ctr; + DRIVER_INFO_3 info3; + fstring arch; + fstring driver_name; + + /* parse the command arguements */ + if (argc != 3) + { + printf ("Usage: %s <Environment>\\\n", argv[0]); + printf ("\t<Long Printer Name>:<Driver File Name>:<Data File Name>:\\\n"); + printf ("\t<Config File Name>:<Help File Name>:<Language Monitor Name>:\\\n"); + printf ("\t<Default Data Type>:<Comma Separated list of Files>\n"); + + return NT_STATUS_OK; + } + + /* Fill in the DRIVER_INFO_3 struct */ + ZERO_STRUCT(info3); + if (!get_short_archi(arch, argv[1])) + { + printf ("Error Unknown architechture [%s]\n", argv[1]); + return NT_STATUS_INVALID_PARAMETER; + } + else + set_drv_info_3_env(&info3, arch); + + if (!init_drv_info_3_members(mem_ctx, &info3, argv[2])) + { + printf ("Error Invalid parameter list - %s.\n", argv[2]); + return NT_STATUS_INVALID_PARAMETER; + } + + + ctr.info3 = &info3; + result = cli_spoolss_addprinterdriver (cli, mem_ctx, level, &ctr); + + if (W_ERROR_IS_OK(result)) { + rpcstr_pull(driver_name, info3.name.buffer, + sizeof(driver_name), 0, STR_TERMINATE); + printf ("Printer Driver %s successfully installed.\n", + driver_name); + } + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + + +static NTSTATUS cmd_spoolss_addprinterex(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + WERROR result; + uint32 level = 2; + PRINTER_INFO_CTR ctr; + PRINTER_INFO_2 info2; + fstring servername; + + /* parse the command arguements */ + if (argc != 5) + { + printf ("Usage: %s <name> <shared name> <driver> <port>\n", argv[0]); + return NT_STATUS_OK; + } + + slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (servername); + + /* Fill in the DRIVER_INFO_3 struct */ + ZERO_STRUCT(info2); +#if 0 /* JERRY */ + init_unistr( &info2.servername, servername); +#endif + init_unistr( &info2.printername, argv[1]); + init_unistr( &info2.sharename, argv[2]); + init_unistr( &info2.drivername, argv[3]); + init_unistr( &info2.portname, argv[4]); + init_unistr( &info2.comment, "Created by rpcclient"); + init_unistr( &info2.printprocessor, "winprint"); + init_unistr( &info2.datatype, "RAW"); + info2.devmode = NULL; + info2.secdesc = NULL; + info2.attributes = PRINTER_ATTRIBUTE_SHARED; + info2.priority = 0; + info2.defaultpriority = 0; + info2.starttime = 0; + info2.untiltime = 0; + + /* These three fields must not be used by AddPrinter() + as defined in the MS Platform SDK documentation.. + --jerry + info2.status = 0; + info2.cjobs = 0; + info2.averageppm = 0; + */ + + ctr.printers_2 = &info2; + result = cli_spoolss_addprinterex (cli, mem_ctx, level, &ctr); + + if (W_ERROR_IS_OK(result)) + printf ("Printer %s successfully installed.\n", argv[1]); + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +static NTSTATUS cmd_spoolss_setdriver(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND pol; + WERROR result; + uint32 level = 2; + BOOL opened_hnd = False; + PRINTER_INFO_CTR ctr; + PRINTER_INFO_2 info2; + fstring servername, + printername, + user; + uint32 needed; + + /* parse the command arguements */ + if (argc != 3) + { + printf ("Usage: %s <printer> <driver>\n", argv[0]); + return NT_STATUS_OK; + } + + slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (servername); + slprintf (printername, sizeof(fstring)-1, "%s\\%s", servername, argv[1]); + fstrcpy (user, cli->user_name); + + /* Get a printer handle */ + + result = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, "", + MAXIMUM_ALLOWED_ACCESS, + servername, user, &pol); + + if (!W_ERROR_IS_OK(result)) + goto done; + + opened_hnd = True; + + /* Get printer info */ + + ZERO_STRUCT (info2); + ctr.printers_2 = &info2; + + result = cli_spoolss_getprinter(cli, mem_ctx, 0, &needed, + &pol, level, &ctr); + + if (W_ERROR_V(result) == ERRinsufficientbuffer) + result = cli_spoolss_getprinter( + cli, mem_ctx, needed, NULL, &pol, level, &ctr); + + if (!W_ERROR_IS_OK(result)) { + printf ("Unable to retrieve printer information!\n"); + goto done; + } + + /* Set the printer driver */ + + init_unistr(&ctr.printers_2->drivername, argv[2]); + + result = cli_spoolss_setprinter(cli, mem_ctx, &pol, level, &ctr, 0); + + if (!W_ERROR_IS_OK(result)) { + printf("SetPrinter call failed!\n"); + goto done;; + } + + printf("Succesfully set %s to driver %s.\n", argv[1], argv[2]); + +done: + /* Cleanup */ + + if (opened_hnd) + cli_spoolss_close_printer(cli, mem_ctx, &pol); + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + + +static NTSTATUS cmd_spoolss_deletedriver(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + WERROR result; + fstring servername; + int i; + + /* parse the command arguements */ + if (argc != 2) + { + printf ("Usage: %s <driver>\n", argv[0]); + return NT_STATUS_OK; + } + + slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (servername); + + /* delete the driver for all architectures */ + for (i=0; archi_table[i].long_archi; i++) + { + /* make the call to remove the driver */ + result = cli_spoolss_deleteprinterdriver( + cli, mem_ctx, archi_table[i].long_archi, argv[1]); + + if (!W_ERROR_IS_OK(result)) { + printf ("Failed to remove driver %s for arch [%s] - error 0x%x!\n", + argv[1], archi_table[i].long_archi, + W_ERROR_V(result)); + } else + printf ("Driver %s removed for arch [%s].\n", argv[1], + archi_table[i].long_archi); + } + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +static NTSTATUS cmd_spoolss_getprintprocdir(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + WERROR result; + char *servername = NULL, *environment = NULL; + fstring procdir; + uint32 needed; + + /* parse the command arguements */ + if (argc > 2) { + printf ("Usage: %s [environment]\n", argv[0]); + return NT_STATUS_OK; + } + + if (asprintf(&servername, "\\\\%s", cli->desthost) < 0) + return NT_STATUS_NO_MEMORY; + strupper(servername); + + if (asprintf(&environment, "%s", (argc == 2) ? argv[1] : + PRINTER_DRIVER_ARCHITECTURE) < 0) { + SAFE_FREE(servername); + return NT_STATUS_NO_MEMORY; + } + + result = cli_spoolss_getprintprocessordirectory( + cli, mem_ctx, 0, &needed, servername, environment, procdir); + + if (W_ERROR_V(result) == ERRinsufficientbuffer) + result = cli_spoolss_getprintprocessordirectory( + cli, mem_ctx, needed, NULL, servername, environment, + procdir); + + if (W_ERROR_IS_OK(result)) + printf("%s\n", procdir); + + SAFE_FREE(servername); + SAFE_FREE(environment); + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/* Add a form */ + +static NTSTATUS cmd_spoolss_addform(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND handle; + WERROR werror; + char *servername = NULL, *printername = NULL; + FORM form; + BOOL got_handle = False; + + /* Parse the command arguements */ + + if (argc != 3) { + printf ("Usage: %s <printer> <formname>\n", argv[0]); + return NT_STATUS_OK; + } + + /* Get a printer handle */ + + asprintf(&servername, "\\\\%s", cli->desthost); + strupper(servername); + asprintf(&printername, "%s\\%s", servername, argv[1]); + + werror = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, "", + MAXIMUM_ALLOWED_ACCESS, + servername, cli->user_name, &handle); + + if (!W_ERROR_IS_OK(werror)) + goto done; + + got_handle = True; + + /* Dummy up some values for the form data */ + + form.flags = FORM_USER; + form.size_x = form.size_y = 100; + form.left = 0; + form.top = 10; + form.right = 20; + form.bottom = 30; + + init_unistr2(&form.name, argv[2], strlen(argv[2]) + 1); + + /* Add the form */ + + + werror = cli_spoolss_addform(cli, mem_ctx, &handle, 1, &form); + + done: + if (got_handle) + cli_spoolss_close_printer(cli, mem_ctx, &handle); + + SAFE_FREE(servername); + SAFE_FREE(printername); + + return W_ERROR_IS_OK(werror) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/* Set a form */ + +static NTSTATUS cmd_spoolss_setform(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND handle; + WERROR werror; + char *servername = NULL, *printername = NULL; + FORM form; + BOOL got_handle = False; + + /* Parse the command arguements */ + + if (argc != 3) { + printf ("Usage: %s <printer> <formname>\n", argv[0]); + return NT_STATUS_OK; + } + + /* Get a printer handle */ + + asprintf(&servername, "\\\\%s", cli->desthost); + strupper(servername); + asprintf(&printername, "%s\\%s", servername, argv[1]); + + werror = cli_spoolss_open_printer_ex( + cli, mem_ctx, printername, "", MAXIMUM_ALLOWED_ACCESS, + servername, cli->user_name, &handle); + + if (!W_ERROR_IS_OK(werror)) + goto done; + + got_handle = True; + + /* Dummy up some values for the form data */ + + form.flags = FORM_PRINTER; + form.size_x = form.size_y = 100; + form.left = 0; + form.top = 1000; + form.right = 2000; + form.bottom = 3000; + + init_unistr2(&form.name, argv[2], strlen(argv[2]) + 1); + + /* Set the form */ + + werror = cli_spoolss_setform(cli, mem_ctx, &handle, 1, argv[2], &form); + + done: + if (got_handle) + cli_spoolss_close_printer(cli, mem_ctx, &handle); + + SAFE_FREE(servername); + SAFE_FREE(printername); + + return W_ERROR_IS_OK(werror) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/* Get a form */ + +static NTSTATUS cmd_spoolss_getform(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + POLICY_HND handle; + WERROR werror; + char *servername = NULL, *printername = NULL; + FORM_1 form; + BOOL got_handle = False; + uint32 needed; + + /* Parse the command arguements */ + + if (argc != 3) { + printf ("Usage: %s <printer> <formname>\n", argv[0]); + return NT_STATUS_OK; + } + + /* Get a printer handle */ + + asprintf(&servername, "\\\\%s", cli->desthost); + strupper(servername); + asprintf(&printername, "%s\\%s", servername, argv[1]); + + werror = cli_spoolss_open_printer_ex( + cli, mem_ctx, printername, "", MAXIMUM_ALLOWED_ACCESS, + servername, cli->user_name, &handle); + + if (!W_ERROR_IS_OK(werror)) + goto done; + + got_handle = True; + + /* Set the form */ + + werror = cli_spoolss_getform(cli, mem_ctx, 0, &needed, + &handle, argv[2], 1, &form); + + if (W_ERROR_V(werror) == ERRinsufficientbuffer) + werror = cli_spoolss_getform(cli, mem_ctx, needed, NULL, + &handle, argv[2], 1, &form); + + if (!W_ERROR_IS_OK(werror)) + goto done; + + printf("width: %d\n", form.width); + printf("length: %d\n", form.length); + printf("left: %d\n", form.left); + printf("top: %d\n", form.top); + printf("right: %d\n", form.right); + printf("bottom: %d\n", form.bottom); + + done: + if (got_handle) + cli_spoolss_close_printer(cli, mem_ctx, &handle); + + SAFE_FREE(servername); + SAFE_FREE(printername); + + return W_ERROR_IS_OK(werror) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/* Delete a form */ + +static NTSTATUS cmd_spoolss_deleteform(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND handle; + WERROR werror; + char *servername = NULL, *printername = NULL; + BOOL got_handle = False; + + /* Parse the command arguements */ + + if (argc != 3) { + printf ("Usage: %s <printer> <formname>\n", argv[0]); + return NT_STATUS_OK; + } + + /* Get a printer handle */ + + asprintf(&servername, "\\\\%s", cli->desthost); + strupper(servername); + asprintf(&printername, "%s\\%s", servername, argv[1]); + + werror = cli_spoolss_open_printer_ex( + cli, mem_ctx, printername, "", MAXIMUM_ALLOWED_ACCESS, + servername, cli->user_name, &handle); + + if (!W_ERROR_IS_OK(werror)) + goto done; + + got_handle = True; + + /* Delete the form */ + + werror = cli_spoolss_deleteform(cli, mem_ctx, &handle, argv[2]); + + done: + if (got_handle) + cli_spoolss_close_printer(cli, mem_ctx, &handle); + + SAFE_FREE(servername); + SAFE_FREE(printername); + + return W_ERROR_IS_OK(werror) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +/* Enumerate forms */ + +static NTSTATUS cmd_spoolss_enum_forms(struct cli_state *cli, + TALLOC_CTX *mem_ctx, int argc, + char **argv) +{ + POLICY_HND handle; + WERROR werror; + char *servername = NULL, *printername = NULL; + BOOL got_handle = False; + uint32 needed, num_forms, level = 1, i; + FORM_1 *forms; + + /* Parse the command arguements */ + + if (argc != 2) { + printf ("Usage: %s <printer>\n", argv[0]); + return NT_STATUS_OK; + } + + /* Get a printer handle */ + + asprintf(&servername, "\\\\%s", cli->desthost); + strupper(servername); + asprintf(&printername, "%s\\%s", servername, argv[1]); + + werror = cli_spoolss_open_printer_ex( + cli, mem_ctx, printername, "", MAXIMUM_ALLOWED_ACCESS, + servername, cli->user_name, &handle); + + if (!W_ERROR_IS_OK(werror)) + goto done; + + got_handle = True; + + /* Enumerate forms */ + + werror = cli_spoolss_enumforms( + cli, mem_ctx, 0, &needed, &handle, level, &num_forms, &forms); + + if (W_ERROR_V(werror) == ERRinsufficientbuffer) + werror = cli_spoolss_enumforms( + cli, mem_ctx, needed, NULL, &handle, level, + &num_forms, &forms); + + if (!W_ERROR_IS_OK(werror)) + goto done; + + /* Display output */ + + for (i = 0; i < num_forms; i++) { + fstring form_name; + + if (forms[i].name.buffer) + rpcstr_pull(form_name, forms[i].name.buffer, + sizeof(form_name), 0, STR_TERMINATE); + + printf("%s\n", form_name); + } + + done: + if (got_handle) + cli_spoolss_close_printer(cli, mem_ctx, &handle); + + SAFE_FREE(servername); + SAFE_FREE(printername); + + return W_ERROR_IS_OK(werror) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +static NTSTATUS cmd_spoolss_setprinterdata(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + WERROR result; + uint32 needed; + fstring servername, printername, user; + POLICY_HND pol; + BOOL opened_hnd = False; + PRINTER_INFO_CTR ctr; + PRINTER_INFO_0 *info = NULL; + + /* parse the command arguements */ + if (argc != 4) { + printf ("Usage: %s <printer> <value> <data>\n", argv[0]); + return NT_STATUS_OK; + } + + slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost); + strupper (servername); + slprintf (printername, sizeof(fstring)-1, "%s\\%s", servername, argv[1]); + fstrcpy (user, cli->user_name); + + /* get a printer handle */ + result = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, "", + MAXIMUM_ALLOWED_ACCESS, servername, + user, &pol); + if (!W_ERROR_IS_OK(result)) + goto done; + + opened_hnd = True; + + result = cli_spoolss_getprinter(cli, mem_ctx, 0, &needed, + &pol, 0, &ctr); + + if (W_ERROR_V(result) == ERRinsufficientbuffer) + result = cli_spoolss_getprinter(cli, mem_ctx, needed, NULL, &pol, 0, &ctr); + + if (!W_ERROR_IS_OK(result)) + goto done; + + printf("%s\n", timestring(True)); + printf("\tchange_id (before set)\t:[0x%x]\n", info->change_id); + + + /* Set the printer data */ + + result = cli_spoolss_setprinterdata(cli, mem_ctx, &pol, argv[2], argv[3]); + if (!W_ERROR_IS_OK(result)) { + printf ("Unable to set [%s=%s]!\n", argv[2], argv[3]); + goto done; + } + printf("\tSetPrinterData succeeded [%s: %s]\n", argv[2], argv[3]); + + result = cli_spoolss_getprinter(cli, mem_ctx, 0, &needed, &pol, 0, &ctr); + + if (W_ERROR_V(result) == ERRinsufficientbuffer) + result = cli_spoolss_getprinter(cli, mem_ctx, needed, NULL, &pol, 0, &ctr); + + if (!W_ERROR_IS_OK(result)) + goto done; + + printf("%s\n", timestring(True)); + printf("\tchange_id (after set)\t:[0x%x]\n", info->change_id); + +done: + /* cleanup */ + if (opened_hnd) + cli_spoolss_close_printer(cli, mem_ctx, &pol); + + return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + + +/* List of commands exported by this module */ +struct cmd_set spoolss_commands[] = { + + { "SPOOLSS" }, + + { "adddriver", cmd_spoolss_addprinterdriver, PIPE_SPOOLSS, "Add a print driver", "" }, + { "addprinter", cmd_spoolss_addprinterex, PIPE_SPOOLSS, "Add a printer", "" }, + { "deldriver", cmd_spoolss_deletedriver, PIPE_SPOOLSS, "Delete a printer driver", "" }, + { "enumdata", cmd_spoolss_not_implemented, PIPE_SPOOLSS, "Enumerate printer data (*)", "" }, + { "enumjobs", cmd_spoolss_not_implemented, PIPE_SPOOLSS, "Enumerate print jobs (*)", "" }, + { "enumports", cmd_spoolss_enum_ports, PIPE_SPOOLSS, "Enumerate printer ports", "" }, + { "enumdrivers", cmd_spoolss_enum_drivers, PIPE_SPOOLSS, "Enumerate installed printer drivers", "" }, + { "enumprinters", cmd_spoolss_enum_printers, PIPE_SPOOLSS, "Enumerate printers", "" }, + { "getdata", cmd_spoolss_not_implemented, PIPE_SPOOLSS, "Get print driver data (*)", "" }, + { "getdriver", cmd_spoolss_getdriver, PIPE_SPOOLSS, "Get print driver information", "" }, + { "getdriverdir", cmd_spoolss_getdriverdir, PIPE_SPOOLSS, "Get print driver upload directory", "" }, + { "getprinter", cmd_spoolss_getprinter, PIPE_SPOOLSS, "Get printer info", "" }, + { "getprintprocdir", cmd_spoolss_getprintprocdir, PIPE_SPOOLSS, "Get print processor directory", "" }, + { "openprinter", cmd_spoolss_open_printer_ex, PIPE_SPOOLSS, "Open printer handle", "" }, + { "setdriver", cmd_spoolss_setdriver, PIPE_SPOOLSS, "Set printer driver", "" }, + { "getprintprocdir", cmd_spoolss_getprintprocdir, PIPE_SPOOLSS, "Get print processor directory", "" }, + { "addform", cmd_spoolss_addform, PIPE_SPOOLSS, "Add form", "" }, + { "setform", cmd_spoolss_setform, PIPE_SPOOLSS, "Set form", "" }, + { "getform", cmd_spoolss_getform, PIPE_SPOOLSS, "Get form", "" }, + { "deleteform", cmd_spoolss_deleteform, PIPE_SPOOLSS, "Delete form", "" }, + { "enumforms", cmd_spoolss_enum_forms, PIPE_SPOOLSS, "Enumerate forms", "" }, + { "setprinter", cmd_spoolss_setprinter, PIPE_SPOOLSS, "Set printer comment", "" }, + { "setprinterdata", cmd_spoolss_setprinterdata, PIPE_SPOOLSS, "Set REG_SZ printer data", "" }, + + { NULL } +}; diff --git a/source3/rpcclient/cmd_srvsvc.c b/source3/rpcclient/cmd_srvsvc.c new file mode 100644 index 0000000000..6fbd152dfb --- /dev/null +++ b/source3/rpcclient/cmd_srvsvc.c @@ -0,0 +1,233 @@ +/* + Unix SMB/CIFS implementation. + RPC pipe client + + Copyright (C) Andrew Tridgell 1992-1999 + Copyright (C) Luke Kenneth Casson Leighton 1996 - 1999 + Copyright (C) Tim Potter 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" +#include "rpcclient.h" + +/* Display server query info */ + +static char *get_server_type_str(uint32 type) +{ + static fstring typestr; + int i; + + if (type == SV_TYPE_ALL) { + fstrcpy(typestr, "All"); + return typestr; + } + + typestr[0] = 0; + + for (i = 0; i < 32; i++) { + if (type & (1 << i)) { + switch (1 << i) { + case SV_TYPE_WORKSTATION: + fstrcat(typestr, "Wk "); + break; + case SV_TYPE_SERVER: + fstrcat(typestr, "Sv "); + break; + case SV_TYPE_SQLSERVER: + fstrcat(typestr, "Sql "); + break; + case SV_TYPE_DOMAIN_CTRL: + fstrcat(typestr, "PDC "); + break; + case SV_TYPE_DOMAIN_BAKCTRL: + fstrcat(typestr, "BDC "); + break; + case SV_TYPE_TIME_SOURCE: + fstrcat(typestr, "Tim "); + break; + case SV_TYPE_AFP: + fstrcat(typestr, "AFP "); + break; + case SV_TYPE_NOVELL: + fstrcat(typestr, "Nov "); + break; + case SV_TYPE_DOMAIN_MEMBER: + fstrcat(typestr, "Dom "); + break; + case SV_TYPE_PRINTQ_SERVER: + fstrcat(typestr, "PrQ "); + break; + case SV_TYPE_DIALIN_SERVER: + fstrcat(typestr, "Din "); + break; + case SV_TYPE_SERVER_UNIX: + fstrcat(typestr, "Unx "); + break; + case SV_TYPE_NT: + fstrcat(typestr, "NT "); + break; + case SV_TYPE_WFW: + fstrcat(typestr, "Wfw "); + break; + case SV_TYPE_SERVER_MFPN: + fstrcat(typestr, "Mfp "); + break; + case SV_TYPE_SERVER_NT: + fstrcat(typestr, "SNT "); + break; + case SV_TYPE_POTENTIAL_BROWSER: + fstrcat(typestr, "PtB "); + break; + case SV_TYPE_BACKUP_BROWSER: + fstrcat(typestr, "BMB "); + break; + case SV_TYPE_MASTER_BROWSER: + fstrcat(typestr, "LMB "); + break; + case SV_TYPE_DOMAIN_MASTER: + fstrcat(typestr, "DMB "); + break; + case SV_TYPE_SERVER_OSF: + fstrcat(typestr, "OSF "); + break; + case SV_TYPE_SERVER_VMS: + fstrcat(typestr, "VMS "); + break; + case SV_TYPE_WIN95_PLUS: + fstrcat(typestr, "W95 "); + break; + case SV_TYPE_ALTERNATE_XPORT: + fstrcat(typestr, "Xpt "); + break; + case SV_TYPE_LOCAL_LIST_ONLY: + fstrcat(typestr, "Dom "); + break; + case SV_TYPE_DOMAIN_ENUM: + fstrcat(typestr, "Loc "); + break; + } + } + } + + i = strlen(typestr) - 1; + + if (typestr[i] == ' ') + typestr[i] = 0; + + return typestr; +} + +static void display_server(char *sname, uint32 type, const char *comment) +{ + printf("\t%-15.15s%-20s %s\n", sname, get_server_type_str(type), + comment); +} + +static void display_srv_info_101(SRV_INFO_101 *sv101) +{ + fstring name; + fstring comment; + + unistr2_to_ascii(name, &sv101->uni_name, sizeof(name) - 1); + unistr2_to_ascii(comment, &sv101->uni_comment, sizeof(comment) - 1); + + display_server(name, sv101->srv_type, comment); + + printf("\tplatform_id :\t%d\n", sv101->platform_id); + printf("\tos version :\t%d.%d\n", sv101->ver_major, + sv101->ver_minor); + + printf("\tserver type :\t0x%x\n", sv101->srv_type); +} + +static void display_srv_info_102(SRV_INFO_102 *sv102) +{ + fstring name; + fstring comment; + fstring usr_path; + + unistr2_to_ascii(name, &sv102->uni_name, sizeof(name) - 1); + unistr2_to_ascii(comment, &sv102->uni_comment, sizeof(comment) - 1); + unistr2_to_ascii(usr_path, &sv102->uni_usr_path, sizeof(usr_path) - 1); + + display_server(name, sv102->srv_type, comment); + + printf("\tplatform_id :\t%d\n", sv102->platform_id); + printf("\tos version :\t%d.%d\n", sv102->ver_major, + sv102->ver_minor); + + printf("\tusers :\t%x\n", sv102->users); + printf("\tdisc, hidden :\t%x, %x\n", sv102->disc, sv102->hidden); + printf("\tannounce, delta :\t%d, %d\n", sv102->announce, + sv102->ann_delta); + printf("\tlicenses :\t%d\n", sv102->licenses); + printf("\tuser path :\t%s\n", usr_path); +} + +/* Server query info */ + +static NTSTATUS cmd_srvsvc_srv_query_info(struct cli_state *cli, + TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + uint32 info_level = 101; + SRV_INFO_CTR ctr; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + + if (argc > 2) { + printf("Usage: %s [infolevel]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc == 2) + info_level = atoi(argv[1]); + + result = cli_srvsvc_net_srv_get_info(cli, mem_ctx, info_level, + &ctr); + + if (!NT_STATUS_IS_OK(result)) { + goto done; + } + + /* Display results */ + + switch (info_level) { + case 101: + display_srv_info_101(&ctr.srv.sv101); + break; + case 102: + display_srv_info_102(&ctr.srv.sv102); + break; + default: + printf("unsupported info level %d\n", info_level); + break; + } + + done: + return result; +} + +/* List of commands exported by this module */ + +struct cmd_set srvsvc_commands[] = { + + { "SRVSVC" }, + + { "srvinfo", cmd_srvsvc_srv_query_info, PIPE_SRVSVC, "Server query info", "" }, + + { NULL } +}; diff --git a/source3/rpcclient/cmd_wkssvc.c b/source3/rpcclient/cmd_wkssvc.c new file mode 100644 index 0000000000..79acf35943 --- /dev/null +++ b/source3/rpcclient/cmd_wkssvc.c @@ -0,0 +1,84 @@ +/* + Unix SMB/CIFS implementation. + NT Domain Authentication SMB / MSRPC client + Copyright (C) Andrew Tridgell 1994-1997 + Copyright (C) Luke Kenneth Casson Leighton 1996-1997 + + 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" + +#define DEBUG_TESTING + +extern struct cli_state *smb_cli; + +extern FILE* out_hnd; + + +/**************************************************************************** +workstation get info query +****************************************************************************/ +void cmd_wks_query_info(struct client_info *info) +{ + fstring dest_wks; + fstring tmp; + WKS_INFO_100 ctr; + uint32 info_level = 100; + + BOOL res = True; + + memset((char *)&ctr, '\0', sizeof(ctr)); + + fstrcpy(dest_wks, "\\\\"); + fstrcat(dest_wks, info->dest_host); + strupper(dest_wks); + + if (next_token_nr(NULL, tmp, NULL, sizeof(tmp))) + { + info_level = (uint32)strtol(tmp, (char**)NULL, 10); + } + + DEBUG(4,("cmd_wks_query_info: server:%s info level: %d\n", + dest_wks, info_level)); + + DEBUG(5, ("cmd_wks_query_info: smb_cli->fd:%d\n", smb_cli->fd)); + + /* open LSARPC session. */ + res = res ? cli_nt_session_open(smb_cli, PIPE_WKSSVC) : False; + + /* send info level: receive requested info. hopefully. */ + res = res ? do_wks_query_info(smb_cli, + dest_wks, info_level, &ctr) : False; + + /* close the session */ + cli_nt_session_close(smb_cli); + + if (res) + { + DEBUG(5,("cmd_wks_query_info: query succeeded\n")); + +#if 0 + display_wks_info_100(out_hnd, ACTION_HEADER , &ctr); + display_wks_info_100(out_hnd, ACTION_ENUMERATE, &ctr); + display_wks_info_100(out_hnd, ACTION_FOOTER , &ctr); +#endif + + } + else + { + DEBUG(5,("cmd_wks_query_info: query failed\n")); + } +} diff --git a/source3/rpcclient/display.c b/source3/rpcclient/display.c new file mode 100644 index 0000000000..d03465206e --- /dev/null +++ b/source3/rpcclient/display.c @@ -0,0 +1,1338 @@ +/* + Unix SMB/CIFS implementation. + Samba utility functions + Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Luke Kenneth Casson Leighton 1996 - 1998 + + 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" + + +/**************************************************************************** +convert a share mode to a string +****************************************************************************/ +char *get_file_mode_str(uint32 share_mode) +{ + static fstring mode; + + switch (GET_DENY_MODE(share_mode)) + { + case DENY_NONE : fstrcpy(mode, "DENY_NONE "); break; + case DENY_ALL : fstrcpy(mode, "DENY_ALL "); break; + case DENY_DOS : fstrcpy(mode, "DENY_DOS "); break; + case DENY_READ : fstrcpy(mode, "DENY_READ "); break; + case DENY_WRITE: fstrcpy(mode, "DENY_WRITE "); break; + case DENY_FCB: fstrcpy(mode, "DENY_FCB "); break; + default : fstrcpy(mode, "DENY_???? "); break; + } + + switch (share_mode & 0xF) + { + case 0 : fstrcat(mode, "RDONLY"); break; + case 1 : fstrcat(mode, "WRONLY"); break; + case 2 : fstrcat(mode, "RDWR "); break; + default: fstrcat(mode, "R??W??"); break; + } + + return mode; +} + +/**************************************************************************** +convert an oplock mode to a string +****************************************************************************/ +char *get_file_oplock_str(uint32 op_type) +{ + static fstring oplock; + BOOL excl = ((op_type & EXCLUSIVE_OPLOCK) != 0); + BOOL batch = ((op_type & BATCH_OPLOCK ) != 0); + + oplock[0] = 0; + + if (excl ) fstrcat(oplock, "EXCLUSIVE"); + if (excl && batch) fstrcat(oplock, "+"); + if ( batch) fstrcat(oplock, "BATCH"); + if (!excl && !batch) fstrcat(oplock, "NONE"); + + return oplock; +} + +/**************************************************************************** +convert a share type enum to a string +****************************************************************************/ +char *get_share_type_str(uint32 type) +{ + static fstring typestr; + + switch (type) + { + case STYPE_DISKTREE: fstrcpy(typestr, "Disk" ); break; + case STYPE_PRINTQ : fstrcpy(typestr, "Printer"); break; + case STYPE_DEVICE : fstrcpy(typestr, "Device" ); break; + case STYPE_IPC : fstrcpy(typestr, "IPC" ); break; + default : fstrcpy(typestr, "????" ); break; + } + return typestr; +} + +/**************************************************************************** +convert a server type enum to a string +****************************************************************************/ +char *get_server_type_str(uint32 type) +{ + static fstring typestr; + + if (type == SV_TYPE_ALL) + { + fstrcpy(typestr, "All"); + } + else + { + int i; + typestr[0] = 0; + for (i = 0; i < 32; i++) + { + if (type & (1 << i)) + { + switch (((unsigned)1) << i) + { + case SV_TYPE_WORKSTATION : fstrcat(typestr, "Wk " ); break; + case SV_TYPE_SERVER : fstrcat(typestr, "Sv " ); break; + case SV_TYPE_SQLSERVER : fstrcat(typestr, "Sql "); break; + case SV_TYPE_DOMAIN_CTRL : fstrcat(typestr, "PDC "); break; + case SV_TYPE_DOMAIN_BAKCTRL : fstrcat(typestr, "BDC "); break; + case SV_TYPE_TIME_SOURCE : fstrcat(typestr, "Tim "); break; + case SV_TYPE_AFP : fstrcat(typestr, "AFP "); break; + case SV_TYPE_NOVELL : fstrcat(typestr, "Nov "); break; + case SV_TYPE_DOMAIN_MEMBER : fstrcat(typestr, "Dom "); break; + case SV_TYPE_PRINTQ_SERVER : fstrcat(typestr, "PrQ "); break; + case SV_TYPE_DIALIN_SERVER : fstrcat(typestr, "Din "); break; + case SV_TYPE_SERVER_UNIX : fstrcat(typestr, "Unx "); break; + case SV_TYPE_NT : fstrcat(typestr, "NT " ); break; + case SV_TYPE_WFW : fstrcat(typestr, "Wfw "); break; + case SV_TYPE_SERVER_MFPN : fstrcat(typestr, "Mfp "); break; + case SV_TYPE_SERVER_NT : fstrcat(typestr, "SNT "); break; + case SV_TYPE_POTENTIAL_BROWSER: fstrcat(typestr, "PtB "); break; + case SV_TYPE_BACKUP_BROWSER : fstrcat(typestr, "BMB "); break; + case SV_TYPE_MASTER_BROWSER : fstrcat(typestr, "LMB "); break; + case SV_TYPE_DOMAIN_MASTER : fstrcat(typestr, "DMB "); break; + case SV_TYPE_SERVER_OSF : fstrcat(typestr, "OSF "); break; + case SV_TYPE_SERVER_VMS : fstrcat(typestr, "VMS "); break; + case SV_TYPE_WIN95_PLUS : fstrcat(typestr, "W95 "); break; + case SV_TYPE_ALTERNATE_XPORT : fstrcat(typestr, "Xpt "); break; + case SV_TYPE_LOCAL_LIST_ONLY : fstrcat(typestr, "Dom "); break; + case SV_TYPE_DOMAIN_ENUM : fstrcat(typestr, "Loc "); break; + } + } + } + i = strlen(typestr)-1; + if (typestr[i] == ' ') typestr[i] = 0; + + } + return typestr; +} + +/**************************************************************************** +server info level 101 display function +****************************************************************************/ +void display_srv_info_101(FILE *out_hnd, enum action_type action, + SRV_INFO_101 *sv101) +{ + if (sv101 == NULL) + { + return; + } + + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "Server Info Level 101:\n"); + + break; + } + case ACTION_ENUMERATE: + { + fstring name; + fstring comment; + + rpcstr_pull(name, sv101->uni_name.buffer, sizeof(name), sv101->uni_name.uni_str_len*2, 0); + rpcstr_pull(comment, sv101->uni_comment.buffer, sizeof(comment), sv101->uni_comment.uni_str_len*2, 0); + + display_server(out_hnd, action, name, sv101->srv_type, comment); + + fprintf(out_hnd, "\tplatform_id : %d\n" , sv101->platform_id); + fprintf(out_hnd, "\tos version : %d.%d\n" , sv101->ver_major, sv101->ver_minor); + + break; + } + case ACTION_FOOTER: + { + break; + } + } + +} + +/**************************************************************************** +server info level 102 display function +****************************************************************************/ +void display_srv_info_102(FILE *out_hnd, enum action_type action,SRV_INFO_102 *sv102) +{ + if (sv102 == NULL) + { + return; + } + + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "Server Info Level 102:\n"); + + break; + } + case ACTION_ENUMERATE: + { + fstring name; + fstring comment; + fstring usr_path; + + rpcstr_pull(name, sv102->uni_name.buffer, sizeof(name), sv102->uni_name.uni_str_len*2, 0); + rpcstr_pull(comment, sv102->uni_comment.buffer, sizeof(comment), sv102->uni_comment.uni_str_len*2, 0); + rpcstr_pull(usr_path, sv102->uni_usr_path.buffer, sizeof(usr_path), sv102->uni_usr_path.uni_str_len*2, 0); + + display_server(out_hnd, action, name, sv102->srv_type, comment); + + fprintf(out_hnd, "\tplatform_id : %d\n" , sv102->platform_id); + fprintf(out_hnd, "\tos version : %d.%d\n" , sv102->ver_major, sv102->ver_minor); + + fprintf(out_hnd, "\tusers : %x\n" , sv102->users ); + fprintf(out_hnd, "\tdisc, hidden : %x,%x\n" , sv102->disc , sv102->hidden ); + fprintf(out_hnd, "\tannounce, delta : %d, %d\n", sv102->announce , sv102->ann_delta); + fprintf(out_hnd, "\tlicenses : %d\n" , sv102->licenses ); + fprintf(out_hnd, "\tuser path : %s\n" , usr_path); + + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** +server info container display function +****************************************************************************/ +void display_srv_info_ctr(FILE *out_hnd, enum action_type action,SRV_INFO_CTR *ctr) +{ + if (ctr == NULL || ctr->ptr_srv_ctr == 0) + { + fprintf(out_hnd, "Server Information: unavailable due to an error\n"); + return; + } + + switch (ctr->switch_value) + { + case 101: + { + display_srv_info_101(out_hnd, action, &(ctr->srv.sv101)); + break; + } + case 102: + { + display_srv_info_102(out_hnd, action, &(ctr->srv.sv102)); + break; + } + default: + { + fprintf(out_hnd, "Server Information: Unknown Info Level\n"); + break; + } + } +} + +/**************************************************************************** +connection info level 0 display function +****************************************************************************/ +void display_conn_info_0(FILE *out_hnd, enum action_type action, + CONN_INFO_0 *info0) +{ + if (info0 == NULL) + { + return; + } + + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "Connection Info Level 0:\n"); + + break; + } + case ACTION_ENUMERATE: + { + fprintf(out_hnd, "\tid: %d\n", info0->id); + + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } + +} + +/**************************************************************************** +connection info level 1 display function +****************************************************************************/ +void display_conn_info_1(FILE *out_hnd, enum action_type action, + CONN_INFO_1 *info1, CONN_INFO_1_STR *str1) +{ + if (info1 == NULL || str1 == NULL) + { + return; + } + + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "Connection Info Level 1:\n"); + + break; + } + case ACTION_ENUMERATE: + { + fstring usr_name; + fstring net_name; + + rpcstr_pull(usr_name, str1->uni_usr_name.buffer, sizeof(usr_name), str1->uni_usr_name.uni_str_len*2,0); + rpcstr_pull(net_name, str1->uni_net_name.buffer, sizeof(net_name), str1->uni_net_name.uni_str_len*2,0); + + fprintf(out_hnd, "\tid : %d\n", info1->id); + fprintf(out_hnd, "\ttype : %s\n", get_share_type_str(info1->type)); + fprintf(out_hnd, "\tnum_opens: %d\n", info1->num_opens); + fprintf(out_hnd, "\tnum_users: %d\n", info1->num_users); + fprintf(out_hnd, "\topen_time: %d\n", info1->open_time); + + fprintf(out_hnd, "\tuser name: %s\n", usr_name); + fprintf(out_hnd, "\tnet name: %s\n", net_name); + + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } + +} + +/**************************************************************************** +connection info level 0 container display function +****************************************************************************/ +void display_srv_conn_info_0_ctr(FILE *out_hnd, enum action_type action, + SRV_CONN_INFO_0 *ctr) +{ + if (ctr == NULL) + { + fprintf(out_hnd, "display_srv_conn_info_0_ctr: unavailable due to an internal error\n"); + return; + } + + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + int i; + + for (i = 0; i < ctr->num_entries_read; i++) + { + display_conn_info_0(out_hnd, ACTION_HEADER , &(ctr->info_0[i])); + display_conn_info_0(out_hnd, ACTION_ENUMERATE, &(ctr->info_0[i])); + display_conn_info_0(out_hnd, ACTION_FOOTER , &(ctr->info_0[i])); + } + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** +connection info level 1 container display function +****************************************************************************/ +void display_srv_conn_info_1_ctr(FILE *out_hnd, enum action_type action, + SRV_CONN_INFO_1 *ctr) +{ + if (ctr == NULL) + { + fprintf(out_hnd, "display_srv_conn_info_1_ctr: unavailable due to an internal error\n"); + return; + } + + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + int i; + + for (i = 0; i < ctr->num_entries_read; i++) + { + display_conn_info_1(out_hnd, ACTION_HEADER , &(ctr->info_1[i]), &(ctr->info_1_str[i])); + display_conn_info_1(out_hnd, ACTION_ENUMERATE, &(ctr->info_1[i]), &(ctr->info_1_str[i])); + display_conn_info_1(out_hnd, ACTION_FOOTER , &(ctr->info_1[i]), &(ctr->info_1_str[i])); + } + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** +connection info container display function +****************************************************************************/ +void display_srv_conn_info_ctr(FILE *out_hnd, enum action_type action, + SRV_CONN_INFO_CTR *ctr) +{ + if (ctr == NULL || ctr->ptr_conn_ctr == 0) + { + fprintf(out_hnd, "display_srv_conn_info_ctr: unavailable due to an internal error\n"); + return; + } + + switch (ctr->switch_value) + { + case 0: + { + display_srv_conn_info_0_ctr(out_hnd, action, + &(ctr->conn.info0)); + break; + } + case 1: + { + display_srv_conn_info_1_ctr(out_hnd, action, + &(ctr->conn.info1)); + break; + } + default: + { + fprintf(out_hnd, "display_srv_conn_info_ctr: Unknown Info Level\n"); + break; + } + } +} + + +/**************************************************************************** +share info level 1 display function +****************************************************************************/ +void display_share_info_1(FILE *out_hnd, enum action_type action, + SRV_SHARE_INFO_1 *info1) +{ + if (info1 == NULL) + { + return; + } + + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "Share Info Level 1:\n"); + + break; + } + case ACTION_ENUMERATE: + { + fstring remark ; + fstring net_name; + + rpcstr_pull(net_name, info1->info_1_str.uni_netname.buffer, sizeof(net_name), info1->info_1_str.uni_netname.uni_str_len*2, 0); + rpcstr_pull(remark, info1->info_1_str.uni_remark.buffer, sizeof(remark), info1->info_1_str.uni_remark.uni_str_len*2, 0); + + display_share(out_hnd, action, net_name, info1->info_1.type, remark); + + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } + +} + +/**************************************************************************** +share info level 2 display function +****************************************************************************/ +void display_share_info_2(FILE *out_hnd, enum action_type action, + SRV_SHARE_INFO_2 *info2) +{ + if (info2 == NULL) + { + return; + } + + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "Share Info Level 2:\n"); + + break; + } + case ACTION_ENUMERATE: + { + fstring remark ; + fstring net_name; + fstring path ; + fstring passwd ; + + rpcstr_pull(net_name, info2->info_2_str.uni_netname.buffer, sizeof(net_name), info2->info_2_str.uni_netname.uni_str_len*2, 0); + rpcstr_pull(remark, info2->info_2_str.uni_remark.buffer, sizeof(remark), info2->info_2_str.uni_remark.uni_str_len*2, 0); + rpcstr_pull(path, info2->info_2_str.uni_path.buffer, sizeof(path), info2->info_2_str.uni_path.uni_str_len*2, 0); + rpcstr_pull(passwd, info2->info_2_str.uni_passwd.buffer, sizeof(passwd), info2->info_2_str.uni_passwd.uni_str_len*2, 0); + + display_share2(out_hnd, action, net_name, + info2->info_2.type, remark, info2->info_2.perms, + info2->info_2.max_uses, info2->info_2.num_uses, + path, passwd); + + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } + +} + +/**************************************************************************** +share info container display function +****************************************************************************/ +void display_srv_share_info_ctr(FILE *out_hnd, enum action_type action, + SRV_SHARE_INFO_CTR *ctr) +{ + if (ctr == NULL) + { + fprintf(out_hnd, "display_srv_share_info_ctr: unavailable due to an internal error\n"); + return; + } + + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + int i; + + for (i = 0; i < ctr->num_entries; i++) + { + switch (ctr->info_level) { + case 1: + display_share_info_1(out_hnd, ACTION_HEADER , &(ctr->share.info1[i])); + display_share_info_1(out_hnd, ACTION_ENUMERATE, &(ctr->share.info1[i])); + display_share_info_1(out_hnd, ACTION_FOOTER , &(ctr->share.info1[i])); + break; + case 2: + display_share_info_2(out_hnd, ACTION_HEADER , &(ctr->share.info2[i])); + display_share_info_2(out_hnd, ACTION_ENUMERATE, &(ctr->share.info2[i])); + display_share_info_2(out_hnd, ACTION_FOOTER , &(ctr->share.info2[i])); + break; + default: + fprintf(out_hnd, "display_srv_share_info_ctr: Unknown Info Level\n"); + break; + } + } + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** +file info level 3 display function +****************************************************************************/ +void display_file_info_3(FILE *out_hnd, enum action_type action, + FILE_INFO_3 *info3, FILE_INFO_3_STR *str3) +{ + if (info3 == NULL || str3 == NULL) + { + return; + } + + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "File Info Level 3:\n"); + + break; + } + case ACTION_ENUMERATE: + { + fstring path_name; + fstring user_name; + + rpcstr_pull(path_name, str3->uni_path_name.buffer, sizeof(path_name), str3->uni_path_name.uni_str_len*2, 0); + rpcstr_pull(user_name, str3->uni_user_name.buffer, sizeof(user_name), str3->uni_user_name.uni_str_len*2, 0); + + fprintf(out_hnd, "\tid : %d\n", info3->id); + fprintf(out_hnd, "\tperms : %s\n", get_file_mode_str(info3->perms)); + fprintf(out_hnd, "\tnum_locks: %d\n", info3->num_locks); + + fprintf(out_hnd, "\tpath name: %s\n", path_name); + fprintf(out_hnd, "\tuser name: %s\n", user_name); + + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } + +} + +/**************************************************************************** +file info level 3 container display function +****************************************************************************/ +void display_srv_file_info_3_ctr(FILE *out_hnd, enum action_type action, + SRV_FILE_INFO_3 *ctr) +{ + if (ctr == NULL) + { + fprintf(out_hnd, "display_srv_file_info_3_ctr: unavailable due to an internal error\n"); + return; + } + + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + int i; + + for (i = 0; i < ctr->num_entries_read; i++) + { + display_file_info_3(out_hnd, ACTION_HEADER , &(ctr->info_3[i]), &(ctr->info_3_str[i])); + display_file_info_3(out_hnd, ACTION_ENUMERATE, &(ctr->info_3[i]), &(ctr->info_3_str[i])); + display_file_info_3(out_hnd, ACTION_FOOTER , &(ctr->info_3[i]), &(ctr->info_3_str[i])); + } + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** +file info container display function +****************************************************************************/ +void display_srv_file_info_ctr(FILE *out_hnd, enum action_type action, + SRV_FILE_INFO_CTR *ctr) +{ + if (ctr == NULL || ctr->ptr_file_ctr == 0) + { + fprintf(out_hnd, "display_srv_file_info_ctr: unavailable due to an internal error\n"); + return; + } + + switch (ctr->switch_value) + { + case 3: + { + display_srv_file_info_3_ctr(out_hnd, action, + &(ctr->file.info3)); + break; + } + default: + { + fprintf(out_hnd, "display_srv_file_info_ctr: Unknown Info Level\n"); + break; + } + } +} + +/**************************************************************************** + print browse connection on a host + ****************************************************************************/ +void display_server(FILE *out_hnd, enum action_type action, + char *sname, uint32 type, char *comment) +{ + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + fprintf(out_hnd, "\t%-15.15s%-20s %s\n", + sname, get_server_type_str(type), comment); + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** +print shares on a host +****************************************************************************/ +void display_share(FILE *out_hnd, enum action_type action, + char *sname, uint32 type, char *comment) +{ + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + fprintf(out_hnd, "\t%-15.15s%-10.10s%s\n", + sname, get_share_type_str(type), comment); + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + + +/**************************************************************************** +print shares on a host, level 2 +****************************************************************************/ +void display_share2(FILE *out_hnd, enum action_type action, + char *sname, uint32 type, char *comment, + uint32 perms, uint32 max_uses, uint32 num_uses, + char *path, char *passwd) +{ + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + fprintf(out_hnd, "\t%-15.15s%-10.10s%s %x %x %x %s %s\n", + sname, get_share_type_str(type), comment, + perms, max_uses, num_uses, path, passwd); + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + + +/**************************************************************************** +print name info +****************************************************************************/ +void display_name(FILE *out_hnd, enum action_type action, + char *sname) +{ + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + fprintf(out_hnd, "\t%-21.21s\n", sname); + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + + +/**************************************************************************** + display group rid info + ****************************************************************************/ +void display_group_rid_info(FILE *out_hnd, enum action_type action, + uint32 num_gids, DOM_GID *gid) +{ + switch (action) + { + case ACTION_HEADER: + { + if (num_gids == 0) + { + fprintf(out_hnd, "\tNo Groups\n"); + } + else + { + fprintf(out_hnd, "\tGroup Info\n"); + fprintf(out_hnd, "\t----------\n"); + } + break; + } + case ACTION_ENUMERATE: + { + int i; + + for (i = 0; i < num_gids; i++) + { + fprintf(out_hnd, "\tGroup RID: %8x attr: %x\n", + gid[i].g_rid, gid[i].attr); + } + + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } +} + + +/**************************************************************************** + display alias name info + ****************************************************************************/ +void display_alias_name_info(FILE *out_hnd, enum action_type action, + uint32 num_aliases, fstring *alias_name, uint32 *num_als_usrs) +{ + switch (action) + { + case ACTION_HEADER: + { + if (num_aliases == 0) + { + fprintf(out_hnd, "\tNo Aliases\n"); + } + else + { + fprintf(out_hnd, "\tAlias Names\n"); + fprintf(out_hnd, "\t----------- \n"); + } + break; + } + case ACTION_ENUMERATE: + { + int i; + + for (i = 0; i < num_aliases; i++) + { + fprintf(out_hnd, "\tAlias Name: %s Attributes: %3d\n", + alias_name[i], num_als_usrs[i]); + } + + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } +} + + +/**************************************************************************** + display sam_user_info_21 structure + ****************************************************************************/ +void display_sam_user_info_21(FILE *out_hnd, enum action_type action, SAM_USER_INFO_21 *usr) +{ + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "\tUser Info, Level 0x15\n"); + fprintf(out_hnd, "\t---------------------\n"); + + break; + } + case ACTION_ENUMERATE: + { + pstring tmp; + rpcstr_pull(tmp, usr->uni_user_name.buffer, sizeof(tmp),usr->uni_user_name.uni_str_len*2, 0); + fprintf(out_hnd, "\t\tUser Name : %s\n", tmp); /* username unicode string */ + rpcstr_pull(tmp, usr->uni_full_name.buffer, sizeof(tmp),usr->uni_full_name.uni_str_len*2, 0); + fprintf(out_hnd, "\t\tFull Name : %s\n", tmp); /* user's full name unicode string */ + rpcstr_pull(tmp, usr->uni_home_dir.buffer, sizeof(tmp),usr->uni_home_dir.uni_str_len*2, 0); + fprintf(out_hnd, "\t\tHome Drive : %s\n", tmp); /* home directory unicode string */ + rpcstr_pull(tmp, usr->uni_dir_drive.buffer, sizeof(tmp),usr->uni_dir_drive.uni_str_len*2, 0); + fprintf(out_hnd, "\t\tDir Drive : %s\n", tmp); /* home directory drive unicode string */ + rpcstr_pull(tmp, usr->uni_profile_path.buffer, sizeof(tmp),usr->uni_profile_path.uni_str_len*2, 0); + fprintf(out_hnd, "\t\tProfile Path: %s\n", tmp); /* profile path unicode string */ + rpcstr_pull(tmp, usr->uni_logon_script.buffer, sizeof(tmp),usr->uni_logon_script.uni_str_len*2, 0); + fprintf(out_hnd, "\t\tLogon Script: %s\n", tmp); /* logon script unicode string */ + rpcstr_pull(tmp, usr->uni_acct_desc.buffer, sizeof(tmp),usr->uni_acct_desc.uni_str_len*2, 0); + fprintf(out_hnd, "\t\tDescription : %s\n", tmp); /* user description unicode string */ + rpcstr_pull(tmp, usr->uni_workstations.buffer, sizeof(tmp),usr->uni_workstations.uni_str_len*2, 0); + fprintf(out_hnd, "\t\tWorkstations: %s\n", tmp); /* workstaions unicode string */ + rpcstr_pull(tmp, usr->uni_unknows_str.buffer, sizeof(tmp),usr->uni_unknown_str.uni_str_len*2, 0); + fprintf(out_hnd, "\t\tUnknown Str : %s\n", tmp); /* unknown string unicode string */ + rpcstr_pull(tmp, usr->uni_munged_dial.buffer, sizeof(tmp),usr->uni_munged_dial.uni_str_len*2, 0); + fprintf(out_hnd, "\t\tRemote Dial : %s\n", tmp); /* munged remote access unicode string */ + + fprintf(out_hnd, "\t\tLogon Time : %s\n", http_timestring(nt_time_to_unix(&(usr->logon_time )))); + fprintf(out_hnd, "\t\tLogoff Time : %s\n", http_timestring(nt_time_to_unix(&(usr->logoff_time )))); + fprintf(out_hnd, "\t\tKickoff Time : %s\n", http_timestring(nt_time_to_unix(&(usr->kickoff_time )))); + fprintf(out_hnd, "\t\tPassword last set Time : %s\n", http_timestring(nt_time_to_unix(&(usr->pass_last_set_time )))); + fprintf(out_hnd, "\t\tPassword can change Time : %s\n", http_timestring(nt_time_to_unix(&(usr->pass_can_change_time )))); + fprintf(out_hnd, "\t\tPassword must change Time: %s\n", http_timestring(nt_time_to_unix(&(usr->pass_must_change_time)))); + + fprintf(out_hnd, "\t\tunknown_2[0..31]...\n"); /* user passwords? */ + + fprintf(out_hnd, "\t\tuser_rid : %x\n" , usr->user_rid ); /* User ID */ + fprintf(out_hnd, "\t\tgroup_rid: %x\n" , usr->group_rid); /* Group ID */ + fprintf(out_hnd, "\t\tacb_info : %04x\n", usr->acb_info ); /* Account Control Info */ + + fprintf(out_hnd, "\t\tunknown_3: %08x\n", usr->unknown_3); /* 0x00ff ffff */ + fprintf(out_hnd, "\t\tlogon_divs: %d\n", usr->logon_divs); /* 0x0000 00a8 which is 168 which is num hrs in a week */ + fprintf(out_hnd, "\t\tunknown_5: %08x\n", usr->unknown_5); /* 0x0002 0000 */ + + fprintf(out_hnd, "\t\tpadding1[0..7]...\n"); + + if (usr->ptr_logon_hrs) + { + fprintf(out_hnd, "\t\tlogon_hrs[0..%d]...\n", usr->logon_hrs.len); + } + + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } +} + + +/**************************************************************************** +convert a security permissions into a string +****************************************************************************/ +char *get_sec_mask_str(uint32 type) +{ + static fstring typestr; + int i; + + switch (type) + { + case SEC_RIGHTS_FULL_CONTROL: + { + fstrcpy(typestr, "Full Control"); + return typestr; + } + + case SEC_RIGHTS_READ: + { + fstrcpy(typestr, "Read"); + return typestr; + } + default: + { + break; + } + } + + typestr[0] = 0; + for (i = 0; i < 32; i++) + { + if (type & (1 << i)) + { + switch (((unsigned)1) << i) + { + case SEC_RIGHTS_QUERY_VALUE : fstrcat(typestr, "Query " ); break; + case SEC_RIGHTS_SET_VALUE : fstrcat(typestr, "Set " ); break; + case SEC_RIGHTS_CREATE_SUBKEY : fstrcat(typestr, "Create "); break; + case SEC_RIGHTS_ENUM_SUBKEYS : fstrcat(typestr, "Enum "); break; + case SEC_RIGHTS_NOTIFY : fstrcat(typestr, "Notify "); break; + case SEC_RIGHTS_CREATE_LINK : fstrcat(typestr, "CreateLink "); break; + case SEC_RIGHTS_DELETE : fstrcat(typestr, "Delete "); break; + case SEC_RIGHTS_READ_CONTROL : fstrcat(typestr, "ReadControl "); break; + case SEC_RIGHTS_WRITE_DAC : fstrcat(typestr, "WriteDAC "); break; + case SEC_RIGHTS_WRITE_OWNER : fstrcat(typestr, "WriteOwner "); break; + } + type &= ~(1 << i); + } + } + + /* remaining bits get added on as-is */ + if (type != 0) + { + fstring tmp; + slprintf(tmp, sizeof(tmp)-1, "[%08x]", type); + fstrcat(typestr, tmp); + } + + /* remove last space */ + i = strlen(typestr)-1; + if (typestr[i] == ' ') typestr[i] = 0; + + return typestr; +} + +/**************************************************************************** + display sec_access structure + ****************************************************************************/ +void display_sec_access(FILE *out_hnd, enum action_type action, SEC_ACCESS *info) +{ + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + fprintf(out_hnd, "\t\tPermissions: %s\n", + get_sec_mask_str(info->mask)); + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** + display sec_ace structure + ****************************************************************************/ +void display_sec_ace(FILE *out_hnd, enum action_type action, SEC_ACE *ace) +{ + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "\tACE\n"); + break; + } + case ACTION_ENUMERATE: + { + fstring sid_str; + + display_sec_access(out_hnd, ACTION_HEADER , &ace->info); + display_sec_access(out_hnd, ACTION_ENUMERATE, &ace->info); + display_sec_access(out_hnd, ACTION_FOOTER , &ace->info); + + sid_to_string(sid_str, &ace->sid); + fprintf(out_hnd, "\t\tSID: %s\n", sid_str); + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** + display sec_acl structure + ****************************************************************************/ +void display_sec_acl(FILE *out_hnd, enum action_type action, SEC_ACL *sec_acl) +{ + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "\tACL\tNum ACEs:\t%d\trevision:\t%x\n", + sec_acl->num_aces, sec_acl->revision); + fprintf(out_hnd, "\t---\n"); + + break; + } + case ACTION_ENUMERATE: + { + if (sec_acl->size != 0 && sec_acl->num_aces != 0) + { + int i; + for (i = 0; i < sec_acl->num_aces; i++) + { + display_sec_ace(out_hnd, ACTION_HEADER , &sec_acl->ace[i]); + display_sec_ace(out_hnd, ACTION_ENUMERATE, &sec_acl->ace[i]); + display_sec_ace(out_hnd, ACTION_FOOTER , &sec_acl->ace[i]); + } + } + + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } +} + +/**************************************************************************** + display sec_desc structure + ****************************************************************************/ +void display_sec_desc(FILE *out_hnd, enum action_type action, SEC_DESC *sec) +{ + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "\tSecurity Descriptor\trevision:\t%x\ttype:\t%x\n", + sec->revision, sec->type); + fprintf(out_hnd, "\t-------------------\n"); + + break; + } + case ACTION_ENUMERATE: + { + fstring sid_str; + + if (sec->off_sacl != 0) + { + display_sec_acl(out_hnd, ACTION_HEADER , sec->sacl); + display_sec_acl(out_hnd, ACTION_ENUMERATE, sec->sacl); + display_sec_acl(out_hnd, ACTION_FOOTER , sec->sacl); + } + if (sec->off_dacl != 0) + { + display_sec_acl(out_hnd, ACTION_HEADER , sec->dacl); + display_sec_acl(out_hnd, ACTION_ENUMERATE, sec->dacl); + display_sec_acl(out_hnd, ACTION_FOOTER , sec->dacl); + } + if (sec->off_owner_sid != 0) + { + sid_to_string(sid_str, sec->owner_sid); + fprintf(out_hnd, "\tOwner SID:\t%s\n", sid_str); + } + if (sec->off_grp_sid != 0) + { + sid_to_string(sid_str, sec->grp_sid); + fprintf(out_hnd, "\tParent SID:\t%s\n", sid_str); + } + + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } +} + +/**************************************************************************** +convert a security permissions into a string +****************************************************************************/ +char *get_reg_val_type_str(uint32 type) +{ + static fstring typestr; + + switch (type) + { + case 0x01: + { + fstrcpy(typestr, "string"); + return typestr; + } + + case 0x03: + { + fstrcpy(typestr, "bytes"); + return typestr; + } + + case 0x04: + { + fstrcpy(typestr, "uint32"); + return typestr; + } + + case 0x07: + { + fstrcpy(typestr, "multi"); + return typestr; + } + default: + { + break; + } + } + slprintf(typestr, sizeof(typestr)-1, "[%d]", type); + return typestr; +} + + +static void print_reg_value(FILE *out_hnd, char *val_name, uint32 val_type, BUFFER2 *value) +{ + fstring type; + pstring intvalue; + fstrcpy(type, get_reg_val_type_str(val_type)); + + switch (val_type) + { + case 0x01: /* unistr */ + { + rpcstr_pull(intvalue, value->buffer, sizeof(intvalue), value->buf_len, 0); + /*fprintf(out_hnd,"\t%s:\t%s:\t%s\n", val_name, type, dos_buffer2_to_str(value));*/ + fprintf(out_hnd,"\t%s:\t%s:\t%s\n", val_name, type, value); + break; + } + + default: /* unknown */ + case 0x03: /* bytes */ + { + if (value->buf_len <= 8) + { + fprintf(out_hnd,"\t%s:\t%s:\t", val_name, type); + out_data(out_hnd, (char*)value->buffer, value->buf_len, 8); + } + else + { + fprintf(out_hnd,"\t%s:\t%s:\n", val_name, type); + out_data(out_hnd, (char*)value->buffer, value->buf_len, 16); + } + break; + } + + case 0x04: /* uint32 */ + { + fprintf(out_hnd,"\t%s:\t%s: 0x%08x\n", val_name, type, buffer2_to_uint32(value)); + break; + } + + case 0x07: /* multiunistr */ + { + fprintf(out_hnd,"\t%s:\t%s:\t%s\n", val_name, type, dos_buffer2_to_multistr(value)); + break; + } + } +} + +/**************************************************************************** + display structure + ****************************************************************************/ +void display_reg_value_info(FILE *out_hnd, enum action_type action, + char *val_name, uint32 val_type, BUFFER2 *value) +{ + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + print_reg_value(out_hnd, val_name, val_type, value); + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** + display structure + ****************************************************************************/ +void display_reg_key_info(FILE *out_hnd, enum action_type action, + char *key_name, time_t key_mod_time) +{ + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + fprintf(out_hnd, "\t%s\t(%s)\n", + key_name, http_timestring(key_mod_time)); + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + +#if COPY_THIS_TEMPLATE +/**************************************************************************** + display structure + ****************************************************************************/ + void display_(FILE *out_hnd, enum action_type action, *) +{ + switch (action) + { + case ACTION_HEADER: + { + fprintf(out_hnd, "\t\n"); + fprintf(out_hnd, "\t-------------------\n"); + + break; + } + case ACTION_ENUMERATE: + { + break; + } + case ACTION_FOOTER: + { + fprintf(out_hnd, "\n"); + break; + } + } +} + +#endif diff --git a/source3/rpcclient/display_sec.c b/source3/rpcclient/display_sec.c new file mode 100644 index 0000000000..37043c5012 --- /dev/null +++ b/source3/rpcclient/display_sec.c @@ -0,0 +1,144 @@ +/* + Unix SMB/CIFS implementation. + Samba utility functions + Copyright (C) Andrew Tridgell 1992-1999 + Copyright (C) Luke Kenneth Casson Leighton 1996 - 1999 + + 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" +#include "rpcclient.h" + +/**************************************************************************** +convert a security permissions into a string +****************************************************************************/ +char *get_sec_mask_str(uint32 type) +{ + static fstring typestr=""; + + typestr[0] = 0; + + if (type & GENERIC_ALL_ACCESS) + fstrcat(typestr, "Generic all access "); + if (type & GENERIC_EXECUTE_ACCESS) + fstrcat(typestr, "Generic execute access "); + if (type & GENERIC_WRITE_ACCESS) + fstrcat(typestr, "Generic write access "); + if (type & GENERIC_READ_ACCESS) + fstrcat(typestr, "Generic read access "); + if (type & MAXIMUM_ALLOWED_ACCESS) + fstrcat(typestr, "MAXIMUM_ALLOWED_ACCESS "); + if (type & SYSTEM_SECURITY_ACCESS) + fstrcat(typestr, "SYSTEM_SECURITY_ACCESS "); + if (type & SYNCHRONIZE_ACCESS) + fstrcat(typestr, "SYNCHRONIZE_ACCESS "); + if (type & WRITE_OWNER_ACCESS) + fstrcat(typestr, "WRITE_OWNER_ACCESS "); + if (type & WRITE_DAC_ACCESS) + fstrcat(typestr, "WRITE_DAC_ACCESS "); + if (type & READ_CONTROL_ACCESS) + fstrcat(typestr, "READ_CONTROL_ACCESS "); + if (type & DELETE_ACCESS) + fstrcat(typestr, "DELETE_ACCESS "); + + printf("\t\tSpecific bits: 0x%lx\n", type&SPECIFIC_RIGHTS_MASK); + + return typestr; +} + +/**************************************************************************** + display sec_access structure + ****************************************************************************/ +void display_sec_access(SEC_ACCESS *info) +{ + printf("\t\tPermissions: 0x%x: %s\n", info->mask, get_sec_mask_str(info->mask)); +} + +/**************************************************************************** + display sec_ace structure + ****************************************************************************/ +void display_sec_ace(SEC_ACE *ace) +{ + fstring sid_str; + + printf("\tACE\n\t\ttype: "); + switch (ace->type) { + case SEC_ACE_TYPE_ACCESS_ALLOWED: + printf("ACCESS ALLOWED"); + break; + case SEC_ACE_TYPE_ACCESS_DENIED: + printf("ACCESS DENIED"); + break; + case SEC_ACE_TYPE_SYSTEM_AUDIT: + printf("SYSTEM AUDIT"); + break; + case SEC_ACE_TYPE_SYSTEM_ALARM: + printf("SYSTEM ALARM"); + break; + default: + printf("????"); + break; + } + printf(" (%d) flags: %d\n", ace->type, ace->flags); + display_sec_access(&ace->info); + sid_to_string(sid_str, &ace->trustee); + printf("\t\tSID: %s\n\n", sid_str); +} + +/**************************************************************************** + display sec_acl structure + ****************************************************************************/ +void display_sec_acl(SEC_ACL *sec_acl) +{ + int i; + + printf("\tACL\tNum ACEs:\t%d\trevision:\t%x\n", + sec_acl->num_aces, sec_acl->revision); + printf("\t---\n"); + + if (sec_acl->size != 0 && sec_acl->num_aces != 0) + for (i = 0; i < sec_acl->num_aces; i++) + display_sec_ace(&sec_acl->ace[i]); + +} + +/**************************************************************************** + display sec_desc structure + ****************************************************************************/ +void display_sec_desc(SEC_DESC *sec) +{ + fstring sid_str; + + if (sec->sacl) { + printf("SACL\n"); + display_sec_acl(sec->sacl); + } + + if (sec->dacl) { + printf("DACL\n"); + display_sec_acl(sec->dacl); + } + + if (sec->owner_sid) { + sid_to_string(sid_str, sec->owner_sid); + printf("\tOwner SID:\t%s\n", sid_str); + } + + if (sec->grp_sid) { + sid_to_string(sid_str, sec->grp_sid); + printf("\tParent SID:\t%s\n", sid_str); + } +} diff --git a/source3/rpcclient/display_spool.c b/source3/rpcclient/display_spool.c new file mode 100644 index 0000000000..b4baf570f1 --- /dev/null +++ b/source3/rpcclient/display_spool.c @@ -0,0 +1,927 @@ +/* + Unix SMB/CIFS implementation. + Samba utility functions + Copyright (C) Andrew Tridgell 1992-1999 + Copyright (C) Luke Kenneth Casson Leighton 1996 - 1999 + + 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" + +/**************************************************************************** +printer info level 0 display function +****************************************************************************/ +static void display_print_info_0(FILE *out_hnd, PRINTER_INFO_0 *i1) +{ + fstring name; + fstring server; + if (i1 == NULL) + return; + + rpcstr_pull(name, i1->printername.buffer, sizeof(name), 0, STR_TERMINATE); + rpcstr_pull(server, i1->servername.buffer, sizeof(server), 0, STR_TERMINATE); + + report(out_hnd, "\tprintername:[%s]\n", name); + report(out_hnd, "\tservername:[%s]\n", server); + report(out_hnd, "\tcjobs:[%x]\n", i1->cjobs); + report(out_hnd, "\ttotal_jobs:[%x]\n", i1->total_jobs); + + report(out_hnd, "\t:date: [%d]-[%d]-[%d] (%d)\n", i1->year, i1->month, i1->day, i1->dayofweek); + report(out_hnd, "\t:time: [%d]-[%d]-[%d]-[%d]\n", i1->hour, i1->minute, i1->second, i1->milliseconds); + + report(out_hnd, "\tglobal_counter:[%x]\n", i1->global_counter); + report(out_hnd, "\ttotal_pages:[%x]\n", i1->total_pages); + + report(out_hnd, "\tmajorversion:[%x]\n", i1->major_version); + report(out_hnd, "\tbuildversion:[%x]\n", i1->build_version); + + report(out_hnd, "\tunknown7:[%x]\n", i1->unknown7); + report(out_hnd, "\tunknown8:[%x]\n", i1->unknown8); + report(out_hnd, "\tunknown9:[%x]\n", i1->unknown9); + report(out_hnd, "\tsession_counter:[%x]\n", i1->session_counter); + report(out_hnd, "\tunknown11:[%x]\n", i1->unknown11); + report(out_hnd, "\tprinter_errors:[%x]\n", i1->printer_errors); + report(out_hnd, "\tunknown13:[%x]\n", i1->unknown13); + report(out_hnd, "\tunknown14:[%x]\n", i1->unknown14); + report(out_hnd, "\tunknown15:[%x]\n", i1->unknown15); + report(out_hnd, "\tunknown16:[%x]\n", i1->unknown16); + report(out_hnd, "\tchange_id:[%x]\n", i1->change_id); + report(out_hnd, "\tunknown18:[%x]\n", i1->unknown18); + report(out_hnd, "\tstatus:[%x]\n", i1->status); + report(out_hnd, "\tunknown20:[%x]\n", i1->unknown20); + report(out_hnd, "\tc_setprinter:[%x]\n", i1->c_setprinter); + report(out_hnd, "\tunknown22:[%x]\n", i1->unknown22); + report(out_hnd, "\tunknown23:[%x]\n", i1->unknown23); + report(out_hnd, "\tunknown24:[%x]\n", i1->unknown24); + report(out_hnd, "\tunknown25:[%x]\n", i1->unknown25); + report(out_hnd, "\tunknown26:[%x]\n", i1->unknown26); + report(out_hnd, "\tunknown27:[%x]\n", i1->unknown27); + report(out_hnd, "\tunknown28:[%x]\n", i1->unknown28); + report(out_hnd, "\tunknown29:[%x]\n", i1->unknown29); +} + +/**************************************************************************** +printer info level 1 display function +****************************************************************************/ +static void display_print_info_1(FILE *out_hnd, PRINTER_INFO_1 *i1) +{ + fstring desc; + fstring name; + fstring comm; + if (i1 == NULL) + return; + + rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE); + rpcstr_pull(desc, i1->description.buffer, sizeof(desc), 0, STR_TERMINATE); + rpcstr_pull(comm, i1->comment.buffer, sizeof(comm), 0, STR_TERMINATE); + + report(out_hnd, "\tflags:[%x]\n", i1->flags); + report(out_hnd, "\tname:[%s]\n", name); + report(out_hnd, "\tdescription:[%s]\n", desc); + report(out_hnd, "\tcomment:[%s]\n\n", comm); +} + +/**************************************************************************** +printer info level 2 display function +****************************************************************************/ +static void display_print_info_2(FILE *out_hnd, PRINTER_INFO_2 *i2) +{ + fstring servername; + fstring printername; + fstring sharename; + fstring portname; + fstring drivername; + fstring comment; + fstring location; + fstring sepfile; + fstring printprocessor; + fstring datatype; + fstring parameters; + + if (i2 == NULL) + return; + + rpcstr_pull(servername, i2->servername.buffer,sizeof(servername), 0, STR_TERMINATE); + rpcstr_pull(printername, i2->printername.buffer,sizeof(printername), 0, STR_TERMINATE); + rpcstr_pull(sharename, i2->sharename.buffer,sizeof(sharename), 0, STR_TERMINATE); + rpcstr_pull(portname, i2->portname.buffer,sizeof(portname), 0, STR_TERMINATE); + rpcstr_pull(drivername, i2->drivername.buffer,sizeof(drivername), 0, STR_TERMINATE); + rpcstr_pull(comment, i2->comment.buffer,sizeof(comment), 0, STR_TERMINATE); + rpcstr_pull(location, i2->location.buffer,sizeof(location), 0, STR_TERMINATE); + rpcstr_pull(sepfile, i2->sepfile.buffer,sizeof(sepfile), 0, STR_TERMINATE); + rpcstr_pull(printprocessor, i2->printprocessor.buffer,sizeof(printprocessor), 0, STR_TERMINATE); + rpcstr_pull(datatype, i2->datatype.buffer,sizeof(datatype), 0, STR_TERMINATE); + rpcstr_pull(parameters, i2->parameters.buffer,sizeof(parameters), 0, STR_TERMINATE); + + report(out_hnd, "\tservername:[%s]\n", servername); + report(out_hnd, "\tprintername:[%s]\n", printername); + report(out_hnd, "\tsharename:[%s]\n", sharename); + report(out_hnd, "\tportname:[%s]\n", portname); + report(out_hnd, "\tdrivername:[%s]\n", drivername); + report(out_hnd, "\tcomment:[%s]\n", comment); + report(out_hnd, "\tlocation:[%s]\n", location); + report(out_hnd, "\tsepfile:[%s]\n", sepfile); + report(out_hnd, "\tprintprocessor:[%s]\n", printprocessor); + report(out_hnd, "\tdatatype:[%s]\n", datatype); + report(out_hnd, "\tparameters:[%s]\n", parameters); + report(out_hnd, "\tattributes:[%x]\n", i2->attributes); + report(out_hnd, "\tpriority:[%x]\n", i2->priority); + report(out_hnd, "\tdefaultpriority:[%x]\n", i2->defaultpriority); + report(out_hnd, "\tstarttime:[%x]\n", i2->starttime); + report(out_hnd, "\tuntiltime:[%x]\n", i2->untiltime); + report(out_hnd, "\tstatus:[%x]\n", i2->status); + report(out_hnd, "\tcjobs:[%x]\n", i2->cjobs); + report(out_hnd, "\taverageppm:[%x]\n\n", i2->averageppm); + + if (i2->secdesc != NULL) + { + display_sec_desc(out_hnd, ACTION_HEADER , i2->secdesc); + display_sec_desc(out_hnd, ACTION_ENUMERATE, i2->secdesc); + display_sec_desc(out_hnd, ACTION_FOOTER , i2->secdesc); + } +} + +/**************************************************************************** +printer info level 3 display function +****************************************************************************/ +static void display_print_info_3(FILE *out_hnd, PRINTER_INFO_3 *i3) +{ + if (i3 == NULL) + return; + + report(out_hnd, "\tflags:[%x]\n", i3->flags); + + display_sec_desc(out_hnd, ACTION_HEADER , i3->secdesc); + display_sec_desc(out_hnd, ACTION_ENUMERATE, i3->secdesc); + display_sec_desc(out_hnd, ACTION_FOOTER , i3->secdesc); +} + +/**************************************************************************** +connection info level 0 container display function +****************************************************************************/ +static void display_printer_info_0_ctr(FILE *out_hnd, enum action_type action, uint32 count, PRINTER_INFO_CTR ctr) +{ + int i; + PRINTER_INFO_0 *in; + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Printer Info Level 0:\n"); + break; + case ACTION_ENUMERATE: + for (i = 0; i < count; i++) { + in=ctr.printers_0; + display_print_info_0(out_hnd, &(in[i]) ); + } + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info level 1 container display function +****************************************************************************/ +static void display_printer_info_1_ctr(FILE *out_hnd, enum action_type action, uint32 count, PRINTER_INFO_CTR ctr) +{ + int i; + PRINTER_INFO_1 *in; + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Printer Info Level 1:\n"); + break; + case ACTION_ENUMERATE: + for (i = 0; i < count; i++) { + in=ctr.printers_1; + display_print_info_1(out_hnd, &(in[i]) ); + } + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info level 2 container display function +****************************************************************************/ +static void display_printer_info_2_ctr(FILE *out_hnd, enum action_type action, uint32 count, PRINTER_INFO_CTR ctr) +{ + int i; + PRINTER_INFO_2 *in; + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Printer Info Level 2:\n"); + break; + case ACTION_ENUMERATE: + for (i = 0; i < count; i++) { + in=ctr.printers_2; + display_print_info_2(out_hnd, &(in[i]) ); + } + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info level 3 container display function +****************************************************************************/ +static void display_printer_info_3_ctr(FILE *out_hnd, enum action_type action, uint32 count, PRINTER_INFO_CTR ctr) +{ + int i; + PRINTER_INFO_3 *in; + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Printer Info Level 3:\n"); + break; + case ACTION_ENUMERATE: + for (i = 0; i < count; i++) { + in=ctr.printers_3; + display_print_info_3(out_hnd, &(in[i]) ); + } + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info container display function +****************************************************************************/ +void display_printer_info_ctr(FILE *out_hnd, enum action_type action, uint32 level, + uint32 count, PRINTER_INFO_CTR ctr) +{ + switch (level) { + case 0: + display_printer_info_0_ctr(out_hnd, action, count, ctr); + break; + case 1: + display_printer_info_1_ctr(out_hnd, action, count, ctr); + break; + case 2: + display_printer_info_2_ctr(out_hnd, action, count, ctr); + break; + case 3: + display_printer_info_3_ctr(out_hnd, action, count, ctr); + break; + default: + report(out_hnd, "display_printer_info_ctr: Unknown Info Level\n"); + break; + } +} + +/**************************************************************************** +connection info level 3 container display function +****************************************************************************/ +static void display_port_info_1_ctr(FILE *out_hnd, enum action_type action, + uint32 count, PORT_INFO_CTR *ctr) +{ + uint32 i = 0; + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Port Info Level 1:\n"); + break; + case ACTION_ENUMERATE: + for (i=0; i<count; i++) + display_port_info_1(out_hnd, action, &ctr->port.info_1[i]); + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info level 3 container display function +****************************************************************************/ +static void display_port_info_2_ctr(FILE *out_hnd, enum action_type action, + uint32 count, PORT_INFO_CTR *ctr) +{ + uint32 i = 0; + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Port Info Level 2:\n"); + break; + case ACTION_ENUMERATE: + for (i=0; i<count; i++) + display_port_info_2(out_hnd, action, &ctr->port.info_2[i]); + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info container display function +****************************************************************************/ +void display_port_info_ctr(FILE *out_hnd, enum action_type action, uint32 level, + uint32 count, PORT_INFO_CTR *ctr) +{ + switch (level) { + case 1: + display_port_info_1_ctr(out_hnd, action, count, ctr); + break; + case 2: + display_port_info_2_ctr(out_hnd, action, count, ctr); + break; + default: + report(out_hnd, "display_port_info_ctr: Unknown Info Level\n"); + break; + } +} + +/**************************************************************************** +connection info container display function +****************************************************************************/ +void display_port_info_1(FILE *out_hnd, enum action_type action, PORT_INFO_1 *i1) +{ + fstring buffer; + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Port:\n"); + break; + case ACTION_ENUMERATE: + rpcstr_pull(buffer, i1->port_name.buffer, sizeof(bufferi), 0, STR_TERMINATE); + fprintf (out_hnd, "\tPort Name:\t[%s]\n\n", buffer); + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info container display function +****************************************************************************/ +void display_port_info_2(FILE *out_hnd, enum action_type action, PORT_INFO_2 *i2) +{ + fstring buffer; + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Port:\n"); + break; + case ACTION_ENUMERATE: + rpcstr_pull(buffer, i2->port_name.buffer, sizeof(buffer), 0, STR_TERMINATE); + fprintf (out_hnd, "\tPort Name:\t[%s]\n", buffer); + rpcstr_pull(buffer, i2->monitor_name.buffer, sizeof(buffer), 0, STR_TERMINATE); + + fprintf (out_hnd, "\tMonitor Name:\t[%s]\n", buffer); + rpcstr_pull(buffer, i2->description.buffer, sizeof(buffer), 0, STR_TERMINATE); + fprintf (out_hnd, "\tDescription:\t[%s]\n", buffer); + fprintf (out_hnd, "\tPort Type:\t[%d]\n", i2->port_type); + fprintf (out_hnd, "\tReserved:\t[%d]\n", i2->reserved); + fprintf (out_hnd, "\n"); + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info container display function +****************************************************************************/ +void display_printer_enumdata(FILE *out_hnd, enum action_type action, uint32 idx, + uint32 valuelen, uint16 *value, uint32 rvaluelen, + uint32 type, + uint32 datalen, uint8 *data, uint32 rdatalen) +{ + fstring buffer; + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Printer enum data:\n"); + report(out_hnd, "index\tvaluelen\tvalue\t\trvaluelen"); + report(out_hnd, "\ttype\tdatalen\tdata\trdatalen\n"); + break; + case ACTION_ENUMERATE: + report(out_hnd, "[%d]", idx); + report(out_hnd, "\t[%d]", valuelen); + rpcstr_pull(buffer, value, sizeof(buffer), 0, STR_TERMINATE); + report(out_hnd, "\t[%s]", buffer); + report(out_hnd, "\t[%d]", rvaluelen); + report(out_hnd, "\t\t[%d]", type); + report(out_hnd, "\t[%d]", datalen); +/* report(out_hnd, "\t[%s]", data);*/ + report(out_hnd, "\t[%d]\n", rdatalen); + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +job info level 2 display function +****************************************************************************/ +void display_job_info_2(FILE *out_hnd, enum action_type action, + JOB_INFO_2 *const i2) +{ + if (i2 == NULL) + { + return; + } + + switch (action) + { + case ACTION_HEADER: + { + report(out_hnd, "Job Info Level 2:\n"); + + break; + } + case ACTION_ENUMERATE: + { + fstring tmp; + + report(out_hnd, "\tjob id:\t%d\n", i2->jobid); + rpcstr_pull(tmp, i2->printername.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tprinter name:\t%s\n", tmp); + rpcstr_pull(tmp, i2->machinename.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tmachine name:\t%s\n", tmp); + rpcstr_pull(tmp, i2->username.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tusername:\t%s\n", tmp); + rpcstr_pull(tmp, i2->document.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tdocument:\t%s\n", tmp); + rpcstr_pull(tmp, i2->notifyname.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tnotify name:\t%s\n", tmp); + rpcstr_pull(tmp, i2->datatype.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tdata type:\t%s\n", tmp); + rpcstr_pull(tmp, i2->printprocessor.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tprint processor:\t%s\n", tmp); + rpcstr_pull(tmp, i2->parameters.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tparameters:\t%s\n", tmp); + rpcstr_pull(tmp, i2->drivername.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tdriver name:\t%s\n", tmp); + report(out_hnd, "\tDevice Mode:\tNOT DISPLAYED YET\n"); + + rpcstr_pull(tmp, i2->text_status.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\ttext status:\t%s\n", tmp); + /* SEC_DESC sec_desc;*/ + report(out_hnd, "\tstatus:\t%d\n", i2->status); + report(out_hnd, "\tpriority:\t%d\n", i2->priority); + report(out_hnd, "\tposition:\t%d\n", i2->position); + report(out_hnd, "\tstarttime:\t%d\n", i2->starttime); + report(out_hnd, "\tuntiltime:\t%d\n", i2->untiltime); + report(out_hnd, "\ttotalpages:\t%d\n", i2->totalpages); + report(out_hnd, "\tsize:\t%d\n", i2->size); +/* + SYSTEMTIME submitted; +*/ + report(out_hnd, "\tsubmitted:\tNOT DISPLAYED YET\n"); + report(out_hnd, "\ttimeelapsed:\t%d\n", i2->timeelapsed); + report(out_hnd, "\tpagesprinted:\t%d\n", i2->pagesprinted); + } + case ACTION_FOOTER: + { + report(out_hnd, "\n"); + break; + } + } + +} + +/**************************************************************************** +job info level 1 display function +****************************************************************************/ +void display_job_info_1(FILE *out_hnd, enum action_type action, + JOB_INFO_1 *const i1) +{ + if (i1 == NULL) + { + return; + } + + switch (action) + { + case ACTION_HEADER: + { + report(out_hnd, "Job Info Level 1:\n"); + + break; + } + case ACTION_ENUMERATE: + { + fstring tmp; + + report(out_hnd, "\tjob id:\t%d\n", i1->jobid); + rpcstr_pull(tmp, i1->printername.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tprinter name:\t%s\n", tmp); + rpcstr_pull(tmp, i1->machinename.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tmachine name:\t%s\n", tmp); + rpcstr_pull(tmp, i1->username.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tusername:\t%s\n", tmp); + rpcstr_pull(tmp, i1->document.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tdocument:\t%s\n", tmp); + rpcstr_pull(tmp, i1->datatype.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\tdata type:\t%s\n", tmp); + rpcstr_pull(tmp, i1->text_status.buffer, sizeof(tmp), 0, STR_TERMINATE); + report(out_hnd, "\ttext status:\t%s\n", tmp); + report(out_hnd, "\tstatus:\t%d\n", i1->status); + report(out_hnd, "\tpriority:\t%d\n", i1->priority); + report(out_hnd, "\tposition:\t%d\n", i1->position); + report(out_hnd, "\ttotalpages:\t%d\n", i1->totalpages); +/* + SYSTEMTIME submitted; +*/ + report(out_hnd, "\tsubmitted:\tNOT DISPLAYED YET\n"); + report(out_hnd, "\tpagesprinted:\t%d\n", i1->pagesprinted); + + break; + } + case ACTION_FOOTER: + { + report(out_hnd, "\n"); + break; + } + } + +} + +/**************************************************************************** +connection info level 2 container display function +****************************************************************************/ +void display_job_info_2_ctr(FILE *out_hnd, enum action_type action, + uint32 count, JOB_INFO_2 *const *const ctr) +{ + if (ctr == NULL) + { + report(out_hnd, "display_job_info_2_ctr: unavailable due to an internal error\n"); + return; + } + + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + int i; + + for (i = 0; i < count; i++) + { + display_job_info_2(out_hnd, ACTION_HEADER , ctr[i]); + display_job_info_2(out_hnd, ACTION_ENUMERATE, ctr[i]); + display_job_info_2(out_hnd, ACTION_FOOTER , ctr[i]); + } + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** +connection info level 1 container display function +****************************************************************************/ +void display_job_info_1_ctr(FILE *out_hnd, enum action_type action, + uint32 count, JOB_INFO_1 *const *const ctr) +{ + if (ctr == NULL) + { + report(out_hnd, "display_job_info_1_ctr: unavailable due to an internal error\n"); + return; + } + + switch (action) + { + case ACTION_HEADER: + { + break; + } + case ACTION_ENUMERATE: + { + int i; + + for (i = 0; i < count; i++) + { + display_job_info_1(out_hnd, ACTION_HEADER , ctr[i]); + display_job_info_1(out_hnd, ACTION_ENUMERATE, ctr[i]); + display_job_info_1(out_hnd, ACTION_FOOTER , ctr[i]); + } + break; + } + case ACTION_FOOTER: + { + break; + } + } +} + +/**************************************************************************** +connection info container display function +****************************************************************************/ +void display_job_info_ctr(FILE *out_hnd, enum action_type action, + uint32 level, uint32 count, + void *const *const ctr) +{ + if (ctr == NULL) + { + report(out_hnd, "display_job_info_ctr: unavailable due to an internal error\n"); + return; + } + + switch (level) + { + case 1: + { + display_job_info_1_ctr(out_hnd, action, + count, (JOB_INFO_1*const*const)ctr); + break; + } + case 2: + { + display_job_info_2_ctr(out_hnd, action, + count, (JOB_INFO_2*const*const)ctr); + break; + } + default: + { + report(out_hnd, "display_job_info_ctr: Unknown Info Level\n"); + break; + } + } +} + +/**************************************************************************** +printer info level 0 display function +****************************************************************************/ +static void display_print_driver_1(FILE *out_hnd, DRIVER_INFO_1 *i1) +{ + fstring name; + if (i1 == NULL) + return; + + rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE); + + report(out_hnd, "\tname:[%s]\n", name); +} + +/**************************************************************************** +printer info level 1 display function +****************************************************************************/ +static void display_print_driver_2(FILE *out_hnd, DRIVER_INFO_2 *i1) +{ + fstring name; + fstring architecture; + fstring driverpath; + fstring datafile; + fstring configfile; + if (i1 == NULL) + return; + + rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE); + rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), 0, STR_TERMINATE); + rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), 0, STR_TERMINATE); + rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), 0, STR_TERMINATE); + rpcstr_pull(configfile, i1->conigfile.buffer, sizeof(configfile), 0, STR_TERMINATE); + + report(out_hnd, "\tversion:[%x]\n", i1->version); + report(out_hnd, "\tname:[%s]\n", name); + report(out_hnd, "\tarchitecture:[%s]\n", architecture); + report(out_hnd, "\tdriverpath:[%s]\n", driverpath); + report(out_hnd, "\tdatafile:[%s]\n", datafile); + report(out_hnd, "\tconfigfile:[%s]\n", configfile); +} + +/**************************************************************************** +printer info level 2 display function +****************************************************************************/ +static void display_print_driver_3(FILE *out_hnd, DRIVER_INFO_3 *i1) +{ + fstring name; + fstring architecture; + fstring driverpath; + fstring datafile; + fstring configfile; + fstring helpfile; + fstring dependentfiles; + fstring monitorname; + fstring defaultdatatype; + + int length=0; + BOOL valid = True; + + if (i1 == NULL) + return; + + rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE); + rpcstr_pull(architecture, i1->architecture.buffer, sizeof(architecture), 0, STR_TERMINATE); + rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), 0, STR_TERMINATE); + rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), 0, STR_TERMINATE); + rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), 0, STR_TERMINATE); + rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), 0, STR_TERMINATE); + rpcstr_pull(monitorname, i1->monitorname.buffer, sizeof(monitorname), 0, STR_TERMINATE); + rpcstr_pull(defaultdatatype, i1->defaultdatatype.buffer, sizeof(defaultdatatype), 0, STR_TERMINATE); + + report(out_hnd, "\tversion:[%x]\n", i1->version); + report(out_hnd, "\tname:[%s]\n",name); + report(out_hnd, "\tarchitecture:[%s]\n", architecture); + report(out_hnd, "\tdriverpath:[%s]\n", driverpath); + report(out_hnd, "\tdatafile:[%s]\n", datafile); + report(out_hnd, "\tconfigfile:[%s]\n", configfile); + report(out_hnd, "\thelpfile:[%s]\n\n", helpfile); + + while (valid) + { + rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), 0, STR_TERMINATE); + length+=strlen(dependentfiles)+1; + + if (strlen(dependentfiles) > 0) + { + report(out_hnd, "\tdependentfiles:[%s]\n", dependentfiles); + } + else + { + valid = False; + } + } + + report(out_hnd, "\n\tmonitorname:[%s]\n", monitorname); + report(out_hnd, "\tdefaultdatatype:[%s]\n", defaultdatatype); + +} + +/**************************************************************************** +connection info level 1 container display function +****************************************************************************/ +static void display_printer_driver_1_ctr(FILE *out_hnd, enum action_type action, uint32 count, PRINTER_DRIVER_CTR ctr) +{ + int i; + DRIVER_INFO_1 *in; + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Printer driver Level 1:\n"); + break; + case ACTION_ENUMERATE: + for (i = 0; i < count; i++) { + in=ctr.info1; + display_print_driver_1(out_hnd, &(in[i]) ); + } + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info level 2 container display function +****************************************************************************/ +static void display_printer_driver_2_ctr(FILE *out_hnd, enum action_type action, uint32 count, PRINTER_DRIVER_CTR ctr) +{ + int i; + DRIVER_INFO_2 *in; + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Printer driver Level 2:\n"); + break; + case ACTION_ENUMERATE: + for (i = 0; i < count; i++) { + in=ctr.info2; + display_print_driver_2(out_hnd, &(in[i]) ); + } + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info level 3 container display function +****************************************************************************/ +static void display_printer_driver_3_ctr(FILE *out_hnd, enum action_type action, uint32 count, PRINTER_DRIVER_CTR ctr) +{ + int i; + DRIVER_INFO_3 *in; + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Printer driver Level 3:\n"); + break; + case ACTION_ENUMERATE: + for (i = 0; i < count; i++) { + in=ctr.info3; + display_print_driver_3(out_hnd, &(in[i]) ); + } + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info container display function +****************************************************************************/ +void display_printer_driver_ctr(FILE *out_hnd, enum action_type action, uint32 level, + uint32 count, PRINTER_DRIVER_CTR ctr) +{ + switch (level) { + case 1: + display_printer_driver_1_ctr(out_hnd, action, count, ctr); + break; + case 2: + display_printer_driver_2_ctr(out_hnd, action, count, ctr); + break; + case 3: + display_printer_driver_3_ctr(out_hnd, action, count, ctr); + break; + default: + report(out_hnd, "display_printer_driver_ctr: Unknown Info Level\n"); + break; + } +} + + +/**************************************************************************** +printer info level 1 display function +****************************************************************************/ +static void display_printdriverdir_info_1(FILE *out_hnd, DRIVER_DIRECTORY_1 *i1) +{ + fstring name; + if (i1 == NULL) + return; + + rpcstr_pull(name, i1->name.buffer, sizeof(name), 0, STR_TERMINATE); + + report(out_hnd, "\tname:[%s]\n", name); +} + +/**************************************************************************** +connection info level 1 container display function +****************************************************************************/ +static void display_printerdriverdir_info_1_ctr(FILE *out_hnd, enum action_type action, DRIVER_DIRECTORY_CTR ctr) +{ + + switch (action) + { + case ACTION_HEADER: + report(out_hnd, "Printer driver dir Info Level 1:\n"); + break; + case ACTION_ENUMERATE: + display_printdriverdir_info_1(out_hnd, &(ctr.driver.info_1) ); + break; + case ACTION_FOOTER: + report(out_hnd, "\n"); + break; + } +} + +/**************************************************************************** +connection info container display function +****************************************************************************/ +void display_printerdriverdir_info_ctr(FILE *out_hnd, enum action_type action, uint32 level, + DRIVER_DIRECTORY_CTR ctr) +{ + switch (level) { + case 1: + display_printerdriverdir_info_1_ctr(out_hnd, action, ctr); + break; + default: + report(out_hnd, "display_printerdriverdir_info_ctr: Unknown Info Level\n"); + break; + } +} diff --git a/source3/rpcclient/rpcclient.c b/source3/rpcclient/rpcclient.c new file mode 100644 index 0000000000..193c27e8a0 --- /dev/null +++ b/source3/rpcclient/rpcclient.c @@ -0,0 +1,811 @@ +/* + Unix SMB/CIFS implementation. + RPC pipe client + + Copyright (C) Tim Potter 2000-2001 + + 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" +#include "rpcclient.h" + +DOM_SID domain_sid; + +/* List to hold groups of commands */ + +static struct cmd_list { + struct cmd_list *prev, *next; + struct cmd_set *cmd_set; +} *cmd_list; + +/**************************************************************************** +handle completion of commands for readline +****************************************************************************/ +static char **completion_fn(char *text, int start, int end) +{ +#define MAX_COMPLETIONS 100 + char **matches; + int i, count=0; + struct cmd_list *commands = cmd_list; + +#if 0 /* JERRY */ + /* FIXME!!! -- what to do when completing argument? */ + /* for words not at the start of the line fallback + to filename completion */ + if (start) + return NULL; +#endif + + /* make sure we have a list of valid commands */ + if (!commands) + return NULL; + + matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS); + if (!matches) return NULL; + + matches[count++] = strdup(text); + if (!matches[0]) return NULL; + + while (commands && count < MAX_COMPLETIONS-1) + { + if (!commands->cmd_set) + break; + + for (i=0; commands->cmd_set[i].name; i++) + { + if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) && + commands->cmd_set[i].fn) + { + matches[count] = strdup(commands->cmd_set[i].name); + if (!matches[count]) + return NULL; + count++; + } + } + + commands = commands->next; + + } + + if (count == 2) { + SAFE_FREE(matches[0]); + matches[0] = strdup(matches[1]); + } + matches[count] = NULL; + return matches; +} + +/*********************************************************************** + * read in username/password credentials from a file + */ +static void read_authfile ( + char *filename, + char* username, + char* password, + char* domain +) +{ + FILE *auth; + fstring buf; + uint16 len = 0; + char *ptr, *val, *param; + + if ((auth=sys_fopen(filename, "r")) == NULL) + { + printf ("ERROR: Unable to open credentials file!\n"); + return; + } + + while (!feof(auth)) + { + /* get a line from the file */ + if (!fgets (buf, sizeof(buf), auth)) + continue; + + len = strlen(buf); + + /* skip empty lines */ + if ((len) && (buf[len-1]=='\n')) + { + buf[len-1] = '\0'; + len--; + } + if (len == 0) + continue; + + /* break up the line into parameter & value. + will need to eat a little whitespace possibly */ + param = buf; + if (!(ptr = strchr_m(buf, '='))) + continue; + val = ptr+1; + *ptr = '\0'; + + /* eat leading white space */ + while ((*val!='\0') && ((*val==' ') || (*val=='\t'))) + val++; + + if (strwicmp("password", param) == 0) + fstrcpy (password, val); + else if (strwicmp("username", param) == 0) + fstrcpy (username, val); + else if (strwicmp("domain", param) == 0) + fstrcpy (domain, val); + + memset(buf, 0, sizeof(buf)); + } + fclose(auth); + + return; +} + +static char* next_command (char** cmdstr) +{ + static pstring command; + char *p; + + if (!cmdstr || !(*cmdstr)) + return NULL; + + p = strchr_m(*cmdstr, ';'); + if (p) + *p = '\0'; + pstrcpy(command, *cmdstr); + *cmdstr = p; + + return command; +} + +static void get_username (char *username) +{ + if (getenv("USER")) + pstrcpy(username,getenv("USER")); + + if (*username == 0 && getenv("LOGNAME")) + pstrcpy(username,getenv("LOGNAME")); + + if (*username == 0) { + pstrcpy(username,"GUEST"); + } + + return; +} + +/* Fetch the SID for this computer */ + +void fetch_machine_sid(struct cli_state *cli) +{ + POLICY_HND pol; + NTSTATUS result = NT_STATUS_OK; + uint32 info_class = 5; + fstring domain_name; + static BOOL got_domain_sid; + TALLOC_CTX *mem_ctx; + + if (got_domain_sid) return; + + if (!(mem_ctx=talloc_init())) + { + DEBUG(0,("fetch_domain_sid: talloc_init returned NULL!\n")); + goto error; + } + + + if (!cli_nt_session_open (cli, PIPE_LSARPC)) { + fprintf(stderr, "could not initialise lsa pipe\n"); + goto error; + } + + result = cli_lsa_open_policy(cli, mem_ctx, True, + SEC_RIGHTS_MAXIMUM_ALLOWED, + &pol); + if (!NT_STATUS_IS_OK(result)) { + goto error; + } + + result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class, + domain_name, &domain_sid); + if (!NT_STATUS_IS_OK(result)) { + goto error; + } + + got_domain_sid = True; + + cli_lsa_close(cli, mem_ctx, &pol); + cli_nt_session_close(cli); + talloc_destroy(mem_ctx); + + return; + + error: + fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain); + + if (!NT_STATUS_IS_OK(result)) { + fprintf(stderr, "error: %s\n", nt_errstr(result)); + } + + exit(1); +} + +/* List the available commands on a given pipe */ + +static NTSTATUS cmd_listcommands(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + struct cmd_list *tmp; + struct cmd_set *tmp_set; + int i; + + /* Usage */ + + if (argc != 2) { + printf("Usage: %s <pipe>\n", argv[0]); + return NT_STATUS_OK; + } + + /* Help on one command */ + + for (tmp = cmd_list; tmp; tmp = tmp->next) + { + tmp_set = tmp->cmd_set; + + if (!StrCaseCmp(argv[1], tmp_set->name)) + { + printf("Available commands on the %s pipe:\n\n", tmp_set->name); + + i = 0; + tmp_set++; + while(tmp_set->name) { + printf("%20s", tmp_set->name); + tmp_set++; + i++; + if (i%4 == 0) + printf("\n"); + } + + /* drop out of the loop */ + break; + } + } + printf("\n\n"); + + return NT_STATUS_OK; +} + +/* Display help on commands */ + +static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + struct cmd_list *tmp; + struct cmd_set *tmp_set; + + /* Usage */ + + if (argc > 2) { + printf("Usage: %s [command]\n", argv[0]); + return NT_STATUS_OK; + } + + /* Help on one command */ + + if (argc == 2) { + for (tmp = cmd_list; tmp; tmp = tmp->next) { + + tmp_set = tmp->cmd_set; + + while(tmp_set->name) { + if (strequal(argv[1], tmp_set->name)) { + if (tmp_set->usage && + tmp_set->usage[0]) + printf("%s\n", tmp_set->usage); + else + printf("No help for %s\n", tmp_set->name); + + return NT_STATUS_OK; + } + + tmp_set++; + } + } + + printf("No such command: %s\n", argv[1]); + return NT_STATUS_OK; + } + + /* List all commands */ + + for (tmp = cmd_list; tmp; tmp = tmp->next) { + + tmp_set = tmp->cmd_set; + + while(tmp_set->name) { + + printf("%15s\t\t%s\n", tmp_set->name, + tmp_set->description ? tmp_set->description: + ""); + + tmp_set++; + } + } + + return NT_STATUS_OK; +} + +/* Change the debug level */ + +static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + if (argc > 2) { + printf("Usage: %s [debuglevel]\n", argv[0]); + return NT_STATUS_OK; + } + + if (argc == 2) { + DEBUGLEVEL = atoi(argv[1]); + } + + printf("debuglevel is %d\n", DEBUGLEVEL); + + return NT_STATUS_OK; +} + +static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx, + int argc, char **argv) +{ + exit(0); + return NT_STATUS_OK; /* NOTREACHED */ +} + +/* Build in rpcclient commands */ + +static struct cmd_set rpcclient_commands[] = { + + { "GENERAL OPTIONS" }, + + { "help", cmd_help, NULL, "Get help on commands", "[command]" }, + { "?", cmd_help, NULL, "Get help on commands", "[command]" }, + { "debuglevel", cmd_debuglevel, NULL, "Set debug level", "level" }, + { "list", cmd_listcommands, NULL, "List available commands on <pipe>", "pipe" }, + { "exit", cmd_quit, NULL, "Exit program", "" }, + { "quit", cmd_quit, NULL, "Exit program", "" }, + + { NULL } +}; + +static struct cmd_set separator_command[] = { + { "---------------", NULL, NULL, "----------------------" }, + { NULL } +}; + + +/* Various pipe commands */ + +extern struct cmd_set lsarpc_commands[]; +extern struct cmd_set samr_commands[]; +extern struct cmd_set spoolss_commands[]; +extern struct cmd_set netlogon_commands[]; +extern struct cmd_set srvsvc_commands[]; +extern struct cmd_set dfs_commands[]; +extern struct cmd_set reg_commands[]; + +static struct cmd_set *rpcclient_command_list[] = { + rpcclient_commands, + lsarpc_commands, + samr_commands, + spoolss_commands, + netlogon_commands, + srvsvc_commands, + dfs_commands, + reg_commands, + NULL +}; + +static void add_command_set(struct cmd_set *cmd_set) +{ + struct cmd_list *entry; + + if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) { + DEBUG(0, ("out of memory\n")); + return; + } + + ZERO_STRUCTP(entry); + + entry->cmd_set = cmd_set; + DLIST_ADD(cmd_list, entry); +} + +static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry, + char *cmd) +{ + char *p = cmd, **argv = NULL; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + pstring buf; + int argc = 0, i; + + /* Count number of arguments first time through the loop then + allocate memory and strdup them. */ + + again: + while(next_token(&p, buf, " ", sizeof(buf))) { + if (argv) { + argv[argc] = strdup(buf); + } + + argc++; + } + + if (!argv) { + + /* Create argument list */ + + argv = (char **)malloc(sizeof(char *) * argc); + memset(argv, 0, sizeof(char *) * argc); + + if (!argv) { + fprintf(stderr, "out of memory\n"); + result = NT_STATUS_NO_MEMORY; + goto done; + } + + p = cmd; + argc = 0; + + goto again; + } + + /* Call the function */ + + if (cmd_entry->fn) { + TALLOC_CTX *mem_ctx; + + /* Create mem_ctx */ + + if (!(mem_ctx = talloc_init())) { + DEBUG(0, ("talloc_init() failed\n")); + goto done; + } + + /* Open pipe */ + + if (cmd_entry->pipe) + if (!cli_nt_session_open(cli, cmd_entry->pipe)) { + DEBUG(0, ("Could not initialise %s\n", + cmd_entry->pipe)); + goto done; + } + + /* Run command */ + + result = cmd_entry->fn(cli, mem_ctx, argc, argv); + + /* Cleanup */ + + if (cmd_entry->pipe) + cli_nt_session_close(cli); + + talloc_destroy(mem_ctx); + + } else { + fprintf (stderr, "Invalid command\n"); + goto done; + } + + done: + + /* Cleanup */ + + if (argv) { + for (i = 0; i < argc; i++) + SAFE_FREE(argv[i]); + + SAFE_FREE(argv); + } + + return result; +} + +/* Process a command entered at the prompt or as part of -c */ + +static NTSTATUS process_cmd(struct cli_state *cli, char *cmd) +{ + struct cmd_list *temp_list; + BOOL found = False; + pstring buf; + char *p = cmd; + NTSTATUS result = NT_STATUS_OK; + int len = 0; + + if (cmd[strlen(cmd) - 1] == '\n') + cmd[strlen(cmd) - 1] = '\0'; + + if (!next_token(&p, buf, " ", sizeof(buf))) { + return NT_STATUS_OK; + } + + /* strip the trainly \n if it exsists */ + len = strlen(buf); + if (buf[len-1] == '\n') + buf[len-1] = '\0'; + + /* Search for matching commands */ + + for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) { + struct cmd_set *temp_set = temp_list->cmd_set; + + while(temp_set->name) { + if (strequal(buf, temp_set->name)) { + found = True; + result = do_cmd(cli, temp_set, cmd); + + goto done; + } + temp_set++; + } + } + + done: + if (!found && buf[0]) { + printf("command not found: %s\n", buf); + return NT_STATUS_OK; + } + + if (!NT_STATUS_IS_OK(result)) { + printf("result was %s\n", nt_errstr(result)); + } + + return result; +} + + +/* Print usage information */ +static void usage(void) +{ + printf("Usage: rpcclient server [options]\n"); + + printf("\t-A or --authfile authfile file containing user credentials\n"); + printf("\t-c or --command \"command string\" execute semicolon separated cmds\n"); + printf("\t-d or --debug debuglevel set the debuglevel\n"); + printf("\t-l or --logfile logfile logfile to use instead of stdout\n"); + printf("\t-h or --help Print this help message.\n"); + printf("\t-N or --nopass don't ask for a password\n"); + printf("\t-s or --conf configfile specify an alternative config file\n"); + printf("\t-U or --user username set the network username\n"); + printf("\t-W or --workgroup domain set the domain name for user account\n"); + printf("\n"); +} + +/* Main function */ + + int main(int argc, char *argv[]) +{ + extern char *optarg; + extern int optind; + extern pstring global_myname; + static int got_pass = 0; + BOOL interactive = True; + int opt; + int olddebug; + static char *cmdstr = ""; + struct cli_state *cli; + fstring password="", + username="", + domain="", + server=""; + static char *opt_authfile=NULL, + *opt_username=NULL, + *opt_domain=NULL, + *opt_configfile=NULL, + *opt_logfile=NULL; + static int opt_debuglevel; + pstring logfile; + struct cmd_set **cmd_set; + struct in_addr server_ip; + NTSTATUS nt_status; + extern BOOL AllowDebugChange; + + /* make sure the vars that get altered (4th field) are in + a fixed location or certain compilers complain */ + poptContext pc; + struct poptOption long_options[] = { + {"authfile", 'A', POPT_ARG_STRING, &opt_authfile, 'A'}, + {"conf", 's', POPT_ARG_STRING, &opt_configfile, 's'}, + {"nopass", 'N', POPT_ARG_NONE, &got_pass}, + {"debug", 'd', POPT_ARG_INT, &opt_debuglevel, 'd'}, + {"debuglevel", 'd', POPT_ARG_INT, &opt_debuglevel, 'd'}, + {"user", 'U', POPT_ARG_STRING, &opt_username, 'U'}, + {"workgroup", 'W', POPT_ARG_STRING, &opt_domain, 'W'}, + {"command", 'c', POPT_ARG_STRING, &cmdstr}, + {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l'}, + {"help", 'h', POPT_ARG_NONE, 0, 'h'}, + { 0, 0, 0, 0} + }; + + + setlinebuf(stdout); + + DEBUGLEVEL = 1; + AllowDebugChange = False; + + /* Parse options */ + + if (argc == 1) { + usage(); + return 0; + } + + if (strncmp("//", argv[1], 2) == 0 || strncmp("\\\\", argv[1], 2) == 0) + argv[1] += 2; + + pstrcpy(server, argv[1]); + + argv++; + argc--; + + pc = poptGetContext(NULL, argc, (const char **) argv, long_options, + POPT_CONTEXT_KEEP_FIRST); + + while((opt = poptGetNextOpt(pc)) != -1) { + switch (opt) { + case 'A': + /* only get the username, password, and domain from the file */ + read_authfile (opt_authfile, username, + password, domain); + if (strlen (password)) + got_pass = 1; + break; + + case 'l': + slprintf(logfile, sizeof(logfile) - 1, "%s.client", + opt_logfile); + lp_set_logfile(logfile); + interactive = False; + break; + + case 's': + pstrcpy(dyn_CONFIGFILE, opt_configfile); + break; + + case 'd': + DEBUGLEVEL = opt_debuglevel; + break; + + case 'U': { + char *lp; + pstrcpy(username,opt_username); + if ((lp=strchr_m(username,'%'))) { + *lp = 0; + pstrcpy(password,lp+1); + got_pass = 1; + memset(strchr_m(opt_username,'%')+1,'X',strlen(password)); + } + break; + } + + case 'W': + pstrcpy(domain, opt_domain); + break; + + case 'h': + default: + usage(); + exit(1); + } + } + + poptFreeContext(pc); + + /* the following functions are part of the Samba debugging + facilities. See lib/debug.c */ + setup_logging("rpcclient", interactive); + if (!interactive) + reopen_logs(); + + /* Load smb.conf file */ + /* FIXME! How to get this DEBUGLEVEL to last over lp_load()? */ + olddebug = DEBUGLEVEL; + if (!lp_load(dyn_CONFIGFILE,True,False,False)) { + fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE); + } + DEBUGLEVEL = olddebug; + + load_interfaces(); + + get_myname((*global_myname)?NULL:global_myname); + strupper(global_myname); + + /* Resolve the IP address */ + + if (!resolve_name(server, &server_ip, 0x20)) { + DEBUG(1,("Unable to resolve %s\n", server)); + return 1; + } + + /* + * Get password + * from stdin if necessary + */ + + if (!got_pass) { + char *pass = getpass("Password:"); + if (pass) { + fstrcpy(password, pass); + } + } + + if (!strlen(username) && !got_pass) + get_username(username); + + nt_status = cli_full_connection(&cli, global_myname, server, + &server_ip, 0, + "IPC$", "IPC", + username, domain, + password, strlen(password)); + + if (!NT_STATUS_IS_OK(nt_status)) { + DEBUG(1,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status))); + return 1; + } + + memset(password,'X',sizeof(password)); + + /* Load command lists */ + + cmd_set = rpcclient_command_list; + + while(*cmd_set) { + add_command_set(*cmd_set); + add_command_set(separator_command); + cmd_set++; + } + + fetch_machine_sid(cli); + + /* Do anything specified with -c */ + if (cmdstr[0]) { + char *cmd; + char *p = cmdstr; + + while((cmd=next_command(&p)) != NULL) { + process_cmd(cli, cmd); + } + + cli_shutdown(cli); + return 0; + } + + /* Loop around accepting commands */ + + while(1) { + pstring prompt; + char *line; + + slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> "); + + line = smb_readline(prompt, NULL, completion_fn); + + if (line == NULL) + break; + + if (line[0] != '\n') + process_cmd(cli, line); + } + + cli_shutdown(cli); + return 0; +} diff --git a/source3/rpcclient/rpcclient.h b/source3/rpcclient/rpcclient.h new file mode 100644 index 0000000000..72491373d6 --- /dev/null +++ b/source3/rpcclient/rpcclient.h @@ -0,0 +1,34 @@ +/* + Unix SMB/CIFS implementation. + RPC pipe client + + Copyright (C) Tim Potter 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. +*/ + +#ifndef RPCCLIENT_H +#define RPCCLIENT_H + +struct cmd_set { + char *name; + NTSTATUS (*fn)(struct cli_state*, TALLOC_CTX *mem_ctx, int argc, + char **argv); + char *pipe; + char *description; + char *usage; +}; + +#endif /* RPCCLIENT_H */ diff --git a/source3/rpcclient/samsync.c b/source3/rpcclient/samsync.c new file mode 100644 index 0000000000..14f7ed8953 --- /dev/null +++ b/source3/rpcclient/samsync.c @@ -0,0 +1,619 @@ +/* + Unix SMB/CIFS implementation. + RPC pipe client + + Copyright (C) Tim Potter 2001 + + 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" + +static void decode_domain_info(SAM_DOMAIN_INFO *a) +{ + fstring temp; + printf("Domain Information\n"); + printf("------------------\n"); + + unistr2_to_ascii(temp, &a->uni_dom_name, sizeof(temp)-1); + printf("\tDomain :%s\n", temp); + printf("\tMin password len :%d\n", a->min_pwd_len); + printf("\tpassword history len:%d\n", a->pwd_history_len); + printf("\tcreation time :%s\n", http_timestring(nt_time_to_unix(&a->creation_time))); +} + +static void decode_sam_group_info(SAM_GROUP_INFO *a) +{ + fstring temp; + printf("\nDomain Group Information\n"); + printf("------------------------\n"); + + unistr2_to_ascii(temp, &a->uni_grp_name, sizeof(temp)-1); + printf("\tGroup name :%s\n", temp); + unistr2_to_ascii(temp, &a->uni_grp_desc, sizeof(temp)-1); + printf("\tGroup description :%s\n", temp); + printf("\trid :%d\n", a->gid.g_rid); + printf("\tattribute :%d\n", a->gid.attr); +} + +static void decode_sam_account_info(SAM_ACCOUNT_INFO *a) +{ + fstring temp; + printf("\nUser Information\n"); + printf("----------------\n"); + + unistr2_to_ascii(temp, &a->uni_acct_name, sizeof(temp)-1); + printf("\tUser name :%s\n", temp); + printf("\tuser's rid :%d\n", a->user_rid); + printf("\tuser's primary gid :%d\n", a->group_rid); + unistr2_to_ascii(temp, &a->uni_full_name, sizeof(temp)-1); + printf("\tfull name :%s\n", temp); + unistr2_to_ascii(temp, &a->uni_home_dir, sizeof(temp)-1); + printf("\thome directory :%s\n", temp); + unistr2_to_ascii(temp, &a->uni_dir_drive, sizeof(temp)-1); + printf("\tdrive :%s\n", temp); + unistr2_to_ascii(temp, &a->uni_logon_script, sizeof(temp)-1); + printf("\tlogon script :%s\n", temp); + unistr2_to_ascii(temp, &a->uni_acct_desc, sizeof(temp)-1); + printf("\tdescription :%s\n", temp); + unistr2_to_ascii(temp, &a->uni_workstations, sizeof(temp)-1); + printf("\tworkstations :%s\n", temp); +} + +static void decode_sam_grp_mem_info(SAM_GROUP_MEM_INFO *a) +{ + int i; + printf("\nGroup members information\n"); + printf("-------------------------\n"); + printf("\tnum members :%d\n", a->num_members); + + for (i=0; i<a->num_members; i++) { + printf("\trid, attr:%d, %d\n", a->rids[i], a->attribs[i]); + } +} + +static void decode_sam_alias_info(SAM_ALIAS_INFO *a) +{ + fstring temp; + printf("\nAlias Information\n"); + printf("-----------------\n"); + + unistr2_to_ascii(temp, &a->uni_als_name, sizeof(temp)-1); + printf("\tname :%s\n", temp); + unistr2_to_ascii(temp, &a->uni_als_desc, sizeof(temp)-1); + printf("\tdescription :%s\n", temp); + printf("\trid :%d\n", a->als_rid); +} + +static void decode_sam_als_mem_info(SAM_ALIAS_MEM_INFO *a) +{ + int i; + fstring temp; + printf("\nAlias members Information\n"); + printf("-------------------------\n"); + printf("\tnum members :%d\n", a->num_members); + printf("\tnum sids :%d\n", a->num_sids); + for (i=0; i<a->num_sids; i++) { + printf("\tsid :%s\n", sid_to_string(temp, &a->sids[i].sid)); + } + + +} + +static void decode_sam_dom_info(SAM_DELTA_DOM *a) +{ + fstring temp; + printf("\nDomain information\n"); + printf("------------------\n"); + + unistr2_to_ascii(temp, &a->domain_name, sizeof(temp)-1); + printf("\tdomain name :%s\n", temp); + printf("\tsid :%s\n", sid_to_string(temp, &a->domain_sid.sid)); +} + +static void decode_sam_unk0e_info(SAM_DELTA_UNK0E *a) +{ + fstring temp; + printf("\nTrust information\n"); + printf("-----------------\n"); + + unistr2_to_ascii(temp, &a->domain, sizeof(temp)-1); + printf("\tdomain name :%s\n", temp); + printf("\tsid :%s\n", sid_to_string(temp, &a->sid.sid)); + display_sec_desc(a->sec_desc); +} + +static void decode_sam_privs_info(SAM_DELTA_PRIVS *a) +{ + int i; + fstring temp; + printf("\nSID and privileges information\n"); + printf("------------------------------\n"); + printf("\tsid :%s\n", sid_to_string(temp, &a->sid.sid)); + display_sec_desc(a->sec_desc); + printf("\tprivileges count :%d\n", a->privlist_count); + for (i=0; i<a->privlist_count; i++) { + unistr2_to_ascii(temp, &a->uni_privslist[i], sizeof(temp)-1); + printf("\tprivilege name :%s\n", temp); + printf("\tattribute :%d\n", a->attributes[i]); + } +} + +static void decode_sam_unk12_info(SAM_DELTA_UNK12 *a) +{ + fstring temp; + printf("\nTrusted information\n"); + printf("-------------------\n"); + + unistr2_to_ascii(temp, &a->secret, sizeof(temp)-1); + printf("\tsecret name :%s\n", temp); + display_sec_desc(a->sec_desc); + + printf("\ttime 1 :%s\n", http_timestring(nt_time_to_unix(&a->time1))); + printf("\ttime 2 :%s\n", http_timestring(nt_time_to_unix(&a->time2))); + + display_sec_desc(a->sec_desc2); +} + +static void decode_sam_stamp(SAM_DELTA_STAMP *a) +{ + printf("\nStamp information\n"); + printf("-----------------\n"); + printf("\tsequence number :%d\n", a->seqnum); +} + +static void decode_sam_deltas(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas, SAM_DELTA_CTR *deltas) +{ + int i; + for (i = 0; i < num_deltas; i++) { + switch (hdr_deltas[i].type) { + case SAM_DELTA_DOMAIN_INFO: { + SAM_DOMAIN_INFO *a; + a = &deltas[i].domain_info; + decode_domain_info(a); + break; + } + case SAM_DELTA_GROUP_INFO: { + SAM_GROUP_INFO *a; + a = &deltas[i].group_info; + decode_sam_group_info(a); + break; + } + case SAM_DELTA_ACCOUNT_INFO: { + SAM_ACCOUNT_INFO *a; + a = &deltas[i].account_info; + decode_sam_account_info(a); + break; + } + case SAM_DELTA_GROUP_MEM: { + SAM_GROUP_MEM_INFO *a; + a = &deltas[i].grp_mem_info; + decode_sam_grp_mem_info(a); + break; + } + case SAM_DELTA_ALIAS_INFO: { + SAM_ALIAS_INFO *a; + a = &deltas[i].alias_info; + decode_sam_alias_info(a); + break; + } + case SAM_DELTA_ALIAS_MEM: { + SAM_ALIAS_MEM_INFO *a; + a = &deltas[i].als_mem_info; + decode_sam_als_mem_info(a); + break; + } + case SAM_DELTA_DOM_INFO: { + SAM_DELTA_DOM *a; + a = &deltas[i].dom_info; + decode_sam_dom_info(a); + break; + } + case SAM_DELTA_UNK0E_INFO: { + SAM_DELTA_UNK0E *a; + a = &deltas[i].unk0e_info; + decode_sam_unk0e_info(a); + break; + } + case SAM_DELTA_PRIVS_INFO: { + SAM_DELTA_PRIVS *a; + a = &deltas[i].privs_info; + decode_sam_privs_info(a); + break; + } + case SAM_DELTA_UNK12_INFO: { + SAM_DELTA_UNK12 *a; + a = &deltas[i].unk12_info; + decode_sam_unk12_info(a); + break; + } + case SAM_DELTA_SAM_STAMP: { + SAM_DELTA_STAMP *a; + a = &deltas[i].stamp; + decode_sam_stamp(a); + break; + } + default: + DEBUG(0,("unknown delta type: %d\n", hdr_deltas[i].type)); + break; + } + } +} + +/* Synchronise sam database */ + +static NTSTATUS sam_sync(struct cli_state *cli, unsigned char trust_passwd[16], + BOOL do_smbpasswd_output, BOOL verbose) +{ + TALLOC_CTX *mem_ctx; + SAM_DELTA_HDR *hdr_deltas_0, *hdr_deltas_1, *hdr_deltas_2; + SAM_DELTA_CTR *deltas_0, *deltas_1, *deltas_2; + uint32 num_deltas_0, num_deltas_1, num_deltas_2; + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + + DOM_CRED ret_creds; + /* Initialise */ + + if (!(mem_ctx = talloc_init())) { + DEBUG(0,("talloc_init failed\n")); + return result; + } + + if (!cli_nt_session_open (cli, PIPE_NETLOGON)) { + DEBUG(0, ("Could not initialize netlogon pipe!\n")); + goto done; + } + + /* Request a challenge */ + + if (!NT_STATUS_IS_OK(new_cli_nt_setup_creds(cli, SEC_CHAN_BDC, trust_passwd))) { + DEBUG(0, ("Error initialising session creds\n")); + goto done; + } + + /* on first call the returnAuthenticator is empty */ + memset(&ret_creds, 0, sizeof(ret_creds)); + + /* Do sam synchronisation on the SAM database*/ + + result = cli_netlogon_sam_sync(cli, mem_ctx, &ret_creds, 0, &num_deltas_0, &hdr_deltas_0, &deltas_0); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* verbose mode */ + if (verbose) + decode_sam_deltas(num_deltas_0, hdr_deltas_0, deltas_0); + + + /* + * we can't yet do several sam_sync in a raw, it's a credential problem + * we must chain the credentials + */ + +#if 1 + /* Do sam synchronisation on the LSA database */ + + result = cli_netlogon_sam_sync(cli, mem_ctx, &ret_creds, 2, &num_deltas_2, &hdr_deltas_2, &deltas_2); + + if (!NT_STATUS_IS_OK(result)) + goto done; + + /* verbose mode */ + if (verbose) + decode_sam_deltas(num_deltas_2, hdr_deltas_2, deltas_2); +#endif + + /* Produce smbpasswd output - good for migrating from NT! */ + + if (do_smbpasswd_output) { + int i; + + for (i = 0; i < num_deltas_0; i++) { + SAM_ACCOUNT_INFO *a; + fstring acct_name, hex_nt_passwd, hex_lm_passwd; + uchar lm_passwd[16], nt_passwd[16]; + + /* Skip non-user accounts */ + + if (hdr_deltas_0[i].type != SAM_DELTA_ACCOUNT_INFO) + continue; + + a = &deltas_0[i].account_info; + + unistr2_to_ascii(acct_name, &a->uni_acct_name, + sizeof(acct_name) - 1); + + /* Decode hashes from password hash */ + + sam_pwd_hash(a->user_rid, a->pass.buf_lm_pwd, + lm_passwd, 0); + sam_pwd_hash(a->user_rid, a->pass.buf_nt_pwd, + nt_passwd, 0); + + /* Encode as strings */ + + smbpasswd_sethexpwd(hex_lm_passwd, lm_passwd, + a->acb_info); + smbpasswd_sethexpwd(hex_nt_passwd, nt_passwd, + a->acb_info); + + /* Display user info */ + + printf("%s:%d:%s:%s:%s:LCT-0\n", acct_name, + a->user_rid, hex_lm_passwd, hex_nt_passwd, + smbpasswd_encode_acb_info(a->acb_info)); + } + + goto done; + } + + /* Update sam tdb */ + + done: + cli_nt_session_close(cli); + talloc_destroy(mem_ctx); + + return result; +} + +/* Replicate sam deltas */ + +static NTSTATUS sam_repl(struct cli_state *cli, unsigned char trust_passwde[16], + uint32 low_serial) +{ + NTSTATUS result = NT_STATUS_UNSUCCESSFUL; + + return result; +} + +/* Print usage information */ + +static void usage(void) +{ + printf("Usage: samsync [options]\n"); + + printf("\t-d debuglevel set the debuglevel\n"); + printf("\t-h Print this help message.\n"); + printf("\t-s configfile specify an alternative config file\n"); + printf("\t-S synchronise sam database\n"); + printf("\t-R replicate sam deltas\n"); + printf("\t-U username username and password\n"); + printf("\t-p produce smbpasswd output\n"); + printf("\t-V verbose output\n"); + printf("\n"); +} + +/* Initialise client credentials for authenticated pipe access */ + +void init_rpcclient_creds(struct ntuser_creds *creds, char* username, + char* domain, char* password) +{ + ZERO_STRUCTP(creds); + + if (lp_encrypted_passwords()) { + pwd_make_lm_nt_16(&creds->pwd, password); + } else { + pwd_set_cleartext(&creds->pwd, password); + } + + fstrcpy(creds->user_name, username); + fstrcpy(creds->domain, domain); + + if (! *username) { + creds->pwd.null_pwd = True; + } +} + +/* Connect to primary domain controller */ + +static struct cli_state *init_connection(struct cli_state *cli, + char *username, char *domain, + char *password) +{ + struct ntuser_creds creds; + extern pstring global_myname; + struct in_addr *dest_ip; + struct nmb_name calling, called; + int count; + fstring dest_host; + + /* Initialise cli_state information */ + + ZERO_STRUCTP(cli); + + if (!cli_initialise(cli)) { + return NULL; + } + + init_rpcclient_creds(&creds, username, domain, password); + cli_init_creds(cli, &creds); + + /* Look up name of PDC controller */ + + if (!get_dc_list(True, lp_workgroup(), &dest_ip, &count)) { + DEBUG(0, ("Cannot find domain controller for domain %s\n", + lp_workgroup())); + return NULL; + } + + if (!lookup_dc_name(global_myname, lp_workgroup(), dest_ip, + dest_host)) { + DEBUG(0, ("Could not lookup up PDC name for domain %s\n", + lp_workgroup())); + return NULL; + } + + get_myname((*global_myname)?NULL:global_myname); + strupper(global_myname); + + make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20); + make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0); + + /* Establish a SMB connection */ + + if (!cli_establish_connection(cli, dest_host, dest_ip, &calling, + &called, "IPC$", "IPC", False, True)) { + return NULL; + } + + return cli; +} + +/* Main function */ + + int main(int argc, char **argv) +{ + BOOL do_sam_sync = False, do_sam_repl = False; + struct cli_state cli; + NTSTATUS result; + int opt; + pstring logfile; + BOOL interactive = False, do_smbpasswd_output = False; + BOOL verbose = False; + uint32 low_serial = 0; + unsigned char trust_passwd[16]; + fstring username, domain, password; + + if (argc == 1) { + usage(); + return 1; + } + + ZERO_STRUCT(username); + ZERO_STRUCT(domain); + ZERO_STRUCT(password); + + /* Parse command line options */ + + while((opt = getopt(argc, argv, "s:d:SR:hiU:W:pV")) != EOF) { + switch (opt) { + case 's': + pstrcpy(dyn_CONFIGFILE, optarg); + break; + case 'd': + DEBUGLEVEL = atoi(optarg); + break; + case 'S': + do_sam_sync = 1; + break; + case 'R': + do_sam_repl = 1; + low_serial = atoi(optarg); + break; + case 'i': + interactive = True; + break; + case 'U': { + char *lp; + + fstrcpy(username,optarg); + if ((lp=strchr_m(username,'%'))) { + *lp = 0; + fstrcpy(password,lp+1); + memset(strchr_m(optarg, '%') + 1, 'X', + strlen(password)); + } + break; + } + case 'W': + pstrcpy(domain, optarg); + break; + case 'p': + do_smbpasswd_output = True; + break; + case 'V': + verbose = True; + break; + case 'h': + default: + usage(); + exit(1); + } + } + + argc -= optind; + + if (argc > 0) { + usage(); + return 1; + } + + /* Initialise samba */ + + slprintf(logfile, sizeof(logfile) - 1, "%s/log.%s", dyn_LOGFILEBASE, + "samsync"); + lp_set_logfile(logfile); + + setup_logging("samsync", interactive); + + if (!interactive) + reopen_logs(); + + if (!lp_load(dyn_CONFIGFILE, True, False, False)) { + fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE); + } + + load_interfaces(); + + /* Check arguments make sense */ + + if (do_sam_sync && do_sam_repl) { + fprintf(stderr, "cannot specify both -S and -R\n"); + return 1; + + } + + if (!do_sam_sync && !do_sam_repl) { + fprintf(stderr, "must specify either -S or -R\n"); + return 1; + } + + if (do_sam_repl && low_serial == 0) { + fprintf(stderr, "serial number must be positive\n"); + return 1; + } + + /* BDC operations require the machine account password */ + + if (!secrets_init()) { + DEBUG(0, ("Unable to initialise secrets database\n")); + return 1; + } + + if (!secrets_fetch_trust_account_password(lp_workgroup(), + trust_passwd, NULL)) { + DEBUG(0, ("could not fetch trust account password\n")); + return 1; + } + + /* Perform sync or replication */ + + if (!init_connection(&cli, username, domain, password)) + return 1; + + if (do_sam_sync) + result = sam_sync(&cli, trust_passwd, do_smbpasswd_output, verbose); + + if (do_sam_repl) + result = sam_repl(&cli, trust_passwd, low_serial); + + if (!NT_STATUS_IS_OK(result)) { + DEBUG(0, ("%s\n", nt_errstr(result))); + return 1; + } + + return 0; +} |