ec493b7c0655ff8b24d9e33112b4617bae328df6
[vlendec/samba-autobuild/.git] / source3 / auth / auth.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Password and authentication handling
5    Copyright (C) Andrew Tridgell              1992-2000
6    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
7    Copyright (C) Andrew Bartlett              2001
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 extern int DEBUGLEVEL;
27
28 extern pstring global_myname;
29
30 /****************************************************************************
31  Check user is in correct domain if required
32 ****************************************************************************/
33
34 static BOOL check_domain_match(char *user, char *domain) 
35 {
36   /*
37    * If we aren't serving to trusted domains, we must make sure that
38    * the validation request comes from an account in the same domain
39    * as the Samba server
40    */
41
42   if (!lp_allow_trusted_domains() &&
43       !strequal(lp_workgroup(), domain) ) {
44       DEBUG(1, ("check_domain_match: Attempt to connect as user %s from domain %s denied.\n", user, domain));
45       return False;
46   } else {
47       return True;
48   }
49 }
50
51 /****************************************************************************
52  Check a users password, as given in the user-info struct and return various
53  interesting details in the server_info struct.
54
55  This functions does NOT need to be in a become_root()/unbecome_root() pair
56  as it makes the calls itself when needed.
57 ****************************************************************************/
58
59 NTSTATUS check_password(const auth_usersupplied_info *user_info, 
60                         auth_serversupplied_info *server_info)
61 {
62         
63         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
64         BOOL done_pam = False;
65         
66         DEBUG(3, ("check_password:  Checking password for user %s with the new password interface\n", user_info->smb_username.str));
67         if (!check_domain_match(user_info->smb_username.str, user_info->domain.str)) {
68                 return NT_STATUS_LOGON_FAILURE;
69         }
70
71         if (nt_status != NT_STATUS_NOPROBLEMO) {
72                 nt_status = check_rhosts_security(user_info, server_info);
73         }
74         
75         if ((lp_security() == SEC_DOMAIN) && (nt_status != NT_STATUS_NOPROBLEMO)) {
76                 nt_status = check_domain_security(user_info, server_info);
77         }
78         
79         if ((lp_security() == SEC_SERVER) && (nt_status != NT_STATUS_NOPROBLEMO)) {
80                 nt_status = check_server_security(user_info, server_info);
81         }
82
83         if (lp_security() >= SEC_SERVER) {
84                 smb_user_control(user_info->smb_username.str, nt_status);
85         }
86
87         if (nt_status != NT_STATUS_NOPROBLEMO) {
88                 if ((user_info->plaintext_password.len > 0) 
89                     && (!lp_plaintext_to_smbpasswd())) {
90                         nt_status = check_unix_security(user_info, server_info);
91                         done_pam = True;
92                 } else { 
93                         nt_status = check_smbpasswd_security(user_info, server_info);
94                 }
95         }
96
97         if ((nt_status == NT_STATUS_NOPROBLEMO) && !done_pam) {
98                 /* We might not be root if we are an RPC call */
99                 become_root();
100                 nt_status = smb_pam_accountcheck(user_info->smb_username.str);
101                 unbecome_root();
102         }
103         
104         if (nt_status == NT_STATUS_NOPROBLEMO) {
105                 DEBUG(5, ("check_password:  Password for user %s suceeded\n", user_info->smb_username.str));
106         } else {
107                 DEBUG(3, ("check_password:  Password for user %s FAILED with error %s\n", user_info->smb_username.str, get_nt_error_msg(nt_status)));
108
109         }               
110         return nt_status;
111
112 }
113
114 /****************************************************************************
115  COMPATABILITY INTERFACES:
116  ***************************************************************************/
117
118 /****************************************************************************
119 check if a username/password is OK assuming the password is a 24 byte
120 SMB hash
121 return True if the password is correct, False otherwise
122 ****************************************************************************/
123
124 NTSTATUS pass_check_smb_with_chal(char *user, char *domain, uchar chal[8], 
125                                   uchar *lm_pwd, int lm_pwd_len,
126                                   uchar *nt_pwd, int nt_pwd_len)
127 {
128
129         auth_usersupplied_info user_info;
130         auth_serversupplied_info server_info;
131         AUTH_STR ourdomain, theirdomain, smb_username, wksta_name;
132                 
133         ZERO_STRUCT(user_info);
134         ZERO_STRUCT(ourdomain);
135         ZERO_STRUCT(theirdomain);
136         ZERO_STRUCT(smb_username);
137         ZERO_STRUCT(wksta_name);
138         
139         ourdomain.str = lp_workgroup();
140         ourdomain.len = strlen(ourdomain.str);
141
142         theirdomain.str = domain;
143         theirdomain.len = strlen(theirdomain.str);
144
145         user_info.requested_domain = theirdomain;
146         user_info.domain = ourdomain;
147         
148         smb_username.str = user;
149         smb_username.len = strlen(smb_username.str);
150
151         user_info.requested_username = smb_username;  /* For the time-being */
152         user_info.smb_username = smb_username;
153
154         user_info.wksta_name.str = client_name();
155         user_info.wksta_name.len = strlen(client_name());
156
157         user_info.wksta_name = wksta_name;
158
159         memcpy(user_info.chal, chal, 8);
160
161         if ((lm_pwd_len >= 24 || nt_pwd_len >= 24) || 
162             (lp_encrypted_passwords() && (lm_pwd_len == 0) && lp_null_passwords())) {
163                 /* if 24 bytes long assume it is an encrypted password */
164           
165                 user_info.lm_resp.buffer = (uint8 *)lm_pwd;
166                 user_info.lm_resp.len = lm_pwd_len;
167                 user_info.nt_resp.buffer = (uint8 *)nt_pwd;
168                 user_info.nt_resp.len = nt_pwd_len;
169
170         } else {
171                 unsigned char local_lm_response[24];
172                 unsigned char local_nt_response[24];
173                 
174                 /*
175                  * Not encrypted - do so.
176                  */
177                 
178                 DEBUG(5,("pass_check_smb: User passwords not in encrypted format.\n"));
179
180                 if (lm_pwd_len > 0) {
181                         SMBencrypt( (uchar *)lm_pwd, user_info.chal, local_lm_response);
182                         user_info.lm_resp.buffer = (uint8 *)local_lm_response;
183                         user_info.lm_resp.len = 24;
184
185                         /* This encrypts the lm_pwd feild, which actualy contains the password
186                            rather than the nt_pwd field becouse that contains nothing */
187                         SMBNTencrypt((uchar *)lm_pwd, user_info.chal, local_nt_response);
188                         user_info.nt_resp.buffer = (uint8 *)local_nt_response;
189                         user_info.nt_resp.len = 24;
190                 }
191                 
192                 user_info.plaintext_password.str = (char *)lm_pwd;
193                 user_info.plaintext_password.len = lm_pwd_len;
194
195         }
196
197         return check_password(&user_info, &server_info);
198 }
199
200 NTSTATUS pass_check_smb(char *user, char *domain,
201                         uchar *lm_pwd, int lm_pwd_len,
202                         uchar *nt_pwd, int nt_pwd_len)
203 {
204         uchar chal[8];
205
206         if (!last_challenge(chal)) {
207                 generate_random_buffer( chal, 8, False);
208         }
209
210         return pass_check_smb_with_chal(user, domain, chal, 
211                                         lm_pwd, lm_pwd_len,
212                                         nt_pwd, nt_pwd_len);
213
214 }
215
216 /****************************************************************************
217 check if a username/password pair is OK either via the system password
218 database or the encrypted SMB password database
219 return True if the password is correct, False otherwise
220 ****************************************************************************/
221 BOOL password_ok(char *user, char *password, int pwlen)
222 {
223
224         /* 
225          *  This hack must die!  But until I rewrite the rest of samba
226          *  it must stay - abartlet 2001-08-03
227          */
228
229         if ((pwlen == 0) && !lp_null_passwords()) {
230                 DEBUG(4,("Null passwords not allowed.\n"));
231                 return False;
232         }
233         
234         /* The password could be either NTLM or plain LM.  Try NTLM first, but fall-through as
235            required. */
236         if (pass_check_smb(user, lp_workgroup(), NULL, 0, (unsigned char *)password, pwlen) == NT_STATUS_NOPROBLEMO) {
237                 return True;
238         }
239
240         if (pass_check_smb(user, lp_workgroup(), (unsigned char *)password, pwlen, NULL, 0) == NT_STATUS_NOPROBLEMO) {
241                 return True;
242         }
243
244         return False;
245 }
246