From 8de0c24a08cf71058dd58004fe82ca2e8368ffbc Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Wed, 18 Aug 2004 07:56:35 +0000 Subject: r1875: - move 'net' code into a subdir - and remove all unneeded files we'll reimplement the stuff step by step using the functions from libnet/ which will do the hard work for us metze (This used to be commit 8b2d5ec973fde980389bfe03cbcd70274b98b2dc) --- source4/utils/net.c | 642 ----------- source4/utils/net.h | 59 - source4/utils/net/net.c | 642 +++++++++++ source4/utils/net/net.h | 59 + source4/utils/net_ads.c | 1176 -------------------- source4/utils/net_ads_cldap.c | 354 ------ source4/utils/net_cache.c | 348 ------ source4/utils/net_help.c | 199 ---- source4/utils/net_lookup.c | 234 ---- source4/utils/net_rap.c | 1051 ------------------ source4/utils/net_rpc.c | 2262 --------------------------------------- source4/utils/net_rpc_join.c | 354 ------ source4/utils/net_rpc_samsync.c | 725 ------------- source4/utils/net_time.c | 180 ---- 14 files changed, 701 insertions(+), 7584 deletions(-) delete mode 100644 source4/utils/net.c delete mode 100644 source4/utils/net.h create mode 100644 source4/utils/net/net.c create mode 100644 source4/utils/net/net.h delete mode 100644 source4/utils/net_ads.c delete mode 100644 source4/utils/net_ads_cldap.c delete mode 100644 source4/utils/net_cache.c delete mode 100644 source4/utils/net_help.c delete mode 100644 source4/utils/net_lookup.c delete mode 100644 source4/utils/net_rap.c delete mode 100644 source4/utils/net_rpc.c delete mode 100644 source4/utils/net_rpc_join.c delete mode 100644 source4/utils/net_rpc_samsync.c delete mode 100644 source4/utils/net_time.c (limited to 'source4/utils') diff --git a/source4/utils/net.c b/source4/utils/net.c deleted file mode 100644 index 6db62ea6b0..0000000000 --- a/source4/utils/net.c +++ /dev/null @@ -1,642 +0,0 @@ -/* - Samba Unix/Linux SMB client library - Distributed SMB/CIFS Server Management Utility - Copyright (C) 2001 Steve French (sfrench@us.ibm.com) - Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com) - Copyright (C) 2001 Andrew Tridgell (tridge@samba.org) - Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org) - - Originally written by Steve and Jim. Largely rewritten by tridge in - November 2001. - - Reworked again by abartlet in December 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. */ - -/*****************************************************/ -/* */ -/* Distributed SMB/CIFS Server Management Utility */ -/* */ -/* The intent was to make the syntax similar */ -/* to the NET utility (first developed in DOS */ -/* with additional interesting & useful functions */ -/* added in later SMB server network operating */ -/* systems). */ -/* */ -/*****************************************************/ - -#include "includes.h" -#include "../utils/net.h" - -/***********************************************************************/ -/* Beginning of internationalization section. Translatable constants */ -/* should be kept in this area and referenced in the rest of the code. */ -/* */ -/* No functions, outside of Samba or LSB (Linux Standards Base) should */ -/* be used (if possible). */ -/***********************************************************************/ - -#define YES_STRING "Yes" -#define NO_STRING "No" - -/************************************************************************************/ -/* end of internationalization section */ -/************************************************************************************/ - -/* Yes, these buggers are globals.... */ -const char *opt_requester_name = NULL; -const char *opt_host = NULL; -const char *opt_password = NULL; -const char *opt_user_name = NULL; -BOOL opt_user_specified = False; -const char *opt_workgroup = NULL; -int opt_long_list_entries = 0; -int opt_reboot = 0; -int opt_force = 0; -int opt_port = 0; -int opt_maxusers = -1; -const char *opt_comment = ""; -char *opt_container = "cn=Users"; -int opt_flags = -1; -int opt_jobid = 0; -int opt_timeout = 0; -const char *opt_target_workgroup = NULL; -static int opt_machine_pass = 0; - -BOOL opt_have_ip = False; -struct in_addr opt_dest_ip; - -extern BOOL AllowDebugChange; - -/* - run a function from a function table. If not found then - call the specified usage function -*/ -int net_run_function(int argc, const char **argv, struct functable *table, - int (*usage_fn)(int argc, const char **argv)) -{ - int i; - - if (argc < 1) { - d_printf("\nUsage: \n"); - return usage_fn(argc, argv); - } - for (i=0; table[i].funcname; i++) { - if (StrCaseCmp(argv[0], table[i].funcname) == 0) - return table[i].fn(argc-1, argv+1); - } - d_printf("No command: %s\n", argv[0]); - return usage_fn(argc, argv); -} - - -/**************************************************************************** -connect to \\server\ipc$ -****************************************************************************/ -NTSTATUS connect_to_ipc(struct smbcli_state **c, struct in_addr *server_ip, - const char *server_name) -{ - NTSTATUS nt_status; - - if (!opt_password) { - char *pass = getpass("Password:"); - if (pass) { - opt_password = strdup(pass); - } - } - - nt_status = smbcli_full_connection(c, opt_requester_name, server_name, - server_ip, opt_port, - "IPC$", "IPC", - opt_user_name, opt_workgroup, - opt_password, 0, NULL); - - if (NT_STATUS_IS_OK(nt_status)) { - return nt_status; - } else { - DEBUG(1,("Cannot connect to server. Error was %s\n", - nt_errstr(nt_status))); - - /* Display a nicer message depending on the result */ - - if (NT_STATUS_V(nt_status) == - NT_STATUS_V(NT_STATUS_LOGON_FAILURE)) - d_printf("The username or password was not correct.\n"); - - return nt_status; - } -} - -/**************************************************************************** -connect to \\server\ipc$ anonymously -****************************************************************************/ -NTSTATUS connect_to_ipc_anonymous(struct smbcli_state **c, - struct in_addr *server_ip, const char *server_name) -{ - NTSTATUS nt_status; - - nt_status = smbcli_full_connection(c, opt_requester_name, server_name, - server_ip, opt_port, - "IPC$", "IPC", - "", "", - "", 0, NULL); - - if (NT_STATUS_IS_OK(nt_status)) { - return nt_status; - } else { - DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status))); - return nt_status; - } -} - -BOOL net_find_server(uint_t flags, struct in_addr *server_ip, char **server_name) -{ - - if (opt_host) { - *server_name = strdup(opt_host); - } - - if (opt_have_ip) { - *server_ip = opt_dest_ip; - if (!*server_name) { - *server_name = strdup(inet_ntoa(opt_dest_ip)); - } - } else if (*server_name) { - /* resolve the IP address */ - if (!resolve_name(*server_name, server_ip, 0x20)) { - DEBUG(1,("Unable to resolve server name\n")); - return False; - } - } else if (flags & NET_FLAGS_PDC) { - struct in_addr pdc_ip; - - if (get_pdc_ip(opt_target_workgroup, &pdc_ip)) { - fstring dc_name; - - if (is_zero_ip(pdc_ip)) - return False; - - if (!lookup_dc_name(lp_netbios_name(), opt_target_workgroup, &pdc_ip, dc_name)) - return False; - - *server_name = strdup(dc_name); - *server_ip = pdc_ip; - } - - } else if (flags & NET_FLAGS_DMB) { - struct in_addr msbrow_ip; - /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */ - if (!resolve_name(opt_target_workgroup, &msbrow_ip, 0x1B)) { - DEBUG(1,("Unable to resolve domain browser via name lookup\n")); - return False; - } else { - *server_ip = msbrow_ip; - } - *server_name = strdup(inet_ntoa(opt_dest_ip)); - } else if (flags & NET_FLAGS_MASTER) { - struct in_addr brow_ips; - if (!resolve_name(opt_target_workgroup, &brow_ips, 0x1D)) { - /* go looking for workgroups */ - DEBUG(1,("Unable to resolve master browser via name lookup\n")); - return False; - } else { - *server_ip = brow_ips; - } - *server_name = strdup(inet_ntoa(opt_dest_ip)); - } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) { - extern struct in_addr loopback_ip; - *server_ip = loopback_ip; - *server_name = strdup("127.0.0.1"); - } - - if (!server_name || !*server_name) { - DEBUG(1,("no server to connect to\n")); - return False; - } - - return True; -} - - -BOOL net_find_dc(struct in_addr *server_ip, fstring server_name, const char *domain_name) -{ - if (get_pdc_ip(domain_name, server_ip)) { - fstring dc_name; - - if (is_zero_ip(*server_ip)) - return False; - - if (!lookup_dc_name(lp_netbios_name(), domain_name, server_ip, dc_name)) - return False; - - safe_strcpy(server_name, dc_name, FSTRING_LEN); - return True; - } else - return False; -} - - -struct smbcli_state *net_make_ipc_connection(uint_t flags) -{ - char *server_name = NULL; - struct in_addr server_ip; - struct smbcli_state *cli = NULL; - NTSTATUS nt_status; - - if (!net_find_server(flags, &server_ip, &server_name)) { - d_printf("\nUnable to find a suitable server\n"); - return NULL; - } - - if (flags & NET_FLAGS_ANONYMOUS) { - nt_status = connect_to_ipc_anonymous(&cli, &server_ip, server_name); - } else { - nt_status = connect_to_ipc(&cli, &server_ip, server_name); - } - - SAFE_FREE(server_name); - if (NT_STATUS_IS_OK(nt_status)) { - return cli; - } else { - return NULL; - } -} - -static int net_user(int argc, const char **argv) -{ - if (net_ads_check() == 0) - return net_ads_user(argc, argv); - - /* if server is not specified, default to PDC? */ - if (net_rpc_check(NET_FLAGS_PDC)) - return net_rpc_user(argc, argv); - - return net_rap_user(argc, argv); -} - -static int net_group(int argc, const char **argv) -{ - if (net_ads_check() == 0) - return net_ads_group(argc, argv); - - if (argc == 0 && net_rpc_check(NET_FLAGS_PDC)) - return net_rpc_group(argc, argv); - - return net_rap_group(argc, argv); -} - -static int net_join(int argc, const char **argv) -{ - if (net_ads_check() == 0) { - if (net_ads_join(argc, argv) == 0) - return 0; - else - d_printf("ADS join did not work, trying RPC...\n"); - } - return net_rpc_join(argc, argv); -} - -static int net_share(int argc, const char **argv) -{ - if (net_rpc_check(0)) - return net_rpc_share(argc, argv); - return net_rap_share(argc, argv); -} - -static int net_file(int argc, const char **argv) -{ - if (net_rpc_check(0)) - return net_rpc_file(argc, argv); - return net_rap_file(argc, argv); -} - -/* - Retrieve our local SID or the SID for the specified name - */ -static int net_getlocalsid(int argc, const char **argv) -{ - DOM_SID sid; - const char *name; - fstring sid_str; - - if (argc >= 1) { - name = argv[0]; - } - else { - name = lp_netbios_name(); - } - - if (!secrets_fetch_domain_sid(name, &sid)) { - DEBUG(0, ("Can't fetch domain SID for name: %s\n", name)); - return 1; - } - sid_to_string(sid_str, &sid); - d_printf("SID for domain %s is: %s\n", name, sid_str); - return 0; -} - -static int net_setlocalsid(int argc, const char **argv) -{ - DOM_SID sid; - - if ( (argc != 1) - || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0) - || (!string_to_sid(&sid, argv[0])) - || (sid.num_auths != 4)) { - d_printf("usage: net setlocalsid S-1-5-21-x-y-z\n"); - return 1; - } - - if (!secrets_store_domain_sid(lp_netbios_name(), &sid)) { - DEBUG(0,("Can't store domain SID as a pdc/bdc.\n")); - return 1; - } - - return 0; -} - -static int net_getdomainsid(int argc, const char **argv) -{ - DOM_SID domain_sid; - fstring sid_str; - - if (!secrets_fetch_domain_sid(lp_netbios_name(), &domain_sid)) { - d_printf("Could not fetch local SID\n"); - return 1; - } - sid_to_string(sid_str, &domain_sid); - d_printf("SID for domain %s is: %s\n", lp_netbios_name(), sid_str); - - if (!secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) { - d_printf("Could not fetch domain SID\n"); - return 1; - } - - sid_to_string(sid_str, &domain_sid); - d_printf("SID for domain %s is: %s\n", lp_workgroup(), sid_str); - - return 0; -} - -static uint32_t get_maxrid(void) -{ - SAM_ACCOUNT *pwd = NULL; - uint32_t max_rid = 0; - GROUP_MAP *map = NULL; - int num_entries = 0; - int i; - - if (!pdb_setsampwent(False)) { - DEBUG(0, ("load_sampwd_entries: Unable to open passdb.\n")); - return 0; - } - - for (; (NT_STATUS_IS_OK(pdb_init_sam(&pwd))) - && pdb_getsampwent(pwd) == True; pwd=NULL) { - uint32_t rid; - - if (!sid_peek_rid(pdb_get_user_sid(pwd), &rid)) { - DEBUG(0, ("can't get RID for user '%s'\n", - pdb_get_username(pwd))); - pdb_free_sam(&pwd); - continue; - } - - if (rid > max_rid) - max_rid = rid; - - DEBUG(1,("%d is user '%s'\n", rid, pdb_get_username(pwd))); - pdb_free_sam(&pwd); - } - - pdb_endsampwent(); - pdb_free_sam(&pwd); - - if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, - ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV)) - return max_rid; - - for (i = 0; i < num_entries; i++) { - uint32_t rid; - - if (!sid_peek_check_rid(get_global_sam_sid(), &map[i].sid, - &rid)) { - DEBUG(3, ("skipping map for group '%s', SID %s\n", - map[i].nt_name, - sid_string_static(&map[i].sid))); - continue; - } - DEBUG(1,("%d is group '%s'\n", rid, map[i].nt_name)); - - if (rid > max_rid) - max_rid = rid; - } - - SAFE_FREE(map); - - return max_rid; -} - -static int net_maxrid(int argc, const char **argv) -{ - uint32_t rid; - - if (argc != 0) { - DEBUG(0, ("usage: net initrid\n")); - return 1; - } - - if ((rid = get_maxrid()) == 0) { - DEBUG(0, ("can't get current maximum rid\n")); - return 1; - } - - d_printf("Currently used maximum rid: %d\n", rid); - - return 0; -} - -/* main function table */ -static struct functable net_func[] = { - {"RPC", net_rpc}, - {"RAP", net_rap}, - {"ADS", net_ads}, - - /* eventually these should auto-choose the transport ... */ - {"FILE", net_file}, - {"SHARE", net_share}, - {"SESSION", net_rap_session}, - {"SERVER", net_rap_server}, - {"DOMAIN", net_rap_domain}, - {"PRINTQ", net_rap_printq}, - {"USER", net_user}, - {"GROUP", net_group}, - {"VALIDATE", net_rap_validate}, - {"GROUPMEMBER", net_rap_groupmember}, - {"ADMIN", net_rap_admin}, - {"SERVICE", net_rap_service}, - {"PASSWORD", net_rap_password}, - {"TIME", net_time}, - {"LOOKUP", net_lookup}, - {"JOIN", net_join}, - {"CACHE", net_cache}, - {"GETLOCALSID", net_getlocalsid}, - {"SETLOCALSID", net_setlocalsid}, - {"GETDOMAINSID", net_getdomainsid}, - {"MAXRID", net_maxrid}, - - {"HELP", net_help}, - {NULL, NULL} -}; - - -/**************************************************************************** - main program -****************************************************************************/ - int main(int argc, const char **argv) -{ - int opt,i; - char *p; - int rc = 0; - int argc_new = 0; - const char ** argv_new; - poptContext pc; - static char *servicesf = dyn_CONFIGFILE; - static char *debuglevel = NULL; - - struct poptOption long_options[] = { - {"help", 'h', POPT_ARG_NONE, 0, 'h'}, - {"workgroup", 'w', POPT_ARG_STRING, &opt_target_workgroup}, - {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup}, - {"user", 'U', POPT_ARG_STRING, &opt_user_name, 'U'}, - {"ipaddress", 'I', POPT_ARG_STRING, 0,'I'}, - {"port", 'p', POPT_ARG_INT, &opt_port}, - {"myname", 'n', POPT_ARG_STRING, &opt_requester_name}, - {"conf", 's', POPT_ARG_STRING, &servicesf}, - {"server", 'S', POPT_ARG_STRING, &opt_host}, - {"container", 'c', POPT_ARG_STRING, &opt_container}, - {"comment", 'C', POPT_ARG_STRING, &opt_comment}, - {"maxusers", 'M', POPT_ARG_INT, &opt_maxusers}, - {"flags", 'F', POPT_ARG_INT, &opt_flags}, - {"jobid", 'j', POPT_ARG_INT, &opt_jobid}, - {"long", 'l', POPT_ARG_NONE, &opt_long_list_entries}, - {"reboot", 'r', POPT_ARG_NONE, &opt_reboot}, - {"force", 'f', POPT_ARG_NONE, &opt_force}, - {"timeout", 't', POPT_ARG_INT, &opt_timeout}, - {"machine-pass",'P', POPT_ARG_NONE, &opt_machine_pass}, - {"debuglevel", 'd', POPT_ARG_STRING, &debuglevel}, - {NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version}, - { 0, 0, 0, 0} - }; - - zero_ip(&opt_dest_ip); - - dbf = x_stderr; - - pc = poptGetContext(NULL, argc, (const char **) argv, long_options, - POPT_CONTEXT_KEEP_FIRST); - - while((opt = poptGetNextOpt(pc)) != -1) { - switch (opt) { - case 'h': - net_help(argc, argv); - exit(0); - break; - case 'I': - opt_dest_ip = *interpret_addr2(poptGetOptArg(pc)); - if (is_zero_ip(opt_dest_ip)) - d_printf("\nInvalid ip address specified\n"); - else - opt_have_ip = True; - break; - case 'U': - opt_user_specified = True; - opt_user_name = strdup(opt_user_name); - p = strchr(opt_user_name,'%'); - if (p) { - *p = 0; - opt_password = p+1; - } - break; - default: - d_printf("\nInvalid option %s: %s\n", - poptBadOption(pc, 0), poptStrerror(opt)); - net_help(argc, argv); - exit(1); - } - } - - if (debuglevel) { - debug_parse_levels(debuglevel); - AllowDebugChange = False; - } - - lp_load(servicesf,True,False,False); - - argv_new = (const char **)poptGetArgs(pc); - - argc_new = argc; - for (i=0; i= 1) { + name = argv[0]; + } + else { + name = lp_netbios_name(); + } + + if (!secrets_fetch_domain_sid(name, &sid)) { + DEBUG(0, ("Can't fetch domain SID for name: %s\n", name)); + return 1; + } + sid_to_string(sid_str, &sid); + d_printf("SID for domain %s is: %s\n", name, sid_str); + return 0; +} + +static int net_setlocalsid(int argc, const char **argv) +{ + DOM_SID sid; + + if ( (argc != 1) + || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0) + || (!string_to_sid(&sid, argv[0])) + || (sid.num_auths != 4)) { + d_printf("usage: net setlocalsid S-1-5-21-x-y-z\n"); + return 1; + } + + if (!secrets_store_domain_sid(lp_netbios_name(), &sid)) { + DEBUG(0,("Can't store domain SID as a pdc/bdc.\n")); + return 1; + } + + return 0; +} + +static int net_getdomainsid(int argc, const char **argv) +{ + DOM_SID domain_sid; + fstring sid_str; + + if (!secrets_fetch_domain_sid(lp_netbios_name(), &domain_sid)) { + d_printf("Could not fetch local SID\n"); + return 1; + } + sid_to_string(sid_str, &domain_sid); + d_printf("SID for domain %s is: %s\n", lp_netbios_name(), sid_str); + + if (!secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) { + d_printf("Could not fetch domain SID\n"); + return 1; + } + + sid_to_string(sid_str, &domain_sid); + d_printf("SID for domain %s is: %s\n", lp_workgroup(), sid_str); + + return 0; +} + +static uint32_t get_maxrid(void) +{ + SAM_ACCOUNT *pwd = NULL; + uint32_t max_rid = 0; + GROUP_MAP *map = NULL; + int num_entries = 0; + int i; + + if (!pdb_setsampwent(False)) { + DEBUG(0, ("load_sampwd_entries: Unable to open passdb.\n")); + return 0; + } + + for (; (NT_STATUS_IS_OK(pdb_init_sam(&pwd))) + && pdb_getsampwent(pwd) == True; pwd=NULL) { + uint32_t rid; + + if (!sid_peek_rid(pdb_get_user_sid(pwd), &rid)) { + DEBUG(0, ("can't get RID for user '%s'\n", + pdb_get_username(pwd))); + pdb_free_sam(&pwd); + continue; + } + + if (rid > max_rid) + max_rid = rid; + + DEBUG(1,("%d is user '%s'\n", rid, pdb_get_username(pwd))); + pdb_free_sam(&pwd); + } + + pdb_endsampwent(); + pdb_free_sam(&pwd); + + if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries, + ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV)) + return max_rid; + + for (i = 0; i < num_entries; i++) { + uint32_t rid; + + if (!sid_peek_check_rid(get_global_sam_sid(), &map[i].sid, + &rid)) { + DEBUG(3, ("skipping map for group '%s', SID %s\n", + map[i].nt_name, + sid_string_static(&map[i].sid))); + continue; + } + DEBUG(1,("%d is group '%s'\n", rid, map[i].nt_name)); + + if (rid > max_rid) + max_rid = rid; + } + + SAFE_FREE(map); + + return max_rid; +} + +static int net_maxrid(int argc, const char **argv) +{ + uint32_t rid; + + if (argc != 0) { + DEBUG(0, ("usage: net initrid\n")); + return 1; + } + + if ((rid = get_maxrid()) == 0) { + DEBUG(0, ("can't get current maximum rid\n")); + return 1; + } + + d_printf("Currently used maximum rid: %d\n", rid); + + return 0; +} + +/* main function table */ +static struct functable net_func[] = { + {"RPC", net_rpc}, + {"RAP", net_rap}, + {"ADS", net_ads}, + + /* eventually these should auto-choose the transport ... */ + {"FILE", net_file}, + {"SHARE", net_share}, + {"SESSION", net_rap_session}, + {"SERVER", net_rap_server}, + {"DOMAIN", net_rap_domain}, + {"PRINTQ", net_rap_printq}, + {"USER", net_user}, + {"GROUP", net_group}, + {"VALIDATE", net_rap_validate}, + {"GROUPMEMBER", net_rap_groupmember}, + {"ADMIN", net_rap_admin}, + {"SERVICE", net_rap_service}, + {"PASSWORD", net_rap_password}, + {"TIME", net_time}, + {"LOOKUP", net_lookup}, + {"JOIN", net_join}, + {"CACHE", net_cache}, + {"GETLOCALSID", net_getlocalsid}, + {"SETLOCALSID", net_setlocalsid}, + {"GETDOMAINSID", net_getdomainsid}, + {"MAXRID", net_maxrid}, + + {"HELP", net_help}, + {NULL, NULL} +}; + + +/**************************************************************************** + main program +****************************************************************************/ + int main(int argc, const char **argv) +{ + int opt,i; + char *p; + int rc = 0; + int argc_new = 0; + const char ** argv_new; + poptContext pc; + static char *servicesf = dyn_CONFIGFILE; + static char *debuglevel = NULL; + + struct poptOption long_options[] = { + {"help", 'h', POPT_ARG_NONE, 0, 'h'}, + {"workgroup", 'w', POPT_ARG_STRING, &opt_target_workgroup}, + {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup}, + {"user", 'U', POPT_ARG_STRING, &opt_user_name, 'U'}, + {"ipaddress", 'I', POPT_ARG_STRING, 0,'I'}, + {"port", 'p', POPT_ARG_INT, &opt_port}, + {"myname", 'n', POPT_ARG_STRING, &opt_requester_name}, + {"conf", 's', POPT_ARG_STRING, &servicesf}, + {"server", 'S', POPT_ARG_STRING, &opt_host}, + {"container", 'c', POPT_ARG_STRING, &opt_container}, + {"comment", 'C', POPT_ARG_STRING, &opt_comment}, + {"maxusers", 'M', POPT_ARG_INT, &opt_maxusers}, + {"flags", 'F', POPT_ARG_INT, &opt_flags}, + {"jobid", 'j', POPT_ARG_INT, &opt_jobid}, + {"long", 'l', POPT_ARG_NONE, &opt_long_list_entries}, + {"reboot", 'r', POPT_ARG_NONE, &opt_reboot}, + {"force", 'f', POPT_ARG_NONE, &opt_force}, + {"timeout", 't', POPT_ARG_INT, &opt_timeout}, + {"machine-pass",'P', POPT_ARG_NONE, &opt_machine_pass}, + {"debuglevel", 'd', POPT_ARG_STRING, &debuglevel}, + {NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version}, + { 0, 0, 0, 0} + }; + + zero_ip(&opt_dest_ip); + + dbf = x_stderr; + + pc = poptGetContext(NULL, argc, (const char **) argv, long_options, + POPT_CONTEXT_KEEP_FIRST); + + while((opt = poptGetNextOpt(pc)) != -1) { + switch (opt) { + case 'h': + net_help(argc, argv); + exit(0); + break; + case 'I': + opt_dest_ip = *interpret_addr2(poptGetOptArg(pc)); + if (is_zero_ip(opt_dest_ip)) + d_printf("\nInvalid ip address specified\n"); + else + opt_have_ip = True; + break; + case 'U': + opt_user_specified = True; + opt_user_name = strdup(opt_user_name); + p = strchr(opt_user_name,'%'); + if (p) { + *p = 0; + opt_password = p+1; + } + break; + default: + d_printf("\nInvalid option %s: %s\n", + poptBadOption(pc, 0), poptStrerror(opt)); + net_help(argc, argv); + exit(1); + } + } + + if (debuglevel) { + debug_parse_levels(debuglevel); + AllowDebugChange = False; + } + + lp_load(servicesf,True,False,False); + + argv_new = (const char **)poptGetArgs(pc); + + argc_new = argc; + for (i=0; i"\ -"\n\tjoins the local machine to a ADS realm\n"\ -"\nnet ads leave"\ -"\n\tremoves the local machine from a ADS realm\n"\ -"\nnet ads testjoin"\ -"\n\ttests that an exiting join is OK\n"\ -"\nnet ads user"\ -"\n\tlist, add, or delete users in the realm\n"\ -"\nnet ads group"\ -"\n\tlist, add, or delete groups in the realm\n"\ -"\nnet ads info"\ -"\n\tshows some info on the server\n"\ -"\nnet ads status"\ -"\n\tdump the machine account details to stdout\n" -"\nnet ads lookup"\ -"\n\tperform a CLDAP search on the server\n" -"\nnet ads password -Uadmin_username@realm%%admin_pass"\ -"\n\tchange a user's password using an admin account"\ -"\n\t(note: use realm in UPPERCASE)\n"\ -"\nnet ads chostpass"\ -"\n\tchange the trust account password of this machine in the AD tree\n"\ -"\nnet ads printer [info | publish | remove] "\ -"\n\t lookup, add, or remove directory entry for a printer\n"\ -"\nnet ads search"\ -"\n\tperform a raw LDAP search and dump the results\n" -"\nnet ads dn"\ -"\n\tperform a raw LDAP search and dump attributes of a particular DN\n" - ); - return -1; -} - - -/* - this implements the CLDAP based netlogon lookup requests - for finding the domain controller of a ADS domain -*/ -static int net_ads_lookup(int argc, const char **argv) -{ - ADS_STRUCT *ads; - - ads = ads_init(NULL, NULL, opt_host); - if (ads) { - ads->auth.flags |= ADS_AUTH_NO_BIND; - } - - ads_connect(ads); - - if (!ads || !ads->config.realm) { - d_printf("Didn't find the cldap server!\n"); - return -1; - } - - return ads_cldap_netlogon(ads); -} - - - -static int net_ads_info(int argc, const char **argv) -{ - ADS_STRUCT *ads; - - ads = ads_init(NULL, NULL, opt_host); - - if (ads) { - ads->auth.flags |= ADS_AUTH_NO_BIND; - } - - ads_connect(ads); - - if (!ads || !ads->config.realm) { - d_printf("Didn't find the ldap server!\n"); - return -1; - } - - d_printf("LDAP server: %s\n", inet_ntoa(ads->ldap_ip)); - d_printf("LDAP server name: %s\n", ads->config.ldap_server_name); - d_printf("Realm: %s\n", ads->config.realm); - d_printf("Bind Path: %s\n", ads->config.bind_path); - d_printf("LDAP port: %d\n", ads->ldap_port); - d_printf("Server time: %s\n", timestring(ads->config.current_time)); - - return 0; -} - -static void use_in_memory_ccache(void) { - /* Use in-memory credentials cache so we do not interfere with - * existing credentials */ - setenv(KRB5_ENV_CCNAME, "MEMORY:net_ads", 1); -} - -static ADS_STRUCT *ads_startup(void) -{ - ADS_STRUCT *ads; - ADS_STATUS status; - BOOL need_password = False; - BOOL second_time = False; - - ads = ads_init(NULL, NULL, opt_host); - - if (!opt_user_name) { - opt_user_name = "administrator"; - } - - if (opt_user_specified) { - need_password = True; - } - -retry: - if (!opt_password && need_password) { - char *prompt; - asprintf(&prompt,"%s password: ", opt_user_name); - opt_password = getpass(prompt); - free(prompt); - } - - if (opt_password) { - use_in_memory_ccache(); - ads->auth.password = strdup(opt_password); - } - - ads->auth.user_name = strdup(opt_user_name); - - status = ads_connect(ads); - if (!ADS_ERR_OK(status)) { - if (!need_password && !second_time) { - need_password = True; - second_time = True; - goto retry; - } else { - DEBUG(1,("ads_connect: %s\n", ads_errstr(status))); - return NULL; - } - } - return ads; -} - - -/* - Check to see if connection can be made via ads. - ads_startup() stores the password in opt_password if it needs to so - that rpc or rap can use it without re-prompting. -*/ -int net_ads_check(void) -{ - ADS_STRUCT *ads; - - ads = ads_startup(); - if (!ads) - return -1; - ads_destroy(&ads); - return 0; -} - -/* - determine the netbios workgroup name for a domain - */ -static int net_ads_workgroup(int argc, const char **argv) -{ - ADS_STRUCT *ads; - TALLOC_CTX *ctx; - char *workgroup; - - if (!(ads = ads_startup())) return -1; - - if (!(ctx = talloc_init("net_ads_workgroup"))) { - return -1; - } - - if (!ADS_ERR_OK(ads_workgroup_name(ads, ctx, &workgroup))) { - d_printf("Failed to find workgroup for realm '%s'\n", - ads->config.realm); - talloc_destroy(ctx); - return -1; - } - - d_printf("Workgroup: %s\n", workgroup); - - talloc_destroy(ctx); - - return 0; -} - - - -static BOOL usergrp_display(char *field, void **values, void *data_area) -{ - char **disp_fields = (char **) data_area; - - if (!field) { /* must be end of record */ - if (!strchr_m(disp_fields[0], '$')) { - if (disp_fields[1]) - d_printf("%-21.21s %-50.50s\n", - disp_fields[0], disp_fields[1]); - else - d_printf("%s\n", disp_fields[0]); - } - SAFE_FREE(disp_fields[0]); - SAFE_FREE(disp_fields[1]); - return True; - } - if (!values) /* must be new field, indicate string field */ - return True; - if (StrCaseCmp(field, "sAMAccountName") == 0) { - disp_fields[0] = strdup((char *) values[0]); - } - if (StrCaseCmp(field, "description") == 0) - disp_fields[1] = strdup((char *) values[0]); - return True; -} - -static int net_ads_user_usage(int argc, const char **argv) -{ - return net_help_user(argc, argv); -} - -static int ads_user_add(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS status; - char *upn, *userdn; - void *res=NULL; - int rc = -1; - - if (argc < 1) return net_ads_user_usage(argc, argv); - - if (!(ads = ads_startup())) return -1; - - status = ads_find_user_acct(ads, &res, argv[0]); - - if (!ADS_ERR_OK(status)) { - d_printf("ads_user_add: %s\n", ads_errstr(status)); - goto done; - } - - if (ads_count_replies(ads, res)) { - d_printf("ads_user_add: User %s already exists\n", argv[0]); - goto done; - } - - status = ads_add_user_acct(ads, argv[0], opt_container, opt_comment); - - if (!ADS_ERR_OK(status)) { - d_printf("Could not add user %s: %s\n", argv[0], - ads_errstr(status)); - goto done; - } - - /* if no password is to be set, we're done */ - if (argc == 1) { - d_printf("User %s added\n", argv[0]); - rc = 0; - goto done; - } - - /* try setting the password */ - asprintf(&upn, "%s@%s", argv[0], ads->config.realm); - status = krb5_set_password(ads->auth.kdc_server, upn, argv[1], ads->auth.time_offset); - safe_free(upn); - if (ADS_ERR_OK(status)) { - d_printf("User %s added\n", argv[0]); - rc = 0; - goto done; - } - - /* password didn't set, delete account */ - d_printf("Could not add user %s. Error setting password %s\n", - argv[0], ads_errstr(status)); - ads_msgfree(ads, res); - status=ads_find_user_acct(ads, &res, argv[0]); - if (ADS_ERR_OK(status)) { - userdn = ads_get_dn(ads, res); - ads_del_dn(ads, userdn); - ads_memfree(ads, userdn); - } - - done: - if (res) - ads_msgfree(ads, res); - ads_destroy(&ads); - return rc; -} - -static int ads_user_info(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS rc; - void *res; - const char *attrs[] = {"memberOf", NULL}; - char *searchstring=NULL; - char **grouplist; - char *escaped_user = escape_ldap_string_alloc(argv[0]); - - if (argc < 1) return net_ads_user_usage(argc, argv); - - if (!(ads = ads_startup())) return -1; - - if (!escaped_user) { - d_printf("ads_user_info: failed to escape user %s\n", argv[0]); - return -1; - } - - asprintf(&searchstring, "(sAMAccountName=%s)", escaped_user); - rc = ads_search(ads, &res, searchstring, attrs); - safe_free(searchstring); - - if (!ADS_ERR_OK(rc)) { - d_printf("ads_search: %s\n", ads_errstr(rc)); - return -1; - } - - grouplist = ldap_get_values(ads->ld, res, "memberOf"); - - if (grouplist) { - int i; - char **groupname; - for (i=0;grouplist[i];i++) { - groupname = ldap_explode_dn(grouplist[i], 1); - d_printf("%s\n", groupname[0]); - ldap_value_free(groupname); - } - ldap_value_free(grouplist); - } - - ads_msgfree(ads, res); - - ads_destroy(&ads); - return 0; -} - -static int ads_user_delete(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS rc; - void *res; - char *userdn; - - if (argc < 1) return net_ads_user_usage(argc, argv); - - if (!(ads = ads_startup())) return -1; - - rc = ads_find_user_acct(ads, &res, argv[0]); - if (!ADS_ERR_OK(rc)) { - DEBUG(0, ("User %s does not exist\n", argv[0])); - return -1; - } - userdn = ads_get_dn(ads, res); - ads_msgfree(ads, res); - rc = ads_del_dn(ads, userdn); - ads_memfree(ads, userdn); - if (!ADS_ERR_OK(rc)) { - d_printf("User %s deleted\n", argv[0]); - return 0; - } - d_printf("Error deleting user %s: %s\n", argv[0], - ads_errstr(rc)); - return -1; -} - -int net_ads_user(int argc, const char **argv) -{ - struct functable func[] = { - {"ADD", ads_user_add}, - {"INFO", ads_user_info}, - {"DELETE", ads_user_delete}, - {NULL, NULL} - }; - ADS_STRUCT *ads; - ADS_STATUS rc; - const char *shortattrs[] = {"sAMAccountName", NULL}; - const char *longattrs[] = {"sAMAccountName", "description", NULL}; - char *disp_fields[2] = {NULL, NULL}; - - if (argc == 0) { - if (!(ads = ads_startup())) return -1; - - if (opt_long_list_entries) - d_printf("\nUser name Comment"\ - "\n-----------------------------\n"); - - rc = ads_do_search_all_fn(ads, ads->config.bind_path, - LDAP_SCOPE_SUBTREE, - "(objectclass=user)", - opt_long_list_entries ? longattrs : - shortattrs, usergrp_display, - disp_fields); - ads_destroy(&ads); - return 0; - } - - return net_run_function(argc, argv, func, net_ads_user_usage); -} - -static int net_ads_group_usage(int argc, const char **argv) -{ - return net_help_group(argc, argv); -} - -static int ads_group_add(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS status; - void *res=NULL; - int rc = -1; - - if (argc < 1) return net_ads_group_usage(argc, argv); - - if (!(ads = ads_startup())) return -1; - - status = ads_find_user_acct(ads, &res, argv[0]); - - if (!ADS_ERR_OK(status)) { - d_printf("ads_group_add: %s\n", ads_errstr(status)); - goto done; - } - - if (ads_count_replies(ads, res)) { - d_printf("ads_group_add: Group %s already exists\n", argv[0]); - ads_msgfree(ads, res); - goto done; - } - - status = ads_add_group_acct(ads, argv[0], opt_container, opt_comment); - - if (ADS_ERR_OK(status)) { - d_printf("Group %s added\n", argv[0]); - rc = 0; - } else { - d_printf("Could not add group %s: %s\n", argv[0], - ads_errstr(status)); - } - - done: - if (res) - ads_msgfree(ads, res); - ads_destroy(&ads); - return rc; -} - -static int ads_group_delete(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS rc; - void *res; - char *groupdn; - - if (argc < 1) return net_ads_group_usage(argc, argv); - - if (!(ads = ads_startup())) return -1; - - rc = ads_find_user_acct(ads, &res, argv[0]); - if (!ADS_ERR_OK(rc)) { - DEBUG(0, ("Group %s does not exist\n", argv[0])); - return -1; - } - groupdn = ads_get_dn(ads, res); - ads_msgfree(ads, res); - rc = ads_del_dn(ads, groupdn); - ads_memfree(ads, groupdn); - if (!ADS_ERR_OK(rc)) { - d_printf("Group %s deleted\n", argv[0]); - return 0; - } - d_printf("Error deleting group %s: %s\n", argv[0], - ads_errstr(rc)); - return -1; -} - -int net_ads_group(int argc, const char **argv) -{ - struct functable func[] = { - {"ADD", ads_group_add}, - {"DELETE", ads_group_delete}, - {NULL, NULL} - }; - ADS_STRUCT *ads; - ADS_STATUS rc; - const char *shortattrs[] = {"sAMAccountName", NULL}; - const char *longattrs[] = {"sAMAccountName", "description", NULL}; - char *disp_fields[2] = {NULL, NULL}; - - if (argc == 0) { - if (!(ads = ads_startup())) return -1; - - if (opt_long_list_entries) - d_printf("\nGroup name Comment"\ - "\n-----------------------------\n"); - rc = ads_do_search_all_fn(ads, ads->config.bind_path, - LDAP_SCOPE_SUBTREE, - "(objectclass=group)", - opt_long_list_entries ? longattrs : - shortattrs, usergrp_display, - disp_fields); - - ads_destroy(&ads); - return 0; - } - return net_run_function(argc, argv, func, net_ads_group_usage); -} - -static int net_ads_status(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS rc; - void *res; - - if (!(ads = ads_startup())) return -1; - - rc = ads_find_machine_acct(ads, &res, lp_netbios_name()); - if (!ADS_ERR_OK(rc)) { - d_printf("ads_find_machine_acct: %s\n", ads_errstr(rc)); - return -1; - } - - if (ads_count_replies(ads, res) == 0) { - d_printf("No machine account for '%s' found\n", lp_netbios_name()); - return -1; - } - - ads_dump(ads, res); - - return 0; -} - -static int net_ads_leave(int argc, const char **argv) -{ - ADS_STRUCT *ads = NULL; - ADS_STATUS rc; - - if (!secrets_init()) { - DEBUG(1,("Failed to initialise secrets database\n")); - return -1; - } - - if (!opt_password) { - char *user_name; - asprintf(&user_name, "%s$", lp_netbios_name()); - opt_password = secrets_fetch_machine_password(); - opt_user_name = user_name; - } - - if (!(ads = ads_startup())) { - return -1; - } - - rc = ads_leave_realm(ads, lp_netbios_name()); - if (!ADS_ERR_OK(rc)) { - d_printf("Failed to delete host '%s' from the '%s' realm.\n", - lp_netbios_name(), ads->config.realm); - return -1; - } - - d_printf("Removed '%s' from realm '%s'\n", lp_netbios_name(), ads->config.realm); - - return 0; -} - -static int net_ads_join_ok(void) -{ - char *user_name; - ADS_STRUCT *ads = NULL; - - if (!secrets_init()) { - DEBUG(1,("Failed to initialise secrets database\n")); - return -1; - } - - asprintf(&user_name, "%s$", lp_netbios_name()); - opt_user_name = user_name; - opt_password = secrets_fetch_machine_password(); - - if (!(ads = ads_startup())) { - return -1; - } - - ads_destroy(&ads); - return 0; -} - -/* - check that an existing join is OK - */ -int net_ads_testjoin(int argc, const char **argv) -{ - use_in_memory_ccache(); - - /* Display success or failure */ - if (net_ads_join_ok() != 0) { - fprintf(stderr,"Join to domain is not valid\n"); - return -1; - } - - printf("Join is OK\n"); - return 0; -} - -/* - join a domain using ADS - */ -int net_ads_join(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS rc; - char *password; - char *tmp_password; - const char *org_unit = "Computers"; - char *dn; - void *res; - DOM_SID dom_sid; - char *ou_str; - - if (argc > 0) org_unit = argv[0]; - - if (!secrets_init()) { - DEBUG(1,("Failed to initialise secrets database\n")); - return -1; - } - - tmp_password = generate_random_str(DEFAULT_TRUST_ACCOUNT_PASSWORD_LENGTH); - password = strdup(tmp_password); - - if (!(ads = ads_startup())) return -1; - - ou_str = ads_ou_string(org_unit); - asprintf(&dn, "%s,%s", ou_str, ads->config.bind_path); - free(ou_str); - - rc = ads_search_dn(ads, &res, dn, NULL); - ads_msgfree(ads, res); - - if (rc.error_type == ADS_ERROR_LDAP && rc.err.rc == LDAP_NO_SUCH_OBJECT) { - d_printf("ads_join_realm: organizational unit %s does not exist (dn:%s)\n", - org_unit, dn); - return -1; - } - free(dn); - - if (!ADS_ERR_OK(rc)) { - d_printf("ads_join_realm: %s\n", ads_errstr(rc)); - return -1; - } - - rc = ads_join_realm(ads, lp_netbios_name(), org_unit); - if (!ADS_ERR_OK(rc)) { - d_printf("ads_join_realm: %s\n", ads_errstr(rc)); - return -1; - } - - rc = ads_domain_sid(ads, &dom_sid); - if (!ADS_ERR_OK(rc)) { - d_printf("ads_domain_sid: %s\n", ads_errstr(rc)); - return -1; - } - - rc = ads_set_machine_password(ads, lp_netbios_name(), password); - if (!ADS_ERR_OK(rc)) { - d_printf("ads_set_machine_password: %s\n", ads_errstr(rc)); - return -1; - } - - if (!secrets_store_domain_sid(lp_workgroup(), &dom_sid)) { - DEBUG(1,("Failed to save domain sid\n")); - return -1; - } - - if (!secrets_store_machine_password(password)) { - DEBUG(1,("Failed to save machine password\n")); - return -1; - } - - d_printf("Joined '%s' to realm '%s'\n", lp_netbios_name(), ads->config.realm); - - free(password); - - return 0; -} - -int net_ads_printer_usage(int argc, const char **argv) -{ - d_printf( -"\nnet ads printer info " -"\n\tlookup info in directory for printer on server" -"\n\t(note: printer defaults to \"*\", server defaults to local)\n" -"\nnet ads printer publish " -"\n\tpublish printer in directory" -"\n\t(note: printer name is required)\n" -"\nnet ads printer remove " -"\n\tremove printer from directory" -"\n\t(note: printer name is required)\n"); - return -1; -} - -static int net_ads_printer_info(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS rc; - const char *servername, *printername; - void *res = NULL; - - if (!(ads = ads_startup())) return -1; - - if (argc > 0) - printername = argv[0]; - else - printername = "*"; - - if (argc > 1) - servername = argv[1]; - else - servername = lp_netbios_name(); - - rc = ads_find_printer_on_server(ads, &res, printername, servername); - - if (!ADS_ERR_OK(rc)) { - d_printf("ads_find_printer_on_server: %s\n", ads_errstr(rc)); - ads_msgfree(ads, res); - return -1; - } - - if (ads_count_replies(ads, res) == 0) { - d_printf("Printer '%s' not found\n", printername); - ads_msgfree(ads, res); - return -1; - } - - ads_dump(ads, res); - ads_msgfree(ads, res); - - return 0; -} - -void do_drv_upgrade_printer(int msg_type, pid_t src, void *buf, size_t len) -{ - return; -} - -static int net_ads_printer_publish(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS rc; - const char *servername; - struct smbcli_state *cli; - struct in_addr server_ip; - NTSTATUS nt_status; - TALLOC_CTX *mem_ctx = talloc_init("net_ads_printer_publish"); - ADS_MODLIST mods = ads_init_mods(mem_ctx); - char *prt_dn, *srv_dn, **srv_cn; - void *res = NULL; - - if (!(ads = ads_startup())) return -1; - - if (argc < 1) - return net_ads_printer_usage(argc, argv); - - if (argc == 2) - servername = argv[1]; - else - servername = lp_netbios_name(); - - ads_find_machine_acct(ads, &res, servername); - srv_dn = ldap_get_dn(ads->ld, res); - srv_cn = ldap_explode_dn(srv_dn, 1); - asprintf(&prt_dn, "cn=%s-%s,%s", srv_cn[0], argv[0], srv_dn); - - resolve_name(servername, &server_ip, 0x20); - - nt_status = smbcli_full_connection(&cli, lp_netbios_name(), servername, - &server_ip, 0, - "IPC$", "IPC", - opt_user_name, opt_workgroup, - opt_password ? opt_password : "", - SMBCLI_FULL_CONNECTION_USE_KERBEROS, - NULL); - - smbcli_nt_session_open(cli, PI_SPOOLSS); - get_remote_printer_publishing_data(cli, mem_ctx, &mods, argv[0]); - - rc = ads_add_printer_entry(ads, prt_dn, mem_ctx, &mods); - if (!ADS_ERR_OK(rc)) { - d_printf("ads_publish_printer: %s\n", ads_errstr(rc)); - return -1; - } - - d_printf("published printer\n"); - - return 0; -} - -static int net_ads_printer_remove(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS rc; - const char *servername; - char *prt_dn; - void *res = NULL; - - if (!(ads = ads_startup())) return -1; - - if (argc < 1) - return net_ads_printer_usage(argc, argv); - - if (argc > 1) - servername = argv[1]; - else - servername = lp_netbios_name(); - - rc = ads_find_printer_on_server(ads, &res, argv[0], servername); - - if (!ADS_ERR_OK(rc)) { - d_printf("ads_find_printer_on_server: %s\n", ads_errstr(rc)); - ads_msgfree(ads, res); - return -1; - } - - if (ads_count_replies(ads, res) == 0) { - d_printf("Printer '%s' not found\n", argv[1]); - ads_msgfree(ads, res); - return -1; - } - - prt_dn = ads_get_dn(ads, res); - ads_msgfree(ads, res); - rc = ads_del_dn(ads, prt_dn); - ads_memfree(ads, prt_dn); - - if (!ADS_ERR_OK(rc)) { - d_printf("ads_del_dn: %s\n", ads_errstr(rc)); - return -1; - } - - return 0; -} - -static int net_ads_printer(int argc, const char **argv) -{ - struct functable func[] = { - {"INFO", net_ads_printer_info}, - {"PUBLISH", net_ads_printer_publish}, - {"REMOVE", net_ads_printer_remove}, - {NULL, NULL} - }; - - return net_run_function(argc, argv, func, net_ads_printer_usage); -} - - -static int net_ads_password(int argc, const char **argv) -{ - ADS_STRUCT *ads; - const char *auth_principal = opt_user_name; - const char *auth_password = opt_password; - char *realm = NULL; - char *new_password = NULL; - char *c; - char *prompt; - ADS_STATUS ret; - - - if ((argc != 1) || (opt_user_name == NULL) || - (opt_password == NULL) || (strchr(opt_user_name, '@') == NULL) || - (strchr(argv[0], '@') == NULL)) { - return net_ads_usage(argc, argv); - } - - use_in_memory_ccache(); - c = strchr(auth_principal, '@'); - realm = ++c; - - /* use the realm so we can eventually change passwords for users - in realms other than default */ - if (!(ads = ads_init(realm, NULL, NULL))) return -1; - - asprintf(&prompt, "Enter new password for %s:", argv[0]); - - new_password = getpass(prompt); - - ret = kerberos_set_password(ads->auth.kdc_server, auth_principal, - auth_password, argv[0], new_password, ads->auth.time_offset); - if (!ADS_ERR_OK(ret)) { - d_printf("Password change failed :-( ...\n"); - ads_destroy(&ads); - free(prompt); - return -1; - } - - d_printf("Password change for %s completed.\n", argv[0]); - ads_destroy(&ads); - free(prompt); - - return 0; -} - - -static int net_ads_change_localhost_pass(int argc, const char **argv) -{ - ADS_STRUCT *ads; - char *host_principal; - char *hostname; - ADS_STATUS ret; - char *user_name; - - if (!secrets_init()) { - DEBUG(1,("Failed to initialise secrets database\n")); - return -1; - } - - asprintf(&user_name, "%s$", lp_netbios_name()); - opt_user_name = user_name; - - opt_password = secrets_fetch_machine_password(); - - use_in_memory_ccache(); - - if (!(ads = ads_startup())) { - return -1; - } - - hostname = strdup(lp_netbios_name()); - strlower(hostname); - asprintf(&host_principal, "%s@%s", hostname, ads->config.realm); - SAFE_FREE(hostname); - d_printf("Changing password for principal: HOST/%s\n", host_principal); - - ret = ads_change_trust_account_password(ads, host_principal); - - if (!ADS_ERR_OK(ret)) { - d_printf("Password change failed :-( ...\n"); - ads_destroy(&ads); - SAFE_FREE(host_principal); - return -1; - } - - d_printf("Password change for principal HOST/%s succeeded.\n", host_principal); - ads_destroy(&ads); - SAFE_FREE(host_principal); - - return 0; -} - -/* - help for net ads search -*/ -static int net_ads_search_usage(int argc, const char **argv) -{ - d_printf( - "\nnet ads search \n"\ - "\nperform a raw LDAP search on a ADS server and dump the results\n"\ - "The expression is a standard LDAP search expression, and the\n"\ - "attributes are a list of LDAP fields to show in the results\n\n"\ - "Example: net ads search '(objectCategory=group)' sAMAccountName\n\n" - ); - net_common_flags_usage(argc, argv); - return -1; -} - - -/* - general ADS search function. Useful in diagnosing problems in ADS -*/ -static int net_ads_search(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS rc; - const char *exp; - const char **attrs; - void *res = NULL; - - if (argc < 1) { - return net_ads_search_usage(argc, argv); - } - - if (!(ads = ads_startup())) { - return -1; - } - - exp = argv[0]; - attrs = (argv + 1); - - rc = ads_do_search_all(ads, ads->config.bind_path, - LDAP_SCOPE_SUBTREE, - exp, attrs, &res); - if (!ADS_ERR_OK(rc)) { - d_printf("search failed: %s\n", ads_errstr(rc)); - return -1; - } - - d_printf("Got %d replies\n\n", ads_count_replies(ads, res)); - - /* dump the results */ - ads_dump(ads, res); - - ads_msgfree(ads, res); - ads_destroy(&ads); - - return 0; -} - - -/* - help for net ads search -*/ -static int net_ads_dn_usage(int argc, const char **argv) -{ - d_printf( - "\nnet ads dn \n"\ - "\nperform a raw LDAP search on a ADS server and dump the results\n"\ - "The DN standard LDAP DN, and the attributes are a list of LDAP fields \n"\ - "to show in the results\n\n"\ - "Example: net ads dn 'CN=administrator,CN=Users,DC=my,DC=domain' sAMAccountName\n\n" - ); - net_common_flags_usage(argc, argv); - return -1; -} - - -/* - general ADS search function. Useful in diagnosing problems in ADS -*/ -static int net_ads_dn(int argc, const char **argv) -{ - ADS_STRUCT *ads; - ADS_STATUS rc; - const char *dn; - const char **attrs; - void *res = NULL; - - if (argc < 1) { - return net_ads_dn_usage(argc, argv); - } - - if (!(ads = ads_startup())) { - return -1; - } - - dn = argv[0]; - attrs = (argv + 1); - - rc = ads_do_search_all(ads, dn, - LDAP_SCOPE_BASE, - "(objectclass=*)", attrs, &res); - if (!ADS_ERR_OK(rc)) { - d_printf("search failed: %s\n", ads_errstr(rc)); - return -1; - } - - d_printf("Got %d replies\n\n", ads_count_replies(ads, res)); - - /* dump the results */ - ads_dump(ads, res); - - ads_msgfree(ads, res); - ads_destroy(&ads); - - return 0; -} - - -int net_ads_help(int argc, const char **argv) -{ - struct functable func[] = { - {"USER", net_ads_user_usage}, - {"GROUP", net_ads_group_usage}, - {"PRINTER", net_ads_printer_usage}, - {"SEARCH", net_ads_search_usage}, -#if 0 - {"INFO", net_ads_info}, - {"JOIN", net_ads_join}, - {"LEAVE", net_ads_leave}, - {"STATUS", net_ads_status}, - {"PASSWORD", net_ads_password}, - {"CHOSTPASS", net_ads_change_localhost_pass}, -#endif - {NULL, NULL} - }; - - return net_run_function(argc, argv, func, net_ads_usage); -} - -int net_ads(int argc, const char **argv) -{ - struct functable func[] = { - {"INFO", net_ads_info}, - {"JOIN", net_ads_join}, - {"TESTJOIN", net_ads_testjoin}, - {"LEAVE", net_ads_leave}, - {"STATUS", net_ads_status}, - {"USER", net_ads_user}, - {"GROUP", net_ads_group}, - {"PASSWORD", net_ads_password}, - {"CHOSTPASS", net_ads_change_localhost_pass}, - {"PRINTER", net_ads_printer}, - {"SEARCH", net_ads_search}, - {"DN", net_ads_dn}, - {"WORKGROUP", net_ads_workgroup}, - {"LOOKUP", net_ads_lookup}, - {"HELP", net_ads_help}, - {NULL, NULL} - }; - - return net_run_function(argc, argv, func, net_ads_usage); -} - -#else - -static int net_ads_noads(void) -{ - d_printf("ADS support not compiled in\n"); - return -1; -} - -int net_ads_usage(int argc, const char **argv) -{ - return net_ads_noads(); -} - -int net_ads_help(int argc, const char **argv) -{ - return net_ads_noads(); -} - -int net_ads_join(int argc, const char **argv) -{ - return net_ads_noads(); -} - -int net_ads_user(int argc, const char **argv) -{ - return net_ads_noads(); -} - -int net_ads_group(int argc, const char **argv) -{ - return net_ads_noads(); -} - -/* this one shouldn't display a message */ -int net_ads_check(void) -{ - return -1; -} - -int net_ads(int argc, const char **argv) -{ - return net_ads_usage(argc, argv); -} - -#endif diff --git a/source4/utils/net_ads_cldap.c b/source4/utils/net_ads_cldap.c deleted file mode 100644 index a4464182bb..0000000000 --- a/source4/utils/net_ads_cldap.c +++ /dev/null @@ -1,354 +0,0 @@ -/* - Samba Unix/Linux SMB client library - net ads cldap functions - Copyright (C) 2001 Andrew Tridgell (tridge@samba.org) - Copyright (C) 2003 Jim McDonough (jmcd@us.ibm.com) - - 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 "../utils/net.h" - -#ifdef HAVE_ADS - -struct netlogon_string { - uint32_t comp_len; - char **component; - uint8_t extra_flag; -}; - -struct cldap_netlogon_reply { - uint32_t type; - uint32_t flags; - GUID guid; - - struct netlogon_string forest; - struct netlogon_string domain; - struct netlogon_string hostname; - - struct netlogon_string netbios_domain; - struct netlogon_string netbios_hostname; - - struct netlogon_string user_name; - struct netlogon_string site_name; - - struct netlogon_string unk0; - - uint32_t version; - uint16_t lmnt_token; - uint16_t lm20_token; -}; - -/* - These strings are rather interesting... They are composed of a series of - length encoded strings, terminated by either 1) a zero length string or 2) - a 0xc0 byte with what appears to be a one byte flags immediately following. -*/ -static uint_t pull_netlogon_string(struct netlogon_string *ret,const char *d) -{ - char *p = (char *)d; - - ZERO_STRUCTP(ret); - - do { - uint_t len = (uint8_t)*p; - p++; - - if (len > 0 && len != 0xc0) { - ret->component = realloc(ret->component, - ++ret->comp_len * - sizeof(char *)); - - ret->component[ret->comp_len - 1] = - smb_xstrndup(p, len); - p += len; - } else { - if (len == 0xc0) { - ret->extra_flag = *p; - p++; - }; - break; - } - } while (1); - - return (p - d); -} - -/* - do a cldap netlogon query -*/ -static int send_cldap_netlogon(int sock, const char *domain, - const char *hostname, uint_t ntversion) -{ - ASN1_DATA data; - char ntver[4]; - - SIVAL(ntver, 0, ntversion); - - memset(&data, 0, sizeof(data)); - - asn1_push_tag(&data,ASN1_SEQUENCE(0)); - asn1_write_Integer(&data, 4); - asn1_push_tag(&data, ASN1_APPLICATION(3)); - asn1_write_OctetString(&data, NULL, 0); - asn1_write_enumerated(&data, 0); - asn1_write_enumerated(&data, 0); - asn1_write_Integer(&data, 0); - asn1_write_Integer(&data, 0); - asn1_write_BOOLEAN2(&data, False); - asn1_push_tag(&data, ASN1_CONTEXT(0)); - - asn1_push_tag(&data, ASN1_CONTEXT(3)); - asn1_write_OctetString(&data, "DnsDomain", 9); - asn1_write_OctetString(&data, domain, strlen(domain)); - asn1_pop_tag(&data); - - asn1_push_tag(&data, ASN1_CONTEXT(3)); - asn1_write_OctetString(&data, "Host", 4); - asn1_write_OctetString(&data, hostname, strlen(hostname)); - asn1_pop_tag(&data); - - asn1_push_tag(&data, ASN1_CONTEXT(3)); - asn1_write_OctetString(&data, "NtVer", 5); - asn1_write_OctetString(&data, ntver, 4); - asn1_pop_tag(&data); - - asn1_pop_tag(&data); - - asn1_push_tag(&data,ASN1_SEQUENCE(0)); - asn1_write_OctetString(&data, "NetLogon", 8); - asn1_pop_tag(&data); - asn1_pop_tag(&data); - asn1_pop_tag(&data); - - if (data.has_error) { - d_printf("Failed to build cldap netlogon at offset %d\n", (int)data.ofs); - asn1_free(&data); - return -1; - } - - if (write(sock, data.data, data.length) != data.length) { - d_printf("failed to send cldap query (%s)\n", strerror(errno)); - } - - file_save("cldap_query.dat", data.data, data.length); - asn1_free(&data); - - return 0; -} - - -/* - receive a cldap netlogon reply -*/ -static int recv_cldap_netlogon(int sock, struct cldap_netlogon_reply *reply) -{ - int ret; - ASN1_DATA data; - DATA_BLOB blob; - DATA_BLOB os1, os2, os3; - uint32_t i1; - char *p; - - blob = data_blob(NULL, 8192); - - ret = read(sock, blob.data, blob.length); - - if (ret <= 0) { - d_printf("no reply received to cldap netlogon\n"); - return -1; - } - blob.length = ret; - - file_save("cldap_reply.dat", blob.data, blob.length); - - asn1_load(&data, blob); - asn1_start_tag(&data, ASN1_SEQUENCE(0)); - asn1_read_Integer(&data, &i1); - asn1_start_tag(&data, ASN1_APPLICATION(4)); - asn1_read_OctetString(&data, &os1); - asn1_start_tag(&data, ASN1_SEQUENCE(0)); - asn1_start_tag(&data, ASN1_SEQUENCE(0)); - asn1_read_OctetString(&data, &os2); - asn1_start_tag(&data, ASN1_SET); - asn1_read_OctetString(&data, &os3); - asn1_end_tag(&data); - asn1_end_tag(&data); - asn1_end_tag(&data); - asn1_end_tag(&data); - asn1_end_tag(&data); - - if (data.has_error) { - d_printf("Failed to parse cldap reply\n"); - return -1; - } - - file_save("cldap_reply_core.dat", os3.data, os3.length); - - p = os3.data; - - reply->type = IVAL(p, 0); p += 4; - reply->flags = IVAL(p, 0); p += 4; - - memcpy(&reply->guid.info, p, GUID_SIZE); - p += GUID_SIZE; - - p += pull_netlogon_string(&reply->forest, p); - p += pull_netlogon_string(&reply->domain, p); - p += pull_netlogon_string(&reply->hostname, p); - p += pull_netlogon_string(&reply->netbios_domain, p); - p += pull_netlogon_string(&reply->netbios_hostname, p); - p += pull_netlogon_string(&reply->user_name, p); - p += pull_netlogon_string(&reply->site_name, p); - - p += pull_netlogon_string(&reply->unk0, p); - - reply->version = IVAL(p, 0); - reply->lmnt_token = SVAL(p, 4); - reply->lm20_token = SVAL(p, 6); - - data_blob_free(&os1); - data_blob_free(&os2); - data_blob_free(&os3); - data_blob_free(&blob); - - return 0; -} - -/* - free a netlogon string -*/ -static void netlogon_string_free(struct netlogon_string *str) -{ - int i; - - for (i = 0; i < str->comp_len; ++i) { - SAFE_FREE(str->component[i]); - } - SAFE_FREE(str->component); -} - -/* - free a cldap reply packet -*/ -static void cldap_reply_free(struct cldap_netlogon_reply *reply) -{ - netlogon_string_free(&reply->forest); - netlogon_string_free(&reply->domain); - netlogon_string_free(&reply->hostname); - netlogon_string_free(&reply->netbios_domain); - netlogon_string_free(&reply->netbios_hostname); - netlogon_string_free(&reply->user_name); - netlogon_string_free(&reply->site_name); - netlogon_string_free(&reply->unk0); -} - -static void d_print_netlogon_string(const char *label, - struct netlogon_string *str) -{ - int i; - - if (str->comp_len) { - d_printf("%s", label); - if (str->extra_flag) { - d_printf("[%d]", str->extra_flag); - } - d_printf(": "); - for (i = 0; i < str->comp_len; ++i) { - d_printf("%s%s", (i ? "." : ""), str->component[i]); - } - d_printf("\n"); - } -} - -/* - do a cldap netlogon query -*/ -int ads_cldap_netlogon(ADS_STRUCT *ads) -{ - int sock; - int ret; - struct cldap_netlogon_reply reply; - - sock = open_udp_socket(inet_ntoa(ads->ldap_ip), ads->ldap_port); - if (sock == -1) { - d_printf("Failed to open udp socket to %s:%u\n", - inet_ntoa(ads->ldap_ip), - ads->ldap_port); - return -1; - } - - ret = send_cldap_netlogon(sock, ads->config.realm, lp_netbios_name(), 6); - if (ret != 0) { - return ret; - } - ret = recv_cldap_netlogon(sock, &reply); - close(sock); - - if (ret == -1) { - return -1; - } - - d_printf("Information for Domain Controller: %s\n\n", - ads->config.ldap_server_name); - - d_printf("Response Type: 0x%x\n", reply.type); - d_printf("GUID: "); - print_guid(&reply.guid); - d_printf("Flags:\n" - "\tIs a PDC: %s\n" - "\tIs a GC of the forest: %s\n" - "\tIs an LDAP server: %s\n" - "\tSupports DS: %s\n" - "\tIs running a KDC: %s\n" - "\tIs running time services: %s\n" - "\tIs the closest DC: %s\n" - "\tIs writable: %s\n" - "\tHas a hardware clock: %s\n" - "\tIs a non-domain NC serviced by LDAP server: %s\n", - (reply.flags & ADS_PDC) ? "yes" : "no", - (reply.flags & ADS_GC) ? "yes" : "no", - (reply.flags & ADS_LDAP) ? "yes" : "no", - (reply.flags & ADS_DS) ? "yes" : "no", - (reply.flags & ADS_KDC) ? "yes" : "no", - (reply.flags & ADS_TIMESERV) ? "yes" : "no", - (reply.flags & ADS_CLOSEST) ? "yes" : "no", - (reply.flags & ADS_WRITABLE) ? "yes" : "no", - (reply.flags & ADS_GOOD_TIMESERV) ? "yes" : "no", - (reply.flags & ADS_NDNC) ? "yes" : "no"); - - d_print_netlogon_string("Forest", &reply.forest); - d_print_netlogon_string("Domain", &reply.domain); - d_print_netlogon_string("Hostname", &reply.hostname); - - d_print_netlogon_string("Pre-Win2k Domain", &reply.netbios_domain); - d_print_netlogon_string("Pre-Win2k Hostname", &reply.netbios_hostname); - - d_print_netlogon_string("User name", &reply.user_name); - d_print_netlogon_string("Site Name", &reply.site_name); - d_print_netlogon_string("Unknown Field", &reply.unk0); - - d_printf("NT Version: %d\n", reply.version); - d_printf("LMNT Token: %.2x\n", reply.lmnt_token); - d_printf("LM20 Token: %.2x\n", reply.lm20_token); - - cldap_reply_free(&reply); - - return ret; -} - - -#endif diff --git a/source4/utils/net_cache.c b/source4/utils/net_cache.c deleted file mode 100644 index 93c4f1aa1d..0000000000 --- a/source4/utils/net_cache.c +++ /dev/null @@ -1,348 +0,0 @@ -/* - Samba Unix/Linux SMB client library - Distributed SMB/CIFS Server Management Utility - Copyright (C) Rafal Szczesniak 2002 - - 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 "net.h" - -/** - * @file net_cache.c - * @brief This is part of the net tool which is basically command - * line wrapper for gencache.c functions (mainly for testing) - * - **/ - - -/* - * These routines are used via gencache_iterate() to display the cache's contents - * (print_cache_entry) and to flush it (delete_cache_entry). - * Both of them are defined by first arg of gencache_iterate() routine. - */ -static void print_cache_entry(const char* keystr, const char* datastr, - const time_t timeout, void* dptr) -{ - char* timeout_str; - time_t now_t = time(NULL); - struct tm timeout_tm, *now_tm; - /* localtime returns statically allocated pointer, so timeout_tm - has to be copied somewhere else */ - memcpy(&timeout_tm, localtime(&timeout), sizeof(struct tm)); - now_tm = localtime(&now_t); - - /* form up timeout string depending whether it's today's date or not */ - if (timeout_tm.tm_year != now_tm->tm_year || - timeout_tm.tm_mon != now_tm->tm_mon || - timeout_tm.tm_mday != now_tm->tm_mday) { - - timeout_str = asctime(&timeout_tm); - timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */ - } else - asprintf(&timeout_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour, - timeout_tm.tm_min, timeout_tm.tm_sec); - - d_printf("Key: %s\t Timeout: %s\t Value: %s %s\n", keystr, - timeout_str, datastr, timeout > now_t ? "": "(expired)"); -} - -static void delete_cache_entry(const char* keystr, const char* datastr, - const time_t timeout, void* dptr) -{ - if (!gencache_del(keystr)) - d_printf("Couldn't delete entry! key = %s", keystr); -} - - -/** - * Parse text representation of timeout value - * - * @param timeout_str string containing text representation of the timeout - * @return numeric timeout of time_t type - **/ -static time_t parse_timeout(const char* timeout_str) -{ - char sign = '\0', *number = NULL, unit = '\0'; - int len, number_begin, number_end; - time_t timeout; - - /* sign detection */ - if (timeout_str[0] == '!' || timeout_str[0] == '+') { - sign = timeout_str[0]; - number_begin = 1; - } else { - number_begin = 0; - } - - /* unit detection */ - len = strlen(timeout_str); - switch (timeout_str[len - 1]) { - case 's': - case 'm': - case 'h': - case 'd': - case 'w': unit = timeout_str[len - 1]; - } - - /* number detection */ - len = (sign) ? strlen(&timeout_str[number_begin]) : len; - number_end = (unit) ? len - 1 : len; - number = strndup(&timeout_str[number_begin], number_end); - - /* calculate actual timeout value */ - timeout = (time_t)atoi(number); - - switch (unit) { - case 'm': timeout *= 60; break; - case 'h': timeout *= 60*60; break; - case 'd': timeout *= 60*60*24; break; - case 'w': timeout *= 60*60*24*7; break; /* that's fair enough, I think :) */ - } - - switch (sign) { - case '!': timeout = time(NULL) - timeout; break; - case '+': - default: timeout += time(NULL); break; - } - - if (number) SAFE_FREE(number); - return timeout; -} - - -/** - * Add an entry to the cache. If it does exist, then set it. - * - * @param argv key, value and timeout are passed in command line - * @return 0 on success, otherwise failure - **/ -static int net_cache_add(int argc, const char **argv) -{ - const char *keystr, *datastr, *timeout_str; - time_t timeout; - - if (argc < 3) { - d_printf("\nUsage: net cache add \n"); - return -1; - } - - keystr = argv[0]; - datastr = argv[1]; - timeout_str = argv[2]; - - /* parse timeout given in command line */ - timeout = parse_timeout(timeout_str); - if (!timeout) { - d_printf("Invalid timeout argument.\n"); - return -1; - } - - if (gencache_set(keystr, datastr, timeout)) { - d_printf("New cache entry stored successfully.\n"); - gencache_shutdown(); - return 0; - } - - d_printf("Entry couldn't be added. Perhaps there's already such a key.\n"); - gencache_shutdown(); - return -1; -} - - -/** - * Set new value of an existing entry in the cache. Fail If the entry doesn't - * exist. - * - * @param argv key being searched and new value and timeout to set in the entry - * @return 0 on success, otherwise failure - **/ -static int net_cache_set(int argc, const char **argv) -{ - const char *keystr, *datastr, *timeout_str; - time_t timeout; - - if (argc < 3) { - d_printf("\nUsage: net cache set \n"); - return -1; - } - - keystr = argv[0]; - datastr = argv[1]; - timeout_str = argv[2]; - - /* parse timeout given in command line */ - timeout = parse_timeout(timeout_str); - if (!timeout) { - d_printf("Invalid timeout argument.\n"); - return -1; - } - - if (gencache_set_only(keystr, datastr, timeout)) { - d_printf("Cache entry set successfully.\n"); - gencache_shutdown(); - return 0; - } - - d_printf("Entry couldn't be set. Perhaps there's no such a key.\n"); - gencache_shutdown(); - return -1; -} - - -/** - * Delete an entry in the cache - * - * @param argv key to delete an entry of - * @return 0 on success, otherwise failure - **/ -static int net_cache_del(int argc, const char **argv) -{ - const char *keystr = argv[0]; - - if (argc < 1) { - d_printf("\nUsage: net cache add \n"); - return -1; - } - - if(gencache_del(keystr)) { - d_printf("Entry deleted.\n"); - return 0; - } - - d_printf("Couldn't delete specified entry\n"); - return -1; -} - - -/** - * Get and display an entry from the cache - * - * @param argv key to search an entry of - * @return 0 on success, otherwise failure - **/ -static int net_cache_get(int argc, const char **argv) -{ - const char* keystr = argv[0]; - char* valuestr; - time_t timeout; - - if (argc < 1) { - d_printf("\nUsage: net cache get \n"); - return -1; - } - - if (gencache_get(keystr, &valuestr, &timeout)) { - print_cache_entry(keystr, valuestr, timeout, NULL); - return 0; - } - - d_printf("Failed to find entry\n"); - return -1; -} - - -/** - * Search an entry/entries in the cache - * - * @param argv key pattern to match the entries to - * @return 0 on success, otherwise failure - **/ -static int net_cache_search(int argc, const char **argv) -{ - const char* pattern; - - if (argc < 1) { - d_printf("Usage: net cache search \n"); - return -1; - } - - pattern = argv[0]; - gencache_iterate(print_cache_entry, NULL, pattern); - return 0; -} - - -/** - * List the contents of the cache - * - * @param argv ignored in this functionailty - * @return always returns 0 - **/ -static int net_cache_list(int argc, const char **argv) -{ - const char* pattern = "*"; - gencache_iterate(print_cache_entry, NULL, pattern); - gencache_shutdown(); - return 0; -} - - -/** - * Flush the whole cache - * - * @param argv ignored in this functionality - * @return always returns 0 - **/ -static int net_cache_flush(int argc, const char **argv) -{ - const char* pattern = "*"; - gencache_iterate(delete_cache_entry, NULL, pattern); - gencache_shutdown(); - return 0; -} - - -/** - * Short help - * - * @param argv ignored in this functionality - * @return always returns -1 - **/ -static int net_cache_usage(int argc, const char **argv) -{ - d_printf(" net cache add \t add add new cache entry\n"); - d_printf(" net cache set \t set new value for existing cache entry\n"); - d_printf(" net cache del \t delete existing cache entry by key\n"); - d_printf(" net cache flush \t delete all entries existing in the cache\n"); - d_printf(" net cache get \t get cache entry by key\n"); - d_printf(" net cache search \t search for entries in the cache, by given pattern\n"); - d_printf(" net cache list \t list all cache entries (just like search for \"*\")\n"); - return -1; -} - - -/** - * Entry point to 'net cache' subfunctionality - * - * @param argv arguments passed to further called functions - * @return whatever further functions return - **/ -int net_cache(int argc, const char **argv) -{ - struct functable func[] = { - {"add", net_cache_add}, - {"set", net_cache_set}, - {"del", net_cache_del}, - {"get", net_cache_get}, - {"search", net_cache_search}, - {"list", net_cache_list}, - {"flush", net_cache_flush}, - {NULL, NULL} - }; - - return net_run_function(argc, argv, func, net_cache_usage); -} diff --git a/source4/utils/net_help.c b/source4/utils/net_help.c deleted file mode 100644 index 4000a248ff..0000000000 --- a/source4/utils/net_help.c +++ /dev/null @@ -1,199 +0,0 @@ -/* - Samba Unix/Linux SMB client library - net help commands - Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com) - - 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 "../utils/net.h" - -int net_common_methods_usage(int argc, const char**argv) -{ - d_printf("Valid methods: (auto-detected if not specified)\n"); - d_printf("\tads\t\t\t\tActive Directory (LDAP/Kerberos)\n"); - d_printf("\trpc\t\t\t\tDCE-RPC\n"); - d_printf("\trap\t\t\t\tRAP (older systems)\n"); - d_printf("\n"); - return 0; -} - -int net_common_flags_usage(int argc, const char **argv) -{ - d_printf("Valid targets: choose one (none defaults to localhost)\n"); - d_printf("\t-S or --server=\t\tserver name\n"); - d_printf("\t-I or --ipaddress=\taddress of target server\n"); - d_printf("\t-w or --workgroup=\t\ttarget workgroup or domain\n"); - - d_printf("\n"); - d_printf("Valid miscellaneous options are:\n"); /* misc options */ - d_printf("\t-p or --port=\t\tconnection port on target\n"); - d_printf("\t-W or --myworkgroup=\tclient workgroup\n"); - d_printf("\t-d or --debug=\t\tdebug level (0-10)\n"); - d_printf("\t-n or --myname=\t\tclient name\n"); - d_printf("\t-U or --user=\t\tuser name\n"); - d_printf("\t-s or --conf=\t\tpathname of smb.conf file\n"); - d_printf("\t-l or --long\t\t\tDisplay full information\n"); - d_printf("\t-P or --machine-pass\t\tAuthenticate as machine account\n"); - return -1; -} - -static int help_usage(int argc, const char **argv) -{ - d_printf( -"\n"\ -"Usage: net help \n"\ -"\n"\ -"Valid functions are:\n"\ -" RPC RAP ADS FILE SHARE SESSION SERVER DOMAIN PRINTQ USER GROUP VALIDATE\n"\ -" GROUPMEMBER ADMIN SERVICE PASSWORD TIME LOOKUP GETLOCALSID SETLOCALSID\n"); - return -1; -} - -int net_help_user(int argc, const char **argv) -{ - d_printf("\nnet [] user [misc. options] [targets]"\ - "\n\tList users\n\n"); - d_printf("net [] user DELETE [misc. options] [targets]"\ - "\n\tDelete specified user\n"); - d_printf("\nnet [] user INFO [misc. options] [targets]"\ - "\n\tList the domain groups of the specified user\n"); - d_printf("\nnet [] user ADD [password] [-c container] "\ - "[-F user flags] [misc. options]"\ - " [targets]\n\tAdd specified user\n"); - - net_common_methods_usage(argc, argv); - net_common_flags_usage(argc, argv); - d_printf("\t-C or --comment=\tdescriptive comment (for add only)\n"); - d_printf("\t-c or --container=\tLDAP container, defaults to cn=Users (for add in ADS only)\n"); - return -1; -} - -int net_help_group(int argc, const char **argv) -{ - d_printf("net [] group [misc. options] [targets]"\ - "\n\tList user groups\n\n"); - d_printf("net [] group DELETE "\ - "[misc. options] [targets]"\ - "\n\tDelete specified group\n"); - d_printf("\nnet [] group ADD [-C comment] [-c container]"\ - " [misc. options] [targets]\n\tCreate specified group\n"); - net_common_methods_usage(argc, argv); - net_common_flags_usage(argc, argv); - d_printf("\t-C or --comment=\tdescriptive comment (for add only)\n"); - d_printf("\t-c or --container=\tLDAP container, defaults to cn=Users (for add in ADS only)\n"); - return -1; -} - - -int net_help_join(int argc, const char **argv) -{ - d_printf("\nnet [] join [misc. options]\n" - "\tjoins this server to a domain\n"); - d_printf("Valid methods: (auto-detected if not specified)\n"); - d_printf("\tads\t\t\t\tActive Directory (LDAP/Kerberos)\n"); - d_printf("\trpc\t\t\t\tDCE-RPC\n"); - net_common_flags_usage(argc, argv); - return -1; -} - -int net_help_share(int argc, const char **argv) -{ - d_printf( - "\nnet [] share [misc. options] [targets] \n" - "\tenumerates all exported resources (network shares) " - "on target server\n\n" - "net [] share ADD [misc. options] [targets]" - "\n\tAdds a share from a server (makes the export active)\n\n" - "net [] share DELETE [misc. options] [targets]\n" - "\n\tDeletes a share from a server (makes the export inactive)\n"); - net_common_methods_usage(argc, argv); - net_common_flags_usage(argc, argv); - d_printf( - "\t-C or --comment=\tdescriptive comment (for add only)\n" - "\t-M or --maxusers=\t\tmax users allowed for share\n"); - return -1; -} - -int net_help_file(int argc, const char **argv) -{ - d_printf("net [] file [misc. options] [targets]\n"\ - "\tlists all open files on file server\n\n"); - d_printf("net [] file USER "\ - "[misc. options] [targets]"\ - "\n\tlists all files opened by username on file server\n\n"); - d_printf("net [] file CLOSE [misc. options] [targets]\n"\ - "\tcloses specified file on target server\n\n"); - d_printf("net [rap] file INFO [misc. options] [targets]\n"\ - "\tdisplays information about the specified open file\n"); - - net_common_methods_usage(argc, argv); - net_common_flags_usage(argc, argv); - return -1; -} - -static int net_usage(int argc, const char **argv) -{ - d_printf(" net time\t\tto view or set time information\n"\ - " net lookup\t\tto lookup host name or ip address\n"\ - " net user\t\tto manage users\n"\ - " net group\t\tto manage groups\n"\ - " net join\t\tto join a domain\n"\ - " net cache\t\tto operate on cache tdb file\n"\ - " net getlocalsid [NAME]\tto get the SID for local name\n"\ - " net setlocalsid SID\tto set the local domain SID\n"\ - "\n"\ - " net ads \tto run ADS commands\n"\ - " net rap \tto run RAP (pre-RPC) commands\n"\ - " net rpc \tto run RPC commands\n"\ - "\n"\ - "Type \"net help