VERSION: Raise version number up to 3.0.36.
[Samba.git] / source / nsswitch / pam_winbind.c
blob6ee80d0f6f36a0073b1220fbf40096fa4899c7c5
1 /* pam_winbind module
3 Copyright Andrew Tridgell <tridge@samba.org> 2000
4 Copyright Tim Potter <tpot@samba.org> 2000
5 Copyright Andrew Bartlett <abartlet@samba.org> 2002
6 Copyright Guenther Deschner <gd@samba.org> 2005-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) \
16 do { \
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); \
19 } while (0)
21 #define _PAM_LOG_FUNCTION_LEAVE(function, pamh, ctrl, retval) \
22 do { \
23 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] LEAVE: " function " returning %d", (uint32) pamh, retval); \
24 _pam_log_state(pamh, ctrl); \
25 } while (0)
27 /* data tokens */
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,
36 const void *_item)
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);
48 /* some syslogging */
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);
55 #else
56 static void _pam_log_int(const pam_handle_t *pamh, int err, const char *format, va_list args)
58 char *format2 = NULL;
59 const char *service;
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);
67 return;
70 sprintf(format2, "%s(%s): %s", MODULE_NAME, service, format);
71 vsyslog(err, format2, args);
72 SAFE_FREE(format2);
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, ...) PRINTF_ATTRIBUTE(4,5);
82 static void _pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
84 va_list args;
86 if (_pam_log_is_silent(ctrl)) {
87 return;
90 va_start(args, format);
91 _pam_log_int(pamh, err, format, args);
92 va_end(args);
95 static BOOL _pam_log_is_debug_enabled(int ctrl)
97 if (ctrl == -1) {
98 return False;
101 if (_pam_log_is_silent(ctrl)) {
102 return False;
105 if (!(ctrl & WINBIND_DEBUG_ARG)) {
106 return False;
109 return True;
112 static BOOL _pam_log_is_debug_state_enabled(int ctrl)
114 if (!(ctrl & WINBIND_DEBUG_STATE)) {
115 return False;
118 return _pam_log_is_debug_enabled(ctrl);
121 static void _pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
122 static void _pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
124 va_list args;
126 if (!_pam_log_is_debug_enabled(ctrl)) {
127 return;
130 va_start(args, format);
131 _pam_log_int(pamh, err, format, args);
132 va_end(args);
135 static void _pam_log_state_datum(const pam_handle_t *pamh, int ctrl, int item_type, const char *key, int is_string)
137 const void *data = NULL;
138 if (item_type != 0) {
139 pam_get_item(pamh, item_type, &data);
140 } else {
141 pam_get_data(pamh, key, &data);
143 if (data != NULL) {
144 const char *type = (item_type != 0) ? "ITEM" : "DATA";
145 if (is_string != 0) {
146 _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);
147 } else {
148 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "[pamh: 0x%08x] STATE: %s(%s) = 0x%08x", (uint32) pamh, type, key, (uint32) data);
153 #define _PAM_LOG_STATE_DATA_POINTER(pamh, ctrl, module_data_name) \
154 _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 0)
156 #define _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, module_data_name) \
157 _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 1)
159 #define _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, item_type) \
160 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 0)
162 #define _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, item_type) \
163 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 1)
165 #ifdef DEBUG_PASSWORD
166 #define _LOG_PASSWORD_AS_STRING 1
167 #else
168 #define _LOG_PASSWORD_AS_STRING 0
169 #endif
171 #define _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, item_type) \
172 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, _LOG_PASSWORD_AS_STRING)
174 static void _pam_log_state(const pam_handle_t *pamh, int ctrl)
176 if (!_pam_log_is_debug_state_enabled(ctrl)) {
177 return;
180 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_SERVICE);
181 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_USER);
182 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_TTY);
183 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_RHOST);
184 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_RUSER);
185 _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, PAM_OLDAUTHTOK);
186 _PAM_LOG_STATE_ITEM_PASSWORD(pamh, ctrl, PAM_AUTHTOK);
187 _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, PAM_USER_PROMPT);
188 _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_CONV);
189 #ifdef PAM_FAIL_DELAY
190 _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_FAIL_DELAY);
191 #endif
192 #ifdef PAM_REPOSITORY
193 _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, PAM_REPOSITORY);
194 #endif
196 _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_HOMEDIR);
197 _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_LOGONSCRIPT);
198 _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_LOGONSERVER);
199 _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_PROFILEPATH);
200 _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_NEW_AUTHTOK_REQD); /* Use atoi to get PAM result code */
201 _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH);
202 _PAM_LOG_STATE_DATA_POINTER(pamh, ctrl, PAM_WINBIND_PWD_LAST_SET);
205 static int _pam_parse(const pam_handle_t *pamh, int flags, int argc, const char **argv, dictionary **result_d)
207 int ctrl = 0;
208 const char *config_file = NULL;
209 int i;
210 const char **v;
211 dictionary *d = NULL;
213 if (flags & PAM_SILENT) {
214 ctrl |= WINBIND_SILENT;
217 for (i=argc,v=argv; i-- > 0; ++v) {
218 if (!strncasecmp(*v, "config", strlen("config"))) {
219 ctrl |= WINBIND_CONFIG_FILE;
220 config_file = v[i];
221 break;
225 if (config_file == NULL) {
226 config_file = PAM_WINBIND_CONFIG_FILE;
229 d = iniparser_load(config_file);
230 if (d == NULL) {
231 goto config_from_pam;
234 if (iniparser_getboolean(d, "global:debug", False)) {
235 ctrl |= WINBIND_DEBUG_ARG;
238 if (iniparser_getboolean(d, "global:debug_state", False)) {
239 ctrl |= WINBIND_DEBUG_STATE;
242 if (iniparser_getboolean(d, "global:cached_login", False)) {
243 ctrl |= WINBIND_CACHED_LOGIN;
246 if (iniparser_getboolean(d, "global:krb5_auth", False)) {
247 ctrl |= WINBIND_KRB5_AUTH;
250 if (iniparser_getboolean(d, "global:silent", False)) {
251 ctrl |= WINBIND_SILENT;
254 if (iniparser_getstr(d, "global:krb5_ccache_type") != NULL) {
255 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
258 if ((iniparser_getstr(d, "global:require-membership-of") != NULL) ||
259 (iniparser_getstr(d, "global:require_membership_of") != NULL)) {
260 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
263 if (iniparser_getboolean(d, "global:try_first_pass", False)) {
264 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
267 config_from_pam:
268 /* step through arguments */
269 for (i=argc,v=argv; i-- > 0; ++v) {
271 /* generic options */
272 if (!strcmp(*v,"debug"))
273 ctrl |= WINBIND_DEBUG_ARG;
274 else if (!strcasecmp(*v, "debug_state"))
275 ctrl |= WINBIND_DEBUG_STATE;
276 else if (!strcasecmp(*v, "use_authtok"))
277 ctrl |= WINBIND_USE_AUTHTOK_ARG;
278 else if (!strcasecmp(*v, "use_first_pass"))
279 ctrl |= WINBIND_USE_FIRST_PASS_ARG;
280 else if (!strcasecmp(*v, "try_first_pass"))
281 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
282 else if (!strcasecmp(*v, "unknown_ok"))
283 ctrl |= WINBIND_UNKNOWN_OK_ARG;
284 else if (!strncasecmp(*v, "require_membership_of", strlen("require_membership_of")))
285 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
286 else if (!strncasecmp(*v, "require-membership-of", strlen("require-membership-of")))
287 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
288 else if (!strcasecmp(*v, "krb5_auth"))
289 ctrl |= WINBIND_KRB5_AUTH;
290 else if (!strncasecmp(*v, "krb5_ccache_type", strlen("krb5_ccache_type")))
291 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
292 else if (!strcasecmp(*v, "cached_login"))
293 ctrl |= WINBIND_CACHED_LOGIN;
294 else {
295 _pam_log(pamh, ctrl, LOG_ERR, "pam_parse: unknown option: %s", *v);
296 return -1;
301 if (result_d) {
302 *result_d = d;
303 } else {
304 if (d) {
305 iniparser_freedict(d);
309 return ctrl;
312 static void _pam_winbind_cleanup_func(pam_handle_t *pamh, void *data, int error_status)
314 int ctrl = _pam_parse(pamh, 0, 0, NULL, NULL);
315 if (_pam_log_is_debug_state_enabled(ctrl)) {
316 _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 SAFE_FREE(data);
322 static const struct ntstatus_errors {
323 const char *ntstatus_string;
324 const char *error_string;
325 } ntstatus_errors[] = {
326 {"NT_STATUS_OK", "Success"},
327 {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
328 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND", "No domain controllers found"},
329 {"NT_STATUS_NO_LOGON_SERVERS", "No logon servers"},
330 {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
331 {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
332 {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
333 {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
334 {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
335 {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
336 {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
337 {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
338 {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
339 {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
340 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
341 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
342 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
343 {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
344 {NULL, NULL}
347 const char *_get_ntstatus_error_string(const char *nt_status_string)
349 int i;
350 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
351 if (!strcasecmp(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
352 return ntstatus_errors[i].error_string;
355 return NULL;
358 /* --- authentication management functions --- */
360 /* Attempt a conversation */
362 static int converse(pam_handle_t *pamh, int nargs,
363 struct pam_message **message,
364 struct pam_response **response)
366 int retval;
367 struct pam_conv *conv;
369 retval = _pam_get_item(pamh, PAM_CONV, &conv );
370 if (retval == PAM_SUCCESS) {
371 retval = conv->conv(nargs, (const struct pam_message **)message,
372 response, conv->appdata_ptr);
375 return retval; /* propagate error status */
379 static int _make_remark(pam_handle_t * pamh, int flags, int type, const char *text)
381 int retval = PAM_SUCCESS;
383 struct pam_message *pmsg[1], msg[1];
384 struct pam_response *resp;
386 if (flags & WINBIND_SILENT) {
387 return PAM_SUCCESS;
390 pmsg[0] = &msg[0];
391 msg[0].msg = CONST_DISCARD(char *, text);
392 msg[0].msg_style = type;
394 resp = NULL;
395 retval = converse(pamh, 1, pmsg, &resp);
397 if (resp) {
398 _pam_drop_reply(resp, 1);
400 return retval;
403 static int _make_remark_v(pam_handle_t * pamh, int flags, int type, const char *format, va_list args)
405 char *var;
406 int ret;
408 ret = vasprintf(&var, format, args);
409 if (ret < 0) {
410 _pam_log(pamh, 0, LOG_ERR, "memory allocation failure");
411 return ret;
414 ret = _make_remark(pamh, flags, type, var);
415 SAFE_FREE(var);
416 return ret;
419 static int _make_remark_format(pam_handle_t * pamh, int flags, int type, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
420 static int _make_remark_format(pam_handle_t * pamh, int flags, int type, const char *format, ...)
422 int ret;
423 va_list args;
425 va_start(args, format);
426 ret = _make_remark_v(pamh, flags, type, format, args);
427 va_end(args);
428 return ret;
431 static int pam_winbind_request(pam_handle_t * pamh, int ctrl,
432 enum winbindd_cmd req_type,
433 struct winbindd_request *request,
434 struct winbindd_response *response)
436 /* Fill in request and send down pipe */
437 init_request(request, req_type);
439 if (write_sock(request, sizeof(*request), 0, 0) == -1) {
440 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: write to socket failed!");
441 close_sock();
442 return PAM_SERVICE_ERR;
445 /* Wait for reply */
446 if (read_reply(response) == -1) {
447 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: read from socket failed!");
448 close_sock();
449 return PAM_SERVICE_ERR;
452 /* We are done with the socket - close it and avoid mischeif */
453 close_sock();
455 /* Copy reply data from socket */
456 if (response->result == WINBINDD_OK) {
457 return PAM_SUCCESS;
460 /* no need to check for pam_error codes for getpwnam() */
461 switch (req_type) {
463 case WINBINDD_GETPWNAM:
464 case WINBINDD_LOOKUPNAME:
465 if (strlen(response->data.auth.nt_status_string) > 0) {
466 _pam_log(pamh, ctrl, LOG_ERR, "request failed, NT error was %s",
467 response->data.auth.nt_status_string);
468 } else {
469 _pam_log(pamh, ctrl, LOG_ERR, "request failed");
471 return PAM_USER_UNKNOWN;
472 default:
473 break;
476 if (response->data.auth.pam_error != PAM_SUCCESS) {
477 _pam_log(pamh, ctrl, LOG_ERR, "request failed: %s, PAM error was %s (%d), NT error was %s",
478 response->data.auth.error_string,
479 pam_strerror(pamh, response->data.auth.pam_error),
480 response->data.auth.pam_error,
481 response->data.auth.nt_status_string);
482 return response->data.auth.pam_error;
485 _pam_log(pamh, ctrl, LOG_ERR, "request failed, but PAM error 0!");
487 return PAM_SERVICE_ERR;
490 static int pam_winbind_request_log(pam_handle_t * pamh,
491 int ctrl,
492 enum winbindd_cmd req_type,
493 struct winbindd_request *request,
494 struct winbindd_response *response,
495 const char *user)
497 int retval;
499 retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
501 switch (retval) {
502 case PAM_AUTH_ERR:
503 /* incorrect password */
504 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' denied access (incorrect password or invalid membership)", user);
505 return retval;
506 case PAM_ACCT_EXPIRED:
507 /* account expired */
508 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' account expired", user);
509 return retval;
510 case PAM_AUTHTOK_EXPIRED:
511 /* password expired */
512 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' password expired", user);
513 return retval;
514 case PAM_NEW_AUTHTOK_REQD:
515 /* new password required */
516 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' new password required", user);
517 return retval;
518 case PAM_USER_UNKNOWN:
519 /* the user does not exist */
520 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", user);
521 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
522 return PAM_IGNORE;
524 return retval;
525 case PAM_SUCCESS:
526 /* Otherwise, the authentication looked good */
527 switch (req_type) {
528 case WINBINDD_INFO:
529 break;
530 case WINBINDD_PAM_AUTH:
531 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", user);
532 break;
533 case WINBINDD_PAM_CHAUTHTOK:
534 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' password changed", user);
535 break;
536 default:
537 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' OK", user);
538 break;
541 return retval;
542 default:
543 /* we don't know anything about this return value */
544 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (retval = %d, user = '%s')",
545 retval, user);
546 return retval;
551 * send a password expiry message if required
553 * @param pamh PAM handle
554 * @param ctrl PAM winbind options.
555 * @param next_change expected (calculated) next expiry date.
556 * @param already_expired pointer to a boolean to indicate if the password is
557 * already expired.
559 * @return boolean Returns True if message has been sent, False if not.
562 static BOOL _pam_send_password_expiry_message(pam_handle_t *pamh, int ctrl, time_t next_change, time_t now, BOOL *already_expired)
564 int days = 0;
565 struct tm tm_now, tm_next_change;
567 if (already_expired) {
568 *already_expired = False;
571 if (next_change <= now) {
572 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PASSWORD_EXPIRED");
573 if (already_expired) {
574 *already_expired = True;
576 return True;
579 if ((next_change < 0) ||
580 (next_change > now + DAYS_TO_WARN_BEFORE_PWD_EXPIRES * SECONDS_PER_DAY)) {
581 return False;
584 if ((localtime_r(&now, &tm_now) == NULL) ||
585 (localtime_r(&next_change, &tm_next_change) == NULL)) {
586 return False;
589 days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) - (tm_now.tm_yday+tm_now.tm_year*365);
591 if (days == 0) {
592 _make_remark(pamh, ctrl, PAM_TEXT_INFO, "Your password expires today");
593 return True;
596 if (days > 0 && days < DAYS_TO_WARN_BEFORE_PWD_EXPIRES) {
597 _make_remark_format(pamh, ctrl, PAM_TEXT_INFO, "Your password will expire in %d %s",
598 days, (days > 1) ? "days":"day");
599 return True;
602 return False;
606 * Send a warning if the password expires in the near future
608 * @param pamh PAM handle
609 * @param ctrl PAM winbind options.
610 * @param response The full authentication response structure.
611 * @param already_expired boolean, is the pwd already expired?
613 * @return void.
616 static void _pam_warn_password_expiry(pam_handle_t *pamh,
617 int flags,
618 const struct winbindd_response *response,
619 BOOL *already_expired)
621 time_t now = time(NULL);
622 time_t next_change = 0;
624 if (already_expired) {
625 *already_expired = False;
628 /* accounts with ACB_PWNOEXP set never receive a warning */
629 if (response->data.auth.info3.acct_flags & ACB_PWNOEXP) {
630 return;
633 /* no point in sending a warning if this is a grace logon */
634 if (PAM_WB_GRACE_LOGON(response->data.auth.info3.user_flgs)) {
635 return;
638 /* check if the info3 must change timestamp has been set */
639 next_change = response->data.auth.info3.pass_must_change_time;
641 if (_pam_send_password_expiry_message(pamh, flags, next_change, now,
642 already_expired)) {
643 return;
646 /* now check for the global password policy */
647 /* good catch from Ralf Haferkamp: an expiry of "never" is translated
648 * to -1 */
649 if (response->data.auth.policy.expire <= 0) {
650 return;
653 next_change = response->data.auth.info3.pass_last_set_time +
654 response->data.auth.policy.expire;
656 if (_pam_send_password_expiry_message(pamh, flags, next_change, now,
657 already_expired)) {
658 return;
661 /* no warning sent */
664 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
666 static BOOL safe_append_string(char *dest,
667 const char *src,
668 int dest_buffer_size)
670 * Append a string, making sure not to overflow and to always return a NULL-terminated
671 * string.
673 * @param dest Destination string buffer (must already be NULL-terminated).
674 * @param src Source string buffer.
675 * @param dest_buffer_size Size of dest buffer in bytes.
677 * @return False if dest buffer is not big enough (no bytes copied), True on success.
680 int dest_length = strlen(dest);
681 int src_length = strlen(src);
683 if ( dest_length + src_length + 1 > dest_buffer_size ) {
684 return False;
687 memcpy(dest + dest_length, src, src_length + 1);
688 return True;
691 static BOOL winbind_name_to_sid_string(pam_handle_t *pamh,
692 int ctrl,
693 const char *user,
694 const char *name,
695 char *sid_list_buffer,
696 int sid_list_buffer_size)
698 * Convert a names into a SID string, appending it to a buffer.
700 * @param pamh PAM handle
701 * @param ctrl PAM winbind options.
702 * @param user User in PAM request.
703 * @param name Name to convert.
704 * @param sid_list_buffer Where to append the string sid.
705 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
707 * @return False on failure, True on success.
710 const char* sid_string;
711 struct winbindd_response sid_response;
713 /* lookup name? */
714 if (IS_SID_STRING(name)) {
715 sid_string = name;
716 } else {
717 struct winbindd_request sid_request;
719 ZERO_STRUCT(sid_request);
720 ZERO_STRUCT(sid_response);
722 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", name);
724 /* fortunatly winbindd can handle non-separated names */
725 strncpy(sid_request.data.name.name, name,
726 sizeof(sid_request.data.name.name) - 1);
728 if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME, &sid_request, &sid_response, user)) {
729 _pam_log(pamh, ctrl, LOG_INFO, "could not lookup name: %s\n", name);
730 return False;
733 sid_string = sid_response.data.sid.sid;
736 if (!safe_append_string(sid_list_buffer, sid_string, sid_list_buffer_size)) {
737 return False;
740 return True;
743 static BOOL winbind_name_list_to_sid_string_list(pam_handle_t *pamh,
744 int ctrl,
745 const char *user,
746 const char *name_list,
747 char *sid_list_buffer,
748 int sid_list_buffer_size)
750 * Convert a list of names into a list of sids.
752 * @param pamh PAM handle
753 * @param ctrl PAM winbind options.
754 * @param user User in PAM request.
755 * @param name_list List of names or string sids, separated by commas.
756 * @param sid_list_buffer Where to put the list of string sids.
757 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
759 * @return False on failure, True on success.
762 BOOL result = False;
763 char *current_name = NULL;
764 const char *search_location;
765 const char *comma;
767 if ( sid_list_buffer_size > 0 ) {
768 sid_list_buffer[0] = 0;
771 search_location = name_list;
772 while ( (comma = strstr(search_location, ",")) != NULL ) {
773 current_name = strndup(search_location, comma - search_location);
774 if (NULL == current_name) {
775 goto out;
778 if (!winbind_name_to_sid_string(pamh, ctrl, user, current_name, sid_list_buffer, sid_list_buffer_size)) {
779 goto out;
782 SAFE_FREE(current_name);
784 if (!safe_append_string(sid_list_buffer, ",", sid_list_buffer_size)) {
785 goto out;
788 search_location = comma + 1;
791 if (!winbind_name_to_sid_string(pamh, ctrl, user, search_location, sid_list_buffer, sid_list_buffer_size)) {
792 goto out;
795 result = True;
797 out:
798 SAFE_FREE(current_name);
799 return result;
803 * put krb5ccname variable into environment
805 * @param pamh PAM handle
806 * @param ctrl PAM winbind options.
807 * @param krb5ccname env variable retrieved from winbindd.
809 * @return void.
812 static void _pam_setup_krb5_env(pam_handle_t *pamh, int ctrl, const char *krb5ccname)
814 char var[PATH_MAX];
815 int ret;
817 if (off(ctrl, WINBIND_KRB5_AUTH)) {
818 return;
821 if (!krb5ccname || (strlen(krb5ccname) == 0)) {
822 return;
825 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "request returned KRB5CCNAME: %s", krb5ccname);
827 if (snprintf(var, sizeof(var), "KRB5CCNAME=%s", krb5ccname) == -1) {
828 return;
831 ret = pam_putenv(pamh, var);
832 if (ret) {
833 _pam_log(pamh, ctrl, LOG_ERR, "failed to set KRB5CCNAME to %s: %s",
834 var, pam_strerror(pamh, ret));
839 * Set string into the PAM stack.
841 * @param pamh PAM handle
842 * @param ctrl PAM winbind options.
843 * @param data_name Key name for pam_set_data.
844 * @param value String value.
846 * @return void.
849 static void _pam_set_data_string(pam_handle_t *pamh, int ctrl, const char *data_name, const char *value)
851 int ret;
853 if ( !data_name || !value || (strlen(data_name) == 0) || (strlen(value) == 0) ) {
854 return;
857 ret = pam_set_data(pamh, data_name, (void *)strdup(value), _pam_winbind_cleanup_func);
858 if (ret) {
859 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data %s: %s\n",
860 data_name, pam_strerror(pamh, ret));
866 * Set info3 strings into the PAM stack.
868 * @param pamh PAM handle
869 * @param ctrl PAM winbind options.
870 * @param data_name Key name for pam_set_data.
871 * @param value String value.
873 * @return void.
876 static void _pam_set_data_info3(pam_handle_t *pamh, int ctrl, struct winbindd_response *response)
878 _pam_set_data_string(pamh, ctrl, PAM_WINBIND_HOMEDIR, response->data.auth.info3.home_dir);
879 _pam_set_data_string(pamh, ctrl, PAM_WINBIND_LOGONSCRIPT, response->data.auth.info3.logon_script);
880 _pam_set_data_string(pamh, ctrl, PAM_WINBIND_LOGONSERVER, response->data.auth.info3.logon_srv);
881 _pam_set_data_string(pamh, ctrl, PAM_WINBIND_PROFILEPATH, response->data.auth.info3.profile_path);
885 * Free info3 strings in the PAM stack.
887 * @param pamh PAM handle
889 * @return void.
892 static void _pam_free_data_info3(pam_handle_t *pamh)
894 pam_set_data(pamh, PAM_WINBIND_HOMEDIR, NULL, NULL);
895 pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT, NULL, NULL);
896 pam_set_data(pamh, PAM_WINBIND_LOGONSERVER, NULL, NULL);
897 pam_set_data(pamh, PAM_WINBIND_PROFILEPATH, NULL, NULL);
901 * Send PAM_ERROR_MSG for cached or grace logons.
903 * @param pamh PAM handle
904 * @param ctrl PAM winbind options.
905 * @param username User in PAM request.
906 * @param info3_user_flgs Info3 flags containing logon type bits.
908 * @return void.
911 static void _pam_warn_logon_type(pam_handle_t *pamh, int ctrl, const char *username, uint32 info3_user_flgs)
913 /* inform about logon type */
914 if (PAM_WB_GRACE_LOGON(info3_user_flgs)) {
916 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
917 "Grace login. Please change your password as soon you're online again");
918 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
919 "User %s logged on using grace logon\n", username);
921 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs)) {
923 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
924 "Domain Controller unreachable, using cached credentials instead. Network resources may be unavailable");
925 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
926 "User %s logged on using cached credentials\n", username);
931 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
933 * @param response The struct winbindd_response.
935 * @return string (caller needs to free).
938 static char *_pam_compose_pwd_restriction_string(struct winbindd_response *response)
940 char *str = NULL;
941 size_t offset = 0, ret = 0, str_size = 1024;
943 str = (char *)malloc(str_size);
944 if (!str) {
945 return NULL;
948 memset(str, '\0', str_size);
950 offset = snprintf(str, str_size, "Your password ");
951 if (offset == -1) {
952 goto failed;
955 if (response->data.auth.policy.min_length_password > 0) {
956 ret = snprintf(str+offset, str_size-offset,
957 "must be at least %d characters; ",
958 response->data.auth.policy.min_length_password);
959 if (ret == -1) {
960 goto failed;
962 offset += ret;
965 if (response->data.auth.policy.password_history > 0) {
966 ret = snprintf(str+offset, str_size-offset,
967 "cannot repeat any of your previous %d passwords; ",
968 response->data.auth.policy.password_history);
969 if (ret == -1) {
970 goto failed;
972 offset += ret;
975 if (response->data.auth.policy.password_properties & DOMAIN_PASSWORD_COMPLEX) {
976 ret = snprintf(str+offset, str_size-offset,
977 "must contain capitals, numerals or punctuation; "
978 "and cannot contain your account or full name; ");
979 if (ret == -1) {
980 goto failed;
982 offset += ret;
985 ret = snprintf(str+offset, str_size-offset,
986 "Please type a different password. "
987 "Type a password which meets these requirements in both text boxes.");
988 if (ret == -1) {
989 goto failed;
992 return str;
994 failed:
995 SAFE_FREE(str);
996 return NULL;
999 /* talk to winbindd */
1000 static int winbind_auth_request(pam_handle_t * pamh,
1001 int ctrl,
1002 const char *user,
1003 const char *pass,
1004 const char *member,
1005 const char *cctype,
1006 struct winbindd_response *p_response,
1007 time_t *pwd_last_set,
1008 char **user_ret)
1010 struct winbindd_request request;
1011 struct winbindd_response response;
1012 int ret;
1013 BOOL already_expired = False;
1015 ZERO_STRUCT(request);
1016 ZERO_STRUCT(response);
1018 if (pwd_last_set) {
1019 *pwd_last_set = 0;
1022 strncpy(request.data.auth.user, user,
1023 sizeof(request.data.auth.user)-1);
1025 strncpy(request.data.auth.pass, pass,
1026 sizeof(request.data.auth.pass)-1);
1028 request.data.auth.krb5_cc_type[0] = '\0';
1029 request.data.auth.uid = -1;
1031 request.flags = WBFLAG_PAM_INFO3_TEXT |
1032 WBFLAG_PAM_GET_PWD_POLICY |
1033 WBFLAG_PAM_CONTACT_TRUSTDOM;
1035 if (ctrl & (WINBIND_KRB5_AUTH|WINBIND_CACHED_LOGIN)) {
1036 struct passwd *pwd = NULL;
1038 pwd = getpwnam(user);
1039 if (pwd == NULL) {
1040 return PAM_USER_UNKNOWN;
1042 request.data.auth.uid = pwd->pw_uid;
1045 if (ctrl & WINBIND_KRB5_AUTH) {
1047 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling krb5 login flag\n");
1049 request.flags |= WBFLAG_PAM_KRB5 | WBFLAG_PAM_FALLBACK_AFTER_KRB5;
1052 if (ctrl & WINBIND_CACHED_LOGIN) {
1053 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling cached login flag\n");
1054 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
1057 if (user_ret) {
1058 *user_ret = NULL;
1059 request.flags |= WBFLAG_PAM_UNIX_NAME;
1062 if (cctype != NULL) {
1063 strncpy(request.data.auth.krb5_cc_type, cctype,
1064 sizeof(request.data.auth.krb5_cc_type) - 1);
1065 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling request for a %s krb5 ccache\n", cctype);
1068 request.data.auth.require_membership_of_sid[0] = '\0';
1070 if (member != NULL) {
1072 if (!winbind_name_list_to_sid_string_list(pamh, ctrl, user, member,
1073 request.data.auth.require_membership_of_sid,
1074 sizeof(request.data.auth.require_membership_of_sid))) {
1076 _pam_log_debug(pamh, ctrl, LOG_ERR, "failed to serialize membership of sid \"%s\"\n", member);
1077 return PAM_AUTH_ERR;
1081 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
1083 if (pwd_last_set) {
1084 *pwd_last_set = response.data.auth.info3.pass_last_set_time;
1087 if (p_response) {
1088 /* We want to process the response in the caller. */
1089 *p_response = response;
1090 return ret;
1093 if (ret) {
1094 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PASSWORD_EXPIRED");
1095 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PASSWORD_MUST_CHANGE");
1096 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_INVALID_WORKSTATION");
1097 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_INVALID_LOGON_HOURS");
1098 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCOUNT_EXPIRED");
1099 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCOUNT_DISABLED");
1100 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCOUNT_LOCKED_OUT");
1101 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
1102 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
1103 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
1104 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1105 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NO_LOGON_SERVERS");
1106 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_WRONG_PASSWORD");
1107 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCESS_DENIED");
1110 if (ret == PAM_SUCCESS) {
1112 /* warn a user if the password is about to expire soon */
1113 _pam_warn_password_expiry(pamh, ctrl, &response, &already_expired);
1115 if (already_expired == True) {
1116 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Password has expired "
1117 "(Password was last set: %lld, the policy says "
1118 "it should expire here %lld (now it's: %lu))\n",
1119 response.data.auth.info3.pass_last_set_time,
1120 response.data.auth.info3.pass_last_set_time +
1121 response.data.auth.policy.expire,
1122 time(NULL));
1124 return PAM_AUTHTOK_EXPIRED;
1127 /* inform about logon type */
1128 _pam_warn_logon_type(pamh, ctrl, user, response.data.auth.info3.user_flgs);
1130 /* set some info3 info for other modules in the stack */
1131 _pam_set_data_info3(pamh, ctrl, &response);
1133 /* put krb5ccname into env */
1134 _pam_setup_krb5_env(pamh, ctrl, response.data.auth.krb5ccname);
1136 /* If winbindd returned a username, return the pointer to it here. */
1137 if (user_ret && response.extra_data.data) {
1138 /* We have to trust it's a null terminated string. */
1139 *user_ret = (char *)response.extra_data.data;
1143 return ret;
1146 /* talk to winbindd */
1147 static int winbind_chauthtok_request(pam_handle_t * pamh,
1148 int ctrl,
1149 const char *user,
1150 const char *oldpass,
1151 const char *newpass,
1152 time_t pwd_last_set)
1154 struct winbindd_request request;
1155 struct winbindd_response response;
1156 int ret;
1158 ZERO_STRUCT(request);
1159 ZERO_STRUCT(response);
1161 if (request.data.chauthtok.user == NULL) return -2;
1163 strncpy(request.data.chauthtok.user, user,
1164 sizeof(request.data.chauthtok.user) - 1);
1166 if (oldpass != NULL) {
1167 strncpy(request.data.chauthtok.oldpass, oldpass,
1168 sizeof(request.data.chauthtok.oldpass) - 1);
1169 } else {
1170 request.data.chauthtok.oldpass[0] = '\0';
1173 if (newpass != NULL) {
1174 strncpy(request.data.chauthtok.newpass, newpass,
1175 sizeof(request.data.chauthtok.newpass) - 1);
1176 } else {
1177 request.data.chauthtok.newpass[0] = '\0';
1180 if (ctrl & WINBIND_KRB5_AUTH) {
1181 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1184 if (ctrl & WINBIND_CACHED_LOGIN) {
1185 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
1188 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
1190 if (ret == PAM_SUCCESS) {
1191 return ret;
1194 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_BACKUP_CONTROLLER");
1195 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1196 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NO_LOGON_SERVERS");
1197 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCESS_DENIED");
1199 /* TODO: tell the min pwd length ? */
1200 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_TOO_SHORT");
1202 /* TODO: tell the minage ? */
1203 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_TOO_RECENT");
1205 /* TODO: tell the history length ? */
1206 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
1208 if (!strcasecmp(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
1210 char *pwd_restriction_string = NULL;
1212 /* FIXME: avoid to send multiple PAM messages after another */
1213 switch (response.data.auth.reject_reason) {
1214 case -1:
1215 break;
1216 case REJECT_REASON_OTHER:
1217 if ((response.data.auth.policy.min_passwordage > 0) &&
1218 (pwd_last_set + response.data.auth.policy.min_passwordage > time(NULL))) {
1219 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PWD_TOO_RECENT");
1221 break;
1222 case REJECT_REASON_TOO_SHORT:
1223 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PWD_TOO_SHORT");
1224 break;
1225 case REJECT_REASON_IN_HISTORY:
1226 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PWD_HISTORY_CONFLICT");
1227 break;
1228 case REJECT_REASON_NOT_COMPLEX:
1229 _make_remark(pamh, ctrl, PAM_ERROR_MSG, "Password does not meet complexity requirements");
1230 break;
1231 default:
1232 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1233 "unknown password change reject reason: %d",
1234 response.data.auth.reject_reason);
1235 break;
1238 pwd_restriction_string = _pam_compose_pwd_restriction_string(&response);
1239 if (pwd_restriction_string) {
1240 _make_remark(pamh, ctrl, PAM_ERROR_MSG, pwd_restriction_string);
1241 SAFE_FREE(pwd_restriction_string);
1245 return ret;
1249 * Checks if a user has an account
1251 * return values:
1252 * 1 = User not found
1253 * 0 = OK
1254 * -1 = System error
1256 static int valid_user(pam_handle_t *pamh, int ctrl, const char *user)
1258 /* check not only if the user is available over NSS calls, also make
1259 * sure it's really a winbind user, this is important when stacking PAM
1260 * modules in the 'account' or 'password' facility. */
1262 struct passwd *pwd = NULL;
1263 struct winbindd_request request;
1264 struct winbindd_response response;
1265 int ret;
1267 ZERO_STRUCT(request);
1268 ZERO_STRUCT(response);
1270 pwd = getpwnam(user);
1271 if (pwd == NULL) {
1272 return 1;
1275 strncpy(request.data.username, user,
1276 sizeof(request.data.username) - 1);
1278 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_GETPWNAM, &request, &response, user);
1280 switch (ret) {
1281 case PAM_USER_UNKNOWN:
1282 return 1;
1283 case PAM_SUCCESS:
1284 return 0;
1285 default:
1286 break;
1288 return -1;
1291 static char *_pam_delete(register char *xx)
1293 _pam_overwrite(xx);
1294 _pam_drop(xx);
1295 return NULL;
1299 * obtain a password from the user
1302 static int _winbind_read_password(pam_handle_t * pamh,
1303 unsigned int ctrl,
1304 const char *comment,
1305 const char *prompt1,
1306 const char *prompt2,
1307 const char **pass)
1309 int authtok_flag;
1310 int retval;
1311 const char *item;
1312 char *token;
1314 _pam_log(pamh, ctrl, LOG_DEBUG, "getting password (0x%08x)", ctrl);
1317 * make sure nothing inappropriate gets returned
1320 *pass = token = NULL;
1323 * which authentication token are we getting?
1326 authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
1329 * should we obtain the password from a PAM item ?
1332 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1333 retval = _pam_get_item(pamh, authtok_flag, &item);
1334 if (retval != PAM_SUCCESS) {
1335 /* very strange. */
1336 _pam_log(pamh, ctrl, LOG_ALERT,
1337 "pam_get_item returned error to unix-read-password"
1339 return retval;
1340 } else if (item != NULL) { /* we have a password! */
1341 *pass = item;
1342 item = NULL;
1343 _pam_log(pamh, ctrl, LOG_DEBUG,
1344 "pam_get_item returned a password");
1345 return PAM_SUCCESS;
1346 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1347 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
1348 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
1349 && off(WINBIND__OLD_PASSWORD, ctrl)) {
1350 return PAM_AUTHTOK_RECOVER_ERR;
1354 * getting here implies we will have to get the password from the
1355 * user directly.
1359 struct pam_message msg[3], *pmsg[3];
1360 struct pam_response *resp;
1361 int i, replies;
1363 /* prepare to converse */
1365 if (comment != NULL && off(ctrl, WINBIND_SILENT)) {
1366 pmsg[0] = &msg[0];
1367 msg[0].msg_style = PAM_TEXT_INFO;
1368 msg[0].msg = CONST_DISCARD(char *, comment);
1369 i = 1;
1370 } else {
1371 i = 0;
1374 pmsg[i] = &msg[i];
1375 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1376 msg[i++].msg = CONST_DISCARD(char *, prompt1);
1377 replies = 1;
1379 if (prompt2 != NULL) {
1380 pmsg[i] = &msg[i];
1381 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1382 msg[i++].msg = CONST_DISCARD(char *, prompt2);
1383 ++replies;
1385 /* so call the conversation expecting i responses */
1386 resp = NULL;
1387 retval = converse(pamh, i, pmsg, &resp);
1389 if (resp != NULL) {
1391 /* interpret the response */
1393 if (retval == PAM_SUCCESS) { /* a good conversation */
1395 token = x_strdup(resp[i - replies].resp);
1396 if (token != NULL) {
1397 if (replies == 2) {
1398 /* verify that password entered correctly */
1399 if (!resp[i - 1].resp
1400 || strcmp(token, resp[i - 1].resp)) {
1401 _pam_delete(token); /* mistyped */
1402 retval = PAM_AUTHTOK_RECOVER_ERR;
1403 _make_remark(pamh, ctrl, PAM_ERROR_MSG, MISTYPED_PASS);
1406 } else {
1407 _pam_log(pamh, ctrl, LOG_NOTICE, "could not recover authentication token");
1408 retval = PAM_AUTHTOK_RECOVER_ERR;
1413 * tidy up the conversation (resp_retcode) is ignored
1414 * -- what is it for anyway? AGM
1417 _pam_drop_reply(resp, i);
1419 } else {
1420 retval = (retval == PAM_SUCCESS)
1421 ? PAM_AUTHTOK_RECOVER_ERR : retval;
1425 if (retval != PAM_SUCCESS) {
1426 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1427 "unable to obtain a password");
1428 return retval;
1430 /* 'token' is the entered password */
1432 /* we store this password as an item */
1434 retval = pam_set_item(pamh, authtok_flag, token);
1435 _pam_delete(token); /* clean it up */
1436 if (retval != PAM_SUCCESS ||
1437 (retval = _pam_get_item(pamh, authtok_flag, &item)) != PAM_SUCCESS) {
1439 _pam_log(pamh, ctrl, LOG_CRIT, "error manipulating password");
1440 return retval;
1444 *pass = item;
1445 item = NULL; /* break link to password */
1447 return PAM_SUCCESS;
1450 const char *get_conf_item_string(const pam_handle_t *pamh,
1451 int argc,
1452 const char **argv,
1453 int ctrl,
1454 dictionary *d,
1455 const char *item,
1456 int config_flag)
1458 int i = 0;
1459 const char *parm_opt = NULL;
1460 char *key = NULL;
1462 if (!(ctrl & config_flag)) {
1463 goto out;
1466 /* let the pam opt take precedence over the pam_winbind.conf option */
1468 if (d != NULL) {
1470 if (!asprintf(&key, "global:%s", item)) {
1471 goto out;
1474 parm_opt = iniparser_getstr(d, key);
1475 SAFE_FREE(key);
1478 for ( i=0; i<argc; i++ ) {
1480 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
1481 char *p;
1483 if ( (p = strchr( argv[i], '=' )) == NULL) {
1484 _pam_log(pamh, ctrl, LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
1485 goto out;
1487 _pam_log_debug(pamh, ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
1488 return p + 1;
1492 if (d != NULL) {
1493 _pam_log_debug(pamh, ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
1495 out:
1496 return parm_opt;
1499 const char *get_krb5_cc_type_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1501 return get_conf_item_string(pamh, argc, argv, ctrl, d, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
1504 const char *get_member_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1506 const char *ret = NULL;
1507 ret = get_conf_item_string(pamh, argc, argv, ctrl, d, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
1508 if (ret) {
1509 return ret;
1511 return get_conf_item_string(pamh, argc, argv, ctrl, d, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
1514 PAM_EXTERN
1515 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
1516 int argc, const char **argv)
1518 const char *username;
1519 const char *password;
1520 const char *member = NULL;
1521 const char *cctype = NULL;
1522 int retval = PAM_AUTH_ERR;
1523 dictionary *d = NULL;
1524 char *username_ret = NULL;
1525 char *new_authtok_required = NULL;
1526 char *real_username = NULL;
1528 /* parse arguments */
1529 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1530 if (ctrl == -1) {
1531 retval = PAM_SYSTEM_ERR;
1532 goto out;
1535 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", pamh, ctrl, flags);
1537 /* Get the username */
1538 retval = pam_get_user(pamh, &username, NULL);
1539 if ((retval != PAM_SUCCESS) || (!username)) {
1540 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "can not get the username");
1541 retval = PAM_SERVICE_ERR;
1542 goto out;
1545 #if defined(AIX)
1546 /* Decode the user name since AIX does not support logn user
1547 names by default. The name is encoded as _#uid. */
1549 if ( username[0] == '_' ) {
1550 uid_t id = atoi( &username[1] );
1551 struct passwd *pw = NULL;
1553 if ( (id!=0) && ((pw = getpwuid( id )) != NULL) ) {
1554 real_username = strdup( pw->pw_name );
1557 #endif
1559 if ( !real_username ) {
1560 /* Just making a copy of the username we got from PAM */
1561 if ( (real_username = strdup( username )) == NULL ) {
1562 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1563 "memory allocation failure when copying username");
1564 retval = PAM_SERVICE_ERR;
1565 goto out;
1569 retval = _winbind_read_password(pamh, ctrl, NULL,
1570 "Password: ", NULL,
1571 &password);
1573 if (retval != PAM_SUCCESS) {
1574 _pam_log(pamh, ctrl, LOG_ERR, "Could not retrieve user's password");
1575 retval = PAM_AUTHTOK_ERR;
1576 goto out;
1579 /* Let's not give too much away in the log file */
1581 #ifdef DEBUG_PASSWORD
1582 _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s' with password '%s'",
1583 real_username, password);
1584 #else
1585 _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s'", real_username);
1586 #endif
1588 member = get_member_from_config(pamh, argc, argv, ctrl, d);
1590 cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1592 /* Now use the username to look up password */
1593 retval = winbind_auth_request(pamh, ctrl, username, password, member,
1594 cctype, NULL, NULL, &username_ret);
1596 if (retval == PAM_NEW_AUTHTOK_REQD ||
1597 retval == PAM_AUTHTOK_EXPIRED) {
1599 char *new_authtok_required_during_auth = NULL;
1601 if (!asprintf(&new_authtok_required, "%d", retval)) {
1602 retval = PAM_BUF_ERR;
1603 goto out;
1606 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, new_authtok_required, _pam_winbind_cleanup_func);
1608 retval = PAM_SUCCESS;
1610 if (!asprintf(&new_authtok_required_during_auth, "%d", True)) {
1611 retval = PAM_BUF_ERR;
1612 goto out;
1615 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
1616 new_authtok_required_during_auth, _pam_winbind_cleanup_func);
1618 goto out;
1621 out:
1622 if (username_ret) {
1623 pam_set_item (pamh, PAM_USER, username_ret);
1624 _pam_log_debug(pamh, ctrl, LOG_INFO, "Returned user was '%s'", username_ret);
1625 free(username_ret);
1628 if ( real_username ) {
1629 free( real_username );
1632 if (d) {
1633 iniparser_freedict(d);
1636 if (!new_authtok_required) {
1637 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
1640 if (retval != PAM_SUCCESS) {
1641 _pam_free_data_info3(pamh);
1644 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", pamh, ctrl, retval);
1646 return retval;
1649 PAM_EXTERN
1650 int pam_sm_setcred(pam_handle_t *pamh, int flags,
1651 int argc, const char **argv)
1653 int ret = PAM_SYSTEM_ERR;
1654 dictionary *d = NULL;
1656 /* parse arguments */
1657 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1658 if (ctrl == -1) {
1659 ret = PAM_SYSTEM_ERR;
1660 goto out;
1663 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", pamh, ctrl, flags);
1665 switch (flags & ~PAM_SILENT) {
1667 case PAM_DELETE_CRED:
1668 ret = pam_sm_close_session(pamh, flags, argc, argv);
1669 break;
1670 case PAM_REFRESH_CRED:
1671 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_REFRESH_CRED not implemented");
1672 ret = PAM_SUCCESS;
1673 break;
1674 case PAM_REINITIALIZE_CRED:
1675 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_REINITIALIZE_CRED not implemented");
1676 ret = PAM_SUCCESS;
1677 break;
1678 case PAM_ESTABLISH_CRED:
1679 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_ESTABLISH_CRED not implemented");
1680 ret = PAM_SUCCESS;
1681 break;
1682 default:
1683 ret = PAM_SYSTEM_ERR;
1684 break;
1687 out:
1688 if (d) {
1689 iniparser_freedict(d);
1692 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", pamh, ctrl, ret);
1694 return ret;
1698 * Account management. We want to verify that the account exists
1699 * before returning PAM_SUCCESS
1701 PAM_EXTERN
1702 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
1703 int argc, const char **argv)
1705 const char *username;
1706 int ret = PAM_USER_UNKNOWN;
1707 void *tmp = NULL;
1708 dictionary *d = NULL;
1710 /* parse arguments */
1711 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1712 if (ctrl == -1) {
1713 return PAM_SYSTEM_ERR;
1716 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", pamh, ctrl, flags);
1719 /* Get the username */
1720 ret = pam_get_user(pamh, &username, NULL);
1721 if ((ret != PAM_SUCCESS) || (!username)) {
1722 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"can not get the username");
1723 ret = PAM_SERVICE_ERR;
1724 goto out;
1727 /* Verify the username */
1728 ret = valid_user(pamh, ctrl, username);
1729 switch (ret) {
1730 case -1:
1731 /* some sort of system error. The log was already printed */
1732 ret = PAM_SERVICE_ERR;
1733 goto out;
1734 case 1:
1735 /* the user does not exist */
1736 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", username);
1737 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
1738 ret = PAM_IGNORE;
1739 goto out;
1741 ret = PAM_USER_UNKNOWN;
1742 goto out;
1743 case 0:
1744 pam_get_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (const void **)&tmp);
1745 if (tmp != NULL) {
1746 ret = atoi((const char *)tmp);
1747 switch (ret) {
1748 case PAM_AUTHTOK_EXPIRED:
1749 /* fall through, since new token is required in this case */
1750 case PAM_NEW_AUTHTOK_REQD:
1751 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success but %s is set",
1752 PAM_WINBIND_NEW_AUTHTOK_REQD);
1753 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' needs new password", username);
1754 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
1755 ret = PAM_NEW_AUTHTOK_REQD;
1756 goto out;
1757 default:
1758 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success");
1759 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1760 ret = PAM_SUCCESS;
1761 goto out;
1765 /* Otherwise, the authentication looked good */
1766 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1767 ret = PAM_SUCCESS;
1768 goto out;
1769 default:
1770 /* we don't know anything about this return value */
1771 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (ret = %d, user = '%s')",
1772 ret, username);
1773 ret = PAM_SERVICE_ERR;
1774 goto out;
1777 /* should not be reached */
1778 ret = PAM_IGNORE;
1780 out:
1782 if (d) {
1783 iniparser_freedict(d);
1786 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", pamh, ctrl, ret);
1788 return ret;
1791 PAM_EXTERN
1792 int pam_sm_open_session(pam_handle_t *pamh, int flags,
1793 int argc, const char **argv)
1795 int ret = PAM_SYSTEM_ERR;
1796 dictionary *d = NULL;
1798 /* parse arguments */
1799 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1800 if (ctrl == -1) {
1801 ret = PAM_SYSTEM_ERR;
1802 goto out;
1805 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", pamh, ctrl, flags);
1807 ret = PAM_SUCCESS;
1809 out:
1810 if (d) {
1811 iniparser_freedict(d);
1814 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", pamh, ctrl, ret);
1816 return ret;
1819 PAM_EXTERN
1820 int pam_sm_close_session(pam_handle_t *pamh, int flags,
1821 int argc, const char **argv)
1823 dictionary *d = NULL;
1824 int retval = PAM_SUCCESS;
1826 /* parse arguments */
1827 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1828 if (ctrl == -1) {
1829 retval = PAM_SYSTEM_ERR;
1830 goto out;
1833 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", pamh, ctrl, flags);
1835 if (!(flags & PAM_DELETE_CRED)) {
1836 retval = PAM_SUCCESS;
1837 goto out;
1840 if (ctrl & WINBIND_KRB5_AUTH) {
1842 /* destroy the ccache here */
1843 struct winbindd_request request;
1844 struct winbindd_response response;
1845 const char *user;
1846 const char *ccname = NULL;
1847 struct passwd *pwd = NULL;
1849 ZERO_STRUCT(request);
1850 ZERO_STRUCT(response);
1852 retval = pam_get_user(pamh, &user, "Username: ");
1853 if (retval) {
1854 _pam_log(pamh, ctrl, LOG_ERR, "could not identify user");
1855 goto out;
1858 if (user == NULL) {
1859 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1860 retval = PAM_USER_UNKNOWN;
1861 goto out;
1864 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
1866 ccname = pam_getenv(pamh, "KRB5CCNAME");
1867 if (ccname == NULL) {
1868 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1871 strncpy(request.data.logoff.user, user,
1872 sizeof(request.data.logoff.user) - 1);
1874 if (ccname) {
1875 strncpy(request.data.logoff.krb5ccname, ccname,
1876 sizeof(request.data.logoff.krb5ccname) - 1);
1879 pwd = getpwnam(user);
1880 if (pwd == NULL) {
1881 retval = PAM_USER_UNKNOWN;
1882 goto out;
1884 request.data.logoff.uid = pwd->pw_uid;
1886 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1888 retval = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1891 out:
1892 if (d) {
1893 iniparser_freedict(d);
1897 * Delete the krb5 ccname variable from the PAM environment
1898 * if it was set by winbind.
1900 if (ctrl & WINBIND_KRB5_AUTH) {
1901 pam_putenv(pamh, "KRB5CCNAME");
1904 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", pamh, ctrl, retval);
1906 return retval;
1910 * evaluate whether we need to re-authenticate with kerberos after a password change
1912 * @param pamh PAM handle
1913 * @param ctrl PAM winbind options.
1914 * @param user The username
1916 * @return boolean Returns True if required, False if not.
1919 static BOOL _pam_require_krb5_auth_after_chauthtok(pam_handle_t *pamh, int ctrl, const char *user)
1922 /* Make sure that we only do this if
1923 * a) the chauthtok got initiated during a logon attempt (authenticate->acct_mgmt->chauthtok)
1924 * b) any later password change via the "passwd" command if done by the user itself
1926 * NB. If we login from gdm or xdm and the password expires,
1927 * we change the password, but there is no memory cache.
1928 * Thus, even for passthrough login, we should do the
1929 * authentication again to update memory cache.
1930 * --- BoYang
1931 * */
1933 char *new_authtok_reqd_during_auth = NULL;
1934 struct passwd *pwd = NULL;
1936 _pam_get_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH, &new_authtok_reqd_during_auth);
1937 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH, NULL, NULL);
1939 if (new_authtok_reqd_during_auth) {
1940 return True;
1943 pwd = getpwnam(user);
1944 if (!pwd) {
1945 return False;
1948 if (getuid() == pwd->pw_uid) {
1949 return True;
1952 return False;
1956 PAM_EXTERN
1957 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1958 int argc, const char **argv)
1960 unsigned int lctrl;
1961 int ret;
1962 unsigned int ctrl;
1963 bool cached_login = False;
1965 /* <DO NOT free() THESE> */
1966 const char *user;
1967 char *pass_old, *pass_new;
1968 /* </DO NOT free() THESE> */
1970 char *Announce;
1972 int retry = 0;
1973 dictionary *d = NULL;
1974 char *username_ret = NULL;
1975 struct winbindd_response response;
1977 ZERO_STRUCT(response);
1979 ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1980 if (ctrl == -1) {
1981 ret = PAM_SYSTEM_ERR;
1982 goto out;
1985 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", pamh, ctrl, flags);
1987 cached_login = (ctrl & WINBIND_CACHED_LOGIN);
1989 /* clearing offline bit for auth */
1990 ctrl &= ~WINBIND_CACHED_LOGIN;
1993 * First get the name of a user
1995 ret = pam_get_user(pamh, &user, "Username: ");
1996 if (ret) {
1997 _pam_log(pamh, ctrl, LOG_ERR,
1998 "password - could not identify user");
1999 goto out;
2002 if (user == NULL) {
2003 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
2004 ret = PAM_USER_UNKNOWN;
2005 goto out;
2008 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
2010 /* check if this is really a user in winbindd, not only in NSS */
2011 ret = valid_user(pamh, ctrl, user);
2012 switch (ret) {
2013 case 1:
2014 ret = PAM_USER_UNKNOWN;
2015 goto out;
2016 case -1:
2017 ret = PAM_SYSTEM_ERR;
2018 goto out;
2019 default:
2020 break;
2024 * obtain and verify the current password (OLDAUTHTOK) for
2025 * the user.
2028 if (flags & PAM_PRELIM_CHECK) {
2029 time_t pwdlastset_prelim = 0;
2031 /* instruct user what is happening */
2032 #define greeting "Changing password for "
2033 Announce = (char *) malloc(sizeof(greeting) + strlen(user));
2034 if (Announce == NULL) {
2035 _pam_log(pamh, ctrl, LOG_CRIT, "password - out of memory");
2036 ret = PAM_BUF_ERR;
2037 goto out;
2039 (void) strcpy(Announce, greeting);
2040 (void) strcpy(Announce + sizeof(greeting) - 1, user);
2041 #undef greeting
2043 lctrl = ctrl | WINBIND__OLD_PASSWORD;
2044 ret = _winbind_read_password(pamh, lctrl,
2045 Announce,
2046 "(current) NT password: ",
2047 NULL,
2048 (const char **) &pass_old);
2049 if (ret != PAM_SUCCESS) {
2050 _pam_log(pamh, ctrl, LOG_NOTICE, "password - (old) token not obtained");
2051 goto out;
2054 /* verify that this is the password for this user */
2056 ret = winbind_auth_request(pamh, ctrl, user, pass_old,
2057 NULL, NULL, &response, &pwdlastset_prelim, NULL);
2059 if (ret != PAM_ACCT_EXPIRED &&
2060 ret != PAM_AUTHTOK_EXPIRED &&
2061 ret != PAM_NEW_AUTHTOK_REQD &&
2062 ret != PAM_SUCCESS) {
2063 pass_old = NULL;
2064 goto out;
2067 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET, (void *)pwdlastset_prelim, NULL);
2069 ret = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
2070 pass_old = NULL;
2071 if (ret != PAM_SUCCESS) {
2072 _pam_log(pamh, ctrl, LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
2074 } else if (flags & PAM_UPDATE_AUTHTOK) {
2076 time_t pwdlastset_update = 0;
2079 * obtain the proposed password
2083 * get the old token back.
2086 ret = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
2088 if (ret != PAM_SUCCESS) {
2089 _pam_log(pamh, ctrl, LOG_NOTICE, "user not authenticated");
2090 goto out;
2093 lctrl = ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
2095 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
2096 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
2098 retry = 0;
2099 ret = PAM_AUTHTOK_ERR;
2100 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
2102 * use_authtok is to force the use of a previously entered
2103 * password -- needed for pluggable password strength checking
2106 ret = _winbind_read_password(pamh, lctrl,
2107 NULL,
2108 "Enter new NT password: ",
2109 "Retype new NT password: ",
2110 (const char **) &pass_new);
2112 if (ret != PAM_SUCCESS) {
2113 _pam_log_debug(pamh, ctrl, LOG_ALERT
2114 ,"password - new password not obtained");
2115 pass_old = NULL;/* tidy up */
2116 goto out;
2120 * At this point we know who the user is and what they
2121 * propose as their new password. Verify that the new
2122 * password is acceptable.
2125 if (pass_new[0] == '\0') {/* "\0" password = NULL */
2126 pass_new = NULL;
2131 * By reaching here we have approved the passwords and must now
2132 * rebuild the password database file.
2134 _pam_get_data( pamh, PAM_WINBIND_PWD_LAST_SET,
2135 &pwdlastset_update);
2138 * if cached creds were enabled, make sure to set the
2139 * WINBIND_CACHED_LOGIN bit here in order to have winbindd
2140 * update the cached creds storage - gd
2142 if (cached_login) {
2143 ctrl |= WINBIND_CACHED_LOGIN;
2146 ret = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new, pwdlastset_update);
2147 if (ret) {
2148 _pam_overwrite(pass_new);
2149 _pam_overwrite(pass_old);
2150 pass_old = pass_new = NULL;
2151 goto out;
2154 if (_pam_require_krb5_auth_after_chauthtok(pamh, ctrl, user)) {
2156 const char *member = get_member_from_config(pamh, argc, argv, ctrl, d);
2157 const char *cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
2159 /* Keep the WINBIND_CACHED_LOGIN bit for
2160 * authentication after changing the password.
2161 * This will update the cached credentials in case
2162 * that winbindd_dual_pam_chauthtok() fails
2163 * to update them.
2164 * --- BoYang
2165 * */
2167 ret = winbind_auth_request(pamh, ctrl, user, pass_new,
2168 member, cctype, &response, NULL, &username_ret);
2169 _pam_overwrite(pass_new);
2170 _pam_overwrite(pass_old);
2171 pass_old = pass_new = NULL;
2173 if (ret == PAM_SUCCESS) {
2175 /* warn a user if the password is about to expire soon */
2176 _pam_warn_password_expiry(pamh, ctrl, &response, NULL);
2178 /* set some info3 info for other modules in the stack */
2179 _pam_set_data_info3(pamh, ctrl, &response);
2181 /* put krb5ccname into env */
2182 _pam_setup_krb5_env(pamh, ctrl, response.data.auth.krb5ccname);
2184 if (username_ret) {
2185 pam_set_item (pamh, PAM_USER, username_ret);
2186 _pam_log_debug(pamh, ctrl, LOG_INFO, "Returned user was '%s'", username_ret);
2187 free(username_ret);
2191 goto out;
2193 } else {
2194 ret = PAM_SERVICE_ERR;
2197 out:
2198 if (d) {
2199 iniparser_freedict(d);
2202 /* Deal with offline errors. */
2203 PAM_WB_REMARK_CHECK_RESPONSE(pamh, ctrl, response, "NT_STATUS_NO_LOGON_SERVERS");
2204 PAM_WB_REMARK_CHECK_RESPONSE(pamh, ctrl, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
2205 PAM_WB_REMARK_CHECK_RESPONSE(pamh, ctrl, response, "NT_STATUS_ACCESS_DENIED");
2207 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", pamh, ctrl, ret);
2209 return ret;
2212 #ifdef PAM_STATIC
2214 /* static module data */
2216 struct pam_module _pam_winbind_modstruct = {
2217 MODULE_NAME,
2218 pam_sm_authenticate,
2219 pam_sm_setcred,
2220 pam_sm_acct_mgmt,
2221 pam_sm_open_session,
2222 pam_sm_close_session,
2223 pam_sm_chauthtok
2226 #endif
2229 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
2230 * Copyright (c) Tim Potter <tpot@samba.org> 2000
2231 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
2232 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2007
2233 * Copyright (c) Jan Rêkorajski 1999.
2234 * Copyright (c) Andrew G. Morgan 1996-8.
2235 * Copyright (c) Alex O. Yuriev, 1996.
2236 * Copyright (c) Cristian Gafton 1996.
2237 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
2239 * Redistribution and use in source and binary forms, with or without
2240 * modification, are permitted provided that the following conditions
2241 * are met:
2242 * 1. Redistributions of source code must retain the above copyright
2243 * notice, and the entire permission notice in its entirety,
2244 * including the disclaimer of warranties.
2245 * 2. Redistributions in binary form must reproduce the above copyright
2246 * notice, this list of conditions and the following disclaimer in the
2247 * documentation and/or other materials provided with the distribution.
2248 * 3. The name of the author may not be used to endorse or promote
2249 * products derived from this software without specific prior
2250 * written permission.
2252 * ALTERNATIVELY, this product may be distributed under the terms of
2253 * the GNU Public License, in which case the provisions of the GPL are
2254 * required INSTEAD OF the above restrictions. (This clause is
2255 * necessary due to a potential bad interaction between the GPL and
2256 * the restrictions contained in a BSD-style copyright.)
2258 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
2259 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2260 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2261 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2262 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2263 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2264 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2265 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2266 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2267 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2268 * OF THE POSSIBILITY OF SUCH DAMAGE.