Alex 'AdUser' Z
9 years ago
commit
df9819beb6
5 changed files with 132 additions and 0 deletions
@ -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) |
@ -0,0 +1,4 @@
|
||||
add_library("openvpn-auth-ldap" SHARED "config.c" "main.c") |
||||
|
||||
install(TARGETS "openvpn-auth-ldap" |
||||
LIBRARY DESTINATION "${INSTALL_LIB}") |
@ -0,0 +1,77 @@
|
||||
#include <ctype.h> |
||||
#include <errno.h> |
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
#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; |
||||
} |
@ -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 */ |
@ -0,0 +1,19 @@
|
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include <openvpn/openvpn-plugin.h> |
||||
|
||||
#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; |
||||
} |
Loading…
Reference in new issue