2011-02-04 12:57:53 +01:00
|
|
|
/* keybox-init.c - Initalization of the library
|
2003-06-05 07:14:21 +00:00
|
|
|
* Copyright (C) 2001 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 19:49:40 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 07:14:21 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG 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
|
2007-07-04 19:49:40 +00:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-06-05 07:14:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2010-03-10 12:24:58 +00:00
|
|
|
#include "../common/mischelp.h"
|
2003-06-05 07:14:21 +00:00
|
|
|
#include "keybox-defs.h"
|
|
|
|
|
|
|
|
static KB_NAME kb_names;
|
|
|
|
|
|
|
|
|
2008-04-01 15:08:57 +00:00
|
|
|
/* Register a filename for plain keybox files. Returns a pointer to
|
|
|
|
be used to create a handles and so on. Returns NULL to indicate
|
|
|
|
that FNAME has already been registered. */
|
2003-06-05 07:14:21 +00:00
|
|
|
void *
|
|
|
|
keybox_register_file (const char *fname, int secret)
|
|
|
|
{
|
|
|
|
KB_NAME kr;
|
|
|
|
|
|
|
|
for (kr=kb_names; kr; kr = kr->next)
|
|
|
|
{
|
2007-08-24 09:34:39 +00:00
|
|
|
if (same_file_p (kr->fname, fname) )
|
|
|
|
return NULL; /* Already registered. */
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kr = xtrymalloc (sizeof *kr + strlen (fname));
|
|
|
|
if (!kr)
|
|
|
|
return NULL;
|
|
|
|
strcpy (kr->fname, fname);
|
|
|
|
kr->secret = !!secret;
|
2008-04-01 15:08:57 +00:00
|
|
|
|
|
|
|
kr->handle_table = NULL;
|
|
|
|
kr->handle_table_size = 0;
|
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
/* kr->lockhd = NULL;*/
|
|
|
|
kr->is_locked = 0;
|
|
|
|
kr->did_full_scan = 0;
|
|
|
|
/* keep a list of all issued pointers */
|
|
|
|
kr->next = kb_names;
|
|
|
|
kb_names = kr;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
/* create the offset table the first time a function here is used */
|
|
|
|
/* if (!kb_offtbl) */
|
|
|
|
/* kb_offtbl = new_offset_hash_table (); */
|
|
|
|
|
|
|
|
return kr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
keybox_is_writable (void *token)
|
|
|
|
{
|
|
|
|
KB_NAME r = token;
|
|
|
|
|
|
|
|
return r? !access (r->fname, W_OK) : 0;
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
/* Create a new handle for the resource associated with TOKEN. SECRET
|
|
|
|
is just a cross-check.
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 07:14:21 +00:00
|
|
|
The returned handle must be released using keybox_release (). */
|
|
|
|
KEYBOX_HANDLE
|
|
|
|
keybox_new (void *token, int secret)
|
|
|
|
{
|
|
|
|
KEYBOX_HANDLE hd;
|
|
|
|
KB_NAME resource = token;
|
2008-04-01 15:08:57 +00:00
|
|
|
int idx;
|
2003-06-05 07:14:21 +00:00
|
|
|
|
|
|
|
assert (resource && !resource->secret == !secret);
|
|
|
|
hd = xtrycalloc (1, sizeof *hd);
|
|
|
|
if (hd)
|
|
|
|
{
|
|
|
|
hd->kb = resource;
|
|
|
|
hd->secret = !!secret;
|
2008-04-01 15:08:57 +00:00
|
|
|
if (!resource->handle_table)
|
|
|
|
{
|
|
|
|
resource->handle_table_size = 3;
|
|
|
|
resource->handle_table = xtrycalloc (resource->handle_table_size,
|
|
|
|
sizeof *resource->handle_table);
|
|
|
|
if (!resource->handle_table)
|
|
|
|
{
|
|
|
|
resource->handle_table_size = 0;
|
|
|
|
xfree (hd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (idx=0; idx < resource->handle_table_size; idx++)
|
|
|
|
if (!resource->handle_table[idx])
|
|
|
|
{
|
|
|
|
resource->handle_table[idx] = hd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!(idx < resource->handle_table_size))
|
|
|
|
{
|
|
|
|
KEYBOX_HANDLE *tmptbl;
|
|
|
|
size_t newsize;
|
|
|
|
|
|
|
|
newsize = resource->handle_table_size + 5;
|
2011-02-04 12:57:53 +01:00
|
|
|
tmptbl = xtryrealloc (resource->handle_table,
|
2008-04-01 15:08:57 +00:00
|
|
|
newsize * sizeof (*tmptbl));
|
|
|
|
if (!tmptbl)
|
|
|
|
{
|
|
|
|
xfree (hd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
resource->handle_table = tmptbl;
|
|
|
|
resource->handle_table_size = newsize;
|
|
|
|
resource->handle_table[idx] = hd;
|
|
|
|
for (idx++; idx < resource->handle_table_size; idx++)
|
|
|
|
resource->handle_table[idx] = NULL;
|
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
}
|
|
|
|
return hd;
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
void
|
2003-06-05 07:14:21 +00:00
|
|
|
keybox_release (KEYBOX_HANDLE hd)
|
|
|
|
{
|
|
|
|
if (!hd)
|
|
|
|
return;
|
2008-04-01 15:08:57 +00:00
|
|
|
if (hd->kb->handle_table)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
for (idx=0; idx < hd->kb->handle_table_size; idx++)
|
|
|
|
if (hd->kb->handle_table[idx] == hd)
|
|
|
|
hd->kb->handle_table[idx] = NULL;
|
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
_keybox_release_blob (hd->found.blob);
|
2004-12-07 18:07:30 +00:00
|
|
|
if (hd->fp)
|
|
|
|
{
|
|
|
|
fclose (hd->fp);
|
|
|
|
hd->fp = NULL;
|
|
|
|
}
|
2003-06-05 07:14:21 +00:00
|
|
|
xfree (hd->word_match.name);
|
|
|
|
xfree (hd->word_match.pattern);
|
|
|
|
xfree (hd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
keybox_get_resource_name (KEYBOX_HANDLE hd)
|
|
|
|
{
|
|
|
|
if (!hd || !hd->kb)
|
|
|
|
return NULL;
|
|
|
|
return hd->kb->fname;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
keybox_set_ephemeral (KEYBOX_HANDLE hd, int yes)
|
|
|
|
{
|
|
|
|
if (!hd)
|
2011-02-04 12:57:53 +01:00
|
|
|
return gpg_error (GPG_ERR_INV_HANDLE);
|
2003-06-05 07:14:21 +00:00
|
|
|
hd->ephemeral = yes;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-01 15:08:57 +00:00
|
|
|
|
|
|
|
/* Close the file of the resource identified by HD. For consistent
|
|
|
|
results this fucntion closes the files of all handles pointing to
|
|
|
|
the resource identified by HD. */
|
2011-02-04 12:57:53 +01:00
|
|
|
void
|
2008-04-01 15:08:57 +00:00
|
|
|
_keybox_close_file (KEYBOX_HANDLE hd)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
KEYBOX_HANDLE roverhd;
|
|
|
|
|
|
|
|
if (!hd || !hd->kb || !hd->kb->handle_table)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (idx=0; idx < hd->kb->handle_table_size; idx++)
|
|
|
|
if ((roverhd = hd->kb->handle_table[idx]))
|
|
|
|
{
|
|
|
|
if (roverhd->fp)
|
|
|
|
{
|
|
|
|
fclose (roverhd->fp);
|
|
|
|
roverhd->fp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert (!hd->fp);
|
|
|
|
}
|
2012-12-27 15:04:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock the keybox at handle HD, or unlock if YES is false. Note that
|
|
|
|
* we currently ignore the handle and lock all registered keyboxes.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
keybox_lock (KEYBOX_HANDLE hd, int yes)
|
|
|
|
{
|
|
|
|
/* FIXME: We need to implement it before we can use it with gpg.
|
|
|
|
gpgsm does the locking in its local keydb.c driver; this should
|
|
|
|
be changed as well. */
|
|
|
|
|
|
|
|
(void)hd;
|
|
|
|
(void)yes;
|
|
|
|
return 0;
|
|
|
|
}
|