[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / client / client.c
blob0cc14330cc98747a66c0e7cead175598c8d2d1fc
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
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
25 #include "client/client_proto.h"
26 #include "include/rpc_client.h"
27 #ifndef REGISTER
28 #define REGISTER 0
29 #endif
31 extern BOOL AllowDebugChange;
32 extern BOOL override_logfile;
33 extern char tar_type;
34 extern BOOL in_client;
35 static int port = 0;
36 pstring cur_dir = "\\";
37 static pstring cd_path = "";
38 static pstring service;
39 static pstring desthost;
40 static pstring username;
41 static pstring calling_name;
42 static BOOL grepable=False;
43 static char *cmdstr = NULL;
45 static int io_bufsize = 64512;
47 static int name_type = 0x20;
48 extern int max_protocol;
50 static int process_tok(pstring tok);
51 static int cmd_help(void);
53 static TALLOC_CTX *ctx;
54 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
55 static pstring cwd;
57 /* 30 second timeout on most commands */
58 #define CLIENT_TIMEOUT (30*1000)
59 #define SHORT_TIMEOUT (5*1000)
61 /* value for unused fid field in trans2 secondary request */
62 #define FID_UNUSED (0xFFFF)
64 time_t newer_than = 0;
65 static int archive_level = 0;
67 static BOOL translation = False;
68 static BOOL have_ip;
70 /* clitar bits insert */
71 extern int blocksize;
72 extern BOOL tar_inc;
73 extern BOOL tar_reset;
74 /* clitar bits end */
77 static BOOL prompt = True;
79 static BOOL recurse = False;
80 static BOOL showacls = False;
81 BOOL lowercase = False;
83 static struct in_addr dest_ip;
85 #define SEPARATORS " \t\n\r"
87 static BOOL abort_mget = True;
89 static pstring fileselection = "";
91 extern file_info def_finfo;
93 /* timing globals */
94 SMB_BIG_UINT get_total_size = 0;
95 unsigned int get_total_time_ms = 0;
96 static SMB_BIG_UINT put_total_size = 0;
97 static unsigned int put_total_time_ms = 0;
99 /* totals globals */
100 static double dir_total;
102 /* root cli_state connection */
104 struct cli_state *cli;
106 static char CLI_DIRSEP_CHAR = '\\';
107 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
109 /****************************************************************************
110 Write to a local file with CR/LF->LF translation if appropriate. Return the
111 number taken from the buffer. This may not equal the number written.
112 ****************************************************************************/
114 static int writefile(int f, char *b, int n)
116 int i;
118 if (!translation) {
119 return write(f,b,n);
122 i = 0;
123 while (i < n) {
124 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
125 b++;i++;
127 if (write(f, b, 1) != 1) {
128 break;
130 b++;
131 i++;
134 return(i);
137 /****************************************************************************
138 Read from a file with LF->CR/LF translation if appropriate. Return the
139 number read. read approx n bytes.
140 ****************************************************************************/
142 static int readfile(char *b, int n, XFILE *f)
144 int i;
145 int c;
147 if (!translation)
148 return x_fread(b,1,n,f);
150 i = 0;
151 while (i < (n - 1) && (i < BUFFER_SIZE)) {
152 if ((c = x_getc(f)) == EOF) {
153 break;
156 if (c == '\n') { /* change all LFs to CR/LF */
157 b[i++] = '\r';
160 b[i++] = c;
163 return(i);
166 /****************************************************************************
167 Send a message.
168 ****************************************************************************/
170 static void send_message(void)
172 int total_len = 0;
173 int grp_id;
175 if (!cli_message_start(cli, desthost, username, &grp_id)) {
176 d_printf("message start: %s\n", cli_errstr(cli));
177 return;
181 d_printf("Connected. Type your message, ending it with a Control-D\n");
183 while (!feof(stdin) && total_len < 1600) {
184 int maxlen = MIN(1600 - total_len,127);
185 pstring msg;
186 int l=0;
187 int c;
189 ZERO_ARRAY(msg);
191 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
192 if (c == '\n')
193 msg[l++] = '\r';
194 msg[l] = c;
197 if ((total_len > 0) && (strlen(msg) == 0)) {
198 break;
201 if (!cli_message_text(cli, msg, l, grp_id)) {
202 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
203 return;
206 total_len += l;
209 if (total_len >= 1600)
210 d_printf("the message was truncated to 1600 bytes\n");
211 else
212 d_printf("sent %d bytes\n",total_len);
214 if (!cli_message_end(cli, grp_id)) {
215 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
216 return;
220 /****************************************************************************
221 Check the space on a device.
222 ****************************************************************************/
224 static int do_dskattr(void)
226 int total, bsize, avail;
227 struct cli_state *targetcli;
228 pstring targetpath;
230 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
231 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
232 return 1;
235 if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
236 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli));
237 return 1;
240 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
241 total, bsize, avail);
243 return 0;
246 /****************************************************************************
247 Show cd/pwd.
248 ****************************************************************************/
250 static int cmd_pwd(void)
252 d_printf("Current directory is %s",service);
253 d_printf("%s\n",cur_dir);
254 return 0;
257 /****************************************************************************
258 Change directory - inner section.
259 ****************************************************************************/
261 static int do_cd(char *newdir)
263 char *p = newdir;
264 pstring saved_dir;
265 pstring dname;
266 pstring targetpath;
267 struct cli_state *targetcli;
268 SMB_STRUCT_STAT sbuf;
269 uint32 attributes;
270 int ret = 1;
272 dos_format(newdir);
274 /* Save the current directory in case the new directory is invalid */
276 pstrcpy(saved_dir, cur_dir);
278 if (*p == CLI_DIRSEP_CHAR) {
279 pstrcpy(cur_dir,p);
280 } else {
281 pstrcat(cur_dir,p);
282 if ((cur_dir[0] != '\0') && (*(cur_dir+strlen(cur_dir)-1) != CLI_DIRSEP_CHAR)) {
283 pstrcat(cur_dir, CLI_DIRSEP_STR);
287 clean_name(cur_dir);
288 pstrcpy( dname, cur_dir );
290 if ( !cli_resolve_path( "", cli, dname, &targetcli, targetpath ) ) {
291 d_printf("cd %s: %s\n", dname, cli_errstr(cli));
292 pstrcpy(cur_dir,saved_dir);
293 goto out;
296 if (strequal(targetpath,CLI_DIRSEP_STR )) {
297 return 0;
300 /* Use a trans2_qpathinfo to test directories for modern servers.
301 Except Win9x doesn't support the qpathinfo_basic() call..... */
303 if ( targetcli->protocol > PROTOCOL_LANMAN2 && !targetcli->win95 ) {
304 if ( !cli_qpathinfo_basic( targetcli, targetpath, &sbuf, &attributes ) ) {
305 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
306 pstrcpy(cur_dir,saved_dir);
307 goto out;
310 if ( !(attributes&FILE_ATTRIBUTE_DIRECTORY) ) {
311 d_printf("cd %s: not a directory\n", dname);
312 pstrcpy(cur_dir,saved_dir);
313 goto out;
315 } else {
316 pstrcat( targetpath, CLI_DIRSEP_STR );
317 clean_name( targetpath );
319 if ( !cli_chkpath(targetcli, targetpath) ) {
320 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
321 pstrcpy(cur_dir,saved_dir);
322 goto out;
326 ret = 0;
328 out:
330 pstrcpy(cd_path,cur_dir);
331 return ret;
334 /****************************************************************************
335 Change directory.
336 ****************************************************************************/
338 static int cmd_cd(void)
340 pstring buf;
341 int rc = 0;
343 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
344 rc = do_cd(buf);
345 else
346 d_printf("Current directory is %s\n",cur_dir);
348 return rc;
351 /*******************************************************************
352 Decide if a file should be operated on.
353 ********************************************************************/
355 static BOOL do_this_one(file_info *finfo)
357 if (finfo->mode & aDIR)
358 return(True);
360 if (*fileselection &&
361 !mask_match(finfo->name,fileselection,False)) {
362 DEBUG(3,("mask_match %s failed\n", finfo->name));
363 return False;
366 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
367 DEBUG(3,("newer_than %s failed\n", finfo->name));
368 return(False);
371 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
372 DEBUG(3,("archive %s failed\n", finfo->name));
373 return(False);
376 return(True);
379 /****************************************************************************
380 Display info about a file.
381 ****************************************************************************/
383 static void display_finfo(file_info *finfo)
385 if (do_this_one(finfo)) {
386 time_t t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
387 if (!showacls) {
388 d_printf(" %-30s%7.7s %8.0f %s",
389 finfo->name,
390 attrib_string(finfo->mode),
391 (double)finfo->size,
392 time_to_asc(t));
393 dir_total += finfo->size;
394 } else {
395 pstring afname;
396 int fnum;
398 /* skip if this is . or .. */
399 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
400 return;
401 /* create absolute filename for cli_nt_create() FIXME */
402 pstrcpy( afname, cwd);
403 pstrcat( afname, CLI_DIRSEP_STR);
404 pstrcat( afname, finfo->name);
405 /* print file meta date header */
406 d_printf( "FILENAME:%s\n", afname);
407 d_printf( "MODE:%s\n", attrib_string(finfo->mode));
408 d_printf( "SIZE:%.0f\n", (double)finfo->size);
409 d_printf( "MTIME:%s", time_to_asc(t));
410 fnum = cli_nt_create(finfo->cli, afname, CREATE_ACCESS_READ);
411 if (fnum == -1) {
412 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
413 afname,
414 cli_errstr( finfo->cli)));
415 } else {
416 SEC_DESC *sd = NULL;
417 sd = cli_query_secdesc(finfo->cli, fnum, ctx);
418 if (!sd) {
419 DEBUG( 0, ("display_finfo() failed to "
420 "get security descriptor: %s",
421 cli_errstr( finfo->cli)));
422 } else {
423 display_sec_desc(sd);
430 /****************************************************************************
431 Accumulate size of a file.
432 ****************************************************************************/
434 static void do_du(file_info *finfo)
436 if (do_this_one(finfo)) {
437 dir_total += finfo->size;
441 static BOOL do_list_recurse;
442 static BOOL do_list_dirs;
443 static char *do_list_queue = 0;
444 static long do_list_queue_size = 0;
445 static long do_list_queue_start = 0;
446 static long do_list_queue_end = 0;
447 static void (*do_list_fn)(file_info *);
449 /****************************************************************************
450 Functions for do_list_queue.
451 ****************************************************************************/
454 * The do_list_queue is a NUL-separated list of strings stored in a
455 * char*. Since this is a FIFO, we keep track of the beginning and
456 * ending locations of the data in the queue. When we overflow, we
457 * double the size of the char*. When the start of the data passes
458 * the midpoint, we move everything back. This is logically more
459 * complex than a linked list, but easier from a memory management
460 * angle. In any memory error condition, do_list_queue is reset.
461 * Functions check to ensure that do_list_queue is non-NULL before
462 * accessing it.
465 static void reset_do_list_queue(void)
467 SAFE_FREE(do_list_queue);
468 do_list_queue_size = 0;
469 do_list_queue_start = 0;
470 do_list_queue_end = 0;
473 static void init_do_list_queue(void)
475 reset_do_list_queue();
476 do_list_queue_size = 1024;
477 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
478 if (do_list_queue == 0) {
479 d_printf("malloc fail for size %d\n",
480 (int)do_list_queue_size);
481 reset_do_list_queue();
482 } else {
483 memset(do_list_queue, 0, do_list_queue_size);
487 static void adjust_do_list_queue(void)
490 * If the starting point of the queue is more than half way through,
491 * move everything toward the beginning.
494 if (do_list_queue == NULL) {
495 DEBUG(4,("do_list_queue is empty\n"));
496 do_list_queue_start = do_list_queue_end = 0;
497 return;
500 if (do_list_queue_start == do_list_queue_end) {
501 DEBUG(4,("do_list_queue is empty\n"));
502 do_list_queue_start = do_list_queue_end = 0;
503 *do_list_queue = '\0';
504 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
505 DEBUG(4,("sliding do_list_queue backward\n"));
506 memmove(do_list_queue,
507 do_list_queue + do_list_queue_start,
508 do_list_queue_end - do_list_queue_start);
509 do_list_queue_end -= do_list_queue_start;
510 do_list_queue_start = 0;
514 static void add_to_do_list_queue(const char* entry)
516 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
517 while (new_end > do_list_queue_size) {
518 do_list_queue_size *= 2;
519 DEBUG(4,("enlarging do_list_queue to %d\n",
520 (int)do_list_queue_size));
521 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
522 if (! do_list_queue) {
523 d_printf("failure enlarging do_list_queue to %d bytes\n",
524 (int)do_list_queue_size);
525 reset_do_list_queue();
526 } else {
527 memset(do_list_queue + do_list_queue_size / 2,
528 0, do_list_queue_size / 2);
531 if (do_list_queue) {
532 safe_strcpy_base(do_list_queue + do_list_queue_end,
533 entry, do_list_queue, do_list_queue_size);
534 do_list_queue_end = new_end;
535 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
536 entry, (int)do_list_queue_start, (int)do_list_queue_end));
540 static char *do_list_queue_head(void)
542 return do_list_queue + do_list_queue_start;
545 static void remove_do_list_queue_head(void)
547 if (do_list_queue_end > do_list_queue_start) {
548 do_list_queue_start += strlen(do_list_queue_head()) + 1;
549 adjust_do_list_queue();
550 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
551 (int)do_list_queue_start, (int)do_list_queue_end));
555 static int do_list_queue_empty(void)
557 return (! (do_list_queue && *do_list_queue));
560 /****************************************************************************
561 A helper for do_list.
562 ****************************************************************************/
564 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
566 char *dir_end;
568 /* save the directory */
569 pstrcpy( f->dir, mask );
570 if ( (dir_end = strrchr( f->dir, CLI_DIRSEP_CHAR )) != NULL ) {
571 *dir_end = '\0';
574 if (f->mode & aDIR) {
575 if (do_list_dirs && do_this_one(f)) {
576 do_list_fn(f);
578 if (do_list_recurse &&
579 !strequal(f->name,".") &&
580 !strequal(f->name,"..")) {
581 pstring mask2;
582 char *p;
584 if (!f->name[0]) {
585 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
586 return;
589 pstrcpy(mask2, mntpoint);
590 pstrcat(mask2, mask);
591 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
592 if (!p)
593 return;
594 p[1] = 0;
595 pstrcat(mask2, f->name);
596 pstrcat(mask2,CLI_DIRSEP_STR);
597 pstrcat(mask2,"*");
598 add_to_do_list_queue(mask2);
600 return;
603 if (do_this_one(f)) {
604 do_list_fn(f);
608 /****************************************************************************
609 A wrapper around cli_list that adds recursion.
610 ****************************************************************************/
612 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
614 static int in_do_list = 0;
615 struct cli_state *targetcli;
616 pstring targetpath;
618 if (in_do_list && rec) {
619 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
620 exit(1);
623 in_do_list = 1;
625 do_list_recurse = rec;
626 do_list_dirs = dirs;
627 do_list_fn = fn;
629 if (rec) {
630 init_do_list_queue();
631 add_to_do_list_queue(mask);
633 while (! do_list_queue_empty()) {
635 * Need to copy head so that it doesn't become
636 * invalid inside the call to cli_list. This
637 * would happen if the list were expanded
638 * during the call.
639 * Fix from E. Jay Berkenbilt (ejb@ql.org)
641 pstring head;
642 pstrcpy(head, do_list_queue_head());
644 /* check for dfs */
646 if ( !cli_resolve_path( "", cli, head, &targetcli, targetpath ) ) {
647 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
648 remove_do_list_queue_head();
649 continue;
652 cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
653 remove_do_list_queue_head();
654 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
655 char* next_file = do_list_queue_head();
656 char* save_ch = 0;
657 if ((strlen(next_file) >= 2) &&
658 (next_file[strlen(next_file) - 1] == '*') &&
659 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
660 save_ch = next_file +
661 strlen(next_file) - 2;
662 *save_ch = '\0';
663 if (showacls) /* cwd is only used if showacls is on */
664 pstrcpy( cwd, next_file);
666 if (!showacls) /* don't disturbe the showacls output */
667 d_printf("\n%s\n",next_file);
668 if (save_ch) {
669 *save_ch = CLI_DIRSEP_CHAR;
673 } else {
674 /* check for dfs */
676 if ( cli_resolve_path( "", cli, mask, &targetcli, targetpath ) ) {
677 if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1)
678 d_printf("%s listing %s\n", cli_errstr(targetcli), targetpath);
680 else
681 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
685 in_do_list = 0;
686 reset_do_list_queue();
689 /****************************************************************************
690 Get a directory listing.
691 ****************************************************************************/
693 static int cmd_dir(void)
695 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
696 pstring mask;
697 pstring buf;
698 char *p=buf;
699 int rc;
701 dir_total = 0;
702 if (strcmp(cur_dir, CLI_DIRSEP_STR) != 0) {
703 pstrcpy(mask,cur_dir);
704 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR))
705 pstrcat(mask,CLI_DIRSEP_STR);
706 } else {
707 pstrcpy(mask, CLI_DIRSEP_STR);
710 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
711 dos_format(p);
712 if (*p == CLI_DIRSEP_CHAR)
713 pstrcpy(mask,p + 1);
714 else
715 pstrcat(mask,p);
716 } else {
717 pstrcat(mask,"*");
720 if (showacls) {
721 /* cwd is only used if showacls is on */
722 pstrcpy(cwd, cur_dir);
725 do_list(mask, attribute, display_finfo, recurse, True);
727 rc = do_dskattr();
729 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
731 return rc;
734 /****************************************************************************
735 Get a directory listing.
736 ****************************************************************************/
738 static int cmd_du(void)
740 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
741 pstring mask;
742 pstring buf;
743 char *p=buf;
744 int rc;
746 dir_total = 0;
747 pstrcpy(mask,cur_dir);
748 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR))
749 pstrcat(mask,CLI_DIRSEP_STR);
751 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
752 dos_format(p);
753 if (*p == CLI_DIRSEP_CHAR)
754 pstrcpy(mask,p);
755 else
756 pstrcat(mask,p);
757 } else {
758 pstrcat(mask,"*");
761 do_list(mask, attribute, do_du, recurse, True);
763 rc = do_dskattr();
765 d_printf("Total number of bytes: %.0f\n", dir_total);
767 return rc;
770 /****************************************************************************
771 Get a file from rname to lname
772 ****************************************************************************/
774 static int do_get(char *rname, char *lname, BOOL reget)
776 int handle = 0, fnum;
777 BOOL newhandle = False;
778 char *data;
779 struct timeval tp_start;
780 int read_size = io_bufsize;
781 uint16 attr;
782 SMB_OFF_T size;
783 off_t start = 0;
784 off_t nread = 0;
785 int rc = 0;
786 struct cli_state *targetcli;
787 pstring targetname;
790 if (lowercase) {
791 strlower_m(lname);
794 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
795 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
796 return 1;
799 GetTimeOfDay(&tp_start);
801 fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
803 if (fnum == -1) {
804 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
805 return 1;
808 if(!strcmp(lname,"-")) {
809 handle = fileno(stdout);
810 } else {
811 if (reget) {
812 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
813 if (handle >= 0) {
814 start = sys_lseek(handle, 0, SEEK_END);
815 if (start == -1) {
816 d_printf("Error seeking local file\n");
817 return 1;
820 } else {
821 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
823 newhandle = True;
825 if (handle < 0) {
826 d_printf("Error opening local file %s\n",lname);
827 return 1;
831 if (!cli_qfileinfo(targetcli, fnum,
832 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
833 !cli_getattrE(targetcli, fnum,
834 &attr, &size, NULL, NULL, NULL)) {
835 d_printf("getattrib: %s\n",cli_errstr(targetcli));
836 return 1;
839 DEBUG(1,("getting file %s of size %.0f as %s ",
840 rname, (double)size, lname));
842 if(!(data = (char *)SMB_MALLOC(read_size))) {
843 d_printf("malloc fail for size %d\n", read_size);
844 cli_close(targetcli, fnum);
845 return 1;
848 while (1) {
849 int n = cli_read(targetcli, fnum, data, nread + start, read_size);
851 if (n <= 0)
852 break;
854 if (writefile(handle,data, n) != n) {
855 d_printf("Error writing local file\n");
856 rc = 1;
857 break;
860 nread += n;
863 if (nread + start < size) {
864 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
865 rname, (long)nread));
867 rc = 1;
870 SAFE_FREE(data);
872 if (!cli_close(targetcli, fnum)) {
873 d_printf("Error %s closing remote file\n",cli_errstr(cli));
874 rc = 1;
877 if (newhandle) {
878 close(handle);
881 if (archive_level >= 2 && (attr & aARCH)) {
882 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
886 struct timeval tp_end;
887 int this_time;
889 GetTimeOfDay(&tp_end);
890 this_time =
891 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
892 (tp_end.tv_usec - tp_start.tv_usec)/1000;
893 get_total_time_ms += this_time;
894 get_total_size += nread;
896 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
897 nread / (1.024*this_time + 1.0e-4),
898 get_total_size / (1.024*get_total_time_ms)));
901 return rc;
904 /****************************************************************************
905 Get a file.
906 ****************************************************************************/
908 static int cmd_get(void)
910 pstring lname;
911 pstring rname;
912 char *p;
914 pstrcpy(rname,cur_dir);
915 pstrcat(rname,CLI_DIRSEP_STR);
917 p = rname + strlen(rname);
919 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
920 d_printf("get <filename>\n");
921 return 1;
923 pstrcpy(lname,p);
924 clean_name(rname);
926 next_token_nr(NULL,lname,NULL,sizeof(lname));
928 return do_get(rname, lname, False);
931 /****************************************************************************
932 Do an mget operation on one file.
933 ****************************************************************************/
935 static void do_mget(file_info *finfo)
937 pstring rname;
938 pstring quest;
939 pstring saved_curdir;
940 pstring mget_mask;
942 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
943 return;
945 if (abort_mget) {
946 d_printf("mget aborted\n");
947 return;
950 if (finfo->mode & aDIR)
951 slprintf(quest,sizeof(pstring)-1,
952 "Get directory %s? ",finfo->name);
953 else
954 slprintf(quest,sizeof(pstring)-1,
955 "Get file %s? ",finfo->name);
957 if (prompt && !yesno(quest))
958 return;
960 if (!(finfo->mode & aDIR)) {
961 pstrcpy(rname,cur_dir);
962 pstrcat(rname,finfo->name);
963 do_get(rname, finfo->name, False);
964 return;
967 /* handle directories */
968 pstrcpy(saved_curdir,cur_dir);
970 pstrcat(cur_dir,finfo->name);
971 pstrcat(cur_dir,CLI_DIRSEP_STR);
973 unix_format(finfo->name);
974 if (lowercase)
975 strlower_m(finfo->name);
977 if (!directory_exist(finfo->name,NULL) &&
978 mkdir(finfo->name,0777) != 0) {
979 d_printf("failed to create directory %s\n",finfo->name);
980 pstrcpy(cur_dir,saved_curdir);
981 return;
984 if (chdir(finfo->name) != 0) {
985 d_printf("failed to chdir to directory %s\n",finfo->name);
986 pstrcpy(cur_dir,saved_curdir);
987 return;
990 pstrcpy(mget_mask,cur_dir);
991 pstrcat(mget_mask,"*");
993 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
994 chdir("..");
995 pstrcpy(cur_dir,saved_curdir);
998 /****************************************************************************
999 View the file using the pager.
1000 ****************************************************************************/
1002 static int cmd_more(void)
1004 pstring rname,lname,pager_cmd;
1005 char *pager;
1006 int fd;
1007 int rc = 0;
1009 pstrcpy(rname,cur_dir);
1010 pstrcat(rname,CLI_DIRSEP_STR);
1012 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
1013 fd = smb_mkstemp(lname);
1014 if (fd == -1) {
1015 d_printf("failed to create temporary file for more\n");
1016 return 1;
1018 close(fd);
1020 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
1021 d_printf("more <filename>\n");
1022 unlink(lname);
1023 return 1;
1025 clean_name(rname);
1027 rc = do_get(rname, lname, False);
1029 pager=getenv("PAGER");
1031 slprintf(pager_cmd,sizeof(pager_cmd)-1,
1032 "%s %s",(pager? pager:PAGER), lname);
1033 system(pager_cmd);
1034 unlink(lname);
1036 return rc;
1039 /****************************************************************************
1040 Do a mget command.
1041 ****************************************************************************/
1043 static int cmd_mget(void)
1045 uint16 attribute = aSYSTEM | aHIDDEN;
1046 pstring mget_mask;
1047 pstring buf;
1048 char *p=buf;
1050 *mget_mask = 0;
1052 if (recurse)
1053 attribute |= aDIR;
1055 abort_mget = False;
1057 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1058 pstrcpy(mget_mask,cur_dir);
1059 if ((mget_mask[0] != '\0') && (mget_mask[strlen(mget_mask)-1]!=CLI_DIRSEP_CHAR))
1060 pstrcat(mget_mask,CLI_DIRSEP_STR);
1062 if (*p == CLI_DIRSEP_CHAR)
1063 pstrcpy(mget_mask,p);
1064 else
1065 pstrcat(mget_mask,p);
1066 do_list(mget_mask, attribute,do_mget,False,True);
1069 if (!*mget_mask) {
1070 pstrcpy(mget_mask,cur_dir);
1071 if(mget_mask[strlen(mget_mask)-1]!=CLI_DIRSEP_CHAR)
1072 pstrcat(mget_mask,CLI_DIRSEP_STR);
1073 pstrcat(mget_mask,"*");
1074 do_list(mget_mask, attribute,do_mget,False,True);
1077 return 0;
1080 /****************************************************************************
1081 Make a directory of name "name".
1082 ****************************************************************************/
1084 static BOOL do_mkdir(char *name)
1086 struct cli_state *targetcli;
1087 pstring targetname;
1089 if ( !cli_resolve_path( "", cli, name, &targetcli, targetname ) ) {
1090 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1091 return False;
1094 if (!cli_mkdir(targetcli, targetname)) {
1095 d_printf("%s making remote directory %s\n",
1096 cli_errstr(targetcli),name);
1097 return(False);
1100 return(True);
1103 /****************************************************************************
1104 Show 8.3 name of a file.
1105 ****************************************************************************/
1107 static BOOL do_altname(char *name)
1109 pstring altname;
1110 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1111 d_printf("%s getting alt name for %s\n",
1112 cli_errstr(cli),name);
1113 return(False);
1115 d_printf("%s\n", altname);
1117 return(True);
1120 /****************************************************************************
1121 Exit client.
1122 ****************************************************************************/
1124 static int cmd_quit(void)
1126 cli_cm_shutdown();
1127 talloc_destroy( ctx);
1128 exit(0);
1129 /* NOTREACHED */
1130 return 0;
1133 /****************************************************************************
1134 Make a directory.
1135 ****************************************************************************/
1137 static int cmd_mkdir(void)
1139 pstring mask;
1140 pstring buf;
1141 char *p=buf;
1143 pstrcpy(mask,cur_dir);
1145 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1146 if (!recurse)
1147 d_printf("mkdir <dirname>\n");
1148 return 1;
1150 pstrcat(mask,p);
1152 if (recurse) {
1153 pstring ddir;
1154 pstring ddir2;
1155 struct cli_state *targetcli;
1156 pstring targetname;
1157 *ddir2 = 0;
1159 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1160 return 1;
1163 pstrcpy(ddir,targetname);
1164 trim_char(ddir,'.','\0');
1165 p = strtok(ddir,"/\\");
1166 while (p) {
1167 pstrcat(ddir2,p);
1168 if (!cli_chkpath(targetcli, ddir2)) {
1169 do_mkdir(ddir2);
1171 pstrcat(ddir2,CLI_DIRSEP_STR);
1172 p = strtok(NULL,"/\\");
1174 } else {
1175 do_mkdir(mask);
1178 return 0;
1181 /****************************************************************************
1182 Show alt name.
1183 ****************************************************************************/
1185 static int cmd_altname(void)
1187 pstring name;
1188 pstring buf;
1189 char *p=buf;
1191 pstrcpy(name,cur_dir);
1193 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1194 d_printf("altname <file>\n");
1195 return 1;
1197 pstrcat(name,p);
1199 do_altname(name);
1201 return 0;
1204 /****************************************************************************
1205 Put a single file.
1206 ****************************************************************************/
1208 static int do_put(char *rname, char *lname, BOOL reput)
1210 int fnum;
1211 XFILE *f;
1212 SMB_OFF_T start = 0;
1213 off_t nread = 0;
1214 char *buf = NULL;
1215 int maxwrite = io_bufsize;
1216 int rc = 0;
1217 struct timeval tp_start;
1218 struct cli_state *targetcli;
1219 pstring targetname;
1221 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
1222 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1223 return 1;
1226 GetTimeOfDay(&tp_start);
1228 if (reput) {
1229 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
1230 if (fnum >= 0) {
1231 if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1232 !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
1233 d_printf("getattrib: %s\n",cli_errstr(cli));
1234 return 1;
1237 } else {
1238 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1241 if (fnum == -1) {
1242 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1243 return 1;
1246 /* allow files to be piped into smbclient
1247 jdblair 24.jun.98
1249 Note that in this case this function will exit(0) rather
1250 than returning. */
1251 if (!strcmp(lname, "-")) {
1252 f = x_stdin;
1253 /* size of file is not known */
1254 } else {
1255 f = x_fopen(lname,O_RDONLY, 0);
1256 if (f && reput) {
1257 if (x_tseek(f, start, SEEK_SET) == -1) {
1258 d_printf("Error seeking local file\n");
1259 return 1;
1264 if (!f) {
1265 d_printf("Error opening local file %s\n",lname);
1266 return 1;
1269 DEBUG(1,("putting file %s as %s ",lname,
1270 rname));
1272 buf = (char *)SMB_MALLOC(maxwrite);
1273 if (!buf) {
1274 d_printf("ERROR: Not enough memory!\n");
1275 return 1;
1277 while (!x_feof(f)) {
1278 int n = maxwrite;
1279 int ret;
1281 if ((n = readfile(buf,n,f)) < 1) {
1282 if((n == 0) && x_feof(f))
1283 break; /* Empty local file. */
1285 d_printf("Error reading local file: %s\n", strerror(errno));
1286 rc = 1;
1287 break;
1290 ret = cli_write(targetcli, fnum, 0, buf, nread + start, n);
1292 if (n != ret) {
1293 d_printf("Error writing file: %s\n", cli_errstr(cli));
1294 rc = 1;
1295 break;
1298 nread += n;
1301 if (!cli_close(targetcli, fnum)) {
1302 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1303 x_fclose(f);
1304 SAFE_FREE(buf);
1305 return 1;
1309 if (f != x_stdin) {
1310 x_fclose(f);
1313 SAFE_FREE(buf);
1316 struct timeval tp_end;
1317 int this_time;
1319 GetTimeOfDay(&tp_end);
1320 this_time =
1321 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1322 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1323 put_total_time_ms += this_time;
1324 put_total_size += nread;
1326 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1327 nread / (1.024*this_time + 1.0e-4),
1328 put_total_size / (1.024*put_total_time_ms)));
1331 if (f == x_stdin) {
1332 cli_cm_shutdown();
1333 exit(0);
1336 return rc;
1339 /****************************************************************************
1340 Put a file.
1341 ****************************************************************************/
1343 static int cmd_put(void)
1345 pstring lname;
1346 pstring rname;
1347 pstring buf;
1348 char *p=buf;
1350 pstrcpy(rname,cur_dir);
1351 pstrcat(rname,CLI_DIRSEP_STR);
1353 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1354 d_printf("put <filename>\n");
1355 return 1;
1357 pstrcpy(lname,p);
1359 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1360 pstrcat(rname,p);
1361 else
1362 pstrcat(rname,lname);
1364 clean_name(rname);
1367 SMB_STRUCT_STAT st;
1368 /* allow '-' to represent stdin
1369 jdblair, 24.jun.98 */
1370 if (!file_exist(lname,&st) &&
1371 (strcmp(lname,"-"))) {
1372 d_printf("%s does not exist\n",lname);
1373 return 1;
1377 return do_put(rname, lname, False);
1380 /*************************************
1381 File list structure.
1382 *************************************/
1384 static struct file_list {
1385 struct file_list *prev, *next;
1386 char *file_path;
1387 BOOL isdir;
1388 } *file_list;
1390 /****************************************************************************
1391 Free a file_list structure.
1392 ****************************************************************************/
1394 static void free_file_list (struct file_list *list_head)
1396 struct file_list *list, *next;
1398 for (list = list_head; list; list = next) {
1399 next = list->next;
1400 DLIST_REMOVE(list_head, list);
1401 SAFE_FREE(list->file_path);
1402 SAFE_FREE(list);
1406 /****************************************************************************
1407 Seek in a directory/file list until you get something that doesn't start with
1408 the specified name.
1409 ****************************************************************************/
1411 static BOOL seek_list(struct file_list *list, char *name)
1413 while (list) {
1414 trim_string(list->file_path,"./","\n");
1415 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1416 return(True);
1418 list = list->next;
1421 return(False);
1424 /****************************************************************************
1425 Set the file selection mask.
1426 ****************************************************************************/
1428 static int cmd_select(void)
1430 pstrcpy(fileselection,"");
1431 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1433 return 0;
1436 /****************************************************************************
1437 Recursive file matching function act as find
1438 match must be always set to True when calling this function
1439 ****************************************************************************/
1441 static int file_find(struct file_list **list, const char *directory,
1442 const char *expression, BOOL match)
1444 SMB_STRUCT_DIR *dir;
1445 struct file_list *entry;
1446 struct stat statbuf;
1447 int ret;
1448 char *path;
1449 BOOL isdir;
1450 const char *dname;
1452 dir = sys_opendir(directory);
1453 if (!dir)
1454 return -1;
1456 while ((dname = readdirname(dir))) {
1457 if (!strcmp("..", dname))
1458 continue;
1459 if (!strcmp(".", dname))
1460 continue;
1462 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1463 continue;
1466 isdir = False;
1467 if (!match || !gen_fnmatch(expression, dname)) {
1468 if (recurse) {
1469 ret = stat(path, &statbuf);
1470 if (ret == 0) {
1471 if (S_ISDIR(statbuf.st_mode)) {
1472 isdir = True;
1473 ret = file_find(list, path, expression, False);
1475 } else {
1476 d_printf("file_find: cannot stat file %s\n", path);
1479 if (ret == -1) {
1480 SAFE_FREE(path);
1481 sys_closedir(dir);
1482 return -1;
1485 entry = SMB_MALLOC_P(struct file_list);
1486 if (!entry) {
1487 d_printf("Out of memory in file_find\n");
1488 sys_closedir(dir);
1489 return -1;
1491 entry->file_path = path;
1492 entry->isdir = isdir;
1493 DLIST_ADD(*list, entry);
1494 } else {
1495 SAFE_FREE(path);
1499 sys_closedir(dir);
1500 return 0;
1503 /****************************************************************************
1504 mput some files.
1505 ****************************************************************************/
1507 static int cmd_mput(void)
1509 pstring buf;
1510 char *p=buf;
1512 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1513 int ret;
1514 struct file_list *temp_list;
1515 char *quest, *lname, *rname;
1517 file_list = NULL;
1519 ret = file_find(&file_list, ".", p, True);
1520 if (ret) {
1521 free_file_list(file_list);
1522 continue;
1525 quest = NULL;
1526 lname = NULL;
1527 rname = NULL;
1529 for (temp_list = file_list; temp_list;
1530 temp_list = temp_list->next) {
1532 SAFE_FREE(lname);
1533 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1534 continue;
1535 trim_string(lname, "./", "/");
1537 /* check if it's a directory */
1538 if (temp_list->isdir) {
1539 /* if (!recurse) continue; */
1541 SAFE_FREE(quest);
1542 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1543 if (prompt && !yesno(quest)) { /* No */
1544 /* Skip the directory */
1545 lname[strlen(lname)-1] = '/';
1546 if (!seek_list(temp_list, lname))
1547 break;
1548 } else { /* Yes */
1549 SAFE_FREE(rname);
1550 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1551 dos_format(rname);
1552 if (!cli_chkpath(cli, rname) &&
1553 !do_mkdir(rname)) {
1554 DEBUG (0, ("Unable to make dir, skipping..."));
1555 /* Skip the directory */
1556 lname[strlen(lname)-1] = '/';
1557 if (!seek_list(temp_list, lname))
1558 break;
1561 continue;
1562 } else {
1563 SAFE_FREE(quest);
1564 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1565 if (prompt && !yesno(quest)) /* No */
1566 continue;
1568 /* Yes */
1569 SAFE_FREE(rname);
1570 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1573 dos_format(rname);
1575 do_put(rname, lname, False);
1577 free_file_list(file_list);
1578 SAFE_FREE(quest);
1579 SAFE_FREE(lname);
1580 SAFE_FREE(rname);
1583 return 0;
1586 /****************************************************************************
1587 Cancel a print job.
1588 ****************************************************************************/
1590 static int do_cancel(int job)
1592 if (cli_printjob_del(cli, job)) {
1593 d_printf("Job %d cancelled\n",job);
1594 return 0;
1595 } else {
1596 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1597 return 1;
1601 /****************************************************************************
1602 Cancel a print job.
1603 ****************************************************************************/
1605 static int cmd_cancel(void)
1607 pstring buf;
1608 int job;
1610 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1611 d_printf("cancel <jobid> ...\n");
1612 return 1;
1614 do {
1615 job = atoi(buf);
1616 do_cancel(job);
1617 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1619 return 0;
1622 /****************************************************************************
1623 Print a file.
1624 ****************************************************************************/
1626 static int cmd_print(void)
1628 pstring lname;
1629 pstring rname;
1630 char *p;
1632 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1633 d_printf("print <filename>\n");
1634 return 1;
1637 pstrcpy(rname,lname);
1638 p = strrchr_m(rname,'/');
1639 if (p) {
1640 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1643 if (strequal(lname,"-")) {
1644 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1647 return do_put(rname, lname, False);
1650 /****************************************************************************
1651 Show a print queue entry.
1652 ****************************************************************************/
1654 static void queue_fn(struct print_job_info *p)
1656 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
1659 /****************************************************************************
1660 Show a print queue.
1661 ****************************************************************************/
1663 static int cmd_queue(void)
1665 cli_print_queue(cli, queue_fn);
1667 return 0;
1670 /****************************************************************************
1671 Delete some files.
1672 ****************************************************************************/
1674 static void do_del(file_info *finfo)
1676 pstring mask;
1678 pstr_sprintf( mask, "%s%c%s", finfo->dir, CLI_DIRSEP_CHAR, finfo->name );
1680 if (finfo->mode & aDIR)
1681 return;
1683 if (!cli_unlink(finfo->cli, mask)) {
1684 d_printf("%s deleting remote file %s\n",cli_errstr(finfo->cli),mask);
1688 /****************************************************************************
1689 Delete some files.
1690 ****************************************************************************/
1692 static int cmd_del(void)
1694 pstring mask;
1695 pstring buf;
1696 uint16 attribute = aSYSTEM | aHIDDEN;
1698 if (recurse)
1699 attribute |= aDIR;
1701 pstrcpy(mask,cur_dir);
1703 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1704 d_printf("del <filename>\n");
1705 return 1;
1707 pstrcat(mask,buf);
1709 do_list(mask, attribute,do_del,False,False);
1711 return 0;
1714 /****************************************************************************
1715 Wildcard delete some files.
1716 ****************************************************************************/
1718 static int cmd_wdel(void)
1720 pstring mask;
1721 pstring buf;
1722 uint16 attribute;
1723 struct cli_state *targetcli;
1724 pstring targetname;
1726 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1727 d_printf("wdel 0x<attrib> <wcard>\n");
1728 return 1;
1731 attribute = (uint16)strtol(buf, (char **)NULL, 16);
1733 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1734 d_printf("wdel 0x<attrib> <wcard>\n");
1735 return 1;
1738 pstrcpy(mask,cur_dir);
1739 pstrcat(mask,buf);
1741 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1742 d_printf("cmd_wdel %s: %s\n", mask, cli_errstr(cli));
1743 return 1;
1746 if (!cli_unlink_full(targetcli, targetname, attribute)) {
1747 d_printf("%s deleting remote files %s\n",cli_errstr(targetcli),targetname);
1749 return 0;
1752 /****************************************************************************
1753 ****************************************************************************/
1755 static int cmd_open(void)
1757 pstring mask;
1758 pstring buf;
1759 struct cli_state *targetcli;
1760 pstring targetname;
1761 int fnum;
1763 pstrcpy(mask,cur_dir);
1765 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1766 d_printf("open <filename>\n");
1767 return 1;
1769 pstrcat(mask,buf);
1771 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1772 d_printf("open %s: %s\n", mask, cli_errstr(cli));
1773 return 1;
1776 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA|FILE_WRITE_DATA);
1777 if (fnum == -1) {
1778 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA);
1779 if (fnum != -1) {
1780 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
1781 } else {
1782 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
1784 } else {
1785 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
1788 return 0;
1791 /****************************************************************************
1792 ****************************************************************************/
1794 static int cmd_posix_open(void)
1796 pstring mask;
1797 pstring buf;
1798 struct cli_state *targetcli;
1799 pstring targetname;
1800 mode_t mode;
1801 int fnum;
1803 pstrcpy(mask,cur_dir);
1805 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1806 d_printf("posix_open <filename> 0<mode>\n");
1807 return 1;
1809 pstrcat(mask,buf);
1810 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1811 d_printf("posix_open <filename> 0<mode>\n");
1812 return 1;
1814 mode = (mode_t)strtol(buf, (char **)NULL, 8);
1816 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname )) {
1817 d_printf("posix_open %s: %s\n", mask, cli_errstr(cli));
1818 return 1;
1821 fnum = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode);
1822 if (fnum == -1) {
1823 fnum = cli_posix_open(targetcli, targetname, O_CREAT|O_RDONLY, mode);
1824 if (fnum != -1) {
1825 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
1826 } else {
1827 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
1829 } else {
1830 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
1833 return 0;
1836 static int cmd_posix_mkdir(void)
1838 pstring mask;
1839 pstring buf;
1840 struct cli_state *targetcli;
1841 pstring targetname;
1842 mode_t mode;
1843 int fnum;
1845 pstrcpy(mask,cur_dir);
1847 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1848 d_printf("posix_mkdir <filename> 0<mode>\n");
1849 return 1;
1851 pstrcat(mask,buf);
1852 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1853 d_printf("posix_mkdir <filename> 0<mode>\n");
1854 return 1;
1856 mode = (mode_t)strtol(buf, (char **)NULL, 8);
1858 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname )) {
1859 d_printf("posix_mkdir %s: %s\n", mask, cli_errstr(cli));
1860 return 1;
1863 fnum = cli_posix_mkdir(targetcli, targetname, mode);
1864 if (fnum == -1) {
1865 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
1866 } else {
1867 d_printf("posix_mkdir created directory %s\n", targetname);
1870 return 0;
1873 static int cmd_posix_unlink(void)
1875 pstring mask;
1876 pstring buf;
1877 struct cli_state *targetcli;
1878 pstring targetname;
1880 pstrcpy(mask,cur_dir);
1882 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1883 d_printf("posix_unlink <filename>\n");
1884 return 1;
1886 pstrcat(mask,buf);
1888 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname )) {
1889 d_printf("posix_unlink %s: %s\n", mask, cli_errstr(cli));
1890 return 1;
1893 if (!cli_posix_unlink(targetcli, targetname)) {
1894 d_printf("Failed to unlink file %s. %s\n", targetname, cli_errstr(cli));
1895 } else {
1896 d_printf("posix_unlink deleted file %s\n", targetname);
1899 return 0;
1902 static int cmd_posix_rmdir(void)
1904 pstring mask;
1905 pstring buf;
1906 struct cli_state *targetcli;
1907 pstring targetname;
1909 pstrcpy(mask,cur_dir);
1911 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1912 d_printf("posix_rmdir <filename>\n");
1913 return 1;
1915 pstrcat(mask,buf);
1917 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname)) {
1918 d_printf("posix_rmdir %s: %s\n", mask, cli_errstr(cli));
1919 return 1;
1922 if (!cli_posix_rmdir(targetcli, targetname)) {
1923 d_printf("Failed to unlink directory %s. %s\n", targetname, cli_errstr(cli));
1924 } else {
1925 d_printf("posix_rmdir deleted directory %s\n", targetname);
1928 return 0;
1931 static int cmd_close(void)
1933 fstring buf;
1934 int fnum;
1936 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1937 d_printf("close <fnum>\n");
1938 return 1;
1941 fnum = atoi(buf);
1942 /* We really should use the targetcli here.... */
1943 if (!cli_close(cli, fnum)) {
1944 d_printf("close %d: %s\n", fnum, cli_errstr(cli));
1945 return 1;
1947 return 0;
1950 static int cmd_posix(void)
1952 uint16 major, minor;
1953 uint32 caplow, caphigh;
1954 pstring caps;
1956 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1957 d_printf("Server doesn't support UNIX CIFS extensions.\n");
1958 return 1;
1961 if (!cli_unix_extensions_version(cli, &major, &minor, &caplow, &caphigh)) {
1962 d_printf("Can't get UNIX CIFS extensions version from server.\n");
1963 return 1;
1966 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
1968 *caps = '\0';
1969 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
1970 pstrcat(caps, "locks ");
1972 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
1973 pstrcat(caps, "acls ");
1975 if (caplow & CIFS_UNIX_XATTTR_CAP) {
1976 pstrcat(caps, "eas ");
1978 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
1979 pstrcat(caps, "pathnames ");
1981 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
1982 pstrcat(caps, "posix_path_operations ");
1985 if (strlen(caps) > 0 && caps[strlen(caps)-1] == ' ') {
1986 caps[strlen(caps)-1] = '\0';
1989 if (!cli_set_unix_extensions_capabilities(cli, major, minor, caplow, caphigh)) {
1990 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n", cli_errstr(cli));
1991 return 1;
1994 d_printf("Selecting server supported CIFS capabilities %s\n", caps);
1996 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
1997 CLI_DIRSEP_CHAR = '/';
1998 *CLI_DIRSEP_STR = '/';
1999 pstrcpy(cur_dir, CLI_DIRSEP_STR);
2002 return 0;
2005 static int cmd_lock(void)
2007 fstring buf;
2008 SMB_BIG_UINT start, len;
2009 enum brl_type lock_type;
2010 int fnum;
2012 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2013 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2014 return 1;
2016 fnum = atoi(buf);
2018 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2019 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2020 return 1;
2023 if (*buf == 'r' || *buf == 'R') {
2024 lock_type = READ_LOCK;
2025 } else if (*buf == 'w' || *buf == 'W') {
2026 lock_type = WRITE_LOCK;
2027 } else {
2028 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2029 return 1;
2032 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2033 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2034 return 1;
2037 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2039 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2040 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2041 return 1;
2044 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2046 if (!cli_posix_lock(cli, fnum, start, len, True, lock_type)) {
2047 d_printf("lock failed %d: %s\n", fnum, cli_errstr(cli));
2050 return 0;
2053 static int cmd_unlock(void)
2055 fstring buf;
2056 SMB_BIG_UINT start, len;
2057 int fnum;
2059 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2060 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2061 return 1;
2063 fnum = atoi(buf);
2065 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2066 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2067 return 1;
2070 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2072 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2073 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2074 return 1;
2077 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2079 if (!cli_posix_unlock(cli, fnum, start, len)) {
2080 d_printf("unlock failed %d: %s\n", fnum, cli_errstr(cli));
2083 return 0;
2087 /****************************************************************************
2088 Remove a directory.
2089 ****************************************************************************/
2091 static int cmd_rmdir(void)
2093 pstring mask;
2094 pstring buf;
2095 struct cli_state *targetcli;
2096 pstring targetname;
2098 pstrcpy(mask,cur_dir);
2100 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2101 d_printf("rmdir <dirname>\n");
2102 return 1;
2104 pstrcat(mask,buf);
2106 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
2107 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
2108 return 1;
2111 if (!cli_rmdir(targetcli, targetname)) {
2112 d_printf("%s removing remote directory file %s\n",
2113 cli_errstr(targetcli),mask);
2116 return 0;
2119 /****************************************************************************
2120 UNIX hardlink.
2121 ****************************************************************************/
2123 static int cmd_link(void)
2125 pstring oldname,newname;
2126 pstring buf,buf2;
2127 struct cli_state *targetcli;
2128 pstring targetname;
2130 pstrcpy(oldname,cur_dir);
2131 pstrcpy(newname,cur_dir);
2133 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2134 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2135 d_printf("link <oldname> <newname>\n");
2136 return 1;
2139 pstrcat(oldname,buf);
2140 pstrcat(newname,buf2);
2142 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
2143 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2144 return 1;
2147 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2148 d_printf("Server doesn't support UNIX CIFS calls.\n");
2149 return 1;
2152 if (!cli_unix_hardlink(targetcli, targetname, newname)) {
2153 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
2154 return 1;
2157 return 0;
2160 /****************************************************************************
2161 UNIX symlink.
2162 ****************************************************************************/
2164 static int cmd_symlink(void)
2166 pstring oldname,newname;
2167 pstring buf,buf2;
2168 struct cli_state *targetcli;
2169 pstring targetname;
2171 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2172 d_printf("Server doesn't support UNIX CIFS calls.\n");
2173 return 1;
2176 pstrcpy(newname,cur_dir);
2178 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2179 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2180 d_printf("symlink <oldname> <newname>\n");
2181 return 1;
2184 pstrcpy(oldname,buf);
2185 pstrcat(newname,buf2);
2187 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
2188 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2189 return 1;
2192 if (!cli_unix_symlink(targetcli, targetname, newname)) {
2193 d_printf("%s symlinking files (%s -> %s)\n",
2194 cli_errstr(targetcli), newname, targetname);
2195 return 1;
2198 return 0;
2201 /****************************************************************************
2202 UNIX chmod.
2203 ****************************************************************************/
2205 static int cmd_chmod(void)
2207 pstring src;
2208 mode_t mode;
2209 pstring buf, buf2;
2210 struct cli_state *targetcli;
2211 pstring targetname;
2213 pstrcpy(src,cur_dir);
2215 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2216 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2217 d_printf("chmod mode file\n");
2218 return 1;
2221 mode = (mode_t)strtol(buf, NULL, 8);
2222 pstrcat(src,buf2);
2224 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2225 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
2226 return 1;
2229 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2230 d_printf("Server doesn't support UNIX CIFS calls.\n");
2231 return 1;
2234 if (!cli_unix_chmod(targetcli, targetname, mode)) {
2235 d_printf("%s chmod file %s 0%o\n",
2236 cli_errstr(targetcli), src, (unsigned int)mode);
2237 return 1;
2240 return 0;
2243 static const char *filetype_to_str(mode_t mode)
2245 if (S_ISREG(mode)) {
2246 return "regular file";
2247 } else if (S_ISDIR(mode)) {
2248 return "directory";
2249 } else
2250 #ifdef S_ISCHR
2251 if (S_ISCHR(mode)) {
2252 return "character device";
2253 } else
2254 #endif
2255 #ifdef S_ISBLK
2256 if (S_ISBLK(mode)) {
2257 return "block device";
2258 } else
2259 #endif
2260 #ifdef S_ISFIFO
2261 if (S_ISFIFO(mode)) {
2262 return "fifo";
2263 } else
2264 #endif
2265 #ifdef S_ISLNK
2266 if (S_ISLNK(mode)) {
2267 return "symbolic link";
2268 } else
2269 #endif
2270 #ifdef S_ISSOCK
2271 if (S_ISSOCK(mode)) {
2272 return "socket";
2273 } else
2274 #endif
2275 return "";
2278 static char rwx_to_str(mode_t m, mode_t bt, char ret)
2280 if (m & bt) {
2281 return ret;
2282 } else {
2283 return '-';
2287 static char *unix_mode_to_str(char *s, mode_t m)
2289 char *p = s;
2290 const char *str = filetype_to_str(m);
2292 switch(str[0]) {
2293 case 'd':
2294 *p++ = 'd';
2295 break;
2296 case 'c':
2297 *p++ = 'c';
2298 break;
2299 case 'b':
2300 *p++ = 'b';
2301 break;
2302 case 'f':
2303 *p++ = 'p';
2304 break;
2305 case 's':
2306 *p++ = str[1] == 'y' ? 'l' : 's';
2307 break;
2308 case 'r':
2309 default:
2310 *p++ = '-';
2311 break;
2313 *p++ = rwx_to_str(m, S_IRUSR, 'r');
2314 *p++ = rwx_to_str(m, S_IWUSR, 'w');
2315 *p++ = rwx_to_str(m, S_IXUSR, 'x');
2316 *p++ = rwx_to_str(m, S_IRGRP, 'r');
2317 *p++ = rwx_to_str(m, S_IWGRP, 'w');
2318 *p++ = rwx_to_str(m, S_IXGRP, 'x');
2319 *p++ = rwx_to_str(m, S_IROTH, 'r');
2320 *p++ = rwx_to_str(m, S_IWOTH, 'w');
2321 *p++ = rwx_to_str(m, S_IXOTH, 'x');
2322 *p++ = '\0';
2323 return s;
2326 /****************************************************************************
2327 Utility function for UNIX getfacl.
2328 ****************************************************************************/
2330 static char *perms_to_string(fstring permstr, unsigned char perms)
2332 fstrcpy(permstr, "---");
2333 if (perms & SMB_POSIX_ACL_READ) {
2334 permstr[0] = 'r';
2336 if (perms & SMB_POSIX_ACL_WRITE) {
2337 permstr[1] = 'w';
2339 if (perms & SMB_POSIX_ACL_EXECUTE) {
2340 permstr[2] = 'x';
2342 return permstr;
2345 /****************************************************************************
2346 UNIX getfacl.
2347 ****************************************************************************/
2349 static int cmd_getfacl(void)
2351 pstring src, name;
2352 uint16 major, minor;
2353 uint32 caplow, caphigh;
2354 char *retbuf = NULL;
2355 size_t rb_size = 0;
2356 SMB_STRUCT_STAT sbuf;
2357 uint16 num_file_acls = 0;
2358 uint16 num_dir_acls = 0;
2359 uint16 i;
2360 struct cli_state *targetcli;
2361 pstring targetname;
2363 pstrcpy(src,cur_dir);
2365 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2366 d_printf("stat file\n");
2367 return 1;
2370 pstrcat(src,name);
2372 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2373 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2374 return 1;
2377 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2378 d_printf("Server doesn't support UNIX CIFS calls.\n");
2379 return 1;
2382 if (!cli_unix_extensions_version(targetcli, &major, &minor, &caplow, &caphigh)) {
2383 d_printf("Can't get UNIX CIFS version from server.\n");
2384 return 1;
2387 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
2388 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
2389 return 1;
2392 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2393 d_printf("%s getfacl doing a stat on file %s\n",
2394 cli_errstr(targetcli), src);
2395 return 1;
2398 if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
2399 d_printf("%s getfacl file %s\n",
2400 cli_errstr(targetcli), src);
2401 return 1;
2404 /* ToDo : Print out the ACL values. */
2405 if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
2406 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2407 src, (unsigned int)CVAL(retbuf,0) );
2408 SAFE_FREE(retbuf);
2409 return 1;
2412 num_file_acls = SVAL(retbuf,2);
2413 num_dir_acls = SVAL(retbuf,4);
2414 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
2415 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2416 src,
2417 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
2418 (unsigned int)rb_size);
2420 SAFE_FREE(retbuf);
2421 return 1;
2424 d_printf("# file: %s\n", src);
2425 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
2427 if (num_file_acls == 0 && num_dir_acls == 0) {
2428 d_printf("No acls found.\n");
2431 for (i = 0; i < num_file_acls; i++) {
2432 uint32 uorg;
2433 fstring permstring;
2434 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
2435 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2437 switch(tagtype) {
2438 case SMB_POSIX_ACL_USER_OBJ:
2439 d_printf("user::");
2440 break;
2441 case SMB_POSIX_ACL_USER:
2442 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2443 d_printf("user:%u:", uorg);
2444 break;
2445 case SMB_POSIX_ACL_GROUP_OBJ:
2446 d_printf("group::");
2447 break;
2448 case SMB_POSIX_ACL_GROUP:
2449 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2450 d_printf("group:%u", uorg);
2451 break;
2452 case SMB_POSIX_ACL_MASK:
2453 d_printf("mask::");
2454 break;
2455 case SMB_POSIX_ACL_OTHER:
2456 d_printf("other::");
2457 break;
2458 default:
2459 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2460 src, (unsigned int)tagtype );
2461 SAFE_FREE(retbuf);
2462 return 1;
2465 d_printf("%s\n", perms_to_string(permstring, perms));
2468 for (i = 0; i < num_dir_acls; i++) {
2469 uint32 uorg;
2470 fstring permstring;
2471 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
2472 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2474 switch(tagtype) {
2475 case SMB_POSIX_ACL_USER_OBJ:
2476 d_printf("default:user::");
2477 break;
2478 case SMB_POSIX_ACL_USER:
2479 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2480 d_printf("default:user:%u:", uorg);
2481 break;
2482 case SMB_POSIX_ACL_GROUP_OBJ:
2483 d_printf("default:group::");
2484 break;
2485 case SMB_POSIX_ACL_GROUP:
2486 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2487 d_printf("default:group:%u", uorg);
2488 break;
2489 case SMB_POSIX_ACL_MASK:
2490 d_printf("default:mask::");
2491 break;
2492 case SMB_POSIX_ACL_OTHER:
2493 d_printf("default:other::");
2494 break;
2495 default:
2496 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2497 src, (unsigned int)tagtype );
2498 SAFE_FREE(retbuf);
2499 return 1;
2502 d_printf("%s\n", perms_to_string(permstring, perms));
2505 SAFE_FREE(retbuf);
2506 return 0;
2509 /****************************************************************************
2510 UNIX stat.
2511 ****************************************************************************/
2513 static int cmd_stat(void)
2515 pstring src, name;
2516 fstring mode_str;
2517 SMB_STRUCT_STAT sbuf;
2518 struct cli_state *targetcli;
2519 struct tm *lt;
2520 pstring targetname;
2522 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2523 d_printf("Server doesn't support UNIX CIFS calls.\n");
2524 return 1;
2527 pstrcpy(src,cur_dir);
2529 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2530 d_printf("stat file\n");
2531 return 1;
2534 pstrcat(src,name);
2537 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2538 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2539 return 1;
2542 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2543 d_printf("%s stat file %s\n",
2544 cli_errstr(targetcli), src);
2545 return 1;
2548 /* Print out the stat values. */
2549 d_printf("File: %s\n", src);
2550 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2551 (double)sbuf.st_size,
2552 (unsigned int)sbuf.st_blocks,
2553 filetype_to_str(sbuf.st_mode));
2555 #if defined(S_ISCHR) && defined(S_ISBLK)
2556 if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
2557 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2558 (double)sbuf.st_ino,
2559 (unsigned int)sbuf.st_nlink,
2560 unix_dev_major(sbuf.st_rdev),
2561 unix_dev_minor(sbuf.st_rdev));
2562 } else
2563 #endif
2564 d_printf("Inode: %.0f\tLinks: %u\n",
2565 (double)sbuf.st_ino,
2566 (unsigned int)sbuf.st_nlink);
2568 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2569 ((int)sbuf.st_mode & 0777),
2570 unix_mode_to_str(mode_str, sbuf.st_mode),
2571 (unsigned int)sbuf.st_uid,
2572 (unsigned int)sbuf.st_gid);
2574 lt = localtime(&sbuf.st_atime);
2575 if (lt) {
2576 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
2577 } else {
2578 fstrcpy(mode_str, "unknown");
2580 d_printf("Access: %s\n", mode_str);
2582 lt = localtime(&sbuf.st_mtime);
2583 if (lt) {
2584 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
2585 } else {
2586 fstrcpy(mode_str, "unknown");
2588 d_printf("Modify: %s\n", mode_str);
2590 lt = localtime(&sbuf.st_ctime);
2591 if (lt) {
2592 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
2593 } else {
2594 fstrcpy(mode_str, "unknown");
2596 d_printf("Change: %s\n", mode_str);
2598 return 0;
2602 /****************************************************************************
2603 UNIX chown.
2604 ****************************************************************************/
2606 static int cmd_chown(void)
2608 pstring src;
2609 uid_t uid;
2610 gid_t gid;
2611 pstring buf, buf2, buf3;
2612 struct cli_state *targetcli;
2613 pstring targetname;
2615 pstrcpy(src,cur_dir);
2617 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2618 !next_token_nr(NULL,buf2,NULL, sizeof(buf2)) ||
2619 !next_token_nr(NULL,buf3,NULL, sizeof(buf3))) {
2620 d_printf("chown uid gid file\n");
2621 return 1;
2624 uid = (uid_t)atoi(buf);
2625 gid = (gid_t)atoi(buf2);
2626 pstrcat(src,buf3);
2628 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2629 d_printf("chown %s: %s\n", src, cli_errstr(cli));
2630 return 1;
2633 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2634 d_printf("Server doesn't support UNIX CIFS calls.\n");
2635 return 1;
2638 if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
2639 d_printf("%s chown file %s uid=%d, gid=%d\n",
2640 cli_errstr(targetcli), src, (int)uid, (int)gid);
2641 return 1;
2644 return 0;
2647 /****************************************************************************
2648 Rename some file.
2649 ****************************************************************************/
2651 static int cmd_rename(void)
2653 pstring src,dest;
2654 pstring buf,buf2;
2655 struct cli_state *targetcli;
2656 pstring targetsrc;
2657 pstring targetdest;
2659 pstrcpy(src,cur_dir);
2660 pstrcpy(dest,cur_dir);
2662 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2663 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2664 d_printf("rename <src> <dest>\n");
2665 return 1;
2668 pstrcat(src,buf);
2669 pstrcat(dest,buf2);
2671 if ( !cli_resolve_path( "", cli, src, &targetcli, targetsrc ) ) {
2672 d_printf("rename %s: %s\n", src, cli_errstr(cli));
2673 return 1;
2676 if ( !cli_resolve_path( "", cli, dest, &targetcli, targetdest ) ) {
2677 d_printf("rename %s: %s\n", dest, cli_errstr(cli));
2678 return 1;
2681 if (!cli_rename(targetcli, targetsrc, targetdest)) {
2682 d_printf("%s renaming files %s -> %s \n",
2683 cli_errstr(targetcli),
2684 targetsrc,
2685 targetdest);
2686 return 1;
2689 return 0;
2692 /****************************************************************************
2693 Print the volume name.
2694 ****************************************************************************/
2696 static int cmd_volume(void)
2698 fstring volname;
2699 uint32 serial_num;
2700 time_t create_date;
2702 if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
2703 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
2704 return 1;
2707 d_printf("Volume: |%s| serial number 0x%x\n", volname, (unsigned int)serial_num);
2708 return 0;
2711 /****************************************************************************
2712 Hard link files using the NT call.
2713 ****************************************************************************/
2715 static int cmd_hardlink(void)
2717 pstring src,dest;
2718 pstring buf,buf2;
2719 struct cli_state *targetcli;
2720 pstring targetname;
2722 pstrcpy(src,cur_dir);
2723 pstrcpy(dest,cur_dir);
2725 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2726 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2727 d_printf("hardlink <src> <dest>\n");
2728 return 1;
2731 pstrcat(src,buf);
2732 pstrcat(dest,buf2);
2734 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2735 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
2736 return 1;
2739 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2740 d_printf("Server doesn't support UNIX CIFS calls.\n");
2741 return 1;
2744 if (!cli_nt_hardlink(targetcli, targetname, dest)) {
2745 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
2746 return 1;
2749 return 0;
2752 /****************************************************************************
2753 Toggle the prompt flag.
2754 ****************************************************************************/
2756 static int cmd_prompt(void)
2758 prompt = !prompt;
2759 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2761 return 1;
2764 /****************************************************************************
2765 Set the newer than time.
2766 ****************************************************************************/
2768 static int cmd_newer(void)
2770 pstring buf;
2771 BOOL ok;
2772 SMB_STRUCT_STAT sbuf;
2774 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2775 if (ok && (sys_stat(buf,&sbuf) == 0)) {
2776 newer_than = sbuf.st_mtime;
2777 DEBUG(1,("Getting files newer than %s",
2778 time_to_asc(newer_than)));
2779 } else {
2780 newer_than = 0;
2783 if (ok && newer_than == 0) {
2784 d_printf("Error setting newer-than time\n");
2785 return 1;
2788 return 0;
2791 /****************************************************************************
2792 Set the archive level.
2793 ****************************************************************************/
2795 static int cmd_archive(void)
2797 pstring buf;
2799 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2800 archive_level = atoi(buf);
2801 } else
2802 d_printf("Archive level is %d\n",archive_level);
2804 return 0;
2807 /****************************************************************************
2808 Toggle the lowercaseflag.
2809 ****************************************************************************/
2811 static int cmd_lowercase(void)
2813 lowercase = !lowercase;
2814 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2816 return 0;
2819 /****************************************************************************
2820 Toggle the case sensitive flag.
2821 ****************************************************************************/
2823 static int cmd_setcase(void)
2825 BOOL orig_case_sensitive = cli_set_case_sensitive(cli, False);
2827 cli_set_case_sensitive(cli, !orig_case_sensitive);
2828 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
2829 "on":"off"));
2831 return 0;
2834 /****************************************************************************
2835 Toggle the showacls flag.
2836 ****************************************************************************/
2838 static int cmd_showacls(void)
2840 showacls = !showacls;
2841 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
2843 if (!ctx && showacls)
2844 ctx = talloc_init("smbclient:showacls");
2845 if (!ctx) {
2846 DEBUG( 0, ("cmd_showacls() out of memory. talloc_init() failed.\n"));
2849 return 0;
2853 /****************************************************************************
2854 Toggle the recurse flag.
2855 ****************************************************************************/
2857 static int cmd_recurse(void)
2859 recurse = !recurse;
2860 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2862 return 0;
2865 /****************************************************************************
2866 Toggle the translate flag.
2867 ****************************************************************************/
2869 static int cmd_translate(void)
2871 translation = !translation;
2872 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2873 translation?"on":"off"));
2875 return 0;
2878 /****************************************************************************
2879 Do the lcd command.
2880 ****************************************************************************/
2882 static int cmd_lcd(void)
2884 pstring buf;
2885 pstring d;
2887 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2888 chdir(buf);
2889 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2891 return 0;
2894 /****************************************************************************
2895 Get a file restarting at end of local file.
2896 ****************************************************************************/
2898 static int cmd_reget(void)
2900 pstring local_name;
2901 pstring remote_name;
2902 char *p;
2904 pstrcpy(remote_name, cur_dir);
2905 pstrcat(remote_name, CLI_DIRSEP_STR);
2907 p = remote_name + strlen(remote_name);
2909 if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2910 d_printf("reget <filename>\n");
2911 return 1;
2913 pstrcpy(local_name, p);
2914 clean_name(remote_name);
2916 next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2918 return do_get(remote_name, local_name, True);
2921 /****************************************************************************
2922 Put a file restarting at end of local file.
2923 ****************************************************************************/
2925 static int cmd_reput(void)
2927 pstring local_name;
2928 pstring remote_name;
2929 pstring buf;
2930 char *p = buf;
2931 SMB_STRUCT_STAT st;
2933 pstrcpy(remote_name, cur_dir);
2934 pstrcat(remote_name, CLI_DIRSEP_STR);
2936 if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2937 d_printf("reput <filename>\n");
2938 return 1;
2940 pstrcpy(local_name, p);
2942 if (!file_exist(local_name, &st)) {
2943 d_printf("%s does not exist\n", local_name);
2944 return 1;
2947 if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2948 pstrcat(remote_name, p);
2949 else
2950 pstrcat(remote_name, local_name);
2952 clean_name(remote_name);
2954 return do_put(remote_name, local_name, True);
2957 /****************************************************************************
2958 List a share name.
2959 ****************************************************************************/
2961 static void browse_fn(const char *name, uint32 m,
2962 const char *comment, void *state)
2964 fstring typestr;
2966 *typestr=0;
2968 switch (m & 7)
2970 case STYPE_DISKTREE:
2971 fstrcpy(typestr,"Disk"); break;
2972 case STYPE_PRINTQ:
2973 fstrcpy(typestr,"Printer"); break;
2974 case STYPE_DEVICE:
2975 fstrcpy(typestr,"Device"); break;
2976 case STYPE_IPC:
2977 fstrcpy(typestr,"IPC"); break;
2979 /* FIXME: If the remote machine returns non-ascii characters
2980 in any of these fields, they can corrupt the output. We
2981 should remove them. */
2982 if (!grepable) {
2983 d_printf("\t%-15s %-10.10s%s\n",
2984 name,typestr,comment);
2985 } else {
2986 d_printf ("%s|%s|%s\n",typestr,name,comment);
2990 static BOOL browse_host_rpc(BOOL sort)
2992 NTSTATUS status;
2993 struct rpc_pipe_client *pipe_hnd;
2994 TALLOC_CTX *mem_ctx;
2995 ENUM_HND enum_hnd;
2996 WERROR werr;
2997 SRV_SHARE_INFO_CTR ctr;
2998 int i;
3000 mem_ctx = talloc_new(NULL);
3001 if (mem_ctx == NULL) {
3002 DEBUG(0, ("talloc_new failed\n"));
3003 return False;
3006 init_enum_hnd(&enum_hnd, 0);
3008 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
3010 if (pipe_hnd == NULL) {
3011 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
3012 nt_errstr(status)));
3013 TALLOC_FREE(mem_ctx);
3014 return False;
3017 werr = rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, 1, &ctr,
3018 0xffffffff, &enum_hnd);
3020 if (!W_ERROR_IS_OK(werr)) {
3021 TALLOC_FREE(mem_ctx);
3022 cli_rpc_pipe_close(pipe_hnd);
3023 return False;
3026 for (i=0; i<ctr.num_entries; i++) {
3027 SRV_SHARE_INFO_1 *info = &ctr.share.info1[i];
3028 char *name, *comment;
3029 name = rpcstr_pull_unistr2_talloc(
3030 mem_ctx, &info->info_1_str.uni_netname);
3031 comment = rpcstr_pull_unistr2_talloc(
3032 mem_ctx, &info->info_1_str.uni_remark);
3033 browse_fn(name, info->info_1.type, comment, NULL);
3036 TALLOC_FREE(mem_ctx);
3037 cli_rpc_pipe_close(pipe_hnd);
3038 return True;
3041 /****************************************************************************
3042 Try and browse available connections on a host.
3043 ****************************************************************************/
3045 static BOOL browse_host(BOOL sort)
3047 int ret;
3048 if (!grepable) {
3049 d_printf("\n\tSharename Type Comment\n");
3050 d_printf("\t--------- ---- -------\n");
3053 if (browse_host_rpc(sort)) {
3054 return True;
3057 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
3058 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
3060 return (ret != -1);
3063 /****************************************************************************
3064 List a server name.
3065 ****************************************************************************/
3067 static void server_fn(const char *name, uint32 m,
3068 const char *comment, void *state)
3071 if (!grepable){
3072 d_printf("\t%-16s %s\n", name, comment);
3073 } else {
3074 d_printf("%s|%s|%s\n",(char *)state, name, comment);
3078 /****************************************************************************
3079 Try and browse available connections on a host.
3080 ****************************************************************************/
3082 static BOOL list_servers(const char *wk_grp)
3084 fstring state;
3086 if (!cli->server_domain)
3087 return False;
3089 if (!grepable) {
3090 d_printf("\n\tServer Comment\n");
3091 d_printf("\t--------- -------\n");
3093 fstrcpy( state, "Server" );
3094 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
3095 state);
3097 if (!grepable) {
3098 d_printf("\n\tWorkgroup Master\n");
3099 d_printf("\t--------- -------\n");
3102 fstrcpy( state, "Workgroup" );
3103 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
3104 server_fn, state);
3105 return True;
3108 /****************************************************************************
3109 Print or set current VUID
3110 ****************************************************************************/
3112 static int cmd_vuid(void)
3114 fstring buf;
3116 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3117 d_printf("Current VUID is %d\n", cli->vuid);
3118 return 0;
3121 cli->vuid = atoi(buf);
3122 return 0;
3125 /****************************************************************************
3126 Setup a new VUID, by issuing a session setup
3127 ****************************************************************************/
3129 static int cmd_logon(void)
3131 pstring l_username, l_password;
3132 pstring buf,buf2;
3134 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3135 d_printf("logon <username> [<password>]\n");
3136 return 0;
3139 pstrcpy(l_username, buf);
3141 if (!next_token_nr(NULL,buf2,NULL,sizeof(buf)))
3143 char *pass = getpass("Password: ");
3144 if (pass)
3145 pstrcpy(l_password, pass);
3147 else
3148 pstrcpy(l_password, buf2);
3150 if (!NT_STATUS_IS_OK(cli_session_setup(cli, l_username,
3151 l_password, strlen(l_password),
3152 l_password, strlen(l_password),
3153 lp_workgroup()))) {
3154 d_printf("session setup failed: %s\n", cli_errstr(cli));
3155 return -1;
3158 d_printf("Current VUID is %d\n", cli->vuid);
3159 return 0;
3163 /****************************************************************************
3164 list active connections
3165 ****************************************************************************/
3167 static int cmd_list_connect(void)
3169 cli_cm_display();
3171 return 0;
3174 /****************************************************************************
3175 display the current active client connection
3176 ****************************************************************************/
3178 static int cmd_show_connect( void )
3180 struct cli_state *targetcli;
3181 pstring targetpath;
3183 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
3184 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
3185 return 1;
3188 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
3189 return 0;
3192 /* Some constants for completing filename arguments */
3194 #define COMPL_NONE 0 /* No completions */
3195 #define COMPL_REMOTE 1 /* Complete remote filename */
3196 #define COMPL_LOCAL 2 /* Complete local filename */
3198 /* This defines the commands supported by this client.
3199 * NOTE: The "!" must be the last one in the list because it's fn pointer
3200 * field is NULL, and NULL in that field is used in process_tok()
3201 * (below) to indicate the end of the list. crh
3203 static struct
3205 const char *name;
3206 int (*fn)(void);
3207 const char *description;
3208 char compl_args[2]; /* Completion argument info */
3209 } commands[] = {
3210 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
3211 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
3212 {"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}},
3213 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
3214 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
3215 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
3216 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
3217 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
3218 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
3219 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
3220 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3221 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3222 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3223 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3224 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
3225 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
3226 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
3227 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
3228 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
3229 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
3230 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
3231 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
3232 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
3233 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3234 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
3235 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
3236 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
3237 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
3238 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
3239 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
3240 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
3241 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
3242 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
3243 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3244 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3245 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3246 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3247 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
3248 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
3249 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
3250 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
3251 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3252 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
3253 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3254 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
3255 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
3256 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
3257 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
3258 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
3259 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3260 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
3261 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
3262 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
3263 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
3264 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
3265 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
3266 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
3267 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
3268 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
3269 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
3270 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
3271 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3272 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
3273 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
3274 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
3276 /* Yes, this must be here, see crh's comment above. */
3277 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
3278 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
3281 /*******************************************************************
3282 Lookup a command string in the list of commands, including
3283 abbreviations.
3284 ******************************************************************/
3286 static int process_tok(pstring tok)
3288 int i = 0, matches = 0;
3289 int cmd=0;
3290 int tok_len = strlen(tok);
3292 while (commands[i].fn != NULL) {
3293 if (strequal(commands[i].name,tok)) {
3294 matches = 1;
3295 cmd = i;
3296 break;
3297 } else if (strnequal(commands[i].name, tok, tok_len)) {
3298 matches++;
3299 cmd = i;
3301 i++;
3304 if (matches == 0)
3305 return(-1);
3306 else if (matches == 1)
3307 return(cmd);
3308 else
3309 return(-2);
3312 /****************************************************************************
3313 Help.
3314 ****************************************************************************/
3316 static int cmd_help(void)
3318 int i=0,j;
3319 pstring buf;
3321 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3322 if ((i = process_tok(buf)) >= 0)
3323 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
3324 } else {
3325 while (commands[i].description) {
3326 for (j=0; commands[i].description && (j<5); j++) {
3327 d_printf("%-15s",commands[i].name);
3328 i++;
3330 d_printf("\n");
3333 return 0;
3336 /****************************************************************************
3337 Process a -c command string.
3338 ****************************************************************************/
3340 static int process_command_string(char *cmd)
3342 pstring line;
3343 const char *ptr;
3344 int rc = 0;
3346 /* establish the connection if not already */
3348 if (!cli) {
3349 cli = cli_cm_open(desthost, service, True);
3350 if (!cli)
3351 return 0;
3354 while (cmd[0] != '\0') {
3355 char *p;
3356 pstring tok;
3357 int i;
3359 if ((p = strchr_m(cmd, ';')) == 0) {
3360 strncpy(line, cmd, 999);
3361 line[1000] = '\0';
3362 cmd += strlen(cmd);
3363 } else {
3364 if (p - cmd > 999)
3365 p = cmd + 999;
3366 strncpy(line, cmd, p - cmd);
3367 line[p - cmd] = '\0';
3368 cmd = p + 1;
3371 /* and get the first part of the command */
3372 ptr = line;
3373 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3375 if ((i = process_tok(tok)) >= 0) {
3376 rc = commands[i].fn();
3377 } else if (i == -2) {
3378 d_printf("%s: command abbreviation ambiguous\n",tok);
3379 } else {
3380 d_printf("%s: command not found\n",tok);
3384 return rc;
3387 #define MAX_COMPLETIONS 100
3389 typedef struct {
3390 pstring dirmask;
3391 char **matches;
3392 int count, samelen;
3393 const char *text;
3394 int len;
3395 } completion_remote_t;
3397 static void completion_remote_filter(const char *mnt, file_info *f, const char *mask, void *state)
3399 completion_remote_t *info = (completion_remote_t *)state;
3401 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
3402 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
3403 info->matches[info->count] = SMB_STRDUP(f->name);
3404 else {
3405 pstring tmp;
3407 if (info->dirmask[0] != 0)
3408 pstrcpy(tmp, info->dirmask);
3409 else
3410 tmp[0] = 0;
3411 pstrcat(tmp, f->name);
3412 if (f->mode & aDIR)
3413 pstrcat(tmp, "/");
3414 info->matches[info->count] = SMB_STRDUP(tmp);
3416 if (info->matches[info->count] == NULL)
3417 return;
3418 if (f->mode & aDIR)
3419 smb_readline_ca_char(0);
3421 if (info->count == 1)
3422 info->samelen = strlen(info->matches[info->count]);
3423 else
3424 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
3425 info->samelen--;
3426 info->count++;
3430 static char **remote_completion(const char *text, int len)
3432 pstring dirmask;
3433 int i;
3434 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
3436 /* can't have non-static intialisation on Sun CC, so do it
3437 at run time here */
3438 info.samelen = len;
3439 info.text = text;
3440 info.len = len;
3442 if (len >= MIN(PATH_MAX,sizeof(pstring))) {
3443 return(NULL);
3446 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
3447 if (!info.matches) {
3448 return NULL;
3452 * We're leaving matches[0] free to fill it later with the text to
3453 * display: Either the one single match or the longest common subset
3454 * of the matches.
3456 info.matches[0] = NULL;
3457 info.count = 1;
3459 for (i = len-1; i >= 0; i--) {
3460 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
3461 break;
3465 info.text = text+i+1;
3466 info.samelen = info.len = len-i-1;
3468 if (i > 0) {
3469 strncpy(info.dirmask, text, i+1);
3470 info.dirmask[i+1] = 0;
3471 pstr_sprintf(dirmask, "%s%*s*", cur_dir, i-1, text);
3472 } else {
3473 pstr_sprintf(dirmask, "%s*", cur_dir);
3476 if (cli_list(cli, dirmask, aDIR | aSYSTEM | aHIDDEN, completion_remote_filter, &info) < 0)
3477 goto cleanup;
3479 if (info.count == 1) {
3482 * No matches at all, NULL indicates there is nothing
3485 SAFE_FREE(info.matches[0]);
3486 SAFE_FREE(info.matches);
3487 return NULL;
3490 if (info.count == 2) {
3493 * Exactly one match in matches[1], indicate this is the one
3494 * in matches[0].
3497 info.matches[0] = info.matches[1];
3498 info.matches[1] = NULL;
3499 info.count -= 1;
3500 return info.matches;
3504 * We got more than one possible match, set the result to the maximum
3505 * common subset
3508 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
3509 info.matches[info.count] = NULL;
3510 return info.matches;
3512 cleanup:
3513 for (i = 0; i < info.count; i++)
3514 free(info.matches[i]);
3515 free(info.matches);
3516 return NULL;
3519 static char **completion_fn(const char *text, int start, int end)
3521 smb_readline_ca_char(' ');
3523 if (start) {
3524 const char *buf, *sp;
3525 int i;
3526 char compl_type;
3528 buf = smb_readline_get_line_buffer();
3529 if (buf == NULL)
3530 return NULL;
3532 sp = strchr(buf, ' ');
3533 if (sp == NULL)
3534 return NULL;
3536 for (i = 0; commands[i].name; i++) {
3537 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
3538 (commands[i].name[sp - buf] == 0)) {
3539 break;
3542 if (commands[i].name == NULL)
3543 return NULL;
3545 while (*sp == ' ')
3546 sp++;
3548 if (sp == (buf + start))
3549 compl_type = commands[i].compl_args[0];
3550 else
3551 compl_type = commands[i].compl_args[1];
3553 if (compl_type == COMPL_REMOTE)
3554 return remote_completion(text, end - start);
3555 else /* fall back to local filename completion */
3556 return NULL;
3557 } else {
3558 char **matches;
3559 int i, len, samelen = 0, count=1;
3561 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
3562 if (!matches) {
3563 return NULL;
3565 matches[0] = NULL;
3567 len = strlen(text);
3568 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3569 if (strncmp(text, commands[i].name, len) == 0) {
3570 matches[count] = SMB_STRDUP(commands[i].name);
3571 if (!matches[count])
3572 goto cleanup;
3573 if (count == 1)
3574 samelen = strlen(matches[count]);
3575 else
3576 while (strncmp(matches[count], matches[count-1], samelen) != 0)
3577 samelen--;
3578 count++;
3582 switch (count) {
3583 case 0: /* should never happen */
3584 case 1:
3585 goto cleanup;
3586 case 2:
3587 matches[0] = SMB_STRDUP(matches[1]);
3588 break;
3589 default:
3590 matches[0] = (char *)SMB_MALLOC(samelen+1);
3591 if (!matches[0])
3592 goto cleanup;
3593 strncpy(matches[0], matches[1], samelen);
3594 matches[0][samelen] = 0;
3596 matches[count] = NULL;
3597 return matches;
3599 cleanup:
3600 for (i = 0; i < count; i++)
3601 free(matches[i]);
3603 free(matches);
3604 return NULL;
3608 /****************************************************************************
3609 Make sure we swallow keepalives during idle time.
3610 ****************************************************************************/
3612 static void readline_callback(void)
3614 fd_set fds;
3615 struct timeval timeout;
3616 static time_t last_t;
3617 time_t t;
3619 t = time(NULL);
3621 if (t - last_t < 5)
3622 return;
3624 last_t = t;
3626 again:
3628 if (cli->fd == -1)
3629 return;
3631 FD_ZERO(&fds);
3632 FD_SET(cli->fd,&fds);
3634 timeout.tv_sec = 0;
3635 timeout.tv_usec = 0;
3636 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
3638 /* We deliberately use receive_smb instead of
3639 client_receive_smb as we want to receive
3640 session keepalives and then drop them here.
3642 if (FD_ISSET(cli->fd,&fds)) {
3643 if (!receive_smb(cli->fd,cli->inbuf,0)) {
3644 DEBUG(0, ("Read from server failed, maybe it closed the "
3645 "connection\n"));
3646 return;
3648 goto again;
3651 /* Ping the server to keep the connection alive using SMBecho. */
3653 unsigned char garbage[16];
3654 memset(garbage, 0xf0, sizeof(garbage));
3655 cli_echo(cli, garbage, sizeof(garbage));
3659 /****************************************************************************
3660 Process commands on stdin.
3661 ****************************************************************************/
3663 static int process_stdin(void)
3665 const char *ptr;
3666 int rc = 0;
3668 while (1) {
3669 pstring tok;
3670 pstring the_prompt;
3671 char *cline;
3672 pstring line;
3673 int i;
3675 /* display a prompt */
3676 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3677 cline = smb_readline(the_prompt, readline_callback, completion_fn);
3679 if (!cline) break;
3681 pstrcpy(line, cline);
3683 /* special case - first char is ! */
3684 if (*line == '!') {
3685 system(line + 1);
3686 continue;
3689 /* and get the first part of the command */
3690 ptr = line;
3691 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3693 if ((i = process_tok(tok)) >= 0) {
3694 rc = commands[i].fn();
3695 } else if (i == -2) {
3696 d_printf("%s: command abbreviation ambiguous\n",tok);
3697 } else {
3698 d_printf("%s: command not found\n",tok);
3701 return rc;
3704 /****************************************************************************
3705 Process commands from the client.
3706 ****************************************************************************/
3708 static int process(char *base_directory)
3710 int rc = 0;
3712 cli = cli_cm_open(desthost, service, True);
3713 if (!cli) {
3714 return 1;
3717 if (*base_directory) {
3718 rc = do_cd(base_directory);
3719 if (rc) {
3720 cli_cm_shutdown();
3721 return rc;
3725 if (cmdstr) {
3726 rc = process_command_string(cmdstr);
3727 } else {
3728 process_stdin();
3731 cli_cm_shutdown();
3732 return rc;
3735 /****************************************************************************
3736 Handle a -L query.
3737 ****************************************************************************/
3739 static int do_host_query(char *query_host)
3741 cli = cli_cm_open(query_host, "IPC$", True);
3742 if (!cli)
3743 return 1;
3745 browse_host(True);
3747 if (port != 139) {
3749 /* Workgroups simply don't make sense over anything
3750 else but port 139... */
3752 cli_cm_shutdown();
3753 cli_cm_set_port( 139 );
3754 cli = cli_cm_open(query_host, "IPC$", True);
3757 if (cli == NULL) {
3758 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3759 return 1;
3762 list_servers(lp_workgroup());
3764 cli_cm_shutdown();
3766 return(0);
3769 /****************************************************************************
3770 Handle a tar operation.
3771 ****************************************************************************/
3773 static int do_tar_op(char *base_directory)
3775 int ret;
3777 /* do we already have a connection? */
3778 if (!cli) {
3779 cli = cli_cm_open(desthost, service, True);
3780 if (!cli)
3781 return 1;
3784 recurse=True;
3786 if (*base_directory) {
3787 ret = do_cd(base_directory);
3788 if (ret) {
3789 cli_cm_shutdown();
3790 return ret;
3794 ret=process_tar();
3796 cli_cm_shutdown();
3798 return(ret);
3801 /****************************************************************************
3802 Handle a message operation.
3803 ****************************************************************************/
3805 static int do_message_op(void)
3807 struct in_addr ip;
3808 struct nmb_name called, calling;
3809 fstring server_name;
3810 char name_type_hex[10];
3811 int msg_port;
3812 NTSTATUS status;
3814 make_nmb_name(&calling, calling_name, 0x0);
3815 make_nmb_name(&called , desthost, name_type);
3817 fstrcpy(server_name, desthost);
3818 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
3819 fstrcat(server_name, name_type_hex);
3821 zero_ip(&ip);
3822 if (have_ip)
3823 ip = dest_ip;
3825 /* we can only do messages over port 139 (to windows clients at least) */
3827 msg_port = port ? port : 139;
3829 if (!(cli=cli_initialise()) || (cli_set_port(cli, msg_port) != msg_port)) {
3830 d_printf("Connection to %s failed\n", desthost);
3831 return 1;
3834 status = cli_connect(cli, server_name, &ip);
3835 if (!NT_STATUS_IS_OK(status)) {
3836 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
3837 return 1;
3840 if (!cli_session_request(cli, &calling, &called)) {
3841 d_printf("session request failed\n");
3842 cli_cm_shutdown();
3843 return 1;
3846 send_message();
3847 cli_cm_shutdown();
3849 return 0;
3853 /****************************************************************************
3854 main program
3855 ****************************************************************************/
3857 int main(int argc,char *argv[])
3859 pstring base_directory;
3860 int opt;
3861 pstring query_host;
3862 BOOL message = False;
3863 pstring term_code;
3864 static const char *new_name_resolve_order = NULL;
3865 poptContext pc;
3866 char *p;
3867 int rc = 0;
3868 fstring new_workgroup;
3869 BOOL tar_opt = False;
3870 BOOL service_opt = False;
3871 struct poptOption long_options[] = {
3872 POPT_AUTOHELP
3874 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3875 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3876 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3877 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3878 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3879 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3880 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
3881 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3882 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3883 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
3884 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
3885 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3886 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
3887 POPT_COMMON_SAMBA
3888 POPT_COMMON_CONNECTION
3889 POPT_COMMON_CREDENTIALS
3890 POPT_TABLEEND
3893 load_case_tables();
3895 #ifdef KANJI
3896 pstrcpy(term_code, KANJI);
3897 #else /* KANJI */
3898 *term_code = 0;
3899 #endif /* KANJI */
3901 *query_host = 0;
3902 *base_directory = 0;
3904 /* initialize the workgroup name so we can determine whether or
3905 not it was set by a command line option */
3907 set_global_myworkgroup( "" );
3908 set_global_myname( "" );
3910 /* set default debug level to 0 regardless of what smb.conf sets */
3911 setup_logging( "smbclient", True );
3912 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
3913 if ((dbf = x_fdup(x_stderr))) {
3914 x_setbuf( dbf, NULL );
3917 /* skip argv(0) */
3918 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
3919 poptSetOtherOptionHelp(pc, "service <password>");
3921 in_client = True; /* Make sure that we tell lp_load we are */
3923 while ((opt = poptGetNextOpt(pc)) != -1) {
3925 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
3926 /* I see no other way to keep things sane --SSS */
3927 if (tar_opt == True) {
3928 while (poptPeekArg(pc)) {
3929 poptGetArg(pc);
3931 tar_opt = False;
3934 /* if the service has not yet been specified lets see if it is available in the popt stack */
3935 if (!service_opt && poptPeekArg(pc)) {
3936 pstrcpy(service, poptGetArg(pc));
3937 service_opt = True;
3940 /* if the service has already been retrieved then check if we have also a password */
3941 if (service_opt && (!cmdline_auth_info.got_pass) && poptPeekArg(pc)) {
3942 pstrcpy(cmdline_auth_info.password, poptGetArg(pc));
3943 cmdline_auth_info.got_pass = True;
3946 switch (opt) {
3947 case 'M':
3948 /* Messages are sent to NetBIOS name type 0x3
3949 * (Messenger Service). Make sure we default
3950 * to port 139 instead of port 445. srl,crh
3952 name_type = 0x03;
3953 cli_cm_set_dest_name_type( name_type );
3954 pstrcpy(desthost,poptGetOptArg(pc));
3955 if( !port )
3956 cli_cm_set_port( 139 );
3957 message = True;
3958 break;
3959 case 'I':
3961 dest_ip = *interpret_addr2(poptGetOptArg(pc));
3962 if (is_zero_ip(dest_ip))
3963 exit(1);
3964 have_ip = True;
3966 cli_cm_set_dest_ip( dest_ip );
3968 break;
3969 case 'E':
3970 if (dbf) {
3971 x_fclose(dbf);
3973 dbf = x_stderr;
3974 display_set_stderr();
3975 break;
3977 case 'L':
3978 pstrcpy(query_host, poptGetOptArg(pc));
3979 break;
3980 case 't':
3981 pstrcpy(term_code, poptGetOptArg(pc));
3982 break;
3983 case 'm':
3984 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
3985 break;
3986 case 'T':
3987 /* We must use old option processing for this. Find the
3988 * position of the -T option in the raw argv[]. */
3990 int i;
3991 for (i = 1; i < argc; i++) {
3992 if (strncmp("-T", argv[i],2)==0)
3993 break;
3995 i++;
3996 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
3997 poptPrintUsage(pc, stderr, 0);
3998 exit(1);
4001 /* this must be the last option, mark we have parsed it so that we know we have */
4002 tar_opt = True;
4003 break;
4004 case 'D':
4005 pstrcpy(base_directory,poptGetOptArg(pc));
4006 break;
4007 case 'g':
4008 grepable=True;
4009 break;
4013 /* We may still have some leftovers after the last popt option has been called */
4014 if (tar_opt == True) {
4015 while (poptPeekArg(pc)) {
4016 poptGetArg(pc);
4018 tar_opt = False;
4021 /* if the service has not yet been specified lets see if it is available in the popt stack */
4022 if (!service_opt && poptPeekArg(pc)) {
4023 pstrcpy(service, poptGetArg(pc));
4024 service_opt = True;
4027 /* if the service has already been retrieved then check if we have also a password */
4028 if (service_opt && (!cmdline_auth_info.got_pass) && poptPeekArg(pc)) {
4029 pstrcpy(cmdline_auth_info.password, poptGetArg(pc));
4030 cmdline_auth_info.got_pass = True;
4033 /* check for the -P option */
4035 if ( port != 0 )
4036 cli_cm_set_port( port );
4039 * Don't load debug level from smb.conf. It should be
4040 * set by cmdline arg or remain default (0)
4042 AllowDebugChange = False;
4044 /* save the workgroup...
4046 FIXME!! do we need to do this for other options as well
4047 (or maybe a generic way to keep lp_load() from overwriting
4048 everything)? */
4050 fstrcpy( new_workgroup, lp_workgroup() );
4051 pstrcpy( calling_name, global_myname() );
4053 if ( override_logfile )
4054 setup_logging( lp_logfile(), False );
4056 if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
4057 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
4058 argv[0], dyn_CONFIGFILE);
4061 load_interfaces();
4063 if (service_opt) {
4064 /* Convert any '/' characters in the service name to '\' characters */
4065 string_replace(service, '/','\\');
4066 if (count_chars(service,'\\') < 3) {
4067 d_printf("\n%s: Not enough '\\' characters in service\n",service);
4068 poptPrintUsage(pc, stderr, 0);
4069 exit(1);
4073 if ( strlen(new_workgroup) != 0 )
4074 set_global_myworkgroup( new_workgroup );
4076 if ( strlen(calling_name) != 0 )
4077 set_global_myname( calling_name );
4078 else
4079 pstrcpy( calling_name, global_myname() );
4081 init_names();
4083 if(new_name_resolve_order)
4084 lp_set_name_resolve_order(new_name_resolve_order);
4086 if (!tar_type && !*query_host && !*service && !message) {
4087 poptPrintUsage(pc, stderr, 0);
4088 exit(1);
4091 poptFreeContext(pc);
4093 /* store the username an password for dfs support */
4095 cli_cm_set_credentials( &cmdline_auth_info );
4096 pstrcpy(username, cmdline_auth_info.username);
4098 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
4100 if (tar_type) {
4101 if (cmdstr)
4102 process_command_string(cmdstr);
4103 return do_tar_op(base_directory);
4106 if (*query_host) {
4107 char *qhost = query_host;
4108 char *slash;
4110 while (*qhost == '\\' || *qhost == '/')
4111 qhost++;
4113 if ((slash = strchr_m(qhost, '/'))
4114 || (slash = strchr_m(qhost, '\\'))) {
4115 *slash = 0;
4118 if ((p=strchr_m(qhost, '#'))) {
4119 *p = 0;
4120 p++;
4121 sscanf(p, "%x", &name_type);
4122 cli_cm_set_dest_name_type( name_type );
4125 return do_host_query(qhost);
4128 if (message) {
4129 return do_message_op();
4132 if (process(base_directory)) {
4133 return 1;
4136 talloc_destroy( ctx);
4137 return rc;