Removed version number from file header.
[ira/wip.git] / source3 / passdb / passgrp.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Jeremy Allison 1996-1998
5    Copyright (C) Luke Kenneth Casson Leighton 1996-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 #include "includes.h"
23
24 /*
25  * NOTE. All these functions are abstracted into a structure
26  * that points to the correct function for the selected database. JRA.
27  *
28  * the API does NOT fill in the gaps if you set an API function
29  * to NULL: it will deliberately attempt to call the NULL function.
30  *
31  */
32
33 static struct passgrp_ops *pwgrp_ops;
34
35 /***************************************************************
36  Initialise the passgrp operations.
37 ***************************************************************/
38
39 BOOL initialise_passgrp_db(void)
40 {
41   if (pwgrp_ops)
42   {
43     return True;
44   }
45
46 #ifdef WITH_NISPLUS
47   pwgrp_ops =  nisplus_initialise_password_grp();
48 #elif defined(WITH_LDAP)
49   pwgrp_ops = ldap_initialize_password_grp();
50 #else 
51   pwgrp_ops = file_initialise_password_grp();
52 #endif 
53
54   return (pwgrp_ops != NULL);
55 }
56
57 /*
58  * Functions that return/manipulate a struct smb_passwd.
59  */
60
61 /************************************************************************
62  Utility function to search smb passwd by rid.  
63 *************************************************************************/
64
65 struct smb_passwd *iterate_getsmbgrprid(uint32 user_rid,
66                 uint32 **grps, int *num_grps,
67                 uint32 **alss, int *num_alss)
68 {
69         return iterate_getsmbgrpuid(pwdb_user_rid_to_uid(user_rid),
70                                     grps, num_grps, alss, num_alss);
71 }
72
73 /************************************************************************
74  Utility function to search smb passwd by uid.  use this if your database
75  does not have search facilities.
76 *************************************************************************/
77
78 struct smb_passwd *iterate_getsmbgrpuid(uid_t smb_userid,
79                 uint32 **grps, int *num_grps,
80                 uint32 **alss, int *num_alss)
81 {
82         struct smb_passwd *pwd = NULL;
83         void *fp = NULL;
84
85         DEBUG(10, ("search by smb_userid: %x\n", (int)smb_userid));
86
87         /* Open the smb password database - not for update. */
88         fp = startsmbgrpent(False);
89
90         if (fp == NULL)
91         {
92                 DEBUG(0, ("unable to open smb passgrp database.\n"));
93                 return NULL;
94         }
95
96         while ((pwd = getsmbgrpent(fp, grps, num_grps, alss, num_alss)) != NULL && pwd->smb_userid != smb_userid)
97       ;
98
99         if (pwd != NULL)
100         {
101                 DEBUG(10, ("found by smb_userid: %x\n", (int)smb_userid));
102         }
103
104         endsmbgrpent(fp);
105         return pwd;
106 }
107
108 /************************************************************************
109  Utility function to search smb passwd by name.  use this if your database
110  does not have search facilities.
111 *************************************************************************/
112
113 struct smb_passwd *iterate_getsmbgrpnam(char *name,
114                 uint32 **grps, int *num_grps,
115                 uint32 **alss, int *num_alss)
116 {
117         struct smb_passwd *pwd = NULL;
118         void *fp = NULL;
119
120         DEBUG(10, ("search by name: %s\n", name));
121
122         /* Open the passgrp file - not for update. */
123         fp = startsmbgrpent(False);
124
125         if (fp == NULL)
126         {
127                 DEBUG(0, ("unable to open smb passgrp database.\n"));
128                 return NULL;
129         }
130
131         while ((pwd = getsmbgrpent(fp, grps, num_grps, alss, num_alss)) != NULL && !strequal(pwd->smb_name, name))
132       ;
133
134         if (pwd != NULL)
135         {
136                 DEBUG(10, ("found by name: %s\n", name));
137         }
138
139         endsmbgrpent(fp);
140         return pwd;
141 }
142
143 /***************************************************************
144  Start to enumerate the smb or sam passwd list. Returns a void pointer
145  to ensure no modification outside this module.
146
147  Note that currently it is being assumed that a pointer returned
148  from this function may be used to enumerate struct sam_passwd
149  entries as well as struct smb_passwd entries. This may need
150  to change. JRA.
151
152 ****************************************************************/
153
154 void *startsmbgrpent(BOOL update)
155 {
156   return pwgrp_ops->startsmbgrpent(update);
157 }
158
159 /***************************************************************
160  End enumeration of the smb or sam passwd list.
161
162  Note that currently it is being assumed that a pointer returned
163  from this function may be used to enumerate struct sam_passwd
164  entries as well as struct smb_passwd entries. This may need
165  to change. JRA.
166
167 ****************************************************************/
168
169 void endsmbgrpent(void *vp)
170 {
171   pwgrp_ops->endsmbgrpent(vp);
172 }
173
174 /*************************************************************************
175  Routine to return the next entry in the smb passwd list.
176  *************************************************************************/
177
178 struct smb_passwd *getsmbgrpent(void *vp,
179                 uint32 **grps, int *num_grps,
180                 uint32 **alss, int *num_alss)
181 {
182         return pwgrp_ops->getsmbgrpent(vp, grps, num_grps, alss, num_alss);
183 }
184
185 /************************************************************************
186  Routine to search smb passwd by name.
187 *************************************************************************/
188
189 struct smb_passwd *getsmbgrpnam(char *name,
190                 uint32 **grps, int *num_grps,
191                 uint32 **alss, int *num_alss)
192 {
193         return pwgrp_ops->getsmbgrpnam(name, grps, num_grps, alss, num_alss);
194 }
195
196 /************************************************************************
197  Routine to search smb passwd by user rid.
198 *************************************************************************/
199
200 struct smb_passwd *getsmbgrprid(uint32 user_rid,
201                 uint32 **grps, int *num_grps,
202                 uint32 **alss, int *num_alss)
203 {
204         return pwgrp_ops->getsmbgrprid(user_rid, grps, num_grps, alss, num_alss);
205 }
206
207 /************************************************************************
208  Routine to search smb passwd by uid.
209 *************************************************************************/
210
211 struct smb_passwd *getsmbgrpuid(uid_t smb_userid,
212                 uint32 **grps, int *num_grps,
213                 uint32 **alss, int *num_alss)
214 {
215         return pwgrp_ops->getsmbgrpuid(smb_userid, grps, num_grps, alss, num_alss);
216 }