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