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
5*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * 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 /*
22*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <nss_dbdefs.h>
330Sstevel@tonic-gate #include <prof_attr.h>
340Sstevel@tonic-gate #include <getxby_door.h>
350Sstevel@tonic-gate #include <sys/mman.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /* Externs from libnsl */
390Sstevel@tonic-gate extern profstr_t *_getprofnam(const char *, profstr_t *, char *, int, int *);
400Sstevel@tonic-gate extern profstr_t *_getprofattr(profstr_t *, char *, int, int *);
410Sstevel@tonic-gate extern void _setprofattr(void);
420Sstevel@tonic-gate extern void _endprofattr(void);
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static profattr_t *profstr2attr(profstr_t *);
450Sstevel@tonic-gate 
460Sstevel@tonic-gate profattr_t *
470Sstevel@tonic-gate getprofattr()
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	int		err = 0;
500Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_PROFATTR];
510Sstevel@tonic-gate 	profstr_t	prof;
520Sstevel@tonic-gate 	profstr_t	*tmp;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	tmp = _getprofattr(&prof, buf, NSS_BUFLEN_PROFATTR, &err);
550Sstevel@tonic-gate 	return (profstr2attr(tmp));
560Sstevel@tonic-gate }
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 
590Sstevel@tonic-gate profattr_t *
600Sstevel@tonic-gate getprofnam(const char *name)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	int		err = 0;
630Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_PROFATTR];
640Sstevel@tonic-gate 	profstr_t	prof;
650Sstevel@tonic-gate 	profstr_t	*resptr = (profstr_t *)NULL;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	(void) memset(&prof, 0, sizeof (profstr_t));
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	resptr = _getprofnam(name, &prof, buf, NSS_BUFLEN_PROFATTR, &err);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	return (profstr2attr(resptr));
720Sstevel@tonic-gate 
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 
760Sstevel@tonic-gate void
770Sstevel@tonic-gate setprofattr()
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	_setprofattr();
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 
830Sstevel@tonic-gate void
840Sstevel@tonic-gate endprofattr()
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	_endprofattr();
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 
900Sstevel@tonic-gate void
910Sstevel@tonic-gate free_profattr(profattr_t *prof)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	if (prof) {
940Sstevel@tonic-gate 		free(prof->name);
950Sstevel@tonic-gate 		free(prof->res1);
960Sstevel@tonic-gate 		free(prof->res2);
970Sstevel@tonic-gate 		free(prof->desc);
980Sstevel@tonic-gate 		_kva_free(prof->attr);
990Sstevel@tonic-gate 		free(prof);
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static profattr_t *
1050Sstevel@tonic-gate profstr2attr(profstr_t *prof)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	profattr_t *newprof;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (prof == NULL)
1100Sstevel@tonic-gate 		return ((profattr_t *)NULL);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if ((newprof = (profattr_t *)malloc(sizeof (profattr_t))) == NULL)
1130Sstevel@tonic-gate 		return ((profattr_t *)NULL);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	newprof->name = _do_unescape(prof->name);
1160Sstevel@tonic-gate 	newprof->res1 = _do_unescape(prof->res1);
1170Sstevel@tonic-gate 	newprof->res2 = _do_unescape(prof->res2);
1180Sstevel@tonic-gate 	newprof->desc = _do_unescape(prof->desc);
1190Sstevel@tonic-gate 	newprof->attr = _str2kva(prof->attr, KV_ASSIGN, KV_DELIMITER);
1200Sstevel@tonic-gate 	return (newprof);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * Given a profile name, gets the list of profiles found from
1260Sstevel@tonic-gate  * the whole hierarchy, using the given profile as root
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate void
1290Sstevel@tonic-gate getproflist(const char *profileName, char **profArray, int *profcnt)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	profattr_t	*profattr;
1320Sstevel@tonic-gate 	char		*subprofiles, *lasts, *profname;
1330Sstevel@tonic-gate 	int		i;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	/* Check if this is a duplicate */
1360Sstevel@tonic-gate 	for (i = 0; i < *profcnt; i++) {
1370Sstevel@tonic-gate 		if (strcmp(profileName, profArray[i]) == 0) {
1380Sstevel@tonic-gate 			/* It's a duplicate, don't need to do anything */
1390Sstevel@tonic-gate 			return;
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	profArray[*profcnt] = strdup(profileName);
1440Sstevel@tonic-gate 	*profcnt = *profcnt + 1;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	profattr = getprofnam(profileName);
1470Sstevel@tonic-gate 	if (profattr == NULL) {
1480Sstevel@tonic-gate 		return;
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (profattr->attr == NULL) {
1520Sstevel@tonic-gate 		free_profattr(profattr);
1530Sstevel@tonic-gate 		return;
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	subprofiles = kva_match(profattr->attr, PROFATTR_PROFS_KW);
1570Sstevel@tonic-gate 	if (subprofiles == NULL) {
1580Sstevel@tonic-gate 		free_profattr(profattr);
1590Sstevel@tonic-gate 		return;
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/* get execattr from each subprofiles */
1630Sstevel@tonic-gate 	for (profname = (char *)strtok_r(subprofiles, ",", &lasts);
1640Sstevel@tonic-gate 	    profname != NULL;
1650Sstevel@tonic-gate 	    profname = (char *)strtok_r(NULL, ",", &lasts)) {
1660Sstevel@tonic-gate 			getproflist(profname, profArray, profcnt);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 	free_profattr(profattr);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate void
1720Sstevel@tonic-gate free_proflist(char **profArray, int profcnt)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	int i;
1750Sstevel@tonic-gate 	for (i = 0; i < profcnt; i++) {
1760Sstevel@tonic-gate 		free(profArray[i]);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate #ifdef DEBUG
1820Sstevel@tonic-gate void
1830Sstevel@tonic-gate print_profattr(profattr_t *prof)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	extern void print_kva(kva_t *);
1860Sstevel@tonic-gate 	char *empty = "empty";
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (prof == NULL) {
1890Sstevel@tonic-gate 		printf("NULL\n");
1900Sstevel@tonic-gate 		return;
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	printf("name=%s\n", prof->name ? prof->name : empty);
1940Sstevel@tonic-gate 	printf("res1=%s\n", prof->res1 ? prof->res1 : empty);
1950Sstevel@tonic-gate 	printf("res2=%s\n", prof->res2 ? prof->res2 : empty);
1960Sstevel@tonic-gate 	printf("desc=%s\n", prof->desc ? prof->desc : empty);
1970Sstevel@tonic-gate 	printf("attr=\n");
1980Sstevel@tonic-gate 	print_kva(prof->attr);
1990Sstevel@tonic-gate 	fflush(stdout);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate #endif  /* DEBUG */
202