Honor --root in rpmkeys too
[platform/upstream/rpm.git] / rpmkeys.c
1 #include "system.h"
2
3 #include <popt.h>
4 #include <rpm/rpmcli.h>
5 #include "cliutils.h"
6 #include "debug.h"
7
8 #if !defined(__GLIBC__) && !defined(__APPLE__)
9 char ** environ = NULL;
10 #endif
11
12 enum modes {
13     MODE_CHECKSIG       = (1 << 0),
14     MODE_IMPORTKEY      = (1 << 1),
15     MODE_DELKEY         = (1 << 2),
16     MODE_LISTKEY        = (1 << 3),
17 };
18
19 static int mode = 0;
20
21 static struct poptOption keyOptsTable[] = {
22     { "checksig", 'K', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_CHECKSIG,
23         N_("verify package signature(s)"), NULL },
24     { "import", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_IMPORTKEY,
25         N_("import an armored public key"), NULL },
26 #if 0
27     { "delete-key", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_DELKEY,
28         N_("list keys from RPM keyring"), NULL },
29     { "list-keys", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_LISTKEY,
30         N_("list keys from RPM keyring"), NULL },
31 #endif
32     POPT_TABLEEND
33 };
34
35 static struct poptOption optionsTable[] = {
36     { NULL, '\0', POPT_ARG_INCLUDE_TABLE, keyOptsTable, 0,
37         N_("Keyring options:"), NULL },
38     { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
39         N_("Common options for all rpm modes and executables:"), NULL },
40
41     POPT_AUTOALIAS
42     POPT_AUTOHELP
43     POPT_TABLEEND
44 };
45
46 int main(int argc, char *argv[])
47 {
48     int ec = EXIT_FAILURE;
49     poptContext optCon = rpmcliInit(argc, argv, optionsTable);
50     rpmts ts = rpmtsCreate();
51     ARGV_const_t args = NULL;
52     
53     if (argc < 2) {
54         printUsage(optCon, stderr, 0);
55         goto exit;
56     }
57
58     args = (ARGV_const_t) poptGetArgs(optCon);
59
60     if (mode != MODE_LISTKEY && args == NULL)
61         argerror(_("no arguments given"));
62
63     rpmtsSetRootDir(ts, rpmcliRootDir);
64
65     switch (mode) {
66     case MODE_CHECKSIG:
67         ec = rpmcliVerifySignatures(ts, args);
68         break;
69     case MODE_IMPORTKEY:
70         ec = rpmcliImportPubkeys(ts, args);
71         break;
72     /* XXX TODO: actually implement these... */
73     case MODE_DELKEY:
74     case MODE_LISTKEY:
75         break;
76     default:
77         argerror(_("only one major mode may be specified"));
78     }
79
80 exit:
81     rpmtsFree(ts);
82     rpmcliFini(optCon);
83     return ec;
84 }