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