aboutsummaryrefslogtreecommitdiff
path: root/src/usb-manager.cpp
blob: 8b24f1716adcd916c08e77ed82da994c34497019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright 2016 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

#include <src/adbd-client.h>
#include <src/usb-manager.h>
#include <src/usb-snap.h>

#include <glib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <set>

class UsbManager::Impl
{
public:
 
    explicit Impl(
        const std::string& socket_path,
        const std::string& public_keys_filename,
        const std::shared_ptr<UsbMonitor>& usb_monitor,
        const std::shared_ptr<Greeter>& greeter
    ):
        m_socket_path{socket_path},
        m_public_keys_filename{public_keys_filename},
        m_usb_monitor{usb_monitor},
        m_greeter{greeter}
    {
g_message("%s %s", G_STRLOC, G_STRFUNC);
        m_usb_monitor->on_usb_disconnected().connect([this](const std::string& /*usb_name*/) {
g_message("%s %s", G_STRLOC, G_STRFUNC);
            restart();
        });

g_message("%s %s", G_STRLOC, G_STRFUNC);
        m_greeter->is_active().changed().connect([this](bool /*is_active*/) {
g_message("%s %s", G_STRLOC, G_STRFUNC);
            maybe_snap();
        });

g_message("%s %s", G_STRLOC, G_STRFUNC);
        restart();
g_message("%s %s", G_STRLOC, G_STRFUNC);
    }

    ~Impl()
    {
g_message("%s %s", G_STRLOC, G_STRFUNC);
        if (m_restart_idle_tag)
            g_source_remove(m_restart_idle_tag);

g_message("%s %s", G_STRLOC, G_STRFUNC);
        clear();
g_message("%s %s", G_STRLOC, G_STRFUNC);
    }

private:

    void clear()
    {
g_message("%s %s", G_STRLOC, G_STRFUNC);
        // clear out old state
        m_snap_connections.clear();
g_message("%s %s", G_STRLOC, G_STRFUNC);
        m_snap.reset();
g_message("%s %s", G_STRLOC, G_STRFUNC);
        m_req = decltype(m_req)();
g_message("%s %s", G_STRLOC, G_STRFUNC);
        m_adbd_client.reset();
g_message("%s %s", G_STRLOC, G_STRFUNC);
    }

    void restart()
    {
g_message("%s %s", G_STRLOC, G_STRFUNC);
        clear();

g_message("%s %s", G_STRLOC, G_STRFUNC);
        // set a new client
        m_adbd_client.reset(new GAdbdClient{m_socket_path});
g_message("%s %s", G_STRLOC, G_STRFUNC);
        m_adbd_client->on_pk_request().connect(
            [this](const AdbdClient::PKRequest& req) {
g_message("%s %s", G_STRLOC, G_STRFUNC);
                g_debug("%s got pk request: %s", G_STRLOC, req.fingerprint.c_str());
g_message("%s %s", G_STRLOC, G_STRFUNC);
                m_req = req;
g_message("%s %s", G_STRLOC, G_STRFUNC);
                maybe_snap();
g_message("%s %s", G_STRLOC, G_STRFUNC);
            }
        );
    }

    void maybe_snap()
    {
g_message("%s %s", G_STRLOC, G_STRFUNC);
        // don't prompt in the greeter!
        if (!m_req.public_key.empty() && !m_greeter->is_active().get())
            snap();
g_message("%s %s", G_STRLOC, G_STRFUNC);
    }

    void snap()
    {
g_message("%s %s", G_STRLOC, G_STRFUNC);
        m_snap = std::make_shared<UsbSnap>(m_req.fingerprint);
g_message("%s %s", G_STRLOC, G_STRFUNC);
        m_snap_connections.insert((*m_snap).on_user_response().connect(
            [this](AdbdClient::PKResponse response, bool remember_choice){
g_message("%s %s", G_STRLOC, G_STRFUNC);
                g_debug("%s thread %p user responded! response %d, remember %d", G_STRLOC, g_thread_self(), int(response), int(remember_choice));
g_message("%s %s", G_STRLOC, G_STRFUNC);
                m_req.respond(response);
g_message("%s %s", G_STRLOC, G_STRFUNC);
                if (remember_choice && (response == AdbdClient::PKResponse::ALLOW))
                    write_public_key(m_req.public_key);
g_message("%s %s", G_STRLOC, G_STRFUNC);
                m_restart_idle_tag = g_idle_add([](gpointer gself){
g_message("%s %s", G_STRLOC, G_STRFUNC);
                    auto self = static_cast<Impl*>(gself);
g_message("%s %s", G_STRLOC, G_STRFUNC);
                    self->m_restart_idle_tag = 0;
g_message("%s %s", G_STRLOC, G_STRFUNC);
                    self->restart();
g_message("%s %s", G_STRLOC, G_STRFUNC);
                    return G_SOURCE_REMOVE;
                }, this);
            }
        ));
g_message("%s %s", G_STRLOC, G_STRFUNC);
    }

    void write_public_key(const std::string& public_key)
    {
g_message("%s %s", G_STRLOC, G_STRFUNC);
        g_debug("%s writing public key '%s' to '%s'", G_STRLOC, public_key.c_str(), m_public_keys_filename.c_str());

        // confirm the directory exists
        auto dirname = g_path_get_dirname(m_public_keys_filename.c_str());
g_message("%s %s", G_STRLOC, G_STRFUNC);
        const auto dir_exists = g_file_test(dirname, G_FILE_TEST_IS_DIR);
        if (!dir_exists)
            g_warning("ADB data directory '%s' does not exist", dirname);
        g_clear_pointer(&dirname, g_free);
g_message("%s %s", G_STRLOC, G_STRFUNC);
        if (!dir_exists)
            return;
g_message("%s %s", G_STRLOC, G_STRFUNC);

        // open the file in append mode, with user rw and group r permissions
        const auto fd = open(
            m_public_keys_filename.c_str(),
            O_APPEND|O_CREAT|O_WRONLY,
            S_IRUSR|S_IWUSR|S_IRGRP
        );
g_message("%s %s", G_STRLOC, G_STRFUNC);
        if (fd == -1) {
            g_warning("Error opening ADB datafile: %s", g_strerror(errno));
g_message("%s %s", G_STRLOC, G_STRFUNC);
            return;
        }
g_message("%s %s", G_STRLOC, G_STRFUNC);

        // write the new public key on its own line
        std::string buf {public_key + '\n'};
        if (write(fd, buf.c_str(), buf.size()) == -1)
            g_warning("Error writing ADB datafile: %d %s", errno, g_strerror(errno));
        close(fd);
g_message("%s %s", G_STRLOC, G_STRFUNC);
    }

    const std::string m_socket_path;
    const std::string m_public_keys_filename;
    const std::shared_ptr<UsbMonitor> m_usb_monitor;
    const std::shared_ptr<Greeter> m_greeter;
 
    unsigned int m_restart_idle_tag {};

    std::shared_ptr<GAdbdClient> m_adbd_client;
    AdbdClient::PKRequest m_req;
    std::shared_ptr<UsbSnap> m_snap;
    std::set<core::ScopedConnection> m_snap_connections;
};

/***
****
***/

UsbManager::UsbManager(
    const std::string& socket_path,
    const std::string& public_keys_filename,
    const std::shared_ptr<UsbMonitor>& usb_monitor,
    const std::shared_ptr<Greeter>& greeter
):
    impl{new Impl{socket_path, public_keys_filename, usb_monitor, greeter}}
{
}

UsbManager::~UsbManager()
{
}