[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / pam_smbpass / pam_smb_auth.c
blobdf6d20e01ab5d53b250fd7443ea027738e02d891
1 /* Unix NT password database implementation, version 0.7.5.
3 * This program is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 675
15 * Mass Ave, Cambridge, MA 02139, USA.
18 /* indicate the following groups are defined */
19 #define PAM_SM_AUTH
21 #include "includes.h"
22 #include "debug.h"
24 #ifndef LINUX
26 /* This is only used in the Sun implementation. */
27 #include <security/pam_appl.h>
29 #endif /* LINUX */
31 #include <security/pam_modules.h>
33 #include "general.h"
35 #include "support.h"
37 #define AUTH_RETURN \
38 do { \
39 /* Restore application signal handler */ \
40 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler); \
41 if(ret_data) { \
42 *ret_data = retval; \
43 pam_set_data( pamh, "smb_setcred_return" \
44 , (void *) ret_data, NULL ); \
45 } \
46 return retval; \
47 } while (0)
49 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
50 const char *name, struct samu *sampass, BOOL exist);
54 * pam_sm_authenticate() authenticates users against the samba password file.
56 * First, obtain the password from the user. Then use a
57 * routine in 'support.c' to authenticate the user.
60 #define _SMB_AUTHTOK "-SMB-PASS"
62 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
63 int argc, const char **argv)
65 unsigned int ctrl;
66 int retval, *ret_data = NULL;
67 struct samu *sampass = NULL;
68 extern BOOL in_client;
69 const char *name;
70 void (*oldsig_handler)(int) = NULL;
71 BOOL found;
73 /* Points to memory managed by the PAM library. Do not free. */
74 char *p = NULL;
76 /* Samba initialization. */
77 load_case_tables();
78 setup_logging("pam_smbpass",False);
79 in_client = True;
81 ctrl = set_ctrl(flags, argc, argv);
83 /* Get a few bytes so we can pass our return value to
84 pam_sm_setcred(). */
85 ret_data = SMB_MALLOC_P(int);
87 /* we need to do this before we call AUTH_RETURN */
88 /* Getting into places that might use LDAP -- protect the app
89 from a SIGPIPE it's not expecting */
90 oldsig_handler = CatchSignal(SIGPIPE, SIGNAL_CAST SIG_IGN);
92 /* get the username */
93 retval = pam_get_user( pamh, &name, "Username: " );
94 if ( retval != PAM_SUCCESS ) {
95 if (on( SMB_DEBUG, ctrl )) {
96 _log_err(LOG_DEBUG, "auth: could not identify user");
98 AUTH_RETURN;
100 if (on( SMB_DEBUG, ctrl )) {
101 _log_err( LOG_DEBUG, "username [%s] obtained", name );
104 if (!initialize_password_db(True)) {
105 _log_err( LOG_ALERT, "Cannot access samba password database" );
106 retval = PAM_AUTHINFO_UNAVAIL;
107 AUTH_RETURN;
110 sampass = samu_new( NULL );
111 if (!sampass) {
112 _log_err( LOG_ALERT, "Cannot talloc a samu struct" );
113 retval = nt_status_to_pam(NT_STATUS_NO_MEMORY);
114 AUTH_RETURN;
117 found = pdb_getsampwnam( sampass, name );
119 if (on( SMB_MIGRATE, ctrl )) {
120 retval = _smb_add_user(pamh, ctrl, name, sampass, found);
121 TALLOC_FREE(sampass);
122 AUTH_RETURN;
125 if (!found) {
126 _log_err(LOG_ALERT, "Failed to find entry for user %s.", name);
127 retval = PAM_USER_UNKNOWN;
128 TALLOC_FREE(sampass);
129 sampass = NULL;
130 AUTH_RETURN;
133 /* if this user does not have a password... */
135 if (_smb_blankpasswd( ctrl, sampass )) {
136 TALLOC_FREE(sampass);
137 retval = PAM_SUCCESS;
138 AUTH_RETURN;
141 /* get this user's authentication token */
143 retval = _smb_read_password(pamh, ctrl, NULL, "Password: ", NULL, _SMB_AUTHTOK, &p);
144 if (retval != PAM_SUCCESS ) {
145 _log_err(LOG_CRIT, "auth: no password provided for [%s]", name);
146 TALLOC_FREE(sampass);
147 AUTH_RETURN;
150 /* verify the password of this user */
152 retval = _smb_verify_password( pamh, sampass, p, ctrl );
153 TALLOC_FREE(sampass);
154 p = NULL;
155 AUTH_RETURN;
159 * This function is for setting samba credentials. If anyone comes up
160 * with any credentials they think should be set, let me know.
163 int pam_sm_setcred(pam_handle_t *pamh, int flags,
164 int argc, const char **argv)
166 int retval, *pretval = NULL;
168 retval = PAM_SUCCESS;
170 pam_get_data(pamh, "smb_setcred_return", (const void **) &pretval);
171 if(pretval) {
172 retval = *pretval;
173 SAFE_FREE(pretval);
175 pam_set_data(pamh, "smb_setcred_return", NULL, NULL);
177 return retval;
181 /* Helper function for adding a user to the db. */
182 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
183 const char *name, struct samu *sampass, BOOL exist)
185 pstring err_str;
186 pstring msg_str;
187 const char *pass = NULL;
188 int retval;
190 err_str[0] = '\0';
191 msg_str[0] = '\0';
193 /* Get the authtok; if we don't have one, silently fail. */
194 retval = pam_get_item( pamh, PAM_AUTHTOK, (const void **) &pass );
196 if (retval != PAM_SUCCESS) {
197 _log_err( LOG_ALERT
198 , "pam_get_item returned error to pam_sm_authenticate" );
199 return PAM_AUTHTOK_RECOVER_ERR;
200 } else if (pass == NULL) {
201 return PAM_AUTHTOK_RECOVER_ERR;
204 /* Add the user to the db if they aren't already there. */
205 if (!exist) {
206 retval = NT_STATUS_IS_OK(local_password_change( name, LOCAL_ADD_USER|LOCAL_SET_PASSWORD,
207 pass, err_str,
208 sizeof(err_str),
209 msg_str, sizeof(msg_str) ));
210 if (!retval && *err_str)
212 err_str[PSTRING_LEN-1] = '\0';
213 make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
215 else if (*msg_str)
217 msg_str[PSTRING_LEN-1] = '\0';
218 make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
220 pass = NULL;
222 return PAM_IGNORE;
224 else {
225 /* mimick 'update encrypted' as long as the 'no pw req' flag is not set */
226 if ( pdb_get_acct_ctrl(sampass) & ~ACB_PWNOTREQ )
228 retval = NT_STATUS_IS_OK(local_password_change( name, LOCAL_SET_PASSWORD, pass, err_str, sizeof(err_str),
229 msg_str, sizeof(msg_str) ));
230 if (!retval && *err_str)
232 err_str[PSTRING_LEN-1] = '\0';
233 make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
235 else if (*msg_str)
237 msg_str[PSTRING_LEN-1] = '\0';
238 make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
243 pass = NULL;
245 return PAM_IGNORE;
249 /* static module data */
250 #ifdef PAM_STATIC
251 struct pam_module _pam_smbpass_auth_modstruct = {
252 "pam_smbpass",
253 pam_sm_authenticate,
254 pam_sm_setcred,
255 NULL,
256 NULL,
257 NULL,
258 NULL
260 #endif