2 const char *__progname;
11 #include <rpm/rpmcli.h>
12 #include <rpm/rpmlib.h> /* RPMSIGTAG, rpmReadPackageFile .. */
13 #include <rpm/rpmbuild.h>
14 #include <rpm/rpmlog.h>
15 #include <rpm/rpmfileutil.h>
16 #include <rpm/rpmdb.h>
17 #include <rpm/rpmps.h>
18 #include <rpm/rpmts.h>
21 #include "lib/signature.h"
28 MODE_QUERY = (1 << 0),
29 MODE_VERIFY = (1 << 3),
30 #define MODES_QV (MODE_QUERY | MODE_VERIFY)
32 MODE_INSTALL = (1 << 1),
33 MODE_ERASE = (1 << 2),
34 #define MODES_IE (MODE_INSTALL | MODE_ERASE)
36 MODE_CHECKSIG = (1 << 6),
37 MODE_RESIGN = (1 << 7),
38 #define MODES_K (MODE_CHECKSIG | MODE_RESIGN)
40 MODE_INITDB = (1 << 10),
41 MODE_REBUILDDB = (1 << 12),
42 MODE_VERIFYDB = (1 << 13),
43 #define MODES_DB (MODE_INITDB | MODE_REBUILDDB | MODE_VERIFYDB)
49 #define MODES_FOR_DBPATH (MODES_IE | MODES_QV | MODES_DB)
50 #define MODES_FOR_NODEPS (MODES_IE | MODE_VERIFY)
51 #define MODES_FOR_TEST (MODES_IE)
52 #define MODES_FOR_ROOT (MODES_IE | MODES_QV | MODES_DB | MODES_K)
56 /* the structure describing the options we take and the defaults */
57 static struct poptOption optionsTable[] = {
60 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmQVSourcePoptTable, 0,
61 N_("Query/Verify package selection options:"),
63 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmQueryPoptTable, 0,
64 N_("Query options (with -q or --query):"),
66 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmVerifyPoptTable, 0,
67 N_("Verify options (with -V or --verify):"),
69 #endif /* IAM_RPMQV */
72 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmSignPoptTable, 0,
73 N_("Signature options:"),
78 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmDatabasePoptTable, 0,
79 N_("Database options:"),
81 #endif /* IAM_RPMDB */
84 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmInstallPoptTable, 0,
85 N_("Install/Upgrade/Erase options:"),
87 #endif /* IAM_RPMEIU */
89 { "quiet", '\0', 0, &quiet, 0, NULL, NULL},
91 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
92 N_("Common options for all rpm modes and executables:"),
101 /* MiNT cannot dynamically increase the stack. */
102 long _stksize = 64 * 1024L;
106 static void argerror(const char * desc)
108 fprintf(stderr, _("%s: %s\n"), __progname, desc);
112 static void printVersion(FILE * fp)
114 fprintf(fp, _("RPM version %s\n"), rpmEVR);
117 static void printBanner(FILE * fp)
119 fprintf(fp, _("Copyright (C) 1998-2002 - Red Hat, Inc.\n"));
120 fprintf(fp, _("This program may be freely redistributed under the terms of the GNU GPL\n"));
123 static void printUsage(poptContext con, FILE * fp, int flags)
130 poptPrintHelp(con, fp, flags);
132 poptPrintUsage(con, fp, flags);
135 int main(int argc, char *argv[])
138 enum modes bigMode = MODE_UNKNOWN;
140 #if defined(IAM_RPMQV)
141 QVA_t qva = &rpmQVKArgs;
145 struct rpmInstallArguments_s * ia = &rpmIArgs;
148 #if defined(IAM_RPMDB)
149 struct rpmDatabaseArguments_s * da = &rpmDBArgs;
152 #if defined(IAM_RPMK)
153 QVA_t ka = &rpmQVKArgs;
156 #if defined(IAM_RPMK)
157 char * passPhrase = "";
162 const char *optArg, *poptCtx;
172 #if HAVE_MCHECK_H && HAVE_MTRACE
173 mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
175 setprogname(argv[0]); /* Retrofit glibc __progname */
177 /* XXX glibc churn sanity */
178 if (__progname == NULL) {
179 if ((__progname = strrchr(argv[0], '/')) != NULL) __progname++;
180 else __progname = argv[0];
183 /* Set the major mode based on argv[0] */
185 if (rstreq(__progname, "rpmquery")) bigMode = MODE_QUERY;
186 if (rstreq(__progname, "rpmverify")) bigMode = MODE_VERIFY;
189 #if defined(IAM_RPMQV)
190 /* Jumpstart option from argv[0] if necessary. */
192 case MODE_QUERY: qva->qva_mode = 'q'; break;
193 case MODE_VERIFY: qva->qva_mode = 'V'; break;
194 case MODE_CHECKSIG: qva->qva_mode = 'K'; break;
195 case MODE_RESIGN: qva->qva_mode = 'R'; break;
207 #if defined(ENABLE_NLS)
208 /* set up the correct locale */
209 (void) setlocale(LC_ALL, "" );
211 bindtextdomain(PACKAGE, LOCALEDIR);
215 rpmSetVerbosity(RPMLOG_NOTICE); /* XXX silly use by showrc */
217 /* Only build has it's own set of aliases, everything else uses rpm */
220 /* Make a first pass through the arguments, looking for --rcfile */
221 /* We need to handle that before dealing with the rest of the arguments. */
222 /* XXX popt argv definition should be fixed instead of casting... */
223 optCon = poptGetContext(poptCtx, argc, (const char **)argv, optionsTable, 0);
225 char *poptfile = rpmGenPath(rpmConfigDir(), LIBRPMALIAS_FILENAME, NULL);
226 (void) poptReadConfigFile(optCon, poptfile);
229 (void) poptReadDefaultConfig(optCon, 1);
230 poptSetExecPath(optCon, rpmConfigDir(), 1);
232 while ((arg = poptGetNextOpt(optCon)) > 0) {
233 optArg = poptGetOptArg(optCon);
237 fprintf(stderr, _("Internal error in argument processing (%d) :-(\n"), arg);
243 fprintf(stderr, "%s: %s\n",
244 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
252 if (bigMode == MODE_UNKNOWN || (bigMode & MODES_DB)) {
254 if (bigMode != MODE_UNKNOWN)
255 argerror(_("only one major mode may be specified"));
257 bigMode = MODE_INITDB;
260 if (bigMode != MODE_UNKNOWN)
261 argerror(_("only one major mode may be specified"));
263 bigMode = MODE_REBUILDDB;
266 if (bigMode != MODE_UNKNOWN)
267 argerror(_("only one major mode may be specified"));
269 bigMode = MODE_VERIFYDB;
272 #endif /* IAM_RPMDB */
275 if (bigMode == MODE_UNKNOWN || (bigMode & MODES_QV)) {
276 switch (qva->qva_mode) {
277 case 'q': bigMode = MODE_QUERY; break;
278 case 'V': bigMode = MODE_VERIFY; break;
281 if (qva->qva_sourceCount) {
282 if (qva->qva_sourceCount > 2)
283 argerror(_("one type of query/verify may be performed at a "
286 if (qva->qva_flags && (bigMode & ~MODES_QV))
287 argerror(_("unexpected query flags"));
289 if (qva->qva_queryFormat && (bigMode & ~MODES_QV))
290 argerror(_("unexpected query format"));
292 if (qva->qva_source != RPMQV_PACKAGE && (bigMode & ~MODES_QV))
293 argerror(_("unexpected query source"));
295 #endif /* IAM_RPMQV */
298 if (bigMode == MODE_UNKNOWN || (bigMode & MODES_IE))
299 { int iflags = (ia->installInterfaceFlags &
300 (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL));
301 int eflags = (ia->installInterfaceFlags & INSTALL_ERASE);
304 argerror(_("only one major mode may be specified"));
306 bigMode = MODE_INSTALL;
308 bigMode = MODE_ERASE;
310 #endif /* IAM_RPMEIU */
313 if (bigMode == MODE_UNKNOWN || (bigMode & MODES_K)) {
314 switch (ka->qva_mode) {
318 case RPMSIGN_IMPORT_PUBKEY:
319 case RPMSIGN_CHK_SIGNATURE:
320 bigMode = MODE_CHECKSIG;
323 case RPMSIGN_ADD_SIGNATURE:
324 case RPMSIGN_NEW_SIGNATURE:
325 case RPMSIGN_DEL_SIGNATURE:
326 bigMode = MODE_RESIGN;
327 ka->sign = (ka->qva_mode != RPMSIGN_DEL_SIGNATURE);
331 #endif /* IAM_RPMK */
333 #if defined(IAM_RPMEIU)
334 if (!( bigMode == MODE_INSTALL ) &&
335 (ia->probFilter & (RPMPROB_FILTER_REPLACEPKG | RPMPROB_FILTER_OLDPACKAGE)))
336 argerror(_("only installation, upgrading, rmsource and rmspec may be forced"));
337 if (bigMode != MODE_INSTALL && (ia->probFilter & RPMPROB_FILTER_FORCERELOCATE))
338 argerror(_("files may only be relocated during package installation"));
340 if (ia->relocations && ia->prefix)
341 argerror(_("cannot use --prefix with --relocate or --excludepath"));
343 if (bigMode != MODE_INSTALL && ia->relocations)
344 argerror(_("--relocate and --excludepath may only be used when installing new packages"));
346 if (bigMode != MODE_INSTALL && ia->prefix)
347 argerror(_("--prefix may only be used when installing new packages"));
349 if (ia->prefix && ia->prefix[0] != '/')
350 argerror(_("arguments to --prefix must begin with a /"));
352 if (bigMode != MODE_INSTALL && (ia->installInterfaceFlags & INSTALL_HASH))
353 argerror(_("--hash (-h) may only be specified during package "
356 if (bigMode != MODE_INSTALL && (ia->installInterfaceFlags & INSTALL_PERCENT))
357 argerror(_("--percent may only be specified during package "
360 if (bigMode != MODE_INSTALL && (ia->probFilter & RPMPROB_FILTER_REPLACEPKG))
361 argerror(_("--replacepkgs may only be specified during package "
364 if (bigMode != MODE_INSTALL && (ia->transFlags & RPMTRANS_FLAG_NODOCS))
365 argerror(_("--excludedocs may only be specified during package "
368 if (bigMode != MODE_INSTALL && ia->incldocs)
369 argerror(_("--includedocs may only be specified during package "
372 if (ia->incldocs && (ia->transFlags & RPMTRANS_FLAG_NODOCS))
373 argerror(_("only one of --excludedocs and --includedocs may be "
376 if (bigMode != MODE_INSTALL && (ia->probFilter & RPMPROB_FILTER_IGNOREARCH))
377 argerror(_("--ignorearch may only be specified during package "
380 if (bigMode != MODE_INSTALL && (ia->probFilter & RPMPROB_FILTER_IGNOREOS))
381 argerror(_("--ignoreos may only be specified during package "
384 if (bigMode != MODE_INSTALL && bigMode != MODE_ERASE &&
385 (ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES)))
386 argerror(_("--ignoresize may only be specified during package "
389 if ((ia->installInterfaceFlags & UNINSTALL_ALLMATCHES) && bigMode != MODE_ERASE)
390 argerror(_("--allmatches may only be specified during package "
393 if ((ia->transFlags & RPMTRANS_FLAG_ALLFILES) && bigMode != MODE_INSTALL)
394 argerror(_("--allfiles may only be specified during package "
397 if ((ia->transFlags & RPMTRANS_FLAG_JUSTDB) &&
398 bigMode != MODE_INSTALL && bigMode != MODE_ERASE)
399 argerror(_("--justdb may only be specified during package "
400 "installation and erasure"));
402 if (bigMode != MODE_INSTALL && bigMode != MODE_ERASE && bigMode != MODE_VERIFY &&
403 (ia->transFlags & (RPMTRANS_FLAG_NOSCRIPTS | _noTransScripts | _noTransTriggers)))
404 argerror(_("script disabling options may only be specified during "
405 "package installation and erasure"));
407 if (bigMode != MODE_INSTALL && bigMode != MODE_ERASE && bigMode != MODE_VERIFY &&
408 (ia->transFlags & (RPMTRANS_FLAG_NOTRIGGERS | _noTransTriggers)))
409 argerror(_("trigger disabling options may only be specified during "
410 "package installation and erasure"));
412 if (ia->noDeps & (bigMode & ~MODES_FOR_NODEPS))
413 argerror(_("--nodeps may only be specified during package "
414 "building, rebuilding, recompilation, installation,"
415 "erasure, and verification"));
417 if ((ia->transFlags & RPMTRANS_FLAG_TEST) && (bigMode & ~MODES_FOR_TEST))
418 argerror(_("--test may only be specified during package installation, "
419 "erasure, and building"));
420 #endif /* IAM_RPMEIU */
422 if (rpmcliRootDir && rpmcliRootDir[1] && (bigMode & ~MODES_FOR_ROOT))
423 argerror(_("--root (-r) may only be specified during "
424 "installation, erasure, querying, and "
425 "database rebuilds"));
428 switch (urlIsURL(rpmcliRootDir)) {
430 if (bigMode & MODES_FOR_ROOT)
433 if (rpmcliRootDir[0] != '/')
434 argerror(_("arguments to --root (-r) must begin with a /"));
440 rpmSetVerbosity(RPMLOG_WARNING);
444 if (bigMode == MODE_RESIGN) {
449 if ((av = poptGetArgs(optCon)) == NULL) {
450 fprintf(stderr, _("no files to sign\n"));
454 if (stat(*av, &sb)) {
455 fprintf(stderr, _("cannot access file %s\n"), *av);
466 if (poptPeekArg(optCon)) {
467 int sigTag = rpmLookupSignatureType(RPMLOOKUPSIG_QUERY);
475 passPhrase = rpmGetPassPhrase(_("Enter pass phrase: "), sigTag);
476 if (passPhrase == NULL) {
477 fprintf(stderr, _("Pass phrase check failed\n"));
481 fprintf(stderr, _("Pass phrase is good.\n"));
482 passPhrase = xstrdup(passPhrase);
486 _("Invalid %%_signature spec in macro file.\n"));
493 argerror(_("--sign may only be used during package building"));
496 /* Make rpmLookupSignatureType() return 0 ("none") from now on */
497 (void) rpmLookupSignatureType(RPMLOOKUPSIG_DISABLE);
499 #endif /* IAM_RPMK */
501 if (rpmcliPipeOutput) {
503 fprintf(stderr, _("creating a pipe for --pipe failed: %m\n"));
507 if (!(pipeChild = fork())) {
508 (void) signal(SIGPIPE, SIG_DFL);
510 (void) dup2(p[0], STDIN_FILENO);
512 (void) execl("/bin/sh", "/bin/sh", "-c", rpmcliPipeOutput, NULL);
513 fprintf(stderr, _("exec failed\n"));
517 (void) dup2(p[1], STDOUT_FILENO);
522 (void) rpmtsSetRootDir(ts, rpmcliRootDir);
526 ec = rpmtsInitDB(ts, 0644);
530 { rpmVSFlags vsflags = rpmExpandNumeric("%{_vsflags_rebuilddb}");
531 rpmVSFlags ovsflags = rpmtsSetVSFlags(ts, vsflags);
532 ec = rpmtsRebuildDB(ts);
533 vsflags = rpmtsSetVSFlags(ts, ovsflags);
536 ec = rpmtsVerifyDB(ts);
538 #endif /* IAM_RPMDB */
542 if (ia->noDeps) ia->installInterfaceFlags |= UNINSTALL_NODEPS;
544 if (!poptPeekArg(optCon)) {
545 argerror(_("no packages given for erase"));
547 ec += rpmErase(ts, ia, (ARGV_const_t) poptGetArgs(optCon));
553 /* RPMTRANS_FLAG_KEEPOBSOLETE */
556 if (ia->transFlags & RPMTRANS_FLAG_NODOCS) {
558 } else if (rpmExpandNumeric("%{_excludedocs}"))
559 ia->transFlags |= RPMTRANS_FLAG_NODOCS;
562 if (ia->noDeps) ia->installInterfaceFlags |= INSTALL_NODEPS;
564 /* we've already ensured !(!ia->prefix && !ia->relocations) */
566 ia->relocations = xmalloc(2 * sizeof(*ia->relocations));
567 ia->relocations[0].oldPath = NULL; /* special case magic */
568 ia->relocations[0].newPath = ia->prefix;
569 ia->relocations[1].oldPath = NULL;
570 ia->relocations[1].newPath = NULL;
571 } else if (ia->relocations) {
572 ia->relocations = xrealloc(ia->relocations,
573 sizeof(*ia->relocations) * (ia->numRelocations + 1));
574 ia->relocations[ia->numRelocations].oldPath = NULL;
575 ia->relocations[ia->numRelocations].newPath = NULL;
578 if (!poptPeekArg(optCon)) {
579 argerror(_("no packages given for install"));
581 /* FIX: ia->relocations[0].newPath undefined */
582 ec += rpmInstall(ts, ia, (ARGV_t) poptGetArgs(optCon));
586 #endif /* IAM_RPMEIU */
590 if (!poptPeekArg(optCon) && !(qva->qva_source == RPMQV_ALL))
591 argerror(_("no arguments given for query"));
593 qva->qva_specQuery = rpmspecQuery;
594 ec = rpmcliQuery(ts, qva, (ARGV_const_t) poptGetArgs(optCon));
595 qva->qva_specQuery = NULL;
599 { rpmVerifyFlags verifyFlags = VERIFY_ALL;
601 verifyFlags &= ~qva->qva_flags;
602 qva->qva_flags = (rpmQueryFlags) verifyFlags;
604 if (!poptPeekArg(optCon) && !(qva->qva_source == RPMQV_ALL))
605 argerror(_("no arguments given for verify"));
606 ec = rpmcliVerify(ts, qva, (ARGV_const_t) poptGetArgs(optCon));
608 #endif /* IAM_RPMQV */
612 { rpmVerifyFlags verifyFlags =
613 (VERIFY_FILEDIGEST|VERIFY_DIGEST|VERIFY_SIGNATURE);
615 verifyFlags &= ~ka->qva_flags;
616 ka->qva_flags = (rpmQueryFlags) verifyFlags;
619 if (!poptPeekArg(optCon))
620 argerror(_("no arguments given"));
621 ka->passPhrase = passPhrase;
622 ec = rpmcliSign(ts, ka, (ARGV_const_t) poptGetArgs(optCon));
624 #endif /* IAM_RPMK */
626 #if !defined(IAM_RPMQV)
630 #if !defined(IAM_RPMK)
634 #if !defined(IAM_RPMDB)
639 #if !defined(IAM_RPMEIU)
644 if (poptPeekArg(optCon) != NULL || argc <= 1 || rpmIsVerbose()) {
645 printUsage(optCon, stderr, 0);
655 optCon = poptFreeContext(optCon);
657 rpmFreeMacros(rpmCLIMacroContext);
661 (void) fclose(stdout);
662 (void) waitpid(pipeChild, &status, 0);
665 /* keeps memory leak checkers quiet */
669 qva->qva_queryFormat = _free(qva->qva_queryFormat);
673 if (ia->relocations != NULL)
674 for (i = 0; i < ia->numRelocations; i++)
675 ia->relocations[i].oldPath = _free(ia->relocations[i].oldPath);
676 ia->relocations = _free(ia->relocations);
679 #if HAVE_MCHECK_H && HAVE_MTRACE
680 muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
683 /* XXX Avoid exit status overflow. Status 255 is special to xargs(1) */
684 if (ec > 254) ec = 254;