17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * prof_get.c --- routines that expose the public interfaces for
37c478bd9Sstevel@tonic-gate  * 	querying items from the profile.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  */
67c478bd9Sstevel@tonic-gate 
7505d05c7Sgtb #include "prof_int.h"
87c478bd9Sstevel@tonic-gate #include <stdio.h>
97c478bd9Sstevel@tonic-gate #include <string.h>
107c478bd9Sstevel@tonic-gate #ifdef HAVE_STDLIB_H
117c478bd9Sstevel@tonic-gate #include <stdlib.h>
127c478bd9Sstevel@tonic-gate #endif
137c478bd9Sstevel@tonic-gate #include <errno.h>
14505d05c7Sgtb #include <limits.h>
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate /*
17bfc032a1SShawn Emery  * Solaris Kerberos: The following functions are made public so that other
18bfc032a1SShawn Emery  * profile functions can call upon these basic routines:
19bfc032a1SShawn Emery  *	init_list(), end_list(), and add_to_list().
20bfc032a1SShawn Emery  * Note: That profile_string_list is moved to prof_int.h as a result.
21bfc032a1SShawn Emery  *
227c478bd9Sstevel@tonic-gate  * These functions --- init_list(), end_list(), and add_to_list() are
23bfc032a1SShawn Emery  * publicy exported functions used to build up a null-terminated char ** list
247c478bd9Sstevel@tonic-gate  * of strings to be returned by functions like profile_get_values.
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 /*
317c478bd9Sstevel@tonic-gate  * Initialize the string list abstraction.
327c478bd9Sstevel@tonic-gate  */
init_list(struct profile_string_list * list)33bfc032a1SShawn Emery errcode_t init_list(struct profile_string_list *list)
347c478bd9Sstevel@tonic-gate {
357c478bd9Sstevel@tonic-gate 	list->num = 0;
367c478bd9Sstevel@tonic-gate 	list->max = 10;
37159d09a2SMark Phalan 	list->list = malloc(list->max * sizeof(char *));
387c478bd9Sstevel@tonic-gate 	if (list->list == 0)
397c478bd9Sstevel@tonic-gate 		return ENOMEM;
407c478bd9Sstevel@tonic-gate 	list->list[0] = 0;
417c478bd9Sstevel@tonic-gate 	return 0;
427c478bd9Sstevel@tonic-gate }
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Free any memory left over in the string abstraction, returning the
467c478bd9Sstevel@tonic-gate  * built up list in *ret_list if it is non-null.
477c478bd9Sstevel@tonic-gate  */
end_list(struct profile_string_list * list,char *** ret_list)48bfc032a1SShawn Emery void end_list(struct profile_string_list *list, char ***ret_list)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	char	**cp;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if (list == 0)
537c478bd9Sstevel@tonic-gate 		return;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	if (ret_list) {
567c478bd9Sstevel@tonic-gate 		*ret_list = list->list;
577c478bd9Sstevel@tonic-gate 		return;
587c478bd9Sstevel@tonic-gate 	} else {
597c478bd9Sstevel@tonic-gate 		for (cp = list->list; *cp; cp++)
607c478bd9Sstevel@tonic-gate 			free(*cp);
617c478bd9Sstevel@tonic-gate 		free(list->list);
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 	list->num = list->max = 0;
647c478bd9Sstevel@tonic-gate 	list->list = 0;
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Add a string to the list.
697c478bd9Sstevel@tonic-gate  */
add_to_list(struct profile_string_list * list,const char * str)70bfc032a1SShawn Emery errcode_t add_to_list(struct profile_string_list *list, const char *str)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	char 	*newstr, **newlist;
737c478bd9Sstevel@tonic-gate 	int	newmax;
74*55fea89dSDan Cross 
757c478bd9Sstevel@tonic-gate 	if (list->num+1 >= list->max) {
767c478bd9Sstevel@tonic-gate 		newmax = list->max + 10;
77159d09a2SMark Phalan 		newlist = realloc(list->list, newmax * sizeof(char *));
787c478bd9Sstevel@tonic-gate 		if (newlist == 0)
797c478bd9Sstevel@tonic-gate 			return ENOMEM;
807c478bd9Sstevel@tonic-gate 		list->max = newmax;
817c478bd9Sstevel@tonic-gate 		list->list = newlist;
827c478bd9Sstevel@tonic-gate 	}
83159d09a2SMark Phalan 	newstr = malloc(strlen(str)+1);
847c478bd9Sstevel@tonic-gate 	if (newstr == 0)
857c478bd9Sstevel@tonic-gate 		return ENOMEM;
867c478bd9Sstevel@tonic-gate 	strcpy(newstr, str);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	list->list[list->num++] = newstr;
897c478bd9Sstevel@tonic-gate 	list->list[list->num] = 0;
907c478bd9Sstevel@tonic-gate 	return 0;
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Return TRUE if the string is already a member of the list.
957c478bd9Sstevel@tonic-gate  */
is_list_member(struct profile_string_list * list,const char * str)96505d05c7Sgtb static int is_list_member(struct profile_string_list *list, const char *str)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	char **cpp;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (!list->list)
1017c478bd9Sstevel@tonic-gate 		return 0;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	for (cpp = list->list; *cpp; cpp++) {
1047c478bd9Sstevel@tonic-gate 		if (!strcmp(*cpp, str))
1057c478bd9Sstevel@tonic-gate 			return 1;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	return 0;
108*55fea89dSDan Cross }
109*55fea89dSDan Cross 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * This function frees a null-terminated list as returned by
1127c478bd9Sstevel@tonic-gate  * profile_get_values.
1137c478bd9Sstevel@tonic-gate  */
profile_free_list(char ** list)114505d05c7Sgtb void KRB5_CALLCONV profile_free_list(char **list)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate     char	**cp;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate     if (list == 0)
1197c478bd9Sstevel@tonic-gate 	    return;
120*55fea89dSDan Cross 
1217c478bd9Sstevel@tonic-gate     for (cp = list; *cp; cp++)
1227c478bd9Sstevel@tonic-gate 	free(*cp);
1237c478bd9Sstevel@tonic-gate     free(list);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
126505d05c7Sgtb errcode_t KRB5_CALLCONV
profile_get_values(profile_t profile,const char * const * names,char *** ret_values)127505d05c7Sgtb profile_get_values(profile_t profile, const char *const *names,
128505d05c7Sgtb 		   char ***ret_values)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	errcode_t		retval;
1317c478bd9Sstevel@tonic-gate 	void			*state;
1327c478bd9Sstevel@tonic-gate 	char			*value;
1337c478bd9Sstevel@tonic-gate 	struct profile_string_list values;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if ((retval = profile_node_iterator_create(profile, names,
1367c478bd9Sstevel@tonic-gate 						   PROFILE_ITER_RELATIONS_ONLY,
1377c478bd9Sstevel@tonic-gate 						   &state)))
1387c478bd9Sstevel@tonic-gate 		return retval;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if ((retval = init_list(&values)))
1417c478bd9Sstevel@tonic-gate 		return retval;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	do {
1447c478bd9Sstevel@tonic-gate 		if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1457c478bd9Sstevel@tonic-gate 			goto cleanup;
1467c478bd9Sstevel@tonic-gate 		if (value)
1477c478bd9Sstevel@tonic-gate 			add_to_list(&values, value);
1487c478bd9Sstevel@tonic-gate 	} while (state);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (values.num == 0) {
1517c478bd9Sstevel@tonic-gate 		retval = PROF_NO_RELATION;
1527c478bd9Sstevel@tonic-gate 		goto cleanup;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	end_list(&values, ret_values);
1567c478bd9Sstevel@tonic-gate 	return 0;
157*55fea89dSDan Cross 
1587c478bd9Sstevel@tonic-gate cleanup:
159159d09a2SMark Phalan 	end_list(&values, 0);
1607c478bd9Sstevel@tonic-gate 	return retval;
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * This function only gets the first value from the file; it is a
1657c478bd9Sstevel@tonic-gate  * helper function for profile_get_string, profile_get_integer, etc.
1667c478bd9Sstevel@tonic-gate  */
profile_get_value(profile_t profile,const char ** names,const char ** ret_value)167505d05c7Sgtb errcode_t profile_get_value(profile_t profile, const char **names,
168505d05c7Sgtb 			    const char **ret_value)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	errcode_t		retval;
1717c478bd9Sstevel@tonic-gate 	void			*state;
1727c478bd9Sstevel@tonic-gate 	char			*value;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if ((retval = profile_node_iterator_create(profile, names,
1757c478bd9Sstevel@tonic-gate 						   PROFILE_ITER_RELATIONS_ONLY,
1767c478bd9Sstevel@tonic-gate 						   &state)))
1777c478bd9Sstevel@tonic-gate 		return retval;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1807c478bd9Sstevel@tonic-gate 		goto cleanup;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (value)
1837c478bd9Sstevel@tonic-gate 		*ret_value = value;
1847c478bd9Sstevel@tonic-gate 	else
1857c478bd9Sstevel@tonic-gate 		retval = PROF_NO_RELATION;
186*55fea89dSDan Cross 
1877c478bd9Sstevel@tonic-gate cleanup:
1887c478bd9Sstevel@tonic-gate 	profile_node_iterator_free(&state);
1897c478bd9Sstevel@tonic-gate 	return retval;
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
192505d05c7Sgtb errcode_t KRB5_CALLCONV
profile_get_string(profile_t profile,const char * name,const char * subname,const char * subsubname,const char * def_val,char ** ret_string)193505d05c7Sgtb profile_get_string(profile_t profile, const char *name, const char *subname,
194505d05c7Sgtb 		   const char *subsubname, const char *def_val,
195505d05c7Sgtb 		   char **ret_string)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	const char	*value;
1987c478bd9Sstevel@tonic-gate 	errcode_t	retval;
1997c478bd9Sstevel@tonic-gate 	const char	*names[4];
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (profile) {
2027c478bd9Sstevel@tonic-gate 		names[0] = name;
2037c478bd9Sstevel@tonic-gate 		names[1] = subname;
2047c478bd9Sstevel@tonic-gate 		names[2] = subsubname;
2057c478bd9Sstevel@tonic-gate 		names[3] = 0;
2067c478bd9Sstevel@tonic-gate 		retval = profile_get_value(profile, names, &value);
2077c478bd9Sstevel@tonic-gate 		if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
2087c478bd9Sstevel@tonic-gate 			value = def_val;
2097c478bd9Sstevel@tonic-gate 		else if (retval)
2107c478bd9Sstevel@tonic-gate 			return retval;
2117c478bd9Sstevel@tonic-gate 	} else
2127c478bd9Sstevel@tonic-gate 		value = def_val;
213*55fea89dSDan Cross 
2147c478bd9Sstevel@tonic-gate 	if (value) {
215159d09a2SMark Phalan 		*ret_string = malloc(strlen(value)+1);
2167c478bd9Sstevel@tonic-gate 		if (*ret_string == 0)
2177c478bd9Sstevel@tonic-gate 			return ENOMEM;
2187c478bd9Sstevel@tonic-gate 		strcpy(*ret_string, value);
2197c478bd9Sstevel@tonic-gate 	} else
2207c478bd9Sstevel@tonic-gate 		*ret_string = 0;
2217c478bd9Sstevel@tonic-gate 	return 0;
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
224505d05c7Sgtb errcode_t KRB5_CALLCONV
profile_get_integer(profile_t profile,const char * name,const char * subname,const char * subsubname,int def_val,int * ret_int)225505d05c7Sgtb profile_get_integer(profile_t profile, const char *name, const char *subname,
226505d05c7Sgtb 		    const char *subsubname, int def_val, int *ret_int)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	const char	*value;
2297c478bd9Sstevel@tonic-gate 	errcode_t	retval;
2307c478bd9Sstevel@tonic-gate 	const char	*names[4];
231505d05c7Sgtb 	char            *end_value;
232505d05c7Sgtb 	long		ret_long;
2337c478bd9Sstevel@tonic-gate 
234505d05c7Sgtb 	*ret_int = def_val;
235505d05c7Sgtb 	if (profile == 0)
2367c478bd9Sstevel@tonic-gate 		return 0;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	names[0] = name;
2397c478bd9Sstevel@tonic-gate 	names[1] = subname;
2407c478bd9Sstevel@tonic-gate 	names[2] = subsubname;
2417c478bd9Sstevel@tonic-gate 	names[3] = 0;
2427c478bd9Sstevel@tonic-gate 	retval = profile_get_value(profile, names, &value);
2437c478bd9Sstevel@tonic-gate 	if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
2447c478bd9Sstevel@tonic-gate 		*ret_int = def_val;
2457c478bd9Sstevel@tonic-gate 		return 0;
2467c478bd9Sstevel@tonic-gate 	} else if (retval)
2477c478bd9Sstevel@tonic-gate 		return retval;
248505d05c7Sgtb 
249505d05c7Sgtb 	if (value[0] == 0)
250505d05c7Sgtb 	    /* Empty string is no good.  */
251505d05c7Sgtb 	    return PROF_BAD_INTEGER;
252505d05c7Sgtb 	errno = 0;
253505d05c7Sgtb 	ret_long = strtol (value, &end_value, 10);
254505d05c7Sgtb 
255505d05c7Sgtb 	/* Overflow or underflow.  */
256505d05c7Sgtb 	if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
257505d05c7Sgtb 	    return PROF_BAD_INTEGER;
258505d05c7Sgtb 	/* Value outside "int" range.  */
259505d05c7Sgtb 	if ((long) (int) ret_long != ret_long)
260505d05c7Sgtb 	    return PROF_BAD_INTEGER;
261505d05c7Sgtb 	/* Garbage in string.  */
262505d05c7Sgtb 	if (end_value != value + strlen (value))
263505d05c7Sgtb 	    return PROF_BAD_INTEGER;
264*55fea89dSDan Cross 
265*55fea89dSDan Cross 
266505d05c7Sgtb 	*ret_int = ret_long;
2677c478bd9Sstevel@tonic-gate 	return 0;
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
270505d05c7Sgtb static const char *const conf_yes[] = {
271505d05c7Sgtb     "y", "yes", "true", "t", "1", "on",
272505d05c7Sgtb     0,
273505d05c7Sgtb };
274505d05c7Sgtb 
275505d05c7Sgtb static const char *const conf_no[] = {
276505d05c7Sgtb     "n", "no", "false", "nil", "0", "off",
277505d05c7Sgtb     0,
278505d05c7Sgtb };
279505d05c7Sgtb 
280505d05c7Sgtb static errcode_t
profile_parse_boolean(const char * s,int * ret_boolean)281505d05c7Sgtb profile_parse_boolean(const char *s, int *ret_boolean)
282505d05c7Sgtb {
283505d05c7Sgtb     const char *const *p;
284*55fea89dSDan Cross 
285505d05c7Sgtb     if (ret_boolean == NULL)
286505d05c7Sgtb     	return PROF_EINVAL;
287505d05c7Sgtb 
288505d05c7Sgtb     for(p=conf_yes; *p; p++) {
289505d05c7Sgtb 		if (!strcasecmp(*p,s)) {
290505d05c7Sgtb 			*ret_boolean = 1;
291505d05c7Sgtb 	    	return 0;
292505d05c7Sgtb 		}
293505d05c7Sgtb     }
294505d05c7Sgtb 
295505d05c7Sgtb     for(p=conf_no; *p; p++) {
296505d05c7Sgtb 		if (!strcasecmp(*p,s)) {
297505d05c7Sgtb 			*ret_boolean = 0;
298505d05c7Sgtb 			return 0;
299505d05c7Sgtb 		}
300505d05c7Sgtb     }
301*55fea89dSDan Cross 
302505d05c7Sgtb 	return PROF_BAD_BOOLEAN;
303505d05c7Sgtb }
304505d05c7Sgtb 
305505d05c7Sgtb errcode_t KRB5_CALLCONV
profile_get_boolean(profile_t profile,const char * name,const char * subname,const char * subsubname,int def_val,int * ret_boolean)306505d05c7Sgtb profile_get_boolean(profile_t profile, const char *name, const char *subname,
307505d05c7Sgtb 		    const char *subsubname, int def_val, int *ret_boolean)
308505d05c7Sgtb {
309505d05c7Sgtb 	const char	*value;
310505d05c7Sgtb 	errcode_t	retval;
311505d05c7Sgtb 	const char	*names[4];
312505d05c7Sgtb 
313505d05c7Sgtb 	if (profile == 0) {
314505d05c7Sgtb 		*ret_boolean = def_val;
315505d05c7Sgtb 		return 0;
316505d05c7Sgtb 	}
317505d05c7Sgtb 
318505d05c7Sgtb 	names[0] = name;
319505d05c7Sgtb 	names[1] = subname;
320505d05c7Sgtb 	names[2] = subsubname;
321505d05c7Sgtb 	names[3] = 0;
322505d05c7Sgtb 	retval = profile_get_value(profile, names, &value);
323505d05c7Sgtb 	if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
324505d05c7Sgtb 		*ret_boolean = def_val;
325505d05c7Sgtb 		return 0;
326505d05c7Sgtb 	} else if (retval)
327505d05c7Sgtb 		return retval;
328*55fea89dSDan Cross 
329505d05c7Sgtb 	return profile_parse_boolean (value, ret_boolean);
330505d05c7Sgtb }
331505d05c7Sgtb 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * This function will return the list of the names of subections in the
3347c478bd9Sstevel@tonic-gate  * under the specified section name.
3357c478bd9Sstevel@tonic-gate  */
336505d05c7Sgtb errcode_t KRB5_CALLCONV
profile_get_subsection_names(profile_t profile,const char ** names,char *** ret_names)337505d05c7Sgtb profile_get_subsection_names(profile_t profile, const char **names,
338505d05c7Sgtb 			     char ***ret_names)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	errcode_t		retval;
3417c478bd9Sstevel@tonic-gate 	void			*state;
3427c478bd9Sstevel@tonic-gate 	char			*name;
3437c478bd9Sstevel@tonic-gate 	struct profile_string_list values;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if ((retval = profile_node_iterator_create(profile, names,
3467c478bd9Sstevel@tonic-gate 		   PROFILE_ITER_LIST_SECTION | PROFILE_ITER_SECTIONS_ONLY,
3477c478bd9Sstevel@tonic-gate 		   &state)))
3487c478bd9Sstevel@tonic-gate 		return retval;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if ((retval = init_list(&values)))
3517c478bd9Sstevel@tonic-gate 		return retval;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	do {
3547c478bd9Sstevel@tonic-gate 		if ((retval = profile_node_iterator(&state, 0, &name, 0)))
3557c478bd9Sstevel@tonic-gate 			goto cleanup;
3567c478bd9Sstevel@tonic-gate 		if (name)
3577c478bd9Sstevel@tonic-gate 			add_to_list(&values, name);
3587c478bd9Sstevel@tonic-gate 	} while (state);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	end_list(&values, ret_names);
3617c478bd9Sstevel@tonic-gate 	return 0;
362*55fea89dSDan Cross 
3637c478bd9Sstevel@tonic-gate cleanup:
364159d09a2SMark Phalan 	end_list(&values, 0);
3657c478bd9Sstevel@tonic-gate 	return retval;
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * This function will return the list of the names of relations in the
3707c478bd9Sstevel@tonic-gate  * under the specified section name.
3717c478bd9Sstevel@tonic-gate  */
372505d05c7Sgtb errcode_t KRB5_CALLCONV
profile_get_relation_names(profile_t profile,const char ** names,char *** ret_names)373505d05c7Sgtb profile_get_relation_names(profile_t profile, const char **names,
374505d05c7Sgtb 			   char ***ret_names)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	errcode_t		retval;
3777c478bd9Sstevel@tonic-gate 	void			*state;
3787c478bd9Sstevel@tonic-gate 	char			*name;
3797c478bd9Sstevel@tonic-gate 	struct profile_string_list values;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	if ((retval = profile_node_iterator_create(profile, names,
3827c478bd9Sstevel@tonic-gate 		   PROFILE_ITER_LIST_SECTION | PROFILE_ITER_RELATIONS_ONLY,
3837c478bd9Sstevel@tonic-gate 		   &state)))
3847c478bd9Sstevel@tonic-gate 		return retval;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if ((retval = init_list(&values)))
3877c478bd9Sstevel@tonic-gate 		return retval;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	do {
3907c478bd9Sstevel@tonic-gate 		if ((retval = profile_node_iterator(&state, 0, &name, 0)))
3917c478bd9Sstevel@tonic-gate 			goto cleanup;
3927c478bd9Sstevel@tonic-gate 		if (name && !is_list_member(&values, name))
3937c478bd9Sstevel@tonic-gate 			add_to_list(&values, name);
3947c478bd9Sstevel@tonic-gate 	} while (state);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	end_list(&values, ret_names);
3977c478bd9Sstevel@tonic-gate 	return 0;
398*55fea89dSDan Cross 
3997c478bd9Sstevel@tonic-gate cleanup:
400159d09a2SMark Phalan 	end_list(&values, 0);
4017c478bd9Sstevel@tonic-gate 	return retval;
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
404505d05c7Sgtb errcode_t KRB5_CALLCONV
profile_iterator_create(profile_t profile,const char * const * names,int flags,void ** ret_iter)405505d05c7Sgtb profile_iterator_create(profile_t profile, const char *const *names, int flags,
406505d05c7Sgtb 			void **ret_iter)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	return profile_node_iterator_create(profile, names, flags, ret_iter);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
411505d05c7Sgtb void KRB5_CALLCONV
profile_iterator_free(void ** iter_p)412505d05c7Sgtb profile_iterator_free(void **iter_p)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	profile_node_iterator_free(iter_p);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
417505d05c7Sgtb errcode_t KRB5_CALLCONV
profile_iterator(void ** iter_p,char ** ret_name,char ** ret_value)418505d05c7Sgtb profile_iterator(void **iter_p, char **ret_name, char **ret_value)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	char *name, *value;
4217c478bd9Sstevel@tonic-gate 	errcode_t	retval;
422*55fea89dSDan Cross 
4237c478bd9Sstevel@tonic-gate 	retval = profile_node_iterator(iter_p, 0, &name, &value);
4247c478bd9Sstevel@tonic-gate 	if (retval)
4257c478bd9Sstevel@tonic-gate 		return retval;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (ret_name) {
4287c478bd9Sstevel@tonic-gate 		if (name) {
429159d09a2SMark Phalan 			*ret_name = malloc(strlen(name)+1);
4307c478bd9Sstevel@tonic-gate 			if (!*ret_name)
4317c478bd9Sstevel@tonic-gate 				return ENOMEM;
4327c478bd9Sstevel@tonic-gate 			strcpy(*ret_name, name);
4337c478bd9Sstevel@tonic-gate 		} else
4347c478bd9Sstevel@tonic-gate 			*ret_name = 0;
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 	if (ret_value) {
4377c478bd9Sstevel@tonic-gate 		if (value) {
438159d09a2SMark Phalan 			*ret_value = malloc(strlen(value)+1);
4397c478bd9Sstevel@tonic-gate 			if (!*ret_value) {
4407c478bd9Sstevel@tonic-gate 				if (ret_name) {
4417c478bd9Sstevel@tonic-gate 					free(*ret_name);
4427c478bd9Sstevel@tonic-gate 					*ret_name = 0;
4437c478bd9Sstevel@tonic-gate 				}
4447c478bd9Sstevel@tonic-gate 				return ENOMEM;
4457c478bd9Sstevel@tonic-gate 			}
4467c478bd9Sstevel@tonic-gate 			strcpy(*ret_value, value);
4477c478bd9Sstevel@tonic-gate 		} else
4487c478bd9Sstevel@tonic-gate 			*ret_value = 0;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 	return 0;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
453505d05c7Sgtb void KRB5_CALLCONV
profile_release_string(char * str)454505d05c7Sgtb profile_release_string(char *str)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	free(str);
4577c478bd9Sstevel@tonic-gate }
458