LCOV - code coverage report
Current view: top level - tests - run-export.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 85 0.0 %
Date: 2016-09-12 12:35:26 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /* pgp-export.c  - Helper to run an export command
       2             :    Copyright (C) 2008, 2009 g10 Code GmbH
       3             : 
       4             :    This file is part of GPGME.
       5             : 
       6             :    GPGME is free software; you can redistribute it and/or modify it
       7             :    under the terms of the GNU Lesser General Public License as
       8             :    published by the Free Software Foundation; either version 2.1 of
       9             :    the License, or (at your option) any later version.
      10             : 
      11             :    GPGME is distributed in the hope that it will be useful, but
      12             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :    Lesser General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU Lesser General Public
      17             :    License along with this program; if not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : /* We need to include config.h so that we know whether we are building
      21             :    with large file system (LFS) support. */
      22             : #ifdef HAVE_CONFIG_H
      23             : #include <config.h>
      24             : #endif
      25             : 
      26             : #include <stdlib.h>
      27             : #include <stdio.h>
      28             : #include <string.h>
      29             : 
      30             : #include <gpgme.h>
      31             : 
      32             : #define PGM "run-export"
      33             : 
      34             : #include "run-support.h"
      35             : 
      36             : 
      37             : static int verbose;
      38             : 
      39             : 
      40             : static int
      41           0 : show_usage (int ex)
      42             : {
      43           0 :   fputs ("usage: " PGM " [options] USERIDS\n\n"
      44             :          "Options:\n"
      45             :          "  --verbose        run in verbose mode\n"
      46             :          "  --openpgp        use OpenPGP protocol (default)\n"
      47             :          "  --cms            use X.509 protocol\n"
      48             :          "  --extern         send keys to the keyserver (TAKE CARE!)\n"
      49             :          "  --secret         export secret keys instead of public keys\n"
      50             :          "  --raw            use PKCS#1 as secret key format\n"
      51             :          "  --pkcs12         use PKCS#12 as secret key format\n"
      52             :          , stderr);
      53           0 :   exit (ex);
      54             : }
      55             : 
      56             : int
      57           0 : main (int argc, char **argv)
      58             : {
      59           0 :   int last_argc = -1;
      60             :   gpgme_error_t err;
      61             :   gpgme_ctx_t ctx;
      62             :   gpgme_key_t key;
      63             :   gpgme_keylist_result_t result;
      64             :   gpgme_key_t keyarray[100];
      65           0 :   int keyidx = 0;
      66             :   gpgme_data_t out;
      67           0 :   gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
      68           0 :   gpgme_export_mode_t mode = 0;
      69             : 
      70           0 :   if (argc)
      71           0 :     { argc--; argv++; }
      72             : 
      73           0 :   while (argc && last_argc != argc )
      74             :     {
      75           0 :       last_argc = argc;
      76           0 :       if (!strcmp (*argv, "--"))
      77             :         {
      78           0 :           argc--; argv++;
      79           0 :           break;
      80             :         }
      81           0 :       else if (!strcmp (*argv, "--help"))
      82           0 :         show_usage (0);
      83           0 :       else if (!strcmp (*argv, "--verbose"))
      84             :         {
      85           0 :           verbose = 1;
      86           0 :           argc--; argv++;
      87             :         }
      88           0 :       else if (!strcmp (*argv, "--openpgp"))
      89             :         {
      90           0 :           protocol = GPGME_PROTOCOL_OpenPGP;
      91           0 :           argc--; argv++;
      92             :         }
      93           0 :       else if (!strcmp (*argv, "--cms"))
      94             :         {
      95           0 :           protocol = GPGME_PROTOCOL_CMS;
      96           0 :           argc--; argv++;
      97             :         }
      98           0 :       else if (!strcmp (*argv, "--extern"))
      99             :         {
     100           0 :           mode |= GPGME_EXPORT_MODE_EXTERN;
     101           0 :           argc--; argv++;
     102             :         }
     103           0 :       else if (!strcmp (*argv, "--secret"))
     104             :         {
     105           0 :           mode |= GPGME_EXPORT_MODE_SECRET;
     106           0 :           argc--; argv++;
     107             :         }
     108           0 :       else if (!strcmp (*argv, "--raw"))
     109             :         {
     110           0 :           mode |= GPGME_EXPORT_MODE_RAW;
     111           0 :           argc--; argv++;
     112             :         }
     113           0 :       else if (!strcmp (*argv, "--pkcs12"))
     114             :         {
     115           0 :           mode |= GPGME_EXPORT_MODE_PKCS12;
     116           0 :           argc--; argv++;
     117             :         }
     118           0 :       else if (!strncmp (*argv, "--", 2))
     119           0 :         show_usage (1);
     120             : 
     121             :     }
     122             : 
     123           0 :   if (!argc)
     124           0 :     show_usage (1);
     125             : 
     126           0 :   init_gpgme (protocol);
     127             : 
     128           0 :   err = gpgme_new (&ctx);
     129           0 :   fail_if_err (err);
     130           0 :   gpgme_set_protocol (ctx, protocol);
     131             : 
     132             :   /* Lookup the keys.  */
     133           0 :   err = gpgme_op_keylist_ext_start (ctx, (const char**)argv, 0, 0);
     134           0 :   fail_if_err (err);
     135             : 
     136           0 :   while (!(err = gpgme_op_keylist_next (ctx, &key)))
     137             :     {
     138           0 :       printf ("keyid: %s  (fpr: %s)\n",
     139           0 :               key->subkeys?nonnull (key->subkeys->keyid):"?",
     140           0 :               key->subkeys?nonnull (key->subkeys->fpr):"?");
     141             : 
     142           0 :       if (keyidx < DIM (keyarray)-1)
     143           0 :         keyarray[keyidx++] = key;
     144             :       else
     145             :         {
     146           0 :           fprintf (stderr, PGM": too many keys"
     147             :                    "- skipping this key\n");
     148           0 :           gpgme_key_unref (key);
     149             :         }
     150             :     }
     151           0 :   if (gpgme_err_code (err) != GPG_ERR_EOF)
     152           0 :     fail_if_err (err);
     153           0 :   err = gpgme_op_keylist_end (ctx);
     154           0 :   fail_if_err (err);
     155           0 :   keyarray[keyidx] = NULL;
     156             : 
     157           0 :   result = gpgme_op_keylist_result (ctx);
     158           0 :   if (result->truncated)
     159             :     {
     160           0 :       fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
     161           0 :       exit (1);
     162             :     }
     163             : 
     164             :   /* Now for the actual export.  */
     165           0 :   if ((mode & GPGME_EXPORT_MODE_EXTERN))
     166           0 :     printf ("sending keys to keyserver\n");
     167           0 :   if ((mode & GPGME_EXPORT_MODE_SECRET))
     168           0 :     printf ("exporting secret keys!\n");
     169             : 
     170           0 :   err = gpgme_data_new (&out);
     171           0 :   fail_if_err (err);
     172             : 
     173           0 :   gpgme_set_armor (ctx, 1);
     174           0 :   err = gpgme_op_export_keys (ctx, keyarray, mode,
     175           0 :                               (mode & GPGME_KEYLIST_MODE_EXTERN)? NULL:out);
     176           0 :   fail_if_err (err);
     177             : 
     178           0 :   fflush (NULL);
     179           0 :   if (!(mode & GPGME_KEYLIST_MODE_EXTERN))
     180             :     {
     181           0 :       fputs ("Begin Result:\n", stdout);
     182           0 :       print_data (out);
     183           0 :       fputs ("End Result.\n", stdout);
     184             :     }
     185             : 
     186             :   /* Cleanup.  */
     187           0 :   gpgme_data_release (out);
     188             : 
     189           0 :   for (keyidx=0; keyarray[keyidx]; keyidx++)
     190           0 :     gpgme_key_unref (keyarray[keyidx]);
     191             : 
     192           0 :   gpgme_release (ctx);
     193           0 :   return 0;
     194             : }

Generated by: LCOV version 1.11