taskrambler  0.1.9
Web server and task management solution.
ldap.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <ldap.h>
28 
29 #include "class.h"
30 #include "uuid.h"
31 #include "utils/memory.h"
32 #include "commons.h"
33 
34 #include "auth/ldap.h"
35 #include "auth/credential.h"
36 #include "auth/interface/auth.h"
37 
38 static
39 int
40 authLdapCtor(void * _this, va_list * params)
41 {
42  AuthLdap this = _this;
43  char * url = va_arg(*params, char*);
44  char * base_dn;
45 
46  this->url = memMalloc(strlen(url) + 1);
47  strcpy(this->url, url);
48 
49  this->version = 3;
50 
51  base_dn = va_arg(* params, char *);
52  this->nbase_dn = va_arg(* params, size_t);
53 
54  this->base_dn = memMalloc(this->nbase_dn + 1);
55  this->base_dn[this->nbase_dn] = 0;
56  memcpy(this->base_dn, base_dn, this->nbase_dn);
57 
58  return 0;
59 }
60 
61 static
62 void
63 authLdapDtor(void * _this)
64 {
65  AuthLdap this = _this;
66 
67  MEM_FREE(this->base_dn);
68  MEM_FREE(this->url);
69 }
70 
71 static
72 int
73 authLdapAuthenticate(void * _this, Credential cred, Uuid user_index)
74 {
75  AuthLdap this = _this;
76  char who[256];
77  char * who_ptr = who;
78  int ldap_err;
79 
80  struct berval ldap_cred;
81  struct berval * ldap_servcred;
82 
83  if (CRED_PASSWORD != cred->type) {
84  return FALSE;
85  }
86 
87  ldap_initialize(&(this->ldap), this->url);
88  ldap_set_option(this->ldap, LDAP_OPT_PROTOCOL_VERSION, &(this->version));
89 
90  memcpy(who_ptr, "cn=", sizeof("cn=") - 1);
91  who_ptr += sizeof("cn=") - 1;
92  memcpy(who_ptr, CRED_PWD(cred).user, CRED_PWD(cred).nuser);
93  who_ptr += CRED_PWD(cred).nuser;
94  *who_ptr++ = ',';
95  memcpy(who_ptr, this->base_dn, this->nbase_dn);
96  who_ptr[this->nbase_dn] = 0;
97 
98  ldap_cred.bv_val = CRED_PWD(cred).pass;
99  ldap_cred.bv_len = CRED_PWD(cred).npass;
100  ldap_err = ldap_sasl_bind_s(
101  this->ldap,
102  who,
103  LDAP_SASL_SIMPLE,
104  &ldap_cred,
105  NULL,
106  NULL,
107  &ldap_servcred);
108 
109  ldap_unbind_ext_s(this->ldap, NULL, NULL);
110 
111  if (0 == ldap_err) {
112  //! \todo here we need to get and return the user id
113  return TRUE;
114  }
115 
116  //fprintf(stderr, "%s\n", ldap_err2string(ldap_err));
117  /** \todo do error logging instead. */
118  return FALSE;
119 }
120 
121 INIT_IFACE(Class, authLdapCtor, authLdapDtor, NULL);
123 CREATE_CLASS(AuthLdap, NULL, IFACE(Class), IFACE(Auth));
124 
125 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
static void authLdapDtor(void *_this)
Definition: ldap.c:63
static int authLdapAuthenticate(void *_this, Credential cred, Uuid user_index)
Definition: ldap.c:73
void * memMalloc(size_t)
Definition: memory.c:783
#define IFACE(name)
Definition: interface.h:34
#define FALSE
Definition: commons.h:28
#define TRUE
Definition: commons.h:27
static int authLdapCtor(void *_this, va_list *params)
Definition: ldap.c:40
INIT_IFACE(Class, authLdapCtor, authLdapDtor, NULL)
CREATE_CLASS(AuthLdap, NULL, IFACE(Class), IFACE(Auth))
#define CRED_PWD(c)
Definition: credential.h:30