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 /* Yes, these buggers are globals.... */
60 char *opt_requester_name = NULL;
61 char *opt_host = NULL;
62 char *opt_password = NULL;
63 char *opt_user_name = NULL;
64 char *opt_workgroup = NULL;
65 int opt_long_list_entries = 0;
67 int opt_maxusers = -1;
68 char *opt_comment = "";
71 char *opt_target_workgroup = NULL;
73 static BOOL got_pass = False;
74 BOOL opt_have_ip = False;
75 struct in_addr opt_dest_ip;
77 extern pstring global_myname;
80 run a function from a function table. If not found then
81 call the specified usage function
83 int net_run_function(int argc, const char **argv, struct functable *table,
84 int (*usage_fn)(int argc, const char **argv))
89 d_printf("\nUsage: \n");
90 return usage_fn(argc, argv);
92 for (i=0; table[i].funcname; i++) {
93 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
94 return table[i].fn(argc-1, argv+1);
96 d_printf("No command: %s\n", argv[0]);
97 return usage_fn(argc, argv);
101 /****************************************************************************
102 connect to \\server\ipc$
103 ****************************************************************************/
104 static struct cli_state *connect_to_ipc(struct in_addr *server_ip, const char *server_name)
110 char *pass = getpass("Password:");
112 opt_password = strdup(pass);
116 nt_status = cli_full_connection(&c, opt_requester_name, server_name,
119 opt_user_name, opt_workgroup,
120 opt_password, strlen(opt_password));
122 if (NT_STATUS_IS_OK(nt_status)) {
125 DEBUG(0,("Cannot connect to server. Error was %s\n",
126 get_nt_error_msg(nt_status)));
131 /****************************************************************************
132 connect to \\server\ipc$ anonymously
133 ****************************************************************************/
134 static struct cli_state *connect_to_ipc_anonymous(struct in_addr *server_ip, const char *server_name)
139 nt_status = cli_full_connection(&c, opt_requester_name, server_name,
145 if (NT_STATUS_IS_OK(nt_status)) {
148 DEBUG(0,("Cannot connect to server (anonymously). Error was %s\n", get_nt_error_msg(nt_status)));
153 static BOOL net_find_server(unsigned flags, struct in_addr *server_ip, char **server_name)
157 *server_name = strdup(opt_host);
161 *server_ip = opt_dest_ip;
163 *server_name = strdup(inet_ntoa(opt_dest_ip));
165 } else if (*server_name) {
166 /* resolve the IP address */
167 if (!resolve_name(*server_name, server_ip, 0x20)) {
168 DEBUG(1,("Unable to resolve server name\n"));
171 } else if (flags & NET_FLAGS_PDC) {
172 struct in_addr *ip_list;
174 if (get_dc_list(True /* PDC only*/, opt_target_workgroup, &ip_list, &addr_count)) {
176 if (addr_count < 1) {
180 *server_ip = *ip_list;
182 if (is_zero_ip(*server_ip))
185 if (!lookup_dc_name(global_myname, opt_target_workgroup, server_ip, dc_name))
188 *server_name = strdup(dc_name);
191 } else if (flags & NET_FLAGS_DMB) {
192 struct in_addr msbrow_ip;
193 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
194 if (!resolve_name(opt_target_workgroup, &msbrow_ip, 0x1B)) {
195 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
198 *server_ip = msbrow_ip;
200 *server_name = strdup(inet_ntoa(opt_dest_ip));
201 } else if (flags & NET_FLAGS_MASTER) {
202 struct in_addr brow_ips;
203 if (!resolve_name(opt_target_workgroup, &brow_ips, 0x1D)) {
204 /* go looking for workgroups */
205 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
208 *server_ip = brow_ips;
210 *server_name = strdup(inet_ntoa(opt_dest_ip));
211 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
212 extern struct in_addr loopback_ip;
213 *server_ip = loopback_ip;
214 *server_name = strdup("127.0.0.1");
217 if (!server_name || !*server_name) {
218 DEBUG(1,("no server to connect to\n"));
225 struct cli_state *net_make_ipc_connection(unsigned flags)
227 char *server_name = NULL;
228 struct in_addr server_ip;
229 struct cli_state *cli;
231 if (!net_find_server(flags, &server_ip, &server_name)) {
232 d_printf("\nUnable to find a suitable server\n");
236 if (flags & NET_FLAGS_ANONYMOUS) {
237 cli = connect_to_ipc_anonymous(&server_ip, server_name);
239 cli = connect_to_ipc(&server_ip, server_name);
241 SAFE_FREE(server_name);
246 static int net_usage(int argc, const char **argv)
248 d_printf(" net ads [command]\tto run ADS commands\n"\
249 " net rap [command]\tto run RAP (pre-RPC) commands\n"\
250 " net rpc [command]\tto run RPC commands\n"\
252 "\nType \"net help <option>\" to get more information on that option\n");
256 static int help_usage(int argc, const char **argv)
260 "Usage: net help <function>\n"\
262 "Valid functions are:\n"\
263 " RPC RAP ADS FILE SHARE SESSION SERVER DOMAIN PRINTQ USER GROUP VALIDATE\n"\
264 " GROUPMEMBER ADMIN SERVICE PASSWORD TIME\n");
269 handle "net help *" subcommands
271 static int net_help(int argc, const char **argv)
273 struct functable func[] = {
274 {"ADS", net_ads_usage},
275 {"RAP", net_rap_usage},
276 {"RPC", net_rpc_usage},
278 {"FILE", net_rap_file_usage},
279 {"SHARE", net_rap_share_usage},
280 {"SESSION", net_rap_session_usage},
281 {"SERVER", net_rap_server_usage},
282 {"DOMAIN", net_rap_domain_usage},
283 {"PRINTQ", net_rap_printq_usage},
284 {"USER", net_rap_user_usage},
285 {"GROUP", net_rap_group_usage},
286 {"VALIDATE", net_rap_validate_usage},
287 {"GROUPMEMBER", net_rap_groupmember_usage},
288 {"ADMIN", net_rap_admin_usage},
289 {"SERVICE", net_rap_service_usage},
290 {"PASSWORD", net_rap_password_usage},
291 {"TIME", net_time_usage},
293 {"HELP", help_usage},
296 return net_run_function(argc, argv, func, help_usage);
299 /* main function table */
300 static struct functable net_func[] = {
305 /* eventually these should auto-choose the transport ... */
306 {"FILE", net_rap_file},
307 {"SHARE", net_rap_share},
308 {"SESSION", net_rap_session},
309 {"SERVER", net_rap_server},
310 {"DOMAIN", net_rap_domain},
311 {"PRINTQ", net_rap_printq},
312 {"USER", net_rap_user},
313 {"GROUP", net_rap_group},
314 {"VALIDATE", net_rap_validate},
315 {"GROUPMEMBER", net_rap_groupmember},
316 {"ADMIN", net_rap_admin},
317 {"SERVICE", net_rap_service},
318 {"PASSWORD", net_rap_password},
326 /****************************************************************************
328 ****************************************************************************/
329 int main(int argc, const char **argv)
335 const char ** argv_new;
337 static char *servicesf = dyn_CONFIGFILE;
338 static int debuglevel;
340 struct poptOption long_options[] = {
341 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
342 {"workgroup", 'w', POPT_ARG_STRING, &opt_target_workgroup},
343 {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
344 {"user", 'U', POPT_ARG_STRING, &opt_user_name, 'U'},
345 {"ipaddress", 'I', POPT_ARG_STRING, 0, 'I'},
346 {"port", 'p', POPT_ARG_INT, &opt_port},
347 {"myname", 'n', POPT_ARG_STRING, &opt_requester_name},
348 {"conf", 's', POPT_ARG_STRING, &servicesf},
349 {"debug", 'd', POPT_ARG_INT, &debuglevel, 'd'},
350 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd'},
351 {"server", 'S', POPT_ARG_STRING, &opt_host},
352 {"comment", 'C', POPT_ARG_STRING, &opt_comment},
353 {"maxusers", 'M', POPT_ARG_INT, &opt_maxusers},
354 {"flags", 'F', POPT_ARG_INT, &opt_flags},
355 {"jobid", 'j', POPT_ARG_INT, &opt_jobid},
356 {"long", 'l', POPT_ARG_NONE, &opt_long_list_entries},
361 zero_ip(&opt_dest_ip);
365 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
366 POPT_CONTEXT_KEEP_FIRST);
368 while((opt = poptGetNextOpt(pc)) != -1) {
371 net_usage(argc, argv);
375 opt_dest_ip = *interpret_addr2(poptGetOptArg(pc));
376 if (is_zero_ip(opt_dest_ip))
377 d_printf("\nInvalid ip address specified\n");
382 opt_user_name = strdup(opt_user_name);
383 p = strchr(opt_user_name,'%');
391 d_printf("\nInvalid option %c (%d)\n", (char)opt, opt);
392 net_usage(argc, argv);
396 lp_load(servicesf,True,False,False);
398 DEBUGLEVEL = debuglevel;
400 argv_new = (const char **)poptGetArgs(pc);
403 for (i=0; i<argc; i++) {
404 if (argv_new[i] == NULL) {
410 if (!opt_requester_name) {
411 static fstring myname;
413 opt_requester_name = myname;
416 if (!opt_user_name && getenv("LOGNAME")) {
417 opt_user_name = getenv("LOGNAME");
420 if (!opt_workgroup) {
421 opt_workgroup = lp_workgroup();
424 if (!opt_target_workgroup) {
425 opt_target_workgroup = lp_workgroup();
428 if (!*global_myname) {
431 fstrcpy(global_myname, myhostname());
432 p2 = strchr_m(global_myname, '.');
437 strupper(global_myname);
441 rc = net_run_function(argc_new-1, argv_new+1, net_func, net_usage);
443 DEBUG(2,("return code = %d\n", rc));