xref: /onnv-gate/usr/src/cmd/cmd-crypto/kmfcfg/uninstall.c (revision 5626:1f8878c75f54)
1*5626Shylee /*
2*5626Shylee  * CDDL HEADER START
3*5626Shylee  *
4*5626Shylee  * The contents of this file are subject to the terms of the
5*5626Shylee  * Common Development and Distribution License (the "License").
6*5626Shylee  * You may not use this file except in compliance with the License.
7*5626Shylee  *
8*5626Shylee  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5626Shylee  * or http://www.opensolaris.org/os/licensing.
10*5626Shylee  * See the License for the specific language governing permissions
11*5626Shylee  * and limitations under the License.
12*5626Shylee  *
13*5626Shylee  * When distributing Covered Code, include this CDDL HEADER in each
14*5626Shylee  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5626Shylee  * If applicable, add the following below this CDDL HEADER, with the
16*5626Shylee  * fields enclosed by brackets "[]" replaced with your own identifying
17*5626Shylee  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5626Shylee  *
19*5626Shylee  * CDDL HEADER END
20*5626Shylee  *
21*5626Shylee  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
22*5626Shylee  * Use is subject to license terms.
23*5626Shylee  */
24*5626Shylee 
25*5626Shylee #pragma ident	"%Z%%M%	%I%	%E% SMI"
26*5626Shylee 
27*5626Shylee #include <stdio.h>
28*5626Shylee #include <strings.h>
29*5626Shylee #include <ctype.h>
30*5626Shylee #include <libgen.h>
31*5626Shylee #include <libintl.h>
32*5626Shylee #include <errno.h>
33*5626Shylee #include <kmfapiP.h>
34*5626Shylee #include <sys/stat.h>
35*5626Shylee #include <sys/param.h>
36*5626Shylee #include <cryptoutil.h>
37*5626Shylee #include "util.h"
38*5626Shylee 
39*5626Shylee static int err; /* To store errno which may be overwritten by gettext() */
40*5626Shylee 
41*5626Shylee int
kc_uninstall(int argc,char * argv[])42*5626Shylee kc_uninstall(int argc, char *argv[])
43*5626Shylee {
44*5626Shylee 	int 		rv = KC_OK;
45*5626Shylee 	int		opt;
46*5626Shylee 	extern int	optind_av;
47*5626Shylee 	extern char	*optarg_av;
48*5626Shylee 	char 		*keystore_name = NULL;
49*5626Shylee 	conf_entry_t	*entry = NULL;
50*5626Shylee 	FILE		*pfile = NULL;
51*5626Shylee 	FILE		*pfile_tmp = NULL;
52*5626Shylee 	char		tmpfile_name[MAXPATHLEN];
53*5626Shylee 	char		buffer[MAXPATHLEN];
54*5626Shylee 	char		buffer2[MAXPATHLEN];
55*5626Shylee 	boolean_t 	found;
56*5626Shylee 	boolean_t	in_package;
57*5626Shylee 
58*5626Shylee 	while ((opt = getopt_av(argc, argv, "k:(keystore)")) != EOF) {
59*5626Shylee 		switch (opt) {
60*5626Shylee 		case 'k':
61*5626Shylee 			if (keystore_name != NULL)
62*5626Shylee 				rv = KC_ERR_USAGE;
63*5626Shylee 			else {
64*5626Shylee 				keystore_name = get_string(optarg_av, &rv);
65*5626Shylee 				if (keystore_name == NULL) {
66*5626Shylee 					(void) fprintf(stderr, gettext(
67*5626Shylee 					    "Error keystore input.\n"));
68*5626Shylee 				}
69*5626Shylee 			}
70*5626Shylee 			break;
71*5626Shylee 		default:
72*5626Shylee 			(void) fprintf(stderr,
73*5626Shylee 			    gettext("Error input option.\n"));
74*5626Shylee 			rv = KC_ERR_USAGE;
75*5626Shylee 			break;
76*5626Shylee 		}
77*5626Shylee 		if (rv != KC_OK)
78*5626Shylee 			goto out;
79*5626Shylee 	}
80*5626Shylee 
81*5626Shylee 	/* No additional args allowed. */
82*5626Shylee 	argc -= optind_av;
83*5626Shylee 	if (argc) {
84*5626Shylee 		(void) fprintf(stderr,
85*5626Shylee 		    gettext("Error input option\n"));
86*5626Shylee 		rv = KC_ERR_USAGE;
87*5626Shylee 		goto out;
88*5626Shylee 	}
89*5626Shylee 
90*5626Shylee 	if (keystore_name == NULL) {
91*5626Shylee 		(void) fprintf(stderr,
92*5626Shylee 		    gettext("Error input option\n"));
93*5626Shylee 		rv = KC_ERR_USAGE;
94*5626Shylee 		goto out;
95*5626Shylee 	}
96*5626Shylee 
97*5626Shylee 	if (strcasecmp(keystore_name, "nss") == 0 ||
98*5626Shylee 	    strcasecmp(keystore_name, "pkcs11") == 0 ||
99*5626Shylee 	    strcasecmp(keystore_name, "file") == 0) {
100*5626Shylee 		(void) fprintf(stderr,
101*5626Shylee 		    gettext("Can not uninstall the built-in keystore %s\n"),
102*5626Shylee 		    keystore_name);
103*5626Shylee 		rv = KC_ERR_UNINSTALL;
104*5626Shylee 		goto out;
105*5626Shylee 	}
106*5626Shylee 
107*5626Shylee 	entry = get_keystore_entry(keystore_name);
108*5626Shylee 	if (entry == NULL) {
109*5626Shylee 		(void) fprintf(stderr, gettext("%s does not exist.\n"),
110*5626Shylee 		    keystore_name);
111*5626Shylee 		rv = KC_ERR_USAGE;
112*5626Shylee 		goto out;
113*5626Shylee 	}
114*5626Shylee 
115*5626Shylee 	if ((pfile = fopen(_PATH_KMF_CONF, "r+")) == NULL) {
116*5626Shylee 		err = errno;
117*5626Shylee 		(void) fprintf(stderr,
118*5626Shylee 		    gettext("failed to update the configuration - %s\n"),
119*5626Shylee 		    strerror(err));
120*5626Shylee 		rv = KC_ERR_ACCESS;
121*5626Shylee 		goto out;
122*5626Shylee 	}
123*5626Shylee 
124*5626Shylee 	if (lockf(fileno(pfile), F_TLOCK, 0) == -1) {
125*5626Shylee 		err = errno;
126*5626Shylee 		(void) fprintf(stderr,
127*5626Shylee 		    gettext("failed to lock the configuration - %s\n"),
128*5626Shylee 		    strerror(err));
129*5626Shylee 		rv = KC_ERR_UNINSTALL;
130*5626Shylee 		goto out;
131*5626Shylee 	}
132*5626Shylee 
133*5626Shylee 	/*
134*5626Shylee 	 * Create a temporary file in the /etc/crypto directory.
135*5626Shylee 	 */
136*5626Shylee 	(void) strlcpy(tmpfile_name, CONF_TEMPFILE, sizeof (tmpfile_name));
137*5626Shylee 	if (mkstemp(tmpfile_name) == -1) {
138*5626Shylee 		err = errno;
139*5626Shylee 		(void) fprintf(stderr,
140*5626Shylee 		    gettext("failed to create a temporary file - %s\n"),
141*5626Shylee 		    strerror(err));
142*5626Shylee 		rv = KC_ERR_UNINSTALL;
143*5626Shylee 		goto out;
144*5626Shylee 	}
145*5626Shylee 
146*5626Shylee 	if ((pfile_tmp = fopen(tmpfile_name, "w")) == NULL) {
147*5626Shylee 		err = errno;
148*5626Shylee 		(void) fprintf(stderr,
149*5626Shylee 		    gettext("failed to open a temporary file - %s\n"),
150*5626Shylee 		    strerror(err));
151*5626Shylee 		rv = KC_ERR_UNINSTALL;
152*5626Shylee 		goto out;
153*5626Shylee 	}
154*5626Shylee 
155*5626Shylee 	/*
156*5626Shylee 	 * Loop thru the config file. If the plugin to be uninstalled is in
157*5626Shylee 	 * a package, then just comment it off.
158*5626Shylee 	 */
159*5626Shylee 	in_package = B_FALSE;
160*5626Shylee 	while (fgets(buffer, MAXPATHLEN, pfile) != NULL) {
161*5626Shylee 		found = B_FALSE;
162*5626Shylee 		if (buffer[0] != ' ' && buffer[0] != '\n' &&
163*5626Shylee 		    buffer[0] != '\t') {
164*5626Shylee 			if (strstr(buffer, " Start ") != NULL) {
165*5626Shylee 				in_package = B_TRUE;
166*5626Shylee 			} else if (strstr(buffer, " End ") != NULL) {
167*5626Shylee 				in_package = B_FALSE;
168*5626Shylee 			} else if (buffer[0] != '#') {
169*5626Shylee 				char *name;
170*5626Shylee 				int len;
171*5626Shylee 
172*5626Shylee 				/*
173*5626Shylee 				 * make a copy of the original buffer to
174*5626Shylee 				 * buffer2.  Also get rid of the trailing
175*5626Shylee 				 * '\n' from buffer2.
176*5626Shylee 				 */
177*5626Shylee 				(void) strlcpy(buffer2, buffer, MAXPATHLEN);
178*5626Shylee 				/* get rid of trailing '\n' */
179*5626Shylee 				len = strlen(buffer2);
180*5626Shylee 				if (buffer2[len-1] == '\n') {
181*5626Shylee 					len--;
182*5626Shylee 				}
183*5626Shylee 				buffer2[len] = '\0';
184*5626Shylee 
185*5626Shylee 				if ((name = strtok(buffer2, SEP_COLON)) ==
186*5626Shylee 				    NULL) {
187*5626Shylee 					rv = KC_ERR_UNINSTALL;
188*5626Shylee 					goto out;
189*5626Shylee 				}
190*5626Shylee 
191*5626Shylee 				if (strcmp(keystore_name, name) == 0)
192*5626Shylee 					found = B_TRUE;
193*5626Shylee 			}
194*5626Shylee 		}
195*5626Shylee 
196*5626Shylee 		if (found) {
197*5626Shylee 			/*
198*5626Shylee 			 * If found and not in_package, then don't write
199*5626Shylee 			 * this line to the result file.
200*5626Shylee 			 */
201*5626Shylee 			if (in_package) {
202*5626Shylee 				(void) snprintf(buffer2, sizeof (buffer2),
203*5626Shylee 				    "%s%s", "#", buffer);
204*5626Shylee 
205*5626Shylee 				if (fputs(buffer2, pfile_tmp) == EOF) {
206*5626Shylee 					rv = KC_ERR_UNINSTALL;
207*5626Shylee 					goto out;
208*5626Shylee 				}
209*5626Shylee 			}
210*5626Shylee 		} else {
211*5626Shylee 			if (fputs(buffer, pfile_tmp) == EOF) {
212*5626Shylee 				rv = KC_ERR_UNINSTALL;
213*5626Shylee 				goto out;
214*5626Shylee 			}
215*5626Shylee 		}
216*5626Shylee 	}
217*5626Shylee 
218*5626Shylee out:
219*5626Shylee 	if (pfile != NULL)
220*5626Shylee 		(void) fclose(pfile);
221*5626Shylee 
222*5626Shylee 	if (rv != KC_OK && pfile_tmp != NULL)
223*5626Shylee 		(void) unlink(tmpfile_name);
224*5626Shylee 
225*5626Shylee 	if (pfile_tmp != NULL)
226*5626Shylee 		(void) fclose(pfile_tmp);
227*5626Shylee 
228*5626Shylee 	if (rv == KC_OK) {
229*5626Shylee 		if (rename(tmpfile_name, _PATH_KMF_CONF) == -1) {
230*5626Shylee 			err = errno;
231*5626Shylee 			(void) fprintf(stderr, gettext(
232*5626Shylee 			    "failed to update the configuration - %s"),
233*5626Shylee 			    strerror(err));
234*5626Shylee 			return (KC_ERR_UNINSTALL);
235*5626Shylee 		}
236*5626Shylee 
237*5626Shylee 		if (chmod(_PATH_KMF_CONF,
238*5626Shylee 		    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
239*5626Shylee 			err = errno;
240*5626Shylee 			(void) fprintf(stderr, gettext(
241*5626Shylee 			    "failed to update the configuration - %s\n"),
242*5626Shylee 			    strerror(err));
243*5626Shylee 			return (KC_ERR_UNINSTALL);
244*5626Shylee 		}
245*5626Shylee 	}
246*5626Shylee 
247*5626Shylee 	return (rv);
248*5626Shylee }
249