Make sure we don't clobber the stack when response consists of the empty
[Samba/gebeck_regimport.git] / source3 / pam_smbpass / pam_smb_auth.c
blobf4cbb59af31e4fe6c2632fbf08c2b13d84e93bdc
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, SAM_ACCOUNT *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 SAM_ACCOUNT *sampass = NULL;
68 extern BOOL in_client;
69 const char *name;
70 void (*oldsig_handler)(int);
71 BOOL found;
73 /* Points to memory managed by the PAM library. Do not free. */
74 char *p = NULL;
77 /* Samba initialization. */
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 = malloc(sizeof(int));
87 /* get the username */
88 retval = pam_get_user( pamh, &name, "Username: " );
89 if ( retval != PAM_SUCCESS ) {
90 if (on( SMB_DEBUG, ctrl )) {
91 _log_err(LOG_DEBUG, "auth: could not identify user");
93 AUTH_RETURN;
95 if (on( SMB_DEBUG, ctrl )) {
96 _log_err( LOG_DEBUG, "username [%s] obtained", name );
99 /* Getting into places that might use LDAP -- protect the app
100 from a SIGPIPE it's not expecting */
101 oldsig_handler = CatchSignal(SIGPIPE, SIGNAL_CAST SIG_IGN);
103 if (!initialize_password_db(True)) {
104 _log_err( LOG_ALERT, "Cannot access samba password database" );
105 retval = PAM_AUTHINFO_UNAVAIL;
106 AUTH_RETURN;
109 pdb_init_sam(&sampass);
111 found = pdb_getsampwnam( sampass, name );
113 if (on( SMB_MIGRATE, ctrl )) {
114 retval = _smb_add_user(pamh, ctrl, name, sampass, found);
115 pdb_free_sam(&sampass);
116 AUTH_RETURN;
119 if (!found) {
120 _log_err(LOG_ALERT, "Failed to find entry for user %s.", name);
121 retval = PAM_USER_UNKNOWN;
122 pdb_free_sam(&sampass);
123 sampass = NULL;
124 AUTH_RETURN;
127 /* if this user does not have a password... */
129 if (_smb_blankpasswd( ctrl, sampass )) {
130 pdb_free_sam(&sampass);
131 retval = PAM_SUCCESS;
132 AUTH_RETURN;
135 /* get this user's authentication token */
137 retval = _smb_read_password(pamh, ctrl, NULL, "Password: ", NULL, _SMB_AUTHTOK, &p);
138 if (retval != PAM_SUCCESS ) {
139 _log_err(LOG_CRIT, "auth: no password provided for [%s]"
140 , name);
141 pdb_free_sam(&sampass);
142 AUTH_RETURN;
145 /* verify the password of this user */
147 retval = _smb_verify_password( pamh, sampass, p, ctrl );
148 pdb_free_sam(&sampass);
149 p = NULL;
150 AUTH_RETURN;
154 * This function is for setting samba credentials. If anyone comes up
155 * with any credentials they think should be set, let me know.
158 int pam_sm_setcred(pam_handle_t *pamh, int flags,
159 int argc, const char **argv)
161 int retval, *pretval = NULL;
163 retval = PAM_SUCCESS;
165 pam_get_data(pamh, "smb_setcred_return", (const void **) &pretval);
166 if(pretval) {
167 retval = *pretval;
168 SAFE_FREE(pretval);
170 pam_set_data(pamh, "smb_setcred_return", NULL, NULL);
172 return retval;
176 /* Helper function for adding a user to the db. */
177 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
178 const char *name, SAM_ACCOUNT *sampass, BOOL exist)
180 pstring err_str;
181 pstring msg_str;
182 const char *pass = NULL;
183 int retval;
185 err_str[0] = '\0';
186 msg_str[0] = '\0';
188 /* Get the authtok; if we don't have one, silently fail. */
189 retval = pam_get_item( pamh, PAM_AUTHTOK, (const void **) &pass );
191 if (retval != PAM_SUCCESS) {
192 _log_err( LOG_ALERT
193 , "pam_get_item returned error to pam_sm_authenticate" );
194 return PAM_AUTHTOK_RECOVER_ERR;
195 } else if (pass == NULL) {
196 return PAM_AUTHTOK_RECOVER_ERR;
199 /* Add the user to the db if they aren't already there. */
200 if (!exist) {
201 retval = local_password_change( name, LOCAL_ADD_USER,
202 pass, err_str,
203 sizeof(err_str),
204 msg_str, sizeof(msg_str) );
205 if (!retval && *err_str)
207 err_str[PSTRING_LEN-1] = '\0';
208 make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
210 else if (*msg_str)
212 msg_str[PSTRING_LEN-1] = '\0';
213 make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
215 pass = NULL;
217 return PAM_IGNORE;
219 else {
220 /* Change the user's password IFF it's null. */
221 if ((pdb_get_lanman_passwd(sampass) == NULL) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ))
223 retval = local_password_change( name, 0, pass, err_str, sizeof(err_str),
224 msg_str, sizeof(msg_str) );
225 if (!retval && *err_str)
227 err_str[PSTRING_LEN-1] = '\0';
228 make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
230 else if (*msg_str)
232 msg_str[PSTRING_LEN-1] = '\0';
233 make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
238 pass = NULL;
240 return PAM_IGNORE;
244 /* static module data */
245 #ifdef PAM_STATIC
246 struct pam_module _pam_smbpass_auth_modstruct = {
247 "pam_smbpass",
248 pam_sm_authenticate,
249 pam_sm_setcred,
250 NULL,
251 NULL,
252 NULL,
253 NULL
255 #endif