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
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", msg, 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 = {
136 * PAM Closing out cleanup handler
138 static BOOL proc_pam_end(pam_handle_t *pamh)
144 pam_error = pam_end(pamh, 0);
145 if(pam_error_handler(pamh, pam_error, "End Cleanup Failed", 2) == True) {
146 DEBUG(4, ("PAM: PAM_END OK.\n"));
150 DEBUG(2,("PAM: not initialised"));
155 * Start PAM authentication for specified account
157 static BOOL proc_pam_start(pam_handle_t **pamh, char *user)
162 DEBUG(4,("PAM: Init user: %s\n", user));
164 pam_error = pam_start("samba", user, &PAM_conversation, pamh);
165 if( !pam_error_handler(*pamh, pam_error, "Init Failed", 0)) {
170 rhost = client_name();
171 if (strcmp(rhost,"UNKNOWN") == 0)
172 rhost = client_addr();
175 DEBUG(4,("PAM: setting rhost to: %s\n", rhost));
176 pam_error = pam_set_item(*pamh, PAM_RHOST, rhost);
177 if(!pam_error_handler(*pamh, pam_error, "set rhost failed", 0)) {
183 DEBUG(4,("PAM: setting tty\n"));
184 pam_error = pam_set_item(*pamh, PAM_TTY, "samba");
185 if (!pam_error_handler(*pamh, pam_error, "set tty failed", 0)) {
190 DEBUG(4,("PAM: Init passed for user: %s\n", user));
195 * PAM Authentication Handler
197 static BOOL pam_auth(pam_handle_t *pamh, char *user, char *password)
202 * To enable debugging set in /etc/pam.d/samba:
203 * auth required /lib/security/pam_pwdb.so nullok shadow audit
206 DEBUG(4,("PAM: Authenticate User: %s\n", user));
207 pam_error = pam_authenticate(pamh, PAM_SILENT); /* Can we authenticate user? */
210 DEBUG(2, ("PAM: Athentication Error\n"));
212 case PAM_CRED_INSUFFICIENT:
213 DEBUG(2, ("PAM: Insufficient Credentials\n"));
215 case PAM_AUTHINFO_UNAVAIL:
216 DEBUG(2, ("PAM: Authentication Information Unavailable\n"));
218 case PAM_USER_UNKNOWN:
219 DEBUG(2, ("PAM: Username NOT known to Authentication system\n"));
222 DEBUG(2, ("PAM: One or more authentication modules reports user limit exceeeded\n"));
225 DEBUG(0, ("PAM: One or more PAM modules failed to load\n"));
228 DEBUG(4, ("PAM: User %s Authenticated OK\n", user));
230 if(!pam_error_handler(pamh, pam_error, "Authentication Failure", 2)) {
234 /* If this point is reached, the user has been authenticated. */
239 * PAM Account Handler
241 static BOOL pam_account(pam_handle_t *pamh, char * user, char * password)
245 DEBUG(4,("PAM: Account Management for User: %s\n", user));
246 pam_error = pam_acct_mgmt(pamh, PAM_SILENT); /* Is user account enabled? */
247 switch( pam_error ) {
248 case PAM_AUTHTOK_EXPIRED:
249 DEBUG(2, ("PAM: User is valid but password is expired\n"));
251 case PAM_ACCT_EXPIRED:
252 DEBUG(2, ("PAM: User no longer permitted to access system\n"));
255 DEBUG(2, ("PAM: There was an authentication error\n"));
257 case PAM_PERM_DENIED:
258 DEBUG(0, ("PAM: User is NOT permitted to access system at this time\n"));
260 case PAM_USER_UNKNOWN:
261 DEBUG(0, ("PAM: User \"%s\" is NOT known to account management\n", user));
264 DEBUG(4, ("PAM: Account OK for User: %s\n", user));
266 if(!pam_error_handler(pamh, pam_error, "Account Check Failed", 2)) {
272 * This will allow samba to aquire a kerberos token. And, when
273 * exporting an AFS cell, be able to /write/ to this cell.
276 DEBUG(4,("PAM: Account Management SetCredentials for User: %s\n", user));
277 pam_error = pam_setcred(pamh, (PAM_ESTABLISH_CRED|PAM_SILENT));
278 if(!pam_error_handler(pamh, pam_error, "Set Credential Failure", 2)) {
283 /* If this point is reached, the user has been authenticated. */
289 * PAM Internal Session Handler
291 static BOOL proc_pam_session(pam_handle_t *pamh, char *user, char *tty, BOOL flag)
299 DEBUG(4,("PAM: tty set to: %s\n", tty));
300 pam_error = pam_set_item(pamh, PAM_TTY, tty);
301 if (!pam_error_handler(pamh, pam_error, "set tty failed", 0)) {
308 pam_error = pam_open_session(pamh, PAM_SILENT);
309 if (!pam_error_handler(pamh, pam_error, "session setup failed", 0)) {
316 pam_error = pam_close_session(pamh, PAM_SILENT);
317 if (!pam_error_handler(pamh, pam_error, "session close failed", 0)) {
326 * PAM Externally accessible Session handler
328 BOOL pam_session(BOOL flag, const connection_struct *conn, char *tty)
330 pam_handle_t *pamh = NULL;
333 user = malloc(strlen(conn->user)+1);
336 DEBUG(0, ("PAM: PAM_session Malloc Failed!\n"));
340 /* This is freed by PAM */
341 StrnCpy(user, conn->user, strlen(conn->user)+1);
343 if (!proc_pam_start(&pamh, user))
349 if (proc_pam_session(pamh, user, tty, flag))
351 return proc_pam_end(pamh);
361 * PAM Password Validation Suite
363 BOOL pam_passcheck(char * user, char * password)
365 pam_handle_t *pamh = NULL;
368 PAM_password = password;
370 if( proc_pam_start(&pamh, user))
372 if ( pam_auth(pamh, user, password))
374 if ( pam_account(pamh, user, password))
376 return( proc_pam_end(pamh));
380 DEBUG(0, ("PAM: System Validation Failed - Rejecting User!\n"));
386 /* Do *NOT* make this function static. Doing so breaks the compile on gcc */
388 void pampass_dummy_function( void ) { } /*This stops compiler complaints */
390 #endif /* WITH_PAM */