Skip to content

Instantly share code, notes, and snippets.

@tumdum
Created December 13, 2014 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tumdum/43461abcb148ea11fcfc to your computer and use it in GitHub Desktop.
Save tumdum/43461abcb148ea11fcfc to your computer and use it in GitHub Desktop.
how to use smbclient
#include <samba-4.0/libsmbclient.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
void die(const char* format, ...)
{
va_list vl;
va_start(vl, format);
vfprintf(stderr, format, vl);
va_end(vl);
exit(1);
}
void auth_fn(const char *srv, const char *shr, char *wg,
int wglen, char *un, int unlen, char *pw, int pwlen)
{
fprintf(stderr, "srv: '%s'\n", srv);
fprintf(stderr, "shr: '%s'\n", shr);
fprintf(stderr, "wg: '%s', wglen: %d\n", wg, wglen);
fprintf(stderr, "un: '%s', unlen: %d\n", un, unlen);
fprintf(stderr, "pwlen: %d\n", pwlen);
char* pass = getenv("SMB_PASS");
if (!pass)
{
die("SMB_PASS not set, unknown samba password\n");
}
strncpy(pw, pass, strlen(pass));
fprintf(stderr, "pw: '%s'\n", pw);
}
int interesting_dirent(struct smbc_dirent* dirent)
{
return (dirent->smbc_type == 7 || dirent->smbc_type == 8 ||
dirent->smbc_type == 9);
}
int is_file(struct smbc_dirent* d)
{
return d->smbc_type == 8;
}
void usage(const char* app)
{
fprintf(stderr, "usage: %s smb_share\n", app);
exit(1);
}
int main(int argc, char** argv)
{
if (argc != 2)
{
usage(argv[0]);
}
int e = smbc_init(&auth_fn, 1);
if (e)
{
die("smbc_init failed with code: %d\n", e);
}
int dir_fd = smbc_opendir(argv[1]);
fprintf(stderr, "dir_fd: %d\n", dir_fd);
if (dir_fd < 0)
{
die("smbc_opendir failed: %s\n", strerror(errno));
}
const int size = 10000;
char dirsbuf[size];
e = smbc_getdents(dir_fd, (struct smbc_dirent*) dirsbuf, size);
if (e == 0 || e < size)
{
fprintf(stderr, "No more dirents\n");
}
else if (e < 0)
{
die("smbc_getdents failed: %s\n", strerror(errno));
}
char* current = dirsbuf;
for (int i = 0; i < e; ++i)
{
struct smbc_dirent* d = (struct smbc_dirent*) current;
if (interesting_dirent(d))
{
char* url = calloc(1, strlen(argv[1]) + strlen(d->name) + 1);
strcat(url, argv[1]);
strcat(url, d->name);
fprintf(stderr, "%s\t", url);
struct stat fs;
int e = smbc_stat(url, &fs);
free(url);
if (e < 0)
{
die("smbc_stat: failed for '%s'\n", url);
}
fprintf(stderr, " size: %ld\n", fs.st_size);
}
current += d->dirlen;
}
smbc_closedir(dir_fd);
return e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment