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