s4-winbind: Add support for the WINBINDD_LIST_GROUPS command.
[gd/samba-autobuild/.git] / source4 / winbind / wb_samba3_cmd.c
index a32342b62b99bb7d5a659615e15e9bb12ffe1f71..2c273e517595f4bd82cbd2ebf1a296806ee21e21 100644 (file)
@@ -4,10 +4,12 @@
 
    Copyright (C) Stefan Metzmacher     2005
    Copyright (C) Volker Lendecke       2005
+   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
+   Copyright (C) Kai Blin              2009
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
-#include "smbd/service_stream.h"
-#include "nsswitch/winbind_nss_config.h"
-#include "nsswitch/winbindd_nss.h"
 #include "winbind/wb_server.h"
-#include "winbind/wb_samba3_protocol.h"
 #include "winbind/wb_async_helpers.h"
-#include "librpc/gen_ndr/nbt.h"
-#include "libcli/raw/libcliraw.h"
+#include "param/param.h"
+#include "winbind/wb_helper.h"
 #include "libcli/composite/composite.h"
-#include "libcli/smb_composite/smb_composite.h"
-#include "include/version.h"
-#include "librpc/rpc/dcerpc_composite.h"
-#include "lib/events/events.h"
+#include "version.h"
+#include "librpc/gen_ndr/netlogon.h"
+#include "libcli/security/security.h"
+#include "auth/ntlm/pam_errors.h"
+#include "auth/credentials/credentials.h"
+#include "smbd/service_task.h"
+
+/* 
+   Send off the reply to an async Samba3 query, handling filling in the PAM, NTSTATUS and string errors.
+*/
+
+static void wbsrv_samba3_async_auth_epilogue(NTSTATUS status,
+                                            struct wbsrv_samba3_call *s3call)
+{
+       struct winbindd_response *resp = &s3call->response;
+       if (!NT_STATUS_IS_OK(status)) {
+               resp->result = WINBINDD_ERROR;
+       } else {
+               resp->result = WINBINDD_OK;
+       }
+       
+       WBSRV_SAMBA3_SET_STRING(resp->data.auth.nt_status_string,
+                               nt_errstr(status));
+       WBSRV_SAMBA3_SET_STRING(resp->data.auth.error_string,
+                               get_friendly_nt_error_msg(status));
+
+       resp->data.auth.pam_error = nt_status_to_pam(status);
+       resp->data.auth.nt_status = NT_STATUS_V(status);
+
+       wbsrv_samba3_send_reply(s3call);
+}
+
+/* 
+   Send of a generic reply to a Samba3 query
+*/
+
+static void wbsrv_samba3_async_epilogue(NTSTATUS status,
+                                       struct wbsrv_samba3_call *s3call)
+{
+       struct winbindd_response *resp = &s3call->response;
+       if (NT_STATUS_IS_OK(status)) {
+               resp->result = WINBINDD_OK;
+       } else {
+               resp->result = WINBINDD_ERROR;
+       }
+
+       wbsrv_samba3_send_reply(s3call);
+}
+
+/* 
+   Boilerplate commands, simple queries without network traffic 
+*/
 
 NTSTATUS wbsrv_samba3_interface_version(struct wbsrv_samba3_call *s3call)
 {
@@ -45,31 +90,35 @@ NTSTATUS wbsrv_samba3_interface_version(struct wbsrv_samba3_call *s3call)
 NTSTATUS wbsrv_samba3_info(struct wbsrv_samba3_call *s3call)
 {
        s3call->response.result                 = WINBINDD_OK;
-       s3call->response.data.info.winbind_separator = *lp_winbind_separator();
-       WBSRV_SAMBA3_SET_STRING(s3call->response.data.info.samba_version, SAMBA_VERSION_STRING);
+       s3call->response.data.info.winbind_separator = *lp_winbind_separator(s3call->wbconn->lp_ctx);
+       WBSRV_SAMBA3_SET_STRING(s3call->response.data.info.samba_version,
+                               SAMBA_VERSION_STRING);
        return NT_STATUS_OK;
 }
 
 NTSTATUS wbsrv_samba3_domain_name(struct wbsrv_samba3_call *s3call)
 {
        s3call->response.result                 = WINBINDD_OK;
-       WBSRV_SAMBA3_SET_STRING(s3call->response.data.domain_name, lp_workgroup());
+       WBSRV_SAMBA3_SET_STRING(s3call->response.data.domain_name,
+                               lp_workgroup(s3call->wbconn->lp_ctx));
        return NT_STATUS_OK;
 }
 
 NTSTATUS wbsrv_samba3_netbios_name(struct wbsrv_samba3_call *s3call)
 {
        s3call->response.result                 = WINBINDD_OK;
-       WBSRV_SAMBA3_SET_STRING(s3call->response.data.netbios_name, lp_netbios_name());
+       WBSRV_SAMBA3_SET_STRING(s3call->response.data.netbios_name,
+                               lp_netbios_name(s3call->wbconn->lp_ctx));
        return NT_STATUS_OK;
 }
 
 NTSTATUS wbsrv_samba3_priv_pipe_dir(struct wbsrv_samba3_call *s3call)
 {
-       s3call->response.result                 = WINBINDD_OK;
-       s3call->response.extra_data             = smbd_tmp_path(s3call,
-                                                 WINBINDD_SAMBA3_PRIVILEGED_SOCKET);
-       NT_STATUS_HAVE_NO_MEMORY(s3call->response.extra_data);
+       const char *path = s3call->wbconn->listen_socket->service->priv_socket_path;
+       s3call->response.result          = WINBINDD_OK;
+       s3call->response.extra_data.data = discard_const(path);
+
+       s3call->response.length += strlen(path) + 1;
        return NT_STATUS_OK;
 }
 
@@ -79,236 +128,1092 @@ NTSTATUS wbsrv_samba3_ping(struct wbsrv_samba3_call *s3call)
        return NT_STATUS_OK;
 }
 
