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 #include "pam_winbind.h"
15 static int wbc_error_to_pam_error(wbcErr status
)
20 case WBC_ERR_NOT_IMPLEMENTED
:
21 return PAM_SERVICE_ERR
;
22 case WBC_ERR_UNKNOWN_FAILURE
:
24 case WBC_ERR_NO_MEMORY
:
26 case WBC_ERR_INVALID_SID
:
27 case WBC_ERR_INVALID_PARAM
:
29 case WBC_ERR_WINBIND_NOT_AVAILABLE
:
30 return PAM_AUTHINFO_UNAVAIL
;
31 case WBC_ERR_DOMAIN_NOT_FOUND
:
32 return PAM_AUTHINFO_UNAVAIL
;
33 case WBC_ERR_INVALID_RESPONSE
:
35 case WBC_ERR_NSS_ERROR
:
36 return PAM_USER_UNKNOWN
;
37 case WBC_ERR_AUTH_ERROR
:
39 case WBC_ERR_UNKNOWN_USER
:
40 return PAM_USER_UNKNOWN
;
41 case WBC_ERR_UNKNOWN_GROUP
:
42 return PAM_USER_UNKNOWN
;
43 case WBC_ERR_PWD_CHANGE_FAILED
:
51 static const char *_pam_error_code_str(int err
)
57 return "PAM_OPEN_ERR";
59 return "PAM_SYMBOL_ERR";
61 return "PAM_SERVICE_ERR";
63 return "PAM_SYSTEM_ERR";
67 return "PAM_PERM_DENIED";
69 return "PAM_AUTH_ERR";
70 case PAM_CRED_INSUFFICIENT
:
71 return "PAM_CRED_INSUFFICIENT";
72 case PAM_AUTHINFO_UNAVAIL
:
73 return "PAM_AUTHINFO_UNAVAIL";
74 case PAM_USER_UNKNOWN
:
75 return "PAM_USER_UNKNOWN";
77 return "PAM_MAXTRIES";
78 case PAM_NEW_AUTHTOK_REQD
:
79 return "PAM_NEW_AUTHTOK_REQD";
80 case PAM_ACCT_EXPIRED
:
81 return "PAM_ACCT_EXPIRED";
83 return "PAM_SESSION_ERR";
84 case PAM_CRED_UNAVAIL
:
85 return "PAM_CRED_UNAVAIL";
86 case PAM_CRED_EXPIRED
:
87 return "PAM_CRED_EXPIRED";
89 return "PAM_CRED_ERR";
90 case PAM_NO_MODULE_DATA
:
91 return "PAM_NO_MODULE_DATA";
93 return "PAM_CONV_ERR";
95 return "PAM_AUTHTOK_ERR";
96 case PAM_AUTHTOK_RECOVERY_ERR
:
97 return "PAM_AUTHTOK_RECOVERY_ERR";
98 case PAM_AUTHTOK_LOCK_BUSY
:
99 return "PAM_AUTHTOK_LOCK_BUSY";
100 case PAM_AUTHTOK_DISABLE_AGING
:
101 return "PAM_AUTHTOK_DISABLE_AGING";
103 return "PAM_TRY_AGAIN";
108 case PAM_AUTHTOK_EXPIRED
:
109 return "PAM_AUTHTOK_EXPIRED";
110 #ifdef PAM_MODULE_UNKNOWN
111 case PAM_MODULE_UNKNOWN
:
112 return "PAM_MODULE_UNKNOWN";
116 return "PAM_BAD_ITEM";
118 #ifdef PAM_CONV_AGAIN
120 return "PAM_CONV_AGAIN";
122 #ifdef PAM_INCOMPLETE
124 return "PAM_INCOMPLETE";
131 #define _PAM_LOG_FUNCTION_ENTER(function, ctx) \
133 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] ENTER: " \
134 function " (flags: 0x%04x)", ctx->pamh, ctx->flags); \
135 _pam_log_state(ctx); \
138 #define _PAM_LOG_FUNCTION_LEAVE(function, ctx, retval) \
140 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] LEAVE: " \
141 function " returning %d (%s)", ctx->pamh, retval, \
142 _pam_error_code_str(retval)); \
143 _pam_log_state(ctx); \
148 #define MAX_PASSWD_TRIES 3
151 * Work around the pam API that has functions with void ** as parameters
152 * These lead to strict aliasing warnings with gcc.
154 static int _pam_get_item(const pam_handle_t
*pamh
,
158 const void **item
= (const void **)_item
;
159 return pam_get_item(pamh
, item_type
, item
);
161 static int _pam_get_data(const pam_handle_t
*pamh
,
162 const char *module_data_name
,
165 const void **data
= (const void **)_data
;
166 return pam_get_data(pamh
, module_data_name
, data
);
169 /* some syslogging */
171 #ifdef HAVE_PAM_VSYSLOG
172 static void _pam_log_int(const pam_handle_t
*pamh
,
177 pam_vsyslog(pamh
, err
, format
, args
);
180 static void _pam_log_int(const pam_handle_t
*pamh
,
185 char *format2
= NULL
;
188 _pam_get_item(pamh
, PAM_SERVICE
, &service
);
190 format2
= (char *)malloc(strlen(MODULE_NAME
)+strlen(format
)+strlen(service
)+5);
191 if (format2
== NULL
) {
192 /* what else todo ? */
193 vsyslog(err
, format
, args
);
197 sprintf(format2
, "%s(%s): %s", MODULE_NAME
, service
, format
);
198 vsyslog(err
, format2
, args
);
201 #endif /* HAVE_PAM_VSYSLOG */
203 static bool _pam_log_is_silent(int ctrl
)
205 return on(ctrl
, WINBIND_SILENT
);
208 static void _pam_log(struct pwb_context
*r
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
209 static void _pam_log(struct pwb_context
*r
, int err
, const char *format
, ...)
213 if (_pam_log_is_silent(r
->ctrl
)) {
217 va_start(args
, format
);
218 _pam_log_int(r
->pamh
, err
, format
, args
);
221 static void __pam_log(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(4,5);
222 static void __pam_log(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...)
226 if (_pam_log_is_silent(ctrl
)) {
230 va_start(args
, format
);
231 _pam_log_int(pamh
, err
, format
, args
);
235 static bool _pam_log_is_debug_enabled(int ctrl
)
241 if (_pam_log_is_silent(ctrl
)) {
245 if (!(ctrl
& WINBIND_DEBUG_ARG
)) {
252 static bool _pam_log_is_debug_state_enabled(int ctrl
)
254 if (!(ctrl
& WINBIND_DEBUG_STATE
)) {
258 return _pam_log_is_debug_enabled(ctrl
);
261 static void _pam_log_debug(struct pwb_context
*r
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
262 static void _pam_log_debug(struct pwb_context
*r
, int err
, const char *format
, ...)
266 if (!_pam_log_is_debug_enabled(r
->ctrl
)) {
270 va_start(args
, format
);
271 _pam_log_int(r
->pamh
, err
, format
, args
);
274 static void __pam_log_debug(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(4,5);
275 static void __pam_log_debug(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...)
279 if (!_pam_log_is_debug_enabled(ctrl
)) {
283 va_start(args
, format
);
284 _pam_log_int(pamh
, err
, format
, args
);
288 static void _pam_log_state_datum(struct pwb_context
*ctx
,
293 const void *data
= NULL
;
294 if (item_type
!= 0) {
295 pam_get_item(ctx
->pamh
, item_type
, &data
);
297 pam_get_data(ctx
->pamh
, key
, &data
);
300 const char *type
= (item_type
!= 0) ? "ITEM" : "DATA";
301 if (is_string
!= 0) {
302 _pam_log_debug(ctx
, LOG_DEBUG
,
303 "[pamh: %p] STATE: %s(%s) = \"%s\" (%p)",
304 ctx
->pamh
, type
, key
, (const char *)data
,
307 _pam_log_debug(ctx
, LOG_DEBUG
,
308 "[pamh: %p] STATE: %s(%s) = %p",
309 ctx
->pamh
, type
, key
, data
);
314 #define _PAM_LOG_STATE_DATA_POINTER(ctx, module_data_name) \
315 _pam_log_state_datum(ctx, 0, module_data_name, 0)
317 #define _PAM_LOG_STATE_DATA_STRING(ctx, module_data_name) \
318 _pam_log_state_datum(ctx, 0, module_data_name, 1)
320 #define _PAM_LOG_STATE_ITEM_POINTER(ctx, item_type) \
321 _pam_log_state_datum(ctx, item_type, #item_type, 0)
323 #define _PAM_LOG_STATE_ITEM_STRING(ctx, item_type) \
324 _pam_log_state_datum(ctx, item_type, #item_type, 1)
326 #ifdef DEBUG_PASSWORD
327 #define _LOG_PASSWORD_AS_STRING 1
329 #define _LOG_PASSWORD_AS_STRING 0
332 #define _PAM_LOG_STATE_ITEM_PASSWORD(ctx, item_type) \
333 _pam_log_state_datum(ctx, item_type, #item_type, \
334 _LOG_PASSWORD_AS_STRING)
336 static void _pam_log_state(struct pwb_context
*ctx
)
338 if (!_pam_log_is_debug_state_enabled(ctx
->ctrl
)) {
342 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_SERVICE
);
343 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_USER
);
344 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_TTY
);
345 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_RHOST
);
346 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_RUSER
);
347 _PAM_LOG_STATE_ITEM_PASSWORD(ctx
, PAM_OLDAUTHTOK
);
348 _PAM_LOG_STATE_ITEM_PASSWORD(ctx
, PAM_AUTHTOK
);
349 _PAM_LOG_STATE_ITEM_STRING(ctx
, PAM_USER_PROMPT
);
350 _PAM_LOG_STATE_ITEM_POINTER(ctx
, PAM_CONV
);
351 #ifdef PAM_FAIL_DELAY
352 _PAM_LOG_STATE_ITEM_POINTER(ctx
, PAM_FAIL_DELAY
);
354 #ifdef PAM_REPOSITORY
355 _PAM_LOG_STATE_ITEM_POINTER(ctx
, PAM_REPOSITORY
);
358 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_HOMEDIR
);
359 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_LOGONSCRIPT
);
360 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_LOGONSERVER
);
361 _PAM_LOG_STATE_DATA_STRING(ctx
, PAM_WINBIND_PROFILEPATH
);
362 _PAM_LOG_STATE_DATA_STRING(ctx
,
363 PAM_WINBIND_NEW_AUTHTOK_REQD
);
364 /* Use atoi to get PAM result code */
365 _PAM_LOG_STATE_DATA_STRING(ctx
,
366 PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
);
367 _PAM_LOG_STATE_DATA_POINTER(ctx
, PAM_WINBIND_PWD_LAST_SET
);
370 static int _pam_parse(const pam_handle_t
*pamh
,
374 dictionary
**result_d
)
377 const char *config_file
= NULL
;
380 dictionary
*d
= NULL
;
382 if (flags
& PAM_SILENT
) {
383 ctrl
|= WINBIND_SILENT
;
386 for (i
=argc
,v
=argv
; i
-- > 0; ++v
) {
387 if (!strncasecmp(*v
, "config", strlen("config"))) {
388 ctrl
|= WINBIND_CONFIG_FILE
;
394 if (config_file
== NULL
) {
395 config_file
= PAM_WINBIND_CONFIG_FILE
;
398 d
= iniparser_load(config_file
);
400 goto config_from_pam
;
403 if (iniparser_getboolean(d
, "global:debug", false)) {
404 ctrl
|= WINBIND_DEBUG_ARG
;
407 if (iniparser_getboolean(d
, "global:debug_state", false)) {
408 ctrl
|= WINBIND_DEBUG_STATE
;
411 if (iniparser_getboolean(d
, "global:cached_login", false)) {
412 ctrl
|= WINBIND_CACHED_LOGIN
;
415 if (iniparser_getboolean(d
, "global:krb5_auth", false)) {
416 ctrl
|= WINBIND_KRB5_AUTH
;
419 if (iniparser_getboolean(d
, "global:silent", false)) {
420 ctrl
|= WINBIND_SILENT
;
423 if (iniparser_getstr(d
, "global:krb5_ccache_type") != NULL
) {
424 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
427 if ((iniparser_getstr(d
, "global:require-membership-of") != NULL
) ||
428 (iniparser_getstr(d
, "global:require_membership_of") != NULL
)) {
429 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
432 if (iniparser_getboolean(d
, "global:try_first_pass", false)) {
433 ctrl
|= WINBIND_TRY_FIRST_PASS_ARG
;
436 if (iniparser_getint(d
, "global:warn_pwd_expire", 0)) {
437 ctrl
|= WINBIND_WARN_PWD_EXPIRE
;
441 /* step through arguments */
442 for (i
=argc
,v
=argv
; i
-- > 0; ++v
) {
444 /* generic options */
445 if (!strcmp(*v
,"debug"))
446 ctrl
|= WINBIND_DEBUG_ARG
;
447 else if (!strcasecmp(*v
, "debug_state"))
448 ctrl
|= WINBIND_DEBUG_STATE
;
449 else if (!strcasecmp(*v
, "silent"))
450 ctrl
|= WINBIND_SILENT
;
451 else if (!strcasecmp(*v
, "use_authtok"))
452 ctrl
|= WINBIND_USE_AUTHTOK_ARG
;
453 else if (!strcasecmp(*v
, "use_first_pass"))
454 ctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
455 else if (!strcasecmp(*v
, "try_first_pass"))
456 ctrl
|= WINBIND_TRY_FIRST_PASS_ARG
;
457 else if (!strcasecmp(*v
, "unknown_ok"))
458 ctrl
|= WINBIND_UNKNOWN_OK_ARG
;
459 else if (!strncasecmp(*v
, "require_membership_of",
460 strlen("require_membership_of")))
461 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
462 else if (!strncasecmp(*v
, "require-membership-of",
463 strlen("require-membership-of")))
464 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
465 else if (!strcasecmp(*v
, "krb5_auth"))
466 ctrl
|= WINBIND_KRB5_AUTH
;
467 else if (!strncasecmp(*v
, "krb5_ccache_type",
468 strlen("krb5_ccache_type")))
469 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
470 else if (!strcasecmp(*v
, "cached_login"))
471 ctrl
|= WINBIND_CACHED_LOGIN
;
473 __pam_log(pamh
, ctrl
, LOG_ERR
,
474 "pam_parse: unknown option: %s", *v
);
484 iniparser_freedict(d
);
491 static int _pam_winbind_free_context(struct pwb_context
*ctx
)
498 iniparser_freedict(ctx
->dict
);
504 static int _pam_winbind_init_context(pam_handle_t
*pamh
,
508 struct pwb_context
**ctx_p
)
510 struct pwb_context
*r
= NULL
;
512 r
= TALLOC_ZERO_P(NULL
, struct pwb_context
);
517 talloc_set_destructor(r
, _pam_winbind_free_context
);
523 r
->ctrl
= _pam_parse(pamh
, flags
, argc
, argv
, &r
->dict
);
526 return PAM_SYSTEM_ERR
;
534 static void _pam_winbind_cleanup_func(pam_handle_t
*pamh
,
538 int ctrl
= _pam_parse(pamh
, 0, 0, NULL
, NULL
);
539 if (_pam_log_is_debug_state_enabled(ctrl
)) {
540 __pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
541 "[pamh: %p] CLEAN: cleaning up PAM data %p "
542 "(error_status = %d)", pamh
, data
,
549 static const struct ntstatus_errors
{
550 const char *ntstatus_string
;
551 const char *error_string
;
552 } ntstatus_errors
[] = {
555 {"NT_STATUS_BACKUP_CONTROLLER",
556 "No primary Domain Controler available"},
557 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
558 "No domain controllers found"},
559 {"NT_STATUS_NO_LOGON_SERVERS",
561 {"NT_STATUS_PWD_TOO_SHORT",
562 "Password too short"},
563 {"NT_STATUS_PWD_TOO_RECENT",
564 "The password of this user is too recent to change"},
565 {"NT_STATUS_PWD_HISTORY_CONFLICT",
566 "Password is already in password history"},
567 {"NT_STATUS_PASSWORD_EXPIRED",
568 "Your password has expired"},
569 {"NT_STATUS_PASSWORD_MUST_CHANGE",
570 "You need to change your password now"},
571 {"NT_STATUS_INVALID_WORKSTATION",
572 "You are not allowed to logon from this workstation"},
573 {"NT_STATUS_INVALID_LOGON_HOURS",
574 "You are not allowed to logon at this time"},
575 {"NT_STATUS_ACCOUNT_EXPIRED",
576 "Your account has expired. "
577 "Please contact your System administrator"}, /* SCNR */
578 {"NT_STATUS_ACCOUNT_DISABLED",
579 "Your account is disabled. "
580 "Please contact your System administrator"}, /* SCNR */
581 {"NT_STATUS_ACCOUNT_LOCKED_OUT",
582 "Your account has been locked. "
583 "Please contact your System administrator"}, /* SCNR */
584 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
585 "Invalid Trust Account"},
586 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
587 "Invalid Trust Account"},
588 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
589 "Invalid Trust Account"},
590 {"NT_STATUS_ACCESS_DENIED",
595 static const char *_get_ntstatus_error_string(const char *nt_status_string
)
598 for (i
=0; ntstatus_errors
[i
].ntstatus_string
!= NULL
; i
++) {
599 if (!strcasecmp(ntstatus_errors
[i
].ntstatus_string
,
601 return ntstatus_errors
[i
].error_string
;
607 /* --- authentication management functions --- */
609 /* Attempt a conversation */
611 static int converse(const pam_handle_t
*pamh
,
613 struct pam_message
**message
,
614 struct pam_response
**response
)
617 struct pam_conv
*conv
;
619 retval
= _pam_get_item(pamh
, PAM_CONV
, &conv
);
620 if (retval
== PAM_SUCCESS
) {
621 retval
= conv
->conv(nargs
,
622 (const struct pam_message
**)message
,
623 response
, conv
->appdata_ptr
);
626 return retval
; /* propagate error status */
630 static int _make_remark(struct pwb_context
*ctx
,
634 int retval
= PAM_SUCCESS
;
636 struct pam_message
*pmsg
[1], msg
[1];
637 struct pam_response
*resp
;
639 if (ctx
->flags
& WINBIND_SILENT
) {
644 msg
[0].msg
= discard_const_p(char, text
);
645 msg
[0].msg_style
= type
;
648 retval
= converse(ctx
->pamh
, 1, pmsg
, &resp
);
651 _pam_drop_reply(resp
, 1);
656 static int _make_remark_v(struct pwb_context
*ctx
,
664 ret
= vasprintf(&var
, format
, args
);
666 _pam_log(ctx
, LOG_ERR
, "memory allocation failure");
670 ret
= _make_remark(ctx
, type
, var
);
675 static int _make_remark_format(struct pwb_context
*ctx
, int type
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
676 static int _make_remark_format(struct pwb_context
*ctx
, int type
, const char *format
, ...)
681 va_start(args
, format
);
682 ret
= _make_remark_v(ctx
, type
, format
, args
);
687 static int pam_winbind_request(struct pwb_context
*ctx
,
688 enum winbindd_cmd req_type
,
689 struct winbindd_request
*request
,
690 struct winbindd_response
*response
)
692 /* Fill in request and send down pipe */
693 winbindd_init_request(request
, req_type
);
695 if (winbind_write_sock(request
, sizeof(*request
), 0, 0) == -1) {
696 _pam_log(ctx
, LOG_ERR
,
697 "pam_winbind_request: write to socket failed!");
698 winbind_close_sock();
699 return PAM_SERVICE_ERR
;
703 if (winbindd_read_reply(response
) == -1) {
704 _pam_log(ctx
, LOG_ERR
,
705 "pam_winbind_request: read from socket failed!");
706 winbind_close_sock();
707 return PAM_SERVICE_ERR
;
710 /* We are done with the socket - close it and avoid mischeif */
711 winbind_close_sock();
713 /* Copy reply data from socket */
714 if (response
->result
== WINBINDD_OK
) {
718 /* no need to check for pam_error codes for getpwnam() */
721 case WINBINDD_GETPWNAM
:
722 case WINBINDD_LOOKUPNAME
:
723 if (strlen(response
->data
.auth
.nt_status_string
) > 0) {
724 _pam_log(ctx
, LOG_ERR
,
725 "request failed, NT error was %s",
726 response
->data
.auth
.nt_status_string
);
728 _pam_log(ctx
, LOG_ERR
, "request failed");
730 return PAM_USER_UNKNOWN
;
735 if (response
->data
.auth
.pam_error
!= PAM_SUCCESS
) {
736 _pam_log(ctx
, LOG_ERR
,
737 "request failed: %s, "
738 "PAM error was %s (%d), NT error was %s",
739 response
->data
.auth
.error_string
,
740 pam_strerror(ctx
->pamh
, response
->data
.auth
.pam_error
),
741 response
->data
.auth
.pam_error
,
742 response
->data
.auth
.nt_status_string
);
743 return response
->data
.auth
.pam_error
;
746 _pam_log(ctx
, LOG_ERR
, "request failed, but PAM error 0!");
748 return PAM_SERVICE_ERR
;
751 static int pam_winbind_request_log(struct pwb_context
*ctx
,
757 /* incorrect password */
758 _pam_log(ctx
, LOG_WARNING
, "user '%s' denied access "
759 "(incorrect password or invalid membership)", user
);
761 case PAM_ACCT_EXPIRED
:
762 /* account expired */
763 _pam_log(ctx
, LOG_WARNING
, "user '%s' account expired",
766 case PAM_AUTHTOK_EXPIRED
:
767 /* password expired */
768 _pam_log(ctx
, LOG_WARNING
, "user '%s' password expired",
771 case PAM_NEW_AUTHTOK_REQD
:
772 /* new password required */
773 _pam_log(ctx
, LOG_WARNING
, "user '%s' new password "
776 case PAM_USER_UNKNOWN
:
777 /* the user does not exist */
778 _pam_log_debug(ctx
, LOG_NOTICE
, "user '%s' not found",
780 if (ctx
->ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
785 /* Otherwise, the authentication looked good */
790 case WINBINDD_PAM_AUTH
:
791 _pam_log(ctx
, LOG_NOTICE
,
792 "user '%s' granted access", user
);
794 case WINBINDD_PAM_CHAUTHTOK
:
795 _pam_log(ctx
, LOG_NOTICE
,
796 "user '%s' password changed", user
);
799 _pam_log(ctx
, LOG_NOTICE
,
800 "user '%s' OK", user
);
806 /* we don't know anything about this return value */
807 _pam_log(ctx
, LOG_ERR
,
808 "internal module error (retval = %d, user = '%s')",
814 static int wbc_auth_error_to_pam_error(struct pwb_context
*ctx
,
815 struct wbcAuthErrorInfo
*e
,
817 const char *username
,
820 int ret
= PAM_AUTH_ERR
;
822 if (WBC_ERROR_IS_OK(status
)) {
823 _pam_log_debug(ctx
, LOG_DEBUG
, "request %s succeeded",
826 return pam_winbind_request_log(ctx
, ret
, username
);
830 if (e
->pam_error
!= PAM_SUCCESS
) {
831 _pam_log(ctx
, LOG_ERR
,
832 "request %s failed: %s, "
833 "PAM error: %s (%d), NTSTATUS: %s, "
834 "Error message was: %s",
836 wbcErrorString(status
),
837 _pam_error_code_str(e
->pam_error
),
842 return pam_winbind_request_log(ctx
, ret
, username
);
845 _pam_log(ctx
, LOG_ERR
, "request %s failed, but PAM error 0!", fn
);
847 ret
= PAM_SERVICE_ERR
;
848 return pam_winbind_request_log(ctx
, ret
, username
);
851 ret
= wbc_error_to_pam_error(status
);
852 return pam_winbind_request_log(ctx
, ret
, username
);
857 * send a password expiry message if required
859 * @param ctx PAM winbind context.
860 * @param next_change expected (calculated) next expiry date.
861 * @param already_expired pointer to a boolean to indicate if the password is
864 * @return boolean Returns true if message has been sent, false if not.
867 static bool _pam_send_password_expiry_message(struct pwb_context
*ctx
,
871 bool *already_expired
)
874 struct tm tm_now
, tm_next_change
;
876 if (already_expired
) {
877 *already_expired
= false;
880 if (next_change
<= now
) {
881 PAM_WB_REMARK_DIRECT(ctx
, "NT_STATUS_PASSWORD_EXPIRED");
882 if (already_expired
) {
883 *already_expired
= true;
888 if ((next_change
< 0) ||
889 (next_change
> now
+ warn_pwd_expire
* SECONDS_PER_DAY
)) {
893 if ((localtime_r(&now
, &tm_now
) == NULL
) ||
894 (localtime_r(&next_change
, &tm_next_change
) == NULL
)) {
898 days
= (tm_next_change
.tm_yday
+tm_next_change
.tm_year
*365) -
899 (tm_now
.tm_yday
+tm_now
.tm_year
*365);
902 _make_remark(ctx
, PAM_TEXT_INFO
,
903 "Your password expires today");
907 if (days
> 0 && days
< warn_pwd_expire
) {
908 _make_remark_format(ctx
, PAM_TEXT_INFO
,
909 "Your password will expire in %d %s",
910 days
, (days
> 1) ? "days":"day");
918 * Send a warning if the password expires in the near future
920 * @param ctx PAM winbind context.
921 * @param response The full authentication response structure.
922 * @param already_expired boolean, is the pwd already expired?
927 static void _pam_warn_password_expiry(struct pwb_context
*ctx
,
928 const struct winbindd_response
*response
,
930 bool *already_expired
)
932 time_t now
= time(NULL
);
933 time_t next_change
= 0;
935 if (already_expired
) {
936 *already_expired
= false;
939 /* accounts with ACB_PWNOEXP set never receive a warning */
940 if (response
->data
.auth
.info3
.acct_flags
& ACB_PWNOEXP
) {
944 /* no point in sending a warning if this is a grace logon */
945 if (PAM_WB_GRACE_LOGON(response
->data
.auth
.info3
.user_flgs
)) {
949 /* check if the info3 must change timestamp has been set */
950 next_change
= response
->data
.auth
.info3
.pass_must_change_time
;
952 if (_pam_send_password_expiry_message(ctx
, next_change
, now
,
958 /* now check for the global password policy */
959 /* good catch from Ralf Haferkamp: an expiry of "never" is translated
961 if (response
->data
.auth
.policy
.expire
<= 0) {
965 next_change
= response
->data
.auth
.info3
.pass_last_set_time
+
966 response
->data
.auth
.policy
.expire
;
968 if (_pam_send_password_expiry_message(ctx
, next_change
, now
,
974 /* no warning sent */
977 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
980 * Append a string, making sure not to overflow and to always return a
981 * NULL-terminated string.
983 * @param dest Destination string buffer (must already be NULL-terminated).
984 * @param src Source string buffer.
985 * @param dest_buffer_size Size of dest buffer in bytes.
987 * @return false if dest buffer is not big enough (no bytes copied), true on
991 static bool safe_append_string(char *dest
,
993 int dest_buffer_size
)
995 int dest_length
= strlen(dest
);
996 int src_length
= strlen(src
);
998 if (dest_length
+ src_length
+ 1 > dest_buffer_size
) {
1002 memcpy(dest
+ dest_length
, src
, src_length
+ 1);
1007 * Convert a names into a SID string, appending it to a buffer.
1009 * @param ctx PAM winbind context.
1010 * @param user User in PAM request.
1011 * @param name Name to convert.
1012 * @param sid_list_buffer Where to append the string sid.
1013 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1015 * @return false on failure, true on success.
1017 static bool winbind_name_to_sid_string(struct pwb_context
*ctx
,
1020 char *sid_list_buffer
,
1021 int sid_list_buffer_size
)
1023 const char* sid_string
;
1024 struct winbindd_response sid_response
;
1027 if (IS_SID_STRING(name
)) {
1030 struct winbindd_request sid_request
;
1032 ZERO_STRUCT(sid_request
);
1033 ZERO_STRUCT(sid_response
);
1035 _pam_log_debug(ctx
, LOG_DEBUG
,
1036 "no sid given, looking up: %s\n", name
);
1038 /* fortunatly winbindd can handle non-separated names */
1039 strncpy(sid_request
.data
.name
.name
, name
,
1040 sizeof(sid_request
.data
.name
.name
) - 1);
1042 if (pam_winbind_request_log(ctx
, WINBINDD_LOOKUPNAME
,
1043 &sid_request
, &sid_response
,
1045 _pam_log(ctx
, LOG_INFO
,
1046 "could not lookup name: %s\n", name
);
1050 sid_string
= sid_response
.data
.sid
.sid
;
1053 if (!safe_append_string(sid_list_buffer
, sid_string
,
1054 sid_list_buffer_size
)) {
1062 * Convert a list of names into a list of sids.
1064 * @param ctx PAM winbind context.
1065 * @param user User in PAM request.
1066 * @param name_list List of names or string sids, separated by commas.
1067 * @param sid_list_buffer Where to put the list of string sids.
1068 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1070 * @return false on failure, true on success.
1072 static bool winbind_name_list_to_sid_string_list(struct pwb_context
*ctx
,
1074 const char *name_list
,
1075 char *sid_list_buffer
,
1076 int sid_list_buffer_size
)
1078 bool result
= false;
1079 char *current_name
= NULL
;
1080 const char *search_location
;
1083 if (sid_list_buffer_size
> 0) {
1084 sid_list_buffer
[0] = 0;
1087 search_location
= name_list
;
1088 while ((comma
= strstr(search_location
, ",")) != NULL
) {
1089 current_name
= strndup(search_location
,
1090 comma
- search_location
);
1091 if (NULL
== current_name
) {
1095 if (!winbind_name_to_sid_string(ctx
, user
,
1098 sid_list_buffer_size
)) {
1102 SAFE_FREE(current_name
);
1104 if (!safe_append_string(sid_list_buffer
, ",",
1105 sid_list_buffer_size
)) {
1109 search_location
= comma
+ 1;
1112 if (!winbind_name_to_sid_string(ctx
, user
, search_location
,
1114 sid_list_buffer_size
)) {
1121 SAFE_FREE(current_name
);
1126 * put krb5ccname variable into environment
1128 * @param ctx PAM winbind context.
1129 * @param krb5ccname env variable retrieved from winbindd.
1134 static void _pam_setup_krb5_env(struct pwb_context
*ctx
,
1135 const char *krb5ccname
)
1140 if (off(ctx
->ctrl
, WINBIND_KRB5_AUTH
)) {
1144 if (!krb5ccname
|| (strlen(krb5ccname
) == 0)) {
1148 _pam_log_debug(ctx
, LOG_DEBUG
,
1149 "request returned KRB5CCNAME: %s", krb5ccname
);
1151 if (snprintf(var
, sizeof(var
), "KRB5CCNAME=%s", krb5ccname
) == -1) {
1155 ret
= pam_putenv(ctx
->pamh
, var
);
1157 _pam_log(ctx
, LOG_ERR
,
1158 "failed to set KRB5CCNAME to %s: %s",
1159 var
, pam_strerror(ctx
->pamh
, ret
));
1164 * Set string into the PAM stack.
1166 * @param ctx PAM winbind context.
1167 * @param data_name Key name for pam_set_data.
1168 * @param value String value.
1173 static void _pam_set_data_string(struct pwb_context
*ctx
,
1174 const char *data_name
,
1179 if (!data_name
|| !value
|| (strlen(data_name
) == 0) ||
1180 (strlen(value
) == 0)) {
1184 ret
= pam_set_data(ctx
->pamh
, data_name
, talloc_strdup(NULL
, value
),
1185 _pam_winbind_cleanup_func
);
1187 _pam_log_debug(ctx
, LOG_DEBUG
,
1188 "Could not set data %s: %s\n",
1189 data_name
, pam_strerror(ctx
->pamh
, ret
));
1194 * Set info3 strings into the PAM stack.
1196 * @param ctx PAM winbind context.
1197 * @param data_name Key name for pam_set_data.
1198 * @param value String value.
1203 static void _pam_set_data_info3(struct pwb_context
*ctx
,
1204 struct winbindd_response
*response
)
1206 _pam_set_data_string(ctx
, PAM_WINBIND_HOMEDIR
,
1207 response
->data
.auth
.info3
.home_dir
);
1208 _pam_set_data_string(ctx
, PAM_WINBIND_LOGONSCRIPT
,
1209 response
->data
.auth
.info3
.logon_script
);
1210 _pam_set_data_string(ctx
, PAM_WINBIND_LOGONSERVER
,
1211 response
->data
.auth
.info3
.logon_srv
);
1212 _pam_set_data_string(ctx
, PAM_WINBIND_PROFILEPATH
,
1213 response
->data
.auth
.info3
.profile_path
);
1217 * Free info3 strings in the PAM stack.
1219 * @param pamh PAM handle
1224 static void _pam_free_data_info3(pam_handle_t
*pamh
)
1226 pam_set_data(pamh
, PAM_WINBIND_HOMEDIR
, NULL
, NULL
);
1227 pam_set_data(pamh
, PAM_WINBIND_LOGONSCRIPT
, NULL
, NULL
);
1228 pam_set_data(pamh
, PAM_WINBIND_LOGONSERVER
, NULL
, NULL
);
1229 pam_set_data(pamh
, PAM_WINBIND_PROFILEPATH
, NULL
, NULL
);
1233 * Send PAM_ERROR_MSG for cached or grace logons.
1235 * @param ctx PAM winbind context.
1236 * @param username User in PAM request.
1237 * @param info3_user_flgs Info3 flags containing logon type bits.
1242 static void _pam_warn_logon_type(struct pwb_context
*ctx
,
1243 const char *username
,
1244 uint32_t info3_user_flgs
)
1246 /* inform about logon type */
1247 if (PAM_WB_GRACE_LOGON(info3_user_flgs
)) {
1249 _make_remark(ctx
, PAM_ERROR_MSG
,
1251 "Please change your password as soon you're "
1253 _pam_log_debug(ctx
, LOG_DEBUG
,
1254 "User %s logged on using grace logon\n",
1257 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs
)) {
1259 _make_remark(ctx
, PAM_ERROR_MSG
,
1260 "Domain Controller unreachable, "
1261 "using cached credentials instead. "
1262 "Network resources may be unavailable");
1263 _pam_log_debug(ctx
, LOG_DEBUG
,
1264 "User %s logged on using cached credentials\n",
1270 * Send PAM_ERROR_MSG for krb5 errors.
1272 * @param ctx PAM winbind context.
1273 * @param username User in PAM request.
1274 * @param info3_user_flgs Info3 flags containing logon type bits.
1279 static void _pam_warn_krb5_failure(struct pwb_context
*ctx
,
1280 const char *username
,
1281 uint32_t info3_user_flgs
)
1283 if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs
)) {
1284 _make_remark(ctx
, PAM_ERROR_MSG
,
1285 "Failed to establish your Kerberos Ticket cache "
1286 "due time differences\n"
1287 "with the domain controller. "
1288 "Please verify the system time.\n");
1289 _pam_log_debug(ctx
, LOG_DEBUG
,
1290 "User %s: Clock skew when getting Krb5 TGT\n",
1295 static bool _pam_check_remark_auth_err(struct pwb_context
*ctx
,
1296 const struct wbcAuthErrorInfo
*e
,
1297 const char *nt_status_string
,
1300 const char *ntstatus
= NULL
;
1301 const char *error_string
= NULL
;
1303 if (!e
|| !pam_error
) {
1307 ntstatus
= e
->nt_string
;
1312 if (strcasecmp(ntstatus
, nt_status_string
) == 0) {
1314 error_string
= _get_ntstatus_error_string(nt_status_string
);
1316 _make_remark(ctx
, PAM_ERROR_MSG
, error_string
);
1317 *pam_error
= e
->pam_error
;
1321 if (e
->display_string
) {
1322 _make_remark(ctx
, PAM_ERROR_MSG
, e
->display_string
);
1323 *pam_error
= e
->pam_error
;
1327 _make_remark(ctx
, PAM_ERROR_MSG
, nt_status_string
);
1328 *pam_error
= e
->pam_error
;
1337 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
1339 * @param response The struct winbindd_response.
1341 * @return string (caller needs to free).
1344 static char *_pam_compose_pwd_restriction_string(struct pwb_context
*ctx
,
1345 struct winbindd_response
*response
)
1349 str
= talloc_asprintf(ctx
, "Your password ");
1354 if (response
->data
.auth
.policy
.min_length_password
> 0) {
1355 str
= talloc_asprintf_append(str
,
1356 "must be at least %d characters; ",
1357 response
->data
.auth
.policy
.min_length_password
);
1363 if (response
->data
.auth
.policy
.password_history
> 0) {
1364 str
= talloc_asprintf_append(str
,
1365 "cannot repeat any of your previous %d "
1367 response
->data
.auth
.policy
.password_history
);
1373 if (response
->data
.auth
.policy
.password_properties
&
1374 DOMAIN_PASSWORD_COMPLEX
) {
1375 str
= talloc_asprintf_append(str
,
1376 "must contain capitals, numerals "
1378 "and cannot contain your account "
1385 str
= talloc_asprintf_append(str
,
1386 "Please type a different password. "
1387 "Type a password which meets these requirements in "
1388 "both text boxes.");
1400 /* talk to winbindd */
1401 static int winbind_auth_request(struct pwb_context
*ctx
,
1406 const int warn_pwd_expire
,
1407 struct winbindd_response
*p_response
,
1408 time_t *pwd_last_set
,
1411 struct winbindd_request request
;
1412 struct winbindd_response response
;
1414 bool already_expired
= false;
1416 ZERO_STRUCT(request
);
1417 ZERO_STRUCT(response
);
1423 strncpy(request
.data
.auth
.user
, user
,
1424 sizeof(request
.data
.auth
.user
)-1);
1426 strncpy(request
.data
.auth
.pass
, pass
,
1427 sizeof(request
.data
.auth
.pass
)-1);
1429 request
.data
.auth
.krb5_cc_type
[0] = '\0';
1430 request
.data
.auth
.uid
= -1;
1432 request
.flags
= WBFLAG_PAM_INFO3_TEXT
| WBFLAG_PAM_GET_PWD_POLICY
;
1434 /* Krb5 auth always has to go against the KDC of the user's realm */
1436 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1437 request
.flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
1440 if (ctx
->ctrl
& (WINBIND_KRB5_AUTH
|WINBIND_CACHED_LOGIN
)) {
1441 struct passwd
*pwd
= NULL
;
1443 pwd
= getpwnam(user
);
1445 return PAM_USER_UNKNOWN
;
1447 request
.data
.auth
.uid
= pwd
->pw_uid
;
1450 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1452 _pam_log_debug(ctx
, LOG_DEBUG
,
1453 "enabling krb5 login flag\n");
1455 request
.flags
|= WBFLAG_PAM_KRB5
|
1456 WBFLAG_PAM_FALLBACK_AFTER_KRB5
;
1459 if (ctx
->ctrl
& WINBIND_CACHED_LOGIN
) {
1460 _pam_log_debug(ctx
, LOG_DEBUG
,
1461 "enabling cached login flag\n");
1462 request
.flags
|= WBFLAG_PAM_CACHED_LOGIN
;
1467 request
.flags
|= WBFLAG_PAM_UNIX_NAME
;
1470 if (cctype
!= NULL
) {
1471 strncpy(request
.data
.auth
.krb5_cc_type
, cctype
,
1472 sizeof(request
.data
.auth
.krb5_cc_type
) - 1);
1473 _pam_log_debug(ctx
, LOG_DEBUG
,
1474 "enabling request for a %s krb5 ccache\n",
1478 request
.data
.auth
.require_membership_of_sid
[0] = '\0';
1480 if (member
!= NULL
) {
1482 if (!winbind_name_list_to_sid_string_list(ctx
, user
,
1484 request
.data
.auth
.require_membership_of_sid
,
1485 sizeof(request
.data
.auth
.require_membership_of_sid
))) {
1487 _pam_log_debug(ctx
, LOG_ERR
,
1488 "failed to serialize membership of sid "
1489 "\"%s\"\n", member
);
1490 return PAM_AUTH_ERR
;
1494 ret
= pam_winbind_request_log(ctx
, WINBINDD_PAM_AUTH
,
1495 &request
, &response
, user
);
1498 *pwd_last_set
= response
.data
.auth
.info3
.pass_last_set_time
;
1502 /* We want to process the response in the caller. */
1503 *p_response
= response
;
1508 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1509 "NT_STATUS_PASSWORD_EXPIRED");
1510 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1511 "NT_STATUS_PASSWORD_MUST_CHANGE");
1512 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1513 "NT_STATUS_INVALID_WORKSTATION");
1514 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1515 "NT_STATUS_INVALID_LOGON_HOURS");
1516 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1517 "NT_STATUS_ACCOUNT_EXPIRED");
1518 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1519 "NT_STATUS_ACCOUNT_DISABLED");
1520 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1521 "NT_STATUS_ACCOUNT_LOCKED_OUT");
1522 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1523 "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
1524 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1525 "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
1526 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1527 "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
1528 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1529 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1530 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1531 "NT_STATUS_NO_LOGON_SERVERS");
1532 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1533 "NT_STATUS_WRONG_PASSWORD");
1534 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1535 "NT_STATUS_ACCESS_DENIED");
1538 if (ret
== PAM_SUCCESS
) {
1540 /* warn a user if the password is about to expire soon */
1541 _pam_warn_password_expiry(ctx
, &response
,
1545 if (already_expired
== true) {
1546 SMB_TIME_T last_set
;
1547 last_set
= response
.data
.auth
.info3
.pass_last_set_time
;
1548 _pam_log_debug(ctx
, LOG_DEBUG
,
1549 "Password has expired "
1550 "(Password was last set: %lld, "
1551 "the policy says it should expire here "
1552 "%lld (now it's: %lu))\n",
1553 (long long int)last_set
,
1554 (long long int)last_set
+
1555 response
.data
.auth
.policy
.expire
,
1558 return PAM_AUTHTOK_EXPIRED
;
1561 /* inform about logon type */
1562 _pam_warn_logon_type(ctx
, user
,
1563 response
.data
.auth
.info3
.user_flgs
);
1565 /* inform about krb5 failures */
1566 _pam_warn_krb5_failure(ctx
, user
,
1567 response
.data
.auth
.info3
.user_flgs
);
1569 /* set some info3 info for other modules in the stack */
1570 _pam_set_data_info3(ctx
, &response
);
1572 /* put krb5ccname into env */
1573 _pam_setup_krb5_env(ctx
, response
.data
.auth
.krb5ccname
);
1575 /* If winbindd returned a username, return the pointer to it
1577 if (user_ret
&& response
.data
.auth
.unix_username
[0]) {
1578 /* We have to trust it's a null terminated string. */
1579 *user_ret
= strndup(response
.data
.auth
.unix_username
,
1580 sizeof(response
.data
.auth
.unix_username
) - 1);
1587 /* talk to winbindd */
1588 static int winbind_chauthtok_request(struct pwb_context
*ctx
,
1590 const char *oldpass
,
1591 const char *newpass
,
1592 time_t pwd_last_set
)
1594 struct winbindd_request request
;
1595 struct winbindd_response response
;
1598 ZERO_STRUCT(request
);
1599 ZERO_STRUCT(response
);
1601 if (request
.data
.chauthtok
.user
== NULL
) {
1605 strncpy(request
.data
.chauthtok
.user
, user
,
1606 sizeof(request
.data
.chauthtok
.user
) - 1);
1608 if (oldpass
!= NULL
) {
1609 strncpy(request
.data
.chauthtok
.oldpass
, oldpass
,
1610 sizeof(request
.data
.chauthtok
.oldpass
) - 1);
1612 request
.data
.chauthtok
.oldpass
[0] = '\0';
1615 if (newpass
!= NULL
) {
1616 strncpy(request
.data
.chauthtok
.newpass
, newpass
,
1617 sizeof(request
.data
.chauthtok
.newpass
) - 1);
1619 request
.data
.chauthtok
.newpass
[0] = '\0';
1622 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
1623 request
.flags
= WBFLAG_PAM_KRB5
|
1624 WBFLAG_PAM_CONTACT_TRUSTDOM
;
1627 if (ctx
->ctrl
& WINBIND_CACHED_LOGIN
) {
1628 request
.flags
|= WBFLAG_PAM_CACHED_LOGIN
;
1631 ret
= pam_winbind_request_log(ctx
, WINBINDD_PAM_CHAUTHTOK
,
1632 &request
, &response
, user
);
1634 if (ret
== PAM_SUCCESS
) {
1638 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1639 "NT_STATUS_BACKUP_CONTROLLER");
1640 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1641 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1642 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1643 "NT_STATUS_NO_LOGON_SERVERS");
1644 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1645 "NT_STATUS_ACCESS_DENIED");
1647 /* TODO: tell the min pwd length ? */
1648 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1649 "NT_STATUS_PWD_TOO_SHORT");
1651 /* TODO: tell the minage ? */
1652 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1653 "NT_STATUS_PWD_TOO_RECENT");
1655 /* TODO: tell the history length ? */
1656 PAM_WB_REMARK_CHECK_RESPONSE_RET(ctx
, response
,
1657 "NT_STATUS_PWD_HISTORY_CONFLICT");
1659 if (!strcasecmp(response
.data
.auth
.nt_status_string
,
1660 "NT_STATUS_PASSWORD_RESTRICTION")) {
1662 char *pwd_restriction_string
= NULL
;
1663 SMB_TIME_T min_pwd_age
;
1664 uint32_t reject_reason
= response
.data
.auth
.reject_reason
;
1665 min_pwd_age
= response
.data
.auth
.policy
.min_passwordage
;
1667 /* FIXME: avoid to send multiple PAM messages after another */
1668 switch (reject_reason
) {
1671 case SAMR_REJECT_OTHER
:
1672 if ((min_pwd_age
> 0) &&
1673 (pwd_last_set
+ min_pwd_age
> time(NULL
))) {
1674 PAM_WB_REMARK_DIRECT(ctx
,
1675 "NT_STATUS_PWD_TOO_RECENT");
1678 case SAMR_REJECT_TOO_SHORT
:
1679 PAM_WB_REMARK_DIRECT(ctx
,
1680 "NT_STATUS_PWD_TOO_SHORT");
1682 case SAMR_REJECT_IN_HISTORY
:
1683 PAM_WB_REMARK_DIRECT(ctx
,
1684 "NT_STATUS_PWD_HISTORY_CONFLICT");
1686 case SAMR_REJECT_COMPLEXITY
:
1687 _make_remark(ctx
, PAM_ERROR_MSG
,
1688 "Password does not meet "
1689 "complexity requirements");
1692 _pam_log_debug(ctx
, LOG_DEBUG
,
1693 "unknown password change "
1694 "reject reason: %d",
1699 pwd_restriction_string
=
1700 _pam_compose_pwd_restriction_string(ctx
, &response
);
1701 if (pwd_restriction_string
) {
1702 _make_remark(ctx
, PAM_ERROR_MSG
,
1703 pwd_restriction_string
);
1704 TALLOC_FREE(pwd_restriction_string
);
1712 * Checks if a user has an account
1715 * 1 = User not found
1719 static int valid_user(struct pwb_context
*ctx
,
1722 /* check not only if the user is available over NSS calls, also make
1723 * sure it's really a winbind user, this is important when stacking PAM
1724 * modules in the 'account' or 'password' facility. */
1726 struct passwd
*pwd
= NULL
;
1727 struct winbindd_request request
;
1728 struct winbindd_response response
;
1731 ZERO_STRUCT(request
);
1732 ZERO_STRUCT(response
);
1734 pwd
= getpwnam(user
);
1739 strncpy(request
.data
.username
, user
,
1740 sizeof(request
.data
.username
) - 1);
1742 ret
= pam_winbind_request_log(ctx
, WINBINDD_GETPWNAM
,
1743 &request
, &response
, user
);
1746 case PAM_USER_UNKNOWN
:
1756 static char *_pam_delete(register char *xx
)
1764 * obtain a password from the user
1767 static int _winbind_read_password(struct pwb_context
*ctx
,
1769 const char *comment
,
1770 const char *prompt1
,
1771 const char *prompt2
,
1779 _pam_log(ctx
, LOG_DEBUG
, "getting password (0x%08x)", ctrl
);
1782 * make sure nothing inappropriate gets returned
1785 *pass
= token
= NULL
;
1788 * which authentication token are we getting?
1791 if (on(WINBIND__OLD_PASSWORD
, ctrl
)) {
1792 authtok_flag
= PAM_OLDAUTHTOK
;
1794 authtok_flag
= PAM_AUTHTOK
;
1798 * should we obtain the password from a PAM item ?
1801 if (on(WINBIND_TRY_FIRST_PASS_ARG
, ctrl
) ||
1802 on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
1803 retval
= _pam_get_item(ctx
->pamh
, authtok_flag
, &item
);
1804 if (retval
!= PAM_SUCCESS
) {
1806 _pam_log(ctx
, LOG_ALERT
,
1807 "pam_get_item returned error "
1808 "to unix-read-password");
1810 } else if (item
!= NULL
) { /* we have a password! */
1813 _pam_log(ctx
, LOG_DEBUG
,
1814 "pam_get_item returned a password");
1816 } else if (on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
1817 return PAM_AUTHTOK_RECOVER_ERR
; /* didn't work */
1818 } else if (on(WINBIND_USE_AUTHTOK_ARG
, ctrl
)
1819 && off(WINBIND__OLD_PASSWORD
, ctrl
)) {
1820 return PAM_AUTHTOK_RECOVER_ERR
;
1824 * getting here implies we will have to get the password from the
1829 struct pam_message msg
[3], *pmsg
[3];
1830 struct pam_response
*resp
;
1833 /* prepare to converse */
1835 if (comment
!= NULL
&& off(ctrl
, WINBIND_SILENT
)) {
1837 msg
[0].msg_style
= PAM_TEXT_INFO
;
1838 msg
[0].msg
= discard_const_p(char, comment
);
1845 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
1846 msg
[i
++].msg
= discard_const_p(char, prompt1
);
1849 if (prompt2
!= NULL
) {
1851 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
1852 msg
[i
++].msg
= discard_const_p(char, prompt2
);
1855 /* so call the conversation expecting i responses */
1857 retval
= converse(ctx
->pamh
, i
, pmsg
, &resp
);
1859 if (retval
== PAM_SUCCESS
) {
1860 retval
= PAM_AUTHTOK_RECOVER_ERR
;
1864 if (retval
!= PAM_SUCCESS
) {
1865 _pam_drop_reply(resp
, i
);
1869 /* interpret the response */
1871 token
= x_strdup(resp
[i
- replies
].resp
);
1873 _pam_log(ctx
, LOG_NOTICE
,
1874 "could not recover "
1875 "authentication token");
1876 retval
= PAM_AUTHTOK_RECOVER_ERR
;
1881 /* verify that password entered correctly */
1882 if (!resp
[i
- 1].resp
||
1883 strcmp(token
, resp
[i
- 1].resp
)) {
1884 _pam_delete(token
); /* mistyped */
1885 retval
= PAM_AUTHTOK_RECOVER_ERR
;
1886 _make_remark(ctx
, PAM_ERROR_MSG
,
1892 * tidy up the conversation (resp_retcode) is ignored
1893 * -- what is it for anyway? AGM
1895 _pam_drop_reply(resp
, i
);
1899 if (retval
!= PAM_SUCCESS
) {
1900 _pam_log_debug(ctx
, LOG_DEBUG
,
1901 "unable to obtain a password");
1904 /* 'token' is the entered password */
1906 /* we store this password as an item */
1908 retval
= pam_set_item(ctx
->pamh
, authtok_flag
, token
);
1909 _pam_delete(token
); /* clean it up */
1910 if (retval
!= PAM_SUCCESS
||
1911 (retval
= _pam_get_item(ctx
->pamh
, authtok_flag
, &item
)) != PAM_SUCCESS
) {
1913 _pam_log(ctx
, LOG_CRIT
, "error manipulating password");
1919 item
= NULL
; /* break link to password */
1924 static const char *get_conf_item_string(struct pwb_context
*ctx
,
1929 const char *parm_opt
= NULL
;
1931 if (!(ctx
->ctrl
& config_flag
)) {
1935 /* let the pam opt take precedence over the pam_winbind.conf option */
1936 for (i
=0; i
<ctx
->argc
; i
++) {
1938 if ((strncmp(ctx
->argv
[i
], item
, strlen(item
)) == 0)) {
1941 if ((p
= strchr(ctx
->argv
[i
], '=')) == NULL
) {
1942 _pam_log(ctx
, LOG_INFO
,
1943 "no \"=\" delimiter for \"%s\" found\n",
1947 _pam_log_debug(ctx
, LOG_INFO
,
1948 "PAM config: %s '%s'\n", item
, p
+1);
1956 key
= talloc_asprintf(ctx
, "global:%s", item
);
1961 parm_opt
= iniparser_getstr(ctx
->dict
, key
);
1964 _pam_log_debug(ctx
, LOG_INFO
, "CONFIG file: %s '%s'\n",
1971 static int get_config_item_int(struct pwb_context
*ctx
,
1975 int i
, parm_opt
= -1;
1977 if (!(ctx
->ctrl
& config_flag
)) {
1981 /* let the pam opt take precedence over the pam_winbind.conf option */
1982 for (i
= 0; i
< ctx
->argc
; i
++) {
1984 if ((strncmp(ctx
->argv
[i
], item
, strlen(item
)) == 0)) {
1987 if ((p
= strchr(ctx
->argv
[i
], '=')) == NULL
) {
1988 _pam_log(ctx
, LOG_INFO
,
1989 "no \"=\" delimiter for \"%s\" found\n",
1993 parm_opt
= atoi(p
+ 1);
1994 _pam_log_debug(ctx
, LOG_INFO
,
1995 "PAM config: %s '%d'\n",
2004 key
= talloc_asprintf(ctx
, "global:%s", item
);
2009 parm_opt
= iniparser_getint(ctx
->dict
, key
, -1);
2012 _pam_log_debug(ctx
, LOG_INFO
,
2013 "CONFIG file: %s '%d'\n",
2020 static const char *get_krb5_cc_type_from_config(struct pwb_context
*ctx
)
2022 return get_conf_item_string(ctx
, "krb5_ccache_type",
2023 WINBIND_KRB5_CCACHE_TYPE
);
2026 static const char *get_member_from_config(struct pwb_context
*ctx
)
2028 const char *ret
= NULL
;
2029 ret
= get_conf_item_string(ctx
, "require_membership_of",
2030 WINBIND_REQUIRED_MEMBERSHIP
);
2034 return get_conf_item_string(ctx
, "require-membership-of",
2035 WINBIND_REQUIRED_MEMBERSHIP
);
2038 static int get_warn_pwd_expire_from_config(struct pwb_context
*ctx
)
2041 ret
= get_config_item_int(ctx
, "warn_pwd_expire",
2042 WINBIND_WARN_PWD_EXPIRE
);
2043 /* no or broken setting */
2045 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES
;
2051 * Retrieve the winbind separator.
2053 * @param ctx PAM winbind context.
2055 * @return string separator character. NULL on failure.
2058 static char winbind_get_separator(struct pwb_context
*ctx
)
2060 struct winbindd_request request
;
2061 struct winbindd_response response
;
2063 ZERO_STRUCT(request
);
2064 ZERO_STRUCT(response
);
2066 if (pam_winbind_request_log(ctx
, WINBINDD_INFO
,
2067 &request
, &response
, NULL
)) {
2071 return response
.data
.info
.winbind_separator
;
2075 * Convert a upn to a name.
2077 * @param ctx PAM winbind context.
2078 * @param upn USer UPN to be trabslated.
2080 * @return converted name. NULL pointer on failure. Caller needs to free.
2083 static char* winbind_upn_to_username(struct pwb_context
*ctx
,
2086 struct winbindd_request req
;
2087 struct winbindd_response resp
;
2091 /* This cannot work when the winbind separator = @ */
2093 sep
= winbind_get_separator(ctx
);
2094 if (!sep
|| sep
== '@') {
2098 /* Convert the UPN to a SID */
2103 strncpy(req
.data
.name
.dom_name
, "",
2104 sizeof(req
.data
.name
.dom_name
) - 1);
2105 strncpy(req
.data
.name
.name
, upn
,
2106 sizeof(req
.data
.name
.name
) - 1);
2107 retval
= pam_winbind_request_log(ctx
, WINBINDD_LOOKUPNAME
,
2109 if (retval
!= PAM_SUCCESS
) {
2113 /* Convert the the SID back to the sAMAccountName */
2116 strncpy(req
.data
.sid
, resp
.data
.sid
.sid
, sizeof(req
.data
.sid
)-1);
2118 retval
= pam_winbind_request_log(ctx
, WINBINDD_LOOKUPSID
,
2120 if (retval
!= PAM_SUCCESS
) {
2124 return talloc_asprintf(ctx
, "%s\\%s",
2125 resp
.data
.name
.dom_name
,
2126 resp
.data
.name
.name
);
2130 int pam_sm_authenticate(pam_handle_t
*pamh
, int flags
,
2131 int argc
, const char **argv
)
2133 const char *username
;
2134 const char *password
;
2135 const char *member
= NULL
;
2136 const char *cctype
= NULL
;
2137 int warn_pwd_expire
;
2138 int retval
= PAM_AUTH_ERR
;
2139 char *username_ret
= NULL
;
2140 char *new_authtok_required
= NULL
;
2141 char *real_username
= NULL
;
2142 struct pwb_context
*ctx
= NULL
;
2144 retval
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2149 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", ctx
);
2151 /* Get the username */
2152 retval
= pam_get_user(pamh
, &username
, NULL
);
2153 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
2154 _pam_log_debug(ctx
, LOG_DEBUG
,
2155 "can not get the username");
2156 retval
= PAM_SERVICE_ERR
;
2162 /* Decode the user name since AIX does not support logn user
2163 names by default. The name is encoded as _#uid. */
2165 if (username
[0] == '_') {
2166 uid_t id
= atoi(&username
[1]);
2167 struct passwd
*pw
= NULL
;
2169 if ((id
!=0) && ((pw
= getpwuid(id
)) != NULL
)) {
2170 real_username
= strdup(pw
->pw_name
);
2175 if (!real_username
) {
2176 /* Just making a copy of the username we got from PAM */
2177 if ((real_username
= strdup(username
)) == NULL
) {
2178 _pam_log_debug(ctx
, LOG_DEBUG
,
2179 "memory allocation failure when copying "
2181 retval
= PAM_SERVICE_ERR
;
2186 /* Maybe this was a UPN */
2188 if (strchr(real_username
, '@') != NULL
) {
2189 char *samaccountname
= NULL
;
2191 samaccountname
= winbind_upn_to_username(ctx
,
2193 if (samaccountname
) {
2194 free(real_username
);
2195 real_username
= strdup(samaccountname
);
2199 retval
= _winbind_read_password(ctx
, ctx
->ctrl
, NULL
,
2203 if (retval
!= PAM_SUCCESS
) {
2204 _pam_log(ctx
, LOG_ERR
,
2205 "Could not retrieve user's password");
2206 retval
= PAM_AUTHTOK_ERR
;
2210 /* Let's not give too much away in the log file */
2212 #ifdef DEBUG_PASSWORD
2213 _pam_log_debug(ctx
, LOG_INFO
,
2214 "Verify user '%s' with password '%s'",
2215 real_username
, password
);
2217 _pam_log_debug(ctx
, LOG_INFO
,
2218 "Verify user '%s'", real_username
);
2221 member
= get_member_from_config(ctx
);
2222 cctype
= get_krb5_cc_type_from_config(ctx
);
2223 warn_pwd_expire
= get_warn_pwd_expire_from_config(ctx
);
2225 /* Now use the username to look up password */
2226 retval
= winbind_auth_request(ctx
, real_username
, password
,
2227 member
, cctype
, warn_pwd_expire
, NULL
,
2228 NULL
, &username_ret
);
2230 if (retval
== PAM_NEW_AUTHTOK_REQD
||
2231 retval
== PAM_AUTHTOK_EXPIRED
) {
2233 char *new_authtok_required_during_auth
= NULL
;
2235 new_authtok_required
= talloc_asprintf(NULL
, "%d", retval
);
2236 if (!new_authtok_required
) {
2237 retval
= PAM_BUF_ERR
;
2241 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
,
2242 new_authtok_required
,
2243 _pam_winbind_cleanup_func
);
2245 retval
= PAM_SUCCESS
;
2247 new_authtok_required_during_auth
= talloc_asprintf(NULL
, "%d", true);
2248 if (!new_authtok_required_during_auth
) {
2249 retval
= PAM_BUF_ERR
;
2253 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2254 new_authtok_required_during_auth
,
2255 _pam_winbind_cleanup_func
);
2262 pam_set_item (pamh
, PAM_USER
, username_ret
);
2263 _pam_log_debug(ctx
, LOG_INFO
,
2264 "Returned user was '%s'", username_ret
);
2268 if (real_username
) {
2269 free(real_username
);
2272 if (!new_authtok_required
) {
2273 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, NULL
, NULL
);
2276 if (retval
!= PAM_SUCCESS
) {
2277 _pam_free_data_info3(pamh
);
2280 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", ctx
, retval
);
2288 int pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
2289 int argc
, const char **argv
)
2291 int ret
= PAM_SYSTEM_ERR
;
2292 struct pwb_context
*ctx
= NULL
;
2294 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2299 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", ctx
);
2301 switch (flags
& ~PAM_SILENT
) {
2303 case PAM_DELETE_CRED
:
2304 ret
= pam_sm_close_session(pamh
, flags
, argc
, argv
);
2306 case PAM_REFRESH_CRED
:
2307 _pam_log_debug(ctx
, LOG_WARNING
,
2308 "PAM_REFRESH_CRED not implemented");
2311 case PAM_REINITIALIZE_CRED
:
2312 _pam_log_debug(ctx
, LOG_WARNING
,
2313 "PAM_REINITIALIZE_CRED not implemented");
2316 case PAM_ESTABLISH_CRED
:
2317 _pam_log_debug(ctx
, LOG_WARNING
,
2318 "PAM_ESTABLISH_CRED not implemented");
2322 ret
= PAM_SYSTEM_ERR
;
2328 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", ctx
, ret
);
2336 * Account management. We want to verify that the account exists
2337 * before returning PAM_SUCCESS
2340 int pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags
,
2341 int argc
, const char **argv
)
2343 const char *username
;
2344 int ret
= PAM_USER_UNKNOWN
;
2346 struct pwb_context
*ctx
= NULL
;
2348 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2353 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", ctx
);
2356 /* Get the username */
2357 ret
= pam_get_user(pamh
, &username
, NULL
);
2358 if ((ret
!= PAM_SUCCESS
) || (!username
)) {
2359 _pam_log_debug(ctx
, LOG_DEBUG
,
2360 "can not get the username");
2361 ret
= PAM_SERVICE_ERR
;
2365 /* Verify the username */
2366 ret
= valid_user(ctx
, username
);
2369 /* some sort of system error. The log was already printed */
2370 ret
= PAM_SERVICE_ERR
;
2373 /* the user does not exist */
2374 _pam_log_debug(ctx
, LOG_NOTICE
, "user '%s' not found",
2376 if (ctx
->ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
2380 ret
= PAM_USER_UNKNOWN
;
2383 pam_get_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
,
2384 (const void **)&tmp
);
2386 ret
= atoi((const char *)tmp
);
2388 case PAM_AUTHTOK_EXPIRED
:
2389 /* fall through, since new token is required in this case */
2390 case PAM_NEW_AUTHTOK_REQD
:
2391 _pam_log(ctx
, LOG_WARNING
,
2392 "pam_sm_acct_mgmt success but %s is set",
2393 PAM_WINBIND_NEW_AUTHTOK_REQD
);
2394 _pam_log(ctx
, LOG_NOTICE
,
2395 "user '%s' needs new password",
2397 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2398 ret
= PAM_NEW_AUTHTOK_REQD
;
2401 _pam_log(ctx
, LOG_WARNING
,
2402 "pam_sm_acct_mgmt success");
2403 _pam_log(ctx
, LOG_NOTICE
,
2404 "user '%s' granted access", username
);
2410 /* Otherwise, the authentication looked good */
2411 _pam_log(ctx
, LOG_NOTICE
,
2412 "user '%s' granted access", username
);
2416 /* we don't know anything about this return value */
2417 _pam_log(ctx
, LOG_ERR
,
2418 "internal module error (ret = %d, user = '%s')",
2420 ret
= PAM_SERVICE_ERR
;
2424 /* should not be reached */
2429 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", ctx
, ret
);
2437 int pam_sm_open_session(pam_handle_t
*pamh
, int flags
,
2438 int argc
, const char **argv
)
2440 int ret
= PAM_SYSTEM_ERR
;
2441 struct pwb_context
*ctx
= NULL
;
2443 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2448 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", ctx
);
2453 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", ctx
, ret
);
2461 int pam_sm_close_session(pam_handle_t
*pamh
, int flags
,
2462 int argc
, const char **argv
)
2464 int retval
= PAM_SUCCESS
;
2465 struct pwb_context
*ctx
= NULL
;
2467 retval
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2472 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", ctx
);
2474 if (!(flags
& PAM_DELETE_CRED
)) {
2475 retval
= PAM_SUCCESS
;
2479 if (ctx
->ctrl
& WINBIND_KRB5_AUTH
) {
2481 /* destroy the ccache here */
2482 struct winbindd_request request
;
2483 struct winbindd_response response
;
2485 const char *ccname
= NULL
;
2486 struct passwd
*pwd
= NULL
;
2488 ZERO_STRUCT(request
);
2489 ZERO_STRUCT(response
);
2491 retval
= pam_get_user(pamh
, &user
, "Username: ");
2493 _pam_log(ctx
, LOG_ERR
,
2494 "could not identify user");
2499 _pam_log(ctx
, LOG_ERR
,
2500 "username was NULL!");
2501 retval
= PAM_USER_UNKNOWN
;
2505 _pam_log_debug(ctx
, LOG_DEBUG
,
2506 "username [%s] obtained", user
);
2508 ccname
= pam_getenv(pamh
, "KRB5CCNAME");
2509 if (ccname
== NULL
) {
2510 _pam_log_debug(ctx
, LOG_DEBUG
,
2511 "user has no KRB5CCNAME environment");
2514 strncpy(request
.data
.logoff
.user
, user
,
2515 sizeof(request
.data
.logoff
.user
) - 1);
2518 strncpy(request
.data
.logoff
.krb5ccname
, ccname
,
2519 sizeof(request
.data
.logoff
.krb5ccname
) - 1);
2522 pwd
= getpwnam(user
);
2524 retval
= PAM_USER_UNKNOWN
;
2527 request
.data
.logoff
.uid
= pwd
->pw_uid
;
2529 request
.flags
= WBFLAG_PAM_KRB5
|
2530 WBFLAG_PAM_CONTACT_TRUSTDOM
;
2532 retval
= pam_winbind_request_log(ctx
,
2533 WINBINDD_PAM_LOGOFF
,
2534 &request
, &response
, user
);
2539 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", ctx
, retval
);
2547 * evaluate whether we need to re-authenticate with kerberos after a
2550 * @param ctx PAM winbind context.
2551 * @param user The username
2553 * @return boolean Returns true if required, false if not.
2556 static bool _pam_require_krb5_auth_after_chauthtok(struct pwb_context
*ctx
,
2560 /* Make sure that we only do this if a) the chauthtok got initiated
2561 * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2562 * later password change via the "passwd" command if done by the user
2564 * NB. If we login from gdm or xdm and the password expires,
2565 * we change the password, but there is no memory cache.
2566 * Thus, even for passthrough login, we should do the
2567 * authentication again to update memory cache.
2571 char *new_authtok_reqd_during_auth
= NULL
;
2572 struct passwd
*pwd
= NULL
;
2574 _pam_get_data(ctx
->pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2575 &new_authtok_reqd_during_auth
);
2576 pam_set_data(ctx
->pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2579 if (new_authtok_reqd_during_auth
) {
2583 pwd
= getpwnam(user
);
2588 if (getuid() == pwd
->pw_uid
) {
2597 int pam_sm_chauthtok(pam_handle_t
* pamh
, int flags
,
2598 int argc
, const char **argv
)
2602 bool cached_login
= false;
2604 /* <DO NOT free() THESE> */
2606 char *pass_old
, *pass_new
;
2607 /* </DO NOT free() THESE> */
2612 char *username_ret
= NULL
;
2613 struct winbindd_response response
;
2614 struct pwb_context
*ctx
= NULL
;
2616 ZERO_STRUCT(response
);
2618 ret
= _pam_winbind_init_context(pamh
, flags
, argc
, argv
, &ctx
);
2623 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", ctx
);
2625 cached_login
= (ctx
->ctrl
& WINBIND_CACHED_LOGIN
);
2627 /* clearing offline bit for auth */
2628 ctx
->ctrl
&= ~WINBIND_CACHED_LOGIN
;
2631 * First get the name of a user
2633 ret
= pam_get_user(pamh
, &user
, "Username: ");
2635 _pam_log(ctx
, LOG_ERR
,
2636 "password - could not identify user");
2641 _pam_log(ctx
, LOG_ERR
, "username was NULL!");
2642 ret
= PAM_USER_UNKNOWN
;
2646 _pam_log_debug(ctx
, LOG_DEBUG
, "username [%s] obtained", user
);
2648 /* check if this is really a user in winbindd, not only in NSS */
2649 ret
= valid_user(ctx
, user
);
2652 ret
= PAM_USER_UNKNOWN
;
2655 ret
= PAM_SYSTEM_ERR
;
2662 * obtain and verify the current password (OLDAUTHTOK) for
2666 if (flags
& PAM_PRELIM_CHECK
) {
2667 time_t pwdlastset_prelim
= 0;
2669 /* instruct user what is happening */
2671 #define greeting "Changing password for"
2672 Announce
= talloc_asprintf(ctx
, "%s %s", greeting
, user
);
2674 _pam_log(ctx
, LOG_CRIT
,
2675 "password - out of memory");
2681 lctrl
= ctx
->ctrl
| WINBIND__OLD_PASSWORD
;
2682 ret
= _winbind_read_password(ctx
, lctrl
,
2684 "(current) NT password: ",
2686 (const char **) &pass_old
);
2687 TALLOC_FREE(Announce
);
2688 if (ret
!= PAM_SUCCESS
) {
2689 _pam_log(ctx
, LOG_NOTICE
,
2690 "password - (old) token not obtained");
2694 /* verify that this is the password for this user */
2696 ret
= winbind_auth_request(ctx
, user
, pass_old
,
2697 NULL
, NULL
, 0, &response
,
2698 &pwdlastset_prelim
, NULL
);
2700 if (ret
!= PAM_ACCT_EXPIRED
&&
2701 ret
!= PAM_AUTHTOK_EXPIRED
&&
2702 ret
!= PAM_NEW_AUTHTOK_REQD
&&
2703 ret
!= PAM_SUCCESS
) {
2708 pam_set_data(pamh
, PAM_WINBIND_PWD_LAST_SET
,
2709 (void *)pwdlastset_prelim
, NULL
);
2711 ret
= pam_set_item(pamh
, PAM_OLDAUTHTOK
,
2712 (const void *) pass_old
);
2714 if (ret
!= PAM_SUCCESS
) {
2715 _pam_log(ctx
, LOG_CRIT
,
2716 "failed to set PAM_OLDAUTHTOK");
2718 } else if (flags
& PAM_UPDATE_AUTHTOK
) {
2720 time_t pwdlastset_update
= 0;
2723 * obtain the proposed password
2727 * get the old token back.
2730 ret
= _pam_get_item(pamh
, PAM_OLDAUTHTOK
, &pass_old
);
2732 if (ret
!= PAM_SUCCESS
) {
2733 _pam_log(ctx
, LOG_NOTICE
,
2734 "user not authenticated");
2738 lctrl
= ctx
->ctrl
& ~WINBIND_TRY_FIRST_PASS_ARG
;
2740 if (on(WINBIND_USE_AUTHTOK_ARG
, lctrl
)) {
2741 lctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
2744 ret
= PAM_AUTHTOK_ERR
;
2745 while ((ret
!= PAM_SUCCESS
) && (retry
++ < MAX_PASSWD_TRIES
)) {
2747 * use_authtok is to force the use of a previously entered
2748 * password -- needed for pluggable password strength checking
2751 ret
= _winbind_read_password(ctx
, lctrl
,
2753 "Enter new NT password: ",
2754 "Retype new NT password: ",
2755 (const char **)&pass_new
);
2757 if (ret
!= PAM_SUCCESS
) {
2758 _pam_log_debug(ctx
, LOG_ALERT
,
2760 "new password not obtained");
2761 pass_old
= NULL
;/* tidy up */
2766 * At this point we know who the user is and what they
2767 * propose as their new password. Verify that the new
2768 * password is acceptable.
2771 if (pass_new
[0] == '\0') {/* "\0" password = NULL */
2777 * By reaching here we have approved the passwords and must now
2778 * rebuild the password database file.
2780 _pam_get_data(pamh
, PAM_WINBIND_PWD_LAST_SET
,
2781 &pwdlastset_update
);
2784 * if cached creds were enabled, make sure to set the
2785 * WINBIND_CACHED_LOGIN bit here in order to have winbindd
2786 * update the cached creds storage - gd
2789 ctx
->ctrl
|= WINBIND_CACHED_LOGIN
;
2792 ret
= winbind_chauthtok_request(ctx
, user
, pass_old
,
2793 pass_new
, pwdlastset_update
);
2795 _pam_overwrite(pass_new
);
2796 _pam_overwrite(pass_old
);
2797 pass_old
= pass_new
= NULL
;
2801 if (_pam_require_krb5_auth_after_chauthtok(ctx
, user
)) {
2803 const char *member
= NULL
;
2804 const char *cctype
= NULL
;
2805 int warn_pwd_expire
;
2807 member
= get_member_from_config(ctx
);
2808 cctype
= get_krb5_cc_type_from_config(ctx
);
2809 warn_pwd_expire
= get_warn_pwd_expire_from_config(ctx
);
2811 /* Keep WINBIND_CACHED_LOGIN bit for
2812 * authentication after changing the password.
2813 * This will update the cached credentials in case
2814 * that winbindd_dual_pam_chauthtok() fails
2819 ret
= winbind_auth_request(ctx
, user
, pass_new
,
2820 member
, cctype
, 0, &response
,
2821 NULL
, &username_ret
);
2822 _pam_overwrite(pass_new
);
2823 _pam_overwrite(pass_old
);
2824 pass_old
= pass_new
= NULL
;
2826 if (ret
== PAM_SUCCESS
) {
2828 /* warn a user if the password is about to
2830 _pam_warn_password_expiry(ctx
, &response
,
2834 /* set some info3 info for other modules in the
2836 _pam_set_data_info3(ctx
, &response
);
2838 /* put krb5ccname into env */
2839 _pam_setup_krb5_env(ctx
,
2840 response
.data
.auth
.krb5ccname
);
2843 pam_set_item(pamh
, PAM_USER
,
2845 _pam_log_debug(ctx
, LOG_INFO
,
2846 "Returned user was '%s'",
2855 ret
= PAM_SERVICE_ERR
;
2860 /* Deal with offline errors. */
2861 PAM_WB_REMARK_CHECK_RESPONSE(ctx
, response
,
2862 "NT_STATUS_NO_LOGON_SERVERS");
2863 PAM_WB_REMARK_CHECK_RESPONSE(ctx
, response
,
2864 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
2865 PAM_WB_REMARK_CHECK_RESPONSE(ctx
, response
,
2866 "NT_STATUS_ACCESS_DENIED");
2868 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", ctx
, ret
);
2877 /* static module data */
2879 struct pam_module _pam_winbind_modstruct
= {
2881 pam_sm_authenticate
,
2884 pam_sm_open_session
,
2885 pam_sm_close_session
,
2892 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
2893 * Copyright (c) Tim Potter <tpot@samba.org> 2000
2894 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
2895 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2008
2896 * Copyright (c) Jan Rêkorajski 1999.
2897 * Copyright (c) Andrew G. Morgan 1996-8.
2898 * Copyright (c) Alex O. Yuriev, 1996.
2899 * Copyright (c) Cristian Gafton 1996.
2900 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
2902 * Redistribution and use in source and binary forms, with or without
2903 * modification, are permitted provided that the following conditions
2905 * 1. Redistributions of source code must retain the above copyright
2906 * notice, and the entire permission notice in its entirety,
2907 * including the disclaimer of warranties.
2908 * 2. Redistributions in binary form must reproduce the above copyright
2909 * notice, this list of conditions and the following disclaimer in the
2910 * documentation and/or other materials provided with the distribution.
2911 * 3. The name of the author may not be used to endorse or promote
2912 * products derived from this software without specific prior
2913 * written permission.
2915 * ALTERNATIVELY, this product may be distributed under the terms of
2916 * the GNU Public License, in which case the provisions of the GPL are
2917 * required INSTEAD OF the above restrictions. (This clause is
2918 * necessary due to a potential bad interaction between the GPL and
2919 * the restrictions contained in a BSD-style copyright.)
2921 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
2922 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2923 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2924 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2925 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2926 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2927 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2928 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2929 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2930 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2931 * OF THE POSSIBILITY OF SUCH DAMAGE.