summaryrefslogtreecommitdiff
path: root/snmpcfg/snmpcfg-server.cpp
blob: da7655533320ca6fd4e9a50675bddcce50967cae (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
 * @brief SNMP Configuration manager
 *
 * This file is part of sila-snmp project.
 *
 * Copyright (c) 2022 SILA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <string>
#include <fstream>
#include <streambuf>

#include <sdbusplus/server.hpp>
#include <xyz/openbmc_project/SNMPCfg/server.hpp>
#include <phosphor-logging/log.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <xyz/openbmc_project/Common/error.hpp>

using namespace phosphor::logging;
using SNMPCfg_inherit = sdbusplus::server::object_t<
    sdbusplus::xyz::openbmc_project::server::SNMPCfg>;

using InternalFailure =
    sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
using InvalidArgument =
    sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
using Argument = xyz::openbmc_project::Common::InvalidArgument;

static constexpr auto snmpdConf = "/etc/snmp/snmpd.conf";
static constexpr auto whitespace = " \t";

/**
 * @brief Check if the string contains the token at the position or after
 *        several spaces.
 *
 * @param[in] str   - string to be checked
 * @param[in] token - expected token
 * @param[in/out] pos - start position for checking, will keep the last
 *                      checked position.
 *
 * @return - true if token found.
 */
static bool getToken(const std::string& str, const std::string& token,
                     size_t& pos)
{
    pos = str.find_first_not_of(whitespace, pos);
    if (pos != std::string::npos && 0 == str.compare(pos, token.size(), token))
    {
        auto endPos = str.find_first_of(whitespace, pos);
        if (endPos == std::string::npos || pos + token.size() == endPos)
        {
            pos = endPos;
            return true;
        }
    }
    return false;
}

class Configurator : public SNMPCfg_inherit
{
  public:
    /**
     * @brief Configurator object constructor
     *
     * @param bus  - DBus connection object reference
     * @param path - DBus object path
     */
    Configurator(sdbusplus::bus::bus& bus, const char* path) :
        SNMPCfg_inherit(bus, path), bus(bus)
    {
        readConfig();
    }

    std::string community(std::string value) override
    {
        static constexpr auto communityMaxLen = 256;
        static constexpr auto communityMinLen = 1;

        auto communityLen = value.length();
        if (communityLen < communityMinLen || communityLen > communityMaxLen)
        {
            log<level::ERR>("Invalid community name length");
            elog<InvalidArgument>(Argument::ARGUMENT_NAME("Community"),
                                  Argument::ARGUMENT_VALUE(value.c_str()));
        }

        SNMPCfg_inherit::community(value);
        writeConfig();
        return value;
    }

  private:
    /**
     * @brief Parse line and get community name
     *
     * @param line - the line from configuration file
     *
     * @return community name if corresponding statement found
     *         and nullopt otherwise
     */
    std::optional<std::string> getCommunityName(std::string line) const
    {
        // Required line should be in format:
        // 'com2sec readonly  default     <communityName>'
        size_t pos = 0;

        for (const auto& token : {"com2sec", "readonly", "default"})
        {
            if (!getToken(line, token, pos) || pos == std::string::npos)
            {
                return std::nullopt;
            }
        }

        pos = line.find_first_not_of(whitespace, pos);
        if (pos == std::string::npos)
        {
            return std::nullopt;
        }

        auto endPos = line.find_last_not_of(whitespace);
        if (pos < endPos && line[pos] == '"' && line[endPos] == '"')
        {
            pos++;
            endPos--;
        }

        return (pos <= endPos ? line.substr(pos, endPos - pos + 1)
                              : std::string{});
    }

    /**
     * @brief Read actual settings from configuration file
     */
    void readConfig()
    {
        std::ifstream fileToRead(snmpdConf, std::ios::in);
        if (!fileToRead.is_open())
        {
            log<level::ERR>("Failed to open SNMP daemon configuration file",
                            entry("FILE_NAME=%s", snmpdConf));
            return;
        }

        std::string line;
        while (std::getline(fileToRead, line))
        {
            auto communityName = getCommunityName(line);
            if (communityName)
            {
                SNMPCfg_inherit::community(*communityName);
                return;
            }
        }
        log<level::ERR>("Community not found",
                        entry("FILE_NAME=%s", snmpdConf));
    }

    /**
     * @brief Write actual settings to the configuration file.
     */
    void writeConfig()
    {
        std::string tmpFileName{snmpdConf};
        tmpFileName += ".tmp";

        std::ifstream fileToRead(snmpdConf, std::ios::in);
        std::ofstream fileToWrite(tmpFileName, std::ios::out);
        if (!fileToRead.is_open())
        {
            log<level::ERR>("Failed to open SNMP daemon configuration file",
                            entry("FILE_NAME=%s", snmpdConf));
            return;
        }

        if (!fileToWrite.is_open())
        {
            log<level::ERR>("Failed to create new configuration file",
                            entry("FILE_NAME=%s", tmpFileName.c_str()));
            return;
        }

        auto value = SNMPCfg_inherit::community();
        bool isQuoteRequired =
            (value.find_first_of(whitespace) != std::string::npos);
        bool updated = false;
        std::string line;
        while (std::getline(fileToRead, line))
        {
            auto communityName = getCommunityName(line);
            if (communityName && *communityName != value)
            {
                fileToWrite << "com2sec readonly  default\t  "
                            << (isQuoteRequired ? "\"" : "") << value
                            << (isQuoteRequired ? "\"" : "") << std::endl;
                updated = true;
            }
            else
            {
                fileToWrite << line << std::endl;
            }
        }
        fileToWrite.close();
        fileToRead.close();

        if (updated)
        {
            if (0 != std::rename(tmpFileName.c_str(), snmpdConf))
            {
                int error = errno;
                log<level::ERR>("Failed to update SNMP daemon configuarion",
                                entry("WHAT=%s", strerror(error)),
                                entry("FILE_NAME=%s", snmpdConf));
                elog<InternalFailure>();
            }

            reloadSnmpDaemon();
        }
        else
        {
            if (0 != std::remove(tmpFileName.c_str()))
            {
                int error = errno;
                log<level::ERR>("Failed to remove temporary file",
                                entry("WHAT=%s", strerror(error)),
                                entry("FILE_NAME=%s", tmpFileName.c_str()));
            }
        }
    }

    sdbusplus::bus::bus& bus;

    /**
     * @brief Ask SNMP daemon to reread configuration.
     */
    void reloadSnmpDaemon()
    {
        auto m = bus.new_method_call(
            "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
            "org.freedesktop.systemd1.Manager", "ReloadUnit");
        m.append("snmpd.service", "replace");
        try
        {
            bus.call_noreply(m);
        }
        catch (const sdbusplus::exception::SdBusError& e)
        {
            log<level::ERR>("Failed to reload SNMP daemon",
                            entry("WHATE=%s", e.what()));
            elog<InternalFailure>();
        }
    }
};

/**
 * @brief Application entry point
 *
 * @return exit status
 */
int main()
{
    constexpr auto path = "/xyz/openbmc_project/snmpcfg";

    auto b = sdbusplus::bus::new_default();
    sdbusplus::server::manager_t m{b, path};

    b.request_name("xyz.openbmc_project.SNMPCfg");
    Configurator cfg{b, path};

    while (1)
    {
        b.process_discard();
        b.wait();
    }

    return 0;
}