github.com/rigado/snapd@v2.42.5-go-mod+incompatible/interfaces/builtin/adb_support.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package builtin
    21  
    22  import (
    23  	"bytes"
    24  	"fmt"
    25  	"sort"
    26  
    27  	"github.com/snapcore/snapd/interfaces"
    28  	"github.com/snapcore/snapd/interfaces/udev"
    29  )
    30  
    31  const adbSupportSummary = `allows operating as Android Debug Bridge service`
    32  
    33  const adbSupportBaseDeclarationSlots = `
    34    adb-support:
    35      allow-installation:
    36        slot-snap-type:
    37          - core
    38      deny-auto-connection: true
    39  `
    40  
    41  // adbSupportVendors contains the map of USB vendor IDs to vendor name.
    42  //
    43  // The map contains the list of vendors that make or made devices that ADB may
    44  // want to talk to. It was derived from https://forum.snapcraft.io/t//5443/3
    45  var adbSupportVendors = map[int]string{
    46  	0x03f0: "HP",
    47  	0x03fc: "ECS",
    48  	0x0408: "QUANTA",
    49  	0x0409: "NEC",
    50  	0x0414: "GIGABYTE",
    51  	0x0451: "TI",
    52  	0x0471: "PHILPS",
    53  	0x0482: "KYOCERA",
    54  	0x0489: "FOXCONN",
    55  	0x04b7: "COMPAL",
    56  	0x04c5: "FUJITSU",
    57  	0x04da: "PMC-SIERRA",
    58  	0x04dd: "SHARP",
    59  	0x04e8: "SAMSUNG",
    60  	0x0502: "ACER",
    61  	0x0531: "WACOM",
    62  	0x054c: "SONY",
    63  	0x05c6: "Qualcomm",
    64  	0x067e: "INTERMEC",
    65  	0x091e: "GARMIN-ASUS",
    66  	0x0930: "TOSHIBA",
    67  	0x0955: "NVIDIA",
    68  	0x0b05: "ASUS",
    69  	0x0bb4: "HTC",
    70  	0x0c2e: "HONEYWELL",
    71  	0x0db0: "MSI",
    72  	0x0e79: "ARCHOS",
    73  	0x0e8d: "MTK",
    74  	0x0f1c: "FUNAI",
    75  	0x0fce: "SONY ERICSSON",
    76  	0x1004: "LGE",
    77  	0x109b: "HISENSE",
    78  	0x10a9: "PANTECH",
    79  	0x1219: "COMPALCOMM",
    80  	0x12d1: "HUAWEI",
    81  	0x1662: "POSITIVO",
    82  	0x16d5: "ANYDATA",
    83  	0x17ef: "LENOVO",
    84  	0x18d1: "GOOGLE",
    85  	0x1949: "LAB126",
    86  	0x19a5: "HARRIS",
    87  	0x19d2: "ZTE",
    88  	0x1b8e: "AMLOGIC",
    89  	0x1bbb: "T_AND_A",
    90  	0x1d09: "TECHFAITH",
    91  	0x1d45: "QISDA",
    92  	0x1d4d: "PEGATRON",
    93  	0x1d91: "BYD",
    94  	0x1e85: "GIGASET",
    95  	0x1ebf: "YULONG_COOLPAD",
    96  	0x1f3a: "ALLWINNER",
    97  	0x1f53: "SK TELESYS",
    98  	0x2006: "LENOVOMOBILE",
    99  	0x201e: "HAIER",
   100  	0x2080: "NOOK",
   101  	0x2116: "KT TECH",
   102  	0x2207: "ROCKCHIP",
   103  	0x2237: "KOBO",
   104  	0x2257: "OTGV",
   105  	0x22b8: "MOTOROLA",
   106  	0x22d9: "OPPO",
   107  	0x2314: "INQ_MOBILE",
   108  	0x2340: "TELEEPOCH",
   109  	0x2420: "IRIVER",
   110  	0x24e3: "K-TOUCH",
   111  	0x25e3: "LUMIGON",
   112  	0x2717: "XIAOMI",
   113  	0x271d: "GIONEE",
   114  	0x2836: "OUYA",
   115  	0x2916: "YOTADEVICES",
   116  	0x297f: "EMERGING_TECH",
   117  	0x29a9: "SMARTISAN",
   118  	0x29e4: "PRESTIGIO",
   119  	0x2a45: "MEIZU",
   120  	0x2a47: "BQ",
   121  	0x2a49: "UNOWHY",
   122  	0x2a70: "OnePlus",
   123  	0x2ae5: "FAIRPHONE",
   124  	0x413c: "DELL",
   125  	0x8087: "INTEL", // https://twitter.com/zygoon/status/1032233406564913152
   126  	0xE040: "VIZIO",
   127  }
   128  
   129  var adbSupportConnectedPlugAppArmor = `
   130  # Allow adb (server) to access all usb devices and rely on the device cgroup for mediation.
   131  /dev/bus/usb/[0-9][0-9][0-9]/[0-9][0-9][0-9] rw,
   132  
   133  # Allow access to udev meta-data about character devices with major number 189
   134  # as per https://www.kernel.org/doc/Documentation/admin-guide/devices.txt
   135  # those describe "USB serial converters - alternate devices".
   136  /run/udev/data/c189:* r,
   137  
   138  # Allow reading the serial number of all the USB devices.
   139  # Note that this path encodes the physical connection topology (e.g. any USB
   140  # hubs you are using) and as such there are more recursive patterns than one
   141  # might otherwise see necessary on their own system.
   142  /sys/devices/**/usb*/**/serial r,
   143  `
   144  
   145  type adbSupportInterface struct {
   146  	commonInterface
   147  	sortedVendorIDs []int
   148  }
   149  
   150  func (iface *adbSupportInterface) UDevConnectedPlug(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
   151  	for _, vendorID := range iface.vendorIDs() {
   152  		spec.TagDevice(fmt.Sprintf("SUBSYSTEM==\"usb\", ATTR{idVendor}==\"%04x\"", vendorID))
   153  	}
   154  	return nil
   155  }
   156  
   157  func (iface *adbSupportInterface) UDevConnectedSlot(spec *udev.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
   158  	var buf bytes.Buffer
   159  	fmt.Fprintf(&buf, "# Concatenation of all adb-support udev rules.\n")
   160  	for _, vendorID := range iface.vendorIDs() {
   161  		fmt.Fprintf(&buf, "# %s\n", adbSupportVendors[vendorID])
   162  		// TODO: consider changing to 0660 once we have support for system groups.
   163  		fmt.Fprintf(&buf, "SUBSYSTEM==\"usb\", ATTR{idVendor}==\"%04x\", MODE=\"0666\"\n", vendorID)
   164  	}
   165  	spec.AddSnippet(buf.String())
   166  	return nil
   167  }
   168  
   169  // vendorIDs returns a sorted list of vendor IDs supported by adb interface.
   170  func (iface *adbSupportInterface) vendorIDs() []int {
   171  	if iface.sortedVendorIDs == nil {
   172  		vendorIDs := make([]int, 0, len(adbSupportVendors))
   173  		for vendorID := range adbSupportVendors {
   174  			vendorIDs = append(vendorIDs, vendorID)
   175  		}
   176  		sort.Ints(vendorIDs)
   177  		iface.sortedVendorIDs = vendorIDs
   178  	}
   179  	return iface.sortedVendorIDs
   180  }
   181  
   182  func init() {
   183  	registerIface(&adbSupportInterface{commonInterface: commonInterface{
   184  		name:                  "adb-support",
   185  		summary:               adbSupportSummary,
   186  		implicitOnCore:        true,
   187  		implicitOnClassic:     true,
   188  		baseDeclarationSlots:  adbSupportBaseDeclarationSlots,
   189  		connectedPlugAppArmor: adbSupportConnectedPlugAppArmor,
   190  	}})
   191  }