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