1 /*
2  * Copyright (C) 2020 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 #define TRACE_TAG TRANSPORT
18 
19 #include "adb_mdns.h"
20 
21 #include <algorithm>
22 #include <set>
23 
24 #include <android-base/stringprintf.h>
25 #include <android-base/strings.h>
26 
27 #include "adb_trace.h"
28 
29 #define ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ver) ("v=" #ver)
30 
31 const char* kADBSecurePairingServiceTxtRecord =
32         ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ADB_SECURE_SERVICE_VERSION);
33 const char* kADBSecureConnectServiceTxtRecord =
34         ADB_SECURE_SERVICE_VERSION_TXT_RECORD(ADB_SECURE_SERVICE_VERSION);
35 
36 #define ADB_FULL_MDNS_SERVICE_TYPE(atype) ("_" atype "._tcp")
37 const char* kADBDNSServices[] = {ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_SERVICE_TYPE),
38                                  ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_PAIRING_TYPE),
39                                  ADB_FULL_MDNS_SERVICE_TYPE(ADB_MDNS_TLS_CONNECT_TYPE)};
40 
41 const char* kADBDNSServiceTxtRecords[] = {
42         nullptr,
43         kADBSecurePairingServiceTxtRecord,
44         kADBSecureConnectServiceTxtRecord,
45 };
46 
47 #if ADB_HOST
48 namespace {
49 
50 std::atomic<bool> g_allowedlist_configured{false};
51 [[clang::no_destroy]] std::set<int> g_autoconn_allowedlist;
52 
config_auto_connect_services()53 void config_auto_connect_services() {
54     bool expected = false;
55     if (!g_allowedlist_configured.compare_exchange_strong(expected, true)) {
56         return;
57     }
58 
59     // ADB_MDNS_AUTO_CONNECT is a comma-delimited list of mdns services
60     // that are allowed to auto-connect. By default, only allow "adb-tls-connect"
61     // to auto-connect, since this is filtered down to auto-connect only to paired
62     // devices.
63     g_autoconn_allowedlist.insert(kADBSecureConnectServiceRefIndex);
64     const char* srvs = getenv("ADB_MDNS_AUTO_CONNECT");
65     if (!srvs) {
66         return;
67     }
68 
69     if (strcmp(srvs, "0") == 0) {
70         D("Disabling all auto-connecting");
71         g_autoconn_allowedlist.clear();
72         return;
73     }
74 
75     if (strcmp(srvs, "all") == 0) {
76         D("Allow all auto-connecting");
77         g_autoconn_allowedlist.insert(kADBTransportServiceRefIndex);
78         return;
79     }
80 
81     // Selectively choose which services to allow auto-connect.
82     // E.g. ADB_MDNS_AUTO_CONNECT=adb,adb-tls-connect would allow
83     // _adb._tcp and _adb-tls-connnect._tcp services to auto-connect.
84     auto srvs_list = android::base::Split(srvs, ",");
85     std::set<int> new_allowedlist;
86     for (const auto& item : srvs_list) {
87         auto full_srv = android::base::StringPrintf("_%s._tcp", item.data());
88         std::optional<int> idx = adb_DNSServiceIndexByName(full_srv);
89         if (idx.has_value()) {
90             new_allowedlist.insert(*idx);
91         }
92     }
93 
94     if (!new_allowedlist.empty()) {
95         g_autoconn_allowedlist = std::move(new_allowedlist);
96     }
97 
98     std::string res;
99     std::for_each(g_autoconn_allowedlist.begin(), g_autoconn_allowedlist.end(), [&](const int& i) {
100         res += kADBDNSServices[i];
101         res += ",";
102     });
103     D("mdns auto-connect allowedlist: [%s]", res.data());
104 }
105 
106 }  // namespace
107 
adb_DNSServiceIndexByName(std::string_view reg_type)108 std::optional<int> adb_DNSServiceIndexByName(std::string_view reg_type) {
109     for (int i = 0; i < kNumADBDNSServices; ++i) {
110         if (!strncmp(reg_type.data(), kADBDNSServices[i], strlen(kADBDNSServices[i]))) {
111             return i;
112         }
113     }
114     return std::nullopt;
115 }
116 
adb_DNSServiceShouldAutoConnect(std::string_view reg_type,std::string_view service_name)117 bool adb_DNSServiceShouldAutoConnect(std::string_view reg_type, std::string_view service_name) {
118     config_auto_connect_services();
119 
120     // Try to auto-connect to any "_adb" or "_adb-tls-connect" services excluding emulator services.
121     std::optional<int> index = adb_DNSServiceIndexByName(reg_type);
122     if (!index ||
123         (index != kADBTransportServiceRefIndex && index != kADBSecureConnectServiceRefIndex)) {
124         return false;
125     }
126     if (g_autoconn_allowedlist.find(*index) == g_autoconn_allowedlist.end()) {
127         D("Auto-connect for reg_type '%s' disabled", reg_type.data());
128         return false;
129     }
130     // Ignore adb-EMULATOR* service names, as it interferes with the
131     // emulator ports that are already connected.
132     if (android::base::StartsWith(service_name, "adb-EMULATOR")) {
133         LOG(INFO) << "Ignoring emulator transport service [" << service_name << "]";
134         return false;
135     }
136     return true;
137 }
138 
139 #endif  // ADB_HOST
140