wbclient: "ev" is no longer used in wbc_sids_to_xids
[metze/samba/wip.git] / source4 / libcli / wbclient / wbclient.c
index 5b95be125934ad3b2932059890a995e73ade402f..1ff35c8eba5a305cd410d7b9b8e380cb535609a4 100644 (file)
 
 #include "includes.h"
 #include <tevent.h>
+#include "nsswitch/winbind_client.h"
 #include "libcli/wbclient/wbclient.h"
-#include "nsswitch/wb_reqtrans.h"
-#include "system/network.h"
-#include "libcli/util/error.h"
 #include "libcli/security/dom_sid.h"
+#include "nsswitch/libwbclient/wbclient.h"
 
-/**
- * Initialize the wbclient context, talloc_free() when done.
- *
- * \param mem_ctx talloc context to allocate memory from
- * \param msg_ctx message context to use
- * \param
- */
-struct wbc_context *wbc_init(TALLOC_CTX *mem_ctx,
-                            struct imessaging_context *msg_ctx,
-                            struct tevent_context *event_ctx)
+NTSTATUS wbc_sids_to_xids(struct id_map *ids, uint32_t count)
 {
-       struct wbc_context *ctx;
-
-       ctx = talloc(mem_ctx, struct wbc_context);
-       if (ctx == NULL) return NULL;
-
-       ctx->event_ctx = event_ctx;
+       TALLOC_CTX *mem_ctx;
+       uint32_t i;
+       struct wbcDomainSid *sids;
+       struct wbcUnixId *xids;
+       wbcErr result;
+       bool wb_off;
 
-       ctx->irpc_handle = irpc_binding_handle_by_name(ctx, msg_ctx,
-                                                      "winbind_server",
-                                                      &ndr_table_winbind);
-       if (ctx->irpc_handle == NULL) {
-               talloc_free(ctx);
-               return NULL;
+       mem_ctx = talloc_new(NULL);
+       if (mem_ctx == NULL) {
+               return NT_STATUS_NO_MEMORY;
        }
 
-       return ctx;
-}
-
-struct wbc_idmap_state {
-       struct composite_context *ctx;
-       struct winbind_get_idmap *req;
-       struct id_map *ids;
-};
-
-static void sids_to_xids_recv_ids(struct tevent_req *subreq);
-
-struct composite_context *wbc_sids_to_xids_send(struct wbc_context *wbc_ctx,
-                                               TALLOC_CTX *mem_ctx,
-                                               uint32_t count,
-                                               struct id_map *ids)
-{
-       struct composite_context *ctx;
-       struct wbc_idmap_state *state;
-       struct tevent_req *subreq;
-
-       DEBUG(5, ("wbc_sids_to_xids called\n"));
-
-       ctx = composite_create(mem_ctx, wbc_ctx->event_ctx);
-       if (ctx == NULL) return NULL;
-
-       state = talloc(ctx, struct wbc_idmap_state);
-       if (composite_nomem(state, ctx)) return ctx;
-       ctx->private_data = state;
-
-       state->req = talloc(state, struct winbind_get_idmap);
-       if (composite_nomem(state->req, ctx)) return ctx;
-
-       state->req->in.count = count;
-       state->req->in.level = WINBIND_IDMAP_LEVEL_SIDS_TO_XIDS;
-       state->req->in.ids = ids;
-       state->ctx = ctx;
-
-       subreq = dcerpc_winbind_get_idmap_r_send(state,
-                                                wbc_ctx->event_ctx,
-                                                wbc_ctx->irpc_handle,
-                                                state->req);
-       if (composite_nomem(subreq, ctx)) return ctx;
-
-       tevent_req_set_callback(subreq, sids_to_xids_recv_ids, state);
-
-       return ctx;
-}
-
-static void sids_to_xids_recv_ids(struct tevent_req *subreq)
-{
-       struct wbc_idmap_state *state =
-               tevent_req_callback_data(subreq,
-               struct wbc_idmap_state);
-
-       state->ctx->status = dcerpc_winbind_get_idmap_r_recv(subreq, state);
-       TALLOC_FREE(subreq);
-       if (!composite_is_ok(state->ctx)) return;
-
-       state->ids = state->req->out.ids;
-       composite_done(state->ctx);
-}
-
-NTSTATUS wbc_sids_to_xids_recv(struct composite_context *ctx,
-                              struct id_map **ids)
-{
-       NTSTATUS status = composite_wait(ctx);
-               DEBUG(5, ("wbc_sids_to_xids_recv called\n"));
-       if (NT_STATUS_IS_OK(status)) {
-               struct wbc_idmap_state *state = talloc_get_type_abort(
-                                                       ctx->private_data,
-                                                       struct wbc_idmap_state);
-               *ids = state->ids;
+       sids = talloc_array(mem_ctx, struct wbcDomainSid, count);
+       if (sids == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       return status;
-}
-
-static void xids_to_sids_recv_ids(struct tevent_req *subreq);
-
-struct composite_context *wbc_xids_to_sids_send(struct wbc_context *wbc_ctx,
-                                               TALLOC_CTX *mem_ctx,
-                                               uint32_t count,
-                                               struct id_map *ids)
-{
-       struct composite_context *ctx;
-       struct wbc_idmap_state *state;
-       struct tevent_req *subreq;
-
-       DEBUG(5, ("wbc_xids_to_sids called\n"));
-
-       ctx = composite_create(mem_ctx, wbc_ctx->event_ctx);
-       if (ctx == NULL) return NULL;
-
-       state = talloc(ctx, struct wbc_idmap_state);
-       if (composite_nomem(state, ctx)) return ctx;
-       ctx->private_data = state;
-
-       state->req = talloc(state, struct winbind_get_idmap);
-       if (composite_nomem(state->req, ctx)) return ctx;
-
-       state->req->in.count = count;
-       state->req->in.level = WINBIND_IDMAP_LEVEL_XIDS_TO_SIDS;
-       state->req->in.ids = ids;
-       state->ctx = ctx;
-
-       subreq = dcerpc_winbind_get_idmap_r_send(state,
-                                                wbc_ctx->event_ctx,
-                                                wbc_ctx->irpc_handle,
-                                                state->req);
-       if (composite_nomem(subreq, ctx)) return ctx;
-
-       tevent_req_set_callback(subreq, xids_to_sids_recv_ids, state);
-
-       return ctx;
-}
-
-static void xids_to_sids_recv_ids(struct tevent_req *subreq)
-{
-       struct wbc_idmap_state *state =
-               tevent_req_callback_data(subreq,
-               struct wbc_idmap_state);
-
-       state->ctx->status = dcerpc_winbind_get_idmap_r_recv(subreq, state);
-       TALLOC_FREE(subreq);
-       if (!composite_is_ok(state->ctx)) return;
-
-       state->ids = state->req->out.ids;
-       composite_done(state->ctx);
-}
-
-NTSTATUS wbc_xids_to_sids_recv(struct composite_context *ctx,
-                              struct id_map **ids)
-{
-       NTSTATUS status = composite_wait(ctx);
-               DEBUG(5, ("wbc_xids_to_sids_recv called\n"));
-       if (NT_STATUS_IS_OK(status)) {
-               struct wbc_idmap_state *state = talloc_get_type_abort(
-                                                       ctx->private_data,
-                                                       struct wbc_idmap_state);
-               *ids = state->ids;
+       xids = talloc_array(mem_ctx, struct wbcUnixId, count);
+       if (xids == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       return status;
-}
-
-static int wb_simple_trans(struct tevent_context *ev, int fd,
-                          struct winbindd_request *wb_req,
-                          TALLOC_CTX *mem_ctx,
-                          struct winbindd_response **resp, int *err)
-{
-       struct tevent_req *req;
-       bool polled;
-       int ret;
-
-       req = wb_simple_trans_send(ev, ev, NULL, fd, wb_req);
-       if (req == NULL) {
-               *err = ENOMEM;
-               return -1;
+       for (i=0; i<count; i++) {
+               memcpy(&sids[i], ids[i].sid, sizeof(struct dom_sid));
        }
 
-       polled = tevent_req_poll(req, ev);
-       if (!polled) {
-               *err = errno;
-               DEBUG(10, ("tevent_req_poll returned %s\n",
-                          strerror(*err)));
-               return -1;
+       wb_off = winbind_env_set();
+       if (wb_off) {
+               (void)winbind_on();
        }
 
-       ret = wb_simple_trans_recv(req, mem_ctx, resp, err);
-       TALLOC_FREE(req);
-       return ret;
-}
-
-static const char *winbindd_socket_dir(void)
-{
-#ifdef SOCKET_WRAPPER
-       const char *env_dir;
+       result = wbcSidsToUnixIds(sids, count, xids);
 
-       env_dir = getenv(WINBINDD_SOCKET_DIR_ENVVAR);
-       if (env_dir) {
-               return env_dir;
+       if (wb_off) {
+               (void)winbind_off();
        }
-#endif
 
-       return WINBINDD_SOCKET_DIR;
-}
-
-static int winbindd_pipe_sock(void)
-{
-       struct sockaddr_un sunaddr = {};
-       int ret, fd;
-       char *path;
-
-       ret = asprintf(&path, "%s/%s", winbindd_socket_dir(),
-                      WINBINDD_SOCKET_NAME);
-       if (ret == -1) {
-               errno = ENOMEM;
-               return -1;
+       if (!WBC_ERROR_IS_OK(result)) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_INTERNAL_ERROR;
        }
-       sunaddr.sun_family = AF_UNIX;
-       strlcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path));
-       free(path);
 
-       fd = socket(AF_UNIX, SOCK_STREAM, 0);
-       if (fd == -1) {
-               return -1;
-       }
+       for (i=0; i<count; i++) {
+               struct wbcUnixId *xid = &xids[i];
+               struct unixid *id = &ids[i].xid;
 
-       ret = connect(fd, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
-       if (ret == -1) {
-               int err = errno;
-               close(fd);
-               errno = err;
-               return -1;
+               switch (xid->type) {
+                   case WBC_ID_TYPE_UID:
+                       id->type = ID_TYPE_UID;
+                       id->id = xid->id.uid;
+                       break;
+                   case WBC_ID_TYPE_GID:
+                       id->type = ID_TYPE_GID;
+                       id->id = xid->id.gid;
+                       break;
+                   case WBC_ID_TYPE_BOTH:
+                       id->type = ID_TYPE_BOTH;
+                       id->id = xid->id.uid;
+                       break;
+                   case WBC_ID_TYPE_NOT_SPECIFIED:
+                       id->type = ID_TYPE_NOT_SPECIFIED;
+                       id->id = UINT32_MAX;
+                       break;
+               }
+               ids[i].status = ID_MAPPED;
        }
 
-       return fd;
+       TALLOC_FREE(mem_ctx);
+
+       return NT_STATUS_OK;
 }
 
-NTSTATUS wbc_sids_to_xids(struct tevent_context *ev, struct id_map *ids,
+NTSTATUS wbc_xids_to_sids(struct tevent_context *ev, struct id_map *ids,
                          uint32_t count)
 {
        TALLOC_CTX *mem_ctx;
-       struct winbindd_request req = {};
-       struct winbindd_response *resp;
        uint32_t i;
-       int fd, ret, err;
-       char *sids, *p;
-       size_t sidslen;
-
-       fd = winbindd_pipe_sock();
-       if (fd == -1) {
-               return map_nt_error_from_unix_common(errno);
-       }
+       struct wbcDomainSid *sids;
+       struct wbcUnixId *xids;
+       wbcErr result;
+       bool wb_off;
 
        mem_ctx = talloc_new(NULL);
        if (mem_ctx == NULL) {
-               close(fd);
                return NT_STATUS_NO_MEMORY;
        }
 
-       sidslen = count * (DOM_SID_STR_BUFLEN + 1);
-
-       sids = talloc_array(mem_ctx, char, sidslen);
+       sids = talloc_array(mem_ctx, struct wbcDomainSid, count);
        if (sids == NULL) {
-               close(fd);
                TALLOC_FREE(mem_ctx);
                return NT_STATUS_NO_MEMORY;
        }
 
-       p = sids;
+       xids = talloc_array(mem_ctx, struct wbcUnixId, count);
+       if (xids == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
+
        for (i=0; i<count; i++) {
-               p += dom_sid_string_buf(ids[i].sid, p, sidslen - (p - sids));
-               *p++ = '\n';
+               struct id_map *id = &ids[i];
+               struct wbcUnixId *xid = &xids[i];
+
+               switch (id->xid.type) {
+                   case ID_TYPE_UID:
+                           *xid = (struct wbcUnixId) {
+                                   .type = WBC_ID_TYPE_UID,
+                                   .id.uid = id->xid.id
+                           };
+                           break;
+                   case ID_TYPE_GID:
+                           *xid = (struct wbcUnixId) {
+                                   .type = WBC_ID_TYPE_GID,
+                                   .id.uid = id->xid.id
+                           };
+                           break;
+                   default:
+                           TALLOC_FREE(mem_ctx);
+                           return NT_STATUS_NOT_FOUND;
+               }
        }
-       *p++ = '\0';
 
-       DEBUG(10, ("sids=\n%s", sids));
+       wb_off = winbind_env_set();
+       if (wb_off) {
+               (void)winbind_on();
+       }
 
-       req.length = sizeof(struct winbindd_request);
-       req.cmd = WINBINDD_SIDS_TO_XIDS;
-       req.pid = getpid();
-       req.extra_data.data = sids;
-       req.extra_len = sidslen;
+       result = wbcUnixIdsToSids(xids, count, sids);
 
-       ret = wb_simple_trans(ev, fd, &req, mem_ctx, &resp, &err);
-       if (ret == -1) {
-               return map_nt_error_from_unix_common(err);
+       if (wb_off) {
+               (void)winbind_off();
        }
 
-       close(fd);
-
-       p = resp->extra_data.data;
+       if (!WBC_ERROR_IS_OK(result)) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_INTERNAL_ERROR;
+       }
 
        for (i=0; i<count; i++) {
-               struct unixid *id = &ids[i].xid;
-               char *q;
-
-               switch (p[0]) {
-               case 'U':
-                       id->type = ID_TYPE_UID;
-                       id->id = strtoul(p+1, &q, 10);
-                       break;
-               case 'G':
-                       id->type = ID_TYPE_GID;
-                       id->id = strtoul(p+1, &q, 10);
-                       break;
-               case 'B':
-                       id->type = ID_TYPE_BOTH;
-                       id->id = strtoul(p+1, &q, 10);
-                       break;
-               default:
-                       id->type = ID_TYPE_NOT_SPECIFIED;
-                       id->id = UINT32_MAX;
-                       q = strchr(p, '\n');
-                       break;
-               };
-               ids[i].status = ID_MAPPED;
-
-               if (q == NULL || q[0] != '\n') {
-                       TALLOC_FREE(mem_ctx);
-                       return NT_STATUS_INTERNAL_ERROR;
+               struct wbcDomainSid *sid = &sids[i];
+               struct wbcDomainSid null_sid = { 0 };
+               struct id_map *id = &ids[i];
+
+               if (memcmp(sid, &null_sid, sizeof(*sid)) != 0) {
+                       struct dom_sid domsid;
+                       id->status = ID_MAPPED;
+
+                       memcpy(&domsid, sid, sizeof(struct dom_sid));
+                       id->sid = dom_sid_dup(ids, &domsid);
+                       if (id->sid == NULL) {
+                               TALLOC_FREE(mem_ctx);
+                               return NT_STATUS_NO_MEMORY;
+                       }
+               } else {
+                       id->status = ID_UNMAPPED;
+                       id->sid = NULL;
                }
-               p = q+1;
        }
 
+       TALLOC_FREE(mem_ctx);
        return NT_STATUS_OK;
 }