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)
14 #include "pam_winbind.h"
18 #define MAX_PASSWD_TRIES 3
21 static void _pam_log(int err
, const char *format
, ...)
25 va_start(args
, format
);
26 openlog(MODULE_NAME
, LOG_CONS
|LOG_PID
, LOG_AUTH
);
27 vsyslog(err
, format
, args
);
32 static void _pam_log_debug(int ctrl
, int err
, const char *format
, ...)
36 if (!(ctrl
& WINBIND_DEBUG_ARG
)) {
40 va_start(args
, format
);
41 openlog(MODULE_NAME
, LOG_CONS
|LOG_PID
, LOG_AUTH
);
42 vsyslog(err
, format
, args
);
47 static int _pam_parse(int argc
, const char **argv
)
53 if (!lp_load(dyn_CONFIGFILE
,True
,False
,False
,True
)) {
57 if (lp_parm_bool(-1, "pam_winbind", "cached_login", False
)) {
58 ctrl
|= WINBIND_CACHED_LOGIN
;
60 if (lp_parm_bool(-1, "pam_winbind", "krb5_auth", False
)) {
61 ctrl
|= WINBIND_KRB5_AUTH
;
63 if (lp_parm_const_string(-1, "pam_winbind", "krb5_ccache_type", NULL
) != NULL
) {
64 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
66 if ((lp_parm_const_string(-1, "pam_winbind", "require-membership-of", NULL
) != NULL
) ||
67 (lp_parm_const_string(-1, "pam_winbind", "require_membership_of", NULL
) != NULL
)) {
68 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
70 if (lp_parm_bool(-1, "pam_winbind", "create_homedir", False
)) {
71 ctrl
|= WINBIND_CREATE_HOMEDIR
;
74 /* step through arguments */
75 for (; argc
-- > 0; ++argv
) {
79 if (!StrCaseCmp(*argv
, "debug"))
80 ctrl
|= WINBIND_DEBUG_ARG
;
81 else if (strequal(*argv
, "use_authtok"))
82 ctrl
|= WINBIND_USE_AUTHTOK_ARG
;
83 else if (strequal(*argv
, "use_first_pass"))
84 ctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
85 else if (strequal(*argv
, "try_first_pass"))
86 ctrl
|= WINBIND_TRY_FIRST_PASS_ARG
;
87 else if (strequal(*argv
, "unknown_ok"))
88 ctrl
|= WINBIND_UNKNOWN_OK_ARG
;
89 else if (strnequal(*argv
, "require_membership_of", strlen("require_membership_of")))
90 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
91 else if (strnequal(*argv
, "require-membership-of", strlen("require-membership-of")))
92 ctrl
|= WINBIND_REQUIRED_MEMBERSHIP
;
93 else if (strequal(*argv
, "krb5_auth"))
94 ctrl
|= WINBIND_KRB5_AUTH
;
95 else if (strnequal(*argv
, "krb5_ccache_type", strlen("krb5_ccache_type")))
96 ctrl
|= WINBIND_KRB5_CCACHE_TYPE
;
97 else if (strequal(*argv
, "cached_login"))
98 ctrl
|= WINBIND_CACHED_LOGIN
;
99 else if (strequal(*argv
, "create_homedir"))
100 ctrl
|= WINBIND_CREATE_HOMEDIR
;
102 _pam_log(LOG_ERR
, "pam_parse: unknown option; %s", *argv
);
109 static void _pam_winbind_cleanup_func(pam_handle_t
*pamh
, void *data
, int error_status
)
114 static const struct ntstatus_errors
{
115 const char *ntstatus_string
;
116 const char *error_string
;
117 } ntstatus_errors
[] = {
118 {"NT_STATUS_OK", "Success"},
119 {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
120 {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
121 {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
122 {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
123 {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
124 {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
125 {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
126 {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
127 {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
128 {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
129 {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
130 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
131 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
132 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
133 {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
137 const char *_get_ntstatus_error_string(const char *nt_status_string
)
140 for (i
=0; ntstatus_errors
[i
].ntstatus_string
!= NULL
; i
++) {
141 if (strequal(ntstatus_errors
[i
].ntstatus_string
, nt_status_string
)) {
142 return ntstatus_errors
[i
].error_string
;
148 /* --- authentication management functions --- */
150 /* Attempt a conversation */
152 static int converse(pam_handle_t
*pamh
, int nargs
,
153 struct pam_message
**message
,
154 struct pam_response
**response
)
157 struct pam_conv
*conv
;
159 retval
= pam_get_item(pamh
, PAM_CONV
, (const void **) &conv
);
160 if (retval
== PAM_SUCCESS
) {
161 retval
= conv
->conv(nargs
, (const struct pam_message
**)message
,
162 response
, conv
->appdata_ptr
);
165 return retval
; /* propagate error status */
169 static int _make_remark(pam_handle_t
* pamh
, int type
, const char *text
)
171 int retval
= PAM_SUCCESS
;
173 struct pam_message
*pmsg
[1], msg
[1];
174 struct pam_response
*resp
;
178 msg
[0].msg_style
= type
;
181 retval
= converse(pamh
, 1, pmsg
, &resp
);
184 _pam_drop_reply(resp
, 1);
189 static int _make_remark_format(pam_handle_t
* pamh
, int type
, const char *format
, ...)
194 va_start(args
, format
);
195 vasprintf(&var
, format
, args
);
198 return _make_remark(pamh
, type
, var
);
201 static int pam_winbind_request(pam_handle_t
* pamh
, int ctrl
,
202 enum winbindd_cmd req_type
,
203 struct winbindd_request
*request
,
204 struct winbindd_response
*response
)
206 /* Fill in request and send down pipe */
207 init_request(request
, req_type
);
209 if (write_sock(request
, sizeof(*request
), 0) == -1) {
210 _pam_log(LOG_ERR
, "write to socket failed!");
212 return PAM_SERVICE_ERR
;
216 if (read_reply(response
) == -1) {
217 _pam_log(LOG_ERR
, "read from socket failed!");
219 return PAM_SERVICE_ERR
;
222 /* We are done with the socket - close it and avoid mischeif */
225 /* Copy reply data from socket */
226 if (response
->result
!= WINBINDD_OK
) {
227 if (response
->data
.auth
.pam_error
!= PAM_SUCCESS
) {
228 _pam_log(LOG_ERR
, "request failed: %s, PAM error was %d, NT error was %s",
229 response
->data
.auth
.error_string
,
230 response
->data
.auth
.pam_error
,
231 response
->data
.auth
.nt_status_string
);
232 return response
->data
.auth
.pam_error
;
234 _pam_log(LOG_ERR
, "request failed, but PAM error 0!");
235 return PAM_SERVICE_ERR
;
242 static int pam_winbind_request_log(pam_handle_t
* pamh
,
244 enum winbindd_cmd req_type
,
245 struct winbindd_request
*request
,
246 struct winbindd_response
*response
,
251 retval
= pam_winbind_request(pamh
, ctrl
, req_type
, request
, response
);
255 /* incorrect password */
256 _pam_log(LOG_WARNING
, "user `%s' denied access (incorrect password or invalid membership)", user
);
258 case PAM_ACCT_EXPIRED
:
259 /* account expired */
260 _pam_log(LOG_WARNING
, "user `%s' account expired", user
);
262 case PAM_AUTHTOK_EXPIRED
:
263 /* password expired */
264 _pam_log(LOG_WARNING
, "user `%s' password expired", user
);
266 case PAM_NEW_AUTHTOK_REQD
:
267 /* new password required */
268 _pam_log(LOG_WARNING
, "user `%s' new password required", user
);
270 case PAM_USER_UNKNOWN
:
271 /* the user does not exist */
272 _pam_log_debug(ctrl
, LOG_NOTICE
, "user `%s' not found",
274 if (ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
279 if (req_type
== WINBINDD_PAM_AUTH
) {
280 /* Otherwise, the authentication looked good */
281 _pam_log(LOG_NOTICE
, "user '%s' granted access", user
);
282 } else if (req_type
== WINBINDD_PAM_CHAUTHTOK
) {
283 /* Otherwise, the authentication looked good */
284 _pam_log(LOG_NOTICE
, "user '%s' password changed", user
);
286 /* Otherwise, the authentication looked good */
287 _pam_log(LOG_NOTICE
, "user '%s' OK", user
);
292 /* we don't know anything about this return value */
293 _pam_log(LOG_ERR
, "internal module error (retval = %d, user = `%s')",
299 /* talk to winbindd */
300 static int winbind_auth_request(pam_handle_t
* pamh
,
308 struct winbindd_request request
;
309 struct winbindd_response response
;
312 ZERO_STRUCT(request
);
313 ZERO_STRUCT(response
);
315 strncpy(request
.data
.auth
.user
, user
,
316 sizeof(request
.data
.auth
.user
)-1);
318 strncpy(request
.data
.auth
.pass
, pass
,
319 sizeof(request
.data
.auth
.pass
)-1);
321 request
.data
.auth
.krb5_cc_type
[0] = '\0';
322 request
.data
.auth
.uid
= -1;
324 request
.flags
= WBFLAG_PAM_INFO3_TEXT
;
326 if (ctrl
& WINBIND_KRB5_AUTH
) {
328 struct passwd
*pwd
= NULL
;
330 _pam_log_debug(ctrl
, LOG_DEBUG
, "enabling krb5 login flag\n");
332 request
.flags
|= WBFLAG_PAM_KRB5
| WBFLAG_PAM_FALLBACK_AFTER_KRB5
;
334 pwd
= getpwnam(user
);
336 return PAM_USER_UNKNOWN
;
338 request
.data
.auth
.uid
= pwd
->pw_uid
;
341 if (ctrl
& WINBIND_CACHED_LOGIN
) {
342 _pam_log_debug(ctrl
, LOG_DEBUG
, "enabling cached login flag\n");
343 request
.flags
|= WBFLAG_PAM_CACHED_LOGIN
;
346 if (cctype
!= NULL
) {
347 strncpy(request
.data
.auth
.krb5_cc_type
, cctype
,
348 sizeof(request
.data
.auth
.krb5_cc_type
) - 1);
349 _pam_log_debug(ctrl
, LOG_DEBUG
, "enabling request for a %s krb5 ccache\n", cctype
);
352 request
.data
.auth
.require_membership_of_sid
[0] = '\0';
354 if (member
!= NULL
) {
355 strncpy(request
.data
.auth
.require_membership_of_sid
, member
,
356 sizeof(request
.data
.auth
.require_membership_of_sid
)-1);
360 if ( (member
!= NULL
) && (strncmp("S-", member
, 2) != 0) ) {
362 struct winbindd_request sid_request
;
363 struct winbindd_response sid_response
;
365 ZERO_STRUCT(sid_request
);
366 ZERO_STRUCT(sid_response
);
368 _pam_log_debug(ctrl
, LOG_DEBUG
, "no sid given, looking up: %s\n", member
);
370 /* fortunatly winbindd can handle non-separated names */
371 fstrcpy(sid_request
.data
.name
.name
, member
);
373 if (pam_winbind_request_log(pamh
, ctrl
, WINBINDD_LOOKUPNAME
, &sid_request
, &sid_response
, user
)) {
374 _pam_log(LOG_INFO
, "could not lookup name: %s\n", member
);
378 member
= sid_response
.data
.sid
.sid
;
380 strncpy(request
.data
.auth
.require_membership_of_sid
, member
,
381 sizeof(request
.data
.auth
.require_membership_of_sid
)-1);
384 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_PAM_AUTH
, &request
, &response
, user
);
386 if ((ctrl
& WINBIND_KRB5_AUTH
) &&
387 response
.data
.auth
.krb5ccname
[0] != '\0') {
391 _pam_log_debug(ctrl
, LOG_DEBUG
, "request returned KRB5CCNAME: %s",
392 response
.data
.auth
.krb5ccname
);
394 snprintf(var
, sizeof(var
), "KRB5CCNAME=%s", response
.data
.auth
.krb5ccname
);
396 ret
= pam_putenv(pamh
, var
);
397 if (ret
!= PAM_SUCCESS
) {
398 _pam_log(LOG_ERR
, "failed to set KRB5CCNAME to %s", var
);
403 if (!process_result
) {
408 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PASSWORD_EXPIRED");
409 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PASSWORD_MUST_CHANGE");
410 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_INVALID_WORKSTATION");
411 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_INVALID_LOGON_HOURS");
412 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_ACCOUNT_EXPIRED");
413 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_ACCOUNT_DISABLED");
414 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_ACCOUNT_LOCKED_OUT");
415 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
416 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
417 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
420 /* handle the case where the auth was ok, but the password must expire right now */
421 /* good catch from Ralf Haferkamp: an expiry of "never" is translated to -1 */
422 if ( ! (response
.data
.auth
.info3
.acct_flags
& ACB_PWNOEXP
) &&
423 (response
.data
.auth
.policy
.expire
> 0) &&
424 (response
.data
.auth
.info3
.pass_last_set_time
+ response
.data
.auth
.policy
.expire
< time(NULL
))) {
426 ret
= PAM_AUTHTOK_EXPIRED
;
428 _pam_log_debug(ctrl
, LOG_DEBUG
,"Password has expired (Password was last set: %d, "
429 "the policy says it should expire here %d (now it's: %d)\n",
430 response
.data
.auth
.info3
.pass_last_set_time
,
431 response
.data
.auth
.info3
.pass_last_set_time
+ response
.data
.auth
.policy
.expire
,
434 PAM_WB_REMARK_DIRECT_RET(pamh
, "NT_STATUS_PASSWORD_EXPIRED");
438 /* warn a user if the password is about to expire soon */
439 if ( ! (response
.data
.auth
.info3
.acct_flags
& ACB_PWNOEXP
) &&
440 (response
.data
.auth
.policy
.expire
) &&
441 (response
.data
.auth
.info3
.pass_last_set_time
+ response
.data
.auth
.policy
.expire
> time(NULL
) ) ) {
443 int days
= response
.data
.auth
.policy
.expire
/ SECONDS_PER_DAY
;
444 if (days
<= DAYS_TO_WARN_BEFORE_PWD_EXPIRES
) {
445 _make_remark_format(pamh
, PAM_TEXT_INFO
, "Your password will expire in %d days", days
);
449 if (response
.data
.auth
.info3
.user_flgs
& LOGON_CACHED_ACCOUNT
) {
450 _make_remark(pamh
, PAM_ERROR_MSG
, "Logging on using cached account. Network ressources can be unavailable");
453 /* save the CIFS homedir for pam_cifs / pam_mount */
454 if (response
.data
.auth
.info3
.home_dir
[0] != '\0') {
457 if (!asprintf(&buf
, "%s", response
.data
.auth
.info3
.home_dir
)) {
461 pam_set_data( pamh
, PAM_WINBIND_HOMEDIR
, (void *)buf
, _pam_winbind_cleanup_func
);
467 /* talk to winbindd */
468 static int winbind_chauthtok_request(pam_handle_t
* pamh
,
474 struct winbindd_request request
;
475 struct winbindd_response response
;
478 ZERO_STRUCT(request
);
479 ZERO_STRUCT(response
);
481 if (request
.data
.chauthtok
.user
== NULL
) return -2;
483 strncpy(request
.data
.chauthtok
.user
, user
,
484 sizeof(request
.data
.chauthtok
.user
) - 1);
486 if (oldpass
!= NULL
) {
487 strncpy(request
.data
.chauthtok
.oldpass
, oldpass
,
488 sizeof(request
.data
.chauthtok
.oldpass
) - 1);
490 request
.data
.chauthtok
.oldpass
[0] = '\0';
493 if (newpass
!= NULL
) {
494 strncpy(request
.data
.chauthtok
.newpass
, newpass
,
495 sizeof(request
.data
.chauthtok
.newpass
) - 1);
497 request
.data
.chauthtok
.newpass
[0] = '\0';
500 if (ctrl
& WINBIND_KRB5_AUTH
) {
501 request
.flags
= WBFLAG_PAM_KRB5
;
504 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_PAM_CHAUTHTOK
, &request
, &response
, user
);
506 if (ret
== PAM_SUCCESS
) {
510 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_BACKUP_CONTROLLER");
511 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_ACCESS_DENIED");
513 /* TODO: tell the min pwd length ? */
514 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PWD_TOO_SHORT");
516 /* TODO: tell the minage ? */
517 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PWD_TOO_RECENT");
519 /* TODO: tell the history length ? */
520 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh
, response
, "NT_STATUS_PWD_HISTORY_CONFLICT");
522 if (strequal(response
.data
.auth
.nt_status_string
, "NT_STATUS_PASSWORD_RESTRICTION")) {
524 /* FIXME: avoid to send multiple PAM messages after another */
525 switch (response
.data
.auth
.reject_reason
) {
528 case REJECT_REASON_TOO_SHORT
:
529 PAM_WB_REMARK_DIRECT(pamh
, "NT_STATUS_PWD_TOO_SHORT");
531 case REJECT_REASON_IN_HISTORY
:
532 PAM_WB_REMARK_DIRECT(pamh
, "NT_STATUS_PWD_HISTORY_CONFLICT");
534 case REJECT_REASON_NOT_COMPLEX
:
535 _make_remark(pamh
, PAM_ERROR_MSG
, "Password does not meet complexity requirements");
538 _pam_log_debug(ctrl
, LOG_DEBUG
,
539 "unknown password change reject reason: %d",
540 response
.data
.auth
.reject_reason
);
544 _make_remark_format(pamh
, PAM_ERROR_MSG
,
545 "Your password must be at least %d characters; "
546 "cannot repeat any of the your previous %d passwords"
548 "Please type a different password. "
549 "Type a password which meets these requirements in both text boxes.",
550 response
.data
.auth
.policy
.min_length_password
,
551 response
.data
.auth
.policy
.password_history
,
552 (response
.data
.auth
.policy
.password_properties
& DOMAIN_PASSWORD_COMPLEX
) ?
553 "; must contain capitals, numerals or punctuation; and cannot contain your account or full name" :
562 * Checks if a user has an account
569 static int valid_user(const char *user
, pam_handle_t
*pamh
, int ctrl
)
571 /* check not only if the user is available over NSS calls, also make
572 * sure it's really a winbind user, this is important when stacking PAM
573 * modules in the 'account' or 'password' facility. */
575 struct passwd
*pwd
= NULL
;
576 struct winbindd_request request
;
577 struct winbindd_response response
;
580 ZERO_STRUCT(request
);
581 ZERO_STRUCT(response
);
583 pwd
= getpwnam(user
);
588 fstrcpy(request
.data
.username
, user
);
590 ret
= pam_winbind_request_log(pamh
, ctrl
, WINBINDD_GETPWNAM
, &request
, &response
, user
);
593 case PAM_USER_UNKNOWN
:
603 static char *_pam_delete(register char *xx
)
611 * obtain a password from the user
614 static int _winbind_read_password(pam_handle_t
* pamh
,
627 * make sure nothing inappropriate gets returned
630 *pass
= token
= NULL
;
633 * which authentication token are we getting?
636 authtok_flag
= on(WINBIND__OLD_PASSWORD
, ctrl
) ? PAM_OLDAUTHTOK
: PAM_AUTHTOK
;
639 * should we obtain the password from a PAM item ?
642 if (on(WINBIND_TRY_FIRST_PASS_ARG
, ctrl
) || on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
643 retval
= pam_get_item(pamh
, authtok_flag
, (const void **) &item
);
644 if (retval
!= PAM_SUCCESS
) {
647 "pam_get_item returned error to unix-read-password"
650 } else if (item
!= NULL
) { /* we have a password! */
654 } else if (on(WINBIND_USE_FIRST_PASS_ARG
, ctrl
)) {
655 return PAM_AUTHTOK_RECOVER_ERR
; /* didn't work */
656 } else if (on(WINBIND_USE_AUTHTOK_ARG
, ctrl
)
657 && off(WINBIND__OLD_PASSWORD
, ctrl
)) {
658 return PAM_AUTHTOK_RECOVER_ERR
;
662 * getting here implies we will have to get the password from the
667 struct pam_message msg
[3], *pmsg
[3];
668 struct pam_response
*resp
;
671 /* prepare to converse */
673 if (comment
!= NULL
) {
675 msg
[0].msg_style
= PAM_TEXT_INFO
;
676 msg
[0].msg
= comment
;
683 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
684 msg
[i
++].msg
= prompt1
;
687 if (prompt2
!= NULL
) {
689 msg
[i
].msg_style
= PAM_PROMPT_ECHO_OFF
;
690 msg
[i
++].msg
= prompt2
;
693 /* so call the conversation expecting i responses */
695 retval
= converse(pamh
, i
, pmsg
, &resp
);
699 /* interpret the response */
701 if (retval
== PAM_SUCCESS
) { /* a good conversation */
703 token
= SMB_STRDUP(resp
[i
- replies
].resp
);
706 /* verify that password entered correctly */
707 if (!resp
[i
- 1].resp
708 || StrCaseCmp(token
, resp
[i
- 1].resp
)) {
709 _pam_delete(token
); /* mistyped */
710 retval
= PAM_AUTHTOK_RECOVER_ERR
;
711 _make_remark(pamh
, PAM_ERROR_MSG
, MISTYPED_PASS
);
716 ,"could not recover authentication token");
721 * tidy up the conversation (resp_retcode) is ignored
722 * -- what is it for anyway? AGM
725 _pam_drop_reply(resp
, i
);
728 retval
= (retval
== PAM_SUCCESS
)
729 ? PAM_AUTHTOK_RECOVER_ERR
: retval
;
733 if (retval
!= PAM_SUCCESS
) {
734 _pam_log_debug(ctrl
, LOG_DEBUG
,
735 "unable to obtain a password");
738 /* 'token' is the entered password */
740 /* we store this password as an item */
742 retval
= pam_set_item(pamh
, authtok_flag
, token
);
743 _pam_delete(token
); /* clean it up */
744 if (retval
!= PAM_SUCCESS
||
745 (retval
= pam_get_item(pamh
, authtok_flag
, (const void **) &item
)) != PAM_SUCCESS
) {
747 _pam_log(LOG_CRIT
, "error manipulating password");
753 item
= NULL
; /* break link to password */
758 const char *get_conf_item_string(int argc
,
766 const char *parm_opt
= NULL
;
768 if (!(ctrl
& flag
)) {
772 /* let the pam opt take precedence over the smb.conf option */
773 parm_opt
= lp_parm_const_string(-1, "pam_winbind", item
, NULL
);
775 for ( i
=0; i
<argc
; i
++ ) {
777 if ((strncmp(argv
[i
], item
, strlen(item
)) == 0)) {
780 parm
= SMB_STRDUP(argv
[i
]);
782 if ( (p
= strchr( parm
, '=' )) == NULL
) {
783 _pam_log(LOG_INFO
, "no \"=\" delimiter for \"%s\" found\n", item
);
787 _pam_log_debug(ctrl
, LOG_INFO
, "PAM config: %s '%s'\n", item
, p
+1);
792 _pam_log_debug(ctrl
, LOG_INFO
, "CONFIG file: %s '%s'\n", item
, parm_opt
);
798 const char *get_krb5_cc_type_from_config(int argc
, const char **argv
, int ctrl
)
800 return get_conf_item_string(argc
, argv
, ctrl
, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE
);
803 const char *get_member_from_config(int argc
, const char **argv
, int ctrl
)
805 const char *ret
= NULL
;
806 ret
= get_conf_item_string(argc
, argv
, ctrl
, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP
);
810 return get_conf_item_string(argc
, argv
, ctrl
, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP
);
814 int pam_sm_authenticate(pam_handle_t
*pamh
, int flags
,
815 int argc
, const char **argv
)
817 const char *username
;
818 const char *password
;
819 const char *member
= NULL
;
820 const char *cctype
= NULL
;
821 int retval
= PAM_AUTH_ERR
;
823 /* parse arguments */
824 int ctrl
= _pam_parse(argc
, argv
);
826 return PAM_SYSTEM_ERR
;
829 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_authenticate");
831 /* Get the username */
832 retval
= pam_get_user(pamh
, &username
, NULL
);
833 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
834 _pam_log_debug(ctrl
, LOG_DEBUG
, "can not get the username");
835 return PAM_SERVICE_ERR
;
838 retval
= _winbind_read_password(pamh
, ctrl
, NULL
,
842 if (retval
!= PAM_SUCCESS
) {
843 _pam_log(LOG_ERR
, "Could not retrieve user's password");
844 return PAM_AUTHTOK_ERR
;
847 /* Let's not give too much away in the log file */
849 #ifdef DEBUG_PASSWORD
850 _pam_log_debug(ctrl
, LOG_INFO
, "Verify user `%s' with password `%s'",
853 _pam_log_debug(ctrl
, LOG_INFO
, "Verify user `%s'", username
);
856 member
= get_member_from_config(argc
, argv
, ctrl
);
858 cctype
= get_krb5_cc_type_from_config(argc
, argv
, ctrl
);
860 /* Now use the username to look up password */
861 retval
= winbind_auth_request(pamh
, ctrl
, username
, password
, member
, cctype
, True
);
863 if (retval
== PAM_NEW_AUTHTOK_REQD
||
864 retval
== PAM_AUTHTOK_EXPIRED
) {
868 if (!asprintf(&buf
, "%d", retval
)) {
872 pam_set_data( pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, (void *)buf
, _pam_winbind_cleanup_func
);
881 int pam_sm_setcred(pam_handle_t
*pamh
, int flags
,
882 int argc
, const char **argv
)
884 /* parse arguments */
885 int ctrl
= _pam_parse(argc
, argv
);
887 return PAM_SYSTEM_ERR
;
890 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_setcred");
892 if (flags
& PAM_DELETE_CRED
) {
893 return pam_sm_close_session(pamh
, flags
, argc
, argv
);
900 * Account management. We want to verify that the account exists
901 * before returning PAM_SUCCESS
904 int pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags
,
905 int argc
, const char **argv
)
907 const char *username
;
908 int retval
= PAM_USER_UNKNOWN
;
911 /* parse arguments */
912 int ctrl
= _pam_parse(argc
, argv
);
914 return PAM_SYSTEM_ERR
;
917 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_acct_mgmt");
920 /* Get the username */
921 retval
= pam_get_user(pamh
, &username
, NULL
);
922 if ((retval
!= PAM_SUCCESS
) || (!username
)) {
923 _pam_log_debug(ctrl
, LOG_DEBUG
,"can not get the username");
924 return PAM_SERVICE_ERR
;
927 /* Verify the username */
928 retval
= valid_user(username
, pamh
, ctrl
);
931 /* some sort of system error. The log was already printed */
932 return PAM_SERVICE_ERR
;
934 /* the user does not exist */
935 _pam_log_debug(ctrl
, LOG_NOTICE
, "user `%s' not found", username
);
936 if (ctrl
& WINBIND_UNKNOWN_OK_ARG
) {
939 return PAM_USER_UNKNOWN
;
941 pam_get_data( pamh
, PAM_WINBIND_NEW_AUTHTOK_REQD
, (const void **)&tmp
);
945 case PAM_AUTHTOK_EXPIRED
:
946 /* fall through, since new token is required in this case */
947 case PAM_NEW_AUTHTOK_REQD
:
948 _pam_log(LOG_WARNING
, "pam_sm_acct_mgmt success but %s is set",
949 PAM_WINBIND_NEW_AUTHTOK_REQD
);
950 _pam_log(LOG_NOTICE
, "user '%s' needs new password", username
);
951 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
952 return PAM_NEW_AUTHTOK_REQD
;
954 _pam_log(LOG_WARNING
, "pam_sm_acct_mgmt success");
955 _pam_log(LOG_NOTICE
, "user '%s' granted access", username
);
960 /* Otherwise, the authentication looked good */
961 _pam_log(LOG_NOTICE
, "user '%s' granted access", username
);
964 /* we don't know anything about this return value */
965 _pam_log(LOG_ERR
, "internal module error (retval = %d, user = `%s')",
967 return PAM_SERVICE_ERR
;
970 /* should not be reached */
975 int pam_sm_open_session(pam_handle_t
*pamh
, int flags
,
976 int argc
, const char **argv
)
978 /* parse arguments */
979 int ctrl
= _pam_parse(argc
, argv
);
981 return PAM_SYSTEM_ERR
;
984 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_open_session handler");
987 if (ctrl
& WINBIND_CREATE_HOMEDIR
) {
989 struct passwd
*pwd
= NULL
;
990 const char *username
;
994 SMB_STRUCT_STAT sbuf
;
996 /* Get the username */
997 ret
= pam_get_user(pamh
, &username
, NULL
);
998 if ((ret
!= PAM_SUCCESS
) || (!username
)) {
999 _pam_log_debug(ctrl
, LOG_DEBUG
, "can not get the username");
1000 return PAM_SERVICE_ERR
;
1003 pwd
= getpwnam(username
);
1005 _pam_log_debug(ctrl
, LOG_DEBUG
, "can not get the username");
1006 return PAM_SERVICE_ERR
;
1009 _pam_log_debug(ctrl
, LOG_DEBUG
, "homedir is: %s", pwd
->pw_dir
);
1011 if (directory_exist(pwd
->pw_dir
, &sbuf
)) {
1015 fstrcpy(create_dir
, "/");
1016 while (next_token((const char **)&pwd
->pw_dir
, tok
, "/", sizeof(tok
))) {
1020 fstrcat(create_dir
, tok
);
1021 fstrcat(create_dir
, "/");
1023 if (!directory_exist(create_dir
, &sbuf
)) {
1024 if (mkdir(create_dir
, mode
) != 0) {
1025 _pam_log(LOG_ERR
, "could not create dir: %s (%s)",
1026 create_dir
, strerror(errno
));
1027 return PAM_SERVICE_ERR
;
1032 if (sys_chown(create_dir
, pwd
->pw_uid
, pwd
->pw_gid
) != 0) {
1033 _pam_log(LOG_ERR
, "failed to chown user homedir: %s (%s)",
1034 create_dir
, strerror(errno
));
1035 return PAM_SERVICE_ERR
;
1043 int pam_sm_close_session(pam_handle_t
*pamh
, int flags
,
1044 int argc
, const char **argv
)
1046 /* parse arguments */
1047 int ctrl
= _pam_parse(argc
, argv
);
1049 return PAM_SYSTEM_ERR
;
1052 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_close_session handler");
1054 if (!(flags
& PAM_DELETE_CRED
)) {
1058 if (ctrl
& WINBIND_KRB5_AUTH
) {
1060 /* destroy the ccache here */
1061 struct winbindd_request request
;
1062 struct winbindd_response response
;
1064 const char *ccname
= NULL
;
1065 struct passwd
*pwd
= NULL
;
1069 ZERO_STRUCT(request
);
1070 ZERO_STRUCT(response
);
1072 retval
= pam_get_user(pamh
, &user
, "Username: ");
1073 if (retval
== PAM_SUCCESS
) {
1075 _pam_log(LOG_ERR
, "username was NULL!");
1076 return PAM_USER_UNKNOWN
;
1078 if (retval
== PAM_SUCCESS
) {
1079 _pam_log_debug(ctrl
, LOG_DEBUG
, "username [%s] obtained", user
);
1082 _pam_log_debug(ctrl
, LOG_DEBUG
, "could not identify user");
1086 ccname
= pam_getenv(pamh
, "KRB5CCNAME");
1087 if (ccname
== NULL
) {
1088 _pam_log_debug(ctrl
, LOG_DEBUG
, "user has no KRB5CCNAME environment");
1092 fstrcpy(request
.data
.logoff
.user
, user
);
1093 fstrcpy(request
.data
.logoff
.krb5ccname
, ccname
);
1095 pwd
= getpwnam(user
);
1097 return PAM_USER_UNKNOWN
;
1099 request
.data
.logoff
.uid
= pwd
->pw_uid
;
1101 request
.flags
= WBFLAG_PAM_KRB5
;
1103 return pam_winbind_request_log(pamh
, ctrl
, WINBINDD_PAM_LOGOFF
, &request
, &response
, user
);
1112 int pam_sm_chauthtok(pam_handle_t
* pamh
, int flags
,
1113 int argc
, const char **argv
)
1119 /* <DO NOT free() THESE> */
1121 char *pass_old
, *pass_new
;
1122 /* </DO NOT free() THESE> */
1128 ctrl
= _pam_parse(argc
, argv
);
1130 return PAM_SYSTEM_ERR
;
1133 _pam_log_debug(ctrl
, LOG_DEBUG
,"pam_winbind: pam_sm_chauthtok");
1136 * First get the name of a user
1138 retval
= pam_get_user(pamh
, &user
, "Username: ");
1139 if (retval
== PAM_SUCCESS
) {
1141 _pam_log(LOG_ERR
, "username was NULL!");
1142 return PAM_USER_UNKNOWN
;
1144 if (retval
== PAM_SUCCESS
) {
1145 _pam_log_debug(ctrl
, LOG_DEBUG
, "username [%s] obtained",
1149 _pam_log_debug(ctrl
, LOG_DEBUG
,
1150 "password - could not identify user");
1154 /* check if this is really a user in winbindd, not only in NSS */
1155 retval
= valid_user(user
, pamh
, ctrl
);
1158 return PAM_USER_UNKNOWN
;
1160 return PAM_SYSTEM_ERR
;
1166 * obtain and verify the current password (OLDAUTHTOK) for
1170 if (flags
& PAM_PRELIM_CHECK
) {
1172 /* instruct user what is happening */
1173 #define greeting "Changing password for "
1174 fstrcpy(Announce
, greeting
);
1175 fstrcat(Announce
, user
);
1178 lctrl
= ctrl
| WINBIND__OLD_PASSWORD
;
1179 retval
= _winbind_read_password(pamh
, lctrl
,
1181 "(current) NT password: ",
1183 (const char **) &pass_old
);
1184 if (retval
!= PAM_SUCCESS
) {
1185 _pam_log(LOG_NOTICE
, "password - (old) token not obtained");
1188 /* verify that this is the password for this user */
1190 retval
= winbind_auth_request(pamh
, ctrl
, user
, pass_old
, NULL
, NULL
, False
);
1192 if (retval
!= PAM_ACCT_EXPIRED
1193 && retval
!= PAM_AUTHTOK_EXPIRED
1194 && retval
!= PAM_NEW_AUTHTOK_REQD
1195 && retval
!= PAM_SUCCESS
) {
1200 retval
= pam_set_item(pamh
, PAM_OLDAUTHTOK
, (const void *) pass_old
);
1202 if (retval
!= PAM_SUCCESS
) {
1203 _pam_log(LOG_CRIT
, "failed to set PAM_OLDAUTHTOK");
1205 } else if (flags
& PAM_UPDATE_AUTHTOK
) {
1208 * obtain the proposed password
1212 * get the old token back.
1215 retval
= pam_get_item(pamh
, PAM_OLDAUTHTOK
,
1216 (const void **) &pass_old
);
1218 if (retval
!= PAM_SUCCESS
) {
1219 _pam_log(LOG_NOTICE
, "user not authenticated");
1225 if (on(WINBIND_USE_AUTHTOK_ARG
, lctrl
)) {
1226 lctrl
|= WINBIND_USE_FIRST_PASS_ARG
;
1229 retval
= PAM_AUTHTOK_ERR
;
1230 while ((retval
!= PAM_SUCCESS
) && (retry
++ < MAX_PASSWD_TRIES
)) {
1232 * use_authtok is to force the use of a previously entered
1233 * password -- needed for pluggable password strength checking
1236 retval
= _winbind_read_password(pamh
, lctrl
,
1238 "Enter new NT password: ",
1239 "Retype new NT password: ",
1240 (const char **) &pass_new
);
1242 if (retval
!= PAM_SUCCESS
) {
1243 _pam_log_debug(ctrl
, LOG_ALERT
1244 ,"password - new password not obtained");
1245 pass_old
= NULL
;/* tidy up */
1250 * At this point we know who the user is and what they
1251 * propose as their new password. Verify that the new
1252 * password is acceptable.
1255 if (pass_new
[0] == '\0') {/* "\0" password = NULL */
1261 * By reaching here we have approved the passwords and must now
1262 * rebuild the password database file.
1265 retval
= winbind_chauthtok_request(pamh
, ctrl
, user
, pass_old
, pass_new
);
1267 _pam_overwrite(pass_new
);
1268 _pam_overwrite(pass_old
);
1269 pass_old
= pass_new
= NULL
;
1273 /* just in case we need krb5 creds after a password change over msrpc */
1275 if (ctrl
& WINBIND_KRB5_AUTH
) {
1277 const char *member
= get_member_from_config(argc
, argv
, ctrl
);
1278 const char *cctype
= get_krb5_cc_type_from_config(argc
, argv
, ctrl
);
1280 retval
= winbind_auth_request(pamh
, ctrl
, user
, pass_new
, member
, cctype
, False
);
1281 _pam_overwrite(pass_new
);
1282 _pam_overwrite(pass_old
);
1283 pass_old
= pass_new
= NULL
;
1286 retval
= PAM_SERVICE_ERR
;
1294 /* static module data */
1296 struct pam_module _pam_winbind_modstruct
= {
1298 pam_sm_authenticate
,
1301 pam_sm_open_session
,
1302 pam_sm_close_session
,
1309 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
1310 * Copyright (c) Tim Potter <tpot@samba.org> 2000
1311 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1312 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2006
1313 * Copyright (c) Jan Rêkorajski 1999.
1314 * Copyright (c) Andrew G. Morgan 1996-8.
1315 * Copyright (c) Alex O. Yuriev, 1996.
1316 * Copyright (c) Cristian Gafton 1996.
1317 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
1319 * Redistribution and use in source and binary forms, with or without
1320 * modification, are permitted provided that the following conditions
1322 * 1. Redistributions of source code must retain the above copyright
1323 * notice, and the entire permission notice in its entirety,
1324 * including the disclaimer of warranties.
1325 * 2. Redistributions in binary form must reproduce the above copyright
1326 * notice, this list of conditions and the following disclaimer in the
1327 * documentation and/or other materials provided with the distribution.
1328 * 3. The name of the author may not be used to endorse or promote
1329 * products derived from this software without specific prior
1330 * written permission.
1332 * ALTERNATIVELY, this product may be distributed under the terms of
1333 * the GNU Public License, in which case the provisions of the GPL are
1334 * required INSTEAD OF the above restrictions. (This clause is
1335 * necessary due to a potential bad interaction between the GPL and
1336 * the restrictions contained in a BSD-style copyright.)
1338 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
1339 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1340 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1341 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1342 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1343 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1344 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1345 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1346 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1347 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1348 * OF THE POSSIBILITY OF SUCH DAMAGE.