Browse Source
This patch introduces the notion of a "scheme module". Such a module provides a "get_file" function to retrieve the contents of a file located at a specified URI. Currently available modules: - the "file" module, supporting the "file://" URI scheme; - the "libmtp" module, using the LibMTP library to support the "mtp://' scheme; - the "gio" module, using the GLib-GIO library to support the "uuid://" and "label://" schemes.master
10 changed files with 331 additions and 167 deletions
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* gfsecret - Secret sharing tools |
||||
* Copyright (C) 2016 Damien Goutte-Gattat |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#ifdef HAVE_CONFIG_H |
||||
#include <config.h> |
||||
#endif |
||||
|
||||
#include "scheme-file.h" |
||||
#include "util.h" |
||||
|
||||
static int |
||||
gfsec_scheme_file_get_file(gfsec_scheme_t scheme, |
||||
const char *authority, |
||||
const char *path, |
||||
unsigned char **buffer, |
||||
size_t *len) |
||||
{ |
||||
(void) authority; |
||||
|
||||
if ( scheme != GFSEC_SCHEME_FILE ) |
||||
return GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME; |
||||
|
||||
if ( (*buffer = read_file(path, len, GFSEC_SECRET_MAX_SIZE)) ) { |
||||
return GFSEC_SCHEME_STATUS_SUCCESS; |
||||
} |
||||
else |
||||
return GFSEC_SCHEME_STATUS_ERROR; |
||||
} |
||||
|
||||
gfsec_scheme_module_t |
||||
gfsec_scheme_file_init(void) |
||||
{ |
||||
gfsec_scheme_module_t module; |
||||
|
||||
module.get_file = gfsec_scheme_file_get_file; |
||||
|
||||
return module; |
||||
} |
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* gfsecret - Secret sharing tools |
||||
* Copyright (C) 2016 Damien Goutte-Gattat |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#ifndef ICP20160821_SCHEME_FILE_H |
||||
#define ICP20160821_SCHEME_FILE_H |
||||
|
||||
#include "scheme-module.h" |
||||
|
||||
#ifdef __cpluscplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
gfsec_scheme_module_t |
||||
gfsec_scheme_file_init(void); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* !ICP20160821_SCHEME-FILE_H */ |
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* gfsecret - Secret sharing tools |
||||
* Copyright (C) 2016 Damien Goutte-Gattat |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#ifdef HAVE_CONFIG_H |
||||
#include <config.h> |
||||
#endif |
||||
|
||||
#include "scheme-module.h" |
||||
#include "scheme-file.h" |
||||
#include "scheme-libmtp.h" |
||||
#include "scheme-gio.h" |
||||
|
||||
static gfsec_scheme_module_t *modules; |
||||
|
||||
/**
|
||||
* Initializes the module system. |
||||
* This function should be called once, before any other |
||||
* function from this module. |
||||
*/ |
||||
void |
||||
gfsec_scheme_module_init(void) |
||||
{ |
||||
static gfsec_scheme_module_t mods[4]; |
||||
gfsec_scheme_module_t dummy = { NULL }; |
||||
int n = 0; |
||||
|
||||
mods[n++] = gfsec_scheme_file_init(); |
||||
#ifdef HAVE_LIBMTP |
||||
mods[n++] = gfsec_scheme_libmtp_init(); |
||||
#endif |
||||
#ifdef HAVE_GIO |
||||
mods[n++] = gfsec_scheme_gio_init(); |
||||
#endif |
||||
|
||||
mods[n++] = dummy; |
||||
|
||||
modules = mods; |
||||
} |
||||
|
||||
/**
|
||||
* Attempts to retrieve a file's contents. |
||||
* This function will successively ask all available modules |
||||
* to retrieve the file at the specified location. |
||||
* |
||||
* @param scheme[in] The URI scheme. |
||||
* @param authority[in] The URI authority part. |
||||
* @param path[in] The URI path. |
||||
* @param buffer[out] A pointer to a buffer to store the file |
||||
* contents; will be automatically allocated. |
||||
* @param len[out] The address where the count of retrieved |
||||
* bytes will be stored. |
||||
* |
||||
* @return |
||||
* - GFSEC_SCHEME_STATUS_SUCCESS if successful; |
||||
* - GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME if no module is |
||||
* available to handle the URI scheme; |
||||
* - GFSEC_SCHEME_STATUS_ERROR if a module supported the |
||||
* URI scheme but an error occured during retrieval. |
||||
*/ |
||||
int |
||||
gfsec_scheme_module_get_file(gfsec_scheme_t scheme, |
||||
const char *authority, |
||||
const char *path, |
||||
unsigned char **buffer, |
||||
size_t *len) |
||||
{ |
||||
gfsec_scheme_module_t *mod = modules; |
||||
int rc; |
||||
|
||||
while ( mod->get_file != NULL ) { |
||||
rc = mod->get_file(scheme, authority, path, buffer, len); |
||||
|
||||
if ( rc != GFSEC_SCHEME_STATUS_SUCCESS ) |
||||
mod += 1; |
||||
else |
||||
break; |
||||
} |
||||
|
||||
return rc; |
||||
} |
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* gfsecret - Secret sharing tools |
||||
* Copyright (C) 2016 Damien Goutte-Gattat |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 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, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
|
||||
#ifndef ICP20160820_SCHEME_MODULE_H |
||||
#define ICP20160820_SCHEME_MODULE_H |
||||
|
||||
#include <stdlib.h> |
||||
|
||||
#include "schemes.h" |
||||
|
||||
typedef enum gfsec_scheme_status { |
||||
GFSEC_SCHEME_STATUS_SUCCESS = 0, |
||||
GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME = -1, |
||||
GFSEC_SCHEME_STATUS_ERROR = -2 |
||||
} gfsec_scheme_status_t; |
||||
|
||||
|
||||
typedef int (*gfsec_scheme_get_file)(gfsec_scheme_t, |
||||
const char *, |
||||
const char *, |
||||
unsigned char **, |
||||
size_t *); |
||||
|
||||
typedef struct gfsec_scheme_module { |
||||
gfsec_scheme_get_file get_file; |
||||
} gfsec_scheme_module_t; |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
void |
||||
gfsec_scheme_module_init(void); |
||||
|
||||
int |
||||
gfsec_scheme_module_get_file(gfsec_scheme_t, |
||||
const char *, |
||||
const char *, |
||||
unsigned char **, |
||||
size_t *); |
||||
|
||||
#ifdef __cpluscplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* !ICP20160820_SCHEME_MODULE_H */ |
Loading…
Reference in new issue