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 /* If no valid groups were converted we should fail outright */
1168 if (name_list
!= NULL
&& strlen(sid_list_buffer
) == 0) {
1173 * The lookup of the last name failed..
1174 * It results in require_member_of_sid ends with ','
1175 * It is malformated parameter here, overwrite the last ','.
1177 len
= strlen(sid_list_buffer
);
1178 if ((len
!= 0) && (sid_list_buffer
[len
- 1] == ',')) {
1179 sid_list_buffer
[len
- 1] = '\0';
1186 SAFE_FREE(current_name
);
1191 * put krb5ccname variable into environment
1193 * @param ctx PAM winbind context.
1194 * @param krb5ccname env variable retrieved from winbindd.
1199 static void _pam_setup_krb5_env(struct pwb_context
*ctx
,
1200 struct wbcLogonUserInfo
*info
)
1205 const char *krb5ccname
= NULL
;
1207 if (off(ctx
->ctrl
, WINBIND_KRB5_AUTH
)) {
1215 for (i
=0; i
< info
->num_blobs
; i
++) {
1216 if (strcasecmp(info
->blobs
[i
].name
, "krb5ccname") == 0) {
1217 krb5ccname
= (const char *)info
->blobs
[i
].blob
.data
;
1222 if (!krb5ccname
|| (strlen(krb5ccname
) == 0)) {
1226 _pam_log_debug(ctx
, LOG_DEBUG
,
1227 "request returned KRB5CCNAME: %s", krb5ccname
);
1229 if (asprintf(&var
, "KRB5CCNAME=%s", krb5ccname
) == -1) {
1233 ret
= pam_putenv(ctx
->pamh
, var
);
1235 _pam_log(ctx
, LOG_ERR
,
1236 "failed to set KRB5CCNAME to %s: %s",
1237 var
, pam_strerror(ctx
->pamh
, ret
));
1243 * Copy unix username if available (further processed in PAM).
1245 * @param ctx PAM winbind context
1246 * @param user_ret A pointer that holds a pointer to a string
1247 * @param unix_username A username
1252 static void _pam_setup_unix_username(struct pwb_context
*ctx
,
1254 struct wbcLogonUserInfo
*info
)
1256 const char *unix_username
= NULL
;
1259 if (!user_ret
|| !info
) {
1263 for (i
=0; i
< info
->num_blobs
; i
++) {
1264 if (strcasecmp(info
->blobs
[i
].name
, "unix_username") == 0) {
1265 unix_username
= (const char *)info
->blobs
[i
].blob
.data
;
1270 if (!unix_username
|| !unix_username
[0]) {
1274 *user_ret
= strdup(unix_username
);
1278 * Set string into the PAM stack.
1280 * @param ctx PAM winbind context.
1281 * @param data_name Key name for pam_set_data.
1282 * @param value String value.
1287 static void _pam_set_data_string(struct pwb_context
*ctx
,
1288 const char *data_name
,
1293 if (!data_name
|| !value
|| (strlen(data_name
) == 0) ||
1294 (strlen(value
) == 0)) {
1298 ret
= pam_set_data(ctx
->pamh
, data_name
, talloc_strdup(NULL
, value
),
1299 _pam_winbind_cleanup_func
);
1301 _pam_log_debug(ctx
, LOG_DEBUG
,
1302 "Could not set data %s: %s\n",
1303 data_name
, pam_strerror(ctx
->pamh
, ret
));
1308 * Set info3 strings into the PAM stack.
1310 * @param ctx PAM winbind context.
1311 * @param data_name Key name for pam_set_data.
1312 * @param value String value.
1317 static void _pam_set_data_info3(struct pwb_context
*ctx
,
1318 const struct wbcAuthUserInfo
*info
)
1320 _pam_set_data_string(ctx
, PAM_WINBIND_HOMEDIR
,
1321 info
->home_directory
);
1322 _pam_set_data_string(ctx
, PAM_WINBIND_LOGONSCRIPT
,
1323 info
->logon_script
);
1324 _pam_set_data_string(ctx
, PAM_WINBIND_LOGONSERVER
,
1325 info
->logon_server
);
1326 _pam_set_data_string(ctx
, PAM_WINBIND_PROFILEPATH
,
1327 info
->profile_path
);
1331 * Free info3 strings in the PAM stack.
1333 * @param pamh PAM handle
1338 static void _pam_free_data_info3(pam_handle_t
*pamh
)
1340 pam_set_data(pamh
, PAM_WINBIND_HOMEDIR
, NULL
, NULL
);
1341 pam_set_data(pamh
, PAM_WINBIND_LOGONSCRIPT
, NULL
, NULL
);
1342 pam_set_data(pamh
, PAM_WINBIND_LOGONSERVER
, NULL
, NULL
);
1343 pam_set_data(pamh
, PAM_WINBIND_PROFILEPATH
, NULL
, NULL
);
1347 * Send PAM_ERROR_MSG for cached or grace logons.
1349 * @param ctx PAM winbind context.
1350 * @param username User in PAM request.
1351 * @param info3_user_flgs Info3 flags containing logon type bits.
1356 static void _pam_warn_logon_type(struct pwb_context
*ctx
,
1357 const char *username
,
1358 uint32_t info3_user_flgs
)
1360 /* inform about logon type */
1361 if (PAM_WB_GRACE_LOGON(info3_user_flgs
)) {
1363 _make_remark(ctx
, PAM_ERROR_MSG
,
1365 "Please change your password as soon you're "
1367 _pam_log_debug(ctx
, LOG_DEBUG
,
1368 "User %s logged on using grace logon\n",
1371 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs
)) {
1373 _make_remark(ctx
, PAM_ERROR_MSG
,
1374 _("Domain Controller unreachable, "
1375 "using cached credentials instead. "
1376 "Network resources may be unavailable"));
1377 _pam_log_debug(ctx
, LOG_DEBUG
,
1378 "User %s logged on using cached credentials\n",
1384 * Send PAM_ERROR_MSG for krb5 errors.
1386 * @param ctx PAM winbind context.
1387 * @param username User in PAM request.
1388 * @param info3_user_flgs Info3 flags containing logon type bits.
1393 static void _pam_warn_krb5_failure(struct pwb_context
*ctx
,
1394 const char *username
,
1395 uint32_t info3_user_flgs
)
1397 if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs
)) {
1398 _make_remark(ctx
, PAM_ERROR_MSG
,
1399 _("Failed to establish your Kerberos Ticket cache "
1400 "due time differences\n"
1401 "with the domain controller. "
1402 "Please verify the system time.\n"));
1403 _pam_log_debug(ctx
, LOG_DEBUG
,
1404 "User %s: Clock skew when getting Krb5 TGT\n",
1409 static bool _pam_check_remark_auth_err(struct pwb_context
*ctx
,
1410 const struct wbcAuthErrorInfo
*e
,
1411 const char *nt_status_string
,
1414 const char *ntstatus
= NULL
;
1415 const char *error_string
= NULL
;
1417 if (!e
|| !pam_err
) {
1421 ntstatus
= e
->nt_string
;
1426 if (strcasecmp(ntstatus
, nt_status_string
) == 0) {
1428 error_string
= _get_ntstatus_error_string(nt_status_string
);
1430 _make_remark(ctx
, PAM_ERROR_MSG
, error_string
);
1431 *pam_err
= e
->pam_error
;
1435 if (e
->display_string
) {
1436 _make_remark(ctx
, PAM_ERROR_MSG
, _(e
->display_string
));
1437 *pam_err
= e
->pam_error
;
1441 _make_remark(ctx
, PAM_ERROR_MSG
, nt_status_string
);
1442 *pam_err
= e
->pam_error
;
1451 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
1453 * @param i The wbcUserPasswordPolicyInfo struct.
1455 * @return string (caller needs to talloc_free).
1458 static char *_pam_compose_pwd_restriction_string(struct pwb_context
*ctx
,
1459 struct wbcUserPasswordPolicyInfo
*i
)
1467 str
= talloc_asprintf(ctx
, _("Your password "));
1472 if (i
->min_length_password
> 0) {
1473 str
= talloc_asprintf_append(str
,
1474 _("must be at least %d characters; "),
1475 i
->min_length_password
);
1481 if (i
->password_history
> 0) {
1482 str
= talloc_asprintf_append(str
,
1483 _("cannot repeat any of your previous %d "
1485 i
->password_history
);
1491 if (i
->password_properties
& WBC_DOMAIN_PASSWORD_COMPLEX
) {
1492 str
= talloc_asprintf_append(str
,
1493 _("must contain capitals, numerals "
1495 "and cannot contain your account "
1502 str
= talloc_asprintf_append(str
,
1503 _("Please type a different password. "
1504 "Type a password which meets these requirements in "
1505 "both text boxes."));
1517 static int _pam_create_homedir(struct pwb_context
*ctx
,
1518 const char *dirname
,
1523 if (stat(dirname
, &sbuf
) == 0) {
1527 if (mkdir(dirname
, mode
) != 0) {
1529 _make_remark_format(ctx
, PAM_TEXT_INFO
,
1530 _("Creating directory: %s failed: %s"),
1531 dirname
, strerror(errno
));
1532 _pam_log(ctx
, LOG_ERR
, "could not create dir: %s (%s)",
1533 dirname
, strerror(errno
));
1534 return PAM_PERM_DENIED
;
1540 static int _pam_chown_homedir(struct pwb_context
*ctx
,
1541 const char *dirname
,
1545 if (chown(dirname
, uid
, gid
) != 0) {
1546 _pam_log(ctx
, LOG_ERR
, "failed to chown user homedir: %s (%s)",
1547 dirname
, strerror(errno
));
1548 return PAM_PERM_DENIED
;
1554 static int _pam_mkhomedir(struct pwb_context
*ctx
)
1556 struct passwd
*pwd
= NULL
;
1558 char *create_dir
= NULL
;
1559 char *user_dir
= NULL
;
1561 const char *username
;
1563 char *safe_ptr
= NULL
;
1566 /* Get the username */
1567 ret
= pam_get_user(ctx
->pamh
, &username
, NULL
);
1568 if ((ret
!= PAM_SUCCESS
) || (!username
)) {
1569 _pam_log_debug(ctx
, LOG_DEBUG
, "can not get the username");
1570 return PAM_SERVICE_ERR
;
1573 pwd
= getpwnam(username
);
1575 _pam_log_debug(ctx
, LOG_DEBUG
, "can not get the username");
1576 return PAM_USER_UNKNOWN
;
1578 _pam_log_debug(ctx
, LOG_DEBUG
, "homedir is: %s", pwd
->pw_dir
);
1580 ret
= _pam_create_homedir(ctx
, pwd
->pw_dir
, 0700);
1581 if (ret
== PAM_SUCCESS
) {
1582 ret
= _pam_chown_homedir(ctx
, pwd
->pw_dir
,
1587 if (ret
== PAM_SUCCESS
) {
1591 /* maybe we need to create parent dirs */
1592 create_dir
= talloc_strdup(ctx
, "/");
1597 /* find final directory */
1598 user_dir
= strrchr(pwd
->pw_dir
, '/');
1604 _pam_log(ctx
, LOG_DEBUG
, "final directory: %s", user_dir
);
1608 while ((token
= strtok_r(p
, "/", &safe_ptr
)) != NULL
) {
1614 _pam_log_debug(ctx
, LOG_DEBUG
, "token is %s", token
);
1616 create_dir
= talloc_asprintf_append(create_dir
, "%s/", token
);
1620 _pam_log_debug(ctx
, LOG_DEBUG
, "current_dir is %s", create_dir
);
1622 if (strcmp(token
, user_dir
) == 0) {
1623 _pam_log_debug(ctx
, LOG_DEBUG
, "assuming last directory: %s", token
);
1627 ret
= _pam_create_homedir(ctx
, create_dir
, mode
);
1633 return _pam_chown_homedir(ctx
, create_dir
,
1638 /* talk to winbindd */
1639 static int winbind_auth_request(struct pwb_context
*ctx
,
1644 const int warn_pwd_expire
,
1645 struct wbcAuthErrorInfo
**p_error
,
1646 struct wbcLogonUserInfo
**p_info
,
1647 struct wbcUserPasswordPolicyInfo
**p_policy
,
1648 time_t *pwd_last_set
,
1653 struct wbcLogonUserParams logon
;
1654 char membership_of
[1024];
1655 uid_t user_uid
= -1;
1656 uint32_t flags
= WBFLAG_PAM_INFO3_TEXT
|
1657 WBFLAG_PAM_GET_PWD_POLICY
;
1659 struct wbcLogonUserInfo
*info
= NULL
;
1660 struct wbcAuthUserInfo
*user_info
= NULL
;
1661 struct wbcAuthErrorInfo
*error
= NULL
;
1662 struct wbcUserPasswordPolicyInfo
*policy
= NULL
;
1664 int ret
= PAM_AUTH_ERR
;
1666 const char *codes
[] = {
1667 "NT_STATUS_PASSWORD_EXPIRED",
1668 "NT_STATUS_PASSWORD_MUST_CHANGE",
1669 "NT_STATUS_INVALID_WORKSTATION",
1670 "NT_STATUS_INVALID_LOGON_HOURS",
1671 "NT_STATUS_ACCOUNT_EXPIRED",
1672 "NT_STATUS_ACCOUNT_DISABLED",
1673 "NT_STATUS_ACCOUNT_LOCKED_OUT",
1674 "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
1675 "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
1676 "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
1677 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1678 "NT_STATUS_NO_LOGON_SERVERS",
1679 "NT_STATUS_WRONG_PASSWORD",
1680 "NT_STATUS_ACCESS_DENIED"
1687 /* Krb5 auth always has to go against the KDC of the user's realm */
1689 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1690 flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
1693 if (ctx
->ctrl
& (WINBIND_KRB5_AUTH
|WINBIND_CACHED_LOGIN
)) {
1694 struct passwd
*pwd
= NULL
;
1696 pwd
= getpwnam(user
);
1698 return PAM_USER_UNKNOWN
;
1700 user_uid
= pwd
->pw_uid
;
1703 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1705 _pam_log_debug(ctx
, LOG_DEBUG
,
1706 "enabling krb5 login flag\n");
1708 flags
|= WBFLAG_PAM_KRB5
|
1709 WBFLAG_PAM_FALLBACK_AFTER_KRB5
;
1712 if (ctx
->ctrl
& WINBIND_CACHED_LOGIN
) {
1713 _pam_log_debug(ctx
, LOG_DEBUG
,
1714 "enabling cached login flag\n");
1715 flags
|= WBFLAG_PAM_CACHED_LOGIN
;
1720 flags
|= WBFLAG_PAM_UNIX_NAME
;
1723 if (cctype
!= NULL
) {
1724 _pam_log_debug(ctx
, LOG_DEBUG
,
1725 "enabling request for a %s krb5 ccache\n",
1729 if (member
!= NULL
) {
1731 ZERO_STRUCT(membership_of
);
1733 if (!winbind_name_list_to_sid_string_list(ctx
, user
, member
,
1735 sizeof(membership_of
))) {
1736 _pam_log_debug(ctx
, LOG_ERR
,
1737 "failed to serialize membership of sid "
1738 "\"%s\"\n", member
);
1739 return PAM_AUTH_ERR
;
1745 logon
.username
= user
;
1746 logon
.password
= pass
;
1749 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1753 discard_const_p(uint8_t, cctype
),
1755 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1760 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1766 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1770 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1774 (uint8_t *)&user_uid
,
1776 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1781 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1785 (uint8_t *)membership_of
,
1786 sizeof(membership_of
));
1787 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1792 wbc_status
= wbcLogonUser(&logon
, &info
, &error
, &policy
);
1793 ret
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
1794 user
, "wbcLogonUser");
1795 wbcFreeMemory(logon
.blobs
);
1798 if (info
&& info
->info
) {
1799 user_info
= info
->info
;
1802 if (pwd_last_set
&& user_info
) {
1803 *pwd_last_set
= user_info
->pass_last_set_time
;
1806 if (p_info
&& info
) {
1810 if (p_policy
&& policy
) {
1814 if (p_error
&& error
) {
1815 /* We want to process the error in the caller. */
1820 for (i
=0; i
<ARRAY_SIZE(codes
); i
++) {
1822 if (_pam_check_remark_auth_err(ctx
, error
, codes
[i
], &_ret
)) {
1828 if ((ret
== PAM_SUCCESS
) && user_info
&& policy
&& info
) {
1830 bool already_expired
= false;
1831 bool change_pwd
= false;
1833 /* warn a user if the password is about to expire soon */
1834 _pam_warn_password_expiry(ctx
, user_info
, policy
,
1839 if (already_expired
== true) {
1841 SMB_TIME_T last_set
= user_info
->pass_last_set_time
;
1843 _pam_log_debug(ctx
, LOG_DEBUG
,
1844 "Password has expired "
1845 "(Password was last set: %lld, "
1846 "the policy says it should expire here "
1847 "%lld (now it's: %ld))\n",
1848 (long long int)last_set
,
1849 (long long int)last_set
+
1853 return PAM_AUTHTOK_EXPIRED
;
1857 ret
= PAM_NEW_AUTHTOK_REQD
;
1861 /* inform about logon type */
1862 _pam_warn_logon_type(ctx
, user
, user_info
->user_flags
);
1864 /* inform about krb5 failures */
1865 _pam_warn_krb5_failure(ctx
, user
, user_info
->user_flags
);
1867 /* set some info3 info for other modules in the stack */
1868 _pam_set_data_info3(ctx
, user_info
);
1870 /* put krb5ccname into env */
1871 _pam_setup_krb5_env(ctx
, info
);
1873 /* If winbindd returned a username, return the pointer to it
1875 _pam_setup_unix_username(ctx
, user_ret
, info
);
1879 wbcFreeMemory(logon
.blobs
);
1880 if (info
&& info
->blobs
&& !p_info
) {
1881 wbcFreeMemory(info
->blobs
);
1883 if (error
&& !p_error
) {
1884 wbcFreeMemory(error
);
1886 if (info
&& !p_info
) {
1887 wbcFreeMemory(info
);
1889 if (policy
&& !p_policy
) {
1890 wbcFreeMemory(policy
);
1896 /* talk to winbindd */
1897 static int winbind_chauthtok_request(struct pwb_context
*ctx
,
1899 const char *oldpass
,
1900 const char *newpass
,
1901 time_t pwd_last_set
)
1904 struct wbcChangePasswordParams params
;
1905 struct wbcAuthErrorInfo
*error
= NULL
;
1906 struct wbcUserPasswordPolicyInfo
*policy
= NULL
;
1907 enum wbcPasswordChangeRejectReason reject_reason
= -1;
1911 const char *codes
[] = {
1912 "NT_STATUS_BACKUP_CONTROLLER",
1913 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1914 "NT_STATUS_NO_LOGON_SERVERS",
1915 "NT_STATUS_ACCESS_DENIED",
1916 "NT_STATUS_PWD_TOO_SHORT", /* TODO: tell the min pwd length ? */
1917 "NT_STATUS_PWD_TOO_RECENT", /* TODO: tell the minage ? */
1918 "NT_STATUS_PWD_HISTORY_CONFLICT" /* TODO: tell the history length ? */
1920 int ret
= PAM_AUTH_ERR
;
1922 ZERO_STRUCT(params
);
1924 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1925 flags
|= WBFLAG_PAM_KRB5
|
1926 WBFLAG_PAM_CONTACT_TRUSTDOM
;
1929 if (ctx
->ctrl
& WINBIND_CACHED_LOGIN
) {
1930 flags
|= WBFLAG_PAM_CACHED_LOGIN
;
1933 params
.account_name
= user
;
1934 params
.level
= WBC_CHANGE_PASSWORD_LEVEL_PLAIN
;
1935 params
.old_password
.plaintext
= oldpass
;
1936 params
.new_password
.plaintext
= newpass
;
1937 params
.flags
= flags
;
1939 wbc_status
= wbcChangeUserPasswordEx(¶ms
, &error
, &reject_reason
, &policy
);
1940 ret
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
1941 user
, "wbcChangeUserPasswordEx");
1943 if (WBC_ERROR_IS_OK(wbc_status
)) {
1944 _pam_log(ctx
, LOG_NOTICE
,
1945 "user '%s' password changed", user
);
1950 wbcFreeMemory(policy
);
1954 for (i
=0; i
<ARRAY_SIZE(codes
); i
++) {
1956 if (_pam_check_remark_auth_err(ctx
, error
, codes
[i
], &_ret
)) {
1962 if (!strcasecmp(error
->nt_string
,
1963 "NT_STATUS_PASSWORD_RESTRICTION")) {
1965 char *pwd_restriction_string
= NULL
;
1966 SMB_TIME_T min_pwd_age
= 0;
1969 min_pwd_age
= policy
->min_passwordage
;
1972 /* FIXME: avoid to send multiple PAM messages after another */
1973 switch (reject_reason
) {
1976 case WBC_PWD_CHANGE_NO_ERROR
:
1977 if ((min_pwd_age
> 0) &&
1978 (pwd_last_set
+ min_pwd_age
> time(NULL
))) {
1979 PAM_WB_REMARK_DIRECT(ctx
,
1980 "NT_STATUS_PWD_TOO_RECENT");
1983 case WBC_PWD_CHANGE_PASSWORD_TOO_SHORT
:
1984 PAM_WB_REMARK_DIRECT(ctx
,
1985 "NT_STATUS_PWD_TOO_SHORT");
1987 case WBC_PWD_CHANGE_PWD_IN_HISTORY
:
1988 PAM_WB_REMARK_DIRECT(ctx
,
1989 "NT_STATUS_PWD_HISTORY_CONFLICT");
1991 case WBC_PWD_CHANGE_NOT_COMPLEX
:
1992 _make_remark(ctx
, PAM_ERROR_MSG
,
1993 _("Password does not meet "
1994 "complexity requirements"));
1997 _pam_log_debug(ctx
, LOG_DEBUG
,
1998 "unknown password change "
1999 "reject reason: %d",
2004 pwd_restriction_string
=
2005 _pam_compose_pwd_restriction_string(ctx
, policy
);
2006 if (pwd_restriction_string
) {
2007 _make_remark(ctx
, PAM_ERROR_MSG
,
2008 pwd_restriction_string
);
2009 TALLOC_FREE(pwd_restriction_string
);
2013 wbcFreeMemory(error
);
2014 wbcFreeMemory(policy
);
2020 * Checks if a user has an account
2023 * 1 = User not found
2027 static int valid_user(struct pwb_context
*ctx
,
2030 /* check not only if the user is available over NSS calls, also make
2031 * sure it's really a winbind user, this is important when stacking PAM
2032 * modules in the 'account' or 'password' facility. */
2035 struct passwd
*pwd
= NULL
;
2036 struct passwd
*wb_pwd
= NULL
;
2038 pwd
= getpwnam(user
);
2043 wbc_status
= wbcGetpwnam(user
, &wb_pwd
);
2044 wbcFreeMemory(wb_pwd
);
2045 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2046 _pam_log(ctx
, LOG_DEBUG
, "valid_user: wbcGetpwnam gave %s\n",
2047 wbcErrorString(wbc_status
));
2050 switch (wbc_status
) {
2051 case WBC_ERR_UNKNOWN_USER
:
2052 /* match other insane libwbclient return codes */
2053 case WBC_ERR_WINBIND_NOT_AVAILABLE
:
2054 case WBC_ERR_DOMAIN_NOT_FOUND
:
2056 case WBC_ERR_SUCCESS
:
2064 static char *_pam_delete(register char *xx
)
2072 * obtain a password from the user
2075 static int _winbind_read_password(struct pwb_context
*ctx
,
2077 const char *comment
,
2078 const char *prompt1
,
2079 const char *prompt2
,
2087 _pam_log(ctx
, LOG_DEBUG
, "getting password (0x%08x)", ctrl
);
2090 * make sure nothing inappropriate gets returned
2093 *pass
= token
= NULL
;
2096 * which authentication token are we getting?
2099 if (on(WINBIND__OLD_PASSWORD
, ctrl
)) {
2100 authtok_flag
= PAM_OLDAUTHTOK
;
2102 authtok_flag
= PAM_AUTHTOK
;
2106 * should we obtain the password from a PAM item ?
2109 if (on(WINBIND_TRY_FIRST_PASS_ARG
, ctrl
) ||
2110 on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
2111 retval
= pam_get_item(ctx
->pamh
,
2113 (const void **) &item
);
2114 if (retval
!= PAM_SUCCESS
) {
2116 _pam_log(ctx
, LOG_ALERT
,
2117 "pam_get_item returned error "
2118 "to unix-read-password");
2120 } else if (item
!= NULL
) { /* we have a password! */
2123 _pam_log(ctx
, LOG_DEBUG
,
2124 "pam_get_item returned a password");
2126 } else if (on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
2127 return PAM_AUTHTOK_RECOVER_ERR
; /* didn't work */
2128 } else if (on(WINBIND_USE_AUTHTOK_ARG
, ctrl
)
2129 && off(WINBIND__OLD_PASSWORD
, ctrl
)) {
2130 return PAM_AUTHTOK_RECOVER_ERR
;
2134 * getting here implies we will have to get the password from the
2139 struct pam_message msg
[3], *pmsg
[3];
2140 struct pam_response
*resp
;
2143 /* prepare to converse */
2145 if (comment
!= NULL
&& off(ctrl
, WINBIND_SILENT
)) {
2147 msg
[0].msg_style
= PAM_TEXT_INFO
;
2148 msg
[0].msg
= discard_const_p(char, comment
);
2155 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
2156 msg
[i
++].msg
= discard_const_p(char, prompt1
);
2159 if (prompt2
!= NULL
) {
2161 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
2162 msg
[i
++].msg
= discard_const_p(char, prompt2
);
2165 /* so call the conversation expecting i responses */
2167 retval
= converse(ctx
->pamh
, i
, pmsg
, &resp
);
2169 if (retval
== PAM_SUCCESS
) {
2170 retval
= PAM_AUTHTOK_RECOVER_ERR
;
2174 if (retval
!= PAM_SUCCESS
) {
2175 _pam_drop_reply(resp
, i
);
2179 /* interpret the response */
2181 token
= x_strdup(resp
[i
- replies
].resp
);
2183 _pam_log(ctx
, LOG_NOTICE
,
2184 "could not recover "
2185 "authentication token");
2186 retval
= PAM_AUTHTOK_RECOVER_ERR
;
2191 /* verify that password entered correctly */
2192 if (!resp
[i
- 1].resp
||
2193 strcmp(token
, resp
[i
- 1].resp
)) {
2194 _pam_delete(token
); /* mistyped */
2195 retval
= PAM_AUTHTOK_RECOVER_ERR
;
2196 _make_remark(ctx
, PAM_ERROR_MSG
,
2202 * tidy up the conversation (resp_retcode) is ignored
2203 * -- what is it for anyway? AGM
2205 _pam_drop_reply(resp
, i
);
2209 if (retval
!= PAM_SUCCESS
) {
2210 _pam_log_debug(ctx
, LOG_DEBUG
,
2211 "unable to obtain a password");
2214 /* 'token' is the entered password */
2216 /* we store this password as an item */
2218 retval
= pam_set_item(ctx
->pamh
, authtok_flag
, token
);
2219 _pam_delete(token
); /* clean it up */
2220 if (retval
!= PAM_SUCCESS
||
2221 (retval
= pam_get_item(ctx
->pamh
, authtok_flag
, (const void **) &item
)) != PAM_SUCCESS
) {
2223 _pam_log(ctx
, LOG_CRIT
, "error manipulating password");
2229 item
= NULL
; /* break link to password */
2234 static const char *get_conf_item_string(struct pwb_context
*ctx
,
2239 const char *parm_opt
= NULL
;
2241 if (!(ctx
->ctrl
& config_flag
)) {
2245 /* let the pam opt take precedence over the pam_winbind.conf option */
2246 for (i
=0; i
<ctx
->argc
; i
++) {
2248 if ((strncmp(ctx
->argv
[i
], item
, strlen(item
)) == 0)) {
2251 if ((p
= strchr(ctx
->argv
[i
], '=')) == NULL
) {
2252 _pam_log(ctx
, LOG_INFO
,
2253 "no \"=\" delimiter for \"%s\" found\n",
2257 _pam_log_debug(ctx
, LOG_INFO
,
2258 "PAM config: %s '%s'\n", item
, p
+1);
2266 key
= talloc_asprintf(ctx
, "global:%s", item
);
2271 parm_opt
= iniparser_getstring(ctx
->dict
, key
, NULL
);
2274 _pam_log_debug(ctx
, LOG_INFO
, "CONFIG file: %s '%s'\n",
2281 static int get_config_item_int(struct pwb_context
*ctx
,
2285 int i
, parm_opt
= -1;
2287 if (!(ctx
->ctrl
& config_flag
)) {
2291 /* let the pam opt take precedence over the pam_winbind.conf option */
2292 for (i
= 0; i
< ctx
->argc
; i
++) {
2294 if ((strncmp(ctx
->argv
[i
], item
, strlen(item
)) == 0)) {
2297 if ((p
= strchr(ctx
->argv
[i
], '=')) == NULL
) {
2298 _pam_log(ctx
, LOG_INFO
,
2299 "no \"=\" delimiter for \"%s\" found\n",
2303 parm_opt
= atoi(p
+ 1);
2304 _pam_log_debug(ctx
, LOG_INFO
,
2305 "PAM config: %s '%d'\n",
2314 key
= talloc_asprintf(ctx
, "global:%s", item
);
2319 parm_opt
= iniparser_getint(ctx
->dict
, key
, -1);
2322 _pam_log_debug(ctx
, LOG_INFO
,
2323 "CONFIG file: %s '%d'\n",
2330 static const char *get_krb5_cc_type_from_config(struct pwb_context
*ctx
)
2332 return get_conf_item_string(ctx
, "krb5_ccache_type",
2333 WINBIND_KRB5_CCACHE_TYPE
);
2336 static const char *get_member_from_config(struct pwb_context
*ctx
)
2338 const char *ret
= NULL
;
2339 ret
= get_conf_item_string(ctx
, "require_membership_of",
2340 WINBIND_REQUIRED_MEMBERSHIP
);
2344 return get_conf_item_string(ctx
, "require-membership-of",
2345 WINBIND_REQUIRED_MEMBERSHIP
);
2348 static int get_warn_pwd_expire_from_config(struct pwb_context
*ctx
)
2351 ret
= get_config_item_int(ctx
, "warn_pwd_expire",
2352 WINBIND_WARN_PWD_EXPIRE
);
2353 /* no or broken setting */
2355 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES
;
2361 * Retrieve the winbind separator.
2363 * @param ctx PAM winbind context.
2365 * @return string separator character. NULL on failure.
2368 static char winbind_get_separator(struct pwb_context
*ctx
)
2371 static struct wbcInterfaceDetails
*details
= NULL
;
2373 wbc_status
= wbcInterfaceDetails(&details
);
2374 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2375 _pam_log(ctx
, LOG_ERR
,
2376 "Could not retrieve winbind interface details: %s",
2377 wbcErrorString(wbc_status
));
2385 return details
->winbind_separator
;
2390 * Convert a upn to a name.
2392 * @param ctx PAM winbind context.
2393 * @param upn USer UPN to be trabslated.
2395 * @return converted name. NULL pointer on failure. Caller needs to free.
2398 static char* winbind_upn_to_username(struct pwb_context
*ctx
,
2402 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
2403 struct wbcDomainSid sid
;
2404 enum wbcSidType type
;
2405 char *domain
= NULL
;
2409 /* This cannot work when the winbind separator = @ */
2411 sep
= winbind_get_separator(ctx
);
2412 if (!sep
|| sep
== '@') {
2416 name
= talloc_strdup(ctx
, upn
);
2420 if ((p
= strchr(name
, '@')) != NULL
) {
2425 /* Convert the UPN to a SID */
2427 wbc_status
= wbcLookupName(domain
, name
, &sid
, &type
);
2428 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2432 /* Convert the the SID back to the sAMAccountName */
2434 wbc_status
= wbcLookupSid(&sid
, &domain
, &name
, &type
);
2435 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2439 return talloc_asprintf(ctx
, "%s%c%s", domain
, sep
, name
);
2442 static int _pam_delete_cred(pam_handle_t
*pamh
, int flags
,
2443 int argc
, const char **argv
)
2445 int retval
= PAM_SUCCESS
;
2446 struct pwb_context
*ctx
= NULL
;
2447 struct wbcLogoffUserParams logoff
;
2448 struct wbcAuthErrorInfo
*error
= NULL
;
2450 wbcErr wbc_status
= WBC_ERR_SUCCESS
;
2452 ZERO_STRUCT(logoff
);
2454 retval
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2459 _PAM_LOG_FUNCTION_ENTER("_pam_delete_cred", ctx
);
2461 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
2463 /* destroy the ccache here */
2465 uint32_t wbc_flags
= 0;
2466 const char *ccname
= NULL
;
2467 struct passwd
*pwd
= NULL
;
2469 retval
= pam_get_user(pamh
, &user
, _("Username: "));
2471 _pam_log(ctx
, LOG_ERR
,
2472 "could not identify user");
2477 _pam_log(ctx
, LOG_ERR
,
2478 "username was NULL!");
2479 retval
= PAM_USER_UNKNOWN
;
2483 _pam_log_debug(ctx
, LOG_DEBUG
,
2484 "username [%s] obtained", user
);
2486 ccname
= pam_getenv(pamh
, "KRB5CCNAME");
2487 if (ccname
== NULL
) {
2488 _pam_log_debug(ctx
, LOG_DEBUG
,
2489 "user has no KRB5CCNAME environment");
2492 pwd
= getpwnam(user
);
2494 retval
= PAM_USER_UNKNOWN
;
2498 wbc_flags
= WBFLAG_PAM_KRB5
|
2499 WBFLAG_PAM_CONTACT_TRUSTDOM
;
2501 logoff
.username
= user
;
2504 wbc_status
= wbcAddNamedBlob(&logoff
.num_blobs
,
2508 discard_const_p(uint8_t, ccname
),
2510 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2515 wbc_status
= wbcAddNamedBlob(&logoff
.num_blobs
,
2519 (uint8_t *)&wbc_flags
,
2521 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2525 wbc_status
= wbcAddNamedBlob(&logoff
.num_blobs
,
2529 (uint8_t *)&pwd
->pw_uid
,
2530 sizeof(pwd
->pw_uid
));
2531 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2535 wbc_status
= wbcLogoffUserEx(&logoff
, &error
);
2536 retval
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
2537 user
, "wbcLogoffUser");
2538 wbcFreeMemory(error
);
2539 wbcFreeMemory(logoff
.blobs
);
2540 logoff
.blobs
= NULL
;
2542 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2543 _pam_log(ctx
, LOG_INFO
,
2544 "failed to logoff user %s: %s\n",
2545 user
, wbcErrorString(wbc_status
));
2551 wbcFreeMemory(logoff
.blobs
);
2554 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2555 retval
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
2556 user
, "wbcLogoffUser");
2560 * Delete the krb5 ccname variable from the PAM environment
2561 * if it was set by winbind.
2563 if ((ctx
->ctrl
& WINBIND_KRB5_AUTH
) && pam_getenv(pamh
, "KRB5CCNAME")) {
2564 pam_putenv(pamh
, "KRB5CCNAME");
2567 _PAM_LOG_FUNCTION_LEAVE("_pam_delete_cred", ctx
, retval
);
2575 int pam_sm_authenticate(pam_handle_t
*pamh
, int flags
,
2576 int argc
, const char **argv
)
2578 const char *username
;
2579 const char *password
;
2580 const char *member
= NULL
;
2581 const char *cctype
= NULL
;
2582 int warn_pwd_expire
;
2583 int retval
= PAM_AUTH_ERR
;
2584 char *username_ret
= NULL
;
2585 char *new_authtok_required
= NULL
;
2586 char *real_username
= NULL
;
2587 struct pwb_context
*ctx
= NULL
;
2589 retval
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2594 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", ctx
);
2596 /* Get the username */
2597 retval
= pam_get_user(pamh
, &username
, NULL
);
2598 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
2599 _pam_log_debug(ctx
, LOG_DEBUG
,
2600 "can not get the username");
2601 retval
= PAM_SERVICE_ERR
;
2607 /* Decode the user name since AIX does not support logn user
2608 names by default. The name is encoded as _#uid. */
2610 if (username
[0] == '_') {
2611 uid_t id
= atoi(&username
[1]);
2612 struct passwd
*pw
= NULL
;
2614 if ((id
!=0) && ((pw
= getpwuid(id
)) != NULL
)) {
2615 real_username
= strdup(pw
->pw_name
);
2620 if (!real_username
) {
2621 /* Just making a copy of the username we got from PAM */
2622 if ((real_username
= strdup(username
)) == NULL
) {
2623 _pam_log_debug(ctx
, LOG_DEBUG
,
2624 "memory allocation failure when copying "
2626 retval
= PAM_SERVICE_ERR
;
2631 /* Maybe this was a UPN */
2633 if (strchr(real_username
, '@') != NULL
) {
2634 char *samaccountname
= NULL
;
2636 samaccountname
= winbind_upn_to_username(ctx
,
2638 if (samaccountname
) {
2639 free(real_username
);
2640 real_username
= strdup(samaccountname
);
2644 retval
= _winbind_read_password(ctx
, ctx
->ctrl
, NULL
,
2645 _("Password: "), NULL
,
2648 if (retval
!= PAM_SUCCESS
) {
2649 _pam_log(ctx
, LOG_ERR
,
2650 "Could not retrieve user's password");
2651 retval
= PAM_AUTHTOK_ERR
;
2655 /* Let's not give too much away in the log file */
2657 #ifdef DEBUG_PASSWORD
2658 _pam_log_debug(ctx
, LOG_INFO
,
2659 "Verify user '%s' with password '%s'",
2660 real_username
, password
);
2662 _pam_log_debug(ctx
, LOG_INFO
,
2663 "Verify user '%s'", real_username
);
2666 member
= get_member_from_config(ctx
);
2667 cctype
= get_krb5_cc_type_from_config(ctx
);
2668 warn_pwd_expire
= get_warn_pwd_expire_from_config(ctx
);
2670 /* Now use the username to look up password */
2671 retval
= winbind_auth_request(ctx
, real_username
, password
,
2672 member
, cctype
, warn_pwd_expire
,
2674 NULL
, &username_ret
);
2676 if (retval
== PAM_NEW_AUTHTOK_REQD
||
2677 retval
== PAM_AUTHTOK_EXPIRED
) {
2679 char *new_authtok_required_during_auth
= NULL
;
2681 new_authtok_required
= talloc_asprintf(NULL
, "%d", retval
);
2682 if (!new_authtok_required
) {
2683 retval
= PAM_BUF_ERR
;
2687 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
,
2688 new_authtok_required
,
2689 _pam_winbind_cleanup_func
);
2691 retval
= PAM_SUCCESS
;
2693 new_authtok_required_during_auth
= talloc_asprintf(NULL
, "%d", true);
2694 if (!new_authtok_required_during_auth
) {
2695 retval
= PAM_BUF_ERR
;
2699 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2700 new_authtok_required_during_auth
,
2701 _pam_winbind_cleanup_func
);
2708 pam_set_item (pamh
, PAM_USER
, username_ret
);
2709 _pam_log_debug(ctx
, LOG_INFO
,
2710 "Returned user was '%s'", username_ret
);
2714 if (real_username
) {
2715 free(real_username
);
2718 if (!new_authtok_required
) {
2719 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, NULL
, NULL
);
2722 if (retval
!= PAM_SUCCESS
) {
2723 _pam_free_data_info3(pamh
);
2727 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", ctx
, retval
);
2735 int pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
2736 int argc
, const char **argv
)
2738 int ret
= PAM_SYSTEM_ERR
;
2739 struct pwb_context
*ctx
= NULL
;
2741 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2746 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", ctx
);
2748 switch (flags
& ~PAM_SILENT
) {
2750 case PAM_DELETE_CRED
:
2751 ret
= _pam_delete_cred(pamh
, flags
, argc
, argv
);
2753 case PAM_REFRESH_CRED
:
2754 _pam_log_debug(ctx
, LOG_WARNING
,
2755 "PAM_REFRESH_CRED not implemented");
2758 case PAM_REINITIALIZE_CRED
:
2759 _pam_log_debug(ctx
, LOG_WARNING
,
2760 "PAM_REINITIALIZE_CRED not implemented");
2763 case PAM_ESTABLISH_CRED
:
2764 _pam_log_debug(ctx
, LOG_WARNING
,
2765 "PAM_ESTABLISH_CRED not implemented");
2769 ret
= PAM_SYSTEM_ERR
;
2775 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", ctx
, ret
);
2783 * Account management. We want to verify that the account exists
2784 * before returning PAM_SUCCESS
2787 int pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags
,
2788 int argc
, const char **argv
)
2790 const char *username
;
2791 int ret
= PAM_USER_UNKNOWN
;
2792 const char *tmp
= NULL
;
2793 struct pwb_context
*ctx
= NULL
;
2795 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2800 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", ctx
);
2803 /* Get the username */
2804 ret
= pam_get_user(pamh
, &username
, NULL
);
2805 if ((ret
!= PAM_SUCCESS
) || (!username
)) {
2806 _pam_log_debug(ctx
, LOG_DEBUG
,
2807 "can not get the username");
2808 ret
= PAM_SERVICE_ERR
;
2812 /* Verify the username */
2813 ret
= valid_user(ctx
, username
);
2816 /* some sort of system error. The log was already printed */
2817 ret
= PAM_SERVICE_ERR
;
2820 /* the user does not exist */
2821 _pam_log_debug(ctx
, LOG_NOTICE
, "user '%s' not found",
2823 if (ctx
->ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
2827 ret
= PAM_USER_UNKNOWN
;
2830 pam_get_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
,
2831 (const void **)&tmp
);
2835 case PAM_AUTHTOK_EXPIRED
:
2836 /* fall through, since new token is required in this case */
2837 case PAM_NEW_AUTHTOK_REQD
:
2838 _pam_log(ctx
, LOG_WARNING
,
2839 "pam_sm_acct_mgmt success but %s is set",
2840 PAM_WINBIND_NEW_AUTHTOK_REQD
);
2841 _pam_log(ctx
, LOG_NOTICE
,
2842 "user '%s' needs new password",
2844 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2845 ret
= PAM_NEW_AUTHTOK_REQD
;
2848 _pam_log(ctx
, LOG_WARNING
,
2849 "pam_sm_acct_mgmt success");
2850 _pam_log(ctx
, LOG_NOTICE
,
2851 "user '%s' granted access", username
);
2857 /* Otherwise, the authentication looked good */
2858 _pam_log(ctx
, LOG_NOTICE
,
2859 "user '%s' granted access", username
);
2863 /* we don't know anything about this return value */
2864 _pam_log(ctx
, LOG_ERR
,
2865 "internal module error (ret = %d, user = '%s')",
2867 ret
= PAM_SERVICE_ERR
;
2871 /* should not be reached */
2876 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", ctx
, ret
);
2884 int pam_sm_open_session(pam_handle_t
*pamh
, int flags
,
2885 int argc
, const char **argv
)
2887 int ret
= PAM_SUCCESS
;
2888 struct pwb_context
*ctx
= NULL
;
2890 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2895 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", ctx
);
2897 if (ctx
->ctrl
& WINBIND_MKHOMEDIR
) {
2898 /* check and create homedir */
2899 ret
= _pam_mkhomedir(ctx
);
2902 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", ctx
, ret
);
2910 int pam_sm_close_session(pam_handle_t
*pamh
, int flags
,
2911 int argc
, const char **argv
)
2913 int ret
= PAM_SUCCESS
;
2914 struct pwb_context
*ctx
= NULL
;
2916 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2921 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", ctx
);
2924 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", ctx
, ret
);
2932 * evaluate whether we need to re-authenticate with kerberos after a
2935 * @param ctx PAM winbind context.
2936 * @param user The username
2938 * @return boolean Returns true if required, false if not.
2941 static bool _pam_require_krb5_auth_after_chauthtok(struct pwb_context
*ctx
,
2945 /* Make sure that we only do this if a) the chauthtok got initiated
2946 * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2947 * later password change via the "passwd" command if done by the user
2949 * NB. If we login from gdm or xdm and the password expires,
2950 * we change the password, but there is no memory cache.
2951 * Thus, even for passthrough login, we should do the
2952 * authentication again to update memory cache.
2956 char *new_authtok_reqd_during_auth
= NULL
;
2957 struct passwd
*pwd
= NULL
;
2959 pam_get_data(ctx
->pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2960 (const void **) &new_authtok_reqd_during_auth
);
2961 pam_set_data(ctx
->pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2964 if (new_authtok_reqd_during_auth
) {
2968 pwd
= getpwnam(user
);
2973 if (getuid() == pwd
->pw_uid
) {
2982 int pam_sm_chauthtok(pam_handle_t
* pamh
, int flags
,
2983 int argc
, const char **argv
)
2987 bool cached_login
= false;
2989 /* <DO NOT free() THESE> */
2991 const char *pass_old
;
2992 const char *pass_new
;
2993 /* </DO NOT free() THESE> */
2998 char *username_ret
= NULL
;
2999 struct wbcAuthErrorInfo
*error
= NULL
;
3000 struct pwb_context
*ctx
= NULL
;
3002 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
3007 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", ctx
);
3009 cached_login
= (ctx
->ctrl
& WINBIND_CACHED_LOGIN
);
3011 /* clearing offline bit for auth */
3012 ctx
->ctrl
&= ~WINBIND_CACHED_LOGIN
;
3015 * First get the name of a user
3017 ret
= pam_get_user(pamh
, &user
, _("Username: "));
3019 _pam_log(ctx
, LOG_ERR
,
3020 "password - could not identify user");
3025 _pam_log(ctx
, LOG_ERR
, "username was NULL!");
3026 ret
= PAM_USER_UNKNOWN
;
3030 _pam_log_debug(ctx
, LOG_DEBUG
, "username [%s] obtained", user
);
3032 /* check if this is really a user in winbindd, not only in NSS */
3033 ret
= valid_user(ctx
, user
);
3036 ret
= PAM_USER_UNKNOWN
;
3039 ret
= PAM_SYSTEM_ERR
;
3046 * obtain and verify the current password (OLDAUTHTOK) for
3050 if (flags
& PAM_PRELIM_CHECK
) {
3051 time_t pwdlastset_prelim
= 0;
3053 /* instruct user what is happening */
3055 #define greeting _("Changing password for")
3056 Announce
= talloc_asprintf(ctx
, "%s %s", greeting
, user
);
3058 _pam_log(ctx
, LOG_CRIT
,
3059 "password - out of memory");
3065 lctrl
= ctx
->ctrl
| WINBIND__OLD_PASSWORD
;
3066 ret
= _winbind_read_password(ctx
, lctrl
,
3068 _("(current) NT password: "),
3070 (const char **) &pass_old
);
3071 TALLOC_FREE(Announce
);
3072 if (ret
!= PAM_SUCCESS
) {
3073 _pam_log(ctx
, LOG_NOTICE
,
3074 "password - (old) token not obtained");
3078 /* verify that this is the password for this user */
3080 ret
= winbind_auth_request(ctx
, user
, pass_old
,
3083 &pwdlastset_prelim
, NULL
);
3085 if (ret
!= PAM_ACCT_EXPIRED
&&
3086 ret
!= PAM_AUTHTOK_EXPIRED
&&
3087 ret
!= PAM_NEW_AUTHTOK_REQD
&&
3088 ret
!= PAM_SUCCESS
) {
3093 pam_set_data(pamh
, PAM_WINBIND_PWD_LAST_SET
,
3094 (void *)pwdlastset_prelim
, NULL
);
3096 ret
= pam_set_item(pamh
, PAM_OLDAUTHTOK
,
3097 (const void *) pass_old
);
3099 if (ret
!= PAM_SUCCESS
) {
3100 _pam_log(ctx
, LOG_CRIT
,
3101 "failed to set PAM_OLDAUTHTOK");
3103 } else if (flags
& PAM_UPDATE_AUTHTOK
) {
3105 time_t pwdlastset_update
= 0;
3108 * obtain the proposed password
3112 * get the old token back.
3115 ret
= pam_get_item(pamh
, PAM_OLDAUTHTOK
, (const void **) &pass_old
);
3117 if (ret
!= PAM_SUCCESS
) {
3118 _pam_log(ctx
, LOG_NOTICE
,
3119 "user not authenticated");
3123 lctrl
= ctx
->ctrl
& ~WINBIND_TRY_FIRST_PASS_ARG
;
3125 if (on(WINBIND_USE_AUTHTOK_ARG
, lctrl
)) {
3126 lctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
3129 ret
= PAM_AUTHTOK_ERR
;
3130 while ((ret
!= PAM_SUCCESS
) && (retry
++ < MAX_PASSWD_TRIES
)) {
3132 * use_authtok is to force the use of a previously entered
3133 * password -- needed for pluggable password strength checking
3136 ret
= _winbind_read_password(ctx
, lctrl
,
3138 _("Enter new NT password: "),
3139 _("Retype new NT password: "),
3140 (const char **)&pass_new
);
3142 if (ret
!= PAM_SUCCESS
) {
3143 _pam_log_debug(ctx
, LOG_ALERT
,
3145 "new password not obtained");
3146 pass_old
= NULL
;/* tidy up */
3151 * At this point we know who the user is and what they
3152 * propose as their new password. Verify that the new
3153 * password is acceptable.
3156 if (pass_new
[0] == '\0') {/* "\0" password = NULL */
3162 * By reaching here we have approved the passwords and must now
3163 * rebuild the password database file.
3165 pam_get_data(pamh
, PAM_WINBIND_PWD_LAST_SET
,
3166 (const void **) &pwdlastset_update
);
3169 * if cached creds were enabled, make sure to set the
3170 * WINBIND_CACHED_LOGIN bit here in order to have winbindd
3171 * update the cached creds storage - gd
3174 ctx
->ctrl
|= WINBIND_CACHED_LOGIN
;
3177 ret
= winbind_chauthtok_request(ctx
, user
, pass_old
,
3178 pass_new
, pwdlastset_update
);
3180 pass_old
= pass_new
= NULL
;
3184 if (_pam_require_krb5_auth_after_chauthtok(ctx
, user
)) {
3186 const char *member
= NULL
;
3187 const char *cctype
= NULL
;
3188 int warn_pwd_expire
;
3189 struct wbcLogonUserInfo
*info
= NULL
;
3190 struct wbcUserPasswordPolicyInfo
*policy
= NULL
;
3192 member
= get_member_from_config(ctx
);
3193 cctype
= get_krb5_cc_type_from_config(ctx
);
3194 warn_pwd_expire
= get_warn_pwd_expire_from_config(ctx
);
3196 /* Keep WINBIND_CACHED_LOGIN bit for
3197 * authentication after changing the password.
3198 * This will update the cached credentials in case
3199 * that winbindd_dual_pam_chauthtok() fails
3204 ret
= winbind_auth_request(ctx
, user
, pass_new
,
3206 &error
, &info
, &policy
,
3207 NULL
, &username_ret
);
3208 pass_old
= pass_new
= NULL
;
3210 if (ret
== PAM_SUCCESS
) {
3212 struct wbcAuthUserInfo
*user_info
= NULL
;
3214 if (info
&& info
->info
) {
3215 user_info
= info
->info
;
3218 /* warn a user if the password is about to
3220 _pam_warn_password_expiry(ctx
, user_info
, policy
,
3224 /* set some info3 info for other modules in the
3226 _pam_set_data_info3(ctx
, user_info
);
3228 /* put krb5ccname into env */
3229 _pam_setup_krb5_env(ctx
, info
);
3232 pam_set_item(pamh
, PAM_USER
,
3234 _pam_log_debug(ctx
, LOG_INFO
,
3235 "Returned user was '%s'",
3242 if (info
&& info
->blobs
) {
3243 wbcFreeMemory(info
->blobs
);
3245 wbcFreeMemory(info
);
3246 wbcFreeMemory(policy
);
3251 ret
= PAM_SERVICE_ERR
;
3256 /* Deal with offline errors. */
3258 const char *codes
[] = {
3259 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
3260 "NT_STATUS_NO_LOGON_SERVERS",
3261 "NT_STATUS_ACCESS_DENIED"
3264 for (i
=0; i
<ARRAY_SIZE(codes
); i
++) {
3266 if (_pam_check_remark_auth_err(ctx
, error
, codes
[i
], &_ret
)) {
3272 wbcFreeMemory(error
);
3274 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", ctx
, ret
);
3283 /* static module data */
3285 struct pam_module _pam_winbind_modstruct
= {
3287 pam_sm_authenticate
,
3290 pam_sm_open_session
,
3291 pam_sm_close_session
,
3298 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
3299 * Copyright (c) Tim Potter <tpot@samba.org> 2000
3300 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
3301 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2008
3302 * Copyright (c) Jan Rêkorajski 1999.
3303 * Copyright (c) Andrew G. Morgan 1996-8.
3304 * Copyright (c) Alex O. Yuriev, 1996.
3305 * Copyright (c) Cristian Gafton 1996.
3306 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
3308 * Redistribution and use in source and binary forms, with or without
3309 * modification, are permitted provided that the following conditions
3311 * 1. Redistributions of source code must retain the above copyright
3312 * notice, and the entire permission notice in its entirety,
3313 * including the disclaimer of warranties.
3314 * 2. Redistributions in binary form must reproduce the above copyright
3315 * notice, this list of conditions and the following disclaimer in the
3316 * documentation and/or other materials provided with the distribution.
3317 * 3. The name of the author may not be used to endorse or promote
3318 * products derived from this software without specific prior
3319 * written permission.
3321 * ALTERNATIVELY, this product may be distributed under the terms of
3322 * the GNU Public License, in which case the provisions of the GPL are
3323 * required INSTEAD OF the above restrictions. (This clause is
3324 * necessary due to a potential bad interaction between the GPL and
3325 * the restrictions contained in a BSD-style copyright.)
3327 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
3328 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3329 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3330 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3331 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3332 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3333 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3334 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3335 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3336 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3337 * OF THE POSSIBILITY OF SUCH DAMAGE.