1 |
werner |
46 |
/* kbnode.c - keyblock node utility functions |
2 |
|
|
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
3 |
|
|
* |
4 |
|
|
* This file is part of GnuPG. |
5 |
|
|
* |
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 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
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. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
#include <stdio.h> |
26 |
|
|
#include <stdio.h> |
27 |
|
|
#include <string.h> |
28 |
|
|
#include <assert.h> |
29 |
|
|
#include <malloc.h> |
30 |
|
|
#include <sys/types.h> |
31 |
|
|
|
32 |
|
|
#include "openpgp.h" |
33 |
|
|
|
34 |
|
|
#define is_deleted_kbnode(a) ((a)->private_flag & 1) |
35 |
|
|
#define is_cloned_kbnode(a) ((a)->private_flag & 2) |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
static gpg_kbnode_t |
39 |
|
|
alloc_node( void ) |
40 |
|
|
{ |
41 |
|
|
gpg_kbnode_t n; |
42 |
|
|
|
43 |
|
|
n = malloc( sizeof *n ); |
44 |
|
|
n->next = NULL; |
45 |
|
|
n->pkt = NULL; |
46 |
|
|
n->flag = 0; |
47 |
|
|
n->private_flag=0; |
48 |
|
|
n->recno = 0; |
49 |
|
|
return n; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
static void |
53 |
|
|
free_node( gpg_kbnode_t n ) |
54 |
|
|
{ |
55 |
|
|
if( n ) { |
56 |
|
|
free( n ); |
57 |
|
|
} |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
gpg_kbnode_t |
63 |
|
|
gpg_new_kbnode( PACKET *pkt ) |
64 |
|
|
{ |
65 |
|
|
gpg_kbnode_t n = alloc_node(); |
66 |
|
|
n->pkt = pkt; |
67 |
|
|
return n; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
|
71 |
|
|
gpg_kbnode_t |
72 |
|
|
gpg_clone_kbnode( gpg_kbnode_t node ) |
73 |
|
|
{ |
74 |
|
|
gpg_kbnode_t n = alloc_node(); |
75 |
|
|
|
76 |
|
|
n->pkt = node->pkt; |
77 |
|
|
n->private_flag = node->private_flag | 2; /* mark cloned */ |
78 |
|
|
return n; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
void |
83 |
|
|
gpg_release_kbnode( gpg_kbnode_t n ) |
84 |
|
|
{ |
85 |
|
|
gpg_kbnode_t n2; |
86 |
|
|
|
87 |
|
|
while( n ) { |
88 |
|
|
n2 = n->next; |
89 |
|
|
if( !is_cloned_kbnode(n) ) { |
90 |
|
|
gpg_free_packet( n->pkt ); |
91 |
|
|
free( n->pkt ); |
92 |
|
|
} |
93 |
|
|
free_node( n ); |
94 |
|
|
n = n2; |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
/**************** |
100 |
|
|
* Delete NODE. |
101 |
|
|
* Note: This only works with walk_kbnode!! |
102 |
|
|
*/ |
103 |
|
|
void |
104 |
|
|
gpg_delete_kbnode( gpg_kbnode_t node ) |
105 |
|
|
{ |
106 |
|
|
node->private_flag |= 1; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
/**************** |
112 |
|
|
* Append NODE to ROOT. ROOT must exist! |
113 |
|
|
*/ |
114 |
|
|
void |
115 |
|
|
gpg_add_kbnode( gpg_kbnode_t root, gpg_kbnode_t node ) |
116 |
|
|
{ |
117 |
|
|
gpg_kbnode_t n1; |
118 |
|
|
|
119 |
|
|
for(n1=root; n1->next; n1 = n1->next) |
120 |
|
|
; |
121 |
|
|
n1->next = node; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
|
125 |
|
|
/**************** |
126 |
|
|
* Find the previous node (if PKTTYPE = 0) or the previous node |
127 |
|
|
* with pkttype PKTTYPE in the list starting with ROOT of NODE. |
128 |
|
|
*/ |
129 |
|
|
gpg_kbnode_t |
130 |
|
|
gpg_find_prev_kbnode( gpg_kbnode_t root, gpg_kbnode_t node, int pkttype ) |
131 |
|
|
{ |
132 |
|
|
gpg_kbnode_t n1; |
133 |
|
|
|
134 |
|
|
for (n1=NULL; root && root != node; root = root->next ) { |
135 |
|
|
if (!pkttype ||root->pkt->pkttype == pkttype) |
136 |
|
|
n1 = root; |
137 |
|
|
} |
138 |
|
|
return n1; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
/**************** |
142 |
|
|
* Ditto, but find the next packet. The behaviour is trivial if |
143 |
|
|
* PKTTYPE is 0 but if it is specified, the next node with a packet |
144 |
|
|
* of this type is returned. The function has some knowledge about |
145 |
|
|
* the valid ordering of packets: e.g. if the next signature packet |
146 |
|
|
* is requested, the function will not return one if it encounters |
147 |
|
|
* a user-id. |
148 |
|
|
*/ |
149 |
|
|
gpg_kbnode_t |
150 |
|
|
gpg_find_next_kbnode( gpg_kbnode_t node, int pkttype ) |
151 |
|
|
{ |
152 |
|
|
for( node=node->next ; node; node = node->next ) { |
153 |
|
|
if( !pkttype ) |
154 |
|
|
return node; |
155 |
|
|
else if( pkttype == PKT_USER_ID |
156 |
|
|
&& ( node->pkt->pkttype == PKT_PUBLIC_KEY |
157 |
|
|
|| node->pkt->pkttype == PKT_SECRET_KEY ) ) |
158 |
|
|
return NULL; |
159 |
|
|
else if( pkttype == PKT_SIGNATURE |
160 |
|
|
&& ( node->pkt->pkttype == PKT_USER_ID |
161 |
|
|
|| node->pkt->pkttype == PKT_PUBLIC_KEY |
162 |
|
|
|| node->pkt->pkttype == PKT_SECRET_KEY ) ) |
163 |
|
|
return NULL; |
164 |
|
|
else if( node->pkt->pkttype == pkttype ) |
165 |
|
|
return node; |
166 |
|
|
} |
167 |
|
|
return NULL; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
|
171 |
|
|
gpg_kbnode_t |
172 |
|
|
gpg_find_kbnode( gpg_kbnode_t node, int pkttype ) |
173 |
|
|
{ |
174 |
|
|
for( ; node; node = node->next ) { |
175 |
|
|
if( node->pkt->pkttype == pkttype ) |
176 |
|
|
return node; |
177 |
|
|
} |
178 |
|
|
return NULL; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
/**************** |
184 |
|
|
* Walk through a list of kbnodes. This function returns |
185 |
|
|
* the next kbnode for each call; before using the function the first |
186 |
|
|
* time, the caller must set CONTEXT to NULL (This has simply the effect |
187 |
|
|
* to start with ROOT). |
188 |
|
|
*/ |
189 |
|
|
gpg_kbnode_t |
190 |
|
|
gpg_walk_kbnode( gpg_kbnode_t root, gpg_kbnode_t *context, int all ) |
191 |
|
|
{ |
192 |
|
|
gpg_kbnode_t n; |
193 |
|
|
|
194 |
|
|
do { |
195 |
|
|
if( !*context ) { |
196 |
|
|
*context = root; |
197 |
|
|
n = root; |
198 |
|
|
} |
199 |
|
|
else { |
200 |
|
|
n = (*context)->next; |
201 |
|
|
*context = n; |
202 |
|
|
} |
203 |
|
|
} while( !all && n && is_deleted_kbnode(n) ); |
204 |
|
|
|
205 |
|
|
return n; |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
void |
209 |
|
|
gpg_clear_kbnode_flags( gpg_kbnode_t n ) |
210 |
|
|
{ |
211 |
|
|
for( ; n; n = n->next ) { |
212 |
|
|
n->flag = 0; |
213 |
|
|
} |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|