| 1 |
/* |
| 2 |
* fprintd example to list enrolled fingerprints |
| 3 |
* Copyright (C) 2008 Daniel Drake <[email protected]> |
| 4 |
* Copyright (C) 2020 Marco Trevisan <[email protected]> |
| 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 as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License along |
| 17 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <locale.h> |
| 24 |
#include "fprintd-dbus.h" |
| 25 |
|
| 26 |
static FprintDBusManager *manager = NULL; |
| 27 |
static GDBusConnection *connection = NULL; |
| 28 |
|
| 29 |
static void |
| 30 |
create_manager (void) |
| 31 |
{ |
| 32 |
g_autoptr(GError) error = NULL; |
| 33 |
|
| 34 |
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); |
| 35 |
if (connection == NULL) |
| 36 |
{ |
| 37 |
g_print ("Failed to connect to session bus: %s\n", error->message); |
| 38 |
exit (1); |
| 39 |
} |
| 40 |
|
| 41 |
manager = fprint_dbus_manager_proxy_new_sync (connection, |
| 42 |
G_DBUS_PROXY_FLAGS_NONE, |
| 43 |
"net.reactivated.Fprint", |
| 44 |
"/net/reactivated/Fprint/Manager", |
| 45 |
NULL, &error); |
| 46 |
if (manager == NULL) |
| 47 |
{ |
| 48 |
g_print ("Failed to get Fprintd manager: %s\n", error->message); |
| 49 |
exit (1); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static void |
| 54 |
list_fingerprints (FprintDBusDevice *dev, const char *username) |
| 55 |
{ |
| 56 |
g_autoptr(GError) error = NULL; |
| 57 |
g_auto(GStrv) fingers = NULL; |
| 58 |
guint i; |
| 59 |
|
| 60 |
if (!fprint_dbus_device_call_list_enrolled_fingers_sync (dev, username, |
| 61 |
G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, |
| 62 |
-1, &fingers, NULL, |
| 63 |
&error)) |
| 64 |
{ |
| 65 |
gboolean ignore_error = FALSE; |
| 66 |
if (g_dbus_error_is_remote_error (error)) |
| 67 |
{ |
| 68 |
g_autofree char *dbus_error = |
| 69 |
g_dbus_error_get_remote_error (error); |
| 70 |
if (g_str_equal (dbus_error, |
| 71 |
"net.reactivated.Fprint.Error.NoEnrolledPrints")) |
| 72 |
ignore_error = TRUE; |
| 73 |
} |
| 74 |
|
| 75 |
if (!ignore_error) |
| 76 |
{ |
| 77 |
g_print ("ListEnrolledFingers failed: %s\n", error->message); |
| 78 |
exit (1); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if (fingers == NULL || g_strv_length (fingers) == 0) |
| 83 |
{ |
| 84 |
g_print ("User %s has no fingers enrolled for %s.\n", username, |
| 85 |
fprint_dbus_device_get_name (dev)); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
g_print ("Fingerprints for user %s on %s (%s):\n", |
| 90 |
username, |
| 91 |
fprint_dbus_device_get_name (dev), |
| 92 |
fprint_dbus_device_get_scan_type (dev)); |
| 93 |
|
| 94 |
for (i = 0; fingers[i] != NULL; i++) |
| 95 |
g_print (" - #%d: %s\n", i, fingers[i]); |
| 96 |
} |
| 97 |
|
| 98 |
static void |
| 99 |
process_devices (char **argv) |
| 100 |
{ |
| 101 |
g_auto(GStrv) devices = NULL; |
| 102 |
g_autoptr(GError) error = NULL; |
| 103 |
char *path; |
| 104 |
guint num_devices; |
| 105 |
guint i; |
| 106 |
|
| 107 |
if (!fprint_dbus_manager_call_get_devices_sync (manager, |
| 108 |
G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, |
| 109 |
-1, |
| 110 |
&devices, NULL, &error)) |
| 111 |
{ |
| 112 |
g_print ("Impossible to get devices: %s\n", error->message); |
| 113 |
exit (1); |
| 114 |
} |
| 115 |
|
| 116 |
num_devices = g_strv_length (devices); |
| 117 |
if (num_devices == 0) |
| 118 |
{ |
| 119 |
g_print ("No devices available\n"); |
| 120 |
exit (1); |
| 121 |
} |
| 122 |
|
| 123 |
g_print ("found %u devices\n", num_devices); |
| 124 |
for (i = 0; devices[i] != NULL; i++) |
| 125 |
{ |
| 126 |
path = devices[i]; |
| 127 |
g_print ("Device at %s\n", path); |
| 128 |
} |
| 129 |
|
| 130 |
for (i = 0; devices[i] != NULL; i++) |
| 131 |
{ |
| 132 |
g_autoptr(FprintDBusDevice) dev = NULL; |
| 133 |
guint j; |
| 134 |
|
| 135 |
path = devices[i]; |
| 136 |
g_print ("Using device %s\n", path); |
| 137 |
|
| 138 |
/* NOTE: We should handle error cases! */ |
| 139 |
dev = fprint_dbus_device_proxy_new_sync (connection, |
| 140 |
G_DBUS_PROXY_FLAGS_NONE, |
| 141 |
"net.reactivated.Fprint", |
| 142 |
path, NULL, NULL); |
| 143 |
|
| 144 |
for (j = 1; argv[j] != NULL; j++) |
| 145 |
list_fingerprints (dev, argv[j]); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
int |
| 150 |
main (int argc, char **argv) |
| 151 |
{ |
| 152 |
setlocale (LC_ALL, ""); |
| 153 |
|
| 154 |
create_manager (); |
| 155 |
|
| 156 |
if (argc < 2) |
| 157 |
{ |
| 158 |
g_print ("Usage: %s <username> [usernames...]\n", argv[0]); |
| 159 |
return 1; |
| 160 |
} |
| 161 |
|
| 162 |
process_devices (argv); |
| 163 |
|
| 164 |
return 0; |
| 165 |
} |