* gpg.texi (GPG Configuration Options): Make http_proxy option
[gnupg.git] / g10 / kbnode.c
blobb0954645170487913b601ad66dfb4bf2a09a528d
1 /* kbnode.c - keyblock node utility functions
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2005 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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
29 #include "gpg.h"
30 #include "util.h"
31 #include "packet.h"
32 #include "keydb.h"
34 #define USE_UNUSED_NODES 1
36 static KBNODE unused_nodes;
38 static KBNODE
39 alloc_node(void)
41 KBNODE n;
43 n = unused_nodes;
44 if( n )
45 unused_nodes = n->next;
46 else
47 n = xmalloc( sizeof *n );
48 n->next = NULL;
49 n->pkt = NULL;
50 n->flag = 0;
51 n->private_flag=0;
52 n->recno = 0;
53 return n;
56 static void
57 free_node( KBNODE n )
59 if( n ) {
60 #if USE_UNUSED_NODES
61 n->next = unused_nodes;
62 unused_nodes = n;
63 #else
64 xfree( n );
65 #endif
71 KBNODE
72 new_kbnode( PACKET *pkt )
74 KBNODE n = alloc_node();
75 n->pkt = pkt;
76 return n;
80 KBNODE
81 clone_kbnode( KBNODE node )
83 KBNODE n = alloc_node();
85 n->pkt = node->pkt;
86 n->private_flag = node->private_flag | 2; /* mark cloned */
87 return n;
91 void
92 release_kbnode( KBNODE n )
94 KBNODE n2;
96 while( n ) {
97 n2 = n->next;
98 if( !is_cloned_kbnode(n) ) {
99 free_packet( n->pkt );
100 xfree( n->pkt );
102 free_node( n );
103 n = n2;
108 /****************
109 * Delete NODE.
110 * Note: This only works with walk_kbnode!!
112 void
113 delete_kbnode( KBNODE node )
115 node->private_flag |= 1;
118 /****************
119 * Append NODE to ROOT. ROOT must exist!
121 void
122 add_kbnode( KBNODE root, KBNODE node )
124 KBNODE n1;
126 for(n1=root; n1->next; n1 = n1->next)
128 n1->next = node;
131 /****************
132 * Insert NODE into the list after root but before a packet which is not of
133 * type PKTTYPE
134 * (only if PKTTYPE != 0)
136 void
137 insert_kbnode( KBNODE root, KBNODE node, int pkttype )
139 if( !pkttype ) {
140 node->next = root->next;
141 root->next = node;
143 else {
144 KBNODE n1;
146 for(n1=root; n1->next; n1 = n1->next)
147 if( pkttype != n1->next->pkt->pkttype ) {
148 node->next = n1->next;
149 n1->next = node;
150 return;
152 /* no such packet, append */
153 node->next = NULL;
154 n1->next = node;
159 /****************
160 * Find the previous node (if PKTTYPE = 0) or the previous node
161 * with pkttype PKTTYPE in the list starting with ROOT of NODE.
163 KBNODE
164 find_prev_kbnode( KBNODE root, KBNODE node, int pkttype )
166 KBNODE n1;
168 for (n1=NULL; root && root != node; root = root->next ) {
169 if (!pkttype ||root->pkt->pkttype == pkttype)
170 n1 = root;
172 return n1;
175 /****************
176 * Ditto, but find the next packet. The behaviour is trivial if
177 * PKTTYPE is 0 but if it is specified, the next node with a packet
178 * of this type is returned. The function has some knowledge about
179 * the valid ordering of packets: e.g. if the next signature packet
180 * is requested, the function will not return one if it encounters
181 * a user-id.
183 KBNODE
184 find_next_kbnode( KBNODE node, int pkttype )
186 for( node=node->next ; node; node = node->next ) {
187 if( !pkttype )
188 return node;
189 else if( pkttype == PKT_USER_ID
190 && ( node->pkt->pkttype == PKT_PUBLIC_KEY
191 || node->pkt->pkttype == PKT_SECRET_KEY ) )
192 return NULL;
193 else if( pkttype == PKT_SIGNATURE
194 && ( node->pkt->pkttype == PKT_USER_ID
195 || node->pkt->pkttype == PKT_PUBLIC_KEY
196 || node->pkt->pkttype == PKT_SECRET_KEY ) )
197 return NULL;
198 else if( node->pkt->pkttype == pkttype )
199 return node;
201 return NULL;
205 KBNODE
206 find_kbnode( KBNODE node, int pkttype )
208 for( ; node; node = node->next ) {
209 if( node->pkt->pkttype == pkttype )
210 return node;
212 return NULL;
217 /****************
218 * Walk through a list of kbnodes. This function returns
219 * the next kbnode for each call; before using the function the first
220 * time, the caller must set CONTEXT to NULL (This has simply the effect
221 * to start with ROOT).
223 KBNODE
224 walk_kbnode( KBNODE root, KBNODE *context, int all )
226 KBNODE n;
228 do {
229 if( !*context ) {
230 *context = root;
231 n = root;
233 else {
234 n = (*context)->next;
235 *context = n;
237 } while( !all && n && is_deleted_kbnode(n) );
239 return n;
242 void
243 clear_kbnode_flags( KBNODE n )
245 for( ; n; n = n->next ) {
246 n->flag = 0;
251 /****************
252 * Commit changes made to the kblist at ROOT. Note that ROOT my change,
253 * and it is therefore passed by reference.
254 * The function has the effect of removing all nodes marked as deleted.
255 * returns true if any node has been changed
258 commit_kbnode( KBNODE *root )
260 KBNODE n, nl;
261 int changed = 0;
263 for( n = *root, nl=NULL; n; n = nl->next ) {
264 if( is_deleted_kbnode(n) ) {
265 if( n == *root )
266 *root = nl = n->next;
267 else
268 nl->next = n->next;
269 if( !is_cloned_kbnode(n) ) {
270 free_packet( n->pkt );
271 xfree( n->pkt );
273 free_node( n );
274 changed = 1;
276 else
277 nl = n;
279 return changed;
282 void
283 remove_kbnode( KBNODE *root, KBNODE node )
285 KBNODE n, nl;
287 for( n = *root, nl=NULL; n; n = nl->next ) {
288 if( n == node ) {
289 if( n == *root )
290 *root = nl = n->next;
291 else
292 nl->next = n->next;
293 if( !is_cloned_kbnode(n) ) {
294 free_packet( n->pkt );
295 xfree( n->pkt );
297 free_node( n );
299 else
300 nl = n;
305 /****************
306 * Move NODE behind right after WHERE or to the beginning if WHERE is NULL.
308 void
309 move_kbnode( KBNODE *root, KBNODE node, KBNODE where )
311 KBNODE tmp, prev;
313 if( !root || !*root || !node )
314 return; /* sanity check */
315 for( prev = *root; prev && prev->next != node; prev = prev->next )
317 if( !prev )
318 return; /* node is not in the list */
320 if( !where ) { /* move node before root */
321 if( node == *root ) /* move to itself */
322 return;
323 prev->next = node->next;
324 node->next = *root;
325 *root = node;
326 return;
328 /* move it after where */
329 if( node == where )
330 return;
331 tmp = node->next;
332 node->next = where->next;
333 where->next = node;
334 prev->next = tmp;
340 void
341 dump_kbnode( KBNODE node )
343 for(; node; node = node->next ) {
344 const char *s;
345 switch( node->pkt->pkttype ) {
346 case 0: s="empty"; break;
347 case PKT_PUBLIC_KEY: s="public-key"; break;
348 case PKT_SECRET_KEY: s="secret-key"; break;
349 case PKT_SECRET_SUBKEY: s= "secret-subkey"; break;
350 case PKT_PUBKEY_ENC: s="public-enc"; break;
351 case PKT_SIGNATURE: s="signature"; break;
352 case PKT_ONEPASS_SIG: s="onepass-sig"; break;
353 case PKT_USER_ID: s="user-id"; break;
354 case PKT_PUBLIC_SUBKEY: s="public-subkey"; break;
355 case PKT_COMMENT: s="comment"; break;
356 case PKT_RING_TRUST: s="trust"; break;
357 case PKT_PLAINTEXT: s="plaintext"; break;
358 case PKT_COMPRESSED: s="compressed"; break;
359 case PKT_ENCRYPTED: s="encrypted"; break;
360 case PKT_GPG_CONTROL: s="gpg-control"; break;
361 default: s="unknown"; break;
363 fprintf(stderr, "node %p %02x/%02x type=%s",
364 node, node->flag, node->private_flag, s);
365 if( node->pkt->pkttype == PKT_USER_ID ) {
366 PKT_user_id *uid = node->pkt->pkt.user_id;
367 fputs(" \"", stderr);
368 print_string( stderr, uid->name, uid->len, 0 );
369 fprintf (stderr, "\" %c%c%c%c\n",
370 uid->is_expired? 'e':'.',
371 uid->is_revoked? 'r':'.',
372 uid->created? 'v':'.',
373 uid->is_primary? 'p':'.' );
375 else if( node->pkt->pkttype == PKT_SIGNATURE ) {
376 fprintf(stderr, " class=%02x keyid=%08lX ts=%lu\n",
377 node->pkt->pkt.signature->sig_class,
378 (ulong)node->pkt->pkt.signature->keyid[1],
379 (ulong)node->pkt->pkt.signature->timestamp);
381 else if( node->pkt->pkttype == PKT_GPG_CONTROL ) {
382 fprintf(stderr, " ctrl=%d len=%u\n",
383 node->pkt->pkt.gpg_control->control,
384 (unsigned int)node->pkt->pkt.gpg_control->datalen);
386 else if( node->pkt->pkttype == PKT_PUBLIC_KEY
387 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
388 PKT_public_key *pk = node->pkt->pkt.public_key;
389 fprintf(stderr, " keyid=%08lX a=%d u=%d %c%c%c%c\n",
390 (ulong)keyid_from_pk( pk, NULL ),
391 pk->pubkey_algo, pk->pubkey_usage,
392 pk->has_expired? 'e':'.',
393 pk->is_revoked? 'r':'.',
394 pk->is_valid? 'v':'.',
395 pk->mdc_feature? 'm':'.');
397 else
398 fputs("\n", stderr);