1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/libdef_parse.c */
3 /*
4 * Copyright 2009 by the Massachusetts Institute of Technology.
5 * All Rights Reserved.
6 *
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
11 *
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
25 */
26
27 /*
28 * krb5int_libdefault_string()
29 * krb5int_libdefault_boolean()
30 *
31 */
32 #include "k5-int.h"
33 #include "int-proto.h"
34 /* For _krb5_conf_boolean prototype */
35 #include "os-proto.h"
36
37 static const char *const conf_yes[] = {
38 "y", "yes", "true", "t", "1", "on",
39 0,
40 };
41
42 static const char *const conf_no[] = {
43 "n", "no", "false", "nil", "0", "off",
44 0,
45 };
46
47 int
_krb5_conf_boolean(const char * s)48 _krb5_conf_boolean(const char *s)
49 {
50 const char *const *p;
51
52 for(p=conf_yes; *p; p++) {
53 if (!strcasecmp(*p,s))
54 return 1;
55 }
56
57 for(p=conf_no; *p; p++) {
58 if (!strcasecmp(*p,s))
59 return 0;
60 }
61
62 /* Default to "no" */
63 return 0;
64 }
65
66 krb5_error_code
krb5int_libdefault_string(krb5_context context,const krb5_data * realm,const char * option,char ** ret_value)67 krb5int_libdefault_string(krb5_context context, const krb5_data *realm,
68 const char *option, char **ret_value)
69 {
70 profile_t profile;
71 const char *names[5];
72 char **nameval = NULL;
73 krb5_error_code retval;
74 char realmstr[1024];
75
76 if (realm->length > sizeof(realmstr)-1)
77 return(EINVAL);
78
79 strncpy(realmstr, realm->data, realm->length);
80 realmstr[realm->length] = '\0';
81
82 if (!context || (context->magic != KV5M_CONTEXT))
83 return KV5M_CONTEXT;
84
85 profile = context->profile;
86
87 names[0] = KRB5_CONF_LIBDEFAULTS;
88
89 /*
90 * Try number one:
91 *
92 * [libdefaults]
93 * REALM = {
94 * option = <boolean>
95 * }
96 */
97
98 names[1] = realmstr;
99 names[2] = option;
100 names[3] = 0;
101 retval = profile_get_values(profile, names, &nameval);
102 if (retval == 0 && nameval && nameval[0])
103 goto goodbye;
104
105
106 /*
107 * Try number two:
108 *
109 * [libdefaults]
110 * option = <boolean>
111 */
112
113 names[1] = option;
114 names[2] = 0;
115 retval = profile_get_values(profile, names, &nameval);
116 if (retval == 0 && nameval && nameval[0])
117 goto goodbye;
118
119 goodbye:
120 if (!nameval)
121 return(ENOENT);
122
123 if (!nameval[0]) {
124 retval = ENOENT;
125 } else {
126 *ret_value = strdup(nameval[0]);
127 if (!*ret_value)
128 retval = ENOMEM;
129 }
130
131 profile_free_list(nameval);
132
133 return retval;
134 }
135
136 krb5_error_code
krb5int_libdefault_boolean(krb5_context context,const krb5_data * realm,const char * option,int * ret_value)137 krb5int_libdefault_boolean(krb5_context context, const krb5_data *realm,
138 const char *option, int *ret_value)
139 {
140 char *string = NULL;
141 krb5_error_code retval;
142
143 retval = krb5int_libdefault_string(context, realm, option, &string);
144
145 if (retval)
146 return(retval);
147
148 *ret_value = _krb5_conf_boolean(string);
149 free(string);
150
151 return(0);
152 }
153