libwbclient: Fix two signed/unsigned hickups
[nivanova/samba-autobuild/.git] / nsswitch / libwbclient / wbc_pam.c
index 892ccb7aa2e7bcc89ab5ac90a957fa34a71caff8..c31220ae7eb67a922ec01eb79fcad31d9429e1fd 100644 (file)
@@ -28,8 +28,8 @@
 #include "../winbind_client.h"
 
 /* Authenticate a username/password pair */
-wbcErr wbcAuthenticateUser(const char *username,
-                          const char *password)
+wbcErr wbcCtxAuthenticateUser(struct wbcContext *ctx,
+                             const char *username, const char *password)
 {
        wbcErr wbc_status = WBC_ERR_SUCCESS;
        struct wbcAuthUserParams params;
@@ -40,13 +40,18 @@ wbcErr wbcAuthenticateUser(const char *username,
        params.level                    = WBC_AUTH_USER_LEVEL_PLAIN;
        params.password.plaintext       = password;
 
-       wbc_status = wbcAuthenticateUserEx(&params, NULL, NULL);
+       wbc_status = wbcCtxAuthenticateUserEx(ctx, &params, NULL, NULL);
        BAIL_ON_WBC_ERROR(wbc_status);
 
 done:
        return wbc_status;
 }
 
+wbcErr wbcAuthenticateUser(const char *username, const char *password)
+{
+       return wbcCtxAuthenticateUser(NULL, username, password);
+}
+
 static bool sid_attr_compose(struct wbcSidWithAttr *s,
                             const struct wbcDomainSid *d,
                             uint32_t rid, uint32_t attr)
@@ -87,7 +92,7 @@ static wbcErr wbc_create_auth_info(const struct winbindd_response *resp,
        uint32_t j;
 
        i = (struct wbcAuthUserInfo *)wbcAllocateMemory(
-               sizeof(struct wbcAuthUserInfo), 1,
+               1, sizeof(struct wbcAuthUserInfo),
                wbcAuthUserInfoDestructor);
        BAIL_ON_PTR_ERROR(i, wbc_status);
 
@@ -234,28 +239,38 @@ done:
        return wbc_status;
 }
 
+static void wbcAuthErrorInfoDestructor(void *ptr)
+{
+       struct wbcAuthErrorInfo *e = (struct wbcAuthErrorInfo *)ptr;
+       free(e->nt_string);
+       free(e->display_string);
+}
+
 static wbcErr wbc_create_error_info(const struct winbindd_response *resp,
                                    struct wbcAuthErrorInfo **_e)
 {
        wbcErr wbc_status = WBC_ERR_SUCCESS;
        struct wbcAuthErrorInfo *e;
 
-       e = talloc(NULL, struct wbcAuthErrorInfo);
+       e = (struct wbcAuthErrorInfo *)wbcAllocateMemory(
+               1, sizeof(struct wbcAuthErrorInfo),
+               wbcAuthErrorInfoDestructor);
        BAIL_ON_PTR_ERROR(e, wbc_status);
 
        e->nt_status = resp->data.auth.nt_status;
        e->pam_error = resp->data.auth.pam_error;
-       e->nt_string = talloc_strdup(e, resp->data.auth.nt_status_string);
+       e->authoritative = resp->data.auth.authoritative;
+       e->nt_string = strdup(resp->data.auth.nt_status_string);
        BAIL_ON_PTR_ERROR(e->nt_string, wbc_status);
 
-       e->display_string = talloc_strdup(e, resp->data.auth.error_string);
+       e->display_string = strdup(resp->data.auth.error_string);
        BAIL_ON_PTR_ERROR(e->display_string, wbc_status);
 
        *_e = e;
        e = NULL;
 
 done:
-       talloc_free(e);
+       wbcFreeMemory(e);
        return wbc_status;
 }
 
@@ -266,7 +281,7 @@ static wbcErr wbc_create_password_policy_info(const struct winbindd_response *re
        struct wbcUserPasswordPolicyInfo *i;
 
        i = (struct wbcUserPasswordPolicyInfo *)wbcAllocateMemory(
-               sizeof(struct wbcUserPasswordPolicyInfo), 1, NULL);
+               1, sizeof(struct wbcUserPasswordPolicyInfo), NULL);
        BAIL_ON_PTR_ERROR(i, wbc_status);
 
        i->min_passwordage      = resp->data.auth.policy.min_passwordage;
@@ -297,15 +312,14 @@ static wbcErr wbc_create_logon_info(struct winbindd_response *resp,
        struct wbcLogonUserInfo *i;
 
        i = (struct wbcLogonUserInfo *)wbcAllocateMemory(
-               sizeof(struct wbcLogonUserInfo), 1,
+               1, sizeof(struct wbcLogonUserInfo),
                wbcLogonUserInfoDestructor);
        BAIL_ON_PTR_ERROR(i, wbc_status);
 
        wbc_status = wbc_create_auth_info(resp, &i->info);
        BAIL_ON_WBC_ERROR(wbc_status);
 
-       if (resp->data.auth.krb5ccname &&
-           strlen(resp->data.auth.krb5ccname)) {
+       if (resp->data.auth.krb5ccname[0] != '\0') {
                wbc_status = wbcAddNamedBlob(&i->num_blobs,
                                             &i->blobs,
                                             "krb5ccname",
@@ -315,8 +329,7 @@ static wbcErr wbc_create_logon_info(struct winbindd_response *resp,
                BAIL_ON_WBC_ERROR(wbc_status);
        }
 
-       if (resp->data.auth.unix_username &&
-           strlen(resp->data.auth.unix_username)) {
+       if (resp->data.auth.unix_username[0] != '\0') {
                wbc_status = wbcAddNamedBlob(&i->num_blobs,
                                             &i->blobs,
                                             "unix_username",
@@ -335,9 +348,10 @@ done:
 
 
 /* Authenticate with more detailed information */
-wbcErr wbcAuthenticateUserEx(const struct wbcAuthUserParams *params,
-                            struct wbcAuthUserInfo **info,
-                            struct wbcAuthErrorInfo **error)
+wbcErr wbcCtxAuthenticateUserEx(struct wbcContext *ctx,
+                               const struct wbcAuthUserParams *params,
+                               struct wbcAuthUserInfo **info,
+                               struct wbcAuthErrorInfo **error)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        int cmd = 0;
@@ -356,7 +370,7 @@ wbcErr wbcAuthenticateUserEx(const struct wbcAuthUserParams *params,
                BAIL_ON_WBC_ERROR(wbc_status);
        }
 
-       if (!params->account_name) {
+       if (params->level != WBC_AUTH_USER_LEVEL_PAC && !params->account_name) {
                wbc_status = WBC_ERR_INVALID_PARAM;
                BAIL_ON_WBC_ERROR(wbc_status);
        }
@@ -381,7 +395,7 @@ wbcErr wbcAuthenticateUserEx(const struct wbcAuthUserParams *params,
 
                        ZERO_STRUCT(sep_response);
 
-                       wbc_status = wbcRequestResponse(WINBINDD_INFO,
+                       wbc_status = wbcRequestResponse(ctx, WINBINDD_INFO,
                                                        NULL, &sep_response);
                        BAIL_ON_WBC_ERROR(wbc_status);
 
@@ -468,7 +482,8 @@ wbcErr wbcAuthenticateUserEx(const struct wbcAuthUserParams *params,
                if (params->password.response.nt_length > sizeof(request.data.auth_crap.nt_resp)) {
                        request.flags |= WBFLAG_BIG_NTLMV2_BLOB;
                        request.extra_len = params->password.response.nt_length;
-                       request.extra_data.data = talloc_zero_array(NULL, char, request.extra_len);
+                       request.extra_data.data = (char *)malloc(
+                               request.extra_len);
                        if (request.extra_data.data == NULL) {
                                wbc_status = WBC_ERR_NO_MEMORY;
                                BAIL_ON_WBC_ERROR(wbc_status);
@@ -482,6 +497,20 @@ wbcErr wbcAuthenticateUserEx(const struct wbcAuthUserParams *params,
                               request.data.auth_crap.nt_resp_len);
                }
                break;
+
+       case WBC_AUTH_USER_LEVEL_PAC:
+               cmd = WINBINDD_PAM_AUTH_CRAP;
+               request.flags = WBFLAG_PAM_AUTH_PAC | WBFLAG_PAM_INFO3_TEXT;
+               request.extra_data.data = malloc(params->password.pac.length);
+               if (request.extra_data.data == NULL) {
+                       wbc_status = WBC_ERR_NO_MEMORY;
+                       BAIL_ON_WBC_ERROR(wbc_status);
+               }
+               memcpy(request.extra_data.data, params->password.pac.data,
+                      params->password.pac.length);
+               request.extra_len = params->password.pac.length;
+               break;
+
        default:
                break;
        }
@@ -496,9 +525,11 @@ wbcErr wbcAuthenticateUserEx(const struct wbcAuthUserParams *params,
        }
 
        if (cmd == WINBINDD_PAM_AUTH_CRAP) {
-               wbc_status = wbcRequestResponsePriv(cmd, &request, &response);
+               wbc_status = wbcRequestResponsePriv(ctx, cmd,
+                                                   &request, &response);
        } else {
-               wbc_status = wbcRequestResponse(cmd, &request, &response);
+               wbc_status = wbcRequestResponse(ctx, cmd,
+                                               &request, &response);
        }
        if (response.data.auth.nt_status != 0) {
                if (error) {
@@ -520,14 +551,21 @@ wbcErr wbcAuthenticateUserEx(const struct wbcAuthUserParams *params,
 done:
        winbindd_free_response(&response);
 
-       talloc_free(request.extra_data.data);
+       free(request.extra_data.data);
 
        return wbc_status;
 }
 
+wbcErr wbcAuthenticateUserEx(const struct wbcAuthUserParams *params,
+                            struct wbcAuthUserInfo **info,
+                            struct wbcAuthErrorInfo **error)
+{
+       return wbcCtxAuthenticateUserEx(NULL, params, info, error);
+}
+
 /* Trigger a verification of the trust credentials of a specific domain */
-wbcErr wbcCheckTrustCredentials(const char *domain,
-                               struct wbcAuthErrorInfo **error)
+wbcErr wbcCtxCheckTrustCredentials(struct wbcContext *ctx, const char *domain,
+                                  struct wbcAuthErrorInfo **error)
 {
        struct winbindd_request request;
        struct winbindd_response response;
@@ -543,7 +581,7 @@ wbcErr wbcCheckTrustCredentials(const char *domain,
 
        /* Send request */
 
-       wbc_status = wbcRequestResponsePriv(WINBINDD_CHECK_MACHACC,
+       wbc_status = wbcRequestResponsePriv(ctx, WINBINDD_CHECK_MACHACC,
                                            &request, &response);
        if (response.data.auth.nt_status != 0) {
                if (error) {
@@ -561,9 +599,15 @@ wbcErr wbcCheckTrustCredentials(const char *domain,
        return wbc_status;
 }
 
+wbcErr wbcCheckTrustCredentials(const char *domain,
+                               struct wbcAuthErrorInfo **error)
+{
+       return wbcCtxCheckTrustCredentials(NULL, domain, error);
+}
+
 /* Trigger a change of the trust credentials for a specific domain */
-wbcErr wbcChangeTrustCredentials(const char *domain,
-                                struct wbcAuthErrorInfo **error)
+wbcErr wbcCtxChangeTrustCredentials(struct wbcContext *ctx, const char *domain,
+                                   struct wbcAuthErrorInfo **error)
 {
        struct winbindd_request request;
        struct winbindd_response response;
@@ -579,8 +623,8 @@ wbcErr wbcChangeTrustCredentials(const char *domain,
 
        /* Send request */
 
-       wbc_status = wbcRequestResponsePriv(WINBINDD_CHANGE_MACHACC,
-                                       &request, &response);
+       wbc_status = wbcRequestResponsePriv(ctx, WINBINDD_CHANGE_MACHACC,
+                                           &request, &response);
        if (response.data.auth.nt_status != 0) {
                if (error) {
                        wbc_status = wbc_create_error_info(&response,
@@ -597,33 +641,62 @@ wbcErr wbcChangeTrustCredentials(const char *domain,
        return wbc_status;
 }
 
+wbcErr wbcChangeTrustCredentials(const char *domain,
+                                struct wbcAuthErrorInfo **error)
+{
+       return wbcCtxChangeTrustCredentials(NULL, domain, error);
+}
+
 /*
  * Trigger a no-op NETLOGON call. Lightweight version of
  * wbcCheckTrustCredentials
  */
+wbcErr wbcCtxPingDc(struct wbcContext *ctx, const char *domain,
+                   struct wbcAuthErrorInfo **error)
+{
+       return wbcCtxPingDc2(ctx, domain, error, NULL);
+}
+
 wbcErr wbcPingDc(const char *domain, struct wbcAuthErrorInfo **error)
+{
+       return wbcPingDc2(domain, error, NULL);
+}
+
+/*
+ * Trigger a no-op NETLOGON call. Lightweight version of
+ * wbcCheckTrustCredentials, optionally return attempted DC
+ */
+wbcErr wbcCtxPingDc2(struct wbcContext *ctx, const char *domain,
+                    struct wbcAuthErrorInfo **error, char **dcname)
 {
        struct winbindd_request request;
        struct winbindd_response response;
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
 
-       if (domain) {
-               /*
-                * the current protocol doesn't support
-                * specifying a domain
-                */
-               wbc_status = WBC_ERR_NOT_IMPLEMENTED;
-               BAIL_ON_WBC_ERROR(wbc_status);
-       }
-
        ZERO_STRUCT(request);
        ZERO_STRUCT(response);
 
+       if (domain) {
+               strncpy(request.domain_name, domain,
+                       sizeof(request.domain_name)-1);
+       }
+
        /* Send request */
 
-       wbc_status = wbcRequestResponse(WINBINDD_PING_DC,
+       wbc_status = wbcRequestResponse(ctx, WINBINDD_PING_DC,
                                        &request,
                                        &response);
+
+       if (dcname && response.extra_data.data) {
+               size_t len;
+
+               len = response.length - sizeof(struct winbindd_response);
+               *dcname = wbcAllocateMemory(1, len, NULL);
+               BAIL_ON_PTR_ERROR(*dcname, wbc_status);
+
+               strlcpy(*dcname, response.extra_data.data, len);
+       }
+
        if (response.data.auth.nt_status != 0) {
                if (error) {
                        wbc_status = wbc_create_error_info(&response,
@@ -640,14 +713,21 @@ wbcErr wbcPingDc(const char *domain, struct wbcAuthErrorInfo **error)
        return wbc_status;
 }
 
+wbcErr wbcPingDc2(const char *domain, struct wbcAuthErrorInfo **error,
+                 char **dcname)
+{
+       return wbcCtxPingDc2(NULL, domain, error, dcname);
+}
+
 /* Trigger an extended logoff notification to Winbind for a specific user */
-wbcErr wbcLogoffUserEx(const struct wbcLogoffUserParams *params,
-                      struct wbcAuthErrorInfo **error)
+wbcErr wbcCtxLogoffUserEx(struct wbcContext *ctx,
+                         const struct wbcLogoffUserParams *params,
+                         struct wbcAuthErrorInfo **error)
 {
        struct winbindd_request request;
        struct winbindd_response response;
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
-       int i;
+       size_t i;
 
        /* validate input */
 
@@ -705,7 +785,7 @@ wbcErr wbcLogoffUserEx(const struct wbcLogoffUserParams *params,
 
        /* Send request */
 
-       wbc_status = wbcRequestResponse(WINBINDD_PAM_LOGOFF,
+       wbc_status = wbcRequestResponse(ctx, WINBINDD_PAM_LOGOFF,
                                        &request,
                                        &response);
 
@@ -726,10 +806,16 @@ wbcErr wbcLogoffUserEx(const struct wbcLogoffUserParams *params,
        return wbc_status;
 }
 
+wbcErr wbcLogoffUserEx(const struct wbcLogoffUserParams *params,
+                      struct wbcAuthErrorInfo **error)
+{
+       return wbcCtxLogoffUserEx(NULL, params, error);
+}
+
 /* Trigger a logoff notification to Winbind for a specific user */
-wbcErr wbcLogoffUser(const char *username,
-                    uid_t uid,
-                    const char *ccfilename)
+wbcErr wbcCtxLogoffUser(struct wbcContext *ctx,
+                       const char *username, uid_t uid,
+                       const char *ccfilename)
 {
        struct winbindd_request request;
        struct winbindd_response response;
@@ -756,7 +842,7 @@ wbcErr wbcLogoffUser(const char *username,
 
        /* Send request */
 
-       wbc_status = wbcRequestResponse(WINBINDD_PAM_LOGOFF,
+       wbc_status = wbcRequestResponse(ctx, WINBINDD_PAM_LOGOFF,
                                        &request,
                                        &response);
 
@@ -766,11 +852,19 @@ wbcErr wbcLogoffUser(const char *username,
        return wbc_status;
 }
 
+wbcErr wbcLogoffUser(const char *username,
+                    uid_t uid,
+                    const char *ccfilename)
+{
+       return wbcCtxLogoffUser(NULL, username, uid, ccfilename);
+}
+
 /* Change a password for a user with more detailed information upon failure */
-wbcErr wbcChangeUserPasswordEx(const struct wbcChangePasswordParams *params,
-                              struct wbcAuthErrorInfo **error,
-                              enum wbcPasswordChangeRejectReason *reject_reason,
-                              struct wbcUserPasswordPolicyInfo **policy)
+wbcErr wbcCtxChangeUserPasswordEx(struct wbcContext *ctx,
+                       const struct wbcChangePasswordParams *params,
+                       struct wbcAuthErrorInfo **error,
+                       enum wbcPasswordChangeRejectReason *reject_reason,
+                       struct wbcUserPasswordPolicyInfo **policy)
 {
        struct winbindd_request request;
        struct winbindd_response response;
@@ -781,7 +875,7 @@ wbcErr wbcChangeUserPasswordEx(const struct wbcChangePasswordParams *params,
 
        if (!params->account_name) {
                wbc_status = WBC_ERR_INVALID_PARAM;
-               BAIL_ON_WBC_ERROR(wbc_status);
+               goto done;
        }
 
        if (error) {
@@ -805,7 +899,7 @@ wbcErr wbcChangeUserPasswordEx(const struct wbcChangePasswordParams *params,
 
                if (!params->account_name) {
                        wbc_status = WBC_ERR_INVALID_PARAM;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                strncpy(request.data.chauthtok.user, params->account_name,
@@ -829,55 +923,55 @@ wbcErr wbcChangeUserPasswordEx(const struct wbcChangePasswordParams *params,
 
                if (!params->account_name || !params->domain_name) {
                        wbc_status = WBC_ERR_INVALID_PARAM;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                if (params->old_password.response.old_lm_hash_enc_length &&
                    !params->old_password.response.old_lm_hash_enc_data) {
                        wbc_status = WBC_ERR_INVALID_PARAM;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                if (params->old_password.response.old_lm_hash_enc_length == 0 &&
                    params->old_password.response.old_lm_hash_enc_data) {
                        wbc_status = WBC_ERR_INVALID_PARAM;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                if (params->old_password.response.old_nt_hash_enc_length &&
                    !params->old_password.response.old_nt_hash_enc_data) {
                        wbc_status = WBC_ERR_INVALID_PARAM;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                if (params->old_password.response.old_nt_hash_enc_length == 0 &&
                    params->old_password.response.old_nt_hash_enc_data) {
                        wbc_status = WBC_ERR_INVALID_PARAM;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                if (params->new_password.response.lm_length &&
                    !params->new_password.response.lm_data) {
                        wbc_status = WBC_ERR_INVALID_PARAM;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                if (params->new_password.response.lm_length == 0 &&
                    params->new_password.response.lm_data) {
                        wbc_status = WBC_ERR_INVALID_PARAM;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                if (params->new_password.response.nt_length &&
                    !params->new_password.response.nt_data) {
                        wbc_status = WBC_ERR_INVALID_PARAM;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                if (params->new_password.response.nt_length == 0 &&
                    params->new_password.response.nt_data) {
                        wbc_status = WBC_ERR_INVALID_PARAM;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                strncpy(request.data.chng_pswd_auth_crap.user,
@@ -889,47 +983,47 @@ wbcErr wbcChangeUserPasswordEx(const struct wbcChangePasswordParams *params,
                        sizeof(request.data.chng_pswd_auth_crap.domain) - 1);
 
                if (params->new_password.response.nt_data) {
+                       request.data.chng_pswd_auth_crap.new_nt_pswd_len =
+                               params->new_password.response.nt_length;
                        memcpy(request.data.chng_pswd_auth_crap.new_nt_pswd,
                               params->new_password.response.nt_data,
                               request.data.chng_pswd_auth_crap.new_nt_pswd_len);
-                       request.data.chng_pswd_auth_crap.new_nt_pswd_len =
-                               params->new_password.response.nt_length;
                }
 
                if (params->new_password.response.lm_data) {
+                       request.data.chng_pswd_auth_crap.new_lm_pswd_len =
+                               params->new_password.response.lm_length;
                        memcpy(request.data.chng_pswd_auth_crap.new_lm_pswd,
                               params->new_password.response.lm_data,
                               request.data.chng_pswd_auth_crap.new_lm_pswd_len);
-                       request.data.chng_pswd_auth_crap.new_lm_pswd_len =
-                               params->new_password.response.lm_length;
                }
 
                if (params->old_password.response.old_nt_hash_enc_data) {
+                       request.data.chng_pswd_auth_crap.old_nt_hash_enc_len =
+                               params->old_password.response.old_nt_hash_enc_length;
                        memcpy(request.data.chng_pswd_auth_crap.old_nt_hash_enc,
                               params->old_password.response.old_nt_hash_enc_data,
                               request.data.chng_pswd_auth_crap.old_nt_hash_enc_len);
-                       request.data.chng_pswd_auth_crap.old_nt_hash_enc_len =
-                               params->old_password.response.old_nt_hash_enc_length;
                }
 
                if (params->old_password.response.old_lm_hash_enc_data) {
+                       request.data.chng_pswd_auth_crap.old_lm_hash_enc_len =
+                               params->old_password.response.old_lm_hash_enc_length;
                        memcpy(request.data.chng_pswd_auth_crap.old_lm_hash_enc,
                               params->old_password.response.old_lm_hash_enc_data,
                               request.data.chng_pswd_auth_crap.old_lm_hash_enc_len);
-                       request.data.chng_pswd_auth_crap.old_lm_hash_enc_len =
-                               params->old_password.response.old_lm_hash_enc_length;
                }
 
                break;
        default:
                wbc_status = WBC_ERR_INVALID_PARAM;
-               BAIL_ON_WBC_ERROR(wbc_status);
+               goto done;
                break;
        }
 
        /* Send request */
 
-       wbc_status = wbcRequestResponse(cmd,
+       wbc_status = wbcRequestResponse(ctx, cmd,
                                        &request,
                                        &response);
        if (WBC_ERROR_IS_OK(wbc_status)) {
@@ -964,10 +1058,20 @@ wbcErr wbcChangeUserPasswordEx(const struct wbcChangePasswordParams *params,
        return wbc_status;
 }
 
+wbcErr wbcChangeUserPasswordEx(const struct wbcChangePasswordParams *params,
+                             struct wbcAuthErrorInfo **error,
+                             enum wbcPasswordChangeRejectReason *reject_reason,
+                             struct wbcUserPasswordPolicyInfo **policy)
+{
+       return wbcCtxChangeUserPasswordEx(NULL, params, error,
+                                         reject_reason, policy);
+}
+
 /* Change a password for a user */
-wbcErr wbcChangeUserPassword(const char *username,
-                            const char *old_password,
-                            const char *new_password)
+wbcErr wbcCtxChangeUserPassword(struct wbcContext *ctx,
+                               const char *username,
+                               const char *old_password,
+                               const char *new_password)
 {
        wbcErr wbc_status = WBC_ERR_SUCCESS;
        struct wbcChangePasswordParams params;
@@ -979,21 +1083,30 @@ wbcErr wbcChangeUserPassword(const char *username,
        params.old_password.plaintext   = old_password;
        params.new_password.plaintext   = new_password;
 
-       wbc_status = wbcChangeUserPasswordEx(&params,
-                                            NULL,
-                                            NULL,
-                                            NULL);
+       wbc_status = wbcCtxChangeUserPasswordEx(ctx, &params,
+                                               NULL,
+                                               NULL,
+                                               NULL);
        BAIL_ON_WBC_ERROR(wbc_status);
 
 done:
        return wbc_status;
 }
 
+wbcErr wbcChangeUserPassword(const char *username,
+                            const char *old_password,
+                            const char *new_password)
+{
+       return wbcCtxChangeUserPassword(NULL, username,
+                                       old_password, new_password);
+}
+
 /* Logon a User */
-wbcErr wbcLogonUser(const struct wbcLogonUserParams *params,
-                   struct wbcLogonUserInfo **info,
-                   struct wbcAuthErrorInfo **error,
-                   struct wbcUserPasswordPolicyInfo **policy)
+wbcErr wbcCtxLogonUser(struct wbcContext *ctx,
+                      const struct wbcLogonUserParams *params,
+                      struct wbcLogonUserInfo **info,
+                      struct wbcAuthErrorInfo **error,
+                      struct wbcUserPasswordPolicyInfo **policy)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        struct winbindd_request request;
@@ -1095,7 +1208,7 @@ wbcErr wbcLogonUser(const struct wbcLogonUserParams *params,
                }
        }
 
-       wbc_status = wbcRequestResponse(WINBINDD_PAM_AUTH,
+       wbc_status = wbcRequestResponse(ctx, WINBINDD_PAM_AUTH,
                                        &request,
                                        &response);
 
@@ -1129,10 +1242,26 @@ done:
        return wbc_status;
 }
 
+wbcErr wbcLogonUser(const struct wbcLogonUserParams *params,
+                   struct wbcLogonUserInfo **info,
+                   struct wbcAuthErrorInfo **error,
+                   struct wbcUserPasswordPolicyInfo **policy)
+{
+       return wbcCtxLogonUser(NULL, params, info, error, policy);
+}
+
+static void wbcCredentialCacheInfoDestructor(void *ptr)
+{
+       struct wbcCredentialCacheInfo *i =
+               (struct wbcCredentialCacheInfo *)ptr;
+       wbcFreeMemory(i->blobs);
+}
+
 /* Authenticate a user with cached credentials */
-wbcErr wbcCredentialCache(struct wbcCredentialCacheParams *params,
-                          struct wbcCredentialCacheInfo **info,
-                          struct wbcAuthErrorInfo **error)
+wbcErr wbcCtxCredentialCache(struct wbcContext *ctx,
+                            struct wbcCredentialCacheParams *params,
+                             struct wbcCredentialCacheInfo **info,
+                             struct wbcAuthErrorInfo **error)
 {
        wbcErr status = WBC_ERR_UNKNOWN_FAILURE;
        struct wbcCredentialCacheInfo *result = NULL;
@@ -1140,14 +1269,13 @@ wbcErr wbcCredentialCache(struct wbcCredentialCacheParams *params,
        struct winbindd_response response;
        struct wbcNamedBlob *initial_blob = NULL;
        struct wbcNamedBlob *challenge_blob = NULL;
-       int i;
+       size_t i;
 
        ZERO_STRUCT(request);
        ZERO_STRUCT(response);
 
-       if (info != NULL) {
-               *info = NULL;
-       }
+       *info = NULL;
+
        if (error != NULL) {
                *error = NULL;
        }
@@ -1158,8 +1286,38 @@ wbcErr wbcCredentialCache(struct wbcCredentialCacheParams *params,
                goto fail;
        }
 
+       for (i=0; i<params->num_blobs; i++) {
+               /*
+                * Older callers may used to provide the NEGOTIATE request
+                * as "initial_blob", but it was completely ignored by winbindd.
+                *
+                * So we keep ignoring it.
+                *
+                * A new callers that is capable to support "new_spnego",
+                * will provide the NEGOTIATE request as "negotiate_blob"
+                * instead.
+                */
+               if (strcasecmp(params->blobs[i].name, "negotiate_blob") == 0) {
+                       if (initial_blob != NULL) {
+                               status = WBC_ERR_INVALID_PARAM;
+                               goto fail;
+                       }
+                       initial_blob = &params->blobs[i];
+                       continue;
+               }
+               if (strcasecmp(params->blobs[i].name, "challenge_blob") == 0) {
+                       if (challenge_blob != NULL) {
+                               status = WBC_ERR_INVALID_PARAM;
+                               goto fail;
+                       }
+                       challenge_blob = &params->blobs[i];
+                       continue;
+               }
+       }
+
        if (params->domain_name != NULL) {
-               status = wbcRequestResponse(WINBINDD_INFO, NULL, &response);
+               status = wbcRequestResponse(ctx, WINBINDD_INFO,
+                                           NULL, &response);
                if (!WBC_ERROR_IS_OK(status)) {
                        goto fail;
                }
@@ -1175,17 +1333,6 @@ wbcErr wbcCredentialCache(struct wbcCredentialCacheParams *params,
        }
        request.data.ccache_ntlm_auth.uid = getuid();
 
-       for (i=0; i<params->num_blobs; i++) {
-               if (strcasecmp(params->blobs[i].name, "initial_blob") == 0) {
-                       initial_blob = &params->blobs[i];
-                       break;
-               }
-               if (strcasecmp(params->blobs[i].name, "challenge_blob") == 0) {
-                       challenge_blob = &params->blobs[i];
-                       break;
-               }
-       }
-
        request.data.ccache_ntlm_auth.initial_blob_len = 0;
        request.data.ccache_ntlm_auth.challenge_blob_len = 0;
        request.extra_len = 0;
@@ -1202,8 +1349,7 @@ wbcErr wbcCredentialCache(struct wbcCredentialCacheParams *params,
        }
 
        if (request.extra_len != 0) {
-               request.extra_data.data = talloc_array(
-                       NULL, char, request.extra_len);
+               request.extra_data.data = (char *)malloc(request.extra_len);
                if (request.extra_data.data == NULL) {
                        status = WBC_ERR_NO_MEMORY;
                        goto fail;
@@ -1220,23 +1366,21 @@ wbcErr wbcCredentialCache(struct wbcCredentialCacheParams *params,
                       challenge_blob->blob.length);
        }
 
-       status = wbcRequestResponse(WINBINDD_CCACHE_NTLMAUTH, &request,
-                                   &response);
+       status = wbcRequestResponse(ctx, WINBINDD_CCACHE_NTLMAUTH,
+                                   &request, &response);
        if (!WBC_ERROR_IS_OK(status)) {
                goto fail;
        }
 
-       result = talloc(NULL, struct wbcCredentialCacheInfo);
+       result = (struct wbcCredentialCacheInfo *)wbcAllocateMemory(
+               1, sizeof(struct wbcCredentialCacheInfo),
+               wbcCredentialCacheInfoDestructor);
        if (result == NULL) {
                status = WBC_ERR_NO_MEMORY;
                goto fail;
        }
        result->num_blobs = 0;
-       result->blobs = talloc(result, struct wbcNamedBlob);
-       if (result->blobs == NULL) {
-               status = WBC_ERR_NO_MEMORY;
-               goto fail;
-       }
+       result->blobs = NULL;
        status = wbcAddNamedBlob(&result->num_blobs, &result->blobs,
                                 "auth_blob", 0,
                                 (uint8_t *)response.extra_data.data,
@@ -1251,19 +1395,36 @@ wbcErr wbcCredentialCache(struct wbcCredentialCacheParams *params,
        if (!WBC_ERROR_IS_OK(status)) {
                goto fail;
        }
+       if (response.data.ccache_ntlm_auth.new_spnego) {
+               status = wbcAddNamedBlob(
+                       &result->num_blobs, &result->blobs, "new_spnego", 0,
+                       &response.data.ccache_ntlm_auth.new_spnego,
+                       sizeof(response.data.ccache_ntlm_auth.new_spnego));
+               if (!WBC_ERROR_IS_OK(status)) {
+                       goto fail;
+               }
+       }
 
        *info = result;
        result = NULL;
        status = WBC_ERR_SUCCESS;
 fail:
-       TALLOC_FREE(request.extra_data.data);
+       free(request.extra_data.data);
        winbindd_free_response(&response);
-       talloc_free(result);
+       wbcFreeMemory(result);
        return status;
 }
 
+wbcErr wbcCredentialCache(struct wbcCredentialCacheParams *params,
+                          struct wbcCredentialCacheInfo **info,
+                          struct wbcAuthErrorInfo **error)
+{
+       return wbcCtxCredentialCache(NULL, params, info, error);
+}
+
 /* Authenticate a user with cached credentials */
-wbcErr wbcCredentialSave(const char *user, const char *password)
+wbcErr wbcCtxCredentialSave(struct wbcContext *ctx,
+                           const char *user, const char *password)
 {
        struct winbindd_request request;
        struct winbindd_response response;
@@ -1277,5 +1438,10 @@ wbcErr wbcCredentialSave(const char *user, const char *password)
                sizeof(request.data.ccache_save.pass)-1);
        request.data.ccache_save.uid = getuid();
 
-       return wbcRequestResponse(WINBINDD_CCACHE_SAVE, &request, &response);
+       return wbcRequestResponse(ctx, WINBINDD_CCACHE_SAVE, &request, &response);
+}
+
+wbcErr wbcCredentialSave(const char *user, const char *password)
+{
+       return wbcCtxCredentialSave(NULL, user, password);
 }