1*431deaa0Shylee /*
2*431deaa0Shylee  * CDDL HEADER START
3*431deaa0Shylee  *
4*431deaa0Shylee  * The contents of this file are subject to the terms of the
5*431deaa0Shylee  * Common Development and Distribution License (the "License").
6*431deaa0Shylee  * You may not use this file except in compliance with the License.
7*431deaa0Shylee  *
8*431deaa0Shylee  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*431deaa0Shylee  * or http://www.opensolaris.org/os/licensing.
10*431deaa0Shylee  * See the License for the specific language governing permissions
11*431deaa0Shylee  * and limitations under the License.
12*431deaa0Shylee  *
13*431deaa0Shylee  * When distributing Covered Code, include this CDDL HEADER in each
14*431deaa0Shylee  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*431deaa0Shylee  * If applicable, add the following below this CDDL HEADER, with the
16*431deaa0Shylee  * fields enclosed by brackets "[]" replaced with your own identifying
17*431deaa0Shylee  * information: Portions Copyright [yyyy] [name of copyright owner]
18*431deaa0Shylee  *
19*431deaa0Shylee  * CDDL HEADER END
20*431deaa0Shylee  *
21*431deaa0Shylee  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
22*431deaa0Shylee  * Use is subject to license terms.
23*431deaa0Shylee  */
24*431deaa0Shylee 
25*431deaa0Shylee #include <stdio.h>
26*431deaa0Shylee #include <strings.h>
27*431deaa0Shylee #include <ctype.h>
28*431deaa0Shylee #include <libgen.h>
29*431deaa0Shylee #include <libintl.h>
30*431deaa0Shylee #include <errno.h>
31*431deaa0Shylee #include <kmfapiP.h>
32*431deaa0Shylee #include <sys/stat.h>
33*431deaa0Shylee #include <sys/param.h>
34*431deaa0Shylee #include <cryptoutil.h>
35*431deaa0Shylee #include "util.h"
36*431deaa0Shylee 
37*431deaa0Shylee static int err; /* To store errno which may be overwritten by gettext() */
38*431deaa0Shylee 
39*431deaa0Shylee int
kc_install(int argc,char * argv[])40*431deaa0Shylee kc_install(int argc, char *argv[])
41*431deaa0Shylee {
42*431deaa0Shylee 	int 		rv = KC_OK;
43*431deaa0Shylee 	int		opt;
44*431deaa0Shylee 	extern int	optind_av;
45*431deaa0Shylee 	extern char	*optarg_av;
46*431deaa0Shylee 	char 		*keystore_name = NULL;
47*431deaa0Shylee 	char 		*modulepath = NULL;
48*431deaa0Shylee 	char		*option_str = NULL;
49*431deaa0Shylee 	conf_entry_t	*entry = NULL;
50*431deaa0Shylee 	char		realpath[MAXPATHLEN];
51*431deaa0Shylee 	struct stat 	statbuf;
52*431deaa0Shylee 	FILE		*pfile = NULL;
53*431deaa0Shylee 	FILE		*pfile_tmp = NULL;
54*431deaa0Shylee 	char		tmpfile_name[MAXPATHLEN];
55*431deaa0Shylee 	int		found_count = 0;
56*431deaa0Shylee 	char		buffer[BUFSIZ];
57*431deaa0Shylee 	char		*ptr;
58*431deaa0Shylee 	boolean_t 	found;
59*431deaa0Shylee 
60*431deaa0Shylee 	while ((opt = getopt_av(argc, argv, "k:(keystore)m:(modulepath)"
61*431deaa0Shylee 	    "o:(option)")) != EOF) {
62*431deaa0Shylee 		switch (opt) {
63*431deaa0Shylee 		case 'k':
64*431deaa0Shylee 			if (keystore_name != NULL)
65*431deaa0Shylee 				rv = KC_ERR_USAGE;
66*431deaa0Shylee 			else {
67*431deaa0Shylee 				keystore_name = get_string(optarg_av, &rv);
68*431deaa0Shylee 				if (keystore_name == NULL) {
69*431deaa0Shylee 					(void) fprintf(stderr, gettext(
70*431deaa0Shylee 					    "Error keystore input.\n"));
71*431deaa0Shylee 				}
72*431deaa0Shylee 			}
73*431deaa0Shylee 			break;
74*431deaa0Shylee 		case 'm':
75*431deaa0Shylee 			if (modulepath != NULL)
76*431deaa0Shylee 				rv = KC_ERR_USAGE;
77*431deaa0Shylee 			else {
78*431deaa0Shylee 				modulepath = get_string(optarg_av, &rv);
79*431deaa0Shylee 				if (modulepath == NULL) {
80*431deaa0Shylee 					(void) fprintf(stderr,
81*431deaa0Shylee 					    gettext("Error modulepath.\n"));
82*431deaa0Shylee 				}
83*431deaa0Shylee 			}
84*431deaa0Shylee 			break;
85*431deaa0Shylee 		case 'o':
86*431deaa0Shylee 			if (option_str != NULL) {
87*431deaa0Shylee 				rv = KC_ERR_USAGE;
88*431deaa0Shylee 			} else {
89*431deaa0Shylee 				option_str = get_string(optarg_av, &rv);
90*431deaa0Shylee 				if (option_str == NULL) {
91*431deaa0Shylee 					(void) fprintf(stderr,
92*431deaa0Shylee 					    gettext("Error option input.\n"));
93*431deaa0Shylee 				}
94*431deaa0Shylee 			}
95*431deaa0Shylee 			break;
96*431deaa0Shylee 		default:
97*431deaa0Shylee 			(void) fprintf(stderr,
98*431deaa0Shylee 			    gettext("Error input option.\n"));
99*431deaa0Shylee 			rv = KC_ERR_USAGE;
100*431deaa0Shylee 			break;
101*431deaa0Shylee 		}
102*431deaa0Shylee 		if (rv != KC_OK)
103*431deaa0Shylee 			goto out;
104*431deaa0Shylee 	}
105*431deaa0Shylee 
106*431deaa0Shylee 	/* No additional args allowed. */
107*431deaa0Shylee 	argc -= optind_av;
108*431deaa0Shylee 	if (argc) {
109*431deaa0Shylee 		(void) fprintf(stderr,
110*431deaa0Shylee 		    gettext("Error input option\n"));
111*431deaa0Shylee 		rv = KC_ERR_USAGE;
112*431deaa0Shylee 		goto out;
113*431deaa0Shylee 	}
114*431deaa0Shylee 
115*431deaa0Shylee 	if (keystore_name == NULL || modulepath == NULL) {
116*431deaa0Shylee 		(void) fprintf(stderr, gettext("Error input option\n"));
117*431deaa0Shylee 		rv = KC_ERR_USAGE;
118*431deaa0Shylee 		goto out;
119*431deaa0Shylee 	}
120*431deaa0Shylee 
121*431deaa0Shylee 	if (strcasecmp(keystore_name, "nss") == 0 ||
122*431deaa0Shylee 	    strcasecmp(keystore_name, "pkcs11") == 0 ||
123*431deaa0Shylee 	    strcasecmp(keystore_name, "file") == 0) {
124*431deaa0Shylee 		(void) fprintf(stderr,
125*431deaa0Shylee 		    gettext("Can not use the built-in keystore name %s\n"),
126*431deaa0Shylee 		    keystore_name);
127*431deaa0Shylee 		rv = KC_ERR_USAGE;
128*431deaa0Shylee 		goto out;
129*431deaa0Shylee 	}
130*431deaa0Shylee 
131*431deaa0Shylee 	entry = get_keystore_entry(keystore_name);
132*431deaa0Shylee 	if (entry != NULL) {
133*431deaa0Shylee 		(void) fprintf(stderr, gettext("%s exists already.\n"),
134*431deaa0Shylee 		    keystore_name);
135*431deaa0Shylee 		rv = KC_ERR_USAGE;
136*431deaa0Shylee 		goto out;
137*431deaa0Shylee 	}
138*431deaa0Shylee 
139*431deaa0Shylee 	/*
140*431deaa0Shylee 	 * Find the absolute path of the module and check if it exists in
141*431deaa0Shylee 	 * the system.  If $ISA is in the path, will check the 32bit version
142*431deaa0Shylee 	 * only.
143*431deaa0Shylee 	 */
144*431deaa0Shylee 	if (strncmp(modulepath, "/", 1) != 0) {
145*431deaa0Shylee 		/*
146*431deaa0Shylee 		 * Only contain the base name; prepand it with
147*431deaa0Shylee 		 * KMF_PLUGIN_PATH
148*431deaa0Shylee 		 */
149*431deaa0Shylee 		(void) snprintf(realpath, MAXPATHLEN, "%s%s",
150*431deaa0Shylee 		    KMF_PLUGIN_PATH, modulepath);
151*431deaa0Shylee 	} else {
152*431deaa0Shylee 		char *buf = modulepath;
153*431deaa0Shylee 		char *isa;
154*431deaa0Shylee 
155*431deaa0Shylee 		if ((isa = strstr(buf, PKCS11_ISA)) != NULL) {
156*431deaa0Shylee 			(void) strncpy(realpath, buf, isa - buf);
157*431deaa0Shylee 			isa += strlen(PKCS11_ISA) - 1;
158*431deaa0Shylee 			(void) strlcat(realpath, isa, MAXPATHLEN);
159*431deaa0Shylee 		} else {
160*431deaa0Shylee 			(void) strlcpy(realpath, modulepath, MAXPATHLEN);
161*431deaa0Shylee 		}
162*431deaa0Shylee 	}
163*431deaa0Shylee 
164*431deaa0Shylee 	if (stat(realpath, &statbuf) != 0) {
165*431deaa0Shylee 		(void) fprintf(stderr, gettext("%s not found.\n"),
166*431deaa0Shylee 		    realpath);
167*431deaa0Shylee 		rv = KC_ERR_ACCESS;
168*431deaa0Shylee 		goto out;
169*431deaa0Shylee 	}
170*431deaa0Shylee 
171*431deaa0Shylee 	if ((pfile = fopen(_PATH_KMF_CONF, "r+")) == NULL) {
172*431deaa0Shylee 		err = errno;
173*431deaa0Shylee 		(void) fprintf(stderr,
174*431deaa0Shylee 		    gettext("failed to update the configuration - %s\n"),
175*431deaa0Shylee 		    strerror(err));
176*431deaa0Shylee 		rv = KC_ERR_ACCESS;
177*431deaa0Shylee 		goto out;
178*431deaa0Shylee 	}
179*431deaa0Shylee 
180*431deaa0Shylee 	if (lockf(fileno(pfile), F_TLOCK, 0) == -1) {
181*431deaa0Shylee 		err = errno;
182*431deaa0Shylee 		(void) fprintf(stderr,
183*431deaa0Shylee 		    gettext("failed to lock the configuration - %s\n"),
184*431deaa0Shylee 		    strerror(err));
185*431deaa0Shylee 		rv = KC_ERR_INSTALL;
186*431deaa0Shylee 		goto out;
187*431deaa0Shylee 	}
188*431deaa0Shylee 
189*431deaa0Shylee 	/*
190*431deaa0Shylee 	 * Create a temporary file in the /etc/crypto directory.
191*431deaa0Shylee 	 */
192*431deaa0Shylee 	(void) strlcpy(tmpfile_name, CONF_TEMPFILE, sizeof (tmpfile_name));
193*431deaa0Shylee 	if (mkstemp(tmpfile_name) == -1) {
194*431deaa0Shylee 		err = errno;
195*431deaa0Shylee 		(void) fprintf(stderr,
196*431deaa0Shylee 		    gettext("failed to create a temporary file - %s\n"),
197*431deaa0Shylee 		    strerror(err));
198*431deaa0Shylee 		rv = KC_ERR_INSTALL;
199*431deaa0Shylee 		goto out;
200*431deaa0Shylee 	}
201*431deaa0Shylee 
202*431deaa0Shylee 	if ((pfile_tmp = fopen(tmpfile_name, "w")) == NULL) {
203*431deaa0Shylee 		err = errno;
204*431deaa0Shylee 		(void) fprintf(stderr,
205*431deaa0Shylee 		    gettext("failed to open %s - %s\n"),
206*431deaa0Shylee 		    tmpfile_name, strerror(err));
207*431deaa0Shylee 		rv = KC_ERR_INSTALL;
208*431deaa0Shylee 		goto out;
209*431deaa0Shylee 	}
210*431deaa0Shylee 
211*431deaa0Shylee 	/*
212*431deaa0Shylee 	 * Loop thru the config file. If the file was reserved within a
213*431deaa0Shylee 	 * package bracket, just uncomment it.  Other wise, append it at
214*431deaa0Shylee 	 * the end.  The resulting file will be saved in the temp file first.
215*431deaa0Shylee 	 */
216*431deaa0Shylee 	while (fgets(buffer, BUFSIZ, pfile) != NULL) {
217*431deaa0Shylee 		found = B_FALSE;
218*431deaa0Shylee 		if (buffer[0] == '#') {
219*431deaa0Shylee 			ptr = buffer;
220*431deaa0Shylee 			ptr++;
221*431deaa0Shylee 			while (*ptr == '#' || *ptr == ' ')
222*431deaa0Shylee 				ptr++;
223*431deaa0Shylee 			if (strncmp(keystore_name, ptr, strlen(keystore_name))
224*431deaa0Shylee 			    == 0) {
225*431deaa0Shylee 				found = B_TRUE;
226*431deaa0Shylee 				found_count++;
227*431deaa0Shylee 			}
228*431deaa0Shylee 		}
229*431deaa0Shylee 
230*431deaa0Shylee 		if (found == B_FALSE) {
231*431deaa0Shylee 			if (fputs(buffer, pfile_tmp) == EOF) {
232*431deaa0Shylee 				rv = KC_ERR_INSTALL;
233*431deaa0Shylee 				goto out;
234*431deaa0Shylee 			}
235*431deaa0Shylee 		} else {
236*431deaa0Shylee 			if (found_count == 1) {
237*431deaa0Shylee 				if (fputs(ptr, pfile_tmp) == EOF) {
238*431deaa0Shylee 					rv = KC_ERR_INSTALL;
239*431deaa0Shylee 					goto out;
240*431deaa0Shylee 				}
241*431deaa0Shylee 			} else {
242*431deaa0Shylee 				/*
243*431deaa0Shylee 				 * Found a second entry with #keystore_name.
244*431deaa0Shylee 				 * This should not happen. The kmf.conf file
245*431deaa0Shylee 				 * is corrupted. Give a warning and skip
246*431deaa0Shylee 				 * this entry.
247*431deaa0Shylee 				 */
248*431deaa0Shylee 				(void) fprintf(stderr, gettext(
249*431deaa0Shylee 				    "(Warning) Found an additional reserved "
250*431deaa0Shylee 				    "entry for %s.\n"), keystore_name);
251*431deaa0Shylee 			}
252*431deaa0Shylee 		}
253*431deaa0Shylee 	}
254*431deaa0Shylee 
255*431deaa0Shylee 	if (found_count == 0) {
256*431deaa0Shylee 		char buf[MAXPATHLEN];
257*431deaa0Shylee 		/*
258*431deaa0Shylee 		 * This entry was not in package before, append it to the
259*431deaa0Shylee 		 * end of the temp file.
260*431deaa0Shylee 		 */
261*431deaa0Shylee 		if (option_str == NULL)
262*431deaa0Shylee 			(void) snprintf(buf, MAXPATHLEN, "%s:%s%s\n",
263*431deaa0Shylee 			    keystore_name, CONF_MODULEPATH, modulepath);
264*431deaa0Shylee 		else
265*431deaa0Shylee 			(void) snprintf(buf, MAXPATHLEN, "%s:%s%s;%s%s\n",
266*431deaa0Shylee 			    keystore_name, CONF_MODULEPATH, modulepath,
267*431deaa0Shylee 			    CONF_OPTION, option_str);
268*431deaa0Shylee 
269*431deaa0Shylee 		if (fputs(buf, pfile_tmp) == EOF) {
270*431deaa0Shylee 			err = errno;
271*431deaa0Shylee 			(void) fprintf(stderr, gettext(
272*431deaa0Shylee 			    "failed to write to %s: %s\n"), tmpfile_name,
273*431deaa0Shylee 			    strerror(err));
274*431deaa0Shylee 			rv = KC_ERR_INSTALL;
275*431deaa0Shylee 			goto out;
276*431deaa0Shylee 		}
277*431deaa0Shylee 	}
278*431deaa0Shylee 
279*431deaa0Shylee out:
280*431deaa0Shylee 	if (pfile != NULL)
281*431deaa0Shylee 		(void) fclose(pfile);
282*431deaa0Shylee 
283*431deaa0Shylee 	if (rv != KC_OK && pfile_tmp != NULL)
284*431deaa0Shylee 		(void) unlink(tmpfile_name);
285*431deaa0Shylee 
286*431deaa0Shylee 	if (pfile_tmp != NULL)
287*431deaa0Shylee 		(void) fclose(pfile_tmp);
288*431deaa0Shylee 
289*431deaa0Shylee 	if (rv == KC_OK) {
290*431deaa0Shylee 		if (rename(tmpfile_name, _PATH_KMF_CONF) == -1) {
291*431deaa0Shylee 			err = errno;
292*431deaa0Shylee 			(void) fprintf(stderr, gettext(
293*431deaa0Shylee 			    "failed to update the configuration - %s"),
294*431deaa0Shylee 			    strerror(err));
295*431deaa0Shylee 			return (KC_ERR_INSTALL);
296*431deaa0Shylee 		}
297*431deaa0Shylee 
298*431deaa0Shylee 		if (chmod(_PATH_KMF_CONF,
299*431deaa0Shylee 		    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
300*431deaa0Shylee 			err = errno;
301*431deaa0Shylee 			(void) fprintf(stderr, gettext(
302*431deaa0Shylee 			    "failed to update the configuration - %s\n"),
303*431deaa0Shylee 			    strerror(err));
304*431deaa0Shylee 			return (KC_ERR_INSTALL);
305*431deaa0Shylee 		}
306*431deaa0Shylee 	}
307*431deaa0Shylee 
308*431deaa0Shylee 	return (rv);
309*431deaa0Shylee }
310