aboutsummaryrefslogtreecommitdiffstats
path: root/storage_ldap.c
blob: 4bc99de5b90cdc084b1c883e406cf4e36dec59e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  /********************************************************************\
  * BitlBee -- An IRC to other IM-networks gateway                     *
  *                                                                    *
  * Copyright 2002-2004 Wilmer van der Gaast and others                *
  \********************************************************************/

/* Storage backend that uses a LDAP database */

/* Copyright (C) 2006 Jelmer Vernooij <jelmer@samba.org> */

/*
  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 2 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 with
  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  Suite 330, Boston, MA  02111-1307  USA
*/

#define BITLBEE_CORE
#include "bitlbee.h"
#include <ldap.h>

#define BB_LDAP_HOST "localhost"
#define BB_LDAP_BASE ""

static char *nick_dn(const char *nick)
{
	return g_strdup_printf("bitlBeeNick=%s%s%s", nick, BB_LDAP_BASE?",":"", BB_LDAP_BASE?BB_LDAP_BASE:"");
}

static storage_status_t nick_connect(const char *nick, const char *password, LDAP **ld)
{
	char *mydn;
	int ret;
	storage_status_t status;
	*ld = ldap_init(BB_LDAP_HOST, LDAP_PORT);

	if (!ld) {
		log_message( LOGLVL_WARNING, "Unable to connect to LDAP server at %s", BB_LDAP_HOST );
		return STORAGE_OTHER_ERROR;
	}

	mydn = nick_dn(nick);

	ret = ldap_simple_bind_s(*ld, mydn, password);

	switch (ret) {
	 case LDAP_SUCCESS: status = STORAGE_OK; break;
	 case LDAP_INVALID_CREDENTIALS: status = STORAGE_INVALID_PASSWORD; break;
	 default: 
		log_message( LOGLVL_WARNING, "Unable to authenticate %s: %s", mydn, ldap_err2string(ret) );
		status = STORAGE_OTHER_ERROR;
		break;
	}

	g_free(mydn);

	return status;
}

static storage_status_t sldap_load ( const char *my_nick, const char* password, irc_t *irc )
{
	LDAPMessage *res, *msg;
	LDAP *ld;
	int ret, i;
	storage_status_t status;
	char *mydn; 

	status = nick_connect(my_nick, password, &ld);
	if (status != STORAGE_OK)
		return status;

	mydn = nick_dn(my_nick);

	ret = ldap_search_s(ld, mydn, LDAP_SCOPE_BASE, "(objectClass=*)", NULL, 0, &res);

	if (ret != LDAP_SUCCESS) {
		log_message( LOGLVL_WARNING, "Unable to search for %s: %s", mydn, ldap_err2string(ret) );
		ldap_unbind_s(ld);
		return STORAGE_OTHER_ERROR;
	}

	g_free(mydn);

	for (msg = ldap_first_entry(ld, res); msg; msg = ldap_next_entry(ld, msg)) {
	}

	/* FIXME: Store in irc_t */

	ldap_unbind_s(ld);
	
	return STORAGE_OK;
}

static storage_status_t sldap_check_pass( const char *nick, const char *password )
{
	LDAP *ld;
	storage_status_t status;

	status = nick_connect(nick, password, &ld);

	ldap_unbind_s(ld);

	return status;
}

static storage_status_t sldap_remove( const char *nick, const char *password )
{
	storage_status_t status;
	LDAP *ld;
	char *mydn;
	int ret;
	
	status = nick_connect(nick, password, &ld);

	if (status != STORAGE_OK)
		return status;

	mydn = nick_dn(nick);
	
	ret = ldap_delete(ld, mydn);

	if (ret != LDAP_SUCCESS) {
		log_message( LOGLVL_WARNING, "Error removing %s: %s", mydn, ldap_err2string(ret) );
		ldap_unbind_s(ld);
		return STORAGE_OTHER_ERROR;
	}

	ldap_unbind_s(ld);

	g_free(mydn);
	return STORAGE_OK;
}

static storage_status_t sldap_save( irc_t *irc, int overwrite )
{
	LDAP *ld;
	char *mydn;
	storage_status_t status;
	LDAPMessage *msg;

	status = nick_connect(irc->nick, irc->password, &ld);
	if (status != STORAGE_OK)
		return status;

	mydn = nick_dn(irc->nick);

	/* FIXME: Make this a bit more atomic? What if we crash after 
	 * removing the old account but before adding the new one ? */
	if (overwrite) 
		sldap_remove(irc->nick, irc->password);

	g_free(mydn);

	ldap_unbind_s(ld);
	
	return STORAGE_OK;
}



storage_t storage_ldap = {
	.name = "ldap",
	.check_pass = sldap_check_pass,
	.remove = sldap_remove,
	.load = sldap_load,
	.save = sldap_save
};