10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * prof_get.c --- routines that expose the public interfaces for
30Sstevel@tonic-gate * querying items from the profile.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate */
60Sstevel@tonic-gate
7781Sgtb #include "prof_int.h"
80Sstevel@tonic-gate #include <stdio.h>
90Sstevel@tonic-gate #include <string.h>
100Sstevel@tonic-gate #ifdef HAVE_STDLIB_H
110Sstevel@tonic-gate #include <stdlib.h>
120Sstevel@tonic-gate #endif
130Sstevel@tonic-gate #include <errno.h>
14781Sgtb #include <limits.h>
150Sstevel@tonic-gate
160Sstevel@tonic-gate /*
17*12568SShawn.Emery@Sun.COM * Solaris Kerberos: The following functions are made public so that other
18*12568SShawn.Emery@Sun.COM * profile functions can call upon these basic routines:
19*12568SShawn.Emery@Sun.COM * init_list(), end_list(), and add_to_list().
20*12568SShawn.Emery@Sun.COM * Note: That profile_string_list is moved to prof_int.h as a result.
210Sstevel@tonic-gate *
22*12568SShawn.Emery@Sun.COM * These functions --- init_list(), end_list(), and add_to_list() are
23*12568SShawn.Emery@Sun.COM * publicy exported functions used to build up a null-terminated char ** list
24*12568SShawn.Emery@Sun.COM * of strings to be returned by functions like profile_get_values.
250Sstevel@tonic-gate *
260Sstevel@tonic-gate * The publicly exported interface for freeing char** list is
270Sstevel@tonic-gate * profile_free_list().
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * Initialize the string list abstraction.
320Sstevel@tonic-gate */
init_list(struct profile_string_list * list)33*12568SShawn.Emery@Sun.COM errcode_t init_list(struct profile_string_list *list)
340Sstevel@tonic-gate {
350Sstevel@tonic-gate list->num = 0;
360Sstevel@tonic-gate list->max = 10;
377934SMark.Phalan@Sun.COM list->list = malloc(list->max * sizeof(char *));
380Sstevel@tonic-gate if (list->list == 0)
390Sstevel@tonic-gate return ENOMEM;
400Sstevel@tonic-gate list->list[0] = 0;
410Sstevel@tonic-gate return 0;
420Sstevel@tonic-gate }
430Sstevel@tonic-gate
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate * Free any memory left over in the string abstraction, returning the
460Sstevel@tonic-gate * built up list in *ret_list if it is non-null.
470Sstevel@tonic-gate */
end_list(struct profile_string_list * list,char *** ret_list)48*12568SShawn.Emery@Sun.COM void end_list(struct profile_string_list *list, char ***ret_list)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate char **cp;
510Sstevel@tonic-gate
520Sstevel@tonic-gate if (list == 0)
530Sstevel@tonic-gate return;
540Sstevel@tonic-gate
550Sstevel@tonic-gate if (ret_list) {
560Sstevel@tonic-gate *ret_list = list->list;
570Sstevel@tonic-gate return;
580Sstevel@tonic-gate } else {
590Sstevel@tonic-gate for (cp = list->list; *cp; cp++)
600Sstevel@tonic-gate free(*cp);
610Sstevel@tonic-gate free(list->list);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate list->num = list->max = 0;
640Sstevel@tonic-gate list->list = 0;
650Sstevel@tonic-gate }
660Sstevel@tonic-gate
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate * Add a string to the list.
690Sstevel@tonic-gate */
add_to_list(struct profile_string_list * list,const char * str)70*12568SShawn.Emery@Sun.COM errcode_t add_to_list(struct profile_string_list *list, const char *str)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate char *newstr, **newlist;
730Sstevel@tonic-gate int newmax;
740Sstevel@tonic-gate
750Sstevel@tonic-gate if (list->num+1 >= list->max) {
760Sstevel@tonic-gate newmax = list->max + 10;
777934SMark.Phalan@Sun.COM newlist = realloc(list->list, newmax * sizeof(char *));
780Sstevel@tonic-gate if (newlist == 0)
790Sstevel@tonic-gate return ENOMEM;
800Sstevel@tonic-gate list->max = newmax;
810Sstevel@tonic-gate list->list = newlist;
820Sstevel@tonic-gate }
837934SMark.Phalan@Sun.COM newstr = malloc(strlen(str)+1);
840Sstevel@tonic-gate if (newstr == 0)
850Sstevel@tonic-gate return ENOMEM;
860Sstevel@tonic-gate strcpy(newstr, str);
870Sstevel@tonic-gate
880Sstevel@tonic-gate list->list[list->num++] = newstr;
890Sstevel@tonic-gate list->list[list->num] = 0;
900Sstevel@tonic-gate return 0;
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate * Return TRUE if the string is already a member of the list.
950Sstevel@tonic-gate */
is_list_member(struct profile_string_list * list,const char * str)96781Sgtb static int is_list_member(struct profile_string_list *list, const char *str)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate char **cpp;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate if (!list->list)
1010Sstevel@tonic-gate return 0;
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate for (cpp = list->list; *cpp; cpp++) {
1040Sstevel@tonic-gate if (!strcmp(*cpp, str))
1050Sstevel@tonic-gate return 1;
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate return 0;
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate * This function frees a null-terminated list as returned by
1120Sstevel@tonic-gate * profile_get_values.
1130Sstevel@tonic-gate */
profile_free_list(char ** list)114781Sgtb void KRB5_CALLCONV profile_free_list(char **list)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate char **cp;
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate if (list == 0)
1190Sstevel@tonic-gate return;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate for (cp = list; *cp; cp++)
1220Sstevel@tonic-gate free(*cp);
1230Sstevel@tonic-gate free(list);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
126781Sgtb errcode_t KRB5_CALLCONV
profile_get_values(profile_t profile,const char * const * names,char *** ret_values)127781Sgtb profile_get_values(profile_t profile, const char *const *names,
128781Sgtb char ***ret_values)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate errcode_t retval;
1310Sstevel@tonic-gate void *state;
1320Sstevel@tonic-gate char *value;
1330Sstevel@tonic-gate struct profile_string_list values;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate if ((retval = profile_node_iterator_create(profile, names,
1360Sstevel@tonic-gate PROFILE_ITER_RELATIONS_ONLY,
1370Sstevel@tonic-gate &state)))
1380Sstevel@tonic-gate return retval;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate if ((retval = init_list(&values)))
1410Sstevel@tonic-gate return retval;
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate do {
1440Sstevel@tonic-gate if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1450Sstevel@tonic-gate goto cleanup;
1460Sstevel@tonic-gate if (value)
1470Sstevel@tonic-gate add_to_list(&values, value);
1480Sstevel@tonic-gate } while (state);
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate if (values.num == 0) {
1510Sstevel@tonic-gate retval = PROF_NO_RELATION;
1520Sstevel@tonic-gate goto cleanup;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate end_list(&values, ret_values);
1560Sstevel@tonic-gate return 0;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate cleanup:
1597934SMark.Phalan@Sun.COM end_list(&values, 0);
1600Sstevel@tonic-gate return retval;
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate * This function only gets the first value from the file; it is a
1650Sstevel@tonic-gate * helper function for profile_get_string, profile_get_integer, etc.
1660Sstevel@tonic-gate */
profile_get_value(profile_t profile,const char ** names,const char ** ret_value)167781Sgtb errcode_t profile_get_value(profile_t profile, const char **names,
168781Sgtb const char **ret_value)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate errcode_t retval;
1710Sstevel@tonic-gate void *state;
1720Sstevel@tonic-gate char *value;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate if ((retval = profile_node_iterator_create(profile, names,
1750Sstevel@tonic-gate PROFILE_ITER_RELATIONS_ONLY,
1760Sstevel@tonic-gate &state)))
1770Sstevel@tonic-gate return retval;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1800Sstevel@tonic-gate goto cleanup;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate if (value)
1830Sstevel@tonic-gate *ret_value = value;
1840Sstevel@tonic-gate else
1850Sstevel@tonic-gate retval = PROF_NO_RELATION;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate cleanup:
1880Sstevel@tonic-gate profile_node_iterator_free(&state);
1890Sstevel@tonic-gate return retval;
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
192781Sgtb 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)193781Sgtb profile_get_string(profile_t profile, const char *name, const char *subname,
194781Sgtb const char *subsubname, const char *def_val,
195781Sgtb char **ret_string)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate const char *value;
1980Sstevel@tonic-gate errcode_t retval;
1990Sstevel@tonic-gate const char *names[4];
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (profile) {
2020Sstevel@tonic-gate names[0] = name;
2030Sstevel@tonic-gate names[1] = subname;
2040Sstevel@tonic-gate names[2] = subsubname;
2050Sstevel@tonic-gate names[3] = 0;
2060Sstevel@tonic-gate retval = profile_get_value(profile, names, &value);
2070Sstevel@tonic-gate if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
2080Sstevel@tonic-gate value = def_val;
2090Sstevel@tonic-gate else if (retval)
2100Sstevel@tonic-gate return retval;
2110Sstevel@tonic-gate } else
2120Sstevel@tonic-gate value = def_val;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate if (value) {
2157934SMark.Phalan@Sun.COM *ret_string = malloc(strlen(value)+1);
2160Sstevel@tonic-gate if (*ret_string == 0)
2170Sstevel@tonic-gate return ENOMEM;
2180Sstevel@tonic-gate strcpy(*ret_string, value);
2190Sstevel@tonic-gate } else
2200Sstevel@tonic-gate *ret_string = 0;
2210Sstevel@tonic-gate return 0;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
224781Sgtb 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)225781Sgtb profile_get_integer(profile_t profile, const char *name, const char *subname,
226781Sgtb const char *subsubname, int def_val, int *ret_int)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate const char *value;
2290Sstevel@tonic-gate errcode_t retval;
2300Sstevel@tonic-gate const char *names[4];
231781Sgtb char *end_value;
232781Sgtb long ret_long;
2330Sstevel@tonic-gate
234781Sgtb *ret_int = def_val;
235781Sgtb if (profile == 0)
2360Sstevel@tonic-gate return 0;
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate names[0] = name;
2390Sstevel@tonic-gate names[1] = subname;
2400Sstevel@tonic-gate names[2] = subsubname;
2410Sstevel@tonic-gate names[3] = 0;
2420Sstevel@tonic-gate retval = profile_get_value(profile, names, &value);
2430Sstevel@tonic-gate if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
2440Sstevel@tonic-gate *ret_int = def_val;
2450Sstevel@tonic-gate return 0;
2460Sstevel@tonic-gate } else if (retval)
2470Sstevel@tonic-gate return retval;
248781Sgtb
249781Sgtb if (value[0] == 0)
250781Sgtb /* Empty string is no good. */
251781Sgtb return PROF_BAD_INTEGER;
252781Sgtb errno = 0;
253781Sgtb ret_long = strtol (value, &end_value, 10);
254781Sgtb
255781Sgtb /* Overflow or underflow. */
256781Sgtb if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
257781Sgtb return PROF_BAD_INTEGER;
258781Sgtb /* Value outside "int" range. */
259781Sgtb if ((long) (int) ret_long != ret_long)
260781Sgtb return PROF_BAD_INTEGER;
261781Sgtb /* Garbage in string. */
262781Sgtb if (end_value != value + strlen (value))
263781Sgtb return PROF_BAD_INTEGER;
264781Sgtb
2650Sstevel@tonic-gate
266781Sgtb *ret_int = ret_long;
2670Sstevel@tonic-gate return 0;
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate
270781Sgtb static const char *const conf_yes[] = {
271781Sgtb "y", "yes", "true", "t", "1", "on",
272781Sgtb 0,
273781Sgtb };
274781Sgtb
275781Sgtb static const char *const conf_no[] = {
276781Sgtb "n", "no", "false", "nil", "0", "off",
277781Sgtb 0,
278781Sgtb };
279781Sgtb
280781Sgtb static errcode_t
profile_parse_boolean(const char * s,int * ret_boolean)281781Sgtb profile_parse_boolean(const char *s, int *ret_boolean)
282781Sgtb {
283781Sgtb const char *const *p;
284781Sgtb
285781Sgtb if (ret_boolean == NULL)
286781Sgtb return PROF_EINVAL;
287781Sgtb
288781Sgtb for(p=conf_yes; *p; p++) {
289781Sgtb if (!strcasecmp(*p,s)) {
290781Sgtb *ret_boolean = 1;
291781Sgtb return 0;
292781Sgtb }
293781Sgtb }
294781Sgtb
295781Sgtb for(p=conf_no; *p; p++) {
296781Sgtb if (!strcasecmp(*p,s)) {
297781Sgtb *ret_boolean = 0;
298781Sgtb return 0;
299781Sgtb }
300781Sgtb }
301781Sgtb
302781Sgtb return PROF_BAD_BOOLEAN;
303781Sgtb }
304781Sgtb
305781Sgtb 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)306781Sgtb profile_get_boolean(profile_t profile, const char *name, const char *subname,
307781Sgtb const char *subsubname, int def_val, int *ret_boolean)
308781Sgtb {
309781Sgtb const char *value;
310781Sgtb errcode_t retval;
311781Sgtb const char *names[4];
312781Sgtb
313781Sgtb if (profile == 0) {
314781Sgtb *ret_boolean = def_val;
315781Sgtb return 0;
316781Sgtb }
317781Sgtb
318781Sgtb names[0] = name;
319781Sgtb names[1] = subname;
320781Sgtb names[2] = subsubname;
321781Sgtb names[3] = 0;
322781Sgtb retval = profile_get_value(profile, names, &value);
323781Sgtb if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
324781Sgtb *ret_boolean = def_val;
325781Sgtb return 0;
326781Sgtb } else if (retval)
327781Sgtb return retval;
328781Sgtb
329781Sgtb return profile_parse_boolean (value, ret_boolean);
330781Sgtb }
331781Sgtb
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate * This function will return the list of the names of subections in the
3340Sstevel@tonic-gate * under the specified section name.
3350Sstevel@tonic-gate */
336781Sgtb errcode_t KRB5_CALLCONV
profile_get_subsection_names(profile_t profile,const char ** names,char *** ret_names)337781Sgtb profile_get_subsection_names(profile_t profile, const char **names,
338781Sgtb char ***ret_names)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate errcode_t retval;
3410Sstevel@tonic-gate void *state;
3420Sstevel@tonic-gate char *name;
3430Sstevel@tonic-gate struct profile_string_list values;
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate if ((retval = profile_node_iterator_create(profile, names,
3460Sstevel@tonic-gate PROFILE_ITER_LIST_SECTION | PROFILE_ITER_SECTIONS_ONLY,
3470Sstevel@tonic-gate &state)))
3480Sstevel@tonic-gate return retval;
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate if ((retval = init_list(&values)))
3510Sstevel@tonic-gate return retval;
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate do {
3540Sstevel@tonic-gate if ((retval = profile_node_iterator(&state, 0, &name, 0)))
3550Sstevel@tonic-gate goto cleanup;
3560Sstevel@tonic-gate if (name)
3570Sstevel@tonic-gate add_to_list(&values, name);
3580Sstevel@tonic-gate } while (state);
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate end_list(&values, ret_names);
3610Sstevel@tonic-gate return 0;
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate cleanup:
3647934SMark.Phalan@Sun.COM end_list(&values, 0);
3650Sstevel@tonic-gate return retval;
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate * This function will return the list of the names of relations in the
3700Sstevel@tonic-gate * under the specified section name.
3710Sstevel@tonic-gate */
372781Sgtb errcode_t KRB5_CALLCONV
profile_get_relation_names(profile_t profile,const char ** names,char *** ret_names)373781Sgtb profile_get_relation_names(profile_t profile, const char **names,
374781Sgtb char ***ret_names)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate errcode_t retval;
3770Sstevel@tonic-gate void *state;
3780Sstevel@tonic-gate char *name;
3790Sstevel@tonic-gate struct profile_string_list values;
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate if ((retval = profile_node_iterator_create(profile, names,
3820Sstevel@tonic-gate PROFILE_ITER_LIST_SECTION | PROFILE_ITER_RELATIONS_ONLY,
3830Sstevel@tonic-gate &state)))
3840Sstevel@tonic-gate return retval;
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate if ((retval = init_list(&values)))
3870Sstevel@tonic-gate return retval;
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate do {
3900Sstevel@tonic-gate if ((retval = profile_node_iterator(&state, 0, &name, 0)))
3910Sstevel@tonic-gate goto cleanup;
3920Sstevel@tonic-gate if (name && !is_list_member(&values, name))
3930Sstevel@tonic-gate add_to_list(&values, name);
3940Sstevel@tonic-gate } while (state);
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate end_list(&values, ret_names);
3970Sstevel@tonic-gate return 0;
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate cleanup:
4007934SMark.Phalan@Sun.COM end_list(&values, 0);
4010Sstevel@tonic-gate return retval;
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate
404781Sgtb errcode_t KRB5_CALLCONV
profile_iterator_create(profile_t profile,const char * const * names,int flags,void ** ret_iter)405781Sgtb profile_iterator_create(profile_t profile, const char *const *names, int flags,
406781Sgtb void **ret_iter)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate return profile_node_iterator_create(profile, names, flags, ret_iter);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate
411781Sgtb void KRB5_CALLCONV
profile_iterator_free(void ** iter_p)412781Sgtb profile_iterator_free(void **iter_p)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate profile_node_iterator_free(iter_p);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate
417781Sgtb errcode_t KRB5_CALLCONV
profile_iterator(void ** iter_p,char ** ret_name,char ** ret_value)418781Sgtb profile_iterator(void **iter_p, char **ret_name, char **ret_value)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate char *name, *value;
4210Sstevel@tonic-gate errcode_t retval;
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate retval = profile_node_iterator(iter_p, 0, &name, &value);
4240Sstevel@tonic-gate if (retval)
4250Sstevel@tonic-gate return retval;
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate if (ret_name) {
4280Sstevel@tonic-gate if (name) {
4297934SMark.Phalan@Sun.COM *ret_name = malloc(strlen(name)+1);
4300Sstevel@tonic-gate if (!*ret_name)
4310Sstevel@tonic-gate return ENOMEM;
4320Sstevel@tonic-gate strcpy(*ret_name, name);
4330Sstevel@tonic-gate } else
4340Sstevel@tonic-gate *ret_name = 0;
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate if (ret_value) {
4370Sstevel@tonic-gate if (value) {
4387934SMark.Phalan@Sun.COM *ret_value = malloc(strlen(value)+1);
4390Sstevel@tonic-gate if (!*ret_value) {
4400Sstevel@tonic-gate if (ret_name) {
4410Sstevel@tonic-gate free(*ret_name);
4420Sstevel@tonic-gate *ret_name = 0;
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate return ENOMEM;
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate strcpy(*ret_value, value);
4470Sstevel@tonic-gate } else
4480Sstevel@tonic-gate *ret_value = 0;
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate return 0;
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
453781Sgtb void KRB5_CALLCONV
profile_release_string(char * str)454781Sgtb profile_release_string(char *str)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate free(str);
4570Sstevel@tonic-gate }
458