4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
22 * Use is subject to license terms.
25 #pragma ident "%Z%%M% %I% %E% SMI"
35 #include <sys/param.h>
36 #include <cryptoutil.h>
39 static int err
; /* To store errno which may be overwritten by gettext() */
42 kc_install(int argc
, char *argv
[])
47 extern char *optarg_av
;
48 char *keystore_name
= NULL
;
49 char *modulepath
= NULL
;
50 char *option_str
= NULL
;
51 conf_entry_t
*entry
= NULL
;
52 char realpath
[MAXPATHLEN
];
55 FILE *pfile_tmp
= NULL
;
56 char tmpfile_name
[MAXPATHLEN
];
62 while ((opt
= getopt_av(argc
, argv
, "k:(keystore)m:(modulepath)"
63 "o:(option)")) != EOF
) {
66 if (keystore_name
!= NULL
)
69 keystore_name
= get_string(optarg_av
, &rv
);
70 if (keystore_name
== NULL
) {
71 (void) fprintf(stderr
, gettext(
72 "Error keystore input.\n"));
77 if (modulepath
!= NULL
)
80 modulepath
= get_string(optarg_av
, &rv
);
81 if (modulepath
== NULL
) {
82 (void) fprintf(stderr
,
83 gettext("Error modulepath.\n"));
88 if (option_str
!= NULL
) {
91 option_str
= get_string(optarg_av
, &rv
);
92 if (option_str
== NULL
) {
93 (void) fprintf(stderr
,
94 gettext("Error option input.\n"));
99 (void) fprintf(stderr
,
100 gettext("Error input option.\n"));
108 /* No additional args allowed. */
111 (void) fprintf(stderr
,
112 gettext("Error input option\n"));
117 if (keystore_name
== NULL
|| modulepath
== NULL
) {
118 (void) fprintf(stderr
, gettext("Error input option\n"));
123 if (strcasecmp(keystore_name
, "nss") == 0 ||
124 strcasecmp(keystore_name
, "pkcs11") == 0 ||
125 strcasecmp(keystore_name
, "file") == 0) {
126 (void) fprintf(stderr
,
127 gettext("Can not use the built-in keystore name %s\n"),
133 entry
= get_keystore_entry(keystore_name
);
135 (void) fprintf(stderr
, gettext("%s exists already.\n"),
142 * Find the absolute path of the module and check if it exists in
143 * the system. If $ISA is in the path, will check the 32bit version
146 if (strncmp(modulepath
, "/", 1) != 0) {
148 * Only contain the base name; prepand it with
151 (void) snprintf(realpath
, MAXPATHLEN
, "%s%s",
152 KMF_PLUGIN_PATH
, modulepath
);
154 char *buf
= modulepath
;
157 if ((isa
= strstr(buf
, PKCS11_ISA
)) != NULL
) {
158 (void) strncpy(realpath
, buf
, isa
- buf
);
159 isa
+= strlen(PKCS11_ISA
) - 1;
160 (void) strlcat(realpath
, isa
, MAXPATHLEN
);
162 (void) strlcpy(realpath
, modulepath
, MAXPATHLEN
);
166 if (stat(realpath
, &statbuf
) != 0) {
167 (void) fprintf(stderr
, gettext("%s not found.\n"),
173 if ((pfile
= fopen(_PATH_KMF_CONF
, "r+")) == NULL
) {
175 (void) fprintf(stderr
,
176 gettext("failed to update the configuration - %s\n"),
182 if (lockf(fileno(pfile
), F_TLOCK
, 0) == -1) {
184 (void) fprintf(stderr
,
185 gettext("failed to lock the configuration - %s\n"),
192 * Create a temporary file in the /etc/crypto directory.
194 (void) strlcpy(tmpfile_name
, CONF_TEMPFILE
, sizeof (tmpfile_name
));
195 if (mkstemp(tmpfile_name
) == -1) {
197 (void) fprintf(stderr
,
198 gettext("failed to create a temporary file - %s\n"),
204 if ((pfile_tmp
= fopen(tmpfile_name
, "w")) == NULL
) {
206 (void) fprintf(stderr
,
207 gettext("failed to open %s - %s\n"),
208 tmpfile_name
, strerror(err
));
214 * Loop thru the config file. If the file was reserved within a
215 * package bracket, just uncomment it. Other wise, append it at
216 * the end. The resulting file will be saved in the temp file first.
218 while (fgets(buffer
, BUFSIZ
, pfile
) != NULL
) {
220 if (buffer
[0] == '#') {
223 while (*ptr
== '#' || *ptr
== ' ')
225 if (strncmp(keystore_name
, ptr
, strlen(keystore_name
))
232 if (found
== B_FALSE
) {
233 if (fputs(buffer
, pfile_tmp
) == EOF
) {
238 if (found_count
== 1) {
239 if (fputs(ptr
, pfile_tmp
) == EOF
) {
245 * Found a second entry with #keystore_name.
246 * This should not happen. The kmf.conf file
247 * is corrupted. Give a warning and skip
250 (void) fprintf(stderr
, gettext(
251 "(Warning) Found an additional reserved "
252 "entry for %s.\n"), keystore_name
);
257 if (found_count
== 0) {
258 char buf
[MAXPATHLEN
];
260 * This entry was not in package before, append it to the
261 * end of the temp file.
263 if (option_str
== NULL
)
264 (void) snprintf(buf
, MAXPATHLEN
, "%s:%s%s\n",
265 keystore_name
, CONF_MODULEPATH
, modulepath
);
267 (void) snprintf(buf
, MAXPATHLEN
, "%s:%s%s;%s%s\n",
268 keystore_name
, CONF_MODULEPATH
, modulepath
,
269 CONF_OPTION
, option_str
);
271 if (fputs(buf
, pfile_tmp
) == EOF
) {
273 (void) fprintf(stderr
, gettext(
274 "failed to write to %s: %s\n"), tmpfile_name
,
283 (void) fclose(pfile
);
285 if (rv
!= KC_OK
&& pfile_tmp
!= NULL
)
286 (void) unlink(tmpfile_name
);
288 if (pfile_tmp
!= NULL
)
289 (void) fclose(pfile_tmp
);
292 if (rename(tmpfile_name
, _PATH_KMF_CONF
) == -1) {
294 (void) fprintf(stderr
, gettext(
295 "failed to update the configuration - %s"),
297 return (KC_ERR_INSTALL
);
300 if (chmod(_PATH_KMF_CONF
,
301 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) == -1) {
303 (void) fprintf(stderr
, gettext(
304 "failed to update the configuration - %s\n"),
306 return (KC_ERR_INSTALL
);