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