Port pamusb-agent and pamusb-conf to UDisks.
[pam_usb.git] / src / device.c
blob3b4e6522762942109b8de04d76672e11a4d0c0a8
1 /*
2 * Copyright (c) 2003-2007 Andrea Luzzardi <scox@sig11.org>
4 * This file is part of the pam_usb project. pam_usb is free software;
5 * you can redistribute it and/or modify it under the terms of the GNU General
6 * Public License version 2, as published by the Free Software Foundation.
8 * pam_usb is distributed in the hope that it will be useful, but WITHOUT ANY
9 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 * details.
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <unistd.h>
19 #include <string.h>
20 #include <dbus/dbus.h>
21 #include <libhal-storage.h>
22 #include "conf.h"
23 #include "hal.h"
24 #include "log.h"
25 #include "pad.h"
26 #include "device.h"
28 static int pusb_device_connected(t_pusb_options *opts, LibHalContext *ctx)
30 char *udi = NULL;
32 log_debug("Searching for \"%s\" in the hardware database...\n",
33 opts->device.name);
34 udi = pusb_hal_find_item(ctx,
35 "storage.serial", opts->device.serial,
36 "storage.vendor", opts->device.vendor,
37 "info.product", opts->device.model,
38 NULL);
39 if (!udi)
41 /* Backward compatibility with older versions (0.4.1) */
42 udi = pusb_hal_find_item(ctx,
43 "usb_device.serial", opts->device.serial,
44 "usb_device.vendor", opts->device.vendor,
45 "info.product", opts->device.model,
46 NULL);
47 if (!udi)
49 log_error("Device \"%s\" is not connected.\n",
50 opts->device.name);
51 return (0);
53 log_error("Warning: The configuration file has been " \
54 "generated by an older version of pamusb-conf\n");
56 libhal_free_string(udi);
57 log_info("Device \"%s\" is connected (good).\n", opts->device.name);
58 return (1);
61 int pusb_device_check(t_pusb_options *opts,
62 const char *user)
64 DBusConnection *dbus = NULL;
65 LibHalContext *ctx = NULL;
66 int retval = 0;
68 log_debug("Connecting to HAL...\n");
69 if (!(dbus = pusb_hal_dbus_connect()))
70 return (0);
72 if (!(ctx = pusb_hal_init(dbus)))
74 pusb_hal_dbus_disconnect(dbus);
75 return (0);
78 if (!pusb_device_connected(opts, ctx))
80 pusb_hal_dbus_disconnect(dbus);
81 libhal_ctx_free(ctx);
82 return (0);
85 if (opts->one_time_pad)
87 log_info("Performing one time pad verification...\n");
88 retval = pusb_pad_check(opts, ctx, user);
90 else
92 log_debug("One time pad is disabled, no more verifications to do.\n");
93 retval = 1;
96 pusb_hal_dbus_disconnect(dbus);
97 libhal_ctx_free(ctx);
98 return (retval);