r6369: update release notes
[Samba.git] / source / client / client.c
blobbe17488919df38f08ad368f97dca275a9dd678b3
1 /*
2 Unix SMB/CIFS implementation.
3 SMB client
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Simo Sorce 2001-2002
6 Copyright (C) Jelmer Vernooij 2003
7 Copyright (C) Gerald (Jerry) Carter 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define NO_SYSLOG
26 #include "includes.h"
27 #include "client/client_proto.h"
28 #ifndef REGISTER
29 #define REGISTER 0
30 #endif
32 extern BOOL AllowDebugChange;
33 extern BOOL override_logfile;
34 extern char tar_type;
35 extern BOOL in_client;
36 static int port = 0;
37 pstring cur_dir = "\\";
38 static pstring cd_path = "";
39 static pstring service;
40 static pstring desthost;
41 static pstring username;
42 static pstring calling_name;
43 static BOOL grepable=False;
44 static char *cmdstr = NULL;
46 static int io_bufsize = 64512;
48 static int name_type = 0x20;
49 extern int max_protocol;
51 static int process_tok(pstring tok);
52 static int cmd_help(void);
54 /* 30 second timeout on most commands */
55 #define CLIENT_TIMEOUT (30*1000)
56 #define SHORT_TIMEOUT (5*1000)
58 /* value for unused fid field in trans2 secondary request */
59 #define FID_UNUSED (0xFFFF)
61 time_t newer_than = 0;
62 static int archive_level = 0;
64 static BOOL translation = False;
65 static BOOL have_ip;
67 /* clitar bits insert */
68 extern int blocksize;
69 extern BOOL tar_inc;
70 extern BOOL tar_reset;
71 /* clitar bits end */
74 static BOOL prompt = True;
76 static int printmode = 1;
78 static BOOL recurse = False;
79 BOOL lowercase = False;
81 static struct in_addr dest_ip;
83 #define SEPARATORS " \t\n\r"
85 static BOOL abort_mget = True;
87 static pstring fileselection = "";
89 extern file_info def_finfo;
91 /* timing globals */
92 SMB_BIG_UINT get_total_size = 0;
93 unsigned int get_total_time_ms = 0;
94 static SMB_BIG_UINT put_total_size = 0;
95 static unsigned int put_total_time_ms = 0;
97 /* totals globals */
98 static double dir_total;
100 /* root cli_state connection */
102 struct cli_state *cli;
106 /****************************************************************************
107 Write to a local file with CR/LF->LF translation if appropriate. Return the
108 number taken from the buffer. This may not equal the number written.
109 ****************************************************************************/
111 static int writefile(int f, char *b, int n)
113 int i;
115 if (!translation) {
116 return write(f,b,n);
119 i = 0;
120 while (i < n) {
121 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
122 b++;i++;
124 if (write(f, b, 1) != 1) {
125 break;
127 b++;
128 i++;
131 return(i);
134 /****************************************************************************
135 Read from a file with LF->CR/LF translation if appropriate. Return the
136 number read. read approx n bytes.
137 ****************************************************************************/
139 static int readfile(char *b, int n, XFILE *f)
141 int i;
142 int c;
144 if (!translation)
145 return x_fread(b,1,n,f);
147 i = 0;
148 while (i < (n - 1) && (i < BUFFER_SIZE)) {
149 if ((c = x_getc(f)) == EOF) {
150 break;
153 if (c == '\n') { /* change all LFs to CR/LF */
154 b[i++] = '\r';
157 b[i++] = c;
160 return(i);
163 /****************************************************************************
164 Send a message.
165 ****************************************************************************/
167 static void send_message(void)
169 int total_len = 0;
170 int grp_id;
172 if (!cli_message_start(cli, desthost, username, &grp_id)) {
173 d_printf("message start: %s\n", cli_errstr(cli));
174 return;
178 d_printf("Connected. Type your message, ending it with a Control-D\n");
180 while (!feof(stdin) && total_len < 1600) {
181 int maxlen = MIN(1600 - total_len,127);
182 pstring msg;
183 int l=0;
184 int c;
186 ZERO_ARRAY(msg);
188 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
189 if (c == '\n')
190 msg[l++] = '\r';
191 msg[l] = c;
194 if (!cli_message_text(cli, msg, l, grp_id)) {
195 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
196 return;
199 total_len += l;
202 if (total_len >= 1600)
203 d_printf("the message was truncated to 1600 bytes\n");
204 else
205 d_printf("sent %d bytes\n",total_len);
207 if (!cli_message_end(cli, grp_id)) {
208 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
209 return;
213 /****************************************************************************
214 Check the space on a device.
215 ****************************************************************************/
217 static int do_dskattr(void)
219 int total, bsize, avail;
220 struct cli_state *targetcli;
221 pstring targetpath;
223 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
224 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
225 return 1;
228 if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
229 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli));
230 return 1;
233 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
234 total, bsize, avail);
236 return 0;
239 /****************************************************************************
240 Show cd/pwd.
241 ****************************************************************************/
243 static int cmd_pwd(void)
245 d_printf("Current directory is %s",service);
246 d_printf("%s\n",cur_dir);
247 return 0;
250 /****************************************************************************
251 Change directory - inner section.
252 ****************************************************************************/
254 static int do_cd(char *newdir)
256 char *p = newdir;
257 pstring saved_dir;
258 pstring dname;
259 pstring targetpath;
260 struct cli_state *targetcli;
261 SMB_STRUCT_STAT sbuf;
262 uint32 attributes;
263 int ret = 1;
265 dos_format(newdir);
267 /* Save the current directory in case the new directory is invalid */
269 pstrcpy(saved_dir, cur_dir);
271 if (*p == '\\')
272 pstrcpy(cur_dir,p);
273 else
274 pstrcat(cur_dir,p);
276 if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
277 pstrcat(cur_dir, "\\");
280 dos_clean_name(cur_dir);
281 pstrcpy( dname, cur_dir );
282 pstrcat(cur_dir,"\\");
283 dos_clean_name(cur_dir);
285 if ( !cli_resolve_path( "", cli, dname, &targetcli, targetpath ) ) {
286 d_printf("cd %s: %s\n", dname, cli_errstr(cli));
287 pstrcpy(cur_dir,saved_dir);
288 goto out;
292 if ( strequal(targetpath,"\\" ) )
293 return 0;
295 /* use a trans2_qpathinfo to test directories for modern servers */
297 if ( targetcli->protocol >= PROTOCOL_LANMAN2 ) {
298 if ( !cli_qpathinfo_basic( targetcli, targetpath, &sbuf, &attributes ) ) {
299 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
300 pstrcpy(cur_dir,saved_dir);
301 goto out;
304 if ( !(attributes&FILE_ATTRIBUTE_DIRECTORY) ) {
305 d_printf("cd %s: not a directory\n", dname);
306 pstrcpy(cur_dir,saved_dir);
307 goto out;
309 } else {
310 pstrcat( targetpath, "\\" );
311 dos_clean_name( targetpath );
313 if ( !cli_chkpath(targetcli, targetpath) ) {
314 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
315 pstrcpy(cur_dir,saved_dir);
316 goto out;
320 ret = 0;
322 out:
324 pstrcpy(cd_path,cur_dir);
325 return ret;
328 /****************************************************************************
329 Change directory.
330 ****************************************************************************/
332 static int cmd_cd(void)
334 pstring buf;
335 int rc = 0;
337 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
338 rc = do_cd(buf);
339 else
340 d_printf("Current directory is %s\n",cur_dir);
342 return rc;
345 /*******************************************************************
346 Decide if a file should be operated on.
347 ********************************************************************/
349 static BOOL do_this_one(file_info *finfo)
351 if (finfo->mode & aDIR)
352 return(True);
354 if (*fileselection &&
355 !mask_match(finfo->name,fileselection,False)) {
356 DEBUG(3,("mask_match %s failed\n", finfo->name));
357 return False;
360 if (newer_than && finfo->mtime < newer_than) {
361 DEBUG(3,("newer_than %s failed\n", finfo->name));
362 return(False);
365 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
366 DEBUG(3,("archive %s failed\n", finfo->name));
367 return(False);
370 return(True);
373 /****************************************************************************
374 Display info about a file.
375 ****************************************************************************/
377 static void display_finfo(file_info *finfo)
379 if (do_this_one(finfo)) {
380 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
381 d_printf(" %-30s%7.7s %8.0f %s",
382 finfo->name,
383 attrib_string(finfo->mode),
384 (double)finfo->size,
385 asctime(LocalTime(&t)));
386 dir_total += finfo->size;
390 /****************************************************************************
391 Accumulate size of a file.
392 ****************************************************************************/
394 static void do_du(file_info *finfo)
396 if (do_this_one(finfo)) {
397 dir_total += finfo->size;
401 static BOOL do_list_recurse;
402 static BOOL do_list_dirs;
403 static char *do_list_queue = 0;
404 static long do_list_queue_size = 0;
405 static long do_list_queue_start = 0;
406 static long do_list_queue_end = 0;
407 static void (*do_list_fn)(file_info *);
409 /****************************************************************************
410 Functions for do_list_queue.
411 ****************************************************************************/
414 * The do_list_queue is a NUL-separated list of strings stored in a
415 * char*. Since this is a FIFO, we keep track of the beginning and
416 * ending locations of the data in the queue. When we overflow, we
417 * double the size of the char*. When the start of the data passes
418 * the midpoint, we move everything back. This is logically more
419 * complex than a linked list, but easier from a memory management
420 * angle. In any memory error condition, do_list_queue is reset.
421 * Functions check to ensure that do_list_queue is non-NULL before
422 * accessing it.
425 static void reset_do_list_queue(void)
427 SAFE_FREE(do_list_queue);
428 do_list_queue_size = 0;
429 do_list_queue_start = 0;
430 do_list_queue_end = 0;
433 static void init_do_list_queue(void)
435 reset_do_list_queue();
436 do_list_queue_size = 1024;
437 do_list_queue = SMB_MALLOC(do_list_queue_size);
438 if (do_list_queue == 0) {
439 d_printf("malloc fail for size %d\n",
440 (int)do_list_queue_size);
441 reset_do_list_queue();
442 } else {
443 memset(do_list_queue, 0, do_list_queue_size);
447 static void adjust_do_list_queue(void)
450 * If the starting point of the queue is more than half way through,
451 * move everything toward the beginning.
453 if (do_list_queue && (do_list_queue_start == do_list_queue_end)) {
454 DEBUG(4,("do_list_queue is empty\n"));
455 do_list_queue_start = do_list_queue_end = 0;
456 *do_list_queue = '\0';
457 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
458 DEBUG(4,("sliding do_list_queue backward\n"));
459 memmove(do_list_queue,
460 do_list_queue + do_list_queue_start,
461 do_list_queue_end - do_list_queue_start);
462 do_list_queue_end -= do_list_queue_start;
463 do_list_queue_start = 0;
467 static void add_to_do_list_queue(const char* entry)
469 char *dlq;
470 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
471 while (new_end > do_list_queue_size) {
472 do_list_queue_size *= 2;
473 DEBUG(4,("enlarging do_list_queue to %d\n",
474 (int)do_list_queue_size));
475 dlq = SMB_REALLOC(do_list_queue, do_list_queue_size);
476 if (! dlq) {
477 d_printf("failure enlarging do_list_queue to %d bytes\n",
478 (int)do_list_queue_size);
479 reset_do_list_queue();
480 } else {
481 do_list_queue = dlq;
482 memset(do_list_queue + do_list_queue_size / 2,
483 0, do_list_queue_size / 2);
486 if (do_list_queue) {
487 safe_strcpy_base(do_list_queue + do_list_queue_end,
488 entry, do_list_queue, do_list_queue_size);
489 do_list_queue_end = new_end;
490 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
491 entry, (int)do_list_queue_start, (int)do_list_queue_end));
495 static char *do_list_queue_head(void)
497 return do_list_queue + do_list_queue_start;
500 static void remove_do_list_queue_head(void)
502 if (do_list_queue_end > do_list_queue_start) {
503 do_list_queue_start += strlen(do_list_queue_head()) + 1;
504 adjust_do_list_queue();
505 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
506 (int)do_list_queue_start, (int)do_list_queue_end));
510 static int do_list_queue_empty(void)
512 return (! (do_list_queue && *do_list_queue));
515 /****************************************************************************
516 A helper for do_list.
517 ****************************************************************************/
519 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
521 if (f->mode & aDIR) {
522 if (do_list_dirs && do_this_one(f)) {
523 do_list_fn(f);
525 if (do_list_recurse &&
526 !strequal(f->name,".") &&
527 !strequal(f->name,"..")) {
528 pstring mask2;
529 char *p;
531 if (!f->name[0]) {
532 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
533 return;
536 pstrcpy(mask2, mntpoint);
537 pstrcat(mask2, mask);
538 p = strrchr_m(mask2,'\\');
539 if (!p)
540 return;
541 p[1] = 0;
542 pstrcat(mask2, f->name);
543 pstrcat(mask2,"\\*");
544 add_to_do_list_queue(mask2);
546 return;
549 if (do_this_one(f)) {
550 do_list_fn(f);
554 /****************************************************************************
555 A wrapper around cli_list that adds recursion.
556 ****************************************************************************/
558 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
560 static int in_do_list = 0;
561 struct cli_state *targetcli;
562 pstring targetpath;
564 if (in_do_list && rec) {
565 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
566 exit(1);
569 in_do_list = 1;
571 do_list_recurse = rec;
572 do_list_dirs = dirs;
573 do_list_fn = fn;
575 if (rec) {
576 init_do_list_queue();
577 add_to_do_list_queue(mask);
579 while (! do_list_queue_empty()) {
581 * Need to copy head so that it doesn't become
582 * invalid inside the call to cli_list. This
583 * would happen if the list were expanded
584 * during the call.
585 * Fix from E. Jay Berkenbilt (ejb@ql.org)
587 pstring head;
588 pstrcpy(head, do_list_queue_head());
590 /* check for dfs */
592 if ( !cli_resolve_path( "", cli, head, &targetcli, targetpath ) ) {
593 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
594 remove_do_list_queue_head();
595 continue;
598 cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
599 remove_do_list_queue_head();
600 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
601 char* next_file = do_list_queue_head();
602 char* save_ch = 0;
603 if ((strlen(next_file) >= 2) &&
604 (next_file[strlen(next_file) - 1] == '*') &&
605 (next_file[strlen(next_file) - 2] == '\\')) {
606 save_ch = next_file +
607 strlen(next_file) - 2;
608 *save_ch = '\0';
610 d_printf("\n%s\n",next_file);
611 if (save_ch) {
612 *save_ch = '\\';
616 } else {
617 /* check for dfs */
619 if ( cli_resolve_path( "", cli, mask, &targetcli, targetpath ) ) {
620 if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1)
621 d_printf("%s listing %s\n", cli_errstr(targetcli), targetpath);
623 else
624 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
628 in_do_list = 0;
629 reset_do_list_queue();
632 /****************************************************************************
633 Get a directory listing.
634 ****************************************************************************/
636 static int cmd_dir(void)
638 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
639 pstring mask;
640 pstring buf;
641 char *p=buf;
642 int rc;
644 dir_total = 0;
645 if (strcmp(cur_dir, "\\") != 0) {
646 pstrcpy(mask,cur_dir);
647 if(mask[strlen(mask)-1]!='\\')
648 pstrcat(mask,"\\");
649 } else {
650 pstrcpy(mask, "\\");
653 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
654 dos_format(p);
655 if (*p == '\\')
656 pstrcpy(mask,p + 1);
657 else
658 pstrcat(mask,p);
659 } else {
660 pstrcat(mask,"*");
663 do_list(mask, attribute, display_finfo, recurse, True);
665 rc = do_dskattr();
667 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
669 return rc;
672 /****************************************************************************
673 Get a directory listing.
674 ****************************************************************************/
676 static int cmd_du(void)
678 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
679 pstring mask;
680 pstring buf;
681 char *p=buf;
682 int rc;
684 dir_total = 0;
685 pstrcpy(mask,cur_dir);
686 if(mask[strlen(mask)-1]!='\\')
687 pstrcat(mask,"\\");
689 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
690 dos_format(p);
691 if (*p == '\\')
692 pstrcpy(mask,p);
693 else
694 pstrcat(mask,p);
695 } else {
696 pstrcat(mask,"*");
699 do_list(mask, attribute, do_du, recurse, True);
701 rc = do_dskattr();
703 d_printf("Total number of bytes: %.0f\n", dir_total);
705 return rc;
708 /****************************************************************************
709 Get a file from rname to lname
710 ****************************************************************************/
712 static int do_get(char *rname, char *lname, BOOL reget)
714 int handle = 0, fnum;
715 BOOL newhandle = False;
716 char *data;
717 struct timeval tp_start;
718 int read_size = io_bufsize;
719 uint16 attr;
720 SMB_OFF_T size;
721 off_t start = 0;
722 off_t nread = 0;
723 int rc = 0;
724 struct cli_state *targetcli;
725 pstring targetname;
728 if (lowercase) {
729 strlower_m(lname);
732 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
733 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
734 return 1;
738 GetTimeOfDay(&tp_start);
740 fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
742 if (fnum == -1) {
743 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
744 return 1;
747 if(!strcmp(lname,"-")) {
748 handle = fileno(stdout);
749 } else {
750 if (reget) {
751 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
752 if (handle >= 0) {
753 start = sys_lseek(handle, 0, SEEK_END);
754 if (start == -1) {
755 d_printf("Error seeking local file\n");
756 return 1;
759 } else {
760 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
762 newhandle = True;
764 if (handle < 0) {
765 d_printf("Error opening local file %s\n",lname);
766 return 1;
770 if (!cli_qfileinfo(targetcli, fnum,
771 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
772 !cli_getattrE(targetcli, fnum,
773 &attr, &size, NULL, NULL, NULL)) {
774 d_printf("getattrib: %s\n",cli_errstr(targetcli));
775 return 1;
778 DEBUG(1,("getting file %s of size %.0f as %s ",
779 rname, (double)size, lname));
781 if(!(data = (char *)SMB_MALLOC(read_size))) {
782 d_printf("malloc fail for size %d\n", read_size);
783 cli_close(targetcli, fnum);
784 return 1;
787 while (1) {
788 int n = cli_read(targetcli, fnum, data, nread + start, read_size);
790 if (n <= 0)
791 break;
793 if (writefile(handle,data, n) != n) {
794 d_printf("Error writing local file\n");
795 rc = 1;
796 break;
799 nread += n;
802 if (nread + start < size) {
803 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
804 rname, (long)nread));
806 rc = 1;
809 SAFE_FREE(data);
811 if (!cli_close(targetcli, fnum)) {
812 d_printf("Error %s closing remote file\n",cli_errstr(cli));
813 rc = 1;
816 if (newhandle) {
817 close(handle);
820 if (archive_level >= 2 && (attr & aARCH)) {
821 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
825 struct timeval tp_end;
826 int this_time;
828 GetTimeOfDay(&tp_end);
829 this_time =
830 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
831 (tp_end.tv_usec - tp_start.tv_usec)/1000;
832 get_total_time_ms += this_time;
833 get_total_size += nread;
835 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
836 nread / (1.024*this_time + 1.0e-4),
837 get_total_size / (1.024*get_total_time_ms)));
840 return rc;
843 /****************************************************************************
844 Get a file.
845 ****************************************************************************/
847 static int cmd_get(void)
849 pstring lname;
850 pstring rname;
851 char *p;
853 pstrcpy(rname,cur_dir);
854 pstrcat(rname,"\\");
856 p = rname + strlen(rname);
858 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
859 d_printf("get <filename>\n");
860 return 1;
862 pstrcpy(lname,p);
863 dos_clean_name(rname);
865 next_token_nr(NULL,lname,NULL,sizeof(lname));
867 return do_get(rname, lname, False);
870 /****************************************************************************
871 Do an mget operation on one file.
872 ****************************************************************************/
874 static void do_mget(file_info *finfo)
876 pstring rname;
877 pstring quest;
878 pstring saved_curdir;
879 pstring mget_mask;
881 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
882 return;
884 if (abort_mget) {
885 d_printf("mget aborted\n");
886 return;
889 if (finfo->mode & aDIR)
890 slprintf(quest,sizeof(pstring)-1,
891 "Get directory %s? ",finfo->name);
892 else
893 slprintf(quest,sizeof(pstring)-1,
894 "Get file %s? ",finfo->name);
896 if (prompt && !yesno(quest))
897 return;
899 if (!(finfo->mode & aDIR)) {
900 pstrcpy(rname,cur_dir);
901 pstrcat(rname,finfo->name);
902 do_get(rname, finfo->name, False);
903 return;
906 /* handle directories */
907 pstrcpy(saved_curdir,cur_dir);
909 pstrcat(cur_dir,finfo->name);
910 pstrcat(cur_dir,"\\");
912 unix_format(finfo->name);
913 if (lowercase)
914 strlower_m(finfo->name);
916 if (!directory_exist(finfo->name,NULL) &&
917 mkdir(finfo->name,0777) != 0) {
918 d_printf("failed to create directory %s\n",finfo->name);
919 pstrcpy(cur_dir,saved_curdir);
920 return;
923 if (chdir(finfo->name) != 0) {
924 d_printf("failed to chdir to directory %s\n",finfo->name);
925 pstrcpy(cur_dir,saved_curdir);
926 return;
929 pstrcpy(mget_mask,cur_dir);
930 pstrcat(mget_mask,"*");
932 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
933 chdir("..");
934 pstrcpy(cur_dir,saved_curdir);
937 /****************************************************************************
938 View the file using the pager.
939 ****************************************************************************/
941 static int cmd_more(void)
943 pstring rname,lname,pager_cmd;
944 char *pager;
945 int fd;
946 int rc = 0;
948 pstrcpy(rname,cur_dir);
949 pstrcat(rname,"\\");
951 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
952 fd = smb_mkstemp(lname);
953 if (fd == -1) {
954 d_printf("failed to create temporary file for more\n");
955 return 1;
957 close(fd);
959 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
960 d_printf("more <filename>\n");
961 unlink(lname);
962 return 1;
964 dos_clean_name(rname);
966 rc = do_get(rname, lname, False);
968 pager=getenv("PAGER");
970 slprintf(pager_cmd,sizeof(pager_cmd)-1,
971 "%s %s",(pager? pager:PAGER), lname);
972 system(pager_cmd);
973 unlink(lname);
975 return rc;
978 /****************************************************************************
979 Do a mget command.
980 ****************************************************************************/
982 static int cmd_mget(void)
984 uint16 attribute = aSYSTEM | aHIDDEN;
985 pstring mget_mask;
986 pstring buf;
987 char *p=buf;
989 *mget_mask = 0;
991 if (recurse)
992 attribute |= aDIR;
994 abort_mget = False;
996 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
997 pstrcpy(mget_mask,cur_dir);
998 if(mget_mask[strlen(mget_mask)-1]!='\\')
999 pstrcat(mget_mask,"\\");
1001 if (*p == '\\')
1002 pstrcpy(mget_mask,p);
1003 else
1004 pstrcat(mget_mask,p);
1005 do_list(mget_mask, attribute,do_mget,False,True);
1008 if (!*mget_mask) {
1009 pstrcpy(mget_mask,cur_dir);
1010 if(mget_mask[strlen(mget_mask)-1]!='\\')
1011 pstrcat(mget_mask,"\\");
1012 pstrcat(mget_mask,"*");
1013 do_list(mget_mask, attribute,do_mget,False,True);
1016 return 0;
1019 /****************************************************************************
1020 Make a directory of name "name".
1021 ****************************************************************************/
1023 static BOOL do_mkdir(char *name)
1025 struct cli_state *targetcli;
1026 pstring targetname;
1028 if ( !cli_resolve_path( "", cli, name, &targetcli, targetname ) ) {
1029 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1030 return False;
1033 if (!cli_mkdir(targetcli, targetname)) {
1034 d_printf("%s making remote directory %s\n",
1035 cli_errstr(targetcli),name);
1036 return(False);
1039 return(True);
1042 /****************************************************************************
1043 Show 8.3 name of a file.
1044 ****************************************************************************/
1046 static BOOL do_altname(char *name)
1048 pstring altname;
1049 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1050 d_printf("%s getting alt name for %s\n",
1051 cli_errstr(cli),name);
1052 return(False);
1054 d_printf("%s\n", altname);
1056 return(True);
1059 /****************************************************************************
1060 Exit client.
1061 ****************************************************************************/
1063 static int cmd_quit(void)
1065 cli_cm_shutdown();
1066 exit(0);
1067 /* NOTREACHED */
1068 return 0;
1071 /****************************************************************************
1072 Make a directory.
1073 ****************************************************************************/
1075 static int cmd_mkdir(void)
1077 pstring mask;
1078 pstring buf;
1079 char *p=buf;
1081 pstrcpy(mask,cur_dir);
1083 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1084 if (!recurse)
1085 d_printf("mkdir <dirname>\n");
1086 return 1;
1088 pstrcat(mask,p);
1090 if (recurse) {
1091 pstring ddir;
1092 pstring ddir2;
1093 *ddir2 = 0;
1095 pstrcpy(ddir,mask);
1096 trim_char(ddir,'.','\0');
1097 p = strtok(ddir,"/\\");
1098 while (p) {
1099 pstrcat(ddir2,p);
1100 if (!cli_chkpath(cli, ddir2)) {
1101 do_mkdir(ddir2);
1103 pstrcat(ddir2,"\\");
1104 p = strtok(NULL,"/\\");
1106 } else {
1107 do_mkdir(mask);
1110 return 0;
1113 /****************************************************************************
1114 Show alt name.
1115 ****************************************************************************/
1117 static int cmd_altname(void)
1119 pstring name;
1120 pstring buf;
1121 char *p=buf;
1123 pstrcpy(name,cur_dir);
1125 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1126 d_printf("altname <file>\n");
1127 return 1;
1129 pstrcat(name,p);
1131 do_altname(name);
1133 return 0;
1136 /****************************************************************************
1137 Put a single file.
1138 ****************************************************************************/
1140 static int do_put(char *rname, char *lname, BOOL reput)
1142 int fnum;
1143 XFILE *f;
1144 SMB_OFF_T start = 0;
1145 off_t nread = 0;
1146 char *buf = NULL;
1147 int maxwrite = io_bufsize;
1148 int rc = 0;
1149 struct timeval tp_start;
1150 struct cli_state *targetcli;
1151 pstring targetname;
1153 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
1154 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1155 return 1;
1158 GetTimeOfDay(&tp_start);
1160 if (reput) {
1161 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
1162 if (fnum >= 0) {
1163 if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1164 !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
1165 d_printf("getattrib: %s\n",cli_errstr(cli));
1166 return 1;
1169 } else {
1170 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1173 if (fnum == -1) {
1174 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1175 return 1;
1178 /* allow files to be piped into smbclient
1179 jdblair 24.jun.98
1181 Note that in this case this function will exit(0) rather
1182 than returning. */
1183 if (!strcmp(lname, "-")) {
1184 f = x_stdin;
1185 /* size of file is not known */
1186 } else {
1187 f = x_fopen(lname,O_RDONLY, 0);
1188 if (f && reput) {
1189 if (x_tseek(f, start, SEEK_SET) == -1) {
1190 d_printf("Error seeking local file\n");
1191 return 1;
1196 if (!f) {
1197 d_printf("Error opening local file %s\n",lname);
1198 return 1;
1201 DEBUG(1,("putting file %s as %s ",lname,
1202 rname));
1204 buf = (char *)SMB_MALLOC(maxwrite);
1205 if (!buf) {
1206 d_printf("ERROR: Not enough memory!\n");
1207 return 1;
1209 while (!x_feof(f)) {
1210 int n = maxwrite;
1211 int ret;
1213 if ((n = readfile(buf,n,f)) < 1) {
1214 if((n == 0) && x_feof(f))
1215 break; /* Empty local file. */
1217 d_printf("Error reading local file: %s\n", strerror(errno));
1218 rc = 1;
1219 break;
1222 ret = cli_write(targetcli, fnum, 0, buf, nread + start, n);
1224 if (n != ret) {
1225 d_printf("Error writing file: %s\n", cli_errstr(cli));
1226 rc = 1;
1227 break;
1230 nread += n;
1233 if (!cli_close(targetcli, fnum)) {
1234 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1235 x_fclose(f);
1236 SAFE_FREE(buf);
1237 return 1;
1241 if (f != x_stdin) {
1242 x_fclose(f);
1245 SAFE_FREE(buf);
1248 struct timeval tp_end;
1249 int this_time;
1251 GetTimeOfDay(&tp_end);
1252 this_time =
1253 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1254 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1255 put_total_time_ms += this_time;
1256 put_total_size += nread;
1258 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1259 nread / (1.024*this_time + 1.0e-4),
1260 put_total_size / (1.024*put_total_time_ms)));
1263 if (f == x_stdin) {
1264 cli_cm_shutdown();
1265 exit(0);
1268 return rc;
1271 /****************************************************************************
1272 Put a file.
1273 ****************************************************************************/
1275 static int cmd_put(void)
1277 pstring lname;
1278 pstring rname;
1279 pstring buf;
1280 char *p=buf;
1282 pstrcpy(rname,cur_dir);
1283 pstrcat(rname,"\\");
1285 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1286 d_printf("put <filename>\n");
1287 return 1;
1289 pstrcpy(lname,p);
1291 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1292 pstrcat(rname,p);
1293 else
1294 pstrcat(rname,lname);
1296 dos_clean_name(rname);
1299 SMB_STRUCT_STAT st;
1300 /* allow '-' to represent stdin
1301 jdblair, 24.jun.98 */
1302 if (!file_exist(lname,&st) &&
1303 (strcmp(lname,"-"))) {
1304 d_printf("%s does not exist\n",lname);
1305 return 1;
1309 return do_put(rname, lname, False);
1312 /*************************************
1313 File list structure.
1314 *************************************/
1316 static struct file_list {
1317 struct file_list *prev, *next;
1318 char *file_path;
1319 BOOL isdir;
1320 } *file_list;
1322 /****************************************************************************
1323 Free a file_list structure.
1324 ****************************************************************************/
1326 static void free_file_list (struct file_list * list)
1328 struct file_list *tmp;
1330 while (list) {
1331 tmp = list;
1332 DLIST_REMOVE(list, list);
1333 SAFE_FREE(tmp->file_path);
1334 SAFE_FREE(tmp);
1338 /****************************************************************************
1339 Seek in a directory/file list until you get something that doesn't start with
1340 the specified name.
1341 ****************************************************************************/
1343 static BOOL seek_list(struct file_list *list, char *name)
1345 while (list) {
1346 trim_string(list->file_path,"./","\n");
1347 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1348 return(True);
1350 list = list->next;
1353 return(False);
1356 /****************************************************************************
1357 Set the file selection mask.
1358 ****************************************************************************/
1360 static int cmd_select(void)
1362 pstrcpy(fileselection,"");
1363 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1365 return 0;
1368 /****************************************************************************
1369 Recursive file matching function act as find
1370 match must be always set to True when calling this function
1371 ****************************************************************************/
1373 static int file_find(struct file_list **list, const char *directory,
1374 const char *expression, BOOL match)
1376 DIR *dir;
1377 struct file_list *entry;
1378 struct stat statbuf;
1379 int ret;
1380 char *path;
1381 BOOL isdir;
1382 const char *dname;
1384 dir = opendir(directory);
1385 if (!dir)
1386 return -1;
1388 while ((dname = readdirname(dir))) {
1389 if (!strcmp("..", dname))
1390 continue;
1391 if (!strcmp(".", dname))
1392 continue;
1394 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1395 continue;
1398 isdir = False;
1399 if (!match || !gen_fnmatch(expression, dname)) {
1400 if (recurse) {
1401 ret = stat(path, &statbuf);
1402 if (ret == 0) {
1403 if (S_ISDIR(statbuf.st_mode)) {
1404 isdir = True;
1405 ret = file_find(list, path, expression, False);
1407 } else {
1408 d_printf("file_find: cannot stat file %s\n", path);
1411 if (ret == -1) {
1412 SAFE_FREE(path);
1413 closedir(dir);
1414 return -1;
1417 entry = SMB_MALLOC_P(struct file_list);
1418 if (!entry) {
1419 d_printf("Out of memory in file_find\n");
1420 closedir(dir);
1421 return -1;
1423 entry->file_path = path;
1424 entry->isdir = isdir;
1425 DLIST_ADD(*list, entry);
1426 } else {
1427 SAFE_FREE(path);
1431 closedir(dir);
1432 return 0;
1435 /****************************************************************************
1436 mput some files.
1437 ****************************************************************************/
1439 static int cmd_mput(void)
1441 pstring buf;
1442 char *p=buf;
1444 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1445 int ret;
1446 struct file_list *temp_list;
1447 char *quest, *lname, *rname;
1449 file_list = NULL;
1451 ret = file_find(&file_list, ".", p, True);
1452 if (ret) {
1453 free_file_list(file_list);
1454 continue;
1457 quest = NULL;
1458 lname = NULL;
1459 rname = NULL;
1461 for (temp_list = file_list; temp_list;
1462 temp_list = temp_list->next) {
1464 SAFE_FREE(lname);
1465 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1466 continue;
1467 trim_string(lname, "./", "/");
1469 /* check if it's a directory */
1470 if (temp_list->isdir) {
1471 /* if (!recurse) continue; */
1473 SAFE_FREE(quest);
1474 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1475 if (prompt && !yesno(quest)) { /* No */
1476 /* Skip the directory */
1477 lname[strlen(lname)-1] = '/';
1478 if (!seek_list(temp_list, lname))
1479 break;
1480 } else { /* Yes */
1481 SAFE_FREE(rname);
1482 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1483 dos_format(rname);
1484 if (!cli_chkpath(cli, rname) &&
1485 !do_mkdir(rname)) {
1486 DEBUG (0, ("Unable to make dir, skipping..."));
1487 /* Skip the directory */
1488 lname[strlen(lname)-1] = '/';
1489 if (!seek_list(temp_list, lname))
1490 break;
1493 continue;
1494 } else {
1495 SAFE_FREE(quest);
1496 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1497 if (prompt && !yesno(quest)) /* No */
1498 continue;
1500 /* Yes */
1501 SAFE_FREE(rname);
1502 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1505 dos_format(rname);
1507 do_put(rname, lname, False);
1509 free_file_list(file_list);
1510 SAFE_FREE(quest);
1511 SAFE_FREE(lname);
1512 SAFE_FREE(rname);
1515 return 0;
1518 /****************************************************************************
1519 Cancel a print job.
1520 ****************************************************************************/
1522 static int do_cancel(int job)
1524 if (cli_printjob_del(cli, job)) {
1525 d_printf("Job %d cancelled\n",job);
1526 return 0;
1527 } else {
1528 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1529 return 1;
1533 /****************************************************************************
1534 Cancel a print job.
1535 ****************************************************************************/
1537 static int cmd_cancel(void)
1539 pstring buf;
1540 int job;
1542 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1543 d_printf("cancel <jobid> ...\n");
1544 return 1;
1546 do {
1547 job = atoi(buf);
1548 do_cancel(job);
1549 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1551 return 0;
1554 /****************************************************************************
1555 Print a file.
1556 ****************************************************************************/
1558 static int cmd_print(void)
1560 pstring lname;
1561 pstring rname;
1562 char *p;
1564 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1565 d_printf("print <filename>\n");
1566 return 1;
1569 pstrcpy(rname,lname);
1570 p = strrchr_m(rname,'/');
1571 if (p) {
1572 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1575 if (strequal(lname,"-")) {
1576 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1579 return do_put(rname, lname, False);
1582 /****************************************************************************
1583 Show a print queue entry.
1584 ****************************************************************************/
1586 static void queue_fn(struct print_job_info *p)
1588 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
1591 /****************************************************************************
1592 Show a print queue.
1593 ****************************************************************************/
1595 static int cmd_queue(void)
1597 cli_print_queue(cli, queue_fn);
1599 return 0;
1602 /****************************************************************************
1603 Delete some files.
1604 ****************************************************************************/
1606 static void do_del(file_info *finfo)
1608 pstring mask;
1610 pstrcpy(mask,cur_dir);
1611 pstrcat(mask,finfo->name);
1613 if (finfo->mode & aDIR)
1614 return;
1616 if (!cli_unlink(cli, mask)) {
1617 d_printf("%s deleting remote file %s\n",cli_errstr(cli),mask);
1621 /****************************************************************************
1622 Delete some files.
1623 ****************************************************************************/
1625 static int cmd_del(void)
1627 pstring mask;
1628 pstring buf;
1629 uint16 attribute = aSYSTEM | aHIDDEN;
1631 if (recurse)
1632 attribute |= aDIR;
1634 pstrcpy(mask,cur_dir);
1636 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1637 d_printf("del <filename>\n");
1638 return 1;
1640 pstrcat(mask,buf);
1642 do_list(mask, attribute,do_del,False,False);
1644 return 0;
1647 /****************************************************************************
1648 ****************************************************************************/
1650 static int cmd_open(void)
1652 pstring mask;
1653 pstring buf;
1654 struct cli_state *targetcli;
1655 pstring targetname;
1657 pstrcpy(mask,cur_dir);
1659 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1660 d_printf("open <filename>\n");
1661 return 1;
1663 pstrcat(mask,buf);
1665 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1666 d_printf("open %s: %s\n", mask, cli_errstr(cli));
1667 return 1;
1670 cli_nt_create(targetcli, targetname, FILE_READ_DATA);
1672 return 0;
1676 /****************************************************************************
1677 Remove a directory.
1678 ****************************************************************************/
1680 static int cmd_rmdir(void)
1682 pstring mask;
1683 pstring buf;
1684 struct cli_state *targetcli;
1685 pstring targetname;
1687 pstrcpy(mask,cur_dir);
1689 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1690 d_printf("rmdir <dirname>\n");
1691 return 1;
1693 pstrcat(mask,buf);
1695 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1696 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
1697 return 1;
1700 if (!cli_rmdir(targetcli, targetname)) {
1701 d_printf("%s removing remote directory file %s\n",
1702 cli_errstr(targetcli),mask);
1705 return 0;
1708 /****************************************************************************
1709 UNIX hardlink.
1710 ****************************************************************************/
1712 static int cmd_link(void)
1714 pstring oldname,newname;
1715 pstring buf,buf2;
1716 struct cli_state *targetcli;
1717 pstring targetname;
1719 pstrcpy(oldname,cur_dir);
1720 pstrcpy(newname,cur_dir);
1722 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1723 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1724 d_printf("link <oldname> <newname>\n");
1725 return 1;
1728 pstrcat(oldname,buf);
1729 pstrcat(newname,buf2);
1731 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
1732 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
1733 return 1;
1736 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1737 d_printf("Server doesn't support UNIX CIFS calls.\n");
1738 return 1;
1741 if (!cli_unix_hardlink(targetcli, targetname, newname)) {
1742 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
1743 return 1;
1746 return 0;
1749 /****************************************************************************
1750 UNIX symlink.
1751 ****************************************************************************/
1753 static int cmd_symlink(void)
1755 pstring oldname,newname;
1756 pstring buf,buf2;
1758 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1759 d_printf("Server doesn't support UNIX CIFS calls.\n");
1760 return 1;
1763 pstrcpy(newname,cur_dir);
1765 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1766 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1767 d_printf("symlink <oldname> <newname>\n");
1768 return 1;
1771 pstrcpy(oldname,buf);
1772 pstrcat(newname,buf2);
1774 if (!cli_unix_symlink(cli, oldname, newname)) {
1775 d_printf("%s symlinking files (%s -> %s)\n",
1776 cli_errstr(cli), newname, oldname);
1777 return 1;
1780 return 0;
1783 /****************************************************************************
1784 UNIX chmod.
1785 ****************************************************************************/
1787 static int cmd_chmod(void)
1789 pstring src;
1790 mode_t mode;
1791 pstring buf, buf2;
1792 struct cli_state *targetcli;
1793 pstring targetname;
1795 pstrcpy(src,cur_dir);
1797 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1798 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1799 d_printf("chmod mode file\n");
1800 return 1;
1803 mode = (mode_t)strtol(buf, NULL, 8);
1804 pstrcat(src,buf2);
1806 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
1807 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
1808 return 1;
1811 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1812 d_printf("Server doesn't support UNIX CIFS calls.\n");
1813 return 1;
1816 if (!cli_unix_chmod(targetcli, targetname, mode)) {
1817 d_printf("%s chmod file %s 0%o\n",
1818 cli_errstr(targetcli), src, (unsigned int)mode);
1819 return 1;
1822 return 0;
1825 static const char *filetype_to_str(mode_t mode)
1827 if (S_ISREG(mode)) {
1828 return "regular file";
1829 } else if (S_ISDIR(mode)) {
1830 return "directory";
1831 } else
1832 #ifdef S_ISCHR
1833 if (S_ISCHR(mode)) {
1834 return "character device";
1835 } else
1836 #endif
1837 #ifdef S_ISBLK
1838 if (S_ISBLK(mode)) {
1839 return "block device";
1840 } else
1841 #endif
1842 #ifdef S_ISFIFO
1843 if (S_ISFIFO(mode)) {
1844 return "fifo";
1845 } else
1846 #endif
1847 #ifdef S_ISLNK
1848 if (S_ISLNK(mode)) {
1849 return "symbolic link";
1850 } else
1851 #endif
1852 #ifdef S_ISSOCK
1853 if (S_ISSOCK(mode)) {
1854 return "socket";
1855 } else
1856 #endif
1857 return "";
1860 static char rwx_to_str(mode_t m, mode_t bt, char ret)
1862 if (m & bt) {
1863 return ret;
1864 } else {
1865 return '-';
1869 static char *unix_mode_to_str(char *s, mode_t m)
1871 char *p = s;
1872 const char *str = filetype_to_str(m);
1874 switch(str[0]) {
1875 case 'd':
1876 *p++ = 'd';
1877 break;
1878 case 'c':
1879 *p++ = 'c';
1880 break;
1881 case 'b':
1882 *p++ = 'b';
1883 break;
1884 case 'f':
1885 *p++ = 'p';
1886 break;
1887 case 's':
1888 *p++ = str[1] == 'y' ? 'l' : 's';
1889 break;
1890 case 'r':
1891 default:
1892 *p++ = '-';
1893 break;
1895 *p++ = rwx_to_str(m, S_IRUSR, 'r');
1896 *p++ = rwx_to_str(m, S_IWUSR, 'w');
1897 *p++ = rwx_to_str(m, S_IXUSR, 'x');
1898 *p++ = rwx_to_str(m, S_IRGRP, 'r');
1899 *p++ = rwx_to_str(m, S_IWGRP, 'w');
1900 *p++ = rwx_to_str(m, S_IXGRP, 'x');
1901 *p++ = rwx_to_str(m, S_IROTH, 'r');
1902 *p++ = rwx_to_str(m, S_IWOTH, 'w');
1903 *p++ = rwx_to_str(m, S_IXOTH, 'x');
1904 *p++ = '\0';
1905 return s;
1908 /****************************************************************************
1909 Utility function for UNIX getfacl.
1910 ****************************************************************************/
1912 static char *perms_to_string(fstring permstr, unsigned char perms)
1914 fstrcpy(permstr, "---");
1915 if (perms & SMB_POSIX_ACL_READ) {
1916 permstr[0] = 'r';
1918 if (perms & SMB_POSIX_ACL_WRITE) {
1919 permstr[1] = 'w';
1921 if (perms & SMB_POSIX_ACL_EXECUTE) {
1922 permstr[2] = 'x';
1924 return permstr;
1927 /****************************************************************************
1928 UNIX getfacl.
1929 ****************************************************************************/
1931 static int cmd_getfacl(void)
1933 pstring src, name;
1934 uint16 major, minor;
1935 uint32 caplow, caphigh;
1936 char *retbuf = NULL;
1937 size_t rb_size = 0;
1938 SMB_STRUCT_STAT sbuf;
1939 uint16 num_file_acls = 0;
1940 uint16 num_dir_acls = 0;
1941 uint16 i;
1942 struct cli_state *targetcli;
1943 pstring targetname;
1945 pstrcpy(src,cur_dir);
1947 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
1948 d_printf("stat file\n");
1949 return 1;
1952 pstrcat(src,name);
1954 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
1955 d_printf("stat %s: %s\n", src, cli_errstr(cli));
1956 return 1;
1959 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1960 d_printf("Server doesn't support UNIX CIFS calls.\n");
1961 return 1;
1964 if (!cli_unix_extensions_version(targetcli, &major, &minor, &caplow, &caphigh)) {
1965 d_printf("Can't get UNIX CIFS version from server.\n");
1966 return 1;
1969 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
1970 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
1971 return 1;
1975 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
1976 d_printf("%s getfacl doing a stat on file %s\n",
1977 cli_errstr(targetcli), src);
1978 return 1;
1981 if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
1982 d_printf("%s getfacl file %s\n",
1983 cli_errstr(targetcli), src);
1984 return 1;
1987 /* ToDo : Print out the ACL values. */
1988 if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
1989 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
1990 src, (unsigned int)CVAL(retbuf,0) );
1991 SAFE_FREE(retbuf);
1992 return 1;
1995 num_file_acls = SVAL(retbuf,2);
1996 num_dir_acls = SVAL(retbuf,4);
1997 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
1998 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
1999 src,
2000 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
2001 (unsigned int)rb_size);
2003 SAFE_FREE(retbuf);
2004 return 1;
2007 d_printf("# file: %s\n", src);
2008 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
2010 if (num_file_acls == 0 && num_dir_acls == 0) {
2011 d_printf("No acls found.\n");
2014 for (i = 0; i < num_file_acls; i++) {
2015 uint32 uorg;
2016 fstring permstring;
2017 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
2018 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2020 switch(tagtype) {
2021 case SMB_POSIX_ACL_USER_OBJ:
2022 d_printf("user::");
2023 break;
2024 case SMB_POSIX_ACL_USER:
2025 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2026 d_printf("user:%u:", uorg);
2027 break;
2028 case SMB_POSIX_ACL_GROUP_OBJ:
2029 d_printf("group::");
2030 break;
2031 case SMB_POSIX_ACL_GROUP:
2032 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2033 d_printf("group:%u", uorg);
2034 break;
2035 case SMB_POSIX_ACL_MASK:
2036 d_printf("mask::");
2037 break;
2038 case SMB_POSIX_ACL_OTHER:
2039 d_printf("other::");
2040 break;
2041 default:
2042 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2043 src, (unsigned int)tagtype );
2044 SAFE_FREE(retbuf);
2045 return 1;
2048 d_printf("%s\n", perms_to_string(permstring, perms));
2051 for (i = 0; i < num_dir_acls; i++) {
2052 uint32 uorg;
2053 fstring permstring;
2054 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
2055 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2057 switch(tagtype) {
2058 case SMB_POSIX_ACL_USER_OBJ:
2059 d_printf("default:user::");
2060 break;
2061 case SMB_POSIX_ACL_USER:
2062 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2063 d_printf("default:user:%u:", uorg);
2064 break;
2065 case SMB_POSIX_ACL_GROUP_OBJ:
2066 d_printf("default:group::");
2067 break;
2068 case SMB_POSIX_ACL_GROUP:
2069 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2070 d_printf("default:group:%u", uorg);
2071 break;
2072 case SMB_POSIX_ACL_MASK:
2073 d_printf("default:mask::");
2074 break;
2075 case SMB_POSIX_ACL_OTHER:
2076 d_printf("default:other::");
2077 break;
2078 default:
2079 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2080 src, (unsigned int)tagtype );
2081 SAFE_FREE(retbuf);
2082 return 1;
2085 d_printf("%s\n", perms_to_string(permstring, perms));
2088 SAFE_FREE(retbuf);
2089 return 0;
2092 /****************************************************************************
2093 UNIX stat.
2094 ****************************************************************************/
2096 static int cmd_stat(void)
2098 pstring src, name;
2099 fstring mode_str;
2100 SMB_STRUCT_STAT sbuf;
2101 struct cli_state *targetcli;
2102 pstring targetname;
2104 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2105 d_printf("Server doesn't support UNIX CIFS calls.\n");
2106 return 1;
2109 pstrcpy(src,cur_dir);
2111 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2112 d_printf("stat file\n");
2113 return 1;
2116 pstrcat(src,name);
2119 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2120 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2121 return 1;
2124 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2125 d_printf("%s stat file %s\n",
2126 cli_errstr(targetcli), src);
2127 return 1;
2130 /* Print out the stat values. */
2131 d_printf("File: %s\n", src);
2132 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2133 (double)sbuf.st_size,
2134 (unsigned int)sbuf.st_blocks,
2135 filetype_to_str(sbuf.st_mode));
2137 #if defined(S_ISCHR) && defined(S_ISBLK)
2138 if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
2139 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2140 (double)sbuf.st_ino,
2141 (unsigned int)sbuf.st_nlink,
2142 unix_dev_major(sbuf.st_rdev),
2143 unix_dev_minor(sbuf.st_rdev));
2144 } else
2145 #endif
2146 d_printf("Inode: %.0f\tLinks: %u\n",
2147 (double)sbuf.st_ino,
2148 (unsigned int)sbuf.st_nlink);
2150 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2151 ((int)sbuf.st_mode & 0777),
2152 unix_mode_to_str(mode_str, sbuf.st_mode),
2153 (unsigned int)sbuf.st_uid,
2154 (unsigned int)sbuf.st_gid);
2156 strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_atime));
2157 d_printf("Access: %s\n", mode_str);
2159 strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_mtime));
2160 d_printf("Modify: %s\n", mode_str);
2162 strftime(mode_str, sizeof(mode_str), "%F %T %z", localtime(&sbuf.st_ctime));
2163 d_printf("Change: %s\n", mode_str);
2165 return 0;
2169 /****************************************************************************
2170 UNIX chown.
2171 ****************************************************************************/
2173 static int cmd_chown(void)
2175 pstring src;
2176 uid_t uid;
2177 gid_t gid;
2178 pstring buf, buf2, buf3;
2179 struct cli_state *targetcli;
2180 pstring targetname;
2182 pstrcpy(src,cur_dir);
2184 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2185 !next_token_nr(NULL,buf2,NULL, sizeof(buf2)) ||
2186 !next_token_nr(NULL,buf3,NULL, sizeof(buf3))) {
2187 d_printf("chown uid gid file\n");
2188 return 1;
2191 uid = (uid_t)atoi(buf);
2192 gid = (gid_t)atoi(buf2);
2193 pstrcat(src,buf3);
2195 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2196 d_printf("chown %s: %s\n", src, cli_errstr(cli));
2197 return 1;
2201 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2202 d_printf("Server doesn't support UNIX CIFS calls.\n");
2203 return 1;
2206 if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
2207 d_printf("%s chown file %s uid=%d, gid=%d\n",
2208 cli_errstr(targetcli), src, (int)uid, (int)gid);
2209 return 1;
2212 return 0;
2215 /****************************************************************************
2216 Rename some file.
2217 ****************************************************************************/
2219 static int cmd_rename(void)
2221 pstring src,dest;
2222 pstring buf,buf2;
2224 pstrcpy(src,cur_dir);
2225 pstrcpy(dest,cur_dir);
2227 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2228 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2229 d_printf("rename <src> <dest>\n");
2230 return 1;
2233 pstrcat(src,buf);
2234 pstrcat(dest,buf2);
2236 if (!cli_rename(cli, src, dest)) {
2237 d_printf("%s renaming files\n",cli_errstr(cli));
2238 return 1;
2241 return 0;
2244 /****************************************************************************
2245 Print the volume name.
2246 ****************************************************************************/
2248 static int cmd_volume(void)
2250 fstring volname;
2251 uint32 serial_num;
2252 time_t create_date;
2254 if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
2255 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
2256 return 1;
2259 d_printf("Volume: |%s| serial number 0x%x\n", volname, (unsigned int)serial_num);
2260 return 0;
2263 /****************************************************************************
2264 Hard link files using the NT call.
2265 ****************************************************************************/
2267 static int cmd_hardlink(void)
2269 pstring src,dest;
2270 pstring buf,buf2;
2271 struct cli_state *targetcli;
2272 pstring targetname;
2274 pstrcpy(src,cur_dir);
2275 pstrcpy(dest,cur_dir);
2277 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2278 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2279 d_printf("hardlink <src> <dest>\n");
2280 return 1;
2283 pstrcat(src,buf);
2284 pstrcat(dest,buf2);
2286 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2287 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
2288 return 1;
2291 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2292 d_printf("Server doesn't support UNIX CIFS calls.\n");
2293 return 1;
2296 if (!cli_nt_hardlink(targetcli, targetname, dest)) {
2297 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
2298 return 1;
2301 return 0;
2304 /****************************************************************************
2305 Toggle the prompt flag.
2306 ****************************************************************************/
2308 static int cmd_prompt(void)
2310 prompt = !prompt;
2311 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2313 return 1;
2316 /****************************************************************************
2317 Set the newer than time.
2318 ****************************************************************************/
2320 static int cmd_newer(void)
2322 pstring buf;
2323 BOOL ok;
2324 SMB_STRUCT_STAT sbuf;
2326 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2327 if (ok && (sys_stat(buf,&sbuf) == 0)) {
2328 newer_than = sbuf.st_mtime;
2329 DEBUG(1,("Getting files newer than %s",
2330 asctime(LocalTime(&newer_than))));
2331 } else {
2332 newer_than = 0;
2335 if (ok && newer_than == 0) {
2336 d_printf("Error setting newer-than time\n");
2337 return 1;
2340 return 0;
2343 /****************************************************************************
2344 Set the archive level.
2345 ****************************************************************************/
2347 static int cmd_archive(void)
2349 pstring buf;
2351 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2352 archive_level = atoi(buf);
2353 } else
2354 d_printf("Archive level is %d\n",archive_level);
2356 return 0;
2359 /****************************************************************************
2360 Toggle the lowercaseflag.
2361 ****************************************************************************/
2363 static int cmd_lowercase(void)
2365 lowercase = !lowercase;
2366 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2368 return 0;
2371 /****************************************************************************
2372 Toggle the case sensitive flag.
2373 ****************************************************************************/
2375 static int cmd_setcase(void)
2377 BOOL orig_case_sensitive = cli_set_case_sensitive(cli, False);
2379 cli_set_case_sensitive(cli, !orig_case_sensitive);
2380 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
2381 "on":"off"));
2383 return 0;
2386 /****************************************************************************
2387 Toggle the recurse flag.
2388 ****************************************************************************/
2390 static int cmd_recurse(void)
2392 recurse = !recurse;
2393 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2395 return 0;
2398 /****************************************************************************
2399 Toggle the translate flag.
2400 ****************************************************************************/
2402 static int cmd_translate(void)
2404 translation = !translation;
2405 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2406 translation?"on":"off"));
2408 return 0;
2411 /****************************************************************************
2412 Do a printmode command.
2413 ****************************************************************************/
2415 static int cmd_printmode(void)
2417 fstring buf;
2418 fstring mode;
2420 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2421 if (strequal(buf,"text")) {
2422 printmode = 0;
2423 } else {
2424 if (strequal(buf,"graphics"))
2425 printmode = 1;
2426 else
2427 printmode = atoi(buf);
2431 switch(printmode) {
2432 case 0:
2433 fstrcpy(mode,"text");
2434 break;
2435 case 1:
2436 fstrcpy(mode,"graphics");
2437 break;
2438 default:
2439 slprintf(mode,sizeof(mode)-1,"%d",printmode);
2440 break;
2443 DEBUG(2,("the printmode is now %s\n",mode));
2445 return 0;
2448 /****************************************************************************
2449 Do the lcd command.
2450 ****************************************************************************/
2452 static int cmd_lcd(void)
2454 pstring buf;
2455 pstring d;
2457 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2458 chdir(buf);
2459 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2461 return 0;
2464 /****************************************************************************
2465 Get a file restarting at end of local file.
2466 ****************************************************************************/
2468 static int cmd_reget(void)
2470 pstring local_name;
2471 pstring remote_name;
2472 char *p;
2474 pstrcpy(remote_name, cur_dir);
2475 pstrcat(remote_name, "\\");
2477 p = remote_name + strlen(remote_name);
2479 if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2480 d_printf("reget <filename>\n");
2481 return 1;
2483 pstrcpy(local_name, p);
2484 dos_clean_name(remote_name);
2486 next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2488 return do_get(remote_name, local_name, True);
2491 /****************************************************************************
2492 Put a file restarting at end of local file.
2493 ****************************************************************************/
2495 static int cmd_reput(void)
2497 pstring local_name;
2498 pstring remote_name;
2499 pstring buf;
2500 char *p = buf;
2501 SMB_STRUCT_STAT st;
2503 pstrcpy(remote_name, cur_dir);
2504 pstrcat(remote_name, "\\");
2506 if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2507 d_printf("reput <filename>\n");
2508 return 1;
2510 pstrcpy(local_name, p);
2512 if (!file_exist(local_name, &st)) {
2513 d_printf("%s does not exist\n", local_name);
2514 return 1;
2517 if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2518 pstrcat(remote_name, p);
2519 else
2520 pstrcat(remote_name, local_name);
2522 dos_clean_name(remote_name);
2524 return do_put(remote_name, local_name, True);
2527 /****************************************************************************
2528 List a share name.
2529 ****************************************************************************/
2531 static void browse_fn(const char *name, uint32 m,
2532 const char *comment, void *state)
2534 fstring typestr;
2536 *typestr=0;
2538 switch (m)
2540 case STYPE_DISKTREE:
2541 fstrcpy(typestr,"Disk"); break;
2542 case STYPE_PRINTQ:
2543 fstrcpy(typestr,"Printer"); break;
2544 case STYPE_DEVICE:
2545 fstrcpy(typestr,"Device"); break;
2546 case STYPE_IPC:
2547 fstrcpy(typestr,"IPC"); break;
2549 /* FIXME: If the remote machine returns non-ascii characters
2550 in any of these fields, they can corrupt the output. We
2551 should remove them. */
2552 if (!grepable) {
2553 d_printf("\t%-15s %-10.10s%s\n",
2554 name,typestr,comment);
2555 } else {
2556 d_printf ("%s|%s|%s\n",typestr,name,comment);
2560 /****************************************************************************
2561 Try and browse available connections on a host.
2562 ****************************************************************************/
2564 static BOOL browse_host(BOOL sort)
2566 int ret;
2567 if (!grepable) {
2568 d_printf("\n\tSharename Type Comment\n");
2569 d_printf("\t--------- ---- -------\n");
2572 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
2573 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
2575 return (ret != -1);
2578 /****************************************************************************
2579 List a server name.
2580 ****************************************************************************/
2582 static void server_fn(const char *name, uint32 m,
2583 const char *comment, void *state)
2586 if (!grepable){
2587 d_printf("\t%-16s %s\n", name, comment);
2588 } else {
2589 d_printf("%s|%s|%s\n",(char *)state, name, comment);
2593 /****************************************************************************
2594 Try and browse available connections on a host.
2595 ****************************************************************************/
2597 static BOOL list_servers(const char *wk_grp)
2599 fstring state;
2601 if (!cli->server_domain)
2602 return False;
2604 if (!grepable) {
2605 d_printf("\n\tServer Comment\n");
2606 d_printf("\t--------- -------\n");
2608 fstrcpy( state, "Server" );
2609 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
2610 state);
2612 if (!grepable) {
2613 d_printf("\n\tWorkgroup Master\n");
2614 d_printf("\t--------- -------\n");
2617 fstrcpy( state, "Workgroup" );
2618 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
2619 server_fn, state);
2620 return True;
2623 /****************************************************************************
2624 Print or set current VUID
2625 ****************************************************************************/
2627 static int cmd_vuid(void)
2629 fstring buf;
2631 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2632 d_printf("Current VUID is %d\n", cli->vuid);
2633 return 0;
2636 cli->vuid = atoi(buf);
2637 return 0;
2640 /****************************************************************************
2641 Setup a new VUID, by issuing a session setup
2642 ****************************************************************************/
2644 static int cmd_logon(void)
2646 pstring l_username, l_password;
2647 pstring buf,buf2;
2649 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2650 d_printf("logon <username> [<password>]\n");
2651 return 0;
2654 pstrcpy(l_username, buf);
2656 if (!next_token_nr(NULL,buf2,NULL,sizeof(buf)))
2658 char *pass = getpass("Password: ");
2659 if (pass)
2660 pstrcpy(l_password, pass);
2662 else
2663 pstrcpy(l_password, buf2);
2665 if (!cli_session_setup(cli, l_username,
2666 l_password, strlen(l_password),
2667 l_password, strlen(l_password),
2668 lp_workgroup())) {
2669 d_printf("session setup failed: %s\n", cli_errstr(cli));
2670 return -1;
2673 d_printf("Current VUID is %d\n", cli->vuid);
2674 return 0;
2678 /****************************************************************************
2679 list active connections
2680 ****************************************************************************/
2682 static int cmd_list_connect(void)
2684 cli_cm_display();
2686 return 0;
2689 /****************************************************************************
2690 display the current active client connection
2691 ****************************************************************************/
2693 static int cmd_show_connect( void )
2695 struct cli_state *targetcli;
2696 pstring targetpath;
2698 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
2699 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
2700 return 1;
2703 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
2704 return 0;
2707 /* Some constants for completing filename arguments */
2709 #define COMPL_NONE 0 /* No completions */
2710 #define COMPL_REMOTE 1 /* Complete remote filename */
2711 #define COMPL_LOCAL 2 /* Complete local filename */
2713 /* This defines the commands supported by this client.
2714 * NOTE: The "!" must be the last one in the list because it's fn pointer
2715 * field is NULL, and NULL in that field is used in process_tok()
2716 * (below) to indicate the end of the list. crh
2718 static struct
2720 const char *name;
2721 int (*fn)(void);
2722 const char *description;
2723 char compl_args[2]; /* Completion argument info */
2724 } commands[] = {
2725 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2726 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2727 {"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}},
2728 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
2729 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2730 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
2731 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2732 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2733 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2734 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2735 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2736 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2737 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2738 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2739 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
2740 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2741 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2742 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2743 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2744 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2745 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
2746 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2747 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2748 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2749 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2750 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2751 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
2752 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2753 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2754 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2755 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2756 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
2757 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
2758 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2759 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2760 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2761 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2762 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2763 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2764 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
2765 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2766 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2767 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2768 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2769 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2770 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
2771 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
2772 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2773 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
2774 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
2775 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2776 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
2777 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
2778 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
2779 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
2780 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
2782 /* Yes, this must be here, see crh's comment above. */
2783 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2784 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2787 /*******************************************************************
2788 Lookup a command string in the list of commands, including
2789 abbreviations.
2790 ******************************************************************/
2792 static int process_tok(pstring tok)
2794 int i = 0, matches = 0;
2795 int cmd=0;
2796 int tok_len = strlen(tok);
2798 while (commands[i].fn != NULL) {
2799 if (strequal(commands[i].name,tok)) {
2800 matches = 1;
2801 cmd = i;
2802 break;
2803 } else if (strnequal(commands[i].name, tok, tok_len)) {
2804 matches++;
2805 cmd = i;
2807 i++;
2810 if (matches == 0)
2811 return(-1);
2812 else if (matches == 1)
2813 return(cmd);
2814 else
2815 return(-2);
2818 /****************************************************************************
2819 Help.
2820 ****************************************************************************/
2822 static int cmd_help(void)
2824 int i=0,j;
2825 pstring buf;
2827 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2828 if ((i = process_tok(buf)) >= 0)
2829 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
2830 } else {
2831 while (commands[i].description) {
2832 for (j=0; commands[i].description && (j<5); j++) {
2833 d_printf("%-15s",commands[i].name);
2834 i++;
2836 d_printf("\n");
2839 return 0;
2842 /****************************************************************************
2843 Process a -c command string.
2844 ****************************************************************************/
2846 static int process_command_string(char *cmd)
2848 pstring line;
2849 const char *ptr;
2850 int rc = 0;
2852 /* establish the connection if not already */
2854 if (!cli) {
2855 cli = cli_cm_open(desthost, service, True);
2856 if (!cli)
2857 return 0;
2860 while (cmd[0] != '\0') {
2861 char *p;
2862 pstring tok;
2863 int i;
2865 if ((p = strchr_m(cmd, ';')) == 0) {
2866 strncpy(line, cmd, 999);
2867 line[1000] = '\0';
2868 cmd += strlen(cmd);
2869 } else {
2870 if (p - cmd > 999)
2871 p = cmd + 999;
2872 strncpy(line, cmd, p - cmd);
2873 line[p - cmd] = '\0';
2874 cmd = p + 1;
2877 /* and get the first part of the command */
2878 ptr = line;
2879 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
2881 if ((i = process_tok(tok)) >= 0) {
2882 rc = commands[i].fn();
2883 } else if (i == -2) {
2884 d_printf("%s: command abbreviation ambiguous\n",tok);
2885 } else {
2886 d_printf("%s: command not found\n",tok);
2890 return rc;
2893 #define MAX_COMPLETIONS 100
2895 typedef struct {
2896 pstring dirmask;
2897 char **matches;
2898 int count, samelen;
2899 const char *text;
2900 int len;
2901 } completion_remote_t;
2903 static void completion_remote_filter(const char *mnt, file_info *f, const char *mask, void *state)
2905 completion_remote_t *info = (completion_remote_t *)state;
2907 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
2908 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
2909 info->matches[info->count] = SMB_STRDUP(f->name);
2910 else {
2911 pstring tmp;
2913 if (info->dirmask[0] != 0)
2914 pstrcpy(tmp, info->dirmask);
2915 else
2916 tmp[0] = 0;
2917 pstrcat(tmp, f->name);
2918 if (f->mode & aDIR)
2919 pstrcat(tmp, "/");
2920 info->matches[info->count] = SMB_STRDUP(tmp);
2922 if (info->matches[info->count] == NULL)
2923 return;
2924 if (f->mode & aDIR)
2925 smb_readline_ca_char(0);
2927 if (info->count == 1)
2928 info->samelen = strlen(info->matches[info->count]);
2929 else
2930 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
2931 info->samelen--;
2932 info->count++;
2936 static char **remote_completion(const char *text, int len)
2938 pstring dirmask;
2939 int i;
2940 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
2942 /* can't have non-static intialisation on Sun CC, so do it
2943 at run time here */
2944 info.samelen = len;
2945 info.text = text;
2946 info.len = len;
2948 if (len >= PATH_MAX)
2949 return(NULL);
2951 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
2952 if (!info.matches) return NULL;
2953 info.matches[0] = NULL;
2955 for (i = len-1; i >= 0; i--)
2956 if ((text[i] == '/') || (text[i] == '\\'))
2957 break;
2958 info.text = text+i+1;
2959 info.samelen = info.len = len-i-1;
2961 if (i > 0) {
2962 strncpy(info.dirmask, text, i+1);
2963 info.dirmask[i+1] = 0;
2964 pstr_sprintf(dirmask, "%s%*s*", cur_dir, i-1, text);
2965 } else
2966 pstr_sprintf(dirmask, "%s*", cur_dir);
2968 if (cli_list(cli, dirmask, aDIR | aSYSTEM | aHIDDEN, completion_remote_filter, &info) < 0)
2969 goto cleanup;
2971 if (info.count == 2)
2972 info.matches[0] = SMB_STRDUP(info.matches[1]);
2973 else {
2974 info.matches[0] = SMB_MALLOC(info.samelen+1);
2975 if (!info.matches[0])
2976 goto cleanup;
2977 strncpy(info.matches[0], info.matches[1], info.samelen);
2978 info.matches[0][info.samelen] = 0;
2980 info.matches[info.count] = NULL;
2981 return info.matches;
2983 cleanup:
2984 for (i = 0; i < info.count; i++)
2985 free(info.matches[i]);
2986 free(info.matches);
2987 return NULL;
2990 static char **completion_fn(const char *text, int start, int end)
2992 smb_readline_ca_char(' ');
2994 if (start) {
2995 const char *buf, *sp;
2996 int i;
2997 char compl_type;
2999 buf = smb_readline_get_line_buffer();
3000 if (buf == NULL)
3001 return NULL;
3003 sp = strchr(buf, ' ');
3004 if (sp == NULL)
3005 return NULL;
3007 for (i = 0; commands[i].name; i++)
3008 if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
3009 break;
3010 if (commands[i].name == NULL)
3011 return NULL;
3013 while (*sp == ' ')
3014 sp++;
3016 if (sp == (buf + start))
3017 compl_type = commands[i].compl_args[0];
3018 else
3019 compl_type = commands[i].compl_args[1];
3021 if (compl_type == COMPL_REMOTE)
3022 return remote_completion(text, end - start);
3023 else /* fall back to local filename completion */
3024 return NULL;
3025 } else {
3026 char **matches;
3027 int i, len, samelen = 0, count=1;
3029 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
3030 if (!matches) {
3031 return NULL;
3033 matches[0] = NULL;
3035 len = strlen(text);
3036 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3037 if (strncmp(text, commands[i].name, len) == 0) {
3038 matches[count] = SMB_STRDUP(commands[i].name);
3039 if (!matches[count])
3040 goto cleanup;
3041 if (count == 1)
3042 samelen = strlen(matches[count]);
3043 else
3044 while (strncmp(matches[count], matches[count-1], samelen) != 0)
3045 samelen--;
3046 count++;
3050 switch (count) {
3051 case 0: /* should never happen */
3052 case 1:
3053 goto cleanup;
3054 case 2:
3055 matches[0] = SMB_STRDUP(matches[1]);
3056 break;
3057 default:
3058 matches[0] = SMB_MALLOC(samelen+1);
3059 if (!matches[0])
3060 goto cleanup;
3061 strncpy(matches[0], matches[1], samelen);
3062 matches[0][samelen] = 0;
3064 matches[count] = NULL;
3065 return matches;
3067 cleanup:
3068 for (i = 0; i < count; i++)
3069 free(matches[i]);
3071 free(matches);
3072 return NULL;
3076 /****************************************************************************
3077 Make sure we swallow keepalives during idle time.
3078 ****************************************************************************/
3080 static void readline_callback(void)
3082 fd_set fds;
3083 struct timeval timeout;
3084 static time_t last_t;
3085 time_t t;
3087 t = time(NULL);
3089 if (t - last_t < 5)
3090 return;
3092 last_t = t;
3094 again:
3096 if (cli->fd == -1)
3097 return;
3099 FD_ZERO(&fds);
3100 FD_SET(cli->fd,&fds);
3102 timeout.tv_sec = 0;
3103 timeout.tv_usec = 0;
3104 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
3106 /* We deliberately use receive_smb instead of
3107 client_receive_smb as we want to receive
3108 session keepalives and then drop them here.
3110 if (FD_ISSET(cli->fd,&fds)) {
3111 receive_smb(cli->fd,cli->inbuf,0);
3112 goto again;
3115 cli_chkpath(cli, "\\");
3118 /****************************************************************************
3119 Process commands on stdin.
3120 ****************************************************************************/
3122 static int process_stdin(void)
3124 const char *ptr;
3125 int rc = 0;
3127 while (1) {
3128 pstring tok;
3129 pstring the_prompt;
3130 char *cline;
3131 pstring line;
3132 int i;
3134 /* display a prompt */
3135 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3136 cline = smb_readline(the_prompt, readline_callback, completion_fn);
3138 if (!cline) break;
3140 pstrcpy(line, cline);
3142 /* special case - first char is ! */
3143 if (*line == '!') {
3144 system(line + 1);
3145 continue;
3148 /* and get the first part of the command */
3149 ptr = line;
3150 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3152 if ((i = process_tok(tok)) >= 0) {
3153 rc = commands[i].fn();
3154 } else if (i == -2) {
3155 d_printf("%s: command abbreviation ambiguous\n",tok);
3156 } else {
3157 d_printf("%s: command not found\n",tok);
3160 return rc;
3163 /****************************************************************************
3164 Process commands from the client.
3165 ****************************************************************************/
3167 static int process(char *base_directory)
3169 int rc = 0;
3171 cli = cli_cm_open(desthost, service, True);
3172 if (!cli) {
3173 return 1;
3176 if (*base_directory) {
3177 rc = do_cd(base_directory);
3178 if (rc) {
3179 cli_cm_shutdown();
3180 return rc;
3184 if (cmdstr) {
3185 rc = process_command_string(cmdstr);
3186 } else {
3187 process_stdin();
3190 cli_cm_shutdown();
3191 return rc;
3194 /****************************************************************************
3195 Handle a -L query.
3196 ****************************************************************************/
3198 static int do_host_query(char *query_host)
3200 cli = cli_cm_open(query_host, "IPC$", True);
3201 if (!cli)
3202 return 1;
3204 browse_host(True);
3206 if (port != 139) {
3208 /* Workgroups simply don't make sense over anything
3209 else but port 139... */
3211 cli_cm_shutdown();
3212 cli_cm_set_port( 139 );
3213 cli = cli_cm_open(query_host, "IPC$", True);
3216 if (cli == NULL) {
3217 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3218 return 1;
3221 list_servers(lp_workgroup());
3223 cli_cm_shutdown();
3225 return(0);
3228 /****************************************************************************
3229 Handle a tar operation.
3230 ****************************************************************************/
3232 static int do_tar_op(char *base_directory)
3234 int ret;
3236 /* do we already have a connection? */
3237 if (!cli) {
3238 cli = cli_cm_open(desthost, service, True);
3239 if (!cli)
3240 return 1;
3243 recurse=True;
3245 if (*base_directory) {
3246 ret = do_cd(base_directory);
3247 if (ret) {
3248 cli_cm_shutdown();
3249 return ret;
3253 ret=process_tar();
3255 cli_cm_shutdown();
3257 return(ret);
3260 /****************************************************************************
3261 Handle a message operation.
3262 ****************************************************************************/
3264 static int do_message_op(void)
3266 struct in_addr ip;
3267 struct nmb_name called, calling;
3268 fstring server_name;
3269 char name_type_hex[10];
3270 int msg_port;
3272 make_nmb_name(&calling, calling_name, 0x0);
3273 make_nmb_name(&called , desthost, name_type);
3275 fstrcpy(server_name, desthost);
3276 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
3277 fstrcat(server_name, name_type_hex);
3279 zero_ip(&ip);
3280 if (have_ip)
3281 ip = dest_ip;
3283 /* we can only do messages over port 139 (to windows clients at least) */
3285 msg_port = port ? port : 139;
3287 if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, msg_port) != msg_port) ||
3288 !cli_connect(cli, server_name, &ip)) {
3289 d_printf("Connection to %s failed\n", desthost);
3290 return 1;
3293 if (!cli_session_request(cli, &calling, &called)) {
3294 d_printf("session request failed\n");
3295 cli_cm_shutdown();
3296 return 1;
3299 send_message();
3300 cli_cm_shutdown();
3302 return 0;
3306 /****************************************************************************
3307 main program
3308 ****************************************************************************/
3310 int main(int argc,char *argv[])
3312 pstring base_directory;
3313 int opt;
3314 pstring query_host;
3315 BOOL message = False;
3316 pstring term_code;
3317 static const char *new_name_resolve_order = NULL;
3318 poptContext pc;
3319 char *p;
3320 int rc = 0;
3321 fstring new_workgroup;
3322 struct poptOption long_options[] = {
3323 POPT_AUTOHELP
3325 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3326 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3327 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3328 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3329 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3330 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3331 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
3332 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3333 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3334 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
3335 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
3336 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3337 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
3338 POPT_COMMON_SAMBA
3339 POPT_COMMON_CONNECTION
3340 POPT_COMMON_CREDENTIALS
3341 POPT_TABLEEND
3345 #ifdef KANJI
3346 pstrcpy(term_code, KANJI);
3347 #else /* KANJI */
3348 *term_code = 0;
3349 #endif /* KANJI */
3351 *query_host = 0;
3352 *base_directory = 0;
3354 /* initialize the workgroup name so we can determine whether or
3355 not it was set by a command line option */
3357 set_global_myworkgroup( "" );
3358 set_global_myname( "" );
3360 /* set default debug level to 0 regardless of what smb.conf sets */
3361 setup_logging( "smbclient", True );
3362 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
3363 dbf = x_stderr;
3364 x_setbuf( x_stderr, NULL );
3366 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options,
3367 POPT_CONTEXT_KEEP_FIRST);
3368 poptSetOtherOptionHelp(pc, "service <password>");
3370 in_client = True; /* Make sure that we tell lp_load we are */
3372 while ((opt = poptGetNextOpt(pc)) != -1) {
3373 switch (opt) {
3374 case 'M':
3375 /* Messages are sent to NetBIOS name type 0x3
3376 * (Messenger Service). Make sure we default
3377 * to port 139 instead of port 445. srl,crh
3379 name_type = 0x03;
3380 cli_cm_set_dest_name_type( name_type );
3381 pstrcpy(desthost,poptGetOptArg(pc));
3382 if( !port )
3383 cli_cm_set_port( 139 );
3384 message = True;
3385 break;
3386 case 'I':
3388 dest_ip = *interpret_addr2(poptGetOptArg(pc));
3389 if (is_zero_ip(dest_ip))
3390 exit(1);
3391 have_ip = True;
3393 cli_cm_set_dest_ip( dest_ip );
3395 break;
3396 case 'E':
3397 dbf = x_stderr;
3398 display_set_stderr();
3399 break;
3401 case 'L':
3402 pstrcpy(query_host, poptGetOptArg(pc));
3403 break;
3404 case 't':
3405 pstrcpy(term_code, poptGetOptArg(pc));
3406 break;
3407 case 'm':
3408 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
3409 break;
3410 case 'T':
3411 /* We must use old option processing for this. Find the
3412 * position of the -T option in the raw argv[]. */
3414 int i, optnum;
3415 for (i = 1; i < argc; i++) {
3416 if (strncmp("-T", argv[i],2)==0)
3417 break;
3419 i++;
3420 if (!(optnum = tar_parseargs(argc, argv, poptGetOptArg(pc), i))) {
3421 poptPrintUsage(pc, stderr, 0);
3422 exit(1);
3424 /* Now we must eat (optnum - i) options - they have
3425 * been processed by tar_parseargs().
3427 optnum -= i;
3428 for (i = 0; i < optnum; i++)
3429 poptGetOptArg(pc);
3431 break;
3432 case 'D':
3433 pstrcpy(base_directory,poptGetOptArg(pc));
3434 break;
3435 case 'g':
3436 grepable=True;
3437 break;
3441 poptGetArg(pc);
3443 if ( have_ip )
3446 * Don't load debug level from smb.conf. It should be
3447 * set by cmdline arg or remain default (0)
3449 AllowDebugChange = False;
3451 /* save the workgroup...
3453 FIXME!! do we need to do this for other options as well
3454 (or maybe a generic way to keep lp_load() from overwriting
3455 everything)? */
3457 fstrcpy( new_workgroup, lp_workgroup() );
3458 pstrcpy( calling_name, global_myname() );
3460 if ( override_logfile )
3461 setup_logging( lp_logfile(), False );
3463 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
3464 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
3465 argv[0], dyn_CONFIGFILE);
3468 load_interfaces();
3470 if ( strlen(new_workgroup) != 0 )
3471 set_global_myworkgroup( new_workgroup );
3473 if ( strlen(calling_name) != 0 )
3474 set_global_myname( calling_name );
3476 if(poptPeekArg(pc)) {
3477 pstrcpy(service,poptGetArg(pc));
3478 /* Convert any '/' characters in the service name to '\' characters */
3479 string_replace(service, '/','\\');
3481 if (count_chars(service,'\\') < 3) {
3482 d_printf("\n%s: Not enough '\\' characters in service\n",service);
3483 poptPrintUsage(pc, stderr, 0);
3484 exit(1);
3488 if (poptPeekArg(pc) && !cmdline_auth_info.got_pass) {
3489 cmdline_auth_info.got_pass = True;
3490 pstrcpy(cmdline_auth_info.password,poptGetArg(pc));
3493 init_names();
3495 if(new_name_resolve_order)
3496 lp_set_name_resolve_order(new_name_resolve_order);
3498 if (!tar_type && !*query_host && !*service && !message) {
3499 poptPrintUsage(pc, stderr, 0);
3500 exit(1);
3503 poptFreeContext(pc);
3505 /* store the username an password for dfs support */
3507 cli_cm_set_credentials( &cmdline_auth_info );
3508 pstrcpy(username, cmdline_auth_info.username);
3510 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
3512 if (tar_type) {
3513 if (cmdstr)
3514 process_command_string(cmdstr);
3515 return do_tar_op(base_directory);
3518 if (*query_host) {
3519 char *qhost = query_host;
3520 char *slash;
3522 while (*qhost == '\\' || *qhost == '/')
3523 qhost++;
3525 if ((slash = strchr_m(qhost, '/'))
3526 || (slash = strchr_m(qhost, '\\'))) {
3527 *slash = 0;
3530 if ((p=strchr_m(qhost, '#'))) {
3531 *p = 0;
3532 p++;
3533 sscanf(p, "%x", &name_type);
3534 cli_cm_set_dest_name_type( name_type );
3537 return do_host_query(qhost);
3540 if (message) {
3541 return do_message_op();
3544 if (process(base_directory)) {
3545 return 1;
3548 return rc;