summaryrefslogtreecommitdiff
path: root/virtual-media/src/smb.hpp
blob: 62c3a44de1c690bc8fc23699d71885f4a0a00400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once

#include "logger.hpp"
#include "utils.hpp"

#include <sys/mount.h>

#include <filesystem>
#include <optional>

namespace fs = std::filesystem;

class SmbShare
{
  public:
    SmbShare(const fs::path& mountDir) : mountDir(mountDir)
    {
    }

    bool mount(const fs::path& remote, bool rw,
               const std::unique_ptr<utils::CredentialsProvider>& credentials)
    {
        LogMsg(Logger::Debug, "Trying to mount remote : ", remote);

        const std::string params = "nolock,sec=ntlmsspi,seal,vers=3.0";
        const std::string perm = rw ? "rw" : "ro";
        auto options = params + "," + perm;
        LogMsg(Logger::Debug, "Mounting with options: ", options);

        std::string credentialsOpt;
        if (!credentials)
        {
            LogMsg(Logger::Info, "Mounting as Guest");
            credentialsOpt = "guest,user=OpenBmc";
        }
        else
        {
            LogMsg(Logger::Info, "Authenticating as ", credentials->user());
            credentialsOpt = "user=" + credentials->user() +
                             ",password=" + credentials->password();
        }

        options += "," + credentialsOpt;

        auto ec = ::mount(remote.c_str(), mountDir.c_str(), "cifs", 0,
                          options.c_str());

        utils::secureCleanup(options);
        utils::secureCleanup(credentialsOpt);

        if (ec)
        {
            LogMsg(Logger::Error, "Mount failed with ec = ", ec,
                   " errno = ", errno);
            return false;
        }

        return true;
    }

    static std::optional<fs::path> createMountDir(const fs::path& name)
    {
        auto destPath = fs::temp_directory_path() / name;
        std::error_code ec;

        if (fs::create_directory(destPath, ec))
        {
            return destPath;
        }

        LogMsg(Logger::Error, ec,
               " : Unable to create mount directory: ", destPath);
        return {};
    }

    static void unmount(const fs::path& mountDir)
    {
        int result;
        std::error_code ec;

        result = ::umount(mountDir.string().c_str());
        if (result)
        {
            LogMsg(Logger::Error, result, " : Unable to unmout directory ",
                   mountDir);
        }

        if (!fs::remove_all(mountDir, ec))
        {
            LogMsg(Logger::Error, ec, " : Unable to remove mount directory ",
                   mountDir);
        }
    }

  private:
    std::string mountDir;
};