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
52830Sdjl  * Common Development and Distribution License (the "License").
62830Sdjl  * 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*12273SCasper.Dik@Sun.COM  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <nss_dbdefs.h>
300Sstevel@tonic-gate #include <prof_attr.h>
310Sstevel@tonic-gate #include <getxby_door.h>
320Sstevel@tonic-gate #include <sys/mman.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /* Externs from libnsl */
360Sstevel@tonic-gate extern profstr_t *_getprofnam(const char *, profstr_t *, char *, int, int *);
370Sstevel@tonic-gate extern profstr_t *_getprofattr(profstr_t *, char *, int, int *);
380Sstevel@tonic-gate extern void _setprofattr(void);
390Sstevel@tonic-gate extern void _endprofattr(void);
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static profattr_t *profstr2attr(profstr_t *);
420Sstevel@tonic-gate 
430Sstevel@tonic-gate profattr_t *
440Sstevel@tonic-gate getprofattr()
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	int		err = 0;
470Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_PROFATTR];
480Sstevel@tonic-gate 	profstr_t	prof;
490Sstevel@tonic-gate 	profstr_t	*tmp;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	tmp = _getprofattr(&prof, buf, NSS_BUFLEN_PROFATTR, &err);
520Sstevel@tonic-gate 	return (profstr2attr(tmp));
530Sstevel@tonic-gate }
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 
560Sstevel@tonic-gate profattr_t *
570Sstevel@tonic-gate getprofnam(const char *name)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	int		err = 0;
600Sstevel@tonic-gate 	char		buf[NSS_BUFLEN_PROFATTR];
610Sstevel@tonic-gate 	profstr_t	prof;
620Sstevel@tonic-gate 	profstr_t	*resptr = (profstr_t *)NULL;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	(void) memset(&prof, 0, sizeof (profstr_t));
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	resptr = _getprofnam(name, &prof, buf, NSS_BUFLEN_PROFATTR, &err);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	return (profstr2attr(resptr));
690Sstevel@tonic-gate 
700Sstevel@tonic-gate }
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 
730Sstevel@tonic-gate void
740Sstevel@tonic-gate setprofattr()
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	_setprofattr();
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 
800Sstevel@tonic-gate void
810Sstevel@tonic-gate endprofattr()
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	_endprofattr();
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 
870Sstevel@tonic-gate void
880Sstevel@tonic-gate free_profattr(profattr_t *prof)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	if (prof) {
910Sstevel@tonic-gate 		free(prof->name);
920Sstevel@tonic-gate 		free(prof->res1);
930Sstevel@tonic-gate 		free(prof->res2);
940Sstevel@tonic-gate 		free(prof->desc);
950Sstevel@tonic-gate 		_kva_free(prof->attr);
960Sstevel@tonic-gate 		free(prof);
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static profattr_t *
1020Sstevel@tonic-gate profstr2attr(profstr_t *prof)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	profattr_t *newprof;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (prof == NULL)
1070Sstevel@tonic-gate 		return ((profattr_t *)NULL);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if ((newprof = (profattr_t *)malloc(sizeof (profattr_t))) == NULL)
1100Sstevel@tonic-gate 		return ((profattr_t *)NULL);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	newprof->name = _do_unescape(prof->name);
1130Sstevel@tonic-gate 	newprof->res1 = _do_unescape(prof->res1);
1140Sstevel@tonic-gate 	newprof->res2 = _do_unescape(prof->res2);
1150Sstevel@tonic-gate 	newprof->desc = _do_unescape(prof->desc);
1160Sstevel@tonic-gate 	newprof->attr = _str2kva(prof->attr, KV_ASSIGN, KV_DELIMITER);
1170Sstevel@tonic-gate 	return (newprof);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 
121*12273SCasper.Dik@Sun.COM extern int _enum_common_p(const char *, int (*)(const char *, kva_t *, void *,
122*12273SCasper.Dik@Sun.COM     void *), void *, void *, boolean_t, int *, char *[MAXPROFS]);
123*12273SCasper.Dik@Sun.COM 
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 {
131*12273SCasper.Dik@Sun.COM 	/* There can't be a "," in a profile name. */
132*12273SCasper.Dik@Sun.COM 	if (strchr(profileName, KV_SEPCHAR) != NULL)
1330Sstevel@tonic-gate 		return;
1340Sstevel@tonic-gate 
135*12273SCasper.Dik@Sun.COM 	(void) _enum_common_p(profileName, NULL, NULL, NULL, B_FALSE,
136*12273SCasper.Dik@Sun.COM 	    profcnt, profArray);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate void
1400Sstevel@tonic-gate free_proflist(char **profArray, int profcnt)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	int i;
1430Sstevel@tonic-gate 	for (i = 0; i < profcnt; i++) {
1440Sstevel@tonic-gate 		free(profArray[i]);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate #ifdef DEBUG
1500Sstevel@tonic-gate void
1510Sstevel@tonic-gate print_profattr(profattr_t *prof)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	extern void print_kva(kva_t *);
1540Sstevel@tonic-gate 	char *empty = "empty";
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (prof == NULL) {
1570Sstevel@tonic-gate 		printf("NULL\n");
1580Sstevel@tonic-gate 		return;
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	printf("name=%s\n", prof->name ? prof->name : empty);
1620Sstevel@tonic-gate 	printf("res1=%s\n", prof->res1 ? prof->res1 : empty);
1630Sstevel@tonic-gate 	printf("res2=%s\n", prof->res2 ? prof->res2 : empty);
1640Sstevel@tonic-gate 	printf("desc=%s\n", prof->desc ? prof->desc : empty);
1650Sstevel@tonic-gate 	printf("attr=\n");
1660Sstevel@tonic-gate 	print_kva(prof->attr);
1670Sstevel@tonic-gate 	fflush(stdout);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate #endif  /* DEBUG */
170