source: storage_ldap.c @ f32d557

Last change on this file since f32d557 was f32d557, checked in by Jelmer Vernooij <jelmer@…>, at 2006-03-24T15:53:29Z

Switch from LDB to LDAP (LDB's authentication subsystem is not mature enough yet)

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[f32d557]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Storage backend that uses a LDAP database */
8
9/* Copyright (C) 2006 Jelmer Vernooij <jelmer@samba.org> */
10
11/*
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  GNU General Public License for more details.
21
22  You should have received a copy of the GNU General Public License with
23  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
24  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
25  Suite 330, Boston, MA  02111-1307  USA
26*/
27
28#define BITLBEE_CORE
29#include "bitlbee.h"
30#include <ldap.h>
31
32#define BB_LDAP_HOST "localhost"
33#define BB_LDAP_BASE ""
34
35static char *nick_dn(const char *nick)
36{
37        return g_strdup_printf("bitlBeeNick=%s%s%s", nick, BB_LDAP_BASE?",":"", BB_LDAP_BASE?BB_LDAP_BASE:"");
38}
39
40static storage_status_t nick_connect(const char *nick, const char *password, LDAP **ld)
41{
42        char *mydn;
43        int ret;
44        storage_status_t status;
45        *ld = ldap_init(BB_LDAP_HOST, LDAP_PORT);
46
47        if (!ld) {
48                /* FIXME: report error */
49                return STORAGE_OTHER_ERROR;
50        }
51
52        mydn = nick_dn(nick);
53
54        ret = ldap_simple_bind_s(*ld, mydn, password);
55
56        switch (ret) {
57         case LDAP_SUCCESS: status = STORAGE_OK; break;
58         case LDAP_INVALID_CREDENTIALS: status = STORAGE_INVALID_PASSWORD; break;
59         default: 
60                /* FIXME: Log */
61                status = STORAGE_OTHER_ERROR;
62                break;
63        }
64
65        g_free(mydn);
66
67        return status;
68}
69
70static void sldap_init (void)
71{
72}
73
74static storage_status_t sldap_load ( const char *my_nick, const char* password, irc_t *irc )
75{
76        LDAPMessage *res;
77        LDAP *ld;
78        int ret;
79        storage_status_t status;
80        char *mydn; 
81
82        status = nick_connect(my_nick, password, &ld);
83        if (status != STORAGE_OK)
84                return status;
85
86        mydn = nick_dn(my_nick);
87
88        ret = ldap_search_s(ld, mydn, LDAP_SCOPE_ONELEVEL, "(objectClass=*)", NULL, 0, &res);
89
90        g_free(mydn);
91
92        /* FIXME: Check ret */
93
94        /* FIXME: Store in irc_t */
95       
96        return STORAGE_OK;
97}
98
99static storage_status_t sldap_save( irc_t *irc, int overwrite )
100{
101        LDAP *ld;
102        char *mydn;
103        storage_status_t status;
104
105        status = nick_connect(irc->nick, irc->password, &ld);
106        if (status != STORAGE_OK)
107                return status;
108
109        mydn = nick_dn(irc->nick);
110
111        /* FIXME */
112       
113        g_free(mydn);
114       
115        return STORAGE_OK;
116}
117
118static storage_status_t sldap_check_pass( const char *nick, const char *password )
119{
120        LDAP *ld;
121        storage_status_t status;
122
123        status = nick_connect(nick, password, &ld);
124
125        ldap_unbind_s(ld);
126
127        return status;
128}
129
130static storage_status_t sldap_remove( const char *nick, const char *password )
131{
132        storage_status_t status;
133        LDAP *ld;
134        char *mydn;
135        int ret;
136       
137        status = nick_connect(nick, password, &ld);
138
139        if (status != STORAGE_OK)
140                return status;
141
142        mydn = nick_dn(nick);
143       
144        ret = ldap_delete(ld, mydn);
145
146        if (ret != LDAP_SUCCESS) {
147                /* FIXME: report */
148                return STORAGE_OTHER_ERROR;
149        }
150
151        g_free(mydn);
152        return STORAGE_OK;
153}
154
155storage_t storage_ldap = {
156        .name = "ldap",
157        .init = sldap_init,
158        .check_pass = sldap_check_pass,
159        .remove = sldap_remove,
160        .load = sldap_load,
161        .save = sldap_save
162};
Note: See TracBrowser for help on using the repository browser.