Server 0.0.1
HTTP/REST server implementation

src/auth/ldap.c

Go to the documentation of this file.
00001 
00023 #include <stdarg.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <stdio.h>
00027 #include <ldap.h>
00028 
00029 #include "class.h"
00030 #include "utils/memory.h"
00031 #include "commons.h"
00032 
00033 #include "auth/ldap.h"
00034 #include "auth/credential.h"
00035 #include "auth/interface/auth.h"
00036 
00037 static
00038 int
00039 authLdapCtor(void * _this, va_list * params)
00040 {
00041         AuthLdap this = _this;
00042         char *   url  = va_arg(*params, char*);
00043         char *   base_dn;
00044 
00045         this->url = malloc(strlen(url) + 1);
00046         strcpy(this->url, url);
00047 
00048         this->version  = 3;
00049 
00050         base_dn        = va_arg(* params, char *);
00051         this->nbase_dn = va_arg(* params, size_t);
00052         
00053         this->base_dn = malloc(this->nbase_dn + 1);
00054         this->base_dn[this->nbase_dn] = 0;
00055         memcpy(this->base_dn, base_dn, this->nbase_dn);
00056 
00057         return 0;
00058 }
00059 
00060 static
00061 void
00062 authLdapDtor(void * _this)
00063 {
00064         AuthLdap this = _this;
00065 
00066         FREE(this->base_dn);
00067         FREE(this->url);
00068 }
00069 
00070 static
00071 int
00072 authLdapAuthenticate(void * _this, Credential cred)
00073 {
00074         AuthLdap this = _this;
00075         char     who[256];
00076         char *   who_ptr = who;
00077         int      ldap_err;
00078 
00079         if (CRED_PASSWORD != cred->type) {
00080                 return FALSE;
00081         }
00082 
00083         ldap_initialize(&(this->ldap), this->url);
00084         ldap_set_option(this->ldap, LDAP_OPT_PROTOCOL_VERSION, &(this->version));
00085 
00086         memcpy(who_ptr, "cn=", sizeof("cn=") - 1);
00087         who_ptr   += sizeof("cn=") - 1;
00088         memcpy(who_ptr, CRED_PWD(cred).user, CRED_PWD(cred).nuser);
00089         who_ptr += CRED_PWD(cred).nuser;
00090         *who_ptr++ = ',';
00091         memcpy(who_ptr, this->base_dn, this->nbase_dn);
00092         who_ptr[this->nbase_dn] = 0;
00093 
00094         ldap_err = ldap_simple_bind_s(this->ldap, who, CRED_PWD(cred).pass);
00095         if (0 == ldap_err) {
00096                 ldap_unbind_s(this->ldap);
00098                 return TRUE;
00099         }
00100 
00101         fprintf(stderr, "%s\n", ldap_err2string(ldap_err));
00102         return FALSE;
00103 }
00104 
00105 INIT_IFACE(Class, authLdapCtor, authLdapDtor, NULL);
00106 INIT_IFACE(Auth, authLdapAuthenticate);
00107 CREATE_CLASS(AuthLdap, NULL, IFACE(Class), IFACE(Auth));
00108 
00109 // vim: set ts=4 sw=4:
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines