Debian Codegraph @

utils/verify.c

raw
1 /*
2 * fprintd example to verify a fingerprint
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 <string.h>
24 #include <locale.h>
25 #include <gio/gio.h>
26 #include "fprintd-dbus.h"
27
28 static FprintDBusManager *manager = NULL;
29 static GDBusConnection *connection = NULL;
30 static char *finger_name = NULL;
31 static gboolean g_fatal_warnings = FALSE;
32 static char **usernames = NULL;
33
34 static void
35 create_manager (void)
36 {
37 g_autoptr(GError) error = NULL;
38
39 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
40 if (connection == NULL)
41 {
42 g_print ("Failed to connect to session bus: %s\n", error->message);
43 exit (1);
44 }
45
46 manager = fprint_dbus_manager_proxy_new_sync (connection,
47 G_DBUS_PROXY_FLAGS_NONE,
48 "net.reactivated.Fprint",
49 "/net/reactivated/Fprint/Manager",
50 NULL, &error);
51 if (manager == NULL)
52 {
53 g_print ("Failed to get Fprintd manager: %s\n", error->message);
54 exit (1);
55 }
56 }
57
58 static FprintDBusDevice *
59 open_device (const char *username)
60 {
61 g_autoptr(FprintDBusDevice) dev = NULL;
62 g_autoptr(GError) error = NULL;
63 g_autofree char *path = NULL;
64
65 if (!fprint_dbus_manager_call_get_default_device_sync (manager,
66 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
67 -1,
68 &path, NULL, &error))
69 {
70 g_print ("Impossible to verify: %s\n", error->message);
71 exit (1);
72 }
73
74 g_print ("Using device %s\n", path);
75
76 dev = fprint_dbus_device_proxy_new_sync (connection,
77 G_DBUS_PROXY_FLAGS_NONE,
78 "net.reactivated.Fprint",
79 path, NULL, &error);
80
81 if (error)
82 {
83 g_print ("failed to connect to device: %s\n", error->message);
84 exit (1);
85 }
86
87 if (!fprint_dbus_device_call_claim_sync (dev, username,
88 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
89 -1, NULL, &error))
90 {
91 g_print ("failed to claim device: %s\n", error->message);
92 exit (1);
93 }
94
95 return g_steal_pointer (&dev);
96 }
97
98 static void
99 find_finger (FprintDBusDevice *dev, const char *username)
100 {
101 g_autoptr(GError) error = NULL;
102 g_auto(GStrv) fingers = NULL;
103 guint i;
104
105 if (!fprint_dbus_device_call_list_enrolled_fingers_sync (dev, username,
106 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
107 -1,
108 &fingers,
109 NULL, &error))
110 {
111 g_print ("ListEnrolledFingers failed: %s\n", error->message);
112 exit (1);
113 }
114
115 if (fingers == NULL || g_strv_length (fingers) == 0)
116 {
117 g_print ("No fingers enrolled for this device.\n");
118 exit (1);
119 }
120
121 g_print ("Listing enrolled fingers:\n");
122 for (i = 0; fingers[i] != NULL; i++)
123 g_print (" - #%d: %s\n", i, fingers[i]);
124
125 if (finger_name && !g_str_equal (finger_name, "any") &&
126 !g_strv_contains ((const char **) fingers, finger_name))
127 {
128 g_print ("Finger '%s' not enrolled for user %s.\n", finger_name,
129 username);
130 g_free (finger_name);
131 exit (1);
132 }
133
134 if (finger_name == NULL)
135 finger_name = g_strdup (fingers[0]);
136 }
137
138 struct VerifyState
139 {
140 GError *error;
141 gboolean started;
142 gboolean completed;
143 gboolean match;
144 };
145
146 static void
147 verify_result (GObject *object, const char *result, gboolean done, void *user_data)
148 {
149 struct VerifyState *verify_state = user_data;
150
151 g_print ("Verify result: %s (%s)\n", result, done ? "done" : "not done");
152 verify_state->match = g_str_equal (result, "verify-match");
153
154 if (done != FALSE)
155 verify_state->completed = TRUE;
156 }
157
158 static void
159 verify_finger_selected (GObject *object, const char *name, void *user_data)
160 {
161 g_print ("Verifying: %s\n", name);
162 }
163
164 static void
165 verify_started_cb (GObject *obj,
166 GAsyncResult *res,
167 gpointer user_data)
168 {
169 struct VerifyState *verify_state = user_data;
170
171 if (fprint_dbus_device_call_verify_start_finish (FPRINT_DBUS_DEVICE (obj), res, &verify_state->error))
172 {
173 g_print ("Verify started!\n");
174 verify_state->started = TRUE;
175 }
176 }
177
178 static void
179 proxy_signal_cb (GDBusProxy *proxy,
180 const gchar *sender_name,
181 const gchar *signal_name,
182 GVariant *parameters,
183 gpointer user_data)
184 {
185 struct VerifyState *verify_state = user_data;
186
187 if (!verify_state->started)
188 return;
189
190 if (g_str_equal (signal_name, "VerifyStatus"))
191 {
192 const gchar *result;
193 gboolean done;
194
195 g_variant_get (parameters, "(&sb)", &result, &done);
196 verify_result (G_OBJECT (proxy), result, done, user_data);
197 }
198 else if (g_str_equal (signal_name, "VerifyFingerSelected"))
199 {
200 const gchar *name;
201
202 g_variant_get (parameters, "(&s)", &name);
203 verify_finger_selected (G_OBJECT (proxy), name, user_data);
204 }
205 }
206
207 static gboolean
208 do_verify (FprintDBusDevice *dev)
209 {
210 g_autoptr(GError) error = NULL;
211 struct VerifyState verify_state = { 0 };
212
213 /* This one is funny. We connect to the signal immediately to avoid
214 * race conditions. However, we must ignore any authentication results
215 * that happen before our start call returns.
216 * This is because the verify call itself may internally try to verify
217 * against fprintd (possibly using a separate account).
218 *
219 * To do so, we *must* use the async version of the verify call, as the
220 * sync version would cause the signals to be queued and only processed
221 * after it returns.
222 */
223
224 g_signal_connect (dev, "g-signal", G_CALLBACK (proxy_signal_cb),
225 &verify_state);
226
227 fprint_dbus_device_call_verify_start (dev, finger_name,
228 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
229 -1,
230 NULL,
231 verify_started_cb,
232 &verify_state);
233
234 /* Wait for verify start while discarding any VerifyStatus signals */
235 while (!verify_state.started && !verify_state.error)
236 g_main_context_iteration (NULL, TRUE);
237
238 if (verify_state.error)
239 {
240 g_print ("VerifyStart failed: %s\n", verify_state.error->message);
241 g_clear_error (&verify_state.error);
242 exit (1);
243 }
244
245 /* VerifyStatus signals are processing, wait for completion. */
246 while (!verify_state.completed)
247 g_main_context_iteration (NULL, TRUE);
248
249
250 g_signal_handlers_disconnect_by_func (dev, proxy_signal_cb,
251 &verify_state);
252
253 if (!fprint_dbus_device_call_verify_stop_sync (dev,
254 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
255 -1,
256 NULL, &error))
257 {
258 g_print ("VerifyStop failed: %s\n", error->message);
259 exit (1);
260 }
261
262 return verify_state.match;
263 }
264
265 static void
266 release_device (FprintDBusDevice *dev)
267 {
268 g_autoptr(GError) error = NULL;
269 if (!fprint_dbus_device_call_release_sync (dev,
270 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
271 -1,
272 NULL, &error))
273 {
274 g_print ("ReleaseDevice failed: %s\n", error->message);
275 exit (1);
276 }
277 }
278
279 static const GOptionEntry entries[] = {
280 { "finger", 'f', 0, G_OPTION_ARG_STRING, &finger_name, "Finger selected to verify (default is automatic)", NULL },
281 {"g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, "Make all warnings fatal", NULL},
282 { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &usernames, NULL, "[username]" },
283 { NULL }
284 };
285
286 int
287 main (int argc, char **argv)
288 {
289 g_autoptr(FprintDBusDevice) dev = NULL;
290 g_autoptr(GError) err = NULL;
291 GOptionContext *context;
292 const char *username = NULL;
293 gboolean match;
294
295 setlocale (LC_ALL, "");
296
297 context = g_option_context_new ("Verify a fingerprint");
298 g_option_context_add_main_entries (context, entries, NULL);
299
300 if (g_option_context_parse (context, &argc, &argv, &err) == FALSE)
301 {
302 g_print ("couldn't parse command-line options: %s\n", err->message);
303 return 1;
304 }
305
306 if (usernames == NULL)
307 username = "";
308 else
309 username = usernames[0];
310
311 if (g_fatal_warnings)
312 {
313 GLogLevelFlags fatal_mask;
314
315 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
316 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
317 g_log_set_always_fatal (fatal_mask);
318 }
319
320 create_manager ();
321
322 dev = open_device (username);
323 find_finger (dev, username);
324 match = do_verify (dev);
325 release_device (dev);
326 return match ? EXIT_SUCCESS : EXIT_FAILURE;
327 }