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[0] != '\0') && (*(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),
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.
451 if (do_list_queue == NULL) {
452 DEBUG(4,("do_list_queue is empty\n"));
453 do_list_queue_start = do_list_queue_end = 0;
457 if (do_list_queue_start == do_list_queue_end) {
458 DEBUG(4,("do_list_queue is empty\n"));
459 do_list_queue_start = do_list_queue_end = 0;
460 *do_list_queue = '\0';
461 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
462 DEBUG(4,("sliding do_list_queue backward\n"));
463 memmove(do_list_queue,
464 do_list_queue + do_list_queue_start,
465 do_list_queue_end - do_list_queue_start);
466 do_list_queue_end -= do_list_queue_start;
467 do_list_queue_start = 0;
471 static void add_to_do_list_queue(const char* entry)
473 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
474 while (new_end > do_list_queue_size) {
475 do_list_queue_size *= 2;
476 DEBUG(4,("enlarging do_list_queue to %d\n",
477 (int)do_list_queue_size));
478 do_list_queue = SMB_REALLOC(do_list_queue, do_list_queue_size);
479 if (! do_list_queue) {
480 d_printf("failure enlarging do_list_queue to %d bytes\n",
481 (int)do_list_queue_size);
482 reset_do_list_queue();
484 memset(do_list_queue + do_list_queue_size / 2,
485 0, do_list_queue_size / 2);
489 safe_strcpy_base(do_list_queue + do_list_queue_end,
490 entry, do_list_queue, do_list_queue_size);
491 do_list_queue_end = new_end;
492 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
493 entry, (int)do_list_queue_start, (int)do_list_queue_end));
497 static char *do_list_queue_head(void)
499 return do_list_queue + do_list_queue_start;
502 static void remove_do_list_queue_head(void)
504 if (do_list_queue_end > do_list_queue_start) {
505 do_list_queue_start += strlen(do_list_queue_head()) + 1;
506 adjust_do_list_queue();
507 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
508 (int)do_list_queue_start, (int)do_list_queue_end));
512 static int do_list_queue_empty(void)
514 return (! (do_list_queue && *do_list_queue));
517 /****************************************************************************
518 A helper for do_list.
519 ****************************************************************************/
521 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
525 /* save the directory */
526 pstrcpy( f->dir, mask );
527 if ( (dir_end = strrchr( f->dir, '\\' )) != NULL ) {
531 if (f->mode & aDIR) {
532 if (do_list_dirs && do_this_one(f)) {
535 if (do_list_recurse &&
536 !strequal(f->name,".") &&
537 !strequal(f->name,"..")) {
542 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
546 pstrcpy(mask2, mntpoint);
547 pstrcat(mask2, mask);
548 p = strrchr_m(mask2,'\\');
552 pstrcat(mask2, f->name);
553 pstrcat(mask2,"\\*");
554 add_to_do_list_queue(mask2);
559 if (do_this_one(f)) {
564 /****************************************************************************
565 A wrapper around cli_list that adds recursion.
566 ****************************************************************************/
568 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
570 static int in_do_list = 0;
571 struct cli_state *targetcli;
574 if (in_do_list && rec) {
575 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
581 do_list_recurse = rec;
586 init_do_list_queue();
587 add_to_do_list_queue(mask);
589 while (! do_list_queue_empty()) {
591 * Need to copy head so that it doesn't become
592 * invalid inside the call to cli_list. This
593 * would happen if the list were expanded
595 * Fix from E. Jay Berkenbilt (ejb@ql.org)
598 pstrcpy(head, do_list_queue_head());
602 if ( !cli_resolve_path( "", cli, head, &targetcli, targetpath ) ) {
603 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
604 remove_do_list_queue_head();
608 cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
609 remove_do_list_queue_head();
610 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
611 char* next_file = do_list_queue_head();
613 if ((strlen(next_file) >= 2) &&
614 (next_file[strlen(next_file) - 1] == '*') &&
615 (next_file[strlen(next_file) - 2] == '\\')) {
616 save_ch = next_file +
617 strlen(next_file) - 2;
620 d_printf("\n%s\n",next_file);
629 if ( cli_resolve_path( "", cli, mask, &targetcli, targetpath ) ) {
630 if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1)
631 d_printf("%s listing %s\n", cli_errstr(targetcli), targetpath);
634 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
639 reset_do_list_queue();
642 /****************************************************************************
643 Get a directory listing.
644 ****************************************************************************/
646 static int cmd_dir(void)
648 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
655 if (strcmp(cur_dir, "\\") != 0) {
656 pstrcpy(mask,cur_dir);
657 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!='\\'))
663 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
673 do_list(mask, attribute, display_finfo, recurse, True);
677 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
682 /****************************************************************************
683 Get a directory listing.
684 ****************************************************************************/
686 static int cmd_du(void)
688 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
695 pstrcpy(mask,cur_dir);
696 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!='\\'))
699 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
709 do_list(mask, attribute, do_du, recurse, True);
713 d_printf("Total number of bytes: %.0f\n", dir_total);
718 /****************************************************************************
719 Get a file from rname to lname
720 ****************************************************************************/
722 static int do_get(char *rname, char *lname, BOOL reget)
724 int handle = 0, fnum;
725 BOOL newhandle = False;
727 struct timeval tp_start;
728 int read_size = io_bufsize;
734 struct cli_state *targetcli;
742 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
743 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
747 GetTimeOfDay(&tp_start);
749 if ( targetcli->dfsroot ) {
752 /* we need to refer to the full \server\share\path format
755 pstrcpy( path, targetname );
756 cli_dfs_make_full_path( targetname, targetcli->desthost,
757 targetcli->share, path);
760 fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
763 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
767 if(!strcmp(lname,"-")) {
768 handle = fileno(stdout);
771 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
773 start = sys_lseek(handle, 0, SEEK_END);
775 d_printf("Error seeking local file\n");
780 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
785 d_printf("Error opening local file %s\n",lname);
790 if (!cli_qfileinfo(targetcli, fnum,
791 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
792 !cli_getattrE(targetcli, fnum,
793 &attr, &size, NULL, NULL, NULL)) {
794 d_printf("getattrib: %s\n",cli_errstr(targetcli));
798 DEBUG(1,("getting file %s of size %.0f as %s ",
799 rname, (double)size, lname));
801 if(!(data = (char *)SMB_MALLOC(read_size))) {
802 d_printf("malloc fail for size %d\n", read_size);
803 cli_close(targetcli, fnum);
808 int n = cli_read(targetcli, fnum, data, nread + start, read_size);
813 if (writefile(handle,data, n) != n) {
814 d_printf("Error writing local file\n");
822 if (nread + start < size) {
823 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
824 rname, (long)nread));
831 if (!cli_close(targetcli, fnum)) {
832 d_printf("Error %s closing remote file\n",cli_errstr(cli));
840 if (archive_level >= 2 && (attr & aARCH)) {
841 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
845 struct timeval tp_end;
848 GetTimeOfDay(&tp_end);
850 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
851 (tp_end.tv_usec - tp_start.tv_usec)/1000;
852 get_total_time_ms += this_time;
853 get_total_size += nread;
855 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
856 nread / (1.024*this_time + 1.0e-4),
857 get_total_size / (1.024*get_total_time_ms)));
863 /****************************************************************************
865 ****************************************************************************/
867 static int cmd_get(void)
873 pstrcpy(rname,cur_dir);
876 p = rname + strlen(rname);
878 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
879 d_printf("get <filename>\n");
883 dos_clean_name(rname);
885 next_token_nr(NULL,lname,NULL,sizeof(lname));
887 return do_get(rname, lname, False);
890 /****************************************************************************
891 Do an mget operation on one file.
892 ****************************************************************************/
894 static void do_mget(file_info *finfo)
898 pstring saved_curdir;
901 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
905 d_printf("mget aborted\n");
909 if (finfo->mode & aDIR)
910 slprintf(quest,sizeof(pstring)-1,
911 "Get directory %s? ",finfo->name);
913 slprintf(quest,sizeof(pstring)-1,
914 "Get file %s? ",finfo->name);
916 if (prompt && !yesno(quest))
919 if (!(finfo->mode & aDIR)) {
920 pstrcpy(rname,cur_dir);
921 pstrcat(rname,finfo->name);
922 do_get(rname, finfo->name, False);
926 /* handle directories */
927 pstrcpy(saved_curdir,cur_dir);
929 pstrcat(cur_dir,finfo->name);
930 pstrcat(cur_dir,"\\");
932 unix_format(finfo->name);
934 strlower_m(finfo->name);
936 if (!directory_exist(finfo->name,NULL) &&
937 mkdir(finfo->name,0777) != 0) {
938 d_printf("failed to create directory %s\n",finfo->name);
939 pstrcpy(cur_dir,saved_curdir);
943 if (chdir(finfo->name) != 0) {
944 d_printf("failed to chdir to directory %s\n",finfo->name);
945 pstrcpy(cur_dir,saved_curdir);
949 pstrcpy(mget_mask,cur_dir);
950 pstrcat(mget_mask,"*");
952 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
954 pstrcpy(cur_dir,saved_curdir);
957 /****************************************************************************
958 View the file using the pager.
959 ****************************************************************************/
961 static int cmd_more(void)
963 pstring rname,lname,pager_cmd;
968 pstrcpy(rname,cur_dir);
971 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
972 fd = smb_mkstemp(lname);
974 d_printf("failed to create temporary file for more\n");
979 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
980 d_printf("more <filename>\n");
984 dos_clean_name(rname);
986 rc = do_get(rname, lname, False);
988 pager=getenv("PAGER");
990 slprintf(pager_cmd,sizeof(pager_cmd)-1,
991 "%s %s",(pager? pager:PAGER), lname);
998 /****************************************************************************
1000 ****************************************************************************/
1002 static int cmd_mget(void)
1004 uint16 attribute = aSYSTEM | aHIDDEN;
1016 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1017 pstrcpy(mget_mask,cur_dir);
1018 if ((mget_mask[0] != '\0') && (mget_mask[strlen(mget_mask)-1]!='\\'))
1019 pstrcat(mget_mask,"\\");
1022 pstrcpy(mget_mask,p);
1024 pstrcat(mget_mask,p);
1025 do_list(mget_mask, attribute,do_mget,False,True);
1029 pstrcpy(mget_mask,cur_dir);
1030 if(mget_mask[strlen(mget_mask)-1]!='\\')
1031 pstrcat(mget_mask,"\\");
1032 pstrcat(mget_mask,"*");
1033 do_list(mget_mask, attribute,do_mget,False,True);
1039 /****************************************************************************
1040 Make a directory of name "name".
1041 ****************************************************************************/
1043 static BOOL do_mkdir(char *name)
1045 struct cli_state *targetcli;
1048 if ( !cli_resolve_path( "", cli, name, &targetcli, targetname ) ) {
1049 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1053 if (!cli_mkdir(targetcli, targetname)) {
1054 d_printf("%s making remote directory %s\n",
1055 cli_errstr(targetcli),name);
1062 /****************************************************************************
1063 Show 8.3 name of a file.
1064 ****************************************************************************/
1066 static BOOL do_altname(char *name)
1069 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1070 d_printf("%s getting alt name for %s\n",
1071 cli_errstr(cli),name);
1074 d_printf("%s\n", altname);
1079 /****************************************************************************
1081 ****************************************************************************/
1083 static int cmd_quit(void)
1091 /****************************************************************************
1093 ****************************************************************************/
1095 static int cmd_mkdir(void)
1101 pstrcpy(mask,cur_dir);
1103 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1105 d_printf("mkdir <dirname>\n");
1116 trim_char(ddir,'.','\0');
1117 p = strtok(ddir,"/\\");
1120 if (!cli_chkpath(cli, ddir2)) {
1123 pstrcat(ddir2,"\\");
1124 p = strtok(NULL,"/\\");
1133 /****************************************************************************
1135 ****************************************************************************/
1137 static int cmd_altname(void)
1143 pstrcpy(name,cur_dir);
1145 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1146 d_printf("altname <file>\n");
1156 /****************************************************************************
1158 ****************************************************************************/
1160 static int do_put(char *rname, char *lname, BOOL reput)
1164 SMB_OFF_T start = 0;
1167 int maxwrite = io_bufsize;
1169 struct timeval tp_start;
1170 struct cli_state *targetcli;
1173 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
1174 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1178 GetTimeOfDay(&tp_start);
1181 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
1183 if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1184 !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
1185 d_printf("getattrib: %s\n",cli_errstr(cli));
1190 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1194 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1198 /* allow files to be piped into smbclient
1201 Note that in this case this function will exit(0) rather
1203 if (!strcmp(lname, "-")) {
1205 /* size of file is not known */
1207 f = x_fopen(lname,O_RDONLY, 0);
1209 if (x_tseek(f, start, SEEK_SET) == -1) {
1210 d_printf("Error seeking local file\n");
1217 d_printf("Error opening local file %s\n",lname);
1221 DEBUG(1,("putting file %s as %s ",lname,
1224 buf = (char *)SMB_MALLOC(maxwrite);
1226 d_printf("ERROR: Not enough memory!\n");
1229 while (!x_feof(f)) {
1233 if ((n = readfile(buf,n,f)) < 1) {
1234 if((n == 0) && x_feof(f))
1235 break; /* Empty local file. */
1237 d_printf("Error reading local file: %s\n", strerror(errno));
1242 ret = cli_write(targetcli, fnum, 0, buf, nread + start, n);
1245 d_printf("Error writing file: %s\n", cli_errstr(cli));
1253 if (!cli_close(targetcli, fnum)) {
1254 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1268 struct timeval tp_end;
1271 GetTimeOfDay(&tp_end);
1273 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1274 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1275 put_total_time_ms += this_time;
1276 put_total_size += nread;
1278 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1279 nread / (1.024*this_time + 1.0e-4),
1280 put_total_size / (1.024*put_total_time_ms)));
1291 /****************************************************************************
1293 ****************************************************************************/
1295 static int cmd_put(void)
1302 pstrcpy(rname,cur_dir);
1303 pstrcat(rname,"\\");
1305 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1306 d_printf("put <filename>\n");
1311 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1314 pstrcat(rname,lname);
1316 dos_clean_name(rname);
1320 /* allow '-' to represent stdin
1321 jdblair, 24.jun.98 */
1322 if (!file_exist(lname,&st) &&
1323 (strcmp(lname,"-"))) {
1324 d_printf("%s does not exist\n",lname);
1329 return do_put(rname, lname, False);
1332 /*************************************
1333 File list structure.
1334 *************************************/
1336 static struct file_list {
1337 struct file_list *prev, *next;
1342 /****************************************************************************
1343 Free a file_list structure.
1344 ****************************************************************************/
1346 static void free_file_list (struct file_list *list_head)
1348 struct file_list *list, *next;
1350 for (list = list_head; list; list = next) {
1352 DLIST_REMOVE(list_head, list);
1353 SAFE_FREE(list->file_path);
1358 /****************************************************************************
1359 Seek in a directory/file list until you get something that doesn't start with
1361 ****************************************************************************/
1363 static BOOL seek_list(struct file_list *list, char *name)
1366 trim_string(list->file_path,"./","\n");
1367 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1376 /****************************************************************************
1377 Set the file selection mask.
1378 ****************************************************************************/
1380 static int cmd_select(void)
1382 pstrcpy(fileselection,"");
1383 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1388 /****************************************************************************
1389 Recursive file matching function act as find
1390 match must be always set to True when calling this function
1391 ****************************************************************************/
1393 static int file_find(struct file_list **list, const char *directory,
1394 const char *expression, BOOL match)
1396 SMB_STRUCT_DIR *dir;
1397 struct file_list *entry;
1398 struct stat statbuf;
1404 dir = sys_opendir(directory);
1408 while ((dname = readdirname(dir))) {
1409 if (!strcmp("..", dname))
1411 if (!strcmp(".", dname))
1414 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1419 if (!match || !gen_fnmatch(expression, dname)) {
1421 ret = stat(path, &statbuf);
1423 if (S_ISDIR(statbuf.st_mode)) {
1425 ret = file_find(list, path, expression, False);
1428 d_printf("file_find: cannot stat file %s\n", path);
1437 entry = SMB_MALLOC_P(struct file_list);
1439 d_printf("Out of memory in file_find\n");
1443 entry->file_path = path;
1444 entry->isdir = isdir;
1445 DLIST_ADD(*list, entry);
1455 /****************************************************************************
1457 ****************************************************************************/
1459 static int cmd_mput(void)
1464 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1466 struct file_list *temp_list;
1467 char *quest, *lname, *rname;
1471 ret = file_find(&file_list, ".", p, True);
1473 free_file_list(file_list);
1481 for (temp_list = file_list; temp_list;
1482 temp_list = temp_list->next) {
1485 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1487 trim_string(lname, "./", "/");
1489 /* check if it's a directory */
1490 if (temp_list->isdir) {
1491 /* if (!recurse) continue; */
1494 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1495 if (prompt && !yesno(quest)) { /* No */
1496 /* Skip the directory */
1497 lname[strlen(lname)-1] = '/';
1498 if (!seek_list(temp_list, lname))
1502 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1504 if (!cli_chkpath(cli, rname) &&
1506 DEBUG (0, ("Unable to make dir, skipping..."));
1507 /* Skip the directory */
1508 lname[strlen(lname)-1] = '/';
1509 if (!seek_list(temp_list, lname))
1516 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1517 if (prompt && !yesno(quest)) /* No */
1522 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1527 do_put(rname, lname, False);
1529 free_file_list(file_list);
1538 /****************************************************************************
1540 ****************************************************************************/
1542 static int do_cancel(int job)
1544 if (cli_printjob_del(cli, job)) {
1545 d_printf("Job %d cancelled\n",job);
1548 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1553 /****************************************************************************
1555 ****************************************************************************/
1557 static int cmd_cancel(void)
1562 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1563 d_printf("cancel <jobid> ...\n");
1569 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1574 /****************************************************************************
1576 ****************************************************************************/
1578 static int cmd_print(void)
1584 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1585 d_printf("print <filename>\n");
1589 pstrcpy(rname,lname);
1590 p = strrchr_m(rname,'/');
1592 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1595 if (strequal(lname,"-")) {
1596 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1599 return do_put(rname, lname, False);
1602 /****************************************************************************
1603 Show a print queue entry.
1604 ****************************************************************************/
1606 static void queue_fn(struct print_job_info *p)
1608 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
1611 /****************************************************************************
1613 ****************************************************************************/
1615 static int cmd_queue(void)
1617 cli_print_queue(cli, queue_fn);
1622 /****************************************************************************
1624 ****************************************************************************/
1626 static void do_del(file_info *finfo)
1630 pstr_sprintf( mask, "%s\\%s", finfo->dir, finfo->name );
1632 if (finfo->mode & aDIR)
1635 if (!cli_unlink(cli, mask)) {
1636 d_printf("%s deleting remote file %s\n",cli_errstr(cli),mask);
1640 /****************************************************************************
1642 ****************************************************************************/
1644 static int cmd_del(void)
1648 uint16 attribute = aSYSTEM | aHIDDEN;
1653 pstrcpy(mask,cur_dir);
1655 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1656 d_printf("del <filename>\n");
1661 do_list(mask, attribute,do_del,False,False);
1666 /****************************************************************************
1667 ****************************************************************************/
1669 static int cmd_open(void)
1673 struct cli_state *targetcli;
1676 pstrcpy(mask,cur_dir);
1678 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1679 d_printf("open <filename>\n");
1684 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1685 d_printf("open %s: %s\n", mask, cli_errstr(cli));
1689 cli_nt_create(targetcli, targetname, FILE_READ_DATA);
1695 /****************************************************************************
1697 ****************************************************************************/
1699 static int cmd_rmdir(void)
1703 struct cli_state *targetcli;
1706 pstrcpy(mask,cur_dir);
1708 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1709 d_printf("rmdir <dirname>\n");
1714 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1715 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
1719 if (!cli_rmdir(targetcli, targetname)) {
1720 d_printf("%s removing remote directory file %s\n",
1721 cli_errstr(targetcli),mask);
1727 /****************************************************************************
1729 ****************************************************************************/
1731 static int cmd_link(void)
1733 pstring oldname,newname;
1735 struct cli_state *targetcli;
1738 pstrcpy(oldname,cur_dir);
1739 pstrcpy(newname,cur_dir);
1741 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1742 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1743 d_printf("link <oldname> <newname>\n");
1747 pstrcat(oldname,buf);
1748 pstrcat(newname,buf2);
1750 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
1751 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
1755 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1756 d_printf("Server doesn't support UNIX CIFS calls.\n");
1760 if (!cli_unix_hardlink(targetcli, targetname, newname)) {
1761 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
1768 /****************************************************************************
1770 ****************************************************************************/
1772 static int cmd_symlink(void)
1774 pstring oldname,newname;
1777 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1778 d_printf("Server doesn't support UNIX CIFS calls.\n");
1782 pstrcpy(newname,cur_dir);
1784 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1785 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1786 d_printf("symlink <oldname> <newname>\n");
1790 pstrcpy(oldname,buf);
1791 pstrcat(newname,buf2);
1793 if (!cli_unix_symlink(cli, oldname, newname)) {
1794 d_printf("%s symlinking files (%s -> %s)\n",
1795 cli_errstr(cli), newname, oldname);
1802 /****************************************************************************
1804 ****************************************************************************/
1806 static int cmd_chmod(void)
1811 struct cli_state *targetcli;
1814 pstrcpy(src,cur_dir);
1816 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1817 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1818 d_printf("chmod mode file\n");
1822 mode = (mode_t)strtol(buf, NULL, 8);
1825 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
1826 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
1830 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1831 d_printf("Server doesn't support UNIX CIFS calls.\n");
1835 if (!cli_unix_chmod(targetcli, targetname, mode)) {
1836 d_printf("%s chmod file %s 0%o\n",
1837 cli_errstr(targetcli), src, (unsigned int)mode);
1844 static const char *filetype_to_str(mode_t mode)
1846 if (S_ISREG(mode)) {
1847 return "regular file";
1848 } else if (S_ISDIR(mode)) {
1852 if (S_ISCHR(mode)) {
1853 return "character device";
1857 if (S_ISBLK(mode)) {
1858 return "block device";
1862 if (S_ISFIFO(mode)) {
1867 if (S_ISLNK(mode)) {
1868 return "symbolic link";
1872 if (S_ISSOCK(mode)) {
1879 static char rwx_to_str(mode_t m, mode_t bt, char ret)
1888 static char *unix_mode_to_str(char *s, mode_t m)
1891 const char *str = filetype_to_str(m);
1907 *p++ = str[1] == 'y' ? 'l' : 's';
1914 *p++ = rwx_to_str(m, S_IRUSR, 'r');
1915 *p++ = rwx_to_str(m, S_IWUSR, 'w');
1916 *p++ = rwx_to_str(m, S_IXUSR, 'x');
1917 *p++ = rwx_to_str(m, S_IRGRP, 'r');
1918 *p++ = rwx_to_str(m, S_IWGRP, 'w');
1919 *p++ = rwx_to_str(m, S_IXGRP, 'x');
1920 *p++ = rwx_to_str(m, S_IROTH, 'r');
1921 *p++ = rwx_to_str(m, S_IWOTH, 'w');
1922 *p++ = rwx_to_str(m, S_IXOTH, 'x');
1927 /****************************************************************************
1928 Utility function for UNIX getfacl.
1929 ****************************************************************************/
1931 static char *perms_to_string(fstring permstr, unsigned char perms)
1933 fstrcpy(permstr, "---");
1934 if (perms & SMB_POSIX_ACL_READ) {
1937 if (perms & SMB_POSIX_ACL_WRITE) {
1940 if (perms & SMB_POSIX_ACL_EXECUTE) {
1946 /****************************************************************************
1948 ****************************************************************************/
1950 static int cmd_getfacl(void)
1953 uint16 major, minor;
1954 uint32 caplow, caphigh;
1955 char *retbuf = NULL;
1957 SMB_STRUCT_STAT sbuf;
1958 uint16 num_file_acls = 0;
1959 uint16 num_dir_acls = 0;
1961 struct cli_state *targetcli;
1964 pstrcpy(src,cur_dir);
1966 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
1967 d_printf("stat file\n");
1973 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
1974 d_printf("stat %s: %s\n", src, cli_errstr(cli));
1978 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1979 d_printf("Server doesn't support UNIX CIFS calls.\n");
1983 if (!cli_unix_extensions_version(targetcli, &major, &minor, &caplow, &caphigh)) {
1984 d_printf("Can't get UNIX CIFS version from server.\n");
1988 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
1989 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
1994 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
1995 d_printf("%s getfacl doing a stat on file %s\n",
1996 cli_errstr(targetcli), src);
2000 if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
2001 d_printf("%s getfacl file %s\n",
2002 cli_errstr(targetcli), src);
2006 /* ToDo : Print out the ACL values. */
2007 if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
2008 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2009 src, (unsigned int)CVAL(retbuf,0) );
2014 num_file_acls = SVAL(retbuf,2);
2015 num_dir_acls = SVAL(retbuf,4);
2016 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
2017 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2019 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
2020 (unsigned int)rb_size);
2026 d_printf("# file: %s\n", src);
2027 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
2029 if (num_file_acls == 0 && num_dir_acls == 0) {
2030 d_printf("No acls found.\n");
2033 for (i = 0; i < num_file_acls; i++) {
2036 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
2037 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2040 case SMB_POSIX_ACL_USER_OBJ:
2043 case SMB_POSIX_ACL_USER:
2044 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2045 d_printf("user:%u:", uorg);
2047 case SMB_POSIX_ACL_GROUP_OBJ:
2048 d_printf("group::");
2050 case SMB_POSIX_ACL_GROUP:
2051 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2052 d_printf("group:%u", uorg);
2054 case SMB_POSIX_ACL_MASK:
2057 case SMB_POSIX_ACL_OTHER:
2058 d_printf("other::");
2061 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2062 src, (unsigned int)tagtype );
2067 d_printf("%s\n", perms_to_string(permstring, perms));
2070 for (i = 0; i < num_dir_acls; i++) {
2073 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
2074 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2077 case SMB_POSIX_ACL_USER_OBJ:
2078 d_printf("default:user::");
2080 case SMB_POSIX_ACL_USER:
2081 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2082 d_printf("default:user:%u:", uorg);
2084 case SMB_POSIX_ACL_GROUP_OBJ:
2085 d_printf("default:group::");
2087 case SMB_POSIX_ACL_GROUP:
2088 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2089 d_printf("default:group:%u", uorg);
2091 case SMB_POSIX_ACL_MASK:
2092 d_printf("default:mask::");
2094 case SMB_POSIX_ACL_OTHER:
2095 d_printf("default:other::");
2098 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2099 src, (unsigned int)tagtype );
2104 d_printf("%s\n", perms_to_string(permstring, perms));
2111 /****************************************************************************
2113 ****************************************************************************/
2115 static int cmd_stat(void)
2119 SMB_STRUCT_STAT sbuf;
2120 struct cli_state *targetcli;
2124 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2125 d_printf("Server doesn't support UNIX CIFS calls.\n");
2129 pstrcpy(src,cur_dir);
2131 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2132 d_printf("stat file\n");
2139 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2140 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2144 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2145 d_printf("%s stat file %s\n",
2146 cli_errstr(targetcli), src);
2150 /* Print out the stat values. */
2151 d_printf("File: %s\n", src);
2152 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2153 (double)sbuf.st_size,
2154 (unsigned int)sbuf.st_blocks,
2155 filetype_to_str(sbuf.st_mode));
2157 #if defined(S_ISCHR) && defined(S_ISBLK)
2158 if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
2159 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2160 (double)sbuf.st_ino,
2161 (unsigned int)sbuf.st_nlink,
2162 unix_dev_major(sbuf.st_rdev),
2163 unix_dev_minor(sbuf.st_rdev));
2166 d_printf("Inode: %.0f\tLinks: %u\n",
2167 (double)sbuf.st_ino,
2168 (unsigned int)sbuf.st_nlink);
2170 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2171 ((int)sbuf.st_mode & 0777),
2172 unix_mode_to_str(mode_str, sbuf.st_mode),
2173 (unsigned int)sbuf.st_uid,
2174 (unsigned int)sbuf.st_gid);
2176 lt = localtime(&sbuf.st_atime);
2178 strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
2180 fstrcpy(mode_str, "unknown");
2182 d_printf("Access: %s\n", mode_str);
2184 lt = localtime(&sbuf.st_mtime);
2186 strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
2188 fstrcpy(mode_str, "unknown");
2190 d_printf("Modify: %s\n", mode_str);
2192 lt = localtime(&sbuf.st_ctime);
2194 strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
2196 fstrcpy(mode_str, "unknown");
2198 d_printf("Change: %s\n", mode_str);
2204 /****************************************************************************
2206 ****************************************************************************/
2208 static int cmd_chown(void)
2213 pstring buf, buf2, buf3;
2214 struct cli_state *targetcli;
2217 pstrcpy(src,cur_dir);
2219 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2220 !next_token_nr(NULL,buf2,NULL, sizeof(buf2)) ||
2221 !next_token_nr(NULL,buf3,NULL, sizeof(buf3))) {
2222 d_printf("chown uid gid file\n");
2226 uid = (uid_t)atoi(buf);
2227 gid = (gid_t)atoi(buf2);
2230 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2231 d_printf("chown %s: %s\n", src, cli_errstr(cli));
2236 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2237 d_printf("Server doesn't support UNIX CIFS calls.\n");
2241 if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
2242 d_printf("%s chown file %s uid=%d, gid=%d\n",
2243 cli_errstr(targetcli), src, (int)uid, (int)gid);
2250 /****************************************************************************
2252 ****************************************************************************/
2254 static int cmd_rename(void)
2259 pstrcpy(src,cur_dir);
2260 pstrcpy(dest,cur_dir);
2262 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2263 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2264 d_printf("rename <src> <dest>\n");
2271 if (!cli_rename(cli, src, dest)) {
2272 d_printf("%s renaming files\n",cli_errstr(cli));
2279 /****************************************************************************
2280 Print the volume name.
2281 ****************************************************************************/
2283 static int cmd_volume(void)
2289 if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
2290 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
2294 d_printf("Volume: |%s| serial number 0x%x\n", volname, (unsigned int)serial_num);
2298 /****************************************************************************
2299 Hard link files using the NT call.
2300 ****************************************************************************/
2302 static int cmd_hardlink(void)
2306 struct cli_state *targetcli;
2309 pstrcpy(src,cur_dir);
2310 pstrcpy(dest,cur_dir);
2312 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2313 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2314 d_printf("hardlink <src> <dest>\n");
2321 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2322 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
2326 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2327 d_printf("Server doesn't support UNIX CIFS calls.\n");
2331 if (!cli_nt_hardlink(targetcli, targetname, dest)) {
2332 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
2339 /****************************************************************************
2340 Toggle the prompt flag.
2341 ****************************************************************************/
2343 static int cmd_prompt(void)
2346 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2351 /****************************************************************************
2352 Set the newer than time.
2353 ****************************************************************************/
2355 static int cmd_newer(void)
2359 SMB_STRUCT_STAT sbuf;
2361 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2362 if (ok && (sys_stat(buf,&sbuf) == 0)) {
2363 newer_than = sbuf.st_mtime;
2364 DEBUG(1,("Getting files newer than %s",
2365 time_to_asc(&newer_than)));
2370 if (ok && newer_than == 0) {
2371 d_printf("Error setting newer-than time\n");
2378 /****************************************************************************
2379 Set the archive level.
2380 ****************************************************************************/
2382 static int cmd_archive(void)
2386 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2387 archive_level = atoi(buf);
2389 d_printf("Archive level is %d\n",archive_level);
2394 /****************************************************************************
2395 Toggle the lowercaseflag.
2396 ****************************************************************************/
2398 static int cmd_lowercase(void)
2400 lowercase = !lowercase;
2401 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2406 /****************************************************************************
2407 Toggle the case sensitive flag.
2408 ****************************************************************************/
2410 static int cmd_setcase(void)
2412 BOOL orig_case_sensitive = cli_set_case_sensitive(cli, False);
2414 cli_set_case_sensitive(cli, !orig_case_sensitive);
2415 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
2421 /****************************************************************************
2422 Toggle the recurse flag.
2423 ****************************************************************************/
2425 static int cmd_recurse(void)
2428 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2433 /****************************************************************************
2434 Toggle the translate flag.
2435 ****************************************************************************/
2437 static int cmd_translate(void)
2439 translation = !translation;
2440 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2441 translation?"on":"off"));
2446 /****************************************************************************
2448 ****************************************************************************/
2450 static int cmd_lcd(void)
2455 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2457 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2462 /****************************************************************************
2463 Get a file restarting at end of local file.
2464 ****************************************************************************/
2466 static int cmd_reget(void)
2469 pstring remote_name;
2472 pstrcpy(remote_name, cur_dir);
2473 pstrcat(remote_name, "\\");
2475 p = remote_name + strlen(remote_name);
2477 if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2478 d_printf("reget <filename>\n");
2481 pstrcpy(local_name, p);
2482 dos_clean_name(remote_name);
2484 next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2486 return do_get(remote_name, local_name, True);
2489 /****************************************************************************
2490 Put a file restarting at end of local file.
2491 ****************************************************************************/
2493 static int cmd_reput(void)
2496 pstring remote_name;
2501 pstrcpy(remote_name, cur_dir);
2502 pstrcat(remote_name, "\\");
2504 if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2505 d_printf("reput <filename>\n");
2508 pstrcpy(local_name, p);
2510 if (!file_exist(local_name, &st)) {
2511 d_printf("%s does not exist\n", local_name);
2515 if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2516 pstrcat(remote_name, p);
2518 pstrcat(remote_name, local_name);
2520 dos_clean_name(remote_name);
2522 return do_put(remote_name, local_name, True);
2525 /****************************************************************************
2527 ****************************************************************************/
2529 static void browse_fn(const char *name, uint32 m,
2530 const char *comment, void *state)
2538 case STYPE_DISKTREE:
2539 fstrcpy(typestr,"Disk"); break;
2541 fstrcpy(typestr,"Printer"); break;
2543 fstrcpy(typestr,"Device"); break;
2545 fstrcpy(typestr,"IPC"); break;
2547 /* FIXME: If the remote machine returns non-ascii characters
2548 in any of these fields, they can corrupt the output. We
2549 should remove them. */
2551 d_printf("\t%-15s %-10.10s%s\n",
2552 name,typestr,comment);
2554 d_printf ("%s|%s|%s\n",typestr,name,comment);
2558 static BOOL browse_host_rpc(BOOL sort)
2561 struct rpc_pipe_client *pipe_hnd;
2562 TALLOC_CTX *mem_ctx;
2565 SRV_SHARE_INFO_CTR ctr;
2568 mem_ctx = talloc_new(NULL);
2569 if (mem_ctx == NULL) {
2570 DEBUG(0, ("talloc_new failed\n"));
2574 init_enum_hnd(&enum_hnd, 0);
2576 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
2578 if (pipe_hnd == NULL) {
2579 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
2580 nt_errstr(status)));
2581 TALLOC_FREE(mem_ctx);
2585 werr = rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, 1, &ctr,
2586 0xffffffff, &enum_hnd);
2588 if (!W_ERROR_IS_OK(werr)) {
2589 TALLOC_FREE(mem_ctx);
2590 cli_rpc_pipe_close(pipe_hnd);
2594 for (i=0; i<ctr.num_entries; i++) {
2595 SRV_SHARE_INFO_1 *info = &ctr.share.info1[i];
2596 char *name, *comment;
2597 name = rpcstr_pull_unistr2_talloc(
2598 mem_ctx, &info->info_1_str.uni_netname);
2599 comment = rpcstr_pull_unistr2_talloc(
2600 mem_ctx, &info->info_1_str.uni_remark);
2601 browse_fn(name, info->info_1.type, comment, NULL);
2604 TALLOC_FREE(mem_ctx);
2605 cli_rpc_pipe_close(pipe_hnd);
2609 /****************************************************************************
2610 Try and browse available connections on a host.
2611 ****************************************************************************/
2613 static BOOL browse_host(BOOL sort)
2617 d_printf("\n\tSharename Type Comment\n");
2618 d_printf("\t--------- ---- -------\n");
2621 if (browse_host_rpc(sort)) {
2625 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
2626 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
2631 /****************************************************************************
2633 ****************************************************************************/
2635 static void server_fn(const char *name, uint32 m,
2636 const char *comment, void *state)
2640 d_printf("\t%-16s %s\n", name, comment);
2642 d_printf("%s|%s|%s\n",(char *)state, name, comment);
2646 /****************************************************************************
2647 Try and browse available connections on a host.
2648 ****************************************************************************/
2650 static BOOL list_servers(const char *wk_grp)
2654 if (!cli->server_domain)
2658 d_printf("\n\tServer Comment\n");
2659 d_printf("\t--------- -------\n");
2661 fstrcpy( state, "Server" );
2662 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
2666 d_printf("\n\tWorkgroup Master\n");
2667 d_printf("\t--------- -------\n");
2670 fstrcpy( state, "Workgroup" );
2671 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
2676 /****************************************************************************
2677 Print or set current VUID
2678 ****************************************************************************/
2680 static int cmd_vuid(void)
2684 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2685 d_printf("Current VUID is %d\n", cli->vuid);
2689 cli->vuid = atoi(buf);
2693 /****************************************************************************
2694 Setup a new VUID, by issuing a session setup
2695 ****************************************************************************/
2697 static int cmd_logon(void)
2699 pstring l_username, l_password;
2702 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2703 d_printf("logon <username> [<password>]\n");
2707 pstrcpy(l_username, buf);
2709 if (!next_token_nr(NULL,buf2,NULL,sizeof(buf)))
2711 char *pass = getpass("Password: ");
2713 pstrcpy(l_password, pass);
2716 pstrcpy(l_password, buf2);
2718 if (!cli_session_setup(cli, l_username,
2719 l_password, strlen(l_password),
2720 l_password, strlen(l_password),
2722 d_printf("session setup failed: %s\n", cli_errstr(cli));
2726 d_printf("Current VUID is %d\n", cli->vuid);
2731 /****************************************************************************
2732 list active connections
2733 ****************************************************************************/
2735 static int cmd_list_connect(void)
2742 /****************************************************************************
2743 display the current active client connection
2744 ****************************************************************************/
2746 static int cmd_show_connect( void )
2748 struct cli_state *targetcli;
2751 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
2752 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
2756 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
2760 /* Some constants for completing filename arguments */
2762 #define COMPL_NONE 0 /* No completions */
2763 #define COMPL_REMOTE 1 /* Complete remote filename */
2764 #define COMPL_LOCAL 2 /* Complete local filename */
2766 /* This defines the commands supported by this client.
2767 * NOTE: The "!" must be the last one in the list because it's fn pointer
2768 * field is NULL, and NULL in that field is used in process_tok()
2769 * (below) to indicate the end of the list. crh
2775 const char *description;
2776 char compl_args[2]; /* Completion argument info */
2778 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2779 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2780 {"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}},
2781 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
2782 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2783 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
2784 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2785 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2786 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2787 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2788 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2789 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2790 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2791 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2792 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
2793 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2794 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2795 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2796 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2797 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2798 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
2799 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2800 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2801 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2802 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2803 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2804 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
2805 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2806 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2807 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2808 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2809 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
2810 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2811 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2812 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2813 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2814 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2815 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2816 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
2817 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2818 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2819 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2820 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2821 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2822 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
2823 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
2824 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2825 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
2826 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
2827 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2828 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
2829 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
2830 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
2831 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
2832 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
2834 /* Yes, this must be here, see crh's comment above. */
2835 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2836 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2839 /*******************************************************************
2840 Lookup a command string in the list of commands, including
2842 ******************************************************************/
2844 static int process_tok(pstring tok)
2846 int i = 0, matches = 0;
2848 int tok_len = strlen(tok);
2850 while (commands[i].fn != NULL) {
2851 if (strequal(commands[i].name,tok)) {
2855 } else if (strnequal(commands[i].name, tok, tok_len)) {
2864 else if (matches == 1)
2870 /****************************************************************************
2872 ****************************************************************************/
2874 static int cmd_help(void)
2879 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2880 if ((i = process_tok(buf)) >= 0)
2881 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
2883 while (commands[i].description) {
2884 for (j=0; commands[i].description && (j<5); j++) {
2885 d_printf("%-15s",commands[i].name);
2894 /****************************************************************************
2895 Process a -c command string.
2896 ****************************************************************************/
2898 static int process_command_string(char *cmd)
2904 /* establish the connection if not already */
2907 cli = cli_cm_open(desthost, service, True);
2912 while (cmd[0] != '\0') {
2917 if ((p = strchr_m(cmd, ';')) == 0) {
2918 strncpy(line, cmd, 999);
2924 strncpy(line, cmd, p - cmd);
2925 line[p - cmd] = '\0';
2929 /* and get the first part of the command */
2931 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
2933 if ((i = process_tok(tok)) >= 0) {
2934 rc = commands[i].fn();
2935 } else if (i == -2) {
2936 d_printf("%s: command abbreviation ambiguous\n",tok);
2938 d_printf("%s: command not found\n",tok);
2945 #define MAX_COMPLETIONS 100
2953 } completion_remote_t;
2955 static void completion_remote_filter(const char *mnt, file_info *f, const char *mask, void *state)
2957 completion_remote_t *info = (completion_remote_t *)state;
2959 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
2960 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
2961 info->matches[info->count] = SMB_STRDUP(f->name);
2965 if (info->dirmask[0] != 0)
2966 pstrcpy(tmp, info->dirmask);
2969 pstrcat(tmp, f->name);
2972 info->matches[info->count] = SMB_STRDUP(tmp);
2974 if (info->matches[info->count] == NULL)
2977 smb_readline_ca_char(0);
2979 if (info->count == 1)
2980 info->samelen = strlen(info->matches[info->count]);
2982 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
2988 static char **remote_completion(const char *text, int len)
2992 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
2994 /* can't have non-static intialisation on Sun CC, so do it
3000 if (len >= MIN(PATH_MAX,sizeof(pstring))) {
3004 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
3005 if (!info.matches) {
3008 info.matches[0] = NULL;
3010 for (i = len-1; i >= 0; i--) {
3011 if ((text[i] == '/') || (text[i] == '\\')) {
3016 info.text = text+i+1;
3017 info.samelen = info.len = len-i-1;
3020 strncpy(info.dirmask, text, i+1);
3021 info.dirmask[i+1] = 0;
3022 pstr_sprintf(dirmask, "%s%*s*", cur_dir, i-1, text);
3024 pstr_sprintf(dirmask, "%s*", cur_dir);
3027 if (cli_list(cli, dirmask, aDIR | aSYSTEM | aHIDDEN, completion_remote_filter, &info) < 0)
3030 if (info.count == 2)
3031 info.matches[0] = SMB_STRDUP(info.matches[1]);
3033 info.matches[0] = SMB_MALLOC(info.samelen+1);
3034 if (!info.matches[0])
3036 strncpy(info.matches[0], info.matches[1], info.samelen);
3037 info.matches[0][info.samelen] = 0;
3039 info.matches[info.count] = NULL;
3040 return info.matches;
3043 for (i = 0; i < info.count; i++)
3044 free(info.matches[i]);
3049 static char **completion_fn(const char *text, int start, int end)
3051 smb_readline_ca_char(' ');
3054 const char *buf, *sp;
3058 buf = smb_readline_get_line_buffer();
3062 sp = strchr(buf, ' ');
3066 for (i = 0; commands[i].name; i++)
3067 if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
3069 if (commands[i].name == NULL)
3075 if (sp == (buf + start))
3076 compl_type = commands[i].compl_args[0];
3078 compl_type = commands[i].compl_args[1];
3080 if (compl_type == COMPL_REMOTE)
3081 return remote_completion(text, end - start);
3082 else /* fall back to local filename completion */
3086 int i, len, samelen = 0, count=1;
3088 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
3095 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3096 if (strncmp(text, commands[i].name, len) == 0) {
3097 matches[count] = SMB_STRDUP(commands[i].name);
3098 if (!matches[count])
3101 samelen = strlen(matches[count]);
3103 while (strncmp(matches[count], matches[count-1], samelen) != 0)
3110 case 0: /* should never happen */
3114 matches[0] = SMB_STRDUP(matches[1]);
3117 matches[0] = SMB_MALLOC(samelen+1);
3120 strncpy(matches[0], matches[1], samelen);
3121 matches[0][samelen] = 0;
3123 matches[count] = NULL;
3127 for (i = 0; i < count; i++)
3135 /****************************************************************************
3136 Make sure we swallow keepalives during idle time.
3137 ****************************************************************************/
3139 static void readline_callback(void)
3142 struct timeval timeout;
3143 static time_t last_t;
3159 FD_SET(cli->fd,&fds);
3162 timeout.tv_usec = 0;
3163 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
3165 /* We deliberately use receive_smb instead of
3166 client_receive_smb as we want to receive
3167 session keepalives and then drop them here.
3169 if (FD_ISSET(cli->fd,&fds)) {
3170 if (!receive_smb(cli->fd,cli->inbuf,0)) {
3171 DEBUG(0, ("Read from server failed, maybe it closed the "
3178 /* Ping the server to keep the connection alive using SMBecho. */
3180 unsigned char garbage[16];
3181 memset(garbage, 0xf0, sizeof(garbage));
3182 cli_echo(cli, garbage, sizeof(garbage));
3186 /****************************************************************************
3187 Process commands on stdin.
3188 ****************************************************************************/
3190 static int process_stdin(void)
3202 /* display a prompt */
3203 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3204 cline = smb_readline(the_prompt, readline_callback, completion_fn);
3208 pstrcpy(line, cline);
3210 /* special case - first char is ! */
3216 /* and get the first part of the command */
3218 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3220 if ((i = process_tok(tok)) >= 0) {
3221 rc = commands[i].fn();
3222 } else if (i == -2) {
3223 d_printf("%s: command abbreviation ambiguous\n",tok);
3225 d_printf("%s: command not found\n",tok);
3231 /****************************************************************************
3232 Process commands from the client.
3233 ****************************************************************************/
3235 static int process(char *base_directory)
3239 cli = cli_cm_open(desthost, service, True);
3244 if (*base_directory) {
3245 rc = do_cd(base_directory);
3253 rc = process_command_string(cmdstr);
3262 /****************************************************************************
3264 ****************************************************************************/
3266 static int do_host_query(char *query_host)
3268 cli = cli_cm_open(query_host, "IPC$", True);
3276 /* Workgroups simply don't make sense over anything
3277 else but port 139... */
3280 cli_cm_set_port( 139 );
3281 cli = cli_cm_open(query_host, "IPC$", True);
3285 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3289 list_servers(lp_workgroup());
3296 /****************************************************************************
3297 Handle a tar operation.
3298 ****************************************************************************/
3300 static int do_tar_op(char *base_directory)
3304 /* do we already have a connection? */
3306 cli = cli_cm_open(desthost, service, True);
3313 if (*base_directory) {
3314 ret = do_cd(base_directory);
3328 /****************************************************************************
3329 Handle a message operation.
3330 ****************************************************************************/
3332 static int do_message_op(void)
3335 struct nmb_name called, calling;
3336 fstring server_name;
3337 char name_type_hex[10];
3340 make_nmb_name(&calling, calling_name, 0x0);
3341 make_nmb_name(&called , desthost, name_type);
3343 fstrcpy(server_name, desthost);
3344 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
3345 fstrcat(server_name, name_type_hex);
3351 /* we can only do messages over port 139 (to windows clients at least) */
3353 msg_port = port ? port : 139;
3355 if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, msg_port) != msg_port) ||
3356 !cli_connect(cli, server_name, &ip)) {
3357 d_printf("Connection to %s failed\n", desthost);
3361 if (!cli_session_request(cli, &calling, &called)) {
3362 d_printf("session request failed\n");
3374 /****************************************************************************
3376 ****************************************************************************/
3378 int main(int argc,char *argv[])
3380 pstring base_directory;
3383 BOOL message = False;
3385 static const char *new_name_resolve_order = NULL;
3389 fstring new_workgroup;
3390 struct poptOption long_options[] = {
3393 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3394 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3395 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3396 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3397 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3398 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3399 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
3400 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3401 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3402 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
3403 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
3404 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3405 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
3407 POPT_COMMON_CONNECTION
3408 POPT_COMMON_CREDENTIALS
3415 pstrcpy(term_code, KANJI);
3421 *base_directory = 0;
3423 /* initialize the workgroup name so we can determine whether or
3424 not it was set by a command line option */
3426 set_global_myworkgroup( "" );
3427 set_global_myname( "" );
3429 /* set default debug level to 0 regardless of what smb.conf sets */
3430 setup_logging( "smbclient", True );
3431 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
3432 if ((dbf = x_fdup(x_stderr))) {
3433 x_setbuf( dbf, NULL );
3436 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options,
3437 POPT_CONTEXT_KEEP_FIRST);
3438 poptSetOtherOptionHelp(pc, "service <password>");
3440 in_client = True; /* Make sure that we tell lp_load we are */
3442 while ((opt = poptGetNextOpt(pc)) != -1) {
3445 /* Messages are sent to NetBIOS name type 0x3
3446 * (Messenger Service). Make sure we default
3447 * to port 139 instead of port 445. srl,crh
3450 cli_cm_set_dest_name_type( name_type );
3451 pstrcpy(desthost,poptGetOptArg(pc));
3453 cli_cm_set_port( 139 );
3458 dest_ip = *interpret_addr2(poptGetOptArg(pc));
3459 if (is_zero_ip(dest_ip))
3463 cli_cm_set_dest_ip( dest_ip );
3471 display_set_stderr();
3475 pstrcpy(query_host, poptGetOptArg(pc));
3478 pstrcpy(term_code, poptGetOptArg(pc));
3481 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
3484 /* We must use old option processing for this. Find the
3485 * position of the -T option in the raw argv[]. */
3488 for (i = 1; i < argc; i++) {
3489 if (strncmp("-T", argv[i],2)==0)
3493 if (!(optnum = tar_parseargs(argc, argv, poptGetOptArg(pc), i))) {
3494 poptPrintUsage(pc, stderr, 0);
3497 /* Now we must eat (optnum - i) options - they have
3498 * been processed by tar_parseargs().
3501 for (i = 0; i < optnum; i++)
3506 pstrcpy(base_directory,poptGetOptArg(pc));
3516 /* check for the -P option */
3519 cli_cm_set_port( port );
3522 * Don't load debug level from smb.conf. It should be
3523 * set by cmdline arg or remain default (0)
3525 AllowDebugChange = False;
3527 /* save the workgroup...
3529 FIXME!! do we need to do this for other options as well
3530 (or maybe a generic way to keep lp_load() from overwriting
3533 fstrcpy( new_workgroup, lp_workgroup() );
3534 pstrcpy( calling_name, global_myname() );
3536 if ( override_logfile )
3537 setup_logging( lp_logfile(), False );
3539 if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
3540 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
3541 argv[0], dyn_CONFIGFILE);
3546 if ( strlen(new_workgroup) != 0 )
3547 set_global_myworkgroup( new_workgroup );
3549 if ( strlen(calling_name) != 0 )
3550 set_global_myname( calling_name );
3552 pstrcpy( calling_name, global_myname() );
3554 if(poptPeekArg(pc)) {
3555 pstrcpy(service,poptGetArg(pc));
3556 /* Convert any '/' characters in the service name to '\' characters */
3557 string_replace(service, '/','\\');
3559 if (count_chars(service,'\\') < 3) {
3560 d_printf("\n%s: Not enough '\\' characters in service\n",service);
3561 poptPrintUsage(pc, stderr, 0);
3566 if (poptPeekArg(pc) && !cmdline_auth_info.got_pass) {
3567 cmdline_auth_info.got_pass = True;
3568 pstrcpy(cmdline_auth_info.password,poptGetArg(pc));
3573 if(new_name_resolve_order)
3574 lp_set_name_resolve_order(new_name_resolve_order);
3576 if (!tar_type && !*query_host && !*service && !message) {
3577 poptPrintUsage(pc, stderr, 0);
3581 poptFreeContext(pc);
3583 /* store the username an password for dfs support */
3585 cli_cm_set_credentials( &cmdline_auth_info );
3586 pstrcpy(username, cmdline_auth_info.username);
3588 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
3592 process_command_string(cmdstr);
3593 return do_tar_op(base_directory);
3597 char *qhost = query_host;
3600 while (*qhost == '\\' || *qhost == '/')
3603 if ((slash = strchr_m(qhost, '/'))
3604 || (slash = strchr_m(qhost, '\\'))) {
3608 if ((p=strchr_m(qhost, '#'))) {
3611 sscanf(p, "%x", &name_type);
3612 cli_cm_set_dest_name_type( name_type );
3615 return do_host_query(qhost);
3619 return do_message_op();
3622 if (process(base_directory)) {