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
);
79 ctrl
= set_ctrl(flags
, argc
, argv
);
81 /* Get a few bytes so we can pass our return value to
83 ret_data
= malloc(sizeof(int));
85 /* get the username */
86 retval
= pam_get_user( pamh
, &name
, "Username: " );
87 if ( retval
!= PAM_SUCCESS
) {
88 if (on( SMB_DEBUG
, ctrl
)) {
89 _log_err(LOG_DEBUG
, "auth: could not identify user");
93 if (on( SMB_DEBUG
, ctrl
)) {
94 _log_err( LOG_DEBUG
, "username [%s] obtained", name
);
97 if (!initialize_password_db()) {
98 _log_err( LOG_ALERT
, "Cannot access samba password database" );
99 retval
= PAM_AUTHINFO_UNAVAIL
;
103 smb_pwent
= getsmbpwnam( name
);
105 if (on( SMB_MIGRATE
, ctrl
)) {
106 retval
= _smb_add_user(pamh
, ctrl
, name
, smb_pwent
);
110 if (smb_pwent
== NULL
) {
111 _log_err(LOG_ALERT
, "Failed to find entry for user %s.", name
);
112 retval
= PAM_USER_UNKNOWN
;
116 /* if this user does not have a password... */
118 if (_smb_blankpasswd( ctrl
, smb_pwent
)) {
120 retval
= PAM_SUCCESS
;
124 /* get this user's authentication token */
126 retval
= _smb_read_password(pamh
, ctrl
, NULL
, "Password: ", NULL
128 if (retval
!= PAM_SUCCESS
) {
129 _log_err(LOG_CRIT
, "auth: no password provided for [%s]"
135 /* verify the password of this user */
137 retval
= _smb_verify_password( pamh
, smb_pwent
, p
, ctrl
);
144 * This function is for setting samba credentials. If anyone comes up
145 * with any credentials they think should be set, let me know.
148 int pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
149 int argc
, const char **argv
)
151 int retval
, *pretval
= NULL
;
153 retval
= PAM_SUCCESS
;
155 pam_get_data(pamh
, "smb_setcred_return", (const void **) &pretval
);
160 pam_set_data(pamh
, "smb_setcred_return", NULL
, NULL
);
166 /* Helper function for adding a user to the db. */
167 static int _smb_add_user(pam_handle_t
*pamh
, unsigned int ctrl
,
168 const char *name
, struct smb_passwd
*smb_pwent
)
172 const char *pass
= NULL
;
178 /* Get the authtok; if we don't have one, silently fail. */
179 retval
= pam_get_item( pamh
, PAM_AUTHTOK
, (const void **) &pass
);
181 if (retval
!= PAM_SUCCESS
) {
183 , "pam_get_item returned error to pam_sm_authenticate" );
184 return PAM_AUTHTOK_RECOVER_ERR
;
185 } else if (pass
== NULL
) {
186 return PAM_AUTHTOK_RECOVER_ERR
;
189 /* Add the user to the db if they aren't already there. */
190 if (smb_pwent
== NULL
) {
191 retval
= local_password_change( name
, LOCAL_ADD_USER
,
194 msg_str
, sizeof(msg_str
) );
195 if (!retval
&& *err_str
)
197 err_str
[PSTRING_LEN
-1] = '\0';
198 make_remark( pamh
, ctrl
, PAM_ERROR_MSG
, err_str
);
202 msg_str
[PSTRING_LEN
-1] = '\0';
203 make_remark( pamh
, ctrl
, PAM_TEXT_INFO
, msg_str
);
210 /* Change the user's password IFF it's null. */
211 if (smb_pwent
->smb_passwd
== NULL
&& (smb_pwent
->acct_ctrl
& ACB_PWNOTREQ
))
213 retval
= local_password_change( name
, 0,
216 msg_str
, sizeof(msg_str
) );
217 if (!retval
&& *err_str
)
219 err_str
[PSTRING_LEN
-1] = '\0';
220 make_remark( pamh
, ctrl
, PAM_ERROR_MSG
, err_str
);
224 msg_str
[PSTRING_LEN
-1] = '\0';
225 make_remark( pamh
, ctrl
, PAM_TEXT_INFO
, msg_str
);
234 /* static module data */
236 struct pam_module _pam_smbpass_auth_modstruct
= {