2008-01-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / kbx / keybox-init.c
blobfcf3c7cee8c4e76ada8de74bc8adabd95effa46b
1 /* keybox-init.c - Initalization of the library
2 * Copyright (C) 2001 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <assert.h>
27 #include "../jnlib/mischelp.h"
28 #include "keybox-defs.h"
30 static KB_NAME kb_names;
33 /*
34 Register a filename for plain keybox files. Returns a pointer to be
35 used to create a handles etc or NULL to indicate that it has already
36 been registered */
37 void *
38 keybox_register_file (const char *fname, int secret)
40 KB_NAME kr;
42 for (kr=kb_names; kr; kr = kr->next)
44 if (same_file_p (kr->fname, fname) )
45 return NULL; /* Already registered. */
48 kr = xtrymalloc (sizeof *kr + strlen (fname));
49 if (!kr)
50 return NULL;
51 strcpy (kr->fname, fname);
52 kr->secret = !!secret;
53 /* kr->lockhd = NULL;*/
54 kr->is_locked = 0;
55 kr->did_full_scan = 0;
56 /* keep a list of all issued pointers */
57 kr->next = kb_names;
58 kb_names = kr;
60 /* create the offset table the first time a function here is used */
61 /* if (!kb_offtbl) */
62 /* kb_offtbl = new_offset_hash_table (); */
64 return kr;
67 int
68 keybox_is_writable (void *token)
70 KB_NAME r = token;
72 return r? !access (r->fname, W_OK) : 0;
77 /* Create a new handle for the resource associated with TOKEN. SECRET
78 is just a cross-check.
80 The returned handle must be released using keybox_release (). */
81 KEYBOX_HANDLE
82 keybox_new (void *token, int secret)
84 KEYBOX_HANDLE hd;
85 KB_NAME resource = token;
87 assert (resource && !resource->secret == !secret);
88 hd = xtrycalloc (1, sizeof *hd);
89 if (hd)
91 hd->kb = resource;
92 hd->secret = !!secret;
94 return hd;
97 void
98 keybox_release (KEYBOX_HANDLE hd)
100 if (!hd)
101 return;
102 _keybox_release_blob (hd->found.blob);
103 if (hd->fp)
105 fclose (hd->fp);
106 hd->fp = NULL;
108 xfree (hd->word_match.name);
109 xfree (hd->word_match.pattern);
110 xfree (hd);
114 const char *
115 keybox_get_resource_name (KEYBOX_HANDLE hd)
117 if (!hd || !hd->kb)
118 return NULL;
119 return hd->kb->fname;
123 keybox_set_ephemeral (KEYBOX_HANDLE hd, int yes)
125 if (!hd)
126 return gpg_error (GPG_ERR_INV_HANDLE);
127 hd->ephemeral = yes;
128 return 0;