Try and fix the buildfarm by using getpwnam(root) instead
[ira/wip.git] / source3 / auth / token_util.c
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
33 bool 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
48 bool 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
78 NT_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_null("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 defined(DEVELOPER)
95         if ( !(pw = sys_getpwnam("root")) ) {
96                 DEBUG(0,("get_root_nt_token: sys_getpwnam(\"root\") failed!\n"));
97                 return NULL;
98         }
99 #else
100         if ( !(pw = sys_getpwuid(0)) ) {
101                 DEBUG(0,("get_root_nt_token: sys_getpwuid(0) failed!\n"));
102                 return NULL;
103         }
104 #endif
105
106         /* get the user and primary group SIDs; although the
107            BUILTIN\Administrators SId is really the one that matters here */
108
109         uid_to_sid(&u_sid, pw->pw_uid);
110         gid_to_sid(&g_sid, pw->pw_gid);
111
112         token = create_local_nt_token(talloc_autofree_context(), &u_sid, False,
113                                       1, &global_sid_Builtin_Administrators);
114
115         token->privileges = se_disk_operators;
116
117         for_cache = token;
118
119         memcache_add_talloc(
120                 NULL, SINGLETON_CACHE_TALLOC,
121                 data_blob_string_const_null("root_nt_token"), &for_cache);
122
123         return token;
124 }
125
126
127 /*
128  * Add alias SIDs from memberships within the partially created token SID list
129  */
130
131 NTSTATUS add_aliases(const DOM_SID *domain_sid,
132                      struct nt_user_token *token)
133 {
134         uint32 *aliases;
135         size_t i, num_aliases;
136         NTSTATUS status;
137         TALLOC_CTX *tmp_ctx;
138
139         if (!(tmp_ctx = talloc_init("add_aliases"))) {
140                 return NT_STATUS_NO_MEMORY;
141         }
142
143         aliases = NULL;
144         num_aliases = 0;
145
146         status = pdb_enum_alias_memberships(tmp_ctx, domain_sid,
147                                             token->user_sids,
148                                             token->num_sids,
149                                             &aliases, &num_aliases);
150
151         if (!NT_STATUS_IS_OK(status)) {
152                 DEBUG(10, ("pdb_enum_alias_memberships failed: %s\n",
153                            nt_errstr(status)));
154                 goto done;
155         }
156
157         for (i=0; i<num_aliases; i++) {
158                 DOM_SID alias_sid;
159                 sid_compose(&alias_sid, domain_sid, aliases[i]);
160                 status = add_sid_to_array_unique(token, &alias_sid,
161                                                  &token->user_sids,
162                                                  &token->num_sids);
163                 if (!NT_STATUS_IS_OK(status)) {
164                         DEBUG(0, ("add_sid_to_array failed\n"));
165                         goto done;
166                 }
167         }
168
169 done:
170         TALLOC_FREE(tmp_ctx);
171         return NT_STATUS_OK;
172 }
173
174 /*******************************************************************
175 *******************************************************************/
176
177 static NTSTATUS add_builtin_administrators(struct nt_user_token *token,
178                                            const DOM_SID *dom_sid)
179 {
180         DOM_SID domadm;
181         NTSTATUS status;
182
183         /* nothing to do if we aren't in a domain */
184
185         if ( !(IS_DC || lp_server_role()==ROLE_DOMAIN_MEMBER) ) {
186                 return NT_STATUS_OK;
187         }
188
189         /* Find the Domain Admins SID */
190
191         if ( IS_DC ) {
192                 sid_copy( &domadm, get_global_sam_sid() );
193         } else {
194                 sid_copy(&domadm, dom_sid);
195         }
196         sid_append_rid( &domadm, DOMAIN_GROUP_RID_ADMINS );
197
198         /* Add Administrators if the user beloongs to Domain Admins */
199
200         if ( nt_token_check_sid( &domadm, token ) ) {
201                 status = add_sid_to_array(token,
202                                           &global_sid_Builtin_Administrators,
203                                           &token->user_sids, &token->num_sids);
204         if (!NT_STATUS_IS_OK(status)) {
205                         return status;
206                 }
207         }
208
209         return NT_STATUS_OK;
210 }
211
212 /**
213  * Create the requested BUILTIN if it doesn't already exist.  This requires
214  * winbindd to be running.
215  *
216  * @param[in] rid BUILTIN rid to create
217  * @return Normal NTSTATUS return.
218  */
219 static NTSTATUS create_builtin(uint32 rid)
220 {
221         NTSTATUS status = NT_STATUS_OK;
222         DOM_SID sid;
223         gid_t gid;
224
225         if (!sid_compose(&sid, &global_sid_Builtin, rid)) {
226                 return NT_STATUS_NO_SUCH_ALIAS;
227         }
228
229         if (!sid_to_gid(&sid, &gid)) {
230                 if (!lp_winbind_nested_groups() || !winbind_ping()) {
231                         return NT_STATUS_PROTOCOL_UNREACHABLE;
232                 }
233                 status = pdb_create_builtin_alias(rid);
234         }
235         return status;
236 }
237
238 /**
239  * Add sid as a member of builtin_sid.
240  *
241  * @param[in] builtin_sid       An existing builtin group.
242  * @param[in] dom_sid           sid to add as a member of builtin_sid.
243  * @return Normal NTSTATUS return
244  */
245 static NTSTATUS add_sid_to_builtin(const DOM_SID *builtin_sid,
246                                    const DOM_SID *dom_sid)
247 {
248         NTSTATUS status = NT_STATUS_OK;
249
250         if (!dom_sid || !builtin_sid) {
251                 return NT_STATUS_INVALID_PARAMETER;
252         }
253
254         status = pdb_add_aliasmem(builtin_sid, dom_sid);
255
256         if (NT_STATUS_EQUAL(status, NT_STATUS_MEMBER_IN_ALIAS)) {
257                 DEBUG(5, ("add_sid_to_builtin %s is already a member of %s\n",
258                           sid_string_dbg(dom_sid),
259                           sid_string_dbg(builtin_sid)));
260                 return NT_STATUS_OK;
261         }
262
263         if (!NT_STATUS_IS_OK(status)) {
264                 DEBUG(4, ("add_sid_to_builtin %s could not be added to %s: "
265                           "%s\n", sid_string_dbg(dom_sid),
266                           sid_string_dbg(builtin_sid), nt_errstr(status)));
267         }
268         return status;
269 }
270
271 /*******************************************************************
272 *******************************************************************/
273
274 NTSTATUS create_builtin_users(const DOM_SID *dom_sid)
275 {
276         NTSTATUS status;
277         DOM_SID dom_users;
278
279         status = create_builtin(BUILTIN_ALIAS_RID_USERS);
280         if ( !NT_STATUS_IS_OK(status) ) {
281                 DEBUG(5,("create_builtin_users: Failed to create Users\n"));
282                 return status;
283         }
284
285         /* add domain users */
286         if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER))
287                 && sid_compose(&dom_users, dom_sid, DOMAIN_GROUP_RID_USERS))
288         {
289                 status = add_sid_to_builtin(&global_sid_Builtin_Users,
290                                             &dom_users);
291         }
292
293         return status;
294 }
295
296 /*******************************************************************
297 *******************************************************************/
298
299 NTSTATUS create_builtin_administrators(const DOM_SID *dom_sid)
300 {
301         NTSTATUS status;
302         DOM_SID dom_admins, root_sid;
303         fstring root_name;
304         enum lsa_SidType type;
305         TALLOC_CTX *ctx;
306         bool ret;
307
308         status = create_builtin(BUILTIN_ALIAS_RID_ADMINS);
309         if ( !NT_STATUS_IS_OK(status) ) {
310                 DEBUG(5,("create_builtin_administrators: Failed to create Administrators\n"));
311                 return status;
312         }
313
314         /* add domain admins */
315         if ((IS_DC || (lp_server_role() == ROLE_DOMAIN_MEMBER))
316                 && sid_compose(&dom_admins, dom_sid, DOMAIN_GROUP_RID_ADMINS))
317         {
318                 status = add_sid_to_builtin(&global_sid_Builtin_Administrators,
319                                             &dom_admins);
320                 if (!NT_STATUS_IS_OK(status)) {
321                         return status;
322                 }
323         }
324
325         /* add root */
326         if ( (ctx = talloc_init("create_builtin_administrators")) == NULL ) {
327                 return NT_STATUS_NO_MEMORY;
328         }
329         fstr_sprintf( root_name, "%s\\root", get_global_sam_name() );
330         ret = lookup_name(ctx, root_name, LOOKUP_NAME_DOMAIN, NULL, NULL,
331                           &root_sid, &type);
332         TALLOC_FREE( ctx );
333
334         if ( ret ) {
335                 status = add_sid_to_builtin(&global_sid_Builtin_Administrators,
336                                             &root_sid);
337         }
338
339         return status;
340 }
341
342
343 /*******************************************************************
344  Create a NT token for the user, expanding local aliases
345 *******************************************************************/
346
347 struct nt_user_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
348                                             const DOM_SID *user_sid,
349                                             bool is_guest,
350                                             int num_groupsids,
351                                             const DOM_SID *groupsids)
352 {
353         struct nt_user_token *result = NULL;
354         int i;
355         NTSTATUS status;
356         gid_t gid;
357         DOM_SID dom_sid;
358
359         DEBUG(10, ("Create local NT token for %s\n",
360                    sid_string_dbg(user_sid)));
361
362         if (!(result = TALLOC_ZERO_P(mem_ctx, struct nt_user_token))) {
363                 DEBUG(0, ("talloc failed\n"));
364                 return NULL;
365         }
366
367         /* Add the user and primary group sid */
368
369         status = add_sid_to_array(result, user_sid,
370                                   &result->user_sids, &result->num_sids);
371         if (!NT_STATUS_IS_OK(status)) {
372                 return NULL;
373         }
374
375         /* For guest, num_groupsids may be zero. */
376         if (num_groupsids) {
377                 status = add_sid_to_array(result, &groupsids[0],
378                                           &result->user_sids,
379                                           &result->num_sids);
380                 if (!NT_STATUS_IS_OK(status)) {
381                         return NULL;
382                 }
383         }
384
385         /* Add in BUILTIN sids */
386
387         status = add_sid_to_array(result, &global_sid_World,
388                                   &result->user_sids, &result->num_sids);
389         if (!NT_STATUS_IS_OK(status)) {
390                 return NULL;
391         }
392         status = add_sid_to_array(result, &global_sid_Network,
393                                   &result->user_sids, &result->num_sids);
394         if (!NT_STATUS_IS_OK(status)) {
395                 return NULL;
396         }
397
398         if (is_guest) {
399                 status = add_sid_to_array(result, &global_sid_Builtin_Guests,
400                                           &result->user_sids,
401                                           &result->num_sids);
402                 if (!NT_STATUS_IS_OK(status)) {
403                         return NULL;
404                 }
405         } else {
406                 status = add_sid_to_array(result,
407                                           &global_sid_Authenticated_Users,
408                                           &result->user_sids,
409                                           &result->num_sids);
410                 if (!NT_STATUS_IS_OK(status)) {
411                         return NULL;
412                 }
413         }
414
415         /* Now the SIDs we got from authentication. These are the ones from
416          * the info3 struct or from the pdb_enum_group_memberships, depending
417          * on who authenticated the user.
418          * Note that we start the for loop at "1" here, we already added the
419          * first group sid as primary above. */
420
421         for (i=1; i<num_groupsids; i++) {
422                 status = add_sid_to_array_unique(result, &groupsids[i],
423                                                  &result->user_sids,
424                                                  &result->num_sids);
425                 if (!NT_STATUS_IS_OK(status)) {
426                         return NULL;
427                 }
428         }
429
430         /* Deal with the BUILTIN\Administrators group.  If the SID can
431            be resolved then assume that the add_aliasmem( S-1-5-32 )
432            handled it. */
433
434         if (!sid_to_gid(&global_sid_Builtin_Administrators, &gid)) {
435
436                 become_root();
437                 if (!secrets_fetch_domain_sid(lp_workgroup(), &dom_sid)) {
438                         status = NT_STATUS_OK;
439                         DEBUG(3, ("Failed to fetch domain sid for %s\n",
440                                   lp_workgroup()));
441                 } else {
442                         status = create_builtin_administrators(&dom_sid);
443                 }
444                 unbecome_root();
445
446                 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
447                         /* Add BUILTIN\Administrators directly to token. */
448                         status = add_builtin_administrators(result, &dom_sid);
449                         if ( !NT_STATUS_IS_OK(status) ) {
450                                 DEBUG(3, ("Failed to check for local "
451                                           "Administrators membership (%s)\n",
452                                           nt_errstr(status)));
453                         }
454                 } else if (!NT_STATUS_IS_OK(status)) {
455                         DEBUG(2, ("WARNING: Failed to create "
456                                   "BUILTIN\\Administrators group!  Can "
457                                   "Winbind allocate gids?\n"));
458                 }
459         }
460
461         /* Deal with the BUILTIN\Users group.  If the SID can
462            be resolved then assume that the add_aliasmem( S-1-5-32 )
463            handled it. */
464
465         if (!sid_to_gid(&global_sid_Builtin_Users, &gid)) {
466
467                 become_root();
468                 if (!secrets_fetch_domain_sid(lp_workgroup(), &dom_sid)) {
469                         status = NT_STATUS_OK;
470                         DEBUG(3, ("Failed to fetch domain sid for %s\n",
471                                   lp_workgroup()));
472                 } else {
473                         status = create_builtin_users(&dom_sid);
474                 }
475                 unbecome_root();
476
477                 if (!NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE) &&
478                     !NT_STATUS_IS_OK(status))
479                 {
480                         DEBUG(2, ("WARNING: Failed to create BUILTIN\\Users group! "
481                                   "Can Winbind allocate gids?\n"));
482                 }
483         }
484
485         /* Deal with local groups */
486
487         if (lp_winbind_nested_groups()) {
488
489                 become_root();
490
491                 /* Now add the aliases. First the one from our local SAM */
492
493                 status = add_aliases(get_global_sam_sid(), result);
494
495                 if (!NT_STATUS_IS_OK(status)) {
496                         unbecome_root();
497                         TALLOC_FREE(result);
498                         return NULL;
499                 }
500
501                 /* Finally the builtin ones */
502
503                 status = add_aliases(&global_sid_Builtin, result);
504
505                 if (!NT_STATUS_IS_OK(status)) {
506                         unbecome_root();
507                         TALLOC_FREE(result);
508                         return NULL;
509                 }
510
511                 unbecome_root();
512         }
513
514
515         get_privileges_for_sids(&result->privileges, result->user_sids,
516                                 result->num_sids);
517         return result;
518 }
519
520 /****************************************************************************
521  prints a NT_USER_TOKEN to debug output.
522 ****************************************************************************/
523
524 void debug_nt_user_token(int dbg_class, int dbg_lev, NT_USER_TOKEN *token)
525 {
526         size_t     i;
527
528         if (!token) {
529                 DEBUGC(dbg_class, dbg_lev, ("NT user token: (NULL)\n"));
530                 return;
531         }
532
533         DEBUGC(dbg_class, dbg_lev,
534                ("NT user token of user %s\n",
535                 sid_string_dbg(&token->user_sids[0]) ));
536         DEBUGADDC(dbg_class, dbg_lev,
537                   ("contains %lu SIDs\n", (unsigned long)token->num_sids));
538         for (i = 0; i < token->num_sids; i++)
539                 DEBUGADDC(dbg_class, dbg_lev,
540                           ("SID[%3lu]: %s\n", (unsigned long)i,
541                            sid_string_dbg(&token->user_sids[i])));
542
543         dump_se_priv( dbg_class, dbg_lev, &token->privileges );
544 }
545
546 /****************************************************************************
547  prints a UNIX 'token' to debug output.
548 ****************************************************************************/
549
550 void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid,
551                            int n_groups, gid_t *groups)
552 {
553         int     i;
554         DEBUGC(dbg_class, dbg_lev,
555                ("UNIX token of user %ld\n", (long int)uid));
556
557         DEBUGADDC(dbg_class, dbg_lev,
558                   ("Primary group is %ld and contains %i supplementary "
559                    "groups\n", (long int)gid, n_groups));
560         for (i = 0; i < n_groups; i++)
561                 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i,
562                         (long int)groups[i]));
563 }
564
565 /* END */