2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-2001
6 Copyright (C) John H Terpsta 1999-2001
7 Copyright (C) Andrew Barton 2001
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * This module provides PAM based functions for validation of
26 * username/password pairs, account managment, session and access control.
27 * Note: SMB password checking is done in smbpass.c
32 extern int DEBUGLEVEL;
36 /*******************************************************************
37 * Handle PAM authentication
38 * - Access, Authentication, Session, Password
39 * Note: See PAM Documentation and refer to local system PAM implementation
40 * which determines what actions/limitations/allowances become affected.
41 *********************************************************************/
43 #include <security/pam_appl.h>
46 * Static variables used to communicate between the conversation function
47 * and the server_login function
50 static char *PAM_username;
51 static char *PAM_password;
54 * Macros to help make life easy
56 #define COPY_STRING(s) (s) ? strdup(s) : NULL
59 * Macro converted to a function to simplyify this thing
61 static BOOL pam_error_handler(pam_handle_t *pamh, int pam_error, char *msg, int dbglvl)
66 if( pam_error != PAM_SUCCESS)
68 DEBUG(dbglvl, ("PAM %s: %s\n", pam_strerror(pamh, pam_error)));
75 * PAM conversation function
76 * Here we assume (for now, at least) that echo on means login name, and
77 * echo off means password.
80 static int PAM_conv(int num_msg,
81 const struct pam_message **msg,
82 struct pam_response **resp,
86 struct pam_response *reply = NULL;
88 reply = malloc(sizeof(struct pam_response) * num_msg);
92 for (replies = 0; replies < num_msg; replies++)
94 switch (msg[replies]->msg_style)
96 case PAM_PROMPT_ECHO_ON:
97 reply[replies].resp_retcode = PAM_SUCCESS;
99 COPY_STRING(PAM_username);
103 case PAM_PROMPT_ECHO_OFF:
104 reply[replies].resp_retcode = PAM_SUCCESS;
105 reply[replies].resp =
106 COPY_STRING(PAM_password);
115 reply[replies].resp_retcode = PAM_SUCCESS;
116 reply[replies].resp = NULL;
120 /* Must be an error of some sort... */
130 static struct pam_conv PAM_conversation = {
135 static BOOL proc_pam_end(pam_handle_t *pamh)
141 pam_error = pam_end(pamh, 0);
142 if(pam_error_handler(pamh, pam_error, "End Cleanup Failed", 2) == True) {
146 DEBUG(2,("PAM not initialised"));
151 static BOOL pam_auth(char *user, char *password)
157 * Now use PAM to do authentication. Bail out if there are any
161 PAM_password = password;
163 DEBUG(4,("PAM Start for User: %s\n", user));
164 pam_error = pam_start("samba", user, &PAM_conversation, &pamh);
165 if(!pam_error_handler(pamh, pam_error, "start failure", 2)) {
171 * To enable debugging set in /etc/pam.d/samba:
172 * auth required /lib/security/pam_pwdb.so nullok shadow audit
175 pam_error = pam_authenticate(pamh, PAM_SILENT); /* Can we authenticate user? */
178 DEBUG(2, ("PAM: Athentication Error\n"));
180 case PAM_CRED_INSUFFICIENT:
181 DEBUG(2, ("PAM: Insufficient Credentials\n"));
183 case PAM_AUTHINFO_UNAVAIL:
184 DEBUG(2, ("PAM: Authentication Information Unavailable\n"));
186 case PAM_USER_UNKNOWN:
187 DEBUG(2, ("PAM: Username NOT known to Authentication system\n"));
190 DEBUG(2, ("PAM: One or more authentication modules reports user limit exceeeded\n"));
193 DEBUG(0, ("PAM: One or more PAM modules failed to load\n"));
196 DEBUG(4, ("PAM: User %s Authenticated OK\n", user));
198 if(!pam_error_handler(pamh, pam_error, "Authentication Failure", 2)) {
204 * Now do account management control and validation
206 pam_error = pam_acct_mgmt(pamh, PAM_SILENT); /* Is user account enabled? */
207 switch( pam_error ) {
208 case PAM_AUTHTOK_EXPIRED:
209 DEBUG(2, ("PAM: User is valid but password is expired\n"));
211 case PAM_ACCT_EXPIRED:
212 DEBUG(2, ("PAM: User no longer permitted to access system\n"));
215 DEBUG(2, ("PAM: There was an authentication error\n"));
217 case PAM_PERM_DENIED:
218 DEBUG(0, ("PAM: User is NOT permitted to access system at this time\n"));
220 case PAM_USER_UNKNOWN:
221 DEBUG(2, ("PAM: User \"%s\" is NOT known to account management\n", user));
224 DEBUG(4, ("PAM: Account OK for User: %s\n", user));
226 if(!pam_error_handler(pamh, pam_error, "Account Check Failed", 2)) {
232 * This will allow samba to aquire a kerberos token. And, when
233 * exporting an AFS cell, be able to /write/ to this cell.
236 pam_error = pam_setcred(pamh, (PAM_ESTABLISH_CRED|PAM_SILENT));
237 if(!pam_error_handler(pamh, pam_error, "Set Credential Failure", 2)) {
242 if( !proc_pam_end(pamh))
245 /* If this point is reached, the user has been authenticated. */
246 DEBUG(4, ("PAM: pam_authentication passed for User: %s\n", user));
251 /* Start PAM authentication for specified account */
252 static BOOL proc_pam_start(pam_handle_t **pamh, char *user)
257 DEBUG(4,("PAM Init for user: %s\n", user));
259 pam_error = pam_start("samba", user, &PAM_conversation, pamh);
260 if( !pam_error_handler(*pamh, pam_error, "Init Failed", 0)) {
265 rhost = client_name();
266 if (strcmp(rhost,"UNKNOWN") == 0)
267 rhost = client_addr();
270 DEBUG(4,("PAM setting rhost to: %s\n", rhost));
271 pam_error = pam_set_item(*pamh, PAM_RHOST, rhost);
272 if(!pam_error_handler(*pamh, pam_error, "set rhost failed", 0)) {
278 #if defined(PAM_TTY_KLUDGE) && defined(PAM_TTY)
279 pam_error = pam_set_item(*pamh, PAM_TTY, "samba");
280 if (!pam_error_handler(*pamh, pam_error, "set tty failed", 0)) {
289 static BOOL pam_session(pam_handle_t *pamh, char *user, char *tty, BOOL instance)
297 DEBUG(4,("PAM tty set to: %s\"\n", tty));
298 pam_error = pam_set_item(pamh, PAM_TTY, tty);
299 if (!pam_error_handler(pamh, pam_error, "set tty failed", 0)) {
306 pam_error = pam_open_session(pamh, PAM_SILENT);
307 if (!pam_error_handler(pamh, pam_error, "session setup failed", 0)) {
314 pam_error = pam_close_session(pamh, PAM_SILENT);
315 if (!pam_error_handler(pamh, pam_error, "session close failed", 0)) {
323 static BOOL pam_account(pam_handle_t *pamh, char *user)
330 DEBUG(4,("PAM starting account management for user: %s \n", user));
332 pam_error = pam_acct_mgmt(pamh, PAM_SILENT);
333 if (!pam_error_handler(pamh, pam_error, "PAM set account management failed", 0)) {
337 DEBUG(4,("PAM account management passed\n"));
341 * This will allow samba to aquire a kerberos token. And, when
342 * exporting an AFS cell, be able to /write/ to this cell.
344 pam_error = pam_setcred(pamh, (PAM_ESTABLISH_CRED));
345 if (!pam_error_handler(pamh, pam_error, "set credentials failed\n", 0)) {
350 /* If this point is reached, the user has been authenticated. */
353 static BOOL account_pam(char *user)
356 * Check the account with the PAM account module:
357 * - This means that accounts can be disabled
358 * and or expired with avoidance of samba then just
359 * bypassing the situation.
362 pam_handle_t *pamh = NULL;
365 PAMuser = malloc(strlen(user)+1);
366 /* This is freed by PAM */
367 strncpy(PAMuser, user, strlen(user)+1);
369 if (proc_pam_start(&pamh, PAMuser))
371 if (pam_account(pamh, PAMuser))
373 return proc_pam_end(pamh);
380 BOOL PAM_session(BOOL instance, const connection_struct *conn, char *tty)
382 pam_handle_t *pamh=NULL;
385 user = malloc(strlen(conn->user)+1);
387 /* This is freed by PAM */
388 strncpy(user, conn->user, strlen(conn->user)+1);
390 if (!proc_pam_start(&pamh, user))
396 if (pam_session(pamh, user, tty, instance))
398 return proc_pam_end(pamh);
407 BOOL pam_passcheck(char * user, char * password)
409 pam_handle_t *pamh = NULL;
412 PAM_password = password;
414 if( proc_pam_start(&pamh, user))
416 if( pam_auth(user, password))
418 if( account_pam(user))
420 return( proc_pam_end(pamh));
427 #endif /* NOTBLOCKEDOUT */
429 BOOL pam_passcheck( char * user, char * password )
431 return( pam_auth( user, password ));
436 /* Do *NOT* make this function static. Doing so breaks the compile on gcc */
438 void pampass_dummy_function( void ) { } /*This stops compiler complaints */
440 #endif /* WITH_PAM */