2 Unix SMB/CIFS implementation.
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.
25 #include "client/client_proto.h"
26 #include "include/rpc_client.h"
31 extern BOOL AllowDebugChange;
32 extern BOOL override_logfile;
34 extern BOOL in_client;
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 /* 30 second timeout on most commands */
54 #define CLIENT_TIMEOUT (30*1000)
55 #define SHORT_TIMEOUT (5*1000)
57 /* value for unused fid field in trans2 secondary request */
58 #define FID_UNUSED (0xFFFF)
60 time_t newer_than = 0;
61 static int archive_level = 0;
63 static BOOL translation = False;
66 /* clitar bits insert */
69 extern BOOL tar_reset;
73 static BOOL prompt = True;
75 static BOOL recurse = False;
76 BOOL lowercase = False;
78 static struct in_addr dest_ip;
80 #define SEPARATORS " \t\n\r"
82 static BOOL abort_mget = True;
84 static pstring fileselection = "";
86 extern file_info def_finfo;
89 SMB_BIG_UINT get_total_size = 0;
90 unsigned int get_total_time_ms = 0;
91 static SMB_BIG_UINT put_total_size = 0;
92 static unsigned int put_total_time_ms = 0;
95 static double dir_total;
97 /* root cli_state connection */
99 struct cli_state *cli;
101 static char CLI_DIRSEP_CHAR = '\\';
102 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
104 /****************************************************************************
105 Write to a local file with CR/LF->LF translation if appropriate. Return the
106 number taken from the buffer. This may not equal the number written.
107 ****************************************************************************/
109 static int writefile(int f, char *b, int n)
119 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
122 if (write(f, b, 1) != 1) {
132 /****************************************************************************
133 Read from a file with LF->CR/LF translation if appropriate. Return the
134 number read. read approx n bytes.
135 ****************************************************************************/
137 static int readfile(char *b, int n, XFILE *f)
143 return x_fread(b,1,n,f);
146 while (i < (n - 1) && (i < BUFFER_SIZE)) {
147 if ((c = x_getc(f)) == EOF) {
151 if (c == '\n') { /* change all LFs to CR/LF */
161 /****************************************************************************
163 ****************************************************************************/
165 static void send_message(void)
170 if (!cli_message_start(cli, desthost, username, &grp_id)) {
171 d_printf("message start: %s\n", cli_errstr(cli));
176 d_printf("Connected. Type your message, ending it with a Control-D\n");
178 while (!feof(stdin) && total_len < 1600) {
179 int maxlen = MIN(1600 - total_len,127);
186 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
192 if (!cli_message_text(cli, msg, l, grp_id)) {
193 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
200 if (total_len >= 1600)
201 d_printf("the message was truncated to 1600 bytes\n");
203 d_printf("sent %d bytes\n",total_len);
205 if (!cli_message_end(cli, grp_id)) {
206 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
211 /****************************************************************************
212 Check the space on a device.
213 ****************************************************************************/
215 static int do_dskattr(void)
217 int total, bsize, avail;
218 struct cli_state *targetcli;
221 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
222 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
226 if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
227 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli));
231 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
232 total, bsize, avail);
237 /****************************************************************************
239 ****************************************************************************/
241 static int cmd_pwd(void)
243 d_printf("Current directory is %s",service);
244 d_printf("%s\n",cur_dir);
248 /****************************************************************************
249 Change directory - inner section.
250 ****************************************************************************/
252 static int do_cd(char *newdir)
258 struct cli_state *targetcli;
259 SMB_STRUCT_STAT sbuf;
265 /* Save the current directory in case the new directory is invalid */
267 pstrcpy(saved_dir, cur_dir);
269 if (*p == CLI_DIRSEP_CHAR)
274 if ((cur_dir[0] != '\0') && (*(cur_dir+strlen(cur_dir)-1) != CLI_DIRSEP_CHAR)) {
275 pstrcat(cur_dir, CLI_DIRSEP_STR);
278 dos_clean_name(cur_dir);
279 pstrcpy( dname, cur_dir );
280 pstrcat(cur_dir,CLI_DIRSEP_STR);
281 dos_clean_name(cur_dir);
283 if ( !cli_resolve_path( "", cli, dname, &targetcli, targetpath ) ) {
284 d_printf("cd %s: %s\n", dname, cli_errstr(cli));
285 pstrcpy(cur_dir,saved_dir);
290 if ( strequal(targetpath,CLI_DIRSEP_STR ) )
293 /* Use a trans2_qpathinfo to test directories for modern servers.
294 Except Win9x doesn't support the qpathinfo_basic() call..... */
296 if ( targetcli->protocol > PROTOCOL_LANMAN2 && !targetcli->win95 ) {
297 if ( !cli_qpathinfo_basic( targetcli, targetpath, &sbuf, &attributes ) ) {
298 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
299 pstrcpy(cur_dir,saved_dir);
303 if ( !(attributes&FILE_ATTRIBUTE_DIRECTORY) ) {
304 d_printf("cd %s: not a directory\n", dname);
305 pstrcpy(cur_dir,saved_dir);
309 pstrcat( targetpath, CLI_DIRSEP_STR );
310 dos_clean_name( targetpath );
312 if ( !cli_chkpath(targetcli, targetpath) ) {
313 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
314 pstrcpy(cur_dir,saved_dir);
323 pstrcpy(cd_path,cur_dir);
327 /****************************************************************************
329 ****************************************************************************/
331 static int cmd_cd(void)
336 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
339 d_printf("Current directory is %s\n",cur_dir);
344 /*******************************************************************
345 Decide if a file should be operated on.
346 ********************************************************************/
348 static BOOL do_this_one(file_info *finfo)
350 if (finfo->mode & aDIR)
353 if (*fileselection &&
354 !mask_match(finfo->name,fileselection,False)) {
355 DEBUG(3,("mask_match %s failed\n", finfo->name));
359 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
360 DEBUG(3,("newer_than %s failed\n", finfo->name));
364 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
365 DEBUG(3,("archive %s failed\n", finfo->name));
372 /****************************************************************************
373 Display info about a file.
374 ****************************************************************************/
376 static void display_finfo(file_info *finfo)
378 if (do_this_one(finfo)) {
379 time_t t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
380 d_printf(" %-30s%7.7s %8.0f %s",
382 attrib_string(finfo->mode),
385 dir_total += finfo->size;
389 /****************************************************************************
390 Accumulate size of a file.
391 ****************************************************************************/
393 static void do_du(file_info *finfo)
395 if (do_this_one(finfo)) {
396 dir_total += finfo->size;
400 static BOOL do_list_recurse;
401 static BOOL do_list_dirs;
402 static char *do_list_queue = 0;
403 static long do_list_queue_size = 0;
404 static long do_list_queue_start = 0;
405 static long do_list_queue_end = 0;
406 static void (*do_list_fn)(file_info *);
408 /****************************************************************************
409 Functions for do_list_queue.
410 ****************************************************************************/
413 * The do_list_queue is a NUL-separated list of strings stored in a
414 * char*. Since this is a FIFO, we keep track of the beginning and
415 * ending locations of the data in the queue. When we overflow, we
416 * double the size of the char*. When the start of the data passes
417 * the midpoint, we move everything back. This is logically more
418 * complex than a linked list, but easier from a memory management
419 * angle. In any memory error condition, do_list_queue is reset.
420 * Functions check to ensure that do_list_queue is non-NULL before
424 static void reset_do_list_queue(void)
426 SAFE_FREE(do_list_queue);
427 do_list_queue_size = 0;
428 do_list_queue_start = 0;
429 do_list_queue_end = 0;
432 static void init_do_list_queue(void)
434 reset_do_list_queue();
435 do_list_queue_size = 1024;
436 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
437 if (do_list_queue == 0) {
438 d_printf("malloc fail for size %d\n",
439 (int)do_list_queue_size);
440 reset_do_list_queue();
442 memset(do_list_queue, 0, do_list_queue_size);
446 static void adjust_do_list_queue(void)
449 * If the starting point of the queue is more than half way through,
450 * move everything toward the beginning.
453 if (do_list_queue == NULL) {
454 DEBUG(4,("do_list_queue is empty\n"));
455 do_list_queue_start = do_list_queue_end = 0;
459 if (do_list_queue_start == do_list_queue_end) {
460 DEBUG(4,("do_list_queue is empty\n"));
461 do_list_queue_start = do_list_queue_end = 0;
462 *do_list_queue = '\0';
463 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
464 DEBUG(4,("sliding do_list_queue backward\n"));
465 memmove(do_list_queue,
466 do_list_queue + do_list_queue_start,
467 do_list_queue_end - do_list_queue_start);
468 do_list_queue_end -= do_list_queue_start;
469 do_list_queue_start = 0;
473 static void add_to_do_list_queue(const char* entry)
475 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
476 while (new_end > do_list_queue_size) {
477 do_list_queue_size *= 2;
478 DEBUG(4,("enlarging do_list_queue to %d\n",
479 (int)do_list_queue_size));
480 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
481 if (! do_list_queue) {
482 d_printf("failure enlarging do_list_queue to %d bytes\n",
483 (int)do_list_queue_size);
484 reset_do_list_queue();
486 memset(do_list_queue + do_list_queue_size / 2,
487 0, do_list_queue_size / 2);
491 safe_strcpy_base(do_list_queue + do_list_queue_end,
492 entry, do_list_queue, do_list_queue_size);
493 do_list_queue_end = new_end;
494 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
495 entry, (int)do_list_queue_start, (int)do_list_queue_end));
499 static char *do_list_queue_head(void)
501 return do_list_queue + do_list_queue_start;
504 static void remove_do_list_queue_head(void)
506 if (do_list_queue_end > do_list_queue_start) {
507 do_list_queue_start += strlen(do_list_queue_head()) + 1;
508 adjust_do_list_queue();
509 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
510 (int)do_list_queue_start, (int)do_list_queue_end));
514 static int do_list_queue_empty(void)
516 return (! (do_list_queue && *do_list_queue));
519 /****************************************************************************
520 A helper for do_list.
521 ****************************************************************************/
523 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
527 /* save the directory */
528 pstrcpy( f->dir, mask );
529 if ( (dir_end = strrchr( f->dir, CLI_DIRSEP_CHAR )) != NULL ) {
533 if (f->mode & aDIR) {
534 if (do_list_dirs && do_this_one(f)) {
537 if (do_list_recurse &&
538 !strequal(f->name,".") &&
539 !strequal(f->name,"..")) {
544 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
548 pstrcpy(mask2, mntpoint);
549 pstrcat(mask2, mask);
550 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
554 pstrcat(mask2, f->name);
555 pstrcat(mask2,"\\*");
556 add_to_do_list_queue(mask2);
561 if (do_this_one(f)) {
566 /****************************************************************************
567 A wrapper around cli_list that adds recursion.
568 ****************************************************************************/
570 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
572 static int in_do_list = 0;
573 struct cli_state *targetcli;
576 if (in_do_list && rec) {
577 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
583 do_list_recurse = rec;
588 init_do_list_queue();
589 add_to_do_list_queue(mask);
591 while (! do_list_queue_empty()) {
593 * Need to copy head so that it doesn't become
594 * invalid inside the call to cli_list. This
595 * would happen if the list were expanded
597 * Fix from E. Jay Berkenbilt (ejb@ql.org)
600 pstrcpy(head, do_list_queue_head());
604 if ( !cli_resolve_path( "", cli, head, &targetcli, targetpath ) ) {
605 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
606 remove_do_list_queue_head();
610 cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
611 remove_do_list_queue_head();
612 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
613 char* next_file = do_list_queue_head();
615 if ((strlen(next_file) >= 2) &&
616 (next_file[strlen(next_file) - 1] == '*') &&
617 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
618 save_ch = next_file +
619 strlen(next_file) - 2;
622 d_printf("\n%s\n",next_file);
624 *save_ch = CLI_DIRSEP_CHAR;
631 if ( cli_resolve_path( "", cli, mask, &targetcli, targetpath ) ) {
632 if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1)
633 d_printf("%s listing %s\n", cli_errstr(targetcli), targetpath);
636 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
641 reset_do_list_queue();
644 /****************************************************************************
645 Get a directory listing.
646 ****************************************************************************/
648 static int cmd_dir(void)
650 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
657 if (strcmp(cur_dir, CLI_DIRSEP_STR) != 0) {
658 pstrcpy(mask,cur_dir);
659 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR))
660 pstrcat(mask,CLI_DIRSEP_STR);
662 pstrcpy(mask, CLI_DIRSEP_STR);
665 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
667 if (*p == CLI_DIRSEP_CHAR)
675 do_list(mask, attribute, display_finfo, recurse, True);
679 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
684 /****************************************************************************
685 Get a directory listing.
686 ****************************************************************************/
688 static int cmd_du(void)
690 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
697 pstrcpy(mask,cur_dir);
698 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR))
699 pstrcat(mask,CLI_DIRSEP_STR);
701 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
703 if (*p == CLI_DIRSEP_CHAR)
711 do_list(mask, attribute, do_du, recurse, True);
715 d_printf("Total number of bytes: %.0f\n", dir_total);
720 /****************************************************************************
721 Get a file from rname to lname
722 ****************************************************************************/
724 static int do_get(char *rname, char *lname, BOOL reget)
726 int handle = 0, fnum;
727 BOOL newhandle = False;
729 struct timeval tp_start;
730 int read_size = io_bufsize;
736 struct cli_state *targetcli;
744 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
745 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
749 GetTimeOfDay(&tp_start);
751 if ( targetcli->dfsroot ) {
754 /* we need to refer to the full \server\share\path format
757 pstrcpy( path, targetname );
758 cli_dfs_make_full_path( targetname, targetcli->desthost,
759 targetcli->share, path);
762 fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
765 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
769 if(!strcmp(lname,"-")) {
770 handle = fileno(stdout);
773 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
775 start = sys_lseek(handle, 0, SEEK_END);
777 d_printf("Error seeking local file\n");
782 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
787 d_printf("Error opening local file %s\n",lname);
792 if (!cli_qfileinfo(targetcli, fnum,
793 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
794 !cli_getattrE(targetcli, fnum,
795 &attr, &size, NULL, NULL, NULL)) {
796 d_printf("getattrib: %s\n",cli_errstr(targetcli));
800 DEBUG(1,("getting file %s of size %.0f as %s ",
801 rname, (double)size, lname));
803 if(!(data = (char *)SMB_MALLOC(read_size))) {
804 d_printf("malloc fail for size %d\n", read_size);
805 cli_close(targetcli, fnum);
810 int n = cli_read(targetcli, fnum, data, nread + start, read_size);
815 if (writefile(handle,data, n) != n) {
816 d_printf("Error writing local file\n");
824 if (nread + start < size) {
825 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
826 rname, (long)nread));
833 if (!cli_close(targetcli, fnum)) {
834 d_printf("Error %s closing remote file\n",cli_errstr(cli));
842 if (archive_level >= 2 && (attr & aARCH)) {
843 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
847 struct timeval tp_end;
850 GetTimeOfDay(&tp_end);
852 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
853 (tp_end.tv_usec - tp_start.tv_usec)/1000;
854 get_total_time_ms += this_time;
855 get_total_size += nread;
857 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
858 nread / (1.024*this_time + 1.0e-4),
859 get_total_size / (1.024*get_total_time_ms)));
865 /****************************************************************************
867 ****************************************************************************/
869 static int cmd_get(void)
875 pstrcpy(rname,cur_dir);
876 pstrcat(rname,CLI_DIRSEP_STR);
878 p = rname + strlen(rname);
880 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
881 d_printf("get <filename>\n");
885 dos_clean_name(rname);
887 next_token_nr(NULL,lname,NULL,sizeof(lname));
889 return do_get(rname, lname, False);
892 /****************************************************************************
893 Do an mget operation on one file.
894 ****************************************************************************/
896 static void do_mget(file_info *finfo)
900 pstring saved_curdir;
903 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
907 d_printf("mget aborted\n");
911 if (finfo->mode & aDIR)
912 slprintf(quest,sizeof(pstring)-1,
913 "Get directory %s? ",finfo->name);
915 slprintf(quest,sizeof(pstring)-1,
916 "Get file %s? ",finfo->name);
918 if (prompt && !yesno(quest))
921 if (!(finfo->mode & aDIR)) {
922 pstrcpy(rname,cur_dir);
923 pstrcat(rname,finfo->name);
924 do_get(rname, finfo->name, False);
928 /* handle directories */
929 pstrcpy(saved_curdir,cur_dir);
931 pstrcat(cur_dir,finfo->name);
932 pstrcat(cur_dir,CLI_DIRSEP_STR);
934 unix_format(finfo->name);
936 strlower_m(finfo->name);
938 if (!directory_exist(finfo->name,NULL) &&
939 mkdir(finfo->name,0777) != 0) {
940 d_printf("failed to create directory %s\n",finfo->name);
941 pstrcpy(cur_dir,saved_curdir);
945 if (chdir(finfo->name) != 0) {
946 d_printf("failed to chdir to directory %s\n",finfo->name);
947 pstrcpy(cur_dir,saved_curdir);
951 pstrcpy(mget_mask,cur_dir);
952 pstrcat(mget_mask,"*");
954 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
956 pstrcpy(cur_dir,saved_curdir);
959 /****************************************************************************
960 View the file using the pager.
961 ****************************************************************************/
963 static int cmd_more(void)
965 pstring rname,lname,pager_cmd;
970 pstrcpy(rname,cur_dir);
971 pstrcat(rname,CLI_DIRSEP_STR);
973 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
974 fd = smb_mkstemp(lname);
976 d_printf("failed to create temporary file for more\n");
981 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
982 d_printf("more <filename>\n");
986 dos_clean_name(rname);
988 rc = do_get(rname, lname, False);
990 pager=getenv("PAGER");
992 slprintf(pager_cmd,sizeof(pager_cmd)-1,
993 "%s %s",(pager? pager:PAGER), lname);
1000 /****************************************************************************
1002 ****************************************************************************/
1004 static int cmd_mget(void)
1006 uint16 attribute = aSYSTEM | aHIDDEN;
1018 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1019 pstrcpy(mget_mask,cur_dir);
1020 if ((mget_mask[0] != '\0') && (mget_mask[strlen(mget_mask)-1]!=CLI_DIRSEP_CHAR))
1021 pstrcat(mget_mask,CLI_DIRSEP_STR);
1023 if (*p == CLI_DIRSEP_CHAR)
1024 pstrcpy(mget_mask,p);
1026 pstrcat(mget_mask,p);
1027 do_list(mget_mask, attribute,do_mget,False,True);
1031 pstrcpy(mget_mask,cur_dir);
1032 if(mget_mask[strlen(mget_mask)-1]!=CLI_DIRSEP_CHAR)
1033 pstrcat(mget_mask,CLI_DIRSEP_STR);
1034 pstrcat(mget_mask,"*");
1035 do_list(mget_mask, attribute,do_mget,False,True);
1041 /****************************************************************************
1042 Make a directory of name "name".
1043 ****************************************************************************/
1045 static BOOL do_mkdir(char *name)
1047 struct cli_state *targetcli;
1050 if ( !cli_resolve_path( "", cli, name, &targetcli, targetname ) ) {
1051 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1055 if (!cli_mkdir(targetcli, targetname)) {
1056 d_printf("%s making remote directory %s\n",
1057 cli_errstr(targetcli),name);
1064 /****************************************************************************
1065 Show 8.3 name of a file.
1066 ****************************************************************************/
1068 static BOOL do_altname(char *name)
1071 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1072 d_printf("%s getting alt name for %s\n",
1073 cli_errstr(cli),name);
1076 d_printf("%s\n", altname);
1081 /****************************************************************************
1083 ****************************************************************************/
1085 static int cmd_quit(void)
1093 /****************************************************************************
1095 ****************************************************************************/
1097 static int cmd_mkdir(void)
1103 pstrcpy(mask,cur_dir);
1105 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1107 d_printf("mkdir <dirname>\n");
1118 trim_char(ddir,'.','\0');
1119 p = strtok(ddir,"/\\");
1122 if (!cli_chkpath(cli, ddir2)) {
1125 pstrcat(ddir2,CLI_DIRSEP_STR);
1126 p = strtok(NULL,"/\\");
1135 /****************************************************************************
1137 ****************************************************************************/
1139 static int cmd_altname(void)
1145 pstrcpy(name,cur_dir);
1147 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1148 d_printf("altname <file>\n");
1158 /****************************************************************************
1160 ****************************************************************************/
1162 static int do_put(char *rname, char *lname, BOOL reput)
1166 SMB_OFF_T start = 0;
1169 int maxwrite = io_bufsize;
1171 struct timeval tp_start;
1172 struct cli_state *targetcli;
1175 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
1176 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1180 GetTimeOfDay(&tp_start);
1183 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
1185 if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1186 !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
1187 d_printf("getattrib: %s\n",cli_errstr(cli));
1192 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1196 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1200 /* allow files to be piped into smbclient
1203 Note that in this case this function will exit(0) rather
1205 if (!strcmp(lname, "-")) {
1207 /* size of file is not known */
1209 f = x_fopen(lname,O_RDONLY, 0);
1211 if (x_tseek(f, start, SEEK_SET) == -1) {
1212 d_printf("Error seeking local file\n");
1219 d_printf("Error opening local file %s\n",lname);
1223 DEBUG(1,("putting file %s as %s ",lname,
1226 buf = (char *)SMB_MALLOC(maxwrite);
1228 d_printf("ERROR: Not enough memory!\n");
1231 while (!x_feof(f)) {
1235 if ((n = readfile(buf,n,f)) < 1) {
1236 if((n == 0) && x_feof(f))
1237 break; /* Empty local file. */
1239 d_printf("Error reading local file: %s\n", strerror(errno));
1244 ret = cli_write(targetcli, fnum, 0, buf, nread + start, n);
1247 d_printf("Error writing file: %s\n", cli_errstr(cli));
1255 if (!cli_close(targetcli, fnum)) {
1256 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1270 struct timeval tp_end;
1273 GetTimeOfDay(&tp_end);
1275 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1276 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1277 put_total_time_ms += this_time;
1278 put_total_size += nread;
1280 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1281 nread / (1.024*this_time + 1.0e-4),
1282 put_total_size / (1.024*put_total_time_ms)));
1293 /****************************************************************************
1295 ****************************************************************************/
1297 static int cmd_put(void)
1304 pstrcpy(rname,cur_dir);
1305 pstrcat(rname,CLI_DIRSEP_STR);
1307 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1308 d_printf("put <filename>\n");
1313 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1316 pstrcat(rname,lname);
1318 dos_clean_name(rname);
1322 /* allow '-' to represent stdin
1323 jdblair, 24.jun.98 */
1324 if (!file_exist(lname,&st) &&
1325 (strcmp(lname,"-"))) {
1326 d_printf("%s does not exist\n",lname);
1331 return do_put(rname, lname, False);
1334 /*************************************
1335 File list structure.
1336 *************************************/
1338 static struct file_list {
1339 struct file_list *prev, *next;
1344 /****************************************************************************
1345 Free a file_list structure.
1346 ****************************************************************************/
1348 static void free_file_list (struct file_list *list_head)
1350 struct file_list *list, *next;
1352 for (list = list_head; list; list = next) {
1354 DLIST_REMOVE(list_head, list);
1355 SAFE_FREE(list->file_path);
1360 /****************************************************************************
1361 Seek in a directory/file list until you get something that doesn't start with
1363 ****************************************************************************/
1365 static BOOL seek_list(struct file_list *list, char *name)
1368 trim_string(list->file_path,"./","\n");
1369 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1378 /****************************************************************************
1379 Set the file selection mask.
1380 ****************************************************************************/
1382 static int cmd_select(void)
1384 pstrcpy(fileselection,"");
1385 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1390 /****************************************************************************
1391 Recursive file matching function act as find
1392 match must be always set to True when calling this function
1393 ****************************************************************************/
1395 static int file_find(struct file_list **list, const char *directory,
1396 const char *expression, BOOL match)
1398 SMB_STRUCT_DIR *dir;
1399 struct file_list *entry;
1400 struct stat statbuf;
1406 dir = sys_opendir(directory);
1410 while ((dname = readdirname(dir))) {
1411 if (!strcmp("..", dname))
1413 if (!strcmp(".", dname))
1416 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1421 if (!match || !gen_fnmatch(expression, dname)) {
1423 ret = stat(path, &statbuf);
1425 if (S_ISDIR(statbuf.st_mode)) {
1427 ret = file_find(list, path, expression, False);
1430 d_printf("file_find: cannot stat file %s\n", path);
1439 entry = SMB_MALLOC_P(struct file_list);
1441 d_printf("Out of memory in file_find\n");
1445 entry->file_path = path;
1446 entry->isdir = isdir;
1447 DLIST_ADD(*list, entry);
1457 /****************************************************************************
1459 ****************************************************************************/
1461 static int cmd_mput(void)
1466 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1468 struct file_list *temp_list;
1469 char *quest, *lname, *rname;
1473 ret = file_find(&file_list, ".", p, True);
1475 free_file_list(file_list);
1483 for (temp_list = file_list; temp_list;
1484 temp_list = temp_list->next) {
1487 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1489 trim_string(lname, "./", "/");
1491 /* check if it's a directory */
1492 if (temp_list->isdir) {
1493 /* if (!recurse) continue; */
1496 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1497 if (prompt && !yesno(quest)) { /* No */
1498 /* Skip the directory */
1499 lname[strlen(lname)-1] = '/';
1500 if (!seek_list(temp_list, lname))
1504 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1506 if (!cli_chkpath(cli, rname) &&
1508 DEBUG (0, ("Unable to make dir, skipping..."));
1509 /* Skip the directory */
1510 lname[strlen(lname)-1] = '/';
1511 if (!seek_list(temp_list, lname))
1518 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1519 if (prompt && !yesno(quest)) /* No */
1524 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1529 do_put(rname, lname, False);
1531 free_file_list(file_list);
1540 /****************************************************************************
1542 ****************************************************************************/
1544 static int do_cancel(int job)
1546 if (cli_printjob_del(cli, job)) {
1547 d_printf("Job %d cancelled\n",job);
1550 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1555 /****************************************************************************
1557 ****************************************************************************/
1559 static int cmd_cancel(void)
1564 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1565 d_printf("cancel <jobid> ...\n");
1571 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1576 /****************************************************************************
1578 ****************************************************************************/
1580 static int cmd_print(void)
1586 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1587 d_printf("print <filename>\n");
1591 pstrcpy(rname,lname);
1592 p = strrchr_m(rname,'/');
1594 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1597 if (strequal(lname,"-")) {
1598 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1601 return do_put(rname, lname, False);
1604 /****************************************************************************
1605 Show a print queue entry.
1606 ****************************************************************************/
1608 static void queue_fn(struct print_job_info *p)
1610 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
1613 /****************************************************************************
1615 ****************************************************************************/
1617 static int cmd_queue(void)
1619 cli_print_queue(cli, queue_fn);
1624 /****************************************************************************
1626 ****************************************************************************/
1628 static void do_del(file_info *finfo)
1632 pstr_sprintf( mask, "%s\\%s", finfo->dir, finfo->name );
1634 if (finfo->mode & aDIR)
1637 if (!cli_unlink(cli, mask)) {
1638 d_printf("%s deleting remote file %s\n",cli_errstr(cli),mask);
1642 /****************************************************************************
1644 ****************************************************************************/
1646 static int cmd_del(void)
1650 uint16 attribute = aSYSTEM | aHIDDEN;
1655 pstrcpy(mask,cur_dir);
1657 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1658 d_printf("del <filename>\n");
1663 do_list(mask, attribute,do_del,False,False);
1668 /****************************************************************************
1669 ****************************************************************************/
1671 static int cmd_open(void)
1675 struct cli_state *targetcli;
1679 pstrcpy(mask,cur_dir);
1681 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1682 d_printf("open <filename>\n");
1687 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1688 d_printf("open %s: %s\n", mask, cli_errstr(cli));
1692 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA|FILE_WRITE_DATA);
1694 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA);
1696 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
1698 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
1701 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
1707 static int cmd_close(void)
1712 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1713 d_printf("close <fnum>\n");
1718 /* We really should use the targetcli here.... */
1719 if (!cli_close(cli, fnum)) {
1720 d_printf("close %d: %s\n", fnum, cli_errstr(cli));
1726 static int cmd_posix(void)
1728 uint16 major, minor;
1729 uint32 caplow, caphigh;
1732 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1733 d_printf("Server doesn't support UNIX CIFS extensions.\n");
1737 if (!cli_unix_extensions_version(cli, &major, &minor, &caplow, &caphigh)) {
1738 d_printf("Can't get UNIX CIFS extensions version from server.\n");
1742 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
1745 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
1746 pstrcat(caps, "locks ");
1748 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
1749 pstrcat(caps, "acls ");
1751 if (caplow & CIFS_UNIX_XATTTR_CAP) {
1752 pstrcat(caps, "eas ");
1754 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
1755 pstrcat(caps, "pathnames ");
1758 if (strlen(caps) > 0 && caps[strlen(caps)-1] == ' ') {
1759 caps[strlen(caps)-1] = '\0';
1762 if (!cli_set_unix_extensions_capabilities(cli, major, minor, caplow, caphigh)) {
1763 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n", cli_errstr(cli));
1767 d_printf("Selecting server supported CIFS capabilities %s\n", caps);
1769 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
1770 CLI_DIRSEP_CHAR = '/';
1771 *CLI_DIRSEP_STR = '/';
1772 pstrcpy(cur_dir, CLI_DIRSEP_STR);
1778 static int cmd_lock(void)
1781 SMB_BIG_UINT start, len;
1782 enum brl_type lock_type;
1785 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1786 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
1791 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1792 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
1796 if (*buf == 'r' || *buf == 'R') {
1797 lock_type = READ_LOCK;
1798 } else if (*buf == 'w' || *buf == 'W') {
1799 lock_type = WRITE_LOCK;
1801 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
1805 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1806 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
1810 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
1812 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1813 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
1817 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
1819 if (!cli_posix_lock(cli, fnum, start, len, True, lock_type)) {
1820 d_printf("lock failed %d: %s\n", fnum, cli_errstr(cli));
1826 static int cmd_unlock(void)
1829 SMB_BIG_UINT start, len;
1832 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1833 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
1838 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1839 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
1843 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
1845 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1846 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
1850 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
1852 if (!cli_posix_unlock(cli, fnum, start, len)) {
1853 d_printf("unlock failed %d: %s\n", fnum, cli_errstr(cli));
1860 /****************************************************************************
1862 ****************************************************************************/
1864 static int cmd_rmdir(void)
1868 struct cli_state *targetcli;
1871 pstrcpy(mask,cur_dir);
1873 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1874 d_printf("rmdir <dirname>\n");
1879 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1880 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
1884 if (!cli_rmdir(targetcli, targetname)) {
1885 d_printf("%s removing remote directory file %s\n",
1886 cli_errstr(targetcli),mask);
1892 /****************************************************************************
1894 ****************************************************************************/
1896 static int cmd_link(void)
1898 pstring oldname,newname;
1900 struct cli_state *targetcli;
1903 pstrcpy(oldname,cur_dir);
1904 pstrcpy(newname,cur_dir);
1906 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1907 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1908 d_printf("link <oldname> <newname>\n");
1912 pstrcat(oldname,buf);
1913 pstrcat(newname,buf2);
1915 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
1916 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
1920 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1921 d_printf("Server doesn't support UNIX CIFS calls.\n");
1925 if (!cli_unix_hardlink(targetcli, targetname, newname)) {
1926 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
1933 /****************************************************************************
1935 ****************************************************************************/
1937 static int cmd_symlink(void)
1939 pstring oldname,newname;
1942 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1943 d_printf("Server doesn't support UNIX CIFS calls.\n");
1947 pstrcpy(newname,cur_dir);
1949 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1950 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1951 d_printf("symlink <oldname> <newname>\n");
1955 pstrcpy(oldname,buf);
1956 pstrcat(newname,buf2);
1958 if (!cli_unix_symlink(cli, oldname, newname)) {
1959 d_printf("%s symlinking files (%s -> %s)\n",
1960 cli_errstr(cli), newname, oldname);
1967 /****************************************************************************
1969 ****************************************************************************/
1971 static int cmd_chmod(void)
1976 struct cli_state *targetcli;
1979 pstrcpy(src,cur_dir);
1981 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1982 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1983 d_printf("chmod mode file\n");
1987 mode = (mode_t)strtol(buf, NULL, 8);
1990 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
1991 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
1995 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1996 d_printf("Server doesn't support UNIX CIFS calls.\n");
2000 if (!cli_unix_chmod(targetcli, targetname, mode)) {
2001 d_printf("%s chmod file %s 0%o\n",
2002 cli_errstr(targetcli), src, (unsigned int)mode);
2009 static const char *filetype_to_str(mode_t mode)
2011 if (S_ISREG(mode)) {
2012 return "regular file";
2013 } else if (S_ISDIR(mode)) {
2017 if (S_ISCHR(mode)) {
2018 return "character device";
2022 if (S_ISBLK(mode)) {
2023 return "block device";
2027 if (S_ISFIFO(mode)) {
2032 if (S_ISLNK(mode)) {
2033 return "symbolic link";
2037 if (S_ISSOCK(mode)) {
2044 static char rwx_to_str(mode_t m, mode_t bt, char ret)
2053 static char *unix_mode_to_str(char *s, mode_t m)
2056 const char *str = filetype_to_str(m);
2072 *p++ = str[1] == 'y' ? 'l' : 's';
2079 *p++ = rwx_to_str(m, S_IRUSR, 'r');
2080 *p++ = rwx_to_str(m, S_IWUSR, 'w');
2081 *p++ = rwx_to_str(m, S_IXUSR, 'x');
2082 *p++ = rwx_to_str(m, S_IRGRP, 'r');
2083 *p++ = rwx_to_str(m, S_IWGRP, 'w');
2084 *p++ = rwx_to_str(m, S_IXGRP, 'x');
2085 *p++ = rwx_to_str(m, S_IROTH, 'r');
2086 *p++ = rwx_to_str(m, S_IWOTH, 'w');
2087 *p++ = rwx_to_str(m, S_IXOTH, 'x');
2092 /****************************************************************************
2093 Utility function for UNIX getfacl.
2094 ****************************************************************************/
2096 static char *perms_to_string(fstring permstr, unsigned char perms)
2098 fstrcpy(permstr, "---");
2099 if (perms & SMB_POSIX_ACL_READ) {
2102 if (perms & SMB_POSIX_ACL_WRITE) {
2105 if (perms & SMB_POSIX_ACL_EXECUTE) {
2111 /****************************************************************************
2113 ****************************************************************************/
2115 static int cmd_getfacl(void)
2118 uint16 major, minor;
2119 uint32 caplow, caphigh;
2120 char *retbuf = NULL;
2122 SMB_STRUCT_STAT sbuf;
2123 uint16 num_file_acls = 0;
2124 uint16 num_dir_acls = 0;
2126 struct cli_state *targetcli;
2129 pstrcpy(src,cur_dir);
2131 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2132 d_printf("stat file\n");
2138 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2139 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2143 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2144 d_printf("Server doesn't support UNIX CIFS calls.\n");
2148 if (!cli_unix_extensions_version(targetcli, &major, &minor, &caplow, &caphigh)) {
2149 d_printf("Can't get UNIX CIFS version from server.\n");
2153 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
2154 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
2159 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2160 d_printf("%s getfacl doing a stat on file %s\n",
2161 cli_errstr(targetcli), src);
2165 if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
2166 d_printf("%s getfacl file %s\n",
2167 cli_errstr(targetcli), src);
2171 /* ToDo : Print out the ACL values. */
2172 if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
2173 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2174 src, (unsigned int)CVAL(retbuf,0) );
2179 num_file_acls = SVAL(retbuf,2);
2180 num_dir_acls = SVAL(retbuf,4);
2181 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
2182 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2184 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
2185 (unsigned int)rb_size);
2191 d_printf("# file: %s\n", src);
2192 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
2194 if (num_file_acls == 0 && num_dir_acls == 0) {
2195 d_printf("No acls found.\n");
2198 for (i = 0; i < num_file_acls; i++) {
2201 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
2202 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2205 case SMB_POSIX_ACL_USER_OBJ:
2208 case SMB_POSIX_ACL_USER:
2209 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2210 d_printf("user:%u:", uorg);
2212 case SMB_POSIX_ACL_GROUP_OBJ:
2213 d_printf("group::");
2215 case SMB_POSIX_ACL_GROUP:
2216 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2217 d_printf("group:%u", uorg);
2219 case SMB_POSIX_ACL_MASK:
2222 case SMB_POSIX_ACL_OTHER:
2223 d_printf("other::");
2226 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2227 src, (unsigned int)tagtype );
2232 d_printf("%s\n", perms_to_string(permstring, perms));
2235 for (i = 0; i < num_dir_acls; i++) {
2238 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
2239 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2242 case SMB_POSIX_ACL_USER_OBJ:
2243 d_printf("default:user::");
2245 case SMB_POSIX_ACL_USER:
2246 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2247 d_printf("default:user:%u:", uorg);
2249 case SMB_POSIX_ACL_GROUP_OBJ:
2250 d_printf("default:group::");
2252 case SMB_POSIX_ACL_GROUP:
2253 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2254 d_printf("default:group:%u", uorg);
2256 case SMB_POSIX_ACL_MASK:
2257 d_printf("default:mask::");
2259 case SMB_POSIX_ACL_OTHER:
2260 d_printf("default:other::");
2263 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2264 src, (unsigned int)tagtype );
2269 d_printf("%s\n", perms_to_string(permstring, perms));
2276 /****************************************************************************
2278 ****************************************************************************/
2280 static int cmd_stat(void)
2284 SMB_STRUCT_STAT sbuf;
2285 struct cli_state *targetcli;
2289 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2290 d_printf("Server doesn't support UNIX CIFS calls.\n");
2294 pstrcpy(src,cur_dir);
2296 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2297 d_printf("stat file\n");
2304 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2305 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2309 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2310 d_printf("%s stat file %s\n",
2311 cli_errstr(targetcli), src);
2315 /* Print out the stat values. */
2316 d_printf("File: %s\n", src);
2317 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2318 (double)sbuf.st_size,
2319 (unsigned int)sbuf.st_blocks,
2320 filetype_to_str(sbuf.st_mode));
2322 #if defined(S_ISCHR) && defined(S_ISBLK)
2323 if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
2324 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2325 (double)sbuf.st_ino,
2326 (unsigned int)sbuf.st_nlink,
2327 unix_dev_major(sbuf.st_rdev),
2328 unix_dev_minor(sbuf.st_rdev));
2331 d_printf("Inode: %.0f\tLinks: %u\n",
2332 (double)sbuf.st_ino,
2333 (unsigned int)sbuf.st_nlink);
2335 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2336 ((int)sbuf.st_mode & 0777),
2337 unix_mode_to_str(mode_str, sbuf.st_mode),
2338 (unsigned int)sbuf.st_uid,
2339 (unsigned int)sbuf.st_gid);
2341 lt = localtime(&sbuf.st_atime);
2343 strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
2345 fstrcpy(mode_str, "unknown");
2347 d_printf("Access: %s\n", mode_str);
2349 lt = localtime(&sbuf.st_mtime);
2351 strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
2353 fstrcpy(mode_str, "unknown");
2355 d_printf("Modify: %s\n", mode_str);
2357 lt = localtime(&sbuf.st_ctime);
2359 strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
2361 fstrcpy(mode_str, "unknown");
2363 d_printf("Change: %s\n", mode_str);
2369 /****************************************************************************
2371 ****************************************************************************/
2373 static int cmd_chown(void)
2378 pstring buf, buf2, buf3;
2379 struct cli_state *targetcli;
2382 pstrcpy(src,cur_dir);
2384 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2385 !next_token_nr(NULL,buf2,NULL, sizeof(buf2)) ||
2386 !next_token_nr(NULL,buf3,NULL, sizeof(buf3))) {
2387 d_printf("chown uid gid file\n");
2391 uid = (uid_t)atoi(buf);
2392 gid = (gid_t)atoi(buf2);
2395 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2396 d_printf("chown %s: %s\n", src, cli_errstr(cli));
2401 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2402 d_printf("Server doesn't support UNIX CIFS calls.\n");
2406 if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
2407 d_printf("%s chown file %s uid=%d, gid=%d\n",
2408 cli_errstr(targetcli), src, (int)uid, (int)gid);
2415 /****************************************************************************
2417 ****************************************************************************/
2419 static int cmd_rename(void)
2424 pstrcpy(src,cur_dir);
2425 pstrcpy(dest,cur_dir);
2427 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2428 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2429 d_printf("rename <src> <dest>\n");
2436 if (!cli_rename(cli, src, dest)) {
2437 d_printf("%s renaming files\n",cli_errstr(cli));
2444 /****************************************************************************
2445 Print the volume name.
2446 ****************************************************************************/
2448 static int cmd_volume(void)
2454 if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
2455 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
2459 d_printf("Volume: |%s| serial number 0x%x\n", volname, (unsigned int)serial_num);
2463 /****************************************************************************
2464 Hard link files using the NT call.
2465 ****************************************************************************/
2467 static int cmd_hardlink(void)
2471 struct cli_state *targetcli;
2474 pstrcpy(src,cur_dir);
2475 pstrcpy(dest,cur_dir);
2477 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2478 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2479 d_printf("hardlink <src> <dest>\n");
2486 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2487 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
2491 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2492 d_printf("Server doesn't support UNIX CIFS calls.\n");
2496 if (!cli_nt_hardlink(targetcli, targetname, dest)) {
2497 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
2504 /****************************************************************************
2505 Toggle the prompt flag.
2506 ****************************************************************************/
2508 static int cmd_prompt(void)
2511 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2516 /****************************************************************************
2517 Set the newer than time.
2518 ****************************************************************************/
2520 static int cmd_newer(void)
2524 SMB_STRUCT_STAT sbuf;
2526 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2527 if (ok && (sys_stat(buf,&sbuf) == 0)) {
2528 newer_than = sbuf.st_mtime;
2529 DEBUG(1,("Getting files newer than %s",
2530 time_to_asc(&newer_than)));
2535 if (ok && newer_than == 0) {
2536 d_printf("Error setting newer-than time\n");
2543 /****************************************************************************
2544 Set the archive level.
2545 ****************************************************************************/
2547 static int cmd_archive(void)
2551 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2552 archive_level = atoi(buf);
2554 d_printf("Archive level is %d\n",archive_level);
2559 /****************************************************************************
2560 Toggle the lowercaseflag.
2561 ****************************************************************************/
2563 static int cmd_lowercase(void)
2565 lowercase = !lowercase;
2566 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2571 /****************************************************************************
2572 Toggle the case sensitive flag.
2573 ****************************************************************************/
2575 static int cmd_setcase(void)
2577 BOOL orig_case_sensitive = cli_set_case_sensitive(cli, False);
2579 cli_set_case_sensitive(cli, !orig_case_sensitive);
2580 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
2586 /****************************************************************************
2587 Toggle the recurse flag.
2588 ****************************************************************************/
2590 static int cmd_recurse(void)
2593 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2598 /****************************************************************************
2599 Toggle the translate flag.
2600 ****************************************************************************/
2602 static int cmd_translate(void)
2604 translation = !translation;
2605 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2606 translation?"on":"off"));
2611 /****************************************************************************
2613 ****************************************************************************/
2615 static int cmd_lcd(void)
2620 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2622 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2627 /****************************************************************************
2628 Get a file restarting at end of local file.
2629 ****************************************************************************/
2631 static int cmd_reget(void)
2634 pstring remote_name;
2637 pstrcpy(remote_name, cur_dir);
2638 pstrcat(remote_name, CLI_DIRSEP_STR);
2640 p = remote_name + strlen(remote_name);
2642 if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2643 d_printf("reget <filename>\n");
2646 pstrcpy(local_name, p);
2647 dos_clean_name(remote_name);
2649 next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2651 return do_get(remote_name, local_name, True);
2654 /****************************************************************************
2655 Put a file restarting at end of local file.
2656 ****************************************************************************/
2658 static int cmd_reput(void)
2661 pstring remote_name;
2666 pstrcpy(remote_name, cur_dir);
2667 pstrcat(remote_name, CLI_DIRSEP_STR);
2669 if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2670 d_printf("reput <filename>\n");
2673 pstrcpy(local_name, p);
2675 if (!file_exist(local_name, &st)) {
2676 d_printf("%s does not exist\n", local_name);
2680 if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2681 pstrcat(remote_name, p);
2683 pstrcat(remote_name, local_name);
2685 dos_clean_name(remote_name);
2687 return do_put(remote_name, local_name, True);
2690 /****************************************************************************
2692 ****************************************************************************/
2694 static void browse_fn(const char *name, uint32 m,
2695 const char *comment, void *state)
2703 case STYPE_DISKTREE:
2704 fstrcpy(typestr,"Disk"); break;
2706 fstrcpy(typestr,"Printer"); break;
2708 fstrcpy(typestr,"Device"); break;
2710 fstrcpy(typestr,"IPC"); break;
2712 /* FIXME: If the remote machine returns non-ascii characters
2713 in any of these fields, they can corrupt the output. We
2714 should remove them. */
2716 d_printf("\t%-15s %-10.10s%s\n",
2717 name,typestr,comment);
2719 d_printf ("%s|%s|%s\n",typestr,name,comment);
2723 static BOOL browse_host_rpc(BOOL sort)
2726 struct rpc_pipe_client *pipe_hnd;
2727 TALLOC_CTX *mem_ctx;
2728 uint32 enum_hnd = 0;
2730 union srvsvc_NetShareCtr ctr;
2735 mem_ctx = talloc_new(NULL);
2736 if (mem_ctx == NULL) {
2737 DEBUG(0, ("talloc_new failed\n"));
2741 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
2743 if (pipe_hnd == NULL) {
2744 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
2745 nt_errstr(status)));
2746 TALLOC_FREE(mem_ctx);
2752 werr = rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL, &level, &ctr,
2753 0xffffffff, &numentries, &enum_hnd);
2755 if (!W_ERROR_IS_OK(werr)) {
2756 TALLOC_FREE(mem_ctx);
2757 cli_rpc_pipe_close(pipe_hnd);
2761 for (i=0; i<numentries; i++) {
2762 struct srvsvc_NetShareInfo1 *info = &ctr.ctr1->array[i];
2763 browse_fn(info->name, info->type, info->comment, NULL);
2766 TALLOC_FREE(mem_ctx);
2767 cli_rpc_pipe_close(pipe_hnd);
2771 /****************************************************************************
2772 Try and browse available connections on a host.
2773 ****************************************************************************/
2775 static BOOL browse_host(BOOL sort)
2779 d_printf("\n\tSharename Type Comment\n");
2780 d_printf("\t--------- ---- -------\n");
2783 if (browse_host_rpc(sort)) {
2787 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
2788 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
2793 /****************************************************************************
2795 ****************************************************************************/
2797 static void server_fn(const char *name, uint32 m,
2798 const char *comment, void *state)
2802 d_printf("\t%-16s %s\n", name, comment);
2804 d_printf("%s|%s|%s\n",(char *)state, name, comment);
2808 /****************************************************************************
2809 Try and browse available connections on a host.
2810 ****************************************************************************/
2812 static BOOL list_servers(const char *wk_grp)
2816 if (!cli->server_domain)
2820 d_printf("\n\tServer Comment\n");
2821 d_printf("\t--------- -------\n");
2823 fstrcpy( state, "Server" );
2824 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
2828 d_printf("\n\tWorkgroup Master\n");
2829 d_printf("\t--------- -------\n");
2832 fstrcpy( state, "Workgroup" );
2833 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
2838 /****************************************************************************
2839 Print or set current VUID
2840 ****************************************************************************/
2842 static int cmd_vuid(void)
2846 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2847 d_printf("Current VUID is %d\n", cli->vuid);
2851 cli->vuid = atoi(buf);
2855 /****************************************************************************
2856 Setup a new VUID, by issuing a session setup
2857 ****************************************************************************/
2859 static int cmd_logon(void)
2861 pstring l_username, l_password;
2864 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2865 d_printf("logon <username> [<password>]\n");
2869 pstrcpy(l_username, buf);
2871 if (!next_token_nr(NULL,buf2,NULL,sizeof(buf)))
2873 char *pass = getpass("Password: ");
2875 pstrcpy(l_password, pass);
2878 pstrcpy(l_password, buf2);
2880 if (!NT_STATUS_IS_OK(cli_session_setup(cli, l_username,
2881 l_password, strlen(l_password),
2882 l_password, strlen(l_password),
2884 d_printf("session setup failed: %s\n", cli_errstr(cli));
2888 d_printf("Current VUID is %d\n", cli->vuid);
2893 /****************************************************************************
2894 list active connections
2895 ****************************************************************************/
2897 static int cmd_list_connect(void)
2904 /****************************************************************************
2905 display the current active client connection
2906 ****************************************************************************/
2908 static int cmd_show_connect( void )
2910 struct cli_state *targetcli;
2913 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
2914 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
2918 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
2922 /* Some constants for completing filename arguments */
2924 #define COMPL_NONE 0 /* No completions */
2925 #define COMPL_REMOTE 1 /* Complete remote filename */
2926 #define COMPL_LOCAL 2 /* Complete local filename */
2928 /* This defines the commands supported by this client.
2929 * NOTE: The "!" must be the last one in the list because it's fn pointer
2930 * field is NULL, and NULL in that field is used in process_tok()
2931 * (below) to indicate the end of the list. crh
2937 const char *description;
2938 char compl_args[2]; /* Completion argument info */
2940 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2941 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2942 {"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}},
2943 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
2944 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2945 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
2946 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2947 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2948 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2949 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
2950 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2951 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2952 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2953 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2954 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2955 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
2956 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2957 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2958 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2959 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2960 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2961 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
2962 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
2963 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2964 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2965 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2966 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2967 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2968 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
2969 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2970 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2971 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2972 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
2973 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2974 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
2975 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2976 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2977 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2978 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2979 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2980 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2981 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
2982 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2983 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2984 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2985 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2986 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2987 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
2988 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
2989 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2990 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
2991 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
2992 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2993 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
2994 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
2995 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
2996 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
2997 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
2998 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
3000 /* Yes, this must be here, see crh's comment above. */
3001 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
3002 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
3005 /*******************************************************************
3006 Lookup a command string in the list of commands, including
3008 ******************************************************************/
3010 static int process_tok(pstring tok)
3012 int i = 0, matches = 0;
3014 int tok_len = strlen(tok);
3016 while (commands[i].fn != NULL) {
3017 if (strequal(commands[i].name,tok)) {
3021 } else if (strnequal(commands[i].name, tok, tok_len)) {
3030 else if (matches == 1)
3036 /****************************************************************************
3038 ****************************************************************************/
3040 static int cmd_help(void)
3045 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3046 if ((i = process_tok(buf)) >= 0)
3047 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
3049 while (commands[i].description) {
3050 for (j=0; commands[i].description && (j<5); j++) {
3051 d_printf("%-15s",commands[i].name);
3060 /****************************************************************************
3061 Process a -c command string.
3062 ****************************************************************************/
3064 static int process_command_string(char *cmd)
3070 /* establish the connection if not already */
3073 cli = cli_cm_open(desthost, service, True);
3078 while (cmd[0] != '\0') {
3083 if ((p = strchr_m(cmd, ';')) == 0) {
3084 strncpy(line, cmd, 999);
3090 strncpy(line, cmd, p - cmd);
3091 line[p - cmd] = '\0';
3095 /* and get the first part of the command */
3097 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3099 if ((i = process_tok(tok)) >= 0) {
3100 rc = commands[i].fn();
3101 } else if (i == -2) {
3102 d_printf("%s: command abbreviation ambiguous\n",tok);
3104 d_printf("%s: command not found\n",tok);
3111 #define MAX_COMPLETIONS 100
3119 } completion_remote_t;
3121 static void completion_remote_filter(const char *mnt, file_info *f, const char *mask, void *state)
3123 completion_remote_t *info = (completion_remote_t *)state;
3125 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
3126 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
3127 info->matches[info->count] = SMB_STRDUP(f->name);
3131 if (info->dirmask[0] != 0)
3132 pstrcpy(tmp, info->dirmask);
3135 pstrcat(tmp, f->name);
3138 info->matches[info->count] = SMB_STRDUP(tmp);
3140 if (info->matches[info->count] == NULL)
3143 smb_readline_ca_char(0);
3145 if (info->count == 1)
3146 info->samelen = strlen(info->matches[info->count]);
3148 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
3154 static char **remote_completion(const char *text, int len)
3158 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
3160 /* can't have non-static intialisation on Sun CC, so do it
3166 if (len >= MIN(PATH_MAX,sizeof(pstring))) {
3170 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
3171 if (!info.matches) {
3176 * We're leaving matches[0] free to fill it later with the text to
3177 * display: Either the one single match or the longest common subset
3180 info.matches[0] = NULL;
3183 for (i = len-1; i >= 0; i--) {
3184 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
3189 info.text = text+i+1;
3190 info.samelen = info.len = len-i-1;
3193 strncpy(info.dirmask, text, i+1);
3194 info.dirmask[i+1] = 0;
3195 pstr_sprintf(dirmask, "%s%*s*", cur_dir, i-1, text);
3197 pstr_sprintf(dirmask, "%s*", cur_dir);
3200 if (cli_list(cli, dirmask, aDIR | aSYSTEM | aHIDDEN, completion_remote_filter, &info) < 0)
3203 if (info.count == 1) {
3206 * No matches at all, NULL indicates there is nothing
3209 SAFE_FREE(info.matches[0]);
3210 SAFE_FREE(info.matches);
3214 if (info.count == 2) {
3217 * Exactly one match in matches[1], indicate this is the one
3221 info.matches[0] = info.matches[1];
3222 info.matches[1] = NULL;
3224 return info.matches;
3228 * We got more than one possible match, set the result to the maximum
3232 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
3233 info.matches[info.count] = NULL;
3234 return info.matches;
3237 for (i = 0; i < info.count; i++)
3238 free(info.matches[i]);
3243 static char **completion_fn(const char *text, int start, int end)
3245 smb_readline_ca_char(' ');
3248 const char *buf, *sp;
3252 buf = smb_readline_get_line_buffer();
3256 sp = strchr(buf, ' ');
3260 for (i = 0; commands[i].name; i++) {
3261 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
3262 (commands[i].name[sp - buf] == 0)) {
3266 if (commands[i].name == NULL)
3272 if (sp == (buf + start))
3273 compl_type = commands[i].compl_args[0];
3275 compl_type = commands[i].compl_args[1];
3277 if (compl_type == COMPL_REMOTE)
3278 return remote_completion(text, end - start);
3279 else /* fall back to local filename completion */
3283 int i, len, samelen = 0, count=1;
3285 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
3292 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3293 if (strncmp(text, commands[i].name, len) == 0) {
3294 matches[count] = SMB_STRDUP(commands[i].name);
3295 if (!matches[count])
3298 samelen = strlen(matches[count]);
3300 while (strncmp(matches[count], matches[count-1], samelen) != 0)
3307 case 0: /* should never happen */
3311 matches[0] = SMB_STRDUP(matches[1]);
3314 matches[0] = (char *)SMB_MALLOC(samelen+1);
3317 strncpy(matches[0], matches[1], samelen);
3318 matches[0][samelen] = 0;
3320 matches[count] = NULL;
3324 for (i = 0; i < count; i++)
3332 /****************************************************************************
3333 Make sure we swallow keepalives during idle time.
3334 ****************************************************************************/
3336 static void readline_callback(void)
3339 struct timeval timeout;
3340 static time_t last_t;
3356 FD_SET(cli->fd,&fds);
3359 timeout.tv_usec = 0;
3360 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
3362 /* We deliberately use receive_smb instead of
3363 client_receive_smb as we want to receive
3364 session keepalives and then drop them here.
3366 if (FD_ISSET(cli->fd,&fds)) {
3367 if (!receive_smb(cli->fd,cli->inbuf,0)) {
3368 DEBUG(0, ("Read from server failed, maybe it closed the "
3375 /* Ping the server to keep the connection alive using SMBecho. */
3377 unsigned char garbage[16];
3378 memset(garbage, 0xf0, sizeof(garbage));
3379 cli_echo(cli, garbage, sizeof(garbage));
3383 /****************************************************************************
3384 Process commands on stdin.
3385 ****************************************************************************/
3387 static int process_stdin(void)
3399 /* display a prompt */
3400 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3401 cline = smb_readline(the_prompt, readline_callback, completion_fn);
3405 pstrcpy(line, cline);
3407 /* special case - first char is ! */
3413 /* and get the first part of the command */
3415 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3417 if ((i = process_tok(tok)) >= 0) {
3418 rc = commands[i].fn();
3419 } else if (i == -2) {
3420 d_printf("%s: command abbreviation ambiguous\n",tok);
3422 d_printf("%s: command not found\n",tok);
3428 /****************************************************************************
3429 Process commands from the client.
3430 ****************************************************************************/
3432 static int process(char *base_directory)
3436 cli = cli_cm_open(desthost, service, True);
3441 if (*base_directory) {
3442 rc = do_cd(base_directory);
3450 rc = process_command_string(cmdstr);
3459 /****************************************************************************
3461 ****************************************************************************/
3463 static int do_host_query(char *query_host)
3465 cli = cli_cm_open(query_host, "IPC$", True);
3473 /* Workgroups simply don't make sense over anything
3474 else but port 139... */
3477 cli_cm_set_port( 139 );
3478 cli = cli_cm_open(query_host, "IPC$", True);
3482 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3486 list_servers(lp_workgroup());
3493 /****************************************************************************
3494 Handle a tar operation.
3495 ****************************************************************************/
3497 static int do_tar_op(char *base_directory)
3501 /* do we already have a connection? */
3503 cli = cli_cm_open(desthost, service, True);
3510 if (*base_directory) {
3511 ret = do_cd(base_directory);
3525 /****************************************************************************
3526 Handle a message operation.
3527 ****************************************************************************/
3529 static int do_message_op(void)
3532 struct nmb_name called, calling;
3533 fstring server_name;
3534 char name_type_hex[10];
3537 make_nmb_name(&calling, calling_name, 0x0);
3538 make_nmb_name(&called , desthost, name_type);
3540 fstrcpy(server_name, desthost);
3541 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
3542 fstrcat(server_name, name_type_hex);
3548 /* we can only do messages over port 139 (to windows clients at least) */
3550 msg_port = port ? port : 139;
3552 if (!(cli=cli_initialise()) || (cli_set_port(cli, msg_port) != msg_port) ||
3553 !cli_connect(cli, server_name, &ip)) {
3554 d_printf("Connection to %s failed\n", desthost);
3558 if (!cli_session_request(cli, &calling, &called)) {
3559 d_printf("session request failed\n");
3571 /****************************************************************************
3573 ****************************************************************************/
3575 int main(int argc,char *argv[])
3577 pstring base_directory;
3580 BOOL message = False;
3582 static const char *new_name_resolve_order = NULL;
3586 fstring new_workgroup;
3587 struct poptOption long_options[] = {
3590 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3591 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3592 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3593 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3594 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3595 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3596 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
3597 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3598 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3599 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
3600 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
3601 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3602 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
3604 POPT_COMMON_CONNECTION
3605 POPT_COMMON_CREDENTIALS
3612 pstrcpy(term_code, KANJI);
3618 *base_directory = 0;
3620 /* initialize the workgroup name so we can determine whether or
3621 not it was set by a command line option */
3623 set_global_myworkgroup( "" );
3624 set_global_myname( "" );
3626 /* set default debug level to 0 regardless of what smb.conf sets */
3627 setup_logging( "smbclient", True );
3628 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
3629 if ((dbf = x_fdup(x_stderr))) {
3630 x_setbuf( dbf, NULL );
3633 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options,
3634 POPT_CONTEXT_KEEP_FIRST);
3635 poptSetOtherOptionHelp(pc, "service <password>");
3637 in_client = True; /* Make sure that we tell lp_load we are */
3639 while ((opt = poptGetNextOpt(pc)) != -1) {
3642 /* Messages are sent to NetBIOS name type 0x3
3643 * (Messenger Service). Make sure we default
3644 * to port 139 instead of port 445. srl,crh
3647 cli_cm_set_dest_name_type( name_type );
3648 pstrcpy(desthost,poptGetOptArg(pc));
3650 cli_cm_set_port( 139 );
3655 dest_ip = *interpret_addr2(poptGetOptArg(pc));
3656 if (is_zero_ip(dest_ip))
3660 cli_cm_set_dest_ip( dest_ip );
3668 display_set_stderr();
3672 pstrcpy(query_host, poptGetOptArg(pc));
3675 pstrcpy(term_code, poptGetOptArg(pc));
3678 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
3681 /* We must use old option processing for this. Find the
3682 * position of the -T option in the raw argv[]. */
3685 for (i = 1; i < argc; i++) {
3686 if (strncmp("-T", argv[i],2)==0)
3690 if (!(optnum = tar_parseargs(argc, argv, poptGetOptArg(pc), i))) {
3691 poptPrintUsage(pc, stderr, 0);
3694 /* Now we must eat (optnum - i) options - they have
3695 * been processed by tar_parseargs().
3698 for (i = 0; i < optnum; i++)
3703 pstrcpy(base_directory,poptGetOptArg(pc));
3713 /* check for the -P option */
3716 cli_cm_set_port( port );
3719 * Don't load debug level from smb.conf. It should be
3720 * set by cmdline arg or remain default (0)
3722 AllowDebugChange = False;
3724 /* save the workgroup...
3726 FIXME!! do we need to do this for other options as well
3727 (or maybe a generic way to keep lp_load() from overwriting
3730 fstrcpy( new_workgroup, lp_workgroup() );
3731 pstrcpy( calling_name, global_myname() );
3733 if ( override_logfile )
3734 setup_logging( lp_logfile(), False );
3736 if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
3737 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
3738 argv[0], dyn_CONFIGFILE);
3743 if ( strlen(new_workgroup) != 0 )
3744 set_global_myworkgroup( new_workgroup );
3746 if ( strlen(calling_name) != 0 )
3747 set_global_myname( calling_name );
3749 pstrcpy( calling_name, global_myname() );
3751 if(poptPeekArg(pc)) {
3752 pstrcpy(service,poptGetArg(pc));
3753 /* Convert any '/' characters in the service name to '\' characters */
3754 string_replace(service, '/','\\');
3756 if (count_chars(service,'\\') < 3) {
3757 d_printf("\n%s: Not enough '\\' characters in service\n",service);
3758 poptPrintUsage(pc, stderr, 0);
3763 if (poptPeekArg(pc) && !cmdline_auth_info.got_pass) {
3764 cmdline_auth_info.got_pass = True;
3765 pstrcpy(cmdline_auth_info.password,poptGetArg(pc));
3770 if(new_name_resolve_order)
3771 lp_set_name_resolve_order(new_name_resolve_order);
3773 if (!tar_type && !*query_host && !*service && !message) {
3774 poptPrintUsage(pc, stderr, 0);
3778 poptFreeContext(pc);
3780 /* store the username an password for dfs support */
3782 cli_cm_set_credentials( &cmdline_auth_info );
3783 pstrcpy(username, cmdline_auth_info.username);
3785 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
3789 process_command_string(cmdstr);
3790 return do_tar_op(base_directory);
3794 char *qhost = query_host;
3797 while (*qhost == '\\' || *qhost == '/')
3800 if ((slash = strchr_m(qhost, '/'))
3801 || (slash = strchr_m(qhost, '\\'))) {
3805 if ((p=strchr_m(qhost, '#'))) {
3808 sscanf(p, "%x", &name_type);
3809 cli_cm_set_dest_name_type( name_type );
3812 return do_host_query(qhost);
3816 return do_message_op();
3819 if (process(base_directory)) {