]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pkg_install/sign/sign.c
This commit was generated by cvs2svn to compensate for changes in r161389,
[FreeBSD/FreeBSD.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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/types.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <stdio.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <pwd.h>
42 #include <assert.h>
43 #include "stand.h"
44 #include "pgp.h"
45 #include "gzip.h"
46 #include "extern.h"
47
48 #define COPY_TEMPLATE "%s.sign"
49
50 static int 
51 embed_signature_FILE(orig, dest, sign, filename)
52         /*@temp@*/FILE *orig;
53         /*@temp@*/FILE *dest; 
54         struct signature *sign;
55         const char *filename;
56 {
57         struct mygzip_header h;
58         int c;
59
60         if (gzip_read_header(orig, &h, NULL) == GZIP_NOT_GZIP)
61                 return 0;
62
63         if (gzip_write_header(dest, &h, sign) == 0)
64                 return 0;
65         while ((c = fgetc(orig)) != EOF && fputc(c, dest) != EOF)
66                 ;
67         if (ferror(dest) != 0) 
68                 return 0;
69         return 1;
70 }
71
72 static int 
73 embed_signature(filename, copy, sign)
74         const char *filename;
75         const char *copy; 
76         struct signature *sign;
77 {
78         FILE *orig, *dest;
79         int success;
80         
81         success = 0;
82         orig= fopen(filename, "r");
83         if (orig) {
84                 dest = fopen(copy, "w");
85                 if (dest) {
86                         success = embed_signature_FILE(orig, dest, sign, filename);
87                         if (fclose(dest) != 0)
88                                 success = 0;
89                 }
90                 if (fclose(orig) != 0)
91                         success = 0;
92         }
93         return success;
94 }
95
96 int 
97 sign(filename, type, userid, envp)
98         const char *filename;
99         const char *userid;
100         int type;
101         char *envp[];
102 {
103         char *copy;
104         int result;
105         struct signature *sign;
106         int success;
107
108         sign = NULL;
109         switch(type) {
110         case TAG_PGP:
111                 success = retrieve_pgp_signature(filename, &sign, userid, envp);
112                 break;
113         case TAG_SHA1:
114                 success = retrieve_sha1_marker(filename, &sign, userid);
115                 break;
116         case TAG_X509:
117                 success = retrieve_x509_marker(filename, &sign, userid);
118                 break;
119         default:
120                 success = 0;
121                 fprintf(stderr, "Unknown type %d\n", type);
122         }
123
124         if (!success) {
125                 fprintf(stderr, "Problem signing %s\n", filename);
126                 free_signature(sign);
127                 return 0;
128         }
129         copy = malloc(strlen(filename)+sizeof(COPY_TEMPLATE));
130         if (copy == NULL) {
131                 fprintf(stderr, "Can't allocate memory\n");
132                 free_signature(sign);
133                 return 0;
134         }
135         sprintf(copy, COPY_TEMPLATE, filename);
136         result = embed_signature(filename, copy, sign);
137         if (result == 0) {
138                 fprintf(stderr, "Can't embed signature in %s\n", filename);
139         } else if (unlink(filename) != 0) {
140                 fprintf(stderr, "Can't unlink original %s\n", filename);
141                 result = 0;
142         } else if (rename(copy, filename) != 0) {
143                 fprintf(stderr, "Can't rename new file %s\n", copy);
144                 result = 0;
145         }
146         free(copy);
147         free_signature(sign);
148         return result;
149 }
150