Browse Source

Device mount/umount support

Andrea Luzzardi 18 years ago
parent
commit
6bbc5accfd
5 changed files with 192 additions and 51 deletions
  1. +1
    -0
      src/Makefile
  2. +0
    -1
      src/log.c
  3. +3
    -50
      src/otp.c
  4. +163
    -0
      src/volume.c
  5. +25
    -0
      src/volume.h

+ 1
- 0
src/Makefile View File

@ -4,6 +4,7 @@ SRC = test.c \
xpath.c \
hal.c \
otp.c \
volume.c \
device.c
OBJ = $(SRC:.c=.o)
NAME = test


+ 0
- 1
src/log.c View File

@ -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);


+ 3
- 50
src/otp.c View File

@ -25,46 +25,9 @@
#include <libhal-storage.h>
#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);
}

+ 163
- 0
src/volume.c View File

@ -0,0 +1,163 @@
/*
* Copyright (c) 2003-2006 Andrea Luzzardi <scox@sig11.org>
*
* 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 <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <libhal-storage.h>
#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);
}

+ 25
- 0
src/volume.h View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2003-2006 Andrea Luzzardi <scox@sig11.org>
*
* 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_ */

Loading…
Cancel
Save