source: trunk/samba-3.0.25pre1/source/client/client.c@ 1

Last change on this file since 1 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

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