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