source: branches/samba-3.5.x/source3/client/client.c@ 732

Last change on this file since 732 was 732, checked in by Silvan Scherrer, 13 years ago

Samba Server 3.5: update branche to 3.5.12

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