-#define null_no_memory_done(x) do { \
-       if ((x) == NULL) { status = NT_STATUS_NO_MEMORY; goto done; } \
-       } while (0)
-
-struct check_machacc_state {
-       struct wb_finddcs *io;
-       struct smb_composite_connect *conn;
-       struct wb_get_schannel_creds *getcreds;
-};
+/* Plaintext authentication 
+   
+   This interface is used by ntlm_auth in it's 'basic' authentication
+   mode, as well as by pam_winbind to authenticate users where we are
+   given a plaintext password.
+*/
 
-static void wbsrv_samba3_check_machacc_receive_creds(struct composite_context *action);
-static void wbsrv_samba3_check_machacc_receive_tree(struct composite_context *action);
-static void wbsrv_samba3_check_machacc_receive_dcs(struct composite_context *action);
+static void check_machacc_recv(struct composite_context *ctx);
 
 NTSTATUS wbsrv_samba3_check_machacc(struct wbsrv_samba3_call *s3call)
 {
-       struct composite_context *resolve_req;
-       struct check_machacc_state *state;
+       NTSTATUS status;
+       struct cli_credentials *creds;
+       struct composite_context *ctx;
        struct wbsrv_service *service =
-               s3call->call->wbconn->listen_socket->service;
+               s3call->wbconn->listen_socket->service;
+
+       /* Create a credentials structure */
+       creds = cli_credentials_init(s3call);
+       if (creds == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       cli_credentials_set_conf(creds, service->task->lp_ctx);
+
+       /* Connect the machine account to the credentials */
+       status = cli_credentials_set_machine_account(creds, service->task->lp_ctx);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(creds);
+               return status;
+       }
 
-       DEBUG(5, ("check_machacc called\n"));
+       ctx = wb_cmd_pam_auth_send(s3call, service, creds);
 
-       if (service->netlogon != NULL) {
-               talloc_free(service->netlogon);
+       if (!ctx) {
+               talloc_free(creds);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       state = talloc(s3call, struct check_machacc_state);
-       NT_STATUS_HAVE_NO_MEMORY(state);
+       ctx->async.fn = check_machacc_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
 
-       state->io = talloc(s3call, struct wb_finddcs);
-       NT_STATUS_HAVE_NO_MEMORY(state->io);
-       s3call->private_data = state;
+static void check_machacc_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
 
-       state->io->in.msg_ctx = s3call->call->wbconn->conn->msg_ctx;
-       state->io->in.domain = lp_workgroup();
+       status = wb_cmd_pam_auth_recv(ctx);
 
-       resolve_req = wb_finddcs_send(state->io, s3call->call->event_ctx);
-       NT_STATUS_HAVE_NO_MEMORY(resolve_req);
+       if (!NT_STATUS_IS_OK(status)) goto done;
 
-       /* setup the callbacks */
-       resolve_req->async.fn = wbsrv_samba3_check_machacc_receive_dcs;
-       resolve_req->async.private_data = s3call;
+ done:
+       wbsrv_samba3_async_auth_epilogue(status, s3call);
+}
 
-       /* tell the caller we reply later */
-       s3call->call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+/*
+  Find the name of a suitable domain controller, by query on the
+  netlogon pipe to the DC.  
+*/
+
+static void getdcname_recv_dc(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_getdcname(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_getdcname called\n"));
+
+       ctx = wb_cmd_getdcname_send(s3call, service,
+                                   s3call->request.domain_name);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = getdcname_recv_dc;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
        return NT_STATUS_OK;
 }
 
-static void wbsrv_samba3_check_machacc_receive_dcs(struct composite_context *action)
+static void getdcname_recv_dc(struct composite_context *ctx)
 {
        struct wbsrv_samba3_call *s3call =
-               talloc_get_type(action->async.private_data,
+               talloc_get_type(ctx->async.private_data,
                                struct wbsrv_samba3_call);
-       struct check_machacc_state *state =
-               talloc_get_type(s3call->private_data,
-                               struct check_machacc_state);
-       struct composite_context *ctx;
+       const char *dcname;
        NTSTATUS status;
 
-       status = wb_finddcs_recv(action, s3call);
+       status = wb_cmd_getdcname_recv(ctx, s3call, &dcname);
+       if (!NT_STATUS_IS_OK(status)) goto done;
 
-       s3call->response.data.auth.nt_status = NT_STATUS_V(status);
-       WBSRV_SAMBA3_SET_STRING(s3call->response.data.auth.nt_status_string,
-                               nt_errstr(status));
-       WBSRV_SAMBA3_SET_STRING(s3call->response.data.auth.error_string,
-                               nt_errstr(status));
-       s3call->response.data.auth.pam_error = nt_status_to_pam(status);
+       s3call->response.result = WINBINDD_OK;
+       WBSRV_SAMBA3_SET_STRING(s3call->response.data.dc_name, dcname);
 
-       if (!NT_STATUS_IS_OK(status)) {
-               goto done;
+ done:
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+/* 
+   Lookup a user's domain groups
+*/
+
+static void userdomgroups_recv_groups(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_userdomgroups(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct dom_sid *sid;
+
+       DEBUG(5, ("wbsrv_samba3_userdomgroups called\n"));
+
+       sid = dom_sid_parse_talloc(s3call, s3call->request.data.sid);
+       if (sid == NULL) {
+               DEBUG(5, ("Could not parse sid %s\n",
+                         s3call->request.data.sid));
+               return NT_STATUS_NO_MEMORY;
        }
 
-       state->conn = talloc(state, struct smb_composite_connect);
-       if (state->conn == NULL) {
+       ctx = wb_cmd_userdomgroups_send(
+               s3call, s3call->wbconn->listen_socket->service, sid);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = userdomgroups_recv_groups;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void userdomgroups_recv_groups(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       int i, num_sids;
+       struct dom_sid **sids;
+       char *sids_string;
+       NTSTATUS status;
+
+       status = wb_cmd_userdomgroups_recv(ctx, s3call, &num_sids, &sids);
+       if (!NT_STATUS_IS_OK(status)) goto done;
+
+       sids_string = talloc_strdup(s3call, "");
+       if (sids_string == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto done;
        }
 
-       state->conn->in.dest_host = state->io->out.dcs[0].address;
-       state->conn->in.port = 0;
-       state->conn->in.called_name = state->io->out.dcs[0].name;
-       state->conn->in.service = "IPC$";
-       state->conn->in.service_type = "IPC";
-       state->conn->in.workgroup = lp_workgroup();
+       for (i=0; i<num_sids; i++) {
+               sids_string = talloc_asprintf_append_buffer(
+                       sids_string, "%s\n", dom_sid_string(s3call, sids[i]));
+       }
 
-       state->conn->in.credentials = cli_credentials_init(state->conn);
-       if (state->conn->in.credentials == NULL) {
+       if (sids_string == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto done;
        }
-       cli_credentials_set_conf(state->conn->in.credentials);
-       cli_credentials_set_anonymous(state->conn->in.credentials);
 
-       ctx = smb_composite_connect_send(state->conn, state,
-                                        s3call->call->event_ctx);
-       if (ctx == NULL) {
-               status = NT_STATUS_NO_MEMORY;
-               goto done;
+       s3call->response.result = WINBINDD_OK;
+       s3call->response.extra_data.data = sids_string;
+       s3call->response.length += strlen(sids_string)+1;
+       s3call->response.data.num_entries = num_sids;
+
+ done:
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+/* 
+   Lookup the list of SIDs for a user 
+*/
+static void usersids_recv_sids(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_usersids(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct dom_sid *sid;
+
+       DEBUG(5, ("wbsrv_samba3_usersids called\n"));
+
+       sid = dom_sid_parse_talloc(s3call, s3call->request.data.sid);
+       if (sid == NULL) {
+               DEBUG(5, ("Could not parse sid %s\n",
+                         s3call->request.data.sid));
+               return NT_STATUS_NO_MEMORY;
        }
 
-       ctx->async.fn = wbsrv_samba3_check_machacc_receive_tree;
+       ctx = wb_cmd_usersids_send(
+               s3call, s3call->wbconn->listen_socket->service, sid);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = usersids_recv_sids;
        ctx->async.private_data = s3call;
-       return;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
 
- done:
-       if (!NT_STATUS_IS_OK(status)) {
-               s3call->response.result = WINBINDD_ERROR;
+static void usersids_recv_sids(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       int i, num_sids;
+       struct dom_sid **sids;
+       char *sids_string;
+       NTSTATUS status;
+
+       status = wb_cmd_usersids_recv(ctx, s3call, &num_sids, &sids);
+       if (!NT_STATUS_IS_OK(status)) goto done;
+
+       sids_string = talloc_strdup(s3call, "");
+       if (sids_string == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto done;
        }
 
-       status = wbsrv_send_reply(s3call->call);
-       if (!NT_STATUS_IS_OK(status)) {
-               wbsrv_terminate_connection(s3call->call->wbconn,
-                                          "wbsrv_queue_reply() failed");
-               return;
+       for (i=0; i<num_sids; i++) {
+               sids_string = talloc_asprintf_append_buffer(
+                       sids_string, "%s\n", dom_sid_string(s3call, sids[i]));
+               if (sids_string == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto done;
+               }
+       }
+
+       s3call->response.result = WINBINDD_OK;
+       s3call->response.extra_data.data = sids_string;
+       s3call->response.length += strlen(sids_string);
+       s3call->response.data.num_entries = num_sids;
+
+       /* Hmmmm. Nasty protocol -- who invented the zeros between the
+        * SIDs? Hmmm. Could have been me -- vl */
+
+       while (*sids_string != '\0') {
+               if ((*sids_string) == '\n') {
+                       *sids_string = '\0';
+               }
+               sids_string += 1;
        }
+
+ done:
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+/* 
+   Lookup a DOMAIN\\user style name, and return a SID
+*/
+
+static void lookupname_recv_sid(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_lookupname(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_lookupname called\n"));
+
+       ctx = wb_cmd_lookupname_send(s3call, service,
+                                    s3call->request.data.name.dom_name,
+                                    s3call->request.data.name.name);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       /* setup the callbacks */
+       ctx->async.fn = lookupname_recv_sid;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
 }
 
-static void wbsrv_samba3_check_machacc_receive_tree(struct composite_context *action)
+static void lookupname_recv_sid(struct composite_context *ctx)
 {
        struct wbsrv_samba3_call *s3call =
-               talloc_get_type(action->async.private_data,
+               talloc_get_type(ctx->async.private_data,
                                struct wbsrv_samba3_call);
-       struct check_machacc_state *state =
-               talloc_get_type(s3call->private_data,
-                               struct check_machacc_state);
-       struct composite_context *ctx;
+       struct wb_sid_object *sid;
        NTSTATUS status;
-       struct cli_credentials *creds;
 
-       status = smb_composite_connect_recv(action, state);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(5, ("Connect failed: %s\n", nt_errstr(status)));
-               goto done;
+       status = wb_cmd_lookupname_recv(ctx, s3call, &sid);
+       if (!NT_STATUS_IS_OK(status)) goto done;
+
+       s3call->response.result = WINBINDD_OK;
+       s3call->response.data.sid.type = sid->type;
+       WBSRV_SAMBA3_SET_STRING(s3call->response.data.sid.sid,
+                               dom_sid_string(s3call, sid->sid));
+
+ done:
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+/* 
+   Lookup a SID, and return a DOMAIN\\user style name
+*/
+
+static void lookupsid_recv_name(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_lookupsid(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+       struct dom_sid *sid;
+
+       DEBUG(5, ("wbsrv_samba3_lookupsid called\n"));
+
+       sid = dom_sid_parse_talloc(s3call, s3call->request.data.sid);
+       if (sid == NULL) {
+               DEBUG(5, ("Could not parse sid %s\n",
+                         s3call->request.data.sid));
+               return NT_STATUS_NO_MEMORY;
        }
 
-       state->getcreds = talloc(state, struct wb_get_schannel_creds);
-       null_no_memory_done(state->getcreds);
+       ctx = wb_cmd_lookupsid_send(s3call, service, sid);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       /* setup the callbacks */
+       ctx->async.fn = lookupsid_recv_name;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void lookupsid_recv_name(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       struct wb_sid_object *sid;
+       NTSTATUS status;
 
-       creds = cli_credentials_init(state);
-       null_no_memory_done(creds);
-       cli_credentials_set_conf(creds);
-       status = cli_credentials_set_machine_account(creds);
+       status = wb_cmd_lookupsid_recv(ctx, s3call, &sid);
        if (!NT_STATUS_IS_OK(status)) goto done;
 
-       state->getcreds->in.tree = state->conn->out.tree;
-       state->getcreds->in.creds = creds;
+       s3call->response.result = WINBINDD_OK;
+       s3call->response.data.name.type = sid->type;
+       WBSRV_SAMBA3_SET_STRING(s3call->response.data.name.dom_name,
+                               sid->domain);
+       WBSRV_SAMBA3_SET_STRING(s3call->response.data.name.name, sid->name);
+
+ done:
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+/*
+  Challenge-response authentication.  This interface is used by
+  ntlm_auth and the smbd auth subsystem to pass NTLM authentication
+  requests along a common pipe to the domain controller.  
+
+  The return value (in the async reply) may include the 'info3'
+  (effectivly most things you would want to know about the user), or
+  the NT and LM session keys seperated.
+*/
+
+static void pam_auth_crap_recv(struct composite_context *ctx);
 
-       ctx = wb_get_schannel_creds_send(state->getcreds,
-                                        s3call->call->event_ctx);
-       null_no_memory_done(ctx);
+NTSTATUS wbsrv_samba3_pam_auth_crap(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+       DATA_BLOB chal, nt_resp, lm_resp;
+
+       DEBUG(5, ("wbsrv_samba3_pam_auth_crap called\n"));
 
-       ctx->async.fn = wbsrv_samba3_check_machacc_receive_creds;
+       chal.data       = s3call->request.data.auth_crap.chal;
+       chal.length     = sizeof(s3call->request.data.auth_crap.chal);
+       nt_resp.data    = (uint8_t *)s3call->request.data.auth_crap.nt_resp;
+       nt_resp.length  = s3call->request.data.auth_crap.nt_resp_len;
+       lm_resp.data    = (uint8_t *)s3call->request.data.auth_crap.lm_resp;
+       lm_resp.length  = s3call->request.data.auth_crap.lm_resp_len;
+
+       ctx = wb_cmd_pam_auth_crap_send(
+               s3call, service,
+               s3call->request.data.auth_crap.logon_parameters,
+               s3call->request.data.auth_crap.domain,
+               s3call->request.data.auth_crap.user,
+               s3call->request.data.auth_crap.workstation,
+               chal, nt_resp, lm_resp);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = pam_auth_crap_recv;
        ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
 
-       return;
+static void pam_auth_crap_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+       DATA_BLOB info3;
+       struct netr_UserSessionKey user_session_key;
+       struct netr_LMSessionKey lm_key;
+       char *unix_username;
        
- done:
-       s3call->response.result = WINBINDD_OK;
+       status = wb_cmd_pam_auth_crap_recv(ctx, s3call, &info3,
+                                          &user_session_key, &lm_key, &unix_username);
+       if (!NT_STATUS_IS_OK(status)) goto done;
 
-       if (!NT_STATUS_IS_OK(status)) {
-               s3call->response.result = WINBINDD_ERROR;
-               WBSRV_SAMBA3_SET_STRING(s3call->response.data.auth.nt_status_string,
-                                       nt_errstr(status));
-               WBSRV_SAMBA3_SET_STRING(s3call->response.data.auth.error_string,
-                                       nt_errstr(status));
-               s3call->response.data.auth.pam_error = nt_status_to_pam(status);
+       if (s3call->request.flags & WBFLAG_PAM_USER_SESSION_KEY) {
+               memcpy(s3call->response.data.auth.user_session_key, 
+                      &user_session_key.key,
+                      sizeof(s3call->response.data.auth.user_session_key));
+       }
 
+       if (s3call->request.flags & WBFLAG_PAM_INFO3_NDR) {
+               s3call->response.extra_data.data = info3.data;
+               s3call->response.length += info3.length;
        }
 
-       status = wbsrv_send_reply(s3call->call);
-       if (!NT_STATUS_IS_OK(status)) {
-               wbsrv_terminate_connection(s3call->call->wbconn,
-                                          "wbsrv_queue_reply() failed");
-               return;
+       if (s3call->request.flags & WBFLAG_PAM_LMKEY) {
+               memcpy(s3call->response.data.auth.first_8_lm_hash, 
+                      lm_key.key,
+                      sizeof(s3call->response.data.auth.first_8_lm_hash));
+       }
+       
+       if (s3call->request.flags & WBFLAG_PAM_UNIX_NAME) {
+               s3call->response.extra_data.data = unix_username;
+               s3call->response.length += strlen(unix_username)+1;
        }
+
+ done:
+       wbsrv_samba3_async_auth_epilogue(status, s3call);
 }
 
-static void delete_pipe(struct event_context *ctx, struct timed_event *te,
-                       struct timeval tv, void *p)
+/* Plaintext authentication 
+   
+   This interface is used by ntlm_auth in it's 'basic' authentication
+   mode, as well as by pam_winbind to authenticate users where we are
+   given a plaintext password.
+*/
+
+static void pam_auth_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_pam_auth(struct wbsrv_samba3_call *s3call)
 {
-       talloc_free(p);
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+       struct cli_credentials *credentials;
+       char *user, *domain;
+
+       if (!wb_samba3_split_username(s3call, s3call->wbconn->lp_ctx,
+                                s3call->request.data.auth.user,
+                                &domain, &user)) {
+               return NT_STATUS_NO_SUCH_USER;
+       }
+
+       credentials = cli_credentials_init(s3call);
+       if (!credentials) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       cli_credentials_set_conf(credentials, service->task->lp_ctx);
+       cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
+       cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
+
+       cli_credentials_set_password(credentials, s3call->request.data.auth.pass, CRED_SPECIFIED);
+
+       ctx = wb_cmd_pam_auth_send(s3call, service, credentials);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = pam_auth_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
 }
 
-static void wbsrv_samba3_check_machacc_receive_creds(struct composite_context *action)
+static void pam_auth_recv(struct composite_context *ctx)
 {
        struct wbsrv_samba3_call *s3call =
-               talloc_get_type(action->async.private_data,
+               talloc_get_type(ctx->async.private_data,
                                struct wbsrv_samba3_call);
-       struct check_machacc_state *state =
-               talloc_get_type(s3call->private_data,
-                               struct check_machacc_state);
+       NTSTATUS status;
+
+       status = wb_cmd_pam_auth_recv(ctx);
+
+       if (!NT_STATUS_IS_OK(status)) goto done;
+
+ done:
+       wbsrv_samba3_async_auth_epilogue(status, s3call);
+}
+
+/* 
+   List trusted domains
+*/
+
+static void list_trustdom_recv_doms(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_list_trustdom(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
        struct wbsrv_service *service =
-               s3call->call->wbconn->listen_socket->service;
-       
+               s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_list_trustdom called\n"));
+
+       ctx = wb_cmd_list_trustdoms_send(s3call, service);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = list_trustdom_recv_doms;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void list_trustdom_recv_doms(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       int i, num_domains;
+       struct wb_dom_info **domains;
        NTSTATUS status;
-       
-       status = wb_get_schannel_creds_recv(action, service);
+       char *result;
 
-       talloc_unlink(state, state->conn->out.tree); /* The pipe owns it now */
-       state->conn->out.tree = NULL;
+       status = wb_cmd_list_trustdoms_recv(ctx, s3call, &num_domains,
+                                           &domains);
+       if (!NT_STATUS_IS_OK(status)) goto done;
 
-       if (!NT_STATUS_IS_OK(status)) {
-               /* Nasty hack awaiting a proper fix. So far we can not
-                * delete a pipe from an async rpc callback which where we are
-                * in right now, so delete the pipe from an event handler.. */
-               event_add_timed(s3call->call->event_ctx, service,
-                               timeval_zero(),
-                               delete_pipe, state->getcreds->out.netlogon);
-               service->netlogon = NULL;
+       result = talloc_strdup(s3call, "");
+       if (result == NULL) {
+               status = NT_STATUS_NO_MEMORY;
                goto done;
        }
 
-       service->netlogon = state->getcreds->out.netlogon;
-       s3call->response.result = WINBINDD_OK;
- done:
-       if (!NT_STATUS_IS_OK(status)) {
-               s3call->response.result = WINBINDD_ERROR;
-               WBSRV_SAMBA3_SET_STRING(s3call->response.data.auth.nt_status_string,
-                                       nt_errstr(status));
-               WBSRV_SAMBA3_SET_STRING(s3call->response.data.auth.error_string,
-                                       nt_errstr(status));
-               s3call->response.data.auth.pam_error = nt_status_to_pam(status);
+       for (i=0; i<num_domains; i++) {
+               result = talloc_asprintf_append_buffer(
+                       result, "%s\\%s\\%s",
+                       domains[i]->name, domains[i]->name,
+                       dom_sid_string(s3call, domains[i]->sid));
+       }
 
+       if (result == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto done;
        }
 
-       status = wbsrv_send_reply(s3call->call);
-       if (!NT_STATUS_IS_OK(status)) {
-               wbsrv_terminate_connection(s3call->call->wbconn,
-                                          "wbsrv_queue_reply() failed");
-               return;
+       s3call->response.result = WINBINDD_OK;
+       if (num_domains > 0) {
+               s3call->response.extra_data.data = result;
+               s3call->response.length += strlen(result)+1;
        }
+
+ done:
+       wbsrv_samba3_async_epilogue(status, s3call);
 }
+
+/* list groups */
+static void list_groups_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_list_groups(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service = s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba4_list_groups called\n"));
+
+       ctx = wb_cmd_list_groups_send(s3call, service,
+                                     s3call->request.domain_name);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = list_groups_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void list_groups_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call = talloc_get_type_abort(
+                                               ctx->async.private_data,
+                                               struct wbsrv_samba3_call);
+       uint32_t extra_data_len;
+       char *extra_data;
+       NTSTATUS status;
+
+       DEBUG(5, ("list_groups_recv called\n"));
+
+       status = wb_cmd_list_groups_recv(ctx, s3call, &extra_data_len,
+                       &extra_data);
+
+       if (NT_STATUS_IS_OK(status)) {
+               s3call->response.extra_data.data = extra_data;
+               s3call->response.length += extra_data_len;
+               if (extra_data) {
+                       s3call->response.length += 1;
+               }
+       }
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+/* List users */
+
+static void list_users_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_list_users(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_list_users called\n"));
+
+       ctx = wb_cmd_list_users_send(s3call, service,
+                       s3call->request.domain_name);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = list_users_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void list_users_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       uint32_t extra_data_len;
+       char *extra_data;
+       NTSTATUS status;
+
+       DEBUG(5, ("list_users_recv called\n"));
+
+       status = wb_cmd_list_users_recv(ctx, s3call, &extra_data_len,
+                       &extra_data);
+
+       if (NT_STATUS_IS_OK(status)) {
+               s3call->response.extra_data.data = extra_data;
+               s3call->response.length += extra_data_len;
+               if (extra_data) {
+                       s3call->response.length += 1;
+               }
+       }
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+/* NSS calls */
+
+static void getpwnam_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_getpwnam(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_getpwnam called\n"));
+
+       ctx = wb_cmd_getpwnam_send(s3call, service,
+                       s3call->request.data.username);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = getpwnam_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void getpwnam_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+       struct winbindd_pw *pw;
+
+       DEBUG(5, ("getpwnam_recv called\n"));
+
+       status = wb_cmd_getpwnam_recv(ctx, s3call, &pw);
+       if(NT_STATUS_IS_OK(status))
+               s3call->response.data.pw = *pw;
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+static void getpwuid_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_getpwuid(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service = s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_getpwuid called\n"));
+
+       ctx = wb_cmd_getpwuid_send(s3call, service,
+                       s3call->request.data.uid);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = getpwuid_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void getpwuid_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+       struct winbindd_pw *pw;
+
+       DEBUG(5, ("getpwuid_recv called\n"));
+
+       status = wb_cmd_getpwuid_recv(ctx, s3call, &pw);
+       if (NT_STATUS_IS_OK(status))
+               s3call->response.data.pw = *pw;
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+static void setpwent_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_setpwent(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service = s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_setpwent called\n"));
+
+       ctx = wb_cmd_setpwent_send(s3call, service);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = setpwent_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void setpwent_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+       struct wbsrv_pwent *pwent;
+
+       DEBUG(5, ("setpwent_recv called\n"));
+
+       status = wb_cmd_setpwent_recv(ctx, s3call->wbconn, &pwent);
+       if (NT_STATUS_IS_OK(status)) {
+               s3call->wbconn->protocol_private_data = pwent;
+       }
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+static void getpwent_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_getpwent(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service = s3call->wbconn->listen_socket->service;
+       struct wbsrv_pwent *pwent;
+
+       DEBUG(5, ("wbsrv_samba3_getpwent called\n"));
+
+       NT_STATUS_HAVE_NO_MEMORY(s3call->wbconn->protocol_private_data);
+
+       pwent = talloc_get_type(s3call->wbconn->protocol_private_data,
+                       struct wbsrv_pwent);
+       NT_STATUS_HAVE_NO_MEMORY(pwent);
+
+       ctx = wb_cmd_getpwent_send(s3call, service, pwent,
+                       s3call->request.data.num_entries);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = getpwent_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void getpwent_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+       struct winbindd_pw *pw;
+       uint32_t num_users;
+
+       DEBUG(5, ("getpwent_recv called\n"));
+
+       status = wb_cmd_getpwent_recv(ctx, s3call, &pw, &num_users);
+       if (NT_STATUS_IS_OK(status)) {
+               uint32_t extra_len = sizeof(struct winbindd_pw) * num_users;
+
+               s3call->response.data.num_entries = num_users;
+               s3call->response.extra_data.data = pw;
+               s3call->response.length += extra_len;
+       }
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+NTSTATUS wbsrv_samba3_endpwent(struct wbsrv_samba3_call *s3call)
+{
+       struct wbsrv_pwent *pwent =
+               talloc_get_type(s3call->wbconn->protocol_private_data,
+                               struct wbsrv_pwent);
+       DEBUG(5, ("wbsrv_samba3_endpwent called\n"));
+
+       talloc_free(pwent);
+
+       s3call->wbconn->protocol_private_data = NULL;
+       s3call->response.result = WINBINDD_OK;
+       return NT_STATUS_OK;
+}
+
+
+static void getgrnam_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_getgrnam(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_getgrnam called\n"));
+
+       ctx = wb_cmd_getgrnam_send(s3call, service,
+                       s3call->request.data.groupname);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = getgrnam_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void getgrnam_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+       struct winbindd_gr *gr;
+
+       DEBUG(5, ("getgrnam_recv called\n"));
+
+       status = wb_cmd_getgrnam_recv(ctx, s3call, &gr);
+       if(NT_STATUS_IS_OK(status))
+               s3call->response.data.gr = *gr;
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+static void getgrgid_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_getgrgid(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service = s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_getgrgid called\n"));
+
+       ctx = wb_cmd_getgrgid_send(s3call, service,
+                       s3call->request.data.gid);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = getgrgid_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void getgrgid_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+       struct winbindd_gr *gr;
+
+       DEBUG(5, ("getgrgid_recv called\n"));
+
+       status = wb_cmd_getgrgid_recv(ctx, s3call, &gr);
+       if (NT_STATUS_IS_OK(status))
+               s3call->response.data.gr = *gr;
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+NTSTATUS wbsrv_samba3_getgroups(struct wbsrv_samba3_call *s3call)
+{
+       DEBUG(5, ("wbsrv_samba3_getgroups called\n"));
+       s3call->response.result = WINBINDD_ERROR;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS wbsrv_samba3_setgrent(struct wbsrv_samba3_call *s3call)
+{
+       DEBUG(5, ("wbsrv_samba3_setgrent called\n"));
+       s3call->response.result = WINBINDD_OK;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS wbsrv_samba3_getgrent(struct wbsrv_samba3_call *s3call)
+{
+       DEBUG(5, ("wbsrv_samba3_getgrent called\n"));
+       s3call->response.result = WINBINDD_ERROR;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS wbsrv_samba3_endgrent(struct wbsrv_samba3_call *s3call)
+{
+       DEBUG(5, ("wbsrv_samba3_endgrent called\n"));
+       s3call->response.result = WINBINDD_OK;
+       return NT_STATUS_OK;
+}
+
+static void sid2uid_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_sid2uid(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+       struct dom_sid *sid;
+
+       DEBUG(5, ("wbsrv_samba3_sid2uid called\n"));
+
+       sid = dom_sid_parse_talloc(s3call, s3call->request.data.sid);
+       NT_STATUS_HAVE_NO_MEMORY(sid);
+
+       ctx = wb_sid2uid_send(s3call, service, sid);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = sid2uid_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+
+}
+
+static void sid2uid_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+
+       DEBUG(5, ("sid2uid_recv called\n"));
+
+       status = wb_sid2uid_recv(ctx, &s3call->response.data.uid);
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+static void sid2gid_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_sid2gid(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+       struct dom_sid *sid;
+
+       DEBUG(5, ("wbsrv_samba3_sid2gid called\n"));
+
+       sid = dom_sid_parse_talloc(s3call, s3call->request.data.sid);
+       NT_STATUS_HAVE_NO_MEMORY(sid);
+
+       ctx = wb_sid2gid_send(s3call, service, sid);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = sid2gid_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+
+}
+
+static void sid2gid_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+
+       DEBUG(5, ("sid2gid_recv called\n"));
+
+       status = wb_sid2gid_recv(ctx, &s3call->response.data.gid);
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+static void uid2sid_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_uid2sid(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_uid2sid called\n"));
+
+       ctx = wb_uid2sid_send(s3call, service, s3call->request.data.uid);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = uid2sid_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+
+}
+
+static void uid2sid_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+       struct dom_sid *sid;
+       char *sid_str;
+
+       DEBUG(5, ("uid2sid_recv called\n"));
+
+       status = wb_uid2sid_recv(ctx, s3call, &sid);
+       if(NT_STATUS_IS_OK(status)) {
+               sid_str = dom_sid_string(s3call, sid);
+
+               /* If the conversion failed, bail out with a failure. */
+               if (sid_str == NULL)
+                       wbsrv_samba3_async_epilogue(NT_STATUS_NO_MEMORY,s3call);
+
+               /* But we assume this worked, so we'll set the string. Work
+                * done. */
+               WBSRV_SAMBA3_SET_STRING(s3call->response.data.sid.sid, sid_str);
+               s3call->response.data.sid.type = SID_NAME_USER;
+       }
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+
+static void gid2sid_recv(struct composite_context *ctx);
+
+NTSTATUS wbsrv_samba3_gid2sid(struct wbsrv_samba3_call *s3call)
+{
+       struct composite_context *ctx;
+       struct wbsrv_service *service =
+               s3call->wbconn->listen_socket->service;
+
+       DEBUG(5, ("wbsrv_samba3_gid2sid called\n"));
+
+       ctx = wb_gid2sid_send(s3call, service, s3call->request.data.gid);
+       NT_STATUS_HAVE_NO_MEMORY(ctx);
+
+       ctx->async.fn = gid2sid_recv;
+       ctx->async.private_data = s3call;
+       s3call->flags |= WBSRV_CALL_FLAGS_REPLY_ASYNC;
+       return NT_STATUS_OK;
+
+}
+
+static void gid2sid_recv(struct composite_context *ctx)
+{
+       struct wbsrv_samba3_call *s3call =
+               talloc_get_type(ctx->async.private_data,
+                               struct wbsrv_samba3_call);
+       NTSTATUS status;
+       struct dom_sid *sid;
+       char *sid_str;
+
+       DEBUG(5, ("gid2sid_recv called\n"));
+
+       status = wb_gid2sid_recv(ctx, s3call, &sid);
+       if(NT_STATUS_IS_OK(status)) {
+               sid_str = dom_sid_string(s3call, sid);
+
+               if (sid_str == NULL)
+                       wbsrv_samba3_async_epilogue(NT_STATUS_NO_MEMORY,s3call);
+
+               WBSRV_SAMBA3_SET_STRING(s3call->response.data.sid.sid, sid_str);
+               s3call->response.data.sid.type = SID_NAME_DOMAIN;
+       }
+
+       wbsrv_samba3_async_epilogue(status, s3call);
+}
+