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