Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : Winbind daemon for ntdom nss module
5 :
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
10 :
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.
15 :
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.
20 :
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/>.
23 : */
24 :
25 : #include "includes.h"
26 : #include "winbindd.h"
27 : #include "lib/dbwrap/dbwrap.h"
28 :
29 : #undef DBGC_CLASS
30 : #define DBGC_CLASS DBGC_WINBIND
31 :
32 : /* Fill a grent structure from various other information */
33 :
34 5720 : bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
35 : const char *dom_name, const char *gr_name, gid_t unix_gid)
36 : {
37 0 : const char *full_group_name;
38 5720 : char *mapped_name = NULL;
39 5720 : NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
40 :
41 5720 : nt_status = normalize_name_map(mem_ctx, dom_name, gr_name,
42 : &mapped_name);
43 :
44 5720 : D_DEBUG("Filling domain '%s' and group '%s'.\n", dom_name, gr_name);
45 : /* Basic whitespace replacement */
46 5720 : if (NT_STATUS_IS_OK(nt_status)) {
47 0 : full_group_name = fill_domain_username_talloc(mem_ctx, dom_name,
48 : mapped_name, true);
49 : }
50 : /* Mapped to an alias */
51 5720 : else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED)) {
52 0 : full_group_name = mapped_name;
53 : }
54 : /* no change */
55 : else {
56 5720 : full_group_name = fill_domain_username_talloc(mem_ctx, dom_name,
57 : gr_name, True );
58 : }
59 :
60 5720 : if (full_group_name == NULL) {
61 0 : D_DEBUG("Returning false, since there is no full group name.\n");
62 0 : return false;
63 : }
64 :
65 5720 : gr->gr_gid = unix_gid;
66 :
67 : /* Group name and password */
68 :
69 5720 : strlcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name));
70 5720 : strlcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd));
71 :
72 5720 : D_DEBUG("Returning true. Full group name is '%s'.\n", gr_name);
73 5720 : return True;
74 : }
75 :
76 : struct getgr_countmem {
77 : int num;
78 : size_t len;
79 : };
80 :
81 36 : static int getgr_calc_memberlen(struct db_record *rec, void *private_data)
82 : {
83 36 : struct getgr_countmem *buf = private_data;
84 36 : TDB_DATA data = dbwrap_record_get_value(rec);
85 0 : size_t len;
86 :
87 36 : buf->num += 1;
88 :
89 36 : len = buf->len + data.dsize;
90 36 : if (len < buf->len) {
91 0 : return 0;
92 : }
93 36 : buf->len = len;
94 36 : return 0;
95 : }
96 :
97 : struct getgr_stringmem {
98 : size_t ofs;
99 : char *buf;
100 : };
101 :
102 36 : static int getgr_unparse_members(struct db_record *rec, void *private_data)
103 : {
104 36 : struct getgr_stringmem *buf = private_data;
105 36 : TDB_DATA data = dbwrap_record_get_value(rec);
106 0 : int len;
107 :
108 36 : len = data.dsize-1;
109 :
110 36 : memcpy(buf->buf + buf->ofs, data.dptr, len);
111 36 : buf->ofs += len;
112 36 : buf->buf[buf->ofs] = ',';
113 36 : buf->ofs += 1;
114 36 : return 0;
115 : }
116 :
117 5720 : NTSTATUS winbindd_print_groupmembers(struct db_context *members,
118 : TALLOC_CTX *mem_ctx,
119 : int *num_members, char **result)
120 : {
121 0 : struct getgr_countmem c;
122 0 : struct getgr_stringmem m;
123 0 : int count;
124 0 : NTSTATUS status;
125 :
126 5720 : c.num = 0;
127 5720 : c.len = 0;
128 :
129 5720 : status = dbwrap_traverse(members, getgr_calc_memberlen, &c, &count);
130 5720 : if (!NT_STATUS_IS_OK(status)) {
131 0 : DBG_NOTICE("dbwrap_traverse failed: %s\n", nt_errstr(status));
132 0 : return status;
133 : }
134 :
135 5720 : m.ofs = 0;
136 5720 : m.buf = talloc_array(mem_ctx, char, c.len);
137 5720 : if (m.buf == NULL) {
138 0 : D_WARNING("talloc failed\n");
139 0 : return NT_STATUS_NO_MEMORY;
140 : }
141 :
142 5720 : status = dbwrap_traverse(members, getgr_unparse_members, &m, &count);
143 5720 : if (!NT_STATUS_IS_OK(status)) {
144 0 : TALLOC_FREE(m.buf);
145 0 : DBG_NOTICE("dbwrap_traverse failed: %s\n", nt_errstr(status));
146 0 : return status;
147 : }
148 5720 : if (c.len > 0) {
149 16 : m.buf[c.len - 1] = '\0';
150 : }
151 :
152 5720 : *num_members = c.num;
153 5720 : *result = m.buf;
154 5720 : D_DEBUG("Returning %d member(s).\n", *num_members);
155 5720 : return NT_STATUS_OK;
156 : }
|