Skip to content

Commit

Permalink
Merge branch 'master' into Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolf3s committed Oct 12, 2024
2 parents 02783d6 + 69bc169 commit fe2106a
Show file tree
Hide file tree
Showing 56 changed files with 3,059 additions and 756 deletions.
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ pkgconfig_DATA = libsmb2.pc

EXTRA_DIST = \
libsmb2.pc.in

test: $(SUBDIRS)
cd tests; make test
10 changes: 6 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AC_INIT([libsmb2], [4.0.0], [[email protected]])
AC_INIT([libsmb2],[4.0.0],[[email protected]])

AC_PREREQ([2.58])
AC_PREREQ([2.71])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([-Wall foreign subdir-objects 1.11])
AC_CANONICAL_HOST
Expand All @@ -11,7 +11,7 @@ m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
dnl Do not add default CFLAGS in AC_PROG_CC
: ${CFLAGS=""}
AC_PROG_CC
AC_PROG_LIBTOOL
LT_INIT

AM_PROG_CC_C_O

Expand Down Expand Up @@ -174,6 +174,8 @@ AC_CONFIG_FILES([
examples/Makefile
include/Makefile
lib/Makefile
tests/Makefile
])

AC_OUTPUT([libsmb2.pc])
AC_CONFIG_FILES([libsmb2.pc])
AC_OUTPUT
441 changes: 419 additions & 22 deletions examples/smb2-dcerpc-coder-test.c

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions examples/smb2-server-sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,10 @@ static int logoff_handler(struct smb2_server *srvr, struct smb2_context *smb2)

