source: branches/samba-3.2.x/source/auth/token_util.c

Last change on this file was 228, checked in by Herwig Bauernfeind, 16 years ago

Update 3.2 branch to 3.2.6

File size: 14.0 KB
Line 
1/*
2 * Unix SMB/CIFS implementation.
3 * Authentication utility functions
4 * Copyright (C) Andrew Tridgell 1992-1998
5 * Copyright (C) Andrew Bartlett 2001
6 * Copyright (C) Jeremy Allison 2000-2001
7 * Copyright (C) Rafal Szczesniak 2002
8 * Copyright (C) Volker Lendecke 2006
9 * Copyright (C) Michael Adam 2007
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 */
24
25/* functions moved from auth/auth_util.c to minimize linker deps */
26
27#include "includes.h"
28
29/****************************************************************************
30 Check for a SID in an NT_USER_TOKEN
31****************************************************************************/
32
33bool nt_token_check_sid ( const DOM_SID *sid, const NT_USER_TOKEN *token )
34{
35 int i;
36
37 if ( !sid || !token )
38 return False;
39
40 for ( i=0; i<token->num_sids; i++ ) {
41 if ( sid_equal( sid, &token->user_sids[i] ) )
42 return True;
43 }
44
45 return False;
46}
47
48bool nt_token_check_domain_rid( NT_USER_TOKEN *token, uint32 rid )
49{
50 DOM_SID domain_sid;
51
52 /* if we are a domain member, the get the domain SID, else for
53 a DC or standalone server, use our own SID */
54
55 if ( lp_server_role() == ROLE_DOMAIN_MEMBER ) {
56 if ( !secrets_fetch_domain_sid( lp_workgroup(),
57 &domain_sid ) ) {
58 DEBUG(1,("nt_token_check_domain_rid: Cannot lookup "
59 "SID for domain [%s]\n", lp_workgroup()));
60 return False;
61 }
62 }
63 else
64 sid_copy( &domain_sid, get_global_sam_sid() );
65
66 sid_append_rid( &domain_sid, rid );
67
68 return nt_token_check_sid( &domain_sid, token );\
69}
70
71/******************************************************************************
72 Create a token for the root user to be used internally by smbd.
73 This is similar to running under the context of the LOCAL_SYSTEM account
74 in Windows. This is a read-only token. Do not modify it or free() it.
75 Create a copy if your need to change it.
76******************************************************************************/
77
78NT_USER_TOKEN *get_root_nt_token( void )
79{
80 struct nt_user_token *token, *for_cache;
81 DOM_SID u_sid, g_sid;
82 struct passwd *pw;
83 void *cache_data;
84
85 cache_data = memcache_lookup_talloc(
86 NULL, SINGLETON_CACHE_TALLOC,
87 data_blob_string_const("root_nt_token"));
88
89 if (cache_data != NULL) {
90 return talloc_get_type_abort(
91 cache_data, struct nt_user_token);
92 }
93
94 if ( !(pw = sys_getpwnam( "root" )) ) {
95 DEBUG(0,("get_root_nt_token: getpwnam(\"root\") failed!\n"));
96 return NULL;
97 }
98
99 /* get the user and primary group SIDs; although the
100 BUILTIN\Administrators SId is really the one that matters here */
101
102 uid_to_sid(&u_sid, pw->pw_uid);
103 gid_to_sid(&g_sid, pw->pw_gid);
104
105 token = create_local_nt_token(NULL, &u_sid, False,
106 1, &global_sid_Builtin_Administrators);
107
108 token->privileges = se_disk_operators;
109
110 for_cache = token;
111
112 memcache_add_talloc(
113 NULL, SINGLETON_CACHE_TALLOC,
114 data_blob_string_const("root_nt_token"), &for_cache);
115
116 return token;
117}
118
119
120/*
121 * Add alias SIDs from memberships within the partially created token SID list
122 */
123
124NTSTATUS add_aliases(const DOM_SID *domain_sid,
125 struct nt_user_token *token)
126{
127 uint32 *aliases;
128 size_t i, num_aliases;
129 NTSTATUS status;
130 TALLOC_CTX *tmp_ctx;
131
132 if (!(tmp_ctx = talloc_init("add_aliases"))) {
133 return NT_STATUS_NO_MEMORY;
134 }
135
136 aliases = NULL;
137 num_aliases = 0;
138
139 status = pdb_enum_alias_memberships(tmp_ctx, domain_sid,
140 token->user_sids,
141 token->num_sids,
142 &aliases, &num_aliases);
143
144 if (!NT_STATUS_IS_OK(status)) {
145 DEBUG(10, ("pdb_enum_alias_memberships failed: %s\n",
146 nt_errstr(status)));
147 goto done;
148 }
149
150 for (i=0; i<num_aliases; i++) {
151 DOM_SID alias_sid;
152 sid_compose(&alias_sid, domain_sid, aliases[i]);
153 status = add_sid_to_array_unique(token, &alias_sid,
154 &token->user_sids,
155 &token->num_sids);
156 if (!NT_STATUS_IS_OK(status)) {
157 DEBUG(0, ("add_sid_to_array failed\n"));
158 goto done;
159 }
160 }
161
162done:
163 TALLOC_FREE(tmp_ctx);
164 return NT_STATUS_OK;
165}
166
167/*******************************************************************
168*******************************************************************/
169
170static NTSTATUS add_builtin_administrators( struct nt_user_token *token )
171{
172 DOM_SID domadm;
173 NTSTATUS status;
174
175 /* nothing to do if we aren't in a domain */
176
177 if ( !(IS_DC || lp_server_role()==ROLE_DOMAIN_MEMBER) ) {
178 return NT_STATUS_OK;
179 }
180
181 /* Find the Domain Admins SID */
182
183 if ( IS_DC ) {
184 sid_copy( &domadm, get_global_sam_sid() );
185 } else {
186 if ( !secrets_fetch_domain_sid( lp_workgroup(), &domadm ) )
187 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
188 }
189 sid_append_rid( &domadm, DOMAIN_GROUP_RID_ADMINS );
190
191 /* Add Administrators if the user beloongs to Domain Admins */
192
193 if ( nt_token_check_sid( &domadm, token ) ) {
194 status = add_sid_to_array(token,
195 &global_sid_Builtin_Administrators,
196 &token->user_sids, &token->num_sids);
197 if (!NT_STATUS_IS_OK(status)) {
198 return status;
199 }
200 }
201
202 return NT_STATUS_OK;
203}
204
205/*******************************************************************
206*******************************************************************/
207
208static NTSTATUS create_builtin_users( void )
209{
210 NTSTATUS status;
211 DOM_SID dom_users;
212
213 status = pdb_create_builtin_alias( BUILTIN_ALIAS_RID_USERS );
214 if ( !NT_STATUS_IS_OK(status) ) {
215 DEBUG(5,("create_builtin_users: Failed to create Users\n"));
216 return status;
217 }
218
219 /* add domain users */
220 if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER))
221 && secrets_fetch_domain_sid(lp_workgroup(), &dom_users))
222 {
223 sid_append_rid(&dom_users, DOMAIN_GROUP_RID_USERS );
224 status = pdb_add_aliasmem( &global_sid_Builtin_Users, &dom_users);
225 if ( !NT_STATUS_IS_OK(status) ) {
226 DEBUG(4,("create_builtin_administrators: Failed to add Domain Users to"
227 " Users\n"));
228 return status;
229 }
230 }
231
232 return NT_STATUS_OK;
233}
234
235/*******************************************************************
236*******************************************************************/
237
238static NTSTATUS create_builtin_administrators( void )
239{
240 NTSTATUS status;
241 DOM_SID dom_admins, root_sid;
242 fstring root_name;
243 enum lsa_SidType type;
244 TALLOC_CTX *ctx;
245 bool ret;
246
247 status = pdb_create_builtin_alias( BUILTIN_ALIAS_RID_ADMINS );
248 if ( !NT_STATUS_IS_OK(status) ) {
249 DEBUG(5,("create_builtin_administrators: Failed to create Administrators\n"));
250 return status;
251 }
252
253 /* add domain admins */
254 if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER))
255 && secrets_fetch_domain_sid(lp_workgroup(), &dom_admins))
256 {
257 sid_append_rid(&dom_admins, DOMAIN_GROUP_RID_ADMINS);
258 status = pdb_add_aliasmem( &global_sid_Builtin_Administrators, &dom_admins );
259 if ( !NT_STATUS_IS_OK(status) ) {
260 DEBUG(4,("create_builtin_administrators: Failed to add Domain Admins"
261 " Administrators\n"));
262 return status;
263 }
264 }
265
266 /* add root */
267 if ( (ctx = talloc_init("create_builtin_administrators")) == NULL ) {
268 return NT_STATUS_NO_MEMORY;
269 }
270 fstr_sprintf( root_name, "%s\\root", get_global_sam_name() );
271 ret = lookup_name(ctx, root_name, LOOKUP_NAME_DOMAIN, NULL, NULL,
272 &root_sid, &type);
273 TALLOC_FREE( ctx );
274
275 if ( ret ) {
276 status = pdb_add_aliasmem( &global_sid_Builtin_Administrators, &root_sid );
277 if ( !NT_STATUS_IS_OK(status) ) {
278 DEBUG(4,("create_builtin_administrators: Failed to add root"
279 " Administrators\n"));
280 return status;
281 }
282 }
283
284 return NT_STATUS_OK;
285}
286
287
288/*******************************************************************
289 Create a NT token for the user, expanding local aliases
290*******************************************************************/
291
292struct nt_user_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
293 const DOM_SID *user_sid,
294 bool is_guest,
295 int num_groupsids,
296 const DOM_SID *groupsids)
297{
298 struct nt_user_token *result = NULL;
299 int i;
300 NTSTATUS status;
301 gid_t gid;
302
303 DEBUG(10, ("Create local NT token for %s\n",
304 sid_string_dbg(user_sid)));
305
306 if (!(result = TALLOC_ZERO_P(mem_ctx, struct nt_user_token))) {
307 DEBUG(0, ("talloc failed\n"));
308 return NULL;
309 }
310
311 /* Add the user and primary group sid */
312
313 status = add_sid_to_array(result, user_sid,
314 &result->user_sids, &result->num_sids);
315 if (!NT_STATUS_IS_OK(status)) {
316 return NULL;
317 }
318
319 /* For guest, num_groupsids may be zero. */
320 if (num_groupsids) {
321 status = add_sid_to_array(result, &groupsids[0],
322 &result->user_sids,
323 &result->num_sids);
324 if (!NT_STATUS_IS_OK(status)) {
325 return NULL;
326 }
327 }
328
329 /* Add in BUILTIN sids */
330
331 status = add_sid_to_array(result, &global_sid_World,
332 &result->user_sids, &result->num_sids);
333 if (!NT_STATUS_IS_OK(status)) {
334 return NULL;
335 }
336 status = add_sid_to_array(result, &global_sid_Network,
337 &result->user_sids, &result->num_sids);
338 if (!NT_STATUS_IS_OK(status)) {
339 return NULL;
340 }
341
342 if (is_guest) {
343 status = add_sid_to_array(result, &global_sid_Builtin_Guests,
344 &result->user_sids,
345 &result->num_sids);
346 if (!NT_STATUS_IS_OK(status)) {
347 return NULL;
348 }
349 } else {
350 status = add_sid_to_array(result,
351 &global_sid_Authenticated_Users,
352 &result->user_sids,
353 &result->num_sids);
354 if (!NT_STATUS_IS_OK(status)) {
355 return NULL;
356 }
357 }
358
359 /* Now the SIDs we got from authentication. These are the ones from
360 * the info3 struct or from the pdb_enum_group_memberships, depending
361 * on who authenticated the user.
362 * Note that we start the for loop at "1" here, we already added the
363 * first group sid as primary above. */
364
365 for (i=1; i<num_groupsids; i++) {
366 status = add_sid_to_array_unique(result, &groupsids[i],
367 &result->user_sids,
368 &result->num_sids);
369 if (!NT_STATUS_IS_OK(status)) {
370 return NULL;
371 }
372 }
373
374 /* Deal with the BUILTIN\Administrators group. If the SID can
375 be resolved then assume that the add_aliasmem( S-1-5-32 )
376 handled it. */
377
378 if ( !sid_to_gid( &global_sid_Builtin_Administrators, &gid ) ) {
379 /* We can only create a mapping if winbind is running
380 and the nested group functionality has been enabled */
381
382 if ( lp_winbind_nested_groups() && winbind_ping() ) {
383 become_root();
384 status = create_builtin_administrators( );
385 if ( !NT_STATUS_IS_OK(status) ) {
386 DEBUG(2,("WARNING: Failed to create BUILTIN\\Administrators "
387 "group! Can Winbind allocate gids?\n"));
388 /* don't fail, just log the message */
389 }
390 unbecome_root();
391 }
392 else {
393 status = add_builtin_administrators( result );
394 if ( !NT_STATUS_IS_OK(status) ) {
395 /* just log a complaint but do not fail */
396 DEBUG(3,("create_local_nt_token: failed to check for local Administrators"
397 " membership (%s)\n", nt_errstr(status)));
398 }
399 }
400 }
401
402 /* Deal with the BUILTIN\Users group. If the SID can
403 be resolved then assume that the add_aliasmem( S-1-5-32 )
404 handled it. */
405
406 if ( !sid_to_gid( &global_sid_Builtin_Users, &gid ) ) {
407 /* We can only create a mapping if winbind is running
408 and the nested group functionality has been enabled */
409
410 if ( lp_winbind_nested_groups() && winbind_ping() ) {
411 become_root();
412 status = create_builtin_users( );
413 if ( !NT_STATUS_IS_OK(status) ) {
414 DEBUG(2,("WARNING: Failed to create BUILTIN\\Users group! "
415 "Can Winbind allocate gids?\n"));
416 /* don't fail, just log the message */
417 }
418 unbecome_root();
419 }
420 }
421
422 /* Deal with local groups */
423
424 if (lp_winbind_nested_groups()) {
425
426 become_root();
427
428 /* Now add the aliases. First the one from our local SAM */
429
430 status = add_aliases(get_global_sam_sid(), result);
431
432 if (!NT_STATUS_IS_OK(status)) {
433 unbecome_root();
434 TALLOC_FREE(result);
435 return NULL;
436 }
437
438 /* Finally the builtin ones */
439
440 status = add_aliases(&global_sid_Builtin, result);
441
442 if (!NT_STATUS_IS_OK(status)) {
443 unbecome_root();
444 TALLOC_FREE(result);
445 return NULL;
446 }
447
448 unbecome_root();
449 }
450
451
452 get_privileges_for_sids(&result->privileges, result->user_sids,
453 result->num_sids);
454 return result;
455}
456
457/****************************************************************************
458 prints a NT_USER_TOKEN to debug output.
459****************************************************************************/
460
461void debug_nt_user_token(int dbg_class, int dbg_lev, NT_USER_TOKEN *token)
462{
463 size_t i;
464
465 if (!token) {
466 DEBUGC(dbg_class, dbg_lev, ("NT user token: (NULL)\n"));
467 return;
468 }
469
470 DEBUGC(dbg_class, dbg_lev,
471 ("NT user token of user %s\n",
472 sid_string_dbg(&token->user_sids[0]) ));
473 DEBUGADDC(dbg_class, dbg_lev,
474 ("contains %lu SIDs\n", (unsigned long)token->num_sids));
475 for (i = 0; i < token->num_sids; i++)
476 DEBUGADDC(dbg_class, dbg_lev,
477 ("SID[%3lu]: %s\n", (unsigned long)i,
478 sid_string_dbg(&token->user_sids[i])));
479
480 dump_se_priv( dbg_class, dbg_lev, &token->privileges );
481}
482
483/****************************************************************************
484 prints a UNIX 'token' to debug output.
485****************************************************************************/
486
487void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid,
488 int n_groups, gid_t *groups)
489{
490 int i;
491 DEBUGC(dbg_class, dbg_lev,
492 ("UNIX token of user %ld\n", (long int)uid));
493
494 DEBUGADDC(dbg_class, dbg_lev,
495 ("Primary group is %ld and contains %i supplementary "
496 "groups\n", (long int)gid, n_groups));
497 for (i = 0; i < n_groups; i++)
498 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i,
499 (long int)groups[i]));
500}
501
502/* END */
Note: See TracBrowser for help on using the repository browser.