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 /*
2210500SHai-May.Chao@Sun.COM  * Copyright 2009 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>
357968Sopensolaris@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 **);
457968Sopensolaris@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 {
557968Sopensolaris@drydog.com 	mechlist_t	*pres = NULL;
567968Sopensolaris@drydog.com 	mechlist_t	*pcur;
577968Sopensolaris@drydog.com 	mechlist_t	*ptmp;
587968Sopensolaris@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 
997968Sopensolaris@drydog.com /*
1007968Sopensolaris@drydog.com  * Create one item of type entry_t with the provider name.
1017968Sopensolaris@drydog.com  * Return NULL if there's not enough memory or provname is NULL.
1027968Sopensolaris@drydog.com  */
1037968Sopensolaris@drydog.com entry_t *
1047968Sopensolaris@drydog.com create_entry(char *provname)
1057968Sopensolaris@drydog.com {
1067968Sopensolaris@drydog.com 	entry_t		*pent = NULL;
1077968Sopensolaris@drydog.com 
1087968Sopensolaris@drydog.com 	if (provname == NULL) {
1097968Sopensolaris@drydog.com 		return (NULL);
1107968Sopensolaris@drydog.com 	}
1117968Sopensolaris@drydog.com 
1127968Sopensolaris@drydog.com 	pent = calloc(1, sizeof (entry_t));
1137968Sopensolaris@drydog.com 	if (pent == NULL) {
1147968Sopensolaris@drydog.com 		cryptodebug("out of memory.");
1157968Sopensolaris@drydog.com 		return (NULL);
1167968Sopensolaris@drydog.com 	}
1177968Sopensolaris@drydog.com 
1187968Sopensolaris@drydog.com 	(void) strlcpy(pent->name, provname, MAXNAMELEN);
1197968Sopensolaris@drydog.com 	pent->suplist = NULL;
1207968Sopensolaris@drydog.com 	pent->sup_count = 0;
1217968Sopensolaris@drydog.com 	pent->dislist = NULL;
1227968Sopensolaris@drydog.com 	pent->dis_count = 0;
1237968Sopensolaris@drydog.com 	pent->load = B_TRUE;
1247968Sopensolaris@drydog.com 
1257968Sopensolaris@drydog.com 	return (pent);
1267968Sopensolaris@drydog.com }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1297968Sopensolaris@drydog.com  * Duplicate an entry for a provider from kcf.conf.
1307968Sopensolaris@drydog.com  * Return NULL if memory is insufficient or the input argument is NULL.
1317968Sopensolaris@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 
1427968Sopensolaris@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;
1497968Sopensolaris@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:
1747968Sopensolaris@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
1817968Sopensolaris@drydog.com parse_sup_dis_list(char *buf, entry_t *pent)
1820Sstevel@tonic-gate {
1837968Sopensolaris@drydog.com 	mechlist_t	*pmech = NULL;
1847968Sopensolaris@drydog.com 	mechlist_t	*phead = NULL;
1857968Sopensolaris@drydog.com 	char		*next_token;
1867968Sopensolaris@drydog.com 	char		*value;
1877968Sopensolaris@drydog.com 	int		count;
1887968Sopensolaris@drydog.com 	int		supflag = B_FALSE;
1897968Sopensolaris@drydog.com 	int		disflag = B_FALSE;
1907968Sopensolaris@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 /*
2517968Sopensolaris@drydog.com  * Convert a char string containing a line about a provider
2527968Sopensolaris@drydog.com  * from kcf.conf into an entry_t structure.
2537968Sopensolaris@drydog.com  *
2547968Sopensolaris@drydog.com  * See ent2str(), the reverse of this function, for the format of
2557968Sopensolaris@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 {
2607968Sopensolaris@drydog.com 	entry_t	*pent = NULL;
2617968Sopensolaris@drydog.com 	char	*token1;
2627968Sopensolaris@drydog.com 	char	*token2;
2637968Sopensolaris@drydog.com 	char	*token3;
2647968Sopensolaris@drydog.com 	int	rc;
2650Sstevel@tonic-gate 
2667968Sopensolaris@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 
2717968Sopensolaris@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 
2837968Sopensolaris@drydog.com 	if (strncmp(token2, EF_UNLOAD, strlen(EF_UNLOAD)) == 0) {
2847968Sopensolaris@drydog.com 		pent->load = B_FALSE; /* cryptoadm unload */
2857968Sopensolaris@drydog.com 		if ((token2 = strtok(NULL, SEP_SEMICOLON)) == NULL) {
2867968Sopensolaris@drydog.com 			/* The entry contains a provider name:unload only */
2877968Sopensolaris@drydog.com 			free_entry(pent);
2887968Sopensolaris@drydog.com 			return (FAILURE);
2897968Sopensolaris@drydog.com 		}
2907968Sopensolaris@drydog.com 	}
2917968Sopensolaris@drydog.com 
2920Sstevel@tonic-gate 	/* need to get token3 first to satisfy nested strtok invocations */
2937968Sopensolaris@drydog.com 	token3 = strtok(NULL, SEP_SEMICOLON); /* optional */
2940Sstevel@tonic-gate 
2957968Sopensolaris@drydog.com 	/* parse supportedlist (or disabledlist if no supportedlist) */
2967968Sopensolaris@drydog.com 	if ((token2 != NULL) && ((rc = parse_sup_dis_list(token2, pent)) !=
2977968Sopensolaris@drydog.com 	    SUCCESS)) {
2980Sstevel@tonic-gate 		free_entry(pent);
2990Sstevel@tonic-gate 		return (rc);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
3027968Sopensolaris@drydog.com 	/* parse disabledlist (if there's a supportedlist) */
3037968Sopensolaris@drydog.com 	if ((token3 != NULL) && ((rc = parse_sup_dis_list(token3, pent)) !=
3047968Sopensolaris@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 /*
3157968Sopensolaris@drydog.com  * Add an entry about a provider from kcf.conf to the end of an entry list.
3167968Sopensolaris@drydog.com  * If the entry list pplist is NULL, create the linked list with pent as the
3177968Sopensolaris@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 {
3227968Sopensolaris@drydog.com 	entrylist_t	*pentlist;
3237968Sopensolaris@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
3497968Sopensolaris@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 
3797968Sopensolaris@drydog.com /*
3807968Sopensolaris@drydog.com  * Free memory in entry_t.
3817968Sopensolaris@drydog.com  * That is, the supported and disabled lists for a provider
3827968Sopensolaris@drydog.com  * from kcf.conf.
3837968Sopensolaris@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 
3977968Sopensolaris@drydog.com /*
3987968Sopensolaris@drydog.com  * Free elements in a entrylist_t linked list,
3997968Sopensolaris@drydog.com  * which lists providers in kcf.conf.
4007968Sopensolaris@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
4167968Sopensolaris@drydog.com  * to be inserted in the kcf.conf file.  Based on the content of each entry,
4177968Sopensolaris@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  *
4227968Sopensolaris@drydog.com  *  - name:unload;supportedlist=m1,m2,...,mj
4237968Sopensolaris@drydog.com  *  - name:unload;disabledlist=m1,m2,...,mj
4247968Sopensolaris@drydog.com  *  - name:unload;supportedlist=m1,...,mj;disabledlist=m1,m2,...,mk
4257968Sopensolaris@drydog.com  *
4267968Sopensolaris@drydog.com  * Note that the caller is responsible for freeing the returned string
4277968Sopensolaris@drydog.com  * (with free_entry()).
4287968Sopensolaris@drydog.com  * See interpret() for the reverse of this function: converting a string
4297968Sopensolaris@drydog.com  * to an entry_t.
4300Sstevel@tonic-gate  */
4310Sstevel@tonic-gate char *
4320Sstevel@tonic-gate ent2str(entry_t *pent)
4330Sstevel@tonic-gate {
4347968Sopensolaris@drydog.com 	char		*buf;
4357968Sopensolaris@drydog.com 	mechlist_t	*pcur = NULL;
4367968Sopensolaris@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 
4537968Sopensolaris@drydog.com 	if (!pent->load) { /* add "unload" keyword */
4547968Sopensolaris@drydog.com 		if (strlcat(buf, SEP_COLON, BUFSIZ) >= BUFSIZ) {
4557968Sopensolaris@drydog.com 			free(buf);
4567968Sopensolaris@drydog.com 			return (NULL);
4577968Sopensolaris@drydog.com 		}
4587968Sopensolaris@drydog.com 
4597968Sopensolaris@drydog.com 		if (strlcat(buf, EF_UNLOAD, BUFSIZ) >= BUFSIZ) {
4607968Sopensolaris@drydog.com 			free(buf);
4617968Sopensolaris@drydog.com 			return (NULL);
4627968Sopensolaris@drydog.com 		}
4637968Sopensolaris@drydog.com 
4647968Sopensolaris@drydog.com 		semicolon_separator = B_TRUE;
4657968Sopensolaris@drydog.com 	}
4667968Sopensolaris@drydog.com 
4670Sstevel@tonic-gate 	/* convert the supported list if any */
4687968Sopensolaris@drydog.com 	pcur = pent->suplist;
4697968Sopensolaris@drydog.com 	if (pcur != NULL) {
4707968Sopensolaris@drydog.com 		if (strlcat(buf,
4717968Sopensolaris@drydog.com 		    semicolon_separator ? SEP_SEMICOLON : SEP_COLON,
4727968Sopensolaris@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 
4827968Sopensolaris@drydog.com 		while (pcur != NULL) {
4837968Sopensolaris@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 
4887968Sopensolaris@drydog.com 			pcur = pcur->next;
4897968Sopensolaris@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 		}
4977968Sopensolaris@drydog.com 		semicolon_separator = B_TRUE;
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/* convert the disabled list if any */
5017968Sopensolaris@drydog.com 	pcur = pent->dislist;
5027968Sopensolaris@drydog.com 	if (pcur != NULL) {
5037968Sopensolaris@drydog.com 		if (strlcat(buf,
5047968Sopensolaris@drydog.com 		    semicolon_separator ? SEP_SEMICOLON : SEP_COLON,
5057968Sopensolaris@drydog.com 		    BUFSIZ) >= BUFSIZ) {
5067968Sopensolaris@drydog.com 			free(buf);
5077968Sopensolaris@drydog.com 			return (NULL);
5080Sstevel@tonic-gate 		}
5090Sstevel@tonic-gate 
5107968Sopensolaris@drydog.com 		if (strlcat(buf, EF_DISABLED, BUFSIZ) >= BUFSIZ) {
5117968Sopensolaris@drydog.com 			free(buf);
5127968Sopensolaris@drydog.com 			return (NULL);
5137968Sopensolaris@drydog.com 		}
5147968Sopensolaris@drydog.com 
5157968Sopensolaris@drydog.com 		while (pcur != NULL) {
5167968Sopensolaris@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 
5217968Sopensolaris@drydog.com 			pcur = pcur->next;
5227968Sopensolaris@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 		}
5307968Sopensolaris@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 {
5507968Sopensolaris@drydog.com 	entry_t		*pent;
5517968Sopensolaris@drydog.com 	mechlist_t	*phead; /* the current and resulting disabled list */
5527968Sopensolaris@drydog.com 	mechlist_t	*ptr = NULL;
5537968Sopensolaris@drydog.com 	mechlist_t	*pcur = NULL;
5547968Sopensolaris@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
5727968Sopensolaris@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 
6147968Sopensolaris@drydog.com /*
6157968Sopensolaris@drydog.com  * Determine if the kernel provider name, path, is a device
6167968Sopensolaris@drydog.com  * (that is, it contains a slash character (e.g., "mca/0").
6177968Sopensolaris@drydog.com  * If so, it is a hardware provider; otherwise it is a software provider.
6187968Sopensolaris@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
6317968Sopensolaris@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 /*
6607968Sopensolaris@drydog.com  * Retrieve information from kcf.conf and build a hardware device entry list
6617968Sopensolaris@drydog.com  * and a software entry list of kernel crypto providers.
6627968Sopensolaris@drydog.com  *
6637968Sopensolaris@drydog.com  * This list is usually incomplete, as kernel crypto providers only have to
6647968Sopensolaris@drydog.com  * be listed in kcf.conf if a mechanism is disabled (by cryptoadm) or
6657968Sopensolaris@drydog.com  * if the kernel provider module is not one of the default kernel providers.
6667968Sopensolaris@drydog.com  *
6677968Sopensolaris@drydog.com  * The kcf.conf file is available only in the global zone.
6680Sstevel@tonic-gate  */
6690Sstevel@tonic-gate int
670*10979SHai-May.Chao@Sun.COM get_kcfconf_info(entrylist_t **ppdevlist, entrylist_t **ppsoftlist)
6710Sstevel@tonic-gate {
6727968Sopensolaris@drydog.com 	FILE	*pfile = NULL;
6737968Sopensolaris@drydog.com 	char	buffer[BUFSIZ];
6747968Sopensolaris@drydog.com 	int	len;
6757968Sopensolaris@drydog.com 	entry_t	*pent = NULL;
6767968Sopensolaris@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);
6927968Sopensolaris@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) {
698*10979SHai-May.Chao@Sun.COM 			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
7227968Sopensolaris@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 {
7287968Sopensolaris@drydog.com 	crypto_get_dev_list_t	*pdevlist_kernel = NULL;
7297968Sopensolaris@drydog.com 	crypto_get_soft_list_t	*psoftlist_kernel = NULL;
7307968Sopensolaris@drydog.com 	char			*devname;
7317968Sopensolaris@drydog.com 	int			inst_num;
7327968Sopensolaris@drydog.com 	int			mcount;
7337968Sopensolaris@drydog.com 	mechlist_t		*pmech = NULL;
7347968Sopensolaris@drydog.com 	entry_t			*pent_dev = NULL, *pent_soft = NULL;
7357968Sopensolaris@drydog.com 	int			i;
7367968Sopensolaris@drydog.com 	char			*psoftname;
7377968Sopensolaris@drydog.com 	entrylist_t		*tmp_pdev = NULL;
7387968Sopensolaris@drydog.com 	entrylist_t		*tmp_psoft = NULL;
7397968Sopensolaris@drydog.com 	entrylist_t		*phardlist = NULL, *psoftlist = NULL;
7400Sstevel@tonic-gate 
7417968Sopensolaris@drydog.com 	/*
7427968Sopensolaris@drydog.com 	 * Get hardware providers
7437968Sopensolaris@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 
7637968Sopensolaris@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 		}
7687968Sopensolaris@drydog.com 		pent_dev->suplist = pmech;
7697968Sopensolaris@drydog.com 		pent_dev->sup_count = mcount;
7700Sstevel@tonic-gate 
7717968Sopensolaris@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 
7797968Sopensolaris@drydog.com 	/*
7807968Sopensolaris@drydog.com 	 * Get software providers
7817968Sopensolaris@drydog.com 	 */
7827968Sopensolaris@drydog.com 	if (getzoneid() == GLOBAL_ZONEID) {
783*10979SHai-May.Chao@Sun.COM 		if (get_kcfconf_info(&phardlist, &psoftlist) != SUCCESS) {
7847968Sopensolaris@drydog.com 			goto fail_out;
7857968Sopensolaris@drydog.com 		}
7867968Sopensolaris@drydog.com 	}
7877968Sopensolaris@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*10979SHai-May.Chao@Sun.COM 		if (get_soft_info(psoftname, &pmech, phardlist, psoftlist) !=
798*10979SHai-May.Chao@Sun.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 
8057968Sopensolaris@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 		}
8107968Sopensolaris@drydog.com 		pent_soft->suplist = pmech;
8117968Sopensolaris@drydog.com 		pent_soft->sup_count = get_mech_count(pmech);
8120Sstevel@tonic-gate 
8137968Sopensolaris@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:
8277968Sopensolaris@drydog.com 	if (pent_dev != NULL)
8287968Sopensolaris@drydog.com 		free_entry(pent_dev);
8297968Sopensolaris@drydog.com 	if (pent_soft != NULL)
8307968Sopensolaris@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 /*
8447968Sopensolaris@drydog.com  * Return configuration information for a kernel provider from kcf.conf.
8457968Sopensolaris@drydog.com  * For kernel software providers return a enabled list and disabled list.
8467968Sopensolaris@drydog.com  * For kernel hardware providers return just a disabled list.
8477968Sopensolaris@drydog.com  *
8487968Sopensolaris@drydog.com  * Parameters phardlist and psoftlist are supplied by get_kcfconf_info().
8497968Sopensolaris@drydog.com  * If NULL, this function calls get_kcfconf_info() internally.
8500Sstevel@tonic-gate  */
8510Sstevel@tonic-gate entry_t *
852*10979SHai-May.Chao@Sun.COM getent_kef(char *provname, entrylist_t *phardlist, entrylist_t *psoftlist)
8530Sstevel@tonic-gate {
8547968Sopensolaris@drydog.com 	entry_t		*pent = NULL;
8557968Sopensolaris@drydog.com 	boolean_t	memory_allocated = B_FALSE;
8560Sstevel@tonic-gate 
857*10979SHai-May.Chao@Sun.COM 	if ((phardlist == NULL) || (psoftlist == NULL)) {
858*10979SHai-May.Chao@Sun.COM 		if (get_kcfconf_info(&phardlist, &psoftlist) != SUCCESS) {
8597968Sopensolaris@drydog.com 			return (NULL);
8607968Sopensolaris@drydog.com 		}
8617968Sopensolaris@drydog.com 		memory_allocated = B_TRUE;
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	if (is_device(provname)) {
8657968Sopensolaris@drydog.com 		pent = getent(provname, phardlist);
8660Sstevel@tonic-gate 	} else {
8670Sstevel@tonic-gate 		pent = getent(provname, psoftlist);
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
8707968Sopensolaris@drydog.com 	if (memory_allocated) {
8717968Sopensolaris@drydog.com 		free_entrylist(phardlist);
8727968Sopensolaris@drydog.com 		free_entrylist(psoftlist);
8737968Sopensolaris@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 {
8847968Sopensolaris@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 /*
9107968Sopensolaris@drydog.com  * Update the kcf.conf file based on the update mode:
9117968Sopensolaris@drydog.com  * - If update_mode is MODIFY_MODE, modify the entry with the same name.
9127968Sopensolaris@drydog.com  *   If not found, append a new entry to the kcf.conf file.
9137968Sopensolaris@drydog.com  * - If update_mode is DELETE_MODE, delete the entry with the same name.
9147968Sopensolaris@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;
9218273Sopensolaris@drydog.com 	boolean_t	this_entry_matches = B_FALSE;
9220Sstevel@tonic-gate 	boolean_t	found_entry = B_FALSE;
9237968Sopensolaris@drydog.com 	FILE		*pfile = NULL;
9247968Sopensolaris@drydog.com 	FILE		*pfile_tmp = NULL;
9257968Sopensolaris@drydog.com 	char		buffer[BUFSIZ];
9267968Sopensolaris@drydog.com 	char		buffer2[BUFSIZ];
9277968Sopensolaris@drydog.com 	char		tmpfile_name[MAXPATHLEN];
9287968Sopensolaris@drydog.com 	char		*name;
9297968Sopensolaris@drydog.com 	char		*new_str = NULL;
9307968Sopensolaris@drydog.com 	int		rc = SUCCESS;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	if (pent == NULL) {
9330Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("internal error."));
9340Sstevel@tonic-gate 		return (FAILURE);
9350Sstevel@tonic-gate 	}
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	/* Check the update_mode */
9387968Sopensolaris@drydog.com 	switch (update_mode) {
9397968Sopensolaris@drydog.com 	case ADD_MODE:
9400Sstevel@tonic-gate 		add_it = B_TRUE;
9417968Sopensolaris@drydog.com 		/* FALLTHROUGH */
9427968Sopensolaris@drydog.com 	case MODIFY_MODE:
9437968Sopensolaris@drydog.com 		/* Convert the entry a string to add to kcf.conf  */
9440Sstevel@tonic-gate 		if ((new_str = ent2str(pent)) == NULL) {
9450Sstevel@tonic-gate 			return (FAILURE);
9460Sstevel@tonic-gate 		}
9477968Sopensolaris@drydog.com 		break;
9487968Sopensolaris@drydog.com 	case DELETE_MODE:
9490Sstevel@tonic-gate 		delete_it = B_TRUE;
9507968Sopensolaris@drydog.com 		break;
9517968Sopensolaris@drydog.com 	default:
9520Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("internal error."));
9530Sstevel@tonic-gate 		return (FAILURE);
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	/* Open the kcf.conf file */
9570Sstevel@tonic-gate 	if ((pfile = fopen(_PATH_KCF_CONF, "r+")) == NULL) {
9580Sstevel@tonic-gate 		err = errno;
9590Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
9600Sstevel@tonic-gate 		    gettext("failed to update the configuration - %s"),
9610Sstevel@tonic-gate 		    strerror(err));
9620Sstevel@tonic-gate 		cryptodebug("failed to open %s for write.", _PATH_KCF_CONF);
9630Sstevel@tonic-gate 		return (FAILURE);
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/* Lock the kcf.conf file */
9670Sstevel@tonic-gate 	if (lockf(fileno(pfile), F_TLOCK, 0) == -1) {
9680Sstevel@tonic-gate 		err = errno;
9690Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
9700Sstevel@tonic-gate 		    gettext("failed to update the configuration - %s"),
9717968Sopensolaris@drydog.com 		    strerror(err));
9720Sstevel@tonic-gate 		(void) fclose(pfile);
9730Sstevel@tonic-gate 		return (FAILURE);
9740Sstevel@tonic-gate 	}
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	/*
9770Sstevel@tonic-gate 	 * Create a temporary file in the /etc/crypto directory to save
9780Sstevel@tonic-gate 	 * updated configuration file first.
9790Sstevel@tonic-gate 	 */
9800Sstevel@tonic-gate 	(void) strlcpy(tmpfile_name, TMPFILE_TEMPLATE, sizeof (tmpfile_name));
9810Sstevel@tonic-gate 	if (mkstemp(tmpfile_name) == -1) {
9820Sstevel@tonic-gate 		err = errno;
9830Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
9840Sstevel@tonic-gate 		    gettext("failed to create a temporary file - %s"),
9850Sstevel@tonic-gate 		    strerror(err));
9860Sstevel@tonic-gate 		(void) fclose(pfile);
9870Sstevel@tonic-gate 		return (FAILURE);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	if ((pfile_tmp = fopen(tmpfile_name, "w")) == NULL) {
9910Sstevel@tonic-gate 		err = errno;
9920Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("failed to open %s - %s"),
9930Sstevel@tonic-gate 		    tmpfile_name, strerror(err));
9940Sstevel@tonic-gate 		(void) fclose(pfile);
9950Sstevel@tonic-gate 		return (FAILURE);
9960Sstevel@tonic-gate 	}
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	/*
9990Sstevel@tonic-gate 	 * Loop thru the entire kcf.conf file, insert, modify or delete
10000Sstevel@tonic-gate 	 * an entry.
10010Sstevel@tonic-gate 	 */
10020Sstevel@tonic-gate 	while (fgets(buffer, BUFSIZ, pfile) != NULL) {
10030Sstevel@tonic-gate 		if (add_it) {
10040Sstevel@tonic-gate 			if (fputs(buffer, pfile_tmp) == EOF) {
10050Sstevel@tonic-gate 				err = errno;
10060Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
10070Sstevel@tonic-gate 				    "failed to write to a temp file: %s."),
10080Sstevel@tonic-gate 				    strerror(err));
10090Sstevel@tonic-gate 				rc = FAILURE;
10100Sstevel@tonic-gate 				break;
10110Sstevel@tonic-gate 			}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 		} else { /* modify or delete */
10148273Sopensolaris@drydog.com 			this_entry_matches = B_FALSE;
10157968Sopensolaris@drydog.com 
10160Sstevel@tonic-gate 			if (!(buffer[0] == '#' || buffer[0] == ' ' ||
10170Sstevel@tonic-gate 			    buffer[0] == '\n'|| buffer[0] == '\t')) {
10180Sstevel@tonic-gate 				/*
10190Sstevel@tonic-gate 				 * Get the provider name from this line and
10200Sstevel@tonic-gate 				 * check if this is the entry to be updated
10210Sstevel@tonic-gate 				 * or deleted. Note: can not use "buffer"
10220Sstevel@tonic-gate 				 * directly because strtok will change its
10230Sstevel@tonic-gate 				 * value.
10240Sstevel@tonic-gate 				 */
10250Sstevel@tonic-gate 				(void) strlcpy(buffer2, buffer, BUFSIZ);
10260Sstevel@tonic-gate 				if ((name = strtok(buffer2, SEP_COLON)) ==
10270Sstevel@tonic-gate 				    NULL) {
10280Sstevel@tonic-gate 					rc = FAILURE;
10290Sstevel@tonic-gate 					break;
10300Sstevel@tonic-gate 				}
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 				if (strcmp(pent->name, name) == 0) {
10338273Sopensolaris@drydog.com 					this_entry_matches = B_TRUE;
10340Sstevel@tonic-gate 					found_entry = B_TRUE;
10350Sstevel@tonic-gate 				}
10360Sstevel@tonic-gate 			}
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 
10398273Sopensolaris@drydog.com 			if (!this_entry_matches || !delete_it) {
10408273Sopensolaris@drydog.com 				/* write this entry */
10418273Sopensolaris@drydog.com 				if (this_entry_matches) {
10428273Sopensolaris@drydog.com 					/*
10438273Sopensolaris@drydog.com 					 * Modify this entry: get the
10448273Sopensolaris@drydog.com 					 * updated string and place into buffer.
10458273Sopensolaris@drydog.com 					 */
10468273Sopensolaris@drydog.com 					(void) strlcpy(buffer, new_str, BUFSIZ);
10478273Sopensolaris@drydog.com 					free(new_str);
10488273Sopensolaris@drydog.com 				}
10498273Sopensolaris@drydog.com 				/* write the (unchanged or modified) entry */
10500Sstevel@tonic-gate 				if (fputs(buffer, pfile_tmp) == EOF) {
10510Sstevel@tonic-gate 					err = errno;
10520Sstevel@tonic-gate 					cryptoerror(LOG_STDERR, gettext(
10530Sstevel@tonic-gate 					    "failed to write to a temp file: "
10540Sstevel@tonic-gate 					    "%s."), strerror(err));
10550Sstevel@tonic-gate 					rc = FAILURE;
10560Sstevel@tonic-gate 					break;
10570Sstevel@tonic-gate 				}
10580Sstevel@tonic-gate 			}
10590Sstevel@tonic-gate 		}
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 
10627968Sopensolaris@drydog.com 	if ((!delete_it) && (rc != FAILURE)) {
10637968Sopensolaris@drydog.com 		if (add_it || !found_entry) {
10647968Sopensolaris@drydog.com 			/* append new entry to end of file */
10657968Sopensolaris@drydog.com 			if (fputs(new_str, pfile_tmp) == EOF) {
10667968Sopensolaris@drydog.com 				err = errno;
10677968Sopensolaris@drydog.com 				cryptoerror(LOG_STDERR, gettext(
10687968Sopensolaris@drydog.com 				    "failed to write to a temp file: %s."),
10697968Sopensolaris@drydog.com 				    strerror(err));
10707968Sopensolaris@drydog.com 				rc = FAILURE;
10717968Sopensolaris@drydog.com 			}
10727968Sopensolaris@drydog.com 			free(new_str);
10730Sstevel@tonic-gate 		}
10740Sstevel@tonic-gate 	}
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	(void) fclose(pfile);
10770Sstevel@tonic-gate 	if (fclose(pfile_tmp) != 0) {
10780Sstevel@tonic-gate 		err = errno;
10790Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
10800Sstevel@tonic-gate 		    gettext("failed to close %s: %s"), tmpfile_name,
10810Sstevel@tonic-gate 		    strerror(err));
10820Sstevel@tonic-gate 		return (FAILURE);
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	/* Copy the temporary file to the kcf.conf file */
10860Sstevel@tonic-gate 	if (rename(tmpfile_name, _PATH_KCF_CONF) == -1) {
10870Sstevel@tonic-gate 		err = errno;
10880Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
10890Sstevel@tonic-gate 		    gettext("failed to update the configuration - %s"),
10900Sstevel@tonic-gate 		    strerror(err));
10910Sstevel@tonic-gate 		cryptodebug("failed to rename %s to %s: %s", tmpfile,
10920Sstevel@tonic-gate 		    _PATH_KCF_CONF, strerror(err));
10930Sstevel@tonic-gate 		rc = FAILURE;
10940Sstevel@tonic-gate 	} else if (chmod(_PATH_KCF_CONF,
10950Sstevel@tonic-gate 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
10960Sstevel@tonic-gate 		err = errno;
10970Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
10980Sstevel@tonic-gate 		    gettext("failed to update the configuration - %s"),
10990Sstevel@tonic-gate 		    strerror(err));
11000Sstevel@tonic-gate 		cryptodebug("failed to chmod to %s: %s", _PATH_KCF_CONF,
11010Sstevel@tonic-gate 		    strerror(err));
11020Sstevel@tonic-gate 		rc = FAILURE;
11030Sstevel@tonic-gate 	} else {
11040Sstevel@tonic-gate 		rc = SUCCESS;
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	if ((rc == FAILURE) && (unlink(tmpfile_name) != 0)) {
11080Sstevel@tonic-gate 		err = errno;
11090Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
11100Sstevel@tonic-gate 		    "(Warning) failed to remove %s: %s"),
11110Sstevel@tonic-gate 		    tmpfile_name, strerror(err));
11120Sstevel@tonic-gate 	}
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	return (rc);
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate /*
11190Sstevel@tonic-gate  * Disable the mechanisms for the provider pointed by *ppent.  If allflag is
11200Sstevel@tonic-gate  * TRUE, disable all.  Otherwise, disable the mechanisms specified in the
11210Sstevel@tonic-gate  * dislist argument.  The "infolist" argument contains the mechanism list
11220Sstevel@tonic-gate  * supported by this provider.
11230Sstevel@tonic-gate  */
11240Sstevel@tonic-gate int
11250Sstevel@tonic-gate disable_mechs(entry_t **ppent, mechlist_t *infolist, boolean_t allflag,
11260Sstevel@tonic-gate mechlist_t *dislist)
11270Sstevel@tonic-gate {
11287968Sopensolaris@drydog.com 	entry_t		*pent;
11297968Sopensolaris@drydog.com 	mechlist_t	*plist = NULL;
11307968Sopensolaris@drydog.com 	mechlist_t	*phead = NULL;
11317968Sopensolaris@drydog.com 	mechlist_t	*pmech = NULL;
11327968Sopensolaris@drydog.com 	int		rc = SUCCESS;
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	pent = *ppent;
11350Sstevel@tonic-gate 	if (pent == NULL) {
11360Sstevel@tonic-gate 		return (FAILURE);
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	if (allflag) {
11400Sstevel@tonic-gate 		free_mechlist(pent->dislist);
11410Sstevel@tonic-gate 		pent->dis_count = get_mech_count(infolist);
11420Sstevel@tonic-gate 		if (!(pent->dislist = dup_mechlist(infolist))) {
11430Sstevel@tonic-gate 			return (FAILURE);
11440Sstevel@tonic-gate 		} else {
11450Sstevel@tonic-gate 			return (SUCCESS);
11460Sstevel@tonic-gate 		}
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	/*
11500Sstevel@tonic-gate 	 * Not disable all. Now loop thru the mechanisms specified in the
11510Sstevel@tonic-gate 	 * dislist.  If the mechanism is not supported by the provider,
11520Sstevel@tonic-gate 	 * ignore it with a warning.  If the mechanism is disabled already,
11530Sstevel@tonic-gate 	 * do nothing. Otherwise, prepend it to the beginning of the disabled
11540Sstevel@tonic-gate 	 * list of the provider.
11550Sstevel@tonic-gate 	 */
11560Sstevel@tonic-gate 	plist = dislist;
11570Sstevel@tonic-gate 	while (plist != NULL) {
11580Sstevel@tonic-gate 		if (!is_in_list(plist->name, infolist)) {
11590Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext("(Warning) "
11600Sstevel@tonic-gate 			    "%1$s is not a valid mechanism for %2$s."),
11610Sstevel@tonic-gate 			    plist->name, pent->name);
11620Sstevel@tonic-gate 		} else if (!is_in_list(plist->name, pent->dislist)) {
11630Sstevel@tonic-gate 			/* Add this mechanism into the disabled list */
11640Sstevel@tonic-gate 			if ((pmech = create_mech(plist->name)) == NULL) {
11650Sstevel@tonic-gate 				rc = FAILURE;
11660Sstevel@tonic-gate 				break;
11670Sstevel@tonic-gate 			}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 			if (pent->dislist == NULL) {
11700Sstevel@tonic-gate 				pent->dislist = pmech;
11710Sstevel@tonic-gate 			} else {
11720Sstevel@tonic-gate 				phead = pent->dislist;
11730Sstevel@tonic-gate 				pent->dislist = pmech;
11740Sstevel@tonic-gate 				pmech->next = phead;
11750Sstevel@tonic-gate 			}
11760Sstevel@tonic-gate 			pent->dis_count++;
11770Sstevel@tonic-gate 		}
11780Sstevel@tonic-gate 		plist = plist->next;
11790Sstevel@tonic-gate 	}
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	return (rc);
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate /*
11850Sstevel@tonic-gate  * Remove the mechanism passed, specified by mech, from the list of
11860Sstevel@tonic-gate  * mechanisms, if present in the list. Else, do nothing.
11870Sstevel@tonic-gate  *
11880Sstevel@tonic-gate  * Returns B_TRUE if mechanism is present in the list.
11890Sstevel@tonic-gate  */
11900Sstevel@tonic-gate boolean_t
11910Sstevel@tonic-gate filter_mechlist(mechlist_t **pmechlist, const char *mech)
11920Sstevel@tonic-gate {
11937968Sopensolaris@drydog.com 	int		cnt = 0;
11947968Sopensolaris@drydog.com 	mechlist_t	*ptr, *pptr;
11957968Sopensolaris@drydog.com 	boolean_t	mech_present = B_FALSE;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	ptr = pptr = *pmechlist;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	while (ptr != NULL) {
12000Sstevel@tonic-gate 		if (strncmp(ptr->name, mech, sizeof (mech_name_t)) == 0) {
12010Sstevel@tonic-gate 			mech_present = B_TRUE;
12020Sstevel@tonic-gate 			if (ptr == *pmechlist) {
12030Sstevel@tonic-gate 				pptr = *pmechlist = ptr->next;
12040Sstevel@tonic-gate 				free(ptr);
12050Sstevel@tonic-gate 				ptr = pptr;
12060Sstevel@tonic-gate 			} else {
12070Sstevel@tonic-gate 				pptr->next = ptr->next;
12080Sstevel@tonic-gate 				free(ptr);
12090Sstevel@tonic-gate 				ptr = pptr->next;
12100Sstevel@tonic-gate 			}
12110Sstevel@tonic-gate 		} else {
12120Sstevel@tonic-gate 			pptr = ptr;
12130Sstevel@tonic-gate 			ptr = ptr->next;
12140Sstevel@tonic-gate 			cnt++;
12150Sstevel@tonic-gate 		}
12160Sstevel@tonic-gate 	}
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	/* Only one entry is present */
12190Sstevel@tonic-gate 	if (cnt == 0)
12200Sstevel@tonic-gate 		*pmechlist = NULL;
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	return (mech_present);
12230Sstevel@tonic-gate }
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate  * Print out the mechanism policy for a kernel provider that has an entry
12290Sstevel@tonic-gate  * in the kcf.conf file.
12300Sstevel@tonic-gate  *
12310Sstevel@tonic-gate  * The flag has_random is set to B_TRUE if the provider does random
12320Sstevel@tonic-gate  * numbers. The flag has_mechs is set by the caller to B_TRUE if the provider
12330Sstevel@tonic-gate  * has some mechanisms.
12347968Sopensolaris@drydog.com  *
12357968Sopensolaris@drydog.com  * If pent is NULL, the provider doesn't have a kcf.conf entry.
12360Sstevel@tonic-gate  */
12370Sstevel@tonic-gate void
12387968Sopensolaris@drydog.com print_kef_policy(char *provname, entry_t *pent, boolean_t has_random,
12397968Sopensolaris@drydog.com     boolean_t has_mechs)
12400Sstevel@tonic-gate {
12417968Sopensolaris@drydog.com 	mechlist_t	*ptr = NULL;
12427968Sopensolaris@drydog.com 	boolean_t	rnd_disabled = B_FALSE;
12430Sstevel@tonic-gate 
12447968Sopensolaris@drydog.com 	if (pent != NULL) {
12457968Sopensolaris@drydog.com 		rnd_disabled = filter_mechlist(&pent->dislist, RANDOM);
12467968Sopensolaris@drydog.com 		ptr = pent->dislist;
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate 
12497968Sopensolaris@drydog.com 	(void) printf("%s:", provname);
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	if (has_mechs == B_TRUE) {
12520Sstevel@tonic-gate 		/*
12537334SDaniel.Anderson@Sun.COM 		 * TRANSLATION_NOTE
12540Sstevel@tonic-gate 		 * This code block may need to be modified a bit to avoid
12550Sstevel@tonic-gate 		 * constructing the text message on the fly.
12560Sstevel@tonic-gate 		 */
12570Sstevel@tonic-gate 		(void) printf(gettext(" all mechanisms are enabled"));
12580Sstevel@tonic-gate 		if (ptr != NULL)
12590Sstevel@tonic-gate 			(void) printf(gettext(", except "));
12600Sstevel@tonic-gate 		while (ptr != NULL) {
12610Sstevel@tonic-gate 			(void) printf("%s", ptr->name);
12620Sstevel@tonic-gate 			ptr = ptr->next;
12630Sstevel@tonic-gate 			if (ptr != NULL)
12640Sstevel@tonic-gate 				(void) printf(",");
12650Sstevel@tonic-gate 		}
12660Sstevel@tonic-gate 		if (ptr == NULL)
12670Sstevel@tonic-gate 			(void) printf(".");
12680Sstevel@tonic-gate 	}
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	/*
12717334SDaniel.Anderson@Sun.COM 	 * TRANSLATION_NOTE
12720Sstevel@tonic-gate 	 * "random" is a keyword and not to be translated.
12730Sstevel@tonic-gate 	 */
12740Sstevel@tonic-gate 	if (rnd_disabled)
12750Sstevel@tonic-gate 		(void) printf(gettext(" %s is disabled."), "random");
12760Sstevel@tonic-gate 	else if (has_random)
12770Sstevel@tonic-gate 		(void) printf(gettext(" %s is enabled."), "random");
12780Sstevel@tonic-gate 	(void) printf("\n");
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12817968Sopensolaris@drydog.com 
12820Sstevel@tonic-gate /*
12830Sstevel@tonic-gate  * Check if a kernel software provider is in the kernel.
12847968Sopensolaris@drydog.com  *
12857968Sopensolaris@drydog.com  * Parameters:
12867968Sopensolaris@drydog.com  * provname		Provider name
12877968Sopensolaris@drydog.com  * psoftlist_kernel	Optional software provider list.  If NULL, it will be
12887968Sopensolaris@drydog.com  *			obtained from get_soft_list().
12897968Sopensolaris@drydog.com  * in_kernel		Set to B_TRUE if device is in the kernel, else B_FALSE
12900Sstevel@tonic-gate  */
12910Sstevel@tonic-gate int
12927968Sopensolaris@drydog.com check_kernel_for_soft(char *provname, crypto_get_soft_list_t *psoftlist_kernel,
12937968Sopensolaris@drydog.com     boolean_t *in_kernel)
12940Sstevel@tonic-gate {
12957968Sopensolaris@drydog.com 	char		*ptr;
12967968Sopensolaris@drydog.com 	int		i;
12977968Sopensolaris@drydog.com 	boolean_t	psoftlist_allocated = B_FALSE;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	if (provname == NULL) {
13000Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("internal error."));
13010Sstevel@tonic-gate 		return (FAILURE);
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate 
13047968Sopensolaris@drydog.com 	if (psoftlist_kernel == NULL) {
13057968Sopensolaris@drydog.com 		if (get_soft_list(&psoftlist_kernel) == FAILURE) {
13067968Sopensolaris@drydog.com 			cryptodebug("failed to get the software provider list"
13077968Sopensolaris@drydog.com 			" from kernel.");
13087968Sopensolaris@drydog.com 			return (FAILURE);
13097968Sopensolaris@drydog.com 		}
13107968Sopensolaris@drydog.com 		psoftlist_allocated = B_TRUE;
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 
13137968Sopensolaris@drydog.com 	*in_kernel = B_FALSE;
13140Sstevel@tonic-gate 	ptr = psoftlist_kernel->sl_soft_names;
13150Sstevel@tonic-gate 	for (i = 0; i < psoftlist_kernel->sl_soft_count; i++) {
13160Sstevel@tonic-gate 		if (strcmp(provname, ptr) == 0) {
13177968Sopensolaris@drydog.com 			*in_kernel = B_TRUE;
13180Sstevel@tonic-gate 			break;
13190Sstevel@tonic-gate 		}
13200Sstevel@tonic-gate 		ptr = ptr + strlen(ptr) + 1;
13210Sstevel@tonic-gate 	}
13227968Sopensolaris@drydog.com 
13237968Sopensolaris@drydog.com 	if (psoftlist_allocated)
13247968Sopensolaris@drydog.com 		free(psoftlist_kernel);
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	return (SUCCESS);
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate /*
13310Sstevel@tonic-gate  * Check if a kernel hardware provider is in the kernel.
13327968Sopensolaris@drydog.com  *
13337968Sopensolaris@drydog.com  * Parameters:
13347968Sopensolaris@drydog.com  * provname	Provider name
13357968Sopensolaris@drydog.com  * pdevlist	Optional Hardware Crypto Device List.  If NULL, it will be
13367968Sopensolaris@drydog.com  *		obtained from get_dev_list().
13377968Sopensolaris@drydog.com  * in_kernel	Set to B_TRUE if device is in the kernel, otherwise B_FALSE
13380Sstevel@tonic-gate  */
13390Sstevel@tonic-gate int
13407968Sopensolaris@drydog.com check_kernel_for_hard(char *provname,
13417968Sopensolaris@drydog.com     crypto_get_dev_list_t *pdevlist, boolean_t *in_kernel)
13420Sstevel@tonic-gate {
13437968Sopensolaris@drydog.com 	char		devname[MAXNAMELEN];
13447968Sopensolaris@drydog.com 	int		inst_num;
13457968Sopensolaris@drydog.com 	int		i;
13467968Sopensolaris@drydog.com 	boolean_t	dev_list_allocated = B_FALSE;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	if (provname == NULL) {
13490Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("internal error."));
13500Sstevel@tonic-gate 		return (FAILURE);
13510Sstevel@tonic-gate 	}
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	if (split_hw_provname(provname, devname, &inst_num) == FAILURE) {
13540Sstevel@tonic-gate 		return (FAILURE);
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 
13577968Sopensolaris@drydog.com 	if (pdevlist == NULL) {
13587968Sopensolaris@drydog.com 		if (get_dev_list(&pdevlist) == FAILURE) {
13597968Sopensolaris@drydog.com 			cryptoerror(LOG_STDERR, gettext("internal error."));
13607968Sopensolaris@drydog.com 			return (FAILURE);
13617968Sopensolaris@drydog.com 		}
13627968Sopensolaris@drydog.com 		dev_list_allocated = B_TRUE;
13630Sstevel@tonic-gate 	}
13640Sstevel@tonic-gate 
13657968Sopensolaris@drydog.com 	*in_kernel = B_FALSE;
13660Sstevel@tonic-gate 	for (i = 0; i < pdevlist->dl_dev_count; i++) {
13670Sstevel@tonic-gate 		if ((strcmp(pdevlist->dl_devs[i].le_dev_name, devname) == 0) &&
13680Sstevel@tonic-gate 		    (pdevlist->dl_devs[i].le_dev_instance == inst_num)) {
13697968Sopensolaris@drydog.com 			*in_kernel = B_TRUE;
13700Sstevel@tonic-gate 			break;
13710Sstevel@tonic-gate 		}
13720Sstevel@tonic-gate 	}
13737968Sopensolaris@drydog.com 
13747968Sopensolaris@drydog.com 	if (dev_list_allocated)
13757968Sopensolaris@drydog.com 		free(pdevlist);
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	return (SUCCESS);
13780Sstevel@tonic-gate }
1379