s3:winbind: Convert WINBINDD_GETGRGID to the new API
[metze/samba/wip.git] / source3 / winbindd / winbindd_group.c
index 0d3db1d83fa463fa7b95c2f1df6021e6a7be97d8..f33b52f83fe15115622b1a056db9585463d9eb65 100644 (file)
@@ -181,9 +181,8 @@ static bool fill_passdb_alias_grmem(struct winbindd_domain *domain,
 
 /* Fill a grent structure from various other information */
 
-static bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
-                      const char *dom_name,
-                      char *gr_name, gid_t unix_gid)
+bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
+               const char *dom_name, const char *gr_name, gid_t unix_gid)
 {
        fstring full_group_name;
        char *mapped_name = NULL;
@@ -1008,57 +1007,6 @@ static void winbindd_getgrsid( struct winbindd_cli_state *state, const DOM_SID g
                                  getgrsid_lookupsid_recv, s );
 }
 
-
-static void getgrgid_recv(void *private_data, bool success, const char *sid)
-{
-       struct winbindd_cli_state *state = talloc_get_type_abort(private_data, struct winbindd_cli_state);
-       enum lsa_SidType name_type;
-       DOM_SID group_sid;
-
-       if (success) {
-               DEBUG(10,("getgrgid_recv: gid %lu has sid %s\n",
-                         (unsigned long)(state->request->data.gid), sid));
-
-               if (!string_to_sid(&group_sid, sid)) {
-                       DEBUG(1,("getgrgid_recv: Could not convert sid %s "
-                               "from string\n", sid));
-                       request_error(state);
-                       return;
-               }
-
-               winbindd_getgrsid(state, group_sid);
-               return;
-       }
-
-       /* Ok, this might be "ours", i.e. an alias */
-       if (pdb_gid_to_sid(state->request->data.gid, &group_sid) &&
-           lookup_sid(state->mem_ctx, &group_sid, NULL, NULL, &name_type) &&
-           (name_type == SID_NAME_ALIAS)) {
-               /* Hey, got an alias */
-               DEBUG(10,("getgrgid_recv: we have an alias with gid %lu and sid %s\n",
-                         (unsigned long)(state->request->data.gid), sid));
-               winbindd_getgrsid(state, group_sid);
-               return;
-       }
-
-       DEBUG(1, ("could not convert gid %lu to sid\n",
-                 (unsigned long)state->request->data.gid));
-       request_error(state);
-}
-
-/* Return a group structure from a gid number */
-void winbindd_getgrgid(struct winbindd_cli_state *state)
-{
-       gid_t gid = state->request->data.gid;
-
-       DEBUG(3, ("[%5lu]: getgrgid %lu\n",
-                 (unsigned long)state->pid,
-                 (unsigned long)gid));
-
-       /* always use the async interface */
-       winbindd_gid2sid_async(state->mem_ctx, gid, getgrgid_recv, state);
-}
-
 /*
  * set/get/endgrent functions
  */
@@ -1559,178 +1507,6 @@ struct getgroups_state {
        size_t num_token_gids;
 };
 
