2 * Copyright (c) 1999 Marc Espie.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Marc Espie for the OpenBSD
17 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
21 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $OpenBSD: sign.c,v 1.3 1999/10/04 21:46:29 espie Exp $
30 * $FreeBSD: src/usr.sbin/pkg_install/sign/sign.c,v 1.4 2004/06/29 19:06:42 eik Exp $
31 * $DragonFly: src/usr.sbin/pkg_install/sign/Attic/sign.c,v 1.3 2004/07/30 04:46:14 dillon Exp $
34 #include <sys/types.h>
48 #define COPY_TEMPLATE "%s.sign"
51 embed_signature_FILE(orig, dest, sign, filename)
54 struct signature *sign;
57 struct mygzip_header h;
60 if (gzip_read_header(orig, &h, NULL) == GZIP_NOT_GZIP)
63 if (gzip_write_header(dest, &h, sign) == 0)
65 while ((c = fgetc(orig)) != EOF && fputc(c, dest) != EOF)
67 if (ferror(dest) != 0)
73 embed_signature(filename, copy, sign)
76 struct signature *sign;
82 orig= fopen(filename, "r");
84 dest = fopen(copy, "w");
86 success = embed_signature_FILE(orig, dest, sign, filename);
87 if (fclose(dest) != 0)
90 if (fclose(orig) != 0)
97 sign(filename, type, userid, envp)
105 struct signature *sign;
111 success = retrieve_pgp_signature(filename, &sign, userid, envp);
114 success = retrieve_sha1_marker(filename, &sign, userid);
117 success = retrieve_x509_marker(filename, &sign, userid);
122 fprintf(stderr, "Problem signing %s\n", filename);
123 free_signature(sign);
126 copy = malloc(strlen(filename)+sizeof(COPY_TEMPLATE));
128 fprintf(stderr, "Can't allocate memory\n");
129 free_signature(sign);
132 sprintf(copy, COPY_TEMPLATE, filename);
133 result = embed_signature(filename, copy, sign);
135 fprintf(stderr, "Can't embed signature in %s\n", filename);
136 } else if (unlink(filename) != 0) {
137 fprintf(stderr, "Can't unlink original %s\n", filename);
139 } else if (rename(copy, filename) != 0) {
140 fprintf(stderr, "Can't rename new file %s\n", copy);
144 free_signature(sign);