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 * Work around the pam API that has functions with void ** as parameters.
21 * These lead to strict aliasing warnings with gcc.
23 static int _pam_get_item(const pam_handle_t *pamh, int item_type,
26 const void **item = (const void **)_item;
27 return pam_get_item(pamh, item_type, item);
29 static int _pam_get_data(const pam_handle_t *pamh,
30 const char *module_data_name, const void *_data)
32 const void **data = (const void **)_data;
33 return pam_get_data(pamh, module_data_name, data);
38 static void _pam_log_int(const pam_handle_t *pamh, int err, const char *format, va_list args)
41 #ifdef HAVE_PAM_VSYSLOG
42 pam_vsyslog(pamh, err, format, args);
49 _pam_get_item(pamh, PAM_SERVICE, &service);
51 format2 = malloc(strlen(MODULE_NAME)+strlen(format)+strlen(service)+5);
52 if (format2 == NULL) {
53 /* what else todo ? */
54 vsyslog(err, format, args);
58 sprintf(format2, "%s(%s): %s", MODULE_NAME, service, format);
59 vsyslog(err, format2, args);
65 static void _pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
69 if (ctrl & WINBIND_SILENT) {
73 va_start(args, format);
74 _pam_log_int(pamh, err, format, args);
78 static void _pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
82 if (ctrl & WINBIND_SILENT) {
86 if (!(ctrl & WINBIND_DEBUG_ARG)) {
90 va_start(args, format);
91 _pam_log_int(pamh, err, format, args);
95 static int _pam_parse(const pam_handle_t *pamh, int flags, int argc, const char **argv, dictionary **d)
98 const char *config_file = NULL;
102 if (flags & PAM_SILENT) {
103 ctrl |= WINBIND_SILENT;
107 goto config_from_pam;
110 for (i=argc,v=argv; i-- > 0; ++v) {
111 if (!strncasecmp(*v, "config", strlen("config"))) {
112 ctrl |= WINBIND_CONFIG_FILE;
118 if (config_file == NULL) {
119 config_file = PAM_WINBIND_CONFIG_FILE;
122 *d = iniparser_load(config_file);
124 goto config_from_pam;
127 if (iniparser_getboolean(*d, "global:debug", False)) {
128 ctrl |= WINBIND_DEBUG_ARG;
131 if (iniparser_getboolean(*d, "global:cached_login", False)) {
132 ctrl |= WINBIND_CACHED_LOGIN;
135 if (iniparser_getboolean(*d, "global:krb5_auth", False)) {
136 ctrl |= WINBIND_KRB5_AUTH;
139 if (iniparser_getboolean(*d, "global:silent", False)) {
140 ctrl |= WINBIND_SILENT;
143 if (iniparser_getstr(*d, "global:krb5_ccache_type") != NULL) {
144 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
147 if ((iniparser_getstr(*d, "global:require-membership-of") != NULL) ||
148 (iniparser_getstr(*d, "global:require_membership_of") != NULL)) {
149 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
153 /* step through arguments */
154 for (i=argc,v=argv; i-- > 0; ++v) {
156 /* generic options */
157 if (!strcmp(*v,"debug"))
158 ctrl |= WINBIND_DEBUG_ARG;
159 else if (!strcasecmp(*v, "use_authtok"))
160 ctrl |= WINBIND_USE_AUTHTOK_ARG;
161 else if (!strcasecmp(*v, "use_first_pass"))
162 ctrl |= WINBIND_USE_FIRST_PASS_ARG;
163 else if (!strcasecmp(*v, "try_first_pass"))
164 ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
165 else if (!strcasecmp(*v, "unknown_ok"))
166 ctrl |= WINBIND_UNKNOWN_OK_ARG;
167 else if (!strncasecmp(*v, "require_membership_of", strlen("require_membership_of")))
168 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
169 else if (!strncasecmp(*v, "require-membership-of", strlen("require-membership-of")))
170 ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
171 else if (!strcasecmp(*v, "krb5_auth"))
172 ctrl |= WINBIND_KRB5_AUTH;
173 else if (!strncasecmp(*v, "krb5_ccache_type", strlen("krb5_ccache_type")))
174 ctrl |= WINBIND_KRB5_CCACHE_TYPE;
175 else if (!strcasecmp(*v, "cached_login"))
176 ctrl |= WINBIND_CACHED_LOGIN;
178 _pam_log(pamh, ctrl, LOG_ERR, "pam_parse: unknown option; %s", *v);
185 static void _pam_winbind_cleanup_func(pam_handle_t *pamh, void *data, int error_status)
191 static const struct ntstatus_errors {
192 const char *ntstatus_string;
193 const char *error_string;
194 } ntstatus_errors[] = {
195 {"NT_STATUS_OK", "Success"},
196 {"NT_STATUS_BACKUP_CONTROLLER", "No primary Domain Controler available"},
197 {"NT_STATUS_PWD_TOO_SHORT", "Password too short"},
198 {"NT_STATUS_PWD_TOO_RECENT", "The password of this user is too recent to change"},
199 {"NT_STATUS_PWD_HISTORY_CONFLICT", "Password is already in password history"},
200 {"NT_STATUS_PASSWORD_EXPIRED", "Your password has expired"},
201 {"NT_STATUS_PASSWORD_MUST_CHANGE", "You need to change your password now"},
202 {"NT_STATUS_INVALID_WORKSTATION", "You are not allowed to logon from this workstation"},
203 {"NT_STATUS_INVALID_LOGON_HOURS", "You are not allowed to logon at this time"},
204 {"NT_STATUS_ACCOUNT_EXPIRED", "Your account has expired. Please contact your System administrator"}, /* SCNR */
205 {"NT_STATUS_ACCOUNT_DISABLED", "Your account is disabled. Please contact your System administrator"}, /* SCNR */
206 {"NT_STATUS_ACCOUNT_LOCKED_OUT", "Your account has been locked. Please contact your System administrator"}, /* SCNR */
207 {"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT", "Invalid Trust Account"},
208 {"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT", "Invalid Trust Account"},
209 {"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT", "Invalid Trust Account"},
210 {"NT_STATUS_ACCESS_DENIED", "Access is denied"},
214 const char *_get_ntstatus_error_string(const char *nt_status_string)
217 for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
218 if (!strcasecmp(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
219 return ntstatus_errors[i].error_string;
225 /* --- authentication management functions --- */
227 /* Attempt a conversation */
229 static int converse(pam_handle_t *pamh, int nargs,
230 struct pam_message **message,
231 struct pam_response **response)
234 struct pam_conv *conv;
236 retval = _pam_get_item(pamh, PAM_CONV, &conv );
237 if (retval == PAM_SUCCESS) {
238 retval = conv->conv(nargs, (const struct pam_message **)message,
239 response, conv->appdata_ptr);
242 return retval; /* propagate error status */
246 static int _make_remark(pam_handle_t * pamh, int type, const char *text)
248 int retval = PAM_SUCCESS;
250 struct pam_message *pmsg[1], msg[1];
251 struct pam_response *resp;
254 msg[0].msg = CONST_DISCARD(char *, text);
255 msg[0].msg_style = type;
258 retval = converse(pamh, 1, pmsg, &resp);
261 _pam_drop_reply(resp, 1);
266 static int _make_remark_format(pam_handle_t * pamh, int type, const char *format, ...)
272 va_start(args, format);
273 vasprintf(&var, format, args);
276 ret = _make_remark(pamh, type, var);
281 static int pam_winbind_request(pam_handle_t * pamh, int ctrl,
282 enum winbindd_cmd req_type,
283 struct winbindd_request *request,
284 struct winbindd_response *response)
286 /* Fill in request and send down pipe */
287 init_request(request, req_type);
289 if (write_sock(request, sizeof(*request), 0) == -1) {
290 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: write to socket failed!");
292 return PAM_SERVICE_ERR;
296 if (read_reply(response) == -1) {
297 _pam_log(pamh, ctrl, LOG_ERR, "pam_winbind_request: read from socket failed!");
299 return PAM_SERVICE_ERR;
302 /* We are done with the socket - close it and avoid mischeif */
305 /* Copy reply data from socket */
306 if (response->result != WINBINDD_OK) {
307 if (response->data.auth.pam_error != PAM_SUCCESS) {
308 _pam_log(pamh, ctrl, LOG_ERR, "request failed: %s, PAM error was %s (%d), NT error was %s",
309 response->data.auth.error_string,
310 pam_strerror(pamh, response->data.auth.pam_error),
311 response->data.auth.pam_error,
312 response->data.auth.nt_status_string);
313 return response->data.auth.pam_error;
315 _pam_log(pamh, ctrl, LOG_ERR, "request failed, but PAM error 0!");
316 return PAM_SERVICE_ERR;
323 static int pam_winbind_request_log(pam_handle_t * pamh,
325 enum winbindd_cmd req_type,
326 struct winbindd_request *request,
327 struct winbindd_response *response,
332 retval = pam_winbind_request(pamh, ctrl, req_type, request, response);
336 /* incorrect password */
337 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' denied access (incorrect password or invalid membership)", user);
339 case PAM_ACCT_EXPIRED:
340 /* account expired */
341 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' account expired", user);
343 case PAM_AUTHTOK_EXPIRED:
344 /* password expired */
345 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' password expired", user);
347 case PAM_NEW_AUTHTOK_REQD:
348 /* new password required */
349 _pam_log(pamh, ctrl, LOG_WARNING, "user '%s' new password required", user);
351 case PAM_USER_UNKNOWN:
352 /* the user does not exist */
353 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", user);
354 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
359 if (req_type == WINBINDD_PAM_AUTH) {
360 /* Otherwise, the authentication looked good */
361 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", user);
362 } else if (req_type == WINBINDD_PAM_CHAUTHTOK) {
363 /* Otherwise, the authentication looked good */
364 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' password changed", user);
366 /* Otherwise, the authentication looked good */
367 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' OK", user);
372 /* we don't know anything about this return value */
373 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (retval = %d, user = '%s')",
379 /* talk to winbindd */
380 static int winbind_auth_request(pam_handle_t * pamh,
387 time_t *pwd_last_set,
390 struct winbindd_request request;
391 struct winbindd_response response;
394 ZERO_STRUCT(request);
395 ZERO_STRUCT(response);
401 strncpy(request.data.auth.user, user,
402 sizeof(request.data.auth.user)-1);
404 strncpy(request.data.auth.pass, pass,
405 sizeof(request.data.auth.pass)-1);
407 request.data.auth.krb5_cc_type[0] = '\0';
408 request.data.auth.uid = -1;
410 request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_CONTACT_TRUSTDOM;
412 if (ctrl & WINBIND_KRB5_AUTH) {
414 struct passwd *pwd = NULL;
416 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling krb5 login flag\n");
418 request.flags |= WBFLAG_PAM_KRB5 | WBFLAG_PAM_FALLBACK_AFTER_KRB5;
420 pwd = getpwnam(user);
422 return PAM_USER_UNKNOWN;
424 request.data.auth.uid = pwd->pw_uid;
427 if (ctrl & WINBIND_CACHED_LOGIN) {
428 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling cached login flag\n");
429 request.flags |= WBFLAG_PAM_CACHED_LOGIN;
434 request.flags |= WBFLAG_PAM_UNIX_NAME;
437 if (cctype != NULL) {
438 strncpy(request.data.auth.krb5_cc_type, cctype,
439 sizeof(request.data.auth.krb5_cc_type) - 1);
440 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "enabling request for a %s krb5 ccache\n", cctype);
443 request.data.auth.require_membership_of_sid[0] = '\0';
445 if (member != NULL) {
446 strncpy(request.data.auth.require_membership_of_sid, member,
447 sizeof(request.data.auth.require_membership_of_sid)-1);
451 if ( (member != NULL) && (strncmp("S-", member, 2) != 0) ) {
453 struct winbindd_request sid_request;
454 struct winbindd_response sid_response;
456 ZERO_STRUCT(sid_request);
457 ZERO_STRUCT(sid_response);
459 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", member);
461 /* fortunatly winbindd can handle non-separated names */
462 strncpy(sid_request.data.name.name, member,
463 sizeof(sid_request.data.name.name) - 1);
465 if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME, &sid_request, &sid_response, user)) {
466 _pam_log(pamh, ctrl, LOG_INFO, "could not lookup name: %s\n", member);
470 member = sid_response.data.sid.sid;
472 strncpy(request.data.auth.require_membership_of_sid, member,
473 sizeof(request.data.auth.require_membership_of_sid)-1);
476 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
479 *pwd_last_set = response.data.auth.info3.pass_last_set_time;
482 if ((ctrl & WINBIND_KRB5_AUTH) &&
483 response.data.auth.krb5ccname[0] != '\0') {
487 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "request returned KRB5CCNAME: %s",
488 response.data.auth.krb5ccname);
490 snprintf(var, sizeof(var), "KRB5CCNAME=%s", response.data.auth.krb5ccname);
492 ret = pam_putenv(pamh, var);
493 if (ret != PAM_SUCCESS) {
494 _pam_log(pamh, ctrl, LOG_ERR, "failed to set KRB5CCNAME to %s", var);
499 if (!process_result) {
504 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_EXPIRED");
505 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PASSWORD_MUST_CHANGE");
506 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_WORKSTATION");
507 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_INVALID_LOGON_HOURS");
508 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_EXPIRED");
509 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_DISABLED");
510 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCOUNT_LOCKED_OUT");
511 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT");
512 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT");
513 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT");
516 /* handle the case where the auth was ok, but the password must expire right now */
517 /* good catch from Ralf Haferkamp: an expiry of "never" is translated to -1 */
518 if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
519 (response.data.auth.policy.expire > 0) &&
520 (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire < time(NULL))) {
522 ret = PAM_AUTHTOK_EXPIRED;
524 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"Password has expired (Password was last set: %d, "
525 "the policy says it should expire here %d (now it's: %d)\n",
526 response.data.auth.info3.pass_last_set_time,
527 response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire,
530 PAM_WB_REMARK_DIRECT_RET(pamh, "NT_STATUS_PASSWORD_EXPIRED");
534 /* warn a user if the password is about to expire soon */
535 if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
536 (response.data.auth.policy.expire) &&
537 (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire > time(NULL) ) ) {
539 int days = response.data.auth.policy.expire / SECONDS_PER_DAY;
540 if (days <= DAYS_TO_WARN_BEFORE_PWD_EXPIRES) {
541 _make_remark_format(pamh, PAM_TEXT_INFO, "Your password will expire in %d days", days);
545 if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
546 _make_remark(pamh, PAM_ERROR_MSG, "Logging on using cached account. Network ressources can be unavailable");
547 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"User %s logged on using cached account\n", user);
550 /* save the CIFS homedir for pam_cifs / pam_mount */
551 if (response.data.auth.info3.home_dir[0] != '\0') {
553 int ret2 = pam_set_data(pamh, PAM_WINBIND_HOMEDIR,
554 (void *) strdup(response.data.auth.info3.home_dir),
555 _pam_winbind_cleanup_func);
557 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s",
558 pam_strerror(pamh, ret2));
563 /* save the logon script path for other PAM modules */
564 if (response.data.auth.info3.logon_script[0] != '\0') {
566 int ret2 = pam_set_data(pamh, PAM_WINBIND_LOGONSCRIPT,
567 (void *) strdup(response.data.auth.info3.logon_script),
568 _pam_winbind_cleanup_func);
570 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s",
571 pam_strerror(pamh, ret2));
575 /* save the profile path for other PAM modules */
576 if (response.data.auth.info3.profile_path[0] != '\0') {
578 int ret2 = pam_set_data(pamh, PAM_WINBIND_PROFILEPATH,
579 (void *) strdup(response.data.auth.info3.profile_path),
580 _pam_winbind_cleanup_func);
582 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "Could not set data: %s",
583 pam_strerror(pamh, ret2));
587 /* If winbindd returned a username, return the pointer to it here. */
588 if (user_ret && response.extra_data.data) {
589 /* We have to trust it's a null terminated string. */
590 *user_ret = (char *)response.extra_data.data;
596 /* talk to winbindd */
597 static int winbind_chauthtok_request(pam_handle_t * pamh,
604 struct winbindd_request request;
605 struct winbindd_response response;
608 ZERO_STRUCT(request);
609 ZERO_STRUCT(response);
611 if (request.data.chauthtok.user == NULL) return -2;
613 strncpy(request.data.chauthtok.user, user,
614 sizeof(request.data.chauthtok.user) - 1);
616 if (oldpass != NULL) {
617 strncpy(request.data.chauthtok.oldpass, oldpass,
618 sizeof(request.data.chauthtok.oldpass) - 1);
620 request.data.chauthtok.oldpass[0] = '\0';
623 if (newpass != NULL) {
624 strncpy(request.data.chauthtok.newpass, newpass,
625 sizeof(request.data.chauthtok.newpass) - 1);
627 request.data.chauthtok.newpass[0] = '\0';
630 if (ctrl & WINBIND_KRB5_AUTH) {
631 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
634 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
636 if (ret == PAM_SUCCESS) {
640 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_BACKUP_CONTROLLER");
641 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_ACCESS_DENIED");
643 /* TODO: tell the min pwd length ? */
644 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_SHORT");
646 /* TODO: tell the minage ? */
647 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_TOO_RECENT");
649 /* TODO: tell the history length ? */
650 PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
652 if (!strcasecmp(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
654 /* FIXME: avoid to send multiple PAM messages after another */
655 switch (response.data.auth.reject_reason) {
658 case REJECT_REASON_OTHER:
659 if ((response.data.auth.policy.min_passwordage > 0) &&
660 (pwd_last_set + response.data.auth.policy.min_passwordage > time(NULL))) {
661 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_RECENT");
664 case REJECT_REASON_TOO_SHORT:
665 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_SHORT");
667 case REJECT_REASON_IN_HISTORY:
668 PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_HISTORY_CONFLICT");
670 case REJECT_REASON_NOT_COMPLEX:
671 _make_remark(pamh, PAM_ERROR_MSG, "Password does not meet complexity requirements");
674 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
675 "unknown password change reject reason: %d",
676 response.data.auth.reject_reason);
680 _make_remark_format(pamh, PAM_ERROR_MSG,
681 "Your password must be at least %d characters; "
682 "cannot repeat any of the your previous %d passwords"
684 "Please type a different password. "
685 "Type a password which meets these requirements in both text boxes.",
686 response.data.auth.policy.min_length_password,
687 response.data.auth.policy.password_history,
688 (response.data.auth.policy.password_properties & DOMAIN_PASSWORD_COMPLEX) ?
689 "; must contain capitals, numerals or punctuation; and cannot contain your account or full name" :
698 * Checks if a user has an account
705 static int valid_user(pam_handle_t *pamh, int ctrl, const char *user)
707 /* check not only if the user is available over NSS calls, also make
708 * sure it's really a winbind user, this is important when stacking PAM
709 * modules in the 'account' or 'password' facility. */
711 struct passwd *pwd = NULL;
712 struct winbindd_request request;
713 struct winbindd_response response;
716 ZERO_STRUCT(request);
717 ZERO_STRUCT(response);
719 pwd = getpwnam(user);
724 strncpy(request.data.username, user,
725 sizeof(request.data.username) - 1);
727 ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_GETPWNAM, &request, &response, user);
730 case PAM_USER_UNKNOWN:
740 static char *_pam_delete(register char *xx)
748 * obtain a password from the user
751 static int _winbind_read_password(pam_handle_t * pamh,
764 * make sure nothing inappropriate gets returned
767 *pass = token = NULL;
770 * which authentication token are we getting?
773 authtok_flag = on(WINBIND__OLD_PASSWORD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
776 * should we obtain the password from a PAM item ?
779 if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
780 retval = _pam_get_item(pamh, authtok_flag, &item);
781 if (retval != PAM_SUCCESS) {
783 _pam_log(pamh, ctrl, LOG_ALERT,
784 "pam_get_item returned error to unix-read-password"
787 } else if (item != NULL) { /* we have a password! */
791 } else if (on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
792 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
793 } else if (on(WINBIND_USE_AUTHTOK_ARG, ctrl)
794 && off(WINBIND__OLD_PASSWORD, ctrl)) {
795 return PAM_AUTHTOK_RECOVER_ERR;
799 * getting here implies we will have to get the password from the
804 struct pam_message msg[3], *pmsg[3];
805 struct pam_response *resp;
808 /* prepare to converse */
810 if (comment != NULL) {
812 msg[0].msg_style = PAM_TEXT_INFO;
813 msg[0].msg = CONST_DISCARD(char *, comment);
820 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
821 msg[i++].msg = CONST_DISCARD(char *, prompt1);
824 if (prompt2 != NULL) {
826 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
827 msg[i++].msg = CONST_DISCARD(char *, prompt2);
830 /* so call the conversation expecting i responses */
832 retval = converse(pamh, i, pmsg, &resp);
836 /* interpret the response */
838 if (retval == PAM_SUCCESS) { /* a good conversation */
840 token = x_strdup(resp[i - replies].resp);
843 /* verify that password entered correctly */
844 if (!resp[i - 1].resp
845 || strcmp(token, resp[i - 1].resp)) {
846 _pam_delete(token); /* mistyped */
847 retval = PAM_AUTHTOK_RECOVER_ERR;
848 _make_remark(pamh, PAM_ERROR_MSG, MISTYPED_PASS);
852 _pam_log(pamh, ctrl, LOG_NOTICE, "could not recover authentication token");
853 retval = PAM_AUTHTOK_RECOVER_ERR;
858 * tidy up the conversation (resp_retcode) is ignored
859 * -- what is it for anyway? AGM
862 _pam_drop_reply(resp, i);
865 retval = (retval == PAM_SUCCESS)
866 ? PAM_AUTHTOK_RECOVER_ERR : retval;
870 if (retval != PAM_SUCCESS) {
871 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
872 "unable to obtain a password");
875 /* 'token' is the entered password */
877 /* we store this password as an item */
879 retval = pam_set_item(pamh, authtok_flag, token);
880 _pam_delete(token); /* clean it up */
881 if (retval != PAM_SUCCESS ||
882 (retval = _pam_get_item(pamh, authtok_flag, &item)) != PAM_SUCCESS) {
884 _pam_log(pamh, ctrl, LOG_CRIT, "error manipulating password");
890 item = NULL; /* break link to password */
895 const char *get_conf_item_string(const pam_handle_t *pamh,
904 const char *parm_opt = NULL;
907 if (!(ctrl & config_flag)) {
911 /* let the pam opt take precedence over the pam_winbind.conf option */
915 if (!asprintf(&key, "global:%s", item)) {
919 parm_opt = iniparser_getstr(d, key);
923 for ( i=0; i<argc; i++ ) {
925 if ((strncmp(argv[i], item, strlen(item)) == 0)) {
928 if ( (p = strchr( argv[i], '=' )) == NULL) {
929 _pam_log(pamh, ctrl, LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
932 _pam_log_debug(pamh, ctrl, LOG_INFO, "PAM config: %s '%s'\n", item, p+1);
938 _pam_log_debug(pamh, ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
944 const char *get_krb5_cc_type_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
946 return get_conf_item_string(pamh, argc, argv, ctrl, d, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
949 const char *get_member_from_config(const pam_handle_t *pamh, int argc, const char **argv, int ctrl, dictionary *d)
951 const char *ret = NULL;
952 ret = get_conf_item_string(pamh, argc, argv, ctrl, d, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
956 return get_conf_item_string(pamh, argc, argv, ctrl, d, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
960 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
961 int argc, const char **argv)
963 const char *username;
964 const char *password;
965 const char *member = NULL;
966 const char *cctype = NULL;
967 int retval = PAM_AUTH_ERR;
968 dictionary *d = NULL;
969 char *username_ret = NULL;
971 /* parse arguments */
972 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
974 retval = PAM_SYSTEM_ERR;
978 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "pam_winbind: pam_sm_authenticate (flags: 0x%04x)", flags);
980 /* Get the username */
981 retval = pam_get_user(pamh, &username, NULL);
982 if ((retval != PAM_SUCCESS) || (!username)) {
983 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "can not get the username");
984 retval = PAM_SERVICE_ERR;
988 retval = _winbind_read_password(pamh, ctrl, NULL,
992 if (retval != PAM_SUCCESS) {
993 _pam_log(pamh, ctrl, LOG_ERR, "Could not retrieve user's password");
994 retval = PAM_AUTHTOK_ERR;
998 /* Let's not give too much away in the log file */
1000 #ifdef DEBUG_PASSWORD
1001 _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s' with password '%s'",
1002 username, password);
1004 _pam_log_debug(pamh, ctrl, LOG_INFO, "Verify user '%s'", username);
1007 member = get_member_from_config(pamh, argc, argv, ctrl, d);
1009 cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1011 /* Now use the username to look up password */
1012 retval = winbind_auth_request(pamh, ctrl, username, password, member,
1013 cctype, True, NULL, &username_ret);
1015 if (retval == PAM_NEW_AUTHTOK_REQD ||
1016 retval == PAM_AUTHTOK_EXPIRED) {
1020 if (!asprintf(&buf, "%d", retval)) {
1021 retval = PAM_BUF_ERR;
1025 pam_set_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (void *)buf, _pam_winbind_cleanup_func);
1027 retval = PAM_SUCCESS;
1033 pam_set_item (pamh, PAM_USER, username_ret);
1034 _pam_log_debug(pamh, ctrl, LOG_INFO, "Returned user was '%s'", username_ret);
1039 iniparser_freedict(d);
1045 int pam_sm_setcred(pam_handle_t *pamh, int flags,
1046 int argc, const char **argv)
1048 /* parse arguments */
1049 int ctrl = _pam_parse(pamh, flags, argc, argv, NULL);
1051 return PAM_SYSTEM_ERR;
1054 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "pam_winbind: pam_sm_setcred (flags: 0x%04x)", flags);
1056 if (flags & PAM_DELETE_CRED) {
1057 return pam_sm_close_session(pamh, flags, argc, argv);
1064 * Account management. We want to verify that the account exists
1065 * before returning PAM_SUCCESS
1068 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
1069 int argc, const char **argv)
1071 const char *username;
1072 int retval = PAM_USER_UNKNOWN;
1075 /* parse arguments */
1076 int ctrl = _pam_parse(pamh, flags, argc, argv, NULL);
1078 return PAM_SYSTEM_ERR;
1081 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "pam_winbind: pam_sm_acct_mgmt (flags: 0x%04x)", flags);
1084 /* Get the username */
1085 retval = pam_get_user(pamh, &username, NULL);
1086 if ((retval != PAM_SUCCESS) || (!username)) {
1087 _pam_log_debug(pamh, ctrl, LOG_DEBUG,"can not get the username");
1088 return PAM_SERVICE_ERR;
1091 /* Verify the username */
1092 retval = valid_user(pamh, ctrl, username);
1095 /* some sort of system error. The log was already printed */
1096 return PAM_SERVICE_ERR;
1098 /* the user does not exist */
1099 _pam_log_debug(pamh, ctrl, LOG_NOTICE, "user '%s' not found", username);
1100 if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
1103 return PAM_USER_UNKNOWN;
1105 pam_get_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (const void **)&tmp);
1107 retval = atoi((const char *)tmp);
1109 case PAM_AUTHTOK_EXPIRED:
1110 /* fall through, since new token is required in this case */
1111 case PAM_NEW_AUTHTOK_REQD:
1112 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success but %s is set",
1113 PAM_WINBIND_NEW_AUTHTOK_REQD);
1114 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' needs new password", username);
1115 /* PAM_AUTHTOKEN_REQD does not exist, but is documented in the manpage */
1116 return PAM_NEW_AUTHTOK_REQD;
1118 _pam_log(pamh, ctrl, LOG_WARNING, "pam_sm_acct_mgmt success");
1119 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1124 /* Otherwise, the authentication looked good */
1125 _pam_log(pamh, ctrl, LOG_NOTICE, "user '%s' granted access", username);
1128 /* we don't know anything about this return value */
1129 _pam_log(pamh, ctrl, LOG_ERR, "internal module error (retval = %d, user = '%s')",
1131 return PAM_SERVICE_ERR;
1134 /* should not be reached */
1139 int pam_sm_open_session(pam_handle_t *pamh, int flags,
1140 int argc, const char **argv)
1142 /* parse arguments */
1143 int ctrl = _pam_parse(pamh, flags, argc, argv, NULL);
1145 return PAM_SYSTEM_ERR;
1148 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "pam_winbind: pam_sm_open_session handler (flags: 0x%04x)", flags);
1154 int pam_sm_close_session(pam_handle_t *pamh, int flags,
1155 int argc, const char **argv)
1157 dictionary *d = NULL;
1158 int retval = PAM_SUCCESS;
1160 /* parse arguments */
1161 int ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1163 retval = PAM_SYSTEM_ERR;
1167 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "pam_winbind: pam_sm_close_session handler (flags: 0x%04x)", flags);
1169 if (!(flags & PAM_DELETE_CRED)) {
1170 retval = PAM_SUCCESS;
1174 if (ctrl & WINBIND_KRB5_AUTH) {
1176 /* destroy the ccache here */
1177 struct winbindd_request request;
1178 struct winbindd_response response;
1180 const char *ccname = NULL;
1181 struct passwd *pwd = NULL;
1183 ZERO_STRUCT(request);
1184 ZERO_STRUCT(response);
1186 retval = pam_get_user(pamh, &user, "Username: ");
1187 if (retval == PAM_SUCCESS) {
1189 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1190 retval = PAM_USER_UNKNOWN;
1193 if (retval == PAM_SUCCESS) {
1194 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained", user);
1197 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "could not identify user");
1201 ccname = pam_getenv(pamh, "KRB5CCNAME");
1202 if (ccname == NULL) {
1203 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
1206 strncpy(request.data.logoff.user, user,
1207 sizeof(request.data.logoff.user) - 1);
1210 strncpy(request.data.logoff.krb5ccname, ccname,
1211 sizeof(request.data.logoff.krb5ccname) - 1);
1214 pwd = getpwnam(user);
1216 retval = PAM_USER_UNKNOWN;
1219 request.data.logoff.uid = pwd->pw_uid;
1221 request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
1223 retval = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
1228 iniparser_freedict(d);
1236 int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
1237 int argc, const char **argv)
1243 /* <DO NOT free() THESE> */
1245 char *pass_old, *pass_new;
1246 /* </DO NOT free() THESE> */
1251 dictionary *d = NULL;
1253 ctrl = _pam_parse(pamh, flags, argc, argv, &d);
1255 retval = PAM_SYSTEM_ERR;
1259 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "pam_winbind: pam_sm_chauthtok (flags: 0x%04x)", flags);
1261 /* clearing offline bit for the auth in the password change */
1262 ctrl &= ~WINBIND_CACHED_LOGIN;
1265 * First get the name of a user
1267 retval = pam_get_user(pamh, &user, "Username: ");
1268 if (retval == PAM_SUCCESS) {
1270 _pam_log(pamh, ctrl, LOG_ERR, "username was NULL!");
1271 retval = PAM_USER_UNKNOWN;
1274 if (retval == PAM_SUCCESS) {
1275 _pam_log_debug(pamh, ctrl, LOG_DEBUG, "username [%s] obtained",
1279 _pam_log_debug(pamh, ctrl, LOG_DEBUG,
1280 "password - could not identify user");
1284 /* check if this is really a user in winbindd, not only in NSS */
1285 retval = valid_user(pamh, ctrl, user);
1288 retval = PAM_USER_UNKNOWN;
1291 retval = PAM_SYSTEM_ERR;
1298 * obtain and verify the current password (OLDAUTHTOK) for
1302 if (flags & PAM_PRELIM_CHECK) {
1304 time_t pwdlastset_prelim = 0;
1306 /* instruct user what is happening */
1307 #define greeting "Changing password for "
1308 Announce = (char *) malloc(sizeof(greeting) + strlen(user));
1309 if (Announce == NULL) {
1310 _pam_log(pamh, ctrl, LOG_CRIT, "password - out of memory");
1311 retval = PAM_BUF_ERR;
1314 (void) strcpy(Announce, greeting);
1315 (void) strcpy(Announce + sizeof(greeting) - 1, user);
1318 lctrl = ctrl | WINBIND__OLD_PASSWORD;
1319 retval = _winbind_read_password(pamh, lctrl,
1321 "(current) NT password: ",
1323 (const char **) &pass_old);
1324 if (retval != PAM_SUCCESS) {
1325 _pam_log(pamh, ctrl, LOG_NOTICE, "password - (old) token not obtained");
1328 /* verify that this is the password for this user */
1330 retval = winbind_auth_request(pamh, ctrl, user, pass_old,
1331 NULL, NULL, False, &pwdlastset_prelim, NULL);
1333 if (retval != PAM_ACCT_EXPIRED &&
1334 retval != PAM_AUTHTOK_EXPIRED &&
1335 retval != PAM_NEW_AUTHTOK_REQD &&
1336 retval != PAM_SUCCESS) {
1341 pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET, (void *)pwdlastset_prelim, NULL);
1343 retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
1345 if (retval != PAM_SUCCESS) {
1346 _pam_log(pamh, ctrl, LOG_CRIT, "failed to set PAM_OLDAUTHTOK");
1348 } else if (flags & PAM_UPDATE_AUTHTOK) {
1350 time_t pwdlastset_update = 0;
1353 * obtain the proposed password
1357 * get the old token back.
1360 retval = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
1362 if (retval != PAM_SUCCESS) {
1363 _pam_log(pamh, ctrl, LOG_NOTICE, "user not authenticated");
1369 if (on(WINBIND_USE_AUTHTOK_ARG, lctrl)) {
1370 lctrl |= WINBIND_USE_FIRST_PASS_ARG;
1373 retval = PAM_AUTHTOK_ERR;
1374 while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
1376 * use_authtok is to force the use of a previously entered
1377 * password -- needed for pluggable password strength checking
1380 retval = _winbind_read_password(pamh, lctrl,
1382 "Enter new NT password: ",
1383 "Retype new NT password: ",
1384 (const char **) &pass_new);
1386 if (retval != PAM_SUCCESS) {
1387 _pam_log_debug(pamh, ctrl, LOG_ALERT
1388 ,"password - new password not obtained");
1389 pass_old = NULL;/* tidy up */
1394 * At this point we know who the user is and what they
1395 * propose as their new password. Verify that the new
1396 * password is acceptable.
1399 if (pass_new[0] == '\0') {/* "\0" password = NULL */
1405 * By reaching here we have approved the passwords and must now
1406 * rebuild the password database file.
1408 _pam_get_data( pamh, PAM_WINBIND_PWD_LAST_SET,
1409 &pwdlastset_update);
1411 retval = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new, pwdlastset_update);
1413 _pam_overwrite(pass_new);
1414 _pam_overwrite(pass_old);
1415 pass_old = pass_new = NULL;
1419 /* just in case we need krb5 creds after a password change over msrpc */
1421 if (ctrl & WINBIND_KRB5_AUTH) {
1423 const char *member = get_member_from_config(pamh, argc, argv, ctrl, d);
1424 const char *cctype = get_krb5_cc_type_from_config(pamh, argc, argv, ctrl, d);
1426 retval = winbind_auth_request(pamh, ctrl, user, pass_new,
1427 member, cctype, False, NULL, NULL);
1428 _pam_overwrite(pass_new);
1429 _pam_overwrite(pass_old);
1430 pass_old = pass_new = NULL;
1433 retval = PAM_SERVICE_ERR;
1438 iniparser_freedict(d);
1445 /* static module data */
1447 struct pam_module _pam_winbind_modstruct = {
1449 pam_sm_authenticate,
1452 pam_sm_open_session,
1453 pam_sm_close_session,
1460 * Copyright (c) Andrew Tridgell <tridge@samba.org> 2000
1461 * Copyright (c) Tim Potter <tpot@samba.org> 2000
1462 * Copyright (c) Andrew Bartlettt <abartlet@samba.org> 2002
1463 * Copyright (c) Guenther Deschner <gd@samba.org> 2005-2006
1464 * Copyright (c) Jan Rêkorajski 1999.
1465 * Copyright (c) Andrew G. Morgan 1996-8.
1466 * Copyright (c) Alex O. Yuriev, 1996.
1467 * Copyright (c) Cristian Gafton 1996.
1468 * Copyright (C) Elliot Lee <sopwith@redhat.com> 1996, Red Hat Software.
1470 * Redistribution and use in source and binary forms, with or without
1471 * modification, are permitted provided that the following conditions
1473 * 1. Redistributions of source code must retain the above copyright
1474 * notice, and the entire permission notice in its entirety,
1475 * including the disclaimer of warranties.
1476 * 2. Redistributions in binary form must reproduce the above copyright
1477 * notice, this list of conditions and the following disclaimer in the
1478 * documentation and/or other materials provided with the distribution.
1479 * 3. The name of the author may not be used to endorse or promote
1480 * products derived from this software without specific prior
1481 * written permission.
1483 * ALTERNATIVELY, this product may be distributed under the terms of
1484 * the GNU Public License, in which case the provisions of the GPL are
1485 * required INSTEAD OF the above restrictions. (This clause is
1486 * necessary due to a potential bad interaction between the GPL and
1487 * the restrictions contained in a BSD-style copyright.)
1489 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
1490 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1491 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1492 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1493 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1494 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1495 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1496 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1497 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1498 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1499 * OF THE POSSIBILITY OF SUCH DAMAGE.