| 1 |
/* |
| 2 |
* fprintd example to delete 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 gboolean |
| 54 |
delete_user_prints (FprintDBusDevice *dev, |
| 55 |
const char *fingername, |
| 56 |
GError **error) |
| 57 |
{ |
| 58 |
if (fingername) |
| 59 |
return fprint_dbus_device_call_delete_enrolled_finger_sync (dev, fingername, |
| 60 |
G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, |
| 61 |
-1, NULL, error); |
| 62 |
else |
| 63 |
return fprint_dbus_device_call_delete_enrolled_fingers2_sync (dev, |
| 64 |
G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, |
| 65 |
-1, NULL, error); |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
delete_fingerprints (FprintDBusDevice *dev, |
| 70 |
const char *username, |
| 71 |
const char *fingername) |
| 72 |
{ |
| 73 |
g_autoptr(GError) error = NULL; |
| 74 |
|
| 75 |
if (!fprint_dbus_device_call_claim_sync (dev, username, |
| 76 |
G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, |
| 77 |
-1, NULL, &error)) |
| 78 |
{ |
| 79 |
g_print ("failed to claim device: %s\n", error->message); |
| 80 |
exit (1); |
| 81 |
} |
| 82 |
|
| 83 |
if (!delete_user_prints (dev, fingername, &error)) |
| 84 |
{ |
| 85 |
gboolean ignore_error = FALSE; |
| 86 |
if (g_dbus_error_is_remote_error (error)) |
| 87 |
{ |
| 88 |
g_autofree char *dbus_error = |
| 89 |
g_dbus_error_get_remote_error (error); |
| 90 |
if (g_str_equal (dbus_error, |
| 91 |
"net.reactivated.Fprint.Error.NoEnrolledPrints")) |
| 92 |
{ |
| 93 |
g_print ("No fingerprints to delete on %s\n", |
| 94 |
fprint_dbus_device_get_name (dev)); |
| 95 |
ignore_error = TRUE; |
| 96 |
} |
| 97 |
} |
| 98 |
if (!ignore_error) |
| 99 |
{ |
| 100 |
g_print ("Failed to delete fingerprints: %s\n", |
| 101 |
error->message); |
| 102 |
exit (1); |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
g_print ("No fingerprints to delete on %s\n", |
| 107 |
fprint_dbus_device_get_name (dev)); |
| 108 |
} |
| 109 |
} |
| 110 |
else |
| 111 |
{ |
| 112 |
if (fingername) |
| 113 |
g_print ("Fingerprint %s of user %s deleted on %s\n", |
| 114 |
fingername, username, |
| 115 |
fprint_dbus_device_get_name (dev)); |
| 116 |
else |
| 117 |
g_print ("Fingerprints of user %s deleted on %s\n", username, |
| 118 |
fprint_dbus_device_get_name (dev)); |
| 119 |
} |
| 120 |
g_clear_error (&error); |
| 121 |
|
| 122 |
if (!fprint_dbus_device_call_release_sync (dev, |
| 123 |
G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, |
| 124 |
-1, NULL, &error)) |
| 125 |
{ |
| 126 |
g_print ("ReleaseDevice failed: %s\n", error->message); |
| 127 |
exit (1); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
process_devices (guint argc, char **argv) |
| 133 |
{ |
| 134 |
g_autoptr(GOptionContext) option_context = NULL; |
| 135 |
g_autoptr(GError) error = NULL; |
| 136 |
g_auto(GStrv) devices = NULL; |
| 137 |
char *fingername = NULL; |
| 138 |
char *path; |
| 139 |
guint num_devices; |
| 140 |
guint i; |
| 141 |
|
| 142 |
if (!fprint_dbus_manager_call_get_devices_sync (manager, |
| 143 |
G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION, |
| 144 |
-1, &devices, NULL, &error)) |
| 145 |
{ |
| 146 |
g_print ("Impossible to get devices: %s\n", error->message); |
| 147 |
exit (1); |
| 148 |
} |
| 149 |
|
| 150 |
num_devices = g_strv_length (devices); |
| 151 |
if (num_devices == 0) |
| 152 |
{ |
| 153 |
g_print ("No devices available\n"); |
| 154 |
exit (1); |
| 155 |
} |
| 156 |
|
| 157 |
g_print ("found %u devices\n", num_devices); |
| 158 |
for (i = 0; devices[i] != NULL; i++) |
| 159 |
{ |
| 160 |
path = devices[i]; |
| 161 |
g_print ("Device at %s\n", path); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
const GOptionEntry user_options[] = { |
| 166 |
{ "finger", 'f', 0, G_OPTION_ARG_STRING, &fingername, "Finger selected to verify (default is automatic)", NULL }, |
| 167 |
{ NULL } |
| 168 |
}; |
| 169 |
|
| 170 |
option_context = g_option_context_new (NULL); |
| 171 |
g_option_context_add_main_entries (option_context, user_options, NULL); |
| 172 |
|
| 173 |
for (i = 0; devices[i] != NULL; i++) |
| 174 |
{ |
| 175 |
g_autoptr(FprintDBusDevice) dev = NULL; |
| 176 |
guint j; |
| 177 |
|
| 178 |
path = devices[i]; |
| 179 |
g_print ("Using device %s\n", path); |
| 180 |
|
| 181 |
/* NOTE: We should handle error cases! */ |
| 182 |
dev = fprint_dbus_device_proxy_new_sync (connection, |
| 183 |
G_DBUS_PROXY_FLAGS_NONE, |
| 184 |
"net.reactivated.Fprint", |
| 185 |
path, NULL, NULL); |
| 186 |
|
| 187 |
for (j = 1; argv[j] != NULL; j++) |
| 188 |
{ |
| 189 |
const char *username = argv[j]; |
| 190 |
fingername = NULL; |
| 191 |
|
| 192 |
if (argc > j + 1 && argv[j + 1][0] == '-') |
| 193 |
{ |
| 194 |
g_autoptr(GError) local_error = NULL; |
| 195 |
g_autoptr(GPtrArray) user_args = NULL; |
| 196 |
|
| 197 |
user_args = g_ptr_array_new_full (3, NULL); |
| 198 |
g_ptr_array_add (user_args, argv[j]); |
| 199 |
g_ptr_array_add (user_args, argv[j + 1]); |
| 200 |
|
| 201 |
if (argc > j + 2) |
| 202 |
g_ptr_array_add (user_args, argv[j + 2]); |
| 203 |
|
| 204 |
int new_argc = user_args->len; |
| 205 |
char **new_argv = (char **) user_args->pdata; |
| 206 |
|
| 207 |
if (!g_option_context_parse (option_context, &new_argc, |
| 208 |
&new_argv, &local_error)) |
| 209 |
{ |
| 210 |
g_print ("couldn't parse command-line options: %s\n", |
| 211 |
local_error->message); |
| 212 |
j += 1; |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
j += 2; |
| 217 |
} |
| 218 |
|
| 219 |
delete_fingerprints (dev, username, fingername); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
int |
| 225 |
main (int argc, char **argv) |
| 226 |
{ |
| 227 |
g_autoptr(GOptionContext) option_context = NULL; |
| 228 |
g_autoptr(GError) local_error = NULL; |
| 229 |
|
| 230 |
option_context = g_option_context_new ("Delete fingerprints"); |
| 231 |
|
| 232 |
setlocale (LC_ALL, ""); |
| 233 |
|
| 234 |
g_option_context_set_ignore_unknown_options (option_context, TRUE); |
| 235 |
g_option_context_set_summary (option_context, |
| 236 |
"<username> [-f finger-name [usernames [-f finger-name ]...]"); |
| 237 |
|
| 238 |
if (!g_option_context_parse (option_context, &argc, &argv, &local_error)) |
| 239 |
{ |
| 240 |
g_print ("couldn't parse command-line options: %s\n", local_error->message); |
| 241 |
return EXIT_FAILURE; |
| 242 |
} |
| 243 |
|
| 244 |
if (argc < 2) |
| 245 |
{ |
| 246 |
g_autofree char *usage = NULL; |
| 247 |
|
| 248 |
usage = g_option_context_get_help (option_context, FALSE, NULL); |
| 249 |
g_print ("%s", usage); |
| 250 |
return EXIT_FAILURE; |
| 251 |
} |
| 252 |
|
| 253 |
create_manager (); |
| 254 |
process_devices (argc, argv); |
| 255 |
|
| 256 |
return 0; |
| 257 |
} |