1 /* keybox-init.c - Initialization 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 <https://www.gnu.org/licenses/>.
27 #include "keybox-defs.h"
28 #include "../common/mischelp.h"
30 static KB_NAME kb_names;
33 /* Register a filename for plain keybox files. Returns 0 on success,
34 * GPG_ERR_EEXIST if it has already been registered, or another error
35 * code. On success or with error code GPG_ERR_EEXIST a token usable
36 * to access the keybox handle is stored at R_TOKEN, NULL is stored
37 * for all other errors. */
39 keybox_register_file (const char *fname, int secret, void **r_token)
45 for (kr=kb_names; kr; kr = kr->next)
47 if (same_file_p (kr->fname, fname) )
50 return gpg_error (GPG_ERR_EEXIST); /* Already registered. */
54 kr = xtrymalloc (sizeof *kr + strlen (fname));
56 return gpg_error_from_syserror ();
57 strcpy (kr->fname, fname);
58 kr->secret = !!secret;
60 kr->handle_table = NULL;
61 kr->handle_table_size = 0;
65 kr->did_full_scan = 0;
66 /* keep a list of all issued pointers */
70 /* create the offset table the first time a function here is used */
72 /* kb_offtbl = new_offset_hash_table (); */
79 keybox_is_writable (void *token)
83 return r? !access (r->fname, W_OK) : 0;
89 do_keybox_new (KB_NAME resource, int secret, int for_openpgp)
94 assert (resource && !resource->secret == !secret);
95 hd = xtrycalloc (1, sizeof *hd);
99 hd->secret = !!secret;
100 hd->for_openpgp = for_openpgp;
101 if (!resource->handle_table)
103 resource->handle_table_size = 3;
104 resource->handle_table = xtrycalloc (resource->handle_table_size,
105 sizeof *resource->handle_table);
106 if (!resource->handle_table)
108 resource->handle_table_size = 0;
113 for (idx=0; idx < resource->handle_table_size; idx++)
114 if (!resource->handle_table[idx])
116 resource->handle_table[idx] = hd;
119 if (!(idx < resource->handle_table_size))
121 KEYBOX_HANDLE *tmptbl;
124 newsize = resource->handle_table_size + 5;
125 tmptbl = xtryrealloc (resource->handle_table,
126 newsize * sizeof (*tmptbl));
132 resource->handle_table = tmptbl;
133 resource->handle_table_size = newsize;
134 resource->handle_table[idx] = hd;
135 for (idx++; idx < resource->handle_table_size; idx++)
136 resource->handle_table[idx] = NULL;
143 /* Create a new handle for the resource associated with TOKEN. SECRET
144 is just a cross-check. This is the OpenPGP version. The returned
145 handle must be released using keybox_release. */
147 keybox_new_openpgp (void *token, int secret)
149 KB_NAME resource = token;
151 return do_keybox_new (resource, secret, 1);
154 /* Create a new handle for the resource associated with TOKEN. SECRET
155 is just a cross-check. This is the X.509 version. The returned
156 handle must be released using keybox_release. */
158 keybox_new_x509 (void *token, int secret)
160 KB_NAME resource = token;
162 return do_keybox_new (resource, secret, 0);
167 keybox_release (KEYBOX_HANDLE hd)
171 if (hd->kb->handle_table)
174 for (idx=0; idx < hd->kb->handle_table_size; idx++)
175 if (hd->kb->handle_table[idx] == hd)
176 hd->kb->handle_table[idx] = NULL;
178 _keybox_release_blob (hd->found.blob);
179 _keybox_release_blob (hd->saved_found.blob);
185 xfree (hd->word_match.name);
186 xfree (hd->word_match.pattern);
191 /* Save the current found state in HD for later retrieval by
192 keybox_restore_found_state. Only one state may be saved. */
194 keybox_push_found_state (KEYBOX_HANDLE hd)
196 if (hd->saved_found.blob)
198 _keybox_release_blob (hd->saved_found.blob);
199 hd->saved_found.blob = NULL;
201 hd->saved_found = hd->found;
202 hd->found.blob = NULL;
206 /* Restore the saved found state in HD. */
208 keybox_pop_found_state (KEYBOX_HANDLE hd)
212 _keybox_release_blob (hd->found.blob);
213 hd->found.blob = NULL;
215 hd->found = hd->saved_found;
216 hd->saved_found.blob = NULL;
221 keybox_get_resource_name (KEYBOX_HANDLE hd)
225 return hd->kb->fname;
229 keybox_set_ephemeral (KEYBOX_HANDLE hd, int yes)
232 return gpg_error (GPG_ERR_INV_HANDLE);
238 /* Close the file of the resource identified by HD. For consistent
239 results this function closes the files of all handles pointing to
240 the resource identified by HD. */
242 _keybox_close_file (KEYBOX_HANDLE hd)
245 KEYBOX_HANDLE roverhd;
247 if (!hd || !hd->kb || !hd->kb->handle_table)
250 for (idx=0; idx < hd->kb->handle_table_size; idx++)
251 if ((roverhd = hd->kb->handle_table[idx]))
255 fclose (roverhd->fp);
264 * Lock the keybox at handle HD, or unlock if YES is false.
267 keybox_lock (KEYBOX_HANDLE hd, int yes)
272 if (!keybox_is_writable (kb))
275 /* Make sure the lock handle has been created. */
278 kb->lockhd = dotlock_create (kb->fname, 0);
281 err = gpg_error_from_syserror ();
282 log_info ("can't allocate lock for '%s'\n", kb->fname );
287 if (yes) /* Take the lock. */
291 #ifdef HAVE_W32_SYSTEM
292 /* Under Windows we need to close the file before we try
293 * to lock it. This is because another process might have
294 * taken the lock and is using keybox_file_rename to
295 * rename the base file. How if our dotlock_take below is
296 * waiting for the lock but we have the base file still
297 * open, keybox_file_rename will never succeed as we are
304 #endif /*HAVE_W32_SYSTEM*/
305 if (dotlock_take (kb->lockhd, -1))
307 err = gpg_error_from_syserror ();
308 log_info ("can't lock '%s'\n", kb->fname );
314 else /* Release the lock. */
318 if (dotlock_release (kb->lockhd))
320 err = gpg_error_from_syserror ();
321 log_info ("can't unlock '%s'\n", kb->fname );