xref: /titanic_41/usr/src/lib/libsecdb/common/getprofattr.c (revision 159d09a20817016f09b3ea28d1bdada4a336bb91)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <nss_dbdefs.h>
33 #include <prof_attr.h>
34 #include <getxby_door.h>
35 #include <sys/mman.h>
36 
37 
38 /* Externs from libnsl */
39 extern profstr_t *_getprofnam(const char *, profstr_t *, char *, int, int *);
40 extern profstr_t *_getprofattr(profstr_t *, char *, int, int *);
41 extern void _setprofattr(void);
42 extern void _endprofattr(void);
43 
44 static profattr_t *profstr2attr(profstr_t *);
45 
46 profattr_t *
47 getprofattr()
48 {
49 	int		err = 0;
50 	char		buf[NSS_BUFLEN_PROFATTR];
51 	profstr_t	prof;
52 	profstr_t	*tmp;
53 
54 	tmp = _getprofattr(&prof, buf, NSS_BUFLEN_PROFATTR, &err);
55 	return (profstr2attr(tmp));
56 }
57 
58 
59 profattr_t *
60 getprofnam(const char *name)
61 {
62 	int		err = 0;
63 	char		buf[NSS_BUFLEN_PROFATTR];
64 	profstr_t	prof;
65 	profstr_t	*resptr = (profstr_t *)NULL;
66 
67 	(void) memset(&prof, 0, sizeof (profstr_t));
68 
69 	resptr = _getprofnam(name, &prof, buf, NSS_BUFLEN_PROFATTR, &err);
70 
71 	return (profstr2attr(resptr));
72 
73 }
74 
75 
76 void
77 setprofattr()
78 {
79 	_setprofattr();
80 }
81 
82 
83 void
84 endprofattr()
85 {
86 	_endprofattr();
87 }
88 
89 
90 void
91 free_profattr(profattr_t *prof)
92 {
93 	if (prof) {
94 		free(prof->name);
95 		free(prof->res1);
96 		free(prof->res2);
97 		free(prof->desc);
98 		_kva_free(prof->attr);
99 		free(prof);
100 	}
101 }
102 
103 
104 static profattr_t *
105 profstr2attr(profstr_t *prof)
106 {
107 	profattr_t *newprof;
108 
109 	if (prof == NULL)
110 		return ((profattr_t *)NULL);
111 
112 	if ((newprof = (profattr_t *)malloc(sizeof (profattr_t))) == NULL)
113 		return ((profattr_t *)NULL);
114 
115 	newprof->name = _do_unescape(prof->name);
116 	newprof->res1 = _do_unescape(prof->res1);
117 	newprof->res2 = _do_unescape(prof->res2);
118 	newprof->desc = _do_unescape(prof->desc);
119 	newprof->attr = _str2kva(prof->attr, KV_ASSIGN, KV_DELIMITER);
120 	return (newprof);
121 }
122 
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
129 getproflist(const char *profileName, char **profArray, int *profcnt)
130 {
131 	profattr_t	*profattr;
132 	char		*subprofiles, *lasts, *profname;
133 	int		i;
134 
135 	/* Check if this is a duplicate */
136 	for (i = 0; i < *profcnt; i++) {
137 		if (strcmp(profileName, profArray[i]) == 0) {
138 			/* It's a duplicate, don't need to do anything */
139 			return;
140 		}
141 	}
142 
143 	profArray[*profcnt] = strdup(profileName);
144 	*profcnt = *profcnt + 1;
145 
146 	profattr = getprofnam(profileName);
147 	if (profattr == NULL) {
148 		return;
149 	}
150 
151 	if (profattr->attr == NULL) {
152 		free_profattr(profattr);
153 		return;
154 	}
155 
156 	subprofiles = kva_match(profattr->attr, PROFATTR_PROFS_KW);
157 	if (subprofiles == NULL) {
158 		free_profattr(profattr);
159 		return;
160 	}
161 
162 	/* get execattr from each subprofiles */
163 	for (profname = (char *)strtok_r(subprofiles, ",", &lasts);
164 	    profname != NULL;
165 	    profname = (char *)strtok_r(NULL, ",", &lasts)) {
166 			getproflist(profname, profArray, profcnt);
167 	}
168 	free_profattr(profattr);
169 }
170 
171 void
172 free_proflist(char **profArray, int profcnt)
173 {
174 	int i;
175 	for (i = 0; i < profcnt; i++) {
176 		free(profArray[i]);
177 	}
178 }
179 
180 
181 #ifdef DEBUG
182 void
183 print_profattr(profattr_t *prof)
184 {
185 	extern void print_kva(kva_t *);
186 	char *empty = "empty";
187 
188 	if (prof == NULL) {
189 		printf("NULL\n");
190 		return;
191 	}
192 
193 	printf("name=%s\n", prof->name ? prof->name : empty);
194 	printf("res1=%s\n", prof->res1 ? prof->res1 : empty);
195 	printf("res2=%s\n", prof->res2 ? prof->res2 : empty);
196 	printf("desc=%s\n", prof->desc ? prof->desc : empty);
197 	printf("attr=\n");
198 	print_kva(prof->attr);
199 	fflush(stdout);
200 }
201 #endif  /* DEBUG */
202