summaryrefslogtreecommitdiff
path: root/src/smb.hpp
blob: 9e4a2fc29d561582c533ea69928ab254ab21d76e (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
#pragma once

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

#include <sys/mount.h>

#include <filesystem>
#include <optional>
#include <string>

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 = "sec=ntlmsspi,seal";
        const std::string perm = rw ? "rw" : "ro";
        std::string options = params + "," + perm;
        std::string credentialsOpt;

        if (!credentials)
        {
            LogMsg(Logger::Info, "Mounting as Guest");
            credentialsOpt = "guest,username=OpenBmc";
        }
        else
        {
            if (!validateUsername(credentials->user()))
            {
                LogMsg(Logger::Error,
                       "Username for CIFS share can't contain ',' character");
                return false;
            }
            credentials->escapeCommas();
            credentialsOpt = "username=" + credentials->user() +
                             ",password=" + credentials->password();
        }
        options += "," + credentialsOpt;

        std::string versionOpt = "vers=3.1.1";
        auto ec = mountWithSmbVers(remote, options, versionOpt);

        if (ec)
        {
            // vers=3 will negotiate max version from 3.02 and 3.0
            versionOpt = "vers=3";
            ec = mountWithSmbVers(remote, options, versionOpt);
        }

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

        if (ec)
        {
            return false;
        }
        return true;
    }

  private:
    std::string mountDir;

    /* Check if username does not contain comma (,) character */
    bool validateUsername(const std::string& username)
    {
        return username.find(',') == std::string::npos;
    }

    int mountWithSmbVers(const fs::path& remote, std::string options,
                         const std::string& version)
    {
        options += "," + version;

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

        if (ec)
        {
            LogMsg(Logger::Info, "Mount failed for ", version,
                   " with ec = ", ec, " errno = ", errno);
        }

        return ec;
    }
};