Browse Source
added a first basic athentication system with ldap binding. Now login depends on the existens of a valid ldap account
master
added a first basic athentication system with ldap binding. Now login depends on the existens of a valid ldap account
master
24 changed files with 439 additions and 32 deletions
-
6assets/html/main.html
-
6assets/style/common.css
-
45include/auth.h
-
19include/auth/ldap.h
-
8include/commons.h
-
32include/credential.h
-
1include/http/message.h
-
1include/http/parser.h
-
2include/http/worker.h
-
49include/interface/auth.h
-
14src/Makefile.am
-
88src/auth/ldap.c
-
64src/credential.c
-
5src/http/message.c
-
2src/http/parser/body.c
-
6src/http/parser/header.c
-
4src/http/parser/post_vars.c
-
3src/http/worker.c
-
57src/http/worker/process.c
-
4src/http/writer/write.c
-
42src/interface/auth.c
-
2src/interface/http_intro.c
-
2src/interface/subject.c
-
9src/webgameserver.c
@ -0,0 +1,45 @@ |
|||
/** |
|||
* \file |
|||
* Authenticatio module factory |
|||
* |
|||
* A factory to get a specific authentication module. |
|||
* An authentication module is a class that implement the Auth interface. |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#ifndef __AUTH_H__ |
|||
#define __AUTH_H__ |
|||
|
|||
#include "class.h" |
|||
#include "auth/ldap.h" |
|||
|
|||
typedef enum e_AuthModule { |
|||
AUTH_LDAP = 0 |
|||
} AuthModule; |
|||
|
|||
CLASS(Auth) { |
|||
}; |
|||
|
|||
void * authCreateById(Auth, int); |
|||
AuthLdap authCreateLdap(Auth); |
|||
|
|||
#endif // __AUTH_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,19 @@ |
|||
#ifndef __AUTH_LDAP_H__ |
|||
#define __AUTH_LDAP_H__ |
|||
|
|||
#include <ldap.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "class.h" |
|||
|
|||
CLASS(AuthLdap) { |
|||
LDAP * ldap; |
|||
char * url; |
|||
char * base_dn; |
|||
int version; |
|||
size_t nbase_dn; |
|||
}; |
|||
|
|||
#endif // __AUTH_LDAP_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,32 @@ |
|||
#ifndef __CREDENTIAL_H__ |
|||
#define __CREDENTIAL_H__ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#include "class.h" |
|||
|
|||
#define CRED_PWD(c) (((c)->cred).pwd) |
|||
|
|||
typedef enum e_CredentialType { |
|||
CRED_PASSWORD = 0 |
|||
} CredentialType; |
|||
|
|||
|
|||
CLASS(Credential) { |
|||
CredentialType type; |
|||
|
|||
union { |
|||
|
|||
struct { |
|||
char * user; |
|||
size_t nuser; |
|||
char * pass; |
|||
size_t npass; |
|||
} pwd; |
|||
|
|||
} cred; |
|||
}; |
|||
|
|||
#endif // __CREDENTIAL_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,49 @@ |
|||
/** |
|||
* \file |
|||
* The authentication interface. |
|||
* |
|||
* This is the authentication interface. It's only pupose is to |
|||
* authenticate someone or somewhat. It is called AUTH. |
|||
* The concrete access rights are managed within a class called ACL. |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#ifndef __INTERFACE_AUTH_H__ |
|||
#define __INTERFACE_AUTH_H__ |
|||
|
|||
#include <stdarg.h> |
|||
|
|||
#include "interface.h" |
|||
#include "credential.h" |
|||
|
|||
typedef int (* fptr_authenticate)(void *, Credential); |
|||
|
|||
extern const struct interface i_Auth; |
|||
|
|||
struct i_Auth { |
|||
const struct interface * const _; |
|||
fptr_authenticate authenticate; |
|||
}; |
|||
|
|||
extern int authenticate(void *, Credential); |
|||
|
|||
#endif // __INTERFACE_AUTH_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,88 @@ |
|||
#include <stdarg.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include <ldap.h> |
|||
|
|||
#include "auth/ldap.h" |
|||
#include "class.h" |
|||
#include "credential.h" |
|||
#include "interface/class.h" |
|||
#include "interface/auth.h" |
|||
|
|||
#include "utils/memory.h" |
|||
#include "commons.h" |
|||
|
|||
static |
|||
int |
|||
authLdapCtor(void * _this, va_list * params) |
|||
{ |
|||
AuthLdap this = _this; |
|||
char * url = va_arg(*params, char*); |
|||
char * base_dn; |
|||
|
|||
this->url = malloc(strlen(url) + 1); |
|||
strcpy(this->url, url); |
|||
|
|||
this->version = 3; |
|||
|
|||
base_dn = va_arg(* params, char *); |
|||
this->nbase_dn = va_arg(* params, size_t); |
|||
|
|||
this->base_dn = malloc(this->nbase_dn + 1); |
|||
this->base_dn[this->nbase_dn] = 0; |
|||
memcpy(this->base_dn, base_dn, this->nbase_dn); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
authLdapDtor(void * _this) |
|||
{ |
|||
AuthLdap this = _this; |
|||
|
|||
FREE(this->base_dn); |
|||
FREE(this->url); |
|||
} |
|||
|
|||
static |
|||
int |
|||
authLdapAuthenticate(void * _this, Credential cred) |
|||
{ |
|||
AuthLdap this = _this; |
|||
char who[256]; |
|||
char * who_ptr = who; |
|||
int ldap_err; |
|||
|
|||
if (CRED_PASSWORD != cred->type) { |
|||
return FALSE; |
|||
} |
|||
|
|||
ldap_initialize(&(this->ldap), this->url); |
|||
ldap_set_option(this->ldap, LDAP_OPT_PROTOCOL_VERSION, &(this->version)); |
|||
|
|||
memcpy(who_ptr, "cn=", sizeof("cn=") - 1); |
|||
who_ptr += sizeof("cn=") - 1; |
|||
memcpy(who_ptr, CRED_PWD(cred).user, CRED_PWD(cred).nuser); |
|||
who_ptr += CRED_PWD(cred).nuser; |
|||
*who_ptr++ = ','; |
|||
memcpy(who_ptr, this->base_dn, this->nbase_dn); |
|||
who_ptr[this->nbase_dn] = 0; |
|||
|
|||
ldap_err = ldap_simple_bind_s(this->ldap, who, CRED_PWD(cred).pass); |
|||
if (0 == ldap_err) { |
|||
ldap_unbind_s(this->ldap); |
|||
//! \todo here we need to get and return the user id |
|||
return TRUE; |
|||
} |
|||
|
|||
fprintf(stderr, "%s\n", ldap_err2string(ldap_err)); |
|||
return FALSE; |
|||
} |
|||
|
|||
INIT_IFACE(Class, authLdapCtor, authLdapDtor, NULL); |
|||
INIT_IFACE(Auth, authLdapAuthenticate); |
|||
CREATE_CLASS(AuthLdap, NULL, IFACE(Class), IFACE(Auth)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,64 @@ |
|||
#include <stdarg.h> |
|||
#include <sys/types.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
|
|||
#include "credential.h" |
|||
#include "class.h" |
|||
#include "interface/class.h" |
|||
|
|||
#include "utils/memory.h" |
|||
|
|||
static |
|||
int |
|||
credentialCtor(void * _this, va_list * params) |
|||
{ |
|||
Credential this = _this; |
|||
|
|||
this->type = va_arg(* params, CredentialType); |
|||
|
|||
switch(this->type) { |
|||
case CRED_PASSWORD: |
|||
{ |
|||
char * user, *pass; |
|||
|
|||
user = va_arg(* params, char*); |
|||
CRED_PWD(this).nuser = va_arg(* params, size_t); |
|||
pass = va_arg(* params, char*); |
|||
CRED_PWD(this).npass = va_arg(* params, size_t); |
|||
|
|||
CRED_PWD(this).user = malloc(CRED_PWD(this).nuser + 1); |
|||
CRED_PWD(this).user[CRED_PWD(this).nuser] = 0; |
|||
memcpy(CRED_PWD(this).user, user, CRED_PWD(this).nuser); |
|||
|
|||
CRED_PWD(this).pass = malloc(CRED_PWD(this).npass + 1); |
|||
CRED_PWD(this).pass[CRED_PWD(this).npass] = 0; |
|||
memcpy(CRED_PWD(this).pass, pass, CRED_PWD(this).npass); |
|||
} |
|||
break; |
|||
|
|||
default: |
|||
return -1; |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
static |
|||
void |
|||
credentialDtor(void * _this) |
|||
{ |
|||
Credential this = _this; |
|||
|
|||
switch(this->type) { |
|||
case CRED_PASSWORD: |
|||
FREE(CRED_PWD(this).user); |
|||
FREE(CRED_PWD(this).pass); |
|||
break; |
|||
} |
|||
} |
|||
|
|||
INIT_IFACE(Class, credentialCtor, credentialDtor, NULL); |
|||
CREATE_CLASS(Credential, NULL, IFACE(Class)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,42 @@ |
|||
/** |
|||
* \file |
|||
* |
|||
* \author Georg Hopp |
|||
* |
|||
* \copyright |
|||
* Copyright © 2012 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include "auth.h" |
|||
#include "credential.h" |
|||
#include "interface/auth.h" |
|||
|
|||
const struct interface i_Auth = { |
|||
"auth", |
|||
1 |
|||
}; |
|||
|
|||
int |
|||
authenticate(void * auth, Credential cred) |
|||
{ |
|||
int ret; |
|||
|
|||
RETCALL(auth, Auth, authenticate, ret, cred); |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue