Debian Codegraph @

utils/enroll.c

raw
1 /*
2 * fprintd example to enroll right index finger
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 #define _GNU_SOURCE
22 #include <stdlib.h>
23 #include <string.h>
24 #include <locale.h>
25 #include "fprintd-dbus.h"
26
27 #define N_(x) x
28 #define TR(x) x
29 #include "fingerprint-strings.h"
30
31 static FprintDBusManager *manager = NULL;
32 static GDBusConnection *connection = NULL;
33 static char *finger_name = NULL;
34 static char **usernames = NULL;
35
36 typedef enum {
37 ENROLL_INCOMPLETE,
38 ENROLL_COMPLETED,
39 ENROLL_FAILED,
40 } FprintEnrollStatus;
41
42 static void
43 create_manager (void)
44 {
45 g_autoptr(GError) error = NULL;
46
47 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
48 if (connection == NULL)
49 {
50 g_print ("Failed to connect to session bus: %s\n", error->message);
51 exit (1);
52 }
53
54 manager = fprint_dbus_manager_proxy_new_sync (connection,
55 G_DBUS_PROXY_FLAGS_NONE,
56 "net.reactivated.Fprint",
57 "/net/reactivated/Fprint/Manager",
58 NULL, &error);
59 if (manager == NULL)
60 {
61 g_print ("Failed to get Fprintd manager: %s\n", error->message);
62 exit (1);
63 }
64 }
65
66 static FprintDBusDevice *
67 open_device (const char *username)
68 {
69 g_autoptr(FprintDBusDevice) dev = NULL;
70 g_autoptr(GError) error = NULL;
71 g_autofree char *path = NULL;
72
73 if (!fprint_dbus_manager_call_get_default_device_sync (manager,
74 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
75 -1, &path, NULL, &error))
76 {
77 g_print ("Impossible to enroll: %s\n", error->message);
78 exit (1);
79 }
80
81 g_print ("Using device %s\n", path);
82
83 dev = fprint_dbus_device_proxy_new_sync (connection,
84 G_DBUS_PROXY_FLAGS_NONE,
85 "net.reactivated.Fprint",
86 path, NULL, &error);
87
88 if (error)
89 {
90 g_print ("failed to connect to device: %s\n", error->message);
91 exit (1);
92 }
93
94 if (!fprint_dbus_device_call_claim_sync (dev, username,
95 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
96 -1, NULL, &error))
97 {
98 g_print ("failed to claim device: %s\n", error->message);
99 exit (1);
100 }
101 return g_steal_pointer (&dev);
102 }
103
104 static void
105 enroll_result (GObject *object, const char *result, gboolean done, void *user_data)
106 {
107 FprintEnrollStatus *enroll_status = user_data;
108
109 g_print ("Enroll result: %s\n", result);
110 if (done != FALSE)
111 {
112 if (g_str_equal (result, "enroll-completed"))
113 *enroll_status = ENROLL_COMPLETED;
114 else
115 *enroll_status = ENROLL_FAILED;
116 }
117 }
118
119 static void
120 proxy_signal_cb (GDBusProxy *proxy,
121 const gchar *sender_name,
122 const gchar *signal_name,
123 GVariant *parameters,
124 gpointer user_data)
125 {
126 if (g_str_equal (signal_name, "EnrollStatus"))
127 {
128 const gchar *result;
129 gboolean done;
130
131 g_variant_get (parameters, "(&sb)", &result, &done);
132 enroll_result (G_OBJECT (proxy), result, done, user_data);
133 }
134 }
135
136 static FprintEnrollStatus
137 do_enroll (FprintDBusDevice *dev)
138 {
139 g_autoptr(GError) error = NULL;
140 FprintEnrollStatus enroll_status = ENROLL_INCOMPLETE;
141 gboolean found;
142 guint i;
143
144 g_signal_connect (dev, "g-signal", G_CALLBACK (proxy_signal_cb),
145 &enroll_status);
146
147 found = FALSE;
148 for (i = 0; fingers[i].dbus_name != NULL; i++)
149 {
150 if (g_strcmp0 (fingers[i].dbus_name, finger_name) == 0)
151 {
152 found = TRUE;
153 break;
154 }
155 }
156 if (!found)
157 {
158 g_autoptr(GString) s = NULL;
159
160 s = g_string_new (NULL);
161 g_string_append_printf (s, "Invalid finger name '%s'. Name must be one of ", finger_name);
162 for (i = 0; fingers[i].dbus_name != NULL; i++)
163 {
164 g_string_append_printf (s, "%s", fingers[i].dbus_name);
165 if (fingers[i + 1].dbus_name != NULL)
166 g_string_append (s, ", ");
167 }
168 g_warning ("%s", s->str);
169 exit (1);
170 }
171
172 g_print ("Enrolling %s finger.\n", finger_name);
173 if (!fprint_dbus_device_call_enroll_start_sync (dev, finger_name,
174 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
175 -1,
176 NULL, &error))
177 {
178 g_print ("EnrollStart failed: %s\n", error->message);
179 exit (1);
180 }
181
182 while (enroll_status == ENROLL_INCOMPLETE)
183 g_main_context_iteration (NULL, TRUE);
184
185 g_signal_handlers_disconnect_by_func (dev, proxy_signal_cb, &enroll_status);
186
187 if (!fprint_dbus_device_call_enroll_stop_sync (dev,
188 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
189 -1, NULL, &error))
190 {
191 g_print ("EnrollStop failed: %s\n", error->message);
192 exit (1);
193 }
194
195 return enroll_status;
196 }
197
198 static void
199 release_device (FprintDBusDevice *dev)
200 {
201 g_autoptr(GError) error = NULL;
202 if (!fprint_dbus_device_call_release_sync (dev,
203 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
204 -1, NULL, &error))
205 {
206 g_print ("ReleaseDevice failed: %s\n", error->message);
207 exit (1);
208 }
209 }
210
211 static const GOptionEntry entries[] = {
212 { "finger", 'f', 0, G_OPTION_ARG_STRING, &finger_name, "Finger selected to verify (default is automatic)", NULL },
213 { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &usernames, NULL, "[username]" },
214 { NULL }
215 };
216
217 int
218 main (int argc, char **argv)
219 {
220 g_autoptr(FprintDBusDevice) dev = NULL;
221 GOptionContext *context;
222 FprintEnrollStatus status;
223
224 g_autoptr(GError) err = NULL;
225
226 setlocale (LC_ALL, "");
227
228 context = g_option_context_new ("Enroll a fingerprint");
229 g_option_context_add_main_entries (context, entries, NULL);
230
231 if (g_option_context_parse (context, &argc, &argv, &err) == FALSE)
232 {
233 g_print ("couldn't parse command-line options: %s\n", err->message);
234 return 1;
235 }
236
237 if (finger_name == NULL)
238 finger_name = g_strdup ("right-index-finger");
239
240 create_manager ();
241
242 dev = open_device (usernames ? usernames[0] : "");
243 status = do_enroll (dev);
244 release_device (dev);
245 g_free (finger_name);
246 g_strfreev (usernames);
247 return status == ENROLL_COMPLETED ? EXIT_SUCCESS : EXIT_FAILURE;
248 }