static int tree_connect_handler(struct smb2_server *srvr, struct smb2_context *smb2,
struct smb2_tree_connect_request *req,
struct smb2_tree_connect_reply *rep,
uint32_t *tree_id)
struct smb2_tree_connect_reply *rep)
{
rep->share_type = SMB2_SHARE_TYPE_DISK;
rep->maximal_access = 0x101f01ff;
*tree_id = 0x50625678;

if (req->path && req->path_length) {
int ei = (req->path_length / 2) - 4;
Expand Down Expand Up @@ -414,6 +412,7 @@ static int query_info_handler(struct smb2_server *srvr, struct smb2_context *smb
}

struct smb2_server_request_handlers test_handlers = {
NULL,
authorize_handler,
session_handler,
logoff_handler,
Expand All @@ -429,7 +428,9 @@ struct smb2_server_request_handlers test_handlers = {
cancel_handler,
echo_handler,
query_directory_handler,
NULL,
query_info_handler,
NULL
};

struct smb2_server server;
Expand Down Expand Up @@ -474,6 +475,9 @@ int main(int argc, char **argv)
memset(&server, 0, sizeof(server));
server.handlers = &test_handlers;

server.signing_enabled = 1;
server.allow_anonymous = 1;

server.port = strtoul(argv[1], NULL, 0);

err = smb2_serve_port(&server, 1, on_new_client, (void *)&smb2);
Expand Down
82 changes: 57 additions & 25 deletions examples/smb2-share-enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "smb2.h"
#include "libsmb2.h"
Expand All @@ -38,11 +39,12 @@ int poll(struct pollfd *fds, unsigned int nfds, int timo);
#endif

int is_finished;
int level;

int usage(void)
{
fprintf(stderr, "Usage:\n"
"smb2-share-enum <smb2-url>\n\n"
"smb2-share-enum [-l level] <smb2-url>\n\n"
"URL format: "
"smb://[<domain;][<username>@]<host>[:<port>]/\n");
exit(1);
Expand All @@ -61,29 +63,39 @@ void se_cb(struct smb2_context *smb2, int status,
}

/* We always only use Level1 for netshare enum */
printf("Number of shares:%d\n", rep->ses.ShareInfo.Level1.EntriesRead);
for (i = 0; i < rep->ses.ShareInfo.Level1.EntriesRead; i++) {
printf("%-20s %-20s", rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].netname.utf8,
rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].remark.utf8);
if ((rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & 3) == SHARE_TYPE_DISKTREE) {
printf(" DISKTREE");
switch (level) {
case SHARE_INFO_0:
printf("Number of shares:%d\n", rep->ses.ShareInfo.Level0.EntriesRead);
for (i = 0; i < rep->ses.ShareInfo.Level0.EntriesRead; i++) {
printf("%-20s\n", rep->ses.ShareInfo.Level0.Buffer->share_info_0[i].netname.utf8);
}
if ((rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & 3) == SHARE_TYPE_PRINTQ) {
printf(" PRINTQ");
break;
case SHARE_INFO_1:
printf("Number of shares:%d\n", rep->ses.ShareInfo.Level1.EntriesRead);
for (i = 0; i < rep->ses.ShareInfo.Level1.EntriesRead; i++) {
printf("%-20s %-20s", rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].netname.utf8,
rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].remark.utf8);
if ((rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & 3) == SHARE_TYPE_DISKTREE) {
printf(" DISKTREE");
}
if ((rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & 3) == SHARE_TYPE_PRINTQ) {
printf(" PRINTQ");
}
if ((rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & 3) == SHARE_TYPE_DEVICE) {
printf(" DEVICE");
}
if ((rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & 3) == SHARE_TYPE_IPC) {
printf(" IPC");
}
if (rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & SHARE_TYPE_TEMPORARY) {
printf(" TEMPORARY");
}
if (rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & SHARE_TYPE_HIDDEN) {
printf(" HIDDEN");
}
printf("\n");
}
if ((rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & 3) == SHARE_TYPE_DEVICE) {
printf(" DEVICE");
}
if ((rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & 3) == SHARE_TYPE_IPC) {
printf(" IPC");
}
if (rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & SHARE_TYPE_TEMPORARY) {
printf(" TEMPORARY");
}
if (rep->ses.ShareInfo.Level1.Buffer->share_info_1[i].type & SHARE_TYPE_HIDDEN) {
printf(" HIDDEN");
}
printf("\n");
break;
}

smb2_free_data(smb2, rep);
Expand All @@ -96,8 +108,19 @@ int main(int argc, char *argv[])
struct smb2_context *smb2;
struct smb2_url *url;
struct pollfd pfd;
int opt;

while ((opt = getopt(argc, argv, "l:")) != -1) {
switch (opt) {
case 'l':
level = atoi(optarg);
break;
default: /* '?' */
usage();
}
}

if (argc < 2) {
if (optind >= argc) {
usage();
}

Expand All @@ -107,7 +130,16 @@ int main(int argc, char *argv[])
exit(0);
}

url = smb2_parse_url(smb2, argv[1]);
switch (level) {
case SHARE_INFO_0:
case SHARE_INFO_1:
break;
default:
fprintf(stderr, "level must be 0/1\n");
exit(0);
}

url = smb2_parse_url(smb2, argv[optind]);
if (url == NULL) {
fprintf(stderr, "Failed to parse url: %s\n",
smb2_get_error(smb2));
Expand All @@ -125,7 +157,7 @@ int main(int argc, char *argv[])
exit(10);
}

if (smb2_share_enum_async(smb2, se_cb, NULL) != 0) {
if (smb2_share_enum_async(smb2, level, se_cb, NULL) != 0) {
printf("smb2_share_enum failed. %s\n", smb2_get_error(smb2));
exit(10);
}
Expand Down
2 changes: 1 addition & 1 deletion include/apple/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/* #undef HAVE_LIBSOCKET */

/* Whether we have linger */
/* #undef HAVE_LINGER */
#define HAVE_LINGER 1

/* Define to 1 if you have the <netdb.h> header file. */
#define HAVE_NETDB_H 1
Expand Down
39 changes: 38 additions & 1 deletion include/libsmb2-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,20 @@ enum smb2_sec {
SMB2_SEC_KRB5,
};

/* current tree id stack, note: index 0 in the stack is not used
*/
#define SMB2_MAX_TREE_NESTING 32
#define smb2_tree_id(smb2) (((smb2)->tree_id_cur >= 0)?smb2->tree_id[(smb2)->tree_id_cur]:0xdeadbeef)

#define MAX_CREDITS 1024
#define SMB2_SALT_SIZE 32

struct sync_cb_data {
int is_finished;
int status;
void *ptr;
};

struct smb2_context {

t_socket fd;
Expand Down Expand Up @@ -147,12 +158,15 @@ struct smb2_context {
smb2_error_cb error_cb;
smb2_command_cb connect_cb;
void *connect_data;
struct sync_cb_data connect_cb_data;

int credits;

char client_guid[16];

uint32_t tree_id;
uint32_t tree_id[SMB2_MAX_TREE_NESTING];
int tree_id_top;
int tree_id_cur;
uint64_t message_id;
uint64_t session_id;
uint8_t *session_key;
Expand Down Expand Up @@ -202,6 +216,18 @@ struct smb2_context {
* (if this context is a server)
*/
struct smb2_pdu *next_pdu;

/* flag indicated command packers/unpackers can pass "extra"
* content without trying to decode or encode it. this is
* useful for proxies and applies only to the commands with
* complex data: query-info, query-directory, ioctl, and
* create (contexts). the command fixed part is always
* de/en-coded regardless of this setting
*/
int passthrough;

/* last file_id in a create-reply, for "related requests" */
smb2_file_id last_file_id;

/* Server capabilities */
uint8_t supports_multi_credit;
Expand Down Expand Up @@ -288,6 +314,9 @@ struct smb2_iovec *smb2_add_iovector(struct smb2_context *smb2,

int smb2_pad_to_64bit(struct smb2_context *smb2, struct smb2_io_vectors *v);

int smb2_connect_tree_id(struct smb2_context *smb2, uint32_t tree_id);
int smb2_disconnect_tree_id(struct smb2_context *smb2, uint32_t tree_id);

struct smb2_pdu *smb2_allocate_pdu(struct smb2_context *smb2,
enum smb2_command command,
smb2_command_cb cb, void *cb_data);
Expand Down Expand Up @@ -357,6 +386,12 @@ int smb2_process_query_directory_request_fixed(struct smb2_context *smb2,
struct smb2_pdu *pdu);
int smb2_process_query_directory_request_variable(struct smb2_context *smb2,
struct smb2_pdu *pdu);
int smb2_process_change_notify_fixed(struct smb2_context *smb2,
struct smb2_pdu *pdu);
int smb2_process_change_notify_variable(struct smb2_context *smb2,
struct smb2_pdu *pdu);
int smb2_process_change_notify_request_fixed(struct smb2_context *smb2,
struct smb2_pdu *pdu);
int smb2_process_query_info_fixed(struct smb2_context *smb2,
struct smb2_pdu *pdu);
int smb2_process_query_info_variable(struct smb2_context *smb2,
Expand Down Expand Up @@ -399,6 +434,8 @@ int smb2_process_flush_request_fixed(struct smb2_context *smb2,
struct smb2_pdu *pdu);
int smb2_process_read_fixed(struct smb2_context *smb2,
struct smb2_pdu *pdu);
int smb2_process_read_variable(struct smb2_context *smb2,
struct smb2_pdu *pdu);
int smb2_process_read_request_fixed(struct smb2_context *smb2,
struct smb2_pdu *pdu);
int smb2_process_read_request_variable(struct smb2_context *smb2,
Expand Down
21 changes: 21 additions & 0 deletions include/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module SMB2 [system] [extern_c] {
module LibSMB2 {
header "apple/config.h"
header "smb2/smb2-errors.h"
header "smb2/smb2.h"
header "smb2/libsmb2.h"
header "smb2/libsmb2-dcerpc.h"
header "smb2/libsmb2-dcerpc-lsa.h"
header "smb2/libsmb2-dcerpc-srvsvc.h"
}

module Internal {
header "libsmb2-private.h"
}

explicit module Raw {
header "smb2/libsmb2-raw.h"
}

export SMB2
}
38 changes: 34 additions & 4 deletions include/smb2/libsmb2-dcerpc-srvsvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ struct dcerpc_pdu;
#define SHARE_TYPE_TEMPORARY 0x40000000
#define SHARE_TYPE_HIDDEN 0x80000000

enum SHARE_INFO_enum {
SHARE_INFO_0 = 0,
SHARE_INFO_1 = 1,
};

struct srvsvc_SHARE_INFO_0 {
struct dcerpc_utf16 netname;
};
int srvsvc_SHARE_INFO_0_coder(struct dcerpc_context *ctx,
struct dcerpc_pdu *pdu,
struct smb2_iovec *iov, int *offset,
void *ptr);

struct srvsvc_SHARE_INFO_0_carray {
uint32_t max_count;
struct srvsvc_SHARE_INFO_0 *share_info_0;
};

struct srvsvc_SHARE_INFO_0_CONTAINER {
uint32_t EntriesRead;
struct srvsvc_SHARE_INFO_0_carray *Buffer;
};

struct srvsvc_SHARE_INFO_1 {
struct dcerpc_utf16 netname;
uint32_t type;
Expand All @@ -52,17 +75,24 @@ int srvsvc_SHARE_INFO_1_coder(struct dcerpc_context *ctx,
void *ptr);

struct srvsvc_SHARE_INFO_1_carray {
uint32_t max_count; /* filled in by caller before decode */
struct srvsvc_SHARE_INFO_1 share_info_1[];
uint32_t max_count;
struct srvsvc_SHARE_INFO_1 *share_info_1;
};

struct srvsvc_SHARE_INFO_1_CONTAINER {
uint32_t EntriesRead;
struct srvsvc_SHARE_INFO_1_carray *Buffer;
};

int srvsvc_SHARE_INFO_1_CONTAINER_coder(struct dcerpc_context *dce,
struct dcerpc_pdu *pdu,
struct smb2_iovec *iov, int *offset,
void *ptr);

struct srvsvc_SHARE_ENUM_UNION {
uint32_t level;
uint32_t Level;
union {
struct srvsvc_SHARE_INFO_0_CONTAINER Level0;
struct srvsvc_SHARE_INFO_1_CONTAINER Level1;
};
};
Expand Down Expand Up @@ -124,7 +154,7 @@ struct srvsvc_rep {
* This pointer must be freed using smb2_free_data().
* -errno : An error occurred.
*/
int smb2_share_enum_async(struct smb2_context *smb2,
int smb2_share_enum_async(struct smb2_context *smb2, enum SHARE_INFO_enum level,
smb2_command_cb cb, void *cb_data);

int srvsvc_NetrShareEnum_rep_coder(struct dcerpc_context *dce,
Expand Down
Loading

0 comments on commit fe2106a

Please sign in to comment.