summaryrefslogtreecommitdiffstats
path: root/source3/winbindd/winbindd_getgrent.c
blob: 21da75b4c52dda5747b1a54c37c1ff15df4a3503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
   Unix SMB/CIFS implementation.
   async implementation of WINBINDD_GETGRENT
   Copyright (C) Volker Lendecke 2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "winbindd.h"

struct winbindd_getgrent_state {
	struct tevent_context *ev;
	struct winbindd_cli_state *cli;
	int max_groups;
	int num_groups;
	struct winbindd_gr *groups;
	struct talloc_dict **members;
};

static void winbindd_getgrent_done(struct tevent_req *subreq);

struct tevent_req *winbindd_getgrent_send(TALLOC_CTX *mem_ctx,
					  struct tevent_context *ev,
					  struct winbindd_cli_state *cli,
					  struct winbindd_request *request)
{
	struct tevent_req *req, *subreq;
	struct winbindd_getgrent_state *state;

	req = tevent_req_create(mem_ctx, &state,
				struct winbindd_getgrent_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->num_groups = 0;
	state->cli = cli;

	DEBUG(3, ("[%5lu]: getgrent\n", (unsigned long)cli->pid));

	if (!lp_winbind_enum_groups()) {
		tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
		return tevent_req_post(req, ev);
	}

	if (cli->grent_state == NULL) {
		tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
		return tevent_req_post(req, ev);
	}

	state->max_groups = MIN(500, request->data.num_entries);
	if (state->max_groups == 0) {
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return tevent_req_post(req, ev);
	}

	state->groups = talloc_zero_array(state, struct winbindd_gr,
					  state->max_groups);
	if (tevent_req_nomem(state->groups, req)) {
		return tevent_req_post(req, ev);
	}

	state->members = talloc_array(state, struct talloc_dict *,
				      state->max_groups);
	if (tevent_req_nomem(state->members, req)) {
		TALLOC_FREE(state->groups);
		return tevent_req_post(req, ev);
	}

	subreq = wb_next_grent_send(state, ev, lp_winbind_expand_groups(),
				    cli->grent_state,
				    &state->groups[state->num_groups]);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
	return req;
}

static void winbindd_getgrent_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct winbindd_getgrent_state *state = tevent_req_data(
		req, struct winbindd_getgrent_state);
	NTSTATUS status;

	status = wb_next_grent_recv(subreq, state,
				    &state->members[state->num_groups]);
	TALLOC_FREE(subreq);
	if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
		DEBUG(10, ("winbindd_getgrent_done: done with %d groups\n",
			   (int)state->num_groups));
		TALLOC_FREE(state->cli->grent_state);
		tevent_req_done(req);
		return;
	}
	if (!NT_STATUS_IS_OK(status)) {
		tevent_req_nterror(req, status);
		return;
	}
	state->num_groups += 1;
	if (state->num_groups >= state->max_groups) {
		DEBUG(10, ("winbindd_getgrent_done: Got enough groups: %d\n",
			   (int)state->num_groups));
		tevent_req_done(req);
		return;
	}
	if (state->cli->grent_state == NULL) {
		DEBUG(10, ("winbindd_getgrent_done: endgrent called in "
			   "between\n"));
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return;
	}
	subreq = wb_next_grent_send(state, state->ev,
				    lp_winbind_expand_groups(),
				    state->cli->grent_state,
				    &state->groups[state->num_groups]);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
}

NTSTATUS winbindd_getgrent_recv(struct tevent_req *req,
				struct winbindd_response *response)
{
	struct winbindd_getgrent_state *state = tevent_req_data(
		req, struct winbindd_getgrent_state);
	NTSTATUS status;
	char **memberstrings;
	char *result;
	size_t base_memberofs, total_memberlen;
	int i;

	if (tevent_req_is_nterror(req, &status)) {
		DEBUG(5, ("getgrent failed: %s\n", nt_errstr(status)));
		return status;
	}

	if (state->num_groups == 0) {
		return NT_STATUS_NO_MORE_ENTRIES;
	}

	memberstrings = talloc_array(talloc_tos(), char *, state->num_groups);
	if (memberstrings == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	total_memberlen = 0;

	for (i=0; i<state->num_groups; i++) {
		int num_members;

		status = winbindd_print_groupmembers(
			state->members[i], memberstrings, &num_members,
			&memberstrings[i]);

		if (!NT_STATUS_IS_OK(status)) {
			TALLOC_FREE(memberstrings);
			return status;
		}
		TALLOC_FREE(state->members[i]);

		state->groups[i].num_gr_mem = num_members;
		state->groups[i].gr_mem_ofs = total_memberlen;

		total_memberlen += talloc_get_size(memberstrings[i]);
	}

	base_memberofs = state->num_groups * sizeof(struct winbindd_gr);

	result = talloc_realloc(state, state->groups, char,
				base_memberofs + total_memberlen);
	if (result == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	state->groups = (struct winbindd_gr *)result;

	for (i=0; i<state->num_groups; i++) {
		memcpy(result + base_memberofs + state->groups[i].gr_mem_ofs,
		       memberstrings[i], talloc_get_size(memberstrings[i]));
		TALLOC_FREE(memberstrings[i]);
	}

	TALLOC_FREE(memberstrings);

	response->data.num_entries = state->num_groups;
	response->length += talloc_get_size(result);
	response->extra_data.data = talloc_move(response, &result);
	return NT_STATUS_OK;
}