Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pkg_install / sign / sign.c
1 /* $OpenBSD: sign.c,v 1.3 1999/10/04 21:46:29 espie Exp $ */
2 /*-
3  * Copyright (c) 1999 Marc Espie.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Marc Espie for the OpenBSD
16  * Project.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS 
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
21  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
22  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: src/usr.sbin/pkg_install/sign/sign.c,v 1.1.2.2 2002/08/20 06:35:08 obrien Exp $
31  * $DragonFly: src/usr.sbin/pkg_install/sign/Attic/sign.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
32  */
33
34 #include <sys/types.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <pwd.h>
41 #include <assert.h>
42 #include "stand.h"
43 #include "pgp.h"
44 #include "gzip.h"
45 #include "extern.h"
46
47 #define COPY_TEMPLATE "%s.sign"
48
49 static int 
50 embed_signature_FILE(orig, dest, sign, filename)
51         /*@temp@*/FILE *orig;
52         /*@temp@*/FILE *dest; 
53         struct signature *sign;
54         const char *filename;
55 {
56         struct mygzip_header h;
57         int c;
58
59         if (gzip_read_header(orig, &h, NULL) == GZIP_NOT_GZIP)
60                 return 0;
61
62         if (gzip_write_header(dest, &h, sign) == 0)
63                 return 0;
64         while ((c = fgetc(orig)) != EOF && fputc(c, dest) != EOF)
65                 ;
66         if (ferror(dest) != 0) 
67                 return 0;
68         return 1;
69 }
70
71 static int 
72 embed_signature(filename, copy, sign)
73         const char *filename;
74         const char *copy; 
75         struct signature *sign;
76 {
77         FILE *orig, *dest;
78         int success;
79         
80         success = 0;
81         orig= fopen(filename, "r");
82         if (orig) {
83                 dest = fopen(copy, "w");
84                 if (dest) {
85                         success = embed_signature_FILE(orig, dest, sign, filename);
86                         if (fclose(dest) != 0)
87                                 success = 0;
88                 }
89                 if (fclose(orig) != 0)
90                         success = 0;
91         }
92         return success;
93 }
94
95 int 
96 sign(filename, type, userid, envp)
97         const char *filename;
98         const char *userid;
99         int type;
100         char *envp[];
101 {
102         char *copy;
103         int result;
104         struct signature *sign;
105         int success;
106
107         switch(type) {
108         case TAG_PGP:
109                 success = retrieve_pgp_signature(filename, &sign, userid, envp);
110                 break;
111         case TAG_SHA1:
112                 success = retrieve_sha1_marker(filename, &sign, userid);
113                 break;
114         case TAG_X509:
115                 success = retrieve_x509_marker(filename, &sign, userid);
116                 break;
117         }
118
119         if (!success) {
120                 fprintf(stderr, "Problem signing %s\n", filename);
121                 free_signature(sign);
122                 return 0;
123         }
124         copy = malloc(strlen(filename)+sizeof(COPY_TEMPLATE));
125         if (copy == NULL) {
126                 fprintf(stderr, "Can't allocate memory\n");
127                 free_signature(sign);
128                 return 0;
129         }
130         sprintf(copy, COPY_TEMPLATE, filename);
131         result = embed_signature(filename, copy, sign);
132         if (result == 0) {
133                 fprintf(stderr, "Can't embed signature in %s\n", filename);
134         } else if (unlink(filename) != 0) {
135                 fprintf(stderr, "Can't unlink original %s\n", filename);
136                 result = 0;
137         } else if (rename(copy, filename) != 0) {
138                 fprintf(stderr, "Can't rename new file %s\n", copy);
139                 result = 0;
140         }
141         free(copy);
142         free_signature(sign);
143         return result;
144 }
145