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 PASSWORD_PROMPT "Password: "
53 #define YES_STRING "Yes"
54 #define NO_STRING "No"
56 /************************************************************************************/
57 /* end of internationalization section */
58 /************************************************************************************/
60 extern int optind, opterr, optopt;
62 /* Yes, these buggers are globals.... */
63 char *opt_requester_name = NULL;
64 char *opt_host = NULL;
65 char *opt_password = NULL;
66 char *opt_user_name = NULL;
67 char *opt_workgroup = NULL;
68 int opt_long_list_entries = 0;
70 int opt_maxusers = -1;
71 char *opt_comment = "";
74 char *opt_target_workgroup = NULL;
76 static BOOL got_pass = False;
77 static BOOL have_ip = False;
78 static struct in_addr dest_ip;
81 run a function from a function table. If not found then
82 call the specified usage function
84 int net_run_function(int argc, const char **argv, struct functable *table,
85 int (*usage_fn)(int argc, const char **argv))
90 d_printf("\nUsage: \n");
91 return usage_fn(argc, argv);
93 for (i=0; table[i].funcname; i++) {
94 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
95 return table[i].fn(argc-1, argv+1);
97 d_printf("No command: %s\n", argv[0]);
98 return usage_fn(argc, argv);
102 /****************************************************************************
103 connect to \\server\ipc$
104 ****************************************************************************/
105 static struct cli_state *connect_to_ipc(struct in_addr *server_ip, const char *server_name)
108 struct nmb_name called, calling;
112 make_nmb_name(&calling, opt_requester_name, 0x0);
113 make_nmb_name(&called , server_name, 0x20);
118 DEBUG(3,("Connecting to host=%s share=%s\n\n",
119 server_name, "IPC$"));
121 /* have to open a new connection */
122 if (!(c=cli_initialise(NULL)) || cli_set_port(c, opt_port) != opt_port ||
123 !cli_connect(c, server_name, &ip)) {
124 DEBUG(1,("Connection to %s failed\n", server_name));
128 c->protocol = PROTOCOL_NT1;
130 if (!cli_session_request(c, &calling, &called)) {
132 DEBUG(1,("session request to %s failed (%s)\n",
133 called.name, cli_errstr(c)));
135 if ((p=strchr(called.name, '.'))) {
139 if (strcmp(called.name, "*SMBSERVER")) {
140 make_nmb_name(&called , "*SMBSERVER", 0x20);
146 DEBUG(4,(" session request ok\n"));
148 if (!cli_negprot(c)) {
149 DEBUG(1,("protocol negotiation failed\n"));
155 char *pass = getpass(PASSWORD_PROMPT);
157 opt_password = strdup(pass);
161 if (!cli_session_setup(c, opt_user_name,
162 opt_password, strlen(opt_password),
163 opt_password, strlen(opt_password),
165 /* try again with a null username */
166 if (!cli_session_setup(c, "", "", 0, "", 0, opt_workgroup)) {
167 DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
171 DEBUG(3,("Anonymous login successful\n"));
174 snprintf(sharename, sizeof(sharename), "\\\\%s\\IPC$", server_name);
176 DEBUG(4,(" session setup ok\n"));
177 if (!cli_send_tconX(c, sharename, "?????",
178 opt_password, strlen(opt_password)+1)) {
179 DEBUG(1,("tree connect failed: %s\n", cli_errstr(c)));
184 DEBUG(4,(" tconx ok\n"));
189 struct cli_state *net_make_ipc_connection(unsigned flags)
191 char *server_name = opt_host;
192 struct in_addr server_ip;
193 struct cli_state *cli;
197 server_name = strdup(inet_ntoa(dest_ip));
199 } else if (server_name) {
200 /* resolve the IP address */
201 if (!resolve_name(server_name, &server_ip, 0x20)) {
202 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
205 } else if (flags & NET_FLAGS_DMB) {
206 struct in_addr *ip_list;
208 char *our_workgroup = lp_workgroup();
209 struct in_addr msbrow_ip;
210 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
211 if (!get_dmb_list(&ip_list,&addr_count)){
212 DEBUG(1,("Unable to resolve global master browser via name lookup"));
213 if (!resolve_name(our_workgroup, &msbrow_ip, 0x1D)) {
214 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
217 server_ip = msbrow_ip;
220 server_ip = *ip_list;
222 } else if (flags & NET_FLAGS_MASTER) {
223 char *temp_workgroup = lp_workgroup();
224 char our_workgroup[16];
225 struct in_addr brow_ips;
227 /* find target server based on workgroup or domain */
228 if((temp_workgroup == 0) || (temp_workgroup[0] == 0))
229 temp_workgroup = lp_workgroup(); /* by default enum our local workgroup or domain */
231 safe_strcpy(our_workgroup, temp_workgroup,15);
233 if (!resolve_name(our_workgroup, &brow_ips, 0x1D)) {
234 /* go looking for workgroups */
235 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
238 server_ip = brow_ips;
240 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
241 extern struct in_addr loopback_ip;
242 server_ip = loopback_ip;
243 server_name = "127.0.0.1";
247 DEBUG(1,("no server to connect to\n"));
251 cli = connect_to_ipc(&server_ip, server_name);
253 d_printf("\nUnable to connect to target server\n");
260 static int net_usage(int argc, const char **argv)
262 d_printf(" net ads [command]\tto run ADS commands\n"\
263 " net rap [command]\tto run RAP (pre-RPC) commands\n"\
264 " net rpc [command]\tto run RPC commands\n"\
266 "\nType \"net help <option>\" to get more information on that option\n");
270 static int help_usage(int argc, const char **argv)
273 "Usage: net help <function>\n"\
275 "Valid functions are:\n"\
281 handle "net help *" subcommands
283 static int net_help(int argc, const char **argv)
285 struct functable func[] = {
286 {"ADS", net_ads_usage},
287 {"RAP", net_rap_usage},
288 {"RPC", net_rpc_usage},
289 {"HELP", help_usage},
292 return net_run_function(argc, argv, func, help_usage);
295 /* main function table */
296 static struct functable net_func[] = {
305 /****************************************************************************
307 ****************************************************************************/
308 int main(int argc, const char **argv)
314 const char ** argv_new;
316 static char *servicesf = dyn_CONFIGFILE;
317 extern pstring global_myname;
318 static int debuglevel;
320 struct poptOption long_options[] = {
321 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
322 {"workgroup", 'w', POPT_ARG_STRING, &opt_target_workgroup},
323 {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
324 {"user", 'U', POPT_ARG_STRING, &opt_user_name, 'U'},
325 {"ipaddress", 'I', POPT_ARG_STRING, 0, 'I'},
326 {"port", 'p', POPT_ARG_INT, &opt_port},
327 {"myname", 'n', POPT_ARG_STRING, &opt_requester_name},
328 {"conf", 's', POPT_ARG_STRING, &servicesf},
329 {"debug", 'd', POPT_ARG_INT, &debuglevel, 'd'},
330 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd'},
331 {"server", 'S', POPT_ARG_STRING, &opt_host},
332 {"comment", 'C', POPT_ARG_STRING, &opt_comment},
333 {"maxusers", 'M', POPT_ARG_INT, &opt_maxusers},
334 {"flags", 'F', POPT_ARG_INT, &opt_flags},
335 {"jobid", 'j', POPT_ARG_INT, &opt_jobid},
336 {"long", 'l', POPT_ARG_NONE, &opt_long_list_entries},
345 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
346 POPT_CONTEXT_KEEP_FIRST);
348 while((opt = poptGetNextOpt(pc)) != -1) {
351 net_usage(argc, argv);
355 dest_ip = *interpret_addr2(poptGetOptArg(pc));
356 if (is_zero_ip(dest_ip))
357 d_printf("\nInvalid ip address specified\n");
362 opt_user_name = strdup(opt_user_name);
363 p = strchr(opt_user_name,'%');
371 d_printf("\nInvalid option %c (%d)\n", (char)opt, opt);
372 net_usage(argc, argv);
376 DEBUGLEVEL = debuglevel;
378 lp_load(servicesf,True,False,False);
380 argv_new = (const char **)poptGetArgs(pc);
383 for (i=0; i<argc; i++) {
384 if (argv_new[i] == NULL) {
390 if (!opt_requester_name) {
391 static fstring myname;
393 opt_requester_name = myname;
396 if (!opt_user_name && getenv("LOGNAME")) {
397 opt_user_name = getenv("LOGNAME");
400 if (!opt_workgroup) {
401 opt_workgroup = lp_workgroup();
404 if (!*global_myname) {
407 fstrcpy(global_myname, myhostname());
408 p2 = strchr_m(global_myname, '.');
415 rc = net_run_function(argc_new-1, argv_new+1, net_func, net_usage);
417 DEBUG(2,("return code = %d\n", rc));