docs-xml: Fix rid idmap backend documentation
[Samba.git] / source3 / winbindd / winbindd_group.c
blobb5bb60fca6cfb09f8ea98db0cbf5b2811518e782
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"
27 #include "lib/dbwrap/dbwrap.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_WINBIND
32 /* Fill a grent structure from various other information */
34 bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
35 const char *dom_name, const char *gr_name, gid_t unix_gid)
37 const char *full_group_name;
38 char *mapped_name = NULL;
39 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
41 nt_status = normalize_name_map(mem_ctx, dom_name, gr_name,
42 &mapped_name);
44 D_DEBUG("Filling domain '%s' and group '%s'.\n", dom_name, gr_name);
45 /* Basic whitespace replacement */
46 if (NT_STATUS_IS_OK(nt_status)) {
47 full_group_name = fill_domain_username_talloc(mem_ctx, dom_name,
48 mapped_name, true);
50 /* Mapped to an aliase */
51 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED)) {
52 full_group_name = mapped_name;
54 /* no change */
55 else {
56 full_group_name = fill_domain_username_talloc(mem_ctx, dom_name,
57 gr_name, True );
60 if (full_group_name == NULL) {
61 D_DEBUG("Returning false, since there is no full group name.\n");
62 return false;
65 gr->gr_gid = unix_gid;
67 /* Group name and password */
69 strlcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name));
70 strlcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd));
72 D_DEBUG("Returning true. Full group name is '%s'.\n", gr_name);
73 return True;
76 struct getgr_countmem {
77 int num;
78 size_t len;
81 static int getgr_calc_memberlen(struct db_record *rec, void *private_data)
83 struct getgr_countmem *buf = private_data;
84 TDB_DATA data = dbwrap_record_get_value(rec);
85 size_t len;
87 buf->num += 1;
89 len = buf->len + data.dsize;
90 if (len < buf->len) {
91 return 0;
93 buf->len = len;
94 return 0;
97 struct getgr_stringmem {
98 size_t ofs;
99 char *buf;
102 static int getgr_unparse_members(struct db_record *rec, void *private_data)
104 struct getgr_stringmem *buf = private_data;
105 TDB_DATA data = dbwrap_record_get_value(rec);
106 int len;
108 len = data.dsize-1;
110 memcpy(buf->buf + buf->ofs, data.dptr, len);
111 buf->ofs += len;
112 buf->buf[buf->ofs] = ',';
113 buf->ofs += 1;
114 return 0;
117 NTSTATUS winbindd_print_groupmembers(struct db_context *members,
118 TALLOC_CTX *mem_ctx,
119 int *num_members, char **result)
121 struct getgr_countmem c;
122 struct getgr_stringmem m;
123 int count;
124 NTSTATUS status;
126 c.num = 0;
127 c.len = 0;
129 status = dbwrap_traverse(members, getgr_calc_memberlen, &c, &count);
130 if (!NT_STATUS_IS_OK(status)) {
131 DBG_NOTICE("dbwrap_traverse failed: %s\n", nt_errstr(status));
132 return status;
135 m.ofs = 0;
136 m.buf = talloc_array(mem_ctx, char, c.len);
137 if (m.buf == NULL) {
138 D_WARNING("talloc failed\n");
139 return NT_STATUS_NO_MEMORY;
142 status = dbwrap_traverse(members, getgr_unparse_members, &m, &count);
143 if (!NT_STATUS_IS_OK(status)) {
144 TALLOC_FREE(m.buf);
145 DBG_NOTICE("dbwrap_traverse failed: %s\n", nt_errstr(status));
146 return status;
148 if (c.len > 0) {
149 m.buf[c.len - 1] = '\0';
152 *num_members = c.num;
153 *result = m.buf;
154 D_DEBUG("Returning %d member(s).\n", *num_members);
155 return NT_STATUS_OK;