s4:samba_kcc: Use 'dburl' passed from command line rather than lp.samdb_url()
[Samba.git] / source3 / pam_smbpass / pam_smb_auth.c
blobac5ef3f21c5156a0f028eb73f482e4668c72cb65
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 #define AUTH_RETURN \
45 do { \
46 /* Restore application signal handler */ \
47 CatchSignal(SIGPIPE, oldsig_handler); \
48 if(ret_data) { \
49 *ret_data = retval; \
50 pam_set_data( pamh, "smb_setcred_return" \
51 , (void *) ret_data, NULL ); \
52 } \
53 TALLOC_FREE(frame); \
54 return retval; \
55 } while (0)
57 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
58 const char *name, struct samu *sampass, bool exist);
62 * pam_sm_authenticate() authenticates users against the samba password file.
64 * First, obtain the password from the user. Then use a
65 * routine in 'support.c' to authenticate the user.
68 #define _SMB_AUTHTOK "-SMB-PASS"
70 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
71 int argc, const char **argv)
73 unsigned int ctrl;
74 int retval, *ret_data = NULL;
75 struct samu *sampass = NULL;
76 const char *name;
77 void (*oldsig_handler)(int) = NULL;
78 bool found;
79 TALLOC_CTX *frame = talloc_stackframe();
81 /* Points to memory managed by the PAM library. Do not free. */
82 char *p = NULL;
84 /* Samba initialization. */
85 load_case_tables_library();
87 ctrl = set_ctrl(pamh, flags, argc, argv);
89 /* Get a few bytes so we can pass our return value to
90 pam_sm_setcred(). */
91 ret_data = SMB_MALLOC_P(int);
93 /* we need to do this before we call AUTH_RETURN */
94 /* Getting into places that might use LDAP -- protect the app
95 from a SIGPIPE it's not expecting */
96 oldsig_handler = CatchSignal(SIGPIPE, SIG_IGN);
98 /* get the username */
99 retval = pam_get_user( pamh, &name, "Username: " );
100 if ( retval != PAM_SUCCESS ) {
101 if (on( SMB_DEBUG, ctrl )) {
102 _log_err(pamh, LOG_DEBUG, "auth: could not identify user");
104 AUTH_RETURN;
106 if (on( SMB_DEBUG, ctrl )) {
107 _log_err(pamh, LOG_DEBUG, "username [%s] obtained", name );
110 if (geteuid() != 0) {
111 _log_err(pamh, LOG_DEBUG, "Cannot access samba password database, not running as root.");
112 retval = PAM_AUTHINFO_UNAVAIL;
113 AUTH_RETURN;
116 if (!initialize_password_db(True, NULL)) {
117 _log_err(pamh, LOG_ALERT, "Cannot access samba password database" );
118 retval = PAM_AUTHINFO_UNAVAIL;
119 AUTH_RETURN;
122 sampass = samu_new( NULL );
123 if (!sampass) {
124 _log_err(pamh, LOG_ALERT, "Cannot talloc a samu struct" );
125 retval = nt_status_to_pam(NT_STATUS_NO_MEMORY);
126 AUTH_RETURN;
129 found = pdb_getsampwnam( sampass, name );
131 if (on( SMB_MIGRATE, ctrl )) {
132 retval = _smb_add_user(pamh, ctrl, name, sampass, found);
133 TALLOC_FREE(sampass);
134 AUTH_RETURN;
137 if (!found) {
138 _log_err(pamh, LOG_ALERT, "Failed to find entry for user %s.", name);
139 retval = PAM_USER_UNKNOWN;
140 TALLOC_FREE(sampass);
141 sampass = NULL;
142 AUTH_RETURN;
145 /* if this user does not have a password... */
147 if (_smb_blankpasswd( ctrl, sampass )) {
148 TALLOC_FREE(sampass);
149 retval = PAM_SUCCESS;
150 AUTH_RETURN;
153 /* get this user's authentication token */
155 retval = _smb_read_password(pamh, ctrl, NULL, "Password: ", NULL, _SMB_AUTHTOK, &p);
156 if (retval != PAM_SUCCESS ) {
157 _log_err(pamh,LOG_CRIT, "auth: no password provided for [%s]", name);
158 TALLOC_FREE(sampass);
159 AUTH_RETURN;
162 /* verify the password of this user */
164 retval = _smb_verify_password( pamh, sampass, p, ctrl );
165 TALLOC_FREE(sampass);
166 p = NULL;
167 AUTH_RETURN;
171 * This function is for setting samba credentials. If anyone comes up
172 * with any credentials they think should be set, let me know.
175 int pam_sm_setcred(pam_handle_t *pamh, int flags,
176 int argc, const char **argv)
178 int retval, *pretval = NULL;
180 retval = PAM_SUCCESS;
182 _pam_get_data(pamh, "smb_setcred_return", &pretval);
183 if(pretval) {
184 retval = *pretval;
185 SAFE_FREE(pretval);
187 pam_set_data(pamh, "smb_setcred_return", NULL, NULL);
189 return retval;
192 /* Helper function for adding a user to the db. */
193 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
194 const char *name, struct samu *sampass, bool exist)
196 char *err_str = NULL;
197 char *msg_str = NULL;
198 const char *pass = NULL;
199 int retval;
200 TALLOC_CTX *frame = talloc_stackframe();
202 /* Get the authtok; if we don't have one, silently fail. */
203 retval = _pam_get_item( pamh, PAM_AUTHTOK, &pass );
205 if (retval != PAM_SUCCESS) {
206 _log_err(pamh, LOG_ALERT
207 , "pam_get_item returned error to pam_sm_authenticate" );
208 TALLOC_FREE(frame);
209 return PAM_AUTHTOK_RECOVER_ERR;
210 } else if (pass == NULL) {
211 TALLOC_FREE(frame);
212 return PAM_AUTHTOK_RECOVER_ERR;
215 /* Add the user to the db if they aren't already there. */
216 if (!exist) {
217 retval = NT_STATUS_IS_OK(local_password_change(name, LOCAL_ADD_USER|LOCAL_SET_PASSWORD,
218 pass, &err_str, &msg_str));
219 if (!retval && err_str) {
220 make_remark(pamh, ctrl, PAM_ERROR_MSG, err_str );
221 } else if (msg_str) {
222 make_remark(pamh, ctrl, PAM_TEXT_INFO, msg_str );
224 pass = NULL;
226 SAFE_FREE(err_str);
227 SAFE_FREE(msg_str);
228 TALLOC_FREE(frame);
229 return PAM_IGNORE;
230 } else {
231 /* mimick 'update encrypted' as long as the 'no pw req' flag is not set */
232 if ( pdb_get_acct_ctrl(sampass) & ~ACB_PWNOTREQ ) {
233 retval = NT_STATUS_IS_OK(local_password_change(name, LOCAL_SET_PASSWORD,
234 pass, &err_str, &msg_str));
235 if (!retval && err_str) {
236 make_remark(pamh, ctrl, PAM_ERROR_MSG, err_str );
237 } else if (msg_str) {
238 make_remark(pamh, ctrl, PAM_TEXT_INFO, msg_str );
243 SAFE_FREE(err_str);
244 SAFE_FREE(msg_str);
245 pass = NULL;
246 TALLOC_FREE(frame);
247 return PAM_IGNORE;
250 /* static module data */
251 #ifdef PAM_STATIC
252 struct pam_module _pam_smbpass_auth_modstruct = {
253 "pam_smbpass",
254 pam_sm_authenticate,
255 pam_sm_setcred,
256 NULL,
257 NULL,
258 NULL,
259 NULL
261 #endif