Merged John's changes.
[samba.git] / source3 / auth / pampass.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2.
4    PAM Password checking
5    Copyright (C) Andrew Tridgell 1992-2001
6    Copyright (C) John H Terpsta 1999-2001
7    Copyright (C) Andrew Barton 2001
8    
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.
13    
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.
18    
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.
22 */
23
24 /*
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
28  */
29
30 #include "includes.h"
31
32 extern int DEBUGLEVEL;
33
34 #ifdef WITH_PAM
35
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  *********************************************************************/
42
43 #include <security/pam_appl.h>
44
45 /*
46  * Static variables used to communicate between the conversation function
47  * and the server_login function
48  */
49
50 static char *PAM_username;
51 static char *PAM_password;
52
53 /*
54  *  Macros to help make life easy
55  */
56 #define COPY_STRING(s) (s) ? strdup(s) : NULL
57
58 /*
59  * PAM error handler.
60  */
61 static BOOL pam_error_handler(pam_handle_t *pamh, int pam_error, char *msg, int dbglvl)
62 {
63
64         int retval;
65
66         if( pam_error != PAM_SUCCESS)
67         {
68                 DEBUG(dbglvl, ("PAM: %s : %s\n", msg, pam_strerror(pamh, pam_error)));
69                 return False;
70         }
71         return True;
72 }
73
74 /*
75  * PAM conversation function
76  * Here we assume (for now, at least) that echo on means login name, and
77  * echo off means password.
78  */
79
80 static int PAM_conv(int num_msg,
81                     const struct pam_message **msg,
82                     struct pam_response **resp,
83                     void *appdata_ptr)
84 {
85         int replies = 0;
86         struct pam_response *reply = NULL;
87
88         reply = malloc(sizeof(struct pam_response) * num_msg);
89         if (!reply)
90                 return PAM_CONV_ERR;
91
92         for (replies = 0; replies < num_msg; replies++)
93         {
94                 switch (msg[replies]->msg_style)
95                 {
96                         case PAM_PROMPT_ECHO_ON:
97                                 reply[replies].resp_retcode = PAM_SUCCESS;
98                         reply[replies].resp =
99                                         COPY_STRING(PAM_username);
100                                 /* PAM frees resp */
101                                 break;
102
103                         case PAM_PROMPT_ECHO_OFF:
104                                 reply[replies].resp_retcode = PAM_SUCCESS;
105                                 reply[replies].resp =
106                                         COPY_STRING(PAM_password);
107                                 /* PAM frees resp */
108                                 break;
109
110                         case PAM_TEXT_INFO:
111                                 /* fall through */
112
113                         case PAM_ERROR_MSG:
114                                 /* ignore it... */
115                                 reply[replies].resp_retcode = PAM_SUCCESS;
116                                 reply[replies].resp = NULL;
117                                 break;
118
119                         default:
120                                 /* Must be an error of some sort... */
121                                 free(reply);
122                                 return PAM_CONV_ERR;
123                 }
124         }
125         if (reply)
126                 *resp = reply;
127         return PAM_SUCCESS;
128 }
129
130 static struct pam_conv PAM_conversation = {
131         &PAM_conv,
132         NULL
133 };
134
135 /* 
136  * PAM Closing out cleanup handler
137  */
138 static BOOL proc_pam_end(pam_handle_t *pamh)
139 {
140        int pam_error;
141        
142        if( pamh != NULL )
143        {
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"));
147                         return True;
148                 }
149        }
150        DEBUG(2,("PAM: not initialised"));
151        return False;
152 }
153
154 /*
155  * Start PAM authentication for specified account
156  */
157 static BOOL proc_pam_start(pam_handle_t **pamh, char *user)
158 {
159        int pam_error;
160        char * rhost;
161
162        DEBUG(4,("PAM: Init user: %s\n", user));
163
164        pam_error = pam_start("samba", user, &PAM_conversation, pamh);
165        if( !pam_error_handler(*pamh, pam_error, "Init Failed", 0)) {
166                proc_pam_end(*pamh);
167                return False;
168        }
169
170        rhost = client_name();
171        if (strcmp(rhost,"UNKNOWN") == 0)
172                rhost = client_addr();
173
174 #ifdef PAM_RHOST
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)) {
178                proc_pam_end(*pamh);
179                return False;
180        }
181 #endif
182 #ifdef PAM_TTY
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)) {
186                proc_pam_end(*pamh);
187                return False;
188        }
189 #endif
190        DEBUG(4,("PAM: Init passed for user: %s\n", user));
191        return True;
192 }
193
194 /*
195  * PAM Authentication Handler
196  */
197 static BOOL pam_auth(pam_handle_t *pamh, char *user, char *password)
198 {
199         int pam_error;
200
201         /*
202          * To enable debugging set in /etc/pam.d/samba:
203          *      auth required /lib/security/pam_pwdb.so nullok shadow audit
204          */
205         
206         DEBUG(4,("PAM: Authenticate User: %s\n", user));
207         pam_error = pam_authenticate(pamh, PAM_SILENT); /* Can we authenticate user? */
208         switch( pam_error ){
209                 case PAM_AUTH_ERR:
210                         DEBUG(2, ("PAM: Athentication Error\n"));
211                         break;
212                 case PAM_CRED_INSUFFICIENT:
213                         DEBUG(2, ("PAM: Insufficient Credentials\n"));
214                         break;
215                 case PAM_AUTHINFO_UNAVAIL:
216                         DEBUG(2, ("PAM: Authentication Information Unavailable\n"));
217                         break;
218                 case PAM_USER_UNKNOWN:
219                         DEBUG(2, ("PAM: Username NOT known to Authentication system\n"));
220                         break;
221                 case PAM_MAXTRIES:
222                         DEBUG(2, ("PAM: One or more authentication modules reports user limit exceeeded\n"));
223                         break;
224                 case PAM_ABORT:
225                         DEBUG(0, ("PAM: One or more PAM modules failed to load\n"));
226                         break;
227                 default:
228                         DEBUG(4, ("PAM: User %s Authenticated OK\n", user));
229         }
230         if(!pam_error_handler(pamh, pam_error, "Authentication Failure", 2)) {
231                 proc_pam_end(pamh);
232                 return False;
233         }
234         /* If this point is reached, the user has been authenticated. */
235         return (True);
236 }
237
238 /* 
239  * PAM Account Handler
240  */
241 static BOOL pam_account(pam_handle_t *pamh, char * user, char * password)
242 {
243         int pam_error;
244
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"));
250                         break;
251                 case PAM_ACCT_EXPIRED:
252                         DEBUG(2, ("PAM: User no longer permitted to access system\n"));
253                         break;
254                 case PAM_AUTH_ERR:
255                         DEBUG(2, ("PAM: There was an authentication error\n"));
256                         break;
257                 case PAM_PERM_DENIED:
258                         DEBUG(0, ("PAM: User is NOT permitted to access system at this time\n"));
259                         break;
260                 case PAM_USER_UNKNOWN:
261                         DEBUG(0, ("PAM: User \"%s\" is NOT known to account management\n", user));
262                         break;
263                 default:
264                         DEBUG(4, ("PAM: Account OK for User: %s\n", user));
265         }
266         if(!pam_error_handler(pamh, pam_error, "Account Check Failed", 2)) {
267                 proc_pam_end(pamh);
268                 return False;
269         }
270
271         /*
272          * This will allow samba to aquire a kerberos token. And, when
273          * exporting an AFS cell, be able to /write/ to this cell.
274          */
275
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)) {
279                 proc_pam_end(pamh);
280                 return False;
281         }
282         
283         /* If this point is reached, the user has been authenticated. */
284         return (True);
285 }
286
287
288 /*
289  * PAM Internal Session Handler
290  */
291 static BOOL proc_pam_session(pam_handle_t *pamh, char *user, char *tty, BOOL flag)
292 {
293        int pam_error;
294
295        PAM_password = NULL;
296        PAM_username = user;
297
298 #ifdef PAM_TTY
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)) {
302                proc_pam_end(pamh);
303                return False;
304        }
305 #endif
306
307        if (flag) {
308          pam_error = pam_open_session(pamh, PAM_SILENT);
309          if (!pam_error_handler(pamh, pam_error, "session setup failed", 0)) {
310                proc_pam_end(pamh);
311                return False;
312          }
313        }
314        else
315        {
316          pam_error = pam_close_session(pamh, PAM_SILENT);
317          if (!pam_error_handler(pamh, pam_error, "session close failed", 0)) {
318                proc_pam_end(pamh);
319                return False;
320          }
321        }
322       return (True);
323 }
324
325 /*
326  * PAM Externally accessible Session handler
327  */
328 BOOL pam_session(BOOL flag, const connection_struct *conn, char *tty)
329 {
330         pam_handle_t *pamh = NULL;
331         char * user;
332
333         user = malloc(strlen(conn->user)+1);
334         if ( user == NULL )
335         {
336                 DEBUG(0, ("PAM: PAM_session Malloc Failed!\n"));
337                 return False;
338         }
339
340         /* This is freed by PAM */
341         StrnCpy(user, conn->user, strlen(conn->user)+1);
342
343         if (!proc_pam_start(&pamh, user))
344         {
345           proc_pam_end(pamh);
346           return False;
347         }
348
349         if (proc_pam_session(pamh, user, tty, flag))
350         {
351           return proc_pam_end(pamh);
352         }
353         else
354         {
355           proc_pam_end(pamh);
356           return False;
357         }
358 }
359
360 /*
361  * PAM Password Validation Suite
362  */
363 BOOL pam_passcheck(char * user, char * password)
364 {
365         pam_handle_t *pamh = NULL;
366
367         PAM_username = user;
368         PAM_password = password;
369
370         if( proc_pam_start(&pamh, user))
371         {
372                 if ( pam_auth(pamh, user, password))
373                 {
374                         if ( pam_account(pamh, user, password))
375                         {
376                                 return( proc_pam_end(pamh));
377                         }
378                 }
379         }
380         DEBUG(0, ("PAM: System Validation Failed - Rejecting User!\n"));
381         return( False );
382 }
383
384 #else
385
386  /* Do *NOT* make this function static. Doing so breaks the compile on gcc */
387
388  void pampass_dummy_function( void ) { } /*This stops compiler complaints */
389
390 #endif /* WITH_PAM */