summaryrefslogtreecommitdiff
path: root/source3/passdb/smbpassgroupunix.c
blob: 90f804e8efb97f9fab8d65c047a6069fb933953e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 675
 * Mass Ave, Cambridge, MA 02139, USA.
 */

#include "includes.h"

#ifdef USE_SMBUNIX_DB

extern int DEBUGLEVEL;
extern DOM_SID global_member_sid;

/***************************************************************
 Start to enumerate the smbpasswd list. Returns a void pointer
 to ensure no modification outside this module.
****************************************************************/

static void *startsmbunixgrpent(BOOL update)
{
	return startsmbfilepwent(False);
}

/***************************************************************
 End enumeration of the smbpasswd list.
****************************************************************/

static void endsmbunixgrpent(void *vp)
{
	endsmbfilepwent(vp);
}

/*************************************************************************
 Return the current position in the smbpasswd list as an SMB_BIG_UINT.
 This must be treated as an opaque token.
*************************************************************************/

static SMB_BIG_UINT getsmbunixgrppos(void *vp)
{
	return getsmbfilepwpos(vp);
}

/*************************************************************************
 Set the current position in the smbpasswd list from an SMB_BIG_UINT.
 This must be treated as an opaque token.
*************************************************************************/

static BOOL setsmbunixgrppos(void *vp, SMB_BIG_UINT tok)
{
	return setsmbfilepwpos(vp, tok);
}

/*************************************************************************
 Routine to return the next smbpassgroup entry
 *************************************************************************/
static struct smb_passwd *getsmbunixgrpent(void *vp,
		uint32 **grp_rids, int *num_grps,
		uint32 **als_rids, int *num_alss)
{
	/* Static buffers we will return. */
	struct smb_passwd *pw_buf;
	struct passwd *pw;
	int i;
	int unixgrps;
	gid_t *grps;

	if (vp == NULL)
	{
		DEBUG(0,("getsmbunixgrpent: Bad password file pointer.\n"));
		return NULL;
	}

	pw_buf = getsmbfilepwent(vp);
	
	if (grp_rids != NULL)
	{
		(*grp_rids) = NULL;
		(*num_grps) = 0;
	}

	if (als_rids != NULL)
	{
		(*als_rids) = NULL;
		(*num_alss) = 0;
	}
	
	if (als_rids == NULL && grp_rids == NULL)
	{
		return pw_buf;
	}

	/*
	 * find all unix groups
	 */

	pw = Get_Pwnam(pw_buf->smb_name, False);

	if (pw == NULL)
	{
		return NULL;
	}

	if (get_unixgroups(pw_buf->smb_name, pw->pw_uid, pw->pw_gid, &unixgrps, &grps))
	{
		return NULL;
	}

	/*
	 * check each unix group for a mapping as an nt alias or an nt group
	 */

	for (i = 0; i < unixgrps; i++)
	{
		DOM_SID sid;
		char *unix_grpname;
		uint32 status;
		uint32 rid;

		/*
		 * find the unix name for each user's group.
		 * assume the unix group is an nt name (alias? group? user?)
		 * (user or not our own domain will be an error).
		 */

		unix_grpname = gidtoname(grps[i]);
		if (map_unix_alias_name(unix_grpname, &sid, NULL, NULL))
		{
			/*
			 * ok, the unix groupname is mapped to an alias.
			 * check that it is in our domain.
			 */

			sid_split_rid(&sid, &rid);
			if (!sid_equal(&sid, &global_member_sid))
			{
				pstring sid_str;
				sid_to_string(sid_str, &sid);
				DEBUG(0,("user %s is in a UNIX group %s that maps to an NT RID (0x%x) in another domain (%s)\n",
				          pw_buf->smb_name, unix_grpname, rid, sid_str));
				continue;
			}

			if (add_num_to_list(als_rids, num_alss, rid) == NULL)
			{
				return NULL;
			}
		}
		else if (map_unix_group_name(unix_grpname, &sid, NULL, NULL))
		{
			/*
			 * ok, the unix groupname is mapped to a domain group.
			 * check that it is in our domain.
			 */

			sid_split_rid(&sid, &rid);
			if (!sid_equal(&sid, &global_member_sid))
			{
				pstring sid_str;
				sid_to_string(sid_str, &sid);
				DEBUG(0,("user %s is in a UNIX group %s that maps to an NT RID (0x%x) in another domain (%s)\n",
				          pw_buf->smb_name, unix_grpname, rid, sid_str));
				continue;
			}

			if (add_num_to_list(grp_rids, num_grps, rid) == NULL)
			{
				return NULL;
			}
		}
		else if (lp_server_role() == ROLE_DOMAIN_MEMBER)
		{
			/*
			 * server is a member of a domain or stand-alone.
			 * name is not explicitly mapped
			 * so we are responsible for it.
			 * as a LOCAL group.
			 */

			rid = pwdb_gid_to_alias_rid(grps[i]);
			if (add_num_to_list(als_rids, num_alss, rid) == NULL)
			{
				return NULL;
			}
		}
		else if (lp_server_role() != ROLE_DOMAIN_NONE)
		{
			/*
			 * server is a PDC or BDC.
			 * name is explicitly mapped
			 * so we are responsible for it.
			 * as a DOMAIN group.
			 */

			rid = pwdb_gid_to_group_rid(grps[i]);
			if (add_num_to_list(grp_rids, num_grps, rid) == NULL)
			{
				return NULL;
			}
		}
	}

	return pw_buf;
}

static struct passgrp_ops file_ops =
{
	startsmbunixgrpent,
	endsmbunixgrpent,
	getsmbunixgrppos,
	setsmbunixgrppos,
	iterate_getsmbgrpnam,          /* In passgrp.c */
	iterate_getsmbgrpuid,          /* In passgrp.c */
	iterate_getsmbgrprid,          /* In passgrp.c */
	getsmbunixgrpent,
};

struct passgrp_ops *unix_initialise_password_grp(void)
{    
  return &file_ops;
}

#else
 /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
 void smbpass_dummy_function(void) { } /* stop some compilers complaining */
#endif /* USE_SMBPASS_DB */