wb-ndr: implement winbindd_ndr_child_get_idmap()
[metze/samba/wb-ndr.git] / source / winbindd / winbindd_idmap.c
index dd63e1823697eea0d7d868a46ddd13eb24e9743c..505cba93ce68ca1fa108d0af217bf3dbde237d91 100644 (file)
@@ -44,10 +44,9 @@ static struct winbindd_child static_idmap_child;
 
 void init_idmap_child(void)
 {
-       setup_domain_child(NULL,
-                          &static_idmap_child,
-                          idmap_dispatch_table,
-                          "idmap");
+       setup_child(&static_idmap_child,
+                   idmap_dispatch_table,
+                   "log.winbindd", "idmap");
 }
 
 struct winbindd_child *idmap_child(void)
@@ -55,6 +54,175 @@ struct winbindd_child *idmap_child(void)
        return &static_idmap_child;
 }
 
+static void ndr_child_get_idmap_sid2uid(struct winbindd_domain *domain,
+                                       struct winbindd_cli_state *state,
+                                       struct winbind_get_idmap *r)
+{
+       uid_t uid;
+       NTSTATUS result;
+
+       DEBUG(3, ("sid to uid '%s'\n", sid_string_tos(r->in.req.sid)));
+
+       /* Find uid for this sid and return it, possibly ask the slow remote idmap */
+
+       result = idmap_sid_to_uid(r->in.req.sid, &uid);
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(1, ("Can't map '%s' to uid: %s\n",
+                       sid_string_tos(r->in.req.sid),
+                       nt_errstr(result)));
+               r->out.result = WINBIND_STATUS_FOOBAR;
+               return;
+       }
+
+       DEBUG(10, ("sid %s mapped to uid %u\n",
+               sid_string_tos(r->in.req.sid), uid));
+
+       r->out.rep->uid = uid;
+       r->out.result = WINBIND_STATUS_OK;
+}
+
+static void ndr_child_get_idmap_sid2gid(struct winbindd_domain *domain,
+                                       struct winbindd_cli_state *state,
+                                       struct winbind_get_idmap *r)
+{
+       gid_t gid;
+       NTSTATUS result;
+
+       DEBUG(3, ("sid to gid '%s'\n", sid_string_tos(r->in.req.sid)));
+
+       /* Find uid for this sid and return it, possibly ask the slow remote idmap */
+
+       result = idmap_sid_to_gid(r->in.req.sid, &gid);
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(1, ("Can't map '%s' to gid: %s\n",
+                       sid_string_tos(r->in.req.sid),
+                       nt_errstr(result)));
+               r->out.result = WINBIND_STATUS_FOOBAR;
+               return;
+       }
+
+       DEBUG(10, ("sid %s mapped to gid %u\n",
+               sid_string_tos(r->in.req.sid), gid));
+
+       r->out.rep->gid = gid;
+       r->out.result = WINBIND_STATUS_OK;
+}
+
+static void ndr_child_get_idmap_uid2sid(struct winbindd_domain *domain,
+                                       struct winbindd_cli_state *state,
+                                       struct winbind_get_idmap *r)
+{
+       DOM_SID sid;
+       uid_t uid;
+       NTSTATUS result;
+
+       DEBUG(3, ("uid to sid '%llu'\n",
+               (unsigned long long)r->in.req.uid));
+
+       /* the IDMAP subsystem only knows about uint32_t id's yet */
+       if (r->in.req.uid > UINT32_MAX) {
+               DEBUG(1, ("Can't map uid '%llu' to sid\n",
+                       (unsigned long long)r->in.req.uid));
+               r->out.result = WINBIND_STATUS_FOOBAR;
+               return;
+       }
+
+       /* Find uid for this sid and return it, possibly ask the slow remote idmap */
+
+       uid = r->in.req.uid;
+
+       result = idmap_uid_to_sid(&sid, uid);
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(1, ("Can't map uid '%u' to sid: %s\n",
+                       uid, nt_errstr(result)));
+               r->out.result = WINBIND_STATUS_FOOBAR;
+               return;
+       }
+
+       DEBUG(10, ("uid %u mapped to sid %s\n",
+               uid, sid_string_tos(&sid)));
+
+       r->out.rep->sid = sid_dup_talloc(r, &sid);
+       if (!r->out.rep->sid) {
+               r->out.result = WINBIND_STATUS_NO_MEMORY;
+               return;
+       }
+
+       r->out.result = WINBIND_STATUS_OK;
+}
+
+static void ndr_child_get_idmap_gid2sid(struct winbindd_domain *domain,
+                                       struct winbindd_cli_state *state,
+                                       struct winbind_get_idmap *r)
+{
+       DOM_SID sid;
+       gid_t gid;
+       NTSTATUS result;
+
+       DEBUG(3, ("gid to sid '%llu'\n",
+               (unsigned long long)r->in.req.gid));
+
+       /* the IDMAP subsystem only knows about uint32_t id's yet */
+       if (r->in.req.gid > UINT32_MAX) {
+               DEBUG(1, ("Can't map gid '%llu' to sid\n",
+                       (unsigned long long)r->in.req.gid));
+               r->out.result = WINBIND_STATUS_FOOBAR;
+               return;
+       }
+
+       /* Find uid for this sid and return it, possibly ask the slow remote idmap */
+       gid = r->in.req.gid;
+
+       result = idmap_gid_to_sid(&sid, gid);
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(1, ("Can't map gid '%u' to sid: %s\n",
+                       gid, nt_errstr(result)));
+               r->out.result = WINBIND_STATUS_FOOBAR;
+               return;
+       }
+
+       DEBUG(10, ("gid %u mapped to sid %s\n",
+               gid, sid_string_tos(&sid)));
+
+       r->out.rep->sid = sid_dup_talloc(r, &sid);
+       if (!r->out.rep->sid) {
+               r->out.result = WINBIND_STATUS_NO_MEMORY;
+               return;
+       }
+
+       r->out.result = WINBIND_STATUS_OK;
+}
+
+void winbindd_ndr_child_get_idmap(struct winbindd_domain *domain,
+                                 struct winbindd_cli_state *state)
+{
+       struct winbind_get_idmap *r;
+
+       r = talloc_get_type_abort(state->c.ndr.r,
+                                 struct winbind_get_idmap);
+
+       switch (*r->in.level) {
+       case WINBIND_IDMAP_LEVEL_SID_TO_UID:
+               ndr_child_get_idmap_sid2uid(domain, state, r);
+               return;
+
+       case WINBIND_IDMAP_LEVEL_SID_TO_GID:
+               ndr_child_get_idmap_sid2gid(domain, state, r);
+               return;
+
+       case WINBIND_IDMAP_LEVEL_UID_TO_SID:
+               ndr_child_get_idmap_uid2sid(domain, state, r);
+               return;
+
+       case WINBIND_IDMAP_LEVEL_GID_TO_SID:
+               ndr_child_get_idmap_gid2sid(domain, state, r);
+               return;
+       }
+
+       r->out.result = WINBIND_STATUS_UNKNOWN_LEVEL;
+       return;
+}
+
 static void winbindd_set_mapping_recv(TALLOC_CTX *mem_ctx, bool success,
                                   struct winbindd_response *response,
                                   void *c, void *private_data)
