nfs4acls: Introduce a helper variable
[Samba.git] / source3 / pam_smbpass / pam_smb_auth.c
blob06ab8455d5daa73aeceec5758b456ebd2f639f1f
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)
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, see <http://www.gnu.org/licenses/>.
17 /* indicate the following groups are defined */
18 #define PAM_SM_AUTH
20 #include "includes.h"
21 #include "lib/util/debug.h"
23 #ifndef LINUX
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>
30 #endif
32 #endif /* LINUX */
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>
38 #endif
40 #include "general.h"
42 #include "support.h"
44 static void ret_data_cleanup(pam_handle_t *pamh, void *data, int error_status)
46 free(data);
49 #define AUTH_RETURN \
50 do { \
51 /* Restore application signal handler */ \
52 CatchSignal(SIGPIPE, oldsig_handler); \
53 if(ret_data) { \
54 *ret_data = retval; \
55 pam_set_data(pamh, \
56 "smb_setcred_return", \
57 (void *)ret_data, \
58 ret_data_cleanup); \
59 } \
60 TALLOC_FREE(frame); \
61 return retval; \
62 } while (0)
64 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
65 const char *name, struct samu *sampass, bool exist);
69 * pam_sm_authenticate() authenticates users against the samba password file.
71 * First, obtain the password from the user. Then use a
72 * routine in 'support.c' to authenticate the user.
75 #define _SMB_AUTHTOK "-SMB-PASS"
77 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
78 int argc, const char **argv)
80 unsigned int ctrl;
81 int retval, *ret_data = NULL;
82 struct samu *sampass = NULL;
83 const char *name;
84 void (*oldsig_handler)(int) = NULL;
85 bool found;
86 TALLOC_CTX *frame = talloc_stackframe();
88 /* Points to memory managed by the PAM library. Do not free. */
89 char *p = NULL;
91 /* Samba initialization. */
93 ctrl = set_ctrl(pamh, flags, argc, argv);
95 /* Get a few bytes so we can pass our return value to
96 pam_sm_setcred(). */
97 ret_data = SMB_MALLOC_P(int);
99 /* we need to do this before we call AUTH_RETURN */
100 /* Getting into places that might use LDAP -- protect the app
101 from a SIGPIPE it's not expecting */
102 oldsig_handler = CatchSignal(SIGPIPE, SIG_IGN);
104 /* get the username */
105 retval = pam_get_user( pamh, &name, "Username: " );
106 if ( retval != PAM_SUCCESS ) {
107 if (on( SMB_DEBUG, ctrl )) {
108 _log_err(pamh, LOG_DEBUG, "auth: could not identify user");
110 AUTH_RETURN;
112 if (on( SMB_DEBUG, ctrl )) {
113 _log_err(pamh, LOG_DEBUG, "username [%s] obtained", name );
116 if (geteuid() != 0) {
117 _log_err(pamh, LOG_DEBUG, "Cannot access samba password database, not running as root.");
118 retval = PAM_AUTHINFO_UNAVAIL;
119 AUTH_RETURN;
122 if (!initialize_password_db(True, NULL)) {
123 _log_err(pamh, LOG_ALERT, "Cannot access samba password database" );
124 retval = PAM_AUTHINFO_UNAVAIL;
125 AUTH_RETURN;
128 sampass = samu_new( NULL );
129 if (!sampass) {
130 _log_err(pamh, LOG_ALERT, "Cannot talloc a samu struct" );
131 retval = nt_status_to_pam(NT_STATUS_NO_MEMORY);
132 AUTH_RETURN;
135 found = pdb_getsampwnam( sampass, name );
137 if (on( SMB_MIGRATE, ctrl )) {
138 retval = _smb_add_user(pamh, ctrl, name, sampass, found);
139 TALLOC_FREE(sampass);
140 AUTH_RETURN;
143 if (!found) {
144 _log_err(pamh, LOG_ALERT, "Failed to find entry for user %s.", name);
145 retval = PAM_USER_UNKNOWN;
146 TALLOC_FREE(sampass);
147 sampass = NULL;
148 AUTH_RETURN;
151 /* if this user does not have a password... */
153 if (_smb_blankpasswd( ctrl, sampass )) {
154 TALLOC_FREE(sampass);
155 retval = PAM_SUCCESS;
156 AUTH_RETURN;
159 /* get this user's authentication token */
161 retval = _smb_read_password(pamh, ctrl, NULL, "Password: ", NULL, _SMB_AUTHTOK, &p);
162 if (retval != PAM_SUCCESS ) {
163 _log_err(pamh,LOG_CRIT, "auth: no password provided for [%s]", name);
164 TALLOC_FREE(sampass);
165 AUTH_RETURN;
168 /* verify the password of this user */
170 retval = _smb_verify_password( pamh, sampass, p, ctrl );
171 TALLOC_FREE(sampass);
172 p = NULL;
173 AUTH_RETURN;
177 * This function is for setting samba credentials. If anyone comes up
178 * with any credentials they think should be set, let me know.
181 int pam_sm_setcred(pam_handle_t *pamh, int flags,
182 int argc, const char **argv)
184 int retval, *pretval = NULL;
186 retval = PAM_SUCCESS;
188 _pam_get_data(pamh, "smb_setcred_return", &pretval);
189 if(pretval) {
190 retval = *pretval;
191 SAFE_FREE(pretval);
193 pam_set_data(pamh, "smb_setcred_return", NULL, NULL);
195 return retval;
198 /* Helper function for adding a user to the db. */
199 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
200 const char *name, struct samu *sampass, bool exist)
202 char *err_str = NULL;
203 char *msg_str = NULL;
204 const char *pass = NULL;
205 int retval;
206 TALLOC_CTX *frame = talloc_stackframe();
208 /* Get the authtok; if we don't have one, silently fail. */
209 retval = _pam_get_item( pamh, PAM_AUTHTOK, &pass );
211 if (retval != PAM_SUCCESS) {
212 _log_err(pamh, LOG_ALERT
213 , "pam_get_item returned error to pam_sm_authenticate" );
214 TALLOC_FREE(frame);
215 return PAM_AUTHTOK_RECOVER_ERR;
218 /* Add the user to the db if they aren't already there. */
219 if (!exist) {
220 retval = NT_STATUS_IS_OK(local_password_change(name, LOCAL_ADD_USER|LOCAL_SET_PASSWORD,
221 pass, &err_str, &msg_str));
222 if (!retval && err_str) {
223 make_remark(pamh, ctrl, PAM_ERROR_MSG, err_str );
224 } else if (msg_str) {
225 make_remark(pamh, ctrl, PAM_TEXT_INFO, msg_str );
227 pass = NULL;
229 SAFE_FREE(err_str);
230 SAFE_FREE(msg_str);
231 TALLOC_FREE(frame);
232 return PAM_IGNORE;
233 } else {
234 /* mimick 'update encrypted' as long as the 'no pw req' flag is not set */
235 if ( pdb_get_acct_ctrl(sampass) & ~ACB_PWNOTREQ ) {
236 retval = NT_STATUS_IS_OK(local_password_change(name, LOCAL_SET_PASSWORD,
237 pass, &err_str, &msg_str));
238 if (!retval && err_str) {
239 make_remark(pamh, ctrl, PAM_ERROR_MSG, err_str );
240 } else if (msg_str) {
241 make_remark(pamh, ctrl, PAM_TEXT_INFO, msg_str );
246 SAFE_FREE(err_str);
247 SAFE_FREE(msg_str);
248 pass = NULL;
249 TALLOC_FREE(frame);
250 return PAM_IGNORE;
253 /* static module data */
254 #ifdef PAM_STATIC
255 struct pam_module _pam_smbpass_auth_modstruct = {
256 "pam_smbpass",
257 pam_sm_authenticate,
258 pam_sm_setcred,
259 NULL,
260 NULL,
261 NULL,
262 NULL
264 #endif