xref: /onnv-gate/usr/src/cmd/svr4pkg/pkgadm/listcert.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM 
28*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
29*9781SMoriah.Waterland@Sun.COM #include <stdarg.h>
30*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
31*9781SMoriah.Waterland@Sun.COM #include <string.h>
32*9781SMoriah.Waterland@Sun.COM #include <sys/types.h>
33*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
34*9781SMoriah.Waterland@Sun.COM #include <signal.h>
35*9781SMoriah.Waterland@Sun.COM #include <locale.h>
36*9781SMoriah.Waterland@Sun.COM #include <sys/param.h>
37*9781SMoriah.Waterland@Sun.COM #include <openssl/bio.h>
38*9781SMoriah.Waterland@Sun.COM 
39*9781SMoriah.Waterland@Sun.COM #include <libinst.h>
40*9781SMoriah.Waterland@Sun.COM #include <pkglib.h>
41*9781SMoriah.Waterland@Sun.COM #include <pkgerr.h>
42*9781SMoriah.Waterland@Sun.COM #include <keystore.h>
43*9781SMoriah.Waterland@Sun.COM #include "pkgadm.h"
44*9781SMoriah.Waterland@Sun.COM #include "pkgadm_msgs.h"
45*9781SMoriah.Waterland@Sun.COM 
46*9781SMoriah.Waterland@Sun.COM /*
47*9781SMoriah.Waterland@Sun.COM  * Name:	listcert
48*9781SMoriah.Waterland@Sun.COM  * Desc:  Lists one or more certificates from the keystore
49*9781SMoriah.Waterland@Sun.COM  * Syntax:	listcert [-a app] [-f format] [-k keystore] \
50*9781SMoriah.Waterland@Sun.COM  *	[-n name] [-o outfile] [-P passarg] [-R altroot]
51*9781SMoriah.Waterland@Sun.COM  */
52*9781SMoriah.Waterland@Sun.COM int
listcert(int argc,char ** argv)53*9781SMoriah.Waterland@Sun.COM listcert(int argc, char **argv)
54*9781SMoriah.Waterland@Sun.COM {
55*9781SMoriah.Waterland@Sun.COM 	int				i;
56*9781SMoriah.Waterland@Sun.COM 	char				keystore_file[MAXPATHLEN] = "";
57*9781SMoriah.Waterland@Sun.COM 	char				*keystore_base = NULL;
58*9781SMoriah.Waterland@Sun.COM 	char				*homedir;
59*9781SMoriah.Waterland@Sun.COM 	char				*passarg = NULL;
60*9781SMoriah.Waterland@Sun.COM 	char				*altroot = NULL;
61*9781SMoriah.Waterland@Sun.COM 	char				*prog = NULL;
62*9781SMoriah.Waterland@Sun.COM 	char				*format_str = NULL;
63*9781SMoriah.Waterland@Sun.COM 	keystore_encoding_format_t	format;
64*9781SMoriah.Waterland@Sun.COM 	char				*alias = NULL;
65*9781SMoriah.Waterland@Sun.COM 	char				*outfile_str = NULL;
66*9781SMoriah.Waterland@Sun.COM 	FILE				*outfile = NULL;
67*9781SMoriah.Waterland@Sun.COM 	int				ret = 1;
68*9781SMoriah.Waterland@Sun.COM 	PKG_ERR				*err = NULL;
69*9781SMoriah.Waterland@Sun.COM 	keystore_handle_t		keystore = NULL;
70*9781SMoriah.Waterland@Sun.COM 
71*9781SMoriah.Waterland@Sun.COM 	while ((i = getopt(argc, argv, ":a:f:k:n:o:P:R:")) != EOF) {
72*9781SMoriah.Waterland@Sun.COM 		switch (i) {
73*9781SMoriah.Waterland@Sun.COM 		case 'a':
74*9781SMoriah.Waterland@Sun.COM 			prog = optarg;
75*9781SMoriah.Waterland@Sun.COM 			break;
76*9781SMoriah.Waterland@Sun.COM 		case 'f':
77*9781SMoriah.Waterland@Sun.COM 			format_str = optarg;
78*9781SMoriah.Waterland@Sun.COM 			break;
79*9781SMoriah.Waterland@Sun.COM 		case 'k':
80*9781SMoriah.Waterland@Sun.COM 			keystore_base = optarg;
81*9781SMoriah.Waterland@Sun.COM 			break;
82*9781SMoriah.Waterland@Sun.COM 		case 'n':
83*9781SMoriah.Waterland@Sun.COM 			alias = optarg;
84*9781SMoriah.Waterland@Sun.COM 			break;
85*9781SMoriah.Waterland@Sun.COM 		case 'o':
86*9781SMoriah.Waterland@Sun.COM 			outfile_str = optarg;
87*9781SMoriah.Waterland@Sun.COM 			break;
88*9781SMoriah.Waterland@Sun.COM 		case 'P':
89*9781SMoriah.Waterland@Sun.COM 			passarg = optarg;
90*9781SMoriah.Waterland@Sun.COM 			break;
91*9781SMoriah.Waterland@Sun.COM 		case 'R':
92*9781SMoriah.Waterland@Sun.COM 			altroot = optarg;
93*9781SMoriah.Waterland@Sun.COM 			break;
94*9781SMoriah.Waterland@Sun.COM 		case ':':
95*9781SMoriah.Waterland@Sun.COM 			log_msg(LOG_MSG_ERR, MSG_MISSING_OPERAND, optopt);
96*9781SMoriah.Waterland@Sun.COM 			/* fallthrough intentional */
97*9781SMoriah.Waterland@Sun.COM 		case '?':
98*9781SMoriah.Waterland@Sun.COM 		default:
99*9781SMoriah.Waterland@Sun.COM 			log_msg(LOG_MSG_ERR, MSG_USAGE);
100*9781SMoriah.Waterland@Sun.COM 			goto cleanup;
101*9781SMoriah.Waterland@Sun.COM 		}
102*9781SMoriah.Waterland@Sun.COM 	}
103*9781SMoriah.Waterland@Sun.COM 
104*9781SMoriah.Waterland@Sun.COM 	/* should be no arguments left */
105*9781SMoriah.Waterland@Sun.COM 	if ((argc-optind) > 0) {
106*9781SMoriah.Waterland@Sun.COM 		log_msg(LOG_MSG_ERR, MSG_USAGE);
107*9781SMoriah.Waterland@Sun.COM 		goto cleanup;
108*9781SMoriah.Waterland@Sun.COM 	}
109*9781SMoriah.Waterland@Sun.COM 
110*9781SMoriah.Waterland@Sun.COM 	/* figure out format */
111*9781SMoriah.Waterland@Sun.COM 	if (format_str == NULL) {
112*9781SMoriah.Waterland@Sun.COM 		format = KEYSTORE_FORMAT_TEXT;
113*9781SMoriah.Waterland@Sun.COM 	} else {
114*9781SMoriah.Waterland@Sun.COM 		if (ci_streq(format_str, "text")) {
115*9781SMoriah.Waterland@Sun.COM 			format = KEYSTORE_FORMAT_TEXT;
116*9781SMoriah.Waterland@Sun.COM 		} else if (ci_streq(format_str, "pem")) {
117*9781SMoriah.Waterland@Sun.COM 			format = KEYSTORE_FORMAT_PEM;
118*9781SMoriah.Waterland@Sun.COM 		} else if (ci_streq(format_str, "der")) {
119*9781SMoriah.Waterland@Sun.COM 			format = KEYSTORE_FORMAT_DER;
120*9781SMoriah.Waterland@Sun.COM 		} else {
121*9781SMoriah.Waterland@Sun.COM 			log_msg(LOG_MSG_ERR, MSG_BAD_FORMAT, format_str);
122*9781SMoriah.Waterland@Sun.COM 			goto cleanup;
123*9781SMoriah.Waterland@Sun.COM 		}
124*9781SMoriah.Waterland@Sun.COM 	}
125*9781SMoriah.Waterland@Sun.COM 
126*9781SMoriah.Waterland@Sun.COM 	/* open output file */
127*9781SMoriah.Waterland@Sun.COM 	if (outfile_str == NULL) {
128*9781SMoriah.Waterland@Sun.COM 		outfile = stdout;
129*9781SMoriah.Waterland@Sun.COM 		outfile_str = "stdout";
130*9781SMoriah.Waterland@Sun.COM 	} else {
131*9781SMoriah.Waterland@Sun.COM 		if ((outfile = fopen(outfile_str, "w+")) == NULL) {
132*9781SMoriah.Waterland@Sun.COM 			log_msg(LOG_MSG_ERR, MSG_OPEN_WRITE, outfile_str);
133*9781SMoriah.Waterland@Sun.COM 			goto cleanup;
134*9781SMoriah.Waterland@Sun.COM 		}
135*9781SMoriah.Waterland@Sun.COM 	}
136*9781SMoriah.Waterland@Sun.COM 
137*9781SMoriah.Waterland@Sun.COM 	/* set up proper keystore */
138*9781SMoriah.Waterland@Sun.COM 	if (altroot != NULL) {
139*9781SMoriah.Waterland@Sun.COM 	    if (strlcpy(keystore_file, altroot, MAXPATHLEN) >= MAXPATHLEN) {
140*9781SMoriah.Waterland@Sun.COM 		log_msg(LOG_MSG_ERR, MSG_TOO_LONG, altroot);
141*9781SMoriah.Waterland@Sun.COM 		goto cleanup;
142*9781SMoriah.Waterland@Sun.COM 	    }
143*9781SMoriah.Waterland@Sun.COM 
144*9781SMoriah.Waterland@Sun.COM 	    if (strlcat(keystore_file, "/", MAXPATHLEN) >= MAXPATHLEN) {
145*9781SMoriah.Waterland@Sun.COM 		log_msg(LOG_MSG_ERR, MSG_TOO_LONG, altroot);
146*9781SMoriah.Waterland@Sun.COM 		goto cleanup;
147*9781SMoriah.Waterland@Sun.COM 	    }
148*9781SMoriah.Waterland@Sun.COM 	}
149*9781SMoriah.Waterland@Sun.COM 
150*9781SMoriah.Waterland@Sun.COM 	if (keystore_base == NULL) {
151*9781SMoriah.Waterland@Sun.COM 		if (geteuid() == 0 || altroot != NULL) {
152*9781SMoriah.Waterland@Sun.COM 				/*
153*9781SMoriah.Waterland@Sun.COM 				 * If we have an alternate
154*9781SMoriah.Waterland@Sun.COM 				 * root, then we have no choice but to use
155*9781SMoriah.Waterland@Sun.COM 				 * root's keystore on that alternate root,
156*9781SMoriah.Waterland@Sun.COM 				 * since there is no way to resolve a
157*9781SMoriah.Waterland@Sun.COM 				 * user's home dir given an alternate root
158*9781SMoriah.Waterland@Sun.COM 				 */
159*9781SMoriah.Waterland@Sun.COM 			if (strlcat(keystore_file, PKGSEC,
160*9781SMoriah.Waterland@Sun.COM 			    MAXPATHLEN) >= MAXPATHLEN) {
161*9781SMoriah.Waterland@Sun.COM 				log_msg(LOG_MSG_ERR, MSG_TOO_LONG,
162*9781SMoriah.Waterland@Sun.COM 				    keystore_file);
163*9781SMoriah.Waterland@Sun.COM 				goto cleanup;
164*9781SMoriah.Waterland@Sun.COM 			}
165*9781SMoriah.Waterland@Sun.COM 		} else {
166*9781SMoriah.Waterland@Sun.COM 			if ((homedir = getenv("HOME")) == NULL) {
167*9781SMoriah.Waterland@Sun.COM 				/*
168*9781SMoriah.Waterland@Sun.COM 				 * not superuser, but no home dir, so
169*9781SMoriah.Waterland@Sun.COM 				 * use superuser's keystore
170*9781SMoriah.Waterland@Sun.COM 				 */
171*9781SMoriah.Waterland@Sun.COM 				if (strlcat(keystore_file, PKGSEC,
172*9781SMoriah.Waterland@Sun.COM 				    MAXPATHLEN) >= MAXPATHLEN) {
173*9781SMoriah.Waterland@Sun.COM 					log_msg(LOG_MSG_ERR, MSG_TOO_LONG,
174*9781SMoriah.Waterland@Sun.COM 					    keystore_file);
175*9781SMoriah.Waterland@Sun.COM 					goto cleanup;
176*9781SMoriah.Waterland@Sun.COM 				}
177*9781SMoriah.Waterland@Sun.COM 			} else {
178*9781SMoriah.Waterland@Sun.COM 				if (strlcat(keystore_file, homedir,
179*9781SMoriah.Waterland@Sun.COM 				    MAXPATHLEN) >= MAXPATHLEN) {
180*9781SMoriah.Waterland@Sun.COM 					log_msg(LOG_MSG_ERR, MSG_TOO_LONG,
181*9781SMoriah.Waterland@Sun.COM 					    homedir);
182*9781SMoriah.Waterland@Sun.COM 					goto cleanup;
183*9781SMoriah.Waterland@Sun.COM 				}
184*9781SMoriah.Waterland@Sun.COM 				if (strlcat(keystore_file, "/.pkg/security",
185*9781SMoriah.Waterland@Sun.COM 				    MAXPATHLEN) >= MAXPATHLEN) {
186*9781SMoriah.Waterland@Sun.COM 					log_msg(LOG_MSG_ERR, MSG_TOO_LONG,
187*9781SMoriah.Waterland@Sun.COM 					    keystore_file);
188*9781SMoriah.Waterland@Sun.COM 					goto cleanup;
189*9781SMoriah.Waterland@Sun.COM 				}
190*9781SMoriah.Waterland@Sun.COM 			}
191*9781SMoriah.Waterland@Sun.COM 		}
192*9781SMoriah.Waterland@Sun.COM 	} else {
193*9781SMoriah.Waterland@Sun.COM 		if (strlcat(keystore_file, keystore_base,
194*9781SMoriah.Waterland@Sun.COM 		    MAXPATHLEN) >= MAXPATHLEN) {
195*9781SMoriah.Waterland@Sun.COM 		    log_msg(LOG_MSG_ERR, MSG_TOO_LONG,
196*9781SMoriah.Waterland@Sun.COM 			keystore_base);
197*9781SMoriah.Waterland@Sun.COM 		    goto cleanup;
198*9781SMoriah.Waterland@Sun.COM 		}
199*9781SMoriah.Waterland@Sun.COM 	}
200*9781SMoriah.Waterland@Sun.COM 	err = pkgerr_new();
201*9781SMoriah.Waterland@Sun.COM 
202*9781SMoriah.Waterland@Sun.COM 	/* now load the key store */
203*9781SMoriah.Waterland@Sun.COM 	log_msg(LOG_MSG_DEBUG, "Loading keystore <%s>", keystore_file);
204*9781SMoriah.Waterland@Sun.COM 
205*9781SMoriah.Waterland@Sun.COM 	set_passphrase_prompt(MSG_KEYSTORE_PASSPROMPT);
206*9781SMoriah.Waterland@Sun.COM 	set_passphrase_passarg(passarg);
207*9781SMoriah.Waterland@Sun.COM 	if (open_keystore(err, keystore_file, prog,
208*9781SMoriah.Waterland@Sun.COM 	    pkg_passphrase_cb, KEYSTORE_DFLT_FLAGS,
209*9781SMoriah.Waterland@Sun.COM 	    &keystore) != 0) {
210*9781SMoriah.Waterland@Sun.COM 		log_pkgerr(LOG_MSG_ERR, err);
211*9781SMoriah.Waterland@Sun.COM 		log_msg(LOG_MSG_ERR, MSG_PRINT, outfile_str);
212*9781SMoriah.Waterland@Sun.COM 		goto cleanup;
213*9781SMoriah.Waterland@Sun.COM 	}
214*9781SMoriah.Waterland@Sun.COM 
215*9781SMoriah.Waterland@Sun.COM 	/* list the certs */
216*9781SMoriah.Waterland@Sun.COM 	log_msg(LOG_MSG_DEBUG, "Listing certificates");
217*9781SMoriah.Waterland@Sun.COM 	if (print_certs(err, keystore, alias, format, outfile) != 0) {
218*9781SMoriah.Waterland@Sun.COM 		log_pkgerr(LOG_MSG_ERR, err);
219*9781SMoriah.Waterland@Sun.COM 	    log_msg(LOG_MSG_ERR, MSG_PRINT, outfile_str);
220*9781SMoriah.Waterland@Sun.COM 		goto cleanup;
221*9781SMoriah.Waterland@Sun.COM 	}
222*9781SMoriah.Waterland@Sun.COM 
223*9781SMoriah.Waterland@Sun.COM 	/* now close it out */
224*9781SMoriah.Waterland@Sun.COM 	log_msg(LOG_MSG_DEBUG, "Closing keystore");
225*9781SMoriah.Waterland@Sun.COM 	set_passphrase_prompt(MSG_KEYSTORE_PASSOUTPROMPT);
226*9781SMoriah.Waterland@Sun.COM 	set_passphrase_passarg(passarg);
227*9781SMoriah.Waterland@Sun.COM 	if (close_keystore(err, keystore, pkg_passphrase_cb) != 0) {
228*9781SMoriah.Waterland@Sun.COM 		log_pkgerr(LOG_MSG_ERR, err);
229*9781SMoriah.Waterland@Sun.COM 		log_msg(LOG_MSG_ERR, MSG_PRINT, outfile_str);
230*9781SMoriah.Waterland@Sun.COM 		goto cleanup;
231*9781SMoriah.Waterland@Sun.COM 	}
232*9781SMoriah.Waterland@Sun.COM 
233*9781SMoriah.Waterland@Sun.COM 	/* everything worked */
234*9781SMoriah.Waterland@Sun.COM 	ret = 0;
235*9781SMoriah.Waterland@Sun.COM 
236*9781SMoriah.Waterland@Sun.COM 	/* fallthrough intentional */
237*9781SMoriah.Waterland@Sun.COM cleanup:
238*9781SMoriah.Waterland@Sun.COM 	if (outfile != NULL)
239*9781SMoriah.Waterland@Sun.COM 		(void) fclose(outfile);
240*9781SMoriah.Waterland@Sun.COM 
241*9781SMoriah.Waterland@Sun.COM 	if (err != NULL)
242*9781SMoriah.Waterland@Sun.COM 		pkgerr_free(err);
243*9781SMoriah.Waterland@Sun.COM 
244*9781SMoriah.Waterland@Sun.COM 	return (ret);
245*9781SMoriah.Waterland@Sun.COM }
246