r23539: Remove code duplication and unify behaviour of winbind_nss_*.h files
[Samba.git] / source / nsswitch / pam_winbind.c
blob81b9c0bc6cbb4c148db0ba4dedee04527838e3a2
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, "silent"))
277 ctrl |= WINBIND_SILENT;
278 else if (!strcasecmp(*v, "use_authtok"))
279 ctrl |= WINBIND_USE_AUTHTOK_ARG;
280 else if (!strcasecmp(*v, "use_first_pass"))
281 ctrl |= WINBIND_USE_FIRST_PASS_ARG;
282 else if (!strcasecmp(*v, "try_first_pass"))
283 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
284 else if (!strcasecmp(*v, "unknown_ok"))
285 ctrl |= WINBIND_UNKNOWN_OK_ARG;
286 else if (!strncasecmp(*v, "require_membership_of", strlen("require_membership_of")))
287 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
288 else if (!strncasecmp(*v, "require-membership-of", strlen("require-membership-of")))
289 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
290 else if (!strcasecmp(*v, "krb5_auth"))
291 ctrl |= WINBIND_KRB5_AUTH;
292 else if (!strncasecmp(*v, "krb5_ccache_type", strlen("krb5_ccache_type")))
293 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
294 else if (!strcasecmp(*v, "cached_login"))
295 ctrl |= WINBIND_CACHED_LOGIN;
296 else {
297 _pam_log(pamh, ctrl, LOG_ERR, "pam_parse: unknown option: %s", *v);
298 return -1;
303 if (result_d) {
304 *result_d = d;
305 } else {
306 if (d) {
307 iniparser_freedict(d);
311 return ctrl;
314 static void _pam_winbind_cleanup_func(pam_handle_t *pamh, void *data, int error_status)
316 int ctrl = _pam_parse(pamh, 0, 0, NULL, NULL);
317 if (_pam_log_is_debug_state_enabled(ctrl)) {
318 _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);
320 SAFE_FREE(data);
324 static const struct ntstatus_errors {
325 const char *ntstatus_string;
326 const char *error_string;
327 } ntstatus_errors[] = {
328 {"NT_STATUS_OK", "Success"},
329 {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
330 {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND", "No domain controllers found"},
331 {"NT_STATUS_NO_LOGON_SERVERS", "No logon servers"},
332 {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
333 {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
334 {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
335 {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
336 {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
337 {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
338 {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
339 {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
340 {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
341 {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
342 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
343 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
344 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
345 {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
346 {NULL, NULL}
349 const char *_get_ntstatus_error_string(const char *nt_status_string)
351 int i;
352 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
353 if (!strcasecmp(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
354 return ntstatus_errors[i].error_string;
357 return NULL;
360 /* --- authentication management functions --- */
362 /* Attempt a conversation */
364 static int converse(pam_handle_t *pamh, int nargs,
365 struct pam_message **message,
366 struct pam_response **response)
368 int retval;
369 struct pam_conv *conv;
371 retval = _pam_get_item(pamh, PAM_CONV, &conv );
372 if (retval == PAM_SUCCESS) {
373 retval = conv->conv(nargs, (const struct pam_message **)message,
374 response, conv->appdata_ptr);
377 return retval; /* propagate error status */
381 static int _make_remark(pam_handle_t * pamh, int flags, int type, const char *text)
383 int retval = PAM_SUCCESS;
385 struct pam_message *pmsg[1], msg[1];
386 struct pam_response *resp;
388 if (flags & WINBIND_SILENT) {
389 return PAM_SUCCESS;
392 pmsg[0] = &msg[0];
393 msg[0].msg = CONST_DISCARD(char *, text);
394 msg[0].msg_style = type;
396 resp = NULL;
397 retval = converse(pamh, 1, pmsg, &resp);
399 if (resp) {
400 _pam_drop_reply(resp, 1);
402 return retval;
405 static int _make_remark_v(pam_handle_t * pamh, int flags, int type, const char *format, va_list args)
407 char *var;
408 int ret;
410 ret = vasprintf(&var, format, args);
411 if (ret < 0) {
412 _pam_log(pamh, 0, LOG_ERR, "memory allocation failure");
413 return ret;
416 ret = _make_remark(pamh, flags, type, var);
417 SAFE_FREE(var);
418 return ret;
421 static int _make_remark_format(pam_handle_t * pamh, int flags, int type, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
422 static int _make_remark_format(pam_handle_t * pamh, int flags, int type, const char *format, ...)
424 int ret;
425 va_list args;
427 va_start(args, format);
428 ret = _make_remark_v(pamh, flags, type, format, args);
429 va_end(args);
430 return ret;
433 static int pam_winbind_request(pam_handle_t * pamh, int ctrl,
434 enum winbindd_cmd req_type,
435 struct winbindd_request *request,
436 struct winbindd_response *response)
438 /* Fill in request and send down pipe */
439 init_request(request, req_type);
441 if (write_sock(request, sizeof(*request), 0, 0) == -1) {
442 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: write to socket failed!");
443 close_sock();
444 return PAM_SERVICE_ERR;
447 /* Wait for reply */
448 if (read_reply(response) == -1) {
449 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: read from socket failed!");
450 close_sock();
451 return PAM_SERVICE_ERR;
454 /* We are done with the socket - close it and avoid mischeif */
455 close_sock();
457 /* Copy reply data from socket */
458 if (response->result == WINBINDD_OK) {
459 return PAM_SUCCESS;
462 /* no need to check for pam_error codes for getpwnam() */
463 switch (req_type) {
465 case WINBINDD_GETPWNAM:
466 case WINBINDD_LOOKUPNAME:
467 if (strlen(response->data.auth.nt_status_string) > 0) {
468 _pam_log(pamh, ctrl, LOG_ERR, "request failed, NT error was %s",
469 response->data.auth.nt_status_string);
470 } else {
471 _pam_log(pamh, ctrl, LOG_ERR, "request failed");
473 return PAM_USER_UNKNOWN;
474 default:
475 break;
478 if (response->data.auth.pam_error != PAM_SUCCESS) {
479 _pam_log(pamh, ctrl, LOG_ERR, "request failed: %s, PAM error was %s (%d), NT error was %s",
480 response->data.auth.error_string,
481 pam_strerror(pamh, response->data.auth.pam_error),
482 response->data.auth.pam_error,
483 response->data.auth.nt_status_string);
484 return response->data.auth.pam_error;
487 _pam_log(pamh, ctrl, LOG_ERR, "request failed, but PAM error 0!");
489 return PAM_SERVICE_ERR;
492 static int pam_winbind_request_log(pam_handle_t * pamh,
493 int ctrl,
494 enum winbindd_cmd req_type,
495 struct winbindd_request *request,
496 struct winbindd_response *response,
497 const char *user)
499 int retval;
501 retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
503 switch (retval) {
504 case PAM_AUTH_ERR:
505 /* incorrect password */
506 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' denied access (incorrect password or invalid membership)", user);
507 return retval;
508 case PAM_ACCT_EXPIRED:
509 /* account expired */
510 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' account expired", user);
511 return retval;
512 case PAM_AUTHTOK_EXPIRED:
513 /* password expired */
514 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' password expired", user);
515 return retval;
516 case PAM_NEW_AUTHTOK_REQD:
517 /* new password required */
518 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' new password required", user);
519 return retval;
520 case PAM_USER_UNKNOWN:
521 /* the user does not exist */
522 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", user);
523 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
524 return PAM_IGNORE;
526 return retval;
527 case PAM_SUCCESS:
528 /* Otherwise, the authentication looked good */
529 switch (req_type) {
530 case WINBINDD_INFO:
531 break;
532 case WINBINDD_PAM_AUTH:
533 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", user);
534 break;
535 case WINBINDD_PAM_CHAUTHTOK:
536 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' password changed", user);
537 break;
538 default:
539 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' OK", user);
540 break;
543 return retval;
544 default:
545 /* we don't know anything about this return value */
546 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (retval = %d, user = '%s')",
547 retval, user);
548 return retval;
553 * send a password expiry message if required
555 * @param pamh PAM handle
556 * @param ctrl PAM winbind options.
557 * @param next_change expected (calculated) next expiry date.
558 * @param already_expired pointer to a boolean to indicate if the password is
559 * already expired.
561 * @return boolean Returns True if message has been sent, False if not.
564 static BOOL _pam_send_password_expiry_message(pam_handle_t *pamh, int ctrl, time_t next_change, time_t now, BOOL *already_expired)
566 int days = 0;
567 struct tm tm_now, tm_next_change;
569 if (already_expired) {
570 *already_expired = False;
573 if (next_change <= now) {
574 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PASSWORD_EXPIRED");
575 if (already_expired) {
576 *already_expired = True;
578 return True;
581 if ((next_change < 0) ||
582 (next_change > now + DAYS_TO_WARN_BEFORE_PWD_EXPIRES * SECONDS_PER_DAY)) {
583 return False;
586 if ((localtime_r(&now, &tm_now) == NULL) ||
587 (localtime_r(&next_change, &tm_next_change) == NULL)) {
588 return False;
591 days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) - (tm_now.tm_yday+tm_now.tm_year*365);
593 if (days == 0) {
594 _make_remark(pamh, ctrl, PAM_TEXT_INFO, "Your password expires today");
595 return True;
598 if (days > 0 && days < DAYS_TO_WARN_BEFORE_PWD_EXPIRES) {
599 _make_remark_format(pamh, ctrl, PAM_TEXT_INFO, "Your password will expire in %d %s",
600 days, (days > 1) ? "days":"day");
601 return True;
604 return False;
608 * Send a warning if the password expires in the near future
610 * @param pamh PAM handle
611 * @param ctrl PAM winbind options.
612 * @param response The full authentication response structure.
613 * @param already_expired boolean, is the pwd already expired?
615 * @return void.
618 static void _pam_warn_password_expiry(pam_handle_t *pamh,
619 int flags,
620 const struct winbindd_response *response,
621 BOOL *already_expired)
623 time_t now = time(NULL);
624 time_t next_change = 0;
626 if (already_expired) {
627 *already_expired = False;
630 /* accounts with ACB_PWNOEXP set never receive a warning */
631 if (response->data.auth.info3.acct_flags & ACB_PWNOEXP) {
632 return;
635 /* no point in sending a warning if this is a grace logon */
636 if (PAM_WB_GRACE_LOGON(response->data.auth.info3.user_flgs)) {
637 return;
640 /* check if the info3 must change timestamp has been set */
641 next_change = response->data.auth.info3.pass_must_change_time;
643 if (_pam_send_password_expiry_message(pamh, flags, next_change, now,
644 already_expired)) {
645 return;
648 /* now check for the global password policy */
649 /* good catch from Ralf Haferkamp: an expiry of "never" is translated
650 * to -1 */
651 if (response->data.auth.policy.expire <= 0) {
652 return;
655 next_change = response->data.auth.info3.pass_last_set_time +
656 response->data.auth.policy.expire;
658 if (_pam_send_password_expiry_message(pamh, flags, next_change, now,
659 already_expired)) {
660 return;
663 /* no warning sent */
666 #define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
668 static BOOL safe_append_string(char *dest,
669 const char *src,
670 int dest_buffer_size)
672 * Append a string, making sure not to overflow and to always return a NULL-terminated
673 * string.
675 * @param dest Destination string buffer (must already be NULL-terminated).
676 * @param src Source string buffer.
677 * @param dest_buffer_size Size of dest buffer in bytes.
679 * @return False if dest buffer is not big enough (no bytes copied), True on success.
682 int dest_length = strlen(dest);
683 int src_length = strlen(src);
685 if ( dest_length + src_length + 1 > dest_buffer_size ) {
686 return False;
689 memcpy(dest + dest_length, src, src_length + 1);
690 return True;
693 static BOOL winbind_name_to_sid_string(pam_handle_t *pamh,
694 int ctrl,
695 const char *user,
696 const char *name,
697 char *sid_list_buffer,
698 int sid_list_buffer_size)
700 * Convert a names into a SID string, appending it to a buffer.
702 * @param pamh PAM handle
703 * @param ctrl PAM winbind options.
704 * @param user User in PAM request.
705 * @param name Name to convert.
706 * @param sid_list_buffer Where to append the string sid.
707 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
709 * @return False on failure, True on success.
712 const char* sid_string;
713 struct winbindd_response sid_response;
715 /* lookup name? */
716 if (IS_SID_STRING(name)) {
717 sid_string = name;
718 } else {
719 struct winbindd_request sid_request;
721 ZERO_STRUCT(sid_request);
722 ZERO_STRUCT(sid_response);
724 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", name);
726 /* fortunatly winbindd can handle non-separated names */
727 strncpy(sid_request.data.name.name, name,
728 sizeof(sid_request.data.name.name) - 1);
730 if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME, &sid_request, &sid_response, user)) {
731 _pam_log(pamh, ctrl, LOG_INFO, "could not lookup name: %s\n", name);
732 return False;
735 sid_string = sid_response.data.sid.sid;
738 if (!safe_append_string(sid_list_buffer, sid_string, sid_list_buffer_size)) {
739 return False;
742 return True;
745 static BOOL winbind_name_list_to_sid_string_list(pam_handle_t *pamh,
746 int ctrl,
747 const char *user,
748 const char *name_list,
749 char *sid_list_buffer,
750 int sid_list_buffer_size)
752 * Convert a list of names into a list of sids.
754 * @param pamh PAM handle
755 * @param ctrl PAM winbind options.
756 * @param user User in PAM request.
757 * @param name_list List of names or string sids, separated by commas.
758 * @param sid_list_buffer Where to put the list of string sids.
759 * @param sid_list_buffer Size of sid_list_buffer (in bytes).
761 * @return False on failure, True on success.
764 BOOL result = False;
765 char *current_name = NULL;
766 const char *search_location;
767 const char *comma;
769 if ( sid_list_buffer_size > 0 ) {
770 sid_list_buffer[0] = 0;
773 search_location = name_list;
774 while ( (comma = strstr(search_location, ",")) != NULL ) {
775 current_name = strndup(search_location, comma - search_location);
776 if (NULL == current_name) {
777 goto out;
780 if (!winbind_name_to_sid_string(pamh, ctrl, user, current_name, sid_list_buffer, sid_list_buffer_size)) {
781 goto out;
784 SAFE_FREE(current_name);
786 if (!safe_append_string(sid_list_buffer, ",", sid_list_buffer_size)) {
787 goto out;
790 search_location = comma + 1;
793 if (!winbind_name_to_sid_string(pamh, ctrl, user, search_location, sid_list_buffer, sid_list_buffer_size)) {
794 goto out;
797 result = True;
799 out:
800 SAFE_FREE(current_name);
801 return result;
805 * put krb5ccname variable into environment
807 * @param pamh PAM handle
808 * @param ctrl PAM winbind options.
809 * @param krb5ccname env variable retrieved from winbindd.
811 * @return void.
814 static void _pam_setup_krb5_env(pam_handle_t *pamh, int ctrl, const char *krb5ccname)
816 char var[PATH_MAX];
817 int ret;
819 if (off(ctrl, WINBIND_KRB5_AUTH)) {
820 return;
823 if (!krb5ccname || (strlen(krb5ccname) == 0)) {
824 return;
827 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "request returned KRB5CCNAME: %s", krb5ccname);
829 if (snprintf(var, sizeof(var), "KRB5CCNAME=%s", krb5ccname) == -1) {
830 return;
833 ret = pam_putenv(pamh, var);
834 if (ret) {
835 _pam_log(pamh, ctrl, LOG_ERR, "failed to set KRB5CCNAME to %s: %s",
836 var, pam_strerror(pamh, ret));
841 * Set string into the PAM stack.
843 * @param pamh PAM handle
844 * @param ctrl PAM winbind options.
845 * @param data_name Key name for pam_set_data.
846 * @param value String value.
848 * @return void.
851 static void _pam_set_data_string(pam_handle_t *pamh, int ctrl, const char *data_name, const char *value)
853 int ret;
855 if ( !data_name || !value || (strlen(data_name) == 0) || (strlen(value) == 0) ) {
856 return;
859 ret = pam_set_data(pamh, data_name, (void *)strdup(value), _pam_winbind_cleanup_func);
860 if (ret) {
861 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data %s: %s\n",
862 data_name, pam_strerror(pamh, ret));
868 * Set info3 strings into the PAM stack.
870 * @param pamh PAM handle
871 * @param ctrl PAM winbind options.
872 * @param data_name Key name for pam_set_data.
873 * @param value String value.
875 * @return void.
878 static void _pam_set_data_info3(pam_handle_t *pamh, int ctrl, struct winbindd_response *response)
880 _pam_set_data_string(pamh, ctrl, PAM_WINBIND_HOMEDIR, response->data.auth.info3.home_dir);
881 _pam_set_data_string(pamh, ctrl, PAM_WINBIND_LOGONSCRIPT, response->data.auth.info3.logon_script);
882 _pam_set_data_string(pamh, ctrl, PAM_WINBIND_LOGONSERVER, response->data.auth.info3.logon_srv);
883 _pam_set_data_string(pamh, ctrl, PAM_WINBIND_PROFILEPATH, response->data.auth.info3.profile_path);
887 * Free info3 strings in the PAM stack.
889 * @param pamh PAM handle
891 * @return void.
894 static void _pam_free_data_info3(pam_handle_t *pamh)
896 pam_set_data(pamh, PAM_WINBIND_HOMEDIR, NULL, NULL);
897 pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT, NULL, NULL);
898 pam_set_data(pamh, PAM_WINBIND_LOGONSERVER, NULL, NULL);
899 pam_set_data(pamh, PAM_WINBIND_PROFILEPATH, NULL, NULL);
903 * Send PAM_ERROR_MSG for cached or grace logons.
905 * @param pamh PAM handle
906 * @param ctrl PAM winbind options.
907 * @param username User in PAM request.
908 * @param info3_user_flgs Info3 flags containing logon type bits.
910 * @return void.
913 static void _pam_warn_logon_type(pam_handle_t *pamh, int ctrl, const char *username, uint32 info3_user_flgs)
915 /* inform about logon type */
916 if (PAM_WB_GRACE_LOGON(info3_user_flgs)) {
918 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
919 "Grace login. Please change your password as soon you're online again");
920 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
921 "User %s logged on using grace logon\n", username);
923 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs)) {
925 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
926 "Domain Controller unreachable, using cached credentials instead. Network resources may be unavailable");
927 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
928 "User %s logged on using cached credentials\n", username);
933 * Send PAM_ERROR_MSG for krb5 errors.
935 * @param pamh PAM handle
936 * @param ctrl PAM winbind options.
937 * @param username User in PAM request.
938 * @param info3_user_flgs Info3 flags containing logon type bits.
940 * @return void.
943 static void _pam_warn_krb5_failure(pam_handle_t *pamh, int ctrl, const char *username, uint32 info3_user_flgs)
945 if (PAM_WB_KRB5_CLOCK_SKEW(info3_user_flgs)) {
946 _make_remark(pamh, ctrl, PAM_ERROR_MSG,
947 "Failed to establish your Kerberos Ticket cache "
948 "due time differences\n"
949 "with the domain controller. "
950 "Please verify the system time.\n");
951 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
952 "User %s: Clock skew when getting Krb5 TGT\n", username);
957 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
959 * @param response The struct winbindd_response.
961 * @return string (caller needs to free).
964 static char *_pam_compose_pwd_restriction_string(struct winbindd_response *response)
966 char *str = NULL;
967 size_t offset = 0, ret = 0, str_size = 1024;
969 str = (char *)malloc(str_size);
970 if (!str) {
971 return NULL;
974 memset(str, '\0', str_size);
976 offset = snprintf(str, str_size, "Your password ");
977 if (offset == -1) {
978 goto failed;
981 if (response->data.auth.policy.min_length_password > 0) {
982 ret = snprintf(str+offset, str_size-offset,
983 "must be at least %d characters; ",
984 response->data.auth.policy.min_length_password);
985 if (ret == -1) {
986 goto failed;
988 offset += ret;
991 if (response->data.auth.policy.password_history > 0) {
992 ret = snprintf(str+offset, str_size-offset,
993 "cannot repeat any of your previous %d passwords; ",
994 response->data.auth.policy.password_history);
995 if (ret == -1) {
996 goto failed;
998 offset += ret;
1001 if (response->data.auth.policy.password_properties & DOMAIN_PASSWORD_COMPLEX) {
1002 ret = snprintf(str+offset, str_size-offset,
1003 "must contain capitals, numerals or punctuation; "
1004 "and cannot contain your account or full name; ");
1005 if (ret == -1) {
1006 goto failed;
1008 offset += ret;
1011 ret = snprintf(str+offset, str_size-offset,
1012 "Please type a different password. "
1013 "Type a password which meets these requirements in both text boxes.");
1014 if (ret == -1) {
1015 goto failed;
1018 return str;
1020 failed:
1021 SAFE_FREE(str);
1022 return NULL;
1025 /* talk to winbindd */
1026 static int winbind_auth_request(pam_handle_t * pamh,
1027 int ctrl,
1028 const char *user,
1029 const char *pass,
1030 const char *member,
1031 const char *cctype,
1032 struct winbindd_response *p_response,
1033 time_t *pwd_last_set,
1034 char **user_ret)
1036 struct winbindd_request request;
1037 struct winbindd_response response;
1038 int ret;
1039 BOOL already_expired = False;
1041 ZERO_STRUCT(request);
1042 ZERO_STRUCT(response);
1044 if (pwd_last_set) {
1045 *pwd_last_set = 0;
1048 strncpy(request.data.auth.user, user,
1049 sizeof(request.data.auth.user)-1);
1051 strncpy(request.data.auth.pass, pass,
1052 sizeof(request.data.auth.pass)-1);
1054 request.data.auth.krb5_cc_type[0] = '\0';
1055 request.data.auth.uid = -1;
1057 request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_CONTACT_TRUSTDOM;
1059 if (ctrl & (WINBIND_KRB5_AUTH|WINBIND_CACHED_LOGIN)) {
1060 struct passwd *pwd = NULL;
1062 pwd = getpwnam(user);
1063 if (pwd == NULL) {
1064 return PAM_USER_UNKNOWN;
1066 request.data.auth.uid = pwd->pw_uid;
1069 if (ctrl & WINBIND_KRB5_AUTH) {
1071 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling krb5 login flag\n");
1073 request.flags |= WBFLAG_PAM_KRB5 | WBFLAG_PAM_FALLBACK_AFTER_KRB5;
1076 if (ctrl & WINBIND_CACHED_LOGIN) {
1077 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling cached login flag\n");
1078 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
1081 if (user_ret) {
1082 *user_ret = NULL;
1083 request.flags |= WBFLAG_PAM_UNIX_NAME;
1086 if (cctype != NULL) {
1087 strncpy(request.data.auth.krb5_cc_type, cctype,
1088 sizeof(request.data.auth.krb5_cc_type) - 1);
1089 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling request for a %s krb5 ccache\n", cctype);
1092 request.data.auth.require_membership_of_sid[0] = '\0';
1094 if (member != NULL) {
1096 if (!winbind_name_list_to_sid_string_list(pamh, ctrl, user, member,
1097 request.data.auth.require_membership_of_sid,
1098 sizeof(request.data.auth.require_membership_of_sid))) {
1100 _pam_log_debug(pamh, ctrl, LOG_ERR, "failed to serialize membership of sid \"%s\"\n", member);
1101 return PAM_AUTH_ERR;
1105 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
1107 if (pwd_last_set) {
1108 *pwd_last_set = response.data.auth.info3.pass_last_set_time;
1111 if (p_response) {
1112 /* We want to process the response in the caller. */
1113 *p_response = response;
1114 return ret;
1117 if (ret) {
1118 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PASSWORD_EXPIRED");
1119 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PASSWORD_MUST_CHANGE");
1120 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_INVALID_WORKSTATION");
1121 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_INVALID_LOGON_HOURS");
1122 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCOUNT_EXPIRED");
1123 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCOUNT_DISABLED");
1124 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCOUNT_LOCKED_OUT");
1125 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
1126 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
1127 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
1128 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1129 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NO_LOGON_SERVERS");
1130 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_WRONG_PASSWORD");
1131 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCESS_DENIED");
1134 if (ret == PAM_SUCCESS) {
1136 /* warn a user if the password is about to expire soon */
1137 _pam_warn_password_expiry(pamh, ctrl, &response, &already_expired);
1139 if (already_expired == True) {
1140 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Password has expired "
1141 "(Password was last set: %lld, the policy says "
1142 "it should expire here %lld (now it's: %lu))\n",
1143 response.data.auth.info3.pass_last_set_time,
1144 response.data.auth.info3.pass_last_set_time +
1145 response.data.auth.policy.expire,
1146 time(NULL));
1148 return PAM_AUTHTOK_EXPIRED;
1151 /* inform about logon type */
1152 _pam_warn_logon_type(pamh, ctrl, user, response.data.auth.info3.user_flgs);
1154 /* inform about krb5 failures */
1155 _pam_warn_krb5_failure(pamh, ctrl, user, response.data.auth.info3.user_flgs);
1157 /* set some info3 info for other modules in the stack */
1158 _pam_set_data_info3(pamh, ctrl, &response);
1160 /* put krb5ccname into env */
1161 _pam_setup_krb5_env(pamh, ctrl, response.data.auth.krb5ccname);
1163 /* If winbindd returned a username, return the pointer to it here. */
1164 if (user_ret && response.extra_data.data) {
1165 /* We have to trust it's a null terminated string. */
1166 *user_ret = (char *)response.extra_data.data;
1170 return ret;
1173 /* talk to winbindd */
1174 static int winbind_chauthtok_request(pam_handle_t * pamh,
1175 int ctrl,
1176 const char *user,
1177 const char *oldpass,
1178 const char *newpass,
1179 time_t pwd_last_set)
1181 struct winbindd_request request;
1182 struct winbindd_response response;
1183 int ret;
1185 ZERO_STRUCT(request);
1186 ZERO_STRUCT(response);
1188 if (request.data.chauthtok.user == NULL) return -2;
1190 strncpy(request.data.chauthtok.user, user,
1191 sizeof(request.data.chauthtok.user) - 1);
1193 if (oldpass != NULL) {
1194 strncpy(request.data.chauthtok.oldpass, oldpass,
1195 sizeof(request.data.chauthtok.oldpass) - 1);
1196 } else {
1197 request.data.chauthtok.oldpass[0] = '\0';
1200 if (newpass != NULL) {
1201 strncpy(request.data.chauthtok.newpass, newpass,
1202 sizeof(request.data.chauthtok.newpass) - 1);
1203 } else {
1204 request.data.chauthtok.newpass[0] = '\0';
1207 if (ctrl & WINBIND_KRB5_AUTH) {
1208 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1211 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
1213 if (ret == PAM_SUCCESS) {
1214 return ret;
1217 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_BACKUP_CONTROLLER");
1218 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
1219 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_NO_LOGON_SERVERS");
1220 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_ACCESS_DENIED");
1222 /* TODO: tell the min pwd length ? */
1223 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_TOO_SHORT");
1225 /* TODO: tell the minage ? */
1226 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_TOO_RECENT");
1228 /* TODO: tell the history length ? */
1229 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
1231 if (!strcasecmp(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
1233 char *pwd_restriction_string = NULL;
1235 /* FIXME: avoid to send multiple PAM messages after another */
1236 switch (response.data.auth.reject_reason) {
1237 case -1:
1238 break;
1239 case REJECT_REASON_OTHER:
1240 if ((response.data.auth.policy.min_passwordage > 0) &&
1241 (pwd_last_set + response.data.auth.policy.min_passwordage > time(NULL))) {
1242 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PWD_TOO_RECENT");
1244 break;
1245 case REJECT_REASON_TOO_SHORT:
1246 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PWD_TOO_SHORT");
1247 break;
1248 case REJECT_REASON_IN_HISTORY:
1249 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PWD_HISTORY_CONFLICT");
1250 break;
1251 case REJECT_REASON_NOT_COMPLEX:
1252 _make_remark(pamh, ctrl, PAM_ERROR_MSG, "Password does not meet complexity requirements");
1253 break;
1254 default:
1255 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1256 "unknown password change reject reason: %d",
1257 response.data.auth.reject_reason);
1258 break;
1261 pwd_restriction_string = _pam_compose_pwd_restriction_string(&response);
1262 if (pwd_restriction_string) {
1263 _make_remark(pamh, ctrl, PAM_ERROR_MSG, pwd_restriction_string);
1264 SAFE_FREE(pwd_restriction_string);
1268 return ret;
1272 * Checks if a user has an account
1274 * return values:
1275 * 1 = User not found
1276 * 0 = OK
1277 * -1 = System error
1279 static int valid_user(pam_handle_t *pamh, int ctrl, const char *user)
1281 /* check not only if the user is available over NSS calls, also make
1282 * sure it's really a winbind user, this is important when stacking PAM
1283 * modules in the 'account' or 'password' facility. */
1285 struct passwd *pwd = NULL;
1286 struct winbindd_request request;
1287 struct winbindd_response response;
1288 int ret;
1290 ZERO_STRUCT(request);
1291 ZERO_STRUCT(response);
1293 pwd = getpwnam(user);
1294 if (pwd == NULL) {
1295 return 1;
1298 strncpy(request.data.username, user,
1299 sizeof(request.data.username) - 1);
1301 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_GETPWNAM, &request, &response, user);
1303 switch (ret) {
1304 case PAM_USER_UNKNOWN:
1305 return 1;
1306 case PAM_SUCCESS:
1307 return 0;
1308 default:
1309 break;
1311 return -1;
1314 static char *_pam_delete(register char *xx)
1316 _pam_overwrite(xx);
1317 _pam_drop(xx);
1318 return NULL;
1322 * obtain a password from the user
1325 static int _winbind_read_password(pam_handle_t * pamh,
1326 unsigned int ctrl,
1327 const char *comment,
1328 const char *prompt1,
1329 const char *prompt2,
1330 const char **pass)
1332 int authtok_flag;
1333 int retval;
1334 const char *item;
1335 char *token;
1337 _pam_log(pamh, ctrl, LOG_DEBUG, "getting password (0x%08x)", ctrl);
1340 * make sure nothing inappropriate gets returned
1343 *pass = token = NULL;
1346 * which authentication token are we getting?
1349 authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
1352 * should we obtain the password from a PAM item ?
1355 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1356 retval = _pam_get_item(pamh, authtok_flag, &item);
1357 if (retval != PAM_SUCCESS) {
1358 /* very strange. */
1359 _pam_log(pamh, ctrl, LOG_ALERT,
1360 "pam_get_item returned error to unix-read-password"
1362 return retval;
1363 } else if (item != NULL) { /* we have a password! */
1364 *pass = item;
1365 item = NULL;
1366 _pam_log(pamh, ctrl, LOG_DEBUG,
1367 "pam_get_item returned a password");
1368 return PAM_SUCCESS;
1369 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
1370 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
1371 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
1372 && off(WINBIND__OLD_PASSWORD, ctrl)) {
1373 return PAM_AUTHTOK_RECOVER_ERR;
1377 * getting here implies we will have to get the password from the
1378 * user directly.
1382 struct pam_message msg[3], *pmsg[3];
1383 struct pam_response *resp;
1384 int i, replies;
1386 /* prepare to converse */
1388 if (comment != NULL && off(ctrl, WINBIND_SILENT)) {
1389 pmsg[0] = &msg[0];
1390 msg[0].msg_style = PAM_TEXT_INFO;
1391 msg[0].msg = CONST_DISCARD(char *, comment);
1392 i = 1;
1393 } else {
1394 i = 0;
1397 pmsg[i] = &msg[i];
1398 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1399 msg[i++].msg = CONST_DISCARD(char *, prompt1);
1400 replies = 1;
1402 if (prompt2 != NULL) {
1403 pmsg[i] = &msg[i];
1404 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
1405 msg[i++].msg = CONST_DISCARD(char *, prompt2);
1406 ++replies;
1408 /* so call the conversation expecting i responses */
1409 resp = NULL;
1410 retval = converse(pamh, i, pmsg, &resp);
1412 if (resp != NULL) {
1414 /* interpret the response */
1416 if (retval == PAM_SUCCESS) { /* a good conversation */
1418 token = x_strdup(resp[i - replies].resp);
1419 if (token != NULL) {
1420 if (replies == 2) {
1421 /* verify that password entered correctly */
1422 if (!resp[i - 1].resp
1423 || strcmp(token, resp[i - 1].resp)) {
1424 _pam_delete(token); /* mistyped */
1425 retval = PAM_AUTHTOK_RECOVER_ERR;
1426 _make_remark(pamh, ctrl, PAM_ERROR_MSG, MISTYPED_PASS);
1429 } else {
1430 _pam_log(pamh, ctrl, LOG_NOTICE, "could not recover authentication token");
1431 retval = PAM_AUTHTOK_RECOVER_ERR;
1436 * tidy up the conversation (resp_retcode) is ignored
1437 * -- what is it for anyway? AGM
1440 _pam_drop_reply(resp, i);
1442 } else {
1443 retval = (retval == PAM_SUCCESS)
1444 ? PAM_AUTHTOK_RECOVER_ERR : retval;
1448 if (retval != PAM_SUCCESS) {
1449 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1450 "unable to obtain a password");
1451 return retval;
1453 /* 'token' is the entered password */
1455 /* we store this password as an item */
1457 retval = pam_set_item(pamh, authtok_flag, token);
1458 _pam_delete(token); /* clean it up */
1459 if (retval != PAM_SUCCESS ||
1460 (retval = _pam_get_item(pamh, authtok_flag, &item)) != PAM_SUCCESS) {
1462 _pam_log(pamh, ctrl, LOG_CRIT, "error manipulating password");
1463 return retval;
1467 *pass = item;
1468 item = NULL; /* break link to password */
1470 return PAM_SUCCESS;
1473 const char *get_conf_item_string(const pam_handle_t *pamh,
1474 int argc,
1475 const char **argv,
1476 int ctrl,
1477 dictionary *d,
1478 const char *item,
1479 int config_flag)
1481 int i = 0;
1482 const char *parm_opt = NULL;
1483 char *key = NULL;
1485 if (!(ctrl & config_flag)) {
1486 goto out;
1489 /* let the pam opt take precedence over the pam_winbind.conf option */
1491 if (d != NULL) {
1493 if (!asprintf(&key, "global:%s", item)) {
1494 goto out;
1497 parm_opt = iniparser_getstr(d, key);
1498 SAFE_FREE(key);
1501 for ( i=0; i<argc; i++ ) {
1503 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
1504 char *p;
1506 if ( (p = strchr( argv[i], '=' )) == NULL) {
1507 _pam_log(pamh, ctrl, LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
1508 goto out;
1510 _pam_log_debug(pamh, ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
1511 return p + 1;
1515 if (d != NULL) {
1516 _pam_log_debug(pamh, ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
1518 out:
1519 return parm_opt;
1522 const char *get_krb5_cc_type_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1524 return get_conf_item_string(pamh, argc, argv, ctrl, d, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
1527 const char *get_member_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1529 const char *ret = NULL;
1530 ret = get_conf_item_string(pamh, argc, argv, ctrl, d, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
1531 if (ret) {
1532 return ret;
1534 return get_conf_item_string(pamh, argc, argv, ctrl, d, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
1537 PAM_EXTERN
1538 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
1539 int argc, const char **argv)
1541 const char *username;
1542 const char *password;
1543 const char *member = NULL;
1544 const char *cctype = NULL;
1545 int retval = PAM_AUTH_ERR;
1546 dictionary *d = NULL;
1547 char *username_ret = NULL;
1548 char *new_authtok_required = NULL;
1549 char *real_username = NULL;
1551 /* parse arguments */
1552 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1553 if (ctrl == -1) {
1554 retval = PAM_SYSTEM_ERR;
1555 goto out;
1558 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", pamh, ctrl, flags);
1560 /* Get the username */
1561 retval = pam_get_user(pamh, &username, NULL);
1562 if ((retval != PAM_SUCCESS) || (!username)) {
1563 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "can not get the username");
1564 retval = PAM_SERVICE_ERR;
1565 goto out;
1568 #if defined(AIX)
1569 /* Decode the user name since AIX does not support logn user
1570 names by default. The name is encoded as _#uid. */
1572 if ( username[0] == '_' ) {
1573 uid_t id = atoi( &username[1] );
1574 struct passwd *pw = NULL;
1576 if ( (id!=0) && ((pw = getpwuid( id )) != NULL) ) {
1577 real_username = strdup( pw->pw_name );
1580 #endif
1582 if ( !real_username ) {
1583 /* Just making a copy of the username we got from PAM */
1584 if ( (real_username = strdup( username )) == NULL ) {
1585 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1586 "memory allocation failure when copying username");
1587 retval = PAM_SERVICE_ERR;
1588 goto out;
1592 retval = _winbind_read_password(pamh, ctrl, NULL,
1593 "Password: ", NULL,
1594 &password);
1596 if (retval != PAM_SUCCESS) {
1597 _pam_log(pamh, ctrl, LOG_ERR, "Could not retrieve user's password");
1598 retval = PAM_AUTHTOK_ERR;
1599 goto out;
1602 /* Let's not give too much away in the log file */
1604 #ifdef DEBUG_PASSWORD
1605 _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s' with password '%s'",
1606 real_username, password);
1607 #else
1608 _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s'", real_username);
1609 #endif
1611 member = get_member_from_config(pamh, argc, argv, ctrl, d);
1613 cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1615 /* Now use the username to look up password */
1616 retval = winbind_auth_request(pamh, ctrl, username, password, member,
1617 cctype, NULL, NULL, &username_ret);
1619 if (retval == PAM_NEW_AUTHTOK_REQD ||
1620 retval == PAM_AUTHTOK_EXPIRED) {
1622 char *new_authtok_required_during_auth = NULL;
1624 if (!asprintf(&new_authtok_required, "%d", retval)) {
1625 retval = PAM_BUF_ERR;
1626 goto out;
1629 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, new_authtok_required, _pam_winbind_cleanup_func);
1631 retval = PAM_SUCCESS;
1633 if (!asprintf(&new_authtok_required_during_auth, "%d", True)) {
1634 retval = PAM_BUF_ERR;
1635 goto out;
1638 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
1639 new_authtok_required_during_auth, _pam_winbind_cleanup_func);
1641 goto out;
1644 out:
1645 if (username_ret) {
1646 pam_set_item (pamh, PAM_USER, username_ret);
1647 _pam_log_debug(pamh, ctrl, LOG_INFO, "Returned user was '%s'", username_ret);
1648 free(username_ret);
1651 if ( real_username ) {
1652 free( real_username );
1655 if (d) {
1656 iniparser_freedict(d);
1659 if (!new_authtok_required) {
1660 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
1663 if (retval != PAM_SUCCESS) {
1664 _pam_free_data_info3(pamh);
1667 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", pamh, ctrl, retval);
1669 return retval;
1672 PAM_EXTERN
1673 int pam_sm_setcred(pam_handle_t *pamh, int flags,
1674 int argc, const char **argv)
1676 int ret = PAM_SYSTEM_ERR;
1677 dictionary *d = NULL;
1679 /* parse arguments */
1680 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1681 if (ctrl == -1) {
1682 ret = PAM_SYSTEM_ERR;
1683 goto out;
1686 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", pamh, ctrl, flags);
1688 switch (flags & ~PAM_SILENT) {
1690 case PAM_DELETE_CRED:
1691 ret = pam_sm_close_session(pamh, flags, argc, argv);
1692 break;
1693 case PAM_REFRESH_CRED:
1694 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_REFRESH_CRED not implemented");
1695 ret = PAM_SUCCESS;
1696 break;
1697 case PAM_REINITIALIZE_CRED:
1698 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_REINITIALIZE_CRED not implemented");
1699 ret = PAM_SUCCESS;
1700 break;
1701 case PAM_ESTABLISH_CRED:
1702 _pam_log_debug(pamh, ctrl, LOG_WARNING, "PAM_ESTABLISH_CRED not implemented");
1703 ret = PAM_SUCCESS;
1704 break;
1705 default:
1706 ret = PAM_SYSTEM_ERR;
1707 break;
1710 out:
1711 if (d) {
1712 iniparser_freedict(d);
1715 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", pamh, ctrl, ret);
1717 return ret;
1721 * Account management. We want to verify that the account exists
1722 * before returning PAM_SUCCESS
1724 PAM_EXTERN
1725 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
1726 int argc, const char **argv)
1728 const char *username;
1729 int ret = PAM_USER_UNKNOWN;
1730 void *tmp = NULL;
1731 dictionary *d = NULL;
1733 /* parse arguments */
1734 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1735 if (ctrl == -1) {
1736 return PAM_SYSTEM_ERR;
1739 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", pamh, ctrl, flags);
1742 /* Get the username */
1743 ret = pam_get_user(pamh, &username, NULL);
1744 if ((ret != PAM_SUCCESS) || (!username)) {
1745 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"can not get the username");
1746 ret = PAM_SERVICE_ERR;
1747 goto out;
1750 /* Verify the username */
1751 ret = valid_user(pamh, ctrl, username);
1752 switch (ret) {
1753 case -1:
1754 /* some sort of system error. The log was already printed */
1755 ret = PAM_SERVICE_ERR;
1756 goto out;
1757 case 1:
1758 /* the user does not exist */
1759 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", username);
1760 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
1761 ret = PAM_IGNORE;
1762 goto out;
1764 ret = PAM_USER_UNKNOWN;
1765 goto out;
1766 case 0:
1767 pam_get_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (const void **)&tmp);
1768 if (tmp != NULL) {
1769 ret = atoi((const char *)tmp);
1770 switch (ret) {
1771 case PAM_AUTHTOK_EXPIRED:
1772 /* fall through, since new token is required in this case */
1773 case PAM_NEW_AUTHTOK_REQD:
1774 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success but %s is set",
1775 PAM_WINBIND_NEW_AUTHTOK_REQD);
1776 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' needs new password", username);
1777 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
1778 ret = PAM_NEW_AUTHTOK_REQD;
1779 goto out;
1780 default:
1781 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success");
1782 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1783 ret = PAM_SUCCESS;
1784 goto out;
1788 /* Otherwise, the authentication looked good */
1789 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1790 ret = PAM_SUCCESS;
1791 goto out;
1792 default:
1793 /* we don't know anything about this return value */
1794 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (ret = %d, user = '%s')",
1795 ret, username);
1796 ret = PAM_SERVICE_ERR;
1797 goto out;
1800 /* should not be reached */
1801 ret = PAM_IGNORE;
1803 out:
1805 if (d) {
1806 iniparser_freedict(d);
1809 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", pamh, ctrl, ret);
1811 return ret;
1814 PAM_EXTERN
1815 int pam_sm_open_session(pam_handle_t *pamh, int flags,
1816 int argc, const char **argv)
1818 int ret = PAM_SYSTEM_ERR;
1819 dictionary *d = NULL;
1821 /* parse arguments */
1822 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1823 if (ctrl == -1) {
1824 ret = PAM_SYSTEM_ERR;
1825 goto out;
1828 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", pamh, ctrl, flags);
1830 ret = PAM_SUCCESS;
1832 out:
1833 if (d) {
1834 iniparser_freedict(d);
1837 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", pamh, ctrl, ret);
1839 return ret;
1842 PAM_EXTERN
1843 int pam_sm_close_session(pam_handle_t *pamh, int flags,
1844 int argc, const char **argv)
1846 dictionary *d = NULL;
1847 int retval = PAM_SUCCESS;
1849 /* parse arguments */
1850 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1851 if (ctrl == -1) {
1852 retval = PAM_SYSTEM_ERR;
1853 goto out;
1856 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", pamh, ctrl, flags);
1858 if (!(flags & PAM_DELETE_CRED)) {
1859 retval = PAM_SUCCESS;
1860 goto out;
1863 if (ctrl & WINBIND_KRB5_AUTH) {
1865 /* destroy the ccache here */
1866 struct winbindd_request request;
1867 struct winbindd_response response;
1868 const char *user;
1869 const char *ccname = NULL;
1870 struct passwd *pwd = NULL;
1872 ZERO_STRUCT(request);
1873 ZERO_STRUCT(response);
1875 retval = pam_get_user(pamh, &user, "Username: ");
1876 if (retval) {
1877 _pam_log(pamh, ctrl, LOG_ERR, "could not identify user");
1878 goto out;
1881 if (user == NULL) {
1882 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1883 retval = PAM_USER_UNKNOWN;
1884 goto out;
1887 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
1889 ccname = pam_getenv(pamh, "KRB5CCNAME");
1890 if (ccname == NULL) {
1891 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1894 strncpy(request.data.logoff.user, user,
1895 sizeof(request.data.logoff.user) - 1);
1897 if (ccname) {
1898 strncpy(request.data.logoff.krb5ccname, ccname,
1899 sizeof(request.data.logoff.krb5ccname) - 1);
1902 pwd = getpwnam(user);
1903 if (pwd == NULL) {
1904 retval = PAM_USER_UNKNOWN;
1905 goto out;
1907 request.data.logoff.uid = pwd->pw_uid;
1909 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1911 retval = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1914 out:
1915 if (d) {
1916 iniparser_freedict(d);
1919 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", pamh, ctrl, retval);
1921 return retval;
1925 * evaluate whether we need to re-authenticate with kerberos after a password change
1927 * @param pamh PAM handle
1928 * @param ctrl PAM winbind options.
1929 * @param user The username
1931 * @return boolean Returns True if required, False if not.
1934 static BOOL _pam_require_krb5_auth_after_chauthtok(pam_handle_t *pamh, int ctrl, const char *user)
1937 /* Make sure that we only do this if
1938 * a) the chauthtok got initiated during a logon attempt (authenticate->acct_mgmt->chauthtok)
1939 * b) any later password change via the "passwd" command if done by the user itself
1942 char *new_authtok_reqd_during_auth = NULL;
1943 struct passwd *pwd = NULL;
1945 if (!(ctrl & WINBIND_KRB5_AUTH)) {
1946 return False;
1949 _pam_get_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH, &new_authtok_reqd_during_auth);
1950 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH, NULL, NULL);
1952 if (new_authtok_reqd_during_auth) {
1953 return True;
1956 pwd = getpwnam(user);
1957 if (!pwd) {
1958 return False;
1961 if (getuid() == pwd->pw_uid) {
1962 return True;
1965 return False;
1969 PAM_EXTERN
1970 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1971 int argc, const char **argv)
1973 unsigned int lctrl;
1974 int ret;
1975 unsigned int ctrl;
1977 /* <DO NOT free() THESE> */
1978 const char *user;
1979 char *pass_old, *pass_new;
1980 /* </DO NOT free() THESE> */
1982 char *Announce;
1984 int retry = 0;
1985 dictionary *d = NULL;
1986 char *username_ret = NULL;
1987 struct winbindd_response response;
1989 ZERO_STRUCT(response);
1991 ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1992 if (ctrl == -1) {
1993 ret = PAM_SYSTEM_ERR;
1994 goto out;
1997 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", pamh, ctrl, flags);
1999 /* clearing offline bit for the auth in the password change */
2000 ctrl &= ~WINBIND_CACHED_LOGIN;
2003 * First get the name of a user
2005 ret = pam_get_user(pamh, &user, "Username: ");
2006 if (ret) {
2007 _pam_log(pamh, ctrl, LOG_ERR,
2008 "password - could not identify user");
2009 goto out;
2012 if (user == NULL) {
2013 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
2014 ret = PAM_USER_UNKNOWN;
2015 goto out;
2018 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
2020 /* check if this is really a user in winbindd, not only in NSS */
2021 ret = valid_user(pamh, ctrl, user);
2022 switch (ret) {
2023 case 1:
2024 ret = PAM_USER_UNKNOWN;
2025 goto out;
2026 case -1:
2027 ret = PAM_SYSTEM_ERR;
2028 goto out;
2029 default:
2030 break;
2034 * obtain and verify the current password (OLDAUTHTOK) for
2035 * the user.
2038 if (flags & PAM_PRELIM_CHECK) {
2039 time_t pwdlastset_prelim = 0;
2041 /* instruct user what is happening */
2042 #define greeting "Changing password for "
2043 Announce = (char *) malloc(sizeof(greeting) + strlen(user));
2044 if (Announce == NULL) {
2045 _pam_log(pamh, ctrl, LOG_CRIT, "password - out of memory");
2046 ret = PAM_BUF_ERR;
2047 goto out;
2049 (void) strcpy(Announce, greeting);
2050 (void) strcpy(Announce + sizeof(greeting) - 1, user);
2051 #undef greeting
2053 lctrl = ctrl | WINBIND__OLD_PASSWORD;
2054 ret = _winbind_read_password(pamh, lctrl,
2055 Announce,
2056 "(current) NT password: ",
2057 NULL,
2058 (const char **) &pass_old);
2059 if (ret != PAM_SUCCESS) {
2060 _pam_log(pamh, ctrl, LOG_NOTICE, "password - (old) token not obtained");
2061 goto out;
2064 /* verify that this is the password for this user */
2066 ret = winbind_auth_request(pamh, ctrl, user, pass_old,
2067 NULL, NULL, &response, &pwdlastset_prelim, NULL);
2069 if (ret != PAM_ACCT_EXPIRED &&
2070 ret != PAM_AUTHTOK_EXPIRED &&
2071 ret != PAM_NEW_AUTHTOK_REQD &&
2072 ret != PAM_SUCCESS) {
2073 pass_old = NULL;
2074 goto out;
2077 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET, (void *)pwdlastset_prelim, NULL);
2079 ret = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
2080 pass_old = NULL;
2081 if (ret != PAM_SUCCESS) {
2082 _pam_log(pamh, ctrl, LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
2084 } else if (flags & PAM_UPDATE_AUTHTOK) {
2086 time_t pwdlastset_update = 0;
2089 * obtain the proposed password
2093 * get the old token back.
2096 ret = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
2098 if (ret != PAM_SUCCESS) {
2099 _pam_log(pamh, ctrl, LOG_NOTICE, "user not authenticated");
2100 goto out;
2103 lctrl = ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
2105 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
2106 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
2108 retry = 0;
2109 ret = PAM_AUTHTOK_ERR;
2110 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
2112 * use_authtok is to force the use of a previously entered
2113 * password -- needed for pluggable password strength checking
2116 ret = _winbind_read_password(pamh, lctrl,
2117 NULL,
2118 "Enter new NT password: ",
2119 "Retype new NT password: ",
2120 (const char **) &pass_new);
2122 if (ret != PAM_SUCCESS) {
2123 _pam_log_debug(pamh, ctrl, LOG_ALERT
2124 ,"password - new password not obtained");
2125 pass_old = NULL;/* tidy up */
2126 goto out;
2130 * At this point we know who the user is and what they
2131 * propose as their new password. Verify that the new
2132 * password is acceptable.
2135 if (pass_new[0] == '\0') {/* "\0" password = NULL */
2136 pass_new = NULL;
2141 * By reaching here we have approved the passwords and must now
2142 * rebuild the password database file.
2144 _pam_get_data( pamh, PAM_WINBIND_PWD_LAST_SET,
2145 &pwdlastset_update);
2147 ret = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new, pwdlastset_update);
2148 if (ret) {
2149 _pam_overwrite(pass_new);
2150 _pam_overwrite(pass_old);
2151 pass_old = pass_new = NULL;
2152 goto out;
2155 if (_pam_require_krb5_auth_after_chauthtok(pamh, ctrl, user)) {
2157 const char *member = get_member_from_config(pamh, argc, argv, ctrl, d);
2158 const char *cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
2160 ret = winbind_auth_request(pamh, ctrl, user, pass_new,
2161 member, cctype, &response, NULL, &username_ret);
2162 _pam_overwrite(pass_new);
2163 _pam_overwrite(pass_old);
2164 pass_old = pass_new = NULL;
2166 if (ret == PAM_SUCCESS) {
2168 /* warn a user if the password is about to expire soon */
2169 _pam_warn_password_expiry(pamh, ctrl, &response, NULL);
2171 /* set some info3 info for other modules in the stack */
2172 _pam_set_data_info3(pamh, ctrl, &response);
2174 /* put krb5ccname into env */
2175 _pam_setup_krb5_env(pamh, ctrl, response.data.auth.krb5ccname);
2177 if (username_ret) {
2178 pam_set_item (pamh, PAM_USER, username_ret);
2179 _pam_log_debug(pamh, ctrl, LOG_INFO, "Returned user was '%s'", username_ret);
2180 free(username_ret);
2184 goto out;
2186 } else {
2187 ret = PAM_SERVICE_ERR;
2190 out:
2191 if (d) {
2192 iniparser_freedict(d);
2195 /* Deal with offline errors. */
2196 PAM_WB_REMARK_CHECK_RESPONSE(pamh, ctrl, response, "NT_STATUS_NO_LOGON_SERVERS");
2197 PAM_WB_REMARK_CHECK_RESPONSE(pamh, ctrl, response, "NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND");
2198 PAM_WB_REMARK_CHECK_RESPONSE(pamh, ctrl, response, "NT_STATUS_ACCESS_DENIED");
2200 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", pamh, ctrl, ret);
2202 return ret;
2205 #ifdef PAM_STATIC
2207 /* static module data */
2209 struct pam_module _pam_winbind_modstruct = {
2210 MODULE_NAME,
2211 pam_sm_authenticate,
2212 pam_sm_setcred,
2213 pam_sm_acct_mgmt,
2214 pam_sm_open_session,
2215 pam_sm_close_session,
2216 pam_sm_chauthtok
2219 #endif
2222 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
2223 * Copyright (c) Tim Potter <tpot@samba.org> 2000
2224 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
2225 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2007
2226 * Copyright (c) Jan Rêkorajski 1999.
2227 * Copyright (c) Andrew G. Morgan 1996-8.
2228 * Copyright (c) Alex O. Yuriev, 1996.
2229 * Copyright (c) Cristian Gafton 1996.
2230 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
2232 * Redistribution and use in source and binary forms, with or without
2233 * modification, are permitted provided that the following conditions
2234 * are met:
2235 * 1. Redistributions of source code must retain the above copyright
2236 * notice, and the entire permission notice in its entirety,
2237 * including the disclaimer of warranties.
2238 * 2. Redistributions in binary form must reproduce the above copyright
2239 * notice, this list of conditions and the following disclaimer in the
2240 * documentation and/or other materials provided with the distribution.
2241 * 3. The name of the author may not be used to endorse or promote
2242 * products derived from this software without specific prior
2243 * written permission.
2245 * ALTERNATIVELY, this product may be distributed under the terms of
2246 * the GNU Public License, in which case the provisions of the GPL are
2247 * required INSTEAD OF the above restrictions. (This clause is
2248 * necessary due to a potential bad interaction between the GPL and
2249 * the restrictions contained in a BSD-style copyright.)
2251 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
2252 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2253 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2254 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2255 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2256 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2257 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2258 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2259 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2260 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2261 * OF THE POSSIBILITY OF SUCH DAMAGE.