summaryrefslogtreecommitdiffstats
path: root/source/passdb/passgrpldap.c
blob: 3d647cd77633fd3ae20bf81a4b18c3808317b2e8 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
/* 
   Unix SMB/Netbios implementation.
   Version 2.0.
   LDAP passgrp database for SAMBA
   Copyright (C) Matthew Chapman 1998
   
   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
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
*/

#include "includes.h"

#ifdef WITH_LDAP

#include <lber.h>
#include <ldap.h>

extern int DEBUGLEVEL;

/* Internal state */
extern LDAP *ldap_struct;
extern LDAPMessage *ldap_results;
extern LDAPMessage *ldap_entry;


/***************************************************************
  Enumerate RIDs of groups which user is a member of, of type
  given by attribute.
 ****************************************************************/

static void ldappassgrp_member(char *attribute, uint32 **rids, int *numrids)
{
	char **values;
	uint32 *ridlist;
	int i;

	if((values = ldap_get_values(ldap_struct, ldap_entry, attribute))) {
		*numrids = i = ldap_count_values(values);
		*rids = ridlist = malloc(i * sizeof(uint32));
		do {
			ridlist[--i] = atoi(values[i]);
		} while(i > 0);
		ldap_value_free(values);
	} else {
		*numrids = 0;
		*rids = NULL;
	}
}


/***************************************************************
  Begin/end smbgrp enumeration.
 ****************************************************************/

static void *ldappassgrp_enumfirst(BOOL update)
{
	if (!ldap_open_connection(False))
		return NULL;

	ldap_search_for("&(objectclass=sambaAccount)(|(group=*)(alias=*))");

	return ldap_struct;
}

static void ldappassgrp_enumclose(void *vp)
{
	ldap_close_connection();
}


/*************************************************************************
  Save/restore the current position in a query
 *************************************************************************/

static SMB_BIG_UINT ldappassgrp_getdbpos(void *vp)
{
	return (SMB_BIG_UINT)((ulong)ldap_entry);
}

static BOOL ldappassgrp_setdbpos(void *vp, SMB_BIG_UINT tok)
{
	ldap_entry = (LDAPMessage *)((ulong)tok);
	return (True);
}


/*************************************************************************
  Return limited smb_passwd information, and group membership.
 *************************************************************************/

static struct smb_passwd *ldappassgrp_getpwbynam(const char *name,
	       uint32 **grp_rids, int *num_grps,
	       uint32 **als_rids, int *num_alss)
{
	struct smb_passwd *ret;

	if(!ldap_open_connection(False))
		return NULL;

	ldap_search_by_ntname(name);
	ldappassgrp_member("group", grp_rids, num_grps);
	ldappassgrp_member("alias", als_rids, num_alss);
	ret = ldap_getpw();

	ldap_close_connection();
	return ret;
}

static struct smb_passwd *ldappassgrp_getpwbyuid(uid_t userid,
	       uint32 **grp_rids, int *num_grps,
	       uint32 **als_rids, int *num_alss)
{
	struct smb_passwd *ret;

	if(!ldap_open_connection(False))
		return NULL;

	ldap_search_by_uid(userid);
	ldappassgrp_member("group", grp_rids, num_grps);
	ldappassgrp_member("alias", als_rids, num_alss);
	ret = ldap_getpw();

	ldap_close_connection();
	return ret;
}

static struct smb_passwd *ldappassgrp_getpwbyrid(uint32 user_rid,
	       uint32 **grp_rids, int *num_grps,
	       uint32 **als_rids, int *num_alss)
{
	struct smb_passwd *ret;

	if(!ldap_open_connection(False))
		return NULL;

	ldap_search_by_rid(user_rid);
	ldappassgrp_member("group", grp_rids, num_grps);
	ldappassgrp_member("alias", als_rids, num_alss);
	ret = ldap_getpw();

	ldap_close_connection();
	return ret;
}

static struct smb_passwd *ldappassgrp_getcurrentpw(void *vp,
	       uint32 **grp_rids, int *num_grps,
	       uint32 **als_rids, int *num_alss)
{
	ldappassgrp_member("group", grp_rids, num_grps);
	ldappassgrp_member("alias", als_rids, num_alss);
	return ldap_getpw();
}



static struct passgrp_ops ldappassgrp_ops =
{
	ldappassgrp_enumfirst,
	ldappassgrp_enumclose,
	ldappassgrp_getdbpos,
	ldappassgrp_setdbpos,

	ldappassgrp_getpwbynam,
	ldappassgrp_getpwbyuid,
	ldappassgrp_getpwbyrid,
	ldappassgrp_getcurrentpw,
};

struct passgrp_ops *ldap_initialise_password_grp(void)
{
	return &ldappassgrp_ops;
}

#else
 void passgrpldap_dummy_function(void);
 void passgrpldap_dummy_function(void) { } /* stop some compilers complaining */
#endif