41b01a1a49927ee8f77b40f1c82448a5ddfef385
[kai/samba.git] / source3 / passdb / passgrp.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Password and authentication handling
5    Copyright (C) Jeremy Allison 1996-1998
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1998
7       
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "nterr.h"
25
26 extern int DEBUGLEVEL;
27
28 /*
29  * NOTE. All these functions are abstracted into a structure
30  * that points to the correct function for the selected database. JRA.
31  *
32  * the API does NOT fill in the gaps if you set an API function
33  * to NULL: it will deliberately attempt to call the NULL function.
34  *
35  */
36
37 static struct passgrp_ops *pwgrp_ops = NULL;
38
39 /***************************************************************
40  Initialise the passgrp operations.
41 ***************************************************************/
42
43 BOOL initialise_passgrp_db(void)
44 {
45   if (pwgrp_ops)
46   {
47     return True;
48   }
49
50 #ifdef WITH_NISPLUS
51   pwgrp_ops =  nisplus_initialise_password_grp();
52 #elif defined(WITH_LDAP)
53   pwgrp_ops = ldap_initialise_password_grp();
54 #elif defined(USE_SMBUNIX_DB)
55   pwgrp_ops = unix_initialise_password_grp();
56 #elif defined(USE_SMBPASS_DB)
57   pwgrp_ops = file_initialise_password_grp();
58 #endif 
59
60   return (pwgrp_ops != NULL);
61 }
62
63 /*
64  * Functions that return/manipulate a struct smb_passwd.
65  */
66
67 /************************************************************************
68  Utility function to search smb passwd by rid.  
69 *************************************************************************/
70
71 struct smb_passwd *iterate_getsmbgrprid(uint32 user_rid,
72                 uint32 **grps, int *num_grps,
73                 uint32 **alss, int *num_alss)
74 {
75         struct smb_passwd *pwd = NULL;
76         void *fp = NULL;
77
78         DEBUG(10, ("search by user_rid: 0x%x\n", user_rid));
79
80         /* Open the smb password database - not for update. */
81         fp = startsmbgrpent(False);
82
83         if (fp == NULL)
84         {
85                 DEBUG(0, ("unable to open smb passgrp database.\n"));
86                 return NULL;
87         }
88
89         while ((pwd = getsmbgrpent(fp, grps, num_grps, alss, num_alss)) != NULL && pwd->user_rid != user_rid)
90       ;
91
92         if (pwd != NULL)
93         {
94                 DEBUG(10, ("found by user_rid: 0x%x\n", user_rid));
95         }
96
97         endsmbgrpent(fp);
98         return pwd;
99 }
100
101 /************************************************************************
102  Utility function to search smb passwd by uid.  use this if your database
103  does not have search facilities.
104 *************************************************************************/
105
106 struct smb_passwd *iterate_getsmbgrpuid(uid_t unix_uid,
107                 uint32 **grps, int *num_grps,
108                 uint32 **alss, int *num_alss)
109 {
110         struct smb_passwd *pwd = NULL;
111         void *fp = NULL;
112
113         DEBUG(10, ("search by unix_uid: %x\n", (int)unix_uid));
114
115         /* Open the smb password database - not for update. */
116         fp = startsmbgrpent(False);
117
118         if (fp == NULL)
119         {
120                 DEBUG(0, ("unable to open smb passgrp database.\n"));
121                 return NULL;
122         }
123
124         while ((pwd = getsmbgrpent(fp, grps, num_grps, alss, num_alss)) != NULL && pwd->unix_uid != unix_uid)
125       ;
126
127         if (pwd != NULL)
128         {
129                 DEBUG(10, ("found by unix_uid: %x\n", (int)unix_uid));
130         }
131
132         endsmbgrpent(fp);
133         return pwd;
134 }
135
136 /************************************************************************
137  Utility function to search smb passwd by name.  use this if your database
138  does not have search facilities.
139 *************************************************************************/
140
141 struct smb_passwd *iterate_getsmbgrpntnam(const char *nt_name,
142                 uint32 **grps, int *num_grps,
143                 uint32 **alss, int *num_alss)
144 {
145         struct smb_passwd *pwd = NULL;
146         fstring name;
147         void *fp = NULL;
148         fstrcpy(name, nt_name);
149
150         DEBUG(10, ("search by name: %s\n", name));
151
152         /* Open the passgrp file - not for update. */
153         fp = startsmbgrpent(False);
154
155         if (fp == NULL)
156         {
157                 DEBUG(0, ("unable to open smb passgrp database.\n"));
158                 return NULL;
159         }
160
161         while ((pwd = getsmbgrpent(fp, grps, num_grps, alss, num_alss)) != NULL && !strequal(pwd->nt_name, name))
162       ;
163
164         if (pwd != NULL)
165         {
166                 DEBUG(10, ("found by name: %s\n", name));
167         }
168
169         endsmbgrpent(fp);
170         return pwd;
171 }
172
173 /***************************************************************
174  Start to enumerate the smb or sam passwd list. Returns a void pointer
175  to ensure no modification outside this module.
176
177  Note that currently it is being assumed that a pointer returned
178  from this function may be used to enumerate struct sam_passwd
179  entries as well as struct smb_passwd entries. This may need
180  to change. JRA.
181
182 ****************************************************************/
183
184 void *startsmbgrpent(BOOL update)
185 {
186   return pwgrp_ops->startsmbgrpent(update);
187 }
188
189 /***************************************************************
190  End enumeration of the smb or sam passwd list.
191
192  Note that currently it is being assumed that a pointer returned
193  from this function may be used to enumerate struct sam_passwd
194  entries as well as struct smb_passwd entries. This may need
195  to change. JRA.
196
197 ****************************************************************/
198
199 void endsmbgrpent(void *vp)
200 {
201   pwgrp_ops->endsmbgrpent(vp);
202 }
203
204 /*************************************************************************
205  Routine to return the next entry in the smb passwd list.
206  *************************************************************************/
207
208 struct smb_passwd *getsmbgrpent(void *vp,
209                 uint32 **grps, int *num_grps,
210                 uint32 **alss, int *num_alss)
211 {
212         return pwgrp_ops->getsmbgrpent(vp, grps, num_grps, alss, num_alss);
213 }
214
215 /************************************************************************
216  Routine to search smb passwd by name.
217 *************************************************************************/
218
219 struct smb_passwd *getsmbgrpntnam(char *name,
220                 uint32 **grps, int *num_grps,
221                 uint32 **alss, int *num_alss)
222 {
223         return pwgrp_ops->getsmbgrpntnam(name, grps, num_grps, alss, num_alss);
224 }
225
226 /************************************************************************
227  Routine to search smb passwd by user rid.
228 *************************************************************************/
229
230 struct smb_passwd *getsmbgrprid(uint32 user_rid,
231                 uint32 **grps, int *num_grps,
232                 uint32 **alss, int *num_alss)
233 {
234         return pwgrp_ops->getsmbgrprid(user_rid, grps, num_grps, alss, num_alss);
235 }
236
237 /************************************************************************
238  Routine to search smb passwd by uid.
239 *************************************************************************/
240
241 struct smb_passwd *getsmbgrpuid(uid_t unix_uid,
242                 uint32 **grps, int *num_grps,
243                 uint32 **alss, int *num_alss)
244 {
245         return pwgrp_ops->getsmbgrpuid(unix_uid, grps, num_grps, alss, num_alss);
246 }
247