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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
31 #include "cryptoadm.h"
32 #include <cryptoutil.h>
35 * Create one item of type mechlist_t with the mechanism name. A null is
36 * returned to indicate that the storage space available is insufficient.
39 create_mech(char *name
)
41 mechlist_t
*pres
= NULL
;
48 pres
= malloc(sizeof (mechlist_t
));
50 cryptodebug("out of memory.");
55 while (isspace(*first
)) /* nuke leading whitespace */
57 (void) strlcpy(pres
->name
, first
, sizeof (pres
->name
));
59 last
= strrchr(pres
->name
, '\0');
61 while (isspace(*last
)) /* nuke trailing whitespace */
72 free_mechlist(mechlist_t
*plist
)
76 while (plist
!= NULL
) {
86 * Check if the mechanism is in the mechanism list.
89 is_in_list(char *mechname
, mechlist_t
*plist
)
91 boolean_t found
= B_FALSE
;
93 if (mechname
== NULL
) {
97 while (plist
!= NULL
) {
98 if (strcmp(plist
->name
, mechname
) == 0) {
109 update_conf(char *conf_file
, char *entry
)
113 boolean_t fips_entry
= B_FALSE
;
116 char tmpfile_name
[MAXPATHLEN
];
120 char buffer2
[BUFSIZ
];
125 if ((pfile
= fopen(conf_file
, "r+")) == NULL
) {
127 cryptoerror(LOG_STDERR
,
128 gettext("failed to update the configuration - %s"),
130 cryptodebug("failed to open %s for write.", conf_file
);
134 if (lockf(fileno(pfile
), F_TLOCK
, 0) == -1) {
136 cryptoerror(LOG_STDERR
,
137 gettext("failed to lock the configuration - %s"),
139 (void) fclose(pfile
);
144 * Create a temporary file in the /etc/crypto directory.
146 (void) strlcpy(tmpfile_name
, TMPFILE_TEMPLATE
, sizeof (tmpfile_name
));
147 if (mkstemp(tmpfile_name
) == -1) {
149 cryptoerror(LOG_STDERR
,
150 gettext("failed to create a temporary file - %s"),
152 (void) fclose(pfile
);
156 if ((pfile_tmp
= fopen(tmpfile_name
, "w")) == NULL
) {
158 cryptoerror(LOG_STDERR
, gettext("failed to open %s - %s"),
159 tmpfile_name
, strerror(err
));
160 (void) fclose(pfile
);
166 * Loop thru the config file. If the provider was reserved within a
167 * package bracket, just uncomment it. Otherwise, append it at
168 * the end. The resulting file will be saved in the temp file first.
173 while (fgets(buffer
, BUFSIZ
, pfile
) != NULL
) {
175 if (strcmp(conf_file
, _PATH_PKCS11_CONF
) == 0) {
176 if (buffer
[0] == '#') {
179 if (strcmp(entry
, ptr
) == 0) {
184 (void) strlcpy(buffer2
, buffer
, BUFSIZ
);
186 if ((name
= strtok(ptr
, SEP_COLON
)) == NULL
) {
189 } else if (strcmp(FIPS_KEYWORD
, name
) == 0) {
195 } else { /* _PATH_KCF_CONF */
196 if (buffer
[0] == '#') {
197 (void) strlcpy(buffer2
, buffer
, BUFSIZ
);
200 if ((name
= strtok(ptr
, SEP_COLON
)) == NULL
) {
205 (void) strlcpy(buffer2
, buffer
, BUFSIZ
);
207 if ((name
= strtok(ptr
, SEP_COLON
)) == NULL
) {
214 if (found
== B_FALSE
) {
215 if (fputs(buffer
, pfile_tmp
) == EOF
) {
219 if (found_count
== 1) {
220 if (strcmp(conf_file
, _PATH_PKCS11_CONF
) == 0) {
221 if (fips_entry
== B_TRUE
) {
222 if (fputs(entry
, pfile_tmp
) ==
226 fips_entry
= B_FALSE
;
228 if (fputs(ptr
, pfile_tmp
) ==
234 if (fputs(entry
, pfile_tmp
) == EOF
) {
240 * Found a second entry with same tag name.
241 * Should not happen. The config file
242 * is corrupted. Give a warning and skip
245 cryptoerror(LOG_STDERR
, gettext(
246 "(Warning) Found an additional reserved "
247 "entry for %s."), entry
);
256 (void) fclose(pfile
);
259 cryptoerror(LOG_STDERR
, gettext("write error."));
260 (void) fclose(pfile_tmp
);
261 if (unlink(tmpfile_name
) != 0) {
263 cryptoerror(LOG_STDERR
, gettext(
264 "(Warning) failed to remove %s: %s"), tmpfile_name
,
270 if (found_count
== 0) {
272 * The entry was not in config file before, append it to the
273 * end of the temp file.
275 if (fputs(entry
, pfile_tmp
) == EOF
) {
276 cryptoerror(LOG_STDERR
, gettext(
277 "failed to write to %s: %s"), tmpfile_name
,
279 (void) fclose(pfile_tmp
);
280 if (unlink(tmpfile_name
) != 0) {
282 cryptoerror(LOG_STDERR
, gettext(
283 "(Warning) failed to remove %s: %s"),
284 tmpfile_name
, strerror(err
));
290 if (fclose(pfile_tmp
) != 0) {
292 cryptoerror(LOG_STDERR
,
293 gettext("failed to close %s: %s"), tmpfile_name
,
298 if (rename(tmpfile_name
, conf_file
) == -1) {
300 cryptoerror(LOG_STDERR
,
301 gettext("failed to update the configuration - %s"),
303 cryptodebug("failed to rename %s to %s: %s", tmpfile_name
,
304 conf_file
, strerror(err
));
306 } else if (chmod(conf_file
,
307 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) == -1) {
309 cryptoerror(LOG_STDERR
,
310 gettext("failed to update the configuration - %s"),
312 cryptodebug("failed to chmod to %s: %s", conf_file
,
320 if (unlink(tmpfile_name
) != 0) {
322 cryptoerror(LOG_STDERR
, gettext(
323 "(Warning) failed to remove %s: %s"),
324 tmpfile_name
, strerror(err
));