Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pkg_install / sign / check.c
1 /* $OpenBSD: check.c,v 1.2 1999/10/04 21:46:27 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/check.c,v 1.1.2.2 2002/08/20 06:35:08 obrien Exp $
31  * $DragonFly: src/usr.sbin/pkg_install/sign/Attic/check.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
32  */
33
34 /* Simple code for a stand-alone package checker */
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include "stand.h"
40 #include "pgp.h"
41 #include "gzip.h"
42 #include "extern.h"
43
44 struct checker {
45         void *context;
46         void (*add)(void *, const char *, size_t);
47         int (*get)(void *);
48         int status;
49 };
50
51 #define MAX_CHECKERS 20
52
53 int 
54 check_signature(file, userid, envp, filename)
55         /*@dependent@*/FILE *file;
56         const char *userid;     
57         char *envp[];
58         /*@observer@*/const char *filename;
59 {
60         struct signature *sign;
61         struct mygzip_header h;
62         int status;
63         char buffer[1024];
64         size_t length;
65         struct checker checker[MAX_CHECKERS];
66         struct signature *sweep;
67         int i, j;
68
69         status = read_header_and_diagnose(file, &h, &sign, filename);
70         if (status != 1)
71                 return PKG_UNSIGNED;
72
73         for (sweep = sign, i = 0;  
74                 sweep != NULL && i < MAX_CHECKERS;
75                 sweep=sweep->next, i++) {
76                 switch(sweep->type) {
77                 case TAG_OLD:
78                         fprintf(stderr, "File %s uses old signatures, no longer supported\n",
79                                 filename);
80                         checker[i].context = NULL;
81                         break;
82                 case TAG_X509: 
83                         checker[i].context = new_x509_checker(&h, sweep, userid, envp, filename);
84                         checker[i].add = x509_add;
85                         checker[i].get = x509_sign_ok;
86                         break;
87                 case TAG_SHA1: 
88                         checker[i].context = new_sha1_checker(&h, sweep, userid, envp, filename);
89                         checker[i].add = sha1_add;
90                         checker[i].get = sha1_sign_ok;
91                         break;
92                 case TAG_PGP: 
93                         checker[i].context = new_pgp_checker(&h, sweep, userid, envp, filename);
94                         checker[i].add = pgp_add;
95                         checker[i].get = pgp_sign_ok;
96                         break;
97                 default:
98                         abort();
99                 }
100         }
101         while ((length = fread(buffer, 1, sizeof buffer, file)) > 0) {
102             for (j = 0; j < i; j++) {
103                 if (checker[j].context) {
104                     (*checker[j].add)(checker[j].context, buffer, length);
105                 }
106             }
107         }
108 //      for (j = i-1; j >= 0; j--)
109         for (j = 0; j < i; j++) {
110             if (checker[j].context) {
111                 checker[j].status = (*checker[j].get)(checker[j].context);
112             } else {
113                 checker[j].status = PKG_SIGERROR;
114             }
115         }
116         free_signature(sign);
117         return checker[0].status;
118 }
119