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