Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
bdodge committed Dec 21, 2024
2 parents 3c34241 + 2a5e83a commit 05dc512
Show file tree
Hide file tree
Showing 21 changed files with 386 additions and 49 deletions.
23 changes: 21 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,23 @@ endif()
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
endif()
option(ENABLE_EXAMPLES "Build example programs" OFF)

option(ENABLE_LIBKRB5 "Enable libkrb5 support" ON)
option(ENABLE_GSSAPI "Enable gssapi support" ON)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
endif()

if(CMAKE_SYSTEM_NAME MATCHES Linux)
find_package(LibKrb5)
if (ENABLE_LIBKRB5)
find_package(LibKrb5)
endif()
elseif(IOS)
<<<<<<< HEAD
find_package(GSSAPI)
=======
if (ENABLE_GSSAPI)
find_package(GSSAPI)
endif()
>>>>>>> upstream/master
endif()

if(NOT ESP_PLATFORM)
Expand Down Expand Up @@ -95,9 +104,19 @@ endif()
endif()

if(CMAKE_SYSTEM_NAME MATCHES Linux)
<<<<<<< HEAD
set(core_DEPENDS ${LIBKRB5_LIBRARY} CACHE STRING "" FORCE)
elseif(IOS)
set(core_DEPENDS ${GSSAPI_LIBRARIES} CACHE STRING "" FORCE)
=======
if (ENABLE_LIBKRB5)
set(core_DEPENDS ${LIBKRB5_LIBRARY} CACHE STRING "" FORCE)
endif()
elseif(IOS)
if (ENABLE_GSSAPI)
set(core_DEPENDS ${GSSAPI_LIBRARIES} CACHE STRING "" FORCE)
endif()
>>>>>>> upstream/master
endif()

