syncing up changes in 2.2
[Samba.git] / source / passdb / smbpassgroup.c
blob4636c08c9492c0a4282e2ebab287823c624da005
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.
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.
20 #include "includes.h"
22 #ifdef USE_SMBPASS_DB
24 static int grp_file_lock_depth = 0;
25 extern int DEBUGLEVEL;
27 /***************************************************************
28 Start to enumerate the smbpasswd list. Returns a void pointer
29 to ensure no modification outside this module.
30 ****************************************************************/
32 static void *startsmbfilegrpent(BOOL update)
34 static char s_readbuf[1024];
35 return startfilepwent(lp_smb_passgrp_file(), s_readbuf, sizeof(s_readbuf),
36 &grp_file_lock_depth, update);
39 /***************************************************************
40 End enumeration of the smbpasswd list.
41 ****************************************************************/
43 static void endsmbfilegrpent(void *vp)
45 endfilepwent(vp, &grp_file_lock_depth);
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 *************************************************************************/
53 static SMB_BIG_UINT getsmbfilegrppos(void *vp)
55 return getfilepwpos(vp);
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 *************************************************************************/
63 static BOOL setsmbfilegrppos(void *vp, SMB_BIG_UINT tok)
65 return setfilepwpos(vp, tok);
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)
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 unsigned char *p;
81 int uidval;
82 size_t linebuf_len;
84 if (vp == NULL)
86 DEBUG(0,("getsmbfilegrpent: Bad password file pointer.\n"));
87 return NULL;
90 pwdb_init_smb(&pw_buf);
93 * Scan the file, a line at a time.
95 while ((linebuf_len = getfileline(vp, linebuf, sizeof(linebuf))) > 0)
98 * The line we have should be of the form :-
100 * username:uid:domainrid1,domainrid2..:aliasrid1,aliasrid2..:
104 * As 256 is shorter than a pstring we don't need to check
105 * length here - if this ever changes....
107 p = strncpyn(user_name, linebuf, sizeof(user_name), ':');
109 /* Go past ':' */
110 p++;
112 /* Get smb uid. */
114 p = Atoic((char *) p, &uidval, ":");
116 pw_buf.smb_name = user_name;
117 pw_buf.smb_userid = uidval;
120 * Now get the password value - this should be 32 hex digits
121 * which are the ascii representations of a 16 byte string.
122 * Get two at a time and put them into the password.
125 /* Skip the ':' */
126 p++;
128 if (grp_rids != NULL && num_grps != NULL)
130 int i;
131 p = get_numlist(p, grp_rids, num_grps);
132 if (p == NULL)
134 DEBUG(0,("getsmbfilegrpent: invalid line\n"));
135 return NULL;
137 for (i = 0; i < (*num_grps); i++)
139 (*grp_rids)[i] = pwdb_gid_to_group_rid((*grp_rids)[i]);
143 /* Skip the ':' */
144 p++;
146 if (als_rids != NULL && num_alss != NULL)
148 int i;
149 p = get_numlist(p, als_rids, num_alss);
150 if (p == NULL)
152 DEBUG(0,("getsmbfilegrpent: invalid line\n"));
153 return NULL;
155 for (i = 0; i < (*num_alss); i++)
157 (*als_rids)[i] = pwdb_gid_to_alias_rid((*als_rids)[i]);
161 pwfile = Get_Pwnam(pw_buf.smb_name, False);
162 if (pwfile == NULL)
164 DEBUG(0,("getsmbfilegrpent: smbpasswd database is corrupt!\n"));
165 DEBUG(0,("getsmbfilegrpent: username %s not in unix passwd database!\n", pw_buf.smb_name));
166 return NULL;
169 return &pw_buf;
172 DEBUG(5,("getsmbfilegrpent: end of file reached.\n"));
173 return NULL;
176 static struct passgrp_ops file_ops =
178 startsmbfilegrpent,
179 endsmbfilegrpent,
180 getsmbfilegrppos,
181 setsmbfilegrppos,
182 iterate_getsmbgrpnam, /* In passgrp.c */
183 iterate_getsmbgrpuid, /* In passgrp.c */
184 iterate_getsmbgrprid, /* In passgrp.c */
185 getsmbfilegrpent,
188 struct passgrp_ops *file_initialise_password_grp(void)
190 return &file_ops;
193 #else
194 /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
195 void smbpass_dummy_function(void) { } /* stop some compilers complaining */
196 #endif /* USE_SMBPASS_DB */