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;
34 /* Various pipe commands */
36 extern struct cmd_set lsarpc_commands[];
37 extern struct cmd_set samr_commands[];
38 extern struct cmd_set spoolss_commands[];
42 /* Fetch the SID for this domain */
44 void fetch_domain_sid(void)
48 uint32 result = 0, info_class = 5;
49 struct ntuser_creds creds;
51 static BOOL got_domain_sid;
53 if (got_domain_sid) return;
56 init_rpcclient_creds(&creds);
58 if (cli_lsa_initialise(&cli, server, &creds) == NULL) {
59 fprintf(stderr, "could not initialise lsa pipe\n");
63 if ((result = cli_lsa_open_policy(&cli, True,
64 SEC_RIGHTS_MAXIMUM_ALLOWED,
65 &pol) != NT_STATUS_NOPROBLEMO)) {
69 if ((result = cli_lsa_query_info_policy(&cli, &pol, info_class,
70 domain_name, &domain_sid))
71 != NT_STATUS_NOPROBLEMO) {
75 got_domain_sid = True;
77 cli_lsa_close(&cli, &pol);
78 cli_lsa_shutdown(&cli);
83 fprintf(stderr, "could not obtain sid for domain %s\n", workgroup);
85 if (result != NT_STATUS_NOPROBLEMO) {
86 fprintf(stderr, "error: %s\n", get_nt_error_msg(result));
92 /* Initialise client credentials for authenticated pipe access */
94 void init_rpcclient_creds(struct ntuser_creds *creds)
98 if (lp_encrypted_passwords()) {
99 pwd_make_lm_nt_16(&creds->pwd, password);
101 pwd_set_cleartext(&creds->pwd, password);
104 fstrcpy(creds->user_name, username);
105 fstrcpy(creds->domain, workgroup);
108 /* List to hold groups of commands */
110 static struct cmd_list {
111 struct cmd_list *prev, *next;
112 struct cmd_set *cmd_set;
115 static uint32 cmd_help(int argc, char **argv)
117 struct cmd_list *temp_list;
119 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
120 struct cmd_set *temp_set = temp_list->cmd_set;
122 while(temp_set->name) {
123 printf("%s\t%s\n", temp_set->name,
124 temp_set->description);
132 static uint32 cmd_debuglevel(int argc, char **argv)
135 printf("Usage: %s [debuglevel]\n", argv[0]);
136 return NT_STATUS_NOPROBLEMO;
140 DEBUGLEVEL = atoi(argv[1]);
143 printf("debuglevel is %d\n", DEBUGLEVEL);
145 return NT_STATUS_NOPROBLEMO;
148 static uint32 cmd_quit(int argc, char **argv)
153 /* Build in rpcclient commands */
155 static struct cmd_set rpcclient_commands[] = {
156 { "help", cmd_help, "Print list of commands" },
157 { "debuglevel", cmd_debuglevel, "Set debug level" },
158 { "quit", cmd_quit, "Exit program" },
159 { "?", cmd_help, "Print list of commands" },
164 void add_command_set(struct cmd_set *cmd_set)
166 struct cmd_list *entry;
168 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
169 DEBUG(0, ("out of memory\n"));
175 entry->cmd_set = cmd_set;
176 DLIST_ADD(cmd_list, entry);
179 static uint32 do_cmd(struct cmd_set *cmd_entry, char *cmd)
181 char *p = cmd, **argv = NULL;
186 next_token(&p, buf, " ", sizeof(buf));
188 /* Count number of arguments first time through the loop then
189 allocate memory and strdup them. */
192 while(next_token(NULL, buf, " ", sizeof(buf))) {
194 argv[argc] = strdup(buf);
202 /* Create argument list */
204 argv = (char **)malloc(sizeof(char *) * argc);
207 fprintf(stderr, "out of memoryx\n");
212 next_token(&p, buf, " ", sizeof(buf));
213 argv[0] = strdup(buf);
219 /* Call the function */
221 result = cmd_entry->fn(argc, argv);
225 for (i = 0; i < argc; i++) {
234 /* Process a command entered at the prompt or as part of -c */
236 static uint32 process_cmd(char *cmd)
238 struct cmd_list *temp_list;
244 if (!next_token(&p, buf, " ", sizeof(buf))) {
248 /* Search for matching commands */
250 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
251 struct cmd_set *temp_set = temp_list->cmd_set;
253 while(temp_set->name) {
254 if (strequal(buf, temp_set->name)) {
256 result = do_cmd(temp_set, cmd);
264 if (!found && buf[0]) {
265 printf("command not found: %s\n", buf);
270 printf("result was %s\n", get_nt_error_msg(result));
276 /* Print usage information */
278 static void usage(char *pname)
280 printf("Usage: %s server [options]\n", pname);
282 printf("\t-N don't ask for a password\n");
283 printf("\t-d debuglevel set the debuglevel\n");
284 printf("\t-h Print this help message.\n");
285 printf("\t-U username set the network username\n");
286 printf("\t-W workgroup set the workgroup name\n");
287 printf("\t-c command string execute semicolon separated cmds\n");
293 int main(int argc, char *argv[])
297 struct in_addr dest_ip;
298 extern pstring global_myname;
299 BOOL got_pass = False;
300 BOOL have_ip = False;
302 pstring cmdstr = "", servicesf = CONFIGFILE;
308 setup_logging(argv[0], True);
310 #ifdef HAVE_LIBREADLINE
311 /* Allow conditional parsing of the ~/.inputrc file. */
312 rl_readline_name = "rpcclient";
317 /* Load smb.conf file */
319 if (!lp_load(servicesf,True,False,False)) {
320 fprintf(stderr, "Can't load %s\n", servicesf);
323 codepage_initialise(lp_client_code_page());
324 charset_initialise();
329 get_myname((*global_myname)?NULL:global_myname);
330 strupper(global_myname);
339 pstrcpy(server, argv[1]);
344 while ((opt = getopt(argc, argv, "s:Nd:I:U:W:c:")) != EOF) {
347 pstrcpy(servicesf, optarg);
353 DEBUGLEVEL = atoi(optarg);
356 dest_ip = *interpret_addr2(optarg);
361 pstrcpy(username,optarg);
362 if ((lp=strchr(username,'%'))) {
364 pstrcpy(password,lp+1);
366 memset(strchr(optarg,'%')+1,'X',strlen(password));
371 pstrcpy(workgroup, optarg);
374 pstrcpy(cmdstr, optarg);
384 /* Load command lists */
386 add_command_set(rpcclient_commands);
387 add_command_set(lsarpc_commands);
388 add_command_set(samr_commands);
389 add_command_set(spoolss_commands);
391 /* Do anything specified with -c */
398 while(next_token(&p, cmd, ";", sizeof(pstring))) {
399 result = process_cmd(cmd);
405 /* Loop around accepting commands */
413 slprintf(prompt, sizeof(prompt) - 1, "rpcclient> ");
416 cmd = readline(prompt);
418 printf("%s", prompt);
420 if (!fgets(cmd, sizeof(cmd) - 1, stdin)) {
424 cmd[strlen(cmd) - 1] = '\0';
426 result = process_cmd(cmd);