17c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate  * prof_get.c --- routines that expose the public interfaces for
47c478bd9Sstevel@tonic-gate  * 	querying items from the profile.
57c478bd9Sstevel@tonic-gate  *
67c478bd9Sstevel@tonic-gate  */
77c478bd9Sstevel@tonic-gate 
8*505d05c7Sgtb #include "prof_int.h"
97c478bd9Sstevel@tonic-gate #include <stdio.h>
107c478bd9Sstevel@tonic-gate #include <string.h>
117c478bd9Sstevel@tonic-gate #ifdef HAVE_STDLIB_H
127c478bd9Sstevel@tonic-gate #include <stdlib.h>
137c478bd9Sstevel@tonic-gate #endif
147c478bd9Sstevel@tonic-gate #include <errno.h>
15*505d05c7Sgtb #include <limits.h>
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate /*
187c478bd9Sstevel@tonic-gate  * These functions --- init_list(), end_list(), and add_to_list() are
197c478bd9Sstevel@tonic-gate  * internal functions used to build up a null-terminated char ** list
207c478bd9Sstevel@tonic-gate  * of strings to be returned by functions like profile_get_values.
217c478bd9Sstevel@tonic-gate  *
227c478bd9Sstevel@tonic-gate  * The profile_string_list structure is used for internal booking
237c478bd9Sstevel@tonic-gate  * purposes to build up the list, which is returned in *ret_list by
247c478bd9Sstevel@tonic-gate  * the end_list() function.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * The publicly exported interface for freeing char** list is
277c478bd9Sstevel@tonic-gate  * profile_free_list().
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate struct profile_string_list {
317c478bd9Sstevel@tonic-gate 	char	**list;
327c478bd9Sstevel@tonic-gate 	int	num;
337c478bd9Sstevel@tonic-gate 	int	max;
347c478bd9Sstevel@tonic-gate };
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Initialize the string list abstraction.
387c478bd9Sstevel@tonic-gate  */
39*505d05c7Sgtb static errcode_t init_list(struct profile_string_list *list)
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate 	list->num = 0;
427c478bd9Sstevel@tonic-gate 	list->max = 10;
437c478bd9Sstevel@tonic-gate 	list->list = (char **) malloc(list->max * sizeof(char *));
447c478bd9Sstevel@tonic-gate 	if (list->list == 0)
457c478bd9Sstevel@tonic-gate 		return ENOMEM;
467c478bd9Sstevel@tonic-gate 	list->list[0] = 0;
477c478bd9Sstevel@tonic-gate 	return 0;
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Free any memory left over in the string abstraction, returning the
527c478bd9Sstevel@tonic-gate  * built up list in *ret_list if it is non-null.
537c478bd9Sstevel@tonic-gate  */
54*505d05c7Sgtb static void end_list(struct profile_string_list *list, char ***ret_list)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	char	**cp;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	if (list == 0)
597c478bd9Sstevel@tonic-gate 		return;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if (ret_list) {
627c478bd9Sstevel@tonic-gate 		*ret_list = list->list;
637c478bd9Sstevel@tonic-gate 		return;
647c478bd9Sstevel@tonic-gate 	} else {
657c478bd9Sstevel@tonic-gate 		for (cp = list->list; *cp; cp++)
667c478bd9Sstevel@tonic-gate 			free(*cp);
677c478bd9Sstevel@tonic-gate 		free(list->list);
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 	list->num = list->max = 0;
707c478bd9Sstevel@tonic-gate 	list->list = 0;
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Add a string to the list.
757c478bd9Sstevel@tonic-gate  */
76*505d05c7Sgtb static errcode_t add_to_list(struct profile_string_list *list, const char *str)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	char 	*newstr, **newlist;
797c478bd9Sstevel@tonic-gate 	int	newmax;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (list->num+1 >= list->max) {
827c478bd9Sstevel@tonic-gate 		newmax = list->max + 10;
83*505d05c7Sgtb 		newlist = (char **) realloc(list->list, newmax * sizeof(char *));
847c478bd9Sstevel@tonic-gate 		if (newlist == 0)
857c478bd9Sstevel@tonic-gate 			return ENOMEM;
867c478bd9Sstevel@tonic-gate 		list->max = newmax;
877c478bd9Sstevel@tonic-gate 		list->list = newlist;
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	newstr = (char *) malloc(strlen(str)+1);
907c478bd9Sstevel@tonic-gate 	if (newstr == 0)
917c478bd9Sstevel@tonic-gate 		return ENOMEM;
927c478bd9Sstevel@tonic-gate 	strcpy(newstr, str);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	list->list[list->num++] = newstr;
957c478bd9Sstevel@tonic-gate 	list->list[list->num] = 0;
967c478bd9Sstevel@tonic-gate 	return 0;
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * Return TRUE if the string is already a member of the list.
1017c478bd9Sstevel@tonic-gate  */
102*505d05c7Sgtb static int is_list_member(struct profile_string_list *list, const char *str)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	char **cpp;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (!list->list)
1077c478bd9Sstevel@tonic-gate 		return 0;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	for (cpp = list->list; *cpp; cpp++) {
1107c478bd9Sstevel@tonic-gate 		if (!strcmp(*cpp, str))
1117c478bd9Sstevel@tonic-gate 			return 1;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	return 0;
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * This function frees a null-terminated list as returned by
1187c478bd9Sstevel@tonic-gate  * profile_get_values.
1197c478bd9Sstevel@tonic-gate  */
120*505d05c7Sgtb void KRB5_CALLCONV profile_free_list(char **list)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate     char	**cp;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate     if (list == 0)
1257c478bd9Sstevel@tonic-gate 	    return;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate     for (cp = list; *cp; cp++)
1287c478bd9Sstevel@tonic-gate 	free(*cp);
1297c478bd9Sstevel@tonic-gate     free(list);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
132*505d05c7Sgtb errcode_t KRB5_CALLCONV
133*505d05c7Sgtb profile_get_values(profile_t profile, const char *const *names,
134*505d05c7Sgtb 		   char ***ret_values)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	errcode_t		retval;
1377c478bd9Sstevel@tonic-gate 	void			*state;
1387c478bd9Sstevel@tonic-gate 	char			*value;
1397c478bd9Sstevel@tonic-gate 	struct profile_string_list values;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if ((retval = profile_node_iterator_create(profile, names,
1427c478bd9Sstevel@tonic-gate 						   PROFILE_ITER_RELATIONS_ONLY,
1437c478bd9Sstevel@tonic-gate 						   &state)))
1447c478bd9Sstevel@tonic-gate 		return retval;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if ((retval = init_list(&values)))
1477c478bd9Sstevel@tonic-gate 		return retval;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	do {
1507c478bd9Sstevel@tonic-gate 		if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1517c478bd9Sstevel@tonic-gate 			goto cleanup;
1527c478bd9Sstevel@tonic-gate 		if (value)
1537c478bd9Sstevel@tonic-gate 			add_to_list(&values, value);
1547c478bd9Sstevel@tonic-gate 	} while (state);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (values.num == 0) {
1577c478bd9Sstevel@tonic-gate 		retval = PROF_NO_RELATION;
1587c478bd9Sstevel@tonic-gate 		goto cleanup;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	end_list(&values, ret_values);
1627c478bd9Sstevel@tonic-gate 	return 0;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate cleanup:
1657c478bd9Sstevel@tonic-gate 	end_list(&values, (char ***)NULL);
1667c478bd9Sstevel@tonic-gate 	return retval;
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * This function only gets the first value from the file; it is a
1717c478bd9Sstevel@tonic-gate  * helper function for profile_get_string, profile_get_integer, etc.
1727c478bd9Sstevel@tonic-gate  */
173*505d05c7Sgtb errcode_t profile_get_value(profile_t profile, const char **names,
174*505d05c7Sgtb 			    const char **ret_value)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	errcode_t		retval;
1777c478bd9Sstevel@tonic-gate 	void			*state;
1787c478bd9Sstevel@tonic-gate 	char			*value;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if ((retval = profile_node_iterator_create(profile, names,
1817c478bd9Sstevel@tonic-gate 						   PROFILE_ITER_RELATIONS_ONLY,
1827c478bd9Sstevel@tonic-gate 						   &state)))
1837c478bd9Sstevel@tonic-gate 		return retval;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1867c478bd9Sstevel@tonic-gate 		goto cleanup;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (value)
1897c478bd9Sstevel@tonic-gate 		*ret_value = value;
1907c478bd9Sstevel@tonic-gate 	else
1917c478bd9Sstevel@tonic-gate 		retval = PROF_NO_RELATION;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate cleanup:
1947c478bd9Sstevel@tonic-gate 	profile_node_iterator_free(&state);
1957c478bd9Sstevel@tonic-gate 	return retval;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
198*505d05c7Sgtb errcode_t KRB5_CALLCONV
199*505d05c7Sgtb profile_get_string(profile_t profile, const char *name, const char *subname,
200*505d05c7Sgtb 		   const char *subsubname, const char *def_val,
201*505d05c7Sgtb 		   char **ret_string)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	const char	*value;
2047c478bd9Sstevel@tonic-gate 	errcode_t	retval;
2057c478bd9Sstevel@tonic-gate 	const char	*names[4];
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (profile) {
2087c478bd9Sstevel@tonic-gate 		names[0] = name;
2097c478bd9Sstevel@tonic-gate 		names[1] = subname;
2107c478bd9Sstevel@tonic-gate 		names[2] = subsubname;
2117c478bd9Sstevel@tonic-gate 		names[3] = 0;
2127c478bd9Sstevel@tonic-gate 		retval = profile_get_value(profile, names, &value);
2137c478bd9Sstevel@tonic-gate 		if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
2147c478bd9Sstevel@tonic-gate 			value = def_val;
2157c478bd9Sstevel@tonic-gate 		else if (retval)
2167c478bd9Sstevel@tonic-gate 			return retval;
2177c478bd9Sstevel@tonic-gate 	} else
2187c478bd9Sstevel@tonic-gate 		value = def_val;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (value) {
221*505d05c7Sgtb 	  *ret_string = (char *) malloc(strlen(value)+1);
2227c478bd9Sstevel@tonic-gate 		if (*ret_string == 0)
2237c478bd9Sstevel@tonic-gate 			return ENOMEM;
2247c478bd9Sstevel@tonic-gate 		strcpy(*ret_string, value);
2257c478bd9Sstevel@tonic-gate 	} else
2267c478bd9Sstevel@tonic-gate 		*ret_string = 0;
2277c478bd9Sstevel@tonic-gate 	return 0;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
230*505d05c7Sgtb errcode_t KRB5_CALLCONV
231*505d05c7Sgtb profile_get_integer(profile_t profile, const char *name, const char *subname,
232*505d05c7Sgtb 		    const char *subsubname, int def_val, int *ret_int)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	const char	*value;
2357c478bd9Sstevel@tonic-gate 	errcode_t	retval;
2367c478bd9Sstevel@tonic-gate 	const char	*names[4];
237*505d05c7Sgtb 	char            *end_value;
238*505d05c7Sgtb 	long		ret_long;
2397c478bd9Sstevel@tonic-gate 
240*505d05c7Sgtb 	*ret_int = def_val;
241*505d05c7Sgtb 	if (profile == 0)
2427c478bd9Sstevel@tonic-gate 		return 0;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	names[0] = name;
2457c478bd9Sstevel@tonic-gate 	names[1] = subname;
2467c478bd9Sstevel@tonic-gate 	names[2] = subsubname;
2477c478bd9Sstevel@tonic-gate 	names[3] = 0;
2487c478bd9Sstevel@tonic-gate 	retval = profile_get_value(profile, names, &value);
2497c478bd9Sstevel@tonic-gate 	if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
2507c478bd9Sstevel@tonic-gate 		*ret_int = def_val;
2517c478bd9Sstevel@tonic-gate 		return 0;
2527c478bd9Sstevel@tonic-gate 	} else if (retval)
2537c478bd9Sstevel@tonic-gate 		return retval;
254*505d05c7Sgtb 
255*505d05c7Sgtb 	if (value[0] == 0)
256*505d05c7Sgtb 	    /* Empty string is no good.  */
257*505d05c7Sgtb 	    return PROF_BAD_INTEGER;
258*505d05c7Sgtb 	errno = 0;
259*505d05c7Sgtb 	ret_long = strtol (value, &end_value, 10);
260*505d05c7Sgtb 
261*505d05c7Sgtb 	/* Overflow or underflow.  */
262*505d05c7Sgtb 	if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
263*505d05c7Sgtb 	    return PROF_BAD_INTEGER;
264*505d05c7Sgtb 	/* Value outside "int" range.  */
265*505d05c7Sgtb 	if ((long) (int) ret_long != ret_long)
266*505d05c7Sgtb 	    return PROF_BAD_INTEGER;
267*505d05c7Sgtb 	/* Garbage in string.  */
268*505d05c7Sgtb 	if (end_value != value + strlen (value))
269*505d05c7Sgtb 	    return PROF_BAD_INTEGER;
270*505d05c7Sgtb 
2717c478bd9Sstevel@tonic-gate 
272*505d05c7Sgtb 	*ret_int = ret_long;
2737c478bd9Sstevel@tonic-gate 	return 0;
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
276*505d05c7Sgtb static const char *const conf_yes[] = {
277*505d05c7Sgtb     "y", "yes", "true", "t", "1", "on",
278*505d05c7Sgtb     0,
279*505d05c7Sgtb };
280*505d05c7Sgtb 
281*505d05c7Sgtb static const char *const conf_no[] = {
282*505d05c7Sgtb     "n", "no", "false", "nil", "0", "off",
283*505d05c7Sgtb     0,
284*505d05c7Sgtb };
285*505d05c7Sgtb 
286*505d05c7Sgtb static errcode_t
287*505d05c7Sgtb profile_parse_boolean(const char *s, int *ret_boolean)
288*505d05c7Sgtb {
289*505d05c7Sgtb     const char *const *p;
290*505d05c7Sgtb 
291*505d05c7Sgtb     if (ret_boolean == NULL)
292*505d05c7Sgtb     	return PROF_EINVAL;
293*505d05c7Sgtb 
294*505d05c7Sgtb     for(p=conf_yes; *p; p++) {
295*505d05c7Sgtb 		if (!strcasecmp(*p,s)) {
296*505d05c7Sgtb 			*ret_boolean = 1;
297*505d05c7Sgtb 	    	return 0;
298*505d05c7Sgtb 		}
299*505d05c7Sgtb     }
300*505d05c7Sgtb 
301*505d05c7Sgtb     for(p=conf_no; *p; p++) {
302*505d05c7Sgtb 		if (!strcasecmp(*p,s)) {
303*505d05c7Sgtb 			*ret_boolean = 0;
304*505d05c7Sgtb 			return 0;
305*505d05c7Sgtb 		}
306*505d05c7Sgtb     }
307*505d05c7Sgtb 
308*505d05c7Sgtb 	return PROF_BAD_BOOLEAN;
309*505d05c7Sgtb }
310*505d05c7Sgtb 
311*505d05c7Sgtb errcode_t KRB5_CALLCONV
312*505d05c7Sgtb profile_get_boolean(profile_t profile, const char *name, const char *subname,
313*505d05c7Sgtb 		    const char *subsubname, int def_val, int *ret_boolean)
314*505d05c7Sgtb {
315*505d05c7Sgtb 	const char	*value;
316*505d05c7Sgtb 	errcode_t	retval;
317*505d05c7Sgtb 	const char	*names[4];
318*505d05c7Sgtb 
319*505d05c7Sgtb 	if (profile == 0) {
320*505d05c7Sgtb 		*ret_boolean = def_val;
321*505d05c7Sgtb 		return 0;
322*505d05c7Sgtb 	}
323*505d05c7Sgtb 
324*505d05c7Sgtb 	names[0] = name;
325*505d05c7Sgtb 	names[1] = subname;
326*505d05c7Sgtb 	names[2] = subsubname;
327*505d05c7Sgtb 	names[3] = 0;
328*505d05c7Sgtb 	retval = profile_get_value(profile, names, &value);
329*505d05c7Sgtb 	if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
330*505d05c7Sgtb 		*ret_boolean = def_val;
331*505d05c7Sgtb 		return 0;
332*505d05c7Sgtb 	} else if (retval)
333*505d05c7Sgtb 		return retval;
334*505d05c7Sgtb 
335*505d05c7Sgtb 	return profile_parse_boolean (value, ret_boolean);
336*505d05c7Sgtb }
337*505d05c7Sgtb 
3387c478bd9Sstevel@tonic-gate /*
3397c478bd9Sstevel@tonic-gate  * This function will return the list of the names of subections in the
3407c478bd9Sstevel@tonic-gate  * under the specified section name.
3417c478bd9Sstevel@tonic-gate  */
342*505d05c7Sgtb errcode_t KRB5_CALLCONV
343*505d05c7Sgtb profile_get_subsection_names(profile_t profile, const char **names,
344*505d05c7Sgtb 			     char ***ret_names)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	errcode_t		retval;
3477c478bd9Sstevel@tonic-gate 	void			*state;
3487c478bd9Sstevel@tonic-gate 	char			*name;
3497c478bd9Sstevel@tonic-gate 	struct profile_string_list values;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if ((retval = profile_node_iterator_create(profile, names,
3527c478bd9Sstevel@tonic-gate 		   PROFILE_ITER_LIST_SECTION | PROFILE_ITER_SECTIONS_ONLY,
3537c478bd9Sstevel@tonic-gate 		   &state)))
3547c478bd9Sstevel@tonic-gate 		return retval;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if ((retval = init_list(&values)))
3577c478bd9Sstevel@tonic-gate 		return retval;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	do {
3607c478bd9Sstevel@tonic-gate 		if ((retval = profile_node_iterator(&state, 0, &name, 0)))
3617c478bd9Sstevel@tonic-gate 			goto cleanup;
3627c478bd9Sstevel@tonic-gate 		if (name)
3637c478bd9Sstevel@tonic-gate 			add_to_list(&values, name);
3647c478bd9Sstevel@tonic-gate 	} while (state);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	end_list(&values, ret_names);
3677c478bd9Sstevel@tonic-gate 	return 0;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate cleanup:
3707c478bd9Sstevel@tonic-gate 	end_list(&values, (char ***)NULL);
3717c478bd9Sstevel@tonic-gate 	return retval;
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * This function will return the list of the names of relations in the
3767c478bd9Sstevel@tonic-gate  * under the specified section name.
3777c478bd9Sstevel@tonic-gate  */
378*505d05c7Sgtb errcode_t KRB5_CALLCONV
379*505d05c7Sgtb profile_get_relation_names(profile_t profile, const char **names,
380*505d05c7Sgtb 			   char ***ret_names)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	errcode_t		retval;
3837c478bd9Sstevel@tonic-gate 	void			*state;
3847c478bd9Sstevel@tonic-gate 	char			*name;
3857c478bd9Sstevel@tonic-gate 	struct profile_string_list values;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	if ((retval = profile_node_iterator_create(profile, names,
3887c478bd9Sstevel@tonic-gate 		   PROFILE_ITER_LIST_SECTION | PROFILE_ITER_RELATIONS_ONLY,
3897c478bd9Sstevel@tonic-gate 		   &state)))
3907c478bd9Sstevel@tonic-gate 		return retval;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if ((retval = init_list(&values)))
3937c478bd9Sstevel@tonic-gate 		return retval;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	do {
3967c478bd9Sstevel@tonic-gate 		if ((retval = profile_node_iterator(&state, 0, &name, 0)))
3977c478bd9Sstevel@tonic-gate 			goto cleanup;
3987c478bd9Sstevel@tonic-gate 		if (name && !is_list_member(&values, name))
3997c478bd9Sstevel@tonic-gate 			add_to_list(&values, name);
4007c478bd9Sstevel@tonic-gate 	} while (state);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	end_list(&values, ret_names);
4037c478bd9Sstevel@tonic-gate 	return 0;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate cleanup:
4067c478bd9Sstevel@tonic-gate 	end_list(&values, (char ***)NULL);
4077c478bd9Sstevel@tonic-gate 	return retval;
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
410*505d05c7Sgtb errcode_t KRB5_CALLCONV
411*505d05c7Sgtb profile_iterator_create(profile_t profile, const char *const *names, int flags,
412*505d05c7Sgtb 			void **ret_iter)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	return profile_node_iterator_create(profile, names, flags, ret_iter);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
417*505d05c7Sgtb void KRB5_CALLCONV
418*505d05c7Sgtb profile_iterator_free(void **iter_p)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	profile_node_iterator_free(iter_p);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
423*505d05c7Sgtb errcode_t KRB5_CALLCONV
424*505d05c7Sgtb profile_iterator(void **iter_p, char **ret_name, char **ret_value)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	char *name, *value;
4277c478bd9Sstevel@tonic-gate 	errcode_t	retval;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	retval = profile_node_iterator(iter_p, 0, &name, &value);
4307c478bd9Sstevel@tonic-gate 	if (retval)
4317c478bd9Sstevel@tonic-gate 		return retval;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if (ret_name) {
4347c478bd9Sstevel@tonic-gate 		if (name) {
435*505d05c7Sgtb 		  *ret_name = (char *) malloc(strlen(name)+1);
4367c478bd9Sstevel@tonic-gate 			if (!*ret_name)
4377c478bd9Sstevel@tonic-gate 				return ENOMEM;
4387c478bd9Sstevel@tonic-gate 			strcpy(*ret_name, name);
4397c478bd9Sstevel@tonic-gate 		} else
4407c478bd9Sstevel@tonic-gate 			*ret_name = 0;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 	if (ret_value) {
4437c478bd9Sstevel@tonic-gate 		if (value) {
444*505d05c7Sgtb 		  *ret_value = (char *) malloc(strlen(value)+1);
4457c478bd9Sstevel@tonic-gate 			if (!*ret_value) {
4467c478bd9Sstevel@tonic-gate 				if (ret_name) {
4477c478bd9Sstevel@tonic-gate 					free(*ret_name);
4487c478bd9Sstevel@tonic-gate 					*ret_name = 0;
4497c478bd9Sstevel@tonic-gate 				}
4507c478bd9Sstevel@tonic-gate 				return ENOMEM;
4517c478bd9Sstevel@tonic-gate 			}
4527c478bd9Sstevel@tonic-gate 			strcpy(*ret_value, value);
4537c478bd9Sstevel@tonic-gate 		} else
4547c478bd9Sstevel@tonic-gate 			*ret_value = 0;
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 	return 0;
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
459*505d05c7Sgtb void KRB5_CALLCONV
460*505d05c7Sgtb profile_release_string(char *str)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	free(str);
4637c478bd9Sstevel@tonic-gate }
464