if(MSVC AND BUILD_SHARED_LIBS)
Expand Down
4 changes: 4 additions & 0 deletions cmake/ConfigureChecks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ endif()
check_include_file("arpa/inet.h" HAVE_ARPA_INET_H)
check_include_file("dlfcn.h" HAVE_DLFCN_H)
check_include_file("fcntl.h" HAVE_FCNTL_H)
if (ENABLE_GSSAPI)
check_include_file("gssapi/gssapi.h" HAVE_GSSAPI_GSSAPI_H)
endif()
if (ENABLE_LIBKRB5)
check_include_file("krb5/krb5.h" HAVE_LIBKRB5)
endif()
check_include_file("inttypes.h" HAVE_INTTYPES_H)
check_include_file("netdb.h" HAVE_NETDB_H)
check_include_file("netinet/in.h" HAVE_NETINET_IN_H)
Expand Down
8 changes: 2 additions & 6 deletions include/libsmb2-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ extern "C" {
#endif

#ifdef HAVE_LIBKRB5
#if __APPLE__
#import <GSS/GSS.h>
#else
#include <gssapi/gssapi.h>
#endif
#include "krb5-wrapper.h"
#endif

#define MIN(a,b) (((a)<(b))?(a):(b))
Expand Down Expand Up @@ -338,7 +334,7 @@ struct smb2dir {
int index;
};


#define smb2_is_server(ctx) ((ctx)->owning_server != NULL)

void smb2_set_nterror(struct smb2_context *smb2, int nterror,
Expand Down
3 changes: 1 addition & 2 deletions include/smb2/libsmb2.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern "C" {
#endif

#define LIBSMB2_SHARE_ENUM_V2 1

struct smb2_iovec {
uint8_t *buf;
size_t len;
Expand Down Expand Up @@ -1325,7 +1325,6 @@ struct smb2_server {
uint32_t max_write_size;
int signing_enabled;
int allow_anonymous;
int use_krb5_for_ntlmssp;
/* this can be set non-0 to delegate client authentication to
* another client and allow any authentication to this server */
int proxy_authentication;
Expand Down
2 changes: 1 addition & 1 deletion include/smb2/smb2-errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

/* Error codes */
#define SMB2_STATUS_SUCCESS 0x00000000
#define SMB2_STATUS_ABORTED 0xffffffff
#define SMB2_STATUS_SHUTDOWN 0xffffffff
#define SMB2_STATUS_PENDING 0x00000103
#define SMB2_STATUS_SMB_BAD_FID 0x00060001
#define SMB2_STATUS_NO_MORE_FILES 0x80000006
Expand Down
1 change: 1 addition & 0 deletions lib/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ int iop_connect(int sockfd, struct sockaddr *addr, socklen_t addrlen)
#ifdef __ANDROID__
/* getlogin_r() was added in API 28 */
#if __ANDROID_API__ < 28
#include <errno.h>
#define NEED_GETLOGIN_R
#define login_num ENXIO
#endif
Expand Down
8 changes: 4 additions & 4 deletions lib/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const char *nterror_to_str(uint32_t status) {
switch (status) {
case SMB2_STATUS_SUCCESS:
return "STATUS_SUCCESS";
case SMB2_STATUS_ABORTED:
return "STATUS_ABORTED";
case SMB2_STATUS_SHUTDOWN:
return "STATUS_SHUTDOWN";
case SMB2_STATUS_PENDING:
return "STATUS_PENDING";
case SMB2_STATUS_NO_MORE_FILES:
Expand Down Expand Up @@ -1072,8 +1072,8 @@ int nterror_to_errno(uint32_t status) {
return 0;
case SMB2_STATUS_PENDING:
return EAGAIN;
case SMB2_STATUS_ABORTED:
return ECONNRESET;
case SMB2_STATUS_SHUTDOWN:
return -SMB2_STATUS_SHUTDOWN;
case SMB2_STATUS_NO_SUCH_FILE:
case SMB2_STATUS_NO_SUCH_DEVICE:
case SMB2_STATUS_BAD_NETWORK_NAME:
Expand Down
11 changes: 7 additions & 4 deletions lib/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ void smb2_destroy_context(struct smb2_context *smb2)

smb2->outqueue = pdu->next;
if (pdu->cb) {
pdu->cb(smb2, SMB2_STATUS_CANCELLED, NULL, pdu->cb_data);
pdu->cb(smb2, SMB2_STATUS_SHUTDOWN, NULL, pdu->cb_data);
}
smb2_free_pdu(smb2, pdu);
}
if (smb2->pdu) {
struct smb2_pdu *pdu = smb2->pdu;

if (pdu->cb) {
pdu->cb(smb2, SMB2_STATUS_CANCELLED, NULL, pdu->cb_data);
pdu->cb(smb2, SMB2_STATUS_SHUTDOWN, NULL, pdu->cb_data);
}
smb2_free_pdu(smb2, smb2->pdu);
}
Expand All @@ -346,7 +346,7 @@ void smb2_destroy_context(struct smb2_context *smb2)

smb2->waitqueue = pdu->next;
if (pdu->cb) {
pdu->cb(smb2, SMB2_STATUS_CANCELLED, NULL, pdu->cb_data);
pdu->cb(smb2, SMB2_STATUS_SHUTDOWN, NULL, pdu->cb_data);
}
if (pdu == smb2->pdu) {
smb2->pdu = NULL;
Expand Down Expand Up @@ -552,7 +552,10 @@ void smb2_set_password_from_file(struct smb2_context *smb2)
#else
name = getenv("NTLM_USER_FILE");
#endif
if (name == NULL) {
if (name == NULL || smb2->user == NULL) {
#ifdef _MSC_UWP
free(name);
#endif
return;
}
fh = fopen(name, "r");
Expand Down
17 changes: 16 additions & 1 deletion lib/libsmb2.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ od_close_cb(struct smb2_context *smb2, int status,
struct smb2dir *dir = private_data;

if (status != SMB2_STATUS_SUCCESS) {
dir->cb(smb2, -ENOMEM, NULL, dir->cb_data);
dir->cb(smb2, -nterror_to_errno(status),
NULL, dir->cb_data);
free_smb2dir(smb2, dir);
return;
}
Expand Down Expand Up @@ -1064,12 +1065,22 @@ smb2_connect_share_async(struct smb2_context *smb2,
smb2_set_error(smb2, "No server name provided");
return -EINVAL;
}
if (share == NULL) {
smb2_set_error(smb2, "No share name provided");
return -EINVAL;
}
smb2->server = strdup(server);
if (smb2->server == NULL) {
return -ENOMEM;
}

if (smb2->share) {
free(discard_const(smb2->share));
}
smb2->share = strdup(share);
if (smb2->share == NULL) {
return -ENOMEM;
}

if (user) {
smb2_set_user(smb2, user);
Expand All @@ -1091,6 +1102,10 @@ smb2_connect_share_async(struct smb2_context *smb2,
smb2_set_error(smb2, "Failed to strdup(share)");
return -ENOMEM;
}
if (smb2->user == NULL) {
smb2_set_error(smb2, "smb2->user is NULL");
return -ENOMEM;
}
c_data->user = strdup(smb2->user);
if (c_data->user == NULL) {
free_c_data(smb2, c_data);
Expand Down
29 changes: 29 additions & 0 deletions lib/ntlmssp.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,32 @@ ntlmssp_init_context(const char *user,

if (user) {
auth_data->user = strdup(user);
if (auth_data->user == NULL) {
goto failed;
}
}
if (password) {
auth_data->password = strdup(password);
if (auth_data->password == NULL) {
goto failed;
}
}
if (domain) {
auth_data->domain = strdup(domain);
if (auth_data->domain == NULL) {
goto failed;
}
}
if (workstation) {
auth_data->workstation = strdup(workstation);
if (auth_data->workstation == NULL) {
goto failed;
}
}
auth_data->client_challenge = malloc(8);
if (auth_data->client_challenge == NULL) {
goto failed;
}
memcpy(auth_data->client_challenge, client_challenge, 8);
auth_data->is_authenticated = 0;
memset(auth_data->exported_session_key, 0, SMB2_KEY_SIZE);
Expand All @@ -171,6 +186,13 @@ ntlmssp_init_context(const char *user,
auth_data->wintime = smb2_timeval_to_win(&tv);

return auth_data;
failed:
free(auth_data->user);
free(auth_data->password);
free(auth_data->domain);
free(auth_data->workstation);
free(auth_data->client_challenge);
return NULL;
}

void
Expand Down Expand Up @@ -209,6 +231,9 @@ encoder(const void *buffer, size_t size, void *ptr)
free(tmp);
}

if (auth_data->buf == NULL) {
return -1;
}
memcpy(auth_data->buf + auth_data->len, buffer, size);
auth_data->len += size;

Expand Down Expand Up @@ -453,6 +478,10 @@ NTOWFv2(const char *user, const char *password, const char *domain,
struct smb2_utf16 *utf16_userdomain = NULL;
unsigned char ntlm_hash[16];

if (user == NULL || password == NULL) {
return -1;
}

/* ntlm:F638EDF864C4805DC65D9BF2BB77E4C0 */
if ((strlen(password) == 37) && (strncmp(password, "ntlm:", 5) == 0)) {
if (ntlm_convert_password_hash(password + 5, ntlm_hash) < 0) {
Expand Down
6 changes: 6 additions & 0 deletions lib/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ static void opendir_cb(struct smb2_context *smb2, int status,
{
struct sync_cb_data *cb_data = private_data;

if (status == SMB2_STATUS_SHUTDOWN) {
return;
}
if (cb_data->status == SMB2_STATUS_CANCELLED) {
return;
}
Expand Down Expand Up @@ -273,6 +276,9 @@ static void close_cb(struct smb2_context *smb2, int status,
{
struct sync_cb_data *cb_data = private_data;

if (status == SMB2_STATUS_SHUTDOWN) {
return;
}
if (cb_data->status == SMB2_STATUS_CANCELLED) {
free(cb_data);
return;
Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AM_CPPFLAGS = -I${srcdir}/../include -I${srcdir}/../include/smb2 \
AM_CFLAGS = $(WARN_CFLAGS)
LDADD = ../lib/libsmb2.la

noinst_PROGRAMS = prog_mkdir prog_rmdir prog_cat \
noinst_PROGRAMS = prog_ls prog_mkdir prog_rmdir prog_cat \
smb2-dcerpc-coder-test

EXTRA_PROGRAMS = ld_sockerr
Expand Down
Loading

0 comments on commit 05dc512

Please sign in to comment.