1/*
2 * Copyright (C) 2015 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 "diagnose_usb.h"
18
19#include <errno.h>
20#include <unistd.h>
21
22#include <string>
23
24#include <android-base/stringprintf.h>
25
26#if defined(__linux__)
27#include <grp.h>
28#include <pwd.h>
29#endif
30
31static const char kPermissionsHelpUrl[] = "http://developer.android.com/tools/device.html";
32
33// Returns a message describing any potential problems we find with udev, or an empty string if we
34// can't find plugdev information (i.e. udev is not installed).
35static std::string GetUdevProblem() {
36#if defined(__linux__)
37    errno = 0;
38    group* plugdev_group = getgrnam("plugdev");
39
40    if (plugdev_group == nullptr) {
41        if (errno != 0) {
42            perror("failed to read plugdev group info");
43        }
44        // We can't give any generally useful advice here, just let the caller print the help URL.
45        return "";
46    }
47
48    // getgroups(2) indicates that the GNU group_member(3) may not check the egid so we check it
49    // additionally just to be sure.
50    if (group_member(plugdev_group->gr_gid) || getegid() == plugdev_group->gr_gid) {
51        // The user is in plugdev so the problem is likely with the udev rules.
52        return "user in plugdev group; are your udev rules wrong?";
53    }
54    passwd* pwd = getpwuid(getuid());
55    return android::base::StringPrintf("user %s is not in the plugdev group",
56                                       pwd ? pwd->pw_name : "?");
57#else
58    return "";
59#endif
60}
61
62// Short help text must be a single line, and will look something like:
63//
64//   no permissions (reason); see [URL]
65std::string UsbNoPermissionsShortHelpText() {
66    std::string help_text = "no permissions";
67
68    std::string problem(GetUdevProblem());
69    if (!problem.empty()) help_text += " (" + problem + ")";
70
71    return android::base::StringPrintf("%s; see [%s]", help_text.c_str(), kPermissionsHelpUrl);
72}
73
74// Long help text can span multiple lines but doesn't currently provide more detailed information:
75//
76//   insufficient permissions for device: reason
77//   See [URL] for more information
78std::string UsbNoPermissionsLongHelpText() {
79    std::string header = "insufficient permissions for device";
80
81    std::string problem(GetUdevProblem());
82    if (!problem.empty()) header += ": " + problem;
83
84    return android::base::StringPrintf("%s\nSee [%s] for more information", header.c_str(),
85                                       kPermissionsHelpUrl);
86}
87