sync 3.0 into HEAD for the last time
[samba.git] / source / auth / auth_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
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_AUTH
28
29 extern DOM_SID global_sid_World;
30 extern DOM_SID global_sid_Network;
31 extern DOM_SID global_sid_Builtin_Guests;
32 extern DOM_SID global_sid_Authenticated_Users;
33
34
35 /****************************************************************************
36  Create a UNIX user on demand.
37 ****************************************************************************/
38
39 static int smb_create_user(const char *domain, const char *unix_username, const char *homedir)
40 {
41         pstring add_script;
42         int ret;
43
44         pstrcpy(add_script, lp_adduser_script());
45         if (! *add_script)
46                 return -1;
47         all_string_sub(add_script, "%u", unix_username, sizeof(pstring));
48         if (domain)
49                 all_string_sub(add_script, "%D", domain, sizeof(pstring));
50         if (homedir)
51                 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
52         ret = smbrun(add_script,NULL);
53         DEBUG(3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
54         return ret;
55 }
56
57 /****************************************************************************
58  Add and Delete UNIX users on demand, based on NTSTATUS codes.
59  We don't care about RID's here so ignore.
60 ****************************************************************************/
61
62 void auth_add_user_script(const char *domain, const char *username)
63 {
64         uint32 rid;
65         /*
66          * User validated ok against Domain controller.
67          * If the admin wants us to try and create a UNIX
68          * user on the fly, do so.
69          */
70         
71         if ( *lp_adduser_script() )
72                 smb_create_user(domain, username, NULL);
73         else {
74                 DEBUG(10,("auth_add_user_script: no 'add user script'.  Asking winbindd\n"));
75                 
76                 /* should never get here is we a re a domain member running winbindd
77                    However, a host set for 'security = server' might run winbindd for 
78                    account allocation */
79                    
80                 if ( !winbind_create_user(username, NULL) ) {
81                         DEBUG(5,("auth_add_user_script: winbindd_create_user() failed\n"));
82                         rid = 0;
83                 }
84         }
85 }
86
87 /****************************************************************************
88  Create a SAM_ACCOUNT - either by looking in the pdb, or by faking it up from
89  unix info.
90 ****************************************************************************/
91
92 NTSTATUS auth_get_sam_account(const char *user, SAM_ACCOUNT **account) 
93 {
94         BOOL pdb_ret;
95         NTSTATUS nt_status;
96         if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(account))) {
97                 return nt_status;
98         }
99         
100         become_root();
101         pdb_ret = pdb_getsampwnam(*account, user);
102         unbecome_root();
103
104         if (!pdb_ret) {
105                 
106                 struct passwd *pass = Get_Pwnam(user);
107                 if (!pass) 
108                         return NT_STATUS_NO_SUCH_USER;
109
110                 if (!NT_STATUS_IS_OK(nt_status = pdb_fill_sam_pw(*account, pass))) {
111                         return nt_status;
112                 }
113         }
114         return NT_STATUS_OK;
115 }
116
117 /****************************************************************************
118  Create an auth_usersupplied_data structure
119 ****************************************************************************/
120
121 static NTSTATUS make_user_info(auth_usersupplied_info **user_info, 
122                                const char *smb_name, 
123                                const char *internal_username,
124                                const char *client_domain, 
125                                const char *domain,
126                                const char *wksta_name, 
127                                DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
128                                DATA_BLOB plaintext, 
129                                uint32 auth_flags, BOOL encrypted)
130 {
131
132         DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
133
134         *user_info = malloc(sizeof(**user_info));
135         if (!user_info) {
136                 DEBUG(0,("malloc failed for user_info (size %lu)\n", (unsigned long)sizeof(*user_info)));
137                 return NT_STATUS_NO_MEMORY;
138         }
139
140         ZERO_STRUCTP(*user_info);
141
142         DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
143
144         (*user_info)->smb_name.str = strdup(smb_name);
145         if ((*user_info)->smb_name.str) { 
146                 (*user_info)->smb_name.len = strlen(smb_name);
147         } else {
148                 free_user_info(user_info);
149                 return NT_STATUS_NO_MEMORY;
150         }
151         
152         (*user_info)->internal_username.str = strdup(internal_username);
153         if ((*user_info)->internal_username.str) { 
154                 (*user_info)->internal_username.len = strlen(internal_username);
155         } else {
156                 free_user_info(user_info);
157                 return NT_STATUS_NO_MEMORY;
158         }
159
160         (*user_info)->domain.str = strdup(domain);
161         if ((*user_info)->domain.str) { 
162                 (*user_info)->domain.len = strlen(domain);
163         } else {
164                 free_user_info(user_info);
165                 return NT_STATUS_NO_MEMORY;
166         }
167
168         (*user_info)->client_domain.str = strdup(client_domain);
169         if ((*user_info)->client_domain.str) { 
170                 (*user_info)->client_domain.len = strlen(client_domain);
171         } else {
172                 free_user_info(user_info);
173                 return NT_STATUS_NO_MEMORY;
174         }
175
176         (*user_info)->wksta_name.str = strdup(wksta_name);
177         if ((*user_info)->wksta_name.str) { 
178                 (*user_info)->wksta_name.len = strlen(wksta_name);
179         } else {
180                 free_user_info(user_info);
181                 return NT_STATUS_NO_MEMORY;
182         }
183
184         DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
185
186         (*user_info)->lm_resp = data_blob(lm_pwd.data, lm_pwd.length);
187         (*user_info)->nt_resp = data_blob(nt_pwd.data, nt_pwd.length);
188         (*user_info)->plaintext_password = data_blob(plaintext.data, plaintext.length);
189
190         (*user_info)->encrypted = encrypted;
191         (*user_info)->auth_flags = auth_flags;
192
193         DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
194
195         return NT_STATUS_OK;
196 }
197
198 /****************************************************************************
199  Create an auth_usersupplied_data structure after appropriate mapping.
200 ****************************************************************************/
201
202 NTSTATUS make_user_info_map(auth_usersupplied_info **user_info, 
203                             const char *smb_name, 
204                             const char *client_domain, 
205                             const char *wksta_name, 
206                             DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
207                             DATA_BLOB plaintext, 
208                             uint32 ntlmssp_flags, BOOL encrypted)
209 {
210         const char *domain;
211         fstring internal_username;
212         fstrcpy(internal_username, smb_name);
213         map_username(internal_username); 
214         
215         DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
216               client_domain, smb_name, wksta_name));
217         
218         /* don't allow "" as a domain, fixes a Win9X bug 
219            where it doens't supply a domain for logon script
220            'net use' commands.                                 */
221
222         if ( *client_domain )
223                 domain = client_domain;
224         else
225                 domain = lp_workgroup();
226
227         /* do what win2k does.  Always map unknown domains to our own
228            and let the "passdb backend" handle unknown users. */
229
230         if ( !is_trusted_domain(domain) && !strequal(domain, get_global_sam_name()) ) 
231                 domain = get_default_sam_name();
232         
233         /* we know that it is a trusted domain (and we are allowing them) or it is our domain */
234         
235         return make_user_info(user_info, smb_name, internal_username, 
236                 client_domain, domain, wksta_name, lm_pwd, nt_pwd,
237                 plaintext, ntlmssp_flags, encrypted);
238 }
239
240 /****************************************************************************
241  Create an auth_usersupplied_data, making the DATA_BLOBs here. 
242  Decrypt and encrypt the passwords.
243 ****************************************************************************/
244
245 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info, 
246                                      const char *smb_name, 
247                                      const char *client_domain, 
248                                      const char *wksta_name, 
249                                      const uchar *lm_network_pwd, int lm_pwd_len,
250                                      const uchar *nt_network_pwd, int nt_pwd_len)
251 {
252         BOOL ret;
253         NTSTATUS nt_status;
254         DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
255         DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
256         DATA_BLOB plaintext_blob = data_blob(NULL, 0);
257         uint32 auth_flags = AUTH_FLAG_NONE;
258
259         if (lm_pwd_len)
260                 auth_flags |= AUTH_FLAG_LM_RESP;
261         if (nt_pwd_len == 24) {
262                 auth_flags |= AUTH_FLAG_NTLM_RESP; 
263         } else if (nt_pwd_len != 0) {
264                 auth_flags |= AUTH_FLAG_NTLMv2_RESP; 
265         }
266
267         nt_status = make_user_info_map(user_info,
268                                       smb_name, client_domain, 
269                                   wksta_name, 
270                                       lm_blob, nt_blob,
271                                       plaintext_blob, 
272                                       auth_flags, True);
273         
274         ret = NT_STATUS_IS_OK(nt_status) ? True : False;
275                 
276         data_blob_free(&lm_blob);
277         data_blob_free(&nt_blob);
278         return ret;
279 }
280
281 /****************************************************************************
282  Create an auth_usersupplied_data, making the DATA_BLOBs here. 
283  Decrypt and encrypt the passwords.
284 ****************************************************************************/
285
286 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info, 
287                                          const char *smb_name, 
288                                          const char *client_domain, 
289                                          const char *wksta_name, 
290                                          const uchar chal[8], 
291                                          const uchar lm_interactive_pwd[16], 
292                                          const uchar nt_interactive_pwd[16], 
293                                          const uchar *dc_sess_key)
294 {
295         char lm_pwd[16];
296         char nt_pwd[16];
297         unsigned char local_lm_response[24];
298         unsigned char local_nt_response[24];
299         unsigned char key[16];
300         uint32 auth_flags = AUTH_FLAG_NONE;
301         
302         ZERO_STRUCT(key);
303         memcpy(key, dc_sess_key, 8);
304         
305         if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
306         if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
307         
308 #ifdef DEBUG_PASSWORD
309         DEBUG(100,("key:"));
310         dump_data(100, (char *)key, sizeof(key));
311         
312         DEBUG(100,("lm owf password:"));
313         dump_data(100, lm_pwd, sizeof(lm_pwd));
314         
315         DEBUG(100,("nt owf password:"));
316         dump_data(100, nt_pwd, sizeof(nt_pwd));
317 #endif
318         
319         SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
320         SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
321         
322 #ifdef DEBUG_PASSWORD
323         DEBUG(100,("decrypt of lm owf password:"));
324         dump_data(100, lm_pwd, sizeof(lm_pwd));
325         
326         DEBUG(100,("decrypt of nt owf password:"));
327         dump_data(100, nt_pwd, sizeof(nt_pwd));
328 #endif
329         
330         SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
331         SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
332         
333         /* Password info paranoia */
334         ZERO_STRUCT(lm_pwd);
335         ZERO_STRUCT(nt_pwd);
336         ZERO_STRUCT(key);
337
338         {
339                 BOOL ret;
340                 NTSTATUS nt_status;
341                 DATA_BLOB local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
342                 DATA_BLOB local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
343                 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
344
345                 if (lm_interactive_pwd)
346                         auth_flags |= AUTH_FLAG_LM_RESP;
347                 if (nt_interactive_pwd)
348                         auth_flags |= AUTH_FLAG_NTLM_RESP; 
349
350                 nt_status = make_user_info_map(user_info, 
351                                                smb_name, client_domain, 
352                                                wksta_name, 
353                                                local_lm_blob,
354                                                local_nt_blob,
355                                                plaintext_blob, 
356                                                auth_flags, True);
357                 
358                 ret = NT_STATUS_IS_OK(nt_status) ? True : False;
359                 data_blob_free(&local_lm_blob);
360                 data_blob_free(&local_nt_blob);
361                 return ret;
362         }
363 }
364
365
366 /****************************************************************************
367  Create an auth_usersupplied_data structure
368 ****************************************************************************/
369
370 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info, 
371                               const char *smb_name, 
372                               const char *client_domain,
373                               const uint8 chal[8],
374                               DATA_BLOB plaintext_password)
375 {
376
377         DATA_BLOB local_lm_blob;
378         DATA_BLOB local_nt_blob;
379         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
380         uint32 auth_flags = AUTH_FLAG_NONE;
381                         
382         /*
383          * Not encrypted - do so.
384          */
385         
386         DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
387         
388         if (plaintext_password.data) {
389                 unsigned char local_lm_response[24];
390                 
391 #ifdef DEBUG_PASSWORD
392                 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
393                 dump_data(100, plaintext_password.data, plaintext_password.length);
394 #endif
395
396                 SMBencrypt( (const char *)plaintext_password.data, (const uchar*)chal, local_lm_response);
397                 local_lm_blob = data_blob(local_lm_response, 24);
398                 
399                 /* We can't do an NT hash here, as the password needs to be
400                    case insensitive */
401                 local_nt_blob = data_blob(NULL, 0); 
402                 
403                 auth_flags = (AUTH_FLAG_PLAINTEXT | AUTH_FLAG_LM_RESP);
404         } else {
405                 local_lm_blob = data_blob(NULL, 0); 
406                 local_nt_blob = data_blob(NULL, 0); 
407         }
408         
409         ret = make_user_info_map(user_info, smb_name,
410                                  client_domain, 
411                                  get_remote_machine_name(),
412                                  local_lm_blob,
413                                  local_nt_blob,
414                                  plaintext_password, 
415                                  auth_flags, False);
416         
417         data_blob_free(&local_lm_blob);
418         return NT_STATUS_IS_OK(ret) ? True : False;
419 }
420
421 /****************************************************************************
422  Create an auth_usersupplied_data structure
423 ****************************************************************************/
424
425 NTSTATUS make_user_info_for_reply_enc(auth_usersupplied_info **user_info, 
426                                       const char *smb_name,
427                                       const char *client_domain, 
428                                       DATA_BLOB lm_resp, DATA_BLOB nt_resp)
429 {
430         uint32 auth_flags = AUTH_FLAG_NONE;
431
432         DATA_BLOB no_plaintext_blob = data_blob(NULL, 0); 
433         
434         if (lm_resp.length == 24) {
435                 auth_flags |= AUTH_FLAG_LM_RESP;
436         }
437         if (nt_resp.length == 0) {
438         } else if (nt_resp.length == 24) {
439                 auth_flags |= AUTH_FLAG_NTLM_RESP;
440         } else {
441                 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
442         }
443
444         return make_user_info_map(user_info, smb_name, 
445                                  client_domain, 
446                                  get_remote_machine_name(), 
447                                  lm_resp, 
448                                  nt_resp, 
449                                  no_plaintext_blob, 
450                                  auth_flags, True);
451 }
452
453 /****************************************************************************
454  Create a guest user_info blob, for anonymous authenticaion.
455 ****************************************************************************/
456
457 BOOL make_user_info_guest(auth_usersupplied_info **user_info) 
458 {
459         DATA_BLOB lm_blob = data_blob(NULL, 0);
460         DATA_BLOB nt_blob = data_blob(NULL, 0);
461         DATA_BLOB plaintext_blob = data_blob(NULL, 0);
462         uint32 auth_flags = AUTH_FLAG_NONE;
463         NTSTATUS nt_status;
464
465         nt_status = make_user_info(user_info, 
466                               "","", 
467                               "","", 
468                               "", 
469                               nt_blob, lm_blob,
470                               plaintext_blob, 
471                               auth_flags, True);
472                               
473         return NT_STATUS_IS_OK(nt_status) ? True : False;
474 }
475
476 /****************************************************************************
477  prints a NT_USER_TOKEN to debug output.
478 ****************************************************************************/
479
480 void debug_nt_user_token(int dbg_class, int dbg_lev, NT_USER_TOKEN *token)
481 {
482         fstring sid_str;
483         size_t     i;
484         
485         if (!token) {
486                 DEBUGC(dbg_class, dbg_lev, ("NT user token: (NULL)\n"));
487                 return;
488         }
489         
490         DEBUGC(dbg_class, dbg_lev, ("NT user token of user %s\n",
491                                     sid_to_string(sid_str, &token->user_sids[0]) ));
492         DEBUGADDC(dbg_class, dbg_lev, ("contains %lu SIDs\n", (unsigned long)token->num_sids));
493         for (i = 0; i < token->num_sids; i++)
494                 DEBUGADDC(dbg_class, dbg_lev, ("SID[%3lu]: %s\n", (unsigned long)i, 
495                                                sid_to_string(sid_str, &token->user_sids[i])));
496 }
497
498 /****************************************************************************
499  prints a UNIX 'token' to debug output.
500 ****************************************************************************/
501
502 void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid, int n_groups, gid_t *groups)
503 {
504         int     i;
505         DEBUGC(dbg_class, dbg_lev, ("UNIX token of user %ld\n", (long int)uid));
506
507         DEBUGADDC(dbg_class, dbg_lev, ("Primary group is %ld and contains %i supplementary groups\n", (long int)gid, n_groups));
508         for (i = 0; i < n_groups; i++)
509                 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i, 
510                         (long int)groups[i]));
511 }
512
513 /****************************************************************************
514  Create the SID list for this user.
515 ****************************************************************************/
516
517 static NTSTATUS create_nt_user_token(const DOM_SID *user_sid, const DOM_SID *group_sid, 
518                                      int n_groupSIDs, DOM_SID *groupSIDs, 
519                                      BOOL is_guest, NT_USER_TOKEN **token)
520 {
521         NTSTATUS       nt_status = NT_STATUS_OK;
522         NT_USER_TOKEN *ptoken;
523         int i;
524         int sid_ndx;
525         
526         if ((ptoken = malloc( sizeof(NT_USER_TOKEN) ) ) == NULL) {
527                 DEBUG(0, ("create_nt_token: Out of memory allocating token\n"));
528                 nt_status = NT_STATUS_NO_MEMORY;
529                 return nt_status;
530         }
531
532         ZERO_STRUCTP(ptoken);
533
534         ptoken->num_sids = n_groupSIDs + 5;
535
536         if ((ptoken->user_sids = (DOM_SID *)malloc( sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
537                 DEBUG(0, ("create_nt_token: Out of memory allocating SIDs\n"));
538                 nt_status = NT_STATUS_NO_MEMORY;
539                 return nt_status;
540         }
541         
542         memset((char*)ptoken->user_sids,0,sizeof(DOM_SID) * ptoken->num_sids);
543         
544         /*
545          * Note - user SID *MUST* be first in token !
546          * se_access_check depends on this.
547          *
548          * Primary group SID is second in token. Convention.
549          */
550
551         sid_copy(&ptoken->user_sids[PRIMARY_USER_SID_INDEX], user_sid);
552         if (group_sid)
553                 sid_copy(&ptoken->user_sids[PRIMARY_GROUP_SID_INDEX], group_sid);
554
555         /*
556          * Finally add the "standard" SIDs.
557          * The only difference between guest and "anonymous" (which we
558          * don't really support) is the addition of Authenticated_Users.
559          */
560
561         sid_copy(&ptoken->user_sids[2], &global_sid_World);
562         sid_copy(&ptoken->user_sids[3], &global_sid_Network);
563
564         if (is_guest)
565                 sid_copy(&ptoken->user_sids[4], &global_sid_Builtin_Guests);
566         else
567                 sid_copy(&ptoken->user_sids[4], &global_sid_Authenticated_Users);
568         
569         sid_ndx = 5; /* next available spot */
570
571         for (i = 0; i < n_groupSIDs; i++) {
572                 size_t check_sid_idx;
573                 for (check_sid_idx = 1; check_sid_idx < ptoken->num_sids; check_sid_idx++) {
574                         if (sid_equal(&ptoken->user_sids[check_sid_idx], 
575                                       &groupSIDs[i])) {
576                                 break;
577                         }
578                 }
579                 
580                 if (check_sid_idx >= ptoken->num_sids) /* Not found already */ {
581                         sid_copy(&ptoken->user_sids[sid_ndx++], &groupSIDs[i]);
582                 } else {
583                         ptoken->num_sids--;
584                 }
585         }
586         
587         debug_nt_user_token(DBGC_AUTH, 10, ptoken);
588         
589         *token = ptoken;
590
591         return nt_status;
592 }
593
594 /****************************************************************************
595  Create the SID list for this user.
596 ****************************************************************************/
597
598 NT_USER_TOKEN *create_nt_token(uid_t uid, gid_t gid, int ngroups, gid_t *groups, BOOL is_guest)
599 {
600         DOM_SID user_sid;
601         DOM_SID group_sid;
602         DOM_SID *group_sids;
603         NT_USER_TOKEN *token;
604         int i;
605
606         if (!NT_STATUS_IS_OK(uid_to_sid(&user_sid, uid))) {
607                 return NULL;
608         }
609         if (!NT_STATUS_IS_OK(gid_to_sid(&group_sid, gid))) {
610                 return NULL;
611         }
612
613         group_sids = malloc(sizeof(DOM_SID) * ngroups);
614         if (!group_sids) {
615                 DEBUG(0, ("create_nt_token: malloc() failed for DOM_SID list!\n"));
616                 return NULL;
617         }
618
619         for (i = 0; i < ngroups; i++) {
620                 if (!NT_STATUS_IS_OK(gid_to_sid(&(group_sids)[i], (groups)[i]))) {
621                         DEBUG(1, ("create_nt_token: failed to convert gid %ld to a sid!\n", (long int)groups[i]));
622                         SAFE_FREE(group_sids);
623                         return NULL;
624                 }
625         }
626
627         if (!NT_STATUS_IS_OK(create_nt_user_token(&user_sid, &group_sid, 
628                                                   ngroups, group_sids, is_guest, &token))) {
629                 SAFE_FREE(group_sids);
630                 return NULL;
631         }
632
633         SAFE_FREE(group_sids);
634
635         return token;
636 }
637
638 /******************************************************************************
639  * this function returns the groups (SIDs) of the local SAM the user is in.
640  * If this samba server is a DC of the domain the user belongs to, it returns 
641  * both domain groups and local / builtin groups. If the user is in a trusted
642  * domain, or samba is a member server of a domain, then this function returns
643  * local and builtin groups the user is a member of.
644  *
645  * currently this is a hack, as there is no sam implementation that is capable
646  * of groups.
647  ******************************************************************************/
648
649 static NTSTATUS get_user_groups(const char *username, uid_t uid, gid_t gid,
650                                 int *n_groups, DOM_SID **groups, gid_t **unix_groups)
651 {
652         int             n_unix_groups;
653         int             i;
654
655         *n_groups = 0;
656         *groups   = NULL;
657         
658         /* Try winbind first */
659
660         if ( strchr(username, *lp_winbind_separator()) ) {
661                 n_unix_groups = winbind_getgroups( username, unix_groups );
662
663                 DEBUG(10,("get_user_groups: winbind_getgroups(%s): result = %s\n", username, 
664                           n_unix_groups == -1 ? "FAIL" : "SUCCESS"));
665                           
666                 if ( n_unix_groups == -1 )
667                         return NT_STATUS_NO_SUCH_USER; /* what should this return value be? */  
668         }
669         else {
670                 /* fallback to getgrouplist() */
671                 
672                 n_unix_groups = groups_max();
673                 
674                 if ((*unix_groups = malloc( sizeof(gid_t) * n_unix_groups ) ) == NULL) {
675                         DEBUG(0, ("get_user_groups: Out of memory allocating unix group list\n"));
676                         return NT_STATUS_NO_MEMORY;
677                 }
678         
679                 if (sys_getgrouplist(username, gid, *unix_groups, &n_unix_groups) == -1) {
680                 
681                         gid_t *groups_tmp;
682                         
683                         groups_tmp = Realloc(*unix_groups, sizeof(gid_t) * n_unix_groups);
684                         
685                         if (!groups_tmp) {
686                                 SAFE_FREE(*unix_groups);
687                                 return NT_STATUS_NO_MEMORY;
688                         }
689                         *unix_groups = groups_tmp;
690
691                         if (sys_getgrouplist(username, gid, *unix_groups, &n_unix_groups) == -1) {
692                                 DEBUG(0, ("get_user_groups: failed to get the unix group list\n"));
693                                 SAFE_FREE(*unix_groups);
694                                 return NT_STATUS_NO_SUCH_USER; /* what should this return value be? */
695                         }
696                 }
697         }
698
699         debug_unix_user_token(DBGC_CLASS, 5, uid, gid, n_unix_groups, *unix_groups);
700         
701         /* now setup the space for storing the SIDS */
702         
703         if (n_unix_groups > 0) {
704         
705                 *groups   = malloc(sizeof(DOM_SID) * n_unix_groups);
706                 
707                 if (!*groups) {
708                         DEBUG(0, ("get_user_group: malloc() failed for DOM_SID list!\n"));
709                         SAFE_FREE(*unix_groups);
710                         return NT_STATUS_NO_MEMORY;
711                 }
712         }
713
714         *n_groups = n_unix_groups;
715
716         for (i = 0; i < *n_groups; i++) {
717                 if (!NT_STATUS_IS_OK(gid_to_sid(&(*groups)[i], (*unix_groups)[i]))) {
718                         DEBUG(1, ("get_user_groups: failed to convert gid %ld to a sid!\n", 
719                                 (long int)(*unix_groups)[i+1]));
720                         SAFE_FREE(*groups);
721                         SAFE_FREE(*unix_groups);
722                         return NT_STATUS_NO_SUCH_USER;
723                 }
724         }
725                      
726         return NT_STATUS_OK;
727 }
728
729 /***************************************************************************
730  Make a user_info struct
731 ***************************************************************************/
732
733 static NTSTATUS make_server_info(auth_serversupplied_info **server_info)
734 {
735         *server_info = malloc(sizeof(**server_info));
736         if (!*server_info) {
737                 DEBUG(0,("make_server_info: malloc failed!\n"));
738                 return NT_STATUS_NO_MEMORY;
739         }
740         ZERO_STRUCTP(*server_info);
741
742         /* Initialise the uid and gid values to something non-zero
743            which may save us from giving away root access if there
744            is a bug in allocating these fields. */
745
746         (*server_info)->uid = -1;
747         (*server_info)->gid = -1;
748
749         return NT_STATUS_OK;
750 }
751
752 /***************************************************************************
753 Fill a server_info struct from a SAM_ACCOUNT with their groups
754 ***************************************************************************/
755
756 static NTSTATUS add_user_groups(auth_serversupplied_info **server_info, 
757                                 SAM_ACCOUNT *sampass,
758                                 uid_t uid, gid_t gid)
759 {
760         NTSTATUS nt_status;
761         const DOM_SID *user_sid = pdb_get_user_sid(sampass);
762         const DOM_SID *group_sid = pdb_get_group_sid(sampass);
763         int       n_groupSIDs = 0;
764         DOM_SID  *groupSIDs   = NULL;
765         gid_t    *unix_groups = NULL;
766         NT_USER_TOKEN *token;
767         BOOL is_guest;
768         uint32 rid;
769
770         nt_status = get_user_groups(pdb_get_username(sampass), uid, gid, 
771                 &n_groupSIDs, &groupSIDs, &unix_groups);
772                 
773         if (!NT_STATUS_IS_OK(nt_status)) {
774                 DEBUG(4,("get_user_groups_from_local_sam failed\n"));
775                 free_server_info(server_info);
776                 return nt_status;
777         }
778         
779         is_guest = (sid_peek_rid(user_sid, &rid) && rid == DOMAIN_USER_RID_GUEST);
780
781         if (!NT_STATUS_IS_OK(nt_status = create_nt_user_token(user_sid, group_sid,
782                                                               n_groupSIDs, groupSIDs, is_guest, 
783                                                               &token)))
784         {
785                 DEBUG(4,("create_nt_user_token failed\n"));
786                 SAFE_FREE(groupSIDs);
787                 SAFE_FREE(unix_groups);
788                 free_server_info(server_info);
789                 return nt_status;
790         }
791         
792         SAFE_FREE(groupSIDs);
793
794         (*server_info)->n_groups = n_groupSIDs;
795         (*server_info)->groups = unix_groups;
796         (*server_info)->ptok = token;
797
798         return nt_status;
799 }
800
801 /***************************************************************************
802  Make (and fill) a user_info struct from a SAM_ACCOUNT
803 ***************************************************************************/
804
805 NTSTATUS make_server_info_sam(auth_serversupplied_info **server_info, 
806                               SAM_ACCOUNT *sampass)
807 {
808         NTSTATUS nt_status;
809         struct passwd *pwd;
810
811         if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info)))
812                 return nt_status;
813
814         (*server_info)->sam_account    = sampass;
815
816         if ( !(pwd = getpwnam_alloc(pdb_get_username(sampass))) )  {
817                 DEBUG(1, ("User %s in passdb, but getpwnam() fails!\n",
818                           pdb_get_username(sampass)));
819                 free_server_info(server_info);
820                 return NT_STATUS_NO_SUCH_USER;
821         }
822         (*server_info)->unix_name = smb_xstrdup(pwd->pw_name);
823         (*server_info)->gid = pwd->pw_gid;
824         (*server_info)->uid = pwd->pw_uid;
825         
826         passwd_free(&pwd);
827
828         if (!NT_STATUS_IS_OK(nt_status = add_user_groups(server_info, sampass, 
829                                                          (*server_info)->uid, 
830                                                          (*server_info)->gid))) {
831                 free_server_info(server_info);
832                 return nt_status;
833         }
834
835         (*server_info)->sam_fill_level = SAM_FILL_ALL;
836         DEBUG(5,("make_server_info_sam: made server info for user %s -> %s\n",
837                  pdb_get_username(sampass),
838                  (*server_info)->unix_name));
839
840         return nt_status;
841 }
842
843 /***************************************************************************
844  Make (and fill) a user_info struct from a 'struct passwd' by conversion 
845  to a SAM_ACCOUNT
846 ***************************************************************************/
847
848 NTSTATUS make_server_info_pw(auth_serversupplied_info **server_info, const struct passwd *pwd)
849 {
850         NTSTATUS nt_status;
851         SAM_ACCOUNT *sampass = NULL;
852         if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_pw(&sampass, pwd))) {             
853                 return nt_status;
854         }
855         if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info))) {
856                 return nt_status;
857         }
858
859         (*server_info)->sam_account    = sampass;
860
861         if (!NT_STATUS_IS_OK(nt_status = add_user_groups(server_info, sampass, pwd->pw_uid, pwd->pw_gid))) {
862                 return nt_status;
863         }
864
865         (*server_info)->unix_name = smb_xstrdup(pwd->pw_name);
866
867         (*server_info)->sam_fill_level = SAM_FILL_ALL;
868         (*server_info)->uid = pwd->pw_uid;
869         (*server_info)->gid = pwd->pw_gid;
870         return nt_status;
871 }
872
873 /***************************************************************************
874  Make (and fill) a user_info struct for a guest login.
875 ***************************************************************************/
876
877 NTSTATUS make_server_info_guest(auth_serversupplied_info **server_info)
878 {
879         NTSTATUS nt_status;
880         SAM_ACCOUNT *sampass = NULL;
881         DOM_SID guest_sid;
882
883         if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(&sampass))) {
884                 return nt_status;
885         }
886
887         sid_copy(&guest_sid, get_global_sam_sid());
888         sid_append_rid(&guest_sid, DOMAIN_USER_RID_GUEST);
889
890         become_root();
891         if (!pdb_getsampwsid(sampass, &guest_sid)) {
892                 unbecome_root();
893                 return NT_STATUS_NO_SUCH_USER;
894         }
895         unbecome_root();
896
897         nt_status = make_server_info_sam(server_info, sampass);
898
899         if (NT_STATUS_IS_OK(nt_status)) {
900                 (*server_info)->guest = True;
901         }
902
903         return nt_status;
904 }
905
906 /***************************************************************************
907  Purely internal function for make_server_info_info3
908  Fill the sam account from getpwnam
909 ***************************************************************************/
910 static NTSTATUS fill_sam_account(TALLOC_CTX *mem_ctx, 
911                                  const char *domain,
912                                  const char *username,
913                                  char **found_username,
914                                  uid_t *uid, gid_t *gid,
915                                  SAM_ACCOUNT **sam_account)
916 {
917         fstring dom_user;
918         struct passwd *passwd;
919
920         fstr_sprintf(dom_user, "%s%s%s",
921                      domain, lp_winbind_separator(), username);
922
923         passwd = Get_Pwnam(dom_user);
924
925         /* if the lookup for DOMAIN\username failed, try again 
926            with just 'username'.  This is need for accessing the server
927            as a trust user that actually maps to a local account */
928
929         if ( !passwd ) 
930                 passwd = Get_Pwnam(username);
931
932         if (passwd == NULL)
933                 return NT_STATUS_NO_SUCH_USER;
934
935         *uid = passwd->pw_uid;
936         *gid = passwd->pw_gid;
937
938         *found_username = talloc_strdup(mem_ctx, passwd->pw_name);
939
940         return pdb_init_sam_pw(sam_account, passwd);
941 }
942
943 /***************************************************************************
944  Make a server_info struct from the info3 returned by a domain logon 
945 ***************************************************************************/
946
947 NTSTATUS make_server_info_info3(TALLOC_CTX *mem_ctx, 
948                                 const char *internal_username,
949                                 const char *sent_nt_username,
950                                 const char *domain,
951                                 auth_serversupplied_info **server_info, 
952                                 NET_USER_INFO_3 *info3) 
953 {
954         NTSTATUS nt_status = NT_STATUS_OK;
955         char *found_username;
956         const char *nt_domain;
957         const char *nt_username;
958
959         SAM_ACCOUNT *sam_account = NULL;
960         DOM_SID user_sid;
961         DOM_SID group_sid;
962
963         uid_t uid;
964         gid_t gid;
965
966         int n_lgroupSIDs;
967         DOM_SID *lgroupSIDs   = NULL;
968
969         gid_t *unix_groups = NULL;
970         NT_USER_TOKEN *token;
971
972         DOM_SID *all_group_SIDs;
973         size_t i;
974
975         /* 
976            Here is where we should check the list of
977            trusted domains, and verify that the SID 
978            matches.
979         */
980
981         sid_copy(&user_sid, &info3->dom_sid.sid);
982         if (!sid_append_rid(&user_sid, info3->user_rid)) {
983                 return NT_STATUS_INVALID_PARAMETER;
984         }
985         
986         sid_copy(&group_sid, &info3->dom_sid.sid);
987         if (!sid_append_rid(&group_sid, info3->group_rid)) {
988                 return NT_STATUS_INVALID_PARAMETER;
989         }
990
991         if (!(nt_username = unistr2_tdup(mem_ctx, &(info3->uni_user_name)))) {
992                 /* If the server didn't give us one, just use the one we sent them */
993                 nt_username = sent_nt_username;
994         }
995
996         if (!(nt_domain = unistr2_tdup(mem_ctx, &(info3->uni_logon_dom)))) {
997                 /* If the server didn't give us one, just use the one we sent them */
998                 domain = domain;
999         }
1000         
1001         /* try to fill the SAM account..  If getpwnam() fails, then try the 
1002            add user script (2.2.x behavior) */
1003            
1004         nt_status = fill_sam_account(mem_ctx, nt_domain, internal_username,
1005                 &found_username, &uid, &gid, &sam_account);
1006
1007         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1008                 DEBUG(3,("User %s does not exist, trying to add it\n", 
1009                         internal_username));
1010                 auth_add_user_script(nt_domain, internal_username);
1011                 nt_status = fill_sam_account(mem_ctx, nt_domain, 
1012                         internal_username, &found_username,
1013                         &uid, &gid, &sam_account);
1014         }
1015         
1016         if (!NT_STATUS_IS_OK(nt_status)) {
1017                 DEBUG(0, ("make_server_info_info3: pdb_init_sam failed!\n"));
1018                 return nt_status;
1019         }
1020                 
1021         if (!pdb_set_nt_username(sam_account, nt_username, PDB_CHANGED)) {
1022                 pdb_free_sam(&sam_account);
1023                 return NT_STATUS_NO_MEMORY;
1024         }
1025
1026         if (!pdb_set_username(sam_account, nt_username, PDB_CHANGED)) {
1027                 pdb_free_sam(&sam_account);
1028                 return NT_STATUS_NO_MEMORY;
1029         }
1030
1031         if (!pdb_set_domain(sam_account, nt_domain, PDB_CHANGED)) {
1032                 pdb_free_sam(&sam_account);
1033                 return NT_STATUS_NO_MEMORY;
1034         }
1035
1036         if (!pdb_set_user_sid(sam_account, &user_sid, PDB_CHANGED)) {
1037                 pdb_free_sam(&sam_account);
1038                 return NT_STATUS_UNSUCCESSFUL;
1039         }
1040
1041         if (!pdb_set_group_sid(sam_account, &group_sid, PDB_CHANGED)) {
1042                 pdb_free_sam(&sam_account);
1043                 return NT_STATUS_UNSUCCESSFUL;
1044         }
1045                 
1046         if (!pdb_set_fullname(sam_account, unistr2_static(&(info3->uni_full_name)), 
1047                               PDB_CHANGED)) {
1048                 pdb_free_sam(&sam_account);
1049                 return NT_STATUS_NO_MEMORY;
1050         }
1051
1052         if (!pdb_set_logon_script(sam_account, unistr2_static(&(info3->uni_logon_script)), PDB_CHANGED)) {
1053                 pdb_free_sam(&sam_account);
1054                 return NT_STATUS_NO_MEMORY;
1055         }
1056
1057         if (!pdb_set_profile_path(sam_account, unistr2_static(&(info3->uni_profile_path)), PDB_CHANGED)) {
1058                 pdb_free_sam(&sam_account);
1059                 return NT_STATUS_NO_MEMORY;
1060         }
1061
1062         if (!pdb_set_homedir(sam_account, unistr2_static(&(info3->uni_home_dir)), PDB_CHANGED)) {
1063                 pdb_free_sam(&sam_account);
1064                 return NT_STATUS_NO_MEMORY;
1065         }
1066
1067         if (!pdb_set_dir_drive(sam_account, unistr2_static(&(info3->uni_dir_drive)), PDB_CHANGED)) {
1068                 pdb_free_sam(&sam_account);
1069                 return NT_STATUS_NO_MEMORY;
1070         }
1071
1072         if (!NT_STATUS_IS_OK(nt_status = make_server_info(server_info))) {
1073                 DEBUG(4, ("make_server_info failed!\n"));
1074                 pdb_free_sam(&sam_account);
1075                 return nt_status;
1076         }
1077
1078         /* save this here to _net_sam_logon() doesn't fail (it assumes a 
1079            valid SAM_ACCOUNT) */
1080                    
1081         (*server_info)->sam_account = sam_account;
1082
1083         (*server_info)->unix_name = smb_xstrdup(found_username);
1084
1085         /* Fill in the unix info we found on the way */
1086
1087         (*server_info)->sam_fill_level = SAM_FILL_ALL;
1088         (*server_info)->uid = uid;
1089         (*server_info)->gid = gid;
1090
1091         /* Store the user group information in the server_info 
1092            returned to the caller. */
1093         
1094         nt_status = get_user_groups((*server_info)->unix_name,
1095                 uid, gid, &n_lgroupSIDs, &lgroupSIDs, &unix_groups);
1096                 
1097         if ( !NT_STATUS_IS_OK(nt_status) ) {
1098                 DEBUG(4,("get_user_groups failed\n"));
1099                 return nt_status;
1100         }
1101
1102         (*server_info)->groups = unix_groups;
1103         (*server_info)->n_groups = n_lgroupSIDs;
1104         
1105         /* Create a 'combined' list of all SIDs we might want in the SD */
1106         
1107         all_group_SIDs = malloc(sizeof(DOM_SID) * (info3->num_groups2 +info3->num_other_sids));
1108         
1109         if (!all_group_SIDs) {
1110                 DEBUG(0, ("malloc() failed for DOM_SID list!\n"));
1111                 SAFE_FREE(lgroupSIDs);
1112                 free_server_info(server_info);
1113                 return NT_STATUS_NO_MEMORY;
1114         }
1115
1116 #if 0   /* JERRY -- no such thing as local groups in current code */
1117         /* Copy the 'local' sids */
1118         memcpy(all_group_SIDs, lgroupSIDs, sizeof(DOM_SID) * n_lgroupSIDs);
1119         SAFE_FREE(lgroupSIDs);
1120 #endif
1121
1122         /* and create (by appending rids) the 'domain' sids */
1123         
1124         for (i = 0; i < info3->num_groups2; i++) {
1125         
1126                 sid_copy(&all_group_SIDs[i], &(info3->dom_sid.sid));
1127                 
1128                 if (!sid_append_rid(&all_group_SIDs[i], info3->gids[i].g_rid)) {
1129                 
1130                         nt_status = NT_STATUS_INVALID_PARAMETER;
1131                         
1132                         DEBUG(3,("could not append additional group rid 0x%x\n",
1133                                 info3->gids[i].g_rid));                 
1134                                 
1135                         SAFE_FREE(lgroupSIDs);
1136                         free_server_info(server_info);
1137                         
1138                         return nt_status;
1139                         
1140                 }
1141         }
1142
1143         /* Copy 'other' sids.  We need to do sid filtering here to
1144            prevent possible elevation of privileges.  See:
1145
1146            http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
1147          */
1148
1149         for (i = 0; i < info3->num_other_sids; i++) {
1150                 sid_copy(&all_group_SIDs[info3->num_groups2 + i],
1151                          &info3->other_sids[i].sid);
1152         }
1153         
1154         /* Where are the 'global' sids... */
1155
1156         /* can the user be guest? if yes, where is it stored? */
1157         
1158         nt_status = create_nt_user_token(&user_sid, &group_sid,
1159                 info3->num_groups2 + info3->num_other_sids,
1160                 all_group_SIDs, False, &token);
1161                 
1162         if ( !NT_STATUS_IS_OK(nt_status) ) {
1163                 DEBUG(4,("create_nt_user_token failed\n"));
1164                 SAFE_FREE(all_group_SIDs);
1165                 free_server_info(server_info);
1166                 return nt_status;
1167         }
1168
1169         (*server_info)->ptok = token; 
1170
1171         SAFE_FREE(all_group_SIDs);
1172         
1173         memcpy((*server_info)->session_key, info3->user_sess_key, sizeof((*server_info)->session_key)/* 16 */);
1174         memcpy((*server_info)->first_8_lm_hash, info3->padding, 8);
1175
1176         return NT_STATUS_OK;
1177 }
1178
1179 /***************************************************************************
1180  Free a user_info struct
1181 ***************************************************************************/
1182
1183 void free_user_info(auth_usersupplied_info **user_info)
1184 {
1185         DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
1186         if (*user_info != NULL) {
1187                 if ((*user_info)->smb_name.str) {
1188                         DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
1189                 }
1190                 SAFE_FREE((*user_info)->smb_name.str);
1191                 SAFE_FREE((*user_info)->internal_username.str);
1192                 SAFE_FREE((*user_info)->client_domain.str);
1193                 SAFE_FREE((*user_info)->domain.str);
1194                 SAFE_FREE((*user_info)->wksta_name.str);
1195                 data_blob_free(&(*user_info)->lm_resp);
1196                 data_blob_free(&(*user_info)->nt_resp);
1197                 SAFE_FREE((*user_info)->interactive_password);
1198                 data_blob_clear_free(&(*user_info)->plaintext_password);
1199                 ZERO_STRUCT(**user_info);
1200         }
1201         SAFE_FREE(*user_info);
1202 }
1203
1204 /***************************************************************************
1205  Clear out a server_info struct that has been allocated
1206 ***************************************************************************/
1207
1208 void free_server_info(auth_serversupplied_info **server_info)
1209 {
1210         DEBUG(5,("attempting to free (and zero) a server_info structure\n"));
1211         if (*server_info != NULL) {
1212                 pdb_free_sam(&(*server_info)->sam_account);
1213
1214                 /* call pam_end here, unless we know we are keeping it */
1215                 delete_nt_token( &(*server_info)->ptok );
1216                 SAFE_FREE((*server_info)->groups);
1217                 SAFE_FREE((*server_info)->unix_name);
1218                 ZERO_STRUCT(**server_info);
1219         }
1220         SAFE_FREE(*server_info);
1221 }
1222
1223 /***************************************************************************
1224  Make an auth_methods struct
1225 ***************************************************************************/
1226
1227 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method) 
1228 {
1229         if (!auth_context) {
1230                 smb_panic("no auth_context supplied to make_auth_methods()!\n");
1231         }
1232
1233         if (!auth_method) {
1234                 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
1235         }
1236
1237         *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
1238         if (!*auth_method) {
1239                 DEBUG(0,("make_auth_method: malloc failed!\n"));
1240                 return False;
1241         }
1242         ZERO_STRUCTP(*auth_method);
1243         
1244         return True;
1245 }
1246
1247 /****************************************************************************
1248  Delete a SID token.
1249 ****************************************************************************/
1250
1251 void delete_nt_token(NT_USER_TOKEN **pptoken)
1252 {
1253     if (*pptoken) {
1254             NT_USER_TOKEN *ptoken = *pptoken;
1255             SAFE_FREE( ptoken->user_sids );
1256             ZERO_STRUCTP(ptoken);
1257     }
1258     SAFE_FREE(*pptoken);
1259 }
1260
1261 /****************************************************************************
1262  Duplicate a SID token.
1263 ****************************************************************************/
1264
1265 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
1266 {
1267         NT_USER_TOKEN *token;
1268
1269         if (!ptoken)
1270                 return NULL;
1271
1272     if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
1273         return NULL;
1274
1275     ZERO_STRUCTP(token);
1276
1277     if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
1278         SAFE_FREE(token);
1279         return NULL;
1280     }
1281
1282     token->num_sids = ptoken->num_sids;
1283
1284         return token;
1285 }
1286
1287 /**
1288  * Squash an NT_STATUS in line with security requirements.
1289  * In an attempt to avoid giving the whole game away when users
1290  * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and 
1291  * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations 
1292  * (session setups in particular).
1293  *
1294  * @param nt_status NTSTATUS input for squashing.
1295  * @return the 'squashed' nt_status
1296  **/
1297
1298 NTSTATUS nt_status_squash(NTSTATUS nt_status)
1299 {
1300         if NT_STATUS_IS_OK(nt_status) {
1301                 return nt_status;               
1302         } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
1303                 /* Match WinXP and don't give the game away */
1304                 return NT_STATUS_LOGON_FAILURE;
1305                 
1306         } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
1307                 /* Match WinXP and don't give the game away */
1308                 return NT_STATUS_LOGON_FAILURE;
1309         } else {
1310                 return nt_status;
1311         }  
1312 }
1313
1314
1315 /**
1316  * Verify whether or not given domain is trusted.
1317  *
1318  * @param domain_name name of the domain to be verified
1319  * @return true if domain is one of the trusted once or
1320  *         false if otherwise
1321  **/
1322
1323 BOOL is_trusted_domain(const char* dom_name)
1324 {
1325         DOM_SID trustdom_sid;
1326         char *pass = NULL;
1327         time_t lct;
1328         BOOL ret;
1329
1330         /* no trusted domains for a standalone server */
1331
1332         if ( lp_server_role() == ROLE_STANDALONE )
1333                 return False;
1334
1335         /* if we are a DC, then check for a direct trust relationships */
1336
1337         if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) {
1338                 become_root();
1339                 ret = secrets_fetch_trusted_domain_password(dom_name, &pass, &trustdom_sid, &lct);
1340                 unbecome_root();
1341                 SAFE_FREE(pass);
1342                 if (ret)
1343                         return True;
1344         }
1345         else {
1346                 /* if winbindd is not up and we are a domain member) then we need to update the
1347                    trustdom_cache ourselves */
1348
1349                 if ( !winbind_ping() )
1350                         update_trustdom_cache();
1351         }
1352
1353         /* now the trustdom cache should be available a DC could still
1354          * have a transitive trust so fall back to the cache of trusted
1355          * domains (like a domain member would use  */
1356
1357         if ( trustdom_cache_fetch(dom_name, &trustdom_sid) ) {
1358                 return True;
1359         }
1360
1361         return False;
1362 }
1363