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