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) {
105         return Error() << "external_firmware_handler lines must have exactly 3 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     ExternalFirmwareHandler handler(std::move(args[1]), pwd->pw_uid, std::move(args[3]));
121     external_firmware_handlers->emplace_back(std::move(handler));
122 
123     return {};
124 }
125 
ParseEnabledDisabledLine(std::vector<std::string> && args,bool * feature)126 Result<void> ParseEnabledDisabledLine(std::vector<std::string>&& args, bool* feature) {
127     if (args.size() != 2) {
128         return Error() << args[0] << " lines take exactly one parameter";
129     }
130 
131     if (args[1] == "enabled") {
132         *feature = true;
133     } else if (args[1] == "disabled") {
134         *feature = false;
135     } else {
136         return Error() << args[0] << " takes either 'enabled' or 'disabled' as a parameter";
137     }
138 
139     return {};
140 }
141 
ParseUeventSocketRcvbufSizeLine(std::vector<std::string> && args,size_t * uevent_socket_rcvbuf_size)142 Result<void> ParseUeventSocketRcvbufSizeLine(std::vector<std::string>&& args,
143                                              size_t* uevent_socket_rcvbuf_size) {
144     if (args.size() != 2) {
145         return Error() << "uevent_socket_rcvbuf_size lines take exactly one parameter";
146     }
147 
148     size_t parsed_size;
149     if (!ParseByteCount(args[1], &parsed_size)) {
150         return Error() << "could not parse size '" << args[1] << "' for uevent_socket_rcvbuf_line";
151     }
152 
153     *uevent_socket_rcvbuf_size = parsed_size;
154 
155     return {};
156 }
157 
158 class SubsystemParser : public SectionParser {
159   public:
SubsystemParser(std::vector<Subsystem> * subsystems)160     SubsystemParser(std::vector<Subsystem>* subsystems) : subsystems_(subsystems) {}
161     Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
162                               int line) override;
163     Result<void> ParseLineSection(std::vector<std::string>&& args, int line) override;
164     Result<void> EndSection() override;
165 
166   private:
167     Result<void> ParseDevName(std::vector<std::string>&& args);
168     Result<void> ParseDirName(std::vector<std::string>&& args);
169 
170     Subsystem subsystem_;
171     std::vector<Subsystem>* subsystems_;
172 };
173 
ParseSection(std::vector<std::string> && args,const std::string & filename,int line)174 Result<void> SubsystemParser::ParseSection(std::vector<std::string>&& args,
175                                            const std::string& filename, int line) {
176     if (args.size() != 2) {
177         return Error() << "subsystems must have exactly one name";
178     }
179 
180     if (std::find(subsystems_->begin(), subsystems_->end(), args[1]) != subsystems_->end()) {
181         return Error() << "ignoring duplicate subsystem entry";
182     }
183 
184     subsystem_ = Subsystem(std::move(args[1]));
185 
186     return {};
187 }
188 
ParseDevName(std::vector<std::string> && args)189 Result<void> SubsystemParser::ParseDevName(std::vector<std::string>&& args) {
190     if (args[1] == "uevent_devname") {
191         subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME;
192         return {};
193     }
194     if (args[1] == "uevent_devpath") {
195         subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH;
196         return {};
197     }
198 
199     return Error() << "invalid devname '" << args[1] << "'";
200 }
201 
ParseDirName(std::vector<std::string> && args)202 Result<void> SubsystemParser::ParseDirName(std::vector<std::string>&& args) {
203     if (args[1].front() != '/') {
204         return Error() << "dirname '" << args[1] << " ' does not start with '/'";
205     }
206 
207     subsystem_.dir_name_ = args[1];
208     return {};
209 }
210 
ParseLineSection(std::vector<std::string> && args,int line)211 Result<void> SubsystemParser::ParseLineSection(std::vector<std::string>&& args, int line) {
212     using OptionParser = Result<void> (SubsystemParser::*)(std::vector<std::string> && args);
213     // clang-format off
214     static const KeywordMap<OptionParser> parser_map = {
215         {"devname",     {1,     1,      &SubsystemParser::ParseDevName}},
216         {"dirname",     {1,     1,      &SubsystemParser::ParseDirName}},
217     };
218     // clang-format on
219 
220     auto parser = parser_map.Find(args);
221 
222     if (!parser.ok()) return Error() << parser.error();
223 
224     return std::invoke(*parser, this, std::move(args));
225 }
226 
EndSection()227 Result<void> SubsystemParser::EndSection() {
228     subsystems_->emplace_back(std::move(subsystem_));
229 
230     return {};
231 }
232 
ParseConfig(const std::vector<std::string> & configs)233 UeventdConfiguration ParseConfig(const std::vector<std::string>& configs) {
234     Parser parser;
235     UeventdConfiguration ueventd_configuration;
236 
237     parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
238     parser.AddSectionParser("subsystem",
239                             std::make_unique<SubsystemParser>(&ueventd_configuration.subsystems));
240 
241     using namespace std::placeholders;
242     parser.AddSingleLineParser(
243             "/sys/",
244             std::bind(ParsePermissionsLine, _1, &ueventd_configuration.sysfs_permissions, nullptr));
245     parser.AddSingleLineParser("/dev/", std::bind(ParsePermissionsLine, _1, nullptr,
246                                                   &ueventd_configuration.dev_permissions));
247     parser.AddSingleLineParser("firmware_directories",
248                                std::bind(ParseFirmwareDirectoriesLine, _1,
249                                          &ueventd_configuration.firmware_directories));
250     parser.AddSingleLineParser("external_firmware_handler",
251                                std::bind(ParseExternalFirmwareHandlerLine, _1,
252                                          &ueventd_configuration.external_firmware_handlers));
253     parser.AddSingleLineParser("modalias_handling",
254                                std::bind(ParseEnabledDisabledLine, _1,
255                                          &ueventd_configuration.enable_modalias_handling));
256     parser.AddSingleLineParser("uevent_socket_rcvbuf_size",
257                                std::bind(ParseUeventSocketRcvbufSizeLine, _1,
258                                          &ueventd_configuration.uevent_socket_rcvbuf_size));
259     parser.AddSingleLineParser("parallel_restorecon",
260                                std::bind(ParseEnabledDisabledLine, _1,
261                                          &ueventd_configuration.enable_parallel_restorecon));
262 
263     for (const auto& config : configs) {
264         parser.ParseConfig(config);
265     }
266 
267     return ueventd_configuration;
268 }
269 
270 }  // namespace init
271 }  // namespace android
272