2 Unix SMB/Netbios implementation.
6 Copyright (C) Tim Potter 2000
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL;
32 /* Various pipe commands */
34 extern struct cmd_set lsarpc_commands[];
35 extern struct cmd_set samr_commands[];
36 extern struct cmd_set spoolss_commands[];
38 /* Initialise client credentials for authenticated pipe access */
40 void init_rpcclient_creds(struct ntuser_creds *creds)
44 if (lp_encrypted_passwords()) {
45 pwd_make_lm_nt_16(&creds->pwd, password);
47 pwd_set_cleartext(&creds->pwd, password);
50 fstrcpy(creds->user_name, username);
51 fstrcpy(creds->domain, workgroup);
54 /* List to hold groups of commands */
56 static struct cmd_list {
57 struct cmd_list *prev, *next;
58 struct cmd_set *cmd_set;
61 static uint32 cmd_help(int argc, char **argv)
63 struct cmd_list *temp_list;
65 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
66 struct cmd_set *temp_set = temp_list->cmd_set;
68 while(temp_set->name) {
69 printf("%s\t%s\n", temp_set->name,
70 temp_set->description);
78 static uint32 cmd_debuglevel(int argc, char **argv)
81 printf("Usage: %s [debuglevel]\n", argv[0]);
82 return NT_STATUS_NOPROBLEMO;
86 DEBUGLEVEL = atoi(argv[1]);
89 printf("debuglevel is %d\n", DEBUGLEVEL);
91 return NT_STATUS_NOPROBLEMO;
94 /* Build in rpcclient commands */
96 static struct cmd_set rpcclient_commands[] = {
97 { "help", cmd_help, "Print list of commands" },
98 { "debuglevel", cmd_debuglevel, "Set debug level" },
99 { "?", cmd_help, "Print list of commands" },
104 void add_command_set(struct cmd_set *cmd_set)
106 struct cmd_list *entry;
108 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
109 DEBUG(0, ("out of memory\n"));
115 entry->cmd_set = cmd_set;
116 DLIST_ADD(cmd_list, entry);
119 static uint32 do_cmd(struct cmd_set *cmd_entry, char *cmd)
121 char *p = cmd, **argv = NULL;
126 next_token(&p, buf, " ", sizeof(buf));
128 /* Count number of arguments first time through the loop then
129 allocate memory and strdup them. */
132 while(next_token(NULL, buf, " ", sizeof(buf))) {
134 argv[argc] = strdup(buf);
142 /* Create argument list */
144 argv = (char **)malloc(sizeof(char *) * argc);
146 fprintf(stderr, "out of memoryx\n");
152 next_token(&p, buf, " ", sizeof(buf));
153 argv[0] = strdup(buf);
158 /* Call the function */
160 result = cmd_entry->fn(argc, argv);
164 for (i = 0; i < argc; i++) {
173 /* Process a command entered at the prompt or as part of -c */
175 static uint32 process_cmd(char *cmd)
177 struct cmd_list *temp_list;
183 if (!next_token(&p, buf, " ", sizeof(buf))) {
187 /* Search for matching commands */
189 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
190 struct cmd_set *temp_set = temp_list->cmd_set;
192 while(temp_set->name) {
193 if (strequal(buf, temp_set->name)) {
195 result = do_cmd(temp_set, cmd);
204 printf("command not found: %s\n", buf);
209 printf("result was %s\n", get_nt_error_msg(result));
216 /* Print usage information */
218 static void usage(char *pname)
220 printf("Usage: %s server [options]\n", pname);
222 printf("\t-N don't ask for a password\n");
223 printf("\t-d debuglevel set the debuglevel\n");
224 printf("\t-h Print this help message.\n");
225 printf("\t-U username set the network username\n");
226 printf("\t-W workgroup set the workgroup name\n");
227 printf("\t-c command string execute semicolon separated cmds\n");
233 int main(int argc, char *argv[])
237 struct in_addr dest_ip;
238 BOOL got_pass = False;
239 BOOL have_ip = False;
241 pstring cmdstr, servicesf = CONFIGFILE;
247 setup_logging(argv[0], True);
249 #ifdef HAVE_LIBREADLINE
250 /* Allow conditional parsing of the ~/.inputrc file. */
251 rl_readline_name = "smbclient";
257 charset_initialise();
266 pstrcpy(server, argv[1]);
271 while ((opt = getopt(argc, argv, "s:Nd:I:U:W:c:")) != EOF) {
274 pstrcpy(servicesf, optarg);
280 DEBUGLEVEL = atoi(optarg);
283 dest_ip = *interpret_addr2(optarg);
288 pstrcpy(username,optarg);
289 if ((lp=strchr(username,'%'))) {
291 pstrcpy(password,lp+1);
293 memset(strchr(optarg,'%')+1,'X',strlen(password));
298 pstrcpy(workgroup, optarg);
301 pstrcpy(cmdstr, optarg);
311 /* Load smb.conf file */
313 if (!lp_load(servicesf,True,False,False)) {
314 fprintf(stderr, "Can't load %s\n", servicesf);
317 codepage_initialise(lp_client_code_page());
320 /* Load command lists */
322 add_command_set(rpcclient_commands);
323 add_command_set(lsarpc_commands);
324 add_command_set(samr_commands);
325 add_command_set(spoolss_commands);
327 /* Do anything specified with -c */
334 while(next_token(&p, cmd, ";", sizeof(pstring))) {
335 result = process_cmd(cmd);
341 /* Loop around accepting commands */
347 slprintf(prompt, sizeof(prompt) - 1, "rpcclient> ");
350 cmd = readline(prompt);
352 printf("%s", prompt);
354 if (!fgets(cmd, sizeof(cmd) - 1, stdin)) {
358 cmd[strlen(cmd) - 1] = '\0';
360 result = process_cmd(cmd);