Had to add a "pam password change" parameter (defaults to "off") and inlined
[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 Bartlett 2001
8    Copyright (C) Jeremy Allison 2001
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 /*
26  * This module provides PAM based functions for validation of
27  * username/password pairs, account managment, session and access control.
28  * Note: SMB password checking is done in smbpass.c
29  */
30
31 #include "includes.h"
32
33 extern int DEBUGLEVEL;
34
35 #ifdef WITH_PAM
36
37 /*******************************************************************
38  * Handle PAM authentication 
39  *      - Access, Authentication, Session, Password
40  *   Note: See PAM Documentation and refer to local system PAM implementation
41  *   which determines what actions/limitations/allowances become affected.
42  *********************************************************************/
43
44 #include <security/pam_appl.h>
45
46 /*
47  * Structure used to communicate between the conversation function
48  * and the server_login/change password functions.
49  */
50
51 struct smb_pam_userdata {
52         char *PAM_username;
53         char *PAM_password;
54         char *PAM_newpassword;
55 };
56
57 typedef int (*smb_pam_conv_fn)(int, const struct pam_message **, struct pam_response **, void *appdata_ptr);
58
59 /*
60  *  Macros to help make life easy
61  */
62 #define COPY_STRING(s) (s) ? strdup(s) : NULL
63
64 /*******************************************************************
65  PAM error handler.
66  *********************************************************************/
67
68 static BOOL smb_pam_error_handler(pam_handle_t *pamh, int pam_error, char *msg, int dbglvl)
69 {
70
71         if( pam_error != PAM_SUCCESS) {
72                 DEBUG(dbglvl, ("smb_pam_error_handler: PAM: %s : %s\n",
73                                 msg, pam_strerror(pamh, pam_error)));
74                 return False;
75         }
76         return True;
77 }
78
79 /*******************************************************************
80  This function is a sanity check, to make sure that we NEVER report
81  failure as sucess.
82 *********************************************************************/
83
84 static BOOL smb_pam_nt_status_error_handler(pam_handle_t *pamh, int pam_error,
85                                                         char *msg, int dbglvl, uint32 *nt_status)
86 {
87         if (smb_pam_error_handler(pamh, pam_error, msg, dbglvl))
88                 return True;
89
90         if (*nt_status == NT_STATUS_NOPROBLEMO) {
91                 /* Complain LOUDLY */
92                 DEBUG(0, ("smb_pam_nt_status_error_handler: PAM: BUG: PAM and NT_STATUS \
93 error MISMATCH, forcing to NT_STATUS_LOGON_FAILURE"));
94                 *nt_status = NT_STATUS_LOGON_FAILURE;
95         }
96         return False;
97 }
98
99 /*
100  * PAM conversation function
101  * Here we assume (for now, at least) that echo on means login name, and
102  * echo off means password.
103  */
104
105 static int smb_pam_conv(int num_msg,
106                     const struct pam_message **msg,
107                     struct pam_response **resp,
108                     void *appdata_ptr)
109 {
110         int replies = 0;
111         struct pam_response *reply = NULL;
112         struct smb_pam_userdata *udp = (struct smb_pam_userdata *)appdata_ptr;
113
114         *resp = NULL;
115
116         if (num_msg <= 0)
117                 return PAM_CONV_ERR;
118
119         /*
120          * Apparantly HPUX has a buggy PAM that doesn't support the
121          * appdata_ptr. Fail if this is the case. JRA.
122          */
123
124         if (udp == NULL) {
125                 DEBUG(0,("smb_pam_conv: PAM on this system is broken - appdata_ptr == NULL !\n"));
126                 return PAM_CONV_ERR;
127         }
128
129         reply = malloc(sizeof(struct pam_response) * num_msg);
130         if (!reply)
131                 return PAM_CONV_ERR;
132
133         for (replies = 0; replies < num_msg; replies++) {
134                 switch (msg[replies]->msg_style) {
135                         case PAM_PROMPT_ECHO_ON:
136                                 reply[replies].resp_retcode = PAM_SUCCESS;
137                                 reply[replies].resp = COPY_STRING(udp->PAM_username);
138                                 /* PAM frees resp */
139                                 break;
140
141                         case PAM_PROMPT_ECHO_OFF:
142                                 reply[replies].resp_retcode = PAM_SUCCESS;
143                                 reply[replies].resp = COPY_STRING(udp->PAM_password);
144                                 /* PAM frees resp */
145                                 break;
146
147                         case PAM_TEXT_INFO:
148                                 /* fall through */
149
150                         case PAM_ERROR_MSG:
151                                 /* ignore it... */
152                                 reply[replies].resp_retcode = PAM_SUCCESS;
153                                 reply[replies].resp = NULL;
154                                 break;
155
156                         default:
157                                 /* Must be an error of some sort... */
158                                 free(reply);
159                                 return PAM_CONV_ERR;
160                 }
161         }
162         if (reply)
163                 *resp = reply;
164         return PAM_SUCCESS;
165 }
166
167 /*
168  * PAM password change conversation function
169  * Here we assume (for now, at least) that echo on means login name, and
170  * echo off means password.
171  */
172
173 static int smb_pam_passchange_conv(int num_msg,
174                                 const struct pam_message **msg,
175                                 struct pam_response **resp,
176                                 void *appdata_ptr)
177 {
178         int replies = 0;
179         struct pam_response *reply = NULL;
180         fstring newpw_prompt;
181         fstring repeatpw_prompt;
182         char *p = lp_passwd_chat();
183         struct smb_pam_userdata *udp = (struct smb_pam_userdata *)appdata_ptr;
184
185         *resp = NULL;
186
187         if (num_msg <= 0)
188                 return PAM_CONV_ERR;
189
190         /*
191          * Apparantly HPUX has a buggy PAM that doesn't support the
192          * appdata_ptr. Fail if this is the case. JRA.
193          */
194
195         if (udp == NULL) {
196                 DEBUG(0,("smb_pam_passchange_conv: PAM on this system is broken - appdata_ptr == NULL !\n"));
197                 return PAM_CONV_ERR;
198         }
199
200         /* Get the prompts. We're running as root so we only get 2 prompts. */
201
202         if (!next_token(&p, newpw_prompt, NULL, sizeof(fstring)))
203                 return PAM_CONV_ERR;
204         if (!next_token(&p, repeatpw_prompt, NULL, sizeof(fstring)))
205                 return PAM_CONV_ERR;
206
207         reply = malloc(sizeof(struct pam_response) * num_msg);
208         if (!reply)
209                 return PAM_CONV_ERR;
210
211         for (replies = 0; replies < num_msg; replies++) {
212                 switch (msg[replies]->msg_style) {
213                 case PAM_PROMPT_ECHO_ON:
214                         DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: Replied: %s\n", msg[replies]->msg));
215                         reply[replies].resp_retcode = PAM_SUCCESS;
216                         reply[replies].resp = COPY_STRING(udp->PAM_username);
217                         /* PAM frees resp */
218                         break;
219
220                 case PAM_PROMPT_ECHO_OFF:
221                         reply[replies].resp_retcode = PAM_SUCCESS;
222                         DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: Replied: %s\n", msg[replies]->msg));
223                         if (ms_fnmatch( newpw_prompt, msg[replies]->msg) == 0) {
224                                 reply[replies].resp = COPY_STRING(udp->PAM_newpassword);
225                         } else if (ms_fnmatch(repeatpw_prompt, msg[replies]->msg) == 0) {
226                                 reply[replies].resp = COPY_STRING(udp->PAM_newpassword);
227                         } else {
228                                 DEBUG(3,("smb_pam_passchange_conv: Could not find reply for PAM prompt: %s\n",msg[replies]->msg));
229                                 DEBUG(5,("smb_pam_passchange_conv: Prompts available:\n NewPW: \"%s\"\n \
230 RepeatPW: \"%s\"\n",newpw_prompt,repeatpw_prompt));
231                                 free(reply);
232                                 reply = NULL;
233                                 return PAM_CONV_ERR;
234                         }
235                         /* PAM frees resp */
236                         break;
237
238                 case PAM_TEXT_INFO:
239                         /* fall through */
240
241                 case PAM_ERROR_MSG:
242                         /* ignore it... */
243                         reply[replies].resp_retcode = PAM_SUCCESS;
244                         reply[replies].resp = NULL;
245                         break;
246
247                 default:
248                         /* Must be an error of some sort... */
249                         free(reply);
250                         reply = NULL;
251                         return PAM_CONV_ERR;
252                 }
253         }
254
255         if (reply)
256                 *resp = reply;
257         return PAM_SUCCESS;
258 }
259
260 /***************************************************************************
261  Free up a malloced pam_conv struct.
262 ****************************************************************************/
263
264 static void smb_free_pam_conv(struct pam_conv *pconv)
265 {
266         if (pconv)
267                 safe_free(pconv->appdata_ptr);
268
269         safe_free(pconv);
270 }
271
272 /***************************************************************************
273  Allocate a pam_conv struct.
274 ****************************************************************************/
275
276 static struct pam_conv *smb_setup_pam_conv(smb_pam_conv_fn smb_pam_conv_fnptr, char *user,
277                                         char *passwd, char *newpass)
278 {
279         struct pam_conv *pconv = (struct pam_conv *)malloc(sizeof(struct pam_conv));
280         struct smb_pam_userdata *udp = (struct smb_pam_userdata *)malloc(sizeof(struct smb_pam_userdata));
281
282         if (pconv == NULL || udp == NULL) {
283                 safe_free(pconv);
284                 safe_free(udp);
285                 return NULL;
286         }
287
288         udp->PAM_username = user;
289         udp->PAM_password = passwd;
290         udp->PAM_newpassword = newpass;
291
292         pconv->conv = smb_pam_conv_fnptr;
293         pconv->appdata_ptr = (void *)udp;
294         return pconv;
295 }
296
297 /* 
298  * PAM Closing out cleanup handler
299  */
300
301 static BOOL smb_pam_end(pam_handle_t *pamh, struct pam_conv *smb_pam_conv_ptr)
302 {
303         int pam_error;
304
305         smb_free_pam_conv(smb_pam_conv_ptr);
306        
307         if( pamh != NULL ) {
308                 pam_error = pam_end(pamh, 0);
309                 if(smb_pam_error_handler(pamh, pam_error, "End Cleanup Failed", 2) == True) {
310                         DEBUG(4, ("smb_pam_end: PAM: PAM_END OK.\n"));
311                         return True;
312                 }
313         }
314         DEBUG(2,("smb_pam_end: PAM: not initialised"));
315         return False;
316 }
317
318 /*
319  * Start PAM authentication for specified account
320  */
321
322 static BOOL smb_pam_start(pam_handle_t **pamh, char *user, char *rhost, struct pam_conv *pconv)
323 {
324         int pam_error;
325
326         *pamh = (pam_handle_t *)NULL;
327
328         DEBUG(4,("smb_pam_start: PAM: Init user: %s\n", user));
329
330         pam_error = pam_start("samba", user, pconv, pamh);
331         if( !smb_pam_error_handler(*pamh, pam_error, "Init Failed", 0)) {
332                 *pamh = (pam_handle_t *)NULL;
333                 return False;
334         }
335
336         if (rhost == NULL) {
337                 rhost = client_name();
338                 if (strequal(rhost,"UNKNOWN"))
339                         rhost = client_addr();
340         }
341
342 #ifdef PAM_RHOST
343         DEBUG(4,("smb_pam_start: PAM: setting rhost to: %s\n", rhost));
344         pam_error = pam_set_item(*pamh, PAM_RHOST, rhost);
345         if(!smb_pam_error_handler(*pamh, pam_error, "set rhost failed", 0)) {
346                 smb_pam_end(*pamh, pconv);
347                 *pamh = (pam_handle_t *)NULL;
348                 return False;
349         }
350 #endif
351 #ifdef PAM_TTY
352         DEBUG(4,("smb_pam_start: PAM: setting tty\n"));
353         pam_error = pam_set_item(*pamh, PAM_TTY, "samba");
354         if (!smb_pam_error_handler(*pamh, pam_error, "set tty failed", 0)) {
355                 smb_pam_end(*pamh, pconv);
356                 *pamh = (pam_handle_t *)NULL;
357                 return False;
358         }
359 #endif
360         DEBUG(4,("smb_pam_start: PAM: Init passed for user: %s\n", user));
361         return True;
362 }
363
364 /*
365  * PAM Authentication Handler
366  */
367 static uint32 smb_pam_auth(pam_handle_t *pamh, char *user)
368 {
369         int pam_error;
370         uint32 nt_status = NT_STATUS_LOGON_FAILURE;
371
372         /*
373          * To enable debugging set in /etc/pam.d/samba:
374          *      auth required /lib/security/pam_pwdb.so nullok shadow audit
375          */
376         
377         DEBUG(4,("smb_pam_auth: PAM: Authenticate User: %s\n", user));
378         pam_error = pam_authenticate(pamh, PAM_SILENT | lp_null_passwords() ? 0 : PAM_DISALLOW_NULL_AUTHTOK);
379         switch( pam_error ){
380                 case PAM_AUTH_ERR:
381                         DEBUG(2, ("smb_pam_auth: PAM: Athentication Error for user %s\n", user));
382                         nt_status = NT_STATUS_WRONG_PASSWORD;
383                         break;
384                 case PAM_CRED_INSUFFICIENT:
385                         DEBUG(2, ("smb_pam_auth: PAM: Insufficient Credentials for user %s\n", user));
386                         nt_status = NT_STATUS_INSUFFICIENT_LOGON_INFO;
387                         break;
388                 case PAM_AUTHINFO_UNAVAIL:
389                         DEBUG(2, ("smb_pam_auth: PAM: Authentication Information Unavailable for user %s\n", user));
390                         nt_status = NT_STATUS_LOGON_FAILURE;
391                         break;
392                 case PAM_USER_UNKNOWN:
393                         DEBUG(2, ("smb_pam_auth: PAM: Username %s NOT known to Authentication system\n", user));
394                         nt_status = NT_STATUS_NO_SUCH_USER;
395                         break;
396                 case PAM_MAXTRIES:
397                         DEBUG(2, ("smb_pam_auth: PAM: One or more authentication modules reports user limit for user %s exceeeded\n", user));
398                         nt_status = NT_STATUS_REMOTE_SESSION_LIMIT;
399                         break;
400                 case PAM_ABORT:
401                         DEBUG(0, ("smb_pam_auth: PAM: One or more PAM modules failed to load for user %s\n", user));
402                         nt_status = NT_STATUS_LOGON_FAILURE;
403                         break;
404                 case PAM_SUCCESS:
405                         DEBUG(4, ("smb_pam_auth: PAM: User %s Authenticated OK\n", user));
406                         nt_status = NT_STATUS_NOPROBLEMO;
407                         break;
408                 default:
409                         DEBUG(0, ("smb_pam_auth: PAM: UNKNOWN ERROR while authenticating user %s\n", user));
410                         nt_status = NT_STATUS_LOGON_FAILURE;
411                         break;
412         }
413
414         smb_pam_nt_status_error_handler(pamh, pam_error, "Authentication Failure", 2, &nt_status);
415         return nt_status;
416 }
417
418 /* 
419  * PAM Account Handler
420  */
421 static uint32 smb_pam_account(pam_handle_t *pamh, char * user)
422 {
423         int pam_error;
424         uint32 nt_status = NT_STATUS_ACCOUNT_DISABLED;
425
426         DEBUG(4,("smb_pam_account: PAM: Account Management for User: %s\n", user));
427         pam_error = pam_acct_mgmt(pamh, PAM_SILENT); /* Is user account enabled? */
428         switch( pam_error ) {
429                 case PAM_AUTHTOK_EXPIRED:
430                         DEBUG(2, ("smb_pam_account: PAM: User %s is valid but password is expired\n", user));
431                         nt_status = NT_STATUS_PASSWORD_EXPIRED;
432                         break;
433                 case PAM_ACCT_EXPIRED:
434                         DEBUG(2, ("smb_pam_account: PAM: User %s no longer permitted to access system\n", user));
435                         nt_status = NT_STATUS_ACCOUNT_EXPIRED;
436                         break;
437                 case PAM_AUTH_ERR:
438                         DEBUG(2, ("smb_pam_account: PAM: There was an authentication error for user %s\n", user));
439                         nt_status = NT_STATUS_LOGON_FAILURE;
440                         break;
441                 case PAM_PERM_DENIED:
442                         DEBUG(0, ("smb_pam_account: PAM: User %s is NOT permitted to access system at this time\n", user));
443                         nt_status = NT_STATUS_ACCOUNT_RESTRICTION;
444                         break;
445                 case PAM_USER_UNKNOWN:
446                         DEBUG(0, ("smb_pam_account: PAM: User \"%s\" is NOT known to account management\n", user));
447                         nt_status = NT_STATUS_NO_SUCH_USER;
448                         break;
449                 case PAM_SUCCESS:
450                         DEBUG(4, ("smb_pam_account: PAM: Account OK for User: %s\n", user));
451                         nt_status = NT_STATUS_NOPROBLEMO;
452                         break;
453                 default:
454                         nt_status = NT_STATUS_ACCOUNT_DISABLED;
455                         DEBUG(0, ("smb_pam_account: PAM: UNKNOWN PAM ERROR (%d) during Account Management for User: %s\n", pam_error, user));
456                         break;
457         }
458
459         smb_pam_nt_status_error_handler(pamh, pam_error, "Account Check Failed", 2, &nt_status);
460         return nt_status;
461 }
462
463 /*
464  * PAM Credential Setting
465  */
466
467 static uint32 smb_pam_setcred(pam_handle_t *pamh, char * user)
468 {
469         int pam_error;
470         uint32 nt_status = NT_STATUS_NO_TOKEN;
471
472         /*
473          * This will allow samba to aquire a kerberos token. And, when
474          * exporting an AFS cell, be able to /write/ to this cell.
475          */
476
477         DEBUG(4,("PAM: Account Management SetCredentials for User: %s\n", user));
478         pam_error = pam_setcred(pamh, (PAM_ESTABLISH_CRED|PAM_SILENT)); 
479         switch( pam_error ) {
480                 case PAM_CRED_UNAVAIL:
481                         DEBUG(0, ("smb_pam_setcred: PAM: Credentials not found for user:%s\n", user ));
482                          nt_status = NT_STATUS_NO_TOKEN;
483                         break;
484                 case PAM_CRED_EXPIRED:
485                         DEBUG(0, ("smb_pam_setcred: PAM: Credentials for user: \"%s\" EXPIRED!\n", user ));
486                         nt_status = NT_STATUS_PASSWORD_EXPIRED;
487                         break;
488                 case PAM_USER_UNKNOWN:
489                         DEBUG(0, ("smb_pam_setcred: PAM: User: \"%s\" is NOT known so can not set credentials!\n", user ));
490                         nt_status = NT_STATUS_NO_SUCH_USER;
491                         break;
492                 case PAM_CRED_ERR:
493                         DEBUG(0, ("smb_pam_setcred: PAM: Unknown setcredentials error - unable to set credentials for %s\n", user ));
494                         nt_status = NT_STATUS_LOGON_FAILURE;
495                         break;
496                 case PAM_SUCCESS:
497                         DEBUG(4, ("smb_pam_setcred: PAM: SetCredentials OK for User: %s\n", user));
498                         nt_status = NT_STATUS_NOPROBLEMO;
499                         break;
500                 default:
501                         DEBUG(0, ("smb_pam_setcred: PAM: UNKNOWN PAM ERROR (%d) during SetCredentials for User: %s\n", pam_error, user));
502                         nt_status = NT_STATUS_NO_TOKEN;
503                         break;
504         }
505
506         smb_pam_nt_status_error_handler(pamh, pam_error, "Set Credential Failure", 2, &nt_status);
507         return nt_status;
508 }
509
510 /*
511  * PAM Internal Session Handler
512  */
513 static BOOL smb_internal_pam_session(pam_handle_t *pamh, char *user, char *tty, BOOL flag)
514 {
515         int pam_error;
516
517 #ifdef PAM_TTY
518         DEBUG(4,("smb_internal_pam_session: PAM: tty set to: %s\n", tty));
519         pam_error = pam_set_item(pamh, PAM_TTY, tty);
520         if (!smb_pam_error_handler(pamh, pam_error, "set tty failed", 0))
521                 return False;
522 #endif
523
524         if (flag) {
525                 pam_error = pam_open_session(pamh, PAM_SILENT);
526                 if (!smb_pam_error_handler(pamh, pam_error, "session setup failed", 0))
527                         return False;
528         } else {
529                 pam_setcred(pamh, (PAM_DELETE_CRED|PAM_SILENT)); /* We don't care if this fails */
530                 pam_error = pam_close_session(pamh, PAM_SILENT); /* This will probably pick up the error anyway */
531                 if (!smb_pam_error_handler(pamh, pam_error, "session close failed", 0))
532                         return False;
533         }
534         return (True);
535 }
536
537 /*
538  * Internal PAM Password Changer.
539  */
540
541 static BOOL smb_pam_chauthtok(pam_handle_t *pamh, char * user)
542 {
543         int pam_error;
544
545         DEBUG(4,("smb_pam_chauthtok: PAM: Password Change for User: %s\n", user));
546
547         pam_error = pam_chauthtok(pamh, PAM_SILENT); /* Change Password */
548
549         switch( pam_error ) {
550         case PAM_AUTHTOK_ERR:
551                 DEBUG(2, ("PAM: unable to obtain the new authentication token - is password to weak?\n"));
552                 break;
553
554         /* This doesn't seem to be defined on Solaris. JRA */
555 #ifdef PAM_AUTHTOK_RECOVER_ERR
556         case PAM_AUTHTOK_RECOVER_ERR:
557                 DEBUG(2, ("PAM: unable to obtain the old authentication token - was the old password wrong?.\n"));
558                 break;
559 #endif
560
561         case PAM_AUTHTOK_LOCK_BUSY:
562                 DEBUG(2, ("PAM: unable to change the authentication token since it is currently locked.\n"));
563                 break;
564         case PAM_AUTHTOK_DISABLE_AGING:
565                 DEBUG(2, ("PAM: Authentication token aging has been disabled.\n"));
566                 break;
567         case PAM_PERM_DENIED:
568                 DEBUG(0, ("PAM: Permission denied.\n"));
569                 break;
570         case PAM_TRY_AGAIN:
571                 DEBUG(0, ("PAM: Could not update all authentication token(s). No authentication tokens were updated.\n"));
572                 break;
573         case PAM_USER_UNKNOWN:
574                 DEBUG(0, ("PAM: User not known to PAM\n"));
575                 break;
576         case PAM_SUCCESS:
577                 DEBUG(4, ("PAM: Account OK for User: %s\n", user));
578                 break;
579         default:
580                 DEBUG(0, ("PAM: UNKNOWN PAM ERROR (%d) for User: %s\n", pam_error, user));
581         }
582  
583         if(!smb_pam_error_handler(pamh, pam_error, "Password Change Failed", 2)) {
584                 return False;
585         }
586
587         /* If this point is reached, the password has changed. */
588         return True;
589 }
590
591 /*
592  * PAM Externally accessible Session handler
593  */
594
595 BOOL smb_pam_claim_session(char *user, char *tty, char *rhost)
596 {
597         pam_handle_t *pamh = NULL;
598         struct pam_conv *pconv = NULL;
599
600         /* Ignore PAM if told to. */
601
602         if (!lp_obey_pam_restrictions())
603                 return True;
604
605         if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
606                 return False;
607
608         if (!smb_pam_start(&pamh, user, rhost, pconv))
609                 return False;
610
611         if (!smb_internal_pam_session(pamh, user, tty, True)) {
612                 smb_pam_end(pamh, pconv);
613                 return False;
614         }
615
616         return smb_pam_end(pamh, pconv);
617 }
618
619 /*
620  * PAM Externally accessible Session handler
621  */
622
623 BOOL smb_pam_close_session(char *user, char *tty, char *rhost)
624 {
625         pam_handle_t *pamh = NULL;
626         struct pam_conv *pconv = NULL;
627
628         /* Ignore PAM if told to. */
629
630         if (!lp_obey_pam_restrictions())
631                 return True;
632
633         if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
634                 return False;
635
636         if (!smb_pam_start(&pamh, user, rhost, pconv))
637                 return False;
638
639         if (!smb_internal_pam_session(pamh, user, tty, False)) {
640                 smb_pam_end(pamh, pconv);
641                 return False;
642         }
643
644         return smb_pam_end(pamh, pconv);
645 }
646
647 /*
648  * PAM Externally accessible Account handler
649  */
650
651 uint32 smb_pam_accountcheck(char * user)
652 {
653         uint32 nt_status = NT_STATUS_ACCOUNT_DISABLED;
654         pam_handle_t *pamh = NULL;
655         struct pam_conv *pconv = NULL;
656
657         /* Ignore PAM if told to. */
658
659         if (!lp_obey_pam_restrictions())
660                 return NT_STATUS_NOPROBLEMO;
661
662         if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
663                 return False;
664
665         if (!smb_pam_start(&pamh, user, NULL, pconv))
666                 return NT_STATUS_ACCOUNT_DISABLED;
667
668         if ((nt_status = smb_pam_account(pamh, user)) != NT_STATUS_NOPROBLEMO)
669                 DEBUG(0, ("smb_pam_accountcheck: PAM: Account Validation Failed - Rejecting User %s!\n", user));
670
671         smb_pam_end(pamh, pconv);
672         return nt_status;
673 }
674
675 /*
676  * PAM Password Validation Suite
677  */
678
679 uint32 smb_pam_passcheck(char * user, char * password)
680 {
681         pam_handle_t *pamh = NULL;
682         uint32 nt_status = NT_STATUS_LOGON_FAILURE;
683         struct pam_conv *pconv = NULL;
684
685         /*
686          * Note we can't ignore PAM here as this is the only
687          * way of doing auths on plaintext passwords when
688          * compiled --with-pam.
689          */
690
691         if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, password, NULL)) == NULL)
692                 return NT_STATUS_LOGON_FAILURE;
693
694         if (!smb_pam_start(&pamh, user, NULL, pconv))
695                 return NT_STATUS_LOGON_FAILURE;
696
697         if ((nt_status = smb_pam_auth(pamh, user)) != NT_STATUS_NOPROBLEMO) {
698                 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_auth failed - Rejecting User %s !\n", user));
699                 smb_pam_end(pamh, pconv);
700                 return nt_status;
701         }
702
703         if ((nt_status = smb_pam_account(pamh, user)) != NT_STATUS_NOPROBLEMO) {
704                 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_account failed - Rejecting User %s !\n", user));
705                 smb_pam_end(pamh, pconv);
706                 return nt_status;
707         }
708
709         if ((nt_status = smb_pam_setcred(pamh, user)) != NT_STATUS_NOPROBLEMO) {
710                 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_setcred failed - Rejecting User %s !\n", user));
711                 smb_pam_end(pamh, pconv);
712                 return nt_status;
713         }
714
715         smb_pam_end(pamh, pconv);
716         return nt_status;
717 }
718
719 /*
720  * PAM Password Change Suite
721  */
722
723 BOOL smb_pam_passchange(char * user, char * oldpassword, char * newpassword)
724 {
725         /* Appropriate quantities of root should be obtained BEFORE calling this function */
726         struct pam_conv *pconv = NULL;
727         pam_handle_t *pamh = NULL;
728
729         if ((pconv = smb_setup_pam_conv(smb_pam_passchange_conv, user, oldpassword, newpassword)) == NULL)
730                 return False;
731
732         if(!smb_pam_start(&pamh, user, NULL, pconv))
733                 return False;
734
735         if (!smb_pam_chauthtok(pamh, user)) {
736                 DEBUG(0, ("smb_pam_passchange: PAM: Password Change Failed for user %s!\n", user));
737                 smb_pam_end(pamh, pconv);
738                 return False;
739         }
740
741         return smb_pam_end(pamh, pconv);
742 }
743
744 #else
745
746 /* If PAM not used, no PAM restrictions on accounts. */
747  uint32 smb_pam_accountcheck(char * user)
748 {
749         return NT_STATUS_NOPROBLEMO;
750 }
751
752 /* If PAM not used, also no PAM restrictions on sessions. */
753  BOOL smb_pam_claim_session(char *user, char *tty, char *rhost)
754 {
755         return True;
756 }
757
758 /* If PAM not used, also no PAM restrictions on sessions. */
759  BOOL smb_pam_close_session(char *in_user, char *tty, char *rhost)
760 {
761         return True;
762 }
763 #endif /* WITH_PAM */