-static void getgroups_usersid_recv(void *private_data, bool success,
-                                  const DOM_SID *sid, enum lsa_SidType type);
-static void getgroups_tokensids_recv(void *private_data, bool success,
-                                    DOM_SID *token_sids, size_t num_token_sids);
-static void getgroups_sid2gid_recv(void *private_data, bool success, gid_t gid);
-
-void winbindd_getgroups(struct winbindd_cli_state *state)
-{
-       struct getgroups_state *s;
-       char *real_name = NULL;
-       NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
-
-       /* Ensure null termination */
-       state->request->data.username
-               [sizeof(state->request->data.username)-1]='\0';
-
-       DEBUG(3, ("[%5lu]: getgroups %s\n", (unsigned long)state->pid,
-                 state->request->data.username));
-
-       /* Parse domain and username */
-
-       s = TALLOC_P(state->mem_ctx, struct getgroups_state);
-       if (s == NULL) {
-               DEBUG(0, ("talloc failed\n"));
-               request_error(state);
-               return;
-       }
-
-       s->state = state;
-
-       nt_status = normalize_name_unmap(state->mem_ctx,
-                                        state->request->data.username,
-                                        &real_name);
-
-       /* Reset the real_name pointer if we didn't do anything
-          productive in the above call */
-       if (!NT_STATUS_IS_OK(nt_status) &&
-           !NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
-       {
-               real_name = state->request->data.username;
-       }
-
-       if (!parse_domain_user_talloc(state->mem_ctx, real_name,
-                                     &s->domname, &s->username)) {
-               DEBUG(5, ("Could not parse domain user: %s\n",
-                         real_name));
-
-               /* error out if we do not have nested group support */
-
-               if ( !lp_winbind_nested_groups() ) {
-                       request_error(state);
-                       return;
-               }
-
-               s->domname = talloc_strdup(state->mem_ctx,
-                                          get_global_sam_name());
-               s->username = talloc_strdup(state->mem_ctx,
-                                           state->request->data.username);
-       }
-
-       /* Get info for the domain (either by short domain name or
-          DNS name in the case of a UPN) */
-
-       s->domain = find_domain_from_name_noinit(s->domname);
-       if (!s->domain) {
-               char *p = strchr(s->username, '@');
-
-               if (p) {
-                       s->domain = find_domain_from_name_noinit(p+1);
-               }
-
-       }
-
-       if (s->domain == NULL) {
-               DEBUG(7, ("could not find domain entry for domain %s\n",
-                         s->domname));
-               request_error(state);
-               return;
-       }
-
-       if ( s->domain->primary && lp_winbind_trusted_domains_only()) {
-               DEBUG(7,("winbindd_getgroups: My domain -- rejecting "
-                        "getgroups() for %s\\%s.\n", s->domname,
-                        s->username));
-               request_error(state);
-               return;
-       }
-
-       /* Get rid and name type from name.  The following costs 1 packet */
-
-       winbindd_lookupname_async(state->mem_ctx,
-                                 s->domname, s->username,
-                                 getgroups_usersid_recv,
-                                 WINBINDD_GETGROUPS, s);
-}
-
-static void getgroups_usersid_recv(void *private_data, bool success,
-                                  const DOM_SID *sid, enum lsa_SidType type)
-{
-       struct getgroups_state *s =
-               (struct getgroups_state *)private_data;
-
-       if ((!success) ||
-           ((type != SID_NAME_USER) && (type != SID_NAME_COMPUTER))) {
-               request_error(s->state);
-               return;
-       }
-
-       sid_copy(&s->user_sid, sid);
-
-       winbindd_gettoken_async(s->state->mem_ctx, &s->user_sid,
-                               getgroups_tokensids_recv, s);
-}
-
-static void getgroups_tokensids_recv(void *private_data, bool success,
-                                    DOM_SID *token_sids, size_t num_token_sids)
-{
-       struct getgroups_state *s =
-               (struct getgroups_state *)private_data;
-
-       /* We need at least the user sid and the primary group in the token,
-        * otherwise it's an error */
-
-       if ((!success) || (num_token_sids < 2)) {
-               request_error(s->state);
-               return;
-       }
-
-       s->token_sids = token_sids;
-       s->num_token_sids = num_token_sids;
-       s->i = 0;
-
-       s->token_gids = NULL;
-       s->num_token_gids = 0;
-
-       getgroups_sid2gid_recv(s, False, 0);
-}
-
-static void getgroups_sid2gid_recv(void *private_data, bool success, gid_t gid)
-{
-       struct getgroups_state *s =
-               (struct getgroups_state *)private_data;
-
-       if (success) {
-               if (!add_gid_to_array_unique(s->state->mem_ctx, gid,
-                                       &s->token_gids,
-                                       &s->num_token_gids)) {
-                       return;
-               }
-       }
-
-       if (s->i < s->num_token_sids) {
-               const DOM_SID *sid = &s->token_sids[s->i];
-               s->i += 1;
-
-               if (sid_equal(sid, &s->user_sid)) {
-                       getgroups_sid2gid_recv(s, False, 0);
-                       return;
-               }
-
-               winbindd_sid2gid_async(s->state->mem_ctx, sid,
-                                      getgroups_sid2gid_recv, s);
-               return;
-       }
-
-       s->state->response->data.num_entries = s->num_token_gids;
-       if (s->num_token_gids) {
-               s->state->response->extra_data.data = s->token_gids;
-               s->state->response->length += s->num_token_gids * sizeof(gid_t);
-       }
-       request_ok(s->state);
-}
 
 /* Get user supplementary sids. This is equivalent to the
    winbindd_getgroups() function but it involves a SID->SIDs mapping
@@ -1815,32 +1591,6 @@ static void getusersids_recv(void *private_data, bool success, DOM_SID *sids,
        request_ok(state);
 }
 
-void winbindd_getuserdomgroups(struct winbindd_cli_state *state)
-{
-       DOM_SID user_sid;
-       struct winbindd_domain *domain;
-
-       /* Ensure null termination */
-       state->request->data.sid[sizeof(state->request->data.sid)-1]='\0';
-
-       if (!string_to_sid(&user_sid, state->request->data.sid)) {
-               DEBUG(1, ("Could not get convert sid %s from string\n",
-                         state->request->data.sid));
-               request_error(state);
-               return;
-       }
-
-       /* Get info for the domain */
-       if ((domain = find_domain_from_sid_noinit(&user_sid)) == NULL) {
-               DEBUG(0,("could not find domain entry for sid %s\n",
-                        sid_string_dbg(&user_sid)));
-               request_error(state);
-               return;
-       }
-
-       sendto_domain(state, domain);
-}
-
 enum winbindd_result winbindd_dual_getuserdomgroups(struct winbindd_domain *domain,
                                                    struct winbindd_cli_state *state)
 {
@@ -1970,4 +1720,76 @@ enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
        return WINBINDD_OK;
 }
 
