2008-02-09 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / tdbdump.c
blob5e7b685e21680cd9a7a577cd6066029d048b7b14
1 /* tdbdump.c
2 * Copyright (C) 1998, 1999, 2000, 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/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <ctype.h>
26 #include <assert.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
32 #include "gpg.h"
33 #include "status.h"
34 #include "iobuf.h"
35 #include "keydb.h"
36 #include "util.h"
37 #include "trustdb.h"
38 #include "options.h"
39 #include "packet.h"
40 #include "main.h"
41 #include "i18n.h"
42 #include "tdbio.h"
45 #define HEXTOBIN(x) ( (x) >= '0' && (x) <= '9' ? ((x)-'0') : \
46 (x) >= 'A' && (x) <= 'F' ? ((x)-'A'+10) : ((x)-'a'+10))
49 /****************
50 * Wirte a record but die on error
52 static void
53 write_record( TRUSTREC *rec )
55 int rc = tdbio_write_record( rec );
56 if( !rc )
57 return;
58 log_error(_("trust record %lu, type %d: write failed: %s\n"),
59 rec->recnum, rec->rectype, g10_errstr(rc) );
60 tdbio_invalid();
64 /****************
65 * Dump the entire trustdb or only the entries of one key.
67 void
68 list_trustdb( const char *username )
70 TRUSTREC rec;
72 init_trustdb();
73 /* for now we ignore the user ID */
74 if (1) {
75 ulong recnum;
76 int i;
78 printf("TrustDB: %s\n", tdbio_get_dbname() );
79 for(i=9+strlen(tdbio_get_dbname()); i > 0; i-- )
80 putchar('-');
81 putchar('\n');
82 for(recnum=0; !tdbio_read_record( recnum, &rec, 0); recnum++ )
83 tdbio_dump_record( &rec, stdout );
91 /****************
92 * Print a list of all defined owner trust value.
94 void
95 export_ownertrust()
97 TRUSTREC rec;
98 ulong recnum;
99 int i;
100 byte *p;
102 init_trustdb();
103 printf(_("# List of assigned trustvalues, created %s\n"
104 "# (Use \"gpg --import-ownertrust\" to restore them)\n"),
105 asctimestamp( make_timestamp() ) );
106 for(recnum=0; !tdbio_read_record( recnum, &rec, 0); recnum++ ) {
107 if( rec.rectype == RECTYPE_TRUST ) {
108 if( !rec.r.trust.ownertrust )
109 continue;
110 p = rec.r.trust.fingerprint;
111 for(i=0; i < 20; i++, p++ )
112 printf("%02X", *p );
113 printf(":%u:\n", (unsigned int)rec.r.trust.ownertrust );
119 void
120 import_ownertrust( const char *fname )
122 FILE *fp;
123 int is_stdin=0;
124 char line[256];
125 char *p;
126 size_t n, fprlen;
127 unsigned int otrust;
128 byte fpr[20];
129 int any = 0;
130 int rc;
132 init_trustdb();
133 if( iobuf_is_pipe_filename (fname) ) {
134 fp = stdin;
135 fname = "[stdin]";
136 is_stdin = 1;
138 else if( !(fp = fopen( fname, "r" )) ) {
139 log_error ( _("can't open `%s': %s\n"), fname, strerror(errno) );
140 return;
143 if (is_secured_file (fileno (fp)))
145 fclose (fp);
146 errno = EPERM;
147 log_error (_("can't open `%s': %s\n"), fname, strerror(errno) );
148 return;
151 while( fgets( line, DIM(line)-1, fp ) ) {
152 TRUSTREC rec;
154 if( !*line || *line == '#' )
155 continue;
156 n = strlen(line);
157 if( line[n-1] != '\n' ) {
158 log_error (_("error in `%s': %s\n"), fname, _("line too long") );
159 /* ... or last line does not have a LF */
160 break; /* can't continue */
162 for(p = line; *p && *p != ':' ; p++ )
163 if( !hexdigitp(p) )
164 break;
165 if( *p != ':' ) {
166 log_error (_("error in `%s': %s\n"), fname, _("colon missing") );
167 continue;
169 fprlen = p - line;
170 if( fprlen != 32 && fprlen != 40 ) {
171 log_error (_("error in `%s': %s\n"),
172 fname, _("invalid fingerprint") );
173 continue;
175 if( sscanf(p, ":%u:", &otrust ) != 1 ) {
176 log_error (_("error in `%s': %s\n"),
177 fname, _("ownertrust value missing"));
178 continue;
180 if( !otrust )
181 continue; /* no otrust defined - no need to update or insert */
182 /* convert the ascii fingerprint to binary */
183 for(p=line, fprlen=0; fprlen < 20 && *p != ':'; p += 2 )
184 fpr[fprlen++] = HEXTOBIN(p[0]) * 16 + HEXTOBIN(p[1]);
185 while (fprlen < 20)
186 fpr[fprlen++] = 0;
188 rc = tdbio_search_trust_byfpr (fpr, &rec);
189 if( !rc ) { /* found: update */
190 if (rec.r.trust.ownertrust != otrust)
192 if( rec.r.trust.ownertrust )
193 log_info("changing ownertrust from %u to %u\n",
194 rec.r.trust.ownertrust, otrust );
195 else
196 log_info("setting ownertrust to %u\n", otrust );
197 rec.r.trust.ownertrust = otrust;
198 write_record (&rec );
199 any = 1;
202 else if( rc == -1 ) { /* not found: insert */
203 log_info("inserting ownertrust of %u\n", otrust );
204 memset (&rec, 0, sizeof rec);
205 rec.recnum = tdbio_new_recnum ();
206 rec.rectype = RECTYPE_TRUST;
207 memcpy (rec.r.trust.fingerprint, fpr, 20);
208 rec.r.trust.ownertrust = otrust;
209 write_record (&rec );
210 any = 1;
212 else /* error */
213 log_error (_("error finding trust record in `%s': %s\n"),
214 fname, g10_errstr(rc));
216 if( ferror(fp) )
217 log_error ( _("read error in `%s': %s\n"), fname, strerror(errno) );
218 if( !is_stdin )
219 fclose(fp);
221 if (any)
223 revalidation_mark ();
224 rc = tdbio_sync ();
225 if (rc)
226 log_error (_("trustdb: sync failed: %s\n"), g10_errstr(rc) );