This is another rather major change to the samba authenticaion
[kai/samba.git] / source3 / smbd / auth_unix.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2
4    Password and authentication handling
5    Copyright (C) Andrew Bartlett              2001
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /****************************************************************************
25 update the encrypted smbpasswd file from the plaintext username and password
26
27 this ugly hack needs to die, but not quite yet...
28 *****************************************************************************/
29 static BOOL update_smbpassword_file(char *user, char *password)
30 {
31         SAM_ACCOUNT     *sampass = NULL;
32         BOOL            ret;
33         
34         pdb_init_sam(&sampass);
35         
36         become_root();
37         ret = pdb_getsampwnam(sampass, user);
38         unbecome_root();
39
40         if(ret == False) {
41                 DEBUG(0,("pdb_getsampwnam returned NULL\n"));
42                 pdb_free_sam(&sampass);
43                 return False;
44         }
45
46         /*
47          * Remove the account disabled flag - we are updating the
48          * users password from a login.
49          */
50         if (!pdb_set_acct_ctrl(sampass, pdb_get_acct_ctrl(sampass) & ~ACB_DISABLED)) {
51                 pdb_free_sam(&sampass);
52                 return False;
53         }
54
55         if (!pdb_set_plaintext_passwd (sampass, password)) {
56                 pdb_free_sam(&sampass);
57                 return False;
58         }
59
60         /* Now write it into the file. */
61         become_root();
62
63         /* Here, the override flag is True, because we want to ignore the
64            XXXXXXX'd out password */
65         ret = pdb_update_sam_account (sampass, True);
66
67         unbecome_root();
68
69         if (ret) {
70                 DEBUG(3,("pdb_update_sam_account returned %d\n",ret));
71         }
72
73         memset(password, '\0', strlen(password));
74
75         pdb_free_sam(&sampass);
76         return ret;
77 }
78
79
80 /****************************************************************************
81 check if a username/password is OK assuming the password 
82 in PLAIN TEXT
83 ****************************************************************************/
84
85 NTSTATUS check_unix_security(void *my_private_data,
86                              const auth_usersupplied_info *user_info, 
87                              const auth_authsupplied_info *auth_info,
88                              auth_serversupplied_info **server_info)
89 {
90         NTSTATUS nt_status;
91         struct passwd *pass = NULL;
92
93         become_root();
94         pass = Get_Pwnam(user_info->internal_username.str);
95
96         nt_status = pass_check(pass,
97                                 pass ? pass->pw_name : user_info->internal_username.str, 
98                                 (char *)user_info->plaintext_password.data,
99                                 user_info->plaintext_password.length-1,
100                                 lp_update_encrypted() ? 
101                                 update_smbpassword_file : NULL,
102                                 True);
103         
104         unbecome_root();
105
106         if NT_STATUS_IS_OK(nt_status) {
107                 if (pass) {
108                         make_server_info_pw(server_info, pass);
109                 } else {
110                         /* we need to do somthing more useful here */
111                         nt_status = NT_STATUS_NO_SUCH_USER;
112                 }
113         }
114
115         return nt_status;
116 }
117
118 BOOL auth_init_unix(auth_methods **auth_method) 
119 {
120         if (!make_auth_methods(auth_method)) {
121                 return False;
122         }
123         (*auth_method)->auth = check_unix_security;
124         return True;
125 }