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