first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/samba.git] / source / passdb / passgrpldap.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0.
4    LDAP passgrp database for SAMBA
5    Copyright (C) Matthew Chapman 1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #include "includes.h"
24
25 #ifdef WITH_LDAP
26
27 #include <lber.h>
28 #include <ldap.h>
29
30 extern int DEBUGLEVEL;
31
32 /* Internal state */
33 extern LDAP *ldap_struct;
34 extern LDAPMessage *ldap_results;
35 extern LDAPMessage *ldap_entry;
36
37
38 /***************************************************************
39   Enumerate RIDs of groups which user is a member of, of type
40   given by attribute.
41  ****************************************************************/
42
43 static void ldappassgrp_member(char *attribute, uint32 **rids, int *numrids)
44 {
45         char **values;
46         uint32 *ridlist;
47         int i;
48
49         if((values = ldap_get_values(ldap_struct, ldap_entry, attribute))) {
50                 *numrids = i = ldap_count_values(values);
51                 *rids = ridlist = malloc(i * sizeof(uint32));
52                 do {
53                         ridlist[--i] = atoi(values[i]);
54                 } while(i > 0);
55                 ldap_value_free(values);
56         } else {
57                 *numrids = 0;
58                 *rids = NULL;
59         }
60 }
61
62
63 /***************************************************************
64   Begin/end smbgrp enumeration.
65  ****************************************************************/
66
67 static void *ldappassgrp_enumfirst(BOOL update)
68 {
69         if (!ldap_connect())
70                 return NULL;
71
72         ldap_search_for("&(objectclass=sambaAccount)(|(group=*)(alias=*))");
73
74         return ldap_struct;
75 }
76
77 static void ldappassgrp_enumclose(void *vp)
78 {
79         ldap_disconnect();
80 }
81
82
83 /*************************************************************************
84   Save/restore the current position in a query
85  *************************************************************************/
86
87 static SMB_BIG_UINT ldappassgrp_getdbpos(void *vp)
88 {
89         return (SMB_BIG_UINT)((ulong)ldap_entry);
90 }
91
92 static BOOL ldappassgrp_setdbpos(void *vp, SMB_BIG_UINT tok)
93 {
94         ldap_entry = (LDAPMessage *)((ulong)tok);
95         return (True);
96 }
97
98
99 /*************************************************************************
100   Return limited smb_passwd information, and group membership.
101  *************************************************************************/
102
103 static struct smb_passwd *ldappassgrp_getpwbynam(const char *name,
104                uint32 **grp_rids, int *num_grps,
105                uint32 **als_rids, int *num_alss)
106 {
107         struct smb_passwd *ret;
108
109         if(!ldap_connect())
110                 return NULL;
111
112         ldap_search_by_ntname(name);
113         ldappassgrp_member("group", grp_rids, num_grps);
114         ldappassgrp_member("alias", als_rids, num_alss);
115         ret = ldap_getpw();
116
117         ldap_disconnect();
118         return ret;
119 }
120
121 static struct smb_passwd *ldappassgrp_getpwbyuid(uid_t userid,
122                uint32 **grp_rids, int *num_grps,
123                uint32 **als_rids, int *num_alss)
124 {
125         struct smb_passwd *ret;
126
127         if(!ldap_connect())
128                 return NULL;
129
130         ldap_search_by_uid(userid);
131         ldappassgrp_member("group", grp_rids, num_grps);
132         ldappassgrp_member("alias", als_rids, num_alss);
133         ret = ldap_getpw();
134
135         ldap_disconnect();
136         return ret;
137 }
138
139 static struct smb_passwd *ldappassgrp_getpwbyrid(uint32 user_rid,
140                uint32 **grp_rids, int *num_grps,
141                uint32 **als_rids, int *num_alss)
142 {
143         struct smb_passwd *ret;
144
145         if(!ldap_connect())
146                 return NULL;
147
148         ldap_search_by_rid(user_rid);
149         ldappassgrp_member("group", grp_rids, num_grps);
150         ldappassgrp_member("alias", als_rids, num_alss);
151         ret = ldap_getpw();
152
153         ldap_disconnect();
154         return ret;
155 }
156
157 static struct smb_passwd *ldappassgrp_getcurrentpw(void *vp,
158                uint32 **grp_rids, int *num_grps,
159                uint32 **als_rids, int *num_alss)
160 {
161         ldappassgrp_member("group", grp_rids, num_grps);
162         ldappassgrp_member("alias", als_rids, num_alss);
163         return ldap_getpw();
164 }
165
166
167
168 static struct passgrp_ops ldappassgrp_ops =
169 {
170         ldappassgrp_enumfirst,
171         ldappassgrp_enumclose,
172         ldappassgrp_getdbpos,
173         ldappassgrp_setdbpos,
174
175         ldappassgrp_getpwbynam,
176         ldappassgrp_getpwbyuid,
177         ldappassgrp_getpwbyrid,
178         ldappassgrp_getcurrentpw,
179 };
180
181 struct passgrp_ops *ldap_initialise_password_grp(void)
182 {
183         return &ldappassgrp_ops;
184 }
185
186 #else
187  void passgrpldap_dummy_function(void);
188  void passgrpldap_dummy_function(void) { } /* stop some compilers complaining */
189 #endif
190