Refactor our authentication and authentication testing code.
[sfrench/samba-autobuild/.git] / source3 / auth / auth_sam.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Tridgell              1992-2000
5    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
6    Copyright (C) Andrew Bartlett              2001
7    Copyright (C) Gerald Carter                2003
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 /****************************************************************************
30  Core of smb password checking routine.
31 ****************************************************************************/
32
33 static BOOL smb_pwd_check_ntlmv1(const DATA_BLOB *nt_response,
34                                  const uchar *part_passwd,
35                                  const DATA_BLOB *sec_blob,
36                                  DATA_BLOB *user_sess_key)
37 {
38         /* Finish the encryption of part_passwd. */
39         uchar p24[24];
40         
41         if (part_passwd == NULL) {
42                 DEBUG(10,("No password set - DISALLOWING access\n"));
43                 /* No password set - always false ! */
44                 return False;
45         }
46         
47         if (sec_blob->length != 8) {
48                 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect challenge size (%lu)\n", (unsigned long)sec_blob->length));
49                 return False;
50         }
51         
52         if (nt_response->length != 24) {
53                 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%lu)\n", (unsigned long)nt_response->length));
54                 return False;
55         }
56
57         SMBOWFencrypt(part_passwd, sec_blob->data, p24);
58         if (user_sess_key != NULL) {
59                 *user_sess_key = data_blob(NULL, 16);
60                 SMBsesskeygen_ntv1(part_passwd, NULL, user_sess_key->data);
61         }
62         
63         
64         
65 #if DEBUG_PASSWORD
66         DEBUG(100,("Part password (P16) was |\n"));
67         dump_data(100, part_passwd, 16);
68         DEBUGADD(100,("Password from client was |\n"));
69         dump_data(100, nt_response->data, nt_response->length);
70         DEBUGADD(100,("Given challenge was |\n"));
71         dump_data(100, sec_blob->data, sec_blob->length);
72         DEBUGADD(100,("Value from encryption was |\n"));
73         dump_data(100, p24, 24);
74 #endif
75   return (memcmp(p24, nt_response->data, 24) == 0);
76 }
77
78 /****************************************************************************
79  Core of smb password checking routine. (NTLMv2, LMv2)
80  Note:  The same code works with both NTLMv2 and LMv2.
81 ****************************************************************************/
82
83 static BOOL smb_pwd_check_ntlmv2(const DATA_BLOB *ntv2_response,
84                                  const uchar *part_passwd,
85                                  const DATA_BLOB *sec_blob,
86                                  const char *user, const char *domain,
87                                  DATA_BLOB *user_sess_key)
88 {
89         /* Finish the encryption of part_passwd. */
90         uchar kr[16];
91         uchar value_from_encryption[16];
92         uchar client_response[16];
93         DATA_BLOB client_key_data;
94
95         if (part_passwd == NULL) {
96                 DEBUG(10,("No password set - DISALLOWING access\n"));
97                 /* No password set - always False */
98                 return False;
99         }
100
101         if (ntv2_response->length < 24) {
102                 /* We MUST have more than 16 bytes, or the stuff below will go
103                    crazy.  No known implementation sends less than the 24 bytes
104                    for LMv2, let alone NTLMv2. */
105                 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%lu)\n", 
106                           (unsigned long)ntv2_response->length));
107                 return False;
108         }
109
110         client_key_data = data_blob(ntv2_response->data+16, ntv2_response->length-16);
111         /* 
112            todo:  should we be checking this for anything?  We can't for LMv2, 
113            but for NTLMv2 it is meant to contain the current time etc.
114         */
115
116         memcpy(client_response, ntv2_response->data, sizeof(client_response));
117
118         if (!ntv2_owf_gen(part_passwd, user, domain, kr)) {
119                 return False;
120         }
121
122         SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption);
123         if (user_sess_key != NULL) {
124                 *user_sess_key = data_blob(NULL, 16);
125                 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data);
126         }
127
128 #if DEBUG_PASSWORD
129         DEBUG(100,("Part password (P16) was |\n"));
130         dump_data(100, part_passwd, 16);
131         DEBUGADD(100,("Password from client was |\n"));
132         dump_data(100, ntv2_response->data, ntv2_response->length);
133         DEBUGADD(100,("Variable data from client was |\n"));
134         dump_data(100, client_key_data.data, client_key_data.length);
135         DEBUGADD(100,("Given challenge was |\n"));
136         dump_data(100, sec_blob->data, sec_blob->length);
137         DEBUGADD(100,("Value from encryption was |\n"));
138         dump_data(100, value_from_encryption, 16);
139 #endif
140         data_blob_clear_free(&client_key_data);
141         return (memcmp(value_from_encryption, client_response, 16) == 0);
142 }
143
144 /**
145  * Check a challenge-response password against the value of the NT or
146  * LM password hash.
147  *
148  * @param mem_ctx talloc context
149  * @param challenge 8-byte challenge.  If all zero, forces plaintext comparison
150  * @param nt_response 'unicode' NT response to the challenge, or unicode password
151  * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page
152  * @param username internal Samba username, for log messages
153  * @param client_username username the client used
154  * @param client_domain domain name the client used (may be mapped)
155  * @param nt_pw MD4 unicode password from our passdb or similar
156  * @param lm_pw LANMAN ASCII password from our passdb or similar
157  * @param user_sess_key User session key
158  * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
159  */
160
161 static NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx,
162                                     const DATA_BLOB *challenge,
163                                     const DATA_BLOB *lm_response,
164                                     const DATA_BLOB *nt_response,
165                                     const char *username, 
166                                     const char *client_username, 
167                                     const char *client_domain,
168                                     const uint8 *lm_pw, const uint8 *nt_pw, 
169                                     DATA_BLOB *user_sess_key, 
170                                     DATA_BLOB *lm_sess_key)
171 {
172         static const unsigned char zeros[8];
173         if (nt_pw == NULL) {
174                 DEBUG(3,("sam_password_ok: NO NT password stored for user %s.\n", 
175                          username));
176         }
177
178         /* Check for cleartext netlogon. Used by Exchange 5.5. */
179         if (challenge->length == sizeof(zeros) && 
180             (memcmp(challenge->data, zeros, challenge->length) == 0 )) {
181
182                 DEBUG(4,("sam_password_ok: checking plaintext passwords for user %s\n",
183                          username));
184                 if (nt_pw && nt_response->length) {
185                         unsigned char pwhash[16];
186                         mdfour(pwhash, nt_response->data, nt_response->length);
187                         if (memcmp(pwhash, nt_pw, sizeof(pwhash)) == 0) {
188                                 return NT_STATUS_OK;
189                         } else {
190                                 DEBUG(3,("sam_password_ok: NT (Unicode) plaintext password check failed for user %s\n",
191                                          username));
192                                 return NT_STATUS_WRONG_PASSWORD;
193                         }
194
195                 } else if (!lp_lanman_auth()) {
196                         DEBUG(3,("sam_password_ok: (plaintext password check) LANMAN passwords NOT PERMITTED for user %s\n",
197                                  username));
198
199                 } else if (lm_pw && lm_response->length) {
200                         uchar dospwd[14]; 
201                         uchar p16[16]; 
202                         ZERO_STRUCT(dospwd);
203                         
204                         DEBUG(100, ("DOS password: %s\n"));
205                         memcpy(dospwd, lm_response->data, MIN(lm_response->length, sizeof(dospwd)));
206                         /* Only the fisrt 14 chars are considered, password need not be null terminated. */
207                         E_P16((const unsigned char *)dospwd, p16);
208
209                         dump_data_pw("DOS password (first buffer)\n", dospwd, 14);
210                         dump_data_pw("DOS password (wire DES hash)\n", p16, 16);
211                         dump_data_pw("DOS password (passdb DES hash)\n", lm_pw, 16);
212                         if (memcmp(p16, lm_pw, sizeof(p16)) == 0) {
213                                 return NT_STATUS_OK;
214                         } else {
215                                 DEBUG(3,("sam_password_ok: LANMAN (ASCII) plaintext password check failed for user %s\n",
216                                          username));
217                                 return NT_STATUS_WRONG_PASSWORD;
218                         }
219                 } else {
220                         DEBUG(3, ("Plaintext authentication for user %s attempted, but neither NT nor LM passwords available\n", username));
221                         return NT_STATUS_WRONG_PASSWORD;
222                 }
223         }
224
225         if (nt_response->length != 0 && nt_response->length < 24) {
226                 DEBUG(2,("sam_password_ok: invalid NT password length (%lu) for user %s\n", 
227                          (unsigned long)nt_response->length, username));                
228         }
229         
230         if (nt_response->length >= 24 && nt_pw) {
231                 if (nt_response->length > 24) {
232                         /* We have the NT MD4 hash challenge available - see if we can
233                            use it (ie. does it exist in the smbpasswd file).
234                         */
235                         DEBUG(4,("sam_password_ok: Checking NTLMv2 password with domain [%s]\n", client_domain));
236                         if (smb_pwd_check_ntlmv2( nt_response, 
237                                                   nt_pw, challenge, 
238                                           client_username, 
239                                                   client_domain,
240                                                   user_sess_key)) {
241                                 return NT_STATUS_OK;
242                         }
243                         
244                         DEBUG(4,("sam_password_ok: Checking NTLMv2 password without a domain\n"));
245                         if (smb_pwd_check_ntlmv2( nt_response, 
246                                                   nt_pw, challenge, 
247                                                   client_username, 
248                                                   "",
249                                                   user_sess_key)) {
250                                 return NT_STATUS_OK;
251                         } else {
252                                 DEBUG(3,("sam_password_ok: NTLMv2 password check failed\n"));
253                                 return NT_STATUS_WRONG_PASSWORD;
254                         }
255                 }
256
257                 if (lp_ntlm_auth()) {           
258                         /* We have the NT MD4 hash challenge available - see if we can
259                            use it (ie. does it exist in the smbpasswd file).
260                         */
261                         DEBUG(4,("sam_password_ok: Checking NT MD4 password\n"));
262                         if (smb_pwd_check_ntlmv1(nt_response, 
263                                                  nt_pw, challenge,
264                                                  user_sess_key)) {
265                                 /* The LM session key for this response is not very secure, 
266                                    so use it only if we otherwise allow LM authentication */
267
268                                 if (lp_lanman_auth() && lm_pw) {
269                                         uint8 first_8_lm_hash[16];
270                                         memcpy(first_8_lm_hash, lm_pw, 8);
271                                         memset(first_8_lm_hash + 8, '\0', 8);
272                                         *lm_sess_key = data_blob(first_8_lm_hash, 16);
273                                 }
274                                 return NT_STATUS_OK;
275                         } else {
276                                 DEBUG(3,("sam_password_ok: NT MD4 password check failed for user %s\n",
277                                          username));
278                                 return NT_STATUS_WRONG_PASSWORD;
279                         }
280                 } else {
281                         DEBUG(2,("sam_password_ok: NTLMv1 passwords NOT PERMITTED for user %s\n",
282                                  username));                    
283                         /* no return, becouse we might pick up LMv2 in the LM field */
284                 }
285         }
286         
287         if (lm_response->length == 0) {
288                 DEBUG(3,("sam_password_ok: NEITHER LanMan nor NT password supplied for user %s\n",
289                          username));
290                 return NT_STATUS_WRONG_PASSWORD;
291         }
292         
293         if (lm_response->length < 24) {
294                 DEBUG(2,("sam_password_ok: invalid LanMan password length (%lu) for user %s\n", 
295                          (unsigned long)nt_response->length, username));                
296                 return NT_STATUS_WRONG_PASSWORD;
297         }
298                 
299         if (!lp_lanman_auth()) {
300                 DEBUG(3,("sam_password_ok: Lanman passwords NOT PERMITTED for user %s\n",
301                          username));
302         } else if (!lm_pw) {
303                 DEBUG(3,("sam_password_ok: NO LanMan password set for user %s (and no NT password supplied)\n",
304                          username));
305         } else {
306                 DEBUG(4,("sam_password_ok: Checking LM password\n"));
307                 if (smb_pwd_check_ntlmv1(lm_response, 
308                                          lm_pw, challenge,
309                                          NULL)) {
310                         uint8 first_8_lm_hash[16];
311                         memcpy(first_8_lm_hash, lm_pw, 8);
312                         memset(first_8_lm_hash + 8, '\0', 8);
313                         *user_sess_key = data_blob(first_8_lm_hash, 16);
314                         *lm_sess_key = data_blob(first_8_lm_hash, 16);
315                         return NT_STATUS_OK;
316                 }
317         }
318         
319         if (!nt_pw) {
320                 DEBUG(4,("sam_password_ok: LM password check failed for user, no NT password %s\n",username));
321                 return NT_STATUS_WRONG_PASSWORD;
322         }
323         
324         /* This is for 'LMv2' authentication.  almost NTLMv2 but limited to 24 bytes.
325            - related to Win9X, legacy NAS pass-though authentication
326         */
327         DEBUG(4,("sam_password_ok: Checking LMv2 password with domain %s\n", client_domain));
328         if (smb_pwd_check_ntlmv2( lm_response, 
329                                   nt_pw, challenge, 
330                                   client_username,
331                                   client_domain,
332                                   NULL)) {
333                 return NT_STATUS_OK;
334         }
335         
336         DEBUG(4,("sam_password_ok: Checking LMv2 password without a domain\n"));
337         if (smb_pwd_check_ntlmv2( lm_response, 
338                                   nt_pw, challenge, 
339                                   client_username,
340                                   "",
341                                   NULL)) {
342                 return NT_STATUS_OK;
343         }
344
345         /* Apparently NT accepts NT responses in the LM field
346            - I think this is related to Win9X pass-though authentication
347         */
348         DEBUG(4,("sam_password_ok: Checking NT MD4 password in LM field\n"));
349         if (lp_ntlm_auth()) {
350                 if (smb_pwd_check_ntlmv1(lm_response, 
351                                          nt_pw, challenge,
352                                          NULL)) {
353                         /* The session key for this response is still very odd.  
354                            It not very secure, so use it only if we otherwise 
355                            allow LM authentication */
356
357                         if (lp_lanman_auth() && lm_pw) {
358                                 uint8 first_8_lm_hash[16];
359                                 memcpy(first_8_lm_hash, lm_pw, 8);
360                                 memset(first_8_lm_hash + 8, '\0', 8);
361                                 *user_sess_key = data_blob(first_8_lm_hash, 16);
362                                 *lm_sess_key = data_blob(first_8_lm_hash, 16);
363                         }
364                         return NT_STATUS_OK;
365                 }
366                 DEBUG(3,("sam_password_ok: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username));
367         } else {
368                 DEBUG(3,("sam_password_ok: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username));
369         }
370         return NT_STATUS_WRONG_PASSWORD;
371 }
372
373 /****************************************************************************
374  Do a specific test for an smb password being correct, given a smb_password and
375  the lanman and NT responses.
376 ****************************************************************************/
377
378 static NTSTATUS sam_password_ok(const struct auth_context *auth_context,
379                                 TALLOC_CTX *mem_ctx,
380                                 SAM_ACCOUNT *sampass, 
381                                 const auth_usersupplied_info *user_info, 
382                                 DATA_BLOB *user_sess_key, 
383                                 DATA_BLOB *lm_sess_key)
384 {
385         uint16 acct_ctrl;
386         const uint8 *lm_pw, *nt_pw;
387         const char *username = pdb_get_username(sampass);
388
389         acct_ctrl = pdb_get_acct_ctrl(sampass);
390         if (acct_ctrl & ACB_PWNOTREQ) {
391                 if (lp_null_passwords()) {
392                         DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n", username));
393                         return NT_STATUS_OK;
394                 } else {
395                         DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n", username));
396                         return NT_STATUS_LOGON_FAILURE;
397                 }               
398         }
399
400         lm_pw = pdb_get_lanman_passwd(sampass);
401         nt_pw = pdb_get_nt_passwd(sampass);
402
403         return ntlm_password_check(mem_ctx, &auth_context->challenge, 
404                                    &user_info->lm_resp, &user_info->nt_resp,  
405                                    username, 
406                                    user_info->smb_name.str, 
407                                    user_info->client_domain.str, 
408                                    lm_pw, nt_pw, user_sess_key, lm_sess_key);
409 }
410
411
412 /****************************************************************************
413  Do a specific test for a SAM_ACCOUNT being vaild for this connection 
414  (ie not disabled, expired and the like).
415 ****************************************************************************/
416
417 static NTSTATUS sam_account_ok(TALLOC_CTX *mem_ctx,
418                                SAM_ACCOUNT *sampass, 
419                                const auth_usersupplied_info *user_info)
420 {
421         uint16  acct_ctrl = pdb_get_acct_ctrl(sampass);
422         char *workstation_list;
423         time_t kickoff_time;
424         
425         DEBUG(4,("sam_account_ok: Checking SMB password for user %s\n",pdb_get_username(sampass)));
426
427         /* Quit if the account was disabled. */
428         if (acct_ctrl & ACB_DISABLED) {
429                 DEBUG(1,("sam_account_ok: Account for user '%s' was disabled.\n", pdb_get_username(sampass)));
430                 return NT_STATUS_ACCOUNT_DISABLED;
431         }
432
433         /* Quit if the account was locked out. */
434         if (acct_ctrl & ACB_AUTOLOCK) {
435                 DEBUG(1,("sam_account_ok: Account for user %s was locked out.\n", pdb_get_username(sampass)));
436                 return NT_STATUS_ACCOUNT_LOCKED_OUT;
437         }
438
439         /* Test account expire time */
440         
441         kickoff_time = pdb_get_kickoff_time(sampass);
442         if (kickoff_time != 0 && time(NULL) > kickoff_time) {
443                 DEBUG(1,("sam_account_ok: Account for user '%s' has expired.\n", pdb_get_username(sampass)));
444                 DEBUG(3,("sam_account_ok: Account expired at '%ld' unix time.\n", (long)kickoff_time));
445                 return NT_STATUS_ACCOUNT_EXPIRED;
446         }
447
448         if (!(pdb_get_acct_ctrl(sampass) & ACB_PWNOEXP)) {
449                 time_t must_change_time = pdb_get_pass_must_change_time(sampass);
450                 time_t last_set_time = pdb_get_pass_last_set_time(sampass);
451
452                 /* check for immediate expiry "must change at next logon" */
453                 if (must_change_time == 0 && last_set_time != 0) {
454                         DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n", pdb_get_username(sampass)));
455                         return NT_STATUS_PASSWORD_MUST_CHANGE;
456                 }
457
458                 /* check for expired password */
459                 if (must_change_time < time(NULL) && must_change_time != 0) {
460                         DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n", pdb_get_username(sampass)));
461                         DEBUG(1,("sam_account_ok: Password expired at '%s' (%ld) unix time.\n", http_timestring(must_change_time), (long)must_change_time));
462                         return NT_STATUS_PASSWORD_EXPIRED;
463                 }
464         }
465
466         /* Test workstation. Workstation list is comma separated. */
467
468         workstation_list = talloc_strdup(mem_ctx, pdb_get_workstations(sampass));
469         if (!workstation_list)
470                 return NT_STATUS_NO_MEMORY;
471
472         if (*workstation_list) {
473                 BOOL invalid_ws = True;
474                 const char *s = workstation_list;
475                         
476                 fstring tok;
477                         
478                 while (next_token(&s, tok, ",", sizeof(tok))) {
479                         DEBUG(10,("sam_account_ok: checking for workstation match %s and %s (len=%d)\n",
480                                   tok, user_info->wksta_name.str, user_info->wksta_name.len));
481                         if(strequal(tok, user_info->wksta_name.str)) {
482                                 invalid_ws = False;
483                                 break;
484                         }
485                 }
486                 
487                 if (invalid_ws) 
488                         return NT_STATUS_INVALID_WORKSTATION;
489         }
490
491         if (acct_ctrl & ACB_DOMTRUST) {
492                 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", pdb_get_username(sampass)));
493                 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
494         }
495         
496         if (acct_ctrl & ACB_SVRTRUST) {
497                 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", pdb_get_username(sampass)));
498                 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
499         }
500         
501         if (acct_ctrl & ACB_WSTRUST) {
502                 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", pdb_get_username(sampass)));
503                 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
504         }
505         
506         return NT_STATUS_OK;
507 }
508
509 /****************************************************************************
510 check if a username/password is OK assuming the password is a 24 byte
511 SMB hash supplied in the user_info structure
512 return an NT_STATUS constant.
513 ****************************************************************************/
514
515 static NTSTATUS check_sam_security(const struct auth_context *auth_context,
516                                    void *my_private_data, 
517                                    TALLOC_CTX *mem_ctx,
518                                    const auth_usersupplied_info *user_info, 
519                                    auth_serversupplied_info **server_info)
520 {
521         SAM_ACCOUNT *sampass=NULL;
522         BOOL ret;
523         NTSTATUS nt_status;
524         DATA_BLOB user_sess_key = data_blob(NULL, 0);
525         DATA_BLOB lm_sess_key = data_blob(NULL, 0);
526
527         if (!user_info || !auth_context) {
528                 return NT_STATUS_UNSUCCESSFUL;
529         }
530
531         /* Can't use the talloc version here, because the returned struct gets
532            kept on the server_info */
533         if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(&sampass))) {
534                 return nt_status;
535         }
536
537         /* get the account information */
538
539         become_root();
540         ret = pdb_getsampwnam(sampass, user_info->internal_username.str);
541         unbecome_root();
542
543         if (ret == False) {
544                 DEBUG(3,("check_sam_security: Couldn't find user '%s' in passdb file.\n", user_info->internal_username.str));
545                 pdb_free_sam(&sampass);
546                 return NT_STATUS_NO_SUCH_USER;
547         }
548
549         nt_status = sam_password_ok(auth_context, mem_ctx, sampass, 
550                                     user_info, &user_sess_key, &lm_sess_key);
551         
552         if (!NT_STATUS_IS_OK(nt_status)) {
553                 pdb_free_sam(&sampass);
554                 return nt_status;
555         }
556
557         nt_status = sam_account_ok(mem_ctx, sampass, user_info);
558
559         if (!NT_STATUS_IS_OK(nt_status)) {
560                 pdb_free_sam(&sampass);
561                 return nt_status;
562         }
563
564         if (!NT_STATUS_IS_OK(nt_status = make_server_info_sam(server_info, sampass))) {         
565                 DEBUG(0,("check_sam_security: make_server_info_sam() failed with '%s'\n", nt_errstr(nt_status)));
566                 return nt_status;
567         }
568
569         (*server_info)->nt_session_key = user_sess_key;
570         (*server_info)->lm_session_key = lm_sess_key;
571
572         return nt_status;
573 }
574
575 /* module initialisation */
576 static NTSTATUS auth_init_sam_ignoredomain(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
577 {
578         if (!make_auth_methods(auth_context, auth_method)) {
579                 return NT_STATUS_NO_MEMORY;
580         }
581
582         (*auth_method)->auth = check_sam_security;      
583         (*auth_method)->name = "sam_ignoredomain";
584         return NT_STATUS_OK;
585 }
586
587
588 /****************************************************************************
589 Check SAM security (above) but with a few extra checks.
590 ****************************************************************************/
591
592 static NTSTATUS check_samstrict_security(const struct auth_context *auth_context,
593                                          void *my_private_data, 
594                                          TALLOC_CTX *mem_ctx,
595                                          const auth_usersupplied_info *user_info, 
596                                          auth_serversupplied_info **server_info)
597 {
598         BOOL is_local_name, is_my_domain;
599
600         if (!user_info || !auth_context) {
601                 return NT_STATUS_LOGON_FAILURE;
602         }
603
604         is_local_name = is_myname(user_info->domain.str);
605         is_my_domain  = strequal(user_info->domain.str, lp_workgroup());
606
607         /* check whether or not we service this domain/workgroup name */
608         
609         switch ( lp_server_role() ) {
610                 case ROLE_STANDALONE:
611                 case ROLE_DOMAIN_MEMBER:
612                         if ( !is_local_name ) {
613                                 DEBUG(6,("check_samstrict_security: %s is not one of my local names (%s)\n",
614                                         user_info->domain.str, (lp_server_role() == ROLE_DOMAIN_MEMBER 
615                                         ? "ROLE_DOMAIN_MEMBER" : "ROLE_STANDALONE") ));
616                                 return NT_STATUS_NOT_IMPLEMENTED;
617                         }
618                 case ROLE_DOMAIN_PDC:
619                 case ROLE_DOMAIN_BDC:
620                         if ( !is_local_name && !is_my_domain ) {
621                                 DEBUG(6,("check_samstrict_security: %s is not one of my local names or domain name (DC)\n",
622                                         user_info->domain.str));
623                                 return NT_STATUS_NOT_IMPLEMENTED;
624                         }
625                 default: /* name is ok */
626                         break;
627         }
628         
629         return check_sam_security(auth_context, my_private_data, mem_ctx, user_info, server_info);
630 }
631
632 /* module initialisation */
633 static NTSTATUS auth_init_sam(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
634 {
635         if (!make_auth_methods(auth_context, auth_method)) {
636                 return NT_STATUS_NO_MEMORY;
637         }
638
639         (*auth_method)->auth = check_samstrict_security;
640         (*auth_method)->name = "sam";
641         return NT_STATUS_OK;
642 }
643
644 NTSTATUS auth_sam_init(void)
645 {
646         smb_register_auth(AUTH_INTERFACE_VERSION, "sam", auth_init_sam);
647         smb_register_auth(AUTH_INTERFACE_VERSION, "sam_ignoredomain", auth_init_sam_ignoredomain);
648         return NT_STATUS_OK;
649 }