weekend work. user / group database API.
[ira/wip.git] / source3 / passdb / smbpassgroup.c
1 /*
2  * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
3  * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  * 
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 675
17  * Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "includes.h"
21
22 #ifdef USE_SMBGROUP_DB
23
24 static int grp_file_lock_depth = 0;
25 extern int DEBUGLEVEL;
26
27 /***************************************************************
28  Start to enumerate the smbpasswd list. Returns a void pointer
29  to ensure no modification outside this module.
30 ****************************************************************/
31
32 static void *startsmbfilegrpent(BOOL update)
33 {
34         static char s_readbuf[1024];
35         return startfilepwent(lp_smb_passgrp_file(), s_readbuf, sizeof(s_readbuf),
36                               &grp_file_lock_depth, update);
37 }
38
39 /***************************************************************
40  End enumeration of the smbpasswd list.
41 ****************************************************************/
42
43 static void endsmbfilegrpent(void *vp)
44 {
45         endfilepwent(vp, &grp_file_lock_depth);
46 }
47
48 /*************************************************************************
49  Return the current position in the smbpasswd list as an SMB_BIG_UINT.
50  This must be treated as an opaque token.
51 *************************************************************************/
52
53 static SMB_BIG_UINT getsmbfilegrppos(void *vp)
54 {
55         return getfilepwpos(vp);
56 }
57
58 /*************************************************************************
59  Set the current position in the smbpasswd list from an SMB_BIG_UINT.
60  This must be treated as an opaque token.
61 *************************************************************************/
62
63 static BOOL setsmbfilegrppos(void *vp, SMB_BIG_UINT tok)
64 {
65         return setfilepwpos(vp, tok);
66 }
67
68 /*************************************************************************
69  Routine to return the next entry in the smbpasswd list.
70  *************************************************************************/
71 static struct smb_passwd *getsmbfilegrpent(void *vp,
72                 uint32 **grp_rids, int *num_grps,
73                 uint32 **als_rids, int *num_alss)
74 {
75         /* Static buffers we will return. */
76         static struct smb_passwd pw_buf;
77         static pstring  user_name;
78         struct passwd *pwfile;
79         pstring         linebuf;
80         char  *p;
81         int            uidval;
82
83         if (vp == NULL)
84         {
85                 DEBUG(0,("getsmbfilegrpent: Bad password file pointer.\n"));
86                 return NULL;
87         }
88
89         pwdb_init_smb(&pw_buf);
90
91         /*
92          * Scan the file, a line at a time.
93          */
94         while (getfileline(vp, linebuf, sizeof(linebuf)) > 0)
95         {
96                 /*
97                  * The line we have should be of the form :-
98                  * 
99                  * username:uid:aliasrid1,aliasrid2..:domainrid1,domainrid2..:
100                  */
101
102                 /*
103                  * As 256 is shorter than a pstring we don't need to check
104                  * length here - if this ever changes....
105                  */
106                 p = strncpyn(user_name, linebuf, sizeof(user_name), ':');
107
108                 /* Go past ':' */
109                 p++;
110
111                 /* Get smb uid. */
112
113                 p = Atoic((char *) p, &uidval, ":");
114
115                 pw_buf.smb_name = user_name;
116                 pw_buf.unix_uid = uidval;
117
118                 /*
119                  * Now get a list of alias RIDs
120                  */
121
122                 /* Skip the ':' */
123                 p++;
124
125                 if (als_rids != NULL && num_alss != NULL)
126                 {
127                         int i;
128                         p = get_numlist(p, als_rids, num_alss);
129                         if (p == NULL)
130                         {
131                                 DEBUG(0,("getsmbfilegrpent: invalid line\n"));
132                                 return NULL;
133                         }
134                 }
135
136                 /*
137                  * Now get a list of group RIDs
138                  */
139
140                 /* Skip the ':' */
141                 p++;
142
143                 if (grp_rids != NULL && num_grps != NULL)
144                 {
145                         int i;
146                         p = get_numlist(p, grp_rids, num_grps);
147                         if (p == NULL)
148                         {
149                                 DEBUG(0,("getsmbfilegrpent: invalid line\n"));
150                                 return NULL;
151                         }
152                 }
153
154                 pwfile = Get_Pwnam(pw_buf.smb_name, False);
155                 if (pwfile == NULL)
156                 {
157                         DEBUG(0,("getsmbfilegrpent: smbpasswd database is corrupt!\n"));
158                         DEBUG(0,("getsmbfilegrpent: username %s not in unix passwd database!\n", pw_buf.smb_name));
159                         return NULL;
160                 }
161
162                 return &pw_buf;
163         }
164
165         DEBUG(5,("getsmbfilegrpent: end of file reached.\n"));
166         return NULL;
167 }
168
169 static struct passgrp_ops file_ops =
170 {
171         startsmbfilegrpent,
172         endsmbfilegrpent,
173         getsmbfilegrppos,
174         setsmbfilegrppos,
175         iterate_getsmbgrpnam,          /* In passgrp.c */
176         iterate_getsmbgrpuid,          /* In passgrp.c */
177         iterate_getsmbgrprid,          /* In passgrp.c */
178         getsmbfilegrpent,
179 };
180
181 struct passgrp_ops *file_initialise_password_grp(void)
182 {    
183   return &file_ops;
184 }
185
186 #else
187  /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
188  void smbpass_dummy_function(void) { } /* stop some compilers complaining */
189 #endif /* USE_SMBPASS_DB */