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