@@ -85,7 +253,7 @@ void winbindd_set_mapping_async(TALLOC_CTX *mem_ctx, const struct id_map *map,
        request.cmd = WINBINDD_DUAL_SET_MAPPING;
        request.data.dual_idmapset.id = map->xid.id;
        request.data.dual_idmapset.type = map->xid.type;
-       sid_to_string(request.data.dual_idmapset.sid, map->sid);
+       sid_to_fstring(request.data.dual_idmapset.sid, map->sid);
 
        do_async(mem_ctx, idmap_child(), &request, winbindd_set_mapping_recv,
                 (void *)cont, private_data);
@@ -171,108 +339,6 @@ enum winbindd_result winbindd_dual_set_hwm(struct winbindd_domain *domain,
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
 
-static void winbindd_sids2xids_recv(TALLOC_CTX *mem_ctx, bool success,
-                              struct winbindd_response *response,
-                              void *c, void *private_data)
-{
-       void (*cont)(void *priv, bool succ, void *, int) =
-               (void (*)(void *, bool, void *, int))c;
-
-       if (!success) {
-               DEBUG(5, ("Could not trigger sids2xids\n"));
-               cont(private_data, False, NULL, 0);
-               return;
-       }
-
-       if (response->result != WINBINDD_OK) {
-               DEBUG(5, ("sids2xids returned an error\n"));
-               cont(private_data, False, NULL, 0);
-               return;
-       }
-
-       cont(private_data, True, response->extra_data.data, response->length - sizeof(response));
-}
-
-void winbindd_sids2xids_async(TALLOC_CTX *mem_ctx, void *sids, int size,
-                        void (*cont)(void *private_data, bool success, void *data, int len),
-                        void *private_data)
-{
-       struct winbindd_request request;
-       ZERO_STRUCT(request);
-       request.cmd = WINBINDD_DUAL_SIDS2XIDS;
-       request.extra_data.data = (char *)sids;
-       request.extra_len = size;
-       do_async(mem_ctx, idmap_child(), &request, winbindd_sids2xids_recv,
-                (void *)cont, private_data);
-}
-
-enum winbindd_result winbindd_dual_sids2xids(struct winbindd_domain *domain,
-                                          struct winbindd_cli_state *state)
-{
-       DOM_SID *sids;
-       struct unixid *xids;
-       struct id_map **ids;
-       NTSTATUS result;
-       int num, i;
-
-       DEBUG(3, ("[%5lu]: sids to unix ids\n", (unsigned long)state->pid));
-
-       if (state->request.extra_len == 0) {
-               DEBUG(0, ("Invalid buffer size!\n"));
-               return WINBINDD_ERROR;
-       }
-
-       sids = (DOM_SID *)state->request.extra_data.data;
-       num = state->request.extra_len / sizeof(DOM_SID);
-
-       ids = TALLOC_ZERO_ARRAY(state->mem_ctx, struct id_map *, num + 1);
-       if ( ! ids) {
-               DEBUG(0, ("Out of memory!\n"));
-               return WINBINDD_ERROR;
-       }
-       for (i = 0; i < num; i++) {
-               ids[i] = TALLOC_P(ids, struct id_map);
-               if ( ! ids[i]) {
-                       DEBUG(0, ("Out of memory!\n"));
-                       talloc_free(ids);
-                       return WINBINDD_ERROR;
-               }
-               ids[i]->sid = &sids[i];
-       }
-
-       result = idmap_sids_to_unixids(ids);
-
-       if (NT_STATUS_IS_OK(result)) {
-
-               xids = SMB_MALLOC_ARRAY(struct unixid, num);
-               if ( ! xids) {
-                       DEBUG(0, ("Out of memory!\n"));
-                       talloc_free(ids);
-                       return WINBINDD_ERROR;
-               }
-
-               for (i = 0; i < num; i++) {
-                       if (ids[i]->status == ID_MAPPED) {
-                               xids[i].type = ids[i]->xid.type;
-                               xids[i].id = ids[i]->xid.id;
-                       } else {
-                               xids[i].type = -1;
-                       }
-               }
-
-               state->response.length = sizeof(state->response) + (sizeof(struct unixid) * num);
-               state->response.extra_data.data = xids;
-
-       } else {
-               DEBUG (2, ("idmap_sids_to_unixids returned an error: 0x%08x\n", NT_STATUS_V(result)));
-               talloc_free(ids);
-               return WINBINDD_ERROR;
-       }
-
-       talloc_free(ids);
-       return WINBINDD_OK;
-}
-
 static void winbindd_sid2uid_recv(TALLOC_CTX *mem_ctx, bool success,
                               struct winbindd_response *response,
                               void *c, void *private_data)
@@ -302,7 +368,7 @@ void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
        struct winbindd_request request;
        ZERO_STRUCT(request);
        request.cmd = WINBINDD_DUAL_SID2UID;
-       sid_to_string(request.data.dual_sid2id.sid, sid);
+       sid_to_fstring(request.data.dual_sid2id.sid, sid);
        do_async(mem_ctx, idmap_child(), &request, winbindd_sid2uid_recv,
                 (void *)cont, private_data);
 }
@@ -358,7 +424,7 @@ void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
        struct winbindd_request request;
        ZERO_STRUCT(request);
        request.cmd = WINBINDD_DUAL_SID2GID;
-       sid_to_string(request.data.dual_sid2id.sid, sid);
+       sid_to_fstring(request.data.dual_sid2id.sid, sid);
 
        DEBUG(7,("winbindd_sid2gid_async: Resolving %s to a gid\n",
                request.data.dual_sid2id.sid));
@@ -386,7 +452,9 @@ enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
 
        result = idmap_sid_to_gid(&sid, &state->response.data.gid);
 
-       DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n", NT_STATUS_V(result), sid_string_static(&sid), state->response.data.gid));
+       DEBUG(10, ("winbindd_dual_sid2gid: 0x%08x - %s - %u\n",
+                  NT_STATUS_V(result), sid_string_dbg(&sid),
+                  state->response.data.gid));
 
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
@@ -443,7 +511,7 @@ enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
        result = idmap_uid_to_sid(&sid, state->request.data.uid);
 
        if (NT_STATUS_IS_OK(result)) {
-               sid_to_string(state->response.data.sid.sid, &sid);
+               sid_to_fstring(state->response.data.sid.sid, &sid);
                state->response.data.sid.type = SID_NAME_USER;
                return WINBINDD_OK;
        }
@@ -500,7 +568,7 @@ enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
        result = idmap_gid_to_sid(&sid, state->request.data.gid);
 
        if (NT_STATUS_IS_OK(result)) {
-               sid_to_string(state->response.data.sid.sid, &sid);
+               sid_to_fstring(state->response.data.sid.sid, &sid);
                DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
                           (unsigned long)state->pid,
                           state->response.data.sid.sid));
