diff --git a/pam/fingerprint-strings.h b/pam/fingerprint-strings.h index c72dba4..68fc67c 100644 --- a/pam/fingerprint-strings.h +++ b/pam/fingerprint-strings.h @@ -1,7 +1,7 @@ /* * Helper functions to translate statuses and actions to strings * Copyright (C) 2008 Bastien Nocera - * + * * Experimental code. This will be moved out of fprintd into it's own * package once the system has matured. * @@ -9,12 +9,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -28,115 +28,128 @@ #define GNUC_UNUSED __attribute__((__unused__)) -static bool str_equal (const char *a, const char *b) +static bool +str_equal (const char *a, const char *b) { - if (a == NULL && b == NULL) - return true; - if (a == NULL || b == NULL) - return false; - return (strcmp (a, b) == 0); + if (a == NULL && b == NULL) + return true; + if (a == NULL || b == NULL) + return false; + return strcmp (a, b) == 0; } -struct { - const char *dbus_name; - const char *place_str_generic; - const char *place_str_specific; - const char *swipe_str_generic; - const char *swipe_str_specific; +struct +{ + const char *dbus_name; + const char *place_str_generic; + const char *place_str_specific; + const char *swipe_str_generic; + const char *swipe_str_specific; } fingers[] = { - { "any", - N_("Place your finger on the fingerprint reader"), - N_("Place your finger on %s"), - N_("Swipe your finger across the fingerprint reader"), - N_("Swipe your finger across %s") }, - { "left-thumb", - N_("Place your left thumb on the fingerprint reader"), - N_("Place your left thumb on %s"), - N_("Swipe your left thumb across the fingerprint reader"), - N_("Swipe your left thumb across %s") }, - { "left-index-finger", - N_("Place your left index finger on the fingerprint reader"), - N_("Place your left index finger on %s"), - N_("Swipe your left index finger across the fingerprint reader"), - N_("Swipe your left index finger across %s") }, - { "left-middle-finger", - N_("Place your left middle finger on the fingerprint reader"), - N_("Place your left middle finger on %s"), - N_("Swipe your left middle finger across the fingerprint reader"), - N_("Swipe your left middle finger across %s") }, - { "left-ring-finger", - N_("Place your left ring finger on the fingerprint reader"), - N_("Place your left ring finger on %s"), - N_("Swipe your left ring finger across the fingerprint reader"), - N_("Swipe your left ring finger across %s") }, - { "left-little-finger", - N_("Place your left little finger on the fingerprint reader"), - N_("Place your left little finger on %s"), - N_("Swipe your left little finger across the fingerprint reader"), - N_("Swipe your left little finger across %s") }, - { "right-thumb", - N_("Place your right thumb on the fingerprint reader"), - N_("Place your right thumb on %s"), - N_("Swipe your right thumb across the fingerprint reader"), - N_("Swipe your right thumb across %s") }, - { "right-index-finger", - N_("Place your right index finger on the fingerprint reader"), - N_("Place your right index finger on %s"), - N_("Swipe your right index finger across the fingerprint reader"), - N_("Swipe your right index finger across %s") }, - { "right-middle-finger", - N_("Place your right middle finger on the fingerprint reader"), - N_("Place your right middle finger on %s"), - N_("Swipe your right middle finger across the fingerprint reader"), - N_("Swipe your right middle finger across %s") }, - { "right-ring-finger", - N_("Place your right ring finger on the fingerprint reader"), - N_("Place your right ring finger on %s"), - N_("Swipe your right ring finger across the fingerprint reader"), - N_("Swipe your right ring finger across %s") }, - { "right-little-finger", - N_("Place your right little finger on the fingerprint reader"), - N_("Place your right little finger on %s"), - N_("Swipe your right little finger across the fingerprint reader"), - N_("Swipe your right little finger across %s") }, - { NULL, NULL, NULL, NULL, NULL } + { "any", + N_("Place your finger on the fingerprint reader"), + N_("Place your finger on %s"), + N_("Swipe your finger across the fingerprint reader"), + N_("Swipe your finger across %s") }, + { "left-thumb", + N_("Place your left thumb on the fingerprint reader"), + N_("Place your left thumb on %s"), + N_("Swipe your left thumb across the fingerprint reader"), + N_("Swipe your left thumb across %s") }, + { "left-index-finger", + N_("Place your left index finger on the fingerprint reader"), + N_("Place your left index finger on %s"), + N_("Swipe your left index finger across the fingerprint reader"), + N_("Swipe your left index finger across %s") }, + { "left-middle-finger", + N_("Place your left middle finger on the fingerprint reader"), + N_("Place your left middle finger on %s"), + N_("Swipe your left middle finger across the fingerprint reader"), + N_("Swipe your left middle finger across %s") }, + { "left-ring-finger", + N_("Place your left ring finger on the fingerprint reader"), + N_("Place your left ring finger on %s"), + N_("Swipe your left ring finger across the fingerprint reader"), + N_("Swipe your left ring finger across %s") }, + { "left-little-finger", + N_("Place your left little finger on the fingerprint reader"), + N_("Place your left little finger on %s"), + N_("Swipe your left little finger across the fingerprint reader"), + N_("Swipe your left little finger across %s") }, + { "right-thumb", + N_("Place your right thumb on the fingerprint reader"), + N_("Place your right thumb on %s"), + N_("Swipe your right thumb across the fingerprint reader"), + N_("Swipe your right thumb across %s") }, + { "right-index-finger", + N_("Place your right index finger on the fingerprint reader"), + N_("Place your right index finger on %s"), + N_("Swipe your right index finger across the fingerprint reader"), + N_("Swipe your right index finger across %s") }, + { "right-middle-finger", + N_("Place your right middle finger on the fingerprint reader"), + N_("Place your right middle finger on %s"), + N_("Swipe your right middle finger across the fingerprint reader"), + N_("Swipe your right middle finger across %s") }, + { "right-ring-finger", + N_("Place your right ring finger on the fingerprint reader"), + N_("Place your right ring finger on %s"), + N_("Swipe your right ring finger across the fingerprint reader"), + N_("Swipe your right ring finger across %s") }, + { "right-little-finger", + N_("Place your right little finger on the fingerprint reader"), + N_("Place your right little finger on %s"), + N_("Swipe your right little finger across the fingerprint reader"), + N_("Swipe your right little finger across %s") }, + { NULL, NULL, NULL, NULL, NULL } }; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" -GNUC_UNUSED static char *finger_str_to_msg(const char *finger_name, const char *driver_name, bool is_swipe) +GNUC_UNUSED static char * +finger_str_to_msg (const char *finger_name, const char *driver_name, bool is_swipe) { - int i; + int i; - if (finger_name == NULL) - return NULL; + if (finger_name == NULL) + return NULL; - for (i = 0; fingers[i].dbus_name != NULL; i++) { - if (!str_equal (fingers[i].dbus_name, finger_name)) - continue; - if (is_swipe == false) { - if (driver_name) { - char *s; - int ret; - ret = asprintf (&s, TR (fingers[i].place_str_specific), driver_name); - return ret >= 0 ? s : NULL; - } else { - return strdup (TR (fingers[i].place_str_generic)); - } - } else { - if (driver_name) { - char *s; - int ret; - ret = asprintf (&s, TR (fingers[i].swipe_str_specific), driver_name); - return ret >= 0 ? s : NULL; - } else { - return strdup (TR (fingers[i].swipe_str_generic)); - } - } - } + for (i = 0; fingers[i].dbus_name != NULL; i++) + { + if (!str_equal (fingers[i].dbus_name, finger_name)) + continue; + if (is_swipe == false) + { + if (driver_name) + { + char *s; + int ret; + ret = asprintf (&s, TR (fingers[i].place_str_specific), driver_name); + return ret >= 0 ? s : NULL; + } + else + { + return strdup (TR (fingers[i].place_str_generic)); + } + } + else + { + if (driver_name) + { + char *s; + int ret; + ret = asprintf (&s, TR (fingers[i].swipe_str_specific), driver_name); + return ret >= 0 ? s : NULL; + } + else + { + return strdup (TR (fingers[i].swipe_str_generic)); + } + } + } - return NULL; + return NULL; } #pragma GCC diagnostic pop @@ -146,25 +159,27 @@ GNUC_UNUSED static char *finger_str_to_msg(const char *finger_name, const char * * verify-match * verify-unknown-error */ -GNUC_UNUSED static const char *verify_result_str_to_msg(const char *result, bool is_swipe) +GNUC_UNUSED static const char * +verify_result_str_to_msg (const char *result, bool is_swipe) { - if (result == NULL) - return NULL; + if (result == NULL) + return NULL; - if (strcmp (result, "verify-retry-scan") == 0) { - if (is_swipe == false) - return TR (N_("Place your finger on the reader again")); - else - return TR (N_("Swipe your finger again")); - } - if (strcmp (result, "verify-swipe-too-short") == 0) - return TR (N_("Swipe was too short, try again")); - if (strcmp (result, "verify-finger-not-centered") == 0) - return TR (N_("Your finger was not centered, try swiping your finger again")); - if (strcmp (result, "verify-remove-and-retry") == 0) - return TR (N_("Remove your finger, and try swiping your finger again")); + if (strcmp (result, "verify-retry-scan") == 0) + { + if (is_swipe == false) + return TR (N_("Place your finger on the reader again")); + else + return TR (N_("Swipe your finger again")); + } + if (strcmp (result, "verify-swipe-too-short") == 0) + return TR (N_("Swipe was too short, try again")); + if (strcmp (result, "verify-finger-not-centered") == 0) + return TR (N_("Your finger was not centered, try swiping your finger again")); + if (strcmp (result, "verify-remove-and-retry") == 0) + return TR (N_("Remove your finger, and try swiping your finger again")); - return NULL; + return NULL; } /* Cases not handled: @@ -172,24 +187,25 @@ GNUC_UNUSED static const char *verify_result_str_to_msg(const char *result, bool * enroll-failed * enroll-unknown-error */ -GNUC_UNUSED static const char *enroll_result_str_to_msg(const char *result, bool is_swipe) +GNUC_UNUSED static const char * +enroll_result_str_to_msg (const char *result, bool is_swipe) { - if (result == NULL) - return NULL; + if (result == NULL) + return NULL; - if (strcmp (result, "enroll-retry-scan") == 0 || strcmp (result, "enroll-stage-passed") == 0) { - if (is_swipe == false) - return TR (N_("Place your finger on the reader again")); - else - return TR (N_("Swipe your finger again")); - } - if (strcmp (result, "enroll-swipe-too-short") == 0) - return TR (N_("Swipe was too short, try again")); - if (strcmp (result, "enroll-finger-not-centered") == 0) - return TR (N_("Your finger was not centered, try swiping your finger again")); - if (strcmp (result, "enroll-remove-and-retry") == 0) - return TR (N_("Remove your finger, and try swiping your finger again")); + if (strcmp (result, "enroll-retry-scan") == 0 || strcmp (result, "enroll-stage-passed") == 0) + { + if (is_swipe == false) + return TR (N_("Place your finger on the reader again")); + else + return TR (N_("Swipe your finger again")); + } + if (strcmp (result, "enroll-swipe-too-short") == 0) + return TR (N_("Swipe was too short, try again")); + if (strcmp (result, "enroll-finger-not-centered") == 0) + return TR (N_("Your finger was not centered, try swiping your finger again")); + if (strcmp (result, "enroll-remove-and-retry") == 0) + return TR (N_("Remove your finger, and try swiping your finger again")); - return NULL; + return NULL; } - diff --git a/pam/pam_fprintd.c b/pam/pam_fprintd.c index bd156ce..902c59b 100644 --- a/pam/pam_fprintd.c +++ b/pam/pam_fprintd.c @@ -39,7 +39,7 @@ #include #define _(s) ((char *) dgettext (GETTEXT_PACKAGE, s)) -#define TR(s) dgettext(GETTEXT_PACKAGE, s) +#define TR(s) dgettext (GETTEXT_PACKAGE, s) #define N_(s) (s) #include "fingerprint-strings.h" @@ -63,690 +63,753 @@ static unsigned timeout = DEFAULT_TIMEOUT; static uint64_t now (void) { - struct timespec ts; - clock_gettime (CLOCK_MONOTONIC, &ts); - return (uint64_t) ts.tv_sec * USEC_PER_SEC + (uint64_t) ts.tv_nsec / NSEC_PER_USEC; + struct timespec ts; + + clock_gettime (CLOCK_MONOTONIC, &ts); + return (uint64_t) ts.tv_sec * USEC_PER_SEC + (uint64_t) ts.tv_nsec / NSEC_PER_USEC; } -static bool str_has_prefix (const char *s, const char *prefix) +static bool +str_has_prefix (const char *s, const char *prefix) { - if (s == NULL || prefix == NULL) - return false; - return (strncmp (s, prefix, strlen (prefix)) == 0); + if (s == NULL || prefix == NULL) + return false; + return strncmp (s, prefix, strlen (prefix)) == 0; } -static bool send_msg(pam_handle_t *pamh, const char *msg, int style) +static bool +send_msg (pam_handle_t *pamh, const char *msg, int style) { - const struct pam_message mymsg = { - .msg_style = style, - .msg = msg, - }; - const struct pam_message *msgp = &mymsg; - const struct pam_conv *pc; - struct pam_response *resp; + const struct pam_message mymsg = { + .msg_style = style, + .msg = msg, + }; + const struct pam_message *msgp = &mymsg; + const struct pam_conv *pc; + struct pam_response *resp; - if (pam_get_item(pamh, PAM_CONV, (const void **) &pc) != PAM_SUCCESS) - return false; + if (pam_get_item (pamh, PAM_CONV, (const void **) &pc) != PAM_SUCCESS) + return false; - if (!pc || !pc->conv) - return false; + if (!pc || !pc->conv) + return false; - return (pc->conv(1, &msgp, &resp, pc->appdata_ptr) == PAM_SUCCESS); + return pc->conv (1, &msgp, &resp, pc->appdata_ptr) == PAM_SUCCESS; } -static bool send_info_msg(pam_handle_t *pamh, const char *msg) +static bool +send_info_msg (pam_handle_t *pamh, const char *msg) { - return send_msg(pamh, msg, PAM_TEXT_INFO); + return send_msg (pamh, msg, PAM_TEXT_INFO); } -static bool send_err_msg(pam_handle_t *pamh, const char *msg) +static bool +send_err_msg (pam_handle_t *pamh, const char *msg) { - return send_msg(pamh, msg, PAM_ERROR_MSG); + return send_msg (pamh, msg, PAM_ERROR_MSG); } static char * -open_device (pam_handle_t *pamh, - sd_bus *bus, - bool *has_multiple_devices) +open_device (pam_handle_t *pamh, + sd_bus *bus, + bool *has_multiple_devices) { - pf_auto(sd_bus_error) error = SD_BUS_ERROR_NULL; - pf_autoptr(sd_bus_message) m = NULL; - size_t num_devices; - const char *path = NULL; - const char *s; - int r; + pf_auto (sd_bus_error) error = SD_BUS_ERROR_NULL; + pf_autoptr (sd_bus_message) m = NULL; + size_t num_devices; + const char *path = NULL; + const char *s; + int r; - *has_multiple_devices = false; + *has_multiple_devices = false; - if (sd_bus_call_method (bus, - "net.reactivated.Fprint", - "/net/reactivated/Fprint/Manager", - "net.reactivated.Fprint.Manager", - "GetDevices", - &error, - &m, - NULL) < 0) { - pam_syslog (pamh, LOG_ERR, "GetDevices failed: %s", error.message); - return NULL; - } + if (sd_bus_call_method (bus, + "net.reactivated.Fprint", + "/net/reactivated/Fprint/Manager", + "net.reactivated.Fprint.Manager", + "GetDevices", + &error, + &m, + NULL) < 0) + { + pam_syslog (pamh, LOG_ERR, "GetDevices failed: %s", error.message); + return NULL; + } - r = sd_bus_message_enter_container (m, 'a', "o"); - if (r < 0) { - pam_syslog (pamh, LOG_ERR, "Failed to parse answer from GetDevices(): %d", r); - return NULL; - } + r = sd_bus_message_enter_container (m, 'a', "o"); + if (r < 0) + { + pam_syslog (pamh, LOG_ERR, "Failed to parse answer from GetDevices(): %d", r); + return NULL; + } - if (sd_bus_message_read_basic (m, 'o', &path) < 0) - return NULL; + if (sd_bus_message_read_basic (m, 'o', &path) < 0) + return NULL; - num_devices = 1; - while ((r = sd_bus_message_read_basic(m, 'o', &s)) > 0) - num_devices++; - *has_multiple_devices = (num_devices > 1); - if (debug) - pam_syslog(pamh, LOG_DEBUG, "Using device %s (out of %ld devices)", path, num_devices); + num_devices = 1; + while ((r = sd_bus_message_read_basic (m, 'o', &s)) > 0) + num_devices++; + *has_multiple_devices = (num_devices > 1); + if (debug) + pam_syslog (pamh, LOG_DEBUG, "Using device %s (out of %ld devices)", path, num_devices); - sd_bus_message_exit_container (m); + sd_bus_message_exit_container (m); - return strdup (path); + return strdup (path); } -typedef struct { - char *dev; - bool has_multiple_devices; +typedef struct +{ + char *dev; + bool has_multiple_devices; - unsigned max_tries; - char *result; - bool timed_out; - bool is_swipe; - bool verify_started; - int verify_ret; - pam_handle_t *pamh; + unsigned max_tries; + char *result; + bool timed_out; + bool is_swipe; + bool verify_started; + int verify_ret; + pam_handle_t *pamh; - char *driver; + char *driver; } verify_data; static void verify_data_free (verify_data *data) { - free (data->result); - free (data->driver); - free (data->dev); - free (data); + free (data->result); + free (data->driver); + free (data->dev); + free (data); } PF_DEFINE_AUTOPTR_CLEANUP_FUNC (verify_data, verify_data_free) static int verify_result (sd_bus_message *m, - void *userdata, - sd_bus_error *ret_error) + void *userdata, + sd_bus_error *ret_error) { - verify_data *data = userdata; - const char *msg; - const char *result = NULL; - /* see https://github.com/systemd/systemd/issues/14643 */ - uint64_t done = false; - int r; + verify_data *data = userdata; + const char *msg; + const char *result = NULL; + /* see https://github.com/systemd/systemd/issues/14643 */ + uint64_t done = false; + int r; - if (!sd_bus_message_is_signal(m, "net.reactivated.Fprint.Device", "VerifyStatus")) { - pam_syslog (data->pamh, LOG_ERR, "Not the signal we expected (iface: %s, member: %s)", - sd_bus_message_get_interface (m), - sd_bus_message_get_member (m)); - return 0; - } + if (!sd_bus_message_is_signal (m, "net.reactivated.Fprint.Device", "VerifyStatus")) + { + pam_syslog (data->pamh, LOG_ERR, "Not the signal we expected (iface: %s, member: %s)", + sd_bus_message_get_interface (m), + sd_bus_message_get_member (m)); + return 0; + } - if ((r = sd_bus_message_read (m, "sb", &result, &done)) < 0) { - pam_syslog (data->pamh, LOG_ERR, "Failed to parse VerifyResult signal: %d", r); - return 0; - } + if ((r = sd_bus_message_read (m, "sb", &result, &done)) < 0) + { + pam_syslog (data->pamh, LOG_ERR, "Failed to parse VerifyResult signal: %d", r); + return 0; + } - if (!data->verify_started) { - pam_syslog (data->pamh, LOG_ERR, "Unexpected VerifyResult '%s', %"PRIu64" signal", result, done); - return 0; - } + if (!data->verify_started) + { + pam_syslog (data->pamh, LOG_ERR, "Unexpected VerifyResult '%s', %" PRIu64 " signal", result, done); + return 0; + } - if (debug) - pam_syslog (data->pamh, LOG_DEBUG, "Verify result: %s (done: %d)", result, done ? 1 : 0); + if (debug) + pam_syslog (data->pamh, LOG_DEBUG, "Verify result: %s (done: %d)", result, done ? 1 : 0); - if (data->result) { - free (data->result); - data->result = NULL; - } + if (data->result) + { + free (data->result); + data->result = NULL; + } - if (done) { - data->result = strdup (result); - return 0; - } + if (done) + { + data->result = strdup (result); + return 0; + } - msg = verify_result_str_to_msg (result, data->is_swipe); - if (!msg) { - data->result = strdup ("Protocol error with fprintd!"); - return 0; - } - send_err_msg (data->pamh, msg); + msg = verify_result_str_to_msg (result, data->is_swipe); + if (!msg) + { + data->result = strdup ("Protocol error with fprintd!"); + return 0; + } + send_err_msg (data->pamh, msg); - return 0; + return 0; } static int verify_finger_selected (sd_bus_message *m, - void *userdata, - sd_bus_error *ret_error) + void *userdata, + sd_bus_error *ret_error) { - verify_data *data = userdata; - const char *finger_name = NULL; - pf_autofree char *msg = NULL; + verify_data *data = userdata; + const char *finger_name = NULL; + pf_autofree char *msg = NULL; - if (sd_bus_message_read_basic (m, 's', &finger_name) < 0) { - pam_syslog (data->pamh, LOG_ERR, "Failed to parse VerifyFingerSelected signal: %d", errno); - return 0; - } + if (sd_bus_message_read_basic (m, 's', &finger_name) < 0) + { + pam_syslog (data->pamh, LOG_ERR, "Failed to parse VerifyFingerSelected signal: %d", errno); + return 0; + } - if (!data->verify_started) { - pam_syslog (data->pamh, LOG_ERR, "Unexpected VerifyFingerSelected %s signal", finger_name); - return 0; - } + if (!data->verify_started) + { + pam_syslog (data->pamh, LOG_ERR, "Unexpected VerifyFingerSelected %s signal", finger_name); + return 0; + } - msg = finger_str_to_msg(finger_name, data->driver, data->is_swipe); - if (!msg) { - data->result = strdup ("Protocol error with fprintd!"); - return 0; - } - if (debug) - pam_syslog (data->pamh, LOG_DEBUG, "verify_finger_selected %s", msg); - send_info_msg (data->pamh, msg); + msg = finger_str_to_msg (finger_name, data->driver, data->is_swipe); + if (!msg) + { + data->result = strdup ("Protocol error with fprintd!"); + return 0; + } + if (debug) + pam_syslog (data->pamh, LOG_DEBUG, "verify_finger_selected %s", msg); + send_info_msg (data->pamh, msg); - return 0; + return 0; } /* See https://github.com/systemd/systemd/issues/14636 */ static int -get_property_string (sd_bus *bus, - const char *destination, - const char *path, - const char *interface, - const char *member, - sd_bus_error *error, - char **ret) { +get_property_string (sd_bus *bus, + const char *destination, + const char *path, + const char *interface, + const char *member, + sd_bus_error *error, + char **ret) +{ - pf_autoptr(sd_bus_message) reply = NULL; - const char *s; - char *n; - int r; + pf_autoptr (sd_bus_message) reply = NULL; + const char *s; + char *n; + int r; - r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", interface, member); - if (r < 0) - return r; + r = sd_bus_call_method (bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &reply, "ss", interface, member); + if (r < 0) + return r; - r = sd_bus_message_enter_container(reply, 'v', "s"); - if (r < 0) - return sd_bus_error_set_errno(error, r); + r = sd_bus_message_enter_container (reply, 'v', "s"); + if (r < 0) + return sd_bus_error_set_errno (error, r); - r = sd_bus_message_read_basic(reply, 's', &s); - if (r < 0) - return sd_bus_error_set_errno(error, r); + r = sd_bus_message_read_basic (reply, 's', &s); + if (r < 0) + return sd_bus_error_set_errno (error, r); - n = strdup(s); - if (!n) { - return sd_bus_error_set_errno(error, -ENOMEM); - } + n = strdup (s); + if (!n) + return sd_bus_error_set_errno (error, -ENOMEM); - *ret = n; - return 0; + *ret = n; + return 0; } -static int verify_started_cb (sd_bus_message *m, - void *userdata, - sd_bus_error *ret_error) { - const sd_bus_error *error = sd_bus_message_get_error (m); - verify_data *data = userdata; +static int +verify_started_cb (sd_bus_message *m, + void *userdata, + sd_bus_error *ret_error) +{ + const sd_bus_error *error = sd_bus_message_get_error (m); + verify_data *data = userdata; - if (error) { - if (sd_bus_error_has_name (error, "net.reactivated.Fprint.Error.NoEnrolledPrints")) { - pam_syslog (data->pamh, LOG_DEBUG, "No prints enrolled"); - data->verify_ret = PAM_USER_UNKNOWN; - } else { - data->verify_ret = PAM_AUTH_ERR; - } + if (error) + { + if (sd_bus_error_has_name (error, "net.reactivated.Fprint.Error.NoEnrolledPrints")) + { + pam_syslog (data->pamh, LOG_DEBUG, "No prints enrolled"); + data->verify_ret = PAM_USER_UNKNOWN; + } + else + { + data->verify_ret = PAM_AUTH_ERR; + } - if (debug) - pam_syslog (data->pamh, LOG_DEBUG, "VerifyStart failed: %s", error->message); + if (debug) + pam_syslog (data->pamh, LOG_DEBUG, "VerifyStart failed: %s", error->message); - return 1; - } + return 1; + } - if (debug) - pam_syslog (data->pamh, LOG_DEBUG, "VerifyStart completed successfully"); + if (debug) + pam_syslog (data->pamh, LOG_DEBUG, "VerifyStart completed successfully"); - data->verify_started = true; + data->verify_started = true; - return 1; + return 1; } static int -do_verify (sd_bus *bus, - verify_data *data) +do_verify (sd_bus *bus, + verify_data *data) { - pf_autoptr(sd_bus_slot) verify_status_slot = NULL; - pf_autoptr(sd_bus_slot) verify_finger_selected_slot = NULL; - pf_autofree char *scan_type = NULL; - int r; + pf_autoptr (sd_bus_slot) verify_status_slot = NULL; + pf_autoptr (sd_bus_slot) verify_finger_selected_slot = NULL; + pf_autofree char *scan_type = NULL; + int r; - /* Get some properties for the device */ - r = get_property_string (bus, - "net.reactivated.Fprint", - data->dev, - "net.reactivated.Fprint.Device", - "scan-type", - NULL, - &scan_type); - if (r < 0) - pam_syslog (data->pamh, LOG_ERR, "Failed to get scan-type for %s: %d", data->dev, r); - if (debug) - pam_syslog (data->pamh, LOG_DEBUG, "scan-type for %s: %s", data->dev, scan_type); - if (str_equal (scan_type, "swipe")) - data->is_swipe = true; + /* Get some properties for the device */ + r = get_property_string (bus, + "net.reactivated.Fprint", + data->dev, + "net.reactivated.Fprint.Device", + "scan-type", + NULL, + &scan_type); + if (r < 0) + pam_syslog (data->pamh, LOG_ERR, "Failed to get scan-type for %s: %d", data->dev, r); + if (debug) + pam_syslog (data->pamh, LOG_DEBUG, "scan-type for %s: %s", data->dev, scan_type); + if (str_equal (scan_type, "swipe")) + data->is_swipe = true; - if (data->has_multiple_devices) { - get_property_string (bus, - "net.reactivated.Fprint", - data->dev, - "net.reactivated.Fprint.Device", - "name", - NULL, - &data->driver); - if (r < 0) - pam_syslog (data->pamh, LOG_ERR, "Failed to get driver name for %s: %d", data->dev, r); - if (debug && r == 0) - pam_syslog (data->pamh, LOG_DEBUG, "driver name for %s: %s", data->dev, data->driver); - } + if (data->has_multiple_devices) + { + get_property_string (bus, + "net.reactivated.Fprint", + data->dev, + "net.reactivated.Fprint.Device", + "name", + NULL, + &data->driver); + if (r < 0) + pam_syslog (data->pamh, LOG_ERR, "Failed to get driver name for %s: %d", data->dev, r); + if (debug && r == 0) + pam_syslog (data->pamh, LOG_DEBUG, "driver name for %s: %s", data->dev, data->driver); + } - sd_bus_match_signal (bus, - &verify_status_slot, - "net.reactivated.Fprint", - data->dev, - "net.reactivated.Fprint.Device", - "VerifyStatus", - verify_result, - data); + sd_bus_match_signal (bus, + &verify_status_slot, + "net.reactivated.Fprint", + data->dev, + "net.reactivated.Fprint.Device", + "VerifyStatus", + verify_result, + data); - sd_bus_match_signal (bus, - &verify_finger_selected_slot, - "net.reactivated.Fprint", - data->dev, - "net.reactivated.Fprint.Device", - "VerifyFingerSelected", - verify_finger_selected, - data); + sd_bus_match_signal (bus, + &verify_finger_selected_slot, + "net.reactivated.Fprint", + data->dev, + "net.reactivated.Fprint.Device", + "VerifyFingerSelected", + verify_finger_selected, + data); - while (data->max_tries > 0) { - uint64_t verification_end = now () + (timeout * USEC_PER_SEC); + while (data->max_tries > 0) + { + uint64_t verification_end = now () + (timeout * USEC_PER_SEC); - data->timed_out = false; - data->verify_started = false; - data->verify_ret = PAM_INCOMPLETE; + data->timed_out = false; + data->verify_started = false; + data->verify_ret = PAM_INCOMPLETE; - free (data->result); - data->result = NULL; + free (data->result); + data->result = NULL; - if (debug) - pam_syslog (data->pamh, LOG_DEBUG, "About to call VerifyStart"); + if (debug) + pam_syslog (data->pamh, LOG_DEBUG, "About to call VerifyStart"); - r = sd_bus_call_method_async (bus, - NULL, - "net.reactivated.Fprint", - data->dev, - "net.reactivated.Fprint.Device", - "VerifyStart", - verify_started_cb, - data, - "s", - "any"); + r = sd_bus_call_method_async (bus, + NULL, + "net.reactivated.Fprint", + data->dev, + "net.reactivated.Fprint.Device", + "VerifyStart", + verify_started_cb, + data, + "s", + "any"); - if (r < 0) { - if (debug) - pam_syslog (data->pamh, LOG_DEBUG, "VerifyStart call failed: %d", r); - break; - } + if (r < 0) + { + if (debug) + pam_syslog (data->pamh, LOG_DEBUG, "VerifyStart call failed: %d", r); + break; + } - for (;;) { - int64_t wait_time; + for (;;) + { + int64_t wait_time; - wait_time = verification_end - now(); - if (wait_time <= 0) - break; + wait_time = verification_end - now (); + if (wait_time <= 0) + break; - r = sd_bus_process (bus, NULL); - if (r < 0) - break; - if (data->verify_ret != PAM_INCOMPLETE) - break; - if (!data->verify_started) - continue; - if (data->result != NULL) - break; - if (r == 0) { - if (debug) { - pam_syslog(data->pamh, LOG_DEBUG, - "Waiting for %"PRId64" seconds (%"PRId64" usecs)", - wait_time / USEC_PER_SEC, - wait_time); - } - if (sd_bus_wait (bus, wait_time) < 0) - break; - } - } + r = sd_bus_process (bus, NULL); + if (r < 0) + break; + if (data->verify_ret != PAM_INCOMPLETE) + break; + if (!data->verify_started) + continue; + if (data->result != NULL) + break; + if (r == 0) + { + if (debug) + { + pam_syslog (data->pamh, LOG_DEBUG, + "Waiting for %" PRId64 " seconds (%" PRId64 " usecs)", + wait_time / USEC_PER_SEC, + wait_time); + } + if (sd_bus_wait (bus, wait_time) < 0) + break; + } + } - if (data->verify_ret != PAM_INCOMPLETE) { - return data->verify_ret; - } + if (data->verify_ret != PAM_INCOMPLETE) + return data->verify_ret; - if (now () >= verification_end) { - data->timed_out = true; - send_info_msg (data->pamh, _("Verification timed out")); - } + if (now () >= verification_end) + { + data->timed_out = true; + send_info_msg (data->pamh, _("Verification timed out")); + } - /* Ignore errors from VerifyStop */ - data->verify_started = false; - sd_bus_call_method (bus, - "net.reactivated.Fprint", - data->dev, - "net.reactivated.Fprint.Device", - "VerifyStop", - NULL, - NULL, - NULL, - NULL); + /* Ignore errors from VerifyStop */ + data->verify_started = false; + sd_bus_call_method (bus, + "net.reactivated.Fprint", + data->dev, + "net.reactivated.Fprint.Device", + "VerifyStop", + NULL, + NULL, + NULL, + NULL); - if (data->timed_out) { - return PAM_AUTHINFO_UNAVAIL; - } else { - if (str_equal (data->result, "verify-no-match")) { - send_err_msg (data->pamh, "Failed to match fingerprint"); - } else if (str_equal (data->result, "verify-match")) { - return PAM_SUCCESS; - } else if (str_equal (data->result, "verify-unknown-error")) { - return PAM_AUTHINFO_UNAVAIL; - } else if (str_equal (data->result, "verify-disconnected")) { - return PAM_AUTHINFO_UNAVAIL; - } else { - send_err_msg (data->pamh, _("An unknown error occurred")); - return PAM_AUTH_ERR; - } - } - data->max_tries--; - } + if (data->timed_out) + { + return PAM_AUTHINFO_UNAVAIL; + } + else + { + if (str_equal (data->result, "verify-no-match")) + { + send_err_msg (data->pamh, "Failed to match fingerprint"); + } + else if (str_equal (data->result, "verify-match")) + { + return PAM_SUCCESS; + } + else if (str_equal (data->result, "verify-unknown-error")) + { + return PAM_AUTHINFO_UNAVAIL; + } + else if (str_equal (data->result, "verify-disconnected")) + { + return PAM_AUTHINFO_UNAVAIL; + } + else + { + send_err_msg (data->pamh, _("An unknown error occurred")); + return PAM_AUTH_ERR; + } + } + data->max_tries--; + } - if (data->max_tries == 0) - return PAM_MAXTRIES; + if (data->max_tries == 0) + return PAM_MAXTRIES; - return PAM_AUTH_ERR; + return PAM_AUTH_ERR; } static bool user_has_prints (pam_handle_t *pamh, - sd_bus *bus, - const char *dev, - const char *username) + sd_bus *bus, + const char *dev, + const char *username) { - pf_auto(sd_bus_error) error = SD_BUS_ERROR_NULL; - pf_autoptr(sd_bus_message) m = NULL; - size_t num_fingers = 0; - const char *s; - int r; + pf_auto (sd_bus_error) error = SD_BUS_ERROR_NULL; + pf_autoptr (sd_bus_message) m = NULL; + size_t num_fingers = 0; + const char *s; + int r; - r = sd_bus_call_method (bus, - "net.reactivated.Fprint", - dev, - "net.reactivated.Fprint.Device", - "ListEnrolledFingers", - &error, - &m, - "s", - username); - if (r < 0) { - /* If ListEnrolledFingers fails then verification should - * also fail (both use the same underlying call), so we - * report false here and bail out early. */ - if (debug) { - pam_syslog (pamh, LOG_DEBUG, "ListEnrolledFingers failed for %s: %s", - username, error.message); - } - return false; - } + r = sd_bus_call_method (bus, + "net.reactivated.Fprint", + dev, + "net.reactivated.Fprint.Device", + "ListEnrolledFingers", + &error, + &m, + "s", + username); + if (r < 0) + { + /* If ListEnrolledFingers fails then verification should + * also fail (both use the same underlying call), so we + * report false here and bail out early. */ + if (debug) + pam_syslog (pamh, LOG_DEBUG, "ListEnrolledFingers failed for %s: %s", + username, error.message); + return false; + } - r = sd_bus_message_enter_container (m, 'a', "s"); - if (r < 0) { - pam_syslog (pamh, LOG_ERR, "Failed to parse answer from ListEnrolledFingers(): %d", r); - return false; - } + r = sd_bus_message_enter_container (m, 'a', "s"); + if (r < 0) + { + pam_syslog (pamh, LOG_ERR, "Failed to parse answer from ListEnrolledFingers(): %d", r); + return false; + } - while ((r = sd_bus_message_read_basic(m, 's', &s)) > 0) - num_fingers++; - sd_bus_message_exit_container (m); + while ((r = sd_bus_message_read_basic (m, 's', &s)) > 0) + num_fingers++; + sd_bus_message_exit_container (m); - return (num_fingers > 0); + return num_fingers > 0; } static void release_device (pam_handle_t *pamh, - sd_bus *bus, - const char *dev) + sd_bus *bus, + const char *dev) { - pf_auto(sd_bus_error) error = SD_BUS_ERROR_NULL; + pf_auto (sd_bus_error) error = SD_BUS_ERROR_NULL; - if (sd_bus_call_method (bus, - "net.reactivated.Fprint", - dev, - "net.reactivated.Fprint.Device", - "Release", - &error, - NULL, - NULL, - NULL) < 0) { - pam_syslog (pamh, LOG_ERR, "ReleaseDevice failed: %s", error.message); - } + if (sd_bus_call_method (bus, + "net.reactivated.Fprint", + dev, + "net.reactivated.Fprint.Device", + "Release", + &error, + NULL, + NULL, + NULL) < 0) + pam_syslog (pamh, LOG_ERR, "ReleaseDevice failed: %s", error.message); } static bool claim_device (pam_handle_t *pamh, - sd_bus *bus, - const char *dev, - const char *username) + sd_bus *bus, + const char *dev, + const char *username) { - pf_auto(sd_bus_error) error = SD_BUS_ERROR_NULL; + pf_auto (sd_bus_error) error = SD_BUS_ERROR_NULL; - if (sd_bus_call_method (bus, - "net.reactivated.Fprint", - dev, - "net.reactivated.Fprint.Device", - "Claim", - &error, - NULL, - "s", - username) < 0) { - if (debug) - pam_syslog (pamh, LOG_DEBUG, "failed to claim device %s", error.message); - return false; - } + if (sd_bus_call_method (bus, + "net.reactivated.Fprint", + dev, + "net.reactivated.Fprint.Device", + "Claim", + &error, + NULL, + "s", + username) < 0) + { + if (debug) + pam_syslog (pamh, LOG_DEBUG, "failed to claim device %s", error.message); + return false; + } - return true; + return true; } static int name_owner_changed (sd_bus_message *m, - void *userdata, - sd_bus_error *ret_error) + void *userdata, + sd_bus_error *ret_error) { - verify_data *data = userdata; - const char *name = NULL; - const char *old_owner = NULL; - const char *new_owner = NULL; + verify_data *data = userdata; + const char *name = NULL; + const char *old_owner = NULL; + const char *new_owner = NULL; - if (sd_bus_message_read (m, "sss", &name, &old_owner, &new_owner) < 0) { - pam_syslog (data->pamh, LOG_ERR, "Failed to parse NameOwnerChanged signal: %d", errno); - return 0; - } + if (sd_bus_message_read (m, "sss", &name, &old_owner, &new_owner) < 0) + { + pam_syslog (data->pamh, LOG_ERR, "Failed to parse NameOwnerChanged signal: %d", errno); + return 0; + } - if (strcmp (name, "net.reactivated.Fprint") != 0) - return 0; + if (strcmp (name, "net.reactivated.Fprint") != 0) + return 0; - /* Name owner for fprintd changed, give up as we might start listening - * to events from a new name owner otherwise. */ - data->verify_ret = PAM_AUTHINFO_UNAVAIL; + /* Name owner for fprintd changed, give up as we might start listening + * to events from a new name owner otherwise. */ + data->verify_ret = PAM_AUTHINFO_UNAVAIL; - if (debug) - pam_syslog (data->pamh, LOG_ERR, "fprintd name owner changed during operation!\n"); + if (debug) + pam_syslog (data->pamh, LOG_ERR, "fprintd name owner changed during operation!\n"); - return 0; + return 0; } -static int do_auth(pam_handle_t *pamh, const char *username) +static int +do_auth (pam_handle_t *pamh, const char *username) { - bool have_prints; - pf_autoptr(verify_data) data = NULL; - pf_autoptr(sd_bus) bus = NULL; - pf_autoptr(sd_bus_slot) name_owner_changed_slot = NULL; + bool have_prints; - data = calloc (1, sizeof(verify_data)); - data->max_tries = max_tries; - data->pamh = pamh; + pf_autoptr (verify_data) data = NULL; + pf_autoptr (sd_bus) bus = NULL; + pf_autoptr (sd_bus_slot) name_owner_changed_slot = NULL; - if (sd_bus_open_system (&bus) < 0) { - pam_syslog (pamh, LOG_ERR, "Error with getting the bus: %d", errno); - return PAM_AUTHINFO_UNAVAIL; - } + data = calloc (1, sizeof (verify_data)); + data->max_tries = max_tries; + data->pamh = pamh; - name_owner_changed_slot = NULL; - sd_bus_match_signal (bus, - &name_owner_changed_slot, - "org.freedesktop.DBus", - "/org/freedesktop/DBus", - "org.freedesktop.DBus", - "NameOwnerChanged", - name_owner_changed, - data); + if (sd_bus_open_system (&bus) < 0) + { + pam_syslog (pamh, LOG_ERR, "Error with getting the bus: %d", errno); + return PAM_AUTHINFO_UNAVAIL; + } - data->dev = open_device (pamh, bus, &data->has_multiple_devices); - if (data->dev == NULL) { - return PAM_AUTHINFO_UNAVAIL; - } + name_owner_changed_slot = NULL; + sd_bus_match_signal (bus, + &name_owner_changed_slot, + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus", + "NameOwnerChanged", + name_owner_changed, + data); - have_prints = user_has_prints (pamh, bus, data->dev, username); - if (debug) - pam_syslog (pamh, LOG_DEBUG, "prints registered: %s\n", have_prints ? "yes" : "no"); + data->dev = open_device (pamh, bus, &data->has_multiple_devices); + if (data->dev == NULL) + return PAM_AUTHINFO_UNAVAIL; - if (!have_prints) - return PAM_AUTHINFO_UNAVAIL; + have_prints = user_has_prints (pamh, bus, data->dev, username); + if (debug) + pam_syslog (pamh, LOG_DEBUG, "prints registered: %s\n", have_prints ? "yes" : "no"); - if (claim_device (pamh, bus, data->dev, username)) { - int ret = do_verify (bus, data); - release_device (pamh, bus, data->dev); - return ret; - } + if (!have_prints) + return PAM_AUTHINFO_UNAVAIL; - return PAM_AUTHINFO_UNAVAIL; + if (claim_device (pamh, bus, data->dev, username)) + { + int ret = do_verify (bus, data); + release_device (pamh, bus, data->dev); + return ret; + } + + return PAM_AUTHINFO_UNAVAIL; } static bool is_remote (pam_handle_t *pamh) { - const char *rhost = NULL; + const char *rhost = NULL; - pam_get_item(pamh, PAM_RHOST, (const void **)(const void*) &rhost); + pam_get_item (pamh, PAM_RHOST, (const void **) (const void *) &rhost); - /* NULL or empty rhost if the host information is not available or set. - * "localhost" if the host is local. - * We want to not run for known remote hosts */ - if (rhost != NULL && - *rhost != '\0' && - strcmp (rhost, "localhost") != 0) { - return true; - } + /* NULL or empty rhost if the host information is not available or set. + * "localhost" if the host is local. + * We want to not run for known remote hosts */ + if (rhost != NULL && + *rhost != '\0' && + strcmp (rhost, "localhost") != 0) + return true; - if (sd_session_is_remote (NULL) > 0) - return true; + if (sd_session_is_remote (NULL) > 0) + return true; - return false; + return false; } -PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, - const char **argv) +PAM_EXTERN int +pam_sm_authenticate (pam_handle_t *pamh, int flags, int argc, + const char **argv) { - const char *username; - int i; + const char *username; + int i; - bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); - bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); + bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); - if (is_remote (pamh)) - return PAM_AUTHINFO_UNAVAIL; + if (is_remote (pamh)) + return PAM_AUTHINFO_UNAVAIL; - if (pam_get_user(pamh, &username, NULL) != PAM_SUCCESS) - return PAM_AUTHINFO_UNAVAIL; + if (pam_get_user (pamh, &username, NULL) != PAM_SUCCESS) + return PAM_AUTHINFO_UNAVAIL; - for (i = 0; i < argc; i++) { - if (argv[i] != NULL) { - if (str_equal (argv[i], "debug")) { - pam_syslog (pamh, LOG_DEBUG, "debug on"); - debug = true; - } else if (str_has_prefix (argv[i], DEBUG_MATCH)) { - pam_syslog (pamh, LOG_DEBUG, "debug on"); - const char *value; + for (i = 0; i < argc; i++) + { + if (argv[i] != NULL) + { + if (str_equal (argv[i], "debug")) + { + pam_syslog (pamh, LOG_DEBUG, "debug on"); + debug = true; + } + else if (str_has_prefix (argv[i], DEBUG_MATCH)) + { + pam_syslog (pamh, LOG_DEBUG, "debug on"); + const char *value; - value = argv[i] + strlen (DEBUG_MATCH); - if (str_equal (value, "on") || - str_equal (value, "true") || - str_equal (value, "1")) { - pam_syslog (pamh, LOG_DEBUG, "debug on"); - debug = true; - } else if (str_equal (value, "off") || - str_equal (value, "false") || - str_equal (value, "0")) { - debug = false; - } else { - pam_syslog (pamh, LOG_DEBUG, "invalid debug value '%s', disabling", value); - } - } else if (str_has_prefix (argv[i], MAX_TRIES_MATCH) && strlen(argv[i]) == strlen (MAX_TRIES_MATCH) + 1) { - max_tries = atoi (argv[i] + strlen (MAX_TRIES_MATCH)); - if (max_tries < 1) { - if (debug) { - pam_syslog (pamh, LOG_DEBUG, "invalid max tries '%s', using %d", - argv[i] + strlen (MAX_TRIES_MATCH), DEFAULT_MAX_TRIES); - } - max_tries = DEFAULT_MAX_TRIES; - } - if (debug) - pam_syslog (pamh, LOG_DEBUG, "max_tries specified as: %d", max_tries); - } else if (str_has_prefix (argv[i], TIMEOUT_MATCH) && strlen(argv[i]) <= strlen (TIMEOUT_MATCH) + 2) { - timeout = atoi (argv[i] + strlen (TIMEOUT_MATCH)); - if (timeout < MIN_TIMEOUT) { - if (debug) { - pam_syslog (pamh, LOG_DEBUG, "timeout %d secs too low, using %d", - timeout, MIN_TIMEOUT); - } - timeout = MIN_TIMEOUT; - } else if (debug) { - pam_syslog (pamh, LOG_DEBUG, "timeout specified as: %d secs", timeout); - } - } - } - } + value = argv[i] + strlen (DEBUG_MATCH); + if (str_equal (value, "on") || + str_equal (value, "true") || + str_equal (value, "1")) + { + pam_syslog (pamh, LOG_DEBUG, "debug on"); + debug = true; + } + else if (str_equal (value, "off") || + str_equal (value, "false") || + str_equal (value, "0")) + { + debug = false; + } + else + { + pam_syslog (pamh, LOG_DEBUG, "invalid debug value '%s', disabling", value); + } + } + else if (str_has_prefix (argv[i], MAX_TRIES_MATCH) && strlen (argv[i]) == strlen (MAX_TRIES_MATCH) + 1) + { + max_tries = atoi (argv[i] + strlen (MAX_TRIES_MATCH)); + if (max_tries < 1) + { + if (debug) + pam_syslog (pamh, LOG_DEBUG, "invalid max tries '%s', using %d", + argv[i] + strlen (MAX_TRIES_MATCH), DEFAULT_MAX_TRIES); + max_tries = DEFAULT_MAX_TRIES; + } + if (debug) + pam_syslog (pamh, LOG_DEBUG, "max_tries specified as: %d", max_tries); + } + else if (str_has_prefix (argv[i], TIMEOUT_MATCH) && strlen (argv[i]) <= strlen (TIMEOUT_MATCH) + 2) + { + timeout = atoi (argv[i] + strlen (TIMEOUT_MATCH)); + if (timeout < MIN_TIMEOUT) + { + if (debug) + pam_syslog (pamh, LOG_DEBUG, "timeout %d secs too low, using %d", + timeout, MIN_TIMEOUT); + timeout = MIN_TIMEOUT; + } + else if (debug) + { + pam_syslog (pamh, LOG_DEBUG, "timeout specified as: %d secs", timeout); + } + } + } + } - return do_auth(pamh, username); + return do_auth (pamh, username); } -PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, - const char **argv) +PAM_EXTERN int +pam_sm_setcred (pam_handle_t *pamh, int flags, int argc, + const char **argv) { - return PAM_SUCCESS; + return PAM_SUCCESS; } -PAM_EXTERN int pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, - const char **argv) +PAM_EXTERN int +pam_sm_chauthtok (pam_handle_t *pamh, int flags, int argc, + const char **argv) { - return PAM_SUCCESS; + return PAM_SUCCESS; } - diff --git a/pam/pam_fprintd_autoptrs.h b/pam/pam_fprintd_autoptrs.h index 3328129..3a7fef6 100644 --- a/pam/pam_fprintd_autoptrs.h +++ b/pam/pam_fprintd_autoptrs.h @@ -23,35 +23,36 @@ /* Define auto-pointers functions, based on GLib Macros */ -#define _CLEANUP_FUNC(func) __attribute__((cleanup(func))) +#define _CLEANUP_FUNC(func) __attribute__((cleanup (func))) -#define _PF_AUTOPTR_FUNC_NAME(TypeName) pf_autoptr_cleanup_##TypeName -#define _PF_AUTOPTR_TYPENAME(TypeName) TypeName##_pf_autoptr +#define _PF_AUTOPTR_FUNC_NAME(TypeName) pf_autoptr_cleanup_ ## TypeName +#define _PF_AUTOPTR_TYPENAME(TypeName) TypeName ## _pf_autoptr #define PF_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, cleanup) \ - typedef TypeName *_PF_AUTOPTR_TYPENAME (TypeName); \ - static __attribute__((__unused__)) inline void \ - _PF_AUTOPTR_FUNC_NAME (TypeName) (TypeName **_ptr) \ - { if (_ptr) (cleanup) (*_ptr); } + typedef TypeName *_PF_AUTOPTR_TYPENAME (TypeName); \ + static __attribute__((__unused__)) inline void \ + _PF_AUTOPTR_FUNC_NAME (TypeName) (TypeName **_ptr) \ + { if (_ptr) (cleanup) (*_ptr); } #define PF_DEFINE_AUTO_CLEAN_FUNC(TypeName, cleanup) \ - static __attribute__((__unused__)) inline void \ - _PF_AUTOPTR_FUNC_NAME (TypeName) (TypeName *_ptr) \ - { cleanup (_ptr); } + static __attribute__((__unused__)) inline void \ + _PF_AUTOPTR_FUNC_NAME (TypeName) (TypeName *_ptr) \ + { cleanup (_ptr); } static inline void autoptr_cleanup_generic_free (void *p) { - void **pp = (void**)p; + void **pp = (void **) p; + free (*pp); } #define pf_autofree _CLEANUP_FUNC (autoptr_cleanup_generic_free) #define pf_autoptr(TypeName) \ - _CLEANUP_FUNC (_PF_AUTOPTR_FUNC_NAME (TypeName)) \ - _PF_AUTOPTR_TYPENAME (TypeName) + _CLEANUP_FUNC (_PF_AUTOPTR_FUNC_NAME (TypeName)) \ + _PF_AUTOPTR_TYPENAME (TypeName) #define pf_auto(TypeName) \ - _CLEANUP_FUNC (_PF_AUTOPTR_FUNC_NAME (TypeName)) TypeName + _CLEANUP_FUNC (_PF_AUTOPTR_FUNC_NAME (TypeName)) TypeName PF_DEFINE_AUTOPTR_CLEANUP_FUNC (sd_bus, sd_bus_unref) PF_DEFINE_AUTOPTR_CLEANUP_FUNC (sd_bus_message, sd_bus_message_unref) diff --git a/src/device.c b/src/device.c index b41ebb2..9e64d63 100644 --- a/src/device.c +++ b/src/device.c @@ -7,12 +7,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -33,99 +33,101 @@ #include "storage.h" static const char *FINGERS_NAMES[] = { - [FP_FINGER_UNKNOWN] = "unknown", - [FP_FINGER_LEFT_THUMB] = "left-thumb", - [FP_FINGER_LEFT_INDEX] = "left-index-finger", - [FP_FINGER_LEFT_MIDDLE] = "left-middle-finger", - [FP_FINGER_LEFT_RING] = "left-ring-finger", - [FP_FINGER_LEFT_LITTLE] = "left-little-finger", - [FP_FINGER_RIGHT_THUMB] = "right-thumb", - [FP_FINGER_RIGHT_INDEX] = "right-index-finger", - [FP_FINGER_RIGHT_MIDDLE] = "right-middle-finger", - [FP_FINGER_RIGHT_RING] = "right-ring-finger", - [FP_FINGER_RIGHT_LITTLE] = "right-little-finger" + [FP_FINGER_UNKNOWN] = "unknown", + [FP_FINGER_LEFT_THUMB] = "left-thumb", + [FP_FINGER_LEFT_INDEX] = "left-index-finger", + [FP_FINGER_LEFT_MIDDLE] = "left-middle-finger", + [FP_FINGER_LEFT_RING] = "left-ring-finger", + [FP_FINGER_LEFT_LITTLE] = "left-little-finger", + [FP_FINGER_RIGHT_THUMB] = "right-thumb", + [FP_FINGER_RIGHT_INDEX] = "right-index-finger", + [FP_FINGER_RIGHT_MIDDLE] = "right-middle-finger", + [FP_FINGER_RIGHT_RING] = "right-ring-finger", + [FP_FINGER_RIGHT_LITTLE] = "right-little-finger" }; static void fprint_device_dbus_skeleton_iface_init (FprintDBusDeviceIface *); static gboolean action_authorization_handler (GDBusInterfaceSkeleton *, - GDBusMethodInvocation *, - gpointer user_data); + GDBusMethodInvocation *, + gpointer user_data); static GQuark quark_auth_user = 0; typedef enum { - ACTION_NONE = 0, - ACTION_IDENTIFY, - ACTION_VERIFY, - ACTION_ENROLL, - ACTION_OPEN, - ACTION_CLOSE, - ACTION_DELETE, + ACTION_NONE = 0, + ACTION_IDENTIFY, + ACTION_VERIFY, + ACTION_ENROLL, + ACTION_OPEN, + ACTION_CLOSE, + ACTION_DELETE, } FprintDeviceAction; typedef enum { - STATE_CLAIMED, - STATE_UNCLAIMED, - STATE_AUTO_CLAIM, - STATE_ANYTIME, + STATE_CLAIMED, + STATE_UNCLAIMED, + STATE_AUTO_CLAIM, + STATE_ANYTIME, } FprintDeviceClaimState; -typedef struct { - volatile gint _refcount; +typedef struct +{ + volatile gint _refcount; - /* current method invocation */ - GDBusMethodInvocation *invocation; + /* current method invocation */ + GDBusMethodInvocation *invocation; - /* The current user of the device, if claimed */ - const char * const sender; + /* The current user of the device, if claimed */ + const char * const sender; - /* The current user of the device, or if allowed, - * what was passed as a username argument */ - const char * const username; + /* The current user of the device, or if allowed, + * what was passed as a username argument */ + const char * const username; - gboolean verify_status_reported; + gboolean verify_status_reported; } SessionData; -typedef struct { - guint32 id; - FpDevice *dev; - SessionData *_session; +typedef struct +{ + guint32 id; + FpDevice *dev; + SessionData *_session; - PolkitAuthority *auth; + PolkitAuthority *auth; - /* Hashtable of connected clients */ - GHashTable *clients; + /* Hashtable of connected clients */ + GHashTable *clients; - /* Required to restart the operation on a retry failure. */ - FpPrint *verify_data; - GPtrArray *identify_data; - int enroll_data; + /* Required to restart the operation on a retry failure. */ + FpPrint *verify_data; + GPtrArray *identify_data; + int enroll_data; - /* whether we're running an identify, or a verify */ - FprintDeviceAction current_action; - GCancellable *current_cancellable; - GDBusMethodInvocation *current_cancel_invocation; + /* whether we're running an identify, or a verify */ + FprintDeviceAction current_action; + GCancellable *current_cancellable; + GDBusMethodInvocation *current_cancel_invocation; } FprintDevicePrivate; G_DEFINE_TYPE_WITH_CODE (FprintDevice, fprint_device, - FPRINT_DBUS_TYPE_DEVICE_SKELETON, - G_ADD_PRIVATE (FprintDevice) - G_IMPLEMENT_INTERFACE (FPRINT_DBUS_TYPE_DEVICE, - fprint_device_dbus_skeleton_iface_init)); + FPRINT_DBUS_TYPE_DEVICE_SKELETON, + G_ADD_PRIVATE (FprintDevice) + G_IMPLEMENT_INTERFACE (FPRINT_DBUS_TYPE_DEVICE, + fprint_device_dbus_skeleton_iface_init)); enum fprint_device_properties { - FPRINT_DEVICE_CONSTRUCT_DEV = 1, - FPRINT_DEVICE_IN_USE, - FPRINT_DEVICE_NAME, - FPRINT_DEVICE_NUM_ENROLL, - FPRINT_DEVICE_SCAN_TYPE + FPRINT_DEVICE_CONSTRUCT_DEV = 1, + FPRINT_DEVICE_IN_USE, + FPRINT_DEVICE_NAME, + FPRINT_DEVICE_NUM_ENROLL, + FPRINT_DEVICE_SCAN_TYPE }; enum fprint_device_signals { - SIGNAL_VERIFY_STATUS, - SIGNAL_VERIFY_FINGER_SELECTED, - SIGNAL_ENROLL_STATUS, - NUM_SIGNALS, + SIGNAL_VERIFY_STATUS, + SIGNAL_VERIFY_FINGER_SELECTED, + SIGNAL_ENROLL_STATUS, + NUM_SIGNALS, }; static guint32 last_id = ~0; @@ -138,1690 +140,1863 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC (PolkitSubject, g_object_unref) #endif static void -session_data_unref(SessionData *session) +session_data_unref (SessionData *session) { - if (g_atomic_int_dec_and_test (&session->_refcount)) { - g_clear_pointer((char**) &session->sender, g_free); - g_clear_pointer((char**) &session->username, g_free); - g_clear_object (&session->invocation); - g_free(session); - } + if (g_atomic_int_dec_and_test (&session->_refcount)) + { + g_clear_pointer ((char **) &session->sender, g_free); + g_clear_pointer ((char **) &session->username, g_free); + g_clear_object (&session->invocation); + g_free (session); + } } -G_DEFINE_AUTOPTR_CLEANUP_FUNC(SessionData, session_data_unref); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (SessionData, session_data_unref); -static SessionData* +static SessionData * session_data_get (FprintDevicePrivate *priv) { - SessionData *invalid = (SessionData*) &priv->_session; - SessionData *cur; + SessionData *invalid = (SessionData *) &priv->_session; + SessionData *cur; - /* Get the current pointer and mark the pointer as "busy". */ - do { - cur = priv->_session; - /* Swap if cur is valid, otherwise busy loop. */ - } while (cur == invalid || !g_atomic_pointer_compare_and_exchange (&priv->_session, cur, invalid)); + /* Get the current pointer and mark the pointer as "busy". */ + do + { + cur = priv->_session; + /* Swap if cur is valid, otherwise busy loop. */ + } + while (cur == invalid || !g_atomic_pointer_compare_and_exchange (&priv->_session, cur, invalid)); - /* We can safely increase the reference count now. */ - if (cur) - g_atomic_int_inc (&cur->_refcount); + /* We can safely increase the reference count now. */ + if (cur) + g_atomic_int_inc (&cur->_refcount); - /* Swap back, this must succeed. */ - if (!g_atomic_pointer_compare_and_exchange (&priv->_session, invalid, cur)) - g_assert_not_reached (); + /* Swap back, this must succeed. */ + if (!g_atomic_pointer_compare_and_exchange (&priv->_session, invalid, cur)) + g_assert_not_reached (); - return cur; + return cur; } /* Pass NULL sender and username to unset session data. */ -static SessionData* +static SessionData * session_data_set_new (FprintDevicePrivate *priv, gchar *sender, gchar *username) { - SessionData *invalid = (SessionData*) &priv->_session; - SessionData *new = NULL; - SessionData *old; + SessionData *invalid = (SessionData *) &priv->_session; + SessionData *new = NULL; + SessionData *old; - g_assert ((!sender && !username) || (sender && username)); - if (sender) { - new = g_new0 (SessionData, 1); - /* Internal reference of the pointer and returned reference. */ - new->_refcount = 2; - *(char**) &new->sender = sender; - *(char**) &new->username = username; - } + g_assert ((!sender && !username) || (sender && username)); + if (sender) + { + new = g_new0 (SessionData, 1); + /* Internal reference of the pointer and returned reference. */ + new->_refcount = 2; + *(char **) &new->sender = sender; + *(char **) &new->username = username; + } - /* Get the current (but not if it is busy) and put the new one in place. */ - do { - old = priv->_session; - /* Swap if old is valid, otherwise busy loop as someone is ref'ing it currently. */ - } while (old == invalid || !g_atomic_pointer_compare_and_exchange (&priv->_session, old, new)); + /* Get the current (but not if it is busy) and put the new one in place. */ + do + { + old = priv->_session; + /* Swap if old is valid, otherwise busy loop as someone is ref'ing it currently. */ + } + while (old == invalid || !g_atomic_pointer_compare_and_exchange (&priv->_session, old, new)); - /* We can safely drop the our internal reference now. */ - if (old) - session_data_unref (old); + /* We can safely drop the our internal reference now. */ + if (old) + session_data_unref (old); - return new; + return new; } -static void fprint_device_dispose(GObject *object) +static void +fprint_device_dispose (GObject *object) { - FprintDevice *self = (FprintDevice *) object; - FprintDevicePrivate *priv = fprint_device_get_instance_private(self); + FprintDevice *self = (FprintDevice *) object; + FprintDevicePrivate *priv = fprint_device_get_instance_private (self); - g_hash_table_remove_all (priv->clients); + g_hash_table_remove_all (priv->clients); - G_OBJECT_CLASS(fprint_device_parent_class)->dispose(object); + G_OBJECT_CLASS (fprint_device_parent_class)->dispose (object); } -static void fprint_device_finalize(GObject *object) +static void +fprint_device_finalize (GObject *object) { - FprintDevice *self = (FprintDevice *) object; - FprintDevicePrivate *priv = fprint_device_get_instance_private(self); + FprintDevice *self = (FprintDevice *) object; + FprintDevicePrivate *priv = fprint_device_get_instance_private (self); - g_hash_table_destroy (priv->clients); - session_data_set_new(priv, NULL, NULL); - g_clear_object (&priv->auth); - g_clear_object (&priv->dev); + g_hash_table_destroy (priv->clients); + session_data_set_new (priv, NULL, NULL); + g_clear_object (&priv->auth); + g_clear_object (&priv->dev); - if (priv->current_action != ACTION_NONE || - priv->_session || - priv->verify_data || - priv->identify_data || - priv->current_cancellable || - priv->current_cancel_invocation) - g_critical("Device was not cleaned up properly before being finalized."); + if (priv->current_action != ACTION_NONE || + priv->_session || + priv->verify_data || + priv->identify_data || + priv->current_cancellable || + priv->current_cancel_invocation) + g_critical ("Device was not cleaned up properly before being finalized."); - G_OBJECT_CLASS(fprint_device_parent_class)->finalize(object); + G_OBJECT_CLASS (fprint_device_parent_class)->finalize (object); } -static void fprint_device_set_property(GObject *object, guint property_id, - const GValue *value, GParamSpec *pspec) +static void +fprint_device_set_property (GObject *object, guint property_id, + const GValue *value, GParamSpec *pspec) { - FprintDevice *self = (FprintDevice *) object; - FprintDevicePrivate *priv = fprint_device_get_instance_private(self); + FprintDevice *self = (FprintDevice *) object; + FprintDevicePrivate *priv = fprint_device_get_instance_private (self); - switch (property_id) { - case FPRINT_DEVICE_CONSTRUCT_DEV: - priv->dev = g_value_dup_object(value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); - break; - } + switch (property_id) + { + case FPRINT_DEVICE_CONSTRUCT_DEV: + priv->dev = g_value_dup_object (value); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } } -static void fprint_device_get_property(GObject *object, guint property_id, - GValue *value, GParamSpec *pspec) +static void +fprint_device_get_property (GObject *object, guint property_id, + GValue *value, GParamSpec *pspec) { - FprintDevice *self = (FprintDevice *) object; - FprintDevicePrivate *priv = fprint_device_get_instance_private(self); + FprintDevice *self = (FprintDevice *) object; + FprintDevicePrivate *priv = fprint_device_get_instance_private (self); - switch (property_id) { - case FPRINT_DEVICE_CONSTRUCT_DEV: - g_value_set_object(value, priv->dev); - break; - case FPRINT_DEVICE_IN_USE: - g_value_set_boolean(value, g_hash_table_size (priv->clients) != 0); - break; - case FPRINT_DEVICE_NAME: - g_value_set_static_string (value, fp_device_get_name (priv->dev)); - break; - case FPRINT_DEVICE_NUM_ENROLL: - if (priv->dev) - g_value_set_int (value, fp_device_get_nr_enroll_stages (priv->dev)); - else - g_value_set_int (value, -1); - break; - case FPRINT_DEVICE_SCAN_TYPE: { - const char *type; + switch (property_id) + { + case FPRINT_DEVICE_CONSTRUCT_DEV: + g_value_set_object (value, priv->dev); + break; - if (fp_device_get_scan_type (priv->dev) == FP_SCAN_TYPE_PRESS) - type = "press"; - else - type = "swipe"; + case FPRINT_DEVICE_IN_USE: + g_value_set_boolean (value, g_hash_table_size (priv->clients) != 0); + break; - g_value_set_static_string (value, type); - break; - } - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); - break; - } + case FPRINT_DEVICE_NAME: + g_value_set_static_string (value, fp_device_get_name (priv->dev)); + break; + + case FPRINT_DEVICE_NUM_ENROLL: + if (priv->dev) + g_value_set_int (value, fp_device_get_nr_enroll_stages (priv->dev)); + else + g_value_set_int (value, -1); + break; + + case FPRINT_DEVICE_SCAN_TYPE: { + const char *type; + + if (fp_device_get_scan_type (priv->dev) == FP_SCAN_TYPE_PRESS) + type = "press"; + else + type = "swipe"; + + g_value_set_static_string (value, type); + break; + } + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } } -static void fprint_device_class_init(FprintDeviceClass *klass) +static void +fprint_device_class_init (FprintDeviceClass *klass) { - GObjectClass *gobject_class = G_OBJECT_CLASS(klass); - GParamSpec *pspec; + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + GParamSpec *pspec; - gobject_class->dispose = fprint_device_dispose; - gobject_class->finalize = fprint_device_finalize; - gobject_class->set_property = fprint_device_set_property; - gobject_class->get_property = fprint_device_get_property; + gobject_class->dispose = fprint_device_dispose; + gobject_class->finalize = fprint_device_finalize; + gobject_class->set_property = fprint_device_set_property; + gobject_class->get_property = fprint_device_get_property; - pspec = g_param_spec_object("dev", "Device", - "Set device construction property", - FP_TYPE_DEVICE, - G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE); - g_object_class_install_property(gobject_class, - FPRINT_DEVICE_CONSTRUCT_DEV, pspec); + pspec = g_param_spec_object ("dev", "Device", + "Set device construction property", + FP_TYPE_DEVICE, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE); + g_object_class_install_property (gobject_class, + FPRINT_DEVICE_CONSTRUCT_DEV, pspec); - pspec = g_param_spec_boolean("in-use", "In use", - "Whether the device is currently in use", FALSE, - G_PARAM_READABLE); - g_object_class_install_property(gobject_class, - FPRINT_DEVICE_IN_USE, pspec); + pspec = g_param_spec_boolean ("in-use", "In use", + "Whether the device is currently in use", FALSE, + G_PARAM_READABLE); + g_object_class_install_property (gobject_class, + FPRINT_DEVICE_IN_USE, pspec); - g_object_class_override_property (gobject_class, - FPRINT_DEVICE_NAME, - "name"); + g_object_class_override_property (gobject_class, + FPRINT_DEVICE_NAME, + "name"); - g_object_class_override_property (gobject_class, - FPRINT_DEVICE_SCAN_TYPE, - "scan-type"); + g_object_class_override_property (gobject_class, + FPRINT_DEVICE_SCAN_TYPE, + "scan-type"); - g_object_class_override_property (gobject_class, - FPRINT_DEVICE_NUM_ENROLL, - "num-enroll-stages"); + g_object_class_override_property (gobject_class, + FPRINT_DEVICE_NUM_ENROLL, + "num-enroll-stages"); - signals[SIGNAL_VERIFY_STATUS] = - g_signal_lookup ("verify-status", FPRINT_TYPE_DEVICE); - signals[SIGNAL_ENROLL_STATUS] = - g_signal_lookup ("enroll-status", FPRINT_TYPE_DEVICE); - signals[SIGNAL_VERIFY_FINGER_SELECTED] = - g_signal_lookup ("verify-finger-selected", FPRINT_TYPE_DEVICE); + signals[SIGNAL_VERIFY_STATUS] = + g_signal_lookup ("verify-status", FPRINT_TYPE_DEVICE); + signals[SIGNAL_ENROLL_STATUS] = + g_signal_lookup ("enroll-status", FPRINT_TYPE_DEVICE); + signals[SIGNAL_VERIFY_FINGER_SELECTED] = + g_signal_lookup ("verify-finger-selected", FPRINT_TYPE_DEVICE); - quark_auth_user = g_quark_from_static_string ("authorized-user"); + quark_auth_user = g_quark_from_static_string ("authorized-user"); } -static void _unwatch_name (gpointer id) +static void +_unwatch_name (gpointer id) { - g_bus_unwatch_name (GPOINTER_TO_INT (id)); + g_bus_unwatch_name (GPOINTER_TO_INT (id)); } -static void fprint_device_init(FprintDevice *device) +static void +fprint_device_init (FprintDevice *device) { - FprintDevicePrivate *priv = fprint_device_get_instance_private(device); - priv->id = ++last_id; + FprintDevicePrivate *priv = fprint_device_get_instance_private (device); - /* Setup PolicyKit */ - priv->auth = polkit_authority_get_sync (NULL, NULL); - priv->clients = g_hash_table_new_full (g_str_hash, - g_str_equal, - g_free, - _unwatch_name); + priv->id = ++last_id; - g_signal_connect (device, "g-authorize-method", - G_CALLBACK (action_authorization_handler), - NULL); + /* Setup PolicyKit */ + priv->auth = polkit_authority_get_sync (NULL, NULL); + priv->clients = g_hash_table_new_full (g_str_hash, + g_str_equal, + g_free, + _unwatch_name); + + g_signal_connect (device, "g-authorize-method", + G_CALLBACK (action_authorization_handler), + NULL); } -FprintDevice *fprint_device_new(FpDevice *dev) +FprintDevice * +fprint_device_new (FpDevice *dev) { - return g_object_new(FPRINT_TYPE_DEVICE, "dev", dev, NULL); + return g_object_new (FPRINT_TYPE_DEVICE, "dev", dev, NULL); } -guint32 _fprint_device_get_id(FprintDevice *rdev) +guint32 +_fprint_device_get_id (FprintDevice *rdev) { - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - return priv->id; + return priv->id; } static const char * fp_finger_to_name (FpFinger finger) { - if (finger == FP_FINGER_UNKNOWN) - return "any"; - if (!FP_FINGER_IS_VALID (finger)) - return NULL; - return FINGERS_NAMES[finger]; + if (finger == FP_FINGER_UNKNOWN) + return "any"; + if (!FP_FINGER_IS_VALID (finger)) + return NULL; + return FINGERS_NAMES[finger]; } static FpFinger finger_name_to_fp_finger (const char *finger_name) { - FpFinger i; + FpFinger i; - if (finger_name == NULL || *finger_name == '\0' || g_str_equal (finger_name, "any")) - return FP_FINGER_UNKNOWN; + if (finger_name == NULL || *finger_name == '\0' || g_str_equal (finger_name, "any")) + return FP_FINGER_UNKNOWN; - for (i = FP_FINGER_FIRST; i <= FP_FINGER_LAST; i++) { - if (g_str_equal (finger_name, FINGERS_NAMES[i])) - return i; - } + for (i = FP_FINGER_FIRST; i <= FP_FINGER_LAST; i++) + if (g_str_equal (finger_name, FINGERS_NAMES[i])) + return i; - /* Invalid, let's try that */ - return FP_FINGER_UNKNOWN; + /* Invalid, let's try that */ + return FP_FINGER_UNKNOWN; } static const char * verify_result_to_name (gboolean match, GError *error) { - if (!error) { - if (match) - return "verify-match"; - else - return "verify-no-match"; - } + if (!error) + { + if (match) + return "verify-match"; + else + return "verify-no-match"; + } - if (error->domain == FP_DEVICE_RETRY) { - switch (error->code) { - case FP_DEVICE_RETRY_TOO_SHORT: - return "verify-swipe-too-short"; - case FP_DEVICE_RETRY_CENTER_FINGER: - return "verify-finger-not-centered"; - case FP_DEVICE_RETRY_REMOVE_FINGER: - return "verify-remove-and-retry"; - default: - return "verify-retry-scan"; - } - } else { - /* Which errors should be mapped to disconnection? - * Are drivers/libfprint/fprintd really in agreement here? - */ - if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_PROTO)) - return "verify-disconnected"; - else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - return "verify-no-match"; + if (error->domain == FP_DEVICE_RETRY) + { + switch (error->code) + { + case FP_DEVICE_RETRY_TOO_SHORT: + return "verify-swipe-too-short"; - return "verify-unknown-error"; - } + case FP_DEVICE_RETRY_CENTER_FINGER: + return "verify-finger-not-centered"; + + case FP_DEVICE_RETRY_REMOVE_FINGER: + return "verify-remove-and-retry"; + + default: + return "verify-retry-scan"; + } + } + else + { + /* Which errors should be mapped to disconnection? + * Are drivers/libfprint/fprintd really in agreement here? + */ + if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_PROTO)) + return "verify-disconnected"; + else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + return "verify-no-match"; + + return "verify-unknown-error"; + } } static const char * enroll_result_to_name (gboolean completed, gboolean enrolled, GError *error) { - if (!error) { - if (!completed) - return "enroll-stage-passed"; - else if (enrolled) - return "enroll-completed"; - else - return "enroll-failed"; - } + if (!error) + { + if (!completed) + return "enroll-stage-passed"; + else if (enrolled) + return "enroll-completed"; + else + return "enroll-failed"; + } - if (error->domain == FP_DEVICE_RETRY) { - switch (error->code) { - case FP_DEVICE_RETRY_TOO_SHORT: - return "enroll-swipe-too-short"; - case FP_DEVICE_RETRY_CENTER_FINGER: - return "enroll-finger-not-centered"; - case FP_DEVICE_RETRY_REMOVE_FINGER: - return "enroll-remove-and-retry"; - default: - return "enroll-retry-scan"; - } - } else { - /* Which errors should be mapped to disconnection? - * Are drivers/libfprint/fprintd really in agreement here? - */ - if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_PROTO)) - return "enroll-disconnected"; - else if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_FULL)) - return "enroll-data-full"; - else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - return "enroll-failed"; + if (error->domain == FP_DEVICE_RETRY) + { + switch (error->code) + { + case FP_DEVICE_RETRY_TOO_SHORT: + return "enroll-swipe-too-short"; - return "enroll-unknown-error"; - } + case FP_DEVICE_RETRY_CENTER_FINGER: + return "enroll-finger-not-centered"; + + case FP_DEVICE_RETRY_REMOVE_FINGER: + return "enroll-remove-and-retry"; + + default: + return "enroll-retry-scan"; + } + } + else + { + /* Which errors should be mapped to disconnection? + * Are drivers/libfprint/fprintd really in agreement here? + */ + if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_PROTO)) + return "enroll-disconnected"; + else if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_FULL)) + return "enroll-data-full"; + else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + return "enroll-failed"; + + return "enroll-unknown-error"; + } } static FprintDevicePermission get_permissions_for_invocation (GDBusMethodInvocation *invocation) { - FprintDevicePermission required_perms; - const char *method_name; + FprintDevicePermission required_perms; + const char *method_name; - required_perms = FPRINT_DEVICE_PERMISSION_NONE; - method_name = g_dbus_method_invocation_get_method_name (invocation); + required_perms = FPRINT_DEVICE_PERMISSION_NONE; + method_name = g_dbus_method_invocation_get_method_name (invocation); - if (g_str_equal (method_name, "Claim")) { - required_perms |= FPRINT_DEVICE_PERMISSION_VERIFY; - required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; - } else if (g_str_equal (method_name, "DeleteEnrolledFingers")) { - required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; - } else if (g_str_equal (method_name, "DeleteEnrolledFingers2")) { - required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; - } else if (g_str_equal (method_name, "EnrollStart")) { - required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; - } else if (g_str_equal (method_name, "ListEnrolledFingers")) { - required_perms |= FPRINT_DEVICE_PERMISSION_VERIFY; - } else if (g_str_equal (method_name, "VerifyStart")) { - required_perms |= FPRINT_DEVICE_PERMISSION_VERIFY; - } else if (g_str_equal (method_name, "Release")) { - } else if (g_str_equal (method_name, "EnrollStop")) { - } else if (g_str_equal (method_name, "VerifyStop")) { - /* Don't require permissiong for for release/stop operations. - * We are authenticated already if we could start, and we don't - * want to end up authorizing interactively again. - */ - } else { - g_assert_not_reached (); - } + if (g_str_equal (method_name, "Claim")) + { + required_perms |= FPRINT_DEVICE_PERMISSION_VERIFY; + required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; + } + else if (g_str_equal (method_name, "DeleteEnrolledFingers")) + { + required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; + } + else if (g_str_equal (method_name, "DeleteEnrolledFingers2")) + { + required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; + } + else if (g_str_equal (method_name, "EnrollStart")) + { + required_perms |= FPRINT_DEVICE_PERMISSION_ENROLL; + } + else if (g_str_equal (method_name, "ListEnrolledFingers")) + { + required_perms |= FPRINT_DEVICE_PERMISSION_VERIFY; + } + else if (g_str_equal (method_name, "VerifyStart")) + { + required_perms |= FPRINT_DEVICE_PERMISSION_VERIFY; + } + else if (g_str_equal (method_name, "Release")) + { + } + else if (g_str_equal (method_name, "EnrollStop")) + { + } + else if (g_str_equal (method_name, "VerifyStop")) + { + /* Don't require permissiong for for release/stop operations. + * We are authenticated already if we could start, and we don't + * want to end up authorizing interactively again. + */ + } + else + { + g_assert_not_reached (); + } - return required_perms; + return required_perms; } static FprintDeviceClaimState get_claim_state_for_invocation (GDBusMethodInvocation *invocation) { - const char *method_name; + const char *method_name; - method_name = g_dbus_method_invocation_get_method_name (invocation); + method_name = g_dbus_method_invocation_get_method_name (invocation); - if (g_str_equal (method_name, "Claim")) { - return STATE_UNCLAIMED; - } else if (g_str_equal (method_name, "DeleteEnrolledFingers")) { - return STATE_AUTO_CLAIM; - } else if (g_str_equal (method_name, "ListEnrolledFingers")) { - return STATE_ANYTIME; - } + if (g_str_equal (method_name, "Claim")) + return STATE_UNCLAIMED; + else if (g_str_equal (method_name, "DeleteEnrolledFingers")) + return STATE_AUTO_CLAIM; + else if (g_str_equal (method_name, "ListEnrolledFingers")) + return STATE_ANYTIME; - return STATE_CLAIMED; + return STATE_CLAIMED; } static gboolean -_fprint_device_check_claimed (FprintDevice *rdev, - GDBusMethodInvocation *invocation, - GError **error) +_fprint_device_check_claimed (FprintDevice *rdev, + GDBusMethodInvocation *invocation, + GError **error) { - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr(SessionData) session = NULL; - FprintDeviceClaimState requested_state; - const char *sender; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - requested_state = get_claim_state_for_invocation (invocation); + g_autoptr(SessionData) session = NULL; + FprintDeviceClaimState requested_state; + const char *sender; - if (requested_state == STATE_ANYTIME) - return TRUE; + requested_state = get_claim_state_for_invocation (invocation); - session = session_data_get (priv); - if (requested_state == STATE_AUTO_CLAIM) - requested_state = session ? STATE_CLAIMED : STATE_UNCLAIMED; + if (requested_state == STATE_ANYTIME) + return TRUE; - if (requested_state == STATE_UNCLAIMED) { - /* Is it already claimed? */ - if (!session) { - return TRUE; - } + session = session_data_get (priv); + if (requested_state == STATE_AUTO_CLAIM) + requested_state = session ? STATE_CLAIMED : STATE_UNCLAIMED; - g_set_error(error, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, - "Device was already claimed"); - return FALSE; - } + if (requested_state == STATE_UNCLAIMED) + { + /* Is it already claimed? */ + if (!session) + return TRUE; - g_assert (requested_state == STATE_CLAIMED); + g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, + "Device was already claimed"); + return FALSE; + } - /* The device wasn't claimed, exit */ - if (session == NULL) { - g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_CLAIM_DEVICE, - _("Device was not claimed before use")); - return FALSE; - } + g_assert (requested_state == STATE_CLAIMED); - sender = g_dbus_method_invocation_get_sender (invocation); + /* The device wasn't claimed, exit */ + if (session == NULL) + { + g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_CLAIM_DEVICE, + _("Device was not claimed before use")); + return FALSE; + } - if (!g_str_equal (sender, session->sender) || session->invocation != NULL) { - g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, - _("Device already in use by another user")); - return FALSE; - } + sender = g_dbus_method_invocation_get_sender (invocation); - return TRUE; + if (!g_str_equal (sender, session->sender) || session->invocation != NULL) + { + g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, + _("Device already in use by another user")); + return FALSE; + } + + return TRUE; } static gboolean -_fprint_device_check_polkit_for_action (FprintDevice *rdev, - GDBusMethodInvocation *invocation, - const char *action, - GError **error) +_fprint_device_check_polkit_for_action (FprintDevice *rdev, + GDBusMethodInvocation *invocation, + const char *action, + GError **error) { - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - const char *sender; - g_autoptr(GError) local_error = NULL; - g_autoptr(PolkitAuthorizationResult) result = NULL; - g_autoptr(PolkitSubject) subject = NULL; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + const char *sender; - /* Check that caller is privileged */ - sender = g_dbus_method_invocation_get_sender (invocation); - subject = polkit_system_bus_name_new (sender); + g_autoptr(GError) local_error = NULL; + g_autoptr(PolkitAuthorizationResult) result = NULL; + g_autoptr(PolkitSubject) subject = NULL; - result = polkit_authority_check_authorization_sync (priv->auth, - subject, - action, - NULL, - POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, - NULL, &local_error); - if (result == NULL) { - g_set_error (error, FPRINT_ERROR, - FPRINT_ERROR_PERMISSION_DENIED, - "Not Authorized: %s", local_error->message); - return FALSE; - } + /* Check that caller is privileged */ + sender = g_dbus_method_invocation_get_sender (invocation); + subject = polkit_system_bus_name_new (sender); - if (!polkit_authorization_result_get_is_authorized (result)) { - g_set_error (error, FPRINT_ERROR, - FPRINT_ERROR_PERMISSION_DENIED, - "Not Authorized: %s", action); - return FALSE; - } + result = polkit_authority_check_authorization_sync (priv->auth, + subject, + action, + NULL, + POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, + NULL, &local_error); + if (result == NULL) + { + g_set_error (error, FPRINT_ERROR, + FPRINT_ERROR_PERMISSION_DENIED, + "Not Authorized: %s", local_error->message); + return FALSE; + } - return TRUE; + if (!polkit_authorization_result_get_is_authorized (result)) + { + g_set_error (error, FPRINT_ERROR, + FPRINT_ERROR_PERMISSION_DENIED, + "Not Authorized: %s", action); + return FALSE; + } + + return TRUE; } static gboolean -fprint_device_check_polkit_for_permissions (FprintDevice *rdev, - GDBusMethodInvocation *invocation, - FprintDevicePermission permissions, - GError **error) +fprint_device_check_polkit_for_permissions (FprintDevice *rdev, + GDBusMethodInvocation *invocation, + FprintDevicePermission permissions, + GError **error) { - g_autoptr(GFlagsClass) permission_flags = NULL; - unsigned i; + g_autoptr(GFlagsClass) permission_flags = NULL; + unsigned i; - if (permissions == FPRINT_DEVICE_PERMISSION_NONE) - return TRUE; + if (permissions == FPRINT_DEVICE_PERMISSION_NONE) + return TRUE; - permission_flags = g_type_class_ref (FPRINT_TYPE_DEVICE_PERMISSION); + permission_flags = g_type_class_ref (FPRINT_TYPE_DEVICE_PERMISSION); - for (i = 0; i < permission_flags->n_values; ++i) { - GFlagsValue *value = &permission_flags->values[i]; - const char *action; + for (i = 0; i < permission_flags->n_values; ++i) + { + GFlagsValue *value = &permission_flags->values[i]; + const char *action; - if (!(value->value & permissions)) { - continue; - } + if (!(value->value & permissions)) + continue; - action = value->value_nick; - g_debug ("Getting authorization to perform Polkit action %s", - action); + action = value->value_nick; + g_debug ("Getting authorization to perform Polkit action %s", + action); - g_clear_error (error); - if (_fprint_device_check_polkit_for_action (rdev, invocation, - action, error)) { - return TRUE; - } - } + g_clear_error (error); + if (_fprint_device_check_polkit_for_action (rdev, invocation, + action, error)) + return TRUE; + } - g_assert (!error || *error); - return FALSE; + g_assert (!error || *error); + return FALSE; } static char * -_fprint_device_check_for_username (FprintDevice *rdev, - GDBusMethodInvocation *invocation, - const char *username, - GError **error) +_fprint_device_check_for_username (FprintDevice *rdev, + GDBusMethodInvocation *invocation, + const char *username, + GError **error) { - g_autoptr(GVariant) ret = NULL; - g_autoptr(GError) err = NULL; - GDBusConnection *connection; - const char *sender; - struct passwd *user; - guint32 uid; + g_autoptr(GVariant) ret = NULL; + g_autoptr(GError) err = NULL; + GDBusConnection *connection; + const char *sender; + struct passwd *user; + guint32 uid; - /* Get details about the current sender, and username/uid */ - connection = g_dbus_method_invocation_get_connection (invocation); - sender = g_dbus_method_invocation_get_sender (invocation); + /* Get details about the current sender, and username/uid */ + connection = g_dbus_method_invocation_get_connection (invocation); + sender = g_dbus_method_invocation_get_sender (invocation); - ret = g_dbus_connection_call_sync (connection, - "org.freedesktop.DBus", - "/org/freedesktop/DBus", - "org.freedesktop.DBus", - "GetConnectionUnixUser", - g_variant_new ("(s)", sender), - NULL, G_DBUS_CALL_FLAGS_NONE, -1, - NULL, &err); + ret = g_dbus_connection_call_sync (connection, + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus", + "GetConnectionUnixUser", + g_variant_new ("(s)", sender), + NULL, G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &err); - if (!ret) { - g_autoptr(GError) e = NULL; + if (!ret) + { + g_autoptr(GError) e = NULL; - g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_INTERNAL, - "Could not get conection unix user ID: %s", - err->message); - return NULL; - } + g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_INTERNAL, + "Could not get conection unix user ID: %s", + err->message); + return NULL; + } - g_variant_get (ret, "(u)", &uid); - user = getpwuid (uid); - if (user == NULL) { - g_set_error(error, FPRINT_ERROR, FPRINT_ERROR_INTERNAL, - "Failed to get information about user UID %u", uid); - return NULL; - } + g_variant_get (ret, "(u)", &uid); + user = getpwuid (uid); + if (user == NULL) + { + g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_INTERNAL, + "Failed to get information about user UID %u", uid); + return NULL; + } - /* The current user is usually allowed to access their - * own data, this should be followed by PolicyKit checks - * anyway */ - if (username == NULL || *username == '\0' || g_str_equal (username, user->pw_name)) { - return g_strdup (user->pw_name); - } + /* The current user is usually allowed to access their + * own data, this should be followed by PolicyKit checks + * anyway */ + if (username == NULL || *username == '\0' || g_str_equal (username, user->pw_name)) + return g_strdup (user->pw_name); - /* If we're not allowed to set a different username, - * then fail */ - if (!fprint_device_check_polkit_for_permissions (rdev, invocation, - FPRINT_DEVICE_PERMISSION_SETUSERNAME, - error)) { - return NULL; - } + /* If we're not allowed to set a different username, + * then fail */ + if (!fprint_device_check_polkit_for_permissions (rdev, invocation, + FPRINT_DEVICE_PERMISSION_SETUSERNAME, + error)) + return NULL; - return g_strdup (username); + return g_strdup (username); } static void _fprint_device_client_vanished (GDBusConnection *connection, - const char *name, - FprintDevice *rdev) + const char *name, + FprintDevice *rdev) { - g_autoptr(GError) error = NULL; - g_autoptr(SessionData) session = NULL; - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); + g_autoptr(GError) error = NULL; + g_autoptr(SessionData) session = NULL; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - session = session_data_get (priv); + session = session_data_get (priv); - /* Was that the client that claimed the device? */ - if (session != NULL && - g_strcmp0 (session->sender, name) == 0) { - while (priv->current_action != ACTION_NONE) { - /* OPEN/CLOSE are not cancellable, we just need to wait */ - if (priv->current_cancellable) - g_cancellable_cancel (priv->current_cancellable); + /* Was that the client that claimed the device? */ + if (session != NULL && + g_strcmp0 (session->sender, name) == 0) + { + while (priv->current_action != ACTION_NONE) + { + /* OPEN/CLOSE are not cancellable, we just need to wait */ + if (priv->current_cancellable) + g_cancellable_cancel (priv->current_cancellable); - g_main_context_iteration (NULL, TRUE); - } + g_main_context_iteration (NULL, TRUE); + } - /* The session may have disappeared at this point if the device - * was already closing. */ - g_clear_pointer (&session, session_data_unref); - session = session_data_get (priv); - if (session && !fp_device_close_sync (priv->dev, NULL, &error)) - g_critical ("Error closing device after disconnect: %s", error->message); + /* The session may have disappeared at this point if the device + * was already closing. */ + g_clear_pointer (&session, session_data_unref); + session = session_data_get (priv); + if (session && !fp_device_close_sync (priv->dev, NULL, &error)) + g_critical ("Error closing device after disconnect: %s", error->message); - session_data_set_new (priv, NULL, NULL); - } - g_hash_table_remove (priv->clients, name); + session_data_set_new (priv, NULL, NULL); + } + g_hash_table_remove (priv->clients, name); - if (g_hash_table_size (priv->clients) == 0) { - g_object_notify (G_OBJECT (rdev), "in-use"); - } + if (g_hash_table_size (priv->clients) == 0) + g_object_notify (G_OBJECT (rdev), "in-use"); } static void _fprint_device_add_client (FprintDevice *rdev, const char *sender) { - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - guint id; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + guint id; - id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->clients, sender)); - if (id == 0) { - id = g_bus_watch_name (G_BUS_TYPE_SYSTEM, - sender, - G_BUS_NAME_WATCHER_FLAGS_NONE, - NULL, - (GBusNameVanishedCallback) _fprint_device_client_vanished, - rdev, - NULL); - g_hash_table_insert (priv->clients, g_strdup (sender), GUINT_TO_POINTER(id)); - g_object_notify (G_OBJECT (rdev), "in-use"); - } + id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->clients, sender)); + if (id == 0) + { + id = g_bus_watch_name (G_BUS_TYPE_SYSTEM, + sender, + G_BUS_NAME_WATCHER_FLAGS_NONE, + NULL, + (GBusNameVanishedCallback) _fprint_device_client_vanished, + rdev, + NULL); + g_hash_table_insert (priv->clients, g_strdup (sender), GUINT_TO_POINTER (id)); + g_object_notify (G_OBJECT (rdev), "in-use"); + } } -static void dev_open_cb(FpDevice *dev, GAsyncResult *res, void *user_data) +static void +dev_open_cb (FpDevice *dev, GAsyncResult *res, void *user_data) { - g_autoptr(GError) error = NULL; - FprintDevice *rdev = user_data; - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr(SessionData) session = NULL; - g_autoptr(GDBusMethodInvocation) invocation = NULL; + g_autoptr(GError) error = NULL; + FprintDevice *rdev = user_data; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - session = session_data_get (priv); - invocation = g_steal_pointer (&session->invocation); + g_autoptr(SessionData) session = NULL; + g_autoptr(GDBusMethodInvocation) invocation = NULL; - priv->current_action = ACTION_NONE; - if (!fp_device_open_finish (dev, res, &error)) { - g_autoptr(GError) dbus_error = NULL; + session = session_data_get (priv); + invocation = g_steal_pointer (&session->invocation); - dbus_error = g_error_new (FPRINT_ERROR, - FPRINT_ERROR_INTERNAL, - "Open failed with error: %s", error->message); - g_dbus_method_invocation_return_gerror (invocation, dbus_error); - session_data_set_new (priv, NULL, NULL); - return; - } + priv->current_action = ACTION_NONE; + if (!fp_device_open_finish (dev, res, &error)) + { + g_autoptr(GError) dbus_error = NULL; - g_debug("claimed device %d", priv->id); + dbus_error = g_error_new (FPRINT_ERROR, + FPRINT_ERROR_INTERNAL, + "Open failed with error: %s", error->message); + g_dbus_method_invocation_return_gerror (invocation, dbus_error); + session_data_set_new (priv, NULL, NULL); + return; + } - fprint_dbus_device_complete_claim (FPRINT_DBUS_DEVICE (rdev), - invocation); + g_debug ("claimed device %d", priv->id); + + fprint_dbus_device_complete_claim (FPRINT_DBUS_DEVICE (rdev), + invocation); } static gboolean -fprintd_device_authorize_user (FprintDevice *rdev, - GDBusMethodInvocation *invocation, - GError **error) +fprintd_device_authorize_user (FprintDevice *rdev, + GDBusMethodInvocation *invocation, + GError **error) { - GVariant *params = NULL; - const char *username = NULL; - g_autofree char *user = NULL; + GVariant *params = NULL; + const char *username = NULL; + g_autofree char *user = NULL; - params = g_dbus_method_invocation_get_parameters (invocation); - g_assert (g_variant_n_children (params) == 1); - g_variant_get (params, "(&s)", &username); - g_assert (username); + params = g_dbus_method_invocation_get_parameters (invocation); + g_assert (g_variant_n_children (params) == 1); + g_variant_get (params, "(&s)", &username); + g_assert (username); - user = _fprint_device_check_for_username (rdev, - invocation, - username, - error); - if (user == NULL) { - return FALSE; - } + user = _fprint_device_check_for_username (rdev, + invocation, + username, + error); + if (user == NULL) + return FALSE; - /* We keep the user attached to the invocation as it may not be the same - * of the requested one, in case an empty one was passed. - * Given that now we may have multiple cuncurrent requests, it wouldn't - * be safe to add another member to the priv, as it would need even more - * multi-thread checks around, and over-complicate things. - */ - g_object_set_qdata_full (G_OBJECT (invocation), quark_auth_user, - g_steal_pointer (&user), g_free); + /* We keep the user attached to the invocation as it may not be the same + * of the requested one, in case an empty one was passed. + * Given that now we may have multiple cuncurrent requests, it wouldn't + * be safe to add another member to the priv, as it would need even more + * multi-thread checks around, and over-complicate things. + */ + g_object_set_qdata_full (G_OBJECT (invocation), quark_auth_user, + g_steal_pointer (&user), g_free); - return TRUE; + return TRUE; } -static gboolean fprint_device_claim (FprintDBusDevice *dbus_dev, - GDBusMethodInvocation *invocation, - const char *username) +static gboolean +fprint_device_claim (FprintDBusDevice *dbus_dev, + GDBusMethodInvocation *invocation, + const char *username) { - FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr(SessionData) session = NULL; - g_autoptr(GError) error = NULL; - char *sender, *user; + FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - if (!_fprint_device_check_claimed (rdev, invocation, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } + g_autoptr(SessionData) session = NULL; + g_autoptr(GError) error = NULL; + char *sender, *user; - user = g_object_steal_qdata (G_OBJECT (invocation), quark_auth_user); - g_assert (user); - g_assert (g_str_equal (username, "") || g_str_equal (user, username)); + if (!_fprint_device_check_claimed (rdev, invocation, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - sender = g_strdup (g_dbus_method_invocation_get_sender (invocation)); - _fprint_device_add_client (rdev, sender); + user = g_object_steal_qdata (G_OBJECT (invocation), quark_auth_user); + g_assert (user); + g_assert (g_str_equal (username, "") || g_str_equal (user, username)); - session = session_data_set_new (priv, g_steal_pointer (&sender), g_steal_pointer(&user)); - session->invocation = g_object_ref (invocation); + sender = g_strdup (g_dbus_method_invocation_get_sender (invocation)); + _fprint_device_add_client (rdev, sender); - g_debug ("user '%s' claiming the device: %d", session->username, priv->id); + session = session_data_set_new (priv, g_steal_pointer (&sender), g_steal_pointer (&user)); + session->invocation = g_object_ref (invocation); - priv->current_action = ACTION_OPEN; - fp_device_open (priv->dev, NULL, (GAsyncReadyCallback) dev_open_cb, rdev); + g_debug ("user '%s' claiming the device: %d", session->username, priv->id); - return TRUE; + priv->current_action = ACTION_OPEN; + fp_device_open (priv->dev, NULL, (GAsyncReadyCallback) dev_open_cb, rdev); + + return TRUE; } -static void dev_close_cb(FpDevice *dev, GAsyncResult *res, void *user_data) +static void +dev_close_cb (FpDevice *dev, GAsyncResult *res, void *user_data) { - g_autoptr(GError) error = NULL; - FprintDevice *rdev = user_data; - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr(SessionData) session = NULL; - g_autoptr(GDBusMethodInvocation) invocation = NULL; + g_autoptr(GError) error = NULL; + FprintDevice *rdev = user_data; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - session = session_data_get (priv); - session_data_set_new (priv, NULL, NULL); - invocation = g_steal_pointer (&session->invocation); + g_autoptr(SessionData) session = NULL; + g_autoptr(GDBusMethodInvocation) invocation = NULL; - priv->current_action = ACTION_NONE; - if (!fp_device_close_finish (dev, res, &error)) { - g_autoptr(GError) dbus_error = NULL; + session = session_data_get (priv); + session_data_set_new (priv, NULL, NULL); + invocation = g_steal_pointer (&session->invocation); - dbus_error = g_error_new (FPRINT_ERROR, - FPRINT_ERROR_INTERNAL, - "Release failed with error: %s", error->message); - g_dbus_method_invocation_return_gerror (invocation, dbus_error); - return; - } + priv->current_action = ACTION_NONE; + if (!fp_device_close_finish (dev, res, &error)) + { + g_autoptr(GError) dbus_error = NULL; - g_debug("released device %d", priv->id); + dbus_error = g_error_new (FPRINT_ERROR, + FPRINT_ERROR_INTERNAL, + "Release failed with error: %s", error->message); + g_dbus_method_invocation_return_gerror (invocation, dbus_error); + return; + } - fprint_dbus_device_complete_release (FPRINT_DBUS_DEVICE (rdev), - invocation); + g_debug ("released device %d", priv->id); + + fprint_dbus_device_complete_release (FPRINT_DBUS_DEVICE (rdev), + invocation); } -static gboolean fprint_device_release (FprintDBusDevice *dbus_dev, - GDBusMethodInvocation *invocation) +static gboolean +fprint_device_release (FprintDBusDevice *dbus_dev, + GDBusMethodInvocation *invocation) { - g_autoptr(GError) error = NULL; - g_autoptr(SessionData) session = NULL; - FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); + g_autoptr(GError) error = NULL; + g_autoptr(SessionData) session = NULL; + FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - if (!_fprint_device_check_claimed (rdev, invocation, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } + if (!_fprint_device_check_claimed (rdev, invocation, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - if (priv->current_cancellable) { - if (priv->current_action == ACTION_ENROLL) { - g_warning("Enrollment was in progress, stopping it"); - } else if (priv->current_action == ACTION_IDENTIFY || - priv->current_action == ACTION_VERIFY) { - g_warning("Verification was in progress, stopping it"); - } else if (priv->current_action == ACTION_DELETE) { - g_warning("Deletion was in progress, stopping it"); - } + if (priv->current_cancellable) + { + if (priv->current_action == ACTION_ENROLL) + g_warning ("Enrollment was in progress, stopping it"); + else if (priv->current_action == ACTION_IDENTIFY || + priv->current_action == ACTION_VERIFY) + g_warning ("Verification was in progress, stopping it"); + else if (priv->current_action == ACTION_DELETE) + g_warning ("Deletion was in progress, stopping it"); - g_cancellable_cancel (priv->current_cancellable); - while (priv->current_action != ACTION_NONE) - g_main_context_iteration (NULL, TRUE); - } + g_cancellable_cancel (priv->current_cancellable); + while (priv->current_action != ACTION_NONE) + g_main_context_iteration (NULL, TRUE); + } - session = session_data_get (priv); - session->invocation = g_object_ref (invocation); + session = session_data_get (priv); + session->invocation = g_object_ref (invocation); - priv->current_action = ACTION_CLOSE; - fp_device_close (priv->dev, NULL, (GAsyncReadyCallback) dev_close_cb, rdev); + priv->current_action = ACTION_CLOSE; + fp_device_close (priv->dev, NULL, (GAsyncReadyCallback) dev_close_cb, rdev); - return TRUE; + return TRUE; } -static void report_verify_status (FprintDevice *rdev, - gboolean match, - GError *error) +static void +report_verify_status (FprintDevice *rdev, + gboolean match, + GError *error) { - FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - const char *result = verify_result_to_name (match, error); - g_autoptr(SessionData) session = NULL; - gboolean done; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + const char *result = verify_result_to_name (match, error); - done = (error == NULL || error->domain != FP_DEVICE_RETRY); + g_autoptr(SessionData) session = NULL; + gboolean done; - session = session_data_get (priv); + done = (error == NULL || error->domain != FP_DEVICE_RETRY); - if (done && session->verify_status_reported) { - /* It is completely fine for cancellation to occur after a - * result has been reported. */ - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning ("Verify status already reported. Ignoring %s", result); - return; - } + session = session_data_get (priv); - g_debug ("report_verify_status: result %s", result); - g_signal_emit (rdev, signals[SIGNAL_VERIFY_STATUS], 0, result, done); + if (done && session->verify_status_reported) + { + /* It is completely fine for cancellation to occur after a + * result has been reported. */ + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning ("Verify status already reported. Ignoring %s", result); + return; + } - if (done) - session->verify_status_reported = TRUE; + g_debug ("report_verify_status: result %s", result); + g_signal_emit (rdev, signals[SIGNAL_VERIFY_STATUS], 0, result, done); + + if (done) + session->verify_status_reported = TRUE; } -static gboolean can_start_action(FprintDevice *rdev, GError **error) { - FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); +static gboolean +can_start_action (FprintDevice *rdev, GError **error) +{ + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - switch (priv->current_action) { - case ACTION_NONE: - return TRUE; - case ACTION_ENROLL: - g_set_error (error, - FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, - "Enrollment already in progress"); - break; - case ACTION_IDENTIFY: - case ACTION_VERIFY: - g_set_error (error, - FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, - "Enrollment already in progress"); - break; - case ACTION_OPEN: - g_set_error (error, - FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, - "Claim already in progress"); - break; - case ACTION_CLOSE: - g_set_error (error, - FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, - "Release already in progress"); - break; - case ACTION_DELETE: - g_set_error (error, - FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, - "Delete already in progress"); - break; - default: /* Fallback only. */ - g_assert_not_reached(); - g_set_error (error, - FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, - "Another operation is already in progress"); - } + switch (priv->current_action) + { + case ACTION_NONE: + return TRUE; - return FALSE; + case ACTION_ENROLL: + g_set_error (error, + FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, + "Enrollment already in progress"); + break; + + case ACTION_IDENTIFY: + case ACTION_VERIFY: + g_set_error (error, + FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, + "Enrollment already in progress"); + break; + + case ACTION_OPEN: + g_set_error (error, + FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, + "Claim already in progress"); + break; + + case ACTION_CLOSE: + g_set_error (error, + FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, + "Release already in progress"); + break; + + case ACTION_DELETE: + g_set_error (error, + FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, + "Delete already in progress"); + break; + + default: /* Fallback only. */ + g_assert_not_reached (); + g_set_error (error, + FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, + "Another operation is already in progress"); + } + + return FALSE; } -static void match_cb (FpDevice *device, - FpPrint *match, - FpPrint *print, - gpointer user_data, - GError *error) +static void +match_cb (FpDevice *device, + FpPrint *match, + FpPrint *print, + gpointer user_data, + GError *error) { - FprintDevice *rdev = user_data; - FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - gboolean matched; - gboolean cancelled; + FprintDevice *rdev = user_data; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + gboolean matched; + gboolean cancelled; - g_assert_true (error == NULL || error->domain == FP_DEVICE_RETRY); + g_assert_true (error == NULL || error->domain == FP_DEVICE_RETRY); - cancelled = g_cancellable_is_cancelled (priv->current_cancellable); - matched = match != NULL && cancelled == FALSE; + cancelled = g_cancellable_is_cancelled (priv->current_cancellable); + matched = match != NULL && cancelled == FALSE; - /* No-match is reported only after the operation completes. - * This avoids problems when the operation is immediately restarted. */ - report_verify_status (rdev, matched, error); + /* No-match is reported only after the operation completes. + * This avoids problems when the operation is immediately restarted. */ + report_verify_status (rdev, matched, error); } -static void verify_cb(FpDevice *dev, GAsyncResult *res, void *user_data) +static void +verify_cb (FpDevice *dev, GAsyncResult *res, void *user_data) { - g_autoptr(GError) error = NULL; - g_autoptr(SessionData) session = NULL; - FprintDevice *rdev = user_data; - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); - gboolean success; - const char *name; - gboolean match; + g_autoptr(GError) error = NULL; + g_autoptr(SessionData) session = NULL; + FprintDevice *rdev = user_data; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); + gboolean success; + const char *name; + gboolean match; - success = fp_device_verify_finish (dev, res, &match, NULL, &error); - g_assert (!!success == !error); - name = verify_result_to_name (match, error); + success = fp_device_verify_finish (dev, res, &match, NULL, &error); + g_assert (!!success == !error); + name = verify_result_to_name (match, error); - session = session_data_get (priv); + session = session_data_get (priv); - g_debug("verify_cb: result %s", name); + g_debug ("verify_cb: result %s", name); - /* Automatically restart the operation for retry failures */ - if (error && error->domain == FP_DEVICE_RETRY) { - fp_device_verify (priv->dev, - priv->verify_data, - priv->current_cancellable, - match_cb, rdev, NULL, - (GAsyncReadyCallback) verify_cb, - rdev); - } else { - g_clear_object (&priv->verify_data); + /* Automatically restart the operation for retry failures */ + if (error && error->domain == FP_DEVICE_RETRY) + { + fp_device_verify (priv->dev, + priv->verify_data, + priv->current_cancellable, + match_cb, rdev, NULL, + (GAsyncReadyCallback) verify_cb, + rdev); + } + else + { + g_clear_object (&priv->verify_data); - if (error) { - report_verify_status (rdev, FALSE, error); + if (error) + { + report_verify_status (rdev, FALSE, error); - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { - g_warning ("Device reported an error during verify: %s", - error->message); - } - } + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning ("Device reported an error during verify: %s", + error->message); + } - /* Return the cancellation or reset action right away if vanished. */ - if (priv->current_cancel_invocation) { - fprint_dbus_device_complete_verify_stop (dbus_dev, - g_steal_pointer (&priv->current_cancel_invocation)); - priv->current_action = ACTION_NONE; - session->verify_status_reported = FALSE; - } else if (g_cancellable_is_cancelled (priv->current_cancellable)) { - priv->current_action = ACTION_NONE; - session->verify_status_reported = FALSE; - } + /* Return the cancellation or reset action right away if vanished. */ + if (priv->current_cancel_invocation) + { + fprint_dbus_device_complete_verify_stop (dbus_dev, + g_steal_pointer (&priv->current_cancel_invocation)); + priv->current_action = ACTION_NONE; + session->verify_status_reported = FALSE; + } + else if (g_cancellable_is_cancelled (priv->current_cancellable)) + { + priv->current_action = ACTION_NONE; + session->verify_status_reported = FALSE; + } - g_clear_object (&priv->current_cancellable); - } + g_clear_object (&priv->current_cancellable); + } } -static void identify_cb(FpDevice *dev, GAsyncResult *res, void *user_data) +static void +identify_cb (FpDevice *dev, GAsyncResult *res, void *user_data) { - g_autoptr(GError) error = NULL; - g_autoptr(FpPrint) match = NULL; - FprintDevice *rdev = user_data; - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); - const char *name; - gboolean success; + g_autoptr(GError) error = NULL; + g_autoptr(FpPrint) match = NULL; + FprintDevice *rdev = user_data; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); + const char *name; + gboolean success; - success = fp_device_identify_finish (dev, res, &match, NULL, &error); - g_assert (!!success == !error); - name = verify_result_to_name (match != NULL, error); + success = fp_device_identify_finish (dev, res, &match, NULL, &error); + g_assert (!!success == !error); + name = verify_result_to_name (match != NULL, error); - g_debug("identify_cb: result %s", name); + g_debug ("identify_cb: result %s", name); - /* Automatically restart the operation for retry failures */ - if (error && error->domain == FP_DEVICE_RETRY) { - fp_device_identify (priv->dev, - priv->identify_data, - priv->current_cancellable, - match_cb, rdev, NULL, - (GAsyncReadyCallback) identify_cb, - rdev); - } else { - g_clear_pointer (&priv->identify_data, g_ptr_array_unref); + /* Automatically restart the operation for retry failures */ + if (error && error->domain == FP_DEVICE_RETRY) + { + fp_device_identify (priv->dev, + priv->identify_data, + priv->current_cancellable, + match_cb, rdev, NULL, + (GAsyncReadyCallback) identify_cb, + rdev); + } + else + { + g_clear_pointer (&priv->identify_data, g_ptr_array_unref); - if (error) { - report_verify_status (rdev, FALSE, error); + if (error) + { + report_verify_status (rdev, FALSE, error); - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { - g_warning ("Device reported an error during identify: %s", - error->message); - } - } + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning ("Device reported an error during identify: %s", + error->message); + } - /* Return the cancellation or reset action right away if vanished. */ - if (priv->current_cancel_invocation) { - fprint_dbus_device_complete_verify_stop (dbus_dev, - g_steal_pointer (&priv->current_cancel_invocation)); - priv->current_action = ACTION_NONE; - } else if (g_cancellable_is_cancelled (priv->current_cancellable)) { - g_autoptr(SessionData) session = NULL; - session = session_data_get (priv); - priv->current_action = ACTION_NONE; - session->verify_status_reported = FALSE; - } + /* Return the cancellation or reset action right away if vanished. */ + if (priv->current_cancel_invocation) + { + fprint_dbus_device_complete_verify_stop (dbus_dev, + g_steal_pointer (&priv->current_cancel_invocation)); + priv->current_action = ACTION_NONE; + } + else if (g_cancellable_is_cancelled (priv->current_cancellable)) + { + g_autoptr(SessionData) session = NULL; + session = session_data_get (priv); + priv->current_action = ACTION_NONE; + session->verify_status_reported = FALSE; + } - g_clear_object (&priv->current_cancellable); - } + g_clear_object (&priv->current_cancellable); + } } -static gboolean fprint_device_verify_start (FprintDBusDevice *dbus_dev, - GDBusMethodInvocation *invocation, - const char *finger_name) +static gboolean +fprint_device_verify_start (FprintDBusDevice *dbus_dev, + GDBusMethodInvocation *invocation, + const char *finger_name) { - FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr(GPtrArray) gallery = NULL; - g_autoptr(FpPrint) print = NULL; - g_autoptr(SessionData) session = NULL; - g_autoptr(GError) error = NULL; - FpFinger finger = finger_name_to_fp_finger (finger_name); + FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - if (!_fprint_device_check_claimed (rdev, invocation, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } + g_autoptr(GPtrArray) gallery = NULL; + g_autoptr(FpPrint) print = NULL; + g_autoptr(SessionData) session = NULL; + g_autoptr(GError) error = NULL; + FpFinger finger = finger_name_to_fp_finger (finger_name); - session = session_data_get (priv); + if (!_fprint_device_check_claimed (rdev, invocation, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - if (!can_start_action (rdev, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } + session = session_data_get (priv); - if (finger == FP_FINGER_UNKNOWN) { - g_autoptr(GSList) prints = NULL; + if (!can_start_action (rdev, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - prints = store.discover_prints(priv->dev, session->username); - if (prints == NULL) { - g_set_error(&error, FPRINT_ERROR, FPRINT_ERROR_NO_ENROLLED_PRINTS, - "No fingerprints enrolled"); - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } - if (fp_device_supports_identify (priv->dev)) { - GSList *l; + if (finger == FP_FINGER_UNKNOWN) + { + g_autoptr(GSList) prints = NULL; - gallery = g_ptr_array_new_with_free_func (g_object_unref); + prints = store.discover_prints (priv->dev, session->username); + if (prints == NULL) + { + g_set_error (&error, FPRINT_ERROR, FPRINT_ERROR_NO_ENROLLED_PRINTS, + "No fingerprints enrolled"); + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } + if (fp_device_supports_identify (priv->dev)) + { + GSList *l; - for (l = prints; l != NULL; l = l->next) { - g_debug ("adding finger %u to the gallery", GPOINTER_TO_UINT (l->data)); - store.print_data_load(priv->dev, GPOINTER_TO_UINT (l->data), - session->username, &print); + gallery = g_ptr_array_new_with_free_func (g_object_unref); - if (print) - g_ptr_array_add (gallery, g_steal_pointer (&print)); - } - } else { - finger = GPOINTER_TO_UINT (prints->data); - } - } + for (l = prints; l != NULL; l = l->next) + { + g_debug ("adding finger %u to the gallery", GPOINTER_TO_UINT (l->data)); + store.print_data_load (priv->dev, GPOINTER_TO_UINT (l->data), + session->username, &print); - if (fp_device_supports_identify (priv->dev) && finger == FP_FINGER_UNKNOWN) { - if (gallery->len == 0) { - g_set_error(&error, FPRINT_ERROR, FPRINT_ERROR_NO_ENROLLED_PRINTS, - "No fingerprints on that device"); - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } - priv->current_action = ACTION_IDENTIFY; + if (print) + g_ptr_array_add (gallery, g_steal_pointer (&print)); + } + } + else + { + finger = GPOINTER_TO_UINT (prints->data); + } + } - g_debug ("start identification device %d", priv->id); - priv->current_cancellable = g_cancellable_new (); - priv->identify_data = g_ptr_array_ref (gallery); - fp_device_identify (priv->dev, gallery, priv->current_cancellable, - match_cb, rdev, NULL, - (GAsyncReadyCallback) identify_cb, rdev); - } else { - priv->current_action = ACTION_VERIFY; + if (fp_device_supports_identify (priv->dev) && finger == FP_FINGER_UNKNOWN) + { + if (gallery->len == 0) + { + g_set_error (&error, FPRINT_ERROR, FPRINT_ERROR_NO_ENROLLED_PRINTS, + "No fingerprints on that device"); + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } + priv->current_action = ACTION_IDENTIFY; - g_debug("start verification device %d finger %d", priv->id, finger); + g_debug ("start identification device %d", priv->id); + priv->current_cancellable = g_cancellable_new (); + priv->identify_data = g_ptr_array_ref (gallery); + fp_device_identify (priv->dev, gallery, priv->current_cancellable, + match_cb, rdev, NULL, + (GAsyncReadyCallback) identify_cb, rdev); + } + else + { + priv->current_action = ACTION_VERIFY; - store.print_data_load(priv->dev, finger, - session->username, &print); + g_debug ("start verification device %d finger %d", priv->id, finger); - if (!print) { - g_set_error(&error, FPRINT_ERROR, FPRINT_ERROR_NO_ENROLLED_PRINTS, - "No such print %d", finger); - g_dbus_method_invocation_return_gerror (invocation, - error); - return TRUE; - } + store.print_data_load (priv->dev, finger, + session->username, &print); - priv->current_cancellable = g_cancellable_new (); - priv->verify_data = g_object_ref (print); - fp_device_verify (priv->dev, print, priv->current_cancellable, - match_cb, rdev, NULL, - (GAsyncReadyCallback) verify_cb, rdev); - } + if (!print) + { + g_set_error (&error, FPRINT_ERROR, FPRINT_ERROR_NO_ENROLLED_PRINTS, + "No such print %d", finger); + g_dbus_method_invocation_return_gerror (invocation, + error); + return TRUE; + } - fprint_dbus_device_complete_verify_start (dbus_dev, invocation); + priv->current_cancellable = g_cancellable_new (); + priv->verify_data = g_object_ref (print); + fp_device_verify (priv->dev, print, priv->current_cancellable, + match_cb, rdev, NULL, + (GAsyncReadyCallback) verify_cb, rdev); + } - /* Emit VerifyFingerSelected telling the front-end which finger - * we selected for auth */ - g_signal_emit(rdev, signals[SIGNAL_VERIFY_FINGER_SELECTED], - 0, fp_finger_to_name (finger)); + fprint_dbus_device_complete_verify_start (dbus_dev, invocation); - return TRUE; + /* Emit VerifyFingerSelected telling the front-end which finger + * we selected for auth */ + g_signal_emit (rdev, signals[SIGNAL_VERIFY_FINGER_SELECTED], + 0, fp_finger_to_name (finger)); + + return TRUE; } -static gboolean fprint_device_verify_stop (FprintDBusDevice *dbus_dev, - GDBusMethodInvocation *invocation) +static gboolean +fprint_device_verify_stop (FprintDBusDevice *dbus_dev, + GDBusMethodInvocation *invocation) { - g_autoptr(SessionData) session = NULL; - FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr(GError) error = NULL; + g_autoptr(SessionData) session = NULL; + FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - if (!_fprint_device_check_claimed (rdev, invocation, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } + g_autoptr(GError) error = NULL; - switch (priv->current_action) { - case ACTION_VERIFY: - case ACTION_IDENTIFY: - break; - case ACTION_NONE: - g_dbus_method_invocation_return_error_literal ( - invocation, FPRINT_ERROR, - FPRINT_ERROR_NO_ACTION_IN_PROGRESS, - "No verification in progress"); - return TRUE; - default: - g_dbus_method_invocation_return_error_literal ( - invocation, FPRINT_ERROR, - FPRINT_ERROR_ALREADY_IN_USE, - "Another operation is already in progress"); - return TRUE; - } + if (!_fprint_device_check_claimed (rdev, invocation, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - if (priv->current_cancellable) { - /* We return only when the action was cancelled */ - g_cancellable_cancel (priv->current_cancellable); - priv->current_cancel_invocation = invocation; - } else { - fprint_dbus_device_complete_verify_stop (dbus_dev, invocation); - priv->current_action = ACTION_NONE; + switch (priv->current_action) + { + case ACTION_VERIFY: + case ACTION_IDENTIFY: + break; - session = session_data_get (priv); - session->verify_status_reported = FALSE; - } + case ACTION_NONE: + g_dbus_method_invocation_return_error_literal ( + invocation, FPRINT_ERROR, + FPRINT_ERROR_NO_ACTION_IN_PROGRESS, + "No verification in progress"); + return TRUE; - return TRUE; + default: + g_dbus_method_invocation_return_error_literal ( + invocation, FPRINT_ERROR, + FPRINT_ERROR_ALREADY_IN_USE, + "Another operation is already in progress"); + return TRUE; + } + + if (priv->current_cancellable) + { + /* We return only when the action was cancelled */ + g_cancellable_cancel (priv->current_cancellable); + priv->current_cancel_invocation = invocation; + } + else + { + fprint_dbus_device_complete_verify_stop (dbus_dev, invocation); + priv->current_action = ACTION_NONE; + + session = session_data_get (priv); + session->verify_status_reported = FALSE; + } + + return TRUE; } -static void enroll_progress_cb(FpDevice *dev, - gint completed_stages, - FpPrint *print, - gpointer user_data, - GError *error) +static void +enroll_progress_cb (FpDevice *dev, + gint completed_stages, + FpPrint *print, + gpointer user_data, + GError *error) { - FprintDevice *rdev = user_data; - const char *name = enroll_result_to_name (FALSE, FALSE, error); + FprintDevice *rdev = user_data; + const char *name = enroll_result_to_name (FALSE, FALSE, error); - g_debug("enroll_stage_cb: result %s", name); + g_debug ("enroll_stage_cb: result %s", name); - if (completed_stages < fp_device_get_nr_enroll_stages (dev)) - g_signal_emit(rdev, signals[SIGNAL_ENROLL_STATUS], 0, name, FALSE); + if (completed_stages < fp_device_get_nr_enroll_stages (dev)) + g_signal_emit (rdev, signals[SIGNAL_ENROLL_STATUS], 0, name, FALSE); } -static gboolean try_delete_print(FprintDevice *rdev) +static gboolean +try_delete_print (FprintDevice *rdev) { - g_autoptr(GError) error = NULL; - g_autoptr(GPtrArray) device_prints = NULL; - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - GSList *users, *user; + g_autoptr(GError) error = NULL; + g_autoptr(GPtrArray) device_prints = NULL; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + GSList *users, *user; - device_prints = fp_device_list_prints_sync (priv->dev, NULL, &error); - if (!device_prints) { - g_warning ("Failed to query prints: %s", error->message); - return FALSE; - } + device_prints = fp_device_list_prints_sync (priv->dev, NULL, &error); + if (!device_prints) + { + g_warning ("Failed to query prints: %s", error->message); + return FALSE; + } - g_debug ("Device has %d prints stored", device_prints->len); + g_debug ("Device has %d prints stored", device_prints->len); - users = store.discover_users(); + users = store.discover_users (); - for (user = users; user; user = user->next) { - const char *username = user->data; - g_autoptr(GSList) fingers = NULL; - GSList *finger; + for (user = users; user; user = user->next) + { + const char *username = user->data; + g_autoptr(GSList) fingers = NULL; + GSList *finger; - fingers = store.discover_prints (priv->dev, username); + fingers = store.discover_prints (priv->dev, username); - for (finger = fingers; finger; finger = finger->next) { - g_autoptr(FpPrint) print = NULL; - guint index; + for (finger = fingers; finger; finger = finger->next) + { + g_autoptr(FpPrint) print = NULL; + guint index; - store.print_data_load (priv->dev, - GPOINTER_TO_UINT (fingers->data), - username, - &print); + store.print_data_load (priv->dev, + GPOINTER_TO_UINT (fingers->data), + username, + &print); - if (!print) - continue; + if (!print) + continue; - if (!g_ptr_array_find_with_equal_func (device_prints, - print, - (GEqualFunc) fp_print_equal, - &index)) - continue; + if (!g_ptr_array_find_with_equal_func (device_prints, + print, + (GEqualFunc) fp_print_equal, + &index)) + continue; - /* Found an equal print, remove it */ - g_ptr_array_remove_index (device_prints, index); - } - } + /* Found an equal print, remove it */ + g_ptr_array_remove_index (device_prints, index); + } + } - g_slist_free_full (users, g_free); + g_slist_free_full (users, g_free); - g_debug ("Device has %d prints stored that we do not need", device_prints->len); - if (device_prints->len == 0) - return FALSE; + g_debug ("Device has %d prints stored that we do not need", device_prints->len); + if (device_prints->len == 0) + return FALSE; - /* Just delete the first print in the list at this point. - * We could be smarter and fetch some more metadata. */ - fp_device_delete_print_sync (priv->dev, - g_ptr_array_index (device_prints, 0), - NULL, - &error); + /* Just delete the first print in the list at this point. + * We could be smarter and fetch some more metadata. */ + fp_device_delete_print_sync (priv->dev, + g_ptr_array_index (device_prints, 0), + NULL, + &error); - if (error) { - g_warning ("Failed to garbage collect a print: %s", error->message); - return FALSE; - } + if (error) + { + g_warning ("Failed to garbage collect a print: %s", error->message); + return FALSE; + } - return TRUE; + return TRUE; } #if !GLIB_CHECK_VERSION (2, 63, 3) G_DEFINE_AUTOPTR_CLEANUP_FUNC (GDate, g_date_free); #endif -static FpPrint* -fprint_device_create_enroll_template(FprintDevice *rdev, FpFinger finger) +static FpPrint * +fprint_device_create_enroll_template (FprintDevice *rdev, FpFinger finger) { - g_autoptr(SessionData) session = NULL; - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr(GDateTime) datetime = NULL; - g_autoptr(GDate) date = NULL; - FpPrint *template = NULL; - gint year, month, day; + g_autoptr(SessionData) session = NULL; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - session = session_data_get (priv); + g_autoptr(GDateTime) datetime = NULL; + g_autoptr(GDate) date = NULL; + FpPrint *template = NULL; + gint year, month, day; - template = fp_print_new (priv->dev); - fp_print_set_finger (template, finger); - fp_print_set_username (template, session->username); - datetime = g_date_time_new_now_local (); - g_date_time_get_ymd (datetime, &year, &month, &day); - date = g_date_new_dmy (day, month, year); - fp_print_set_enroll_date (template, date); + session = session_data_get (priv); - return template; + template = fp_print_new (priv->dev); + fp_print_set_finger (template, finger); + fp_print_set_username (template, session->username); + datetime = g_date_time_new_now_local (); + g_date_time_get_ymd (datetime, &year, &month, &day); + date = g_date_new_dmy (day, month, year); + fp_print_set_enroll_date (template, date); + + return template; } -static void enroll_cb(FpDevice *dev, GAsyncResult *res, void *user_data) +static void +enroll_cb (FpDevice *dev, GAsyncResult *res, void *user_data) { - g_autoptr(GError) error = NULL; - FprintDevice *rdev = user_data; - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); - g_autoptr(FpPrint) print = NULL; - const char *name; + g_autoptr(GError) error = NULL; + FprintDevice *rdev = user_data; + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev); - print = fp_device_enroll_finish (dev, res, &error); + g_autoptr(FpPrint) print = NULL; + const char *name; - /* We need to special case the issue where the on device storage - * is completely full. In that case, we check whether we can delete - * a print that is not coming from us; assuming it is from an old - * installation. - * We do this synchronously, which is not great but should be good - * enough. */ - if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_FULL)) { - g_debug ("Device storage is full, trying to garbage collect old prints"); - if (try_delete_print (rdev)) { - /* Success? Then restart the operation */ - fp_device_enroll (priv->dev, - fprint_device_create_enroll_template (rdev, priv->enroll_data), - priv->current_cancellable, - enroll_progress_cb, - rdev, - NULL, - (GAsyncReadyCallback) enroll_cb, - rdev); - return; - } - } + print = fp_device_enroll_finish (dev, res, &error); - name = enroll_result_to_name (TRUE, print != NULL, error); + /* We need to special case the issue where the on device storage + * is completely full. In that case, we check whether we can delete + * a print that is not coming from us; assuming it is from an old + * installation. + * We do this synchronously, which is not great but should be good + * enough. */ + if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_FULL)) + { + g_debug ("Device storage is full, trying to garbage collect old prints"); + if (try_delete_print (rdev)) + { + /* Success? Then restart the operation */ + fp_device_enroll (priv->dev, + fprint_device_create_enroll_template (rdev, priv->enroll_data), + priv->current_cancellable, + enroll_progress_cb, + rdev, + NULL, + (GAsyncReadyCallback) enroll_cb, + rdev); + return; + } + } - g_debug ("enroll_cb: result %s", name); + name = enroll_result_to_name (TRUE, print != NULL, error); - if (print) { - int r; - r = store.print_data_save(print); - if (r < 0) - name = "enroll-failed"; - } + g_debug ("enroll_cb: result %s", name); - g_signal_emit(rdev, signals[SIGNAL_ENROLL_STATUS], 0, name, TRUE); + if (print) + { + int r; + r = store.print_data_save (print); + if (r < 0) + name = "enroll-failed"; + } - if (error && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning ("Device reported an error during enroll: %s", error->message); + g_signal_emit (rdev, signals[SIGNAL_ENROLL_STATUS], 0, name, TRUE); - /* Return the cancellation or reset action right away if vanished. */ - if (priv->current_cancel_invocation) { - fprint_dbus_device_complete_enroll_stop (dbus_dev, - g_steal_pointer (&priv->current_cancel_invocation)); - priv->current_action = ACTION_NONE; - } else if (g_cancellable_is_cancelled (priv->current_cancellable)) { - priv->current_action = ACTION_NONE; - } - g_clear_object (&priv->current_cancellable); + if (error && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning ("Device reported an error during enroll: %s", error->message); + + /* Return the cancellation or reset action right away if vanished. */ + if (priv->current_cancel_invocation) + { + fprint_dbus_device_complete_enroll_stop (dbus_dev, + g_steal_pointer (&priv->current_cancel_invocation)); + priv->current_action = ACTION_NONE; + } + else if (g_cancellable_is_cancelled (priv->current_cancellable)) + { + priv->current_action = ACTION_NONE; + } + g_clear_object (&priv->current_cancellable); } -static gboolean fprint_device_enroll_start (FprintDBusDevice *dbus_dev, - GDBusMethodInvocation *invocation, - const char *finger_name) +static gboolean +fprint_device_enroll_start (FprintDBusDevice *dbus_dev, + GDBusMethodInvocation *invocation, + const char *finger_name) { - g_autoptr(GError) error = NULL; - FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - FpFinger finger = finger_name_to_fp_finger (finger_name); + g_autoptr(GError) error = NULL; + FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + FpFinger finger = finger_name_to_fp_finger (finger_name); - if (!_fprint_device_check_claimed (rdev, invocation, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } + if (!_fprint_device_check_claimed (rdev, invocation, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - if (finger == FP_FINGER_UNKNOWN) { - g_set_error(&error, FPRINT_ERROR, FPRINT_ERROR_INVALID_FINGERNAME, - "Invalid finger name"); - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } + if (finger == FP_FINGER_UNKNOWN) + { + g_set_error (&error, FPRINT_ERROR, FPRINT_ERROR_INVALID_FINGERNAME, + "Invalid finger name"); + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - if (!can_start_action (rdev, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } + if (!can_start_action (rdev, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - g_debug("start enrollment device %d finger %d", priv->id, finger); + g_debug ("start enrollment device %d finger %d", priv->id, finger); - priv->current_cancellable = g_cancellable_new (); - priv->enroll_data = finger; - fp_device_enroll (priv->dev, - fprint_device_create_enroll_template (rdev, priv->enroll_data), - priv->current_cancellable, - enroll_progress_cb, - rdev, - NULL, - (GAsyncReadyCallback) enroll_cb, - rdev); + priv->current_cancellable = g_cancellable_new (); + priv->enroll_data = finger; + fp_device_enroll (priv->dev, + fprint_device_create_enroll_template (rdev, priv->enroll_data), + priv->current_cancellable, + enroll_progress_cb, + rdev, + NULL, + (GAsyncReadyCallback) enroll_cb, + rdev); - priv->current_action = ACTION_ENROLL; + priv->current_action = ACTION_ENROLL; - fprint_dbus_device_complete_enroll_start (dbus_dev, invocation); + fprint_dbus_device_complete_enroll_start (dbus_dev, invocation); - return TRUE; -} - -static gboolean fprint_device_enroll_stop (FprintDBusDevice *dbus_dev, - GDBusMethodInvocation *invocation) -{ - FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr(GError) error = NULL; - - if (!_fprint_device_check_claimed (rdev, invocation, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } - - switch (priv->current_action) { - case ACTION_ENROLL: - break; - case ACTION_NONE: - g_dbus_method_invocation_return_error_literal ( - invocation, FPRINT_ERROR, - FPRINT_ERROR_NO_ACTION_IN_PROGRESS, - "No enrollment in progress"); - return TRUE; - default: - g_dbus_method_invocation_return_error_literal ( - invocation, FPRINT_ERROR, - FPRINT_ERROR_ALREADY_IN_USE, - "Another operation is already in progress"); - return TRUE; - } - - if (priv->current_cancellable) { - /* We return only when the action was cancelled */ - g_cancellable_cancel (priv->current_cancellable); - priv->current_cancel_invocation = invocation; - } else { - fprint_dbus_device_complete_enroll_stop (dbus_dev, invocation); - priv->current_action = ACTION_NONE; - } - - return TRUE; -} -static gboolean fprint_device_list_enrolled_fingers (FprintDBusDevice *dbus_dev, - GDBusMethodInvocation *invocation, - const char *username) -{ - FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr (GPtrArray) ret = NULL; - g_autoptr(GSList) prints = NULL; - GSList *item; - const char *sender; - const char *user; - - sender = g_dbus_method_invocation_get_sender (invocation); - _fprint_device_add_client (rdev, sender); - - user = g_object_get_qdata (G_OBJECT (invocation), quark_auth_user); - g_assert (user); - prints = store.discover_prints(priv->dev, user); - - if (!prints) { - g_dbus_method_invocation_return_error_literal (invocation, - FPRINT_ERROR, - FPRINT_ERROR_NO_ENROLLED_PRINTS, - "Failed to discover prints"); - return TRUE; - } - - ret = g_ptr_array_new (); - for (item = prints; item; item = item->next) { - FpFinger finger = GPOINTER_TO_UINT (item->data); - g_ptr_array_add (ret, (char *) fp_finger_to_name (finger)); - } - g_ptr_array_add (ret, NULL); - - fprint_dbus_device_complete_list_enrolled_fingers (dbus_dev, - invocation, (const gchar *const *) ret->pdata); - - return TRUE; -} - -static void delete_enrolled_fingers(FprintDevice *rdev, const char *user) -{ - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - guint i; - - g_debug ("Deleting enrolled fingers for user %s", user); - - /* First try deleting the print from the device, we don't consider it - * fatal if this does not work. */ - if (fp_device_has_storage (priv->dev)) { - g_autoptr(GSList) prints = NULL; - GSList *l; - - prints = store.discover_prints(priv->dev, user); - - for (l = prints; l != NULL; l = l->next) { - g_autoptr(FpPrint) print = NULL; - - store.print_data_load(priv->dev, - GPOINTER_TO_UINT (l->data), - user, - &print); - - if (print) { - g_autoptr(GError) error = NULL; - - if (!fp_device_delete_print_sync (priv->dev, print, NULL, &error)) { - g_warning ("Error deleting print from device: %s", error->message); - g_warning ("This might indicate an issue in the libfprint driver or in the fingerprint device."); - } - } - } - } - - for (i = FP_FINGER_FIRST; i <= FP_FINGER_LAST; i++) { - store.print_data_delete(priv->dev, i, user); - } -} - -#ifdef __linux__ -static void log_offending_client_cb (GObject *object, - GAsyncResult *res, - gpointer user_data) -{ - GDBusConnection *connection = G_DBUS_CONNECTION (object); - g_autoptr(GVariant) ret = NULL; - g_autofree char *path = NULL; - g_autofree char *content = NULL; - guint pid = 0; - - ret = g_dbus_connection_call_finish (connection, res, NULL); - - if (!ret) - return; - - g_variant_get (ret, "(u)", &pid); - path = g_strdup_printf ("/proc/%u/comm", pid); - if (g_file_get_contents (path, &content, NULL, NULL)) { - g_strchomp (content); - g_warning ("Offending API user is %s", content); - } -} - -static void log_offending_client (GDBusMethodInvocation *invocation) -{ - const char *sender; - GDBusConnection *connection; - - connection = g_dbus_method_invocation_get_connection (invocation); - sender = g_dbus_method_invocation_get_sender (invocation); - - g_dbus_connection_call (connection, - "org.freedesktop.DBus", - "/org/freedesktop/DBus", - "org.freedesktop.DBus", - "GetConnectionUnixProcessID", - g_variant_new ("(s)", sender), - NULL, G_DBUS_CALL_FLAGS_NONE, - -1, NULL, log_offending_client_cb, NULL); -} -#endif - -static gboolean fprint_device_delete_enrolled_fingers (FprintDBusDevice *dbus_dev, - GDBusMethodInvocation *invocation, - const char *username) -{ - FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr(GError) error = NULL; - g_autofree char *user = NULL; - const char *sender; - gboolean opened; - - g_warning ("The API user should be updated to use DeleteEnrolledFingers2 method!"); -#ifdef __linux__ - log_offending_client (invocation); -#endif - - if (!can_start_action(rdev, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } - - priv->current_action = ACTION_DELETE; - - if (!_fprint_device_check_claimed (rdev, invocation, &error)) { - /* Return error for anything but FPRINT_ERROR_CLAIM_DEVICE */ - if (!g_error_matches (error, FPRINT_ERROR, FPRINT_ERROR_CLAIM_DEVICE)) { - g_dbus_method_invocation_return_gerror (invocation, - error); - return TRUE; - } - - opened = FALSE; - } else { - opened = TRUE; - } - - sender = g_dbus_method_invocation_get_sender (invocation); - _fprint_device_add_client (rdev, sender); - - if (!opened && fp_device_has_storage (priv->dev)) - fp_device_open_sync (priv->dev, NULL, NULL); - - user = g_object_steal_qdata (G_OBJECT (invocation), quark_auth_user); - g_assert (user); - g_assert (g_str_equal (username, "") || g_str_equal (user, username)); - - delete_enrolled_fingers (rdev, user); - - if (!opened && fp_device_has_storage (priv->dev)) - fp_device_close_sync (priv->dev, NULL, NULL); - - priv->current_action = ACTION_NONE; - - fprint_dbus_device_complete_delete_enrolled_fingers (dbus_dev, - invocation); - return TRUE; -} - -static gboolean fprint_device_delete_enrolled_fingers2 (FprintDBusDevice *dbus_dev, - GDBusMethodInvocation *invocation) -{ - FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private(rdev); - g_autoptr(SessionData) session = NULL; - g_autoptr(GError) error = NULL; - - if (!_fprint_device_check_claimed (rdev, invocation, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } - - if (!can_start_action(rdev, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } - - priv->current_action = ACTION_DELETE; - - session = session_data_get (priv); - - delete_enrolled_fingers (rdev, session->username); - - priv->current_action = ACTION_NONE; - - fprint_dbus_device_complete_delete_enrolled_fingers2 (dbus_dev, - invocation); - return TRUE; + return TRUE; } static gboolean -handle_unauthorized_access (FprintDevice *rdev, - GDBusMethodInvocation *invocation, - GError *error) +fprint_device_enroll_stop (FprintDBusDevice *dbus_dev, + GDBusMethodInvocation *invocation) { - FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - g_assert (error); + g_autoptr(GError) error = NULL; - g_warning ("Client %s not authorized for device %s: %s", - g_dbus_method_invocation_get_sender (invocation), - fp_device_get_name (priv->dev), - error->message); - g_dbus_method_invocation_return_gerror (invocation, error); + if (!_fprint_device_check_claimed (rdev, invocation, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - return FALSE; + switch (priv->current_action) + { + case ACTION_ENROLL: + break; + + case ACTION_NONE: + g_dbus_method_invocation_return_error_literal ( + invocation, FPRINT_ERROR, + FPRINT_ERROR_NO_ACTION_IN_PROGRESS, + "No enrollment in progress"); + return TRUE; + + default: + g_dbus_method_invocation_return_error_literal ( + invocation, FPRINT_ERROR, + FPRINT_ERROR_ALREADY_IN_USE, + "Another operation is already in progress"); + return TRUE; + } + + if (priv->current_cancellable) + { + /* We return only when the action was cancelled */ + g_cancellable_cancel (priv->current_cancellable); + priv->current_cancel_invocation = invocation; + } + else + { + fprint_dbus_device_complete_enroll_stop (dbus_dev, invocation); + priv->current_action = ACTION_NONE; + } + + return TRUE; +} +static gboolean +fprint_device_list_enrolled_fingers (FprintDBusDevice *dbus_dev, + GDBusMethodInvocation *invocation, + const char *username) +{ + FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + + g_autoptr(GPtrArray) ret = NULL; + g_autoptr(GSList) prints = NULL; + GSList *item; + const char *sender; + const char *user; + + sender = g_dbus_method_invocation_get_sender (invocation); + _fprint_device_add_client (rdev, sender); + + user = g_object_get_qdata (G_OBJECT (invocation), quark_auth_user); + g_assert (user); + prints = store.discover_prints (priv->dev, user); + + if (!prints) + { + g_dbus_method_invocation_return_error_literal (invocation, + FPRINT_ERROR, + FPRINT_ERROR_NO_ENROLLED_PRINTS, + "Failed to discover prints"); + return TRUE; + } + + ret = g_ptr_array_new (); + for (item = prints; item; item = item->next) + { + FpFinger finger = GPOINTER_TO_UINT (item->data); + g_ptr_array_add (ret, (char *) fp_finger_to_name (finger)); + } + g_ptr_array_add (ret, NULL); + + fprint_dbus_device_complete_list_enrolled_fingers (dbus_dev, + invocation, (const gchar *const *) ret->pdata); + + return TRUE; +} + +static void +delete_enrolled_fingers (FprintDevice *rdev, const char *user) +{ + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + guint i; + + g_debug ("Deleting enrolled fingers for user %s", user); + + /* First try deleting the print from the device, we don't consider it + * fatal if this does not work. */ + if (fp_device_has_storage (priv->dev)) + { + g_autoptr(GSList) prints = NULL; + GSList *l; + + prints = store.discover_prints (priv->dev, user); + + for (l = prints; l != NULL; l = l->next) + { + g_autoptr(FpPrint) print = NULL; + + store.print_data_load (priv->dev, + GPOINTER_TO_UINT (l->data), + user, + &print); + + if (print) + { + g_autoptr(GError) error = NULL; + + if (!fp_device_delete_print_sync (priv->dev, print, NULL, &error)) + { + g_warning ("Error deleting print from device: %s", error->message); + g_warning ("This might indicate an issue in the libfprint driver or in the fingerprint device."); + } + } + } + } + + for (i = FP_FINGER_FIRST; i <= FP_FINGER_LAST; i++) + store.print_data_delete (priv->dev, i, user); +} + +#ifdef __linux__ +static void +log_offending_client_cb (GObject *object, + GAsyncResult *res, + gpointer user_data) +{ + GDBusConnection *connection = G_DBUS_CONNECTION (object); + + g_autoptr(GVariant) ret = NULL; + g_autofree char *path = NULL; + g_autofree char *content = NULL; + guint pid = 0; + + ret = g_dbus_connection_call_finish (connection, res, NULL); + + if (!ret) + return; + + g_variant_get (ret, "(u)", &pid); + path = g_strdup_printf ("/proc/%u/comm", pid); + if (g_file_get_contents (path, &content, NULL, NULL)) + { + g_strchomp (content); + g_warning ("Offending API user is %s", content); + } +} + +static void +log_offending_client (GDBusMethodInvocation *invocation) +{ + const char *sender; + GDBusConnection *connection; + + connection = g_dbus_method_invocation_get_connection (invocation); + sender = g_dbus_method_invocation_get_sender (invocation); + + g_dbus_connection_call (connection, + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus", + "GetConnectionUnixProcessID", + g_variant_new ("(s)", sender), + NULL, G_DBUS_CALL_FLAGS_NONE, + -1, NULL, log_offending_client_cb, NULL); +} +#endif + +static gboolean +fprint_device_delete_enrolled_fingers (FprintDBusDevice *dbus_dev, + GDBusMethodInvocation *invocation, + const char *username) +{ + FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + + g_autoptr(GError) error = NULL; + g_autofree char *user = NULL; + const char *sender; + gboolean opened; + + g_warning ("The API user should be updated to use DeleteEnrolledFingers2 method!"); +#ifdef __linux__ + log_offending_client (invocation); +#endif + + if (!can_start_action (rdev, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } + + priv->current_action = ACTION_DELETE; + + if (!_fprint_device_check_claimed (rdev, invocation, &error)) + { + /* Return error for anything but FPRINT_ERROR_CLAIM_DEVICE */ + if (!g_error_matches (error, FPRINT_ERROR, FPRINT_ERROR_CLAIM_DEVICE)) + { + g_dbus_method_invocation_return_gerror (invocation, + error); + return TRUE; + } + + opened = FALSE; + } + else + { + opened = TRUE; + } + + sender = g_dbus_method_invocation_get_sender (invocation); + _fprint_device_add_client (rdev, sender); + + if (!opened && fp_device_has_storage (priv->dev)) + fp_device_open_sync (priv->dev, NULL, NULL); + + user = g_object_steal_qdata (G_OBJECT (invocation), quark_auth_user); + g_assert (user); + g_assert (g_str_equal (username, "") || g_str_equal (user, username)); + + delete_enrolled_fingers (rdev, user); + + if (!opened && fp_device_has_storage (priv->dev)) + fp_device_close_sync (priv->dev, NULL, NULL); + + priv->current_action = ACTION_NONE; + + fprint_dbus_device_complete_delete_enrolled_fingers (dbus_dev, + invocation); + return TRUE; +} + +static gboolean +fprint_device_delete_enrolled_fingers2 (FprintDBusDevice *dbus_dev, + GDBusMethodInvocation *invocation) +{ + FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + + g_autoptr(SessionData) session = NULL; + g_autoptr(GError) error = NULL; + + if (!_fprint_device_check_claimed (rdev, invocation, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } + + if (!can_start_action (rdev, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } + + priv->current_action = ACTION_DELETE; + + session = session_data_get (priv); + + delete_enrolled_fingers (rdev, session->username); + + priv->current_action = ACTION_NONE; + + fprint_dbus_device_complete_delete_enrolled_fingers2 (dbus_dev, + invocation); + return TRUE; +} + +static gboolean +handle_unauthorized_access (FprintDevice *rdev, + GDBusMethodInvocation *invocation, + GError *error) +{ + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + + g_assert (error); + + g_warning ("Client %s not authorized for device %s: %s", + g_dbus_method_invocation_get_sender (invocation), + fp_device_get_name (priv->dev), + error->message); + g_dbus_method_invocation_return_gerror (invocation, error); + + return FALSE; } static gboolean action_authorization_handler (GDBusInterfaceSkeleton *interface, - GDBusMethodInvocation *invocation, - gpointer user_data) + GDBusMethodInvocation *invocation, + gpointer user_data) { - FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (interface); - FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); - FprintDevicePermission required_perms; - gboolean needs_user_auth = FALSE; - g_autoptr(GError) error = NULL; - const gchar *method_name; + FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (interface); + FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + FprintDevicePermission required_perms; + gboolean needs_user_auth = FALSE; - method_name = g_dbus_method_invocation_get_method_name (invocation); + g_autoptr(GError) error = NULL; + const gchar *method_name; - g_debug ("Requesting device '%s' authorization for method %s from %s", - fp_device_get_name (priv->dev), method_name, - g_dbus_method_invocation_get_sender (invocation)); + method_name = g_dbus_method_invocation_get_method_name (invocation); - if (g_str_equal (method_name, "Claim")) { - needs_user_auth = TRUE; - } else if (g_str_equal (method_name, "DeleteEnrolledFingers")) { - needs_user_auth = TRUE; - } else if (g_str_equal (method_name, "ListEnrolledFingers")) { - needs_user_auth = TRUE; - } + g_debug ("Requesting device '%s' authorization for method %s from %s", + fp_device_get_name (priv->dev), method_name, + g_dbus_method_invocation_get_sender (invocation)); - /* This is just a quick check in order to avoid authentication if - * the user cannot make the call at this time anyway. - * The method handler itself is required to check again! */ - if (!_fprint_device_check_claimed (rdev, invocation, &error)) { - return handle_unauthorized_access (rdev, invocation, error); - } + if (g_str_equal (method_name, "Claim")) + needs_user_auth = TRUE; + else if (g_str_equal (method_name, "DeleteEnrolledFingers")) + needs_user_auth = TRUE; + else if (g_str_equal (method_name, "ListEnrolledFingers")) + needs_user_auth = TRUE; - if (needs_user_auth && - !fprintd_device_authorize_user (rdev, invocation, &error)) { - return handle_unauthorized_access (rdev, invocation, error); - } + /* This is just a quick check in order to avoid authentication if + * the user cannot make the call at this time anyway. + * The method handler itself is required to check again! */ + if (!_fprint_device_check_claimed (rdev, invocation, &error)) + return handle_unauthorized_access (rdev, invocation, error); - required_perms = get_permissions_for_invocation (invocation); + if (needs_user_auth && + !fprintd_device_authorize_user (rdev, invocation, &error)) + return handle_unauthorized_access (rdev, invocation, error); - /* This may possibly block the invocation till the user has not - * provided an authentication method, so other calls could arrive */ - if (!fprint_device_check_polkit_for_permissions (rdev, invocation, - required_perms, - &error)) { - return handle_unauthorized_access (rdev, invocation, error); - } + required_perms = get_permissions_for_invocation (invocation); - g_debug ("Authorization granted to %s for device %s!", - fp_device_get_name (priv->dev), - g_dbus_method_invocation_get_sender (invocation)); + /* This may possibly block the invocation till the user has not + * provided an authentication method, so other calls could arrive */ + if (!fprint_device_check_polkit_for_permissions (rdev, invocation, + required_perms, + &error)) + return handle_unauthorized_access (rdev, invocation, error); - return TRUE; + g_debug ("Authorization granted to %s for device %s!", + fp_device_get_name (priv->dev), + g_dbus_method_invocation_get_sender (invocation)); + + return TRUE; } -static void fprint_device_dbus_skeleton_iface_init (FprintDBusDeviceIface *iface) +static void +fprint_device_dbus_skeleton_iface_init (FprintDBusDeviceIface *iface) { - iface->handle_claim = fprint_device_claim; - iface->handle_delete_enrolled_fingers = fprint_device_delete_enrolled_fingers; - iface->handle_delete_enrolled_fingers2 = fprint_device_delete_enrolled_fingers2; - iface->handle_enroll_start = fprint_device_enroll_start; - iface->handle_enroll_stop = fprint_device_enroll_stop; - iface->handle_list_enrolled_fingers = fprint_device_list_enrolled_fingers; - iface->handle_release = fprint_device_release; - iface->handle_verify_start = fprint_device_verify_start; - iface->handle_verify_stop = fprint_device_verify_stop; + iface->handle_claim = fprint_device_claim; + iface->handle_delete_enrolled_fingers = fprint_device_delete_enrolled_fingers; + iface->handle_delete_enrolled_fingers2 = fprint_device_delete_enrolled_fingers2; + iface->handle_enroll_start = fprint_device_enroll_start; + iface->handle_enroll_stop = fprint_device_enroll_stop; + iface->handle_list_enrolled_fingers = fprint_device_list_enrolled_fingers; + iface->handle_release = fprint_device_release; + iface->handle_verify_start = fprint_device_verify_start; + iface->handle_verify_stop = fprint_device_verify_stop; } - diff --git a/src/file_storage.c b/src/file_storage.c index 4a72fca..158f35a 100644 --- a/src/file_storage.c +++ b/src/file_storage.c @@ -7,12 +7,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -44,271 +44,297 @@ static char *storage_path = NULL; -static const char *get_storage_path(void) +static const char * +get_storage_path (void) { - const char *path = NULL; + const char *path = NULL; - if (storage_path != NULL) - return storage_path; + if (storage_path != NULL) + return storage_path; - /* set by systemd >= 240 to an absolute path - * taking into account the StateDirectory - * unit file setting */ - path = g_getenv ("STATE_DIRECTORY"); - if (path != NULL) { - /* If multiple directories are set, then in the environment variable - * the paths are concatenated with colon (":"). */ - if (strchr (path, ':')) { - g_auto(GStrv) elems = NULL; - elems = g_strsplit (path, ":", -1); - storage_path = g_strdup (elems[0]); - } - } + /* set by systemd >= 240 to an absolute path + * taking into account the StateDirectory + * unit file setting */ + path = g_getenv ("STATE_DIRECTORY"); + if (path != NULL) + { + /* If multiple directories are set, then in the environment variable + * the paths are concatenated with colon (":"). */ + if (strchr (path, ':')) + { + g_auto(GStrv) elems = NULL; + elems = g_strsplit (path, ":", -1); + storage_path = g_strdup (elems[0]); + } + } - if (storage_path == NULL) - storage_path = g_strdup (FILE_STORAGE_PATH); + if (storage_path == NULL) + storage_path = g_strdup (FILE_STORAGE_PATH); - return storage_path; + return storage_path; } -static char *get_path_to_storedir(const char *driver, const char * device_id, char *base_store) +static char * +get_path_to_storedir (const char *driver, const char * device_id, char *base_store) { - return g_build_filename(base_store, driver, device_id, NULL); + return g_build_filename (base_store, driver, device_id, NULL); } -static char *__get_path_to_print(const char *driver, const char * device_id, - FpFinger finger, char *base_store) +static char * +__get_path_to_print (const char *driver, const char * device_id, + FpFinger finger, char *base_store) { - g_autofree char *dirpath = NULL; - char *path; - char fingername[2]; + g_autofree char *dirpath = NULL; + char *path; + char fingername[2]; - g_snprintf(fingername, 2, "%x", finger); + g_snprintf (fingername, 2, "%x", finger); - dirpath = get_path_to_storedir(driver, device_id, base_store); - path = g_build_filename(dirpath, fingername, NULL); - return path; + dirpath = get_path_to_storedir (driver, device_id, base_store); + path = g_build_filename (dirpath, fingername, NULL); + return path; } -static char *get_path_to_print(FpDevice *dev, FpFinger finger, char *base_store) +static char * +get_path_to_print (FpDevice *dev, FpFinger finger, char *base_store) { - return __get_path_to_print(fp_device_get_driver (dev), - fp_device_get_device_id(dev), - finger, - base_store); + return __get_path_to_print (fp_device_get_driver (dev), + fp_device_get_device_id (dev), + finger, + base_store); } -static char *get_path_to_print_dscv(FpDevice *dev, FpFinger finger, char *base_store) +static char * +get_path_to_print_dscv (FpDevice *dev, FpFinger finger, char *base_store) { - return __get_path_to_print(fp_device_get_driver (dev), - fp_device_get_device_id(dev), - finger, - base_store); + return __get_path_to_print (fp_device_get_driver (dev), + fp_device_get_device_id (dev), + finger, + base_store); } -static char *file_storage_get_basestore_for_username(const char *username) +static char * +file_storage_get_basestore_for_username (const char *username) { - return g_build_filename(get_storage_path(), username, NULL); + return g_build_filename (get_storage_path (), username, NULL); } -int file_storage_print_data_save(FpPrint *print) +int +file_storage_print_data_save (FpPrint *print) { - g_autoptr(GError) err = NULL; - g_autofree char *path = NULL; - g_autofree char *dirpath = NULL; - g_autofree char *base_store = NULL; - g_autofree char *buf = NULL; - gsize len; - int r; + g_autoptr(GError) err = NULL; + g_autofree char *path = NULL; + g_autofree char *dirpath = NULL; + g_autofree char *base_store = NULL; + g_autofree char *buf = NULL; + gsize len; + int r; - base_store = file_storage_get_basestore_for_username(fp_print_get_username (print)); + base_store = file_storage_get_basestore_for_username (fp_print_get_username (print)); - if (!fp_print_serialize (print, (guchar **) &buf, &len, &err)) { - g_warning ("Error serializing data: %s", err->message); - return -ENOMEM; - } + if (!fp_print_serialize (print, (guchar **) &buf, &len, &err)) + { + g_warning ("Error serializing data: %s", err->message); + return -ENOMEM; + } - path = __get_path_to_print(fp_print_get_driver (print), - fp_print_get_device_id (print), - fp_print_get_finger (print), - base_store); - dirpath = g_path_get_dirname(path); - r = g_mkdir_with_parents(dirpath, DIR_PERMS); - if (r < 0) { - g_debug("file_storage_print_data_save(): could not mkdir(\"%s\"): %s", - dirpath, g_strerror(r)); - return r; - } + path = __get_path_to_print (fp_print_get_driver (print), + fp_print_get_device_id (print), + fp_print_get_finger (print), + base_store); + dirpath = g_path_get_dirname (path); + r = g_mkdir_with_parents (dirpath, DIR_PERMS); + if (r < 0) + { + g_debug ("file_storage_print_data_save(): could not mkdir(\"%s\"): %s", + dirpath, g_strerror (r)); + return r; + } - //fp_dbg("saving to %s", path); - g_file_set_contents(path, buf, len, &err); - if (err) { - g_debug("file_storage_print_data_save(): could not save '%s': %s", - path, err->message); - /* FIXME interpret error codes */ - return err->code; - } + //fp_dbg("saving to %s", path); + g_file_set_contents (path, buf, len, &err); + if (err) + { + g_debug ("file_storage_print_data_save(): could not save '%s': %s", + path, err->message); + /* FIXME interpret error codes */ + return err->code; + } - return 0; + return 0; } -static int load_from_file(char *path, FpPrint **print) +static int +load_from_file (char *path, FpPrint **print) { - g_autoptr(GError) err = NULL; - gsize length; - g_autofree char *contents = NULL; - FpPrint *new; + g_autoptr(GError) err = NULL; + gsize length; + g_autofree char *contents = NULL; + FpPrint *new; - //fp_dbg("from %s", path); - g_file_get_contents(path, &contents, &length, &err); - if (err) { - int r = err->code; - /* FIXME interpret more error codes */ - if (r == G_FILE_ERROR_NOENT) - return -ENOENT; - else - return r; - } + //fp_dbg("from %s", path); + g_file_get_contents (path, &contents, &length, &err); + if (err) + { + int r = err->code; + /* FIXME interpret more error codes */ + if (r == G_FILE_ERROR_NOENT) + return -ENOENT; + else + return r; + } - new = fp_print_deserialize ((guchar *) contents, length, &err); - if (!new) { - g_print ("Error deserializing data: %s", err->message); - return -EIO; - } + new = fp_print_deserialize ((guchar *) contents, length, &err); + if (!new) + { + g_print ("Error deserializing data: %s", err->message); + return -EIO; + } - *print = new; - return 0; + *print = new; + return 0; } -int file_storage_print_data_load(FpDevice *dev, - FpFinger finger, - const char *username, - FpPrint **print) +int +file_storage_print_data_load (FpDevice *dev, + FpFinger finger, + const char *username, + FpPrint **print) { - g_autofree gchar *path = NULL; - g_autofree gchar *base_store = NULL; - g_autoptr(FpPrint) new = NULL; - int r; + g_autofree gchar *path = NULL; + g_autofree gchar *base_store = NULL; - base_store = file_storage_get_basestore_for_username(username); + g_autoptr(FpPrint) new = NULL; + int r; - path = get_path_to_print(dev, finger, base_store); - r = load_from_file(path, &new); - g_debug ("file_storage_print_data_load(): loaded '%s' %s", - path, g_strerror(r)); - if (r) - return r; + base_store = file_storage_get_basestore_for_username (username); - if (!fp_print_compatible (new, dev)) { - return -EINVAL; - } + path = get_path_to_print (dev, finger, base_store); + r = load_from_file (path, &new); + g_debug ("file_storage_print_data_load(): loaded '%s' %s", + path, g_strerror (r)); + if (r) + return r; - *print = g_steal_pointer (&new); - return 0; + if (!fp_print_compatible (new, dev)) + return -EINVAL; + + *print = g_steal_pointer (&new); + return 0; } -int file_storage_print_data_delete(FpDevice *dev, FpFinger finger, const char *username) +int +file_storage_print_data_delete (FpDevice *dev, FpFinger finger, const char *username) { - g_autofree gchar *base_store = NULL; - g_autofree gchar *path = NULL; - int r; + g_autofree gchar *base_store = NULL; + g_autofree gchar *path = NULL; + int r; - base_store = file_storage_get_basestore_for_username(username); + base_store = file_storage_get_basestore_for_username (username); - path = get_path_to_print_dscv(dev, finger, base_store); + path = get_path_to_print_dscv (dev, finger, base_store); - r = g_unlink(path); - g_debug("file_storage_print_data_delete(): unlink(\"%s\") %s", - path, g_strerror(r)); + r = g_unlink (path); + g_debug ("file_storage_print_data_delete(): unlink(\"%s\") %s", + path, g_strerror (r)); - /* FIXME: cleanup empty directory */ - return g_unlink(path); + /* FIXME: cleanup empty directory */ + return g_unlink (path); } -static GSList *scan_dev_storedir(char *devpath, - GSList *list) +static GSList * +scan_dev_storedir (char *devpath, + GSList *list) { - g_autoptr(GError) err = NULL; - const gchar *ent; + g_autoptr(GError) err = NULL; + const gchar *ent; - GDir *dir = g_dir_open(devpath, 0, &err); - if (!dir) { - g_debug("scan_dev_storedir(): opendir(\"%s\") failed: %s", devpath, err->message); - return list; - } + GDir *dir = g_dir_open (devpath, 0, &err); - while ((ent = g_dir_read_name(dir))) { - /* ent is an 1 hex character fp_finger code */ - guint64 val; - gchar *endptr; + if (!dir) + { + g_debug ("scan_dev_storedir(): opendir(\"%s\") failed: %s", devpath, err->message); + return list; + } - if (*ent == 0 || strlen(ent) != 1) - continue; + while ((ent = g_dir_read_name (dir))) + { + /* ent is an 1 hex character fp_finger code */ + guint64 val; + gchar *endptr; - val = g_ascii_strtoull(ent, &endptr, 16); - if (endptr == ent || !FP_FINGER_IS_VALID(val)) { - g_debug("scan_dev_storedir(): skipping print file '%s'", ent); - continue; - } + if (*ent == 0 || strlen (ent) != 1) + continue; - list = g_slist_prepend (list, GUINT_TO_POINTER (val)); - } + val = g_ascii_strtoull (ent, &endptr, 16); + if (endptr == ent || !FP_FINGER_IS_VALID (val)) + { + g_debug ("scan_dev_storedir(): skipping print file '%s'", ent); + continue; + } - g_dir_close(dir); - return list; + list = g_slist_prepend (list, GUINT_TO_POINTER (val)); + } + + g_dir_close (dir); + return list; } -GSList *file_storage_discover_prints(FpDevice *dev, const char *username) +GSList * +file_storage_discover_prints (FpDevice *dev, const char *username) { - GSList *list = NULL; - g_autofree gchar *base_store = NULL; - g_autofree gchar *storedir = NULL; + GSList *list = NULL; + g_autofree gchar *base_store = NULL; + g_autofree gchar *storedir = NULL; - base_store = file_storage_get_basestore_for_username(username); + base_store = file_storage_get_basestore_for_username (username); - storedir = get_path_to_storedir(fp_device_get_driver (dev), - fp_device_get_device_id (dev), - base_store); + storedir = get_path_to_storedir (fp_device_get_driver (dev), + fp_device_get_device_id (dev), + base_store); - g_debug ("file_storage_discover_prints() for user '%s' in '%s'", - username, storedir); + g_debug ("file_storage_discover_prints() for user '%s' in '%s'", + username, storedir); - list = scan_dev_storedir(storedir, list); + list = scan_dev_storedir (storedir, list); - return list; + return list; } -GSList *file_storage_discover_users(void) +GSList * +file_storage_discover_users (void) { - g_autoptr(GError) err = NULL; - GSList *list = NULL; - const gchar *ent; - GDir *dir = g_dir_open(get_storage_path(), 0, &err); + g_autoptr(GError) err = NULL; + GSList *list = NULL; + const gchar *ent; + GDir *dir = g_dir_open (get_storage_path (), 0, &err); - if (!dir) { - return list; - } + if (!dir) + return list; - while ((ent = g_dir_read_name(dir))) { - /* ent is a username */ - if (*ent == 0) - continue; + while ((ent = g_dir_read_name (dir))) + { + /* ent is a username */ + if (*ent == 0) + continue; - list = g_slist_prepend(list, g_strdup (ent)); - } + list = g_slist_prepend (list, g_strdup (ent)); + } - g_dir_close(dir); - return list; + g_dir_close (dir); + return list; } -int file_storage_init(void) +int +file_storage_init (void) { - /* Nothing to do */ - return 0; + /* Nothing to do */ + return 0; } -int file_storage_deinit(void) +int +file_storage_deinit (void) { - g_clear_pointer (&storage_path, g_free); - return 0; + g_clear_pointer (&storage_path, g_free); + return 0; } diff --git a/src/file_storage.h b/src/file_storage.h index 240d0fb..29e70df 100644 --- a/src/file_storage.h +++ b/src/file_storage.h @@ -6,12 +6,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -20,20 +20,21 @@ #pragma once -int file_storage_print_data_save(FpPrint *print); +int file_storage_print_data_save (FpPrint *print); -int file_storage_print_data_load(FpDevice *dev, - FpFinger finger, - const char *username, - FpPrint **print); +int file_storage_print_data_load (FpDevice *dev, + FpFinger finger, + const char *username, + FpPrint **print); -int file_storage_print_data_delete(FpDevice *dev, - FpFinger finger, - const char *username); +int file_storage_print_data_delete (FpDevice *dev, + FpFinger finger, + const char *username); -int file_storage_init(void); +int file_storage_init (void); -int file_storage_deinit(void); +int file_storage_deinit (void); -GSList *file_storage_discover_prints(FpDevice *dev, const char *username); -GSList *file_storage_discover_users(void); +GSList *file_storage_discover_prints (FpDevice *dev, + const char *username); +GSList *file_storage_discover_users (void); diff --git a/src/fprintd.h b/src/fprintd.h index fd8b9b1..4075bc2 100644 --- a/src/fprintd.h +++ b/src/fprintd.h @@ -6,12 +6,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -31,60 +31,63 @@ #define FPRINT_SERVICE_PATH "/net/reactivated/Fprint" /* Errors */ -GQuark fprint_error_quark(void); +GQuark fprint_error_quark (void); -#define FPRINT_ERROR fprint_error_quark() +#define FPRINT_ERROR fprint_error_quark () typedef enum { - /* developer didn't claim the device */ - FPRINT_ERROR_CLAIM_DEVICE, /*< nick=net.reactivated.Fprint.Error.ClaimDevice >*/ - /* device is already claimed by somebody else */ - FPRINT_ERROR_ALREADY_IN_USE, /*< nick=net.reactivated.Fprint.Error.AlreadyInUse >*/ - /* internal error occurred */ - FPRINT_ERROR_INTERNAL, /*< nick=net.reactivated.Fprint.Error.Internal >*/ - /* PolicyKit refused the action */ - FPRINT_ERROR_PERMISSION_DENIED, /*< nick=net.reactivated.Fprint.Error.PermissionDenied >*/ - /* No prints are enrolled */ - FPRINT_ERROR_NO_ENROLLED_PRINTS, /*< nick=net.reactivated.Fprint.Error.NoEnrolledPrints >*/ - /* No actions currently in progress */ - FPRINT_ERROR_NO_ACTION_IN_PROGRESS, /*< nick=net.reactivated.Fprint.Error.NoActionInProgress >*/ - /* the finger name passed was invalid */ - FPRINT_ERROR_INVALID_FINGERNAME, /*< nick=net.reactivated.Fprint.Error.InvalidFingername >*/ - /* device does not exist */ - FPRINT_ERROR_NO_SUCH_DEVICE, /*< nick=net.reactivated.Fprint.Error.NoSuchDevice >*/ + /* developer didn't claim the device */ + FPRINT_ERROR_CLAIM_DEVICE, /*< nick=net.reactivated.Fprint.Error.ClaimDevice >*/ + /* device is already claimed by somebody else */ + FPRINT_ERROR_ALREADY_IN_USE, /*< nick=net.reactivated.Fprint.Error.AlreadyInUse >*/ + /* internal error occurred */ + FPRINT_ERROR_INTERNAL, /*< nick=net.reactivated.Fprint.Error.Internal >*/ + /* PolicyKit refused the action */ + FPRINT_ERROR_PERMISSION_DENIED, /*< nick=net.reactivated.Fprint.Error.PermissionDenied >*/ + /* No prints are enrolled */ + FPRINT_ERROR_NO_ENROLLED_PRINTS, /*< nick=net.reactivated.Fprint.Error.NoEnrolledPrints >*/ + /* No actions currently in progress */ + FPRINT_ERROR_NO_ACTION_IN_PROGRESS, /*< nick=net.reactivated.Fprint.Error.NoActionInProgress >*/ + /* the finger name passed was invalid */ + FPRINT_ERROR_INVALID_FINGERNAME, /*< nick=net.reactivated.Fprint.Error.InvalidFingername >*/ + /* device does not exist */ + FPRINT_ERROR_NO_SUCH_DEVICE, /*< nick=net.reactivated.Fprint.Error.NoSuchDevice >*/ } FprintError; /* Enum of possible permissions, orders and nick matter here: - The order controls the priority of a required permission when various are accepted: the lowest the value, the more priorty it has. - Nick must match the relative polkit rule. -*/ + */ typedef enum { - FPRINT_DEVICE_PERMISSION_NONE = 0, - FPRINT_DEVICE_PERMISSION_VERIFY = (1 << 0), /*< nick=net.reactivated.fprint.device.verify >*/ - FPRINT_DEVICE_PERMISSION_ENROLL = (1 << 1), /*< nick=net.reactivated.fprint.device.enroll >*/ - FPRINT_DEVICE_PERMISSION_SETUSERNAME = (1 << 2), /*< nick=net.reactivated.fprint.device.setusername >*/ + FPRINT_DEVICE_PERMISSION_NONE = 0, + FPRINT_DEVICE_PERMISSION_VERIFY = (1 << 0), /*< nick=net.reactivated.fprint.device.verify >*/ + FPRINT_DEVICE_PERMISSION_ENROLL = (1 << 1), /*< nick=net.reactivated.fprint.device.enroll >*/ + FPRINT_DEVICE_PERMISSION_SETUSERNAME = (1 << 2), /*< nick=net.reactivated.fprint.device.setusername >*/ } FprintDevicePermission; /* Manager */ -#define FPRINT_TYPE_MANAGER (fprint_manager_get_type()) +#define FPRINT_TYPE_MANAGER (fprint_manager_get_type ()) G_DECLARE_FINAL_TYPE (FprintManager, fprint_manager, FPRINT, MANAGER, GObject) -struct _FprintManager { - GObject parent; +struct _FprintManager +{ + GObject parent; }; -FprintManager *fprint_manager_new (GDBusConnection *connection, gboolean no_timeout); +FprintManager *fprint_manager_new (GDBusConnection *connection, + gboolean no_timeout); /* Device */ -#define FPRINT_TYPE_DEVICE (fprint_device_get_type()) +#define FPRINT_TYPE_DEVICE (fprint_device_get_type ()) G_DECLARE_FINAL_TYPE (FprintDevice, fprint_device, FPRINT, DEVICE, - FprintDBusDeviceSkeleton) + FprintDBusDeviceSkeleton) -struct _FprintDevice { - FprintDBusDeviceSkeleton parent; +struct _FprintDevice +{ + FprintDBusDeviceSkeleton parent; }; -FprintDevice *fprint_device_new(FpDevice *dev); -guint32 _fprint_device_get_id(FprintDevice *rdev); +FprintDevice *fprint_device_new (FpDevice *dev); +guint32 _fprint_device_get_id (FprintDevice *rdev); /* Print */ /* TODO */ diff --git a/src/main.c b/src/main.c index f534957..00171a0 100644 --- a/src/main.c +++ b/src/main.c @@ -7,12 +7,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -44,171 +44,179 @@ static gboolean g_fatal_warnings = FALSE; static void set_storage_file (void) { - store.init = &file_storage_init; - store.deinit = &file_storage_deinit; - store.print_data_save = &file_storage_print_data_save; - store.print_data_load = &file_storage_print_data_load; - store.print_data_delete = &file_storage_print_data_delete; - store.discover_prints = &file_storage_discover_prints; - store.discover_users = &file_storage_discover_users; + store.init = &file_storage_init; + store.deinit = &file_storage_deinit; + store.print_data_save = &file_storage_print_data_save; + store.print_data_load = &file_storage_print_data_load; + store.print_data_delete = &file_storage_print_data_delete; + store.discover_prints = &file_storage_discover_prints; + store.discover_users = &file_storage_discover_users; } static gboolean load_storage_module (const char *module_name) { - GModule *module; - g_autofree char *filename = NULL; + GModule *module; + g_autofree char *filename = NULL; - filename = g_module_build_path (PLUGINDIR, module_name); - module = g_module_open (filename, 0); - if (module == NULL) - return FALSE; + filename = g_module_build_path (PLUGINDIR, module_name); + module = g_module_open (filename, 0); + if (module == NULL) + return FALSE; - if (!g_module_symbol (module, "init", (gpointer *) &store.init) || - !g_module_symbol (module, "deinit", (gpointer *) &store.deinit) || - !g_module_symbol (module, "print_data_save", (gpointer *) &store.print_data_save) || - !g_module_symbol (module, "print_data_load", (gpointer *) &store.print_data_load) || - !g_module_symbol (module, "print_data_delete", (gpointer *) &store.print_data_delete) || - !g_module_symbol (module, "discover_prints", (gpointer *) &store.discover_prints)) { - g_module_close (module); - return FALSE; - } + if (!g_module_symbol (module, "init", (gpointer *) &store.init) || + !g_module_symbol (module, "deinit", (gpointer *) &store.deinit) || + !g_module_symbol (module, "print_data_save", (gpointer *) &store.print_data_save) || + !g_module_symbol (module, "print_data_load", (gpointer *) &store.print_data_load) || + !g_module_symbol (module, "print_data_delete", (gpointer *) &store.print_data_delete) || + !g_module_symbol (module, "discover_prints", (gpointer *) &store.discover_prints)) + { + g_module_close (module); + return FALSE; + } - g_module_make_resident (module); + g_module_make_resident (module); - return TRUE; + return TRUE; } static gboolean load_conf (void) { - g_autofree char *filename = NULL; - g_autofree char *module_name = NULL; - g_autoptr(GKeyFile) file = NULL; - g_autoptr(GError) error = NULL; + g_autofree char *filename = NULL; + g_autofree char *module_name = NULL; - filename = g_build_filename (SYSCONFDIR, "fprintd.conf", NULL); - file = g_key_file_new (); - g_debug("About to load configuration file '%s'", filename); - if (!g_key_file_load_from_file (file, filename, G_KEY_FILE_NONE, &error)) { - g_warning ("Could not open \"%s\": %s\n", filename, error->message); - return FALSE; - } + g_autoptr(GKeyFile) file = NULL; + g_autoptr(GError) error = NULL; - module_name = g_key_file_get_string (file, "storage", "type", &error); - if (module_name == NULL) - return FALSE; + filename = g_build_filename (SYSCONFDIR, "fprintd.conf", NULL); + file = g_key_file_new (); + g_debug ("About to load configuration file '%s'", filename); + if (!g_key_file_load_from_file (file, filename, G_KEY_FILE_NONE, &error)) + { + g_warning ("Could not open \"%s\": %s\n", filename, error->message); + return FALSE; + } - if (g_str_equal (module_name, "file")) { - set_storage_file (); - return TRUE; - } + module_name = g_key_file_get_string (file, "storage", "type", &error); + if (module_name == NULL) + return FALSE; - return load_storage_module (module_name); + if (g_str_equal (module_name, "file")) + { + set_storage_file (); + return TRUE; + } + + return load_storage_module (module_name); } static const GOptionEntry entries[] = { - {"g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, "Make all warnings fatal", NULL}, - {"no-timeout", 't', 0, G_OPTION_ARG_NONE, &no_timeout, "Do not exit after unused for a while", NULL}, - { NULL } + {"g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, "Make all warnings fatal", NULL}, + {"no-timeout", 't', 0, G_OPTION_ARG_NONE, &no_timeout, "Do not exit after unused for a while", NULL}, + { NULL } }; -static gboolean sigterm_callback(gpointer data) +static gboolean +sigterm_callback (gpointer data) { - GMainLoop *loop = data; + GMainLoop *loop = data; - g_main_loop_quit (loop); - return FALSE; + g_main_loop_quit (loop); + return FALSE; } static void on_name_acquired (GDBusConnection *connection, - const char *name, - gpointer user_data) + const char *name, + gpointer user_data) { - g_debug ("D-Bus service launched with name: %s", name); + g_debug ("D-Bus service launched with name: %s", name); } static void on_name_lost (GDBusConnection *connection, - const char *name, - gpointer user_data) + const char *name, + gpointer user_data) { - GMainLoop *loop = user_data; + GMainLoop *loop = user_data; - g_warning ("Failed to get name: %s", name); + g_warning ("Failed to get name: %s", name); - g_main_loop_quit (loop); + g_main_loop_quit (loop); } -int main(int argc, char **argv) +int +main (int argc, char **argv) { - g_autoptr(GOptionContext) context = NULL; - g_autoptr(GMainLoop) loop = NULL; - g_autoptr(GError) error = NULL; - g_autoptr(FprintManager) manager = NULL; - g_autoptr(GDBusConnection) connection = NULL; - guint32 request_name_ret; + g_autoptr(GOptionContext) context = NULL; + g_autoptr(GMainLoop) loop = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(FprintManager) manager = NULL; + g_autoptr(GDBusConnection) connection = NULL; + guint32 request_name_ret; - setlocale (LC_ALL, ""); + setlocale (LC_ALL, ""); - bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); - bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); - textdomain (GETTEXT_PACKAGE); + bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); + textdomain (GETTEXT_PACKAGE); - context = g_option_context_new ("Fingerprint handler daemon"); - g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + context = g_option_context_new ("Fingerprint handler daemon"); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); - if (g_option_context_parse (context, &argc, &argv, &error) == FALSE) { - g_warning ("couldn't parse command-line options: %s\n", error->message); - return 1; - } + if (g_option_context_parse (context, &argc, &argv, &error) == FALSE) + { + g_warning ("couldn't parse command-line options: %s\n", error->message); + return 1; + } - if (g_fatal_warnings) { - GLogLevelFlags fatal_mask; + if (g_fatal_warnings) + { + GLogLevelFlags fatal_mask; - fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); - fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; - g_log_set_always_fatal (fatal_mask); - } + fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); + fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; + g_log_set_always_fatal (fatal_mask); + } - /* Obtain a connection to the system bus */ - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); - if (!G_IS_DBUS_CONNECTION (connection)) { - g_warning("Failed to open connection to bus: %s", error->message); - return 1; - } + /* Obtain a connection to the system bus */ + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + if (!G_IS_DBUS_CONNECTION (connection)) + { + g_warning ("Failed to open connection to bus: %s", error->message); + return 1; + } - /* Load the configuration file, - * and the default storage plugin */ - if (!load_conf()) - set_storage_file (); - store.init (); + /* Load the configuration file, + * and the default storage plugin */ + if (!load_conf ()) + set_storage_file (); + store.init (); - loop = g_main_loop_new(NULL, FALSE); - g_unix_signal_add (SIGTERM, sigterm_callback, loop); + loop = g_main_loop_new (NULL, FALSE); + g_unix_signal_add (SIGTERM, sigterm_callback, loop); - g_debug("Launching FprintObject"); + g_debug ("Launching FprintObject"); - /* create the one instance of the Manager object to be shared between - * all fprintd users. This blocks until all the devices are enumerated */ - manager = fprint_manager_new (connection, no_timeout); + /* create the one instance of the Manager object to be shared between + * all fprintd users. This blocks until all the devices are enumerated */ + manager = fprint_manager_new (connection, no_timeout); - /* Obtain the well-known name after the manager has been initialized. - * Otherwise a client immediately enumerating the devices will not see - * any. */ - request_name_ret = g_bus_own_name_on_connection (connection, - FPRINT_SERVICE_NAME, - G_BUS_NAME_OWNER_FLAGS_NONE, - on_name_acquired, - on_name_lost, - loop, NULL); + /* Obtain the well-known name after the manager has been initialized. + * Otherwise a client immediately enumerating the devices will not see + * any. */ + request_name_ret = g_bus_own_name_on_connection (connection, + FPRINT_SERVICE_NAME, + G_BUS_NAME_OWNER_FLAGS_NONE, + on_name_acquired, + on_name_lost, + loop, NULL); - g_debug("entering main loop"); - g_main_loop_run(loop); - g_bus_unown_name (request_name_ret); - g_debug("main loop completed"); + g_debug ("entering main loop"); + g_main_loop_run (loop); + g_bus_unown_name (request_name_ret); + g_debug ("main loop completed"); - return 0; + return 0; } - diff --git a/src/manager.c b/src/manager.c index c2bec00..e4fa9f7 100644 --- a/src/manager.c +++ b/src/manager.c @@ -7,12 +7,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -28,294 +28,316 @@ #include "fprintd.h" static void fprint_manager_constructed (GObject *object); -static gboolean fprint_manager_get_devices(FprintManager *manager, - GPtrArray **devices, GError **error); -static gboolean fprint_manager_get_default_device(FprintManager *manager, - const char **device, GError **error); +static gboolean fprint_manager_get_devices (FprintManager *manager, + GPtrArray **devices, + GError **error); +static gboolean fprint_manager_get_default_device (FprintManager *manager, + const char **device, + GError **error); typedef struct { - GDBusConnection *connection; - GDBusObjectManager *object_manager; - FprintDBusManager *dbus_manager; - FpContext *context; - gboolean no_timeout; - guint timeout_id; + GDBusConnection *connection; + GDBusObjectManager *object_manager; + FprintDBusManager *dbus_manager; + FpContext *context; + gboolean no_timeout; + guint timeout_id; } FprintManagerPrivate; -G_DEFINE_TYPE_WITH_CODE(FprintManager, fprint_manager, G_TYPE_OBJECT, G_ADD_PRIVATE (FprintManager)) +G_DEFINE_TYPE_WITH_CODE (FprintManager, fprint_manager, G_TYPE_OBJECT, G_ADD_PRIVATE (FprintManager)) enum { - PROP_0, - FPRINT_MANAGER_CONNECTION, - N_PROPS + PROP_0, + FPRINT_MANAGER_CONNECTION, + N_PROPS }; static GParamSpec *properties[N_PROPS]; -static void fprint_manager_finalize(GObject *object) +static void +fprint_manager_finalize (GObject *object) { - FprintManagerPrivate *priv = fprint_manager_get_instance_private (FPRINT_MANAGER (object)); + FprintManagerPrivate *priv = fprint_manager_get_instance_private (FPRINT_MANAGER (object)); - g_clear_object (&priv->object_manager); - g_clear_object (&priv->dbus_manager); - g_clear_object (&priv->connection); - g_clear_object (&priv->context); + g_clear_object (&priv->object_manager); + g_clear_object (&priv->dbus_manager); + g_clear_object (&priv->connection); + g_clear_object (&priv->context); - G_OBJECT_CLASS(fprint_manager_parent_class)->finalize(object); + G_OBJECT_CLASS (fprint_manager_parent_class)->finalize (object); } static FprintDevice * -fprint_dbus_object_skeleton_get_device (FprintDBusObjectSkeleton *object) { - FprintDevice *rdev; +fprint_dbus_object_skeleton_get_device (FprintDBusObjectSkeleton *object) +{ + FprintDevice *rdev; - g_object_get (object, "device", &rdev, NULL); - return rdev; + g_object_get (object, "device", &rdev, NULL); + return rdev; } -static void fprint_manager_set_property (GObject *object, guint property_id, - const GValue *value, GParamSpec *pspec) +static void +fprint_manager_set_property (GObject *object, guint property_id, + const GValue *value, GParamSpec *pspec) { - FprintManager *self = FPRINT_MANAGER (object); - FprintManagerPrivate *priv = fprint_manager_get_instance_private (self); + FprintManager *self = FPRINT_MANAGER (object); + FprintManagerPrivate *priv = fprint_manager_get_instance_private (self); - switch (property_id) { - case FPRINT_MANAGER_CONNECTION: - priv->connection = g_value_dup_object (value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - break; - } + switch (property_id) + { + case FPRINT_MANAGER_CONNECTION: + priv->connection = g_value_dup_object (value); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } } -static void fprint_manager_get_property (GObject *object, guint property_id, - GValue *value, GParamSpec *pspec) +static void +fprint_manager_get_property (GObject *object, guint property_id, + GValue *value, GParamSpec *pspec) { - FprintManager *self = FPRINT_MANAGER (object); - FprintManagerPrivate *priv = fprint_manager_get_instance_private (self); + FprintManager *self = FPRINT_MANAGER (object); + FprintManagerPrivate *priv = fprint_manager_get_instance_private (self); - switch (property_id) { - case FPRINT_MANAGER_CONNECTION: - g_value_set_object (value, priv->connection); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - break; - } + switch (property_id) + { + case FPRINT_MANAGER_CONNECTION: + g_value_set_object (value, priv->connection); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } } -static void fprint_manager_class_init(FprintManagerClass *klass) +static void +fprint_manager_class_init (FprintManagerClass *klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); + GObjectClass *object_class = G_OBJECT_CLASS (klass); - object_class->constructed = fprint_manager_constructed; - object_class->set_property = fprint_manager_set_property; - object_class->get_property = fprint_manager_get_property; - object_class->finalize = fprint_manager_finalize; + object_class->constructed = fprint_manager_constructed; + object_class->set_property = fprint_manager_set_property; + object_class->get_property = fprint_manager_get_property; + object_class->finalize = fprint_manager_finalize; - properties[FPRINT_MANAGER_CONNECTION] = - g_param_spec_object ("connection", - "Connection", - "Set GDBus connection property", - G_TYPE_DBUS_CONNECTION, - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_READWRITE); + properties[FPRINT_MANAGER_CONNECTION] = + g_param_spec_object ("connection", + "Connection", + "Set GDBus connection property", + G_TYPE_DBUS_CONNECTION, + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READWRITE); - g_object_class_install_properties (object_class, N_PROPS, properties); + g_object_class_install_properties (object_class, N_PROPS, properties); } -static gchar *get_device_path(FprintDevice *rdev) +static gchar * +get_device_path (FprintDevice *rdev) { - return g_strdup_printf (FPRINT_SERVICE_PATH "/Device/%d", - _fprint_device_get_id(rdev)); + return g_strdup_printf (FPRINT_SERVICE_PATH "/Device/%d", + _fprint_device_get_id (rdev)); } static gboolean fprint_manager_timeout_cb (FprintManager *manager) { - //FIXME kill all the devices - exit(0); - return FALSE; + //FIXME kill all the devices + exit (0); + return FALSE; } static void fprint_manager_in_use_notified (FprintDevice *rdev, GParamSpec *spec, FprintManager *manager) { - FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); - guint num_devices_used = 0; - g_autolist(GDBusObject) devices = NULL; - GList *l; - gboolean in_use; + FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); + guint num_devices_used = 0; - if (priv->timeout_id > 0) { - g_source_remove (priv->timeout_id); - priv->timeout_id = 0; - } - if (priv->no_timeout) - return; + g_autolist (GDBusObject) devices = NULL; + GList *l; + gboolean in_use; - devices = g_dbus_object_manager_get_objects (priv->object_manager); + if (priv->timeout_id > 0) + { + g_source_remove (priv->timeout_id); + priv->timeout_id = 0; + } + if (priv->no_timeout) + return; - for (l = devices; l != NULL; l = l->next) { - g_autoptr(FprintDevice) dev = NULL; - FprintDBusObjectSkeleton *object = l->data; + devices = g_dbus_object_manager_get_objects (priv->object_manager); - dev = fprint_dbus_object_skeleton_get_device (object); - g_object_get (G_OBJECT(dev), "in-use", &in_use, NULL); - if (in_use != FALSE) - num_devices_used++; - } + for (l = devices; l != NULL; l = l->next) + { + g_autoptr(FprintDevice) dev = NULL; + FprintDBusObjectSkeleton *object = l->data; - if (num_devices_used == 0) - priv->timeout_id = g_timeout_add_seconds (TIMEOUT, (GSourceFunc) fprint_manager_timeout_cb, manager); + dev = fprint_dbus_object_skeleton_get_device (object); + g_object_get (G_OBJECT (dev), "in-use", &in_use, NULL); + if (in_use != FALSE) + num_devices_used++; + } + + if (num_devices_used == 0) + priv->timeout_id = g_timeout_add_seconds (TIMEOUT, (GSourceFunc) fprint_manager_timeout_cb, manager); } static gboolean handle_get_devices (FprintManager *manager, GDBusMethodInvocation *invocation, - FprintDBusManager *skeleton) + FprintDBusManager *skeleton) { - g_autoptr(GPtrArray) devices = NULL; - g_autoptr(GError) error = NULL; + g_autoptr(GPtrArray) devices = NULL; + g_autoptr(GError) error = NULL; - if (!fprint_manager_get_devices (manager, &devices, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } + if (!fprint_manager_get_devices (manager, &devices, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - fprint_dbus_manager_complete_get_devices (skeleton, invocation, - (const gchar *const *) - devices->pdata); + fprint_dbus_manager_complete_get_devices (skeleton, invocation, + (const gchar *const *) + devices->pdata); - return TRUE; + return TRUE; } static gboolean -handle_get_default_device (FprintManager *manager, - GDBusMethodInvocation *invocation, - FprintDBusManager *skeleton) +handle_get_default_device (FprintManager *manager, + GDBusMethodInvocation *invocation, + FprintDBusManager *skeleton) { - const gchar *device; - g_autoptr(GError) error = NULL; + const gchar *device; - if (!fprint_manager_get_default_device (manager, &device, &error)) { - g_dbus_method_invocation_return_gerror (invocation, error); - return TRUE; - } + g_autoptr(GError) error = NULL; - fprint_dbus_manager_complete_get_default_device (skeleton, invocation, - device); + if (!fprint_manager_get_default_device (manager, &device, &error)) + { + g_dbus_method_invocation_return_gerror (invocation, error); + return TRUE; + } - return TRUE; + fprint_dbus_manager_complete_get_default_device (skeleton, invocation, + device); + + return TRUE; } static void device_added_cb (FprintManager *manager, FpDevice *device, FpContext *context) { - FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); - g_autoptr(FprintDBusObjectSkeleton) object = NULL; - g_autoptr(FprintDevice) rdev = NULL; - g_autofree gchar *path = NULL; + FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); - rdev = fprint_device_new(device); + g_autoptr(FprintDBusObjectSkeleton) object = NULL; + g_autoptr(FprintDevice) rdev = NULL; + g_autofree gchar *path = NULL; - g_signal_connect (G_OBJECT(rdev), "notify::in-use", - G_CALLBACK (fprint_manager_in_use_notified), manager); + rdev = fprint_device_new (device); - path = get_device_path (rdev); + g_signal_connect (G_OBJECT (rdev), "notify::in-use", + G_CALLBACK (fprint_manager_in_use_notified), manager); - object = fprint_dbus_object_skeleton_new (path); - fprint_dbus_object_skeleton_set_device (object, - FPRINT_DBUS_DEVICE (rdev)); - g_dbus_object_manager_server_export ( - G_DBUS_OBJECT_MANAGER_SERVER (priv->object_manager), - G_DBUS_OBJECT_SKELETON (object)); + path = get_device_path (rdev); + + object = fprint_dbus_object_skeleton_new (path); + fprint_dbus_object_skeleton_set_device (object, + FPRINT_DBUS_DEVICE (rdev)); + g_dbus_object_manager_server_export ( + G_DBUS_OBJECT_MANAGER_SERVER (priv->object_manager), + G_DBUS_OBJECT_SKELETON (object)); } static void device_removed_cb (FprintManager *manager, FpDevice *device, FpContext *context) { - FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); - g_autolist (FprintDBusObjectSkeleton) objects = NULL; - GList *item; + FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); - objects = g_dbus_object_manager_get_objects (priv->object_manager); + g_autolist (FprintDBusObjectSkeleton) objects = NULL; + GList *item; - for (item = objects; item; item = item->next) { - g_autoptr(FprintDevice) rdev = NULL; - g_autoptr(FpDevice) dev = NULL; - FprintDBusObjectSkeleton *object = item->data; + objects = g_dbus_object_manager_get_objects (priv->object_manager); - rdev = fprint_dbus_object_skeleton_get_device (object); - g_object_get (rdev, "dev", &dev, NULL); - if (dev != device) - continue; + for (item = objects; item; item = item->next) + { + g_autoptr(FprintDevice) rdev = NULL; + g_autoptr(FpDevice) dev = NULL; + FprintDBusObjectSkeleton *object = item->data; - g_dbus_object_manager_server_unexport ( - G_DBUS_OBJECT_MANAGER_SERVER (priv->object_manager), - g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (rdev))); + rdev = fprint_dbus_object_skeleton_get_device (object); + g_object_get (rdev, "dev", &dev, NULL); + if (dev != device) + continue; - g_signal_handlers_disconnect_by_data (rdev, manager); + g_dbus_object_manager_server_unexport ( + G_DBUS_OBJECT_MANAGER_SERVER (priv->object_manager), + g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (rdev))); - /* We cannot continue to iterate at this point, but we don't need to either */ - break; - } + g_signal_handlers_disconnect_by_data (rdev, manager); - /* The device that disappeared might have been in-use. - * Do we need to do anything else in this case to clean up more gracefully? */ - fprint_manager_in_use_notified (NULL, NULL, manager); + /* We cannot continue to iterate at this point, but we don't need to either */ + break; + } + + /* The device that disappeared might have been in-use. + * Do we need to do anything else in this case to clean up more gracefully? */ + fprint_manager_in_use_notified (NULL, NULL, manager); } -static void fprint_manager_constructed (GObject *object) +static void +fprint_manager_constructed (GObject *object) { - FprintManager *manager = FPRINT_MANAGER (object); - FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); - GDBusObjectManagerServer *object_manager_server; + FprintManager *manager = FPRINT_MANAGER (object); + FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); + GDBusObjectManagerServer *object_manager_server; - object_manager_server = - g_dbus_object_manager_server_new (FPRINT_SERVICE_PATH "/Device"); + object_manager_server = + g_dbus_object_manager_server_new (FPRINT_SERVICE_PATH "/Device"); - priv->object_manager = G_DBUS_OBJECT_MANAGER (object_manager_server); - priv->dbus_manager = fprint_dbus_manager_skeleton_new (); - priv->context = fp_context_new (); + priv->object_manager = G_DBUS_OBJECT_MANAGER (object_manager_server); + priv->dbus_manager = fprint_dbus_manager_skeleton_new (); + priv->context = fp_context_new (); - g_signal_connect_object (priv->dbus_manager, - "handle-get-devices", - G_CALLBACK (handle_get_devices), - manager, - G_CONNECT_SWAPPED); - g_signal_connect_object (priv->dbus_manager, - "handle-get-default-device", - G_CALLBACK (handle_get_default_device), - manager, - G_CONNECT_SWAPPED); + g_signal_connect_object (priv->dbus_manager, + "handle-get-devices", + G_CALLBACK (handle_get_devices), + manager, + G_CONNECT_SWAPPED); + g_signal_connect_object (priv->dbus_manager, + "handle-get-default-device", + G_CALLBACK (handle_get_default_device), + manager, + G_CONNECT_SWAPPED); - g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (priv->dbus_manager), - priv->connection, - FPRINT_SERVICE_PATH "/Manager", NULL); + g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (priv->dbus_manager), + priv->connection, + FPRINT_SERVICE_PATH "/Manager", NULL); - g_dbus_object_manager_server_set_connection (object_manager_server, - priv->connection); + g_dbus_object_manager_server_set_connection (object_manager_server, + priv->connection); - /* And register the signals for initial enumeration and hotplug. */ - g_signal_connect_object (priv->context, - "device-added", - (GCallback) device_added_cb, - manager, - G_CONNECT_SWAPPED); + /* And register the signals for initial enumeration and hotplug. */ + g_signal_connect_object (priv->context, + "device-added", + (GCallback) device_added_cb, + manager, + G_CONNECT_SWAPPED); - g_signal_connect_object (priv->context, - "device-removed", - (GCallback) device_removed_cb, - manager, - G_CONNECT_SWAPPED); + g_signal_connect_object (priv->context, + "device-removed", + (GCallback) device_removed_cb, + manager, + G_CONNECT_SWAPPED); - /* Prepare everything by enumerating all devices. - * This blocks the main loop until the existing devices are enumerated - */ - fp_context_enumerate (priv->context); + /* Prepare everything by enumerating all devices. + * This blocks the main loop until the existing devices are enumerated + */ + fp_context_enumerate (priv->context); - G_OBJECT_CLASS (fprint_manager_parent_class)->constructed (object); + G_OBJECT_CLASS (fprint_manager_parent_class)->constructed (object); } static void @@ -323,99 +345,113 @@ fprint_manager_init (FprintManager *manager) { } -FprintManager *fprint_manager_new (GDBusConnection *connection, gboolean no_timeout) +FprintManager * +fprint_manager_new (GDBusConnection *connection, gboolean no_timeout) { - FprintManagerPrivate *priv; - GObject *object; + FprintManagerPrivate *priv; + GObject *object; - object = g_object_new (FPRINT_TYPE_MANAGER, "connection", connection, NULL); - priv = fprint_manager_get_instance_private (FPRINT_MANAGER (object)); - priv->no_timeout = no_timeout; + object = g_object_new (FPRINT_TYPE_MANAGER, "connection", connection, NULL); + priv = fprint_manager_get_instance_private (FPRINT_MANAGER (object)); + priv->no_timeout = no_timeout; - if (!priv->no_timeout) - priv->timeout_id = g_timeout_add_seconds (TIMEOUT, (GSourceFunc) fprint_manager_timeout_cb, object); + if (!priv->no_timeout) + priv->timeout_id = g_timeout_add_seconds (TIMEOUT, (GSourceFunc) fprint_manager_timeout_cb, object); - return FPRINT_MANAGER (object); + return FPRINT_MANAGER (object); } -static gboolean fprint_manager_get_devices(FprintManager *manager, - GPtrArray **devices, GError **error) +static gboolean +fprint_manager_get_devices (FprintManager *manager, + GPtrArray **devices, GError **error) { - FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); - g_autolist (FprintDBusObjectSkeleton) objects = NULL; - GList *l; - int num_open; - GPtrArray *devs; + FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); - objects = g_dbus_object_manager_get_objects (priv->object_manager); - objects = g_list_reverse (objects); + g_autolist (FprintDBusObjectSkeleton) objects = NULL; + GList *l; + int num_open; + GPtrArray *devs; - num_open = g_list_length (objects); - devs = g_ptr_array_sized_new(num_open); + objects = g_dbus_object_manager_get_objects (priv->object_manager); + objects = g_list_reverse (objects); - if (num_open > 0) { - for (l = objects; l != NULL; l = l->next) { - g_autoptr(FprintDevice) rdev = NULL; - FprintDBusObjectSkeleton *object = l->data; - const char *path; + num_open = g_list_length (objects); + devs = g_ptr_array_sized_new (num_open); - rdev = fprint_dbus_object_skeleton_get_device (object); - path = g_dbus_interface_skeleton_get_object_path ( - G_DBUS_INTERFACE_SKELETON (rdev)); - g_ptr_array_add (devs, (char *) path); - } - } - g_ptr_array_add (devs, NULL); + if (num_open > 0) + { + for (l = objects; l != NULL; l = l->next) + { + g_autoptr(FprintDevice) rdev = NULL; + FprintDBusObjectSkeleton *object = l->data; + const char *path; - *devices = devs; - return TRUE; + rdev = fprint_dbus_object_skeleton_get_device (object); + path = g_dbus_interface_skeleton_get_object_path ( + G_DBUS_INTERFACE_SKELETON (rdev)); + g_ptr_array_add (devs, (char *) path); + } + } + g_ptr_array_add (devs, NULL); + + *devices = devs; + return TRUE; } -static gboolean fprint_manager_get_default_device(FprintManager *manager, - const char **device, GError **error) +static gboolean +fprint_manager_get_default_device (FprintManager *manager, + const char **device, GError **error) { - FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); - g_autolist (FprintDBusObjectSkeleton) objects = NULL; - int num_open; + FprintManagerPrivate *priv = fprint_manager_get_instance_private (manager); - objects = g_dbus_object_manager_get_objects (priv->object_manager); - num_open = g_list_length (objects); + g_autolist (FprintDBusObjectSkeleton) objects = NULL; + int num_open; - if (num_open > 0) { - g_autoptr(FprintDevice) rdev = NULL; - FprintDBusObjectSkeleton *object = g_list_last (objects)->data; + objects = g_dbus_object_manager_get_objects (priv->object_manager); + num_open = g_list_length (objects); - rdev = fprint_dbus_object_skeleton_get_device (object); - *device = g_dbus_interface_skeleton_get_object_path ( - G_DBUS_INTERFACE_SKELETON (rdev)); - return TRUE; - } else { - g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_NO_SUCH_DEVICE, - "No devices available"); - *device = NULL; - return FALSE; - } + if (num_open > 0) + { + g_autoptr(FprintDevice) rdev = NULL; + FprintDBusObjectSkeleton *object = g_list_last (objects)->data; + + rdev = fprint_dbus_object_skeleton_get_device (object); + *device = g_dbus_interface_skeleton_get_object_path ( + G_DBUS_INTERFACE_SKELETON (rdev)); + return TRUE; + } + else + { + g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_NO_SUCH_DEVICE, + "No devices available"); + *device = NULL; + return FALSE; + } } -GQuark fprint_error_quark (void) +GQuark +fprint_error_quark (void) { - static volatile gsize quark = 0; - if (g_once_init_enter (&quark)) { - g_autoptr(GEnumClass) errors_enum = NULL; - GQuark domain; - unsigned i; + static volatile gsize quark = 0; - domain = g_quark_from_static_string ("fprintd-error-quark"); - errors_enum = g_type_class_ref (FPRINT_TYPE_ERROR); + if (g_once_init_enter (&quark)) + { + g_autoptr(GEnumClass) errors_enum = NULL; + GQuark domain; + unsigned i; - for (i = 0; i < errors_enum->n_values; ++i) { - GEnumValue *value = &errors_enum->values[i]; + domain = g_quark_from_static_string ("fprintd-error-quark"); + errors_enum = g_type_class_ref (FPRINT_TYPE_ERROR); - g_dbus_error_register_error (domain, value->value, - value->value_nick); - } + for (i = 0; i < errors_enum->n_values; ++i) + { + GEnumValue *value = &errors_enum->values[i]; - g_once_init_leave (&quark, domain); - } - return (GQuark) quark; + g_dbus_error_register_error (domain, value->value, + value->value_nick); + } + + g_once_init_leave (&quark, domain); + } + return (GQuark) quark; } diff --git a/src/storage.h b/src/storage.h index 099e995..ef25a84 100644 --- a/src/storage.h +++ b/src/storage.h @@ -6,12 +6,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -28,19 +28,21 @@ typedef int (*storage_print_data_load)(FpDevice *dev, typedef int (*storage_print_data_delete)(FpDevice *dev, FpFinger finger, const char *username); -typedef GSList *(*storage_discover_prints)(FpDevice *dev, const char *username); +typedef GSList *(*storage_discover_prints)(FpDevice *dev, + const char *username); typedef GSList *(*storage_discover_users)(void); typedef int (*storage_init)(void); typedef int (*storage_deinit)(void); -struct storage { - storage_init init; - storage_deinit deinit; - storage_print_data_save print_data_save; - storage_print_data_load print_data_load; - storage_print_data_delete print_data_delete; - storage_discover_prints discover_prints; - storage_discover_users discover_users; +struct storage +{ + storage_init init; + storage_deinit deinit; + storage_print_data_save print_data_save; + storage_print_data_load print_data_load; + storage_print_data_delete print_data_delete; + storage_discover_prints discover_prints; + storage_discover_users discover_users; }; typedef struct storage fp_storage; diff --git a/utils/delete.c b/utils/delete.c index 2e0dbd6..49948f0 100644 --- a/utils/delete.c +++ b/utils/delete.c @@ -7,12 +7,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -26,126 +26,146 @@ static FprintDBusManager *manager = NULL; static GDBusConnection *connection = NULL; -static void create_manager(void) +static void +create_manager (void) { - g_autoptr(GError) error = NULL; + g_autoptr(GError) error = NULL; - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); - if (connection == NULL) { - g_print("Failed to connect to session bus: %s\n", error->message); - exit (1); - } + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + if (connection == NULL) + { + g_print ("Failed to connect to session bus: %s\n", error->message); + exit (1); + } - manager = fprint_dbus_manager_proxy_new_sync (connection, - G_DBUS_PROXY_FLAGS_NONE, - "net.reactivated.Fprint", - "/net/reactivated/Fprint/Manager", - NULL, &error); - if (manager == NULL) { - g_print ("Failed to get Fprintd manager: %s\n", error->message); - exit (1); - } + manager = fprint_dbus_manager_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + "net.reactivated.Fprint", + "/net/reactivated/Fprint/Manager", + NULL, &error); + if (manager == NULL) + { + g_print ("Failed to get Fprintd manager: %s\n", error->message); + exit (1); + } } -static void delete_fingerprints (FprintDBusDevice *dev, const char *username) +static void +delete_fingerprints (FprintDBusDevice *dev, const char *username) { - g_autoptr(GError) error = NULL; + g_autoptr(GError) error = NULL; - if (!fprint_dbus_device_call_claim_sync (dev, username, NULL, &error)) { - g_print("failed to claim device: %s\n", error->message); - exit (1); - } + if (!fprint_dbus_device_call_claim_sync (dev, username, NULL, &error)) + { + g_print ("failed to claim device: %s\n", error->message); + exit (1); + } - if (!fprint_dbus_device_call_delete_enrolled_fingers2_sync (dev, NULL, - &error)) { - gboolean ignore_error = FALSE; - if (g_dbus_error_is_remote_error (error)) { - g_autofree char *dbus_error = - g_dbus_error_get_remote_error (error); - if (g_str_equal (dbus_error, - "net.reactivated.Fprint.Error.NoEnrolledPrints")) { - g_print ("No fingerprints to delete on %s\n", - fprint_dbus_device_get_name (dev)); - ignore_error = TRUE; - } - } - if (!ignore_error) { - g_print("ListEnrolledFingers failed: %s\n", - error->message); - exit (1); - } else { - g_print ("No fingerprints to delete on %s\n", - fprint_dbus_device_get_name (dev)); - } - } else { - g_print ("Fingerprints deleted on %s\n", - fprint_dbus_device_get_name (dev)); - } - g_clear_error (&error); + if (!fprint_dbus_device_call_delete_enrolled_fingers2_sync (dev, NULL, + &error)) + { + gboolean ignore_error = FALSE; + if (g_dbus_error_is_remote_error (error)) + { + g_autofree char *dbus_error = + g_dbus_error_get_remote_error (error); + if (g_str_equal (dbus_error, + "net.reactivated.Fprint.Error.NoEnrolledPrints")) + { + g_print ("No fingerprints to delete on %s\n", + fprint_dbus_device_get_name (dev)); + ignore_error = TRUE; + } + } + if (!ignore_error) + { + g_print ("ListEnrolledFingers failed: %s\n", + error->message); + exit (1); + } + else + { + g_print ("No fingerprints to delete on %s\n", + fprint_dbus_device_get_name (dev)); + } + } + else + { + g_print ("Fingerprints deleted on %s\n", + fprint_dbus_device_get_name (dev)); + } + g_clear_error (&error); - if (!fprint_dbus_device_call_release_sync (dev, NULL, &error)) { - g_print("ReleaseDevice failed: %s\n", error->message); - exit (1); - } + if (!fprint_dbus_device_call_release_sync (dev, NULL, &error)) + { + g_print ("ReleaseDevice failed: %s\n", error->message); + exit (1); + } } -static void process_devices(char **argv) +static void +process_devices (char **argv) { - g_autoptr(GError) error = NULL; - g_auto(GStrv) devices = NULL; - char *path; - guint num_devices; - guint i; + g_autoptr(GError) error = NULL; + g_auto(GStrv) devices = NULL; + char *path; + guint num_devices; + guint i; - if (!fprint_dbus_manager_call_get_devices_sync (manager, &devices, - NULL, &error)) { - g_print("Impossible to get devices: %s\n", error->message); - exit (1); - } + if (!fprint_dbus_manager_call_get_devices_sync (manager, &devices, + NULL, &error)) + { + g_print ("Impossible to get devices: %s\n", error->message); + exit (1); + } - num_devices = g_strv_length (devices); - if (num_devices == 0) { - g_print("No devices available\n"); - exit(1); - } + num_devices = g_strv_length (devices); + if (num_devices == 0) + { + g_print ("No devices available\n"); + exit (1); + } - g_print ("found %u devices\n", num_devices); - for (i = 0; devices[i] != NULL; i++) { - path = devices[i]; - g_print("Device at %s\n", path); - } + g_print ("found %u devices\n", num_devices); + for (i = 0; devices[i] != NULL; i++) + { + path = devices[i]; + g_print ("Device at %s\n", path); + } - for (i = 0; devices[i] != NULL; i++) { - g_autoptr(FprintDBusDevice) dev = NULL; - guint j; + for (i = 0; devices[i] != NULL; i++) + { + g_autoptr(FprintDBusDevice) dev = NULL; + guint j; - path = devices[i]; - g_print("Using device %s\n", path); + path = devices[i]; + g_print ("Using device %s\n", path); - /* NOTE: We should handle error cases! */ - dev = fprint_dbus_device_proxy_new_sync (connection, - G_DBUS_PROXY_FLAGS_NONE, - "net.reactivated.Fprint", - path, NULL, NULL); + /* NOTE: We should handle error cases! */ + dev = fprint_dbus_device_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + "net.reactivated.Fprint", + path, NULL, NULL); - for (j = 1; argv[j] != NULL; j++) - delete_fingerprints (dev, argv[j]); - } + for (j = 1; argv[j] != NULL; j++) + delete_fingerprints (dev, argv[j]); + } } -int main(int argc, char **argv) +int +main (int argc, char **argv) { - setlocale (LC_ALL, ""); + setlocale (LC_ALL, ""); - create_manager(); + create_manager (); - if (argc < 2) { - g_print ("Usage: %s [usernames...]\n", argv[0]); - return 1; - } + if (argc < 2) + { + g_print ("Usage: %s [usernames...]\n", argv[0]); + return 1; + } - process_devices (argv); + process_devices (argv); - return 0; + return 0; } - diff --git a/utils/enroll.c b/utils/enroll.c index ab068dd..84098ab 100644 --- a/utils/enroll.c +++ b/utils/enroll.c @@ -7,12 +7,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -33,171 +33,193 @@ static GDBusConnection *connection = NULL; static char *finger_name = NULL; static char **usernames = NULL; -static void create_manager(void) +static void +create_manager (void) { - g_autoptr(GError) error = NULL; + g_autoptr(GError) error = NULL; - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); - if (connection == NULL) { - g_print("Failed to connect to session bus: %s\n", error->message); - exit (1); - } + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + if (connection == NULL) + { + g_print ("Failed to connect to session bus: %s\n", error->message); + exit (1); + } - manager = fprint_dbus_manager_proxy_new_sync (connection, - G_DBUS_PROXY_FLAGS_NONE, - "net.reactivated.Fprint", - "/net/reactivated/Fprint/Manager", - NULL, &error); - if (manager == NULL) { - g_print ("Failed to get Fprintd manager: %s\n", error->message); - exit (1); - } + manager = fprint_dbus_manager_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + "net.reactivated.Fprint", + "/net/reactivated/Fprint/Manager", + NULL, &error); + if (manager == NULL) + { + g_print ("Failed to get Fprintd manager: %s\n", error->message); + exit (1); + } } -static FprintDBusDevice *open_device (const char *username) +static FprintDBusDevice * +open_device (const char *username) { - g_autoptr(FprintDBusDevice) dev = NULL; - g_autoptr(GError) error = NULL; - g_autofree char *path = NULL; + g_autoptr(FprintDBusDevice) dev = NULL; + g_autoptr(GError) error = NULL; + g_autofree char *path = NULL; - if (!fprint_dbus_manager_call_get_default_device_sync (manager, &path, - NULL, &error)) { - g_print("Impossible to enroll: %s\n", error->message); - exit (1); - } + if (!fprint_dbus_manager_call_get_default_device_sync (manager, &path, + NULL, &error)) + { + g_print ("Impossible to enroll: %s\n", error->message); + exit (1); + } - g_print("Using device %s\n", path); + g_print ("Using device %s\n", path); - dev = fprint_dbus_device_proxy_new_sync (connection, - G_DBUS_PROXY_FLAGS_NONE, - "net.reactivated.Fprint", - path, NULL, &error); + dev = fprint_dbus_device_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + "net.reactivated.Fprint", + path, NULL, &error); - if (error) { - g_print ("failed to connect to device: %s\n", error->message); - exit (1); - } + if (error) + { + g_print ("failed to connect to device: %s\n", error->message); + exit (1); + } - if (!fprint_dbus_device_call_claim_sync (dev, username, NULL, &error)) { - g_print("failed to claim device: %s\n", error->message); - exit (1); - } - return g_steal_pointer (&dev); + if (!fprint_dbus_device_call_claim_sync (dev, username, NULL, &error)) + { + g_print ("failed to claim device: %s\n", error->message); + exit (1); + } + return g_steal_pointer (&dev); } -static void enroll_result(GObject *object, const char *result, gboolean done, void *user_data) +static void +enroll_result (GObject *object, const char *result, gboolean done, void *user_data) { - gboolean *enroll_completed = user_data; - g_print("Enroll result: %s\n", result); - if (done != FALSE) - *enroll_completed = TRUE; + gboolean *enroll_completed = user_data; + + g_print ("Enroll result: %s\n", result); + if (done != FALSE) + *enroll_completed = TRUE; } -static void proxy_signal_cb (GDBusProxy *proxy, - const gchar *sender_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) +static void +proxy_signal_cb (GDBusProxy *proxy, + const gchar *sender_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) { - if (g_str_equal (signal_name, "EnrollStatus")) { - const gchar *result; - gboolean done; + if (g_str_equal (signal_name, "EnrollStatus")) + { + const gchar *result; + gboolean done; - g_variant_get (parameters, "(&sb)", &result, &done); - enroll_result (G_OBJECT (proxy), result, done, user_data); - } + g_variant_get (parameters, "(&sb)", &result, &done); + enroll_result (G_OBJECT (proxy), result, done, user_data); + } } -static void do_enroll (FprintDBusDevice *dev) +static void +do_enroll (FprintDBusDevice *dev) { - g_autoptr(GError) error = NULL; - gboolean enroll_completed = FALSE; - gboolean found; - guint i; + g_autoptr(GError) error = NULL; + gboolean enroll_completed = FALSE; + gboolean found; + guint i; - g_signal_connect (dev, "g-signal", G_CALLBACK (proxy_signal_cb), - &enroll_completed); + g_signal_connect (dev, "g-signal", G_CALLBACK (proxy_signal_cb), + &enroll_completed); - found = FALSE; - for (i = 0; fingers[i].dbus_name != NULL; i++) { - if (g_strcmp0 (fingers[i].dbus_name, finger_name) == 0) { - found = TRUE; - break; - } - } - if (!found) { - g_autoptr(GString) s = NULL; + found = FALSE; + for (i = 0; fingers[i].dbus_name != NULL; i++) + { + if (g_strcmp0 (fingers[i].dbus_name, finger_name) == 0) + { + found = TRUE; + break; + } + } + if (!found) + { + g_autoptr(GString) s = NULL; - s = g_string_new (NULL); - g_string_append_printf (s, "Invalid finger name '%s'. Name must be one of ", finger_name); - for (i = 0; fingers[i].dbus_name != NULL; i++) { - g_string_append_printf (s, "%s", fingers[i].dbus_name); - if (fingers[i + 1].dbus_name != NULL) - g_string_append (s, ", "); - } - g_warning ("%s", s->str); - exit (1); - } + s = g_string_new (NULL); + g_string_append_printf (s, "Invalid finger name '%s'. Name must be one of ", finger_name); + for (i = 0; fingers[i].dbus_name != NULL; i++) + { + g_string_append_printf (s, "%s", fingers[i].dbus_name); + if (fingers[i + 1].dbus_name != NULL) + g_string_append (s, ", "); + } + g_warning ("%s", s->str); + exit (1); + } - g_print("Enrolling %s finger.\n", finger_name); - if (!fprint_dbus_device_call_enroll_start_sync (dev, finger_name, NULL, - &error)) { - g_print("EnrollStart failed: %s\n", error->message); - exit (1); - } + g_print ("Enrolling %s finger.\n", finger_name); + if (!fprint_dbus_device_call_enroll_start_sync (dev, finger_name, NULL, + &error)) + { + g_print ("EnrollStart failed: %s\n", error->message); + exit (1); + } - while (!enroll_completed) - g_main_context_iteration(NULL, TRUE); + while (!enroll_completed) + g_main_context_iteration (NULL, TRUE); - g_signal_handlers_disconnect_by_func (dev, proxy_signal_cb, &enroll_result); + g_signal_handlers_disconnect_by_func (dev, proxy_signal_cb, &enroll_result); - if (!fprint_dbus_device_call_enroll_stop_sync (dev, NULL, &error)) { - g_print("VerifyStop failed: %s\n", error->message); - exit(1); - } + if (!fprint_dbus_device_call_enroll_stop_sync (dev, NULL, &error)) + { + g_print ("VerifyStop failed: %s\n", error->message); + exit (1); + } } -static void release_device (FprintDBusDevice *dev) +static void +release_device (FprintDBusDevice *dev) { - g_autoptr(GError) error = NULL; - if (!fprint_dbus_device_call_release_sync (dev, NULL, &error)) { - g_print("ReleaseDevice failed: %s\n", error->message); - exit (1); - } + g_autoptr(GError) error = NULL; + if (!fprint_dbus_device_call_release_sync (dev, NULL, &error)) + { + g_print ("ReleaseDevice failed: %s\n", error->message); + exit (1); + } } static const GOptionEntry entries[] = { - { "finger", 'f', 0, G_OPTION_ARG_STRING, &finger_name, "Finger selected to verify (default is automatic)", NULL }, - { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &usernames, NULL, "[username]" }, - { NULL } + { "finger", 'f', 0, G_OPTION_ARG_STRING, &finger_name, "Finger selected to verify (default is automatic)", NULL }, + { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &usernames, NULL, "[username]" }, + { NULL } }; -int main(int argc, char **argv) +int +main (int argc, char **argv) { - g_autoptr(FprintDBusDevice) dev = NULL; - GOptionContext *context; - g_autoptr(GError) err = NULL; + g_autoptr(FprintDBusDevice) dev = NULL; + GOptionContext *context; - setlocale (LC_ALL, ""); + g_autoptr(GError) err = NULL; - context = g_option_context_new ("Enroll a fingerprint"); - g_option_context_add_main_entries (context, entries, NULL); + setlocale (LC_ALL, ""); - if (g_option_context_parse (context, &argc, &argv, &err) == FALSE) { - g_print ("couldn't parse command-line options: %s\n", err->message); - return 1; - } + context = g_option_context_new ("Enroll a fingerprint"); + g_option_context_add_main_entries (context, entries, NULL); - if (finger_name == NULL) - finger_name = g_strdup("right-index-finger"); + if (g_option_context_parse (context, &argc, &argv, &err) == FALSE) + { + g_print ("couldn't parse command-line options: %s\n", err->message); + return 1; + } - create_manager(); + if (finger_name == NULL) + finger_name = g_strdup ("right-index-finger"); - dev = open_device (usernames ? usernames[0] : ""); - do_enroll(dev); - release_device(dev); - g_free(finger_name); - g_strfreev(usernames); - return 0; + create_manager (); + + dev = open_device (usernames ? usernames[0] : ""); + do_enroll (dev); + release_device (dev); + g_free (finger_name); + g_strfreev (usernames); + return 0; } - diff --git a/utils/list.c b/utils/list.c index d51cf6d..df6422c 100644 --- a/utils/list.c +++ b/utils/list.c @@ -7,12 +7,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -26,125 +26,137 @@ static FprintDBusManager *manager = NULL; static GDBusConnection *connection = NULL; -static void create_manager(void) +static void +create_manager (void) { - g_autoptr(GError) error = NULL; + g_autoptr(GError) error = NULL; - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); - if (connection == NULL) { - g_print("Failed to connect to session bus: %s\n", error->message); - exit (1); - } + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + if (connection == NULL) + { + g_print ("Failed to connect to session bus: %s\n", error->message); + exit (1); + } - manager = fprint_dbus_manager_proxy_new_sync (connection, - G_DBUS_PROXY_FLAGS_NONE, - "net.reactivated.Fprint", - "/net/reactivated/Fprint/Manager", - NULL, &error); - if (manager == NULL) { - g_print ("Failed to get Fprintd manager: %s\n", error->message); - exit (1); - } + manager = fprint_dbus_manager_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + "net.reactivated.Fprint", + "/net/reactivated/Fprint/Manager", + NULL, &error); + if (manager == NULL) + { + g_print ("Failed to get Fprintd manager: %s\n", error->message); + exit (1); + } } -static void list_fingerprints (FprintDBusDevice *dev, const char *username) +static void +list_fingerprints (FprintDBusDevice *dev, const char *username) { - g_autoptr(GError) error = NULL; - g_auto(GStrv) fingers = NULL; - guint i; + g_autoptr(GError) error = NULL; + g_auto(GStrv) fingers = NULL; + guint i; - if (!fprint_dbus_device_call_list_enrolled_fingers_sync (dev, username, - &fingers, NULL, - &error)) { - gboolean ignore_error = FALSE; - if (g_dbus_error_is_remote_error (error)) { - g_autofree char *dbus_error = - g_dbus_error_get_remote_error (error); - if (g_str_equal (dbus_error, - "net.reactivated.Fprint.Error.NoEnrolledPrints")) { - ignore_error = TRUE; - } - } + if (!fprint_dbus_device_call_list_enrolled_fingers_sync (dev, username, + &fingers, NULL, + &error)) + { + gboolean ignore_error = FALSE; + if (g_dbus_error_is_remote_error (error)) + { + g_autofree char *dbus_error = + g_dbus_error_get_remote_error (error); + if (g_str_equal (dbus_error, + "net.reactivated.Fprint.Error.NoEnrolledPrints")) + ignore_error = TRUE; + } - if (!ignore_error) { - g_print("ListEnrolledFingers failed: %s\n", error->message); - exit (1); - } - } + if (!ignore_error) + { + g_print ("ListEnrolledFingers failed: %s\n", error->message); + exit (1); + } + } - if (fingers == NULL || g_strv_length (fingers) == 0) { - g_print ("User %s has no fingers enrolled for %s.\n", username, - fprint_dbus_device_get_name (dev)); - return; - } + if (fingers == NULL || g_strv_length (fingers) == 0) + { + g_print ("User %s has no fingers enrolled for %s.\n", username, + fprint_dbus_device_get_name (dev)); + return; + } - g_print("Fingerprints for user %s on %s (%s):\n", - username, - fprint_dbus_device_get_name (dev), - fprint_dbus_device_get_scan_type (dev)); + g_print ("Fingerprints for user %s on %s (%s):\n", + username, + fprint_dbus_device_get_name (dev), + fprint_dbus_device_get_scan_type (dev)); - for (i = 0; fingers[i] != NULL; i++) { - g_print(" - #%d: %s\n", i, fingers[i]); - } + for (i = 0; fingers[i] != NULL; i++) + g_print (" - #%d: %s\n", i, fingers[i]); } -static void process_devices(char **argv) +static void +process_devices (char **argv) { - g_auto(GStrv) devices = NULL; - g_autoptr(GError) error = NULL; - char *path; - guint num_devices; - guint i; + g_auto(GStrv) devices = NULL; + g_autoptr(GError) error = NULL; + char *path; + guint num_devices; + guint i; - if (!fprint_dbus_manager_call_get_devices_sync (manager, &devices, NULL, - &error)) { - g_print("Impossible to get devices: %s\n", error->message); - exit (1); - } + if (!fprint_dbus_manager_call_get_devices_sync (manager, &devices, NULL, + &error)) + { + g_print ("Impossible to get devices: %s\n", error->message); + exit (1); + } - num_devices = g_strv_length (devices); - if (num_devices == 0) { - g_print("No devices available\n"); - exit(1); - } + num_devices = g_strv_length (devices); + if (num_devices == 0) + { + g_print ("No devices available\n"); + exit (1); + } - g_print ("found %u devices\n", num_devices); - for (i = 0; devices[i] != NULL; i++) { - path = devices[i]; - g_print("Device at %s\n", path); - } + g_print ("found %u devices\n", num_devices); + for (i = 0; devices[i] != NULL; i++) + { + path = devices[i]; + g_print ("Device at %s\n", path); + } - for (i = 0; devices[i] != NULL; i++) { - g_autoptr(FprintDBusDevice) dev = NULL; - guint j; + for (i = 0; devices[i] != NULL; i++) + { + g_autoptr(FprintDBusDevice) dev = NULL; + guint j; - path = devices[i]; - g_print("Using device %s\n", path); + path = devices[i]; + g_print ("Using device %s\n", path); - /* NOTE: We should handle error cases! */ - dev = fprint_dbus_device_proxy_new_sync (connection, - G_DBUS_PROXY_FLAGS_NONE, - "net.reactivated.Fprint", - path, NULL, NULL); + /* NOTE: We should handle error cases! */ + dev = fprint_dbus_device_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + "net.reactivated.Fprint", + path, NULL, NULL); - for (j = 1; argv[j] != NULL; j++) - list_fingerprints (dev, argv[j]); - } + for (j = 1; argv[j] != NULL; j++) + list_fingerprints (dev, argv[j]); + } } -int main(int argc, char **argv) +int +main (int argc, char **argv) { - setlocale (LC_ALL, ""); + setlocale (LC_ALL, ""); - create_manager(); + create_manager (); - if (argc < 2) { - g_print ("Usage: %s [usernames...]\n", argv[0]); - return 1; - } + if (argc < 2) + { + g_print ("Usage: %s [usernames...]\n", argv[0]); + return 1; + } - process_devices (argv); + process_devices (argv); - return 0; + return 0; } - diff --git a/utils/verify.c b/utils/verify.c index af3a1a7..8e82e03 100644 --- a/utils/verify.c +++ b/utils/verify.c @@ -7,12 +7,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -31,250 +31,274 @@ static char *finger_name = NULL; static gboolean g_fatal_warnings = FALSE; static char **usernames = NULL; -static void create_manager(void) +static void +create_manager (void) { - g_autoptr(GError) error = NULL; + g_autoptr(GError) error = NULL; - connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); - if (connection == NULL) { - g_print("Failed to connect to session bus: %s\n", error->message); - exit (1); - } + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + if (connection == NULL) + { + g_print ("Failed to connect to session bus: %s\n", error->message); + exit (1); + } - manager = fprint_dbus_manager_proxy_new_sync (connection, - G_DBUS_PROXY_FLAGS_NONE, - "net.reactivated.Fprint", - "/net/reactivated/Fprint/Manager", - NULL, &error); - if (manager == NULL) { - g_print ("Failed to get Fprintd manager: %s\n", error->message); - exit (1); - } + manager = fprint_dbus_manager_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + "net.reactivated.Fprint", + "/net/reactivated/Fprint/Manager", + NULL, &error); + if (manager == NULL) + { + g_print ("Failed to get Fprintd manager: %s\n", error->message); + exit (1); + } } -static FprintDBusDevice *open_device (const char *username) +static FprintDBusDevice * +open_device (const char *username) { - g_autoptr(FprintDBusDevice) dev = NULL; - g_autoptr(GError) error = NULL; - g_autofree char *path = NULL; + g_autoptr(FprintDBusDevice) dev = NULL; + g_autoptr(GError) error = NULL; + g_autofree char *path = NULL; - if (!fprint_dbus_manager_call_get_default_device_sync (manager, &path, - NULL, &error)) { - g_print("Impossible to verify: %s\n", error->message); - exit (1); - } + if (!fprint_dbus_manager_call_get_default_device_sync (manager, &path, + NULL, &error)) + { + g_print ("Impossible to verify: %s\n", error->message); + exit (1); + } - g_print("Using device %s\n", path); + g_print ("Using device %s\n", path); - dev = fprint_dbus_device_proxy_new_sync (connection, - G_DBUS_PROXY_FLAGS_NONE, - "net.reactivated.Fprint", - path, NULL, &error); + dev = fprint_dbus_device_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + "net.reactivated.Fprint", + path, NULL, &error); - if (error) { - g_print ("failed to connect to device: %s\n", error->message); - exit (1); - } + if (error) + { + g_print ("failed to connect to device: %s\n", error->message); + exit (1); + } - if (!fprint_dbus_device_call_claim_sync (dev, username, NULL, &error)) { - g_print("failed to claim device: %s\n", error->message); - exit (1); - } + if (!fprint_dbus_device_call_claim_sync (dev, username, NULL, &error)) + { + g_print ("failed to claim device: %s\n", error->message); + exit (1); + } - return g_steal_pointer (&dev); + return g_steal_pointer (&dev); } -static void find_finger (FprintDBusDevice *dev, const char *username) +static void +find_finger (FprintDBusDevice *dev, const char *username) { - g_autoptr(GError) error = NULL; - g_auto(GStrv) fingers = NULL; - guint i; + g_autoptr(GError) error = NULL; + g_auto(GStrv) fingers = NULL; + guint i; - if (!fprint_dbus_device_call_list_enrolled_fingers_sync (dev, username, - &fingers, - NULL, &error)) { - g_print("ListEnrolledFingers failed: %s\n", error->message); - exit (1); - } + if (!fprint_dbus_device_call_list_enrolled_fingers_sync (dev, username, + &fingers, + NULL, &error)) + { + g_print ("ListEnrolledFingers failed: %s\n", error->message); + exit (1); + } - if (fingers == NULL || g_strv_length (fingers) == 0) { - g_print("No fingers enrolled for this device.\n"); - exit(1); - } + if (fingers == NULL || g_strv_length (fingers) == 0) + { + g_print ("No fingers enrolled for this device.\n"); + exit (1); + } - g_print("Listing enrolled fingers:\n"); - for (i = 0; fingers[i] != NULL; i++) { - g_print(" - #%d: %s\n", i, fingers[i]); - } + g_print ("Listing enrolled fingers:\n"); + for (i = 0; fingers[i] != NULL; i++) + g_print (" - #%d: %s\n", i, fingers[i]); - if (finger_name && !g_str_equal (finger_name, "any") && - !g_strv_contains ((const char **) fingers, finger_name)) { - g_print("Finger '%s' not enrolled for user %s.\n", finger_name, - username); - g_free (finger_name); - exit(1); - } + if (finger_name && !g_str_equal (finger_name, "any") && + !g_strv_contains ((const char **) fingers, finger_name)) + { + g_print ("Finger '%s' not enrolled for user %s.\n", finger_name, + username); + g_free (finger_name); + exit (1); + } - if (finger_name == NULL) { - finger_name = g_strdup (fingers[0]); - } + if (finger_name == NULL) + finger_name = g_strdup (fingers[0]); } -struct VerifyState { - GError *error; - gboolean started; - gboolean completed; +struct VerifyState +{ + GError *error; + gboolean started; + gboolean completed; }; -static void verify_result(GObject *object, const char *result, gboolean done, void *user_data) +static void +verify_result (GObject *object, const char *result, gboolean done, void *user_data) { - struct VerifyState *verify_state = user_data; - g_print("Verify result: %s (%s)\n", result, done ? "done" : "not done"); - if (done != FALSE) - verify_state->completed = TRUE; + struct VerifyState *verify_state = user_data; + + g_print ("Verify result: %s (%s)\n", result, done ? "done" : "not done"); + if (done != FALSE) + verify_state->completed = TRUE; } -static void verify_finger_selected(GObject *object, const char *name, void *user_data) +static void +verify_finger_selected (GObject *object, const char *name, void *user_data) { - g_print("Verifying: %s\n", name); + g_print ("Verifying: %s\n", name); } -static void verify_started_cb (GObject *obj, - GAsyncResult *res, - gpointer user_data) +static void +verify_started_cb (GObject *obj, + GAsyncResult *res, + gpointer user_data) { - struct VerifyState *verify_state = user_data; + struct VerifyState *verify_state = user_data; - if (fprint_dbus_device_call_verify_start_finish (FPRINT_DBUS_DEVICE (obj), res, &verify_state->error)) - verify_state->started = TRUE; + if (fprint_dbus_device_call_verify_start_finish (FPRINT_DBUS_DEVICE (obj), res, &verify_state->error)) + verify_state->started = TRUE; } -static void proxy_signal_cb (GDBusProxy *proxy, - const gchar *sender_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) +static void +proxy_signal_cb (GDBusProxy *proxy, + const gchar *sender_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) { - struct VerifyState *verify_state = user_data; + struct VerifyState *verify_state = user_data; - if (!verify_state->started) - return; + if (!verify_state->started) + return; - if (g_str_equal (signal_name, "VerifyStatus")) { - const gchar *result; - gboolean done; + if (g_str_equal (signal_name, "VerifyStatus")) + { + const gchar *result; + gboolean done; - g_variant_get (parameters, "(&sb)", &result, &done); - verify_result (G_OBJECT (proxy), result, done, user_data); - } else if (g_str_equal (signal_name, "VerifyFingerSelected")) { - const gchar *name; + g_variant_get (parameters, "(&sb)", &result, &done); + verify_result (G_OBJECT (proxy), result, done, user_data); + } + else if (g_str_equal (signal_name, "VerifyFingerSelected")) + { + const gchar *name; - g_variant_get (parameters, "(&s)", &name); - verify_finger_selected (G_OBJECT (proxy), name, user_data); - } + g_variant_get (parameters, "(&s)", &name); + verify_finger_selected (G_OBJECT (proxy), name, user_data); + } } -static void do_verify (FprintDBusDevice *dev) +static void +do_verify (FprintDBusDevice *dev) { - g_autoptr(GError) error = NULL; - struct VerifyState verify_state = { 0 }; + g_autoptr(GError) error = NULL; + struct VerifyState verify_state = { 0 }; - /* This one is funny. We connect to the signal immediately to avoid - * race conditions. However, we must ignore any authentication results - * that happen before our start call returns. - * This is because the verify call itself may internally try to verify - * against fprintd (possibly using a separate account). - * - * To do so, we *must* use the async version of the verify call, as the - * sync version would cause the signals to be queued and only processed - * after it returns. - */ + /* This one is funny. We connect to the signal immediately to avoid + * race conditions. However, we must ignore any authentication results + * that happen before our start call returns. + * This is because the verify call itself may internally try to verify + * against fprintd (possibly using a separate account). + * + * To do so, we *must* use the async version of the verify call, as the + * sync version would cause the signals to be queued and only processed + * after it returns. + */ - g_signal_connect (dev, "g-signal", G_CALLBACK (proxy_signal_cb), - &verify_state); + g_signal_connect (dev, "g-signal", G_CALLBACK (proxy_signal_cb), + &verify_state); - fprint_dbus_device_call_verify_start (dev, finger_name, NULL, - verify_started_cb, - &verify_state); + fprint_dbus_device_call_verify_start (dev, finger_name, NULL, + verify_started_cb, + &verify_state); - /* Wait for verify start while discarding any VerifyStatus signals */ - while (!verify_state.started && !verify_state.error) - g_main_context_iteration(NULL, TRUE); + /* Wait for verify start while discarding any VerifyStatus signals */ + while (!verify_state.started && !verify_state.error) + g_main_context_iteration (NULL, TRUE); - if (verify_state.error) { - g_print("VerifyStart failed: %s\n", verify_state.error->message); - g_clear_error (&verify_state.error); - exit (1); - } - g_print("Verify started!\n"); + if (verify_state.error) + { + g_print ("VerifyStart failed: %s\n", verify_state.error->message); + g_clear_error (&verify_state.error); + exit (1); + } + g_print ("Verify started!\n"); - /* VerifyStatus signals are processing, wait for completion. */ - while (!verify_state.completed) - g_main_context_iteration(NULL, TRUE); + /* VerifyStatus signals are processing, wait for completion. */ + while (!verify_state.completed) + g_main_context_iteration (NULL, TRUE); - g_signal_handlers_disconnect_by_func (dev, proxy_signal_cb, - &verify_state); + g_signal_handlers_disconnect_by_func (dev, proxy_signal_cb, + &verify_state); - if (!fprint_dbus_device_call_verify_stop_sync (dev, NULL, &error)) { - g_print("VerifyStop failed: %s\n", error->message); - exit (1); - } + if (!fprint_dbus_device_call_verify_stop_sync (dev, NULL, &error)) + { + g_print ("VerifyStop failed: %s\n", error->message); + exit (1); + } } -static void release_device (FprintDBusDevice *dev) +static void +release_device (FprintDBusDevice *dev) { - g_autoptr(GError) error = NULL; - if (!fprint_dbus_device_call_release_sync (dev, NULL, &error)) { - g_print("ReleaseDevice failed: %s\n", error->message); - exit (1); - } + g_autoptr(GError) error = NULL; + if (!fprint_dbus_device_call_release_sync (dev, NULL, &error)) + { + g_print ("ReleaseDevice failed: %s\n", error->message); + exit (1); + } } static const GOptionEntry entries[] = { - { "finger", 'f', 0, G_OPTION_ARG_STRING, &finger_name, "Finger selected to verify (default is automatic)", NULL }, - {"g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, "Make all warnings fatal", NULL}, - { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &usernames, NULL, "[username]" }, - { NULL } + { "finger", 'f', 0, G_OPTION_ARG_STRING, &finger_name, "Finger selected to verify (default is automatic)", NULL }, + {"g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, "Make all warnings fatal", NULL}, + { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &usernames, NULL, "[username]" }, + { NULL } }; -int main(int argc, char **argv) +int +main (int argc, char **argv) { - g_autoptr(FprintDBusDevice) dev = NULL; - g_autoptr(GError) err = NULL; - GOptionContext *context; - const char *username = NULL; + g_autoptr(FprintDBusDevice) dev = NULL; + g_autoptr(GError) err = NULL; + GOptionContext *context; + const char *username = NULL; - setlocale (LC_ALL, ""); + setlocale (LC_ALL, ""); - context = g_option_context_new ("Verify a fingerprint"); - g_option_context_add_main_entries (context, entries, NULL); + context = g_option_context_new ("Verify a fingerprint"); + g_option_context_add_main_entries (context, entries, NULL); - if (g_option_context_parse (context, &argc, &argv, &err) == FALSE) { - g_print ("couldn't parse command-line options: %s\n", err->message); - return 1; - } + if (g_option_context_parse (context, &argc, &argv, &err) == FALSE) + { + g_print ("couldn't parse command-line options: %s\n", err->message); + return 1; + } - if (usernames == NULL) { - username = ""; - } else { - username = usernames[0]; - } + if (usernames == NULL) + username = ""; + else + username = usernames[0]; - if (g_fatal_warnings) { - GLogLevelFlags fatal_mask; + if (g_fatal_warnings) + { + GLogLevelFlags fatal_mask; - fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); - fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; - g_log_set_always_fatal (fatal_mask); - } + fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); + fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; + g_log_set_always_fatal (fatal_mask); + } - create_manager(); + create_manager (); - dev = open_device(username); - find_finger(dev, username); - do_verify(dev); - release_device(dev); - return 0; + dev = open_device (username); + find_finger (dev, username); + do_verify (dev); + release_device (dev); + return 0; } -