aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2016-03-10 17:09:59 -0600
committerCharles Kerr <charles.kerr@canonical.com>2016-03-10 17:09:59 -0600
commitd8ef8e68805ab7f53258427c79ee5aaafec916ba (patch)
tree9d3b9afff38739829f41dc6c8ee327ccbafdb96b /src
parentf8a5d99b5ac03b5b759f67b33ed2c989fc0d0ceb (diff)
downloadayatana-indicator-display-d8ef8e68805ab7f53258427c79ee5aaafec916ba.tar.gz
ayatana-indicator-display-d8ef8e68805ab7f53258427c79ee5aaafec916ba.tar.bz2
ayatana-indicator-display-d8ef8e68805ab7f53258427c79ee5aaafec916ba.zip
add UsbManager
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/main.cpp14
-rw-r--r--src/usb-manager.cpp109
-rw-r--r--src/usb-manager.h34
4 files changed, 148 insertions, 10 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 6cfda91..d0ab901 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -10,6 +10,7 @@ add_library(
exporter.cpp
indicator.cpp
rotation-lock.cpp
+ usb-manager.cpp
usb-snap.cpp
)
diff --git a/src/main.cpp b/src/main.cpp
index 151b642..eb1bb2c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -17,10 +17,9 @@
* Charles Kerr <charles.kerr@canonical.com>
*/
-#include <src/adbd-client.h>
#include <src/exporter.h>
#include <src/rotation-lock.h>
-#include <src/usb-snap.h>
+#include <src/usb-manager.h>
#include <glib/gi18n.h> // bindtextdomain()
#include <gio/gio.h>
@@ -59,15 +58,10 @@ main(int /*argc*/, char** /*argv*/)
// We need the ADBD handler running,
// even though it doesn't have an indicator component yet
static constexpr char const * ADB_SOCKET_PATH {"/dev/socket/adb"};
- GAdbdClient adbd_client{ADB_SOCKET_PATH};
- adbd_client.on_pk_request().connect([](const AdbdClient::PKRequest& req){
- auto snap = new UsbSnap(req.fingerprint);
- snap->on_user_response().connect([req,snap](AdbdClient::PKResponse response, bool /*FIXME: remember_choice*/){
- req.respond(response);
- g_idle_add([](gpointer gsnap){delete static_cast<UsbSnap*>(gsnap); return G_SOURCE_REMOVE;}, snap); // delete-later
- });
- });
+ static constexpr char const * PUBLIC_KEYS_FILENAME {"/data/misc/adb/adb_keys"};
+ UsbManager usb_manager {ADB_SOCKET_PATH, PUBLIC_KEYS_FILENAME};
+ // let's go!
g_main_loop_run(loop);
// cleanup
diff --git a/src/usb-manager.cpp b/src/usb-manager.cpp
new file mode 100644
index 0000000..6b40cea
--- /dev/null
+++ b/src/usb-manager.cpp
@@ -0,0 +1,109 @@
+/*
+ * 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 /*FIXME: remember_choice*/){
+ req.respond(response);
+ if (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)
+ {
+ // 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()
+{
+}
+
diff --git a/src/usb-manager.h b/src/usb-manager.h
new file mode 100644
index 0000000..28a27f3
--- /dev/null
+++ b/src/usb-manager.h
@@ -0,0 +1,34 @@
+/*
+ * 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>
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+class UsbManager
+{
+public:
+ UsbManager(const std::string& socket_path, const std::string& public_key_filename);
+ ~UsbManager();
+
+protected:
+ class Impl;
+ std::unique_ptr<Impl> impl;
+};