wb-ndr: implement winbindd_ndr_child_get_idmap()
authorStefan Metzmacher <metze@sernet.de>
Wed, 12 Dec 2007 10:42:44 +0000 (11:42 +0100)
committerStefan Metzmacher <metze@sernet.de>
Fri, 2 May 2008 14:12:35 +0000 (16:12 +0200)
metze

source/winbindd/winbindd_idmap.c

index 41782ff0d151c8c75c94db44f30223a71de99309..505cba93ce68ca1fa108d0af217bf3dbde237d91 100644 (file)
@@ -54,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)
@@ -443,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,
        }