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 enum pam_winbind_request_type
17 PAM_WINBIND_AUTHENTICATE,
19 PAM_WINBIND_ACCT_MGMT,
20 PAM_WINBIND_OPEN_SESSION,
21 PAM_WINBIND_CLOSE_SESSION,
22 PAM_WINBIND_CHAUTHTOK,
26 static int wbc_error_to_pam_error(wbcErr status)
31 case WBC_ERR_NOT_IMPLEMENTED:
32 return PAM_SERVICE_ERR;
33 case WBC_ERR_UNKNOWN_FAILURE:
35 case WBC_ERR_NO_MEMORY:
37 case WBC_ERR_INVALID_SID:
38 case WBC_ERR_INVALID_PARAM:
40 case WBC_ERR_WINBIND_NOT_AVAILABLE:
41 return PAM_AUTHINFO_UNAVAIL;
42 case WBC_ERR_DOMAIN_NOT_FOUND:
43 return PAM_AUTHINFO_UNAVAIL;
44 case WBC_ERR_INVALID_RESPONSE:
46 case WBC_ERR_NSS_ERROR:
47 return PAM_USER_UNKNOWN;
48 case WBC_ERR_AUTH_ERROR:
50 case WBC_ERR_UNKNOWN_USER:
51 return PAM_USER_UNKNOWN;
52 case WBC_ERR_UNKNOWN_GROUP:
53 return PAM_USER_UNKNOWN;
54 case WBC_ERR_PWD_CHANGE_FAILED:
62 static const char *_pam_error_code_str(int err)
68 return "PAM_OPEN_ERR";
70 return "PAM_SYMBOL_ERR";
72 return "PAM_SERVICE_ERR";
74 return "PAM_SYSTEM_ERR";
78 return "PAM_PERM_DENIED";
80 return "PAM_AUTH_ERR";
81 case PAM_CRED_INSUFFICIENT:
82 return "PAM_CRED_INSUFFICIENT";
83 case PAM_AUTHINFO_UNAVAIL:
84 return "PAM_AUTHINFO_UNAVAIL";
85 case PAM_USER_UNKNOWN:
86 return "PAM_USER_UNKNOWN";
88 return "PAM_MAXTRIES";
89 case PAM_NEW_AUTHTOK_REQD:
90 return "PAM_NEW_AUTHTOK_REQD";
91 case PAM_ACCT_EXPIRED:
92 return "PAM_ACCT_EXPIRED";
94 return "PAM_SESSION_ERR";
95 case PAM_CRED_UNAVAIL:
96 return "PAM_CRED_UNAVAIL";
97 case PAM_CRED_EXPIRED:
98 return "PAM_CRED_EXPIRED";
100 return "PAM_CRED_ERR";
101 case PAM_NO_MODULE_DATA:
102 return "PAM_NO_MODULE_DATA";
104 return "PAM_CONV_ERR";
105 case PAM_AUTHTOK_ERR:
106 return "PAM_AUTHTOK_ERR";
107 case PAM_AUTHTOK_RECOVER_ERR:
108 return "PAM_AUTHTOK_RECOVER_ERR";
109 case PAM_AUTHTOK_LOCK_BUSY:
110 return "PAM_AUTHTOK_LOCK_BUSY";
111 case PAM_AUTHTOK_DISABLE_AGING:
112 return "PAM_AUTHTOK_DISABLE_AGING";
114 return "PAM_TRY_AGAIN";
119 case PAM_AUTHTOK_EXPIRED:
120 return "PAM_AUTHTOK_EXPIRED";
121 #ifdef PAM_MODULE_UNKNOWN
122 case PAM_MODULE_UNKNOWN:
123 return "PAM_MODULE_UNKNOWN";
127 return "PAM_BAD_ITEM";
129 #ifdef PAM_CONV_AGAIN
131 return "PAM_CONV_AGAIN";
133 #ifdef PAM_INCOMPLETE
135 return "PAM_INCOMPLETE";
142 #define _PAM_LOG_FUNCTION_ENTER(function, ctx) \
144 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] ENTER: " \
145 function " (flags: 0x%04x)", ctx->pamh, ctx->flags); \
146 _pam_log_state(ctx); \
149 #define _PAM_LOG_FUNCTION_LEAVE(function, ctx, retval) \
151 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] LEAVE: " \
152 function " returning %d (%s)", ctx ? ctx->pamh : NULL, retval, \
153 _pam_error_code_str(retval)); \
154 _pam_log_state(ctx); \
159 #define MAX_PASSWD_TRIES 3
162 static char initialized = 0;
164 static inline void textdomain_init(void);
165 static inline void textdomain_init(void)
168 bindtextdomain(MODULE_NAME, LOCALEDIR);
176 /* some syslogging */
178 #ifdef HAVE_PAM_VSYSLOG
179 static void _pam_log_int(const pam_handle_t *pamh,
184 pam_vsyslog(pamh, err, format, args);
187 static void _pam_log_int(const pam_handle_t *pamh,
192 char *format2 = NULL;
196 pam_get_item(pamh, PAM_SERVICE, (const void **) &service);
198 ret = asprintf(&format2, "%s(%s): %s", MODULE_NAME, service, format);
200 /* what else todo ? */
201 vsyslog(err, format, args);
205 vsyslog(err, format2, args);
208 #endif /* HAVE_PAM_VSYSLOG */
210 static bool _pam_log_is_silent(int ctrl)
212 return on(ctrl, WINBIND_SILENT);
215 static void _pam_log(struct pwb_context *r, int err, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
216 static void _pam_log(struct pwb_context *r, int err, const char *format, ...)
220 if (_pam_log_is_silent(r->ctrl)) {
224 va_start(args, format);
225 _pam_log_int(r->pamh, err, format, args);
228 static void __pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
229 static void __pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
233 if (_pam_log_is_silent(ctrl)) {
237 va_start(args, format);
238 _pam_log_int(pamh, err, format, args);
242 static bool _pam_log_is_debug_enabled(int ctrl)
248 if (_pam_log_is_silent(ctrl)) {
252 if (!(ctrl & WINBIND_DEBUG_ARG)) {
259 static bool _pam_log_is_debug_state_enabled(int ctrl)
261 if (!(ctrl & WINBIND_DEBUG_STATE)) {
265 return _pam_log_is_debug_enabled(ctrl);
268 static void _pam_log_debug(struct pwb_context *r, int err, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
269 static void _pam_log_debug(struct pwb_context *r, int err, const char *format, ...)
273 if (!r || !_pam_log_is_debug_enabled(r->ctrl)) {
277 va_start(args, format);
278 _pam_log_int(r->pamh, err, format, args);
281 static void __pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
282 static void __pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
286 if (!_pam_log_is_debug_enabled(ctrl)) {
290 va_start(args, format);
291 _pam_log_int(pamh, err, format, args);
295 static void _pam_log_state_datum(struct pwb_context *ctx,
300 const void *data = NULL;
301 if (item_type != 0) {
302 pam_get_item(ctx->pamh, item_type, &data);
304 pam_get_data(ctx->pamh, key, &data);
307 const char *type = (item_type != 0) ? "ITEM" : "DATA";
308 if (is_string != 0) {
309 _pam_log_debug(ctx, LOG_DEBUG,
310 "[pamh: %p] STATE: %s(%s) = \"%s\" (%p)",
311 ctx->pamh, type, key, (const char *)data,
314 _pam_log_debug(ctx, LOG_DEBUG,
315 "[pamh: %p] STATE: %s(%s) = %p",
316 ctx->pamh, type, key, data);
321 #define _PAM_LOG_STATE_DATA_POINTER(ctx, module_data_name) \
322 _pam_log_state_datum(ctx, 0, module_data_name, 0)
324 #define _PAM_LOG_STATE_DATA_STRING(ctx, module_data_name) \
325 _pam_log_state_datum(ctx, 0, module_data_name, 1)
327 #define _PAM_LOG_STATE_ITEM_POINTER(ctx, item_type) \
328 _pam_log_state_datum(ctx, item_type, #item_type, 0)
330 #define _PAM_LOG_STATE_ITEM_STRING(ctx, item_type) \
331 _pam_log_state_datum(ctx, item_type, #item_type, 1)
333 #ifdef DEBUG_PASSWORD
334 #define _LOG_PASSWORD_AS_STRING 1
336 #define _LOG_PASSWORD_AS_STRING 0
339 #define _PAM_LOG_STATE_ITEM_PASSWORD(ctx, item_type) \
340 _pam_log_state_datum(ctx, item_type, #item_type, \
341 _LOG_PASSWORD_AS_STRING)
343 * wrapper to preserve old behaviour of iniparser which ignored
344 * key values that had no value assigned like
346 * for a key like above newer iniparser will return a zero-length
347 * string, previously iniparser would return NULL
349 * JRA: For compatibility, tiniparser behaves like iniparser.
351 static const char *tiniparser_getstring_nonempty(struct tiniparser_dictionary *d,
355 const char *ret = tiniparser_getstring(d, key, def);
356 if (ret && strlen(ret) == 0) {
362 static void _pam_log_state(struct pwb_context *ctx)
364 if (!ctx || !_pam_log_is_debug_state_enabled(ctx->ctrl)) {
368 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_SERVICE);
369 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_USER);
370 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_TTY);
371 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_RHOST);
372 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_RUSER);
373 _PAM_LOG_STATE_ITEM_PASSWORD(ctx, PAM_OLDAUTHTOK);
374 _PAM_LOG_STATE_ITEM_PASSWORD(ctx, PAM_AUTHTOK);
375 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_USER_PROMPT);
376 _PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_CONV);
377 #ifdef PAM_FAIL_DELAY
378 _PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_FAIL_DELAY);
380 #ifdef PAM_REPOSITORY
381 _PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_REPOSITORY);
384 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_HOMEDIR);
385 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_LOGONSCRIPT);
386 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_LOGONSERVER);
387 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_PROFILEPATH);
388 _PAM_LOG_STATE_DATA_STRING(ctx,
389 PAM_WINBIND_NEW_AUTHTOK_REQD);
390 /* Use atoi to get PAM result code */
391 _PAM_LOG_STATE_DATA_STRING(ctx,
392 PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH);
393 _PAM_LOG_STATE_DATA_POINTER(ctx, PAM_WINBIND_PWD_LAST_SET);
396 static int _pam_parse(const pam_handle_t *pamh,
400 enum pam_winbind_request_type type,
401 struct tiniparser_dictionary **result_d)
404 const char *config_file = NULL;
407 struct tiniparser_dictionary *d = NULL;
409 if (flags & PAM_SILENT) {
410 ctrl |= WINBIND_SILENT;
413 for (i=argc,v=argv; i-- > 0; ++v) {
414 if (!strncasecmp(*v, "config", strlen("config"))) {
415 ctrl |= WINBIND_CONFIG_FILE;
421 if (config_file == NULL) {
422 config_file = PAM_WINBIND_CONFIG_FILE;
425 d = tiniparser_load(config_file);
427 goto config_from_pam;
430 if (tiniparser_getboolean(d, "global:debug", false)) {
431 ctrl |= WINBIND_DEBUG_ARG;
434 if (tiniparser_getboolean(d, "global:debug_state", false)) {
435 ctrl |= WINBIND_DEBUG_STATE;
438 if (tiniparser_getboolean(d, "global:cached_login", false)) {
439 ctrl |= WINBIND_CACHED_LOGIN;
442 if (tiniparser_getboolean(d, "global:krb5_auth", false)) {
443 ctrl |= WINBIND_KRB5_AUTH;
446 if (tiniparser_getboolean(d, "global:silent", false)) {
447 ctrl |= WINBIND_SILENT;
450 if (tiniparser_getstring_nonempty(d, "global:krb5_ccache_type", NULL) != NULL) {
451 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
454 if ((tiniparser_getstring_nonempty(d, "global:require-membership-of", NULL)
456 (tiniparser_getstring_nonempty(d, "global:require_membership_of", NULL)
458 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
461 if (tiniparser_getboolean(d, "global:try_first_pass", false)) {
462 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
465 if (tiniparser_getint(d, "global:warn_pwd_expire", 0)) {
466 ctrl |= WINBIND_WARN_PWD_EXPIRE;
469 if (tiniparser_getboolean(d, "global:mkhomedir", false)) {
470 ctrl |= WINBIND_MKHOMEDIR;
474 /* step through arguments */
475 for (i=argc,v=argv; i-- > 0; ++v) {
477 /* generic options */
478 if (!strcmp(*v,"debug"))
479 ctrl |= WINBIND_DEBUG_ARG;
480 else if (!strcasecmp(*v, "debug_state"))
481 ctrl |= WINBIND_DEBUG_STATE;
482 else if (!strcasecmp(*v, "silent"))
483 ctrl |= WINBIND_SILENT;
484 else if (!strcasecmp(*v, "use_authtok"))
485 ctrl |= WINBIND_USE_AUTHTOK_ARG;
486 else if (!strcasecmp(*v, "use_first_pass"))
487 ctrl |= WINBIND_USE_FIRST_PASS_ARG;
488 else if (!strcasecmp(*v, "try_first_pass"))
489 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
490 else if (!strcasecmp(*v, "unknown_ok"))
491 ctrl |= WINBIND_UNKNOWN_OK_ARG;
492 else if ((type == PAM_WINBIND_AUTHENTICATE
493 || type == PAM_WINBIND_SETCRED)
494 && !strncasecmp(*v, "require_membership_of",
495 strlen("require_membership_of")))
496 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
497 else if ((type == PAM_WINBIND_AUTHENTICATE
498 || type == PAM_WINBIND_SETCRED)
499 && !strncasecmp(*v, "require-membership-of",
500 strlen("require-membership-of")))
501 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
502 else if (!strcasecmp(*v, "krb5_auth"))
503 ctrl |= WINBIND_KRB5_AUTH;
504 else if (!strncasecmp(*v, "krb5_ccache_type",
505 strlen("krb5_ccache_type")))
506 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
507 else if (!strcasecmp(*v, "cached_login"))
508 ctrl |= WINBIND_CACHED_LOGIN;
509 else if (!strcasecmp(*v, "mkhomedir"))
510 ctrl |= WINBIND_MKHOMEDIR;
511 else if (!strncasecmp(*v, "warn_pwd_expire",
512 strlen("warn_pwd_expire")))
513 ctrl |= WINBIND_WARN_PWD_EXPIRE;
514 else if (type != PAM_WINBIND_CLEANUP) {
515 __pam_log(pamh, ctrl, LOG_ERR,
516 "pam_parse: unknown option: %s", *v);
526 tiniparser_freedict(d);
533 static int _pam_winbind_free_context(struct pwb_context *ctx)
540 tiniparser_freedict(ctx->dict);
543 wbcCtxFree(ctx->wbc_ctx);
548 static int _pam_winbind_init_context(pam_handle_t *pamh,
552 enum pam_winbind_request_type type,
553 struct pwb_context **ctx_p)
555 struct pwb_context *r = NULL;
562 r = talloc_zero(NULL, struct pwb_context);
567 talloc_set_destructor(r, _pam_winbind_free_context);
573 ctrl_code = _pam_parse(pamh, flags, argc, argv, type, &r->dict);
574 if (ctrl_code == -1) {
576 return PAM_SYSTEM_ERR;
580 r->wbc_ctx = wbcCtxCreate();
581 if (r->wbc_ctx == NULL) {
583 return PAM_SYSTEM_ERR;
591 static void _pam_winbind_cleanup_func(pam_handle_t *pamh,
595 int ctrl = _pam_parse(pamh, 0, 0, NULL, PAM_WINBIND_CLEANUP, NULL);
596 if (_pam_log_is_debug_state_enabled(ctrl)) {
597 __pam_log_debug(pamh, ctrl, LOG_DEBUG,
598 "[pamh: %p] CLEAN: cleaning up PAM data %p "
599 "(error_status = %d)", pamh, data,
606 static const struct ntstatus_errors {
607 const char *ntstatus_string;
608 const char *error_string;
609 } ntstatus_errors[] = {
612 {"NT_STATUS_BACKUP_CONTROLLER",
613 N_("No primary Domain Controller available")},
614 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
615 N_("No domain controllers found")},
616 {"NT_STATUS_NO_LOGON_SERVERS",
617 N_("No logon servers")},
618 {"NT_STATUS_PWD_TOO_SHORT",
619 N_("Password too short")},
620 {"NT_STATUS_PWD_TOO_RECENT",
621 N_("The password of this user is too recent to change")},
622 {"NT_STATUS_PWD_HISTORY_CONFLICT",
623 N_("Password is already in password history")},
624 {"NT_STATUS_PASSWORD_EXPIRED",
625 N_("Your password has expired")},
626 {"NT_STATUS_PASSWORD_MUST_CHANGE",
627 N_("You need to change your password now")},
628 {"NT_STATUS_INVALID_WORKSTATION",
629 N_("You are not allowed to logon from this workstation")},
630 {"NT_STATUS_INVALID_LOGON_HOURS",
631 N_("You are not allowed to logon at this time")},
632 {"NT_STATUS_ACCOUNT_EXPIRED",
633 N_("Your account has expired. "
634 "Please contact your System administrator")}, /* SCNR */
635 {"NT_STATUS_ACCOUNT_DISABLED",
636 N_("Your account is disabled. "
637 "Please contact your System administrator")}, /* SCNR */
638 {"NT_STATUS_ACCOUNT_LOCKED_OUT",
639 N_("Your account has been locked. "
640 "Please contact your System administrator")}, /* SCNR */
641 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
642 N_("Invalid Trust Account")},
643 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
644 N_("Invalid Trust Account")},
645 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
646 N_("Invalid Trust Account")},
647 {"NT_STATUS_ACCESS_DENIED",
648 N_("Access is denied")},
652 static const char *_get_ntstatus_error_string(const char *nt_status_string)
655 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
656 if (!strcasecmp(ntstatus_errors[i].ntstatus_string,
658 return _(ntstatus_errors[i].error_string);
664 /* --- authentication management functions --- */
666 /* Attempt a conversation */
668 static int converse(const pam_handle_t *pamh,
670 const struct pam_message **message,
671 struct pam_response **response)
674 struct pam_conv *conv;
676 retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
677 if (retval == PAM_SUCCESS) {
678 retval = conv->conv(nargs,
679 discard_const_p(const struct pam_message *, message),
680 response, conv->appdata_ptr);
683 return retval; /* propagate error status */
687 static int _make_remark(struct pwb_context *ctx,
691 int retval = PAM_SUCCESS;
693 const struct pam_message *pmsg[1];
694 struct pam_message msg[1];
695 struct pam_response *resp;
697 if (ctx->flags & WINBIND_SILENT) {
702 msg[0].msg = discard_const_p(char, text);
703 msg[0].msg_style = type;
706 retval = converse(ctx->pamh, 1, pmsg, &resp);
709 _pam_drop_reply(resp, 1);
714 static int _make_remark_v(struct pwb_context *ctx,
722 ret = vasprintf(&var, format, args);
724 _pam_log(ctx, LOG_ERR, "memory allocation failure");
728 ret = _make_remark(ctx, type, var);
733 static int _make_remark_format(struct pwb_context *ctx, int type, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
734 static int _make_remark_format(struct pwb_context *ctx, int type, const char *format, ...)
739 va_start(args, format);
740 ret = _make_remark_v(ctx, type, format, args);
745 static int pam_winbind_request_log(struct pwb_context *ctx,
752 /* incorrect password */
753 _pam_log(ctx, LOG_WARNING, "user '%s' denied access "
754 "(incorrect password or invalid membership)", user);
756 case PAM_ACCT_EXPIRED:
757 /* account expired */
758 _pam_log(ctx, LOG_WARNING, "user '%s' account expired",
761 case PAM_AUTHTOK_EXPIRED:
762 /* password expired */
763 _pam_log(ctx, LOG_WARNING, "user '%s' password expired",
766 case PAM_NEW_AUTHTOK_REQD:
767 /* new password required */
768 _pam_log(ctx, LOG_WARNING, "user '%s' new password "
771 case PAM_USER_UNKNOWN:
772 /* the user does not exist */
773 _pam_log_debug(ctx, LOG_NOTICE, "user '%s' not found",
775 if (ctx->ctrl & WINBIND_UNKNOWN_OK_ARG) {
779 case PAM_AUTHTOK_ERR:
780 /* Authentication token manipulation error */
781 _pam_log(ctx, LOG_WARNING, "user `%s' authentication token change failed "
782 "(pwd complexity/history/min_age not met?)", user);
785 /* Otherwise, the authentication looked good */
786 if (strcmp(fn, "wbcLogonUser") == 0) {
787 _pam_log(ctx, LOG_NOTICE,
788 "user '%s' granted access", user);
790 _pam_log(ctx, LOG_NOTICE,
791 "user '%s' OK", user);
795 /* we don't know anything about this return value */
796 _pam_log(ctx, LOG_ERR,
797 "internal module error (retval = %s(%d), user = '%s')",
798 _pam_error_code_str(retval), retval, user);
803 static int wbc_auth_error_to_pam_error(struct pwb_context *ctx,
804 struct wbcAuthErrorInfo *e,
806 const char *username,
809 int ret = PAM_AUTH_ERR;
811 if (WBC_ERROR_IS_OK(status)) {
812 _pam_log_debug(ctx, LOG_DEBUG, "request %s succeeded",
815 return pam_winbind_request_log(ctx, ret, username, fn);
819 if (e->pam_error != PAM_SUCCESS) {
820 _pam_log(ctx, LOG_ERR,
821 "request %s failed: %s, "
822 "PAM error: %s (%d), NTSTATUS: %s, "
823 "Error message was: %s",
825 wbcErrorString(status),
826 _pam_error_code_str(e->pam_error),
831 return pam_winbind_request_log(ctx, ret, username, fn);
834 _pam_log(ctx, LOG_ERR, "request %s failed, but PAM error 0!", fn);
836 ret = PAM_SERVICE_ERR;
837 return pam_winbind_request_log(ctx, ret, username, fn);
840 ret = wbc_error_to_pam_error(status);
841 return pam_winbind_request_log(ctx, ret, username, fn);
844 #if defined(HAVE_PAM_RADIO_TYPE)
845 static bool _pam_winbind_change_pwd(struct pwb_context *ctx)
847 struct pam_message msg;
848 const struct pam_message *pmsg;
849 struct pam_response *resp = NULL;
853 msg.msg_style = PAM_RADIO_TYPE;
854 msg.msg = _("Do you want to change your password now?");
855 ret = converse(ctx->pamh, 1, &pmsg, &resp);
857 if (ret == PAM_SUCCESS) {
858 _pam_log(ctx, LOG_CRIT, "pam_winbind: system error!\n");
862 if (ret != PAM_SUCCESS) {
865 _pam_log(ctx, LOG_CRIT, "Received [%s] reply from application.\n", resp->resp);
867 if ((resp->resp != NULL) && (strcasecmp(resp->resp, "yes") == 0)) {
871 _pam_drop_reply(resp, 1);
875 static bool _pam_winbind_change_pwd(struct pwb_context *ctx)
882 * send a password expiry message if required
884 * @param ctx PAM winbind context.
885 * @param next_change expected (calculated) next expiry date.
886 * @param already_expired pointer to a boolean to indicate if the password is
889 * @return boolean Returns true if message has been sent, false if not.
892 static bool _pam_send_password_expiry_message(struct pwb_context *ctx,
896 bool *already_expired,
900 struct tm tm_now, tm_next_change;
904 if (already_expired) {
905 *already_expired = false;
912 if (next_change <= now) {
913 PAM_WB_REMARK_DIRECT(ctx, "NT_STATUS_PASSWORD_EXPIRED");
914 if (already_expired) {
915 *already_expired = true;
920 if ((next_change < 0) ||
921 (next_change > now + warn_pwd_expire * SECONDS_PER_DAY)) {
925 if ((localtime_r(&now, &tm_now) == NULL) ||
926 (localtime_r(&next_change, &tm_next_change) == NULL)) {
930 days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) -
931 (tm_now.tm_yday+tm_now.tm_year*365);
934 ret = _make_remark(ctx, PAM_TEXT_INFO,
935 _("Your password expires today.\n"));
938 * If change_pwd and already_expired is null.
939 * We are just sending a notification message.
940 * We don't expect any response in this case.
943 if (!change_pwd && !already_expired) {
948 * successfully sent the warning message.
949 * Give the user a chance to change pwd.
951 if (ret == PAM_SUCCESS) {
953 retval = _pam_winbind_change_pwd(ctx);
962 if (days > 0 && days < warn_pwd_expire) {
964 ret = _make_remark_format(ctx, PAM_TEXT_INFO,
965 _("Your password will expire in %d %s.\n"),
966 days, (days > 1) ? _("days"):_("day"));
968 * If change_pwd and already_expired is null.
969 * We are just sending a notification message.
970 * We don't expect any response in this case.
973 if (!change_pwd && !already_expired) {
978 * successfully sent the warning message.
979 * Give the user a chance to change pwd.
981 if (ret == PAM_SUCCESS) {
983 retval = _pam_winbind_change_pwd(ctx);
996 * Send a warning if the password expires in the near future
998 * @param ctx PAM winbind context.
999 * @param response The full authentication response structure.
1000 * @param already_expired boolean, is the pwd already expired?
1005 static void _pam_warn_password_expiry(struct pwb_context *ctx,
1006 const struct wbcAuthUserInfo *info,
1007 int warn_pwd_expire,
1008 bool *already_expired,
1011 time_t now = time(NULL);
1012 time_t next_change = 0;
1018 if (already_expired) {
1019 *already_expired = false;
1023 *change_pwd = false;
1026 /* accounts with WBC_ACB_PWNOEXP set never receive a warning */
1027 if (info->acct_flags & WBC_ACB_PWNOEXP) {
1031 /* no point in sending a warning if this is a grace logon */
1032 if (PAM_WB_GRACE_LOGON(info->user_flags)) {
1036 /* check if the info3 must change timestamp has been set */
1037 next_change = info->pass_must_change_time;
1039 if (_pam_send_password_expiry_message(ctx, next_change, now,
1046 /* no warning sent */
1049 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
1052 * Append a string, making sure not to overflow and to always return a
1053 * NULL-terminated string.
1055 * @param dest Destination string buffer (must already be NULL-terminated).
1056 * @param src Source string buffer.
1057 * @param dest_buffer_size Size of dest buffer in bytes.
1059 * @return false if dest buffer is not big enough (no bytes copied), true on
1063 static bool safe_append_string(char *dest,
1065 int dest_buffer_size)
1068 len = strlcat(dest, src, dest_buffer_size);
1069 return (len < dest_buffer_size);
1073 * Convert a names into a SID string, appending it to a buffer.
1075 * @param ctx PAM winbind context.
1076 * @param user User in PAM request.
1077 * @param name Name to convert.
1078 * @param sid_list_buffer Where to append the string sid.
1079 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1081 * @return false on failure, true on success.
1083 static bool winbind_name_to_sid_string(struct pwb_context *ctx,
1086 char *sid_list_buffer,
1087 int sid_list_buffer_size)
1089 char sid_string[WBC_SID_STRING_BUFLEN];
1092 if (IS_SID_STRING(name)) {
1093 strlcpy(sid_string, name, sizeof(sid_string));
1096 struct wbcDomainSid sid;
1097 enum wbcSidType type;
1099 _pam_log_debug(ctx, LOG_DEBUG,
1100 "no sid given, looking up: %s\n", name);
1102 wbc_status = wbcCtxLookupName(ctx->wbc_ctx,
1107 if (!WBC_ERROR_IS_OK(wbc_status)) {
1108 _pam_log(ctx, LOG_INFO,
1109 "could not lookup name: %s\n", name);
1113 wbcSidToStringBuf(&sid, sid_string, sizeof(sid_string));
1116 if (!safe_append_string(sid_list_buffer, sid_string,
1117 sid_list_buffer_size)) {
1124 * Convert a list of names into a list of sids.
1126 * @param ctx PAM winbind context.
1127 * @param user User in PAM request.
1128 * @param name_list List of names or string sids, separated by commas.
1129 * @param sid_list_buffer Where to put the list of string sids.
1130 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1132 * @return false on failure, true on success.
1134 static bool winbind_name_list_to_sid_string_list(struct pwb_context *ctx,
1136 const char *name_list,
1137 char *sid_list_buffer,
1138 int sid_list_buffer_size)
1140 bool result = false;
1141 char *current_name = NULL;
1142 const char *search_location;
1146 if (sid_list_buffer_size > 0) {
1147 sid_list_buffer[0] = 0;
1150 search_location = name_list;
1151 while ((comma = strchr(search_location, ',')) != NULL) {
1152 current_name = strndup(search_location,
1153 comma - search_location);
1154 if (NULL == current_name) {
1158 if (!winbind_name_to_sid_string(ctx, user,
1161 sid_list_buffer_size)) {
1163 * If one group name failed, we must not fail
1164 * the authentication totally, continue with
1165 * the following group names. If user belongs to
1166 * one of the valid groups, we must allow it
1170 _pam_log(ctx, LOG_INFO, "cannot convert group %s to sid, "
1171 "check if group %s is valid group.", current_name,
1173 _make_remark_format(ctx, PAM_TEXT_INFO, _("Cannot convert group %s "
1174 "to sid, please contact your administrator to see "
1175 "if group %s is valid."), current_name, current_name);
1176 SAFE_FREE(current_name);
1177 search_location = comma + 1;
1181 SAFE_FREE(current_name);
1183 if (!safe_append_string(sid_list_buffer, ",",
1184 sid_list_buffer_size)) {
1188 search_location = comma + 1;
1191 if (!winbind_name_to_sid_string(ctx, user, search_location,
1193 sid_list_buffer_size)) {
1194 _pam_log(ctx, LOG_INFO, "cannot convert group %s to sid, "
1195 "check if group %s is valid group.", search_location,
1197 _make_remark_format(ctx, PAM_TEXT_INFO, _("Cannot convert group %s "
1198 "to sid, please contact your administrator to see "
1199 "if group %s is valid."), search_location, search_location);
1201 /* If no valid groups were converted we should fail outright */
1202 if (name_list != NULL && strlen(sid_list_buffer) == 0) {
1207 * The lookup of the last name failed..
1208 * It results in require_member_of_sid ends with ','
1209 * It is malformated parameter here, overwrite the last ','.
1211 len = strlen(sid_list_buffer);
1212 if ((len != 0) && (sid_list_buffer[len - 1] == ',')) {
1213 sid_list_buffer[len - 1] = '\0';
1220 SAFE_FREE(current_name);
1225 * put krb5ccname variable into environment
1227 * @param ctx PAM winbind context.
1228 * @param krb5ccname env variable retrieved from winbindd.
1233 static void _pam_setup_krb5_env(struct pwb_context *ctx,
1234 struct wbcLogonUserInfo *info)
1239 const char *krb5ccname = NULL;
1241 if (off(ctx->ctrl, WINBIND_KRB5_AUTH)) {
1249 for (i=0; i < info->num_blobs; i++) {
1250 if (strcasecmp(info->blobs[i].name, "krb5ccname") == 0) {
1251 krb5ccname = (const char *)info->blobs[i].blob.data;
1256 if (!krb5ccname || (strlen(krb5ccname) == 0)) {
1260 _pam_log_debug(ctx, LOG_DEBUG,
1261 "request returned KRB5CCNAME: %s", krb5ccname);
1263 if (asprintf(&var, "KRB5CCNAME=%s", krb5ccname) == -1) {
1267 ret = pam_putenv(ctx->pamh, var);
1268 if (ret != PAM_SUCCESS) {
1269 _pam_log(ctx, LOG_ERR,
1270 "failed to set KRB5CCNAME to %s: %s",
1271 var, pam_strerror(ctx->pamh, ret));
1277 * Copy unix username if available (further processed in PAM).
1279 * @param ctx PAM winbind context
1280 * @param user_ret A pointer that holds a pointer to a string
1281 * @param unix_username A username
1286 static void _pam_setup_unix_username(struct pwb_context *ctx,
1288 struct wbcLogonUserInfo *info)
1290 const char *unix_username = NULL;
1293 if (!user_ret || !info) {
1297 for (i=0; i < info->num_blobs; i++) {
1298 if (strcasecmp(info->blobs[i].name, "unix_username") == 0) {
1299 unix_username = (const char *)info->blobs[i].blob.data;
1304 if (!unix_username || !unix_username[0]) {
1308 *user_ret = strdup(unix_username);
1312 * Set string into the PAM stack.
1314 * @param ctx PAM winbind context.
1315 * @param data_name Key name for pam_set_data.
1316 * @param value String value.
1321 static void _pam_set_data_string(struct pwb_context *ctx,
1322 const char *data_name,
1327 if (!data_name || !value || (strlen(data_name) == 0) ||
1328 (strlen(value) == 0)) {
1332 ret = pam_set_data(ctx->pamh, data_name, talloc_strdup(NULL, value),
1333 _pam_winbind_cleanup_func);
1334 if (ret != PAM_SUCCESS) {
1335 _pam_log_debug(ctx, LOG_DEBUG,
1336 "Could not set data %s: %s\n",
1337 data_name, pam_strerror(ctx->pamh, ret));
1342 * Set info3 strings into the PAM stack.
1344 * @param ctx PAM winbind context.
1345 * @param data_name Key name for pam_set_data.
1346 * @param value String value.
1351 static void _pam_set_data_info3(struct pwb_context *ctx,
1352 const struct wbcAuthUserInfo *info)
1354 _pam_set_data_string(ctx, PAM_WINBIND_HOMEDIR,
1355 info->home_directory);
1356 _pam_set_data_string(ctx, PAM_WINBIND_LOGONSCRIPT,
1357 info->logon_script);
1358 _pam_set_data_string(ctx, PAM_WINBIND_LOGONSERVER,
1359 info->logon_server);
1360 _pam_set_data_string(ctx, PAM_WINBIND_PROFILEPATH,
1361 info->profile_path);
1365 * Free info3 strings in the PAM stack.
1367 * @param pamh PAM handle
1372 static void _pam_free_data_info3(pam_handle_t *pamh)
1374 pam_set_data(pamh, PAM_WINBIND_HOMEDIR, NULL, NULL);
1375 pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT, NULL, NULL);
1376 pam_set_data(pamh, PAM_WINBIND_LOGONSERVER, NULL, NULL);
1377 pam_set_data(pamh, PAM_WINBIND_PROFILEPATH, NULL, NULL);
1381 * Send PAM_ERROR_MSG for cached or grace logons.
1383 * @param ctx PAM winbind context.
1384 * @param username User in PAM request.
1385 * @param info3_user_flgs Info3 flags containing logon type bits.
1390 static void _pam_warn_logon_type(struct pwb_context *ctx,
1391 const char *username,
1392 uint32_t info3_user_flgs)
1394 /* inform about logon type */
1395 if (PAM_WB_GRACE_LOGON(info3_user_flgs)) {
1397 _make_remark(ctx, PAM_ERROR_MSG,
1399 "Please change your password as soon you're "
1401 _pam_log_debug(ctx, LOG_DEBUG,
1402 "User %s logged on using grace logon\n",
1405 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs)) {
1407 _make_remark(ctx, PAM_ERROR_MSG,
1408 _("Domain Controller unreachable, "
1409 "using cached credentials instead. "
1410 "Network resources may be unavailable"));
1411 _pam_log_debug(ctx, LOG_DEBUG,
1412 "User %s logged on using cached credentials\n",
1418 * Send PAM_ERROR_MSG for krb5 errors.
1420 * @param ctx PAM winbind context.
1421 * @param username User in PAM request.
1422 * @param info3_user_flgs Info3 flags containing logon type bits.
1427 static void _pam_warn_krb5_failure(struct pwb_context *ctx,
1428 const char *username,
1429 uint32_t info3_user_flgs)
1431 if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs)) {
1432 _make_remark(ctx, PAM_ERROR_MSG,
1433 _("Failed to establish your Kerberos Ticket cache "
1434 "due time differences\n"
1435 "with the domain controller. "
1436 "Please verify the system time.\n"));
1437 _pam_log_debug(ctx, LOG_DEBUG,
1438 "User %s: Clock skew when getting Krb5 TGT\n",
1443 static bool _pam_check_remark_auth_err(struct pwb_context *ctx,
1444 const struct wbcAuthErrorInfo *e,
1445 const char *nt_status_string,
1448 const char *ntstatus = NULL;
1449 const char *error_string = NULL;
1451 if (!e || !pam_err) {
1455 ntstatus = e->nt_string;
1460 if (strcasecmp(ntstatus, nt_status_string) == 0) {
1462 error_string = _get_ntstatus_error_string(nt_status_string);
1464 _make_remark(ctx, PAM_ERROR_MSG, error_string);
1465 *pam_err = e->pam_error;
1469 if (e->display_string) {
1470 _make_remark(ctx, PAM_ERROR_MSG, _(e->display_string));
1471 *pam_err = e->pam_error;
1475 _make_remark(ctx, PAM_ERROR_MSG, nt_status_string);
1476 *pam_err = e->pam_error;
1485 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
1487 * @param i The wbcUserPasswordPolicyInfo struct.
1489 * @return string (caller needs to talloc_free).
1492 static char *_pam_compose_pwd_restriction_string(struct pwb_context *ctx,
1493 struct wbcUserPasswordPolicyInfo *i)
1501 str = talloc_asprintf(ctx, _("Your password "));
1506 if (i->min_length_password > 0) {
1507 str = talloc_asprintf_append(str,
1508 _("must be at least %d characters; "),
1509 i->min_length_password);
1515 if (i->password_history > 0) {
1516 str = talloc_asprintf_append(str,
1517 _("cannot repeat any of your previous %d "
1519 i->password_history);
1525 if (i->password_properties & WBC_DOMAIN_PASSWORD_COMPLEX) {
1526 str = talloc_asprintf_append(str,
1527 _("must contain capitals, numerals "
1529 "and cannot contain your account "
1536 str = talloc_asprintf_append(str,
1537 _("Please type a different password. "
1538 "Type a password which meets these requirements in "
1539 "both text boxes."));
1551 static int _pam_create_homedir(struct pwb_context *ctx,
1552 const char *dirname,
1557 if (stat(dirname, &sbuf) == 0) {
1561 if (mkdir(dirname, mode) != 0) {
1563 _make_remark_format(ctx, PAM_TEXT_INFO,
1564 _("Creating directory: %s failed: %s"),
1565 dirname, strerror(errno));
1566 _pam_log(ctx, LOG_ERR, "could not create dir: %s (%s)",
1567 dirname, strerror(errno));
1568 return PAM_PERM_DENIED;
1574 static int _pam_chown_homedir(struct pwb_context *ctx,
1575 const char *dirname,
1579 if (chown(dirname, uid, gid) != 0) {
1580 _pam_log(ctx, LOG_ERR, "failed to chown user homedir: %s (%s)",
1581 dirname, strerror(errno));
1582 return PAM_PERM_DENIED;
1588 static int _pam_mkhomedir(struct pwb_context *ctx)
1590 struct passwd *pwd = NULL;
1592 char *create_dir = NULL;
1593 char *user_dir = NULL;
1595 const char *username;
1597 char *safe_ptr = NULL;
1600 /* Get the username */
1601 ret = pam_get_user(ctx->pamh, &username, NULL);
1602 if ((ret != PAM_SUCCESS) || (!username)) {
1603 _pam_log_debug(ctx, LOG_DEBUG, "can not get the username");
1604 return PAM_SERVICE_ERR;
1607 pwd = getpwnam(username);
1609 _pam_log_debug(ctx, LOG_DEBUG, "can not get the username");
1610 return PAM_USER_UNKNOWN;
1612 _pam_log_debug(ctx, LOG_DEBUG, "homedir is: %s", pwd->pw_dir);
1614 ret = _pam_create_homedir(ctx, pwd->pw_dir, 0700);
1615 if (ret == PAM_SUCCESS) {
1616 ret = _pam_chown_homedir(ctx, pwd->pw_dir,
1621 if (ret == PAM_SUCCESS) {
1625 /* maybe we need to create parent dirs */
1626 create_dir = talloc_strdup(ctx, "/");
1631 /* find final directory */
1632 user_dir = strrchr(pwd->pw_dir, '/');
1638 _pam_log(ctx, LOG_DEBUG, "final directory: %s", user_dir);
1642 while ((token = strtok_r(p, "/", &safe_ptr)) != NULL) {
1648 _pam_log_debug(ctx, LOG_DEBUG, "token is %s", token);
1650 create_dir = talloc_asprintf_append(create_dir, "%s/", token);
1654 _pam_log_debug(ctx, LOG_DEBUG, "current_dir is %s", create_dir);
1656 if (strcmp(token, user_dir) == 0) {
1657 _pam_log_debug(ctx, LOG_DEBUG, "assuming last directory: %s", token);
1661 ret = _pam_create_homedir(ctx, create_dir, mode);
1662 if (ret != PAM_SUCCESS) {
1667 return _pam_chown_homedir(ctx, create_dir,
1672 /* talk to winbindd */
1673 static int winbind_auth_request(struct pwb_context *ctx,
1678 const int warn_pwd_expire,
1679 struct wbcAuthErrorInfo **p_error,
1680 struct wbcLogonUserInfo **p_info,
1681 time_t *pwd_last_set,
1685 struct wbcLogonUserParams logon;
1686 char membership_of[1024];
1687 uid_t user_uid = -1;
1688 uint32_t flags = WBFLAG_PAM_INFO3_TEXT;
1689 struct wbcLogonUserInfo *info = NULL;
1690 struct wbcAuthUserInfo *user_info = NULL;
1691 struct wbcAuthErrorInfo *error = NULL;
1692 int ret = PAM_AUTH_ERR;
1694 const char *codes[] = {
1695 "NT_STATUS_PASSWORD_EXPIRED",
1696 "NT_STATUS_PASSWORD_MUST_CHANGE",
1697 "NT_STATUS_INVALID_WORKSTATION",
1698 "NT_STATUS_INVALID_LOGON_HOURS",
1699 "NT_STATUS_ACCOUNT_EXPIRED",
1700 "NT_STATUS_ACCOUNT_DISABLED",
1701 "NT_STATUS_ACCOUNT_LOCKED_OUT",
1702 "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
1703 "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
1704 "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
1705 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1706 "NT_STATUS_NO_LOGON_SERVERS",
1707 "NT_STATUS_WRONG_PASSWORD",
1708 "NT_STATUS_ACCESS_DENIED"
1715 /* Krb5 auth always has to go against the KDC of the user's realm */
1717 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1718 flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
1721 if (ctx->ctrl & (WINBIND_KRB5_AUTH|WINBIND_CACHED_LOGIN)) {
1722 struct passwd *pwd = NULL;
1724 pwd = getpwnam(user);
1726 return PAM_USER_UNKNOWN;
1728 user_uid = pwd->pw_uid;
1731 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1733 _pam_log_debug(ctx, LOG_DEBUG,
1734 "enabling krb5 login flag\n");
1736 flags |= WBFLAG_PAM_KRB5 |
1737 WBFLAG_PAM_FALLBACK_AFTER_KRB5;
1740 if (ctx->ctrl & WINBIND_CACHED_LOGIN) {
1741 _pam_log_debug(ctx, LOG_DEBUG,
1742 "enabling cached login flag\n");
1743 flags |= WBFLAG_PAM_CACHED_LOGIN;
1748 flags |= WBFLAG_PAM_UNIX_NAME;
1751 if (cctype != NULL) {
1752 _pam_log_debug(ctx, LOG_DEBUG,
1753 "enabling request for a %s krb5 ccache\n",
1757 if (member != NULL) {
1759 ZERO_STRUCT(membership_of);
1761 if (!winbind_name_list_to_sid_string_list(ctx, user, member,
1763 sizeof(membership_of))) {
1764 _pam_log_debug(ctx, LOG_ERR,
1765 "failed to serialize membership of sid "
1766 "\"%s\"\n", member);
1767 return PAM_AUTH_ERR;
1773 logon.username = user;
1774 logon.password = pass;
1777 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1781 discard_const_p(uint8_t, cctype),
1783 if (!WBC_ERROR_IS_OK(wbc_status)) {
1788 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1794 if (!WBC_ERROR_IS_OK(wbc_status)) {
1798 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1802 (uint8_t *)&user_uid,
1804 if (!WBC_ERROR_IS_OK(wbc_status)) {
1809 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1813 (uint8_t *)membership_of,
1814 sizeof(membership_of));
1815 if (!WBC_ERROR_IS_OK(wbc_status)) {
1820 wbc_status = wbcCtxLogonUser(ctx->wbc_ctx,
1825 ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
1826 user, "wbcLogonUser");
1827 wbcFreeMemory(logon.blobs);
1830 if (info && info->info) {
1831 user_info = info->info;
1834 if (pwd_last_set && user_info) {
1835 *pwd_last_set = user_info->pass_last_set_time;
1838 if (p_info && info) {
1842 if (p_error && error) {
1843 /* We want to process the error in the caller. */
1848 for (i=0; i<ARRAY_SIZE(codes); i++) {
1850 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
1856 if ((ret == PAM_SUCCESS) && user_info && info) {
1858 bool already_expired = false;
1859 bool change_pwd = false;
1861 /* warn a user if the password is about to expire soon */
1862 _pam_warn_password_expiry(ctx, user_info,
1867 if (already_expired == true) {
1869 SMB_TIME_T last_set = user_info->pass_last_set_time;
1870 SMB_TIME_T must_set = user_info->pass_must_change_time;
1872 _pam_log_debug(ctx, LOG_DEBUG,
1873 "Password has expired "
1874 "(Password was last set: %lld, "
1875 "it must be changed here "
1876 "%lld (now it's: %ld))\n",
1877 (long long int)last_set,
1878 (long long int)must_set,
1881 return PAM_AUTHTOK_EXPIRED;
1885 ret = PAM_NEW_AUTHTOK_REQD;
1889 /* inform about logon type */
1890 _pam_warn_logon_type(ctx, user, user_info->user_flags);
1892 /* inform about krb5 failures */
1893 _pam_warn_krb5_failure(ctx, user, user_info->user_flags);
1895 /* set some info3 info for other modules in the stack */
1896 _pam_set_data_info3(ctx, user_info);
1898 /* put krb5ccname into env */
1899 _pam_setup_krb5_env(ctx, info);
1901 /* If winbindd returned a username, return the pointer to it
1903 _pam_setup_unix_username(ctx, user_ret, info);
1907 wbcFreeMemory(logon.blobs);
1908 if (info && info->blobs && !p_info) {
1909 wbcFreeMemory(info->blobs);
1911 if (error && !p_error) {
1912 wbcFreeMemory(error);
1914 if (info && !p_info) {
1915 wbcFreeMemory(info);
1921 /* talk to winbindd */
1922 static int winbind_chauthtok_request(struct pwb_context *ctx,
1924 const char *oldpass,
1925 const char *newpass,
1926 time_t pwd_last_set)
1929 struct wbcChangePasswordParams params;
1930 struct wbcAuthErrorInfo *error = NULL;
1931 struct wbcUserPasswordPolicyInfo *policy = NULL;
1932 enum wbcPasswordChangeRejectReason reject_reason = -1;
1936 const char *codes[] = {
1937 "NT_STATUS_BACKUP_CONTROLLER",
1938 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1939 "NT_STATUS_NO_LOGON_SERVERS",
1940 "NT_STATUS_ACCESS_DENIED",
1941 "NT_STATUS_PWD_TOO_SHORT", /* TODO: tell the min pwd length ? */
1942 "NT_STATUS_PWD_TOO_RECENT", /* TODO: tell the minage ? */
1943 "NT_STATUS_PWD_HISTORY_CONFLICT" /* TODO: tell the history length ? */
1945 int ret = PAM_AUTH_ERR;
1947 ZERO_STRUCT(params);
1949 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1950 flags |= WBFLAG_PAM_KRB5 |
1951 WBFLAG_PAM_CONTACT_TRUSTDOM;
1954 if (ctx->ctrl & WINBIND_CACHED_LOGIN) {
1955 flags |= WBFLAG_PAM_CACHED_LOGIN;
1958 params.account_name = user;
1959 params.level = WBC_CHANGE_PASSWORD_LEVEL_PLAIN;
1960 params.old_password.plaintext = oldpass;
1961 params.new_password.plaintext = newpass;
1962 params.flags = flags;
1964 wbc_status = wbcCtxChangeUserPasswordEx(ctx->wbc_ctx,
1969 ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
1970 user, "wbcChangeUserPasswordEx");
1972 if (WBC_ERROR_IS_OK(wbc_status)) {
1973 _pam_log(ctx, LOG_NOTICE,
1974 "user '%s' password changed", user);
1979 wbcFreeMemory(policy);
1983 for (i=0; i<ARRAY_SIZE(codes); i++) {
1985 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
1991 if (!strcasecmp(error->nt_string,
1992 "NT_STATUS_PASSWORD_RESTRICTION")) {
1994 char *pwd_restriction_string = NULL;
1995 SMB_TIME_T min_pwd_age = 0;
1998 min_pwd_age = policy->min_passwordage;
2001 /* FIXME: avoid to send multiple PAM messages after another */
2002 switch ((int)reject_reason) {
2005 case WBC_PWD_CHANGE_NO_ERROR:
2006 if ((min_pwd_age > 0) &&
2007 (pwd_last_set + min_pwd_age > time(NULL))) {
2008 PAM_WB_REMARK_DIRECT(ctx,
2009 "NT_STATUS_PWD_TOO_RECENT");
2012 case WBC_PWD_CHANGE_PASSWORD_TOO_SHORT:
2013 PAM_WB_REMARK_DIRECT(ctx,
2014 "NT_STATUS_PWD_TOO_SHORT");
2016 case WBC_PWD_CHANGE_PWD_IN_HISTORY:
2017 PAM_WB_REMARK_DIRECT(ctx,
2018 "NT_STATUS_PWD_HISTORY_CONFLICT");
2020 case WBC_PWD_CHANGE_NOT_COMPLEX:
2021 _make_remark(ctx, PAM_ERROR_MSG,
2022 _("Password does not meet "
2023 "complexity requirements"));
2026 _pam_log_debug(ctx, LOG_DEBUG,
2027 "unknown password change "
2028 "reject reason: %d",
2033 pwd_restriction_string =
2034 _pam_compose_pwd_restriction_string(ctx, policy);
2035 if (pwd_restriction_string) {
2036 _make_remark(ctx, PAM_ERROR_MSG,
2037 pwd_restriction_string);
2038 TALLOC_FREE(pwd_restriction_string);
2042 wbcFreeMemory(error);
2043 wbcFreeMemory(policy);
2049 * Checks if a user has an account
2052 * 1 = User not found
2056 static int valid_user(struct pwb_context *ctx,
2059 /* check not only if the user is available over NSS calls, also make
2060 * sure it's really a winbind user, this is important when stacking PAM
2061 * modules in the 'account' or 'password' facility. */
2064 struct passwd *pwd = NULL;
2065 struct passwd *wb_pwd = NULL;
2067 pwd = getpwnam(user);
2072 wbc_status = wbcCtxGetpwnam(ctx->wbc_ctx, user, &wb_pwd);
2073 wbcFreeMemory(wb_pwd);
2074 if (!WBC_ERROR_IS_OK(wbc_status)) {
2075 _pam_log(ctx, LOG_DEBUG, "valid_user: wbcGetpwnam gave %s\n",
2076 wbcErrorString(wbc_status));
2079 switch (wbc_status) {
2080 case WBC_ERR_UNKNOWN_USER:
2081 /* match other insane libwbclient return codes */
2082 case WBC_ERR_WINBIND_NOT_AVAILABLE:
2083 case WBC_ERR_DOMAIN_NOT_FOUND:
2085 case WBC_ERR_SUCCESS:
2093 static char *_pam_delete(register char *xx)
2101 * obtain a password from the user
2104 static int _winbind_read_password(struct pwb_context *ctx,
2106 const char *comment,
2107 const char *prompt1,
2108 const char *prompt2,
2116 _pam_log(ctx, LOG_DEBUG, "getting password (0x%08x)", ctrl);
2119 * make sure nothing inappropriate gets returned
2122 *pass = token = NULL;
2125 * which authentication token are we getting?
2128 if (on(WINBIND__OLD_PASSWORD, ctrl)) {
2129 authtok_flag = PAM_OLDAUTHTOK;
2131 authtok_flag = PAM_AUTHTOK;
2135 * should we obtain the password from a PAM item ?
2138 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) ||
2139 on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
2140 retval = pam_get_item(ctx->pamh,
2142 (const void **) &item);
2143 if (retval != PAM_SUCCESS) {
2145 _pam_log(ctx, LOG_ALERT,
2146 "pam_get_item returned error "
2147 "to unix-read-password");
2149 } else if (item != NULL) { /* we have a password! */
2152 _pam_log(ctx, LOG_DEBUG,
2153 "pam_get_item returned a password");
2155 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
2156 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
2157 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
2158 && off(WINBIND__OLD_PASSWORD, ctrl)) {
2159 return PAM_AUTHTOK_RECOVER_ERR;
2163 * getting here implies we will have to get the password from the
2168 struct pam_message msg[3];
2169 const struct pam_message *pmsg[3];
2170 struct pam_response *resp;
2173 /* prepare to converse */
2175 if (comment != NULL && off(ctrl, WINBIND_SILENT)) {
2177 msg[0].msg_style = PAM_TEXT_INFO;
2178 msg[0].msg = discard_const_p(char, comment);
2185 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
2186 msg[i++].msg = discard_const_p(char, prompt1);
2189 if (prompt2 != NULL) {
2191 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
2192 msg[i++].msg = discard_const_p(char, prompt2);
2195 /* so call the conversation expecting i responses */
2197 retval = converse(ctx->pamh, i, pmsg, &resp);
2199 if (retval == PAM_SUCCESS) {
2200 retval = PAM_AUTHTOK_RECOVER_ERR;
2204 if (retval != PAM_SUCCESS) {
2205 _pam_drop_reply(resp, i);
2209 /* interpret the response */
2211 token = x_strdup(resp[i - replies].resp);
2213 _pam_log(ctx, LOG_NOTICE,
2214 "could not recover "
2215 "authentication token");
2216 retval = PAM_AUTHTOK_RECOVER_ERR;
2221 /* verify that password entered correctly */
2222 if (!resp[i - 1].resp ||
2223 strcmp(token, resp[i - 1].resp)) {
2224 _pam_delete(token); /* mistyped */
2225 retval = PAM_AUTHTOK_RECOVER_ERR;
2226 _make_remark(ctx, PAM_ERROR_MSG,
2232 * tidy up the conversation (resp_retcode) is ignored
2233 * -- what is it for anyway? AGM
2235 _pam_drop_reply(resp, i);
2239 if (retval != PAM_SUCCESS) {
2240 _pam_log_debug(ctx, LOG_DEBUG,
2241 "unable to obtain a password");
2244 /* 'token' is the entered password */
2246 /* we store this password as an item */
2248 retval = pam_set_item(ctx->pamh, authtok_flag, token);
2249 _pam_delete(token); /* clean it up */
2250 if (retval != PAM_SUCCESS ||
2251 (retval = pam_get_item(ctx->pamh, authtok_flag, (const void **) &item)) != PAM_SUCCESS) {
2253 _pam_log(ctx, LOG_CRIT, "error manipulating password");
2259 item = NULL; /* break link to password */
2264 static const char *get_conf_item_string(struct pwb_context *ctx,
2269 const char *parm_opt = NULL;
2271 if (!(ctx->ctrl & config_flag)) {
2275 /* let the pam opt take precedence over the pam_winbind.conf option */
2276 for (i=0; i<ctx->argc; i++) {
2278 if ((strncmp(ctx->argv[i], item, strlen(item)) == 0)) {
2281 if ((p = strchr(ctx->argv[i], '=')) == NULL) {
2282 _pam_log(ctx, LOG_INFO,
2283 "no \"=\" delimiter for \"%s\" found\n",
2287 _pam_log_debug(ctx, LOG_INFO,
2288 "PAM config: %s '%s'\n", item, p+1);
2296 key = talloc_asprintf(ctx, "global:%s", item);
2301 parm_opt = tiniparser_getstring_nonempty(ctx->dict, key, NULL);
2304 _pam_log_debug(ctx, LOG_INFO, "CONFIG file: %s '%s'\n",
2311 static int get_config_item_int(struct pwb_context *ctx,
2315 int i, parm_opt = -1;
2317 if (!(ctx->ctrl & config_flag)) {
2321 /* let the pam opt take precedence over the pam_winbind.conf option */
2322 for (i = 0; i < ctx->argc; i++) {
2324 if ((strncmp(ctx->argv[i], item, strlen(item)) == 0)) {
2327 if ((p = strchr(ctx->argv[i], '=')) == NULL) {
2328 _pam_log(ctx, LOG_INFO,
2329 "no \"=\" delimiter for \"%s\" found\n",
2333 parm_opt = atoi(p + 1);
2334 _pam_log_debug(ctx, LOG_INFO,
2335 "PAM config: %s '%d'\n",
2344 key = talloc_asprintf(ctx, "global:%s", item);
2349 parm_opt = tiniparser_getint(ctx->dict, key, -1);
2352 _pam_log_debug(ctx, LOG_INFO,
2353 "CONFIG file: %s '%d'\n",
2360 static const char *get_krb5_cc_type_from_config(struct pwb_context *ctx)
2362 return get_conf_item_string(ctx, "krb5_ccache_type",
2363 WINBIND_KRB5_CCACHE_TYPE);
2366 static const char *get_member_from_config(struct pwb_context *ctx)
2368 const char *ret = NULL;
2369 ret = get_conf_item_string(ctx, "require_membership_of",
2370 WINBIND_REQUIRED_MEMBERSHIP);
2374 return get_conf_item_string(ctx, "require-membership-of",
2375 WINBIND_REQUIRED_MEMBERSHIP);
2378 static int get_warn_pwd_expire_from_config(struct pwb_context *ctx)
2381 ret = get_config_item_int(ctx, "warn_pwd_expire",
2382 WINBIND_WARN_PWD_EXPIRE);
2383 /* no or broken setting */
2385 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES;
2391 * Retrieve the winbind separator.
2393 * @param ctx PAM winbind context.
2395 * @return string separator character. NULL on failure.
2398 static char winbind_get_separator(struct pwb_context *ctx)
2401 static struct wbcInterfaceDetails *details = NULL;
2403 wbc_status = wbcCtxInterfaceDetails(ctx->wbc_ctx, &details);
2404 if (!WBC_ERROR_IS_OK(wbc_status)) {
2405 _pam_log(ctx, LOG_ERR,
2406 "Could not retrieve winbind interface details: %s",
2407 wbcErrorString(wbc_status));
2415 return details->winbind_separator;
2420 * Convert a upn to a name.
2422 * @param ctx PAM winbind context.
2423 * @param upn User UPN to be translated.
2425 * @return converted name. NULL pointer on failure. Caller needs to free.
2428 static char* winbind_upn_to_username(struct pwb_context *ctx,
2432 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
2433 struct wbcDomainSid sid;
2434 enum wbcSidType type;
2435 char *domain = NULL;
2440 /* This cannot work when the winbind separator = @ */
2442 sep = winbind_get_separator(ctx);
2443 if (!sep || sep == '@') {
2447 name = talloc_strdup(ctx, upn);
2452 p = strchr(name, '@');
2460 /* Convert the UPN to a SID */
2462 wbc_status = wbcCtxLookupName(ctx->wbc_ctx, domain, name, &sid, &type);
2463 if (!WBC_ERROR_IS_OK(wbc_status)) {
2467 /* Convert the the SID back to the sAMAccountName */
2469 wbc_status = wbcCtxLookupSid(ctx->wbc_ctx, &sid, &domain, &name, &type);
2470 if (!WBC_ERROR_IS_OK(wbc_status)) {
2474 result = talloc_asprintf(ctx, "%s%c%s", domain, sep, name);
2475 wbcFreeMemory(domain);
2476 wbcFreeMemory(name);
2480 static int _pam_delete_cred(pam_handle_t *pamh, int flags,
2481 int argc, enum pam_winbind_request_type type,
2484 int retval = PAM_SUCCESS;
2485 struct pwb_context *ctx = NULL;
2486 struct wbcLogoffUserParams logoff;
2487 struct wbcAuthErrorInfo *error = NULL;
2489 wbcErr wbc_status = WBC_ERR_SUCCESS;
2491 ZERO_STRUCT(logoff);
2493 retval = _pam_winbind_init_context(pamh, flags, argc, argv, type, &ctx);
2494 if (retval != PAM_SUCCESS) {
2498 _PAM_LOG_FUNCTION_ENTER("_pam_delete_cred", ctx);
2500 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
2502 /* destroy the ccache here */
2504 uint32_t wbc_flags = 0;
2505 const char *ccname = NULL;
2506 struct passwd *pwd = NULL;
2508 retval = pam_get_user(pamh, &user, _("Username: "));
2509 if (retval != PAM_SUCCESS) {
2510 _pam_log(ctx, LOG_ERR,
2511 "could not identify user");
2516 _pam_log(ctx, LOG_ERR,
2517 "username was NULL!");
2518 retval = PAM_USER_UNKNOWN;
2522 _pam_log_debug(ctx, LOG_DEBUG,
2523 "username [%s] obtained", user);
2525 ccname = pam_getenv(pamh, "KRB5CCNAME");
2526 if (ccname == NULL) {
2527 _pam_log_debug(ctx, LOG_DEBUG,
2528 "user has no KRB5CCNAME environment");
2531 pwd = getpwnam(user);
2533 retval = PAM_USER_UNKNOWN;
2537 wbc_flags = WBFLAG_PAM_KRB5 |
2538 WBFLAG_PAM_CONTACT_TRUSTDOM;
2540 logoff.username = user;
2543 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2547 discard_const_p(uint8_t, ccname),
2549 if (!WBC_ERROR_IS_OK(wbc_status)) {
2554 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2558 (uint8_t *)&wbc_flags,
2560 if (!WBC_ERROR_IS_OK(wbc_status)) {
2564 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2568 (uint8_t *)&pwd->pw_uid,
2569 sizeof(pwd->pw_uid));
2570 if (!WBC_ERROR_IS_OK(wbc_status)) {
2574 wbc_status = wbcCtxLogoffUserEx(ctx->wbc_ctx, &logoff, &error);
2575 retval = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
2576 user, "wbcLogoffUser");
2577 wbcFreeMemory(error);
2578 wbcFreeMemory(logoff.blobs);
2579 logoff.blobs = NULL;
2581 if (!WBC_ERROR_IS_OK(wbc_status)) {
2582 _pam_log(ctx, LOG_INFO,
2583 "failed to logoff user %s: %s\n",
2584 user, wbcErrorString(wbc_status));
2590 wbcFreeMemory(logoff.blobs);
2593 if (!WBC_ERROR_IS_OK(wbc_status)) {
2594 retval = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
2595 user, "wbcLogoffUser");
2599 * Delete the krb5 ccname variable from the PAM environment
2600 * if it was set by winbind.
2602 if ((ctx->ctrl & WINBIND_KRB5_AUTH) && pam_getenv(pamh, "KRB5CCNAME")) {
2603 pam_putenv(pamh, "KRB5CCNAME");
2606 _PAM_LOG_FUNCTION_LEAVE("_pam_delete_cred", ctx, retval);
2614 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
2615 int argc, const char **argv)
2617 const char *username;
2618 const char *password;
2619 const char *member = NULL;
2620 const char *cctype = NULL;
2621 int warn_pwd_expire;
2622 int retval = PAM_AUTH_ERR;
2623 char *username_ret = NULL;
2624 char *new_authtok_required = NULL;
2625 char *real_username = NULL;
2626 struct pwb_context *ctx = NULL;
2628 retval = _pam_winbind_init_context(pamh, flags, argc, argv,
2629 PAM_WINBIND_AUTHENTICATE, &ctx);
2630 if (retval != PAM_SUCCESS) {
2634 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", ctx);
2636 /* Get the username */
2637 retval = pam_get_user(pamh, &username, NULL);
2638 if ((retval != PAM_SUCCESS) || (!username)) {
2639 _pam_log_debug(ctx, LOG_DEBUG,
2640 "can not get the username");
2641 retval = PAM_SERVICE_ERR;
2647 /* Decode the user name since AIX does not support logn user
2648 names by default. The name is encoded as _#uid. */
2650 if (username[0] == '_') {
2651 uid_t id = atoi(&username[1]);
2652 struct passwd *pw = NULL;
2654 if ((id!=0) && ((pw = getpwuid(id)) != NULL)) {
2655 real_username = strdup(pw->pw_name);
2660 if (!real_username) {
2661 /* Just making a copy of the username we got from PAM */
2662 if ((real_username = strdup(username)) == NULL) {
2663 _pam_log_debug(ctx, LOG_DEBUG,
2664 "memory allocation failure when copying "
2666 retval = PAM_SERVICE_ERR;
2671 /* Maybe this was a UPN */
2673 if (strchr(real_username, '@') != NULL) {
2674 char *samaccountname = NULL;
2676 samaccountname = winbind_upn_to_username(ctx,
2678 if (samaccountname) {
2679 free(real_username);
2680 real_username = strdup(samaccountname);
2684 retval = _winbind_read_password(ctx, ctx->ctrl, NULL,
2685 _("Password: "), NULL,
2688 if (retval != PAM_SUCCESS) {
2689 _pam_log(ctx, LOG_ERR,
2690 "Could not retrieve user's password");
2691 retval = PAM_AUTHTOK_ERR;
2695 /* Let's not give too much away in the log file */
2697 #ifdef DEBUG_PASSWORD
2698 _pam_log_debug(ctx, LOG_INFO,
2699 "Verify user '%s' with password '%s'",
2700 real_username, password);
2702 _pam_log_debug(ctx, LOG_INFO,
2703 "Verify user '%s'", real_username);
2706 member = get_member_from_config(ctx);
2707 cctype = get_krb5_cc_type_from_config(ctx);
2708 warn_pwd_expire = get_warn_pwd_expire_from_config(ctx);
2710 /* Now use the username to look up password */
2711 retval = winbind_auth_request(ctx, real_username, password,
2712 member, cctype, warn_pwd_expire,
2713 NULL, NULL, NULL, &username_ret);
2715 if (retval == PAM_NEW_AUTHTOK_REQD ||
2716 retval == PAM_AUTHTOK_EXPIRED) {
2718 char *new_authtok_required_during_auth = NULL;
2720 new_authtok_required = talloc_asprintf(NULL, "%d", retval);
2721 if (!new_authtok_required) {
2722 retval = PAM_BUF_ERR;
2726 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2727 new_authtok_required,
2728 _pam_winbind_cleanup_func);
2730 retval = PAM_SUCCESS;
2732 new_authtok_required_during_auth = talloc_asprintf(NULL, "%d", true);
2733 if (!new_authtok_required_during_auth) {
2734 retval = PAM_BUF_ERR;
2738 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2739 new_authtok_required_during_auth,
2740 _pam_winbind_cleanup_func);
2747 pam_set_item (pamh, PAM_USER, username_ret);
2748 _pam_log_debug(ctx, LOG_INFO,
2749 "Returned user was '%s'", username_ret);
2753 if (real_username) {
2754 free(real_username);
2757 if (!new_authtok_required) {
2758 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
2761 if (retval != PAM_SUCCESS) {
2762 _pam_free_data_info3(pamh);
2765 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", ctx, retval);
2773 int pam_sm_setcred(pam_handle_t *pamh, int flags,
2774 int argc, const char **argv)
2776 int ret = PAM_SYSTEM_ERR;
2777 struct pwb_context *ctx = NULL;
2779 ret = _pam_winbind_init_context(pamh, flags, argc, argv,
2780 PAM_WINBIND_SETCRED, &ctx);
2781 if (ret != PAM_SUCCESS) {
2785 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", ctx);
2787 switch (flags & ~PAM_SILENT) {
2789 case PAM_DELETE_CRED:
2790 ret = _pam_delete_cred(pamh, flags, argc,
2791 PAM_WINBIND_SETCRED, argv);
2793 case PAM_REFRESH_CRED:
2794 _pam_log_debug(ctx, LOG_WARNING,
2795 "PAM_REFRESH_CRED not implemented");
2798 case PAM_REINITIALIZE_CRED:
2799 _pam_log_debug(ctx, LOG_WARNING,
2800 "PAM_REINITIALIZE_CRED not implemented");
2803 case PAM_ESTABLISH_CRED:
2804 _pam_log_debug(ctx, LOG_WARNING,
2805 "PAM_ESTABLISH_CRED not implemented");
2809 ret = PAM_SYSTEM_ERR;
2813 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", ctx, ret);
2821 * Account management. We want to verify that the account exists
2822 * before returning PAM_SUCCESS
2825 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
2826 int argc, const char **argv)
2828 const char *username;
2829 int ret = PAM_USER_UNKNOWN;
2830 const char *tmp = NULL;
2831 struct pwb_context *ctx = NULL;
2833 ret = _pam_winbind_init_context(pamh, flags, argc, argv,
2834 PAM_WINBIND_ACCT_MGMT, &ctx);
2835 if (ret != PAM_SUCCESS) {
2839 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", ctx);
2842 /* Get the username */
2843 ret = pam_get_user(pamh, &username, NULL);
2844 if ((ret != PAM_SUCCESS) || (!username)) {
2845 _pam_log_debug(ctx, LOG_DEBUG,
2846 "can not get the username");
2847 ret = PAM_SERVICE_ERR;
2851 /* Verify the username */
2852 ret = valid_user(ctx, username);
2855 /* some sort of system error. The log was already printed */
2856 ret = PAM_SERVICE_ERR;
2859 /* the user does not exist */
2860 _pam_log_debug(ctx, LOG_NOTICE, "user '%s' not found",
2862 if (ctx->ctrl & WINBIND_UNKNOWN_OK_ARG) {
2866 ret = PAM_USER_UNKNOWN;
2869 pam_get_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2870 (const void **)&tmp);
2874 case PAM_AUTHTOK_EXPIRED:
2875 /* fall through, since new token is required in this case */
2876 case PAM_NEW_AUTHTOK_REQD:
2877 _pam_log(ctx, LOG_WARNING,
2878 "pam_sm_acct_mgmt success but %s is set",
2879 PAM_WINBIND_NEW_AUTHTOK_REQD);
2880 _pam_log(ctx, LOG_NOTICE,
2881 "user '%s' needs new password",
2883 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2884 ret = PAM_NEW_AUTHTOK_REQD;
2887 _pam_log(ctx, LOG_WARNING,
2888 "pam_sm_acct_mgmt success");
2889 _pam_log(ctx, LOG_NOTICE,
2890 "user '%s' granted access", username);
2896 /* Otherwise, the authentication looked good */
2897 _pam_log(ctx, LOG_NOTICE,
2898 "user '%s' granted access", username);
2902 /* we don't know anything about this return value */
2903 _pam_log(ctx, LOG_ERR,
2904 "internal module error (ret = %d, user = '%s')",
2906 ret = PAM_SERVICE_ERR;
2910 /* should not be reached */
2915 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", ctx, ret);
2923 int pam_sm_open_session(pam_handle_t *pamh, int flags,
2924 int argc, const char **argv)
2926 int ret = PAM_SUCCESS;
2927 struct pwb_context *ctx = NULL;
2929 ret = _pam_winbind_init_context(pamh, flags, argc, argv,
2930 PAM_WINBIND_OPEN_SESSION, &ctx);
2931 if (ret != PAM_SUCCESS) {
2935 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", ctx);
2937 if (ctx->ctrl & WINBIND_MKHOMEDIR) {
2938 /* check and create homedir */
2939 ret = _pam_mkhomedir(ctx);
2942 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", ctx, ret);
2950 int pam_sm_close_session(pam_handle_t *pamh, int flags,
2951 int argc, const char **argv)
2953 int ret = PAM_SUCCESS;
2954 struct pwb_context *ctx = NULL;
2956 ret = _pam_winbind_init_context(pamh, flags, argc, argv,
2957 PAM_WINBIND_CLOSE_SESSION, &ctx);
2958 if (ret != PAM_SUCCESS) {
2962 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", ctx);
2964 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", ctx, ret);
2972 * evaluate whether we need to re-authenticate with kerberos after a
2975 * @param ctx PAM winbind context.
2976 * @param user The username
2978 * @return boolean Returns true if required, false if not.
2981 static bool _pam_require_krb5_auth_after_chauthtok(struct pwb_context *ctx,
2985 /* Make sure that we only do this if a) the chauthtok got initiated
2986 * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2987 * later password change via the "passwd" command if done by the user
2989 * NB. If we login from gdm or xdm and the password expires,
2990 * we change the password, but there is no memory cache.
2991 * Thus, even for passthrough login, we should do the
2992 * authentication again to update memory cache.
2996 char *new_authtok_reqd_during_auth = NULL;
2997 struct passwd *pwd = NULL;
2999 pam_get_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
3000 (const void **) &new_authtok_reqd_during_auth);
3001 pam_set_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
3004 if (new_authtok_reqd_during_auth) {
3008 pwd = getpwnam(user);
3013 if (getuid() == pwd->pw_uid) {
3022 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
3023 int argc, const char **argv)
3027 bool cached_login = false;
3029 /* <DO NOT free() THESE> */
3031 const char *pass_old;
3032 const char *pass_new;
3033 /* </DO NOT free() THESE> */
3038 char *username_ret = NULL;
3039 struct wbcAuthErrorInfo *error = NULL;
3040 struct pwb_context *ctx = NULL;
3042 ret = _pam_winbind_init_context(pamh, flags, argc, argv,
3043 PAM_WINBIND_CHAUTHTOK, &ctx);
3044 if (ret != PAM_SUCCESS) {
3048 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", ctx);
3050 cached_login = (ctx->ctrl & WINBIND_CACHED_LOGIN);
3052 /* clearing offline bit for auth */
3053 ctx->ctrl &= ~WINBIND_CACHED_LOGIN;
3056 * First get the name of a user
3058 ret = pam_get_user(pamh, &user, _("Username: "));
3059 if (ret != PAM_SUCCESS) {
3060 _pam_log(ctx, LOG_ERR,
3061 "password - could not identify user");
3066 _pam_log(ctx, LOG_ERR, "username was NULL!");
3067 ret = PAM_USER_UNKNOWN;
3071 _pam_log_debug(ctx, LOG_DEBUG, "username [%s] obtained", user);
3073 /* check if this is really a user in winbindd, not only in NSS */
3074 ret = valid_user(ctx, user);
3077 ret = PAM_USER_UNKNOWN;
3080 ret = PAM_SYSTEM_ERR;
3087 * obtain and verify the current password (OLDAUTHTOK) for
3091 if (flags & PAM_PRELIM_CHECK) {
3092 time_t pwdlastset_prelim = 0;
3094 /* instruct user what is happening */
3096 #define greeting _("Changing password for")
3097 Announce = talloc_asprintf(ctx, "%s %s", greeting, user);
3099 _pam_log(ctx, LOG_CRIT,
3100 "password - out of memory");
3106 lctrl = ctx->ctrl | WINBIND__OLD_PASSWORD;
3107 ret = _winbind_read_password(ctx, lctrl,
3109 _("(current) NT password: "),
3111 (const char **) &pass_old);
3112 TALLOC_FREE(Announce);
3113 if (ret != PAM_SUCCESS) {
3114 _pam_log(ctx, LOG_NOTICE,
3115 "password - (old) token not obtained");
3119 /* verify that this is the password for this user */
3121 ret = winbind_auth_request(ctx, user, pass_old,
3124 &pwdlastset_prelim, NULL);
3126 if (ret != PAM_ACCT_EXPIRED &&
3127 ret != PAM_AUTHTOK_EXPIRED &&
3128 ret != PAM_NEW_AUTHTOK_REQD &&
3129 ret != PAM_SUCCESS) {
3134 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET,
3135 (void *)pwdlastset_prelim, NULL);
3137 ret = pam_set_item(pamh, PAM_OLDAUTHTOK,
3138 (const void *) pass_old);
3140 if (ret != PAM_SUCCESS) {
3141 _pam_log(ctx, LOG_CRIT,
3142 "failed to set PAM_OLDAUTHTOK");
3144 } else if (flags & PAM_UPDATE_AUTHTOK) {
3146 time_t pwdlastset_update = 0;
3149 * obtain the proposed password
3153 * get the old token back.
3156 ret = pam_get_item(pamh, PAM_OLDAUTHTOK, (const void **) &pass_old);
3158 if (ret != PAM_SUCCESS) {
3159 _pam_log(ctx, LOG_NOTICE,
3160 "user not authenticated");
3164 lctrl = ctx->ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
3166 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
3167 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
3170 ret = PAM_AUTHTOK_ERR;
3171 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
3173 * use_authtok is to force the use of a previously entered
3174 * password -- needed for pluggable password strength checking
3177 ret = _winbind_read_password(ctx, lctrl,
3179 _("Enter new NT password: "),
3180 _("Retype new NT password: "),
3181 (const char **)&pass_new);
3183 if (ret != PAM_SUCCESS) {
3184 _pam_log_debug(ctx, LOG_ALERT,
3186 "new password not obtained");
3187 pass_old = NULL;/* tidy up */
3192 * At this point we know who the user is and what they
3193 * propose as their new password. Verify that the new
3194 * password is acceptable.
3197 if (pass_new[0] == '\0') {/* "\0" password = NULL */
3203 * By reaching here we have approved the passwords and must now
3204 * rebuild the password database file.
3206 pam_get_data(pamh, PAM_WINBIND_PWD_LAST_SET,
3207 (const void **) &pwdlastset_update);
3210 * if cached creds were enabled, make sure to set the
3211 * WINBIND_CACHED_LOGIN bit here in order to have winbindd
3212 * update the cached creds storage - gd
3215 ctx->ctrl |= WINBIND_CACHED_LOGIN;
3218 ret = winbind_chauthtok_request(ctx, user, pass_old,
3219 pass_new, pwdlastset_update);
3220 if (ret != PAM_SUCCESS) {
3221 pass_old = pass_new = NULL;
3225 if (_pam_require_krb5_auth_after_chauthtok(ctx, user)) {
3227 const char *member = NULL;
3228 const char *cctype = NULL;
3229 int warn_pwd_expire;
3230 struct wbcLogonUserInfo *info = NULL;
3232 member = get_member_from_config(ctx);
3233 cctype = get_krb5_cc_type_from_config(ctx);
3234 warn_pwd_expire = get_warn_pwd_expire_from_config(ctx);
3236 /* Keep WINBIND_CACHED_LOGIN bit for
3237 * authentication after changing the password.
3238 * This will update the cached credentials in case
3239 * that winbindd_dual_pam_chauthtok() fails
3244 ret = winbind_auth_request(ctx, user, pass_new,
3247 NULL, &username_ret);
3248 pass_old = pass_new = NULL;
3250 if (ret == PAM_SUCCESS) {
3252 struct wbcAuthUserInfo *user_info = NULL;
3254 if (info && info->info) {
3255 user_info = info->info;
3258 /* warn a user if the password is about to
3260 _pam_warn_password_expiry(ctx, user_info,
3264 /* set some info3 info for other modules in the
3266 _pam_set_data_info3(ctx, user_info);
3268 /* put krb5ccname into env */
3269 _pam_setup_krb5_env(ctx, info);
3272 pam_set_item(pamh, PAM_USER,
3274 _pam_log_debug(ctx, LOG_INFO,
3275 "Returned user was '%s'",
3282 if (info && info->blobs) {
3283 wbcFreeMemory(info->blobs);
3285 wbcFreeMemory(info);
3290 ret = PAM_SERVICE_ERR;
3295 /* Deal with offline errors. */
3297 const char *codes[] = {
3298 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
3299 "NT_STATUS_NO_LOGON_SERVERS",
3300 "NT_STATUS_ACCESS_DENIED"
3303 for (i=0; i<ARRAY_SIZE(codes); i++) {
3305 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
3311 wbcFreeMemory(error);
3313 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", ctx, ret);
3322 /* static module data */
3324 struct pam_module _pam_winbind_modstruct = {
3326 pam_sm_authenticate,
3329 pam_sm_open_session,
3330 pam_sm_close_session,
3337 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
3338 * Copyright (c) Tim Potter <tpot@samba.org> 2000
3339 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
3340 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2008
3341 * Copyright (c) Jan Rêkorajski 1999.
3342 * Copyright (c) Andrew G. Morgan 1996-8.
3343 * Copyright (c) Alex O. Yuriev, 1996.
3344 * Copyright (c) Cristian Gafton 1996.
3345 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
3347 * Redistribution and use in source and binary forms, with or without
3348 * modification, are permitted provided that the following conditions
3350 * 1. Redistributions of source code must retain the above copyright
3351 * notice, and the entire permission notice in its entirety,
3352 * including the disclaimer of warranties.
3353 * 2. Redistributions in binary form must reproduce the above copyright
3354 * notice, this list of conditions and the following disclaimer in the
3355 * documentation and/or other materials provided with the distribution.
3356 * 3. The name of the author may not be used to endorse or promote
3357 * products derived from this software without specific prior
3358 * written permission.
3360 * ALTERNATIVELY, this product may be distributed under the terms of
3361 * the GNU Public License, in which case the provisions of the GPL are
3362 * required INSTEAD OF the above restrictions. (This clause is
3363 * necessary due to a potential bad interaction between the GPL and
3364 * the restrictions contained in a BSD-style copyright.)
3366 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
3367 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3368 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3369 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3370 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3371 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3372 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3373 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3374 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3375 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3376 * OF THE POSSIBILITY OF SUCH DAMAGE.