rpm  4.12.90
rpmsign.c
Go to the documentation of this file.
1 #include "system.h"
2 #include <errno.h>
3 #include <sys/wait.h>
4 
5 #include <popt.h>
6 #include <rpm/rpmcli.h>
7 #include <rpm/rpmsign.h>
8 #include "cliutils.h"
9 #include "debug.h"
10 
11 #if !defined(__GLIBC__) && !defined(__APPLE__)
12 char ** environ = NULL;
13 #endif
14 
15 enum modes {
16  MODE_ADDSIGN = (1 << 0),
17  MODE_RESIGN = (1 << 1),
18  MODE_DELSIGN = (1 << 2),
19 };
20 
21 static int mode = 0;
22 
23 static struct poptOption signOptsTable[] = {
24  { "addsign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_ADDSIGN,
25  N_("sign package(s)"), NULL },
26  { "resign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_RESIGN,
27  N_("sign package(s) (identical to --addsign)"), NULL },
28  { "delsign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_DELSIGN,
29  N_("delete package signatures"), NULL },
30  POPT_TABLEEND
31 };
32 
33 static struct poptOption optionsTable[] = {
34  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, signOptsTable, 0,
35  N_("Signature options:"), NULL },
36  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
37  N_("Common options for all rpm modes and executables:"), NULL },
38 
39  POPT_AUTOALIAS
40  POPT_AUTOHELP
41  POPT_TABLEEND
42 };
43 
44 /* TODO: permit overriding macro setup on the command line */
45 static int doSign(poptContext optCon)
46 {
47  int rc = EXIT_FAILURE;
48  char * passPhrase = NULL;
49  char * name = rpmExpand("%{?_gpg_name}", NULL);
50 
51  if (rstreq(name, "")) {
52  fprintf(stderr, _("You must set \"%%_gpg_name\" in your macro file\n"));
53  goto exit;
54  }
55 
56  const char *arg;
57  rc = 0;
58  while ((arg = poptGetArg(optCon)) != NULL) {
59  rc += rpmPkgSign(arg, NULL);
60  }
61 
62 exit:
63  free(passPhrase);
64  free(name);
65  return rc;
66 }
67 
68 int main(int argc, char *argv[])
69 {
70  int ec = EXIT_FAILURE;
71  poptContext optCon = rpmcliInit(argc, argv, optionsTable);
72  const char *arg;
73 
74  if (argc <= 1) {
75  printUsage(optCon, stderr, 0);
76  goto exit;
77  }
78 
79  if (poptPeekArg(optCon) == NULL) {
80  argerror(_("no arguments given"));
81  }
82 
83  switch (mode) {
84  case MODE_ADDSIGN:
85  case MODE_RESIGN:
86  ec = doSign(optCon);
87  break;
88  case MODE_DELSIGN:
89  ec = 0;
90  while ((arg = poptGetArg(optCon)) != NULL) {
91  ec += rpmPkgDelSign(arg);
92  }
93  break;
94  default:
95  argerror(_("only one major mode may be specified"));
96  break;
97  }
98 
99 exit:
100  rpmcliFini(optCon);
101  return ec;
102 }
int rpmPkgSign(const char *path, const struct rpmSignArgs *args)
Sign a package.
void printUsage(poptContext con, FILE *fp, int flags)
Definition: cliutils.c:36
poptContext rpmcliInit(int argc, char *const argv[], struct poptOption *optionsTable)
Initialize most everything needed by an rpm CLI executable context.
poptContext rpmcliFini(poptContext optCon)
Destroy most everything needed by an rpm CLI executable context.
#define _(Text)
Definition: system.h:110
void argerror(const char *desc)
Definition: cliutils.c:19
static struct poptOption optionsTable[]
Definition: rpmsign.c:33
static struct poptOption signOptsTable[]
Definition: rpmsign.c:23
static int mode
Definition: rpmsign.c:21
static int rstreq(const char *s1, const char *s2)
Test for string equality.
Definition: rpmstring.h:113
modes
Definition: rpmbuild.c:242
int main(int argc, char *argv[])
Definition: rpmsign.c:68
char ** environ
Definition: rpmsign.c:12
int rpmPkgDelSign(const char *path)
Delete signature(s) from a package.
char * rpmExpand(const char *arg,...) RPM_GNUC_NULL_TERMINATED
Return (malloc'ed) concatenated macro expansion(s).
#define N_(Text)
Definition: system.h:113
static int doSign(poptContext optCon)
Definition: rpmsign.c:45
struct poptOption rpmcliAllPoptTable[]
Popt option table for options shared by all modes and executables.