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