From 5c16cccac8bee859b6159b877b2431a4caba4a9a Mon Sep 17 00:00:00 2001 From: Semyon Knyazev Date: Wed, 18 Dec 2024 01:14:13 +0400 Subject: [PATCH] Add smb context wrapper --- src/adldap/samba/smb_context.cpp | 59 ++++++++++++++++++++++++++++++++ src/adldap/samba/smb_context.h | 52 ++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/adldap/samba/smb_context.cpp create mode 100644 src/adldap/samba/smb_context.h diff --git a/src/adldap/samba/smb_context.cpp b/src/adldap/samba/smb_context.cpp new file mode 100644 index 00000000..77292ba8 --- /dev/null +++ b/src/adldap/samba/smb_context.cpp @@ -0,0 +1,59 @@ +/* + * ADMC - AD Management Center + * + * Copyright (C) 2020-2024 BaseALT Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "smb_context.h" + +#include + +SMBContext::SMBContext() : smb_ctx_ptr(createContext(), freeContext) { + if (is_valid()) { + smbc_set_context(smb_ctx_ptr.get()); + } +} + +bool SMBContext::is_valid() const { + return bool(smb_ctx_ptr); +} + +int SMBContext::smbcGetxattr(const char *fname, const char *name, const void *value, size_t size) { + return smbc_getFunctionGetxattr(smb_ctx_ptr.get())(smb_ctx_ptr.get(), fname, name, value, size); +} + +SMBCCTX *SMBContext::createContext() { + SMBCCTX* newContext = smbc_new_context(); + + if (newContext) { + // smbc_setDebug(newContext, SMB_DEBUG_LEVEL); + smbc_setOptionUseKerberos(newContext, true); + smbc_setOptionFallbackAfterKerberos(newContext, true); + + if (smbc_init_context(newContext) == nullptr) { + smbc_free_context(newContext, SMB_FREE_EVEN_IF_BUSY); + newContext = nullptr; + } + } + + return newContext; +} + +void SMBContext::freeContext(SMBCCTX *context) { + if (context) { + smbc_free_context(context, SMB_FREE_EVEN_IF_BUSY); + } +} diff --git a/src/adldap/samba/smb_context.h b/src/adldap/samba/smb_context.h new file mode 100644 index 00000000..566d0b38 --- /dev/null +++ b/src/adldap/samba/smb_context.h @@ -0,0 +1,52 @@ +/* + * ADMC - AD Management Center + * + * Copyright (C) 2020-2024 BaseALT Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef SMBCONTEXT_H +#define SMBCONTEXT_H + +#include + +typedef struct _SMBCCTX SMBCCTX; + +const int SMB_FREE_EVEN_IF_BUSY = 1; +const int SMB_DEBUG_LEVEL = 5; + +class SMBContext { +public: + SMBContext(); + ~SMBContext() = default; + + SMBContext(const SMBContext&) = delete; + SMBContext& operator=(const SMBContext&) = delete; + + SMBContext(SMBContext&&) = default; + SMBContext& operator=(SMBContext&&) = default; + + bool is_valid() const; + + int smbcGetxattr(const char *fname, const char *name, const void *value, size_t size); + +private: + SMBCCTX* createContext(); + static void freeContext(SMBCCTX* context); + + std::unique_ptr smb_ctx_ptr; +}; + +#endif // SMBCONTEXT_H