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;
1619 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1625 if (!WBC_ERROR_IS_OK(wbc_status)) {
1630 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1636 if (!WBC_ERROR_IS_OK(wbc_status)) {
1640 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1644 (uint8_t *)&user_uid,
1646 if (!WBC_ERROR_IS_OK(wbc_status)) {
1651 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1655 (uint8_t *)membership_of,
1656 sizeof(membership_of));
1657 if (!WBC_ERROR_IS_OK(wbc_status)) {
1662 wbc_status = wbcLogonUser(&logon, &info, &error, &policy);
1663 ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
1664 user, "wbcLogonUser");
1665 wbcFreeMemory(logon.blobs);
1668 if (info && info->info) {
1669 user_info = info->info;
1672 if (pwd_last_set && user_info) {
1673 *pwd_last_set = user_info->pass_last_set_time;
1676 if (p_info && info) {
1680 if (p_policy && policy) {
1684 if (p_error && error) {
1685 /* We want to process the error in the caller. */
1690 for (i=0; i<ARRAY_SIZE(codes); i++) {
1692 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
1698 if ((ret == PAM_SUCCESS) && user_info && policy && info) {
1700 bool already_expired = false;
1702 /* warn a user if the password is about to expire soon */
1703 _pam_warn_password_expiry(ctx, user_info, policy,
1707 if (already_expired == true) {
1709 SMB_TIME_T last_set = user_info->pass_last_set_time;
1711 _pam_log_debug(ctx, LOG_DEBUG,
1712 "Password has expired "
1713 "(Password was last set: %lld, "
1714 "the policy says it should expire here "
1715 "%lld (now it's: %lu))\n",
1716 (long long int)last_set,
1717 (long long int)last_set +
1721 return PAM_AUTHTOK_EXPIRED;
1724 /* inform about logon type */
1725 _pam_warn_logon_type(ctx, user, user_info->user_flags);
1727 /* inform about krb5 failures */
1728 _pam_warn_krb5_failure(ctx, user, user_info->user_flags);
1730 /* set some info3 info for other modules in the stack */
1731 _pam_set_data_info3(ctx, user_info);
1733 /* put krb5ccname into env */
1734 _pam_setup_krb5_env(ctx, info);
1736 /* If winbindd returned a username, return the pointer to it
1738 _pam_setup_unix_username(ctx, user_ret, info);
1743 wbcFreeMemory(logon.blobs);
1745 if (info && info->blobs) {
1746 wbcFreeMemory(info->blobs);
1748 if (error && !p_error) {
1749 wbcFreeMemory(error);
1751 if (info && !p_info) {
1752 wbcFreeMemory(info);
1754 if (policy && !p_policy) {
1755 wbcFreeMemory(policy);
1761 /* talk to winbindd */
1762 static int winbind_chauthtok_request(struct pwb_context *ctx,
1764 const char *oldpass,
1765 const char *newpass,
1766 time_t pwd_last_set)
1769 struct wbcChangePasswordParams params;
1770 struct wbcAuthErrorInfo *error = NULL;
1771 struct wbcUserPasswordPolicyInfo *policy = NULL;
1772 enum wbcPasswordChangeRejectReason reject_reason = -1;
1776 const char *codes[] = {
1777 "NT_STATUS_BACKUP_CONTROLLER",
1778 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1779 "NT_STATUS_NO_LOGON_SERVERS",
1780 "NT_STATUS_ACCESS_DENIED",
1781 "NT_STATUS_PWD_TOO_SHORT", /* TODO: tell the min pwd length ? */
1782 "NT_STATUS_PWD_TOO_RECENT", /* TODO: tell the minage ? */
1783 "NT_STATUS_PWD_HISTORY_CONFLICT" /* TODO: tell the history length ? */
1785 int ret = PAM_AUTH_ERR;
1787 ZERO_STRUCT(params);
1789 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1790 flags |= WBFLAG_PAM_KRB5 |
1791 WBFLAG_PAM_CONTACT_TRUSTDOM;
1794 if (ctx->ctrl & WINBIND_CACHED_LOGIN) {
1795 flags |= WBFLAG_PAM_CACHED_LOGIN;
1798 params.account_name = user;
1799 params.level = WBC_AUTH_USER_LEVEL_PLAIN;
1800 params.old_password.plaintext = oldpass;
1801 params.new_password.plaintext = newpass;
1802 params.flags = flags;
1804 wbc_status = wbcChangeUserPasswordEx(¶ms, &error, &reject_reason, &policy);
1805 ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
1806 user, "wbcChangeUserPasswordEx");
1808 if (WBC_ERROR_IS_OK(wbc_status)) {
1809 _pam_log(ctx, LOG_NOTICE,
1810 "user '%s' password changed", user);
1815 wbcFreeMemory(policy);
1819 for (i=0; i<ARRAY_SIZE(codes); i++) {
1821 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
1827 if (!strcasecmp(error->nt_string,
1828 "NT_STATUS_PASSWORD_RESTRICTION")) {
1830 char *pwd_restriction_string = NULL;
1831 SMB_TIME_T min_pwd_age = 0;
1834 min_pwd_age = policy->min_passwordage;
1837 /* FIXME: avoid to send multiple PAM messages after another */
1838 switch (reject_reason) {
1841 case WBC_PWD_CHANGE_REJECT_OTHER:
1842 if ((min_pwd_age > 0) &&
1843 (pwd_last_set + min_pwd_age > time(NULL))) {
1844 PAM_WB_REMARK_DIRECT(ctx,
1845 "NT_STATUS_PWD_TOO_RECENT");
1848 case WBC_PWD_CHANGE_REJECT_TOO_SHORT:
1849 PAM_WB_REMARK_DIRECT(ctx,
1850 "NT_STATUS_PWD_TOO_SHORT");
1852 case WBC_PWD_CHANGE_REJECT_IN_HISTORY:
1853 PAM_WB_REMARK_DIRECT(ctx,
1854 "NT_STATUS_PWD_HISTORY_CONFLICT");
1856 case WBC_PWD_CHANGE_REJECT_COMPLEXITY:
1857 _make_remark(ctx, PAM_ERROR_MSG,
1858 "Password does not meet "
1859 "complexity requirements");
1862 _pam_log_debug(ctx, LOG_DEBUG,
1863 "unknown password change "
1864 "reject reason: %d",
1869 pwd_restriction_string =
1870 _pam_compose_pwd_restriction_string(ctx, policy);
1871 if (pwd_restriction_string) {
1872 _make_remark(ctx, PAM_ERROR_MSG,
1873 pwd_restriction_string);
1874 TALLOC_FREE(pwd_restriction_string);
1878 wbcFreeMemory(error);
1879 wbcFreeMemory(policy);
1885 * Checks if a user has an account
1888 * 1 = User not found
1892 static int valid_user(struct pwb_context *ctx,
1895 /* check not only if the user is available over NSS calls, also make
1896 * sure it's really a winbind user, this is important when stacking PAM
1897 * modules in the 'account' or 'password' facility. */
1900 struct passwd *pwd = NULL;
1901 struct passwd *wb_pwd = NULL;
1903 pwd = getpwnam(user);
1908 wbc_status = wbcGetpwnam(user, &wb_pwd);
1909 wbcFreeMemory(wb_pwd);
1910 if (!WBC_ERROR_IS_OK(wbc_status)) {
1911 _pam_log(ctx, LOG_DEBUG, "valid_user: wbcGetpwnam gave %s\n",
1912 wbcErrorString(wbc_status));
1915 switch (wbc_status) {
1916 case WBC_ERR_UNKNOWN_USER:
1918 case WBC_ERR_SUCCESS:
1926 static char *_pam_delete(register char *xx)
1934 * obtain a password from the user
1937 static int _winbind_read_password(struct pwb_context *ctx,
1939 const char *comment,
1940 const char *prompt1,
1941 const char *prompt2,
1949 _pam_log(ctx, LOG_DEBUG, "getting password (0x%08x)", ctrl);
1952 * make sure nothing inappropriate gets returned
1955 *pass = token = NULL;
1958 * which authentication token are we getting?
1961 if (on(WINBIND__OLD_PASSWORD, ctrl)) {
1962 authtok_flag = PAM_OLDAUTHTOK;
1964 authtok_flag = PAM_AUTHTOK;
1968 * should we obtain the password from a PAM item ?
1971 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) ||
1972 on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1973 retval = _pam_get_item(ctx->pamh, authtok_flag, &item);
1974 if (retval != PAM_SUCCESS) {
1976 _pam_log(ctx, LOG_ALERT,
1977 "pam_get_item returned error "
1978 "to unix-read-password");
1980 } else if (item != NULL) { /* we have a password! */
1983 _pam_log(ctx, LOG_DEBUG,
1984 "pam_get_item returned a password");
1986 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1987 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
1988 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
1989 && off(WINBIND__OLD_PASSWORD, ctrl)) {
1990 return PAM_AUTHTOK_RECOVER_ERR;
1994 * getting here implies we will have to get the password from the
1999 struct pam_message msg[3], *pmsg[3];
2000 struct pam_response *resp;
2003 /* prepare to converse */
2005 if (comment != NULL && off(ctrl, WINBIND_SILENT)) {
2007 msg[0].msg_style = PAM_TEXT_INFO;
2008 msg[0].msg = discard_const_p(char, comment);
2015 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
2016 msg[i++].msg = discard_const_p(char, prompt1);
2019 if (prompt2 != NULL) {
2021 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
2022 msg[i++].msg = discard_const_p(char, prompt2);
2025 /* so call the conversation expecting i responses */
2027 retval = converse(ctx->pamh, i, pmsg, &resp);
2029 if (retval == PAM_SUCCESS) {
2030 retval = PAM_AUTHTOK_RECOVER_ERR;
2034 if (retval != PAM_SUCCESS) {
2035 _pam_drop_reply(resp, i);
2039 /* interpret the response */
2041 token = x_strdup(resp[i - replies].resp);
2043 _pam_log(ctx, LOG_NOTICE,
2044 "could not recover "
2045 "authentication token");
2046 retval = PAM_AUTHTOK_RECOVER_ERR;
2051 /* verify that password entered correctly */
2052 if (!resp[i - 1].resp ||
2053 strcmp(token, resp[i - 1].resp)) {
2054 _pam_delete(token); /* mistyped */
2055 retval = PAM_AUTHTOK_RECOVER_ERR;
2056 _make_remark(ctx, PAM_ERROR_MSG,
2062 * tidy up the conversation (resp_retcode) is ignored
2063 * -- what is it for anyway? AGM
2065 _pam_drop_reply(resp, i);
2069 if (retval != PAM_SUCCESS) {
2070 _pam_log_debug(ctx, LOG_DEBUG,
2071 "unable to obtain a password");
2074 /* 'token' is the entered password */
2076 /* we store this password as an item */
2078 retval = pam_set_item(ctx->pamh, authtok_flag, token);
2079 _pam_delete(token); /* clean it up */
2080 if (retval != PAM_SUCCESS ||
2081 (retval = _pam_get_item(ctx->pamh, authtok_flag, &item)) != PAM_SUCCESS) {
2083 _pam_log(ctx, LOG_CRIT, "error manipulating password");
2089 item = NULL; /* break link to password */
2094 static const char *get_conf_item_string(struct pwb_context *ctx,
2099 const char *parm_opt = NULL;
2101 if (!(ctx->ctrl & config_flag)) {
2105 /* let the pam opt take precedence over the pam_winbind.conf option */
2106 for (i=0; i<ctx->argc; i++) {
2108 if ((strncmp(ctx->argv[i], item, strlen(item)) == 0)) {
2111 if ((p = strchr(ctx->argv[i], '=')) == NULL) {
2112 _pam_log(ctx, LOG_INFO,
2113 "no \"=\" delimiter for \"%s\" found\n",
2117 _pam_log_debug(ctx, LOG_INFO,
2118 "PAM config: %s '%s'\n", item, p+1);
2126 key = talloc_asprintf(ctx, "global:%s", item);
2131 parm_opt = iniparser_getstr(ctx->dict, key);
2134 _pam_log_debug(ctx, LOG_INFO, "CONFIG file: %s '%s'\n",
2141 static int get_config_item_int(struct pwb_context *ctx,
2145 int i, parm_opt = -1;
2147 if (!(ctx->ctrl & config_flag)) {
2151 /* let the pam opt take precedence over the pam_winbind.conf option */
2152 for (i = 0; i < ctx->argc; i++) {
2154 if ((strncmp(ctx->argv[i], item, strlen(item)) == 0)) {
2157 if ((p = strchr(ctx->argv[i], '=')) == NULL) {
2158 _pam_log(ctx, LOG_INFO,
2159 "no \"=\" delimiter for \"%s\" found\n",
2163 parm_opt = atoi(p + 1);
2164 _pam_log_debug(ctx, LOG_INFO,
2165 "PAM config: %s '%d'\n",
2174 key = talloc_asprintf(ctx, "global:%s", item);
2179 parm_opt = iniparser_getint(ctx->dict, key, -1);
2182 _pam_log_debug(ctx, LOG_INFO,
2183 "CONFIG file: %s '%d'\n",
2190 static const char *get_krb5_cc_type_from_config(struct pwb_context *ctx)
2192 return get_conf_item_string(ctx, "krb5_ccache_type",
2193 WINBIND_KRB5_CCACHE_TYPE);
2196 static const char *get_member_from_config(struct pwb_context *ctx)
2198 const char *ret = NULL;
2199 ret = get_conf_item_string(ctx, "require_membership_of",
2200 WINBIND_REQUIRED_MEMBERSHIP);
2204 return get_conf_item_string(ctx, "require-membership-of",
2205 WINBIND_REQUIRED_MEMBERSHIP);
2208 static int get_warn_pwd_expire_from_config(struct pwb_context *ctx)
2211 ret = get_config_item_int(ctx, "warn_pwd_expire",
2212 WINBIND_WARN_PWD_EXPIRE);
2213 /* no or broken setting */
2215 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES;
2221 * Retrieve the winbind separator.
2223 * @param ctx PAM winbind context.
2225 * @return string separator character. NULL on failure.
2228 static char winbind_get_separator(struct pwb_context *ctx)
2231 static struct wbcInterfaceDetails *details = NULL;
2233 wbc_status = wbcInterfaceDetails(&details);
2234 if (!WBC_ERROR_IS_OK(wbc_status)) {
2235 _pam_log(ctx, LOG_ERR,
2236 "Could not retrieve winbind interface details: %s",
2237 wbcErrorString(wbc_status));
2245 return details->winbind_separator;
2250 * Convert a upn to a name.
2252 * @param ctx PAM winbind context.
2253 * @param upn USer UPN to be trabslated.
2255 * @return converted name. NULL pointer on failure. Caller needs to free.
2258 static char* winbind_upn_to_username(struct pwb_context *ctx,
2262 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
2263 struct wbcDomainSid sid;
2264 enum wbcSidType type;
2268 /* This cannot work when the winbind separator = @ */
2270 sep = winbind_get_separator(ctx);
2271 if (!sep || sep == '@') {
2275 /* Convert the UPN to a SID */
2277 wbc_status = wbcLookupName("", upn, &sid, &type);
2278 if (!WBC_ERROR_IS_OK(wbc_status)) {
2282 /* Convert the the SID back to the sAMAccountName */
2284 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
2285 if (!WBC_ERROR_IS_OK(wbc_status)) {
2289 return talloc_asprintf(ctx, "%s\\%s", domain, name);
2292 static int _pam_delete_cred(pam_handle_t *pamh, int flags,
2293 int argc, const char **argv)
2295 int retval = PAM_SUCCESS;
2296 struct pwb_context *ctx = NULL;
2297 struct wbcLogoffUserParams logoff;
2298 struct wbcAuthErrorInfo *error = NULL;
2302 retval = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2307 _PAM_LOG_FUNCTION_ENTER("_pam_delete_cred", ctx);
2309 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
2311 /* destroy the ccache here */
2313 uint32_t wbc_flags = 0;
2314 const char *ccname = NULL;
2315 struct passwd *pwd = NULL;
2317 retval = pam_get_user(pamh, &user, "Username: ");
2319 _pam_log(ctx, LOG_ERR,
2320 "could not identify user");
2325 _pam_log(ctx, LOG_ERR,
2326 "username was NULL!");
2327 retval = PAM_USER_UNKNOWN;
2331 _pam_log_debug(ctx, LOG_DEBUG,
2332 "username [%s] obtained", user);
2334 ccname = pam_getenv(pamh, "KRB5CCNAME");
2335 if (ccname == NULL) {
2336 _pam_log_debug(ctx, LOG_DEBUG,
2337 "user has no KRB5CCNAME environment");
2340 pwd = getpwnam(user);
2342 retval = PAM_USER_UNKNOWN;
2346 wbc_flags = WBFLAG_PAM_KRB5 |
2347 WBFLAG_PAM_CONTACT_TRUSTDOM;
2349 ZERO_STRUCT(logoff);
2351 logoff.username = user;
2353 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2359 if (!WBC_ERROR_IS_OK(wbc_status)) {
2363 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2367 (uint8_t *)&wbc_flags,
2369 if (!WBC_ERROR_IS_OK(wbc_status)) {
2373 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2377 (uint8_t *)&pwd->pw_uid,
2378 sizeof(pwd->pw_uid));
2379 if (!WBC_ERROR_IS_OK(wbc_status)) {
2383 wbc_status = wbcLogoffUserEx(&logoff, &error);
2384 retval = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
2385 user, "wbcLogoffUser");
2386 wbcFreeMemory(error);
2387 wbcFreeMemory(logoff.blobs);
2389 if (!WBC_ERROR_IS_OK(wbc_status)) {
2390 _pam_log(ctx, LOG_INFO,
2391 "failed to logoff user %s: %s\n",
2392 user, wbcErrorString(wbc_status));
2398 wbcFreeMemory(logoff.blobs);
2401 if (!WBC_ERROR_IS_OK(wbc_status)) {
2402 retval = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
2403 user, "wbcLogoffUser");
2407 * Delete the krb5 ccname variable from the PAM environment
2408 * if it was set by winbind.
2410 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
2411 pam_putenv(pamh, "KRB5CCNAME");
2414 _PAM_LOG_FUNCTION_LEAVE("_pam_delete_cred", ctx, retval);
2422 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
2423 int argc, const char **argv)
2425 const char *username;
2426 const char *password;
2427 const char *member = NULL;
2428 const char *cctype = NULL;
2429 int warn_pwd_expire;
2430 int retval = PAM_AUTH_ERR;
2431 char *username_ret = NULL;
2432 char *new_authtok_required = NULL;
2433 char *real_username = NULL;
2434 struct pwb_context *ctx = NULL;
2436 retval = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2441 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", ctx);
2443 /* Get the username */
2444 retval = pam_get_user(pamh, &username, NULL);
2445 if ((retval != PAM_SUCCESS) || (!username)) {
2446 _pam_log_debug(ctx, LOG_DEBUG,
2447 "can not get the username");
2448 retval = PAM_SERVICE_ERR;
2454 /* Decode the user name since AIX does not support logn user
2455 names by default. The name is encoded as _#uid. */
2457 if (username[0] == '_') {
2458 uid_t id = atoi(&username[1]);
2459 struct passwd *pw = NULL;
2461 if ((id!=0) && ((pw = getpwuid(id)) != NULL)) {
2462 real_username = strdup(pw->pw_name);
2467 if (!real_username) {
2468 /* Just making a copy of the username we got from PAM */
2469 if ((real_username = strdup(username)) == NULL) {
2470 _pam_log_debug(ctx, LOG_DEBUG,
2471 "memory allocation failure when copying "
2473 retval = PAM_SERVICE_ERR;
2478 /* Maybe this was a UPN */
2480 if (strchr(real_username, '@') != NULL) {
2481 char *samaccountname = NULL;
2483 samaccountname = winbind_upn_to_username(ctx,
2485 if (samaccountname) {
2486 free(real_username);
2487 real_username = strdup(samaccountname);
2491 retval = _winbind_read_password(ctx, ctx->ctrl, NULL,
2495 if (retval != PAM_SUCCESS) {
2496 _pam_log(ctx, LOG_ERR,
2497 "Could not retrieve user's password");
2498 retval = PAM_AUTHTOK_ERR;
2502 /* Let's not give too much away in the log file */
2504 #ifdef DEBUG_PASSWORD
2505 _pam_log_debug(ctx, LOG_INFO,
2506 "Verify user '%s' with password '%s'",
2507 real_username, password);
2509 _pam_log_debug(ctx, LOG_INFO,
2510 "Verify user '%s'", real_username);
2513 member = get_member_from_config(ctx);
2514 cctype = get_krb5_cc_type_from_config(ctx);
2515 warn_pwd_expire = get_warn_pwd_expire_from_config(ctx);
2517 /* Now use the username to look up password */
2518 retval = winbind_auth_request(ctx, real_username, password,
2519 member, cctype, warn_pwd_expire,
2521 NULL, &username_ret);
2523 if (retval == PAM_NEW_AUTHTOK_REQD ||
2524 retval == PAM_AUTHTOK_EXPIRED) {
2526 char *new_authtok_required_during_auth = NULL;
2528 new_authtok_required = talloc_asprintf(NULL, "%d", retval);
2529 if (!new_authtok_required) {
2530 retval = PAM_BUF_ERR;
2534 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2535 new_authtok_required,
2536 _pam_winbind_cleanup_func);
2538 retval = PAM_SUCCESS;
2540 new_authtok_required_during_auth = talloc_asprintf(NULL, "%d", true);
2541 if (!new_authtok_required_during_auth) {
2542 retval = PAM_BUF_ERR;
2546 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2547 new_authtok_required_during_auth,
2548 _pam_winbind_cleanup_func);
2555 pam_set_item (pamh, PAM_USER, username_ret);
2556 _pam_log_debug(ctx, LOG_INFO,
2557 "Returned user was '%s'", username_ret);
2561 if (real_username) {
2562 free(real_username);
2565 if (!new_authtok_required) {
2566 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
2569 if (retval != PAM_SUCCESS) {
2570 _pam_free_data_info3(pamh);
2573 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", ctx, retval);
2581 int pam_sm_setcred(pam_handle_t *pamh, int flags,
2582 int argc, const char **argv)
2584 int ret = PAM_SYSTEM_ERR;
2585 struct pwb_context *ctx = NULL;
2587 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2592 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", ctx);
2594 switch (flags & ~PAM_SILENT) {
2596 case PAM_DELETE_CRED:
2597 ret = _pam_delete_cred(pamh, flags, argc, argv);
2599 case PAM_REFRESH_CRED:
2600 _pam_log_debug(ctx, LOG_WARNING,
2601 "PAM_REFRESH_CRED not implemented");
2604 case PAM_REINITIALIZE_CRED:
2605 _pam_log_debug(ctx, LOG_WARNING,
2606 "PAM_REINITIALIZE_CRED not implemented");
2609 case PAM_ESTABLISH_CRED:
2610 _pam_log_debug(ctx, LOG_WARNING,
2611 "PAM_ESTABLISH_CRED not implemented");
2615 ret = PAM_SYSTEM_ERR;
2621 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", ctx, ret);
2629 * Account management. We want to verify that the account exists
2630 * before returning PAM_SUCCESS
2633 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
2634 int argc, const char **argv)
2636 const char *username;
2637 int ret = PAM_USER_UNKNOWN;
2639 struct pwb_context *ctx = NULL;
2641 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2646 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", ctx);
2649 /* Get the username */
2650 ret = pam_get_user(pamh, &username, NULL);
2651 if ((ret != PAM_SUCCESS) || (!username)) {
2652 _pam_log_debug(ctx, LOG_DEBUG,
2653 "can not get the username");
2654 ret = PAM_SERVICE_ERR;
2658 /* Verify the username */
2659 ret = valid_user(ctx, username);
2662 /* some sort of system error. The log was already printed */
2663 ret = PAM_SERVICE_ERR;
2666 /* the user does not exist */
2667 _pam_log_debug(ctx, LOG_NOTICE, "user '%s' not found",
2669 if (ctx->ctrl & WINBIND_UNKNOWN_OK_ARG) {
2673 ret = PAM_USER_UNKNOWN;
2676 pam_get_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2677 (const void **)&tmp);
2679 ret = atoi((const char *)tmp);
2681 case PAM_AUTHTOK_EXPIRED:
2682 /* fall through, since new token is required in this case */
2683 case PAM_NEW_AUTHTOK_REQD:
2684 _pam_log(ctx, LOG_WARNING,
2685 "pam_sm_acct_mgmt success but %s is set",
2686 PAM_WINBIND_NEW_AUTHTOK_REQD);
2687 _pam_log(ctx, LOG_NOTICE,
2688 "user '%s' needs new password",
2690 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2691 ret = PAM_NEW_AUTHTOK_REQD;
2694 _pam_log(ctx, LOG_WARNING,
2695 "pam_sm_acct_mgmt success");
2696 _pam_log(ctx, LOG_NOTICE,
2697 "user '%s' granted access", username);
2703 /* Otherwise, the authentication looked good */
2704 _pam_log(ctx, LOG_NOTICE,
2705 "user '%s' granted access", username);
2709 /* we don't know anything about this return value */
2710 _pam_log(ctx, LOG_ERR,
2711 "internal module error (ret = %d, user = '%s')",
2713 ret = PAM_SERVICE_ERR;
2717 /* should not be reached */
2722 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", ctx, ret);
2730 int pam_sm_open_session(pam_handle_t *pamh, int flags,
2731 int argc, const char **argv)
2733 int ret = PAM_SUCCESS;
2734 struct pwb_context *ctx = NULL;
2736 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2741 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", ctx);
2743 if (ctx->ctrl & WINBIND_MKHOMEDIR) {
2744 /* check and create homedir */
2745 ret = _pam_mkhomedir(ctx);
2748 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", ctx, ret);
2756 int pam_sm_close_session(pam_handle_t *pamh, int flags,
2757 int argc, const char **argv)
2759 int ret = PAM_SUCCESS;
2760 struct pwb_context *ctx = NULL;
2762 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2767 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", ctx);
2770 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", ctx, ret);
2778 * evaluate whether we need to re-authenticate with kerberos after a
2781 * @param ctx PAM winbind context.
2782 * @param user The username
2784 * @return boolean Returns true if required, false if not.
2787 static bool _pam_require_krb5_auth_after_chauthtok(struct pwb_context *ctx,
2791 /* Make sure that we only do this if a) the chauthtok got initiated
2792 * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2793 * later password change via the "passwd" command if done by the user
2795 * NB. If we login from gdm or xdm and the password expires,
2796 * we change the password, but there is no memory cache.
2797 * Thus, even for passthrough login, we should do the
2798 * authentication again to update memory cache.
2802 char *new_authtok_reqd_during_auth = NULL;
2803 struct passwd *pwd = NULL;
2805 _pam_get_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2806 &new_authtok_reqd_during_auth);
2807 pam_set_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2810 if (new_authtok_reqd_during_auth) {
2814 pwd = getpwnam(user);
2819 if (getuid() == pwd->pw_uid) {
2828 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
2829 int argc, const char **argv)
2833 bool cached_login = false;
2835 /* <DO NOT free() THESE> */
2837 char *pass_old, *pass_new;
2838 /* </DO NOT free() THESE> */
2843 char *username_ret = NULL;
2844 struct wbcAuthErrorInfo *error = NULL;
2845 struct pwb_context *ctx = NULL;
2847 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2852 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", ctx);
2854 cached_login = (ctx->ctrl & WINBIND_CACHED_LOGIN);
2856 /* clearing offline bit for auth */
2857 ctx->ctrl &= ~WINBIND_CACHED_LOGIN;
2860 * First get the name of a user
2862 ret = pam_get_user(pamh, &user, "Username: ");
2864 _pam_log(ctx, LOG_ERR,
2865 "password - could not identify user");
2870 _pam_log(ctx, LOG_ERR, "username was NULL!");
2871 ret = PAM_USER_UNKNOWN;
2875 _pam_log_debug(ctx, LOG_DEBUG, "username [%s] obtained", user);
2877 /* check if this is really a user in winbindd, not only in NSS */
2878 ret = valid_user(ctx, user);
2881 ret = PAM_USER_UNKNOWN;
2884 ret = PAM_SYSTEM_ERR;
2891 * obtain and verify the current password (OLDAUTHTOK) for
2895 if (flags & PAM_PRELIM_CHECK) {
2896 time_t pwdlastset_prelim = 0;
2898 /* instruct user what is happening */
2900 #define greeting "Changing password for"
2901 Announce = talloc_asprintf(ctx, "%s %s", greeting, user);
2903 _pam_log(ctx, LOG_CRIT,
2904 "password - out of memory");
2910 lctrl = ctx->ctrl | WINBIND__OLD_PASSWORD;
2911 ret = _winbind_read_password(ctx, lctrl,
2913 "(current) NT password: ",
2915 (const char **) &pass_old);
2916 TALLOC_FREE(Announce);
2917 if (ret != PAM_SUCCESS) {
2918 _pam_log(ctx, LOG_NOTICE,
2919 "password - (old) token not obtained");
2923 /* verify that this is the password for this user */
2925 ret = winbind_auth_request(ctx, user, pass_old,
2928 &pwdlastset_prelim, NULL);
2930 if (ret != PAM_ACCT_EXPIRED &&
2931 ret != PAM_AUTHTOK_EXPIRED &&
2932 ret != PAM_NEW_AUTHTOK_REQD &&
2933 ret != PAM_SUCCESS) {
2938 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET,
2939 (void *)pwdlastset_prelim, NULL);
2941 ret = pam_set_item(pamh, PAM_OLDAUTHTOK,
2942 (const void *) pass_old);
2944 if (ret != PAM_SUCCESS) {
2945 _pam_log(ctx, LOG_CRIT,
2946 "failed to set PAM_OLDAUTHTOK");
2948 } else if (flags & PAM_UPDATE_AUTHTOK) {
2950 time_t pwdlastset_update = 0;
2953 * obtain the proposed password
2957 * get the old token back.
2960 ret = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
2962 if (ret != PAM_SUCCESS) {
2963 _pam_log(ctx, LOG_NOTICE,
2964 "user not authenticated");
2968 lctrl = ctx->ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
2970 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
2971 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
2974 ret = PAM_AUTHTOK_ERR;
2975 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
2977 * use_authtok is to force the use of a previously entered
2978 * password -- needed for pluggable password strength checking
2981 ret = _winbind_read_password(ctx, lctrl,
2983 "Enter new NT password: ",
2984 "Retype new NT password: ",
2985 (const char **)&pass_new);
2987 if (ret != PAM_SUCCESS) {
2988 _pam_log_debug(ctx, LOG_ALERT,
2990 "new password not obtained");
2991 pass_old = NULL;/* tidy up */
2996 * At this point we know who the user is and what they
2997 * propose as their new password. Verify that the new
2998 * password is acceptable.
3001 if (pass_new[0] == '\0') {/* "\0" password = NULL */
3007 * By reaching here we have approved the passwords and must now
3008 * rebuild the password database file.
3010 _pam_get_data(pamh, PAM_WINBIND_PWD_LAST_SET,
3011 &pwdlastset_update);
3014 * if cached creds were enabled, make sure to set the
3015 * WINBIND_CACHED_LOGIN bit here in order to have winbindd
3016 * update the cached creds storage - gd
3019 ctx->ctrl |= WINBIND_CACHED_LOGIN;
3022 ret = winbind_chauthtok_request(ctx, user, pass_old,
3023 pass_new, pwdlastset_update);
3025 _pam_overwrite(pass_new);
3026 _pam_overwrite(pass_old);
3027 pass_old = pass_new = NULL;
3031 if (_pam_require_krb5_auth_after_chauthtok(ctx, user)) {
3033 const char *member = NULL;
3034 const char *cctype = NULL;
3035 int warn_pwd_expire;
3036 struct wbcLogonUserInfo *info = NULL;
3037 struct wbcUserPasswordPolicyInfo *policy = NULL;
3039 member = get_member_from_config(ctx);
3040 cctype = get_krb5_cc_type_from_config(ctx);
3041 warn_pwd_expire = get_warn_pwd_expire_from_config(ctx);
3043 /* Keep WINBIND_CACHED_LOGIN bit for
3044 * authentication after changing the password.
3045 * This will update the cached credentials in case
3046 * that winbindd_dual_pam_chauthtok() fails
3051 ret = winbind_auth_request(ctx, user, pass_new,
3053 &error, &info, &policy,
3054 NULL, &username_ret);
3055 _pam_overwrite(pass_new);
3056 _pam_overwrite(pass_old);
3057 pass_old = pass_new = NULL;
3059 if (ret == PAM_SUCCESS) {
3061 struct wbcAuthUserInfo *user_info = NULL;
3063 if (info && info->info) {
3064 user_info = info->info;
3067 /* warn a user if the password is about to
3069 _pam_warn_password_expiry(ctx, user_info, policy,
3073 /* set some info3 info for other modules in the
3075 _pam_set_data_info3(ctx, user_info);
3077 /* put krb5ccname into env */
3078 _pam_setup_krb5_env(ctx, info);
3081 pam_set_item(pamh, PAM_USER,
3083 _pam_log_debug(ctx, LOG_INFO,
3084 "Returned user was '%s'",
3089 wbcFreeMemory(info);
3090 wbcFreeMemory(policy);
3096 ret = PAM_SERVICE_ERR;
3101 /* Deal with offline errors. */
3103 const char *codes[] = {
3104 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
3105 "NT_STATUS_NO_LOGON_SERVERS",
3106 "NT_STATUS_ACCESS_DENIED"
3109 for (i=0; i<ARRAY_SIZE(codes); i++) {
3111 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
3117 wbcFreeMemory(error);
3119 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", ctx, ret);
3128 /* static module data */
3130 struct pam_module _pam_winbind_modstruct = {
3132 pam_sm_authenticate,
3135 pam_sm_open_session,
3136 pam_sm_close_session,
3143 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
3144 * Copyright (c) Tim Potter <tpot@samba.org> 2000
3145 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
3146 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2008
3147 * Copyright (c) Jan Rêkorajski 1999.
3148 * Copyright (c) Andrew G. Morgan 1996-8.
3149 * Copyright (c) Alex O. Yuriev, 1996.
3150 * Copyright (c) Cristian Gafton 1996.
3151 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
3153 * Redistribution and use in source and binary forms, with or without
3154 * modification, are permitted provided that the following conditions
3156 * 1. Redistributions of source code must retain the above copyright
3157 * notice, and the entire permission notice in its entirety,
3158 * including the disclaimer of warranties.
3159 * 2. Redistributions in binary form must reproduce the above copyright
3160 * notice, this list of conditions and the following disclaimer in the
3161 * documentation and/or other materials provided with the distribution.
3162 * 3. The name of the author may not be used to endorse or promote
3163 * products derived from this software without specific prior
3164 * written permission.
3166 * ALTERNATIVELY, this product may be distributed under the terms of
3167 * the GNU Public License, in which case the provisions of the GPL are
3168 * required INSTEAD OF the above restrictions. (This clause is
3169 * necessary due to a potential bad interaction between the GPL and
3170 * the restrictions contained in a BSD-style copyright.)
3172 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
3173 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3174 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3175 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3176 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3177 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3178 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3179 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3180 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3181 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3182 * OF THE POSSIBILITY OF SUCH DAMAGE.