1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ueventd_parser.h"
18 
19 #include <grp.h>
20 #include <pwd.h>
21 
22 #include <android-base/parseint.h>
23 
24 #include "keyword_map.h"
25 #include "parser.h"
26 
27 using android::base::ParseByteCount;
28 
29 namespace android {
30 namespace init {
31 
ParsePermissionsLine(std::vector<std::string> && args,std::vector<SysfsPermissions> * out_sysfs_permissions,std::vector<Permissions> * out_dev_permissions)32 Result<void> ParsePermissionsLine(std::vector<std::string>&& args,
33                                   std::vector<SysfsPermissions>* out_sysfs_permissions,
34                                   std::vector<Permissions>* out_dev_permissions) {
35     bool is_sysfs = out_sysfs_permissions != nullptr;
36     if (is_sysfs && args.size() != 5) {
37         return Error() << "/sys/ lines must have 5 entries";
38     }
39 
40     if (!is_sysfs && args.size() != 4) {
41         return Error() << "/dev/ lines must have 4 entries";
42     }
43 
44     auto it = args.begin();
45     const std::string& name = *it++;
46 
47     std::string sysfs_attribute;
48     if (is_sysfs) sysfs_attribute = *it++;
49 
50     // args is now common to both sys and dev entries and contains: <perm> <uid> <gid>
51     std::string& perm_string = *it++;
52     char* end_pointer = 0;
53     mode_t perm = strtol(perm_string.c_str(), &end_pointer, 8);
54     if (end_pointer == nullptr || *end_pointer != '\0') {
55         return Error() << "invalid mode '" << perm_string << "'";
56     }
57 
58     std::string& uid_string = *it++;
59     passwd* pwd = getpwnam(uid_string.c_str());
60     if (!pwd) {
61         return Error() << "invalid uid '" << uid_string << "'";
62     }
63     uid_t uid = pwd->pw_uid;
64 
65     std::string& gid_string = *it++;
66     struct group* grp = getgrnam(gid_string.c_str());
67     if (!grp) {
68         return Error() << "invalid gid '" << gid_string << "'";
69     }
70     gid_t gid = grp->gr_gid;
71 
72     if (is_sysfs) {
73         out_sysfs_permissions->emplace_back(name, sysfs_attribute, perm, uid, gid);
74     } else {
75         out_dev_permissions->emplace_back(name, perm, uid, gid);
76     }
77     return {};
78 }
79 
ParseFirmwareDirectoriesLine(std::vector<std::string> && args,std::vector<std::string> * firmware_directories)80 Result<void> ParseFirmwareDirectoriesLine(std::vector<std::string>&& args,
81                                           std::vector<std::string>* firmware_directories) {
82     if (args.size() < 2) {
83         return Error() << "firmware_directories must have at least 1 entry";
84     }
85 
86     std::move(std::next(args.begin()), args.end(), std::back_inserter(*firmware_directories));
87 
88     return {};
89 }
90 
ParseExternalFirmwareHandlerLine(std::vector<std::string> && args,std::vector<ExternalFirmwareHandler> * external_firmware_handlers)91 Result<void> ParseExternalFirmwareHandlerLine(
92         std::vector<std::string>&& args,
93         std::vector<ExternalFirmwareHandler>* external_firmware_handlers) {
94     if (args.size() != 4) {
95         return Error() << "external_firmware_handler lines must have exactly 3 parameters";
96     }
97 
98     if (std::find_if(external_firmware_handlers->begin(), external_firmware_handlers->end(),
99                      [&args](const auto& other) { return other.devpath == args[2]; }) !=
100         external_firmware_handlers->end()) {
101         return Error() << "found a previous external_firmware_handler with the same devpath, '"
102                        << args[2] << "'";
103     }
104 
105     passwd* pwd = getpwnam(args[2].c_str());
106     if (!pwd) {
107         return ErrnoError() << "invalid handler uid'" << args[2] << "'";
108     }
109 
110     ExternalFirmwareHandler handler(std::move(args[1]), pwd->pw_uid, std::move(args[3]));
111     external_firmware_handlers->emplace_back(std::move(handler));
112 
113     return {};
114 }
115 
ParseEnabledDisabledLine(std::vector<std::string> && args,bool * feature)116 Result<void> ParseEnabledDisabledLine(std::vector<std::string>&& args, bool* feature) {
117     if (args.size() != 2) {
118         return Error() << args[0] << " lines take exactly one parameter";
119     }
120 
121     if (args[1] == "enabled") {
122         *feature = true;
123     } else if (args[1] == "disabled") {
124         *feature = false;
125     } else {
126         return Error() << args[0] << " takes either 'enabled' or 'disabled' as a parameter";
127     }
128 
129     return {};
130 }
131 
ParseUeventSocketRcvbufSizeLine(std::vector<std::string> && args,size_t * uevent_socket_rcvbuf_size)132 Result<void> ParseUeventSocketRcvbufSizeLine(std::vector<std::string>&& args,
133                                              size_t* uevent_socket_rcvbuf_size) {
134     if (args.size() != 2) {
135         return Error() << "uevent_socket_rcvbuf_size lines take exactly one parameter";
136     }
137 
138     size_t parsed_size;
139     if (!ParseByteCount(args[1], &parsed_size)) {
140         return Error() << "could not parse size '" << args[1] << "' for uevent_socket_rcvbuf_line";
141     }
142 
143     *uevent_socket_rcvbuf_size = parsed_size;
144 
145     return {};
146 }
147 
148 class SubsystemParser : public SectionParser {
149   public:
SubsystemParser(std::vector<Subsystem> * subsystems)150     SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
151     Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
152                               int line) override;
153     Result<void> ParseLineSection(std::vector<std::string>&& args, int line) override;
154     Result<void> EndSection() override;
155 
156   private:
157     Result<void> ParseDevName(std::vector<std::string>&& args);
158     Result<void> ParseDirName(std::vector<std::string>&& args);
159 
160     Subsystem subsystem_;
161     std::vector<Subsystem>* subsystems_;
162 };
163 
ParseSection(std::vector<std::string> && args,const std::string & filename,int line)164 Result<void> SubsystemParser::ParseSection(std::vector<std::string>&& args,
165                                            const std::string& filename, int line) {
166     if (args.size() != 2) {
167         return Error() << "subsystems must have exactly one name";
168     }
169 
170     if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
171         return Error() << "ignoring duplicate subsystem entry";
172     }
173 
174     subsystem_ = Subsystem(std::move(args[1]));
175 
176     return {};
177 }
178 
ParseDevName(std::vector<std::string> && args)179 Result<void> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
180     if (args[1] == "uevent_devname") {
181         subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
182         return {};
183     }
184     if (args[1] == "uevent_devpath") {
185         subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
186         return {};
187     }
188 
189     return Error() << "invalid devname '" << args[1] << "'";
190 }
191 
ParseDirName(std::vector<std::string> && args)192 Result<void> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
193     if (args[1].front() != '/') {
194         return Error() << "dirname '" << args[1] << " ' does not start with '/'";
195     }
196 
197     subsystem_.dir_name_ = args[1];
198     return {};
199 }
200 
ParseLineSection(std::vector<std::string> && args,int line)201 Result<void> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
202     using OptionParser = Result<void> (SubsystemParser::*)(std::vector<std::string> && args);
203     // clang-format off
204     static const KeywordMap<OptionParser> parser_map = {
205         {"devname",     {1,     1,      &SubsystemParser::ParseDevName}},
206         {"dirname",     {1,     1,      &SubsystemParser::ParseDirName}},
207     };
208     // clang-format on
209 
210     auto parser = parser_map.Find(args);
211 
212     if (!parser.ok()) return Error() << parser.error();
213 
214     return std::invoke(*parser, this, std::move(args));
215 }
216 
EndSection()217 Result<void> SubsystemParser::EndSection() {
218     subsystems_->emplace_back(std::move(subsystem_));
219 
220     return {};
221 }
222 
ParseConfig(const std::vector<std::string> & configs)223 UeventdConfiguration ParseConfig(const std::vector<std::string>& configs) {
224     Parser parser;
225     UeventdConfiguration ueventd_configuration;
226 
227     parser.AddSectionParser("subsystem",
228                             std::make_unique<SubsystemParser>(&ueventd_configuration.subsystems));
229 
230     using namespace std::placeholders;
231     parser.AddSingleLineParser(
232             "/sys/",
233             std::bind(ParsePermissionsLine, _1, &ueventd_configuration.sysfs_permissions, nullptr));
234     parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, nullptr,
235                                                   &ueventd_configuration.dev_permissions));
236     parser.AddSingleLineParser("firmware_directories",
237                                std::bind(ParseFirmwareDirectoriesLine, _1,
238                                          &ueventd_configuration.firmware_directories));
239     parser.AddSingleLineParser("external_firmware_handler",
240                                std::bind(ParseExternalFirmwareHandlerLine, _1,
241                                          &ueventd_configuration.external_firmware_handlers));
242     parser.AddSingleLineParser("modalias_handling",
243                                std::bind(ParseEnabledDisabledLine, _1,
244                                          &ueventd_configuration.enable_modalias_handling));
245     parser.AddSingleLineParser("uevent_socket_rcvbuf_size",
246                                std::bind(ParseUeventSocketRcvbufSizeLine, _1,
247                                          &ueventd_configuration.uevent_socket_rcvbuf_size));
248     parser.AddSingleLineParser("parallel_restorecon",
249                                std::bind(ParseEnabledDisabledLine, _1,
250                                          &ueventd_configuration.enable_parallel_restorecon));
251 
252     for (const auto& config : configs) {
253         parser.ParseConfig(config);
254     }
255 
256     return ueventd_configuration;
257 }
258 
259 }  // namespace init
260 }  // namespace android
261