2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/* SASL Config file API
2N/A * Rob Siemborski
2N/A * Tim Martin (originally in Cyrus distribution)
2N/A * $Id: config.c,v 1.13 2003/02/13 19:55:54 rjs3 Exp $
2N/A */
2N/A/*
2N/A * Copyright (c) 1998-2003 Carnegie Mellon University. All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A *
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A *
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in
2N/A * the documentation and/or other materials provided with the
2N/A * distribution.
2N/A *
2N/A * 3. The name "Carnegie Mellon University" must not be used to
2N/A * endorse or promote products derived from this software without
2N/A * prior written permission. For permission or any other legal
2N/A * details, please contact
2N/A * Office of Technology Transfer
2N/A * Carnegie Mellon University
2N/A * 5000 Forbes Avenue
2N/A * Pittsburgh, PA 15213-3890
2N/A * (412) 268-4387, fax: (412) 268-7395
2N/A * tech-transfer@andrew.cmu.edu
2N/A *
2N/A * 4. Redistributions of any form whatsoever must retain the following
2N/A * acknowledgment:
2N/A * "This product includes software developed by Computing Services
2N/A * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
2N/A *
2N/A * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
2N/A * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
2N/A * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
2N/A * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
2N/A * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
2N/A * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A/*
2N/A * Current Valid keys:
2N/A *
2N/A * canon_user_plugin: <string>
2N/A * pwcheck_method: <string>
2N/A * auto_transition: <boolean>
2N/A * plugin_list: <string>
2N/A *
2N/A * srvtab: <string>
2N/A */
2N/A
2N/A
2N/A#include "sasl.h"
2N/A#include "saslint.h"
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <ctype.h>
2N/A
2N/A#include "config.h" /* _SUN_SDK_ */
2N/A
2N/Astruct configlist {
2N/A char *key;
2N/A char *value;
2N/A};
2N/A
2N/A#ifndef _SUN_SDK_
2N/Astatic struct configlist *configlist;
2N/Astatic int nconfiglist;
2N/A#endif /* !_SUN_SDK_ */
2N/A
2N/A#define CONFIGLISTGROWSIZE 100
2N/A
2N/A#ifdef _SUN_SDK_
2N/Aint sasl_config_init(_sasl_global_context_t *gctx, const char *filename)
2N/A#else
2N/Aint sasl_config_init(const char *filename)
2N/A#endif /* _SUN_SDK_ */
2N/A{
2N/A FILE *infile;
2N/A int lineno = 0;
2N/A int alloced = 0;
2N/A char buf[4096];
2N/A char *p, *key;
2N/A int result;
2N/A#ifdef _SUN_SDK_
2N/A int invalid_line = 0;
2N/A
2N/A gctx->nconfiglist=0;
2N/A#else
2N/A nconfiglist=0;
2N/A#endif /* _SUN_SDK_ */
2N/A
2N/A infile = fopen(filename, "rF");
2N/A if (!infile) {
2N/A return SASL_CONTINUE;
2N/A }
2N/A#ifdef _SUN_SDK_
2N/A result = _sasl_strdup(filename, &gctx->config_path, NULL);
2N/A if (result != SASL_OK)
2N/A goto done;
2N/A#endif /* _SUN_SDK_ */
2N/A
2N/A while (fgets(buf, sizeof(buf), infile)) {
2N/A lineno++;
2N/A
2N/A if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
2N/A for (p = buf; *p && isspace((int) *p); p++);
2N/A if (!*p || *p == '#') continue;
2N/A
2N/A key = p;
2N/A while (*p && (isalnum((int) *p) || *p == '-' || *p == '_')) {
2N/A if (isupper((int) *p)) *p = tolower(*p);
2N/A p++;
2N/A }
2N/A if (*p != ':') {
2N/A#ifdef _SUN_SDK_
2N/A invalid_line = 1;
2N/A goto done;
2N/A#else
2N/A return SASL_FAIL;
2N/A#endif /* _SUN_SDK_ */
2N/A }
2N/A *p++ = '\0';
2N/A
2N/A while (*p && isspace((int) *p)) p++;
2N/A
2N/A if (!*p) {
2N/A#ifdef _SUN_SDK_
2N/A invalid_line = 1;
2N/A goto done;
2N/A#else
2N/A return SASL_FAIL;
2N/A#endif /* _SUN_SDK_ */
2N/A }
2N/A
2N/A#ifdef _SUN_SDK_
2N/A if (gctx->nconfiglist == alloced) {
2N/A#else
2N/A if (nconfiglist == alloced) {
2N/A#endif /* _SUN_SDK_ */
2N/A alloced += CONFIGLISTGROWSIZE;
2N/A#ifdef _SUN_SDK_
2N/A gctx->configlist=sasl_REALLOC((char *)gctx->configlist,
2N/A alloced * sizeof(struct configlist));
2N/A if (gctx->configlist==NULL) {
2N/A result = SASL_NOMEM;
2N/A goto done;
2N/A }
2N/A#else
2N/A configlist=sasl_REALLOC((char *)configlist,
2N/A alloced * sizeof(struct configlist));
2N/A if (configlist==NULL) return SASL_NOMEM;
2N/A#endif /* _SUN_SDK_ */
2N/A }
2N/A
2N/A
2N/A
2N/A#ifdef _SUN_SDK_
2N/A result = _sasl_strdup(key,
2N/A &(((struct configlist *)(gctx->configlist))
2N/A [gctx->nconfiglist].key),
2N/A NULL);
2N/A if (result!=SASL_OK)
2N/A goto done;
2N/A#else
2N/A result = _sasl_strdup(key,
2N/A &(configlist[nconfiglist].key),
2N/A NULL);
2N/A if (result!=SASL_OK) return result;
2N/A#endif /* _SUN_SDK_ */
2N/A#ifdef _SUN_SDK_
2N/A result = _sasl_strdup(p,
2N/A &(((struct configlist *)(gctx->configlist))
2N/A [gctx->nconfiglist].value),
2N/A NULL);
2N/A if (result!=SASL_OK) {
2N/A sasl_FREE(((struct configlist *)(gctx->configlist))
2N/A [gctx->nconfiglist].key);
2N/A goto done;
2N/A }
2N/A#else
2N/A result = _sasl_strdup(p,
2N/A &(configlist[nconfiglist].value),
2N/A NULL);
2N/A if (result!=SASL_OK) return result;
2N/A#endif /* _SUN_SDK_ */
2N/A
2N/A#ifdef _SUN_SDK_
2N/A (gctx->nconfiglist)++;
2N/A#else
2N/A nconfiglist++;
2N/A#endif /* _SUN_SDK_ */
2N/A }
2N/A#ifdef _SUN_SDK_
2N/A result = SASL_OK;
2N/A
2N/Adone:
2N/A fclose(infile);
2N/A
2N/A if (invalid_line) {
2N/A __sasl_log(gctx, gctx->server_global_callbacks.callbacks,
2N/A SASL_LOG_ERR, "%s: bad config line: '%s'", filename, buf);
2N/A result = SASL_FAIL;
2N/A }
2N/A
2N/A return result;
2N/A#else
2N/A fclose(infile);
2N/A
2N/A return SASL_OK;
2N/A#endif /* _SUN_SDK_ */
2N/A}
2N/A
2N/A#ifdef _SUN_SDK_
2N/A/* Releases the resources acquired in sasl_config_init() */
2N/Avoid sasl_config_free(_sasl_global_context_t *gctx)
2N/A{
2N/A int i;
2N/A
2N/A if (gctx->config_path != NULL)
2N/A sasl_FREE(gctx->config_path);
2N/A gctx->config_path = NULL;
2N/A if (gctx->configlist == NULL)
2N/A return;
2N/A
2N/A for (i = 0; i < gctx->nconfiglist; i++) {
2N/A if ((((struct configlist *)gctx->configlist))[i].key)
2N/A sasl_FREE(((struct configlist *)gctx->configlist)[i].key);
2N/A if (((struct configlist *)gctx->configlist)[i].value)
2N/A sasl_FREE(((struct configlist *)gctx->configlist)[i].value);
2N/A }
2N/A sasl_FREE(gctx->configlist);
2N/A gctx->configlist = NULL;
2N/A gctx->nconfiglist = 0;
2N/A}
2N/A
2N/Aconst char *sasl_config_getstring(_sasl_global_context_t *gctx,
2N/A const char *key, const char *def)
2N/A{
2N/A int opt;
2N/A struct configlist *clist = (struct configlist *)gctx->configlist;
2N/A
2N/A for (opt = 0; opt < gctx->nconfiglist; opt++) {
2N/A if (*key == clist[opt].key[0] &&
2N/A !strcmp(key, clist[opt].key))
2N/A return clist[opt].value;
2N/A }
2N/A return def;
2N/A}
2N/A#else
2N/Aconst char *sasl_config_getstring(const char *key,const char *def)
2N/A{
2N/A int opt;
2N/A
2N/A for (opt = 0; opt < nconfiglist; opt++) {
2N/A if (*key == configlist[opt].key[0] &&
2N/A !strcmp(key, configlist[opt].key))
2N/A return configlist[opt].value;
2N/A }
2N/A return def;
2N/A}
2N/A#endif /* _SUN_SDK_ */
2N/A
2N/A#ifdef _SUN_SDK_
2N/Aint sasl_config_getint(_sasl_global_context_t *gctx, const char *key,int def)
2N/A#else
2N/Aint sasl_config_getint(const char *key,int def)
2N/A#endif /* _SUN_SDK_ */
2N/A{
2N/A#ifdef _SUN_SDK_
2N/A const char *val = sasl_config_getstring(gctx, key, (char *)0);
2N/A#else
2N/A const char *val = sasl_config_getstring(key, (char *)0);
2N/A#endif /* _SUN_SDK_ */
2N/A
2N/A if (!val) return def;
2N/A if (!isdigit((int) *val) && (*val != '-' || !isdigit((int) val[1]))) return def;
2N/A return atoi(val);
2N/A}
2N/A
2N/A#ifdef _SUN_SDK_
2N/Aint sasl_config_getswitch(_sasl_global_context_t *gctx,const char *key,int def)
2N/A#else
2N/Aint sasl_config_getswitch(const char *key,int def)
2N/A#endif /* _SUN_SDK_ */
2N/A{
2N/A#ifdef _SUN_SDK_
2N/A const char *val = sasl_config_getstring(gctx, key, (char *)0);
2N/A#else
2N/A const char *val = sasl_config_getstring(key, (char *)0);
2N/A#endif /* _SUN_SDK_ */
2N/A
2N/A if (!val) return def;
2N/A
2N/A if (*val == '0' || *val == 'n' ||
2N/A (*val == 'o' && val[1] == 'f') || *val == 'f') {
2N/A return 0;
2N/A }
2N/A else if (*val == '1' || *val == 'y' ||
2N/A (*val == 'o' && val[1] == 'n') || *val == 't') {
2N/A return 1;
2N/A }
2N/A return def;
2N/A}
2N/A