From 6bbc5accfd89220a570fdc891418ea3adcbb4dd1 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Sun, 8 Oct 2006 22:53:11 +0000 Subject: [PATCH] Device mount/umount support --- src/Makefile | 1 + src/log.c | 1 - src/otp.c | 53 +---------------- src/volume.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/volume.h | 25 ++++++++ 5 files changed, 192 insertions(+), 51 deletions(-) create mode 100644 src/volume.c create mode 100644 src/volume.h diff --git a/src/Makefile b/src/Makefile index 0ce71c8..7364d70 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,6 +4,7 @@ SRC = test.c \ xpath.c \ hal.c \ otp.c \ + volume.c \ device.c OBJ = $(SRC:.c=.o) NAME = test diff --git a/src/log.c b/src/log.c index 561045b..d51b32b 100644 --- a/src/log.c +++ b/src/log.c @@ -23,7 +23,6 @@ void __log_debug(const char *file, int line, const char *fmt, ...) { va_list ap; - return ; fprintf(stderr, "\033[01;34m*\033[00m [%s:%03d] ", file, line); va_start(ap, fmt); vfprintf(stderr, fmt, ap); diff --git a/src/otp.c b/src/otp.c index 3b3f073..9823c94 100644 --- a/src/otp.c +++ b/src/otp.c @@ -25,46 +25,9 @@ #include #include "conf.h" #include "log.h" +#include "volume.h" #include "otp.h" -static LibHalVolume *pusb_otp_find_volume(t_pusb_options *opts, LibHalContext *ctx, - LibHalDrive *drive) -{ - char **volumes; - int n_volumes = 0; - int i; - - volumes = libhal_drive_find_all_volumes(ctx, drive, &n_volumes); - if (!n_volumes) - { - libhal_free_string_array(volumes); - log_debug("No volumes found\n"); - return (NULL); - } - for (i = 0; i < n_volumes; ++i) - { - LibHalVolume *volume; - - volume = libhal_volume_from_udi(ctx, - volumes[i]); - if (!volume) - continue; - if (libhal_volume_should_ignore(volume)) - { - libhal_volume_free(volume); - continue; - } - if (libhal_volume_is_mounted(volume)) - { - libhal_free_string_array(volumes); - return (volume); - } - libhal_volume_free(volume); - } - libhal_free_string_array(volumes); - return (NULL); -} - static FILE *pusb_otp_open_device(t_pusb_options *opts, LibHalVolume *volume, const char *mode) { @@ -185,18 +148,8 @@ int pusb_otp_check(t_pusb_options *opts, LibHalContext *ctx, { LibHalVolume *volume = NULL; int retval; - int maxtries; - int i; - maxtries = ((opts->probe_timeout * 1000000) / 250000); - for (i = 0; i < maxtries; ++i) - { - log_debug("Waiting for volumes to come up...\n"); - volume = pusb_otp_find_volume(opts, ctx, drive); - if (volume) - break; - usleep(250000); - } + volume = pusb_volume_find(opts, ctx, drive); if (!volume) return (!opts->enforce_otp); retval = pusb_otp_compare(opts, volume); @@ -207,6 +160,6 @@ int pusb_otp_check(t_pusb_options *opts, LibHalContext *ctx, } else log_error("Pad checking failed !\n"); - libhal_volume_free(volume); + pusb_volume_destroy(volume); return (retval); } diff --git a/src/volume.c b/src/volume.c new file mode 100644 index 0000000..b56aa3f --- /dev/null +++ b/src/volume.c @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2003-2006 Andrea Luzzardi + * + * This file is part of the pam_usb project. pam_usb is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * pam_usb 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., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "conf.h" +#include "log.h" +#include "volume.h" + +static int pusb_volume_mount(t_pusb_options *opts, LibHalVolume **volume, + LibHalContext *ctx) +{ + char command[1024]; + char tempname[32]; + const char *devname; + const char *fs; + + snprintf(tempname, sizeof(tempname), "pam_usb%d", getpid()); + if (!(fs = libhal_volume_get_fstype(*volume))) + { + log_error("Unable to retrieve filesystem type\n"); + return (0); + } + if (!(devname = libhal_volume_get_device_file(*volume))) + { + log_error("Unable to retrieve device filename\n"); + return (0); + } + log_debug("Attempting to mount device %s with label %s (fs: %s)\n", + devname, tempname, fs); + snprintf(command, sizeof(command), "pmount -s -t %s %s %s", + fs, devname, tempname); + log_debug("Executing \"%s\"\n", command); + if (system(command) != 0) + { + log_error("Mount failed\n"); + return (0); + } + else + { + const char *udi; + + udi = libhal_volume_get_udi(*volume); + if (!udi) + { + log_error("Unable to retrieve volume UDI\n"); + return (0); + } + udi = strdup(udi); + libhal_volume_free(*volume); + *volume = libhal_volume_from_udi(ctx, udi); + free((char *)udi); + } + log_debug("Mount succeeded.\n"); + return (libhal_volume_is_mounted(*volume)); +} + +static int __pusb_volume_find(t_pusb_options *opts, LibHalContext *ctx, + LibHalDrive *drive, LibHalVolume **out) +{ + char **volumes; + int n_volumes = 0; + int i; + + *out = NULL; + volumes = libhal_drive_find_all_volumes(ctx, drive, &n_volumes); + if (!n_volumes) + { + libhal_free_string_array(volumes); + log_debug("No volumes found\n"); + return (1); + } + for (i = 0; i < n_volumes; ++i) + { + LibHalVolume *volume; + + volume = libhal_volume_from_udi(ctx, + volumes[i]); + if (!volume) + continue; + if (libhal_volume_should_ignore(volume)) + { + libhal_volume_free(volume); + continue; + } + *out = volume; + libhal_free_string_array(volumes); + if (libhal_volume_is_mounted(volume)) + { + log_debug("Volume is already mounted\n"); + return (1); + } + else + { + if (pusb_volume_mount(opts, &volume, ctx)) + return (1); + return (0); + } + libhal_volume_free(volume); + } + libhal_free_string_array(volumes); + return (1); +} + +LibHalVolume *pusb_volume_find(t_pusb_options *opts, LibHalContext *ctx, + LibHalDrive *drive) +{ + LibHalVolume *volume = NULL; + int maxtries = 0; + int i; + + maxtries = ((opts->probe_timeout * 1000000) / 250000); + for (i = 0; i < maxtries; ++i) + { + log_debug("Waiting for volumes to come up...\n"); + if (!__pusb_volume_find(opts, ctx, drive, &volume)) + return (NULL); + if (volume) + break; + usleep(250000); + } + return (volume); +} + +void pusb_volume_destroy(LibHalVolume *volume) +{ + const char *mntpoint; + + mntpoint = libhal_volume_get_mount_point(volume); + if (mntpoint && strstr(mntpoint, "pam_usb")) + { + char command[1024]; + + log_debug("Attempting to umount %s\n", + mntpoint); + snprintf(command, sizeof(command), "pumount %s", mntpoint); + if (!system(command)) + log_debug("Umount succeeded.\n"); + else + log_error("Unable to umount %s\n", mntpoint); + } + libhal_volume_free(volume); +} diff --git a/src/volume.h b/src/volume.h new file mode 100644 index 0000000..991bef4 --- /dev/null +++ b/src/volume.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2003-2006 Andrea Luzzardi + * + * This file is part of the pam_usb project. pam_usb is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * pam_usb 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., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef VOLUME_H_ +# define VOLUME_H_ + +LibHalVolume *pusb_volume_find(t_pusb_options *opts, LibHalContext *ctx, + LibHalDrive *drive); +void pusb_volume_destroy(LibHalVolume *volume); + +#endif /* !VOLUME_H_ */