Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pkg_install / sign / main.c
1 /* $OpenBSD: main.c,v 1.2 1999/10/04 21:46:28 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/main.c,v 1.1.2.2 2002/08/20 06:35:08 obrien Exp $
31  * $DragonFly: src/usr.sbin/pkg_install/sign/Attic/main.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
32  */
33
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include "stand.h"
41 #include "gzip.h"
42 #include "pgp.h"
43 #include "extern.h"
44
45 #ifdef __OpenBSD__
46 extern char *__progname;
47 #define argv0   __progname
48 #else
49 static char *argv0;
50 #endif
51
52 #define NM_SIGN "pkg_sign"
53
54 int verbose = 0;
55 int quiet = 0;
56 char *userkey = NULL;
57
58 static void 
59 usage()
60 {
61         fprintf(stderr, "usage: %s [-sc] [-t type] [-u userid] [-k keyfile] pkg1 ...\n", argv0);
62         exit(EXIT_FAILURE);
63 }
64
65 #define SIGN 0
66 #define CHECK 1
67
68 /* wrapper for the check_signature function (open file if needed) */
69 static int
70 check(filename, type, userid, envp)
71         /*@observer@*/const char *filename;
72         int type;
73         /*@null@*/const char *userid;
74         char *envp[];
75 {
76         int result;
77         FILE *file;
78
79         if (strcmp(filename, "-") == 0)
80                 return check_signature(stdin, userid, envp, "stdin");
81         file = fopen(filename, "r");
82         if (file == NULL) {
83                 fprintf(stderr, "Can't open %s\n", filename);
84                 return 0;
85         }
86         result = check_signature(file, userid, envp, filename);
87         if (fclose(file) == 0) {
88                 if (result == PKG_BADSIG || result == PKG_SIGERROR)
89                         return 0;
90                 else
91                         return 1;
92         } else
93                 return 0;
94 }
95
96 int 
97 main(argc, argv, envp)
98         int argc; 
99         char *argv[];
100         char *envp[];
101 {
102         int success = 1;
103         int ch;
104         char *userid = NULL;
105         int mode;
106         int i;
107         int type = TAG_ANY;
108
109 /* #ifndef BSD4_4 */
110         set_program_name(argv[0]);
111 /* #endif */
112 #ifdef CHECKER_ONLY
113         mode = CHECK;
114 #else
115 #ifndef __OpenBSD__
116         if ((argv0 = strrchr(argv[0], '/')) != NULL)
117                 argv0++;
118         else
119                 argv0 = argv[0];
120 #endif
121         if (strcmp(argv0, NM_SIGN) == 0)
122                 mode = SIGN;
123         else
124                 mode = CHECK;
125 #endif
126
127         while ((ch = getopt(argc, argv, "t:u:k:qscv")) != -1) {
128                 switch(ch) {
129                 case 't':
130                         if (strcmp(optarg, "pgp") == 0) 
131                                 type = TAG_PGP;
132                         else if (strcmp(optarg, "sha1") == 0)
133                                 type = TAG_SHA1;
134                         else if (strcmp(optarg, "x509") == 0)
135                                 type = TAG_X509;
136                         else
137                                 usage();
138                         break;
139                 case 'u':
140                         userid = strdup(optarg);
141                         break;
142
143                 case 'k':
144                         userkey = optarg;
145                         break;
146
147                 case 'q':
148                         quiet = 1;
149                         break;
150
151 #ifndef CHECKER_ONLY
152                 case 's':
153                         mode = SIGN;
154                         break;
155 #endif
156                 case 'c':
157                         mode = CHECK;
158                         break;
159
160                 case 'v':
161                         verbose = 1;
162                         break;
163                 default:
164                         usage();
165                 }
166         }
167         argc -= optind;
168         argv += optind;
169         if (argc == 0) {
170                 if (mode == CHECK)
171                         success &= check("-", 0, userid, envp);
172                 else
173                         usage();
174         }
175         
176 #ifndef CHECKER_ONLY
177         if (mode == SIGN && type == TAG_ANY)
178                 type = TAG_PGP;
179         if (mode == SIGN && type == TAG_PGP)
180                 handle_pgp_passphrase();
181 #endif
182         for (i = 0; i < argc; i++)
183                 success &= (mode == SIGN ? sign : check)(argv[i], type, userid, envp);
184         exit(success == 1 ? EXIT_SUCCESS : EXIT_FAILURE);
185 }