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/>.
27 #include <sys/types.h>
45 #define HEXTOBIN(x) ( (x) >= '0' && (x) <= '9' ? ((x)-'0') : \
46 (x) >= 'A' && (x) <= 'F' ? ((x)-'A'+10) : ((x)-'a'+10))
50 * Wirte a record but die on error
53 write_record( TRUSTREC *rec )
55 int rc = tdbio_write_record( rec );
58 log_error(_("trust record %lu, type %d: write failed: %s\n"),
59 rec->recnum, rec->rectype, gpg_strerror (rc) );
65 * Dump the entire trustdb or only the entries of one key.
68 list_trustdb( const char *username )
75 /* For now we ignore the user ID. */
81 es_printf ("TrustDB: %s\n", tdbio_get_dbname() );
82 for(i=9+strlen(tdbio_get_dbname()); i > 0; i-- )
83 es_putc ('-', es_stdout);
84 es_putc ('\n', es_stdout);
85 for(recnum=0; !tdbio_read_record( recnum, &rec, 0); recnum++ )
86 tdbio_dump_record (&rec, es_stdout);
95 * Print a list of all defined owner trust value.
106 es_printf (_("# List of assigned trustvalues, created %s\n"
107 "# (Use \"gpg --import-ownertrust\" to restore them)\n"),
108 asctimestamp( make_timestamp() ) );
109 for (recnum=0; !tdbio_read_record (recnum, &rec, 0); recnum++ )
111 if (rec.rectype == RECTYPE_TRUST)
113 if (!rec.r.trust.ownertrust)
115 p = rec.r.trust.fingerprint;
116 for (i=0; i < 20; i++, p++ )
117 es_printf("%02X", *p );
118 es_printf (":%u:\n", (unsigned int)rec.r.trust.ownertrust );
125 import_ownertrust( const char *fname )
138 if( iobuf_is_pipe_filename (fname) ) {
143 else if( !(fp = es_fopen( fname, "r" )) ) {
144 log_error ( _("can't open '%s': %s\n"), fname, strerror(errno) );
148 if (is_secured_file (es_fileno (fp)))
151 gpg_err_set_errno (EPERM);
152 log_error (_("can't open '%s': %s\n"), fname, strerror(errno) );
156 while (es_fgets (line, DIM(line)-1, fp)) {
159 if( !*line || *line == '#' )
162 if( line[n-1] != '\n' ) {
163 log_error (_("error in '%s': %s\n"), fname, _("line too long") );
164 /* ... or last line does not have a LF */
165 break; /* can't continue */
167 for(p = line; *p && *p != ':' ; p++ )
171 log_error (_("error in '%s': %s\n"), fname, _("colon missing") );
175 if( fprlen != 32 && fprlen != 40 ) {
176 log_error (_("error in '%s': %s\n"),
177 fname, _("invalid fingerprint") );
180 if( sscanf(p, ":%u:", &otrust ) != 1 ) {
181 log_error (_("error in '%s': %s\n"),
182 fname, _("ownertrust value missing"));
186 continue; /* no otrust defined - no need to update or insert */
187 /* convert the ascii fingerprint to binary */
188 for(p=line, fprlen=0; fprlen < 20 && *p != ':'; p += 2 )
189 fpr[fprlen++] = HEXTOBIN(p[0]) * 16 + HEXTOBIN(p[1]);
193 rc = tdbio_search_trust_byfpr (fpr, &rec);
194 if( !rc ) { /* found: update */
195 if (rec.r.trust.ownertrust != otrust)
197 if( rec.r.trust.ownertrust )
198 log_info("changing ownertrust from %u to %u\n",
199 rec.r.trust.ownertrust, otrust );
201 log_info("setting ownertrust to %u\n", otrust );
202 rec.r.trust.ownertrust = otrust;
203 write_record (&rec );
207 else if( rc == -1 ) { /* not found: insert */
208 log_info("inserting ownertrust of %u\n", otrust );
209 memset (&rec, 0, sizeof rec);
210 rec.recnum = tdbio_new_recnum ();
211 rec.rectype = RECTYPE_TRUST;
212 memcpy (rec.r.trust.fingerprint, fpr, 20);
213 rec.r.trust.ownertrust = otrust;
214 write_record (&rec );
218 log_error (_("error finding trust record in '%s': %s\n"),
219 fname, gpg_strerror (rc));
222 log_error ( _("read error in '%s': %s\n"), fname, strerror(errno) );
228 revalidation_mark ();
231 log_error (_("trustdb: sync failed: %s\n"), gpg_strerror (rc) );