1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <nss_dbdefs.h>
30 #include <prof_attr.h>
31 #include <getxby_door.h>
32 #include <sys/mman.h>
33 
34 
35 /* Externs from libnsl */
36 extern profstr_t *_getprofnam(const char *, profstr_t *, char *, int, int *);
37 extern profstr_t *_getprofattr(profstr_t *, char *, int, int *);
38 extern void _setprofattr(void);
39 extern void _endprofattr(void);
40 
41 static profattr_t *profstr2attr(profstr_t *);
42 
43 profattr_t *
getprofattr()44 getprofattr()
45 {
46 	int		err = 0;
47 	char		buf[NSS_BUFLEN_PROFATTR];
48 	profstr_t	prof;
49 	profstr_t	*tmp;
50 
51 	tmp = _getprofattr(&prof, buf, NSS_BUFLEN_PROFATTR, &err);
52 	return (profstr2attr(tmp));
53 }
54 
55 
56 profattr_t *
getprofnam(const char * name)57 getprofnam(const char *name)
58 {
59 	int		err = 0;
60 	char		buf[NSS_BUFLEN_PROFATTR];
61 	profstr_t	prof;
62 	profstr_t	*resptr = (profstr_t *)NULL;
63 
64 	(void) memset(&prof, 0, sizeof (profstr_t));
65 
66 	resptr = _getprofnam(name, &prof, buf, NSS_BUFLEN_PROFATTR, &err);
67 
68 	return (profstr2attr(resptr));
69 
70 }
71 
72 
73 void
setprofattr()74 setprofattr()
75 {
76 	_setprofattr();
77 }
78 
79 
80 void
endprofattr()81 endprofattr()
82 {
83 	_endprofattr();
84 }
85 
86 
87 void
free_profattr(profattr_t * prof)88 free_profattr(profattr_t *prof)
89 {
90 	if (prof) {
91 		free(prof->name);
92 		free(prof->res1);
93 		free(prof->res2);
94 		free(prof->desc);
95 		_kva_free(prof->attr);
96 		free(prof);
97 	}
98 }
99 
100 
101 static profattr_t *
profstr2attr(profstr_t * prof)102 profstr2attr(profstr_t *prof)
103 {
104 	profattr_t *newprof;
105 
106 	if (prof == NULL)
107 		return ((profattr_t *)NULL);
108 
109 	if ((newprof = (profattr_t *)malloc(sizeof (profattr_t))) == NULL)
110 		return ((profattr_t *)NULL);
111 
112 	newprof->name = _do_unescape(prof->name);
113 	newprof->res1 = _do_unescape(prof->res1);
114 	newprof->res2 = _do_unescape(prof->res2);
115 	newprof->desc = _do_unescape(prof->desc);
116 	newprof->attr = _str2kva(prof->attr, KV_ASSIGN, KV_DELIMITER);
117 	return (newprof);
118 }
119 
120 
121 extern int _enum_common_p(const char *, int (*)(const char *, kva_t *, void *,
122     void *), void *, void *, boolean_t, int *, char *[MAXPROFS]);
123 
124 /*
125  * Given a profile name, gets the list of profiles found from
126  * the whole hierarchy, using the given profile as root
127  */
128 void
getproflist(const char * profileName,char ** profArray,int * profcnt)129 getproflist(const char *profileName, char **profArray, int *profcnt)
130 {
131 	/* There can't be a "," in a profile name. */
132 	if (strchr(profileName, KV_SEPCHAR) != NULL)
133 		return;
134 
135 	(void) _enum_common_p(profileName, NULL, NULL, NULL, B_FALSE,
136 	    profcnt, profArray);
137 }
138 
139 void
free_proflist(char ** profArray,int profcnt)140 free_proflist(char **profArray, int profcnt)
141 {
142 	int i;
143 	for (i = 0; i < profcnt; i++) {
144 		free(profArray[i]);
145 	}
146 }
147 
148 
149 #ifdef DEBUG
150 void
print_profattr(profattr_t * prof)151 print_profattr(profattr_t *prof)
152 {
153 	extern void print_kva(kva_t *);
154 	char *empty = "empty";
155 
156 	if (prof == NULL) {
157 		printf("NULL\n");
158 		return;
159 	}
160 
161 	printf("name=%s\n", prof->name ? prof->name : empty);
162 	printf("res1=%s\n", prof->res1 ? prof->res1 : empty);
163 	printf("res2=%s\n", prof->res2 ? prof->res2 : empty);
164 	printf("desc=%s\n", prof->desc ? prof->desc : empty);
165 	printf("attr=\n");
166 	print_kva(prof->attr);
167 	fflush(stdout);
168 }
169 #endif  /* DEBUG */
170