s3-winbindd: use fill_domain_username_talloc() in winbind.
[Samba.git] / source3 / winbindd / winbindd_group.c
blob098d2f69113610767d031b4eeee6cf472cd3b09c
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2001.
8 Copyright (C) Gerald (Jerry) Carter 2003.
9 Copyright (C) Volker Lendecke 2005
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "winbindd.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_WINBIND
31 /* Fill a grent structure from various other information */
33 bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
34 const char *dom_name, const char *gr_name, gid_t unix_gid)
36 const char *full_group_name;
37 char *mapped_name = NULL;
38 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
40 nt_status = normalize_name_map(mem_ctx, dom_name, gr_name,
41 &mapped_name);
43 /* Basic whitespace replacement */
44 if (NT_STATUS_IS_OK(nt_status)) {
45 full_group_name = fill_domain_username_talloc(mem_ctx, dom_name,
46 mapped_name, true);
48 /* Mapped to an aliase */
49 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED)) {
50 full_group_name = mapped_name;
52 /* no change */
53 else {
54 full_group_name = fill_domain_username_talloc(mem_ctx, dom_name,
55 gr_name, True );
58 if (full_group_name == NULL) {
59 return false;
62 gr->gr_gid = unix_gid;
64 /* Group name and password */
66 strlcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name));
67 strlcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd));
69 return True;
72 struct getgr_countmem {
73 int num;
74 size_t len;
77 static int getgr_calc_memberlen(DATA_BLOB key, void *data, void *priv)
79 struct wbint_Principal *m = talloc_get_type_abort(
80 data, struct wbint_Principal);
81 struct getgr_countmem *buf = (struct getgr_countmem *)priv;
83 buf->num += 1;
84 buf->len += strlen(m->name) + 1;
85 return 0;
88 struct getgr_stringmem {
89 size_t ofs;
90 char *buf;
93 static int getgr_unparse_members(DATA_BLOB key, void *data, void *priv)
95 struct wbint_Principal *m = talloc_get_type_abort(
96 data, struct wbint_Principal);
97 struct getgr_stringmem *buf = (struct getgr_stringmem *)priv;
98 int len;
100 len = strlen(m->name);
102 memcpy(buf->buf + buf->ofs, m->name, len);
103 buf->ofs += len;
104 buf->buf[buf->ofs] = ',';
105 buf->ofs += 1;
106 return 0;
109 NTSTATUS winbindd_print_groupmembers(struct talloc_dict *members,
110 TALLOC_CTX *mem_ctx,
111 int *num_members, char **result)
113 struct getgr_countmem c;
114 struct getgr_stringmem m;
115 int res;
117 c.num = 0;
118 c.len = 0;
120 res = talloc_dict_traverse(members, getgr_calc_memberlen, &c);
121 if (res == -1) {
122 DEBUG(5, ("talloc_dict_traverse failed\n"));
123 return NT_STATUS_INTERNAL_ERROR;
126 m.ofs = 0;
127 m.buf = talloc_array(mem_ctx, char, c.len);
128 if (m.buf == NULL) {
129 DEBUG(5, ("talloc failed\n"));
130 return NT_STATUS_NO_MEMORY;
133 res = talloc_dict_traverse(members, getgr_unparse_members, &m);
134 if (res == -1) {
135 DEBUG(5, ("talloc_dict_traverse failed\n"));
136 TALLOC_FREE(m.buf);
137 return NT_STATUS_INTERNAL_ERROR;
139 m.buf[c.len-1] = '\0';
141 *num_members = c.num;
142 *result = m.buf;
143 return NT_STATUS_OK;