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
));
514 /* talk to winbindd */
515 static int winbind_chauthtok_request(pam_handle_t
* pamh
,
522 struct winbindd_request request
;
523 struct winbindd_response response
;
526 ZERO_STRUCT(request
);
527 ZERO_STRUCT(response
);
529 if (request
.data
.chauthtok
.user
== NULL
) return -2;
531 strncpy(request
.data
.chauthtok
.user
, user
,
532 sizeof(request
.data
.chauthtok
.user
) - 1);
534 if (oldpass
!= NULL
) {
535 strncpy(request
.data
.chauthtok
.oldpass
, oldpass
,
536 sizeof(request
.data
.chauthtok
.oldpass
) - 1);
538 request
.data
.chauthtok
.oldpass
[0] = '\0';
541 if (newpass
!= NULL
) {
542 strncpy(request
.data
.chauthtok
.newpass
, newpass
,
543 sizeof(request
.data
.chauthtok
.newpass
) - 1);
545 request
.data
.chauthtok
.newpass
[0] = '\0';
548 if (ctrl
& WINBIND_KRB5_AUTH
) {
549 request
.flags
= WBFLAG_PAM_KRB5
| WBFLAG_PAM_CONTACT_TRUSTDOM
;
552 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_PAM_CHAUTHTOK
, &request
, &response
, user
);
554 if (ret
== PAM_SUCCESS
) {
558 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_BACKUP_CONTROLLER");
559 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_ACCESS_DENIED");
561 /* TODO: tell the min pwd length ? */
562 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PWD_TOO_SHORT");
564 /* TODO: tell the minage ? */
565 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PWD_TOO_RECENT");
567 /* TODO: tell the history length ? */
568 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PWD_HISTORY_CONFLICT");
570 if (!strcasecmp(response
.data
.auth
.nt_status_string
, "NT_STATUS_PASSWORD_RESTRICTION")) {
572 /* FIXME: avoid to send multiple PAM messages after another */
573 switch (response
.data
.auth
.reject_reason
) {
576 case REJECT_REASON_OTHER
:
577 if ((response
.data
.auth
.policy
.min_passwordage
> 0) &&
578 (pwd_last_set
+ response
.data
.auth
.policy
.min_passwordage
> time(NULL
))) {
579 PAM_WB_REMARK_DIRECT(pamh
, "NT_STATUS_PWD_TOO_RECENT");
582 case REJECT_REASON_TOO_SHORT
:
583 PAM_WB_REMARK_DIRECT(pamh
, "NT_STATUS_PWD_TOO_SHORT");
585 case REJECT_REASON_IN_HISTORY
:
586 PAM_WB_REMARK_DIRECT(pamh
, "NT_STATUS_PWD_HISTORY_CONFLICT");
588 case REJECT_REASON_NOT_COMPLEX
:
589 _make_remark(pamh
, PAM_ERROR_MSG
, "Password does not meet complexity requirements");
592 _pam_log_debug(ctrl
, LOG_DEBUG
,
593 "unknown password change reject reason: %d",
594 response
.data
.auth
.reject_reason
);
598 _make_remark_format(pamh
, PAM_ERROR_MSG
,
599 "Your password must be at least %d characters; "
600 "cannot repeat any of the your previous %d passwords"
602 "Please type a different password. "
603 "Type a password which meets these requirements in both text boxes.",
604 response
.data
.auth
.policy
.min_length_password
,
605 response
.data
.auth
.policy
.password_history
,
606 (response
.data
.auth
.policy
.password_properties
& DOMAIN_PASSWORD_COMPLEX
) ?
607 "; must contain capitals, numerals or punctuation; and cannot contain your account or full name" :
616 * Checks if a user has an account
623 static int valid_user(const char *user
, pam_handle_t
*pamh
, int ctrl
)
625 /* check not only if the user is available over NSS calls, also make
626 * sure it's really a winbind user, this is important when stacking PAM
627 * modules in the 'account' or 'password' facility. */
629 struct passwd
*pwd
= NULL
;
630 struct winbindd_request request
;
631 struct winbindd_response response
;
634 ZERO_STRUCT(request
);
635 ZERO_STRUCT(response
);
637 pwd
= getpwnam(user
);
642 strncpy(request
.data
.username
, user
,
643 sizeof(request
.data
.username
) - 1);
645 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_GETPWNAM
, &request
, &response
, user
);
648 case PAM_USER_UNKNOWN
:
658 static char *_pam_delete(register char *xx
)
666 * obtain a password from the user
669 static int _winbind_read_password(pam_handle_t
* pamh
,
682 * make sure nothing inappropriate gets returned
685 *pass
= token
= NULL
;
688 * which authentication token are we getting?
691 authtok_flag
= on(WINBIND__OLD_PASSWORD
, ctrl
) ? PAM_OLDAUTHTOK
: PAM_AUTHTOK
;
694 * should we obtain the password from a PAM item ?
697 if (on(WINBIND_TRY_FIRST_PASS_ARG
, ctrl
) || on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
698 retval
= pam_get_item(pamh
, authtok_flag
, (const void **) &item
);
699 if (retval
!= PAM_SUCCESS
) {
702 "pam_get_item returned error to unix-read-password"
705 } else if (item
!= NULL
) { /* we have a password! */
709 } else if (on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
710 return PAM_AUTHTOK_RECOVER_ERR
; /* didn't work */
711 } else if (on(WINBIND_USE_AUTHTOK_ARG
, ctrl
)
712 && off(WINBIND__OLD_PASSWORD
, ctrl
)) {
713 return PAM_AUTHTOK_RECOVER_ERR
;
717 * getting here implies we will have to get the password from the
722 struct pam_message msg
[3], *pmsg
[3];
723 struct pam_response
*resp
;
726 /* prepare to converse */
728 if (comment
!= NULL
) {
730 msg
[0].msg_style
= PAM_TEXT_INFO
;
731 msg
[0].msg
= CONST_DISCARD(char *, comment
);
738 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
739 msg
[i
++].msg
= CONST_DISCARD(char *, prompt1
);
742 if (prompt2
!= NULL
) {
744 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
745 msg
[i
++].msg
= CONST_DISCARD(char *, prompt2
);
748 /* so call the conversation expecting i responses */
750 retval
= converse(pamh
, i
, pmsg
, &resp
);
754 /* interpret the response */
756 if (retval
== PAM_SUCCESS
) { /* a good conversation */
758 token
= x_strdup(resp
[i
- replies
].resp
);
761 /* verify that password entered correctly */
762 if (!resp
[i
- 1].resp
763 || strcmp(token
, resp
[i
- 1].resp
)) {
764 _pam_delete(token
); /* mistyped */
765 retval
= PAM_AUTHTOK_RECOVER_ERR
;
766 _make_remark(pamh
, PAM_ERROR_MSG
, MISTYPED_PASS
);
770 _pam_log(LOG_NOTICE
, "could not recover authentication token");
771 retval
= PAM_AUTHTOK_RECOVER_ERR
;
776 * tidy up the conversation (resp_retcode) is ignored
777 * -- what is it for anyway? AGM
780 _pam_drop_reply(resp
, i
);
783 retval
= (retval
== PAM_SUCCESS
)
784 ? PAM_AUTHTOK_RECOVER_ERR
: retval
;
788 if (retval
!= PAM_SUCCESS
) {
789 _pam_log_debug(ctrl
, LOG_DEBUG
,
790 "unable to obtain a password");
793 /* 'token' is the entered password */
795 /* we store this password as an item */
797 retval
= pam_set_item(pamh
, authtok_flag
, token
);
798 _pam_delete(token
); /* clean it up */
799 if (retval
!= PAM_SUCCESS
||
800 (retval
= pam_get_item(pamh
, authtok_flag
, (const void **) &item
)) != PAM_SUCCESS
) {
802 _pam_log(LOG_CRIT
, "error manipulating password");
808 item
= NULL
; /* break link to password */
813 const char *get_conf_item_string(int argc
,
822 const char *parm_opt
= NULL
;
825 if (!(ctrl
& flag
)) {
829 /* let the pam opt take precedence over the pam_winbind.conf option */
833 if (!asprintf(&key
, "global:%s", item
)) {
837 parm_opt
= iniparser_getstr(d
, key
);
841 for ( i
=0; i
<argc
; i
++ ) {
843 if ((strncmp(argv
[i
], item
, strlen(item
)) == 0)) {
846 parm
= strdup(argv
[i
]);
848 if ( (p
= strchr( parm
, '=' )) == NULL
) {
849 _pam_log(LOG_INFO
, "no \"=\" delimiter for \"%s\" found\n", item
);
853 _pam_log_debug(ctrl
, LOG_INFO
, "PAM config: %s '%s'\n", item
, p
+1);
859 _pam_log_debug(ctrl
, LOG_INFO
, "CONFIG file: %s '%s'\n", item
, parm_opt
);
866 const char *get_krb5_cc_type_from_config(int argc
, const char **argv
, int ctrl
, dictionary
*d
)
868 return get_conf_item_string(argc
, argv
, ctrl
, d
, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE
);
871 const char *get_member_from_config(int argc
, const char **argv
, int ctrl
, dictionary
*d
)
873 const char *ret
= NULL
;
874 ret
= get_conf_item_string(argc
, argv
, ctrl
, d
, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP
);
878 return get_conf_item_string(argc
, argv
, ctrl
, d
, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP
);
882 int pam_sm_authenticate(pam_handle_t
*pamh
, int flags
,
883 int argc
, const char **argv
)
885 const char *username
;
886 const char *password
;
887 const char *member
= NULL
;
888 const char *cctype
= NULL
;
889 int retval
= PAM_AUTH_ERR
;
892 /* parse arguments */
893 int ctrl
= _pam_parse(argc
, argv
, &d
);
895 retval
= PAM_SYSTEM_ERR
;
899 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_authenticate (flags: 0x%04x)", flags
);
901 /* Get the username */
902 retval
= pam_get_user(pamh
, &username
, NULL
);
903 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
904 _pam_log_debug(ctrl
, LOG_DEBUG
, "can not get the username");
905 retval
= PAM_SERVICE_ERR
;
909 retval
= _winbind_read_password(pamh
, ctrl
, NULL
,
913 if (retval
!= PAM_SUCCESS
) {
914 _pam_log(LOG_ERR
, "Could not retrieve user's password");
915 retval
= PAM_AUTHTOK_ERR
;
919 /* Let's not give too much away in the log file */
921 #ifdef DEBUG_PASSWORD
922 _pam_log_debug(ctrl
, LOG_INFO
, "Verify user `%s' with password `%s'",
925 _pam_log_debug(ctrl
, LOG_INFO
, "Verify user `%s'", username
);
928 member
= get_member_from_config(argc
, argv
, ctrl
, d
);
930 cctype
= get_krb5_cc_type_from_config(argc
, argv
, ctrl
, d
);
932 /* Now use the username to look up password */
933 retval
= winbind_auth_request(pamh
, ctrl
, username
, password
, member
, cctype
, True
, NULL
);
935 if (retval
== PAM_NEW_AUTHTOK_REQD
||
936 retval
== PAM_AUTHTOK_EXPIRED
) {
940 if (!asprintf(&buf
, "%d", retval
)) {
941 retval
= PAM_BUF_ERR
;
945 pam_set_data( pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, (void *)buf
, _pam_winbind_cleanup_func
);
947 retval
= PAM_SUCCESS
;
953 iniparser_freedict(d
);
959 int pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
960 int argc
, const char **argv
)
962 /* parse arguments */
963 int ctrl
= _pam_parse(argc
, argv
, NULL
);
965 return PAM_SYSTEM_ERR
;
968 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_setcred (flags: 0x%04x)", flags
);
970 if (flags
& PAM_DELETE_CRED
) {
971 return pam_sm_close_session(pamh
, flags
, argc
, argv
);
978 * Account management. We want to verify that the account exists
979 * before returning PAM_SUCCESS
982 int pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags
,
983 int argc
, const char **argv
)
985 const char *username
;
986 int retval
= PAM_USER_UNKNOWN
;
989 /* parse arguments */
990 int ctrl
= _pam_parse(argc
, argv
, NULL
);
992 return PAM_SYSTEM_ERR
;
995 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_acct_mgmt (flags: 0x%04x)", flags
);
998 /* Get the username */
999 retval
= pam_get_user(pamh
, &username
, NULL
);
1000 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
1001 _pam_log_debug(ctrl
, LOG_DEBUG
,"can not get the username");
1002 return PAM_SERVICE_ERR
;
1005 /* Verify the username */
1006 retval
= valid_user(username
, pamh
, ctrl
);
1009 /* some sort of system error. The log was already printed */
1010 return PAM_SERVICE_ERR
;
1012 /* the user does not exist */
1013 _pam_log_debug(ctrl
, LOG_NOTICE
, "user `%s' not found", username
);
1014 if (ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
1017 return PAM_USER_UNKNOWN
;
1019 pam_get_data( pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, (const void **)&tmp
);
1023 case PAM_AUTHTOK_EXPIRED
:
1024 /* fall through, since new token is required in this case */
1025 case PAM_NEW_AUTHTOK_REQD
:
1026 _pam_log(LOG_WARNING
, "pam_sm_acct_mgmt success but %s is set",
1027 PAM_WINBIND_NEW_AUTHTOK_REQD
);
1028 _pam_log(LOG_NOTICE
, "user '%s' needs new password", username
);
1029 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
1030 return PAM_NEW_AUTHTOK_REQD
;
1032 _pam_log(LOG_WARNING
, "pam_sm_acct_mgmt success");
1033 _pam_log(LOG_NOTICE
, "user '%s' granted access", username
);
1038 /* Otherwise, the authentication looked good */
1039 _pam_log(LOG_NOTICE
, "user '%s' granted access", username
);
1042 /* we don't know anything about this return value */
1043 _pam_log(LOG_ERR
, "internal module error (retval = %d, user = `%s')",
1045 return PAM_SERVICE_ERR
;
1048 /* should not be reached */
1053 int pam_sm_open_session(pam_handle_t
*pamh
, int flags
,
1054 int argc
, const char **argv
)
1056 /* parse arguments */
1057 int ctrl
= _pam_parse(argc
, argv
, NULL
);
1059 return PAM_SYSTEM_ERR
;
1062 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_open_session handler (flags: 0x%04x)", flags
);
1068 int pam_sm_close_session(pam_handle_t
*pamh
, int flags
,
1069 int argc
, const char **argv
)
1072 int retval
= PAM_SUCCESS
;
1074 /* parse arguments */
1075 int ctrl
= _pam_parse(argc
, argv
, &d
);
1077 retval
= PAM_SYSTEM_ERR
;
1081 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_close_session handler (flags: 0x%04x)", flags
);
1083 if (!(flags
& PAM_DELETE_CRED
)) {
1084 retval
= PAM_SUCCESS
;
1088 if (ctrl
& WINBIND_KRB5_AUTH
) {
1090 /* destroy the ccache here */
1091 struct winbindd_request request
;
1092 struct winbindd_response response
;
1094 const char *ccname
= NULL
;
1095 struct passwd
*pwd
= NULL
;
1097 ZERO_STRUCT(request
);
1098 ZERO_STRUCT(response
);
1100 retval
= pam_get_user(pamh
, &user
, "Username: ");
1101 if (retval
== PAM_SUCCESS
) {
1103 _pam_log(LOG_ERR
, "username was NULL!");
1104 retval
= PAM_USER_UNKNOWN
;
1107 if (retval
== PAM_SUCCESS
) {
1108 _pam_log_debug(ctrl
, LOG_DEBUG
, "username [%s] obtained", user
);
1111 _pam_log_debug(ctrl
, LOG_DEBUG
, "could not identify user");
1115 ccname
= pam_getenv(pamh
, "KRB5CCNAME");
1116 if (ccname
== NULL
) {
1117 _pam_log_debug(ctrl
, LOG_DEBUG
, "user has no KRB5CCNAME environment");
1118 retval
= PAM_SUCCESS
;
1122 strncpy(request
.data
.logoff
.user
, user
,
1123 sizeof(request
.data
.logoff
.user
) - 1);
1125 strncpy(request
.data
.logoff
.krb5ccname
, ccname
,
1126 sizeof(request
.data
.logoff
.krb5ccname
) - 1);
1128 pwd
= getpwnam(user
);
1130 retval
= PAM_USER_UNKNOWN
;
1133 request
.data
.logoff
.uid
= pwd
->pw_uid
;
1135 request
.flags
= WBFLAG_PAM_KRB5
| WBFLAG_PAM_CONTACT_TRUSTDOM
;
1137 retval
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_PAM_LOGOFF
, &request
, &response
, user
);
1142 iniparser_freedict(d
);
1150 int pam_sm_chauthtok(pam_handle_t
* pamh
, int flags
,
1151 int argc
, const char **argv
)
1157 /* <DO NOT free() THESE> */
1159 char *pass_old
, *pass_new
;
1160 /* </DO NOT free() THESE> */
1167 ctrl
= _pam_parse(argc
, argv
, &d
);
1169 retval
= PAM_SYSTEM_ERR
;
1173 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_chauthtok (flags: 0x%04x)", flags
);
1175 /* clearing offline bit for the auth in the password change */
1176 ctrl
&= ~WINBIND_CACHED_LOGIN
;
1179 * First get the name of a user
1181 retval
= pam_get_user(pamh
, &user
, "Username: ");
1182 if (retval
== PAM_SUCCESS
) {
1184 _pam_log(LOG_ERR
, "username was NULL!");
1185 retval
= PAM_USER_UNKNOWN
;
1188 if (retval
== PAM_SUCCESS
) {
1189 _pam_log_debug(ctrl
, LOG_DEBUG
, "username [%s] obtained",
1193 _pam_log_debug(ctrl
, LOG_DEBUG
,
1194 "password - could not identify user");
1198 /* check if this is really a user in winbindd, not only in NSS */
1199 retval
= valid_user(user
, pamh
, ctrl
);
1202 retval
= PAM_USER_UNKNOWN
;
1205 retval
= PAM_SYSTEM_ERR
;
1212 * obtain and verify the current password (OLDAUTHTOK) for
1216 if (flags
& PAM_PRELIM_CHECK
) {
1218 time_t pwdlastset_prelim
= 0;
1220 /* instruct user what is happening */
1221 #define greeting "Changing password for "
1222 Announce
= (char *) malloc(sizeof(greeting
) + strlen(user
));
1223 if (Announce
== NULL
) {
1224 _pam_log(LOG_CRIT
, "password - out of memory");
1225 retval
= PAM_BUF_ERR
;
1228 (void) strcpy(Announce
, greeting
);
1229 (void) strcpy(Announce
+ sizeof(greeting
) - 1, user
);
1232 lctrl
= ctrl
| WINBIND__OLD_PASSWORD
;
1233 retval
= _winbind_read_password(pamh
, lctrl
,
1235 "(current) NT password: ",
1237 (const char **) &pass_old
);
1238 if (retval
!= PAM_SUCCESS
) {
1239 _pam_log(LOG_NOTICE
, "password - (old) token not obtained");
1242 /* verify that this is the password for this user */
1244 retval
= winbind_auth_request(pamh
, ctrl
, user
, pass_old
, NULL
, NULL
, False
, &pwdlastset_prelim
);
1246 if (retval
!= PAM_ACCT_EXPIRED
&&
1247 retval
!= PAM_AUTHTOK_EXPIRED
&&
1248 retval
!= PAM_NEW_AUTHTOK_REQD
&&
1249 retval
!= PAM_SUCCESS
) {
1254 pam_set_data(pamh
, PAM_WINBIND_PWD_LAST_SET
, (void *)pwdlastset_prelim
, _pam_winbind_cleanup_func
);
1256 retval
= pam_set_item(pamh
, PAM_OLDAUTHTOK
, (const void *) pass_old
);
1258 if (retval
!= PAM_SUCCESS
) {
1259 _pam_log(LOG_CRIT
, "failed to set PAM_OLDAUTHTOK");
1261 } else if (flags
& PAM_UPDATE_AUTHTOK
) {
1263 time_t pwdlastset_update
= 0;
1266 * obtain the proposed password
1270 * get the old token back.
1273 retval
= pam_get_item(pamh
, PAM_OLDAUTHTOK
,
1274 (const void **) &pass_old
);
1276 if (retval
!= PAM_SUCCESS
) {
1277 _pam_log(LOG_NOTICE
, "user not authenticated");
1283 if (on(WINBIND_USE_AUTHTOK_ARG
, lctrl
)) {
1284 lctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
1287 retval
= PAM_AUTHTOK_ERR
;
1288 while ((retval
!= PAM_SUCCESS
) && (retry
++ < MAX_PASSWD_TRIES
)) {
1290 * use_authtok is to force the use of a previously entered
1291 * password -- needed for pluggable password strength checking
1294 retval
= _winbind_read_password(pamh
, lctrl
,
1296 "Enter new NT password: ",
1297 "Retype new NT password: ",
1298 (const char **) &pass_new
);
1300 if (retval
!= PAM_SUCCESS
) {
1301 _pam_log_debug(ctrl
, LOG_ALERT
1302 ,"password - new password not obtained");
1303 pass_old
= NULL
;/* tidy up */
1308 * At this point we know who the user is and what they
1309 * propose as their new password. Verify that the new
1310 * password is acceptable.
1313 if (pass_new
[0] == '\0') {/* "\0" password = NULL */
1319 * By reaching here we have approved the passwords and must now
1320 * rebuild the password database file.
1322 pam_get_data( pamh
, PAM_WINBIND_PWD_LAST_SET
, (const void **)&pwdlastset_update
);
1324 retval
= winbind_chauthtok_request(pamh
, ctrl
, user
, pass_old
, pass_new
, pwdlastset_update
);
1326 _pam_overwrite(pass_new
);
1327 _pam_overwrite(pass_old
);
1328 pass_old
= pass_new
= NULL
;
1332 /* just in case we need krb5 creds after a password change over msrpc */
1334 if (ctrl
& WINBIND_KRB5_AUTH
) {
1336 const char *member
= get_member_from_config(argc
, argv
, ctrl
, d
);
1337 const char *cctype
= get_krb5_cc_type_from_config(argc
, argv
, ctrl
, d
);
1339 retval
= winbind_auth_request(pamh
, ctrl
, user
, pass_new
, member
, cctype
, False
, NULL
);
1340 _pam_overwrite(pass_new
);
1341 _pam_overwrite(pass_old
);
1342 pass_old
= pass_new
= NULL
;
1345 retval
= PAM_SERVICE_ERR
;
1350 iniparser_freedict(d
);
1357 /* static module data */
1359 struct pam_module _pam_winbind_modstruct
= {
1361 pam_sm_authenticate
,
1364 pam_sm_open_session
,
1365 pam_sm_close_session
,
1372 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
1373 * Copyright (c) Tim Potter <tpot@samba.org> 2000
1374 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1375 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2006
1376 * Copyright (c) Jan Rêkorajski 1999.
1377 * Copyright (c) Andrew G. Morgan 1996-8.
1378 * Copyright (c) Alex O. Yuriev, 1996.
1379 * Copyright (c) Cristian Gafton 1996.
1380 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
1382 * Redistribution and use in source and binary forms, with or without
1383 * modification, are permitted provided that the following conditions
1385 * 1. Redistributions of source code must retain the above copyright
1386 * notice, and the entire permission notice in its entirety,
1387 * including the disclaimer of warranties.
1388 * 2. Redistributions in binary form must reproduce the above copyright
1389 * notice, this list of conditions and the following disclaimer in the
1390 * documentation and/or other materials provided with the distribution.
1391 * 3. The name of the author may not be used to endorse or promote
1392 * products derived from this software without specific prior
1393 * written permission.
1395 * ALTERNATIVELY, this product may be distributed under the terms of
1396 * the GNU Public License, in which case the provisions of the GPL are
1397 * required INSTEAD OF the above restrictions. (This clause is
1398 * necessary due to a potential bad interaction between the GPL and
1399 * the restrictions contained in a BSD-style copyright.)
1401 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
1402 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1403 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1404 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1405 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1406 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1407 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1408 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1409 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1410 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1411 * OF THE POSSIBILITY OF SUCH DAMAGE.