3 Copyright Andrew Tridgell <tridge@samba.org> 2000
4 Copyright Tim Potter <tpot@samba.org> 2000
5 Copyright Andrew Bartlett <abartlet@samba.org> 2002
6 Copyright Guenther Deschner <gd@samba.org> 2005-2008
8 largely based on pam_userdb by Cristian Gafton <gafton@redhat.com> also
9 contains large slabs of code from pam_unix by Elliot Lee
10 <sopwith@redhat.com> (see copyright below for full details)
13 #define UID_WRAPPER_NOT_REPLACE
15 #include "pam_winbind.h"
17 static int wbc_error_to_pam_error(wbcErr status
)
22 case WBC_ERR_NOT_IMPLEMENTED
:
23 return PAM_SERVICE_ERR
;
24 case WBC_ERR_UNKNOWN_FAILURE
:
26 case WBC_ERR_NO_MEMORY
:
28 case WBC_ERR_INVALID_SID
:
29 case WBC_ERR_INVALID_PARAM
:
31 case WBC_ERR_WINBIND_NOT_AVAILABLE
:
32 return PAM_AUTHINFO_UNAVAIL
;
33 case WBC_ERR_DOMAIN_NOT_FOUND
:
34 return PAM_AUTHINFO_UNAVAIL
;
35 case WBC_ERR_INVALID_RESPONSE
:
37 case WBC_ERR_NSS_ERROR
:
38 return PAM_USER_UNKNOWN
;
39 case WBC_ERR_AUTH_ERROR
:
41 case WBC_ERR_UNKNOWN_USER
:
42 return PAM_USER_UNKNOWN
;
43 case WBC_ERR_UNKNOWN_GROUP
:
44 return PAM_USER_UNKNOWN
;
45 case WBC_ERR_PWD_CHANGE_FAILED
:
53 static const char *_pam_error_code_str(int err
)
59 return "PAM_OPEN_ERR";
61 return "PAM_SYMBOL_ERR";
63 return "PAM_SERVICE_ERR";
65 return "PAM_SYSTEM_ERR";
69 return "PAM_PERM_DENIED";
71 return "PAM_AUTH_ERR";
72 case PAM_CRED_INSUFFICIENT
:
73 return "PAM_CRED_INSUFFICIENT";
74 case PAM_AUTHINFO_UNAVAIL
:
75 return "PAM_AUTHINFO_UNAVAIL";
76 case PAM_USER_UNKNOWN
:
77 return "PAM_USER_UNKNOWN";
79 return "PAM_MAXTRIES";
80 case PAM_NEW_AUTHTOK_REQD
:
81 return "PAM_NEW_AUTHTOK_REQD";
82 case PAM_ACCT_EXPIRED
:
83 return "PAM_ACCT_EXPIRED";
85 return "PAM_SESSION_ERR";
86 case PAM_CRED_UNAVAIL
:
87 return "PAM_CRED_UNAVAIL";
88 case PAM_CRED_EXPIRED
:
89 return "PAM_CRED_EXPIRED";
91 return "PAM_CRED_ERR";
92 case PAM_NO_MODULE_DATA
:
93 return "PAM_NO_MODULE_DATA";
95 return "PAM_CONV_ERR";
97 return "PAM_AUTHTOK_ERR";
98 case PAM_AUTHTOK_RECOVER_ERR
:
99 return "PAM_AUTHTOK_RECOVER_ERR";
100 case PAM_AUTHTOK_LOCK_BUSY
:
101 return "PAM_AUTHTOK_LOCK_BUSY";
102 case PAM_AUTHTOK_DISABLE_AGING
:
103 return "PAM_AUTHTOK_DISABLE_AGING";
105 return "PAM_TRY_AGAIN";
110 case PAM_AUTHTOK_EXPIRED
:
111 return "PAM_AUTHTOK_EXPIRED";
112 #ifdef PAM_MODULE_UNKNOWN
113 case PAM_MODULE_UNKNOWN
:
114 return "PAM_MODULE_UNKNOWN";
118 return "PAM_BAD_ITEM";
120 #ifdef PAM_CONV_AGAIN
122 return "PAM_CONV_AGAIN";
124 #ifdef PAM_INCOMPLETE
126 return "PAM_INCOMPLETE";
133 #define _PAM_LOG_FUNCTION_ENTER(function, ctx) \
135 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] ENTER: " \
136 function " (flags: 0x%04x)", ctx->pamh, ctx->flags); \
137 _pam_log_state(ctx); \
140 #define _PAM_LOG_FUNCTION_LEAVE(function, ctx, retval) \
142 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] LEAVE: " \
143 function " returning %d (%s)", ctx->pamh, retval, \
144 _pam_error_code_str(retval)); \
145 _pam_log_state(ctx); \
150 #define MAX_PASSWD_TRIES 3
153 static char initialized
= 0;
155 static inline void textdomain_init(void);
156 static inline void textdomain_init(void)
159 bindtextdomain(MODULE_NAME
, LOCALEDIR
);
167 /* some syslogging */
169 #ifdef HAVE_PAM_VSYSLOG
170 static void _pam_log_int(const pam_handle_t
*pamh
,
175 pam_vsyslog(pamh
, err
, format
, args
);
178 static void _pam_log_int(const pam_handle_t
*pamh
,
183 char *format2
= NULL
;
186 pam_get_item(pamh
, PAM_SERVICE
, (const void **) &service
);
188 format2
= (char *)malloc(strlen(MODULE_NAME
)+strlen(format
)+strlen(service
)+5);
189 if (format2
== NULL
) {
190 /* what else todo ? */
191 vsyslog(err
, format
, args
);
195 sprintf(format2
, "%s(%s): %s", MODULE_NAME
, service
, format
);
196 vsyslog(err
, format2
, args
);
199 #endif /* HAVE_PAM_VSYSLOG */
201 static bool _pam_log_is_silent(int ctrl
)
203 return on(ctrl
, WINBIND_SILENT
);
206 static void _pam_log(struct pwb_context
*r
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
207 static void _pam_log(struct pwb_context
*r
, int err
, const char *format
, ...)
211 if (_pam_log_is_silent(r
->ctrl
)) {
215 va_start(args
, format
);
216 _pam_log_int(r
->pamh
, err
, format
, args
);
219 static void __pam_log(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(4,5);
220 static void __pam_log(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...)
224 if (_pam_log_is_silent(ctrl
)) {
228 va_start(args
, format
);
229 _pam_log_int(pamh
, err
, format
, args
);
233 static bool _pam_log_is_debug_enabled(int ctrl
)
239 if (_pam_log_is_silent(ctrl
)) {
243 if (!(ctrl
& WINBIND_DEBUG_ARG
)) {
250 static bool _pam_log_is_debug_state_enabled(int ctrl
)
252 if (!(ctrl
& WINBIND_DEBUG_STATE
)) {
256 return _pam_log_is_debug_enabled(ctrl
);
259 static void _pam_log_debug(struct pwb_context
*r
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
260 static void _pam_log_debug(struct pwb_context
*r
, int err
, const char *format
, ...)
264 if (!_pam_log_is_debug_enabled(r
->ctrl
)) {
268 va_start(args
, format
);
269 _pam_log_int(r
->pamh
, err
, format
, args
);
272 static void __pam_log_debug(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(4,5);
273 static void __pam_log_debug(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...)
277 if (!_pam_log_is_debug_enabled(ctrl
)) {
281 va_start(args
, format
);
282 _pam_log_int(pamh
, err
, format
, args
);
286 static void _pam_log_state_datum(struct pwb_context
*ctx
,
291 const void *data
= NULL
;
292 if (item_type
!= 0) {
293 pam_get_item(ctx
->pamh
, item_type
, &data
);
295 pam_get_data(ctx
->pamh
, key
, &data
);
298 const char *type
= (item_type
!= 0) ? "ITEM" : "DATA";
299 if (is_string
!= 0) {
300 _pam_log_debug(ctx
, LOG_DEBUG
,
301 "[pamh: %p] STATE: %s(%s) = \"%s\" (%p)",
302 ctx
->pamh
, type
, key
, (const char *)data
,
305 _pam_log_debug(ctx
, LOG_DEBUG
,
306 "[pamh: %p] STATE: %s(%s) = %p",
307 ctx
->pamh
, type
, key
, data
);
312 #define _PAM_LOG_STATE_DATA_POINTER(ctx, module_data_name) \
313 _pam_log_state_datum(ctx, 0, module_data_name, 0)
315 #define _PAM_LOG_STATE_DATA_STRING(ctx, module_data_name) \
316 _pam_log_state_datum(ctx, 0, module_data_name, 1)
318 #define _PAM_LOG_STATE_ITEM_POINTER(ctx, item_type) \
319 _pam_log_state_datum(ctx, item_type, #item_type, 0)
321 #define _PAM_LOG_STATE_ITEM_STRING(ctx, item_type) \
322 _pam_log_state_datum(ctx, item_type, #item_type, 1)
324 #ifdef DEBUG_PASSWORD
325 #define _LOG_PASSWORD_AS_STRING 1
327 #define _LOG_PASSWORD_AS_STRING 0
330 #define _PAM_LOG_STATE_ITEM_PASSWORD(ctx, item_type) \
331 _pam_log_state_datum(ctx, item_type, #item_type, \
332 _LOG_PASSWORD_AS_STRING)
334 static void _pam_log_state(struct pwb_context
*ctx
)
336 if (!_pam_log_is_debug_state_enabled(ctx
->ctrl
)) {
340 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_SERVICE
);
341 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_USER
);
342 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_TTY
);
343 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_RHOST
);
344 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_RUSER
);
345 _PAM_LOG_STATE_ITEM_PASSWORD(ctx
, PAM_OLDAUTHTOK
);
346 _PAM_LOG_STATE_ITEM_PASSWORD(ctx
, PAM_AUTHTOK
);
347 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_USER_PROMPT
);
348 _PAM_LOG_STATE_ITEM_POINTER(ctx
, PAM_CONV
);
349 #ifdef PAM_FAIL_DELAY
350 _PAM_LOG_STATE_ITEM_POINTER(ctx
, PAM_FAIL_DELAY
);
352 #ifdef PAM_REPOSITORY
353 _PAM_LOG_STATE_ITEM_POINTER(ctx
, PAM_REPOSITORY
);
356 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_HOMEDIR
);
357 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_LOGONSCRIPT
);
358 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_LOGONSERVER
);
359 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_PROFILEPATH
);
360 _PAM_LOG_STATE_DATA_STRING(ctx
,
361 PAM_WINBIND_NEW_AUTHTOK_REQD
);
362 /* Use atoi to get PAM result code */
363 _PAM_LOG_STATE_DATA_STRING(ctx
,
364 PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
);
365 _PAM_LOG_STATE_DATA_POINTER(ctx
, PAM_WINBIND_PWD_LAST_SET
);
368 static int _pam_parse(const pam_handle_t
*pamh
,
372 dictionary
**result_d
)
375 const char *config_file
= NULL
;
378 dictionary
*d
= NULL
;
380 if (flags
& PAM_SILENT
) {
381 ctrl
|= WINBIND_SILENT
;
384 for (i
=argc
,v
=argv
; i
-- > 0; ++v
) {
385 if (!strncasecmp(*v
, "config", strlen("config"))) {
386 ctrl
|= WINBIND_CONFIG_FILE
;
392 if (config_file
== NULL
) {
393 config_file
= PAM_WINBIND_CONFIG_FILE
;
396 d
= iniparser_load(discard_const_p(char, config_file
));
398 goto config_from_pam
;
401 if (iniparser_getboolean(d
, discard_const_p(char, "global:debug"), false)) {
402 ctrl
|= WINBIND_DEBUG_ARG
;
405 if (iniparser_getboolean(d
, discard_const_p(char, "global:debug_state"), false)) {
406 ctrl
|= WINBIND_DEBUG_STATE
;
409 if (iniparser_getboolean(d
, discard_const_p(char, "global:cached_login"), false)) {
410 ctrl
|= WINBIND_CACHED_LOGIN
;
413 if (iniparser_getboolean(d
, discard_const_p(char, "global:krb5_auth"), false)) {
414 ctrl
|= WINBIND_KRB5_AUTH
;
417 if (iniparser_getboolean(d
, discard_const_p(char, "global:silent"), false)) {
418 ctrl
|= WINBIND_SILENT
;
421 if (iniparser_getstring(d
, discard_const_p(char, "global:krb5_ccache_type"), NULL
) != NULL
) {
422 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
425 if ((iniparser_getstring(d
, discard_const_p(char, "global:require-membership-of"), NULL
)
427 (iniparser_getstring(d
, discard_const_p(char, "global:require_membership_of"), NULL
)
429 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
432 if (iniparser_getboolean(d
, discard_const_p(char, "global:try_first_pass"), false)) {
433 ctrl
|= WINBIND_TRY_FIRST_PASS_ARG
;
436 if (iniparser_getint(d
, discard_const_p(char, "global:warn_pwd_expire"), 0)) {
437 ctrl
|= WINBIND_WARN_PWD_EXPIRE
;
440 if (iniparser_getboolean(d
, discard_const_p(char, "global:mkhomedir"), false)) {
441 ctrl
|= WINBIND_MKHOMEDIR
;
445 /* step through arguments */
446 for (i
=argc
,v
=argv
; i
-- > 0; ++v
) {
448 /* generic options */
449 if (!strcmp(*v
,"debug"))
450 ctrl
|= WINBIND_DEBUG_ARG
;
451 else if (!strcasecmp(*v
, "debug_state"))
452 ctrl
|= WINBIND_DEBUG_STATE
;
453 else if (!strcasecmp(*v
, "silent"))
454 ctrl
|= WINBIND_SILENT
;
455 else if (!strcasecmp(*v
, "use_authtok"))
456 ctrl
|= WINBIND_USE_AUTHTOK_ARG
;
457 else if (!strcasecmp(*v
, "use_first_pass"))
458 ctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
459 else if (!strcasecmp(*v
, "try_first_pass"))
460 ctrl
|= WINBIND_TRY_FIRST_PASS_ARG
;
461 else if (!strcasecmp(*v
, "unknown_ok"))
462 ctrl
|= WINBIND_UNKNOWN_OK_ARG
;
463 else if (!strncasecmp(*v
, "require_membership_of",
464 strlen("require_membership_of")))
465 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
466 else if (!strncasecmp(*v
, "require-membership-of",
467 strlen("require-membership-of")))
468 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
469 else if (!strcasecmp(*v
, "krb5_auth"))
470 ctrl
|= WINBIND_KRB5_AUTH
;
471 else if (!strncasecmp(*v
, "krb5_ccache_type",
472 strlen("krb5_ccache_type")))
473 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
474 else if (!strcasecmp(*v
, "cached_login"))
475 ctrl
|= WINBIND_CACHED_LOGIN
;
476 else if (!strcasecmp(*v
, "mkhomedir"))
477 ctrl
|= WINBIND_MKHOMEDIR
;
479 __pam_log(pamh
, ctrl
, LOG_ERR
,
480 "pam_parse: unknown option: %s", *v
);
490 iniparser_freedict(d
);
497 static int _pam_winbind_free_context(struct pwb_context
*ctx
)
504 iniparser_freedict(ctx
->dict
);
510 static int _pam_winbind_init_context(pam_handle_t
*pamh
,
514 struct pwb_context
**ctx_p
)
516 struct pwb_context
*r
= NULL
;
522 r
= talloc_zero(NULL
, struct pwb_context
);
527 talloc_set_destructor(r
, _pam_winbind_free_context
);
533 r
->ctrl
= _pam_parse(pamh
, flags
, argc
, argv
, &r
->dict
);
536 return PAM_SYSTEM_ERR
;
544 static void _pam_winbind_cleanup_func(pam_handle_t
*pamh
,
548 int ctrl
= _pam_parse(pamh
, 0, 0, NULL
, NULL
);
549 if (_pam_log_is_debug_state_enabled(ctrl
)) {
550 __pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
551 "[pamh: %p] CLEAN: cleaning up PAM data %p "
552 "(error_status = %d)", pamh
, data
,
559 static const struct ntstatus_errors
{
560 const char *ntstatus_string
;
561 const char *error_string
;
562 } ntstatus_errors
[] = {
565 {"NT_STATUS_BACKUP_CONTROLLER",
566 N_("No primary Domain Controler available")},
567 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
568 N_("No domain controllers found")},
569 {"NT_STATUS_NO_LOGON_SERVERS",
570 N_("No logon servers")},
571 {"NT_STATUS_PWD_TOO_SHORT",
572 N_("Password too short")},
573 {"NT_STATUS_PWD_TOO_RECENT",
574 N_("The password of this user is too recent to change")},
575 {"NT_STATUS_PWD_HISTORY_CONFLICT",
576 N_("Password is already in password history")},
577 {"NT_STATUS_PASSWORD_EXPIRED",
578 N_("Your password has expired")},
579 {"NT_STATUS_PASSWORD_MUST_CHANGE",
580 N_("You need to change your password now")},
581 {"NT_STATUS_INVALID_WORKSTATION",
582 N_("You are not allowed to logon from this workstation")},
583 {"NT_STATUS_INVALID_LOGON_HOURS",
584 N_("You are not allowed to logon at this time")},
585 {"NT_STATUS_ACCOUNT_EXPIRED",
586 N_("Your account has expired. "
587 "Please contact your System administrator")}, /* SCNR */
588 {"NT_STATUS_ACCOUNT_DISABLED",
589 N_("Your account is disabled. "
590 "Please contact your System administrator")}, /* SCNR */
591 {"NT_STATUS_ACCOUNT_LOCKED_OUT",
592 N_("Your account has been locked. "
593 "Please contact your System administrator")}, /* SCNR */
594 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
595 N_("Invalid Trust Account")},
596 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
597 N_("Invalid Trust Account")},
598 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
599 N_("Invalid Trust Account")},
600 {"NT_STATUS_ACCESS_DENIED",
601 N_("Access is denied")},
605 static const char *_get_ntstatus_error_string(const char *nt_status_string
)
608 for (i
=0; ntstatus_errors
[i
].ntstatus_string
!= NULL
; i
++) {
609 if (!strcasecmp(ntstatus_errors
[i
].ntstatus_string
,
611 return _(ntstatus_errors
[i
].error_string
);
617 /* --- authentication management functions --- */
619 /* Attempt a conversation */
621 static int converse(const pam_handle_t
*pamh
,
623 struct pam_message
**message
,
624 struct pam_response
**response
)
627 struct pam_conv
*conv
;
629 retval
= pam_get_item(pamh
, PAM_CONV
, (const void **) &conv
);
630 if (retval
== PAM_SUCCESS
) {
631 retval
= conv
->conv(nargs
,
632 (const struct pam_message
**)message
,
633 response
, conv
->appdata_ptr
);
636 return retval
; /* propagate error status */
640 static int _make_remark(struct pwb_context
*ctx
,
644 int retval
= PAM_SUCCESS
;
646 struct pam_message
*pmsg
[1], msg
[1];
647 struct pam_response
*resp
;
649 if (ctx
->flags
& WINBIND_SILENT
) {
654 msg
[0].msg
= discard_const_p(char, text
);
655 msg
[0].msg_style
= type
;
658 retval
= converse(ctx
->pamh
, 1, pmsg
, &resp
);
661 _pam_drop_reply(resp
, 1);
666 static int _make_remark_v(struct pwb_context
*ctx
,
674 ret
= vasprintf(&var
, format
, args
);
676 _pam_log(ctx
, LOG_ERR
, "memory allocation failure");
680 ret
= _make_remark(ctx
, type
, var
);
685 static int _make_remark_format(struct pwb_context
*ctx
, int type
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
686 static int _make_remark_format(struct pwb_context
*ctx
, int type
, const char *format
, ...)
691 va_start(args
, format
);
692 ret
= _make_remark_v(ctx
, type
, format
, args
);
697 static int pam_winbind_request_log(struct pwb_context
*ctx
,
704 /* incorrect password */
705 _pam_log(ctx
, LOG_WARNING
, "user '%s' denied access "
706 "(incorrect password or invalid membership)", user
);
708 case PAM_ACCT_EXPIRED
:
709 /* account expired */
710 _pam_log(ctx
, LOG_WARNING
, "user '%s' account expired",
713 case PAM_AUTHTOK_EXPIRED
:
714 /* password expired */
715 _pam_log(ctx
, LOG_WARNING
, "user '%s' password expired",
718 case PAM_NEW_AUTHTOK_REQD
:
719 /* new password required */
720 _pam_log(ctx
, LOG_WARNING
, "user '%s' new password "
723 case PAM_USER_UNKNOWN
:
724 /* the user does not exist */
725 _pam_log_debug(ctx
, LOG_NOTICE
, "user '%s' not found",
727 if (ctx
->ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
732 /* Otherwise, the authentication looked good */
733 if (strcmp(fn
, "wbcLogonUser") == 0) {
734 _pam_log(ctx
, LOG_NOTICE
,
735 "user '%s' granted access", user
);
737 _pam_log(ctx
, LOG_NOTICE
,
738 "user '%s' OK", user
);
742 /* we don't know anything about this return value */
743 _pam_log(ctx
, LOG_ERR
,
744 "internal module error (retval = %s(%d), user = '%s')",
745 _pam_error_code_str(retval
), retval
, user
);
750 static int wbc_auth_error_to_pam_error(struct pwb_context
*ctx
,
751 struct wbcAuthErrorInfo
*e
,
753 const char *username
,
756 int ret
= PAM_AUTH_ERR
;
758 if (WBC_ERROR_IS_OK(status
)) {
759 _pam_log_debug(ctx
, LOG_DEBUG
, "request %s succeeded",
762 return pam_winbind_request_log(ctx
, ret
, username
, fn
);
766 if (e
->pam_error
!= PAM_SUCCESS
) {
767 _pam_log(ctx
, LOG_ERR
,
768 "request %s failed: %s, "
769 "PAM error: %s (%d), NTSTATUS: %s, "
770 "Error message was: %s",
772 wbcErrorString(status
),
773 _pam_error_code_str(e
->pam_error
),
778 return pam_winbind_request_log(ctx
, ret
, username
, fn
);
781 _pam_log(ctx
, LOG_ERR
, "request %s failed, but PAM error 0!", fn
);
783 ret
= PAM_SERVICE_ERR
;
784 return pam_winbind_request_log(ctx
, ret
, username
, fn
);
787 ret
= wbc_error_to_pam_error(status
);
788 return pam_winbind_request_log(ctx
, ret
, username
, fn
);
791 #if defined(HAVE_PAM_RADIO_TYPE)
792 static bool _pam_winbind_change_pwd(struct pwb_context
*ctx
)
794 struct pam_message msg
, *pmsg
;
795 struct pam_response
*resp
= NULL
;
799 msg
.msg_style
= PAM_RADIO_TYPE
;
800 msg
.msg
= _("Do you want to change your password now?");
801 ret
= converse(ctx
->pamh
, 1, &pmsg
, &resp
);
803 if (ret
== PAM_SUCCESS
) {
804 _pam_log(ctx
, LOG_CRIT
, "pam_winbind: system error!\n");
808 if (ret
!= PAM_SUCCESS
) {
811 _pam_log(ctx
, LOG_CRIT
, "Received [%s] reply from application.\n", resp
->resp
);
813 if ((resp
->resp
!= NULL
) && (strcasecmp(resp
->resp
, "yes") == 0)) {
817 _pam_drop_reply(resp
, 1);
821 static bool _pam_winbind_change_pwd(struct pwb_context
*ctx
)
828 * send a password expiry message if required
830 * @param ctx PAM winbind context.
831 * @param next_change expected (calculated) next expiry date.
832 * @param already_expired pointer to a boolean to indicate if the password is
835 * @return boolean Returns true if message has been sent, false if not.
838 static bool _pam_send_password_expiry_message(struct pwb_context
*ctx
,
842 bool *already_expired
,
846 struct tm tm_now
, tm_next_change
;
850 if (already_expired
) {
851 *already_expired
= false;
858 if (next_change
<= now
) {
859 PAM_WB_REMARK_DIRECT(ctx
, "NT_STATUS_PASSWORD_EXPIRED");
860 if (already_expired
) {
861 *already_expired
= true;
866 if ((next_change
< 0) ||
867 (next_change
> now
+ warn_pwd_expire
* SECONDS_PER_DAY
)) {
871 if ((localtime_r(&now
, &tm_now
) == NULL
) ||
872 (localtime_r(&next_change
, &tm_next_change
) == NULL
)) {
876 days
= (tm_next_change
.tm_yday
+tm_next_change
.tm_year
*365) -
877 (tm_now
.tm_yday
+tm_now
.tm_year
*365);
880 ret
= _make_remark(ctx
, PAM_TEXT_INFO
,
881 _("Your password expires today.\n"));
884 * If change_pwd and already_expired is null.
885 * We are just sending a notification message.
886 * We don't expect any response in this case.
889 if (!change_pwd
&& !already_expired
) {
894 * successfully sent the warning message.
895 * Give the user a chance to change pwd.
897 if (ret
== PAM_SUCCESS
) {
899 retval
= _pam_winbind_change_pwd(ctx
);
908 if (days
> 0 && days
< warn_pwd_expire
) {
910 ret
= _make_remark_format(ctx
, PAM_TEXT_INFO
,
911 _("Your password will expire in %d %s.\n"),
912 days
, (days
> 1) ? _("days"):_("day"));
914 * If change_pwd and already_expired is null.
915 * We are just sending a notification message.
916 * We don't expect any response in this case.
919 if (!change_pwd
&& !already_expired
) {
924 * successfully sent the warning message.
925 * Give the user a chance to change pwd.
927 if (ret
== PAM_SUCCESS
) {
929 retval
= _pam_winbind_change_pwd(ctx
);
942 * Send a warning if the password expires in the near future
944 * @param ctx PAM winbind context.
945 * @param response The full authentication response structure.
946 * @param already_expired boolean, is the pwd already expired?
951 static void _pam_warn_password_expiry(struct pwb_context
*ctx
,
952 const struct wbcAuthUserInfo
*info
,
953 const struct wbcUserPasswordPolicyInfo
*policy
,
955 bool *already_expired
,
958 time_t now
= time(NULL
);
959 time_t next_change
= 0;
961 if (!info
|| !policy
) {
965 if (already_expired
) {
966 *already_expired
= false;
973 /* accounts with WBC_ACB_PWNOEXP set never receive a warning */
974 if (info
->acct_flags
& WBC_ACB_PWNOEXP
) {
978 /* no point in sending a warning if this is a grace logon */
979 if (PAM_WB_GRACE_LOGON(info
->user_flags
)) {
983 /* check if the info3 must change timestamp has been set */
984 next_change
= info
->pass_must_change_time
;
986 if (_pam_send_password_expiry_message(ctx
, next_change
, now
,
993 /* now check for the global password policy */
994 /* good catch from Ralf Haferkamp: an expiry of "never" is translated
996 if ((policy
->expire
== (int64_t)-1) ||
997 (policy
->expire
== 0)) {
1001 next_change
= info
->pass_last_set_time
+ policy
->expire
;
1003 if (_pam_send_password_expiry_message(ctx
, next_change
, now
,
1010 /* no warning sent */
1013 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
1016 * Append a string, making sure not to overflow and to always return a
1017 * NULL-terminated string.
1019 * @param dest Destination string buffer (must already be NULL-terminated).
1020 * @param src Source string buffer.
1021 * @param dest_buffer_size Size of dest buffer in bytes.
1023 * @return false if dest buffer is not big enough (no bytes copied), true on
1027 static bool safe_append_string(char *dest
,
1029 int dest_buffer_size
)
1031 int dest_length
= strlen(dest
);
1032 int src_length
= strlen(src
);
1034 if (dest_length
+ src_length
+ 1 > dest_buffer_size
) {
1038 memcpy(dest
+ dest_length
, src
, src_length
+ 1);
1043 * Convert a names into a SID string, appending it to a buffer.
1045 * @param ctx PAM winbind context.
1046 * @param user User in PAM request.
1047 * @param name Name to convert.
1048 * @param sid_list_buffer Where to append the string sid.
1049 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1051 * @return false on failure, true on success.
1053 static bool winbind_name_to_sid_string(struct pwb_context
*ctx
,
1056 char *sid_list_buffer
,
1057 int sid_list_buffer_size
)
1059 char sid_string
[WBC_SID_STRING_BUFLEN
];
1062 if (IS_SID_STRING(name
)) {
1063 strlcpy(sid_string
, name
, sizeof(sid_string
));
1066 struct wbcDomainSid sid
;
1067 enum wbcSidType type
;
1069 _pam_log_debug(ctx
, LOG_DEBUG
,
1070 "no sid given, looking up: %s\n", name
);
1072 wbc_status
= wbcLookupName("", name
, &sid
, &type
);
1073 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1074 _pam_log(ctx
, LOG_INFO
,
1075 "could not lookup name: %s\n", name
);
1079 wbcSidToStringBuf(&sid
, sid_string
, sizeof(sid_string
));
1082 if (!safe_append_string(sid_list_buffer
, sid_string
,
1083 sid_list_buffer_size
)) {
1090 * Convert a list of names into a list of sids.
1092 * @param ctx PAM winbind context.
1093 * @param user User in PAM request.
1094 * @param name_list List of names or string sids, separated by commas.
1095 * @param sid_list_buffer Where to put the list of string sids.
1096 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1098 * @return false on failure, true on success.
1100 static bool winbind_name_list_to_sid_string_list(struct pwb_context
*ctx
,
1102 const char *name_list
,
1103 char *sid_list_buffer
,
1104 int sid_list_buffer_size
)
1106 bool result
= false;
1107 char *current_name
= NULL
;
1108 const char *search_location
;
1112 if (sid_list_buffer_size
> 0) {
1113 sid_list_buffer
[0] = 0;
1116 search_location
= name_list
;
1117 while ((comma
= strchr(search_location
, ',')) != NULL
) {
1118 current_name
= strndup(search_location
,
1119 comma
- search_location
);
1120 if (NULL
== current_name
) {
1124 if (!winbind_name_to_sid_string(ctx
, user
,
1127 sid_list_buffer_size
)) {
1129 * If one group name failed, we must not fail
1130 * the authentication totally, continue with
1131 * the following group names. If user belongs to
1132 * one of the valid groups, we must allow it
1136 _pam_log(ctx
, LOG_INFO
, "cannot convert group %s to sid, "
1137 "check if group %s is valid group.", current_name
,
1139 _make_remark_format(ctx
, PAM_TEXT_INFO
, _("Cannot convert group %s "
1140 "to sid, please contact your administrator to see "
1141 "if group %s is valid."), current_name
, current_name
);
1142 SAFE_FREE(current_name
);
1143 search_location
= comma
+ 1;
1147 SAFE_FREE(current_name
);
1149 if (!safe_append_string(sid_list_buffer
, ",",
1150 sid_list_buffer_size
)) {
1154 search_location
= comma
+ 1;
1157 if (!winbind_name_to_sid_string(ctx
, user
, search_location
,
1159 sid_list_buffer_size
)) {
1160 _pam_log(ctx
, LOG_INFO
, "cannot convert group %s to sid, "
1161 "check if group %s is valid group.", search_location
,
1163 _make_remark_format(ctx
, PAM_TEXT_INFO
, _("Cannot convert group %s "
1164 "to sid, please contact your administrator to see "
1165 "if group %s is valid."), search_location
, search_location
);
1167 * The lookup of the last name failed..
1168 * It results in require_member_of_sid ends with ','
1169 * It is malformated parameter here, overwrite the last ','.
1171 len
= strlen(sid_list_buffer
);
1172 if ((len
!= 0) && (sid_list_buffer
[len
- 1] == ',')) {
1173 sid_list_buffer
[len
- 1] = '\0';
1180 SAFE_FREE(current_name
);
1185 * put krb5ccname variable into environment
1187 * @param ctx PAM winbind context.
1188 * @param krb5ccname env variable retrieved from winbindd.
1193 static void _pam_setup_krb5_env(struct pwb_context
*ctx
,
1194 struct wbcLogonUserInfo
*info
)
1199 const char *krb5ccname
= NULL
;
1201 if (off(ctx
->ctrl
, WINBIND_KRB5_AUTH
)) {
1209 for (i
=0; i
< info
->num_blobs
; i
++) {
1210 if (strcasecmp(info
->blobs
[i
].name
, "krb5ccname") == 0) {
1211 krb5ccname
= (const char *)info
->blobs
[i
].blob
.data
;
1216 if (!krb5ccname
|| (strlen(krb5ccname
) == 0)) {
1220 _pam_log_debug(ctx
, LOG_DEBUG
,
1221 "request returned KRB5CCNAME: %s", krb5ccname
);
1223 if (asprintf(&var
, "KRB5CCNAME=%s", krb5ccname
) == -1) {
1227 ret
= pam_putenv(ctx
->pamh
, var
);
1229 _pam_log(ctx
, LOG_ERR
,
1230 "failed to set KRB5CCNAME to %s: %s",
1231 var
, pam_strerror(ctx
->pamh
, ret
));
1237 * Copy unix username if available (further processed in PAM).
1239 * @param ctx PAM winbind context
1240 * @param user_ret A pointer that holds a pointer to a string
1241 * @param unix_username A username
1246 static void _pam_setup_unix_username(struct pwb_context
*ctx
,
1248 struct wbcLogonUserInfo
*info
)
1250 const char *unix_username
= NULL
;
1253 if (!user_ret
|| !info
) {
1257 for (i
=0; i
< info
->num_blobs
; i
++) {
1258 if (strcasecmp(info
->blobs
[i
].name
, "unix_username") == 0) {
1259 unix_username
= (const char *)info
->blobs
[i
].blob
.data
;
1264 if (!unix_username
|| !unix_username
[0]) {
1268 *user_ret
= strdup(unix_username
);
1272 * Set string into the PAM stack.
1274 * @param ctx PAM winbind context.
1275 * @param data_name Key name for pam_set_data.
1276 * @param value String value.
1281 static void _pam_set_data_string(struct pwb_context
*ctx
,
1282 const char *data_name
,
1287 if (!data_name
|| !value
|| (strlen(data_name
) == 0) ||
1288 (strlen(value
) == 0)) {
1292 ret
= pam_set_data(ctx
->pamh
, data_name
, talloc_strdup(NULL
, value
),
1293 _pam_winbind_cleanup_func
);
1295 _pam_log_debug(ctx
, LOG_DEBUG
,
1296 "Could not set data %s: %s\n",
1297 data_name
, pam_strerror(ctx
->pamh
, ret
));
1302 * Set info3 strings into the PAM stack.
1304 * @param ctx PAM winbind context.
1305 * @param data_name Key name for pam_set_data.
1306 * @param value String value.
1311 static void _pam_set_data_info3(struct pwb_context
*ctx
,
1312 const struct wbcAuthUserInfo
*info
)
1314 _pam_set_data_string(ctx
, PAM_WINBIND_HOMEDIR
,
1315 info
->home_directory
);
1316 _pam_set_data_string(ctx
, PAM_WINBIND_LOGONSCRIPT
,
1317 info
->logon_script
);
1318 _pam_set_data_string(ctx
, PAM_WINBIND_LOGONSERVER
,
1319 info
->logon_server
);
1320 _pam_set_data_string(ctx
, PAM_WINBIND_PROFILEPATH
,
1321 info
->profile_path
);
1325 * Free info3 strings in the PAM stack.
1327 * @param pamh PAM handle
1332 static void _pam_free_data_info3(pam_handle_t
*pamh
)
1334 pam_set_data(pamh
, PAM_WINBIND_HOMEDIR
, NULL
, NULL
);
1335 pam_set_data(pamh
, PAM_WINBIND_LOGONSCRIPT
, NULL
, NULL
);
1336 pam_set_data(pamh
, PAM_WINBIND_LOGONSERVER
, NULL
, NULL
);
1337 pam_set_data(pamh
, PAM_WINBIND_PROFILEPATH
, NULL
, NULL
);
1341 * Send PAM_ERROR_MSG for cached or grace logons.
1343 * @param ctx PAM winbind context.
1344 * @param username User in PAM request.
1345 * @param info3_user_flgs Info3 flags containing logon type bits.
1350 static void _pam_warn_logon_type(struct pwb_context
*ctx
,
1351 const char *username
,
1352 uint32_t info3_user_flgs
)
1354 /* inform about logon type */
1355 if (PAM_WB_GRACE_LOGON(info3_user_flgs
)) {
1357 _make_remark(ctx
, PAM_ERROR_MSG
,
1359 "Please change your password as soon you're "
1361 _pam_log_debug(ctx
, LOG_DEBUG
,
1362 "User %s logged on using grace logon\n",
1365 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs
)) {
1367 _make_remark(ctx
, PAM_ERROR_MSG
,
1368 _("Domain Controller unreachable, "
1369 "using cached credentials instead. "
1370 "Network resources may be unavailable"));
1371 _pam_log_debug(ctx
, LOG_DEBUG
,
1372 "User %s logged on using cached credentials\n",
1378 * Send PAM_ERROR_MSG for krb5 errors.
1380 * @param ctx PAM winbind context.
1381 * @param username User in PAM request.
1382 * @param info3_user_flgs Info3 flags containing logon type bits.
1387 static void _pam_warn_krb5_failure(struct pwb_context
*ctx
,
1388 const char *username
,
1389 uint32_t info3_user_flgs
)
1391 if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs
)) {
1392 _make_remark(ctx
, PAM_ERROR_MSG
,
1393 _("Failed to establish your Kerberos Ticket cache "
1394 "due time differences\n"
1395 "with the domain controller. "
1396 "Please verify the system time.\n"));
1397 _pam_log_debug(ctx
, LOG_DEBUG
,
1398 "User %s: Clock skew when getting Krb5 TGT\n",
1403 static bool _pam_check_remark_auth_err(struct pwb_context
*ctx
,
1404 const struct wbcAuthErrorInfo
*e
,
1405 const char *nt_status_string
,
1408 const char *ntstatus
= NULL
;
1409 const char *error_string
= NULL
;
1411 if (!e
|| !pam_err
) {
1415 ntstatus
= e
->nt_string
;
1420 if (strcasecmp(ntstatus
, nt_status_string
) == 0) {
1422 error_string
= _get_ntstatus_error_string(nt_status_string
);
1424 _make_remark(ctx
, PAM_ERROR_MSG
, error_string
);
1425 *pam_err
= e
->pam_error
;
1429 if (e
->display_string
) {
1430 _make_remark(ctx
, PAM_ERROR_MSG
, _(e
->display_string
));
1431 *pam_err
= e
->pam_error
;
1435 _make_remark(ctx
, PAM_ERROR_MSG
, nt_status_string
);
1436 *pam_err
= e
->pam_error
;
1445 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
1447 * @param i The wbcUserPasswordPolicyInfo struct.
1449 * @return string (caller needs to talloc_free).
1452 static char *_pam_compose_pwd_restriction_string(struct pwb_context
*ctx
,
1453 struct wbcUserPasswordPolicyInfo
*i
)
1461 str
= talloc_asprintf(ctx
, _("Your password "));
1466 if (i
->min_length_password
> 0) {
1467 str
= talloc_asprintf_append(str
,
1468 _("must be at least %d characters; "),
1469 i
->min_length_password
);
1475 if (i
->password_history
> 0) {
1476 str
= talloc_asprintf_append(str
,
1477 _("cannot repeat any of your previous %d "
1479 i
->password_history
);
1485 if (i
->password_properties
& WBC_DOMAIN_PASSWORD_COMPLEX
) {
1486 str
= talloc_asprintf_append(str
,
1487 _("must contain capitals, numerals "
1489 "and cannot contain your account "
1496 str
= talloc_asprintf_append(str
,
1497 _("Please type a different password. "
1498 "Type a password which meets these requirements in "
1499 "both text boxes."));
1511 static int _pam_create_homedir(struct pwb_context
*ctx
,
1512 const char *dirname
,
1517 if (stat(dirname
, &sbuf
) == 0) {
1521 if (mkdir(dirname
, mode
) != 0) {
1523 _make_remark_format(ctx
, PAM_TEXT_INFO
,
1524 _("Creating directory: %s failed: %s"),
1525 dirname
, strerror(errno
));
1526 _pam_log(ctx
, LOG_ERR
, "could not create dir: %s (%s)",
1527 dirname
, strerror(errno
));
1528 return PAM_PERM_DENIED
;
1534 static int _pam_chown_homedir(struct pwb_context
*ctx
,
1535 const char *dirname
,
1539 if (chown(dirname
, uid
, gid
) != 0) {
1540 _pam_log(ctx
, LOG_ERR
, "failed to chown user homedir: %s (%s)",
1541 dirname
, strerror(errno
));
1542 return PAM_PERM_DENIED
;
1548 static int _pam_mkhomedir(struct pwb_context
*ctx
)
1550 struct passwd
*pwd
= NULL
;
1552 char *create_dir
= NULL
;
1553 char *user_dir
= NULL
;
1555 const char *username
;
1557 char *safe_ptr
= NULL
;
1560 /* Get the username */
1561 ret
= pam_get_user(ctx
->pamh
, &username
, NULL
);
1562 if ((ret
!= PAM_SUCCESS
) || (!username
)) {
1563 _pam_log_debug(ctx
, LOG_DEBUG
, "can not get the username");
1564 return PAM_SERVICE_ERR
;
1567 pwd
= getpwnam(username
);
1569 _pam_log_debug(ctx
, LOG_DEBUG
, "can not get the username");
1570 return PAM_USER_UNKNOWN
;
1572 _pam_log_debug(ctx
, LOG_DEBUG
, "homedir is: %s", pwd
->pw_dir
);
1574 ret
= _pam_create_homedir(ctx
, pwd
->pw_dir
, 0700);
1575 if (ret
== PAM_SUCCESS
) {
1576 ret
= _pam_chown_homedir(ctx
, pwd
->pw_dir
,
1581 if (ret
== PAM_SUCCESS
) {
1585 /* maybe we need to create parent dirs */
1586 create_dir
= talloc_strdup(ctx
, "/");
1591 /* find final directory */
1592 user_dir
= strrchr(pwd
->pw_dir
, '/');
1598 _pam_log(ctx
, LOG_DEBUG
, "final directory: %s", user_dir
);
1602 while ((token
= strtok_r(p
, "/", &safe_ptr
)) != NULL
) {
1608 _pam_log_debug(ctx
, LOG_DEBUG
, "token is %s", token
);
1610 create_dir
= talloc_asprintf_append(create_dir
, "%s/", token
);
1614 _pam_log_debug(ctx
, LOG_DEBUG
, "current_dir is %s", create_dir
);
1616 if (strcmp(token
, user_dir
) == 0) {
1617 _pam_log_debug(ctx
, LOG_DEBUG
, "assuming last directory: %s", token
);
1621 ret
= _pam_create_homedir(ctx
, create_dir
, mode
);
1627 return _pam_chown_homedir(ctx
, create_dir
,
1632 /* talk to winbindd */
1633 static int winbind_auth_request(struct pwb_context
*ctx
,
1638 const int warn_pwd_expire
,
1639 struct wbcAuthErrorInfo
**p_error
,
1640 struct wbcLogonUserInfo
**p_info
,
1641 struct wbcUserPasswordPolicyInfo
**p_policy
,
1642 time_t *pwd_last_set
,
1647 struct wbcLogonUserParams logon
;
1648 char membership_of
[1024];
1649 uid_t user_uid
= -1;
1650 uint32_t flags
= WBFLAG_PAM_INFO3_TEXT
|
1651 WBFLAG_PAM_GET_PWD_POLICY
;
1653 struct wbcLogonUserInfo
*info
= NULL
;
1654 struct wbcAuthUserInfo
*user_info
= NULL
;
1655 struct wbcAuthErrorInfo
*error
= NULL
;
1656 struct wbcUserPasswordPolicyInfo
*policy
= NULL
;
1658 int ret
= PAM_AUTH_ERR
;
1660 const char *codes
[] = {
1661 "NT_STATUS_PASSWORD_EXPIRED",
1662 "NT_STATUS_PASSWORD_MUST_CHANGE",
1663 "NT_STATUS_INVALID_WORKSTATION",
1664 "NT_STATUS_INVALID_LOGON_HOURS",
1665 "NT_STATUS_ACCOUNT_EXPIRED",
1666 "NT_STATUS_ACCOUNT_DISABLED",
1667 "NT_STATUS_ACCOUNT_LOCKED_OUT",
1668 "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
1669 "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
1670 "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
1671 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1672 "NT_STATUS_NO_LOGON_SERVERS",
1673 "NT_STATUS_WRONG_PASSWORD",
1674 "NT_STATUS_ACCESS_DENIED"
1681 /* Krb5 auth always has to go against the KDC of the user's realm */
1683 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1684 flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
1687 if (ctx
->ctrl
& (WINBIND_KRB5_AUTH
|WINBIND_CACHED_LOGIN
)) {
1688 struct passwd
*pwd
= NULL
;
1690 pwd
= getpwnam(user
);
1692 return PAM_USER_UNKNOWN
;
1694 user_uid
= pwd
->pw_uid
;
1697 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1699 _pam_log_debug(ctx
, LOG_DEBUG
,
1700 "enabling krb5 login flag\n");
1702 flags
|= WBFLAG_PAM_KRB5
|
1703 WBFLAG_PAM_FALLBACK_AFTER_KRB5
;
1706 if (ctx
->ctrl
& WINBIND_CACHED_LOGIN
) {
1707 _pam_log_debug(ctx
, LOG_DEBUG
,
1708 "enabling cached login flag\n");
1709 flags
|= WBFLAG_PAM_CACHED_LOGIN
;
1714 flags
|= WBFLAG_PAM_UNIX_NAME
;
1717 if (cctype
!= NULL
) {
1718 _pam_log_debug(ctx
, LOG_DEBUG
,
1719 "enabling request for a %s krb5 ccache\n",
1723 if (member
!= NULL
) {
1725 ZERO_STRUCT(membership_of
);
1727 if (!winbind_name_list_to_sid_string_list(ctx
, user
, member
,
1729 sizeof(membership_of
))) {
1730 _pam_log_debug(ctx
, LOG_ERR
,
1731 "failed to serialize membership of sid "
1732 "\"%s\"\n", member
);
1733 return PAM_AUTH_ERR
;
1739 logon
.username
= user
;
1740 logon
.password
= pass
;
1743 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1747 discard_const_p(uint8_t, cctype
),
1749 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1754 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1760 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1764 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1768 (uint8_t *)&user_uid
,
1770 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1775 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1779 (uint8_t *)membership_of
,
1780 sizeof(membership_of
));
1781 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1786 wbc_status
= wbcLogonUser(&logon
, &info
, &error
, &policy
);
1787 ret
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
1788 user
, "wbcLogonUser");
1789 wbcFreeMemory(logon
.blobs
);
1792 if (info
&& info
->info
) {
1793 user_info
= info
->info
;
1796 if (pwd_last_set
&& user_info
) {
1797 *pwd_last_set
= user_info
->pass_last_set_time
;
1800 if (p_info
&& info
) {
1804 if (p_policy
&& policy
) {
1808 if (p_error
&& error
) {
1809 /* We want to process the error in the caller. */
1814 for (i
=0; i
<ARRAY_SIZE(codes
); i
++) {
1816 if (_pam_check_remark_auth_err(ctx
, error
, codes
[i
], &_ret
)) {
1822 if ((ret
== PAM_SUCCESS
) && user_info
&& policy
&& info
) {
1824 bool already_expired
= false;
1825 bool change_pwd
= false;
1827 /* warn a user if the password is about to expire soon */
1828 _pam_warn_password_expiry(ctx
, user_info
, policy
,
1833 if (already_expired
== true) {
1835 SMB_TIME_T last_set
= user_info
->pass_last_set_time
;
1837 _pam_log_debug(ctx
, LOG_DEBUG
,
1838 "Password has expired "
1839 "(Password was last set: %lld, "
1840 "the policy says it should expire here "
1841 "%lld (now it's: %ld))\n",
1842 (long long int)last_set
,
1843 (long long int)last_set
+
1847 return PAM_AUTHTOK_EXPIRED
;
1851 ret
= PAM_NEW_AUTHTOK_REQD
;
1855 /* inform about logon type */
1856 _pam_warn_logon_type(ctx
, user
, user_info
->user_flags
);
1858 /* inform about krb5 failures */
1859 _pam_warn_krb5_failure(ctx
, user
, user_info
->user_flags
);
1861 /* set some info3 info for other modules in the stack */
1862 _pam_set_data_info3(ctx
, user_info
);
1864 /* put krb5ccname into env */
1865 _pam_setup_krb5_env(ctx
, info
);
1867 /* If winbindd returned a username, return the pointer to it
1869 _pam_setup_unix_username(ctx
, user_ret
, info
);
1873 wbcFreeMemory(logon
.blobs
);
1874 if (info
&& info
->blobs
&& !p_info
) {
1875 wbcFreeMemory(info
->blobs
);
1877 if (error
&& !p_error
) {
1878 wbcFreeMemory(error
);
1880 if (info
&& !p_info
) {
1881 wbcFreeMemory(info
);
1883 if (policy
&& !p_policy
) {
1884 wbcFreeMemory(policy
);
1890 /* talk to winbindd */
1891 static int winbind_chauthtok_request(struct pwb_context
*ctx
,
1893 const char *oldpass
,
1894 const char *newpass
,
1895 time_t pwd_last_set
)
1898 struct wbcChangePasswordParams params
;
1899 struct wbcAuthErrorInfo
*error
= NULL
;
1900 struct wbcUserPasswordPolicyInfo
*policy
= NULL
;
1901 enum wbcPasswordChangeRejectReason reject_reason
= -1;
1905 const char *codes
[] = {
1906 "NT_STATUS_BACKUP_CONTROLLER",
1907 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1908 "NT_STATUS_NO_LOGON_SERVERS",
1909 "NT_STATUS_ACCESS_DENIED",
1910 "NT_STATUS_PWD_TOO_SHORT", /* TODO: tell the min pwd length ? */
1911 "NT_STATUS_PWD_TOO_RECENT", /* TODO: tell the minage ? */
1912 "NT_STATUS_PWD_HISTORY_CONFLICT" /* TODO: tell the history length ? */
1914 int ret
= PAM_AUTH_ERR
;
1916 ZERO_STRUCT(params
);
1918 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1919 flags
|= WBFLAG_PAM_KRB5
|
1920 WBFLAG_PAM_CONTACT_TRUSTDOM
;
1923 if (ctx
->ctrl
& WINBIND_CACHED_LOGIN
) {
1924 flags
|= WBFLAG_PAM_CACHED_LOGIN
;
1927 params
.account_name
= user
;
1928 params
.level
= WBC_CHANGE_PASSWORD_LEVEL_PLAIN
;
1929 params
.old_password
.plaintext
= oldpass
;
1930 params
.new_password
.plaintext
= newpass
;
1931 params
.flags
= flags
;
1933 wbc_status
= wbcChangeUserPasswordEx(¶ms
, &error
, &reject_reason
, &policy
);
1934 ret
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
1935 user
, "wbcChangeUserPasswordEx");
1937 if (WBC_ERROR_IS_OK(wbc_status
)) {
1938 _pam_log(ctx
, LOG_NOTICE
,
1939 "user '%s' password changed", user
);
1944 wbcFreeMemory(policy
);
1948 for (i
=0; i
<ARRAY_SIZE(codes
); i
++) {
1950 if (_pam_check_remark_auth_err(ctx
, error
, codes
[i
], &_ret
)) {
1956 if (!strcasecmp(error
->nt_string
,
1957 "NT_STATUS_PASSWORD_RESTRICTION")) {
1959 char *pwd_restriction_string
= NULL
;
1960 SMB_TIME_T min_pwd_age
= 0;
1963 min_pwd_age
= policy
->min_passwordage
;
1966 /* FIXME: avoid to send multiple PAM messages after another */
1967 switch (reject_reason
) {
1970 case WBC_PWD_CHANGE_NO_ERROR
:
1971 if ((min_pwd_age
> 0) &&
1972 (pwd_last_set
+ min_pwd_age
> time(NULL
))) {
1973 PAM_WB_REMARK_DIRECT(ctx
,
1974 "NT_STATUS_PWD_TOO_RECENT");
1977 case WBC_PWD_CHANGE_PASSWORD_TOO_SHORT
:
1978 PAM_WB_REMARK_DIRECT(ctx
,
1979 "NT_STATUS_PWD_TOO_SHORT");
1981 case WBC_PWD_CHANGE_PWD_IN_HISTORY
:
1982 PAM_WB_REMARK_DIRECT(ctx
,
1983 "NT_STATUS_PWD_HISTORY_CONFLICT");
1985 case WBC_PWD_CHANGE_NOT_COMPLEX
:
1986 _make_remark(ctx
, PAM_ERROR_MSG
,
1987 _("Password does not meet "
1988 "complexity requirements"));
1991 _pam_log_debug(ctx
, LOG_DEBUG
,
1992 "unknown password change "
1993 "reject reason: %d",
1998 pwd_restriction_string
=
1999 _pam_compose_pwd_restriction_string(ctx
, policy
);
2000 if (pwd_restriction_string
) {
2001 _make_remark(ctx
, PAM_ERROR_MSG
,
2002 pwd_restriction_string
);
2003 TALLOC_FREE(pwd_restriction_string
);
2007 wbcFreeMemory(error
);
2008 wbcFreeMemory(policy
);
2014 * Checks if a user has an account
2017 * 1 = User not found
2021 static int valid_user(struct pwb_context
*ctx
,
2024 /* check not only if the user is available over NSS calls, also make
2025 * sure it's really a winbind user, this is important when stacking PAM
2026 * modules in the 'account' or 'password' facility. */
2029 struct passwd
*pwd
= NULL
;
2030 struct passwd
*wb_pwd
= NULL
;
2032 pwd
= getpwnam(user
);
2037 wbc_status
= wbcGetpwnam(user
, &wb_pwd
);
2038 wbcFreeMemory(wb_pwd
);
2039 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2040 _pam_log(ctx
, LOG_DEBUG
, "valid_user: wbcGetpwnam gave %s\n",
2041 wbcErrorString(wbc_status
));
2044 switch (wbc_status
) {
2045 case WBC_ERR_UNKNOWN_USER
:
2046 /* match other insane libwbclient return codes */
2047 case WBC_ERR_WINBIND_NOT_AVAILABLE
:
2048 case WBC_ERR_DOMAIN_NOT_FOUND
:
2050 case WBC_ERR_SUCCESS
:
2058 static char *_pam_delete(register char *xx
)
2066 * obtain a password from the user
2069 static int _winbind_read_password(struct pwb_context
*ctx
,
2071 const char *comment
,
2072 const char *prompt1
,
2073 const char *prompt2
,
2081 _pam_log(ctx
, LOG_DEBUG
, "getting password (0x%08x)", ctrl
);
2084 * make sure nothing inappropriate gets returned
2087 *pass
= token
= NULL
;
2090 * which authentication token are we getting?
2093 if (on(WINBIND__OLD_PASSWORD
, ctrl
)) {
2094 authtok_flag
= PAM_OLDAUTHTOK
;
2096 authtok_flag
= PAM_AUTHTOK
;
2100 * should we obtain the password from a PAM item ?
2103 if (on(WINBIND_TRY_FIRST_PASS_ARG
, ctrl
) ||
2104 on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
2105 retval
= pam_get_item(ctx
->pamh
,
2107 (const void **) &item
);
2108 if (retval
!= PAM_SUCCESS
) {
2110 _pam_log(ctx
, LOG_ALERT
,
2111 "pam_get_item returned error "
2112 "to unix-read-password");
2114 } else if (item
!= NULL
) { /* we have a password! */
2117 _pam_log(ctx
, LOG_DEBUG
,
2118 "pam_get_item returned a password");
2120 } else if (on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
2121 return PAM_AUTHTOK_RECOVER_ERR
; /* didn't work */
2122 } else if (on(WINBIND_USE_AUTHTOK_ARG
, ctrl
)
2123 && off(WINBIND__OLD_PASSWORD
, ctrl
)) {
2124 return PAM_AUTHTOK_RECOVER_ERR
;
2128 * getting here implies we will have to get the password from the
2133 struct pam_message msg
[3], *pmsg
[3];
2134 struct pam_response
*resp
;
2137 /* prepare to converse */
2139 if (comment
!= NULL
&& off(ctrl
, WINBIND_SILENT
)) {
2141 msg
[0].msg_style
= PAM_TEXT_INFO
;
2142 msg
[0].msg
= discard_const_p(char, comment
);
2149 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
2150 msg
[i
++].msg
= discard_const_p(char, prompt1
);
2153 if (prompt2
!= NULL
) {
2155 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
2156 msg
[i
++].msg
= discard_const_p(char, prompt2
);
2159 /* so call the conversation expecting i responses */
2161 retval
= converse(ctx
->pamh
, i
, pmsg
, &resp
);
2163 if (retval
== PAM_SUCCESS
) {
2164 retval
= PAM_AUTHTOK_RECOVER_ERR
;
2168 if (retval
!= PAM_SUCCESS
) {
2169 _pam_drop_reply(resp
, i
);
2173 /* interpret the response */
2175 token
= x_strdup(resp
[i
- replies
].resp
);
2177 _pam_log(ctx
, LOG_NOTICE
,
2178 "could not recover "
2179 "authentication token");
2180 retval
= PAM_AUTHTOK_RECOVER_ERR
;
2185 /* verify that password entered correctly */
2186 if (!resp
[i
- 1].resp
||
2187 strcmp(token
, resp
[i
- 1].resp
)) {
2188 _pam_delete(token
); /* mistyped */
2189 retval
= PAM_AUTHTOK_RECOVER_ERR
;
2190 _make_remark(ctx
, PAM_ERROR_MSG
,
2196 * tidy up the conversation (resp_retcode) is ignored
2197 * -- what is it for anyway? AGM
2199 _pam_drop_reply(resp
, i
);
2203 if (retval
!= PAM_SUCCESS
) {
2204 _pam_log_debug(ctx
, LOG_DEBUG
,
2205 "unable to obtain a password");
2208 /* 'token' is the entered password */
2210 /* we store this password as an item */
2212 retval
= pam_set_item(ctx
->pamh
, authtok_flag
, token
);
2213 _pam_delete(token
); /* clean it up */
2214 if (retval
!= PAM_SUCCESS
||
2215 (retval
= pam_get_item(ctx
->pamh
, authtok_flag
, (const void **) &item
)) != PAM_SUCCESS
) {
2217 _pam_log(ctx
, LOG_CRIT
, "error manipulating password");
2223 item
= NULL
; /* break link to password */
2228 static const char *get_conf_item_string(struct pwb_context
*ctx
,
2233 const char *parm_opt
= NULL
;
2235 if (!(ctx
->ctrl
& config_flag
)) {
2239 /* let the pam opt take precedence over the pam_winbind.conf option */
2240 for (i
=0; i
<ctx
->argc
; i
++) {
2242 if ((strncmp(ctx
->argv
[i
], item
, strlen(item
)) == 0)) {
2245 if ((p
= strchr(ctx
->argv
[i
], '=')) == NULL
) {
2246 _pam_log(ctx
, LOG_INFO
,
2247 "no \"=\" delimiter for \"%s\" found\n",
2251 _pam_log_debug(ctx
, LOG_INFO
,
2252 "PAM config: %s '%s'\n", item
, p
+1);
2260 key
= talloc_asprintf(ctx
, "global:%s", item
);
2265 parm_opt
= iniparser_getstring(ctx
->dict
, key
, NULL
);
2268 _pam_log_debug(ctx
, LOG_INFO
, "CONFIG file: %s '%s'\n",
2275 static int get_config_item_int(struct pwb_context
*ctx
,
2279 int i
, parm_opt
= -1;
2281 if (!(ctx
->ctrl
& config_flag
)) {
2285 /* let the pam opt take precedence over the pam_winbind.conf option */
2286 for (i
= 0; i
< ctx
->argc
; i
++) {
2288 if ((strncmp(ctx
->argv
[i
], item
, strlen(item
)) == 0)) {
2291 if ((p
= strchr(ctx
->argv
[i
], '=')) == NULL
) {
2292 _pam_log(ctx
, LOG_INFO
,
2293 "no \"=\" delimiter for \"%s\" found\n",
2297 parm_opt
= atoi(p
+ 1);
2298 _pam_log_debug(ctx
, LOG_INFO
,
2299 "PAM config: %s '%d'\n",
2308 key
= talloc_asprintf(ctx
, "global:%s", item
);
2313 parm_opt
= iniparser_getint(ctx
->dict
, key
, -1);
2316 _pam_log_debug(ctx
, LOG_INFO
,
2317 "CONFIG file: %s '%d'\n",
2324 static const char *get_krb5_cc_type_from_config(struct pwb_context
*ctx
)
2326 return get_conf_item_string(ctx
, "krb5_ccache_type",
2327 WINBIND_KRB5_CCACHE_TYPE
);
2330 static const char *get_member_from_config(struct pwb_context
*ctx
)
2332 const char *ret
= NULL
;
2333 ret
= get_conf_item_string(ctx
, "require_membership_of",
2334 WINBIND_REQUIRED_MEMBERSHIP
);
2338 return get_conf_item_string(ctx
, "require-membership-of",
2339 WINBIND_REQUIRED_MEMBERSHIP
);
2342 static int get_warn_pwd_expire_from_config(struct pwb_context
*ctx
)
2345 ret
= get_config_item_int(ctx
, "warn_pwd_expire",
2346 WINBIND_WARN_PWD_EXPIRE
);
2347 /* no or broken setting */
2349 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES
;
2355 * Retrieve the winbind separator.
2357 * @param ctx PAM winbind context.
2359 * @return string separator character. NULL on failure.
2362 static char winbind_get_separator(struct pwb_context
*ctx
)
2365 static struct wbcInterfaceDetails
*details
= NULL
;
2367 wbc_status
= wbcInterfaceDetails(&details
);
2368 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2369 _pam_log(ctx
, LOG_ERR
,
2370 "Could not retrieve winbind interface details: %s",
2371 wbcErrorString(wbc_status
));
2379 return details
->winbind_separator
;
2384 * Convert a upn to a name.
2386 * @param ctx PAM winbind context.
2387 * @param upn USer UPN to be trabslated.
2389 * @return converted name. NULL pointer on failure. Caller needs to free.
2392 static char* winbind_upn_to_username(struct pwb_context
*ctx
,
2396 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
2397 struct wbcDomainSid sid
;
2398 enum wbcSidType type
;
2399 char *domain
= NULL
;
2403 /* This cannot work when the winbind separator = @ */
2405 sep
= winbind_get_separator(ctx
);
2406 if (!sep
|| sep
== '@') {
2410 name
= talloc_strdup(ctx
, upn
);
2414 if ((p
= strchr(name
, '@')) != NULL
) {
2419 /* Convert the UPN to a SID */
2421 wbc_status
= wbcLookupName(domain
, name
, &sid
, &type
);
2422 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2426 /* Convert the the SID back to the sAMAccountName */
2428 wbc_status
= wbcLookupSid(&sid
, &domain
, &name
, &type
);
2429 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2433 return talloc_asprintf(ctx
, "%s\\%s", domain
, name
);
2436 static int _pam_delete_cred(pam_handle_t
*pamh
, int flags
,
2437 int argc
, const char **argv
)
2439 int retval
= PAM_SUCCESS
;
2440 struct pwb_context
*ctx
= NULL
;
2441 struct wbcLogoffUserParams logoff
;
2442 struct wbcAuthErrorInfo
*error
= NULL
;
2444 wbcErr wbc_status
= WBC_ERR_SUCCESS
;
2446 ZERO_STRUCT(logoff
);
2448 retval
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2453 _PAM_LOG_FUNCTION_ENTER("_pam_delete_cred", ctx
);
2455 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
2457 /* destroy the ccache here */
2459 uint32_t wbc_flags
= 0;
2460 const char *ccname
= NULL
;
2461 struct passwd
*pwd
= NULL
;
2463 retval
= pam_get_user(pamh
, &user
, _("Username: "));
2465 _pam_log(ctx
, LOG_ERR
,
2466 "could not identify user");
2471 _pam_log(ctx
, LOG_ERR
,
2472 "username was NULL!");
2473 retval
= PAM_USER_UNKNOWN
;
2477 _pam_log_debug(ctx
, LOG_DEBUG
,
2478 "username [%s] obtained", user
);
2480 ccname
= pam_getenv(pamh
, "KRB5CCNAME");
2481 if (ccname
== NULL
) {
2482 _pam_log_debug(ctx
, LOG_DEBUG
,
2483 "user has no KRB5CCNAME environment");
2486 pwd
= getpwnam(user
);
2488 retval
= PAM_USER_UNKNOWN
;
2492 wbc_flags
= WBFLAG_PAM_KRB5
|
2493 WBFLAG_PAM_CONTACT_TRUSTDOM
;
2495 logoff
.username
= user
;
2498 wbc_status
= wbcAddNamedBlob(&logoff
.num_blobs
,
2502 discard_const_p(uint8_t, ccname
),
2504 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2509 wbc_status
= wbcAddNamedBlob(&logoff
.num_blobs
,
2513 (uint8_t *)&wbc_flags
,
2515 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2519 wbc_status
= wbcAddNamedBlob(&logoff
.num_blobs
,
2523 (uint8_t *)&pwd
->pw_uid
,
2524 sizeof(pwd
->pw_uid
));
2525 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2529 wbc_status
= wbcLogoffUserEx(&logoff
, &error
);
2530 retval
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
2531 user
, "wbcLogoffUser");
2532 wbcFreeMemory(error
);
2533 wbcFreeMemory(logoff
.blobs
);
2534 logoff
.blobs
= NULL
;
2536 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2537 _pam_log(ctx
, LOG_INFO
,
2538 "failed to logoff user %s: %s\n",
2539 user
, wbcErrorString(wbc_status
));
2545 wbcFreeMemory(logoff
.blobs
);
2548 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2549 retval
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
2550 user
, "wbcLogoffUser");
2554 * Delete the krb5 ccname variable from the PAM environment
2555 * if it was set by winbind.
2557 if ((ctx
->ctrl
& WINBIND_KRB5_AUTH
) && pam_getenv(pamh
, "KRB5CCNAME")) {
2558 pam_putenv(pamh
, "KRB5CCNAME");
2561 _PAM_LOG_FUNCTION_LEAVE("_pam_delete_cred", ctx
, retval
);
2569 int pam_sm_authenticate(pam_handle_t
*pamh
, int flags
,
2570 int argc
, const char **argv
)
2572 const char *username
;
2573 const char *password
;
2574 const char *member
= NULL
;
2575 const char *cctype
= NULL
;
2576 int warn_pwd_expire
;
2577 int retval
= PAM_AUTH_ERR
;
2578 char *username_ret
= NULL
;
2579 char *new_authtok_required
= NULL
;
2580 char *real_username
= NULL
;
2581 struct pwb_context
*ctx
= NULL
;
2583 retval
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2588 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", ctx
);
2590 /* Get the username */
2591 retval
= pam_get_user(pamh
, &username
, NULL
);
2592 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
2593 _pam_log_debug(ctx
, LOG_DEBUG
,
2594 "can not get the username");
2595 retval
= PAM_SERVICE_ERR
;
2601 /* Decode the user name since AIX does not support logn user
2602 names by default. The name is encoded as _#uid. */
2604 if (username
[0] == '_') {
2605 uid_t id
= atoi(&username
[1]);
2606 struct passwd
*pw
= NULL
;
2608 if ((id
!=0) && ((pw
= getpwuid(id
)) != NULL
)) {
2609 real_username
= strdup(pw
->pw_name
);
2614 if (!real_username
) {
2615 /* Just making a copy of the username we got from PAM */
2616 if ((real_username
= strdup(username
)) == NULL
) {
2617 _pam_log_debug(ctx
, LOG_DEBUG
,
2618 "memory allocation failure when copying "
2620 retval
= PAM_SERVICE_ERR
;
2625 /* Maybe this was a UPN */
2627 if (strchr(real_username
, '@') != NULL
) {
2628 char *samaccountname
= NULL
;
2630 samaccountname
= winbind_upn_to_username(ctx
,
2632 if (samaccountname
) {
2633 free(real_username
);
2634 real_username
= strdup(samaccountname
);
2638 retval
= _winbind_read_password(ctx
, ctx
->ctrl
, NULL
,
2639 _("Password: "), NULL
,
2642 if (retval
!= PAM_SUCCESS
) {
2643 _pam_log(ctx
, LOG_ERR
,
2644 "Could not retrieve user's password");
2645 retval
= PAM_AUTHTOK_ERR
;
2649 /* Let's not give too much away in the log file */
2651 #ifdef DEBUG_PASSWORD
2652 _pam_log_debug(ctx
, LOG_INFO
,
2653 "Verify user '%s' with password '%s'",
2654 real_username
, password
);
2656 _pam_log_debug(ctx
, LOG_INFO
,
2657 "Verify user '%s'", real_username
);
2660 member
= get_member_from_config(ctx
);
2661 cctype
= get_krb5_cc_type_from_config(ctx
);
2662 warn_pwd_expire
= get_warn_pwd_expire_from_config(ctx
);
2664 /* Now use the username to look up password */
2665 retval
= winbind_auth_request(ctx
, real_username
, password
,
2666 member
, cctype
, warn_pwd_expire
,
2668 NULL
, &username_ret
);
2670 if (retval
== PAM_NEW_AUTHTOK_REQD
||
2671 retval
== PAM_AUTHTOK_EXPIRED
) {
2673 char *new_authtok_required_during_auth
= NULL
;
2675 new_authtok_required
= talloc_asprintf(NULL
, "%d", retval
);
2676 if (!new_authtok_required
) {
2677 retval
= PAM_BUF_ERR
;
2681 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
,
2682 new_authtok_required
,
2683 _pam_winbind_cleanup_func
);
2685 retval
= PAM_SUCCESS
;
2687 new_authtok_required_during_auth
= talloc_asprintf(NULL
, "%d", true);
2688 if (!new_authtok_required_during_auth
) {
2689 retval
= PAM_BUF_ERR
;
2693 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2694 new_authtok_required_during_auth
,
2695 _pam_winbind_cleanup_func
);
2702 pam_set_item (pamh
, PAM_USER
, username_ret
);
2703 _pam_log_debug(ctx
, LOG_INFO
,
2704 "Returned user was '%s'", username_ret
);
2708 if (real_username
) {
2709 free(real_username
);
2712 if (!new_authtok_required
) {
2713 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, NULL
, NULL
);
2716 if (retval
!= PAM_SUCCESS
) {
2717 _pam_free_data_info3(pamh
);
2721 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", ctx
, retval
);
2729 int pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
2730 int argc
, const char **argv
)
2732 int ret
= PAM_SYSTEM_ERR
;
2733 struct pwb_context
*ctx
= NULL
;
2735 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2740 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", ctx
);
2742 switch (flags
& ~PAM_SILENT
) {
2744 case PAM_DELETE_CRED
:
2745 ret
= _pam_delete_cred(pamh
, flags
, argc
, argv
);
2747 case PAM_REFRESH_CRED
:
2748 _pam_log_debug(ctx
, LOG_WARNING
,
2749 "PAM_REFRESH_CRED not implemented");
2752 case PAM_REINITIALIZE_CRED
:
2753 _pam_log_debug(ctx
, LOG_WARNING
,
2754 "PAM_REINITIALIZE_CRED not implemented");
2757 case PAM_ESTABLISH_CRED
:
2758 _pam_log_debug(ctx
, LOG_WARNING
,
2759 "PAM_ESTABLISH_CRED not implemented");
2763 ret
= PAM_SYSTEM_ERR
;
2769 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", ctx
, ret
);
2777 * Account management. We want to verify that the account exists
2778 * before returning PAM_SUCCESS
2781 int pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags
,
2782 int argc
, const char **argv
)
2784 const char *username
;
2785 int ret
= PAM_USER_UNKNOWN
;
2786 const char *tmp
= NULL
;
2787 struct pwb_context
*ctx
= NULL
;
2789 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2794 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", ctx
);
2797 /* Get the username */
2798 ret
= pam_get_user(pamh
, &username
, NULL
);
2799 if ((ret
!= PAM_SUCCESS
) || (!username
)) {
2800 _pam_log_debug(ctx
, LOG_DEBUG
,
2801 "can not get the username");
2802 ret
= PAM_SERVICE_ERR
;
2806 /* Verify the username */
2807 ret
= valid_user(ctx
, username
);
2810 /* some sort of system error. The log was already printed */
2811 ret
= PAM_SERVICE_ERR
;
2814 /* the user does not exist */
2815 _pam_log_debug(ctx
, LOG_NOTICE
, "user '%s' not found",
2817 if (ctx
->ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
2821 ret
= PAM_USER_UNKNOWN
;
2824 pam_get_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
,
2825 (const void **)&tmp
);
2829 case PAM_AUTHTOK_EXPIRED
:
2830 /* fall through, since new token is required in this case */
2831 case PAM_NEW_AUTHTOK_REQD
:
2832 _pam_log(ctx
, LOG_WARNING
,
2833 "pam_sm_acct_mgmt success but %s is set",
2834 PAM_WINBIND_NEW_AUTHTOK_REQD
);
2835 _pam_log(ctx
, LOG_NOTICE
,
2836 "user '%s' needs new password",
2838 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2839 ret
= PAM_NEW_AUTHTOK_REQD
;
2842 _pam_log(ctx
, LOG_WARNING
,
2843 "pam_sm_acct_mgmt success");
2844 _pam_log(ctx
, LOG_NOTICE
,
2845 "user '%s' granted access", username
);
2851 /* Otherwise, the authentication looked good */
2852 _pam_log(ctx
, LOG_NOTICE
,
2853 "user '%s' granted access", username
);
2857 /* we don't know anything about this return value */
2858 _pam_log(ctx
, LOG_ERR
,
2859 "internal module error (ret = %d, user = '%s')",
2861 ret
= PAM_SERVICE_ERR
;
2865 /* should not be reached */
2870 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", ctx
, ret
);
2878 int pam_sm_open_session(pam_handle_t
*pamh
, int flags
,
2879 int argc
, const char **argv
)
2881 int ret
= PAM_SUCCESS
;
2882 struct pwb_context
*ctx
= NULL
;
2884 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2889 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", ctx
);
2891 if (ctx
->ctrl
& WINBIND_MKHOMEDIR
) {
2892 /* check and create homedir */
2893 ret
= _pam_mkhomedir(ctx
);
2896 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", ctx
, ret
);
2904 int pam_sm_close_session(pam_handle_t
*pamh
, int flags
,
2905 int argc
, const char **argv
)
2907 int ret
= PAM_SUCCESS
;
2908 struct pwb_context
*ctx
= NULL
;
2910 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2915 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", ctx
);
2918 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", ctx
, ret
);
2926 * evaluate whether we need to re-authenticate with kerberos after a
2929 * @param ctx PAM winbind context.
2930 * @param user The username
2932 * @return boolean Returns true if required, false if not.
2935 static bool _pam_require_krb5_auth_after_chauthtok(struct pwb_context
*ctx
,
2939 /* Make sure that we only do this if a) the chauthtok got initiated
2940 * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2941 * later password change via the "passwd" command if done by the user
2943 * NB. If we login from gdm or xdm and the password expires,
2944 * we change the password, but there is no memory cache.
2945 * Thus, even for passthrough login, we should do the
2946 * authentication again to update memory cache.
2950 char *new_authtok_reqd_during_auth
= NULL
;
2951 struct passwd
*pwd
= NULL
;
2953 pam_get_data(ctx
->pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2954 (const void **) &new_authtok_reqd_during_auth
);
2955 pam_set_data(ctx
->pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2958 if (new_authtok_reqd_during_auth
) {
2962 pwd
= getpwnam(user
);
2967 if (getuid() == pwd
->pw_uid
) {
2976 int pam_sm_chauthtok(pam_handle_t
* pamh
, int flags
,
2977 int argc
, const char **argv
)
2981 bool cached_login
= false;
2983 /* <DO NOT free() THESE> */
2985 const char *pass_old
;
2986 const char *pass_new
;
2987 /* </DO NOT free() THESE> */
2992 char *username_ret
= NULL
;
2993 struct wbcAuthErrorInfo
*error
= NULL
;
2994 struct pwb_context
*ctx
= NULL
;
2996 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
3001 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", ctx
);
3003 cached_login
= (ctx
->ctrl
& WINBIND_CACHED_LOGIN
);
3005 /* clearing offline bit for auth */
3006 ctx
->ctrl
&= ~WINBIND_CACHED_LOGIN
;
3009 * First get the name of a user
3011 ret
= pam_get_user(pamh
, &user
, _("Username: "));
3013 _pam_log(ctx
, LOG_ERR
,
3014 "password - could not identify user");
3019 _pam_log(ctx
, LOG_ERR
, "username was NULL!");
3020 ret
= PAM_USER_UNKNOWN
;
3024 _pam_log_debug(ctx
, LOG_DEBUG
, "username [%s] obtained", user
);
3026 /* check if this is really a user in winbindd, not only in NSS */
3027 ret
= valid_user(ctx
, user
);
3030 ret
= PAM_USER_UNKNOWN
;
3033 ret
= PAM_SYSTEM_ERR
;
3040 * obtain and verify the current password (OLDAUTHTOK) for
3044 if (flags
& PAM_PRELIM_CHECK
) {
3045 time_t pwdlastset_prelim
= 0;
3047 /* instruct user what is happening */
3049 #define greeting _("Changing password for")
3050 Announce
= talloc_asprintf(ctx
, "%s %s", greeting
, user
);
3052 _pam_log(ctx
, LOG_CRIT
,
3053 "password - out of memory");
3059 lctrl
= ctx
->ctrl
| WINBIND__OLD_PASSWORD
;
3060 ret
= _winbind_read_password(ctx
, lctrl
,
3062 _("(current) NT password: "),
3064 (const char **) &pass_old
);
3065 TALLOC_FREE(Announce
);
3066 if (ret
!= PAM_SUCCESS
) {
3067 _pam_log(ctx
, LOG_NOTICE
,
3068 "password - (old) token not obtained");
3072 /* verify that this is the password for this user */
3074 ret
= winbind_auth_request(ctx
, user
, pass_old
,
3077 &pwdlastset_prelim
, NULL
);
3079 if (ret
!= PAM_ACCT_EXPIRED
&&
3080 ret
!= PAM_AUTHTOK_EXPIRED
&&
3081 ret
!= PAM_NEW_AUTHTOK_REQD
&&
3082 ret
!= PAM_SUCCESS
) {
3087 pam_set_data(pamh
, PAM_WINBIND_PWD_LAST_SET
,
3088 (void *)pwdlastset_prelim
, NULL
);
3090 ret
= pam_set_item(pamh
, PAM_OLDAUTHTOK
,
3091 (const void *) pass_old
);
3093 if (ret
!= PAM_SUCCESS
) {
3094 _pam_log(ctx
, LOG_CRIT
,
3095 "failed to set PAM_OLDAUTHTOK");
3097 } else if (flags
& PAM_UPDATE_AUTHTOK
) {
3099 time_t pwdlastset_update
= 0;
3102 * obtain the proposed password
3106 * get the old token back.
3109 ret
= pam_get_item(pamh
, PAM_OLDAUTHTOK
, (const void **) &pass_old
);
3111 if (ret
!= PAM_SUCCESS
) {
3112 _pam_log(ctx
, LOG_NOTICE
,
3113 "user not authenticated");
3117 lctrl
= ctx
->ctrl
& ~WINBIND_TRY_FIRST_PASS_ARG
;
3119 if (on(WINBIND_USE_AUTHTOK_ARG
, lctrl
)) {
3120 lctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
3123 ret
= PAM_AUTHTOK_ERR
;
3124 while ((ret
!= PAM_SUCCESS
) && (retry
++ < MAX_PASSWD_TRIES
)) {
3126 * use_authtok is to force the use of a previously entered
3127 * password -- needed for pluggable password strength checking
3130 ret
= _winbind_read_password(ctx
, lctrl
,
3132 _("Enter new NT password: "),
3133 _("Retype new NT password: "),
3134 (const char **)&pass_new
);
3136 if (ret
!= PAM_SUCCESS
) {
3137 _pam_log_debug(ctx
, LOG_ALERT
,
3139 "new password not obtained");
3140 pass_old
= NULL
;/* tidy up */
3145 * At this point we know who the user is and what they
3146 * propose as their new password. Verify that the new
3147 * password is acceptable.
3150 if (pass_new
[0] == '\0') {/* "\0" password = NULL */
3156 * By reaching here we have approved the passwords and must now
3157 * rebuild the password database file.
3159 pam_get_data(pamh
, PAM_WINBIND_PWD_LAST_SET
,
3160 (const void **) &pwdlastset_update
);
3163 * if cached creds were enabled, make sure to set the
3164 * WINBIND_CACHED_LOGIN bit here in order to have winbindd
3165 * update the cached creds storage - gd
3168 ctx
->ctrl
|= WINBIND_CACHED_LOGIN
;
3171 ret
= winbind_chauthtok_request(ctx
, user
, pass_old
,
3172 pass_new
, pwdlastset_update
);
3174 pass_old
= pass_new
= NULL
;
3178 if (_pam_require_krb5_auth_after_chauthtok(ctx
, user
)) {
3180 const char *member
= NULL
;
3181 const char *cctype
= NULL
;
3182 int warn_pwd_expire
;
3183 struct wbcLogonUserInfo
*info
= NULL
;
3184 struct wbcUserPasswordPolicyInfo
*policy
= NULL
;
3186 member
= get_member_from_config(ctx
);
3187 cctype
= get_krb5_cc_type_from_config(ctx
);
3188 warn_pwd_expire
= get_warn_pwd_expire_from_config(ctx
);
3190 /* Keep WINBIND_CACHED_LOGIN bit for
3191 * authentication after changing the password.
3192 * This will update the cached credentials in case
3193 * that winbindd_dual_pam_chauthtok() fails
3198 ret
= winbind_auth_request(ctx
, user
, pass_new
,
3200 &error
, &info
, &policy
,
3201 NULL
, &username_ret
);
3202 pass_old
= pass_new
= NULL
;
3204 if (ret
== PAM_SUCCESS
) {
3206 struct wbcAuthUserInfo
*user_info
= NULL
;
3208 if (info
&& info
->info
) {
3209 user_info
= info
->info
;
3212 /* warn a user if the password is about to
3214 _pam_warn_password_expiry(ctx
, user_info
, policy
,
3218 /* set some info3 info for other modules in the
3220 _pam_set_data_info3(ctx
, user_info
);
3222 /* put krb5ccname into env */
3223 _pam_setup_krb5_env(ctx
, info
);
3226 pam_set_item(pamh
, PAM_USER
,
3228 _pam_log_debug(ctx
, LOG_INFO
,
3229 "Returned user was '%s'",
3236 if (info
&& info
->blobs
) {
3237 wbcFreeMemory(info
->blobs
);
3239 wbcFreeMemory(info
);
3240 wbcFreeMemory(policy
);
3245 ret
= PAM_SERVICE_ERR
;
3250 /* Deal with offline errors. */
3252 const char *codes
[] = {
3253 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
3254 "NT_STATUS_NO_LOGON_SERVERS",
3255 "NT_STATUS_ACCESS_DENIED"
3258 for (i
=0; i
<ARRAY_SIZE(codes
); i
++) {
3260 if (_pam_check_remark_auth_err(ctx
, error
, codes
[i
], &_ret
)) {
3266 wbcFreeMemory(error
);
3268 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", ctx
, ret
);
3277 /* static module data */
3279 struct pam_module _pam_winbind_modstruct
= {
3281 pam_sm_authenticate
,
3284 pam_sm_open_session
,
3285 pam_sm_close_session
,
3292 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
3293 * Copyright (c) Tim Potter <tpot@samba.org> 2000
3294 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
3295 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2008
3296 * Copyright (c) Jan Rêkorajski 1999.
3297 * Copyright (c) Andrew G. Morgan 1996-8.
3298 * Copyright (c) Alex O. Yuriev, 1996.
3299 * Copyright (c) Cristian Gafton 1996.
3300 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
3302 * Redistribution and use in source and binary forms, with or without
3303 * modification, are permitted provided that the following conditions
3305 * 1. Redistributions of source code must retain the above copyright
3306 * notice, and the entire permission notice in its entirety,
3307 * including the disclaimer of warranties.
3308 * 2. Redistributions in binary form must reproduce the above copyright
3309 * notice, this list of conditions and the following disclaimer in the
3310 * documentation and/or other materials provided with the distribution.
3311 * 3. The name of the author may not be used to endorse or promote
3312 * products derived from this software without specific prior
3313 * written permission.
3315 * ALTERNATIVELY, this product may be distributed under the terms of
3316 * the GNU Public License, in which case the provisions of the GPL are
3317 * required INSTEAD OF the above restrictions. (This clause is
3318 * necessary due to a potential bad interaction between the GPL and
3319 * the restrictions contained in a BSD-style copyright.)
3321 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
3322 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3323 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3324 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3325 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3326 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3327 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3328 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3329 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3330 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3331 * OF THE POSSIBILITY OF SUCH DAMAGE.