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>
9 also contains large slabs of code from pam_unix by Elliot Lee <sopwith@redhat.com>
10 (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: 0x%08x] ENTER: " function " (flags: 0x%04x)", (uint32) pamh, flags); \
18 _pam_log_state(pamh, ctrl); \
21 #define _PAM_LOG_FUNCTION_LEAVE(function, pamh, ctrl, retval) \
23 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] LEAVE: " function " returning %d", (uint32) pamh, retval); \
24 _pam_log_state(pamh, ctrl); \
29 #define MAX_PASSWD_TRIES 3
32 * Work around the pam API that has functions with void ** as parameters.
33 * These lead to strict aliasing warnings with gcc.
35 static int _pam_get_item(const pam_handle_t *pamh, int item_type,
38 const void **item = (const void **)_item;
39 return pam_get_item(pamh, item_type, item);
41 static int _pam_get_data(const pam_handle_t *pamh,
42 const char *module_data_name, const void *_data)
44 const void **data = (const void **)_data;
45 return pam_get_data(pamh, module_data_name, data);
50 #ifdef HAVE_PAM_VSYSLOG
51 static void _pam_log_int(const pam_handle_t *pamh, int err, const char *format, va_list args)
53 pam_vsyslog(pamh, err, format, args);
56 static void _pam_log_int(const pam_handle_t *pamh, int err, const char *format, va_list args)
61 _pam_get_item(pamh, PAM_SERVICE, &service);
63 format2 = malloc(strlen(MODULE_NAME)+strlen(format)+strlen(service)+5);
64 if (format2 == NULL) {
65 /* what else todo ? */
66 vsyslog(err, format, args);
70 sprintf(format2, "%s(%s): %s", MODULE_NAME, service, format);
71 vsyslog(err, format2, args);
74 #endif /* HAVE_PAM_VSYSLOG */
76 static int _pam_log_is_silent(int ctrl)
78 return (ctrl & WINBIND_SILENT) ? 1 : 0;
81 static void _pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
85 if (_pam_log_is_silent(ctrl)) {
89 va_start(args, format);
90 _pam_log_int(pamh, err, format, args);
94 static int _pam_log_is_debug_enabled(int ctrl)
100 if (_pam_log_is_silent(ctrl)) {
104 if (!(ctrl & WINBIND_DEBUG_ARG)) {
111 static int _pam_log_is_debug_state_enabled(int ctrl)
113 if (!(ctrl & WINBIND_DEBUG_STATE)) {
117 return _pam_log_is_debug_enabled(ctrl);
120 static void _pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
124 if (!_pam_log_is_debug_enabled(ctrl)) {
128 va_start(args, format);
129 _pam_log_int(pamh, err, format, args);
133 static void _pam_log_state_datum(const pam_handle_t *pamh, int ctrl, int item_type, const char *key, int is_string)
135 const void *data = NULL;
136 if (item_type != 0) {
137 pam_get_item(pamh, item_type, &data);
139 pam_get_data(pamh, key, &data);
142 const char *type = (item_type != 0) ? "ITEM" : "DATA";
143 if (is_string != 0) {
144 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] STATE: %s(%s) = \"%s\" (0x%08x)", (uint32) pamh, type, key, (const char *) data, (uint32) data);
146 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] STATE: %s(%s) = 0x%08x", (uint32) pamh, type, key, (uint32) data);
151 #define _PAM_LOG_STATE_DATA_POINTER(pamh, ctrl, module_data_name) \
152 _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 0)
154 #define _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, module_data_name) \
155 _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 1)
157 #define _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, item_type) \
158 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 0)
160 #define _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, item_type) \
161 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 1)
163 #ifdef DEBUG_PASSWORD
164 #define _LOG_PASSWORD_AS_STRING 1
166 #define _LOG_PASSWORD_AS_STRING 0
169 #define _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, item_type) \
170 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, _LOG_PASSWORD_AS_STRING)
172 static void _pam_log_state(const pam_handle_t *pamh, int ctrl)
174 if (!_pam_log_is_debug_state_enabled(ctrl)) {
178 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_SERVICE);
179 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_USER);
180 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_TTY);
181 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_RHOST);
182 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_RUSER);
183 _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, PAM_OLDAUTHTOK);
184 _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, PAM_AUTHTOK);
185 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_USER_PROMPT);
186 _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_CONV);
187 #ifdef PAM_FAIL_DELAY
188 _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_FAIL_DELAY);
190 #ifdef PAM_REPOSITORY
191 _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_REPOSITORY);
194 _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_HOMEDIR);
195 _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_LOGONSCRIPT);
196 _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_PROFILEPATH);
197 _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_NEW_AUTHTOK_REQD); /* Use atoi to get PAM result code */
198 _PAM_LOG_STATE_DATA_POINTER(pamh, ctrl, PAM_WINBIND_PWD_LAST_SET);
201 static int _pam_parse(const pam_handle_t *pamh, int flags, int argc, const char **argv, dictionary **result_d)
204 const char *config_file = NULL;
207 dictionary *d = NULL;
209 if (flags & PAM_SILENT) {
210 ctrl |= WINBIND_SILENT;
213 for (i=argc,v=argv; i-- > 0; ++v) {
214 if (!strncasecmp(*v, "config", strlen("config"))) {
215 ctrl |= WINBIND_CONFIG_FILE;
221 if (config_file == NULL) {
222 config_file = PAM_WINBIND_CONFIG_FILE;
225 d = iniparser_load(config_file);
227 goto config_from_pam;
230 if (iniparser_getboolean(d, "global:debug", False)) {
231 ctrl |= WINBIND_DEBUG_ARG;
234 if (iniparser_getboolean(d, "global:debug_state", False)) {
235 ctrl |= WINBIND_DEBUG_STATE;
238 if (iniparser_getboolean(d, "global:cached_login", False)) {
239 ctrl |= WINBIND_CACHED_LOGIN;
242 if (iniparser_getboolean(d, "global:krb5_auth", False)) {
243 ctrl |= WINBIND_KRB5_AUTH;
246 if (iniparser_getboolean(d, "global:silent", False)) {
247 ctrl |= WINBIND_SILENT;
250 if (iniparser_getstr(d, "global:krb5_ccache_type") != NULL) {
251 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
254 if ((iniparser_getstr(d, "global:require-membership-of") != NULL) ||
255 (iniparser_getstr(d, "global:require_membership_of") != NULL)) {
256 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
259 if (iniparser_getboolean(d, "global:try_first_pass", False)) {
260 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
264 /* step through arguments */
265 for (i=argc,v=argv; i-- > 0; ++v) {
267 /* generic options */
268 if (!strcmp(*v,"debug"))
269 ctrl |= WINBIND_DEBUG_ARG;
270 else if (!strcasecmp(*v, "use_authtok"))
271 ctrl |= WINBIND_USE_AUTHTOK_ARG;
272 else if (!strcasecmp(*v, "use_first_pass"))
273 ctrl |= WINBIND_USE_FIRST_PASS_ARG;
274 else if (!strcasecmp(*v, "try_first_pass"))
275 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
276 else if (!strcasecmp(*v, "unknown_ok"))
277 ctrl |= WINBIND_UNKNOWN_OK_ARG;
278 else if (!strncasecmp(*v, "require_membership_of", strlen("require_membership_of")))
279 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
280 else if (!strncasecmp(*v, "require-membership-of", strlen("require-membership-of")))
281 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
282 else if (!strcasecmp(*v, "krb5_auth"))
283 ctrl |= WINBIND_KRB5_AUTH;
284 else if (!strncasecmp(*v, "krb5_ccache_type", strlen("krb5_ccache_type")))
285 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
286 else if (!strcasecmp(*v, "cached_login"))
287 ctrl |= WINBIND_CACHED_LOGIN;
289 _pam_log(pamh, ctrl, LOG_ERR, "pam_parse: unknown option: %s", *v);
299 iniparser_freedict(d);
306 static void _pam_winbind_cleanup_func(pam_handle_t *pamh, void *data, int error_status)
308 int ctrl = _pam_parse(pamh, 0, 0, NULL, NULL);
309 if (_pam_log_is_debug_state_enabled(ctrl)) {
310 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] CLEAN: cleaning up PAM data 0x%08x (error_status = %d)", (uint32) pamh, (uint32) data, error_status);
316 static const struct ntstatus_errors {
317 const char *ntstatus_string;
318 const char *error_string;
319 } ntstatus_errors[] = {
320 {"NT_STATUS_OK", "Success"},
321 {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
322 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND", "No domain controllers found"},
323 {"NT_STATUS_NO_LOGON_SERVERS", "No logon servers"},
324 {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
325 {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
326 {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
327 {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
328 {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
329 {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
330 {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
331 {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
332 {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
333 {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
334 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
335 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
336 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
337 {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
341 const char *_get_ntstatus_error_string(const char *nt_status_string)
344 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
345 if (!strcasecmp(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
346 return ntstatus_errors[i].error_string;
352 /* --- authentication management functions --- */
354 /* Attempt a conversation */
356 static int converse(pam_handle_t *pamh, int nargs,
357 struct pam_message **message,
358 struct pam_response **response)
361 struct pam_conv *conv;
363 retval = _pam_get_item(pamh, PAM_CONV, &conv );
364 if (retval == PAM_SUCCESS) {
365 retval = conv->conv(nargs, (const struct pam_message **)message,
366 response, conv->appdata_ptr);
369 return retval; /* propagate error status */
373 static int _make_remark(pam_handle_t * pamh, int type, const char *text)
375 int retval = PAM_SUCCESS;
377 struct pam_message *pmsg[1], msg[1];
378 struct pam_response *resp;
381 msg[0].msg = CONST_DISCARD(char *, text);
382 msg[0].msg_style = type;
385 retval = converse(pamh, 1, pmsg, &resp);
388 _pam_drop_reply(resp, 1);
393 static int _make_remark_v(pam_handle_t * pamh, int type, const char *format, va_list args)
398 ret = vasprintf(&var, format, args);
400 _pam_log(pamh, 0, LOG_ERR, "memory allocation failure");
404 ret = _make_remark(pamh, type, var);
409 static int _make_remark_format(pam_handle_t * pamh, int type, const char *format, ...)
414 va_start(args, format);
415 ret = _make_remark_v(pamh, type, format, args);
420 static int pam_winbind_request(pam_handle_t * pamh, int ctrl,
421 enum winbindd_cmd req_type,
422 struct winbindd_request *request,
423 struct winbindd_response *response)
425 /* Fill in request and send down pipe */
426 init_request(request, req_type);
428 if (write_sock(request, sizeof(*request), 0) == -1) {
429 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: write to socket failed!");
431 return PAM_SERVICE_ERR;
435 if (read_reply(response) == -1) {
436 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: read from socket failed!");
438 return PAM_SERVICE_ERR;
441 /* We are done with the socket - close it and avoid mischeif */
444 /* Copy reply data from socket */
445 if (response->result != WINBINDD_OK) {
446 if (response->data.auth.pam_error != PAM_SUCCESS) {
447 _pam_log(pamh, ctrl, LOG_ERR, "request failed: %s, PAM error was %s (%d), NT error was %s",
448 response->data.auth.error_string,
449 pam_strerror(pamh, response->data.auth.pam_error),
450 response->data.auth.pam_error,
451 response->data.auth.nt_status_string);
452 return response->data.auth.pam_error;
454 _pam_log(pamh, ctrl, LOG_ERR, "request failed, but PAM error 0!");
455 return PAM_SERVICE_ERR;
462 static int pam_winbind_request_log(pam_handle_t * pamh,
464 enum winbindd_cmd req_type,
465 struct winbindd_request *request,
466 struct winbindd_response *response,
471 retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
475 /* incorrect password */
476 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' denied access (incorrect password or invalid membership)", user);
478 case PAM_ACCT_EXPIRED:
479 /* account expired */
480 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' account expired", user);
482 case PAM_AUTHTOK_EXPIRED:
483 /* password expired */
484 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' password expired", user);
486 case PAM_NEW_AUTHTOK_REQD:
487 /* new password required */
488 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' new password required", user);
490 case PAM_USER_UNKNOWN:
491 /* the user does not exist */
492 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", user);
493 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
498 if (req_type == WINBINDD_PAM_AUTH) {
499 /* Otherwise, the authentication looked good */
500 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", user);
501 } else if (req_type == WINBINDD_PAM_CHAUTHTOK) {
502 /* Otherwise, the authentication looked good */
503 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' password changed", user);
505 /* Otherwise, the authentication looked good */
506 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' OK", user);
511 /* we don't know anything about this return value */
512 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (retval = %d, user = '%s')",
518 static BOOL _pam_send_password_expiry_message(pam_handle_t *pamh, time_t next_change, time_t now)
521 struct tm tm_now, tm_next_change;
523 if ((next_change < 0) ||
524 (next_change < now) ||
525 (next_change > now + DAYS_TO_WARN_BEFORE_PWD_EXPIRES * SECONDS_PER_DAY)) {
529 if ((localtime_r(&now, &tm_now) == NULL) ||
530 (localtime_r(&next_change, &tm_next_change) == NULL)) {
534 days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) - (tm_now.tm_yday+tm_now.tm_year*365);
537 _make_remark(pamh, PAM_TEXT_INFO, "Your password expires today");
541 if (days > 0 && days < DAYS_TO_WARN_BEFORE_PWD_EXPIRES) {
542 _make_remark_format(pamh, PAM_TEXT_INFO, "Your password will expire in %d %s",
543 days, (days > 1) ? "days":"day");
550 static void _pam_warn_password_expires_in_future(pam_handle_t *pamh, struct winbindd_response *response)
552 time_t now = time(NULL);
553 time_t next_change = 0;
555 /* accounts with ACB_PWNOEXP set never receive a warning */
556 if (response->data.auth.info3.acct_flags & ACB_PWNOEXP) {
560 /* no point in sending a warning if this is a grace logon */
561 if (PAM_WB_GRACE_LOGON(response->data.auth.info3.user_flgs)) {
565 /* check if the info3 must change timestamp has been set */
566 next_change = response->data.auth.info3.pass_must_change_time;
568 if (_pam_send_password_expiry_message(pamh, next_change, now)) {
572 /* now check for the global password policy */
573 if (response->data.auth.policy.expire <= 0) {
577 next_change = response->data.auth.info3.pass_last_set_time +
578 response->data.auth.policy.expire;
580 if (_pam_send_password_expiry_message(pamh, next_change, now)) {
584 /* no warning sent */
587 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
589 int safe_append_string(char *dest,
591 int dest_buffer_size)
593 * Append a string, making sure not to overflow and to always return a NULL-terminated
596 * @param dest Destination string buffer (must already be NULL-terminated).
597 * @param src Source string buffer.
598 * @param dest_buffer_size Size of dest buffer in bytes.
600 * @return 0 if dest buffer is not big enough (no bytes copied), non-zero on success.
603 int dest_length = strlen(dest);
604 int src_length = strlen(src);
606 if ( dest_length + src_length + 1 > dest_buffer_size ) {
610 memcpy(dest + dest_length, src, src_length + 1);
614 static int winbind_name_to_sid_string(pam_handle_t *pamh,
618 char *sid_list_buffer,
619 int sid_list_buffer_size)
621 * Convert a names into a SID string, appending it to a buffer.
623 * @param pamh PAM handle
624 * @param ctrl PAM winbind options.
625 * @param user User in PAM request.
626 * @param name Name to convert.
627 * @param sid_list_buffer Where to append the string sid.
628 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
630 * @return 0 on failure, non-zero on success.
633 const char* sid_string;
634 struct winbindd_response sid_response;
637 if (IS_SID_STRING(name)) {
640 struct winbindd_request sid_request;
642 ZERO_STRUCT(sid_request);
643 ZERO_STRUCT(sid_response);
645 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", name);
647 /* fortunatly winbindd can handle non-separated names */
648 strncpy(sid_request.data.name.name, name,
649 sizeof(sid_request.data.name.name) - 1);
651 if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME, &sid_request, &sid_response, user)) {
652 _pam_log(pamh, ctrl, LOG_INFO, "could not lookup name: %s\n", name);
656 sid_string = sid_response.data.sid.sid;
659 if (!safe_append_string(sid_list_buffer, sid_string, sid_list_buffer_size)) {
666 static int winbind_name_list_to_sid_string_list(pam_handle_t *pamh,
669 const char *name_list,
670 char *sid_list_buffer,
671 int sid_list_buffer_size)
673 * Convert a list of names into a list of sids.
675 * @param pamh PAM handle
676 * @param ctrl PAM winbind options.
677 * @param user User in PAM request.
678 * @param name_list List of names or string sids, separated by commas.
679 * @param sid_list_buffer Where to put the list of string sids.
680 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
682 * @return 0 on failure, non-zero on success.
686 char *current_name = NULL;
687 const char *search_location;
690 if ( sid_list_buffer_size > 0 ) {
691 sid_list_buffer[0] = 0;
694 search_location = name_list;
695 while ( (comma = strstr(search_location, ",")) != NULL ) {
696 current_name = strndup(search_location, comma - search_location);
697 if (NULL == current_name) {
701 if (!winbind_name_to_sid_string(pamh, ctrl, user, current_name, sid_list_buffer, sid_list_buffer_size)) {
708 if (!safe_append_string(sid_list_buffer, ",", sid_list_buffer_size)) {
712 search_location = comma + 1;
715 if (!winbind_name_to_sid_string(pamh, ctrl, user, search_location, sid_list_buffer, sid_list_buffer_size)) {
722 if (current_name != NULL) {
728 /* talk to winbindd */
729 static int winbind_auth_request(pam_handle_t * pamh,
735 struct winbindd_response *p_response,
736 time_t *pwd_last_set,
739 struct winbindd_request request;
740 struct winbindd_response response;
743 ZERO_STRUCT(request);
744 ZERO_STRUCT(response);
750 strncpy(request.data.auth.user, user,
751 sizeof(request.data.auth.user)-1);
753 strncpy(request.data.auth.pass, pass,
754 sizeof(request.data.auth.pass)-1);
756 request.data.auth.krb5_cc_type[0] = '\0';
757 request.data.auth.uid = -1;
759 request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_CONTACT_TRUSTDOM;
761 if (ctrl & WINBIND_KRB5_AUTH) {
763 struct passwd *pwd = NULL;
765 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling krb5 login flag\n");
767 request.flags |= WBFLAG_PAM_KRB5 | WBFLAG_PAM_FALLBACK_AFTER_KRB5;
769 pwd = getpwnam(user);
771 return PAM_USER_UNKNOWN;
773 request.data.auth.uid = pwd->pw_uid;
776 if (ctrl & WINBIND_CACHED_LOGIN) {
777 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling cached login flag\n");
778 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
783 request.flags |= WBFLAG_PAM_UNIX_NAME;
786 if (cctype != NULL) {
787 strncpy(request.data.auth.krb5_cc_type, cctype,
788 sizeof(request.data.auth.krb5_cc_type) - 1);
789 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling request for a %s krb5 ccache\n", cctype);
792 request.data.auth.require_membership_of_sid[0] = '\0';
794 if (member != NULL) {
796 if (!winbind_name_list_to_sid_string_list(pamh, ctrl, user, member,
797 request.data.auth.require_membership_of_sid,
798 sizeof(request.data.auth.require_membership_of_sid))) {
800 _pam_log_debug(pamh, ctrl, LOG_ERR, "failed to serialize membership of sid \"%s\"\n", member);
805 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
808 *pwd_last_set = response.data.auth.info3.pass_last_set_time;
811 if ((ctrl & WINBIND_KRB5_AUTH) &&
812 response.data.auth.krb5ccname[0] != '\0') {
816 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "request returned KRB5CCNAME: %s",
817 response.data.auth.krb5ccname);
819 snprintf(var, sizeof(var), "KRB5CCNAME=%s", response.data.auth.krb5ccname);
821 ret = pam_putenv(pamh, var);
822 if (ret != PAM_SUCCESS) {
823 _pam_log(pamh, ctrl, LOG_ERR, "failed to set KRB5CCNAME to %s", var);
829 /* We want to process the response in the caller. */
830 *p_response = response;
835 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_EXPIRED");
836 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_MUST_CHANGE");
837 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_WORKSTATION");
838 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_LOGON_HOURS");
839 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_EXPIRED");
840 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_DISABLED");
841 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_LOCKED_OUT");
842 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
843 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
844 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
845 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
846 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NO_LOGON_SERVERS");
849 /* handle the case where the auth was ok, but the password must expire right now */
850 /* good catch from Ralf Haferkamp: an expiry of "never" is translated to -1 */
851 if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
852 ! (PAM_WB_GRACE_LOGON(response.data.auth.info3.user_flgs)) &&
853 (response.data.auth.policy.expire > 0) &&
854 (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire < time(NULL))) {
856 ret = PAM_AUTHTOK_EXPIRED;
858 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"Password has expired (Password was last set: %d, "
859 "the policy says it should expire here %d (now it's: %d)\n",
860 response.data.auth.info3.pass_last_set_time,
861 response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire,
864 PAM_WB_REMARK_DIRECT_RET(pamh, "NT_STATUS_PASSWORD_EXPIRED");
868 /* warn a user if the password is about to expire soon */
869 _pam_warn_password_expires_in_future(pamh, &response);
871 /* inform about logon type */
872 if (PAM_WB_GRACE_LOGON(response.data.auth.info3.user_flgs)) {
874 _make_remark(pamh, PAM_ERROR_MSG,
875 "Grace login. Please change your password as soon you're online again");
876 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
877 "User %s logged on using grace logon\n", user);
879 } else if (PAM_WB_CACHED_LOGON(response.data.auth.info3.user_flgs)) {
881 _make_remark(pamh, PAM_ERROR_MSG,
882 "Logging on using cached account. Network ressources can be unavailable");
883 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
884 "User %s logged on using cached account\n", user);
887 /* save the CIFS homedir for pam_cifs / pam_mount */
888 if (response.data.auth.info3.home_dir[0] != '\0') {
890 int ret2 = pam_set_data(pamh, PAM_WINBIND_HOMEDIR,
891 (void *) strdup(response.data.auth.info3.home_dir),
892 _pam_winbind_cleanup_func);
894 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s",
895 pam_strerror(pamh, ret2));
900 /* save the logon script path for other PAM modules */
901 if (response.data.auth.info3.logon_script[0] != '\0') {
903 int ret2 = pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT,
904 (void *) strdup(response.data.auth.info3.logon_script),
905 _pam_winbind_cleanup_func);
907 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s",
908 pam_strerror(pamh, ret2));
912 /* save the profile path for other PAM modules */
913 if (response.data.auth.info3.profile_path[0] != '\0') {
915 int ret2 = pam_set_data(pamh, PAM_WINBIND_PROFILEPATH,
916 (void *) strdup(response.data.auth.info3.profile_path),
917 _pam_winbind_cleanup_func);
919 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s",
920 pam_strerror(pamh, ret2));
924 /* If winbindd returned a username, return the pointer to it here. */
925 if (user_ret && response.extra_data.data) {
926 /* We have to trust it's a null terminated string. */
927 *user_ret = (char *)response.extra_data.data;
933 /* talk to winbindd */
934 static int winbind_chauthtok_request(pam_handle_t * pamh,
941 struct winbindd_request request;
942 struct winbindd_response response;
945 ZERO_STRUCT(request);
946 ZERO_STRUCT(response);
948 if (request.data.chauthtok.user == NULL) return -2;
950 strncpy(request.data.chauthtok.user, user,
951 sizeof(request.data.chauthtok.user) - 1);
953 if (oldpass != NULL) {
954 strncpy(request.data.chauthtok.oldpass, oldpass,
955 sizeof(request.data.chauthtok.oldpass) - 1);
957 request.data.chauthtok.oldpass[0] = '\0';
960 if (newpass != NULL) {
961 strncpy(request.data.chauthtok.newpass, newpass,
962 sizeof(request.data.chauthtok.newpass) - 1);
964 request.data.chauthtok.newpass[0] = '\0';
967 if (ctrl & WINBIND_KRB5_AUTH) {
968 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
971 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
973 if (ret == PAM_SUCCESS) {
977 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_BACKUP_CONTROLLER");
978 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
979 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NO_LOGON_SERVERS");
980 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCESS_DENIED");
982 /* TODO: tell the min pwd length ? */
983 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_SHORT");
985 /* TODO: tell the minage ? */
986 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_RECENT");
988 /* TODO: tell the history length ? */
989 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
991 if (!strcasecmp(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
993 /* FIXME: avoid to send multiple PAM messages after another */
994 switch (response.data.auth.reject_reason) {
997 case REJECT_REASON_OTHER:
998 if ((response.data.auth.policy.min_passwordage > 0) &&
999 (pwd_last_set + response.data.auth.policy.min_passwordage > time(NULL))) {
1000 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_RECENT");
1003 case REJECT_REASON_TOO_SHORT:
1004 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_SHORT");
1006 case REJECT_REASON_IN_HISTORY:
1007 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_HISTORY_CONFLICT");
1009 case REJECT_REASON_NOT_COMPLEX:
1010 _make_remark(pamh, PAM_ERROR_MSG, "Password does not meet complexity requirements");
1013 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1014 "unknown password change reject reason: %d",
1015 response.data.auth.reject_reason);
1019 _make_remark_format(pamh, PAM_ERROR_MSG,
1020 "Your password must be at least %d characters; "
1021 "cannot repeat any of the your previous %d passwords"
1023 "Please type a different password. "
1024 "Type a password which meets these requirements in both text boxes.",
1025 response.data.auth.policy.min_length_password,
1026 response.data.auth.policy.password_history,
1027 (response.data.auth.policy.password_properties & DOMAIN_PASSWORD_COMPLEX) ?
1028 "; must contain capitals, numerals or punctuation; and cannot contain your account or full name" :
1037 * Checks if a user has an account
1040 * 1 = User not found
1044 static int valid_user(pam_handle_t *pamh, int ctrl, const char *user)
1046 /* check not only if the user is available over NSS calls, also make
1047 * sure it's really a winbind user, this is important when stacking PAM
1048 * modules in the 'account' or 'password' facility. */
1050 struct passwd *pwd = NULL;
1051 struct winbindd_request request;
1052 struct winbindd_response response;
1055 ZERO_STRUCT(request);
1056 ZERO_STRUCT(response);
1058 pwd = getpwnam(user);
1063 strncpy(request.data.username, user,
1064 sizeof(request.data.username) - 1);
1066 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_GETPWNAM, &request, &response, user);
1069 case PAM_USER_UNKNOWN:
1079 static char *_pam_delete(register char *xx)
1087 * obtain a password from the user
1090 static int _winbind_read_password(pam_handle_t * pamh,
1092 const char *comment,
1093 const char *prompt1,
1094 const char *prompt2,
1102 _pam_log(pamh, ctrl, LOG_DEBUG, "getting password (0x%08x)", ctrl);
1105 * make sure nothing inappropriate gets returned
1108 *pass = token = NULL;
1111 * which authentication token are we getting?
1114 authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
1117 * should we obtain the password from a PAM item ?
1120 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1121 retval = _pam_get_item(pamh, authtok_flag, &item);
1122 if (retval != PAM_SUCCESS) {
1124 _pam_log(pamh, ctrl, LOG_ALERT,
1125 "pam_get_item returned error to unix-read-password"
1128 } else if (item != NULL) { /* we have a password! */
1131 _pam_log(pamh, ctrl, LOG_DEBUG,
1132 "pam_get_item returned a password");
1134 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1135 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
1136 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
1137 && off(WINBIND__OLD_PASSWORD, ctrl)) {
1138 return PAM_AUTHTOK_RECOVER_ERR;
1142 * getting here implies we will have to get the password from the
1147 struct pam_message msg[3], *pmsg[3];
1148 struct pam_response *resp;
1151 /* prepare to converse */
1153 if (comment != NULL) {
1155 msg[0].msg_style = PAM_TEXT_INFO;
1156 msg[0].msg = CONST_DISCARD(char *, comment);
1163 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1164 msg[i++].msg = CONST_DISCARD(char *, prompt1);
1167 if (prompt2 != NULL) {
1169 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1170 msg[i++].msg = CONST_DISCARD(char *, prompt2);
1173 /* so call the conversation expecting i responses */
1175 retval = converse(pamh, i, pmsg, &resp);
1179 /* interpret the response */
1181 if (retval == PAM_SUCCESS) { /* a good conversation */
1183 token = x_strdup(resp[i - replies].resp);
1184 if (token != NULL) {
1186 /* verify that password entered correctly */
1187 if (!resp[i - 1].resp
1188 || strcmp(token, resp[i - 1].resp)) {
1189 _pam_delete(token); /* mistyped */
1190 retval = PAM_AUTHTOK_RECOVER_ERR;
1191 _make_remark(pamh, PAM_ERROR_MSG, MISTYPED_PASS);
1195 _pam_log(pamh, ctrl, LOG_NOTICE, "could not recover authentication token");
1196 retval = PAM_AUTHTOK_RECOVER_ERR;
1201 * tidy up the conversation (resp_retcode) is ignored
1202 * -- what is it for anyway? AGM
1205 _pam_drop_reply(resp, i);
1208 retval = (retval == PAM_SUCCESS)
1209 ? PAM_AUTHTOK_RECOVER_ERR : retval;
1213 if (retval != PAM_SUCCESS) {
1214 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1215 "unable to obtain a password");
1218 /* 'token' is the entered password */
1220 /* we store this password as an item */
1222 retval = pam_set_item(pamh, authtok_flag, token);
1223 _pam_delete(token); /* clean it up */
1224 if (retval != PAM_SUCCESS ||
1225 (retval = _pam_get_item(pamh, authtok_flag, &item)) != PAM_SUCCESS) {
1227 _pam_log(pamh, ctrl, LOG_CRIT, "error manipulating password");
1233 item = NULL; /* break link to password */
1238 const char *get_conf_item_string(const pam_handle_t *pamh,
1247 const char *parm_opt = NULL;
1250 if (!(ctrl & config_flag)) {
1254 /* let the pam opt take precedence over the pam_winbind.conf option */
1258 if (!asprintf(&key, "global:%s", item)) {
1262 parm_opt = iniparser_getstr(d, key);
1266 for ( i=0; i<argc; i++ ) {
1268 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
1271 if ( (p = strchr( argv[i], '=' )) == NULL) {
1272 _pam_log(pamh, ctrl, LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
1275 _pam_log_debug(pamh, ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
1281 _pam_log_debug(pamh, ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
1287 const char *get_krb5_cc_type_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1289 return get_conf_item_string(pamh, argc, argv, ctrl, d, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
1292 const char *get_member_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1294 const char *ret = NULL;
1295 ret = get_conf_item_string(pamh, argc, argv, ctrl, d, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
1299 return get_conf_item_string(pamh, argc, argv, ctrl, d, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
1303 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
1304 int argc, const char **argv)
1306 const char *username;
1307 const char *password;
1308 const char *member = NULL;
1309 const char *cctype = NULL;
1310 int retval = PAM_AUTH_ERR;
1311 dictionary *d = NULL;
1312 char *username_ret = NULL;
1313 char *new_authtok_required = NULL;
1315 /* parse arguments */
1316 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1318 retval = PAM_SYSTEM_ERR;
1322 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", pamh, ctrl, flags);
1324 /* Get the username */
1325 retval = pam_get_user(pamh, &username, NULL);
1326 if ((retval != PAM_SUCCESS) || (!username)) {
1327 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "can not get the username");
1328 retval = PAM_SERVICE_ERR;
1332 retval = _winbind_read_password(pamh, ctrl, NULL,
1336 if (retval != PAM_SUCCESS) {
1337 _pam_log(pamh, ctrl, LOG_ERR, "Could not retrieve user's password");
1338 retval = PAM_AUTHTOK_ERR;
1342 /* Let's not give too much away in the log file */
1344 #ifdef DEBUG_PASSWORD
1345 _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s' with password '%s'",
1346 username, password);
1348 _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s'", username);
1351 member = get_member_from_config(pamh, argc, argv, ctrl, d);
1353 cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1355 /* Now use the username to look up password */
1356 retval = winbind_auth_request(pamh, ctrl, username, password, member,
1357 cctype, NULL, NULL, &username_ret);
1359 if (retval == PAM_NEW_AUTHTOK_REQD ||
1360 retval == PAM_AUTHTOK_EXPIRED) {
1362 if (!asprintf(&new_authtok_required, "%d", retval)) {
1363 retval = PAM_BUF_ERR;
1367 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, new_authtok_required, _pam_winbind_cleanup_func);
1369 retval = PAM_SUCCESS;
1375 pam_set_item (pamh, PAM_USER, username_ret);
1376 _pam_log_debug(pamh, ctrl, LOG_INFO, "Returned user was '%s'", username_ret);
1381 iniparser_freedict(d);
1384 if (!new_authtok_required) {
1385 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
1388 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", pamh, ctrl, retval);
1394 int pam_sm_setcred(pam_handle_t *pamh, int flags,
1395 int argc, const char **argv)
1397 int ret = PAM_SYSTEM_ERR;
1398 dictionary *d = NULL;
1400 /* parse arguments */
1401 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1403 ret = PAM_SYSTEM_ERR;
1407 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", pamh, ctrl, flags);
1411 case PAM_DELETE_CRED:
1412 ret = pam_sm_close_session(pamh, flags, argc, argv);
1414 case PAM_REFRESH_CRED:
1415 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_REFRESH_CRED not implemented");
1418 case PAM_REINITIALIZE_CRED:
1419 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_REINITIALIZE_CRED not implemented");
1422 case PAM_ESTABLISH_CRED:
1423 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_ESTABLISH_CRED not implemented");
1427 ret = PAM_SYSTEM_ERR;
1433 iniparser_freedict(d);
1436 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", pamh, ctrl, ret);
1442 * Account management. We want to verify that the account exists
1443 * before returning PAM_SUCCESS
1446 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
1447 int argc, const char **argv)
1449 const char *username;
1450 int ret = PAM_USER_UNKNOWN;
1452 dictionary *d = NULL;
1454 /* parse arguments */
1455 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1457 return PAM_SYSTEM_ERR;
1460 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", pamh, ctrl, flags);
1463 /* Get the username */
1464 ret = pam_get_user(pamh, &username, NULL);
1465 if ((ret != PAM_SUCCESS) || (!username)) {
1466 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"can not get the username");
1467 ret = PAM_SERVICE_ERR;
1471 /* Verify the username */
1472 ret = valid_user(pamh, ctrl, username);
1475 /* some sort of system error. The log was already printed */
1476 ret = PAM_SERVICE_ERR;
1479 /* the user does not exist */
1480 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", username);
1481 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
1485 ret = PAM_USER_UNKNOWN;
1488 pam_get_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (const void **)&tmp);
1490 ret = atoi((const char *)tmp);
1492 case PAM_AUTHTOK_EXPIRED:
1493 /* fall through, since new token is required in this case */
1494 case PAM_NEW_AUTHTOK_REQD:
1495 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success but %s is set",
1496 PAM_WINBIND_NEW_AUTHTOK_REQD);
1497 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' needs new password", username);
1498 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
1499 ret = PAM_NEW_AUTHTOK_REQD;
1502 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success");
1503 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1509 /* Otherwise, the authentication looked good */
1510 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1514 /* we don't know anything about this return value */
1515 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (ret = %d, user = '%s')",
1517 ret = PAM_SERVICE_ERR;
1521 /* should not be reached */
1527 iniparser_freedict(d);
1530 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", pamh, ctrl, ret);
1536 int pam_sm_open_session(pam_handle_t *pamh, int flags,
1537 int argc, const char **argv)
1539 int ret = PAM_SYSTEM_ERR;
1540 dictionary *d = NULL;
1542 /* parse arguments */
1543 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1545 ret = PAM_SYSTEM_ERR;
1549 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", pamh, ctrl, flags);
1555 iniparser_freedict(d);
1558 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", pamh, ctrl, ret);
1564 int pam_sm_close_session(pam_handle_t *pamh, int flags,
1565 int argc, const char **argv)
1567 dictionary *d = NULL;
1568 int retval = PAM_SUCCESS;
1570 /* parse arguments */
1571 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1573 retval = PAM_SYSTEM_ERR;
1577 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", pamh, ctrl, flags);
1579 if (!(flags & PAM_DELETE_CRED)) {
1580 retval = PAM_SUCCESS;
1584 if (ctrl & WINBIND_KRB5_AUTH) {
1586 /* destroy the ccache here */
1587 struct winbindd_request request;
1588 struct winbindd_response response;
1590 const char *ccname = NULL;
1591 struct passwd *pwd = NULL;
1593 ZERO_STRUCT(request);
1594 ZERO_STRUCT(response);
1596 retval = pam_get_user(pamh, &user, "Username: ");
1597 if (retval == PAM_SUCCESS) {
1599 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1600 retval = PAM_USER_UNKNOWN;
1603 if (retval == PAM_SUCCESS) {
1604 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
1607 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "could not identify user");
1611 ccname = pam_getenv(pamh, "KRB5CCNAME");
1612 if (ccname == NULL) {
1613 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1616 strncpy(request.data.logoff.user, user,
1617 sizeof(request.data.logoff.user) - 1);
1620 strncpy(request.data.logoff.krb5ccname, ccname,
1621 sizeof(request.data.logoff.krb5ccname) - 1);
1624 pwd = getpwnam(user);
1626 retval = PAM_USER_UNKNOWN;
1629 request.data.logoff.uid = pwd->pw_uid;
1631 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1633 retval = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1638 iniparser_freedict(d);
1641 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", pamh, ctrl, retval);
1649 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1650 int argc, const char **argv)
1656 /* <DO NOT free() THESE> */
1658 char *pass_old, *pass_new;
1659 /* </DO NOT free() THESE> */
1664 dictionary *d = NULL;
1666 ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1668 ret = PAM_SYSTEM_ERR;
1672 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", pamh, ctrl, flags);
1674 /* clearing offline bit for the auth in the password change */
1675 ctrl &= ~WINBIND_CACHED_LOGIN;
1678 * First get the name of a user
1680 ret = pam_get_user(pamh, &user, "Username: ");
1681 if (ret == PAM_SUCCESS) {
1683 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1684 ret = PAM_USER_UNKNOWN;
1687 if (ret == PAM_SUCCESS) {
1688 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained",
1692 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1693 "password - could not identify user");
1697 /* check if this is really a user in winbindd, not only in NSS */
1698 ret = valid_user(pamh, ctrl, user);
1701 ret = PAM_USER_UNKNOWN;
1704 ret = PAM_SYSTEM_ERR;
1711 * obtain and verify the current password (OLDAUTHTOK) for
1715 if (flags & PAM_PRELIM_CHECK) {
1716 struct winbindd_response response;
1717 time_t pwdlastset_prelim = 0;
1719 /* instruct user what is happening */
1720 #define greeting "Changing password for "
1721 Announce = (char *) malloc(sizeof(greeting) + strlen(user));
1722 if (Announce == NULL) {
1723 _pam_log(pamh, ctrl, LOG_CRIT, "password - out of memory");
1727 (void) strcpy(Announce, greeting);
1728 (void) strcpy(Announce + sizeof(greeting) - 1, user);
1731 lctrl = ctrl | WINBIND__OLD_PASSWORD;
1732 ret = _winbind_read_password(pamh, lctrl,
1734 "(current) NT password: ",
1736 (const char **) &pass_old);
1737 if (ret != PAM_SUCCESS) {
1738 _pam_log(pamh, ctrl, LOG_NOTICE, "password - (old) token not obtained");
1742 /* We don't need krb5 env set for password change test. */
1743 ctrl &= ~WINBIND_KRB5_AUTH;
1745 /* verify that this is the password for this user */
1747 ret = winbind_auth_request(pamh, ctrl, user, pass_old,
1748 NULL, NULL, &response, &pwdlastset_prelim, NULL);
1750 if (ret != PAM_ACCT_EXPIRED &&
1751 ret != PAM_AUTHTOK_EXPIRED &&
1752 ret != PAM_NEW_AUTHTOK_REQD &&
1753 ret != PAM_SUCCESS) {
1756 iniparser_freedict(d);
1758 /* Deal with offline errors. */
1759 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1761 "NT_STATUS_NO_LOGON_SERVERS");
1762 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1764 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1765 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1767 "NT_STATUS_ACCESS_DENIED");
1771 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET, (void *)pwdlastset_prelim, NULL);
1773 ret = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
1775 if (ret != PAM_SUCCESS) {
1776 _pam_log(pamh, ctrl, LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
1778 } else if (flags & PAM_UPDATE_AUTHTOK) {
1780 time_t pwdlastset_update = 0;
1783 * obtain the proposed password
1787 * get the old token back.
1790 ret = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
1792 if (ret != PAM_SUCCESS) {
1793 _pam_log(pamh, ctrl, LOG_NOTICE, "user not authenticated");
1797 lctrl = ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
1799 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
1800 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
1803 ret = PAM_AUTHTOK_ERR;
1804 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
1806 * use_authtok is to force the use of a previously entered
1807 * password -- needed for pluggable password strength checking
1810 ret = _winbind_read_password(pamh, lctrl,
1812 "Enter new NT password: ",
1813 "Retype new NT password: ",
1814 (const char **) &pass_new);
1816 if (ret != PAM_SUCCESS) {
1817 _pam_log_debug(pamh, ctrl, LOG_ALERT
1818 ,"password - new password not obtained");
1819 pass_old = NULL;/* tidy up */
1824 * At this point we know who the user is and what they
1825 * propose as their new password. Verify that the new
1826 * password is acceptable.
1829 if (pass_new[0] == '\0') {/* "\0" password = NULL */
1835 * By reaching here we have approved the passwords and must now
1836 * rebuild the password database file.
1838 _pam_get_data( pamh, PAM_WINBIND_PWD_LAST_SET,
1839 &pwdlastset_update);
1841 ret = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new, pwdlastset_update);
1843 _pam_overwrite(pass_new);
1844 _pam_overwrite(pass_old);
1845 pass_old = pass_new = NULL;
1849 /* just in case we need krb5 creds after a password change over msrpc */
1851 if (ctrl & WINBIND_KRB5_AUTH) {
1852 struct winbindd_response response;
1854 const char *member = get_member_from_config(pamh, argc, argv, ctrl, d);
1855 const char *cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1857 ret = winbind_auth_request(pamh, ctrl, user, pass_new,
1858 member, cctype, &response, NULL, NULL);
1859 _pam_overwrite(pass_new);
1860 _pam_overwrite(pass_old);
1861 pass_old = pass_new = NULL;
1863 iniparser_freedict(d);
1865 /* Deal with offline errors. */
1866 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1868 "NT_STATUS_NO_LOGON_SERVERS");
1869 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1871 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1872 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh,
1874 "NT_STATUS_ACCESS_DENIED");
1878 ret = PAM_SERVICE_ERR;
1883 iniparser_freedict(d);
1886 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", pamh, ctrl, ret);
1893 /* static module data */
1895 struct pam_module _pam_winbind_modstruct = {
1897 pam_sm_authenticate,
1900 pam_sm_open_session,
1901 pam_sm_close_session,
1908 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
1909 * Copyright (c) Tim Potter <tpot@samba.org> 2000
1910 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1911 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2007
1912 * Copyright (c) Jan Rêkorajski 1999.
1913 * Copyright (c) Andrew G. Morgan 1996-8.
1914 * Copyright (c) Alex O. Yuriev, 1996.
1915 * Copyright (c) Cristian Gafton 1996.
1916 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
1918 * Redistribution and use in source and binary forms, with or without
1919 * modification, are permitted provided that the following conditions
1921 * 1. Redistributions of source code must retain the above copyright
1922 * notice, and the entire permission notice in its entirety,
1923 * including the disclaimer of warranties.
1924 * 2. Redistributions in binary form must reproduce the above copyright
1925 * notice, this list of conditions and the following disclaimer in the
1926 * documentation and/or other materials provided with the distribution.
1927 * 3. The name of the author may not be used to endorse or promote
1928 * products derived from this software without specific prior
1929 * written permission.
1931 * ALTERNATIVELY, this product may be distributed under the terms of
1932 * the GNU Public License, in which case the provisions of the GPL are
1933 * required INSTEAD OF the above restrictions. (This clause is
1934 * necessary due to a potential bad interaction between the GPL and
1935 * the restrictions contained in a BSD-style copyright.)
1937 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
1938 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1939 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1940 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1941 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1942 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1943 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1944 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1945 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1946 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1947 * OF THE POSSIBILITY OF SUCH DAMAGE.