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-2006
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"
17 #define MAX_PASSWD_TRIES 3
20 static void _pam_log(int err
, const char *format
, ...)
24 va_start(args
, format
);
25 openlog(MODULE_NAME
, LOG_CONS
|LOG_PID
, LOG_AUTH
);
26 vsyslog(err
, format
, args
);
31 static void _pam_log_debug(int ctrl
, int err
, const char *format
, ...)
35 if (!(ctrl
& WINBIND_DEBUG_ARG
)) {
39 va_start(args
, format
);
40 openlog(MODULE_NAME
, LOG_CONS
|LOG_PID
, LOG_AUTH
);
41 vsyslog(err
, format
, args
);
46 static int _pam_parse(int argc
, const char **argv
, dictionary
**d
)
49 const char *config_file
= NULL
;
57 for (i
=argc
,v
=argv
; i
-- > 0; ++v
) {
58 if (!strncasecmp(*v
, "config", strlen("config"))) {
59 ctrl
|= WINBIND_CONFIG_FILE
;
65 if (config_file
== NULL
) {
66 config_file
= PAM_WINBIND_CONFIG_FILE
;
69 *d
= iniparser_load(CONST_DISCARD(char *, config_file
));
74 if (iniparser_getboolean(*d
, CONST_DISCARD(char *, "global:debug"), False
)) {
75 ctrl
|= WINBIND_DEBUG_ARG
;
78 if (iniparser_getboolean(*d
, CONST_DISCARD(char *, "global:cached_login"), False
)) {
79 ctrl
|= WINBIND_CACHED_LOGIN
;
82 if (iniparser_getboolean(*d
, CONST_DISCARD(char *, "global:krb5_auth"), False
)) {
83 ctrl
|= WINBIND_KRB5_AUTH
;
86 if (iniparser_getstr(*d
, CONST_DISCARD(char *,"global:krb5_ccache_type")) != NULL
) {
87 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
90 if ((iniparser_getstr(*d
, CONST_DISCARD(char *, "global:require-membership-of")) != NULL
) ||
91 (iniparser_getstr(*d
, CONST_DISCARD(char *, "global:require_membership_of")) != NULL
)) {
92 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
96 /* step through arguments */
97 for (i
=argc
,v
=argv
; i
-- > 0; ++v
) {
100 if (!strcmp(*v
,"debug"))
101 ctrl
|= WINBIND_DEBUG_ARG
;
102 else if (!strcasecmp(*v
, "use_authtok"))
103 ctrl
|= WINBIND_USE_AUTHTOK_ARG
;
104 else if (!strcasecmp(*v
, "use_first_pass"))
105 ctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
106 else if (!strcasecmp(*v
, "try_first_pass"))
107 ctrl
|= WINBIND_TRY_FIRST_PASS_ARG
;
108 else if (!strcasecmp(*v
, "unknown_ok"))
109 ctrl
|= WINBIND_UNKNOWN_OK_ARG
;
110 else if (!strncasecmp(*v
, "require_membership_of", strlen("require_membership_of")))
111 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
112 else if (!strncasecmp(*v
, "require-membership-of", strlen("require-membership-of")))
113 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
114 else if (!strcasecmp(*v
, "krb5_auth"))
115 ctrl
|= WINBIND_KRB5_AUTH
;
116 else if (!strncasecmp(*v
, "krb5_ccache_type", strlen("krb5_ccache_type")))
117 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
118 else if (!strcasecmp(*v
, "cached_login"))
119 ctrl
|= WINBIND_CACHED_LOGIN
;
121 _pam_log(LOG_ERR
, "pam_parse: unknown option; %s", *v
);
128 static void _pam_winbind_cleanup_func(pam_handle_t
*pamh
, void *data
, int error_status
)
133 static const struct ntstatus_errors
{
134 const char *ntstatus_string
;
135 const char *error_string
;
136 } ntstatus_errors
[] = {
137 {"NT_STATUS_OK", "Success"},
138 {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
139 {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
140 {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
141 {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
142 {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
143 {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
144 {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
145 {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
146 {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
147 {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
148 {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
149 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
150 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
151 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
152 {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
156 const char *_get_ntstatus_error_string(const char *nt_status_string
)
159 for (i
=0; ntstatus_errors
[i
].ntstatus_string
!= NULL
; i
++) {
160 if (!strcasecmp(ntstatus_errors
[i
].ntstatus_string
, nt_status_string
)) {
161 return ntstatus_errors
[i
].error_string
;
167 /* --- authentication management functions --- */
169 /* Attempt a conversation */
171 static int converse(pam_handle_t
*pamh
, int nargs
,
172 struct pam_message
**message
,
173 struct pam_response
**response
)
176 struct pam_conv
*conv
;
178 retval
= pam_get_item(pamh
, PAM_CONV
, (const void **) &conv
);
179 if (retval
== PAM_SUCCESS
) {
180 retval
= conv
->conv(nargs
, (const struct pam_message
**)message
,
181 response
, conv
->appdata_ptr
);
184 return retval
; /* propagate error status */
188 static int _make_remark(pam_handle_t
* pamh
, int type
, const char *text
)
190 int retval
= PAM_SUCCESS
;
192 struct pam_message
*pmsg
[1], msg
[1];
193 struct pam_response
*resp
;
196 msg
[0].msg
= CONST_DISCARD(char *, text
);
197 msg
[0].msg_style
= type
;
200 retval
= converse(pamh
, 1, pmsg
, &resp
);
203 _pam_drop_reply(resp
, 1);
208 static int _make_remark_format(pam_handle_t
* pamh
, int type
, const char *format
, ...)
214 va_start(args
, format
);
215 vasprintf(&var
, format
, args
);
218 ret
= _make_remark(pamh
, type
, var
);
223 static int pam_winbind_request(pam_handle_t
* pamh
, int ctrl
,
224 enum winbindd_cmd req_type
,
225 struct winbindd_request
*request
,
226 struct winbindd_response
*response
)
228 /* Fill in request and send down pipe */
229 init_request(request
, req_type
);
231 if (write_sock(request
, sizeof(*request
), 0) == -1) {
232 _pam_log(LOG_ERR
, "write to socket failed!");
234 return PAM_SERVICE_ERR
;
238 if (read_reply(response
) == -1) {
239 _pam_log(LOG_ERR
, "read from socket failed!");
241 return PAM_SERVICE_ERR
;
244 /* We are done with the socket - close it and avoid mischeif */
247 /* Copy reply data from socket */
248 if (response
->result
!= WINBINDD_OK
) {
249 if (response
->data
.auth
.pam_error
!= PAM_SUCCESS
) {
250 _pam_log(LOG_ERR
, "request failed: %s, PAM error was %s (%d), NT error was %s",
251 response
->data
.auth
.error_string
,
252 pam_strerror(pamh
, response
->data
.auth
.pam_error
),
253 response
->data
.auth
.pam_error
,
254 response
->data
.auth
.nt_status_string
);
255 return response
->data
.auth
.pam_error
;
257 _pam_log(LOG_ERR
, "request failed, but PAM error 0!");
258 return PAM_SERVICE_ERR
;
265 static int pam_winbind_request_log(pam_handle_t
* pamh
,
267 enum winbindd_cmd req_type
,
268 struct winbindd_request
*request
,
269 struct winbindd_response
*response
,
274 retval
= pam_winbind_request(pamh
, ctrl
, req_type
, request
, response
);
278 /* incorrect password */
279 _pam_log(LOG_WARNING
, "user `%s' denied access (incorrect password or invalid membership)", user
);
281 case PAM_ACCT_EXPIRED
:
282 /* account expired */
283 _pam_log(LOG_WARNING
, "user `%s' account expired", user
);
285 case PAM_AUTHTOK_EXPIRED
:
286 /* password expired */
287 _pam_log(LOG_WARNING
, "user `%s' password expired", user
);
289 case PAM_NEW_AUTHTOK_REQD
:
290 /* new password required */
291 _pam_log(LOG_WARNING
, "user `%s' new password required", user
);
293 case PAM_USER_UNKNOWN
:
294 /* the user does not exist */
295 _pam_log_debug(ctrl
, LOG_NOTICE
, "user `%s' not found", user
);
296 if (ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
301 if (req_type
== WINBINDD_PAM_AUTH
) {
302 /* Otherwise, the authentication looked good */
303 _pam_log(LOG_NOTICE
, "user '%s' granted access", user
);
304 } else if (req_type
== WINBINDD_PAM_CHAUTHTOK
) {
305 /* Otherwise, the authentication looked good */
306 _pam_log(LOG_NOTICE
, "user '%s' password changed", user
);
308 /* Otherwise, the authentication looked good */
309 _pam_log(LOG_NOTICE
, "user '%s' OK", user
);
314 /* we don't know anything about this return value */
315 _pam_log(LOG_ERR
, "internal module error (retval = %d, user = `%s')",
321 /* talk to winbindd */
322 static int winbind_auth_request(pam_handle_t
* pamh
,
329 time_t *pwd_last_set
)
331 struct winbindd_request request
;
332 struct winbindd_response response
;
335 ZERO_STRUCT(request
);
336 ZERO_STRUCT(response
);
342 strncpy(request
.data
.auth
.user
, user
,
343 sizeof(request
.data
.auth
.user
)-1);
345 strncpy(request
.data
.auth
.pass
, pass
,
346 sizeof(request
.data
.auth
.pass
)-1);
348 request
.data
.auth
.krb5_cc_type
[0] = '\0';
349 request
.data
.auth
.uid
= -1;
351 request
.flags
= WBFLAG_PAM_INFO3_TEXT
| WBFLAG_PAM_CONTACT_TRUSTDOM
;
353 if (ctrl
& WINBIND_KRB5_AUTH
) {
355 struct passwd
*pwd
= NULL
;
357 _pam_log_debug(ctrl
, LOG_DEBUG
, "enabling krb5 login flag\n");
359 request
.flags
|= WBFLAG_PAM_KRB5
| WBFLAG_PAM_FALLBACK_AFTER_KRB5
;
361 pwd
= getpwnam(user
);
363 return PAM_USER_UNKNOWN
;
365 request
.data
.auth
.uid
= pwd
->pw_uid
;
368 if (ctrl
& WINBIND_CACHED_LOGIN
) {
369 _pam_log_debug(ctrl
, LOG_DEBUG
, "enabling cached login flag\n");
370 request
.flags
|= WBFLAG_PAM_CACHED_LOGIN
;
373 if (cctype
!= NULL
) {
374 strncpy(request
.data
.auth
.krb5_cc_type
, cctype
,
375 sizeof(request
.data
.auth
.krb5_cc_type
) - 1);
376 _pam_log_debug(ctrl
, LOG_DEBUG
, "enabling request for a %s krb5 ccache\n", cctype
);
379 request
.data
.auth
.require_membership_of_sid
[0] = '\0';
381 if (member
!= NULL
) {
382 strncpy(request
.data
.auth
.require_membership_of_sid
, member
,
383 sizeof(request
.data
.auth
.require_membership_of_sid
)-1);
387 if ( (member
!= NULL
) && (strncmp("S-", member
, 2) != 0) ) {
389 struct winbindd_request sid_request
;
390 struct winbindd_response sid_response
;
392 ZERO_STRUCT(sid_request
);
393 ZERO_STRUCT(sid_response
);
395 _pam_log_debug(ctrl
, LOG_DEBUG
, "no sid given, looking up: %s\n", member
);
397 /* fortunatly winbindd can handle non-separated names */
398 strncpy(sid_request
.data
.name
.name
, member
,
399 sizeof(sid_request
.data
.name
.name
) - 1);
401 if (pam_winbind_request_log(pamh
, ctrl
, WINBINDD_LOOKUPNAME
, &sid_request
, &sid_response
, user
)) {
402 _pam_log(LOG_INFO
, "could not lookup name: %s\n", member
);
406 member
= sid_response
.data
.sid
.sid
;
408 strncpy(request
.data
.auth
.require_membership_of_sid
, member
,
409 sizeof(request
.data
.auth
.require_membership_of_sid
)-1);
412 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_PAM_AUTH
, &request
, &response
, user
);
415 *pwd_last_set
= response
.data
.auth
.info3
.pass_last_set_time
;
418 if ((ctrl
& WINBIND_KRB5_AUTH
) &&
419 response
.data
.auth
.krb5ccname
[0] != '\0') {
423 _pam_log_debug(ctrl
, LOG_DEBUG
, "request returned KRB5CCNAME: %s",
424 response
.data
.auth
.krb5ccname
);
426 snprintf(var
, sizeof(var
), "KRB5CCNAME=%s", response
.data
.auth
.krb5ccname
);
428 ret
= pam_putenv(pamh
, var
);
429 if (ret
!= PAM_SUCCESS
) {
430 _pam_log(LOG_ERR
, "failed to set KRB5CCNAME to %s", var
);
435 if (!process_result
) {
440 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PASSWORD_EXPIRED");
441 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PASSWORD_MUST_CHANGE");
442 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_INVALID_WORKSTATION");
443 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_INVALID_LOGON_HOURS");
444 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_ACCOUNT_EXPIRED");
445 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_ACCOUNT_DISABLED");
446 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_ACCOUNT_LOCKED_OUT");
447 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
448 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
449 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
452 /* handle the case where the auth was ok, but the password must expire right now */
453 /* good catch from Ralf Haferkamp: an expiry of "never" is translated to -1 */
454 if ( ! (response
.data
.auth
.info3
.acct_flags
& ACB_PWNOEXP
) &&
455 (response
.data
.auth
.policy
.expire
> 0) &&
456 (response
.data
.auth
.info3
.pass_last_set_time
+ response
.data
.auth
.policy
.expire
< time(NULL
))) {
458 ret
= PAM_AUTHTOK_EXPIRED
;
460 _pam_log_debug(ctrl
, LOG_DEBUG
,"Password has expired (Password was last set: %d, "
461 "the policy says it should expire here %d (now it's: %d)\n",
462 response
.data
.auth
.info3
.pass_last_set_time
,
463 response
.data
.auth
.info3
.pass_last_set_time
+ response
.data
.auth
.policy
.expire
,
466 PAM_WB_REMARK_DIRECT_RET(pamh
, "NT_STATUS_PASSWORD_EXPIRED");
470 /* warn a user if the password is about to expire soon */
471 if ( ! (response
.data
.auth
.info3
.acct_flags
& ACB_PWNOEXP
) &&
472 (response
.data
.auth
.policy
.expire
) &&
473 (response
.data
.auth
.info3
.pass_last_set_time
+ response
.data
.auth
.policy
.expire
> time(NULL
) ) ) {
475 int days
= response
.data
.auth
.policy
.expire
/ SECONDS_PER_DAY
;
476 if (days
<= DAYS_TO_WARN_BEFORE_PWD_EXPIRES
) {
477 _make_remark_format(pamh
, PAM_TEXT_INFO
, "Your password will expire in %d days", days
);
481 if (response
.data
.auth
.info3
.user_flgs
& LOGON_CACHED_ACCOUNT
) {
482 _make_remark(pamh
, PAM_ERROR_MSG
, "Logging on using cached account. Network ressources can be unavailable");
483 _pam_log_debug(ctrl
, LOG_DEBUG
,"User %s logged on using cached account\n", user
);
486 /* save the CIFS homedir for pam_cifs / pam_mount */
487 if (response
.data
.auth
.info3
.home_dir
[0] != '\0') {
489 int ret2
= pam_set_data(pamh
, PAM_WINBIND_HOMEDIR
,
490 (void *) strdup(response
.data
.auth
.info3
.home_dir
),
491 _pam_winbind_cleanup_func
);
493 _pam_log_debug(ctrl
, LOG_DEBUG
, "Could not set data: %s",
494 pam_strerror(pamh
, ret2
));
499 /* save the logon script path for other PAM modules */
500 if (response
.data
.auth
.info3
.logon_script
[0] != '\0') {
502 int ret2
= pam_set_data(pamh
, PAM_WINBIND_LOGONSCRIPT
,
503 (void *) strdup(response
.data
.auth
.info3
.logon_script
),
504 _pam_winbind_cleanup_func
);
506 _pam_log_debug(ctrl
, LOG_DEBUG
, "Could not set data: %s",
507 pam_strerror(pamh
, ret2
));
511 /* save the profile path for other PAM modules */
512 if (response
.data
.auth
.info3
.profile_path
[0] != '\0') {
514 int ret2
= pam_set_data(pamh
, PAM_WINBIND_PROFILEPATH
,
515 (void *) strdup(response
.data
.auth
.info3
.profile_path
),
516 _pam_winbind_cleanup_func
);
518 _pam_log_debug(ctrl
, LOG_DEBUG
, "Could not set data: %s",
519 pam_strerror(pamh
, ret2
));
526 /* talk to winbindd */
527 static int winbind_chauthtok_request(pam_handle_t
* pamh
,
534 struct winbindd_request request
;
535 struct winbindd_response response
;
538 ZERO_STRUCT(request
);
539 ZERO_STRUCT(response
);
541 if (request
.data
.chauthtok
.user
== NULL
) return -2;
543 strncpy(request
.data
.chauthtok
.user
, user
,
544 sizeof(request
.data
.chauthtok
.user
) - 1);
546 if (oldpass
!= NULL
) {
547 strncpy(request
.data
.chauthtok
.oldpass
, oldpass
,
548 sizeof(request
.data
.chauthtok
.oldpass
) - 1);
550 request
.data
.chauthtok
.oldpass
[0] = '\0';
553 if (newpass
!= NULL
) {
554 strncpy(request
.data
.chauthtok
.newpass
, newpass
,
555 sizeof(request
.data
.chauthtok
.newpass
) - 1);
557 request
.data
.chauthtok
.newpass
[0] = '\0';
560 if (ctrl
& WINBIND_KRB5_AUTH
) {
561 request
.flags
= WBFLAG_PAM_KRB5
| WBFLAG_PAM_CONTACT_TRUSTDOM
;
564 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_PAM_CHAUTHTOK
, &request
, &response
, user
);
566 if (ret
== PAM_SUCCESS
) {
570 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_BACKUP_CONTROLLER");
571 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_ACCESS_DENIED");
573 /* TODO: tell the min pwd length ? */
574 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PWD_TOO_SHORT");
576 /* TODO: tell the minage ? */
577 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PWD_TOO_RECENT");
579 /* TODO: tell the history length ? */
580 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PWD_HISTORY_CONFLICT");
582 if (!strcasecmp(response
.data
.auth
.nt_status_string
, "NT_STATUS_PASSWORD_RESTRICTION")) {
584 /* FIXME: avoid to send multiple PAM messages after another */
585 switch (response
.data
.auth
.reject_reason
) {
588 case REJECT_REASON_OTHER
:
589 if ((response
.data
.auth
.policy
.min_passwordage
> 0) &&
590 (pwd_last_set
+ response
.data
.auth
.policy
.min_passwordage
> time(NULL
))) {
591 PAM_WB_REMARK_DIRECT(pamh
, "NT_STATUS_PWD_TOO_RECENT");
594 case REJECT_REASON_TOO_SHORT
:
595 PAM_WB_REMARK_DIRECT(pamh
, "NT_STATUS_PWD_TOO_SHORT");
597 case REJECT_REASON_IN_HISTORY
:
598 PAM_WB_REMARK_DIRECT(pamh
, "NT_STATUS_PWD_HISTORY_CONFLICT");
600 case REJECT_REASON_NOT_COMPLEX
:
601 _make_remark(pamh
, PAM_ERROR_MSG
, "Password does not meet complexity requirements");
604 _pam_log_debug(ctrl
, LOG_DEBUG
,
605 "unknown password change reject reason: %d",
606 response
.data
.auth
.reject_reason
);
610 _make_remark_format(pamh
, PAM_ERROR_MSG
,
611 "Your password must be at least %d characters; "
612 "cannot repeat any of the your previous %d passwords"
614 "Please type a different password. "
615 "Type a password which meets these requirements in both text boxes.",
616 response
.data
.auth
.policy
.min_length_password
,
617 response
.data
.auth
.policy
.password_history
,
618 (response
.data
.auth
.policy
.password_properties
& DOMAIN_PASSWORD_COMPLEX
) ?
619 "; must contain capitals, numerals or punctuation; and cannot contain your account or full name" :
628 * Checks if a user has an account
635 static int valid_user(const char *user
, pam_handle_t
*pamh
, int ctrl
)
637 /* check not only if the user is available over NSS calls, also make
638 * sure it's really a winbind user, this is important when stacking PAM
639 * modules in the 'account' or 'password' facility. */
641 struct passwd
*pwd
= NULL
;
642 struct winbindd_request request
;
643 struct winbindd_response response
;
646 ZERO_STRUCT(request
);
647 ZERO_STRUCT(response
);
649 pwd
= getpwnam(user
);
654 strncpy(request
.data
.username
, user
,
655 sizeof(request
.data
.username
) - 1);
657 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_GETPWNAM
, &request
, &response
, user
);
660 case PAM_USER_UNKNOWN
:
670 static char *_pam_delete(register char *xx
)
678 * obtain a password from the user
681 static int _winbind_read_password(pam_handle_t
* pamh
,
694 * make sure nothing inappropriate gets returned
697 *pass
= token
= NULL
;
700 * which authentication token are we getting?
703 authtok_flag
= on(WINBIND__OLD_PASSWORD
, ctrl
) ? PAM_OLDAUTHTOK
: PAM_AUTHTOK
;
706 * should we obtain the password from a PAM item ?
709 if (on(WINBIND_TRY_FIRST_PASS_ARG
, ctrl
) || on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
710 retval
= pam_get_item(pamh
, authtok_flag
, (const void **) &item
);
711 if (retval
!= PAM_SUCCESS
) {
714 "pam_get_item returned error to unix-read-password"
717 } else if (item
!= NULL
) { /* we have a password! */
721 } else if (on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
722 return PAM_AUTHTOK_RECOVER_ERR
; /* didn't work */
723 } else if (on(WINBIND_USE_AUTHTOK_ARG
, ctrl
)
724 && off(WINBIND__OLD_PASSWORD
, ctrl
)) {
725 return PAM_AUTHTOK_RECOVER_ERR
;
729 * getting here implies we will have to get the password from the
734 struct pam_message msg
[3], *pmsg
[3];
735 struct pam_response
*resp
;
738 /* prepare to converse */
740 if (comment
!= NULL
) {
742 msg
[0].msg_style
= PAM_TEXT_INFO
;
743 msg
[0].msg
= CONST_DISCARD(char *, comment
);
750 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
751 msg
[i
++].msg
= CONST_DISCARD(char *, prompt1
);
754 if (prompt2
!= NULL
) {
756 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
757 msg
[i
++].msg
= CONST_DISCARD(char *, prompt2
);
760 /* so call the conversation expecting i responses */
762 retval
= converse(pamh
, i
, pmsg
, &resp
);
766 /* interpret the response */
768 if (retval
== PAM_SUCCESS
) { /* a good conversation */
770 token
= x_strdup(resp
[i
- replies
].resp
);
773 /* verify that password entered correctly */
774 if (!resp
[i
- 1].resp
775 || strcmp(token
, resp
[i
- 1].resp
)) {
776 _pam_delete(token
); /* mistyped */
777 retval
= PAM_AUTHTOK_RECOVER_ERR
;
778 _make_remark(pamh
, PAM_ERROR_MSG
, MISTYPED_PASS
);
782 _pam_log(LOG_NOTICE
, "could not recover authentication token");
783 retval
= PAM_AUTHTOK_RECOVER_ERR
;
788 * tidy up the conversation (resp_retcode) is ignored
789 * -- what is it for anyway? AGM
792 _pam_drop_reply(resp
, i
);
795 retval
= (retval
== PAM_SUCCESS
)
796 ? PAM_AUTHTOK_RECOVER_ERR
: retval
;
800 if (retval
!= PAM_SUCCESS
) {
801 _pam_log_debug(ctrl
, LOG_DEBUG
,
802 "unable to obtain a password");
805 /* 'token' is the entered password */
807 /* we store this password as an item */
809 retval
= pam_set_item(pamh
, authtok_flag
, token
);
810 _pam_delete(token
); /* clean it up */
811 if (retval
!= PAM_SUCCESS
||
812 (retval
= pam_get_item(pamh
, authtok_flag
, (const void **) &item
)) != PAM_SUCCESS
) {
814 _pam_log(LOG_CRIT
, "error manipulating password");
820 item
= NULL
; /* break link to password */
825 const char *get_conf_item_string(int argc
,
833 const char *parm_opt
= NULL
;
836 if (!(ctrl
& flag
)) {
840 /* let the pam opt take precedence over the pam_winbind.conf option */
844 if (!asprintf(&key
, "global:%s", item
)) {
848 parm_opt
= iniparser_getstr(d
, key
);
852 for ( i
=0; i
<argc
; i
++ ) {
854 if ((strncmp(argv
[i
], item
, strlen(item
)) == 0)) {
857 if ( (p
= strchr( argv
[i
], '=' )) == NULL
) {
858 _pam_log(LOG_INFO
, "no \"=\" delimiter for \"%s\" found\n", item
);
861 _pam_log_debug(ctrl
, LOG_INFO
, "PAM config: %s '%s'\n", item
, p
+1);
867 _pam_log_debug(ctrl
, LOG_INFO
, "CONFIG file: %s '%s'\n", item
, parm_opt
);
873 const char *get_krb5_cc_type_from_config(int argc
, const char **argv
, int ctrl
, dictionary
*d
)
875 return get_conf_item_string(argc
, argv
, ctrl
, d
, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE
);
878 const char *get_member_from_config(int argc
, const char **argv
, int ctrl
, dictionary
*d
)
880 const char *ret
= NULL
;
881 ret
= get_conf_item_string(argc
, argv
, ctrl
, d
, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP
);
885 return get_conf_item_string(argc
, argv
, ctrl
, d
, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP
);
889 int pam_sm_authenticate(pam_handle_t
*pamh
, int flags
,
890 int argc
, const char **argv
)
892 const char *username
;
893 const char *password
;
894 const char *member
= NULL
;
895 const char *cctype
= NULL
;
896 int retval
= PAM_AUTH_ERR
;
897 dictionary
*d
= NULL
;
899 /* parse arguments */
900 int ctrl
= _pam_parse(argc
, argv
, &d
);
902 retval
= PAM_SYSTEM_ERR
;
906 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_authenticate (flags: 0x%04x)", flags
);
908 /* Get the username */
909 retval
= pam_get_user(pamh
, &username
, NULL
);
910 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
911 _pam_log_debug(ctrl
, LOG_DEBUG
, "can not get the username");
912 retval
= PAM_SERVICE_ERR
;
916 retval
= _winbind_read_password(pamh
, ctrl
, NULL
,
920 if (retval
!= PAM_SUCCESS
) {
921 _pam_log(LOG_ERR
, "Could not retrieve user's password");
922 retval
= PAM_AUTHTOK_ERR
;
926 /* Let's not give too much away in the log file */
928 #ifdef DEBUG_PASSWORD
929 _pam_log_debug(ctrl
, LOG_INFO
, "Verify user `%s' with password `%s'",
932 _pam_log_debug(ctrl
, LOG_INFO
, "Verify user `%s'", username
);
935 member
= get_member_from_config(argc
, argv
, ctrl
, d
);
937 cctype
= get_krb5_cc_type_from_config(argc
, argv
, ctrl
, d
);
939 /* Now use the username to look up password */
940 retval
= winbind_auth_request(pamh
, ctrl
, username
, password
, member
, cctype
, True
, NULL
);
942 if (retval
== PAM_NEW_AUTHTOK_REQD
||
943 retval
== PAM_AUTHTOK_EXPIRED
) {
947 if (!asprintf(&buf
, "%d", retval
)) {
948 retval
= PAM_BUF_ERR
;
952 pam_set_data( pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, (void *)buf
, _pam_winbind_cleanup_func
);
954 retval
= PAM_SUCCESS
;
960 iniparser_freedict(d
);
966 int pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
967 int argc
, const char **argv
)
969 /* parse arguments */
970 int ctrl
= _pam_parse(argc
, argv
, NULL
);
972 return PAM_SYSTEM_ERR
;
975 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_setcred (flags: 0x%04x)", flags
);
977 if (flags
& PAM_DELETE_CRED
) {
978 return pam_sm_close_session(pamh
, flags
, argc
, argv
);
985 * Account management. We want to verify that the account exists
986 * before returning PAM_SUCCESS
989 int pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags
,
990 int argc
, const char **argv
)
992 const char *username
;
993 int retval
= PAM_USER_UNKNOWN
;
996 /* parse arguments */
997 int ctrl
= _pam_parse(argc
, argv
, NULL
);
999 return PAM_SYSTEM_ERR
;
1002 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_acct_mgmt (flags: 0x%04x)", flags
);
1005 /* Get the username */
1006 retval
= pam_get_user(pamh
, &username
, NULL
);
1007 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
1008 _pam_log_debug(ctrl
, LOG_DEBUG
,"can not get the username");
1009 return PAM_SERVICE_ERR
;
1012 /* Verify the username */
1013 retval
= valid_user(username
, pamh
, ctrl
);
1016 /* some sort of system error. The log was already printed */
1017 return PAM_SERVICE_ERR
;
1019 /* the user does not exist */
1020 _pam_log_debug(ctrl
, LOG_NOTICE
, "user `%s' not found", username
);
1021 if (ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
1024 return PAM_USER_UNKNOWN
;
1026 pam_get_data( pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, (const void **)&tmp
);
1030 case PAM_AUTHTOK_EXPIRED
:
1031 /* fall through, since new token is required in this case */
1032 case PAM_NEW_AUTHTOK_REQD
:
1033 _pam_log(LOG_WARNING
, "pam_sm_acct_mgmt success but %s is set",
1034 PAM_WINBIND_NEW_AUTHTOK_REQD
);
1035 _pam_log(LOG_NOTICE
, "user '%s' needs new password", username
);
1036 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
1037 return PAM_NEW_AUTHTOK_REQD
;
1039 _pam_log(LOG_WARNING
, "pam_sm_acct_mgmt success");
1040 _pam_log(LOG_NOTICE
, "user '%s' granted access", username
);
1045 /* Otherwise, the authentication looked good */
1046 _pam_log(LOG_NOTICE
, "user '%s' granted access", username
);
1049 /* we don't know anything about this return value */
1050 _pam_log(LOG_ERR
, "internal module error (retval = %d, user = `%s')",
1052 return PAM_SERVICE_ERR
;
1055 /* should not be reached */
1060 int pam_sm_open_session(pam_handle_t
*pamh
, int flags
,
1061 int argc
, const char **argv
)
1063 /* parse arguments */
1064 int ctrl
= _pam_parse(argc
, argv
, NULL
);
1066 return PAM_SYSTEM_ERR
;
1069 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_open_session handler (flags: 0x%04x)", flags
);
1075 int pam_sm_close_session(pam_handle_t
*pamh
, int flags
,
1076 int argc
, const char **argv
)
1078 dictionary
*d
= NULL
;
1079 int retval
= PAM_SUCCESS
;
1081 /* parse arguments */
1082 int ctrl
= _pam_parse(argc
, argv
, &d
);
1084 retval
= PAM_SYSTEM_ERR
;
1088 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_close_session handler (flags: 0x%04x)", flags
);
1090 if (!(flags
& PAM_DELETE_CRED
)) {
1091 retval
= PAM_SUCCESS
;
1095 if (ctrl
& WINBIND_KRB5_AUTH
) {
1097 /* destroy the ccache here */
1098 struct winbindd_request request
;
1099 struct winbindd_response response
;
1101 const char *ccname
= NULL
;
1102 struct passwd
*pwd
= NULL
;
1104 ZERO_STRUCT(request
);
1105 ZERO_STRUCT(response
);
1107 retval
= pam_get_user(pamh
, &user
, "Username: ");
1108 if (retval
== PAM_SUCCESS
) {
1110 _pam_log(LOG_ERR
, "username was NULL!");
1111 retval
= PAM_USER_UNKNOWN
;
1114 if (retval
== PAM_SUCCESS
) {
1115 _pam_log_debug(ctrl
, LOG_DEBUG
, "username [%s] obtained", user
);
1118 _pam_log_debug(ctrl
, LOG_DEBUG
, "could not identify user");
1122 ccname
= pam_getenv(pamh
, "KRB5CCNAME");
1123 if (ccname
== NULL
) {
1124 _pam_log_debug(ctrl
, LOG_DEBUG
, "user has no KRB5CCNAME environment");
1125 retval
= PAM_SUCCESS
;
1129 strncpy(request
.data
.logoff
.user
, user
,
1130 sizeof(request
.data
.logoff
.user
) - 1);
1132 strncpy(request
.data
.logoff
.krb5ccname
, ccname
,
1133 sizeof(request
.data
.logoff
.krb5ccname
) - 1);
1135 pwd
= getpwnam(user
);
1137 retval
= PAM_USER_UNKNOWN
;
1140 request
.data
.logoff
.uid
= pwd
->pw_uid
;
1142 request
.flags
= WBFLAG_PAM_KRB5
| WBFLAG_PAM_CONTACT_TRUSTDOM
;
1144 retval
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_PAM_LOGOFF
, &request
, &response
, user
);
1149 iniparser_freedict(d
);
1157 int pam_sm_chauthtok(pam_handle_t
* pamh
, int flags
,
1158 int argc
, const char **argv
)
1164 /* <DO NOT free() THESE> */
1166 char *pass_old
, *pass_new
;
1167 /* </DO NOT free() THESE> */
1172 dictionary
*d
= NULL
;
1174 ctrl
= _pam_parse(argc
, argv
, &d
);
1176 retval
= PAM_SYSTEM_ERR
;
1180 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_chauthtok (flags: 0x%04x)", flags
);
1182 /* clearing offline bit for the auth in the password change */
1183 ctrl
&= ~WINBIND_CACHED_LOGIN
;
1186 * First get the name of a user
1188 retval
= pam_get_user(pamh
, &user
, "Username: ");
1189 if (retval
== PAM_SUCCESS
) {
1191 _pam_log(LOG_ERR
, "username was NULL!");
1192 retval
= PAM_USER_UNKNOWN
;
1195 if (retval
== PAM_SUCCESS
) {
1196 _pam_log_debug(ctrl
, LOG_DEBUG
, "username [%s] obtained",
1200 _pam_log_debug(ctrl
, LOG_DEBUG
,
1201 "password - could not identify user");
1205 /* check if this is really a user in winbindd, not only in NSS */
1206 retval
= valid_user(user
, pamh
, ctrl
);
1209 retval
= PAM_USER_UNKNOWN
;
1212 retval
= PAM_SYSTEM_ERR
;
1219 * obtain and verify the current password (OLDAUTHTOK) for
1223 if (flags
& PAM_PRELIM_CHECK
) {
1225 time_t pwdlastset_prelim
= 0;
1227 /* instruct user what is happening */
1228 #define greeting "Changing password for "
1229 Announce
= (char *) malloc(sizeof(greeting
) + strlen(user
));
1230 if (Announce
== NULL
) {
1231 _pam_log(LOG_CRIT
, "password - out of memory");
1232 retval
= PAM_BUF_ERR
;
1235 (void) strcpy(Announce
, greeting
);
1236 (void) strcpy(Announce
+ sizeof(greeting
) - 1, user
);
1239 lctrl
= ctrl
| WINBIND__OLD_PASSWORD
;
1240 retval
= _winbind_read_password(pamh
, lctrl
,
1242 "(current) NT password: ",
1244 (const char **) &pass_old
);
1245 if (retval
!= PAM_SUCCESS
) {
1246 _pam_log(LOG_NOTICE
, "password - (old) token not obtained");
1249 /* verify that this is the password for this user */
1251 retval
= winbind_auth_request(pamh
, ctrl
, user
, pass_old
, NULL
, NULL
, False
, &pwdlastset_prelim
);
1253 if (retval
!= PAM_ACCT_EXPIRED
&&
1254 retval
!= PAM_AUTHTOK_EXPIRED
&&
1255 retval
!= PAM_NEW_AUTHTOK_REQD
&&
1256 retval
!= PAM_SUCCESS
) {
1261 pam_set_data(pamh
, PAM_WINBIND_PWD_LAST_SET
, (void *)pwdlastset_prelim
, NULL
);
1263 retval
= pam_set_item(pamh
, PAM_OLDAUTHTOK
, (const void *) pass_old
);
1265 if (retval
!= PAM_SUCCESS
) {
1266 _pam_log(LOG_CRIT
, "failed to set PAM_OLDAUTHTOK");
1268 } else if (flags
& PAM_UPDATE_AUTHTOK
) {
1270 time_t pwdlastset_update
= 0;
1273 * obtain the proposed password
1277 * get the old token back.
1280 retval
= pam_get_item(pamh
, PAM_OLDAUTHTOK
,
1281 (const void **) &pass_old
);
1283 if (retval
!= PAM_SUCCESS
) {
1284 _pam_log(LOG_NOTICE
, "user not authenticated");
1290 if (on(WINBIND_USE_AUTHTOK_ARG
, lctrl
)) {
1291 lctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
1294 retval
= PAM_AUTHTOK_ERR
;
1295 while ((retval
!= PAM_SUCCESS
) && (retry
++ < MAX_PASSWD_TRIES
)) {
1297 * use_authtok is to force the use of a previously entered
1298 * password -- needed for pluggable password strength checking
1301 retval
= _winbind_read_password(pamh
, lctrl
,
1303 "Enter new NT password: ",
1304 "Retype new NT password: ",
1305 (const char **) &pass_new
);
1307 if (retval
!= PAM_SUCCESS
) {
1308 _pam_log_debug(ctrl
, LOG_ALERT
1309 ,"password - new password not obtained");
1310 pass_old
= NULL
;/* tidy up */
1315 * At this point we know who the user is and what they
1316 * propose as their new password. Verify that the new
1317 * password is acceptable.
1320 if (pass_new
[0] == '\0') {/* "\0" password = NULL */
1326 * By reaching here we have approved the passwords and must now
1327 * rebuild the password database file.
1329 pam_get_data( pamh
, PAM_WINBIND_PWD_LAST_SET
, (const void **)&pwdlastset_update
);
1331 retval
= winbind_chauthtok_request(pamh
, ctrl
, user
, pass_old
, pass_new
, pwdlastset_update
);
1333 _pam_overwrite(pass_new
);
1334 _pam_overwrite(pass_old
);
1335 pass_old
= pass_new
= NULL
;
1339 /* just in case we need krb5 creds after a password change over msrpc */
1341 if (ctrl
& WINBIND_KRB5_AUTH
) {
1343 const char *member
= get_member_from_config(argc
, argv
, ctrl
, d
);
1344 const char *cctype
= get_krb5_cc_type_from_config(argc
, argv
, ctrl
, d
);
1346 retval
= winbind_auth_request(pamh
, ctrl
, user
, pass_new
, member
, cctype
, False
, NULL
);
1347 _pam_overwrite(pass_new
);
1348 _pam_overwrite(pass_old
);
1349 pass_old
= pass_new
= NULL
;
1352 retval
= PAM_SERVICE_ERR
;
1357 iniparser_freedict(d
);
1364 /* static module data */
1366 struct pam_module _pam_winbind_modstruct
= {
1368 pam_sm_authenticate
,
1371 pam_sm_open_session
,
1372 pam_sm_close_session
,
1379 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
1380 * Copyright (c) Tim Potter <tpot@samba.org> 2000
1381 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1382 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2006
1383 * Copyright (c) Jan Rêkorajski 1999.
1384 * Copyright (c) Andrew G. Morgan 1996-8.
1385 * Copyright (c) Alex O. Yuriev, 1996.
1386 * Copyright (c) Cristian Gafton 1996.
1387 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
1389 * Redistribution and use in source and binary forms, with or without
1390 * modification, are permitted provided that the following conditions
1392 * 1. Redistributions of source code must retain the above copyright
1393 * notice, and the entire permission notice in its entirety,
1394 * including the disclaimer of warranties.
1395 * 2. Redistributions in binary form must reproduce the above copyright
1396 * notice, this list of conditions and the following disclaimer in the
1397 * documentation and/or other materials provided with the distribution.
1398 * 3. The name of the author may not be used to endorse or promote
1399 * products derived from this software without specific prior
1400 * written permission.
1402 * ALTERNATIVELY, this product may be distributed under the terms of
1403 * the GNU Public License, in which case the provisions of the GPL are
1404 * required INSTEAD OF the above restrictions. (This clause is
1405 * necessary due to a potential bad interaction between the GPL and
1406 * the restrictions contained in a BSD-style copyright.)
1408 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
1409 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1410 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1411 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1412 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1413 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1414 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1415 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1416 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1417 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1418 * OF THE POSSIBILITY OF SUCH DAMAGE.