remove compile-time option to get configuration from LDAP

This commit is contained in:
marschap
2007-04-14 07:54:27 +00:00
parent 2695c2fdd6
commit a44091f6fe
10 changed files with 100 additions and 475 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ libLCDstuff_a_SOURCES = LL.c LL.h sockets.c sockets.h str.c str.h configfile.c c
libLCDstuff_a_LIBADD = @LIBOBJS@
AM_CPPFLAGS = @ldap_includes@ -I$(top_srcdir)
AM_CPPFLAGS = -I$(top_srcdir)
EXTRA_DIST = Makefile.in
+64 -256
View File
@@ -1,16 +1,16 @@
/*
* configfile.c
* This file is part of LCDd, the lcdproc server.
/** \file configfile.c
* Define routines to read INI-file like files.
*/
/* This file is part of LCDd, the lcdproc server.
*
* This file is released under the GNU General Public License. Refer to the
* COPYING file distributed with this package.
*
* Copyright(c) 2001, Joris Robijn
* (c) 2003, Rene Wagner
* (c) 2006,2007 Peter Marschall
*
*
* Defines routines to read ini-file-like files.
* Optionally retrieves settings from an LDAP directory(OpenLDAP 2.1.x)
*/
#include "config.h"
@@ -20,10 +20,6 @@
#include <unistd.h>
#include <stdlib.h>
#ifdef WITH_LDAP_SUPPORT
# include <ldap.h>
#endif /* WITH_LDAP_SUPPORT */
#include "shared/report.h"
@@ -56,28 +52,12 @@ static int process_config(section **current_section, const char *source_descr, F
#endif
#ifdef WITH_LDAP_SUPPORT
static int connect_to_ldap(void);
static LDAP *ld = NULL;
int use_ldap = 0;
static char *ldap_host = NULL, *ldap_base_dn = NULL;
int ldap_port;
/* not supported for now
* char ldap_user[255] = "",
* ldap_pwd[255] = "";
*/
#endif /* WITH_LDAP_SUPPORT */
/**** PUBLIC FUNCTIONS ****/
/** Parse configuration from INI-file style config file into memory.
* \param filename Name of the config file.
* \returns 0 : config successfully parsed
* \returns <0 : error occurred
* \retval 0 config successfully parsed
* \retval <0 error occurred
*/
int config_read_file(const char *filename)
{
@@ -85,41 +65,8 @@ int config_read_file(const char *filename)
section *curr_section = NULL;
int result = 0;
#ifdef WITH_LDAP_SUPPORT
LDAPURLDesc *url = NULL;
int retval;
#endif /* WITH_LDAP_SUPPORT */
report(RPT_NOTICE, "Using Configuration File: %s", filename);
#ifdef WITH_LDAP_SUPPORT
if (ldap_is_ldap_url(filename)) {
use_ldap = 1;
if (0 != (retval = ldap_url_parse(filename, &url))) {
report(RPT_ERR, "Errors parsing LDAP URL %s: %s", filename, ldap_err2string(retval));
ldap_free_urldesc(url);
return(-1);
}
ldap_host = strdup(url->lud_host);
ldap_port = url->lud_port;
report(RPT_INFO, "Using LDAP server: %s:%d", ldap_host, ldap_port);
ldap_base_dn = strdup(url->lud_dn);
report(RPT_INFO, "Using LDAP base DN: %s", ldap_base_dn);
ldap_free_urldesc(url);
if (connect_to_ldap() < 0) {
debug(RPT_DEBUG, "connect_to_ldap returned errors.");
return(-1);
}
return 0;
}
#endif /* WITH_LDAP_SUPPORT */
f = fopen(filename, "r");
if (f == NULL) {
return -1;
@@ -158,11 +105,40 @@ int config_read_string(const char *sectionname, const char *str)
/** Get string from configuration in memory.
*
* The strings returned are always NUL-terminated.
* They should never be modified, and used only short-term.
* In successive calls this function can re-use the data space !
*
* You can do some things with the returned string:
* \li Scan or parse it:
* \code
* s = config_get_string(...);
* sscanf(s, "%dx%d", &w, &h); // scan format like: 20x4
* \endcode
* ...and check the w and h values...
* \li Copy it to a preallocated buffer like \c device[256]:
* \code
* s = config_get_string(...);
* strncpy(device, s, sizeof(device));
* device[sizeof(device)-1] = '\0'; // make sure it is terminated
* \endcode
* \li Copy it to a newly allocated space in \c char \c *device:
* \code
* s = config_get_string(...);
* device = malloc(strlen(s)+1);
* if (device == NULL) return -5; // or whatever < 0
* strcpy( device, s );
* \endcode
*
* \param sectionname Name of the section where the key is sought.
* \param keyname Name of the key to look for.
* \param skip Number of values to skip/ignore before returning the value.
* \param default_value Default value if section/key is not found.
* \return Value found / default value
* This is used to iterate through the values of a multi-valued key:
* \c 0 for the first value, \c 1 for the 2nd, ... and \c -1 for the last.
* \param default_value Default value if section/key is not found
* or \c skip exceeds the number of values of the key.
* \return Value found / \c default_value
*/
const char *config_get_string(const char *sectionname, const char *keyname,
int skip, const char *default_value)
@@ -187,11 +163,19 @@ const char *config_get_string(const char *sectionname, const char *keyname,
/** Get boolean value from configuration in memory.
*
* Legal boolean values are:
* \li \c 0 , \c false , \c off , \c no or \c n for FALSE.
* \li \c 1 , \c true , \c on , \c yes or \c y for TRUE
*
* \param sectionname Name of the section where the key is sought.
* \param keyname Name of the key to look for.
* \param skip Number of values to skip/ignore before returning the value.
* \param default_value Default value if section/key is not found or value is no legal boolean.
* \return Value found / default value
* This is used to iterate through the values of a multi-valued key:
* \c 0 for the first value, \c 1 for the 2nd, ... and \c -1 for the last.
* \param default_value Default value if section/key is not found, value is no legal boolean,
* or \c skip exceeds the number of values of the key.
* \return Value found / \c default_value
*/
short config_get_bool(const char *sectionname, const char *keyname,
int skip, short default_value)
@@ -219,8 +203,11 @@ short config_get_bool(const char *sectionname, const char *keyname,
* \param sectionname Name of the section where the key is sought.
* \param keyname Name of the key to look for.
* \param skip Number of values to skip/ignore before returning the value.
* \param default_value Default value if section/key is not found or value is no integer.
* \return Value found / default value
* This is used to iterate through the values of a multi-valued key:
* \c 0 for the first value, \c 1 for the 2nd, ... and \c -1 for the last.
* \param default_value Default value if section/key is not found, value is no integer,
* or \c skip exceeds the number of values of the key.
* \return Value found / \c default_value
*/
long int config_get_int(const char *sectionname, const char *keyname,
int skip, long int default_value)
@@ -243,8 +230,11 @@ long int config_get_int(const char *sectionname, const char *keyname,
* \param sectionname Name of the section where the key is sought.
* \param keyname Name of the key to look for.
* \param skip Number of values to skip/ignore before returning the value.
* \param default_value Default value if section/key is not found or value is no floating point number.
* \return Value found / default value
* This is used to iterate through the values of a multi-valued key:
* \c 0 for the first value, \c 1 for the 2nd, ... and \c -1 for the last.
* \param default_value Default value if section/key is not found, value is no floating point number
* or \c skip exceeds the number of values of the key.
* \return Value found / \c default_value
*/
double config_get_float(const char *sectionname, const char *keyname,
int skip, double default_value)
@@ -265,7 +255,8 @@ double config_get_float(const char *sectionname, const char *keyname,
/** Test whether the configuration containis a specific section.
* \param sectionname Name of the section to look for.
* \return 0 = section not in config; 1 = section in config
* \retval 0 section not in config
* \retval 1 section in config
*/
int config_has_section(const char *sectionname)
{
@@ -276,7 +267,8 @@ int config_has_section(const char *sectionname)
/** Test whether the configuration contains a specific key in a specfic section.
* \param sectionname Name of the section where the key is sought.
* \param keyname Name of the key to look for.
* \return 0 = key or section not found; n = key found with n values
* \retval 0 key or section not found
* \retval n key found with \c n values (\c n > 0)
*/
int config_has_key(const char *sectionname, const char *keyname)
{
@@ -297,7 +289,7 @@ int config_has_key(const char *sectionname, const char *keyname)
/** Clear configuration. */
void config_clear()
void config_clear(void)
{
section *s;
section *next_s;
@@ -328,97 +320,10 @@ void config_clear()
/**** INTERNAL FUNCTIONS ****/
#ifdef WITH_LDAP_SUPPORT
static int
connect_to_ldap(void)
{
int retval;
LDAPMessage *res;
debug(RPT_INFO, "Connecting to LDAP server: %s:%d", ldap_host, ldap_port);
if (!(ld = ldap_init(ldap_host, ldap_port))) {
report(RPT_ERR, "LDAP session could not be initialized.");
return(-1);
}
/*****************************************************
* disabled unless you really have a DN/pwd to bind to
* WARNING: LCDd should not have LDAP write access!!
*
* if (LDAP_SUCCESS != (retval = ldap_simple_bind_s(ld, ldap_user, ldap_pwd))) {
* report(RPT_ERR, "LDAP login on %s:%d failed: %s", ldap_host, ldap_port, ldap_err2string(retval));
* ldap_unbind(ld);
* ld = NULL;
*
* return(-1);
* }
* fprintf(stderr, "LDAP login successful on %s:%d\n", ldap_host, ldap_port);
********************************************************/
/* check for the existence of the config object... */
if (LDAP_SUCCESS != (retval = ldap_search_s(ld, ldap_base_dn, LDAP_SCOPE_BASE, "objectClass=lcdprocConfig", NULL, 0, &res))) {
report(RPT_ERR, "Could not access LDAP server on %s:%d", ldap_host, ldap_port);
return(-1);
}
if (0 == ldap_count_entries(ld, res)) {
report(RPT_ERR, "No configuration object found in LDAP at: %s", ldap_base_dn);
return(-1);
}
debug(RPT_DEBUG, "Configuration LDAP object found.");
return 0;
}
#define BUFSIZE 255
#endif /* WITH_LDAP_SUPPORT */
static section *find_section(const char *sectionname)
{
section *s;
#ifdef WITH_LDAP_SUPPORT
LDAPMessage *res;
int retval;
char *filter = NULL;
if (use_ldap) {
debug(RPT_DEBUG, "Searching LDAP for section [%s]", sectionname);
if (NULL == (filter = malloc(BUFSIZE))){
report(RPT_ERR, "Could not allocate memory in find_section()");
return NULL;
}
strcpy(filter, "cn=");
strncat(filter, sectionname, BUFSIZE);
if (LDAP_SUCCESS != (retval = ldap_search_s(ld, ldap_base_dn, LDAP_SCOPE_ONELEVEL, filter, NULL, 0, &res))) {
if (NULL != filter) {
free(filter);
filter = NULL;
}
ldap_msgfree(res);
report(RPT_ERR, "Could not access LDAP server on %s:%d", ldap_host, ldap_port);
return NULL;
}
if (NULL != filter) {
free(filter);
filter = NULL;
}
if (0 == ldap_count_entries(ld, res)) {
debug(RPT_DEBUG, "Section [%s] not found in LDAP.", sectionname);
return NULL;
}
ldap_msgfree(res);
debug(RPT_DEBUG, "Found section [%s] in LDAP", sectionname);
s = (section*) malloc(sizeof(section));
if (s != NULL) {
s->name = strdup(sectionname);
s->first_key = NULL;
s->next_section = NULL;
}
return s;
}
#endif /* WITH_LDAP_SUPPORT */
for (s = first_section; s != NULL; s = s->next_section) {
if (strcasecmp(s->name, sectionname) == 0) {
return s;
@@ -453,107 +358,10 @@ static key *find_key(section *s, const char *keyname, int skip)
int count = 0;
key *last_key = NULL;
#ifdef WITH_LDAP_SUPPORT
LDAPMessage *res;
LDAPMessage *entry;
int retval;
char *buf = NULL;
char **vals;
#endif /* WITH_LDAP_SUPPORT */
/* Check for NULL section*/
if (s == NULL)
return NULL;
#ifdef WITH_LDAP_SUPPORT
if (use_ldap) {
debug(RPT_DEBUG, "Searching LDAP for key '%s' in section [%s] skipping %d entries.", keyname, s->name, skip);
if (NULL == (buf = malloc(BUFSIZE))){
report(RPT_ERR, "Could not allocate memory in find_key().");
}
strcpy(buf, "cn=");
strncat(buf, s->name, BUFSIZE);
if (LDAP_SUCCESS != (retval = ldap_search_s(ld, ldap_base_dn, LDAP_SCOPE_ONELEVEL, buf, NULL, 0, &res))) {
if (NULL != buf) {
free(buf);
buf=NULL;
}
ldap_msgfree(res);
report(RPT_ERR, "Could not access LDAP server on %s:%d", ldap_host, ldap_port);
return NULL;
}
if (NULL == (entry = ldap_first_entry(ld, res))) {
debug(RPT_DEBUG, "Section [%s] not found in LDAP.", s->name);
if (NULL != buf) {
free(buf);
buf = NULL;
}
/* DON'T enable the following
* ldap_msgfree(entry);
* ldap_msgfree below does that already
*/
ldap_msgfree(res);
return NULL;
}
strcpy(buf, "lcdproc");
strncat(buf, keyname, BUFSIZE);
/* debug(RPT_DEBUG, "Key name translated to attribute name: %s", buf); */
vals = ldap_get_values(ld, entry, buf);
if (skip+1 > ldap_count_values(vals)) {
debug(RPT_DEBUG, "No such entry found.");
if (NULL != buf) {
free(buf);
buf = NULL;
}
ldap_value_free(vals);
/* DON'T enable the following
* ldap_msgfree(entry);
* ldap_msgfree below does that already
*/
ldap_msgfree(res);
return NULL;
}
/* DON'T enable the following
* ldap_msgfree(entry);
* ldap_msgfree below does that already
*/
ldap_msgfree(res);
if (vals && vals[skip]) {
if (NULL != buf) {
free(buf);
buf = NULL;
}
buf=strdup(vals[skip]);
debug(RPT_DEBUG, "Entry found. Value is: %s", buf);
ldap_value_free(vals);
k = (key *) malloc(sizeof(key));
if ( k != NULL) {
k->name = strdup(keyname);
k->value = strdup(buf);
k->next_key = NULL;
}
if (NULL != buf) {
free(buf);
buf = NULL;
}
return k;
}
report(RPT_ERR, "LDAP server encountered errors.");
ldap_value_free(vals);
if (NULL != buf) {
free(buf);
buf = NULL;
}
return NULL;
}
#endif /* WITH_LDAP_SUPPORT */
for (k = s->first_key; k != NULL; k = k->next_key) {
/* Did we find the right key ?*/
+30 -34
View File
@@ -1,14 +1,14 @@
/*
* configfile.h
* This file is part of LCDd, the lcdproc server.
/** \file configfile.h
* Declares routines to read INI-file like files.
*/
/* This file is part of LCDd, the lcdproc server.
*
* This file is released under the GNU General Public License. Refer to the
* COPYING file distributed with this package.
*
* Copyright (c) 2001, Joris Robijn
*
*
* Defines routines to read ini-file-like files.
* (c) 2006,2007 Peter Marschall
*
*/
@@ -19,52 +19,47 @@
#include "config.h"
#endif
int config_read_file(const char *filename);
/* Opens the specified file and reads everything into memory.
* Returns -1 on parsing errors.
* Returns -2 if the file could not be opened or a read error occured.
* Returns -16 if a malloc went wrong.
* Returns 0 when config file was successfully parsed
* Returns <0 on errors
*/
int config_read_file(const char *filename);
int config_read_string(const char *sectionname, const char *str);
#if defined(LCDPROC_CONFIG_READ_STRING)
/* Reads everything in the string into memory.
* Returns -1 on parsing errors.
* Returns -16 if a malloc went wrong.
* Returns 0 when config file was successfully parsed
* Returns <0 on errors
*/
int config_read_string(const char *sectionname, const char *str);
#endif
short config_get_bool(const char *sectionname, const char *keyname,
int skip, short default_value);
/* Tries to interpret a value in the config file as a boolean.
* 0, false, no, n = false
* 1, true, yes, y = true
* 0, false, off, no, n = false
* 1, true, on, yes, y = true
* If the key is not found or cannot be interpreted, the given default value is
* returned.
* The skip value can be used to iterate over multiple values with the same
* key. Should be 0 to get the first one, 1 for the second etc. and -1 for the
* last.
*/
short config_get_bool(const char *sectionname, const char *keyname,
int skip, short default_value);
/* Tries to interpret a value in the config file as an integer.*/
long int config_get_int(const char *sectionname, const char *keyname,
int skip, long int default_value);
/* Tries to interpret a value in the config file as an integer.*/
/* Tries to interpret a value in the config file as a float.*/
double config_get_float(const char *sectionname, const char *keyname,
int skip, double default_value);
/* Tries to interpret a value in the config file as a float.*/
const char *config_get_string(const char *sectionname, const char *keyname,
int skip, const char *default_value);
/* Returns a pointer to the string associated with the specified key.
* The string should never be modified, and used only short-term. You can
* for example scan it or copy it. In successive calls this function can
* re-use the data space !
* The strings returned are always NUL-terminated.
* The string should never be modified, and used only short-term.
* In successive calls this function can * re-use the data space !
*
* found in a mail from Joris:
*
* The strings it returns are always terminated.
*
* You can do some things with them:
* 1. Scan them:
* You can do some things with the returned string:
* 1. Scan or parse it:
* s = config_get_string(...);
* sscanf( s, "%dx%d", &w, &h ); // scan format like: 20x4
* ...and check the w and h values...
@@ -77,22 +72,23 @@ const char *config_get_string(const char *sectionname, const char *keyname,
* device = malloc(strlen(s)+1);
* if( device == NULL ) return -5; // or whatever < 0
* strcpy( device, s );
*
*/
const char *config_get_string(const char *sectionname, const char *keyname,
int skip, const char *default_value);
int config_has_section(const char *sectionname);
/* Checks if a specified section exists.
* Returns whether it exists.
*/
int config_has_section(const char *sectionname);
int config_has_key(const char *sectionname, const char *keyname);
/* Checks if a specified key within the specified section exists.
* Returns the number of times the key exists.
*/
int config_has_key(const char *sectionname, const char *keyname);
void config_clear();
/* Clears all data stored by the config_read_* functions.
* Should be called if the config should be reread.
*/
void config_clear(void);
#endif