summaryrefslogtreecommitdiff
path: root/usr/src/lib/libsasl/lib/config.c
blob: 265d95d0b712e76d506e479302d10c075f5ab7d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
#pragma ident	"%Z%%M%	%I%	%E% SMI"

/* SASL Config file API
 * Rob Siemborski
 * Tim Martin (originally in Cyrus distribution)
 * $Id: config.c,v 1.13 2003/02/13 19:55:54 rjs3 Exp $
 */
/* 
 * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any other legal
 *    details, please contact  
 *      Office of Technology Transfer
 *      Carnegie Mellon University
 *      5000 Forbes Avenue
 *      Pittsburgh, PA  15213-3890
 *      (412) 268-4387, fax: (412) 268-7395
 *      tech-transfer@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Current Valid keys:
 *
 * canon_user_plugin: <string>
 * pwcheck_method: <string>
 * auto_transition: <boolean>
 * plugin_list: <string>
 *
 * srvtab: <string>
 */


#include "sasl.h"
#include "saslint.h"

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "config.h"	/* _SUN_SDK_ */

struct configlist {
    char *key;
    char *value;
};

#ifndef _SUN_SDK_
static struct configlist *configlist;
static int nconfiglist;
#endif /* !_SUN_SDK_ */

#define CONFIGLISTGROWSIZE 100

