2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Steve French (sfrench@us.ibm.com)
5 Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
7 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
9 Originally written by Steve and Jim. Largely rewritten by tridge in
12 Reworked again by abartlet in December 2001
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 /*****************************************************/
30 /* Distributed SMB/CIFS Server Management Utility */
32 /* The intent was to make the syntax similar */
33 /* to the NET utility (first developed in DOS */
34 /* with additional interesting & useful functions */
35 /* added in later SMB server network operating */
38 /*****************************************************/
41 #include "../utils/net.h"
43 /***********************************************************************/
44 /* Beginning of internationalization section. Translatable constants */
45 /* should be kept in this area and referenced in the rest of the code. */
47 /* No functions, outside of Samba or LSB (Linux Standards Base) should */
48 /* be used (if possible). */
49 /***********************************************************************/
51 #define YES_STRING "Yes"
52 #define NO_STRING "No"
54 /************************************************************************************/
55 /* end of internationalization section */
56 /************************************************************************************/
58 /* Yes, these buggers are globals.... */
59 char *opt_requester_name = NULL;
60 char *opt_host = NULL;
61 char *opt_password = NULL;
62 char *opt_user_name = NULL;
63 BOOL opt_user_specified = False;
64 char *opt_workgroup = NULL;
65 int opt_long_list_entries = 0;
69 int opt_maxusers = -1;
70 char *opt_comment = "";
74 char *opt_target_workgroup = NULL;
76 static BOOL got_pass = False;
77 BOOL opt_have_ip = False;
78 struct in_addr opt_dest_ip;
80 extern pstring global_myname;
82 int net_common_flags_usage(int argc, const char **argv)
85 d_printf("Valid targets: choose one (none defaults to localhost)\n");
86 d_printf("\t-S or --server=<server>\t\tserver name\n");
87 d_printf("\t-I or --ipaddress=<ipaddr>\taddress of target server\n");
88 d_printf("\t-w or --workgroup=<wg>\t\ttarget workgroup or domain\n");
91 d_printf("Valid miscellaneous options are:\n"); /* misc options */
92 d_printf("\t-p or --port=<port>\tconnection port on target server\n");
93 d_printf("\t-W or --myworkgroup=<wg>\tclient workgroup\n");
94 d_printf("\t-d or --debug=<level>\t\tdebug level (0-10)\n");
95 d_printf("\t-n or --myname=<name>\t\tclient name\n");
96 d_printf("\t-U or --user=<name>\t\tuser name\n");
97 d_printf("\t-s or --conf=<path>\t\tpathname of smb.conf file\n");
98 d_printf("\t-l or --long\t\t\tDisplay full information\n");
104 run a function from a function table. If not found then
105 call the specified usage function
107 int net_run_function(int argc, const char **argv, struct functable *table,
108 int (*usage_fn)(int argc, const char **argv))
113 d_printf("\nUsage: \n");
114 return usage_fn(argc, argv);
116 for (i=0; table[i].funcname; i++) {
117 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
118 return table[i].fn(argc-1, argv+1);
120 d_printf("No command: %s\n", argv[0]);
121 return usage_fn(argc, argv);
125 /****************************************************************************
126 connect to \\server\ipc$
127 ****************************************************************************/
128 NTSTATUS connect_to_ipc(struct cli_state **c, struct in_addr *server_ip,
129 const char *server_name)
134 char *pass = getpass("Password:");
136 opt_password = strdup(pass);
140 nt_status = cli_full_connection(c, opt_requester_name, server_name,
143 opt_user_name, opt_workgroup,
144 opt_password, strlen(opt_password));
146 if (NT_STATUS_IS_OK(nt_status)) {
149 DEBUG(0,("Cannot connect to server. Error was %s\n",
150 get_nt_error_msg(nt_status)));
152 /* Display a nicer message depending on the result */
154 if (NT_STATUS_V(nt_status) ==
155 NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
156 d_printf("The username or password was not correct.\n");
162 /****************************************************************************
163 connect to \\server\ipc$ anonymously
164 ****************************************************************************/
165 NTSTATUS connect_to_ipc_anonymous(struct cli_state **c,
166 struct in_addr *server_ip, const char *server_name)
170 nt_status = cli_full_connection(c, opt_requester_name, server_name,
176 if (NT_STATUS_IS_OK(nt_status)) {
179 DEBUG(0,("Cannot connect to server (anonymously). Error was %s\n", get_nt_error_msg(nt_status)));
184 static BOOL net_find_server(unsigned flags, struct in_addr *server_ip, char **server_name)
188 *server_name = strdup(opt_host);
192 *server_ip = opt_dest_ip;
194 *server_name = strdup(inet_ntoa(opt_dest_ip));
196 } else if (*server_name) {
197 /* resolve the IP address */
198 if (!resolve_name(*server_name, server_ip, 0x20)) {
199 DEBUG(1,("Unable to resolve server name\n"));
202 } else if (flags & NET_FLAGS_PDC) {
203 struct in_addr *ip_list;
205 if (get_dc_list(True /* PDC only*/, opt_target_workgroup, &ip_list, &addr_count)) {
207 if (addr_count < 1) {
211 *server_ip = *ip_list;
213 if (is_zero_ip(*server_ip))
216 if (!lookup_dc_name(global_myname, opt_target_workgroup, server_ip, dc_name))
219 *server_name = strdup(dc_name);
222 } else if (flags & NET_FLAGS_DMB) {
223 struct in_addr msbrow_ip;
224 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
225 if (!resolve_name(opt_target_workgroup, &msbrow_ip, 0x1B)) {
226 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
229 *server_ip = msbrow_ip;
231 *server_name = strdup(inet_ntoa(opt_dest_ip));
232 } else if (flags & NET_FLAGS_MASTER) {
233 struct in_addr brow_ips;
234 if (!resolve_name(opt_target_workgroup, &brow_ips, 0x1D)) {
235 /* go looking for workgroups */
236 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
239 *server_ip = brow_ips;
241 *server_name = strdup(inet_ntoa(opt_dest_ip));
242 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
243 extern struct in_addr loopback_ip;
244 *server_ip = loopback_ip;
245 *server_name = strdup("127.0.0.1");
248 if (!server_name || !*server_name) {
249 DEBUG(1,("no server to connect to\n"));
257 BOOL net_find_dc(struct in_addr *server_ip, fstring server_name, char *domain_name)
259 struct in_addr *ip_list;
262 if (get_dc_list(True /* PDC only*/, domain_name, &ip_list, &addr_count)) {
264 if (addr_count < 1) {
268 *server_ip = *ip_list;
270 if (is_zero_ip(*server_ip))
273 if (!lookup_dc_name(global_myname, domain_name, server_ip, dc_name))
276 safe_strcpy(server_name, dc_name, FSTRING_LEN);
283 struct cli_state *net_make_ipc_connection(unsigned flags)
285 char *server_name = NULL;
286 struct in_addr server_ip;
287 struct cli_state *cli = NULL;
290 if (!net_find_server(flags, &server_ip, &server_name)) {
291 d_printf("\nUnable to find a suitable server\n");
295 if (flags & NET_FLAGS_ANONYMOUS) {
296 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, server_name);
298 nt_status = connect_to_ipc(&cli, &server_ip, server_name);
300 SAFE_FREE(server_name);
305 static int net_join(int argc, const char **argv)
307 if (lp_security() < SEC_DOMAIN) {
308 d_printf("Error: security setting must be DOMAIN or ADS to "\
309 "joing a domain\n\n");
312 if (lp_security() == SEC_ADS) {
313 if (net_ads_join(argc, argv) == 0)
316 d_printf("ADS join did not work, trying RPC...\n");
318 return net_rpc_join(argc, argv);
321 static int net_usage(int argc, const char **argv)
323 d_printf(" net time\t\t to view or set time information\n"\
324 " net lookup\t\t to lookup host name or ip address\n"\
326 " net ads [command]\tto run ADS commands\n"\
327 " net rap [command]\tto run RAP (pre-RPC) commands\n"\
328 " net rpc [command]\tto run RPC commands\n"\
330 "Type \"net help <option>\" to get more information on that option\n");
334 static int help_usage(int argc, const char **argv)
338 "Usage: net help <function>\n"\
340 "Valid functions are:\n"\
341 " RPC RAP ADS FILE SHARE SESSION SERVER DOMAIN PRINTQ USER GROUP VALIDATE\n"\
342 " GROUPMEMBER ADMIN SERVICE PASSWORD TIME LOOKUP\n");
347 handle "net help *" subcommands
349 static int net_help(int argc, const char **argv)
351 struct functable func[] = {
352 {"ADS", net_ads_usage},
353 {"RAP", net_rap_help},
354 {"RPC", net_rpc_help},
356 {"FILE", net_rap_file_usage},
357 {"SHARE", net_rap_share_usage},
358 {"SESSION", net_rap_session_usage},
359 {"SERVER", net_rap_server_usage},
360 {"DOMAIN", net_rap_domain_usage},
361 {"PRINTQ", net_rap_printq_usage},
362 {"USER", net_rap_user_usage},
363 {"GROUP", net_rap_group_usage},
364 {"VALIDATE", net_rap_validate_usage},
365 {"GROUPMEMBER", net_rap_groupmember_usage},
366 {"ADMIN", net_rap_admin_usage},
367 {"SERVICE", net_rap_service_usage},
368 {"PASSWORD", net_rap_password_usage},
369 {"TIME", net_time_usage},
370 {"LOOKUP", net_lookup_usage},
372 {"HELP", help_usage},
375 return net_run_function(argc, argv, func, net_usage);
378 /* main function table */
379 static struct functable net_func[] = {
384 /* eventually these should auto-choose the transport ... */
385 {"FILE", net_rap_file},
386 {"SHARE", net_rap_share},
387 {"SESSION", net_rap_session},
388 {"SERVER", net_rap_server},
389 {"DOMAIN", net_rap_domain},
390 {"PRINTQ", net_rap_printq},
391 {"USER", net_rap_user},
392 {"GROUP", net_rap_group},
393 {"VALIDATE", net_rap_validate},
394 {"GROUPMEMBER", net_rap_groupmember},
395 {"ADMIN", net_rap_admin},
396 {"SERVICE", net_rap_service},
397 {"PASSWORD", net_rap_password},
399 {"LOOKUP", net_lookup},
407 /****************************************************************************
409 ****************************************************************************/
410 int main(int argc, const char **argv)
416 const char ** argv_new;
418 static char *servicesf = dyn_CONFIGFILE;
419 static int debuglevel;
421 struct poptOption long_options[] = {
422 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
423 {"workgroup", 'w', POPT_ARG_STRING, &opt_target_workgroup},
424 {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
425 {"user", 'U', POPT_ARG_STRING, &opt_user_name, 'U'},
426 {"ipaddress", 'I', POPT_ARG_STRING, 0,'I'},
427 {"port", 'p', POPT_ARG_INT, &opt_port},
428 {"myname", 'n', POPT_ARG_STRING, &opt_requester_name},
429 {"conf", 's', POPT_ARG_STRING, &servicesf},
430 {"debug", 'd', POPT_ARG_INT, &debuglevel, 'd'},
431 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd'},
432 {"server", 'S', POPT_ARG_STRING, &opt_host},
433 {"comment", 'C', POPT_ARG_STRING, &opt_comment},
434 {"maxusers", 'M', POPT_ARG_INT, &opt_maxusers},
435 {"flags", 'F', POPT_ARG_INT, &opt_flags},
436 {"jobid", 'j', POPT_ARG_INT, &opt_jobid},
437 {"long", 'l', POPT_ARG_NONE, &opt_long_list_entries},
438 {"reboot", 'r', POPT_ARG_NONE, &opt_reboot},
439 {"force", 'f', POPT_ARG_NONE, &opt_force},
440 {"timeout", 't', POPT_ARG_INT, &opt_timeout},
445 zero_ip(&opt_dest_ip);
449 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
450 POPT_CONTEXT_KEEP_FIRST);
452 while((opt = poptGetNextOpt(pc)) != -1) {
455 net_usage(argc, argv);
459 opt_dest_ip = *interpret_addr2(poptGetOptArg(pc));
460 if (is_zero_ip(opt_dest_ip))
461 d_printf("\nInvalid ip address specified\n");
466 opt_user_specified = True;
467 opt_user_name = strdup(opt_user_name);
468 p = strchr(opt_user_name,'%');
476 d_printf("\nInvalid option %c (%d)\n", (char)opt, opt);
477 net_usage(argc, argv);
481 lp_load(servicesf,True,False,False);
483 DEBUGLEVEL = debuglevel;
485 argv_new = (const char **)poptGetArgs(pc);
488 for (i=0; i<argc; i++) {
489 if (argv_new[i] == NULL) {
495 if (!opt_requester_name) {
496 static fstring myname;
498 opt_requester_name = myname;
501 if (!opt_user_name && getenv("LOGNAME")) {
502 opt_user_name = getenv("LOGNAME");
505 if (!opt_workgroup) {
506 opt_workgroup = lp_workgroup();
509 if (!opt_target_workgroup) {
510 opt_target_workgroup = lp_workgroup();
513 if (!*global_myname) {
516 fstrcpy(global_myname, myhostname());
517 p2 = strchr_m(global_myname, '.');
522 strupper(global_myname);
526 rc = net_run_function(argc_new-1, argv_new+1, net_func, net_usage);
528 DEBUG(2,("return code = %d\n", rc));