053cad9707c15163893c050d2632908c27b1939e
[kamenim/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "../librpc/gen_ndr/samr.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_AUTH
25
26 /**
27  * update the encrypted smbpasswd file from the plaintext username and password
28  *  
29  *  this ugly hack needs to die, but not quite yet, I think people still use it...
30  **/
31 static bool update_smbpassword_file(const char *user, const char *password)
32 {
33         struct samu     *sampass;
34         bool            ret;
35
36         if ( !(sampass = samu_new( NULL )) ) {
37                 return False;
38         }
39
40         become_root();
41         ret = pdb_getsampwnam(sampass, user);
42         unbecome_root();
43
44         if(ret == False) {
45                 DEBUG(0,("pdb_getsampwnam returned NULL\n"));
46                 TALLOC_FREE(sampass);
47                 return False;
48         }
49
50         /*
51          * Remove the account disabled flag - we are updating the
52          * users password from a login.
53          */
54         if (!pdb_set_acct_ctrl(sampass, pdb_get_acct_ctrl(sampass) & ~ACB_DISABLED, PDB_CHANGED)) {
55                 TALLOC_FREE(sampass);
56                 return False;
57         }
58
59         if (!pdb_set_plaintext_passwd (sampass, password)) {
60                 TALLOC_FREE(sampass);
61                 return False;
62         }
63
64         /* Now write it into the file. */
65         become_root();
66
67         ret = NT_STATUS_IS_OK(pdb_update_sam_account (sampass));
68
69         unbecome_root();
70
71         if (ret) {
72                 DEBUG(3,("pdb_update_sam_account returned %d\n",ret));
73         }
74
75         TALLOC_FREE(sampass);
76         return ret;
77 }
78
79
80 /** Check a plaintext username/password
81  *
82  * Cannot deal with an encrupted password in any manner whatsoever,
83  * unless the account has a null password.
84  **/
85
86 static NTSTATUS check_unix_security(const struct auth_context *auth_context,
87                              void *my_private_data, 
88                              TALLOC_CTX *mem_ctx,
89                              const struct auth_usersupplied_info *user_info,
90                              struct auth_serversupplied_info **server_info)
91 {
92         NTSTATUS nt_status;
93         struct passwd *pass = NULL;
94
95         DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
96
97         become_root();
98         pass = Get_Pwnam_alloc(talloc_tos(), user_info->mapped.account_name);
99
100         /** @todo This call assumes a ASCII password, no charset transformation is 
101             done.  We may need to revisit this **/
102         nt_status = pass_check(pass,
103                                 pass ? pass->pw_name : user_info->mapped.account_name,
104                                 (char *)user_info->plaintext_password.data,
105                                 user_info->plaintext_password.length-1,
106                                 lp_update_encrypted() ? 
107                                 update_smbpassword_file : NULL,
108                                 True);
109
110         unbecome_root();
111
112         if (NT_STATUS_IS_OK(nt_status)) {
113                 if (pass) {
114                         /* if a real user check pam account restrictions */
115                         /* only really perfomed if "obey pam restriction" is true */
116                         nt_status = smb_pam_accountcheck(pass->pw_name);
117                         if (  !NT_STATUS_IS_OK(nt_status)) {
118                                 DEBUG(1, ("PAM account restriction prevents user login\n"));
119                         } else {
120                                 make_server_info_pw(server_info, pass->pw_name, pass);
121                         }
122                 } else {
123                         /* we need to do somthing more useful here */
124                         nt_status = NT_STATUS_NO_SUCH_USER;
125                 }
126         }
127
128         TALLOC_FREE(pass);
129         return nt_status;
130 }
131
132 /* module initialisation */
133 static NTSTATUS auth_init_unix(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
134 {
135         struct auth_methods *result;
136
137         result = TALLOC_ZERO_P(auth_context, struct auth_methods);
138         if (result == NULL) {
139                 return NT_STATUS_NO_MEMORY;
140         }
141         result->name = "unix";
142         result->auth = check_unix_security;
143
144         *auth_method = result;
145         return NT_STATUS_OK;
146 }
147
148 NTSTATUS auth_unix_init(void)
149 {
150         return smb_register_auth(AUTH_INTERFACE_VERSION, "unix", auth_init_unix);
151 }