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