Commit 77770b0e authored by jan.koester's avatar jan.koester
Browse files

bugfix

parent aa87ff13
Loading
Loading
Loading
Loading
+1 −0
Changes for CMakeLists.txt: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ add_executable(sharedd
    src/main.cpp
    src/config.cpp
    src/smb.cpp
    src/adquery.cpp
    src/nfs.cpp
    src/rpc.cpp
    src/files.cpp

src/adquery.cpp

0 → 100644
+113 −0
Changes for src/adquery.cpp: 113 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
Copyright (c) 2026, Jan Koester jan.koester@gmx.net
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include "adquery.h"

#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>

namespace sharedd {

// one request to adconnectd, its whole answer; false if it couldn't be
// asked (not running, no answer within timeout seconds)
bool adconnectQuery(const std::string &line, std::string &reply, int timeout) {
    int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
    if (fd < 0) return false;
    sockaddr_un a{};
    a.sun_family = AF_UNIX;
    snprintf(a.sun_path, sizeof a.sun_path, "%s", SHAREDD_ADCONNECT_SOCKET);
    struct timeval tv = {timeout, 0};
    setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
    setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
    if (connect(fd, (sockaddr *)&a, sizeof a) != 0) {
        close(fd);
        return false;
    }
    std::string req = line + "\n";
    bool ok = ::send(fd, req.data(), req.size(), MSG_NOSIGNAL) == (ssize_t)req.size();
    reply.clear();
    char buf[4096];
    ssize_t n = 0;
    while (ok && (n = recv(fd, buf, sizeof buf, 0)) > 0) reply.append(buf, (size_t)n);
    close(fd);
    return ok && n == 0;
}

// the tab-separated fields of the first record of an adconnectd answer
std::vector<std::string> firstRecord(const std::string &reply) {
    std::vector<std::string> f;
    std::string line = reply.substr(0, reply.find('\n'));
    size_t p = 0;
    for (size_t q; (q = line.find('\t', p)) != std::string::npos; p = q + 1) f.push_back(line.substr(p, q - p));
    f.push_back(line.substr(p));
    return f;
}

// a binary SID (revision 1, at most 15 sub-authorities) as S-1-...; "" if malformed
std::string sidText(const unsigned char *b, size_t size) {
    if (size < 8 || b[0] != 1 || b[1] > 15 || size < 8 + 4 * (size_t)b[1]) return std::string();
    unsigned long long auth = 0;
    for (int i = 2; i < 8; i++) auth = auth << 8 | b[i];
    std::string s = "S-1-" + std::to_string(auth);
    for (int i = 0; i < b[1]; i++) {
        const unsigned char *p = b + 8 + 4 * i;
        s += "-" + std::to_string((uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24);
    }
    return s;
}

// S-1-... into out[SidSize]; false if malformed
bool sidBinary(const std::string &t, unsigned char out[SidBinSize]) {
    if (t.compare(0, 4, "S-1-") != 0) return false;
    std::vector<unsigned long long> parts;
    for (size_t p = 4;;) {
        size_t q = t.find('-', p);
        std::string part = t.substr(p, q == std::string::npos ? std::string::npos : q - p);
        if (part.empty() || part.size() > 15 || part.find_first_not_of("0123456789") != std::string::npos) return false;
        parts.push_back(strtoull(part.c_str(), nullptr, 10));
        if (q == std::string::npos) break;
        p = q + 1;
    }
    if (parts.size() < 2 || parts.size() > 16 || parts[0] >= 1ULL << 48) return false;
    memset(out, 0, SidBinSize);
    out[0] = 1;
    out[1] = (unsigned char)(parts.size() - 1);
    for (int i = 0; i < 6; i++) out[2 + i] = (unsigned char)(parts[0] >> (8 * (5 - i)));
    for (size_t i = 1; i < parts.size(); i++) {
        if (parts[i] > 0xffffffffULL) return false;
        for (int k = 0; k < 4; k++) out[8 + 4 * (i - 1) + k] = (unsigned char)(parts[i] >> (8 * k));
    }
    return true;
}

} // namespace sharedd

src/adquery.h

0 → 100644
+52 −0
Changes for src/adquery.h: 52 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
Copyright (c) 2026, Jan Koester jan.koester@gmx.net
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

// asking adconnectd (its socket, protocol.h of adconnect) and the binary
// and text forms of SIDs: shared by the SMB login/SID mapping and the RPC
// services

#pragma once

#include <cstddef>
#include <string>
#include <vector>

namespace sharedd {

constexpr size_t SidBinSize = 68;  // binary SID: 8 bytes + 15 sub-authorities

// one request to adconnectd, its whole answer; false if it couldn't be
// asked (not running, no answer within timeout seconds)
bool adconnectQuery(const std::string &line, std::string &reply, int timeout);
// the tab-separated fields of the first record of an adconnectd answer
std::vector<std::string> firstRecord(const std::string &reply);
// a binary SID (revision 1, at most 15 sub-authorities) as S-1-...; "" if malformed
std::string sidText(const unsigned char *b, size_t size);
// S-1-... into out[SidBinSize]; false if malformed
bool sidBinary(const std::string &t, unsigned char out[SidBinSize]);

} // namespace sharedd
+1 −72
Changes for src/smb.cpp: 1 added line, 72 removed lines.
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include "smb.h"
#include "adquery.h"

#include <algorithm>
#include <cerrno>
@@ -1247,78 +1248,6 @@ bool ntlmViaAdconnect(const std::string &domain, const std::string &account, con
    return true;
}

// one request to adconnectd, its whole answer; false if it couldn't be
// asked (not running, no answer within timeout seconds)
bool adconnectQuery(const std::string &line, std::string &reply, int timeout) {
    int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
    if (fd < 0) return false;
    sockaddr_un a{};
    a.sun_family = AF_UNIX;
    snprintf(a.sun_path, sizeof a.sun_path, "%s", SHAREDD_ADCONNECT_SOCKET);
    struct timeval tv = {timeout, 0};
    setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
    setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
    if (connect(fd, (sockaddr *)&a, sizeof a) != 0) {
        close(fd);
        return false;
    }
    std::string req = line + "\n";
    bool ok = ::send(fd, req.data(), req.size(), MSG_NOSIGNAL) == (ssize_t)req.size();
    reply.clear();
    char buf[4096];
    ssize_t n = 0;
    while (ok && (n = recv(fd, buf, sizeof buf, 0)) > 0) reply.append(buf, (size_t)n);
    close(fd);
    return ok && n == 0;
}

// the tab-separated fields of the first record of an adconnectd answer
std::vector<std::string> firstRecord(const std::string &reply) {
    std::vector<std::string> f;
    std::string line = reply.substr(0, reply.find('\n'));
    size_t p = 0;
    for (size_t q; (q = line.find('\t', p)) != std::string::npos; p = q + 1) f.push_back(line.substr(p, q - p));
    f.push_back(line.substr(p));
    return f;
}

// a binary SID (revision 1, at most 15 sub-authorities) as S-1-...; "" if malformed
std::string sidText(const unsigned char *b, size_t size) {
    if (size < 8 || b[0] != 1 || b[1] > 15 || size < 8 + 4 * (size_t)b[1]) return std::string();
    unsigned long long auth = 0;
    for (int i = 2; i < 8; i++) auth = auth << 8 | b[i];
    std::string s = "S-1-" + std::to_string(auth);
    for (int i = 0; i < b[1]; i++) {
        const unsigned char *p = b + 8 + 4 * i;
        s += "-" + std::to_string((uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24);
    }
    return s;
}

// S-1-... into out[SidSize]; false if malformed
bool sidBinary(const std::string &t, unsigned char out[SidSize]) {
    if (t.compare(0, 4, "S-1-") != 0) return false;
    std::vector<unsigned long long> parts;
    for (size_t p = 4;;) {
        size_t q = t.find('-', p);
        std::string part = t.substr(p, q == std::string::npos ? std::string::npos : q - p);
        if (part.empty() || part.size() > 15 || part.find_first_not_of("0123456789") != std::string::npos) return false;
        parts.push_back(strtoull(part.c_str(), nullptr, 10));
        if (q == std::string::npos) break;
        p = q + 1;
    }
    if (parts.size() < 2 || parts.size() > 16 || parts[0] >= 1ULL << 48) return false;
    memset(out, 0, SidSize);
    out[0] = 1;
    out[1] = (unsigned char)(parts.size() - 1);
    for (int i = 0; i < 6; i++) out[2 + i] = (unsigned char)(parts[0] >> (8 * (5 - i)));
    for (size_t i = 1; i < parts.size(); i++) {
        if (parts[i] > 0xffffffffULL) return false;
        for (int k = 0; k < 4; k++) out[8 + 4 * (i - 1) + k] = (unsigned char)(parts[i] >> (8 * k));
    }
    return true;
}

// whether acct, found by name, is the account adconnectd has under that
// name (the same uid) -- and not a local one of the same name
bool sameAsDirectory(const std::string &name, const Account &acct) {