Debian Codegraph @

src/file_storage.c

raw
1 /*
2 * Simple file storage for fprintd
3 * Copyright (C) 2007 Daniel Drake <[email protected]>
4 * Copyright (C) 2008 Vasily Khoruzhick <[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
22
23 /* FIXME:
24 * This file almost duplicate data.c from libfprint
25 * Maybe someday data.c will be upgraded to this one ;)
26 */
27
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include <glib.h>
36 #include <glib/gstdio.h>
37
38 #include <fprint.h>
39
40 #include "file_storage.h"
41
42 #define FILE_STORAGE_PATH "/var/lib/fprint"
43 #define DIR_PERMS 0700
44
45 static char *storage_path = NULL;
46
47 static const char *
48 get_storage_path (void)
49 {
50 const char *path = NULL;
51
52 if (storage_path != NULL)
53 return storage_path;
54
55 /* set by systemd >= 240 to an absolute path
56 * taking into account the StateDirectory
57 * unit file setting */
58 path = g_getenv ("STATE_DIRECTORY");
59 if (path != NULL)
60 {
61 /* If multiple directories are set, then in the environment variable
62 * the paths are concatenated with colon (":"). */
63 if (strchr (path, ':'))
64 {
65 g_auto(GStrv) elems = NULL;
66 elems = g_strsplit (path, ":", -1);
67 storage_path = g_strdup (elems[0]);
68 }
69 else if (*path)
70 {
71 storage_path = g_strdup (path);
72 }
73 }
74
75 if (storage_path == NULL)
76 storage_path = g_strdup (FILE_STORAGE_PATH);
77
78 return storage_path;
79 }
80
81 static char *
82 get_path_to_storedir (const char *driver, const char * device_id, char *base_store)
83 {
84 return g_build_filename (base_store, driver, device_id, NULL);
85 }
86
87 static char *
88 __get_path_to_print (const char *driver, const char * device_id,
89 FpFinger finger, char *base_store)
90 {
91 g_autofree char *dirpath = NULL;
92 char *path;
93 char fingername[2];
94
95 g_snprintf (fingername, 2, "%x", finger);
96
97 dirpath = get_path_to_storedir (driver, device_id, base_store);
98 path = g_build_filename (dirpath, fingername, NULL);
99 return path;
100 }
101
102 static char *
103 get_path_to_print (FpDevice *dev, FpFinger finger, char *base_store)
104 {
105 return __get_path_to_print (fp_device_get_driver (dev),
106 fp_device_get_device_id (dev),
107 finger,
108 base_store);
109 }
110
111 static char *
112 get_path_to_print_dscv (FpDevice *dev, FpFinger finger, char *base_store)
113 {
114 return __get_path_to_print (fp_device_get_driver (dev),
115 fp_device_get_device_id (dev),
116 finger,
117 base_store);
118 }
119
120 static char *
121 file_storage_get_basestore_for_username (const char *username)
122 {
123 return g_build_filename (get_storage_path (), username, NULL);
124 }
125
126 int
127 file_storage_print_data_save (FpPrint *print)
128 {
129 g_autoptr(GError) err = NULL;
130 g_autofree char *path = NULL;
131 g_autofree char *dirpath = NULL;
132 g_autofree char *base_store = NULL;
133 g_autofree char *buf = NULL;
134 gsize len;
135 int r;
136
137 base_store = file_storage_get_basestore_for_username (fp_print_get_username (print));
138
139 if (!fp_print_serialize (print, (guchar **) &buf, &len, &err))
140 {
141 g_warning ("Error serializing data: %s", err->message);
142 return -ENOMEM;
143 }
144
145 path = __get_path_to_print (fp_print_get_driver (print),
146 fp_print_get_device_id (print),
147 fp_print_get_finger (print),
148 base_store);
149 dirpath = g_path_get_dirname (path);
150 r = g_mkdir_with_parents (dirpath, DIR_PERMS);
151 if (r < 0)
152 {
153 g_debug ("file_storage_print_data_save(): could not mkdir(\"%s\"): %s",
154 dirpath, g_strerror (r));
155 return r;
156 }
157
158 g_file_set_contents (path, buf, len, &err);
159 if (err)
160 {
161 g_debug ("file_storage_print_data_save(): could not save '%s': %s",
162 path, err->message);
163 /* FIXME interpret error codes */
164 return err->code;
165 }
166
167 g_debug ("file_storage_print_data_save(): print saved to %s", path);
168
169 return 0;
170 }
171
172 static int
173 load_from_file (char *path, FpPrint **print)
174 {
175 g_autoptr(GError) err = NULL;
176 gsize length;
177 g_autofree char *contents = NULL;
178 FpPrint *new;
179
180 g_file_get_contents (path, &contents, &length, &err);
181 if (err)
182 {
183 int r = err->code;
184 /* FIXME interpret more error codes */
185 if (r == G_FILE_ERROR_NOENT)
186 return -ENOENT;
187 else
188 return r;
189 }
190
191 new = fp_print_deserialize ((guchar *) contents, length, &err);
192 if (!new)
193 {
194 g_print ("Error deserializing data: %s", err->message);
195 return -EIO;
196 }
197
198 *print = new;
199 return 0;
200 }
201
202 int
203 file_storage_print_data_load (FpDevice *dev,
204 FpFinger finger,
205 const char *username,
206 FpPrint **print)
207 {
208 g_autofree gchar *path = NULL;
209 g_autofree gchar *base_store = NULL;
210
211 g_autoptr(FpPrint) new = NULL;
212 int r;
213
214 base_store = file_storage_get_basestore_for_username (username);
215
216 path = get_path_to_print (dev, finger, base_store);
217 r = load_from_file (path, &new);
218 g_debug ("file_storage_print_data_load(): loaded '%s' %s",
219 path, g_strerror (r));
220 if (r)
221 return r;
222
223 /* Make sure the username/finger matches our expectations. */
224 if (fp_print_get_finger (new) != finger)
225 return -EINVAL;
226
227 if (g_strcmp0 (fp_print_get_username (new), username) != 0)
228 return -EINVAL;
229
230 /* And that the print is compatible with the device. */
231 if (!fp_print_compatible (new, dev))
232 return -EINVAL;
233
234 *print = g_steal_pointer (&new);
235 return 0;
236 }
237
238 int
239 file_storage_print_data_delete (FpDevice *dev, FpFinger finger, const char *username)
240 {
241 g_autoptr(GSList) prints = NULL;
242 g_autofree gchar *base_store = NULL;
243 g_autofree gchar *path = NULL;
244 int r;
245
246 base_store = file_storage_get_basestore_for_username (username);
247
248 path = get_path_to_print_dscv (dev, finger, base_store);
249
250 if (!g_file_test (path, G_FILE_TEST_EXISTS))
251 return 0;
252
253 r = g_unlink (path);
254 g_debug ("file_storage_print_data_delete(): unlink(\"%s\") %s",
255 path, g_strerror (r));
256
257 prints = file_storage_discover_prints (dev, username);
258 if (!prints)
259 {
260 g_autofree char *dir = g_steal_pointer (&path);
261
262 do
263 {
264 g_autofree char *tmp = g_steal_pointer (&dir);
265 dir = g_path_get_dirname (tmp);
266 }
267 while (g_str_has_prefix (dir, base_store) && g_rmdir (dir) == 0);
268 }
269
270 return r;
271 }
272
273 static GSList *
274 scan_dev_storedir (char *devpath,
275 GSList *list)
276 {
277 g_autoptr(GError) err = NULL;
278 const gchar *ent;
279
280 GDir *dir = g_dir_open (devpath, 0, &err);
281
282 if (!dir)
283 {
284 g_debug ("scan_dev_storedir(): opendir(\"%s\") failed: %s", devpath, err->message);
285 return list;
286 }
287
288 while ((ent = g_dir_read_name (dir)))
289 {
290 /* ent is an 1 hex character fp_finger code */
291 guint64 val;
292 gchar *endptr;
293
294 if (*ent == 0 || strlen (ent) != 1)
295 continue;
296
297 val = g_ascii_strtoull (ent, &endptr, 16);
298 if (endptr == ent || !FP_FINGER_IS_VALID (val))
299 {
300 g_debug ("scan_dev_storedir(): skipping print file '%s'", ent);
301 continue;
302 }
303
304 list = g_slist_prepend (list, GUINT_TO_POINTER (val));
305 }
306
307 g_dir_close (dir);
308 return list;
309 }
310
311 GSList *
312 file_storage_discover_prints (FpDevice *dev, const char *username)
313 {
314 GSList *list = NULL;
315 g_autofree gchar *base_store = NULL;
316 g_autofree gchar *storedir = NULL;
317
318 base_store = file_storage_get_basestore_for_username (username);
319
320 storedir = get_path_to_storedir (fp_device_get_driver (dev),
321 fp_device_get_device_id (dev),
322 base_store);
323
324 g_debug ("file_storage_discover_prints() for user '%s' in '%s'",
325 username, storedir);
326
327 list = scan_dev_storedir (storedir, list);
328
329 return list;
330 }
331
332 GSList *
333 file_storage_discover_users (void)
334 {
335 g_autoptr(GError) err = NULL;
336 GSList *list = NULL;
337 const gchar *ent;
338 GDir *dir = g_dir_open (get_storage_path (), 0, &err);
339
340 if (!dir)
341 return list;
342
343 while ((ent = g_dir_read_name (dir)))
344 {
345 /* ent is a username */
346 if (*ent == 0)
347 continue;
348
349 list = g_slist_prepend (list, g_strdup (ent));
350 }
351
352 g_dir_close (dir);
353 return list;
354 }
355
356 int
357 file_storage_init (void)
358 {
359 /* Nothing to do */
360 return 0;
361 }
362
363 int
364 file_storage_deinit (void)
365 {
366 g_clear_pointer (&storage_path, g_free);
367 return 0;
368 }