ldb:pyldb: fix doc string for set_extended_component()
[Samba.git] / nsswitch / pam_winbind.c
blob98cf97e2daa33d346d57aea37922d2e39251a68d
1 /* pam_winbind module
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 #define UID_WRAPPER_NOT_REPLACE
15 #include "pam_winbind.h"
17 static int wbc_error_to_pam_error(wbcErr status)
19 switch (status) {
20 case WBC_ERR_SUCCESS:
21 return PAM_SUCCESS;
22 case WBC_ERR_NOT_IMPLEMENTED:
23 return PAM_SERVICE_ERR;
24 case WBC_ERR_UNKNOWN_FAILURE:
25 break;
26 case WBC_ERR_NO_MEMORY:
27 return PAM_BUF_ERR;
28 case WBC_ERR_INVALID_SID:
29 case WBC_ERR_INVALID_PARAM:
30 break;
31 case WBC_ERR_WINBIND_NOT_AVAILABLE:
32 return PAM_AUTHINFO_UNAVAIL;
33 case WBC_ERR_DOMAIN_NOT_FOUND:
34 return PAM_AUTHINFO_UNAVAIL;
35 case WBC_ERR_INVALID_RESPONSE:
36 return PAM_BUF_ERR;
37 case WBC_ERR_NSS_ERROR:
38 return PAM_USER_UNKNOWN;
39 case WBC_ERR_AUTH_ERROR:
40 return PAM_AUTH_ERR;
41 case WBC_ERR_UNKNOWN_USER:
42 return PAM_USER_UNKNOWN;
43 case WBC_ERR_UNKNOWN_GROUP:
44 return PAM_USER_UNKNOWN;
45 case WBC_ERR_PWD_CHANGE_FAILED:
46 break;
49 /* be paranoid */
50 return PAM_AUTH_ERR;
53 static const char *_pam_error_code_str(int err)
55 switch (err) {
56 case PAM_SUCCESS:
57 return "PAM_SUCCESS";
58 case PAM_OPEN_ERR:
59 return "PAM_OPEN_ERR";
60 case PAM_SYMBOL_ERR:
61 return "PAM_SYMBOL_ERR";
62 case PAM_SERVICE_ERR:
63 return "PAM_SERVICE_ERR";
64 case PAM_SYSTEM_ERR:
65 return "PAM_SYSTEM_ERR";
66 case PAM_BUF_ERR:
67 return "PAM_BUF_ERR";
68 case PAM_PERM_DENIED:
69 return "PAM_PERM_DENIED";
70 case PAM_AUTH_ERR:
71 return "PAM_AUTH_ERR";
72 case PAM_CRED_INSUFFICIENT:
73 return "PAM_CRED_INSUFFICIENT";
74 case PAM_AUTHINFO_UNAVAIL:
75 return "PAM_AUTHINFO_UNAVAIL";
76 case PAM_USER_UNKNOWN:
77 return "PAM_USER_UNKNOWN";
78 case PAM_MAXTRIES:
79 return "PAM_MAXTRIES";
80 case PAM_NEW_AUTHTOK_REQD:
81 return "PAM_NEW_AUTHTOK_REQD";
82 case PAM_ACCT_EXPIRED:
83 return "PAM_ACCT_EXPIRED";
84 case PAM_SESSION_ERR:
85 return "PAM_SESSION_ERR";
86 case PAM_CRED_UNAVAIL:
87 return "PAM_CRED_UNAVAIL";
88 case PAM_CRED_EXPIRED:
89 return "PAM_CRED_EXPIRED";
90 case PAM_CRED_ERR:
91 return "PAM_CRED_ERR";
92 case PAM_NO_MODULE_DATA:
93 return "PAM_NO_MODULE_DATA";
94 case PAM_CONV_ERR:
95 return "PAM_CONV_ERR";
96 case PAM_AUTHTOK_ERR:
97 return "PAM_AUTHTOK_ERR";
98 case PAM_AUTHTOK_RECOVER_ERR:
99 return "PAM_AUTHTOK_RECOVER_ERR";
100 case PAM_AUTHTOK_LOCK_BUSY:
101 return "PAM_AUTHTOK_LOCK_BUSY";
102 case PAM_AUTHTOK_DISABLE_AGING:
103 return "PAM_AUTHTOK_DISABLE_AGING";
104 case PAM_TRY_AGAIN:
105 return "PAM_TRY_AGAIN";
106 case PAM_IGNORE:
107 return "PAM_IGNORE";
108 case PAM_ABORT:
109 return "PAM_ABORT";
110 case PAM_AUTHTOK_EXPIRED:
111 return "PAM_AUTHTOK_EXPIRED";
112 #ifdef PAM_MODULE_UNKNOWN
113 case PAM_MODULE_UNKNOWN:
114 return "PAM_MODULE_UNKNOWN";
115 #endif
116 #ifdef PAM_BAD_ITEM
117 case PAM_BAD_ITEM:
118 return "PAM_BAD_ITEM";
119 #endif
120 #ifdef PAM_CONV_AGAIN
121 case PAM_CONV_AGAIN:
122 return "PAM_CONV_AGAIN";
123 #endif
124 #ifdef PAM_INCOMPLETE
125 case PAM_INCOMPLETE:
126 return "PAM_INCOMPLETE";
127 #endif
128 default:
129 return NULL;
133 #define _PAM_LOG_FUNCTION_ENTER(function, ctx) \
134 do { \
135 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] ENTER: " \
136 function " (flags: 0x%04x)", ctx->pamh, ctx->flags); \
137 _pam_log_state(ctx); \
138 } while (0)
140 #define _PAM_LOG_FUNCTION_LEAVE(function, ctx, retval) \
141 do { \
142 _pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] LEAVE: " \
143 function " returning %d (%s)", ctx->pamh, retval, \
144 _pam_error_code_str(retval)); \
145 _pam_log_state(ctx); \
146 } while (0)
148 /* data tokens */
150 #define MAX_PASSWD_TRIES 3
152 #ifdef HAVE_GETTEXT
153 static char initialized = 0;
155 static inline void textdomain_init(void);
156 static inline void textdomain_init(void)
158 if (!initialized) {
159 bindtextdomain(MODULE_NAME, LOCALEDIR);
160 initialized = 1;
162 return;
164 #endif
167 /* some syslogging */
169 #ifdef HAVE_PAM_VSYSLOG
170 static void _pam_log_int(const pam_handle_t *pamh,
171 int err,
172 const char *format,
173 va_list args)
175 pam_vsyslog(pamh, err, format, args);
177 #else
178 static void _pam_log_int(const pam_handle_t *pamh,
179 int err,
180 const char *format,
181 va_list args)
183 char *format2 = NULL;
184 const char *service;
186 pam_get_item(pamh, PAM_SERVICE, (const void **) &service);
188 format2 = (char *)malloc(strlen(MODULE_NAME)+strlen(format)+strlen(service)+5);
189 if (format2 == NULL) {
190 /* what else todo ? */
191 vsyslog(err, format, args);
192 return;
195 sprintf(format2, "%s(%s): %s", MODULE_NAME, service, format);
196 vsyslog(err, format2, args);
197 SAFE_FREE(format2);
199 #endif /* HAVE_PAM_VSYSLOG */
201 static bool _pam_log_is_silent(int ctrl)
203 return on(ctrl, WINBIND_SILENT);
206 static void _pam_log(struct pwb_context *r, int err, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
207 static void _pam_log(struct pwb_context *r, int err, const char *format, ...)
209 va_list args;
211 if (_pam_log_is_silent(r->ctrl)) {
212 return;
215 va_start(args, format);
216 _pam_log_int(r->pamh, err, format, args);
217 va_end(args);
219 static void __pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
220 static void __pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
222 va_list args;
224 if (_pam_log_is_silent(ctrl)) {
225 return;
228 va_start(args, format);
229 _pam_log_int(pamh, err, format, args);
230 va_end(args);
233 static bool _pam_log_is_debug_enabled(int ctrl)
235 if (ctrl == -1) {
236 return false;
239 if (_pam_log_is_silent(ctrl)) {
240 return false;
243 if (!(ctrl & WINBIND_DEBUG_ARG)) {
244 return false;
247 return true;
250 static bool _pam_log_is_debug_state_enabled(int ctrl)
252 if (!(ctrl & WINBIND_DEBUG_STATE)) {
253 return false;
256 return _pam_log_is_debug_enabled(ctrl);
259 static void _pam_log_debug(struct pwb_context *r, int err, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
260 static void _pam_log_debug(struct pwb_context *r, int err, const char *format, ...)
262 va_list args;
264 if (!_pam_log_is_debug_enabled(r->ctrl)) {
265 return;
268 va_start(args, format);
269 _pam_log_int(r->pamh, err, format, args);
270 va_end(args);
272 static void __pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
273 static void __pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
275 va_list args;
277 if (!_pam_log_is_debug_enabled(ctrl)) {
278 return;
281 va_start(args, format);
282 _pam_log_int(pamh, err, format, args);
283 va_end(args);
286 static void _pam_log_state_datum(struct pwb_context *ctx,
287 int item_type,
288 const char *key,
289 int is_string)
291 const void *data = NULL;
292 if (item_type != 0) {
293 pam_get_item(ctx->pamh, item_type, &data);
294 } else {
295 pam_get_data(ctx->pamh, key, &data);
297 if (data != NULL) {
298 const char *type = (item_type != 0) ? "ITEM" : "DATA";
299 if (is_string != 0) {
300 _pam_log_debug(ctx, LOG_DEBUG,
301 "[pamh: %p] STATE: %s(%s) = \"%s\" (%p)",
302 ctx->pamh, type, key, (const char *)data,
303 data);
304 } else {
305 _pam_log_debug(ctx, LOG_DEBUG,
306 "[pamh: %p] STATE: %s(%s) = %p",
307 ctx->pamh, type, key, data);
312 #define _PAM_LOG_STATE_DATA_POINTER(ctx, module_data_name) \
313 _pam_log_state_datum(ctx, 0, module_data_name, 0)
315 #define _PAM_LOG_STATE_DATA_STRING(ctx, module_data_name) \
316 _pam_log_state_datum(ctx, 0, module_data_name, 1)
318 #define _PAM_LOG_STATE_ITEM_POINTER(ctx, item_type) \
319 _pam_log_state_datum(ctx, item_type, #item_type, 0)
321 #define _PAM_LOG_STATE_ITEM_STRING(ctx, item_type) \
322 _pam_log_state_datum(ctx, item_type, #item_type, 1)
324 #ifdef DEBUG_PASSWORD
325 #define _LOG_PASSWORD_AS_STRING 1
326 #else
327 #define _LOG_PASSWORD_AS_STRING 0
328 #endif
330 #define _PAM_LOG_STATE_ITEM_PASSWORD(ctx, item_type) \
331 _pam_log_state_datum(ctx, item_type, #item_type, \
332 _LOG_PASSWORD_AS_STRING)
334 static void _pam_log_state(struct pwb_context *ctx)
336 if (!_pam_log_is_debug_state_enabled(ctx->ctrl)) {
337 return;
340 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_SERVICE);
341 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_USER);
342 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_TTY);
343 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_RHOST);
344 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_RUSER);
345 _PAM_LOG_STATE_ITEM_PASSWORD(ctx, PAM_OLDAUTHTOK);
346 _PAM_LOG_STATE_ITEM_PASSWORD(ctx, PAM_AUTHTOK);
347 _PAM_LOG_STATE_ITEM_STRING(ctx, PAM_USER_PROMPT);
348 _PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_CONV);
349 #ifdef PAM_FAIL_DELAY
350 _PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_FAIL_DELAY);
351 #endif
352 #ifdef PAM_REPOSITORY
353 _PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_REPOSITORY);
354 #endif
356 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_HOMEDIR);
357 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_LOGONSCRIPT);
358 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_LOGONSERVER);
359 _PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_PROFILEPATH);
360 _PAM_LOG_STATE_DATA_STRING(ctx,
361 PAM_WINBIND_NEW_AUTHTOK_REQD);
362 /* Use atoi to get PAM result code */
363 _PAM_LOG_STATE_DATA_STRING(ctx,
364 PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH);
365 _PAM_LOG_STATE_DATA_POINTER(ctx, PAM_WINBIND_PWD_LAST_SET);
368 static int _pam_parse(const pam_handle_t *pamh,
369 int flags,
370 int argc,
371 const char **argv,
372 dictionary **result_d)
374 int ctrl = 0;
375 const char *config_file = NULL;
376 int i;
377 const char **v;
378 dictionary *d = NULL;
380 if (flags & PAM_SILENT) {
381 ctrl |= WINBIND_SILENT;
384 for (i=argc,v=argv; i-- > 0; ++v) {
385 if (!strncasecmp(*v, "config", strlen("config"))) {
386 ctrl |= WINBIND_CONFIG_FILE;
387 config_file = v[i];
388 break;
392 if (config_file == NULL) {
393 config_file = PAM_WINBIND_CONFIG_FILE;
396 d = iniparser_load(discard_const_p(char, config_file));
397 if (d == NULL) {
398 goto config_from_pam;
401 if (iniparser_getboolean(d, discard_const_p(char, "global:debug"), false)) {
402 ctrl |= WINBIND_DEBUG_ARG;
405 if (iniparser_getboolean(d, discard_const_p(char, "global:debug_state"), false)) {
406 ctrl |= WINBIND_DEBUG_STATE;
409 if (iniparser_getboolean(d, discard_const_p(char, "global:cached_login"), false)) {
410 ctrl |= WINBIND_CACHED_LOGIN;
413 if (iniparser_getboolean(d, discard_const_p(char, "global:krb5_auth"), false)) {
414 ctrl |= WINBIND_KRB5_AUTH;
417 if (iniparser_getboolean(d, discard_const_p(char, "global:silent"), false)) {
418 ctrl |= WINBIND_SILENT;
421 if (iniparser_getstring(d, discard_const_p(char, "global:krb5_ccache_type"), NULL) != NULL) {
422 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
425 if ((iniparser_getstring(d, discard_const_p(char, "global:require-membership-of"), NULL)
426 != NULL) ||
427 (iniparser_getstring(d, discard_const_p(char, "global:require_membership_of"), NULL)
428 != NULL)) {
429 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
432 if (iniparser_getboolean(d, discard_const_p(char, "global:try_first_pass"), false)) {
433 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
436 if (iniparser_getint(d, discard_const_p(char, "global:warn_pwd_expire"), 0)) {
437 ctrl |= WINBIND_WARN_PWD_EXPIRE;
440 if (iniparser_getboolean(d, discard_const_p(char, "global:mkhomedir"), false)) {
441 ctrl |= WINBIND_MKHOMEDIR;
444 config_from_pam:
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;
478 else {
479 __pam_log(pamh, ctrl, LOG_ERR,
480 "pam_parse: unknown option: %s", *v);
481 return -1;
486 if (result_d) {
487 *result_d = d;
488 } else {
489 if (d) {
490 iniparser_freedict(d);
494 return ctrl;
497 static int _pam_winbind_free_context(struct pwb_context *ctx)
499 if (!ctx) {
500 return 0;
503 if (ctx->dict) {
504 iniparser_freedict(ctx->dict);
507 return 0;
510 static int _pam_winbind_init_context(pam_handle_t *pamh,
511 int flags,
512 int argc,
513 const char **argv,
514 struct pwb_context **ctx_p)
516 struct pwb_context *r = NULL;
518 #ifdef HAVE_GETTEXT
519 textdomain_init();
520 #endif
522 r = talloc_zero(NULL, struct pwb_context);
523 if (!r) {
524 return PAM_BUF_ERR;
527 talloc_set_destructor(r, _pam_winbind_free_context);
529 r->pamh = pamh;
530 r->flags = flags;
531 r->argc = argc;
532 r->argv = argv;
533 r->ctrl = _pam_parse(pamh, flags, argc, argv, &r->dict);
534 if (r->ctrl == -1) {
535 TALLOC_FREE(r);
536 return PAM_SYSTEM_ERR;
539 *ctx_p = r;
541 return PAM_SUCCESS;
544 static void _pam_winbind_cleanup_func(pam_handle_t *pamh,
545 void *data,
546 int error_status)
548 int ctrl = _pam_parse(pamh, 0, 0, NULL, NULL);
549 if (_pam_log_is_debug_state_enabled(ctrl)) {
550 __pam_log_debug(pamh, ctrl, LOG_DEBUG,
551 "[pamh: %p] CLEAN: cleaning up PAM data %p "
552 "(error_status = %d)", pamh, data,
553 error_status);
555 TALLOC_FREE(data);
559 static const struct ntstatus_errors {
560 const char *ntstatus_string;
561 const char *error_string;
562 } ntstatus_errors[] = {
563 {"NT_STATUS_OK",
564 N_("Success")},
565 {"NT_STATUS_BACKUP_CONTROLLER",
566 N_("No primary Domain Controler available")},
567 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
568 N_("No domain controllers found")},
569 {"NT_STATUS_NO_LOGON_SERVERS",
570 N_("No logon servers")},
571 {"NT_STATUS_PWD_TOO_SHORT",
572 N_("Password too short")},
573 {"NT_STATUS_PWD_TOO_RECENT",
574 N_("The password of this user is too recent to change")},
575 {"NT_STATUS_PWD_HISTORY_CONFLICT",
576 N_("Password is already in password history")},
577 {"NT_STATUS_PASSWORD_EXPIRED",
578 N_("Your password has expired")},
579 {"NT_STATUS_PASSWORD_MUST_CHANGE",
580 N_("You need to change your password now")},
581 {"NT_STATUS_INVALID_WORKSTATION",
582 N_("You are not allowed to logon from this workstation")},
583 {"NT_STATUS_INVALID_LOGON_HOURS",
584 N_("You are not allowed to logon at this time")},
585 {"NT_STATUS_ACCOUNT_EXPIRED",
586 N_("Your account has expired. "
587 "Please contact your System administrator")}, /* SCNR */
588 {"NT_STATUS_ACCOUNT_DISABLED",
589 N_("Your account is disabled. "
590 "Please contact your System administrator")}, /* SCNR */
591 {"NT_STATUS_ACCOUNT_LOCKED_OUT",
592 N_("Your account has been locked. "
593 "Please contact your System administrator")}, /* SCNR */
594 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
595 N_("Invalid Trust Account")},
596 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
597 N_("Invalid Trust Account")},
598 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
599 N_("Invalid Trust Account")},
600 {"NT_STATUS_ACCESS_DENIED",
601 N_("Access is denied")},
602 {NULL, NULL}
605 static const char *_get_ntstatus_error_string(const char *nt_status_string)
607 int i;
608 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
609 if (!strcasecmp(ntstatus_errors[i].ntstatus_string,
610 nt_status_string)) {
611 return _(ntstatus_errors[i].error_string);
614 return NULL;
617 /* --- authentication management functions --- */
619 /* Attempt a conversation */
621 static int converse(const pam_handle_t *pamh,
622 int nargs,
623 struct pam_message **message,
624 struct pam_response **response)
626 int retval;
627 struct pam_conv *conv;
629 retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
630 if (retval == PAM_SUCCESS) {
631 retval = conv->conv(nargs,
632 (const struct pam_message **)message,
633 response, conv->appdata_ptr);
636 return retval; /* propagate error status */
640 static int _make_remark(struct pwb_context *ctx,
641 int type,
642 const char *text)
644 int retval = PAM_SUCCESS;
646 struct pam_message *pmsg[1], msg[1];
647 struct pam_response *resp;
649 if (ctx->flags & WINBIND_SILENT) {
650 return PAM_SUCCESS;
653 pmsg[0] = &msg[0];
654 msg[0].msg = discard_const_p(char, text);
655 msg[0].msg_style = type;
657 resp = NULL;
658 retval = converse(ctx->pamh, 1, pmsg, &resp);
660 if (resp) {
661 _pam_drop_reply(resp, 1);
663 return retval;
666 static int _make_remark_v(struct pwb_context *ctx,
667 int type,
668 const char *format,
669 va_list args)
671 char *var;
672 int ret;
674 ret = vasprintf(&var, format, args);
675 if (ret < 0) {
676 _pam_log(ctx, LOG_ERR, "memory allocation failure");
677 return ret;
680 ret = _make_remark(ctx, type, var);
681 SAFE_FREE(var);
682 return ret;
685 static int _make_remark_format(struct pwb_context *ctx, int type, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
686 static int _make_remark_format(struct pwb_context *ctx, int type, const char *format, ...)
688 int ret;
689 va_list args;
691 va_start(args, format);
692 ret = _make_remark_v(ctx, type, format, args);
693 va_end(args);
694 return ret;
697 static int pam_winbind_request_log(struct pwb_context *ctx,
698 int retval,
699 const char *user,
700 const char *fn)
702 switch (retval) {
703 case PAM_AUTH_ERR:
704 /* incorrect password */
705 _pam_log(ctx, LOG_WARNING, "user '%s' denied access "
706 "(incorrect password or invalid membership)", user);
707 return retval;
708 case PAM_ACCT_EXPIRED:
709 /* account expired */
710 _pam_log(ctx, LOG_WARNING, "user '%s' account expired",
711 user);
712 return retval;
713 case PAM_AUTHTOK_EXPIRED:
714 /* password expired */
715 _pam_log(ctx, LOG_WARNING, "user '%s' password expired",
716 user);
717 return retval;
718 case PAM_NEW_AUTHTOK_REQD:
719 /* new password required */
720 _pam_log(ctx, LOG_WARNING, "user '%s' new password "
721 "required", user);
722 return retval;
723 case PAM_USER_UNKNOWN:
724 /* the user does not exist */
725 _pam_log_debug(ctx, LOG_NOTICE, "user '%s' not found",
726 user);
727 if (ctx->ctrl & WINBIND_UNKNOWN_OK_ARG) {
728 return PAM_IGNORE;
730 return retval;
731 case PAM_SUCCESS:
732 /* Otherwise, the authentication looked good */
733 if (strcmp(fn, "wbcLogonUser") == 0) {
734 _pam_log(ctx, LOG_NOTICE,
735 "user '%s' granted access", user);
736 } else {
737 _pam_log(ctx, LOG_NOTICE,
738 "user '%s' OK", user);
740 return retval;
741 default:
742 /* we don't know anything about this return value */
743 _pam_log(ctx, LOG_ERR,
744 "internal module error (retval = %s(%d), user = '%s')",
745 _pam_error_code_str(retval), retval, user);
746 return retval;
750 static int wbc_auth_error_to_pam_error(struct pwb_context *ctx,
751 struct wbcAuthErrorInfo *e,
752 wbcErr status,
753 const char *username,
754 const char *fn)
756 int ret = PAM_AUTH_ERR;
758 if (WBC_ERROR_IS_OK(status)) {
759 _pam_log_debug(ctx, LOG_DEBUG, "request %s succeeded",
760 fn);
761 ret = PAM_SUCCESS;
762 return pam_winbind_request_log(ctx, ret, username, fn);
765 if (e) {
766 if (e->pam_error != PAM_SUCCESS) {
767 _pam_log(ctx, LOG_ERR,
768 "request %s failed: %s, "
769 "PAM error: %s (%d), NTSTATUS: %s, "
770 "Error message was: %s",
772 wbcErrorString(status),
773 _pam_error_code_str(e->pam_error),
774 e->pam_error,
775 e->nt_string,
776 e->display_string);
777 ret = e->pam_error;
778 return pam_winbind_request_log(ctx, ret, username, fn);
781 _pam_log(ctx, LOG_ERR, "request %s failed, but PAM error 0!", fn);
783 ret = PAM_SERVICE_ERR;
784 return pam_winbind_request_log(ctx, ret, username, fn);
787 ret = wbc_error_to_pam_error(status);
788 return pam_winbind_request_log(ctx, ret, username, fn);
791 #if defined(HAVE_PAM_RADIO_TYPE)
792 static bool _pam_winbind_change_pwd(struct pwb_context *ctx)
794 struct pam_message msg, *pmsg;
795 struct pam_response *resp = NULL;
796 int ret;
797 bool retval = false;
798 pmsg = &msg;
799 msg.msg_style = PAM_RADIO_TYPE;
800 msg.msg = _("Do you want to change your password now?");
801 ret = converse(ctx->pamh, 1, &pmsg, &resp);
802 if (resp == NULL) {
803 if (ret == PAM_SUCCESS) {
804 _pam_log(ctx, LOG_CRIT, "pam_winbind: system error!\n");
805 return false;
808 if (ret != PAM_SUCCESS) {
809 return false;
811 _pam_log(ctx, LOG_CRIT, "Received [%s] reply from application.\n", resp->resp);
813 if ((resp->resp != NULL) && (strcasecmp(resp->resp, "yes") == 0)) {
814 retval = true;
817 _pam_drop_reply(resp, 1);
818 return retval;
820 #else
821 static bool _pam_winbind_change_pwd(struct pwb_context *ctx)
823 return false;
825 #endif
828 * send a password expiry message if required
830 * @param ctx PAM winbind context.
831 * @param next_change expected (calculated) next expiry date.
832 * @param already_expired pointer to a boolean to indicate if the password is
833 * already expired.
835 * @return boolean Returns true if message has been sent, false if not.
838 static bool _pam_send_password_expiry_message(struct pwb_context *ctx,
839 time_t next_change,
840 time_t now,
841 int warn_pwd_expire,
842 bool *already_expired,
843 bool *change_pwd)
845 int days = 0;
846 struct tm tm_now, tm_next_change;
847 bool retval = false;
848 int ret;
850 if (already_expired) {
851 *already_expired = false;
854 if (change_pwd) {
855 *change_pwd = false;
858 if (next_change <= now) {
859 PAM_WB_REMARK_DIRECT(ctx, "NT_STATUS_PASSWORD_EXPIRED");
860 if (already_expired) {
861 *already_expired = true;
863 return true;
866 if ((next_change < 0) ||
867 (next_change > now + warn_pwd_expire * SECONDS_PER_DAY)) {
868 return false;
871 if ((localtime_r(&now, &tm_now) == NULL) ||
872 (localtime_r(&next_change, &tm_next_change) == NULL)) {
873 return false;
876 days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) -
877 (tm_now.tm_yday+tm_now.tm_year*365);
879 if (days == 0) {
880 ret = _make_remark(ctx, PAM_TEXT_INFO,
881 _("Your password expires today.\n"));
884 * If change_pwd and already_expired is null.
885 * We are just sending a notification message.
886 * We don't expect any response in this case.
889 if (!change_pwd && !already_expired) {
890 return true;
894 * successfully sent the warning message.
895 * Give the user a chance to change pwd.
897 if (ret == PAM_SUCCESS) {
898 if (change_pwd) {
899 retval = _pam_winbind_change_pwd(ctx);
900 if (retval) {
901 *change_pwd = true;
905 return true;
908 if (days > 0 && days < warn_pwd_expire) {
910 ret = _make_remark_format(ctx, PAM_TEXT_INFO,
911 _("Your password will expire in %d %s.\n"),
912 days, (days > 1) ? _("days"):_("day"));
914 * If change_pwd and already_expired is null.
915 * We are just sending a notification message.
916 * We don't expect any response in this case.
919 if (!change_pwd && !already_expired) {
920 return true;
924 * successfully sent the warning message.
925 * Give the user a chance to change pwd.
927 if (ret == PAM_SUCCESS) {
928 if (change_pwd) {
929 retval = _pam_winbind_change_pwd(ctx);
930 if (retval) {
931 *change_pwd = true;
935 return true;
938 return false;
942 * Send a warning if the password expires in the near future
944 * @param ctx PAM winbind context.
945 * @param response The full authentication response structure.
946 * @param already_expired boolean, is the pwd already expired?
948 * @return void.
951 static void _pam_warn_password_expiry(struct pwb_context *ctx,
952 const struct wbcAuthUserInfo *info,
953 const struct wbcUserPasswordPolicyInfo *policy,
954 int warn_pwd_expire,
955 bool *already_expired,
956 bool *change_pwd)
958 time_t now = time(NULL);
959 time_t next_change = 0;
961 if (!info || !policy) {
962 return;
965 if (already_expired) {
966 *already_expired = false;
969 if (change_pwd) {
970 *change_pwd = false;
973 /* accounts with WBC_ACB_PWNOEXP set never receive a warning */
974 if (info->acct_flags & WBC_ACB_PWNOEXP) {
975 return;
978 /* no point in sending a warning if this is a grace logon */
979 if (PAM_WB_GRACE_LOGON(info->user_flags)) {
980 return;
983 /* check if the info3 must change timestamp has been set */
984 next_change = info->pass_must_change_time;
986 if (_pam_send_password_expiry_message(ctx, next_change, now,
987 warn_pwd_expire,
988 already_expired,
989 change_pwd)) {
990 return;
993 /* now check for the global password policy */
994 /* good catch from Ralf Haferkamp: an expiry of "never" is translated
995 * to -1 */
996 if ((policy->expire == (int64_t)-1) ||
997 (policy->expire == 0)) {
998 return;
1001 next_change = info->pass_last_set_time + policy->expire;
1003 if (_pam_send_password_expiry_message(ctx, next_change, now,
1004 warn_pwd_expire,
1005 already_expired,
1006 change_pwd)) {
1007 return;
1010 /* no warning sent */
1013 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
1016 * Append a string, making sure not to overflow and to always return a
1017 * NULL-terminated string.
1019 * @param dest Destination string buffer (must already be NULL-terminated).
1020 * @param src Source string buffer.
1021 * @param dest_buffer_size Size of dest buffer in bytes.
1023 * @return false if dest buffer is not big enough (no bytes copied), true on
1024 * success.
1027 static bool safe_append_string(char *dest,
1028 const char *src,
1029 int dest_buffer_size)
1031 int dest_length = strlen(dest);
1032 int src_length = strlen(src);
1034 if (dest_length + src_length + 1 > dest_buffer_size) {
1035 return false;
1038 memcpy(dest + dest_length, src, src_length + 1);
1039 return true;
1043 * Convert a names into a SID string, appending it to a buffer.
1045 * @param ctx PAM winbind context.
1046 * @param user User in PAM request.
1047 * @param name Name to convert.
1048 * @param sid_list_buffer Where to append the string sid.
1049 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1051 * @return false on failure, true on success.
1053 static bool winbind_name_to_sid_string(struct pwb_context *ctx,
1054 const char *user,
1055 const char *name,
1056 char *sid_list_buffer,
1057 int sid_list_buffer_size)
1059 char sid_string[WBC_SID_STRING_BUFLEN];
1061 /* lookup name? */
1062 if (IS_SID_STRING(name)) {
1063 strlcpy(sid_string, name, sizeof(sid_string));
1064 } else {
1065 wbcErr wbc_status;
1066 struct wbcDomainSid sid;
1067 enum wbcSidType type;
1069 _pam_log_debug(ctx, LOG_DEBUG,
1070 "no sid given, looking up: %s\n", name);
1072 wbc_status = wbcLookupName("", name, &sid, &type);
1073 if (!WBC_ERROR_IS_OK(wbc_status)) {
1074 _pam_log(ctx, LOG_INFO,
1075 "could not lookup name: %s\n", name);
1076 return false;
1079 wbcSidToStringBuf(&sid, sid_string, sizeof(sid_string));
1082 if (!safe_append_string(sid_list_buffer, sid_string,
1083 sid_list_buffer_size)) {
1084 return false;
1086 return true;
1090 * Convert a list of names into a list of sids.
1092 * @param ctx PAM winbind context.
1093 * @param user User in PAM request.
1094 * @param name_list List of names or string sids, separated by commas.
1095 * @param sid_list_buffer Where to put the list of string sids.
1096 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
1098 * @return false on failure, true on success.
1100 static bool winbind_name_list_to_sid_string_list(struct pwb_context *ctx,
1101 const char *user,
1102 const char *name_list,
1103 char *sid_list_buffer,
1104 int sid_list_buffer_size)
1106 bool result = false;
1107 char *current_name = NULL;
1108 const char *search_location;
1109 const char *comma;
1110 int len;
1112 if (sid_list_buffer_size > 0) {
1113 sid_list_buffer[0] = 0;
1116 search_location = name_list;
1117 while ((comma = strchr(search_location, ',')) != NULL) {
1118 current_name = strndup(search_location,
1119 comma - search_location);
1120 if (NULL == current_name) {
1121 goto out;
1124 if (!winbind_name_to_sid_string(ctx, user,
1125 current_name,
1126 sid_list_buffer,
1127 sid_list_buffer_size)) {
1129 * If one group name failed, we must not fail
1130 * the authentication totally, continue with
1131 * the following group names. If user belongs to
1132 * one of the valid groups, we must allow it
1133 * login. -- BoYang
1136 _pam_log(ctx, LOG_INFO, "cannot convert group %s to sid, "
1137 "check if group %s is valid group.", current_name,
1138 current_name);
1139 _make_remark_format(ctx, PAM_TEXT_INFO, _("Cannot convert group %s "
1140 "to sid, please contact your administrator to see "
1141 "if group %s is valid."), current_name, current_name);
1142 SAFE_FREE(current_name);
1143 search_location = comma + 1;
1144 continue;
1147 SAFE_FREE(current_name);
1149 if (!safe_append_string(sid_list_buffer, ",",
1150 sid_list_buffer_size)) {
1151 goto out;
1154 search_location = comma + 1;
1157 if (!winbind_name_to_sid_string(ctx, user, search_location,
1158 sid_list_buffer,
1159 sid_list_buffer_size)) {
1160 _pam_log(ctx, LOG_INFO, "cannot convert group %s to sid, "
1161 "check if group %s is valid group.", search_location,
1162 search_location);
1163 _make_remark_format(ctx, PAM_TEXT_INFO, _("Cannot convert group %s "
1164 "to sid, please contact your administrator to see "
1165 "if group %s is valid."), search_location, search_location);
1167 /* If no valid groups were converted we should fail outright */
1168 if (name_list != NULL && strlen(sid_list_buffer) == 0) {
1169 result = false;
1170 goto out;
1173 * The lookup of the last name failed..
1174 * It results in require_member_of_sid ends with ','
1175 * It is malformated parameter here, overwrite the last ','.
1177 len = strlen(sid_list_buffer);
1178 if ((len != 0) && (sid_list_buffer[len - 1] == ',')) {
1179 sid_list_buffer[len - 1] = '\0';
1183 result = true;
1185 out:
1186 SAFE_FREE(current_name);
1187 return result;
1191 * put krb5ccname variable into environment
1193 * @param ctx PAM winbind context.
1194 * @param krb5ccname env variable retrieved from winbindd.
1196 * @return void.
1199 static void _pam_setup_krb5_env(struct pwb_context *ctx,
1200 struct wbcLogonUserInfo *info)
1202 char *var = NULL;
1203 int ret;
1204 uint32_t i;
1205 const char *krb5ccname = NULL;
1207 if (off(ctx->ctrl, WINBIND_KRB5_AUTH)) {
1208 return;
1211 if (!info) {
1212 return;
1215 for (i=0; i < info->num_blobs; i++) {
1216 if (strcasecmp(info->blobs[i].name, "krb5ccname") == 0) {
1217 krb5ccname = (const char *)info->blobs[i].blob.data;
1218 break;
1222 if (!krb5ccname || (strlen(krb5ccname) == 0)) {
1223 return;
1226 _pam_log_debug(ctx, LOG_DEBUG,
1227 "request returned KRB5CCNAME: %s", krb5ccname);
1229 if (asprintf(&var, "KRB5CCNAME=%s", krb5ccname) == -1) {
1230 return;
1233 ret = pam_putenv(ctx->pamh, var);
1234 if (ret) {
1235 _pam_log(ctx, LOG_ERR,
1236 "failed to set KRB5CCNAME to %s: %s",
1237 var, pam_strerror(ctx->pamh, ret));
1239 free(var);
1243 * Copy unix username if available (further processed in PAM).
1245 * @param ctx PAM winbind context
1246 * @param user_ret A pointer that holds a pointer to a string
1247 * @param unix_username A username
1249 * @return void.
1252 static void _pam_setup_unix_username(struct pwb_context *ctx,
1253 char **user_ret,
1254 struct wbcLogonUserInfo *info)
1256 const char *unix_username = NULL;
1257 uint32_t i;
1259 if (!user_ret || !info) {
1260 return;
1263 for (i=0; i < info->num_blobs; i++) {
1264 if (strcasecmp(info->blobs[i].name, "unix_username") == 0) {
1265 unix_username = (const char *)info->blobs[i].blob.data;
1266 break;
1270 if (!unix_username || !unix_username[0]) {
1271 return;
1274 *user_ret = strdup(unix_username);
1278 * Set string into the PAM stack.
1280 * @param ctx PAM winbind context.
1281 * @param data_name Key name for pam_set_data.
1282 * @param value String value.
1284 * @return void.
1287 static void _pam_set_data_string(struct pwb_context *ctx,
1288 const char *data_name,
1289 const char *value)
1291 int ret;
1293 if (!data_name || !value || (strlen(data_name) == 0) ||
1294 (strlen(value) == 0)) {
1295 return;
1298 ret = pam_set_data(ctx->pamh, data_name, talloc_strdup(NULL, value),
1299 _pam_winbind_cleanup_func);
1300 if (ret) {
1301 _pam_log_debug(ctx, LOG_DEBUG,
1302 "Could not set data %s: %s\n",
1303 data_name, pam_strerror(ctx->pamh, ret));
1308 * Set info3 strings into the PAM stack.
1310 * @param ctx PAM winbind context.
1311 * @param data_name Key name for pam_set_data.
1312 * @param value String value.
1314 * @return void.
1317 static void _pam_set_data_info3(struct pwb_context *ctx,
1318 const struct wbcAuthUserInfo *info)
1320 _pam_set_data_string(ctx, PAM_WINBIND_HOMEDIR,
1321 info->home_directory);
1322 _pam_set_data_string(ctx, PAM_WINBIND_LOGONSCRIPT,
1323 info->logon_script);
1324 _pam_set_data_string(ctx, PAM_WINBIND_LOGONSERVER,
1325 info->logon_server);
1326 _pam_set_data_string(ctx, PAM_WINBIND_PROFILEPATH,
1327 info->profile_path);
1331 * Free info3 strings in the PAM stack.
1333 * @param pamh PAM handle
1335 * @return void.
1338 static void _pam_free_data_info3(pam_handle_t *pamh)
1340 pam_set_data(pamh, PAM_WINBIND_HOMEDIR, NULL, NULL);
1341 pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT, NULL, NULL);
1342 pam_set_data(pamh, PAM_WINBIND_LOGONSERVER, NULL, NULL);
1343 pam_set_data(pamh, PAM_WINBIND_PROFILEPATH, NULL, NULL);
1347 * Send PAM_ERROR_MSG for cached or grace logons.
1349 * @param ctx PAM winbind context.
1350 * @param username User in PAM request.
1351 * @param info3_user_flgs Info3 flags containing logon type bits.
1353 * @return void.
1356 static void _pam_warn_logon_type(struct pwb_context *ctx,
1357 const char *username,
1358 uint32_t info3_user_flgs)
1360 /* inform about logon type */
1361 if (PAM_WB_GRACE_LOGON(info3_user_flgs)) {
1363 _make_remark(ctx, PAM_ERROR_MSG,
1364 _("Grace login. "
1365 "Please change your password as soon you're "
1366 "online again"));
1367 _pam_log_debug(ctx, LOG_DEBUG,
1368 "User %s logged on using grace logon\n",
1369 username);
1371 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs)) {
1373 _make_remark(ctx, PAM_ERROR_MSG,
1374 _("Domain Controller unreachable, "
1375 "using cached credentials instead. "
1376 "Network resources may be unavailable"));
1377 _pam_log_debug(ctx, LOG_DEBUG,
1378 "User %s logged on using cached credentials\n",
1379 username);
1384 * Send PAM_ERROR_MSG for krb5 errors.
1386 * @param ctx PAM winbind context.
1387 * @param username User in PAM request.
1388 * @param info3_user_flgs Info3 flags containing logon type bits.
1390 * @return void.
1393 static void _pam_warn_krb5_failure(struct pwb_context *ctx,
1394 const char *username,
1395 uint32_t info3_user_flgs)
1397 if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs)) {
1398 _make_remark(ctx, PAM_ERROR_MSG,
1399 _("Failed to establish your Kerberos Ticket cache "
1400 "due time differences\n"
1401 "with the domain controller. "
1402 "Please verify the system time.\n"));
1403 _pam_log_debug(ctx, LOG_DEBUG,
1404 "User %s: Clock skew when getting Krb5 TGT\n",
1405 username);
1409 static bool _pam_check_remark_auth_err(struct pwb_context *ctx,
1410 const struct wbcAuthErrorInfo *e,
1411 const char *nt_status_string,
1412 int *pam_err)
1414 const char *ntstatus = NULL;
1415 const char *error_string = NULL;
1417 if (!e || !pam_err) {
1418 return false;
1421 ntstatus = e->nt_string;
1422 if (!ntstatus) {
1423 return false;
1426 if (strcasecmp(ntstatus, nt_status_string) == 0) {
1428 error_string = _get_ntstatus_error_string(nt_status_string);
1429 if (error_string) {
1430 _make_remark(ctx, PAM_ERROR_MSG, error_string);
1431 *pam_err = e->pam_error;
1432 return true;
1435 if (e->display_string) {
1436 _make_remark(ctx, PAM_ERROR_MSG, _(e->display_string));
1437 *pam_err = e->pam_error;
1438 return true;
1441 _make_remark(ctx, PAM_ERROR_MSG, nt_status_string);
1442 *pam_err = e->pam_error;
1444 return true;
1447 return false;
1451 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
1453 * @param i The wbcUserPasswordPolicyInfo struct.
1455 * @return string (caller needs to talloc_free).
1458 static char *_pam_compose_pwd_restriction_string(struct pwb_context *ctx,
1459 struct wbcUserPasswordPolicyInfo *i)
1461 char *str = NULL;
1463 if (!i) {
1464 goto failed;
1467 str = talloc_asprintf(ctx, _("Your password "));
1468 if (!str) {
1469 goto failed;
1472 if (i->min_length_password > 0) {
1473 str = talloc_asprintf_append(str,
1474 _("must be at least %d characters; "),
1475 i->min_length_password);
1476 if (!str) {
1477 goto failed;
1481 if (i->password_history > 0) {
1482 str = talloc_asprintf_append(str,
1483 _("cannot repeat any of your previous %d "
1484 "passwords; "),
1485 i->password_history);
1486 if (!str) {
1487 goto failed;
1491 if (i->password_properties & WBC_DOMAIN_PASSWORD_COMPLEX) {
1492 str = talloc_asprintf_append(str,
1493 _("must contain capitals, numerals "
1494 "or punctuation; "
1495 "and cannot contain your account "
1496 "or full name; "));
1497 if (!str) {
1498 goto failed;
1502 str = talloc_asprintf_append(str,
1503 _("Please type a different password. "
1504 "Type a password which meets these requirements in "
1505 "both text boxes."));
1506 if (!str) {
1507 goto failed;
1510 return str;
1512 failed:
1513 TALLOC_FREE(str);
1514 return NULL;
1517 static int _pam_create_homedir(struct pwb_context *ctx,
1518 const char *dirname,
1519 mode_t mode)
1521 struct stat sbuf;
1523 if (stat(dirname, &sbuf) == 0) {
1524 return PAM_SUCCESS;
1527 if (mkdir(dirname, mode) != 0) {
1529 _make_remark_format(ctx, PAM_TEXT_INFO,
1530 _("Creating directory: %s failed: %s"),
1531 dirname, strerror(errno));
1532 _pam_log(ctx, LOG_ERR, "could not create dir: %s (%s)",
1533 dirname, strerror(errno));
1534 return PAM_PERM_DENIED;
1537 return PAM_SUCCESS;
1540 static int _pam_chown_homedir(struct pwb_context *ctx,
1541 const char *dirname,
1542 uid_t uid,
1543 gid_t gid)
1545 if (chown(dirname, uid, gid) != 0) {
1546 _pam_log(ctx, LOG_ERR, "failed to chown user homedir: %s (%s)",
1547 dirname, strerror(errno));
1548 return PAM_PERM_DENIED;
1551 return PAM_SUCCESS;
1554 static int _pam_mkhomedir(struct pwb_context *ctx)
1556 struct passwd *pwd = NULL;
1557 char *token = NULL;
1558 char *create_dir = NULL;
1559 char *user_dir = NULL;
1560 int ret;
1561 const char *username;
1562 mode_t mode = 0700;
1563 char *safe_ptr = NULL;
1564 char *p = NULL;
1566 /* Get the username */
1567 ret = pam_get_user(ctx->pamh, &username, NULL);
1568 if ((ret != PAM_SUCCESS) || (!username)) {
1569 _pam_log_debug(ctx, LOG_DEBUG, "can not get the username");
1570 return PAM_SERVICE_ERR;
1573 pwd = getpwnam(username);
1574 if (pwd == NULL) {
1575 _pam_log_debug(ctx, LOG_DEBUG, "can not get the username");
1576 return PAM_USER_UNKNOWN;
1578 _pam_log_debug(ctx, LOG_DEBUG, "homedir is: %s", pwd->pw_dir);
1580 ret = _pam_create_homedir(ctx, pwd->pw_dir, 0700);
1581 if (ret == PAM_SUCCESS) {
1582 ret = _pam_chown_homedir(ctx, pwd->pw_dir,
1583 pwd->pw_uid,
1584 pwd->pw_gid);
1587 if (ret == PAM_SUCCESS) {
1588 return ret;
1591 /* maybe we need to create parent dirs */
1592 create_dir = talloc_strdup(ctx, "/");
1593 if (!create_dir) {
1594 return PAM_BUF_ERR;
1597 /* find final directory */
1598 user_dir = strrchr(pwd->pw_dir, '/');
1599 if (!user_dir) {
1600 return PAM_BUF_ERR;
1602 user_dir++;
1604 _pam_log(ctx, LOG_DEBUG, "final directory: %s", user_dir);
1606 p = pwd->pw_dir;
1608 while ((token = strtok_r(p, "/", &safe_ptr)) != NULL) {
1610 mode = 0755;
1612 p = NULL;
1614 _pam_log_debug(ctx, LOG_DEBUG, "token is %s", token);
1616 create_dir = talloc_asprintf_append(create_dir, "%s/", token);
1617 if (!create_dir) {
1618 return PAM_BUF_ERR;
1620 _pam_log_debug(ctx, LOG_DEBUG, "current_dir is %s", create_dir);
1622 if (strcmp(token, user_dir) == 0) {
1623 _pam_log_debug(ctx, LOG_DEBUG, "assuming last directory: %s", token);
1624 mode = 0700;
1627 ret = _pam_create_homedir(ctx, create_dir, mode);
1628 if (ret) {
1629 return ret;
1633 return _pam_chown_homedir(ctx, create_dir,
1634 pwd->pw_uid,
1635 pwd->pw_gid);
1638 /* talk to winbindd */
1639 static int winbind_auth_request(struct pwb_context *ctx,
1640 const char *user,
1641 const char *pass,
1642 const char *member,
1643 const char *cctype,
1644 const int warn_pwd_expire,
1645 struct wbcAuthErrorInfo **p_error,
1646 struct wbcLogonUserInfo **p_info,
1647 struct wbcUserPasswordPolicyInfo **p_policy,
1648 time_t *pwd_last_set,
1649 char **user_ret)
1651 wbcErr wbc_status;
1653 struct wbcLogonUserParams logon;
1654 char membership_of[1024];
1655 uid_t user_uid = -1;
1656 uint32_t flags = WBFLAG_PAM_INFO3_TEXT |
1657 WBFLAG_PAM_GET_PWD_POLICY;
1659 struct wbcLogonUserInfo *info = NULL;
1660 struct wbcAuthUserInfo *user_info = NULL;
1661 struct wbcAuthErrorInfo *error = NULL;
1662 struct wbcUserPasswordPolicyInfo *policy = NULL;
1664 int ret = PAM_AUTH_ERR;
1665 int i;
1666 const char *codes[] = {
1667 "NT_STATUS_PASSWORD_EXPIRED",
1668 "NT_STATUS_PASSWORD_MUST_CHANGE",
1669 "NT_STATUS_INVALID_WORKSTATION",
1670 "NT_STATUS_INVALID_LOGON_HOURS",
1671 "NT_STATUS_ACCOUNT_EXPIRED",
1672 "NT_STATUS_ACCOUNT_DISABLED",
1673 "NT_STATUS_ACCOUNT_LOCKED_OUT",
1674 "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
1675 "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
1676 "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
1677 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1678 "NT_STATUS_NO_LOGON_SERVERS",
1679 "NT_STATUS_WRONG_PASSWORD",
1680 "NT_STATUS_ACCESS_DENIED"
1683 if (pwd_last_set) {
1684 *pwd_last_set = 0;
1687 /* Krb5 auth always has to go against the KDC of the user's realm */
1689 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1690 flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
1693 if (ctx->ctrl & (WINBIND_KRB5_AUTH|WINBIND_CACHED_LOGIN)) {
1694 struct passwd *pwd = NULL;
1696 pwd = getpwnam(user);
1697 if (pwd == NULL) {
1698 return PAM_USER_UNKNOWN;
1700 user_uid = pwd->pw_uid;
1703 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1705 _pam_log_debug(ctx, LOG_DEBUG,
1706 "enabling krb5 login flag\n");
1708 flags |= WBFLAG_PAM_KRB5 |
1709 WBFLAG_PAM_FALLBACK_AFTER_KRB5;
1712 if (ctx->ctrl & WINBIND_CACHED_LOGIN) {
1713 _pam_log_debug(ctx, LOG_DEBUG,
1714 "enabling cached login flag\n");
1715 flags |= WBFLAG_PAM_CACHED_LOGIN;
1718 if (user_ret) {
1719 *user_ret = NULL;
1720 flags |= WBFLAG_PAM_UNIX_NAME;
1723 if (cctype != NULL) {
1724 _pam_log_debug(ctx, LOG_DEBUG,
1725 "enabling request for a %s krb5 ccache\n",
1726 cctype);
1729 if (member != NULL) {
1731 ZERO_STRUCT(membership_of);
1733 if (!winbind_name_list_to_sid_string_list(ctx, user, member,
1734 membership_of,
1735 sizeof(membership_of))) {
1736 _pam_log_debug(ctx, LOG_ERR,
1737 "failed to serialize membership of sid "
1738 "\"%s\"\n", member);
1739 return PAM_AUTH_ERR;
1743 ZERO_STRUCT(logon);
1745 logon.username = user;
1746 logon.password = pass;
1748 if (cctype) {
1749 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1750 &logon.blobs,
1751 "krb5_cc_type",
1753 discard_const_p(uint8_t, cctype),
1754 strlen(cctype)+1);
1755 if (!WBC_ERROR_IS_OK(wbc_status)) {
1756 goto done;
1760 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1761 &logon.blobs,
1762 "flags",
1764 (uint8_t *)&flags,
1765 sizeof(flags));
1766 if (!WBC_ERROR_IS_OK(wbc_status)) {
1767 goto done;
1770 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1771 &logon.blobs,
1772 "user_uid",
1774 (uint8_t *)&user_uid,
1775 sizeof(user_uid));
1776 if (!WBC_ERROR_IS_OK(wbc_status)) {
1777 goto done;
1780 if (member) {
1781 wbc_status = wbcAddNamedBlob(&logon.num_blobs,
1782 &logon.blobs,
1783 "membership_of",
1785 (uint8_t *)membership_of,
1786 sizeof(membership_of));
1787 if (!WBC_ERROR_IS_OK(wbc_status)) {
1788 goto done;
1792 wbc_status = wbcLogonUser(&logon, &info, &error, &policy);
1793 ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
1794 user, "wbcLogonUser");
1795 wbcFreeMemory(logon.blobs);
1796 logon.blobs = NULL;
1798 if (info && info->info) {
1799 user_info = info->info;
1802 if (pwd_last_set && user_info) {
1803 *pwd_last_set = user_info->pass_last_set_time;
1806 if (p_info && info) {
1807 *p_info = info;
1810 if (p_policy && policy) {
1811 *p_policy = policy;
1814 if (p_error && error) {
1815 /* We want to process the error in the caller. */
1816 *p_error = error;
1817 return ret;
1820 for (i=0; i<ARRAY_SIZE(codes); i++) {
1821 int _ret = ret;
1822 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
1823 ret = _ret;
1824 goto done;
1828 if ((ret == PAM_SUCCESS) && user_info && policy && info) {
1830 bool already_expired = false;
1831 bool change_pwd = false;
1833 /* warn a user if the password is about to expire soon */
1834 _pam_warn_password_expiry(ctx, user_info, policy,
1835 warn_pwd_expire,
1836 &already_expired,
1837 &change_pwd);
1839 if (already_expired == true) {
1841 SMB_TIME_T last_set = user_info->pass_last_set_time;
1843 _pam_log_debug(ctx, LOG_DEBUG,
1844 "Password has expired "
1845 "(Password was last set: %lld, "
1846 "the policy says it should expire here "
1847 "%lld (now it's: %ld))\n",
1848 (long long int)last_set,
1849 (long long int)last_set +
1850 policy->expire,
1851 (long)time(NULL));
1853 return PAM_AUTHTOK_EXPIRED;
1856 if (change_pwd) {
1857 ret = PAM_NEW_AUTHTOK_REQD;
1858 goto done;
1861 /* inform about logon type */
1862 _pam_warn_logon_type(ctx, user, user_info->user_flags);
1864 /* inform about krb5 failures */
1865 _pam_warn_krb5_failure(ctx, user, user_info->user_flags);
1867 /* set some info3 info for other modules in the stack */
1868 _pam_set_data_info3(ctx, user_info);
1870 /* put krb5ccname into env */
1871 _pam_setup_krb5_env(ctx, info);
1873 /* If winbindd returned a username, return the pointer to it
1874 * here. */
1875 _pam_setup_unix_username(ctx, user_ret, info);
1878 done:
1879 wbcFreeMemory(logon.blobs);
1880 if (info && info->blobs && !p_info) {
1881 wbcFreeMemory(info->blobs);
1883 if (error && !p_error) {
1884 wbcFreeMemory(error);
1886 if (info && !p_info) {
1887 wbcFreeMemory(info);
1889 if (policy && !p_policy) {
1890 wbcFreeMemory(policy);
1893 return ret;
1896 /* talk to winbindd */
1897 static int winbind_chauthtok_request(struct pwb_context *ctx,
1898 const char *user,
1899 const char *oldpass,
1900 const char *newpass,
1901 time_t pwd_last_set)
1903 wbcErr wbc_status;
1904 struct wbcChangePasswordParams params;
1905 struct wbcAuthErrorInfo *error = NULL;
1906 struct wbcUserPasswordPolicyInfo *policy = NULL;
1907 enum wbcPasswordChangeRejectReason reject_reason = -1;
1908 uint32_t flags = 0;
1910 int i;
1911 const char *codes[] = {
1912 "NT_STATUS_BACKUP_CONTROLLER",
1913 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
1914 "NT_STATUS_NO_LOGON_SERVERS",
1915 "NT_STATUS_ACCESS_DENIED",
1916 "NT_STATUS_PWD_TOO_SHORT", /* TODO: tell the min pwd length ? */
1917 "NT_STATUS_PWD_TOO_RECENT", /* TODO: tell the minage ? */
1918 "NT_STATUS_PWD_HISTORY_CONFLICT" /* TODO: tell the history length ? */
1920 int ret = PAM_AUTH_ERR;
1922 ZERO_STRUCT(params);
1924 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
1925 flags |= WBFLAG_PAM_KRB5 |
1926 WBFLAG_PAM_CONTACT_TRUSTDOM;
1929 if (ctx->ctrl & WINBIND_CACHED_LOGIN) {
1930 flags |= WBFLAG_PAM_CACHED_LOGIN;
1933 params.account_name = user;
1934 params.level = WBC_CHANGE_PASSWORD_LEVEL_PLAIN;
1935 params.old_password.plaintext = oldpass;
1936 params.new_password.plaintext = newpass;
1937 params.flags = flags;
1939 wbc_status = wbcChangeUserPasswordEx(&params, &error, &reject_reason, &policy);
1940 ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
1941 user, "wbcChangeUserPasswordEx");
1943 if (WBC_ERROR_IS_OK(wbc_status)) {
1944 _pam_log(ctx, LOG_NOTICE,
1945 "user '%s' password changed", user);
1946 return PAM_SUCCESS;
1949 if (!error) {
1950 wbcFreeMemory(policy);
1951 return ret;
1954 for (i=0; i<ARRAY_SIZE(codes); i++) {
1955 int _ret = ret;
1956 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
1957 ret = _ret;
1958 goto done;
1962 if (!strcasecmp(error->nt_string,
1963 "NT_STATUS_PASSWORD_RESTRICTION")) {
1965 char *pwd_restriction_string = NULL;
1966 SMB_TIME_T min_pwd_age = 0;
1968 if (policy) {
1969 min_pwd_age = policy->min_passwordage;
1972 /* FIXME: avoid to send multiple PAM messages after another */
1973 switch (reject_reason) {
1974 case -1:
1975 break;
1976 case WBC_PWD_CHANGE_NO_ERROR:
1977 if ((min_pwd_age > 0) &&
1978 (pwd_last_set + min_pwd_age > time(NULL))) {
1979 PAM_WB_REMARK_DIRECT(ctx,
1980 "NT_STATUS_PWD_TOO_RECENT");
1982 break;
1983 case WBC_PWD_CHANGE_PASSWORD_TOO_SHORT:
1984 PAM_WB_REMARK_DIRECT(ctx,
1985 "NT_STATUS_PWD_TOO_SHORT");
1986 break;
1987 case WBC_PWD_CHANGE_PWD_IN_HISTORY:
1988 PAM_WB_REMARK_DIRECT(ctx,
1989 "NT_STATUS_PWD_HISTORY_CONFLICT");
1990 break;
1991 case WBC_PWD_CHANGE_NOT_COMPLEX:
1992 _make_remark(ctx, PAM_ERROR_MSG,
1993 _("Password does not meet "
1994 "complexity requirements"));
1995 break;
1996 default:
1997 _pam_log_debug(ctx, LOG_DEBUG,
1998 "unknown password change "
1999 "reject reason: %d",
2000 reject_reason);
2001 break;
2004 pwd_restriction_string =
2005 _pam_compose_pwd_restriction_string(ctx, policy);
2006 if (pwd_restriction_string) {
2007 _make_remark(ctx, PAM_ERROR_MSG,
2008 pwd_restriction_string);
2009 TALLOC_FREE(pwd_restriction_string);
2012 done:
2013 wbcFreeMemory(error);
2014 wbcFreeMemory(policy);
2016 return ret;
2020 * Checks if a user has an account
2022 * return values:
2023 * 1 = User not found
2024 * 0 = OK
2025 * -1 = System error
2027 static int valid_user(struct pwb_context *ctx,
2028 const char *user)
2030 /* check not only if the user is available over NSS calls, also make
2031 * sure it's really a winbind user, this is important when stacking PAM
2032 * modules in the 'account' or 'password' facility. */
2034 wbcErr wbc_status;
2035 struct passwd *pwd = NULL;
2036 struct passwd *wb_pwd = NULL;
2038 pwd = getpwnam(user);
2039 if (pwd == NULL) {
2040 return 1;
2043 wbc_status = wbcGetpwnam(user, &wb_pwd);
2044 wbcFreeMemory(wb_pwd);
2045 if (!WBC_ERROR_IS_OK(wbc_status)) {
2046 _pam_log(ctx, LOG_DEBUG, "valid_user: wbcGetpwnam gave %s\n",
2047 wbcErrorString(wbc_status));
2050 switch (wbc_status) {
2051 case WBC_ERR_UNKNOWN_USER:
2052 /* match other insane libwbclient return codes */
2053 case WBC_ERR_WINBIND_NOT_AVAILABLE:
2054 case WBC_ERR_DOMAIN_NOT_FOUND:
2055 return 1;
2056 case WBC_ERR_SUCCESS:
2057 return 0;
2058 default:
2059 break;
2061 return -1;
2064 static char *_pam_delete(register char *xx)
2066 _pam_overwrite(xx);
2067 _pam_drop(xx);
2068 return NULL;
2072 * obtain a password from the user
2075 static int _winbind_read_password(struct pwb_context *ctx,
2076 unsigned int ctrl,
2077 const char *comment,
2078 const char *prompt1,
2079 const char *prompt2,
2080 const char **pass)
2082 int authtok_flag;
2083 int retval;
2084 const char *item;
2085 char *token;
2087 _pam_log(ctx, LOG_DEBUG, "getting password (0x%08x)", ctrl);
2090 * make sure nothing inappropriate gets returned
2093 *pass = token = NULL;
2096 * which authentication token are we getting?
2099 if (on(WINBIND__OLD_PASSWORD, ctrl)) {
2100 authtok_flag = PAM_OLDAUTHTOK;
2101 } else {
2102 authtok_flag = PAM_AUTHTOK;
2106 * should we obtain the password from a PAM item ?
2109 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) ||
2110 on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
2111 retval = pam_get_item(ctx->pamh,
2112 authtok_flag,
2113 (const void **) &item);
2114 if (retval != PAM_SUCCESS) {
2115 /* very strange. */
2116 _pam_log(ctx, LOG_ALERT,
2117 "pam_get_item returned error "
2118 "to unix-read-password");
2119 return retval;
2120 } else if (item != NULL) { /* we have a password! */
2121 *pass = item;
2122 item = NULL;
2123 _pam_log(ctx, LOG_DEBUG,
2124 "pam_get_item returned a password");
2125 return PAM_SUCCESS;
2126 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
2127 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
2128 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
2129 && off(WINBIND__OLD_PASSWORD, ctrl)) {
2130 return PAM_AUTHTOK_RECOVER_ERR;
2134 * getting here implies we will have to get the password from the
2135 * user directly.
2139 struct pam_message msg[3], *pmsg[3];
2140 struct pam_response *resp;
2141 int i, replies;
2143 /* prepare to converse */
2145 if (comment != NULL && off(ctrl, WINBIND_SILENT)) {
2146 pmsg[0] = &msg[0];
2147 msg[0].msg_style = PAM_TEXT_INFO;
2148 msg[0].msg = discard_const_p(char, comment);
2149 i = 1;
2150 } else {
2151 i = 0;
2154 pmsg[i] = &msg[i];
2155 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
2156 msg[i++].msg = discard_const_p(char, prompt1);
2157 replies = 1;
2159 if (prompt2 != NULL) {
2160 pmsg[i] = &msg[i];
2161 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
2162 msg[i++].msg = discard_const_p(char, prompt2);
2163 ++replies;
2165 /* so call the conversation expecting i responses */
2166 resp = NULL;
2167 retval = converse(ctx->pamh, i, pmsg, &resp);
2168 if (resp == NULL) {
2169 if (retval == PAM_SUCCESS) {
2170 retval = PAM_AUTHTOK_RECOVER_ERR;
2172 goto done;
2174 if (retval != PAM_SUCCESS) {
2175 _pam_drop_reply(resp, i);
2176 goto done;
2179 /* interpret the response */
2181 token = x_strdup(resp[i - replies].resp);
2182 if (!token) {
2183 _pam_log(ctx, LOG_NOTICE,
2184 "could not recover "
2185 "authentication token");
2186 retval = PAM_AUTHTOK_RECOVER_ERR;
2187 goto done;
2190 if (replies == 2) {
2191 /* verify that password entered correctly */
2192 if (!resp[i - 1].resp ||
2193 strcmp(token, resp[i - 1].resp)) {
2194 _pam_delete(token); /* mistyped */
2195 retval = PAM_AUTHTOK_RECOVER_ERR;
2196 _make_remark(ctx, PAM_ERROR_MSG,
2197 MISTYPED_PASS);
2202 * tidy up the conversation (resp_retcode) is ignored
2203 * -- what is it for anyway? AGM
2205 _pam_drop_reply(resp, i);
2208 done:
2209 if (retval != PAM_SUCCESS) {
2210 _pam_log_debug(ctx, LOG_DEBUG,
2211 "unable to obtain a password");
2212 return retval;
2214 /* 'token' is the entered password */
2216 /* we store this password as an item */
2218 retval = pam_set_item(ctx->pamh, authtok_flag, token);
2219 _pam_delete(token); /* clean it up */
2220 if (retval != PAM_SUCCESS ||
2221 (retval = pam_get_item(ctx->pamh, authtok_flag, (const void **) &item)) != PAM_SUCCESS) {
2223 _pam_log(ctx, LOG_CRIT, "error manipulating password");
2224 return retval;
2228 *pass = item;
2229 item = NULL; /* break link to password */
2231 return PAM_SUCCESS;
2234 static const char *get_conf_item_string(struct pwb_context *ctx,
2235 const char *item,
2236 int config_flag)
2238 int i = 0;
2239 const char *parm_opt = NULL;
2241 if (!(ctx->ctrl & config_flag)) {
2242 goto out;
2245 /* let the pam opt take precedence over the pam_winbind.conf option */
2246 for (i=0; i<ctx->argc; i++) {
2248 if ((strncmp(ctx->argv[i], item, strlen(item)) == 0)) {
2249 char *p;
2251 if ((p = strchr(ctx->argv[i], '=')) == NULL) {
2252 _pam_log(ctx, LOG_INFO,
2253 "no \"=\" delimiter for \"%s\" found\n",
2254 item);
2255 goto out;
2257 _pam_log_debug(ctx, LOG_INFO,
2258 "PAM config: %s '%s'\n", item, p+1);
2259 return p + 1;
2263 if (ctx->dict) {
2264 char *key = NULL;
2266 key = talloc_asprintf(ctx, "global:%s", item);
2267 if (!key) {
2268 goto out;
2271 parm_opt = iniparser_getstring(ctx->dict, key, NULL);
2272 TALLOC_FREE(key);
2274 _pam_log_debug(ctx, LOG_INFO, "CONFIG file: %s '%s'\n",
2275 item, parm_opt);
2277 out:
2278 return parm_opt;
2281 static int get_config_item_int(struct pwb_context *ctx,
2282 const char *item,
2283 int config_flag)
2285 int i, parm_opt = -1;
2287 if (!(ctx->ctrl & config_flag)) {
2288 goto out;
2291 /* let the pam opt take precedence over the pam_winbind.conf option */
2292 for (i = 0; i < ctx->argc; i++) {
2294 if ((strncmp(ctx->argv[i], item, strlen(item)) == 0)) {
2295 char *p;
2297 if ((p = strchr(ctx->argv[i], '=')) == NULL) {
2298 _pam_log(ctx, LOG_INFO,
2299 "no \"=\" delimiter for \"%s\" found\n",
2300 item);
2301 goto out;
2303 parm_opt = atoi(p + 1);
2304 _pam_log_debug(ctx, LOG_INFO,
2305 "PAM config: %s '%d'\n",
2306 item, parm_opt);
2307 return parm_opt;
2311 if (ctx->dict) {
2312 char *key = NULL;
2314 key = talloc_asprintf(ctx, "global:%s", item);
2315 if (!key) {
2316 goto out;
2319 parm_opt = iniparser_getint(ctx->dict, key, -1);
2320 TALLOC_FREE(key);
2322 _pam_log_debug(ctx, LOG_INFO,
2323 "CONFIG file: %s '%d'\n",
2324 item, parm_opt);
2326 out:
2327 return parm_opt;
2330 static const char *get_krb5_cc_type_from_config(struct pwb_context *ctx)
2332 return get_conf_item_string(ctx, "krb5_ccache_type",
2333 WINBIND_KRB5_CCACHE_TYPE);
2336 static const char *get_member_from_config(struct pwb_context *ctx)
2338 const char *ret = NULL;
2339 ret = get_conf_item_string(ctx, "require_membership_of",
2340 WINBIND_REQUIRED_MEMBERSHIP);
2341 if (ret) {
2342 return ret;
2344 return get_conf_item_string(ctx, "require-membership-of",
2345 WINBIND_REQUIRED_MEMBERSHIP);
2348 static int get_warn_pwd_expire_from_config(struct pwb_context *ctx)
2350 int ret;
2351 ret = get_config_item_int(ctx, "warn_pwd_expire",
2352 WINBIND_WARN_PWD_EXPIRE);
2353 /* no or broken setting */
2354 if (ret <= 0) {
2355 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES;
2357 return ret;
2361 * Retrieve the winbind separator.
2363 * @param ctx PAM winbind context.
2365 * @return string separator character. NULL on failure.
2368 static char winbind_get_separator(struct pwb_context *ctx)
2370 wbcErr wbc_status;
2371 static struct wbcInterfaceDetails *details = NULL;
2373 wbc_status = wbcInterfaceDetails(&details);
2374 if (!WBC_ERROR_IS_OK(wbc_status)) {
2375 _pam_log(ctx, LOG_ERR,
2376 "Could not retrieve winbind interface details: %s",
2377 wbcErrorString(wbc_status));
2378 return '\0';
2381 if (!details) {
2382 return '\0';
2385 return details->winbind_separator;
2390 * Convert a upn to a name.
2392 * @param ctx PAM winbind context.
2393 * @param upn USer UPN to be trabslated.
2395 * @return converted name. NULL pointer on failure. Caller needs to free.
2398 static char* winbind_upn_to_username(struct pwb_context *ctx,
2399 const char *upn)
2401 char sep;
2402 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
2403 struct wbcDomainSid sid;
2404 enum wbcSidType type;
2405 char *domain = NULL;
2406 char *name;
2407 char *p;
2409 /* This cannot work when the winbind separator = @ */
2411 sep = winbind_get_separator(ctx);
2412 if (!sep || sep == '@') {
2413 return NULL;
2416 name = talloc_strdup(ctx, upn);
2417 if (!name) {
2418 return NULL;
2420 if ((p = strchr(name, '@')) != NULL) {
2421 *p = 0;
2422 domain = p + 1;
2425 /* Convert the UPN to a SID */
2427 wbc_status = wbcLookupName(domain, name, &sid, &type);
2428 if (!WBC_ERROR_IS_OK(wbc_status)) {
2429 return NULL;
2432 /* Convert the the SID back to the sAMAccountName */
2434 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
2435 if (!WBC_ERROR_IS_OK(wbc_status)) {
2436 return NULL;
2439 return talloc_asprintf(ctx, "%s%c%s", domain, sep, name);
2442 static int _pam_delete_cred(pam_handle_t *pamh, int flags,
2443 int argc, const char **argv)
2445 int retval = PAM_SUCCESS;
2446 struct pwb_context *ctx = NULL;
2447 struct wbcLogoffUserParams logoff;
2448 struct wbcAuthErrorInfo *error = NULL;
2449 const char *user;
2450 wbcErr wbc_status = WBC_ERR_SUCCESS;
2452 ZERO_STRUCT(logoff);
2454 retval = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2455 if (retval) {
2456 goto out;
2459 _PAM_LOG_FUNCTION_ENTER("_pam_delete_cred", ctx);
2461 if (ctx->ctrl & WINBIND_KRB5_AUTH) {
2463 /* destroy the ccache here */
2465 uint32_t wbc_flags = 0;
2466 const char *ccname = NULL;
2467 struct passwd *pwd = NULL;
2469 retval = pam_get_user(pamh, &user, _("Username: "));
2470 if (retval) {
2471 _pam_log(ctx, LOG_ERR,
2472 "could not identify user");
2473 goto out;
2476 if (user == NULL) {
2477 _pam_log(ctx, LOG_ERR,
2478 "username was NULL!");
2479 retval = PAM_USER_UNKNOWN;
2480 goto out;
2483 _pam_log_debug(ctx, LOG_DEBUG,
2484 "username [%s] obtained", user);
2486 ccname = pam_getenv(pamh, "KRB5CCNAME");
2487 if (ccname == NULL) {
2488 _pam_log_debug(ctx, LOG_DEBUG,
2489 "user has no KRB5CCNAME environment");
2492 pwd = getpwnam(user);
2493 if (pwd == NULL) {
2494 retval = PAM_USER_UNKNOWN;
2495 goto out;
2498 wbc_flags = WBFLAG_PAM_KRB5 |
2499 WBFLAG_PAM_CONTACT_TRUSTDOM;
2501 logoff.username = user;
2503 if (ccname) {
2504 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2505 &logoff.blobs,
2506 "ccfilename",
2508 discard_const_p(uint8_t, ccname),
2509 strlen(ccname)+1);
2510 if (!WBC_ERROR_IS_OK(wbc_status)) {
2511 goto out;
2515 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2516 &logoff.blobs,
2517 "flags",
2519 (uint8_t *)&wbc_flags,
2520 sizeof(wbc_flags));
2521 if (!WBC_ERROR_IS_OK(wbc_status)) {
2522 goto out;
2525 wbc_status = wbcAddNamedBlob(&logoff.num_blobs,
2526 &logoff.blobs,
2527 "user_uid",
2529 (uint8_t *)&pwd->pw_uid,
2530 sizeof(pwd->pw_uid));
2531 if (!WBC_ERROR_IS_OK(wbc_status)) {
2532 goto out;
2535 wbc_status = wbcLogoffUserEx(&logoff, &error);
2536 retval = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
2537 user, "wbcLogoffUser");
2538 wbcFreeMemory(error);
2539 wbcFreeMemory(logoff.blobs);
2540 logoff.blobs = NULL;
2542 if (!WBC_ERROR_IS_OK(wbc_status)) {
2543 _pam_log(ctx, LOG_INFO,
2544 "failed to logoff user %s: %s\n",
2545 user, wbcErrorString(wbc_status));
2549 out:
2550 if (logoff.blobs) {
2551 wbcFreeMemory(logoff.blobs);
2554 if (!WBC_ERROR_IS_OK(wbc_status)) {
2555 retval = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
2556 user, "wbcLogoffUser");
2560 * Delete the krb5 ccname variable from the PAM environment
2561 * if it was set by winbind.
2563 if ((ctx->ctrl & WINBIND_KRB5_AUTH) && pam_getenv(pamh, "KRB5CCNAME")) {
2564 pam_putenv(pamh, "KRB5CCNAME");
2567 _PAM_LOG_FUNCTION_LEAVE("_pam_delete_cred", ctx, retval);
2569 TALLOC_FREE(ctx);
2571 return retval;
2574 PAM_EXTERN
2575 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
2576 int argc, const char **argv)
2578 const char *username;
2579 const char *password;
2580 const char *member = NULL;
2581 const char *cctype = NULL;
2582 int warn_pwd_expire;
2583 int retval = PAM_AUTH_ERR;
2584 char *username_ret = NULL;
2585 char *new_authtok_required = NULL;
2586 char *real_username = NULL;
2587 struct pwb_context *ctx = NULL;
2589 retval = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2590 if (retval) {
2591 goto out;
2594 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", ctx);
2596 /* Get the username */
2597 retval = pam_get_user(pamh, &username, NULL);
2598 if ((retval != PAM_SUCCESS) || (!username)) {
2599 _pam_log_debug(ctx, LOG_DEBUG,
2600 "can not get the username");
2601 retval = PAM_SERVICE_ERR;
2602 goto out;
2606 #if defined(AIX)
2607 /* Decode the user name since AIX does not support logn user
2608 names by default. The name is encoded as _#uid. */
2610 if (username[0] == '_') {
2611 uid_t id = atoi(&username[1]);
2612 struct passwd *pw = NULL;
2614 if ((id!=0) && ((pw = getpwuid(id)) != NULL)) {
2615 real_username = strdup(pw->pw_name);
2618 #endif
2620 if (!real_username) {
2621 /* Just making a copy of the username we got from PAM */
2622 if ((real_username = strdup(username)) == NULL) {
2623 _pam_log_debug(ctx, LOG_DEBUG,
2624 "memory allocation failure when copying "
2625 "username");
2626 retval = PAM_SERVICE_ERR;
2627 goto out;
2631 /* Maybe this was a UPN */
2633 if (strchr(real_username, '@') != NULL) {
2634 char *samaccountname = NULL;
2636 samaccountname = winbind_upn_to_username(ctx,
2637 real_username);
2638 if (samaccountname) {
2639 free(real_username);
2640 real_username = strdup(samaccountname);
2644 retval = _winbind_read_password(ctx, ctx->ctrl, NULL,
2645 _("Password: "), NULL,
2646 &password);
2648 if (retval != PAM_SUCCESS) {
2649 _pam_log(ctx, LOG_ERR,
2650 "Could not retrieve user's password");
2651 retval = PAM_AUTHTOK_ERR;
2652 goto out;
2655 /* Let's not give too much away in the log file */
2657 #ifdef DEBUG_PASSWORD
2658 _pam_log_debug(ctx, LOG_INFO,
2659 "Verify user '%s' with password '%s'",
2660 real_username, password);
2661 #else
2662 _pam_log_debug(ctx, LOG_INFO,
2663 "Verify user '%s'", real_username);
2664 #endif
2666 member = get_member_from_config(ctx);
2667 cctype = get_krb5_cc_type_from_config(ctx);
2668 warn_pwd_expire = get_warn_pwd_expire_from_config(ctx);
2670 /* Now use the username to look up password */
2671 retval = winbind_auth_request(ctx, real_username, password,
2672 member, cctype, warn_pwd_expire,
2673 NULL, NULL, NULL,
2674 NULL, &username_ret);
2676 if (retval == PAM_NEW_AUTHTOK_REQD ||
2677 retval == PAM_AUTHTOK_EXPIRED) {
2679 char *new_authtok_required_during_auth = NULL;
2681 new_authtok_required = talloc_asprintf(NULL, "%d", retval);
2682 if (!new_authtok_required) {
2683 retval = PAM_BUF_ERR;
2684 goto out;
2687 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2688 new_authtok_required,
2689 _pam_winbind_cleanup_func);
2691 retval = PAM_SUCCESS;
2693 new_authtok_required_during_auth = talloc_asprintf(NULL, "%d", true);
2694 if (!new_authtok_required_during_auth) {
2695 retval = PAM_BUF_ERR;
2696 goto out;
2699 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2700 new_authtok_required_during_auth,
2701 _pam_winbind_cleanup_func);
2703 goto out;
2706 out:
2707 if (username_ret) {
2708 pam_set_item (pamh, PAM_USER, username_ret);
2709 _pam_log_debug(ctx, LOG_INFO,
2710 "Returned user was '%s'", username_ret);
2711 free(username_ret);
2714 if (real_username) {
2715 free(real_username);
2718 if (!new_authtok_required) {
2719 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
2722 if (retval != PAM_SUCCESS) {
2723 _pam_free_data_info3(pamh);
2726 if (ctx != NULL) {
2727 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", ctx, retval);
2728 TALLOC_FREE(ctx);
2731 return retval;
2734 PAM_EXTERN
2735 int pam_sm_setcred(pam_handle_t *pamh, int flags,
2736 int argc, const char **argv)
2738 int ret = PAM_SYSTEM_ERR;
2739 struct pwb_context *ctx = NULL;
2741 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2742 if (ret) {
2743 goto out;
2746 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", ctx);
2748 switch (flags & ~PAM_SILENT) {
2750 case PAM_DELETE_CRED:
2751 ret = _pam_delete_cred(pamh, flags, argc, argv);
2752 break;
2753 case PAM_REFRESH_CRED:
2754 _pam_log_debug(ctx, LOG_WARNING,
2755 "PAM_REFRESH_CRED not implemented");
2756 ret = PAM_SUCCESS;
2757 break;
2758 case PAM_REINITIALIZE_CRED:
2759 _pam_log_debug(ctx, LOG_WARNING,
2760 "PAM_REINITIALIZE_CRED not implemented");
2761 ret = PAM_SUCCESS;
2762 break;
2763 case PAM_ESTABLISH_CRED:
2764 _pam_log_debug(ctx, LOG_WARNING,
2765 "PAM_ESTABLISH_CRED not implemented");
2766 ret = PAM_SUCCESS;
2767 break;
2768 default:
2769 ret = PAM_SYSTEM_ERR;
2770 break;
2773 out:
2775 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", ctx, ret);
2777 TALLOC_FREE(ctx);
2779 return ret;
2783 * Account management. We want to verify that the account exists
2784 * before returning PAM_SUCCESS
2786 PAM_EXTERN
2787 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
2788 int argc, const char **argv)
2790 const char *username;
2791 int ret = PAM_USER_UNKNOWN;
2792 const char *tmp = NULL;
2793 struct pwb_context *ctx = NULL;
2795 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2796 if (ret) {
2797 goto out;
2800 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", ctx);
2803 /* Get the username */
2804 ret = pam_get_user(pamh, &username, NULL);
2805 if ((ret != PAM_SUCCESS) || (!username)) {
2806 _pam_log_debug(ctx, LOG_DEBUG,
2807 "can not get the username");
2808 ret = PAM_SERVICE_ERR;
2809 goto out;
2812 /* Verify the username */
2813 ret = valid_user(ctx, username);
2814 switch (ret) {
2815 case -1:
2816 /* some sort of system error. The log was already printed */
2817 ret = PAM_SERVICE_ERR;
2818 goto out;
2819 case 1:
2820 /* the user does not exist */
2821 _pam_log_debug(ctx, LOG_NOTICE, "user '%s' not found",
2822 username);
2823 if (ctx->ctrl & WINBIND_UNKNOWN_OK_ARG) {
2824 ret = PAM_IGNORE;
2825 goto out;
2827 ret = PAM_USER_UNKNOWN;
2828 goto out;
2829 case 0:
2830 pam_get_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD,
2831 (const void **)&tmp);
2832 if (tmp != NULL) {
2833 ret = atoi(tmp);
2834 switch (ret) {
2835 case PAM_AUTHTOK_EXPIRED:
2836 /* fall through, since new token is required in this case */
2837 case PAM_NEW_AUTHTOK_REQD:
2838 _pam_log(ctx, LOG_WARNING,
2839 "pam_sm_acct_mgmt success but %s is set",
2840 PAM_WINBIND_NEW_AUTHTOK_REQD);
2841 _pam_log(ctx, LOG_NOTICE,
2842 "user '%s' needs new password",
2843 username);
2844 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2845 ret = PAM_NEW_AUTHTOK_REQD;
2846 goto out;
2847 default:
2848 _pam_log(ctx, LOG_WARNING,
2849 "pam_sm_acct_mgmt success");
2850 _pam_log(ctx, LOG_NOTICE,
2851 "user '%s' granted access", username);
2852 ret = PAM_SUCCESS;
2853 goto out;
2857 /* Otherwise, the authentication looked good */
2858 _pam_log(ctx, LOG_NOTICE,
2859 "user '%s' granted access", username);
2860 ret = PAM_SUCCESS;
2861 goto out;
2862 default:
2863 /* we don't know anything about this return value */
2864 _pam_log(ctx, LOG_ERR,
2865 "internal module error (ret = %d, user = '%s')",
2866 ret, username);
2867 ret = PAM_SERVICE_ERR;
2868 goto out;
2871 /* should not be reached */
2872 ret = PAM_IGNORE;
2874 out:
2876 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", ctx, ret);
2878 TALLOC_FREE(ctx);
2880 return ret;
2883 PAM_EXTERN
2884 int pam_sm_open_session(pam_handle_t *pamh, int flags,
2885 int argc, const char **argv)
2887 int ret = PAM_SUCCESS;
2888 struct pwb_context *ctx = NULL;
2890 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2891 if (ret) {
2892 goto out;
2895 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", ctx);
2897 if (ctx->ctrl & WINBIND_MKHOMEDIR) {
2898 /* check and create homedir */
2899 ret = _pam_mkhomedir(ctx);
2901 out:
2902 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", ctx, ret);
2904 TALLOC_FREE(ctx);
2906 return ret;
2909 PAM_EXTERN
2910 int pam_sm_close_session(pam_handle_t *pamh, int flags,
2911 int argc, const char **argv)
2913 int ret = PAM_SUCCESS;
2914 struct pwb_context *ctx = NULL;
2916 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
2917 if (ret) {
2918 goto out;
2921 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", ctx);
2923 out:
2924 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", ctx, ret);
2926 TALLOC_FREE(ctx);
2928 return ret;
2932 * evaluate whether we need to re-authenticate with kerberos after a
2933 * password change
2935 * @param ctx PAM winbind context.
2936 * @param user The username
2938 * @return boolean Returns true if required, false if not.
2941 static bool _pam_require_krb5_auth_after_chauthtok(struct pwb_context *ctx,
2942 const char *user)
2945 /* Make sure that we only do this if a) the chauthtok got initiated
2946 * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2947 * later password change via the "passwd" command if done by the user
2948 * itself
2949 * NB. If we login from gdm or xdm and the password expires,
2950 * we change the password, but there is no memory cache.
2951 * Thus, even for passthrough login, we should do the
2952 * authentication again to update memory cache.
2953 * --- BoYang
2954 * */
2956 char *new_authtok_reqd_during_auth = NULL;
2957 struct passwd *pwd = NULL;
2959 pam_get_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2960 (const void **) &new_authtok_reqd_during_auth);
2961 pam_set_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
2962 NULL, NULL);
2964 if (new_authtok_reqd_during_auth) {
2965 return true;
2968 pwd = getpwnam(user);
2969 if (!pwd) {
2970 return false;
2973 if (getuid() == pwd->pw_uid) {
2974 return true;
2977 return false;
2981 PAM_EXTERN
2982 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
2983 int argc, const char **argv)
2985 unsigned int lctrl;
2986 int ret;
2987 bool cached_login = false;
2989 /* <DO NOT free() THESE> */
2990 const char *user;
2991 const char *pass_old;
2992 const char *pass_new;
2993 /* </DO NOT free() THESE> */
2995 char *Announce;
2997 int retry = 0;
2998 char *username_ret = NULL;
2999 struct wbcAuthErrorInfo *error = NULL;
3000 struct pwb_context *ctx = NULL;
3002 ret = _pam_winbind_init_context(pamh, flags, argc, argv, &ctx);
3003 if (ret) {
3004 goto out;
3007 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", ctx);
3009 cached_login = (ctx->ctrl & WINBIND_CACHED_LOGIN);
3011 /* clearing offline bit for auth */
3012 ctx->ctrl &= ~WINBIND_CACHED_LOGIN;
3015 * First get the name of a user
3017 ret = pam_get_user(pamh, &user, _("Username: "));
3018 if (ret) {
3019 _pam_log(ctx, LOG_ERR,
3020 "password - could not identify user");
3021 goto out;
3024 if (user == NULL) {
3025 _pam_log(ctx, LOG_ERR, "username was NULL!");
3026 ret = PAM_USER_UNKNOWN;
3027 goto out;
3030 _pam_log_debug(ctx, LOG_DEBUG, "username [%s] obtained", user);
3032 /* check if this is really a user in winbindd, not only in NSS */
3033 ret = valid_user(ctx, user);
3034 switch (ret) {
3035 case 1:
3036 ret = PAM_USER_UNKNOWN;
3037 goto out;
3038 case -1:
3039 ret = PAM_SYSTEM_ERR;
3040 goto out;
3041 default:
3042 break;
3046 * obtain and verify the current password (OLDAUTHTOK) for
3047 * the user.
3050 if (flags & PAM_PRELIM_CHECK) {
3051 time_t pwdlastset_prelim = 0;
3053 /* instruct user what is happening */
3055 #define greeting _("Changing password for")
3056 Announce = talloc_asprintf(ctx, "%s %s", greeting, user);
3057 if (!Announce) {
3058 _pam_log(ctx, LOG_CRIT,
3059 "password - out of memory");
3060 ret = PAM_BUF_ERR;
3061 goto out;
3063 #undef greeting
3065 lctrl = ctx->ctrl | WINBIND__OLD_PASSWORD;
3066 ret = _winbind_read_password(ctx, lctrl,
3067 Announce,
3068 _("(current) NT password: "),
3069 NULL,
3070 (const char **) &pass_old);
3071 TALLOC_FREE(Announce);
3072 if (ret != PAM_SUCCESS) {
3073 _pam_log(ctx, LOG_NOTICE,
3074 "password - (old) token not obtained");
3075 goto out;
3078 /* verify that this is the password for this user */
3080 ret = winbind_auth_request(ctx, user, pass_old,
3081 NULL, NULL, 0,
3082 &error, NULL, NULL,
3083 &pwdlastset_prelim, NULL);
3085 if (ret != PAM_ACCT_EXPIRED &&
3086 ret != PAM_AUTHTOK_EXPIRED &&
3087 ret != PAM_NEW_AUTHTOK_REQD &&
3088 ret != PAM_SUCCESS) {
3089 pass_old = NULL;
3090 goto out;
3093 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET,
3094 (void *)pwdlastset_prelim, NULL);
3096 ret = pam_set_item(pamh, PAM_OLDAUTHTOK,
3097 (const void *) pass_old);
3098 pass_old = NULL;
3099 if (ret != PAM_SUCCESS) {
3100 _pam_log(ctx, LOG_CRIT,
3101 "failed to set PAM_OLDAUTHTOK");
3103 } else if (flags & PAM_UPDATE_AUTHTOK) {
3105 time_t pwdlastset_update = 0;
3108 * obtain the proposed password
3112 * get the old token back.
3115 ret = pam_get_item(pamh, PAM_OLDAUTHTOK, (const void **) &pass_old);
3117 if (ret != PAM_SUCCESS) {
3118 _pam_log(ctx, LOG_NOTICE,
3119 "user not authenticated");
3120 goto out;
3123 lctrl = ctx->ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
3125 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
3126 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
3128 retry = 0;
3129 ret = PAM_AUTHTOK_ERR;
3130 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
3132 * use_authtok is to force the use of a previously entered
3133 * password -- needed for pluggable password strength checking
3136 ret = _winbind_read_password(ctx, lctrl,
3137 NULL,
3138 _("Enter new NT password: "),
3139 _("Retype new NT password: "),
3140 (const char **)&pass_new);
3142 if (ret != PAM_SUCCESS) {
3143 _pam_log_debug(ctx, LOG_ALERT,
3144 "password - "
3145 "new password not obtained");
3146 pass_old = NULL;/* tidy up */
3147 goto out;
3151 * At this point we know who the user is and what they
3152 * propose as their new password. Verify that the new
3153 * password is acceptable.
3156 if (pass_new[0] == '\0') {/* "\0" password = NULL */
3157 pass_new = NULL;
3162 * By reaching here we have approved the passwords and must now
3163 * rebuild the password database file.
3165 pam_get_data(pamh, PAM_WINBIND_PWD_LAST_SET,
3166 (const void **) &pwdlastset_update);
3169 * if cached creds were enabled, make sure to set the
3170 * WINBIND_CACHED_LOGIN bit here in order to have winbindd
3171 * update the cached creds storage - gd
3173 if (cached_login) {
3174 ctx->ctrl |= WINBIND_CACHED_LOGIN;
3177 ret = winbind_chauthtok_request(ctx, user, pass_old,
3178 pass_new, pwdlastset_update);
3179 if (ret) {
3180 pass_old = pass_new = NULL;
3181 goto out;
3184 if (_pam_require_krb5_auth_after_chauthtok(ctx, user)) {
3186 const char *member = NULL;
3187 const char *cctype = NULL;
3188 int warn_pwd_expire;
3189 struct wbcLogonUserInfo *info = NULL;
3190 struct wbcUserPasswordPolicyInfo *policy = NULL;
3192 member = get_member_from_config(ctx);
3193 cctype = get_krb5_cc_type_from_config(ctx);
3194 warn_pwd_expire = get_warn_pwd_expire_from_config(ctx);
3196 /* Keep WINBIND_CACHED_LOGIN bit for
3197 * authentication after changing the password.
3198 * This will update the cached credentials in case
3199 * that winbindd_dual_pam_chauthtok() fails
3200 * to update them.
3201 * --- BoYang
3202 * */
3204 ret = winbind_auth_request(ctx, user, pass_new,
3205 member, cctype, 0,
3206 &error, &info, &policy,
3207 NULL, &username_ret);
3208 pass_old = pass_new = NULL;
3210 if (ret == PAM_SUCCESS) {
3212 struct wbcAuthUserInfo *user_info = NULL;
3214 if (info && info->info) {
3215 user_info = info->info;
3218 /* warn a user if the password is about to
3219 * expire soon */
3220 _pam_warn_password_expiry(ctx, user_info, policy,
3221 warn_pwd_expire,
3222 NULL, NULL);
3224 /* set some info3 info for other modules in the
3225 * stack */
3226 _pam_set_data_info3(ctx, user_info);
3228 /* put krb5ccname into env */
3229 _pam_setup_krb5_env(ctx, info);
3231 if (username_ret) {
3232 pam_set_item(pamh, PAM_USER,
3233 username_ret);
3234 _pam_log_debug(ctx, LOG_INFO,
3235 "Returned user was '%s'",
3236 username_ret);
3237 free(username_ret);
3242 if (info && info->blobs) {
3243 wbcFreeMemory(info->blobs);
3245 wbcFreeMemory(info);
3246 wbcFreeMemory(policy);
3248 goto out;
3250 } else {
3251 ret = PAM_SERVICE_ERR;
3254 out:
3256 /* Deal with offline errors. */
3257 int i;
3258 const char *codes[] = {
3259 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
3260 "NT_STATUS_NO_LOGON_SERVERS",
3261 "NT_STATUS_ACCESS_DENIED"
3264 for (i=0; i<ARRAY_SIZE(codes); i++) {
3265 int _ret;
3266 if (_pam_check_remark_auth_err(ctx, error, codes[i], &_ret)) {
3267 break;
3272 wbcFreeMemory(error);
3274 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", ctx, ret);
3276 TALLOC_FREE(ctx);
3278 return ret;
3281 #ifdef PAM_STATIC
3283 /* static module data */
3285 struct pam_module _pam_winbind_modstruct = {
3286 MODULE_NAME,
3287 pam_sm_authenticate,
3288 pam_sm_setcred,
3289 pam_sm_acct_mgmt,
3290 pam_sm_open_session,
3291 pam_sm_close_session,
3292 pam_sm_chauthtok
3295 #endif
3298 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
3299 * Copyright (c) Tim Potter <tpot@samba.org> 2000
3300 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
3301 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2008
3302 * Copyright (c) Jan Rêkorajski 1999.
3303 * Copyright (c) Andrew G. Morgan 1996-8.
3304 * Copyright (c) Alex O. Yuriev, 1996.
3305 * Copyright (c) Cristian Gafton 1996.
3306 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
3308 * Redistribution and use in source and binary forms, with or without
3309 * modification, are permitted provided that the following conditions
3310 * are met:
3311 * 1. Redistributions of source code must retain the above copyright
3312 * notice, and the entire permission notice in its entirety,
3313 * including the disclaimer of warranties.
3314 * 2. Redistributions in binary form must reproduce the above copyright
3315 * notice, this list of conditions and the following disclaimer in the
3316 * documentation and/or other materials provided with the distribution.
3317 * 3. The name of the author may not be used to endorse or promote
3318 * products derived from this software without specific prior
3319 * written permission.
3321 * ALTERNATIVELY, this product may be distributed under the terms of
3322 * the GNU Public License, in which case the provisions of the GPL are
3323 * required INSTEAD OF the above restrictions. (This clause is
3324 * necessary due to a potential bad interaction between the GPL and
3325 * the restrictions contained in a BSD-style copyright.)
3327 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
3328 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3329 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3330 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3331 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3332 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3333 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3334 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3335 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3336 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3337 * OF THE POSSIBILITY OF SUCH DAMAGE.