00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "includes.h"
00021 #include "libsmbclient.h"
00022
00023 #if _FILE_OFFSET_BITS==64
00024 #define OFF_T_FORMAT "%lld"
00025 #define OFF_T_FORMAT_CAST long long
00026 #else
00027 #define OFF_T_FORMAT "%ld"
00028 #define OFF_T_FORMAT_CAST long
00029 #endif
00030
00031 int columns = 0;
00032
00033 static int _resume, _recursive, debuglevel;
00034 static char *outputfile;
00035
00036
00037 time_t total_start_time = 0;
00038 off_t total_bytes = 0;
00039
00040 #define SMB_MAXPATHLEN MAXPATHLEN
00041
00042
00043 #define RESUME_CHECK_SIZE 512
00044 #define RESUME_DOWNLOAD_OFFSET 1024
00045 #define RESUME_CHECK_OFFSET RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE
00046
00047 #define SMB_DEFAULT_BLOCKSIZE 64000
00048
00049 const char *username = NULL, *password = NULL, *workgroup = NULL;
00050 int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0;
00051 int blocksize = SMB_DEFAULT_BLOCKSIZE;
00052
00053 static int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile);
00054
00055 static int get_num_cols(void)
00056 {
00057 #ifdef TIOCGWINSZ
00058 struct winsize ws;
00059 if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
00060 return 0;
00061 }
00062 return ws.ws_col;
00063 #else
00064 #warning No support for TIOCGWINSZ
00065 char *cols = getenv("COLUMNS");
00066 if(!cols) return 0;
00067 return atoi(cols);
00068 #endif
00069 }
00070
00071 static void change_columns(int sig)
00072 {
00073 columns = get_num_cols();
00074 }
00075
00076 static void human_readable(off_t s, char *buffer, int l)
00077 {
00078 if(s > 1024 * 1024 * 1024) snprintf(buffer, l, "%.2fGb", 1.0 * s / (1024 * 1024 * 1024));
00079 else if(s > 1024 * 1024) snprintf(buffer, l, "%.2fMb", 1.0 * s / (1024 * 1024));
00080 else if(s > 1024) snprintf(buffer, l, "%.2fkb", 1.0 * s / 1024);
00081 else snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s);
00082 }
00083
00084 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
00085 {
00086 static char hasasked = 0;
00087 char *wgtmp, *usertmp;
00088 char tmp[128];
00089
00090 if(hasasked) return;
00091 hasasked = 1;
00092
00093 if(!nonprompt && !username) {
00094 printf("Username for %s at %s [guest] ", shr, srv);
00095 fgets(tmp, sizeof(tmp), stdin);
00096 if(tmp[strlen(tmp)-1] == '\n')tmp[strlen(tmp)-1] = '\0';
00097 strncpy(un, tmp, unlen-1);
00098 } else if(username) strncpy(un, username, unlen-1);
00099
00100 if(!nonprompt && !password) {
00101 char *prompt, *pass;
00102 asprintf(&prompt, "Password for %s at %s: ", shr, srv);
00103 pass = getpass(prompt);
00104 free(prompt);
00105 strncpy(pw, pass, pwlen-1);
00106 } else if(password) strncpy(pw, password, pwlen-1);
00107
00108 if(workgroup)strncpy(wg, workgroup, wglen-1);
00109
00110 wgtmp = SMB_STRNDUP(wg, wglen);
00111 usertmp = SMB_STRNDUP(un, unlen);
00112 if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp);
00113 free(wgtmp); free(usertmp);
00114 }
00115
00116 static int smb_download_dir(const char *base, const char *name, int resume)
00117 {
00118 char path[SMB_MAXPATHLEN];
00119 int dirhandle;
00120 struct smbc_dirent *dirent;
00121 const char *relname = name;
00122 char *tmpname;
00123 struct stat remotestat;
00124 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
00125
00126
00127 dirhandle = smbc_opendir(path);
00128 if(dirhandle < 1) {
00129 if(errno == ENOTDIR) return smb_download_file(base, name, 1, resume, NULL);
00130 fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
00131 return 0;
00132 }
00133
00134 while(*relname == '/')relname++;
00135 mkdir(relname, 0755);
00136
00137 tmpname = SMB_STRDUP(name);
00138
00139 while((dirent = smbc_readdir(dirhandle))) {
00140 char *newname;
00141 if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
00142 asprintf(&newname, "%s/%s", tmpname, dirent->name);
00143 switch(dirent->smbc_type) {
00144 case SMBC_DIR:
00145 smb_download_dir(base, newname, resume);
00146 break;
00147
00148 case SMBC_WORKGROUP:
00149 smb_download_dir("smb://", dirent->name, resume);
00150 break;
00151
00152 case SMBC_SERVER:
00153 smb_download_dir("smb://", dirent->name, resume);
00154 break;
00155
00156 case SMBC_FILE:
00157 smb_download_file(base, newname, 1, resume, NULL);
00158 break;
00159
00160 case SMBC_FILE_SHARE:
00161 smb_download_dir(base, newname, resume);
00162 break;
00163
00164 case SMBC_PRINTER_SHARE:
00165 if(!quiet)printf("Ignoring printer share %s\n", dirent->name);
00166 break;
00167
00168 case SMBC_COMMS_SHARE:
00169 if(!quiet)printf("Ignoring comms share %s\n", dirent->name);
00170 break;
00171
00172 case SMBC_IPC_SHARE:
00173 if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name);
00174 break;
00175
00176 default:
00177 fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type);
00178 break;
00179 }
00180 free(newname);
00181 }
00182 free(tmpname);
00183
00184 if(keep_permissions) {
00185 if(smbc_fstat(dirhandle, &remotestat) < 0) {
00186 fprintf(stderr, "Unable to get stats on %s on remote server\n", path);
00187 smbc_closedir(dirhandle);
00188 return 0;
00189 }
00190
00191 if(chmod(relname, remotestat.st_mode) < 0) {
00192 fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname, remotestat.st_mode);
00193 smbc_closedir(dirhandle);
00194 return 0;
00195 }
00196 }
00197
00198 smbc_closedir(dirhandle);
00199 return 1;
00200 }
00201
00202 static char *print_time(long t)
00203 {
00204 static char buffer[100];
00205 int secs, mins, hours;
00206 if(t < -1) {
00207 strncpy(buffer, "Unknown", sizeof(buffer));
00208 return buffer;
00209 }
00210
00211 secs = (int)t % 60;
00212 mins = (int)t / 60 % 60;
00213 hours = (int)t / (60 * 60);
00214 snprintf(buffer, sizeof(buffer)-1, "%02d:%02d:%02d", hours, mins, secs);
00215 return buffer;
00216 }
00217
00218 static void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total)
00219 {
00220 double avg = 0.0;
00221 long eta = -1;
00222 double prcnt = 0.0;
00223 char hpos[20], htotal[20], havg[20];
00224 char *status, *filename;
00225 int len;
00226 if(now - start)avg = 1.0 * (pos - start_pos) / (now - start);
00227 eta = (total - pos) / avg;
00228 if(total)prcnt = 100.0 * pos / total;
00229
00230 human_readable(pos, hpos, sizeof(hpos));
00231 human_readable(total, htotal, sizeof(htotal));
00232 human_readable(avg, havg, sizeof(havg));
00233
00234 len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos, htotal, prcnt, havg, print_time(eta));
00235
00236 if(columns) {
00237 int required = strlen(name), available = columns - len - strlen("[] ");
00238 if(required > available) asprintf(&filename, "...%s", name + required - available + 3);
00239 else filename = SMB_STRNDUP(name, available);
00240 } else filename = SMB_STRDUP(name);
00241
00242 fprintf(stderr, "\r[%s] %s", filename, status);
00243
00244 free(filename); free(status);
00245 }
00246
00247 static int smb_download_file(const char *base, const char *name, int recursive, int resume, char *outfile) {
00248 int remotehandle, localhandle;
00249 time_t start_time = time(NULL);
00250 const char *newpath;
00251 char path[SMB_MAXPATHLEN];
00252 char checkbuf[2][RESUME_CHECK_SIZE];
00253 char *readbuf = NULL;
00254 off_t offset_download = 0, offset_check = 0, curpos = 0, start_offset = 0;
00255 struct stat localstat, remotestat;
00256
00257 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (*base && *name && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
00258
00259 remotehandle = smbc_open(path, O_RDONLY, 0755);
00260
00261 if(remotehandle < 0) {
00262 switch(errno) {
00263 case EISDIR:
00264 if(!recursive) {
00265 fprintf(stderr, "%s is a directory. Specify -R to download recursively\n", path);
00266 return 0;
00267 }
00268 smb_download_dir(base, name, resume);
00269 return 0;
00270
00271 case ENOENT:
00272 fprintf(stderr, "%s can't be found on the remote server\n", path);
00273 return 0;
00274
00275 case ENOMEM:
00276 fprintf(stderr, "Not enough memory\n");
00277 exit(1);
00278 return 0;
00279
00280 case ENODEV:
00281 fprintf(stderr, "The share name used in %s does not exist\n", path);
00282 return 0;
00283
00284 case EACCES:
00285 fprintf(stderr, "You don't have enough permissions to access %s\n", path);
00286 return 0;
00287
00288 default:
00289 perror("smbc_open");
00290 return 0;
00291 }
00292 }
00293
00294 if(smbc_fstat(remotehandle, &remotestat) < 0) {
00295 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
00296 return 0;
00297 }
00298
00299 if(outfile) newpath = outfile;
00300 else if(!name[0]) {
00301 newpath = strrchr(base, '/');
00302 if(newpath)newpath++; else newpath = base;
00303 } else newpath = name;
00304
00305 if(newpath[0] == '/')newpath++;
00306
00307
00308 if(!send_stdout) {
00309 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755);
00310 if(localhandle < 0) {
00311 fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno));
00312 smbc_close(remotehandle);
00313 return 0;
00314 }
00315
00316 fstat(localhandle, &localstat);
00317
00318 start_offset = localstat.st_size;
00319
00320 if(localstat.st_size && localstat.st_size == remotestat.st_size) {
00321 if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path);
00322 else if(!quiet)fprintf(stderr, "%s\n", path);
00323 smbc_close(remotehandle);
00324 close(localhandle);
00325 return 1;
00326 }
00327
00328 if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) {
00329 offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET;
00330 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
00331 if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n"
00332 "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n",
00333 newpath, (OFF_T_FORMAT_CAST)offset_check,
00334 (OFF_T_FORMAT_CAST)localstat.st_size,
00335 (OFF_T_FORMAT_CAST)remotestat.st_size);
00336 }
00337
00338 if(offset_check) {
00339 off_t off1, off2;
00340
00341 off1 = lseek(localhandle, offset_check, SEEK_SET);
00342 if(off1 < 0) {
00343 fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n",
00344 (OFF_T_FORMAT_CAST)offset_check, newpath);
00345 smbc_close(remotehandle); close(localhandle);
00346 return 0;
00347 }
00348
00349 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
00350 if(off2 < 0) {
00351 fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n",
00352 (OFF_T_FORMAT_CAST)offset_check, newpath);
00353 smbc_close(remotehandle); close(localhandle);
00354 return 0;
00355 }
00356
00357 if(off1 != off2) {
00358 fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n",
00359 (OFF_T_FORMAT_CAST)off1,
00360 (OFF_T_FORMAT_CAST)off2);
00361 return 0;
00362 }
00363
00364 if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
00365 fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path);
00366 smbc_close(remotehandle); close(localhandle);
00367 return 0;
00368 }
00369
00370 if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
00371 fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name);
00372 smbc_close(remotehandle); close(localhandle);
00373 return 0;
00374 }
00375
00376 if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) {
00377 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);
00378 } else {
00379 fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path);
00380 smbc_close(remotehandle); close(localhandle);
00381 return 0;
00382 }
00383 }
00384 } else {
00385 localhandle = STDOUT_FILENO;
00386 start_offset = 0;
00387 offset_download = 0;
00388 offset_check = 0;
00389 }
00390
00391 readbuf = (char *)SMB_MALLOC(blocksize);
00392
00393
00394 for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) {
00395 ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
00396 if(bytesread < 0) {
00397 fprintf(stderr, "Can't read %u bytes at offset "OFF_T_FORMAT", file %s\n", (unsigned int)blocksize, (OFF_T_FORMAT_CAST)curpos, path);
00398 smbc_close(remotehandle);
00399 if (localhandle != STDOUT_FILENO) close(localhandle);
00400 free(readbuf);
00401 return 0;
00402 }
00403
00404 total_bytes += bytesread;
00405
00406 if(write(localhandle, readbuf, bytesread) < 0) {
00407 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);
00408 free(readbuf);
00409 smbc_close(remotehandle);
00410 if (localhandle != STDOUT_FILENO) close(localhandle);
00411 return 0;
00412 }
00413
00414 if(dots)fputc('.', stderr);
00415 else if(!quiet) {
00416 print_progress(newpath, start_time, time(NULL), start_offset, curpos, remotestat.st_size);
00417 }
00418 }
00419
00420 free(readbuf);
00421
00422 if(dots){
00423 fputc('\n', stderr);
00424 printf("%s downloaded\n", path);
00425 } else if(!quiet) {
00426 int i;
00427 fprintf(stderr, "\r%s", path);
00428 if(columns) {
00429 for(i = strlen(path); i < columns; i++) {
00430 fputc(' ', stderr);
00431 }
00432 }
00433 fputc('\n', stderr);
00434 }
00435
00436 if(keep_permissions && !send_stdout) {
00437 if(fchmod(localhandle, remotestat.st_mode) < 0) {
00438 fprintf(stderr, "Unable to change mode of local file %s to %o\n", path, remotestat.st_mode);
00439 smbc_close(remotehandle);
00440 close(localhandle);
00441 return 0;
00442 }
00443 }
00444
00445 smbc_close(remotehandle);
00446 if (localhandle != STDOUT_FILENO) close(localhandle);
00447 return 1;
00448 }
00449
00450 static void clean_exit(void)
00451 {
00452 char bs[100];
00453 human_readable(total_bytes, bs, sizeof(bs));
00454 if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs, time(NULL) - total_start_time);
00455 exit(0);
00456 }
00457
00458 static void signal_quit(int v)
00459 {
00460 clean_exit();
00461 }
00462
00463 static int readrcfile(const char *name, const struct poptOption long_options[])
00464 {
00465 FILE *fd = fopen(name, "r");
00466 int lineno = 0, i;
00467 char var[101], val[101];
00468 char found;
00469 int *intdata; char **stringdata;
00470 if(!fd) {
00471 fprintf(stderr, "Can't open RC file %s\n", name);
00472 return 1;
00473 }
00474
00475 while(!feof(fd)) {
00476 lineno++;
00477 if(fscanf(fd, "%100s %100s\n", var, val) < 2) {
00478 fprintf(stderr, "Can't parse line %d of %s, ignoring.\n", lineno, name);
00479 continue;
00480 }
00481
00482 found = 0;
00483
00484 for(i = 0; long_options[i].shortName; i++) {
00485 if(!long_options[i].longName)continue;
00486 if(strcmp(long_options[i].longName, var)) continue;
00487 if(!long_options[i].arg)continue;
00488
00489 switch(long_options[i].argInfo) {
00490 case POPT_ARG_NONE:
00491 intdata = (int *)long_options[i].arg;
00492 if(!strcmp(val, "on")) *intdata = 1;
00493 else if(!strcmp(val, "off")) *intdata = 0;
00494 else fprintf(stderr, "Illegal value %s for %s at line %d in %s\n", val, var, lineno, name);
00495 break;
00496 case POPT_ARG_INT:
00497 intdata = (int *)long_options[i].arg;
00498 *intdata = atoi(val);
00499 break;
00500 case POPT_ARG_STRING:
00501 stringdata = (char **)long_options[i].arg;
00502 *stringdata = SMB_STRDUP(val);
00503 break;
00504 default:
00505 fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
00506 break;
00507 }
00508
00509 found = 1;
00510 }
00511 if(!found) {
00512 fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
00513 }
00514 }
00515
00516 fclose(fd);
00517 return 0;
00518 }
00519
00520 int main(int argc, const char **argv)
00521 {
00522 int c = 0;
00523 const char *file = NULL;
00524 char *rcfile = NULL;
00525 struct poptOption long_options[] = {
00526 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
00527 {"resume", 'r', POPT_ARG_NONE, &_resume, 0, "Automatically resume aborted files" },
00528 {"recursive", 'R', POPT_ARG_NONE, &_recursive, 0, "Recursively download files" },
00529 {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },
00530 {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },
00531 {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },
00532 {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" },
00533 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" },
00534 {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" },
00535 {"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" },
00536 {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" },
00537 {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" },
00538 {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
00539 {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },
00540 {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},
00541 {"rcfile", 'f', POPT_ARG_STRING, NULL, 0, "Use specified rc file"},
00542 POPT_AUTOHELP
00543 POPT_TABLEEND
00544 };
00545 poptContext pc;
00546
00547 load_case_tables();
00548
00549
00550 asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME"));
00551 if(access(rcfile, F_OK) == 0)
00552 readrcfile(rcfile, long_options);
00553 free(rcfile);
00554
00555 #ifdef SIGWINCH
00556 signal(SIGWINCH, change_columns);
00557 #endif
00558 signal(SIGINT, signal_quit);
00559 signal(SIGTERM, signal_quit);
00560
00561 pc = poptGetContext(argv[0], argc, argv, long_options, 0);
00562
00563 while((c = poptGetNextOpt(pc)) >= 0) {
00564 switch(c) {
00565 case 'f':
00566 readrcfile(poptGetOptArg(pc), long_options);
00567 break;
00568 case 'a':
00569 username = ""; password = "";
00570 break;
00571 }
00572 }
00573
00574 if((send_stdout || outputfile) && _recursive) {
00575 fprintf(stderr, "The -o or -O and -R options can not be used together.\n");
00576 return 1;
00577 }
00578
00579 if(outputfile && send_stdout) {
00580 fprintf(stderr, "The -o and -O options cannot be used together.\n");
00581 return 1;
00582 }
00583
00584 if(smbc_init(get_auth_data, debuglevel) < 0) {
00585 fprintf(stderr, "Unable to initialize libsmbclient\n");
00586 return 1;
00587 }
00588
00589 columns = get_num_cols();
00590
00591 total_start_time = time(NULL);
00592
00593 while ( (file = poptGetArg(pc)) ) {
00594 if (!_recursive)
00595 return smb_download_file(file, "", _recursive, _resume, outputfile);
00596 else
00597 return smb_download_dir(file, "", _resume);
00598 }
00599
00600 clean_exit();
00601
00602 return 0;
00603 }