#include "sasl.h"
#include "saslint.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "config.h"
struct configlist {
char *key;
char *value;
};
#ifndef _SUN_SDK_
static struct configlist *configlist;
static int nconfiglist;
#endif
#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
{
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
infile = fopen(filename, "rF");
if (!infile) {
return SASL_CONTINUE;
}
#ifdef _SUN_SDK_
result = _sasl_strdup(filename, &gctx->config_path, NULL);
if (result != SASL_OK)
goto done;
#endif
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
}
*p++ = '\0';
while (*p && isspace((int) *p)) p++;
if (!*p) {
#ifdef _SUN_SDK_
invalid_line = 1;
goto done;
#else
return SASL_FAIL;
#endif
}
#ifdef _SUN_SDK_
if (gctx->nconfiglist == alloced) {
#else
if (nconfiglist == alloced) {
#endif
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
}
#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
#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
#ifdef _SUN_SDK_
(gctx->nconfiglist)++;
#else
nconfiglist++;
#endif
}
#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
}
#ifdef _SUN_SDK_
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
#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
{
#ifdef _SUN_SDK_
const char *val = sasl_config_getstring(gctx, key, (char *)0);
#else
const char *val = sasl_config_getstring(key, (char *)0);
#endif
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
{
#ifdef _SUN_SDK_
const char *val = sasl_config_getstring(gctx, key, (char *)0);
#else
const char *val = sasl_config_getstring(key, (char *)0);
#endif
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;
}