chiark / gitweb /
Import gnupg2_2.1.17.orig.tar.bz2
[gnupg2.git] / g10 / tdbdump.c
1 /* tdbdump.c
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 3 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, see <https://www.gnu.org/licenses/>.
18  */
19
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 <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include "gpg.h"
32 #include "status.h"
33 #include "iobuf.h"
34 #include "keydb.h"
35 #include "util.h"
36 #include "trustdb.h"
37 #include "options.h"
38 #include "packet.h"
39 #include "main.h"
40 #include "i18n.h"
41 #include "tdbio.h"
42
43
44 #define HEXTOBIN(x) ( (x) >= '0' && (x) <= '9' ? ((x)-'0') : \
45                       (x) >= 'A' && (x) <= 'F' ? ((x)-'A'+10) : ((x)-'a'+10))
46
47
48 /*
49  * Write a record; die on error.
50  */
51 static void
52 write_record( TRUSTREC *rec )
53 {
54     int rc = tdbio_write_record( rec );
55     if( !rc )
56         return;
57     log_error(_("trust record %lu, type %d: write failed: %s\n"),
58                             rec->recnum, rec->rectype, gpg_strerror (rc) );
59     tdbio_invalid();
60 }
61
62
63 /*
64  * Dump the entire trustdb to FP or only the entries of one key.
65  */
66 void
67 list_trustdb (estream_t fp, const char *username)
68 {
69   TRUSTREC rec;
70
71   (void)username;
72
73   init_trustdb();
74   /* For now we ignore the user ID. */
75   if (1)
76     {
77       ulong recnum;
78       int i;
79
80       es_fprintf (fp, "TrustDB: %s\n", tdbio_get_dbname ());
81       for (i = 9 + strlen (tdbio_get_dbname()); i > 0; i-- )
82         es_fputc ('-', fp);
83       es_putc ('\n', fp);
84       for (recnum=0; !tdbio_read_record (recnum, &rec, 0); recnum++)
85         tdbio_dump_record (&rec, fp);
86     }
87 }
88
89
90
91
92
93 /****************
94  * Print a list of all defined owner trust value.
95  */
96 void
97 export_ownertrust()
98 {
99   TRUSTREC rec;
100   ulong recnum;
101   int i;
102   byte *p;
103
104   init_trustdb();
105   es_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     {
110       if (rec.rectype == RECTYPE_TRUST)
111         {
112           if (!rec.r.trust.ownertrust)
113             continue;
114           p = rec.r.trust.fingerprint;
115           for (i=0; i < 20; i++, p++ )
116             es_printf("%02X", *p );
117           es_printf (":%u:\n", (unsigned int)rec.r.trust.ownertrust );
118         }
119     }
120 }
121
122
123 void
124 import_ownertrust( const char *fname )
125 {
126     estream_t fp;
127     int is_stdin=0;
128     char line[256];
129     char *p;
130     size_t n, fprlen;
131     unsigned int otrust;
132     byte fpr[20];
133     int any = 0;
134     int rc;
135
136     init_trustdb();
137     if( iobuf_is_pipe_filename (fname) ) {
138         fp = es_stdin;
139         fname = "[stdin]";
140         is_stdin = 1;
141     }
142     else if( !(fp = es_fopen( fname, "r" )) ) {
143         log_error ( _("can't open '%s': %s\n"), fname, strerror(errno) );
144         return;
145     }
146
147     if (is_secured_file (es_fileno (fp)))
148       {
149         es_fclose (fp);
150         gpg_err_set_errno (EPERM);
151         log_error (_("can't open '%s': %s\n"), fname, strerror(errno) );
152         return;
153       }
154
155     while (es_fgets (line, DIM(line)-1, fp)) {
156         TRUSTREC rec;
157
158         if( !*line || *line == '#' )
159             continue;
160         n = strlen(line);
161         if( line[n-1] != '\n' ) {
162             log_error (_("error in '%s': %s\n"), fname, _("line too long") );
163             /* ... or last line does not have a LF */
164             break; /* can't continue */
165         }
166         for(p = line; *p && *p != ':' ; p++ )
167             if( !hexdigitp(p) )
168                 break;
169         if( *p != ':' ) {
170             log_error (_("error in '%s': %s\n"), fname, _("colon missing") );
171             continue;
172         }
173         fprlen = p - line;
174         if( fprlen != 32 && fprlen != 40 ) {
175             log_error (_("error in '%s': %s\n"),
176                        fname, _("invalid fingerprint") );
177             continue;
178         }
179         if( sscanf(p, ":%u:", &otrust ) != 1 ) {
180             log_error (_("error in '%s': %s\n"),
181                        fname, _("ownertrust value missing"));
182             continue;
183         }
184         if( !otrust )
185             continue; /* no otrust defined - no need to update or insert */
186         /* convert the ascii fingerprint to binary */
187         for(p=line, fprlen=0; fprlen < 20 && *p != ':'; p += 2 )
188             fpr[fprlen++] = HEXTOBIN(p[0]) * 16 + HEXTOBIN(p[1]);
189         while (fprlen < 20)
190             fpr[fprlen++] = 0;
191
192         rc = tdbio_search_trust_byfpr (fpr, &rec);
193         if( !rc ) { /* found: update */
194             if (rec.r.trust.ownertrust != otrust)
195               {
196                 if( rec.r.trust.ownertrust )
197                   log_info("changing ownertrust from %u to %u\n",
198                            rec.r.trust.ownertrust, otrust );
199                 else
200                   log_info("setting ownertrust to %u\n", otrust );
201                 rec.r.trust.ownertrust = otrust;
202                 write_record (&rec );
203                 any = 1;
204               }
205         }
206         else if (gpg_err_code (rc) == GPG_ERR_NOT_FOUND) { /* insert */
207             log_info("inserting ownertrust of %u\n", otrust );
208             memset (&rec, 0, sizeof rec);
209             rec.recnum = tdbio_new_recnum ();
210             rec.rectype = RECTYPE_TRUST;
211             memcpy (rec.r.trust.fingerprint, fpr, 20);
212             rec.r.trust.ownertrust = otrust;
213             write_record (&rec );
214             any = 1;
215         }
216         else /* error */
217             log_error (_("error finding trust record in '%s': %s\n"),
218                        fname, gpg_strerror (rc));
219     }
220     if (es_ferror (fp))
221         log_error ( _("read error in '%s': %s\n"), fname, strerror(errno) );
222     if (!is_stdin)
223         es_fclose (fp);
224
225     if (any)
226       {
227         revalidation_mark ();
228         rc = tdbio_sync ();
229         if (rc)
230           log_error (_("trustdb: sync failed: %s\n"), gpg_strerror (rc) );
231       }
232
233 }