1 /* kbnode.c - keyblock node utility functions
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2005, 2010 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
28 #include "../common/init.h"
32 #define USE_UNUSED_NODES 1
34 static int cleanup_registered;
35 static KBNODE unused_nodes;
38 release_unused_nodes (void)
43 kbnode_t next = unused_nodes->next;
47 #endif /*USE_UNUSED_NODES*/
58 unused_nodes = n->next;
61 if (!cleanup_registered)
63 cleanup_registered = 1;
64 register_mem_cleanup_func (release_unused_nodes);
66 n = xmalloc (sizeof *n);
82 n->next = unused_nodes;
93 new_kbnode( PACKET *pkt )
95 KBNODE n = alloc_node();
102 clone_kbnode( KBNODE node )
104 KBNODE n = alloc_node();
107 n->private_flag = node->private_flag | 2; /* mark cloned */
113 release_kbnode( KBNODE n )
119 if( !is_cloned_kbnode(n) ) {
120 free_packet( n->pkt );
131 * Note: This only works with walk_kbnode!!
134 delete_kbnode( KBNODE node )
136 node->private_flag |= 1;
140 * Append NODE to ROOT. ROOT must exist!
143 add_kbnode( KBNODE root, KBNODE node )
147 for(n1=root; n1->next; n1 = n1->next)
153 * Insert NODE into the list after root but before a packet which is not of
155 * (only if PKTTYPE != 0)
158 insert_kbnode( KBNODE root, KBNODE node, int pkttype )
161 node->next = root->next;
167 for(n1=root; n1->next; n1 = n1->next)
168 if( pkttype != n1->next->pkt->pkttype ) {
169 node->next = n1->next;
173 /* no such packet, append */
181 * Find the previous node (if PKTTYPE = 0) or the previous node
182 * with pkttype PKTTYPE in the list starting with ROOT of NODE.
185 find_prev_kbnode( KBNODE root, KBNODE node, int pkttype )
189 for (n1=NULL; root && root != node; root = root->next ) {
190 if (!pkttype ||root->pkt->pkttype == pkttype)
197 * Ditto, but find the next packet. The behaviour is trivial if
198 * PKTTYPE is 0 but if it is specified, the next node with a packet
199 * of this type is returned. The function has some knowledge about
200 * the valid ordering of packets: e.g. if the next signature packet
201 * is requested, the function will not return one if it encounters
205 find_next_kbnode( KBNODE node, int pkttype )
207 for( node=node->next ; node; node = node->next ) {
210 else if( pkttype == PKT_USER_ID
211 && ( node->pkt->pkttype == PKT_PUBLIC_KEY
212 || node->pkt->pkttype == PKT_SECRET_KEY ) )
214 else if( pkttype == PKT_SIGNATURE
215 && ( node->pkt->pkttype == PKT_USER_ID
216 || node->pkt->pkttype == PKT_PUBLIC_KEY
217 || node->pkt->pkttype == PKT_SECRET_KEY ) )
219 else if( node->pkt->pkttype == pkttype )
227 find_kbnode( KBNODE node, int pkttype )
229 for( ; node; node = node->next ) {
230 if( node->pkt->pkttype == pkttype )
239 * Walk through a list of kbnodes. This function returns
240 * the next kbnode for each call; before using the function the first
241 * time, the caller must set CONTEXT to NULL (This has simply the effect
242 * to start with ROOT).
245 walk_kbnode( KBNODE root, KBNODE *context, int all )
255 n = (*context)->next;
258 } while( !all && n && is_deleted_kbnode(n) );
264 clear_kbnode_flags( KBNODE n )
266 for( ; n; n = n->next ) {
273 * Commit changes made to the kblist at ROOT. Note that ROOT my change,
274 * and it is therefore passed by reference.
275 * The function has the effect of removing all nodes marked as deleted.
276 * returns true if any node has been changed
279 commit_kbnode( KBNODE *root )
284 for( n = *root, nl=NULL; n; n = nl->next ) {
285 if( is_deleted_kbnode(n) ) {
287 *root = nl = n->next;
290 if( !is_cloned_kbnode(n) ) {
291 free_packet( n->pkt );
304 remove_kbnode( KBNODE *root, KBNODE node )
308 for( n = *root, nl=NULL; n; n = nl->next ) {
311 *root = nl = n->next;
314 if( !is_cloned_kbnode(n) ) {
315 free_packet( n->pkt );
327 * Move NODE behind right after WHERE or to the beginning if WHERE is NULL.
330 move_kbnode( KBNODE *root, KBNODE node, KBNODE where )
334 if( !root || !*root || !node )
335 return; /* sanity check */
336 for( prev = *root; prev && prev->next != node; prev = prev->next )
339 return; /* node is not in the list */
341 if( !where ) { /* move node before root */
342 if( node == *root ) /* move to itself */
344 prev->next = node->next;
349 /* move it after where */
353 node->next = where->next;
362 dump_kbnode (KBNODE node)
364 for (; node; node = node->next )
367 switch (node->pkt->pkttype)
369 case 0: s="empty"; break;
370 case PKT_PUBLIC_KEY: s="public-key"; break;
371 case PKT_SECRET_KEY: s="secret-key"; break;
372 case PKT_SECRET_SUBKEY: s= "secret-subkey"; break;
373 case PKT_PUBKEY_ENC: s="public-enc"; break;
374 case PKT_SIGNATURE: s="signature"; break;
375 case PKT_ONEPASS_SIG: s="onepass-sig"; break;
376 case PKT_USER_ID: s="user-id"; break;
377 case PKT_PUBLIC_SUBKEY: s="public-subkey"; break;
378 case PKT_COMMENT: s="comment"; break;
379 case PKT_RING_TRUST: s="trust"; break;
380 case PKT_PLAINTEXT: s="plaintext"; break;
381 case PKT_COMPRESSED: s="compressed"; break;
382 case PKT_ENCRYPTED: s="encrypted"; break;
383 case PKT_GPG_CONTROL: s="gpg-control"; break;
384 default: s="unknown"; break;
386 log_debug ("node %p %02x/%02x type=%s",
387 node, node->flag, node->private_flag, s);
388 if (node->pkt->pkttype == PKT_USER_ID)
390 PKT_user_id *uid = node->pkt->pkt.user_id;
392 es_write_sanitized (log_get_stream (), uid->name, uid->len,
394 log_printf ("\" %c%c%c%c\n",
395 uid->is_expired? 'e':'.',
396 uid->is_revoked? 'r':'.',
397 uid->created? 'v':'.',
398 uid->is_primary? 'p':'.' );
400 else if (node->pkt->pkttype == PKT_SIGNATURE)
402 log_printf (" class=%02x keyid=%08lX ts=%lu\n",
403 node->pkt->pkt.signature->sig_class,
404 (ulong)node->pkt->pkt.signature->keyid[1],
405 (ulong)node->pkt->pkt.signature->timestamp);
407 else if (node->pkt->pkttype == PKT_GPG_CONTROL)
409 log_printf (" ctrl=%d len=%u\n",
410 node->pkt->pkt.gpg_control->control,
411 (unsigned int)node->pkt->pkt.gpg_control->datalen);
413 else if (node->pkt->pkttype == PKT_PUBLIC_KEY
414 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
416 PKT_public_key *pk = node->pkt->pkt.public_key;
418 log_printf (" keyid=%08lX a=%d u=%d %c%c%c%c\n",
419 (ulong)keyid_from_pk( pk, NULL ),
420 pk->pubkey_algo, pk->pubkey_usage,
421 pk->has_expired? 'e':'.',
422 pk->flags.revoked? 'r':'.',
423 pk->flags.valid? 'v':'.',
424 pk->flags.mdc? 'm':'.');