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)
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, write to the Free Software Foundation, Inc., 675
15 * Mass Ave, Cambridge, MA 02139, USA.
18 /* indicate the following groups are defined */
26 /* This is only used in the Sun implementation. */
27 #include <security/pam_appl.h>
31 #include <security/pam_modules.h>
41 pam_set_data( pamh, "smb_setcred_return" \
42 , (void *) ret_data, NULL ); \
47 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
48 const char *name, struct smb_passwd *smb_pwent);
51 * pam_sm_authenticate() authenticates users against the samba password file.
53 * First, obtain the password from the user. Then use a
54 * routine in 'support.c' to authenticate the user.
57 #define _SMB_AUTHTOK "-SMB-PASS"
59 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
60 int argc, const char **argv)
63 int retval, *ret_data = NULL;
67 /* Points to memory managed by the PAM library. Do not free. */
70 struct smb_passwd *smb_pwent = NULL;
72 extern BOOL in_client;
74 /* Samba initialization. */
75 setup_logging("pam_smbpass",False);
78 ctrl = set_ctrl(flags, argc, argv);
80 /* Get a few bytes so we can pass our return value to
81 pam_sm_setcred(). Used in AUTH_RETURN macro */
82 ret_data = malloc(sizeof(int));
84 /* get the username */
85 retval = pam_get_user( pamh, &name, "Username: " );
86 if ( retval != PAM_SUCCESS ) {
87 if (on( SMB_DEBUG, ctrl )) {
88 _log_err(LOG_DEBUG, "auth: could not identify user");
92 if (on( SMB_DEBUG, ctrl )) {
93 _log_err( LOG_DEBUG, "username [%s] obtained", name );
96 if (!initialize_password_db()) {
97 _log_err( LOG_ALERT, "Cannot access samba password database" );
98 retval = PAM_AUTHINFO_UNAVAIL;
102 smb_pwent = getsmbpwnam( name );
104 if (on( SMB_MIGRATE, ctrl )) {
105 retval = _smb_add_user(pamh, ctrl, name, smb_pwent);
109 if (smb_pwent == NULL) {
110 _log_err(LOG_ALERT, "Failed to find entry for user %s.", name);
111 retval = PAM_USER_UNKNOWN;
115 /* if this user does not have a password... */
117 if (_smb_blankpasswd( ctrl, smb_pwent )) {
119 retval = PAM_SUCCESS;
123 /* get this user's authentication token */
125 retval = _smb_read_password(pamh, ctrl, NULL, "Password: ", NULL
127 if (retval != PAM_SUCCESS ) {
128 _log_err(LOG_CRIT, "auth: no password provided for [%s]"
134 /* verify the password of this user */
136 retval = _smb_verify_password( pamh, smb_pwent, p, ctrl );
143 * This function is for setting samba credentials. If anyone comes up
144 * with any credentials they think should be set, let me know.
147 int pam_sm_setcred(pam_handle_t *pamh, int flags,
148 int argc, const char **argv)
150 int retval, *pretval = NULL;
152 retval = PAM_SUCCESS;
154 pam_get_data(pamh, "smb_setcred_return", (const void **) &pretval);
159 pam_set_data(pamh, "smb_setcred_return", NULL, NULL);
165 /* Helper function for adding a user to the db. */
166 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
167 const char *name, struct smb_passwd *smb_pwent)
171 const char *pass = NULL;
177 /* Get the authtok; if we don't have one, silently fail. */
178 retval = pam_get_item( pamh, PAM_AUTHTOK, (const void **) &pass );
180 if (retval != PAM_SUCCESS) {
182 , "pam_get_item returned error to pam_sm_authenticate" );
183 return PAM_AUTHTOK_RECOVER_ERR;
184 } else if (pass == NULL) {
185 return PAM_AUTHTOK_RECOVER_ERR;
188 /* Add the user to the db if they aren't already there. */
189 if (smb_pwent == NULL) {
190 retval = local_password_change( name, LOCAL_ADD_USER|LOCAL_SET_PASSWORD,
193 msg_str, sizeof(msg_str) );
194 if (!retval && *err_str)
196 err_str[PSTRING_LEN-1] = '\0';
197 make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
201 msg_str[PSTRING_LEN-1] = '\0';
202 make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
209 /* Change the user's password IFF it's null. */
210 if (smb_pwent->smb_passwd == NULL && (smb_pwent->acct_ctrl & ACB_PWNOTREQ))
212 retval = local_password_change( name, LOCAL_SET_PASSWORD,
215 msg_str, sizeof(msg_str) );
216 if (!retval && *err_str)
218 err_str[PSTRING_LEN-1] = '\0';
219 make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
223 msg_str[PSTRING_LEN-1] = '\0';
224 make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
233 /* static module data */
235 struct pam_module _pam_smbpass_auth_modstruct = {