s4:librpc/rpc: make PyErr_SetDCERPCStatus() static
[Samba/gebeck_regimport.git] / source3 / auth / auth_unix.c
blob86c5a586d95054cc54305697552023e81f3c6744
1 /*
2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett 2001
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.
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.
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/>.
20 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_AUTH
25 /**
26 * update the encrypted smbpasswd file from the plaintext username and password
28 * this ugly hack needs to die, but not quite yet, I think people still use it...
29 **/
30 static bool update_smbpassword_file(const char *user, const char *password)
32 struct samu *sampass;
33 bool ret;
35 if ( !(sampass = samu_new( NULL )) ) {
36 return False;
39 become_root();
40 ret = pdb_getsampwnam(sampass, user);
41 unbecome_root();
43 if(ret == False) {
44 DEBUG(0,("pdb_getsampwnam returned NULL\n"));
45 TALLOC_FREE(sampass);
46 return False;
50 * Remove the account disabled flag - we are updating the
51 * users password from a login.
53 if (!pdb_set_acct_ctrl(sampass, pdb_get_acct_ctrl(sampass) & ~ACB_DISABLED, PDB_CHANGED)) {
54 TALLOC_FREE(sampass);
55 return False;
58 if (!pdb_set_plaintext_passwd (sampass, password)) {
59 TALLOC_FREE(sampass);
60 return False;
63 /* Now write it into the file. */
64 become_root();
66 ret = NT_STATUS_IS_OK(pdb_update_sam_account (sampass));
68 unbecome_root();
70 if (ret) {
71 DEBUG(3,("pdb_update_sam_account returned %d\n",ret));
74 TALLOC_FREE(sampass);
75 return ret;
79 /** Check a plaintext username/password
81 * Cannot deal with an encrupted password in any manner whatsoever,
82 * unless the account has a null password.
83 **/
85 static NTSTATUS check_unix_security(const struct auth_context *auth_context,
86 void *my_private_data,
87 TALLOC_CTX *mem_ctx,
88 const struct auth_usersupplied_info *user_info,
89 struct auth_serversupplied_info **server_info)
91 NTSTATUS nt_status;
92 struct passwd *pass = NULL;
94 become_root();
95 pass = Get_Pwnam_alloc(talloc_tos(), user_info->internal_username);
97 /** @todo This call assumes a ASCII password, no charset transformation is
98 done. We may need to revisit this **/
99 nt_status = pass_check(pass,
100 pass ? pass->pw_name : user_info->internal_username,
101 (char *)user_info->plaintext_password.data,
102 user_info->plaintext_password.length-1,
103 lp_update_encrypted() ?
104 update_smbpassword_file : NULL,
105 True);
107 unbecome_root();
109 if (NT_STATUS_IS_OK(nt_status)) {
110 if (pass) {
111 /* if a real user check pam account restrictions */
112 /* only really perfomed if "obey pam restriction" is true */
113 nt_status = smb_pam_accountcheck(pass->pw_name);
114 if ( !NT_STATUS_IS_OK(nt_status)) {
115 DEBUG(1, ("PAM account restriction prevents user login\n"));
116 } else {
117 make_server_info_pw(server_info, pass->pw_name, pass);
119 } else {
120 /* we need to do somthing more useful here */
121 nt_status = NT_STATUS_NO_SUCH_USER;
125 TALLOC_FREE(pass);
126 return nt_status;
129 /* module initialisation */
130 static NTSTATUS auth_init_unix(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
132 struct auth_methods *result;
134 result = TALLOC_ZERO_P(auth_context, struct auth_methods);
135 if (result == NULL) {
136 return NT_STATUS_NO_MEMORY;
138 result->name = "unix";
139 result->auth = check_unix_security;
141 *auth_method = result;
142 return NT_STATUS_OK;
145 NTSTATUS auth_unix_init(void)
147 return smb_register_auth(AUTH_INTERFACE_VERSION, "unix", auth_init_unix);