+struct getgr_countmem {
+       int num;
+       size_t len;
+};
+
+static int getgr_calc_memberlen(DATA_BLOB key, void *data, void *priv)
+{
+       struct wbint_GroupMember *m = talloc_get_type_abort(
+               data, struct wbint_GroupMember);
+       struct getgr_countmem *buf = (struct getgr_countmem *)priv;
 
+       buf->num += 1;
+       buf->len += strlen(m->name) + 1;
+       return 0;
+}
+
+struct getgr_stringmem {
+       size_t ofs;
+       char *buf;
+};
+
+static int getgr_unparse_members(DATA_BLOB key, void *data, void *priv)
+{
+       struct wbint_GroupMember *m = talloc_get_type_abort(
+               data, struct wbint_GroupMember);
+       struct getgr_stringmem *buf = (struct getgr_stringmem *)priv;
+       int len;
+
+       len = strlen(m->name);
+
+       memcpy(buf->buf + buf->ofs, m->name, len);
+       buf->ofs += len;
+       buf->buf[buf->ofs] = ',';
+       buf->ofs += 1;
+       return 0;
+}
+
+NTSTATUS winbindd_print_groupmembers(struct talloc_dict *members,
+                                    TALLOC_CTX *mem_ctx,
+                                    int *num_members, char **result)
+{
+       struct getgr_countmem c;
+       struct getgr_stringmem m;
+       int res;
+
+       c.num = 0;
+       c.len = 0;
+
+       res = talloc_dict_traverse(members, getgr_calc_memberlen, &c);
+       if (res != 0) {
+               DEBUG(5, ("talloc_dict_traverse failed\n"));
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       m.ofs = 0;
+       m.buf = talloc_array(mem_ctx, char, c.len);
+       if (m.buf == NULL) {
+               DEBUG(5, ("talloc failed\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       res = talloc_dict_traverse(members, getgr_unparse_members, &m);
+       if (res != 0) {
+               DEBUG(5, ("talloc_dict_traverse failed\n"));
+               TALLOC_FREE(m.buf);
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+       m.buf[c.len-1] = '\0';
+
+       *num_members = c.num;
+       *result = m.buf;
+       return NT_STATUS_OK;
+}