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 BOOL _pam_log_is_silent(int ctrl)
78 return on(ctrl, WINBIND_SILENT);
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 BOOL _pam_log_is_debug_enabled(int ctrl)
100 if (_pam_log_is_silent(ctrl)) {
104 if (!(ctrl & WINBIND_DEBUG_ARG)) {
111 static BOOL _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, "debug_state"))
271 ctrl |= WINBIND_DEBUG_STATE;
272 else if (!strcasecmp(*v, "use_authtok"))
273 ctrl |= WINBIND_USE_AUTHTOK_ARG;
274 else if (!strcasecmp(*v, "use_first_pass"))
275 ctrl |= WINBIND_USE_FIRST_PASS_ARG;
276 else if (!strcasecmp(*v, "try_first_pass"))
277 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
278 else if (!strcasecmp(*v, "unknown_ok"))
279 ctrl |= WINBIND_UNKNOWN_OK_ARG;
280 else if (!strncasecmp(*v, "require_membership_of", strlen("require_membership_of")))
281 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
282 else if (!strncasecmp(*v, "require-membership-of", strlen("require-membership-of")))
283 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
284 else if (!strcasecmp(*v, "krb5_auth"))
285 ctrl |= WINBIND_KRB5_AUTH;
286 else if (!strncasecmp(*v, "krb5_ccache_type", strlen("krb5_ccache_type")))
287 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
288 else if (!strcasecmp(*v, "cached_login"))
289 ctrl |= WINBIND_CACHED_LOGIN;
291 _pam_log(pamh, ctrl, LOG_ERR, "pam_parse: unknown option: %s", *v);
301 iniparser_freedict(d);
308 static void _pam_winbind_cleanup_func(pam_handle_t *pamh, void *data, int error_status)
310 int ctrl = _pam_parse(pamh, 0, 0, NULL, NULL);
311 if (_pam_log_is_debug_state_enabled(ctrl)) {
312 _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);
318 static const struct ntstatus_errors {
319 const char *ntstatus_string;
320 const char *error_string;
321 } ntstatus_errors[] = {
322 {"NT_STATUS_OK", "Success"},
323 {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
324 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND", "No domain controllers found"},
325 {"NT_STATUS_NO_LOGON_SERVERS", "No logon servers"},
326 {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
327 {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
328 {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
329 {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
330 {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
331 {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
332 {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
333 {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
334 {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
335 {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
336 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
337 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
338 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
339 {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
343 const char *_get_ntstatus_error_string(const char *nt_status_string)
346 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
347 if (!strcasecmp(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
348 return ntstatus_errors[i].error_string;
354 /* --- authentication management functions --- */
356 /* Attempt a conversation */
358 static int converse(pam_handle_t *pamh, int nargs,
359 struct pam_message **message,
360 struct pam_response **response)
363 struct pam_conv *conv;
365 retval = _pam_get_item(pamh, PAM_CONV, &conv );
366 if (retval == PAM_SUCCESS) {
367 retval = conv->conv(nargs, (const struct pam_message **)message,
368 response, conv->appdata_ptr);
371 return retval; /* propagate error status */
375 static int _make_remark(pam_handle_t * pamh, int flags, int type, const char *text)
377 int retval = PAM_SUCCESS;
379 struct pam_message *pmsg[1], msg[1];
380 struct pam_response *resp;
382 if (flags & WINBIND_SILENT) {
387 msg[0].msg = CONST_DISCARD(char *, text);
388 msg[0].msg_style = type;
391 retval = converse(pamh, 1, pmsg, &resp);
394 _pam_drop_reply(resp, 1);
399 static int _make_remark_v(pam_handle_t * pamh, int flags, int type, const char *format, va_list args)
404 ret = vasprintf(&var, format, args);
406 _pam_log(pamh, 0, LOG_ERR, "memory allocation failure");
410 ret = _make_remark(pamh, flags, type, var);
415 static int _make_remark_format(pam_handle_t * pamh, int flags, int type, const char *format, ...)
420 va_start(args, format);
421 ret = _make_remark_v(pamh, flags, type, format, args);
426 static int pam_winbind_request(pam_handle_t * pamh, int ctrl,
427 enum winbindd_cmd req_type,
428 struct winbindd_request *request,
429 struct winbindd_response *response)
431 /* Fill in request and send down pipe */
432 init_request(request, req_type);
434 if (write_sock(request, sizeof(*request), 0) == -1) {
435 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: write to socket failed!");
437 return PAM_SERVICE_ERR;
441 if (read_reply(response) == -1) {
442 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: read from socket failed!");
444 return PAM_SERVICE_ERR;
447 /* We are done with the socket - close it and avoid mischeif */
450 /* Copy reply data from socket */
451 if (response->result == WINBINDD_OK) {
455 /* no need to check for pam_error codes for getpwnam() */
458 case WINBINDD_GETPWNAM:
459 case WINBINDD_LOOKUPNAME:
460 _pam_log(pamh, ctrl, LOG_ERR, "request failed: %s, NT error was %s",
461 response->data.auth.nt_status_string);
462 return PAM_USER_UNKNOWN;
467 if (response->data.auth.pam_error != PAM_SUCCESS) {
468 _pam_log(pamh, ctrl, LOG_ERR, "request failed: %s, PAM error was %s (%d), NT error was %s",
469 response->data.auth.error_string,
470 pam_strerror(pamh, response->data.auth.pam_error),
471 response->data.auth.pam_error,
472 response->data.auth.nt_status_string);
473 return response->data.auth.pam_error;
476 _pam_log(pamh, ctrl, LOG_ERR, "request failed, but PAM error 0!");
478 return PAM_SERVICE_ERR;
481 static int pam_winbind_request_log(pam_handle_t * pamh,
483 enum winbindd_cmd req_type,
484 struct winbindd_request *request,
485 struct winbindd_response *response,
490 retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
494 /* incorrect password */
495 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' denied access (incorrect password or invalid membership)", user);
497 case PAM_ACCT_EXPIRED:
498 /* account expired */
499 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' account expired", user);
501 case PAM_AUTHTOK_EXPIRED:
502 /* password expired */
503 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' password expired", user);
505 case PAM_NEW_AUTHTOK_REQD:
506 /* new password required */
507 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' new password required", user);
509 case PAM_USER_UNKNOWN:
510 /* the user does not exist */
511 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", user);
512 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
517 if (req_type == WINBINDD_PAM_AUTH) {
518 /* Otherwise, the authentication looked good */
519 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", user);
520 } else if (req_type == WINBINDD_PAM_CHAUTHTOK) {
521 /* Otherwise, the authentication looked good */
522 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' password changed", user);
524 /* Otherwise, the authentication looked good */
525 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' OK", user);
530 /* we don't know anything about this return value */
531 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (retval = %d, user = '%s')",
537 static BOOL _pam_send_password_expiry_message(pam_handle_t *pamh, int ctrl, time_t next_change, time_t now)
540 struct tm tm_now, tm_next_change;
542 if ((next_change < 0) ||
543 (next_change < now) ||
544 (next_change > now + DAYS_TO_WARN_BEFORE_PWD_EXPIRES * SECONDS_PER_DAY)) {
548 if ((localtime_r(&now, &tm_now) == NULL) ||
549 (localtime_r(&next_change, &tm_next_change) == NULL)) {
553 days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) - (tm_now.tm_yday+tm_now.tm_year*365);
556 _make_remark(pamh, ctrl, PAM_TEXT_INFO, "Your password expires today");
560 if (days > 0 && days < DAYS_TO_WARN_BEFORE_PWD_EXPIRES) {
561 _make_remark_format(pamh, ctrl, PAM_TEXT_INFO, "Your password will expire in %d %s",
562 days, (days > 1) ? "days":"day");
569 static void _pam_warn_password_expires_in_future(pam_handle_t *pamh, int ctrl, struct winbindd_response *response)
571 time_t now = time(NULL);
572 time_t next_change = 0;
574 /* accounts with ACB_PWNOEXP set never receive a warning */
575 if (response->data.auth.info3.acct_flags & ACB_PWNOEXP) {
579 /* no point in sending a warning if this is a grace logon */
580 if (PAM_WB_GRACE_LOGON(response->data.auth.info3.user_flgs)) {
584 /* check if the info3 must change timestamp has been set */
585 next_change = response->data.auth.info3.pass_must_change_time;
587 if (_pam_send_password_expiry_message(pamh, ctrl, next_change, now)) {
591 /* now check for the global password policy */
592 if (response->data.auth.policy.expire <= 0) {
596 next_change = response->data.auth.info3.pass_last_set_time +
597 response->data.auth.policy.expire;
599 if (_pam_send_password_expiry_message(pamh, ctrl, next_change, now)) {
603 /* no warning sent */
606 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
608 static BOOL safe_append_string(char *dest,
610 int dest_buffer_size)
612 * Append a string, making sure not to overflow and to always return a NULL-terminated
615 * @param dest Destination string buffer (must already be NULL-terminated).
616 * @param src Source string buffer.
617 * @param dest_buffer_size Size of dest buffer in bytes.
619 * @return False if dest buffer is not big enough (no bytes copied), True on success.
622 int dest_length = strlen(dest);
623 int src_length = strlen(src);
625 if ( dest_length + src_length + 1 > dest_buffer_size ) {
629 memcpy(dest + dest_length, src, src_length + 1);
633 static BOOL winbind_name_to_sid_string(pam_handle_t *pamh,
637 char *sid_list_buffer,
638 int sid_list_buffer_size)
640 * Convert a names into a SID string, appending it to a buffer.
642 * @param pamh PAM handle
643 * @param ctrl PAM winbind options.
644 * @param user User in PAM request.
645 * @param name Name to convert.
646 * @param sid_list_buffer Where to append the string sid.
647 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
649 * @return False on failure, True on success.
652 const char* sid_string;
653 struct winbindd_response sid_response;
656 if (IS_SID_STRING(name)) {
659 struct winbindd_request sid_request;
661 ZERO_STRUCT(sid_request);
662 ZERO_STRUCT(sid_response);
664 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", name);
666 /* fortunatly winbindd can handle non-separated names */
667 strncpy(sid_request.data.name.name, name,
668 sizeof(sid_request.data.name.name) - 1);
670 if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME, &sid_request, &sid_response, user)) {
671 _pam_log(pamh, ctrl, LOG_INFO, "could not lookup name: %s\n", name);
675 sid_string = sid_response.data.sid.sid;
678 if (!safe_append_string(sid_list_buffer, sid_string, sid_list_buffer_size)) {
685 static BOOL winbind_name_list_to_sid_string_list(pam_handle_t *pamh,
688 const char *name_list,
689 char *sid_list_buffer,
690 int sid_list_buffer_size)
692 * Convert a list of names into a list of sids.
694 * @param pamh PAM handle
695 * @param ctrl PAM winbind options.
696 * @param user User in PAM request.
697 * @param name_list List of names or string sids, separated by commas.
698 * @param sid_list_buffer Where to put the list of string sids.
699 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
701 * @return False on failure, True on success.
705 char *current_name = NULL;
706 const char *search_location;
709 if ( sid_list_buffer_size > 0 ) {
710 sid_list_buffer[0] = 0;
713 search_location = name_list;
714 while ( (comma = strstr(search_location, ",")) != NULL ) {
715 current_name = strndup(search_location, comma - search_location);
716 if (NULL == current_name) {
720 if (!winbind_name_to_sid_string(pamh, ctrl, user, current_name, sid_list_buffer, sid_list_buffer_size)) {
724 SAFE_FREE(current_name);
726 if (!safe_append_string(sid_list_buffer, ",", sid_list_buffer_size)) {
730 search_location = comma + 1;
733 if (!winbind_name_to_sid_string(pamh, ctrl, user, search_location, sid_list_buffer, sid_list_buffer_size)) {
740 SAFE_FREE(current_name);
745 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
747 * @param response The struct winbindd_response.
749 * @return string (caller needs to free).
752 static char *_pam_compose_pwd_restriction_string(struct winbindd_response *response)
755 size_t offset = 0, ret = 0, str_size = 1024;
757 str = (char *)malloc(str_size);
762 memset(str, '\0', str_size);
764 offset = snprintf(str, str_size, "Your password ");
769 if (response->data.auth.policy.min_length_password > 0) {
770 ret = snprintf(str+offset, str_size-offset,
771 "must be at least %d characters; ",
772 response->data.auth.policy.min_length_password);
779 if (response->data.auth.policy.password_history > 0) {
780 ret = snprintf(str+offset, str_size-offset,
781 "cannot repeat any of your previous %d passwords; ",
782 response->data.auth.policy.password_history);
789 if (response->data.auth.policy.password_properties & DOMAIN_PASSWORD_COMPLEX) {
790 ret = snprintf(str+offset, str_size-offset,
791 "must contain capitals, numerals or punctuation; "
792 "and cannot contain your account or full name; ");
799 ret = snprintf(str+offset, str_size-offset,
800 "Please type a different password. "
801 "Type a password which meets these requirements in both text boxes.");
813 /* talk to winbindd */
814 static int winbind_auth_request(pam_handle_t * pamh,
820 struct winbindd_response *p_response,
821 time_t *pwd_last_set,
824 struct winbindd_request request;
825 struct winbindd_response response;
828 ZERO_STRUCT(request);
829 ZERO_STRUCT(response);
835 strncpy(request.data.auth.user, user,
836 sizeof(request.data.auth.user)-1);
838 strncpy(request.data.auth.pass, pass,
839 sizeof(request.data.auth.pass)-1);
841 request.data.auth.krb5_cc_type[0] = '\0';
842 request.data.auth.uid = -1;
844 request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_CONTACT_TRUSTDOM;
846 if (ctrl & WINBIND_KRB5_AUTH) {
848 struct passwd *pwd = NULL;
850 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling krb5 login flag\n");
852 request.flags |= WBFLAG_PAM_KRB5 | WBFLAG_PAM_FALLBACK_AFTER_KRB5;
854 pwd = getpwnam(user);
856 return PAM_USER_UNKNOWN;
858 request.data.auth.uid = pwd->pw_uid;
861 if (ctrl & WINBIND_CACHED_LOGIN) {
862 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling cached login flag\n");
863 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
868 request.flags |= WBFLAG_PAM_UNIX_NAME;
871 if (cctype != NULL) {
872 strncpy(request.data.auth.krb5_cc_type, cctype,
873 sizeof(request.data.auth.krb5_cc_type) - 1);
874 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling request for a %s krb5 ccache\n", cctype);
877 request.data.auth.require_membership_of_sid[0] = '\0';
879 if (member != NULL) {
881 if (!winbind_name_list_to_sid_string_list(pamh, ctrl, user, member,
882 request.data.auth.require_membership_of_sid,
883 sizeof(request.data.auth.require_membership_of_sid))) {
885 _pam_log_debug(pamh, ctrl, LOG_ERR, "failed to serialize membership of sid \"%s\"\n", member);
890 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
893 *pwd_last_set = response.data.auth.info3.pass_last_set_time;
896 if ((ctrl & WINBIND_KRB5_AUTH) &&
897 response.data.auth.krb5ccname[0] != '\0') {
901 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "request returned KRB5CCNAME: %s",
902 response.data.auth.krb5ccname);
904 snprintf(var, sizeof(var), "KRB5CCNAME=%s", response.data.auth.krb5ccname);
906 ret = pam_putenv(pamh, var);
907 if (ret != PAM_SUCCESS) {
908 _pam_log(pamh, ctrl, LOG_ERR, "failed to set KRB5CCNAME to %s", var);
914 /* We want to process the response in the caller. */
915 *p_response = response;
920 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PASSWORD_EXPIRED");
921 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PASSWORD_MUST_CHANGE");
922 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_INVALID_WORKSTATION");
923 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_INVALID_LOGON_HOURS");
924 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCOUNT_EXPIRED");
925 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCOUNT_DISABLED");
926 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCOUNT_LOCKED_OUT");
927 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
928 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
929 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
930 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
931 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NO_LOGON_SERVERS");
934 /* handle the case where the auth was ok, but the password must expire right now */
935 /* good catch from Ralf Haferkamp: an expiry of "never" is translated to -1 */
936 if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
937 ! (PAM_WB_GRACE_LOGON(response.data.auth.info3.user_flgs)) &&
938 (response.data.auth.policy.expire > 0) &&
939 (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire < time(NULL))) {
941 ret = PAM_AUTHTOK_EXPIRED;
943 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"Password has expired (Password was last set: %d, "
944 "the policy says it should expire here %d (now it's: %d)\n",
945 response.data.auth.info3.pass_last_set_time,
946 response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire,
949 PAM_WB_REMARK_DIRECT_RET(pamh, ctrl, "NT_STATUS_PASSWORD_EXPIRED");
953 /* warn a user if the password is about to expire soon */
954 _pam_warn_password_expires_in_future(pamh, ctrl, &response);
956 /* inform about logon type */
957 if (PAM_WB_GRACE_LOGON(response.data.auth.info3.user_flgs)) {
959 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
960 "Grace login. Please change your password as soon you're online again");
961 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
962 "User %s logged on using grace logon\n", user);
964 } else if (PAM_WB_CACHED_LOGON(response.data.auth.info3.user_flgs)) {
966 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
967 "Logging on using cached account. Network resources can be unavailable");
968 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
969 "User %s logged on using cached account\n", user);
972 /* save the CIFS homedir for pam_cifs / pam_mount */
973 if (response.data.auth.info3.home_dir[0] != '\0') {
975 int ret2 = pam_set_data(pamh, PAM_WINBIND_HOMEDIR,
976 (void *) strdup(response.data.auth.info3.home_dir),
977 _pam_winbind_cleanup_func);
979 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s",
980 pam_strerror(pamh, ret2));
985 /* save the logon script path for other PAM modules */
986 if (response.data.auth.info3.logon_script[0] != '\0') {
988 int ret2 = pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT,
989 (void *) strdup(response.data.auth.info3.logon_script),
990 _pam_winbind_cleanup_func);
992 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s",
993 pam_strerror(pamh, ret2));
997 /* save the profile path for other PAM modules */
998 if (response.data.auth.info3.profile_path[0] != '\0') {
1000 int ret2 = pam_set_data(pamh, PAM_WINBIND_PROFILEPATH,
1001 (void *) strdup(response.data.auth.info3.profile_path),
1002 _pam_winbind_cleanup_func);
1004 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s",
1005 pam_strerror(pamh, ret2));
1009 /* If winbindd returned a username, return the pointer to it here. */
1010 if (user_ret && response.extra_data.data) {
1011 /* We have to trust it's a null terminated string. */
1012 *user_ret = (char *)response.extra_data.data;
1018 /* talk to winbindd */
1019 static int winbind_chauthtok_request(pam_handle_t * pamh,
1022 const char *oldpass,
1023 const char *newpass,
1024 time_t pwd_last_set)
1026 struct winbindd_request request;
1027 struct winbindd_response response;
1030 ZERO_STRUCT(request);
1031 ZERO_STRUCT(response);
1033 if (request.data.chauthtok.user == NULL) return -2;
1035 strncpy(request.data.chauthtok.user, user,
1036 sizeof(request.data.chauthtok.user) - 1);
1038 if (oldpass != NULL) {
1039 strncpy(request.data.chauthtok.oldpass, oldpass,
1040 sizeof(request.data.chauthtok.oldpass) - 1);
1042 request.data.chauthtok.oldpass[0] = '\0';
1045 if (newpass != NULL) {
1046 strncpy(request.data.chauthtok.newpass, newpass,
1047 sizeof(request.data.chauthtok.newpass) - 1);
1049 request.data.chauthtok.newpass[0] = '\0';
1052 if (ctrl & WINBIND_KRB5_AUTH) {
1053 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1056 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
1058 if (ret == PAM_SUCCESS) {
1062 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_BACKUP_CONTROLLER");
1063 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1064 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NO_LOGON_SERVERS");
1065 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCESS_DENIED");
1067 /* TODO: tell the min pwd length ? */
1068 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_TOO_SHORT");
1070 /* TODO: tell the minage ? */
1071 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_TOO_RECENT");
1073 /* TODO: tell the history length ? */
1074 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
1076 if (!strcasecmp(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
1078 char *pwd_restriction_string = NULL;
1080 /* FIXME: avoid to send multiple PAM messages after another */
1081 switch (response.data.auth.reject_reason) {
1084 case REJECT_REASON_OTHER:
1085 if ((response.data.auth.policy.min_passwordage > 0) &&
1086 (pwd_last_set + response.data.auth.policy.min_passwordage > time(NULL))) {
1087 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PWD_TOO_RECENT");
1090 case REJECT_REASON_TOO_SHORT:
1091 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PWD_TOO_SHORT");
1093 case REJECT_REASON_IN_HISTORY:
1094 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PWD_HISTORY_CONFLICT");
1096 case REJECT_REASON_NOT_COMPLEX:
1097 _make_remark(pamh, ctrl, PAM_ERROR_MSG, "Password does not meet complexity requirements");
1100 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1101 "unknown password change reject reason: %d",
1102 response.data.auth.reject_reason);
1106 pwd_restriction_string = _pam_compose_pwd_restriction_string(&response);
1107 if (pwd_restriction_string) {
1108 _make_remark(pamh, ctrl, PAM_ERROR_MSG, pwd_restriction_string);
1109 SAFE_FREE(pwd_restriction_string);
1117 * Checks if a user has an account
1120 * 1 = User not found
1124 static int valid_user(pam_handle_t *pamh, int ctrl, const char *user)
1126 /* check not only if the user is available over NSS calls, also make
1127 * sure it's really a winbind user, this is important when stacking PAM
1128 * modules in the 'account' or 'password' facility. */
1130 struct passwd *pwd = NULL;
1131 struct winbindd_request request;
1132 struct winbindd_response response;
1135 ZERO_STRUCT(request);
1136 ZERO_STRUCT(response);
1138 pwd = getpwnam(user);
1143 strncpy(request.data.username, user,
1144 sizeof(request.data.username) - 1);
1146 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_GETPWNAM, &request, &response, user);
1149 case PAM_USER_UNKNOWN:
1159 static char *_pam_delete(register char *xx)
1167 * obtain a password from the user
1170 static int _winbind_read_password(pam_handle_t * pamh,
1172 const char *comment,
1173 const char *prompt1,
1174 const char *prompt2,
1182 _pam_log(pamh, ctrl, LOG_DEBUG, "getting password (0x%08x)", ctrl);
1185 * make sure nothing inappropriate gets returned
1188 *pass = token = NULL;
1191 * which authentication token are we getting?
1194 authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
1197 * should we obtain the password from a PAM item ?
1200 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1201 retval = _pam_get_item(pamh, authtok_flag, &item);
1202 if (retval != PAM_SUCCESS) {
1204 _pam_log(pamh, ctrl, LOG_ALERT,
1205 "pam_get_item returned error to unix-read-password"
1208 } else if (item != NULL) { /* we have a password! */
1211 _pam_log(pamh, ctrl, LOG_DEBUG,
1212 "pam_get_item returned a password");
1214 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1215 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
1216 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
1217 && off(WINBIND__OLD_PASSWORD, ctrl)) {
1218 return PAM_AUTHTOK_RECOVER_ERR;
1222 * getting here implies we will have to get the password from the
1227 struct pam_message msg[3], *pmsg[3];
1228 struct pam_response *resp;
1231 /* prepare to converse */
1233 if (comment != NULL && off(ctrl, WINBIND_SILENT)) {
1235 msg[0].msg_style = PAM_TEXT_INFO;
1236 msg[0].msg = CONST_DISCARD(char *, comment);
1243 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1244 msg[i++].msg = CONST_DISCARD(char *, prompt1);
1247 if (prompt2 != NULL) {
1249 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1250 msg[i++].msg = CONST_DISCARD(char *, prompt2);
1253 /* so call the conversation expecting i responses */
1255 retval = converse(pamh, i, pmsg, &resp);
1259 /* interpret the response */
1261 if (retval == PAM_SUCCESS) { /* a good conversation */
1263 token = x_strdup(resp[i - replies].resp);
1264 if (token != NULL) {
1266 /* verify that password entered correctly */
1267 if (!resp[i - 1].resp
1268 || strcmp(token, resp[i - 1].resp)) {
1269 _pam_delete(token); /* mistyped */
1270 retval = PAM_AUTHTOK_RECOVER_ERR;
1271 _make_remark(pamh, ctrl, PAM_ERROR_MSG, MISTYPED_PASS);
1275 _pam_log(pamh, ctrl, LOG_NOTICE, "could not recover authentication token");
1276 retval = PAM_AUTHTOK_RECOVER_ERR;
1281 * tidy up the conversation (resp_retcode) is ignored
1282 * -- what is it for anyway? AGM
1285 _pam_drop_reply(resp, i);
1288 retval = (retval == PAM_SUCCESS)
1289 ? PAM_AUTHTOK_RECOVER_ERR : retval;
1293 if (retval != PAM_SUCCESS) {
1294 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1295 "unable to obtain a password");
1298 /* 'token' is the entered password */
1300 /* we store this password as an item */
1302 retval = pam_set_item(pamh, authtok_flag, token);
1303 _pam_delete(token); /* clean it up */
1304 if (retval != PAM_SUCCESS ||
1305 (retval = _pam_get_item(pamh, authtok_flag, &item)) != PAM_SUCCESS) {
1307 _pam_log(pamh, ctrl, LOG_CRIT, "error manipulating password");
1313 item = NULL; /* break link to password */
1318 const char *get_conf_item_string(const pam_handle_t *pamh,
1327 const char *parm_opt = NULL;
1330 if (!(ctrl & config_flag)) {
1334 /* let the pam opt take precedence over the pam_winbind.conf option */
1338 if (!asprintf(&key, "global:%s", item)) {
1342 parm_opt = iniparser_getstr(d, key);
1346 for ( i=0; i<argc; i++ ) {
1348 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
1351 if ( (p = strchr( argv[i], '=' )) == NULL) {
1352 _pam_log(pamh, ctrl, LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
1355 _pam_log_debug(pamh, ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
1361 _pam_log_debug(pamh, ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
1367 const char *get_krb5_cc_type_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1369 return get_conf_item_string(pamh, argc, argv, ctrl, d, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
1372 const char *get_member_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1374 const char *ret = NULL;
1375 ret = get_conf_item_string(pamh, argc, argv, ctrl, d, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
1379 return get_conf_item_string(pamh, argc, argv, ctrl, d, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
1383 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
1384 int argc, const char **argv)
1386 const char *username;
1387 const char *password;
1388 const char *member = NULL;
1389 const char *cctype = NULL;
1390 int retval = PAM_AUTH_ERR;
1391 dictionary *d = NULL;
1392 char *username_ret = NULL;
1393 char *new_authtok_required = NULL;
1395 /* parse arguments */
1396 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1398 retval = PAM_SYSTEM_ERR;
1402 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", pamh, ctrl, flags);
1404 /* Get the username */
1405 retval = pam_get_user(pamh, &username, NULL);
1406 if ((retval != PAM_SUCCESS) || (!username)) {
1407 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "can not get the username");
1408 retval = PAM_SERVICE_ERR;
1412 retval = _winbind_read_password(pamh, ctrl, NULL,
1416 if (retval != PAM_SUCCESS) {
1417 _pam_log(pamh, ctrl, LOG_ERR, "Could not retrieve user's password");
1418 retval = PAM_AUTHTOK_ERR;
1422 /* Let's not give too much away in the log file */
1424 #ifdef DEBUG_PASSWORD
1425 _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s' with password '%s'",
1426 username, password);
1428 _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s'", username);
1431 member = get_member_from_config(pamh, argc, argv, ctrl, d);
1433 cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1435 /* Now use the username to look up password */
1436 retval = winbind_auth_request(pamh, ctrl, username, password, member,
1437 cctype, NULL, NULL, &username_ret);
1439 if (retval == PAM_NEW_AUTHTOK_REQD ||
1440 retval == PAM_AUTHTOK_EXPIRED) {
1442 if (!asprintf(&new_authtok_required, "%d", retval)) {
1443 retval = PAM_BUF_ERR;
1447 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, new_authtok_required, _pam_winbind_cleanup_func);
1449 retval = PAM_SUCCESS;
1455 pam_set_item (pamh, PAM_USER, username_ret);
1456 _pam_log_debug(pamh, ctrl, LOG_INFO, "Returned user was '%s'", username_ret);
1461 iniparser_freedict(d);
1464 if (!new_authtok_required) {
1465 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
1468 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", pamh, ctrl, retval);
1474 int pam_sm_setcred(pam_handle_t *pamh, int flags,
1475 int argc, const char **argv)
1477 int ret = PAM_SYSTEM_ERR;
1478 dictionary *d = NULL;
1480 /* parse arguments */
1481 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1483 ret = PAM_SYSTEM_ERR;
1487 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", pamh, ctrl, flags);
1489 switch (flags & ~PAM_SILENT) {
1491 case PAM_DELETE_CRED:
1492 ret = pam_sm_close_session(pamh, flags, argc, argv);
1494 case PAM_REFRESH_CRED:
1495 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_REFRESH_CRED not implemented");
1498 case PAM_REINITIALIZE_CRED:
1499 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_REINITIALIZE_CRED not implemented");
1502 case PAM_ESTABLISH_CRED:
1503 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_ESTABLISH_CRED not implemented");
1507 ret = PAM_SYSTEM_ERR;
1513 iniparser_freedict(d);
1516 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", pamh, ctrl, ret);
1522 * Account management. We want to verify that the account exists
1523 * before returning PAM_SUCCESS
1526 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
1527 int argc, const char **argv)
1529 const char *username;
1530 int ret = PAM_USER_UNKNOWN;
1532 dictionary *d = NULL;
1534 /* parse arguments */
1535 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1537 return PAM_SYSTEM_ERR;
1540 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", pamh, ctrl, flags);
1543 /* Get the username */
1544 ret = pam_get_user(pamh, &username, NULL);
1545 if ((ret != PAM_SUCCESS) || (!username)) {
1546 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"can not get the username");
1547 ret = PAM_SERVICE_ERR;
1551 /* Verify the username */
1552 ret = valid_user(pamh, ctrl, username);
1555 /* some sort of system error. The log was already printed */
1556 ret = PAM_SERVICE_ERR;
1559 /* the user does not exist */
1560 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", username);
1561 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
1565 ret = PAM_USER_UNKNOWN;
1568 pam_get_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (const void **)&tmp);
1570 ret = atoi((const char *)tmp);
1572 case PAM_AUTHTOK_EXPIRED:
1573 /* fall through, since new token is required in this case */
1574 case PAM_NEW_AUTHTOK_REQD:
1575 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success but %s is set",
1576 PAM_WINBIND_NEW_AUTHTOK_REQD);
1577 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' needs new password", username);
1578 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
1579 ret = PAM_NEW_AUTHTOK_REQD;
1582 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success");
1583 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1589 /* Otherwise, the authentication looked good */
1590 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1594 /* we don't know anything about this return value */
1595 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (ret = %d, user = '%s')",
1597 ret = PAM_SERVICE_ERR;
1601 /* should not be reached */
1607 iniparser_freedict(d);
1610 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", pamh, ctrl, ret);
1616 int pam_sm_open_session(pam_handle_t *pamh, int flags,
1617 int argc, const char **argv)
1619 int ret = PAM_SYSTEM_ERR;
1620 dictionary *d = NULL;
1622 /* parse arguments */
1623 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1625 ret = PAM_SYSTEM_ERR;
1629 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", pamh, ctrl, flags);
1635 iniparser_freedict(d);
1638 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", pamh, ctrl, ret);
1644 int pam_sm_close_session(pam_handle_t *pamh, int flags,
1645 int argc, const char **argv)
1647 dictionary *d = NULL;
1648 int retval = PAM_SUCCESS;
1650 /* parse arguments */
1651 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1653 retval = PAM_SYSTEM_ERR;
1657 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", pamh, ctrl, flags);
1659 if (!(flags & PAM_DELETE_CRED)) {
1660 retval = PAM_SUCCESS;
1664 if (ctrl & WINBIND_KRB5_AUTH) {
1666 /* destroy the ccache here */
1667 struct winbindd_request request;
1668 struct winbindd_response response;
1670 const char *ccname = NULL;
1671 struct passwd *pwd = NULL;
1673 ZERO_STRUCT(request);
1674 ZERO_STRUCT(response);
1676 retval = pam_get_user(pamh, &user, "Username: ");
1678 _pam_log(pamh, ctrl, LOG_ERR, "could not identify user");
1683 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1684 retval = PAM_USER_UNKNOWN;
1688 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
1690 ccname = pam_getenv(pamh, "KRB5CCNAME");
1691 if (ccname == NULL) {
1692 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1695 strncpy(request.data.logoff.user, user,
1696 sizeof(request.data.logoff.user) - 1);
1699 strncpy(request.data.logoff.krb5ccname, ccname,
1700 sizeof(request.data.logoff.krb5ccname) - 1);
1703 pwd = getpwnam(user);
1705 retval = PAM_USER_UNKNOWN;
1708 request.data.logoff.uid = pwd->pw_uid;
1710 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1712 retval = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1717 iniparser_freedict(d);
1720 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", pamh, ctrl, retval);
1728 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1729 int argc, const char **argv)
1735 /* <DO NOT free() THESE> */
1737 char *pass_old, *pass_new;
1738 /* </DO NOT free() THESE> */
1743 dictionary *d = NULL;
1745 ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1747 ret = PAM_SYSTEM_ERR;
1751 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", pamh, ctrl, flags);
1753 /* clearing offline bit for the auth in the password change */
1754 ctrl &= ~WINBIND_CACHED_LOGIN;
1757 * First get the name of a user
1759 ret = pam_get_user(pamh, &user, "Username: ");
1761 _pam_log(pamh, ctrl, LOG_ERR,
1762 "password - could not identify user");
1767 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1768 ret = PAM_USER_UNKNOWN;
1772 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
1774 /* check if this is really a user in winbindd, not only in NSS */
1775 ret = valid_user(pamh, ctrl, user);
1778 ret = PAM_USER_UNKNOWN;
1781 ret = PAM_SYSTEM_ERR;
1788 * obtain and verify the current password (OLDAUTHTOK) for
1792 if (flags & PAM_PRELIM_CHECK) {
1793 struct winbindd_response response;
1794 time_t pwdlastset_prelim = 0;
1796 /* instruct user what is happening */
1797 #define greeting "Changing password for "
1798 Announce = (char *) malloc(sizeof(greeting) + strlen(user));
1799 if (Announce == NULL) {
1800 _pam_log(pamh, ctrl, LOG_CRIT, "password - out of memory");
1804 (void) strcpy(Announce, greeting);
1805 (void) strcpy(Announce + sizeof(greeting) - 1, user);
1808 lctrl = ctrl | WINBIND__OLD_PASSWORD;
1809 ret = _winbind_read_password(pamh, lctrl,
1811 "(current) NT password: ",
1813 (const char **) &pass_old);
1814 if (ret != PAM_SUCCESS) {
1815 _pam_log(pamh, ctrl, LOG_NOTICE, "password - (old) token not obtained");
1819 /* We don't need krb5 env set for password change test. */
1820 ctrl &= ~WINBIND_KRB5_AUTH;
1822 /* verify that this is the password for this user */
1824 ret = winbind_auth_request(pamh, ctrl, user, pass_old,
1825 NULL, NULL, &response, &pwdlastset_prelim, NULL);
1827 if (ret != PAM_ACCT_EXPIRED &&
1828 ret != PAM_AUTHTOK_EXPIRED &&
1829 ret != PAM_NEW_AUTHTOK_REQD &&
1830 ret != PAM_SUCCESS) {
1833 iniparser_freedict(d);
1835 /* Deal with offline errors. */
1836 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl,
1838 "NT_STATUS_NO_LOGON_SERVERS");
1839 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl,
1841 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1842 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl,
1844 "NT_STATUS_ACCESS_DENIED");
1848 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET, (void *)pwdlastset_prelim, NULL);
1850 ret = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
1852 if (ret != PAM_SUCCESS) {
1853 _pam_log(pamh, ctrl, LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
1855 } else if (flags & PAM_UPDATE_AUTHTOK) {
1857 time_t pwdlastset_update = 0;
1860 * obtain the proposed password
1864 * get the old token back.
1867 ret = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
1869 if (ret != PAM_SUCCESS) {
1870 _pam_log(pamh, ctrl, LOG_NOTICE, "user not authenticated");
1874 lctrl = ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
1876 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
1877 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
1880 ret = PAM_AUTHTOK_ERR;
1881 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
1883 * use_authtok is to force the use of a previously entered
1884 * password -- needed for pluggable password strength checking
1887 ret = _winbind_read_password(pamh, lctrl,
1889 "Enter new NT password: ",
1890 "Retype new NT password: ",
1891 (const char **) &pass_new);
1893 if (ret != PAM_SUCCESS) {
1894 _pam_log_debug(pamh, ctrl, LOG_ALERT
1895 ,"password - new password not obtained");
1896 pass_old = NULL;/* tidy up */
1901 * At this point we know who the user is and what they
1902 * propose as their new password. Verify that the new
1903 * password is acceptable.
1906 if (pass_new[0] == '\0') {/* "\0" password = NULL */
1912 * By reaching here we have approved the passwords and must now
1913 * rebuild the password database file.
1915 _pam_get_data( pamh, PAM_WINBIND_PWD_LAST_SET,
1916 &pwdlastset_update);
1918 ret = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new, pwdlastset_update);
1920 _pam_overwrite(pass_new);
1921 _pam_overwrite(pass_old);
1922 pass_old = pass_new = NULL;
1926 /* just in case we need krb5 creds after a password change over msrpc */
1928 if (ctrl & WINBIND_KRB5_AUTH) {
1929 struct winbindd_response response;
1931 const char *member = get_member_from_config(pamh, argc, argv, ctrl, d);
1932 const char *cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1934 ret = winbind_auth_request(pamh, ctrl, user, pass_new,
1935 member, cctype, &response, NULL, NULL);
1936 _pam_overwrite(pass_new);
1937 _pam_overwrite(pass_old);
1938 pass_old = pass_new = NULL;
1940 iniparser_freedict(d);
1942 /* Deal with offline errors. */
1943 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl,
1945 "NT_STATUS_NO_LOGON_SERVERS");
1946 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl,
1948 "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1949 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl,
1951 "NT_STATUS_ACCESS_DENIED");
1955 ret = PAM_SERVICE_ERR;
1960 iniparser_freedict(d);
1963 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", pamh, ctrl, ret);
1970 /* static module data */
1972 struct pam_module _pam_winbind_modstruct = {
1974 pam_sm_authenticate,
1977 pam_sm_open_session,
1978 pam_sm_close_session,
1985 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
1986 * Copyright (c) Tim Potter <tpot@samba.org> 2000
1987 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1988 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2007
1989 * Copyright (c) Jan Rêkorajski 1999.
1990 * Copyright (c) Andrew G. Morgan 1996-8.
1991 * Copyright (c) Alex O. Yuriev, 1996.
1992 * Copyright (c) Cristian Gafton 1996.
1993 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
1995 * Redistribution and use in source and binary forms, with or without
1996 * modification, are permitted provided that the following conditions
1998 * 1. Redistributions of source code must retain the above copyright
1999 * notice, and the entire permission notice in its entirety,
2000 * including the disclaimer of warranties.
2001 * 2. Redistributions in binary form must reproduce the above copyright
2002 * notice, this list of conditions and the following disclaimer in the
2003 * documentation and/or other materials provided with the distribution.
2004 * 3. The name of the author may not be used to endorse or promote
2005 * products derived from this software without specific prior
2006 * written permission.
2008 * ALTERNATIVELY, this product may be distributed under the terms of
2009 * the GNU Public License, in which case the provisions of the GPL are
2010 * required INSTEAD OF the above restrictions. (This clause is
2011 * necessary due to a potential bad interaction between the GPL and
2012 * the restrictions contained in a BSD-style copyright.)
2014 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
2015 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2016 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2017 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2018 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2019 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2020 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2021 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2022 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2023 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2024 * OF THE POSSIBILITY OF SUCH DAMAGE.