LCOV - code coverage report
Current view: top level - sm - gpgsm.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 213 928 23.0 %
Date: 2015-11-05 17:10:59 Functions: 9 25 36.0 %

          Line data    Source code
       1             : /* gpgsm.c - GnuPG for S/MIME
       2             :  * Copyright (C) 2001-2008, 2010  Free Software Foundation, Inc.
       3             :  * Copyright (C) 2001-2008, 2010  Werner Koch
       4             :  *
       5             :  * This file is part of GnuPG.
       6             :  *
       7             :  * GnuPG is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the GNU General Public License as published by
       9             :  * the Free Software Foundation; either version 3 of the License, or
      10             :  * (at your option) any later version.
      11             :  *
      12             :  * GnuPG is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  * GNU General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License
      18             :  * along with this program; if not, see <http://www.gnu.org/licenses/>.
      19             :  */
      20             : 
      21             : #include <config.h>
      22             : #include <errno.h>
      23             : #include <stdio.h>
      24             : #include <stdlib.h>
      25             : #include <string.h>
      26             : #include <ctype.h>
      27             : #include <unistd.h>
      28             : #include <fcntl.h>
      29             : /*#include <mcheck.h>*/
      30             : 
      31             : #include "gpgsm.h"
      32             : #include <gcrypt.h>
      33             : #include <assuan.h> /* malloc hooks */
      34             : 
      35             : #include "../kbx/keybox.h" /* malloc hooks */
      36             : #include "i18n.h"
      37             : #include "keydb.h"
      38             : #include "sysutils.h"
      39             : #include "gc-opt-flags.h"
      40             : #include "asshelp.h"
      41             : #include "../common/init.h"
      42             : 
      43             : 
      44             : #ifndef O_BINARY
      45             : #define O_BINARY 0
      46             : #endif
      47             : 
      48             : enum cmd_and_opt_values {
      49             :   aNull = 0,
      50             :   oArmor        = 'a',
      51             :   aDetachedSign = 'b',
      52             :   aSym          = 'c',
      53             :   aDecrypt      = 'd',
      54             :   aEncr         = 'e',
      55             :   aListKeys     = 'k',
      56             :   aListSecretKeys = 'K',
      57             :   oDryRun       = 'n',
      58             :   oOutput       = 'o',
      59             :   oQuiet        = 'q',
      60             :   oRecipient    = 'r',
      61             :   aSign         = 's',
      62             :   oUser         = 'u',
      63             :   oVerbose      = 'v',
      64             :   oBatch        = 500,
      65             :   aClearsign,
      66             :   aKeygen,
      67             :   aSignEncr,
      68             :   aDeleteKey,
      69             :   aImport,
      70             :   aVerify,
      71             :   aListExternalKeys,
      72             :   aListChain,
      73             :   aSendKeys,
      74             :   aRecvKeys,
      75             :   aExport,
      76             :   aExportSecretKeyP12,
      77             :   aExportSecretKeyP8,
      78             :   aExportSecretKeyRaw,
      79             :   aServer,
      80             :   aLearnCard,
      81             :   aCallDirmngr,
      82             :   aCallProtectTool,
      83             :   aPasswd,
      84             :   aGPGConfList,
      85             :   aGPGConfTest,
      86             :   aDumpKeys,
      87             :   aDumpChain,
      88             :   aDumpSecretKeys,
      89             :   aDumpExternalKeys,
      90             :   aKeydbClearSomeCertFlags,
      91             :   aFingerprint,
      92             : 
      93             :   oOptions,
      94             :   oDebug,
      95             :   oDebugLevel,
      96             :   oDebugAll,
      97             :   oDebugNone,
      98             :   oDebugWait,
      99             :   oDebugAllowCoreDump,
     100             :   oDebugNoChainValidation,
     101             :   oDebugIgnoreExpiration,
     102             :   oFixedPassphrase,
     103             :   oLogFile,
     104             :   oNoLogFile,
     105             :   oAuditLog,
     106             :   oHtmlAuditLog,
     107             : 
     108             :   oEnableSpecialFilenames,
     109             : 
     110             :   oAgentProgram,
     111             :   oDisplay,
     112             :   oTTYname,
     113             :   oTTYtype,
     114             :   oLCctype,
     115             :   oLCmessages,
     116             :   oXauthority,
     117             : 
     118             :   oPreferSystemDirmngr,
     119             :   oDirmngrProgram,
     120             :   oDisableDirmngr,
     121             :   oProtectToolProgram,
     122             :   oFakedSystemTime,
     123             : 
     124             : 
     125             :   oAssumeArmor,
     126             :   oAssumeBase64,
     127             :   oAssumeBinary,
     128             : 
     129             :   oBase64,
     130             :   oNoArmor,
     131             :   oP12Charset,
     132             : 
     133             :   oDisableCRLChecks,
     134             :   oEnableCRLChecks,
     135             :   oDisableTrustedCertCRLCheck,
     136             :   oEnableTrustedCertCRLCheck,
     137             :   oForceCRLRefresh,
     138             : 
     139             :   oDisableOCSP,
     140             :   oEnableOCSP,
     141             : 
     142             :   oIncludeCerts,
     143             :   oPolicyFile,
     144             :   oDisablePolicyChecks,
     145             :   oEnablePolicyChecks,
     146             :   oAutoIssuerKeyRetrieve,
     147             : 
     148             :   oWithFingerprint,
     149             :   oWithMD5Fingerprint,
     150             :   oWithKeygrip,
     151             :   oWithSecret,
     152             :   oAnswerYes,
     153             :   oAnswerNo,
     154             :   oKeyring,
     155             :   oDefaultKey,
     156             :   oDefRecipient,
     157             :   oDefRecipientSelf,
     158             :   oNoDefRecipient,
     159             :   oStatusFD,
     160             :   oCipherAlgo,
     161             :   oDigestAlgo,
     162             :   oExtraDigestAlgo,
     163             :   oNoVerbose,
     164             :   oNoSecmemWarn,
     165             :   oNoDefKeyring,
     166             :   oNoGreeting,
     167             :   oNoTTY,
     168             :   oNoOptions,
     169             :   oNoBatch,
     170             :   oHomedir,
     171             :   oWithColons,
     172             :   oWithKeyData,
     173             :   oWithValidation,
     174             :   oWithEphemeralKeys,
     175             :   oSkipVerify,
     176             :   oValidationModel,
     177             :   oKeyServer,
     178             :   oEncryptTo,
     179             :   oNoEncryptTo,
     180             :   oLoggerFD,
     181             :   oDisableCipherAlgo,
     182             :   oDisablePubkeyAlgo,
     183             :   oIgnoreTimeConflict,
     184             :   oNoRandomSeedFile,
     185             :   oNoCommonCertsImport,
     186             :   oIgnoreCertExtension,
     187             :   oNoAutostart
     188             :  };
     189             : 
     190             : 
     191             : static ARGPARSE_OPTS opts[] = {
     192             : 
     193             :   ARGPARSE_group (300, N_("@Commands:\n ")),
     194             : 
     195             :   ARGPARSE_c (aSign, "sign", N_("make a signature")),
     196             : /*ARGPARSE_c (aClearsign, "clearsign", N_("make a clear text signature") ),*/
     197             :   ARGPARSE_c (aDetachedSign, "detach-sign", N_("make a detached signature")),
     198             :   ARGPARSE_c (aEncr, "encrypt", N_("encrypt data")),
     199             : /*ARGPARSE_c (aSym, "symmetric", N_("encryption only with symmetric cipher")),*/
     200             :   ARGPARSE_c (aDecrypt, "decrypt", N_("decrypt data (default)")),
     201             :   ARGPARSE_c (aVerify, "verify",  N_("verify a signature")),
     202             :   ARGPARSE_c (aListKeys, "list-keys", N_("list keys")),
     203             :   ARGPARSE_c (aListExternalKeys, "list-external-keys",
     204             :               N_("list external keys")),
     205             :   ARGPARSE_c (aListSecretKeys, "list-secret-keys", N_("list secret keys")),
     206             :   ARGPARSE_c (aListChain,   "list-chain",  N_("list certificate chain")),
     207             :   ARGPARSE_c (aFingerprint, "fingerprint", N_("list keys and fingerprints")),
     208             :   ARGPARSE_c (aKeygen, "gen-key", N_("generate a new key pair")),
     209             :   ARGPARSE_c (aDeleteKey, "delete-keys",
     210             :               N_("remove keys from the public keyring")),
     211             : /*ARGPARSE_c (aSendKeys, "send-keys", N_("export keys to a key server")),*/
     212             : /*ARGPARSE_c (aRecvKeys, "recv-keys", N_("import keys from a key server")),*/
     213             :   ARGPARSE_c (aImport, "import", N_("import certificates")),
     214             :   ARGPARSE_c (aExport, "export", N_("export certificates")),
     215             : 
     216             :   /* We use -raw and not -p1 for pkcs#1 secret key export so that it
     217             :      won't accidently be used in case -p12 was intended.  */
     218             :   ARGPARSE_c (aExportSecretKeyP12, "export-secret-key-p12", "@"),
     219             :   ARGPARSE_c (aExportSecretKeyP8,  "export-secret-key-p8", "@"),
     220             :   ARGPARSE_c (aExportSecretKeyRaw, "export-secret-key-raw", "@"),
     221             : 
     222             :   ARGPARSE_c (aLearnCard, "learn-card", N_("register a smartcard")),
     223             :   ARGPARSE_c (aServer, "server", N_("run in server mode")),
     224             :   ARGPARSE_c (aCallDirmngr, "call-dirmngr",
     225             :               N_("pass a command to the dirmngr")),
     226             :   ARGPARSE_c (aCallProtectTool, "call-protect-tool",
     227             :               N_("invoke gpg-protect-tool")),
     228             :   ARGPARSE_c (aPasswd, "passwd", N_("change a passphrase")),
     229             :   ARGPARSE_c (aGPGConfList, "gpgconf-list", "@"),
     230             :   ARGPARSE_c (aGPGConfTest, "gpgconf-test", "@"),
     231             : 
     232             :   ARGPARSE_c (aDumpKeys, "dump-cert", "@"),
     233             :   ARGPARSE_c (aDumpKeys, "dump-keys", "@"),
     234             :   ARGPARSE_c (aDumpChain, "dump-chain", "@"),
     235             :   ARGPARSE_c (aDumpExternalKeys, "dump-external-keys", "@"),
     236             :   ARGPARSE_c (aDumpSecretKeys, "dump-secret-keys", "@"),
     237             :   ARGPARSE_c (aKeydbClearSomeCertFlags, "keydb-clear-some-cert-flags", "@"),
     238             : 
     239             :   ARGPARSE_group (301, N_("@\nOptions:\n ")),
     240             : 
     241             :   ARGPARSE_s_n (oArmor, "armor", N_("create ascii armored output")),
     242             :   ARGPARSE_s_n (oArmor, "armour", "@"),
     243             :   ARGPARSE_s_n (oBase64, "base64", N_("create base-64 encoded output")),
     244             : 
     245             :   ARGPARSE_s_s (oP12Charset, "p12-charset", "@"),
     246             : 
     247             :   ARGPARSE_s_n (oAssumeArmor, "assume-armor",
     248             :                 N_("assume input is in PEM format")),
     249             :   ARGPARSE_s_n (oAssumeBase64, "assume-base64",
     250             :                 N_("assume input is in base-64 format")),
     251             :   ARGPARSE_s_n (oAssumeBinary, "assume-binary",
     252             :                 N_("assume input is in binary format")),
     253             : 
     254             :   ARGPARSE_s_s (oRecipient, "recipient", N_("|USER-ID|encrypt for USER-ID")),
     255             : 
     256             :   ARGPARSE_s_n (oPreferSystemDirmngr,"prefer-system-dirmngr", "@"),
     257             : 
     258             :   ARGPARSE_s_n (oDisableCRLChecks, "disable-crl-checks",
     259             :                 N_("never consult a CRL")),
     260             :   ARGPARSE_s_n (oEnableCRLChecks, "enable-crl-checks", "@"),
     261             :   ARGPARSE_s_n (oDisableTrustedCertCRLCheck,
     262             :                 "disable-trusted-cert-crl-check", "@"),
     263             :   ARGPARSE_s_n (oEnableTrustedCertCRLCheck,
     264             :                 "enable-trusted-cert-crl-check", "@"),
     265             : 
     266             :   ARGPARSE_s_n (oForceCRLRefresh, "force-crl-refresh", "@"),
     267             : 
     268             :   ARGPARSE_s_n (oDisableOCSP, "disable-ocsp", "@"),
     269             :   ARGPARSE_s_n (oEnableOCSP,  "enable-ocsp", N_("check validity using OCSP")),
     270             : 
     271             :   ARGPARSE_s_s (oValidationModel, "validation-model", "@"),
     272             : 
     273             :   ARGPARSE_s_i (oIncludeCerts, "include-certs",
     274             :                 N_("|N|number of certificates to include") ),
     275             : 
     276             :   ARGPARSE_s_s (oPolicyFile, "policy-file",
     277             :                 N_("|FILE|take policy information from FILE")),
     278             : 
     279             :   ARGPARSE_s_n (oDisablePolicyChecks, "disable-policy-checks",
     280             :                 N_("do not check certificate policies")),
     281             :   ARGPARSE_s_n (oEnablePolicyChecks, "enable-policy-checks", "@"),
     282             : 
     283             :   ARGPARSE_s_n (oAutoIssuerKeyRetrieve, "auto-issuer-key-retrieve",
     284             :                 N_("fetch missing issuer certificates")),
     285             : 
     286             :   ARGPARSE_s_s (oEncryptTo, "encrypt-to", "@"),
     287             :   ARGPARSE_s_n (oNoEncryptTo, "no-encrypt-to", "@"),
     288             : 
     289             :   ARGPARSE_s_s (oUser, "local-user",
     290             :                 N_("|USER-ID|use USER-ID to sign or decrypt")),
     291             : 
     292             :   ARGPARSE_s_s (oOutput, "output", N_("|FILE|write output to FILE")),
     293             :   ARGPARSE_s_n (oVerbose, "verbose", N_("verbose")),
     294             :   ARGPARSE_s_n (oQuiet, "quiet",  N_("be somewhat more quiet")),
     295             :   ARGPARSE_s_n (oNoTTY, "no-tty", N_("don't use the terminal at all")),
     296             :   ARGPARSE_s_s (oLogFile, "log-file",
     297             :                 N_("|FILE|write a server mode log to FILE")),
     298             :   ARGPARSE_s_n (oNoLogFile, "no-log-file", "@"),
     299             :   ARGPARSE_s_i (oLoggerFD, "logger-fd", "@"),
     300             : 
     301             :   ARGPARSE_s_s (oAuditLog, "audit-log",
     302             :                 N_("|FILE|write an audit log to FILE")),
     303             :   ARGPARSE_s_s (oHtmlAuditLog, "html-audit-log", "@"),
     304             :   ARGPARSE_s_n (oDryRun, "dry-run", N_("do not make any changes")),
     305             :   ARGPARSE_s_n (oBatch, "batch", N_("batch mode: never ask")),
     306             :   ARGPARSE_s_n (oAnswerYes, "yes", N_("assume yes on most questions")),
     307             :   ARGPARSE_s_n (oAnswerNo,  "no",  N_("assume no on most questions")),
     308             : 
     309             :   ARGPARSE_s_s (oKeyring, "keyring",
     310             :                 N_("|FILE|add keyring to the list of keyrings")),
     311             : 
     312             :   ARGPARSE_s_s (oDefaultKey, "default-key",
     313             :                 N_("|USER-ID|use USER-ID as default secret key")),
     314             : 
     315             :   /* Not yet used: */
     316             :   /*   ARGPARSE_s_s (oDefRecipient, "default-recipient", */
     317             :   /*                  N_("|NAME|use NAME as default recipient")), */
     318             :   /*   ARGPARSE_s_n (oDefRecipientSelf, "default-recipient-self", */
     319             :   /*                  N_("use the default key as default recipient")), */
     320             :   /*   ARGPARSE_s_n (oNoDefRecipient, "no-default-recipient", "@"), */
     321             : 
     322             :   ARGPARSE_s_s (oKeyServer, "keyserver",
     323             :                 N_("|SPEC|use this keyserver to lookup keys")),
     324             :   ARGPARSE_s_s (oOptions, "options", N_("|FILE|read options from FILE")),
     325             : 
     326             :   ARGPARSE_s_s (oDebug, "debug", "@"),
     327             :   ARGPARSE_s_s (oDebugLevel, "debug-level",
     328             :                 N_("|LEVEL|set the debugging level to LEVEL")),
     329             :   ARGPARSE_s_n (oDebugAll, "debug-all", "@"),
     330             :   ARGPARSE_s_n (oDebugNone, "debug-none", "@"),
     331             :   ARGPARSE_s_i (oDebugWait, "debug-wait", "@"),
     332             :   ARGPARSE_s_n (oDebugAllowCoreDump, "debug-allow-core-dump", "@"),
     333             :   ARGPARSE_s_n (oDebugNoChainValidation, "debug-no-chain-validation", "@"),
     334             :   ARGPARSE_s_n (oDebugIgnoreExpiration,  "debug-ignore-expiration", "@"),
     335             :   ARGPARSE_s_s (oFixedPassphrase, "fixed-passphrase", "@"),
     336             : 
     337             :   ARGPARSE_s_i (oStatusFD, "status-fd",
     338             :                 N_("|FD|write status info to this FD")),
     339             : 
     340             :   ARGPARSE_s_s (oCipherAlgo, "cipher-algo",
     341             :                 N_("|NAME|use cipher algorithm NAME")),
     342             :   ARGPARSE_s_s (oDigestAlgo, "digest-algo",
     343             :                 N_("|NAME|use message digest algorithm NAME")),
     344             :   ARGPARSE_s_s (oExtraDigestAlgo, "extra-digest-algo", "@"),
     345             : 
     346             : 
     347             :   ARGPARSE_group (302, N_(
     348             :   "@\n(See the man page for a complete listing of all commands and options)\n"
     349             :   )),
     350             : 
     351             :   ARGPARSE_group (303, N_("@\nExamples:\n\n"
     352             :     " -se -r Bob [file]          sign and encrypt for user Bob\n"
     353             :     " --clearsign [file]         make a clear text signature\n"
     354             :     " --detach-sign [file]       make a detached signature\n"
     355             :     " --list-keys [names]        show keys\n"
     356             :     " --fingerprint [names]      show fingerprints\n"  )),
     357             : 
     358             :   /* Hidden options. */
     359             :   ARGPARSE_s_n (oNoVerbose, "no-verbose", "@"),
     360             :   ARGPARSE_s_n (oEnableSpecialFilenames, "enable-special-filenames", "@"),
     361             :   ARGPARSE_s_n (oNoSecmemWarn, "no-secmem-warning", "@"),
     362             :   ARGPARSE_s_n (oNoArmor, "no-armor", "@"),
     363             :   ARGPARSE_s_n (oNoArmor, "no-armour", "@"),
     364             :   ARGPARSE_s_n (oNoDefKeyring, "no-default-keyring", "@"),
     365             :   ARGPARSE_s_n (oNoGreeting, "no-greeting", "@"),
     366             :   ARGPARSE_s_n (oNoOptions, "no-options", "@"),
     367             :   ARGPARSE_s_s (oHomedir, "homedir", "@"),
     368             :   ARGPARSE_s_s (oAgentProgram, "agent-program", "@"),
     369             :   ARGPARSE_s_s (oDisplay,    "display", "@"),
     370             :   ARGPARSE_s_s (oTTYname,    "ttyname", "@"),
     371             :   ARGPARSE_s_s (oTTYtype,    "ttytype", "@"),
     372             :   ARGPARSE_s_s (oLCctype,    "lc-ctype", "@"),
     373             :   ARGPARSE_s_s (oLCmessages, "lc-messages", "@"),
     374             :   ARGPARSE_s_s (oXauthority, "xauthority", "@"),
     375             :   ARGPARSE_s_s (oDirmngrProgram, "dirmngr-program", "@"),
     376             :   ARGPARSE_s_n (oDisableDirmngr, "disable-dirmngr", "@"),
     377             :   ARGPARSE_s_s (oProtectToolProgram, "protect-tool-program", "@"),
     378             :   ARGPARSE_s_s (oFakedSystemTime, "faked-system-time", "@"),
     379             :   ARGPARSE_s_n (oNoBatch, "no-batch", "@"),
     380             :   ARGPARSE_s_n (oWithColons, "with-colons", "@"),
     381             :   ARGPARSE_s_n (oWithKeyData,"with-key-data", "@"),
     382             :   ARGPARSE_s_n (oWithValidation, "with-validation", "@"),
     383             :   ARGPARSE_s_n (oWithMD5Fingerprint, "with-md5-fingerprint", "@"),
     384             :   ARGPARSE_s_n (oWithEphemeralKeys,  "with-ephemeral-keys", "@"),
     385             :   ARGPARSE_s_n (oSkipVerify, "skip-verify", "@"),
     386             :   ARGPARSE_s_n (oWithFingerprint, "with-fingerprint", "@"),
     387             :   ARGPARSE_s_n (oWithKeygrip,     "with-keygrip", "@"),
     388             :   ARGPARSE_s_n (oWithSecret,      "with-secret", "@"),
     389             :   ARGPARSE_s_s (oDisableCipherAlgo,  "disable-cipher-algo", "@"),
     390             :   ARGPARSE_s_s (oDisablePubkeyAlgo,  "disable-pubkey-algo", "@"),
     391             :   ARGPARSE_s_n (oIgnoreTimeConflict, "ignore-time-conflict", "@"),
     392             :   ARGPARSE_s_n (oNoRandomSeedFile,  "no-random-seed-file", "@"),
     393             :   ARGPARSE_s_n (oNoCommonCertsImport, "no-common-certs-import", "@"),
     394             :   ARGPARSE_s_s (oIgnoreCertExtension, "ignore-cert-extension", "@"),
     395             :   ARGPARSE_s_n (oNoAutostart, "no-autostart", "@"),
     396             : 
     397             :   /* Command aliases.  */
     398             :   ARGPARSE_c (aListKeys, "list-key", "@"),
     399             :   ARGPARSE_c (aListChain, "list-sig", "@"),
     400             :   ARGPARSE_c (aListChain, "list-sigs", "@"),
     401             :   ARGPARSE_c (aListChain, "check-sig", "@"),
     402             :   ARGPARSE_c (aListChain, "check-sigs", "@"),
     403             :   ARGPARSE_c (aDeleteKey, "delete-key", "@"),
     404             : 
     405             :   ARGPARSE_end ()
     406             : };
     407             : 
     408             : 
     409             : /* The list of supported debug flags.  */
     410             : static struct debug_flags_s debug_flags [] =
     411             :   {
     412             :     { DBG_X509_VALUE   , "x509"    },
     413             :     { DBG_MPI_VALUE    , "mpi"     },
     414             :     { DBG_CRYPTO_VALUE , "crypto"  },
     415             :     { DBG_MEMORY_VALUE , "memory"  },
     416             :     { DBG_CACHE_VALUE  , "cache"   },
     417             :     { DBG_MEMSTAT_VALUE, "memstat" },
     418             :     { DBG_HASHING_VALUE, "hashing" },
     419             :     { DBG_IPC_VALUE    , "ipc"     },
     420             :     { 0, NULL }
     421             :   };
     422             : 
     423             : 
     424             : /* Global variable to keep an error count. */
     425             : int gpgsm_errors_seen = 0;
     426             : 
     427             : /* It is possible that we are currentlu running under setuid permissions */
     428             : static int maybe_setuid = 1;
     429             : 
     430             : /* Helper to implement --debug-level and --debug*/
     431             : static const char *debug_level;
     432             : static unsigned int debug_value;
     433             : 
     434             : /* Option --enable-special-filenames */
     435             : static int allow_special_filenames;
     436             : 
     437             : /* Default value for include-certs.  We need an extra macro for
     438             :    gpgconf-list because the variable will be changed by the command
     439             :    line option.
     440             : 
     441             :    It is often cumbersome to locate intermediate certificates, thus by
     442             :    default we include all certificates in the chain.  However we leave
     443             :    out the root certificate because that would make it too easy for
     444             :    the recipient to import that root certificate.  A root certificate
     445             :    should be installed only after due checks and thus it won't help to
     446             :    send it along with each message.  */
     447             : #define DEFAULT_INCLUDE_CERTS -2 /* Include all certs but root. */
     448             : static int default_include_certs = DEFAULT_INCLUDE_CERTS;
     449             : 
     450             : /* Whether the chain mode shall be used for validation.  */
     451             : static int default_validation_model;
     452             : 
     453             : /* The default cipher algo.  */
     454             : #define DEFAULT_CIPHER_ALGO "AES"
     455             : 
     456             : 
     457             : static char *build_list (const char *text,
     458             :                          const char *(*mapf)(int), int (*chkf)(int));
     459             : static void set_cmd (enum cmd_and_opt_values *ret_cmd,
     460             :                      enum cmd_and_opt_values new_cmd );
     461             : 
     462             : static void emergency_cleanup (void);
     463             : static int check_special_filename (const char *fname, int for_write);
     464             : static int open_read (const char *filename);
     465             : static estream_t open_es_fread (const char *filename, const char *mode);
     466             : static estream_t open_es_fwrite (const char *filename);
     467             : static void run_protect_tool (int argc, char **argv);
     468             : 
     469             : static int
     470           0 : our_pk_test_algo (int algo)
     471             : {
     472           0 :   switch (algo)
     473             :     {
     474             :     case GCRY_PK_RSA:
     475             :     case GCRY_PK_ECDSA:
     476           0 :       return gcry_pk_test_algo (algo);
     477             :     default:
     478           0 :       return 1;
     479             :     }
     480             : }
     481             : 
     482             : static int
     483           0 : our_cipher_test_algo (int algo)
     484             : {
     485           0 :   switch (algo)
     486             :     {
     487             :     case GCRY_CIPHER_3DES:
     488             :     case GCRY_CIPHER_AES128:
     489             :     case GCRY_CIPHER_AES192:
     490             :     case GCRY_CIPHER_AES256:
     491             :     case GCRY_CIPHER_SERPENT128:
     492             :     case GCRY_CIPHER_SERPENT192:
     493             :     case GCRY_CIPHER_SERPENT256:
     494             :     case GCRY_CIPHER_SEED:
     495             :     case GCRY_CIPHER_CAMELLIA128:
     496             :     case GCRY_CIPHER_CAMELLIA192:
     497             :     case GCRY_CIPHER_CAMELLIA256:
     498           0 :       return gcry_cipher_test_algo (algo);
     499             :     default:
     500           0 :       return 1;
     501             :     }
     502             : }
     503             : 
     504             : 
     505             : static int
     506           0 : our_md_test_algo (int algo)
     507             : {
     508           0 :   switch (algo)
     509             :     {
     510             :     case GCRY_MD_MD5:
     511             :     case GCRY_MD_SHA1:
     512             :     case GCRY_MD_RMD160:
     513             :     case GCRY_MD_SHA224:
     514             :     case GCRY_MD_SHA256:
     515             :     case GCRY_MD_SHA384:
     516             :     case GCRY_MD_SHA512:
     517             :     case GCRY_MD_WHIRLPOOL:
     518           0 :       return gcry_md_test_algo (algo);
     519             :     default:
     520           0 :       return 1;
     521             :     }
     522             : }
     523             : 
     524             : 
     525             : static char *
     526           0 : make_libversion (const char *libname, const char *(*getfnc)(const char*))
     527             : {
     528             :   const char *s;
     529             :   char *result;
     530             : 
     531           0 :   if (maybe_setuid)
     532             :     {
     533           0 :       gcry_control (GCRYCTL_INIT_SECMEM, 0, 0);  /* Drop setuid. */
     534           0 :       maybe_setuid = 0;
     535             :     }
     536           0 :   s = getfnc (NULL);
     537           0 :   result = xmalloc (strlen (libname) + 1 + strlen (s) + 1);
     538           0 :   strcpy (stpcpy (stpcpy (result, libname), " "), s);
     539           0 :   return result;
     540             : }
     541             : 
     542             : 
     543             : static const char *
     544           0 : my_strusage( int level )
     545             : {
     546             :   static char *digests, *pubkeys, *ciphers;
     547             :   static char *ver_gcry, *ver_ksba;
     548             :   const char *p;
     549             : 
     550           0 :   switch (level)
     551             :     {
     552           0 :     case 11: p = "@GPGSM@ (@GNUPG@)";
     553           0 :       break;
     554           0 :     case 13: p = VERSION; break;
     555           0 :     case 17: p = PRINTABLE_OS_NAME; break;
     556           0 :     case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
     557             : 
     558             :     case 1:
     559           0 :     case 40: p = _("Usage: @GPGSM@ [options] [files] (-h for help)");
     560           0 :       break;
     561             :     case 41:
     562           0 :       p = _("Syntax: @GPGSM@ [options] [files]\n"
     563             :             "Sign, check, encrypt or decrypt using the S/MIME protocol\n"
     564             :             "Default operation depends on the input data\n");
     565           0 :       break;
     566             : 
     567             :     case 20:
     568           0 :       if (!ver_gcry)
     569           0 :         ver_gcry = make_libversion ("libgcrypt", gcry_check_version);
     570           0 :       p = ver_gcry;
     571           0 :       break;
     572             :     case 21:
     573           0 :       if (!ver_ksba)
     574           0 :         ver_ksba = make_libversion ("libksba", ksba_check_version);
     575           0 :       p = ver_ksba;
     576           0 :       break;
     577             : 
     578           0 :     case 31: p = "\nHome: "; break;
     579           0 :     case 32: p = opt.homedir; break;
     580           0 :     case 33: p = _("\nSupported algorithms:\n"); break;
     581             :     case 34:
     582           0 :       if (!ciphers)
     583           0 :         ciphers = build_list ("Cipher: ", gnupg_cipher_algo_name,
     584             :                               our_cipher_test_algo );
     585           0 :       p = ciphers;
     586           0 :       break;
     587             :     case 35:
     588           0 :       if (!pubkeys)
     589           0 :         pubkeys = build_list ("Pubkey: ", gcry_pk_algo_name,
     590             :                               our_pk_test_algo );
     591           0 :       p = pubkeys;
     592           0 :       break;
     593             :     case 36:
     594           0 :       if (!digests)
     595           0 :         digests = build_list("Hash: ", gcry_md_algo_name, our_md_test_algo );
     596           0 :       p = digests;
     597           0 :       break;
     598             : 
     599           0 :     default: p = NULL; break;
     600             :     }
     601           0 :   return p;
     602             : }
     603             : 
     604             : 
     605             : static char *
     606           0 : build_list (const char *text, const char * (*mapf)(int), int (*chkf)(int))
     607             : {
     608             :   int i;
     609           0 :   size_t n=strlen(text)+2;
     610             :   char *list, *p;
     611             : 
     612           0 :   if (maybe_setuid) {
     613           0 :     gcry_control (GCRYCTL_DROP_PRIVS); /* drop setuid */
     614             :   }
     615             : 
     616           0 :   for (i=1; i < 400; i++ )
     617           0 :     if (!chkf(i))
     618           0 :       n += strlen(mapf(i)) + 2;
     619           0 :   list = xmalloc (21 + n);
     620           0 :   *list = 0;
     621           0 :   for (p=NULL, i=1; i < 400; i++)
     622             :     {
     623           0 :       if (!chkf(i))
     624             :         {
     625           0 :           if( !p )
     626           0 :             p = stpcpy (list, text );
     627             :           else
     628           0 :             p = stpcpy (p, ", ");
     629           0 :           p = stpcpy (p, mapf(i) );
     630             :         }
     631             :     }
     632           0 :   if (p)
     633           0 :     p = stpcpy(p, "\n" );
     634           0 :   return list;
     635             : }
     636             : 
     637             : 
     638             : /* Set the file pointer into binary mode if required.  */
     639             : static void
     640           0 : set_binary (FILE *fp)
     641             : {
     642             : #ifdef HAVE_DOSISH_SYSTEM
     643             :   setmode (fileno (fp), O_BINARY);
     644             : #else
     645             :   (void)fp;
     646             : #endif
     647           0 : }
     648             : 
     649             : 
     650             : 
     651             : static void
     652           0 : wrong_args (const char *text)
     653             : {
     654           0 :   fprintf (stderr, _("usage: %s [options] %s\n"), GPGSM_NAME, text);
     655           0 :   gpgsm_exit (2);
     656           0 : }
     657             : 
     658             : 
     659             : static void
     660           0 : set_opt_session_env (const char *name, const char *value)
     661             : {
     662             :   gpg_error_t err;
     663             : 
     664           0 :   err = session_env_setenv (opt.session_env, name, value);
     665           0 :   if (err)
     666           0 :     log_fatal ("error setting session environment: %s\n",
     667             :                gpg_strerror (err));
     668           0 : }
     669             : 
     670             : 
     671             : /* Setup the debugging.  With a DEBUG_LEVEL of NULL only the active
     672             :    debug flags are propagated to the subsystems.  With DEBUG_LEVEL
     673             :    set, a specific set of debug flags is set; and individual debugging
     674             :    flags will be added on top.  */
     675             : static void
     676           3 : set_debug (void)
     677             : {
     678           3 :   int numok = (debug_level && digitp (debug_level));
     679           3 :   int numlvl = numok? atoi (debug_level) : 0;
     680             : 
     681           3 :   if (!debug_level)
     682             :     ;
     683           0 :   else if (!strcmp (debug_level, "none") || (numok && numlvl < 1))
     684           0 :     opt.debug = 0;
     685           0 :   else if (!strcmp (debug_level, "basic") || (numok && numlvl <= 2))
     686           0 :     opt.debug = DBG_IPC_VALUE;
     687           0 :   else if (!strcmp (debug_level, "advanced") || (numok && numlvl <= 5))
     688           0 :     opt.debug = DBG_IPC_VALUE|DBG_X509_VALUE;
     689           0 :   else if (!strcmp (debug_level, "expert")  || (numok && numlvl <= 8))
     690           0 :     opt.debug = (DBG_IPC_VALUE|DBG_X509_VALUE
     691             :                  |DBG_CACHE_VALUE|DBG_CRYPTO_VALUE);
     692           0 :   else if (!strcmp (debug_level, "guru") || numok)
     693             :     {
     694           0 :       opt.debug = ~0;
     695             :       /* Unless the "guru" string has been used we don't want to allow
     696             :          hashing debugging.  The rationale is that people tend to
     697             :          select the highest debug value and would then clutter their
     698             :          disk with debug files which may reveal confidential data.  */
     699           0 :       if (numok)
     700           0 :         opt.debug &= ~(DBG_HASHING_VALUE);
     701             :     }
     702             :   else
     703             :     {
     704           0 :       log_error (_("invalid debug-level '%s' given\n"), debug_level);
     705           0 :       gpgsm_exit (2);
     706             :     }
     707             : 
     708           3 :   opt.debug |= debug_value;
     709             : 
     710           3 :   if (opt.debug && !opt.verbose)
     711           0 :     opt.verbose = 1;
     712           3 :   if (opt.debug)
     713           0 :     opt.quiet = 0;
     714             : 
     715           3 :   if (opt.debug & DBG_MPI_VALUE)
     716           0 :     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 2);
     717           3 :   if (opt.debug & DBG_CRYPTO_VALUE )
     718           0 :     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1);
     719           3 :   gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose);
     720             : 
     721           3 :   if (opt.debug)
     722           0 :     parse_debug_flag (NULL, &opt.debug, debug_flags);
     723           3 : }
     724             : 
     725             : 
     726             : 
     727             : static void
     728           3 : set_cmd (enum cmd_and_opt_values *ret_cmd, enum cmd_and_opt_values new_cmd)
     729             : {
     730           3 :   enum cmd_and_opt_values cmd = *ret_cmd;
     731             : 
     732           3 :   if (!cmd || cmd == new_cmd)
     733           3 :     cmd = new_cmd;
     734           0 :   else if ( cmd == aSign && new_cmd == aEncr )
     735           0 :     cmd = aSignEncr;
     736           0 :   else if ( cmd == aEncr && new_cmd == aSign )
     737           0 :     cmd = aSignEncr;
     738           0 :   else if ( (cmd == aSign && new_cmd == aClearsign)
     739           0 :             || (cmd == aClearsign && new_cmd == aSign) )
     740           0 :     cmd = aClearsign;
     741             :   else
     742             :     {
     743           0 :       log_error(_("conflicting commands\n"));
     744           0 :       gpgsm_exit(2);
     745             :     }
     746             : 
     747           3 :   *ret_cmd = cmd;
     748           3 : }
     749             : 
     750             : 
     751             : /* Helper to add recipients to a list. */
     752             : static void
     753           0 : do_add_recipient (ctrl_t ctrl, const char *name,
     754             :                   certlist_t *recplist, int is_encrypt_to, int recp_required)
     755             : {
     756           0 :   int rc = gpgsm_add_to_certlist (ctrl, name, 0, recplist, is_encrypt_to);
     757           0 :   if (rc)
     758             :     {
     759           0 :       if (recp_required)
     760             :         {
     761           0 :           log_error ("can't encrypt to '%s': %s\n", name, gpg_strerror (rc));
     762           0 :           gpgsm_status2 (ctrl, STATUS_INV_RECP,
     763             :                          get_inv_recpsgnr_code (rc), name, NULL);
     764             :         }
     765             :       else
     766           0 :         log_info (_("Note: won't be able to encrypt to '%s': %s\n"),
     767             :                   name, gpg_strerror (rc));
     768             :     }
     769           0 : }
     770             : 
     771             : 
     772             : static void
     773           0 : parse_validation_model (const char *model)
     774             : {
     775           0 :   int i = gpgsm_parse_validation_model (model);
     776           0 :   if (i == -1)
     777           0 :     log_error (_("unknown validation model '%s'\n"), model);
     778             :   else
     779           0 :     default_validation_model = i;
     780           0 : }
     781             : 
     782             : 
     783             : /* Release the list of SERVERS.  As usual it is okay to call this
     784             :    function with SERVERS passed as NULL.  */
     785             : void
     786           3 : keyserver_list_free (struct keyserver_spec *servers)
     787             : {
     788           6 :   while (servers)
     789             :     {
     790           0 :       struct keyserver_spec *tmp = servers->next;
     791           0 :       xfree (servers->host);
     792           0 :       xfree (servers->user);
     793           0 :       if (servers->pass)
     794           0 :         memset (servers->pass, 0, strlen (servers->pass));
     795           0 :       xfree (servers->pass);
     796           0 :       xfree (servers->base);
     797           0 :       xfree (servers);
     798           0 :       servers = tmp;
     799             :     }
     800           3 : }
     801             : 
     802             : /* See also dirmngr ldapserver_parse_one().  */
     803             : struct keyserver_spec *
     804           0 : parse_keyserver_line (char *line,
     805             :                       const char *filename, unsigned int lineno)
     806             : {
     807             :   char *p;
     808             :   char *endp;
     809             :   struct keyserver_spec *server;
     810             :   int fieldno;
     811           0 :   int fail = 0;
     812             : 
     813             :   /* Parse the colon separated fields.  */
     814           0 :   server = xcalloc (1, sizeof *server);
     815           0 :   for (fieldno = 1, p = line; p; p = endp, fieldno++ )
     816             :     {
     817           0 :       endp = strchr (p, ':');
     818           0 :       if (endp)
     819           0 :         *endp++ = '\0';
     820           0 :       trim_spaces (p);
     821           0 :       switch (fieldno)
     822             :         {
     823             :         case 1:
     824           0 :           if (*p)
     825           0 :             server->host = xstrdup (p);
     826             :           else
     827             :             {
     828           0 :               log_error (_("%s:%u: no hostname given\n"),
     829             :                          filename, lineno);
     830           0 :               fail = 1;
     831             :             }
     832           0 :           break;
     833             : 
     834             :         case 2:
     835           0 :           if (*p)
     836           0 :             server->port = atoi (p);
     837           0 :           break;
     838             : 
     839             :         case 3:
     840           0 :           if (*p)
     841           0 :             server->user = xstrdup (p);
     842           0 :           break;
     843             : 
     844             :         case 4:
     845           0 :           if (*p && !server->user)
     846             :             {
     847           0 :               log_error (_("%s:%u: password given without user\n"),
     848             :                          filename, lineno);
     849           0 :               fail = 1;
     850             :             }
     851           0 :           else if (*p)
     852           0 :             server->pass = xstrdup (p);
     853           0 :           break;
     854             : 
     855             :         case 5:
     856           0 :           if (*p)
     857           0 :             server->base = xstrdup (p);
     858           0 :           break;
     859             : 
     860             :         default:
     861             :           /* (We silently ignore extra fields.) */
     862           0 :           break;
     863             :         }
     864             :     }
     865             : 
     866           0 :   if (fail)
     867             :     {
     868           0 :       log_info (_("%s:%u: skipping this line\n"), filename, lineno);
     869           0 :       keyserver_list_free (server);
     870           0 :       server = NULL;
     871             :     }
     872             : 
     873           0 :   return server;
     874             : }
     875             : 
     876             : 
     877             : int
     878           3 : main ( int argc, char **argv)
     879             : {
     880             :   ARGPARSE_ARGS pargs;
     881             :   int orig_argc;
     882             :   char **orig_argv;
     883             :   /*  char *username;*/
     884             :   int may_coredump;
     885           3 :   strlist_t sl, remusr= NULL, locusr=NULL;
     886           3 :   strlist_t nrings=NULL;
     887           3 :   int detached_sig = 0;
     888           3 :   FILE *configfp = NULL;
     889           3 :   char *configname = NULL;
     890             :   unsigned configlineno;
     891           3 :   int parse_debug = 0;
     892           3 :   int no_more_options = 0;
     893           3 :   int default_config =1;
     894           3 :   int default_keyring = 1;
     895           3 :   char *logfile = NULL;
     896           3 :   char *auditlog = NULL;
     897           3 :   char *htmlauditlog = NULL;
     898           3 :   int greeting = 0;
     899           3 :   int nogreeting = 0;
     900           3 :   int debug_wait = 0;
     901           3 :   int use_random_seed = 1;
     902           3 :   int no_common_certs_import = 0;
     903           3 :   int with_fpr = 0;
     904           3 :   const char *forced_digest_algo = NULL;
     905           3 :   const char *extra_digest_algo = NULL;
     906           3 :   enum cmd_and_opt_values cmd = 0;
     907             :   struct server_control_s ctrl;
     908           3 :   certlist_t recplist = NULL;
     909           3 :   certlist_t signerlist = NULL;
     910           3 :   int do_not_setup_keys = 0;
     911           3 :   int recp_required = 0;
     912           3 :   estream_t auditfp = NULL;
     913           3 :   estream_t htmlauditfp = NULL;
     914             :   struct assuan_malloc_hooks malloc_hooks;
     915             : 
     916             :   /*mtrace();*/
     917             : 
     918           3 :   early_system_init ();
     919           3 :   gnupg_reopen_std (GPGSM_NAME);
     920             :   /* trap_unaligned ();*/
     921           3 :   gnupg_rl_initialize ();
     922           3 :   set_strusage (my_strusage);
     923           3 :   gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
     924             : 
     925             :   /* Please note that we may running SUID(ROOT), so be very CAREFUL
     926             :      when adding any stuff between here and the call to secmem_init()
     927             :      somewhere after the option parsing */
     928           3 :   log_set_prefix (GPGSM_NAME, 1);
     929             : 
     930             :   /* Make sure that our subsystems are ready.  */
     931           3 :   i18n_init ();
     932           3 :   init_common_subsystems (&argc, &argv);
     933             : 
     934             :   /* Check that the libraries are suitable.  Do it here because the
     935             :      option parse may need services of the library */
     936           3 :   if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )
     937           0 :     log_fatal (_("%s is too old (need %s, have %s)\n"), "libgcrypt",
     938             :                NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL) );
     939           3 :   if (!ksba_check_version (NEED_KSBA_VERSION) )
     940           0 :     log_fatal (_("%s is too old (need %s, have %s)\n"), "libksba",
     941             :                NEED_KSBA_VERSION, ksba_check_version (NULL) );
     942             : 
     943             : 
     944           3 :   gcry_control (GCRYCTL_USE_SECURE_RNDPOOL);
     945             : 
     946           3 :   may_coredump = disable_core_dumps ();
     947             : 
     948           3 :   gnupg_init_signals (0, emergency_cleanup);
     949             : 
     950           3 :   dotlock_create (NULL, 0); /* Register lockfile cleanup.  */
     951             : 
     952           3 :   opt.autostart = 1;
     953           3 :   opt.session_env = session_env_new ();
     954           3 :   if (!opt.session_env)
     955           0 :     log_fatal ("error allocating session environment block: %s\n",
     956           0 :                strerror (errno));
     957             : 
     958             :   /* Note: If you change this default cipher algorithm , please
     959             :      remember to update the Gpgconflist entry as well.  */
     960           3 :   opt.def_cipher_algoid = DEFAULT_CIPHER_ALGO;
     961             : 
     962           3 :   opt.homedir = default_homedir ();
     963             : 
     964             : 
     965             :   /* First check whether we have a config file on the commandline */
     966           3 :   orig_argc = argc;
     967           3 :   orig_argv = argv;
     968           3 :   pargs.argc = &argc;
     969           3 :   pargs.argv = &argv;
     970           3 :   pargs.flags= 1|(1<<6);  /* do not remove the args, ignore version */
     971           9 :   while (arg_parse( &pargs, opts))
     972             :     {
     973           3 :       if (pargs.r_opt == oDebug || pargs.r_opt == oDebugAll)
     974           0 :         parse_debug++;
     975           3 :       else if (pargs.r_opt == oOptions)
     976             :         { /* yes there is one, so we do not try the default one but
     977             :              read the config file when it is encountered at the
     978             :              commandline */
     979           0 :           default_config = 0;
     980             :         }
     981           3 :       else if (pargs.r_opt == oNoOptions)
     982             :         {
     983           0 :           default_config = 0; /* --no-options */
     984           0 :           opt.no_homedir_creation = 1;
     985             :         }
     986           3 :       else if (pargs.r_opt == oHomedir)
     987           0 :         opt.homedir = pargs.r.ret_str;
     988           3 :       else if (pargs.r_opt == aCallProtectTool)
     989           0 :         break; /* This break makes sure that --version and --help are
     990             :                   passed to the protect-tool. */
     991             :     }
     992             : 
     993             : 
     994             :   /* Initialize the secure memory. */
     995           3 :   gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
     996           3 :   maybe_setuid = 0;
     997             : 
     998             :   /*
     999             :      Now we are now working under our real uid
    1000             :   */
    1001             : 
    1002           3 :   ksba_set_malloc_hooks (gcry_malloc, gcry_realloc, gcry_free );
    1003             : 
    1004           3 :   malloc_hooks.malloc = gcry_malloc;
    1005           3 :   malloc_hooks.realloc = gcry_realloc;
    1006           3 :   malloc_hooks.free = gcry_free;
    1007           3 :   assuan_set_malloc_hooks (&malloc_hooks);
    1008           3 :   assuan_set_gpg_err_source (GPG_ERR_SOURCE_DEFAULT);
    1009           3 :   setup_libassuan_logging (&opt.debug);
    1010             : 
    1011           3 :   keybox_set_malloc_hooks (gcry_malloc, gcry_realloc, gcry_free);
    1012             : 
    1013             :   /* Setup a default control structure for command line mode */
    1014           3 :   memset (&ctrl, 0, sizeof ctrl);
    1015           3 :   gpgsm_init_default_ctrl (&ctrl);
    1016           3 :   ctrl.no_server = 1;
    1017           3 :   ctrl.status_fd = -1; /* No status output. */
    1018           3 :   ctrl.autodetect_encoding = 1;
    1019             : 
    1020             :   /* Set the default option file */
    1021           3 :   if (default_config )
    1022           3 :     configname = make_filename (opt.homedir, GPGSM_NAME EXTSEP_S "conf", NULL);
    1023             :   /* Set the default policy file */
    1024           3 :   opt.policy_file = make_filename (opt.homedir, "policies.txt", NULL);
    1025             : 
    1026           3 :   argc        = orig_argc;
    1027           3 :   argv        = orig_argv;
    1028           3 :   pargs.argc  = &argc;
    1029           3 :   pargs.argv  = &argv;
    1030           3 :   pargs.flags =  1;  /* do not remove the args */
    1031             : 
    1032             :  next_pass:
    1033           6 :   if (configname) {
    1034           3 :     configlineno = 0;
    1035           3 :     configfp = fopen (configname, "r");
    1036           3 :     if (!configfp)
    1037             :       {
    1038           0 :         if (default_config)
    1039             :           {
    1040           0 :             if (parse_debug)
    1041           0 :               log_info (_("Note: no default option file '%s'\n"), configname);
    1042             :           }
    1043             :         else
    1044             :           {
    1045           0 :             log_error (_("option file '%s': %s\n"), configname, strerror(errno));
    1046           0 :             gpgsm_exit(2);
    1047             :           }
    1048           0 :         xfree(configname);
    1049           0 :         configname = NULL;
    1050             :       }
    1051           3 :     if (parse_debug && configname)
    1052           0 :       log_info (_("reading options from '%s'\n"), configname);
    1053           3 :     default_config = 0;
    1054             :   }
    1055             : 
    1056          27 :   while (!no_more_options
    1057          21 :          && optfile_parse (configfp, configname, &configlineno, &pargs, opts))
    1058             :     {
    1059          15 :       switch (pargs.r_opt)
    1060             :         {
    1061             :         case aGPGConfList:
    1062             :         case aGPGConfTest:
    1063           0 :           set_cmd (&cmd, pargs.r_opt);
    1064           0 :           do_not_setup_keys = 1;
    1065           0 :           nogreeting = 1;
    1066           0 :           break;
    1067             : 
    1068             :         case aServer:
    1069           0 :           opt.batch = 1;
    1070           0 :           set_cmd (&cmd, aServer);
    1071           0 :           break;
    1072             : 
    1073             :         case aCallDirmngr:
    1074           0 :           opt.batch = 1;
    1075           0 :           set_cmd (&cmd, aCallDirmngr);
    1076           0 :           do_not_setup_keys = 1;
    1077           0 :           break;
    1078             : 
    1079             :         case aCallProtectTool:
    1080           0 :           opt.batch = 1;
    1081           0 :           set_cmd (&cmd, aCallProtectTool);
    1082           0 :           no_more_options = 1; /* Stop parsing. */
    1083           0 :           do_not_setup_keys = 1;
    1084           0 :           break;
    1085             : 
    1086             :         case aDeleteKey:
    1087           0 :           set_cmd (&cmd, aDeleteKey);
    1088             :           /*greeting=1;*/
    1089           0 :           do_not_setup_keys = 1;
    1090           0 :           break;
    1091             : 
    1092             :         case aDetachedSign:
    1093           0 :           detached_sig = 1;
    1094           0 :           set_cmd (&cmd, aSign );
    1095           0 :           break;
    1096             : 
    1097             :         case aKeygen:
    1098           0 :           set_cmd (&cmd, aKeygen);
    1099           0 :           greeting=1;
    1100           0 :           do_not_setup_keys = 1;
    1101           0 :           break;
    1102             : 
    1103             :         case aImport:
    1104             :         case aSendKeys:
    1105             :         case aRecvKeys:
    1106             :         case aExport:
    1107             :         case aExportSecretKeyP12:
    1108             :         case aExportSecretKeyP8:
    1109             :         case aExportSecretKeyRaw:
    1110             :         case aDumpKeys:
    1111             :         case aDumpChain:
    1112             :         case aDumpExternalKeys:
    1113             :         case aDumpSecretKeys:
    1114             :         case aListKeys:
    1115             :         case aListExternalKeys:
    1116             :         case aListSecretKeys:
    1117             :         case aListChain:
    1118             :         case aLearnCard:
    1119             :         case aPasswd:
    1120             :         case aKeydbClearSomeCertFlags:
    1121           3 :           do_not_setup_keys = 1;
    1122           3 :           set_cmd (&cmd, pargs.r_opt);
    1123           3 :           break;
    1124             : 
    1125             :         case aEncr:
    1126           0 :           recp_required = 1;
    1127           0 :           set_cmd (&cmd, pargs.r_opt);
    1128           0 :           break;
    1129             : 
    1130             :         case aSym:
    1131             :         case aDecrypt:
    1132             :         case aSign:
    1133             :         case aClearsign:
    1134             :         case aVerify:
    1135           0 :           set_cmd (&cmd, pargs.r_opt);
    1136           0 :           break;
    1137             : 
    1138             :           /* Output encoding selection.  */
    1139             :         case oArmor:
    1140           0 :           ctrl.create_pem = 1;
    1141           0 :           break;
    1142             :         case oBase64:
    1143           0 :           ctrl.create_pem = 0;
    1144           0 :           ctrl.create_base64 = 1;
    1145           0 :           break;
    1146             :         case oNoArmor:
    1147           0 :           ctrl.create_pem = 0;
    1148           0 :           ctrl.create_base64 = 0;
    1149           0 :           break;
    1150             : 
    1151             :         case oP12Charset:
    1152           0 :           opt.p12_charset = pargs.r.ret_str;
    1153           0 :           break;
    1154             : 
    1155             :           /* Input encoding selection.  */
    1156             :         case oAssumeArmor:
    1157           0 :           ctrl.autodetect_encoding = 0;
    1158           0 :           ctrl.is_pem = 1;
    1159           0 :           ctrl.is_base64 = 0;
    1160           0 :           break;
    1161             :         case oAssumeBase64:
    1162           0 :           ctrl.autodetect_encoding = 0;
    1163           0 :           ctrl.is_pem = 0;
    1164           0 :           ctrl.is_base64 = 1;
    1165           0 :           break;
    1166             :         case oAssumeBinary:
    1167           0 :           ctrl.autodetect_encoding = 0;
    1168           0 :           ctrl.is_pem = 0;
    1169           0 :           ctrl.is_base64 = 0;
    1170           0 :           break;
    1171             : 
    1172             :         case oDisableCRLChecks:
    1173           3 :           opt.no_crl_check = 1;
    1174           3 :           break;
    1175             :         case oEnableCRLChecks:
    1176           0 :           opt.no_crl_check = 0;
    1177           0 :           break;
    1178             :         case oDisableTrustedCertCRLCheck:
    1179           0 :           opt.no_trusted_cert_crl_check = 1;
    1180           0 :           break;
    1181             :         case oEnableTrustedCertCRLCheck:
    1182           0 :           opt.no_trusted_cert_crl_check = 0;
    1183           0 :           break;
    1184             :         case oForceCRLRefresh:
    1185           0 :           opt.force_crl_refresh = 1;
    1186           0 :           break;
    1187             : 
    1188             :         case oDisableOCSP:
    1189           0 :           ctrl.use_ocsp = opt.enable_ocsp = 0;
    1190           0 :           break;
    1191             :         case oEnableOCSP:
    1192           0 :           ctrl.use_ocsp = opt.enable_ocsp = 1;
    1193           0 :           break;
    1194             : 
    1195             :         case oIncludeCerts:
    1196           0 :           ctrl.include_certs = default_include_certs = pargs.r.ret_int;
    1197           0 :           break;
    1198             : 
    1199             :         case oPolicyFile:
    1200           0 :           xfree (opt.policy_file);
    1201           0 :           if (*pargs.r.ret_str)
    1202           0 :             opt.policy_file = xstrdup (pargs.r.ret_str);
    1203             :           else
    1204           0 :             opt.policy_file = NULL;
    1205           0 :           break;
    1206             : 
    1207             :         case oDisablePolicyChecks:
    1208           0 :           opt.no_policy_check = 1;
    1209           0 :           break;
    1210             :         case oEnablePolicyChecks:
    1211           0 :           opt.no_policy_check = 0;
    1212           0 :           break;
    1213             : 
    1214             :         case oAutoIssuerKeyRetrieve:
    1215           0 :           opt.auto_issuer_key_retrieve = 1;
    1216           0 :           break;
    1217             : 
    1218           0 :         case oOutput: opt.outfile = pargs.r.ret_str; break;
    1219             : 
    1220             : 
    1221           0 :         case oQuiet: opt.quiet = 1; break;
    1222           0 :         case oNoTTY: /* fixme:tty_no_terminal(1);*/ break;
    1223           0 :         case oDryRun: opt.dry_run = 1; break;
    1224             : 
    1225             :         case oVerbose:
    1226           0 :           opt.verbose++;
    1227           0 :           gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose);
    1228           0 :           break;
    1229             :         case oNoVerbose:
    1230           0 :           opt.verbose = 0;
    1231           0 :           gcry_control (GCRYCTL_SET_VERBOSITY, (int)opt.verbose);
    1232           0 :           break;
    1233             : 
    1234           0 :         case oLogFile: logfile = pargs.r.ret_str; break;
    1235           0 :         case oNoLogFile: logfile = NULL; break;
    1236             : 
    1237           0 :         case oAuditLog: auditlog = pargs.r.ret_str; break;
    1238           0 :         case oHtmlAuditLog: htmlauditlog = pargs.r.ret_str; break;
    1239             : 
    1240             :         case oBatch:
    1241           0 :           opt.batch = 1;
    1242           0 :           greeting = 0;
    1243           0 :           break;
    1244           0 :         case oNoBatch: opt.batch = 0; break;
    1245             : 
    1246           0 :         case oAnswerYes: opt.answer_yes = 1; break;
    1247           0 :         case oAnswerNo: opt.answer_no = 1; break;
    1248             : 
    1249           0 :         case oKeyring: append_to_strlist (&nrings, pargs.r.ret_str); break;
    1250             : 
    1251             :         case oDebug:
    1252           0 :           if (parse_debug_flag (pargs.r.ret_str, &debug_value, debug_flags))
    1253             :             {
    1254           0 :               pargs.r_opt = ARGPARSE_INVALID_ARG;
    1255           0 :               pargs.err = ARGPARSE_PRINT_ERROR;
    1256             :             }
    1257           0 :           break;
    1258           0 :         case oDebugAll: debug_value = ~0; break;
    1259           0 :         case oDebugNone: debug_value = 0; break;
    1260           0 :         case oDebugLevel: debug_level = pargs.r.ret_str; break;
    1261           0 :         case oDebugWait: debug_wait = pargs.r.ret_int; break;
    1262             :         case oDebugAllowCoreDump:
    1263           0 :           may_coredump = enable_core_dumps ();
    1264           0 :           break;
    1265           0 :         case oDebugNoChainValidation: opt.no_chain_validation = 1; break;
    1266           0 :         case oDebugIgnoreExpiration: opt.ignore_expiration = 1; break;
    1267           0 :         case oFixedPassphrase: opt.fixed_passphrase = pargs.r.ret_str; break;
    1268             : 
    1269           0 :         case oStatusFD: ctrl.status_fd = pargs.r.ret_int; break;
    1270           0 :         case oLoggerFD: log_set_fd (pargs.r.ret_int ); break;
    1271             :         case oWithMD5Fingerprint:
    1272           0 :           opt.with_md5_fingerprint=1; /*fall thru*/
    1273             :         case oWithFingerprint:
    1274           0 :           with_fpr=1; /*fall thru*/
    1275             :         case aFingerprint:
    1276           0 :           opt.fingerprint++;
    1277           0 :           break;
    1278             : 
    1279             :         case oWithKeygrip:
    1280           0 :           opt.with_keygrip = 1;
    1281           0 :           break;
    1282             : 
    1283             :         case oOptions:
    1284             :           /* config files may not be nested (silently ignore them) */
    1285           0 :           if (!configfp)
    1286             :             {
    1287           0 :               xfree(configname);
    1288           0 :               configname = xstrdup (pargs.r.ret_str);
    1289           0 :               goto next_pass;
    1290             :             }
    1291           0 :           break;
    1292           0 :         case oNoOptions: opt.no_homedir_creation = 1; break; /* no-options */
    1293           0 :         case oHomedir: opt.homedir = pargs.r.ret_str; break;
    1294           3 :         case oAgentProgram: opt.agent_program = pargs.r.ret_str;  break;
    1295             : 
    1296             :         case oDisplay:
    1297           0 :           set_opt_session_env ("DISPLAY", pargs.r.ret_str);
    1298           0 :           break;
    1299             :         case oTTYname:
    1300           0 :           set_opt_session_env ("GPG_TTY", pargs.r.ret_str);
    1301           0 :           break;
    1302             :         case oTTYtype:
    1303           0 :           set_opt_session_env ("TERM", pargs.r.ret_str);
    1304           0 :           break;
    1305             :         case oXauthority:
    1306           0 :           set_opt_session_env ("XAUTHORITY", pargs.r.ret_str);
    1307           0 :           break;
    1308             : 
    1309           0 :         case oLCctype: opt.lc_ctype = xstrdup (pargs.r.ret_str); break;
    1310           0 :         case oLCmessages: opt.lc_messages = xstrdup (pargs.r.ret_str); break;
    1311             : 
    1312           0 :         case oDirmngrProgram: opt.dirmngr_program = pargs.r.ret_str;  break;
    1313           0 :         case oDisableDirmngr: opt.disable_dirmngr = 1;  break;
    1314           0 :         case oPreferSystemDirmngr: /* Obsolete */; break;
    1315             :         case oProtectToolProgram:
    1316           0 :           opt.protect_tool_program = pargs.r.ret_str;
    1317           0 :           break;
    1318             : 
    1319             :         case oFakedSystemTime:
    1320             :           {
    1321           3 :             time_t faked_time = isotime2epoch (pargs.r.ret_str);
    1322           3 :             if (faked_time == (time_t)(-1))
    1323           3 :               faked_time = (time_t)strtoul (pargs.r.ret_str, NULL, 10);
    1324           3 :             gnupg_set_time (faked_time, 0);
    1325             :           }
    1326           3 :           break;
    1327             : 
    1328           0 :         case oNoDefKeyring: default_keyring = 0; break;
    1329           0 :         case oNoGreeting: nogreeting = 1; break;
    1330             : 
    1331             :         case oDefaultKey:
    1332           0 :           if (*pargs.r.ret_str)
    1333             :             {
    1334           0 :               xfree (opt.local_user);
    1335           0 :               opt.local_user = xstrdup (pargs.r.ret_str);
    1336             :             }
    1337           0 :           break;
    1338             :         case oDefRecipient:
    1339           0 :           if (*pargs.r.ret_str)
    1340           0 :             opt.def_recipient = xstrdup (pargs.r.ret_str);
    1341           0 :           break;
    1342             :         case oDefRecipientSelf:
    1343           0 :           xfree (opt.def_recipient);
    1344           0 :           opt.def_recipient = NULL;
    1345           0 :           opt.def_recipient_self = 1;
    1346           0 :           break;
    1347             :         case oNoDefRecipient:
    1348           0 :           xfree (opt.def_recipient);
    1349           0 :           opt.def_recipient = NULL;
    1350           0 :           opt.def_recipient_self = 0;
    1351           0 :           break;
    1352             : 
    1353           0 :         case oWithKeyData: opt.with_key_data=1; /* fall thru */
    1354           0 :         case oWithColons: ctrl.with_colons = 1; break;
    1355           0 :         case oWithSecret: ctrl.with_secret = 1; break;
    1356           0 :         case oWithValidation: ctrl.with_validation=1; break;
    1357           0 :         case oWithEphemeralKeys: ctrl.with_ephemeral_keys=1; break;
    1358             : 
    1359           0 :         case oSkipVerify: opt.skip_verify=1; break;
    1360             : 
    1361           0 :         case oNoEncryptTo: opt.no_encrypt_to = 1; break;
    1362             :         case oEncryptTo: /* Store the recipient in the second list */
    1363           0 :           sl = add_to_strlist (&remusr, pargs.r.ret_str);
    1364           0 :           sl->flags = 1;
    1365           0 :           break;
    1366             : 
    1367             :         case oRecipient: /* store the recipient */
    1368           0 :           add_to_strlist ( &remusr, pargs.r.ret_str);
    1369           0 :           break;
    1370             : 
    1371             :         case oUser: /* Store the local users, the first one is the default */
    1372           0 :           if (!opt.local_user)
    1373           0 :             opt.local_user = xstrdup (pargs.r.ret_str);
    1374           0 :           add_to_strlist (&locusr, pargs.r.ret_str);
    1375           0 :           break;
    1376             : 
    1377             :         case oNoSecmemWarn:
    1378           3 :           gcry_control (GCRYCTL_DISABLE_SECMEM_WARN);
    1379           3 :           break;
    1380             : 
    1381             :         case oCipherAlgo:
    1382           0 :           opt.def_cipher_algoid = pargs.r.ret_str;
    1383           0 :           break;
    1384             : 
    1385             :         case oDisableCipherAlgo:
    1386             :           {
    1387           0 :             int algo = gcry_cipher_map_name (pargs.r.ret_str);
    1388           0 :             gcry_cipher_ctl (NULL, GCRYCTL_DISABLE_ALGO, &algo, sizeof algo);
    1389             :           }
    1390           0 :           break;
    1391             :         case oDisablePubkeyAlgo:
    1392             :           {
    1393           0 :             int algo = gcry_pk_map_name (pargs.r.ret_str);
    1394           0 :             gcry_pk_ctl (GCRYCTL_DISABLE_ALGO,&algo, sizeof algo );
    1395             :           }
    1396           0 :           break;
    1397             : 
    1398             :         case oDigestAlgo:
    1399           0 :           forced_digest_algo = pargs.r.ret_str;
    1400           0 :           break;
    1401             : 
    1402             :         case oExtraDigestAlgo:
    1403           0 :           extra_digest_algo = pargs.r.ret_str;
    1404           0 :           break;
    1405             : 
    1406           0 :         case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;
    1407           0 :         case oNoRandomSeedFile: use_random_seed = 0; break;
    1408           0 :         case oNoCommonCertsImport: no_common_certs_import = 1; break;
    1409             : 
    1410           0 :         case oEnableSpecialFilenames: allow_special_filenames =1; break;
    1411             : 
    1412           0 :         case oValidationModel: parse_validation_model (pargs.r.ret_str); break;
    1413             : 
    1414             :         case oKeyServer:
    1415             :           {
    1416             :             struct keyserver_spec *keyserver;
    1417           0 :             keyserver = parse_keyserver_line (pargs.r.ret_str,
    1418             :                                               configname, configlineno);
    1419           0 :             if (! keyserver)
    1420           0 :               log_error (_("could not parse keyserver\n"));
    1421             :             else
    1422             :               {
    1423             :                 /* FIXME: Keep last next pointer.  */
    1424           0 :                 struct keyserver_spec **next_p = &opt.keyserver;
    1425           0 :                 while (*next_p)
    1426           0 :                   next_p = &(*next_p)->next;
    1427           0 :                 *next_p = keyserver;
    1428             :               }
    1429             :           }
    1430           0 :           break;
    1431             : 
    1432             :         case oIgnoreCertExtension:
    1433           0 :           add_to_strlist (&opt.ignored_cert_extensions, pargs.r.ret_str);
    1434           0 :           break;
    1435             : 
    1436           0 :         case oNoAutostart: opt.autostart = 0; break;
    1437             : 
    1438             :         default:
    1439           0 :           pargs.err = configfp? ARGPARSE_PRINT_WARNING:ARGPARSE_PRINT_ERROR;
    1440           0 :           break;
    1441             :         }
    1442             :     }
    1443             : 
    1444           6 :   if (configfp)
    1445             :     {
    1446           3 :       fclose (configfp);
    1447           3 :       configfp = NULL;
    1448             :       /* Keep a copy of the config filename. */
    1449           3 :       opt.config_filename = configname;
    1450           3 :       configname = NULL;
    1451           3 :       goto next_pass;
    1452             :     }
    1453           3 :   xfree (configname);
    1454           3 :   configname = NULL;
    1455             : 
    1456           3 :   if (!opt.config_filename)
    1457           0 :     opt.config_filename = make_filename (opt.homedir,
    1458             :                                          GPGSM_NAME EXTSEP_S "conf",
    1459             :                                          NULL);
    1460             : 
    1461           3 :   if (log_get_errorcount(0))
    1462           0 :     gpgsm_exit(2);
    1463             : 
    1464             :   /* Now that we have the options parsed we need to update the default
    1465             :      control structure.  */
    1466           3 :   gpgsm_init_default_ctrl (&ctrl);
    1467             : 
    1468           3 :   if (nogreeting)
    1469           0 :     greeting = 0;
    1470             : 
    1471           3 :   if (greeting)
    1472             :     {
    1473           0 :       es_fprintf (es_stderr, "%s %s; %s\n",
    1474             :                   strusage(11), strusage(13), strusage(14) );
    1475           0 :       es_fprintf (es_stderr, "%s\n", strusage(15) );
    1476             :     }
    1477             : #  ifdef IS_DEVELOPMENT_VERSION
    1478             :   if (!opt.batch)
    1479             :     {
    1480             :       log_info ("NOTE: THIS IS A DEVELOPMENT VERSION!\n");
    1481             :       log_info ("It is only intended for test purposes and should NOT be\n");
    1482             :       log_info ("used in a production environment or with production keys!\n");
    1483             :     }
    1484             : #  endif
    1485             : 
    1486           3 :   if (may_coredump && !opt.quiet)
    1487           0 :     log_info (_("WARNING: program may create a core file!\n"));
    1488             : 
    1489             : /*   if (opt.qualsig_approval && !opt.quiet) */
    1490             : /*     log_info (_("This software has offically been approved to " */
    1491             : /*                 "create and verify\n" */
    1492             : /*                 "qualified signatures according to German law.\n")); */
    1493             : 
    1494           3 :   if (logfile && cmd == aServer)
    1495             :     {
    1496           0 :       log_set_file (logfile);
    1497           0 :       log_set_prefix (NULL, 1|2|4);
    1498             :     }
    1499             : 
    1500           3 :   if (gnupg_faked_time_p ())
    1501             :     {
    1502             :       gnupg_isotime_t tbuf;
    1503             : 
    1504           3 :       log_info (_("WARNING: running with faked system time: "));
    1505           3 :       gnupg_get_isotime (tbuf);
    1506           3 :       dump_isotime (tbuf);
    1507           3 :       log_printf ("\n");
    1508             :     }
    1509             : 
    1510             :   /* Print a warning if an argument looks like an option.  */
    1511           3 :   if (!opt.quiet && !(pargs.flags & ARGPARSE_FLAG_STOP_SEEN))
    1512             :     {
    1513             :       int i;
    1514             : 
    1515           6 :       for (i=0; i < argc; i++)
    1516           3 :         if (argv[i][0] == '-' && argv[i][1] == '-')
    1517           0 :           log_info (_("Note: '%s' is not considered an option\n"), argv[i]);
    1518             :     }
    1519             : 
    1520             : /*FIXME    if (opt.batch) */
    1521             : /*      tty_batchmode (1); */
    1522             : 
    1523           3 :   gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
    1524             : 
    1525           3 :   set_debug ();
    1526             : 
    1527             :   /* Although we alwasy use gpgsm_exit, we better install a regualr
    1528             :      exit handler so that at least the secure memory gets wiped
    1529             :      out. */
    1530           3 :   if (atexit (emergency_cleanup))
    1531             :     {
    1532           0 :       log_error ("atexit failed\n");
    1533           0 :       gpgsm_exit (2);
    1534             :     }
    1535             : 
    1536             :   /* Must do this after dropping setuid, because the mapping functions
    1537             :      may try to load an module and we may have disabled an algorithm.
    1538             :      We remap the commonly used algorithms to the OIDs for
    1539             :      convenience.  We need to work with the OIDs because they are used
    1540             :      to check whether the encryption mode is actually available. */
    1541           3 :   if (!strcmp (opt.def_cipher_algoid, "3DES") )
    1542           0 :     opt.def_cipher_algoid = "1.2.840.113549.3.7";
    1543           3 :   else if (!strcmp (opt.def_cipher_algoid, "AES")
    1544           0 :            || !strcmp (opt.def_cipher_algoid, "AES128"))
    1545           3 :     opt.def_cipher_algoid = "2.16.840.1.101.3.4.1.2";
    1546           0 :   else if (!strcmp (opt.def_cipher_algoid, "AES256") )
    1547           0 :     opt.def_cipher_algoid = "2.16.840.1.101.3.4.1.42";
    1548           0 :   else if (!strcmp (opt.def_cipher_algoid, "SERPENT")
    1549           0 :            || !strcmp (opt.def_cipher_algoid, "SERPENT128") )
    1550           0 :     opt.def_cipher_algoid = "1.3.6.1.4.1.11591.13.2.2";
    1551           0 :   else if (!strcmp (opt.def_cipher_algoid, "SERPENT192") )
    1552           0 :     opt.def_cipher_algoid = "1.3.6.1.4.1.11591.13.2.22";
    1553           0 :   else if (!strcmp (opt.def_cipher_algoid, "SERPENT192") )
    1554           0 :     opt.def_cipher_algoid = "1.3.6.1.4.1.11591.13.2.42";
    1555           0 :   else if (!strcmp (opt.def_cipher_algoid, "SEED") )
    1556           0 :     opt.def_cipher_algoid = "1.2.410.200004.1.4";
    1557           0 :   else if (!strcmp (opt.def_cipher_algoid, "CAMELLIA")
    1558           0 :            || !strcmp (opt.def_cipher_algoid, "CAMELLIA128") )
    1559           0 :     opt.def_cipher_algoid = "1.2.392.200011.61.1.1.1.2";
    1560           0 :   else if (!strcmp (opt.def_cipher_algoid, "CAMELLIA192") )
    1561           0 :     opt.def_cipher_algoid = "1.2.392.200011.61.1.1.1.3";
    1562           0 :   else if (!strcmp (opt.def_cipher_algoid, "CAMELLIA256") )
    1563           0 :     opt.def_cipher_algoid = "1.2.392.200011.61.1.1.1.4";
    1564             : 
    1565           3 :   if (cmd != aGPGConfList)
    1566             :     {
    1567           3 :       if ( !gcry_cipher_map_name (opt.def_cipher_algoid)
    1568           3 :            || !gcry_cipher_mode_from_oid (opt.def_cipher_algoid))
    1569           0 :         log_error (_("selected cipher algorithm is invalid\n"));
    1570             : 
    1571           3 :       if (forced_digest_algo)
    1572             :         {
    1573           0 :           opt.forced_digest_algo = gcry_md_map_name (forced_digest_algo);
    1574           0 :           if (our_md_test_algo(opt.forced_digest_algo) )
    1575           0 :             log_error (_("selected digest algorithm is invalid\n"));
    1576             :         }
    1577           3 :       if (extra_digest_algo)
    1578             :         {
    1579           0 :           opt.extra_digest_algo = gcry_md_map_name (extra_digest_algo);
    1580           0 :           if (our_md_test_algo (opt.extra_digest_algo) )
    1581           0 :             log_error (_("selected digest algorithm is invalid\n"));
    1582             :         }
    1583             :     }
    1584             : 
    1585           3 :   if (log_get_errorcount(0))
    1586           0 :     gpgsm_exit(2);
    1587             : 
    1588             :   /* Set the random seed file. */
    1589           3 :   if (use_random_seed)
    1590             :     {
    1591           3 :       char *p = make_filename (opt.homedir, "random_seed", NULL);
    1592           3 :       gcry_control (GCRYCTL_SET_RANDOM_SEED_FILE, p);
    1593           3 :       xfree(p);
    1594             :     }
    1595             : 
    1596           3 :   if (!cmd && opt.fingerprint && !with_fpr)
    1597           0 :     set_cmd (&cmd, aListKeys);
    1598             : 
    1599             :   /* Add default keybox. */
    1600           3 :   if (!nrings && default_keyring)
    1601             :     {
    1602             :       int created;
    1603             : 
    1604           3 :       keydb_add_resource ("pubring.kbx", 0, 0, &created);
    1605           3 :       if (created && !no_common_certs_import)
    1606             :         {
    1607             :           /* Import the standard certificates for a new default keybox. */
    1608             :           char *filelist[2];
    1609             : 
    1610           1 :           filelist[0] = make_filename (gnupg_datadir (),"com-certs.pem", NULL);
    1611           1 :           filelist[1] = NULL;
    1612           1 :           if (!access (filelist[0], F_OK))
    1613             :             {
    1614           0 :               log_info (_("importing common certificates '%s'\n"),
    1615             :                         filelist[0]);
    1616           0 :               gpgsm_import_files (&ctrl, 1, filelist, open_read);
    1617             :             }
    1618           1 :           xfree (filelist[0]);
    1619             :         }
    1620             :     }
    1621           3 :   for (sl = nrings; sl; sl = sl->next)
    1622           0 :     keydb_add_resource (sl->d, 0, 0, NULL);
    1623           3 :   FREE_STRLIST(nrings);
    1624             : 
    1625             : 
    1626             :   /* Prepare the audit log feature for certain commands.  */
    1627           3 :   if (auditlog || htmlauditlog)
    1628             :     {
    1629           0 :       switch (cmd)
    1630             :         {
    1631             :         case aEncr:
    1632             :         case aSign:
    1633             :         case aDecrypt:
    1634             :         case aVerify:
    1635           0 :           audit_release (ctrl.audit);
    1636           0 :           ctrl.audit = audit_new ();
    1637           0 :           if (auditlog)
    1638           0 :             auditfp = open_es_fwrite (auditlog);
    1639           0 :           if (htmlauditlog)
    1640           0 :             htmlauditfp = open_es_fwrite (htmlauditlog);
    1641           0 :           break;
    1642             :         default:
    1643           0 :           break;
    1644             :         }
    1645             :     }
    1646             : 
    1647             : 
    1648           3 :   if (!do_not_setup_keys)
    1649             :     {
    1650           0 :       for (sl = locusr; sl ; sl = sl->next)
    1651             :         {
    1652           0 :           int rc = gpgsm_add_to_certlist (&ctrl, sl->d, 1, &signerlist, 0);
    1653           0 :           if (rc)
    1654             :             {
    1655           0 :               log_error (_("can't sign using '%s': %s\n"),
    1656           0 :                          sl->d, gpg_strerror (rc));
    1657           0 :               gpgsm_status2 (&ctrl, STATUS_INV_SGNR,
    1658           0 :                              get_inv_recpsgnr_code (rc), sl->d, NULL);
    1659           0 :               gpgsm_status2 (&ctrl, STATUS_INV_RECP,
    1660           0 :                              get_inv_recpsgnr_code (rc), sl->d, NULL);
    1661             :             }
    1662             :         }
    1663             : 
    1664             :       /* Build the recipient list.  We first add the regular ones and then
    1665             :          the encrypt-to ones because the underlying function will silently
    1666             :          ignore duplicates and we can't allow to keep a duplicate which is
    1667             :          flagged as encrypt-to as the actually encrypt function would then
    1668             :          complain about no (regular) recipients. */
    1669           0 :       for (sl = remusr; sl; sl = sl->next)
    1670           0 :         if (!(sl->flags & 1))
    1671           0 :           do_add_recipient (&ctrl, sl->d, &recplist, 0, recp_required);
    1672           0 :       if (!opt.no_encrypt_to)
    1673             :         {
    1674           0 :           for (sl = remusr; sl; sl = sl->next)
    1675           0 :             if ((sl->flags & 1))
    1676           0 :               do_add_recipient (&ctrl, sl->d, &recplist, 1, recp_required);
    1677             :         }
    1678             :     }
    1679             : 
    1680           3 :   if (log_get_errorcount(0))
    1681           0 :     gpgsm_exit(1); /* Must stop for invalid recipients. */
    1682             : 
    1683             :   /* Dispatch command.  */
    1684           3 :   switch (cmd)
    1685             :     {
    1686             :     case aGPGConfList:
    1687             :       { /* List options and default values in the GPG Conf format.  */
    1688           0 :         char *config_filename_esc = percent_escape (opt.config_filename, NULL);
    1689             : 
    1690           0 :         es_printf ("%s-%s.conf:%lu:\"%s\n",
    1691             :                    GPGCONF_NAME, GPGSM_NAME,
    1692             :                    GC_OPT_FLAG_DEFAULT, config_filename_esc);
    1693           0 :         xfree (config_filename_esc);
    1694             : 
    1695           0 :         es_printf ("verbose:%lu:\n", GC_OPT_FLAG_NONE);
    1696           0 :         es_printf ("quiet:%lu:\n", GC_OPT_FLAG_NONE);
    1697           0 :         es_printf ("debug-level:%lu:\"none:\n", GC_OPT_FLAG_DEFAULT);
    1698           0 :         es_printf ("log-file:%lu:\n", GC_OPT_FLAG_NONE);
    1699           0 :         es_printf ("disable-crl-checks:%lu:\n", GC_OPT_FLAG_NONE);
    1700           0 :         es_printf ("disable-trusted-cert-crl-check:%lu:\n", GC_OPT_FLAG_NONE);
    1701           0 :         es_printf ("enable-ocsp:%lu:\n", GC_OPT_FLAG_NONE);
    1702           0 :         es_printf ("include-certs:%lu:%d:\n", GC_OPT_FLAG_DEFAULT,
    1703             :                    DEFAULT_INCLUDE_CERTS);
    1704           0 :         es_printf ("disable-policy-checks:%lu:\n", GC_OPT_FLAG_NONE);
    1705           0 :         es_printf ("auto-issuer-key-retrieve:%lu:\n", GC_OPT_FLAG_NONE);
    1706           0 :         es_printf ("disable-dirmngr:%lu:\n", GC_OPT_FLAG_NONE);
    1707           0 :         es_printf ("cipher-algo:%lu:\"%s:\n", GC_OPT_FLAG_DEFAULT,
    1708             :                    DEFAULT_CIPHER_ALGO);
    1709           0 :         es_printf ("p12-charset:%lu:\n", GC_OPT_FLAG_DEFAULT);
    1710           0 :         es_printf ("default-key:%lu:\n", GC_OPT_FLAG_DEFAULT);
    1711           0 :         es_printf ("encrypt-to:%lu:\n", GC_OPT_FLAG_DEFAULT);
    1712           0 :         es_printf ("keyserver:%lu:\n", GC_OPT_FLAG_NONE);
    1713             : 
    1714             :         /* The next one is an info only item and should match what
    1715             :            proc_parameters actually implements.  */
    1716           0 :         es_printf ("default_pubkey_algo:%lu:\"%s:\n", GC_OPT_FLAG_DEFAULT,
    1717             :                    "RSA-2048");
    1718             : 
    1719             :       }
    1720           0 :       break;
    1721             :     case aGPGConfTest:
    1722             :       /* This is merely a dummy command to test whether the
    1723             :          configuration file is valid.  */
    1724           0 :       break;
    1725             : 
    1726             :     case aServer:
    1727           0 :       if (debug_wait)
    1728             :         {
    1729           0 :           log_debug ("waiting for debugger - my pid is %u .....\n",
    1730           0 :                      (unsigned int)getpid());
    1731           0 :           gnupg_sleep (debug_wait);
    1732           0 :           log_debug ("... okay\n");
    1733             :          }
    1734           0 :       gpgsm_server (recplist);
    1735           0 :       break;
    1736             : 
    1737             :     case aCallDirmngr:
    1738           0 :       if (!argc)
    1739           0 :         wrong_args ("--call-dirmngr <command> {args}");
    1740             :       else
    1741           0 :         if (gpgsm_dirmngr_run_command (&ctrl, *argv, argc-1, argv+1))
    1742           0 :           gpgsm_exit (1);
    1743           0 :       break;
    1744             : 
    1745             :     case aCallProtectTool:
    1746           0 :       run_protect_tool (argc, argv);
    1747           0 :       break;
    1748             : 
    1749             :     case aEncr: /* Encrypt the given file. */
    1750             :       {
    1751           0 :         estream_t fp = open_es_fwrite (opt.outfile?opt.outfile:"-");
    1752             : 
    1753           0 :         set_binary (stdin);
    1754             : 
    1755           0 :         if (!argc) /* Source is stdin. */
    1756           0 :           gpgsm_encrypt (&ctrl, recplist, 0, fp);
    1757           0 :         else if (argc == 1)  /* Source is the given file. */
    1758           0 :           gpgsm_encrypt (&ctrl, recplist, open_read (*argv), fp);
    1759             :         else
    1760           0 :           wrong_args ("--encrypt [datafile]");
    1761             : 
    1762           0 :         es_fclose (fp);
    1763             :       }
    1764           0 :       break;
    1765             : 
    1766             :     case aSign: /* Sign the given file. */
    1767             :       {
    1768           0 :         estream_t fp = open_es_fwrite (opt.outfile?opt.outfile:"-");
    1769             : 
    1770             :         /* Fixme: We should also allow to concatenate multiple files for
    1771             :            signing because that is what gpg does.*/
    1772           0 :         set_binary (stdin);
    1773           0 :         if (!argc) /* Create from stdin. */
    1774           0 :           gpgsm_sign (&ctrl, signerlist, 0, detached_sig, fp);
    1775           0 :         else if (argc == 1) /* From file. */
    1776           0 :           gpgsm_sign (&ctrl, signerlist,
    1777             :                       open_read (*argv), detached_sig, fp);
    1778             :         else
    1779           0 :           wrong_args ("--sign [datafile]");
    1780             : 
    1781           0 :         es_fclose (fp);
    1782             :       }
    1783           0 :       break;
    1784             : 
    1785             :     case aSignEncr: /* sign and encrypt the given file */
    1786           0 :       log_error ("this command has not yet been implemented\n");
    1787           0 :       break;
    1788             : 
    1789             :     case aClearsign: /* make a clearsig */
    1790           0 :       log_error ("this command has not yet been implemented\n");
    1791           0 :       break;
    1792             : 
    1793             :     case aVerify:
    1794             :       {
    1795           0 :         estream_t fp = NULL;
    1796             : 
    1797           0 :         set_binary (stdin);
    1798           0 :         if (argc == 2 && opt.outfile)
    1799           0 :           log_info ("option --output ignored for a detached signature\n");
    1800           0 :         else if (opt.outfile)
    1801           0 :           fp = open_es_fwrite (opt.outfile);
    1802             : 
    1803           0 :         if (!argc)
    1804           0 :           gpgsm_verify (&ctrl, 0, -1, fp); /* normal signature from stdin */
    1805           0 :         else if (argc == 1)
    1806           0 :           gpgsm_verify (&ctrl, open_read (*argv), -1, fp); /* std signature */
    1807           0 :         else if (argc == 2) /* detached signature (sig, detached) */
    1808           0 :           gpgsm_verify (&ctrl, open_read (*argv), open_read (argv[1]), NULL);
    1809             :         else
    1810           0 :           wrong_args ("--verify [signature [detached_data]]");
    1811             : 
    1812           0 :         es_fclose (fp);
    1813             :       }
    1814           0 :       break;
    1815             : 
    1816             :     case aDecrypt:
    1817             :       {
    1818           0 :         estream_t fp = open_es_fwrite (opt.outfile?opt.outfile:"-");
    1819             : 
    1820           0 :         set_binary (stdin);
    1821           0 :         if (!argc)
    1822           0 :           gpgsm_decrypt (&ctrl, 0, fp); /* from stdin */
    1823           0 :         else if (argc == 1)
    1824           0 :           gpgsm_decrypt (&ctrl, open_read (*argv), fp); /* from file */
    1825             :         else
    1826           0 :           wrong_args ("--decrypt [filename]");
    1827             : 
    1828           0 :         es_fclose (fp);
    1829             :       }
    1830           0 :       break;
    1831             : 
    1832             :     case aDeleteKey:
    1833           0 :       for (sl=NULL; argc; argc--, argv++)
    1834           0 :         add_to_strlist (&sl, *argv);
    1835           0 :       gpgsm_delete (&ctrl, sl);
    1836           0 :       free_strlist(sl);
    1837           0 :       break;
    1838             : 
    1839             :     case aListChain:
    1840             :     case aDumpChain:
    1841           0 :        ctrl.with_chain = 1;
    1842             :     case aListKeys:
    1843             :     case aDumpKeys:
    1844             :     case aListExternalKeys:
    1845             :     case aDumpExternalKeys:
    1846             :     case aListSecretKeys:
    1847             :     case aDumpSecretKeys:
    1848             :       {
    1849             :         unsigned int mode;
    1850             :         estream_t fp;
    1851             : 
    1852           0 :         switch (cmd)
    1853             :           {
    1854             :           case aListChain:
    1855           0 :           case aListKeys:         mode = (0   | 0 | (1<<6)); break;
    1856             :           case aDumpChain:
    1857           0 :           case aDumpKeys:         mode = (256 | 0 | (1<<6)); break;
    1858           0 :           case aListExternalKeys: mode = (0   | 0 | (1<<7)); break;
    1859           0 :           case aDumpExternalKeys: mode = (256 | 0 | (1<<7)); break;
    1860           0 :           case aListSecretKeys:   mode = (0   | 2 | (1<<6)); break;
    1861           0 :           case aDumpSecretKeys:   mode = (256 | 2 | (1<<6)); break;
    1862           0 :           default: BUG();
    1863             :           }
    1864             : 
    1865           0 :         fp = open_es_fwrite (opt.outfile?opt.outfile:"-");
    1866           0 :         for (sl=NULL; argc; argc--, argv++)
    1867           0 :           add_to_strlist (&sl, *argv);
    1868           0 :         gpgsm_list_keys (&ctrl, sl, fp, mode);
    1869           0 :         free_strlist(sl);
    1870           0 :         es_fclose (fp);
    1871             :       }
    1872           0 :       break;
    1873             : 
    1874             : 
    1875             :     case aKeygen: /* Generate a key; well kind of. */
    1876             :       {
    1877           0 :         estream_t fpin = NULL;
    1878             :         estream_t fpout;
    1879             : 
    1880           0 :         if (opt.batch)
    1881             :           {
    1882           0 :             if (!argc) /* Create from stdin. */
    1883           0 :               fpin = open_es_fread ("-", "r");
    1884           0 :             else if (argc == 1) /* From file. */
    1885           0 :               fpin = open_es_fread (*argv, "r");
    1886             :             else
    1887           0 :               wrong_args ("--gen-key --batch [parmfile]");
    1888             :           }
    1889             : 
    1890           0 :         fpout = open_es_fwrite (opt.outfile?opt.outfile:"-");
    1891             : 
    1892           0 :         if (fpin)
    1893           0 :           gpgsm_genkey (&ctrl, fpin, fpout);
    1894             :         else
    1895           0 :           gpgsm_gencertreq_tty (&ctrl, fpout);
    1896             : 
    1897           0 :         es_fclose (fpout);
    1898             :       }
    1899           0 :       break;
    1900             : 
    1901             : 
    1902             :     case aImport:
    1903           3 :       gpgsm_import_files (&ctrl, argc, argv, open_read);
    1904           3 :       break;
    1905             : 
    1906             :     case aExport:
    1907             :       {
    1908             :         estream_t fp;
    1909             : 
    1910           0 :         fp = open_es_fwrite (opt.outfile?opt.outfile:"-");
    1911           0 :         for (sl=NULL; argc; argc--, argv++)
    1912           0 :           add_to_strlist (&sl, *argv);
    1913           0 :         gpgsm_export (&ctrl, sl, fp);
    1914           0 :         free_strlist(sl);
    1915           0 :         es_fclose (fp);
    1916             :       }
    1917           0 :       break;
    1918             : 
    1919             :     case aExportSecretKeyP12:
    1920             :       {
    1921           0 :         estream_t fp = open_es_fwrite (opt.outfile?opt.outfile:"-");
    1922             : 
    1923           0 :         if (argc == 1)
    1924           0 :           gpgsm_p12_export (&ctrl, *argv, fp, 0);
    1925             :         else
    1926           0 :           wrong_args ("--export-secret-key-p12 KEY-ID");
    1927           0 :         if (fp != es_stdout)
    1928           0 :           es_fclose (fp);
    1929             :       }
    1930           0 :       break;
    1931             : 
    1932             :     case aExportSecretKeyP8:
    1933             :       {
    1934           0 :         estream_t fp = open_es_fwrite (opt.outfile?opt.outfile:"-");
    1935             : 
    1936           0 :         if (argc == 1)
    1937           0 :           gpgsm_p12_export (&ctrl, *argv, fp, 1);
    1938             :         else
    1939           0 :           wrong_args ("--export-secret-key-p8 KEY-ID");
    1940           0 :         if (fp != es_stdout)
    1941           0 :           es_fclose (fp);
    1942             :       }
    1943           0 :       break;
    1944             : 
    1945             :     case aExportSecretKeyRaw:
    1946             :       {
    1947           0 :         estream_t fp = open_es_fwrite (opt.outfile?opt.outfile:"-");
    1948             : 
    1949           0 :         if (argc == 1)
    1950           0 :           gpgsm_p12_export (&ctrl, *argv, fp, 2);
    1951             :         else
    1952           0 :           wrong_args ("--export-secret-key-raw KEY-ID");
    1953           0 :         if (fp != es_stdout)
    1954           0 :           es_fclose (fp);
    1955             :       }
    1956           0 :       break;
    1957             : 
    1958             :     case aSendKeys:
    1959             :     case aRecvKeys:
    1960           0 :       log_error ("this command has not yet been implemented\n");
    1961           0 :       break;
    1962             : 
    1963             : 
    1964             :     case aLearnCard:
    1965           0 :       if (argc)
    1966           0 :         wrong_args ("--learn-card");
    1967             :       else
    1968             :         {
    1969           0 :           int rc = gpgsm_agent_learn (&ctrl);
    1970           0 :           if (rc)
    1971           0 :             log_error ("error learning card: %s\n", gpg_strerror (rc));
    1972             :         }
    1973           0 :       break;
    1974             : 
    1975             :     case aPasswd:
    1976           0 :       if (argc != 1)
    1977           0 :         wrong_args ("--passwd <key-Id>");
    1978             :       else
    1979             :         {
    1980             :           int rc;
    1981           0 :           ksba_cert_t cert = NULL;
    1982           0 :           char *grip = NULL;
    1983             : 
    1984           0 :           rc = gpgsm_find_cert (*argv, NULL, &cert);
    1985           0 :           if (rc)
    1986             :             ;
    1987           0 :           else if (!(grip = gpgsm_get_keygrip_hexstring (cert)))
    1988           0 :             rc = gpg_error (GPG_ERR_BUG);
    1989             :           else
    1990             :             {
    1991           0 :               char *desc = gpgsm_format_keydesc (cert);
    1992           0 :               rc = gpgsm_agent_passwd (&ctrl, grip, desc);
    1993           0 :               xfree (desc);
    1994             :             }
    1995           0 :           if (rc)
    1996           0 :             log_error ("error changing passphrase: %s\n", gpg_strerror (rc));
    1997           0 :           xfree (grip);
    1998           0 :           ksba_cert_release (cert);
    1999             :         }
    2000           0 :       break;
    2001             : 
    2002             :     case aKeydbClearSomeCertFlags:
    2003           0 :       for (sl=NULL; argc; argc--, argv++)
    2004           0 :         add_to_strlist (&sl, *argv);
    2005           0 :       keydb_clear_some_cert_flags (&ctrl, sl);
    2006           0 :       free_strlist(sl);
    2007           0 :       break;
    2008             : 
    2009             : 
    2010             :     default:
    2011           0 :         log_error (_("invalid command (there is no implicit command)\n"));
    2012           0 :         break;
    2013             :     }
    2014             : 
    2015             :   /* Print the audit result if needed.  */
    2016           3 :   if ((auditlog && auditfp) || (htmlauditlog && htmlauditfp))
    2017             :     {
    2018           0 :       if (auditlog && auditfp)
    2019           0 :         audit_print_result (ctrl.audit, auditfp, 0);
    2020           0 :       if (htmlauditlog && htmlauditfp)
    2021           0 :         audit_print_result (ctrl.audit, htmlauditfp, 1);
    2022           0 :       audit_release (ctrl.audit);
    2023           0 :       ctrl.audit = NULL;
    2024           0 :       es_fclose (auditfp);
    2025           0 :       es_fclose (htmlauditfp);
    2026             :     }
    2027             : 
    2028             :   /* cleanup */
    2029           3 :   keyserver_list_free (opt.keyserver);
    2030           3 :   opt.keyserver = NULL;
    2031           3 :   gpgsm_release_certlist (recplist);
    2032           3 :   gpgsm_release_certlist (signerlist);
    2033           3 :   FREE_STRLIST (remusr);
    2034           3 :   FREE_STRLIST (locusr);
    2035           3 :   gpgsm_exit(0);
    2036           0 :   return 8; /*NOTREACHED*/
    2037             : }
    2038             : 
    2039             : /* Note: This function is used by signal handlers!. */
    2040             : static void
    2041           6 : emergency_cleanup (void)
    2042             : {
    2043           6 :   gcry_control (GCRYCTL_TERM_SECMEM );
    2044           6 : }
    2045             : 
    2046             : 
    2047             : void
    2048           3 : gpgsm_exit (int rc)
    2049             : {
    2050           3 :   gcry_control (GCRYCTL_UPDATE_RANDOM_SEED_FILE);
    2051           3 :   if (opt.debug & DBG_MEMSTAT_VALUE)
    2052             :     {
    2053           0 :       gcry_control( GCRYCTL_DUMP_MEMORY_STATS );
    2054           0 :       gcry_control( GCRYCTL_DUMP_RANDOM_STATS );
    2055             :     }
    2056           3 :   if (opt.debug)
    2057           0 :     gcry_control (GCRYCTL_DUMP_SECMEM_STATS );
    2058           3 :   emergency_cleanup ();
    2059           3 :   rc = rc? rc : log_get_errorcount(0)? 2 : gpgsm_errors_seen? 1 : 0;
    2060           3 :   exit (rc);
    2061             : }
    2062             : 
    2063             : 
    2064             : void
    2065           6 : gpgsm_init_default_ctrl (struct server_control_s *ctrl)
    2066             : {
    2067           6 :   ctrl->include_certs = default_include_certs;
    2068           6 :   ctrl->use_ocsp = opt.enable_ocsp;
    2069           6 :   ctrl->validation_model = default_validation_model;
    2070           6 :   ctrl->offline = opt.disable_dirmngr;
    2071           6 : }
    2072             : 
    2073             : 
    2074             : int
    2075           0 : gpgsm_parse_validation_model (const char *model)
    2076             : {
    2077           0 :   if (!ascii_strcasecmp (model, "shell") )
    2078           0 :     return 0;
    2079           0 :   else if ( !ascii_strcasecmp (model, "chain") )
    2080           0 :     return 1;
    2081           0 :   else if ( !ascii_strcasecmp (model, "steed") )
    2082           0 :     return 2;
    2083             :   else
    2084           0 :     return -1;
    2085             : }
    2086             : 
    2087             : 
    2088             : /* Check whether the filename has the form "-&nnnn", where n is a
    2089             :    non-zero number.  Returns this number or -1 if it is not the case.  */
    2090             : static int
    2091           3 : check_special_filename (const char *fname, int for_write)
    2092             : {
    2093           3 :   if (allow_special_filenames
    2094           0 :       && fname && *fname == '-' && fname[1] == '&' ) {
    2095             :     int i;
    2096             : 
    2097           0 :     fname += 2;
    2098           0 :     for (i=0; isdigit (fname[i]); i++ )
    2099             :       ;
    2100           0 :     if ( !fname[i] )
    2101           0 :       return translate_sys2libc_fd_int (atoi (fname), for_write);
    2102             :   }
    2103           3 :   return -1;
    2104             : }
    2105             : 
    2106             : 
    2107             : 
    2108             : /* Open the FILENAME for read and return the file descriptor.  Stop
    2109             :    with an error message in case of problems.  "-" denotes stdin and
    2110             :    if special filenames are allowed the given fd is opened instead.  */
    2111             : static int
    2112           3 : open_read (const char *filename)
    2113             : {
    2114             :   int fd;
    2115             : 
    2116           3 :   if (filename[0] == '-' && !filename[1])
    2117             :     {
    2118           0 :       set_binary (stdin);
    2119           0 :       return 0; /* stdin */
    2120             :     }
    2121           3 :   fd = check_special_filename (filename, 0);
    2122           3 :   if (fd != -1)
    2123           0 :     return fd;
    2124           3 :   fd = open (filename, O_RDONLY | O_BINARY);
    2125           3 :   if (fd == -1)
    2126             :     {
    2127           0 :       log_error (_("can't open '%s': %s\n"), filename, strerror (errno));
    2128           0 :       gpgsm_exit (2);
    2129             :     }
    2130           3 :   return fd;
    2131             : }
    2132             : 
    2133             : /* Same as open_read but return an estream_t.  */
    2134             : static estream_t
    2135           0 : open_es_fread (const char *filename, const char *mode)
    2136             : {
    2137             :   int fd;
    2138             :   estream_t fp;
    2139             : 
    2140           0 :   if (filename[0] == '-' && !filename[1])
    2141           0 :     fd = fileno (stdin);
    2142             :   else
    2143           0 :     fd = check_special_filename (filename, 0);
    2144           0 :   if (fd != -1)
    2145             :     {
    2146           0 :       fp = es_fdopen_nc (fd, mode);
    2147           0 :       if (!fp)
    2148             :         {
    2149           0 :           log_error ("es_fdopen(%d) failed: %s\n", fd, strerror (errno));
    2150           0 :           gpgsm_exit (2);
    2151             :         }
    2152           0 :       return fp;
    2153             :     }
    2154           0 :   fp = es_fopen (filename, mode);
    2155           0 :   if (!fp)
    2156             :     {
    2157           0 :       log_error (_("can't open '%s': %s\n"), filename, strerror (errno));
    2158           0 :       gpgsm_exit (2);
    2159             :     }
    2160           0 :   return fp;
    2161             : }
    2162             : 
    2163             : 
    2164             : /* Open FILENAME for fwrite and return an extended stream.  Stop with
    2165             :    an error message in case of problems.  "-" denotes stdout and if
    2166             :    special filenames are allowed the given fd is opened instead.
    2167             :    Caller must close the returned stream. */
    2168             : static estream_t
    2169           0 : open_es_fwrite (const char *filename)
    2170             : {
    2171             :   int fd;
    2172             :   estream_t fp;
    2173             : 
    2174           0 :   if (filename[0] == '-' && !filename[1])
    2175             :     {
    2176           0 :       fflush (stdout);
    2177           0 :       fp = es_fdopen_nc (fileno(stdout), "wb");
    2178           0 :       return fp;
    2179             :     }
    2180             : 
    2181           0 :   fd = check_special_filename (filename, 1);
    2182           0 :   if (fd != -1)
    2183             :     {
    2184           0 :       fp = es_fdopen_nc (fd, "wb");
    2185           0 :       if (!fp)
    2186             :         {
    2187           0 :           log_error ("es_fdopen(%d) failed: %s\n", fd, strerror (errno));
    2188           0 :           gpgsm_exit (2);
    2189             :         }
    2190           0 :       return fp;
    2191             :     }
    2192           0 :   fp = es_fopen (filename, "wb");
    2193           0 :   if (!fp)
    2194             :     {
    2195           0 :       log_error (_("can't open '%s': %s\n"), filename, strerror (errno));
    2196           0 :       gpgsm_exit (2);
    2197             :     }
    2198           0 :   return fp;
    2199             : }
    2200             : 
    2201             : 
    2202             : static void
    2203           0 : run_protect_tool (int argc, char **argv)
    2204             : {
    2205             : #ifdef HAVE_W32_SYSTEM
    2206             :   (void)argc;
    2207             :   (void)argv;
    2208             : #else
    2209             :   const char *pgm;
    2210             :   char **av;
    2211             :   int i;
    2212             : 
    2213           0 :   if (!opt.protect_tool_program || !*opt.protect_tool_program)
    2214           0 :     pgm = gnupg_module_name (GNUPG_MODULE_NAME_PROTECT_TOOL);
    2215             :   else
    2216           0 :     pgm = opt.protect_tool_program;
    2217             : 
    2218           0 :   av = xcalloc (argc+2, sizeof *av);
    2219           0 :   av[0] = strrchr (pgm, '/');
    2220           0 :   if (!av[0])
    2221           0 :     av[0] = xstrdup (pgm);
    2222           0 :   for (i=1; argc; i++, argc--, argv++)
    2223           0 :     av[i] = *argv;
    2224           0 :   av[i] = NULL;
    2225           0 :   execv (pgm, av);
    2226           0 :   log_error ("error executing '%s': %s\n", pgm, strerror (errno));
    2227             : #endif /*!HAVE_W32_SYSTEM*/
    2228           0 :   gpgsm_exit (2);
    2229           0 : }

Generated by: LCOV version 1.11