Initial import from FreeBSD RELENG_4:
[games.git] / crypto / kerberosIV / lib / krb / extra.c
1 /*
2  * Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 #include "krb_locl.h"
35
36 RCSID("$Id: extra.c,v 1.7.2.1 2000/12/07 16:06:09 assar Exp $");
37
38 struct value {
39     char *variable;
40     char *value;
41     struct value *next;
42 };
43
44 static struct value *_extra_values;
45
46 static int _krb_extra_read = 0;
47
48 static int
49 define_variable(const char *variable, const char *value)
50 {
51     struct value *e;
52     e = malloc(sizeof(*e));
53     if(e == NULL)
54         return ENOMEM;
55     e->variable = strdup(variable);
56     if(e->variable == NULL) {
57         free(e);
58         return ENOMEM;
59     }
60     e->value = strdup(value);
61     if(e->value == NULL) {
62         free(e->variable);
63         free(e);
64         return ENOMEM;
65     }
66     e->next = _extra_values;
67     _extra_values = e;
68     return 0;
69 }
70
71 #ifndef WIN32
72
73 static int
74 read_extra_file(void)
75 {
76     int i = 0;
77     char file[128];
78     char line[1024];
79     if(_krb_extra_read)
80         return 0;
81     _krb_extra_read = 1;
82     while(krb_get_krbextra(i++, file, sizeof(file)) == 0) {
83         FILE *f = fopen(file, "r");
84         if(f == NULL)
85             continue;
86         while(fgets(line, sizeof(line), f)) {
87             char *var, *tmp, *val;
88
89             /* skip initial whitespace */
90             var = line + strspn(line, " \t");
91             /* skip non-whitespace */
92             tmp = var + strcspn(var, " \t=");
93             /* skip whitespace */
94             val = tmp + strspn(tmp, " \t=");
95             *tmp = '\0';
96             tmp = val + strcspn(val, " \t\n");
97             *tmp = '\0';
98             if(*var == '\0' || *var == '#' || *val == '\0')
99                 continue;
100             if(krb_debug)
101                 krb_warning("%s: setting `%s' to `%s'\n", file, var, val);
102             define_variable(var, val);
103         }
104         fclose(f);
105         return 0;
106     }
107     return ENOENT;
108 }
109
110 #else /* WIN32 */
111
112 static int
113 read_extra_file(void)
114 {
115     char name[1024], data[1024];
116     DWORD name_sz, data_sz;
117     DWORD type;
118     int num = 0;
119     HKEY reg_key;
120
121     if(_krb_extra_read)
122         return 0;
123     _krb_extra_read = 1;
124
125     if(RegCreateKey(HKEY_CURRENT_USER, "krb4", &reg_key) != 0)
126         return -1;
127         
128
129     while(1) {
130         name_sz = sizeof(name);
131         data_sz = sizeof(data);
132         if(RegEnumValue(reg_key,
133                         num++,
134                         name,
135                         &name_sz,
136                         NULL,
137                         &type,
138                         data,
139                         &data_sz) != 0)
140             break;
141         if(type == REG_SZ)
142             define_variable(name, data);
143     }
144     RegCloseKey(reg_key);
145     return 0;
146 }
147
148 #endif
149
150 static const char*
151 find_variable(const char *variable)
152 {
153     struct value *e;
154     for(e = _extra_values; e; e = e->next) {
155         if(strcasecmp(variable, e->variable) == 0)
156             return e->value;
157     }
158     return NULL;
159 }
160
161 const char *
162 krb_get_config_string(const char *variable)
163 {
164     read_extra_file();
165     return find_variable(variable);
166 }
167
168 int
169 krb_get_config_bool(const char *variable)
170 {
171     const char *value = krb_get_config_string(variable);
172     if(value == NULL)
173         return 0;
174     return strcasecmp(value, "yes") == 0 || 
175         strcasecmp(value, "true") == 0 ||
176         atoi(value);
177 }