10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57334SDaniel.Anderson@Sun.COM  * Common Development and Distribution License (the "License").
67334SDaniel.Anderson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
227334SDaniel.Anderson@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <errno.h>
270Sstevel@tonic-gate #include <fcntl.h>
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <time.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <locale.h>
340Sstevel@tonic-gate #include <sys/types.h>
35*7968Sopensolaris@drydog.com #include <zone.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include "cryptoadm.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate static int err; /* To store errno which may be overwritten by gettext() */
400Sstevel@tonic-gate static int build_entrylist(entry_t *, entrylist_t **);
410Sstevel@tonic-gate static entry_t *dup_entry(entry_t *);
420Sstevel@tonic-gate static mechlist_t *dup_mechlist(mechlist_t *);
430Sstevel@tonic-gate static entry_t *getent(char *, entrylist_t *);
440Sstevel@tonic-gate static int interpret(char *, entry_t **);
45*7968Sopensolaris@drydog.com static int parse_sup_dis_list(char *, entry_t *);
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * Duplicate the mechanism list.  A null pointer is returned if the storage
500Sstevel@tonic-gate  * space available is insufficient or the input argument is NULL.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate static mechlist_t *
530Sstevel@tonic-gate dup_mechlist(mechlist_t *plist)
540Sstevel@tonic-gate {
55*7968Sopensolaris@drydog.com 	mechlist_t	*pres = NULL;
56*7968Sopensolaris@drydog.com 	mechlist_t	*pcur;
57*7968Sopensolaris@drydog.com 	mechlist_t	*ptmp;
58*7968Sopensolaris@drydog.com 	int		rc = SUCCESS;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	while (plist != NULL) {
610Sstevel@tonic-gate 		if (!(ptmp = create_mech(plist->name))) {
620Sstevel@tonic-gate 			rc = FAILURE;
630Sstevel@tonic-gate 			break;
640Sstevel@tonic-gate 		}
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 		if (pres == NULL) {
670Sstevel@tonic-gate 			pres = pcur = ptmp;
680Sstevel@tonic-gate 		} else {
690Sstevel@tonic-gate 			pcur->next = ptmp;
700Sstevel@tonic-gate 			pcur = pcur->next;
710Sstevel@tonic-gate 		}
720Sstevel@tonic-gate 		plist = plist->next;
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (rc != SUCCESS) {
760Sstevel@tonic-gate 		free_mechlist(pres);
770Sstevel@tonic-gate 		return (NULL);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	return (pres);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Get the number of mechanisms in the mechanism list.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate int
880Sstevel@tonic-gate get_mech_count(mechlist_t *plist)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	int count = 0;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	while (plist != NULL) {
930Sstevel@tonic-gate 		count++;
940Sstevel@tonic-gate 		plist = plist->next;
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 	return (count);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate 
99*7968Sopensolaris@drydog.com /*
100*7968Sopensolaris@drydog.com  * Create one item of type entry_t with the provider name.
101*7968Sopensolaris@drydog.com  * Return NULL if there's not enough memory or provname is NULL.
102*7968Sopensolaris@drydog.com  */
103*7968Sopensolaris@drydog.com entry_t *
104*7968Sopensolaris@drydog.com create_entry(char *provname)
105*7968Sopensolaris@drydog.com {
106*7968Sopensolaris@drydog.com 	entry_t		*pent = NULL;
107*7968Sopensolaris@drydog.com 
108*7968Sopensolaris@drydog.com 	if (provname == NULL) {
109*7968Sopensolaris@drydog.com 		return (NULL);
110*7968Sopensolaris@drydog.com 	}
111*7968Sopensolaris@drydog.com 
112*7968Sopensolaris@drydog.com 	pent = calloc(1, sizeof (entry_t));
113*7968Sopensolaris@drydog.com 	if (pent == NULL) {
114*7968Sopensolaris@drydog.com 		cryptodebug("out of memory.");
115*7968Sopensolaris@drydog.com 		return (NULL);
116*7968Sopensolaris@drydog.com 	}
117*7968Sopensolaris@drydog.com 
118*7968Sopensolaris@drydog.com 	(void) strlcpy(pent->name, provname, MAXNAMELEN);
119*7968Sopensolaris@drydog.com 	pent->suplist = NULL;
120*7968Sopensolaris@drydog.com 	pent->sup_count = 0;
121*7968Sopensolaris@drydog.com 	pent->dislist = NULL;
122*7968Sopensolaris@drydog.com 	pent->dis_count = 0;
123*7968Sopensolaris@drydog.com 	pent->load = B_TRUE;
124*7968Sopensolaris@drydog.com 
125*7968Sopensolaris@drydog.com 	return (pent);
126*7968Sopensolaris@drydog.com }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
129*7968Sopensolaris@drydog.com  * Duplicate an entry for a provider from kcf.conf.
130*7968Sopensolaris@drydog.com  * Return NULL if memory is insufficient or the input argument is NULL.
131*7968Sopensolaris@drydog.com  * Called by getent().
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate static entry_t *
1340Sstevel@tonic-gate dup_entry(entry_t *pent1)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	entry_t	*pent2 = NULL;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (pent1 == NULL) {
1390Sstevel@tonic-gate 		return (NULL);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
142*7968Sopensolaris@drydog.com 	if ((pent2 = create_entry(pent1->name)) == NULL) {
1430Sstevel@tonic-gate 		cryptodebug("out of memory.");
1440Sstevel@tonic-gate 		return (NULL);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	pent2->sup_count = pent1->sup_count;
1480Sstevel@tonic-gate 	pent2->dis_count = pent1->dis_count;
149*7968Sopensolaris@drydog.com 	pent2->load = pent1->load;
1500Sstevel@tonic-gate 	if (pent1->suplist != NULL) {
1510Sstevel@tonic-gate 		pent2->suplist = dup_mechlist(pent1->suplist);
1520Sstevel@tonic-gate 		if (pent2->suplist == NULL) {
1530Sstevel@tonic-gate 			free_entry(pent2);
1540Sstevel@tonic-gate 			return (NULL);
1550Sstevel@tonic-gate 		}
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 	if (pent1->dislist != NULL) {
1580Sstevel@tonic-gate 		pent2->dislist = dup_mechlist(pent1->dislist);
1590Sstevel@tonic-gate 		if (pent2->dislist == NULL) {
1600Sstevel@tonic-gate 			free_entry(pent2);
1610Sstevel@tonic-gate 			return (NULL);
1620Sstevel@tonic-gate 		}
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	return (pent2);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * This routine parses the disabledlist or the supportedlist of an entry
1710Sstevel@tonic-gate  * in the kcf.conf configuration file.
1720Sstevel@tonic-gate  *
1730Sstevel@tonic-gate  * Arguments:
174*7968Sopensolaris@drydog.com  *	buf: an input argument which is a char string with the format of
1750Sstevel@tonic-gate  *	     "disabledlist=m1,m2,..." or "supportedlist=m1,m2,..."
1760Sstevel@tonic-gate  *	pent: the entry for the disabledlist.  This is an IN/OUT argument.
1770Sstevel@tonic-gate  *
1780Sstevel@tonic-gate  * Return value: SUCCESS or FAILURE.
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate static int
181*7968Sopensolaris@drydog.com parse_sup_dis_list(char *buf, entry_t *pent)
1820Sstevel@tonic-gate {
183*7968Sopensolaris@drydog.com 	mechlist_t	*pmech = NULL;
184*7968Sopensolaris@drydog.com 	mechlist_t	*phead = NULL;
185*7968Sopensolaris@drydog.com 	char		*next_token;
186*7968Sopensolaris@drydog.com 	char		*value;
187*7968Sopensolaris@drydog.com 	int		count;
188*7968Sopensolaris@drydog.com 	int		supflag = B_FALSE;
189*7968Sopensolaris@drydog.com 	int		disflag = B_FALSE;
190*7968Sopensolaris@drydog.com 	int		rc = SUCCESS;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if (strncmp(buf, EF_SUPPORTED, strlen(EF_SUPPORTED)) == 0) {
1930Sstevel@tonic-gate 		supflag = B_TRUE;
1940Sstevel@tonic-gate 	} else if (strncmp(buf, EF_DISABLED, strlen(EF_DISABLED)) == 0) {
1950Sstevel@tonic-gate 		disflag = B_TRUE;
1960Sstevel@tonic-gate 	} else {
1970Sstevel@tonic-gate 		/* should not come here */
1980Sstevel@tonic-gate 		return (FAILURE);
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if (value = strpbrk(buf, SEP_EQUAL)) {
2020Sstevel@tonic-gate 		value++; /* get rid of = */
2030Sstevel@tonic-gate 	} else {
2040Sstevel@tonic-gate 		cryptodebug("failed to parse the kcf.conf file.");
2050Sstevel@tonic-gate 		return (FAILURE);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if ((next_token = strtok(value, SEP_COMMA)) == NULL) {
2090Sstevel@tonic-gate 		cryptodebug("failed to parse the kcf.conf file.");
2100Sstevel@tonic-gate 		return (FAILURE);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if ((pmech = create_mech(next_token)) == NULL) {
2140Sstevel@tonic-gate 		return (FAILURE);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if (supflag) {
2180Sstevel@tonic-gate 		pent->suplist = phead = pmech;
2190Sstevel@tonic-gate 	} else if (disflag) {
2200Sstevel@tonic-gate 		pent->dislist = phead = pmech;
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	count = 1;
2240Sstevel@tonic-gate 	while (next_token) {
2250Sstevel@tonic-gate 		if (next_token = strtok(NULL, SEP_COMMA)) {
2260Sstevel@tonic-gate 			if ((pmech = create_mech(next_token)) == NULL) {
2270Sstevel@tonic-gate 				rc = FAILURE;
2280Sstevel@tonic-gate 				break;
2290Sstevel@tonic-gate 			}
2300Sstevel@tonic-gate 			count++;
2310Sstevel@tonic-gate 			phead->next = pmech;
2320Sstevel@tonic-gate 			phead = phead->next;
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	if (rc == SUCCESS) {
2370Sstevel@tonic-gate 		if (supflag) {
2380Sstevel@tonic-gate 			pent->sup_count = count;
2390Sstevel@tonic-gate 		} else if (disflag) {
2400Sstevel@tonic-gate 			pent->dis_count = count;
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 	} else {
2430Sstevel@tonic-gate 		free_mechlist(phead);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	return (rc);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
251*7968Sopensolaris@drydog.com  * Convert a char string containing a line about a provider
252*7968Sopensolaris@drydog.com  * from kcf.conf into an entry_t structure.
253*7968Sopensolaris@drydog.com  *
254*7968Sopensolaris@drydog.com  * See ent2str(), the reverse of this function, for the format of
255*7968Sopensolaris@drydog.com  * kcf.conf lines.
2560Sstevel@tonic-gate  */
2570Sstevel@tonic-gate static int
2580Sstevel@tonic-gate interpret(char *buf, entry_t **ppent)
2590Sstevel@tonic-gate {
260*7968Sopensolaris@drydog.com 	entry_t	*pent = NULL;
261*7968Sopensolaris@drydog.com 	char	*token1;
262*7968Sopensolaris@drydog.com 	char	*token2;
263*7968Sopensolaris@drydog.com 	char	*token3;
264*7968Sopensolaris@drydog.com 	int	rc;
2650Sstevel@tonic-gate 
266*7968Sopensolaris@drydog.com 	/* Get provider name */
2670Sstevel@tonic-gate 	if ((token1 = strtok(buf, SEP_COLON)) == NULL) { /* buf is NULL */
2680Sstevel@tonic-gate 		return (FAILURE);
2690Sstevel@tonic-gate 	};
2700Sstevel@tonic-gate 
271*7968Sopensolaris@drydog.com 	pent = create_entry(token1);
2720Sstevel@tonic-gate 	if (pent == NULL) {
2730Sstevel@tonic-gate 		cryptodebug("out of memory.");
2740Sstevel@tonic-gate 		return (FAILURE);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if ((token2 = strtok(NULL, SEP_SEMICOLON)) == NULL) {
2780Sstevel@tonic-gate 		/* The entry contains a provider name only */
2790Sstevel@tonic-gate 		free_entry(pent);
2800Sstevel@tonic-gate 		return (FAILURE);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 
283*7968Sopensolaris@drydog.com 	if (strncmp(token2, EF_UNLOAD, strlen(EF_UNLOAD)) == 0) {
284*7968Sopensolaris@drydog.com 		pent->load = B_FALSE; /* cryptoadm unload */
285*7968Sopensolaris@drydog.com 		if ((token2 = strtok(NULL, SEP_SEMICOLON)) == NULL) {
286*7968Sopensolaris@drydog.com 			/* The entry contains a provider name:unload only */
287*7968Sopensolaris@drydog.com 			free_entry(pent);
288*7968Sopensolaris@drydog.com 			return (FAILURE);
289*7968Sopensolaris@drydog.com 		}
290*7968Sopensolaris@drydog.com 	}
291*7968Sopensolaris@drydog.com 
2920Sstevel@tonic-gate 	/* need to get token3 first to satisfy nested strtok invocations */
293*7968Sopensolaris@drydog.com 	token3 = strtok(NULL, SEP_SEMICOLON); /* optional */
2940Sstevel@tonic-gate 
295*7968Sopensolaris@drydog.com 	/* parse supportedlist (or disabledlist if no supportedlist) */
296*7968Sopensolaris@drydog.com 	if ((token2 != NULL) && ((rc = parse_sup_dis_list(token2, pent)) !=
297*7968Sopensolaris@drydog.com 	    SUCCESS)) {
2980Sstevel@tonic-gate 		free_entry(pent);
2990Sstevel@tonic-gate 		return (rc);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
302*7968Sopensolaris@drydog.com 	/* parse disabledlist (if there's a supportedlist) */
303*7968Sopensolaris@drydog.com 	if ((token3 != NULL) && ((rc = parse_sup_dis_list(token3, pent)) !=
304*7968Sopensolaris@drydog.com 	    SUCCESS)) {
3050Sstevel@tonic-gate 		free_entry(pent);
3060Sstevel@tonic-gate 		return (rc);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	*ppent = pent;
3100Sstevel@tonic-gate 	return (SUCCESS);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
315*7968Sopensolaris@drydog.com  * Add an entry about a provider from kcf.conf to the end of an entry list.
316*7968Sopensolaris@drydog.com  * If the entry list pplist is NULL, create the linked list with pent as the
317*7968Sopensolaris@drydog.com  * first element.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate static int
3200Sstevel@tonic-gate build_entrylist(entry_t *pent, entrylist_t **pplist)
3210Sstevel@tonic-gate {
322*7968Sopensolaris@drydog.com 	entrylist_t	*pentlist;
323*7968Sopensolaris@drydog.com 	entrylist_t	*pcur = NULL;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	pentlist = malloc(sizeof (entrylist_t));
3260Sstevel@tonic-gate 	if (pentlist == NULL) {
3270Sstevel@tonic-gate 		cryptodebug("out of memory.");
3280Sstevel@tonic-gate 		return (FAILURE);
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	pentlist->pent = pent;
3310Sstevel@tonic-gate 	pentlist->next = NULL;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	if (*pplist) {
3340Sstevel@tonic-gate 		pcur = *pplist;
3350Sstevel@tonic-gate 		while (pcur->next != NULL)
3360Sstevel@tonic-gate 			pcur = pcur->next;
3370Sstevel@tonic-gate 		pcur->next = pentlist;
3380Sstevel@tonic-gate 	} else { /* empty list */
3390Sstevel@tonic-gate 		*pplist = pentlist;
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	return (SUCCESS);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate /*
3480Sstevel@tonic-gate  * Find the entry with the "provname" name from the entry list and duplicate
349*7968Sopensolaris@drydog.com  * it.  Called by getent_kef().
3500Sstevel@tonic-gate  */
3510Sstevel@tonic-gate static entry_t *
3520Sstevel@tonic-gate getent(char *provname, entrylist_t *entrylist)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	boolean_t	found = B_FALSE;
3550Sstevel@tonic-gate 	entry_t		*pent1 = NULL;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	if ((provname == NULL) || (entrylist == NULL)) {
3580Sstevel@tonic-gate 		return (NULL);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	while (!found && entrylist) {
3620Sstevel@tonic-gate 		if (strcmp(entrylist->pent->name, provname) == 0) {
3630Sstevel@tonic-gate 			found = B_TRUE;
3640Sstevel@tonic-gate 			pent1 = entrylist->pent;
3650Sstevel@tonic-gate 		} else {
3660Sstevel@tonic-gate 			entrylist = entrylist->next;
3670Sstevel@tonic-gate 		}
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (!found) {
3710Sstevel@tonic-gate 		return (NULL);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	/* duplicate the entry to be returned */
3750Sstevel@tonic-gate 	return (dup_entry(pent1));
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 
379*7968Sopensolaris@drydog.com /*
380*7968Sopensolaris@drydog.com  * Free memory in entry_t.
381*7968Sopensolaris@drydog.com  * That is, the supported and disabled lists for a provider
382*7968Sopensolaris@drydog.com  * from kcf.conf.
383*7968Sopensolaris@drydog.com  */
3840Sstevel@tonic-gate void
3850Sstevel@tonic-gate free_entry(entry_t  *pent)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	if (pent == NULL) {
3880Sstevel@tonic-gate 		return;
3890Sstevel@tonic-gate 	} else {
3900Sstevel@tonic-gate 		free_mechlist(pent->suplist);
3910Sstevel@tonic-gate 		free_mechlist(pent->dislist);
3920Sstevel@tonic-gate 		free(pent);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 
397*7968Sopensolaris@drydog.com /*
398*7968Sopensolaris@drydog.com  * Free elements in a entrylist_t linked list,
399*7968Sopensolaris@drydog.com  * which lists providers in kcf.conf.
400*7968Sopensolaris@drydog.com  */
4010Sstevel@tonic-gate void
4020Sstevel@tonic-gate free_entrylist(entrylist_t *entrylist)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	entrylist_t *pnext;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	while (entrylist != NULL) {
4070Sstevel@tonic-gate 		pnext = entrylist->next;
4080Sstevel@tonic-gate 		free_entry(entrylist->pent);
4090Sstevel@tonic-gate 		entrylist = pnext;
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate  * Convert an entry to a string.  This routine builds a string for the entry
416*7968Sopensolaris@drydog.com  * to be inserted in the kcf.conf file.  Based on the content of each entry,
417*7968Sopensolaris@drydog.com  * the result string can be one of these 6 forms:
4180Sstevel@tonic-gate  *  - name:supportedlist=m1,m2,...,mj
4190Sstevel@tonic-gate  *  - name:disabledlist=m1,m2,...,mj
4200Sstevel@tonic-gate  *  - name:supportedlist=m1,...,mj;disabledlist=m1,m2,...,mk
4210Sstevel@tonic-gate  *
422*7968Sopensolaris@drydog.com  *  - name:unload;supportedlist=m1,m2,...,mj
423*7968Sopensolaris@drydog.com  *  - name:unload;disabledlist=m1,m2,...,mj
424*7968Sopensolaris@drydog.com  *  - name:unload;supportedlist=m1,...,mj;disabledlist=m1,m2,...,mk
425*7968Sopensolaris@drydog.com  *
426*7968Sopensolaris@drydog.com  * Note that the caller is responsible for freeing the returned string
427*7968Sopensolaris@drydog.com  * (with free_entry()).
428*7968Sopensolaris@drydog.com  * See interpret() for the reverse of this function: converting a string
429*7968Sopensolaris@drydog.com  * to an entry_t.
4300Sstevel@tonic-gate  */
4310Sstevel@tonic-gate char *
4320Sstevel@tonic-gate ent2str(entry_t *pent)
4330Sstevel@tonic-gate {
434*7968Sopensolaris@drydog.com 	char		*buf;
435*7968Sopensolaris@drydog.com 	mechlist_t	*pcur = NULL;
436*7968Sopensolaris@drydog.com 	boolean_t	semicolon_separator = B_FALSE;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if (pent == NULL) {
4400Sstevel@tonic-gate 		return (NULL);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	if ((buf = malloc(BUFSIZ)) == NULL) {
4440Sstevel@tonic-gate 		return (NULL);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	/* convert the provider name */
4480Sstevel@tonic-gate 	if (strlcpy(buf, pent->name, BUFSIZ) >= BUFSIZ) {
4490Sstevel@tonic-gate 		free(buf);
4500Sstevel@tonic-gate 		return (NULL);
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
453*7968Sopensolaris@drydog.com 	if (!pent->load) { /* add "unload" keyword */
454*7968Sopensolaris@drydog.com 		if (strlcat(buf, SEP_COLON, BUFSIZ) >= BUFSIZ) {
455*7968Sopensolaris@drydog.com 			free(buf);
456*7968Sopensolaris@drydog.com 			return (NULL);
457*7968Sopensolaris@drydog.com 		}
458*7968Sopensolaris@drydog.com 
459*7968Sopensolaris@drydog.com 		if (strlcat(buf, EF_UNLOAD, BUFSIZ) >= BUFSIZ) {
460*7968Sopensolaris@drydog.com 			free(buf);
461*7968Sopensolaris@drydog.com 			return (NULL);
462*7968Sopensolaris@drydog.com 		}
463*7968Sopensolaris@drydog.com 
464*7968Sopensolaris@drydog.com 		semicolon_separator = B_TRUE;
465*7968Sopensolaris@drydog.com 	}
466*7968Sopensolaris@drydog.com 
4670Sstevel@tonic-gate 	/* convert the supported list if any */
468*7968Sopensolaris@drydog.com 	pcur = pent->suplist;
469*7968Sopensolaris@drydog.com 	if (pcur != NULL) {
470*7968Sopensolaris@drydog.com 		if (strlcat(buf,
471*7968Sopensolaris@drydog.com 		    semicolon_separator ? SEP_SEMICOLON : SEP_COLON,
472*7968Sopensolaris@drydog.com 		    BUFSIZ) >= BUFSIZ) {
4730Sstevel@tonic-gate 			free(buf);
4740Sstevel@tonic-gate 			return (NULL);
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		if (strlcat(buf, EF_SUPPORTED, BUFSIZ) >= BUFSIZ) {
4780Sstevel@tonic-gate 			free(buf);
4790Sstevel@tonic-gate 			return (NULL);
4800Sstevel@tonic-gate 		}
4810Sstevel@tonic-gate 
482*7968Sopensolaris@drydog.com 		while (pcur != NULL) {
483*7968Sopensolaris@drydog.com 			if (strlcat(buf, pcur->name, BUFSIZ) >= BUFSIZ) {
4840Sstevel@tonic-gate 				free(buf);
4850Sstevel@tonic-gate 				return (NULL);
4860Sstevel@tonic-gate 			}
4870Sstevel@tonic-gate 
488*7968Sopensolaris@drydog.com 			pcur = pcur->next;
489*7968Sopensolaris@drydog.com 			if (pcur != NULL) {
4900Sstevel@tonic-gate 				if (strlcat(buf, SEP_COMMA, BUFSIZ)
4910Sstevel@tonic-gate 				    >= BUFSIZ) {
4920Sstevel@tonic-gate 					free(buf);
4930Sstevel@tonic-gate 					return (NULL);
4940Sstevel@tonic-gate 				}
4950Sstevel@tonic-gate 			}
4960Sstevel@tonic-gate 		}
497*7968Sopensolaris@drydog.com 		semicolon_separator = B_TRUE;
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/* convert the disabled list if any */
501*7968Sopensolaris@drydog.com 	pcur = pent->dislist;
502*7968Sopensolaris@drydog.com 	if (pcur != NULL) {
503*7968Sopensolaris@drydog.com 		if (strlcat(buf,
504*7968Sopensolaris@drydog.com 		    semicolon_separator ? SEP_SEMICOLON : SEP_COLON,
505*7968Sopensolaris@drydog.com 		    BUFSIZ) >= BUFSIZ) {
506*7968Sopensolaris@drydog.com 			free(buf);
507*7968Sopensolaris@drydog.com 			return (NULL);
5080Sstevel@tonic-gate 		}
5090Sstevel@tonic-gate 
510*7968Sopensolaris@drydog.com 		if (strlcat(buf, EF_DISABLED, BUFSIZ) >= BUFSIZ) {
511*7968Sopensolaris@drydog.com 			free(buf);
512*7968Sopensolaris@drydog.com 			return (NULL);
513*7968Sopensolaris@drydog.com 		}
514*7968Sopensolaris@drydog.com 
515*7968Sopensolaris@drydog.com 		while (pcur != NULL) {
516*7968Sopensolaris@drydog.com 			if (strlcat(buf, pcur->name, BUFSIZ) >= BUFSIZ) {
5170Sstevel@tonic-gate 				free(buf);
5180Sstevel@tonic-gate 				return (NULL);
5190Sstevel@tonic-gate 			}
5200Sstevel@tonic-gate 
521*7968Sopensolaris@drydog.com 			pcur = pcur->next;
522*7968Sopensolaris@drydog.com 			if (pcur != NULL) {
5230Sstevel@tonic-gate 				if (strlcat(buf, SEP_COMMA, BUFSIZ)
5240Sstevel@tonic-gate 				    >= BUFSIZ) {
5250Sstevel@tonic-gate 					free(buf);
5260Sstevel@tonic-gate 					return (NULL);
5270Sstevel@tonic-gate 				}
5280Sstevel@tonic-gate 			}
5290Sstevel@tonic-gate 		}
530*7968Sopensolaris@drydog.com 		semicolon_separator = B_TRUE;
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	if (strlcat(buf, "\n", BUFSIZ) >= BUFSIZ) {
5340Sstevel@tonic-gate 		free(buf);
5350Sstevel@tonic-gate 		return (NULL);
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	return (buf);
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate /*
5430Sstevel@tonic-gate  * Enable the mechanisms for the provider pointed by *ppent.  If allflag is
5440Sstevel@tonic-gate  * TRUE, enable all.  Otherwise, enable the mechanisms specified in the 3rd
5450Sstevel@tonic-gate  * argument "mlist".  The result will be stored in ppent also.
5460Sstevel@tonic-gate  */
5470Sstevel@tonic-gate int
5480Sstevel@tonic-gate enable_mechs(entry_t **ppent, boolean_t allflag, mechlist_t *mlist)
5490Sstevel@tonic-gate {
550*7968Sopensolaris@drydog.com 	entry_t		*pent;
551*7968Sopensolaris@drydog.com 	mechlist_t	*phead; /* the current and resulting disabled list */
552*7968Sopensolaris@drydog.com 	mechlist_t	*ptr = NULL;
553*7968Sopensolaris@drydog.com 	mechlist_t	*pcur = NULL;
554*7968Sopensolaris@drydog.com 	boolean_t	found;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	pent = *ppent;
5570Sstevel@tonic-gate 	if (pent == NULL) {
5580Sstevel@tonic-gate 		return (FAILURE);
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if (allflag) {
5620Sstevel@tonic-gate 		free_mechlist(pent->dislist);
5630Sstevel@tonic-gate 		pent->dis_count = 0;
5640Sstevel@tonic-gate 		pent->dislist = NULL;
5650Sstevel@tonic-gate 		return (SUCCESS);
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	/*
5690Sstevel@tonic-gate 	 * for each mechanism in the to-be-enabled mechanism list,
5700Sstevel@tonic-gate 	 * -	check if it is in the current disabled list
5710Sstevel@tonic-gate 	 * -	if found, delete it from the disabled list
572*7968Sopensolaris@drydog.com 	 *	otherwise, give a warning.
5730Sstevel@tonic-gate 	 */
5740Sstevel@tonic-gate 	ptr = mlist;
5750Sstevel@tonic-gate 	while (ptr != NULL) {
5760Sstevel@tonic-gate 		found = B_FALSE;
5770Sstevel@tonic-gate 		phead = pcur =  pent->dislist;
5780Sstevel@tonic-gate 		while (!found && pcur) {
5790Sstevel@tonic-gate 			if (strcmp(pcur->name, ptr->name) == 0) {
5800Sstevel@tonic-gate 				found = B_TRUE;
5810Sstevel@tonic-gate 			} else {
5820Sstevel@tonic-gate 				phead = pcur;
5830Sstevel@tonic-gate 				pcur = pcur->next;
5840Sstevel@tonic-gate 			}
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 		if (found) {
5880Sstevel@tonic-gate 			if (phead == pcur) {
5890Sstevel@tonic-gate 				pent->dislist = pent->dislist->next;
5900Sstevel@tonic-gate 				free(pcur);
5910Sstevel@tonic-gate 			} else {
5920Sstevel@tonic-gate 				phead->next = pcur->next;
5930Sstevel@tonic-gate 				free(pcur);
5940Sstevel@tonic-gate 			}
5950Sstevel@tonic-gate 			pent->dis_count--;
5960Sstevel@tonic-gate 		} else {
5970Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
5980Sstevel@tonic-gate 			    "(Warning) %1$s is either enabled already or not "
5990Sstevel@tonic-gate 			    "a valid mechanism for %2$s"), ptr->name,
6000Sstevel@tonic-gate 			    pent->name);
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 		ptr = ptr->next;
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	if (pent->dis_count == 0) {
6060Sstevel@tonic-gate 		pent->dislist = NULL;
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	return (SUCCESS);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 
614*7968Sopensolaris@drydog.com /*
615*7968Sopensolaris@drydog.com  * Determine if the kernel provider name, path, is a device
616*7968Sopensolaris@drydog.com  * (that is, it contains a slash character (e.g., "mca/0").
617*7968Sopensolaris@drydog.com  * If so, it is a hardware provider; otherwise it is a software provider.
618*7968Sopensolaris@drydog.com  */
6190Sstevel@tonic-gate boolean_t
6200Sstevel@tonic-gate is_device(char *path)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	if (strchr(path, SEP_SLASH) != NULL) {
6230Sstevel@tonic-gate 		return (B_TRUE);
6240Sstevel@tonic-gate 	} else {
6250Sstevel@tonic-gate 		return (B_FALSE);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate  * Split a hardware provider name with the "name/inst_num" format into
631*7968Sopensolaris@drydog.com  * a name and a number (e.g., split "mca/0" into "mca" instance 0).
6320Sstevel@tonic-gate  */
6330Sstevel@tonic-gate int
6340Sstevel@tonic-gate split_hw_provname(char *provname, char *pname, int *inst_num)
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 	char	name[MAXNAMELEN];
6370Sstevel@tonic-gate 	char	*inst_str;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (provname == NULL) {
6400Sstevel@tonic-gate 		return (FAILURE);
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	(void) strlcpy(name, provname, MAXNAMELEN);
6440Sstevel@tonic-gate 	if (strtok(name, "/") == NULL) {
6450Sstevel@tonic-gate 		return (FAILURE);
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	if ((inst_str = strtok(NULL, "/")) == NULL) {
6490Sstevel@tonic-gate 		return (FAILURE);
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	(void) strlcpy(pname, name, MAXNAMELEN);
6530Sstevel@tonic-gate 	*inst_num = atoi(inst_str);
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	return (SUCCESS);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate /*
660*7968Sopensolaris@drydog.com  * Retrieve information from kcf.conf and build a hardware device entry list
661*7968Sopensolaris@drydog.com  * and a software entry list of kernel crypto providers.
662*7968Sopensolaris@drydog.com  *
663*7968Sopensolaris@drydog.com  * This list is usually incomplete, as kernel crypto providers only have to
664*7968Sopensolaris@drydog.com  * be listed in kcf.conf if a mechanism is disabled (by cryptoadm) or
665*7968Sopensolaris@drydog.com  * if the kernel provider module is not one of the default kernel providers.
666*7968Sopensolaris@drydog.com  *
667*7968Sopensolaris@drydog.com  * The kcf.conf file is available only in the global zone.
6680Sstevel@tonic-gate  */
6690Sstevel@tonic-gate int
6700Sstevel@tonic-gate get_kcfconf_info(entrylist_t **ppdevlist, entrylist_t **ppsoftlist)
6710Sstevel@tonic-gate {
672*7968Sopensolaris@drydog.com 	FILE	*pfile = NULL;
673*7968Sopensolaris@drydog.com 	char	buffer[BUFSIZ];
674*7968Sopensolaris@drydog.com 	int	len;
675*7968Sopensolaris@drydog.com 	entry_t	*pent = NULL;
676*7968Sopensolaris@drydog.com 	int	rc = SUCCESS;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	if ((pfile = fopen(_PATH_KCF_CONF, "r")) == NULL) {
6790Sstevel@tonic-gate 		cryptodebug("failed to open the kcf.conf file for read only");
6800Sstevel@tonic-gate 		return (FAILURE);
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	*ppdevlist = NULL;
6840Sstevel@tonic-gate 	*ppsoftlist = NULL;
6850Sstevel@tonic-gate 	while (fgets(buffer, BUFSIZ, pfile) != NULL) {
6860Sstevel@tonic-gate 		if (buffer[0] == '#' || buffer[0] == ' ' ||
6870Sstevel@tonic-gate 		    buffer[0] == '\n'|| buffer[0] == '\t') {
6880Sstevel@tonic-gate 			continue;   /* ignore comment lines */
6890Sstevel@tonic-gate 		}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		len = strlen(buffer);
692*7968Sopensolaris@drydog.com 		if (buffer[len - 1] == '\n') { /* get rid of trailing '\n' */
6930Sstevel@tonic-gate 			len--;
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 		buffer[len] = '\0';
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		if ((rc = interpret(buffer,  &pent)) == SUCCESS) {
6980Sstevel@tonic-gate 			if (is_device(pent->name)) {
6990Sstevel@tonic-gate 				rc = build_entrylist(pent, ppdevlist);
7000Sstevel@tonic-gate 			} else {
7010Sstevel@tonic-gate 				rc = build_entrylist(pent, ppsoftlist);
7020Sstevel@tonic-gate 			}
7030Sstevel@tonic-gate 		} else {
7040Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
7050Sstevel@tonic-gate 			    "failed to parse configuration."));
7060Sstevel@tonic-gate 		}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 		if (rc != SUCCESS) {
7090Sstevel@tonic-gate 			free_entrylist(*ppdevlist);
7100Sstevel@tonic-gate 			free_entrylist(*ppsoftlist);
7110Sstevel@tonic-gate 			free_entry(pent);
7120Sstevel@tonic-gate 			break;
7130Sstevel@tonic-gate 		}
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	(void) fclose(pfile);
7170Sstevel@tonic-gate 	return (rc);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate /*
7210Sstevel@tonic-gate  * Retrieve information from admin device and build a device entry list and
722*7968Sopensolaris@drydog.com  * a software entry list.  This is used where there is no kcf.conf, e.g., the
7230Sstevel@tonic-gate  * non-global zone.
7240Sstevel@tonic-gate  */
7250Sstevel@tonic-gate int
7260Sstevel@tonic-gate get_admindev_info(entrylist_t **ppdevlist, entrylist_t **ppsoftlist)
7270Sstevel@tonic-gate {
728*7968Sopensolaris@drydog.com 	crypto_get_dev_list_t	*pdevlist_kernel = NULL;
729*7968Sopensolaris@drydog.com 	crypto_get_soft_list_t	*psoftlist_kernel = NULL;
730*7968Sopensolaris@drydog.com 	char			*devname;
731*7968Sopensolaris@drydog.com 	int			inst_num;
732*7968Sopensolaris@drydog.com 	int			mcount;
733*7968Sopensolaris@drydog.com 	mechlist_t		*pmech = NULL;
734*7968Sopensolaris@drydog.com 	entry_t			*pent_dev = NULL, *pent_soft = NULL;
735*7968Sopensolaris@drydog.com 	int			i;
736*7968Sopensolaris@drydog.com 	char			*psoftname;
737*7968Sopensolaris@drydog.com 	entrylist_t		*tmp_pdev = NULL;
738*7968Sopensolaris@drydog.com 	entrylist_t		*tmp_psoft = NULL;
739*7968Sopensolaris@drydog.com 	entrylist_t		*phardlist = NULL, *psoftlist = NULL;
7400Sstevel@tonic-gate 
741*7968Sopensolaris@drydog.com 	/*
742*7968Sopensolaris@drydog.com 	 * Get hardware providers
743*7968Sopensolaris@drydog.com 	 */
7440Sstevel@tonic-gate 	if (get_dev_list(&pdevlist_kernel) != SUCCESS) {
7450Sstevel@tonic-gate 		cryptodebug("failed to get hardware provider list from kernel");
7460Sstevel@tonic-gate 		return (FAILURE);
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	for (i = 0; i < pdevlist_kernel->dl_dev_count; i++) {
7500Sstevel@tonic-gate 		devname = pdevlist_kernel->dl_devs[i].le_dev_name;
7510Sstevel@tonic-gate 		inst_num = pdevlist_kernel->dl_devs[i].le_dev_instance;
7520Sstevel@tonic-gate 		mcount = pdevlist_kernel->dl_devs[i].le_mechanism_count;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 		pmech = NULL;
7550Sstevel@tonic-gate 		if (get_dev_info(devname, inst_num, mcount, &pmech) !=
7560Sstevel@tonic-gate 		    SUCCESS) {
7570Sstevel@tonic-gate 			cryptodebug(
7580Sstevel@tonic-gate 			    "failed to retrieve the mechanism list for %s/%d.",
7590Sstevel@tonic-gate 			    devname, inst_num);
7600Sstevel@tonic-gate 			goto fail_out;
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 
763*7968Sopensolaris@drydog.com 		if ((pent_dev = create_entry(devname)) == NULL) {
7640Sstevel@tonic-gate 			cryptodebug("out of memory.");
7650Sstevel@tonic-gate 			free_mechlist(pmech);
7660Sstevel@tonic-gate 			goto fail_out;
7670Sstevel@tonic-gate 		}
768*7968Sopensolaris@drydog.com 		pent_dev->suplist = pmech;
769*7968Sopensolaris@drydog.com 		pent_dev->sup_count = mcount;
7700Sstevel@tonic-gate 
771*7968Sopensolaris@drydog.com 		if (build_entrylist(pent_dev, &tmp_pdev) != SUCCESS) {
7720Sstevel@tonic-gate 			goto fail_out;
7730Sstevel@tonic-gate 		}
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	free(pdevlist_kernel);
7770Sstevel@tonic-gate 	pdevlist_kernel = NULL;
7780Sstevel@tonic-gate 
779*7968Sopensolaris@drydog.com 	/*
780*7968Sopensolaris@drydog.com 	 * Get software providers
781*7968Sopensolaris@drydog.com 	 */
782*7968Sopensolaris@drydog.com 	if (getzoneid() == GLOBAL_ZONEID) {
783*7968Sopensolaris@drydog.com 		if (get_kcfconf_info(&phardlist, &psoftlist) != SUCCESS) {
784*7968Sopensolaris@drydog.com 			goto fail_out;
785*7968Sopensolaris@drydog.com 		}
786*7968Sopensolaris@drydog.com 	}
787*7968Sopensolaris@drydog.com 
7880Sstevel@tonic-gate 	if (get_soft_list(&psoftlist_kernel) != SUCCESS) {
7890Sstevel@tonic-gate 		cryptodebug("failed to get software provider list from kernel");
7900Sstevel@tonic-gate 		goto fail_out;
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	for (i = 0, psoftname = psoftlist_kernel->sl_soft_names;
7940Sstevel@tonic-gate 	    i < psoftlist_kernel->sl_soft_count;
7950Sstevel@tonic-gate 	    i++, psoftname = psoftname + strlen(psoftname) + 1) {
7960Sstevel@tonic-gate 		pmech = NULL;
797*7968Sopensolaris@drydog.com 		if (get_soft_info(psoftname, &pmech, phardlist, psoftlist) !=
798*7968Sopensolaris@drydog.com 		    SUCCESS) {
7990Sstevel@tonic-gate 			cryptodebug(
8000Sstevel@tonic-gate 			    "failed to retrieve the mechanism list for %s.",
8010Sstevel@tonic-gate 			    psoftname);
8020Sstevel@tonic-gate 			goto fail_out;
8030Sstevel@tonic-gate 		}
8040Sstevel@tonic-gate 
805*7968Sopensolaris@drydog.com 		if ((pent_soft = create_entry(psoftname)) == NULL) {
8060Sstevel@tonic-gate 			cryptodebug("out of memory.");
8070Sstevel@tonic-gate 			free_mechlist(pmech);
8080Sstevel@tonic-gate 			goto fail_out;
8090Sstevel@tonic-gate 		}
810*7968Sopensolaris@drydog.com 		pent_soft->suplist = pmech;
811*7968Sopensolaris@drydog.com 		pent_soft->sup_count = get_mech_count(pmech);
8120Sstevel@tonic-gate 
813*7968Sopensolaris@drydog.com 		if (build_entrylist(pent_soft, &tmp_psoft) != SUCCESS) {
8140Sstevel@tonic-gate 			goto fail_out;
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	free(psoftlist_kernel);
8190Sstevel@tonic-gate 	psoftlist_kernel = NULL;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	*ppdevlist = tmp_pdev;
8220Sstevel@tonic-gate 	*ppsoftlist = tmp_psoft;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	return (SUCCESS);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate fail_out:
827*7968Sopensolaris@drydog.com 	if (pent_dev != NULL)
828*7968Sopensolaris@drydog.com 		free_entry(pent_dev);
829*7968Sopensolaris@drydog.com 	if (pent_soft != NULL)
830*7968Sopensolaris@drydog.com 		free_entry(pent_soft);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	free_entrylist(tmp_pdev);
8330Sstevel@tonic-gate 	free_entrylist(tmp_psoft);
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	if (pdevlist_kernel != NULL)
8360Sstevel@tonic-gate 		free(pdevlist_kernel);
8370Sstevel@tonic-gate 	if (psoftlist_kernel != NULL)
8380Sstevel@tonic-gate 		free(psoftlist_kernel);
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	return (FAILURE);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate /*
844*7968Sopensolaris@drydog.com  * Return configuration information for a kernel provider from kcf.conf.
845*7968Sopensolaris@drydog.com  * For kernel software providers return a enabled list and disabled list.
846*7968Sopensolaris@drydog.com  * For kernel hardware providers return just a disabled list.
847*7968Sopensolaris@drydog.com  *
848*7968Sopensolaris@drydog.com  * Parameters phardlist and psoftlist are supplied by get_kcfconf_info().
849*7968Sopensolaris@drydog.com  * If NULL, this function calls get_kcfconf_info() internally.
8500Sstevel@tonic-gate  */
8510Sstevel@tonic-gate entry_t *
852*7968Sopensolaris@drydog.com getent_kef(char *provname, entrylist_t *phardlist, entrylist_t *psoftlist)
8530Sstevel@tonic-gate {
854*7968Sopensolaris@drydog.com 	entry_t		*pent = NULL;
855*7968Sopensolaris@drydog.com 	boolean_t	memory_allocated = B_FALSE;
8560Sstevel@tonic-gate 
857*7968Sopensolaris@drydog.com 	if ((phardlist == NULL) || (psoftlist == NULL)) {
858*7968Sopensolaris@drydog.com 		if (get_kcfconf_info(&phardlist, &psoftlist) != SUCCESS) {
859*7968Sopensolaris@drydog.com 			return (NULL);
860*7968Sopensolaris@drydog.com 		}
861*7968Sopensolaris@drydog.com 		memory_allocated = B_TRUE;
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	if (is_device(provname)) {
865*7968Sopensolaris@drydog.com 		pent = getent(provname, phardlist);
8660Sstevel@tonic-gate 	} else {
8670Sstevel@tonic-gate 		pent = getent(provname, psoftlist);
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
870*7968Sopensolaris@drydog.com 	if (memory_allocated) {
871*7968Sopensolaris@drydog.com 		free_entrylist(phardlist);
872*7968Sopensolaris@drydog.com 		free_entrylist(psoftlist);
873*7968Sopensolaris@drydog.com 	}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	return (pent);
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate /*
8790Sstevel@tonic-gate  * Print out the provider name and the mechanism list.
8800Sstevel@tonic-gate  */
8810Sstevel@tonic-gate void
8820Sstevel@tonic-gate print_mechlist(char *provname, mechlist_t *pmechlist)
8830Sstevel@tonic-gate {
884*7968Sopensolaris@drydog.com 	mechlist_t *ptr = NULL;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	if (provname == NULL) {
8870Sstevel@tonic-gate 		return;
8880Sstevel@tonic-gate 	}
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	(void) printf("%s: ", provname);
8910Sstevel@tonic-gate 	if (pmechlist == NULL) {
8920Sstevel@tonic-gate 		(void) printf(gettext("No mechanisms presented.\n"));
8930Sstevel@tonic-gate 		return;
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	ptr = pmechlist;
8970Sstevel@tonic-gate 	while (ptr != NULL) {
8980Sstevel@tonic-gate 		(void) printf("%s", ptr->name);
8990Sstevel@tonic-gate 		ptr = ptr->next;
9000Sstevel@tonic-gate 		if (ptr == NULL) {
9010Sstevel@tonic-gate 			(void) printf("\n");
9020Sstevel@tonic-gate 		} else {
9030Sstevel@tonic-gate 			(void) printf(",");
9040Sstevel@tonic-gate 		}
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate /*
910*7968Sopensolaris@drydog.com  * Update the kcf.conf file based on the update mode:
911*7968Sopensolaris@drydog.com  * - If update_mode is MODIFY_MODE, modify the entry with the same name.
912*7968Sopensolaris@drydog.com  *   If not found, append a new entry to the kcf.conf file.
913*7968Sopensolaris@drydog.com  * - If update_mode is DELETE_MODE, delete the entry with the same name.
914*7968Sopensolaris@drydog.com  * - If update_mode is ADD_MODE, append a new entry to the kcf.conf file.
9150Sstevel@tonic-gate  */
9160Sstevel@tonic-gate int
9170Sstevel@tonic-gate update_kcfconf(entry_t *pent, int update_mode)
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate 	boolean_t	add_it = B_FALSE;
9200Sstevel@tonic-gate 	boolean_t	delete_it = B_FALSE;
9210Sstevel@tonic-gate 	boolean_t	found_entry = B_FALSE;
922*7968Sopensolaris@drydog.com 	FILE		*pfile = NULL;
923*7968Sopensolaris@drydog.com 	FILE		*pfile_tmp = NULL;
924*7968Sopensolaris@drydog.com 	char		buffer[BUFSIZ];
925*7968Sopensolaris@drydog.com 	char		buffer2[BUFSIZ];
926*7968Sopensolaris@drydog.com 	char		tmpfile_name[MAXPATHLEN];
927*7968Sopensolaris@drydog.com 	char		*name;
928*7968Sopensolaris@drydog.com 	char		*new_str = NULL;
929*7968Sopensolaris@drydog.com 	int		rc = SUCCESS;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	if (pent == NULL) {
9320Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("internal error."));
9330Sstevel@tonic-gate 		return (FAILURE);
9340Sstevel@tonic-gate 	}
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	/* Check the update_mode */
937*7968Sopensolaris@drydog.com 	switch (update_mode) {
938*7968Sopensolaris@drydog.com 	case ADD_MODE:
9390Sstevel@tonic-gate 		add_it = B_TRUE;
940*7968Sopensolaris@drydog.com 		/* FALLTHROUGH */
941*7968Sopensolaris@drydog.com 	case MODIFY_MODE:
942*7968Sopensolaris@drydog.com 		/* Convert the entry a string to add to kcf.conf  */
9430Sstevel@tonic-gate 		if ((new_str = ent2str(pent)) == NULL) {
9440Sstevel@tonic-gate 			return (FAILURE);
9450Sstevel@tonic-gate 		}
946*7968Sopensolaris@drydog.com 		break;
947*7968Sopensolaris@drydog.com 	case DELETE_MODE:
9480Sstevel@tonic-gate 		delete_it = B_TRUE;
949*7968Sopensolaris@drydog.com 		break;
950*7968Sopensolaris@drydog.com 	default:
9510Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("internal error."));
9520Sstevel@tonic-gate 		return (FAILURE);
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/* Open the kcf.conf file */
9560Sstevel@tonic-gate 	if ((pfile = fopen(_PATH_KCF_CONF, "r+")) == NULL) {
9570Sstevel@tonic-gate 		err = errno;
9580Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
9590Sstevel@tonic-gate 		    gettext("failed to update the configuration - %s"),
9600Sstevel@tonic-gate 		    strerror(err));
9610Sstevel@tonic-gate 		cryptodebug("failed to open %s for write.", _PATH_KCF_CONF);
9620Sstevel@tonic-gate 		return (FAILURE);
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/* Lock the kcf.conf file */
9660Sstevel@tonic-gate 	if (lockf(fileno(pfile), F_TLOCK, 0) == -1) {
9670Sstevel@tonic-gate 		err = errno;
9680Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
9690Sstevel@tonic-gate 		    gettext("failed to update the configuration - %s"),
970*7968Sopensolaris@drydog.com 		    strerror(err));
9710Sstevel@tonic-gate 		(void) fclose(pfile);
9720Sstevel@tonic-gate 		return (FAILURE);
9730Sstevel@tonic-gate 	}
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	/*
9760Sstevel@tonic-gate 	 * Create a temporary file in the /etc/crypto directory to save
9770Sstevel@tonic-gate 	 * updated configuration file first.
9780Sstevel@tonic-gate 	 */
9790Sstevel@tonic-gate 	(void) strlcpy(tmpfile_name, TMPFILE_TEMPLATE, sizeof (tmpfile_name));
9800Sstevel@tonic-gate 	if (mkstemp(tmpfile_name) == -1) {
9810Sstevel@tonic-gate 		err = errno;
9820Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
9830Sstevel@tonic-gate 		    gettext("failed to create a temporary file - %s"),
9840Sstevel@tonic-gate 		    strerror(err));
9850Sstevel@tonic-gate 		(void) fclose(pfile);
9860Sstevel@tonic-gate 		return (FAILURE);
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	if ((pfile_tmp = fopen(tmpfile_name, "w")) == NULL) {
9900Sstevel@tonic-gate 		err = errno;
9910Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("failed to open %s - %s"),
9920Sstevel@tonic-gate 		    tmpfile_name, strerror(err));
9930Sstevel@tonic-gate 		(void) fclose(pfile);
9940Sstevel@tonic-gate 		return (FAILURE);
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	/*
9980Sstevel@tonic-gate 	 * Loop thru the entire kcf.conf file, insert, modify or delete
9990Sstevel@tonic-gate 	 * an entry.
10000Sstevel@tonic-gate 	 */
10010Sstevel@tonic-gate 	while (fgets(buffer, BUFSIZ, pfile) != NULL) {
10020Sstevel@tonic-gate 		if (add_it) {
10030Sstevel@tonic-gate 			if (fputs(buffer, pfile_tmp) == EOF) {
10040Sstevel@tonic-gate 				err = errno;
10050Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
10060Sstevel@tonic-gate 				    "failed to write to a temp file: %s."),
10070Sstevel@tonic-gate 				    strerror(err));
10080Sstevel@tonic-gate 				rc = FAILURE;
10090Sstevel@tonic-gate 				break;
10100Sstevel@tonic-gate 			}
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 		} else { /* modify or delete */
10130Sstevel@tonic-gate 			found_entry = B_FALSE;
1014*7968Sopensolaris@drydog.com 
10150Sstevel@tonic-gate 			if (!(buffer[0] == '#' || buffer[0] == ' ' ||
10160Sstevel@tonic-gate 			    buffer[0] == '\n'|| buffer[0] == '\t')) {
10170Sstevel@tonic-gate 				/*
10180Sstevel@tonic-gate 				 * Get the provider name from this line and
10190Sstevel@tonic-gate 				 * check if this is the entry to be updated
10200Sstevel@tonic-gate 				 * or deleted. Note: can not use "buffer"
10210Sstevel@tonic-gate 				 * directly because strtok will change its
10220Sstevel@tonic-gate 				 * value.
10230Sstevel@tonic-gate 				 */
10240Sstevel@tonic-gate 				(void) strlcpy(buffer2, buffer, BUFSIZ);
10250Sstevel@tonic-gate 				if ((name = strtok(buffer2, SEP_COLON)) ==
10260Sstevel@tonic-gate 				    NULL) {
10270Sstevel@tonic-gate 					rc = FAILURE;
10280Sstevel@tonic-gate 					break;
10290Sstevel@tonic-gate 				}
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 				if (strcmp(pent->name, name) == 0) {
10320Sstevel@tonic-gate 					found_entry = B_TRUE;
10330Sstevel@tonic-gate 				}
10340Sstevel@tonic-gate 			}
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 			if (found_entry && !delete_it) {
10370Sstevel@tonic-gate 				/*
10380Sstevel@tonic-gate 				 * This is the entry to be updated; get the
10390Sstevel@tonic-gate 				 * updated string and place into buffer.
10400Sstevel@tonic-gate 				 */
1041*7968Sopensolaris@drydog.com 				(void) strlcpy(buffer, new_str, BUFSIZ);
1042*7968Sopensolaris@drydog.com 				free(new_str);
10430Sstevel@tonic-gate 			}
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 			if (!(found_entry && delete_it)) {
10460Sstevel@tonic-gate 				/* This is the entry to be updated/reserved */
10470Sstevel@tonic-gate 				if (fputs(buffer, pfile_tmp) == EOF) {
10480Sstevel@tonic-gate 					err = errno;
10490Sstevel@tonic-gate 					cryptoerror(LOG_STDERR, gettext(
10500Sstevel@tonic-gate 					    "failed to write to a temp file: "
10510Sstevel@tonic-gate 					    "%s."), strerror(err));
10520Sstevel@tonic-gate 					rc = FAILURE;
10530Sstevel@tonic-gate 					break;
10540Sstevel@tonic-gate 				}
10550Sstevel@tonic-gate 			}
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
1059*7968Sopensolaris@drydog.com 	if ((!delete_it) && (rc != FAILURE)) {
1060*7968Sopensolaris@drydog.com 		if (add_it || !found_entry) {
1061*7968Sopensolaris@drydog.com 			/* append new entry to end of file */
1062*7968Sopensolaris@drydog.com 			if (fputs(new_str, pfile_tmp) == EOF) {
1063*7968Sopensolaris@drydog.com 				err = errno;
1064*7968Sopensolaris@drydog.com 				cryptoerror(LOG_STDERR, gettext(
1065*7968Sopensolaris@drydog.com 				    "failed to write to a temp file: %s."),
1066*7968Sopensolaris@drydog.com 				    strerror(err));
1067*7968Sopensolaris@drydog.com 				rc = FAILURE;
1068*7968Sopensolaris@drydog.com 			}
1069*7968Sopensolaris@drydog.com 			free(new_str);
10700Sstevel@tonic-gate 		}
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	(void) fclose(pfile);
10740Sstevel@tonic-gate 	if (fclose(pfile_tmp) != 0) {
10750Sstevel@tonic-gate 		err = errno;
10760Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
10770Sstevel@tonic-gate 		    gettext("failed to close %s: %s"), tmpfile_name,
10780Sstevel@tonic-gate 		    strerror(err));
10790Sstevel@tonic-gate 		return (FAILURE);
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	/* Copy the temporary file to the kcf.conf file */
10830Sstevel@tonic-gate 	if (rename(tmpfile_name, _PATH_KCF_CONF) == -1) {
10840Sstevel@tonic-gate 		err = errno;
10850Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
10860Sstevel@tonic-gate 		    gettext("failed to update the configuration - %s"),
10870Sstevel@tonic-gate 		    strerror(err));
10880Sstevel@tonic-gate 		cryptodebug("failed to rename %s to %s: %s", tmpfile,
10890Sstevel@tonic-gate 		    _PATH_KCF_CONF, strerror(err));
10900Sstevel@tonic-gate 		rc = FAILURE;
10910Sstevel@tonic-gate 	} else if (chmod(_PATH_KCF_CONF,
10920Sstevel@tonic-gate 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
10930Sstevel@tonic-gate 		err = errno;
10940Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
10950Sstevel@tonic-gate 		    gettext("failed to update the configuration - %s"),
10960Sstevel@tonic-gate 		    strerror(err));
10970Sstevel@tonic-gate 		cryptodebug("failed to chmod to %s: %s", _PATH_KCF_CONF,
10980Sstevel@tonic-gate 		    strerror(err));
10990Sstevel@tonic-gate 		rc = FAILURE;
11000Sstevel@tonic-gate 	} else {
11010Sstevel@tonic-gate 		rc = SUCCESS;
11020Sstevel@tonic-gate 	}
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	if ((rc == FAILURE) && (unlink(tmpfile_name) != 0)) {
11050Sstevel@tonic-gate 		err = errno;
11060Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
11070Sstevel@tonic-gate 		    "(Warning) failed to remove %s: %s"),
11080Sstevel@tonic-gate 		    tmpfile_name, strerror(err));
11090Sstevel@tonic-gate 	}
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	return (rc);
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate /*
11160Sstevel@tonic-gate  * Disable the mechanisms for the provider pointed by *ppent.  If allflag is
11170Sstevel@tonic-gate  * TRUE, disable all.  Otherwise, disable the mechanisms specified in the
11180Sstevel@tonic-gate  * dislist argument.  The "infolist" argument contains the mechanism list
11190Sstevel@tonic-gate  * supported by this provider.
11200Sstevel@tonic-gate  */
11210Sstevel@tonic-gate int
11220Sstevel@tonic-gate disable_mechs(entry_t **ppent, mechlist_t *infolist, boolean_t allflag,
11230Sstevel@tonic-gate mechlist_t *dislist)
11240Sstevel@tonic-gate {
1125*7968Sopensolaris@drydog.com 	entry_t		*pent;
1126*7968Sopensolaris@drydog.com 	mechlist_t	*plist = NULL;
1127*7968Sopensolaris@drydog.com 	mechlist_t	*phead = NULL;
1128*7968Sopensolaris@drydog.com 	mechlist_t	*pmech = NULL;
1129*7968Sopensolaris@drydog.com 	int		rc = SUCCESS;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	pent = *ppent;
11320Sstevel@tonic-gate 	if (pent == NULL) {
11330Sstevel@tonic-gate 		return (FAILURE);
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	if (allflag) {
11370Sstevel@tonic-gate 		free_mechlist(pent->dislist);
11380Sstevel@tonic-gate 		pent->dis_count = get_mech_count(infolist);
11390Sstevel@tonic-gate 		if (!(pent->dislist = dup_mechlist(infolist))) {
11400Sstevel@tonic-gate 			return (FAILURE);
11410Sstevel@tonic-gate 		} else {
11420Sstevel@tonic-gate 			return (SUCCESS);
11430Sstevel@tonic-gate 		}
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	/*
11470Sstevel@tonic-gate 	 * Not disable all. Now loop thru the mechanisms specified in the
11480Sstevel@tonic-gate 	 * dislist.  If the mechanism is not supported by the provider,
11490Sstevel@tonic-gate 	 * ignore it with a warning.  If the mechanism is disabled already,
11500Sstevel@tonic-gate 	 * do nothing. Otherwise, prepend it to the beginning of the disabled
11510Sstevel@tonic-gate 	 * list of the provider.
11520Sstevel@tonic-gate 	 */
11530Sstevel@tonic-gate 	plist = dislist;
11540Sstevel@tonic-gate 	while (plist != NULL) {
11550Sstevel@tonic-gate 		if (!is_in_list(plist->name, infolist)) {
11560Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext("(Warning) "
11570Sstevel@tonic-gate 			    "%1$s is not a valid mechanism for %2$s."),
11580Sstevel@tonic-gate 			    plist->name, pent->name);
11590Sstevel@tonic-gate 		} else if (!is_in_list(plist->name, pent->dislist)) {
11600Sstevel@tonic-gate 			/* Add this mechanism into the disabled list */
11610Sstevel@tonic-gate 			if ((pmech = create_mech(plist->name)) == NULL) {
11620Sstevel@tonic-gate 				rc = FAILURE;
11630Sstevel@tonic-gate 				break;
11640Sstevel@tonic-gate 			}
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 			if (pent->dislist == NULL) {
11670Sstevel@tonic-gate 				pent->dislist = pmech;
11680Sstevel@tonic-gate 			} else {
11690Sstevel@tonic-gate 				phead = pent->dislist;
11700Sstevel@tonic-gate 				pent->dislist = pmech;
11710Sstevel@tonic-gate 				pmech->next = phead;
11720Sstevel@tonic-gate 			}
11730Sstevel@tonic-gate 			pent->dis_count++;
11740Sstevel@tonic-gate 		}
11750Sstevel@tonic-gate 		plist = plist->next;
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	return (rc);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate  * Remove the mechanism passed, specified by mech, from the list of
11830Sstevel@tonic-gate  * mechanisms, if present in the list. Else, do nothing.
11840Sstevel@tonic-gate  *
11850Sstevel@tonic-gate  * Returns B_TRUE if mechanism is present in the list.
11860Sstevel@tonic-gate  */
11870Sstevel@tonic-gate boolean_t
11880Sstevel@tonic-gate filter_mechlist(mechlist_t **pmechlist, const char *mech)
11890Sstevel@tonic-gate {
1190*7968Sopensolaris@drydog.com 	int		cnt = 0;
1191*7968Sopensolaris@drydog.com 	mechlist_t	*ptr, *pptr;
1192*7968Sopensolaris@drydog.com 	boolean_t	mech_present = B_FALSE;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	ptr = pptr = *pmechlist;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	while (ptr != NULL) {
11970Sstevel@tonic-gate 		if (strncmp(ptr->name, mech, sizeof (mech_name_t)) == 0) {
11980Sstevel@tonic-gate 			mech_present = B_TRUE;
11990Sstevel@tonic-gate 			if (ptr == *pmechlist) {
12000Sstevel@tonic-gate 				pptr = *pmechlist = ptr->next;
12010Sstevel@tonic-gate 				free(ptr);
12020Sstevel@tonic-gate 				ptr = pptr;
12030Sstevel@tonic-gate 			} else {
12040Sstevel@tonic-gate 				pptr->next = ptr->next;
12050Sstevel@tonic-gate 				free(ptr);
12060Sstevel@tonic-gate 				ptr = pptr->next;
12070Sstevel@tonic-gate 			}
12080Sstevel@tonic-gate 		} else {
12090Sstevel@tonic-gate 			pptr = ptr;
12100Sstevel@tonic-gate 			ptr = ptr->next;
12110Sstevel@tonic-gate 			cnt++;
12120Sstevel@tonic-gate 		}
12130Sstevel@tonic-gate 	}
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	/* Only one entry is present */
12160Sstevel@tonic-gate 	if (cnt == 0)
12170Sstevel@tonic-gate 		*pmechlist = NULL;
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	return (mech_present);
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate /*
12250Sstevel@tonic-gate  * Print out the mechanism policy for a kernel provider that has an entry
12260Sstevel@tonic-gate  * in the kcf.conf file.
12270Sstevel@tonic-gate  *
12280Sstevel@tonic-gate  * The flag has_random is set to B_TRUE if the provider does random
12290Sstevel@tonic-gate  * numbers. The flag has_mechs is set by the caller to B_TRUE if the provider
12300Sstevel@tonic-gate  * has some mechanisms.
1231*7968Sopensolaris@drydog.com  *
1232*7968Sopensolaris@drydog.com  * If pent is NULL, the provider doesn't have a kcf.conf entry.
12330Sstevel@tonic-gate  */
12340Sstevel@tonic-gate void
1235*7968Sopensolaris@drydog.com print_kef_policy(char *provname, entry_t *pent, boolean_t has_random,
1236*7968Sopensolaris@drydog.com     boolean_t has_mechs)
12370Sstevel@tonic-gate {
1238*7968Sopensolaris@drydog.com 	mechlist_t	*ptr = NULL;
1239*7968Sopensolaris@drydog.com 	boolean_t	rnd_disabled = B_FALSE;
12400Sstevel@tonic-gate 
1241*7968Sopensolaris@drydog.com 	if (pent != NULL) {
1242*7968Sopensolaris@drydog.com 		rnd_disabled = filter_mechlist(&pent->dislist, RANDOM);
1243*7968Sopensolaris@drydog.com 		ptr = pent->dislist;
12440Sstevel@tonic-gate 	}
12450Sstevel@tonic-gate 
1246*7968Sopensolaris@drydog.com 	(void) printf("%s:", provname);
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	if (has_mechs == B_TRUE) {
12490Sstevel@tonic-gate 		/*
12507334SDaniel.Anderson@Sun.COM 		 * TRANSLATION_NOTE
12510Sstevel@tonic-gate 		 * This code block may need to be modified a bit to avoid
12520Sstevel@tonic-gate 		 * constructing the text message on the fly.
12530Sstevel@tonic-gate 		 */
12540Sstevel@tonic-gate 		(void) printf(gettext(" all mechanisms are enabled"));
12550Sstevel@tonic-gate 		if (ptr != NULL)
12560Sstevel@tonic-gate 			(void) printf(gettext(", except "));
12570Sstevel@tonic-gate 		while (ptr != NULL) {
12580Sstevel@tonic-gate 			(void) printf("%s", ptr->name);
12590Sstevel@tonic-gate 			ptr = ptr->next;
12600Sstevel@tonic-gate 			if (ptr != NULL)
12610Sstevel@tonic-gate 				(void) printf(",");
12620Sstevel@tonic-gate 		}
12630Sstevel@tonic-gate 		if (ptr == NULL)
12640Sstevel@tonic-gate 			(void) printf(".");
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	/*
12687334SDaniel.Anderson@Sun.COM 	 * TRANSLATION_NOTE
12690Sstevel@tonic-gate 	 * "random" is a keyword and not to be translated.
12700Sstevel@tonic-gate 	 */
12710Sstevel@tonic-gate 	if (rnd_disabled)
12720Sstevel@tonic-gate 		(void) printf(gettext(" %s is disabled."), "random");
12730Sstevel@tonic-gate 	else if (has_random)
12740Sstevel@tonic-gate 		(void) printf(gettext(" %s is enabled."), "random");
12750Sstevel@tonic-gate 	(void) printf("\n");
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
1278*7968Sopensolaris@drydog.com 
12790Sstevel@tonic-gate /*
12800Sstevel@tonic-gate  * Check if a kernel software provider is in the kernel.
1281*7968Sopensolaris@drydog.com  *
1282*7968Sopensolaris@drydog.com  * Parameters:
1283*7968Sopensolaris@drydog.com  * provname		Provider name
1284*7968Sopensolaris@drydog.com  * psoftlist_kernel	Optional software provider list.  If NULL, it will be
1285*7968Sopensolaris@drydog.com  *			obtained from get_soft_list().
1286*7968Sopensolaris@drydog.com  * in_kernel		Set to B_TRUE if device is in the kernel, else B_FALSE
12870Sstevel@tonic-gate  */
12880Sstevel@tonic-gate int
1289*7968Sopensolaris@drydog.com check_kernel_for_soft(char *provname, crypto_get_soft_list_t *psoftlist_kernel,
1290*7968Sopensolaris@drydog.com     boolean_t *in_kernel)
12910Sstevel@tonic-gate {
1292*7968Sopensolaris@drydog.com 	char		*ptr;
1293*7968Sopensolaris@drydog.com 	int		i;
1294*7968Sopensolaris@drydog.com 	boolean_t	psoftlist_allocated = B_FALSE;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	if (provname == NULL) {
12970Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("internal error."));
12980Sstevel@tonic-gate 		return (FAILURE);
12990Sstevel@tonic-gate 	}
13000Sstevel@tonic-gate 
1301*7968Sopensolaris@drydog.com 	if (psoftlist_kernel == NULL) {
1302*7968Sopensolaris@drydog.com 		if (get_soft_list(&psoftlist_kernel) == FAILURE) {
1303*7968Sopensolaris@drydog.com 			cryptodebug("failed to get the software provider list"
1304*7968Sopensolaris@drydog.com 			" from kernel.");
1305*7968Sopensolaris@drydog.com 			return (FAILURE);
1306*7968Sopensolaris@drydog.com 		}
1307*7968Sopensolaris@drydog.com 		psoftlist_allocated = B_TRUE;
13080Sstevel@tonic-gate 	}
13090Sstevel@tonic-gate 
1310*7968Sopensolaris@drydog.com 	*in_kernel = B_FALSE;
13110Sstevel@tonic-gate 	ptr = psoftlist_kernel->sl_soft_names;
13120Sstevel@tonic-gate 	for (i = 0; i < psoftlist_kernel->sl_soft_count; i++) {
13130Sstevel@tonic-gate 		if (strcmp(provname, ptr) == 0) {
1314*7968Sopensolaris@drydog.com 			*in_kernel = B_TRUE;
13150Sstevel@tonic-gate 			break;
13160Sstevel@tonic-gate 		}
13170Sstevel@tonic-gate 		ptr = ptr + strlen(ptr) + 1;
13180Sstevel@tonic-gate 	}
1319*7968Sopensolaris@drydog.com 
1320*7968Sopensolaris@drydog.com 	if (psoftlist_allocated)
1321*7968Sopensolaris@drydog.com 		free(psoftlist_kernel);
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	return (SUCCESS);
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate /*
13280Sstevel@tonic-gate  * Check if a kernel hardware provider is in the kernel.
1329*7968Sopensolaris@drydog.com  *
1330*7968Sopensolaris@drydog.com  * Parameters:
1331*7968Sopensolaris@drydog.com  * provname	Provider name
1332*7968Sopensolaris@drydog.com  * pdevlist	Optional Hardware Crypto Device List.  If NULL, it will be
1333*7968Sopensolaris@drydog.com  *		obtained from get_dev_list().
1334*7968Sopensolaris@drydog.com  * in_kernel	Set to B_TRUE if device is in the kernel, otherwise B_FALSE
13350Sstevel@tonic-gate  */
13360Sstevel@tonic-gate int
1337*7968Sopensolaris@drydog.com check_kernel_for_hard(char *provname,
1338*7968Sopensolaris@drydog.com     crypto_get_dev_list_t *pdevlist, boolean_t *in_kernel)
13390Sstevel@tonic-gate {
1340*7968Sopensolaris@drydog.com 	char		devname[MAXNAMELEN];
1341*7968Sopensolaris@drydog.com 	int		inst_num;
1342*7968Sopensolaris@drydog.com 	int		i;
1343*7968Sopensolaris@drydog.com 	boolean_t	dev_list_allocated = B_FALSE;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	if (provname == NULL) {
13460Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("internal error."));
13470Sstevel@tonic-gate 		return (FAILURE);
13480Sstevel@tonic-gate 	}
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	if (split_hw_provname(provname, devname, &inst_num) == FAILURE) {
13510Sstevel@tonic-gate 		return (FAILURE);
13520Sstevel@tonic-gate 	}
13530Sstevel@tonic-gate 
1354*7968Sopensolaris@drydog.com 	if (pdevlist == NULL) {
1355*7968Sopensolaris@drydog.com 		if (get_dev_list(&pdevlist) == FAILURE) {
1356*7968Sopensolaris@drydog.com 			cryptoerror(LOG_STDERR, gettext("internal error."));
1357*7968Sopensolaris@drydog.com 			return (FAILURE);
1358*7968Sopensolaris@drydog.com 		}
1359*7968Sopensolaris@drydog.com 		dev_list_allocated = B_TRUE;
13600Sstevel@tonic-gate 	}
13610Sstevel@tonic-gate 
1362*7968Sopensolaris@drydog.com 	*in_kernel = B_FALSE;
13630Sstevel@tonic-gate 	for (i = 0; i < pdevlist->dl_dev_count; i++) {
13640Sstevel@tonic-gate 		if ((strcmp(pdevlist->dl_devs[i].le_dev_name, devname) == 0) &&
13650Sstevel@tonic-gate 		    (pdevlist->dl_devs[i].le_dev_instance == inst_num)) {
1366*7968Sopensolaris@drydog.com 			*in_kernel = B_TRUE;
13670Sstevel@tonic-gate 			break;
13680Sstevel@tonic-gate 		}
13690Sstevel@tonic-gate 	}
1370*7968Sopensolaris@drydog.com 
1371*7968Sopensolaris@drydog.com 	if (dev_list_allocated)
1372*7968Sopensolaris@drydog.com 		free(pdevlist);
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	return (SUCCESS);
13750Sstevel@tonic-gate }
1376