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