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/>.
27 #include "../jnlib/mischelp.h"
28 #include "keybox-defs.h"
30 static KB_NAME kb_names
;
33 /* Register a filename for plain keybox files. Returns a pointer to
34 be used to create a handles and so on. Returns NULL to indicate
35 that FNAME has already been registered. */
37 keybox_register_file (const char *fname
, int secret
)
41 for (kr
=kb_names
; kr
; kr
= kr
->next
)
43 if (same_file_p (kr
->fname
, fname
) )
44 return NULL
; /* Already registered. */
47 kr
= xtrymalloc (sizeof *kr
+ strlen (fname
));
50 strcpy (kr
->fname
, fname
);
51 kr
->secret
= !!secret
;
53 kr
->handle_table
= NULL
;
54 kr
->handle_table_size
= 0;
56 /* kr->lockhd = NULL;*/
58 kr
->did_full_scan
= 0;
59 /* keep a list of all issued pointers */
63 /* create the offset table the first time a function here is used */
65 /* kb_offtbl = new_offset_hash_table (); */
71 keybox_is_writable (void *token
)
75 return r
? !access (r
->fname
, W_OK
) : 0;
80 /* Create a new handle for the resource associated with TOKEN. SECRET
81 is just a cross-check.
83 The returned handle must be released using keybox_release (). */
85 keybox_new (void *token
, int secret
)
88 KB_NAME resource
= token
;
91 assert (resource
&& !resource
->secret
== !secret
);
92 hd
= xtrycalloc (1, sizeof *hd
);
96 hd
->secret
= !!secret
;
97 if (!resource
->handle_table
)
99 resource
->handle_table_size
= 3;
100 resource
->handle_table
= xtrycalloc (resource
->handle_table_size
,
101 sizeof *resource
->handle_table
);
102 if (!resource
->handle_table
)
104 resource
->handle_table_size
= 0;
109 for (idx
=0; idx
< resource
->handle_table_size
; idx
++)
110 if (!resource
->handle_table
[idx
])
112 resource
->handle_table
[idx
] = hd
;
115 if (!(idx
< resource
->handle_table_size
))
117 KEYBOX_HANDLE
*tmptbl
;
120 newsize
= resource
->handle_table_size
+ 5;
121 tmptbl
= xtryrealloc (resource
->handle_table
,
122 newsize
* sizeof (*tmptbl
));
128 resource
->handle_table
= tmptbl
;
129 resource
->handle_table_size
= newsize
;
130 resource
->handle_table
[idx
] = hd
;
131 for (idx
++; idx
< resource
->handle_table_size
; idx
++)
132 resource
->handle_table
[idx
] = NULL
;
139 keybox_release (KEYBOX_HANDLE hd
)
143 if (hd
->kb
->handle_table
)
146 for (idx
=0; idx
< hd
->kb
->handle_table_size
; idx
++)
147 if (hd
->kb
->handle_table
[idx
] == hd
)
148 hd
->kb
->handle_table
[idx
] = NULL
;
150 _keybox_release_blob (hd
->found
.blob
);
156 xfree (hd
->word_match
.name
);
157 xfree (hd
->word_match
.pattern
);
163 keybox_get_resource_name (KEYBOX_HANDLE hd
)
167 return hd
->kb
->fname
;
171 keybox_set_ephemeral (KEYBOX_HANDLE hd
, int yes
)
174 return gpg_error (GPG_ERR_INV_HANDLE
);
180 /* Close the file of the resource identified by HD. For consistent
181 results this fucntion closes the files of all handles pointing to
182 the resource identified by HD. */
184 _keybox_close_file (KEYBOX_HANDLE hd
)
187 KEYBOX_HANDLE roverhd
;
189 if (!hd
|| !hd
->kb
|| !hd
->kb
->handle_table
)
192 for (idx
=0; idx
< hd
->kb
->handle_table_size
; idx
++)
193 if ((roverhd
= hd
->kb
->handle_table
[idx
]))
197 fclose (roverhd
->fp
);