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 | WBFLAG_PAM_CONTACT_TRUSTDOM;
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.policy.expire > 0) &&
423 (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire < time(NULL))) {
425 ret = PAM_AUTHTOK_EXPIRED;
427 _pam_log_debug(ctrl, LOG_DEBUG,"Password has expired (Password was last set: %d, "
428 "the policy says it should expire here %d (now it's: %d)\n",
429 response.data.auth.info3.pass_last_set_time,
430 response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire,
433 PAM_WB_REMARK_DIRECT_RET(pamh, "NT_STATUS_PASSWORD_EXPIRED");
437 /* warn a user if the password is about to expire soon */
438 if ((response.data.auth.policy.expire) &&
439 (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire > time(NULL) ) ) {
441 int days = response.data.auth.policy.expire / SECONDS_PER_DAY;
442 if (days <= DAYS_TO_WARN_BEFORE_PWD_EXPIRES) {
443 _make_remark_format(pamh, PAM_TEXT_INFO, "Your password will expire in %d days", days);
447 if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
448 _make_remark(pamh, PAM_ERROR_MSG, "Logging on using cached account. Network ressources can be unavailable");
451 /* save the CIFS homedir for pam_cifs / pam_mount */
452 if (response.data.auth.info3.home_dir[0] != '\0') {
455 if (!asprintf(&buf, "%s", response.data.auth.info3.home_dir)) {
459 pam_set_data( pamh, PAM_WINBIND_HOMEDIR, (void *)buf, _pam_winbind_cleanup_func);
465 /* talk to winbindd */
466 static int winbind_chauthtok_request(pam_handle_t * pamh,
472 struct winbindd_request request;
473 struct winbindd_response response;
476 ZERO_STRUCT(request);
477 ZERO_STRUCT(response);
479 if (request.data.chauthtok.user == NULL) return -2;
481 strncpy(request.data.chauthtok.user, user,
482 sizeof(request.data.chauthtok.user) - 1);
484 if (oldpass != NULL) {
485 strncpy(request.data.chauthtok.oldpass, oldpass,
486 sizeof(request.data.chauthtok.oldpass) - 1);
488 request.data.chauthtok.oldpass[0] = '\0';
491 if (newpass != NULL) {
492 strncpy(request.data.chauthtok.newpass, newpass,
493 sizeof(request.data.chauthtok.newpass) - 1);
495 request.data.chauthtok.newpass[0] = '\0';
498 if (ctrl & WINBIND_KRB5_AUTH) {
499 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
502 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
504 if (ret == PAM_SUCCESS) {
508 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_BACKUP_CONTROLLER");
509 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCESS_DENIED");
511 /* TODO: tell the min pwd length ? */
512 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_SHORT");
514 /* TODO: tell the minage ? */
515 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_RECENT");
517 /* TODO: tell the history length ? */
518 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
520 if (strequal(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
522 /* FIXME: avoid to send multiple PAM messages after another */
523 switch (response.data.auth.reject_reason) {
526 case REJECT_REASON_TOO_SHORT:
527 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_SHORT");
529 case REJECT_REASON_IN_HISTORY:
530 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_HISTORY_CONFLICT");
532 case REJECT_REASON_NOT_COMPLEX:
533 _make_remark(pamh, PAM_ERROR_MSG, "Password does not meet complexity requirements");
536 _pam_log_debug(ctrl, LOG_DEBUG,
537 "unknown password change reject reason: %d",
538 response.data.auth.reject_reason);
542 _make_remark_format(pamh, PAM_ERROR_MSG,
543 "Your password must be at least %d characters; "
544 "cannot repeat any of the your previous %d passwords"
546 "Please type a different password. "
547 "Type a password which meets these requirements in both text boxes.",
548 response.data.auth.policy.min_length_password,
549 response.data.auth.policy.password_history,
550 (response.data.auth.policy.password_properties & DOMAIN_PASSWORD_COMPLEX) ?
551 "; must contain capitals, numerals or punctuation; and cannot contain your account or full name" :
560 * Checks if a user has an account
567 static int valid_user(const char *user)
569 if (getpwnam(user)) return 0;
573 static char *_pam_delete(register char *xx)
581 * obtain a password from the user
584 static int _winbind_read_password(pam_handle_t * pamh,
597 * make sure nothing inappropriate gets returned
600 *pass = token = NULL;
603 * which authentication token are we getting?
606 authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
609 * should we obtain the password from a PAM item ?
612 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
613 retval = pam_get_item(pamh, authtok_flag, (const void **) &item);
614 if (retval != PAM_SUCCESS) {
617 "pam_get_item returned error to unix-read-password"
620 } else if (item != NULL) { /* we have a password! */
624 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
625 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
626 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
627 && off(WINBIND__OLD_PASSWORD, ctrl)) {
628 return PAM_AUTHTOK_RECOVER_ERR;
632 * getting here implies we will have to get the password from the
637 struct pam_message msg[3], *pmsg[3];
638 struct pam_response *resp;
641 /* prepare to converse */
643 if (comment != NULL) {
645 msg[0].msg_style = PAM_TEXT_INFO;
646 msg[0].msg = comment;
653 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
654 msg[i++].msg = prompt1;
657 if (prompt2 != NULL) {
659 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
660 msg[i++].msg = prompt2;
663 /* so call the conversation expecting i responses */
665 retval = converse(pamh, i, pmsg, &resp);
669 /* interpret the response */
671 if (retval == PAM_SUCCESS) { /* a good conversation */
673 token = SMB_STRDUP(resp[i - replies].resp);
676 /* verify that password entered correctly */
677 if (!resp[i - 1].resp
678 || StrCaseCmp(token, resp[i - 1].resp)) {
679 _pam_delete(token); /* mistyped */
680 retval = PAM_AUTHTOK_RECOVER_ERR;
681 _make_remark(pamh, PAM_ERROR_MSG, MISTYPED_PASS);
686 ,"could not recover authentication token");
691 * tidy up the conversation (resp_retcode) is ignored
692 * -- what is it for anyway? AGM
695 _pam_drop_reply(resp, i);
698 retval = (retval == PAM_SUCCESS)
699 ? PAM_AUTHTOK_RECOVER_ERR : retval;
703 if (retval != PAM_SUCCESS) {
704 _pam_log_debug(ctrl, LOG_DEBUG,
705 "unable to obtain a password");
708 /* 'token' is the entered password */
710 /* we store this password as an item */
712 retval = pam_set_item(pamh, authtok_flag, token);
713 _pam_delete(token); /* clean it up */
714 if (retval != PAM_SUCCESS ||
715 (retval = pam_get_item(pamh, authtok_flag, (const void **) &item)) != PAM_SUCCESS) {
717 _pam_log(LOG_CRIT, "error manipulating password");
723 item = NULL; /* break link to password */
728 const char *get_conf_item_string(int argc,
736 const char *parm_opt = NULL;
738 if (!(ctrl & flag)) {
742 /* let the pam opt take precedence over the smb.conf option */
743 parm_opt = lp_parm_const_string(-1, "pam_winbind", item, NULL);
745 for ( i=0; i<argc; i++ ) {
747 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
750 parm = SMB_STRDUP(argv[i]);
752 if ( (p = strchr( parm, '=' )) == NULL) {
753 _pam_log(LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
757 _pam_log_debug(ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
762 _pam_log_debug(ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
768 const char *get_krb5_cc_type_from_config(int argc, const char **argv, int ctrl)
770 return get_conf_item_string(argc, argv, ctrl, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
773 const char *get_member_from_config(int argc, const char **argv, int ctrl)
775 const char *ret = NULL;
776 ret = get_conf_item_string(argc, argv, ctrl, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
780 return get_conf_item_string(argc, argv, ctrl, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
784 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
785 int argc, const char **argv)
787 const char *username;
788 const char *password;
789 const char *member = NULL;
790 const char *cctype = NULL;
791 int retval = PAM_AUTH_ERR;
793 /* parse arguments */
794 int ctrl = _pam_parse(argc, argv);
796 return PAM_SYSTEM_ERR;
799 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_authenticate");
801 /* Get the username */
802 retval = pam_get_user(pamh, &username, NULL);
803 if ((retval != PAM_SUCCESS) || (!username)) {
804 _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
805 return PAM_SERVICE_ERR;
808 retval = _winbind_read_password(pamh, ctrl, NULL,
812 if (retval != PAM_SUCCESS) {
813 _pam_log(LOG_ERR, "Could not retrieve user's password");
814 return PAM_AUTHTOK_ERR;
817 /* Let's not give too much away in the log file */
819 #ifdef DEBUG_PASSWORD
820 _pam_log_debug(ctrl, LOG_INFO, "Verify user `%s' with password `%s'",
823 _pam_log_debug(ctrl, LOG_INFO, "Verify user `%s'", username);
826 member = get_member_from_config(argc, argv, ctrl);
828 cctype = get_krb5_cc_type_from_config(argc, argv, ctrl);
830 /* Now use the username to look up password */
831 retval = winbind_auth_request(pamh, ctrl, username, password, member, cctype, True);
833 if (retval == PAM_NEW_AUTHTOK_REQD ||
834 retval == PAM_AUTHTOK_EXPIRED) {
838 if (!asprintf(&buf, "%d", retval)) {
842 pam_set_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (void *)buf, _pam_winbind_cleanup_func);
851 int pam_sm_setcred(pam_handle_t *pamh, int flags,
852 int argc, const char **argv)
854 /* parse arguments */
855 int ctrl = _pam_parse(argc, argv);
857 return PAM_SYSTEM_ERR;
860 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_setcred");
862 if (flags & PAM_DELETE_CRED) {
863 return pam_sm_close_session(pamh, flags, argc, argv);
870 * Account management. We want to verify that the account exists
871 * before returning PAM_SUCCESS
874 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
875 int argc, const char **argv)
877 const char *username;
878 int retval = PAM_USER_UNKNOWN;
881 /* parse arguments */
882 int ctrl = _pam_parse(argc, argv);
884 return PAM_SYSTEM_ERR;
887 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_acct_mgmt");
890 /* Get the username */
891 retval = pam_get_user(pamh, &username, NULL);
892 if ((retval != PAM_SUCCESS) || (!username)) {
893 _pam_log_debug(ctrl, LOG_DEBUG,"can not get the username");
894 return PAM_SERVICE_ERR;
897 /* Verify the username */
898 retval = valid_user(username);
901 /* some sort of system error. The log was already printed */
902 return PAM_SERVICE_ERR;
904 /* the user does not exist */
905 _pam_log_debug(ctrl, LOG_NOTICE, "user `%s' not found", username);
906 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
909 return PAM_USER_UNKNOWN;
911 pam_get_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (const void **)&tmp);
915 case PAM_AUTHTOK_EXPIRED:
916 /* fall through, since new token is required in this case */
917 case PAM_NEW_AUTHTOK_REQD:
918 _pam_log(LOG_WARNING, "pam_sm_acct_mgmt success but %s is set",
919 PAM_WINBIND_NEW_AUTHTOK_REQD);
920 _pam_log(LOG_NOTICE, "user '%s' needs new password", username);
921 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
922 return PAM_NEW_AUTHTOK_REQD;
924 _pam_log(LOG_WARNING, "pam_sm_acct_mgmt success");
925 _pam_log(LOG_NOTICE, "user '%s' granted access", username);
930 /* Otherwise, the authentication looked good */
931 _pam_log(LOG_NOTICE, "user '%s' granted access", username);
934 /* we don't know anything about this return value */
935 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s')",
937 return PAM_SERVICE_ERR;
940 /* should not be reached */
945 int pam_sm_open_session(pam_handle_t *pamh, int flags,
946 int argc, const char **argv)
948 /* parse arguments */
949 int ctrl = _pam_parse(argc, argv);
951 return PAM_SYSTEM_ERR;
954 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_open_session handler");
957 if (ctrl & WINBIND_CREATE_HOMEDIR) {
959 struct passwd *pwd = NULL;
960 const char *username;
964 SMB_STRUCT_STAT sbuf;
966 /* Get the username */
967 ret = pam_get_user(pamh, &username, NULL);
968 if ((ret != PAM_SUCCESS) || (!username)) {
969 _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
970 return PAM_SERVICE_ERR;
973 pwd = getpwnam(username);
975 _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
976 return PAM_SERVICE_ERR;
979 _pam_log_debug(ctrl, LOG_DEBUG, "homedir is: %s", pwd->pw_dir);
981 if (directory_exist(pwd->pw_dir, &sbuf)) {
985 fstrcpy(create_dir, "/");
986 while (next_token((const char **)&pwd->pw_dir, tok, "/", sizeof(tok))) {
990 fstrcat(create_dir, tok);
991 fstrcat(create_dir, "/");
993 if (!directory_exist(create_dir, &sbuf)) {
994 if (mkdir(create_dir, mode) != 0) {
995 _pam_log(LOG_ERR, "could not create dir: %s (%s)",
996 create_dir, strerror(errno));
997 return PAM_SERVICE_ERR;
1002 if (sys_chown(create_dir, pwd->pw_uid, pwd->pw_gid) != 0) {
1003 _pam_log(LOG_ERR, "failed to chown user homedir: %s (%s)",
1004 create_dir, strerror(errno));
1005 return PAM_SERVICE_ERR;
1013 int pam_sm_close_session(pam_handle_t *pamh, int flags,
1014 int argc, const char **argv)
1016 /* parse arguments */
1017 int ctrl = _pam_parse(argc, argv);
1019 return PAM_SYSTEM_ERR;
1022 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_close_session handler");
1024 if (!(flags & PAM_DELETE_CRED)) {
1028 if (ctrl & WINBIND_KRB5_AUTH) {
1030 /* destroy the ccache here */
1031 struct winbindd_request request;
1032 struct winbindd_response response;
1034 const char *ccname = NULL;
1035 struct passwd *pwd = NULL;
1039 ZERO_STRUCT(request);
1040 ZERO_STRUCT(response);
1042 retval = pam_get_user(pamh, &user, "Username: ");
1043 if (retval == PAM_SUCCESS) {
1045 _pam_log(LOG_ERR, "username was NULL!");
1046 return PAM_USER_UNKNOWN;
1048 if (retval == PAM_SUCCESS) {
1049 _pam_log_debug(ctrl, LOG_DEBUG, "username [%s] obtained", user);
1052 _pam_log_debug(ctrl, LOG_DEBUG, "could not identify user");
1056 ccname = pam_getenv(pamh, "KRB5CCNAME");
1057 if (ccname == NULL) {
1058 _pam_log_debug(ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1062 fstrcpy(request.data.logoff.user, user);
1063 fstrcpy(request.data.logoff.krb5ccname, ccname);
1065 pwd = getpwnam(user);
1067 return PAM_USER_UNKNOWN;
1069 request.data.logoff.uid = pwd->pw_uid;
1071 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1073 return pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1082 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1083 int argc, const char **argv)
1089 /* <DO NOT free() THESE> */
1091 char *pass_old, *pass_new;
1092 /* </DO NOT free() THESE> */
1098 ctrl = _pam_parse(argc, argv);
1100 return PAM_SYSTEM_ERR;
1103 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_chauthtok");
1106 * First get the name of a user
1108 retval = pam_get_user(pamh, &user, "Username: ");
1109 if (retval == PAM_SUCCESS) {
1111 _pam_log(LOG_ERR, "username was NULL!");
1112 return PAM_USER_UNKNOWN;
1114 if (retval == PAM_SUCCESS) {
1115 _pam_log_debug(ctrl, LOG_DEBUG, "username [%s] obtained",
1119 _pam_log_debug(ctrl, LOG_DEBUG,
1120 "password - could not identify user");
1125 * obtain and verify the current password (OLDAUTHTOK) for
1129 if (flags & PAM_PRELIM_CHECK) {
1131 /* instruct user what is happening */
1132 #define greeting "Changing password for "
1133 fstrcpy(Announce, greeting);
1134 fstrcat(Announce, user);
1137 lctrl = ctrl | WINBIND__OLD_PASSWORD;
1138 retval = _winbind_read_password(pamh, lctrl,
1140 "(current) NT password: ",
1142 (const char **) &pass_old);
1143 if (retval != PAM_SUCCESS) {
1144 _pam_log(LOG_NOTICE, "password - (old) token not obtained");
1147 /* verify that this is the password for this user */
1149 retval = winbind_auth_request(pamh, ctrl, user, pass_old, NULL, NULL, False);
1151 if (retval != PAM_ACCT_EXPIRED
1152 && retval != PAM_AUTHTOK_EXPIRED
1153 && retval != PAM_NEW_AUTHTOK_REQD
1154 && retval != PAM_SUCCESS) {
1159 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
1161 if (retval != PAM_SUCCESS) {
1162 _pam_log(LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
1164 } else if (flags & PAM_UPDATE_AUTHTOK) {
1167 * obtain the proposed password
1171 * get the old token back.
1174 retval = pam_get_item(pamh, PAM_OLDAUTHTOK,
1175 (const void **) &pass_old);
1177 if (retval != PAM_SUCCESS) {
1178 _pam_log(LOG_NOTICE, "user not authenticated");
1184 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
1185 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
1188 retval = PAM_AUTHTOK_ERR;
1189 while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
1191 * use_authtok is to force the use of a previously entered
1192 * password -- needed for pluggable password strength checking
1195 retval = _winbind_read_password(pamh, lctrl,
1197 "Enter new NT password: ",
1198 "Retype new NT password: ",
1199 (const char **) &pass_new);
1201 if (retval != PAM_SUCCESS) {
1202 _pam_log_debug(ctrl, LOG_ALERT
1203 ,"password - new password not obtained");
1204 pass_old = NULL;/* tidy up */
1209 * At this point we know who the user is and what they
1210 * propose as their new password. Verify that the new
1211 * password is acceptable.
1214 if (pass_new[0] == '\0') {/* "\0" password = NULL */
1220 * By reaching here we have approved the passwords and must now
1221 * rebuild the password database file.
1224 retval = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new);
1226 _pam_overwrite(pass_new);
1227 _pam_overwrite(pass_old);
1228 pass_old = pass_new = NULL;
1232 /* just in case we need krb5 creds after a password change over msrpc */
1234 if (ctrl & WINBIND_KRB5_AUTH) {
1236 const char *member = get_member_from_config(argc, argv, ctrl);
1237 const char *cctype = get_krb5_cc_type_from_config(argc, argv, ctrl);
1239 retval = winbind_auth_request(pamh, ctrl, user, pass_new, member, cctype, False);
1240 _pam_overwrite(pass_new);
1241 _pam_overwrite(pass_old);
1242 pass_old = pass_new = NULL;
1245 retval = PAM_SERVICE_ERR;
1253 /* static module data */
1255 struct pam_module _pam_winbind_modstruct = {
1257 pam_sm_authenticate,
1260 pam_sm_open_session,
1261 pam_sm_close_session,
1268 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
1269 * Copyright (c) Tim Potter <tpot@samba.org> 2000
1270 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1271 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2006
1272 * Copyright (c) Jan Rêkorajski 1999.
1273 * Copyright (c) Andrew G. Morgan 1996-8.
1274 * Copyright (c) Alex O. Yuriev, 1996.
1275 * Copyright (c) Cristian Gafton 1996.
1276 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
1278 * Redistribution and use in source and binary forms, with or without
1279 * modification, are permitted provided that the following conditions
1281 * 1. Redistributions of source code must retain the above copyright
1282 * notice, and the entire permission notice in its entirety,
1283 * including the disclaimer of warranties.
1284 * 2. Redistributions in binary form must reproduce the above copyright
1285 * notice, this list of conditions and the following disclaimer in the
1286 * documentation and/or other materials provided with the distribution.
1287 * 3. The name of the author may not be used to endorse or promote
1288 * products derived from this software without specific prior
1289 * written permission.
1291 * ALTERNATIVELY, this product may be distributed under the terms of
1292 * the GNU Public License, in which case the provisions of the GPL are
1293 * required INSTEAD OF the above restrictions. (This clause is
1294 * necessary due to a potential bad interaction between the GPL and
1295 * the restrictions contained in a BSD-style copyright.)
1297 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
1298 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1299 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1300 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1301 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1302 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1303 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1304 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1305 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1306 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1307 * OF THE POSSIBILITY OF SUCH DAMAGE.