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"
30 extern BOOL AllowDebugChange;
31 extern BOOL override_logfile;
33 extern BOOL in_client;
35 pstring cur_dir = "\\";
36 static pstring cd_path = "";
37 static pstring service;
38 static pstring desthost;
39 static pstring username;
40 static pstring calling_name;
41 static BOOL grepable=False;
42 static char *cmdstr = NULL;
44 static int io_bufsize = 64512;
46 static int name_type = 0x20;
47 extern int max_protocol;
49 static int process_tok(pstring tok);
50 static int cmd_help(void);
52 /* 30 second timeout on most commands */
53 #define CLIENT_TIMEOUT (30*1000)
54 #define SHORT_TIMEOUT (5*1000)
56 /* value for unused fid field in trans2 secondary request */
57 #define FID_UNUSED (0xFFFF)
59 time_t newer_than = 0;
60 static int archive_level = 0;
62 static BOOL translation = False;
65 /* clitar bits insert */
68 extern BOOL tar_reset;
72 static BOOL prompt = True;
74 static BOOL recurse = False;
75 BOOL lowercase = False;
77 static struct in_addr dest_ip;
79 #define SEPARATORS " \t\n\r"
81 static BOOL abort_mget = True;
83 static pstring fileselection = "";
85 extern file_info def_finfo;
88 SMB_BIG_UINT get_total_size = 0;
89 unsigned int get_total_time_ms = 0;
90 static SMB_BIG_UINT put_total_size = 0;
91 static unsigned int put_total_time_ms = 0;
94 static double dir_total;
96 /* root cli_state connection */
98 struct cli_state *cli;
102 /****************************************************************************
103 Write to a local file with CR/LF->LF translation if appropriate. Return the
104 number taken from the buffer. This may not equal the number written.
105 ****************************************************************************/
107 static int writefile(int f, char *b, int n)
117 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
120 if (write(f, b, 1) != 1) {
130 /****************************************************************************
131 Read from a file with LF->CR/LF translation if appropriate. Return the
132 number read. read approx n bytes.
133 ****************************************************************************/
135 static int readfile(char *b, int n, XFILE *f)
141 return x_fread(b,1,n,f);
144 while (i < (n - 1) && (i < BUFFER_SIZE)) {
145 if ((c = x_getc(f)) == EOF) {
149 if (c == '\n') { /* change all LFs to CR/LF */
159 /****************************************************************************
161 ****************************************************************************/
163 static void send_message(void)
168 if (!cli_message_start(cli, desthost, username, &grp_id)) {
169 d_printf("message start: %s\n", cli_errstr(cli));
174 d_printf("Connected. Type your message, ending it with a Control-D\n");
176 while (!feof(stdin) && total_len < 1600) {
177 int maxlen = MIN(1600 - total_len,127);
184 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
190 if (!cli_message_text(cli, msg, l, grp_id)) {
191 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
198 if (total_len >= 1600)
199 d_printf("the message was truncated to 1600 bytes\n");
201 d_printf("sent %d bytes\n",total_len);
203 if (!cli_message_end(cli, grp_id)) {
204 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
209 /****************************************************************************
210 Check the space on a device.
211 ****************************************************************************/
213 static int do_dskattr(void)
215 int total, bsize, avail;
216 struct cli_state *targetcli;
219 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
220 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
224 if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
225 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli));
229 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
230 total, bsize, avail);
235 /****************************************************************************
237 ****************************************************************************/
239 static int cmd_pwd(void)
241 d_printf("Current directory is %s",service);
242 d_printf("%s\n",cur_dir);
246 /****************************************************************************
247 Change directory - inner section.
248 ****************************************************************************/
250 static int do_cd(char *newdir)
256 struct cli_state *targetcli;
257 SMB_STRUCT_STAT sbuf;
263 /* Save the current directory in case the new directory is invalid */
265 pstrcpy(saved_dir, cur_dir);
272 if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
273 pstrcat(cur_dir, "\\");
276 dos_clean_name(cur_dir);
277 pstrcpy( dname, cur_dir );
278 pstrcat(cur_dir,"\\");
279 dos_clean_name(cur_dir);
281 if ( !cli_resolve_path( "", cli, dname, &targetcli, targetpath ) ) {
282 d_printf("cd %s: %s\n", dname, cli_errstr(cli));
283 pstrcpy(cur_dir,saved_dir);
288 if ( strequal(targetpath,"\\" ) )
291 /* Use a trans2_qpathinfo to test directories for modern servers.
292 Except Win9x doesn't support the qpathinfo_basic() call..... */
294 if ( targetcli->protocol > PROTOCOL_LANMAN2 && !targetcli->win95 ) {
295 if ( !cli_qpathinfo_basic( targetcli, targetpath, &sbuf, &attributes ) ) {
296 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
297 pstrcpy(cur_dir,saved_dir);
301 if ( !(attributes&FILE_ATTRIBUTE_DIRECTORY) ) {
302 d_printf("cd %s: not a directory\n", dname);
303 pstrcpy(cur_dir,saved_dir);
307 pstrcat( targetpath, "\\" );
308 dos_clean_name( targetpath );
310 if ( !cli_chkpath(targetcli, targetpath) ) {
311 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
312 pstrcpy(cur_dir,saved_dir);
321 pstrcpy(cd_path,cur_dir);
325 /****************************************************************************
327 ****************************************************************************/
329 static int cmd_cd(void)
334 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
337 d_printf("Current directory is %s\n",cur_dir);
342 /*******************************************************************
343 Decide if a file should be operated on.
344 ********************************************************************/
346 static BOOL do_this_one(file_info *finfo)
348 if (finfo->mode & aDIR)
351 if (*fileselection &&
352 !mask_match(finfo->name,fileselection,False)) {
353 DEBUG(3,("mask_match %s failed\n", finfo->name));
357 if (newer_than && finfo->mtime < newer_than) {
358 DEBUG(3,("newer_than %s failed\n", finfo->name));
362 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
363 DEBUG(3,("archive %s failed\n", finfo->name));
370 /****************************************************************************
371 Display info about a file.
372 ****************************************************************************/
374 static void display_finfo(file_info *finfo)
376 if (do_this_one(finfo)) {
377 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
378 d_printf(" %-30s%7.7s %8.0f %s",
380 attrib_string(finfo->mode),
382 asctime(localtime(&t)));
383 dir_total += finfo->size;
387 /****************************************************************************
388 Accumulate size of a file.
389 ****************************************************************************/
391 static void do_du(file_info *finfo)
393 if (do_this_one(finfo)) {
394 dir_total += finfo->size;
398 static BOOL do_list_recurse;
399 static BOOL do_list_dirs;
400 static char *do_list_queue = 0;
401 static long do_list_queue_size = 0;
402 static long do_list_queue_start = 0;
403 static long do_list_queue_end = 0;
404 static void (*do_list_fn)(file_info *);
406 /****************************************************************************
407 Functions for do_list_queue.
408 ****************************************************************************/
411 * The do_list_queue is a NUL-separated list of strings stored in a
412 * char*. Since this is a FIFO, we keep track of the beginning and
413 * ending locations of the data in the queue. When we overflow, we
414 * double the size of the char*. When the start of the data passes
415 * the midpoint, we move everything back. This is logically more
416 * complex than a linked list, but easier from a memory management
417 * angle. In any memory error condition, do_list_queue is reset.
418 * Functions check to ensure that do_list_queue is non-NULL before
422 static void reset_do_list_queue(void)
424 SAFE_FREE(do_list_queue);
425 do_list_queue_size = 0;
426 do_list_queue_start = 0;
427 do_list_queue_end = 0;
430 static void init_do_list_queue(void)
432 reset_do_list_queue();
433 do_list_queue_size = 1024;
434 do_list_queue = SMB_MALLOC(do_list_queue_size);
435 if (do_list_queue == 0) {
436 d_printf("malloc fail for size %d\n",
437 (int)do_list_queue_size);
438 reset_do_list_queue();
440 memset(do_list_queue, 0, do_list_queue_size);
444 static void adjust_do_list_queue(void)
447 * If the starting point of the queue is more than half way through,
448 * move everything toward the beginning.
450 if (do_list_queue && (do_list_queue_start == do_list_queue_end)) {
451 DEBUG(4,("do_list_queue is empty\n"));
452 do_list_queue_start = do_list_queue_end = 0;
453 *do_list_queue = '\0';
454 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
455 DEBUG(4,("sliding do_list_queue backward\n"));
456 memmove(do_list_queue,
457 do_list_queue + do_list_queue_start,
458 do_list_queue_end - do_list_queue_start);
459 do_list_queue_end -= do_list_queue_start;
460 do_list_queue_start = 0;
464 static void add_to_do_list_queue(const char* entry)
467 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
468 while (new_end > do_list_queue_size) {
469 do_list_queue_size *= 2;
470 DEBUG(4,("enlarging do_list_queue to %d\n",
471 (int)do_list_queue_size));
472 dlq = SMB_REALLOC(do_list_queue, do_list_queue_size);
474 d_printf("failure enlarging do_list_queue to %d bytes\n",
475 (int)do_list_queue_size);
476 reset_do_list_queue();
479 memset(do_list_queue + do_list_queue_size / 2,
480 0, do_list_queue_size / 2);
484 safe_strcpy_base(do_list_queue + do_list_queue_end,
485 entry, do_list_queue, do_list_queue_size);
486 do_list_queue_end = new_end;
487 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
488 entry, (int)do_list_queue_start, (int)do_list_queue_end));
492 static char *do_list_queue_head(void)
494 return do_list_queue + do_list_queue_start;
497 static void remove_do_list_queue_head(void)
499 if (do_list_queue_end > do_list_queue_start) {
500 do_list_queue_start += strlen(do_list_queue_head()) + 1;
501 adjust_do_list_queue();
502 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
503 (int)do_list_queue_start, (int)do_list_queue_end));
507 static int do_list_queue_empty(void)
509 return (! (do_list_queue && *do_list_queue));
512 /****************************************************************************
513 A helper for do_list.
514 ****************************************************************************/
516 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
520 /* save the directory */
521 pstrcpy( f->dir, mask );
522 if ( (dir_end = strrchr( f->dir, '\\' )) != NULL ) {
526 if (f->mode & aDIR) {
527 if (do_list_dirs && do_this_one(f)) {
530 if (do_list_recurse &&
531 !strequal(f->name,".") &&
532 !strequal(f->name,"..")) {
537 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
541 pstrcpy(mask2, mntpoint);
542 pstrcat(mask2, mask);
543 p = strrchr_m(mask2,'\\');
547 pstrcat(mask2, f->name);
548 pstrcat(mask2,"\\*");
549 add_to_do_list_queue(mask2);
554 if (do_this_one(f)) {
559 /****************************************************************************
560 A wrapper around cli_list that adds recursion.
561 ****************************************************************************/
563 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
565 static int in_do_list = 0;
566 struct cli_state *targetcli;
569 if (in_do_list && rec) {
570 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
576 do_list_recurse = rec;
581 init_do_list_queue();
582 add_to_do_list_queue(mask);
584 while (! do_list_queue_empty()) {
586 * Need to copy head so that it doesn't become
587 * invalid inside the call to cli_list. This
588 * would happen if the list were expanded
590 * Fix from E. Jay Berkenbilt (ejb@ql.org)
593 pstrcpy(head, do_list_queue_head());
597 if ( !cli_resolve_path( "", cli, head, &targetcli, targetpath ) ) {
598 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
599 remove_do_list_queue_head();
603 cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
604 remove_do_list_queue_head();
605 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
606 char* next_file = do_list_queue_head();
608 if ((strlen(next_file) >= 2) &&
609 (next_file[strlen(next_file) - 1] == '*') &&
610 (next_file[strlen(next_file) - 2] == '\\')) {
611 save_ch = next_file +
612 strlen(next_file) - 2;
615 d_printf("\n%s\n",next_file);
624 if ( cli_resolve_path( "", cli, mask, &targetcli, targetpath ) ) {
625 if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1)
626 d_printf("%s listing %s\n", cli_errstr(targetcli), targetpath);
629 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
634 reset_do_list_queue();
637 /****************************************************************************
638 Get a directory listing.
639 ****************************************************************************/
641 static int cmd_dir(void)
643 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
650 if (strcmp(cur_dir, "\\") != 0) {
651 pstrcpy(mask,cur_dir);
652 if(mask[strlen(mask)-1]!='\\')
658 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
668 do_list(mask, attribute, display_finfo, recurse, True);
672 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
677 /****************************************************************************
678 Get a directory listing.
679 ****************************************************************************/
681 static int cmd_du(void)
683 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
690 pstrcpy(mask,cur_dir);
691 if(mask[strlen(mask)-1]!='\\')
694 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
704 do_list(mask, attribute, do_du, recurse, True);
708 d_printf("Total number of bytes: %.0f\n", dir_total);
713 /****************************************************************************
714 Get a file from rname to lname
715 ****************************************************************************/
717 static int do_get(char *rname, char *lname, BOOL reget)
719 int handle = 0, fnum;
720 BOOL newhandle = False;
722 struct timeval tp_start;
723 int read_size = io_bufsize;
729 struct cli_state *targetcli;
737 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
738 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
742 GetTimeOfDay(&tp_start);
744 if ( targetcli->dfsroot ) {
747 /* we need to refer to the full \server\share\path format
750 pstrcpy( path, targetname );
751 cli_dfs_make_full_path( targetname, targetcli->desthost,
752 targetcli->share, path);
755 fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
758 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
762 if(!strcmp(lname,"-")) {
763 handle = fileno(stdout);
766 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
768 start = sys_lseek(handle, 0, SEEK_END);
770 d_printf("Error seeking local file\n");
775 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
780 d_printf("Error opening local file %s\n",lname);
785 if (!cli_qfileinfo(targetcli, fnum,
786 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
787 !cli_getattrE(targetcli, fnum,
788 &attr, &size, NULL, NULL, NULL)) {
789 d_printf("getattrib: %s\n",cli_errstr(targetcli));
793 DEBUG(1,("getting file %s of size %.0f as %s ",
794 rname, (double)size, lname));
796 if(!(data = (char *)SMB_MALLOC(read_size))) {
797 d_printf("malloc fail for size %d\n", read_size);
798 cli_close(targetcli, fnum);
803 int n = cli_read(targetcli, fnum, data, nread + start, read_size);
808 if (writefile(handle,data, n) != n) {
809 d_printf("Error writing local file\n");
817 if (nread + start < size) {
818 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
819 rname, (long)nread));
826 if (!cli_close(targetcli, fnum)) {
827 d_printf("Error %s closing remote file\n",cli_errstr(cli));
835 if (archive_level >= 2 && (attr & aARCH)) {
836 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
840 struct timeval tp_end;
843 GetTimeOfDay(&tp_end);
845 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
846 (tp_end.tv_usec - tp_start.tv_usec)/1000;
847 get_total_time_ms += this_time;
848 get_total_size += nread;
850 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
851 nread / (1.024*this_time + 1.0e-4),
852 get_total_size / (1.024*get_total_time_ms)));
858 /****************************************************************************
860 ****************************************************************************/
862 static int cmd_get(void)
868 pstrcpy(rname,cur_dir);
871 p = rname + strlen(rname);
873 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
874 d_printf("get <filename>\n");
878 dos_clean_name(rname);
880 next_token_nr(NULL,lname,NULL,sizeof(lname));
882 return do_get(rname, lname, False);
885 /****************************************************************************
886 Do an mget operation on one file.
887 ****************************************************************************/
889 static void do_mget(file_info *finfo)
893 pstring saved_curdir;
896 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
900 d_printf("mget aborted\n");
904 if (finfo->mode & aDIR)
905 slprintf(quest,sizeof(pstring)-1,
906 "Get directory %s? ",finfo->name);
908 slprintf(quest,sizeof(pstring)-1,
909 "Get file %s? ",finfo->name);
911 if (prompt && !yesno(quest))
914 if (!(finfo->mode & aDIR)) {
915 pstrcpy(rname,cur_dir);
916 pstrcat(rname,finfo->name);
917 do_get(rname, finfo->name, False);
921 /* handle directories */
922 pstrcpy(saved_curdir,cur_dir);
924 pstrcat(cur_dir,finfo->name);
925 pstrcat(cur_dir,"\\");
927 unix_format(finfo->name);
929 strlower_m(finfo->name);
931 if (!directory_exist(finfo->name,NULL) &&
932 mkdir(finfo->name,0777) != 0) {
933 d_printf("failed to create directory %s\n",finfo->name);
934 pstrcpy(cur_dir,saved_curdir);
938 if (chdir(finfo->name) != 0) {
939 d_printf("failed to chdir to directory %s\n",finfo->name);
940 pstrcpy(cur_dir,saved_curdir);
944 pstrcpy(mget_mask,cur_dir);
945 pstrcat(mget_mask,"*");
947 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
949 pstrcpy(cur_dir,saved_curdir);
952 /****************************************************************************
953 View the file using the pager.
954 ****************************************************************************/
956 static int cmd_more(void)
958 pstring rname,lname,pager_cmd;
963 pstrcpy(rname,cur_dir);
966 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
967 fd = smb_mkstemp(lname);
969 d_printf("failed to create temporary file for more\n");
974 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
975 d_printf("more <filename>\n");
979 dos_clean_name(rname);
981 rc = do_get(rname, lname, False);
983 pager=getenv("PAGER");
985 slprintf(pager_cmd,sizeof(pager_cmd)-1,
986 "%s %s",(pager? pager:PAGER), lname);
993 /****************************************************************************
995 ****************************************************************************/
997 static int cmd_mget(void)
999 uint16 attribute = aSYSTEM | aHIDDEN;
1011 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1012 pstrcpy(mget_mask,cur_dir);
1013 if(mget_mask[strlen(mget_mask)-1]!='\\')
1014 pstrcat(mget_mask,"\\");
1017 pstrcpy(mget_mask,p);
1019 pstrcat(mget_mask,p);
1020 do_list(mget_mask, attribute,do_mget,False,True);
1024 pstrcpy(mget_mask,cur_dir);
1025 if(mget_mask[strlen(mget_mask)-1]!='\\')
1026 pstrcat(mget_mask,"\\");
1027 pstrcat(mget_mask,"*");
1028 do_list(mget_mask, attribute,do_mget,False,True);
1034 /****************************************************************************
1035 Make a directory of name "name".
1036 ****************************************************************************/
1038 static BOOL do_mkdir(char *name)
1040 struct cli_state *targetcli;
1043 if ( !cli_resolve_path( "", cli, name, &targetcli, targetname ) ) {
1044 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1048 if (!cli_mkdir(targetcli, targetname)) {
1049 d_printf("%s making remote directory %s\n",
1050 cli_errstr(targetcli),name);
1057 /****************************************************************************
1058 Show 8.3 name of a file.
1059 ****************************************************************************/
1061 static BOOL do_altname(char *name)
1064 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1065 d_printf("%s getting alt name for %s\n",
1066 cli_errstr(cli),name);
1069 d_printf("%s\n", altname);
1074 /****************************************************************************
1076 ****************************************************************************/
1078 static int cmd_quit(void)
1086 /****************************************************************************
1088 ****************************************************************************/
1090 static int cmd_mkdir(void)
1096 pstrcpy(mask,cur_dir);
1098 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1100 d_printf("mkdir <dirname>\n");
1111 trim_char(ddir,'.','\0');
1112 p = strtok(ddir,"/\\");
1115 if (!cli_chkpath(cli, ddir2)) {
1118 pstrcat(ddir2,"\\");
1119 p = strtok(NULL,"/\\");
1128 /****************************************************************************
1130 ****************************************************************************/
1132 static int cmd_altname(void)
1138 pstrcpy(name,cur_dir);
1140 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1141 d_printf("altname <file>\n");
1151 /****************************************************************************
1153 ****************************************************************************/
1155 static int do_put(char *rname, char *lname, BOOL reput)
1159 SMB_OFF_T start = 0;
1162 int maxwrite = io_bufsize;
1164 struct timeval tp_start;
1165 struct cli_state *targetcli;
1168 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
1169 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1173 GetTimeOfDay(&tp_start);
1176 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
1178 if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1179 !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
1180 d_printf("getattrib: %s\n",cli_errstr(cli));
1185 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1189 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1193 /* allow files to be piped into smbclient
1196 Note that in this case this function will exit(0) rather
1198 if (!strcmp(lname, "-")) {
1200 /* size of file is not known */
1202 f = x_fopen(lname,O_RDONLY, 0);
1204 if (x_tseek(f, start, SEEK_SET) == -1) {
1205 d_printf("Error seeking local file\n");
1212 d_printf("Error opening local file %s\n",lname);
1216 DEBUG(1,("putting file %s as %s ",lname,
1219 buf = (char *)SMB_MALLOC(maxwrite);
1221 d_printf("ERROR: Not enough memory!\n");
1224 while (!x_feof(f)) {
1228 if ((n = readfile(buf,n,f)) < 1) {
1229 if((n == 0) && x_feof(f))
1230 break; /* Empty local file. */
1232 d_printf("Error reading local file: %s\n", strerror(errno));
1237 ret = cli_write(targetcli, fnum, 0, buf, nread + start, n);
1240 d_printf("Error writing file: %s\n", cli_errstr(cli));
1248 if (!cli_close(targetcli, fnum)) {
1249 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1263 struct timeval tp_end;
1266 GetTimeOfDay(&tp_end);
1268 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1269 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1270 put_total_time_ms += this_time;
1271 put_total_size += nread;
1273 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1274 nread / (1.024*this_time + 1.0e-4),
1275 put_total_size / (1.024*put_total_time_ms)));
1286 /****************************************************************************
1288 ****************************************************************************/
1290 static int cmd_put(void)
1297 pstrcpy(rname,cur_dir);
1298 pstrcat(rname,"\\");
1300 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1301 d_printf("put <filename>\n");
1306 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1309 pstrcat(rname,lname);
1311 dos_clean_name(rname);
1315 /* allow '-' to represent stdin
1316 jdblair, 24.jun.98 */
1317 if (!file_exist(lname,&st) &&
1318 (strcmp(lname,"-"))) {
1319 d_printf("%s does not exist\n",lname);
1324 return do_put(rname, lname, False);
1327 /*************************************
1328 File list structure.
1329 *************************************/
1331 static struct file_list {
1332 struct file_list *prev, *next;
1337 /****************************************************************************
1338 Free a file_list structure.
1339 ****************************************************************************/
1341 static void free_file_list (struct file_list * list)
1343 struct file_list *tmp;
1347 DLIST_REMOVE(list, list);
1348 SAFE_FREE(tmp->file_path);
1353 /****************************************************************************
1354 Seek in a directory/file list until you get something that doesn't start with
1356 ****************************************************************************/
1358 static BOOL seek_list(struct file_list *list, char *name)
1361 trim_string(list->file_path,"./","\n");
1362 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1371 /****************************************************************************
1372 Set the file selection mask.
1373 ****************************************************************************/
1375 static int cmd_select(void)
1377 pstrcpy(fileselection,"");
1378 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1383 /****************************************************************************
1384 Recursive file matching function act as find
1385 match must be always set to True when calling this function
1386 ****************************************************************************/
1388 static int file_find(struct file_list **list, const char *directory,
1389 const char *expression, BOOL match)
1391 SMB_STRUCT_DIR *dir;
1392 struct file_list *entry;
1393 struct stat statbuf;
1399 dir = sys_opendir(directory);
1403 while ((dname = readdirname(dir))) {
1404 if (!strcmp("..", dname))
1406 if (!strcmp(".", dname))
1409 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1414 if (!match || !gen_fnmatch(expression, dname)) {
1416 ret = stat(path, &statbuf);
1418 if (S_ISDIR(statbuf.st_mode)) {
1420 ret = file_find(list, path, expression, False);
1423 d_printf("file_find: cannot stat file %s\n", path);
1432 entry = SMB_MALLOC_P(struct file_list);
1434 d_printf("Out of memory in file_find\n");
1438 entry->file_path = path;
1439 entry->isdir = isdir;
1440 DLIST_ADD(*list, entry);
1450 /****************************************************************************
1452 ****************************************************************************/
1454 static int cmd_mput(void)
1459 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1461 struct file_list *temp_list;
1462 char *quest, *lname, *rname;
1466 ret = file_find(&file_list, ".", p, True);
1468 free_file_list(file_list);
1476 for (temp_list = file_list; temp_list;
1477 temp_list = temp_list->next) {
1480 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1482 trim_string(lname, "./", "/");
1484 /* check if it's a directory */
1485 if (temp_list->isdir) {
1486 /* if (!recurse) continue; */
1489 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1490 if (prompt && !yesno(quest)) { /* No */
1491 /* Skip the directory */
1492 lname[strlen(lname)-1] = '/';
1493 if (!seek_list(temp_list, lname))
1497 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1499 if (!cli_chkpath(cli, rname) &&
1501 DEBUG (0, ("Unable to make dir, skipping..."));
1502 /* Skip the directory */
1503 lname[strlen(lname)-1] = '/';
1504 if (!seek_list(temp_list, lname))
1511 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1512 if (prompt && !yesno(quest)) /* No */
1517 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1522 do_put(rname, lname, False);
1524 free_file_list(file_list);
1533 /****************************************************************************
1535 ****************************************************************************/
1537 static int do_cancel(int job)
1539 if (cli_printjob_del(cli, job)) {
1540 d_printf("Job %d cancelled\n",job);
1543 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1548 /****************************************************************************
1550 ****************************************************************************/
1552 static int cmd_cancel(void)
1557 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1558 d_printf("cancel <jobid> ...\n");
1564 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1569 /****************************************************************************
1571 ****************************************************************************/
1573 static int cmd_print(void)
1579 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1580 d_printf("print <filename>\n");
1584 pstrcpy(rname,lname);
1585 p = strrchr_m(rname,'/');
1587 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1590 if (strequal(lname,"-")) {
1591 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1594 return do_put(rname, lname, False);
1597 /****************************************************************************
1598 Show a print queue entry.
1599 ****************************************************************************/
1601 static void queue_fn(struct print_job_info *p)
1603 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
1606 /****************************************************************************
1608 ****************************************************************************/
1610 static int cmd_queue(void)
1612 cli_print_queue(cli, queue_fn);
1617 /****************************************************************************
1619 ****************************************************************************/
1621 static void do_del(file_info *finfo)
1625 pstr_sprintf( mask, "%s\\%s", finfo->dir, finfo->name );
1627 if (finfo->mode & aDIR)
1630 if (!cli_unlink(cli, mask)) {
1631 d_printf("%s deleting remote file %s\n",cli_errstr(cli),mask);
1635 /****************************************************************************
1637 ****************************************************************************/
1639 static int cmd_del(void)
1643 uint16 attribute = aSYSTEM | aHIDDEN;
1648 pstrcpy(mask,cur_dir);
1650 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1651 d_printf("del <filename>\n");
1656 do_list(mask, attribute,do_del,False,False);
1661 /****************************************************************************
1662 ****************************************************************************/
1664 static int cmd_open(void)
1668 struct cli_state *targetcli;
1671 pstrcpy(mask,cur_dir);
1673 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1674 d_printf("open <filename>\n");
1679 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1680 d_printf("open %s: %s\n", mask, cli_errstr(cli));
1684 cli_nt_create(targetcli, targetname, FILE_READ_DATA);
1690 /****************************************************************************
1692 ****************************************************************************/
1694 static int cmd_rmdir(void)
1698 struct cli_state *targetcli;
1701 pstrcpy(mask,cur_dir);
1703 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1704 d_printf("rmdir <dirname>\n");
1709 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1710 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
1714 if (!cli_rmdir(targetcli, targetname)) {
1715 d_printf("%s removing remote directory file %s\n",
1716 cli_errstr(targetcli),mask);
1722 /****************************************************************************
1724 ****************************************************************************/
1726 static int cmd_link(void)
1728 pstring oldname,newname;
1730 struct cli_state *targetcli;
1733 pstrcpy(oldname,cur_dir);
1734 pstrcpy(newname,cur_dir);
1736 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1737 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1738 d_printf("link <oldname> <newname>\n");
1742 pstrcat(oldname,buf);
1743 pstrcat(newname,buf2);
1745 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
1746 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
1750 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1751 d_printf("Server doesn't support UNIX CIFS calls.\n");
1755 if (!cli_unix_hardlink(targetcli, targetname, newname)) {
1756 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
1763 /****************************************************************************
1765 ****************************************************************************/
1767 static int cmd_symlink(void)
1769 pstring oldname,newname;
1772 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1773 d_printf("Server doesn't support UNIX CIFS calls.\n");
1777 pstrcpy(newname,cur_dir);
1779 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1780 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1781 d_printf("symlink <oldname> <newname>\n");
1785 pstrcpy(oldname,buf);
1786 pstrcat(newname,buf2);
1788 if (!cli_unix_symlink(cli, oldname, newname)) {
1789 d_printf("%s symlinking files (%s -> %s)\n",
1790 cli_errstr(cli), newname, oldname);
1797 /****************************************************************************
1799 ****************************************************************************/
1801 static int cmd_chmod(void)
1806 struct cli_state *targetcli;
1809 pstrcpy(src,cur_dir);
1811 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1812 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1813 d_printf("chmod mode file\n");
1817 mode = (mode_t)strtol(buf, NULL, 8);
1820 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
1821 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
1825 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1826 d_printf("Server doesn't support UNIX CIFS calls.\n");
1830 if (!cli_unix_chmod(targetcli, targetname, mode)) {
1831 d_printf("%s chmod file %s 0%o\n",
1832 cli_errstr(targetcli), src, (unsigned int)mode);
1839 static const char *filetype_to_str(mode_t mode)
1841 if (S_ISREG(mode)) {
1842 return "regular file";
1843 } else if (S_ISDIR(mode)) {
1847 if (S_ISCHR(mode)) {
1848 return "character device";
1852 if (S_ISBLK(mode)) {
1853 return "block device";
1857 if (S_ISFIFO(mode)) {
1862 if (S_ISLNK(mode)) {
1863 return "symbolic link";
1867 if (S_ISSOCK(mode)) {
1874 static char rwx_to_str(mode_t m, mode_t bt, char ret)
1883 static char *unix_mode_to_str(char *s, mode_t m)
1886 const char *str = filetype_to_str(m);
1902 *p++ = str[1] == 'y' ? 'l' : 's';
1909 *p++ = rwx_to_str(m, S_IRUSR, 'r');
1910 *p++ = rwx_to_str(m, S_IWUSR, 'w');
1911 *p++ = rwx_to_str(m, S_IXUSR, 'x');
1912 *p++ = rwx_to_str(m, S_IRGRP, 'r');
1913 *p++ = rwx_to_str(m, S_IWGRP, 'w');
1914 *p++ = rwx_to_str(m, S_IXGRP, 'x');
1915 *p++ = rwx_to_str(m, S_IROTH, 'r');
1916 *p++ = rwx_to_str(m, S_IWOTH, 'w');
1917 *p++ = rwx_to_str(m, S_IXOTH, 'x');
1922 /****************************************************************************
1923 Utility function for UNIX getfacl.
1924 ****************************************************************************/
1926 static char *perms_to_string(fstring permstr, unsigned char perms)
1928 fstrcpy(permstr, "---");
1929 if (perms & SMB_POSIX_ACL_READ) {
1932 if (perms & SMB_POSIX_ACL_WRITE) {
1935 if (perms & SMB_POSIX_ACL_EXECUTE) {
1941 /****************************************************************************
1943 ****************************************************************************/
1945 static int cmd_getfacl(void)
1948 uint16 major, minor;
1949 uint32 caplow, caphigh;
1950 char *retbuf = NULL;
1952 SMB_STRUCT_STAT sbuf;
1953 uint16 num_file_acls = 0;
1954 uint16 num_dir_acls = 0;
1956 struct cli_state *targetcli;
1959 pstrcpy(src,cur_dir);
1961 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
1962 d_printf("stat file\n");
1968 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
1969 d_printf("stat %s: %s\n", src, cli_errstr(cli));
1973 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1974 d_printf("Server doesn't support UNIX CIFS calls.\n");
1978 if (!cli_unix_extensions_version(targetcli, &major, &minor, &caplow, &caphigh)) {
1979 d_printf("Can't get UNIX CIFS version from server.\n");
1983 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
1984 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
1989 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
1990 d_printf("%s getfacl doing a stat on file %s\n",
1991 cli_errstr(targetcli), src);
1995 if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
1996 d_printf("%s getfacl file %s\n",
1997 cli_errstr(targetcli), src);
2001 /* ToDo : Print out the ACL values. */
2002 if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
2003 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2004 src, (unsigned int)CVAL(retbuf,0) );
2009 num_file_acls = SVAL(retbuf,2);
2010 num_dir_acls = SVAL(retbuf,4);
2011 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
2012 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2014 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
2015 (unsigned int)rb_size);
2021 d_printf("# file: %s\n", src);
2022 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
2024 if (num_file_acls == 0 && num_dir_acls == 0) {
2025 d_printf("No acls found.\n");
2028 for (i = 0; i < num_file_acls; i++) {
2031 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
2032 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2035 case SMB_POSIX_ACL_USER_OBJ:
2038 case SMB_POSIX_ACL_USER:
2039 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2040 d_printf("user:%u:", uorg);
2042 case SMB_POSIX_ACL_GROUP_OBJ:
2043 d_printf("group::");
2045 case SMB_POSIX_ACL_GROUP:
2046 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2047 d_printf("group:%u", uorg);
2049 case SMB_POSIX_ACL_MASK:
2052 case SMB_POSIX_ACL_OTHER:
2053 d_printf("other::");
2056 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2057 src, (unsigned int)tagtype );
2062 d_printf("%s\n", perms_to_string(permstring, perms));
2065 for (i = 0; i < num_dir_acls; i++) {
2068 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
2069 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2072 case SMB_POSIX_ACL_USER_OBJ:
2073 d_printf("default:user::");
2075 case SMB_POSIX_ACL_USER:
2076 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2077 d_printf("default:user:%u:", uorg);
2079 case SMB_POSIX_ACL_GROUP_OBJ:
2080 d_printf("default:group::");
2082 case SMB_POSIX_ACL_GROUP:
2083 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2084 d_printf("default:group:%u", uorg);
2086 case SMB_POSIX_ACL_MASK:
2087 d_printf("default:mask::");
2089 case SMB_POSIX_ACL_OTHER:
2090 d_printf("default:other::");
2093 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2094 src, (unsigned int)tagtype );
2099 d_printf("%s\n", perms_to_string(permstring, perms));
2106 /****************************************************************************
2108 ****************************************************************************/
2110 static int cmd_stat(void)
2114 SMB_STRUCT_STAT sbuf;
2115 struct cli_state *targetcli;
2118 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2119 d_printf("Server doesn't support UNIX CIFS calls.\n");
2123 pstrcpy(src,cur_dir);
2125 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2126 d_printf("stat file\n");
2133 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2134 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2138 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2139 d_printf("%s stat file %s\n",
2140 cli_errstr(targetcli), src);
2144 /* Print out the stat values. */
2145 d_printf("File: %s\n", src);
2146 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2147 (double)sbuf.st_size,
2148 (unsigned int)sbuf.st_blocks,
2149 filetype_to_str(sbuf.st_mode));
2151 #if defined(S_ISCHR) && defined(S_ISBLK)
2152 if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
2153 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2154 (double)sbuf.st_ino,
2155 (unsigned int)sbuf.st_nlink,
2156 unix_dev_major(sbuf.st_rdev),
2157 unix_dev_minor(sbuf.st_rdev));
2160 d_printf("Inode: %.0f\tLinks: %u\n",
2161 (double)sbuf.st_ino,
2162 (unsigned int)sbuf.st_nlink);
2164 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2165 ((int)sbuf.st_mode & 0777),
2166 unix_mode_to_str(mode_str, sbuf.st_mode),
2167 (unsigned int)sbuf.st_uid,
2168 (unsigned int)sbuf.st_gid);
2170 strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_atime));
2171 d_printf("Access: %s\n", mode_str);
2173 strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_mtime));
2174 d_printf("Modify: %s\n", mode_str);
2176 strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_ctime));
2177 d_printf("Change: %s\n", mode_str);
2183 /****************************************************************************
2185 ****************************************************************************/
2187 static int cmd_chown(void)
2192 pstring buf, buf2, buf3;
2193 struct cli_state *targetcli;
2196 pstrcpy(src,cur_dir);
2198 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2199 !next_token_nr(NULL,buf2,NULL, sizeof(buf2)) ||
2200 !next_token_nr(NULL,buf3,NULL, sizeof(buf3))) {
2201 d_printf("chown uid gid file\n");
2205 uid = (uid_t)atoi(buf);
2206 gid = (gid_t)atoi(buf2);
2209 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2210 d_printf("chown %s: %s\n", src, cli_errstr(cli));
2215 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2216 d_printf("Server doesn't support UNIX CIFS calls.\n");
2220 if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
2221 d_printf("%s chown file %s uid=%d, gid=%d\n",
2222 cli_errstr(targetcli), src, (int)uid, (int)gid);
2229 /****************************************************************************
2231 ****************************************************************************/
2233 static int cmd_rename(void)
2238 pstrcpy(src,cur_dir);
2239 pstrcpy(dest,cur_dir);
2241 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2242 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2243 d_printf("rename <src> <dest>\n");
2250 if (!cli_rename(cli, src, dest)) {
2251 d_printf("%s renaming files\n",cli_errstr(cli));
2258 /****************************************************************************
2259 Print the volume name.
2260 ****************************************************************************/
2262 static int cmd_volume(void)
2268 if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
2269 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
2273 d_printf("Volume: |%s| serial number 0x%x\n", volname, (unsigned int)serial_num);
2277 /****************************************************************************
2278 Hard link files using the NT call.
2279 ****************************************************************************/
2281 static int cmd_hardlink(void)
2285 struct cli_state *targetcli;
2288 pstrcpy(src,cur_dir);
2289 pstrcpy(dest,cur_dir);
2291 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2292 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2293 d_printf("hardlink <src> <dest>\n");
2300 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2301 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
2305 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2306 d_printf("Server doesn't support UNIX CIFS calls.\n");
2310 if (!cli_nt_hardlink(targetcli, targetname, dest)) {
2311 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
2318 /****************************************************************************
2319 Toggle the prompt flag.
2320 ****************************************************************************/
2322 static int cmd_prompt(void)
2325 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2330 /****************************************************************************
2331 Set the newer than time.
2332 ****************************************************************************/
2334 static int cmd_newer(void)
2338 SMB_STRUCT_STAT sbuf;
2340 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2341 if (ok && (sys_stat(buf,&sbuf) == 0)) {
2342 newer_than = sbuf.st_mtime;
2343 DEBUG(1,("Getting files newer than %s",
2344 asctime(localtime(&newer_than))));
2349 if (ok && newer_than == 0) {
2350 d_printf("Error setting newer-than time\n");
2357 /****************************************************************************
2358 Set the archive level.
2359 ****************************************************************************/
2361 static int cmd_archive(void)
2365 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2366 archive_level = atoi(buf);
2368 d_printf("Archive level is %d\n",archive_level);
2373 /****************************************************************************
2374 Toggle the lowercaseflag.
2375 ****************************************************************************/
2377 static int cmd_lowercase(void)
2379 lowercase = !lowercase;
2380 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2385 /****************************************************************************
2386 Toggle the case sensitive flag.
2387 ****************************************************************************/
2389 static int cmd_setcase(void)
2391 BOOL orig_case_sensitive = cli_set_case_sensitive(cli, False);
2393 cli_set_case_sensitive(cli, !orig_case_sensitive);
2394 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
2400 /****************************************************************************
2401 Toggle the recurse flag.
2402 ****************************************************************************/
2404 static int cmd_recurse(void)
2407 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2412 /****************************************************************************
2413 Toggle the translate flag.
2414 ****************************************************************************/
2416 static int cmd_translate(void)
2418 translation = !translation;
2419 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2420 translation?"on":"off"));
2425 /****************************************************************************
2427 ****************************************************************************/
2429 static int cmd_lcd(void)
2434 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2436 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2441 /****************************************************************************
2442 Get a file restarting at end of local file.
2443 ****************************************************************************/
2445 static int cmd_reget(void)
2448 pstring remote_name;
2451 pstrcpy(remote_name, cur_dir);
2452 pstrcat(remote_name, "\\");
2454 p = remote_name + strlen(remote_name);
2456 if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2457 d_printf("reget <filename>\n");
2460 pstrcpy(local_name, p);
2461 dos_clean_name(remote_name);
2463 next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2465 return do_get(remote_name, local_name, True);
2468 /****************************************************************************
2469 Put a file restarting at end of local file.
2470 ****************************************************************************/
2472 static int cmd_reput(void)
2475 pstring remote_name;
2480 pstrcpy(remote_name, cur_dir);
2481 pstrcat(remote_name, "\\");
2483 if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2484 d_printf("reput <filename>\n");
2487 pstrcpy(local_name, p);
2489 if (!file_exist(local_name, &st)) {
2490 d_printf("%s does not exist\n", local_name);
2494 if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2495 pstrcat(remote_name, p);
2497 pstrcat(remote_name, local_name);
2499 dos_clean_name(remote_name);
2501 return do_put(remote_name, local_name, True);
2504 /****************************************************************************
2506 ****************************************************************************/
2508 static void browse_fn(const char *name, uint32 m,
2509 const char *comment, void *state)
2517 case STYPE_DISKTREE:
2518 fstrcpy(typestr,"Disk"); break;
2520 fstrcpy(typestr,"Printer"); break;
2522 fstrcpy(typestr,"Device"); break;
2524 fstrcpy(typestr,"IPC"); break;
2526 /* FIXME: If the remote machine returns non-ascii characters
2527 in any of these fields, they can corrupt the output. We
2528 should remove them. */
2530 d_printf("\t%-15s %-10.10s%s\n",
2531 name,typestr,comment);
2533 d_printf ("%s|%s|%s\n",typestr,name,comment);
2537 /****************************************************************************
2538 Try and browse available connections on a host.
2539 ****************************************************************************/
2541 static BOOL browse_host(BOOL sort)
2545 d_printf("\n\tSharename Type Comment\n");
2546 d_printf("\t--------- ---- -------\n");
2549 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
2550 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
2555 /****************************************************************************
2557 ****************************************************************************/
2559 static void server_fn(const char *name, uint32 m,
2560 const char *comment, void *state)
2564 d_printf("\t%-16s %s\n", name, comment);
2566 d_printf("%s|%s|%s\n",(char *)state, name, comment);
2570 /****************************************************************************
2571 Try and browse available connections on a host.
2572 ****************************************************************************/
2574 static BOOL list_servers(const char *wk_grp)
2578 if (!cli->server_domain)
2582 d_printf("\n\tServer Comment\n");
2583 d_printf("\t--------- -------\n");
2585 fstrcpy( state, "Server" );
2586 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
2590 d_printf("\n\tWorkgroup Master\n");
2591 d_printf("\t--------- -------\n");
2594 fstrcpy( state, "Workgroup" );
2595 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
2600 /****************************************************************************
2601 Print or set current VUID
2602 ****************************************************************************/
2604 static int cmd_vuid(void)
2608 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2609 d_printf("Current VUID is %d\n", cli->vuid);
2613 cli->vuid = atoi(buf);
2617 /****************************************************************************
2618 Setup a new VUID, by issuing a session setup
2619 ****************************************************************************/
2621 static int cmd_logon(void)
2623 pstring l_username, l_password;
2626 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2627 d_printf("logon <username> [<password>]\n");
2631 pstrcpy(l_username, buf);
2633 if (!next_token_nr(NULL,buf2,NULL,sizeof(buf)))
2635 char *pass = getpass("Password: ");
2637 pstrcpy(l_password, pass);
2640 pstrcpy(l_password, buf2);
2642 if (!cli_session_setup(cli, l_username,
2643 l_password, strlen(l_password),
2644 l_password, strlen(l_password),
2646 d_printf("session setup failed: %s\n", cli_errstr(cli));
2650 d_printf("Current VUID is %d\n", cli->vuid);
2655 /****************************************************************************
2656 list active connections
2657 ****************************************************************************/
2659 static int cmd_list_connect(void)
2666 /****************************************************************************
2667 display the current active client connection
2668 ****************************************************************************/
2670 static int cmd_show_connect( void )
2672 struct cli_state *targetcli;
2675 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
2676 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
2680 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
2684 /* Some constants for completing filename arguments */
2686 #define COMPL_NONE 0 /* No completions */
2687 #define COMPL_REMOTE 1 /* Complete remote filename */
2688 #define COMPL_LOCAL 2 /* Complete local filename */
2690 /* This defines the commands supported by this client.
2691 * NOTE: The "!" must be the last one in the list because it's fn pointer
2692 * field is NULL, and NULL in that field is used in process_tok()
2693 * (below) to indicate the end of the list. crh
2699 const char *description;
2700 char compl_args[2]; /* Completion argument info */
2702 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2703 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2704 {"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}},
2705 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
2706 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2707 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
2708 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2709 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2710 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2711 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2712 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2713 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2714 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2715 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2716 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
2717 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2718 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2719 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2720 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2721 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2722 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
2723 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2724 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2725 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2726 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2727 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2728 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
2729 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2730 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2731 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2732 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2733 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
2734 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2735 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2736 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2737 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2738 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2739 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2740 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
2741 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2742 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2743 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2744 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2745 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2746 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
2747 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
2748 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2749 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
2750 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
2751 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2752 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
2753 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
2754 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
2755 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
2756 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
2758 /* Yes, this must be here, see crh's comment above. */
2759 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2760 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2763 /*******************************************************************
2764 Lookup a command string in the list of commands, including
2766 ******************************************************************/
2768 static int process_tok(pstring tok)
2770 int i = 0, matches = 0;
2772 int tok_len = strlen(tok);
2774 while (commands[i].fn != NULL) {
2775 if (strequal(commands[i].name,tok)) {
2779 } else if (strnequal(commands[i].name, tok, tok_len)) {
2788 else if (matches == 1)
2794 /****************************************************************************
2796 ****************************************************************************/
2798 static int cmd_help(void)
2803 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2804 if ((i = process_tok(buf)) >= 0)
2805 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
2807 while (commands[i].description) {
2808 for (j=0; commands[i].description && (j<5); j++) {
2809 d_printf("%-15s",commands[i].name);
2818 /****************************************************************************
2819 Process a -c command string.
2820 ****************************************************************************/
2822 static int process_command_string(char *cmd)
2828 /* establish the connection if not already */
2831 cli = cli_cm_open(desthost, service, True);
2836 while (cmd[0] != '\0') {
2841 if ((p = strchr_m(cmd, ';')) == 0) {
2842 strncpy(line, cmd, 999);
2848 strncpy(line, cmd, p - cmd);
2849 line[p - cmd] = '\0';
2853 /* and get the first part of the command */
2855 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
2857 if ((i = process_tok(tok)) >= 0) {
2858 rc = commands[i].fn();
2859 } else if (i == -2) {
2860 d_printf("%s: command abbreviation ambiguous\n",tok);
2862 d_printf("%s: command not found\n",tok);
2869 #define MAX_COMPLETIONS 100
2877 } completion_remote_t;
2879 static void completion_remote_filter(const char *mnt, file_info *f, const char *mask, void *state)
2881 completion_remote_t *info = (completion_remote_t *)state;
2883 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
2884 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
2885 info->matches[info->count] = SMB_STRDUP(f->name);
2889 if (info->dirmask[0] != 0)
2890 pstrcpy(tmp, info->dirmask);
2893 pstrcat(tmp, f->name);
2896 info->matches[info->count] = SMB_STRDUP(tmp);
2898 if (info->matches[info->count] == NULL)
2901 smb_readline_ca_char(0);
2903 if (info->count == 1)
2904 info->samelen = strlen(info->matches[info->count]);
2906 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
2912 static char **remote_completion(const char *text, int len)
2916 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
2918 /* can't have non-static intialisation on Sun CC, so do it
2924 if (len >= PATH_MAX)
2927 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
2928 if (!info.matches) return NULL;
2929 info.matches[0] = NULL;
2931 for (i = len-1; i >= 0; i--)
2932 if ((text[i] == '/') || (text[i] == '\\'))
2934 info.text = text+i+1;
2935 info.samelen = info.len = len-i-1;
2938 strncpy(info.dirmask, text, i+1);
2939 info.dirmask[i+1] = 0;
2940 pstr_sprintf(dirmask, "%s%*s*", cur_dir, i-1, text);
2942 pstr_sprintf(dirmask, "%s*", cur_dir);
2944 if (cli_list(cli, dirmask, aDIR | aSYSTEM | aHIDDEN, completion_remote_filter, &info) < 0)
2947 if (info.count == 2)
2948 info.matches[0] = SMB_STRDUP(info.matches[1]);
2950 info.matches[0] = SMB_MALLOC(info.samelen+1);
2951 if (!info.matches[0])
2953 strncpy(info.matches[0], info.matches[1], info.samelen);
2954 info.matches[0][info.samelen] = 0;
2956 info.matches[info.count] = NULL;
2957 return info.matches;
2960 for (i = 0; i < info.count; i++)
2961 free(info.matches[i]);
2966 static char **completion_fn(const char *text, int start, int end)
2968 smb_readline_ca_char(' ');
2971 const char *buf, *sp;
2975 buf = smb_readline_get_line_buffer();
2979 sp = strchr(buf, ' ');
2983 for (i = 0; commands[i].name; i++)
2984 if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
2986 if (commands[i].name == NULL)
2992 if (sp == (buf + start))
2993 compl_type = commands[i].compl_args[0];
2995 compl_type = commands[i].compl_args[1];
2997 if (compl_type == COMPL_REMOTE)
2998 return remote_completion(text, end - start);
2999 else /* fall back to local filename completion */
3003 int i, len, samelen = 0, count=1;
3005 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
3012 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3013 if (strncmp(text, commands[i].name, len) == 0) {
3014 matches[count] = SMB_STRDUP(commands[i].name);
3015 if (!matches[count])
3018 samelen = strlen(matches[count]);
3020 while (strncmp(matches[count], matches[count-1], samelen) != 0)
3027 case 0: /* should never happen */
3031 matches[0] = SMB_STRDUP(matches[1]);
3034 matches[0] = SMB_MALLOC(samelen+1);
3037 strncpy(matches[0], matches[1], samelen);
3038 matches[0][samelen] = 0;
3040 matches[count] = NULL;
3044 for (i = 0; i < count; i++)
3052 /****************************************************************************
3053 Make sure we swallow keepalives during idle time.
3054 ****************************************************************************/
3056 static void readline_callback(void)
3059 struct timeval timeout;
3060 static time_t last_t;
3076 FD_SET(cli->fd,&fds);
3079 timeout.tv_usec = 0;
3080 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
3082 /* We deliberately use receive_smb instead of
3083 client_receive_smb as we want to receive
3084 session keepalives and then drop them here.
3086 if (FD_ISSET(cli->fd,&fds)) {
3087 if (!receive_smb(cli->fd,cli->inbuf,0)) {
3088 DEBUG(0, ("Read from server failed, maybe it closed the "
3095 /* Ping the server to keep the connection alive using SMBecho. */
3097 unsigned char garbage[16];
3098 memset(garbage, 0xf0, sizeof(garbage));
3099 cli_echo(cli, garbage, sizeof(garbage));
3103 /****************************************************************************
3104 Process commands on stdin.
3105 ****************************************************************************/
3107 static int process_stdin(void)
3119 /* display a prompt */
3120 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3121 cline = smb_readline(the_prompt, readline_callback, completion_fn);
3125 pstrcpy(line, cline);
3127 /* special case - first char is ! */
3133 /* and get the first part of the command */
3135 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3137 if ((i = process_tok(tok)) >= 0) {
3138 rc = commands[i].fn();
3139 } else if (i == -2) {
3140 d_printf("%s: command abbreviation ambiguous\n",tok);
3142 d_printf("%s: command not found\n",tok);
3148 /****************************************************************************
3149 Process commands from the client.
3150 ****************************************************************************/
3152 static int process(char *base_directory)
3156 cli = cli_cm_open(desthost, service, True);
3161 if (*base_directory) {
3162 rc = do_cd(base_directory);
3170 rc = process_command_string(cmdstr);
3179 /****************************************************************************
3181 ****************************************************************************/
3183 static int do_host_query(char *query_host)
3185 cli = cli_cm_open(query_host, "IPC$", True);
3193 /* Workgroups simply don't make sense over anything
3194 else but port 139... */
3197 cli_cm_set_port( 139 );
3198 cli = cli_cm_open(query_host, "IPC$", True);
3202 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3206 list_servers(lp_workgroup());
3213 /****************************************************************************
3214 Handle a tar operation.
3215 ****************************************************************************/
3217 static int do_tar_op(char *base_directory)
3221 /* do we already have a connection? */
3223 cli = cli_cm_open(desthost, service, True);
3230 if (*base_directory) {
3231 ret = do_cd(base_directory);
3245 /****************************************************************************
3246 Handle a message operation.
3247 ****************************************************************************/
3249 static int do_message_op(void)
3252 struct nmb_name called, calling;
3253 fstring server_name;
3254 char name_type_hex[10];
3257 make_nmb_name(&calling, calling_name, 0x0);
3258 make_nmb_name(&called , desthost, name_type);
3260 fstrcpy(server_name, desthost);
3261 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
3262 fstrcat(server_name, name_type_hex);
3268 /* we can only do messages over port 139 (to windows clients at least) */
3270 msg_port = port ? port : 139;
3272 if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, msg_port) != msg_port) ||
3273 !cli_connect(cli, server_name, &ip)) {
3274 d_printf("Connection to %s failed\n", desthost);
3278 if (!cli_session_request(cli, &calling, &called)) {
3279 d_printf("session request failed\n");
3291 /****************************************************************************
3293 ****************************************************************************/
3295 int main(int argc,char *argv[])
3297 pstring base_directory;
3300 BOOL message = False;
3302 static const char *new_name_resolve_order = NULL;
3306 fstring new_workgroup;
3307 struct poptOption long_options[] = {
3310 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3311 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3312 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3313 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3314 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3315 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3316 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
3317 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3318 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3319 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
3320 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
3321 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3322 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
3324 POPT_COMMON_CONNECTION
3325 POPT_COMMON_CREDENTIALS
3332 pstrcpy(term_code, KANJI);
3338 *base_directory = 0;
3340 /* initialize the workgroup name so we can determine whether or
3341 not it was set by a command line option */
3343 set_global_myworkgroup( "" );
3344 set_global_myname( "" );
3346 /* set default debug level to 0 regardless of what smb.conf sets */
3347 setup_logging( "smbclient", True );
3348 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
3350 x_setbuf( x_stderr, NULL );
3352 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options,
3353 POPT_CONTEXT_KEEP_FIRST);
3354 poptSetOtherOptionHelp(pc, "service <password>");
3356 in_client = True; /* Make sure that we tell lp_load we are */
3358 while ((opt = poptGetNextOpt(pc)) != -1) {
3361 /* Messages are sent to NetBIOS name type 0x3
3362 * (Messenger Service). Make sure we default
3363 * to port 139 instead of port 445. srl,crh
3366 cli_cm_set_dest_name_type( name_type );
3367 pstrcpy(desthost,poptGetOptArg(pc));
3369 cli_cm_set_port( 139 );
3374 dest_ip = *interpret_addr2(poptGetOptArg(pc));
3375 if (is_zero_ip(dest_ip))
3379 cli_cm_set_dest_ip( dest_ip );
3384 display_set_stderr();
3388 pstrcpy(query_host, poptGetOptArg(pc));
3391 pstrcpy(term_code, poptGetOptArg(pc));
3394 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
3397 /* We must use old option processing for this. Find the
3398 * position of the -T option in the raw argv[]. */
3401 for (i = 1; i < argc; i++) {
3402 if (strncmp("-T", argv[i],2)==0)
3406 if (!(optnum = tar_parseargs(argc, argv, poptGetOptArg(pc), i))) {
3407 poptPrintUsage(pc, stderr, 0);
3410 /* Now we must eat (optnum - i) options - they have
3411 * been processed by tar_parseargs().
3414 for (i = 0; i < optnum; i++)
3419 pstrcpy(base_directory,poptGetOptArg(pc));
3429 /* check for the -P option */
3432 cli_cm_set_port( port );
3435 * Don't load debug level from smb.conf. It should be
3436 * set by cmdline arg or remain default (0)
3438 AllowDebugChange = False;
3440 /* save the workgroup...
3442 FIXME!! do we need to do this for other options as well
3443 (or maybe a generic way to keep lp_load() from overwriting
3446 fstrcpy( new_workgroup, lp_workgroup() );
3447 pstrcpy( calling_name, global_myname() );
3449 if ( override_logfile )
3450 setup_logging( lp_logfile(), False );
3452 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
3453 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
3454 argv[0], dyn_CONFIGFILE);
3459 if ( strlen(new_workgroup) != 0 )
3460 set_global_myworkgroup( new_workgroup );
3462 if ( strlen(calling_name) != 0 )
3463 set_global_myname( calling_name );
3465 pstrcpy( calling_name, global_myname() );
3467 if(poptPeekArg(pc)) {
3468 pstrcpy(service,poptGetArg(pc));
3469 /* Convert any '/' characters in the service name to '\' characters */
3470 string_replace(service, '/','\\');
3472 if (count_chars(service,'\\') < 3) {
3473 d_printf("\n%s: Not enough '\\' characters in service\n",service);
3474 poptPrintUsage(pc, stderr, 0);
3479 if (poptPeekArg(pc) && !cmdline_auth_info.got_pass) {
3480 cmdline_auth_info.got_pass = True;
3481 pstrcpy(cmdline_auth_info.password,poptGetArg(pc));
3486 if(new_name_resolve_order)
3487 lp_set_name_resolve_order(new_name_resolve_order);
3489 if (!tar_type && !*query_host && !*service && !message) {
3490 poptPrintUsage(pc, stderr, 0);
3494 poptFreeContext(pc);
3496 /* store the username an password for dfs support */
3498 cli_cm_set_credentials( &cmdline_auth_info );
3499 pstrcpy(username, cmdline_auth_info.username);
3501 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
3505 process_command_string(cmdstr);
3506 return do_tar_op(base_directory);
3510 char *qhost = query_host;
3513 while (*qhost == '\\' || *qhost == '/')
3516 if ((slash = strchr_m(qhost, '/'))
3517 || (slash = strchr_m(qhost, '\\'))) {
3521 if ((p=strchr_m(qhost, '#'))) {
3524 sscanf(p, "%x", &name_type);
3525 cli_cm_set_dest_name_type( name_type );
3528 return do_host_query(qhost);
3532 return do_message_op();
3535 if (process(base_directory)) {