first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[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;
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_initialize_password_grp();
54 #else 
55   pwgrp_ops = file_initialise_password_grp();
56 #endif 
57
58   return (pwgrp_ops != NULL);
59 }
60
61 /*
62  * Functions that return/manipulate a struct smb_passwd.
63  */
64
65 /************************************************************************
66  Utility function to search smb passwd by rid.  
67 *************************************************************************/
68
69 struct smb_passwd *iterate_getsmbgrprid(uint32 user_rid,
70                 uint32 **grps, int *num_grps,
71                 uint32 **alss, int *num_alss)
72 {
73         return iterate_getsmbgrpuid(pwdb_user_rid_to_uid(user_rid),
74                                     grps, num_grps, alss, num_alss);
75 }
76
77 /************************************************************************
78  Utility function to search smb passwd by uid.  use this if your database
79  does not have search facilities.
80 *************************************************************************/
81
82 struct smb_passwd *iterate_getsmbgrpuid(uid_t smb_userid,
83                 uint32 **grps, int *num_grps,
84                 uint32 **alss, int *num_alss)
85 {
86         struct smb_passwd *pwd = NULL;
87         void *fp = NULL;
88
89         DEBUG(10, ("search by smb_userid: %x\n", (int)smb_userid));
90
91         /* Open the smb password database - not for update. */
92         fp = startsmbgrpent(False);
93
94         if (fp == NULL)
95         {
96                 DEBUG(0, ("unable to open smb passgrp database.\n"));
97                 return NULL;
98         }
99
100         while ((pwd = getsmbgrpent(fp, grps, num_grps, alss, num_alss)) != NULL && pwd->smb_userid != smb_userid)
101       ;
102
103         if (pwd != NULL)
104         {
105                 DEBUG(10, ("found by smb_userid: %x\n", (int)smb_userid));
106         }
107
108         endsmbgrpent(fp);
109         return pwd;
110 }
111
112 /************************************************************************
113  Utility function to search smb passwd by name.  use this if your database
114  does not have search facilities.
115 *************************************************************************/
116
117 struct smb_passwd *iterate_getsmbgrpnam(char *name,
118                 uint32 **grps, int *num_grps,
119                 uint32 **alss, int *num_alss)
120 {
121         struct smb_passwd *pwd = NULL;
122         void *fp = NULL;
123
124         DEBUG(10, ("search by name: %s\n", name));
125
126         /* Open the passgrp file - not for update. */
127         fp = startsmbgrpent(False);
128
129         if (fp == NULL)
130         {
131                 DEBUG(0, ("unable to open smb passgrp database.\n"));
132                 return NULL;
133         }
134
135         while ((pwd = getsmbgrpent(fp, grps, num_grps, alss, num_alss)) != NULL && !strequal(pwd->smb_name, name))
136       ;
137
138         if (pwd != NULL)
139         {
140                 DEBUG(10, ("found by name: %s\n", name));
141         }
142
143         endsmbgrpent(fp);
144         return pwd;
145 }
146
147 /***************************************************************
148  Start to enumerate the smb or sam passwd list. Returns a void pointer
149  to ensure no modification outside this module.
150
151  Note that currently it is being assumed that a pointer returned
152  from this function may be used to enumerate struct sam_passwd
153  entries as well as struct smb_passwd entries. This may need
154  to change. JRA.
155
156 ****************************************************************/
157
158 void *startsmbgrpent(BOOL update)
159 {
160   return pwgrp_ops->startsmbgrpent(update);
161 }
162
163 /***************************************************************
164  End enumeration of the smb or sam passwd list.
165
166  Note that currently it is being assumed that a pointer returned
167  from this function may be used to enumerate struct sam_passwd
168  entries as well as struct smb_passwd entries. This may need
169  to change. JRA.
170
171 ****************************************************************/
172
173 void endsmbgrpent(void *vp)
174 {
175   pwgrp_ops->endsmbgrpent(vp);
176 }
177
178 /*************************************************************************
179  Routine to return the next entry in the smb passwd list.
180  *************************************************************************/
181
182 struct smb_passwd *getsmbgrpent(void *vp,
183                 uint32 **grps, int *num_grps,
184                 uint32 **alss, int *num_alss)
185 {
186         return pwgrp_ops->getsmbgrpent(vp, grps, num_grps, alss, num_alss);
187 }
188
189 /************************************************************************
190  Routine to search smb passwd by name.
191 *************************************************************************/
192
193 struct smb_passwd *getsmbgrpnam(char *name,
194                 uint32 **grps, int *num_grps,
195                 uint32 **alss, int *num_alss)
196 {
197         return pwgrp_ops->getsmbgrpnam(name, grps, num_grps, alss, num_alss);
198 }
199
200 /************************************************************************
201  Routine to search smb passwd by user rid.
202 *************************************************************************/
203
204 struct smb_passwd *getsmbgrprid(uint32 user_rid,
205                 uint32 **grps, int *num_grps,
206                 uint32 **alss, int *num_alss)
207 {
208         return pwgrp_ops->getsmbgrprid(user_rid, grps, num_grps, alss, num_alss);
209 }
210
211 /************************************************************************
212  Routine to search smb passwd by uid.
213 *************************************************************************/
214
215 struct smb_passwd *getsmbgrpuid(uid_t smb_userid,
216                 uint32 **grps, int *num_grps,
217                 uint32 **alss, int *num_alss)
218 {
219         return pwgrp_ops->getsmbgrpuid(smb_userid, grps, num_grps, alss, num_alss);
220 }
221