#ifdef _SUN_SDK_
int sasl_config_init(_sasl_global_context_t *gctx, const char *filename)
#else
int sasl_config_init(const char *filename)
#endif /* _SUN_SDK_ */
{
    FILE *infile;
    int lineno = 0;
    int alloced = 0;
    char buf[4096];
    char *p, *key;
    int result;
#ifdef _SUN_SDK_
    int invalid_line = 0;

    gctx->nconfiglist=0;
#else
    nconfiglist=0;
#endif /* _SUN_SDK_ */

    infile = fopen(filename, "r");
    if (!infile) {
      return SASL_CONTINUE;
    }
#ifdef _SUN_SDK_
    result = _sasl_strdup(filename, &gctx->config_path, NULL);
    if (result != SASL_OK)
	goto done;
#endif /* _SUN_SDK_ */
    
    while (fgets(buf, sizeof(buf), infile)) {
	lineno++;

	if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
	for (p = buf; *p && isspace((int) *p); p++);
	if (!*p || *p == '#') continue;

	key = p;
	while (*p && (isalnum((int) *p) || *p == '-' || *p == '_')) {
	    if (isupper((int) *p)) *p = tolower(*p);
	    p++;
	}
	if (*p != ':') {
#ifdef _SUN_SDK_
	  invalid_line = 1;
	  goto done;
#else
	  return SASL_FAIL;
#endif /* _SUN_SDK_ */
	}
	*p++ = '\0';

	while (*p && isspace((int) *p)) p++;
	
	if (!*p) {
#ifdef _SUN_SDK_
	  invalid_line = 1;
	  goto done;
#else
	  return SASL_FAIL;
#endif /* _SUN_SDK_ */
	}

#ifdef _SUN_SDK_
	if (gctx->nconfiglist == alloced) {
#else
	if (nconfiglist == alloced) {
#endif /* _SUN_SDK_ */
	    alloced += CONFIGLISTGROWSIZE;
#ifdef _SUN_SDK_
	    gctx->configlist=sasl_REALLOC((char *)gctx->configlist, 
				    alloced * sizeof(struct configlist));
	    if (gctx->configlist==NULL) {
		result = SASL_NOMEM;
		goto done;
	    }
#else
	    configlist=sasl_REALLOC((char *)configlist, 
				    alloced * sizeof(struct configlist));
	    if (configlist==NULL) return SASL_NOMEM;
#endif /* _SUN_SDK_ */
	}



#ifdef _SUN_SDK_
	result = _sasl_strdup(key,
			      &(((struct configlist *)(gctx->configlist))
				[gctx->nconfiglist].key),
			      NULL);
	if (result!=SASL_OK)
	  goto done;
#else
	result = _sasl_strdup(key,
			      &(configlist[nconfiglist].key),
			      NULL);
	if (result!=SASL_OK) return result;
#endif /* _SUN_SDK_ */
#ifdef _SUN_SDK_
	result = _sasl_strdup(p,
			      &(((struct configlist *)(gctx->configlist))
				[gctx->nconfiglist].value),
			      NULL);
	if (result!=SASL_OK) {
	    sasl_FREE(((struct configlist *)(gctx->configlist))
				[gctx->nconfiglist].key);
	    goto done;
	}
#else
	result = _sasl_strdup(p,
			      &(configlist[nconfiglist].value),
			      NULL);
	if (result!=SASL_OK) return result;
#endif /* _SUN_SDK_ */

#ifdef _SUN_SDK_
	(gctx->nconfiglist)++;
#else
	nconfiglist++;
#endif /* _SUN_SDK_ */
    }
#ifdef _SUN_SDK_
    result = SASL_OK;

done:
    fclose(infile);

    if (invalid_line) {
	__sasl_log(gctx, gctx->server_global_callbacks.callbacks,
		   SASL_LOG_ERR, "%s: bad config line: '%s'", filename, buf);
	result = SASL_FAIL;
    }

    return result;
#else
    fclose(infile);

    return SASL_OK;
#endif /* _SUN_SDK_ */
}

#ifdef _SUN_SDK_
/* Releases the resources acquired in sasl_config_init() */
void sasl_config_free(_sasl_global_context_t *gctx)
{
    int i;

    if (gctx->config_path != NULL)
	sasl_FREE(gctx->config_path);
    gctx->config_path = NULL;
    if (gctx->configlist == NULL)
	return;

    for (i = 0; i < gctx->nconfiglist; i++) {
	if ((((struct configlist *)gctx->configlist))[i].key)
	    sasl_FREE(((struct configlist *)gctx->configlist)[i].key);
	if (((struct configlist *)gctx->configlist)[i].value)
	    sasl_FREE(((struct configlist *)gctx->configlist)[i].value);
    }
    sasl_FREE(gctx->configlist);
    gctx->configlist = NULL;
    gctx->nconfiglist = 0;
}

const char *sasl_config_getstring(_sasl_global_context_t *gctx,
	const char *key, const char *def)
{
    int opt;
    struct configlist *clist = (struct configlist *)gctx->configlist;

    for (opt = 0; opt < gctx->nconfiglist; opt++) {
	if (*key == clist[opt].key[0] &&
	    !strcmp(key, clist[opt].key))
	  return clist[opt].value;
    }
    return def;
}
#else
const char *sasl_config_getstring(const char *key,const char *def)
{
    int opt;

    for (opt = 0; opt < nconfiglist; opt++) {
	if (*key == configlist[opt].key[0] &&
	    !strcmp(key, configlist[opt].key))
	  return configlist[opt].value;
    }
    return def;
}
#endif /* _SUN_SDK_ */

#ifdef _SUN_SDK_
int sasl_config_getint(_sasl_global_context_t *gctx, const char *key,int def)
#else
int sasl_config_getint(const char *key,int def)
#endif /* _SUN_SDK_ */
{
#ifdef _SUN_SDK_
    const char *val = sasl_config_getstring(gctx, key, (char *)0);
#else
    const char *val = sasl_config_getstring(key, (char *)0);
#endif /* _SUN_SDK_ */

    if (!val) return def;
    if (!isdigit((int) *val) && (*val != '-' || !isdigit((int) val[1]))) return def;
    return atoi(val);
}

#ifdef _SUN_SDK_
int sasl_config_getswitch(_sasl_global_context_t *gctx,const char *key,int def)
#else
int sasl_config_getswitch(const char *key,int def)
#endif /* _SUN_SDK_ */
{
#ifdef _SUN_SDK_
    const char *val = sasl_config_getstring(gctx, key, (char *)0);
#else
    const char *val = sasl_config_getstring(key, (char *)0);
#endif /* _SUN_SDK_ */

    if (!val) return def;

    if (*val == '0' || *val == 'n' ||
	(*val == 'o' && val[1] == 'f') || *val == 'f') {
	return 0;
    }
    else if (*val == '1' || *val == 'y' ||
	     (*val == 'o' && val[1] == 'n') || *val == 't') {
	return 1;
    }
    return def;
}