@@ -511,51 +579,6 @@ enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
        return WINBINDD_ERROR;
 }
 
-static void winbindd_dump_id_maps_recv(TALLOC_CTX *mem_ctx, bool success,
-                              struct winbindd_response *response,
-                              void *c, void *private_data)
-{
-       void (*cont)(void *priv, bool succ) =
-               (void (*)(void *, bool))c;
-
-       if (!success) {
-               DEBUG(5, ("Could not trigger a map dump\n"));
-               cont(private_data, False);
-               return;
-       }
-
-       if (response->result != WINBINDD_OK) {
-               DEBUG(5, ("idmap dump maps returned an error\n"));
-               cont(private_data, False);
-               return;
-       }
-
-       cont(private_data, True);
-}
-
-void winbindd_dump_maps_async(TALLOC_CTX *mem_ctx, const char *logfile,
-                        void (*cont)(void *private_data, bool success),
-                        void *private_data)
-{
-       struct winbindd_request request;
-       ZERO_STRUCT(request);
-       request.cmd = WINBINDD_DUAL_DUMP_MAPS;
-       request.extra_data.data = discard_const(logfile);
-       request.extra_len = strlen(logfile)+1;
-       do_async(mem_ctx, idmap_child(), &request, winbindd_dump_id_maps_recv,
-                (void *)cont, private_data);
-}
-
-enum winbindd_result winbindd_dual_dump_maps(struct winbindd_domain *domain,
-                                          struct winbindd_cli_state *state)
-{
-       DEBUG(3, ("[%5lu]: dual dump maps\n", (unsigned long)state->pid));
-
-       idmap_dump_maps((char *)state->request.extra_data.data);
-
-       return WINBINDD_OK;
-}
-
 static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
        {
                .name           = "DUAL_SID2UID",
@@ -565,12 +588,6 @@ static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
                .name           = "DUAL_SID2GID",
                .struct_cmd     = WINBINDD_DUAL_SID2GID,
                .struct_fn      = winbindd_dual_sid2gid,
-#if 0   /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
-       },{
-               .name           = "DUAL_SIDS2XIDS",
-               .struct_cmd     = WINBINDD_DUAL_SIDS2XIDS,
-               .struct_fn      = winbindd_dual_sids2xids,
-#endif  /* end DISABLED */
        },{
                .name           = "DUAL_UID2SID",
                .struct_cmd     = WINBINDD_DUAL_UID2SID,
@@ -587,10 +604,6 @@ static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
                .name           = "DUAL_SET_HWMS",
                .struct_cmd     = WINBINDD_DUAL_SET_HWM,
                .struct_fn      = winbindd_dual_set_hwm,
-       },{
-               .name           = "DUAL_DUMP_MAPS",
-               .struct_cmd     = WINBINDD_DUAL_DUMP_MAPS,
-               .struct_fn      = winbindd_dual_dump_maps,
        },{
                .name           = "ALLOCATE_UID",
                .struct_cmd     = WINBINDD_ALLOCATE_UID,
@@ -599,6 +612,10 @@ static const struct winbindd_child_dispatch_table idmap_dispatch_table[] = {
                .name           = "ALLOCATE_GID",
                .struct_cmd     = WINBINDD_ALLOCATE_GID,
                .struct_fn      = winbindd_dual_allocate_gid,
+       },{
+               .name           = "NDR_WINBIND_GET_IDMAP",
+               .ndr_opnum      = NDR_WINBIND_GET_IDMAP,
+               .ndr_fn         = winbindd_ndr_child_get_idmap,
        },{
                .name           = NULL,
        }