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