libwbclient: Fix two signed/unsigned hickups
[nivanova/samba-autobuild/.git] / nsswitch / libwbclient / wbc_pam.c
index e243538b818d31f66367081a88a77eff74b0e248..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)
@@ -254,6 +259,7 @@ static wbcErr wbc_create_error_info(const struct winbindd_response *resp,
 
        e->nt_status = resp->data.auth.nt_status;
        e->pam_error = resp->data.auth.pam_error;
+       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);
 
@@ -342,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;
@@ -388,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);
 
@@ -518,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) {
@@ -547,9 +556,16 @@ done:
        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;
@@ -565,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) {
@@ -583,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;
@@ -601,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,
@@ -619,10 +641,22 @@ 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);
@@ -632,8 +666,8 @@ wbcErr wbcPingDc(const char *domain, struct wbcAuthErrorInfo **error)
  * Trigger a no-op NETLOGON call. Lightweight version of
  * wbcCheckTrustCredentials, optionally return attempted DC
  */
-wbcErr wbcPingDc2(const char *domain, struct wbcAuthErrorInfo **error,
-                 char **dcname)
+wbcErr wbcCtxPingDc2(struct wbcContext *ctx, const char *domain,
+                    struct wbcAuthErrorInfo **error, char **dcname)
 {
        struct winbindd_request request;
        struct winbindd_response response;
@@ -649,7 +683,7 @@ wbcErr wbcPingDc2(const char *domain, struct wbcAuthErrorInfo **error,
 
        /* Send request */
 
-       wbc_status = wbcRequestResponse(WINBINDD_PING_DC,
+       wbc_status = wbcRequestResponse(ctx, WINBINDD_PING_DC,
                                        &request,
                                        &response);
 
@@ -679,14 +713,21 @@ wbcErr wbcPingDc2(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 */
 
@@ -744,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);
 
@@ -765,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;
@@ -795,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);
 
@@ -805,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;
@@ -968,7 +1023,7 @@ wbcErr wbcChangeUserPasswordEx(const struct wbcChangePasswordParams *params,
 
        /* Send request */
 
-       wbc_status = wbcRequestResponse(cmd,
+       wbc_status = wbcRequestResponse(ctx, cmd,
                                        &request,
                                        &response);
        if (WBC_ERROR_IS_OK(wbc_status)) {
@@ -1003,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;
@@ -1018,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;
@@ -1134,7 +1208,7 @@ wbcErr wbcLogonUser(const struct wbcLogonUserParams *params,
                }
        }
 
-       wbc_status = wbcRequestResponse(WINBINDD_PAM_AUTH,
+       wbc_status = wbcRequestResponse(ctx, WINBINDD_PAM_AUTH,
                                        &request,
                                        &response);
 
@@ -1168,6 +1242,14 @@ 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 =
@@ -1176,9 +1258,10 @@ static void wbcCredentialCacheInfoDestructor(void *ptr)
 }
 
 /* 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;
@@ -1186,7 +1269,7 @@ 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);
@@ -1204,7 +1287,17 @@ wbcErr wbcCredentialCache(struct wbcCredentialCacheParams *params,
        }
 
        for (i=0; i<params->num_blobs; i++) {
-               if (strcasecmp(params->blobs[i].name, "initial_blob") == 0) {
+               /*
+                * 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;
@@ -1223,7 +1316,8 @@ wbcErr wbcCredentialCache(struct wbcCredentialCacheParams *params,
        }
 
        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;
                }
@@ -1272,8 +1366,8 @@ 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;
        }
@@ -1301,6 +1395,15 @@ 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;
@@ -1312,8 +1415,16 @@ fail:
        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;
@@ -1327,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);
 }