3 Copyright Andrew Tridgell <tridge@samba.org> 2000
4 Copyright Tim Potter <tpot@samba.org> 2000
5 Copyright Andrew Bartlett <abartlet@samba.org> 2002
6 Copyright Guenther Deschner <gd@samba.org> 2005-2008
8 largely based on pam_userdb by Cristian Gafton <gafton@redhat.com> also
9 contains large slabs of code from pam_unix by Elliot Lee
10 <sopwith@redhat.com> (see copyright below for full details)
13 #include "pam_winbind.h"
15 static int wbc_error_to_pam_error(wbcErr status)
20 case WBC_ERR_NOT_IMPLEMENTED:
21 return PAM_SERVICE_ERR;
22 case WBC_ERR_UNKNOWN_FAILURE:
24 case WBC_ERR_NO_MEMORY:
26 case WBC_ERR_INVALID_SID:
27 case WBC_ERR_INVALID_PARAM:
29 case WBC_ERR_WINBIND_NOT_AVAILABLE:
30 return PAM_AUTHINFO_UNAVAIL;
31 case WBC_ERR_DOMAIN_NOT_FOUND:
32 return PAM_AUTHINFO_UNAVAIL;
33 case WBC_ERR_INVALID_RESPONSE:
35 case WBC_ERR_NSS_ERROR:
36 return PAM_USER_UNKNOWN;
37 case WBC_ERR_AUTH_ERROR:
39 case WBC_ERR_UNKNOWN_USER:
40 return PAM_USER_UNKNOWN;
41 case WBC_ERR_UNKNOWN_GROUP:
42 return PAM_USER_UNKNOWN;
43 case WBC_ERR_PWD_CHANGE_FAILED:
51 static const char *_pam_error_code_str(int err)
57 return "PAM_OPEN_ERR";
59 return "PAM_SYMBOL_ERR";
61 return "PAM_SERVICE_ERR";
63 return "PAM_SYSTEM_ERR";
67 return "PAM_PERM_DENIED";
69 return "PAM_AUTH_ERR";
70 case PAM_CRED_INSUFFICIENT:
71 return "PAM_CRED_INSUFFICIENT";
72 case PAM_AUTHINFO_UNAVAIL:
73 return "PAM_AUTHINFO_UNAVAIL";
74 case PAM_USER_UNKNOWN:
75 return "PAM_USER_UNKNOWN";
77 return "PAM_MAXTRIES";
78 case PAM_NEW_AUTHTOK_REQD:
79 return "PAM_NEW_AUTHTOK_REQD";
80 case PAM_ACCT_EXPIRED:
81 return "PAM_ACCT_EXPIRED";
83 return "PAM_SESSION_ERR";
84 case PAM_CRED_UNAVAIL:
85 return "PAM_CRED_UNAVAIL";
86 case PAM_CRED_EXPIRED:
87 return "PAM_CRED_EXPIRED";
89 return "PAM_CRED_ERR";
90 case PAM_NO_MODULE_DATA:
91 return "PAM_NO_MODULE_DATA";
93 return "PAM_CONV_ERR";
95 return "PAM_AUTHTOK_ERR";
96 case PAM_AUTHTOK_RECOVERY_ERR:
97 return "PAM_AUTHTOK_RECOVERY_ERR";
98 case PAM_AUTHTOK_LOCK_BUSY:
99 return "PAM_AUTHTOK_LOCK_BUSY";
100 case PAM_AUTHTOK_DISABLE_AGING:
101 return "PAM_AUTHTOK_DISABLE_AGING";
103 return "PAM_TRY_AGAIN";
108 case PAM_AUTHTOK_EXPIRED:
109 return "PAM_AUTHTOK_EXPIRED";
110 #ifdef PAM_MODULE_UNKNOWN
111 case PAM_MODULE_UNKNOWN:
112 return "PAM_MODULE_UNKNOWN";
116 return "PAM_BAD_ITEM";
118 #ifdef PAM_CONV_AGAIN
120 return "PAM_CONV_AGAIN";
122 #ifdef PAM_INCOMPLETE
124 return "PAM_INCOMPLETE";
131 #define _PAM_LOG_FUNCTION_ENTER(function, ctx) \
133 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] ENTER: " \
134 function " (flags: 0x%04x)", ctx->pamh, ctx->flags); \
135 _pam_log_state(ctx); \
138 #define _PAM_LOG_FUNCTION_LEAVE(function, ctx, retval) \
140 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] LEAVE: " \
141 function " returning %d (%s)", ctx->pamh, retval, \
142 _pam_error_code_str(retval)); \
143 _pam_log_state(ctx); \
148 #define MAX_PASSWD_TRIES 3
151 * Work around the pam API that has functions with void ** as parameters
152 * These lead to strict aliasing warnings with gcc.
154 static int _pam_get_item(const pam_handle_t *pamh,
158 const void **item = (const void **)_item;
159 return pam_get_item(pamh, item_type, item);
161 static int _pam_get_data(const pam_handle_t *pamh,
162 const char *module_data_name,
165 const void **data = (const void **)_data;
166 return pam_get_data(pamh, module_data_name, data);
169 /* some syslogging */
171 #ifdef HAVE_PAM_VSYSLOG
172 static void _pam_log_int(const pam_handle_t *pamh,
177 pam_vsyslog(pamh, err, format, args);
180 static void _pam_log_int(const pam_handle_t *pamh,
185 char *format2 = NULL;
188 _pam_get_item(pamh, PAM_SERVICE, &service);
190 format2 = (char *)malloc(strlen(MODULE_NAME)+strlen(format)+strlen(service)+5);
191 if (format2 == NULL) {
192 /* what else todo ? */
193 vsyslog(err, format, args);
197 sprintf(format2, "%s(%s): %s", MODULE_NAME, service, format);
198 vsyslog(err, format2, args);
201 #endif /* HAVE_PAM_VSYSLOG */
203 static bool _pam_log_is_silent(int ctrl)
205 return on(ctrl, WINBIND_SILENT);
208 static void _pam_log(struct pwb_context *r, int err, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
209 static void _pam_log(struct pwb_context *r, int err, const char *format, ...)
213 if (_pam_log_is_silent(r->ctrl)) {
217 va_start(args, format);
218 _pam_log_int(r->pamh, err, format, args);
221 static void __pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
222 static void __pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
226 if (_pam_log_is_silent(ctrl)) {
230 va_start(args, format);
231 _pam_log_int(pamh, err, format, args);
235 static bool _pam_log_is_debug_enabled(int ctrl)
241 if (_pam_log_is_silent(ctrl)) {
245 if (!(ctrl & WINBIND_DEBUG_ARG)) {
252 static bool _pam_log_is_debug_state_enabled(int ctrl)
254 if (!(ctrl & WINBIND_DEBUG_STATE)) {
258 return _pam_log_is_debug_enabled(ctrl);
261 static void _pam_log_debug(struct pwb_context *r, int err, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
262 static void _pam_log_debug(struct pwb_context *r, int err, const char *format, ...)
266 if (!_pam_log_is_debug_enabled(r->ctrl)) {
270 va_start(args, format);
271 _pam_log_int(r->pamh, err, format, args);
274 static void __pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
275 static void __pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
279 if (!_pam_log_is_debug_enabled(ctrl)) {
283 va_start(args, format);
284 _pam_log_int(pamh, err, format, args);
288 static void _pam_log_state_datum(struct pwb_context *ctx,
293 const void *data = NULL;
294 if (item_type != 0) {
295 pam_get_item(ctx->pamh, item_type, &data);
297 pam_get_data(ctx->pamh, key, &data);
300 const char *type = (item_type != 0) ? "ITEM" : "DATA";
301 if (is_string != 0) {
302 _pam_log_debug(ctx, LOG_DEBUG,
303 "[pamh: %p] STATE: %s(%s) = \"%s\" (%p)",
304 ctx->pamh, type, key, (const char *)data,
307 _pam_log_debug(ctx, LOG_DEBUG,
308 "[pamh: %p] STATE: %s(%s) = %p",
309 ctx->pamh, type, key, data);
314 #define _PAM_LOG_STATE_DATA_POINTER(ctx, module_data_name) \
315 _pam_log_state_datum(ctx, 0, module_data_name, 0)
317 #define _PAM_LOG_STATE_DATA_STRING(ctx, module_data_name) \
318 _pam_log_state_datum(ctx, 0, module_data_name, 1)
320 #define _PAM_LOG_STATE_ITEM_POINTER(ctx, item_type) \
321 _pam_log_state_datum(ctx, item_type, #item_type, 0)
323 #define _PAM_LOG_STATE_ITEM_STRING(ctx, item_type) \
324 _pam_log_state_datum(ctx, item_type, #item_type, 1)
326 #ifdef DEBUG_PASSWORD
327 #define _LOG_PASSWORD_AS_STRING 1
329 #define _LOG_PASSWORD_AS_STRING 0
332 #define _PAM_LOG_STATE_ITEM_PASSWORD(ctx, item_type) \
333 _pam_log_state_datum(ctx, item_type, #item_type, \
334 _LOG_PASSWORD_AS_STRING)
336 static void _pam_log_state(struct pwb_context *ctx)
338 if (!_pam_log_is_debug_state_enabled(ctx->ctrl)) {
342 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_SERVICE);
343 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_USER);
344 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_TTY);
345 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_RHOST);
346 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_RUSER);
347 _PAM_LOG_STATE_ITEM_PASSWORD(ctx, PAM_OLDAUTHTOK);
348 _PAM_LOG_STATE_ITEM_PASSWORD(ctx, PAM_AUTHTOK);
349 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_USER_PROMPT);
350 _PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_CONV);
351 #ifdef PAM_FAIL_DELAY
352 _PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_FAIL_DELAY);
354 #ifdef PAM_REPOSITORY
355 _PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_REPOSITORY);
358 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_HOMEDIR);
359 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_LOGONSCRIPT);
360 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_LOGONSERVER);
361 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_PROFILEPATH);
362 _PAM_LOG_STATE_DATA_STRING(ctx,
363 PAM_WINBIND_NEW_AUTHTOK_REQD);
364 /* Use atoi to get PAM result code */
365 _PAM_LOG_STATE_DATA_STRING(ctx,
366 PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH);
367 _PAM_LOG_STATE_DATA_POINTER(ctx, PAM_WINBIND_PWD_LAST_SET);
370 static int _pam_parse(const pam_handle_t *pamh,
374 dictionary **result_d)
377 const char *config_file = NULL;
380 dictionary *d = NULL;
382 if (flags & PAM_SILENT) {
383 ctrl |= WINBIND_SILENT;
386 for (i=argc,v=argv; i-- > 0; ++v) {
387 if (!strncasecmp(*v, "config", strlen("config"))) {
388 ctrl |= WINBIND_CONFIG_FILE;
394 if (config_file == NULL) {
395 config_file = PAM_WINBIND_CONFIG_FILE;
398 d = iniparser_load(config_file);
400 goto config_from_pam;
403 if (iniparser_getboolean(d, "global:debug", false)) {
404 ctrl |= WINBIND_DEBUG_ARG;
407 if (iniparser_getboolean(d, "global:debug_state", false)) {
408 ctrl |= WINBIND_DEBUG_STATE;
411 if (iniparser_getboolean(d, "global:cached_login", false)) {
412 ctrl |= WINBIND_CACHED_LOGIN;
415 if (iniparser_getboolean(d, "global:krb5_auth", false)) {
416 ctrl |= WINBIND_KRB5_AUTH;
419 if (iniparser_getboolean(d, "global:silent", false)) {
420 ctrl |= WINBIND_SILENT;
423 if (iniparser_getstr(d, "global:krb5_ccache_type") != NULL) {
424 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
427 if ((iniparser_getstr(d, "global:require-membership-of") != NULL) ||
428 (iniparser_getstr(d, "global:require_membership_of") != NULL)) {
429 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
432 if (iniparser_getboolean(d, "global:try_first_pass", false)) {
433 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
436 if (iniparser_getint(d, "global:warn_pwd_expire", 0)) {
437 ctrl |= WINBIND_WARN_PWD_EXPIRE;
441 /* step through arguments */
442 for (i=argc,v=argv; i-- > 0; ++v) {
444 /* generic options */
445 if (!strcmp(*v,"debug"))
446 ctrl |= WINBIND_DEBUG_ARG;
447 else if (!strcasecmp(*v, "debug_state"))
448 ctrl |= WINBIND_DEBUG_STATE;
449 else if (!strcasecmp(*v, "silent"))
450 ctrl |= WINBIND_SILENT;
451 else if (!strcasecmp(*v, "use_authtok"))
452 ctrl |= WINBIND_USE_AUTHTOK_ARG;
453 else if (!strcasecmp(*v, "use_first_pass"))
454 ctrl |= WINBIND_USE_FIRST_PASS_ARG;
455 else if (!strcasecmp(*v, "try_first_pass"))
456 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
457 else if (!strcasecmp(*v, "unknown_ok"))
458 ctrl |= WINBIND_UNKNOWN_OK_ARG;
459 else if (!strncasecmp(*v, "require_membership_of",
460 strlen("require_membership_of")))
461 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
462 else if (!strncasecmp(*v, "require-membership-of",
463 strlen("require-membership-of")))
464 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
465 else if (!strcasecmp(*v, "krb5_auth"))
466 ctrl |= WINBIND_KRB5_AUTH;
467 else if (!strncasecmp(*v, "krb5_ccache_type",
468 strlen("krb5_ccache_type")))
469 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
470 else if (!strcasecmp(*v, "cached_login"))
471 ctrl |= WINBIND_CACHED_LOGIN;
473 __pam_log(pamh, ctrl, LOG_ERR,
474 "pam_parse: unknown option: %s", *v);
484 iniparser_freedict(d);
491 static int _pam_winbind_free_context(struct pwb_context *ctx)
498 iniparser_freedict(ctx->dict);
504 static int _pam_winbind_init_context(pam_handle_t *pamh,
508 struct pwb_context **ctx_p)
510 struct pwb_context *r = NULL;
512 r = TALLOC_ZERO_P(NULL, struct pwb_context);
517 talloc_set_destructor(r, _pam_winbind_free_context);
523 r->ctrl = _pam_parse(pamh, flags, argc, argv, &r->dict);
526 return PAM_SYSTEM_ERR;
534 static void _pam_winbind_cleanup_func(pam_handle_t *pamh,
538 int ctrl = _pam_parse(pamh, 0, 0, NULL, NULL);
539 if (_pam_log_is_debug_state_enabled(ctrl)) {
540 __pam_log_debug(pamh, ctrl, LOG_DEBUG,
541 "[pamh: %p] CLEAN: cleaning up PAM data %p "
542 "(error_status = %d)", pamh, data,
549 static const struct ntstatus_errors {
550 const char *ntstatus_string;
551 const char *error_string;
552 } ntstatus_errors[] = {
555 {"NT_STATUS_BACKUP_CONTROLLER",
556 "No primary Domain Controler available"},
557 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
558 "No domain controllers found"},
559 {"NT_STATUS_NO_LOGON_SERVERS",
561 {"NT_STATUS_PWD_TOO_SHORT",
562 "Password too short"},
563 {"NT_STATUS_PWD_TOO_RECENT",
564 "The password of this user is too recent to change"},
565 {"NT_STATUS_PWD_HISTORY_CONFLICT",
566 "Password is already in password history"},
567 {"NT_STATUS_PASSWORD_EXPIRED",
568 "Your password has expired"},
569 {"NT_STATUS_PASSWORD_MUST_CHANGE",
570 "You need to change your password now"},
571 {"NT_STATUS_INVALID_WORKSTATION",
572 "You are not allowed to logon from this workstation"},
573 {"NT_STATUS_INVALID_LOGON_HOURS",
574 "You are not allowed to logon at this time"},
575 {"NT_STATUS_ACCOUNT_EXPIRED",
576 "Your account has expired. "
577 "Please contact your System administrator"}, /* SCNR */
578 {"NT_STATUS_ACCOUNT_DISABLED",
579 "Your account is disabled. "
580 "Please contact your System administrator"}, /* SCNR */
581 {"NT_STATUS_ACCOUNT_LOCKED_OUT",
582 "Your account has been locked. "
583 "Please contact your System administrator"}, /* SCNR */
584 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
585 "Invalid Trust Account"},
586 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
587 "Invalid Trust Account"},
588 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
589 "Invalid Trust Account"},
590 {"NT_STATUS_ACCESS_DENIED",
595 static const char *_get_ntstatus_error_string(const char *nt_status_string)
598 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
599 if (!strcasecmp(ntstatus_errors[i].ntstatus_string,
601 return ntstatus_errors[i].error_string;
607 /* --- authentication management functions --- */
609 /* Attempt a conversation */
611 static int converse(const pam_handle_t *pamh,
613 struct pam_message **message,
614 struct pam_response **response)
617 struct pam_conv *conv;
619 retval = _pam_get_item(pamh, PAM_CONV, &conv);
620 if (retval == PAM_SUCCESS) {
621 retval = conv->conv(nargs,
622 (const struct pam_message **)message,
623 response, conv->appdata_ptr);
626 return retval; /* propagate error status */
630 static int _make_remark(struct pwb_context *ctx,
634 int retval = PAM_SUCCESS;
636 struct pam_message *pmsg[1], msg[1];
637 struct pam_response *resp;
639 if (ctx->flags & WINBIND_SILENT) {
644 msg[0].msg = discard_const_p(char, text);
645 msg[0].msg_style = type;
648 retval = converse(ctx->pamh, 1, pmsg, &resp);
651 _pam_drop_reply(resp, 1);
656 static int _make_remark_v(struct pwb_context *ctx,
664 ret = vasprintf(&var, format, args);
666 _pam_log(ctx, LOG_ERR, "memory allocation failure");
670 ret = _make_remark(ctx, type, var);
675 static int _make_remark_format(struct pwb_context *ctx, int type, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
676 static int _make_remark_format(struct pwb_context *ctx, int type, const char *format, ...)
681 va_start(args, format);
682 ret = _make_remark_v(ctx, type, format, args);
687 static int pam_winbind_request_log(struct pwb_context *ctx,
694 /* incorrect password */
695 _pam_log(ctx, LOG_WARNING, "user '%s' denied access "
696 "(incorrect password or invalid membership)", user);
698 case PAM_ACCT_EXPIRED:
699 /* account expired */
700 _pam_log(ctx, LOG_WARNING, "user '%s' account expired",
703 case PAM_AUTHTOK_EXPIRED:
704 /* password expired */
705 _pam_log(ctx, LOG_WARNING, "user '%s' password expired",
708 case PAM_NEW_AUTHTOK_REQD:
709 /* new password required */
710 _pam_log(ctx, LOG_WARNING, "user '%s' new password "
713 case PAM_USER_UNKNOWN:
714 /* the user does not exist */
715 _pam_log_debug(ctx, LOG_NOTICE, "user '%s' not found",
717 if (ctx->ctrl & WINBIND_UNKNOWN_OK_ARG) {
722 /* Otherwise, the authentication looked good */
723 if (strcmp(fn, "wbcLogonUser") == 0) {
724 _pam_log(ctx, LOG_NOTICE,
725 "user '%s' granted access", user);
727 _pam_log(ctx, LOG_NOTICE,
728 "user '%s' OK", user);
732 /* we don't know anything about this return value */
733 _pam_log(ctx, LOG_ERR,
734 "internal module error (retval = %s(%d), user = '%s')",
735 _pam_error_code_str(retval), retval, user);
740 static int wbc_auth_error_to_pam_error(struct pwb_context *ctx,
741 struct wbcAuthErrorInfo *e,
743 const char *username,
746 int ret = PAM_AUTH_ERR;
748 if (WBC_ERROR_IS_OK(status)) {
749 _pam_log_debug(ctx, LOG_DEBUG, "request %s succeeded",
752 return pam_winbind_request_log(ctx, ret, username, fn);
756 if (e->pam_error != PAM_SUCCESS) {
757 _pam_log(ctx, LOG_ERR,
758 "request %s failed: %s, "
759 "PAM error: %s (%d), NTSTATUS: %s, "
760 "Error message was: %s",
762 wbcErrorString(status),
763 _pam_error_code_str(e->pam_error),
768 return pam_winbind_request_log(ctx, ret, username, fn);
771 _pam_log(ctx, LOG_ERR, "request %s failed, but PAM error 0!", fn);
773 ret = PAM_SERVICE_ERR;
774 return pam_winbind_request_log(ctx, ret, username, fn);
777 ret = wbc_error_to_pam_error(status);
778 return pam_winbind_request_log(ctx, ret, username, fn);
783 * send a password expiry message if required
785 * @param ctx PAM winbind context.
786 * @param next_change expected (calculated) next expiry date.
787 * @param already_expired pointer to a boolean to indicate if the password is
790 * @return boolean Returns true if message has been sent, false if not.
793 static bool _pam_send_password_expiry_message(struct pwb_context *ctx,
797 bool *already_expired)
800 struct tm tm_now, tm_next_change;
802 if (already_expired) {
803 *already_expired = false;
806 if (next_change <= now) {
807 PAM_WB_REMARK_DIRECT(ctx, "NT_STATUS_PASSWORD_EXPIRED");
808 if (already_expired) {
809 *already_expired = true;
814 if ((next_change < 0) ||
815 (next_change > now + warn_pwd_expire * SECONDS_PER_DAY)) {
819 if ((localtime_r(&now, &tm_now) == NULL) ||
820 (localtime_r(&next_change, &tm_next_change) == NULL)) {
824 days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) -
825 (tm_now.tm_yday+tm_now.tm_year*365);
828 _make_remark(ctx, PAM_TEXT_INFO,
829 "Your password expires today");
833 if (days > 0 && days < warn_pwd_expire) {
834 _make_remark_format(ctx, PAM_TEXT_INFO,
835 "Your password will expire in %d %s",
836 days, (days > 1) ? "days":"day");
844 * Send a warning if the password expires in the near future
846 * @param ctx PAM winbind context.
847 * @param response The full authentication response structure.
848 * @param already_expired boolean, is the pwd already expired?
853 static void _pam_warn_password_expiry(struct pwb_context *ctx,
854 const struct wbcAuthUserInfo *info,
855 const struct wbcUserPasswordPolicyInfo *policy,
857 bool *already_expired)
859 time_t now = time(NULL);
860 time_t next_change = 0;
862 if (!info || !policy) {
866 if (already_expired) {
867 *already_expired = false;
870 /* accounts with WBC_ACB_PWNOEXP set never receive a warning */
871 if (info->acct_flags & WBC_ACB_PWNOEXP) {
875 /* no point in sending a warning if this is a grace logon */
876 if (PAM_WB_GRACE_LOGON(info->user_flags)) {
880 /* check if the info3 must change timestamp has been set */
881 next_change = info->pass_must_change_time;
883 if (_pam_send_password_expiry_message(ctx, next_change, now,
889 /* now check for the global password policy */
890 /* good catch from Ralf Haferkamp: an expiry of "never" is translated
892 if (policy->expire <= 0) {
896 next_change = info->pass_last_set_time + policy->expire;
898 if (_pam_send_password_expiry_message(ctx, next_change, now,
904 /* no warning sent */
907 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
910 * Append a string, making sure not to overflow and to always return a
911 * NULL-terminated string.
913 * @param dest Destination string buffer (must already be NULL-terminated).
914 * @param src Source string buffer.
915 * @param dest_buffer_size Size of dest buffer in bytes.
917 * @return false if dest buffer is not big enough (no bytes copied), true on
921 static bool safe_append_string(char *dest,
923 int dest_buffer_size)
925 int dest_length = strlen(dest);
926 int src_length = strlen(src);
928 if (dest_length + src_length + 1 > dest_buffer_size) {
932 memcpy(dest + dest_length, src, src_length + 1);
937 * Convert a names into a SID string, appending it to a buffer.
939 * @param ctx PAM winbind context.
940 * @param user User in PAM request.
941 * @param name Name to convert.
942 * @param sid_list_buffer Where to append the string sid.
943 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
945 * @return false on failure, true on success.
947 static bool winbind_name_to_sid_string(struct pwb_context *ctx,
950 char *sid_list_buffer,
951 int sid_list_buffer_size)
953 const char* sid_string;
956 if (IS_SID_STRING(name)) {
960 struct wbcDomainSid sid;
961 enum wbcSidType type;
964 _pam_log_debug(ctx, LOG_DEBUG,
965 "no sid given, looking up: %s\n", name);
967 wbc_status = wbcLookupName("", name, &sid, &type);
968 if (!WBC_ERROR_IS_OK(wbc_status)) {
969 _pam_log(ctx, LOG_INFO,
970 "could not lookup name: %s\n", name);
974 wbc_status = wbcSidToString(&sid, &sid_str);
975 if (!WBC_ERROR_IS_OK(wbc_status)) {
979 wbcFreeMemory(sid_str);
980 sid_string = sid_str;
983 if (!safe_append_string(sid_list_buffer, sid_string,
984 sid_list_buffer_size)) {
992 * Convert a list of names into a list of sids.
994 * @param ctx PAM winbind context.
995 * @param user User in PAM request.
996 * @param name_list List of names or string sids, separated by commas.
997 * @param sid_list_buffer Where to put the list of string sids.
998 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1000 * @return false on failure, true on success.
1002 static bool winbind_name_list_to_sid_string_list(struct pwb_context *ctx,
1004 const char *name_list,
1005 char *sid_list_buffer,
1006 int sid_list_buffer_size)
1008 bool result = false;
1009 char *current_name = NULL;
1010 const char *search_location;
1013 if (sid_list_buffer_size > 0) {
1014 sid_list_buffer[0] = 0;
1017 search_location = name_list;
1018 while ((comma = strstr(search_location, ",")) != NULL) {
1019 current_name = strndup(search_location,
1020 comma - search_location);
1021 if (NULL == current_name) {
1025 if (!winbind_name_to_sid_string(ctx, user,
1028 sid_list_buffer_size)) {
1032 SAFE_FREE(current_name);
1034 if (!safe_append_string(sid_list_buffer, ",",
1035 sid_list_buffer_size)) {
1039 search_location = comma + 1;
1042 if (!winbind_name_to_sid_string(ctx, user, search_location,
1044 sid_list_buffer_size)) {
1051 SAFE_FREE(current_name);
1056 * put krb5ccname variable into environment
1058 * @param ctx PAM winbind context.
1059 * @param krb5ccname env variable retrieved from winbindd.
1064 static void _pam_setup_krb5_env(struct pwb_context *ctx,
1065 struct wbcLogonUserInfo *info)
1070 const char *krb5ccname = NULL;
1072 if (off(ctx->ctrl, WINBIND_KRB5_AUTH)) {
1080 for (i=0; i < info->num_blobs; i++) {
1081 if (strcasecmp(info->blobs[i].name, "krb5ccname") == 0) {
1082 krb5ccname = (const char *)info->blobs[i].blob.data;
1087 if (!krb5ccname || (strlen(krb5ccname) == 0)) {
1091 _pam_log_debug(ctx, LOG_DEBUG,
1092 "request returned KRB5CCNAME: %s", krb5ccname);
1094 if (snprintf(var, sizeof(var), "KRB5CCNAME=%s", krb5ccname) == -1) {
1098 ret = pam_putenv(ctx->pamh, var);
1100 _pam_log(ctx, LOG_ERR,
1101 "failed to set KRB5CCNAME to %s: %s",
1102 var, pam_strerror(ctx->pamh, ret));
1107 * Copy unix username if available (further processed in PAM).
1109 * @param ctx PAM winbind context
1110 * @param user_ret A pointer that holds a pointer to a string
1111 * @param unix_username A username
1116 static void _pam_setup_unix_username(struct pwb_context *ctx,
1118 struct wbcLogonUserInfo *info)
1120 const char *unix_username = NULL;
1123 if (!user_ret || !info) {
1127 for (i=0; i < info->num_blobs; i++) {
1128 if (strcasecmp(info->blobs[i].name, "unix_username") == 0) {
1129 unix_username = (const char *)info->blobs[i].blob.data;
1134 if (!unix_username || !unix_username[0]) {
1138 *user_ret = strdup(unix_username);
1142 * Set string into the PAM stack.
1144 * @param ctx PAM winbind context.
1145 * @param data_name Key name for pam_set_data.
1146 * @param value String value.
1151 static void _pam_set_data_string(struct pwb_context *ctx,
1152 const char *data_name,
1157 if (!data_name || !value || (strlen(data_name) == 0) ||
1158 (strlen(value) == 0)) {
1162 ret = pam_set_data(ctx->pamh, data_name, talloc_strdup(NULL, value),
1163 _pam_winbind_cleanup_func);
1165 _pam_log_debug(ctx, LOG_DEBUG,
1166 "Could not set data %s: %s\n",
1167 data_name, pam_strerror(ctx->pamh, ret));
1172 * Set info3 strings into the PAM stack.
1174 * @param ctx PAM winbind context.
1175 * @param data_name Key name for pam_set_data.
1176 * @param value String value.
1181 static void _pam_set_data_info3(struct pwb_context *ctx,
1182 const struct wbcAuthUserInfo *info)
1184 _pam_set_data_string(ctx, PAM_WINBIND_HOMEDIR,
1185 info->home_directory);
1186 _pam_set_data_string(ctx, PAM_WINBIND_LOGONSCRIPT,
1187 info->logon_script);
1188 _pam_set_data_string(ctx, PAM_WINBIND_LOGONSERVER,
1189 info->logon_server);
1190 _pam_set_data_string(ctx, PAM_WINBIND_PROFILEPATH,
1191 info->profile_path);
1195 * Free info3 strings in the PAM stack.
1197 * @param pamh PAM handle
1202 static void _pam_free_data_info3(pam_handle_t *pamh)
1204 pam_set_data(pamh, PAM_WINBIND_HOMEDIR, NULL, NULL);
1205 pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT, NULL, NULL);
1206 pam_set_data(pamh, PAM_WINBIND_LOGONSERVER, NULL, NULL);
1207 pam_set_data(pamh, PAM_WINBIND_PROFILEPATH, NULL, NULL);
1211 * Send PAM_ERROR_MSG for cached or grace logons.
1213 * @param ctx PAM winbind context.
1214 * @param username User in PAM request.
1215 * @param info3_user_flgs Info3 flags containing logon type bits.
1220 static void _pam_warn_logon_type(struct pwb_context *ctx,
1221 const char *username,
1222 uint32_t info3_user_flgs)
1224 /* inform about logon type */
1225 if (PAM_WB_GRACE_LOGON(info3_user_flgs)) {
1227 _make_remark(ctx, PAM_ERROR_MSG,
1229 "Please change your password as soon you're "
1231 _pam_log_debug(ctx, LOG_DEBUG,
1232 "User %s logged on using grace logon\n",
1235 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs)) {
1237 _make_remark(ctx, PAM_ERROR_MSG,
1238 "Domain Controller unreachable, "
1239 "using cached credentials instead. "
1240 "Network resources may be unavailable");
1241 _pam_log_debug(ctx, LOG_DEBUG,
1242 "User %s logged on using cached credentials\n",
1248 * Send PAM_ERROR_MSG for krb5 errors.
1250 * @param ctx PAM winbind context.
1251 * @param username User in PAM request.
1252 * @param info3_user_flgs Info3 flags containing logon type bits.
1257 static void _pam_warn_krb5_failure(struct pwb_context *ctx,
1258 const char *username,
1259 uint32_t info3_user_flgs)
1261 if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs)) {
1262 _make_remark(ctx, PAM_ERROR_MSG,
1263 "Failed to establish your Kerberos Ticket cache "
1264 "due time differences\n"
1265 "with the domain controller. "
1266 "Please verify the system time.\n");
1267 _pam_log_debug(ctx, LOG_DEBUG,
1268 "User %s: Clock skew when getting Krb5 TGT\n",
1273 static bool _pam_check_remark_auth_err(struct pwb_context *ctx,
1274 const struct wbcAuthErrorInfo *e,
1275 const char *nt_status_string,
1278 const char *ntstatus = NULL;
1279 const char *error_string = NULL;
1281 if (!e || !pam_error) {
1285 ntstatus = e->nt_string;
1290 if (strcasecmp(ntstatus, nt_status_string) == 0) {
1292 error_string = _get_ntstatus_error_string(nt_status_string);
1294 _make_remark(ctx, PAM_ERROR_MSG, error_string);
1295 *pam_error = e->pam_error;
1299 if (e->display_string) {
1300 _make_remark(ctx, PAM_ERROR_MSG, e->display_string);
1301 *pam_error = e->pam_error;
1305 _make_remark(ctx, PAM_ERROR_MSG, nt_status_string);
1306 *pam_error = e->pam_error;
1315 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
1317 * @param i The wbcUserPasswordPolicyInfo struct.
1319 * @return string (caller needs to talloc_free).
1322 static char *_pam_compose_pwd_restriction_string(struct pwb_context *ctx,
1323 struct wbcUserPasswordPolicyInfo *i)
1331 str = talloc_asprintf(ctx, "Your password ");
1336 if (i->min_length_password > 0) {
1337 str = talloc_asprintf_append(str,
1338 "must be at least %d characters; ",
1339 i->min_length_password);
1345 if (i->password_history > 0) {
1346 str = talloc_asprintf_append(str,
1347 "cannot repeat any of your previous %d "
1349 i->password_history);
1355 if (i->password_properties & WBC_DOMAIN_PASSWORD_COMPLEX) {
1356 str = talloc_asprintf_append(str,
1357 "must contain capitals, numerals "
1359 "and cannot contain your account "
1366 str = talloc_asprintf_append(str,
1367 "Please type a different password. "
1368 "Type a password which meets these requirements in "
1369 "both text boxes.");
1381 /* talk to winbindd */
1382 static int winbind_auth_request(struct pwb_context *ctx,
1387 const int warn_pwd_expire,
1388 struct wbcAuthErrorInfo **p_error,
1389 struct wbcLogonUserInfo **p_info,
1390 struct wbcUserPasswordPolicyInfo **p_policy,
1391 time_t *pwd_last_set,
1396 struct wbcLogonUserParams logon;
1397 char membership_of[1024];
1398 uid_t user_uid = -1;
1399 uint32_t flags = WBFLAG_PAM_INFO3_TEXT |
1400 WBFLAG_PAM_GET_PWD_POLICY;
1402 struct wbcLogonUserInfo *info = NULL;
1403 struct wbcAuthUserInfo *user_info = NULL;
1404 struct wbcAuthErrorInfo *error = NULL;
1405 struct wbcUserPasswordPolicyInfo *policy = NULL;
1407 int ret = PAM_AUTH_ERR;
1409 const char *codes[] = {
1410 "NT_STATUS_PASSWORD_EXPIRED",
1411 "NT_STATUS_PASSWORD_MUST_CHANGE",
1412 "NT_STATUS_INVALID_WORKSTATION",
1413 "NT_STATUS_INVALID_LOGON_HOURS",
1414 "NT_STATUS_ACCOUNT_EXPIRED",
1415 "NT_STATUS_ACCOUNT_DISABLED",
1416 "NT_STATUS_ACCOUNT_LOCKED_OUT",
1417 "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
1418 "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
1419 "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
1420 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1421 "NT_STATUS_NO_LOGON_SERVERS",
1422 "NT_STATUS_WRONG_PASSWORD",
1423 "NT_STATUS_ACCESS_DENIED"
1430 /* Krb5 auth always has to go against the KDC of the user's realm */
1432 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1433 flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
1436 if (ctx->ctrl & (WINBIND_KRB5_AUTH|WINBIND_CACHED_LOGIN)) {
1437 struct passwd *pwd = NULL;
1439 pwd = getpwnam(user);
1441 return PAM_USER_UNKNOWN;
1443 user_uid = pwd->pw_uid;
1446 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1448 _pam_log_debug(ctx, LOG_DEBUG,
1449 "enabling krb5 login flag\n");
1451 flags |= WBFLAG_PAM_KRB5 |
1452 WBFLAG_PAM_FALLBACK_AFTER_KRB5;
1455 if (ctx->ctrl & WINBIND_CACHED_LOGIN) {
1456 _pam_log_debug(ctx, LOG_DEBUG,
1457 "enabling cached login flag\n");
1458 flags |= WBFLAG_PAM_CACHED_LOGIN;
1463 flags |= WBFLAG_PAM_UNIX_NAME;
1466 if (cctype != NULL) {
1467 _pam_log_debug(ctx, LOG_DEBUG,
1468 "enabling request for a %s krb5 ccache\n",
1472 if (member != NULL) {
1474 ZERO_STRUCT(membership_of);
1476 if (!winbind_name_list_to_sid_string_list(ctx, user, member,
1478 sizeof(membership_of))) {
1479 _pam_log_debug(ctx, LOG_ERR,
1480 "failed to serialize membership of sid "
1481 "\"%s\"\n", member);
1482 return PAM_AUTH_ERR;
1488 logon.username = user;
1489 logon.password = pass;
1491 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1497 if (!WBC_ERROR_IS_OK(wbc_status)) {
1501 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1507 if (!WBC_ERROR_IS_OK(wbc_status)) {
1511 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1515 (uint8_t *)&user_uid,
1517 if (!WBC_ERROR_IS_OK(wbc_status)) {
1521 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1525 (uint8_t *)membership_of,
1526 sizeof(membership_of));
1527 if (!WBC_ERROR_IS_OK(wbc_status)) {
1531 wbc_status = wbcLogonUser(&logon, &info, &error, &policy);
1532 ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
1533 user, "wbcLogonUser");
1534 wbcFreeMemory(logon.blobs);
1537 if (info && info->info) {
1538 user_info = info->info;
1541 if (pwd_last_set && user_info) {
1542 *pwd_last_set = user_info->pass_last_set_time;
1545 if (p_info && info) {
1549 if (p_policy && policy) {
1553 if (p_error && error) {
1554 /* We want to process the error in the caller. */
1559 for (i=0; i<ARRAY_SIZE(codes); i++) {
1561 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
1567 if ((ret == PAM_SUCCESS) && user_info && policy && info) {
1569 bool already_expired = false;
1571 /* warn a user if the password is about to expire soon */
1572 _pam_warn_password_expiry(ctx, user_info, policy,
1576 if (already_expired == true) {
1578 SMB_TIME_T last_set = user_info->pass_last_set_time;
1580 _pam_log_debug(ctx, LOG_DEBUG,
1581 "Password has expired "
1582 "(Password was last set: %lld, "
1583 "the policy says it should expire here "
1584 "%lld (now it's: %lu))\n",
1585 (long long int)last_set,
1586 (long long int)last_set +
1590 return PAM_AUTHTOK_EXPIRED;
1593 /* inform about logon type */
1594 _pam_warn_logon_type(ctx, user, user_info->user_flags);
1596 /* inform about krb5 failures */
1597 _pam_warn_krb5_failure(ctx, user, user_info->user_flags);
1599 /* set some info3 info for other modules in the stack */
1600 _pam_set_data_info3(ctx, user_info);
1602 /* put krb5ccname into env */
1603 _pam_setup_krb5_env(ctx, info);
1605 /* If winbindd returned a username, return the pointer to it
1607 _pam_setup_unix_username(ctx, user_ret, info);
1612 wbcFreeMemory(logon.blobs);
1614 if (info && info->blobs) {
1615 wbcFreeMemory(info->blobs);
1617 if (error && !p_error) {
1618 wbcFreeMemory(error);
1620 if (info && !p_info) {
1621 wbcFreeMemory(info);
1623 if (policy && !p_policy) {
1624 wbcFreeMemory(policy);
1630 /* talk to winbindd */
1631 static int winbind_chauthtok_request(struct pwb_context *ctx,
1633 const char *oldpass,
1634 const char *newpass,
1635 time_t pwd_last_set)
1638 struct wbcChangePasswordParams params;
1639 struct wbcAuthErrorInfo *error = NULL;
1640 struct wbcUserPasswordPolicyInfo *policy = NULL;
1641 enum wbcPasswordChangeRejectReason reject_reason = -1;
1645 const char *codes[] = {
1646 "NT_STATUS_BACKUP_CONTROLLER",
1647 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1648 "NT_STATUS_NO_LOGON_SERVERS",
1649 "NT_STATUS_ACCESS_DENIED",
1650 "NT_STATUS_PWD_TOO_SHORT", /* TODO: tell the min pwd length ? */
1651 "NT_STATUS_PWD_TOO_RECENT", /* TODO: tell the minage ? */
1652 "NT_STATUS_PWD_HISTORY_CONFLICT" /* TODO: tell the history length ? */
1654 int ret = PAM_AUTH_ERR;
1656 ZERO_STRUCT(params);
1658 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1659 flags |= WBFLAG_PAM_KRB5 |
1660 WBFLAG_PAM_CONTACT_TRUSTDOM;
1663 if (ctx->ctrl & WINBIND_CACHED_LOGIN) {
1664 flags |= WBFLAG_PAM_CACHED_LOGIN;
1667 params.account_name = user;
1668 params.level = WBC_AUTH_USER_LEVEL_PLAIN;
1669 params.old_password.plaintext = oldpass;
1670 params.new_password.plaintext = newpass;
1671 params.flags = flags;
1673 wbc_status = wbcChangeUserPasswordEx(¶ms, &error, &reject_reason, &policy);
1674 ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
1675 user, "wbcChangeUserPasswordEx");
1677 if (WBC_ERROR_IS_OK(wbc_status)) {
1678 _pam_log(ctx, LOG_NOTICE,
1679 "user '%s' password changed", user);
1684 wbcFreeMemory(policy);
1688 for (i=0; i<ARRAY_SIZE(codes); i++) {
1690 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
1696 if (!strcasecmp(error->nt_string,
1697 "NT_STATUS_PASSWORD_RESTRICTION")) {
1699 char *pwd_restriction_string = NULL;
1700 SMB_TIME_T min_pwd_age = 0;
1703 min_pwd_age = policy->min_passwordage;
1706 /* FIXME: avoid to send multiple PAM messages after another */
1707 switch (reject_reason) {
1710 case WBC_PWD_CHANGE_REJECT_OTHER:
1711 if ((min_pwd_age > 0) &&
1712 (pwd_last_set + min_pwd_age > time(NULL))) {
1713 PAM_WB_REMARK_DIRECT(ctx,
1714 "NT_STATUS_PWD_TOO_RECENT");
1717 case WBC_PWD_CHANGE_REJECT_TOO_SHORT:
1718 PAM_WB_REMARK_DIRECT(ctx,
1719 "NT_STATUS_PWD_TOO_SHORT");
1721 case WBC_PWD_CHANGE_REJECT_IN_HISTORY:
1722 PAM_WB_REMARK_DIRECT(ctx,
1723 "NT_STATUS_PWD_HISTORY_CONFLICT");
1725 case WBC_PWD_CHANGE_REJECT_COMPLEXITY:
1726 _make_remark(ctx, PAM_ERROR_MSG,
1727 "Password does not meet "
1728 "complexity requirements");
1731 _pam_log_debug(ctx, LOG_DEBUG,
1732 "unknown password change "
1733 "reject reason: %d",
1738 pwd_restriction_string =
1739 _pam_compose_pwd_restriction_string(ctx, policy);
1740 if (pwd_restriction_string) {
1741 _make_remark(ctx, PAM_ERROR_MSG,
1742 pwd_restriction_string);
1743 TALLOC_FREE(pwd_restriction_string);
1747 wbcFreeMemory(error);
1748 wbcFreeMemory(policy);
1754 * Checks if a user has an account
1757 * 1 = User not found
1761 static int valid_user(struct pwb_context *ctx,
1764 /* check not only if the user is available over NSS calls, also make
1765 * sure it's really a winbind user, this is important when stacking PAM
1766 * modules in the 'account' or 'password' facility. */
1769 struct passwd *pwd = NULL;
1770 struct passwd *wb_pwd = NULL;
1772 pwd = getpwnam(user);
1777 wbc_status = wbcGetpwnam(user, &wb_pwd);
1778 wbcFreeMemory(wb_pwd);
1779 if (!WBC_ERROR_IS_OK(wbc_status)) {
1780 _pam_log(ctx, LOG_DEBUG, "valid_user: wbcGetpwnam gave %s\n",
1781 wbcErrorString(wbc_status));
1784 switch (wbc_status) {
1785 case WBC_ERR_UNKNOWN_USER:
1787 case WBC_ERR_SUCCESS:
1795 static char *_pam_delete(register char *xx)
1803 * obtain a password from the user
1806 static int _winbind_read_password(struct pwb_context *ctx,
1808 const char *comment,
1809 const char *prompt1,
1810 const char *prompt2,
1818 _pam_log(ctx, LOG_DEBUG, "getting password (0x%08x)", ctrl);
1821 * make sure nothing inappropriate gets returned
1824 *pass = token = NULL;
1827 * which authentication token are we getting?
1830 if (on(WINBIND__OLD_PASSWORD, ctrl)) {
1831 authtok_flag = PAM_OLDAUTHTOK;
1833 authtok_flag = PAM_AUTHTOK;
1837 * should we obtain the password from a PAM item ?
1840 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) ||
1841 on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1842 retval = _pam_get_item(ctx->pamh, authtok_flag, &item);
1843 if (retval != PAM_SUCCESS) {
1845 _pam_log(ctx, LOG_ALERT,
1846 "pam_get_item returned error "
1847 "to unix-read-password");
1849 } else if (item != NULL) { /* we have a password! */
1852 _pam_log(ctx, LOG_DEBUG,
1853 "pam_get_item returned a password");
1855 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1856 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
1857 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
1858 && off(WINBIND__OLD_PASSWORD, ctrl)) {
1859 return PAM_AUTHTOK_RECOVER_ERR;
1863 * getting here implies we will have to get the password from the
1868 struct pam_message msg[3], *pmsg[3];
1869 struct pam_response *resp;
1872 /* prepare to converse */
1874 if (comment != NULL && off(ctrl, WINBIND_SILENT)) {
1876 msg[0].msg_style = PAM_TEXT_INFO;
1877 msg[0].msg = discard_const_p(char, comment);
1884 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1885 msg[i++].msg = discard_const_p(char, prompt1);
1888 if (prompt2 != NULL) {
1890 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1891 msg[i++].msg = discard_const_p(char, prompt2);
1894 /* so call the conversation expecting i responses */
1896 retval = converse(ctx->pamh, i, pmsg, &resp);
1898 if (retval == PAM_SUCCESS) {
1899 retval = PAM_AUTHTOK_RECOVER_ERR;
1903 if (retval != PAM_SUCCESS) {
1904 _pam_drop_reply(resp, i);
1908 /* interpret the response */
1910 token = x_strdup(resp[i - replies].resp);
1912 _pam_log(ctx, LOG_NOTICE,
1913 "could not recover "
1914 "authentication token");
1915 retval = PAM_AUTHTOK_RECOVER_ERR;
1920 /* verify that password entered correctly */
1921 if (!resp[i - 1].resp ||
1922 strcmp(token, resp[i - 1].resp)) {
1923 _pam_delete(token); /* mistyped */
1924 retval = PAM_AUTHTOK_RECOVER_ERR;
1925 _make_remark(ctx, PAM_ERROR_MSG,
1931 * tidy up the conversation (resp_retcode) is ignored
1932 * -- what is it for anyway? AGM
1934 _pam_drop_reply(resp, i);
1938 if (retval != PAM_SUCCESS) {
1939 _pam_log_debug(ctx, LOG_DEBUG,
1940 "unable to obtain a password");
1943 /* 'token' is the entered password */
1945 /* we store this password as an item */
1947 retval = pam_set_item(ctx->pamh, authtok_flag, token);
1948 _pam_delete(token); /* clean it up */
1949 if (retval != PAM_SUCCESS ||
1950 (retval = _pam_get_item(ctx->pamh, authtok_flag, &item)) != PAM_SUCCESS) {
1952 _pam_log(ctx, LOG_CRIT, "error manipulating password");
1958 item = NULL; /* break link to password */
1963 static const char *get_conf_item_string(struct pwb_context *ctx,
1968 const char *parm_opt = NULL;
1970 if (!(ctx->ctrl & config_flag)) {
1974 /* let the pam opt take precedence over the pam_winbind.conf option */
1975 for (i=0; i<ctx->argc; i++) {
1977 if ((strncmp(ctx->argv[i], item, strlen(item)) == 0)) {
1980 if ((p = strchr(ctx->argv[i], '=')) == NULL) {
1981 _pam_log(ctx, LOG_INFO,
1982 "no \"=\" delimiter for \"%s\" found\n",
1986 _pam_log_debug(ctx, LOG_INFO,
1987 "PAM config: %s '%s'\n", item, p+1);
1995 key = talloc_asprintf(ctx, "global:%s", item);
2000 parm_opt = iniparser_getstr(ctx->dict, key);
2003 _pam_log_debug(ctx, LOG_INFO, "CONFIG file: %s '%s'\n",
2010 static int get_config_item_int(struct pwb_context *ctx,
2014 int i, parm_opt = -1;
2016 if (!(ctx->ctrl & config_flag)) {
2020 /* let the pam opt take precedence over the pam_winbind.conf option */
2021 for (i = 0; i < ctx->argc; i++) {
2023 if ((strncmp(ctx->argv[i], item, strlen(item)) == 0)) {
2026 if ((p = strchr(ctx->argv[i], '=')) == NULL) {
2027 _pam_log(ctx, LOG_INFO,
2028 "no \"=\" delimiter for \"%s\" found\n",
2032 parm_opt = atoi(p + 1);
2033 _pam_log_debug(ctx, LOG_INFO,
2034 "PAM config: %s '%d'\n",
2043 key = talloc_asprintf(ctx, "global:%s", item);
2048 parm_opt = iniparser_getint(ctx->dict, key, -1);
2051 _pam_log_debug(ctx, LOG_INFO,
2052 "CONFIG file: %s '%d'\n",
2059 static const char *get_krb5_cc_type_from_config(struct pwb_context *ctx)
2061 return get_conf_item_string(ctx, "krb5_ccache_type",
2062 WINBIND_KRB5_CCACHE_TYPE);
2065 static const char *get_member_from_config(struct pwb_context *ctx)
2067 const char *ret = NULL;
2068 ret = get_conf_item_string(ctx, "require_membership_of",
2069 WINBIND_REQUIRED_MEMBERSHIP);
2073 return get_conf_item_string(ctx, "require-membership-of",
2074 WINBIND_REQUIRED_MEMBERSHIP);
2077 static int get_warn_pwd_expire_from_config(struct pwb_context *ctx)
2080 ret = get_config_item_int(ctx, "warn_pwd_expire",
2081 WINBIND_WARN_PWD_EXPIRE);
2082 /* no or broken setting */
2084 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES;
2090 * Retrieve the winbind separator.
2092 * @param ctx PAM winbind context.
2094 * @return string separator character. NULL on failure.
2097 static char winbind_get_separator(struct pwb_context *ctx)
2100 static struct wbcInterfaceDetails *details = NULL;
2102 wbc_status = wbcInterfaceDetails(&details);
2103 if (!WBC_ERROR_IS_OK(wbc_status)) {
2104 _pam_log(ctx, LOG_ERR,
2105 "Could not retrieve winbind interface details: %s",
2106 wbcErrorString(wbc_status));
2114 return details->winbind_separator;
2119 * Convert a upn to a name.
2121 * @param ctx PAM winbind context.
2122 * @param upn USer UPN to be trabslated.
2124 * @return converted name. NULL pointer on failure. Caller needs to free.
2127 static char* winbind_upn_to_username(struct pwb_context *ctx,
2131 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
2132 struct wbcDomainSid sid;
2133 enum wbcSidType type;
2137 /* This cannot work when the winbind separator = @ */
2139 sep = winbind_get_separator(ctx);
2140 if (!sep || sep == '@') {
2144 /* Convert the UPN to a SID */
2146 wbc_status = wbcLookupName("", upn, &sid, &type);
2147 if (!WBC_ERROR_IS_OK(wbc_status)) {
2151 /* Convert the the SID back to the sAMAccountName */
2153 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
2154 if (!WBC_ERROR_IS_OK(wbc_status)) {
2158 return talloc_asprintf(ctx, "%s\\%s", domain, name);
2162 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
2163 int argc, const char **argv)
2165 const char *username;
2166 const char *password;
2167 const char *member = NULL;
2168 const char *cctype = NULL;
2169 int warn_pwd_expire;
2170 int retval = PAM_AUTH_ERR;
2171 char *username_ret = NULL;
2172 char *new_authtok_required = NULL;
2173 char *real_username = NULL;
2174 struct pwb_context *ctx = NULL;
2176 retval = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2181 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", ctx);
2183 /* Get the username */
2184 retval = pam_get_user(pamh, &username, NULL);
2185 if ((retval != PAM_SUCCESS) || (!username)) {
2186 _pam_log_debug(ctx, LOG_DEBUG,
2187 "can not get the username");
2188 retval = PAM_SERVICE_ERR;
2194 /* Decode the user name since AIX does not support logn user
2195 names by default. The name is encoded as _#uid. */
2197 if (username[0] == '_') {
2198 uid_t id = atoi(&username[1]);
2199 struct passwd *pw = NULL;
2201 if ((id!=0) && ((pw = getpwuid(id)) != NULL)) {
2202 real_username = strdup(pw->pw_name);
2207 if (!real_username) {
2208 /* Just making a copy of the username we got from PAM */
2209 if ((real_username = strdup(username)) == NULL) {
2210 _pam_log_debug(ctx, LOG_DEBUG,
2211 "memory allocation failure when copying "
2213 retval = PAM_SERVICE_ERR;
2218 /* Maybe this was a UPN */
2220 if (strchr(real_username, '@') != NULL) {
2221 char *samaccountname = NULL;
2223 samaccountname = winbind_upn_to_username(ctx,
2225 if (samaccountname) {
2226 free(real_username);
2227 real_username = strdup(samaccountname);
2231 retval = _winbind_read_password(ctx, ctx->ctrl, NULL,
2235 if (retval != PAM_SUCCESS) {
2236 _pam_log(ctx, LOG_ERR,
2237 "Could not retrieve user's password");
2238 retval = PAM_AUTHTOK_ERR;
2242 /* Let's not give too much away in the log file */
2244 #ifdef DEBUG_PASSWORD
2245 _pam_log_debug(ctx, LOG_INFO,
2246 "Verify user '%s' with password '%s'",
2247 real_username, password);
2249 _pam_log_debug(ctx, LOG_INFO,
2250 "Verify user '%s'", real_username);
2253 member = get_member_from_config(ctx);
2254 cctype = get_krb5_cc_type_from_config(ctx);
2255 warn_pwd_expire = get_warn_pwd_expire_from_config(ctx);
2257 /* Now use the username to look up password */
2258 retval = winbind_auth_request(ctx, real_username, password,
2259 member, cctype, warn_pwd_expire,
2261 NULL, &username_ret);
2263 if (retval == PAM_NEW_AUTHTOK_REQD ||
2264 retval == PAM_AUTHTOK_EXPIRED) {
2266 char *new_authtok_required_during_auth = NULL;
2268 new_authtok_required = talloc_asprintf(NULL, "%d", retval);
2269 if (!new_authtok_required) {
2270 retval = PAM_BUF_ERR;
2274 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2275 new_authtok_required,
2276 _pam_winbind_cleanup_func);
2278 retval = PAM_SUCCESS;
2280 new_authtok_required_during_auth = talloc_asprintf(NULL, "%d", true);
2281 if (!new_authtok_required_during_auth) {
2282 retval = PAM_BUF_ERR;
2286 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2287 new_authtok_required_during_auth,
2288 _pam_winbind_cleanup_func);
2295 pam_set_item (pamh, PAM_USER, username_ret);
2296 _pam_log_debug(ctx, LOG_INFO,
2297 "Returned user was '%s'", username_ret);
2301 if (real_username) {
2302 free(real_username);
2305 if (!new_authtok_required) {
2306 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
2309 if (retval != PAM_SUCCESS) {
2310 _pam_free_data_info3(pamh);
2313 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", ctx, retval);
2321 int pam_sm_setcred(pam_handle_t *pamh, int flags,
2322 int argc, const char **argv)
2324 int ret = PAM_SYSTEM_ERR;
2325 struct pwb_context *ctx = NULL;
2327 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2332 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", ctx);
2334 switch (flags & ~PAM_SILENT) {
2336 case PAM_DELETE_CRED:
2337 ret = pam_sm_close_session(pamh, flags, argc, argv);
2339 case PAM_REFRESH_CRED:
2340 _pam_log_debug(ctx, LOG_WARNING,
2341 "PAM_REFRESH_CRED not implemented");
2344 case PAM_REINITIALIZE_CRED:
2345 _pam_log_debug(ctx, LOG_WARNING,
2346 "PAM_REINITIALIZE_CRED not implemented");
2349 case PAM_ESTABLISH_CRED:
2350 _pam_log_debug(ctx, LOG_WARNING,
2351 "PAM_ESTABLISH_CRED not implemented");
2355 ret = PAM_SYSTEM_ERR;
2361 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", ctx, ret);
2369 * Account management. We want to verify that the account exists
2370 * before returning PAM_SUCCESS
2373 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
2374 int argc, const char **argv)
2376 const char *username;
2377 int ret = PAM_USER_UNKNOWN;
2379 struct pwb_context *ctx = NULL;
2381 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2386 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", ctx);
2389 /* Get the username */
2390 ret = pam_get_user(pamh, &username, NULL);
2391 if ((ret != PAM_SUCCESS) || (!username)) {
2392 _pam_log_debug(ctx, LOG_DEBUG,
2393 "can not get the username");
2394 ret = PAM_SERVICE_ERR;
2398 /* Verify the username */
2399 ret = valid_user(ctx, username);
2402 /* some sort of system error. The log was already printed */
2403 ret = PAM_SERVICE_ERR;
2406 /* the user does not exist */
2407 _pam_log_debug(ctx, LOG_NOTICE, "user '%s' not found",
2409 if (ctx->ctrl & WINBIND_UNKNOWN_OK_ARG) {
2413 ret = PAM_USER_UNKNOWN;
2416 pam_get_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2417 (const void **)&tmp);
2419 ret = atoi((const char *)tmp);
2421 case PAM_AUTHTOK_EXPIRED:
2422 /* fall through, since new token is required in this case */
2423 case PAM_NEW_AUTHTOK_REQD:
2424 _pam_log(ctx, LOG_WARNING,
2425 "pam_sm_acct_mgmt success but %s is set",
2426 PAM_WINBIND_NEW_AUTHTOK_REQD);
2427 _pam_log(ctx, LOG_NOTICE,
2428 "user '%s' needs new password",
2430 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2431 ret = PAM_NEW_AUTHTOK_REQD;
2434 _pam_log(ctx, LOG_WARNING,
2435 "pam_sm_acct_mgmt success");
2436 _pam_log(ctx, LOG_NOTICE,
2437 "user '%s' granted access", username);
2443 /* Otherwise, the authentication looked good */
2444 _pam_log(ctx, LOG_NOTICE,
2445 "user '%s' granted access", username);
2449 /* we don't know anything about this return value */
2450 _pam_log(ctx, LOG_ERR,
2451 "internal module error (ret = %d, user = '%s')",
2453 ret = PAM_SERVICE_ERR;
2457 /* should not be reached */
2462 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", ctx, ret);
2470 int pam_sm_open_session(pam_handle_t *pamh, int flags,
2471 int argc, const char **argv)
2473 int ret = PAM_SYSTEM_ERR;
2474 struct pwb_context *ctx = NULL;
2476 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2481 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", ctx);
2486 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", ctx, ret);
2494 int pam_sm_close_session(pam_handle_t *pamh, int flags,
2495 int argc, const char **argv)
2497 int retval = PAM_SUCCESS;
2498 struct pwb_context *ctx = NULL;
2499 struct wbcLogoffUserParams logoff;
2501 retval = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2506 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", ctx);
2508 if (!(flags & PAM_DELETE_CRED)) {
2509 retval = PAM_SUCCESS;
2513 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
2515 /* destroy the ccache here */
2518 struct wbcAuthErrorInfo *error = NULL;
2522 const char *ccname = NULL;
2523 struct passwd *pwd = NULL;
2525 retval = pam_get_user(pamh, &user, "Username: ");
2527 _pam_log(ctx, LOG_ERR,
2528 "could not identify user");
2533 _pam_log(ctx, LOG_ERR,
2534 "username was NULL!");
2535 retval = PAM_USER_UNKNOWN;
2539 _pam_log_debug(ctx, LOG_DEBUG,
2540 "username [%s] obtained", user);
2542 ccname = pam_getenv(pamh, "KRB5CCNAME");
2543 if (ccname == NULL) {
2544 _pam_log_debug(ctx, LOG_DEBUG,
2545 "user has no KRB5CCNAME environment");
2548 pwd = getpwnam(user);
2550 retval = PAM_USER_UNKNOWN;
2554 flags = WBFLAG_PAM_KRB5 |
2555 WBFLAG_PAM_CONTACT_TRUSTDOM;
2557 ZERO_STRUCT(logoff);
2559 logoff.username = user;
2561 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2567 if (!WBC_ERROR_IS_OK(wbc_status)) {
2571 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2577 if (!WBC_ERROR_IS_OK(wbc_status)) {
2581 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2585 (uint8_t *)&pwd->pw_uid,
2586 sizeof(pwd->pw_uid));
2587 if (!WBC_ERROR_IS_OK(wbc_status)) {
2591 wbc_status = wbcLogoffUserEx(&logoff, &error);
2592 retval = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
2593 user, "wbcLogoffUser");
2594 wbcFreeMemory(error);
2595 wbcFreeMemory(logoff.blobs);
2597 if (!WBC_ERROR_IS_OK(wbc_status)) {
2598 _pam_log(ctx, LOG_INFO,
2599 "failed to logoff user %s: %s\n",
2600 user, wbcErrorString(wbc_status));
2606 wbcFreeMemory(logoff.blobs);
2609 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", ctx, retval);
2617 * evaluate whether we need to re-authenticate with kerberos after a
2620 * @param ctx PAM winbind context.
2621 * @param user The username
2623 * @return boolean Returns true if required, false if not.
2626 static bool _pam_require_krb5_auth_after_chauthtok(struct pwb_context *ctx,
2630 /* Make sure that we only do this if a) the chauthtok got initiated
2631 * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2632 * later password change via the "passwd" command if done by the user
2634 * NB. If we login from gdm or xdm and the password expires,
2635 * we change the password, but there is no memory cache.
2636 * Thus, even for passthrough login, we should do the
2637 * authentication again to update memory cache.
2641 char *new_authtok_reqd_during_auth = NULL;
2642 struct passwd *pwd = NULL;
2644 _pam_get_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2645 &new_authtok_reqd_during_auth);
2646 pam_set_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2649 if (new_authtok_reqd_during_auth) {
2653 pwd = getpwnam(user);
2658 if (getuid() == pwd->pw_uid) {
2667 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
2668 int argc, const char **argv)
2672 bool cached_login = false;
2674 /* <DO NOT free() THESE> */
2676 char *pass_old, *pass_new;
2677 /* </DO NOT free() THESE> */
2682 char *username_ret = NULL;
2683 struct wbcAuthErrorInfo *error = NULL;
2684 struct pwb_context *ctx = NULL;
2686 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2691 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", ctx);
2693 cached_login = (ctx->ctrl & WINBIND_CACHED_LOGIN);
2695 /* clearing offline bit for auth */
2696 ctx->ctrl &= ~WINBIND_CACHED_LOGIN;
2699 * First get the name of a user
2701 ret = pam_get_user(pamh, &user, "Username: ");
2703 _pam_log(ctx, LOG_ERR,
2704 "password - could not identify user");
2709 _pam_log(ctx, LOG_ERR, "username was NULL!");
2710 ret = PAM_USER_UNKNOWN;
2714 _pam_log_debug(ctx, LOG_DEBUG, "username [%s] obtained", user);
2716 /* check if this is really a user in winbindd, not only in NSS */
2717 ret = valid_user(ctx, user);
2720 ret = PAM_USER_UNKNOWN;
2723 ret = PAM_SYSTEM_ERR;
2730 * obtain and verify the current password (OLDAUTHTOK) for
2734 if (flags & PAM_PRELIM_CHECK) {
2735 time_t pwdlastset_prelim = 0;
2737 /* instruct user what is happening */
2739 #define greeting "Changing password for"
2740 Announce = talloc_asprintf(ctx, "%s %s", greeting, user);
2742 _pam_log(ctx, LOG_CRIT,
2743 "password - out of memory");
2749 lctrl = ctx->ctrl | WINBIND__OLD_PASSWORD;
2750 ret = _winbind_read_password(ctx, lctrl,
2752 "(current) NT password: ",
2754 (const char **) &pass_old);
2755 TALLOC_FREE(Announce);
2756 if (ret != PAM_SUCCESS) {
2757 _pam_log(ctx, LOG_NOTICE,
2758 "password - (old) token not obtained");
2762 /* verify that this is the password for this user */
2764 ret = winbind_auth_request(ctx, user, pass_old,
2767 &pwdlastset_prelim, NULL);
2769 if (ret != PAM_ACCT_EXPIRED &&
2770 ret != PAM_AUTHTOK_EXPIRED &&
2771 ret != PAM_NEW_AUTHTOK_REQD &&
2772 ret != PAM_SUCCESS) {
2777 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET,
2778 (void *)pwdlastset_prelim, NULL);
2780 ret = pam_set_item(pamh, PAM_OLDAUTHTOK,
2781 (const void *) pass_old);
2783 if (ret != PAM_SUCCESS) {
2784 _pam_log(ctx, LOG_CRIT,
2785 "failed to set PAM_OLDAUTHTOK");
2787 } else if (flags & PAM_UPDATE_AUTHTOK) {
2789 time_t pwdlastset_update = 0;
2792 * obtain the proposed password
2796 * get the old token back.
2799 ret = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
2801 if (ret != PAM_SUCCESS) {
2802 _pam_log(ctx, LOG_NOTICE,
2803 "user not authenticated");
2807 lctrl = ctx->ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
2809 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
2810 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
2813 ret = PAM_AUTHTOK_ERR;
2814 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
2816 * use_authtok is to force the use of a previously entered
2817 * password -- needed for pluggable password strength checking
2820 ret = _winbind_read_password(ctx, lctrl,
2822 "Enter new NT password: ",
2823 "Retype new NT password: ",
2824 (const char **)&pass_new);
2826 if (ret != PAM_SUCCESS) {
2827 _pam_log_debug(ctx, LOG_ALERT,
2829 "new password not obtained");
2830 pass_old = NULL;/* tidy up */
2835 * At this point we know who the user is and what they
2836 * propose as their new password. Verify that the new
2837 * password is acceptable.
2840 if (pass_new[0] == '\0') {/* "\0" password = NULL */
2846 * By reaching here we have approved the passwords and must now
2847 * rebuild the password database file.
2849 _pam_get_data(pamh, PAM_WINBIND_PWD_LAST_SET,
2850 &pwdlastset_update);
2853 * if cached creds were enabled, make sure to set the
2854 * WINBIND_CACHED_LOGIN bit here in order to have winbindd
2855 * update the cached creds storage - gd
2858 ctx->ctrl |= WINBIND_CACHED_LOGIN;
2861 ret = winbind_chauthtok_request(ctx, user, pass_old,
2862 pass_new, pwdlastset_update);
2864 _pam_overwrite(pass_new);
2865 _pam_overwrite(pass_old);
2866 pass_old = pass_new = NULL;
2870 if (_pam_require_krb5_auth_after_chauthtok(ctx, user)) {
2872 const char *member = NULL;
2873 const char *cctype = NULL;
2874 int warn_pwd_expire;
2875 struct wbcLogonUserInfo *info = NULL;
2876 struct wbcUserPasswordPolicyInfo *policy = NULL;
2878 member = get_member_from_config(ctx);
2879 cctype = get_krb5_cc_type_from_config(ctx);
2880 warn_pwd_expire = get_warn_pwd_expire_from_config(ctx);
2882 /* Keep WINBIND_CACHED_LOGIN bit for
2883 * authentication after changing the password.
2884 * This will update the cached credentials in case
2885 * that winbindd_dual_pam_chauthtok() fails
2890 ret = winbind_auth_request(ctx, user, pass_new,
2892 &error, &info, &policy,
2893 NULL, &username_ret);
2894 _pam_overwrite(pass_new);
2895 _pam_overwrite(pass_old);
2896 pass_old = pass_new = NULL;
2898 if (ret == PAM_SUCCESS) {
2900 struct wbcAuthUserInfo *user_info = NULL;
2902 if (info && info->info) {
2903 user_info = info->info;
2906 /* warn a user if the password is about to
2908 _pam_warn_password_expiry(ctx, user_info, policy,
2912 /* set some info3 info for other modules in the
2914 _pam_set_data_info3(ctx, user_info);
2916 /* put krb5ccname into env */
2917 _pam_setup_krb5_env(ctx, info);
2920 pam_set_item(pamh, PAM_USER,
2922 _pam_log_debug(ctx, LOG_INFO,
2923 "Returned user was '%s'",
2928 wbcFreeMemory(info);
2929 wbcFreeMemory(policy);
2935 ret = PAM_SERVICE_ERR;
2940 /* Deal with offline errors. */
2942 const char *codes[] = {
2943 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
2944 "NT_STATUS_NO_LOGON_SERVERS",
2945 "NT_STATUS_ACCESS_DENIED"
2948 for (i=0; i<ARRAY_SIZE(codes); i++) {
2950 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
2956 wbcFreeMemory(error);
2958 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", ctx, ret);
2967 /* static module data */
2969 struct pam_module _pam_winbind_modstruct = {
2971 pam_sm_authenticate,
2974 pam_sm_open_session,
2975 pam_sm_close_session,
2982 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
2983 * Copyright (c) Tim Potter <tpot@samba.org> 2000
2984 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
2985 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2008
2986 * Copyright (c) Jan Rêkorajski 1999.
2987 * Copyright (c) Andrew G. Morgan 1996-8.
2988 * Copyright (c) Alex O. Yuriev, 1996.
2989 * Copyright (c) Cristian Gafton 1996.
2990 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
2992 * Redistribution and use in source and binary forms, with or without
2993 * modification, are permitted provided that the following conditions
2995 * 1. Redistributions of source code must retain the above copyright
2996 * notice, and the entire permission notice in its entirety,
2997 * including the disclaimer of warranties.
2998 * 2. Redistributions in binary form must reproduce the above copyright
2999 * notice, this list of conditions and the following disclaimer in the
3000 * documentation and/or other materials provided with the distribution.
3001 * 3. The name of the author may not be used to endorse or promote
3002 * products derived from this software without specific prior
3003 * written permission.
3005 * ALTERNATIVELY, this product may be distributed under the terms of
3006 * the GNU Public License, in which case the provisions of the GPL are
3007 * required INSTEAD OF the above restrictions. (This clause is
3008 * necessary due to a potential bad interaction between the GPL and
3009 * the restrictions contained in a BSD-style copyright.)
3011 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
3012 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3013 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3014 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3015 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3016 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3017 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3018 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3019 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3020 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3021 * OF THE POSSIBILITY OF SUCH DAMAGE.