source: branches/samba-3.0/source/nsswitch/pam_winbind.c

Last change on this file was 165, checked in by Paul Smedley, 17 years ago

Add 'missing' 3.0.34 diffs

File size: 60.4 KB
Line 
1/* pam_winbind module
2
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
7
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)
11*/
12
13#include "pam_winbind.h"
14
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)
20
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)
26
27/* data tokens */
28
29#define MAX_PASSWD_TRIES 3
30
31/*
32 * Work around the pam API that has functions with void ** as parameters.
33 * These lead to strict aliasing warnings with gcc.
34 */
35static int _pam_get_item(const pam_handle_t *pamh, int item_type,
36 const void *_item)
37{
38 const void **item = (const void **)_item;
39 return pam_get_item(pamh, item_type, item);
40}
41static int _pam_get_data(const pam_handle_t *pamh,
42 const char *module_data_name, const void *_data)
43{
44 const void **data = (const void **)_data;
45 return pam_get_data(pamh, module_data_name, data);
46}
47
48/* some syslogging */
49
50#ifdef HAVE_PAM_VSYSLOG
51static void _pam_log_int(const pam_handle_t *pamh, int err, const char *format, va_list args)
52{
53 pam_vsyslog(pamh, err, format, args);
54}
55#else
56static void _pam_log_int(const pam_handle_t *pamh, int err, const char *format, va_list args)
57{
58 char *format2 = NULL;
59 const char *service;
60
61 _pam_get_item(pamh, PAM_SERVICE, &service);
62
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;
68 }
69
70 sprintf(format2, "%s(%s): %s", MODULE_NAME, service, format);
71 vsyslog(err, format2, args);
72 SAFE_FREE(format2);
73}
74#endif /* HAVE_PAM_VSYSLOG */
75
76static BOOL _pam_log_is_silent(int ctrl)
77{
78 return on(ctrl, WINBIND_SILENT);
79}
80
81static void _pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
82static void _pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
83{
84 va_list args;
85
86 if (_pam_log_is_silent(ctrl)) {
87 return;
88 }
89
90 va_start(args, format);
91 _pam_log_int(pamh, err, format, args);
92 va_end(args);
93}
94
95static BOOL _pam_log_is_debug_enabled(int ctrl)
96{
97 if (ctrl == -1) {
98 return False;
99 }
100
101 if (_pam_log_is_silent(ctrl)) {
102 return False;
103 }
104
105 if (!(ctrl & WINBIND_DEBUG_ARG)) {
106 return False;
107 }
108
109 return True;
110}
111
112static BOOL _pam_log_is_debug_state_enabled(int ctrl)
113{
114 if (!(ctrl & WINBIND_DEBUG_STATE)) {
115 return False;
116 }
117
118 return _pam_log_is_debug_enabled(ctrl);
119}
120
121static void _pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
122static void _pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
123{
124 va_list args;
125
126 if (!_pam_log_is_debug_enabled(ctrl)) {
127 return;
128 }
129
130 va_start(args, format);
131 _pam_log_int(pamh, err, format, args);
132 va_end(args);
133}
134
135static void _pam_log_state_datum(const pam_handle_t *pamh, int ctrl, int item_type, const char *key, int is_string)
136{
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);
142 }
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);
149 }
150 }
151}
152
153#define _PAM_LOG_STATE_DATA_POINTER(pamh, ctrl, module_data_name) \
154 _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 0)
155
156#define _PAM_LOG_STATE_DATA_STRING(pamh, ctrl, module_data_name) \
157 _pam_log_state_datum(pamh, ctrl, 0, module_data_name, 1)
158
159#define _PAM_LOG_STATE_ITEM_POINTER(pamh, ctrl, item_type) \
160 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 0)
161
162#define _PAM_LOG_STATE_ITEM_STRING(pamh, ctrl, item_type) \
163 _pam_log_state_datum(pamh, ctrl, item_type, #item_type, 1)
164
165#ifdef DEBUG_PASSWORD
166#define _LOG_PASSWORD_AS_STRING 1
167#else
168#define _LOG_PASSWORD_AS_STRING 0
169#endif
170
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)
173
174static void _pam_log_state(const pam_handle_t *pamh, int ctrl)
175{
176 if (!_pam_log_is_debug_state_enabled(ctrl)) {
177 return;
178 }
179
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
195
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);
203}
204
205static int _pam_parse(const pam_handle_t *pamh, int flags, int argc, const char **argv, dictionary **result_d)
206{
207 int ctrl = 0;
208 const char *config_file = NULL;
209 int i;
210 const char **v;
211 dictionary *d = NULL;
212
213 if (flags & PAM_SILENT) {
214 ctrl |= WINBIND_SILENT;
215 }
216
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;
222 }
223 }
224
225 if (config_file == NULL) {
226 config_file = PAM_WINBIND_CONFIG_FILE;
227 }
228
229 d = iniparser_load(config_file);
230 if (d == NULL) {
231 goto config_from_pam;
232 }
233
234 if (iniparser_getboolean(d, "global:debug", False)) {
235 ctrl |= WINBIND_DEBUG_ARG;
236 }
237
238 if (iniparser_getboolean(d, "global:debug_state", False)) {
239 ctrl |= WINBIND_DEBUG_STATE;
240 }
241
242 if (iniparser_getboolean(d, "global:cached_login", False)) {
243 ctrl |= WINBIND_CACHED_LOGIN;
244 }
245
246 if (iniparser_getboolean(d, "global:krb5_auth", False)) {
247 ctrl |= WINBIND_KRB5_AUTH;
248 }
249
250 if (iniparser_getboolean(d, "global:silent", False)) {
251 ctrl |= WINBIND_SILENT;
252 }
253
254 if (iniparser_getstr(d, "global:krb5_ccache_type") != NULL) {
255 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
256 }
257
258 if ((iniparser_getstr(d, "global:require-membership-of") != NULL) ||
259 (iniparser_getstr(d, "global:require_membership_of") != NULL)) {
260 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
261 }
262
263 if (iniparser_getboolean(d, "global:try_first_pass", False)) {
264 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
265 }
266
267config_from_pam:
268 /* step through arguments */
269 for (i=argc,v=argv; i-- > 0; ++v) {
270
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;
297 }
298
299 }
300
301 if (result_d) {
302 *result_d = d;
303 } else {
304 if (d) {
305 iniparser_freedict(d);
306 }
307 }
308
309 return ctrl;
310};
311
312static void _pam_winbind_cleanup_func(pam_handle_t *pamh, void *data, int error_status)
313{
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);
317 }
318 SAFE_FREE(data);
319}
320
321
322static 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}
345};
346
347const char *_get_ntstatus_error_string(const char *nt_status_string)
348{
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;
353 }
354 }
355 return NULL;
356}
357
358/* --- authentication management functions --- */
359
360/* Attempt a conversation */
361
362static int converse(pam_handle_t *pamh, int nargs,
363 struct pam_message **message,
364 struct pam_response **response)
365{
366 int retval;
367 struct pam_conv *conv;
368
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);
373 }
374
375 return retval; /* propagate error status */
376}
377
378
379static int _make_remark(pam_handle_t * pamh, int flags, int type, const char *text)
380{
381 int retval = PAM_SUCCESS;
382
383 struct pam_message *pmsg[1], msg[1];
384 struct pam_response *resp;
385
386 if (flags & WINBIND_SILENT) {
387 return PAM_SUCCESS;
388 }
389
390 pmsg[0] = &msg[0];
391 msg[0].msg = CONST_DISCARD(char *, text);
392 msg[0].msg_style = type;
393
394 resp = NULL;
395 retval = converse(pamh, 1, pmsg, &resp);
396
397 if (resp) {
398 _pam_drop_reply(resp, 1);
399 }
400 return retval;
401}
402
403static int _make_remark_v(pam_handle_t * pamh, int flags, int type, const char *format, va_list args)
404{
405 char *var;
406 int ret;
407
408 ret = vasprintf(&var, format, args);
409 if (ret < 0) {
410 _pam_log(pamh, 0, LOG_ERR, "memory allocation failure");
411 return ret;
412 }
413
414 ret = _make_remark(pamh, flags, type, var);
415 SAFE_FREE(var);
416 return ret;
417}
418
419static int _make_remark_format(pam_handle_t * pamh, int flags, int type, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
420static int _make_remark_format(pam_handle_t * pamh, int flags, int type, const char *format, ...)
421{
422 int ret;
423 va_list args;
424
425 va_start(args, format);
426 ret = _make_remark_v(pamh, flags, type, format, args);
427 va_end(args);
428 return ret;
429}
430
431static 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)
435{
436 /* Fill in request and send down pipe */
437 init_request(request, req_type);
438
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;
443 }
444
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;
450 }
451
452 /* We are done with the socket - close it and avoid mischeif */
453 close_sock();
454
455 /* Copy reply data from socket */
456 if (response->result == WINBINDD_OK) {
457 return PAM_SUCCESS;
458 }
459
460 /* no need to check for pam_error codes for getpwnam() */
461 switch (req_type) {
462
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");
470 }
471 return PAM_USER_UNKNOWN;
472 default:
473 break;
474 }
475
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;
483 }
484
485 _pam_log(pamh, ctrl, LOG_ERR, "request failed, but PAM error 0!");
486
487 return PAM_SERVICE_ERR;
488}
489
490static 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)
496{
497 int retval;
498
499 retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
500
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;
523 }
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;
539 }
540
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;
547 }
548}
549
550/**
551 * send a password expiry message if required
552 *
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.
558 *
559 * @return boolean Returns True if message has been sent, False if not.
560 */
561
562static BOOL _pam_send_password_expiry_message(pam_handle_t *pamh, int ctrl, time_t next_change, time_t now, BOOL *already_expired)
563{
564 int days = 0;
565 struct tm tm_now, tm_next_change;
566
567 if (already_expired) {
568 *already_expired = False;
569 }
570
571 if (next_change <= now) {
572 PAM_WB_REMARK_DIRECT(pamh, ctrl, "NT_STATUS_PASSWORD_EXPIRED");
573 if (already_expired) {
574 *already_expired = True;
575 }
576 return True;
577 }
578
579 if ((next_change < 0) ||
580 (next_change > now + DAYS_TO_WARN_BEFORE_PWD_EXPIRES * SECONDS_PER_DAY)) {
581 return False;
582 }
583
584 if ((localtime_r(&now, &tm_now) == NULL) ||
585 (localtime_r(&next_change, &tm_next_change) == NULL)) {
586 return False;
587 }
588
589 days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) - (tm_now.tm_yday+tm_now.tm_year*365);
590
591 if (days == 0) {
592 _make_remark(pamh, ctrl, PAM_TEXT_INFO, "Your password expires today");
593 return True;
594 }
595
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;
600 }
601
602 return False;
603}
604
605/**
606 * Send a warning if the password expires in the near future
607 *
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?
612 *
613 * @return void.
614 */
615
616static void _pam_warn_password_expiry(pam_handle_t *pamh,
617 int flags,
618 const struct winbindd_response *response,
619 BOOL *already_expired)
620{
621 time_t now = time(NULL);
622 time_t next_change = 0;
623
624 if (already_expired) {
625 *already_expired = False;
626 }
627
628 /* accounts with ACB_PWNOEXP set never receive a warning */
629 if (response->data.auth.info3.acct_flags & ACB_PWNOEXP) {
630 return;
631 }
632
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;
636 }
637
638 /* check if the info3 must change timestamp has been set */
639 next_change = response->data.auth.info3.pass_must_change_time;
640
641 if (_pam_send_password_expiry_message(pamh, flags, next_change, now,
642 already_expired)) {
643 return;
644 }
645
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;
651 }
652
653 next_change = response->data.auth.info3.pass_last_set_time +
654 response->data.auth.policy.expire;
655
656 if (_pam_send_password_expiry_message(pamh, flags, next_change, now,
657 already_expired)) {
658 return;
659 }
660
661 /* no warning sent */
662}
663
664#define IS_SID_STRING(name) (strncmp("S-", name, 2) == 0)
665
666static BOOL safe_append_string(char *dest,
667 const char *src,
668 int dest_buffer_size)
669/**
670 * Append a string, making sure not to overflow and to always return a NULL-terminated
671 * string.
672 *
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.
676 *
677 * @return False if dest buffer is not big enough (no bytes copied), True on success.
678 */
679{
680 int dest_length = strlen(dest);
681 int src_length = strlen(src);
682
683 if ( dest_length + src_length + 1 > dest_buffer_size ) {
684 return False;
685 }
686
687 memcpy(dest + dest_length, src, src_length + 1);
688 return True;
689}
690
691static 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)
697/**
698 * Convert a names into a SID string, appending it to a buffer.
699 *
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).
706 *
707 * @return False on failure, True on success.
708 */
709{
710 const char* sid_string;
711 struct winbindd_response sid_response;
712
713 /* lookup name? */
714 if (IS_SID_STRING(name)) {
715 sid_string = name;
716 } else {
717 struct winbindd_request sid_request;
718
719 ZERO_STRUCT(sid_request);
720 ZERO_STRUCT(sid_response);
721
722 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", name);
723
724 /* fortunatly winbindd can handle non-separated names */
725 strncpy(sid_request.data.name.name, name,
726 sizeof(sid_request.data.name.name) - 1);
727
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;
731 }
732
733 sid_string = sid_response.data.sid.sid;
734 }
735
736 if (!safe_append_string(sid_list_buffer, sid_string, sid_list_buffer_size)) {
737 return False;
738 }
739
740 return True;
741}
742
743static 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)
749/**
750 * Convert a list of names into a list of sids.
751 *
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).
758 *
759 * @return False on failure, True on success.
760 */
761{
762 BOOL result = False;
763 char *current_name = NULL;
764 const char *search_location;
765 const char *comma;
766
767 if ( sid_list_buffer_size > 0 ) {
768 sid_list_buffer[0] = 0;
769 }
770
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;
776 }
777
778 if (!winbind_name_to_sid_string(pamh, ctrl, user, current_name, sid_list_buffer, sid_list_buffer_size)) {
779 goto out;
780 }
781
782 SAFE_FREE(current_name);
783
784 if (!safe_append_string(sid_list_buffer, ",", sid_list_buffer_size)) {
785 goto out;
786 }
787
788 search_location = comma + 1;
789 }
790
791 if (!winbind_name_to_sid_string(pamh, ctrl, user, search_location, sid_list_buffer, sid_list_buffer_size)) {
792 goto out;
793 }
794
795 result = True;
796
797out:
798 SAFE_FREE(current_name);
799 return result;
800}
801
802/**
803 * put krb5ccname variable into environment
804 *
805 * @param pamh PAM handle
806 * @param ctrl PAM winbind options.
807 * @param krb5ccname env variable retrieved from winbindd.
808 *
809 * @return void.
810 */
811
812static void _pam_setup_krb5_env(pam_handle_t *pamh, int ctrl, const char *krb5ccname)
813{
814 char var[PATH_MAX];
815 int ret;
816
817 if (off(ctrl, WINBIND_KRB5_AUTH)) {
818 return;
819 }
820
821 if (!krb5ccname || (strlen(krb5ccname) == 0)) {
822 return;
823 }
824
825 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "request returned KRB5CCNAME: %s", krb5ccname);
826
827 if (snprintf(var, sizeof(var), "KRB5CCNAME=%s", krb5ccname) == -1) {
828 return;
829 }
830
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));
835 }
836}
837
838/**
839 * Set string into the PAM stack.
840 *
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.
845 *
846 * @return void.
847 */
848
849static void _pam_set_data_string(pam_handle_t *pamh, int ctrl, const char *data_name, const char *value)
850{
851 int ret;
852
853 if ( !data_name || !value || (strlen(data_name) == 0) || (strlen(value) == 0) ) {
854 return;
855 }
856
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));
861 }
862
863}
864
865/**
866 * Set info3 strings into the PAM stack.
867 *
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.
872 *
873 * @return void.
874 */
875
876static void _pam_set_data_info3(pam_handle_t *pamh, int ctrl, struct winbindd_response *response)
877{
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);
882}
883
884/**
885 * Free info3 strings in the PAM stack.
886 *
887 * @param pamh PAM handle
888 *
889 * @return void.
890 */
891
892static void _pam_free_data_info3(pam_handle_t *pamh)
893{
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);
898}
899
900/**
901 * Send PAM_ERROR_MSG for cached or grace logons.
902 *
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.
907 *
908 * @return void.
909 */
910
911static void _pam_warn_logon_type(pam_handle_t *pamh, int ctrl, const char *username, uint32 info3_user_flgs)
912{
913 /* inform about logon type */
914 if (PAM_WB_GRACE_LOGON(info3_user_flgs)) {
915
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);
920
921 } else if (PAM_WB_CACHED_LOGON(info3_user_flgs)) {
922
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);
927 }
928}
929
930/**
931 * Compose Password Restriction String for a PAM_ERROR_MSG conversation.
932 *
933 * @param response The struct winbindd_response.
934 *
935 * @return string (caller needs to free).
936 */
937
938static char *_pam_compose_pwd_restriction_string(struct winbindd_response *response)
939{
940 char *str = NULL;
941 size_t offset = 0, ret = 0, str_size = 1024;
942
943 str = (char *)malloc(str_size);
944 if (!str) {
945 return NULL;
946 }
947
948 memset(str, '\0', str_size);
949
950 offset = snprintf(str, str_size, "Your password ");
951 if (offset == -1) {
952 goto failed;
953 }
954
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;
961 }
962 offset += ret;
963 }
964
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;
971 }
972 offset += ret;
973 }
974
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;
981 }
982 offset += ret;
983 }
984
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;
990 }
991
992 return str;
993
994 failed:
995 SAFE_FREE(str);
996 return NULL;
997}
998
999/* talk to winbindd */
1000static 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)
1009{
1010 struct winbindd_request request;
1011 struct winbindd_response response;
1012 int ret;
1013 BOOL already_expired = False;
1014
1015 ZERO_STRUCT(request);
1016 ZERO_STRUCT(response);
1017
1018 if (pwd_last_set) {
1019 *pwd_last_set = 0;
1020 }
1021
1022 strncpy(request.data.auth.user, user,
1023 sizeof(request.data.auth.user)-1);
1024
1025 strncpy(request.data.auth.pass, pass,
1026 sizeof(request.data.auth.pass)-1);
1027
1028 request.data.auth.krb5_cc_type[0] = '\0';
1029 request.data.auth.uid = -1;
1030
1031 request.flags = WBFLAG_PAM_INFO3_TEXT |
1032 WBFLAG_PAM_GET_PWD_POLICY |
1033 WBFLAG_PAM_CONTACT_TRUSTDOM;
1034
1035 if (ctrl & (WINBIND_KRB5_AUTH|WINBIND_CACHED_LOGIN)) {
1036 struct passwd *pwd = NULL;
1037
1038 pwd = getpwnam(user);
1039 if (pwd == NULL) {
1040 return PAM_USER_UNKNOWN;
1041 }
1042 request.data.auth.uid = pwd->pw_uid;
1043 }
1044
1045 if (ctrl & WINBIND_KRB5_AUTH) {
1046
1047 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling krb5 login flag\n");
1048
1049 request.flags |= WBFLAG_PAM_KRB5 | WBFLAG_PAM_FALLBACK_AFTER_KRB5;
1050 }
1051
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;
1055 }
1056
1057 if (user_ret) {
1058 *user_ret = NULL;
1059 request.flags |= WBFLAG_PAM_UNIX_NAME;
1060 }
1061
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);
1066 }
1067
1068 request.data.auth.require_membership_of_sid[0] = '\0';
1069
1070 if (member != NULL) {
1071
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))) {
1075
1076 _pam_log_debug(pamh, ctrl, LOG_ERR, "failed to serialize membership of sid \"%s\"\n", member);
1077 return PAM_AUTH_ERR;
1078 }
1079 }
1080
1081 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
1082
1083 if (pwd_last_set) {
1084 *pwd_last_set = response.data.auth.info3.pass_last_set_time;
1085 }
1086
1087 if (p_response) {
1088 /* We want to process the response in the caller. */
1089 *p_response = response;
1090 return ret;
1091 }
1092
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");
1108 }
1109
1110 if (ret == PAM_SUCCESS) {
1111
1112 /* warn a user if the password is about to expire soon */
1113 _pam_warn_password_expiry(pamh, ctrl, &response, &already_expired);
1114
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));
1123
1124 return PAM_AUTHTOK_EXPIRED;
1125 }
1126
1127 /* inform about logon type */
1128 _pam_warn_logon_type(pamh, ctrl, user, response.data.auth.info3.user_flgs);
1129
1130 /* set some info3 info for other modules in the stack */
1131 _pam_set_data_info3(pamh, ctrl, &response);
1132
1133 /* put krb5ccname into env */
1134 _pam_setup_krb5_env(pamh, ctrl, response.data.auth.krb5ccname);
1135
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;
1140 }
1141 }
1142
1143 return ret;
1144}
1145
1146/* talk to winbindd */
1147static 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)
1153{
1154 struct winbindd_request request;
1155 struct winbindd_response response;
1156 int ret;
1157
1158 ZERO_STRUCT(request);
1159 ZERO_STRUCT(response);
1160
1161 if (request.data.chauthtok.user == NULL) return -2;
1162
1163 strncpy(request.data.chauthtok.user, user,
1164 sizeof(request.data.chauthtok.user) - 1);
1165
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';
1171 }
1172
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';
1178 }
1179
1180 if (ctrl & WINBIND_KRB5_AUTH) {
1181 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1182 }
1183
1184 if (ctrl & WINBIND_CACHED_LOGIN) {
1185 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
1186 }
1187
1188 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
1189
1190 if (ret == PAM_SUCCESS) {
1191 return ret;
1192 }
1193
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");
1198
1199 /* TODO: tell the min pwd length ? */
1200 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_TOO_SHORT");
1201
1202 /* TODO: tell the minage ? */
1203 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_TOO_RECENT");
1204
1205 /* TODO: tell the history length ? */
1206 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, ctrl, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
1207
1208 if (!strcasecmp(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
1209
1210 char *pwd_restriction_string = NULL;
1211
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");
1220 }
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;
1236 }
1237
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);
1242 }
1243 }
1244
1245 return ret;
1246}
1247
1248/*
1249 * Checks if a user has an account
1250 *
1251 * return values:
1252 * 1 = User not found
1253 * 0 = OK
1254 * -1 = System error
1255 */
1256static int valid_user(pam_handle_t *pamh, int ctrl, const char *user)
1257{
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. */
1261
1262 struct passwd *pwd = NULL;
1263 struct winbindd_request request;
1264 struct winbindd_response response;
1265 int ret;
1266
1267 ZERO_STRUCT(request);
1268 ZERO_STRUCT(response);
1269
1270 pwd = getpwnam(user);
1271 if (pwd == NULL) {
1272 return 1;
1273 }
1274
1275 strncpy(request.data.username, user,
1276 sizeof(request.data.username) - 1);
1277
1278 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_GETPWNAM, &request, &response, user);
1279
1280 switch (ret) {
1281 case PAM_USER_UNKNOWN:
1282 return 1;
1283 case PAM_SUCCESS:
1284 return 0;
1285 default:
1286 break;
1287 }
1288 return -1;
1289}
1290
1291static char *_pam_delete(register char *xx)
1292{
1293 _pam_overwrite(xx);
1294 _pam_drop(xx);
1295 return NULL;
1296}
1297
1298/*
1299 * obtain a password from the user
1300 */
1301
1302static 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)
1308{
1309 int authtok_flag;
1310 int retval;
1311 const char *item;
1312 char *token;
1313
1314 _pam_log(pamh, ctrl, LOG_DEBUG, "getting password (0x%08x)", ctrl);
1315
1316 /*
1317 * make sure nothing inappropriate gets returned
1318 */
1319
1320 *pass = token = NULL;
1321
1322 /*
1323 * which authentication token are we getting?
1324 */
1325
1326 authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
1327
1328 /*
1329 * should we obtain the password from a PAM item ?
1330 */
1331
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"
1338 );
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;
1351 }
1352 }
1353 /*
1354 * getting here implies we will have to get the password from the
1355 * user directly.
1356 */
1357
1358 {
1359 struct pam_message msg[3], *pmsg[3];
1360 struct pam_response *resp;
1361 int i, replies;
1362
1363 /* prepare to converse */
1364
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;
1372 }
1373
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;
1378
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;
1384 }
1385 /* so call the conversation expecting i responses */
1386 resp = NULL;
1387 retval = converse(pamh, i, pmsg, &resp);
1388
1389 if (resp != NULL) {
1390
1391 /* interpret the response */
1392
1393 if (retval == PAM_SUCCESS) { /* a good conversation */
1394
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);
1404 }
1405 }
1406 } else {
1407 _pam_log(pamh, ctrl, LOG_NOTICE, "could not recover authentication token");
1408 retval = PAM_AUTHTOK_RECOVER_ERR;
1409 }
1410
1411 }
1412 /*
1413 * tidy up the conversation (resp_retcode) is ignored
1414 * -- what is it for anyway? AGM
1415 */
1416
1417 _pam_drop_reply(resp, i);
1418
1419 } else {
1420 retval = (retval == PAM_SUCCESS)
1421 ? PAM_AUTHTOK_RECOVER_ERR : retval;
1422 }
1423 }
1424
1425 if (retval != PAM_SUCCESS) {
1426 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1427 "unable to obtain a password");
1428 return retval;
1429 }
1430 /* 'token' is the entered password */
1431
1432 /* we store this password as an item */
1433
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) {
1438
1439 _pam_log(pamh, ctrl, LOG_CRIT, "error manipulating password");
1440 return retval;
1441
1442 }
1443
1444 *pass = item;
1445 item = NULL; /* break link to password */
1446
1447 return PAM_SUCCESS;
1448}
1449
1450const 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)
1457{
1458 int i = 0;
1459 const char *parm_opt = NULL;
1460 char *key = NULL;
1461
1462 if (!(ctrl & config_flag)) {
1463 goto out;
1464 }
1465
1466 /* let the pam opt take precedence over the pam_winbind.conf option */
1467
1468 if (d != NULL) {
1469
1470 if (!asprintf(&key, "global:%s", item)) {
1471 goto out;
1472 }
1473
1474 parm_opt = iniparser_getstr(d, key);
1475 SAFE_FREE(key);
1476 }
1477
1478 for ( i=0; i<argc; i++ ) {
1479
1480 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
1481 char *p;
1482
1483 if ( (p = strchr( argv[i], '=' )) == NULL) {
1484 _pam_log(pamh, ctrl, LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
1485 goto out;
1486 }
1487 _pam_log_debug(pamh, ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
1488 return p + 1;
1489 }
1490 }
1491
1492 if (d != NULL) {
1493 _pam_log_debug(pamh, ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
1494 }
1495out:
1496 return parm_opt;
1497}
1498
1499const char *get_krb5_cc_type_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1500{
1501 return get_conf_item_string(pamh, argc, argv, ctrl, d, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
1502}
1503
1504const char *get_member_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
1505{
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;
1510 }
1511 return get_conf_item_string(pamh, argc, argv, ctrl, d, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
1512}
1513
1514PAM_EXTERN
1515int pam_sm_authenticate(pam_handle_t *pamh, int flags,
1516 int argc, const char **argv)
1517{
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;
1527
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;
1533 }
1534
1535 _PAM_LOG_FUNCTION_ENTER("pam_sm_authenticate", pamh, ctrl, flags);
1536
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;
1543 }
1544
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. */
1548
1549 if ( username[0] == '_' ) {
1550 uid_t id = atoi( &username[1] );
1551 struct passwd *pw = NULL;
1552
1553 if ( (id!=0) && ((pw = getpwuid( id )) != NULL) ) {
1554 real_username = strdup( pw->pw_name );
1555 }
1556 }
1557#endif
1558
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;
1566 }
1567 }
1568
1569 retval = _winbind_read_password(pamh, ctrl, NULL,
1570 "Password: ", NULL,
1571 &password);
1572
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;
1577 }
1578
1579 /* Let's not give too much away in the log file */
1580
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
1587
1588 member = get_member_from_config(pamh, argc, argv, ctrl, d);
1589
1590 cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1591
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);
1595
1596 if (retval == PAM_NEW_AUTHTOK_REQD ||
1597 retval == PAM_AUTHTOK_EXPIRED) {
1598
1599 char *new_authtok_required_during_auth = NULL;
1600
1601 if (!asprintf(&new_authtok_required, "%d", retval)) {
1602 retval = PAM_BUF_ERR;
1603 goto out;
1604 }
1605
1606 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, new_authtok_required, _pam_winbind_cleanup_func);
1607
1608 retval = PAM_SUCCESS;
1609
1610 if (!asprintf(&new_authtok_required_during_auth, "%d", True)) {
1611 retval = PAM_BUF_ERR;
1612 goto out;
1613 }
1614
1615 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
1616 new_authtok_required_during_auth, _pam_winbind_cleanup_func);
1617
1618 goto out;
1619 }
1620
1621out:
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);
1626 }
1627
1628 if ( real_username ) {
1629 free( real_username );
1630 }
1631
1632 if (d) {
1633 iniparser_freedict(d);
1634 }
1635
1636 if (!new_authtok_required) {
1637 pam_set_data(pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, NULL, NULL);
1638 }
1639
1640 if (retval != PAM_SUCCESS) {
1641 _pam_free_data_info3(pamh);
1642 }
1643
1644 _PAM_LOG_FUNCTION_LEAVE("pam_sm_authenticate", pamh, ctrl, retval);
1645
1646 return retval;
1647}
1648
1649PAM_EXTERN
1650int pam_sm_setcred(pam_handle_t *pamh, int flags,
1651 int argc, const char **argv)
1652{
1653 int ret = PAM_SYSTEM_ERR;
1654 dictionary *d = NULL;
1655
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;
1661 }
1662
1663 _PAM_LOG_FUNCTION_ENTER("pam_sm_setcred", pamh, ctrl, flags);
1664
1665 switch (flags & ~PAM_SILENT) {
1666
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;
1685 }
1686
1687 out:
1688 if (d) {
1689 iniparser_freedict(d);
1690 }
1691
1692 _PAM_LOG_FUNCTION_LEAVE("pam_sm_setcred", pamh, ctrl, ret);
1693
1694 return ret;
1695}
1696
1697/*
1698 * Account management. We want to verify that the account exists
1699 * before returning PAM_SUCCESS
1700 */
1701PAM_EXTERN
1702int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
1703 int argc, const char **argv)
1704{
1705 const char *username;
1706 int ret = PAM_USER_UNKNOWN;
1707 void *tmp = NULL;
1708 dictionary *d = NULL;
1709
1710 /* parse arguments */
1711 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1712 if (ctrl == -1) {
1713 return PAM_SYSTEM_ERR;
1714 }
1715
1716 _PAM_LOG_FUNCTION_ENTER("pam_sm_acct_mgmt", pamh, ctrl, flags);
1717
1718
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;
1725 }
1726
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;
1740 }
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;
1762 }
1763 }
1764
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;
1775 }
1776
1777 /* should not be reached */
1778 ret = PAM_IGNORE;
1779
1780 out:
1781
1782 if (d) {
1783 iniparser_freedict(d);
1784 }
1785
1786 _PAM_LOG_FUNCTION_LEAVE("pam_sm_acct_mgmt", pamh, ctrl, ret);
1787
1788 return ret;
1789}
1790
1791PAM_EXTERN
1792int pam_sm_open_session(pam_handle_t *pamh, int flags,
1793 int argc, const char **argv)
1794{
1795 int ret = PAM_SYSTEM_ERR;
1796 dictionary *d = NULL;
1797
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;
1803 }
1804
1805 _PAM_LOG_FUNCTION_ENTER("pam_sm_open_session", pamh, ctrl, flags);
1806
1807 ret = PAM_SUCCESS;
1808
1809 out:
1810 if (d) {
1811 iniparser_freedict(d);
1812 }
1813
1814 _PAM_LOG_FUNCTION_LEAVE("pam_sm_open_session", pamh, ctrl, ret);
1815
1816 return ret;
1817}
1818
1819PAM_EXTERN
1820int pam_sm_close_session(pam_handle_t *pamh, int flags,
1821 int argc, const char **argv)
1822{
1823 dictionary *d = NULL;
1824 int retval = PAM_SUCCESS;
1825
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;
1831 }
1832
1833 _PAM_LOG_FUNCTION_ENTER("pam_sm_close_session", pamh, ctrl, flags);
1834
1835 if (!(flags & PAM_DELETE_CRED)) {
1836 retval = PAM_SUCCESS;
1837 goto out;
1838 }
1839
1840 if (ctrl & WINBIND_KRB5_AUTH) {
1841
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;
1848
1849 ZERO_STRUCT(request);
1850 ZERO_STRUCT(response);
1851
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;
1856 }
1857
1858 if (user == NULL) {
1859 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1860 retval = PAM_USER_UNKNOWN;
1861 goto out;
1862 }
1863
1864 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
1865
1866 ccname = pam_getenv(pamh, "KRB5CCNAME");
1867 if (ccname == NULL) {
1868 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1869 }
1870
1871 strncpy(request.data.logoff.user, user,
1872 sizeof(request.data.logoff.user) - 1);
1873
1874 if (ccname) {
1875 strncpy(request.data.logoff.krb5ccname, ccname,
1876 sizeof(request.data.logoff.krb5ccname) - 1);
1877 }
1878
1879 pwd = getpwnam(user);
1880 if (pwd == NULL) {
1881 retval = PAM_USER_UNKNOWN;
1882 goto out;
1883 }
1884 request.data.logoff.uid = pwd->pw_uid;
1885
1886 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1887
1888 retval = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1889 }
1890
1891out:
1892 if (d) {
1893 iniparser_freedict(d);
1894 }
1895
1896 /*
1897 * Delete the krb5 ccname variable from the PAM environment
1898 * if it was set by winbind.
1899 */
1900 if (ctrl & WINBIND_KRB5_AUTH) {
1901 pam_putenv(pamh, "KRB5CCNAME");
1902 }
1903
1904 _PAM_LOG_FUNCTION_LEAVE("pam_sm_close_session", pamh, ctrl, retval);
1905
1906 return retval;
1907}
1908
1909/**
1910 * evaluate whether we need to re-authenticate with kerberos after a password change
1911 *
1912 * @param pamh PAM handle
1913 * @param ctrl PAM winbind options.
1914 * @param user The username
1915 *
1916 * @return boolean Returns True if required, False if not.
1917 */
1918
1919static BOOL _pam_require_krb5_auth_after_chauthtok(pam_handle_t *pamh, int ctrl, const char *user)
1920{
1921
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
1925 *
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 * */
1932
1933 char *new_authtok_reqd_during_auth = NULL;
1934 struct passwd *pwd = NULL;
1935
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);
1938
1939 if (new_authtok_reqd_during_auth) {
1940 return True;
1941 }
1942
1943 pwd = getpwnam(user);
1944 if (!pwd) {
1945 return False;
1946 }
1947
1948 if (getuid() == pwd->pw_uid) {
1949 return True;
1950 }
1951
1952 return False;
1953}
1954
1955
1956PAM_EXTERN
1957int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1958 int argc, const char **argv)
1959{
1960 unsigned int lctrl;
1961 int ret;
1962 unsigned int ctrl;
1963 bool cached_login = False;
1964
1965 /* <DO NOT free() THESE> */
1966 const char *user;
1967 char *pass_old, *pass_new;
1968 /* </DO NOT free() THESE> */
1969
1970 char *Announce;
1971
1972 int retry = 0;
1973 dictionary *d = NULL;
1974 char *username_ret = NULL;
1975 struct winbindd_response response;
1976
1977 ZERO_STRUCT(response);
1978
1979 ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1980 if (ctrl == -1) {
1981 ret = PAM_SYSTEM_ERR;
1982 goto out;
1983 }
1984
1985 _PAM_LOG_FUNCTION_ENTER("pam_sm_chauthtok", pamh, ctrl, flags);
1986
1987 cached_login = (ctrl & WINBIND_CACHED_LOGIN);
1988
1989 /* clearing offline bit for auth */
1990 ctrl &= ~WINBIND_CACHED_LOGIN;
1991
1992 /*
1993 * First get the name of a user
1994 */
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;
2000 }
2001
2002 if (user == NULL) {
2003 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
2004 ret = PAM_USER_UNKNOWN;
2005 goto out;
2006 }
2007
2008 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
2009
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;
2021 }
2022
2023 /*
2024 * obtain and verify the current password (OLDAUTHTOK) for
2025 * the user.
2026 */
2027
2028 if (flags & PAM_PRELIM_CHECK) {
2029 time_t pwdlastset_prelim = 0;
2030
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;
2038 }
2039 (void) strcpy(Announce, greeting);
2040 (void) strcpy(Announce + sizeof(greeting) - 1, user);
2041#undef greeting
2042
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;
2052 }
2053
2054 /* verify that this is the password for this user */
2055
2056 ret = winbind_auth_request(pamh, ctrl, user, pass_old,
2057 NULL, NULL, &response, &pwdlastset_prelim, NULL);
2058
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;
2065 }
2066
2067 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET, (void *)pwdlastset_prelim, NULL);
2068
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");
2073 }
2074 } else if (flags & PAM_UPDATE_AUTHTOK) {
2075
2076 time_t pwdlastset_update = 0;
2077
2078 /*
2079 * obtain the proposed password
2080 */
2081
2082 /*
2083 * get the old token back.
2084 */
2085
2086 ret = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
2087
2088 if (ret != PAM_SUCCESS) {
2089 _pam_log(pamh, ctrl, LOG_NOTICE, "user not authenticated");
2090 goto out;
2091 }
2092
2093 lctrl = ctrl & ~WINBIND_TRY_FIRST_PASS_ARG;
2094
2095 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
2096 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
2097 }
2098 retry = 0;
2099 ret = PAM_AUTHTOK_ERR;
2100 while ((ret != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
2101 /*
2102 * use_authtok is to force the use of a previously entered
2103 * password -- needed for pluggable password strength checking
2104 */
2105
2106 ret = _winbind_read_password(pamh, lctrl,
2107 NULL,
2108 "Enter new NT password: ",
2109 "Retype new NT password: ",
2110 (const char **) &pass_new);
2111
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;
2117 }
2118
2119 /*
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.
2123 */
2124
2125 if (pass_new[0] == '\0') {/* "\0" password = NULL */
2126 pass_new = NULL;
2127 }
2128 }
2129
2130 /*
2131 * By reaching here we have approved the passwords and must now
2132 * rebuild the password database file.
2133 */
2134 _pam_get_data( pamh, PAM_WINBIND_PWD_LAST_SET,
2135 &pwdlastset_update);
2136
2137 /*
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
2141 */
2142 if (cached_login) {
2143 ctrl |= WINBIND_CACHED_LOGIN;
2144 }
2145
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;
2152 }
2153
2154 if (_pam_require_krb5_auth_after_chauthtok(pamh, ctrl, user)) {
2155
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);
2158
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 * */
2166
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;
2172
2173 if (ret == PAM_SUCCESS) {
2174
2175 /* warn a user if the password is about to expire soon */
2176 _pam_warn_password_expiry(pamh, ctrl, &response, NULL);
2177
2178 /* set some info3 info for other modules in the stack */
2179 _pam_set_data_info3(pamh, ctrl, &response);
2180
2181 /* put krb5ccname into env */
2182 _pam_setup_krb5_env(pamh, ctrl, response.data.auth.krb5ccname);
2183
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);
2188 }
2189 }
2190
2191 goto out;
2192 }
2193 } else {
2194 ret = PAM_SERVICE_ERR;
2195 }
2196
2197out:
2198 if (d) {
2199 iniparser_freedict(d);
2200 }
2201
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");
2206
2207 _PAM_LOG_FUNCTION_LEAVE("pam_sm_chauthtok", pamh, ctrl, ret);
2208
2209 return ret;
2210}
2211
2212#ifdef PAM_STATIC
2213
2214/* static module data */
2215
2216struct 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
2224};
2225
2226#endif
2227
2228/*
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.
2238 *
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.
2251 *
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.)
2257 *
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.
2269 */
Note: See TracBrowser for help on using the repository browser.