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