2006-09-24 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / tdbdump.c
blobd840c088200a64f7ee25cf8065125dfcac1140cf
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <assert.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
34 #include "gpg.h"
35 #include "errors.h"
36 #include "iobuf.h"
37 #include "keydb.h"
38 #include "util.h"
39 #include "trustdb.h"
40 #include "options.h"
41 #include "packet.h"
42 #include "main.h"
43 #include "i18n.h"
44 #include "tdbio.h"
47 #define HEXTOBIN(x) ( (x) >= '0' && (x) <= '9' ? ((x)-'0') : \
48 (x) >= 'A' && (x) <= 'F' ? ((x)-'A'+10) : ((x)-'a'+10))
51 /****************
52 * Wirte a record but die on error
54 static void
55 write_record( TRUSTREC *rec )
57 int rc = tdbio_write_record( rec );
58 if( !rc )
59 return;
60 log_error(_("trust record %lu, type %d: write failed: %s\n"),
61 rec->recnum, rec->rectype, g10_errstr(rc) );
62 tdbio_invalid();
66 /****************
67 * Dump the entire trustdb or only the entries of one key.
69 void
70 list_trustdb( const char *username )
72 TRUSTREC rec;
74 init_trustdb();
75 /* for now we ignore the user ID */
76 if (1) {
77 ulong recnum;
78 int i;
80 printf("TrustDB: %s\n", tdbio_get_dbname() );
81 for(i=9+strlen(tdbio_get_dbname()); i > 0; i-- )
82 putchar('-');
83 putchar('\n');
84 for(recnum=0; !tdbio_read_record( recnum, &rec, 0); recnum++ )
85 tdbio_dump_record( &rec, stdout );
93 /****************
94 * Print a list of all defined owner trust value.
96 void
97 export_ownertrust()
99 TRUSTREC rec;
100 ulong recnum;
101 int i;
102 byte *p;
104 init_trustdb();
105 printf(_("# List of assigned trustvalues, created %s\n"
106 "# (Use \"gpg --import-ownertrust\" to restore them)\n"),
107 asctimestamp( make_timestamp() ) );
108 for(recnum=0; !tdbio_read_record( recnum, &rec, 0); recnum++ ) {
109 if( rec.rectype == RECTYPE_TRUST ) {
110 if( !rec.r.trust.ownertrust )
111 continue;
112 p = rec.r.trust.fingerprint;
113 for(i=0; i < 20; i++, p++ )
114 printf("%02X", *p );
115 printf(":%u:\n", (unsigned int)rec.r.trust.ownertrust );
121 void
122 import_ownertrust( const char *fname )
124 FILE *fp;
125 int is_stdin=0;
126 char line[256];
127 char *p;
128 size_t n, fprlen;
129 unsigned int otrust;
130 byte fpr[20];
131 int any = 0;
132 int rc;
134 init_trustdb();
135 if( iobuf_is_pipe_filename (fname) ) {
136 fp = stdin;
137 fname = "[stdin]";
138 is_stdin = 1;
140 else if( !(fp = fopen( fname, "r" )) ) {
141 log_error ( _("can't open `%s': %s\n"), fname, strerror(errno) );
142 return;
145 if (is_secured_file (fileno (fp)))
147 fclose (fp);
148 errno = EPERM;
149 log_error (_("can't open `%s': %s\n"), fname, strerror(errno) );
150 return;
153 while( fgets( line, DIM(line)-1, fp ) ) {
154 TRUSTREC rec;
156 if( !*line || *line == '#' )
157 continue;
158 n = strlen(line);
159 if( line[n-1] != '\n' ) {
160 log_error (_("error in `%s': %s\n"), fname, _("line too long") );
161 /* ... or last line does not have a LF */
162 break; /* can't continue */
164 for(p = line; *p && *p != ':' ; p++ )
165 if( !hexdigitp(p) )
166 break;
167 if( *p != ':' ) {
168 log_error (_("error in `%s': %s\n"), fname, _("colon missing") );
169 continue;
171 fprlen = p - line;
172 if( fprlen != 32 && fprlen != 40 ) {
173 log_error (_("error in `%s': %s\n"),
174 fname, _("invalid fingerprint") );
175 continue;
177 if( sscanf(p, ":%u:", &otrust ) != 1 ) {
178 log_error (_("error in `%s': %s\n"),
179 fname, _("ownertrust value missing"));
180 continue;
182 if( !otrust )
183 continue; /* no otrust defined - no need to update or insert */
184 /* convert the ascii fingerprint to binary */
185 for(p=line, fprlen=0; fprlen < 20 && *p != ':'; p += 2 )
186 fpr[fprlen++] = HEXTOBIN(p[0]) * 16 + HEXTOBIN(p[1]);
187 while (fprlen < 20)
188 fpr[fprlen++] = 0;
190 rc = tdbio_search_trust_byfpr (fpr, &rec);
191 if( !rc ) { /* found: update */
192 if (rec.r.trust.ownertrust != otrust)
194 if( rec.r.trust.ownertrust )
195 log_info("changing ownertrust from %u to %u\n",
196 rec.r.trust.ownertrust, otrust );
197 else
198 log_info("setting ownertrust to %u\n", otrust );
199 rec.r.trust.ownertrust = otrust;
200 write_record (&rec );
201 any = 1;
204 else if( rc == -1 ) { /* not found: insert */
205 log_info("inserting ownertrust of %u\n", otrust );
206 memset (&rec, 0, sizeof rec);
207 rec.recnum = tdbio_new_recnum ();
208 rec.rectype = RECTYPE_TRUST;
209 memcpy (rec.r.trust.fingerprint, fpr, 20);
210 rec.r.trust.ownertrust = otrust;
211 write_record (&rec );
212 any = 1;
214 else /* error */
215 log_error (_("error finding trust record in `%s': %s\n"),
216 fname, g10_errstr(rc));
218 if( ferror(fp) )
219 log_error ( _("read error in `%s': %s\n"), fname, strerror(errno) );
220 if( !is_stdin )
221 fclose(fp);
223 if (any)
225 revalidation_mark ();
226 rc = tdbio_sync ();
227 if (rc)
228 log_error (_("trustdb: sync failed: %s\n"), g10_errstr(rc) );