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;
440 if (iniparser_getboolean(d, "global:mkhomedir", false)) {
441 ctrl |= WINBIND_MKHOMEDIR;
445 /* step through arguments */
446 for (i=argc,v=argv; i-- > 0; ++v) {
448 /* generic options */
449 if (!strcmp(*v,"debug"))
450 ctrl |= WINBIND_DEBUG_ARG;
451 else if (!strcasecmp(*v, "debug_state"))
452 ctrl |= WINBIND_DEBUG_STATE;
453 else if (!strcasecmp(*v, "silent"))
454 ctrl |= WINBIND_SILENT;
455 else if (!strcasecmp(*v, "use_authtok"))
456 ctrl |= WINBIND_USE_AUTHTOK_ARG;
457 else if (!strcasecmp(*v, "use_first_pass"))
458 ctrl |= WINBIND_USE_FIRST_PASS_ARG;
459 else if (!strcasecmp(*v, "try_first_pass"))
460 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
461 else if (!strcasecmp(*v, "unknown_ok"))
462 ctrl |= WINBIND_UNKNOWN_OK_ARG;
463 else if (!strncasecmp(*v, "require_membership_of",
464 strlen("require_membership_of")))
465 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
466 else if (!strncasecmp(*v, "require-membership-of",
467 strlen("require-membership-of")))
468 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
469 else if (!strcasecmp(*v, "krb5_auth"))
470 ctrl |= WINBIND_KRB5_AUTH;
471 else if (!strncasecmp(*v, "krb5_ccache_type",
472 strlen("krb5_ccache_type")))
473 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
474 else if (!strcasecmp(*v, "cached_login"))
475 ctrl |= WINBIND_CACHED_LOGIN;
476 else if (!strcasecmp(*v, "mkhomedir"))
477 ctrl |= WINBIND_MKHOMEDIR;
479 __pam_log(pamh, ctrl, LOG_ERR,
480 "pam_parse: unknown option: %s", *v);
490 iniparser_freedict(d);
497 static int _pam_winbind_free_context(struct pwb_context *ctx)
504 iniparser_freedict(ctx->dict);
510 static int _pam_winbind_init_context(pam_handle_t *pamh,
514 struct pwb_context **ctx_p)
516 struct pwb_context *r = NULL;
518 r = TALLOC_ZERO_P(NULL, struct pwb_context);
523 talloc_set_destructor(r, _pam_winbind_free_context);
529 r->ctrl = _pam_parse(pamh, flags, argc, argv, &r->dict);
532 return PAM_SYSTEM_ERR;
540 static void _pam_winbind_cleanup_func(pam_handle_t *pamh,
544 int ctrl = _pam_parse(pamh, 0, 0, NULL, NULL);
545 if (_pam_log_is_debug_state_enabled(ctrl)) {
546 __pam_log_debug(pamh, ctrl, LOG_DEBUG,
547 "[pamh: %p] CLEAN: cleaning up PAM data %p "
548 "(error_status = %d)", pamh, data,
555 static const struct ntstatus_errors {
556 const char *ntstatus_string;
557 const char *error_string;
558 } ntstatus_errors[] = {
561 {"NT_STATUS_BACKUP_CONTROLLER",
562 "No primary Domain Controler available"},
563 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
564 "No domain controllers found"},
565 {"NT_STATUS_NO_LOGON_SERVERS",
567 {"NT_STATUS_PWD_TOO_SHORT",
568 "Password too short"},
569 {"NT_STATUS_PWD_TOO_RECENT",
570 "The password of this user is too recent to change"},
571 {"NT_STATUS_PWD_HISTORY_CONFLICT",
572 "Password is already in password history"},
573 {"NT_STATUS_PASSWORD_EXPIRED",
574 "Your password has expired"},
575 {"NT_STATUS_PASSWORD_MUST_CHANGE",
576 "You need to change your password now"},
577 {"NT_STATUS_INVALID_WORKSTATION",
578 "You are not allowed to logon from this workstation"},
579 {"NT_STATUS_INVALID_LOGON_HOURS",
580 "You are not allowed to logon at this time"},
581 {"NT_STATUS_ACCOUNT_EXPIRED",
582 "Your account has expired. "
583 "Please contact your System administrator"}, /* SCNR */
584 {"NT_STATUS_ACCOUNT_DISABLED",
585 "Your account is disabled. "
586 "Please contact your System administrator"}, /* SCNR */
587 {"NT_STATUS_ACCOUNT_LOCKED_OUT",
588 "Your account has been locked. "
589 "Please contact your System administrator"}, /* SCNR */
590 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
591 "Invalid Trust Account"},
592 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
593 "Invalid Trust Account"},
594 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
595 "Invalid Trust Account"},
596 {"NT_STATUS_ACCESS_DENIED",
601 static const char *_get_ntstatus_error_string(const char *nt_status_string)
604 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
605 if (!strcasecmp(ntstatus_errors[i].ntstatus_string,
607 return ntstatus_errors[i].error_string;
613 /* --- authentication management functions --- */
615 /* Attempt a conversation */
617 static int converse(const pam_handle_t *pamh,
619 struct pam_message **message,
620 struct pam_response **response)
623 struct pam_conv *conv;
625 retval = _pam_get_item(pamh, PAM_CONV, &conv);
626 if (retval == PAM_SUCCESS) {
627 retval = conv->conv(nargs,
628 (const struct pam_message **)message,
629 response, conv->appdata_ptr);
632 return retval; /* propagate error status */
636 static int _make_remark(struct pwb_context *ctx,
640 int retval = PAM_SUCCESS;
642 struct pam_message *pmsg[1], msg[1];
643 struct pam_response *resp;
645 if (ctx->flags & WINBIND_SILENT) {
650 msg[0].msg = discard_const_p(char, text);
651 msg[0].msg_style = type;
654 retval = converse(ctx->pamh, 1, pmsg, &resp);
657 _pam_drop_reply(resp, 1);
662 static int _make_remark_v(struct pwb_context *ctx,
670 ret = vasprintf(&var, format, args);
672 _pam_log(ctx, LOG_ERR, "memory allocation failure");
676 ret = _make_remark(ctx, type, var);
681 static int _make_remark_format(struct pwb_context *ctx, int type, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
682 static int _make_remark_format(struct pwb_context *ctx, int type, const char *format, ...)
687 va_start(args, format);
688 ret = _make_remark_v(ctx, type, format, args);
693 static int pam_winbind_request_log(struct pwb_context *ctx,
700 /* incorrect password */
701 _pam_log(ctx, LOG_WARNING, "user '%s' denied access "
702 "(incorrect password or invalid membership)", user);
704 case PAM_ACCT_EXPIRED:
705 /* account expired */
706 _pam_log(ctx, LOG_WARNING, "user '%s' account expired",
709 case PAM_AUTHTOK_EXPIRED:
710 /* password expired */
711 _pam_log(ctx, LOG_WARNING, "user '%s' password expired",
714 case PAM_NEW_AUTHTOK_REQD:
715 /* new password required */
716 _pam_log(ctx, LOG_WARNING, "user '%s' new password "
719 case PAM_USER_UNKNOWN:
720 /* the user does not exist */
721 _pam_log_debug(ctx, LOG_NOTICE, "user '%s' not found",
723 if (ctx->ctrl & WINBIND_UNKNOWN_OK_ARG) {
728 /* Otherwise, the authentication looked good */
729 if (strcmp(fn, "wbcLogonUser") == 0) {
730 _pam_log(ctx, LOG_NOTICE,
731 "user '%s' granted access", user);
733 _pam_log(ctx, LOG_NOTICE,
734 "user '%s' OK", user);
738 /* we don't know anything about this return value */
739 _pam_log(ctx, LOG_ERR,
740 "internal module error (retval = %s(%d), user = '%s')",
741 _pam_error_code_str(retval), retval, user);
746 static int wbc_auth_error_to_pam_error(struct pwb_context *ctx,
747 struct wbcAuthErrorInfo *e,
749 const char *username,
752 int ret = PAM_AUTH_ERR;
754 if (WBC_ERROR_IS_OK(status)) {
755 _pam_log_debug(ctx, LOG_DEBUG, "request %s succeeded",
758 return pam_winbind_request_log(ctx, ret, username, fn);
762 if (e->pam_error != PAM_SUCCESS) {
763 _pam_log(ctx, LOG_ERR,
764 "request %s failed: %s, "
765 "PAM error: %s (%d), NTSTATUS: %s, "
766 "Error message was: %s",
768 wbcErrorString(status),
769 _pam_error_code_str(e->pam_error),
774 return pam_winbind_request_log(ctx, ret, username, fn);
777 _pam_log(ctx, LOG_ERR, "request %s failed, but PAM error 0!", fn);
779 ret = PAM_SERVICE_ERR;
780 return pam_winbind_request_log(ctx, ret, username, fn);
783 ret = wbc_error_to_pam_error(status);
784 return pam_winbind_request_log(ctx, ret, username, fn);
789 * send a password expiry message if required
791 * @param ctx PAM winbind context.
792 * @param next_change expected (calculated) next expiry date.
793 * @param already_expired pointer to a boolean to indicate if the password is
796 * @return boolean Returns true if message has been sent, false if not.
799 static bool _pam_send_password_expiry_message(struct pwb_context *ctx,
803 bool *already_expired)
806 struct tm tm_now, tm_next_change;
808 if (already_expired) {
809 *already_expired = false;
812 if (next_change <= now) {
813 PAM_WB_REMARK_DIRECT(ctx, "NT_STATUS_PASSWORD_EXPIRED");
814 if (already_expired) {
815 *already_expired = true;
820 if ((next_change < 0) ||
821 (next_change > now + warn_pwd_expire * SECONDS_PER_DAY)) {
825 if ((localtime_r(&now, &tm_now) == NULL) ||
826 (localtime_r(&next_change, &tm_next_change) == NULL)) {
830 days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) -
831 (tm_now.tm_yday+tm_now.tm_year*365);
834 _make_remark(ctx, PAM_TEXT_INFO,
835 "Your password expires today");
839 if (days > 0 && days < warn_pwd_expire) {
840 _make_remark_format(ctx, PAM_TEXT_INFO,
841 "Your password will expire in %d %s",
842 days, (days > 1) ? "days":"day");
850 * Send a warning if the password expires in the near future
852 * @param ctx PAM winbind context.
853 * @param response The full authentication response structure.
854 * @param already_expired boolean, is the pwd already expired?
859 static void _pam_warn_password_expiry(struct pwb_context *ctx,
860 const struct wbcAuthUserInfo *info,
861 const struct wbcUserPasswordPolicyInfo *policy,
863 bool *already_expired)
865 time_t now = time(NULL);
866 time_t next_change = 0;
868 if (!info || !policy) {
872 if (already_expired) {
873 *already_expired = false;
876 /* accounts with WBC_ACB_PWNOEXP set never receive a warning */
877 if (info->acct_flags & WBC_ACB_PWNOEXP) {
881 /* no point in sending a warning if this is a grace logon */
882 if (PAM_WB_GRACE_LOGON(info->user_flags)) {
886 /* check if the info3 must change timestamp has been set */
887 next_change = info->pass_must_change_time;
889 if (_pam_send_password_expiry_message(ctx, next_change, now,
895 /* now check for the global password policy */
896 /* good catch from Ralf Haferkamp: an expiry of "never" is translated
898 if (policy->expire <= 0) {
902 next_change = info->pass_last_set_time + policy->expire;
904 if (_pam_send_password_expiry_message(ctx, next_change, now,
910 /* no warning sent */
913 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
916 * Append a string, making sure not to overflow and to always return a
917 * NULL-terminated string.
919 * @param dest Destination string buffer (must already be NULL-terminated).
920 * @param src Source string buffer.
921 * @param dest_buffer_size Size of dest buffer in bytes.
923 * @return false if dest buffer is not big enough (no bytes copied), true on
927 static bool safe_append_string(char *dest,
929 int dest_buffer_size)
931 int dest_length = strlen(dest);
932 int src_length = strlen(src);
934 if (dest_length + src_length + 1 > dest_buffer_size) {
938 memcpy(dest + dest_length, src, src_length + 1);
943 * Convert a names into a SID string, appending it to a buffer.
945 * @param ctx PAM winbind context.
946 * @param user User in PAM request.
947 * @param name Name to convert.
948 * @param sid_list_buffer Where to append the string sid.
949 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
951 * @return false on failure, true on success.
953 static bool winbind_name_to_sid_string(struct pwb_context *ctx,
956 char *sid_list_buffer,
957 int sid_list_buffer_size)
959 const char* sid_string;
962 if (IS_SID_STRING(name)) {
966 struct wbcDomainSid sid;
967 enum wbcSidType type;
970 _pam_log_debug(ctx, LOG_DEBUG,
971 "no sid given, looking up: %s\n", name);
973 wbc_status = wbcLookupName("", name, &sid, &type);
974 if (!WBC_ERROR_IS_OK(wbc_status)) {
975 _pam_log(ctx, LOG_INFO,
976 "could not lookup name: %s\n", name);
980 wbc_status = wbcSidToString(&sid, &sid_str);
981 if (!WBC_ERROR_IS_OK(wbc_status)) {
985 wbcFreeMemory(sid_str);
986 sid_string = sid_str;
989 if (!safe_append_string(sid_list_buffer, sid_string,
990 sid_list_buffer_size)) {
998 * Convert a list of names into a list of sids.
1000 * @param ctx PAM winbind context.
1001 * @param user User in PAM request.
1002 * @param name_list List of names or string sids, separated by commas.
1003 * @param sid_list_buffer Where to put the list of string sids.
1004 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1006 * @return false on failure, true on success.
1008 static bool winbind_name_list_to_sid_string_list(struct pwb_context *ctx,
1010 const char *name_list,
1011 char *sid_list_buffer,
1012 int sid_list_buffer_size)
1014 bool result = false;
1015 char *current_name = NULL;
1016 const char *search_location;
1019 if (sid_list_buffer_size > 0) {
1020 sid_list_buffer[0] = 0;
1023 search_location = name_list;
1024 while ((comma = strstr(search_location, ",")) != NULL) {
1025 current_name = strndup(search_location,
1026 comma - search_location);
1027 if (NULL == current_name) {
1031 if (!winbind_name_to_sid_string(ctx, user,
1034 sid_list_buffer_size)) {
1038 SAFE_FREE(current_name);
1040 if (!safe_append_string(sid_list_buffer, ",",
1041 sid_list_buffer_size)) {
1045 search_location = comma + 1;
1048 if (!winbind_name_to_sid_string(ctx, user, search_location,
1050 sid_list_buffer_size)) {
1057 SAFE_FREE(current_name);
1062 * put krb5ccname variable into environment
1064 * @param ctx PAM winbind context.
1065 * @param krb5ccname env variable retrieved from winbindd.
1070 static void _pam_setup_krb5_env(struct pwb_context *ctx,
1071 struct wbcLogonUserInfo *info)
1076 const char *krb5ccname = NULL;
1078 if (off(ctx->ctrl, WINBIND_KRB5_AUTH)) {
1086 for (i=0; i < info->num_blobs; i++) {
1087 if (strcasecmp(info->blobs[i].name, "krb5ccname") == 0) {
1088 krb5ccname = (const char *)info->blobs[i].blob.data;
1093 if (!krb5ccname || (strlen(krb5ccname) == 0)) {
1097 _pam_log_debug(ctx, LOG_DEBUG,
1098 "request returned KRB5CCNAME: %s", krb5ccname);
1100 if (snprintf(var, sizeof(var), "KRB5CCNAME=%s", krb5ccname) == -1) {
1104 ret = pam_putenv(ctx->pamh, var);
1106 _pam_log(ctx, LOG_ERR,
1107 "failed to set KRB5CCNAME to %s: %s",
1108 var, pam_strerror(ctx->pamh, ret));
1113 * Copy unix username if available (further processed in PAM).
1115 * @param ctx PAM winbind context
1116 * @param user_ret A pointer that holds a pointer to a string
1117 * @param unix_username A username
1122 static void _pam_setup_unix_username(struct pwb_context *ctx,
1124 struct wbcLogonUserInfo *info)
1126 const char *unix_username = NULL;
1129 if (!user_ret || !info) {
1133 for (i=0; i < info->num_blobs; i++) {
1134 if (strcasecmp(info->blobs[i].name, "unix_username") == 0) {
1135 unix_username = (const char *)info->blobs[i].blob.data;
1140 if (!unix_username || !unix_username[0]) {
1144 *user_ret = strdup(unix_username);
1148 * Set string into the PAM stack.
1150 * @param ctx PAM winbind context.
1151 * @param data_name Key name for pam_set_data.
1152 * @param value String value.
1157 static void _pam_set_data_string(struct pwb_context *ctx,
1158 const char *data_name,
1163 if (!data_name || !value || (strlen(data_name) == 0) ||
1164 (strlen(value) == 0)) {
1168 ret = pam_set_data(ctx->pamh, data_name, talloc_strdup(NULL, value),
1169 _pam_winbind_cleanup_func);
1171 _pam_log_debug(ctx, LOG_DEBUG,
1172 "Could not set data %s: %s\n",
1173 data_name, pam_strerror(ctx->pamh, ret));
1178 * Set info3 strings into the PAM stack.
1180 * @param ctx PAM winbind context.
1181 * @param data_name Key name for pam_set_data.
1182 * @param value String value.
1187 static void _pam_set_data_info3(struct pwb_context *ctx,
1188 const struct wbcAuthUserInfo *info)
1190 _pam_set_data_string(ctx, PAM_WINBIND_HOMEDIR,
1191 info->home_directory);
1192 _pam_set_data_string(ctx, PAM_WINBIND_LOGONSCRIPT,
1193 info->logon_script);
1194 _pam_set_data_string(ctx, PAM_WINBIND_LOGONSERVER,
1195 info->logon_server);
1196 _pam_set_data_string(ctx, PAM_WINBIND_PROFILEPATH,
1197 info->profile_path);
1201 * Free info3 strings in the PAM stack.
1203 * @param pamh PAM handle
1208 static void _pam_free_data_info3(pam_handle_t *pamh)
1210 pam_set_data(pamh, PAM_WINBIND_HOMEDIR, NULL, NULL);
1211 pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT, NULL, NULL);
1212 pam_set_data(pamh, PAM_WINBIND_LOGONSERVER, NULL, NULL);
1213 pam_set_data(pamh, PAM_WINBIND_PROFILEPATH, NULL, NULL);
1217 * Send PAM_ERROR_MSG for cached or grace logons.
1219 * @param ctx PAM winbind context.
1220 * @param username User in PAM request.
1221 * @param info3_user_flgs Info3 flags containing logon type bits.
1226 static void _pam_warn_logon_type(struct pwb_context *ctx,
1227 const char *username,
1228 uint32_t info3_user_flgs)
1230 /* inform about logon type */
1231 if (PAM_WB_GRACE_LOGON(info3_user_flgs)) {
1233 _make_remark(ctx, PAM_ERROR_MSG,
1235 "Please change your password as soon you're "
1237 _pam_log_debug(ctx, LOG_DEBUG,
1238 "User %s logged on using grace logon\n",
1241 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs)) {
1243 _make_remark(ctx, PAM_ERROR_MSG,
1244 "Domain Controller unreachable, "
1245 "using cached credentials instead. "
1246 "Network resources may be unavailable");
1247 _pam_log_debug(ctx, LOG_DEBUG,
1248 "User %s logged on using cached credentials\n",
1254 * Send PAM_ERROR_MSG for krb5 errors.
1256 * @param ctx PAM winbind context.
1257 * @param username User in PAM request.
1258 * @param info3_user_flgs Info3 flags containing logon type bits.
1263 static void _pam_warn_krb5_failure(struct pwb_context *ctx,
1264 const char *username,
1265 uint32_t info3_user_flgs)
1267 if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs)) {
1268 _make_remark(ctx, PAM_ERROR_MSG,
1269 "Failed to establish your Kerberos Ticket cache "
1270 "due time differences\n"
1271 "with the domain controller. "
1272 "Please verify the system time.\n");
1273 _pam_log_debug(ctx, LOG_DEBUG,
1274 "User %s: Clock skew when getting Krb5 TGT\n",
1279 static bool _pam_check_remark_auth_err(struct pwb_context *ctx,
1280 const struct wbcAuthErrorInfo *e,
1281 const char *nt_status_string,
1284 const char *ntstatus = NULL;
1285 const char *error_string = NULL;
1287 if (!e || !pam_error) {
1291 ntstatus = e->nt_string;
1296 if (strcasecmp(ntstatus, nt_status_string) == 0) {
1298 error_string = _get_ntstatus_error_string(nt_status_string);
1300 _make_remark(ctx, PAM_ERROR_MSG, error_string);
1301 *pam_error = e->pam_error;
1305 if (e->display_string) {
1306 _make_remark(ctx, PAM_ERROR_MSG, e->display_string);
1307 *pam_error = e->pam_error;
1311 _make_remark(ctx, PAM_ERROR_MSG, nt_status_string);
1312 *pam_error = e->pam_error;
1321 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
1323 * @param i The wbcUserPasswordPolicyInfo struct.
1325 * @return string (caller needs to talloc_free).
1328 static char *_pam_compose_pwd_restriction_string(struct pwb_context *ctx,
1329 struct wbcUserPasswordPolicyInfo *i)
1337 str = talloc_asprintf(ctx, "Your password ");
1342 if (i->min_length_password > 0) {
1343 str = talloc_asprintf_append(str,
1344 "must be at least %d characters; ",
1345 i->min_length_password);
1351 if (i->password_history > 0) {
1352 str = talloc_asprintf_append(str,
1353 "cannot repeat any of your previous %d "
1355 i->password_history);
1361 if (i->password_properties & WBC_DOMAIN_PASSWORD_COMPLEX) {
1362 str = talloc_asprintf_append(str,
1363 "must contain capitals, numerals "
1365 "and cannot contain your account "
1372 str = talloc_asprintf_append(str,
1373 "Please type a different password. "
1374 "Type a password which meets these requirements in "
1375 "both text boxes.");
1387 static int _pam_create_homedir(struct pwb_context *ctx,
1388 const char *dirname,
1393 if (stat(dirname, &sbuf) == 0) {
1397 if (mkdir(dirname, mode) != 0) {
1399 _make_remark_format(ctx, PAM_TEXT_INFO,
1400 "Creating directory: %s failed: %s",
1401 dirname, strerror(errno));
1402 _pam_log(ctx, LOG_ERR, "could not create dir: %s (%s)",
1403 dirname, strerror(errno));
1404 return PAM_PERM_DENIED;
1410 static int _pam_chown_homedir(struct pwb_context *ctx,
1411 const char *dirname,
1415 if (chown(dirname, uid, gid) != 0) {
1416 _pam_log(ctx, LOG_ERR, "failed to chown user homedir: %s (%s)",
1417 dirname, strerror(errno));
1418 return PAM_PERM_DENIED;
1424 static int _pam_mkhomedir(struct pwb_context *ctx)
1426 struct passwd *pwd = NULL;
1428 char *create_dir = NULL;
1429 char *user_dir = NULL;
1431 const char *username;
1433 char *safe_ptr = NULL;
1436 /* Get the username */
1437 ret = pam_get_user(ctx->pamh, &username, NULL);
1438 if ((ret != PAM_SUCCESS) || (!username)) {
1439 _pam_log_debug(ctx, LOG_DEBUG, "can not get the username");
1440 return PAM_SERVICE_ERR;
1443 pwd = getpwnam(username);
1445 _pam_log_debug(ctx, LOG_DEBUG, "can not get the username");
1446 return PAM_USER_UNKNOWN;
1448 _pam_log_debug(ctx, LOG_DEBUG, "homedir is: %s", pwd->pw_dir);
1450 ret = _pam_create_homedir(ctx, pwd->pw_dir, 0700);
1451 if (ret == PAM_SUCCESS) {
1452 ret = _pam_chown_homedir(ctx, pwd->pw_dir,
1457 if (ret == PAM_SUCCESS) {
1461 /* maybe we need to create parent dirs */
1462 create_dir = talloc_strdup(ctx, "/");
1467 /* find final directory */
1468 user_dir = strrchr(pwd->pw_dir, '/');
1474 _pam_log(ctx, LOG_DEBUG, "final directory: %s", user_dir);
1478 while ((token = strtok_r(p, "/", &safe_ptr)) != NULL) {
1484 _pam_log_debug(ctx, LOG_DEBUG, "token is %s", token);
1486 create_dir = talloc_asprintf_append(create_dir, "%s/", token);
1490 _pam_log_debug(ctx, LOG_DEBUG, "current_dir is %s", create_dir);
1492 if (strcmp(token, user_dir) == 0) {
1493 _pam_log_debug(ctx, LOG_DEBUG, "assuming last directory: %s", token);
1497 ret = _pam_create_homedir(ctx, create_dir, mode);
1503 return _pam_chown_homedir(ctx, create_dir,
1508 /* talk to winbindd */
1509 static int winbind_auth_request(struct pwb_context *ctx,
1514 const int warn_pwd_expire,
1515 struct wbcAuthErrorInfo **p_error,
1516 struct wbcLogonUserInfo **p_info,
1517 struct wbcUserPasswordPolicyInfo **p_policy,
1518 time_t *pwd_last_set,
1523 struct wbcLogonUserParams logon;
1524 char membership_of[1024];
1525 uid_t user_uid = -1;
1526 uint32_t flags = WBFLAG_PAM_INFO3_TEXT |
1527 WBFLAG_PAM_GET_PWD_POLICY;
1529 struct wbcLogonUserInfo *info = NULL;
1530 struct wbcAuthUserInfo *user_info = NULL;
1531 struct wbcAuthErrorInfo *error = NULL;
1532 struct wbcUserPasswordPolicyInfo *policy = NULL;
1534 int ret = PAM_AUTH_ERR;
1536 const char *codes[] = {
1537 "NT_STATUS_PASSWORD_EXPIRED",
1538 "NT_STATUS_PASSWORD_MUST_CHANGE",
1539 "NT_STATUS_INVALID_WORKSTATION",
1540 "NT_STATUS_INVALID_LOGON_HOURS",
1541 "NT_STATUS_ACCOUNT_EXPIRED",
1542 "NT_STATUS_ACCOUNT_DISABLED",
1543 "NT_STATUS_ACCOUNT_LOCKED_OUT",
1544 "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
1545 "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
1546 "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
1547 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1548 "NT_STATUS_NO_LOGON_SERVERS",
1549 "NT_STATUS_WRONG_PASSWORD",
1550 "NT_STATUS_ACCESS_DENIED"
1557 /* Krb5 auth always has to go against the KDC of the user's realm */
1559 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1560 flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
1563 if (ctx->ctrl & (WINBIND_KRB5_AUTH|WINBIND_CACHED_LOGIN)) {
1564 struct passwd *pwd = NULL;
1566 pwd = getpwnam(user);
1568 return PAM_USER_UNKNOWN;
1570 user_uid = pwd->pw_uid;
1573 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1575 _pam_log_debug(ctx, LOG_DEBUG,
1576 "enabling krb5 login flag\n");
1578 flags |= WBFLAG_PAM_KRB5 |
1579 WBFLAG_PAM_FALLBACK_AFTER_KRB5;
1582 if (ctx->ctrl & WINBIND_CACHED_LOGIN) {
1583 _pam_log_debug(ctx, LOG_DEBUG,
1584 "enabling cached login flag\n");
1585 flags |= WBFLAG_PAM_CACHED_LOGIN;
1590 flags |= WBFLAG_PAM_UNIX_NAME;
1593 if (cctype != NULL) {
1594 _pam_log_debug(ctx, LOG_DEBUG,
1595 "enabling request for a %s krb5 ccache\n",
1599 if (member != NULL) {
1601 ZERO_STRUCT(membership_of);
1603 if (!winbind_name_list_to_sid_string_list(ctx, user, member,
1605 sizeof(membership_of))) {
1606 _pam_log_debug(ctx, LOG_ERR,
1607 "failed to serialize membership of sid "
1608 "\"%s\"\n", member);
1609 return PAM_AUTH_ERR;
1615 logon.username = user;
1616 logon.password = pass;
1618 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1624 if (!WBC_ERROR_IS_OK(wbc_status)) {
1628 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1634 if (!WBC_ERROR_IS_OK(wbc_status)) {
1638 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1642 (uint8_t *)&user_uid,
1644 if (!WBC_ERROR_IS_OK(wbc_status)) {
1648 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1652 (uint8_t *)membership_of,
1653 sizeof(membership_of));
1654 if (!WBC_ERROR_IS_OK(wbc_status)) {
1658 wbc_status = wbcLogonUser(&logon, &info, &error, &policy);
1659 ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
1660 user, "wbcLogonUser");
1661 wbcFreeMemory(logon.blobs);
1664 if (info && info->info) {
1665 user_info = info->info;
1668 if (pwd_last_set && user_info) {
1669 *pwd_last_set = user_info->pass_last_set_time;
1672 if (p_info && info) {
1676 if (p_policy && policy) {
1680 if (p_error && error) {
1681 /* We want to process the error in the caller. */
1686 for (i=0; i<ARRAY_SIZE(codes); i++) {
1688 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
1694 if ((ret == PAM_SUCCESS) && user_info && policy && info) {
1696 bool already_expired = false;
1698 /* warn a user if the password is about to expire soon */
1699 _pam_warn_password_expiry(ctx, user_info, policy,
1703 if (already_expired == true) {
1705 SMB_TIME_T last_set = user_info->pass_last_set_time;
1707 _pam_log_debug(ctx, LOG_DEBUG,
1708 "Password has expired "
1709 "(Password was last set: %lld, "
1710 "the policy says it should expire here "
1711 "%lld (now it's: %lu))\n",
1712 (long long int)last_set,
1713 (long long int)last_set +
1717 return PAM_AUTHTOK_EXPIRED;
1720 /* inform about logon type */
1721 _pam_warn_logon_type(ctx, user, user_info->user_flags);
1723 /* inform about krb5 failures */
1724 _pam_warn_krb5_failure(ctx, user, user_info->user_flags);
1726 /* set some info3 info for other modules in the stack */
1727 _pam_set_data_info3(ctx, user_info);
1729 /* put krb5ccname into env */
1730 _pam_setup_krb5_env(ctx, info);
1732 /* If winbindd returned a username, return the pointer to it
1734 _pam_setup_unix_username(ctx, user_ret, info);
1739 wbcFreeMemory(logon.blobs);
1741 if (info && info->blobs) {
1742 wbcFreeMemory(info->blobs);
1744 if (error && !p_error) {
1745 wbcFreeMemory(error);
1747 if (info && !p_info) {
1748 wbcFreeMemory(info);
1750 if (policy && !p_policy) {
1751 wbcFreeMemory(policy);
1757 /* talk to winbindd */
1758 static int winbind_chauthtok_request(struct pwb_context *ctx,
1760 const char *oldpass,
1761 const char *newpass,
1762 time_t pwd_last_set)
1765 struct wbcChangePasswordParams params;
1766 struct wbcAuthErrorInfo *error = NULL;
1767 struct wbcUserPasswordPolicyInfo *policy = NULL;
1768 enum wbcPasswordChangeRejectReason reject_reason = -1;
1772 const char *codes[] = {
1773 "NT_STATUS_BACKUP_CONTROLLER",
1774 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1775 "NT_STATUS_NO_LOGON_SERVERS",
1776 "NT_STATUS_ACCESS_DENIED",
1777 "NT_STATUS_PWD_TOO_SHORT", /* TODO: tell the min pwd length ? */
1778 "NT_STATUS_PWD_TOO_RECENT", /* TODO: tell the minage ? */
1779 "NT_STATUS_PWD_HISTORY_CONFLICT" /* TODO: tell the history length ? */
1781 int ret = PAM_AUTH_ERR;
1783 ZERO_STRUCT(params);
1785 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1786 flags |= WBFLAG_PAM_KRB5 |
1787 WBFLAG_PAM_CONTACT_TRUSTDOM;
1790 if (ctx->ctrl & WINBIND_CACHED_LOGIN) {
1791 flags |= WBFLAG_PAM_CACHED_LOGIN;
1794 params.account_name = user;
1795 params.level = WBC_AUTH_USER_LEVEL_PLAIN;
1796 params.old_password.plaintext = oldpass;
1797 params.new_password.plaintext = newpass;
1798 params.flags = flags;
1800 wbc_status = wbcChangeUserPasswordEx(¶ms, &error, &reject_reason, &policy);
1801 ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
1802 user, "wbcChangeUserPasswordEx");
1804 if (WBC_ERROR_IS_OK(wbc_status)) {
1805 _pam_log(ctx, LOG_NOTICE,
1806 "user '%s' password changed", user);
1811 wbcFreeMemory(policy);
1815 for (i=0; i<ARRAY_SIZE(codes); i++) {
1817 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
1823 if (!strcasecmp(error->nt_string,
1824 "NT_STATUS_PASSWORD_RESTRICTION")) {
1826 char *pwd_restriction_string = NULL;
1827 SMB_TIME_T min_pwd_age = 0;
1830 min_pwd_age = policy->min_passwordage;
1833 /* FIXME: avoid to send multiple PAM messages after another */
1834 switch (reject_reason) {
1837 case WBC_PWD_CHANGE_REJECT_OTHER:
1838 if ((min_pwd_age > 0) &&
1839 (pwd_last_set + min_pwd_age > time(NULL))) {
1840 PAM_WB_REMARK_DIRECT(ctx,
1841 "NT_STATUS_PWD_TOO_RECENT");
1844 case WBC_PWD_CHANGE_REJECT_TOO_SHORT:
1845 PAM_WB_REMARK_DIRECT(ctx,
1846 "NT_STATUS_PWD_TOO_SHORT");
1848 case WBC_PWD_CHANGE_REJECT_IN_HISTORY:
1849 PAM_WB_REMARK_DIRECT(ctx,
1850 "NT_STATUS_PWD_HISTORY_CONFLICT");
1852 case WBC_PWD_CHANGE_REJECT_COMPLEXITY:
1853 _make_remark(ctx, PAM_ERROR_MSG,
1854 "Password does not meet "
1855 "complexity requirements");
1858 _pam_log_debug(ctx, LOG_DEBUG,
1859 "unknown password change "
1860 "reject reason: %d",
1865 pwd_restriction_string =
1866 _pam_compose_pwd_restriction_string(ctx, policy);
1867 if (pwd_restriction_string) {
1868 _make_remark(ctx, PAM_ERROR_MSG,
1869 pwd_restriction_string);
1870 TALLOC_FREE(pwd_restriction_string);
1874 wbcFreeMemory(error);
1875 wbcFreeMemory(policy);
1881 * Checks if a user has an account
1884 * 1 = User not found
1888 static int valid_user(struct pwb_context *ctx,
1891 /* check not only if the user is available over NSS calls, also make
1892 * sure it's really a winbind user, this is important when stacking PAM
1893 * modules in the 'account' or 'password' facility. */
1896 struct passwd *pwd = NULL;
1897 struct passwd *wb_pwd = NULL;
1899 pwd = getpwnam(user);
1904 wbc_status = wbcGetpwnam(user, &wb_pwd);
1905 wbcFreeMemory(wb_pwd);
1906 if (!WBC_ERROR_IS_OK(wbc_status)) {
1907 _pam_log(ctx, LOG_DEBUG, "valid_user: wbcGetpwnam gave %s\n",
1908 wbcErrorString(wbc_status));
1911 switch (wbc_status) {
1912 case WBC_ERR_UNKNOWN_USER:
1914 case WBC_ERR_SUCCESS:
1922 static char *_pam_delete(register char *xx)
1930 * obtain a password from the user
1933 static int _winbind_read_password(struct pwb_context *ctx,
1935 const char *comment,
1936 const char *prompt1,
1937 const char *prompt2,
1945 _pam_log(ctx, LOG_DEBUG, "getting password (0x%08x)", ctrl);
1948 * make sure nothing inappropriate gets returned
1951 *pass = token = NULL;
1954 * which authentication token are we getting?
1957 if (on(WINBIND__OLD_PASSWORD, ctrl)) {
1958 authtok_flag = PAM_OLDAUTHTOK;
1960 authtok_flag = PAM_AUTHTOK;
1964 * should we obtain the password from a PAM item ?
1967 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) ||
1968 on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1969 retval = _pam_get_item(ctx->pamh, authtok_flag, &item);
1970 if (retval != PAM_SUCCESS) {
1972 _pam_log(ctx, LOG_ALERT,
1973 "pam_get_item returned error "
1974 "to unix-read-password");
1976 } else if (item != NULL) { /* we have a password! */
1979 _pam_log(ctx, LOG_DEBUG,
1980 "pam_get_item returned a password");
1982 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1983 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
1984 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
1985 && off(WINBIND__OLD_PASSWORD, ctrl)) {
1986 return PAM_AUTHTOK_RECOVER_ERR;
1990 * getting here implies we will have to get the password from the
1995 struct pam_message msg[3], *pmsg[3];
1996 struct pam_response *resp;
1999 /* prepare to converse */
2001 if (comment != NULL && off(ctrl, WINBIND_SILENT)) {
2003 msg[0].msg_style = PAM_TEXT_INFO;
2004 msg[0].msg = discard_const_p(char, comment);
2011 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
2012 msg[i++].msg = discard_const_p(char, prompt1);
2015 if (prompt2 != NULL) {
2017 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
2018 msg[i++].msg = discard_const_p(char, prompt2);
2021 /* so call the conversation expecting i responses */
2023 retval = converse(ctx->pamh, i, pmsg, &resp);
2025 if (retval == PAM_SUCCESS) {
2026 retval = PAM_AUTHTOK_RECOVER_ERR;
2030 if (retval != PAM_SUCCESS) {
2031 _pam_drop_reply(resp, i);
2035 /* interpret the response */
2037 token = x_strdup(resp[i - replies].resp);
2039 _pam_log(ctx, LOG_NOTICE,
2040 "could not recover "
2041 "authentication token");
2042 retval = PAM_AUTHTOK_RECOVER_ERR;
2047 /* verify that password entered correctly */
2048 if (!resp[i - 1].resp ||
2049 strcmp(token, resp[i - 1].resp)) {
2050 _pam_delete(token); /* mistyped */
2051 retval = PAM_AUTHTOK_RECOVER_ERR;
2052 _make_remark(ctx, PAM_ERROR_MSG,
2058 * tidy up the conversation (resp_retcode) is ignored
2059 * -- what is it for anyway? AGM
2061 _pam_drop_reply(resp, i);
2065 if (retval != PAM_SUCCESS) {
2066 _pam_log_debug(ctx, LOG_DEBUG,
2067 "unable to obtain a password");
2070 /* 'token' is the entered password */
2072 /* we store this password as an item */
2074 retval = pam_set_item(ctx->pamh, authtok_flag, token);
2075 _pam_delete(token); /* clean it up */
2076 if (retval != PAM_SUCCESS ||
2077 (retval = _pam_get_item(ctx->pamh, authtok_flag, &item)) != PAM_SUCCESS) {
2079 _pam_log(ctx, LOG_CRIT, "error manipulating password");
2085 item = NULL; /* break link to password */
2090 static const char *get_conf_item_string(struct pwb_context *ctx,
2095 const char *parm_opt = NULL;
2097 if (!(ctx->ctrl & config_flag)) {
2101 /* let the pam opt take precedence over the pam_winbind.conf option */
2102 for (i=0; i<ctx->argc; i++) {
2104 if ((strncmp(ctx->argv[i], item, strlen(item)) == 0)) {
2107 if ((p = strchr(ctx->argv[i], '=')) == NULL) {
2108 _pam_log(ctx, LOG_INFO,
2109 "no \"=\" delimiter for \"%s\" found\n",
2113 _pam_log_debug(ctx, LOG_INFO,
2114 "PAM config: %s '%s'\n", item, p+1);
2122 key = talloc_asprintf(ctx, "global:%s", item);
2127 parm_opt = iniparser_getstr(ctx->dict, key);
2130 _pam_log_debug(ctx, LOG_INFO, "CONFIG file: %s '%s'\n",
2137 static int get_config_item_int(struct pwb_context *ctx,
2141 int i, parm_opt = -1;
2143 if (!(ctx->ctrl & config_flag)) {
2147 /* let the pam opt take precedence over the pam_winbind.conf option */
2148 for (i = 0; i < ctx->argc; i++) {
2150 if ((strncmp(ctx->argv[i], item, strlen(item)) == 0)) {
2153 if ((p = strchr(ctx->argv[i], '=')) == NULL) {
2154 _pam_log(ctx, LOG_INFO,
2155 "no \"=\" delimiter for \"%s\" found\n",
2159 parm_opt = atoi(p + 1);
2160 _pam_log_debug(ctx, LOG_INFO,
2161 "PAM config: %s '%d'\n",
2170 key = talloc_asprintf(ctx, "global:%s", item);
2175 parm_opt = iniparser_getint(ctx->dict, key, -1);
2178 _pam_log_debug(ctx, LOG_INFO,
2179 "CONFIG file: %s '%d'\n",
2186 static const char *get_krb5_cc_type_from_config(struct pwb_context *ctx)
2188 return get_conf_item_string(ctx, "krb5_ccache_type",
2189 WINBIND_KRB5_CCACHE_TYPE);
2192 static const char *get_member_from_config(struct pwb_context *ctx)
2194 const char *ret = NULL;
2195 ret = get_conf_item_string(ctx, "require_membership_of",
2196 WINBIND_REQUIRED_MEMBERSHIP);
2200 return get_conf_item_string(ctx, "require-membership-of",
2201 WINBIND_REQUIRED_MEMBERSHIP);
2204 static int get_warn_pwd_expire_from_config(struct pwb_context *ctx)
2207 ret = get_config_item_int(ctx, "warn_pwd_expire",
2208 WINBIND_WARN_PWD_EXPIRE);
2209 /* no or broken setting */
2211 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES;
2217 * Retrieve the winbind separator.
2219 * @param ctx PAM winbind context.
2221 * @return string separator character. NULL on failure.
2224 static char winbind_get_separator(struct pwb_context *ctx)
2227 static struct wbcInterfaceDetails *details = NULL;
2229 wbc_status = wbcInterfaceDetails(&details);
2230 if (!WBC_ERROR_IS_OK(wbc_status)) {
2231 _pam_log(ctx, LOG_ERR,
2232 "Could not retrieve winbind interface details: %s",
2233 wbcErrorString(wbc_status));
2241 return details->winbind_separator;
2246 * Convert a upn to a name.
2248 * @param ctx PAM winbind context.
2249 * @param upn USer UPN to be trabslated.
2251 * @return converted name. NULL pointer on failure. Caller needs to free.
2254 static char* winbind_upn_to_username(struct pwb_context *ctx,
2258 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
2259 struct wbcDomainSid sid;
2260 enum wbcSidType type;
2264 /* This cannot work when the winbind separator = @ */
2266 sep = winbind_get_separator(ctx);
2267 if (!sep || sep == '@') {
2271 /* Convert the UPN to a SID */
2273 wbc_status = wbcLookupName("", upn, &sid, &type);
2274 if (!WBC_ERROR_IS_OK(wbc_status)) {
2278 /* Convert the the SID back to the sAMAccountName */
2280 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
2281 if (!WBC_ERROR_IS_OK(wbc_status)) {
2285 return talloc_asprintf(ctx, "%s\\%s", domain, name);
2289 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
2290 int argc, const char **argv)
2292 const char *username;
2293 const char *password;
2294 const char *member = NULL;
2295 const char *cctype = NULL;
2296 int warn_pwd_expire;
2297 int retval = PAM_AUTH_ERR;
2298 char *username_ret = NULL;
2299 char *new_authtok_required = NULL;
2300 char *real_username = NULL;
2301 struct pwb_context *ctx = NULL;
2303 retval = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2308 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", ctx);
2310 /* Get the username */
2311 retval = pam_get_user(pamh, &username, NULL);
2312 if ((retval != PAM_SUCCESS) || (!username)) {
2313 _pam_log_debug(ctx, LOG_DEBUG,
2314 "can not get the username");
2315 retval = PAM_SERVICE_ERR;
2321 /* Decode the user name since AIX does not support logn user
2322 names by default. The name is encoded as _#uid. */
2324 if (username[0] == '_') {
2325 uid_t id = atoi(&username[1]);
2326 struct passwd *pw = NULL;
2328 if ((id!=0) && ((pw = getpwuid(id)) != NULL)) {
2329 real_username = strdup(pw->pw_name);
2334 if (!real_username) {
2335 /* Just making a copy of the username we got from PAM */
2336 if ((real_username = strdup(username)) == NULL) {
2337 _pam_log_debug(ctx, LOG_DEBUG,
2338 "memory allocation failure when copying "
2340 retval = PAM_SERVICE_ERR;
2345 /* Maybe this was a UPN */
2347 if (strchr(real_username, '@') != NULL) {
2348 char *samaccountname = NULL;
2350 samaccountname = winbind_upn_to_username(ctx,
2352 if (samaccountname) {
2353 free(real_username);
2354 real_username = strdup(samaccountname);
2358 retval = _winbind_read_password(ctx, ctx->ctrl, NULL,
2362 if (retval != PAM_SUCCESS) {
2363 _pam_log(ctx, LOG_ERR,
2364 "Could not retrieve user's password");
2365 retval = PAM_AUTHTOK_ERR;
2369 /* Let's not give too much away in the log file */
2371 #ifdef DEBUG_PASSWORD
2372 _pam_log_debug(ctx, LOG_INFO,
2373 "Verify user '%s' with password '%s'",
2374 real_username, password);
2376 _pam_log_debug(ctx, LOG_INFO,
2377 "Verify user '%s'", real_username);
2380 member = get_member_from_config(ctx);
2381 cctype = get_krb5_cc_type_from_config(ctx);
2382 warn_pwd_expire = get_warn_pwd_expire_from_config(ctx);
2384 /* Now use the username to look up password */
2385 retval = winbind_auth_request(ctx, real_username, password,
2386 member, cctype, warn_pwd_expire,
2388 NULL, &username_ret);
2390 if (retval == PAM_NEW_AUTHTOK_REQD ||
2391 retval == PAM_AUTHTOK_EXPIRED) {
2393 char *new_authtok_required_during_auth = NULL;
2395 new_authtok_required = talloc_asprintf(NULL, "%d", retval);
2396 if (!new_authtok_required) {
2397 retval = PAM_BUF_ERR;
2401 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2402 new_authtok_required,
2403 _pam_winbind_cleanup_func);
2405 retval = PAM_SUCCESS;
2407 new_authtok_required_during_auth = talloc_asprintf(NULL, "%d", true);
2408 if (!new_authtok_required_during_auth) {
2409 retval = PAM_BUF_ERR;
2413 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2414 new_authtok_required_during_auth,
2415 _pam_winbind_cleanup_func);
2422 pam_set_item (pamh, PAM_USER, username_ret);
2423 _pam_log_debug(ctx, LOG_INFO,
2424 "Returned user was '%s'", username_ret);
2428 if (real_username) {
2429 free(real_username);
2432 if (!new_authtok_required) {
2433 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
2436 if (retval != PAM_SUCCESS) {
2437 _pam_free_data_info3(pamh);
2440 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", ctx, retval);
2448 int pam_sm_setcred(pam_handle_t *pamh, int flags,
2449 int argc, const char **argv)
2451 int ret = PAM_SYSTEM_ERR;
2452 struct pwb_context *ctx = NULL;
2454 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2459 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", ctx);
2461 switch (flags & ~PAM_SILENT) {
2463 case PAM_DELETE_CRED:
2464 ret = pam_sm_close_session(pamh, flags, argc, argv);
2466 case PAM_REFRESH_CRED:
2467 _pam_log_debug(ctx, LOG_WARNING,
2468 "PAM_REFRESH_CRED not implemented");
2471 case PAM_REINITIALIZE_CRED:
2472 _pam_log_debug(ctx, LOG_WARNING,
2473 "PAM_REINITIALIZE_CRED not implemented");
2476 case PAM_ESTABLISH_CRED:
2477 _pam_log_debug(ctx, LOG_WARNING,
2478 "PAM_ESTABLISH_CRED not implemented");
2482 ret = PAM_SYSTEM_ERR;
2488 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", ctx, ret);
2496 * Account management. We want to verify that the account exists
2497 * before returning PAM_SUCCESS
2500 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
2501 int argc, const char **argv)
2503 const char *username;
2504 int ret = PAM_USER_UNKNOWN;
2506 struct pwb_context *ctx = NULL;
2508 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2513 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", ctx);
2516 /* Get the username */
2517 ret = pam_get_user(pamh, &username, NULL);
2518 if ((ret != PAM_SUCCESS) || (!username)) {
2519 _pam_log_debug(ctx, LOG_DEBUG,
2520 "can not get the username");
2521 ret = PAM_SERVICE_ERR;
2525 /* Verify the username */
2526 ret = valid_user(ctx, username);
2529 /* some sort of system error. The log was already printed */
2530 ret = PAM_SERVICE_ERR;
2533 /* the user does not exist */
2534 _pam_log_debug(ctx, LOG_NOTICE, "user '%s' not found",
2536 if (ctx->ctrl & WINBIND_UNKNOWN_OK_ARG) {
2540 ret = PAM_USER_UNKNOWN;
2543 pam_get_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2544 (const void **)&tmp);
2546 ret = atoi((const char *)tmp);
2548 case PAM_AUTHTOK_EXPIRED:
2549 /* fall through, since new token is required in this case */
2550 case PAM_NEW_AUTHTOK_REQD:
2551 _pam_log(ctx, LOG_WARNING,
2552 "pam_sm_acct_mgmt success but %s is set",
2553 PAM_WINBIND_NEW_AUTHTOK_REQD);
2554 _pam_log(ctx, LOG_NOTICE,
2555 "user '%s' needs new password",
2557 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2558 ret = PAM_NEW_AUTHTOK_REQD;
2561 _pam_log(ctx, LOG_WARNING,
2562 "pam_sm_acct_mgmt success");
2563 _pam_log(ctx, LOG_NOTICE,
2564 "user '%s' granted access", username);
2570 /* Otherwise, the authentication looked good */
2571 _pam_log(ctx, LOG_NOTICE,
2572 "user '%s' granted access", username);
2576 /* we don't know anything about this return value */
2577 _pam_log(ctx, LOG_ERR,
2578 "internal module error (ret = %d, user = '%s')",
2580 ret = PAM_SERVICE_ERR;
2584 /* should not be reached */
2589 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", ctx, ret);
2597 int pam_sm_open_session(pam_handle_t *pamh, int flags,
2598 int argc, const char **argv)
2600 int ret = PAM_SUCCESS;
2601 struct pwb_context *ctx = NULL;
2603 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2608 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", ctx);
2610 if (ctx->ctrl & WINBIND_MKHOMEDIR) {
2611 /* check and create homedir */
2612 ret = _pam_mkhomedir(ctx);
2615 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", ctx, ret);
2623 int pam_sm_close_session(pam_handle_t *pamh, int flags,
2624 int argc, const char **argv)
2626 int retval = PAM_SUCCESS;
2627 struct pwb_context *ctx = NULL;
2628 struct wbcLogoffUserParams logoff;
2630 retval = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2635 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", ctx);
2637 if (!(flags & PAM_DELETE_CRED)) {
2638 retval = PAM_SUCCESS;
2642 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
2644 /* destroy the ccache here */
2647 struct wbcAuthErrorInfo *error = NULL;
2651 const char *ccname = NULL;
2652 struct passwd *pwd = NULL;
2654 retval = pam_get_user(pamh, &user, "Username: ");
2656 _pam_log(ctx, LOG_ERR,
2657 "could not identify user");
2662 _pam_log(ctx, LOG_ERR,
2663 "username was NULL!");
2664 retval = PAM_USER_UNKNOWN;
2668 _pam_log_debug(ctx, LOG_DEBUG,
2669 "username [%s] obtained", user);
2671 ccname = pam_getenv(pamh, "KRB5CCNAME");
2672 if (ccname == NULL) {
2673 _pam_log_debug(ctx, LOG_DEBUG,
2674 "user has no KRB5CCNAME environment");
2677 pwd = getpwnam(user);
2679 retval = PAM_USER_UNKNOWN;
2683 flags = WBFLAG_PAM_KRB5 |
2684 WBFLAG_PAM_CONTACT_TRUSTDOM;
2686 ZERO_STRUCT(logoff);
2688 logoff.username = user;
2690 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2696 if (!WBC_ERROR_IS_OK(wbc_status)) {
2700 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2706 if (!WBC_ERROR_IS_OK(wbc_status)) {
2710 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2714 (uint8_t *)&pwd->pw_uid,
2715 sizeof(pwd->pw_uid));
2716 if (!WBC_ERROR_IS_OK(wbc_status)) {
2720 wbc_status = wbcLogoffUserEx(&logoff, &error);
2721 retval = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
2722 user, "wbcLogoffUser");
2723 wbcFreeMemory(error);
2724 wbcFreeMemory(logoff.blobs);
2726 if (!WBC_ERROR_IS_OK(wbc_status)) {
2727 _pam_log(ctx, LOG_INFO,
2728 "failed to logoff user %s: %s\n",
2729 user, wbcErrorString(wbc_status));
2735 wbcFreeMemory(logoff.blobs);
2738 if (!WBC_ERROR_IS_OK(wbc_status)) {
2739 retval = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
2740 user, "wbcLogoffUser");
2743 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", ctx, retval);
2751 * evaluate whether we need to re-authenticate with kerberos after a
2754 * @param ctx PAM winbind context.
2755 * @param user The username
2757 * @return boolean Returns true if required, false if not.
2760 static bool _pam_require_krb5_auth_after_chauthtok(struct pwb_context *ctx,
2764 /* Make sure that we only do this if a) the chauthtok got initiated
2765 * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2766 * later password change via the "passwd" command if done by the user
2768 * NB. If we login from gdm or xdm and the password expires,
2769 * we change the password, but there is no memory cache.
2770 * Thus, even for passthrough login, we should do the
2771 * authentication again to update memory cache.
2775 char *new_authtok_reqd_during_auth = NULL;
2776 struct passwd *pwd = NULL;
2778 _pam_get_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2779 &new_authtok_reqd_during_auth);
2780 pam_set_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2783 if (new_authtok_reqd_during_auth) {
2787 pwd = getpwnam(user);
2792 if (getuid() == pwd->pw_uid) {
2801 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
2802 int argc, const char **argv)
2806 bool cached_login = false;
2808 /* <DO NOT free() THESE> */
2810 char *pass_old, *pass_new;
2811 /* </DO NOT free() THESE> */
2816 char *username_ret = NULL;
2817 struct wbcAuthErrorInfo *error = NULL;
2818 struct pwb_context *ctx = NULL;
2820 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2825 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", ctx);
2827 cached_login = (ctx->ctrl & WINBIND_CACHED_LOGIN);
2829 /* clearing offline bit for auth */
2830 ctx->ctrl &= ~WINBIND_CACHED_LOGIN;
2833 * First get the name of a user
2835 ret = pam_get_user(pamh, &user, "Username: ");
2837 _pam_log(ctx, LOG_ERR,
2838 "password - could not identify user");
2843 _pam_log(ctx, LOG_ERR, "username was NULL!");
2844 ret = PAM_USER_UNKNOWN;
2848 _pam_log_debug(ctx, LOG_DEBUG, "username [%s] obtained", user);
2850 /* check if this is really a user in winbindd, not only in NSS */
2851 ret = valid_user(ctx, user);
2854 ret = PAM_USER_UNKNOWN;
2857 ret = PAM_SYSTEM_ERR;
2864 * obtain and verify the current password (OLDAUTHTOK) for
2868 if (flags & PAM_PRELIM_CHECK) {
2869 time_t pwdlastset_prelim = 0;
2871 /* instruct user what is happening */
2873 #define greeting "Changing password for"
2874 Announce = talloc_asprintf(ctx, "%s %s", greeting, user);
2876 _pam_log(ctx, LOG_CRIT,
2877 "password - out of memory");
2883 lctrl = ctx->ctrl | WINBIND__OLD_PASSWORD;
2884 ret = _winbind_read_password(ctx, lctrl,
2886 "(current) NT password: ",
2888 (const char **) &pass_old);
2889 TALLOC_FREE(Announce);
2890 if (ret != PAM_SUCCESS) {
2891 _pam_log(ctx, LOG_NOTICE,
2892 "password - (old) token not obtained");
2896 /* verify that this is the password for this user */
2898 ret = winbind_auth_request(ctx, user, pass_old,
2901 &pwdlastset_prelim, NULL);
2903 if (ret != PAM_ACCT_EXPIRED &&
2904 ret != PAM_AUTHTOK_EXPIRED &&
2905 ret != PAM_NEW_AUTHTOK_REQD &&
2906 ret != PAM_SUCCESS) {
2911 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET,
2912 (void *)pwdlastset_prelim, NULL);
2914 ret = pam_set_item(pamh, PAM_OLDAUTHTOK,
2915 (const void *) pass_old);
2917 if (ret != PAM_SUCCESS) {
2918 _pam_log(ctx, LOG_CRIT,
2919 "failed to set PAM_OLDAUTHTOK");
2921 } else if (flags & PAM_UPDATE_AUTHTOK) {
2923 time_t pwdlastset_update = 0;
2926 * obtain the proposed password
2930 * get the old token back.
2933 ret = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
2935 if (ret != PAM_SUCCESS) {
2936 _pam_log(ctx, LOG_NOTICE,
2937 "user not authenticated");
2941 lctrl = ctx->ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
2943 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
2944 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
2947 ret = PAM_AUTHTOK_ERR;
2948 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
2950 * use_authtok is to force the use of a previously entered
2951 * password -- needed for pluggable password strength checking
2954 ret = _winbind_read_password(ctx, lctrl,
2956 "Enter new NT password: ",
2957 "Retype new NT password: ",
2958 (const char **)&pass_new);
2960 if (ret != PAM_SUCCESS) {
2961 _pam_log_debug(ctx, LOG_ALERT,
2963 "new password not obtained");
2964 pass_old = NULL;/* tidy up */
2969 * At this point we know who the user is and what they
2970 * propose as their new password. Verify that the new
2971 * password is acceptable.
2974 if (pass_new[0] == '\0') {/* "\0" password = NULL */
2980 * By reaching here we have approved the passwords and must now
2981 * rebuild the password database file.
2983 _pam_get_data(pamh, PAM_WINBIND_PWD_LAST_SET,
2984 &pwdlastset_update);
2987 * if cached creds were enabled, make sure to set the
2988 * WINBIND_CACHED_LOGIN bit here in order to have winbindd
2989 * update the cached creds storage - gd
2992 ctx->ctrl |= WINBIND_CACHED_LOGIN;
2995 ret = winbind_chauthtok_request(ctx, user, pass_old,
2996 pass_new, pwdlastset_update);
2998 _pam_overwrite(pass_new);
2999 _pam_overwrite(pass_old);
3000 pass_old = pass_new = NULL;
3004 if (_pam_require_krb5_auth_after_chauthtok(ctx, user)) {
3006 const char *member = NULL;
3007 const char *cctype = NULL;
3008 int warn_pwd_expire;
3009 struct wbcLogonUserInfo *info = NULL;
3010 struct wbcUserPasswordPolicyInfo *policy = NULL;
3012 member = get_member_from_config(ctx);
3013 cctype = get_krb5_cc_type_from_config(ctx);
3014 warn_pwd_expire = get_warn_pwd_expire_from_config(ctx);
3016 /* Keep WINBIND_CACHED_LOGIN bit for
3017 * authentication after changing the password.
3018 * This will update the cached credentials in case
3019 * that winbindd_dual_pam_chauthtok() fails
3024 ret = winbind_auth_request(ctx, user, pass_new,
3026 &error, &info, &policy,
3027 NULL, &username_ret);
3028 _pam_overwrite(pass_new);
3029 _pam_overwrite(pass_old);
3030 pass_old = pass_new = NULL;
3032 if (ret == PAM_SUCCESS) {
3034 struct wbcAuthUserInfo *user_info = NULL;
3036 if (info && info->info) {
3037 user_info = info->info;
3040 /* warn a user if the password is about to
3042 _pam_warn_password_expiry(ctx, user_info, policy,
3046 /* set some info3 info for other modules in the
3048 _pam_set_data_info3(ctx, user_info);
3050 /* put krb5ccname into env */
3051 _pam_setup_krb5_env(ctx, info);
3054 pam_set_item(pamh, PAM_USER,
3056 _pam_log_debug(ctx, LOG_INFO,
3057 "Returned user was '%s'",
3062 wbcFreeMemory(info);
3063 wbcFreeMemory(policy);
3069 ret = PAM_SERVICE_ERR;
3074 /* Deal with offline errors. */
3076 const char *codes[] = {
3077 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
3078 "NT_STATUS_NO_LOGON_SERVERS",
3079 "NT_STATUS_ACCESS_DENIED"
3082 for (i=0; i<ARRAY_SIZE(codes); i++) {
3084 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
3090 wbcFreeMemory(error);
3092 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", ctx, ret);
3101 /* static module data */
3103 struct pam_module _pam_winbind_modstruct = {
3105 pam_sm_authenticate,
3108 pam_sm_open_session,
3109 pam_sm_close_session,
3116 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
3117 * Copyright (c) Tim Potter <tpot@samba.org> 2000
3118 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
3119 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2008
3120 * Copyright (c) Jan Rêkorajski 1999.
3121 * Copyright (c) Andrew G. Morgan 1996-8.
3122 * Copyright (c) Alex O. Yuriev, 1996.
3123 * Copyright (c) Cristian Gafton 1996.
3124 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
3126 * Redistribution and use in source and binary forms, with or without
3127 * modification, are permitted provided that the following conditions
3129 * 1. Redistributions of source code must retain the above copyright
3130 * notice, and the entire permission notice in its entirety,
3131 * including the disclaimer of warranties.
3132 * 2. Redistributions in binary form must reproduce the above copyright
3133 * notice, this list of conditions and the following disclaimer in the
3134 * documentation and/or other materials provided with the distribution.
3135 * 3. The name of the author may not be used to endorse or promote
3136 * products derived from this software without specific prior
3137 * written permission.
3139 * ALTERNATIVELY, this product may be distributed under the terms of
3140 * the GNU Public License, in which case the provisions of the GPL are
3141 * required INSTEAD OF the above restrictions. (This clause is
3142 * necessary due to a potential bad interaction between the GPL and
3143 * the restrictions contained in a BSD-style copyright.)
3145 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
3146 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3147 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3148 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3149 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3150 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3151 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3152 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3153 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3154 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3155 * OF THE POSSIBILITY OF SUCH DAMAGE.