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-2007
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 #define _PAM_LOG_FUNCTION_ENTER(function, pamh, ctrl, flags) \
17 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: %p] ENTER: " \
18 function " (flags: 0x%04x)", pamh, flags); \
19 _pam_log_state(pamh, ctrl); \
22 #define _PAM_LOG_FUNCTION_LEAVE(function, pamh, ctrl, retval) \
24 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: %p] LEAVE: " \
25 function " returning %d", pamh, retval); \
26 _pam_log_state(pamh, ctrl); \
31 #define MAX_PASSWD_TRIES 3
34 * Work around the pam API that has functions with void ** as parameters
35 * These lead to strict aliasing warnings with gcc.
37 static int _pam_get_item(const pam_handle_t
*pamh
,
41 const void **item
= (const void **)_item
;
42 return pam_get_item(pamh
, item_type
, item
);
44 static int _pam_get_data(const pam_handle_t
*pamh
,
45 const char *module_data_name
,
48 const void **data
= (const void **)_data
;
49 return pam_get_data(pamh
, module_data_name
, data
);
54 #ifdef HAVE_PAM_VSYSLOG
55 static void _pam_log_int(const pam_handle_t
*pamh
,
60 pam_vsyslog(pamh
, err
, format
, args
);
63 static void _pam_log_int(const pam_handle_t
*pamh
,
71 _pam_get_item(pamh
, PAM_SERVICE
, &service
);
73 format2
= (char *)malloc(strlen(MODULE_NAME
)+strlen(format
)+strlen(service
)+5);
74 if (format2
== NULL
) {
75 /* what else todo ? */
76 vsyslog(err
, format
, args
);
80 sprintf(format2
, "%s(%s): %s", MODULE_NAME
, service
, format
);
81 vsyslog(err
, format2
, args
);
84 #endif /* HAVE_PAM_VSYSLOG */
86 static bool _pam_log_is_silent(int ctrl
)
88 return on(ctrl
, WINBIND_SILENT
);
91 static void _pam_log(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(4,5);
92 static void _pam_log(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...)
96 if (_pam_log_is_silent(ctrl
)) {
100 va_start(args
, format
);
101 _pam_log_int(pamh
, err
, format
, args
);
105 static bool _pam_log_is_debug_enabled(int ctrl
)
111 if (_pam_log_is_silent(ctrl
)) {
115 if (!(ctrl
& WINBIND_DEBUG_ARG
)) {
122 static bool _pam_log_is_debug_state_enabled(int ctrl
)
124 if (!(ctrl
& WINBIND_DEBUG_STATE
)) {
128 return _pam_log_is_debug_enabled(ctrl
);
131 static void _pam_log_debug(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...) PRINTF_ATTRIBUTE(4,5);
132 static void _pam_log_debug(const pam_handle_t
*pamh
, int ctrl
, int err
, const char *format
, ...)
136 if (!_pam_log_is_debug_enabled(ctrl
)) {
140 va_start(args
, format
);
141 _pam_log_int(pamh
, err
, format
, args
);
145 static void _pam_log_state_datum(const pam_handle_t
*pamh
,
151 const void *data
= NULL
;
152 if (item_type
!= 0) {
153 pam_get_item(pamh
, item_type
, &data
);
155 pam_get_data(pamh
, key
, &data
);
158 const char *type
= (item_type
!= 0) ? "ITEM" : "DATA";
159 if (is_string
!= 0) {
160 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
161 "[pamh: %p] STATE: %s(%s) = \"%s\" (%p)",
162 pamh
, type
, key
, (const char *)data
,
165 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
166 "[pamh: %p] STATE: %s(%s) = %p",
167 pamh
, type
, key
, data
);
172 #define _PAM_LOG_STATE_DATA_POINTER(pamh, ctrl, module_data_name) \
173 _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 0)
175 #define _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, module_data_name) \
176 _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 1)
178 #define _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, item_type) \
179 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 0)
181 #define _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, item_type) \
182 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 1)
184 #ifdef DEBUG_PASSWORD
185 #define _LOG_PASSWORD_AS_STRING 1
187 #define _LOG_PASSWORD_AS_STRING 0
190 #define _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, item_type) \
191 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, \
192 _LOG_PASSWORD_AS_STRING)
194 static void _pam_log_state(const pam_handle_t
*pamh
, int ctrl
)
196 if (!_pam_log_is_debug_state_enabled(ctrl
)) {
200 _PAM_LOG_STATE_ITEM_STRING(pamh
, ctrl
, PAM_SERVICE
);
201 _PAM_LOG_STATE_ITEM_STRING(pamh
, ctrl
, PAM_USER
);
202 _PAM_LOG_STATE_ITEM_STRING(pamh
, ctrl
, PAM_TTY
);
203 _PAM_LOG_STATE_ITEM_STRING(pamh
, ctrl
, PAM_RHOST
);
204 _PAM_LOG_STATE_ITEM_STRING(pamh
, ctrl
, PAM_RUSER
);
205 _PAM_LOG_STATE_ITEM_PASSWORD(pamh
, ctrl
, PAM_OLDAUTHTOK
);
206 _PAM_LOG_STATE_ITEM_PASSWORD(pamh
, ctrl
, PAM_AUTHTOK
);
207 _PAM_LOG_STATE_ITEM_STRING(pamh
, ctrl
, PAM_USER_PROMPT
);
208 _PAM_LOG_STATE_ITEM_POINTER(pamh
, ctrl
, PAM_CONV
);
209 #ifdef PAM_FAIL_DELAY
210 _PAM_LOG_STATE_ITEM_POINTER(pamh
, ctrl
, PAM_FAIL_DELAY
);
212 #ifdef PAM_REPOSITORY
213 _PAM_LOG_STATE_ITEM_POINTER(pamh
, ctrl
, PAM_REPOSITORY
);
216 _PAM_LOG_STATE_DATA_STRING(pamh
, ctrl
, PAM_WINBIND_HOMEDIR
);
217 _PAM_LOG_STATE_DATA_STRING(pamh
, ctrl
, PAM_WINBIND_LOGONSCRIPT
);
218 _PAM_LOG_STATE_DATA_STRING(pamh
, ctrl
, PAM_WINBIND_LOGONSERVER
);
219 _PAM_LOG_STATE_DATA_STRING(pamh
, ctrl
, PAM_WINBIND_PROFILEPATH
);
220 _PAM_LOG_STATE_DATA_STRING(pamh
, ctrl
,
221 PAM_WINBIND_NEW_AUTHTOK_REQD
);
222 /* Use atoi to get PAM result code */
223 _PAM_LOG_STATE_DATA_STRING(pamh
, ctrl
,
224 PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
);
225 _PAM_LOG_STATE_DATA_POINTER(pamh
, ctrl
, PAM_WINBIND_PWD_LAST_SET
);
228 static int _pam_parse(const pam_handle_t
*pamh
,
232 dictionary
**result_d
)
235 const char *config_file
= NULL
;
238 dictionary
*d
= NULL
;
240 if (flags
& PAM_SILENT
) {
241 ctrl
|= WINBIND_SILENT
;
244 for (i
=argc
,v
=argv
; i
-- > 0; ++v
) {
245 if (!strncasecmp(*v
, "config", strlen("config"))) {
246 ctrl
|= WINBIND_CONFIG_FILE
;
252 if (config_file
== NULL
) {
253 config_file
= PAM_WINBIND_CONFIG_FILE
;
256 d
= iniparser_load(config_file
);
258 goto config_from_pam
;
261 if (iniparser_getboolean(d
, "global:debug", false)) {
262 ctrl
|= WINBIND_DEBUG_ARG
;
265 if (iniparser_getboolean(d
, "global:debug_state", false)) {
266 ctrl
|= WINBIND_DEBUG_STATE
;
269 if (iniparser_getboolean(d
, "global:cached_login", false)) {
270 ctrl
|= WINBIND_CACHED_LOGIN
;
273 if (iniparser_getboolean(d
, "global:krb5_auth", false)) {
274 ctrl
|= WINBIND_KRB5_AUTH
;
277 if (iniparser_getboolean(d
, "global:silent", false)) {
278 ctrl
|= WINBIND_SILENT
;
281 if (iniparser_getstr(d
, "global:krb5_ccache_type") != NULL
) {
282 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
285 if ((iniparser_getstr(d
, "global:require-membership-of") != NULL
) ||
286 (iniparser_getstr(d
, "global:require_membership_of") != NULL
)) {
287 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
290 if (iniparser_getboolean(d
, "global:try_first_pass", false)) {
291 ctrl
|= WINBIND_TRY_FIRST_PASS_ARG
;
294 if (iniparser_getint(d
, "global:warn_pwd_expire", 0)) {
295 ctrl
|= WINBIND_WARN_PWD_EXPIRE
;
299 /* step through arguments */
300 for (i
=argc
,v
=argv
; i
-- > 0; ++v
) {
302 /* generic options */
303 if (!strcmp(*v
,"debug"))
304 ctrl
|= WINBIND_DEBUG_ARG
;
305 else if (!strcasecmp(*v
, "debug_state"))
306 ctrl
|= WINBIND_DEBUG_STATE
;
307 else if (!strcasecmp(*v
, "silent"))
308 ctrl
|= WINBIND_SILENT
;
309 else if (!strcasecmp(*v
, "use_authtok"))
310 ctrl
|= WINBIND_USE_AUTHTOK_ARG
;
311 else if (!strcasecmp(*v
, "use_first_pass"))
312 ctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
313 else if (!strcasecmp(*v
, "try_first_pass"))
314 ctrl
|= WINBIND_TRY_FIRST_PASS_ARG
;
315 else if (!strcasecmp(*v
, "unknown_ok"))
316 ctrl
|= WINBIND_UNKNOWN_OK_ARG
;
317 else if (!strncasecmp(*v
, "require_membership_of",
318 strlen("require_membership_of")))
319 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
320 else if (!strncasecmp(*v
, "require-membership-of",
321 strlen("require-membership-of")))
322 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
323 else if (!strcasecmp(*v
, "krb5_auth"))
324 ctrl
|= WINBIND_KRB5_AUTH
;
325 else if (!strncasecmp(*v
, "krb5_ccache_type",
326 strlen("krb5_ccache_type")))
327 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
328 else if (!strcasecmp(*v
, "cached_login"))
329 ctrl
|= WINBIND_CACHED_LOGIN
;
331 _pam_log(pamh
, ctrl
, LOG_ERR
,
332 "pam_parse: unknown option: %s", *v
);
342 iniparser_freedict(d
);
349 static void _pam_winbind_cleanup_func(pam_handle_t
*pamh
,
353 int ctrl
= _pam_parse(pamh
, 0, 0, NULL
, NULL
);
354 if (_pam_log_is_debug_state_enabled(ctrl
)) {
355 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
356 "[pamh: %p] CLEAN: cleaning up PAM data %p "
357 "(error_status = %d)", pamh
, data
,
364 static const struct ntstatus_errors
{
365 const char *ntstatus_string
;
366 const char *error_string
;
367 } ntstatus_errors
[] = {
370 {"NT_STATUS_BACKUP_CONTROLLER",
371 "No primary Domain Controler available"},
372 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
373 "No domain controllers found"},
374 {"NT_STATUS_NO_LOGON_SERVERS",
376 {"NT_STATUS_PWD_TOO_SHORT",
377 "Password too short"},
378 {"NT_STATUS_PWD_TOO_RECENT",
379 "The password of this user is too recent to change"},
380 {"NT_STATUS_PWD_HISTORY_CONFLICT",
381 "Password is already in password history"},
382 {"NT_STATUS_PASSWORD_EXPIRED",
383 "Your password has expired"},
384 {"NT_STATUS_PASSWORD_MUST_CHANGE",
385 "You need to change your password now"},
386 {"NT_STATUS_INVALID_WORKSTATION",
387 "You are not allowed to logon from this workstation"},
388 {"NT_STATUS_INVALID_LOGON_HOURS",
389 "You are not allowed to logon at this time"},
390 {"NT_STATUS_ACCOUNT_EXPIRED",
391 "Your account has expired. "
392 "Please contact your System administrator"}, /* SCNR */
393 {"NT_STATUS_ACCOUNT_DISABLED",
394 "Your account is disabled. "
395 "Please contact your System administrator"}, /* SCNR */
396 {"NT_STATUS_ACCOUNT_LOCKED_OUT",
397 "Your account has been locked. "
398 "Please contact your System administrator"}, /* SCNR */
399 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
400 "Invalid Trust Account"},
401 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
402 "Invalid Trust Account"},
403 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
404 "Invalid Trust Account"},
405 {"NT_STATUS_ACCESS_DENIED",
410 const char *_get_ntstatus_error_string(const char *nt_status_string
)
413 for (i
=0; ntstatus_errors
[i
].ntstatus_string
!= NULL
; i
++) {
414 if (!strcasecmp(ntstatus_errors
[i
].ntstatus_string
,
416 return ntstatus_errors
[i
].error_string
;
422 /* --- authentication management functions --- */
424 /* Attempt a conversation */
426 static int converse(pam_handle_t
*pamh
,
428 struct pam_message
**message
,
429 struct pam_response
**response
)
432 struct pam_conv
*conv
;
434 retval
= _pam_get_item(pamh
, PAM_CONV
, &conv
);
435 if (retval
== PAM_SUCCESS
) {
436 retval
= conv
->conv(nargs
,
437 (const struct pam_message
**)message
,
438 response
, conv
->appdata_ptr
);
441 return retval
; /* propagate error status */
445 static int _make_remark(pam_handle_t
* pamh
,
450 int retval
= PAM_SUCCESS
;
452 struct pam_message
*pmsg
[1], msg
[1];
453 struct pam_response
*resp
;
455 if (flags
& WINBIND_SILENT
) {
460 msg
[0].msg
= discard_const_p(char, text
);
461 msg
[0].msg_style
= type
;
464 retval
= converse(pamh
, 1, pmsg
, &resp
);
467 _pam_drop_reply(resp
, 1);
472 static int _make_remark_v(pam_handle_t
*pamh
,
481 ret
= vasprintf(&var
, format
, args
);
483 _pam_log(pamh
, 0, LOG_ERR
, "memory allocation failure");
487 ret
= _make_remark(pamh
, flags
, type
, var
);
492 static int _make_remark_format(pam_handle_t
* pamh
, int flags
, int type
, const char *format
, ...) PRINTF_ATTRIBUTE(4,5);
493 static int _make_remark_format(pam_handle_t
* pamh
, int flags
, int type
, const char *format
, ...)
498 va_start(args
, format
);
499 ret
= _make_remark_v(pamh
, flags
, type
, format
, args
);
504 static int pam_winbind_request(pam_handle_t
*pamh
,
506 enum winbindd_cmd req_type
,
507 struct winbindd_request
*request
,
508 struct winbindd_response
*response
)
510 /* Fill in request and send down pipe */
511 winbindd_init_request(request
, req_type
);
513 if (winbind_write_sock(request
, sizeof(*request
), 0, 0) == -1) {
514 _pam_log(pamh
, ctrl
, LOG_ERR
,
515 "pam_winbind_request: write to socket failed!");
516 winbind_close_sock();
517 return PAM_SERVICE_ERR
;
521 if (winbindd_read_reply(response
) == -1) {
522 _pam_log(pamh
, ctrl
, LOG_ERR
,
523 "pam_winbind_request: read from socket failed!");
524 winbind_close_sock();
525 return PAM_SERVICE_ERR
;
528 /* We are done with the socket - close it and avoid mischeif */
529 winbind_close_sock();
531 /* Copy reply data from socket */
532 if (response
->result
== WINBINDD_OK
) {
536 /* no need to check for pam_error codes for getpwnam() */
539 case WINBINDD_GETPWNAM
:
540 case WINBINDD_LOOKUPNAME
:
541 if (strlen(response
->data
.auth
.nt_status_string
) > 0) {
542 _pam_log(pamh
, ctrl
, LOG_ERR
,
543 "request failed, NT error was %s",
544 response
->data
.auth
.nt_status_string
);
546 _pam_log(pamh
, ctrl
, LOG_ERR
, "request failed");
548 return PAM_USER_UNKNOWN
;
553 if (response
->data
.auth
.pam_error
!= PAM_SUCCESS
) {
554 _pam_log(pamh
, ctrl
, LOG_ERR
,
555 "request failed: %s, "
556 "PAM error was %s (%d), NT error was %s",
557 response
->data
.auth
.error_string
,
558 pam_strerror(pamh
, response
->data
.auth
.pam_error
),
559 response
->data
.auth
.pam_error
,
560 response
->data
.auth
.nt_status_string
);
561 return response
->data
.auth
.pam_error
;
564 _pam_log(pamh
, ctrl
, LOG_ERR
, "request failed, but PAM error 0!");
566 return PAM_SERVICE_ERR
;
569 static int pam_winbind_request_log(pam_handle_t
*pamh
,
571 enum winbindd_cmd req_type
,
572 struct winbindd_request
*request
,
573 struct winbindd_response
*response
,
578 retval
= pam_winbind_request(pamh
, ctrl
, req_type
, request
, response
);
582 /* incorrect password */
583 _pam_log(pamh
, ctrl
, LOG_WARNING
, "user '%s' denied access "
584 "(incorrect password or invalid membership)", user
);
586 case PAM_ACCT_EXPIRED
:
587 /* account expired */
588 _pam_log(pamh
, ctrl
, LOG_WARNING
, "user '%s' account expired",
591 case PAM_AUTHTOK_EXPIRED
:
592 /* password expired */
593 _pam_log(pamh
, ctrl
, LOG_WARNING
, "user '%s' password expired",
596 case PAM_NEW_AUTHTOK_REQD
:
597 /* new password required */
598 _pam_log(pamh
, ctrl
, LOG_WARNING
, "user '%s' new password "
601 case PAM_USER_UNKNOWN
:
602 /* the user does not exist */
603 _pam_log_debug(pamh
, ctrl
, LOG_NOTICE
, "user '%s' not found",
605 if (ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
610 /* Otherwise, the authentication looked good */
614 case WINBINDD_PAM_AUTH
:
615 _pam_log(pamh
, ctrl
, LOG_NOTICE
,
616 "user '%s' granted access", user
);
618 case WINBINDD_PAM_CHAUTHTOK
:
619 _pam_log(pamh
, ctrl
, LOG_NOTICE
,
620 "user '%s' password changed", user
);
623 _pam_log(pamh
, ctrl
, LOG_NOTICE
,
624 "user '%s' OK", user
);
630 /* we don't know anything about this return value */
631 _pam_log(pamh
, ctrl
, LOG_ERR
,
632 "internal module error (retval = %d, user = '%s')",
639 * send a password expiry message if required
641 * @param pamh PAM handle
642 * @param ctrl PAM winbind options.
643 * @param next_change expected (calculated) next expiry date.
644 * @param already_expired pointer to a boolean to indicate if the password is
647 * @return boolean Returns true if message has been sent, false if not.
650 static bool _pam_send_password_expiry_message(pam_handle_t
*pamh
,
655 bool *already_expired
)
658 struct tm tm_now
, tm_next_change
;
660 if (already_expired
) {
661 *already_expired
= false;
664 if (next_change
<= now
) {
665 PAM_WB_REMARK_DIRECT(pamh
, ctrl
, "NT_STATUS_PASSWORD_EXPIRED");
666 if (already_expired
) {
667 *already_expired
= true;
672 if ((next_change
< 0) ||
673 (next_change
> now
+ warn_pwd_expire
* SECONDS_PER_DAY
)) {
677 if ((localtime_r(&now
, &tm_now
) == NULL
) ||
678 (localtime_r(&next_change
, &tm_next_change
) == NULL
)) {
682 days
= (tm_next_change
.tm_yday
+tm_next_change
.tm_year
*365) -
683 (tm_now
.tm_yday
+tm_now
.tm_year
*365);
686 _make_remark(pamh
, ctrl
, PAM_TEXT_INFO
,
687 "Your password expires today");
691 if (days
> 0 && days
< warn_pwd_expire
) {
692 _make_remark_format(pamh
, ctrl
, PAM_TEXT_INFO
,
693 "Your password will expire in %d %s",
694 days
, (days
> 1) ? "days":"day");
702 * Send a warning if the password expires in the near future
704 * @param pamh PAM handle
705 * @param ctrl PAM winbind options.
706 * @param response The full authentication response structure.
707 * @param already_expired boolean, is the pwd already expired?
712 static void _pam_warn_password_expiry(pam_handle_t
*pamh
,
714 const struct winbindd_response
*response
,
716 bool *already_expired
)
718 time_t now
= time(NULL
);
719 time_t next_change
= 0;
721 if (already_expired
) {
722 *already_expired
= false;
725 /* accounts with ACB_PWNOEXP set never receive a warning */
726 if (response
->data
.auth
.info3
.acct_flags
& ACB_PWNOEXP
) {
730 /* no point in sending a warning if this is a grace logon */
731 if (PAM_WB_GRACE_LOGON(response
->data
.auth
.info3
.user_flgs
)) {
735 /* check if the info3 must change timestamp has been set */
736 next_change
= response
->data
.auth
.info3
.pass_must_change_time
;
738 if (_pam_send_password_expiry_message(pamh
, flags
, next_change
, now
,
744 /* now check for the global password policy */
745 /* good catch from Ralf Haferkamp: an expiry of "never" is translated
747 if (response
->data
.auth
.policy
.expire
<= 0) {
751 next_change
= response
->data
.auth
.info3
.pass_last_set_time
+
752 response
->data
.auth
.policy
.expire
;
754 if (_pam_send_password_expiry_message(pamh
, flags
, next_change
, now
,
760 /* no warning sent */
763 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
766 * Append a string, making sure not to overflow and to always return a
767 * NULL-terminated string.
769 * @param dest Destination string buffer (must already be NULL-terminated).
770 * @param src Source string buffer.
771 * @param dest_buffer_size Size of dest buffer in bytes.
773 * @return false if dest buffer is not big enough (no bytes copied), true on
777 static bool safe_append_string(char *dest
,
779 int dest_buffer_size
)
781 int dest_length
= strlen(dest
);
782 int src_length
= strlen(src
);
784 if (dest_length
+ src_length
+ 1 > dest_buffer_size
) {
788 memcpy(dest
+ dest_length
, src
, src_length
+ 1);
793 * Convert a names into a SID string, appending it to a buffer.
795 * @param pamh PAM handle
796 * @param ctrl PAM winbind options.
797 * @param user User in PAM request.
798 * @param name Name to convert.
799 * @param sid_list_buffer Where to append the string sid.
800 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
802 * @return false on failure, true on success.
804 static bool winbind_name_to_sid_string(pam_handle_t
*pamh
,
808 char *sid_list_buffer
,
809 int sid_list_buffer_size
)
811 const char* sid_string
;
812 struct winbindd_response sid_response
;
815 if (IS_SID_STRING(name
)) {
818 struct winbindd_request sid_request
;
820 ZERO_STRUCT(sid_request
);
821 ZERO_STRUCT(sid_response
);
823 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
824 "no sid given, looking up: %s\n", name
);
826 /* fortunatly winbindd can handle non-separated names */
827 strncpy(sid_request
.data
.name
.name
, name
,
828 sizeof(sid_request
.data
.name
.name
) - 1);
830 if (pam_winbind_request_log(pamh
, ctrl
, WINBINDD_LOOKUPNAME
,
831 &sid_request
, &sid_response
,
833 _pam_log(pamh
, ctrl
, LOG_INFO
,
834 "could not lookup name: %s\n", name
);
838 sid_string
= sid_response
.data
.sid
.sid
;
841 if (!safe_append_string(sid_list_buffer
, sid_string
,
842 sid_list_buffer_size
)) {
850 * Convert a list of names into a list of sids.
852 * @param pamh PAM handle
853 * @param ctrl PAM winbind options.
854 * @param user User in PAM request.
855 * @param name_list List of names or string sids, separated by commas.
856 * @param sid_list_buffer Where to put the list of string sids.
857 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
859 * @return false on failure, true on success.
861 static bool winbind_name_list_to_sid_string_list(pam_handle_t
*pamh
,
864 const char *name_list
,
865 char *sid_list_buffer
,
866 int sid_list_buffer_size
)
869 char *current_name
= NULL
;
870 const char *search_location
;
873 if (sid_list_buffer_size
> 0) {
874 sid_list_buffer
[0] = 0;
877 search_location
= name_list
;
878 while ((comma
= strstr(search_location
, ",")) != NULL
) {
879 current_name
= strndup(search_location
,
880 comma
- search_location
);
881 if (NULL
== current_name
) {
885 if (!winbind_name_to_sid_string(pamh
, ctrl
, user
,
888 sid_list_buffer_size
)) {
892 SAFE_FREE(current_name
);
894 if (!safe_append_string(sid_list_buffer
, ",",
895 sid_list_buffer_size
)) {
899 search_location
= comma
+ 1;
902 if (!winbind_name_to_sid_string(pamh
, ctrl
, user
, search_location
,
904 sid_list_buffer_size
)) {
911 SAFE_FREE(current_name
);
916 * put krb5ccname variable into environment
918 * @param pamh PAM handle
919 * @param ctrl PAM winbind options.
920 * @param krb5ccname env variable retrieved from winbindd.
925 static void _pam_setup_krb5_env(pam_handle_t
*pamh
,
927 const char *krb5ccname
)
932 if (off(ctrl
, WINBIND_KRB5_AUTH
)) {
936 if (!krb5ccname
|| (strlen(krb5ccname
) == 0)) {
940 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
941 "request returned KRB5CCNAME: %s", krb5ccname
);
943 if (snprintf(var
, sizeof(var
), "KRB5CCNAME=%s", krb5ccname
) == -1) {
947 ret
= pam_putenv(pamh
, var
);
949 _pam_log(pamh
, ctrl
, LOG_ERR
,
950 "failed to set KRB5CCNAME to %s: %s",
951 var
, pam_strerror(pamh
, ret
));
956 * Set string into the PAM stack.
958 * @param pamh PAM handle
959 * @param ctrl PAM winbind options.
960 * @param data_name Key name for pam_set_data.
961 * @param value String value.
966 static void _pam_set_data_string(pam_handle_t
*pamh
,
968 const char *data_name
,
973 if (!data_name
|| !value
|| (strlen(data_name
) == 0) ||
974 (strlen(value
) == 0)) {
978 ret
= pam_set_data(pamh
, data_name
, (void *)strdup(value
),
979 _pam_winbind_cleanup_func
);
981 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
982 "Could not set data %s: %s\n",
983 data_name
, pam_strerror(pamh
, ret
));
989 * Set info3 strings into the PAM stack.
991 * @param pamh PAM handle
992 * @param ctrl PAM winbind options.
993 * @param data_name Key name for pam_set_data.
994 * @param value String value.
999 static void _pam_set_data_info3(pam_handle_t
*pamh
,
1001 struct winbindd_response
*response
)
1003 _pam_set_data_string(pamh
, ctrl
, PAM_WINBIND_HOMEDIR
,
1004 response
->data
.auth
.info3
.home_dir
);
1005 _pam_set_data_string(pamh
, ctrl
, PAM_WINBIND_LOGONSCRIPT
,
1006 response
->data
.auth
.info3
.logon_script
);
1007 _pam_set_data_string(pamh
, ctrl
, PAM_WINBIND_LOGONSERVER
,
1008 response
->data
.auth
.info3
.logon_srv
);
1009 _pam_set_data_string(pamh
, ctrl
, PAM_WINBIND_PROFILEPATH
,
1010 response
->data
.auth
.info3
.profile_path
);
1014 * Free info3 strings in the PAM stack.
1016 * @param pamh PAM handle
1021 static void _pam_free_data_info3(pam_handle_t
*pamh
)
1023 pam_set_data(pamh
, PAM_WINBIND_HOMEDIR
, NULL
, NULL
);
1024 pam_set_data(pamh
, PAM_WINBIND_LOGONSCRIPT
, NULL
, NULL
);
1025 pam_set_data(pamh
, PAM_WINBIND_LOGONSERVER
, NULL
, NULL
);
1026 pam_set_data(pamh
, PAM_WINBIND_PROFILEPATH
, NULL
, NULL
);
1030 * Send PAM_ERROR_MSG for cached or grace logons.
1032 * @param pamh PAM handle
1033 * @param ctrl PAM winbind options.
1034 * @param username User in PAM request.
1035 * @param info3_user_flgs Info3 flags containing logon type bits.
1040 static void _pam_warn_logon_type(pam_handle_t
*pamh
,
1042 const char *username
,
1043 uint32_t info3_user_flgs
)
1045 /* inform about logon type */
1046 if (PAM_WB_GRACE_LOGON(info3_user_flgs
)) {
1048 _make_remark(pamh
, ctrl
, PAM_ERROR_MSG
,
1050 "Please change your password as soon you're "
1052 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1053 "User %s logged on using grace logon\n",
1056 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs
)) {
1058 _make_remark(pamh
, ctrl
, PAM_ERROR_MSG
,
1059 "Domain Controller unreachable, "
1060 "using cached credentials instead. "
1061 "Network resources may be unavailable");
1062 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1063 "User %s logged on using cached credentials\n",
1069 * Send PAM_ERROR_MSG for krb5 errors.
1071 * @param pamh PAM handle
1072 * @param ctrl PAM winbind options.
1073 * @param username User in PAM request.
1074 * @param info3_user_flgs Info3 flags containing logon type bits.
1079 static void _pam_warn_krb5_failure(pam_handle_t
*pamh
,
1081 const char *username
,
1082 uint32_t info3_user_flgs
)
1084 if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs
)) {
1085 _make_remark(pamh
, ctrl
, PAM_ERROR_MSG
,
1086 "Failed to establish your Kerberos Ticket cache "
1087 "due time differences\n"
1088 "with the domain controller. "
1089 "Please verify the system time.\n");
1090 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1091 "User %s: Clock skew when getting Krb5 TGT\n",
1097 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
1099 * @param response The struct winbindd_response.
1101 * @return string (caller needs to free).
1104 static char *_pam_compose_pwd_restriction_string(struct winbindd_response
*response
)
1107 size_t offset
= 0, ret
= 0, str_size
= 1024;
1109 str
= (char *)malloc(str_size
);
1114 memset(str
, '\0', str_size
);
1116 offset
= snprintf(str
, str_size
, "Your password ");
1121 if (response
->data
.auth
.policy
.min_length_password
> 0) {
1122 ret
= snprintf(str
+offset
, str_size
-offset
,
1123 "must be at least %d characters; ",
1124 response
->data
.auth
.policy
.min_length_password
);
1131 if (response
->data
.auth
.policy
.password_history
> 0) {
1132 ret
= snprintf(str
+offset
, str_size
-offset
,
1133 "cannot repeat any of your previous %d "
1135 response
->data
.auth
.policy
.password_history
);
1142 if (response
->data
.auth
.policy
.password_properties
&
1143 DOMAIN_PASSWORD_COMPLEX
) {
1144 ret
= snprintf(str
+offset
, str_size
-offset
,
1145 "must contain capitals, numerals "
1147 "and cannot contain your account "
1155 ret
= snprintf(str
+offset
, str_size
-offset
,
1156 "Please type a different password. "
1157 "Type a password which meets these requirements in "
1158 "both text boxes.");
1170 /* talk to winbindd */
1171 static int winbind_auth_request(pam_handle_t
* pamh
,
1177 const int warn_pwd_expire
,
1178 struct winbindd_response
*p_response
,
1179 time_t *pwd_last_set
,
1182 struct winbindd_request request
;
1183 struct winbindd_response response
;
1185 bool already_expired
= false;
1187 ZERO_STRUCT(request
);
1188 ZERO_STRUCT(response
);
1194 strncpy(request
.data
.auth
.user
, user
,
1195 sizeof(request
.data
.auth
.user
)-1);
1197 strncpy(request
.data
.auth
.pass
, pass
,
1198 sizeof(request
.data
.auth
.pass
)-1);
1200 request
.data
.auth
.krb5_cc_type
[0] = '\0';
1201 request
.data
.auth
.uid
= -1;
1203 request
.flags
= WBFLAG_PAM_INFO3_TEXT
| WBFLAG_PAM_GET_PWD_POLICY
;
1205 /* Krb5 auth always has to go against the KDC of the user's realm */
1207 if (ctrl
& WINBIND_KRB5_AUTH
) {
1208 request
.flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
1211 if (ctrl
& (WINBIND_KRB5_AUTH
|WINBIND_CACHED_LOGIN
)) {
1212 struct passwd
*pwd
= NULL
;
1214 pwd
= getpwnam(user
);
1216 return PAM_USER_UNKNOWN
;
1218 request
.data
.auth
.uid
= pwd
->pw_uid
;
1221 if (ctrl
& WINBIND_KRB5_AUTH
) {
1223 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1224 "enabling krb5 login flag\n");
1226 request
.flags
|= WBFLAG_PAM_KRB5
|
1227 WBFLAG_PAM_FALLBACK_AFTER_KRB5
;
1230 if (ctrl
& WINBIND_CACHED_LOGIN
) {
1231 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1232 "enabling cached login flag\n");
1233 request
.flags
|= WBFLAG_PAM_CACHED_LOGIN
;
1238 request
.flags
|= WBFLAG_PAM_UNIX_NAME
;
1241 if (cctype
!= NULL
) {
1242 strncpy(request
.data
.auth
.krb5_cc_type
, cctype
,
1243 sizeof(request
.data
.auth
.krb5_cc_type
) - 1);
1244 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1245 "enabling request for a %s krb5 ccache\n",
1249 request
.data
.auth
.require_membership_of_sid
[0] = '\0';
1251 if (member
!= NULL
) {
1253 if (!winbind_name_list_to_sid_string_list(pamh
, ctrl
, user
,
1255 request
.data
.auth
.require_membership_of_sid
,
1256 sizeof(request
.data
.auth
.require_membership_of_sid
))) {
1258 _pam_log_debug(pamh
, ctrl
, LOG_ERR
,
1259 "failed to serialize membership of sid "
1260 "\"%s\"\n", member
);
1261 return PAM_AUTH_ERR
;
1265 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_PAM_AUTH
,
1266 &request
, &response
, user
);
1269 *pwd_last_set
= response
.data
.auth
.info3
.pass_last_set_time
;
1273 /* We want to process the response in the caller. */
1274 *p_response
= response
;
1279 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1280 "NT_STATUS_PASSWORD_EXPIRED");
1281 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1282 "NT_STATUS_PASSWORD_MUST_CHANGE");
1283 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1284 "NT_STATUS_INVALID_WORKSTATION");
1285 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1286 "NT_STATUS_INVALID_LOGON_HOURS");
1287 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1288 "NT_STATUS_ACCOUNT_EXPIRED");
1289 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1290 "NT_STATUS_ACCOUNT_DISABLED");
1291 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1292 "NT_STATUS_ACCOUNT_LOCKED_OUT");
1293 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1294 "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
1295 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1296 "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
1297 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1298 "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
1299 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1300 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1301 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1302 "NT_STATUS_NO_LOGON_SERVERS");
1303 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1304 "NT_STATUS_WRONG_PASSWORD");
1305 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1306 "NT_STATUS_ACCESS_DENIED");
1309 if (ret
== PAM_SUCCESS
) {
1311 /* warn a user if the password is about to expire soon */
1312 _pam_warn_password_expiry(pamh
, ctrl
, &response
,
1316 if (already_expired
== true) {
1317 SMB_TIME_T last_set
;
1318 last_set
= response
.data
.auth
.info3
.pass_last_set_time
;
1319 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1320 "Password has expired "
1321 "(Password was last set: %lld, "
1322 "the policy says it should expire here "
1323 "%lld (now it's: %lu))\n",
1324 (long long int)last_set
,
1325 (long long int)last_set
+
1326 response
.data
.auth
.policy
.expire
,
1329 return PAM_AUTHTOK_EXPIRED
;
1332 /* inform about logon type */
1333 _pam_warn_logon_type(pamh
, ctrl
, user
,
1334 response
.data
.auth
.info3
.user_flgs
);
1336 /* inform about krb5 failures */
1337 _pam_warn_krb5_failure(pamh
, ctrl
, user
,
1338 response
.data
.auth
.info3
.user_flgs
);
1340 /* set some info3 info for other modules in the stack */
1341 _pam_set_data_info3(pamh
, ctrl
, &response
);
1343 /* put krb5ccname into env */
1344 _pam_setup_krb5_env(pamh
, ctrl
, response
.data
.auth
.krb5ccname
);
1346 /* If winbindd returned a username, return the pointer to it
1348 if (user_ret
&& response
.data
.auth
.unix_username
[0]) {
1349 /* We have to trust it's a null terminated string. */
1350 *user_ret
= strndup(response
.data
.auth
.unix_username
,
1351 sizeof(response
.data
.auth
.unix_username
) - 1);
1358 /* talk to winbindd */
1359 static int winbind_chauthtok_request(pam_handle_t
* pamh
,
1362 const char *oldpass
,
1363 const char *newpass
,
1364 time_t pwd_last_set
)
1366 struct winbindd_request request
;
1367 struct winbindd_response response
;
1370 ZERO_STRUCT(request
);
1371 ZERO_STRUCT(response
);
1373 if (request
.data
.chauthtok
.user
== NULL
) {
1377 strncpy(request
.data
.chauthtok
.user
, user
,
1378 sizeof(request
.data
.chauthtok
.user
) - 1);
1380 if (oldpass
!= NULL
) {
1381 strncpy(request
.data
.chauthtok
.oldpass
, oldpass
,
1382 sizeof(request
.data
.chauthtok
.oldpass
) - 1);
1384 request
.data
.chauthtok
.oldpass
[0] = '\0';
1387 if (newpass
!= NULL
) {
1388 strncpy(request
.data
.chauthtok
.newpass
, newpass
,
1389 sizeof(request
.data
.chauthtok
.newpass
) - 1);
1391 request
.data
.chauthtok
.newpass
[0] = '\0';
1394 if (ctrl
& WINBIND_KRB5_AUTH
) {
1395 request
.flags
= WBFLAG_PAM_KRB5
|
1396 WBFLAG_PAM_CONTACT_TRUSTDOM
;
1399 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_PAM_CHAUTHTOK
,
1400 &request
, &response
, user
);
1402 if (ret
== PAM_SUCCESS
) {
1406 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1407 "NT_STATUS_BACKUP_CONTROLLER");
1408 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1409 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1410 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1411 "NT_STATUS_NO_LOGON_SERVERS");
1412 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1413 "NT_STATUS_ACCESS_DENIED");
1415 /* TODO: tell the min pwd length ? */
1416 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1417 "NT_STATUS_PWD_TOO_SHORT");
1419 /* TODO: tell the minage ? */
1420 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1421 "NT_STATUS_PWD_TOO_RECENT");
1423 /* TODO: tell the history length ? */
1424 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, ctrl
, response
,
1425 "NT_STATUS_PWD_HISTORY_CONFLICT");
1427 if (!strcasecmp(response
.data
.auth
.nt_status_string
,
1428 "NT_STATUS_PASSWORD_RESTRICTION")) {
1430 char *pwd_restriction_string
= NULL
;
1431 SMB_TIME_T min_pwd_age
;
1432 uint32_t reject_reason
= response
.data
.auth
.reject_reason
;
1433 min_pwd_age
= response
.data
.auth
.policy
.min_passwordage
;
1435 /* FIXME: avoid to send multiple PAM messages after another */
1436 switch (reject_reason
) {
1439 case SAMR_REJECT_OTHER
:
1440 if ((min_pwd_age
> 0) &&
1441 (pwd_last_set
+ min_pwd_age
> time(NULL
))) {
1442 PAM_WB_REMARK_DIRECT(pamh
, ctrl
,
1443 "NT_STATUS_PWD_TOO_RECENT");
1446 case SAMR_REJECT_TOO_SHORT
:
1447 PAM_WB_REMARK_DIRECT(pamh
, ctrl
,
1448 "NT_STATUS_PWD_TOO_SHORT");
1450 case SAMR_REJECT_IN_HISTORY
:
1451 PAM_WB_REMARK_DIRECT(pamh
, ctrl
,
1452 "NT_STATUS_PWD_HISTORY_CONFLICT");
1454 case SAMR_REJECT_COMPLEXITY
:
1455 _make_remark(pamh
, ctrl
, PAM_ERROR_MSG
,
1456 "Password does not meet "
1457 "complexity requirements");
1460 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1461 "unknown password change "
1462 "reject reason: %d",
1467 pwd_restriction_string
=
1468 _pam_compose_pwd_restriction_string(&response
);
1469 if (pwd_restriction_string
) {
1470 _make_remark(pamh
, ctrl
, PAM_ERROR_MSG
,
1471 pwd_restriction_string
);
1472 SAFE_FREE(pwd_restriction_string
);
1480 * Checks if a user has an account
1483 * 1 = User not found
1487 static int valid_user(pam_handle_t
*pamh
,
1491 /* check not only if the user is available over NSS calls, also make
1492 * sure it's really a winbind user, this is important when stacking PAM
1493 * modules in the 'account' or 'password' facility. */
1495 struct passwd
*pwd
= NULL
;
1496 struct winbindd_request request
;
1497 struct winbindd_response response
;
1500 ZERO_STRUCT(request
);
1501 ZERO_STRUCT(response
);
1503 pwd
= getpwnam(user
);
1508 strncpy(request
.data
.username
, user
,
1509 sizeof(request
.data
.username
) - 1);
1511 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_GETPWNAM
,
1512 &request
, &response
, user
);
1515 case PAM_USER_UNKNOWN
:
1525 static char *_pam_delete(register char *xx
)
1533 * obtain a password from the user
1536 static int _winbind_read_password(pam_handle_t
* pamh
,
1538 const char *comment
,
1539 const char *prompt1
,
1540 const char *prompt2
,
1548 _pam_log(pamh
, ctrl
, LOG_DEBUG
, "getting password (0x%08x)", ctrl
);
1551 * make sure nothing inappropriate gets returned
1554 *pass
= token
= NULL
;
1557 * which authentication token are we getting?
1560 if (on(WINBIND__OLD_PASSWORD
, ctrl
)) {
1561 authtok_flag
= PAM_OLDAUTHTOK
;
1563 authtok_flag
= PAM_AUTHTOK
;
1567 * should we obtain the password from a PAM item ?
1570 if (on(WINBIND_TRY_FIRST_PASS_ARG
, ctrl
) ||
1571 on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
1572 retval
= _pam_get_item(pamh
, authtok_flag
, &item
);
1573 if (retval
!= PAM_SUCCESS
) {
1575 _pam_log(pamh
, ctrl
, LOG_ALERT
,
1576 "pam_get_item returned error "
1577 "to unix-read-password");
1579 } else if (item
!= NULL
) { /* we have a password! */
1582 _pam_log(pamh
, ctrl
, LOG_DEBUG
,
1583 "pam_get_item returned a password");
1585 } else if (on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
1586 return PAM_AUTHTOK_RECOVER_ERR
; /* didn't work */
1587 } else if (on(WINBIND_USE_AUTHTOK_ARG
, ctrl
)
1588 && off(WINBIND__OLD_PASSWORD
, ctrl
)) {
1589 return PAM_AUTHTOK_RECOVER_ERR
;
1593 * getting here implies we will have to get the password from the
1598 struct pam_message msg
[3], *pmsg
[3];
1599 struct pam_response
*resp
;
1602 /* prepare to converse */
1604 if (comment
!= NULL
&& off(ctrl
, WINBIND_SILENT
)) {
1606 msg
[0].msg_style
= PAM_TEXT_INFO
;
1607 msg
[0].msg
= discard_const_p(char, comment
);
1614 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
1615 msg
[i
++].msg
= discard_const_p(char, prompt1
);
1618 if (prompt2
!= NULL
) {
1620 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
1621 msg
[i
++].msg
= discard_const_p(char, prompt2
);
1624 /* so call the conversation expecting i responses */
1626 retval
= converse(pamh
, i
, pmsg
, &resp
);
1628 if (retval
== PAM_SUCCESS
) {
1629 retval
= PAM_AUTHTOK_RECOVER_ERR
;
1633 if (retval
!= PAM_SUCCESS
) {
1634 _pam_drop_reply(resp
, i
);
1638 /* interpret the response */
1640 token
= x_strdup(resp
[i
- replies
].resp
);
1642 _pam_log(pamh
, ctrl
, LOG_NOTICE
,
1643 "could not recover "
1644 "authentication token");
1645 retval
= PAM_AUTHTOK_RECOVER_ERR
;
1650 /* verify that password entered correctly */
1651 if (!resp
[i
- 1].resp
||
1652 strcmp(token
, resp
[i
- 1].resp
)) {
1653 _pam_delete(token
); /* mistyped */
1654 retval
= PAM_AUTHTOK_RECOVER_ERR
;
1655 _make_remark(pamh
, ctrl
, PAM_ERROR_MSG
,
1661 * tidy up the conversation (resp_retcode) is ignored
1662 * -- what is it for anyway? AGM
1664 _pam_drop_reply(resp
, i
);
1668 if (retval
!= PAM_SUCCESS
) {
1669 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1670 "unable to obtain a password");
1673 /* 'token' is the entered password */
1675 /* we store this password as an item */
1677 retval
= pam_set_item(pamh
, authtok_flag
, token
);
1678 _pam_delete(token
); /* clean it up */
1679 if (retval
!= PAM_SUCCESS
||
1680 (retval
= _pam_get_item(pamh
, authtok_flag
, &item
)) != PAM_SUCCESS
) {
1682 _pam_log(pamh
, ctrl
, LOG_CRIT
, "error manipulating password");
1688 item
= NULL
; /* break link to password */
1693 const char *get_conf_item_string(const pam_handle_t
*pamh
,
1702 const char *parm_opt
= NULL
;
1704 if (!(ctrl
& config_flag
)) {
1708 /* let the pam opt take precedence over the pam_winbind.conf option */
1709 for (i
=0; i
<argc
; i
++) {
1711 if ((strncmp(argv
[i
], item
, strlen(item
)) == 0)) {
1714 if ((p
= strchr(argv
[i
], '=')) == NULL
) {
1715 _pam_log(pamh
, ctrl
, LOG_INFO
,
1716 "no \"=\" delimiter for \"%s\" found\n",
1720 _pam_log_debug(pamh
, ctrl
, LOG_INFO
,
1721 "PAM config: %s '%s'\n", item
, p
+1);
1729 if (!asprintf(&key
, "global:%s", item
)) {
1733 parm_opt
= iniparser_getstr(d
, key
);
1736 _pam_log_debug(pamh
, ctrl
, LOG_INFO
, "CONFIG file: %s '%s'\n",
1743 int get_config_item_int(const pam_handle_t
*pamh
,
1751 int i
, parm_opt
= -1;
1753 if (!(ctrl
& config_flag
)) {
1757 /* let the pam opt take precedence over the pam_winbind.conf option */
1758 for (i
= 0; i
< argc
; i
++) {
1760 if ((strncmp(argv
[i
], item
, strlen(item
)) == 0)) {
1763 if ((p
= strchr(argv
[i
], '=')) == NULL
) {
1764 _pam_log(pamh
, ctrl
, LOG_INFO
,
1765 "no \"=\" delimiter for \"%s\" found\n",
1769 parm_opt
= atoi(p
+ 1);
1770 _pam_log_debug(pamh
, ctrl
, LOG_INFO
,
1771 "PAM config: %s '%d'\n",
1780 if (!asprintf(&key
, "global:%s", item
)) {
1784 parm_opt
= iniparser_getint(d
, key
, -1);
1787 _pam_log_debug(pamh
, ctrl
, LOG_INFO
,
1788 "CONFIG file: %s '%d'\n",
1795 const char *get_krb5_cc_type_from_config(const pam_handle_t
*pamh
,
1801 return get_conf_item_string(pamh
, argc
, argv
, ctrl
, d
,
1803 WINBIND_KRB5_CCACHE_TYPE
);
1806 const char *get_member_from_config(const pam_handle_t
*pamh
,
1812 const char *ret
= NULL
;
1813 ret
= get_conf_item_string(pamh
, argc
, argv
, ctrl
, d
,
1814 "require_membership_of",
1815 WINBIND_REQUIRED_MEMBERSHIP
);
1819 return get_conf_item_string(pamh
, argc
, argv
, ctrl
, d
,
1820 "require-membership-of",
1821 WINBIND_REQUIRED_MEMBERSHIP
);
1824 int get_warn_pwd_expire_from_config(const pam_handle_t
*pamh
,
1831 ret
= get_config_item_int(pamh
, argc
, argv
, ctrl
, d
,
1833 WINBIND_WARN_PWD_EXPIRE
);
1834 /* no or broken setting */
1836 return DEFAULT_DAYS_TO_WARN_BEFORE_PWD_EXPIRES
;
1842 * Retrieve the winbind separator.
1844 * @param pamh PAM handle
1845 * @param ctrl PAM winbind options.
1847 * @return string separator character. NULL on failure.
1850 static char winbind_get_separator(pam_handle_t
*pamh
,
1853 struct winbindd_request request
;
1854 struct winbindd_response response
;
1856 ZERO_STRUCT(request
);
1857 ZERO_STRUCT(response
);
1859 if (pam_winbind_request_log(pamh
, ctrl
, WINBINDD_INFO
,
1860 &request
, &response
, NULL
)) {
1864 return response
.data
.info
.winbind_separator
;
1868 * Convert a upn to a name.
1870 * @param pamh PAM handle
1871 * @param ctrl PAM winbind options.
1872 * @param upn USer UPN to be trabslated.
1874 * @return converted name. NULL pointer on failure. Caller needs to free.
1877 static char* winbind_upn_to_username(pam_handle_t
*pamh
,
1881 struct winbindd_request req
;
1882 struct winbindd_response resp
;
1885 int account_name_len
;
1888 /* This cannot work when the winbind separator = @ */
1890 sep
= winbind_get_separator(pamh
, ctrl
);
1891 if (!sep
|| sep
== '@') {
1895 /* Convert the UPN to a SID */
1900 strncpy(req
.data
.name
.dom_name
, "",
1901 sizeof(req
.data
.name
.dom_name
) - 1);
1902 strncpy(req
.data
.name
.name
, upn
,
1903 sizeof(req
.data
.name
.name
) - 1);
1904 retval
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_LOOKUPNAME
,
1906 if (retval
!= PAM_SUCCESS
) {
1910 /* Convert the the SID back to the sAMAccountName */
1913 strncpy(req
.data
.sid
, resp
.data
.sid
.sid
, sizeof(req
.data
.sid
)-1);
1915 retval
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_LOOKUPSID
,
1917 if (retval
!= PAM_SUCCESS
) {
1921 account_name_len
= asprintf(&account_name
, "%s\\%s",
1922 resp
.data
.name
.dom_name
,
1923 resp
.data
.name
.name
);
1925 return account_name
;
1929 int pam_sm_authenticate(pam_handle_t
*pamh
, int flags
,
1930 int argc
, const char **argv
)
1932 const char *username
;
1933 const char *password
;
1934 const char *member
= NULL
;
1935 const char *cctype
= NULL
;
1936 int warn_pwd_expire
;
1937 int retval
= PAM_AUTH_ERR
;
1938 dictionary
*d
= NULL
;
1939 char *username_ret
= NULL
;
1940 char *new_authtok_required
= NULL
;
1941 char *real_username
= NULL
;
1943 /* parse arguments */
1944 int ctrl
= _pam_parse(pamh
, flags
, argc
, argv
, &d
);
1946 retval
= PAM_SYSTEM_ERR
;
1950 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", pamh
, ctrl
, flags
);
1952 /* Get the username */
1953 retval
= pam_get_user(pamh
, &username
, NULL
);
1954 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
1955 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1956 "can not get the username");
1957 retval
= PAM_SERVICE_ERR
;
1963 /* Decode the user name since AIX does not support logn user
1964 names by default. The name is encoded as _#uid. */
1966 if (username
[0] == '_') {
1967 uid_t id
= atoi(&username
[1]);
1968 struct passwd
*pw
= NULL
;
1970 if ((id
!=0) && ((pw
= getpwuid(id
)) != NULL
)) {
1971 real_username
= strdup(pw
->pw_name
);
1976 if (!real_username
) {
1977 /* Just making a copy of the username we got from PAM */
1978 if ((real_username
= strdup(username
)) == NULL
) {
1979 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
1980 "memory allocation failure when copying "
1982 retval
= PAM_SERVICE_ERR
;
1987 /* Maybe this was a UPN */
1989 if (strchr(real_username
, '@') != NULL
) {
1990 char *samaccountname
= NULL
;
1992 samaccountname
= winbind_upn_to_username(pamh
, ctrl
,
1994 if (samaccountname
) {
1995 free(real_username
);
1996 real_username
= samaccountname
;
2000 retval
= _winbind_read_password(pamh
, ctrl
, NULL
,
2004 if (retval
!= PAM_SUCCESS
) {
2005 _pam_log(pamh
, ctrl
, LOG_ERR
,
2006 "Could not retrieve user's password");
2007 retval
= PAM_AUTHTOK_ERR
;
2011 /* Let's not give too much away in the log file */
2013 #ifdef DEBUG_PASSWORD
2014 _pam_log_debug(pamh
, ctrl
, LOG_INFO
,
2015 "Verify user '%s' with password '%s'",
2016 real_username
, password
);
2018 _pam_log_debug(pamh
, ctrl
, LOG_INFO
,
2019 "Verify user '%s'", real_username
);
2022 member
= get_member_from_config(pamh
, argc
, argv
, ctrl
, d
);
2024 cctype
= get_krb5_cc_type_from_config(pamh
, argc
, argv
, ctrl
, d
);
2026 warn_pwd_expire
= get_warn_pwd_expire_from_config(pamh
, argc
, argv
,
2029 /* Now use the username to look up password */
2030 retval
= winbind_auth_request(pamh
, ctrl
, real_username
, password
,
2031 member
, cctype
, warn_pwd_expire
, NULL
,
2032 NULL
, &username_ret
);
2034 if (retval
== PAM_NEW_AUTHTOK_REQD
||
2035 retval
== PAM_AUTHTOK_EXPIRED
) {
2037 char *new_authtok_required_during_auth
= NULL
;
2039 if (!asprintf(&new_authtok_required
, "%d", retval
)) {
2040 retval
= PAM_BUF_ERR
;
2044 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
,
2045 new_authtok_required
,
2046 _pam_winbind_cleanup_func
);
2048 retval
= PAM_SUCCESS
;
2050 if (!asprintf(&new_authtok_required_during_auth
, "%d", true)) {
2051 retval
= PAM_BUF_ERR
;
2055 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2056 new_authtok_required_during_auth
,
2057 _pam_winbind_cleanup_func
);
2064 pam_set_item (pamh
, PAM_USER
, username_ret
);
2065 _pam_log_debug(pamh
, ctrl
, LOG_INFO
,
2066 "Returned user was '%s'", username_ret
);
2070 if (real_username
) {
2071 free(real_username
);
2075 iniparser_freedict(d
);
2078 if (!new_authtok_required
) {
2079 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, NULL
, NULL
);
2082 if (retval
!= PAM_SUCCESS
) {
2083 _pam_free_data_info3(pamh
);
2086 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", pamh
, ctrl
, retval
);
2092 int pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
2093 int argc
, const char **argv
)
2095 int ret
= PAM_SYSTEM_ERR
;
2096 dictionary
*d
= NULL
;
2098 /* parse arguments */
2099 int ctrl
= _pam_parse(pamh
, flags
, argc
, argv
, &d
);
2101 ret
= PAM_SYSTEM_ERR
;
2105 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", pamh
, ctrl
, flags
);
2107 switch (flags
& ~PAM_SILENT
) {
2109 case PAM_DELETE_CRED
:
2110 ret
= pam_sm_close_session(pamh
, flags
, argc
, argv
);
2112 case PAM_REFRESH_CRED
:
2113 _pam_log_debug(pamh
, ctrl
, LOG_WARNING
,
2114 "PAM_REFRESH_CRED not implemented");
2117 case PAM_REINITIALIZE_CRED
:
2118 _pam_log_debug(pamh
, ctrl
, LOG_WARNING
,
2119 "PAM_REINITIALIZE_CRED not implemented");
2122 case PAM_ESTABLISH_CRED
:
2123 _pam_log_debug(pamh
, ctrl
, LOG_WARNING
,
2124 "PAM_ESTABLISH_CRED not implemented");
2128 ret
= PAM_SYSTEM_ERR
;
2134 iniparser_freedict(d
);
2137 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", pamh
, ctrl
, ret
);
2143 * Account management. We want to verify that the account exists
2144 * before returning PAM_SUCCESS
2147 int pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags
,
2148 int argc
, const char **argv
)
2150 const char *username
;
2151 int ret
= PAM_USER_UNKNOWN
;
2153 dictionary
*d
= NULL
;
2155 /* parse arguments */
2156 int ctrl
= _pam_parse(pamh
, flags
, argc
, argv
, &d
);
2158 return PAM_SYSTEM_ERR
;
2161 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", pamh
, ctrl
, flags
);
2164 /* Get the username */
2165 ret
= pam_get_user(pamh
, &username
, NULL
);
2166 if ((ret
!= PAM_SUCCESS
) || (!username
)) {
2167 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
2168 "can not get the username");
2169 ret
= PAM_SERVICE_ERR
;
2173 /* Verify the username */
2174 ret
= valid_user(pamh
, ctrl
, username
);
2177 /* some sort of system error. The log was already printed */
2178 ret
= PAM_SERVICE_ERR
;
2181 /* the user does not exist */
2182 _pam_log_debug(pamh
, ctrl
, LOG_NOTICE
, "user '%s' not found",
2184 if (ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
2188 ret
= PAM_USER_UNKNOWN
;
2191 pam_get_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
,
2192 (const void **)&tmp
);
2194 ret
= atoi((const char *)tmp
);
2196 case PAM_AUTHTOK_EXPIRED
:
2197 /* fall through, since new token is required in this case */
2198 case PAM_NEW_AUTHTOK_REQD
:
2199 _pam_log(pamh
, ctrl
, LOG_WARNING
,
2200 "pam_sm_acct_mgmt success but %s is set",
2201 PAM_WINBIND_NEW_AUTHTOK_REQD
);
2202 _pam_log(pamh
, ctrl
, LOG_NOTICE
,
2203 "user '%s' needs new password",
2205 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
2206 ret
= PAM_NEW_AUTHTOK_REQD
;
2209 _pam_log(pamh
, ctrl
, LOG_WARNING
,
2210 "pam_sm_acct_mgmt success");
2211 _pam_log(pamh
, ctrl
, LOG_NOTICE
,
2212 "user '%s' granted access", username
);
2218 /* Otherwise, the authentication looked good */
2219 _pam_log(pamh
, ctrl
, LOG_NOTICE
,
2220 "user '%s' granted access", username
);
2224 /* we don't know anything about this return value */
2225 _pam_log(pamh
, ctrl
, LOG_ERR
,
2226 "internal module error (ret = %d, user = '%s')",
2228 ret
= PAM_SERVICE_ERR
;
2232 /* should not be reached */
2238 iniparser_freedict(d
);
2241 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", pamh
, ctrl
, ret
);
2247 int pam_sm_open_session(pam_handle_t
*pamh
, int flags
,
2248 int argc
, const char **argv
)
2250 int ret
= PAM_SYSTEM_ERR
;
2251 dictionary
*d
= NULL
;
2253 /* parse arguments */
2254 int ctrl
= _pam_parse(pamh
, flags
, argc
, argv
, &d
);
2256 ret
= PAM_SYSTEM_ERR
;
2260 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", pamh
, ctrl
, flags
);
2266 iniparser_freedict(d
);
2269 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", pamh
, ctrl
, ret
);
2275 int pam_sm_close_session(pam_handle_t
*pamh
, int flags
,
2276 int argc
, const char **argv
)
2278 dictionary
*d
= NULL
;
2279 int retval
= PAM_SUCCESS
;
2281 /* parse arguments */
2282 int ctrl
= _pam_parse(pamh
, flags
, argc
, argv
, &d
);
2284 retval
= PAM_SYSTEM_ERR
;
2288 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", pamh
, ctrl
, flags
);
2290 if (!(flags
& PAM_DELETE_CRED
)) {
2291 retval
= PAM_SUCCESS
;
2295 if (ctrl
& WINBIND_KRB5_AUTH
) {
2297 /* destroy the ccache here */
2298 struct winbindd_request request
;
2299 struct winbindd_response response
;
2301 const char *ccname
= NULL
;
2302 struct passwd
*pwd
= NULL
;
2304 ZERO_STRUCT(request
);
2305 ZERO_STRUCT(response
);
2307 retval
= pam_get_user(pamh
, &user
, "Username: ");
2309 _pam_log(pamh
, ctrl
, LOG_ERR
,
2310 "could not identify user");
2315 _pam_log(pamh
, ctrl
, LOG_ERR
,
2316 "username was NULL!");
2317 retval
= PAM_USER_UNKNOWN
;
2321 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
2322 "username [%s] obtained", user
);
2324 ccname
= pam_getenv(pamh
, "KRB5CCNAME");
2325 if (ccname
== NULL
) {
2326 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
,
2327 "user has no KRB5CCNAME environment");
2330 strncpy(request
.data
.logoff
.user
, user
,
2331 sizeof(request
.data
.logoff
.user
) - 1);
2334 strncpy(request
.data
.logoff
.krb5ccname
, ccname
,
2335 sizeof(request
.data
.logoff
.krb5ccname
) - 1);
2338 pwd
= getpwnam(user
);
2340 retval
= PAM_USER_UNKNOWN
;
2343 request
.data
.logoff
.uid
= pwd
->pw_uid
;
2345 request
.flags
= WBFLAG_PAM_KRB5
|
2346 WBFLAG_PAM_CONTACT_TRUSTDOM
;
2348 retval
= pam_winbind_request_log(pamh
, ctrl
,
2349 WINBINDD_PAM_LOGOFF
,
2350 &request
, &response
, user
);
2355 iniparser_freedict(d
);
2358 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", pamh
, ctrl
, retval
);
2364 * evaluate whether we need to re-authenticate with kerberos after a
2367 * @param pamh PAM handle
2368 * @param ctrl PAM winbind options.
2369 * @param user The username
2371 * @return boolean Returns true if required, false if not.
2374 static bool _pam_require_krb5_auth_after_chauthtok(pam_handle_t
*pamh
,
2379 /* Make sure that we only do this if a) the chauthtok got initiated
2380 * during a logon attempt (authenticate->acct_mgmt->chauthtok) b) any
2381 * later password change via the "passwd" command if done by the user
2384 char *new_authtok_reqd_during_auth
= NULL
;
2385 struct passwd
*pwd
= NULL
;
2387 if (!(ctrl
& WINBIND_KRB5_AUTH
)) {
2391 _pam_get_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2392 &new_authtok_reqd_during_auth
);
2393 pam_set_data(pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH
,
2396 if (new_authtok_reqd_during_auth
) {
2400 pwd
= getpwnam(user
);
2405 if (getuid() == pwd
->pw_uid
) {
2414 int pam_sm_chauthtok(pam_handle_t
* pamh
, int flags
,
2415 int argc
, const char **argv
)
2421 /* <DO NOT free() THESE> */
2423 char *pass_old
, *pass_new
;
2424 /* </DO NOT free() THESE> */
2429 dictionary
*d
= NULL
;
2430 char *username_ret
= NULL
;
2431 struct winbindd_response response
;
2433 ZERO_STRUCT(response
);
2435 ctrl
= _pam_parse(pamh
, flags
, argc
, argv
, &d
);
2437 ret
= PAM_SYSTEM_ERR
;
2441 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", pamh
, ctrl
, flags
);
2443 /* clearing offline bit for the auth in the password change */
2444 ctrl
&= ~WINBIND_CACHED_LOGIN
;
2447 * First get the name of a user
2449 ret
= pam_get_user(pamh
, &user
, "Username: ");
2451 _pam_log(pamh
, ctrl
, LOG_ERR
,
2452 "password - could not identify user");
2457 _pam_log(pamh
, ctrl
, LOG_ERR
, "username was NULL!");
2458 ret
= PAM_USER_UNKNOWN
;
2462 _pam_log_debug(pamh
, ctrl
, LOG_DEBUG
, "username [%s] obtained", user
);
2464 /* check if this is really a user in winbindd, not only in NSS */
2465 ret
= valid_user(pamh
, ctrl
, user
);
2468 ret
= PAM_USER_UNKNOWN
;
2471 ret
= PAM_SYSTEM_ERR
;
2478 * obtain and verify the current password (OLDAUTHTOK) for
2482 if (flags
& PAM_PRELIM_CHECK
) {
2483 time_t pwdlastset_prelim
= 0;
2485 /* instruct user what is happening */
2486 #define greeting "Changing password for "
2487 Announce
= (char *) malloc(sizeof(greeting
) + strlen(user
));
2488 if (Announce
== NULL
) {
2489 _pam_log(pamh
, ctrl
, LOG_CRIT
,
2490 "password - out of memory");
2494 (void) strcpy(Announce
, greeting
);
2495 (void) strcpy(Announce
+ sizeof(greeting
) - 1, user
);
2498 lctrl
= ctrl
| WINBIND__OLD_PASSWORD
;
2499 ret
= _winbind_read_password(pamh
, lctrl
,
2501 "(current) NT password: ",
2503 (const char **) &pass_old
);
2504 if (ret
!= PAM_SUCCESS
) {
2505 _pam_log(pamh
, ctrl
, LOG_NOTICE
,
2506 "password - (old) token not obtained");
2510 /* verify that this is the password for this user */
2512 ret
= winbind_auth_request(pamh
, ctrl
, user
, pass_old
,
2513 NULL
, NULL
, 0, &response
,
2514 &pwdlastset_prelim
, NULL
);
2516 if (ret
!= PAM_ACCT_EXPIRED
&&
2517 ret
!= PAM_AUTHTOK_EXPIRED
&&
2518 ret
!= PAM_NEW_AUTHTOK_REQD
&&
2519 ret
!= PAM_SUCCESS
) {
2524 pam_set_data(pamh
, PAM_WINBIND_PWD_LAST_SET
,
2525 (void *)pwdlastset_prelim
, NULL
);
2527 ret
= pam_set_item(pamh
, PAM_OLDAUTHTOK
,
2528 (const void *) pass_old
);
2530 if (ret
!= PAM_SUCCESS
) {
2531 _pam_log(pamh
, ctrl
, LOG_CRIT
,
2532 "failed to set PAM_OLDAUTHTOK");
2534 } else if (flags
& PAM_UPDATE_AUTHTOK
) {
2536 time_t pwdlastset_update
= 0;
2539 * obtain the proposed password
2543 * get the old token back.
2546 ret
= _pam_get_item(pamh
, PAM_OLDAUTHTOK
, &pass_old
);
2548 if (ret
!= PAM_SUCCESS
) {
2549 _pam_log(pamh
, ctrl
, LOG_NOTICE
,
2550 "user not authenticated");
2554 lctrl
= ctrl
& ~WINBIND_TRY_FIRST_PASS_ARG
;
2556 if (on(WINBIND_USE_AUTHTOK_ARG
, lctrl
)) {
2557 lctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
2560 ret
= PAM_AUTHTOK_ERR
;
2561 while ((ret
!= PAM_SUCCESS
) && (retry
++ < MAX_PASSWD_TRIES
)) {
2563 * use_authtok is to force the use of a previously entered
2564 * password -- needed for pluggable password strength checking
2567 ret
= _winbind_read_password(pamh
, lctrl
,
2569 "Enter new NT password: ",
2570 "Retype new NT password: ",
2571 (const char **)&pass_new
);
2573 if (ret
!= PAM_SUCCESS
) {
2574 _pam_log_debug(pamh
, ctrl
, LOG_ALERT
,
2576 "new password not obtained");
2577 pass_old
= NULL
;/* tidy up */
2582 * At this point we know who the user is and what they
2583 * propose as their new password. Verify that the new
2584 * password is acceptable.
2587 if (pass_new
[0] == '\0') {/* "\0" password = NULL */
2593 * By reaching here we have approved the passwords and must now
2594 * rebuild the password database file.
2596 _pam_get_data(pamh
, PAM_WINBIND_PWD_LAST_SET
,
2597 &pwdlastset_update
);
2599 ret
= winbind_chauthtok_request(pamh
, ctrl
, user
, pass_old
,
2600 pass_new
, pwdlastset_update
);
2602 _pam_overwrite(pass_new
);
2603 _pam_overwrite(pass_old
);
2604 pass_old
= pass_new
= NULL
;
2608 if (_pam_require_krb5_auth_after_chauthtok(pamh
, ctrl
, user
)) {
2610 const char *member
= NULL
;
2611 const char *cctype
= NULL
;
2612 int warn_pwd_expire
;
2614 member
= get_member_from_config(pamh
, argc
, argv
,
2616 cctype
= get_krb5_cc_type_from_config(pamh
, argc
, argv
,
2618 warn_pwd_expire
= get_warn_pwd_expire_from_config(pamh
,
2625 ret
= winbind_auth_request(pamh
, ctrl
, user
, pass_new
,
2626 member
, cctype
, 0, &response
,
2627 NULL
, &username_ret
);
2628 _pam_overwrite(pass_new
);
2629 _pam_overwrite(pass_old
);
2630 pass_old
= pass_new
= NULL
;
2632 if (ret
== PAM_SUCCESS
) {
2634 /* warn a user if the password is about to
2636 _pam_warn_password_expiry(pamh
, ctrl
, &response
,
2640 /* set some info3 info for other modules in the
2642 _pam_set_data_info3(pamh
, ctrl
, &response
);
2644 /* put krb5ccname into env */
2645 _pam_setup_krb5_env(pamh
, ctrl
,
2646 response
.data
.auth
.krb5ccname
);
2649 pam_set_item(pamh
, PAM_USER
,
2651 _pam_log_debug(pamh
, ctrl
, LOG_INFO
,
2652 "Returned user was '%s'",
2661 ret
= PAM_SERVICE_ERR
;
2666 iniparser_freedict(d
);
2669 /* Deal with offline errors. */
2670 PAM_WB_REMARK_CHECK_RESPONSE(pamh
, ctrl
, response
,
2671 "NT_STATUS_NO_LOGON_SERVERS");
2672 PAM_WB_REMARK_CHECK_RESPONSE(pamh
, ctrl
, response
,
2673 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
2674 PAM_WB_REMARK_CHECK_RESPONSE(pamh
, ctrl
, response
,
2675 "NT_STATUS_ACCESS_DENIED");
2677 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", pamh
, ctrl
, ret
);
2684 /* static module data */
2686 struct pam_module _pam_winbind_modstruct
= {
2688 pam_sm_authenticate
,
2691 pam_sm_open_session
,
2692 pam_sm_close_session
,
2699 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
2700 * Copyright (c) Tim Potter <tpot@samba.org> 2000
2701 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
2702 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2007
2703 * Copyright (c) Jan Rêkorajski 1999.
2704 * Copyright (c) Andrew G. Morgan 1996-8.
2705 * Copyright (c) Alex O. Yuriev, 1996.
2706 * Copyright (c) Cristian Gafton 1996.
2707 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
2709 * Redistribution and use in source and binary forms, with or without
2710 * modification, are permitted provided that the following conditions
2712 * 1. Redistributions of source code must retain the above copyright
2713 * notice, and the entire permission notice in its entirety,
2714 * including the disclaimer of warranties.
2715 * 2. Redistributions in binary form must reproduce the above copyright
2716 * notice, this list of conditions and the following disclaimer in the
2717 * documentation and/or other materials provided with the distribution.
2718 * 3. The name of the author may not be used to endorse or promote
2719 * products derived from this software without specific prior
2720 * written permission.
2722 * ALTERNATIVELY, this product may be distributed under the terms of
2723 * the GNU Public License, in which case the provisions of the GPL are
2724 * required INSTEAD OF the above restrictions. (This clause is
2725 * necessary due to a potential bad interaction between the GPL and
2726 * the restrictions contained in a BSD-style copyright.)
2728 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
2729 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2730 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2731 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2732 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2733 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2734 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2735 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2736 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2737 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2738 * OF THE POSSIBILITY OF SUCH DAMAGE.