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 /*
17*bfc032a1SShawn Emery  * Solaris Kerberos: The following functions are made public so that other
18*bfc032a1SShawn Emery  * profile functions can call upon these basic routines:
19*bfc032a1SShawn Emery  *	init_list(), end_list(), and add_to_list().
20*bfc032a1SShawn Emery  * Note: That profile_string_list is moved to prof_int.h as a result.
21*bfc032a1SShawn Emery  *
227c478bd9Sstevel@tonic-gate  * These functions --- init_list(), end_list(), and add_to_list() are
23*bfc032a1SShawn 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)33*bfc032a1SShawn 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)48*bfc032a1SShawn 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)70*bfc032a1SShawn 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;
747c478bd9Sstevel@tonic-gate 
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;
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
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;
1207c478bd9Sstevel@tonic-gate 
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;
1577c478bd9Sstevel@tonic-gate 
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;
1867c478bd9Sstevel@tonic-gate 
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;
2137c478bd9Sstevel@tonic-gate 
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;
264505d05c7Sgtb 
2657c478bd9Sstevel@tonic-gate 
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)
282