10 #define _RPMPGP_INTERNAL
29 int rpmTempFile(
const char * prefix,
const char ** fnptr,
void * fdptr)
31 const char * tpmacro =
"%{?_tmppath}%{!?_tmppath:/var/tmp/}";
32 const char * tempfn = NULL;
33 const char * tfn = NULL;
34 static int _initialized = 0;
39 if (!prefix) prefix =
"";
45 if (
rpmioMkpath(tempfn, 0755, (uid_t) -1, (gid_t) -1))
50 ran = (unsigned) time(NULL);
52 ran = rand() % 100000;
59 sprintf(tfnbuf,
"rpm-tmp.%u", ran++);
60 tempfn =
_free(tempfn);
63 strcpy(tfnbuf,
"rpm-tmp.XXXXXX");
64 tempfn =
_free(tempfn);
65 tempfn =
rpmGenPath(prefix, tpmacro, mktemp(tfnbuf));
69 if (*tfn ==
'\0')
goto errxit;
83 fd =
Fopen(tempfn,
"w+x.fdio");
85 }
while ((fd == NULL ||
Ferror(fd)) &&
errno == EEXIST);
87 if (fd == NULL ||
Ferror(fd)) {
95 {
struct stat sb, sb2;
96 if (!stat(tfn, &sb) &&
S_ISLNK(sb.st_mode)) {
101 if (sb.st_nlink != 1) {
106 if (fstat(
Fileno(fd), &sb2) == 0) {
107 if (sb2.st_ino != sb.st_ino || sb2.st_dev != sb.st_dev) {
120 tempfn =
_free(tempfn);
127 tempfn =
_free(tempfn);
131 if (fd != NULL) (void)
Fclose(fd);
148 const char * passPhrase)
154 char * sigfile =
alloca(strlen(file)+
sizeof(
".sig"));
164 const char * pw = NULL;
169 addMacro(NULL,
"__plaintext_filename", NULL, file, -1);
170 addMacro(NULL,
"__signature_filename", NULL, sigfile, -1);
172 inpipe[0] = inpipe[1] = 0;
173 if (pipe(inpipe) < 0) {
178 if (!(pid = fork())) {
179 const char *gpg_path =
rpmExpand(
"%{?_gpg_path}", NULL);
181 (void) dup2(inpipe[0], 3);
182 (void) close(inpipe[1]);
184 if (gpg_path && *gpg_path !=
'\0')
185 (void)
setenv(
"GNUPGHOME", gpg_path, 1);
188 cmd =
rpmExpand(
"%{?__gpg_sign_cmd}", NULL);
189 rc = poptParseArgvString(cmd, NULL, (
const char ***)&av);
191 rc = execve(av[0], av+1, environ);
198 delMacro(NULL,
"__plaintext_filename");
199 delMacro(NULL,
"__signature_filename");
208 fpipe = fdopen(inpipe[1],
"w");
209 (void) close(inpipe[0]);
211 fprintf(fpipe,
"%s\n", (pw ? pw :
""));
212 (void) fclose(fpipe);
216 (void) memset((
void *)pw, 0, strlen(pw));
221 (void) waitpid(pid, &status, 0);
223 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
228 if (
Stat(sigfile, &st)) {
230 if (sigfile) (void)
Unlink(sigfile);
242 fd =
Fopen(sigfile,
"r.ufdio");
243 if (fd != NULL && !
Ferror(fd)) {
244 rc = (int)
Fread(*pktp,
sizeof((*pktp)[0]), *pktlenp, fd);
245 if (sigfile) (void)
Unlink(sigfile);
249 *pktp =
_free(*pktp);
298 const char * passPhrase)
302 HE_t he = memset(
alloca(
sizeof(*he)), 0,
sizeof(*he));
307 const char * fn = NULL;
325 {
const char * SHA1 = NULL;
326 fd =
Fopen(file,
"r.fdio");
327 if (fd == NULL ||
Ferror(fd))
329 {
const char item[] =
"Header";
339 (void)
Fclose(fd); fd = NULL;
342 unsigned char * hmagic = NULL;
355 if (hmagic && nmagic > 0)
377 fd =
Fopen(file,
"r.fdio");
378 if (fd == NULL ||
Ferror(fd))
380 {
const char item[] =
"Header";
390 (void)
Fclose(fd); fd = NULL;
394 {
const char item[] =
"Header";
404 (void)
Fclose(fd); fd = NULL;
426 if (fd != NULL) (void)
Fclose(fd);
432 const char * passPhrase)
434 HE_t he = memset(
alloca(
sizeof(*he)), 0,
sizeof(*he));
446 if (
Stat(file, &st) != 0)
462 pkt = memset(
alloca(pktlen), 0, pktlen);
499 if (!(pid = fork())) {
504 xx = close(STDIN_FILENO);
505 xx = close(STDOUT_FILENO);
508 xx = close(STDERR_FILENO);
509 if ((fdno = open(
"/dev/null", O_RDONLY)) != STDIN_FILENO) {
510 xx = dup2(fdno, STDIN_FILENO);
513 if ((fdno = open(
"/dev/null", O_WRONLY)) != STDOUT_FILENO) {
514 xx = dup2(fdno, STDOUT_FILENO);
520 {
const char *gpg_path =
rpmExpand(
"%{?_gpg_path}", NULL);
522 if (gpg_path && *gpg_path !=
'\0')
523 (void)
setenv(
"GNUPGHOME", gpg_path, 1);
525 cmd =
rpmExpand(
"%{?__gpg_check_password_cmd}", NULL);
526 rc = poptParseArgvString(cmd, NULL, (
const char ***)&av);
528 rc = execve(av[0], av+1, environ);
543 xx = (int) write(p[1], pw, strlen(pw));
544 xx = (int) write(p[1],
"\n", 1);
548 (void) memset((
void *)pw, 0, strlen(pw));
553 (void) waitpid(pid, &status, 0);
556 return ((!WIFEXITED(status) || WEXITSTATUS(status)) ? 1 : 0);
583 t =
stpcpy(t,
_(
"Header+Payload size: "));
585 if (sig == NULL || dig == NULL || dig->nbytes == 0) {
591 memcpy(&size, sig,
sizeof(size));
596 sprintf(t,
" Expected(%u) != (%u)\n", (
unsigned)size, (
unsigned)dig->nbytes);
600 sprintf(t,
" (%u)", (
unsigned)dig->nbytes);
619 assert(md5ctx != NULL);
638 if (op != NULL) op->
count--;
641 if (md5len != siglen || memcmp(md5sum, sig, md5len)) {
644 t =
stpcpy(t,
" Expected(");
658 md5sum =
_free(md5sum);
679 const char * SHA1 = NULL;
682 assert(shactx != NULL);
704 if (SHA1 == NULL || strlen(SHA1) != strlen(sig) || strcmp(SHA1, sig)) {
707 t =
stpcpy(t,
" Expected(");
745 assert(rsactx != NULL);
746 assert(sigp != NULL);
753 if (dig->hdrctx == rsactx)
758 switch (sigp->version) {
759 case 3: *t++ =
'3';
break;
760 case 4: *t++ =
'4';
break;
766 if (strcmp(hashname,
"UNKNOWN")) {
771 t =
stpcpy(t,
_(
" signature: "));
777 if (sigp->hash != NULL)
784 trailer[0] = sigp->version;
786 memcpy(trailer+2, &nb,
sizeof(nb));
790 if (op != NULL) op->
count--;
815 t =
stpcpy(t,
", key ID ");
816 (void)
pgpHexCvt(t, sigp->signid+4,
sizeof(sigp->signid)-4);
843 assert(dsactx != NULL);
844 assert(sigp != NULL);
851 if (dig != NULL && dig->hdrsha1ctx == dsactx)
856 switch (sigp->version) {
857 case 3: *t++ =
'3';
break;
858 case 4: *t++ =
'4';
break;
864 if (strcmp(hashname,
"UNKNOWN") && strcmp(hashname,
"SHA1")) {
869 t =
stpcpy(t,
_(
" signature: "));
875 if (sigp->hash != NULL)
882 trailer[0] = sigp->version;
884 memcpy(trailer+2, &nb,
sizeof(nb));
888 if (op != NULL) op->
count--;
913 t =
stpcpy(t,
", key ID ");
914 (void)
pgpHexCvt(t, sigp->signid+4,
sizeof(sigp->signid)-4);
929 if (dig == NULL || sig == NULL || siglen == 0) {
930 sprintf(result,
_(
"Verify signature: BAD PARAMETERS\n"));
939 res =
verifyMD5(dig, result, dig->md5ctx);
942 res =
verifySHA1(dig, result, dig->hdrsha1ctx);
945 res =
verifyRSA(dig, result, dig->hdrctx);
948 res =
verifyDSA(dig, result, dig->hdrsha1ctx);
951 sprintf(result,
_(
"Signature: UNKNOWN (%u)\n"), (
unsigned)sigtag);