aboutsummaryrefslogtreecommitdiff
path: root/src/usb-manager.cpp
blob: 335db00df060aeaca54a45ea8d35273e1dbe5a6c (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
/*
 * 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>

class UsbManager::Impl
{
public:
 
    explicit Impl(
        const std::string& socket_path,
        const std::string& public_keys_filename
    ):
        m_adbd_client{std::make_shared<GAdbdClient>(socket_path)},
        m_public_keys_filename{public_keys_filename}
    {
        m_adbd_client->on_pk_request().connect([this](const AdbdClient::PKRequest& req){
            auto snap = new UsbSnap(req.fingerprint);
            snap->on_user_response().connect([this,req,snap](AdbdClient::PKResponse response, bool remember_choice){
                g_debug("user responded! response %d, remember %d", int(response), int(remember_choice));
                req.respond(response);
                if (remember_choice && (response == AdbdClient::PKResponse::ALLOW))
                    write_public_key(req.public_key);
                // delete_later
                g_idle_add([](gpointer gsnap){delete static_cast<UsbSnap*>(gsnap); return G_SOURCE_REMOVE;}, snap);
            });
        });
    }

    ~Impl()
    {
    }

private:

    void write_public_key(const std::string& public_key)
    {
        g_debug("writing public key '%s' to '%s'", 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());
        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);
        if (!dir_exists)
            return;

        // 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
        );
        if (fd == -1) {
            g_warning("Error opening ADB datafile: %s", g_strerror(errno));
            return;
        }

        // 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);
    }

    std::shared_ptr<GAdbdClient> m_adbd_client;
    const std::string m_public_keys_filename;
};

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

UsbManager::UsbManager(
    const std::string& socket_path,
    const std::string& public_keys_filename
):
    impl{new Impl{socket_path, public_keys_filename}}
{
}

UsbManager::~UsbManager()
{
}