source: vendor/3.6.23/source3/client/client.c

Last change on this file was 860, checked in by Silvan Scherrer, 11 years ago

Samba 3.6: updated vendor to latest version

File size: 131.1 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 SMB client
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Simo Sorce 2001-2002
6 Copyright (C) Jelmer Vernooij 2003
7 Copyright (C) Gerald (Jerry) Carter 2004
8 Copyright (C) Jeremy Allison 1994-2007
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "includes.h"
25#include "system/filesys.h"
26#include "popt_common.h"
27#include "rpc_client/cli_pipe.h"
28#include "client/client_proto.h"
29#include "../librpc/gen_ndr/ndr_srvsvc_c.h"
30#include "../lib/util/select.h"
31#include "system/readline.h"
32#include "../libcli/smbreadline/smbreadline.h"
33#include "../libcli/security/security.h"
34#include "system/select.h"
35#include "libsmb/libsmb.h"
36#include "libsmb/clirap.h"
37#include "trans2.h"
38#include "libsmb/nmblib.h"
39
40#ifndef REGISTER
41#define REGISTER 0
42#endif
43
44extern int do_smb_browse(void); /* mDNS browsing */
45
46extern bool override_logfile;
47extern char tar_type;
48
49static int port = 0;
50static char *service;
51static char *desthost;
52static char *calling_name;
53static bool grepable = false;
54static char *cmdstr = NULL;
55const char *cmd_ptr = NULL;
56
57static int io_bufsize = 524288;
58
59static int name_type = 0x20;
60static int max_protocol = PROTOCOL_NT1;
61
62static int process_tok(char *tok);
63static int cmd_help(void);
64
65#define CREATE_ACCESS_READ READ_CONTROL_ACCESS
66
67/* 30 second timeout on most commands */
68#define CLIENT_TIMEOUT (30*1000)
69#define SHORT_TIMEOUT (5*1000)
70
71/* value for unused fid field in trans2 secondary request */
72#define FID_UNUSED (0xFFFF)
73
74time_t newer_than = 0;
75static int archive_level = 0;
76
77static bool translation = false;
78static bool have_ip;
79
80/* clitar bits insert */
81extern int blocksize;
82extern bool tar_inc;
83extern bool tar_reset;
84/* clitar bits end */
85
86static bool prompt = true;
87
88static bool recurse = false;
89static bool showacls = false;
90bool lowercase = false;
91
92static struct sockaddr_storage dest_ss;
93static char dest_ss_str[INET6_ADDRSTRLEN];
94
95#define SEPARATORS " \t\n\r"
96
97static bool abort_mget = true;
98
99/* timing globals */
100uint64_t get_total_size = 0;
101unsigned int get_total_time_ms = 0;
102static uint64_t put_total_size = 0;
103static unsigned int put_total_time_ms = 0;
104
105/* totals globals */
106static double dir_total;
107
108/* encrypted state. */
109static bool smb_encrypt;
110
111/* root cli_state connection */
112
113struct cli_state *cli;
114
115static char CLI_DIRSEP_CHAR = '\\';
116static char CLI_DIRSEP_STR[] = { '\\', '\0' };
117
118/* Authentication for client connections. */
119struct user_auth_info *auth_info;
120
121/* Accessor functions for directory paths. */
122static char *fileselection;
123static const char *client_get_fileselection(void)
124{
125 if (fileselection) {
126 return fileselection;
127 }
128 return "";
129}
130
131static const char *client_set_fileselection(const char *new_fs)
132{
133 SAFE_FREE(fileselection);
134 if (new_fs) {
135 fileselection = SMB_STRDUP(new_fs);
136 }
137 return client_get_fileselection();
138}
139
140static char *cwd;
141static const char *client_get_cwd(void)
142{
143 if (cwd) {
144 return cwd;
145 }
146 return CLI_DIRSEP_STR;
147}
148
149static const char *client_set_cwd(const char *new_cwd)
150{
151 SAFE_FREE(cwd);
152 if (new_cwd) {
153 cwd = SMB_STRDUP(new_cwd);
154 }
155 return client_get_cwd();
156}
157
158static char *cur_dir;
159const char *client_get_cur_dir(void)
160{
161 if (cur_dir) {
162 return cur_dir;
163 }
164 return CLI_DIRSEP_STR;
165}
166
167const char *client_set_cur_dir(const char *newdir)
168{
169 SAFE_FREE(cur_dir);
170 if (newdir) {
171 cur_dir = SMB_STRDUP(newdir);
172 }
173 return client_get_cur_dir();
174}
175
176/****************************************************************************
177 Put up a yes/no prompt.
178****************************************************************************/
179
180static bool yesno(const char *p)
181{
182 char ans[20];
183 printf("%s",p);
184
185 if (!fgets(ans,sizeof(ans)-1,stdin))
186 return(False);
187
188 if (*ans == 'y' || *ans == 'Y')
189 return(True);
190
191 return(False);
192}
193
194/****************************************************************************
195 Write to a local file with CR/LF->LF translation if appropriate. Return the
196 number taken from the buffer. This may not equal the number written.
197****************************************************************************/
198
199static int writefile(int f, char *b, int n)
200{
201 int i;
202
203 if (!translation) {
204 return write(f,b,n);
205 }
206
207 i = 0;
208 while (i < n) {
209 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
210 b++;i++;
211 }
212 if (write(f, b, 1) != 1) {
213 break;
214 }
215 b++;
216 i++;
217 }
218
219 return(i);
220}
221
222/****************************************************************************
223 Read from a file with LF->CR/LF translation if appropriate. Return the
224 number read. read approx n bytes.
225****************************************************************************/
226
227static int readfile(uint8_t *b, int n, XFILE *f)
228{
229 int i;
230 int c;
231
232 if (!translation)
233 return x_fread(b,1,n,f);
234
235 i = 0;
236 while (i < (n - 1) && (i < BUFFER_SIZE)) {
237 if ((c = x_getc(f)) == EOF) {
238 break;
239 }
240
241 if (c == '\n') { /* change all LFs to CR/LF */
242 b[i++] = '\r';
243 }
244
245 b[i++] = c;
246 }
247
248 return(i);
249}
250
251struct push_state {
252 XFILE *f;
253 SMB_OFF_T nread;
254};
255
256static size_t push_source(uint8_t *buf, size_t n, void *priv)
257{
258 struct push_state *state = (struct push_state *)priv;
259 int result;
260
261 if (x_feof(state->f)) {
262 return 0;
263 }
264
265 result = readfile(buf, n, state->f);
266 state->nread += result;
267 return result;
268}
269
270/****************************************************************************
271 Send a message.
272****************************************************************************/
273
274static void send_message(const char *username)
275{
276 char buf[1600];
277 NTSTATUS status;
278 int i;
279
280 d_printf("Type your message, ending it with a Control-D\n");
281
282 i = 0;
283 while (i<sizeof(buf)-2) {
284 int c = fgetc(stdin);
285 if (c == EOF) {
286 break;
287 }
288 if (c == '\n') {
289 buf[i++] = '\r';
290 }
291 buf[i++] = c;
292 }
293 buf[i] = '\0';
294
295 status = cli_message(cli, desthost, username, buf);
296 if (!NT_STATUS_IS_OK(status)) {
297 d_fprintf(stderr, "cli_message returned %s\n",
298 nt_errstr(status));
299 }
300}
301
302/****************************************************************************
303 Check the space on a device.
304****************************************************************************/
305
306static int do_dskattr(void)
307{
308 int total, bsize, avail;
309 struct cli_state *targetcli = NULL;
310 char *targetpath = NULL;
311 TALLOC_CTX *ctx = talloc_tos();
312 NTSTATUS status;
313
314 if ( !cli_resolve_path(ctx, "", auth_info, cli, client_get_cur_dir(), &targetcli, &targetpath)) {
315 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
316 return 1;
317 }
318
319 status = cli_dskattr(targetcli, &bsize, &total, &avail);
320 if (!NT_STATUS_IS_OK(status)) {
321 d_printf("Error in dskattr: %s\n", nt_errstr(status));
322 return 1;
323 }
324
325 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
326 total, bsize, avail);
327
328 return 0;
329}
330
331/****************************************************************************
332 Show cd/pwd.
333****************************************************************************/
334
335static int cmd_pwd(void)
336{
337 d_printf("Current directory is %s",service);
338 d_printf("%s\n",client_get_cur_dir());
339 return 0;
340}
341
342/****************************************************************************
343 Ensure name has correct directory separators.
344****************************************************************************/
345
346static void normalize_name(char *newdir)
347{
348 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
349 string_replace(newdir,'/','\\');
350 }
351}
352
353/****************************************************************************
354 Change directory - inner section.
355****************************************************************************/
356
357static int do_cd(const char *new_dir)
358{
359 char *newdir = NULL;
360 char *saved_dir = NULL;
361 char *new_cd = NULL;
362 char *targetpath = NULL;
363 struct cli_state *targetcli = NULL;
364 SMB_STRUCT_STAT sbuf;
365 uint32 attributes;
366 int ret = 1;
367 TALLOC_CTX *ctx = talloc_stackframe();
368
369 newdir = talloc_strdup(ctx, new_dir);
370 if (!newdir) {
371 TALLOC_FREE(ctx);
372 return 1;
373 }
374
375 normalize_name(newdir);
376
377 /* Save the current directory in case the new directory is invalid */
378
379 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
380 if (!saved_dir) {
381 TALLOC_FREE(ctx);
382 return 1;
383 }
384
385 if (*newdir == CLI_DIRSEP_CHAR) {
386 client_set_cur_dir(newdir);
387 new_cd = newdir;
388 } else {
389 new_cd = talloc_asprintf(ctx, "%s%s",
390 client_get_cur_dir(),
391 newdir);
392 if (!new_cd) {
393 goto out;
394 }
395 }
396
397 /* Ensure cur_dir ends in a DIRSEP */
398 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
399 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
400 if (!new_cd) {
401 goto out;
402 }
403 }
404 client_set_cur_dir(new_cd);
405
406 new_cd = clean_name(ctx, new_cd);
407 client_set_cur_dir(new_cd);
408
409 if ( !cli_resolve_path(ctx, "", auth_info, cli, new_cd, &targetcli, &targetpath)) {
410 d_printf("cd %s: %s\n", new_cd, cli_errstr(cli));
411 client_set_cur_dir(saved_dir);
412 goto out;
413 }
414
415 if (strequal(targetpath,CLI_DIRSEP_STR )) {
416 TALLOC_FREE(ctx);
417 return 0;
418 }
419
420 /* Use a trans2_qpathinfo to test directories for modern servers.
421 Except Win9x doesn't support the qpathinfo_basic() call..... */
422
423 if (targetcli->protocol > PROTOCOL_LANMAN2 && !targetcli->win95) {
424 NTSTATUS status;
425
426 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
427 &attributes);
428 if (!NT_STATUS_IS_OK(status)) {
429 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
430 client_set_cur_dir(saved_dir);
431 goto out;
432 }
433
434 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
435 d_printf("cd %s: not a directory\n", new_cd);
436 client_set_cur_dir(saved_dir);
437 goto out;
438 }
439 } else {
440 NTSTATUS status;
441
442 targetpath = talloc_asprintf(ctx,
443 "%s%s",
444 targetpath,
445 CLI_DIRSEP_STR );
446 if (!targetpath) {
447 client_set_cur_dir(saved_dir);
448 goto out;
449 }
450 targetpath = clean_name(ctx, targetpath);
451 if (!targetpath) {
452 client_set_cur_dir(saved_dir);
453 goto out;
454 }
455
456 status = cli_chkpath(targetcli, targetpath);
457 if (!NT_STATUS_IS_OK(status)) {
458 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
459 client_set_cur_dir(saved_dir);
460 goto out;
461 }
462 }
463
464 ret = 0;
465
466out:
467
468 TALLOC_FREE(ctx);
469 return ret;
470}
471
472/****************************************************************************
473 Change directory.
474****************************************************************************/
475
476static int cmd_cd(void)
477{
478 char *buf = NULL;
479 int rc = 0;
480
481 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
482 rc = do_cd(buf);
483 } else {
484 d_printf("Current directory is %s\n",client_get_cur_dir());
485 }
486
487 return rc;
488}
489
490/****************************************************************************
491 Change directory.
492****************************************************************************/
493
494static int cmd_cd_oneup(void)
495{
496 return do_cd("..");
497}
498
499/*******************************************************************
500 Decide if a file should be operated on.
501********************************************************************/
502
503static bool do_this_one(struct file_info *finfo)
504{
505 if (!finfo->name) {
506 return false;
507 }
508
509 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
510 return true;
511 }
512
513 if (*client_get_fileselection() &&
514 !mask_match(finfo->name,client_get_fileselection(),false)) {
515 DEBUG(3,("mask_match %s failed\n", finfo->name));
516 return false;
517 }
518
519 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
520 DEBUG(3,("newer_than %s failed\n", finfo->name));
521 return false;
522 }
523
524 if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
525 DEBUG(3,("archive %s failed\n", finfo->name));
526 return false;
527 }
528
529 return true;
530}
531
532/****************************************************************************
533 Display info about a file.
534****************************************************************************/
535
536static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
537 const char *dir)
538{
539 time_t t;
540 TALLOC_CTX *ctx = talloc_tos();
541 NTSTATUS status = NT_STATUS_OK;
542
543 if (!do_this_one(finfo)) {
544 return NT_STATUS_OK;
545 }
546
547 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
548 if (!showacls) {
549 d_printf(" %-30s%7.7s %8.0f %s",
550 finfo->name,
551 attrib_string(finfo->mode),
552 (double)finfo->size,
553 time_to_asc(t));
554 dir_total += finfo->size;
555 } else {
556 char *afname = NULL;
557 uint16_t fnum;
558
559 /* skip if this is . or .. */
560 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
561 return NT_STATUS_OK;
562 /* create absolute filename for cli_ntcreate() FIXME */
563 afname = talloc_asprintf(ctx,
564 "%s%s%s",
565 dir,
566 CLI_DIRSEP_STR,
567 finfo->name);
568 if (!afname) {
569 return NT_STATUS_NO_MEMORY;
570 }
571 /* print file meta date header */
572 d_printf( "FILENAME:%s\n", finfo->name);
573 d_printf( "MODE:%s\n", attrib_string(finfo->mode));
574 d_printf( "SIZE:%.0f\n", (double)finfo->size);
575 d_printf( "MTIME:%s", time_to_asc(t));
576 status = cli_ntcreate(cli_state, afname, 0,
577 CREATE_ACCESS_READ, 0,
578 FILE_SHARE_READ|FILE_SHARE_WRITE,
579 FILE_OPEN, 0x0, 0x0, &fnum);
580 if (!NT_STATUS_IS_OK(status)) {
581 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
582 afname, nt_errstr(status)));
583 } else {
584 struct security_descriptor *sd = NULL;
585 sd = cli_query_secdesc(cli_state, fnum, ctx);
586 if (!sd) {
587 DEBUG( 0, ("display_finfo() failed to "
588 "get security descriptor: %s",
589 cli_errstr(cli_state)));
590 status = cli_nt_error(cli_state);
591 } else {
592 display_sec_desc(sd);
593 }
594 TALLOC_FREE(sd);
595 }
596 TALLOC_FREE(afname);
597 }
598 return status;
599}
600
601/****************************************************************************
602 Accumulate size of a file.
603****************************************************************************/
604
605static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
606 const char *dir)
607{
608 if (do_this_one(finfo)) {
609 dir_total += finfo->size;
610 }
611 return NT_STATUS_OK;
612}
613
614static bool do_list_recurse;
615static bool do_list_dirs;
616static char *do_list_queue = 0;
617static long do_list_queue_size = 0;
618static long do_list_queue_start = 0;
619static long do_list_queue_end = 0;
620static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
621 const char *dir);
622
623/****************************************************************************
624 Functions for do_list_queue.
625****************************************************************************/
626
627/*
628 * The do_list_queue is a NUL-separated list of strings stored in a
629 * char*. Since this is a FIFO, we keep track of the beginning and
630 * ending locations of the data in the queue. When we overflow, we
631 * double the size of the char*. When the start of the data passes
632 * the midpoint, we move everything back. This is logically more
633 * complex than a linked list, but easier from a memory management
634 * angle. In any memory error condition, do_list_queue is reset.
635 * Functions check to ensure that do_list_queue is non-NULL before
636 * accessing it.
637 */
638
639static void reset_do_list_queue(void)
640{
641 SAFE_FREE(do_list_queue);
642 do_list_queue_size = 0;
643 do_list_queue_start = 0;
644 do_list_queue_end = 0;
645}
646
647static void init_do_list_queue(void)
648{
649 reset_do_list_queue();
650 do_list_queue_size = 1024;
651 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
652 if (do_list_queue == 0) {
653 d_printf("malloc fail for size %d\n",
654 (int)do_list_queue_size);
655 reset_do_list_queue();
656 } else {
657 memset(do_list_queue, 0, do_list_queue_size);
658 }
659}
660
661static void adjust_do_list_queue(void)
662{
663 /*
664 * If the starting point of the queue is more than half way through,
665 * move everything toward the beginning.
666 */
667
668 if (do_list_queue == NULL) {
669 DEBUG(4,("do_list_queue is empty\n"));
670 do_list_queue_start = do_list_queue_end = 0;
671 return;
672 }
673
674 if (do_list_queue_start == do_list_queue_end) {
675 DEBUG(4,("do_list_queue is empty\n"));
676 do_list_queue_start = do_list_queue_end = 0;
677 *do_list_queue = '\0';
678 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
679 DEBUG(4,("sliding do_list_queue backward\n"));
680 memmove(do_list_queue,
681 do_list_queue + do_list_queue_start,
682 do_list_queue_end - do_list_queue_start);
683 do_list_queue_end -= do_list_queue_start;
684 do_list_queue_start = 0;
685 }
686}
687
688static void add_to_do_list_queue(const char *entry)
689{
690 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
691 while (new_end > do_list_queue_size) {
692 do_list_queue_size *= 2;
693 DEBUG(4,("enlarging do_list_queue to %d\n",
694 (int)do_list_queue_size));
695 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
696 if (! do_list_queue) {
697 d_printf("failure enlarging do_list_queue to %d bytes\n",
698 (int)do_list_queue_size);
699 reset_do_list_queue();
700 } else {
701 memset(do_list_queue + do_list_queue_size / 2,
702 0, do_list_queue_size / 2);
703 }
704 }
705 if (do_list_queue) {
706 safe_strcpy_base(do_list_queue + do_list_queue_end,
707 entry, do_list_queue, do_list_queue_size);
708 do_list_queue_end = new_end;
709 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
710 entry, (int)do_list_queue_start, (int)do_list_queue_end));
711 }
712}
713
714static char *do_list_queue_head(void)
715{
716 return do_list_queue + do_list_queue_start;
717}
718
719static void remove_do_list_queue_head(void)
720{
721 if (do_list_queue_end > do_list_queue_start) {
722 do_list_queue_start += strlen(do_list_queue_head()) + 1;
723 adjust_do_list_queue();
724 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
725 (int)do_list_queue_start, (int)do_list_queue_end));
726 }
727}
728
729static int do_list_queue_empty(void)
730{
731 return (! (do_list_queue && *do_list_queue));
732}
733
734/****************************************************************************
735 A helper for do_list.
736****************************************************************************/
737
738static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
739 const char *mask, void *state)
740{
741 struct cli_state *cli_state = (struct cli_state *)state;
742 TALLOC_CTX *ctx = talloc_tos();
743 char *dir = NULL;
744 char *dir_end = NULL;
745 NTSTATUS status = NT_STATUS_OK;
746
747 /* Work out the directory. */
748 dir = talloc_strdup(ctx, mask);
749 if (!dir) {
750 return NT_STATUS_NO_MEMORY;
751 }
752 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
753 *dir_end = '\0';
754 }
755
756 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
757 if (do_list_dirs && do_this_one(f)) {
758 status = do_list_fn(cli_state, f, dir);
759 if (!NT_STATUS_IS_OK(status)) {
760 return status;
761 }
762 }
763 if (do_list_recurse &&
764 f->name &&
765 !strequal(f->name,".") &&
766 !strequal(f->name,"..")) {
767 char *mask2 = NULL;
768 char *p = NULL;
769
770 if (!f->name[0]) {
771 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
772 TALLOC_FREE(dir);
773 return NT_STATUS_UNSUCCESSFUL;
774 }
775
776 mask2 = talloc_asprintf(ctx,
777 "%s%s",
778 mntpoint,
779 mask);
780 if (!mask2) {
781 TALLOC_FREE(dir);
782 return NT_STATUS_NO_MEMORY;
783 }
784 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
785 if (p) {
786 p[1] = 0;
787 } else {
788 mask2[0] = '\0';
789 }
790 mask2 = talloc_asprintf_append(mask2,
791 "%s%s*",
792 f->name,
793 CLI_DIRSEP_STR);
794 if (!mask2) {
795 TALLOC_FREE(dir);
796 return NT_STATUS_NO_MEMORY;
797 }
798 add_to_do_list_queue(mask2);
799 TALLOC_FREE(mask2);
800 }
801 TALLOC_FREE(dir);
802 return NT_STATUS_OK;
803 }
804
805 if (do_this_one(f)) {
806 status = do_list_fn(cli_state, f, dir);
807 }
808 TALLOC_FREE(dir);
809 return status;
810}
811
812/****************************************************************************
813 A wrapper around cli_list that adds recursion.
814****************************************************************************/
815
816NTSTATUS do_list(const char *mask,
817 uint16 attribute,
818 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
819 const char *dir),
820 bool rec,
821 bool dirs)
822{
823 static int in_do_list = 0;
824 TALLOC_CTX *ctx = talloc_tos();
825 struct cli_state *targetcli = NULL;
826 char *targetpath = NULL;
827 NTSTATUS ret_status = NT_STATUS_OK;
828 NTSTATUS status = NT_STATUS_OK;
829
830 if (in_do_list && rec) {
831 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
832 exit(1);
833 }
834
835 in_do_list = 1;
836
837 do_list_recurse = rec;
838 do_list_dirs = dirs;
839 do_list_fn = fn;
840
841 if (rec) {
842 init_do_list_queue();
843 add_to_do_list_queue(mask);
844
845 while (!do_list_queue_empty()) {
846 /*
847 * Need to copy head so that it doesn't become
848 * invalid inside the call to cli_list. This
849 * would happen if the list were expanded
850 * during the call.
851 * Fix from E. Jay Berkenbilt (ejb@ql.org)
852 */
853 char *head = talloc_strdup(ctx, do_list_queue_head());
854
855 if (!head) {
856 return NT_STATUS_NO_MEMORY;
857 }
858
859 /* check for dfs */
860
861 if ( !cli_resolve_path(ctx, "", auth_info, cli, head, &targetcli, &targetpath ) ) {
862 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
863 remove_do_list_queue_head();
864 continue;
865 }
866
867 status = cli_list(targetcli, targetpath, attribute,
868 do_list_helper, targetcli);
869 if (!NT_STATUS_IS_OK(status)) {
870 d_printf("%s listing %s\n",
871 nt_errstr(status), targetpath);
872 ret_status = status;
873 }
874 remove_do_list_queue_head();
875 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
876 char *next_file = do_list_queue_head();
877 char *save_ch = 0;
878 if ((strlen(next_file) >= 2) &&
879 (next_file[strlen(next_file) - 1] == '*') &&
880 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
881 save_ch = next_file +
882 strlen(next_file) - 2;
883 *save_ch = '\0';
884 if (showacls) {
885 /* cwd is only used if showacls is on */
886 client_set_cwd(next_file);
887 }
888 }
889 if (!showacls) /* don't disturbe the showacls output */
890 d_printf("\n%s\n",next_file);
891 if (save_ch) {
892 *save_ch = CLI_DIRSEP_CHAR;
893 }
894 }
895 TALLOC_FREE(head);
896 TALLOC_FREE(targetpath);
897 }
898 } else {
899 /* check for dfs */
900 if (cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetpath)) {
901
902 status = cli_list(targetcli, targetpath, attribute,
903 do_list_helper, targetcli);
904 if (!NT_STATUS_IS_OK(status)) {
905 d_printf("%s listing %s\n",
906 nt_errstr(status), targetpath);
907 ret_status = status;
908 }
909 TALLOC_FREE(targetpath);
910 } else {
911 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
912 ret_status = cli_nt_error(cli);
913 }
914 }
915
916 in_do_list = 0;
917 reset_do_list_queue();
918 return ret_status;
919}
920
921/****************************************************************************
922 Get a directory listing.
923****************************************************************************/
924
925static int cmd_dir(void)
926{
927 TALLOC_CTX *ctx = talloc_tos();
928 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
929 char *mask = NULL;
930 char *buf = NULL;
931 int rc = 1;
932 NTSTATUS status;
933
934 dir_total = 0;
935 mask = talloc_strdup(ctx, client_get_cur_dir());
936 if (!mask) {
937 return 1;
938 }
939
940 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
941 normalize_name(buf);
942 if (*buf == CLI_DIRSEP_CHAR) {
943 mask = talloc_strdup(ctx, buf);
944 } else {
945 mask = talloc_asprintf_append(mask, "%s", buf);
946 }
947 } else {
948 mask = talloc_asprintf_append(mask, "*");
949 }
950 if (!mask) {
951 return 1;
952 }
953
954 if (showacls) {
955 /* cwd is only used if showacls is on */
956 client_set_cwd(client_get_cur_dir());
957 }
958
959 status = do_list(mask, attribute, display_finfo, recurse, true);
960 if (!NT_STATUS_IS_OK(status)) {
961 return 1;
962 }
963
964 rc = do_dskattr();
965
966 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
967
968 return rc;
969}
970
971/****************************************************************************
972 Get a directory listing.
973****************************************************************************/
974
975static int cmd_du(void)
976{
977 TALLOC_CTX *ctx = talloc_tos();
978 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
979 char *mask = NULL;
980 char *buf = NULL;
981 NTSTATUS status;
982 int rc = 1;
983
984 dir_total = 0;
985 mask = talloc_strdup(ctx, client_get_cur_dir());
986 if (!mask) {
987 return 1;
988 }
989 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
990 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
991 if (!mask) {
992 return 1;
993 }
994 }
995
996 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
997 normalize_name(buf);
998 if (*buf == CLI_DIRSEP_CHAR) {
999 mask = talloc_strdup(ctx, buf);
1000 } else {
1001 mask = talloc_asprintf_append(mask, "%s", buf);
1002 }
1003 } else {
1004 mask = talloc_strdup(ctx, "*");
1005 }
1006
1007 status = do_list(mask, attribute, do_du, recurse, true);
1008 if (!NT_STATUS_IS_OK(status)) {
1009 return 1;
1010 }
1011
1012 rc = do_dskattr();
1013
1014 d_printf("Total number of bytes: %.0f\n", dir_total);
1015
1016 return rc;
1017}
1018
1019static int cmd_echo(void)
1020{
1021 TALLOC_CTX *ctx = talloc_tos();
1022 char *num;
1023 char *data;
1024 NTSTATUS status;
1025
1026 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1027 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1028 d_printf("echo <num> <data>\n");
1029 return 1;
1030 }
1031
1032 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1033
1034 if (!NT_STATUS_IS_OK(status)) {
1035 d_printf("echo failed: %s\n", nt_errstr(status));
1036 return 1;
1037 }
1038
1039 return 0;
1040}
1041
1042/****************************************************************************
1043 Get a file from rname to lname
1044****************************************************************************/
1045
1046static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1047{
1048 int *pfd = (int *)priv;
1049 if (writefile(*pfd, buf, n) == -1) {
1050 return map_nt_error_from_unix(errno);
1051 }
1052 return NT_STATUS_OK;
1053}
1054
1055static int do_get(const char *rname, const char *lname_in, bool reget)
1056{
1057 TALLOC_CTX *ctx = talloc_tos();
1058 int handle = 0;
1059 uint16_t fnum;
1060 bool newhandle = false;
1061 struct timespec tp_start;
1062 uint16 attr;
1063 SMB_OFF_T size;
1064 off_t start = 0;
1065 SMB_OFF_T nread = 0;
1066 int rc = 0;
1067 struct cli_state *targetcli = NULL;
1068 char *targetname = NULL;
1069 char *lname = NULL;
1070 NTSTATUS status;
1071
1072 lname = talloc_strdup(ctx, lname_in);
1073 if (!lname) {
1074 return 1;
1075 }
1076
1077 if (lowercase) {
1078 strlower_m(lname);
1079 }
1080
1081 if (!cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli, &targetname ) ) {
1082 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1083 return 1;
1084 }
1085
1086 clock_gettime_mono(&tp_start);
1087
1088 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1089 if (!NT_STATUS_IS_OK(status)) {
1090 d_printf("%s opening remote file %s\n", nt_errstr(status),
1091 rname);
1092 return 1;
1093 }
1094
1095 if(!strcmp(lname,"-")) {
1096 handle = fileno(stdout);
1097 } else {
1098 if (reget) {
1099 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
1100 if (handle >= 0) {
1101 start = sys_lseek(handle, 0, SEEK_END);
1102 if (start == -1) {
1103 d_printf("Error seeking local file\n");
1104 return 1;
1105 }
1106 }
1107 } else {
1108 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1109 }
1110 newhandle = true;
1111 }
1112 if (handle < 0) {
1113 d_printf("Error opening local file %s\n",lname);
1114 return 1;
1115 }
1116
1117
1118 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1119 NULL, NULL, NULL);
1120 if (!NT_STATUS_IS_OK(status)) {
1121 status = cli_getattrE(targetcli, fnum, &attr, &size, NULL, NULL,
1122 NULL);
1123 if(!NT_STATUS_IS_OK(status)) {
1124 d_printf("getattrib: %s\n", nt_errstr(status));
1125 return 1;
1126 }
1127 }
1128
1129 DEBUG(1,("getting file %s of size %.0f as %s ",
1130 rname, (double)size, lname));
1131
1132 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1133 writefile_sink, (void *)&handle, &nread);
1134 if (!NT_STATUS_IS_OK(status)) {
1135 d_fprintf(stderr, "parallel_read returned %s\n",
1136 nt_errstr(status));
1137 cli_close(targetcli, fnum);
1138 return 1;
1139 }
1140
1141 status = cli_close(targetcli, fnum);
1142 if (!NT_STATUS_IS_OK(status)) {
1143 d_printf("Error %s closing remote file\n", nt_errstr(status));
1144 rc = 1;
1145 }
1146
1147 if (newhandle) {
1148 close(handle);
1149 }
1150
1151 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1152 cli_setatr(cli, rname, attr & ~(uint16)FILE_ATTRIBUTE_ARCHIVE, 0);
1153 }
1154
1155 {
1156 struct timespec tp_end;
1157 int this_time;
1158
1159 clock_gettime_mono(&tp_end);
1160 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1161 get_total_time_ms += this_time;
1162 get_total_size += nread;
1163
1164 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1165 nread / (1.024*this_time + 1.0e-4),
1166 get_total_size / (1.024*get_total_time_ms)));
1167 }
1168
1169 TALLOC_FREE(targetname);
1170 return rc;
1171}
1172
1173/****************************************************************************
1174 Get a file.
1175****************************************************************************/
1176
1177static int cmd_get(void)
1178{
1179 TALLOC_CTX *ctx = talloc_tos();
1180 char *lname = NULL;
1181 char *rname = NULL;
1182 char *fname = NULL;
1183
1184 rname = talloc_strdup(ctx, client_get_cur_dir());
1185 if (!rname) {
1186 return 1;
1187 }
1188
1189 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1190 d_printf("get <filename> [localname]\n");
1191 return 1;
1192 }
1193 rname = talloc_asprintf_append(rname, "%s", fname);
1194 if (!rname) {
1195 return 1;
1196 }
1197 rname = clean_name(ctx, rname);
1198 if (!rname) {
1199 return 1;
1200 }
1201
1202 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1203 if (!lname) {
1204 lname = fname;
1205 }
1206
1207 return do_get(rname, lname, false);
1208}
1209
1210/****************************************************************************
1211 Do an mget operation on one file.
1212****************************************************************************/
1213
1214static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1215 const char *dir)
1216{
1217 TALLOC_CTX *ctx = talloc_tos();
1218 NTSTATUS status = NT_STATUS_OK;
1219 char *rname = NULL;
1220 char *quest = NULL;
1221 char *saved_curdir = NULL;
1222 char *mget_mask = NULL;
1223 char *new_cd = NULL;
1224
1225 if (!finfo->name) {
1226 return NT_STATUS_OK;
1227 }
1228
1229 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1230 return NT_STATUS_OK;
1231
1232 if (abort_mget) {
1233 d_printf("mget aborted\n");
1234 return NT_STATUS_UNSUCCESSFUL;
1235 }
1236
1237 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
1238 if (asprintf(&quest,
1239 "Get directory %s? ",finfo->name) < 0) {
1240 return NT_STATUS_NO_MEMORY;
1241 }
1242 } else {
1243 if (asprintf(&quest,
1244 "Get file %s? ",finfo->name) < 0) {
1245 return NT_STATUS_NO_MEMORY;
1246 }
1247 }
1248
1249 if (prompt && !yesno(quest)) {
1250 SAFE_FREE(quest);
1251 return NT_STATUS_OK;
1252 }
1253 SAFE_FREE(quest);
1254
1255 if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
1256 rname = talloc_asprintf(ctx,
1257 "%s%s",
1258 client_get_cur_dir(),
1259 finfo->name);
1260 if (!rname) {
1261 return NT_STATUS_NO_MEMORY;
1262 }
1263 do_get(rname, finfo->name, false);
1264 TALLOC_FREE(rname);
1265 return NT_STATUS_OK;
1266 }
1267
1268 /* handle directories */
1269 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1270 if (!saved_curdir) {
1271 return NT_STATUS_NO_MEMORY;
1272 }
1273
1274 new_cd = talloc_asprintf(ctx,
1275 "%s%s%s",
1276 client_get_cur_dir(),
1277 finfo->name,
1278 CLI_DIRSEP_STR);
1279 if (!new_cd) {
1280 return NT_STATUS_NO_MEMORY;
1281 }
1282 client_set_cur_dir(new_cd);
1283
1284 string_replace(finfo->name,'\\','/');
1285 if (lowercase) {
1286 strlower_m(finfo->name);
1287 }
1288
1289 if (!directory_exist(finfo->name) &&
1290 mkdir(finfo->name,0777) != 0) {
1291 d_printf("failed to create directory %s\n",finfo->name);
1292 client_set_cur_dir(saved_curdir);
1293 return map_nt_error_from_unix(errno);
1294 }
1295
1296 if (chdir(finfo->name) != 0) {
1297 d_printf("failed to chdir to directory %s\n",finfo->name);
1298 client_set_cur_dir(saved_curdir);
1299 return map_nt_error_from_unix(errno);
1300 }
1301
1302 mget_mask = talloc_asprintf(ctx,
1303 "%s*",
1304 client_get_cur_dir());
1305
1306 if (!mget_mask) {
1307 return NT_STATUS_NO_MEMORY;
1308 }
1309
1310 status = do_list(mget_mask,
1311 (FILE_ATTRIBUTE_SYSTEM
1312 | FILE_ATTRIBUTE_HIDDEN
1313 | FILE_ATTRIBUTE_DIRECTORY),
1314 do_mget, false, true);
1315 if (!NT_STATUS_IS_OK(status)
1316 && !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1317 /*
1318 * Ignore access denied errors to ensure all permitted files are
1319 * pulled down.
1320 */
1321 return status;
1322 }
1323
1324 if (chdir("..") == -1) {
1325 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1326 strerror(errno) );
1327 return map_nt_error_from_unix(errno);
1328 }
1329 client_set_cur_dir(saved_curdir);
1330 TALLOC_FREE(mget_mask);
1331 TALLOC_FREE(saved_curdir);
1332 TALLOC_FREE(new_cd);
1333 return NT_STATUS_OK;
1334}
1335
1336/****************************************************************************
1337 View the file using the pager.
1338****************************************************************************/
1339
1340static int cmd_more(void)
1341{
1342 TALLOC_CTX *ctx = talloc_tos();
1343 char *rname = NULL;
1344 char *fname = NULL;
1345 char *lname = NULL;
1346 char *pager_cmd = NULL;
1347 const char *pager;
1348 int fd;
1349 int rc = 0;
1350
1351 rname = talloc_strdup(ctx, client_get_cur_dir());
1352 if (!rname) {
1353 return 1;
1354 }
1355
1356 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1357 if (!lname) {
1358 return 1;
1359 }
1360 fd = mkstemp(lname);
1361 if (fd == -1) {
1362 d_printf("failed to create temporary file for more\n");
1363 return 1;
1364 }
1365 close(fd);
1366
1367 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1368 d_printf("more <filename>\n");
1369 unlink(lname);
1370 return 1;
1371 }
1372 rname = talloc_asprintf_append(rname, "%s", fname);
1373 if (!rname) {
1374 return 1;
1375 }
1376 rname = clean_name(ctx,rname);
1377 if (!rname) {
1378 return 1;
1379 }
1380
1381 rc = do_get(rname, lname, false);
1382
1383 pager=getenv("PAGER");
1384
1385 pager_cmd = talloc_asprintf(ctx,
1386 "%s %s",
1387 (pager? pager:PAGER),
1388 lname);
1389 if (!pager_cmd) {
1390 return 1;
1391 }
1392 if (system(pager_cmd) == -1) {
1393 d_printf("system command '%s' returned -1\n",
1394 pager_cmd);
1395 }
1396 unlink(lname);
1397
1398 return rc;
1399}
1400
1401/****************************************************************************
1402 Do a mget command.
1403****************************************************************************/
1404
1405static int cmd_mget(void)
1406{
1407 TALLOC_CTX *ctx = talloc_tos();
1408 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1409 char *mget_mask = NULL;
1410 char *buf = NULL;
1411 NTSTATUS status = NT_STATUS_OK;
1412
1413 if (recurse) {
1414 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1415 }
1416
1417 abort_mget = false;
1418
1419 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1420
1421 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1422 if (!mget_mask) {
1423 return 1;
1424 }
1425 if (*buf == CLI_DIRSEP_CHAR) {
1426 mget_mask = talloc_strdup(ctx, buf);
1427 } else {
1428 mget_mask = talloc_asprintf_append(mget_mask,
1429 "%s", buf);
1430 }
1431 if (!mget_mask) {
1432 return 1;
1433 }
1434 status = do_list(mget_mask, attribute, do_mget, false, true);
1435 if (!NT_STATUS_IS_OK(status)) {
1436 return 1;
1437 }
1438 }
1439
1440 if (mget_mask == NULL) {
1441 d_printf("nothing to mget\n");
1442 return 0;
1443 }
1444
1445 if (!*mget_mask) {
1446 mget_mask = talloc_asprintf(ctx,
1447 "%s*",
1448 client_get_cur_dir());
1449 if (!mget_mask) {
1450 return 1;
1451 }
1452 status = do_list(mget_mask, attribute, do_mget, false, true);
1453 if (!NT_STATUS_IS_OK(status)) {
1454 return 1;
1455 }
1456 }
1457
1458 return 0;
1459}
1460
1461/****************************************************************************
1462 Make a directory of name "name".
1463****************************************************************************/
1464
1465static bool do_mkdir(const char *name)
1466{
1467 TALLOC_CTX *ctx = talloc_tos();
1468 struct cli_state *targetcli;
1469 char *targetname = NULL;
1470 NTSTATUS status;
1471
1472 if (!cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli, &targetname)) {
1473 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1474 return false;
1475 }
1476
1477 status = cli_mkdir(targetcli, targetname);
1478 if (!NT_STATUS_IS_OK(status)) {
1479 d_printf("%s making remote directory %s\n",
1480 nt_errstr(status),name);
1481 return false;
1482 }
1483
1484 return true;
1485}
1486
1487/****************************************************************************
1488 Show 8.3 name of a file.
1489****************************************************************************/
1490
1491static bool do_altname(const char *name)
1492{
1493 fstring altname;
1494 NTSTATUS status;
1495
1496 status = cli_qpathinfo_alt_name(cli, name, altname);
1497 if (!NT_STATUS_IS_OK(status)) {
1498 d_printf("%s getting alt name for %s\n",
1499 nt_errstr(status),name);
1500 return false;
1501 }
1502 d_printf("%s\n", altname);
1503
1504 return true;
1505}
1506
1507/****************************************************************************
1508 Exit client.
1509****************************************************************************/
1510
1511static int cmd_quit(void)
1512{
1513 cli_shutdown(cli);
1514 exit(0);
1515 /* NOTREACHED */
1516 return 0;
1517}
1518
1519/****************************************************************************
1520 Make a directory.
1521****************************************************************************/
1522
1523static int cmd_mkdir(void)
1524{
1525 TALLOC_CTX *ctx = talloc_tos();
1526 char *mask = NULL;
1527 char *buf = NULL;
1528
1529 mask = talloc_strdup(ctx, client_get_cur_dir());
1530 if (!mask) {
1531 return 1;
1532 }
1533
1534 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1535 if (!recurse) {
1536 d_printf("mkdir <dirname>\n");
1537 }
1538 return 1;
1539 }
1540 mask = talloc_asprintf_append(mask, "%s", buf);
1541 if (!mask) {
1542 return 1;
1543 }
1544
1545 if (recurse) {
1546 char *ddir = NULL;
1547 char *ddir2 = NULL;
1548 struct cli_state *targetcli;
1549 char *targetname = NULL;
1550 char *p = NULL;
1551 char *saveptr;
1552
1553 ddir2 = talloc_strdup(ctx, "");
1554 if (!ddir2) {
1555 return 1;
1556 }
1557
1558 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
1559 return 1;
1560 }
1561
1562 ddir = talloc_strdup(ctx, targetname);
1563 if (!ddir) {
1564 return 1;
1565 }
1566 trim_char(ddir,'.','\0');
1567 p = strtok_r(ddir, "/\\", &saveptr);
1568 while (p) {
1569 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1570 if (!ddir2) {
1571 return 1;
1572 }
1573 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1574 do_mkdir(ddir2);
1575 }
1576 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1577 if (!ddir2) {
1578 return 1;
1579 }
1580 p = strtok_r(NULL, "/\\", &saveptr);
1581 }
1582 } else {
1583 do_mkdir(mask);
1584 }
1585
1586 return 0;
1587}
1588
1589/****************************************************************************
1590 Show alt name.
1591****************************************************************************/
1592
1593static int cmd_altname(void)
1594{
1595 TALLOC_CTX *ctx = talloc_tos();
1596 char *name;
1597 char *buf;
1598
1599 name = talloc_strdup(ctx, client_get_cur_dir());
1600 if (!name) {
1601 return 1;
1602 }
1603
1604 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1605 d_printf("altname <file>\n");
1606 return 1;
1607 }
1608 name = talloc_asprintf_append(name, "%s", buf);
1609 if (!name) {
1610 return 1;
1611 }
1612 do_altname(name);
1613 return 0;
1614}
1615
1616static char *attr_str(TALLOC_CTX *mem_ctx, uint16_t mode)
1617{
1618 char *attrs = TALLOC_ZERO_ARRAY(mem_ctx, char, 17);
1619 int i = 0;
1620
1621 if (!(mode & FILE_ATTRIBUTE_NORMAL)) {
1622 if (mode & FILE_ATTRIBUTE_ENCRYPTED) {
1623 attrs[i++] = 'E';
1624 }
1625 if (mode & FILE_ATTRIBUTE_NONINDEXED) {
1626 attrs[i++] = 'N';
1627 }
1628 if (mode & FILE_ATTRIBUTE_OFFLINE) {
1629 attrs[i++] = 'O';
1630 }
1631 if (mode & FILE_ATTRIBUTE_COMPRESSED) {
1632 attrs[i++] = 'C';
1633 }
1634 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1635 attrs[i++] = 'r';
1636 }
1637 if (mode & FILE_ATTRIBUTE_SPARSE) {
1638 attrs[i++] = 's';
1639 }
1640 if (mode & FILE_ATTRIBUTE_TEMPORARY) {
1641 attrs[i++] = 'T';
1642 }
1643 if (mode & FILE_ATTRIBUTE_NORMAL) {
1644 attrs[i++] = 'N';
1645 }
1646 if (mode & FILE_ATTRIBUTE_READONLY) {
1647 attrs[i++] = 'R';
1648 }
1649 if (mode & FILE_ATTRIBUTE_HIDDEN) {
1650 attrs[i++] = 'H';
1651 }
1652 if (mode & FILE_ATTRIBUTE_SYSTEM) {
1653 attrs[i++] = 'S';
1654 }
1655 if (mode & FILE_ATTRIBUTE_DIRECTORY) {
1656 attrs[i++] = 'D';
1657 }
1658 if (mode & FILE_ATTRIBUTE_ARCHIVE) {
1659 attrs[i++] = 'A';
1660 }
1661 }
1662 return attrs;
1663}
1664
1665/****************************************************************************
1666 Show all info we can get
1667****************************************************************************/
1668
1669static int do_allinfo(const char *name)
1670{
1671 fstring altname;
1672 struct timespec b_time, a_time, m_time, c_time;
1673 SMB_OFF_T size;
1674 uint16_t mode;
1675 SMB_INO_T ino;
1676 NTTIME tmp;
1677 uint16_t fnum;
1678 unsigned int num_streams;
1679 struct stream_struct *streams;
1680 int num_snapshots;
1681 char **snapshots;
1682 unsigned int i;
1683 NTSTATUS status;
1684
1685 status = cli_qpathinfo_alt_name(cli, name, altname);
1686 if (!NT_STATUS_IS_OK(status)) {
1687 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1688 name);
1689 return false;
1690 }
1691 d_printf("altname: %s\n", altname);
1692
1693 status = cli_qpathinfo2(cli, name, &b_time, &a_time, &m_time, &c_time,
1694 &size, &mode, &ino);
1695 if (!NT_STATUS_IS_OK(status)) {
1696 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1697 name);
1698 return false;
1699 }
1700
1701 unix_timespec_to_nt_time(&tmp, b_time);
1702 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1703
1704 unix_timespec_to_nt_time(&tmp, a_time);
1705 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1706
1707 unix_timespec_to_nt_time(&tmp, m_time);
1708 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1709
1710 unix_timespec_to_nt_time(&tmp, c_time);
1711 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1712
1713 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode), mode);
1714
1715 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1716 &streams);
1717 if (!NT_STATUS_IS_OK(status)) {
1718 d_printf("%s getting streams for %s\n", nt_errstr(status),
1719 name);
1720 return false;
1721 }
1722
1723 for (i=0; i<num_streams; i++) {
1724 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1725 (unsigned long long)streams[i].size);
1726 }
1727
1728 status = cli_ntcreate(cli, name, 0,
1729 SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1730 SEC_STD_SYNCHRONIZE, 0,
1731 FILE_SHARE_READ|FILE_SHARE_WRITE
1732 |FILE_SHARE_DELETE,
1733 FILE_OPEN, 0x0, 0x0, &fnum);
1734 if (!NT_STATUS_IS_OK(status)) {
1735 /*
1736 * Ignore failure, it does not hurt if we can't list
1737 * snapshots
1738 */
1739 return 0;
1740 }
1741 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1742 true, &snapshots, &num_snapshots);
1743 if (!NT_STATUS_IS_OK(status)) {
1744 cli_close(cli, fnum);
1745 return 0;
1746 }
1747
1748 for (i=0; i<num_snapshots; i++) {
1749 char *snap_name;
1750
1751 d_printf("%s\n", snapshots[i]);
1752 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1753 snapshots[i], name);
1754 status = cli_qpathinfo2(cli, snap_name, &b_time, &a_time,
1755 &m_time, &c_time, &size,
1756 NULL, NULL);
1757 if (!NT_STATUS_IS_OK(status)) {
1758 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1759 snap_name, nt_errstr(status));
1760 TALLOC_FREE(snap_name);
1761 continue;
1762 }
1763 unix_timespec_to_nt_time(&tmp, b_time);
1764 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1765 unix_timespec_to_nt_time(&tmp, a_time);
1766 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1767 unix_timespec_to_nt_time(&tmp, m_time);
1768 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1769 unix_timespec_to_nt_time(&tmp, c_time);
1770 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1771 d_printf("size: %d\n", (int)size);
1772 }
1773
1774 TALLOC_FREE(snapshots);
1775
1776 return 0;
1777}
1778
1779/****************************************************************************
1780 Show all info we can get
1781****************************************************************************/
1782
1783static int cmd_allinfo(void)
1784{
1785 TALLOC_CTX *ctx = talloc_tos();
1786 char *name;
1787 char *buf;
1788
1789 name = talloc_strdup(ctx, client_get_cur_dir());
1790 if (!name) {
1791 return 1;
1792 }
1793
1794 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1795 d_printf("allinfo <file>\n");
1796 return 1;
1797 }
1798 name = talloc_asprintf_append(name, "%s", buf);
1799 if (!name) {
1800 return 1;
1801 }
1802
1803 do_allinfo(name);
1804
1805 return 0;
1806}
1807
1808/****************************************************************************
1809 Put a single file.
1810****************************************************************************/
1811
1812static int do_put(const char *rname, const char *lname, bool reput)
1813{
1814 TALLOC_CTX *ctx = talloc_tos();
1815 uint16_t fnum;
1816 XFILE *f;
1817 SMB_OFF_T start = 0;
1818 int rc = 0;
1819 struct timespec tp_start;
1820 struct cli_state *targetcli;
1821 char *targetname = NULL;
1822 struct push_state state;
1823 NTSTATUS status;
1824
1825 if (!cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli, &targetname)) {
1826 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1827 return 1;
1828 }
1829
1830 clock_gettime_mono(&tp_start);
1831
1832 if (reput) {
1833 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1834 if (NT_STATUS_IS_OK(status)) {
1835 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1836 targetcli, fnum, NULL,
1837 &start, NULL, NULL,
1838 NULL, NULL, NULL)) &&
1839 !NT_STATUS_IS_OK(status = cli_getattrE(
1840 targetcli, fnum, NULL,
1841 &start, NULL, NULL,
1842 NULL))) {
1843 d_printf("getattrib: %s\n", nt_errstr(status));
1844 return 1;
1845 }
1846 }
1847 } else {
1848 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1849 }
1850
1851 if (!NT_STATUS_IS_OK(status)) {
1852 d_printf("%s opening remote file %s\n", nt_errstr(status),
1853 rname);
1854 return 1;
1855 }
1856
1857 /* allow files to be piped into smbclient
1858 jdblair 24.jun.98
1859
1860 Note that in this case this function will exit(0) rather
1861 than returning. */
1862 if (!strcmp(lname, "-")) {
1863 f = x_stdin;
1864 /* size of file is not known */
1865 } else {
1866 f = x_fopen(lname,O_RDONLY, 0);
1867 if (f && reput) {
1868 if (x_tseek(f, start, SEEK_SET) == -1) {
1869 d_printf("Error seeking local file\n");
1870 x_fclose(f);
1871 return 1;
1872 }
1873 }
1874 }
1875
1876 if (!f) {
1877 d_printf("Error opening local file %s\n",lname);
1878 return 1;
1879 }
1880
1881 DEBUG(1,("putting file %s as %s ",lname,
1882 rname));
1883
1884 x_setvbuf(f, NULL, X_IOFBF, io_bufsize);
1885
1886 state.f = f;
1887 state.nread = 0;
1888
1889 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
1890 &state);
1891 if (!NT_STATUS_IS_OK(status)) {
1892 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
1893 rc = 1;
1894 }
1895
1896 status = cli_close(targetcli, fnum);
1897 if (!NT_STATUS_IS_OK(status)) {
1898 d_printf("%s closing remote file %s\n", nt_errstr(status),
1899 rname);
1900 if (f != x_stdin) {
1901 x_fclose(f);
1902 }
1903 return 1;
1904 }
1905
1906 if (f != x_stdin) {
1907 x_fclose(f);
1908 }
1909
1910 {
1911 struct timespec tp_end;
1912 int this_time;
1913
1914 clock_gettime_mono(&tp_end);
1915 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1916 put_total_time_ms += this_time;
1917 put_total_size += state.nread;
1918
1919 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1920 state.nread / (1.024*this_time + 1.0e-4),
1921 put_total_size / (1.024*put_total_time_ms)));
1922 }
1923
1924 if (f == x_stdin) {
1925 cli_shutdown(cli);
1926 exit(rc);
1927 }
1928
1929 return rc;
1930}
1931
1932/****************************************************************************
1933 Put a file.
1934****************************************************************************/
1935
1936static int cmd_put(void)
1937{
1938 TALLOC_CTX *ctx = talloc_tos();
1939 char *lname;
1940 char *rname;
1941 char *buf;
1942
1943 rname = talloc_strdup(ctx, client_get_cur_dir());
1944 if (!rname) {
1945 return 1;
1946 }
1947
1948 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
1949 d_printf("put <filename>\n");
1950 return 1;
1951 }
1952
1953 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1954 rname = talloc_asprintf_append(rname, "%s", buf);
1955 } else {
1956 rname = talloc_asprintf_append(rname, "%s", lname);
1957 }
1958 if (!rname) {
1959 return 1;
1960 }
1961
1962 rname = clean_name(ctx, rname);
1963 if (!rname) {
1964 return 1;
1965 }
1966
1967 {
1968 SMB_STRUCT_STAT st;
1969 /* allow '-' to represent stdin
1970 jdblair, 24.jun.98 */
1971 if (!file_exist_stat(lname, &st, false) &&
1972 (strcmp(lname,"-"))) {
1973 d_printf("%s does not exist\n",lname);
1974 return 1;
1975 }
1976 }
1977
1978 return do_put(rname, lname, false);
1979}
1980
1981/*************************************
1982 File list structure.
1983*************************************/
1984
1985static struct file_list {
1986 struct file_list *prev, *next;
1987 char *file_path;
1988 bool isdir;
1989} *file_list;
1990
1991/****************************************************************************
1992 Free a file_list structure.
1993****************************************************************************/
1994
1995static void free_file_list (struct file_list *l_head)
1996{
1997 struct file_list *list, *next;
1998
1999 for (list = l_head; list; list = next) {
2000 next = list->next;
2001 DLIST_REMOVE(l_head, list);
2002 SAFE_FREE(list->file_path);
2003 SAFE_FREE(list);
2004 }
2005}
2006
2007/****************************************************************************
2008 Seek in a directory/file list until you get something that doesn't start with
2009 the specified name.
2010****************************************************************************/
2011
2012static bool seek_list(struct file_list *list, char *name)
2013{
2014 while (list) {
2015 trim_string(list->file_path,"./","\n");
2016 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2017 return true;
2018 }
2019 list = list->next;
2020 }
2021
2022 return false;
2023}
2024
2025/****************************************************************************
2026 Set the file selection mask.
2027****************************************************************************/
2028
2029static int cmd_select(void)
2030{
2031 TALLOC_CTX *ctx = talloc_tos();
2032 char *new_fs = NULL;
2033 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2034 ;
2035 if (new_fs) {
2036 client_set_fileselection(new_fs);
2037 } else {
2038 client_set_fileselection("");
2039 }
2040 return 0;
2041}
2042
2043/****************************************************************************
2044 Recursive file matching function act as find
2045 match must be always set to true when calling this function
2046****************************************************************************/
2047
2048static int file_find(struct file_list **list, const char *directory,
2049 const char *expression, bool match)
2050{
2051 SMB_STRUCT_DIR *dir;
2052 struct file_list *entry;
2053 struct stat statbuf;
2054 int ret;
2055 char *path;
2056 bool isdir;
2057 const char *dname;
2058
2059 dir = sys_opendir(directory);
2060 if (!dir)
2061 return -1;
2062
2063 while ((dname = readdirname(dir))) {
2064 if (!strcmp("..", dname))
2065 continue;
2066 if (!strcmp(".", dname))
2067 continue;
2068
2069 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
2070 continue;
2071 }
2072
2073 isdir = false;
2074 if (!match || !gen_fnmatch(expression, dname)) {
2075 if (recurse) {
2076 ret = stat(path, &statbuf);
2077 if (ret == 0) {
2078 if (S_ISDIR(statbuf.st_mode)) {
2079 isdir = true;
2080 ret = file_find(list, path, expression, false);
2081 }
2082 } else {
2083 d_printf("file_find: cannot stat file %s\n", path);
2084 }
2085
2086 if (ret == -1) {
2087 SAFE_FREE(path);
2088 sys_closedir(dir);
2089 return -1;
2090 }
2091 }
2092 entry = SMB_MALLOC_P(struct file_list);
2093 if (!entry) {
2094 d_printf("Out of memory in file_find\n");
2095 sys_closedir(dir);
2096 return -1;
2097 }
2098 entry->file_path = path;
2099 entry->isdir = isdir;
2100 DLIST_ADD(*list, entry);
2101 } else {
2102 SAFE_FREE(path);
2103 }
2104 }
2105
2106 sys_closedir(dir);
2107 return 0;
2108}
2109
2110/****************************************************************************
2111 mput some files.
2112****************************************************************************/
2113
2114static int cmd_mput(void)
2115{
2116 TALLOC_CTX *ctx = talloc_tos();
2117 char *p = NULL;
2118
2119 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2120 int ret;
2121 struct file_list *temp_list;
2122 char *quest, *lname, *rname;
2123
2124 file_list = NULL;
2125
2126 ret = file_find(&file_list, ".", p, true);
2127 if (ret) {
2128 free_file_list(file_list);
2129 continue;
2130 }
2131
2132 quest = NULL;
2133 lname = NULL;
2134 rname = NULL;
2135
2136 for (temp_list = file_list; temp_list;
2137 temp_list = temp_list->next) {
2138
2139 SAFE_FREE(lname);
2140 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2141 continue;
2142 }
2143 trim_string(lname, "./", "/");
2144
2145 /* check if it's a directory */
2146 if (temp_list->isdir) {
2147 /* if (!recurse) continue; */
2148
2149 SAFE_FREE(quest);
2150 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2151 break;
2152 }
2153 if (prompt && !yesno(quest)) { /* No */
2154 /* Skip the directory */
2155 lname[strlen(lname)-1] = '/';
2156 if (!seek_list(temp_list, lname))
2157 break;
2158 } else { /* Yes */
2159 SAFE_FREE(rname);
2160 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2161 break;
2162 }
2163 normalize_name(rname);
2164 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2165 !do_mkdir(rname)) {
2166 DEBUG (0, ("Unable to make dir, skipping..."));
2167 /* Skip the directory */
2168 lname[strlen(lname)-1] = '/';
2169 if (!seek_list(temp_list, lname)) {
2170 break;
2171 }
2172 }
2173 }
2174 continue;
2175 } else {
2176 SAFE_FREE(quest);
2177 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2178 break;
2179 }
2180 if (prompt && !yesno(quest)) {
2181 /* No */
2182 continue;
2183 }
2184
2185 /* Yes */
2186 SAFE_FREE(rname);
2187 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2188 break;
2189 }
2190 }
2191
2192 normalize_name(rname);
2193
2194 do_put(rname, lname, false);
2195 }
2196 free_file_list(file_list);
2197 SAFE_FREE(quest);
2198 SAFE_FREE(lname);
2199 SAFE_FREE(rname);
2200 }
2201
2202 return 0;
2203}
2204
2205/****************************************************************************
2206 Cancel a print job.
2207****************************************************************************/
2208
2209static int do_cancel(int job)
2210{
2211 if (cli_printjob_del(cli, job)) {
2212 d_printf("Job %d cancelled\n",job);
2213 return 0;
2214 } else {
2215 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
2216 return 1;
2217 }
2218}
2219
2220/****************************************************************************
2221 Cancel a print job.
2222****************************************************************************/
2223
2224static int cmd_cancel(void)
2225{
2226 TALLOC_CTX *ctx = talloc_tos();
2227 char *buf = NULL;
2228 int job;
2229
2230 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2231 d_printf("cancel <jobid> ...\n");
2232 return 1;
2233 }
2234 do {
2235 job = atoi(buf);
2236 do_cancel(job);
2237 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2238
2239 return 0;
2240}
2241
2242/****************************************************************************
2243 Print a file.
2244****************************************************************************/
2245
2246static int cmd_print(void)
2247{
2248 TALLOC_CTX *ctx = talloc_tos();
2249 char *lname = NULL;
2250 char *rname = NULL;
2251 char *p = NULL;
2252
2253 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2254 d_printf("print <filename>\n");
2255 return 1;
2256 }
2257
2258 rname = talloc_strdup(ctx, lname);
2259 if (!rname) {
2260 return 1;
2261 }
2262 p = strrchr_m(rname,'/');
2263 if (p) {
2264 rname = talloc_asprintf(ctx,
2265 "%s-%d",
2266 p+1,
2267 (int)sys_getpid());
2268 }
2269 if (strequal(lname,"-")) {
2270 rname = talloc_asprintf(ctx,
2271 "stdin-%d",
2272 (int)sys_getpid());
2273 }
2274 if (!rname) {
2275 return 1;
2276 }
2277
2278 return do_put(rname, lname, false);
2279}
2280
2281/****************************************************************************
2282 Show a print queue entry.
2283****************************************************************************/
2284
2285static void queue_fn(struct print_job_info *p)
2286{
2287 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2288}
2289
2290/****************************************************************************
2291 Show a print queue.
2292****************************************************************************/
2293
2294static int cmd_queue(void)
2295{
2296 cli_print_queue(cli, queue_fn);
2297 return 0;
2298}
2299
2300/****************************************************************************
2301 Delete some files.
2302****************************************************************************/
2303
2304static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2305 const char *dir)
2306{
2307 TALLOC_CTX *ctx = talloc_tos();
2308 char *mask = NULL;
2309 NTSTATUS status;
2310
2311 mask = talloc_asprintf(ctx,
2312 "%s%c%s",
2313 dir,
2314 CLI_DIRSEP_CHAR,
2315 finfo->name);
2316 if (!mask) {
2317 return NT_STATUS_NO_MEMORY;
2318 }
2319
2320 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
2321 TALLOC_FREE(mask);
2322 return NT_STATUS_OK;
2323 }
2324
2325 status = cli_unlink(cli_state, mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2326 if (!NT_STATUS_IS_OK(status)) {
2327 d_printf("%s deleting remote file %s\n",
2328 nt_errstr(status), mask);
2329 }
2330 TALLOC_FREE(mask);
2331 return status;
2332}
2333
2334/****************************************************************************
2335 Delete some files.
2336****************************************************************************/
2337
2338static int cmd_del(void)
2339{
2340 TALLOC_CTX *ctx = talloc_tos();
2341 char *mask = NULL;
2342 char *buf = NULL;
2343 NTSTATUS status = NT_STATUS_OK;
2344 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2345
2346 if (recurse) {
2347 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2348 }
2349
2350 mask = talloc_strdup(ctx, client_get_cur_dir());
2351 if (!mask) {
2352 return 1;
2353 }
2354 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2355 d_printf("del <filename>\n");
2356 return 1;
2357 }
2358 mask = talloc_asprintf_append(mask, "%s", buf);
2359 if (!mask) {
2360 return 1;
2361 }
2362
2363 status = do_list(mask,attribute,do_del,false,false);
2364 if (!NT_STATUS_IS_OK(status)) {
2365 return 1;
2366 }
2367 return 0;
2368}
2369
2370/****************************************************************************
2371 Wildcard delete some files.
2372****************************************************************************/
2373
2374static int cmd_wdel(void)
2375{
2376 TALLOC_CTX *ctx = talloc_tos();
2377 char *mask = NULL;
2378 char *buf = NULL;
2379 uint16 attribute;
2380 struct cli_state *targetcli;
2381 char *targetname = NULL;
2382 NTSTATUS status;
2383
2384 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2385 d_printf("wdel 0x<attrib> <wcard>\n");
2386 return 1;
2387 }
2388
2389 attribute = (uint16)strtol(buf, (char **)NULL, 16);
2390
2391 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2392 d_printf("wdel 0x<attrib> <wcard>\n");
2393 return 1;
2394 }
2395
2396 mask = talloc_asprintf(ctx, "%s%s",
2397 client_get_cur_dir(),
2398 buf);
2399 if (!mask) {
2400 return 1;
2401 }
2402
2403 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2404 d_printf("cmd_wdel %s: %s\n", mask, cli_errstr(cli));
2405 return 1;
2406 }
2407
2408 status = cli_unlink(targetcli, targetname, attribute);
2409 if (!NT_STATUS_IS_OK(status)) {
2410 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2411 targetname);
2412 }
2413 return 0;
2414}
2415
2416/****************************************************************************
2417****************************************************************************/
2418
2419static int cmd_open(void)
2420{
2421 TALLOC_CTX *ctx = talloc_tos();
2422 char *mask = NULL;
2423 char *buf = NULL;
2424 char *targetname = NULL;
2425 struct cli_state *targetcli;
2426 uint16_t fnum = (uint16_t)-1;
2427
2428 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2429 d_printf("open <filename>\n");
2430 return 1;
2431 }
2432 mask = talloc_asprintf(ctx,
2433 "%s%s",
2434 client_get_cur_dir(),
2435 buf);
2436 if (!mask) {
2437 return 1;
2438 }
2439
2440 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2441 d_printf("open %s: %s\n", mask, cli_errstr(cli));
2442 return 1;
2443 }
2444
2445 if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetname, 0,
2446 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2447 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
2448 if (NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetname, 0,
2449 FILE_READ_DATA, 0,
2450 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
2451 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2452 } else {
2453 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2454 }
2455 } else {
2456 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2457 }
2458 return 0;
2459}
2460
2461static int cmd_posix_encrypt(void)
2462{
2463 TALLOC_CTX *ctx = talloc_tos();
2464 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2465
2466 if (cli->use_kerberos) {
2467 status = cli_gss_smb_encryption_start(cli);
2468 } else {
2469 char *domain = NULL;
2470 char *user = NULL;
2471 char *password = NULL;
2472
2473 if (!next_token_talloc(ctx, &cmd_ptr,&domain,NULL)) {
2474 d_printf("posix_encrypt domain user password\n");
2475 return 1;
2476 }
2477
2478 if (!next_token_talloc(ctx, &cmd_ptr,&user,NULL)) {
2479 d_printf("posix_encrypt domain user password\n");
2480 return 1;
2481 }
2482
2483 if (!next_token_talloc(ctx, &cmd_ptr,&password,NULL)) {
2484 d_printf("posix_encrypt domain user password\n");
2485 return 1;
2486 }
2487
2488 status = cli_raw_ntlm_smb_encryption_start(cli,
2489 user,
2490 password,
2491 domain);
2492 }
2493
2494 if (!NT_STATUS_IS_OK(status)) {
2495 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2496 } else {
2497 d_printf("encryption on\n");
2498 smb_encrypt = true;
2499 }
2500
2501 return 0;
2502}
2503
2504/****************************************************************************
2505****************************************************************************/
2506
2507static int cmd_posix_open(void)
2508{
2509 TALLOC_CTX *ctx = talloc_tos();
2510 char *mask = NULL;
2511 char *buf = NULL;
2512 char *targetname = NULL;
2513 struct cli_state *targetcli;
2514 mode_t mode;
2515 uint16_t fnum;
2516
2517 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2518 d_printf("posix_open <filename> 0<mode>\n");
2519 return 1;
2520 }
2521 mask = talloc_asprintf(ctx,
2522 "%s%s",
2523 client_get_cur_dir(),
2524 buf);
2525 if (!mask) {
2526 return 1;
2527 }
2528
2529 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2530 d_printf("posix_open <filename> 0<mode>\n");
2531 return 1;
2532 }
2533 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2534
2535 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2536 d_printf("posix_open %s: %s\n", mask, cli_errstr(cli));
2537 return 1;
2538 }
2539
2540 if (!NT_STATUS_IS_OK(cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode, &fnum))) {
2541 if (NT_STATUS_IS_OK(cli_posix_open(targetcli, targetname, O_CREAT|O_RDONLY, mode, &fnum))) {
2542 d_printf("posix_open file %s: for readonly fnum %d\n", targetname, fnum);
2543 } else {
2544 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2545 }
2546 } else {
2547 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
2548 }
2549
2550 return 0;
2551}
2552
2553static int cmd_posix_mkdir(void)
2554{
2555 TALLOC_CTX *ctx = talloc_tos();
2556 char *mask = NULL;
2557 char *buf = NULL;
2558 char *targetname = NULL;
2559 struct cli_state *targetcli;
2560 mode_t mode;
2561
2562 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2563 d_printf("posix_mkdir <filename> 0<mode>\n");
2564 return 1;
2565 }
2566 mask = talloc_asprintf(ctx,
2567 "%s%s",
2568 client_get_cur_dir(),
2569 buf);
2570 if (!mask) {
2571 return 1;
2572 }
2573
2574 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2575 d_printf("posix_mkdir <filename> 0<mode>\n");
2576 return 1;
2577 }
2578 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2579
2580 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2581 d_printf("posix_mkdir %s: %s\n", mask, cli_errstr(cli));
2582 return 1;
2583 }
2584
2585 if (!NT_STATUS_IS_OK(cli_posix_mkdir(targetcli, targetname, mode))) {
2586 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2587 } else {
2588 d_printf("posix_mkdir created directory %s\n", targetname);
2589 }
2590 return 0;
2591}
2592
2593static int cmd_posix_unlink(void)
2594{
2595 TALLOC_CTX *ctx = talloc_tos();
2596 char *mask = NULL;
2597 char *buf = NULL;
2598 char *targetname = NULL;
2599 struct cli_state *targetcli;
2600
2601 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2602 d_printf("posix_unlink <filename>\n");
2603 return 1;
2604 }
2605 mask = talloc_asprintf(ctx,
2606 "%s%s",
2607 client_get_cur_dir(),
2608 buf);
2609 if (!mask) {
2610 return 1;
2611 }
2612
2613 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2614 d_printf("posix_unlink %s: %s\n", mask, cli_errstr(cli));
2615 return 1;
2616 }
2617
2618 if (!NT_STATUS_IS_OK(cli_posix_unlink(targetcli, targetname))) {
2619 d_printf("Failed to unlink file %s. %s\n", targetname, cli_errstr(cli));
2620 } else {
2621 d_printf("posix_unlink deleted file %s\n", targetname);
2622 }
2623
2624 return 0;
2625}
2626
2627static int cmd_posix_rmdir(void)
2628{
2629 TALLOC_CTX *ctx = talloc_tos();
2630 char *mask = NULL;
2631 char *buf = NULL;
2632 char *targetname = NULL;
2633 struct cli_state *targetcli;
2634
2635 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2636 d_printf("posix_rmdir <filename>\n");
2637 return 1;
2638 }
2639 mask = talloc_asprintf(ctx,
2640 "%s%s",
2641 client_get_cur_dir(),
2642 buf);
2643 if (!mask) {
2644 return 1;
2645 }
2646
2647 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2648 d_printf("posix_rmdir %s: %s\n", mask, cli_errstr(cli));
2649 return 1;
2650 }
2651
2652 if (!NT_STATUS_IS_OK(cli_posix_rmdir(targetcli, targetname))) {
2653 d_printf("Failed to unlink directory %s. %s\n", targetname, cli_errstr(cli));
2654 } else {
2655 d_printf("posix_rmdir deleted directory %s\n", targetname);
2656 }
2657
2658 return 0;
2659}
2660
2661static int cmd_close(void)
2662{
2663 TALLOC_CTX *ctx = talloc_tos();
2664 char *buf = NULL;
2665 int fnum;
2666
2667 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2668 d_printf("close <fnum>\n");
2669 return 1;
2670 }
2671
2672 fnum = atoi(buf);
2673 /* We really should use the targetcli here.... */
2674 if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
2675 d_printf("close %d: %s\n", fnum, cli_errstr(cli));
2676 return 1;
2677 }
2678 return 0;
2679}
2680
2681static int cmd_posix(void)
2682{
2683 TALLOC_CTX *ctx = talloc_tos();
2684 uint16 major, minor;
2685 uint32 caplow, caphigh;
2686 char *caps;
2687 NTSTATUS status;
2688
2689 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2690 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2691 return 1;
2692 }
2693
2694 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
2695 &caphigh);
2696 if (!NT_STATUS_IS_OK(status)) {
2697 d_printf("Can't get UNIX CIFS extensions version from "
2698 "server: %s\n", nt_errstr(status));
2699 return 1;
2700 }
2701
2702 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2703
2704 caps = talloc_strdup(ctx, "");
2705 if (!caps) {
2706 return 1;
2707 }
2708 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2709 caps = talloc_asprintf_append(caps, "locks ");
2710 if (!caps) {
2711 return 1;
2712 }
2713 }
2714 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2715 caps = talloc_asprintf_append(caps, "acls ");
2716 if (!caps) {
2717 return 1;
2718 }
2719 }
2720 if (caplow & CIFS_UNIX_XATTTR_CAP) {
2721 caps = talloc_asprintf_append(caps, "eas ");
2722 if (!caps) {
2723 return 1;
2724 }
2725 }
2726 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2727 caps = talloc_asprintf_append(caps, "pathnames ");
2728 if (!caps) {
2729 return 1;
2730 }
2731 }
2732 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2733 caps = talloc_asprintf_append(caps, "posix_path_operations ");
2734 if (!caps) {
2735 return 1;
2736 }
2737 }
2738 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
2739 caps = talloc_asprintf_append(caps, "large_read ");
2740 if (!caps) {
2741 return 1;
2742 }
2743 }
2744 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
2745 caps = talloc_asprintf_append(caps, "large_write ");
2746 if (!caps) {
2747 return 1;
2748 }
2749 }
2750 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
2751 caps = talloc_asprintf_append(caps, "posix_encrypt ");
2752 if (!caps) {
2753 return 1;
2754 }
2755 }
2756 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
2757 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
2758 if (!caps) {
2759 return 1;
2760 }
2761 }
2762
2763 if (*caps && caps[strlen(caps)-1] == ' ') {
2764 caps[strlen(caps)-1] = '\0';
2765 }
2766
2767 d_printf("Server supports CIFS capabilities %s\n", caps);
2768
2769 status = cli_set_unix_extensions_capabilities(cli, major, minor,
2770 caplow, caphigh);
2771 if (!NT_STATUS_IS_OK(status)) {
2772 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
2773 nt_errstr(status));
2774 return 1;
2775 }
2776
2777 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2778 CLI_DIRSEP_CHAR = '/';
2779 *CLI_DIRSEP_STR = '/';
2780 client_set_cur_dir(CLI_DIRSEP_STR);
2781 }
2782
2783 return 0;
2784}
2785
2786static int cmd_lock(void)
2787{
2788 TALLOC_CTX *ctx = talloc_tos();
2789 char *buf = NULL;
2790 uint64_t start, len;
2791 enum brl_type lock_type;
2792 int fnum;
2793
2794 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2795 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2796 return 1;
2797 }
2798 fnum = atoi(buf);
2799
2800 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2801 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2802 return 1;
2803 }
2804
2805 if (*buf == 'r' || *buf == 'R') {
2806 lock_type = READ_LOCK;
2807 } else if (*buf == 'w' || *buf == 'W') {
2808 lock_type = WRITE_LOCK;
2809 } else {
2810 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2811 return 1;
2812 }
2813
2814 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2815 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2816 return 1;
2817 }
2818
2819 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2820
2821 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2822 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2823 return 1;
2824 }
2825
2826 len = (uint64_t)strtol(buf, (char **)NULL, 16);
2827
2828 if (!NT_STATUS_IS_OK(cli_posix_lock(cli, fnum, start, len, true, lock_type))) {
2829 d_printf("lock failed %d: %s\n", fnum, cli_errstr(cli));
2830 }
2831
2832 return 0;
2833}
2834
2835static int cmd_unlock(void)
2836{
2837 TALLOC_CTX *ctx = talloc_tos();
2838 char *buf = NULL;
2839 uint64_t start, len;
2840 int fnum;
2841
2842 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2843 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2844 return 1;
2845 }
2846 fnum = atoi(buf);
2847
2848 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2849 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2850 return 1;
2851 }
2852
2853 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2854
2855 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2856 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2857 return 1;
2858 }
2859
2860 len = (uint64_t)strtol(buf, (char **)NULL, 16);
2861
2862 if (!NT_STATUS_IS_OK(cli_posix_unlock(cli, fnum, start, len))) {
2863 d_printf("unlock failed %d: %s\n", fnum, cli_errstr(cli));
2864 }
2865
2866 return 0;
2867}
2868
2869
2870/****************************************************************************
2871 Remove a directory.
2872****************************************************************************/
2873
2874static int cmd_rmdir(void)
2875{
2876 TALLOC_CTX *ctx = talloc_tos();
2877 char *mask = NULL;
2878 char *buf = NULL;
2879 char *targetname = NULL;
2880 struct cli_state *targetcli;
2881
2882 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2883 d_printf("rmdir <dirname>\n");
2884 return 1;
2885 }
2886 mask = talloc_asprintf(ctx,
2887 "%s%s",
2888 client_get_cur_dir(),
2889 buf);
2890 if (!mask) {
2891 return 1;
2892 }
2893
2894 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2895 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
2896 return 1;
2897 }
2898
2899 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetname))) {
2900 d_printf("%s removing remote directory file %s\n",
2901 cli_errstr(targetcli),mask);
2902 }
2903
2904 return 0;
2905}
2906
2907/****************************************************************************
2908 UNIX hardlink.
2909****************************************************************************/
2910
2911static int cmd_link(void)
2912{
2913 TALLOC_CTX *ctx = talloc_tos();
2914 char *oldname = NULL;
2915 char *newname = NULL;
2916 char *buf = NULL;
2917 char *buf2 = NULL;
2918 char *targetname = NULL;
2919 struct cli_state *targetcli;
2920
2921 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
2922 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
2923 d_printf("link <oldname> <newname>\n");
2924 return 1;
2925 }
2926 oldname = talloc_asprintf(ctx,
2927 "%s%s",
2928 client_get_cur_dir(),
2929 buf);
2930 if (!oldname) {
2931 return 1;
2932 }
2933 newname = talloc_asprintf(ctx,
2934 "%s%s",
2935 client_get_cur_dir(),
2936 buf2);
2937 if (!newname) {
2938 return 1;
2939 }
2940
2941 if (!cli_resolve_path(ctx, "", auth_info, cli, oldname, &targetcli, &targetname)) {
2942 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2943 return 1;
2944 }
2945
2946 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2947 d_printf("Server doesn't support UNIX CIFS calls.\n");
2948 return 1;
2949 }
2950
2951 if (!NT_STATUS_IS_OK(cli_posix_hardlink(targetcli, targetname, newname))) {
2952 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
2953 return 1;
2954 }
2955 return 0;
2956}
2957
2958/****************************************************************************
2959 UNIX readlink.
2960****************************************************************************/
2961
2962static int cmd_readlink(void)
2963{
2964 TALLOC_CTX *ctx = talloc_tos();
2965 char *name= NULL;
2966 char *buf = NULL;
2967 char *targetname = NULL;
2968 char linkname[PATH_MAX+1];
2969 struct cli_state *targetcli;
2970
2971 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2972 d_printf("readlink <name>\n");
2973 return 1;
2974 }
2975 name = talloc_asprintf(ctx,
2976 "%s%s",
2977 client_get_cur_dir(),
2978 buf);
2979 if (!name) {
2980 return 1;
2981 }
2982
2983 if (!cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli, &targetname)) {
2984 d_printf("readlink %s: %s\n", name, cli_errstr(cli));
2985 return 1;
2986 }
2987
2988 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2989 d_printf("Server doesn't support UNIX CIFS calls.\n");
2990 return 1;
2991 }
2992
2993 if (!NT_STATUS_IS_OK(cli_posix_readlink(targetcli, name,
2994 linkname, PATH_MAX+1))) {
2995 d_printf("%s readlink on file %s\n",
2996 cli_errstr(targetcli), name);
2997 return 1;
2998 }
2999
3000 d_printf("%s -> %s\n", name, linkname);
3001
3002 return 0;
3003}
3004
3005
3006/****************************************************************************
3007 UNIX symlink.
3008****************************************************************************/
3009
3010static int cmd_symlink(void)
3011{
3012 TALLOC_CTX *ctx = talloc_tos();
3013 char *oldname = NULL;
3014 char *newname = NULL;
3015 char *buf = NULL;
3016 char *buf2 = NULL;
3017 struct cli_state *newcli;
3018
3019 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3020 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3021 d_printf("symlink <oldname> <newname>\n");
3022 return 1;
3023 }
3024 /* Oldname (link target) must be an untouched blob. */
3025 oldname = buf;
3026
3027 newname = talloc_asprintf(ctx,
3028 "%s%s",
3029 client_get_cur_dir(),
3030 buf2);
3031 if (!newname) {
3032 return 1;
3033 }
3034
3035 /* New name must be present in share namespace. */
3036 if (!cli_resolve_path(ctx, "", auth_info, cli, newname, &newcli, &newname)) {
3037 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
3038 return 1;
3039 }
3040
3041 if (!SERVER_HAS_UNIX_CIFS(newcli)) {
3042 d_printf("Server doesn't support UNIX CIFS calls.\n");
3043 return 1;
3044 }
3045
3046 if (!NT_STATUS_IS_OK(cli_posix_symlink(newcli, oldname, newname))) {
3047 d_printf("%s symlinking files (%s -> %s)\n",
3048 cli_errstr(newcli), newname, newname);
3049 return 1;
3050 }
3051
3052 return 0;
3053}
3054
3055/****************************************************************************
3056 UNIX chmod.
3057****************************************************************************/
3058
3059static int cmd_chmod(void)
3060{
3061 TALLOC_CTX *ctx = talloc_tos();
3062 char *src = NULL;
3063 char *buf = NULL;
3064 char *buf2 = NULL;
3065 char *targetname = NULL;
3066 struct cli_state *targetcli;
3067 mode_t mode;
3068
3069 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3070 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3071 d_printf("chmod mode file\n");
3072 return 1;
3073 }
3074 src = talloc_asprintf(ctx,
3075 "%s%s",
3076 client_get_cur_dir(),
3077 buf2);
3078 if (!src) {
3079 return 1;
3080 }
3081
3082 mode = (mode_t)strtol(buf, NULL, 8);
3083
3084 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3085 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
3086 return 1;
3087 }
3088
3089 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3090 d_printf("Server doesn't support UNIX CIFS calls.\n");
3091 return 1;
3092 }
3093
3094 if (!NT_STATUS_IS_OK(cli_posix_chmod(targetcli, targetname, mode))) {
3095 d_printf("%s chmod file %s 0%o\n",
3096 cli_errstr(targetcli), src, (unsigned int)mode);
3097 return 1;
3098 }
3099
3100 return 0;
3101}
3102
3103static const char *filetype_to_str(mode_t mode)
3104{
3105 if (S_ISREG(mode)) {
3106 return "regular file";
3107 } else if (S_ISDIR(mode)) {
3108 return "directory";
3109 } else
3110#ifdef S_ISCHR
3111 if (S_ISCHR(mode)) {
3112 return "character device";
3113 } else
3114#endif
3115#ifdef S_ISBLK
3116 if (S_ISBLK(mode)) {
3117 return "block device";
3118 } else
3119#endif
3120#ifdef S_ISFIFO
3121 if (S_ISFIFO(mode)) {
3122 return "fifo";
3123 } else
3124#endif
3125#ifdef S_ISLNK
3126 if (S_ISLNK(mode)) {
3127 return "symbolic link";
3128 } else
3129#endif
3130#ifdef S_ISSOCK
3131 if (S_ISSOCK(mode)) {
3132 return "socket";
3133 } else
3134#endif
3135 return "";
3136}
3137
3138static char rwx_to_str(mode_t m, mode_t bt, char ret)
3139{
3140 if (m & bt) {
3141 return ret;
3142 } else {
3143 return '-';
3144 }
3145}
3146
3147static char *unix_mode_to_str(char *s, mode_t m)
3148{
3149 char *p = s;
3150 const char *str = filetype_to_str(m);
3151
3152 switch(str[0]) {
3153 case 'd':
3154 *p++ = 'd';
3155 break;
3156 case 'c':
3157 *p++ = 'c';
3158 break;
3159 case 'b':
3160 *p++ = 'b';
3161 break;
3162 case 'f':
3163 *p++ = 'p';
3164 break;
3165 case 's':
3166 *p++ = str[1] == 'y' ? 'l' : 's';
3167 break;
3168 case 'r':
3169 default:
3170 *p++ = '-';
3171 break;
3172 }
3173 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3174 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3175 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3176 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3177 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3178 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3179 *p++ = rwx_to_str(m, S_IROTH, 'r');
3180 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3181 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3182 *p++ = '\0';
3183 return s;
3184}
3185
3186/****************************************************************************
3187 Utility function for UNIX getfacl.
3188****************************************************************************/
3189
3190static char *perms_to_string(fstring permstr, unsigned char perms)
3191{
3192 fstrcpy(permstr, "---");
3193 if (perms & SMB_POSIX_ACL_READ) {
3194 permstr[0] = 'r';
3195 }
3196 if (perms & SMB_POSIX_ACL_WRITE) {
3197 permstr[1] = 'w';
3198 }
3199 if (perms & SMB_POSIX_ACL_EXECUTE) {
3200 permstr[2] = 'x';
3201 }
3202 return permstr;
3203}
3204
3205/****************************************************************************
3206 UNIX getfacl.
3207****************************************************************************/
3208
3209static int cmd_getfacl(void)
3210{
3211 TALLOC_CTX *ctx = talloc_tos();
3212 char *src = NULL;
3213 char *name = NULL;
3214 char *targetname = NULL;
3215 struct cli_state *targetcli;
3216 uint16 major, minor;
3217 uint32 caplow, caphigh;
3218 char *retbuf = NULL;
3219 size_t rb_size = 0;
3220 SMB_STRUCT_STAT sbuf;
3221 uint16 num_file_acls = 0;
3222 uint16 num_dir_acls = 0;
3223 uint16 i;
3224 NTSTATUS status;
3225
3226 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3227 d_printf("getfacl filename\n");
3228 return 1;
3229 }
3230 src = talloc_asprintf(ctx,
3231 "%s%s",
3232 client_get_cur_dir(),
3233 name);
3234 if (!src) {
3235 return 1;
3236 }
3237
3238 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3239 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3240 return 1;
3241 }
3242
3243 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3244 d_printf("Server doesn't support UNIX CIFS calls.\n");
3245 return 1;
3246 }
3247
3248 status = cli_unix_extensions_version(targetcli, &major, &minor,
3249 &caplow, &caphigh);
3250 if (!NT_STATUS_IS_OK(status)) {
3251 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3252 nt_errstr(status));
3253 return 1;
3254 }
3255
3256 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3257 d_printf("This server supports UNIX extensions "
3258 "but doesn't support POSIX ACLs.\n");
3259 return 1;
3260 }
3261
3262 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3263 d_printf("%s getfacl doing a stat on file %s\n",
3264 cli_errstr(targetcli), src);
3265 return 1;
3266 }
3267
3268 if (!NT_STATUS_IS_OK(cli_posix_getfacl(targetcli, targetname, ctx, &rb_size, &retbuf))) {
3269 d_printf("%s getfacl file %s\n",
3270 cli_errstr(targetcli), src);
3271 return 1;
3272 }
3273
3274 /* ToDo : Print out the ACL values. */
3275 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3276 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3277 src, (unsigned int)CVAL(retbuf,0) );
3278 return 1;
3279 }
3280
3281 num_file_acls = SVAL(retbuf,2);
3282 num_dir_acls = SVAL(retbuf,4);
3283 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
3284 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3285 src,
3286 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
3287 (unsigned int)rb_size);
3288 return 1;
3289 }
3290
3291 d_printf("# file: %s\n", src);
3292 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3293
3294 if (num_file_acls == 0 && num_dir_acls == 0) {
3295 d_printf("No acls found.\n");
3296 }
3297
3298 for (i = 0; i < num_file_acls; i++) {
3299 uint32 uorg;
3300 fstring permstring;
3301 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3302 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3303
3304 switch(tagtype) {
3305 case SMB_POSIX_ACL_USER_OBJ:
3306 d_printf("user::");
3307 break;
3308 case SMB_POSIX_ACL_USER:
3309 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3310 d_printf("user:%u:", uorg);
3311 break;
3312 case SMB_POSIX_ACL_GROUP_OBJ:
3313 d_printf("group::");
3314 break;
3315 case SMB_POSIX_ACL_GROUP:
3316 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3317 d_printf("group:%u:", uorg);
3318 break;
3319 case SMB_POSIX_ACL_MASK:
3320 d_printf("mask::");
3321 break;
3322 case SMB_POSIX_ACL_OTHER:
3323 d_printf("other::");
3324 break;
3325 default:
3326 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3327 src, (unsigned int)tagtype );
3328 SAFE_FREE(retbuf);
3329 return 1;
3330 }
3331
3332 d_printf("%s\n", perms_to_string(permstring, perms));
3333 }
3334
3335 for (i = 0; i < num_dir_acls; i++) {
3336 uint32 uorg;
3337 fstring permstring;
3338 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3339 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3340
3341 switch(tagtype) {
3342 case SMB_POSIX_ACL_USER_OBJ:
3343 d_printf("default:user::");
3344 break;
3345 case SMB_POSIX_ACL_USER:
3346 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3347 d_printf("default:user:%u:", uorg);
3348 break;
3349 case SMB_POSIX_ACL_GROUP_OBJ:
3350 d_printf("default:group::");
3351 break;
3352 case SMB_POSIX_ACL_GROUP:
3353 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3354 d_printf("default:group:%u:", uorg);
3355 break;
3356 case SMB_POSIX_ACL_MASK:
3357 d_printf("default:mask::");
3358 break;
3359 case SMB_POSIX_ACL_OTHER:
3360 d_printf("default:other::");
3361 break;
3362 default:
3363 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3364 src, (unsigned int)tagtype );
3365 SAFE_FREE(retbuf);
3366 return 1;
3367 }
3368
3369 d_printf("%s\n", perms_to_string(permstring, perms));
3370 }
3371
3372 return 0;
3373}
3374
3375static void printf_cb(const char *buf, void *private_data)
3376{
3377 printf("%s", buf);
3378}
3379
3380/****************************************************************************
3381 Get the EA list of a file
3382****************************************************************************/
3383
3384static int cmd_geteas(void)
3385{
3386 TALLOC_CTX *ctx = talloc_tos();
3387 char *src = NULL;
3388 char *name = NULL;
3389 char *targetname = NULL;
3390 struct cli_state *targetcli;
3391 NTSTATUS status;
3392 size_t i, num_eas;
3393 struct ea_struct *eas;
3394
3395 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3396 d_printf("geteas filename\n");
3397 return 1;
3398 }
3399 src = talloc_asprintf(ctx,
3400 "%s%s",
3401 client_get_cur_dir(),
3402 name);
3403 if (!src) {
3404 return 1;
3405 }
3406
3407 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3408 &targetname)) {
3409 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3410 return 1;
3411 }
3412
3413 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
3414 &num_eas, &eas);
3415 if (!NT_STATUS_IS_OK(status)) {
3416 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
3417 return 1;
3418 }
3419
3420 for (i=0; i<num_eas; i++) {
3421 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
3422 dump_data_cb(eas[i].value.data, eas[i].value.length, false,
3423 printf_cb, NULL);
3424 d_printf("\n");
3425 }
3426
3427 TALLOC_FREE(eas);
3428
3429 return 0;
3430}
3431
3432/****************************************************************************
3433 Set an EA of a file
3434****************************************************************************/
3435
3436static int cmd_setea(void)
3437{
3438 TALLOC_CTX *ctx = talloc_tos();
3439 char *src = NULL;
3440 char *name = NULL;
3441 char *eaname = NULL;
3442 char *eavalue = NULL;
3443 char *targetname = NULL;
3444 struct cli_state *targetcli;
3445 NTSTATUS status;
3446
3447 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
3448 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
3449 d_printf("setea filename eaname value\n");
3450 return 1;
3451 }
3452 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
3453 eavalue = talloc_strdup(ctx, "");
3454 }
3455 src = talloc_asprintf(ctx,
3456 "%s%s",
3457 client_get_cur_dir(),
3458 name);
3459 if (!src) {
3460 return 1;
3461 }
3462
3463 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3464 &targetname)) {
3465 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3466 return 1;
3467 }
3468
3469 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
3470 strlen(eavalue));
3471 if (!NT_STATUS_IS_OK(status)) {
3472 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
3473 return 1;
3474 }
3475
3476 return 0;
3477}
3478
3479/****************************************************************************
3480 UNIX stat.
3481****************************************************************************/
3482
3483static int cmd_stat(void)
3484{
3485 TALLOC_CTX *ctx = talloc_tos();
3486 char *src = NULL;
3487 char *name = NULL;
3488 char *targetname = NULL;
3489 struct cli_state *targetcli;
3490 fstring mode_str;
3491 SMB_STRUCT_STAT sbuf;
3492 struct tm *lt;
3493 time_t tmp_time;
3494
3495 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3496 d_printf("stat file\n");
3497 return 1;
3498 }
3499 src = talloc_asprintf(ctx,
3500 "%s%s",
3501 client_get_cur_dir(),
3502 name);
3503 if (!src) {
3504 return 1;
3505 }
3506
3507 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3508 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3509 return 1;
3510 }
3511
3512 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3513 d_printf("Server doesn't support UNIX CIFS calls.\n");
3514 return 1;
3515 }
3516
3517 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3518 d_printf("%s stat file %s\n",
3519 cli_errstr(targetcli), src);
3520 return 1;
3521 }
3522
3523 /* Print out the stat values. */
3524 d_printf("File: %s\n", src);
3525 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3526 (double)sbuf.st_ex_size,
3527 (unsigned int)sbuf.st_ex_blocks,
3528 filetype_to_str(sbuf.st_ex_mode));
3529
3530#if defined(S_ISCHR) && defined(S_ISBLK)
3531 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
3532 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3533 (double)sbuf.st_ex_ino,
3534 (unsigned int)sbuf.st_ex_nlink,
3535 unix_dev_major(sbuf.st_ex_rdev),
3536 unix_dev_minor(sbuf.st_ex_rdev));
3537 } else
3538#endif
3539 d_printf("Inode: %.0f\tLinks: %u\n",
3540 (double)sbuf.st_ex_ino,
3541 (unsigned int)sbuf.st_ex_nlink);
3542
3543 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3544 ((int)sbuf.st_ex_mode & 0777),
3545 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
3546 (unsigned int)sbuf.st_ex_uid,
3547 (unsigned int)sbuf.st_ex_gid);
3548
3549 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
3550 lt = localtime(&tmp_time);
3551 if (lt) {
3552 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3553 } else {
3554 fstrcpy(mode_str, "unknown");
3555 }
3556 d_printf("Access: %s\n", mode_str);
3557
3558 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3559 lt = localtime(&tmp_time);
3560 if (lt) {
3561 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3562 } else {
3563 fstrcpy(mode_str, "unknown");
3564 }
3565 d_printf("Modify: %s\n", mode_str);
3566
3567 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
3568 lt = localtime(&tmp_time);
3569 if (lt) {
3570 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3571 } else {
3572 fstrcpy(mode_str, "unknown");
3573 }
3574 d_printf("Change: %s\n", mode_str);
3575
3576 return 0;
3577}
3578
3579
3580/****************************************************************************
3581 UNIX chown.
3582****************************************************************************/
3583
3584static int cmd_chown(void)
3585{
3586 TALLOC_CTX *ctx = talloc_tos();
3587 char *src = NULL;
3588 uid_t uid;
3589 gid_t gid;
3590 char *buf, *buf2, *buf3;
3591 struct cli_state *targetcli;
3592 char *targetname = NULL;
3593
3594 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3595 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
3596 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
3597 d_printf("chown uid gid file\n");
3598 return 1;
3599 }
3600
3601 uid = (uid_t)atoi(buf);
3602 gid = (gid_t)atoi(buf2);
3603
3604 src = talloc_asprintf(ctx,
3605 "%s%s",
3606 client_get_cur_dir(),
3607 buf3);
3608 if (!src) {
3609 return 1;
3610 }
3611 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname) ) {
3612 d_printf("chown %s: %s\n", src, cli_errstr(cli));
3613 return 1;
3614 }
3615
3616 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3617 d_printf("Server doesn't support UNIX CIFS calls.\n");
3618 return 1;
3619 }
3620
3621 if (!NT_STATUS_IS_OK(cli_posix_chown(targetcli, targetname, uid, gid))) {
3622 d_printf("%s chown file %s uid=%d, gid=%d\n",
3623 cli_errstr(targetcli), src, (int)uid, (int)gid);
3624 return 1;
3625 }
3626
3627 return 0;
3628}
3629
3630/****************************************************************************
3631 Rename some file.
3632****************************************************************************/
3633
3634static int cmd_rename(void)
3635{
3636 TALLOC_CTX *ctx = talloc_tos();
3637 char *src, *dest;
3638 char *buf, *buf2;
3639 struct cli_state *targetcli;
3640 char *targetsrc;
3641 char *targetdest;
3642
3643 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3644 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3645 d_printf("rename <src> <dest>\n");
3646 return 1;
3647 }
3648
3649 src = talloc_asprintf(ctx,
3650 "%s%s",
3651 client_get_cur_dir(),
3652 buf);
3653 if (!src) {
3654 return 1;
3655 }
3656
3657 dest = talloc_asprintf(ctx,
3658 "%s%s",
3659 client_get_cur_dir(),
3660 buf2);
3661 if (!dest) {
3662 return 1;
3663 }
3664
3665 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetsrc)) {
3666 d_printf("rename %s: %s\n", src, cli_errstr(cli));
3667 return 1;
3668 }
3669
3670 if (!cli_resolve_path(ctx, "", auth_info, cli, dest, &targetcli, &targetdest)) {
3671 d_printf("rename %s: %s\n", dest, cli_errstr(cli));
3672 return 1;
3673 }
3674
3675 if (!NT_STATUS_IS_OK(cli_rename(targetcli, targetsrc, targetdest))) {
3676 d_printf("%s renaming files %s -> %s \n",
3677 cli_errstr(targetcli),
3678 targetsrc,
3679 targetdest);
3680 return 1;
3681 }
3682
3683 return 0;
3684}
3685
3686/****************************************************************************
3687 Print the volume name.
3688****************************************************************************/
3689
3690static int cmd_volume(void)
3691{
3692 fstring volname;
3693 uint32 serial_num;
3694 time_t create_date;
3695 NTSTATUS status;
3696
3697 status = cli_get_fs_volume_info(cli, volname, &serial_num,
3698 &create_date);
3699 if (!NT_STATUS_IS_OK(status)) {
3700 d_printf("Error %s getting volume info\n", nt_errstr(status));
3701 return 1;
3702 }
3703
3704 d_printf("Volume: |%s| serial number 0x%x\n",
3705 volname, (unsigned int)serial_num);
3706 return 0;
3707}
3708
3709/****************************************************************************
3710 Hard link files using the NT call.
3711****************************************************************************/
3712
3713static int cmd_hardlink(void)
3714{
3715 TALLOC_CTX *ctx = talloc_tos();
3716 char *src, *dest;
3717 char *buf, *buf2;
3718 struct cli_state *targetcli;
3719 char *targetname;
3720
3721 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3722 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3723 d_printf("hardlink <src> <dest>\n");
3724 return 1;
3725 }
3726
3727 src = talloc_asprintf(ctx,
3728 "%s%s",
3729 client_get_cur_dir(),
3730 buf);
3731 if (!src) {
3732 return 1;
3733 }
3734
3735 dest = talloc_asprintf(ctx,
3736 "%s%s",
3737 client_get_cur_dir(),
3738 buf2);
3739 if (!dest) {
3740 return 1;
3741 }
3742
3743 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3744 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
3745 return 1;
3746 }
3747
3748 if (!NT_STATUS_IS_OK(cli_nt_hardlink(targetcli, targetname, dest))) {
3749 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
3750 return 1;
3751 }
3752
3753 return 0;
3754}
3755
3756/****************************************************************************
3757 Toggle the prompt flag.
3758****************************************************************************/
3759
3760static int cmd_prompt(void)
3761{
3762 prompt = !prompt;
3763 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
3764 return 1;
3765}
3766
3767/****************************************************************************
3768 Set the newer than time.
3769****************************************************************************/
3770
3771static int cmd_newer(void)
3772{
3773 TALLOC_CTX *ctx = talloc_tos();
3774 char *buf;
3775 bool ok;
3776 SMB_STRUCT_STAT sbuf;
3777
3778 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
3779 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
3780 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3781 DEBUG(1,("Getting files newer than %s",
3782 time_to_asc(newer_than)));
3783 } else {
3784 newer_than = 0;
3785 }
3786
3787 if (ok && newer_than == 0) {
3788 d_printf("Error setting newer-than time\n");
3789 return 1;
3790 }
3791
3792 return 0;
3793}
3794
3795/****************************************************************************
3796 Set the archive level.
3797****************************************************************************/
3798
3799static int cmd_archive(void)
3800{
3801 TALLOC_CTX *ctx = talloc_tos();
3802 char *buf;
3803
3804 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3805 archive_level = atoi(buf);
3806 } else {
3807 d_printf("Archive level is %d\n",archive_level);
3808 }
3809
3810 return 0;
3811}
3812
3813/****************************************************************************
3814 Toggle the lowercaseflag.
3815****************************************************************************/
3816
3817static int cmd_lowercase(void)
3818{
3819 lowercase = !lowercase;
3820 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
3821 return 0;
3822}
3823
3824/****************************************************************************
3825 Toggle the case sensitive flag.
3826****************************************************************************/
3827
3828static int cmd_setcase(void)
3829{
3830 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
3831
3832 cli_set_case_sensitive(cli, !orig_case_sensitive);
3833 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
3834 "on":"off"));
3835 return 0;
3836}
3837
3838/****************************************************************************
3839 Toggle the showacls flag.
3840****************************************************************************/
3841
3842static int cmd_showacls(void)
3843{
3844 showacls = !showacls;
3845 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
3846 return 0;
3847}
3848
3849
3850/****************************************************************************
3851 Toggle the recurse flag.
3852****************************************************************************/
3853
3854static int cmd_recurse(void)
3855{
3856 recurse = !recurse;
3857 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
3858 return 0;
3859}
3860
3861/****************************************************************************
3862 Toggle the translate flag.
3863****************************************************************************/
3864
3865static int cmd_translate(void)
3866{
3867 translation = !translation;
3868 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
3869 translation?"on":"off"));
3870 return 0;
3871}
3872
3873/****************************************************************************
3874 Do the lcd command.
3875 ****************************************************************************/
3876
3877static int cmd_lcd(void)
3878{
3879 TALLOC_CTX *ctx = talloc_tos();
3880 char *buf;
3881 char *d;
3882
3883 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3884 if (chdir(buf) == -1) {
3885 d_printf("chdir to %s failed (%s)\n",
3886 buf, strerror(errno));
3887 }
3888 }
3889 d = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
3890 if (!d) {
3891 return 1;
3892 }
3893 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
3894 return 0;
3895}
3896
3897/****************************************************************************
3898 Get a file restarting at end of local file.
3899 ****************************************************************************/
3900
3901static int cmd_reget(void)
3902{
3903 TALLOC_CTX *ctx = talloc_tos();
3904 char *local_name = NULL;
3905 char *remote_name = NULL;
3906 char *fname = NULL;
3907 char *p = NULL;
3908
3909 remote_name = talloc_strdup(ctx, client_get_cur_dir());
3910 if (!remote_name) {
3911 return 1;
3912 }
3913
3914 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
3915 d_printf("reget <filename>\n");
3916 return 1;
3917 }
3918 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
3919 if (!remote_name) {
3920 return 1;
3921 }
3922 remote_name = clean_name(ctx,remote_name);
3923 if (!remote_name) {
3924 return 1;
3925 }
3926
3927 local_name = fname;
3928 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
3929 if (p) {
3930 local_name = p;
3931 }
3932
3933 return do_get(remote_name, local_name, true);
3934}
3935
3936/****************************************************************************
3937 Put a file restarting at end of local file.
3938 ****************************************************************************/
3939
3940static int cmd_reput(void)
3941{
3942 TALLOC_CTX *ctx = talloc_tos();
3943 char *local_name = NULL;
3944 char *remote_name = NULL;
3945 char *buf;
3946 SMB_STRUCT_STAT st;
3947
3948 remote_name = talloc_strdup(ctx, client_get_cur_dir());
3949 if (!remote_name) {
3950 return 1;
3951 }
3952
3953 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
3954 d_printf("reput <filename>\n");
3955 return 1;
3956 }
3957
3958 if (!file_exist_stat(local_name, &st, false)) {
3959 d_printf("%s does not exist\n", local_name);
3960 return 1;
3961 }
3962
3963 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
3964 remote_name = talloc_asprintf_append(remote_name,
3965 "%s", buf);
3966 } else {
3967 remote_name = talloc_asprintf_append(remote_name,
3968 "%s", local_name);
3969 }
3970 if (!remote_name) {
3971 return 1;
3972 }
3973
3974 remote_name = clean_name(ctx, remote_name);
3975 if (!remote_name) {
3976 return 1;
3977 }
3978
3979 return do_put(remote_name, local_name, true);
3980}
3981
3982/****************************************************************************
3983 List a share name.
3984 ****************************************************************************/
3985
3986static void browse_fn(const char *name, uint32 m,
3987 const char *comment, void *state)
3988{
3989 const char *typestr = "";
3990
3991 switch (m & 7) {
3992 case STYPE_DISKTREE:
3993 typestr = "Disk";
3994 break;
3995 case STYPE_PRINTQ:
3996 typestr = "Printer";
3997 break;
3998 case STYPE_DEVICE:
3999 typestr = "Device";
4000 break;
4001 case STYPE_IPC:
4002 typestr = "IPC";
4003 break;
4004 }
4005 /* FIXME: If the remote machine returns non-ascii characters
4006 in any of these fields, they can corrupt the output. We
4007 should remove them. */
4008 if (!grepable) {
4009 d_printf("\t%-15s %-10.10s%s\n",
4010 name,typestr,comment);
4011 } else {
4012 d_printf ("%s|%s|%s\n",typestr,name,comment);
4013 }
4014}
4015
4016static bool browse_host_rpc(bool sort)
4017{
4018 NTSTATUS status;
4019 struct rpc_pipe_client *pipe_hnd = NULL;
4020 TALLOC_CTX *frame = talloc_stackframe();
4021 WERROR werr;
4022 struct srvsvc_NetShareInfoCtr info_ctr;
4023 struct srvsvc_NetShareCtr1 ctr1;
4024 uint32_t resume_handle = 0;
4025 uint32_t total_entries = 0;
4026 int i;
4027 struct dcerpc_binding_handle *b;
4028
4029 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
4030 &pipe_hnd);
4031
4032 if (!NT_STATUS_IS_OK(status)) {
4033 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4034 nt_errstr(status)));
4035 TALLOC_FREE(frame);
4036 return false;
4037 }
4038
4039 b = pipe_hnd->binding_handle;
4040
4041 ZERO_STRUCT(info_ctr);
4042 ZERO_STRUCT(ctr1);
4043
4044 info_ctr.level = 1;
4045 info_ctr.ctr.ctr1 = &ctr1;
4046
4047 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4048 pipe_hnd->desthost,
4049 &info_ctr,
4050 0xffffffff,
4051 &total_entries,
4052 &resume_handle,
4053 &werr);
4054
4055 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4056 TALLOC_FREE(pipe_hnd);
4057 TALLOC_FREE(frame);
4058 return false;
4059 }
4060
4061 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4062 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4063 browse_fn(info.name, info.type, info.comment, NULL);
4064 }
4065
4066 TALLOC_FREE(pipe_hnd);
4067 TALLOC_FREE(frame);
4068 return true;
4069}
4070
4071/****************************************************************************
4072 Try and browse available connections on a host.
4073****************************************************************************/
4074
4075static bool browse_host(bool sort)
4076{
4077 int ret;
4078 if (!grepable) {
4079 d_printf("\n\tSharename Type Comment\n");
4080 d_printf("\t--------- ---- -------\n");
4081 }
4082
4083 if (browse_host_rpc(sort)) {
4084 return true;
4085 }
4086
4087 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
4088 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
4089
4090 return (ret != -1);
4091}
4092
4093/****************************************************************************
4094 List a server name.
4095****************************************************************************/
4096
4097static void server_fn(const char *name, uint32 m,
4098 const char *comment, void *state)
4099{
4100
4101 if (!grepable){
4102 d_printf("\t%-16s %s\n", name, comment);
4103 } else {
4104 d_printf("%s|%s|%s\n",(char *)state, name, comment);
4105 }
4106}
4107
4108/****************************************************************************
4109 Try and browse available connections on a host.
4110****************************************************************************/
4111
4112static bool list_servers(const char *wk_grp)
4113{
4114 fstring state;
4115
4116 if (!cli->server_domain)
4117 return false;
4118
4119 if (!grepable) {
4120 d_printf("\n\tServer Comment\n");
4121 d_printf("\t--------- -------\n");
4122 };
4123 fstrcpy( state, "Server" );
4124 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
4125 state);
4126
4127 if (!grepable) {
4128 d_printf("\n\tWorkgroup Master\n");
4129 d_printf("\t--------- -------\n");
4130 };
4131
4132 fstrcpy( state, "Workgroup" );
4133 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
4134 server_fn, state);
4135 return true;
4136}
4137
4138/****************************************************************************
4139 Print or set current VUID
4140****************************************************************************/
4141
4142static int cmd_vuid(void)
4143{
4144 TALLOC_CTX *ctx = talloc_tos();
4145 char *buf;
4146
4147 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4148 d_printf("Current VUID is %d\n", cli->vuid);
4149 return 0;
4150 }
4151
4152 cli->vuid = atoi(buf);
4153 return 0;
4154}
4155
4156/****************************************************************************
4157 Setup a new VUID, by issuing a session setup
4158****************************************************************************/
4159
4160static int cmd_logon(void)
4161{
4162 TALLOC_CTX *ctx = talloc_tos();
4163 char *l_username, *l_password;
4164 NTSTATUS nt_status;
4165
4166 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
4167 d_printf("logon <username> [<password>]\n");
4168 return 0;
4169 }
4170
4171 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
4172 char *pass = getpass("Password: ");
4173 if (pass) {
4174 l_password = talloc_strdup(ctx,pass);
4175 }
4176 }
4177 if (!l_password) {
4178 return 1;
4179 }
4180
4181 nt_status = cli_session_setup(cli, l_username,
4182 l_password, strlen(l_password),
4183 l_password, strlen(l_password),
4184 lp_workgroup());
4185 if (!NT_STATUS_IS_OK(nt_status)) {
4186 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
4187 return -1;
4188 }
4189
4190 d_printf("Current VUID is %d\n", cli->vuid);
4191 return 0;
4192}
4193
4194
4195/****************************************************************************
4196 list active connections
4197****************************************************************************/
4198
4199static int cmd_list_connect(void)
4200{
4201 cli_cm_display(cli);
4202 return 0;
4203}
4204
4205/****************************************************************************
4206 display the current active client connection
4207****************************************************************************/
4208
4209static int cmd_show_connect( void )
4210{
4211 TALLOC_CTX *ctx = talloc_tos();
4212 struct cli_state *targetcli;
4213 char *targetpath;
4214
4215 if (!cli_resolve_path(ctx, "", auth_info, cli, client_get_cur_dir(),
4216 &targetcli, &targetpath ) ) {
4217 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
4218 return 1;
4219 }
4220
4221 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
4222 return 0;
4223}
4224
4225/****************************************************************************
4226 iosize command
4227***************************************************************************/
4228
4229int cmd_iosize(void)
4230{
4231 TALLOC_CTX *ctx = talloc_tos();
4232 char *buf;
4233 int iosize;
4234
4235 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4236 if (!smb_encrypt) {
4237 d_printf("iosize <n> or iosize 0x<n>. "
4238 "Minimum is 16384 (0x4000), "
4239 "max is 16776960 (0xFFFF00)\n");
4240 } else {
4241 d_printf("iosize <n> or iosize 0x<n>. "
4242 "(Encrypted connection) ,"
4243 "Minimum is 16384 (0x4000), "
4244 "max is 130048 (0x1FC00)\n");
4245 }
4246 return 1;
4247 }
4248
4249 iosize = strtol(buf,NULL,0);
4250 if (smb_encrypt && (iosize < 0x4000 || iosize > 0xFC00)) {
4251 d_printf("iosize out of range for encrypted "
4252 "connection (min = 16384 (0x4000), "
4253 "max = 130048 (0x1FC00)");
4254 return 1;
4255 } else if (!smb_encrypt && (iosize < 0x4000 || iosize > 0xFFFF00)) {
4256 d_printf("iosize out of range (min = 16384 (0x4000), "
4257 "max = 16776960 (0xFFFF00)");
4258 return 1;
4259 }
4260
4261 io_bufsize = iosize;
4262 d_printf("iosize is now %d\n", io_bufsize);
4263 return 0;
4264}
4265
4266/****************************************************************************
4267history
4268****************************************************************************/
4269static int cmd_history(void)
4270{
4271#if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
4272 HIST_ENTRY **hlist;
4273 int i;
4274
4275 hlist = history_list();
4276
4277 for (i = 0; hlist && hlist[i]; i++) {
4278 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
4279 }
4280#else
4281 DEBUG(0,("no history without readline support\n"));
4282#endif
4283
4284 return 0;
4285}
4286
4287/* Some constants for completing filename arguments */
4288
4289#define COMPL_NONE 0 /* No completions */
4290#define COMPL_REMOTE 1 /* Complete remote filename */
4291#define COMPL_LOCAL 2 /* Complete local filename */
4292
4293/* This defines the commands supported by this client.
4294 * NOTE: The "!" must be the last one in the list because it's fn pointer
4295 * field is NULL, and NULL in that field is used in process_tok()
4296 * (below) to indicate the end of the list. crh
4297 */
4298static struct {
4299 const char *name;
4300 int (*fn)(void);
4301 const char *description;
4302 char compl_args[2]; /* Completion argument info */
4303} commands[] = {
4304 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4305 {"allinfo",cmd_allinfo,"<file> show all available info",
4306 {COMPL_NONE,COMPL_NONE}},
4307 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
4308 {"archive",cmd_archive,"<level>\n0=ignore archive bit\n1=only get archive files\n2=only get archive files and reset archive bit\n3=get all files and reset archive bit",{COMPL_NONE,COMPL_NONE}},
4309 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
4310 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
4311 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
4312 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
4313 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
4314 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
4315 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
4316 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4317 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4318 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4319 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
4320 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4321 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
4322 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
4323 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
4324 {COMPL_REMOTE, COMPL_LOCAL}},
4325 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4326 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4327 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
4328 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
4329 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
4330 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4331 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4332 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
4333 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4334 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4335 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
4336 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4337 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
4338 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4339 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
4340 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
4341 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
4342 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
4343 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
4344 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
4345 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4346 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4347 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4348 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4349 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
4350 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
4351 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
4352 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
4353 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4354 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
4355 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4356 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4357 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4358 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
4359 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
4360 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
4361 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
4362 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4363 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4364 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
4365 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
4366 {COMPL_REMOTE, COMPL_LOCAL}},
4367 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
4368 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
4369 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4370 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
4371 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
4372 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
4373 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4374 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
4375 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
4376 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4377 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
4378 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
4379 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
4380 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
4381
4382 /* Yes, this must be here, see crh's comment above. */
4383 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
4384 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
4385};
4386
4387/*******************************************************************
4388 Lookup a command string in the list of commands, including
4389 abbreviations.
4390******************************************************************/
4391
4392static int process_tok(char *tok)
4393{
4394 int i = 0, matches = 0;
4395 int cmd=0;
4396 int tok_len = strlen(tok);
4397
4398 while (commands[i].fn != NULL) {
4399 if (strequal(commands[i].name,tok)) {
4400 matches = 1;
4401 cmd = i;
4402 break;
4403 } else if (strnequal(commands[i].name, tok, tok_len)) {
4404 matches++;
4405 cmd = i;
4406 }
4407 i++;
4408 }
4409
4410 if (matches == 0)
4411 return(-1);
4412 else if (matches == 1)
4413 return(cmd);
4414 else
4415 return(-2);
4416}
4417
4418/****************************************************************************
4419 Help.
4420****************************************************************************/
4421
4422static int cmd_help(void)
4423{
4424 TALLOC_CTX *ctx = talloc_tos();
4425 int i=0,j;
4426 char *buf;
4427
4428 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4429 if ((i = process_tok(buf)) >= 0)
4430 d_printf("HELP %s:\n\t%s\n\n",
4431 commands[i].name,commands[i].description);
4432 } else {
4433 while (commands[i].description) {
4434 for (j=0; commands[i].description && (j<5); j++) {
4435 d_printf("%-15s",commands[i].name);
4436 i++;
4437 }
4438 d_printf("\n");
4439 }
4440 }
4441 return 0;
4442}
4443
4444/****************************************************************************
4445 Process a -c command string.
4446****************************************************************************/
4447
4448static int process_command_string(const char *cmd_in)
4449{
4450 TALLOC_CTX *ctx = talloc_tos();
4451 char *cmd = talloc_strdup(ctx, cmd_in);
4452 int rc = 0;
4453
4454 if (!cmd) {
4455 return 1;
4456 }
4457 /* establish the connection if not already */
4458
4459 if (!cli) {
4460 cli = cli_cm_open(talloc_tos(), NULL,
4461 have_ip ? dest_ss_str : desthost,
4462 service, auth_info,
4463 true, smb_encrypt,
4464 max_protocol, port, name_type);
4465 if (!cli) {
4466 return 1;
4467 }
4468 }
4469
4470 while (cmd[0] != '\0') {
4471 char *line;
4472 char *p;
4473 char *tok;
4474 int i;
4475
4476 if ((p = strchr_m(cmd, ';')) == 0) {
4477 line = cmd;
4478 cmd += strlen(cmd);
4479 } else {
4480 *p = '\0';
4481 line = cmd;
4482 cmd = p + 1;
4483 }
4484
4485 /* and get the first part of the command */
4486 cmd_ptr = line;
4487 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
4488 continue;
4489 }
4490
4491 if ((i = process_tok(tok)) >= 0) {
4492 rc = commands[i].fn();
4493 } else if (i == -2) {
4494 d_printf("%s: command abbreviation ambiguous\n",tok);
4495 } else {
4496 d_printf("%s: command not found\n",tok);
4497 }
4498 }
4499
4500 return rc;
4501}
4502
4503#define MAX_COMPLETIONS 100
4504
4505struct completion_remote {
4506 char *dirmask;
4507 char **matches;
4508 int count, samelen;
4509 const char *text;
4510 int len;
4511};
4512
4513static NTSTATUS completion_remote_filter(const char *mnt,
4514 struct file_info *f,
4515 const char *mask,
4516 void *state)
4517{
4518 struct completion_remote *info = (struct completion_remote *)state;
4519
4520 if (info->count >= MAX_COMPLETIONS - 1) {
4521 return NT_STATUS_OK;
4522 }
4523 if (strncmp(info->text, f->name, info->len) != 0) {
4524 return NT_STATUS_OK;
4525 }
4526 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
4527 return NT_STATUS_OK;
4528 }
4529
4530 if ((info->dirmask[0] == 0) && !(f->mode & FILE_ATTRIBUTE_DIRECTORY))
4531 info->matches[info->count] = SMB_STRDUP(f->name);
4532 else {
4533 TALLOC_CTX *ctx = talloc_stackframe();
4534 char *tmp;
4535
4536 tmp = talloc_strdup(ctx,info->dirmask);
4537 if (!tmp) {
4538 TALLOC_FREE(ctx);
4539 return NT_STATUS_NO_MEMORY;
4540 }
4541 tmp = talloc_asprintf_append(tmp, "%s", f->name);
4542 if (!tmp) {
4543 TALLOC_FREE(ctx);
4544 return NT_STATUS_NO_MEMORY;
4545 }
4546 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
4547 tmp = talloc_asprintf_append(tmp, "%s",
4548 CLI_DIRSEP_STR);
4549 }
4550 if (!tmp) {
4551 TALLOC_FREE(ctx);
4552 return NT_STATUS_NO_MEMORY;
4553 }
4554 info->matches[info->count] = SMB_STRDUP(tmp);
4555 TALLOC_FREE(ctx);
4556 }
4557 if (info->matches[info->count] == NULL) {
4558 return NT_STATUS_OK;
4559 }
4560 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
4561 smb_readline_ca_char(0);
4562 }
4563 if (info->count == 1) {
4564 info->samelen = strlen(info->matches[info->count]);
4565 } else {
4566 while (strncmp(info->matches[info->count],
4567 info->matches[info->count-1],
4568 info->samelen) != 0) {
4569 info->samelen--;
4570 }
4571 }
4572 info->count++;
4573 return NT_STATUS_OK;
4574}
4575
4576static char **remote_completion(const char *text, int len)
4577{
4578 TALLOC_CTX *ctx = talloc_stackframe();
4579 char *dirmask = NULL;
4580 char *targetpath = NULL;
4581 struct cli_state *targetcli = NULL;
4582 int i;
4583 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
4584 NTSTATUS status;
4585
4586 /* can't have non-static initialisation on Sun CC, so do it
4587 at run time here */
4588 info.samelen = len;
4589 info.text = text;
4590 info.len = len;
4591
4592 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
4593 if (!info.matches) {
4594 TALLOC_FREE(ctx);
4595 return NULL;
4596 }
4597
4598 /*
4599 * We're leaving matches[0] free to fill it later with the text to
4600 * display: Either the one single match or the longest common subset
4601 * of the matches.
4602 */
4603 info.matches[0] = NULL;
4604 info.count = 1;
4605
4606 for (i = len-1; i >= 0; i--) {
4607 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
4608 break;
4609 }
4610 }
4611
4612 info.text = text+i+1;
4613 info.samelen = info.len = len-i-1;
4614
4615 if (i > 0) {
4616 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
4617 if (!info.dirmask) {
4618 goto cleanup;
4619 }
4620 strncpy(info.dirmask, text, i+1);
4621 info.dirmask[i+1] = 0;
4622 dirmask = talloc_asprintf(ctx,
4623 "%s%*s*",
4624 client_get_cur_dir(),
4625 i-1,
4626 text);
4627 } else {
4628 info.dirmask = SMB_STRDUP("");
4629 if (!info.dirmask) {
4630 goto cleanup;
4631 }
4632 dirmask = talloc_asprintf(ctx,
4633 "%s*",
4634 client_get_cur_dir());
4635 }
4636 if (!dirmask) {
4637 goto cleanup;
4638 }
4639
4640 if (!cli_resolve_path(ctx, "", auth_info, cli, dirmask, &targetcli, &targetpath)) {
4641 goto cleanup;
4642 }
4643 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
4644 completion_remote_filter, (void *)&info);
4645 if (!NT_STATUS_IS_OK(status)) {
4646 goto cleanup;
4647 }
4648
4649 if (info.count == 1) {
4650 /*
4651 * No matches at all, NULL indicates there is nothing
4652 */
4653 SAFE_FREE(info.matches[0]);
4654 SAFE_FREE(info.matches);
4655 TALLOC_FREE(ctx);
4656 return NULL;
4657 }
4658
4659 if (info.count == 2) {
4660 /*
4661 * Exactly one match in matches[1], indicate this is the one
4662 * in matches[0].
4663 */
4664 info.matches[0] = info.matches[1];
4665 info.matches[1] = NULL;
4666 info.count -= 1;
4667 TALLOC_FREE(ctx);
4668 return info.matches;
4669 }
4670
4671 /*
4672 * We got more than one possible match, set the result to the maximum
4673 * common subset
4674 */
4675
4676 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
4677 info.matches[info.count] = NULL;
4678 return info.matches;
4679
4680cleanup:
4681 for (i = 0; i < info.count; i++) {
4682 SAFE_FREE(info.matches[i]);
4683 }
4684 SAFE_FREE(info.matches);
4685 SAFE_FREE(info.dirmask);
4686 TALLOC_FREE(ctx);
4687 return NULL;
4688}
4689
4690static char **completion_fn(const char *text, int start, int end)
4691{
4692 smb_readline_ca_char(' ');
4693
4694 if (start) {
4695 const char *buf, *sp;
4696 int i;
4697 char compl_type;
4698
4699 buf = smb_readline_get_line_buffer();
4700 if (buf == NULL)
4701 return NULL;
4702
4703 sp = strchr(buf, ' ');
4704 if (sp == NULL)
4705 return NULL;
4706
4707 for (i = 0; commands[i].name; i++) {
4708 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
4709 (commands[i].name[sp - buf] == 0)) {
4710 break;
4711 }
4712 }
4713 if (commands[i].name == NULL)
4714 return NULL;
4715
4716 while (*sp == ' ')
4717 sp++;
4718
4719 if (sp == (buf + start))
4720 compl_type = commands[i].compl_args[0];
4721 else
4722 compl_type = commands[i].compl_args[1];
4723
4724 if (compl_type == COMPL_REMOTE)
4725 return remote_completion(text, end - start);
4726 else /* fall back to local filename completion */
4727 return NULL;
4728 } else {
4729 char **matches;
4730 int i, len, samelen = 0, count=1;
4731
4732 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
4733 if (!matches) {
4734 return NULL;
4735 }
4736 matches[0] = NULL;
4737
4738 len = strlen(text);
4739 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
4740 if (strncmp(text, commands[i].name, len) == 0) {
4741 matches[count] = SMB_STRDUP(commands[i].name);
4742 if (!matches[count])
4743 goto cleanup;
4744 if (count == 1)
4745 samelen = strlen(matches[count]);
4746 else
4747 while (strncmp(matches[count], matches[count-1], samelen) != 0)
4748 samelen--;
4749 count++;
4750 }
4751 }
4752
4753 switch (count) {
4754 case 0: /* should never happen */
4755 case 1:
4756 goto cleanup;
4757 case 2:
4758 matches[0] = SMB_STRDUP(matches[1]);
4759 break;
4760 default:
4761 matches[0] = (char *)SMB_MALLOC(samelen+1);
4762 if (!matches[0])
4763 goto cleanup;
4764 strncpy(matches[0], matches[1], samelen);
4765 matches[0][samelen] = 0;
4766 }
4767 matches[count] = NULL;
4768 return matches;
4769
4770cleanup:
4771 for (i = 0; i < count; i++)
4772 free(matches[i]);
4773
4774 free(matches);
4775 return NULL;
4776 }
4777}
4778
4779static bool finished;
4780
4781/****************************************************************************
4782 Make sure we swallow keepalives during idle time.
4783****************************************************************************/
4784
4785static void readline_callback(void)
4786{
4787 static time_t last_t;
4788 struct timespec now;
4789 time_t t;
4790 int ret, revents;
4791
4792 clock_gettime_mono(&now);
4793 t = now.tv_sec;
4794
4795 if (t - last_t < 5)
4796 return;
4797
4798 last_t = t;
4799
4800 again:
4801
4802 if (cli->fd == -1)
4803 return;
4804
4805 /* We deliberately use receive_smb_raw instead of
4806 client_receive_smb as we want to receive
4807 session keepalives and then drop them here.
4808 */
4809
4810 ret = poll_intr_one_fd(cli->fd, POLLIN|POLLHUP, 0, &revents);
4811
4812 if ((ret > 0) && (revents & (POLLIN|POLLHUP|POLLERR))) {
4813 NTSTATUS status;
4814 size_t len;
4815
4816 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
4817
4818 status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize, 0, 0, &len);
4819
4820 if (!NT_STATUS_IS_OK(status)) {
4821 DEBUG(0, ("Read from server failed, maybe it closed "
4822 "the connection\n"));
4823
4824 finished = true;
4825 smb_readline_done();
4826 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
4827 set_smb_read_error(&cli->smb_rw_error,
4828 SMB_READ_EOF);
4829 return;
4830 }
4831
4832 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
4833 set_smb_read_error(&cli->smb_rw_error,
4834 SMB_READ_TIMEOUT);
4835 return;
4836 }
4837
4838 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
4839 return;
4840 }
4841 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
4842 DEBUG(0, ("Read from server "
4843 "returned unexpected packet!\n"));
4844 return;
4845 }
4846
4847 goto again;
4848 }
4849
4850 /* Ping the server to keep the connection alive using SMBecho. */
4851 {
4852 NTSTATUS status;
4853 unsigned char garbage[16];
4854 memset(garbage, 0xf0, sizeof(garbage));
4855 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
4856
4857 if (NT_STATUS_IS_OK(status)) {
4858 return;
4859 }
4860
4861 if (!cli_state_is_connected(cli)) {
4862 DEBUG(0, ("SMBecho failed (%s). The connection is "
4863 "disconnected now\n", nt_errstr(status)));
4864 finished = true;
4865 smb_readline_done();
4866 }
4867 }
4868}
4869
4870/****************************************************************************
4871 Process commands on stdin.
4872****************************************************************************/
4873
4874static int process_stdin(void)
4875{
4876 int rc = 0;
4877
4878 while (!finished) {
4879 TALLOC_CTX *frame = talloc_stackframe();
4880 char *tok = NULL;
4881 char *the_prompt = NULL;
4882 char *line = NULL;
4883 int i;
4884
4885 /* display a prompt */
4886 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
4887 TALLOC_FREE(frame);
4888 break;
4889 }
4890 line = smb_readline(the_prompt, readline_callback, completion_fn);
4891 SAFE_FREE(the_prompt);
4892 if (!line) {
4893 TALLOC_FREE(frame);
4894 break;
4895 }
4896
4897 /* special case - first char is ! */
4898 if (*line == '!') {
4899 if (system(line + 1) == -1) {
4900 d_printf("system() command %s failed.\n",
4901 line+1);
4902 }
4903 SAFE_FREE(line);
4904 TALLOC_FREE(frame);
4905 continue;
4906 }
4907
4908 /* and get the first part of the command */
4909 cmd_ptr = line;
4910 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
4911 TALLOC_FREE(frame);
4912 SAFE_FREE(line);
4913 continue;
4914 }
4915
4916 if ((i = process_tok(tok)) >= 0) {
4917 rc = commands[i].fn();
4918 } else if (i == -2) {
4919 d_printf("%s: command abbreviation ambiguous\n",tok);
4920 } else {
4921 d_printf("%s: command not found\n",tok);
4922 }
4923 SAFE_FREE(line);
4924 TALLOC_FREE(frame);
4925 }
4926 return rc;
4927}
4928
4929/****************************************************************************
4930 Process commands from the client.
4931****************************************************************************/
4932
4933static int process(const char *base_directory)
4934{
4935 int rc = 0;
4936
4937 cli = cli_cm_open(talloc_tos(), NULL,
4938 have_ip ? dest_ss_str : desthost,
4939 service, auth_info, true, smb_encrypt,
4940 max_protocol, port, name_type);
4941 if (!cli) {
4942 return 1;
4943 }
4944
4945 if (base_directory && *base_directory) {
4946 rc = do_cd(base_directory);
4947 if (rc) {
4948 cli_shutdown(cli);
4949 return rc;
4950 }
4951 }
4952
4953 if (cmdstr) {
4954 rc = process_command_string(cmdstr);
4955 } else {
4956 process_stdin();
4957 }
4958
4959 cli_shutdown(cli);
4960 return rc;
4961}
4962
4963/****************************************************************************
4964 Handle a -L query.
4965****************************************************************************/
4966
4967static int do_host_query(const char *query_host)
4968{
4969 cli = cli_cm_open(talloc_tos(), NULL,
4970 have_ip ? dest_ss_str : query_host, "IPC$", auth_info, true, smb_encrypt,
4971 max_protocol, port, name_type);
4972 if (!cli)
4973 return 1;
4974
4975 browse_host(true);
4976
4977 /* Ensure that the host can do IPv4 */
4978
4979 if (!interpret_addr(query_host)) {
4980 struct sockaddr_storage ss;
4981 if (interpret_string_addr(&ss, query_host, 0) &&
4982 (ss.ss_family != AF_INET)) {
4983 d_printf("%s is an IPv6 address -- no workgroup available\n",
4984 query_host);
4985 return 1;
4986 }
4987 }
4988
4989 if (port != 139) {
4990
4991 /* Workgroups simply don't make sense over anything
4992 else but port 139... */
4993
4994 cli_shutdown(cli);
4995 cli = cli_cm_open(talloc_tos(), NULL,
4996 have_ip ? dest_ss_str : query_host, "IPC$",
4997 auth_info, true, smb_encrypt,
4998 max_protocol, 139, name_type);
4999 }
5000
5001 if (cli == NULL) {
5002 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
5003 return 1;
5004 }
5005
5006 list_servers(lp_workgroup());
5007
5008 cli_shutdown(cli);
5009
5010 return(0);
5011}
5012
5013/****************************************************************************
5014 Handle a tar operation.
5015****************************************************************************/
5016
5017static int do_tar_op(const char *base_directory)
5018{
5019 int ret;
5020
5021 /* do we already have a connection? */
5022 if (!cli) {
5023 cli = cli_cm_open(talloc_tos(), NULL,
5024 have_ip ? dest_ss_str : desthost,
5025 service, auth_info, true, smb_encrypt,
5026 max_protocol, port, name_type);
5027 if (!cli)
5028 return 1;
5029 }
5030
5031 recurse=true;
5032
5033 if (base_directory && *base_directory) {
5034 ret = do_cd(base_directory);
5035 if (ret) {
5036 cli_shutdown(cli);
5037 return ret;
5038 }
5039 }
5040
5041 ret=process_tar();
5042
5043 cli_shutdown(cli);
5044
5045 return(ret);
5046}
5047
5048/****************************************************************************
5049 Handle a message operation.
5050****************************************************************************/
5051
5052static int do_message_op(struct user_auth_info *a_info)
5053{
5054 struct sockaddr_storage ss;
5055 struct nmb_name called, calling;
5056 fstring server_name;
5057 char name_type_hex[10];
5058 int msg_port;
5059 NTSTATUS status;
5060
5061 make_nmb_name(&calling, calling_name, 0x0);
5062 make_nmb_name(&called , desthost, name_type);
5063
5064 fstrcpy(server_name, desthost);
5065 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
5066 fstrcat(server_name, name_type_hex);
5067
5068 zero_sockaddr(&ss);
5069 if (have_ip)
5070 ss = dest_ss;
5071
5072 /* we can only do messages over port 139 (to windows clients at least) */
5073
5074 msg_port = port ? port : 139;
5075
5076 if (!(cli=cli_initialise())) {
5077 d_printf("Connection to %s failed\n", desthost);
5078 return 1;
5079 }
5080 cli_set_port(cli, msg_port);
5081
5082 status = cli_connect(cli, server_name, &ss);
5083 if (!NT_STATUS_IS_OK(status)) {
5084 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
5085 return 1;
5086 }
5087
5088 if (!cli_session_request(cli, &calling, &called)) {
5089 d_printf("session request failed\n");
5090 cli_shutdown(cli);
5091 return 1;
5092 }
5093
5094 send_message(get_cmdline_auth_info_username(a_info));
5095 cli_shutdown(cli);
5096
5097 return 0;
5098}
5099
5100/****************************************************************************
5101 main program
5102****************************************************************************/
5103
5104 int main(int argc,char *argv[])
5105{
5106 char *base_directory = NULL;
5107 int opt;
5108 char *query_host = NULL;
5109 bool message = false;
5110 static const char *new_name_resolve_order = NULL;
5111 poptContext pc;
5112 char *p;
5113 int rc = 0;
5114 fstring new_workgroup;
5115 bool tar_opt = false;
5116 bool service_opt = false;
5117 struct poptOption long_options[] = {
5118 POPT_AUTOHELP
5119
5120 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
5121 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
5122 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
5123 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
5124 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
5125 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
5126 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
5127 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
5128 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
5129 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
5130 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
5131 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
5132 { "browse", 'B', POPT_ARG_NONE, NULL, 'B', "Browse SMB servers using DNS" },
5133 POPT_COMMON_SAMBA
5134 POPT_COMMON_CONNECTION
5135 POPT_COMMON_CREDENTIALS
5136 POPT_TABLEEND
5137 };
5138 TALLOC_CTX *frame = talloc_stackframe();
5139
5140 if (!client_set_cur_dir("\\")) {
5141 exit(ENOMEM);
5142 }
5143
5144 /* initialize the workgroup name so we can determine whether or
5145 not it was set by a command line option */
5146
5147 set_global_myworkgroup( "" );
5148 set_global_myname( "" );
5149
5150 /* set default debug level to 1 regardless of what smb.conf sets */
5151 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
5152 load_case_tables();
5153
5154 lp_set_cmdline("log level", "1");
5155
5156 auth_info = user_auth_info_init(frame);
5157 if (auth_info == NULL) {
5158 exit(1);
5159 }
5160 popt_common_set_auth_info(auth_info);
5161
5162 /* skip argv(0) */
5163 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
5164 poptSetOtherOptionHelp(pc, "service <password>");
5165
5166 lp_set_in_client(true); /* Make sure that we tell lp_load we are */
5167
5168 while ((opt = poptGetNextOpt(pc)) != -1) {
5169
5170 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
5171 /* I see no other way to keep things sane --SSS */
5172 if (tar_opt == true) {
5173 while (poptPeekArg(pc)) {
5174 poptGetArg(pc);
5175 }
5176 tar_opt = false;
5177 }
5178
5179 /* if the service has not yet been specified lets see if it is available in the popt stack */
5180 if (!service_opt && poptPeekArg(pc)) {
5181 service = talloc_strdup(frame, poptGetArg(pc));
5182 if (!service) {
5183 exit(ENOMEM);
5184 }
5185 service_opt = true;
5186 }
5187
5188 /* if the service has already been retrieved then check if we have also a password */
5189 if (service_opt
5190 && (!get_cmdline_auth_info_got_pass(auth_info))
5191 && poptPeekArg(pc)) {
5192 set_cmdline_auth_info_password(auth_info,
5193 poptGetArg(pc));
5194 }
5195
5196 switch (opt) {
5197 case 'M':
5198 /* Messages are sent to NetBIOS name type 0x3
5199 * (Messenger Service). Make sure we default
5200 * to port 139 instead of port 445. srl,crh
5201 */
5202 name_type = 0x03;
5203 desthost = talloc_strdup(frame,poptGetOptArg(pc));
5204 if (!desthost) {
5205 exit(ENOMEM);
5206 }
5207 if( !port )
5208 port = 139;
5209 message = true;
5210 break;
5211 case 'I':
5212 {
5213 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
5214 exit(1);
5215 }
5216 have_ip = true;
5217 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
5218 }
5219 break;
5220 case 'E':
5221 setup_logging("smbclient", DEBUG_STDERR );
5222 display_set_stderr();
5223 break;
5224
5225 case 'L':
5226 query_host = talloc_strdup(frame, poptGetOptArg(pc));
5227 if (!query_host) {
5228 exit(ENOMEM);
5229 }
5230 break;
5231 case 'm':
5232 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
5233 break;
5234 case 'T':
5235 /* We must use old option processing for this. Find the
5236 * position of the -T option in the raw argv[]. */
5237 {
5238 int i;
5239 for (i = 1; i < argc; i++) {
5240 if (strncmp("-T", argv[i],2)==0)
5241 break;
5242 }
5243 i++;
5244 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
5245 poptPrintUsage(pc, stderr, 0);
5246 exit(1);
5247 }
5248 }
5249 /* this must be the last option, mark we have parsed it so that we know we have */
5250 tar_opt = true;
5251 break;
5252 case 'D':
5253 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
5254 if (!base_directory) {
5255 exit(ENOMEM);
5256 }
5257 break;
5258 case 'g':
5259 grepable=true;
5260 break;
5261 case 'e':
5262 smb_encrypt=true;
5263 break;
5264 case 'B':
5265 return(do_smb_browse());
5266
5267 }
5268 }
5269
5270 /* We may still have some leftovers after the last popt option has been called */
5271 if (tar_opt == true) {
5272 while (poptPeekArg(pc)) {
5273 poptGetArg(pc);
5274 }
5275 tar_opt = false;
5276 }
5277
5278 /* if the service has not yet been specified lets see if it is available in the popt stack */
5279 if (!service_opt && poptPeekArg(pc)) {
5280 service = talloc_strdup(frame,poptGetArg(pc));
5281 if (!service) {
5282 exit(ENOMEM);
5283 }
5284 service_opt = true;
5285 }
5286
5287 /* if the service has already been retrieved then check if we have also a password */
5288 if (service_opt
5289 && !get_cmdline_auth_info_got_pass(auth_info)
5290 && poptPeekArg(pc)) {
5291 set_cmdline_auth_info_password(auth_info,
5292 poptGetArg(pc));
5293 }
5294
5295 /* save the workgroup...
5296
5297 FIXME!! do we need to do this for other options as well
5298 (or maybe a generic way to keep lp_load() from overwriting
5299 everything)? */
5300
5301 fstrcpy( new_workgroup, lp_workgroup() );
5302 calling_name = talloc_strdup(frame, global_myname() );
5303 if (!calling_name) {
5304 exit(ENOMEM);
5305 }
5306
5307 if ( override_logfile )
5308 setup_logging( lp_logfile(), DEBUG_FILE );
5309
5310 if (!lp_load(get_dyn_CONFIGFILE(),true,false,false,true)) {
5311 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
5312 argv[0], get_dyn_CONFIGFILE());
5313 }
5314
5315 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
5316 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
5317 exit(-1);
5318 }
5319
5320 load_interfaces();
5321
5322 if (service_opt && service) {
5323 size_t len;
5324
5325 /* Convert any '/' characters in the service name to '\' characters */
5326 string_replace(service, '/','\\');
5327 if (count_chars(service,'\\') < 3) {
5328 d_printf("\n%s: Not enough '\\' characters in service\n",service);
5329 poptPrintUsage(pc, stderr, 0);
5330 exit(1);
5331 }
5332 /* Remove trailing slashes */
5333 len = strlen(service);
5334 while(len > 0 && service[len - 1] == '\\') {
5335 --len;
5336 service[len] = '\0';
5337 }
5338 }
5339
5340 if ( strlen(new_workgroup) != 0 ) {
5341 set_global_myworkgroup( new_workgroup );
5342 }
5343
5344 if ( strlen(calling_name) != 0 ) {
5345 set_global_myname( calling_name );
5346 } else {
5347 TALLOC_FREE(calling_name);
5348 calling_name = talloc_strdup(frame, global_myname() );
5349 }
5350
5351 smb_encrypt = get_cmdline_auth_info_smb_encrypt(auth_info);
5352 if (!init_names()) {
5353 fprintf(stderr, "init_names() failed\n");
5354 exit(1);
5355 }
5356
5357 if(new_name_resolve_order)
5358 lp_set_name_resolve_order(new_name_resolve_order);
5359
5360 if (!tar_type && !query_host && !service && !message) {
5361 poptPrintUsage(pc, stderr, 0);
5362 exit(1);
5363 }
5364
5365 poptFreeContext(pc);
5366
5367 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
5368
5369 /* Ensure we have a password (or equivalent). */
5370 set_cmdline_auth_info_getpass(auth_info);
5371
5372 if (tar_type) {
5373 if (cmdstr)
5374 process_command_string(cmdstr);
5375 return do_tar_op(base_directory);
5376 }
5377
5378 if (query_host && *query_host) {
5379 char *qhost = query_host;
5380 char *slash;
5381
5382 while (*qhost == '\\' || *qhost == '/')
5383 qhost++;
5384
5385 if ((slash = strchr_m(qhost, '/'))
5386 || (slash = strchr_m(qhost, '\\'))) {
5387 *slash = 0;
5388 }
5389
5390 if ((p=strchr_m(qhost, '#'))) {
5391 *p = 0;
5392 p++;
5393 sscanf(p, "%x", &name_type);
5394 }
5395
5396 return do_host_query(qhost);
5397 }
5398
5399 if (message) {
5400 return do_message_op(auth_info);
5401 }
5402
5403 if (process(base_directory)) {
5404 return 1;
5405 }
5406
5407 TALLOC_FREE(frame);
5408 return rc;
5409}
Note: See TracBrowser for help on using the repository browser.