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