commit df9819beb6f378c7f06246cde9bc8ea36248df11 Author: Alex 'AdUser' Z Date: Sun Nov 29 16:02:54 2015 +1000 + initial diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..861f537 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,16 @@ +SET(CNAME "openvpn-auth-ldap") +SET(VERSION 0.01) + +PROJECT(${CNAME} C) +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) + +SET(CMAKE_INSTALL_PREFIX "/usr") +SET(INSTALL_LIB "${CMAKE_INSTALL_PREFIX}/lib/openvpn") + +MESSAGE (STATUS "------------------------------------------") +MESSAGE (STATUS "Build type is: ${CMAKE_BUILD_TYPE}") +MESSAGE (STATUS "") +MESSAGE (STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}") +MESSAGE (STATUS "Library directory: ${INSTALL_LIB}") + +ADD_SUBDIRECTORY (src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..14eccf4 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,4 @@ +add_library("openvpn-auth-ldap" SHARED "config.c" "main.c") + +install(TARGETS "openvpn-auth-ldap" + LIBRARY DESTINATION "${INSTALL_LIB}") diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..f2cd430 --- /dev/null +++ b/src/config.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include + +#include "config.h" + +#define BUFSIZE 1024 + +int parse_config(oal_config_t * const config, const char *file) { + FILE *f; + const size_t bufsize = 1024; + unsigned short linenum = 0; + char buf[BUFSIZE]; + char err[BUFSIZE]; + char *key, *value; + size_t valsize; + + if ((f = fopen(file, "r")) == NULL) { + snprintf(err, bufsize, "can't open file: %s", strerror(errno)); + config->error = strndup(err, bufsize); + return 1; + } + + while (fgets(buf, BUFSIZE, f)) { + linenum++; + key = buf; + while (isspace(*key)) + key++; + if (*key == '#') + continue; /* ignore comments */ + if (!isalpha(*key)) { + snprintf(err, bufsize, "can't parse line %d", linenum); + config->error = strdup(err); + return 1; + } + value = key; + while(*value && !isspace(*value)) + value++; + if (!isspace(value)) { + snprintf(err, bufsize, "can't find value at line %d", linenum); + config->error = strndup(err, bufsize); + return 1; + } + *value = '\0', value += 1; + while (isspace(*value)) + value++; + valsize = strnlen(value, bufsize - (value - buf)); + + if (strncmp(key, "bindurls", 6) == 0) { + config->bindurls = strndup(value, valsize); + } else + if (strncmp(key, "binddn", 6) == 0) { + config->binddn = strndup(value, valsize); + } else + if (strncmp(key, "bindpass", 8) == 0) { + config->bindpass = strndup(value, valsize); + } else + if (strncmp(key, "bindtimeout", 11) == 0) { + config->bindtimeout = atoi(value); + } else + if (strncmp(key, "basedn", 6) == 0) { + config->basedn = strndup(value, valsize); + } else + if (strncmp(key, "userfilter", 10) == 0) { + config->userfilter = strndup(value, valsize); + } else + { + snprintf(err, bufsize, "unknown key '%s' at line %d", key, linenum); + config->error = strndup(err, bufsize); + return 1; + } + } + + return 0; +} \ No newline at end of file diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..ecf3c11 --- /dev/null +++ b/src/config.h @@ -0,0 +1,16 @@ +#ifndef OAL_CONFIG_H_INCLUDED +#define OAL_CONFIG_H_INCLUDED + +typedef struct { + char *bindurls; /** space-separated list ldap of URIs */ + char *binddn; /** bind as this user before search for user */ + char *bindpass; /** bind with this password */ + size_t bindtimeout; /** bind timeout */ + char *basedn; /** where to search for users */ + char *userfilter; /** ldap filter for user entry */ + char *error; /** parser error */ +} oal_config_t; + +int parse_config(oal_config_t * const config, const char *file); + +#endif /* OAL_CONFIG_H_INCLUDED */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..e81c1d0 --- /dev/null +++ b/src/main.c @@ -0,0 +1,19 @@ +#include +#include +#include + +#include + +#include "config.h" + +int main(void) { + oal_config_t config; + memset(&config, 0x0, sizeof(oal_config_t)); + + if (!parse_config(&config, "test.conf")) { + fprintf(stderr, "config parser failed: %s\n", config.error); + exit(1); + } + + return 0; +}