r12620: Get rid of automatically generated lists of init functions of subsystems.
[jelmer/samba4-debian.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-2004
7    Copyright (C) James J Myers   2003 <myersjj@samba.org>
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "version.h"
26 #include "clilist.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "librpc/gen_ndr/ndr_srvsvc.h"
29 #include "librpc/gen_ndr/ndr_lsa.h"
30 #include "libcli/raw/libcliraw.h"
31 #include "system/dir.h"
32 #include "system/filesys.h"
33 #include "dlinklist.h"
34 #include "system/readline.h"
35 #include "pstring.h"
36 #include "auth/gensec/gensec.h"
37
38 static struct smbcli_state *cli;
39 static int port = 0;
40 static pstring cur_dir = "\\";
41 static pstring cd_path = "";
42 static pstring service;
43 static pstring desthost;
44 static char *cmdstr = NULL;
45
46 static int io_bufsize = 64512;
47
48 static int name_type = 0x20;
49
50 static int process_tok(fstring tok);
51 static int cmd_help(const char **cmd_ptr);
52
53 static time_t newer_than = 0;
54 static int archive_level = 0;
55
56 static BOOL translation = False;
57
58 static BOOL prompt = True;
59
60 static int printmode = 1;
61
62 static BOOL recurse = False;
63 static BOOL lowercase = False;
64
65 static const char *dest_ip;
66
67 static BOOL abort_mget = True;
68
69 static pstring fileselection = "";
70
71 /* timing globals */
72 static uint64_t get_total_size = 0;
73 static uint_t get_total_time_ms = 0;
74 static uint64_t put_total_size = 0;
75 static uint_t put_total_time_ms = 0;
76
77 /* totals globals */
78 static double dir_total;
79
80
81 /*******************************************************************
82  Reduce a file name, removing .. elements.
83 ********************************************************************/
84 void dos_clean_name(char *s)
85 {
86         char *p=NULL;
87
88         DEBUG(3,("dos_clean_name [%s]\n",s));
89
90         /* remove any double slashes */
91         all_string_sub(s, "\\\\", "\\", 0);
92
93         while ((p = strstr(s,"\\..\\")) != NULL) {
94                 pstring s1;
95
96                 *p = 0;
97                 pstrcpy(s1,p+3);
98
99                 if ((p=strrchr_m(s,'\\')) != NULL)
100                         *p = 0;
101                 else
102                         *s = 0;
103                 pstrcat(s,s1);
104         }  
105
106         trim_string(s,NULL,"\\..");
107
108         all_string_sub(s, "\\.\\", "\\", 0);
109 }
110
111 /****************************************************************************
112 write to a local file with CR/LF->LF translation if appropriate. return the 
113 number taken from the buffer. This may not equal the number written.
114 ****************************************************************************/
115 static int writefile(int f, const void *_b, int n)
116 {
117         const uint8_t *b = _b;
118         int i;
119
120         if (!translation) {
121                 return write(f,b,n);
122         }
123
124         i = 0;
125         while (i < n) {
126                 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
127                         b++;i++;
128                 }
129                 if (write(f, b, 1) != 1) {
130                         break;
131                 }
132                 b++;
133                 i++;
134         }
135   
136         return(i);
137 }
138
139 /****************************************************************************
140   read from a file with LF->CR/LF translation if appropriate. return the 
141   number read. read approx n bytes.
142 ****************************************************************************/
143 static int readfile(void *_b, int n, XFILE *f)
144 {
145         uint8_t *b = _b;
146         int i;
147         int c;
148
149         if (!translation)
150                 return x_fread(b,1,n,f);
151   
152         i = 0;
153         while (i < (n - 1)) {
154                 if ((c = x_getc(f)) == EOF) {
155                         break;
156                 }
157       
158                 if (c == '\n') { /* change all LFs to CR/LF */
159                         b[i++] = '\r';
160                 }
161       
162                 b[i++] = c;
163         }
164   
165         return(i);
166 }
167  
168
169 /****************************************************************************
170 send a message
171 ****************************************************************************/
172 static void send_message(void)
173 {
174         int total_len = 0;
175         int grp_id;
176
177         if (!smbcli_message_start(cli->tree, desthost, cli_credentials_get_username(cmdline_credentials), &grp_id)) {
178                 d_printf("message start: %s\n", smbcli_errstr(cli->tree));
179                 return;
180         }
181
182
183         d_printf("Connected. Type your message, ending it with a Control-D\n");
184
185         while (!feof(stdin) && total_len < 1600) {
186                 int maxlen = MIN(1600 - total_len,127);
187                 pstring msg;
188                 int l=0;
189                 int c;
190
191                 ZERO_STRUCT(msg);
192
193                 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
194                         if (c == '\n')
195                                 msg[l++] = '\r';
196                         msg[l] = c;   
197                 }
198
199                 if (!smbcli_message_text(cli->tree, msg, l, grp_id)) {
200                         d_printf("SMBsendtxt failed (%s)\n",smbcli_errstr(cli->tree));
201                         return;
202                 }      
203                 
204                 total_len += l;
205         }
206
207         if (total_len >= 1600)
208                 d_printf("the message was truncated to 1600 bytes\n");
209         else
210                 d_printf("sent %d bytes\n",total_len);
211
212         if (!smbcli_message_end(cli->tree, grp_id)) {
213                 d_printf("SMBsendend failed (%s)\n",smbcli_errstr(cli->tree));
214                 return;
215         }      
216 }
217
218
219
220 /****************************************************************************
221 check the space on a device
222 ****************************************************************************/
223 static int do_dskattr(void)
224 {
225         int total, bsize, avail;
226
227         if (NT_STATUS_IS_ERR(smbcli_dskattr(cli->tree, &bsize, &total, &avail))) {
228                 d_printf("Error in dskattr: %s\n",smbcli_errstr(cli->tree)); 
229                 return 1;
230         }
231
232         d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
233                  total, bsize, avail);
234
235         return 0;
236 }
237
238 /****************************************************************************
239 show cd/pwd
240 ****************************************************************************/
241 static int cmd_pwd(const char **cmd_ptr)
242 {
243         d_printf("Current directory is %s",service);
244         d_printf("%s\n",cur_dir);
245         return 0;
246 }
247
248 /*
249   convert a string to dos format
250 */
251 static void dos_format(char *s)
252 {
253         string_replace(s, '/', '\\');
254 }
255
256 /****************************************************************************
257 change directory - inner section
258 ****************************************************************************/
259 static int do_cd(char *newdir)
260 {
261         char *p = newdir;
262         pstring saved_dir;
263         pstring dname;
264       
265         dos_format(newdir);
266
267         /* Save the current directory in case the
268            new directory is invalid */
269         pstrcpy(saved_dir, cur_dir);
270         if (*p == '\\')
271                 pstrcpy(cur_dir,p);
272         else
273                 pstrcat(cur_dir,p);
274         if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
275                 pstrcat(cur_dir, "\\");
276         }
277         dos_clean_name(cur_dir);
278         pstrcpy(dname,cur_dir);
279         pstrcat(cur_dir,"\\");
280         dos_clean_name(cur_dir);
281         
282         if (!strequal(cur_dir,"\\")) {
283                 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, dname))) {
284                         d_printf("cd %s: %s\n", dname, smbcli_errstr(cli->tree));
285                         pstrcpy(cur_dir,saved_dir);
286                 }
287         }
288         
289         pstrcpy(cd_path,cur_dir);
290
291         return 0;
292 }
293
294 /****************************************************************************
295 change directory
296 ****************************************************************************/
297 static int cmd_cd(const char **cmd_ptr)
298 {
299         fstring buf;
300         int rc = 0;
301
302         if (next_token(cmd_ptr,buf,NULL,sizeof(buf)))
303                 rc = do_cd(buf);
304         else
305                 d_printf("Current directory is %s\n",cur_dir);
306
307         return rc;
308 }
309
310
311 BOOL mask_match(struct smbcli_state *c, const char *string, char *pattern, 
312                 BOOL is_case_sensitive)
313 {
314         fstring p2, s2;
315
316         if (strcmp(string,"..") == 0)
317                 string = ".";
318         if (strcmp(pattern,".") == 0)
319                 return False;
320         
321         if (is_case_sensitive)
322                 return ms_fnmatch(pattern, string, 
323                                   c->transport->negotiate.protocol) == 0;
324
325         fstrcpy(p2, pattern);
326         fstrcpy(s2, string);
327         strlower(p2); 
328         strlower(s2);
329         return ms_fnmatch(p2, s2, c->transport->negotiate.protocol) == 0;
330 }
331
332
333
334 /*******************************************************************
335   decide if a file should be operated on
336   ********************************************************************/
337 static BOOL do_this_one(struct clilist_file_info *finfo)
338 {
339         if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY) return(True);
340
341         if (*fileselection && 
342             !mask_match(cli, finfo->name,fileselection,False)) {
343                 DEBUG(3,("mask_match %s failed\n", finfo->name));
344                 return False;
345         }
346
347         if (newer_than && finfo->mtime < newer_than) {
348                 DEBUG(3,("newer_than %s failed\n", finfo->name));
349                 return(False);
350         }
351
352         if ((archive_level==1 || archive_level==2) && !(finfo->attrib & FILE_ATTRIBUTE_ARCHIVE)) {
353                 DEBUG(3,("archive %s failed\n", finfo->name));
354                 return(False);
355         }
356         
357         return(True);
358 }
359
360 /****************************************************************************
361   display info about a file
362   ****************************************************************************/
363 static void display_finfo(struct clilist_file_info *finfo)
364 {
365         if (do_this_one(finfo)) {
366                 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
367                 char *astr = attrib_string(NULL, finfo->attrib);
368                 d_printf("  %-30s%7.7s %8.0f  %s",
369                          finfo->name,
370                          astr,
371                          (double)finfo->size,
372                          asctime(localtime(&t)));
373                 dir_total += finfo->size;
374                 talloc_free(astr);
375         }
376 }
377
378
379 /****************************************************************************
380    accumulate size of a file
381   ****************************************************************************/
382 static void do_du(struct clilist_file_info *finfo)
383 {
384         if (do_this_one(finfo)) {
385                 dir_total += finfo->size;
386         }
387 }
388
389 static BOOL do_list_recurse;
390 static BOOL do_list_dirs;
391 static char *do_list_queue = 0;
392 static long do_list_queue_size = 0;
393 static long do_list_queue_start = 0;
394 static long do_list_queue_end = 0;
395 static void (*do_list_fn)(struct clilist_file_info *);
396
397 /****************************************************************************
398 functions for do_list_queue
399   ****************************************************************************/
400
401 /*
402  * The do_list_queue is a NUL-separated list of strings stored in a
403  * char*.  Since this is a FIFO, we keep track of the beginning and
404  * ending locations of the data in the queue.  When we overflow, we
405  * double the size of the char*.  When the start of the data passes
406  * the midpoint, we move everything back.  This is logically more
407  * complex than a linked list, but easier from a memory management
408  * angle.  In any memory error condition, do_list_queue is reset.
409  * Functions check to ensure that do_list_queue is non-NULL before
410  * accessing it.
411  */
412 static void reset_do_list_queue(void)
413 {
414         SAFE_FREE(do_list_queue);
415         do_list_queue_size = 0;
416         do_list_queue_start = 0;
417         do_list_queue_end = 0;
418 }
419
420 static void init_do_list_queue(void)
421 {
422         reset_do_list_queue();
423         do_list_queue_size = 1024;
424         do_list_queue = malloc(do_list_queue_size);
425         if (do_list_queue == 0) { 
426                 d_printf("malloc fail for size %d\n",
427                          (int)do_list_queue_size);
428                 reset_do_list_queue();
429         } else {
430                 memset(do_list_queue, 0, do_list_queue_size);
431         }
432 }
433
434 static void adjust_do_list_queue(void)
435 {
436         /*
437          * If the starting point of the queue is more than half way through,
438          * move everything toward the beginning.
439          */
440         if (do_list_queue && (do_list_queue_start == do_list_queue_end))
441         {
442                 DEBUG(4,("do_list_queue is empty\n"));
443                 do_list_queue_start = do_list_queue_end = 0;
444                 *do_list_queue = '\0';
445         }
446         else if (do_list_queue_start > (do_list_queue_size / 2))
447         {
448                 DEBUG(4,("sliding do_list_queue backward\n"));
449                 memmove(do_list_queue,
450                         do_list_queue + do_list_queue_start,
451                         do_list_queue_end - do_list_queue_start);
452                 do_list_queue_end -= do_list_queue_start;
453                 do_list_queue_start = 0;
454         }
455            
456 }
457
458 static void add_to_do_list_queue(const char* entry)
459 {
460         char *dlq;
461         long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
462         while (new_end > do_list_queue_size)
463         {
464                 do_list_queue_size *= 2;
465                 DEBUG(4,("enlarging do_list_queue to %d\n",
466                          (int)do_list_queue_size));
467                 dlq = realloc_p(do_list_queue, char, do_list_queue_size);
468                 if (! dlq) {
469                         d_printf("failure enlarging do_list_queue to %d bytes\n",
470                                  (int)do_list_queue_size);
471                         reset_do_list_queue();
472                 }
473                 else
474                 {
475                         do_list_queue = dlq;
476                         memset(do_list_queue + do_list_queue_size / 2,
477                                0, do_list_queue_size / 2);
478                 }
479         }
480         if (do_list_queue)
481         {
482                 safe_strcpy(do_list_queue + do_list_queue_end, entry, 
483                             do_list_queue_size - do_list_queue_end - 1);
484                 do_list_queue_end = new_end;
485                 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
486                          entry, (int)do_list_queue_start, (int)do_list_queue_end));
487         }
488 }
489
490 static char *do_list_queue_head(void)
491 {
492         return do_list_queue + do_list_queue_start;
493 }
494
495 static void remove_do_list_queue_head(void)
496 {
497         if (do_list_queue_end > do_list_queue_start)
498         {
499                 do_list_queue_start += strlen(do_list_queue_head()) + 1;
500                 adjust_do_list_queue();
501                 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
502                          (int)do_list_queue_start, (int)do_list_queue_end));
503         }
504 }
505
506 static int do_list_queue_empty(void)
507 {
508         return (! (do_list_queue && *do_list_queue));
509 }
510
511 /****************************************************************************
512 a helper for do_list
513   ****************************************************************************/
514 static void do_list_helper(struct clilist_file_info *f, const char *mask, void *state)
515 {
516         if (f->attrib & FILE_ATTRIBUTE_DIRECTORY) {
517                 if (do_list_dirs && do_this_one(f)) {
518                         do_list_fn(f);
519                 }
520                 if (do_list_recurse && 
521                     !strequal(f->name,".") && 
522                     !strequal(f->name,"..")) {
523                         pstring mask2;
524                         char *p;
525
526                         pstrcpy(mask2, mask);
527                         p = strrchr_m(mask2,'\\');
528                         if (!p) return;
529                         p[1] = 0;
530                         pstrcat(mask2, f->name);
531                         pstrcat(mask2,"\\*");
532                         add_to_do_list_queue(mask2);
533                 }
534                 return;
535         }
536
537         if (do_this_one(f)) {
538                 do_list_fn(f);
539         }
540 }
541
542
543 /****************************************************************************
544 a wrapper around smbcli_list that adds recursion
545   ****************************************************************************/
546 void do_list(const char *mask,uint16_t attribute,
547              void (*fn)(struct clilist_file_info *),BOOL rec, BOOL dirs)
548 {
549         static int in_do_list = 0;
550
551         if (in_do_list && rec)
552         {
553                 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
554                 exit(1);
555         }
556
557         in_do_list = 1;
558
559         do_list_recurse = rec;
560         do_list_dirs = dirs;
561         do_list_fn = fn;
562
563         if (rec)
564         {
565                 init_do_list_queue();
566                 add_to_do_list_queue(mask);
567                 
568                 while (! do_list_queue_empty())
569                 {
570                         /*
571                          * Need to copy head so that it doesn't become
572                          * invalid inside the call to smbcli_list.  This
573                          * would happen if the list were expanded
574                          * during the call.
575                          * Fix from E. Jay Berkenbilt (ejb@ql.org)
576                          */
577                         pstring head;
578                         pstrcpy(head, do_list_queue_head());
579                         smbcli_list(cli->tree, head, attribute, do_list_helper, NULL);
580                         remove_do_list_queue_head();
581                         if ((! do_list_queue_empty()) && (fn == display_finfo))
582                         {
583                                 char* next_file = do_list_queue_head();
584                                 char* save_ch = 0;
585                                 if ((strlen(next_file) >= 2) &&
586                                     (next_file[strlen(next_file) - 1] == '*') &&
587                                     (next_file[strlen(next_file) - 2] == '\\'))
588                                 {
589                                         save_ch = next_file +
590                                                 strlen(next_file) - 2;
591                                         *save_ch = '\0';
592                                 }
593                                 d_printf("\n%s\n",next_file);
594                                 if (save_ch)
595                                 {
596                                         *save_ch = '\\';
597                                 }
598                         }
599                 }
600         }
601         else
602         {
603                 if (smbcli_list(cli->tree, mask, attribute, do_list_helper, NULL) == -1)
604                 {
605                         d_printf("%s listing %s\n", smbcli_errstr(cli->tree), mask);
606                 }
607         }
608
609         in_do_list = 0;
610         reset_do_list_queue();
611 }
612
613 /****************************************************************************
614   get a directory listing
615   ****************************************************************************/
616 static int cmd_dir(const char **cmd_ptr)
617 {
618         uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
619         pstring mask;
620         fstring buf;
621         char *p=buf;
622         int rc;
623         
624         dir_total = 0;
625         pstrcpy(mask,cur_dir);
626         if(mask[strlen(mask)-1]!='\\')
627                 pstrcat(mask,"\\");
628         
629         if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
630                 dos_format(p);
631                 if (*p == '\\')
632                         pstrcpy(mask,p);
633                 else
634                         pstrcat(mask,p);
635         }
636         else {
637                 if (cli->tree->session->transport->negotiate.protocol <= 
638                     PROTOCOL_LANMAN1) { 
639                         pstrcat(mask,"*.*");
640                 } else {
641                         pstrcat(mask,"*");
642                 }
643         }
644
645         do_list(mask, attribute, display_finfo, recurse, True);
646
647         rc = do_dskattr();
648
649         DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
650
651         return rc;
652 }
653
654
655 /****************************************************************************
656   get a directory listing
657   ****************************************************************************/
658 static int cmd_du(const char **cmd_ptr)
659 {
660         uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
661         pstring mask;
662         fstring buf;
663         char *p=buf;
664         int rc;
665         
666         dir_total = 0;
667         pstrcpy(mask,cur_dir);
668         if(mask[strlen(mask)-1]!='\\')
669                 pstrcat(mask,"\\");
670         
671         if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
672                 dos_format(p);
673                 if (*p == '\\')
674                         pstrcpy(mask,p);
675                 else
676                         pstrcat(mask,p);
677         } else {
678                 pstrcat(mask,"\\*");
679         }
680
681         do_list(mask, attribute, do_du, recurse, True);
682
683         rc = do_dskattr();
684
685         d_printf("Total number of bytes: %.0f\n", dir_total);
686
687         return rc;
688 }
689
690
691 /****************************************************************************
692   get a file from rname to lname
693   ****************************************************************************/
694 static int do_get(char *rname, const char *lname, BOOL reget)
695 {  
696         int handle = 0, fnum;
697         BOOL newhandle = False;
698         uint8_t *data;
699         struct timeval tp_start;
700         int read_size = io_bufsize;
701         uint16_t attr;
702         size_t size;
703         off_t start = 0;
704         off_t nread = 0;
705         int rc = 0;
706
707         GetTimeOfDay(&tp_start);
708
709         if (lowercase) {
710                 strlower(discard_const_p(char, lname));
711         }
712
713         fnum = smbcli_open(cli->tree, rname, O_RDONLY, DENY_NONE);
714
715         if (fnum == -1) {
716                 d_printf("%s opening remote file %s\n",smbcli_errstr(cli->tree),rname);
717                 return 1;
718         }
719
720         if(!strcmp(lname,"-")) {
721                 handle = fileno(stdout);
722         } else {
723                 if (reget) {
724                         handle = open(lname, O_WRONLY|O_CREAT, 0644);
725                         if (handle >= 0) {
726                                 start = lseek(handle, 0, SEEK_END);
727                                 if (start == -1) {
728                                         d_printf("Error seeking local file\n");
729                                         return 1;
730                                 }
731                         }
732                 } else {
733                         handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
734                 }
735                 newhandle = True;
736         }
737         if (handle < 0) {
738                 d_printf("Error opening local file %s\n",lname);
739                 return 1;
740         }
741
742
743         if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, 
744                            &attr, &size, NULL, NULL, NULL, NULL, NULL)) &&
745             NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum, 
746                           &attr, &size, NULL, NULL, NULL))) {
747                 d_printf("getattrib: %s\n",smbcli_errstr(cli->tree));
748                 return 1;
749         }
750
751         DEBUG(2,("getting file %s of size %.0f as %s ", 
752                  rname, (double)size, lname));
753
754         if(!(data = (uint8_t *)malloc(read_size))) { 
755                 d_printf("malloc fail for size %d\n", read_size);
756                 smbcli_close(cli->tree, fnum);
757                 return 1;
758         }
759
760         while (1) {
761                 int n = smbcli_read(cli->tree, fnum, data, nread + start, read_size);
762
763                 if (n <= 0) break;
764  
765                 if (writefile(handle,data, n) != n) {
766                         d_printf("Error writing local file\n");
767                         rc = 1;
768                         break;
769                 }
770       
771                 nread += n;
772         }
773
774         if (nread + start < size) {
775                 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
776                             rname, (long)nread));
777
778                 rc = 1;
779         }
780
781         SAFE_FREE(data);
782         
783         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
784                 d_printf("Error %s closing remote file\n",smbcli_errstr(cli->tree));
785                 rc = 1;
786         }
787
788         if (newhandle) {
789                 close(handle);
790         }
791
792         if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
793                 smbcli_setatr(cli->tree, rname, attr & ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE, 0);
794         }
795
796         {
797                 struct timeval tp_end;
798                 int this_time;
799                 
800                 GetTimeOfDay(&tp_end);
801                 this_time = 
802                         (tp_end.tv_sec - tp_start.tv_sec)*1000 +
803                         (tp_end.tv_usec - tp_start.tv_usec)/1000;
804                 get_total_time_ms += this_time;
805                 get_total_size += nread;
806                 
807                 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
808                          nread / (1.024*this_time + 1.0e-4),
809                          get_total_size / (1.024*get_total_time_ms)));
810         }
811         
812         return rc;
813 }
814
815
816 /****************************************************************************
817   get a file
818   ****************************************************************************/
819 static int cmd_get(const char **cmd_ptr)
820 {
821         pstring lname;
822         pstring rname;
823         char *p;
824
825         pstrcpy(rname,cur_dir);
826         pstrcat(rname,"\\");
827         
828         p = rname + strlen(rname);
829         
830         if (!next_token(cmd_ptr,p,NULL,sizeof(rname)-strlen(rname))) {
831                 d_printf("get <filename>\n");
832                 return 1;
833         }
834         pstrcpy(lname,p);
835         dos_clean_name(rname);
836         
837         next_token(cmd_ptr,lname,NULL,sizeof(lname));
838         
839         return do_get(rname, lname, False);
840 }
841
842 /****************************************************************************
843  Put up a yes/no prompt.
844 ****************************************************************************/
845 static BOOL yesno(char *p)
846 {
847         pstring ans;
848         printf("%s",p);
849
850         if (!fgets(ans,sizeof(ans)-1,stdin))
851                 return(False);
852
853         if (*ans == 'y' || *ans == 'Y')
854                 return(True);
855
856         return(False);
857 }
858
859 /****************************************************************************
860   do a mget operation on one file
861   ****************************************************************************/
862 static void do_mget(struct clilist_file_info *finfo)
863 {
864         pstring rname;
865         pstring quest;
866         pstring saved_curdir;
867         pstring mget_mask;
868
869         if (strequal(finfo->name,".") || strequal(finfo->name,".."))
870                 return;
871
872         if (abort_mget) {
873                 d_printf("mget aborted\n");
874                 return;
875         }
876
877         if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY)
878                 slprintf(quest,sizeof(pstring)-1,
879                          "Get directory %s? ",finfo->name);
880         else
881                 slprintf(quest,sizeof(pstring)-1,
882                          "Get file %s? ",finfo->name);
883
884         if (prompt && !yesno(quest)) return;
885
886         if (!(finfo->attrib & FILE_ATTRIBUTE_DIRECTORY)) {
887                 pstrcpy(rname,cur_dir);
888                 pstrcat(rname,finfo->name);
889                 do_get(rname, finfo->name, False);
890                 return;
891         }
892
893         /* handle directories */
894         pstrcpy(saved_curdir,cur_dir);
895
896         pstrcat(cur_dir,finfo->name);
897         pstrcat(cur_dir,"\\");
898
899         string_replace(discard_const_p(char, finfo->name), '\\', '/');
900         if (lowercase) {
901                 strlower(discard_const_p(char, finfo->name));
902         }
903         
904         if (!directory_exist(finfo->name) && 
905             mkdir(finfo->name,0777) != 0) {
906                 d_printf("failed to create directory %s\n",finfo->name);
907                 pstrcpy(cur_dir,saved_curdir);
908                 return;
909         }
910         
911         if (chdir(finfo->name) != 0) {
912                 d_printf("failed to chdir to directory %s\n",finfo->name);
913                 pstrcpy(cur_dir,saved_curdir);
914                 return;
915         }
916
917         pstrcpy(mget_mask,cur_dir);
918         pstrcat(mget_mask,"*");
919         
920         do_list(mget_mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,do_mget,False, True);
921         chdir("..");
922         pstrcpy(cur_dir,saved_curdir);
923 }
924
925
926 /****************************************************************************
927 view the file using the pager
928 ****************************************************************************/
929 static int cmd_more(const char **cmd_ptr)
930 {
931         fstring rname,lname,pager_cmd;
932         char *pager;
933         int fd;
934         int rc = 0;
935
936         fstrcpy(rname,cur_dir);
937         fstrcat(rname,"\\");
938         
939         slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
940         fd = mkstemp(lname);
941         if (fd == -1) {
942                 d_printf("failed to create temporary file for more\n");
943                 return 1;
944         }
945         close(fd);
946
947         if (!next_token(cmd_ptr,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
948                 d_printf("more <filename>\n");
949                 unlink(lname);
950                 return 1;
951         }
952         dos_clean_name(rname);
953
954         rc = do_get(rname, lname, False);
955
956         pager=getenv("PAGER");
957
958         slprintf(pager_cmd,sizeof(pager_cmd)-1,
959                  "%s %s",(pager? pager:PAGER), lname);
960         system(pager_cmd);
961         unlink(lname);
962         
963         return rc;
964 }
965
966
967
968 /****************************************************************************
969 do a mget command
970 ****************************************************************************/
971 static int cmd_mget(const char **cmd_ptr)
972 {
973         uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
974         pstring mget_mask;
975         fstring buf;
976         char *p=buf;
977
978         *mget_mask = 0;
979
980         if (recurse)
981                 attribute |= FILE_ATTRIBUTE_DIRECTORY;
982         
983         abort_mget = False;
984
985         while (next_token(cmd_ptr,p,NULL,sizeof(buf))) {
986                 pstrcpy(mget_mask,cur_dir);
987                 if(mget_mask[strlen(mget_mask)-1]!='\\')
988                         pstrcat(mget_mask,"\\");
989                 
990                 if (*p == '\\')
991                         pstrcpy(mget_mask,p);
992                 else
993                         pstrcat(mget_mask,p);
994                 do_list(mget_mask, attribute,do_mget,False,True);
995         }
996
997         if (!*mget_mask) {
998                 pstrcpy(mget_mask,cur_dir);
999                 if(mget_mask[strlen(mget_mask)-1]!='\\')
1000                         pstrcat(mget_mask,"\\");
1001                 pstrcat(mget_mask,"*");
1002                 do_list(mget_mask, attribute,do_mget,False,True);
1003         }
1004         
1005         return 0;
1006 }
1007
1008
1009 /****************************************************************************
1010 make a directory of name "name"
1011 ****************************************************************************/
1012 static NTSTATUS do_mkdir(char *name)
1013 {
1014         NTSTATUS status;
1015
1016         if (NT_STATUS_IS_ERR(status = smbcli_mkdir(cli->tree, name))) {
1017                 d_printf("%s making remote directory %s\n",
1018                          smbcli_errstr(cli->tree),name);
1019                 return status;
1020         }
1021
1022         return status;
1023 }
1024
1025 /****************************************************************************
1026 show 8.3 name of a file
1027 ****************************************************************************/
1028 static BOOL do_altname(char *name)
1029 {
1030         const char *altname;
1031         if (!NT_STATUS_IS_OK(smbcli_qpathinfo_alt_name(cli->tree, name, &altname))) {
1032                 d_printf("%s getting alt name for %s\n",
1033                          smbcli_errstr(cli->tree),name);
1034                 return(False);
1035         }
1036         d_printf("%s\n", altname);
1037
1038         return(True);
1039 }
1040
1041
1042 /****************************************************************************
1043  Exit client.
1044 ****************************************************************************/
1045 static int cmd_quit(const char **cmd_ptr)
1046 {
1047         talloc_free(cli);
1048         exit(0);
1049         /* NOTREACHED */
1050         return 0;
1051 }
1052
1053
1054 /****************************************************************************
1055   make a directory
1056   ****************************************************************************/
1057 static int cmd_mkdir(const char **cmd_ptr)
1058 {
1059         pstring mask;
1060         fstring buf;
1061         char *p=buf;
1062   
1063         pstrcpy(mask,cur_dir);
1064
1065         if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1066                 if (!recurse)
1067                         d_printf("mkdir <dirname>\n");
1068                 return 1;
1069         }
1070         pstrcat(mask,p);
1071
1072         if (recurse) {
1073                 pstring ddir;
1074                 pstring ddir2;
1075                 *ddir2 = 0;
1076                 
1077                 pstrcpy(ddir,mask);
1078                 trim_string(ddir,".",NULL);
1079                 p = strtok(ddir,"/\\");
1080                 while (p) {
1081                         pstrcat(ddir2,p);
1082                         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, ddir2))) { 
1083                                 do_mkdir(ddir2);
1084                         }
1085                         pstrcat(ddir2,"\\");
1086                         p = strtok(NULL,"/\\");
1087                 }        
1088         } else {
1089                 do_mkdir(mask);
1090         }
1091         
1092         return 0;
1093 }
1094
1095
1096 /****************************************************************************
1097   show alt name
1098   ****************************************************************************/
1099 static int cmd_altname(const char **cmd_ptr)
1100 {
1101         pstring name;
1102         fstring buf;
1103         char *p=buf;
1104   
1105         pstrcpy(name,cur_dir);
1106
1107         if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1108                 d_printf("altname <file>\n");
1109                 return 1;
1110         }
1111         pstrcat(name,p);
1112
1113         do_altname(name);
1114
1115         return 0;
1116 }
1117
1118
1119 /****************************************************************************
1120   put a single file
1121   ****************************************************************************/
1122 static int do_put(char *rname, char *lname, BOOL reput)
1123 {
1124         int fnum;
1125         XFILE *f;
1126         size_t start = 0;
1127         off_t nread = 0;
1128         uint8_t *buf = NULL;
1129         int maxwrite = io_bufsize;
1130         int rc = 0;
1131         
1132         struct timeval tp_start;
1133         GetTimeOfDay(&tp_start);
1134
1135         if (reput) {
1136                 fnum = smbcli_open(cli->tree, rname, O_RDWR|O_CREAT, DENY_NONE);
1137                 if (fnum >= 0) {
1138                         if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL)) &&
1139                             NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum, NULL, &start, NULL, NULL, NULL))) {
1140                                 d_printf("getattrib: %s\n",smbcli_errstr(cli->tree));
1141                                 return 1;
1142                         }
1143                 }
1144         } else {
1145                 fnum = smbcli_open(cli->tree, rname, O_RDWR|O_CREAT|O_TRUNC, 
1146                                 DENY_NONE);
1147         }
1148   
1149         if (fnum == -1) {
1150                 d_printf("%s opening remote file %s\n",smbcli_errstr(cli->tree),rname);
1151                 return 1;
1152         }
1153
1154         /* allow files to be piped into smbclient
1155            jdblair 24.jun.98
1156
1157            Note that in this case this function will exit(0) rather
1158            than returning. */
1159         if (!strcmp(lname, "-")) {
1160                 f = x_stdin;
1161                 /* size of file is not known */
1162         } else {
1163                 f = x_fopen(lname,O_RDONLY, 0);
1164                 if (f && reput) {
1165                         if (x_tseek(f, start, SEEK_SET) == -1) {
1166                                 d_printf("Error seeking local file\n");
1167                                 return 1;
1168                         }
1169                 }
1170         }
1171
1172         if (!f) {
1173                 d_printf("Error opening local file %s\n",lname);
1174                 return 1;
1175         }
1176
1177   
1178         DEBUG(1,("putting file %s as %s ",lname,
1179                  rname));
1180   
1181         buf = (uint8_t *)malloc(maxwrite);
1182         if (!buf) {
1183                 d_printf("ERROR: Not enough memory!\n");
1184                 return 1;
1185         }
1186         while (!x_feof(f)) {
1187                 int n = maxwrite;
1188                 int ret;
1189
1190                 if ((n = readfile(buf,n,f)) < 1) {
1191                         if((n == 0) && x_feof(f))
1192                                 break; /* Empty local file. */
1193
1194                         d_printf("Error reading local file: %s\n", strerror(errno));
1195                         rc = 1;
1196                         break;
1197                 }
1198
1199                 ret = smbcli_write(cli->tree, fnum, 0, buf, nread + start, n);
1200
1201                 if (n != ret) {
1202                         d_printf("Error writing file: %s\n", smbcli_errstr(cli->tree));
1203                         rc = 1;
1204                         break;
1205                 } 
1206
1207                 nread += n;
1208         }
1209
1210         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
1211                 d_printf("%s closing remote file %s\n",smbcli_errstr(cli->tree),rname);
1212                 x_fclose(f);
1213                 SAFE_FREE(buf);
1214                 return 1;
1215         }
1216
1217         
1218         if (f != x_stdin) {
1219                 x_fclose(f);
1220         }
1221
1222         SAFE_FREE(buf);
1223
1224         {
1225                 struct timeval tp_end;
1226                 int this_time;
1227                 
1228                 GetTimeOfDay(&tp_end);
1229                 this_time = 
1230                         (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1231                         (tp_end.tv_usec - tp_start.tv_usec)/1000;
1232                 put_total_time_ms += this_time;
1233                 put_total_size += nread;
1234                 
1235                 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1236                          nread / (1.024*this_time + 1.0e-4),
1237                          put_total_size / (1.024*put_total_time_ms)));
1238         }
1239
1240         if (f == x_stdin) {
1241                 talloc_free(cli);
1242                 exit(0);
1243         }
1244         
1245         return rc;
1246 }
1247
1248  
1249
1250 /****************************************************************************
1251   put a file
1252   ****************************************************************************/
1253 static int cmd_put(const char **cmd_ptr)
1254 {
1255         pstring lname;
1256         pstring rname;
1257         fstring buf;
1258         char *p=buf;
1259         
1260         pstrcpy(rname,cur_dir);
1261         pstrcat(rname,"\\");
1262   
1263         if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1264                 d_printf("put <filename>\n");
1265                 return 1;
1266         }
1267         pstrcpy(lname,p);
1268   
1269         if (next_token(cmd_ptr,p,NULL,sizeof(buf)))
1270                 pstrcat(rname,p);      
1271         else
1272                 pstrcat(rname,lname);
1273         
1274         dos_clean_name(rname);
1275
1276         /* allow '-' to represent stdin
1277            jdblair, 24.jun.98 */
1278         if (!file_exist(lname) && (strcmp(lname,"-"))) {
1279                 d_printf("%s does not exist\n",lname);
1280                 return 1;
1281         }
1282
1283         return do_put(rname, lname, False);
1284 }
1285
1286 /*************************************
1287   File list structure
1288 *************************************/
1289
1290 static struct file_list {
1291         struct file_list *prev, *next;
1292         char *file_path;
1293         BOOL isdir;
1294 } *file_list;
1295
1296 /****************************************************************************
1297   Free a file_list structure
1298 ****************************************************************************/
1299
1300 static void free_file_list (struct file_list * list)
1301 {
1302         struct file_list *tmp;
1303         
1304         while (list)
1305         {
1306                 tmp = list;
1307                 DLIST_REMOVE(list, list);
1308                 SAFE_FREE(tmp->file_path);
1309                 SAFE_FREE(tmp);
1310         }
1311 }
1312
1313 /****************************************************************************
1314   seek in a directory/file list until you get something that doesn't start with
1315   the specified name
1316   ****************************************************************************/
1317 static BOOL seek_list(struct file_list *list, char *name)
1318 {
1319         while (list) {
1320                 trim_string(list->file_path,"./","\n");
1321                 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1322                         return(True);
1323                 }
1324                 list = list->next;
1325         }
1326       
1327         return(False);
1328 }
1329
1330 /****************************************************************************
1331   set the file selection mask
1332   ****************************************************************************/
1333 static int cmd_select(const char **cmd_ptr)
1334 {
1335         pstrcpy(fileselection,"");
1336         next_token(cmd_ptr,fileselection,NULL,sizeof(fileselection));
1337
1338         return 0;
1339 }
1340
1341 /*******************************************************************
1342   A readdir wrapper which just returns the file name.
1343  ********************************************************************/
1344 static const char *readdirname(DIR *p)
1345 {
1346         struct dirent *ptr;
1347         char *dname;
1348
1349         if (!p)
1350                 return(NULL);
1351   
1352         ptr = (struct dirent *)readdir(p);
1353         if (!ptr)
1354                 return(NULL);
1355
1356         dname = ptr->d_name;
1357
1358 #ifdef NEXT2
1359         if (telldir(p) < 0)
1360                 return(NULL);
1361 #endif
1362
1363 #ifdef HAVE_BROKEN_READDIR
1364         /* using /usr/ucb/cc is BAD */
1365         dname = dname - 2;
1366 #endif
1367
1368         {
1369                 static pstring buf;
1370                 int len = NAMLEN(ptr);
1371                 memcpy(buf, dname, len);
1372                 buf[len] = 0;
1373                 dname = buf;
1374         }
1375
1376         return(dname);
1377 }
1378
1379 /****************************************************************************
1380   Recursive file matching function act as find
1381   match must be always set to True when calling this function
1382 ****************************************************************************/
1383 static int file_find(struct file_list **list, const char *directory, 
1384                       const char *expression, BOOL match)
1385 {
1386         DIR *dir;
1387         struct file_list *entry;
1388         struct stat statbuf;
1389         int ret;
1390         char *path;
1391         BOOL isdir;
1392         const char *dname;
1393
1394         dir = opendir(directory);
1395         if (!dir) return -1;
1396         
1397         while ((dname = readdirname(dir))) {
1398                 if (!strcmp("..", dname)) continue;
1399                 if (!strcmp(".", dname)) continue;
1400                 
1401                 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1402                         continue;
1403                 }
1404
1405                 isdir = False;
1406                 if (!match || !gen_fnmatch(expression, dname)) {
1407                         if (recurse) {
1408                                 ret = stat(path, &statbuf);
1409                                 if (ret == 0) {
1410                                         if (S_ISDIR(statbuf.st_mode)) {
1411                                                 isdir = True;
1412                                                 ret = file_find(list, path, expression, False);
1413                                         }
1414                                 } else {
1415                                         d_printf("file_find: cannot stat file %s\n", path);
1416                                 }
1417                                 
1418                                 if (ret == -1) {
1419                                         SAFE_FREE(path);
1420                                         closedir(dir);
1421                                         return -1;
1422                                 }
1423                         }
1424                         entry = malloc_p(struct file_list);
1425                         if (!entry) {
1426                                 d_printf("Out of memory in file_find\n");
1427                                 closedir(dir);
1428                                 return -1;
1429                         }
1430                         entry->file_path = path;
1431                         entry->isdir = isdir;
1432                         DLIST_ADD(*list, entry);
1433                 } else {
1434                         SAFE_FREE(path);
1435                 }
1436         }
1437
1438         closedir(dir);
1439         return 0;
1440 }
1441
1442 /****************************************************************************
1443   mput some files
1444   ****************************************************************************/
1445 static int cmd_mput(const char **cmd_ptr)
1446 {
1447         fstring buf;
1448         char *p=buf;
1449         
1450         while (next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1451                 int ret;
1452                 struct file_list *temp_list;
1453                 char *quest, *lname, *rname;
1454         
1455                 file_list = NULL;
1456
1457                 ret = file_find(&file_list, ".", p, True);
1458                 if (ret) {
1459                         free_file_list(file_list);
1460                         continue;
1461                 }
1462                 
1463                 quest = NULL;
1464                 lname = NULL;
1465                 rname = NULL;
1466                                 
1467                 for (temp_list = file_list; temp_list; 
1468                      temp_list = temp_list->next) {
1469
1470                         SAFE_FREE(lname);
1471                         if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1472                                 continue;
1473                         trim_string(lname, "./", "/");
1474                         
1475                         /* check if it's a directory */
1476                         if (temp_list->isdir) {
1477                                 /* if (!recurse) continue; */
1478                                 
1479                                 SAFE_FREE(quest);
1480                                 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1481                                 if (prompt && !yesno(quest)) { /* No */
1482                                         /* Skip the directory */
1483                                         lname[strlen(lname)-1] = '/';
1484                                         if (!seek_list(temp_list, lname))
1485                                                 break;              
1486                                 } else { /* Yes */
1487                                         SAFE_FREE(rname);
1488                                         if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1489                                         dos_format(rname);
1490                                         if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, rname)) && 
1491                                             NT_STATUS_IS_ERR(do_mkdir(rname))) {
1492                                                 DEBUG (0, ("Unable to make dir, skipping..."));
1493                                                 /* Skip the directory */
1494                                                 lname[strlen(lname)-1] = '/';
1495                                                 if (!seek_list(temp_list, lname))
1496                                                         break;
1497                                         }
1498                                 }
1499                                 continue;
1500                         } else {
1501                                 SAFE_FREE(quest);
1502                                 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1503                                 if (prompt && !yesno(quest)) /* No */
1504                                         continue;
1505                                 
1506                                 /* Yes */
1507                                 SAFE_FREE(rname);
1508                                 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1509                         }
1510
1511                         dos_format(rname);
1512
1513                         do_put(rname, lname, False);
1514                 }
1515                 free_file_list(file_list);
1516                 SAFE_FREE(quest);
1517                 SAFE_FREE(lname);
1518                 SAFE_FREE(rname);
1519         }
1520
1521         return 0;
1522 }
1523
1524
1525 /****************************************************************************
1526   cancel a print job
1527   ****************************************************************************/
1528 static int do_cancel(int job)
1529 {
1530         d_printf("REWRITE: print job cancel not implemented\n");
1531         return 1;
1532 }
1533
1534
1535 /****************************************************************************
1536   cancel a print job
1537   ****************************************************************************/
1538 static int cmd_cancel(const char **cmd_ptr)
1539 {
1540         fstring buf;
1541         int job; 
1542
1543         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1544                 d_printf("cancel <jobid> ...\n");
1545                 return 1;
1546         }
1547         do {
1548                 job = atoi(buf);
1549                 do_cancel(job);
1550         } while (next_token(cmd_ptr,buf,NULL,sizeof(buf)));
1551         
1552         return 0;
1553 }
1554
1555
1556 /****************************************************************************
1557   print a file
1558   ****************************************************************************/
1559 static int cmd_print(const char **cmd_ptr)
1560 {
1561         pstring lname;
1562         pstring rname;
1563         char *p;
1564
1565         if (!next_token(cmd_ptr,lname,NULL, sizeof(lname))) {
1566                 d_printf("print <filename>\n");
1567                 return 1;
1568         }
1569
1570         pstrcpy(rname,lname);
1571         p = strrchr_m(rname,'/');
1572         if (p) {
1573                 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1574         }
1575
1576         if (strequal(lname,"-")) {
1577                 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1578         }
1579
1580         return do_put(rname, lname, False);
1581 }
1582
1583
1584 /****************************************************************************
1585  show a print queue
1586 ****************************************************************************/
1587 static int cmd_queue(const char **cmd_ptr)
1588 {
1589         d_printf("REWRITE: print job queue not implemented\n");
1590         
1591         return 0;
1592 }
1593
1594 /****************************************************************************
1595 delete some files
1596 ****************************************************************************/
1597 static int cmd_del(const char **cmd_ptr)
1598 {
1599         pstring mask;
1600         fstring buf;
1601         uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1602
1603         if (recurse)
1604                 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1605         
1606         pstrcpy(mask,cur_dir);
1607         
1608         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1609                 d_printf("del <filename>\n");
1610                 return 1;
1611         }
1612         pstrcat(mask,buf);
1613
1614         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, mask))) {
1615                 d_printf("%s deleting remote file %s\n",smbcli_errstr(cli->tree),mask);
1616         }
1617         
1618         return 0;
1619 }
1620
1621
1622 /****************************************************************************
1623 delete a whole directory tree
1624 ****************************************************************************/
1625 static int cmd_deltree(const char **cmd_ptr)
1626 {
1627         pstring dname;
1628         fstring buf;
1629         int ret;
1630
1631         pstrcpy(dname,cur_dir);
1632         
1633         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1634                 d_printf("deltree <dirname>\n");
1635                 return 1;
1636         }
1637         pstrcat(dname,buf);
1638         
1639         ret = smbcli_deltree(cli->tree, dname);
1640
1641         if (ret == -1) {
1642                 printf("Failed to delete tree %s - %s\n", dname, smbcli_errstr(cli->tree));
1643                 return -1;
1644         }
1645
1646         printf("Deleted %d files in %s\n", ret, dname);
1647         
1648         return 0;
1649 }
1650
1651 typedef struct {
1652         const char  *level_name;
1653         enum smb_fsinfo_level level;
1654 } fsinfo_level_t;
1655
1656 fsinfo_level_t fsinfo_levels[] = {
1657         {"dskattr", RAW_QFS_DSKATTR},
1658         {"allocation", RAW_QFS_ALLOCATION},
1659         {"volume", RAW_QFS_VOLUME},
1660         {"volumeinfo", RAW_QFS_VOLUME_INFO},
1661         {"sizeinfo", RAW_QFS_SIZE_INFO},
1662         {"deviceinfo", RAW_QFS_DEVICE_INFO},
1663         {"attributeinfo", RAW_QFS_ATTRIBUTE_INFO},
1664         {"unixinfo", RAW_QFS_UNIX_INFO},
1665         {"volume-information", RAW_QFS_VOLUME_INFORMATION},
1666         {"size-information", RAW_QFS_SIZE_INFORMATION},
1667         {"device-information", RAW_QFS_DEVICE_INFORMATION},
1668         {"attribute-information", RAW_QFS_ATTRIBUTE_INFORMATION},
1669         {"quota-information", RAW_QFS_QUOTA_INFORMATION},
1670         {"fullsize-information", RAW_QFS_FULL_SIZE_INFORMATION},
1671         {"objectid", RAW_QFS_OBJECTID_INFORMATION},
1672         {NULL, RAW_QFS_GENERIC}
1673 };
1674
1675
1676 static int cmd_fsinfo(const char **cmd_ptr)
1677 {
1678         fstring buf;
1679         int ret = 0;
1680         union smb_fsinfo fsinfo;
1681         NTSTATUS status;
1682         TALLOC_CTX *mem_ctx;
1683         fsinfo_level_t *fsinfo_level;
1684         
1685         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1686                 d_printf("fsinfo <level>, where level is one of following:\n");
1687                 fsinfo_level = fsinfo_levels;
1688                 while(fsinfo_level->level_name) {
1689                         d_printf("%s\n", fsinfo_level->level_name);
1690                         fsinfo_level++;
1691                 }
1692                 return 1;
1693         }
1694         
1695         fsinfo_level = fsinfo_levels;
1696         while(fsinfo_level->level_name && !strequal(buf,fsinfo_level->level_name)) {
1697                 fsinfo_level++;
1698         }
1699   
1700         if (!fsinfo_level->level_name) {
1701                 d_printf("wrong level name!\n");
1702                 return 1;
1703         }
1704   
1705         mem_ctx = talloc_init("fsinfo-level-%s", fsinfo_level->level_name);
1706         fsinfo.generic.level = fsinfo_level->level;
1707         status = smb_raw_fsinfo(cli->tree, mem_ctx, &fsinfo);
1708         if (!NT_STATUS_IS_OK(status)) {
1709                 d_printf("fsinfo-level-%s - %s\n", fsinfo_level->level_name, nt_errstr(status));
1710                 ret = 1;
1711                 goto done;
1712         }
1713
1714         d_printf("fsinfo-level-%s:\n", fsinfo_level->level_name);
1715         switch(fsinfo.generic.level) {
1716         case RAW_QFS_DSKATTR:
1717                 d_printf("\tunits_total:                %hu\n", 
1718                          (unsigned short) fsinfo.dskattr.out.units_total);
1719                 d_printf("\tblocks_per_unit:            %hu\n", 
1720                          (unsigned short) fsinfo.dskattr.out.blocks_per_unit);
1721                 d_printf("\tblocks_size:                %hu\n", 
1722                          (unsigned short) fsinfo.dskattr.out.block_size);
1723                 d_printf("\tunits_free:                 %hu\n", 
1724                          (unsigned short) fsinfo.dskattr.out.units_free);
1725                 break;
1726         case RAW_QFS_ALLOCATION:
1727                 d_printf("\tfs_id:                      %lu\n", 
1728                          (unsigned long) fsinfo.allocation.out.fs_id);
1729                 d_printf("\tsectors_per_unit:           %lu\n", 
1730                          (unsigned long) fsinfo.allocation.out.sectors_per_unit);
1731                 d_printf("\ttotal_alloc_units:          %lu\n", 
1732                          (unsigned long) fsinfo.allocation.out.total_alloc_units);
1733                 d_printf("\tavail_alloc_units:          %lu\n", 
1734                          (unsigned long) fsinfo.allocation.out.avail_alloc_units);
1735                 d_printf("\tbytes_per_sector:           %hu\n", 
1736                          (unsigned short) fsinfo.allocation.out.bytes_per_sector);
1737                 break;
1738         case RAW_QFS_VOLUME:
1739                 d_printf("\tserial_number:              %lu\n", 
1740                          (unsigned long) fsinfo.volume.out.serial_number);
1741                 d_printf("\tvolume_name:                %s\n", fsinfo.volume.out.volume_name.s);
1742                 break;
1743         case RAW_QFS_VOLUME_INFO:
1744         case RAW_QFS_VOLUME_INFORMATION:
1745                 d_printf("\tcreate_time:                %s\n",
1746                          nt_time_string(mem_ctx,fsinfo.volume_info.out.create_time));
1747                 d_printf("\tserial_number:              %lu\n", 
1748                          (unsigned long) fsinfo.volume_info.out.serial_number);
1749                 d_printf("\tvolume_name:                %s\n", fsinfo.volume_info.out.volume_name.s);
1750                 break;
1751         case RAW_QFS_SIZE_INFO:
1752         case RAW_QFS_SIZE_INFORMATION:
1753                 d_printf("\ttotal_alloc_units:          %llu\n", 
1754                          (unsigned long long) fsinfo.size_info.out.total_alloc_units);
1755                 d_printf("\tavail_alloc_units:          %llu\n", 
1756                          (unsigned long long) fsinfo.size_info.out.avail_alloc_units);
1757                 d_printf("\tsectors_per_unit:           %lu\n", 
1758                          (unsigned long) fsinfo.size_info.out.sectors_per_unit);
1759                 d_printf("\tbytes_per_sector:           %lu\n", 
1760                          (unsigned long) fsinfo.size_info.out.bytes_per_sector);
1761                 break;
1762         case RAW_QFS_DEVICE_INFO:
1763         case RAW_QFS_DEVICE_INFORMATION:
1764                 d_printf("\tdevice_type:                %lu\n", 
1765                          (unsigned long) fsinfo.device_info.out.device_type);
1766                 d_printf("\tcharacteristics:            0x%lx\n", 
1767                          (unsigned long) fsinfo.device_info.out.characteristics);
1768                 break;
1769         case RAW_QFS_ATTRIBUTE_INFORMATION:
1770         case RAW_QFS_ATTRIBUTE_INFO:
1771                 d_printf("\tfs_attr:                    0x%lx\n", 
1772                          (unsigned long) fsinfo.attribute_info.out.fs_attr);
1773                 d_printf("\tmax_file_component_length:  %lu\n", 
1774                          (unsigned long) fsinfo.attribute_info.out.max_file_component_length);
1775                 d_printf("\tfs_type:                    %s\n", fsinfo.attribute_info.out.fs_type.s);
1776                 break;
1777         case RAW_QFS_UNIX_INFO:
1778                 d_printf("\tmajor_version:              %hu\n", 
1779                          (unsigned short) fsinfo.unix_info.out.major_version);
1780                 d_printf("\tminor_version:              %hu\n", 
1781                          (unsigned short) fsinfo.unix_info.out.minor_version);
1782                 d_printf("\tcapability:                 0x%llx\n", 
1783                          (unsigned long long) fsinfo.unix_info.out.capability);
1784                 break;
1785         case RAW_QFS_QUOTA_INFORMATION:
1786                 d_printf("\tunknown[3]:                 [%llu,%llu,%llu]\n", 
1787                          (unsigned long long) fsinfo.quota_information.out.unknown[0],
1788                          (unsigned long long) fsinfo.quota_information.out.unknown[1],
1789                          (unsigned long long) fsinfo.quota_information.out.unknown[2]);
1790                 d_printf("\tquota_soft:                 %llu\n", 
1791                          (unsigned long long) fsinfo.quota_information.out.quota_soft);
1792                 d_printf("\tquota_hard:                 %llu\n", 
1793                          (unsigned long long) fsinfo.quota_information.out.quota_hard);
1794                 d_printf("\tquota_flags:                0x%llx\n", 
1795                          (unsigned long long) fsinfo.quota_information.out.quota_flags);
1796                 break;
1797         case RAW_QFS_FULL_SIZE_INFORMATION:
1798                 d_printf("\ttotal_alloc_units:          %llu\n", 
1799                          (unsigned long long) fsinfo.full_size_information.out.total_alloc_units);
1800                 d_printf("\tcall_avail_alloc_units:     %llu\n", 
1801                          (unsigned long long) fsinfo.full_size_information.out.call_avail_alloc_units);
1802                 d_printf("\tactual_avail_alloc_units:   %llu\n", 
1803                          (unsigned long long) fsinfo.full_size_information.out.actual_avail_alloc_units);
1804                 d_printf("\tsectors_per_unit:           %lu\n", 
1805                          (unsigned long) fsinfo.full_size_information.out.sectors_per_unit);
1806                 d_printf("\tbytes_per_sector:           %lu\n", 
1807                          (unsigned long) fsinfo.full_size_information.out.bytes_per_sector);
1808                 break;
1809         case RAW_QFS_OBJECTID_INFORMATION:
1810                 d_printf("\tGUID:                       %s\n", 
1811                          GUID_string(mem_ctx,&fsinfo.objectid_information.out.guid));
1812                 d_printf("\tunknown[6]:                 [%llu,%llu,%llu,%llu,%llu,%llu]\n", 
1813                          (unsigned long long) fsinfo.objectid_information.out.unknown[0],
1814                          (unsigned long long) fsinfo.objectid_information.out.unknown[2],
1815                          (unsigned long long) fsinfo.objectid_information.out.unknown[3],
1816                          (unsigned long long) fsinfo.objectid_information.out.unknown[4],
1817                          (unsigned long long) fsinfo.objectid_information.out.unknown[5],
1818                          (unsigned long long) fsinfo.objectid_information.out.unknown[6] );
1819                 break;
1820         case RAW_QFS_GENERIC:
1821                 d_printf("\twrong level returned\n");
1822                 break;
1823         }
1824   
1825  done:
1826         talloc_free(mem_ctx);
1827         return ret;
1828 }
1829
1830 /****************************************************************************
1831 show as much information as possible about a file
1832 ****************************************************************************/
1833 static int cmd_allinfo(const char **cmd_ptr)
1834 {
1835         pstring fname;
1836         fstring buf;
1837         int ret = 0;
1838         TALLOC_CTX *mem_ctx;
1839         union smb_fileinfo finfo;
1840         NTSTATUS status;
1841
1842         pstrcpy(fname,cur_dir);
1843         
1844         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1845                 d_printf("allinfo <filename>\n");
1846                 return 1;
1847         }
1848         pstrcat(fname,buf);
1849
1850         mem_ctx = talloc_init("%s", fname);
1851
1852         /* first a ALL_INFO QPATHINFO */
1853         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
1854         finfo.generic.in.fname = fname;
1855         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1856         if (!NT_STATUS_IS_OK(status)) {
1857                 d_printf("%s - %s\n", fname, nt_errstr(status));
1858                 ret = 1;
1859                 goto done;
1860         }
1861
1862         d_printf("\tcreate_time:    %s\n", nt_time_string(mem_ctx, finfo.all_info.out.create_time));
1863         d_printf("\taccess_time:    %s\n", nt_time_string(mem_ctx, finfo.all_info.out.access_time));
1864         d_printf("\twrite_time:     %s\n", nt_time_string(mem_ctx, finfo.all_info.out.write_time));
1865         d_printf("\tchange_time:    %s\n", nt_time_string(mem_ctx, finfo.all_info.out.change_time));
1866         d_printf("\tattrib:         0x%x\n", finfo.all_info.out.attrib);
1867         d_printf("\talloc_size:     %lu\n", (unsigned long)finfo.all_info.out.alloc_size);
1868         d_printf("\tsize:           %lu\n", (unsigned long)finfo.all_info.out.size);
1869         d_printf("\tnlink:          %u\n", finfo.all_info.out.nlink);
1870         d_printf("\tdelete_pending: %u\n", finfo.all_info.out.delete_pending);
1871         d_printf("\tdirectory:      %u\n", finfo.all_info.out.directory);
1872         d_printf("\tea_size:        %u\n", finfo.all_info.out.ea_size);
1873         d_printf("\tfname:          '%s'\n", finfo.all_info.out.fname.s);
1874
1875         /* 8.3 name if any */
1876         finfo.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
1877         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1878         if (NT_STATUS_IS_OK(status)) {
1879                 d_printf("\talt_name:       %s\n", finfo.alt_name_info.out.fname.s);
1880         }
1881
1882         /* file_id if available */
1883         finfo.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
1884         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1885         if (NT_STATUS_IS_OK(status)) {
1886                 d_printf("\tfile_id         %.0f\n", 
1887                          (double)finfo.internal_information.out.file_id);
1888         }
1889
1890         /* the EAs, if any */
1891         finfo.generic.level = RAW_FILEINFO_ALL_EAS;
1892         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1893         if (NT_STATUS_IS_OK(status)) {
1894                 int i;
1895                 for (i=0;i<finfo.all_eas.out.num_eas;i++) {
1896                         d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
1897                                  finfo.all_eas.out.eas[i].flags,
1898                                  (int)finfo.all_eas.out.eas[i].value.length,
1899                                  finfo.all_eas.out.eas[i].name.s);
1900                 }
1901         }
1902
1903         /* streams, if available */
1904         finfo.generic.level = RAW_FILEINFO_STREAM_INFO;
1905         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1906         if (NT_STATUS_IS_OK(status)) {
1907                 int i;
1908                 for (i=0;i<finfo.stream_info.out.num_streams;i++) {
1909                         d_printf("\tstream %d:\n", i);
1910                         d_printf("\t\tsize       %ld\n", 
1911                                  (long)finfo.stream_info.out.streams[i].size);
1912                         d_printf("\t\talloc size %ld\n", 
1913                                  (long)finfo.stream_info.out.streams[i].alloc_size);
1914                         d_printf("\t\tname       %s\n", finfo.stream_info.out.streams[i].stream_name.s);
1915                 }
1916         }       
1917
1918         /* dev/inode if available */
1919         finfo.generic.level = RAW_FILEINFO_COMPRESSION_INFORMATION;
1920         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1921         if (NT_STATUS_IS_OK(status)) {
1922                 d_printf("\tcompressed size %ld\n", (long)finfo.compression_info.out.compressed_size);
1923                 d_printf("\tformat          %ld\n", (long)finfo.compression_info.out.format);
1924                 d_printf("\tunit_shift      %ld\n", (long)finfo.compression_info.out.unit_shift);
1925                 d_printf("\tchunk_shift     %ld\n", (long)finfo.compression_info.out.chunk_shift);
1926                 d_printf("\tcluster_shift   %ld\n", (long)finfo.compression_info.out.cluster_shift);
1927         }
1928
1929         talloc_free(mem_ctx);
1930
1931 done:
1932         return ret;
1933 }
1934
1935
1936 /****************************************************************************
1937 shows EA contents
1938 ****************************************************************************/
1939 static int cmd_eainfo(const char **cmd_ptr)
1940 {
1941         pstring fname;
1942         fstring buf;
1943         int ret = 0;
1944         TALLOC_CTX *mem_ctx;
1945         union smb_fileinfo finfo;
1946         NTSTATUS status;
1947         int i;
1948
1949         pstrcpy(fname,cur_dir);
1950         
1951         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1952                 d_printf("eainfo <filename>\n");
1953                 return 1;
1954         }
1955         pstrcat(fname,buf);
1956
1957         mem_ctx = talloc_init("%s", fname);
1958
1959         finfo.generic.in.fname = fname;
1960         finfo.generic.level = RAW_FILEINFO_ALL_EAS;
1961         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1962         
1963         if (!NT_STATUS_IS_OK(status)) {
1964                 d_printf("RAW_FILEINFO_ALL_EAS - %s\n", nt_errstr(status));
1965                 talloc_free(mem_ctx);
1966                 return 1;
1967         }
1968
1969         d_printf("%s has %d EAs\n", fname, finfo.all_eas.out.num_eas);
1970
1971         for (i=0;i<finfo.all_eas.out.num_eas;i++) {
1972                 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
1973                          finfo.all_eas.out.eas[i].flags,
1974                          (int)finfo.all_eas.out.eas[i].value.length,
1975                          finfo.all_eas.out.eas[i].name.s);
1976                 fflush(stdout);
1977                 dump_data(0, 
1978                           finfo.all_eas.out.eas[i].value.data,
1979                           finfo.all_eas.out.eas[i].value.length);
1980         }
1981
1982         talloc_free(mem_ctx);
1983
1984         return ret;
1985 }
1986
1987
1988 /****************************************************************************
1989 show any ACL on a file
1990 ****************************************************************************/
1991 static int cmd_acl(const char **cmd_ptr)
1992 {
1993         pstring fname;
1994         fstring buf;
1995         int ret = 0;
1996         TALLOC_CTX *mem_ctx;
1997         union smb_fileinfo query;
1998         NTSTATUS status;
1999         int fnum;
2000
2001         pstrcpy(fname,cur_dir);
2002         
2003         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2004                 d_printf("acl <filename>\n");
2005                 return 1;
2006         }
2007         pstrcat(fname,buf);
2008
2009         fnum = smbcli_nt_create_full(cli->tree, fname, 0, 
2010                                      SEC_STD_READ_CONTROL,
2011                                      0,
2012                                      NTCREATEX_SHARE_ACCESS_DELETE|
2013                                      NTCREATEX_SHARE_ACCESS_READ|
2014                                      NTCREATEX_SHARE_ACCESS_WRITE, 
2015                                      NTCREATEX_DISP_OPEN,
2016                                      0, 0);
2017         if (fnum == -1) {
2018                 d_printf("%s - %s\n", fname, smbcli_errstr(cli->tree));
2019                 return -1;
2020         }
2021
2022         mem_ctx = talloc_init("%s", fname);
2023
2024         query.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
2025         query.query_secdesc.in.fnum = fnum;
2026         query.query_secdesc.secinfo_flags = 0x7;
2027
2028         status = smb_raw_fileinfo(cli->tree, mem_ctx, &query);
2029         if (!NT_STATUS_IS_OK(status)) {
2030                 d_printf("%s - %s\n", fname, nt_errstr(status));
2031                 ret = 1;
2032                 goto done;
2033         }
2034
2035         NDR_PRINT_DEBUG(security_descriptor, query.query_secdesc.out.sd);
2036
2037         talloc_free(mem_ctx);
2038
2039 done:
2040         return ret;
2041 }
2042
2043 /****************************************************************************
2044 lookup a name or sid
2045 ****************************************************************************/
2046 static int cmd_lookup(const char **cmd_ptr)
2047 {
2048         fstring buf;
2049         TALLOC_CTX *mem_ctx = talloc_new(NULL);
2050         NTSTATUS status;
2051         struct dom_sid *sid;
2052
2053         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2054                 d_printf("lookup <sid|name>\n");
2055                 talloc_free(mem_ctx);
2056                 return 1;
2057         }
2058
2059         sid = dom_sid_parse_talloc(mem_ctx, buf);
2060         if (sid == NULL) {
2061                 const char *sidstr;
2062                 status = smblsa_lookup_name(cli, buf, mem_ctx, &sidstr);
2063                 if (!NT_STATUS_IS_OK(status)) {
2064                         d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
2065                         talloc_free(mem_ctx);
2066                         return 1;
2067                 }
2068
2069                 d_printf("%s\n", sidstr);
2070         } else {
2071                 const char *name;
2072                 status = smblsa_lookup_sid(cli, buf, mem_ctx, &name);
2073                 if (!NT_STATUS_IS_OK(status)) {
2074                         d_printf("lsa_LookupSids - %s\n", nt_errstr(status));
2075                         talloc_free(mem_ctx);
2076                         return 1;
2077                 }
2078
2079                 d_printf("%s\n", name);
2080         }
2081
2082         talloc_free(mem_ctx);
2083
2084         return 0;
2085 }
2086
2087 /****************************************************************************
2088 show privileges for a user
2089 ****************************************************************************/
2090 static int cmd_privileges(const char **cmd_ptr)
2091 {
2092         fstring buf;
2093         TALLOC_CTX *mem_ctx = talloc_new(NULL);
2094         NTSTATUS status;
2095         struct dom_sid *sid;
2096         struct lsa_RightSet rights;
2097         unsigned i;
2098
2099         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2100                 d_printf("privileges <sid|name>\n");
2101                 talloc_free(mem_ctx);
2102                 return 1;
2103         }
2104
2105         sid = dom_sid_parse_talloc(mem_ctx, buf);
2106         if (sid == NULL) {
2107                 const char *sid_str;
2108                 status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
2109                 if (!NT_STATUS_IS_OK(status)) {
2110                         d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
2111                         talloc_free(mem_ctx);
2112                         return 1;
2113                 }
2114                 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
2115         }
2116
2117         status = smblsa_sid_privileges(cli, sid, mem_ctx, &rights);
2118         if (!NT_STATUS_IS_OK(status)) {
2119                 d_printf("lsa_EnumAccountRights - %s\n", nt_errstr(status));
2120                 talloc_free(mem_ctx);
2121                 return 1;
2122         }
2123
2124         for (i=0;i<rights.count;i++) {
2125                 d_printf("\t%s\n", rights.names[i].string);
2126         }
2127
2128         talloc_free(mem_ctx);
2129
2130         return 0;
2131 }
2132
2133
2134 /****************************************************************************
2135 add privileges for a user
2136 ****************************************************************************/
2137 static int cmd_addprivileges(const char **cmd_ptr)
2138 {
2139         fstring buf;
2140         TALLOC_CTX *mem_ctx = talloc_new(NULL);
2141         NTSTATUS status;
2142         struct dom_sid *sid;
2143         struct lsa_RightSet rights;
2144
2145         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2146                 d_printf("addprivileges <sid|name> <privilege...>\n");
2147                 talloc_free(mem_ctx);
2148                 return 1;
2149         }
2150
2151         sid = dom_sid_parse_talloc(mem_ctx, buf);
2152         if (sid == NULL) {
2153                 const char *sid_str;
2154                 status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
2155                 if (!NT_STATUS_IS_OK(status)) {
2156                         d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
2157                         talloc_free(mem_ctx);
2158                         return 1;
2159                 }
2160                 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
2161         }
2162
2163         ZERO_STRUCT(rights);
2164         while (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2165                 rights.names = talloc_realloc(mem_ctx, rights.names, 
2166                                                 struct lsa_String, rights.count+1);
2167                 rights.names[rights.count].string = talloc_strdup(mem_ctx, buf);
2168                 rights.count++;
2169         }
2170
2171
2172         status = smblsa_sid_add_privileges(cli, sid, mem_ctx, &rights);
2173         if (!NT_STATUS_IS_OK(status)) {
2174                 d_printf("lsa_AddAccountRights - %s\n", nt_errstr(status));
2175                 talloc_free(mem_ctx);
2176                 return 1;
2177         }
2178
2179         talloc_free(mem_ctx);
2180
2181         return 0;
2182 }
2183
2184 /****************************************************************************
2185 delete privileges for a user
2186 ****************************************************************************/
2187 static int cmd_delprivileges(const char **cmd_ptr)
2188 {
2189         fstring buf;
2190         TALLOC_CTX *mem_ctx = talloc_new(NULL);
2191         NTSTATUS status;
2192         struct dom_sid *sid;
2193         struct lsa_RightSet rights;
2194
2195         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2196                 d_printf("delprivileges <sid|name> <privilege...>\n");
2197                 talloc_free(mem_ctx);
2198                 return 1;
2199         }
2200
2201         sid = dom_sid_parse_talloc(mem_ctx, buf);
2202         if (sid == NULL) {
2203                 const char *sid_str;
2204                 status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
2205                 if (!NT_STATUS_IS_OK(status)) {
2206                         d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
2207                         talloc_free(mem_ctx);
2208                         return 1;
2209                 }
2210                 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
2211         }
2212
2213         ZERO_STRUCT(rights);
2214         while (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2215                 rights.names = talloc_realloc(mem_ctx, rights.names, 
2216                                                 struct lsa_String, rights.count+1);
2217                 rights.names[rights.count].string = talloc_strdup(mem_ctx, buf);
2218                 rights.count++;
2219         }
2220
2221
2222         status = smblsa_sid_del_privileges(cli, sid, mem_ctx, &rights);
2223         if (!NT_STATUS_IS_OK(status)) {
2224                 d_printf("lsa_RemoveAccountRights - %s\n", nt_errstr(status));
2225                 talloc_free(mem_ctx);
2226                 return 1;
2227         }
2228
2229         talloc_free(mem_ctx);
2230
2231         return 0;
2232 }
2233
2234
2235 /****************************************************************************
2236 ****************************************************************************/
2237 static int cmd_open(const char **cmd_ptr)
2238 {
2239         pstring mask;
2240         fstring buf;
2241         
2242         pstrcpy(mask,cur_dir);
2243         
2244         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2245                 d_printf("open <filename>\n");
2246                 return 1;
2247         }
2248         pstrcat(mask,buf);
2249
2250         smbcli_open(cli->tree, mask, O_RDWR, DENY_ALL);
2251
2252         return 0;
2253 }
2254
2255
2256 /****************************************************************************
2257 remove a directory
2258 ****************************************************************************/
2259 static int cmd_rmdir(const char **cmd_ptr)
2260 {
2261         pstring mask;
2262         fstring buf;
2263   
2264         pstrcpy(mask,cur_dir);
2265         
2266         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2267                 d_printf("rmdir <dirname>\n");
2268                 return 1;
2269         }
2270         pstrcat(mask,buf);
2271
2272         if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, mask))) {
2273                 d_printf("%s removing remote directory file %s\n",
2274                          smbcli_errstr(cli->tree),mask);
2275         }
2276         
2277         return 0;
2278 }
2279
2280 /****************************************************************************
2281  UNIX hardlink.
2282 ****************************************************************************/
2283 static int cmd_link(const char **cmd_ptr)
2284 {
2285         pstring src,dest;
2286         fstring buf,buf2;
2287   
2288         if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2289                 d_printf("Server doesn't support UNIX CIFS calls.\n");
2290                 return 1;
2291         }
2292
2293         pstrcpy(src,cur_dir);
2294         pstrcpy(dest,cur_dir);
2295   
2296         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
2297             !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2298                 d_printf("link <src> <dest>\n");
2299                 return 1;
2300         }
2301
2302         pstrcat(src,buf);
2303         pstrcat(dest,buf2);
2304
2305         if (NT_STATUS_IS_ERR(smbcli_unix_hardlink(cli->tree, src, dest))) {
2306                 d_printf("%s linking files (%s -> %s)\n", smbcli_errstr(cli->tree), src, dest);
2307                 return 1;
2308         }  
2309
2310         return 0;
2311 }
2312
2313 /****************************************************************************
2314  UNIX symlink.
2315 ****************************************************************************/
2316
2317 static int cmd_symlink(const char **cmd_ptr)
2318 {
2319         pstring src,dest;
2320         fstring buf,buf2;
2321   
2322         if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2323                 d_printf("Server doesn't support UNIX CIFS calls.\n");
2324                 return 1;
2325         }
2326
2327         pstrcpy(src,cur_dir);
2328         pstrcpy(dest,cur_dir);
2329         
2330         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
2331             !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2332                 d_printf("symlink <src> <dest>\n");
2333                 return 1;
2334         }
2335
2336         pstrcat(src,buf);
2337         pstrcat(dest,buf2);
2338
2339         if (NT_STATUS_IS_ERR(smbcli_unix_symlink(cli->tree, src, dest))) {
2340                 d_printf("%s symlinking files (%s -> %s)\n",
2341                         smbcli_errstr(cli->tree), src, dest);
2342                 return 1;
2343         } 
2344
2345         return 0;
2346 }
2347
2348 /****************************************************************************
2349  UNIX chmod.
2350 ****************************************************************************/
2351
2352 static int cmd_chmod(const char **cmd_ptr)
2353 {
2354         pstring src;
2355         mode_t mode;
2356         fstring buf, buf2;
2357   
2358         if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2359                 d_printf("Server doesn't support UNIX CIFS calls.\n");
2360                 return 1;
2361         }
2362
2363         pstrcpy(src,cur_dir);
2364         
2365         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
2366             !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2367                 d_printf("chmod mode file\n");
2368                 return 1;
2369         }
2370
2371         mode = (mode_t)strtol(buf, NULL, 8);
2372         pstrcat(src,buf2);
2373
2374         if (NT_STATUS_IS_ERR(smbcli_unix_chmod(cli->tree, src, mode))) {
2375                 d_printf("%s chmod file %s 0%o\n",
2376                         smbcli_errstr(cli->tree), src, (uint_t)mode);
2377                 return 1;
2378         } 
2379
2380         return 0;
2381 }
2382
2383 /****************************************************************************
2384  UNIX chown.
2385 ****************************************************************************/
2386
2387 static int cmd_chown(const char **cmd_ptr)
2388 {
2389         pstring src;
2390         uid_t uid;
2391         gid_t gid;
2392         fstring buf, buf2, buf3;
2393   
2394         if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2395                 d_printf("Server doesn't support UNIX CIFS calls.\n");
2396                 return 1;
2397         }
2398
2399         pstrcpy(src,cur_dir);
2400         
2401         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
2402             !next_token(cmd_ptr,buf2,NULL, sizeof(buf2)) ||
2403             !next_token(cmd_ptr,buf3,NULL, sizeof(buf3))) {
2404                 d_printf("chown uid gid file\n");
2405                 return 1;
2406         }
2407
2408         uid = (uid_t)atoi(buf);
2409         gid = (gid_t)atoi(buf2);
2410         pstrcat(src,buf3);
2411
2412         if (NT_STATUS_IS_ERR(smbcli_unix_chown(cli->tree, src, uid, gid))) {
2413                 d_printf("%s chown file %s uid=%d, gid=%d\n",
2414                         smbcli_errstr(cli->tree), src, (int)uid, (int)gid);
2415                 return 1;
2416         } 
2417
2418         return 0;
2419 }
2420
2421 /****************************************************************************
2422 rename some files
2423 ****************************************************************************/
2424 static int cmd_rename(const char **cmd_ptr)
2425 {
2426         pstring src,dest;
2427         fstring buf,buf2;
2428   
2429         pstrcpy(src,cur_dir);
2430         pstrcpy(dest,cur_dir);
2431         
2432         if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
2433             !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2434                 d_printf("rename <src> <dest>\n");
2435                 return 1;
2436         }
2437
2438         pstrcat(src,buf);
2439         pstrcat(dest,buf2);
2440
2441         if (NT_STATUS_IS_ERR(smbcli_rename(cli->tree, src, dest))) {
2442                 d_printf("%s renaming files\n",smbcli_errstr(cli->tree));
2443                 return 1;
2444         }
2445         
2446         return 0;
2447 }
2448
2449
2450 /****************************************************************************
2451 toggle the prompt flag
2452 ****************************************************************************/
2453 static int cmd_prompt(const char **cmd_ptr)
2454 {
2455         prompt = !prompt;
2456         DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2457         
2458         return 1;
2459 }
2460
2461
2462 /****************************************************************************
2463 set the newer than time
2464 ****************************************************************************/
2465 static int cmd_newer(const char **cmd_ptr)
2466 {
2467         fstring buf;
2468         BOOL ok;
2469         struct stat sbuf;
2470
2471         ok = next_token(cmd_ptr,buf,NULL,sizeof(buf));
2472         if (ok && (stat(buf,&sbuf) == 0)) {
2473                 newer_than = sbuf.st_mtime;
2474                 DEBUG(1,("Getting files newer than %s",
2475                          asctime(localtime(&newer_than))));
2476         } else {
2477                 newer_than = 0;
2478         }
2479
2480         if (ok && newer_than == 0) {
2481                 d_printf("Error setting newer-than time\n");
2482                 return 1;
2483         }
2484
2485         return 0;
2486 }
2487
2488 /****************************************************************************
2489 set the archive level
2490 ****************************************************************************/
2491 static int cmd_archive(const char **cmd_ptr)
2492 {
2493         fstring buf;
2494
2495         if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2496                 archive_level = atoi(buf);
2497         } else
2498                 d_printf("Archive level is %d\n",archive_level);
2499
2500         return 0;
2501 }
2502
2503 /****************************************************************************
2504 toggle the lowercaseflag
2505 ****************************************************************************/
2506 static int cmd_lowercase(const char **cmd_ptr)
2507 {
2508         lowercase = !lowercase;
2509         DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2510
2511         return 0;
2512 }
2513
2514
2515
2516
2517 /****************************************************************************
2518 toggle the recurse flag
2519 ****************************************************************************/
2520 static int cmd_recurse(const char **cmd_ptr)
2521 {
2522         recurse = !recurse;
2523         DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2524
2525         return 0;
2526 }
2527
2528 /****************************************************************************
2529 toggle the translate flag
2530 ****************************************************************************/
2531 static int cmd_translate(const char **cmd_ptr)
2532 {
2533         translation = !translation;
2534         DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2535                  translation?"on":"off"));
2536
2537         return 0;
2538 }
2539
2540
2541 /****************************************************************************
2542 do a printmode command
2543 ****************************************************************************/
2544 static int cmd_printmode(const char **cmd_ptr)
2545 {
2546         fstring buf;
2547         fstring mode;
2548
2549         if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2550                 if (strequal(buf,"text")) {
2551                         printmode = 0;      
2552                 } else {
2553                         if (strequal(buf,"graphics"))
2554                                 printmode = 1;
2555                         else
2556                                 printmode = atoi(buf);
2557                 }
2558         }
2559
2560         switch(printmode)
2561                 {
2562                 case 0: 
2563                         fstrcpy(mode,"text");
2564                         break;
2565                 case 1: 
2566                         fstrcpy(mode,"graphics");
2567                         break;
2568                 default: 
2569                         slprintf(mode,sizeof(mode)-1,"%d",printmode);
2570                         break;
2571                 }
2572         
2573         DEBUG(2,("the printmode is now %s\n",mode));
2574
2575         return 0;
2576 }
2577
2578 /****************************************************************************
2579  do the lcd command
2580  ****************************************************************************/
2581 static int cmd_lcd(const char **cmd_ptr)
2582 {
2583         fstring buf;
2584         char d[PATH_MAX];
2585         
2586         if (next_token(cmd_ptr,buf,NULL,sizeof(buf)))
2587                 chdir(buf);
2588         DEBUG(2,("the local directory is now %s\n",getcwd(d, PATH_MAX)));
2589
2590         return 0;
2591 }
2592
2593 /****************************************************************************
2594 history
2595 ****************************************************************************/
2596 static int cmd_history(const char **cmd_ptr)
2597 {
2598 #if defined(HAVE_LIBREADLINE)
2599         HIST_ENTRY **hlist;
2600         int i;
2601
2602         hlist = history_list();
2603         
2604         for (i = 0; hlist && hlist[i]; i++) {
2605                 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
2606         }
2607 #else
2608         DEBUG(0,("no history without readline support\n"));
2609 #endif
2610
2611         return 0;
2612 }
2613
2614 /****************************************************************************
2615  get a file restarting at end of local file
2616  ****************************************************************************/
2617 static int cmd_reget(const char **cmd_ptr)
2618 {
2619         pstring local_name;
2620         pstring remote_name;
2621         char *p;
2622
2623         pstrcpy(remote_name, cur_dir);
2624         pstrcat(remote_name, "\\");
2625         
2626         p = remote_name + strlen(remote_name);
2627         
2628         if (!next_token(cmd_ptr, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2629                 d_printf("reget <filename>\n");
2630                 return 1;
2631         }
2632         pstrcpy(local_name, p);
2633         dos_clean_name(remote_name);
2634         
2635         next_token(cmd_ptr, local_name, NULL, sizeof(local_name));
2636         
2637         return do_get(remote_name, local_name, True);
2638 }
2639
2640 /****************************************************************************
2641  put a file restarting at end of local file
2642  ****************************************************************************/
2643 static int cmd_reput(const char **cmd_ptr)
2644 {
2645         pstring local_name;
2646         pstring remote_name;
2647         fstring buf;
2648         char *p = buf;
2649         
2650         pstrcpy(remote_name, cur_dir);
2651         pstrcat(remote_name, "\\");
2652   
2653         if (!next_token(cmd_ptr, p, NULL, sizeof(buf))) {
2654                 d_printf("reput <filename>\n");
2655                 return 1;
2656         }
2657         pstrcpy(local_name, p);
2658   
2659         if (!file_exist(local_name)) {
2660                 d_printf("%s does not exist\n", local_name);
2661                 return 1;
2662         }
2663
2664         if (next_token(cmd_ptr, p, NULL, sizeof(buf)))
2665                 pstrcat(remote_name, p);
2666         else
2667                 pstrcat(remote_name, local_name);
2668         
2669         dos_clean_name(remote_name);
2670
2671         return do_put(remote_name, local_name, True);
2672 }
2673
2674
2675 /*
2676   return a string representing a share type
2677 */
2678 static const char *share_type_str(uint32_t type)
2679 {
2680         switch (type & 0xF) {
2681         case STYPE_DISKTREE: 
2682                 return "Disk";
2683         case STYPE_PRINTQ: 
2684                 return "Printer";
2685         case STYPE_DEVICE: 
2686                 return "Device";
2687         case STYPE_IPC: 
2688                 return "IPC";
2689         default:
2690                 return "Unknown";
2691         }
2692 }
2693
2694
2695 /*
2696   display a list of shares from a level 1 share enum
2697 */
2698 static void display_share_result(struct srvsvc_NetShareCtr1 *ctr1)
2699 {
2700         int i;
2701
2702         for (i=0;i<ctr1->count;i++) {
2703                 struct srvsvc_NetShareInfo1 *info = ctr1->array+i;
2704
2705                 printf("\t%-15s %-10.10s %s\n", 
2706                        info->name, 
2707                        share_type_str(info->type), 
2708                        info->comment);
2709         }
2710 }
2711
2712
2713
2714 /****************************************************************************
2715 try and browse available shares on a host
2716 ****************************************************************************/
2717 static BOOL browse_host(const char *query_host)
2718 {
2719         struct dcerpc_pipe *p;
2720         char *binding;
2721         NTSTATUS status;
2722         struct srvsvc_NetShareEnumAll r;
2723         uint32_t resume_handle = 0;
2724         TALLOC_CTX *mem_ctx = talloc_init("browse_host");
2725         struct srvsvc_NetShareCtr1 ctr1;
2726
2727         binding = talloc_asprintf(mem_ctx, "ncacn_np:%s", query_host);
2728
2729         status = dcerpc_pipe_connect(mem_ctx, &p, binding, 
2730                                          &dcerpc_table_srvsvc,
2731                                      cmdline_credentials, NULL);
2732         if (!NT_STATUS_IS_OK(status)) {
2733                 d_printf("Failed to connect to %s - %s\n", 
2734                          binding, nt_errstr(status));
2735                 talloc_free(mem_ctx);
2736                 return False;
2737         }
2738
2739         r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
2740         r.in.level = 1;
2741         r.in.ctr.ctr1 = &ctr1;
2742         r.in.max_buffer = ~0;
2743         r.in.resume_handle = &resume_handle;
2744
2745         d_printf("\n\tSharename       Type       Comment\n");
2746         d_printf("\t---------       ----       -------\n");
2747
2748         do {
2749                 ZERO_STRUCT(ctr1);
2750                 status = dcerpc_srvsvc_NetShareEnumAll(p, mem_ctx, &r);
2751
2752                 if (NT_STATUS_IS_OK(status) && 
2753                     (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA) ||
2754                      W_ERROR_IS_OK(r.out.result)) &&
2755                     r.out.ctr.ctr1) {
2756                         display_share_result(r.out.ctr.ctr1);
2757                         resume_handle += r.out.ctr.ctr1->count;
2758                 }
2759         } while (NT_STATUS_IS_OK(status) && W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA));
2760
2761         talloc_free(mem_ctx);
2762
2763         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2764                 d_printf("Failed NetShareEnumAll %s - %s/%s\n", 
2765                          binding, nt_errstr(status), win_errstr(r.out.result));
2766                 return False;
2767         }
2768
2769         return False;
2770 }
2771
2772 /****************************************************************************
2773 try and browse available connections on a host
2774 ****************************************************************************/
2775 static BOOL list_servers(const char *wk_grp)
2776 {
2777         d_printf("REWRITE: list servers not implemented\n");
2778         return False;
2779 }
2780
2781 /* Some constants for completing filename arguments */
2782
2783 #define COMPL_NONE        0          /* No completions */
2784 #define COMPL_REMOTE      1          /* Complete remote filename */
2785 #define COMPL_LOCAL       2          /* Complete local filename */
2786
2787 /* This defines the commands supported by this client.
2788  * NOTE: The "!" must be the last one in the list because it's fn pointer
2789  *       field is NULL, and NULL in that field is used in process_tok()
2790  *       (below) to indicate the end of the list.  crh
2791  */
2792 static struct
2793 {
2794   const char *name;
2795   int (*fn)(const char **cmd_ptr);
2796   const char *description;
2797   char compl_args[2];      /* Completion argument info */
2798 } commands[] = 
2799 {
2800   {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2801   {"addprivileges",cmd_addprivileges,"<sid|name> <privilege...> add privileges for a user",{COMPL_NONE,COMPL_NONE}},
2802   {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2803   {"acl",cmd_acl,"<file> show file ACL",{COMPL_NONE,COMPL_NONE}},
2804   {"allinfo",cmd_allinfo,"<file> show all possible info about a file",{COMPL_NONE,COMPL_NONE}},
2805   {"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}},
2806   {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2807   {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2808   {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2809   {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2810   {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2811   {"delprivileges",cmd_delprivileges,"<sid|name> <privilege...> remove privileges for a user",{COMPL_NONE,COMPL_NONE}},
2812   {"deltree",cmd_deltree,"<dir> delete a whole directory tree",{COMPL_REMOTE,COMPL_NONE}},
2813   {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2814   {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2815   {"eainfo",cmd_eainfo,"<file> show EA contents for a file",{COMPL_NONE,COMPL_NONE}},
2816   {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2817   {"fsinfo",cmd_fsinfo,"query file system info",{COMPL_NONE,COMPL_NONE}},
2818   {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2819   {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2820   {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2821   {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2822   {"link",cmd_link,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2823   {"lookup",cmd_lookup,"<sid|name> show SID for name or name for SID",{COMPL_NONE,COMPL_NONE}},
2824   {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},  
2825   {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2826   {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2827   {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2828   {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2829   {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2830   {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},  
2831   {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2832   {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2833   {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2834   {"privileges",cmd_privileges,"<user> show privileges for a user",{COMPL_NONE,COMPL_NONE}},
2835   {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2836   {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
2837   {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},  
2838   {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2839   {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2840   {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2841   {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2842   {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2843   {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2844   {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},  
2845   {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2846   {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2847   {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2848   {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2849   {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2850   {"symlink",cmd_symlink,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2851   {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2852   
2853   /* Yes, this must be here, see crh's comment above. */
2854   {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2855   {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2856 };
2857
2858
2859 /*******************************************************************
2860   lookup a command string in the list of commands, including 
2861   abbreviations
2862   ******************************************************************/
2863 static int process_tok(fstring tok)
2864 {
2865         int i = 0, matches = 0;
2866         int cmd=0;
2867         int tok_len = strlen(tok);
2868         
2869         while (commands[i].fn != NULL) {
2870                 if (strequal(commands[i].name,tok)) {
2871                         matches = 1;
2872                         cmd = i;
2873                         break;
2874                 } else if (strncasecmp(commands[i].name, tok, tok_len) == 0) {
2875                         matches++;
2876                         cmd = i;
2877                 }
2878                 i++;
2879         }
2880   
2881         if (matches == 0)
2882                 return(-1);
2883         else if (matches == 1)
2884                 return(cmd);
2885         else
2886                 return(-2);
2887 }
2888
2889 /****************************************************************************
2890 help
2891 ****************************************************************************/
2892 static int cmd_help(const char **cmd_ptr)
2893 {
2894         int i=0,j;
2895         fstring buf;
2896         
2897         if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2898                 if ((i = process_tok(buf)) >= 0)
2899                         d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
2900         } else {
2901                 while (commands[i].description) {
2902                         for (j=0; commands[i].description && (j<5); j++) {
2903                                 d_printf("%-15s",commands[i].name);
2904                                 i++;
2905                         }
2906                         d_printf("\n");
2907                 }
2908         }
2909         return 0;
2910 }
2911
2912 /****************************************************************************
2913 process a -c command string
2914 ****************************************************************************/
2915 static int process_command_string(char *cmd)
2916 {
2917         pstring line;
2918         const char *ptr;
2919         int rc = 0;
2920
2921         while (cmd[0] != '\0')    {
2922                 char *p;
2923                 fstring tok;
2924                 int i;
2925                 
2926                 if ((p = strchr_m(cmd, ';')) == 0) {
2927                         strncpy(line, cmd, 999);
2928                         line[1000] = '\0';
2929                         cmd += strlen(cmd);
2930                 } else {
2931                         if (p - cmd > 999) p = cmd + 999;
2932                         strncpy(line, cmd, p - cmd);
2933                         line[p - cmd] = '\0';
2934                         cmd = p + 1;
2935                 }
2936                 
2937                 /* and get the first part of the command */
2938                 ptr = line;
2939                 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
2940                 
2941                 if ((i = process_tok(tok)) >= 0) {
2942                         rc = commands[i].fn(&ptr);
2943                 } else if (i == -2) {
2944                         d_printf("%s: command abbreviation ambiguous\n",tok);
2945                 } else {
2946                         d_printf("%s: command not found\n",tok);
2947                 }
2948         }
2949         
2950         return rc;
2951 }       
2952
2953 #define MAX_COMPLETIONS 100
2954
2955 typedef struct {
2956         pstring dirmask;
2957         char **matches;
2958         int count, samelen;
2959         const char *text;
2960         int len;
2961 } completion_remote_t;
2962
2963 static void completion_remote_filter(struct clilist_file_info *f, const char *mask, void *state)
2964 {
2965         completion_remote_t *info = (completion_remote_t *)state;
2966
2967         if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
2968                 if ((info->dirmask[0] == 0) && !(f->attrib & FILE_ATTRIBUTE_DIRECTORY))
2969                         info->matches[info->count] = strdup(f->name);
2970                 else {
2971                         pstring tmp;
2972
2973                         if (info->dirmask[0] != 0)
2974                                 pstrcpy(tmp, info->dirmask);
2975                         else
2976                                 tmp[0] = 0;
2977                         pstrcat(tmp, f->name);
2978                         if (f->attrib & FILE_ATTRIBUTE_DIRECTORY)
2979                                 pstrcat(tmp, "/");
2980                         info->matches[info->count] = strdup(tmp);
2981                 }
2982                 if (info->matches[info->count] == NULL)
2983                         return;
2984                 if (f->attrib & FILE_ATTRIBUTE_DIRECTORY)
2985                         smb_readline_ca_char(0);
2986
2987                 if (info->count == 1)
2988                         info->samelen = strlen(info->matches[info->count]);
2989                 else
2990                         while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
2991                                 info->samelen--;
2992                 info->count++;
2993         }
2994 }
2995
2996 static char **remote_completion(const char *text, int len)
2997 {
2998         pstring dirmask;
2999         int i;
3000         completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
3001
3002         info.samelen = len;
3003         info.text = text;
3004         info.len = len;
3005  
3006         if (len >= PATH_MAX)
3007                 return(NULL);
3008
3009         info.matches = malloc_array_p(char *, MAX_COMPLETIONS);
3010         if (!info.matches) return NULL;
3011         info.matches[0] = NULL;
3012
3013         for (i = len-1; i >= 0; i--)
3014                 if ((text[i] == '/') || (text[i] == '\\'))
3015                         break;
3016         info.text = text+i+1;
3017         info.samelen = info.len = len-i-1;
3018
3019         if (i > 0) {
3020                 strncpy(info.dirmask, text, i+1);
3021                 info.dirmask[i+1] = 0;
3022                 snprintf(dirmask, sizeof(dirmask), "%s%*s*", cur_dir, i-1, text);
3023         } else
3024                 snprintf(dirmask, sizeof(dirmask), "%s*", cur_dir);
3025
3026         if (smbcli_list(cli->tree, dirmask, 
3027                      FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN, 
3028                      completion_remote_filter, &info) < 0)
3029                 goto cleanup;
3030
3031         if (info.count == 2)
3032                 info.matches[0] = strdup(info.matches[1]);
3033         else {
3034                 info.matches[0] = malloc(info.samelen+1);
3035                 if (!info.matches[0])
3036                         goto cleanup;
3037                 strncpy(info.matches[0], info.matches[1], info.samelen);
3038                 info.matches[0][info.samelen] = 0;
3039         }
3040         info.matches[info.count] = NULL;
3041         return info.matches;
3042
3043 cleanup:
3044         for (i = 0; i < info.count; i++)
3045                 free(info.matches[i]);
3046         free(info.matches);
3047         return NULL;
3048 }
3049
3050 static char **completion_fn(const char *text, int start, int end)
3051 {
3052         smb_readline_ca_char(' ');
3053
3054         if (start) {
3055                 const char *buf, *sp;
3056                 int i;
3057                 char compl_type;
3058
3059                 buf = smb_readline_get_line_buffer();
3060                 if (buf == NULL)
3061                         return NULL;
3062                 
3063                 sp = strchr(buf, ' ');
3064                 if (sp == NULL)
3065                         return NULL;
3066                 
3067                 for (i = 0; commands[i].name; i++)
3068                         if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
3069                                 break;
3070                 if (commands[i].name == NULL)
3071                         return NULL;
3072
3073                 while (*sp == ' ')
3074                         sp++;
3075
3076                 if (sp == (buf + start))
3077                         compl_type = commands[i].compl_args[0];
3078                 else
3079                         compl_type = commands[i].compl_args[1];
3080
3081                 if (compl_type == COMPL_REMOTE)
3082                         return remote_completion(text, end - start);
3083                 else /* fall back to local filename completion */
3084                         return NULL;
3085         } else {
3086                 char **matches;
3087                 int i, len, samelen = 0, count=1;
3088
3089                 matches = malloc_array_p(char *, MAX_COMPLETIONS);
3090                 if (!matches) return NULL;
3091                 matches[0] = NULL;
3092
3093                 len = strlen(text);
3094                 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3095                         if (strncmp(text, commands[i].name, len) == 0) {
3096                                 matches[count] = strdup(commands[i].name);
3097                                 if (!matches[count])
3098                                         goto cleanup;
3099                                 if (count == 1)
3100                                         samelen = strlen(matches[count]);
3101                                 else
3102                                         while (strncmp(matches[count], matches[count-1], samelen) != 0)
3103                                                 samelen--;
3104                                 count++;
3105                         }
3106                 }
3107
3108                 switch (count) {
3109                 case 0: /* should never happen */
3110                 case 1:
3111                         goto cleanup;
3112                 case 2:
3113                         matches[0] = strdup(matches[1]);
3114                         break;
3115                 default:
3116                         matches[0] = malloc(samelen+1);
3117                         if (!matches[0])
3118                                 goto cleanup;
3119                         strncpy(matches[0], matches[1], samelen);
3120                         matches[0][samelen] = 0;
3121                 }
3122                 matches[count] = NULL;
3123                 return matches;
3124
3125 cleanup:
3126                 while (i >= 0) {
3127                         free(matches[i]);
3128                         i--;
3129                 }
3130                 free(matches);
3131                 return NULL;
3132         }
3133 }
3134
3135 /****************************************************************************
3136 make sure we swallow keepalives during idle time
3137 ****************************************************************************/
3138 static void readline_callback(void)
3139 {
3140         static time_t last_t;
3141         time_t t;
3142
3143         t = time(NULL);
3144
3145         if (t - last_t < 5) return;
3146
3147         last_t = t;
3148
3149         smbcli_transport_process(cli->transport);
3150
3151         if (cli->tree) {
3152                 smbcli_chkpath(cli->tree, "\\");
3153         }
3154 }
3155
3156
3157 /****************************************************************************
3158 process commands on stdin
3159 ****************************************************************************/
3160 static void process_stdin(void)
3161 {
3162         while (1) {
3163                 fstring tok;
3164                 fstring the_prompt;
3165                 char *cline;
3166                 pstring line;
3167                 const char *ptr;
3168                 int i;
3169                 
3170                 /* display a prompt */
3171                 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3172                 cline = smb_readline(the_prompt, readline_callback, completion_fn);
3173                         
3174                 if (!cline) break;
3175                 
3176                 pstrcpy(line, cline);
3177
3178                 /* special case - first char is ! */
3179                 if (*line == '!') {
3180                         system(line + 1);
3181                         continue;
3182                 }
3183       
3184                 /* and get the first part of the command */
3185                 ptr = line;
3186                 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
3187
3188                 if ((i = process_tok(tok)) >= 0) {
3189                         commands[i].fn(&ptr);
3190                 } else if (i == -2) {
3191                         d_printf("%s: command abbreviation ambiguous\n",tok);
3192                 } else {
3193                         d_printf("%s: command not found\n",tok);
3194                 }
3195         }
3196 }
3197
3198
3199 /***************************************************** 
3200 return a connection to a server
3201 *******************************************************/
3202 static struct smbcli_state *do_connect(TALLOC_CTX *mem_ctx, 
3203                                        const char *server, const char *share, struct cli_credentials *cred)
3204 {
3205         struct smbcli_state *c;
3206         NTSTATUS status;
3207         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
3208         if (!tmp_ctx) {
3209                 return NULL;
3210         }
3211
3212         if (strncmp(share, "\\\\", 2) == 0 ||
3213             strncmp(share, "//", 2) == 0) {
3214                 smbcli_parse_unc(share, tmp_ctx, &server, &share);
3215         }
3216         
3217         status = smbcli_full_connection(mem_ctx, &c, server,
3218                                         share, NULL, cred, NULL);
3219         if (!NT_STATUS_IS_OK(status)) {
3220                 d_printf("Connection to \\\\%s\\%s failed - %s\n", 
3221                          server, share, nt_errstr(status));
3222                 talloc_free(tmp_ctx);
3223                 return NULL;
3224         }
3225         talloc_free(tmp_ctx);
3226
3227         return c;
3228 }
3229
3230
3231 /****************************************************************************
3232   process commands from the client
3233 ****************************************************************************/
3234 static int process(char *base_directory)
3235 {
3236         int rc = 0;
3237
3238         TALLOC_CTX *mem_ctx = talloc_new(NULL);
3239         if (!mem_ctx) {
3240                 return 1;
3241         }
3242
3243         cli = do_connect(mem_ctx, desthost, service, cmdline_credentials);
3244         if (!cli) {
3245                 return 1;
3246         }
3247
3248         if (*base_directory) do_cd(base_directory);
3249         
3250         if (cmdstr) {
3251                 rc = process_command_string(cmdstr);
3252         } else {
3253                 process_stdin();
3254         }
3255   
3256         talloc_free(mem_ctx);
3257         return rc;
3258 }
3259
3260 /****************************************************************************
3261 handle a -L query
3262 ****************************************************************************/
3263 static int do_host_query(char *query_host)
3264 {
3265         browse_host(query_host);
3266         list_servers(lp_workgroup());
3267         return(0);
3268 }
3269
3270
3271 /****************************************************************************
3272 handle a message operation
3273 ****************************************************************************/
3274 static int do_message_op(void)
3275 {
3276         struct nbt_name called, calling;
3277         const char *server_name;
3278
3279         make_nbt_name_client(&calling, lp_netbios_name());
3280
3281         nbt_choose_called_name(NULL, &called, desthost, name_type);
3282
3283         server_name = dest_ip ? dest_ip : desthost;
3284
3285         if (!(cli=smbcli_state_init(NULL)) || !smbcli_socket_connect(cli, server_name)) {
3286                 d_printf("Connection to %s failed\n", server_name);
3287                 return 1;
3288         }
3289
3290         if (!smbcli_transport_establish(cli, &calling, &called)) {
3291                 d_printf("session request failed\n");
3292                 talloc_free(cli);
3293                 return 1;
3294         }
3295
3296         send_message();
3297         talloc_free(cli);
3298
3299         return 0;
3300 }
3301
3302
3303 /**
3304  * Process "-L hostname" option.
3305  *
3306  * We don't actually do anything yet -- we just stash the name in a
3307  * global variable and do the query when all options have been read.
3308  **/
3309 static void remember_query_host(const char *arg,
3310                                 pstring query_host)
3311 {
3312         char *slash;
3313         
3314         while (*arg == '\\' || *arg == '/')
3315                 arg++;
3316         pstrcpy(query_host, arg);
3317         if ((slash = strchr(query_host, '/'))
3318             || (slash = strchr(query_host, '\\'))) {
3319                 *slash = 0;
3320         }
3321 }
3322
3323
3324 /****************************************************************************
3325   main program
3326 ****************************************************************************/
3327  int main(int argc,char *argv[])
3328 {
3329         fstring base_directory;
3330         int opt;
3331         pstring query_host;
3332         BOOL message = False;
3333         pstring term_code;
3334         poptContext pc;
3335         char *p;
3336         int rc = 0;
3337         TALLOC_CTX *mem_ctx;
3338         struct poptOption long_options[] = {
3339                 POPT_AUTOHELP
3340
3341                 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3342                 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3343                 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3344                 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3345                 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3346                 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3347                 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" }, 
3348                 { "send-buffer", 'b', POPT_ARG_INT, NULL, 'b', "Changes the transmit/send buffer", "BYTES" },
3349                 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3350                 POPT_COMMON_SAMBA
3351                 POPT_COMMON_CONNECTION
3352                 POPT_COMMON_CREDENTIALS
3353                 POPT_COMMON_VERSION
3354                 POPT_TABLEEND
3355         };
3356         
3357 #ifdef KANJI
3358         pstrcpy(term_code, KANJI);
3359 #else /* KANJI */
3360         *term_code = 0;
3361 #endif /* KANJI */
3362
3363         *query_host = 0;
3364         *base_directory = 0;
3365
3366         mem_ctx = talloc_init("client.c/main");
3367         if (!mem_ctx) {
3368                 d_printf("\nclient.c: Not enough memory\n");
3369                 exit(1);
3370         }
3371
3372         pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
3373         poptSetOtherOptionHelp(pc, "[OPTIONS] service <password>");
3374
3375         while ((opt = poptGetNextOpt(pc)) != -1) {
3376                 switch (opt) {
3377                 case 'M':
3378                         /* Messages are sent to NetBIOS name type 0x3
3379                          * (Messenger Service).  Make sure we default
3380                          * to port 139 instead of port 445. srl,crh
3381                          */
3382                         name_type = 0x03; 
3383                         pstrcpy(desthost,poptGetOptArg(pc));
3384                         if( 0 == port ) port = 139;
3385                         message = True;
3386                         break;
3387                 case 'I':
3388                         dest_ip = poptGetOptArg(pc);
3389                         break;
3390                 case 'L':
3391                         remember_query_host(poptGetOptArg(pc), query_host);
3392                         break;
3393                 case 't':
3394                         pstrcpy(term_code, poptGetOptArg(pc));
3395                         break;
3396                 case 'D':
3397                         fstrcpy(base_directory,poptGetOptArg(pc));
3398                         break;
3399                 case 'b':
3400                         io_bufsize = MAX(1, atoi(poptGetOptArg(pc)));
3401                         break;
3402                 }
3403         }
3404
3405         gensec_init();
3406
3407         if(poptPeekArg(pc)) {
3408                 pstrcpy(service,poptGetArg(pc));  
3409                 /* Convert any '/' characters in the service name to '\' characters */
3410                 string_replace(service, '/','\\');
3411
3412                 if (count_chars(service,'\\') < 3) {
3413                         d_printf("\n%s: Not enough '\\' characters in service\n",service);
3414                         poptPrintUsage(pc, stderr, 0);
3415                         exit(1);
3416                 }
3417         }
3418
3419         if (poptPeekArg(pc)) { 
3420                 cli_credentials_set_password(cmdline_credentials, poptGetArg(pc), CRED_SPECIFIED);
3421         }
3422
3423         /*init_names(); */
3424
3425         if (!*query_host && !*service && !message) {
3426                 poptPrintUsage(pc, stderr, 0);
3427                 exit(1);
3428         }
3429
3430         poptFreeContext(pc);
3431
3432         DEBUG( 3, ( "Client started (version %s).\n", SAMBA_VERSION_STRING ) );
3433
3434         talloc_free(mem_ctx);
3435
3436         if ((p=strchr_m(query_host,'#'))) {
3437                 *p = 0;
3438                 p++;
3439                 sscanf(p, "%x", &name_type);
3440         }
3441   
3442         if (*query_host) {
3443                 return do_host_query(query_host);
3444         }
3445
3446         if (message) {
3447                 return do_message_op();
3448         }
3449         
3450         if (process(base_directory)) {
3451                 return 1;
3452         }
3453
3454         return rc;
3455 }