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 "import_parser.h"
25 #include "keyword_map.h"
26 #include "parser.h"
27
28 using android::base::ParseByteCount;
29
30 namespace android {
31 namespace init {
32
ParsePermissionsLine(std::vector<std::string> && args,std::vector<SysfsPermissions> * out_sysfs_permissions,std::vector<Permissions> * out_dev_permissions)33 Result<void> ParsePermissionsLine(std::vector<std::string>&& args,
34 std::vector<SysfsPermissions>* out_sysfs_permissions,
35 std::vector<Permissions>* out_dev_permissions) {
36 bool is_sysfs = out_sysfs_permissions != nullptr;
37 if (is_sysfs && !(args.size() == 5 || args.size() == 6)) {
38 return Error() << "/sys/ lines must have 5 or 6 entries";
39 }
40
41 if (!is_sysfs && !(args.size() == 4 || args.size() == 5)) {
42 return Error() << "/dev/ lines must have 4 or 5 entries";
43 }
44
45 auto it = args.begin();
46 const std::string& name = *it++;
47
48 std::string sysfs_attribute;
49 if (is_sysfs) sysfs_attribute = *it++;
50
51 // args is now common to both sys and dev entries and contains: <perm> <uid> <gid>
52 std::string& perm_string = *it++;
53 char* end_pointer = 0;
54 mode_t perm = strtol(perm_string.c_str(), &end_pointer, 8);
55 if (end_pointer == nullptr || *end_pointer != '\0') {
56 return Error() << "invalid mode '" << perm_string << "'";
57 }
58
59 std::string& uid_string = *it++;
60 passwd* pwd = getpwnam(uid_string.c_str());
61 if (!pwd) {
62 return Error() << "invalid uid '" << uid_string << "'";
63 }
64 uid_t uid = pwd->pw_uid;
65
66 std::string& gid_string = *it++;
67 struct group* grp = getgrnam(gid_string.c_str());
68 if (!grp) {
69 return Error() << "invalid gid '" << gid_string << "'";
70 }
71 gid_t gid = grp->gr_gid;
72
73 bool no_fnm_pathname = false;
74 if (it != args.end()) {
75 std::string& flags = *it++;
76 if (flags != "no_fnm_pathname") {
77 return Error() << "invalid option '" << flags << "', only no_fnm_pathname is supported";
78 }
79 no_fnm_pathname = true;
80 }
81
82 if (is_sysfs) {
83 out_sysfs_permissions->emplace_back(name, sysfs_attribute, perm, uid, gid, no_fnm_pathname);
84 } else {
85 out_dev_permissions->emplace_back(name, perm, uid, gid, no_fnm_pathname);
86 }
87 return {};
88 }
89
ParseFirmwareDirectoriesLine(std::vector<std::string> && args,std::vector<std::string> * firmware_directories)90 Result<void> ParseFirmwareDirectoriesLine(std::vector<std::string>&& args,
91 std::vector<std::string>* firmware_directories) {
92 if (args.size() < 2) {
93 return Error() << "firmware_directories must have at least 1 entry";
94 }
95
96 std::move(std::next(args.begin()), args.end(), std::back_inserter(*firmware_directories));
97
98 return {};
99 }
100
ParseExternalFirmwareHandlerLine(std::vector<std::string> && args,std::vector<ExternalFirmwareHandler> * external_firmware_handlers)101 Result<void> ParseExternalFirmwareHandlerLine(
102 std::vector<std::string>&& args,
103 std::vector<ExternalFirmwareHandler>* external_firmware_handlers) {
104 if (args.size() != 4 && args.size() != 5) {
105 return Error() << "external_firmware_handler lines must have 3 or 4 parameters";
106 }
107
108 if (std::find_if(external_firmware_handlers->begin(), external_firmware_handlers->end(),
109 [&args](const auto& other) { return other.devpath == args[1]; }) !=
110 external_firmware_handlers->end()) {
111 return Error() << "found a previous external_firmware_handler with the same devpath, '"
112 << args[1] << "'";
113 }
114
115 passwd* pwd = getpwnam(args[2].c_str());
116 if (!pwd) {
117 return ErrnoError() << "invalid handler uid'" << args[2] << "'";
118 }
119
120 gid_t gid = 0;
121 int handler_index = 3;
122 if (args.size() == 5) {
123 struct group* grp = getgrnam(args[3].c_str());
124 if (!grp) {
125 return ErrnoError() << "invalid handler gid '" << args[3] << "'";
126 }
127 gid = grp->gr_gid;
128 handler_index = 4;
129 }
130
131 ExternalFirmwareHandler handler(std::move(args[1]), pwd->pw_uid, gid,
132 std::move(args[handler_index]));
133 external_firmware_handlers->emplace_back(std::move(handler));
134
135 return {};
136 }
137
ParseEnabledDisabledLine(std::vector<std::string> && args,bool * feature)138 Result<void> ParseEnabledDisabledLine(std::vector<std::string>&& args, bool* feature) {
139 if (args.size() != 2) {
140 return Error() << args[0] << " lines take exactly one parameter";
141 }
142
143 if (args[1] == "enabled") {
144 *feature = true;
145 } else if (args[1] == "disabled") {
146 *feature = false;
147 } else {
148 return Error() << args[0] << " takes either 'enabled' or 'disabled' as a parameter";
149 }
150
151 return {};
152 }
153
ParseParallelRestoreconDirsLine(std::vector<std::string> && args,std::vector<std::string> * parallel_restorecon_dirs)154 Result<void> ParseParallelRestoreconDirsLine(std::vector<std::string>&& args,
155 std::vector<std::string>* parallel_restorecon_dirs) {
156 if (args.size() != 2) {
157 return Error() << "parallel_restorecon_dir lines must have exactly 2 parameters";
158 }
159
160 std::move(std::next(args.begin()), args.end(), std::back_inserter(*parallel_restorecon_dirs));
161
162 return {};
163 }
164
ParseUeventSocketRcvbufSizeLine(std::vector<std::string> && args,size_t * uevent_socket_rcvbuf_size)165 Result<void> ParseUeventSocketRcvbufSizeLine(std::vector<std::string>&& args,
166 size_t* uevent_socket_rcvbuf_size) {
167 if (args.size() != 2) {
168 return Error() << "uevent_socket_rcvbuf_size lines take exactly one parameter";
169 }
170
171 size_t parsed_size;
172 if (!ParseByteCount(args[1], &parsed_size)) {
173 return Error() << "could not parse size '" << args[1] << "' for uevent_socket_rcvbuf_line";
174 }
175
176 *uevent_socket_rcvbuf_size = parsed_size;
177
178 return {};
179 }
180
181 class SubsystemParser : public SectionParser {
182 public:
SubsystemParser(std::vector<Subsystem> * subsystems)183 SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
184 Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
185 int line) override;
186 Result<void> ParseLineSection(std::vector<std::string>&& args, int line) override;
187 Result<void> EndSection() override;
188
189 private:
190 Result<void> ParseDevName(std::vector<std::string>&& args);
191 Result<void> ParseDirName(std::vector<std::string>&& args);
192
193 Subsystem subsystem_;
194 std::vector<Subsystem>* subsystems_;
195 };
196
ParseSection(std::vector<std::string> && args,const std::string & filename,int line)197 Result<void> SubsystemParser::ParseSection(std::vector<std::string>&& args,
198 const std::string& filename, int line) {
199 if (args.size() != 2) {
200 return Error() << "subsystems must have exactly one name";
201 }
202
203 if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
204 return Error() << "ignoring duplicate subsystem entry";
205 }
206
207 subsystem_ = Subsystem(std::move(args[1]));
208
209 return {};
210 }
211
ParseDevName(std::vector<std::string> && args)212 Result<void> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
213 if (args[1] == "uevent_devname") {
214 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
215 return {};
216 }
217 if (args[1] == "uevent_devpath") {
218 subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
219 return {};
220 }
221
222 return Error() << "invalid devname '" << args[1] << "'";
223 }
224
ParseDirName(std::vector<std::string> && args)225 Result<void> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
226 if (args[1].front() != '/') {
227 return Error() << "dirname '" << args[1] << " ' does not start with '/'";
228 }
229
230 subsystem_.dir_name_ = args[1];
231 return {};
232 }
233
ParseLineSection(std::vector<std::string> && args,int line)234 Result<void> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
235 using OptionParser = Result<void> (SubsystemParser::*)(std::vector<std::string> && args);
236 // clang-format off
237 static const KeywordMap<OptionParser> parser_map = {
238 {"devname", {1, 1, &SubsystemParser::ParseDevName}},
239 {"dirname", {1, 1, &SubsystemParser::ParseDirName}},
240 };
241 // clang-format on
242
243 auto parser = parser_map.Find(args);
244
245 if (!parser.ok()) return Error() << parser.error();
246
247 return std::invoke(*parser, this, std::move(args));
248 }
249
EndSection()250 Result<void> SubsystemParser::EndSection() {
251 subsystems_->emplace_back(std::move(subsystem_));
252
253 return {};
254 }
255
ParseConfig(const std::vector<std::string> & configs)256 UeventdConfiguration ParseConfig(const std::vector<std::string>& configs) {
257 Parser parser;
258 UeventdConfiguration ueventd_configuration;
259
260 parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
261 parser.AddSectionParser("subsystem",
262 std::make_unique<SubsystemParser>(&ueventd_configuration.subsystems));
263
264 using namespace std::placeholders;
265 parser.AddSingleLineParser(
266 "/sys/",
267 std::bind(ParsePermissionsLine, _1, &ueventd_configuration.sysfs_permissions, nullptr));
268 parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, nullptr,
269 &ueventd_configuration.dev_permissions));
270 parser.AddSingleLineParser("firmware_directories",
271 std::bind(ParseFirmwareDirectoriesLine, _1,
272 &ueventd_configuration.firmware_directories));
273 parser.AddSingleLineParser("external_firmware_handler",
274 std::bind(ParseExternalFirmwareHandlerLine, _1,
275 &ueventd_configuration.external_firmware_handlers));
276 parser.AddSingleLineParser("modalias_handling",
277 std::bind(ParseEnabledDisabledLine, _1,
278 &ueventd_configuration.enable_modalias_handling));
279 parser.AddSingleLineParser("uevent_socket_rcvbuf_size",
280 std::bind(ParseUeventSocketRcvbufSizeLine, _1,
281 &ueventd_configuration.uevent_socket_rcvbuf_size));
282 parser.AddSingleLineParser("parallel_restorecon_dir",
283 std::bind(ParseParallelRestoreconDirsLine, _1,
284 &ueventd_configuration.parallel_restorecon_dirs));
285 parser.AddSingleLineParser("parallel_restorecon",
286 std::bind(ParseEnabledDisabledLine, _1,
287 &ueventd_configuration.enable_parallel_restorecon));
288
289 for (const auto& config : configs) {
290 parser.ParseConfig(config);
291 }
292
293 return ueventd_configuration;
294 }
295
296 } // namespace init
297 } // namespace android
298