- consistent return for all signature verification.
[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 enum rpmVerifySignatureReturn
431 verifySizeSignature(const char *datafile, int_32 size, char *result)
432 {
433     struct stat statbuf;
434
435     stat(datafile, &statbuf);
436     if (size != statbuf.st_size) {
437         sprintf(result, "Header+Archive size mismatch.\n"
438                 "Expected %d, saw %d.\n",
439                 size, (int)statbuf.st_size);
440         return RPMSIG_BAD;
441     }
442
443     sprintf(result, "Header+Archive size OK: %d bytes\n", size);
444     return RPMSIG_OK;
445 }
446
447 #define X(_x)   (unsigned)((_x) & 0xff)
448
449 static enum rpmVerifySignatureReturn
450 verifyMD5Signature(const char *datafile, unsigned char *sig, 
451                               char *result, md5func fn)
452 {
453     unsigned char md5sum[16];
454
455     fn(datafile, md5sum);
456     if (memcmp(md5sum, sig, 16)) {
457         sprintf(result, "MD5 sum mismatch\n"
458                 "Expected: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
459                 "%02x%02x%02x%02x%02x\n"
460                 "Saw     : %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
461                 "%02x%02x%02x%02x%02x\n",
462                 X(sig[0]),  X(sig[1]),  X(sig[2]),  X(sig[3]),
463                 X(sig[4]),  X(sig[5]),  X(sig[6]),  X(sig[7]),
464                 X(sig[8]),  X(sig[9]),  X(sig[10]), X(sig[11]),
465                 X(sig[12]), X(sig[13]), X(sig[14]), X(sig[15]),
466                 X(md5sum[0]),  X(md5sum[1]),  X(md5sum[2]),  X(md5sum[3]),
467                 X(md5sum[4]),  X(md5sum[5]),  X(md5sum[6]),  X(md5sum[7]),
468                 X(md5sum[8]),  X(md5sum[9]),  X(md5sum[10]), X(md5sum[11]),
469                 X(md5sum[12]), X(md5sum[13]), X(md5sum[14]), X(md5sum[15]) );
470         return RPMSIG_BAD;
471     }
472
473     sprintf(result, "MD5 sum OK: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
474                     "%02x%02x%02x%02x%02x\n",
475             X(md5sum[0]),  X(md5sum[1]),  X(md5sum[2]),  X(md5sum[3]),
476             X(md5sum[4]),  X(md5sum[5]),  X(md5sum[6]),  X(md5sum[7]),
477             X(md5sum[8]),  X(md5sum[9]),  X(md5sum[10]), X(md5sum[11]),
478             X(md5sum[12]), X(md5sum[13]), X(md5sum[14]), X(md5sum[15]) );
479
480     return RPMSIG_OK;
481 }
482
483 static enum rpmVerifySignatureReturn
484 verifyPGPSignature(const char *datafile, void *sig, int count, char *result)
485 {
486     int pid, status, outpipe[2];
487     FD_t sfd;
488     char *sigfile;
489     unsigned char buf[BUFSIZ];
490     FILE *file;
491     int res = RPMSIG_OK;
492     const char *path;
493     pgpVersion pgpVer;
494
495     /* What version do we have? */
496     if ((path = rpmDetectPGPVersion(&pgpVer)) == NULL) {
497         errno = ENOENT;
498         rpmError(RPMERR_EXEC, 
499                  _("Could not run pgp.  Use --nopgp to skip PGP checks."));
500         _exit(RPMERR_EXEC);
501     }
502
503     /*
504      * Sad but true: pgp-5.0 returns exit value of 0 on bad signature.
505      * Instead we have to use the text output to detect a bad signature.
506      */
507     if (pgpVer == PGP_5)
508         res = RPMSIG_BAD;
509
510     /* Write out the signature */
511   { const char *tmppath = rpmGetPath("%{_tmppath}", NULL);
512     sigfile = tempnam(tmppath, "rpmsig");
513     xfree(tmppath);
514   }
515     sfd = Fopen(sigfile, "w.fdio");
516     (void)Fwrite(sig, sizeof(char), count, sfd);
517     Fclose(sfd);
518
519     /* Now run PGP */
520     outpipe[0] = outpipe[1] = 0;
521     pipe(outpipe);
522
523     if (!(pid = fork())) {
524         const char *pgp_path = rpmExpand("%{_pgp_path}", NULL);
525
526         close(outpipe[0]);
527         close(STDOUT_FILENO);   /* XXX unnecessary */
528         dup2(outpipe[1], STDOUT_FILENO);
529
530         if (pgp_path && *pgp_path != '%')
531             dosetenv("PGPPATH", pgp_path, 1);
532
533         switch (pgpVer) {
534         case PGP_5:
535             /* Some output (in particular "This signature applies to */
536             /* another message") is _always_ written to stderr; we   */
537             /* want to catch that output, so dup stdout to stderr:   */
538         {   int save_stderr = dup(2);
539             dup2(1, 2);
540             execlp(path, "pgpv", "+batchmode=on", "+verbose=0",
541                    /* Write "Good signature..." to stdout: */
542                    "+OutputInformationFD=1",
543                    /* Write "WARNING: ... is not trusted to... to stdout: */
544                    "+OutputWarningFD=1",
545                    sigfile, "-o", datafile, NULL);
546             /* Restore stderr so we can print the error message below. */
547             dup2(save_stderr, 2);
548             close(save_stderr);
549         }   break;
550         case PGP_2:
551             execlp(path, "pgp", "+batchmode=on", "+verbose=0",
552                    sigfile, datafile, NULL);
553             break;
554         case PGP_UNKNOWN:
555         case PGP_NOTDETECTED:
556             break;
557         }
558
559         fprintf(stderr, _("exec failed!\n"));
560         rpmError(RPMERR_EXEC, 
561                  _("Could not run pgp.  Use --nopgp to skip PGP checks."));
562         _exit(RPMERR_EXEC);
563     }
564
565     close(outpipe[1]);
566     file = fdopen(outpipe[0], "r");
567     result[0] = '\0';
568     while (fgets(buf, 1024, file)) {
569         if (strncmp("File '", buf, 6) &&
570             strncmp("Text is assu", buf, 12) &&
571             strncmp("This signature applies to another message", buf, 41) &&
572             buf[0] != '\n') {
573             strcat(result, buf);
574         }
575         if (!strncmp("WARNING: Can't find the right public key", buf, 40))
576             res = RPMSIG_NOKEY;
577         else if (!strncmp("Signature by unknown keyid:", buf, 27))
578             res = RPMSIG_NOKEY;
579         else if (!strncmp("WARNING: The signing key is not trusted", buf, 39))
580             res = RPMSIG_NOTTRUSTED;
581         else if (!strncmp("Good signature", buf, 14))
582             res = RPMSIG_OK;
583     }
584     fclose(file);
585
586     (void)waitpid(pid, &status, 0);
587     unlink(sigfile);
588     if (!res && (!WIFEXITED(status) || WEXITSTATUS(status))) {
589         res = RPMSIG_BAD;
590     }
591     
592     return res;
593 }
594
595 static enum rpmVerifySignatureReturn
596 verifyGPGSignature(const char *datafile, void *sig, int count, char *result)
597 {
598     int pid, status, outpipe[2];
599     FD_t sfd;
600     char *sigfile;
601     unsigned char buf[8192];
602     FILE *file;
603     int res = RPMSIG_OK;
604   
605     /* Write out the signature */
606   { const char *tmppath = rpmGetPath("%{_tmppath}", NULL);
607     sigfile = tempnam(tmppath, "rpmsig");
608     xfree(tmppath);
609   }
610     sfd = Fopen(sigfile, "w.fdio");
611     (void)Fwrite(sig, sizeof(char), count, sfd);
612     Fclose(sfd);
613
614     /* Now run GPG */
615     outpipe[0] = outpipe[1] = 0;
616     pipe(outpipe);
617
618     if (!(pid = fork())) {
619         const char *gpg_path = rpmExpand("%{_gpg_path}", NULL);
620
621         close(outpipe[0]);
622         /* gpg version 0.9 sends its output to stderr. */
623         dup2(outpipe[1], STDERR_FILENO);
624
625         if (gpg_path && *gpg_path != '%')
626             dosetenv("GNUPGHOME", gpg_path, 1);
627
628         execlp("gpg", "gpg",
629                "--batch", "--no-verbose", 
630                "--verify", sigfile, datafile,
631                NULL);
632         fprintf(stderr, _("exec failed!\n"));
633         rpmError(RPMERR_EXEC, 
634                  _("Could not run gpg.  Use --nogpg to skip GPG checks."));
635         _exit(RPMERR_EXEC);
636     }
637
638     close(outpipe[1]);
639     file = fdopen(outpipe[0], "r");
640     result[0] = '\0';
641     while (fgets(buf, 1024, file)) {
642         strcat(result, buf);
643         if (!strncmp("gpg: Can't check signature: Public key not found", buf, 48)) {
644             res = RPMSIG_NOKEY;
645         }
646     }
647     fclose(file);
648   
649     (void)waitpid(pid, &status, 0);
650     unlink(sigfile);
651     if (!res && (!WIFEXITED(status) || WEXITSTATUS(status))) {
652         res = RPMSIG_BAD;
653     }
654     
655     return res;
656 }
657
658 static int checkPassPhrase(const char *passPhrase, const int sigTag)
659 {
660     int passPhrasePipe[2];
661     int pid, status;
662     int fd;
663
664     passPhrasePipe[0] = passPhrasePipe[1] = 0;
665     pipe(passPhrasePipe);
666     if (!(pid = fork())) {
667         close(STDIN_FILENO);
668         close(STDOUT_FILENO);
669         close(passPhrasePipe[1]);
670         if (! rpmIsVerbose()) {
671             close(STDERR_FILENO);
672         }
673         if ((fd = open("/dev/null", O_RDONLY)) != STDIN_FILENO) {
674             dup2(fd, STDIN_FILENO);
675             close(fd);
676         }
677         if ((fd = open("/dev/null", O_WRONLY)) != STDOUT_FILENO) {
678             dup2(fd, STDOUT_FILENO);
679             close(fd);
680         }
681         dup2(passPhrasePipe[0], 3);
682
683         switch (sigTag) {
684         case RPMSIGTAG_GPG:
685         {   const char *gpg_path = rpmExpand("%{_gpg_path}", NULL);
686             const char *name = rpmExpand("%{_gpg_name}", NULL);
687             if (gpg_path && *gpg_path != '%')
688                 dosetenv("GNUPGHOME", gpg_path, 1);
689             execlp("gpg", "gpg",
690                    "--batch", "--no-verbose", "--passphrase-fd", "3",
691                    "-u", name, "-so", "-",
692                    NULL);
693             rpmError(RPMERR_EXEC, _("Couldn't exec gpg"));
694             _exit(RPMERR_EXEC);
695         }   /*@notreached@*/ break;
696         case RPMSIGTAG_PGP5:    /* XXX legacy */
697         case RPMSIGTAG_PGP:
698         {   const char *pgp_path = rpmExpand("%{_pgp_path}", NULL);
699             const char *name = rpmExpand("+myname=\"%{_pgp_name}\"", NULL);
700             const char *path;
701             pgpVersion pgpVer;
702
703             dosetenv("PGPPASSFD", "3", 1);
704             if (pgp_path && *pgp_path != '%')
705                 dosetenv("PGPPATH", pgp_path, 1);
706
707             if ((path = rpmDetectPGPVersion(&pgpVer)) != NULL) {
708                 switch(pgpVer) {
709                 case PGP_2:
710                     execlp(path, "pgp", "+batchmode=on", "+verbose=0",
711                         name, "-sf", NULL);
712                     break;
713                 case PGP_5:     /* XXX legacy */
714                     execlp(path,"pgps", "+batchmode=on", "+verbose=0",
715                         name, "-f", NULL);
716                     break;
717                 case PGP_UNKNOWN:
718                 case PGP_NOTDETECTED:
719                     break;
720                 }
721             }
722             rpmError(RPMERR_EXEC, _("Couldn't exec pgp"));
723             _exit(RPMERR_EXEC);
724         }   /*@notreached@*/ break;
725         default: /* This case should have been screened out long ago. */
726             rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file"));
727             _exit(RPMERR_SIGGEN);
728             /*@notreached@*/ break;
729         }
730     }
731
732     close(passPhrasePipe[0]);
733     (void)write(passPhrasePipe[1], passPhrase, strlen(passPhrase));
734     (void)write(passPhrasePipe[1], "\n", 1);
735     close(passPhrasePipe[1]);
736
737     (void)waitpid(pid, &status, 0);
738     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
739         return 1;
740     }
741
742     /* passPhrase is good */
743     return 0;
744 }
745
746 char *rpmGetPassPhrase(const char *prompt, const int sigTag)
747 {
748     char *pass;
749     int aok;
750
751     switch (sigTag) {
752     case RPMSIGTAG_GPG:
753       { const char *name = rpmExpand("%{_gpg_name}", NULL);
754         aok = (name && *name != '%');
755         xfree(name);
756       }
757         if (!aok) {
758             rpmError(RPMERR_SIGGEN,
759                 _("You must set \"%%_gpg_name\" in your macro file"));
760             return NULL;
761         }
762         break;
763     case RPMSIGTAG_PGP5:        /* XXX legacy */
764     case RPMSIGTAG_PGP: 
765       { const char *name = rpmExpand("%{_pgp_name}", NULL);
766         aok = (name && *name != '%');
767         xfree(name);
768       }
769         if (!aok) {
770             rpmError(RPMERR_SIGGEN,
771                 _("You must set \"%%_pgp_name\" in your macro file"));
772             return NULL;
773         }
774         break;
775     default:
776         /* Currently the calling function (rpm.c:main) is checking this and
777          * doing a better job.  This section should never be accessed.
778          */
779         rpmError(RPMERR_SIGGEN, _("Invalid %%_signature spec in macro file"));
780         return NULL;
781         /*@notreached@*/ break;
782     }
783
784     pass = /*@-unrecog@*/ getpass( (prompt ? prompt : "") ) /*@=unrecog@*/ ;
785
786     if (checkPassPhrase(pass, sigTag))
787         return NULL;
788
789     return pass;
790 }
791
792 enum rpmVerifySignatureReturn
793 rpmVerifySignature(const char *file, int_32 sigTag, void *sig, int count,
794                     char *result)
795 {
796     switch (sigTag) {
797     case RPMSIGTAG_SIZE:
798         return verifySizeSignature(file, *(int_32 *)sig, result);
799         /*@notreached@*/ break;
800     case RPMSIGTAG_MD5:
801         return verifyMD5Signature(file, sig, result, mdbinfile);
802         /*@notreached@*/ break;
803     case RPMSIGTAG_LEMD5_1:
804     case RPMSIGTAG_LEMD5_2:
805         return verifyMD5Signature(file, sig, result, mdbinfileBroken);
806         /*@notreached@*/ break;
807     case RPMSIGTAG_PGP5:        /* XXX legacy */
808     case RPMSIGTAG_PGP:
809         return verifyPGPSignature(file, sig, count, result);
810         /*@notreached@*/ break;
811     case RPMSIGTAG_GPG:
812         return verifyGPGSignature(file, sig, count, result);
813         /*@notreached@*/ break;
814     default:
815         sprintf(result, "Do not know how to verify sig type %d\n", sigTag);
816         return RPMSIG_UNKNOWN;
817     }
818     /*@notreached@*/
819     return RPMSIG_OK;
820 }