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