first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[bbaumbach/samba-autobuild/.git] / source3 / passdb / smbpassgroupunix.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_SMBUNIX_DB
23
24 extern int DEBUGLEVEL;
25 extern DOM_SID global_sam_sid;
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 *startsmbunixgrpent(BOOL update)
33 {
34         return startsmbpwent(False);
35 }
36
37 /***************************************************************
38  End enumeration of the smbpasswd list.
39 ****************************************************************/
40
41 static void endsmbunixgrpent(void *vp)
42 {
43         endsmbpwent(vp);
44 }
45
46 /*************************************************************************
47  Return the current position in the smbpasswd list as an SMB_BIG_UINT.
48  This must be treated as an opaque token.
49 *************************************************************************/
50
51 static SMB_BIG_UINT getsmbunixgrppos(void *vp)
52 {
53         return getsmbpwpos(vp);
54 }
55
56 /*************************************************************************
57  Set the current position in the smbpasswd list from an SMB_BIG_UINT.
58  This must be treated as an opaque token.
59 *************************************************************************/
60
61 static BOOL setsmbunixgrppos(void *vp, SMB_BIG_UINT tok)
62 {
63         return setsmbpwpos(vp, tok);
64 }
65
66 /*************************************************************************
67  Routine to return the next smbpassgroup entry
68  *************************************************************************/
69 static struct smb_passwd *getsmbunixgrpent(void *vp,
70                 uint32 **grp_rids, int *num_grps,
71                 uint32 **als_rids, int *num_alss)
72 {
73         /* Static buffers we will return. */
74         struct sam_passwd *pw_buf;
75         fstring unix_name;
76         int i;
77         int unixgrps;
78         gid_t *grps;
79         BOOL failed = False;
80
81         if (vp == NULL)
82         {
83                 DEBUG(0,("getsmbunixgrpent: Bad password file pointer.\n"));
84                 return NULL;
85         }
86
87         pw_buf = getsam21pwent(vp);
88         
89         if (pw_buf == NULL)
90         {
91                 return NULL;
92         }
93
94         fstrcpy(unix_name, pw_buf->unix_name);
95
96         if (grp_rids != NULL)
97         {
98                 (*grp_rids) = NULL;
99                 (*num_grps) = 0;
100         }
101
102         if (als_rids != NULL)
103         {
104                 (*als_rids) = NULL;
105                 (*num_alss) = 0;
106         }
107         
108         if (als_rids == NULL && grp_rids == NULL)
109         {
110                 /* they didn't want to know the members. */
111                 return pwdb_sam_to_smb(pw_buf);
112         }
113
114         /*
115          * find all unix groups
116          */
117
118         if (get_unixgroups(unix_name, pw_buf->unix_uid, pw_buf->unix_gid, &unixgrps, &grps))
119         {
120                 return NULL;
121         }
122
123         /*
124          * check each unix group for a mapping as an nt alias or an nt group
125          */
126
127         for (i = 0; i < unixgrps && !failed; i++)
128         {
129                 uint32 rid;
130
131                 /*
132                  * find the unix name for each user's group.
133                  * assume the unix group is an nt name (alias? group? user?)
134                  * (user or not our own domain will be an error).
135                  *
136                  * oh, oh, can anyone spot what's missing heeere?
137                  * you guessed it: built-in aliases.  those are in
138                  * Domain S-1-5-20, and NT Domain Users can only
139                  * have lists of RIDs as groups.
140                  *
141                  * doesn't stop you making NT Domain Users a member
142                  * of a BUILTIN Alias (e.g "Administrators" or "Power Users")
143                  * it's just that there's no way to tell that from this
144                  * API call: wrong domain, sorry.
145                  *
146                  */
147
148                 DOM_NAME_MAP gmep;
149
150                 if (!lookupsmbgrpgid(grps[i], &gmep))
151                 {
152                         continue;
153                 }
154
155                 sid_split_rid(&gmep.sid, &rid);
156                 if (!sid_equal(&global_sam_sid, &gmep.sid))
157                 {
158                         continue;
159                 }
160
161                 switch (gmep.type)
162                 {
163                         case SID_NAME_ALIAS:
164                         {
165                                 if (als_rids != NULL && add_num_to_list(als_rids, num_alss, rid) == NULL)
166                                 {
167                                         failed = True;
168                                 }
169                                 break;
170                         }
171                         case SID_NAME_DOM_GRP:
172                         case SID_NAME_WKN_GRP:
173                         {
174                                 if (grp_rids != NULL && add_num_to_list(grp_rids, num_grps, rid) == NULL)
175                                 {
176                                         failed = True;
177                                 }
178                                 break;
179                         }
180                         default:
181                         {
182                                 break;
183                         }
184                 }
185         }
186
187         if (failed)
188         {
189                 if (grp_rids != NULL && (*grp_rids) != NULL)
190                 {
191                         free(*grp_rids);
192                         (*num_grps) = 0;
193                 }
194
195                 if (als_rids != NULL && (*als_rids) != NULL)
196                 {
197                         free(*als_rids);
198                         (*num_alss) = 0;
199                 }
200
201                 return NULL;
202         }
203
204         return pwdb_sam_to_smb(pw_buf);
205 }
206
207 static struct passgrp_ops smbunixgrp_ops =
208 {
209         startsmbunixgrpent,
210         endsmbunixgrpent,
211         getsmbunixgrppos,
212         setsmbunixgrppos,
213         iterate_getsmbgrpntnam,          /* In passgrp.c */
214         iterate_getsmbgrpuid,          /* In passgrp.c */
215         iterate_getsmbgrprid,          /* In passgrp.c */
216         getsmbunixgrpent
217 };
218
219 struct passgrp_ops *unix_initialise_password_grp(void)
220 {    
221   return &smbunixgrp_ops;
222 }
223
224 #else
225  /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
226  void smbpassgroupunix_dummy_function(void) { } /* stop some compilers complaining */
227 #endif /* USE_SMBPASS_DB */