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 3 of the License, or (at your option)
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
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, see <http://www.gnu.org/licenses/>.
17 /* indicate the following groups are defined */
25 /* This is only used in the Sun implementation. */
26 #if defined(HAVE_SECURITY_PAM_APPL_H)
27 #include <security/pam_appl.h>
28 #elif defined(HAVE_PAM_PAM_APPL_H)
29 #include <pam/pam_appl.h>
34 #if defined(HAVE_SECURITY_PAM_MODULES_H)
35 #include <security/pam_modules.h>
36 #elif defined(HAVE_PAM_PAM_MODULES_H)
37 #include <pam/pam_modules.h>
46 /* Restore application signal handler */ \
47 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler); \
50 pam_set_data( pamh, "smb_setcred_return" \
51 , (void *) ret_data, NULL ); \
56 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
57 const char *name, struct samu *sampass, bool exist);
61 * pam_sm_authenticate() authenticates users against the samba password file.
63 * First, obtain the password from the user. Then use a
64 * routine in 'support.c' to authenticate the user.
67 #define _SMB_AUTHTOK "-SMB-PASS"
69 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
70 int argc, const char **argv)
73 int retval, *ret_data = NULL;
74 struct samu *sampass = NULL;
75 extern bool in_client;
77 void (*oldsig_handler)(int) = NULL;
80 /* Points to memory managed by the PAM library. Do not free. */
83 /* Samba initialization. */
85 setup_logging("pam_smbpass",False);
88 ctrl = set_ctrl(flags, argc, argv);
90 /* Get a few bytes so we can pass our return value to
92 ret_data = SMB_MALLOC_P(int);
94 /* we need to do this before we call AUTH_RETURN */
95 /* Getting into places that might use LDAP -- protect the app
96 from a SIGPIPE it's not expecting */
97 oldsig_handler = CatchSignal(SIGPIPE, SIGNAL_CAST SIG_IGN);
99 /* get the username */
100 retval = pam_get_user( pamh, &name, "Username: " );
101 if ( retval != PAM_SUCCESS ) {
102 if (on( SMB_DEBUG, ctrl )) {
103 _log_err(LOG_DEBUG, "auth: could not identify user");
107 if (on( SMB_DEBUG, ctrl )) {
108 _log_err( LOG_DEBUG, "username [%s] obtained", name );
111 if (geteuid() != 0) {
112 _log_err( LOG_DEBUG, "Cannot access samba password database, not running as root.");
113 retval = PAM_AUTHINFO_UNAVAIL;
117 if (!initialize_password_db(True, NULL)) {
118 _log_err( LOG_ALERT, "Cannot access samba password database" );
119 retval = PAM_AUTHINFO_UNAVAIL;
123 sampass = samu_new( NULL );
125 _log_err( LOG_ALERT, "Cannot talloc a samu struct" );
126 retval = nt_status_to_pam(NT_STATUS_NO_MEMORY);
130 found = pdb_getsampwnam( sampass, name );
132 if (on( SMB_MIGRATE, ctrl )) {
133 retval = _smb_add_user(pamh, ctrl, name, sampass, found);
134 TALLOC_FREE(sampass);
139 _log_err(LOG_ALERT, "Failed to find entry for user %s.", name);
140 retval = PAM_USER_UNKNOWN;
141 TALLOC_FREE(sampass);
146 /* if this user does not have a password... */
148 if (_smb_blankpasswd( ctrl, sampass )) {
149 TALLOC_FREE(sampass);
150 retval = PAM_SUCCESS;
154 /* get this user's authentication token */
156 retval = _smb_read_password(pamh, ctrl, NULL, "Password: ", NULL, _SMB_AUTHTOK, &p);
157 if (retval != PAM_SUCCESS ) {
158 _log_err(LOG_CRIT, "auth: no password provided for [%s]", name);
159 TALLOC_FREE(sampass);
163 /* verify the password of this user */
165 retval = _smb_verify_password( pamh, sampass, p, ctrl );
166 TALLOC_FREE(sampass);
172 * This function is for setting samba credentials. If anyone comes up
173 * with any credentials they think should be set, let me know.
176 int pam_sm_setcred(pam_handle_t *pamh, int flags,
177 int argc, const char **argv)
179 int retval, *pretval = NULL;
181 retval = PAM_SUCCESS;
183 pam_get_data(pamh, "smb_setcred_return", (const void **) &pretval);
188 pam_set_data(pamh, "smb_setcred_return", NULL, NULL);
193 /* Helper function for adding a user to the db. */
194 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
195 const char *name, struct samu *sampass, bool exist)
197 char *err_str = NULL;
198 char *msg_str = NULL;
199 const char *pass = NULL;
202 /* Get the authtok; if we don't have one, silently fail. */
203 retval = pam_get_item( pamh, PAM_AUTHTOK, (const void **) &pass );
205 if (retval != PAM_SUCCESS) {
207 , "pam_get_item returned error to pam_sm_authenticate" );
208 return PAM_AUTHTOK_RECOVER_ERR;
209 } else if (pass == NULL) {
210 return PAM_AUTHTOK_RECOVER_ERR;
213 /* Add the user to the db if they aren't already there. */
215 retval = NT_STATUS_IS_OK(local_password_change(name, LOCAL_ADD_USER|LOCAL_SET_PASSWORD,
216 pass, &err_str, &msg_str));
217 if (!retval && err_str) {
218 make_remark(pamh, ctrl, PAM_ERROR_MSG, err_str );
219 } else if (msg_str) {
220 make_remark(pamh, ctrl, PAM_TEXT_INFO, msg_str );
228 /* mimick 'update encrypted' as long as the 'no pw req' flag is not set */
229 if ( pdb_get_acct_ctrl(sampass) & ~ACB_PWNOTREQ ) {
230 retval = NT_STATUS_IS_OK(local_password_change(name, LOCAL_SET_PASSWORD,
231 pass, &err_str, &msg_str));
232 if (!retval && err_str) {
233 make_remark(pamh, ctrl, PAM_ERROR_MSG, err_str );
234 } else if (msg_str) {
235 make_remark(pamh, ctrl, PAM_TEXT_INFO, msg_str );
246 /* static module data */
248 struct pam_module _pam_smbpass_auth_modstruct = {