rpmsign.c
Go to the documentation of this file.
#include "system.h"
#include <errno.h>
#include <sys/wait.h>
#include <popt.h>
#include <rpm/rpmcli.h>
#include <rpm/rpmsign.h>
#include "cliutils.h"
#include "lib/rpmsignfiles.h"
#include "debug.h"
#if !defined(__GLIBC__) && !defined(__APPLE__)
char ** environ = NULL;
#endif
enum modes {
MODE_ADDSIGN = (1 << 0),
MODE_RESIGN = (1 << 1),
MODE_DELSIGN = (1 << 2),
};
static int mode = 0;
static int signfiles = 0, fskpass = 0;
static char * fileSigningKey = NULL;
static char * fileSigningKeyPassword = NULL;
static struct poptOption signOptsTable[] = {
{ "addsign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_ADDSIGN,
N_("sign package(s)"), NULL },
{ "resign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_RESIGN,
N_("sign package(s) (identical to --addsign)"), NULL },
{ "delsign", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_DELSIGN,
N_("delete package signatures"), NULL },
{ "signfiles", '\0', POPT_ARG_NONE, &signfiles, 0,
N_("sign package(s) files"), NULL},
{ "fskpath", '\0', POPT_ARG_STRING, &fileSigningKey, 0,
N_("use file signing key <key>"),
N_("<key>") },
{ "fskpass", '\0', POPT_ARG_NONE, &fskpass, 0,
N_("prompt for file signing key password"), NULL},
POPT_TABLEEND
};
static struct poptOption optionsTable[] = {
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, signOptsTable, 0,
N_("Signature options:"), NULL },
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
N_("Common options for all rpm modes and executables:"), NULL },
POPT_AUTOALIAS
POPT_AUTOHELP
POPT_TABLEEND
};
/* TODO: permit overriding macro setup on the command line */
static int doSign(poptContext optCon)
{
int rc = EXIT_FAILURE;
char * passPhrase = NULL;
char * name = rpmExpand("%{?_gpg_name}", NULL);
struct rpmSignArgs sig = {NULL, 0, 0};
if (rstreq(name, "")) {
fprintf(stderr, _("You must set \"%%_gpg_name\" in your macro file\n"));
goto exit;
}
if (fileSigningKey) {
addMacro(NULL, "_file_signing_key", NULL, fileSigningKey, RMIL_GLOBAL);
}
if (signfiles) {
const char *key = rpmExpand("%{?_file_signing_key}", NULL);
if (rstreq(key, "")) {
fprintf(stderr, _("You must set \"$$_file_signing_key\" in your macro file or on the command line with --fskpath\n"));
goto exit;
}
if (fskpass) {
#ifndef WITH_IMAEVM
argerror(_("--fskpass may only be specified when signing files"));
#else
fileSigningKeyPassword = get_fskpass();
#endif
}
addMacro(NULL, "_file_signing_key_password", NULL,
fileSigningKeyPassword, RMIL_CMDLINE);
if (fileSigningKeyPassword) {
memset(fileSigningKeyPassword, 0, strlen(fileSigningKeyPassword));
free(fileSigningKeyPassword);
}
sig.signfiles = 1;
}
const char *arg;
rc = 0;
while ((arg = poptGetArg(optCon)) != NULL) {
rc += rpmPkgSign(arg, &sig);
}
exit:
free(passPhrase);
free(name);
return rc;
}
int main(int argc, char *argv[])
{
int ec = EXIT_FAILURE;
poptContext optCon = rpmcliInit(argc, argv, optionsTable);
const char *arg;
if (argc <= 1) {
printUsage(optCon, stderr, 0);
goto exit;
}
if (poptPeekArg(optCon) == NULL) {
argerror(_("no arguments given"));
}
if (fileSigningKey && !signfiles) {
argerror(_("--fskpath may only be specified when signing files"));
}
switch (mode) {
case MODE_ADDSIGN:
case MODE_RESIGN:
ec = doSign(optCon);
break;
case MODE_DELSIGN:
ec = 0;
while ((arg = poptGetArg(optCon)) != NULL) {
ec += rpmPkgDelSign(arg);
}
break;
default:
argerror(_("only one major mode may be specified"));
break;
}
exit:
rpmcliFini(optCon);
return ec;
}
rpmPkgSign
int rpmPkgSign(const char *path, const struct rpmSignArgs *args)
Sign a package.
printUsage
void printUsage(poptContext con, FILE *fp, int flags)
Definition: cliutils.c:36
RMIL_GLOBAL
#define RMIL_GLOBAL
Definition: rpmmacro.h:44
MODE_DELSIGN
Definition: rpmsign.c:19
debug.h
rpmcliInit
poptContext rpmcliInit(int argc, char *const argv[], struct poptOption *optionsTable)
Initialize most everything needed by an rpm CLI executable context.
rpmcliFini
poptContext rpmcliFini(poptContext optCon)
Destroy most everything needed by an rpm CLI executable context.
cliutils.h
_
#define _(Text)
Definition: system.h:110
fileSigningKeyPassword
static char * fileSigningKeyPassword
Definition: rpmsign.c:26
argerror
void argerror(const char *desc)
Definition: cliutils.c:19
optionsTable
static struct poptOption optionsTable[]
Definition: rpmsign.c:45
signOptsTable
static struct poptOption signOptsTable[]
Definition: rpmsign.c:28
mode
static int mode
Definition: rpmsign.c:22
rpmSignArgs
Definition: rpmsign.h:11
rstreq
static int rstreq(const char *s1, const char *s2)
Test for string equality.
Definition: rpmstring.h:113
modes
modes
Definition: rpmbuild.c:250
signfiles
static int signfiles
Definition: rpmsign.c:24
MODE_ADDSIGN
Definition: rpmsign.c:17
main
int main(int argc, char *argv[])
Definition: rpmsign.c:110
fskpass
static int fskpass
Definition: rpmsign.c:24
addMacro
void addMacro(rpmMacroContext mc, const char *n, const char *o, const char *b, int level)
Add macro to context.
MODE_RESIGN
Definition: rpmsign.c:18
environ
char ** environ
Definition: rpmsign.c:13
rpmPkgDelSign
int rpmPkgDelSign(const char *path)
Delete signature(s) from a package.
rpmExpand
char * rpmExpand(const char *arg,...) RPM_GNUC_NULL_TERMINATED
Return (malloc&#39;ed) concatenated macro expansion(s).
system.h
fileSigningKey
static char * fileSigningKey
Definition: rpmsign.c:25
N_
#define N_(Text)
Definition: system.h:113
RMIL_CMDLINE
#define RMIL_CMDLINE
Definition: rpmmacro.h:40
doSign
static int doSign(poptContext optCon)
Definition: rpmsign.c:57
rpmSignArgs::signfiles
int signfiles
Definition: rpmsign.h:14
rpmcliAllPoptTable
struct poptOption rpmcliAllPoptTable[]
Popt option table for options shared by all modes and executables.