summaryrefslogtreecommitdiffstats
path: root/phosphor-ldap-config/ldap_configuration.cpp
blob: 06a4d5dc7729c62b6fac64600423ff6cf4a07982 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include "ldap_configuration.hpp"
#include "config.h"
#include <fstream>
#include <sstream>

namespace phosphor
{
namespace ldap
{
constexpr auto nslcdService = "nslcd.service";

Config::Config(sdbusplus::bus::bus& bus, const char* path, const char* filePath,
               bool secureLDAP, std::string lDAPServerURI,
               std::string lDAPBindDN, std::string lDAPBaseDN,
               std::string lDAPBindDNpassword,
               ldap_base::Config::SearchScope lDAPSearchScope,
               ldap_base::Config::Type lDAPType, ConfigMgr& parent) :
    ConfigIface(bus, path, true),
    configFilePath(filePath), bus(bus), parent(parent)
{
    ConfigIface::secureLDAP(secureLDAP);
    ConfigIface::lDAPServerURI(lDAPServerURI);
    ConfigIface::lDAPBindDN(lDAPBindDN);
    ConfigIface::lDAPBaseDN(lDAPBaseDN);
    ConfigIface::lDAPBINDDNpassword(lDAPBindDNpassword);
    ConfigIface::lDAPSearchScope(lDAPSearchScope);
    ConfigIface::lDAPType(lDAPType);
    writeConfig();
    parent.restartService(nslcdService);
    // Emit deferred signal.
    this->emit_object_added();
}

void Config::writeConfig()
{
    std::fstream stream(configFilePath.c_str(), std::fstream::out);
    std::stringstream confData;
    confData << "uid root\n";
    confData << "gid root\n\n";
    confData << "ldap_version 3\n\n";
    confData << "timelimit 30\n";
    confData << "bind_timelimit 30\n";
    confData << "pagesize 1000\n";
    confData << "referrals off\n\n";
    confData << "uri " << lDAPServerURI() << "\n\n";
    confData << "base " << lDAPBaseDN() << "\n\n";
    confData << "binddn " << lDAPBindDN() << "\n";
    confData << "bindpw " << lDAPBINDDNpassword() << "\n\n";
    switch (lDAPSearchScope())
    {
        case ldap_base::Config::SearchScope::sub:
            confData << "scope sub\n\n";
            break;
        case ldap_base::Config::SearchScope::one:
            confData << "scope one\n\n";
            break;
        case ldap_base::Config::SearchScope::base:
            confData << "scope base\n\n";
            break;
    }
    confData << "base passwd " << lDAPBaseDN() << "\n";
    confData << "base shadow " << lDAPBaseDN() << "\n\n";
    if (secureLDAP() == true)
    {
        confData << "ssl on\n";
        confData << "tls_reqcert allow\n";
        confData << "tls_cert /etc/nslcd/certs/cert.pem\n";
    }
    else
    {
        confData << "ssl off\n\n";
    }
    if (lDAPType() == ldap_base::Config::Type::ActiveDirectory)
    {
        confData << "filter passwd (&(objectClass=user)(objectClass=person)"
                    "(!(objectClass=computer)))\n";
        confData
            << "filter group (|(objectclass=group)(objectclass=groupofnames) "
               "(objectclass=groupofuniquenames))\n";
        confData << "map passwd uid              sAMAccountName\n";
        confData << "map passwd uidNumber        "
                    "objectSid:S-1-5-21-3623811015-3361044348-30300820\n";
        confData << "map passwd gidNumber        primaryGroupID\n";
        confData << "map passwd homeDirectory    \"/home/$sAMAccountName\"\n";
        confData << "map passwd gecos            displayName\n";
        confData << "map passwd loginShell       \"/bin/bash\"\n";
        confData << "map group gidNumber         primaryGroupID\n";
        confData << "map group gidNumber         "
                    "objectSid:S-1-5-21-3623811015-3361044348-30300820\n";
        confData << "map group cn                sAMAccountName\n";
    }
    else if (lDAPType() == ldap_base::Config::Type::OpenLdap)
    {
        confData << "filter passwd (objectclass=*)\n";
        confData << "map passwd uid cn\n";
        confData << "map passwd gecos displayName\n";
    }
    stream << confData.str();
    stream.flush();
    stream.close();
    return;
}

bool Config::secureLDAP(bool value)
{
    if (value == secureLDAP())
    {
        return value;
    }

    auto val = ConfigIface::secureLDAP(value);
    writeConfig();
    parent.restartService(nslcdService);

    return val;
}

std::string Config::lDAPServerURI(std::string value)
{
    if (value == lDAPServerURI())
    {
        return value;
    }

    auto val = ConfigIface::lDAPServerURI(value);
    writeConfig();
    parent.restartService(nslcdService);

    return val;
}

std::string Config::lDAPBindDN(std::string value)
{
    if (value == lDAPBindDN())
    {
        return value;
    }

    auto val = ConfigIface::lDAPBindDN(value);
    writeConfig();
    parent.restartService(nslcdService);

    return val;
}

std::string Config::lDAPBaseDN(std::string value)
{
    if (value == lDAPBaseDN())
    {
        return value;
    }

    auto val = ConfigIface::lDAPBaseDN(value);
    writeConfig();
    parent.restartService(nslcdService);

    return val;
}

std::string Config::lDAPBINDDNpassword(std::string value)
{
    if (value == lDAPBINDDNpassword())
    {
        return value;
    }

    auto val = ConfigIface::lDAPBINDDNpassword(value);
    writeConfig();
    parent.restartService(nslcdService);

    return val;
}

ldap_base::Config::SearchScope
    Config::lDAPSearchScope(ldap_base::Config::SearchScope value)
{
    if (value == lDAPSearchScope())
    {
        return value;
    }

    auto val = ConfigIface::lDAPSearchScope(value);
    writeConfig();
    parent.restartService(nslcdService);

    return val;
}

ldap_base::Config::Type Config::lDAPType(ldap_base::Config::Type value)
{
    if (value == lDAPType())
    {
        return value;
    }

    auto val = ConfigIface::lDAPType(value);
    writeConfig();
    parent.restartService(nslcdService);

    return val;
}

void ConfigMgr::restartService(const std::string& service)
{
    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
                                      SYSTEMD_INTERFACE, "RestartUnit");
    method.append(service.c_str(), "replace");
    bus.call_noreply(method);
}

std::string
    ConfigMgr::createConfig(bool secureLDAP, std::string lDAPServerURI,
                            std::string lDAPBindDN, std::string lDAPBaseDN,
                            std::string lDAPBINDDNpassword,
                            ldap_base::Create::SearchScope lDAPSearchScope,
                            ldap_base::Create::Type lDAPType)
{
    // With current implementation we support only one LDAP server.
    configPtr.reset(nullptr);

    auto objPath = std::string(LDAP_CONFIG_DBUS_OBJ_PATH);
    configPtr = std::make_unique<Config>(
        bus, objPath.c_str(), LDAP_CONFIG_FILE, secureLDAP, lDAPServerURI,
        lDAPBindDN, lDAPBaseDN, lDAPBINDDNpassword,
        static_cast<ldap_base::Config::SearchScope>(lDAPSearchScope),
        static_cast<ldap_base::Config::Type>(lDAPType), *this);

    return objPath;
}

} // namespace ldap
} // namespace phosphor
OpenPOWER on IntegriCloud