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 enum pam_winbind_request_type
19 PAM_WINBIND_AUTHENTICATE
,
21 PAM_WINBIND_ACCT_MGMT
,
22 PAM_WINBIND_OPEN_SESSION
,
23 PAM_WINBIND_CLOSE_SESSION
,
24 PAM_WINBIND_CHAUTHTOK
,
28 static int wbc_error_to_pam_error(wbcErr status
)
33 case WBC_ERR_NOT_IMPLEMENTED
:
34 return PAM_SERVICE_ERR
;
35 case WBC_ERR_UNKNOWN_FAILURE
:
37 case WBC_ERR_NO_MEMORY
:
39 case WBC_ERR_INVALID_SID
:
40 case WBC_ERR_INVALID_PARAM
:
42 case WBC_ERR_WINBIND_NOT_AVAILABLE
:
43 return PAM_AUTHINFO_UNAVAIL
;
44 case WBC_ERR_DOMAIN_NOT_FOUND
:
45 return PAM_AUTHINFO_UNAVAIL
;
46 case WBC_ERR_INVALID_RESPONSE
:
48 case WBC_ERR_NSS_ERROR
:
49 return PAM_USER_UNKNOWN
;
50 case WBC_ERR_AUTH_ERROR
:
52 case WBC_ERR_UNKNOWN_USER
:
53 return PAM_USER_UNKNOWN
;
54 case WBC_ERR_UNKNOWN_GROUP
:
55 return PAM_USER_UNKNOWN
;
56 case WBC_ERR_PWD_CHANGE_FAILED
:
64 static const char *_pam_error_code_str(int err
)
70 return "PAM_OPEN_ERR";
72 return "PAM_SYMBOL_ERR";
74 return "PAM_SERVICE_ERR";
76 return "PAM_SYSTEM_ERR";
80 return "PAM_PERM_DENIED";
82 return "PAM_AUTH_ERR";
83 case PAM_CRED_INSUFFICIENT
:
84 return "PAM_CRED_INSUFFICIENT";
85 case PAM_AUTHINFO_UNAVAIL
:
86 return "PAM_AUTHINFO_UNAVAIL";
87 case PAM_USER_UNKNOWN
:
88 return "PAM_USER_UNKNOWN";
90 return "PAM_MAXTRIES";
91 case PAM_NEW_AUTHTOK_REQD
:
92 return "PAM_NEW_AUTHTOK_REQD";
93 case PAM_ACCT_EXPIRED
:
94 return "PAM_ACCT_EXPIRED";
96 return "PAM_SESSION_ERR";
97 case PAM_CRED_UNAVAIL
:
98 return "PAM_CRED_UNAVAIL";
99 case PAM_CRED_EXPIRED
:
100 return "PAM_CRED_EXPIRED";
102 return "PAM_CRED_ERR";
103 case PAM_NO_MODULE_DATA
:
104 return "PAM_NO_MODULE_DATA";
106 return "PAM_CONV_ERR";
107 case PAM_AUTHTOK_ERR
:
108 return "PAM_AUTHTOK_ERR";
109 case PAM_AUTHTOK_RECOVER_ERR
:
110 return "PAM_AUTHTOK_RECOVER_ERR";
111 case PAM_AUTHTOK_LOCK_BUSY
:
112 return "PAM_AUTHTOK_LOCK_BUSY";
113 case PAM_AUTHTOK_DISABLE_AGING
:
114 return "PAM_AUTHTOK_DISABLE_AGING";
116 return "PAM_TRY_AGAIN";
121 case PAM_AUTHTOK_EXPIRED
:
122 return "PAM_AUTHTOK_EXPIRED";
123 #ifdef PAM_MODULE_UNKNOWN
124 case PAM_MODULE_UNKNOWN
:
125 return "PAM_MODULE_UNKNOWN";
129 return "PAM_BAD_ITEM";
131 #ifdef PAM_CONV_AGAIN
133 return "PAM_CONV_AGAIN";
135 #ifdef PAM_INCOMPLETE
137 return "PAM_INCOMPLETE";
144 #define _PAM_LOG_FUNCTION_ENTER(function, ctx) \
146 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] ENTER: " \
147 function " (flags: 0x%04x)", ctx->pamh, ctx->flags); \
148 _pam_log_state(ctx); \
151 #define _PAM_LOG_FUNCTION_LEAVE(function, ctx, retval) \
153 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] LEAVE: " \
154 function " returning %d (%s)", ctx ? ctx->pamh : NULL, retval, \
155 _pam_error_code_str(retval)); \
156 _pam_log_state(ctx); \
161 #define MAX_PASSWD_TRIES 3
164 static char initialized
= 0;
166 static inline void textdomain_init(void);
167 static inline void textdomain_init(void)
170 bindtextdomain(MODULE_NAME
, LOCALEDIR
);
178 /* some syslogging */
180 #ifdef HAVE_PAM_VSYSLOG
181 static void _pam_log_int(const pam_handle_t
*pamh
,
186 pam_vsyslog(pamh
, err
, format
, args
);
189 static void _pam_log_int(const pam_handle_t
*pamh
,
194 char *format2
= NULL
;
197 pam_get_item(pamh
, PAM_SERVICE
, (const void **) &service
);
199 format2
= (char *)malloc(strlen(MODULE_NAME
)+strlen(format
)+strlen(service
)+5);
200 if (format2
== NULL
) {
201 /* what else todo ? */
202 vsyslog(err
, format
, args
);
206 sprintf(format2
, "%s(%s): %s", MODULE_NAME
, service
, format
);
207 vsyslog(err
, format2
, args
);
210 #endif /* HAVE_PAM_VSYSLOG */
212 static bool _pam_log_is_silent(int ctrl
)
214 return on(ctrl
, WINBIND_SILENT
);
217 static void _pam_log(struct pwb_context
*r
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
218 static void _pam_log(struct pwb_context
*r
, int err
, const char *format
, ...)
222 if (_pam_log_is_silent(r
->ctrl
)) {
226 va_start(args
, format
);
227 _pam_log_int(r
->pamh
, err
, format
, args
);
230 static void __pam_log(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(4,5);
231 static void __pam_log(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...)
235 if (_pam_log_is_silent(ctrl
)) {
239 va_start(args
, format
);
240 _pam_log_int(pamh
, err
, format
, args
);
244 static bool _pam_log_is_debug_enabled(int ctrl
)
250 if (_pam_log_is_silent(ctrl
)) {
254 if (!(ctrl
& WINBIND_DEBUG_ARG
)) {
261 static bool _pam_log_is_debug_state_enabled(int ctrl
)
263 if (!(ctrl
& WINBIND_DEBUG_STATE
)) {
267 return _pam_log_is_debug_enabled(ctrl
);
270 static void _pam_log_debug(struct pwb_context
*r
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
271 static void _pam_log_debug(struct pwb_context
*r
, int err
, const char *format
, ...)
275 if (!r
|| !_pam_log_is_debug_enabled(r
->ctrl
)) {
279 va_start(args
, format
);
280 _pam_log_int(r
->pamh
, err
, format
, args
);
283 static void __pam_log_debug(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(4,5);
284 static void __pam_log_debug(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...)
288 if (!_pam_log_is_debug_enabled(ctrl
)) {
292 va_start(args
, format
);
293 _pam_log_int(pamh
, err
, format
, args
);
297 static void _pam_log_state_datum(struct pwb_context
*ctx
,
302 const void *data
= NULL
;
303 if (item_type
!= 0) {
304 pam_get_item(ctx
->pamh
, item_type
, &data
);
306 pam_get_data(ctx
->pamh
, key
, &data
);
309 const char *type
= (item_type
!= 0) ? "ITEM" : "DATA";
310 if (is_string
!= 0) {
311 _pam_log_debug(ctx
, LOG_DEBUG
,
312 "[pamh: %p] STATE: %s(%s) = \"%s\" (%p)",
313 ctx
->pamh
, type
, key
, (const char *)data
,
316 _pam_log_debug(ctx
, LOG_DEBUG
,
317 "[pamh: %p] STATE: %s(%s) = %p",
318 ctx
->pamh
, type
, key
, data
);
323 #define _PAM_LOG_STATE_DATA_POINTER(ctx, module_data_name) \
324 _pam_log_state_datum(ctx, 0, module_data_name, 0)
326 #define _PAM_LOG_STATE_DATA_STRING(ctx, module_data_name) \
327 _pam_log_state_datum(ctx, 0, module_data_name, 1)
329 #define _PAM_LOG_STATE_ITEM_POINTER(ctx, item_type) \
330 _pam_log_state_datum(ctx, item_type, #item_type, 0)
332 #define _PAM_LOG_STATE_ITEM_STRING(ctx, item_type) \
333 _pam_log_state_datum(ctx, item_type, #item_type, 1)
335 #ifdef DEBUG_PASSWORD
336 #define _LOG_PASSWORD_AS_STRING 1
338 #define _LOG_PASSWORD_AS_STRING 0
341 #define _PAM_LOG_STATE_ITEM_PASSWORD(ctx, item_type) \
342 _pam_log_state_datum(ctx, item_type, #item_type, \
343 _LOG_PASSWORD_AS_STRING)
345 * wrapper to preserve old behaviour of iniparser which ignored
346 * key values that had no value assigned like
348 * for a key like above newer iniparser will return a zero-length
349 * string, previously iniparser would return NULL
351 static char *iniparser_getstring_nonempty(dictionary
*d
, char *key
, char *def
)
353 char *ret
= iniparser_getstring(d
, key
, def
);
354 if (ret
&& strlen(ret
) == 0) {
360 static void _pam_log_state(struct pwb_context
*ctx
)
362 if (!ctx
|| !_pam_log_is_debug_state_enabled(ctx
->ctrl
)) {
366 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_SERVICE
);
367 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_USER
);
368 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_TTY
);
369 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_RHOST
);
370 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_RUSER
);
371 _PAM_LOG_STATE_ITEM_PASSWORD(ctx
, PAM_OLDAUTHTOK
);
372 _PAM_LOG_STATE_ITEM_PASSWORD(ctx
, PAM_AUTHTOK
);
373 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_USER_PROMPT
);
374 _PAM_LOG_STATE_ITEM_POINTER(ctx
, PAM_CONV
);
375 #ifdef PAM_FAIL_DELAY
376 _PAM_LOG_STATE_ITEM_POINTER(ctx
, PAM_FAIL_DELAY
);
378 #ifdef PAM_REPOSITORY
379 _PAM_LOG_STATE_ITEM_POINTER(ctx
, PAM_REPOSITORY
);
382 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_HOMEDIR
);
383 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_LOGONSCRIPT
);
384 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_LOGONSERVER
);
385 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_PROFILEPATH
);
386 _PAM_LOG_STATE_DATA_STRING(ctx
,
387 PAM_WINBIND_NEW_AUTHTOK_REQD
);
388 /* Use atoi to get PAM result code */
389 _PAM_LOG_STATE_DATA_STRING(ctx
,
390 PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
);
391 _PAM_LOG_STATE_DATA_POINTER(ctx
, PAM_WINBIND_PWD_LAST_SET
);
394 static int _pam_parse(const pam_handle_t
*pamh
,
398 enum pam_winbind_request_type type
,
399 dictionary
**result_d
)
402 const char *config_file
= NULL
;
405 dictionary
*d
= NULL
;
407 if (flags
& PAM_SILENT
) {
408 ctrl
|= WINBIND_SILENT
;
411 for (i
=argc
,v
=argv
; i
-- > 0; ++v
) {
412 if (!strncasecmp(*v
, "config", strlen("config"))) {
413 ctrl
|= WINBIND_CONFIG_FILE
;
419 if (config_file
== NULL
) {
420 config_file
= PAM_WINBIND_CONFIG_FILE
;
423 d
= iniparser_load(discard_const_p(char, config_file
));
425 goto config_from_pam
;
428 if (iniparser_getboolean(d
, discard_const_p(char, "global:debug"), false)) {
429 ctrl
|= WINBIND_DEBUG_ARG
;
432 if (iniparser_getboolean(d
, discard_const_p(char, "global:debug_state"), false)) {
433 ctrl
|= WINBIND_DEBUG_STATE
;
436 if (iniparser_getboolean(d
, discard_const_p(char, "global:cached_login"), false)) {
437 ctrl
|= WINBIND_CACHED_LOGIN
;
440 if (iniparser_getboolean(d
, discard_const_p(char, "global:krb5_auth"), false)) {
441 ctrl
|= WINBIND_KRB5_AUTH
;
444 if (iniparser_getboolean(d
, discard_const_p(char, "global:silent"), false)) {
445 ctrl
|= WINBIND_SILENT
;
448 if (iniparser_getstring_nonempty(d
, discard_const_p(char, "global:krb5_ccache_type"), NULL
) != NULL
) {
449 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
452 if ((iniparser_getstring_nonempty(d
, discard_const_p(char, "global:require-membership-of"), NULL
)
454 (iniparser_getstring_nonempty(d
, discard_const_p(char, "global:require_membership_of"), NULL
)
456 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
459 if (iniparser_getboolean(d
, discard_const_p(char, "global:try_first_pass"), false)) {
460 ctrl
|= WINBIND_TRY_FIRST_PASS_ARG
;
463 if (iniparser_getint(d
, discard_const_p(char, "global:warn_pwd_expire"), 0)) {
464 ctrl
|= WINBIND_WARN_PWD_EXPIRE
;
467 if (iniparser_getboolean(d
, discard_const_p(char, "global:mkhomedir"), false)) {
468 ctrl
|= WINBIND_MKHOMEDIR
;
472 /* step through arguments */
473 for (i
=argc
,v
=argv
; i
-- > 0; ++v
) {
475 /* generic options */
476 if (!strcmp(*v
,"debug"))
477 ctrl
|= WINBIND_DEBUG_ARG
;
478 else if (!strcasecmp(*v
, "debug_state"))
479 ctrl
|= WINBIND_DEBUG_STATE
;
480 else if (!strcasecmp(*v
, "silent"))
481 ctrl
|= WINBIND_SILENT
;
482 else if (!strcasecmp(*v
, "use_authtok"))
483 ctrl
|= WINBIND_USE_AUTHTOK_ARG
;
484 else if (!strcasecmp(*v
, "use_first_pass"))
485 ctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
486 else if (!strcasecmp(*v
, "try_first_pass"))
487 ctrl
|= WINBIND_TRY_FIRST_PASS_ARG
;
488 else if (!strcasecmp(*v
, "unknown_ok"))
489 ctrl
|= WINBIND_UNKNOWN_OK_ARG
;
490 else if ((type
== PAM_WINBIND_AUTHENTICATE
491 || type
== PAM_WINBIND_SETCRED
)
492 && !strncasecmp(*v
, "require_membership_of",
493 strlen("require_membership_of")))
494 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
495 else if ((type
== PAM_WINBIND_AUTHENTICATE
496 || type
== PAM_WINBIND_SETCRED
)
497 && !strncasecmp(*v
, "require-membership-of",
498 strlen("require-membership-of")))
499 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
500 else if (!strcasecmp(*v
, "krb5_auth"))
501 ctrl
|= WINBIND_KRB5_AUTH
;
502 else if (!strncasecmp(*v
, "krb5_ccache_type",
503 strlen("krb5_ccache_type")))
504 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
505 else if (!strcasecmp(*v
, "cached_login"))
506 ctrl
|= WINBIND_CACHED_LOGIN
;
507 else if (!strcasecmp(*v
, "mkhomedir"))
508 ctrl
|= WINBIND_MKHOMEDIR
;
509 else if (type
!= PAM_WINBIND_CLEANUP
) {
510 __pam_log(pamh
, ctrl
, LOG_ERR
,
511 "pam_parse: unknown option: %s", *v
);
521 iniparser_freedict(d
);
528 static int _pam_winbind_free_context(struct pwb_context
*ctx
)
535 iniparser_freedict(ctx
->dict
);
541 static int _pam_winbind_init_context(pam_handle_t
*pamh
,
545 enum pam_winbind_request_type type
,
546 struct pwb_context
**ctx_p
)
548 struct pwb_context
*r
= NULL
;
554 r
= talloc_zero(NULL
, struct pwb_context
);
559 talloc_set_destructor(r
, _pam_winbind_free_context
);
565 r
->ctrl
= _pam_parse(pamh
, flags
, argc
, argv
, type
, &r
->dict
);
568 return PAM_SYSTEM_ERR
;
576 static void _pam_winbind_cleanup_func(pam_handle_t
*pamh
,
580 int ctrl
= _pam_parse(pamh
, 0, 0, NULL
, PAM_WINBIND_CLEANUP
, NULL
);
581 if (_pam_log_is_debug_state_enabled(ctrl
)) {
582 __pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
583 "[pamh: %p] CLEAN: cleaning up PAM data %p "
584 "(error_status = %d)", pamh
, data
,
591 static const struct ntstatus_errors
{
592 const char *ntstatus_string
;
593 const char *error_string
;
594 } ntstatus_errors
[] = {
597 {"NT_STATUS_BACKUP_CONTROLLER",
598 N_("No primary Domain Controler available")},
599 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
600 N_("No domain controllers found")},
601 {"NT_STATUS_NO_LOGON_SERVERS",
602 N_("No logon servers")},
603 {"NT_STATUS_PWD_TOO_SHORT",
604 N_("Password too short")},
605 {"NT_STATUS_PWD_TOO_RECENT",
606 N_("The password of this user is too recent to change")},
607 {"NT_STATUS_PWD_HISTORY_CONFLICT",
608 N_("Password is already in password history")},
609 {"NT_STATUS_PASSWORD_EXPIRED",
610 N_("Your password has expired")},
611 {"NT_STATUS_PASSWORD_MUST_CHANGE",
612 N_("You need to change your password now")},
613 {"NT_STATUS_INVALID_WORKSTATION",
614 N_("You are not allowed to logon from this workstation")},
615 {"NT_STATUS_INVALID_LOGON_HOURS",
616 N_("You are not allowed to logon at this time")},
617 {"NT_STATUS_ACCOUNT_EXPIRED",
618 N_("Your account has expired. "
619 "Please contact your System administrator")}, /* SCNR */
620 {"NT_STATUS_ACCOUNT_DISABLED",
621 N_("Your account is disabled. "
622 "Please contact your System administrator")}, /* SCNR */
623 {"NT_STATUS_ACCOUNT_LOCKED_OUT",
624 N_("Your account has been locked. "
625 "Please contact your System administrator")}, /* SCNR */
626 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
627 N_("Invalid Trust Account")},
628 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
629 N_("Invalid Trust Account")},
630 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
631 N_("Invalid Trust Account")},
632 {"NT_STATUS_ACCESS_DENIED",
633 N_("Access is denied")},
637 static const char *_get_ntstatus_error_string(const char *nt_status_string
)
640 for (i
=0; ntstatus_errors
[i
].ntstatus_string
!= NULL
; i
++) {
641 if (!strcasecmp(ntstatus_errors
[i
].ntstatus_string
,
643 return _(ntstatus_errors
[i
].error_string
);
649 /* --- authentication management functions --- */
651 /* Attempt a conversation */
653 static int converse(const pam_handle_t
*pamh
,
655 struct pam_message
**message
,
656 struct pam_response
**response
)
659 struct pam_conv
*conv
;
661 retval
= pam_get_item(pamh
, PAM_CONV
, (const void **) &conv
);
662 if (retval
== PAM_SUCCESS
) {
663 retval
= conv
->conv(nargs
,
664 (const struct pam_message
**)message
,
665 response
, conv
->appdata_ptr
);
668 return retval
; /* propagate error status */
672 static int _make_remark(struct pwb_context
*ctx
,
676 int retval
= PAM_SUCCESS
;
678 struct pam_message
*pmsg
[1], msg
[1];
679 struct pam_response
*resp
;
681 if (ctx
->flags
& WINBIND_SILENT
) {
686 msg
[0].msg
= discard_const_p(char, text
);
687 msg
[0].msg_style
= type
;
690 retval
= converse(ctx
->pamh
, 1, pmsg
, &resp
);
693 _pam_drop_reply(resp
, 1);
698 static int _make_remark_v(struct pwb_context
*ctx
,
706 ret
= vasprintf(&var
, format
, args
);
708 _pam_log(ctx
, LOG_ERR
, "memory allocation failure");
712 ret
= _make_remark(ctx
, type
, var
);
717 static int _make_remark_format(struct pwb_context
*ctx
, int type
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
718 static int _make_remark_format(struct pwb_context
*ctx
, int type
, const char *format
, ...)
723 va_start(args
, format
);
724 ret
= _make_remark_v(ctx
, type
, format
, args
);
729 static int pam_winbind_request_log(struct pwb_context
*ctx
,
736 /* incorrect password */
737 _pam_log(ctx
, LOG_WARNING
, "user '%s' denied access "
738 "(incorrect password or invalid membership)", user
);
740 case PAM_ACCT_EXPIRED
:
741 /* account expired */
742 _pam_log(ctx
, LOG_WARNING
, "user '%s' account expired",
745 case PAM_AUTHTOK_EXPIRED
:
746 /* password expired */
747 _pam_log(ctx
, LOG_WARNING
, "user '%s' password expired",
750 case PAM_NEW_AUTHTOK_REQD
:
751 /* new password required */
752 _pam_log(ctx
, LOG_WARNING
, "user '%s' new password "
755 case PAM_USER_UNKNOWN
:
756 /* the user does not exist */
757 _pam_log_debug(ctx
, LOG_NOTICE
, "user '%s' not found",
759 if (ctx
->ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
764 /* Otherwise, the authentication looked good */
765 if (strcmp(fn
, "wbcLogonUser") == 0) {
766 _pam_log(ctx
, LOG_NOTICE
,
767 "user '%s' granted access", user
);
769 _pam_log(ctx
, LOG_NOTICE
,
770 "user '%s' OK", user
);
774 /* we don't know anything about this return value */
775 _pam_log(ctx
, LOG_ERR
,
776 "internal module error (retval = %s(%d), user = '%s')",
777 _pam_error_code_str(retval
), retval
, user
);
782 static int wbc_auth_error_to_pam_error(struct pwb_context
*ctx
,
783 struct wbcAuthErrorInfo
*e
,
785 const char *username
,
788 int ret
= PAM_AUTH_ERR
;
790 if (WBC_ERROR_IS_OK(status
)) {
791 _pam_log_debug(ctx
, LOG_DEBUG
, "request %s succeeded",
794 return pam_winbind_request_log(ctx
, ret
, username
, fn
);
798 if (e
->pam_error
!= PAM_SUCCESS
) {
799 _pam_log(ctx
, LOG_ERR
,
800 "request %s failed: %s, "
801 "PAM error: %s (%d), NTSTATUS: %s, "
802 "Error message was: %s",
804 wbcErrorString(status
),
805 _pam_error_code_str(e
->pam_error
),
810 return pam_winbind_request_log(ctx
, ret
, username
, fn
);
813 _pam_log(ctx
, LOG_ERR
, "request %s failed, but PAM error 0!", fn
);
815 ret
= PAM_SERVICE_ERR
;
816 return pam_winbind_request_log(ctx
, ret
, username
, fn
);
819 ret
= wbc_error_to_pam_error(status
);
820 return pam_winbind_request_log(ctx
, ret
, username
, fn
);
823 #if defined(HAVE_PAM_RADIO_TYPE)
824 static bool _pam_winbind_change_pwd(struct pwb_context
*ctx
)
826 struct pam_message msg
, *pmsg
;
827 struct pam_response
*resp
= NULL
;
831 msg
.msg_style
= PAM_RADIO_TYPE
;
832 msg
.msg
= _("Do you want to change your password now?");
833 ret
= converse(ctx
->pamh
, 1, &pmsg
, &resp
);
835 if (ret
== PAM_SUCCESS
) {
836 _pam_log(ctx
, LOG_CRIT
, "pam_winbind: system error!\n");
840 if (ret
!= PAM_SUCCESS
) {
843 _pam_log(ctx
, LOG_CRIT
, "Received [%s] reply from application.\n", resp
->resp
);
845 if ((resp
->resp
!= NULL
) && (strcasecmp(resp
->resp
, "yes") == 0)) {
849 _pam_drop_reply(resp
, 1);
853 static bool _pam_winbind_change_pwd(struct pwb_context
*ctx
)
860 * send a password expiry message if required
862 * @param ctx PAM winbind context.
863 * @param next_change expected (calculated) next expiry date.
864 * @param already_expired pointer to a boolean to indicate if the password is
867 * @return boolean Returns true if message has been sent, false if not.
870 static bool _pam_send_password_expiry_message(struct pwb_context
*ctx
,
874 bool *already_expired
,
878 struct tm tm_now
, tm_next_change
;
882 if (already_expired
) {
883 *already_expired
= false;
890 if (next_change
<= now
) {
891 PAM_WB_REMARK_DIRECT(ctx
, "NT_STATUS_PASSWORD_EXPIRED");
892 if (already_expired
) {
893 *already_expired
= true;
898 if ((next_change
< 0) ||
899 (next_change
> now
+ warn_pwd_expire
* SECONDS_PER_DAY
)) {
903 if ((localtime_r(&now
, &tm_now
) == NULL
) ||
904 (localtime_r(&next_change
, &tm_next_change
) == NULL
)) {
908 days
= (tm_next_change
.tm_yday
+tm_next_change
.tm_year
*365) -
909 (tm_now
.tm_yday
+tm_now
.tm_year
*365);
912 ret
= _make_remark(ctx
, PAM_TEXT_INFO
,
913 _("Your password expires today.\n"));
916 * If change_pwd and already_expired is null.
917 * We are just sending a notification message.
918 * We don't expect any response in this case.
921 if (!change_pwd
&& !already_expired
) {
926 * successfully sent the warning message.
927 * Give the user a chance to change pwd.
929 if (ret
== PAM_SUCCESS
) {
931 retval
= _pam_winbind_change_pwd(ctx
);
940 if (days
> 0 && days
< warn_pwd_expire
) {
942 ret
= _make_remark_format(ctx
, PAM_TEXT_INFO
,
943 _("Your password will expire in %d %s.\n"),
944 days
, (days
> 1) ? _("days"):_("day"));
946 * If change_pwd and already_expired is null.
947 * We are just sending a notification message.
948 * We don't expect any response in this case.
951 if (!change_pwd
&& !already_expired
) {
956 * successfully sent the warning message.
957 * Give the user a chance to change pwd.
959 if (ret
== PAM_SUCCESS
) {
961 retval
= _pam_winbind_change_pwd(ctx
);
974 * Send a warning if the password expires in the near future
976 * @param ctx PAM winbind context.
977 * @param response The full authentication response structure.
978 * @param already_expired boolean, is the pwd already expired?
983 static void _pam_warn_password_expiry(struct pwb_context
*ctx
,
984 const struct wbcAuthUserInfo
*info
,
985 const struct wbcUserPasswordPolicyInfo
*policy
,
987 bool *already_expired
,
990 time_t now
= time(NULL
);
991 time_t next_change
= 0;
993 if (!info
|| !policy
) {
997 if (already_expired
) {
998 *already_expired
= false;
1002 *change_pwd
= false;
1005 /* accounts with WBC_ACB_PWNOEXP set never receive a warning */
1006 if (info
->acct_flags
& WBC_ACB_PWNOEXP
) {
1010 /* no point in sending a warning if this is a grace logon */
1011 if (PAM_WB_GRACE_LOGON(info
->user_flags
)) {
1015 /* check if the info3 must change timestamp has been set */
1016 next_change
= info
->pass_must_change_time
;
1018 if (_pam_send_password_expiry_message(ctx
, next_change
, now
,
1025 /* now check for the global password policy */
1026 /* good catch from Ralf Haferkamp: an expiry of "never" is translated
1028 if ((policy
->expire
== (int64_t)-1) ||
1029 (policy
->expire
== 0)) {
1033 next_change
= info
->pass_last_set_time
+ policy
->expire
;
1035 if (_pam_send_password_expiry_message(ctx
, next_change
, now
,
1042 /* no warning sent */
1045 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
1048 * Append a string, making sure not to overflow and to always return a
1049 * NULL-terminated string.
1051 * @param dest Destination string buffer (must already be NULL-terminated).
1052 * @param src Source string buffer.
1053 * @param dest_buffer_size Size of dest buffer in bytes.
1055 * @return false if dest buffer is not big enough (no bytes copied), true on
1059 static bool safe_append_string(char *dest
,
1061 int dest_buffer_size
)
1064 len
= strlcat(dest
, src
, dest_buffer_size
);
1065 return (len
< dest_buffer_size
);
1069 * Convert a names into a SID string, appending it to a buffer.
1071 * @param ctx PAM winbind context.
1072 * @param user User in PAM request.
1073 * @param name Name to convert.
1074 * @param sid_list_buffer Where to append the string sid.
1075 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1077 * @return false on failure, true on success.
1079 static bool winbind_name_to_sid_string(struct pwb_context
*ctx
,
1082 char *sid_list_buffer
,
1083 int sid_list_buffer_size
)
1085 char sid_string
[WBC_SID_STRING_BUFLEN
];
1088 if (IS_SID_STRING(name
)) {
1089 strlcpy(sid_string
, name
, sizeof(sid_string
));
1092 struct wbcDomainSid sid
;
1093 enum wbcSidType type
;
1095 _pam_log_debug(ctx
, LOG_DEBUG
,
1096 "no sid given, looking up: %s\n", name
);
1098 wbc_status
= wbcLookupName("", name
, &sid
, &type
);
1099 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1100 _pam_log(ctx
, LOG_INFO
,
1101 "could not lookup name: %s\n", name
);
1105 wbcSidToStringBuf(&sid
, sid_string
, sizeof(sid_string
));
1108 if (!safe_append_string(sid_list_buffer
, sid_string
,
1109 sid_list_buffer_size
)) {
1116 * Convert a list of names into a list of sids.
1118 * @param ctx PAM winbind context.
1119 * @param user User in PAM request.
1120 * @param name_list List of names or string sids, separated by commas.
1121 * @param sid_list_buffer Where to put the list of string sids.
1122 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1124 * @return false on failure, true on success.
1126 static bool winbind_name_list_to_sid_string_list(struct pwb_context
*ctx
,
1128 const char *name_list
,
1129 char *sid_list_buffer
,
1130 int sid_list_buffer_size
)
1132 bool result
= false;
1133 char *current_name
= NULL
;
1134 const char *search_location
;
1138 if (sid_list_buffer_size
> 0) {
1139 sid_list_buffer
[0] = 0;
1142 search_location
= name_list
;
1143 while ((comma
= strchr(search_location
, ',')) != NULL
) {
1144 current_name
= strndup(search_location
,
1145 comma
- search_location
);
1146 if (NULL
== current_name
) {
1150 if (!winbind_name_to_sid_string(ctx
, user
,
1153 sid_list_buffer_size
)) {
1155 * If one group name failed, we must not fail
1156 * the authentication totally, continue with
1157 * the following group names. If user belongs to
1158 * one of the valid groups, we must allow it
1162 _pam_log(ctx
, LOG_INFO
, "cannot convert group %s to sid, "
1163 "check if group %s is valid group.", current_name
,
1165 _make_remark_format(ctx
, PAM_TEXT_INFO
, _("Cannot convert group %s "
1166 "to sid, please contact your administrator to see "
1167 "if group %s is valid."), current_name
, current_name
);
1168 SAFE_FREE(current_name
);
1169 search_location
= comma
+ 1;
1173 SAFE_FREE(current_name
);
1175 if (!safe_append_string(sid_list_buffer
, ",",
1176 sid_list_buffer_size
)) {
1180 search_location
= comma
+ 1;
1183 if (!winbind_name_to_sid_string(ctx
, user
, search_location
,
1185 sid_list_buffer_size
)) {
1186 _pam_log(ctx
, LOG_INFO
, "cannot convert group %s to sid, "
1187 "check if group %s is valid group.", search_location
,
1189 _make_remark_format(ctx
, PAM_TEXT_INFO
, _("Cannot convert group %s "
1190 "to sid, please contact your administrator to see "
1191 "if group %s is valid."), search_location
, search_location
);
1193 /* If no valid groups were converted we should fail outright */
1194 if (name_list
!= NULL
&& strlen(sid_list_buffer
) == 0) {
1199 * The lookup of the last name failed..
1200 * It results in require_member_of_sid ends with ','
1201 * It is malformated parameter here, overwrite the last ','.
1203 len
= strlen(sid_list_buffer
);
1204 if ((len
!= 0) && (sid_list_buffer
[len
- 1] == ',')) {
1205 sid_list_buffer
[len
- 1] = '\0';
1212 SAFE_FREE(current_name
);
1217 * put krb5ccname variable into environment
1219 * @param ctx PAM winbind context.
1220 * @param krb5ccname env variable retrieved from winbindd.
1225 static void _pam_setup_krb5_env(struct pwb_context
*ctx
,
1226 struct wbcLogonUserInfo
*info
)
1231 const char *krb5ccname
= NULL
;
1233 if (off(ctx
->ctrl
, WINBIND_KRB5_AUTH
)) {
1241 for (i
=0; i
< info
->num_blobs
; i
++) {
1242 if (strcasecmp(info
->blobs
[i
].name
, "krb5ccname") == 0) {
1243 krb5ccname
= (const char *)info
->blobs
[i
].blob
.data
;
1248 if (!krb5ccname
|| (strlen(krb5ccname
) == 0)) {
1252 _pam_log_debug(ctx
, LOG_DEBUG
,
1253 "request returned KRB5CCNAME: %s", krb5ccname
);
1255 if (asprintf(&var
, "KRB5CCNAME=%s", krb5ccname
) == -1) {
1259 ret
= pam_putenv(ctx
->pamh
, var
);
1261 _pam_log(ctx
, LOG_ERR
,
1262 "failed to set KRB5CCNAME to %s: %s",
1263 var
, pam_strerror(ctx
->pamh
, ret
));
1269 * Copy unix username if available (further processed in PAM).
1271 * @param ctx PAM winbind context
1272 * @param user_ret A pointer that holds a pointer to a string
1273 * @param unix_username A username
1278 static void _pam_setup_unix_username(struct pwb_context
*ctx
,
1280 struct wbcLogonUserInfo
*info
)
1282 const char *unix_username
= NULL
;
1285 if (!user_ret
|| !info
) {
1289 for (i
=0; i
< info
->num_blobs
; i
++) {
1290 if (strcasecmp(info
->blobs
[i
].name
, "unix_username") == 0) {
1291 unix_username
= (const char *)info
->blobs
[i
].blob
.data
;
1296 if (!unix_username
|| !unix_username
[0]) {
1300 *user_ret
= strdup(unix_username
);
1304 * Set string into the PAM stack.
1306 * @param ctx PAM winbind context.
1307 * @param data_name Key name for pam_set_data.
1308 * @param value String value.
1313 static void _pam_set_data_string(struct pwb_context
*ctx
,
1314 const char *data_name
,
1319 if (!data_name
|| !value
|| (strlen(data_name
) == 0) ||
1320 (strlen(value
) == 0)) {
1324 ret
= pam_set_data(ctx
->pamh
, data_name
, talloc_strdup(NULL
, value
),
1325 _pam_winbind_cleanup_func
);
1327 _pam_log_debug(ctx
, LOG_DEBUG
,
1328 "Could not set data %s: %s\n",
1329 data_name
, pam_strerror(ctx
->pamh
, ret
));
1334 * Set info3 strings into the PAM stack.
1336 * @param ctx PAM winbind context.
1337 * @param data_name Key name for pam_set_data.
1338 * @param value String value.
1343 static void _pam_set_data_info3(struct pwb_context
*ctx
,
1344 const struct wbcAuthUserInfo
*info
)
1346 _pam_set_data_string(ctx
, PAM_WINBIND_HOMEDIR
,
1347 info
->home_directory
);
1348 _pam_set_data_string(ctx
, PAM_WINBIND_LOGONSCRIPT
,
1349 info
->logon_script
);
1350 _pam_set_data_string(ctx
, PAM_WINBIND_LOGONSERVER
,
1351 info
->logon_server
);
1352 _pam_set_data_string(ctx
, PAM_WINBIND_PROFILEPATH
,
1353 info
->profile_path
);
1357 * Free info3 strings in the PAM stack.
1359 * @param pamh PAM handle
1364 static void _pam_free_data_info3(pam_handle_t
*pamh
)
1366 pam_set_data(pamh
, PAM_WINBIND_HOMEDIR
, NULL
, NULL
);
1367 pam_set_data(pamh
, PAM_WINBIND_LOGONSCRIPT
, NULL
, NULL
);
1368 pam_set_data(pamh
, PAM_WINBIND_LOGONSERVER
, NULL
, NULL
);
1369 pam_set_data(pamh
, PAM_WINBIND_PROFILEPATH
, NULL
, NULL
);
1373 * Send PAM_ERROR_MSG for cached or grace logons.
1375 * @param ctx PAM winbind context.
1376 * @param username User in PAM request.
1377 * @param info3_user_flgs Info3 flags containing logon type bits.
1382 static void _pam_warn_logon_type(struct pwb_context
*ctx
,
1383 const char *username
,
1384 uint32_t info3_user_flgs
)
1386 /* inform about logon type */
1387 if (PAM_WB_GRACE_LOGON(info3_user_flgs
)) {
1389 _make_remark(ctx
, PAM_ERROR_MSG
,
1391 "Please change your password as soon you're "
1393 _pam_log_debug(ctx
, LOG_DEBUG
,
1394 "User %s logged on using grace logon\n",
1397 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs
)) {
1399 _make_remark(ctx
, PAM_ERROR_MSG
,
1400 _("Domain Controller unreachable, "
1401 "using cached credentials instead. "
1402 "Network resources may be unavailable"));
1403 _pam_log_debug(ctx
, LOG_DEBUG
,
1404 "User %s logged on using cached credentials\n",
1410 * Send PAM_ERROR_MSG for krb5 errors.
1412 * @param ctx PAM winbind context.
1413 * @param username User in PAM request.
1414 * @param info3_user_flgs Info3 flags containing logon type bits.
1419 static void _pam_warn_krb5_failure(struct pwb_context
*ctx
,
1420 const char *username
,
1421 uint32_t info3_user_flgs
)
1423 if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs
)) {
1424 _make_remark(ctx
, PAM_ERROR_MSG
,
1425 _("Failed to establish your Kerberos Ticket cache "
1426 "due time differences\n"
1427 "with the domain controller. "
1428 "Please verify the system time.\n"));
1429 _pam_log_debug(ctx
, LOG_DEBUG
,
1430 "User %s: Clock skew when getting Krb5 TGT\n",
1435 static bool _pam_check_remark_auth_err(struct pwb_context
*ctx
,
1436 const struct wbcAuthErrorInfo
*e
,
1437 const char *nt_status_string
,
1440 const char *ntstatus
= NULL
;
1441 const char *error_string
= NULL
;
1443 if (!e
|| !pam_err
) {
1447 ntstatus
= e
->nt_string
;
1452 if (strcasecmp(ntstatus
, nt_status_string
) == 0) {
1454 error_string
= _get_ntstatus_error_string(nt_status_string
);
1456 _make_remark(ctx
, PAM_ERROR_MSG
, error_string
);
1457 *pam_err
= e
->pam_error
;
1461 if (e
->display_string
) {
1462 _make_remark(ctx
, PAM_ERROR_MSG
, _(e
->display_string
));
1463 *pam_err
= e
->pam_error
;
1467 _make_remark(ctx
, PAM_ERROR_MSG
, nt_status_string
);
1468 *pam_err
= e
->pam_error
;
1477 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
1479 * @param i The wbcUserPasswordPolicyInfo struct.
1481 * @return string (caller needs to talloc_free).
1484 static char *_pam_compose_pwd_restriction_string(struct pwb_context
*ctx
,
1485 struct wbcUserPasswordPolicyInfo
*i
)
1493 str
= talloc_asprintf(ctx
, _("Your password "));
1498 if (i
->min_length_password
> 0) {
1499 str
= talloc_asprintf_append(str
,
1500 _("must be at least %d characters; "),
1501 i
->min_length_password
);
1507 if (i
->password_history
> 0) {
1508 str
= talloc_asprintf_append(str
,
1509 _("cannot repeat any of your previous %d "
1511 i
->password_history
);
1517 if (i
->password_properties
& WBC_DOMAIN_PASSWORD_COMPLEX
) {
1518 str
= talloc_asprintf_append(str
,
1519 _("must contain capitals, numerals "
1521 "and cannot contain your account "
1528 str
= talloc_asprintf_append(str
,
1529 _("Please type a different password. "
1530 "Type a password which meets these requirements in "
1531 "both text boxes."));
1543 static int _pam_create_homedir(struct pwb_context
*ctx
,
1544 const char *dirname
,
1549 if (stat(dirname
, &sbuf
) == 0) {
1553 if (mkdir(dirname
, mode
) != 0) {
1555 _make_remark_format(ctx
, PAM_TEXT_INFO
,
1556 _("Creating directory: %s failed: %s"),
1557 dirname
, strerror(errno
));
1558 _pam_log(ctx
, LOG_ERR
, "could not create dir: %s (%s)",
1559 dirname
, strerror(errno
));
1560 return PAM_PERM_DENIED
;
1566 static int _pam_chown_homedir(struct pwb_context
*ctx
,
1567 const char *dirname
,
1571 if (chown(dirname
, uid
, gid
) != 0) {
1572 _pam_log(ctx
, LOG_ERR
, "failed to chown user homedir: %s (%s)",
1573 dirname
, strerror(errno
));
1574 return PAM_PERM_DENIED
;
1580 static int _pam_mkhomedir(struct pwb_context
*ctx
)
1582 struct passwd
*pwd
= NULL
;
1584 char *create_dir
= NULL
;
1585 char *user_dir
= NULL
;
1587 const char *username
;
1589 char *safe_ptr
= NULL
;
1592 /* Get the username */
1593 ret
= pam_get_user(ctx
->pamh
, &username
, NULL
);
1594 if ((ret
!= PAM_SUCCESS
) || (!username
)) {
1595 _pam_log_debug(ctx
, LOG_DEBUG
, "can not get the username");
1596 return PAM_SERVICE_ERR
;
1599 pwd
= getpwnam(username
);
1601 _pam_log_debug(ctx
, LOG_DEBUG
, "can not get the username");
1602 return PAM_USER_UNKNOWN
;
1604 _pam_log_debug(ctx
, LOG_DEBUG
, "homedir is: %s", pwd
->pw_dir
);
1606 ret
= _pam_create_homedir(ctx
, pwd
->pw_dir
, 0700);
1607 if (ret
== PAM_SUCCESS
) {
1608 ret
= _pam_chown_homedir(ctx
, pwd
->pw_dir
,
1613 if (ret
== PAM_SUCCESS
) {
1617 /* maybe we need to create parent dirs */
1618 create_dir
= talloc_strdup(ctx
, "/");
1623 /* find final directory */
1624 user_dir
= strrchr(pwd
->pw_dir
, '/');
1630 _pam_log(ctx
, LOG_DEBUG
, "final directory: %s", user_dir
);
1634 while ((token
= strtok_r(p
, "/", &safe_ptr
)) != NULL
) {
1640 _pam_log_debug(ctx
, LOG_DEBUG
, "token is %s", token
);
1642 create_dir
= talloc_asprintf_append(create_dir
, "%s/", token
);
1646 _pam_log_debug(ctx
, LOG_DEBUG
, "current_dir is %s", create_dir
);
1648 if (strcmp(token
, user_dir
) == 0) {
1649 _pam_log_debug(ctx
, LOG_DEBUG
, "assuming last directory: %s", token
);
1653 ret
= _pam_create_homedir(ctx
, create_dir
, mode
);
1659 return _pam_chown_homedir(ctx
, create_dir
,
1664 /* talk to winbindd */
1665 static int winbind_auth_request(struct pwb_context
*ctx
,
1670 const int warn_pwd_expire
,
1671 struct wbcAuthErrorInfo
**p_error
,
1672 struct wbcLogonUserInfo
**p_info
,
1673 struct wbcUserPasswordPolicyInfo
**p_policy
,
1674 time_t *pwd_last_set
,
1679 struct wbcLogonUserParams logon
;
1680 char membership_of
[1024];
1681 uid_t user_uid
= -1;
1682 uint32_t flags
= WBFLAG_PAM_INFO3_TEXT
|
1683 WBFLAG_PAM_GET_PWD_POLICY
;
1685 struct wbcLogonUserInfo
*info
= NULL
;
1686 struct wbcAuthUserInfo
*user_info
= NULL
;
1687 struct wbcAuthErrorInfo
*error
= NULL
;
1688 struct wbcUserPasswordPolicyInfo
*policy
= NULL
;
1690 int ret
= PAM_AUTH_ERR
;
1692 const char *codes
[] = {
1693 "NT_STATUS_PASSWORD_EXPIRED",
1694 "NT_STATUS_PASSWORD_MUST_CHANGE",
1695 "NT_STATUS_INVALID_WORKSTATION",
1696 "NT_STATUS_INVALID_LOGON_HOURS",
1697 "NT_STATUS_ACCOUNT_EXPIRED",
1698 "NT_STATUS_ACCOUNT_DISABLED",
1699 "NT_STATUS_ACCOUNT_LOCKED_OUT",
1700 "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
1701 "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
1702 "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
1703 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1704 "NT_STATUS_NO_LOGON_SERVERS",
1705 "NT_STATUS_WRONG_PASSWORD",
1706 "NT_STATUS_ACCESS_DENIED"
1713 /* Krb5 auth always has to go against the KDC of the user's realm */
1715 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1716 flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
1719 if (ctx
->ctrl
& (WINBIND_KRB5_AUTH
|WINBIND_CACHED_LOGIN
)) {
1720 struct passwd
*pwd
= NULL
;
1722 pwd
= getpwnam(user
);
1724 return PAM_USER_UNKNOWN
;
1726 user_uid
= pwd
->pw_uid
;
1729 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1731 _pam_log_debug(ctx
, LOG_DEBUG
,
1732 "enabling krb5 login flag\n");
1734 flags
|= WBFLAG_PAM_KRB5
|
1735 WBFLAG_PAM_FALLBACK_AFTER_KRB5
;
1738 if (ctx
->ctrl
& WINBIND_CACHED_LOGIN
) {
1739 _pam_log_debug(ctx
, LOG_DEBUG
,
1740 "enabling cached login flag\n");
1741 flags
|= WBFLAG_PAM_CACHED_LOGIN
;
1746 flags
|= WBFLAG_PAM_UNIX_NAME
;
1749 if (cctype
!= NULL
) {
1750 _pam_log_debug(ctx
, LOG_DEBUG
,
1751 "enabling request for a %s krb5 ccache\n",
1755 if (member
!= NULL
) {
1757 ZERO_STRUCT(membership_of
);
1759 if (!winbind_name_list_to_sid_string_list(ctx
, user
, member
,
1761 sizeof(membership_of
))) {
1762 _pam_log_debug(ctx
, LOG_ERR
,
1763 "failed to serialize membership of sid "
1764 "\"%s\"\n", member
);
1765 return PAM_AUTH_ERR
;
1771 logon
.username
= user
;
1772 logon
.password
= pass
;
1775 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1779 discard_const_p(uint8_t, cctype
),
1781 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1786 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1792 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1796 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1800 (uint8_t *)&user_uid
,
1802 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1807 wbc_status
= wbcAddNamedBlob(&logon
.num_blobs
,
1811 (uint8_t *)membership_of
,
1812 sizeof(membership_of
));
1813 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1818 wbc_status
= wbcLogonUser(&logon
, &info
, &error
, &policy
);
1819 ret
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
1820 user
, "wbcLogonUser");
1821 wbcFreeMemory(logon
.blobs
);
1824 if (info
&& info
->info
) {
1825 user_info
= info
->info
;
1828 if (pwd_last_set
&& user_info
) {
1829 *pwd_last_set
= user_info
->pass_last_set_time
;
1832 if (p_info
&& info
) {
1836 if (p_policy
&& policy
) {
1840 if (p_error
&& error
) {
1841 /* We want to process the error in the caller. */
1846 for (i
=0; i
<ARRAY_SIZE(codes
); i
++) {
1848 if (_pam_check_remark_auth_err(ctx
, error
, codes
[i
], &_ret
)) {
1854 if ((ret
== PAM_SUCCESS
) && user_info
&& policy
&& info
) {
1856 bool already_expired
= false;
1857 bool change_pwd
= false;
1859 /* warn a user if the password is about to expire soon */
1860 _pam_warn_password_expiry(ctx
, user_info
, policy
,
1865 if (already_expired
== true) {
1867 SMB_TIME_T last_set
= user_info
->pass_last_set_time
;
1869 _pam_log_debug(ctx
, LOG_DEBUG
,
1870 "Password has expired "
1871 "(Password was last set: %lld, "
1872 "the policy says it should expire here "
1873 "%lld (now it's: %ld))\n",
1874 (long long int)last_set
,
1875 (long long int)last_set
+
1879 return PAM_AUTHTOK_EXPIRED
;
1883 ret
= PAM_NEW_AUTHTOK_REQD
;
1887 /* inform about logon type */
1888 _pam_warn_logon_type(ctx
, user
, user_info
->user_flags
);
1890 /* inform about krb5 failures */
1891 _pam_warn_krb5_failure(ctx
, user
, user_info
->user_flags
);
1893 /* set some info3 info for other modules in the stack */
1894 _pam_set_data_info3(ctx
, user_info
);
1896 /* put krb5ccname into env */
1897 _pam_setup_krb5_env(ctx
, info
);
1899 /* If winbindd returned a username, return the pointer to it
1901 _pam_setup_unix_username(ctx
, user_ret
, info
);
1905 wbcFreeMemory(logon
.blobs
);
1906 if (info
&& info
->blobs
&& !p_info
) {
1907 wbcFreeMemory(info
->blobs
);
1909 if (error
&& !p_error
) {
1910 wbcFreeMemory(error
);
1912 if (info
&& !p_info
) {
1913 wbcFreeMemory(info
);
1915 if (policy
&& !p_policy
) {
1916 wbcFreeMemory(policy
);
1922 /* talk to winbindd */
1923 static int winbind_chauthtok_request(struct pwb_context
*ctx
,
1925 const char *oldpass
,
1926 const char *newpass
,
1927 time_t pwd_last_set
)
1930 struct wbcChangePasswordParams params
;
1931 struct wbcAuthErrorInfo
*error
= NULL
;
1932 struct wbcUserPasswordPolicyInfo
*policy
= NULL
;
1933 enum wbcPasswordChangeRejectReason reject_reason
= -1;
1937 const char *codes
[] = {
1938 "NT_STATUS_BACKUP_CONTROLLER",
1939 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1940 "NT_STATUS_NO_LOGON_SERVERS",
1941 "NT_STATUS_ACCESS_DENIED",
1942 "NT_STATUS_PWD_TOO_SHORT", /* TODO: tell the min pwd length ? */
1943 "NT_STATUS_PWD_TOO_RECENT", /* TODO: tell the minage ? */
1944 "NT_STATUS_PWD_HISTORY_CONFLICT" /* TODO: tell the history length ? */
1946 int ret
= PAM_AUTH_ERR
;
1948 ZERO_STRUCT(params
);
1950 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1951 flags
|= WBFLAG_PAM_KRB5
|
1952 WBFLAG_PAM_CONTACT_TRUSTDOM
;
1955 if (ctx
->ctrl
& WINBIND_CACHED_LOGIN
) {
1956 flags
|= WBFLAG_PAM_CACHED_LOGIN
;
1959 params
.account_name
= user
;
1960 params
.level
= WBC_CHANGE_PASSWORD_LEVEL_PLAIN
;
1961 params
.old_password
.plaintext
= oldpass
;
1962 params
.new_password
.plaintext
= newpass
;
1963 params
.flags
= flags
;
1965 wbc_status
= wbcChangeUserPasswordEx(¶ms
, &error
, &reject_reason
, &policy
);
1966 ret
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
1967 user
, "wbcChangeUserPasswordEx");
1969 if (WBC_ERROR_IS_OK(wbc_status
)) {
1970 _pam_log(ctx
, LOG_NOTICE
,
1971 "user '%s' password changed", user
);
1976 wbcFreeMemory(policy
);
1980 for (i
=0; i
<ARRAY_SIZE(codes
); i
++) {
1982 if (_pam_check_remark_auth_err(ctx
, error
, codes
[i
], &_ret
)) {
1988 if (!strcasecmp(error
->nt_string
,
1989 "NT_STATUS_PASSWORD_RESTRICTION")) {
1991 char *pwd_restriction_string
= NULL
;
1992 SMB_TIME_T min_pwd_age
= 0;
1995 min_pwd_age
= policy
->min_passwordage
;
1998 /* FIXME: avoid to send multiple PAM messages after another */
1999 switch (reject_reason
) {
2002 case WBC_PWD_CHANGE_NO_ERROR
:
2003 if ((min_pwd_age
> 0) &&
2004 (pwd_last_set
+ min_pwd_age
> time(NULL
))) {
2005 PAM_WB_REMARK_DIRECT(ctx
,
2006 "NT_STATUS_PWD_TOO_RECENT");
2009 case WBC_PWD_CHANGE_PASSWORD_TOO_SHORT
:
2010 PAM_WB_REMARK_DIRECT(ctx
,
2011 "NT_STATUS_PWD_TOO_SHORT");
2013 case WBC_PWD_CHANGE_PWD_IN_HISTORY
:
2014 PAM_WB_REMARK_DIRECT(ctx
,
2015 "NT_STATUS_PWD_HISTORY_CONFLICT");
2017 case WBC_PWD_CHANGE_NOT_COMPLEX
:
2018 _make_remark(ctx
, PAM_ERROR_MSG
,
2019 _("Password does not meet "
2020 "complexity requirements"));
2023 _pam_log_debug(ctx
, LOG_DEBUG
,
2024 "unknown password change "
2025 "reject reason: %d",
2030 pwd_restriction_string
=
2031 _pam_compose_pwd_restriction_string(ctx
, policy
);
2032 if (pwd_restriction_string
) {
2033 _make_remark(ctx
, PAM_ERROR_MSG
,
2034 pwd_restriction_string
);
2035 TALLOC_FREE(pwd_restriction_string
);
2039 wbcFreeMemory(error
);
2040 wbcFreeMemory(policy
);
2046 * Checks if a user has an account
2049 * 1 = User not found
2053 static int valid_user(struct pwb_context
*ctx
,
2056 /* check not only if the user is available over NSS calls, also make
2057 * sure it's really a winbind user, this is important when stacking PAM
2058 * modules in the 'account' or 'password' facility. */
2061 struct passwd
*pwd
= NULL
;
2062 struct passwd
*wb_pwd
= NULL
;
2064 pwd
= getpwnam(user
);
2069 wbc_status
= wbcGetpwnam(user
, &wb_pwd
);
2070 wbcFreeMemory(wb_pwd
);
2071 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2072 _pam_log(ctx
, LOG_DEBUG
, "valid_user: wbcGetpwnam gave %s\n",
2073 wbcErrorString(wbc_status
));
2076 switch (wbc_status
) {
2077 case WBC_ERR_UNKNOWN_USER
:
2078 /* match other insane libwbclient return codes */
2079 case WBC_ERR_WINBIND_NOT_AVAILABLE
:
2080 case WBC_ERR_DOMAIN_NOT_FOUND
:
2082 case WBC_ERR_SUCCESS
:
2090 static char *_pam_delete(register char *xx
)
2098 * obtain a password from the user
2101 static int _winbind_read_password(struct pwb_context
*ctx
,
2103 const char *comment
,
2104 const char *prompt1
,
2105 const char *prompt2
,
2113 _pam_log(ctx
, LOG_DEBUG
, "getting password (0x%08x)", ctrl
);
2116 * make sure nothing inappropriate gets returned
2119 *pass
= token
= NULL
;
2122 * which authentication token are we getting?
2125 if (on(WINBIND__OLD_PASSWORD
, ctrl
)) {
2126 authtok_flag
= PAM_OLDAUTHTOK
;
2128 authtok_flag
= PAM_AUTHTOK
;
2132 * should we obtain the password from a PAM item ?
2135 if (on(WINBIND_TRY_FIRST_PASS_ARG
, ctrl
) ||
2136 on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
2137 retval
= pam_get_item(ctx
->pamh
,
2139 (const void **) &item
);
2140 if (retval
!= PAM_SUCCESS
) {
2142 _pam_log(ctx
, LOG_ALERT
,
2143 "pam_get_item returned error "
2144 "to unix-read-password");
2146 } else if (item
!= NULL
) { /* we have a password! */
2149 _pam_log(ctx
, LOG_DEBUG
,
2150 "pam_get_item returned a password");
2152 } else if (on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
2153 return PAM_AUTHTOK_RECOVER_ERR
; /* didn't work */
2154 } else if (on(WINBIND_USE_AUTHTOK_ARG
, ctrl
)
2155 && off(WINBIND__OLD_PASSWORD
, ctrl
)) {
2156 return PAM_AUTHTOK_RECOVER_ERR
;
2160 * getting here implies we will have to get the password from the
2165 struct pam_message msg
[3], *pmsg
[3];
2166 struct pam_response
*resp
;
2169 /* prepare to converse */
2171 if (comment
!= NULL
&& off(ctrl
, WINBIND_SILENT
)) {
2173 msg
[0].msg_style
= PAM_TEXT_INFO
;
2174 msg
[0].msg
= discard_const_p(char, comment
);
2181 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
2182 msg
[i
++].msg
= discard_const_p(char, prompt1
);
2185 if (prompt2
!= NULL
) {
2187 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
2188 msg
[i
++].msg
= discard_const_p(char, prompt2
);
2191 /* so call the conversation expecting i responses */
2193 retval
= converse(ctx
->pamh
, i
, pmsg
, &resp
);
2195 if (retval
== PAM_SUCCESS
) {
2196 retval
= PAM_AUTHTOK_RECOVER_ERR
;
2200 if (retval
!= PAM_SUCCESS
) {
2201 _pam_drop_reply(resp
, i
);
2205 /* interpret the response */
2207 token
= x_strdup(resp
[i
- replies
].resp
);
2209 _pam_log(ctx
, LOG_NOTICE
,
2210 "could not recover "
2211 "authentication token");
2212 retval
= PAM_AUTHTOK_RECOVER_ERR
;
2217 /* verify that password entered correctly */
2218 if (!resp
[i
- 1].resp
||
2219 strcmp(token
, resp
[i
- 1].resp
)) {
2220 _pam_delete(token
); /* mistyped */
2221 retval
= PAM_AUTHTOK_RECOVER_ERR
;
2222 _make_remark(ctx
, PAM_ERROR_MSG
,
2228 * tidy up the conversation (resp_retcode) is ignored
2229 * -- what is it for anyway? AGM
2231 _pam_drop_reply(resp
, i
);
2235 if (retval
!= PAM_SUCCESS
) {
2236 _pam_log_debug(ctx
, LOG_DEBUG
,
2237 "unable to obtain a password");
2240 /* 'token' is the entered password */
2242 /* we store this password as an item */
2244 retval
= pam_set_item(ctx
->pamh
, authtok_flag
, token
);
2245 _pam_delete(token
); /* clean it up */
2246 if (retval
!= PAM_SUCCESS
||
2247 (retval
= pam_get_item(ctx
->pamh
, authtok_flag
, (const void **) &item
)) != PAM_SUCCESS
) {
2249 _pam_log(ctx
, LOG_CRIT
, "error manipulating password");
2255 item
= NULL
; /* break link to password */
2260 static const char *get_conf_item_string(struct pwb_context
*ctx
,
2265 const char *parm_opt
= NULL
;
2267 if (!(ctx
->ctrl
& config_flag
)) {
2271 /* let the pam opt take precedence over the pam_winbind.conf option */
2272 for (i
=0; i
<ctx
->argc
; i
++) {
2274 if ((strncmp(ctx
->argv
[i
], item
, strlen(item
)) == 0)) {
2277 if ((p
= strchr(ctx
->argv
[i
], '=')) == NULL
) {
2278 _pam_log(ctx
, LOG_INFO
,
2279 "no \"=\" delimiter for \"%s\" found\n",
2283 _pam_log_debug(ctx
, LOG_INFO
,
2284 "PAM config: %s '%s'\n", item
, p
+1);
2292 key
= talloc_asprintf(ctx
, "global:%s", item
);
2297 parm_opt
= iniparser_getstring_nonempty(ctx
->dict
, key
, NULL
);
2300 _pam_log_debug(ctx
, LOG_INFO
, "CONFIG file: %s '%s'\n",
2307 static int get_config_item_int(struct pwb_context
*ctx
,
2311 int i
, parm_opt
= -1;
2313 if (!(ctx
->ctrl
& config_flag
)) {
2317 /* let the pam opt take precedence over the pam_winbind.conf option */
2318 for (i
= 0; i
< ctx
->argc
; i
++) {
2320 if ((strncmp(ctx
->argv
[i
], item
, strlen(item
)) == 0)) {
2323 if ((p
= strchr(ctx
->argv
[i
], '=')) == NULL
) {
2324 _pam_log(ctx
, LOG_INFO
,
2325 "no \"=\" delimiter for \"%s\" found\n",
2329 parm_opt
= atoi(p
+ 1);
2330 _pam_log_debug(ctx
, LOG_INFO
,
2331 "PAM config: %s '%d'\n",
2340 key
= talloc_asprintf(ctx
, "global:%s", item
);
2345 parm_opt
= iniparser_getint(ctx
->dict
, key
, -1);
2348 _pam_log_debug(ctx
, LOG_INFO
,
2349 "CONFIG file: %s '%d'\n",
2356 static const char *get_krb5_cc_type_from_config(struct pwb_context
*ctx
)
2358 return get_conf_item_string(ctx
, "krb5_ccache_type",
2359 WINBIND_KRB5_CCACHE_TYPE
);
2362 static const char *get_member_from_config(struct pwb_context
*ctx
)
2364 const char *ret
= NULL
;
2365 ret
= get_conf_item_string(ctx
, "require_membership_of",
2366 WINBIND_REQUIRED_MEMBERSHIP
);
2370 return get_conf_item_string(ctx
, "require-membership-of",
2371 WINBIND_REQUIRED_MEMBERSHIP
);
2374 static int get_warn_pwd_expire_from_config(struct pwb_context
*ctx
)
2377 ret
= get_config_item_int(ctx
, "warn_pwd_expire",
2378 WINBIND_WARN_PWD_EXPIRE
);
2379 /* no or broken setting */
2381 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES
;
2387 * Retrieve the winbind separator.
2389 * @param ctx PAM winbind context.
2391 * @return string separator character. NULL on failure.
2394 static char winbind_get_separator(struct pwb_context
*ctx
)
2397 static struct wbcInterfaceDetails
*details
= NULL
;
2399 wbc_status
= wbcInterfaceDetails(&details
);
2400 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2401 _pam_log(ctx
, LOG_ERR
,
2402 "Could not retrieve winbind interface details: %s",
2403 wbcErrorString(wbc_status
));
2411 return details
->winbind_separator
;
2416 * Convert a upn to a name.
2418 * @param ctx PAM winbind context.
2419 * @param upn USer UPN to be trabslated.
2421 * @return converted name. NULL pointer on failure. Caller needs to free.
2424 static char* winbind_upn_to_username(struct pwb_context
*ctx
,
2428 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
2429 struct wbcDomainSid sid
;
2430 enum wbcSidType type
;
2431 char *domain
= NULL
;
2435 /* This cannot work when the winbind separator = @ */
2437 sep
= winbind_get_separator(ctx
);
2438 if (!sep
|| sep
== '@') {
2442 name
= talloc_strdup(ctx
, upn
);
2446 if ((p
= strchr(name
, '@')) != NULL
) {
2451 /* Convert the UPN to a SID */
2453 wbc_status
= wbcLookupName(domain
, name
, &sid
, &type
);
2454 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2458 /* Convert the the SID back to the sAMAccountName */
2460 wbc_status
= wbcLookupSid(&sid
, &domain
, &name
, &type
);
2461 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2465 return talloc_asprintf(ctx
, "%s%c%s", domain
, sep
, name
);
2468 static int _pam_delete_cred(pam_handle_t
*pamh
, int flags
,
2469 int argc
, enum pam_winbind_request_type type
,
2472 int retval
= PAM_SUCCESS
;
2473 struct pwb_context
*ctx
= NULL
;
2474 struct wbcLogoffUserParams logoff
;
2475 struct wbcAuthErrorInfo
*error
= NULL
;
2477 wbcErr wbc_status
= WBC_ERR_SUCCESS
;
2479 ZERO_STRUCT(logoff
);
2481 retval
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, type
, &ctx
);
2486 _PAM_LOG_FUNCTION_ENTER("_pam_delete_cred", ctx
);
2488 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
2490 /* destroy the ccache here */
2492 uint32_t wbc_flags
= 0;
2493 const char *ccname
= NULL
;
2494 struct passwd
*pwd
= NULL
;
2496 retval
= pam_get_user(pamh
, &user
, _("Username: "));
2498 _pam_log(ctx
, LOG_ERR
,
2499 "could not identify user");
2504 _pam_log(ctx
, LOG_ERR
,
2505 "username was NULL!");
2506 retval
= PAM_USER_UNKNOWN
;
2510 _pam_log_debug(ctx
, LOG_DEBUG
,
2511 "username [%s] obtained", user
);
2513 ccname
= pam_getenv(pamh
, "KRB5CCNAME");
2514 if (ccname
== NULL
) {
2515 _pam_log_debug(ctx
, LOG_DEBUG
,
2516 "user has no KRB5CCNAME environment");
2519 pwd
= getpwnam(user
);
2521 retval
= PAM_USER_UNKNOWN
;
2525 wbc_flags
= WBFLAG_PAM_KRB5
|
2526 WBFLAG_PAM_CONTACT_TRUSTDOM
;
2528 logoff
.username
= user
;
2531 wbc_status
= wbcAddNamedBlob(&logoff
.num_blobs
,
2535 discard_const_p(uint8_t, ccname
),
2537 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2542 wbc_status
= wbcAddNamedBlob(&logoff
.num_blobs
,
2546 (uint8_t *)&wbc_flags
,
2548 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2552 wbc_status
= wbcAddNamedBlob(&logoff
.num_blobs
,
2556 (uint8_t *)&pwd
->pw_uid
,
2557 sizeof(pwd
->pw_uid
));
2558 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2562 wbc_status
= wbcLogoffUserEx(&logoff
, &error
);
2563 retval
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
2564 user
, "wbcLogoffUser");
2565 wbcFreeMemory(error
);
2566 wbcFreeMemory(logoff
.blobs
);
2567 logoff
.blobs
= NULL
;
2569 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2570 _pam_log(ctx
, LOG_INFO
,
2571 "failed to logoff user %s: %s\n",
2572 user
, wbcErrorString(wbc_status
));
2578 wbcFreeMemory(logoff
.blobs
);
2581 if (!WBC_ERROR_IS_OK(wbc_status
)) {
2582 retval
= wbc_auth_error_to_pam_error(ctx
, error
, wbc_status
,
2583 user
, "wbcLogoffUser");
2587 * Delete the krb5 ccname variable from the PAM environment
2588 * if it was set by winbind.
2590 if ((ctx
->ctrl
& WINBIND_KRB5_AUTH
) && pam_getenv(pamh
, "KRB5CCNAME")) {
2591 pam_putenv(pamh
, "KRB5CCNAME");
2594 _PAM_LOG_FUNCTION_LEAVE("_pam_delete_cred", ctx
, retval
);
2602 int pam_sm_authenticate(pam_handle_t
*pamh
, int flags
,
2603 int argc
, const char **argv
)
2605 const char *username
;
2606 const char *password
;
2607 const char *member
= NULL
;
2608 const char *cctype
= NULL
;
2609 int warn_pwd_expire
;
2610 int retval
= PAM_AUTH_ERR
;
2611 char *username_ret
= NULL
;
2612 char *new_authtok_required
= NULL
;
2613 char *real_username
= NULL
;
2614 struct pwb_context
*ctx
= NULL
;
2616 retval
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
,
2617 PAM_WINBIND_AUTHENTICATE
, &ctx
);
2622 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", ctx
);
2624 /* Get the username */
2625 retval
= pam_get_user(pamh
, &username
, NULL
);
2626 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
2627 _pam_log_debug(ctx
, LOG_DEBUG
,
2628 "can not get the username");
2629 retval
= PAM_SERVICE_ERR
;
2635 /* Decode the user name since AIX does not support logn user
2636 names by default. The name is encoded as _#uid. */
2638 if (username
[0] == '_') {
2639 uid_t id
= atoi(&username
[1]);
2640 struct passwd
*pw
= NULL
;
2642 if ((id
!=0) && ((pw
= getpwuid(id
)) != NULL
)) {
2643 real_username
= strdup(pw
->pw_name
);
2648 if (!real_username
) {
2649 /* Just making a copy of the username we got from PAM */
2650 if ((real_username
= strdup(username
)) == NULL
) {
2651 _pam_log_debug(ctx
, LOG_DEBUG
,
2652 "memory allocation failure when copying "
2654 retval
= PAM_SERVICE_ERR
;
2659 /* Maybe this was a UPN */
2661 if (strchr(real_username
, '@') != NULL
) {
2662 char *samaccountname
= NULL
;
2664 samaccountname
= winbind_upn_to_username(ctx
,
2666 if (samaccountname
) {
2667 free(real_username
);
2668 real_username
= strdup(samaccountname
);
2672 retval
= _winbind_read_password(ctx
, ctx
->ctrl
, NULL
,
2673 _("Password: "), NULL
,
2676 if (retval
!= PAM_SUCCESS
) {
2677 _pam_log(ctx
, LOG_ERR
,
2678 "Could not retrieve user's password");
2679 retval
= PAM_AUTHTOK_ERR
;
2683 /* Let's not give too much away in the log file */
2685 #ifdef DEBUG_PASSWORD
2686 _pam_log_debug(ctx
, LOG_INFO
,
2687 "Verify user '%s' with password '%s'",
2688 real_username
, password
);
2690 _pam_log_debug(ctx
, LOG_INFO
,
2691 "Verify user '%s'", real_username
);
2694 member
= get_member_from_config(ctx
);
2695 cctype
= get_krb5_cc_type_from_config(ctx
);
2696 warn_pwd_expire
= get_warn_pwd_expire_from_config(ctx
);
2698 /* Now use the username to look up password */
2699 retval
= winbind_auth_request(ctx
, real_username
, password
,
2700 member
, cctype
, warn_pwd_expire
,
2702 NULL
, &username_ret
);
2704 if (retval
== PAM_NEW_AUTHTOK_REQD
||
2705 retval
== PAM_AUTHTOK_EXPIRED
) {
2707 char *new_authtok_required_during_auth
= NULL
;
2709 new_authtok_required
= talloc_asprintf(NULL
, "%d", retval
);
2710 if (!new_authtok_required
) {
2711 retval
= PAM_BUF_ERR
;
2715 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
,
2716 new_authtok_required
,
2717 _pam_winbind_cleanup_func
);
2719 retval
= PAM_SUCCESS
;
2721 new_authtok_required_during_auth
= talloc_asprintf(NULL
, "%d", true);
2722 if (!new_authtok_required_during_auth
) {
2723 retval
= PAM_BUF_ERR
;
2727 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2728 new_authtok_required_during_auth
,
2729 _pam_winbind_cleanup_func
);
2736 pam_set_item (pamh
, PAM_USER
, username_ret
);
2737 _pam_log_debug(ctx
, LOG_INFO
,
2738 "Returned user was '%s'", username_ret
);
2742 if (real_username
) {
2743 free(real_username
);
2746 if (!new_authtok_required
) {
2747 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, NULL
, NULL
);
2750 if (retval
!= PAM_SUCCESS
) {
2751 _pam_free_data_info3(pamh
);
2754 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", ctx
, retval
);
2762 int pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
2763 int argc
, const char **argv
)
2765 int ret
= PAM_SYSTEM_ERR
;
2766 struct pwb_context
*ctx
= NULL
;
2768 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
,
2769 PAM_WINBIND_SETCRED
, &ctx
);
2774 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", ctx
);
2776 switch (flags
& ~PAM_SILENT
) {
2778 case PAM_DELETE_CRED
:
2779 ret
= _pam_delete_cred(pamh
, flags
, argc
,
2780 PAM_WINBIND_SETCRED
, argv
);
2782 case PAM_REFRESH_CRED
:
2783 _pam_log_debug(ctx
, LOG_WARNING
,
2784 "PAM_REFRESH_CRED not implemented");
2787 case PAM_REINITIALIZE_CRED
:
2788 _pam_log_debug(ctx
, LOG_WARNING
,
2789 "PAM_REINITIALIZE_CRED not implemented");
2792 case PAM_ESTABLISH_CRED
:
2793 _pam_log_debug(ctx
, LOG_WARNING
,
2794 "PAM_ESTABLISH_CRED not implemented");
2798 ret
= PAM_SYSTEM_ERR
;
2804 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", ctx
, ret
);
2812 * Account management. We want to verify that the account exists
2813 * before returning PAM_SUCCESS
2816 int pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags
,
2817 int argc
, const char **argv
)
2819 const char *username
;
2820 int ret
= PAM_USER_UNKNOWN
;
2821 const char *tmp
= NULL
;
2822 struct pwb_context
*ctx
= NULL
;
2824 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
,
2825 PAM_WINBIND_ACCT_MGMT
, &ctx
);
2830 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", ctx
);
2833 /* Get the username */
2834 ret
= pam_get_user(pamh
, &username
, NULL
);
2835 if ((ret
!= PAM_SUCCESS
) || (!username
)) {
2836 _pam_log_debug(ctx
, LOG_DEBUG
,
2837 "can not get the username");
2838 ret
= PAM_SERVICE_ERR
;
2842 /* Verify the username */
2843 ret
= valid_user(ctx
, username
);
2846 /* some sort of system error. The log was already printed */
2847 ret
= PAM_SERVICE_ERR
;
2850 /* the user does not exist */
2851 _pam_log_debug(ctx
, LOG_NOTICE
, "user '%s' not found",
2853 if (ctx
->ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
2857 ret
= PAM_USER_UNKNOWN
;
2860 pam_get_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
,
2861 (const void **)&tmp
);
2865 case PAM_AUTHTOK_EXPIRED
:
2866 /* fall through, since new token is required in this case */
2867 case PAM_NEW_AUTHTOK_REQD
:
2868 _pam_log(ctx
, LOG_WARNING
,
2869 "pam_sm_acct_mgmt success but %s is set",
2870 PAM_WINBIND_NEW_AUTHTOK_REQD
);
2871 _pam_log(ctx
, LOG_NOTICE
,
2872 "user '%s' needs new password",
2874 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2875 ret
= PAM_NEW_AUTHTOK_REQD
;
2878 _pam_log(ctx
, LOG_WARNING
,
2879 "pam_sm_acct_mgmt success");
2880 _pam_log(ctx
, LOG_NOTICE
,
2881 "user '%s' granted access", username
);
2887 /* Otherwise, the authentication looked good */
2888 _pam_log(ctx
, LOG_NOTICE
,
2889 "user '%s' granted access", username
);
2893 /* we don't know anything about this return value */
2894 _pam_log(ctx
, LOG_ERR
,
2895 "internal module error (ret = %d, user = '%s')",
2897 ret
= PAM_SERVICE_ERR
;
2901 /* should not be reached */
2906 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", ctx
, ret
);
2914 int pam_sm_open_session(pam_handle_t
*pamh
, int flags
,
2915 int argc
, const char **argv
)
2917 int ret
= PAM_SUCCESS
;
2918 struct pwb_context
*ctx
= NULL
;
2920 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
,
2921 PAM_WINBIND_OPEN_SESSION
, &ctx
);
2926 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", ctx
);
2928 if (ctx
->ctrl
& WINBIND_MKHOMEDIR
) {
2929 /* check and create homedir */
2930 ret
= _pam_mkhomedir(ctx
);
2933 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", ctx
, ret
);
2941 int pam_sm_close_session(pam_handle_t
*pamh
, int flags
,
2942 int argc
, const char **argv
)
2944 int ret
= PAM_SUCCESS
;
2945 struct pwb_context
*ctx
= NULL
;
2947 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
,
2948 PAM_WINBIND_CLOSE_SESSION
, &ctx
);
2953 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", ctx
);
2956 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", ctx
, ret
);
2964 * evaluate whether we need to re-authenticate with kerberos after a
2967 * @param ctx PAM winbind context.
2968 * @param user The username
2970 * @return boolean Returns true if required, false if not.
2973 static bool _pam_require_krb5_auth_after_chauthtok(struct pwb_context
*ctx
,
2977 /* Make sure that we only do this if a) the chauthtok got initiated
2978 * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2979 * later password change via the "passwd" command if done by the user
2981 * NB. If we login from gdm or xdm and the password expires,
2982 * we change the password, but there is no memory cache.
2983 * Thus, even for passthrough login, we should do the
2984 * authentication again to update memory cache.
2988 char *new_authtok_reqd_during_auth
= NULL
;
2989 struct passwd
*pwd
= NULL
;
2991 pam_get_data(ctx
->pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2992 (const void **) &new_authtok_reqd_during_auth
);
2993 pam_set_data(ctx
->pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2996 if (new_authtok_reqd_during_auth
) {
3000 pwd
= getpwnam(user
);
3005 if (getuid() == pwd
->pw_uid
) {
3014 int pam_sm_chauthtok(pam_handle_t
* pamh
, int flags
,
3015 int argc
, const char **argv
)
3019 bool cached_login
= false;
3021 /* <DO NOT free() THESE> */
3023 const char *pass_old
;
3024 const char *pass_new
;
3025 /* </DO NOT free() THESE> */
3030 char *username_ret
= NULL
;
3031 struct wbcAuthErrorInfo
*error
= NULL
;
3032 struct pwb_context
*ctx
= NULL
;
3034 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
,
3035 PAM_WINBIND_CHAUTHTOK
, &ctx
);
3040 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", ctx
);
3042 cached_login
= (ctx
->ctrl
& WINBIND_CACHED_LOGIN
);
3044 /* clearing offline bit for auth */
3045 ctx
->ctrl
&= ~WINBIND_CACHED_LOGIN
;
3048 * First get the name of a user
3050 ret
= pam_get_user(pamh
, &user
, _("Username: "));
3052 _pam_log(ctx
, LOG_ERR
,
3053 "password - could not identify user");
3058 _pam_log(ctx
, LOG_ERR
, "username was NULL!");
3059 ret
= PAM_USER_UNKNOWN
;
3063 _pam_log_debug(ctx
, LOG_DEBUG
, "username [%s] obtained", user
);
3065 /* check if this is really a user in winbindd, not only in NSS */
3066 ret
= valid_user(ctx
, user
);
3069 ret
= PAM_USER_UNKNOWN
;
3072 ret
= PAM_SYSTEM_ERR
;
3079 * obtain and verify the current password (OLDAUTHTOK) for
3083 if (flags
& PAM_PRELIM_CHECK
) {
3084 time_t pwdlastset_prelim
= 0;
3086 /* instruct user what is happening */
3088 #define greeting _("Changing password for")
3089 Announce
= talloc_asprintf(ctx
, "%s %s", greeting
, user
);
3091 _pam_log(ctx
, LOG_CRIT
,
3092 "password - out of memory");
3098 lctrl
= ctx
->ctrl
| WINBIND__OLD_PASSWORD
;
3099 ret
= _winbind_read_password(ctx
, lctrl
,
3101 _("(current) NT password: "),
3103 (const char **) &pass_old
);
3104 TALLOC_FREE(Announce
);
3105 if (ret
!= PAM_SUCCESS
) {
3106 _pam_log(ctx
, LOG_NOTICE
,
3107 "password - (old) token not obtained");
3111 /* verify that this is the password for this user */
3113 ret
= winbind_auth_request(ctx
, user
, pass_old
,
3116 &pwdlastset_prelim
, NULL
);
3118 if (ret
!= PAM_ACCT_EXPIRED
&&
3119 ret
!= PAM_AUTHTOK_EXPIRED
&&
3120 ret
!= PAM_NEW_AUTHTOK_REQD
&&
3121 ret
!= PAM_SUCCESS
) {
3126 pam_set_data(pamh
, PAM_WINBIND_PWD_LAST_SET
,
3127 (void *)pwdlastset_prelim
, NULL
);
3129 ret
= pam_set_item(pamh
, PAM_OLDAUTHTOK
,
3130 (const void *) pass_old
);
3132 if (ret
!= PAM_SUCCESS
) {
3133 _pam_log(ctx
, LOG_CRIT
,
3134 "failed to set PAM_OLDAUTHTOK");
3136 } else if (flags
& PAM_UPDATE_AUTHTOK
) {
3138 time_t pwdlastset_update
= 0;
3141 * obtain the proposed password
3145 * get the old token back.
3148 ret
= pam_get_item(pamh
, PAM_OLDAUTHTOK
, (const void **) &pass_old
);
3150 if (ret
!= PAM_SUCCESS
) {
3151 _pam_log(ctx
, LOG_NOTICE
,
3152 "user not authenticated");
3156 lctrl
= ctx
->ctrl
& ~WINBIND_TRY_FIRST_PASS_ARG
;
3158 if (on(WINBIND_USE_AUTHTOK_ARG
, lctrl
)) {
3159 lctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
3162 ret
= PAM_AUTHTOK_ERR
;
3163 while ((ret
!= PAM_SUCCESS
) && (retry
++ < MAX_PASSWD_TRIES
)) {
3165 * use_authtok is to force the use of a previously entered
3166 * password -- needed for pluggable password strength checking
3169 ret
= _winbind_read_password(ctx
, lctrl
,
3171 _("Enter new NT password: "),
3172 _("Retype new NT password: "),
3173 (const char **)&pass_new
);
3175 if (ret
!= PAM_SUCCESS
) {
3176 _pam_log_debug(ctx
, LOG_ALERT
,
3178 "new password not obtained");
3179 pass_old
= NULL
;/* tidy up */
3184 * At this point we know who the user is and what they
3185 * propose as their new password. Verify that the new
3186 * password is acceptable.
3189 if (pass_new
[0] == '\0') {/* "\0" password = NULL */
3195 * By reaching here we have approved the passwords and must now
3196 * rebuild the password database file.
3198 pam_get_data(pamh
, PAM_WINBIND_PWD_LAST_SET
,
3199 (const void **) &pwdlastset_update
);
3202 * if cached creds were enabled, make sure to set the
3203 * WINBIND_CACHED_LOGIN bit here in order to have winbindd
3204 * update the cached creds storage - gd
3207 ctx
->ctrl
|= WINBIND_CACHED_LOGIN
;
3210 ret
= winbind_chauthtok_request(ctx
, user
, pass_old
,
3211 pass_new
, pwdlastset_update
);
3213 pass_old
= pass_new
= NULL
;
3217 if (_pam_require_krb5_auth_after_chauthtok(ctx
, user
)) {
3219 const char *member
= NULL
;
3220 const char *cctype
= NULL
;
3221 int warn_pwd_expire
;
3222 struct wbcLogonUserInfo
*info
= NULL
;
3223 struct wbcUserPasswordPolicyInfo
*policy
= NULL
;
3225 member
= get_member_from_config(ctx
);
3226 cctype
= get_krb5_cc_type_from_config(ctx
);
3227 warn_pwd_expire
= get_warn_pwd_expire_from_config(ctx
);
3229 /* Keep WINBIND_CACHED_LOGIN bit for
3230 * authentication after changing the password.
3231 * This will update the cached credentials in case
3232 * that winbindd_dual_pam_chauthtok() fails
3237 ret
= winbind_auth_request(ctx
, user
, pass_new
,
3239 &error
, &info
, &policy
,
3240 NULL
, &username_ret
);
3241 pass_old
= pass_new
= NULL
;
3243 if (ret
== PAM_SUCCESS
) {
3245 struct wbcAuthUserInfo
*user_info
= NULL
;
3247 if (info
&& info
->info
) {
3248 user_info
= info
->info
;
3251 /* warn a user if the password is about to
3253 _pam_warn_password_expiry(ctx
, user_info
, policy
,
3257 /* set some info3 info for other modules in the
3259 _pam_set_data_info3(ctx
, user_info
);
3261 /* put krb5ccname into env */
3262 _pam_setup_krb5_env(ctx
, info
);
3265 pam_set_item(pamh
, PAM_USER
,
3267 _pam_log_debug(ctx
, LOG_INFO
,
3268 "Returned user was '%s'",
3275 if (info
&& info
->blobs
) {
3276 wbcFreeMemory(info
->blobs
);
3278 wbcFreeMemory(info
);
3279 wbcFreeMemory(policy
);
3284 ret
= PAM_SERVICE_ERR
;
3289 /* Deal with offline errors. */
3291 const char *codes
[] = {
3292 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
3293 "NT_STATUS_NO_LOGON_SERVERS",
3294 "NT_STATUS_ACCESS_DENIED"
3297 for (i
=0; i
<ARRAY_SIZE(codes
); i
++) {
3299 if (_pam_check_remark_auth_err(ctx
, error
, codes
[i
], &_ret
)) {
3305 wbcFreeMemory(error
);
3307 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", ctx
, ret
);
3316 /* static module data */
3318 struct pam_module _pam_winbind_modstruct
= {
3320 pam_sm_authenticate
,
3323 pam_sm_open_session
,
3324 pam_sm_close_session
,
3331 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
3332 * Copyright (c) Tim Potter <tpot@samba.org> 2000
3333 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
3334 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2008
3335 * Copyright (c) Jan Rêkorajski 1999.
3336 * Copyright (c) Andrew G. Morgan 1996-8.
3337 * Copyright (c) Alex O. Yuriev, 1996.
3338 * Copyright (c) Cristian Gafton 1996.
3339 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
3341 * Redistribution and use in source and binary forms, with or without
3342 * modification, are permitted provided that the following conditions
3344 * 1. Redistributions of source code must retain the above copyright
3345 * notice, and the entire permission notice in its entirety,
3346 * including the disclaimer of warranties.
3347 * 2. Redistributions in binary form must reproduce the above copyright
3348 * notice, this list of conditions and the following disclaimer in the
3349 * documentation and/or other materials provided with the distribution.
3350 * 3. The name of the author may not be used to endorse or promote
3351 * products derived from this software without specific prior
3352 * written permission.
3354 * ALTERNATIVELY, this product may be distributed under the terms of
3355 * the GNU Public License, in which case the provisions of the GPL are
3356 * required INSTEAD OF the above restrictions. (This clause is
3357 * necessary due to a potential bad interaction between the GPL and
3358 * the restrictions contained in a BSD-style copyright.)
3360 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
3361 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3362 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3363 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3364 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3365 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3366 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3367 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3368 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3369 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3370 * OF THE POSSIBILITY OF SUCH DAMAGE.