r16962: Add a few utility fns into client. Allow POSIX capabilities
[tprouty/samba.git] / source / client / client.c
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
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25 #include "client/client_proto.h"
26 #ifndef REGISTER
27 #define REGISTER 0
28 #endif
29
30 extern BOOL AllowDebugChange;
31 extern BOOL override_logfile;
32 extern char tar_type;
33 extern BOOL in_client;
34 static int port = 0;
35 pstring cur_dir = "\\";
36 static pstring cd_path = "";
37 static pstring service;
38 static pstring desthost;
39 static pstring username;
40 static pstring calling_name;
41 static BOOL grepable=False;
42 static char *cmdstr = NULL;
43
44 static int io_bufsize = 64512;
45
46 static int name_type = 0x20;
47 extern int max_protocol;
48
49 static int process_tok(pstring tok);
50 static int cmd_help(void);
51
52 /* 30 second timeout on most commands */
53 #define CLIENT_TIMEOUT (30*1000)
54 #define SHORT_TIMEOUT (5*1000)
55
56 /* value for unused fid field in trans2 secondary request */
57 #define FID_UNUSED (0xFFFF)
58
59 time_t newer_than = 0;
60 static int archive_level = 0;
61
62 static BOOL translation = False;
63 static BOOL have_ip;
64
65 /* clitar bits insert */
66 extern int blocksize;
67 extern BOOL tar_inc;
68 extern BOOL tar_reset;
69 /* clitar bits end */
70  
71
72 static BOOL prompt = True;
73
74 static BOOL recurse = False;
75 BOOL lowercase = False;
76
77 static struct in_addr dest_ip;
78
79 #define SEPARATORS " \t\n\r"
80
81 static BOOL abort_mget = True;
82
83 static pstring fileselection = "";
84
85 extern file_info def_finfo;
86
87 /* timing globals */
88 SMB_BIG_UINT get_total_size = 0;
89 unsigned int get_total_time_ms = 0;
90 static SMB_BIG_UINT put_total_size = 0;
91 static unsigned int put_total_time_ms = 0;
92
93 /* totals globals */
94 static double dir_total;
95
96 /* root cli_state connection */
97
98 struct cli_state *cli;
99
100
101
102 /****************************************************************************
103  Write to a local file with CR/LF->LF translation if appropriate. Return the 
104  number taken from the buffer. This may not equal the number written.
105 ****************************************************************************/
106
107 static int writefile(int f, char *b, int n)
108 {
109         int i;
110
111         if (!translation) {
112                 return write(f,b,n);
113         }
114
115         i = 0;
116         while (i < n) {
117                 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
118                         b++;i++;
119                 }
120                 if (write(f, b, 1) != 1) {
121                         break;
122                 }
123                 b++;
124                 i++;
125         }
126   
127         return(i);
128 }
129
130 /****************************************************************************
131  Read from a file with LF->CR/LF translation if appropriate. Return the 
132  number read. read approx n bytes.
133 ****************************************************************************/
134
135 static int readfile(char *b, int n, XFILE *f)
136 {
137         int i;
138         int c;
139
140         if (!translation)
141                 return x_fread(b,1,n,f);
142   
143         i = 0;
144         while (i < (n - 1) && (i < BUFFER_SIZE)) {
145                 if ((c = x_getc(f)) == EOF) {
146                         break;
147                 }
148       
149                 if (c == '\n') { /* change all LFs to CR/LF */
150                         b[i++] = '\r';
151                 }
152       
153                 b[i++] = c;
154         }
155   
156         return(i);
157 }
158  
159 /****************************************************************************
160  Send a message.
161 ****************************************************************************/
162
163 static void send_message(void)
164 {
165         int total_len = 0;
166         int grp_id;
167
168         if (!cli_message_start(cli, desthost, username, &grp_id)) {
169                 d_printf("message start: %s\n", cli_errstr(cli));
170                 return;
171         }
172
173
174         d_printf("Connected. Type your message, ending it with a Control-D\n");
175
176         while (!feof(stdin) && total_len < 1600) {
177                 int maxlen = MIN(1600 - total_len,127);
178                 pstring msg;
179                 int l=0;
180                 int c;
181
182                 ZERO_ARRAY(msg);
183
184                 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
185                         if (c == '\n')
186                                 msg[l++] = '\r';
187                         msg[l] = c;   
188                 }
189
190                 if (!cli_message_text(cli, msg, l, grp_id)) {
191                         d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
192                         return;
193                 }      
194                 
195                 total_len += l;
196         }
197
198         if (total_len >= 1600)
199                 d_printf("the message was truncated to 1600 bytes\n");
200         else
201                 d_printf("sent %d bytes\n",total_len);
202
203         if (!cli_message_end(cli, grp_id)) {
204                 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
205                 return;
206         }      
207 }
208
209 /****************************************************************************
210  Check the space on a device.
211 ****************************************************************************/
212
213 static int do_dskattr(void)
214 {
215         int total, bsize, avail;
216         struct cli_state *targetcli;
217         pstring targetpath;
218
219         if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
220                 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
221                 return 1;
222         }
223
224         if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
225                 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli)); 
226                 return 1;
227         }
228
229         d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
230                  total, bsize, avail);
231
232         return 0;
233 }
234
235 /****************************************************************************
236  Show cd/pwd.
237 ****************************************************************************/
238
239 static int cmd_pwd(void)
240 {
241         d_printf("Current directory is %s",service);
242         d_printf("%s\n",cur_dir);
243         return 0;
244 }
245
246 /****************************************************************************
247  Change directory - inner section.
248 ****************************************************************************/
249
250 static int do_cd(char *newdir)
251 {
252         char *p = newdir;
253         pstring saved_dir;
254         pstring dname;
255         pstring targetpath;
256         struct cli_state *targetcli;
257         SMB_STRUCT_STAT sbuf;
258         uint32 attributes;
259         int ret = 1;
260       
261         dos_format(newdir);
262
263         /* Save the current directory in case the new directory is invalid */
264
265         pstrcpy(saved_dir, cur_dir);
266
267         if (*p == '\\')
268                 pstrcpy(cur_dir,p);
269         else
270                 pstrcat(cur_dir,p);
271
272         if ((cur_dir[0] != '\0') && (*(cur_dir+strlen(cur_dir)-1) != '\\')) {
273                 pstrcat(cur_dir, "\\");
274         }
275         
276         dos_clean_name(cur_dir);
277         pstrcpy( dname, cur_dir );
278         pstrcat(cur_dir,"\\");
279         dos_clean_name(cur_dir);
280         
281         if ( !cli_resolve_path( "", cli, dname, &targetcli, targetpath ) ) {
282                 d_printf("cd %s: %s\n", dname, cli_errstr(cli));
283                 pstrcpy(cur_dir,saved_dir);
284                 goto out;
285         }
286
287         
288         if ( strequal(targetpath,"\\" ) )
289                 return 0;   
290                 
291         /* Use a trans2_qpathinfo to test directories for modern servers.
292            Except Win9x doesn't support the qpathinfo_basic() call..... */ 
293         
294         if ( targetcli->protocol >  PROTOCOL_LANMAN2 && !targetcli->win95 ) {
295                 if ( !cli_qpathinfo_basic( targetcli, targetpath, &sbuf, &attributes ) ) {
296                         d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
297                         pstrcpy(cur_dir,saved_dir);
298                         goto out;
299                 }
300                 
301                 if ( !(attributes&FILE_ATTRIBUTE_DIRECTORY) ) {
302                         d_printf("cd %s: not a directory\n", dname);
303                         pstrcpy(cur_dir,saved_dir);
304                         goto out;
305                 }               
306         } else {
307                 pstrcat( targetpath, "\\" );
308                 dos_clean_name( targetpath );
309                 
310                 if ( !cli_chkpath(targetcli, targetpath) ) {
311                         d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
312                         pstrcpy(cur_dir,saved_dir);
313                         goto out;
314                 }
315         }
316
317         ret = 0;
318
319 out:
320         
321         pstrcpy(cd_path,cur_dir);
322         return ret;
323 }
324
325 /****************************************************************************
326  Change directory.
327 ****************************************************************************/
328
329 static int cmd_cd(void)
330 {
331         pstring buf;
332         int rc = 0;
333                 
334         if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
335                 rc = do_cd(buf);
336         else
337                 d_printf("Current directory is %s\n",cur_dir);
338
339         return rc;
340 }
341
342 /*******************************************************************
343  Decide if a file should be operated on.
344 ********************************************************************/
345
346 static BOOL do_this_one(file_info *finfo)
347 {
348         if (finfo->mode & aDIR)
349                 return(True);
350
351         if (*fileselection && 
352             !mask_match(finfo->name,fileselection,False)) {
353                 DEBUG(3,("mask_match %s failed\n", finfo->name));
354                 return False;
355         }
356
357         if (newer_than && finfo->mtime < newer_than) {
358                 DEBUG(3,("newer_than %s failed\n", finfo->name));
359                 return(False);
360         }
361
362         if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
363                 DEBUG(3,("archive %s failed\n", finfo->name));
364                 return(False);
365         }
366         
367         return(True);
368 }
369
370 /****************************************************************************
371  Display info about a file.
372 ****************************************************************************/
373
374 static void display_finfo(file_info *finfo)
375 {
376         if (do_this_one(finfo)) {
377                 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
378                 d_printf("  %-30s%7.7s %8.0f  %s",
379                          finfo->name,
380                          attrib_string(finfo->mode),
381                          (double)finfo->size,
382                          time_to_asc(&t));
383                 dir_total += finfo->size;
384         }
385 }
386
387 /****************************************************************************
388  Accumulate size of a file.
389 ****************************************************************************/
390
391 static void do_du(file_info *finfo)
392 {
393         if (do_this_one(finfo)) {
394                 dir_total += finfo->size;
395         }
396 }
397
398 static BOOL do_list_recurse;
399 static BOOL do_list_dirs;
400 static char *do_list_queue = 0;
401 static long do_list_queue_size = 0;
402 static long do_list_queue_start = 0;
403 static long do_list_queue_end = 0;
404 static void (*do_list_fn)(file_info *);
405
406 /****************************************************************************
407  Functions for do_list_queue.
408 ****************************************************************************/
409
410 /*
411  * The do_list_queue is a NUL-separated list of strings stored in a
412  * char*.  Since this is a FIFO, we keep track of the beginning and
413  * ending locations of the data in the queue.  When we overflow, we
414  * double the size of the char*.  When the start of the data passes
415  * the midpoint, we move everything back.  This is logically more
416  * complex than a linked list, but easier from a memory management
417  * angle.  In any memory error condition, do_list_queue is reset.
418  * Functions check to ensure that do_list_queue is non-NULL before
419  * accessing it.
420  */
421
422 static void reset_do_list_queue(void)
423 {
424         SAFE_FREE(do_list_queue);
425         do_list_queue_size = 0;
426         do_list_queue_start = 0;
427         do_list_queue_end = 0;
428 }
429
430 static void init_do_list_queue(void)
431 {
432         reset_do_list_queue();
433         do_list_queue_size = 1024;
434         do_list_queue = SMB_MALLOC(do_list_queue_size);
435         if (do_list_queue == 0) { 
436                 d_printf("malloc fail for size %d\n",
437                          (int)do_list_queue_size);
438                 reset_do_list_queue();
439         } else {
440                 memset(do_list_queue, 0, do_list_queue_size);
441         }
442 }
443
444 static void adjust_do_list_queue(void)
445 {
446         /*
447          * If the starting point of the queue is more than half way through,
448          * move everything toward the beginning.
449          */
450
451         if (do_list_queue == NULL) {
452                 DEBUG(4,("do_list_queue is empty\n"));
453                 do_list_queue_start = do_list_queue_end = 0;
454                 return;
455         }
456                 
457         if (do_list_queue_start == do_list_queue_end) {
458                 DEBUG(4,("do_list_queue is empty\n"));
459                 do_list_queue_start = do_list_queue_end = 0;
460                 *do_list_queue = '\0';
461         } else if (do_list_queue_start > (do_list_queue_size / 2)) {
462                 DEBUG(4,("sliding do_list_queue backward\n"));
463                 memmove(do_list_queue,
464                         do_list_queue + do_list_queue_start,
465                         do_list_queue_end - do_list_queue_start);
466                 do_list_queue_end -= do_list_queue_start;
467                 do_list_queue_start = 0;
468         }
469 }
470
471 static void add_to_do_list_queue(const char* entry)
472 {
473         long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
474         while (new_end > do_list_queue_size) {
475                 do_list_queue_size *= 2;
476                 DEBUG(4,("enlarging do_list_queue to %d\n",
477                          (int)do_list_queue_size));
478                 do_list_queue = SMB_REALLOC(do_list_queue, do_list_queue_size);
479                 if (! do_list_queue) {
480                         d_printf("failure enlarging do_list_queue to %d bytes\n",
481                                  (int)do_list_queue_size);
482                         reset_do_list_queue();
483                 } else {
484                         memset(do_list_queue + do_list_queue_size / 2,
485                                0, do_list_queue_size / 2);
486                 }
487         }
488         if (do_list_queue) {
489                 safe_strcpy_base(do_list_queue + do_list_queue_end, 
490                                  entry, do_list_queue, do_list_queue_size);
491                 do_list_queue_end = new_end;
492                 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
493                          entry, (int)do_list_queue_start, (int)do_list_queue_end));
494         }
495 }
496
497 static char *do_list_queue_head(void)
498 {
499         return do_list_queue + do_list_queue_start;
500 }
501
502 static void remove_do_list_queue_head(void)
503 {
504         if (do_list_queue_end > do_list_queue_start) {
505                 do_list_queue_start += strlen(do_list_queue_head()) + 1;
506                 adjust_do_list_queue();
507                 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
508                          (int)do_list_queue_start, (int)do_list_queue_end));
509         }
510 }
511
512 static int do_list_queue_empty(void)
513 {
514         return (! (do_list_queue && *do_list_queue));
515 }
516
517 /****************************************************************************
518  A helper for do_list.
519 ****************************************************************************/
520
521 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
522 {
523         char *dir_end;
524
525         /* save the directory */
526         pstrcpy( f->dir, mask );
527         if ( (dir_end = strrchr( f->dir, '\\' )) != NULL ) {
528                 *dir_end = '\0';
529         }
530
531         if (f->mode & aDIR) {
532                 if (do_list_dirs && do_this_one(f)) {
533                         do_list_fn(f);
534                 }
535                 if (do_list_recurse && 
536                     !strequal(f->name,".") && 
537                     !strequal(f->name,"..")) {
538                         pstring mask2;
539                         char *p;
540
541                         if (!f->name[0]) {
542                                 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
543                                 return;
544                         }
545
546                         pstrcpy(mask2, mntpoint);
547                         pstrcat(mask2, mask);
548                         p = strrchr_m(mask2,'\\');
549                         if (!p)
550                                 return;
551                         p[1] = 0;
552                         pstrcat(mask2, f->name);
553                         pstrcat(mask2,"\\*");
554                         add_to_do_list_queue(mask2);
555                 }
556                 return;
557         }
558
559         if (do_this_one(f)) {
560                 do_list_fn(f);
561         }
562 }
563
564 /****************************************************************************
565  A wrapper around cli_list that adds recursion.
566 ****************************************************************************/
567
568 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
569 {
570         static int in_do_list = 0;
571         struct cli_state *targetcli;
572         pstring targetpath;
573
574         if (in_do_list && rec) {
575                 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
576                 exit(1);
577         }
578
579         in_do_list = 1;
580
581         do_list_recurse = rec;
582         do_list_dirs = dirs;
583         do_list_fn = fn;
584
585         if (rec) {
586                 init_do_list_queue();
587                 add_to_do_list_queue(mask);
588                 
589                 while (! do_list_queue_empty()) {
590                         /*
591                          * Need to copy head so that it doesn't become
592                          * invalid inside the call to cli_list.  This
593                          * would happen if the list were expanded
594                          * during the call.
595                          * Fix from E. Jay Berkenbilt (ejb@ql.org)
596                          */
597                         pstring head;
598                         pstrcpy(head, do_list_queue_head());
599                         
600                         /* check for dfs */
601                         
602                         if ( !cli_resolve_path( "", cli, head, &targetcli, targetpath ) ) {
603                                 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
604                                 remove_do_list_queue_head();
605                                 continue;
606                         }
607                         
608                         cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
609                         remove_do_list_queue_head();
610                         if ((! do_list_queue_empty()) && (fn == display_finfo)) {
611                                 char* next_file = do_list_queue_head();
612                                 char* save_ch = 0;
613                                 if ((strlen(next_file) >= 2) &&
614                                     (next_file[strlen(next_file) - 1] == '*') &&
615                                     (next_file[strlen(next_file) - 2] == '\\')) {
616                                         save_ch = next_file +
617                                                 strlen(next_file) - 2;
618                                         *save_ch = '\0';
619                                 }
620                                 d_printf("\n%s\n",next_file);
621                                 if (save_ch) {
622                                         *save_ch = '\\';
623                                 }
624                         }
625                 }
626         } else {
627                 /* check for dfs */
628                         
629                 if ( cli_resolve_path( "", cli, mask, &targetcli, targetpath ) ) {
630                         if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1) 
631                                 d_printf("%s listing %s\n", cli_errstr(targetcli), targetpath);
632                 }
633                 else
634                         d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
635                 
636         }
637
638         in_do_list = 0;
639         reset_do_list_queue();
640 }
641
642 /****************************************************************************
643  Get a directory listing.
644 ****************************************************************************/
645
646 static int cmd_dir(void)
647 {
648         uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
649         pstring mask;
650         pstring buf;
651         char *p=buf;
652         int rc;
653         
654         dir_total = 0;
655         if (strcmp(cur_dir, "\\") != 0) {
656                 pstrcpy(mask,cur_dir);
657                 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!='\\'))
658                         pstrcat(mask,"\\");
659         } else {
660                 pstrcpy(mask, "\\");
661         }
662         
663         if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
664                 dos_format(p);
665                 if (*p == '\\')
666                         pstrcpy(mask,p + 1);
667                 else
668                         pstrcat(mask,p);
669         } else {
670                 pstrcat(mask,"*");
671         }
672
673         do_list(mask, attribute, display_finfo, recurse, True);
674
675         rc = do_dskattr();
676
677         DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
678
679         return rc;
680 }
681
682 /****************************************************************************
683  Get a directory listing.
684 ****************************************************************************/
685
686 static int cmd_du(void)
687 {
688         uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
689         pstring mask;
690         pstring buf;
691         char *p=buf;
692         int rc;
693         
694         dir_total = 0;
695         pstrcpy(mask,cur_dir);
696         if ((mask[0] != '\0') && (mask[strlen(mask)-1]!='\\'))
697                 pstrcat(mask,"\\");
698         
699         if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
700                 dos_format(p);
701                 if (*p == '\\')
702                         pstrcpy(mask,p);
703                 else
704                         pstrcat(mask,p);
705         } else {
706                 pstrcat(mask,"*");
707         }
708
709         do_list(mask, attribute, do_du, recurse, True);
710
711         rc = do_dskattr();
712
713         d_printf("Total number of bytes: %.0f\n", dir_total);
714
715         return rc;
716 }
717
718 /****************************************************************************
719  Get a file from rname to lname
720 ****************************************************************************/
721
722 static int do_get(char *rname, char *lname, BOOL reget)
723 {  
724         int handle = 0, fnum;
725         BOOL newhandle = False;
726         char *data;
727         struct timeval tp_start;
728         int read_size = io_bufsize;
729         uint16 attr;
730         SMB_OFF_T size;
731         off_t start = 0;
732         off_t nread = 0;
733         int rc = 0;
734         struct cli_state *targetcli;
735         pstring targetname;
736
737
738         if (lowercase) {
739                 strlower_m(lname);
740         }
741
742         if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
743                 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
744                 return 1;
745         }
746
747         GetTimeOfDay(&tp_start);
748         
749         if ( targetcli->dfsroot ) {
750                 pstring path;
751
752                 /* we need to refer to the full \server\share\path format 
753                    for dfs shares */
754
755                 pstrcpy( path, targetname );
756                 cli_dfs_make_full_path( targetname, targetcli->desthost, 
757                         targetcli->share, path);
758         }
759
760         fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
761
762         if (fnum == -1) {
763                 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
764                 return 1;
765         }
766
767         if(!strcmp(lname,"-")) {
768                 handle = fileno(stdout);
769         } else {
770                 if (reget) {
771                         handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
772                         if (handle >= 0) {
773                                 start = sys_lseek(handle, 0, SEEK_END);
774                                 if (start == -1) {
775                                         d_printf("Error seeking local file\n");
776                                         return 1;
777                                 }
778                         }
779                 } else {
780                         handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
781                 }
782                 newhandle = True;
783         }
784         if (handle < 0) {
785                 d_printf("Error opening local file %s\n",lname);
786                 return 1;
787         }
788
789
790         if (!cli_qfileinfo(targetcli, fnum, 
791                            &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
792             !cli_getattrE(targetcli, fnum, 
793                           &attr, &size, NULL, NULL, NULL)) {
794                 d_printf("getattrib: %s\n",cli_errstr(targetcli));
795                 return 1;
796         }
797
798         DEBUG(1,("getting file %s of size %.0f as %s ", 
799                  rname, (double)size, lname));
800
801         if(!(data = (char *)SMB_MALLOC(read_size))) { 
802                 d_printf("malloc fail for size %d\n", read_size);
803                 cli_close(targetcli, fnum);
804                 return 1;
805         }
806
807         while (1) {
808                 int n = cli_read(targetcli, fnum, data, nread + start, read_size);
809
810                 if (n <= 0)
811                         break;
812  
813                 if (writefile(handle,data, n) != n) {
814                         d_printf("Error writing local file\n");
815                         rc = 1;
816                         break;
817                 }
818       
819                 nread += n;
820         }
821
822         if (nread + start < size) {
823                 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
824                             rname, (long)nread));
825
826                 rc = 1;
827         }
828
829         SAFE_FREE(data);
830         
831         if (!cli_close(targetcli, fnum)) {
832                 d_printf("Error %s closing remote file\n",cli_errstr(cli));
833                 rc = 1;
834         }
835
836         if (newhandle) {
837                 close(handle);
838         }
839
840         if (archive_level >= 2 && (attr & aARCH)) {
841                 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
842         }
843
844         {
845                 struct timeval tp_end;
846                 int this_time;
847                 
848                 GetTimeOfDay(&tp_end);
849                 this_time = 
850                         (tp_end.tv_sec - tp_start.tv_sec)*1000 +
851                         (tp_end.tv_usec - tp_start.tv_usec)/1000;
852                 get_total_time_ms += this_time;
853                 get_total_size += nread;
854                 
855                 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
856                          nread / (1.024*this_time + 1.0e-4),
857                          get_total_size / (1.024*get_total_time_ms)));
858         }
859         
860         return rc;
861 }
862
863 /****************************************************************************
864  Get a file.
865 ****************************************************************************/
866
867 static int cmd_get(void)
868 {
869         pstring lname;
870         pstring rname;
871         char *p;
872
873         pstrcpy(rname,cur_dir);
874         pstrcat(rname,"\\");
875         
876         p = rname + strlen(rname);
877         
878         if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
879                 d_printf("get <filename>\n");
880                 return 1;
881         }
882         pstrcpy(lname,p);
883         dos_clean_name(rname);
884         
885         next_token_nr(NULL,lname,NULL,sizeof(lname));
886         
887         return do_get(rname, lname, False);
888 }
889
890 /****************************************************************************
891  Do an mget operation on one file.
892 ****************************************************************************/
893
894 static void do_mget(file_info *finfo)
895 {
896         pstring rname;
897         pstring quest;
898         pstring saved_curdir;
899         pstring mget_mask;
900
901         if (strequal(finfo->name,".") || strequal(finfo->name,".."))
902                 return;
903
904         if (abort_mget) {
905                 d_printf("mget aborted\n");
906                 return;
907         }
908
909         if (finfo->mode & aDIR)
910                 slprintf(quest,sizeof(pstring)-1,
911                          "Get directory %s? ",finfo->name);
912         else
913                 slprintf(quest,sizeof(pstring)-1,
914                          "Get file %s? ",finfo->name);
915
916         if (prompt && !yesno(quest))
917                 return;
918
919         if (!(finfo->mode & aDIR)) {
920                 pstrcpy(rname,cur_dir);
921                 pstrcat(rname,finfo->name);
922                 do_get(rname, finfo->name, False);
923                 return;
924         }
925
926         /* handle directories */
927         pstrcpy(saved_curdir,cur_dir);
928
929         pstrcat(cur_dir,finfo->name);
930         pstrcat(cur_dir,"\\");
931
932         unix_format(finfo->name);
933         if (lowercase)
934                 strlower_m(finfo->name);
935         
936         if (!directory_exist(finfo->name,NULL) && 
937             mkdir(finfo->name,0777) != 0) {
938                 d_printf("failed to create directory %s\n",finfo->name);
939                 pstrcpy(cur_dir,saved_curdir);
940                 return;
941         }
942         
943         if (chdir(finfo->name) != 0) {
944                 d_printf("failed to chdir to directory %s\n",finfo->name);
945                 pstrcpy(cur_dir,saved_curdir);
946                 return;
947         }
948
949         pstrcpy(mget_mask,cur_dir);
950         pstrcat(mget_mask,"*");
951         
952         do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
953         chdir("..");
954         pstrcpy(cur_dir,saved_curdir);
955 }
956
957 /****************************************************************************
958  View the file using the pager.
959 ****************************************************************************/
960
961 static int cmd_more(void)
962 {
963         pstring rname,lname,pager_cmd;
964         char *pager;
965         int fd;
966         int rc = 0;
967
968         pstrcpy(rname,cur_dir);
969         pstrcat(rname,"\\");
970         
971         slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
972         fd = smb_mkstemp(lname);
973         if (fd == -1) {
974                 d_printf("failed to create temporary file for more\n");
975                 return 1;
976         }
977         close(fd);
978
979         if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
980                 d_printf("more <filename>\n");
981                 unlink(lname);
982                 return 1;
983         }
984         dos_clean_name(rname);
985
986         rc = do_get(rname, lname, False);
987
988         pager=getenv("PAGER");
989
990         slprintf(pager_cmd,sizeof(pager_cmd)-1,
991                  "%s %s",(pager? pager:PAGER), lname);
992         system(pager_cmd);
993         unlink(lname);
994         
995         return rc;
996 }
997
998 /****************************************************************************
999  Do a mget command.
1000 ****************************************************************************/
1001
1002 static int cmd_mget(void)
1003 {
1004         uint16 attribute = aSYSTEM | aHIDDEN;
1005         pstring mget_mask;
1006         pstring buf;
1007         char *p=buf;
1008
1009         *mget_mask = 0;
1010
1011         if (recurse)
1012                 attribute |= aDIR;
1013         
1014         abort_mget = False;
1015
1016         while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1017                 pstrcpy(mget_mask,cur_dir);
1018                 if ((mget_mask[0] != '\0') && (mget_mask[strlen(mget_mask)-1]!='\\'))
1019                         pstrcat(mget_mask,"\\");
1020                 
1021                 if (*p == '\\')
1022                         pstrcpy(mget_mask,p);
1023                 else
1024                         pstrcat(mget_mask,p);
1025                 do_list(mget_mask, attribute,do_mget,False,True);
1026         }
1027
1028         if (!*mget_mask) {
1029                 pstrcpy(mget_mask,cur_dir);
1030                 if(mget_mask[strlen(mget_mask)-1]!='\\')
1031                         pstrcat(mget_mask,"\\");
1032                 pstrcat(mget_mask,"*");
1033                 do_list(mget_mask, attribute,do_mget,False,True);
1034         }
1035         
1036         return 0;
1037 }
1038
1039 /****************************************************************************
1040  Make a directory of name "name".
1041 ****************************************************************************/
1042
1043 static BOOL do_mkdir(char *name)
1044 {
1045         struct cli_state *targetcli;
1046         pstring targetname;
1047         
1048         if ( !cli_resolve_path( "", cli, name, &targetcli, targetname ) ) {
1049                 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1050                 return False;
1051         }
1052
1053         if (!cli_mkdir(targetcli, targetname)) {
1054                 d_printf("%s making remote directory %s\n",
1055                          cli_errstr(targetcli),name);
1056                 return(False);
1057         }
1058
1059         return(True);
1060 }
1061
1062 /****************************************************************************
1063  Show 8.3 name of a file.
1064 ****************************************************************************/
1065
1066 static BOOL do_altname(char *name)
1067 {
1068         pstring altname;
1069         if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1070                 d_printf("%s getting alt name for %s\n",
1071                          cli_errstr(cli),name);
1072                 return(False);
1073         }
1074         d_printf("%s\n", altname);
1075
1076         return(True);
1077 }
1078
1079 /****************************************************************************
1080  Exit client.
1081 ****************************************************************************/
1082
1083 static int cmd_quit(void)
1084 {
1085         cli_cm_shutdown();
1086         exit(0);
1087         /* NOTREACHED */
1088         return 0;
1089 }
1090
1091 /****************************************************************************
1092  Make a directory.
1093 ****************************************************************************/
1094
1095 static int cmd_mkdir(void)
1096 {
1097         pstring mask;
1098         pstring buf;
1099         char *p=buf;
1100   
1101         pstrcpy(mask,cur_dir);
1102
1103         if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1104                 if (!recurse)
1105                         d_printf("mkdir <dirname>\n");
1106                 return 1;
1107         }
1108         pstrcat(mask,p);
1109
1110         if (recurse) {
1111                 pstring ddir;
1112                 pstring ddir2;
1113                 *ddir2 = 0;
1114                 
1115                 pstrcpy(ddir,mask);
1116                 trim_char(ddir,'.','\0');
1117                 p = strtok(ddir,"/\\");
1118                 while (p) {
1119                         pstrcat(ddir2,p);
1120                         if (!cli_chkpath(cli, ddir2)) { 
1121                                 do_mkdir(ddir2);
1122                         }
1123                         pstrcat(ddir2,"\\");
1124                         p = strtok(NULL,"/\\");
1125                 }        
1126         } else {
1127                 do_mkdir(mask);
1128         }
1129         
1130         return 0;
1131 }
1132
1133 /****************************************************************************
1134  Show alt name.
1135 ****************************************************************************/
1136
1137 static int cmd_altname(void)
1138 {
1139         pstring name;
1140         pstring buf;
1141         char *p=buf;
1142   
1143         pstrcpy(name,cur_dir);
1144
1145         if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1146                 d_printf("altname <file>\n");
1147                 return 1;
1148         }
1149         pstrcat(name,p);
1150
1151         do_altname(name);
1152
1153         return 0;
1154 }
1155
1156 /****************************************************************************
1157  Put a single file.
1158 ****************************************************************************/
1159
1160 static int do_put(char *rname, char *lname, BOOL reput)
1161 {
1162         int fnum;
1163         XFILE *f;
1164         SMB_OFF_T start = 0;
1165         off_t nread = 0;
1166         char *buf = NULL;
1167         int maxwrite = io_bufsize;
1168         int rc = 0;
1169         struct timeval tp_start;
1170         struct cli_state *targetcli;
1171         pstring targetname;
1172         
1173         if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
1174                 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1175                 return 1;
1176         }
1177         
1178         GetTimeOfDay(&tp_start);
1179
1180         if (reput) {
1181                 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
1182                 if (fnum >= 0) {
1183                         if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1184                             !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
1185                                 d_printf("getattrib: %s\n",cli_errstr(cli));
1186                                 return 1;
1187                         }
1188                 }
1189         } else {
1190                 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1191         }
1192   
1193         if (fnum == -1) {
1194                 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1195                 return 1;
1196         }
1197
1198         /* allow files to be piped into smbclient
1199            jdblair 24.jun.98
1200
1201            Note that in this case this function will exit(0) rather
1202            than returning. */
1203         if (!strcmp(lname, "-")) {
1204                 f = x_stdin;
1205                 /* size of file is not known */
1206         } else {
1207                 f = x_fopen(lname,O_RDONLY, 0);
1208                 if (f && reput) {
1209                         if (x_tseek(f, start, SEEK_SET) == -1) {
1210                                 d_printf("Error seeking local file\n");
1211                                 return 1;
1212                         }
1213                 }
1214         }
1215
1216         if (!f) {
1217                 d_printf("Error opening local file %s\n",lname);
1218                 return 1;
1219         }
1220   
1221         DEBUG(1,("putting file %s as %s ",lname,
1222                  rname));
1223   
1224         buf = (char *)SMB_MALLOC(maxwrite);
1225         if (!buf) {
1226                 d_printf("ERROR: Not enough memory!\n");
1227                 return 1;
1228         }
1229         while (!x_feof(f)) {
1230                 int n = maxwrite;
1231                 int ret;
1232
1233                 if ((n = readfile(buf,n,f)) < 1) {
1234                         if((n == 0) && x_feof(f))
1235                                 break; /* Empty local file. */
1236
1237                         d_printf("Error reading local file: %s\n", strerror(errno));
1238                         rc = 1;
1239                         break;
1240                 }
1241
1242                 ret = cli_write(targetcli, fnum, 0, buf, nread + start, n);
1243
1244                 if (n != ret) {
1245                         d_printf("Error writing file: %s\n", cli_errstr(cli));
1246                         rc = 1;
1247                         break;
1248                 } 
1249
1250                 nread += n;
1251         }
1252
1253         if (!cli_close(targetcli, fnum)) {
1254                 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1255                 x_fclose(f);
1256                 SAFE_FREE(buf);
1257                 return 1;
1258         }
1259
1260         
1261         if (f != x_stdin) {
1262                 x_fclose(f);
1263         }
1264
1265         SAFE_FREE(buf);
1266
1267         {
1268                 struct timeval tp_end;
1269                 int this_time;
1270                 
1271                 GetTimeOfDay(&tp_end);
1272                 this_time = 
1273                         (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1274                         (tp_end.tv_usec - tp_start.tv_usec)/1000;
1275                 put_total_time_ms += this_time;
1276                 put_total_size += nread;
1277                 
1278                 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1279                          nread / (1.024*this_time + 1.0e-4),
1280                          put_total_size / (1.024*put_total_time_ms)));
1281         }
1282
1283         if (f == x_stdin) {
1284                 cli_cm_shutdown();
1285                 exit(0);
1286         }
1287         
1288         return rc;
1289 }
1290
1291 /****************************************************************************
1292  Put a file.
1293 ****************************************************************************/
1294
1295 static int cmd_put(void)
1296 {
1297         pstring lname;
1298         pstring rname;
1299         pstring buf;
1300         char *p=buf;
1301         
1302         pstrcpy(rname,cur_dir);
1303         pstrcat(rname,"\\");
1304   
1305         if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1306                 d_printf("put <filename>\n");
1307                 return 1;
1308         }
1309         pstrcpy(lname,p);
1310   
1311         if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1312                 pstrcat(rname,p);      
1313         else
1314                 pstrcat(rname,lname);
1315         
1316         dos_clean_name(rname);
1317
1318         {
1319                 SMB_STRUCT_STAT st;
1320                 /* allow '-' to represent stdin
1321                    jdblair, 24.jun.98 */
1322                 if (!file_exist(lname,&st) &&
1323                     (strcmp(lname,"-"))) {
1324                         d_printf("%s does not exist\n",lname);
1325                         return 1;
1326                 }
1327         }
1328
1329         return do_put(rname, lname, False);
1330 }
1331
1332 /*************************************
1333  File list structure.
1334 *************************************/
1335
1336 static struct file_list {
1337         struct file_list *prev, *next;
1338         char *file_path;
1339         BOOL isdir;
1340 } *file_list;
1341
1342 /****************************************************************************
1343  Free a file_list structure.
1344 ****************************************************************************/
1345
1346 static void free_file_list (struct file_list *list_head)
1347 {
1348         struct file_list *list, *next;
1349         
1350         for (list = list_head; list; list = next) {
1351                 next = list->next;
1352                 DLIST_REMOVE(list_head, list);
1353                 SAFE_FREE(list->file_path);
1354                 SAFE_FREE(list);
1355         }
1356 }
1357
1358 /****************************************************************************
1359  Seek in a directory/file list until you get something that doesn't start with
1360  the specified name.
1361 ****************************************************************************/
1362
1363 static BOOL seek_list(struct file_list *list, char *name)
1364 {
1365         while (list) {
1366                 trim_string(list->file_path,"./","\n");
1367                 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1368                         return(True);
1369                 }
1370                 list = list->next;
1371         }
1372       
1373         return(False);
1374 }
1375
1376 /****************************************************************************
1377  Set the file selection mask.
1378 ****************************************************************************/
1379
1380 static int cmd_select(void)
1381 {
1382         pstrcpy(fileselection,"");
1383         next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1384
1385         return 0;
1386 }
1387
1388 /****************************************************************************
1389   Recursive file matching function act as find
1390   match must be always set to True when calling this function
1391 ****************************************************************************/
1392
1393 static int file_find(struct file_list **list, const char *directory, 
1394                       const char *expression, BOOL match)
1395 {
1396         SMB_STRUCT_DIR *dir;
1397         struct file_list *entry;
1398         struct stat statbuf;
1399         int ret;
1400         char *path;
1401         BOOL isdir;
1402         const char *dname;
1403
1404         dir = sys_opendir(directory);
1405         if (!dir)
1406                 return -1;
1407         
1408         while ((dname = readdirname(dir))) {
1409                 if (!strcmp("..", dname))
1410                         continue;
1411                 if (!strcmp(".", dname))
1412                         continue;
1413                 
1414                 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1415                         continue;
1416                 }
1417
1418                 isdir = False;
1419                 if (!match || !gen_fnmatch(expression, dname)) {
1420                         if (recurse) {
1421                                 ret = stat(path, &statbuf);
1422                                 if (ret == 0) {
1423                                         if (S_ISDIR(statbuf.st_mode)) {
1424                                                 isdir = True;
1425                                                 ret = file_find(list, path, expression, False);
1426                                         }
1427                                 } else {
1428                                         d_printf("file_find: cannot stat file %s\n", path);
1429                                 }
1430                                 
1431                                 if (ret == -1) {
1432                                         SAFE_FREE(path);
1433                                         sys_closedir(dir);
1434                                         return -1;
1435                                 }
1436                         }
1437                         entry = SMB_MALLOC_P(struct file_list);
1438                         if (!entry) {
1439                                 d_printf("Out of memory in file_find\n");
1440                                 sys_closedir(dir);
1441                                 return -1;
1442                         }
1443                         entry->file_path = path;
1444                         entry->isdir = isdir;
1445                         DLIST_ADD(*list, entry);
1446                 } else {
1447                         SAFE_FREE(path);
1448                 }
1449         }
1450
1451         sys_closedir(dir);
1452         return 0;
1453 }
1454
1455 /****************************************************************************
1456  mput some files.
1457 ****************************************************************************/
1458
1459 static int cmd_mput(void)
1460 {
1461         pstring buf;
1462         char *p=buf;
1463         
1464         while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1465                 int ret;
1466                 struct file_list *temp_list;
1467                 char *quest, *lname, *rname;
1468         
1469                 file_list = NULL;
1470
1471                 ret = file_find(&file_list, ".", p, True);
1472                 if (ret) {
1473                         free_file_list(file_list);
1474                         continue;
1475                 }
1476                 
1477                 quest = NULL;
1478                 lname = NULL;
1479                 rname = NULL;
1480                                 
1481                 for (temp_list = file_list; temp_list; 
1482                      temp_list = temp_list->next) {
1483
1484                         SAFE_FREE(lname);
1485                         if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1486                                 continue;
1487                         trim_string(lname, "./", "/");
1488                         
1489                         /* check if it's a directory */
1490                         if (temp_list->isdir) {
1491                                 /* if (!recurse) continue; */
1492                                 
1493                                 SAFE_FREE(quest);
1494                                 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1495                                 if (prompt && !yesno(quest)) { /* No */
1496                                         /* Skip the directory */
1497                                         lname[strlen(lname)-1] = '/';
1498                                         if (!seek_list(temp_list, lname))
1499                                                 break;              
1500                                 } else { /* Yes */
1501                                         SAFE_FREE(rname);
1502                                         if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1503                                         dos_format(rname);
1504                                         if (!cli_chkpath(cli, rname) && 
1505                                             !do_mkdir(rname)) {
1506                                                 DEBUG (0, ("Unable to make dir, skipping..."));
1507                                                 /* Skip the directory */
1508                                                 lname[strlen(lname)-1] = '/';
1509                                                 if (!seek_list(temp_list, lname))
1510                                                         break;
1511                                         }
1512                                 }
1513                                 continue;
1514                         } else {
1515                                 SAFE_FREE(quest);
1516                                 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1517                                 if (prompt && !yesno(quest)) /* No */
1518                                         continue;
1519                                 
1520                                 /* Yes */
1521                                 SAFE_FREE(rname);
1522                                 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1523                         }
1524
1525                         dos_format(rname);
1526
1527                         do_put(rname, lname, False);
1528                 }
1529                 free_file_list(file_list);
1530                 SAFE_FREE(quest);
1531                 SAFE_FREE(lname);
1532                 SAFE_FREE(rname);
1533         }
1534
1535         return 0;
1536 }
1537
1538 /****************************************************************************
1539  Cancel a print job.
1540 ****************************************************************************/
1541
1542 static int do_cancel(int job)
1543 {
1544         if (cli_printjob_del(cli, job)) {
1545                 d_printf("Job %d cancelled\n",job);
1546                 return 0;
1547         } else {
1548                 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1549                 return 1;
1550         }
1551 }
1552
1553 /****************************************************************************
1554  Cancel a print job.
1555 ****************************************************************************/
1556
1557 static int cmd_cancel(void)
1558 {
1559         pstring buf;
1560         int job; 
1561
1562         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1563                 d_printf("cancel <jobid> ...\n");
1564                 return 1;
1565         }
1566         do {
1567                 job = atoi(buf);
1568                 do_cancel(job);
1569         } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1570         
1571         return 0;
1572 }
1573
1574 /****************************************************************************
1575  Print a file.
1576 ****************************************************************************/
1577
1578 static int cmd_print(void)
1579 {
1580         pstring lname;
1581         pstring rname;
1582         char *p;
1583
1584         if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1585                 d_printf("print <filename>\n");
1586                 return 1;
1587         }
1588
1589         pstrcpy(rname,lname);
1590         p = strrchr_m(rname,'/');
1591         if (p) {
1592                 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1593         }
1594
1595         if (strequal(lname,"-")) {
1596                 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1597         }
1598
1599         return do_put(rname, lname, False);
1600 }
1601
1602 /****************************************************************************
1603  Show a print queue entry.
1604 ****************************************************************************/
1605
1606 static void queue_fn(struct print_job_info *p)
1607 {
1608         d_printf("%-6d   %-9d    %s\n", (int)p->id, (int)p->size, p->name);
1609 }
1610
1611 /****************************************************************************
1612  Show a print queue.
1613 ****************************************************************************/
1614
1615 static int cmd_queue(void)
1616 {
1617         cli_print_queue(cli, queue_fn);
1618         
1619         return 0;
1620 }
1621
1622 /****************************************************************************
1623  Delete some files.
1624 ****************************************************************************/
1625
1626 static void do_del(file_info *finfo)
1627 {
1628         pstring mask;
1629
1630         pstr_sprintf( mask, "%s\\%s", finfo->dir, finfo->name );
1631
1632         if (finfo->mode & aDIR) 
1633                 return;
1634
1635         if (!cli_unlink(cli, mask)) {
1636                 d_printf("%s deleting remote file %s\n",cli_errstr(cli),mask);
1637         }
1638 }
1639
1640 /****************************************************************************
1641  Delete some files.
1642 ****************************************************************************/
1643
1644 static int cmd_del(void)
1645 {
1646         pstring mask;
1647         pstring buf;
1648         uint16 attribute = aSYSTEM | aHIDDEN;
1649
1650         if (recurse)
1651                 attribute |= aDIR;
1652         
1653         pstrcpy(mask,cur_dir);
1654         
1655         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1656                 d_printf("del <filename>\n");
1657                 return 1;
1658         }
1659         pstrcat(mask,buf);
1660
1661         do_list(mask, attribute,do_del,False,False);
1662         
1663         return 0;
1664 }
1665
1666 /****************************************************************************
1667 ****************************************************************************/
1668
1669 static int cmd_open(void)
1670 {
1671         pstring mask;
1672         pstring buf;
1673         struct cli_state *targetcli;
1674         pstring targetname;
1675         int fnum;
1676
1677         pstrcpy(mask,cur_dir);
1678         
1679         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1680                 d_printf("open <filename>\n");
1681                 return 1;
1682         }
1683         pstrcat(mask,buf);
1684
1685         if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1686                 d_printf("open %s: %s\n", mask, cli_errstr(cli));
1687                 return 1;
1688         }
1689         
1690         fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA);
1691         d_printf("open file %s: fnum %d\n", targetname, fnum);
1692
1693         return 0;
1694 }
1695
1696 static int cmd_close(void)
1697 {
1698         fstring buf;
1699         int fnum;
1700
1701         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1702                 d_printf("close <fnum>\n");
1703                 return 1;
1704         }
1705
1706         fnum = atoi(buf);
1707         /* We really should use the targetcli here.... */
1708         if (!cli_close(cli, fnum)) {
1709                 d_printf("close %d: %s\n", fnum, cli_errstr(cli));
1710                 return 1;
1711         }
1712         return 0;
1713 }
1714
1715 static int cmd_posix(void)
1716 {
1717         uint16 major, minor;
1718         uint32 caplow, caphigh;
1719         pstring caps;
1720
1721         if (!SERVER_HAS_UNIX_CIFS(cli)) {
1722                 d_printf("Server doesn't support UNIX CIFS extensions.\n");
1723                 return 1;
1724         }
1725
1726         if (!cli_unix_extensions_version(cli, &major, &minor, &caplow, &caphigh)) {
1727                 d_printf("Can't get UNIX CIFS extensions version from server.\n");
1728                 return 1;
1729         }
1730
1731         d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
1732
1733         *caps = '\0';
1734         if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
1735                 pstrcat(caps, "locks ");
1736         }
1737         if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
1738                 pstrcat(caps, "acls ");
1739         }
1740         if (caplow & CIFS_UNIX_XATTTR_CAP) {
1741                 pstrcat(caps, "eas ");
1742         }
1743         if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
1744                 pstrcat(caps, "pathnames ");
1745         }
1746
1747         if (strlen(caps) > 0 && caps[strlen(caps)-1] == ' ') {
1748                 caps[strlen(caps)-1] = '\0';
1749         }
1750
1751         if (!cli_set_unix_extensions_capabilities(cli, major, minor, caplow, caphigh)) {
1752                 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n", cli_errstr(cli));
1753                 return 1;
1754         }
1755
1756         d_printf("Selecting server supported CIFS capabilities %s\n", caps);
1757         return 0;
1758 }
1759
1760 /****************************************************************************
1761  Remove a directory.
1762 ****************************************************************************/
1763
1764 static int cmd_rmdir(void)
1765 {
1766         pstring mask;
1767         pstring buf;
1768         struct cli_state *targetcli;
1769         pstring targetname;
1770   
1771         pstrcpy(mask,cur_dir);
1772         
1773         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1774                 d_printf("rmdir <dirname>\n");
1775                 return 1;
1776         }
1777         pstrcat(mask,buf);
1778
1779         if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1780                 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
1781                 return 1;
1782         }
1783         
1784         if (!cli_rmdir(targetcli, targetname)) {
1785                 d_printf("%s removing remote directory file %s\n",
1786                          cli_errstr(targetcli),mask);
1787         }
1788         
1789         return 0;
1790 }
1791
1792 /****************************************************************************
1793  UNIX hardlink.
1794 ****************************************************************************/
1795
1796 static int cmd_link(void)
1797 {
1798         pstring oldname,newname;
1799         pstring buf,buf2;
1800         struct cli_state *targetcli;
1801         pstring targetname;
1802   
1803         pstrcpy(oldname,cur_dir);
1804         pstrcpy(newname,cur_dir);
1805   
1806         if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
1807             !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1808                 d_printf("link <oldname> <newname>\n");
1809                 return 1;
1810         }
1811
1812         pstrcat(oldname,buf);
1813         pstrcat(newname,buf2);
1814
1815         if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
1816                 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
1817                 return 1;
1818         }
1819         
1820         if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1821                 d_printf("Server doesn't support UNIX CIFS calls.\n");
1822                 return 1;
1823         }
1824         
1825         if (!cli_unix_hardlink(targetcli, targetname, newname)) {
1826                 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
1827                 return 1;
1828         }  
1829
1830         return 0;
1831 }
1832
1833 /****************************************************************************
1834  UNIX symlink.
1835 ****************************************************************************/
1836
1837 static int cmd_symlink(void)
1838 {
1839         pstring oldname,newname;
1840         pstring buf,buf2;
1841   
1842         if (!SERVER_HAS_UNIX_CIFS(cli)) {
1843                 d_printf("Server doesn't support UNIX CIFS calls.\n");
1844                 return 1;
1845         }
1846
1847         pstrcpy(newname,cur_dir);
1848         
1849         if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
1850             !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1851                 d_printf("symlink <oldname> <newname>\n");
1852                 return 1;
1853         }
1854
1855         pstrcpy(oldname,buf);
1856         pstrcat(newname,buf2);
1857
1858         if (!cli_unix_symlink(cli, oldname, newname)) {
1859                 d_printf("%s symlinking files (%s -> %s)\n",
1860                         cli_errstr(cli), newname, oldname);
1861                 return 1;
1862         } 
1863
1864         return 0;
1865 }
1866
1867 /****************************************************************************
1868  UNIX chmod.
1869 ****************************************************************************/
1870
1871 static int cmd_chmod(void)
1872 {
1873         pstring src;
1874         mode_t mode;
1875         pstring buf, buf2;
1876         struct cli_state *targetcli;
1877         pstring targetname;
1878   
1879         pstrcpy(src,cur_dir);
1880         
1881         if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
1882             !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1883                 d_printf("chmod mode file\n");
1884                 return 1;
1885         }
1886
1887         mode = (mode_t)strtol(buf, NULL, 8);
1888         pstrcat(src,buf2);
1889
1890         if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
1891                 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
1892                 return 1;
1893         }
1894         
1895         if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
1896                 d_printf("Server doesn't support UNIX CIFS calls.\n");
1897                 return 1;
1898         }
1899         
1900         if (!cli_unix_chmod(targetcli, targetname, mode)) {
1901                 d_printf("%s chmod file %s 0%o\n",
1902                         cli_errstr(targetcli), src, (unsigned int)mode);
1903                 return 1;
1904         } 
1905
1906         return 0;
1907 }
1908
1909 static const char *filetype_to_str(mode_t mode)
1910 {
1911         if (S_ISREG(mode)) {
1912                 return "regular file";
1913         } else if (S_ISDIR(mode)) {
1914                 return "directory";
1915         } else 
1916 #ifdef S_ISCHR
1917         if (S_ISCHR(mode)) {
1918                 return "character device";
1919         } else
1920 #endif
1921 #ifdef S_ISBLK
1922         if (S_ISBLK(mode)) {
1923                 return "block device";
1924         } else
1925 #endif
1926 #ifdef S_ISFIFO
1927         if (S_ISFIFO(mode)) {
1928                 return "fifo";
1929         } else
1930 #endif
1931 #ifdef S_ISLNK
1932         if (S_ISLNK(mode)) {
1933                 return "symbolic link";
1934         } else
1935 #endif
1936 #ifdef S_ISSOCK
1937         if (S_ISSOCK(mode)) {
1938                 return "socket";
1939         } else
1940 #endif
1941         return "";
1942 }
1943
1944 static char rwx_to_str(mode_t m, mode_t bt, char ret)
1945 {
1946         if (m & bt) {
1947                 return ret;
1948         } else {
1949                 return '-';
1950         }
1951 }
1952
1953 static char *unix_mode_to_str(char *s, mode_t m)
1954 {
1955         char *p = s;
1956         const char *str = filetype_to_str(m);
1957
1958         switch(str[0]) {
1959                 case 'd':
1960                         *p++ = 'd';
1961                         break;
1962                 case 'c':
1963                         *p++ = 'c';
1964                         break;
1965                 case 'b':
1966                         *p++ = 'b';
1967                         break;
1968                 case 'f':
1969                         *p++ = 'p';
1970                         break;
1971                 case 's':
1972                         *p++ = str[1] == 'y' ? 'l' : 's';
1973                         break;
1974                 case 'r':
1975                 default:
1976                         *p++ = '-';
1977                         break;
1978         }
1979         *p++ = rwx_to_str(m, S_IRUSR, 'r');
1980         *p++ = rwx_to_str(m, S_IWUSR, 'w');
1981         *p++ = rwx_to_str(m, S_IXUSR, 'x');
1982         *p++ = rwx_to_str(m, S_IRGRP, 'r');
1983         *p++ = rwx_to_str(m, S_IWGRP, 'w');
1984         *p++ = rwx_to_str(m, S_IXGRP, 'x');
1985         *p++ = rwx_to_str(m, S_IROTH, 'r');
1986         *p++ = rwx_to_str(m, S_IWOTH, 'w');
1987         *p++ = rwx_to_str(m, S_IXOTH, 'x');
1988         *p++ = '\0';
1989         return s;
1990 }
1991
1992 /****************************************************************************
1993  Utility function for UNIX getfacl.
1994 ****************************************************************************/
1995
1996 static char *perms_to_string(fstring permstr, unsigned char perms)
1997 {
1998         fstrcpy(permstr, "---");
1999         if (perms & SMB_POSIX_ACL_READ) {
2000                 permstr[0] = 'r';
2001         }
2002         if (perms & SMB_POSIX_ACL_WRITE) {
2003                 permstr[1] = 'w';
2004         }
2005         if (perms & SMB_POSIX_ACL_EXECUTE) {
2006                 permstr[2] = 'x';
2007         }
2008         return permstr;
2009 }
2010
2011 /****************************************************************************
2012  UNIX getfacl.
2013 ****************************************************************************/
2014
2015 static int cmd_getfacl(void)
2016 {
2017         pstring src, name;
2018         uint16 major, minor;
2019         uint32 caplow, caphigh;
2020         char *retbuf = NULL;
2021         size_t rb_size = 0;
2022         SMB_STRUCT_STAT sbuf;
2023         uint16 num_file_acls = 0;
2024         uint16 num_dir_acls = 0;
2025         uint16 i;
2026         struct cli_state *targetcli;
2027         pstring targetname;
2028  
2029         pstrcpy(src,cur_dir);
2030         
2031         if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2032                 d_printf("stat file\n");
2033                 return 1;
2034         }
2035
2036         pstrcat(src,name);
2037         
2038         if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2039                 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2040                 return 1;
2041         }
2042         
2043         if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2044                 d_printf("Server doesn't support UNIX CIFS calls.\n");
2045                 return 1;
2046         }
2047         
2048         if (!cli_unix_extensions_version(targetcli, &major, &minor, &caplow, &caphigh)) {
2049                 d_printf("Can't get UNIX CIFS version from server.\n");
2050                 return 1;
2051         }
2052
2053         if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
2054                 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
2055                 return 1;
2056         }
2057
2058
2059         if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2060                 d_printf("%s getfacl doing a stat on file %s\n",
2061                         cli_errstr(targetcli), src);
2062                 return 1;
2063         } 
2064
2065         if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
2066                 d_printf("%s getfacl file %s\n",
2067                         cli_errstr(targetcli), src);
2068                 return 1;
2069         } 
2070
2071         /* ToDo : Print out the ACL values. */
2072         if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
2073                 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2074                         src, (unsigned int)CVAL(retbuf,0) );
2075                 SAFE_FREE(retbuf);
2076                 return 1;
2077         }
2078
2079         num_file_acls = SVAL(retbuf,2);
2080         num_dir_acls = SVAL(retbuf,4);
2081         if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
2082                 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2083                         src,
2084                         (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
2085                         (unsigned int)rb_size);
2086
2087                 SAFE_FREE(retbuf);
2088                 return 1;
2089         }
2090
2091         d_printf("# file: %s\n", src);
2092         d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
2093
2094         if (num_file_acls == 0 && num_dir_acls == 0) {
2095                 d_printf("No acls found.\n");
2096         }
2097
2098         for (i = 0; i < num_file_acls; i++) {
2099                 uint32 uorg;
2100                 fstring permstring;
2101                 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
2102                 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2103
2104                 switch(tagtype) {
2105                         case SMB_POSIX_ACL_USER_OBJ:
2106                                 d_printf("user::");
2107                                 break;
2108                         case SMB_POSIX_ACL_USER:
2109                                 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2110                                 d_printf("user:%u:", uorg);
2111                                 break;
2112                         case SMB_POSIX_ACL_GROUP_OBJ:
2113                                 d_printf("group::");
2114                                 break;
2115                         case SMB_POSIX_ACL_GROUP:
2116                                 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2117                                 d_printf("group:%u", uorg);
2118                                 break;
2119                         case SMB_POSIX_ACL_MASK:
2120                                 d_printf("mask::");
2121                                 break;
2122                         case SMB_POSIX_ACL_OTHER:
2123                                 d_printf("other::");
2124                                 break;
2125                         default:
2126                                 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2127                                         src, (unsigned int)tagtype );
2128                                 SAFE_FREE(retbuf);
2129                                 return 1;
2130                 }
2131
2132                 d_printf("%s\n", perms_to_string(permstring, perms));
2133         }
2134
2135         for (i = 0; i < num_dir_acls; i++) {
2136                 uint32 uorg;
2137                 fstring permstring;
2138                 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
2139                 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2140
2141                 switch(tagtype) {
2142                         case SMB_POSIX_ACL_USER_OBJ:
2143                                 d_printf("default:user::");
2144                                 break;
2145                         case SMB_POSIX_ACL_USER:
2146                                 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2147                                 d_printf("default:user:%u:", uorg);
2148                                 break;
2149                         case SMB_POSIX_ACL_GROUP_OBJ:
2150                                 d_printf("default:group::");
2151                                 break;
2152                         case SMB_POSIX_ACL_GROUP:
2153                                 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2154                                 d_printf("default:group:%u", uorg);
2155                                 break;
2156                         case SMB_POSIX_ACL_MASK:
2157                                 d_printf("default:mask::");
2158                                 break;
2159                         case SMB_POSIX_ACL_OTHER:
2160                                 d_printf("default:other::");
2161                                 break;
2162                         default:
2163                                 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2164                                         src, (unsigned int)tagtype );
2165                                 SAFE_FREE(retbuf);
2166                                 return 1;
2167                 }
2168
2169                 d_printf("%s\n", perms_to_string(permstring, perms));
2170         }
2171
2172         SAFE_FREE(retbuf);
2173         return 0;
2174 }
2175
2176 /****************************************************************************
2177  UNIX stat.
2178 ****************************************************************************/
2179
2180 static int cmd_stat(void)
2181 {
2182         pstring src, name;
2183         fstring mode_str;
2184         SMB_STRUCT_STAT sbuf;
2185         struct cli_state *targetcli;
2186         struct tm *lt;
2187         pstring targetname;
2188  
2189         if (!SERVER_HAS_UNIX_CIFS(cli)) {
2190                 d_printf("Server doesn't support UNIX CIFS calls.\n");
2191                 return 1;
2192         }
2193
2194         pstrcpy(src,cur_dir);
2195         
2196         if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2197                 d_printf("stat file\n");
2198                 return 1;
2199         }
2200
2201         pstrcat(src,name);
2202
2203         
2204         if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2205                 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2206                 return 1;
2207         }
2208         
2209         if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2210                 d_printf("%s stat file %s\n",
2211                         cli_errstr(targetcli), src);
2212                 return 1;
2213         } 
2214
2215         /* Print out the stat values. */
2216         d_printf("File: %s\n", src);
2217         d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2218                 (double)sbuf.st_size,
2219                 (unsigned int)sbuf.st_blocks,
2220                 filetype_to_str(sbuf.st_mode));
2221
2222 #if defined(S_ISCHR) && defined(S_ISBLK)
2223         if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
2224                 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2225                         (double)sbuf.st_ino,
2226                         (unsigned int)sbuf.st_nlink,
2227                         unix_dev_major(sbuf.st_rdev),
2228                         unix_dev_minor(sbuf.st_rdev));
2229         } else 
2230 #endif
2231                 d_printf("Inode: %.0f\tLinks: %u\n",
2232                         (double)sbuf.st_ino,
2233                         (unsigned int)sbuf.st_nlink);
2234
2235         d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2236                 ((int)sbuf.st_mode & 0777),
2237                 unix_mode_to_str(mode_str, sbuf.st_mode),
2238                 (unsigned int)sbuf.st_uid, 
2239                 (unsigned int)sbuf.st_gid);
2240
2241         lt = localtime(&sbuf.st_atime);
2242         if (lt) {
2243                 strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
2244         } else {
2245                 fstrcpy(mode_str, "unknown");
2246         }
2247         d_printf("Access: %s\n", mode_str);
2248
2249         lt = localtime(&sbuf.st_mtime);
2250         if (lt) {
2251                 strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
2252         } else {
2253                 fstrcpy(mode_str, "unknown");
2254         }
2255         d_printf("Modify: %s\n", mode_str);
2256
2257         lt = localtime(&sbuf.st_ctime);
2258         if (lt) {
2259                 strftime(mode_str, sizeof(mode_str), "%F %T %z", lt);
2260         } else {
2261                 fstrcpy(mode_str, "unknown");
2262         }
2263         d_printf("Change: %s\n", mode_str);
2264         
2265         return 0;
2266 }
2267
2268
2269 /****************************************************************************
2270  UNIX chown.
2271 ****************************************************************************/
2272
2273 static int cmd_chown(void)
2274 {
2275         pstring src;
2276         uid_t uid;
2277         gid_t gid;
2278         pstring buf, buf2, buf3;
2279         struct cli_state *targetcli;
2280         pstring targetname;
2281   
2282         pstrcpy(src,cur_dir);
2283         
2284         if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
2285             !next_token_nr(NULL,buf2,NULL, sizeof(buf2)) ||
2286             !next_token_nr(NULL,buf3,NULL, sizeof(buf3))) {
2287                 d_printf("chown uid gid file\n");
2288                 return 1;
2289         }
2290
2291         uid = (uid_t)atoi(buf);
2292         gid = (gid_t)atoi(buf2);
2293         pstrcat(src,buf3);
2294
2295         if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2296                 d_printf("chown %s: %s\n", src, cli_errstr(cli));
2297                 return 1;
2298         }
2299
2300
2301         if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2302                 d_printf("Server doesn't support UNIX CIFS calls.\n");
2303                 return 1;
2304         }
2305         
2306         if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
2307                 d_printf("%s chown file %s uid=%d, gid=%d\n",
2308                         cli_errstr(targetcli), src, (int)uid, (int)gid);
2309                 return 1;
2310         } 
2311
2312         return 0;
2313 }
2314
2315 /****************************************************************************
2316  Rename some file.
2317 ****************************************************************************/
2318
2319 static int cmd_rename(void)
2320 {
2321         pstring src,dest;
2322         pstring buf,buf2;
2323   
2324         pstrcpy(src,cur_dir);
2325         pstrcpy(dest,cur_dir);
2326         
2327         if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
2328             !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2329                 d_printf("rename <src> <dest>\n");
2330                 return 1;
2331         }
2332
2333         pstrcat(src,buf);
2334         pstrcat(dest,buf2);
2335
2336         if (!cli_rename(cli, src, dest)) {
2337                 d_printf("%s renaming files\n",cli_errstr(cli));
2338                 return 1;
2339         }
2340         
2341         return 0;
2342 }
2343
2344 /****************************************************************************
2345  Print the volume name.
2346 ****************************************************************************/
2347
2348 static int cmd_volume(void)
2349 {
2350         fstring volname;
2351         uint32 serial_num;
2352         time_t create_date;
2353   
2354         if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
2355                 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
2356                 return 1;
2357         }
2358         
2359         d_printf("Volume: |%s| serial number 0x%x\n", volname, (unsigned int)serial_num);
2360         return 0;
2361 }
2362
2363 /****************************************************************************
2364  Hard link files using the NT call.
2365 ****************************************************************************/
2366
2367 static int cmd_hardlink(void)
2368 {
2369         pstring src,dest;
2370         pstring buf,buf2;
2371         struct cli_state *targetcli;
2372         pstring targetname;
2373   
2374         pstrcpy(src,cur_dir);
2375         pstrcpy(dest,cur_dir);
2376         
2377         if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
2378             !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2379                 d_printf("hardlink <src> <dest>\n");
2380                 return 1;
2381         }
2382
2383         pstrcat(src,buf);
2384         pstrcat(dest,buf2);
2385
2386         if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2387                 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
2388                 return 1;
2389         }
2390         
2391         if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2392                 d_printf("Server doesn't support UNIX CIFS calls.\n");
2393                 return 1;
2394         }
2395         
2396         if (!cli_nt_hardlink(targetcli, targetname, dest)) {
2397                 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
2398                 return 1;
2399         }
2400         
2401         return 0;
2402 }
2403
2404 /****************************************************************************
2405  Toggle the prompt flag.
2406 ****************************************************************************/
2407
2408 static int cmd_prompt(void)
2409 {
2410         prompt = !prompt;
2411         DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2412         
2413         return 1;
2414 }
2415
2416 /****************************************************************************
2417  Set the newer than time.
2418 ****************************************************************************/
2419
2420 static int cmd_newer(void)
2421 {
2422         pstring buf;
2423         BOOL ok;
2424         SMB_STRUCT_STAT sbuf;
2425
2426         ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2427         if (ok && (sys_stat(buf,&sbuf) == 0)) {
2428                 newer_than = sbuf.st_mtime;
2429                 DEBUG(1,("Getting files newer than %s",
2430                          time_to_asc(&newer_than)));
2431         } else {
2432                 newer_than = 0;
2433         }
2434
2435         if (ok && newer_than == 0) {
2436                 d_printf("Error setting newer-than time\n");
2437                 return 1;
2438         }
2439
2440         return 0;
2441 }
2442
2443 /****************************************************************************
2444  Set the archive level.
2445 ****************************************************************************/
2446
2447 static int cmd_archive(void)
2448 {
2449         pstring buf;
2450
2451         if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2452                 archive_level = atoi(buf);
2453         } else
2454                 d_printf("Archive level is %d\n",archive_level);
2455
2456         return 0;
2457 }
2458
2459 /****************************************************************************
2460  Toggle the lowercaseflag.
2461 ****************************************************************************/
2462
2463 static int cmd_lowercase(void)
2464 {
2465         lowercase = !lowercase;
2466         DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2467
2468         return 0;
2469 }
2470
2471 /****************************************************************************
2472  Toggle the case sensitive flag.
2473 ****************************************************************************/
2474
2475 static int cmd_setcase(void)
2476 {
2477         BOOL orig_case_sensitive = cli_set_case_sensitive(cli, False);
2478
2479         cli_set_case_sensitive(cli, !orig_case_sensitive);
2480         DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
2481                 "on":"off"));
2482
2483         return 0;
2484 }
2485
2486 /****************************************************************************
2487  Toggle the recurse flag.
2488 ****************************************************************************/
2489
2490 static int cmd_recurse(void)
2491 {
2492         recurse = !recurse;
2493         DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2494
2495         return 0;
2496 }
2497
2498 /****************************************************************************
2499  Toggle the translate flag.
2500 ****************************************************************************/
2501
2502 static int cmd_translate(void)
2503 {
2504         translation = !translation;
2505         DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2506                  translation?"on":"off"));
2507
2508         return 0;
2509 }
2510
2511 /****************************************************************************
2512  Do the lcd command.
2513  ****************************************************************************/
2514
2515 static int cmd_lcd(void)
2516 {
2517         pstring buf;
2518         pstring d;
2519         
2520         if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2521                 chdir(buf);
2522         DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2523
2524         return 0;
2525 }
2526
2527 /****************************************************************************
2528  Get a file restarting at end of local file.
2529  ****************************************************************************/
2530
2531 static int cmd_reget(void)
2532 {
2533         pstring local_name;
2534         pstring remote_name;
2535         char *p;
2536
2537         pstrcpy(remote_name, cur_dir);
2538         pstrcat(remote_name, "\\");
2539         
2540         p = remote_name + strlen(remote_name);
2541         
2542         if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2543                 d_printf("reget <filename>\n");
2544                 return 1;
2545         }
2546         pstrcpy(local_name, p);
2547         dos_clean_name(remote_name);
2548         
2549         next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2550         
2551         return do_get(remote_name, local_name, True);
2552 }
2553
2554 /****************************************************************************
2555  Put a file restarting at end of local file.
2556  ****************************************************************************/
2557
2558 static int cmd_reput(void)
2559 {
2560         pstring local_name;
2561         pstring remote_name;
2562         pstring buf;
2563         char *p = buf;
2564         SMB_STRUCT_STAT st;
2565         
2566         pstrcpy(remote_name, cur_dir);
2567         pstrcat(remote_name, "\\");
2568   
2569         if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2570                 d_printf("reput <filename>\n");
2571                 return 1;
2572         }
2573         pstrcpy(local_name, p);
2574   
2575         if (!file_exist(local_name, &st)) {
2576                 d_printf("%s does not exist\n", local_name);
2577                 return 1;
2578         }
2579
2580         if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2581                 pstrcat(remote_name, p);
2582         else
2583                 pstrcat(remote_name, local_name);
2584         
2585         dos_clean_name(remote_name);
2586
2587         return do_put(remote_name, local_name, True);
2588 }
2589
2590 /****************************************************************************
2591  List a share name.
2592  ****************************************************************************/
2593
2594 static void browse_fn(const char *name, uint32 m, 
2595                       const char *comment, void *state)
2596 {
2597         fstring typestr;
2598
2599         *typestr=0;
2600
2601         switch (m & 7)
2602         {
2603           case STYPE_DISKTREE:
2604             fstrcpy(typestr,"Disk"); break;
2605           case STYPE_PRINTQ:
2606             fstrcpy(typestr,"Printer"); break;
2607           case STYPE_DEVICE:
2608             fstrcpy(typestr,"Device"); break;
2609           case STYPE_IPC:
2610             fstrcpy(typestr,"IPC"); break;
2611         }
2612         /* FIXME: If the remote machine returns non-ascii characters
2613            in any of these fields, they can corrupt the output.  We
2614            should remove them. */
2615         if (!grepable) {
2616                 d_printf("\t%-15s %-10.10s%s\n",
2617                         name,typestr,comment);
2618         } else {
2619                 d_printf ("%s|%s|%s\n",typestr,name,comment);
2620         }
2621 }
2622
2623 static BOOL browse_host_rpc(BOOL sort)
2624 {
2625         NTSTATUS status;
2626         struct rpc_pipe_client *pipe_hnd;
2627         TALLOC_CTX *mem_ctx;
2628         ENUM_HND enum_hnd;
2629         WERROR werr;
2630         SRV_SHARE_INFO_CTR ctr;
2631         int i;
2632
2633         mem_ctx = talloc_new(NULL);
2634         if (mem_ctx == NULL) {
2635                 DEBUG(0, ("talloc_new failed\n"));
2636                 return False;
2637         }
2638
2639         init_enum_hnd(&enum_hnd, 0);
2640
2641         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
2642
2643         if (pipe_hnd == NULL) {
2644                 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
2645                            nt_errstr(status)));
2646                 TALLOC_FREE(mem_ctx);
2647                 return False;
2648         }
2649
2650         werr = rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, 1, &ctr,
2651                                             0xffffffff, &enum_hnd);
2652
2653         if (!W_ERROR_IS_OK(werr)) {
2654                 TALLOC_FREE(mem_ctx);
2655                 cli_rpc_pipe_close(pipe_hnd);
2656                 return False;
2657         }
2658
2659         for (i=0; i<ctr.num_entries; i++) {
2660                 SRV_SHARE_INFO_1 *info = &ctr.share.info1[i];
2661                 char *name, *comment;
2662                 name = rpcstr_pull_unistr2_talloc(
2663                         mem_ctx, &info->info_1_str.uni_netname);
2664                 comment = rpcstr_pull_unistr2_talloc(
2665                         mem_ctx, &info->info_1_str.uni_remark);
2666                 browse_fn(name, info->info_1.type, comment, NULL);
2667         }
2668
2669         TALLOC_FREE(mem_ctx);
2670         cli_rpc_pipe_close(pipe_hnd);
2671         return True;
2672 }
2673
2674 /****************************************************************************
2675  Try and browse available connections on a host.
2676 ****************************************************************************/
2677
2678 static BOOL browse_host(BOOL sort)
2679 {
2680         int ret;
2681         if (!grepable) {
2682                 d_printf("\n\tSharename       Type      Comment\n");
2683                 d_printf("\t---------       ----      -------\n");
2684         }
2685
2686         if (browse_host_rpc(sort)) {
2687                 return True;
2688         }
2689
2690         if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
2691                 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
2692
2693         return (ret != -1);
2694 }
2695
2696 /****************************************************************************
2697  List a server name.
2698 ****************************************************************************/
2699
2700 static void server_fn(const char *name, uint32 m, 
2701                       const char *comment, void *state)
2702 {
2703         
2704         if (!grepable){
2705                 d_printf("\t%-16s     %s\n", name, comment);
2706         } else {
2707                 d_printf("%s|%s|%s\n",(char *)state, name, comment);
2708         }
2709 }
2710
2711 /****************************************************************************
2712  Try and browse available connections on a host.
2713 ****************************************************************************/
2714
2715 static BOOL list_servers(const char *wk_grp)
2716 {
2717         fstring state;
2718
2719         if (!cli->server_domain)
2720                 return False;
2721
2722         if (!grepable) {
2723                 d_printf("\n\tServer               Comment\n");
2724                 d_printf("\t---------            -------\n");
2725         };
2726         fstrcpy( state, "Server" );
2727         cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
2728                           state);
2729
2730         if (!grepable) {
2731                 d_printf("\n\tWorkgroup            Master\n");
2732                 d_printf("\t---------            -------\n");
2733         }; 
2734
2735         fstrcpy( state, "Workgroup" );
2736         cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
2737                           server_fn, state);
2738         return True;
2739 }
2740
2741 /****************************************************************************
2742  Print or set current VUID
2743 ****************************************************************************/
2744
2745 static int cmd_vuid(void)
2746 {
2747         fstring buf;
2748         
2749         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2750                 d_printf("Current VUID is %d\n", cli->vuid);
2751                 return 0;
2752         }
2753
2754         cli->vuid = atoi(buf);
2755         return 0;
2756 }
2757
2758 /****************************************************************************
2759  Setup a new VUID, by issuing a session setup
2760 ****************************************************************************/
2761
2762 static int cmd_logon(void)
2763 {
2764         pstring l_username, l_password;
2765         pstring buf,buf2;
2766   
2767         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2768                 d_printf("logon <username> [<password>]\n");
2769                 return 0;
2770         }
2771
2772         pstrcpy(l_username, buf);
2773
2774         if (!next_token_nr(NULL,buf2,NULL,sizeof(buf))) 
2775         {
2776                 char *pass = getpass("Password: ");
2777                 if (pass) 
2778                         pstrcpy(l_password, pass);
2779         } 
2780         else
2781                 pstrcpy(l_password, buf2);
2782
2783         if (!cli_session_setup(cli, l_username, 
2784                                l_password, strlen(l_password),
2785                                l_password, strlen(l_password),
2786                                lp_workgroup())) {
2787                 d_printf("session setup failed: %s\n", cli_errstr(cli));
2788                 return -1;
2789         }
2790
2791         d_printf("Current VUID is %d\n", cli->vuid);
2792         return 0;
2793 }
2794
2795
2796 /****************************************************************************
2797  list active connections
2798 ****************************************************************************/
2799
2800 static int cmd_list_connect(void)
2801 {
2802         cli_cm_display();
2803
2804         return 0;
2805 }
2806
2807 /****************************************************************************
2808  display the current active client connection
2809 ****************************************************************************/
2810
2811 static int cmd_show_connect( void )
2812 {
2813         struct cli_state *targetcli;
2814         pstring targetpath;
2815         
2816         if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
2817                 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
2818                 return 1;
2819         }
2820         
2821         d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
2822         return 0;
2823 }
2824
2825 /* Some constants for completing filename arguments */
2826
2827 #define COMPL_NONE        0          /* No completions */
2828 #define COMPL_REMOTE      1          /* Complete remote filename */
2829 #define COMPL_LOCAL       2          /* Complete local filename */
2830
2831 /* This defines the commands supported by this client.
2832  * NOTE: The "!" must be the last one in the list because it's fn pointer
2833  *       field is NULL, and NULL in that field is used in process_tok()
2834  *       (below) to indicate the end of the list.  crh
2835  */
2836 static struct
2837 {
2838   const char *name;
2839   int (*fn)(void);
2840   const char *description;
2841   char compl_args[2];      /* Completion argument info */
2842 } commands[] = {
2843   {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2844   {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2845   {"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}},
2846   {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
2847   {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2848   {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
2849   {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2850   {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2851   {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2852   {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
2853   {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2854   {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2855   {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2856   {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2857   {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2858   {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
2859   {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2860   {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2861   {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2862   {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2863   {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2864   {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},  
2865   {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2866   {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2867   {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2868   {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2869   {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2870   {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},  
2871   {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2872   {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2873   {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2874   {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
2875   {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2876   {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},  
2877   {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2878   {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2879   {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2880   {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2881   {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2882   {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2883   {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},  
2884   {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2885   {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2886   {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2887   {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2888   {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2889   {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
2890   {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
2891   {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2892   {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
2893   {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
2894   {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2895   {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
2896   {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
2897   {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
2898   {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
2899   {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
2900   
2901   /* Yes, this must be here, see crh's comment above. */
2902   {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2903   {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2904 };
2905
2906 /*******************************************************************
2907  Lookup a command string in the list of commands, including 
2908  abbreviations.
2909 ******************************************************************/
2910
2911 static int process_tok(pstring tok)
2912 {
2913         int i = 0, matches = 0;
2914         int cmd=0;
2915         int tok_len = strlen(tok);
2916         
2917         while (commands[i].fn != NULL) {
2918                 if (strequal(commands[i].name,tok)) {
2919                         matches = 1;
2920                         cmd = i;
2921                         break;
2922                 } else if (strnequal(commands[i].name, tok, tok_len)) {
2923                         matches++;
2924                         cmd = i;
2925                 }
2926                 i++;
2927         }
2928   
2929         if (matches == 0)
2930                 return(-1);
2931         else if (matches == 1)
2932                 return(cmd);
2933         else
2934                 return(-2);
2935 }
2936
2937 /****************************************************************************
2938  Help.
2939 ****************************************************************************/
2940
2941 static int cmd_help(void)
2942 {
2943         int i=0,j;
2944         pstring buf;
2945         
2946         if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2947                 if ((i = process_tok(buf)) >= 0)
2948                         d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
2949         } else {
2950                 while (commands[i].description) {
2951                         for (j=0; commands[i].description && (j<5); j++) {
2952                                 d_printf("%-15s",commands[i].name);
2953                                 i++;
2954                         }
2955                         d_printf("\n");
2956                 }
2957         }
2958         return 0;
2959 }
2960
2961 /****************************************************************************
2962  Process a -c command string.
2963 ****************************************************************************/
2964
2965 static int process_command_string(char *cmd)
2966 {
2967         pstring line;
2968         const char *ptr;
2969         int rc = 0;
2970
2971         /* establish the connection if not already */
2972         
2973         if (!cli) {
2974                 cli = cli_cm_open(desthost, service, True);
2975                 if (!cli)
2976                         return 0;
2977         }
2978         
2979         while (cmd[0] != '\0')    {
2980                 char *p;
2981                 pstring tok;
2982                 int i;
2983                 
2984                 if ((p = strchr_m(cmd, ';')) == 0) {
2985                         strncpy(line, cmd, 999);
2986                         line[1000] = '\0';
2987                         cmd += strlen(cmd);
2988                 } else {
2989                         if (p - cmd > 999)
2990                                 p = cmd + 999;
2991                         strncpy(line, cmd, p - cmd);
2992                         line[p - cmd] = '\0';
2993                         cmd = p + 1;
2994                 }
2995                 
2996                 /* and get the first part of the command */
2997                 ptr = line;
2998                 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
2999                 
3000                 if ((i = process_tok(tok)) >= 0) {
3001                         rc = commands[i].fn();
3002                 } else if (i == -2) {
3003                         d_printf("%s: command abbreviation ambiguous\n",tok);
3004                 } else {
3005                         d_printf("%s: command not found\n",tok);
3006                 }
3007         }
3008         
3009         return rc;
3010 }       
3011
3012 #define MAX_COMPLETIONS 100
3013
3014 typedef struct {
3015         pstring dirmask;
3016         char **matches;
3017         int count, samelen;
3018         const char *text;
3019         int len;
3020 } completion_remote_t;
3021
3022 static void completion_remote_filter(const char *mnt, file_info *f, const char *mask, void *state)
3023 {
3024         completion_remote_t *info = (completion_remote_t *)state;
3025
3026         if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
3027                 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
3028                         info->matches[info->count] = SMB_STRDUP(f->name);
3029                 else {
3030                         pstring tmp;
3031
3032                         if (info->dirmask[0] != 0)
3033                                 pstrcpy(tmp, info->dirmask);
3034                         else
3035                                 tmp[0] = 0;
3036                         pstrcat(tmp, f->name);
3037                         if (f->mode & aDIR)
3038                                 pstrcat(tmp, "/");
3039                         info->matches[info->count] = SMB_STRDUP(tmp);
3040                 }
3041                 if (info->matches[info->count] == NULL)
3042                         return;
3043                 if (f->mode & aDIR)
3044                         smb_readline_ca_char(0);
3045
3046                 if (info->count == 1)
3047                         info->samelen = strlen(info->matches[info->count]);
3048                 else
3049                         while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
3050                                 info->samelen--;
3051                 info->count++;
3052         }
3053 }
3054
3055 static char **remote_completion(const char *text, int len)
3056 {
3057         pstring dirmask;
3058         int i;
3059         completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
3060
3061         /* can't have non-static intialisation on Sun CC, so do it
3062            at run time here */
3063         info.samelen = len;
3064         info.text = text;
3065         info.len = len;
3066                 
3067         if (len >= MIN(PATH_MAX,sizeof(pstring))) {
3068                 return(NULL);
3069         }
3070
3071         info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
3072         if (!info.matches) {
3073                 return NULL;
3074         }
3075         info.matches[0] = NULL;
3076
3077         for (i = len-1; i >= 0; i--) {
3078                 if ((text[i] == '/') || (text[i] == '\\')) {
3079                         break;
3080                 }
3081         }
3082
3083         info.text = text+i+1;
3084         info.samelen = info.len = len-i-1;
3085
3086         if (i > 0) {
3087                 strncpy(info.dirmask, text, i+1);
3088                 info.dirmask[i+1] = 0;
3089                 pstr_sprintf(dirmask, "%s%*s*", cur_dir, i-1, text);
3090         } else {
3091                 pstr_sprintf(dirmask, "%s*", cur_dir);
3092         }
3093
3094         if (cli_list(cli, dirmask, aDIR | aSYSTEM | aHIDDEN, completion_remote_filter, &info) < 0)
3095                 goto cleanup;
3096
3097         if (info.count == 2)
3098                 info.matches[0] = SMB_STRDUP(info.matches[1]);
3099         else {
3100                 info.matches[0] = SMB_MALLOC(info.samelen+1);
3101                 if (!info.matches[0])
3102                         goto cleanup;
3103                 strncpy(info.matches[0], info.matches[1], info.samelen);
3104                 info.matches[0][info.samelen] = 0;
3105         }
3106         info.matches[info.count] = NULL;
3107         return info.matches;
3108
3109 cleanup:
3110         for (i = 0; i < info.count; i++)
3111                 free(info.matches[i]);
3112         free(info.matches);
3113         return NULL;
3114 }
3115
3116 static char **completion_fn(const char *text, int start, int end)
3117 {
3118         smb_readline_ca_char(' ');
3119
3120         if (start) {
3121                 const char *buf, *sp;
3122                 int i;
3123                 char compl_type;
3124
3125                 buf = smb_readline_get_line_buffer();
3126                 if (buf == NULL)
3127                         return NULL;
3128                 
3129                 sp = strchr(buf, ' ');
3130                 if (sp == NULL)
3131                         return NULL;
3132                 
3133                 for (i = 0; commands[i].name; i++)
3134                         if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
3135                                 break;
3136                 if (commands[i].name == NULL)
3137                         return NULL;
3138
3139                 while (*sp == ' ')
3140                         sp++;
3141
3142                 if (sp == (buf + start))
3143                         compl_type = commands[i].compl_args[0];
3144                 else
3145                         compl_type = commands[i].compl_args[1];
3146
3147                 if (compl_type == COMPL_REMOTE)
3148                         return remote_completion(text, end - start);
3149                 else /* fall back to local filename completion */
3150                         return NULL;
3151         } else {
3152                 char **matches;
3153                 int i, len, samelen = 0, count=1;
3154
3155                 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
3156                 if (!matches) {
3157                         return NULL;
3158                 }
3159                 matches[0] = NULL;
3160
3161                 len = strlen(text);
3162                 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3163                         if (strncmp(text, commands[i].name, len) == 0) {
3164                                 matches[count] = SMB_STRDUP(commands[i].name);
3165                                 if (!matches[count])
3166                                         goto cleanup;
3167                                 if (count == 1)
3168                                         samelen = strlen(matches[count]);
3169                                 else
3170                                         while (strncmp(matches[count], matches[count-1], samelen) != 0)
3171                                                 samelen--;
3172                                 count++;
3173                         }
3174                 }
3175
3176                 switch (count) {
3177                 case 0: /* should never happen */
3178                 case 1:
3179                         goto cleanup;
3180                 case 2:
3181                         matches[0] = SMB_STRDUP(matches[1]);
3182                         break;
3183                 default:
3184                         matches[0] = SMB_MALLOC(samelen+1);
3185                         if (!matches[0])
3186                                 goto cleanup;
3187                         strncpy(matches[0], matches[1], samelen);
3188                         matches[0][samelen] = 0;
3189                 }
3190                 matches[count] = NULL;
3191                 return matches;
3192
3193 cleanup:
3194                 for (i = 0; i < count; i++)
3195                         free(matches[i]);
3196
3197                 free(matches);
3198                 return NULL;
3199         }
3200 }
3201
3202 /****************************************************************************
3203  Make sure we swallow keepalives during idle time.
3204 ****************************************************************************/
3205
3206 static void readline_callback(void)
3207 {
3208         fd_set fds;
3209         struct timeval timeout;
3210         static time_t last_t;
3211         time_t t;
3212
3213         t = time(NULL);
3214
3215         if (t - last_t < 5)
3216                 return;
3217
3218         last_t = t;
3219
3220  again:
3221
3222         if (cli->fd == -1)
3223                 return;
3224
3225         FD_ZERO(&fds);
3226         FD_SET(cli->fd,&fds);
3227
3228         timeout.tv_sec = 0;
3229         timeout.tv_usec = 0;
3230         sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
3231                 
3232         /* We deliberately use receive_smb instead of
3233            client_receive_smb as we want to receive
3234            session keepalives and then drop them here.
3235         */
3236         if (FD_ISSET(cli->fd,&fds)) {
3237                 if (!receive_smb(cli->fd,cli->inbuf,0)) {
3238                         DEBUG(0, ("Read from server failed, maybe it closed the "
3239                                 "connection\n"));
3240                         return;
3241                 }
3242                 goto again;
3243         }
3244       
3245         /* Ping the server to keep the connection alive using SMBecho. */
3246         {
3247                 unsigned char garbage[16];
3248                 memset(garbage, 0xf0, sizeof(garbage));
3249                 cli_echo(cli, garbage, sizeof(garbage));
3250         }
3251 }
3252
3253 /****************************************************************************
3254  Process commands on stdin.
3255 ****************************************************************************/
3256
3257 static int process_stdin(void)
3258 {
3259         const char *ptr;
3260         int rc = 0;
3261
3262         while (1) {
3263                 pstring tok;
3264                 pstring the_prompt;
3265                 char *cline;
3266                 pstring line;
3267                 int i;
3268                 
3269                 /* display a prompt */
3270                 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3271                 cline = smb_readline(the_prompt, readline_callback, completion_fn);
3272                         
3273                 if (!cline) break;
3274                 
3275                 pstrcpy(line, cline);
3276
3277                 /* special case - first char is ! */
3278                 if (*line == '!') {
3279                         system(line + 1);
3280                         continue;
3281                 }
3282       
3283                 /* and get the first part of the command */
3284                 ptr = line;
3285                 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3286
3287                 if ((i = process_tok(tok)) >= 0) {
3288                         rc = commands[i].fn();
3289                 } else if (i == -2) {
3290                         d_printf("%s: command abbreviation ambiguous\n",tok);
3291                 } else {
3292                         d_printf("%s: command not found\n",tok);
3293                 }
3294         }
3295         return rc;
3296 }
3297
3298 /****************************************************************************
3299  Process commands from the client.
3300 ****************************************************************************/
3301
3302 static int process(char *base_directory)
3303 {
3304         int rc = 0;
3305
3306         cli = cli_cm_open(desthost, service, True);
3307         if (!cli) {
3308                 return 1;
3309         }
3310
3311         if (*base_directory) {
3312                 rc = do_cd(base_directory);
3313                 if (rc) {
3314                         cli_cm_shutdown();
3315                         return rc;
3316                 }
3317         }
3318         
3319         if (cmdstr) {
3320                 rc = process_command_string(cmdstr);
3321         } else {
3322                 process_stdin();
3323         }
3324   
3325         cli_cm_shutdown();
3326         return rc;
3327 }
3328
3329 /****************************************************************************
3330  Handle a -L query.
3331 ****************************************************************************/
3332
3333 static int do_host_query(char *query_host)
3334 {
3335         cli = cli_cm_open(query_host, "IPC$", True);
3336         if (!cli)
3337                 return 1;
3338
3339         browse_host(True);
3340
3341         if (port != 139) {
3342
3343                 /* Workgroups simply don't make sense over anything
3344                    else but port 139... */
3345
3346                 cli_cm_shutdown();
3347                 cli_cm_set_port( 139 );
3348                 cli = cli_cm_open(query_host, "IPC$", True);
3349         }
3350
3351         if (cli == NULL) {
3352                 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3353                 return 1;
3354         }
3355
3356         list_servers(lp_workgroup());
3357
3358         cli_cm_shutdown();
3359         
3360         return(0);
3361 }
3362
3363 /****************************************************************************
3364  Handle a tar operation.
3365 ****************************************************************************/
3366
3367 static int do_tar_op(char *base_directory)
3368 {
3369         int ret;
3370
3371         /* do we already have a connection? */
3372         if (!cli) {
3373                 cli = cli_cm_open(desthost, service, True);
3374                 if (!cli)
3375                         return 1;
3376         }
3377
3378         recurse=True;
3379
3380         if (*base_directory)  {
3381                 ret = do_cd(base_directory);
3382                 if (ret) {
3383                         cli_cm_shutdown();
3384                         return ret;
3385                 }
3386         }
3387         
3388         ret=process_tar();
3389
3390         cli_cm_shutdown();
3391
3392         return(ret);
3393 }
3394
3395 /****************************************************************************
3396  Handle a message operation.
3397 ****************************************************************************/
3398
3399 static int do_message_op(void)
3400 {
3401         struct in_addr ip;
3402         struct nmb_name called, calling;
3403         fstring server_name;
3404         char name_type_hex[10];
3405         int msg_port;
3406
3407         make_nmb_name(&calling, calling_name, 0x0);
3408         make_nmb_name(&called , desthost, name_type);
3409
3410         fstrcpy(server_name, desthost);
3411         snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
3412         fstrcat(server_name, name_type_hex);
3413
3414         zero_ip(&ip);
3415         if (have_ip) 
3416                 ip = dest_ip;
3417
3418         /* we can only do messages over port 139 (to windows clients at least) */
3419
3420         msg_port = port ? port : 139;
3421
3422         if (!(cli=cli_initialise()) || (cli_set_port(cli, msg_port) != msg_port) ||
3423             !cli_connect(cli, server_name, &ip)) {
3424                 d_printf("Connection to %s failed\n", desthost);
3425                 return 1;
3426         }
3427
3428         if (!cli_session_request(cli, &calling, &called)) {
3429                 d_printf("session request failed\n");
3430                 cli_cm_shutdown();
3431                 return 1;
3432         }
3433
3434         send_message();
3435         cli_cm_shutdown();
3436
3437         return 0;
3438 }
3439
3440
3441 /****************************************************************************
3442   main program
3443 ****************************************************************************/
3444
3445  int main(int argc,char *argv[])
3446 {
3447         pstring base_directory;
3448         int opt;
3449         pstring query_host;
3450         BOOL message = False;
3451         pstring term_code;
3452         static const char *new_name_resolve_order = NULL;
3453         poptContext pc;
3454         char *p;
3455         int rc = 0;
3456         fstring new_workgroup;
3457         struct poptOption long_options[] = {
3458                 POPT_AUTOHELP
3459
3460                 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3461                 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3462                 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3463                 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3464                 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3465                 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3466                 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
3467                 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3468                 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3469                 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" }, 
3470                 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
3471                 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3472                 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
3473                 POPT_COMMON_SAMBA
3474                 POPT_COMMON_CONNECTION
3475                 POPT_COMMON_CREDENTIALS
3476                 POPT_TABLEEND
3477         };
3478         
3479         load_case_tables();
3480
3481 #ifdef KANJI
3482         pstrcpy(term_code, KANJI);
3483 #else /* KANJI */
3484         *term_code = 0;
3485 #endif /* KANJI */
3486
3487         *query_host = 0;
3488         *base_directory = 0;
3489         
3490         /* initialize the workgroup name so we can determine whether or 
3491            not it was set by a command line option */
3492            
3493         set_global_myworkgroup( "" );
3494         set_global_myname( "" );
3495
3496         /* set default debug level to 0 regardless of what smb.conf sets */
3497         setup_logging( "smbclient", True );
3498         DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
3499         if ((dbf = x_fdup(x_stderr))) {
3500                 x_setbuf( dbf, NULL );
3501         }
3502
3503         pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 
3504                                 POPT_CONTEXT_KEEP_FIRST);
3505         poptSetOtherOptionHelp(pc, "service <password>");
3506
3507         in_client = True;   /* Make sure that we tell lp_load we are */
3508
3509         while ((opt = poptGetNextOpt(pc)) != -1) {
3510                 switch (opt) {
3511                 case 'M':
3512                         /* Messages are sent to NetBIOS name type 0x3
3513                          * (Messenger Service).  Make sure we default
3514                          * to port 139 instead of port 445. srl,crh
3515                          */
3516                         name_type = 0x03; 
3517                         cli_cm_set_dest_name_type( name_type );
3518                         pstrcpy(desthost,poptGetOptArg(pc));
3519                         if( !port )
3520                                 cli_cm_set_port( 139 );
3521                         message = True;
3522                         break;
3523                 case 'I':
3524                         {
3525                                 dest_ip = *interpret_addr2(poptGetOptArg(pc));
3526                                 if (is_zero_ip(dest_ip))
3527                                         exit(1);
3528                                 have_ip = True;
3529
3530                                 cli_cm_set_dest_ip( dest_ip );
3531                         }
3532                         break;
3533                 case 'E':
3534                         if (dbf) {
3535                                 x_fclose(dbf);
3536                         }
3537                         dbf = x_stderr;
3538                         display_set_stderr();
3539                         break;
3540
3541                 case 'L':
3542                         pstrcpy(query_host, poptGetOptArg(pc));
3543                         break;
3544                 case 't':
3545                         pstrcpy(term_code, poptGetOptArg(pc));
3546                         break;
3547                 case 'm':
3548                         max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
3549                         break;
3550                 case 'T':
3551                         /* We must use old option processing for this. Find the
3552                          * position of the -T option in the raw argv[]. */
3553                         {
3554                                 int i, optnum;
3555                                 for (i = 1; i < argc; i++) {
3556                                         if (strncmp("-T", argv[i],2)==0)
3557                                                 break;
3558                                 }
3559                                 i++;
3560                                 if (!(optnum = tar_parseargs(argc, argv, poptGetOptArg(pc), i))) {
3561                                         poptPrintUsage(pc, stderr, 0);
3562                                         exit(1);
3563                                 }
3564                                 /* Now we must eat (optnum - i) options - they have
3565                                  * been processed by tar_parseargs().
3566                                  */
3567                                 optnum -= i;
3568                                 for (i = 0; i < optnum; i++)
3569                                         poptGetOptArg(pc);
3570                         }
3571                         break;
3572                 case 'D':
3573                         pstrcpy(base_directory,poptGetOptArg(pc));
3574                         break;
3575                 case 'g':
3576                         grepable=True;
3577                         break;
3578                 }
3579         }
3580
3581         poptGetArg(pc);
3582
3583         /* check for the -P option */
3584
3585         if ( port != 0 )
3586                 cli_cm_set_port( port );
3587
3588         /*
3589          * Don't load debug level from smb.conf. It should be
3590          * set by cmdline arg or remain default (0)
3591          */
3592         AllowDebugChange = False;
3593         
3594         /* save the workgroup...
3595         
3596            FIXME!! do we need to do this for other options as well 
3597            (or maybe a generic way to keep lp_load() from overwriting 
3598            everything)?  */
3599         
3600         fstrcpy( new_workgroup, lp_workgroup() );
3601         pstrcpy( calling_name, global_myname() );
3602         
3603         if ( override_logfile )
3604                 setup_logging( lp_logfile(), False );
3605         
3606         if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
3607                 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
3608                         argv[0], dyn_CONFIGFILE);
3609         }
3610         
3611         load_interfaces();
3612         
3613         if ( strlen(new_workgroup) != 0 )
3614                 set_global_myworkgroup( new_workgroup );
3615
3616         if ( strlen(calling_name) != 0 )
3617                 set_global_myname( calling_name );
3618         else
3619                 pstrcpy( calling_name, global_myname() );
3620
3621         if(poptPeekArg(pc)) {
3622                 pstrcpy(service,poptGetArg(pc));  
3623                 /* Convert any '/' characters in the service name to '\' characters */
3624                 string_replace(service, '/','\\');
3625
3626                 if (count_chars(service,'\\') < 3) {
3627                         d_printf("\n%s: Not enough '\\' characters in service\n",service);
3628                         poptPrintUsage(pc, stderr, 0);
3629                         exit(1);
3630                 }
3631         }
3632
3633         if (poptPeekArg(pc) && !cmdline_auth_info.got_pass) { 
3634                 cmdline_auth_info.got_pass = True;
3635                 pstrcpy(cmdline_auth_info.password,poptGetArg(pc));  
3636         }
3637
3638         init_names();
3639
3640         if(new_name_resolve_order)
3641                 lp_set_name_resolve_order(new_name_resolve_order);
3642
3643         if (!tar_type && !*query_host && !*service && !message) {
3644                 poptPrintUsage(pc, stderr, 0);
3645                 exit(1);
3646         }
3647
3648         poptFreeContext(pc);
3649
3650         /* store the username an password for dfs support */
3651
3652         cli_cm_set_credentials( &cmdline_auth_info );
3653         pstrcpy(username, cmdline_auth_info.username);
3654
3655         DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
3656
3657         if (tar_type) {
3658                 if (cmdstr)
3659                         process_command_string(cmdstr);
3660                 return do_tar_op(base_directory);
3661         }
3662
3663         if (*query_host) {
3664                 char *qhost = query_host;
3665                 char *slash;
3666
3667                 while (*qhost == '\\' || *qhost == '/')
3668                         qhost++;
3669
3670                 if ((slash = strchr_m(qhost, '/'))
3671                     || (slash = strchr_m(qhost, '\\'))) {
3672                         *slash = 0;
3673                 }
3674
3675                 if ((p=strchr_m(qhost, '#'))) {
3676                         *p = 0;
3677                         p++;
3678                         sscanf(p, "%x", &name_type);
3679                         cli_cm_set_dest_name_type( name_type );
3680                 }
3681
3682                 return do_host_query(qhost);
3683         }
3684
3685         if (message) {
3686                 return do_message_op();
3687         }
3688         
3689         if (process(base_directory)) {
3690                 return 1;
3691         }
3692
3693         return rc;
3694 }