2 Samba Unix/Linux SMB client library
4 Distributed SMB/CIFS Server Management Utility
5 Copyright (C) 2001 Steve French (sfrench@us.ibm.com)
6 Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com)
7 Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
8 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
10 Originally written by Steve and Jim. Largely rewritten by tridge in
13 Reworked again by abartlet in December 2001
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /*****************************************************/
31 /* Distributed SMB/CIFS Server Management Utility */
33 /* The intent was to make the syntax similar */
34 /* to the NET utility (first developed in DOS */
35 /* with additional interesting & useful functions */
36 /* added in later SMB server network operating */
39 /*****************************************************/
42 #include "../utils/net.h"
44 /***********************************************************************/
45 /* Beginning of internationalization section. Translatable constants */
46 /* should be kept in this area and referenced in the rest of the code. */
48 /* No functions, outside of Samba or LSB (Linux Standards Base) should */
49 /* be used (if possible). */
50 /***********************************************************************/
52 #define YES_STRING "Yes"
53 #define NO_STRING "No"
55 /************************************************************************************/
56 /* end of internationalization section */
57 /************************************************************************************/
59 extern int optind, opterr, optopt;
61 /* Yes, these buggers are globals.... */
62 char *opt_requester_name = NULL;
63 char *opt_host = NULL;
64 char *opt_password = NULL;
65 char *opt_user_name = NULL;
66 char *opt_workgroup = NULL;
67 int opt_long_list_entries = 0;
69 int opt_maxusers = -1;
70 char *opt_comment = "";
73 char *opt_target_workgroup = NULL;
75 static BOOL got_pass = False;
76 static BOOL have_ip = False;
77 static struct in_addr dest_ip;
79 extern pstring global_myname;
82 run a function from a function table. If not found then
83 call the specified usage function
85 int net_run_function(int argc, const char **argv, struct functable *table,
86 int (*usage_fn)(int argc, const char **argv))
91 d_printf("\nUsage: \n");
92 return usage_fn(argc, argv);
94 for (i=0; table[i].funcname; i++) {
95 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
96 return table[i].fn(argc-1, argv+1);
98 d_printf("No command: %s\n", argv[0]);
99 return usage_fn(argc, argv);
103 /****************************************************************************
104 connect to \\server\ipc$
105 ****************************************************************************/
106 static struct cli_state *connect_to_ipc(struct in_addr *server_ip, const char *server_name)
112 char *pass = getpass("Password:");
114 opt_password = strdup(pass);
118 nt_status = cli_full_connection(&c, opt_requester_name, server_name,
121 opt_user_name, opt_workgroup,
122 opt_password, strlen(opt_password));
124 if (NT_STATUS_IS_OK(nt_status)) {
127 DEBUG(1,("Cannot connect to server. Error was %s\n", get_nt_error_msg(nt_status)));
132 /****************************************************************************
133 connect to \\server\ipc$ anonymously
134 ****************************************************************************/
135 static struct cli_state *connect_to_ipc_anonymous(struct in_addr *server_ip, const char *server_name)
140 nt_status = cli_full_connection(&c, opt_requester_name, server_name,
146 if (NT_STATUS_IS_OK(nt_status)) {
149 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", get_nt_error_msg(nt_status)));
154 static BOOL net_find_server(unsigned flags, struct in_addr *server_ip, char **server_name)
158 *server_name = strdup(opt_host);
162 *server_ip = dest_ip;
164 *server_name = strdup(inet_ntoa(dest_ip));
166 } else if (server_name) {
167 /* resolve the IP address */
168 if (!resolve_name(*server_name, server_ip, 0x20)) {
169 DEBUG(1,("Unable to resolve server name\n"));
172 } else if (flags & NET_FLAGS_PDC) {
173 struct in_addr *ip_list;
175 if (get_dc_list(True /* PDC only*/, opt_target_workgroup, &ip_list, &addr_count)) {
177 if (addr_count < 1) {
181 *server_ip = *ip_list;
183 if (is_zero_ip(*server_ip))
186 if (!lookup_dc_name(global_myname, opt_target_workgroup, server_ip, dc_name))
189 *server_name = strdup(dc_name);
192 } else if (flags & NET_FLAGS_DMB) {
193 struct in_addr msbrow_ip;
194 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
195 if (!resolve_name(opt_target_workgroup, &msbrow_ip, 0x1B)) {
196 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
199 *server_ip = msbrow_ip;
201 *server_name = strdup(inet_ntoa(dest_ip));
202 } else if (flags & NET_FLAGS_MASTER) {
203 struct in_addr brow_ips;
204 if (!resolve_name(opt_target_workgroup, &brow_ips, 0x1D)) {
205 /* go looking for workgroups */
206 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
209 *server_ip = brow_ips;
211 *server_name = strdup(inet_ntoa(dest_ip));
212 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
213 extern struct in_addr loopback_ip;
214 *server_ip = loopback_ip;
215 *server_name = strdup("127.0.0.1");
218 if (!server_name || !*server_name) {
219 DEBUG(1,("no server to connect to\n"));
226 struct cli_state *net_make_ipc_connection(unsigned flags)
228 char *server_name = NULL;
229 struct in_addr server_ip;
230 struct cli_state *cli;
232 if (!net_find_server(flags, &server_ip, &server_name)) {
233 d_printf("\nUnable to find a suitable server\n");
237 if (flags & NET_FLAGS_ANONYMOUS) {
238 cli = connect_to_ipc_anonymous(&server_ip, server_name);
240 cli = connect_to_ipc(&server_ip, server_name);
242 SAFE_FREE(server_name);
244 d_printf("\nUnable to connect to target server\n");
251 static int net_usage(int argc, const char **argv)
253 d_printf(" net ads [command]\tto run ADS commands\n"\
254 " net rap [command]\tto run RAP (pre-RPC) commands\n"\
255 " net rpc [command]\tto run RPC commands\n"\
257 "\nType \"net help <option>\" to get more information on that option\n");
261 static int help_usage(int argc, const char **argv)
264 "Usage: net help <function>\n"\
266 "Valid functions are:\n"\
272 handle "net help *" subcommands
274 static int net_help(int argc, const char **argv)
276 struct functable func[] = {
277 {"ADS", net_ads_usage},
278 {"RAP", net_rap_usage},
279 {"RPC", net_rpc_usage},
280 {"HELP", help_usage},
283 return net_run_function(argc, argv, func, help_usage);
286 /* main function table */
287 static struct functable net_func[] = {
296 /****************************************************************************
298 ****************************************************************************/
299 int main(int argc, const char **argv)
305 const char ** argv_new;
307 static char *servicesf = dyn_CONFIGFILE;
308 static int debuglevel;
310 struct poptOption long_options[] = {
311 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
312 {"workgroup", 'w', POPT_ARG_STRING, &opt_target_workgroup},
313 {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
314 {"user", 'U', POPT_ARG_STRING, &opt_user_name, 'U'},
315 {"ipaddress", 'I', POPT_ARG_STRING, 0, 'I'},
316 {"port", 'p', POPT_ARG_INT, &opt_port},
317 {"myname", 'n', POPT_ARG_STRING, &opt_requester_name},
318 {"conf", 's', POPT_ARG_STRING, &servicesf},
319 {"debug", 'd', POPT_ARG_INT, &debuglevel, 'd'},
320 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd'},
321 {"server", 'S', POPT_ARG_STRING, &opt_host},
322 {"comment", 'C', POPT_ARG_STRING, &opt_comment},
323 {"maxusers", 'M', POPT_ARG_INT, &opt_maxusers},
324 {"flags", 'F', POPT_ARG_INT, &opt_flags},
325 {"jobid", 'j', POPT_ARG_INT, &opt_jobid},
326 {"long", 'l', POPT_ARG_NONE, &opt_long_list_entries},
335 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
336 POPT_CONTEXT_KEEP_FIRST);
338 while((opt = poptGetNextOpt(pc)) != -1) {
341 net_usage(argc, argv);
345 dest_ip = *interpret_addr2(poptGetOptArg(pc));
346 if (is_zero_ip(dest_ip))
347 d_printf("\nInvalid ip address specified\n");
352 opt_user_name = strdup(opt_user_name);
353 p = strchr(opt_user_name,'%');
361 d_printf("\nInvalid option %c (%d)\n", (char)opt, opt);
362 net_usage(argc, argv);
366 DEBUGLEVEL = debuglevel;
368 lp_load(servicesf,True,False,False);
370 argv_new = (const char **)poptGetArgs(pc);
373 for (i=0; i<argc; i++) {
374 if (argv_new[i] == NULL) {
380 if (!opt_requester_name) {
381 static fstring myname;
383 opt_requester_name = myname;
386 if (!opt_user_name && getenv("LOGNAME")) {
387 opt_user_name = getenv("LOGNAME");
390 if (!opt_workgroup) {
391 opt_workgroup = lp_workgroup();
394 if (!opt_target_workgroup) {
395 opt_target_workgroup = lp_workgroup();
398 if (!*global_myname) {
401 fstrcpy(global_myname, myhostname());
402 p2 = strchr_m(global_myname, '.');
409 rc = net_run_function(argc_new-1, argv_new+1, net_func, net_usage);
411 DEBUG(2,("return code = %d\n", rc));