6 #include <rpm/rpmcli.h>
7 #include <rpm/rpmsign.h>
11 #if !defined(__GLIBC__) && !defined(__APPLE__)
12 char ** environ = NULL;
16 MODE_ADDSIGN = (1 << 0),
17 MODE_RESIGN = (1 << 1),
18 MODE_DELSIGN = (1 << 2),
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 },
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 },
44 static int checkPassPhrase(const char * passPhrase)
46 int passPhrasePipe[2];
51 if (passPhrase == NULL)
54 passPhrasePipe[0] = passPhrasePipe[1] = 0;
55 xx = pipe(passPhrasePipe);
56 if (!(pid = fork())) {
57 char * cmd, * gpg_path;
61 xx = close(STDIN_FILENO);
62 xx = close(STDOUT_FILENO);
63 xx = close(passPhrasePipe[1]);
64 if ((fdno = open("/dev/null", O_RDONLY)) != STDIN_FILENO) {
65 xx = dup2(fdno, STDIN_FILENO);
68 if ((fdno = open("/dev/null", O_WRONLY)) != STDOUT_FILENO) {
69 xx = dup2(fdno, STDOUT_FILENO);
72 xx = dup2(passPhrasePipe[0], 3);
74 unsetenv("MALLOC_CHECK_");
75 gpg_path = rpmExpand("%{?_gpg_path}", NULL);
77 if (!rstreq(gpg_path, ""))
78 setenv("GNUPGHOME", gpg_path, 1);
80 cmd = rpmExpand("%{?__gpg_check_password_cmd}", NULL);
81 rc = poptParseArgvString(cmd, NULL, (const char ***)&av);
83 rc = execve(av[0], av+1, environ);
85 fprintf(stderr, _("Could not exec %s: %s\n"), "gpg",
90 xx = close(passPhrasePipe[0]);
91 xx = write(passPhrasePipe[1], passPhrase, strlen(passPhrase));
92 xx = write(passPhrasePipe[1], "\n", 1);
93 xx = close(passPhrasePipe[1]);
95 (void) waitpid(pid, &status, 0);
97 return ((WIFEXITED(status) && WEXITSTATUS(status) == 0)) ? 0 : 1;
100 /* TODO: permit overriding macro setup on the command line */
101 static int doSign(poptContext optCon)
103 int rc = EXIT_FAILURE;
104 char * passPhrase = NULL;
105 char * name = rpmExpand("%{?_gpg_name}", NULL);
107 if (rstreq(name, "")) {
108 fprintf(stderr, _("You must set \"%%_gpg_name\" in your macro file\n"));
112 /* XXX FIXME: eliminate obsolete getpass() usage */
113 passPhrase = getpass(_("Enter pass phrase: "));
114 passPhrase = (passPhrase != NULL) ? rstrdup(passPhrase) : NULL;
115 if (checkPassPhrase(passPhrase) == 0) {
117 fprintf(stderr, _("Pass phrase is good.\n"));
119 while ((arg = poptGetArg(optCon)) != NULL) {
120 rc += rpmPkgSign(arg, NULL, passPhrase);
123 fprintf(stderr, _("Pass phrase check failed or gpg key expired\n"));
132 int main(int argc, char *argv[])
134 int ec = EXIT_FAILURE;
135 poptContext optCon = rpmcliInit(argc, argv, optionsTable);
139 printUsage(optCon, stderr, 0);
143 if (poptPeekArg(optCon) == NULL) {
144 argerror(_("no arguments given"));
154 while ((arg = poptGetArg(optCon)) != NULL) {
155 ec += rpmPkgDelSign(arg);
159 argerror(_("only one major mode may be specified"));