Doxygen annotations.
[platform/upstream/rpm.git] / lib / signature.c
1 /** \ingroup signature
2  * \file lib/signature.c
3  */
4
5 /* signature.c - RPM signature functions */
6
7 /* NOTES
8  *
9  * Things have been cleaned up wrt PGP.  We can now handle
10  * signatures of any length (which means you can use any
11  * size key you like).  We also honor PGPPATH finally.
12  */
13
14 #include "system.h"
15
16 #if HAVE_ASM_BYTEORDER_H
17 #include <asm/byteorder.h>
18 #endif
19
20 #include <rpmlib.h>
21 #include <rpmmacro.h>   /* XXX for rpmGetPath */
22
23 #include "md5.h"
24 #include "misc.h"
25 #include "rpmlead.h"
26 #include "signature.h"
27
28 typedef int (*md5func)(const char * fn, /*@out@*/unsigned char * digest);
29
30 int rpmLookupSignatureType(int action)
31 {
32     static int disabled = 0;
33     int rc = 0;
34
35     switch (action) {
36     case RPMLOOKUPSIG_DISABLE:
37         disabled = -2;
38         break;
39     case RPMLOOKUPSIG_ENABLE:
40         disabled = 0;
41         /* fall through */
42         /*@fallthrough@*/
43     case RPMLOOKUPSIG_QUERY:
44         if (disabled)
45             break;      /* Disabled */
46       { const char *name = rpmExpand("%{_signature}", NULL);
47         if (!(name && *name != '%'))
48             rc = 0;
49         else if (!strcasecmp(name, "none"))
50             rc = 0;
51         else if (!strcasecmp(name, "pgp"))
52             rc = RPMSIGTAG_PGP;
53         else if (!strcasecmp(name, "pgp5"))     /* XXX legacy */
54             rc = RPMSIGTAG_PGP;
55         else if (!strcasecmp(name, "gpg"))
56             rc = RPMSIGTAG_GPG;
57         else
58             rc = -1;    /* Invalid %_signature spec in macro file */
59         xfree(name);
60       } break;
61     }
62     return rc;
63 }
64
65 /* rpmDetectPGPVersion() returns the absolute path to the "pgp"  */
66 /* executable of the requested version, or NULL when none found. */
67
68 const char * rpmDetectPGPVersion(pgpVersion *pgpVer)
69 {
70     /* Actually this should support having more then one pgp version. */ 
71     /* At the moment only one version is possible since we only       */
72     /* have one %_pgpbin and one %_pgp_path.                          */
73
74     static pgpVersion saved_pgp_version = PGP_UNKNOWN;
75     const char *pgpbin = rpmGetPath("%{_pgpbin}", NULL);
76
77     if (saved_pgp_version == PGP_UNKNOWN) {
78         char *pgpvbin;
79         struct stat statbuf;
80         
81         if (!(pgpbin && pgpbin[0] != '%') || ! (pgpvbin = (char *)alloca(strlen(pgpbin) + 2))) {
82           if (pgpbin) xfree(pgpbin);
83           saved_pgp_version = -1;
84           return NULL;
85         }
86         sprintf(pgpvbin, "%sv", pgpbin);
87
88         if (stat(pgpvbin, &statbuf) == 0)
89           saved_pgp_version = PGP_5;
90         else if (stat(pgpbin, &statbuf) == 0)
91           saved_pgp_version = PGP_2;
92         else
93           saved_pgp_version = PGP_NOTDETECTED;
94     }
95
96     if (pgpbin && pgpVer)
97         *pgpVer = saved_pgp_version;
98     return pgpbin;
99 }
100
101 static int checkSize(FD_t fd, int size, int sigsize)
102 {
103     int headerArchiveSize;
104     struct stat statbuf;
105
106     fstat(Fileno(fd), &statbuf);
107
108     if (S_ISREG(statbuf.st_mode)) {
109         headerArchiveSize = statbuf.st_size - sizeof(struct rpmlead) - sigsize;
110
111         rpmMessage(RPMMESS_DEBUG, _("sigsize         : %d\n"), sigsize);
112         rpmMessage(RPMMESS_DEBUG, _("Header + Archive: %d\n"), headerArchiveSize);
113         rpmMessage(RPMMESS_DEBUG, _("expected size   : %d\n"), size);
114
115         return size - headerArchiveSize;
116     } else {
117         rpmMessage(RPMMESS_DEBUG, _("file is not regular -- skipping size check\n"));
118         return 0;
119     }
120 }
121
122 /* rpmReadSignature() emulates the new style signatures if it finds an */
123 /* old-style one.  It also immediately verifies the header+archive  */
124 /* size and returns an error if it doesn't match.                   */
125
126 int rpmReadSignature(FD_t fd, Header *headerp, short sig_type)
127 {
128     unsigned char buf[2048];
129     int sigSize, pad;
130     int_32 type, count;
131     int_32 *archSize;
132     Header h;
133
134     if (headerp)
135         *headerp = NULL;
136     
137     switch (sig_type) {
138       case RPMSIG_NONE:
139         rpmMessage(RPMMESS_DEBUG, _("No signature\n"));
140         break;
141       case RPMSIG_PGP262_1024:
142         rpmMessage(RPMMESS_DEBUG, _("Old PGP signature\n"));
143         /* These are always 256 bytes */
144         if (timedRead(fd, buf, 256) != 256)
145             return 1;
146         if (headerp) {
147             *headerp = headerNew();
148             headerAddEntry(*headerp, RPMSIGTAG_PGP, RPM_BIN_TYPE, buf, 152);
149         }
150         break;
151       case RPMSIG_MD5:
152       case RPMSIG_MD5_PGP:
153         rpmError(RPMERR_BADSIGTYPE,
154               _("Old (internal-only) signature!  How did you get that!?"));
155         return 1;
156         /*@notreached@*/ break;
157       case RPMSIG_HEADERSIG:
158         rpmMessage(RPMMESS_DEBUG, _("New Header signature\n"));
159         /* This is a new style signature */
160         h = headerRead(fd, HEADER_MAGIC_YES);
161         if (h == NULL)
162             return 1;
163         sigSize = headerSizeof(h, HEADER_MAGIC_YES);
164         pad = (8 - (sigSize % 8)) % 8; /* 8-byte pad */
165         rpmMessage(RPMMESS_DEBUG, _("Signature size: %d\n"), sigSize);
166         rpmMessage(RPMMESS_DEBUG, _("Signature pad : %d\n"), pad);
167         if (! headerGetEntry(h, RPMSIGTAG_SIZE, &type, (void **)&archSize, &count)) {
168             headerFree(h);
169             return 1;
170         }
171         if (checkSize(fd, *archSize, sigSize + pad)) {
172             headerFree(h);
173             return 1;
174         }
175         if (pad) {
176             if (timedRead(fd, buf, pad) != pad) {
177                 headerFree(h);
178                 return 1;
179             }
180         }
181         if (headerp) {
182             *headerp = h;
183         } else {
184             headerFree(h);
185         }
186         break;
187       default:
188         return 1;
189     }
190
191     return 0;
192 }
193
194 int rpmWriteSignature(FD_t fd, Header header)
195 {
196     int sigSize, pad;
197     unsigned char buf[8];
198     int rc = 0;
199     
200     rc = headerWrite(fd, header, HEADER_MAGIC_YES);
201     if (rc)
202         return rc;
203
204     sigSize = headerSizeof(header, HEADER_MAGIC_YES);
205     pad = (8 - (sigSize % 8)) % 8;
206     if (pad) {
207         rpmMessage(RPMMESS_DEBUG, _("Signature size: %d\n"), sigSize);
208         rpmMessage(RPMMESS_DEBUG, _("Signature pad : %d\n"), pad);
209         memset(buf, 0, pad);
210         if (Fwrite(buf, sizeof(buf[0]), pad, fd) != pad)
211             rc = 1;
212     }
213     return rc;
214 }
215
216 Header rpmNewSignature(void)
217 {
218     Header h = headerNew();
219     return h;
220 }
221
222 void rpmFreeSignature(Header h)
223 {
224     headerFree(h);
225 }
226
227 static int makePGPSignature(const char *file, /*@out@*/void **sig, /*@out@*/int_32 *size,
228                             const char *passPhrase)
229 {
230     char sigfile[1024];
231     int pid, status;
232     int inpipe[2];
233     struct stat statbuf;
234
235     sprintf(sigfile, "%s.sig", file);
236
237     inpipe[0] = inpipe[1] = 0;
238     pipe(inpipe);
239     
240     if (!(pid = fork())) {
241         const char *pgp_path = rpmExpand("%{_pgp_path}", NULL);
242         const char *name = rpmExpand("+myname=\"%{_pgp_name}\"", NULL);
243         const char *path;
244         pgpVersion pgpVer;
245
246         close(STDIN_FILENO);
247         dup2(inpipe[0], 3);
248         close(inpipe[1]);
249
250         dosetenv("PGPPASSFD", "3", 1);
251         if (pgp_path && *pgp_path != '%')
252             dosetenv("PGPPATH", pgp_path, 1);
253
254         /* dosetenv("PGPPASS", passPhrase, 1); */
255
256         if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
257             switch(pgpVer) {
258             case PGP_2:
259                 execlp(path, "pgp", "+batchmode=on", "+verbose=0", "+armor=off",
260                     name, "-sb", file, sigfile, NULL);
261                 break;
262             case PGP_5:
263                 execlp(path,"pgps", "+batchmode=on", "+verbose=0", "+armor=off",
264                     name, "-b", file, "-o", sigfile, NULL);
265                 break;
266             case PGP_UNKNOWN:
267             case PGP_NOTDETECTED:
268                 break;
269             }
270         }
271         rpmError(RPMERR_EXEC, _("Couldn't exec pgp (%s)"), path);
272         _exit(RPMERR_EXEC);
273     }
274
275     close(inpipe[0]);
276     (void)write(inpipe[1], passPhrase, strlen(passPhrase));
277     (void)write(inpipe[1], "\n", 1);
278     close(inpipe[1]);
279
280     (void)waitpid(pid, &status, 0);
281     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
282         rpmError(RPMERR_SIGGEN, _("pgp failed"));
283         return 1;
284     }
285
286     if (stat(sigfile, &statbuf)) {
287         /* PGP failed to write signature */
288         unlink(sigfile);  /* Just in case */
289         rpmError(RPMERR_SIGGEN, _("pgp failed to write signature"));
290         return 1;
291     }
292
293     *size = statbuf.st_size;
294     rpmMessage(RPMMESS_DEBUG, _("PGP sig size: %d\n"), *size);
295     *sig = xmalloc(*size);
296     
297     {   FD_t fd;
298         int rc;
299         fd = Fopen(sigfile, "r.fdio");
300         rc = timedRead(fd, *sig, *size);
301         unlink(sigfile);
302         Fclose(fd);
303         if (rc != *size) {
304             free(*sig);
305             rpmError(RPMERR_SIGGEN, _("unable to read the signature"));
306             return 1;
307         }
308     }
309
310     rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of PGP sig\n"), *size);
311     
312     return 0;
313 }
314
315 /* This is an adaptation of the makePGPSignature function to use GPG instead
316  * of PGP to create signatures.  I think I've made all the changes necessary,
317  * but this could be a good place to start looking if errors in GPG signature
318  * creation crop up.
319  */
320 static int makeGPGSignature(const char *file, /*@out@*/void **sig, /*@out@*/int_32 *size,
321                             const char *passPhrase)
322 {
323     char sigfile[1024];
324     int pid, status;
325     int inpipe[2];
326     FILE *fpipe;
327     struct stat statbuf;
328
329     sprintf(sigfile, "%s.sig", file);
330
331     inpipe[0] = inpipe[1] = 0;
332     pipe(inpipe);
333     
334     if (!(pid = fork())) {
335         const char *gpg_path = rpmExpand("%{_gpg_path}", NULL);
336         const char *name = rpmExpand("%{_gpg_name}", NULL);
337
338         close(STDIN_FILENO);
339         dup2(inpipe[0], 3);
340         close(inpipe[1]);
341
342         if (gpg_path && *gpg_path != '%')
343             dosetenv("GNUPGHOME", gpg_path, 1);
344         execlp("gpg", "gpg",
345                "--batch", "--no-verbose", "--no-armor", "--passphrase-fd", "3",
346                "-u", name, "-sbo", sigfile, file,
347                NULL);
348         rpmError(RPMERR_EXEC, _("Couldn't exec gpg"));
349         _exit(RPMERR_EXEC);
350     }
351
352     fpipe = fdopen(inpipe[1], "w");
353     close(inpipe[0]);
354     fprintf(fpipe, "%s\n", passPhrase);
355     fclose(fpipe);
356
357     (void)waitpid(pid, &status, 0);
358     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
359         rpmError(RPMERR_SIGGEN, _("gpg failed"));
360         return 1;
361     }
362
363     if (stat(sigfile, &statbuf)) {
364         /* GPG failed to write signature */
365         unlink(sigfile);  /* Just in case */
366         rpmError(RPMERR_SIGGEN, _("gpg failed to write signature"));
367         return 1;
368     }
369
370     *size = statbuf.st_size;
371     rpmMessage(RPMMESS_DEBUG, _("GPG sig size: %d\n"), *size);
372     *sig = xmalloc(*size);
373     
374     {   FD_t fd;
375         int rc;
376         fd = Fopen(sigfile, "r.fdio");
377         rc = timedRead(fd, *sig, *size);
378         unlink(sigfile);
379         Fclose(fd);
380         if (rc != *size) {
381             free(*sig);
382             rpmError(RPMERR_SIGGEN, _("unable to read the signature"));
383             return 1;
384         }
385     }
386
387     rpmMessage(RPMMESS_DEBUG, _("Got %d bytes of GPG sig\n"), *size);
388     
389     return 0;
390 }
391
392 int rpmAddSignature(Header header, const char *file, int_32 sigTag, const char *passPhrase)
393 {
394     struct stat statbuf;
395     int_32 size;
396     unsigned char buf[16];
397     void *sig;
398     int ret = -1;
399     
400     switch (sigTag) {
401     case RPMSIGTAG_SIZE:
402         stat(file, &statbuf);
403         size = statbuf.st_size;
404         ret = 0;
405         headerAddEntry(header, RPMSIGTAG_SIZE, RPM_INT32_TYPE, &size, 1);
406         break;
407     case RPMSIGTAG_MD5:
408         ret = mdbinfile(file, buf);
409         if (ret == 0)
410             headerAddEntry(header, sigTag, RPM_BIN_TYPE, buf, 16);
411         break;
412     case RPMSIGTAG_PGP5:        /* XXX legacy */
413     case RPMSIGTAG_PGP:
414         rpmMessage(RPMMESS_VERBOSE, _("Generating signature using PGP.\n"));
415         ret = makePGPSignature(file, &sig, &size, passPhrase);
416         if (ret == 0)
417             headerAddEntry(header, sigTag, RPM_BIN_TYPE, sig, size);
418         break;
419     case RPMSIGTAG_GPG:
420         rpmMessage(RPMMESS_VERBOSE, _("Generating signature using GPG.\n"));
421         ret = makeGPGSignature(file, &sig, &size, passPhrase);
422         if (ret == 0)
423             headerAddEntry(header, sigTag, RPM_BIN_TYPE, sig, size);
424         break;
425     }
426
427     return ret;
428 }
429
430 static int verifySizeSignature(const char *datafile, int_32 size, char *result)
431 {
432     struct stat statbuf;
433
434     stat(datafile, &statbuf);
435     if (size != statbuf.st_size) {
436         sprintf(result, "Header+Archive size mismatch.\n"
437                 "Expected %d, saw %d.\n",
438                 size, (int)statbuf.st_size);
439         return 1;
440     }
441
442     sprintf(result, "Header+Archive size OK: %d bytes\n", size);
443     return 0;
444 }
445
446 #define X(_x)   (unsigned)((_x) & 0xff)
447
448 static int verifyMD5Signature(const char *datafile, unsigned char *sig, 
449                               char *result, md5func fn)
450 {
451     unsigned char md5sum[16];
452
453     fn(datafile, md5sum);
454     if (memcmp(md5sum, sig, 16)) {
455         sprintf(result, "MD5 sum mismatch\n"
456                 "Expected: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
457                 "%02x%02x%02x%02x%02x\n"
458                 "Saw     : %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
459                 "%02x%02x%02x%02x%02x\n",
460                 X(sig[0]),  X(sig[1]),  X(sig[2]),  X(sig[3]),
461                 X(sig[4]),  X(sig[5]),  X(sig[6]),  X(sig[7]),
462                 X(sig[8]),  X(sig[9]),  X(sig[10]), X(sig[11]),
463                 X(sig[12]), X(sig[13]), X(sig[14]), X(sig[15]),
464                 X(md5sum[0]),  X(md5sum[1]),  X(md5sum[2]),  X(md5sum[3]),
465                 X(md5sum[4]),  X(md5sum[5]),  X(md5sum[6]),  X(md5sum[7]),
466                 X(md5sum[8]),  X(md5sum[9]),  X(md5sum[10]), X(md5sum[11]),
467                 X(md5sum[12]), X(md5sum[13]), X(md5sum[14]), X(md5sum[15]) );
468         return 1;
469     }
470
471     sprintf(result, "MD5 sum OK: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
472                     "%02x%02x%02x%02x%02x\n",
473             X(md5sum[0]),  X(md5sum[1]),  X(md5sum[2]),  X(md5sum[3]),
474             X(md5sum[4]),  X(md5sum[5]),  X(md5sum[6]),  X(md5sum[7]),
475             X(md5sum[8]),  X(md5sum[9]),  X(md5sum[10]), X(md5sum[11]),
476             X(md5sum[12]), X(md5sum[13]), X(md5sum[14]), X(md5sum[15]) );
477
478     return 0;
479 }
480
481 static int verifyPGPSignature(const char *datafile, void *sig,
482                               int count, char *result)
483 {
484     int pid, status, outpipe[2];
485     FD_t sfd;
486     char *sigfile;
487     unsigned char buf[BUFSIZ];
488     FILE *file;
489     int res = RPMSIG_OK;
490     const char *path;
491     pgpVersion pgpVer;
492
493     /* What version do we have? */
494     if ((path = rpmDetectPGPVersion(&pgpVer)) == NULL) {
495         errno = ENOENT;
496         rpmError(RPMERR_EXEC, 
497                  _("Could not run pgp.  Use --nopgp to skip PGP checks."));
498         _exit(RPMERR_EXEC);
499     }
500
501     /*
502      * Sad but true: pgp-5.0 returns exit value of 0 on bad signature.
503      * Instead we have to use the text output to detect a bad signature.
504      */
505     if (pgpVer == PGP_5)
506         res = RPMSIG_BAD;
507
508     /* Write out the signature */
509   { const char *tmppath = rpmGetPath("%{_tmppath}", NULL);
510     sigfile = tempnam(tmppath, "rpmsig");
511     xfree(tmppath);
512   }
513     sfd = Fopen(sigfile, "w.fdio");
514     (void)Fwrite(sig, sizeof(char), count, sfd);
515     Fclose(sfd);
516
517     /* Now run PGP */
518     outpipe[0] = outpipe[1] = 0;
519     pipe(outpipe);
520
521     if (!(pid = fork())) {
522         const char *pgp_path = rpmExpand("%{_pgp_path}", NULL);
523
524         close(outpipe[0]);
525         close(STDOUT_FILENO);   /* XXX unnecessary */
526         dup2(outpipe[1], STDOUT_FILENO);
527
528         if (pgp_path && *pgp_path != '%')
529             dosetenv("PGPPATH", pgp_path, 1);
530
531         switch (pgpVer) {
532         case PGP_5:
533             /* Some output (in particular "This signature applies to */
534             /* another message") is _always_ written to stderr; we   */
535             /* want to catch that output, so dup stdout to stderr:   */
536         {   int save_stderr = dup(2);
537             dup2(1, 2);
538             execlp(path, "pgpv", "+batchmode=on", "+verbose=0",
539                    /* Write "Good signature..." to stdout: */
540                    "+OutputInformationFD=1",
541                    /* Write "WARNING: ... is not trusted to... to stdout: */
542                    "+OutputWarningFD=1",
543                    sigfile, "-o", datafile, NULL);
544             /* Restore stderr so we can print the error message below. */
545             dup2(save_stderr, 2);
546             close(save_stderr);
547         }   break;
548         case PGP_2:
549             execlp(path, "pgp", "+batchmode=on", "+verbose=0",
550                    sigfile, datafile, NULL);
551             break;
552         case PGP_UNKNOWN:
553         case PGP_NOTDETECTED:
554             break;
555         }
556
557         fprintf(stderr, _("exec failed!\n"));
558         rpmError(RPMERR_EXEC, 
559                  _("Could not run pgp.  Use --nopgp to skip PGP checks."));
560         _exit(RPMERR_EXEC);
561     }
562
563     close(outpipe[1]);
564     file = fdopen(outpipe[0], "r");
565     result[0] = '\0';
566     while (fgets(buf, 1024, file)) {
567         if (strncmp("File '", buf, 6) &&
568             strncmp("Text is assu", buf, 12) &&
569             strncmp("This signature applies to another message", buf, 41) &&
570             buf[0] != '\n') {
571             strcat(result, buf);
572         }
573         if (!strncmp("WARNING: Can't find the right public key", buf, 40))
574             res = RPMSIG_NOKEY;
575         else if (!strncmp("Signature by unknown keyid:", buf, 27))
576             res = RPMSIG_NOKEY;
577         else if (!strncmp("WARNING: The signing key is not trusted", buf, 39))
578             res = RPMSIG_NOTTRUSTED;
579         else if (!strncmp("Good signature", buf, 14))
580             res = RPMSIG_OK;
581     }
582     fclose(file);
583
584     (void)waitpid(pid, &status, 0);
585     unlink(sigfile);
586     if (!res && (!WIFEXITED(status) || WEXITSTATUS(status))) {
587         res = RPMSIG_BAD;
588     }
589     
590     return res;
591 }
592
593 static int verifyGPGSignature(const char *datafile, void *sig,
594                               int count, char *result)
595 {
596     int pid, status, outpipe[2];
597     FD_t sfd;
598     char *sigfile;
599     unsigned char buf[8192];
600     FILE *file;
601     int res = RPMSIG_OK;
602   
603     /* Write out the signature */
604   { const char *tmppath = rpmGetPath("%{_tmppath}", NULL);
605     sigfile = tempnam(tmppath, "rpmsig");
606     xfree(tmppath);
607   }
608     sfd = Fopen(sigfile, "w.fdio");
609     (void)Fwrite(sig, sizeof(char), count, sfd);
610     Fclose(sfd);
611
612     /* Now run GPG */
613     outpipe[0] = outpipe[1] = 0;
614     pipe(outpipe);
615
616     if (!(pid = fork())) {
617         const char *gpg_path = rpmExpand("%{_gpg_path}", NULL);
618
619         close(outpipe[0]);
620         /* gpg version 0.9 sends its output to stderr. */
621         dup2(outpipe[1], STDERR_FILENO);
622
623         if (gpg_path && *gpg_path != '%')
624             dosetenv("GNUPGHOME", gpg_path, 1);
625
626         execlp("gpg", "gpg",
627                "--batch", "--no-verbose", 
628                "--verify", sigfile, datafile,
629                NULL);
630         fprintf(stderr, _("exec failed!\n"));
631         rpmError(RPMERR_EXEC, 
632                  _("Could not run gpg.  Use --nogpg to skip GPG checks."));
633         _exit(RPMERR_EXEC);
634     }
635
636     close(outpipe[1]);
637     file = fdopen(outpipe[0], "r");
638     result[0] = '\0';
639     while (fgets(buf, 1024, file)) {
640         strcat(result, buf);
641         if (!strncmp("gpg: Can't check signature: Public key not found", buf, 48)) {
642             res = RPMSIG_NOKEY;
643         }
644     }
645     fclose(file);
646   
647     (void)waitpid(pid, &status, 0);
648     unlink(sigfile);
649     if (!res && (!WIFEXITED(status) || WEXITSTATUS(status))) {
650         res = RPMSIG_BAD;
651     }
652     
653     return res;
654 }
655
656 static int checkPassPhrase(const char *passPhrase, const int sigTag)
657 {
658     int passPhrasePipe[2];
659     int pid, status;
660     int fd;
661
662     passPhrasePipe[0] = passPhrasePipe[1] = 0;
663     pipe(passPhrasePipe);
664     if (!(pid = fork())) {
665         close(STDIN_FILENO);
666         close(STDOUT_FILENO);
667         close(passPhrasePipe[1]);
668         if (! rpmIsVerbose()) {
669             close(STDERR_FILENO);
670         }
671         if ((fd = open("/dev/null", O_RDONLY)) != STDIN_FILENO) {
672             dup2(fd, STDIN_FILENO);
673             close(fd);
674         }
675         if ((fd = open("/dev/null", O_WRONLY)) != STDOUT_FILENO) {
676             dup2(fd, STDOUT_FILENO);
677             close(fd);
678         }
679         dup2(passPhrasePipe[0], 3);
680
681         switch (sigTag) {
682         case RPMSIGTAG_GPG:
683         {   const char *gpg_path = rpmExpand("%{_gpg_path}", NULL);
684             const char *name = rpmExpand("%{_gpg_name}", NULL);
685             if (gpg_path && *gpg_path != '%')
686                 dosetenv("GNUPGHOME", gpg_path, 1);
687             execlp("gpg", "gpg",
688                    "--batch", "--no-verbose", "--passphrase-fd", "3",
689                    "-u", name, "-so", "-",
690                    NULL);
691             rpmError(RPMERR_EXEC, _("Couldn't exec gpg"));
692             _exit(RPMERR_EXEC);
693         }   /*@notreached@*/ break;
694         case RPMSIGTAG_PGP5:    /* XXX legacy */
695         case RPMSIGTAG_PGP:
696         {   const char *pgp_path = rpmExpand("%{_pgp_path}", NULL);
697             const char *name = rpmExpand("+myname=\"%{_pgp_name}\"", NULL);
698             const char *path;
699             pgpVersion pgpVer;
700
701             dosetenv("PGPPASSFD", "3", 1);
702             if (pgp_path && *pgp_path != '%')
703                 dosetenv("PGPPATH", pgp_path, 1);
704
705             if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
706                 switch(pgpVer) {
707                 case PGP_2:
708                     execlp(path, "pgp", "+batchmode=on", "+verbose=0",
709                         name, "-sf", NULL);
710                     break;
711                 case PGP_5:     /* XXX legacy */
712                     execlp(path,"pgps", "+batchmode=on", "+verbose=0",
713                         name, "-f", NULL);
714                     break;
715                 case PGP_UNKNOWN:
716                 case PGP_NOTDETECTED:
717                     break;
718                 }
719             }
720             rpmError(RPMERR_EXEC, _("Couldn't exec pgp"));
721             _exit(RPMERR_EXEC);
722         }   /*@notreached@*/ break;
723         default: /* This case should have been screened out long ago. */
724             rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file"));
725             _exit(RPMERR_SIGGEN);
726             /*@notreached@*/ break;
727         }
728     }
729
730     close(passPhrasePipe[0]);
731     (void)write(passPhrasePipe[1], passPhrase, strlen(passPhrase));
732     (void)write(passPhrasePipe[1], "\n", 1);
733     close(passPhrasePipe[1]);
734
735     (void)waitpid(pid, &status, 0);
736     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
737         return 1;
738     }
739
740     /* passPhrase is good */
741     return 0;
742 }
743
744 char *rpmGetPassPhrase(const char *prompt, const int sigTag)
745 {
746     char *pass;
747     int aok;
748
749     switch (sigTag) {
750     case RPMSIGTAG_GPG:
751       { const char *name = rpmExpand("%{_gpg_name}", NULL);
752         aok = (name && *name != '%');
753         xfree(name);
754       }
755         if (!aok) {
756             rpmError(RPMERR_SIGGEN,
757                 _("You must set \"%%_gpg_name\" in your macro file"));
758             return NULL;
759         }
760         break;
761     case RPMSIGTAG_PGP5:        /* XXX legacy */
762     case RPMSIGTAG_PGP: 
763       { const char *name = rpmExpand("%{_pgp_name}", NULL);
764         aok = (name && *name != '%');
765         xfree(name);
766       }
767         if (!aok) {
768             rpmError(RPMERR_SIGGEN,
769                 _("You must set \"%%_pgp_name\" in your macro file"));
770             return NULL;
771         }
772         break;
773     default:
774         /* Currently the calling function (rpm.c:main) is checking this and
775          * doing a better job.  This section should never be accessed.
776          */
777         rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file"));
778         return NULL;
779         /*@notreached@*/ break;
780     }
781
782     pass = /*@-unrecog@*/ getpass( (prompt ? prompt : "") ) /*@=unrecog@*/ ;
783
784     if (checkPassPhrase(pass, sigTag))
785         return NULL;
786
787     return pass;
788 }
789
790 int rpmVerifySignature(const char *file, int_32 sigTag, void *sig, int count,
791                     char *result)
792 {
793     switch (sigTag) {
794     case RPMSIGTAG_SIZE:
795         if (verifySizeSignature(file, *(int_32 *)sig, result)) {
796             return RPMSIG_BAD;
797         }
798         break;
799     case RPMSIGTAG_MD5:
800         if (verifyMD5Signature(file, sig, result, mdbinfile)) {
801             return 1;
802         }
803         break;
804     case RPMSIGTAG_LEMD5_1:
805     case RPMSIGTAG_LEMD5_2:
806         if (verifyMD5Signature(file, sig, result, mdbinfileBroken)) {
807             return 1;
808         }
809         break;
810     case RPMSIGTAG_PGP5:        /* XXX legacy */
811     case RPMSIGTAG_PGP:
812         return verifyPGPSignature(file, sig, count, result);
813         /*@notreached@*/ break;
814     case RPMSIGTAG_GPG:
815         return verifyGPGSignature(file, sig, count, result);
816         /*@notreached@*/ break;
817     default:
818         sprintf(result, "Do not know how to verify sig type %d\n", sigTag);
819         return RPMSIG_UNKNOWN;
820     }
821
822     return RPMSIG_OK;
823 }