LCOV - code coverage report
Current view: top level - g10 - tdbdump.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 97 0.0 %
Date: 2015-11-05 17:10:59 Functions: 0 4 0.0 %

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

Generated by: LCOV version 1.11