r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[vlendec/samba-autobuild/.git] / source3 / nsswitch / pam_winbind.c
index 57e05dc4bb035d9c28b415e143e224e2a9012a95..118ba358e1693eb539d16de00ff783a8c32d1044 100644 (file)
@@ -10,7 +10,6 @@
    (see copyright below for full details)
 */
 
-#include "includes.h"
 #include "pam_winbind.h"
 
 /* data tokens */
@@ -44,60 +43,78 @@ static void _pam_log_debug(int ctrl, int err, const char *format, ...)
        closelog();
 }
 
-static int _pam_parse(int argc, const char **argv)
+static int _pam_parse(int argc, const char **argv, dictionary **d)
 {
        int ctrl = 0;
+       const char *config_file = NULL;
 
-       load_case_tables();
+       if (d == NULL) {
+               goto config_from_pam;
+       }
+
+       for (; argc-- > 0; ++argv) {
+               if (!strncasecmp(*argv, "config", strlen("config"))) {
+                       ctrl |= WINBIND_CONFIG_FILE;
+                       config_file = argv[argc];
+                       break;
+               }
+       }
+
+       if (config_file == NULL) {
+               config_file = PAM_WINBIND_CONFIG_FILE;
+       }
+
+       *d = iniparser_load(CONST_DISCARD(char *, config_file));
+       if (*d == NULL) {
+               goto config_from_pam;
+       }
 
-       if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
-               return -1;
+       if (iniparser_getboolean(*d, CONST_DISCARD(char *, "global:debug"), False)) {
+               ctrl |= WINBIND_DEBUG_ARG;
        }
 
-       if (lp_parm_bool(-1, "pam_winbind", "cached_login", False)) {
+       if (iniparser_getboolean(*d, CONST_DISCARD(char *, "global:cached_login"), False)) {
                ctrl |= WINBIND_CACHED_LOGIN;
        }
-       if (lp_parm_bool(-1, "pam_winbind", "krb5_auth", False)) {
+
+       if (iniparser_getboolean(*d, CONST_DISCARD(char *, "global:krb5_auth"), False)) {
                ctrl |= WINBIND_KRB5_AUTH;
        }
-       if (lp_parm_const_string(-1, "pam_winbind", "krb5_ccache_type", NULL) != NULL) {
+
+       if (iniparser_getstr(*d, CONST_DISCARD(char *,"global:krb5_ccache_type")) != NULL) {
                ctrl |= WINBIND_KRB5_CCACHE_TYPE;
        }
-       if ((lp_parm_const_string(-1, "pam_winbind", "require-membership-of", NULL) != NULL) || 
-           (lp_parm_const_string(-1, "pam_winbind", "require_membership_of", NULL) != NULL)) { 
+       
+       if ((iniparser_getstr(*d, CONST_DISCARD(char *, "global:require-membership-of")) != NULL) ||
+           (iniparser_getstr(*d, CONST_DISCARD(char *, "global:require_membership_of")) != NULL)) {
                ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
        }
-       if (lp_parm_bool(-1, "pam_winbind", "create_homedir", False)) {
-               ctrl |= WINBIND_CREATE_HOMEDIR;
-       }
 
+config_from_pam:
        /* step through arguments */
        for (; argc-- > 0; ++argv) {
 
                /* generic options */
-
-               if (!StrCaseCmp(*argv, "debug"))
+               if (!strcmp(*argv,"debug"))
                        ctrl |= WINBIND_DEBUG_ARG;
-               else if (strequal(*argv, "use_authtok"))
+               else if (!strcasecmp(*argv, "use_authtok"))
                        ctrl |= WINBIND_USE_AUTHTOK_ARG;
-               else if (strequal(*argv, "use_first_pass"))
+               else if (!strcasecmp(*argv, "use_first_pass"))
                        ctrl |= WINBIND_USE_FIRST_PASS_ARG;
-               else if (strequal(*argv, "try_first_pass"))
+               else if (!strcasecmp(*argv, "try_first_pass"))
                        ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
-               else if (strequal(*argv, "unknown_ok"))
+               else if (!strcasecmp(*argv, "unknown_ok"))
                        ctrl |= WINBIND_UNKNOWN_OK_ARG;
-               else if (strnequal(*argv, "require_membership_of", strlen("require_membership_of")))
+               else if (!strncasecmp(*argv, "require_membership_of", strlen("require_membership_of")))
                        ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
-               else if (strnequal(*argv, "require-membership-of", strlen("require-membership-of")))
+               else if (!strncasecmp(*argv, "require-membership-of", strlen("require-membership-of")))
                        ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
-               else if (strequal(*argv, "krb5_auth"))
+               else if (!strcasecmp(*argv, "krb5_auth"))
                        ctrl |= WINBIND_KRB5_AUTH;
-               else if (strnequal(*argv, "krb5_ccache_type", strlen("krb5_ccache_type")))
+               else if (!strncasecmp(*argv, "krb5_ccache_type", strlen("krb5_ccache_type")))
                        ctrl |= WINBIND_KRB5_CCACHE_TYPE;
-               else if (strequal(*argv, "cached_login"))
+               else if (!strcasecmp(*argv, "cached_login"))
                        ctrl |= WINBIND_CACHED_LOGIN;
-               else if (strequal(*argv, "create_homedir"))
-                       ctrl |= WINBIND_CREATE_HOMEDIR;
                else {
                        _pam_log(LOG_ERR, "pam_parse: unknown option; %s", *argv);
                }
@@ -111,6 +128,24 @@ static void _pam_winbind_cleanup_func(pam_handle_t *pamh, void *data, int error_
        SAFE_FREE(data);
 }
 
+/*
+ * Work around the pam API that has functions with void ** as parameters.
+ * These lead to strict aliasing warnings with gcc.
+ */
+static int _pam_get_item(const pam_handle_t *pamh, int item_type,
+                        const void *_item)
+{
+       const void **item = (const void **)_item;
+       return pam_get_item(pamh, item_type, item);
+}
+static int _pam_get_data(const pam_handle_t *pamh,
+                        const char *module_data_name, const void *_data)
+{
+       const void **data = (const void **)_data;
+       return pam_get_data(pamh, module_data_name, data);
+}
+
+
 static const struct ntstatus_errors {
        const char *ntstatus_string;
        const char *error_string;
@@ -138,7 +173,7 @@ const char *_get_ntstatus_error_string(const char *nt_status_string)
 {
        int i;
        for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
-               if (strequal(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
+               if (!strcasecmp(ntstatus_errors[i].ntstatus_string, nt_status_string)) {
                        return ntstatus_errors[i].error_string;
                }
        }
@@ -156,7 +191,7 @@ static int converse(pam_handle_t *pamh, int nargs,
        int retval;
        struct pam_conv *conv;
 
-       retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv );
+       retval = _pam_get_item(pamh, PAM_CONV, &conv );
        if (retval == PAM_SUCCESS) {
                retval = conv->conv(nargs, (const struct pam_message **)message,
                                    response, conv->appdata_ptr);
@@ -174,7 +209,7 @@ static int _make_remark(pam_handle_t * pamh, int type, const char *text)
        struct pam_response *resp;
        
        pmsg[0] = &msg[0];
-       msg[0].msg = text;
+       msg[0].msg = CONST_DISCARD(char *, text);
        msg[0].msg_style = type;
        
        resp = NULL;
@@ -225,8 +260,9 @@ static int pam_winbind_request(pam_handle_t * pamh, int ctrl,
        /* Copy reply data from socket */
        if (response->result != WINBINDD_OK) {
                if (response->data.auth.pam_error != PAM_SUCCESS) {
-                       _pam_log(LOG_ERR, "request failed: %s, PAM error was %d, NT error was %s", 
+                       _pam_log(LOG_ERR, "request failed: %s, PAM error was %s (%d), NT error was %s", 
                                 response->data.auth.error_string,
+                                pam_strerror(pamh, response->data.auth.pam_error),
                                 response->data.auth.pam_error,
                                 response->data.auth.nt_status_string);
                        return response->data.auth.pam_error;
@@ -269,8 +305,7 @@ static int pam_winbind_request_log(pam_handle_t * pamh,
                return retval;
        case PAM_USER_UNKNOWN:
                /* the user does not exist */
-               _pam_log_debug(ctrl, LOG_NOTICE, "user `%s' not found",
-                                user);
+               _pam_log_debug(ctrl, LOG_NOTICE, "user `%s' not found", user);
                if (ctrl & WINBIND_UNKNOWN_OK_ARG) {
                        return PAM_IGNORE;
                }        
@@ -303,7 +338,8 @@ static int winbind_auth_request(pam_handle_t * pamh,
                                const char *pass, 
                                const char *member, 
                                const char *cctype,
-                               int process_result)
+                               int process_result,
+                               time_t *pwd_last_set)
 {
        struct winbindd_request request;
        struct winbindd_response response;
@@ -312,6 +348,10 @@ static int winbind_auth_request(pam_handle_t * pamh,
        ZERO_STRUCT(request);
        ZERO_STRUCT(response);
 
+       if (pwd_last_set) {
+               *pwd_last_set = 0;
+       }
+
        strncpy(request.data.auth.user, user, 
                sizeof(request.data.auth.user)-1);
 
@@ -321,7 +361,7 @@ static int winbind_auth_request(pam_handle_t * pamh,
        request.data.auth.krb5_cc_type[0] = '\0';
        request.data.auth.uid = -1;
        
-       request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_CONTACT_TRUSTDOM;
+       request.flags = WBFLAG_PAM_INFO3_TEXT | WBFLAG_PAM_GET_PWD_POLICY;
 
        if (ctrl & WINBIND_KRB5_AUTH) {
 
@@ -368,7 +408,8 @@ static int winbind_auth_request(pam_handle_t * pamh,
                _pam_log_debug(ctrl, LOG_DEBUG, "no sid given, looking up: %s\n", member);
 
                /* fortunatly winbindd can handle non-separated names */
-               fstrcpy(sid_request.data.name.name, member);
+               strncpy(sid_request.data.name.name, member,
+                       sizeof(sid_request.data.name.name) - 1);
 
                if (pam_winbind_request_log(pamh, ctrl, WINBINDD_LOOKUPNAME, &sid_request, &sid_response, user)) {
                        _pam_log(LOG_INFO, "could not lookup name: %s\n", member); 
@@ -383,6 +424,10 @@ static int winbind_auth_request(pam_handle_t * pamh,
        
        ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_AUTH, &request, &response, user);
 
+       if (pwd_last_set) {
+               *pwd_last_set = response.data.auth.info3.pass_last_set_time;
+       }
+
        if ((ctrl & WINBIND_KRB5_AUTH) && 
            response.data.auth.krb5ccname[0] != '\0') {
 
@@ -419,7 +464,8 @@ static int winbind_auth_request(pam_handle_t * pamh,
 
        /* handle the case where the auth was ok, but the password must expire right now */
        /* good catch from Ralf Haferkamp: an expiry of "never" is translated to -1 */
-       if ((response.data.auth.policy.expire > 0) && 
+       if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
+           (response.data.auth.policy.expire > 0) && 
            (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire < time(NULL))) {
 
                ret = PAM_AUTHTOK_EXPIRED;
@@ -435,7 +481,8 @@ static int winbind_auth_request(pam_handle_t * pamh,
        }
 
        /* warn a user if the password is about to expire soon */
-       if ((response.data.auth.policy.expire) && 
+       if ( ! (response.data.auth.info3.acct_flags & ACB_PWNOEXP) &&
+           (response.data.auth.policy.expire) && 
            (response.data.auth.info3.pass_last_set_time + response.data.auth.policy.expire > time(NULL) ) ) {
 
                int days = response.data.auth.policy.expire / SECONDS_PER_DAY;
@@ -446,6 +493,7 @@ static int winbind_auth_request(pam_handle_t * pamh,
 
        if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
                _make_remark(pamh, PAM_ERROR_MSG, "Logging on using cached account. Network ressources can be unavailable");
+               _pam_log_debug(ctrl, LOG_DEBUG,"User %s logged on using cached account\n", user);
        }
 
        /* save the CIFS homedir for pam_cifs / pam_mount */
@@ -467,7 +515,8 @@ static int winbind_chauthtok_request(pam_handle_t * pamh,
                                     int ctrl,
                                     const char *user, 
                                     const char *oldpass,
-                                    const char *newpass) 
+                                    const char *newpass,
+                                    time_t pwd_last_set) 
 {
        struct winbindd_request request;
        struct winbindd_response response;
@@ -496,7 +545,7 @@ static int winbind_chauthtok_request(pam_handle_t * pamh,
        }
 
        if (ctrl & WINBIND_KRB5_AUTH) {
-               request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
+               request.flags = WBFLAG_PAM_KRB5;
        }
 
        ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_CHAUTHTOK, &request, &response, user);
@@ -517,11 +566,17 @@ static int winbind_chauthtok_request(pam_handle_t * pamh,
        /* TODO: tell the history length ? */
        PAM_WB_REMARK_CHECK_RESPONSE_RET(pamh, response, "NT_STATUS_PWD_HISTORY_CONFLICT");
 
-       if (strequal(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
+       if (!strcasecmp(response.data.auth.nt_status_string, "NT_STATUS_PASSWORD_RESTRICTION")) {
 
                /* FIXME: avoid to send multiple PAM messages after another */
                switch (response.data.auth.reject_reason) {
-                       case 0:
+                       case -1:
+                               break;
+                       case REJECT_REASON_OTHER:
+                               if ((response.data.auth.policy.min_passwordage > 0) &&
+                                   (pwd_last_set + response.data.auth.policy.min_passwordage > time(NULL))) {
+                                       PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_RECENT");
+                               }
                                break;
                        case REJECT_REASON_TOO_SHORT:
                                PAM_WB_REMARK_DIRECT(pamh, "NT_STATUS_PWD_TOO_SHORT");
@@ -564,10 +619,39 @@ static int winbind_chauthtok_request(pam_handle_t * pamh,
  *      0  = OK
  *     -1  = System error
  */
-static int valid_user(const char *user)
+static int valid_user(const char *user, pam_handle_t *pamh, int ctrl)
 {
-       if (getpwnam(user)) return 0;
-       return 1;
+       /* check not only if the user is available over NSS calls, also make
+        * sure it's really a winbind user, this is important when stacking PAM
+        * modules in the 'account' or 'password' facility. */
+
+       struct passwd *pwd = NULL;
+       struct winbindd_request request;
+       struct winbindd_response response;
+       int ret;
+
+       ZERO_STRUCT(request);
+       ZERO_STRUCT(response);
+
+       pwd = getpwnam(user);
+       if (pwd == NULL) {
+               return 1;
+       }
+
+       strncpy(request.data.username, user,
+               sizeof(request.data.username) - 1);
+
+       ret = pam_winbind_request_log(pamh, ctrl, WINBINDD_GETPWNAM, &request, &response, user);
+
+       switch (ret) {
+               case PAM_USER_UNKNOWN:
+                       return 1;
+               case PAM_SUCCESS:
+                       return 0;
+               default:
+                       break;
+       }
+       return -1;
 }
 
 static char *_pam_delete(register char *xx)
@@ -610,7 +694,7 @@ static int _winbind_read_password(pam_handle_t * pamh,
         */
 
        if (on(WINBIND_TRY_FIRST_PASS_ARG, ctrl) || on(WINBIND_USE_FIRST_PASS_ARG, ctrl)) {
-               retval = pam_get_item(pamh, authtok_flag, (const void **) &item);
+               retval = _pam_get_item(pamh, authtok_flag, &item);
                if (retval != PAM_SUCCESS) {
                        /* very strange. */
                        _pam_log(LOG_ALERT, 
@@ -643,7 +727,7 @@ static int _winbind_read_password(pam_handle_t * pamh,
                if (comment != NULL) {
                        pmsg[0] = &msg[0];
                        msg[0].msg_style = PAM_TEXT_INFO;
-                       msg[0].msg = comment;
+                       msg[0].msg = CONST_DISCARD(char *, comment);
                        i = 1;
                } else {
                        i = 0;
@@ -651,13 +735,13 @@ static int _winbind_read_password(pam_handle_t * pamh,
 
                pmsg[i] = &msg[i];
                msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
-               msg[i++].msg = prompt1;
+               msg[i++].msg = CONST_DISCARD(char *, prompt1);
                replies = 1;
 
                if (prompt2 != NULL) {
                        pmsg[i] = &msg[i];
                        msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
-                       msg[i++].msg = prompt2;
+                       msg[i++].msg = CONST_DISCARD(char *, prompt2);
                        ++replies;
                }
                /* so call the conversation expecting i responses */
@@ -670,20 +754,20 @@ static int _winbind_read_password(pam_handle_t * pamh,
 
                        if (retval == PAM_SUCCESS) {    /* a good conversation */
 
-                               token = SMB_STRDUP(resp[i - replies].resp);
+                               token = x_strdup(resp[i - replies].resp);
                                if (token != NULL) {
                                        if (replies == 2) {
                                                /* verify that password entered correctly */
                                                if (!resp[i - 1].resp
-                                                   || StrCaseCmp(token, resp[i - 1].resp)) {
+                                                   || strcmp(token, resp[i - 1].resp)) {
                                                        _pam_delete(token);     /* mistyped */
                                                        retval = PAM_AUTHTOK_RECOVER_ERR;
                                                        _make_remark(pamh, PAM_ERROR_MSG, MISTYPED_PASS);
                                                }
                                        }
                                } else {
-                                       _pam_log(LOG_NOTICE
-                                                ,"could not recover authentication token");
+                                       _pam_log(LOG_NOTICE, "could not recover authentication token");
+                                       retval = PAM_AUTHTOK_RECOVER_ERR;
                                }
 
                        }
@@ -712,7 +796,7 @@ static int _winbind_read_password(pam_handle_t * pamh,
        retval = pam_set_item(pamh, authtok_flag, token);
        _pam_delete(token);     /* clean it up */
        if (retval != PAM_SUCCESS || 
-           (retval = pam_get_item(pamh, authtok_flag, (const void **) &item)) != PAM_SUCCESS) {
+           (retval = _pam_get_item(pamh, authtok_flag, &item)) != PAM_SUCCESS) {
                
                _pam_log(LOG_CRIT, "error manipulating password");
                return retval;
@@ -727,27 +811,38 @@ static int _winbind_read_password(pam_handle_t * pamh,
 
 const char *get_conf_item_string(int argc, 
                                 const char **argv, 
-                                int ctrl, 
+                                int ctrl,
+                                dictionary *d,
                                 const char *item, 
                                 int flag)
 {
        int i = 0;
        char *parm = NULL;
        const char *parm_opt = NULL;
+       char *key = NULL;
 
        if (!(ctrl & flag)) {
                goto out;
        }
 
-       /* let the pam opt take precedence over the smb.conf option */
-       parm_opt = lp_parm_const_string(-1, "pam_winbind", item, NULL);
+       /* let the pam opt take precedence over the pam_winbind.conf option */
+
+       if (d != NULL) {
+
+               if (!asprintf(&key, "global:%s", item)) {
+                       goto out;
+               }
+
+               parm_opt = iniparser_getstr(d, key);
+               SAFE_FREE(key);
+       }
 
        for ( i=0; i<argc; i++ ) {
 
                if ((strncmp(argv[i], item, strlen(item)) == 0)) {
                        char *p;
 
-                       parm = SMB_STRDUP(argv[i]);
+                       parm = strdup(argv[i]);
 
                        if ( (p = strchr( parm, '=' )) == NULL) {
                                _pam_log(LOG_INFO, "no \"=\" delimiter for \"%s\" found\n", item);
@@ -759,25 +854,27 @@ const char *get_conf_item_string(int argc,
                }
        }
 
-       _pam_log_debug(ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
+       if (d != NULL) {
+               _pam_log_debug(ctrl, LOG_INFO, "CONFIG file: %s '%s'\n", item, parm_opt);
+       }
 out:
        SAFE_FREE(parm);
        return parm_opt;
 }
 
-const char *get_krb5_cc_type_from_config(int argc, const char **argv, int ctrl)
+const char *get_krb5_cc_type_from_config(int argc, const char **argv, int ctrl, dictionary *d)
 {
-       return get_conf_item_string(argc, argv, ctrl, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
+       return get_conf_item_string(argc, argv, ctrl, d, "krb5_ccache_type", WINBIND_KRB5_CCACHE_TYPE);
 }
 
-const char *get_member_from_config(int argc, const char **argv, int ctrl)
+const char *get_member_from_config(int argc, const char **argv, int ctrl, dictionary *d)
 {
        const char *ret = NULL;
-       ret = get_conf_item_string(argc, argv, ctrl, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
+       ret = get_conf_item_string(argc, argv, ctrl, d, "require_membership_of", WINBIND_REQUIRED_MEMBERSHIP);
        if (ret) {
                return ret;
        }
-       return get_conf_item_string(argc, argv, ctrl, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
+       return get_conf_item_string(argc, argv, ctrl, d, "require-membership-of", WINBIND_REQUIRED_MEMBERSHIP);
 }
 
 PAM_EXTERN
@@ -789,20 +886,23 @@ int pam_sm_authenticate(pam_handle_t *pamh, int flags,
        const char *member = NULL;
        const char *cctype = NULL;
        int retval = PAM_AUTH_ERR;
+       dictionary *d;
 
        /* parse arguments */
-       int ctrl = _pam_parse(argc, argv);
+       int ctrl = _pam_parse(argc, argv, &d);
        if (ctrl == -1) {
-               return PAM_SYSTEM_ERR;
+               retval = PAM_SYSTEM_ERR;
+               goto out;
        }
 
-       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_authenticate");
+       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_authenticate (flags: 0x%04x)", flags);
 
        /* Get the username */
        retval = pam_get_user(pamh, &username, NULL);
        if ((retval != PAM_SUCCESS) || (!username)) {
                _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
-               return PAM_SERVICE_ERR;
+               retval = PAM_SERVICE_ERR;
+               goto out;
        }
 
        retval = _winbind_read_password(pamh, ctrl, NULL, 
@@ -811,7 +911,8 @@ int pam_sm_authenticate(pam_handle_t *pamh, int flags,
 
        if (retval != PAM_SUCCESS) {
                _pam_log(LOG_ERR, "Could not retrieve user's password");
-               return PAM_AUTHTOK_ERR;
+               retval = PAM_AUTHTOK_ERR;
+               goto out;
        }
 
        /* Let's not give too much away in the log file */
@@ -823,12 +924,12 @@ int pam_sm_authenticate(pam_handle_t *pamh, int flags,
        _pam_log_debug(ctrl, LOG_INFO, "Verify user `%s'", username);
 #endif
 
-       member = get_member_from_config(argc, argv, ctrl);
+       member = get_member_from_config(argc, argv, ctrl, d);
 
-       cctype = get_krb5_cc_type_from_config(argc, argv, ctrl);
+       cctype = get_krb5_cc_type_from_config(argc, argv, ctrl, d);
 
        /* Now use the username to look up password */
-       retval = winbind_auth_request(pamh, ctrl, username, password, member, cctype, True);
+       retval = winbind_auth_request(pamh, ctrl, username, password, member, cctype, True, NULL);
 
        if (retval == PAM_NEW_AUTHTOK_REQD ||
            retval == PAM_AUTHTOK_EXPIRED) {
@@ -836,14 +937,20 @@ int pam_sm_authenticate(pam_handle_t *pamh, int flags,
                char *buf;
 
                if (!asprintf(&buf, "%d", retval)) {
-                       return PAM_BUF_ERR;
+                       retval = PAM_BUF_ERR;
+                       goto out;
                }
 
                pam_set_data( pamh, PAM_WINBIND_NEW_AUTHTOK_REQD, (void *)buf, _pam_winbind_cleanup_func);
 
-               return PAM_SUCCESS;
+               retval = PAM_SUCCESS;
+               goto out;
        }
 
+out:
+       if (d) {
+               iniparser_freedict(d);
+       }
        return retval;
 }
 
@@ -852,12 +959,12 @@ int pam_sm_setcred(pam_handle_t *pamh, int flags,
                   int argc, const char **argv)
 {
        /* parse arguments */
-       int ctrl = _pam_parse(argc, argv);
+       int ctrl = _pam_parse(argc, argv, NULL);
        if (ctrl == -1) {
                return PAM_SYSTEM_ERR;
        }
 
-       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_setcred");
+       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_setcred (flags: 0x%04x)", flags);
 
        if (flags & PAM_DELETE_CRED) {
                return pam_sm_close_session(pamh, flags, argc, argv);
@@ -879,12 +986,12 @@ int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
        void *tmp = NULL;
 
        /* parse arguments */
-       int ctrl = _pam_parse(argc, argv);
+       int ctrl = _pam_parse(argc, argv, NULL);
        if (ctrl == -1) {
                return PAM_SYSTEM_ERR;
        }
 
-       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_acct_mgmt");
+       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_acct_mgmt (flags: 0x%04x)", flags);
 
 
        /* Get the username */
@@ -895,7 +1002,7 @@ int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
        }
 
        /* Verify the username */
-       retval = valid_user(username);
+       retval = valid_user(username, pamh, ctrl);
        switch (retval) {
        case -1:
                /* some sort of system error. The log was already printed */
@@ -946,65 +1053,12 @@ int pam_sm_open_session(pam_handle_t *pamh, int flags,
                        int argc, const char **argv)
 {
        /* parse arguments */
-       int ctrl = _pam_parse(argc, argv);
+       int ctrl = _pam_parse(argc, argv, NULL);
        if (ctrl == -1) {
                return PAM_SYSTEM_ERR;
        }
 
-       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_open_session handler");
-
-
-       if (ctrl & WINBIND_CREATE_HOMEDIR) {
-
-               struct passwd *pwd = NULL;
-               const char *username;
-               int ret;
-               fstring tok;
-               fstring create_dir;
-               SMB_STRUCT_STAT sbuf;
-
-               /* Get the username */
-               ret = pam_get_user(pamh, &username, NULL);
-               if ((ret != PAM_SUCCESS) || (!username)) {
-                       _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
-                       return PAM_SERVICE_ERR;
-               }
-
-               pwd = getpwnam(username);
-               if (pwd == NULL) {
-                       _pam_log_debug(ctrl, LOG_DEBUG, "can not get the username");
-                       return PAM_SERVICE_ERR;
-               }
-
-               _pam_log_debug(ctrl, LOG_DEBUG, "homedir is: %s", pwd->pw_dir);
-
-               if (directory_exist(pwd->pw_dir, &sbuf)) {
-                       return PAM_SUCCESS;
-               }
-
-               fstrcpy(create_dir, "/");
-               while (next_token((const char **)&pwd->pw_dir, tok, "/", sizeof(tok))) {
-                       
-                       mode_t mode = 0755;
-
-                       fstrcat(create_dir, tok);
-                       fstrcat(create_dir, "/");
-
-                       if (!directory_exist(create_dir, &sbuf)) {
-                               if (mkdir(create_dir, mode) != 0) {
-                                       _pam_log(LOG_ERR, "could not create dir: %s (%s)", 
-                                                create_dir, strerror(errno));
-                                       return PAM_SERVICE_ERR;
-                               }
-                       } 
-               }
-
-               if (sys_chown(create_dir, pwd->pw_uid, pwd->pw_gid) != 0) {
-                       _pam_log(LOG_ERR, "failed to chown user homedir: %s (%s)", 
-                                create_dir, strerror(errno));
-                       return PAM_SERVICE_ERR;
-               }
-       }
+       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_open_session handler (flags: 0x%04x)", flags);
 
        return PAM_SUCCESS;
 }
@@ -1013,16 +1067,21 @@ PAM_EXTERN
 int pam_sm_close_session(pam_handle_t *pamh, int flags,
                         int argc, const char **argv)
 {
+       dictionary *d;
+       int retval = PAM_SUCCESS;
+
        /* parse arguments */
-       int ctrl = _pam_parse(argc, argv);
+       int ctrl = _pam_parse(argc, argv, &d);
        if (ctrl == -1) {
-               return PAM_SYSTEM_ERR;
+               retval = PAM_SYSTEM_ERR;
+               goto out;
        }
 
-       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_close_session handler");
+       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_close_session handler (flags: 0x%04x)", flags);
 
        if (!(flags & PAM_DELETE_CRED)) {
-               return PAM_SUCCESS;
+               retval = PAM_SUCCESS;
+               goto out;
        }
 
        if (ctrl & WINBIND_KRB5_AUTH) {
@@ -1034,8 +1093,6 @@ int pam_sm_close_session(pam_handle_t *pamh, int flags,
                const char *ccname = NULL;
                struct passwd *pwd = NULL;
 
-               int retval;
-
                ZERO_STRUCT(request);
                ZERO_STRUCT(response);
 
@@ -1043,37 +1100,47 @@ int pam_sm_close_session(pam_handle_t *pamh, int flags,
                if (retval == PAM_SUCCESS) {
                        if (user == NULL) {
                                _pam_log(LOG_ERR, "username was NULL!");
-                               return PAM_USER_UNKNOWN;
+                               retval = PAM_USER_UNKNOWN;
+                               goto out;
                        }
                        if (retval == PAM_SUCCESS) {
                                _pam_log_debug(ctrl, LOG_DEBUG, "username [%s] obtained", user);
                        }
                } else {
                        _pam_log_debug(ctrl, LOG_DEBUG, "could not identify user");
-                       return retval;
+                       goto out;
                }
 
                ccname = pam_getenv(pamh, "KRB5CCNAME");
                if (ccname == NULL) {
                        _pam_log_debug(ctrl, LOG_DEBUG, "user has no KRB5CCNAME environment");
-                       return PAM_BUF_ERR;
+                       retval = PAM_SUCCESS;
+                       goto out;
                }
 
-               fstrcpy(request.data.logoff.user, user);
-               fstrcpy(request.data.logoff.krb5ccname, ccname);
+               strncpy(request.data.logoff.user, user,
+                       sizeof(request.data.logoff.user) - 1);
+
+               strncpy(request.data.logoff.krb5ccname, ccname,
+                       sizeof(request.data.logoff.krb5ccname) - 1);
 
                pwd = getpwnam(user);
                if (pwd == NULL) {
-                       return PAM_USER_UNKNOWN;
+                       retval = PAM_USER_UNKNOWN;
+                       goto out;
                }
                request.data.logoff.uid = pwd->pw_uid;
 
-               request.flags = WBFLAG_PAM_KRB5 | WBFLAG_PAM_CONTACT_TRUSTDOM;
+               request.flags = WBFLAG_PAM_KRB5;
 
-               return pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
+               retval = pam_winbind_request_log(pamh, ctrl, WINBINDD_PAM_LOGOFF, &request, &response, user);
        }
-       
-       return PAM_SUCCESS;
+
+out:
+       if (d) {
+               iniparser_freedict(d);
+       }
+       return retval;
 }
 
 
@@ -1091,16 +1158,21 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
        char *pass_old, *pass_new;
        /* </DO NOT free() THESE> */
 
-       fstring Announce;
+       char *Announce;
        
        int retry = 0;
+       dictionary *d;
 
-       ctrl = _pam_parse(argc, argv);
+       ctrl = _pam_parse(argc, argv, &d);
        if (ctrl == -1) {
-               return PAM_SYSTEM_ERR;
+               retval = PAM_SYSTEM_ERR;
+               goto out;
        }
 
-       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_chauthtok");
+       _pam_log_debug(ctrl, LOG_DEBUG,"pam_winbind: pam_sm_chauthtok (flags: 0x%04x)", flags);
+
+       /* clearing offline bit for the auth in the password change */
+       ctrl &= ~WINBIND_CACHED_LOGIN;
 
        /*
         * First get the name of a user
@@ -1109,7 +1181,8 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
        if (retval == PAM_SUCCESS) {
                if (user == NULL) {
                        _pam_log(LOG_ERR, "username was NULL!");
-                       return PAM_USER_UNKNOWN;
+                       retval = PAM_USER_UNKNOWN;
+                       goto out;
                }
                if (retval == PAM_SUCCESS) {
                        _pam_log_debug(ctrl, LOG_DEBUG, "username [%s] obtained",
@@ -1118,9 +1191,22 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
        } else {
                _pam_log_debug(ctrl, LOG_DEBUG,
                         "password - could not identify user");
-               return retval;
+               goto out;
        }
 
+       /* check if this is really a user in winbindd, not only in NSS */
+       retval = valid_user(user, pamh, ctrl);
+       switch (retval) {
+               case 1:
+                       retval = PAM_USER_UNKNOWN;
+                       goto out;
+               case -1:
+                       retval = PAM_SYSTEM_ERR;
+                       goto out;
+               default:
+                       break;
+       }
+               
        /*
         * obtain and verify the current password (OLDAUTHTOK) for
         * the user.
@@ -1128,10 +1214,18 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
 
        if (flags & PAM_PRELIM_CHECK) {
                
+               time_t pwdlastset_prelim = 0;
+               
                /* instruct user what is happening */
 #define greeting "Changing password for "
-               fstrcpy(Announce, greeting);
-               fstrcat(Announce, user);
+               Announce = (char *) malloc(sizeof(greeting) + strlen(user));
+               if (Announce == NULL) {
+                       _pam_log(LOG_CRIT, "password - out of memory");
+                       retval = PAM_BUF_ERR;
+                       goto out;
+               }
+               (void) strcpy(Announce, greeting);
+               (void) strcpy(Announce + sizeof(greeting) - 1, user);
 #undef greeting
                
                lctrl = ctrl | WINBIND__OLD_PASSWORD;
@@ -1142,20 +1236,22 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
                                                (const char **) &pass_old);
                if (retval != PAM_SUCCESS) {
                        _pam_log(LOG_NOTICE, "password - (old) token not obtained");
-                       return retval;
+                       goto out;
                }
                /* verify that this is the password for this user */
                
-               retval = winbind_auth_request(pamh, ctrl, user, pass_old, NULL, NULL, False);
+               retval = winbind_auth_request(pamh, ctrl, user, pass_old, NULL, NULL, False, &pwdlastset_prelim);
 
-               if (retval != PAM_ACCT_EXPIRED 
-                   && retval != PAM_AUTHTOK_EXPIRED
-                   && retval != PAM_NEW_AUTHTOK_REQD 
-                   && retval != PAM_SUCCESS) {
+               if (retval != PAM_ACCT_EXPIRED && 
+                   retval != PAM_AUTHTOK_EXPIRED &&
+                   retval != PAM_NEW_AUTHTOK_REQD &&
+                   retval != PAM_SUCCESS) {
                        pass_old = NULL;
-                       return retval;
+                       goto out;
                }
                
+               pam_set_data(pamh, PAM_WINBIND_PWD_LAST_SET, (void *)pwdlastset_prelim, _pam_winbind_cleanup_func);
+
                retval = pam_set_item(pamh, PAM_OLDAUTHTOK, (const void *) pass_old);
                pass_old = NULL;
                if (retval != PAM_SUCCESS) {
@@ -1163,6 +1259,8 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
                }
        } else if (flags & PAM_UPDATE_AUTHTOK) {
        
+               time_t pwdlastset_update = 0;
+               
                /*
                 * obtain the proposed password
                 */
@@ -1171,12 +1269,11 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
                 * get the old token back. 
                 */
                
-               retval = pam_get_item(pamh, PAM_OLDAUTHTOK,
-                                     (const void **) &pass_old);
+               retval = _pam_get_item(pamh, PAM_OLDAUTHTOK, &pass_old);
                
                if (retval != PAM_SUCCESS) {
                        _pam_log(LOG_NOTICE, "user not authenticated");
-                       return retval;
+                       goto out;
                }
                
                lctrl = ctrl;
@@ -1202,7 +1299,7 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
                                _pam_log_debug(ctrl, LOG_ALERT
                                         ,"password - new password not obtained");
                                pass_old = NULL;/* tidy up */
-                               return retval;
+                               goto out;
                        }
 
                        /*
@@ -1220,23 +1317,25 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
                 * By reaching here we have approved the passwords and must now
                 * rebuild the password database file.
                 */
+               _pam_get_data( pamh, PAM_WINBIND_PWD_LAST_SET,
+                              &pwdlastset_update);
 
-               retval = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new);
+               retval = winbind_chauthtok_request(pamh, ctrl, user, pass_old, pass_new, pwdlastset_update);
                if (retval) {
                        _pam_overwrite(pass_new);
                        _pam_overwrite(pass_old);
                        pass_old = pass_new = NULL;
-                       return retval;
+                       goto out;
                }
 
                /* just in case we need krb5 creds after a password change over msrpc */
 
                if (ctrl & WINBIND_KRB5_AUTH) {
 
-                       const char *member = get_member_from_config(argc, argv, ctrl);
-                       const char *cctype = get_krb5_cc_type_from_config(argc, argv, ctrl);
+                       const char *member = get_member_from_config(argc, argv, ctrl, d);
+                       const char *cctype = get_krb5_cc_type_from_config(argc, argv, ctrl, d);
 
-                       retval = winbind_auth_request(pamh, ctrl, user, pass_new, member, cctype, False);
+                       retval = winbind_auth_request(pamh, ctrl, user, pass_new, member, cctype, False, NULL);
                        _pam_overwrite(pass_new);
                        _pam_overwrite(pass_old);
                        pass_old = pass_new = NULL;
@@ -1245,6 +1344,10 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
                retval = PAM_SERVICE_ERR;
        }
 
+out:
+       if (d) {
+               iniparser_freedict(d);
+       }
        return retval;
 }