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