r10656: BIG merge from trunk. Features not copied over
[nivanova/samba-autobuild/.git] / source3 / utils / smbget.c
1 /*
2    smbget: a wget-like utility with support for recursive downloading and 
3         smb:// urls
4    Copyright (C) 2003-2004 Jelmer Vernooij <jelmer@samba.org>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "includes.h"
21 #include "libsmbclient.h"
22
23 #if _FILE_OFFSET_BITS==64
24 #define OFF_T_FORMAT "%lld"
25 #else
26 #define OFF_T_FORMAT "%ld"
27 #endif
28
29 int columns = 0;
30
31 time_t total_start_time = 0;
32 off_t total_bytes = 0;
33
34 #define SMB_MAXPATHLEN MAXPATHLEN
35
36 /* Number of bytes to read when checking whether local and remote file are really the same file */
37 #define RESUME_CHECK_SIZE                               512
38 #define RESUME_DOWNLOAD_OFFSET                  1024
39 #define RESUME_CHECK_OFFSET                             RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE
40 /* Number of bytes to read at once */
41 #define SMB_DEFAULT_BLOCKSIZE                                   64000
42
43 const char *username = NULL, *password = NULL, *workgroup = NULL;
44 int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0;
45 int blocksize = SMB_DEFAULT_BLOCKSIZE;
46
47 int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile);
48
49 int get_num_cols(void)
50 {
51 #ifdef TIOCGWINSZ
52         struct winsize ws;
53         if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
54                 return 0;
55         }
56         return ws.ws_col;
57 #else
58 #warning No support for TIOCGWINSZ
59         char *cols = getenv("COLUMNS");
60         if(!cols) return 0;
61         return atoi(cols);
62 #endif
63 }
64
65 void change_columns(int sig)
66 {
67         columns = get_num_cols();
68 }
69
70 void human_readable(off_t s, char *buffer, int l)
71 {
72         if(s > 1024 * 1024 * 1024) snprintf(buffer, l, "%.2fGb", 1.0 * s / (1024 * 1024 * 1024));
73         else if(s > 1024 * 1024) snprintf(buffer, l, "%.2fMb", 1.0 * s / (1024 * 1024));
74         else if(s > 1024) snprintf(buffer, l, "%.2fkb", 1.0 * s / 1024);
75         else snprintf(buffer, l, OFF_T_FORMAT"b", s);
76 }
77
78 void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
79 {
80         static char hasasked = 0;
81         char *wgtmp, *usertmp;
82         char tmp[128];
83
84         if(hasasked) return;
85         hasasked = 1;
86
87         if(!nonprompt && !username) {
88                 printf("Username for %s at %s [guest] ", shr, srv);
89                 fgets(tmp, sizeof(tmp), stdin);
90                 if(tmp[strlen(tmp)-1] == '\n')tmp[strlen(tmp)-1] = '\0';
91                 strncpy(un, tmp, unlen-1);
92         } else if(username) strncpy(un, username, unlen-1);
93
94         if(!nonprompt && !password) {
95                 char *prompt, *pass;
96                 asprintf(&prompt, "Password for %s at %s: ", shr, srv);
97                 pass = getpass(prompt);
98                 free(prompt);
99                 strncpy(pw, pass, pwlen-1);
100         } else if(password) strncpy(pw, password, pwlen-1);
101
102         if(workgroup)strncpy(wg, workgroup, wglen-1);
103
104         wgtmp = SMB_STRNDUP(wg, wglen); 
105         usertmp = SMB_STRNDUP(un, unlen);
106         if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp);
107         free(wgtmp); free(usertmp);
108 }
109
110 int smb_download_dir(const char *base, const char *name, int resume)
111 {
112         char path[SMB_MAXPATHLEN];
113         int dirhandle;
114         struct smbc_dirent *dirent;
115         const char *relname = name;
116         char *tmpname;
117         struct stat remotestat;
118         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
119
120         /* List files in directory and call smb_download_file on them */
121         dirhandle = smbc_opendir(path);
122         if(dirhandle < 1) {
123                 if(errno == ENOTDIR) return smb_download_file(base, name, 1, resume, NULL);
124                 fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
125                 return 0;
126         }
127
128         while(*relname == '/')relname++;
129         mkdir(relname, 0755);
130         
131         tmpname = SMB_STRDUP(name);
132
133         while((dirent = smbc_readdir(dirhandle))) {
134                 char *newname;
135                 if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
136                 asprintf(&newname, "%s/%s", tmpname, dirent->name);
137                 switch(dirent->smbc_type) {
138                 case SMBC_DIR:
139                         smb_download_dir(base, newname, resume);
140                         break;
141
142                 case SMBC_WORKGROUP:
143                         smb_download_dir("smb://", dirent->name, resume);
144                         break;
145
146                 case SMBC_SERVER:
147                         smb_download_dir("smb://", dirent->name, resume);
148                         break;
149
150                 case SMBC_FILE:
151                         smb_download_file(base, newname, 1, resume, NULL);
152                         break;
153
154                 case SMBC_FILE_SHARE:
155                         smb_download_dir(base, newname, resume);
156                         break;
157
158                 case SMBC_PRINTER_SHARE:
159                         if(!quiet)printf("Ignoring printer share %s\n", dirent->name);
160                         break;
161
162                 case SMBC_COMMS_SHARE:
163                         if(!quiet)printf("Ignoring comms share %s\n", dirent->name);
164                         break;
165                         
166                 case SMBC_IPC_SHARE:
167                         if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name);
168                         break;
169
170                 default:
171                         fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type);
172                         break;
173                 }
174                 free(newname);
175         }
176         free(tmpname);
177
178         if(keep_permissions) {
179                 if(smbc_fstat(dirhandle, &remotestat) < 0) {
180                         fprintf(stderr, "Unable to get stats on %s on remote server\n", path);
181                         smbc_closedir(dirhandle);
182                         return 0;
183                 }
184                 
185                 if(chmod(relname, remotestat.st_mode) < 0) {
186                         fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname, remotestat.st_mode);
187                         smbc_closedir(dirhandle);
188                         return 0;
189                 }
190         }
191
192         smbc_closedir(dirhandle);
193         return 1;
194 }
195
196 char *print_time(long t)
197 {
198         static char buffer[100];
199         int secs, mins, hours;
200         if(t < -1) {
201                 strncpy(buffer, "Unknown", sizeof(buffer));
202                 return buffer;
203         }
204
205         secs = (int)t % 60;
206         mins = (int)t / 60 % 60;
207         hours = (int)t / (60 * 60);
208         snprintf(buffer, sizeof(buffer)-1, "%02d:%02d:%02d", hours, mins, secs);
209         return buffer;
210 }
211
212 void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total)
213 {
214         double avg = 0.0;
215         long  eta = -1; 
216         double prcnt = 0.0;
217         char hpos[20], htotal[20], havg[20];
218         char *status, *filename;
219         int len;
220         if(now - start)avg = 1.0 * (pos - start_pos) / (now - start);
221         eta = (total - pos) / avg;
222         if(total)prcnt = 100.0 * pos / total;
223
224         human_readable(pos, hpos, sizeof(hpos));
225         human_readable(total, htotal, sizeof(htotal));
226         human_readable(avg, havg, sizeof(havg));
227
228         len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos, htotal, prcnt, havg, print_time(eta));
229         
230         if(columns) {
231                 int required = strlen(name), available = columns - len - strlen("[] ");
232                 if(required > available) asprintf(&filename, "...%s", name + required - available + 3);
233                 else filename = SMB_STRNDUP(name, available);
234         } else filename = SMB_STRDUP(name);
235
236         fprintf(stderr, "\r[%s] %s", filename, status);
237
238         free(filename); free(status);
239 }
240
241 int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile) {
242         int remotehandle, localhandle;
243         time_t start_time = time(NULL);
244         const char *newpath;
245         char path[SMB_MAXPATHLEN];
246         char checkbuf[2][RESUME_CHECK_SIZE];
247         char *readbuf = NULL;
248         off_t offset_download = 0, offset_check = 0, curpos = 0, start_offset = 0;
249         struct stat localstat, remotestat;
250
251         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (*base && *name && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
252         
253         remotehandle = smbc_open(path, O_RDONLY, 0755);
254
255         if(remotehandle < 0) {
256                 switch(errno) {
257                 case EISDIR: 
258                         if(!recursive) {
259                                 fprintf(stderr, "%s is a directory. Specify -R to download recursively\n", path);
260                                 return 0;
261                         }
262                         smb_download_dir(base, name, resume);
263                         return 0;
264
265                 case ENOENT:
266                         fprintf(stderr, "%s can't be found on the remote server\n", path);
267                         return 0;
268
269                 case ENOMEM:
270                         fprintf(stderr, "Not enough memory\n");
271                         exit(1);
272                         return 0;
273
274                 case ENODEV:
275                         fprintf(stderr, "The share name used in %s does not exist\n", path);
276                         return 0;
277
278                 case EACCES:
279                         fprintf(stderr, "You don't have enough permissions to access %s\n", path);
280                         return 0;
281
282                 default:
283                         perror("smbc_open");
284                         return 0;
285                 }
286         } 
287
288         if(smbc_fstat(remotehandle, &remotestat) < 0) {
289                 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
290                 return 0;
291         }
292
293         if(outfile) newpath = outfile;
294         else if(!name[0]) {
295                 newpath = strrchr(base, '/');
296                 if(newpath)newpath++; else newpath = base;
297         } else newpath = name;
298
299         if(newpath[0] == '/')newpath++;
300         
301         /* Open local file and, if necessary, resume */
302         if(!send_stdout) {
303                 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755);
304                 if(localhandle < 0) {
305                         fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno));
306                         smbc_close(remotehandle);
307                         return 0;
308                 }
309         
310                 fstat(localhandle, &localstat);
311
312                 start_offset = localstat.st_size;
313
314                 if(localstat.st_size && localstat.st_size == remotestat.st_size) {
315                         if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path);
316                         else if(!quiet)fprintf(stderr, "%s\n", path);
317                         smbc_close(remotehandle);
318                         close(localhandle);
319                         return 1;
320                 }
321
322                 if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) {
323                         offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET;
324                         offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
325                         if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n"
326                                    "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n", newpath, offset_check, 
327                                    localstat.st_size, remotestat.st_size);
328                 }
329
330                 if(offset_check) { 
331                         off_t off1, off2;
332                         /* First, check all bytes from offset_check to offset_download */
333                         off1 = lseek(localhandle, offset_check, SEEK_SET);
334                         if(off1 < 0) {
335                                 fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n", offset_check, newpath);
336                                 smbc_close(remotehandle); close(localhandle);
337                                 return 0;
338                         }
339
340                         off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); 
341                         if(off2 < 0) {
342                                 fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n", offset_check, newpath);
343                                 smbc_close(remotehandle); close(localhandle);
344                                 return 0;
345                         }
346
347                         if(off1 != off2) {
348                                 fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n", off1, off2);
349                                 return 0;
350                         }
351
352                         if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
353                                 fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path);
354                                 smbc_close(remotehandle); close(localhandle);
355                                 return 0;
356                         }
357
358                         if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
359                                 fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name);
360                                 smbc_close(remotehandle); close(localhandle);
361                                 return 0;
362                         }
363
364                         if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) {
365                                 if(verbose)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT"\n", offset_download);
366                         } else {
367                                 fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path);
368                                 smbc_close(remotehandle); close(localhandle);
369                                 return 0;
370                         }
371                 }
372         } else {
373                 localhandle = STDOUT_FILENO;
374                 start_offset = 0;
375                 offset_download = 0;
376                 offset_check = 0;
377         }
378
379         readbuf = SMB_MALLOC(blocksize);
380
381         /* Now, download all bytes from offset_download to the end */
382         for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) {
383                 ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
384                 if(bytesread < 0) {
385                         fprintf(stderr, "Can't read %d bytes at offset "OFF_T_FORMAT", file %s\n", blocksize, curpos, path);
386                         smbc_close(remotehandle);
387                         if (localhandle != STDOUT_FILENO) close(localhandle);
388                         free(readbuf);
389                         return 0;
390                 }
391
392                 total_bytes += bytesread;
393
394                 if(write(localhandle, readbuf, bytesread) < 0) {
395                         fprintf(stderr, "Can't write %d bytes to local file %s at offset "OFF_T_FORMAT"\n", bytesread, path, curpos);
396                         free(readbuf);
397                         smbc_close(remotehandle);
398                         if (localhandle != STDOUT_FILENO) close(localhandle);
399                         return 0;
400                 }
401
402                 if(dots)fputc('.', stderr);
403                 else if(!quiet) {
404                         print_progress(newpath, start_time, time(NULL), start_offset, curpos, remotestat.st_size);
405                 }
406         }
407
408         free(readbuf);
409
410         if(dots){
411                 fputc('\n', stderr);
412                 printf("%s downloaded\n", path);
413         } else if(!quiet) {
414                 int i;
415                 fprintf(stderr, "\r%s", path);
416                 if(columns) {
417                         for(i = strlen(path); i < columns; i++) {
418                                 fputc(' ', stderr);
419                         }
420                 }
421                 fputc('\n', stderr);
422         }
423
424         if(keep_permissions && !send_stdout) {
425                 if(fchmod(localhandle, remotestat.st_mode) < 0) {
426                         fprintf(stderr, "Unable to change mode of local file %s to %o\n", path, remotestat.st_mode);
427                         smbc_close(remotehandle);
428                         close(localhandle);
429                         return 0;
430                 }
431         }
432
433         smbc_close(remotehandle);
434         if (localhandle != STDOUT_FILENO) close(localhandle);
435         return 1;
436 }
437
438 void clean_exit(void)
439 {
440         char bs[100];
441         human_readable(total_bytes, bs, sizeof(bs));
442         if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs, time(NULL) - total_start_time);
443         exit(0);
444 }
445
446 void signal_quit(int v)
447 {
448         clean_exit();
449 }
450
451 int readrcfile(const char *name, const struct poptOption long_options[])
452 {
453         FILE *fd = fopen(name, "r");
454         int lineno = 0, i;
455         char var[101], val[101];
456         char found;
457         int *intdata; char **stringdata;
458         if(!fd) {
459                 fprintf(stderr, "Can't open RC file %s\n", name);
460                 return 1;
461         }
462
463         while(!feof(fd)) {
464                 lineno++;
465                 if(fscanf(fd, "%100s %100s\n", var, val) < 2) {
466                         fprintf(stderr, "Can't parse line %d of %s, ignoring.\n", lineno, name);
467                         continue;
468                 }
469
470                 found = 0;
471
472                 for(i = 0; long_options[i].shortName; i++) {
473                         if(!long_options[i].longName)continue;
474                         if(strcmp(long_options[i].longName, var)) continue;
475                         if(!long_options[i].arg)continue;
476
477                         switch(long_options[i].argInfo) {
478                         case POPT_ARG_NONE:
479                                 intdata = (int *)long_options[i].arg;
480                                 if(!strcmp(val, "on")) *intdata = 1;
481                                 else if(!strcmp(val, "off")) *intdata = 0;
482                                 else fprintf(stderr, "Illegal value %s for %s at line %d in %s\n", val, var, lineno, name);
483                                 break;
484                         case POPT_ARG_INT:
485                                 intdata = (int *)long_options[i].arg;
486                                 *intdata = atoi(val);
487                                 break;
488                         case POPT_ARG_STRING:
489                                 stringdata = (char **)long_options[i].arg;
490                                 *stringdata = SMB_STRDUP(val);
491                                 break;
492                         default:
493                                 fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
494                                 break;
495                         }
496
497                         found = 1;
498                 }
499                 if(!found) {
500                         fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
501                 }
502         }
503
504         fclose(fd);
505         return 0;
506 }
507
508 int main(int argc, const char **argv)
509 {
510         int resume = 0, recursive = 0;
511         int c = 0;
512         int debuglevel = 0;
513         const char *file = NULL;
514         char *rcfile = NULL;
515         char *outputfile = NULL;
516         struct poptOption long_options[] = {
517                 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },        
518                 {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
519                 {"recursive", 'R',  POPT_ARG_NONE, &recursive, 0, "Recursively download files" },
520                 {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },
521                 {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },
522                 {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },
523                 {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" },
524                 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" },
525                 {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" },
526                 {"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" },
527                 {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" },
528                 {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" },
529                 {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
530                 {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },
531                 {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},
532                 {"rcfile", 'f', POPT_ARG_STRING, NULL, 0, "Use specified rc file"},
533                 POPT_AUTOHELP
534                 POPT_TABLEEND
535         };
536         poptContext pc;
537
538         /* only read rcfile if it exists */
539         asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME"));
540         if(access(rcfile, F_OK) == 0) readrcfile(rcfile, long_options);
541         free(rcfile);
542
543 #ifdef SIGWINCH
544         signal(SIGWINCH, change_columns);
545 #endif
546         signal(SIGINT, signal_quit);
547         signal(SIGTERM, signal_quit);
548
549         pc = poptGetContext(argv[0], argc, argv, long_options, 0);
550
551         while((c = poptGetNextOpt(pc)) >= 0) {
552                 switch(c) {
553                 case 'f':
554                         readrcfile(poptGetOptArg(pc), long_options);
555                         break;
556                 case 'a':
557                         username = ""; password = "";
558                         break;
559                 }
560         }
561
562         if((send_stdout || outputfile) && recursive) {
563                 fprintf(stderr, "The -o or -O and -R options can not be used together.\n");
564                 return 1;
565         }
566
567         if(outputfile && send_stdout) {
568                 fprintf(stderr, "The -o and -O options cannot be used together.\n");
569                 return 1;
570         }
571
572         if(smbc_init(get_auth_data, debuglevel) < 0) {
573                 fprintf(stderr, "Unable to initialize libsmbclient\n");
574                 return 1;
575         }
576
577         columns = get_num_cols();
578
579         total_start_time = time(NULL);
580
581         while((file = poptGetArg(pc))) {
582                 if(!recursive) return smb_download_file(file, "", recursive, resume, outputfile);
583                 else return smb_download_dir(file, "", resume);
584         }
585
586         clean_exit();
587
588         return 0;
589 }