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
8 Copyright (C) Kalim Moghul 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "libsmbclient.h"
29 #include "client/client_proto.h"
34 extern BOOL AllowDebugChange;
35 extern BOOL override_logfile;
37 extern BOOL in_client;
39 pstring cur_dir = "/";
40 static pstring service;
41 static pstring desthost;
42 static pstring username;
43 static pstring workgroup;
44 static pstring calling_name;
45 static BOOL grepable=False;
46 static char *cmdstr = NULL;
48 static int io_bufsize = 64512;
50 static int name_type = 0x20;
51 extern int max_protocol;
53 static int process_tok(pstring tok);
54 static int cmd_help(void);
56 /* 30 second timeout on most commands */
57 #define CLIENT_TIMEOUT (30*1000)
58 #define SHORT_TIMEOUT (5*1000)
60 /* value for unused fid field in trans2 secondary request */
61 #define FID_UNUSED (0xFFFF)
63 time_t newer_than = 0;
64 static int archive_level = 0;
66 static BOOL translation = False;
69 /* clitar bits insert */
72 extern BOOL tar_reset;
76 static BOOL prompt = True;
78 static BOOL recurse = False;
79 BOOL lowercase = False;
81 static struct in_addr dest_ip;
83 #define SEPARATORS " \t\n\r"
85 static BOOL abort_mget = True;
87 static pstring fileselection = "";
89 extern file_info def_finfo;
92 SMB_BIG_UINT get_total_size = 0;
93 unsigned int get_total_time_ms = 0;
94 static SMB_BIG_UINT put_total_size = 0;
95 static unsigned int put_total_time_ms = 0;
98 static double dir_total;
100 /* root cli_state connection */
102 struct cli_state *cli;
105 /****************************************************************************
106 Authentication callback function for libsmbclient
107 ****************************************************************************/
110 get_auth_data_fn(const char * pServer, const char * pShare,
111 char * pWorkgroup, int maxLenWorkgroup,
112 char * pUsername, int maxLenUsername,
113 char * pPassword, int maxLenPassword)
115 char temp[sizeof(fstring)];
117 static char authUsername[sizeof(fstring)];
118 static char authWorkgroup[sizeof(fstring)];
119 static char authPassword[sizeof(fstring)];
120 static char authSet = 0;
125 strncpy(pWorkgroup, authWorkgroup, maxLenWorkgroup - 1);
126 strncpy(pUsername, authUsername, maxLenUsername - 1);
127 strncpy(pPassword, authPassword, maxLenPassword - 1);
131 d_printf("Workgroup: %s\n", workgroup);
132 strncpy(pWorkgroup, workgroup, maxLenWorkgroup - 1);
133 strncpy(authWorkgroup, workgroup, maxLenWorkgroup - 1);
135 d_printf("Username: %s\n", username);
136 strncpy(pUsername, username, maxLenUsername - 1);
137 strncpy(authUsername, username, maxLenUsername - 1);
139 if (cmdline_auth_info.got_pass)
141 strncpy(pPassword, cmdline_auth_info.password, maxLenPassword - 1);
142 strncpy(authPassword, cmdline_auth_info.password, maxLenPassword - 1);
146 char *pass = getpass("Password: ");
149 if (temp[strlen(temp) - 1] == '\n') /* A new line? */
151 temp[strlen(temp) - 1] = '\0';
155 strncpy(pPassword, temp, maxLenPassword - 1);
156 strncpy(authPassword, pPassword, maxLenPassword - 1);
159 pstrcpy(cmdline_auth_info.username, authUsername);
160 pstrcpy(cmdline_auth_info.password, authPassword);
161 cmdline_auth_info.got_pass = True;
162 set_global_myworkgroup(authWorkgroup);
163 cli_cm_set_credentials(&cmdline_auth_info);
168 /*******************************************************************
169 Return a string representing an attribute for a file.
170 ********************************************************************/
172 fstring *mode_t_string(mode_t mode)
174 static fstring attrstr;
178 S_ISDIR(mode) ? fstrcat(attrstr, "d") : fstrcat(attrstr, "-");
179 (mode & S_IRUSR) ? fstrcat(attrstr, "r") : fstrcat(attrstr, "-");
180 (mode & S_IWUSR) ? fstrcat(attrstr, "w") : fstrcat(attrstr, "-");
181 (mode & S_IXUSR) ? fstrcat(attrstr, "x") : fstrcat(attrstr, "-");
183 (mode & S_IRGRP) ? fstrcat(attrstr, "r") : fstrcat(attrstr, "-");
184 (mode & S_IWGRP) ? fstrcat(attrstr, "w") : fstrcat(attrstr, "-");
185 (mode & S_IXGRP) ? fstrcat(attrstr, "x") : fstrcat(attrstr, "-");
187 (mode & S_IROTH) ? fstrcat(attrstr, "r") : fstrcat(attrstr, "-");
188 (mode & S_IWOTH) ? fstrcat(attrstr, "w") : fstrcat(attrstr, "-");
189 (mode & S_IXOTH) ? fstrcat(attrstr, "x") : fstrcat(attrstr, "-");
195 /****************************************************************************
196 Write to a local file with CR/LF->LF translation if appropriate. Return the
197 number taken from the buffer. This may not equal the number written.
198 ****************************************************************************/
200 static int writefile(int f, char *b, int n)
210 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
213 if (write(f, b, 1) != 1) {
223 /****************************************************************************
224 Read from a file with LF->CR/LF translation if appropriate. Return the
225 number read. read approx n bytes.
226 ****************************************************************************/
228 static int readfile(char *b, int n, XFILE *f)
234 return x_fread(b,1,n,f);
237 while (i < (n - 1) && (i < BUFFER_SIZE)) {
238 if ((c = x_getc(f)) == EOF) {
242 if (c == '\n') { /* change all LFs to CR/LF */
252 /****************************************************************************
254 ****************************************************************************/
256 static void send_message(void)
261 if (!cli_message_start(cli, desthost, username, &grp_id)) {
262 d_printf("message start: %s\n", cli_errstr(cli));
267 d_printf("Connected. Type your message, ending it with a Control-D\n");
269 while (!feof(stdin) && total_len < 1600) {
270 int maxlen = MIN(1600 - total_len,127);
277 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
283 if (!cli_message_text(cli, msg, l, grp_id)) {
284 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
291 if (total_len >= 1600)
292 d_printf("the message was truncated to 1600 bytes\n");
294 d_printf("sent %d bytes\n",total_len);
296 if (!cli_message_end(cli, grp_id)) {
297 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
302 /****************************************************************************
303 Check the space on a device.
304 ****************************************************************************/
306 static int do_dskattr(void)
308 int total, bsize, avail;
309 struct cli_state *targetcli;
312 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
313 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
317 if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
318 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli));
322 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
323 total, bsize, avail);
328 /****************************************************************************
330 ****************************************************************************/
332 static int cmd_pwd(void)
334 d_printf("Current directory is %s",service);
335 d_printf("%s\n",cur_dir);
339 /****************************************************************************
340 Change directory - inner section.
341 ****************************************************************************/
343 static int do_cd(char *newdir)
349 struct cli_state *targetcli;
350 SMB_STRUCT_STAT sbuf;
356 /* Save the current directory in case the new directory is invalid */
357 pstrcpy(saved_dir, cur_dir);
359 pstrcpy(dname, p); /* first save the argument */
362 pstrcpy(cur_dir,dname);
364 pstrcat(cur_dir,dname);
366 if (*(cur_dir+strlen(cur_dir)-1) != '/') {
367 pstrcat(cur_dir, "/");
370 all_string_sub(cur_dir, "/./", "/", 0);
372 /* Format the directory in a libmsmbclient friendly way */
373 unix_clean_name(cur_dir);
374 all_string_sub(cur_dir, "/./", "/", 0);
375 pstrcpy(targetpath, "smb:");
376 pstrcat(targetpath, service);
377 pstrcat(targetpath, cur_dir);
378 unix_format(targetpath);
380 dh = smbc_opendir(targetpath);
384 d_printf("%s changing to directory %s\n", strerror(errno), cur_dir);
385 pstrcpy(cur_dir, saved_dir);
393 /****************************************************************************
395 ****************************************************************************/
397 static int cmd_cd(void)
402 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
405 d_printf("Current directory is %s\n",cur_dir);
410 /*******************************************************************
411 Decide if a file should be operated on.
412 ********************************************************************/
414 static BOOL do_this_one(file_info *finfo)
416 if (finfo->mode & aDIR)
419 if (*fileselection &&
420 !mask_match(finfo->name,fileselection,False)) {
421 DEBUG(3,("mask_match %s failed\n", finfo->name));
425 if (newer_than && finfo->mtime < newer_than) {
426 DEBUG(3,("newer_than %s failed\n", finfo->name));
430 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
431 DEBUG(3,("archive %s failed\n", finfo->name));
438 /****************************************************************************
439 Display info about a file.
440 ****************************************************************************/
442 static void display_finfo(file_info *finfo)
444 if (do_this_one(finfo)) {
445 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
446 d_printf(" %-30s%7.7s %8.0f %s",
448 attrib_string(finfo->mode),
450 asctime(localtime(&t)));
451 dir_total += finfo->size;
455 /****************************************************************************
456 Display info about a file.
457 ****************************************************************************/
459 static void display_stat(char *name, struct stat *st)
461 time_t t = st->st_mtime;
463 pstrcpy(time_str, asctime(localtime(&t)));
464 time_str[strlen(time_str)-1] = 0;
465 d_printf("> %-30s", name);
466 d_printf("%10.10s %8.0f %s\n", *mode_t_string(st->st_mode), (double)st->st_size, time_str);
469 /****************************************************************************
470 Accumulate size of a file.
471 ****************************************************************************/
473 static void do_du(file_info *finfo)
475 if (do_this_one(finfo)) {
476 dir_total += finfo->size;
480 static BOOL do_list_recurse;
481 static BOOL do_list_dirs;
482 static char *do_list_queue = 0;
483 static long do_list_queue_size = 0;
484 static long do_list_queue_start = 0;
485 static long do_list_queue_end = 0;
486 static void (*do_list_fn)(file_info *);
487 static void (*tool_list_fn)(char *, struct stat *);
489 /****************************************************************************
490 Functions for do_list_queue.
491 ****************************************************************************/
494 * The do_list_queue is a NUL-separated list of strings stored in a
495 * char*. Since this is a FIFO, we keep track of the beginning and
496 * ending locations of the data in the queue. When we overflow, we
497 * double the size of the char*. When the start of the data passes
498 * the midpoint, we move everything back. This is logically more
499 * complex than a linked list, but easier from a memory management
500 * angle. In any memory error condition, do_list_queue is reset.
501 * Functions check to ensure that do_list_queue is non-NULL before
505 static void reset_do_list_queue(void)
507 SAFE_FREE(do_list_queue);
508 do_list_queue_size = 0;
509 do_list_queue_start = 0;
510 do_list_queue_end = 0;
513 static void init_do_list_queue(void)
515 reset_do_list_queue();
516 do_list_queue_size = 1024;
517 do_list_queue = SMB_MALLOC(do_list_queue_size);
518 if (do_list_queue == 0) {
519 d_printf("malloc fail for size %d\n",
520 (int)do_list_queue_size);
521 reset_do_list_queue();
523 memset(do_list_queue, 0, do_list_queue_size);
527 static void adjust_do_list_queue(void)
530 * If the starting point of the queue is more than half way through,
531 * move everything toward the beginning.
533 if (do_list_queue && (do_list_queue_start == do_list_queue_end)) {
534 DEBUG(4,("do_list_queue is empty\n"));
535 do_list_queue_start = do_list_queue_end = 0;
536 *do_list_queue = '\0';
537 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
538 DEBUG(4,("sliding do_list_queue backward\n"));
539 memmove(do_list_queue,
540 do_list_queue + do_list_queue_start,
541 do_list_queue_end - do_list_queue_start);
542 do_list_queue_end -= do_list_queue_start;
543 do_list_queue_start = 0;
547 static void add_to_do_list_queue(const char* entry)
550 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
551 while (new_end > do_list_queue_size) {
552 do_list_queue_size *= 2;
553 DEBUG(4,("enlarging do_list_queue to %d\n",
554 (int)do_list_queue_size));
555 dlq = SMB_REALLOC(do_list_queue, do_list_queue_size);
557 d_printf("failure enlarging do_list_queue to %d bytes\n",
558 (int)do_list_queue_size);
559 reset_do_list_queue();
562 memset(do_list_queue + do_list_queue_size / 2,
563 0, do_list_queue_size / 2);
567 safe_strcpy_base(do_list_queue + do_list_queue_end,
568 entry, do_list_queue, do_list_queue_size);
569 do_list_queue_end = new_end;
570 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
571 entry, (int)do_list_queue_start, (int)do_list_queue_end));
575 static char *do_list_queue_head(void)
577 return do_list_queue + do_list_queue_start;
580 static void remove_do_list_queue_head(void)
582 if (do_list_queue_end > do_list_queue_start) {
583 do_list_queue_start += strlen(do_list_queue_head()) + 1;
584 adjust_do_list_queue();
585 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
586 (int)do_list_queue_start, (int)do_list_queue_end));
590 static int do_list_queue_empty(void)
592 return (! (do_list_queue && *do_list_queue));
595 /****************************************************************************
596 A helper for tool_list.
597 ****************************************************************************/
599 static void tool_list_helper(const char *mntpoint, struct stat *f, const char *mask, void *state)
601 /*if (f is a directory)
603 if (we want to do directories and we want to do this f)
605 execute the callback on f
607 if (recursion is set and f isn't . and it isn't ..)
609 make sure the name is valid
610 construct a full path out of the name
611 add the full path to the list
615 if (we want to do this f)
617 execute the callback on f
621 /****************************************************************************
622 A cli_list-like function that executes fn on each directory entry.
623 fn operates on the returned entry name and struct stat.
624 ****************************************************************************/
626 int tool_list( char *mask,
628 void (*fn)(char *, struct stat *),
636 struct smbc_dirent* dent;
638 pstrcpy(res, "smb:");
639 pstrcat(res, service);
640 pstrcat(res, cur_dir);
642 if ((dh = smbc_opendir(res)) < 1)
644 d_printf("Error: %s opening %s\n", strerror(errno), res);
648 while (dent = smbc_readdir(dh))
650 switch(dent->smbc_type)
654 case SMBC_FILE_SHARE:
655 case SMBC_PRINTER_SHARE:
656 case SMBC_COMMS_SHARE:
663 pstrcpy(dentname, res);
664 pstrcat(dentname, dent->name);
665 /*if (mask_match(dent->name, mask, False))*/
666 if (mask_match(dentname, mask, False))
668 if (smbc_stat(dentname, &stat) < 0)
670 d_printf("> %s - stat error: %s\n", dent->name, strerror(errno));
674 fn(dent->name, &stat);
685 /****************************************************************************
686 A helper for do_list.
687 ****************************************************************************/
689 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
691 if (f->mode & aDIR) {
692 if (do_list_dirs && do_this_one(f)) {
695 if (do_list_recurse &&
696 !strequal(f->name,".") &&
697 !strequal(f->name,"..")) {
702 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
706 pstrcpy(mask2, mntpoint);
707 pstrcat(mask2, mask);
708 p = strrchr_m(mask2,'/');
712 pstrcat(mask2, f->name);
714 add_to_do_list_queue(mask2);
719 if (do_this_one(f)) {
724 /****************************************************************************
725 A wrapper around cli_list that adds recursion.
726 ****************************************************************************/
728 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
730 static int in_do_list = 0;
731 struct cli_state *targetcli;
734 if (in_do_list && rec) {
735 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
741 do_list_recurse = rec;
746 init_do_list_queue();
747 add_to_do_list_queue(mask);
749 while (! do_list_queue_empty()) {
751 * Need to copy head so that it doesn't become
752 * invalid inside the call to cli_list. This
753 * would happen if the list were expanded
755 * Fix from E. Jay Berkenbilt (ejb@ql.org)
758 pstrcpy(head, do_list_queue_head());
762 if ( !cli_resolve_path( "", cli, head, &targetcli, targetpath ) ) {
763 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
764 remove_do_list_queue_head();
768 cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
769 remove_do_list_queue_head();
770 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
771 char* next_file = do_list_queue_head();
773 if ((strlen(next_file) >= 2) &&
774 (next_file[strlen(next_file) - 1] == '*') &&
775 (next_file[strlen(next_file) - 2] == '/')) {
776 save_ch = next_file +
777 strlen(next_file) - 2;
780 d_printf("\n%s\n",next_file);
789 if ( cli_resolve_path( "", cli, mask, &targetcli, targetpath ) ) {
790 if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1)
791 d_printf("%s listing %s\n", cli_errstr(targetcli), targetpath);
794 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
799 reset_do_list_queue();
802 /****************************************************************************
803 Get a directory listing.
804 ****************************************************************************/
806 static int cmd_dir(void)
808 mode_t mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
813 pstrcpy(mask, "smb:");
814 pstrcat(mask, service);
815 pstrcat(mask, cur_dir);
817 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
822 tool_list(mask, mode, display_stat, recurse, True);
826 /****************************************************************************
827 Get a directory listing.
828 ****************************************************************************/
830 static int cmd_du(void)
832 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
839 pstrcpy(mask,cur_dir);
840 if(mask[strlen(mask)-1]!='/')
843 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
853 do_list(mask, attribute, do_du, recurse, True);
857 d_printf("Total number of bytes: %.0f\n", dir_total);
862 /****************************************************************************
863 Get a file from rname to lname
864 ****************************************************************************/
866 static int do_get(char *rname, char *lname, BOOL reget)
868 int handle = 0, fnum;
869 BOOL newhandle = False;
871 struct timeval tp_start;
872 int read_size = io_bufsize;
883 GetTimeOfDay(&tp_start);
885 fnum = smbc_open(rname, O_RDONLY, 0666);
888 d_printf("%s opening remote file %s\n", strerror(errno), rname);
892 if(!strcmp(lname,"-")) {
893 handle = fileno(stdout);
896 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
898 start = sys_lseek(handle, 0, SEEK_END);
901 d_printf("Error seeking local file\n");
906 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
911 d_printf("Error opening local file %s\n",lname);
916 if (smbc_fstat(fnum, &stat) < 0)
918 d_printf("%s trying to stat remote file %s\n", strerror(errno), rname);
925 DEBUG(1,("getting file %s of size %.0f as %s ",
926 rname, (double)stat.st_size, lname));
928 if(!(data = (char *)SMB_MALLOC(read_size))) {
929 d_printf("malloc fail for size %d\n", read_size);
936 if (smbc_lseek(fnum, start, SEEK_SET) < 0)
938 d_printf("%s trying to lseek remote file %s\n", strerror(errno), rname);
946 int n = smbc_read(fnum, data, read_size);
950 d_printf("%s while reading remote file %s\n", strerror(errno), rname);
960 if (writefile(handle,data, n) != n) {
961 d_printf("Error writing local file\n");
969 if (nread + start < stat.st_size) {
970 DEBUG (1, ("Short read when getting file %s. Only got %ld bytes.\n", rname, (long)nread));
976 if (smbc_close(fnum) < 0)
978 d_printf("%s closing remote file %s\n", strerror(errno), rname);
986 /*if (archive_level >= 2 && (attr & aARCH)) {
987 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
991 struct timeval tp_end;
994 GetTimeOfDay(&tp_end);
996 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
997 (tp_end.tv_usec - tp_start.tv_usec)/1000;
998 get_total_time_ms += this_time;
999 get_total_size += nread;
1001 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1002 nread / (1.024*this_time + 1.0e-4),
1003 get_total_size / (1.024*get_total_time_ms)));
1009 /****************************************************************************
1011 ****************************************************************************/
1013 static int cmd_get(void)
1019 pstrcpy(rname, "smb:");
1020 pstrcat(rname, service);
1021 pstrcat(rname, cur_dir);
1023 p = rname + strlen(rname);
1025 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
1026 d_printf("get <filename>\n");
1031 next_token_nr(NULL,lname,NULL,sizeof(lname));
1032 /*d_printf("lname: %s, rname: %s\n", lname, rname);*/
1033 return do_get(rname, lname, False);
1036 /****************************************************************************
1037 Do an mget operation on one file.
1038 ****************************************************************************/
1040 static void do_mget(char *name, struct stat *st)
1044 pstring saved_curdir;
1048 if (strequal(name,".") || strequal(name,".."))
1051 if (S_ISDIR(st->st_mode))
1052 slprintf(quest,sizeof(pstring)-1, "Get directory %s%s? ", cur_dir, name);
1054 slprintf(quest,sizeof(pstring)-1, "Get file %s%s? ", cur_dir, name);
1056 if (prompt && !yesno(quest))
1059 if (!S_ISDIR(st->st_mode)) {
1060 pstrcpy(rname,"smb:");
1061 pstrcat(rname,service);
1062 pstrcat(rname,cur_dir);
1063 pstrcat(rname,name);
1064 do_get(rname,name, False);
1068 /* handle directories */
1069 /* TODO: clean this code up for recursive calls */
1070 pstrcpy(saved_curdir,cur_dir);
1072 pstrcat(cur_dir,name);
1073 pstrcat(cur_dir,"/");
1079 if (!directory_exist(name,NULL) &&
1080 mkdir(name,0777) != 0) {
1081 d_printf("failed to create directory %s\n",name);
1082 pstrcpy(cur_dir,saved_curdir);
1086 if (chdir(name) != 0) {
1087 d_printf("failed to chdir to directory %s\n",name);
1088 pstrcpy(cur_dir,saved_curdir);
1092 pstrcpy(mget_mask,"smb:");
1093 pstrcat(mget_mask,service);
1094 pstrcat(mget_mask,cur_dir);
1095 pstrcat(mget_mask,"*");
1097 /*d_printf("Calling with mask: %s\n", mget_mask);*/
1098 tool_list(mget_mask, mode, do_mget, recurse, recurse);
1100 pstrcpy(cur_dir,saved_curdir);
1103 /****************************************************************************
1104 View the file using the pager.
1105 ****************************************************************************/
1107 static int cmd_more(void)
1109 pstring rname,lname,pager_cmd;
1114 pstrcpy(rname,cur_dir);
1117 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
1118 fd = smb_mkstemp(lname);
1120 d_printf("failed to create temporary file for more\n");
1125 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
1126 d_printf("more <filename>\n");
1130 dos_clean_name(rname);
1132 rc = do_get(rname, lname, False);
1134 pager=getenv("PAGER");
1136 slprintf(pager_cmd,sizeof(pager_cmd)-1,
1137 "%s %s",(pager? pager:PAGER), lname);
1144 /****************************************************************************
1146 ****************************************************************************/
1148 static int cmd_mget(void)
1157 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1158 pstrcpy(mget_mask, "smb:");
1159 pstrcat(mget_mask, service);
1160 pstrcat(mget_mask,cur_dir);
1161 if(mget_mask[strlen(mget_mask)-1]!='/')
1162 pstrcat(mget_mask,"/");
1166 pstrcpy(mget_mask, "smb:");
1167 pstrcat(mget_mask, service);
1168 pstrcat(mget_mask,p);
1171 pstrcat(mget_mask,p);
1172 /* TODO: enable directories on calls to tool_list
1173 once recursion is worked out */
1174 tool_list(mget_mask, mode, do_mget, recurse, recurse);
1178 pstrcpy(mget_mask, "smb:");
1179 pstrcat(mget_mask, service);
1180 pstrcat(mget_mask,cur_dir);
1181 if(mget_mask[strlen(mget_mask)-1]!='/')
1182 pstrcat(mget_mask,"/");
1183 pstrcat(mget_mask,"*");
1184 tool_list(mget_mask, mode, do_mget, recurse, recurse);
1190 /****************************************************************************
1191 Make a directory of name "name".
1192 ****************************************************************************/
1194 static BOOL do_mkdir(char *name)
1196 if (smbc_mkdir(name, 755) < 0)
1198 d_printf("Error: %s making remote directory %s\n", strerror(errno), name);
1205 /****************************************************************************
1206 Show 8.3 name of a file.
1207 ****************************************************************************/
1209 static BOOL do_altname(char *name)
1212 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1213 d_printf("%s getting alt name for %s\n",
1214 cli_errstr(cli),name);
1217 d_printf("%s\n", altname);
1222 /****************************************************************************
1224 ****************************************************************************/
1226 static int cmd_quit(void)
1234 /****************************************************************************
1236 ****************************************************************************/
1238 static int cmd_mkdir(void)
1246 pstrcpy(mask,cur_dir);
1248 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1250 d_printf("mkdir <dirname>\n");
1261 trim_char(ddir,'.','\0');
1262 p = strtok(ddir,"/\\");
1266 pstrcpy(targetname, "smb:");
1267 pstrcat(targetname, service);
1268 pstrcat(targetname, "/");
1269 pstrcat(targetname, ddir2);
1270 DEBUG(3, ("Recursively making directory %s\n", targetname));
1271 if ((dh = smbc_opendir(targetname)) < 0) {
1272 if (!do_mkdir(targetname))
1279 p = strtok(NULL,"/\\");
1283 pstrcpy(targetname, "smb:");
1284 pstrcat(targetname, service);
1285 pstrcat(targetname, mask);
1287 if (!do_mkdir(targetname))
1294 /****************************************************************************
1296 ****************************************************************************/
1298 static int cmd_altname(void)
1304 pstrcpy(name,cur_dir);
1306 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1307 d_printf("altname <file>\n");
1317 /****************************************************************************
1319 ****************************************************************************/
1321 static int do_put(char *rname, char *lname, BOOL reput)
1325 SMB_OFF_T start = 0;
1328 int maxwrite = io_bufsize;
1330 struct timeval tp_start;
1333 GetTimeOfDay(&tp_start);
1336 fnum = smbc_open(rname, O_RDWR|O_CREAT, 0644);
1339 d_printf("%s opening remote file %s\n", strerror(errno), rname);
1342 if (smbc_fstat(fnum, &stat) < 0)
1344 d_printf("%s trying to stat remote file %s\n", strerror(errno), rname);
1348 start = stat.st_size;
1350 fnum = smbc_creat(rname, 0644);
1353 d_printf("%s trying to create remote file %s\n", strerror(errno), rname);
1358 /* allow files to be piped into smbclient
1361 Note that in this case this function will exit(0) rather
1363 if (!strcmp(lname, "-")) {
1365 /* size of file is not known */
1367 f = x_fopen(lname,O_RDONLY, 0);
1369 if (x_tseek(f, start, SEEK_SET) == -1) {
1370 d_printf("Error seeking local file\n");
1379 d_printf("Error opening local file %s\n",lname);
1384 DEBUG(1,("putting file %s as %s ",lname,rname));
1386 buf = (char *)SMB_MALLOC(maxwrite);
1388 d_printf("ERROR: Not enough memory!\n");
1395 if (smbc_lseek(fnum, start, SEEK_SET) < 0)
1397 d_printf("%s trying to lseek remote file %s\n", strerror(errno), rname);
1404 while (!x_feof(f)) {
1408 if ((n = readfile(buf,n,f)) < 1) {
1409 if((n == 0) && x_feof(f))
1410 break; /* Empty local file. */
1412 d_printf("Error reading local file: %s\n", strerror(errno));
1417 ret = smbc_write(fnum, buf, n);
1420 d_printf("Error writing file: %s\n", strerror(errno));
1428 if (smbc_close(fnum) < 0) {
1429 d_printf("%s closing remote file %s\n",strerror(errno),rname);
1444 struct timeval tp_end;
1447 GetTimeOfDay(&tp_end);
1449 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1450 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1451 put_total_time_ms += this_time;
1452 put_total_size += nread;
1454 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1455 nread / (1.024*this_time + 1.0e-4),
1456 put_total_size / (1.024*put_total_time_ms)));
1467 /****************************************************************************
1469 ****************************************************************************/
1471 static int cmd_put(void)
1478 pstrcpy(rname, "smb:");
1479 pstrcat(rname, service);
1480 pstrcat(rname, cur_dir);
1482 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1483 d_printf("put <filename>\n");
1488 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1491 pstrcat(rname,lname);
1495 /* allow '-' to represent stdin
1496 jdblair, 24.jun.98 */
1497 if (!file_exist(lname,&st) &&
1498 (strcmp(lname,"-"))) {
1499 d_printf("%s does not exist\n",lname);
1504 /*d_printf("lname: %s, rname: %s\n", lname, rname);*/
1505 return do_put(rname, lname, False);
1508 /*************************************
1509 File list structure.
1510 *************************************/
1512 static struct file_list {
1513 struct file_list *prev, *next;
1518 /****************************************************************************
1519 Free a file_list structure.
1520 ****************************************************************************/
1522 static void free_file_list (struct file_list * list)
1524 struct file_list *tmp;
1528 DLIST_REMOVE(list, list);
1529 SAFE_FREE(tmp->file_path);
1534 /****************************************************************************
1535 Seek in a directory/file list until you get something that doesn't start with
1537 ****************************************************************************/
1539 static BOOL seek_list(struct file_list *list, char *name)
1542 trim_string(list->file_path,"./","\n");
1543 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1552 /****************************************************************************
1553 Set the file selection mask.
1554 ****************************************************************************/
1556 static int cmd_select(void)
1558 pstrcpy(fileselection,"");
1559 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1564 /****************************************************************************
1565 Recursive file matching function act as find
1566 match must be always set to True when calling this function
1567 ****************************************************************************/
1569 static int file_find(struct file_list **list, const char *directory,
1570 const char *expression, BOOL match)
1573 struct file_list *entry;
1574 struct stat statbuf;
1580 dir = opendir(directory);
1584 while ((dname = readdirname(dir))) {
1585 if (!strcmp("..", dname))
1587 if (!strcmp(".", dname))
1590 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1595 if (!match || !gen_fnmatch(expression, dname)) {
1597 ret = stat(path, &statbuf);
1599 if (S_ISDIR(statbuf.st_mode)) {
1601 ret = file_find(list, path, expression, False);
1604 d_printf("file_find: cannot stat file %s\n", path);
1613 entry = SMB_MALLOC_P(struct file_list);
1615 d_printf("Out of memory in file_find\n");
1619 entry->file_path = path;
1620 entry->isdir = isdir;
1621 DLIST_ADD(*list, entry);
1631 /****************************************************************************
1633 ****************************************************************************/
1635 static int cmd_mput(void)
1640 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1642 struct file_list *temp_list;
1643 char *quest, *lname, *rname;
1647 ret = file_find(&file_list, ".", p, True);
1649 free_file_list(file_list);
1657 for (temp_list = file_list; temp_list;
1658 temp_list = temp_list->next) {
1661 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1663 trim_string(lname, "./", "/");
1665 /* check if it's a directory */
1666 if (temp_list->isdir) {
1667 /* if (!recurse) continue; */
1670 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1671 if (prompt && !yesno(quest)) { /* No */
1672 /* Skip the directory */
1673 lname[strlen(lname)-1] = '/';
1674 if (!seek_list(temp_list, lname))
1678 if(asprintf(&rname, "smb:%s%s%s", service, cur_dir, lname) < 0) break;
1679 /*dos_format(rname);*/
1681 /* test if the directory exists by opening it */
1682 /*if (!((dh=smbc_opendir(rname)) < 0)) {
1686 /* test if the directory exists by making it */
1687 ret = smbc_mkdir(rname, 755);
1688 if ((ret != 0) && (errno != EEXIST))
1690 d_printf("Error: %s. Unable to open or create dir %s, skipping...\n", strerror(errno), rname);
1691 /* Skip the directory */
1692 lname[strlen(lname)-1] = '/';
1693 if (!seek_list(temp_list, lname))
1700 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1701 if (prompt && !yesno(quest)) /* No */
1706 if (asprintf(&rname, "smb:%s%s%s", service, cur_dir, lname) < 0) break;
1709 /*d_printf("PUT: rname: %s, lname: %s\n", rname, lname);*/
1710 do_put(rname, lname, False);
1712 free_file_list(file_list);
1721 /****************************************************************************
1723 ****************************************************************************/
1725 static int do_cancel(int job)
1727 if (cli_printjob_del(cli, job)) {
1728 d_printf("Job %d cancelled\n",job);
1731 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1736 /****************************************************************************
1738 ****************************************************************************/
1740 static int cmd_cancel(void)
1745 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1746 d_printf("cancel <jobid> ...\n");
1752 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1757 /****************************************************************************
1759 ****************************************************************************/
1761 static int cmd_print(void)
1767 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1768 d_printf("print <filename>\n");
1772 pstrcpy(rname,lname);
1773 p = strrchr_m(rname,'/');
1775 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1778 if (strequal(lname,"-")) {
1779 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1782 return do_put(rname, lname, False);
1785 /****************************************************************************
1786 Show a print queue entry.
1787 ****************************************************************************/
1789 static void queue_fn(struct print_job_info *p)
1791 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
1794 /****************************************************************************
1796 ****************************************************************************/
1798 static int cmd_queue(void)
1800 cli_print_queue(cli, queue_fn);
1805 /****************************************************************************
1807 ****************************************************************************/
1809 static void do_del(file_info *finfo)
1813 pstrcpy(mask,cur_dir);
1814 pstrcat(mask,finfo->name);
1816 if (finfo->mode & aDIR)
1819 if (!cli_unlink(cli, mask)) {
1820 d_printf("%s deleting remote file %s\n",cli_errstr(cli),mask);
1824 /****************************************************************************
1826 ****************************************************************************/
1828 static int cmd_del(void)
1832 uint16 attribute = aSYSTEM | aHIDDEN;
1837 pstrcpy(mask,cur_dir);
1839 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1840 d_printf("del <filename>\n");
1845 do_list(mask, attribute,do_del,False,False);
1850 /****************************************************************************
1851 ****************************************************************************/
1853 static int cmd_open(void)
1857 struct cli_state *targetcli;
1860 pstrcpy(mask,cur_dir);
1862 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1863 d_printf("open <filename>\n");
1868 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1869 d_printf("open %s: %s\n", mask, cli_errstr(cli));
1873 cli_nt_create(targetcli, targetname, FILE_READ_DATA);
1879 /****************************************************************************
1881 ****************************************************************************/
1883 static int cmd_rmdir(void)
1887 struct cli_state *targetcli;
1890 pstrcpy(mask, "smb:");
1891 pstrcat(mask, service);
1892 pstrcat(mask,cur_dir);
1894 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1895 d_printf("rmdir <dirname>\n");
1900 /*if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1901 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
1905 if (!cli_rmdir(targetcli, targetname)) {
1906 d_printf("%s removing remote directory file %s\n",
1907 cli_errstr(targetcli),mask);
1910 if (smbc_rmdir(mask) < 0)
1911 d_printf("Error: %s removing remote directory file %s\n", strerror(errno), mask);
1916 /****************************************************************************
1918 ****************************************************************************/
1920 static int cmd_link(void)
1922 pstring oldname,newname;
1924 struct cli_state *targetcli;
1927 pstrcpy(oldname,cur_dir);
1928 pstrcpy(newname,cur_dir);
1930 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1931 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1932 d_printf("link <oldname> <newname>\n");
1936 pstrcat(oldname,buf);
1937 pstrcat(newname,buf2);
1939 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
1940 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
1944 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1945 d_printf("Server doesn't support UNIX CIFS calls.\n");
1949 if (!cli_unix_hardlink(targetcli, targetname, newname)) {
1950 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
1957 /****************************************************************************
1959 ****************************************************************************/
1961 static int cmd_symlink(void)
1963 pstring oldname,newname;
1966 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1967 d_printf("Server doesn't support UNIX CIFS calls.\n");
1971 pstrcpy(newname,cur_dir);
1973 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1974 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1975 d_printf("symlink <oldname> <newname>\n");
1979 pstrcpy(oldname,buf);
1980 pstrcat(newname,buf2);
1982 if (!cli_unix_symlink(cli, oldname, newname)) {
1983 d_printf("%s symlinking files (%s -> %s)\n",
1984 cli_errstr(cli), newname, oldname);
1991 /****************************************************************************
1993 ****************************************************************************/
1995 static int cmd_chmod(void)
2000 struct cli_state *targetcli;
2003 pstrcpy(src,cur_dir);
2005 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2006 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2007 d_printf("chmod mode file\n");
2011 mode = (mode_t)strtol(buf, NULL, 8);
2014 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2015 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
2019 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2020 d_printf("Server doesn't support UNIX CIFS calls.\n");
2024 if (!cli_unix_chmod(targetcli, targetname, mode)) {
2025 d_printf("%s chmod file %s 0%o\n",
2026 cli_errstr(targetcli), src, (unsigned int)mode);
2033 static const char *filetype_to_str(mode_t mode)
2035 if (S_ISREG(mode)) {
2036 return "regular file";
2037 } else if (S_ISDIR(mode)) {
2041 if (S_ISCHR(mode)) {
2042 return "character device";
2046 if (S_ISBLK(mode)) {
2047 return "block device";
2051 if (S_ISFIFO(mode)) {
2056 if (S_ISLNK(mode)) {
2057 return "symbolic link";
2061 if (S_ISSOCK(mode)) {
2068 static char rwx_to_str(mode_t m, mode_t bt, char ret)
2077 static char *unix_mode_to_str(char *s, mode_t m)
2080 const char *str = filetype_to_str(m);
2096 *p++ = str[1] == 'y' ? 'l' : 's';
2103 *p++ = rwx_to_str(m, S_IRUSR, 'r');
2104 *p++ = rwx_to_str(m, S_IWUSR, 'w');
2105 *p++ = rwx_to_str(m, S_IXUSR, 'x');
2106 *p++ = rwx_to_str(m, S_IRGRP, 'r');
2107 *p++ = rwx_to_str(m, S_IWGRP, 'w');
2108 *p++ = rwx_to_str(m, S_IXGRP, 'x');
2109 *p++ = rwx_to_str(m, S_IROTH, 'r');
2110 *p++ = rwx_to_str(m, S_IWOTH, 'w');
2111 *p++ = rwx_to_str(m, S_IXOTH, 'x');
2116 /****************************************************************************
2117 Utility function for UNIX getfacl.
2118 ****************************************************************************/
2120 static char *perms_to_string(fstring permstr, unsigned char perms)
2122 fstrcpy(permstr, "---");
2123 if (perms & SMB_POSIX_ACL_READ) {
2126 if (perms & SMB_POSIX_ACL_WRITE) {
2129 if (perms & SMB_POSIX_ACL_EXECUTE) {
2135 /****************************************************************************
2137 ****************************************************************************/
2139 static int cmd_getfacl(void)
2142 uint16 major, minor;
2143 uint32 caplow, caphigh;
2144 char *retbuf = NULL;
2146 SMB_STRUCT_STAT sbuf;
2147 uint16 num_file_acls = 0;
2148 uint16 num_dir_acls = 0;
2150 struct cli_state *targetcli;
2153 pstrcpy(src,cur_dir);
2155 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2156 d_printf("stat file\n");
2162 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2163 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2167 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2168 d_printf("Server doesn't support UNIX CIFS calls.\n");
2172 if (!cli_unix_extensions_version(targetcli, &major, &minor, &caplow, &caphigh)) {
2173 d_printf("Can't get UNIX CIFS version from server.\n");
2177 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
2178 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
2183 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2184 d_printf("%s getfacl doing a stat on file %s\n",
2185 cli_errstr(targetcli), src);
2189 if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
2190 d_printf("%s getfacl file %s\n",
2191 cli_errstr(targetcli), src);
2195 /* ToDo : Print out the ACL values. */
2196 if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
2197 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2198 src, (unsigned int)CVAL(retbuf,0) );
2203 num_file_acls = SVAL(retbuf,2);
2204 num_dir_acls = SVAL(retbuf,4);
2205 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
2206 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2208 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
2209 (unsigned int)rb_size);
2215 d_printf("# file: %s\n", src);
2216 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
2218 if (num_file_acls == 0 && num_dir_acls == 0) {
2219 d_printf("No acls found.\n");
2222 for (i = 0; i < num_file_acls; i++) {
2225 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
2226 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2229 case SMB_POSIX_ACL_USER_OBJ:
2232 case SMB_POSIX_ACL_USER:
2233 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2234 d_printf("user:%u:", uorg);
2236 case SMB_POSIX_ACL_GROUP_OBJ:
2237 d_printf("group::");
2239 case SMB_POSIX_ACL_GROUP:
2240 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2241 d_printf("group:%u", uorg);
2243 case SMB_POSIX_ACL_MASK:
2246 case SMB_POSIX_ACL_OTHER:
2247 d_printf("other::");
2250 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2251 src, (unsigned int)tagtype );
2256 d_printf("%s\n", perms_to_string(permstring, perms));
2259 for (i = 0; i < num_dir_acls; i++) {
2262 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
2263 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2266 case SMB_POSIX_ACL_USER_OBJ:
2267 d_printf("default:user::");
2269 case SMB_POSIX_ACL_USER:
2270 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2271 d_printf("default:user:%u:", uorg);
2273 case SMB_POSIX_ACL_GROUP_OBJ:
2274 d_printf("default:group::");
2276 case SMB_POSIX_ACL_GROUP:
2277 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2278 d_printf("default:group:%u", uorg);
2280 case SMB_POSIX_ACL_MASK:
2281 d_printf("default:mask::");
2283 case SMB_POSIX_ACL_OTHER:
2284 d_printf("default:other::");
2287 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2288 src, (unsigned int)tagtype );
2293 d_printf("%s\n", perms_to_string(permstring, perms));
2300 /****************************************************************************
2302 ****************************************************************************/
2304 static int cmd_stat(void)
2308 SMB_STRUCT_STAT sbuf;
2309 struct cli_state *targetcli;
2312 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2313 d_printf("Server doesn't support UNIX CIFS calls.\n");
2317 pstrcpy(src,cur_dir);
2319 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2320 d_printf("stat file\n");
2327 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2328 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2332 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2333 d_printf("%s stat file %s\n",
2334 cli_errstr(targetcli), src);
2338 /* Print out the stat values. */
2339 d_printf("File: %s\n", src);
2340 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2341 (double)sbuf.st_size,
2342 (unsigned int)sbuf.st_blocks,
2343 filetype_to_str(sbuf.st_mode));
2345 #if defined(S_ISCHR) && defined(S_ISBLK)
2346 if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
2347 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2348 (double)sbuf.st_ino,
2349 (unsigned int)sbuf.st_nlink,
2350 unix_dev_major(sbuf.st_rdev),
2351 unix_dev_minor(sbuf.st_rdev));
2354 d_printf("Inode: %.0f\tLinks: %u\n",
2355 (double)sbuf.st_ino,
2356 (unsigned int)sbuf.st_nlink);
2358 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2359 ((int)sbuf.st_mode & 0777),
2360 unix_mode_to_str(mode_str, sbuf.st_mode),
2361 (unsigned int)sbuf.st_uid,
2362 (unsigned int)sbuf.st_gid);
2364 strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_atime));
2365 d_printf("Access: %s\n", mode_str);
2367 strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_mtime));
2368 d_printf("Modify: %s\n", mode_str);
2370 strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_ctime));
2371 d_printf("Change: %s\n", mode_str);
2377 /****************************************************************************
2379 ****************************************************************************/
2381 static int cmd_chown(void)
2386 pstring buf, buf2, buf3;
2387 struct cli_state *targetcli;
2390 pstrcpy(src,cur_dir);
2392 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2393 !next_token_nr(NULL,buf2,NULL, sizeof(buf2)) ||
2394 !next_token_nr(NULL,buf3,NULL, sizeof(buf3))) {
2395 d_printf("chown uid gid file\n");
2399 uid = (uid_t)atoi(buf);
2400 gid = (gid_t)atoi(buf2);
2403 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2404 d_printf("chown %s: %s\n", src, cli_errstr(cli));
2409 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2410 d_printf("Server doesn't support UNIX CIFS calls.\n");
2414 if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
2415 d_printf("%s chown file %s uid=%d, gid=%d\n",
2416 cli_errstr(targetcli), src, (int)uid, (int)gid);
2423 /****************************************************************************
2425 ****************************************************************************/
2427 static int cmd_rename(void)
2431 pstring oname, nname;
2433 pstrcpy(src, "smb:");
2434 pstrcat(src, service);
2435 pstrcat(src, cur_dir);
2438 if (!next_token_nr(NULL,oname,NULL,sizeof(oname)) ||
2439 !next_token_nr(NULL,nname,NULL, sizeof(nname))) {
2440 d_printf("rename <src> <dest>\n");
2444 pstrcat(src, oname);
2445 pstrcat(dest, nname);
2447 DEBUG(4, ("O: %s\nN: %s\n", src, dest));
2449 err = smbc_rename(src, dest);
2452 d_printf("%s renaming files\n", strerror(errno));
2459 /****************************************************************************
2460 Print the volume name.
2461 ****************************************************************************/
2463 static int cmd_volume(void)
2469 if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
2470 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
2474 d_printf("Volume: |%s| serial number 0x%x\n", volname, (unsigned int)serial_num);
2478 /****************************************************************************
2479 Hard link files using the NT call.
2480 ****************************************************************************/
2482 static int cmd_hardlink(void)
2486 struct cli_state *targetcli;
2489 pstrcpy(src,cur_dir);
2490 pstrcpy(dest,cur_dir);
2492 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2493 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2494 d_printf("hardlink <src> <dest>\n");
2501 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2502 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
2506 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2507 d_printf("Server doesn't support UNIX CIFS calls.\n");
2511 if (!cli_nt_hardlink(targetcli, targetname, dest)) {
2512 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
2519 /****************************************************************************
2520 Toggle the prompt flag.
2521 ****************************************************************************/
2523 static int cmd_prompt(void)
2526 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2531 /****************************************************************************
2532 Set the newer than time.
2533 ****************************************************************************/
2535 static int cmd_newer(void)
2539 SMB_STRUCT_STAT sbuf;
2541 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2542 if (ok && (sys_stat(buf,&sbuf) == 0)) {
2543 newer_than = sbuf.st_mtime;
2544 DEBUG(1,("Getting files newer than %s",
2545 asctime(localtime(&newer_than))));
2550 if (ok && newer_than == 0) {
2551 d_printf("Error setting newer-than time\n");
2558 /****************************************************************************
2559 Set the archive level.
2560 ****************************************************************************/
2562 static int cmd_archive(void)
2566 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2567 archive_level = atoi(buf);
2569 d_printf("Archive level is %d\n",archive_level);
2574 /****************************************************************************
2575 Toggle the lowercaseflag.
2576 ****************************************************************************/
2578 static int cmd_lowercase(void)
2580 lowercase = !lowercase;
2581 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2586 /****************************************************************************
2587 Toggle the case sensitive flag.
2588 ****************************************************************************/
2590 static int cmd_setcase(void)
2592 BOOL orig_case_sensitive = cli_set_case_sensitive(cli, False);
2594 cli_set_case_sensitive(cli, !orig_case_sensitive);
2595 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
2601 /****************************************************************************
2602 Toggle the recurse flag.
2603 ****************************************************************************/
2605 static int cmd_recurse(void)
2608 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2613 /****************************************************************************
2614 Toggle the translate flag.
2615 ****************************************************************************/
2617 static int cmd_translate(void)
2619 translation = !translation;
2620 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2621 translation?"on":"off"));
2626 /****************************************************************************
2628 ****************************************************************************/
2630 static int cmd_lcd(void)
2635 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2637 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2642 /****************************************************************************
2643 Get a file restarting at end of local file.
2644 ****************************************************************************/
2646 static int cmd_reget(void)
2649 pstring remote_name;
2652 pstrcpy(remote_name, cur_dir);
2653 pstrcat(remote_name, "/");
2655 p = remote_name + strlen(remote_name);
2657 if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2658 d_printf("reget <filename>\n");
2661 pstrcpy(local_name, p);
2662 dos_clean_name(remote_name);
2664 next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2666 return do_get(remote_name, local_name, True);
2669 /****************************************************************************
2670 Put a file restarting at end of local file.
2671 ****************************************************************************/
2673 static int cmd_reput(void)
2676 pstring remote_name;
2681 pstrcpy(remote_name, cur_dir);
2682 pstrcat(remote_name, "/");
2684 if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2685 d_printf("reput <filename>\n");
2688 pstrcpy(local_name, p);
2690 if (!file_exist(local_name, &st)) {
2691 d_printf("%s does not exist\n", local_name);
2695 if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2696 pstrcat(remote_name, p);
2698 pstrcat(remote_name, local_name);
2700 dos_clean_name(remote_name);
2702 return do_put(remote_name, local_name, True);
2705 /****************************************************************************
2707 ****************************************************************************/
2709 static void browse_fn(const char *name, uint32 m,
2710 const char *comment, void *state)
2718 case STYPE_DISKTREE:
2719 fstrcpy(typestr,"Disk"); break;
2721 fstrcpy(typestr,"Printer"); break;
2723 fstrcpy(typestr,"Device"); break;
2725 fstrcpy(typestr,"IPC"); break;
2727 /* FIXME: If the remote machine returns non-ascii characters
2728 in any of these fields, they can corrupt the output. We
2729 should remove them. */
2731 d_printf("\t%-15s %-10.10s%s\n",
2732 name,typestr,comment);
2734 d_printf ("%s|%s|%s\n",typestr,name,comment);
2738 /****************************************************************************
2739 Try and browse available connections on a host.
2740 ****************************************************************************/
2742 static BOOL browse_host(BOOL sort)
2746 d_printf("\n\tSharename Type Comment\n");
2747 d_printf("\t--------- ---- -------\n");
2750 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
2751 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
2756 /****************************************************************************
2758 ****************************************************************************/
2760 static void server_fn(const char *name, uint32 m,
2761 const char *comment, void *state)
2765 d_printf("\t%-16s %s\n", name, comment);
2767 d_printf("%s|%s|%s\n",(char *)state, name, comment);
2771 /****************************************************************************
2772 Try and browse available connections on a host.
2773 ****************************************************************************/
2775 static BOOL list_servers(const char *wk_grp)
2779 if (!cli->server_domain)
2783 d_printf("\n\tServer Comment\n");
2784 d_printf("\t--------- -------\n");
2786 fstrcpy( state, "Server" );
2787 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
2791 d_printf("\n\tWorkgroup Master\n");
2792 d_printf("\t--------- -------\n");
2795 fstrcpy( state, "Workgroup" );
2796 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
2801 /****************************************************************************
2802 Print or set current VUID
2803 ****************************************************************************/
2805 static int cmd_vuid(void)
2809 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2810 d_printf("Current VUID is %d\n", cli->vuid);
2814 cli->vuid = atoi(buf);
2818 /****************************************************************************
2819 Setup a new VUID, by issuing a session setup
2820 ****************************************************************************/
2822 static int cmd_logon(void)
2824 pstring l_username, l_password;
2827 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2828 d_printf("logon <username> [<password>]\n");
2832 pstrcpy(l_username, buf);
2834 if (!next_token_nr(NULL,buf2,NULL,sizeof(buf)))
2836 char *pass = getpass("Password: ");
2838 pstrcpy(l_password, pass);
2841 pstrcpy(l_password, buf2);
2843 if (!cli_session_setup(cli, l_username,
2844 l_password, strlen(l_password),
2845 l_password, strlen(l_password),
2847 d_printf("session setup failed: %s\n", cli_errstr(cli));
2851 d_printf("Current VUID is %d\n", cli->vuid);
2856 /****************************************************************************
2857 list active connections
2858 ****************************************************************************/
2860 static int cmd_list_connect(void)
2867 /****************************************************************************
2868 display the current active client connection
2869 ****************************************************************************/
2871 static int cmd_show_connect( void )
2873 struct cli_state *targetcli;
2876 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
2877 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
2881 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
2885 /* Some constants for completing filename arguments */
2887 #define COMPL_NONE 0 /* No completions */
2888 #define COMPL_REMOTE 1 /* Complete remote filename */
2889 #define COMPL_LOCAL 2 /* Complete local filename */
2891 /* This defines the commands supported by this client.
2892 * NOTE: The "!" must be the last one in the list because it's fn pointer
2893 * field is NULL, and NULL in that field is used in process_tok()
2894 * (below) to indicate the end of the list. crh
2900 const char *description;
2901 char compl_args[2]; /* Completion argument info */
2903 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2904 /* {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}}, */
2905 {"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}},
2906 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
2907 /* {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}}, */
2908 /* {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}}, */
2909 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2910 /* {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}}, */
2911 /* {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}}, */
2912 /* {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}}, */
2913 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2914 /* {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}}, */
2915 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2916 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2917 /* {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}}, */
2918 /* {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}}, */
2919 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2920 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2921 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2922 /* {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}}, */
2923 /* {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}}, */
2924 /* {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}}, */
2925 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
2926 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2927 /* {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}}, */
2928 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2929 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2930 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2931 /* {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}}, */
2932 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2933 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2934 /* {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}}, */
2935 /* {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}}, */
2936 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
2937 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2938 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2939 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2940 /* {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}}, */
2941 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2942 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2943 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
2944 /* {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},*/
2945 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2946 /* {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},*/
2947 /* {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},*/
2948 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2949 /* {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},*/
2950 /* {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},*/
2951 /* {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},*/
2952 /* {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},*/
2953 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
2954 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
2955 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2956 /* {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}}, */
2957 /* {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}}, */
2959 /* Yes, this must be here, see crh's comment above. */
2960 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2961 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2964 /*******************************************************************
2965 Lookup a command string in the list of commands, including
2967 ******************************************************************/
2969 static int process_tok(pstring tok)
2971 int i = 0, matches = 0;
2973 int tok_len = strlen(tok);
2975 while ((commands[i].fn != NULL) || (strequal(commands[i].name, "!"))) {
2976 if (strequal(commands[i].name,tok)) {
2980 } else if (strnequal(commands[i].name, tok, tok_len)) {
2989 else if (matches == 1)
2995 /****************************************************************************
2997 ****************************************************************************/
2999 static int cmd_help(void)
3004 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3005 if ((i = process_tok(buf)) >= 0)
3006 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
3008 while (commands[i].description) {
3009 for (j=0; commands[i].description && (j<5); j++) {
3010 d_printf("%-15s",commands[i].name);
3019 /****************************************************************************
3020 Process a -c command string.
3021 ****************************************************************************/
3023 static int process_command_string(char *cmd)
3029 /* establish the connection if not already */
3032 cli = cli_cm_open(desthost, service, True);
3037 while (cmd[0] != '\0') {
3042 if ((p = strchr_m(cmd, ';')) == 0) {
3043 strncpy(line, cmd, 999);
3049 strncpy(line, cmd, p - cmd);
3050 line[p - cmd] = '\0';
3054 /* and get the first part of the command */
3056 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3058 if ((i = process_tok(tok)) >= 0) {
3059 rc = commands[i].fn();
3060 } else if (i == -2) {
3061 d_printf("%s: command abbreviation ambiguous\n",tok);
3063 d_printf("%s: command not found\n",tok);
3070 #define MAX_COMPLETIONS 100
3078 } completion_remote_t;
3080 static void completion_remote_filter(const char *mnt, file_info *f, const char *mask, void *state)
3082 completion_remote_t *info = (completion_remote_t *)state;
3084 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
3085 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
3086 info->matches[info->count] = SMB_STRDUP(f->name);
3090 if (info->dirmask[0] != 0)
3091 pstrcpy(tmp, info->dirmask);
3094 pstrcat(tmp, f->name);
3097 info->matches[info->count] = SMB_STRDUP(tmp);
3099 if (info->matches[info->count] == NULL)
3102 smb_readline_ca_char(0);
3104 if (info->count == 1)
3105 info->samelen = strlen(info->matches[info->count]);
3107 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
3113 static char **remote_completion(const char *text, int len)
3117 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
3119 /* can't have non-static intialisation on Sun CC, so do it
3125 if (len >= PATH_MAX)
3128 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
3129 if (!info.matches) return NULL;
3130 info.matches[0] = NULL;
3132 for (i = len-1; i >= 0; i--)
3133 if ((text[i] == '/') || (text[i] == '\\'))
3135 info.text = text+i+1;
3136 info.samelen = info.len = len-i-1;
3139 strncpy(info.dirmask, text, i+1);
3140 info.dirmask[i+1] = 0;
3141 pstr_sprintf(dirmask, "%s%*s*", cur_dir, i-1, text);
3143 pstr_sprintf(dirmask, "%s*", cur_dir);
3145 if (cli_list(cli, dirmask, aDIR | aSYSTEM | aHIDDEN, completion_remote_filter, &info) < 0)
3148 if (info.count == 2)
3149 info.matches[0] = SMB_STRDUP(info.matches[1]);
3151 info.matches[0] = SMB_MALLOC(info.samelen+1);
3152 if (!info.matches[0])
3154 strncpy(info.matches[0], info.matches[1], info.samelen);
3155 info.matches[0][info.samelen] = 0;
3157 info.matches[info.count] = NULL;
3158 return info.matches;
3161 for (i = 0; i < info.count; i++)
3162 free(info.matches[i]);
3167 static char **completion_fn(const char *text, int start, int end)
3169 smb_readline_ca_char(' ');
3172 const char *buf, *sp;
3176 buf = smb_readline_get_line_buffer();
3180 sp = strchr(buf, ' ');
3184 for (i = 0; commands[i].name; i++)
3185 if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
3187 if (commands[i].name == NULL)
3193 if (sp == (buf + start))
3194 compl_type = commands[i].compl_args[0];
3196 compl_type = commands[i].compl_args[1];
3198 if (compl_type == COMPL_REMOTE)
3199 return remote_completion(text, end - start);
3200 else /* fall back to local filename completion */
3204 int i, len, samelen = 0, count=1;
3206 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
3213 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3214 if (strncmp(text, commands[i].name, len) == 0) {
3215 matches[count] = SMB_STRDUP(commands[i].name);
3216 if (!matches[count])
3219 samelen = strlen(matches[count]);
3221 while (strncmp(matches[count], matches[count-1], samelen) != 0)
3228 case 0: /* should never happen */
3232 matches[0] = SMB_STRDUP(matches[1]);
3235 matches[0] = SMB_MALLOC(samelen+1);
3238 strncpy(matches[0], matches[1], samelen);
3239 matches[0][samelen] = 0;
3241 matches[count] = NULL;
3245 for (i = 0; i < count; i++)
3253 /****************************************************************************
3254 Make sure we swallow keepalives during idle time.
3255 ****************************************************************************/
3257 static void readline_callback(void)
3260 struct timeval timeout;
3261 static time_t last_t;
3277 FD_SET(cli->fd,&fds);
3280 timeout.tv_usec = 0;
3281 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
3283 /* We deliberately use receive_smb instead of
3284 client_receive_smb as we want to receive
3285 session keepalives and then drop them here.
3287 if (FD_ISSET(cli->fd,&fds)) {
3288 receive_smb(cli->fd,cli->inbuf,0);
3292 cli_chkpath(cli, "/");
3295 /****************************************************************************
3296 Process commands on stdin.
3297 ****************************************************************************/
3299 static int process_stdin(void)
3311 /* display a prompt */
3312 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3313 /* Removed callback since we don't need to swallow keepalives with libsmbclient */
3314 /*cline = smb_readline(the_prompt, readline_callback, completion_fn);*/
3315 cline = smb_readline(the_prompt, NULL, completion_fn);
3319 pstrcpy(line, cline);
3321 /* special case - first char is ! */
3327 /* and get the first part of the command */
3329 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3331 if ((i = process_tok(tok)) >= 0) {
3332 rc = commands[i].fn();
3333 } else if (i == -2) {
3334 d_printf("%s: command abbreviation ambiguous\n",tok);
3336 d_printf("%s: command not found\n",tok);
3342 /****************************************************************************
3343 Process commands from the client.
3344 ****************************************************************************/
3346 static int process(char *base_directory)
3350 /*cli = cli_cm_open(desthost, service, True);
3354 rc = smbc_init(get_auth_data_fn, 0); /* Initialize libsmbclient */
3357 d_printf("Error initializing libsmbclient: %s\n", strerror(errno));
3361 if (*base_directory) {
3362 rc = do_cd(base_directory);
3368 else /* start the auth fn*/
3376 rc = process_command_string(cmdstr);
3385 /****************************************************************************
3387 ****************************************************************************/
3389 static int do_host_query(char *query_host)
3391 cli = cli_cm_open(query_host, "IPC$", True);
3399 /* Workgroups simply don't make sense over anything
3400 else but port 139... */
3403 cli_cm_set_port( 139 );
3404 cli = cli_cm_open(query_host, "IPC$", True);
3408 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3412 list_servers(lp_workgroup());
3419 /****************************************************************************
3420 Handle a tar operation.
3421 ****************************************************************************/
3423 static int do_tar_op(char *base_directory)
3427 /* do we already have a connection? */
3429 cli = cli_cm_open(desthost, service, True);
3436 if (*base_directory) {
3437 ret = do_cd(base_directory);
3451 /****************************************************************************
3452 Handle a message operation.
3453 ****************************************************************************/
3455 static int do_message_op(void)
3458 struct nmb_name called, calling;
3459 fstring server_name;
3460 char name_type_hex[10];
3463 make_nmb_name(&calling, calling_name, 0x0);
3464 make_nmb_name(&called , desthost, name_type);
3466 fstrcpy(server_name, desthost);
3467 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
3468 fstrcat(server_name, name_type_hex);
3474 /* we can only do messages over port 139 (to windows clients at least) */
3476 msg_port = port ? port : 139;
3478 if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, msg_port) != msg_port) ||
3479 !cli_connect(cli, server_name, &ip)) {
3480 d_printf("Connection to %s failed\n", desthost);
3484 if (!cli_session_request(cli, &calling, &called)) {
3485 d_printf("session request failed\n");
3497 /****************************************************************************
3499 ****************************************************************************/
3501 int main(int argc,char *argv[])
3503 pstring base_directory;
3506 BOOL message = False;
3508 static const char *new_name_resolve_order = NULL;
3512 fstring new_workgroup;
3513 struct poptOption long_options[] = {
3516 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3517 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3518 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3519 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3520 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3521 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3522 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
3523 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3524 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3525 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
3526 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
3527 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3528 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
3530 POPT_COMMON_CONNECTION
3531 POPT_COMMON_CREDENTIALS
3537 pstrcpy(term_code, KANJI);
3543 *base_directory = 0;
3545 /* initialize the workgroup name so we can determine whether or
3546 not it was set by a command line option */
3548 set_global_myworkgroup( "" );
3549 set_global_myname( "" );
3551 /* set default debug level to 0 regardless of what smb.conf sets */
3552 setup_logging( "smbclient", True );
3553 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
3555 x_setbuf( x_stderr, NULL );
3557 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options,
3558 POPT_CONTEXT_KEEP_FIRST);
3559 poptSetOtherOptionHelp(pc, "service <password>");
3561 in_client = True; /* Make sure that we tell lp_load we are */
3563 while ((opt = poptGetNextOpt(pc)) != -1) {
3566 /* Messages are sent to NetBIOS name type 0x3
3567 * (Messenger Service). Make sure we default
3568 * to port 139 instead of port 445. srl,crh
3571 cli_cm_set_dest_name_type( name_type );
3572 pstrcpy(desthost,poptGetOptArg(pc));
3574 cli_cm_set_port( 139 );
3579 dest_ip = *interpret_addr2(poptGetOptArg(pc));
3580 if (is_zero_ip(dest_ip))
3584 cli_cm_set_dest_ip( dest_ip );
3589 display_set_stderr();
3593 pstrcpy(query_host, poptGetOptArg(pc));
3596 pstrcpy(term_code, poptGetOptArg(pc));
3599 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
3602 /* We must use old option processing for this. Find the
3603 * position of the -T option in the raw argv[]. */
3606 for (i = 1; i < argc; i++) {
3607 if (strncmp("-T", argv[i],2)==0)
3611 if (!(optnum = tar_parseargs(argc, argv, poptGetOptArg(pc), i))) {
3612 poptPrintUsage(pc, stderr, 0);
3615 /* Now we must eat (optnum - i) options - they have
3616 * been processed by tar_parseargs().
3619 for (i = 0; i < optnum; i++)
3624 pstrcpy(base_directory,poptGetOptArg(pc));
3634 /* check for the -P option */
3637 cli_cm_set_port( port );
3640 * Don't load debug level from smb.conf. It should be
3641 * set by cmdline arg or remain default (0)
3643 AllowDebugChange = False;
3645 /* save the workgroup...
3647 FIXME!! do we need to do this for other options as well
3648 (or maybe a generic way to keep lp_load() from overwriting
3651 fstrcpy( new_workgroup, lp_workgroup() );
3652 pstrcpy( calling_name, global_myname() );
3654 if ( override_logfile )
3655 setup_logging( lp_logfile(), False );
3657 if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
3658 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
3659 argv[0], dyn_CONFIGFILE);
3664 if ( strlen(new_workgroup) != 0 )
3665 set_global_myworkgroup( new_workgroup );
3666 pstrcpy(workgroup, lp_workgroup());
3668 if ( strlen(calling_name) != 0 )
3669 set_global_myname( calling_name );
3671 pstrcpy( calling_name, global_myname() );
3673 if(poptPeekArg(pc)) {
3674 pstrcpy(service,poptGetArg(pc));
3675 /* Convert any '\' characters in the service name to '/' characters */
3676 string_replace(service, '\\','/');
3678 if (count_chars(service,'/') < 3) {
3679 d_printf("\n%s: Not enough '/' characters in service\n",service);
3680 poptPrintUsage(pc, stderr, 0);
3685 if (poptPeekArg(pc) && !cmdline_auth_info.got_pass) {
3686 cmdline_auth_info.got_pass = True;
3687 pstrcpy(cmdline_auth_info.password,poptGetArg(pc));
3692 if(new_name_resolve_order)
3693 lp_set_name_resolve_order(new_name_resolve_order);
3695 if (!tar_type && !*query_host && !*service && !message) {
3696 poptPrintUsage(pc, stderr, 0);
3700 poptFreeContext(pc);
3702 /* store the username an password for dfs support */
3704 cli_cm_set_credentials( &cmdline_auth_info );
3705 pstrcpy(username, cmdline_auth_info.username);
3707 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
3711 process_command_string(cmdstr);
3712 return do_tar_op(base_directory);
3716 char *qhost = query_host;
3719 while (*qhost == '\\' || *qhost == '/')
3722 if ((slash = strchr_m(qhost, '/'))
3723 || (slash = strchr_m(qhost, '\\'))) {
3727 if ((p=strchr_m(qhost, '#'))) {
3730 sscanf(p, "%x", &name_type);
3731 cli_cm_set_dest_name_type( name_type );
3734 return do_host_query(qhost);
3738 return do_message_op();
3741 if (process(base_directory)) {