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)
134 * Work around the pam API that has functions with void ** as parameters.
135 * These lead to strict aliasing warnings with gcc.
137 static int _pam_get_item(const pam_handle_t *pamh, int item_type,
140 const void **item = (const void **)_item;
141 return pam_get_item(pamh, item_type, item);
143 static int _pam_get_data(const pam_handle_t *pamh,
144 const char *module_data_name, const void *_data)
146 const void **data = (const void **)_data;
147 return pam_get_data(pamh, module_data_name, data);
151 static const struct ntstatus_errors {
152 const char *ntstatus_string;
153 const char *error_string;
154 } ntstatus_errors[] = {
155 {"NT_STATUS_OK", "Success"},
156 {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
157 {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
158 {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
159 {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
160 {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
161 {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
162 {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
163 {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
164 {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
165 {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
166 {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
167 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
168 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
169 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
170 {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
174 const char *_get_ntstatus_error_string(const char *nt_status_string)
177 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
178 if (!strcasecmp(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
179 return ntstatus_errors[i].error_string;
185 /* --- authentication management functions --- */
187 /* Attempt a conversation */
189 static int converse(pam_handle_t *pamh, int nargs,
190 struct pam_message **message,
191 struct pam_response **response)
194 struct pam_conv *conv;
196 retval = _pam_get_item(pamh, PAM_CONV, &conv );
197 if (retval == PAM_SUCCESS) {
198 retval = conv->conv(nargs, (const struct pam_message **)message,
199 response, conv->appdata_ptr);
202 return retval; /* propagate error status */
206 static int _make_remark(pam_handle_t * pamh, int type, const char *text)
208 int retval = PAM_SUCCESS;
210 struct pam_message *pmsg[1], msg[1];
211 struct pam_response *resp;
214 msg[0].msg = CONST_DISCARD(char *, text);
215 msg[0].msg_style = type;
218 retval = converse(pamh, 1, pmsg, &resp);
221 _pam_drop_reply(resp, 1);
226 static int _make_remark_format(pam_handle_t * pamh, int type, const char *format, ...)
232 va_start(args, format);
233 vasprintf(&var, format, args);
236 ret = _make_remark(pamh, type, var);
241 static int pam_winbind_request(pam_handle_t * pamh, int ctrl,
242 enum winbindd_cmd req_type,
243 struct winbindd_request *request,
244 struct winbindd_response *response)
246 /* Fill in request and send down pipe */
247 init_request(request, req_type);
249 if (write_sock(request, sizeof(*request), 0) == -1) {
250 _pam_log(LOG_ERR, "write to socket failed!");
252 return PAM_SERVICE_ERR;
256 if (read_reply(response) == -1) {
257 _pam_log(LOG_ERR, "read from socket failed!");
259 return PAM_SERVICE_ERR;
262 /* We are done with the socket - close it and avoid mischeif */
265 /* Copy reply data from socket */
266 if (response->result != WINBINDD_OK) {
267 if (response->data.auth.pam_error != PAM_SUCCESS) {
268 _pam_log(LOG_ERR, "request failed: %s, PAM error was %s (%d), NT error was %s",
269 response->data.auth.error_string,
270 pam_strerror(pamh, response->data.auth.pam_error),
271 response->data.auth.pam_error,
272 response->data.auth.nt_status_string);
273 return response->data.auth.pam_error;
275 _pam_log(LOG_ERR, "request failed, but PAM error 0!");
276 return PAM_SERVICE_ERR;
283 static int pam_winbind_request_log(pam_handle_t * pamh,
285 enum winbindd_cmd req_type,
286 struct winbindd_request *request,
287 struct winbindd_response *response,
292 retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
296 /* incorrect password */
297 _pam_log(LOG_WARNING, "user `%s' denied access (incorrect password or invalid membership)", user);
299 case PAM_ACCT_EXPIRED:
300 /* account expired */
301 _pam_log(LOG_WARNING, "user `%s' account expired", user);
303 case PAM_AUTHTOK_EXPIRED:
304 /* password expired */
305 _pam_log(LOG_WARNING, "user `%s' password expired", user);
307 case PAM_NEW_AUTHTOK_REQD:
308 /* new password required */
309 _pam_log(LOG_WARNING, "user `%s' new password required", user);
311 case PAM_USER_UNKNOWN:
312 /* the user does not exist */
313 _pam_log_debug(ctrl, LOG_NOTICE, "user `%s' not found", user);
314 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
319 if (req_type == WINBINDD_PAM_AUTH) {
320 /* Otherwise, the authentication looked good */
321 _pam_log(LOG_NOTICE, "user '%s' granted access", user);
322 } else if (req_type == WINBINDD_PAM_CHAUTHTOK) {
323 /* Otherwise, the authentication looked good */
324 _pam_log(LOG_NOTICE, "user '%s' password changed", user);
326 /* Otherwise, the authentication looked good */
327 _pam_log(LOG_NOTICE, "user '%s' OK", user);
332 /* we don't know anything about this return value */
333 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s')",
339 /* talk to winbindd */
340 static int winbind_auth_request(pam_handle_t * pamh,
347 time_t *pwd_last_set)
349 struct winbindd_request request;
350 struct winbindd_response response;
353 ZERO_STRUCT(request);
354 ZERO_STRUCT(response);
360 strncpy(request.data.auth.user, user,
361 sizeof(request.data.auth.user)-1);
363 strncpy(request.data.auth.pass, pass,
364 sizeof(request.data.auth.pass)-1);
366 request.data.auth.krb5_cc_type[0] = '\0';
367 request.data.auth.uid = -1;
369 request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_GET_PWD_POLICY;
371 if (ctrl & WINBIND_KRB5_AUTH) {
373 struct passwd *pwd = NULL;
375 _pam_log_debug(ctrl, LOG_DEBUG, "enabling krb5 login flag\n");
377 request.flags |= WBFLAG_PAM_KRB5 | WBFLAG_PAM_FALLBACK_AFTER_KRB5;
379 pwd = getpwnam(user);
381 return PAM_USER_UNKNOWN;
383 request.data.auth.uid = pwd->pw_uid;
386 if (ctrl & WINBIND_CACHED_LOGIN) {
387 _pam_log_debug(ctrl, LOG_DEBUG, "enabling cached login flag\n");
388 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
391 if (cctype != NULL) {
392 strncpy(request.data.auth.krb5_cc_type, cctype,
393 sizeof(request.data.auth.krb5_cc_type) - 1);
394 _pam_log_debug(ctrl, LOG_DEBUG, "enabling request for a %s krb5 ccache\n", cctype);
397 request.data.auth.require_membership_of_sid[0] = '\0';
399 if (member != NULL) {
400 strncpy(request.data.auth.require_membership_of_sid, member,
401 sizeof(request.data.auth.require_membership_of_sid)-1);
405 if ( (member != NULL) && (strncmp("S-", member, 2) != 0) ) {
407 struct winbindd_request sid_request;
408 struct winbindd_response sid_response;
410 ZERO_STRUCT(sid_request);
411 ZERO_STRUCT(sid_response);
413 _pam_log_debug(ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", member);
415 /* fortunatly winbindd can handle non-separated names */
416 strncpy(sid_request.data.name.name, member,
417 sizeof(sid_request.data.name.name) - 1);
419 if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME, &sid_request, &sid_response, user)) {
420 _pam_log(LOG_INFO, "could not lookup name: %s\n", member);
424 member = sid_response.data.sid.sid;
426 strncpy(request.data.auth.require_membership_of_sid, member,
427 sizeof(request.data.auth.require_membership_of_sid)-1);
430 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
433 *pwd_last_set = response.data.auth.info3.pass_last_set_time;
436 if ((ctrl & WINBIND_KRB5_AUTH) &&
437 response.data.auth.krb5ccname[0] != '\0') {
441 _pam_log_debug(ctrl, LOG_DEBUG, "request returned KRB5CCNAME: %s",
442 response.data.auth.krb5ccname);
444 snprintf(var, sizeof(var), "KRB5CCNAME=%s", response.data.auth.krb5ccname);
446 ret = pam_putenv(pamh, var);
447 if (ret != PAM_SUCCESS) {
448 _pam_log(LOG_ERR, "failed to set KRB5CCNAME to %s", var);
453 if (!process_result) {
458 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_EXPIRED");
459 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_MUST_CHANGE");
460 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_WORKSTATION");
461 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_LOGON_HOURS");
462 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_EXPIRED");
463 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_DISABLED");
464 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_LOCKED_OUT");
465 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
466 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
467 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
470 /* handle the case where the auth was ok, but the password must expire right now */
471 /* good catch from Ralf Haferkamp: an expiry of "never" is translated to -1 */
472 if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
473 (response.data.auth.policy.expire > 0) &&
474 (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire < time(NULL))) {
476 ret = PAM_AUTHTOK_EXPIRED;
478 _pam_log_debug(ctrl, LOG_DEBUG,"Password has expired (Password was last set: %d, "
479 "the policy says it should expire here %d (now it's: %d)\n",
480 response.data.auth.info3.pass_last_set_time,
481 response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire,
484 PAM_WB_REMARK_DIRECT_RET(pamh, "NT_STATUS_PASSWORD_EXPIRED");
488 /* warn a user if the password is about to expire soon */
489 if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
490 (response.data.auth.policy.expire) &&
491 (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire > time(NULL) ) ) {
493 int days = response.data.auth.policy.expire / SECONDS_PER_DAY;
494 if (days <= DAYS_TO_WARN_BEFORE_PWD_EXPIRES) {
495 _make_remark_format(pamh, PAM_TEXT_INFO, "Your password will expire in %d days", days);
499 if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
500 _make_remark(pamh, PAM_ERROR_MSG, "Logging on using cached account. Network ressources can be unavailable");
501 _pam_log_debug(ctrl, LOG_DEBUG,"User %s logged on using cached account\n", user);
504 /* save the CIFS homedir for pam_cifs / pam_mount */
505 if (response.data.auth.info3.home_dir[0] != '\0') {
507 int ret2 = pam_set_data(pamh, PAM_WINBIND_HOMEDIR,
508 (void *) strdup(response.data.auth.info3.home_dir),
509 _pam_winbind_cleanup_func);
511 _pam_log_debug(ctrl, LOG_DEBUG, "Could not set data: %s",
512 pam_strerror(pamh, ret2));
517 /* save the logon script path for other PAM modules */
518 if (response.data.auth.info3.logon_script[0] != '\0') {
520 int ret2 = pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT,
521 (void *) strdup(response.data.auth.info3.logon_script),
522 _pam_winbind_cleanup_func);
524 _pam_log_debug(ctrl, LOG_DEBUG, "Could not set data: %s",
525 pam_strerror(pamh, ret2));
532 /* talk to winbindd */
533 static int winbind_chauthtok_request(pam_handle_t * pamh,
540 struct winbindd_request request;
541 struct winbindd_response response;
544 ZERO_STRUCT(request);
545 ZERO_STRUCT(response);
547 if (request.data.chauthtok.user == NULL) return -2;
549 strncpy(request.data.chauthtok.user, user,
550 sizeof(request.data.chauthtok.user) - 1);
552 if (oldpass != NULL) {
553 strncpy(request.data.chauthtok.oldpass, oldpass,
554 sizeof(request.data.chauthtok.oldpass) - 1);
556 request.data.chauthtok.oldpass[0] = '\0';
559 if (newpass != NULL) {
560 strncpy(request.data.chauthtok.newpass, newpass,
561 sizeof(request.data.chauthtok.newpass) - 1);
563 request.data.chauthtok.newpass[0] = '\0';
566 if (ctrl & WINBIND_KRB5_AUTH) {
567 request.flags = WBFLAG_PAM_KRB5;
570 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
572 if (ret == PAM_SUCCESS) {
576 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_BACKUP_CONTROLLER");
577 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCESS_DENIED");
579 /* TODO: tell the min pwd length ? */
580 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_SHORT");
582 /* TODO: tell the minage ? */
583 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_RECENT");
585 /* TODO: tell the history length ? */
586 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
588 if (!strcasecmp(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
590 /* FIXME: avoid to send multiple PAM messages after another */
591 switch (response.data.auth.reject_reason) {
594 case REJECT_REASON_OTHER:
595 if ((response.data.auth.policy.min_passwordage > 0) &&
596 (pwd_last_set + response.data.auth.policy.min_passwordage > time(NULL))) {
597 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_RECENT");
600 case REJECT_REASON_TOO_SHORT:
601 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_SHORT");
603 case REJECT_REASON_IN_HISTORY:
604 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_HISTORY_CONFLICT");
606 case REJECT_REASON_NOT_COMPLEX:
607 _make_remark(pamh, PAM_ERROR_MSG, "Password does not meet complexity requirements");
610 _pam_log_debug(ctrl, LOG_DEBUG,
611 "unknown password change reject reason: %d",
612 response.data.auth.reject_reason);
616 _make_remark_format(pamh, PAM_ERROR_MSG,
617 "Your password must be at least %d characters; "
618 "cannot repeat any of the your previous %d passwords"
620 "Please type a different password. "
621 "Type a password which meets these requirements in both text boxes.",
622 response.data.auth.policy.min_length_password,
623 response.data.auth.policy.password_history,
624 (response.data.auth.policy.password_properties & DOMAIN_PASSWORD_COMPLEX) ?
625 "; must contain capitals, numerals or punctuation; and cannot contain your account or full name" :
634 * Checks if a user has an account
641 static int valid_user(const char *user, pam_handle_t *pamh, int ctrl)
643 /* check not only if the user is available over NSS calls, also make
644 * sure it's really a winbind user, this is important when stacking PAM
645 * modules in the 'account' or 'password' facility. */
647 struct passwd *pwd = NULL;
648 struct winbindd_request request;
649 struct winbindd_response response;
652 ZERO_STRUCT(request);
653 ZERO_STRUCT(response);
655 pwd = getpwnam(user);
660 strncpy(request.data.username, user,
661 sizeof(request.data.username) - 1);
663 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_GETPWNAM, &request, &response, user);
666 case PAM_USER_UNKNOWN:
676 static char *_pam_delete(register char *xx)
684 * obtain a password from the user
687 static int _winbind_read_password(pam_handle_t * pamh,
700 * make sure nothing inappropriate gets returned
703 *pass = token = NULL;
706 * which authentication token are we getting?
709 authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
712 * should we obtain the password from a PAM item ?
715 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
716 retval = _pam_get_item(pamh, authtok_flag, &item);
717 if (retval != PAM_SUCCESS) {
720 "pam_get_item returned error to unix-read-password"
723 } else if (item != NULL) { /* we have a password! */
727 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
728 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
729 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
730 && off(WINBIND__OLD_PASSWORD, ctrl)) {
731 return PAM_AUTHTOK_RECOVER_ERR;
735 * getting here implies we will have to get the password from the
740 struct pam_message msg[3], *pmsg[3];
741 struct pam_response *resp;
744 /* prepare to converse */
746 if (comment != NULL) {
748 msg[0].msg_style = PAM_TEXT_INFO;
749 msg[0].msg = CONST_DISCARD(char *, comment);
756 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
757 msg[i++].msg = CONST_DISCARD(char *, prompt1);
760 if (prompt2 != NULL) {
762 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
763 msg[i++].msg = CONST_DISCARD(char *, prompt2);
766 /* so call the conversation expecting i responses */
768 retval = converse(pamh, i, pmsg, &resp);
772 /* interpret the response */
774 if (retval == PAM_SUCCESS) { /* a good conversation */
776 token = x_strdup(resp[i - replies].resp);
779 /* verify that password entered correctly */
780 if (!resp[i - 1].resp
781 || strcmp(token, resp[i - 1].resp)) {
782 _pam_delete(token); /* mistyped */
783 retval = PAM_AUTHTOK_RECOVER_ERR;
784 _make_remark(pamh, PAM_ERROR_MSG, MISTYPED_PASS);
788 _pam_log(LOG_NOTICE, "could not recover authentication token");
789 retval = PAM_AUTHTOK_RECOVER_ERR;
794 * tidy up the conversation (resp_retcode) is ignored
795 * -- what is it for anyway? AGM
798 _pam_drop_reply(resp, i);
801 retval = (retval == PAM_SUCCESS)
802 ? PAM_AUTHTOK_RECOVER_ERR : retval;
806 if (retval != PAM_SUCCESS) {
807 _pam_log_debug(ctrl, LOG_DEBUG,
808 "unable to obtain a password");
811 /* 'token' is the entered password */
813 /* we store this password as an item */
815 retval = pam_set_item(pamh, authtok_flag, token);
816 _pam_delete(token); /* clean it up */
817 if (retval != PAM_SUCCESS ||
818 (retval = _pam_get_item(pamh, authtok_flag, &item)) != PAM_SUCCESS) {
820 _pam_log(LOG_CRIT, "error manipulating password");
826 item = NULL; /* break link to password */
831 const char *get_conf_item_string(int argc,
840 const char *parm_opt = NULL;
843 if (!(ctrl & flag)) {
847 /* let the pam opt take precedence over the pam_winbind.conf option */
851 if (!asprintf(&key, "global:%s", item)) {
855 parm_opt = iniparser_getstr(d, key);
859 for ( i=0; i<argc; i++ ) {
861 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
864 parm = strdup(argv[i]);
866 if ( (p = strchr( parm, '=' )) == NULL) {
867 _pam_log(LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
871 _pam_log_debug(ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
877 _pam_log_debug(ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
884 const char *get_krb5_cc_type_from_config(int argc, const char **argv, int ctrl, dictionary *d)
886 return get_conf_item_string(argc, argv, ctrl, d, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
889 const char *get_member_from_config(int argc, const char **argv, int ctrl, dictionary *d)
891 const char *ret = NULL;
892 ret = get_conf_item_string(argc, argv, ctrl, d, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
896 return get_conf_item_string(argc, argv, ctrl, d, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
900 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
901 int argc, const char **argv)
903 const char *username;
904 const char *password;
905 const char *member = NULL;
906 const char *cctype = NULL;
907 int retval = PAM_AUTH_ERR;
910 /* parse arguments */
911 int ctrl = _pam_parse(argc, argv, &d);
913 retval = PAM_SYSTEM_ERR;
917 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_authenticate (flags: 0x%04x)", flags);
919 /* Get the username */
920 retval = pam_get_user(pamh, &username, NULL);
921 if ((retval != PAM_SUCCESS) || (!username)) {
922 _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
923 retval = PAM_SERVICE_ERR;
927 retval = _winbind_read_password(pamh, ctrl, NULL,
931 if (retval != PAM_SUCCESS) {
932 _pam_log(LOG_ERR, "Could not retrieve user's password");
933 retval = PAM_AUTHTOK_ERR;
937 /* Let's not give too much away in the log file */
939 #ifdef DEBUG_PASSWORD
940 _pam_log_debug(ctrl, LOG_INFO, "Verify user `%s' with password `%s'",
943 _pam_log_debug(ctrl, LOG_INFO, "Verify user `%s'", username);
946 member = get_member_from_config(argc, argv, ctrl, d);
948 cctype = get_krb5_cc_type_from_config(argc, argv, ctrl, d);
950 /* Now use the username to look up password */
951 retval = winbind_auth_request(pamh, ctrl, username, password, member, cctype, True, NULL);
953 if (retval == PAM_NEW_AUTHTOK_REQD ||
954 retval == PAM_AUTHTOK_EXPIRED) {
958 if (!asprintf(&buf, "%d", retval)) {
959 retval = PAM_BUF_ERR;
963 pam_set_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (void *)buf, _pam_winbind_cleanup_func);
965 retval = PAM_SUCCESS;
971 iniparser_freedict(d);
977 int pam_sm_setcred(pam_handle_t *pamh, int flags,
978 int argc, const char **argv)
980 /* parse arguments */
981 int ctrl = _pam_parse(argc, argv, NULL);
983 return PAM_SYSTEM_ERR;
986 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_setcred (flags: 0x%04x)", flags);
988 if (flags & PAM_DELETE_CRED) {
989 return pam_sm_close_session(pamh, flags, argc, argv);
996 * Account management. We want to verify that the account exists
997 * before returning PAM_SUCCESS
1000 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
1001 int argc, const char **argv)
1003 const char *username;
1004 int retval = PAM_USER_UNKNOWN;
1007 /* parse arguments */
1008 int ctrl = _pam_parse(argc, argv, NULL);
1010 return PAM_SYSTEM_ERR;
1013 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_acct_mgmt (flags: 0x%04x)", flags);
1016 /* Get the username */
1017 retval = pam_get_user(pamh, &username, NULL);
1018 if ((retval != PAM_SUCCESS) || (!username)) {
1019 _pam_log_debug(ctrl, LOG_DEBUG,"can not get the username");
1020 return PAM_SERVICE_ERR;
1023 /* Verify the username */
1024 retval = valid_user(username, pamh, ctrl);
1027 /* some sort of system error. The log was already printed */
1028 return PAM_SERVICE_ERR;
1030 /* the user does not exist */
1031 _pam_log_debug(ctrl, LOG_NOTICE, "user `%s' not found", username);
1032 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
1035 return PAM_USER_UNKNOWN;
1037 pam_get_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (const void **)&tmp);
1041 case PAM_AUTHTOK_EXPIRED:
1042 /* fall through, since new token is required in this case */
1043 case PAM_NEW_AUTHTOK_REQD:
1044 _pam_log(LOG_WARNING, "pam_sm_acct_mgmt success but %s is set",
1045 PAM_WINBIND_NEW_AUTHTOK_REQD);
1046 _pam_log(LOG_NOTICE, "user '%s' needs new password", username);
1047 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
1048 return PAM_NEW_AUTHTOK_REQD;
1050 _pam_log(LOG_WARNING, "pam_sm_acct_mgmt success");
1051 _pam_log(LOG_NOTICE, "user '%s' granted access", username);
1056 /* Otherwise, the authentication looked good */
1057 _pam_log(LOG_NOTICE, "user '%s' granted access", username);
1060 /* we don't know anything about this return value */
1061 _pam_log(LOG_ERR, "internal module error (retval = %d, user = `%s')",
1063 return PAM_SERVICE_ERR;
1066 /* should not be reached */
1071 int pam_sm_open_session(pam_handle_t *pamh, int flags,
1072 int argc, const char **argv)
1074 /* parse arguments */
1075 int ctrl = _pam_parse(argc, argv, NULL);
1077 return PAM_SYSTEM_ERR;
1080 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_open_session handler (flags: 0x%04x)", flags);
1086 int pam_sm_close_session(pam_handle_t *pamh, int flags,
1087 int argc, const char **argv)
1090 int retval = PAM_SUCCESS;
1092 /* parse arguments */
1093 int ctrl = _pam_parse(argc, argv, &d);
1095 retval = PAM_SYSTEM_ERR;
1099 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_close_session handler (flags: 0x%04x)", flags);
1101 if (!(flags & PAM_DELETE_CRED)) {
1102 retval = PAM_SUCCESS;
1106 if (ctrl & WINBIND_KRB5_AUTH) {
1108 /* destroy the ccache here */
1109 struct winbindd_request request;
1110 struct winbindd_response response;
1112 const char *ccname = NULL;
1113 struct passwd *pwd = NULL;
1115 ZERO_STRUCT(request);
1116 ZERO_STRUCT(response);
1118 retval = pam_get_user(pamh, &user, "Username: ");
1119 if (retval == PAM_SUCCESS) {
1121 _pam_log(LOG_ERR, "username was NULL!");
1122 retval = PAM_USER_UNKNOWN;
1125 if (retval == PAM_SUCCESS) {
1126 _pam_log_debug(ctrl, LOG_DEBUG, "username [%s] obtained", user);
1129 _pam_log_debug(ctrl, LOG_DEBUG, "could not identify user");
1133 ccname = pam_getenv(pamh, "KRB5CCNAME");
1134 if (ccname == NULL) {
1135 _pam_log_debug(ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1136 retval = PAM_SUCCESS;
1140 strncpy(request.data.logoff.user, user,
1141 sizeof(request.data.logoff.user) - 1);
1143 strncpy(request.data.logoff.krb5ccname, ccname,
1144 sizeof(request.data.logoff.krb5ccname) - 1);
1146 pwd = getpwnam(user);
1148 retval = PAM_USER_UNKNOWN;
1151 request.data.logoff.uid = pwd->pw_uid;
1153 request.flags = WBFLAG_PAM_KRB5;
1155 retval = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1160 iniparser_freedict(d);
1168 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1169 int argc, const char **argv)
1175 /* <DO NOT free() THESE> */
1177 char *pass_old, *pass_new;
1178 /* </DO NOT free() THESE> */
1185 ctrl = _pam_parse(argc, argv, &d);
1187 retval = PAM_SYSTEM_ERR;
1191 _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_chauthtok (flags: 0x%04x)", flags);
1193 /* clearing offline bit for the auth in the password change */
1194 ctrl &= ~WINBIND_CACHED_LOGIN;
1197 * First get the name of a user
1199 retval = pam_get_user(pamh, &user, "Username: ");
1200 if (retval == PAM_SUCCESS) {
1202 _pam_log(LOG_ERR, "username was NULL!");
1203 retval = PAM_USER_UNKNOWN;
1206 if (retval == PAM_SUCCESS) {
1207 _pam_log_debug(ctrl, LOG_DEBUG, "username [%s] obtained",
1211 _pam_log_debug(ctrl, LOG_DEBUG,
1212 "password - could not identify user");
1216 /* check if this is really a user in winbindd, not only in NSS */
1217 retval = valid_user(user, pamh, ctrl);
1220 retval = PAM_USER_UNKNOWN;
1223 retval = PAM_SYSTEM_ERR;
1230 * obtain and verify the current password (OLDAUTHTOK) for
1234 if (flags & PAM_PRELIM_CHECK) {
1236 time_t pwdlastset_prelim = 0;
1238 /* instruct user what is happening */
1239 #define greeting "Changing password for "
1240 Announce = (char *) malloc(sizeof(greeting) + strlen(user));
1241 if (Announce == NULL) {
1242 _pam_log(LOG_CRIT, "password - out of memory");
1243 retval = PAM_BUF_ERR;
1246 (void) strcpy(Announce, greeting);
1247 (void) strcpy(Announce + sizeof(greeting) - 1, user);
1250 lctrl = ctrl | WINBIND__OLD_PASSWORD;
1251 retval = _winbind_read_password(pamh, lctrl,
1253 "(current) NT password: ",
1255 (const char **) &pass_old);
1256 if (retval != PAM_SUCCESS) {
1257 _pam_log(LOG_NOTICE, "password - (old) token not obtained");
1260 /* verify that this is the password for this user */
1262 retval = winbind_auth_request(pamh, ctrl, user, pass_old, NULL, NULL, False, &pwdlastset_prelim);
1264 if (retval != PAM_ACCT_EXPIRED &&
1265 retval != PAM_AUTHTOK_EXPIRED &&
1266 retval != PAM_NEW_AUTHTOK_REQD &&
1267 retval != PAM_SUCCESS) {
1272 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET, (void *)pwdlastset_prelim, _pam_winbind_cleanup_func);
1274 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
1276 if (retval != PAM_SUCCESS) {
1277 _pam_log(LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
1279 } else if (flags & PAM_UPDATE_AUTHTOK) {
1281 time_t pwdlastset_update = 0;
1284 * obtain the proposed password
1288 * get the old token back.
1291 retval = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
1293 if (retval != PAM_SUCCESS) {
1294 _pam_log(LOG_NOTICE, "user not authenticated");
1300 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
1301 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
1304 retval = PAM_AUTHTOK_ERR;
1305 while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
1307 * use_authtok is to force the use of a previously entered
1308 * password -- needed for pluggable password strength checking
1311 retval = _winbind_read_password(pamh, lctrl,
1313 "Enter new NT password: ",
1314 "Retype new NT password: ",
1315 (const char **) &pass_new);
1317 if (retval != PAM_SUCCESS) {
1318 _pam_log_debug(ctrl, LOG_ALERT
1319 ,"password - new password not obtained");
1320 pass_old = NULL;/* tidy up */
1325 * At this point we know who the user is and what they
1326 * propose as their new password. Verify that the new
1327 * password is acceptable.
1330 if (pass_new[0] == '\0') {/* "\0" password = NULL */
1336 * By reaching here we have approved the passwords and must now
1337 * rebuild the password database file.
1339 _pam_get_data( pamh, PAM_WINBIND_PWD_LAST_SET,
1340 &pwdlastset_update);
1342 retval = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new, pwdlastset_update);
1344 _pam_overwrite(pass_new);
1345 _pam_overwrite(pass_old);
1346 pass_old = pass_new = NULL;
1350 /* just in case we need krb5 creds after a password change over msrpc */
1352 if (ctrl & WINBIND_KRB5_AUTH) {
1354 const char *member = get_member_from_config(argc, argv, ctrl, d);
1355 const char *cctype = get_krb5_cc_type_from_config(argc, argv, ctrl, d);
1357 retval = winbind_auth_request(pamh, ctrl, user, pass_new, member, cctype, False, NULL);
1358 _pam_overwrite(pass_new);
1359 _pam_overwrite(pass_old);
1360 pass_old = pass_new = NULL;
1363 retval = PAM_SERVICE_ERR;
1368 iniparser_freedict(d);
1375 /* static module data */
1377 struct pam_module _pam_winbind_modstruct = {
1379 pam_sm_authenticate,
1382 pam_sm_open_session,
1383 pam_sm_close_session,
1390 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
1391 * Copyright (c) Tim Potter <tpot@samba.org> 2000
1392 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1393 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2006
1394 * Copyright (c) Jan Rêkorajski 1999.
1395 * Copyright (c) Andrew G. Morgan 1996-8.
1396 * Copyright (c) Alex O. Yuriev, 1996.
1397 * Copyright (c) Cristian Gafton 1996.
1398 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
1400 * Redistribution and use in source and binary forms, with or without
1401 * modification, are permitted provided that the following conditions
1403 * 1. Redistributions of source code must retain the above copyright
1404 * notice, and the entire permission notice in its entirety,
1405 * including the disclaimer of warranties.
1406 * 2. Redistributions in binary form must reproduce the above copyright
1407 * notice, this list of conditions and the following disclaimer in the
1408 * documentation and/or other materials provided with the distribution.
1409 * 3. The name of the author may not be used to endorse or promote
1410 * products derived from this software without specific prior
1411 * written permission.
1413 * ALTERNATIVELY, this product may be distributed under the terms of
1414 * the GNU Public License, in which case the provisions of the GPL are
1415 * required INSTEAD OF the above restrictions. (This clause is
1416 * necessary due to a potential bad interaction between the GPL and
1417 * the restrictions contained in a BSD-style copyright.)
1419 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
1420 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1421 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1422 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1423 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1424 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1425 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1426 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1427 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1428 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1429 * OF THE POSSIBILITY OF SUCH DAMAGE.