s4:registry/util - Don't include the trailing '\0' in the internal data format but...
[Samba/ekacnet.git] / source3 / winbindd / winbindd_getpwent.c
blobef905b48bb2fc71eeb510685ff7343fb10b5bbf0
1 /*
2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_GETPWENT
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "winbindd.h"
23 struct winbindd_getpwent_state {
24 struct tevent_context *ev;
25 struct winbindd_cli_state *cli;
26 int max_users;
27 int num_users;
28 struct winbindd_pw *users;
31 static void winbindd_getpwent_done(struct tevent_req *subreq);
33 struct tevent_req *winbindd_getpwent_send(TALLOC_CTX *mem_ctx,
34 struct tevent_context *ev,
35 struct winbindd_cli_state *cli,
36 struct winbindd_request *request)
38 struct tevent_req *req, *subreq;
39 struct winbindd_getpwent_state *state;
41 req = tevent_req_create(mem_ctx, &state,
42 struct winbindd_getpwent_state);
43 if (req == NULL) {
44 return NULL;
46 state->ev = ev;
47 state->num_users = 0;
48 state->cli = cli;
50 DEBUG(3, ("[%5lu]: getpwent\n", (unsigned long)cli->pid));
52 if (!lp_winbind_enum_users()) {
53 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
54 return tevent_req_post(req, ev);
57 if (cli->pwent_state == NULL) {
58 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
59 return tevent_req_post(req, ev);
62 state->max_users = MIN(500, request->data.num_entries);
63 if (state->max_users == 0) {
64 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
65 return tevent_req_post(req, ev);
68 state->users = talloc_zero_array(state, struct winbindd_pw,
69 state->max_users);
70 if (tevent_req_nomem(state->users, req)) {
71 return tevent_req_post(req, ev);
74 subreq = wb_next_pwent_send(state, ev, cli->pwent_state,
75 &state->users[state->num_users]);
76 if (tevent_req_nomem(subreq, req)) {
77 return tevent_req_post(req, ev);
79 tevent_req_set_callback(subreq, winbindd_getpwent_done, req);
80 return req;
83 static void winbindd_getpwent_done(struct tevent_req *subreq)
85 struct tevent_req *req = tevent_req_callback_data(
86 subreq, struct tevent_req);
87 struct winbindd_getpwent_state *state = tevent_req_data(
88 req, struct winbindd_getpwent_state);
89 NTSTATUS status;
91 status = wb_next_pwent_recv(subreq);
92 TALLOC_FREE(subreq);
93 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
94 DEBUG(10, ("winbindd_getpwent_done: done with %d users\n",
95 (int)state->num_users));
96 TALLOC_FREE(state->cli->pwent_state);
97 tevent_req_done(req);
98 return;
100 if (!NT_STATUS_IS_OK(status)) {
101 tevent_req_nterror(req, status);
102 return;
104 state->num_users += 1;
105 if (state->num_users >= state->max_users) {
106 DEBUG(10, ("winbindd_getpwent_done: Got enough users: %d\n",
107 (int)state->num_users));
108 tevent_req_done(req);
109 return;
111 if (state->cli->pwent_state == NULL) {
112 DEBUG(10, ("winbindd_getpwent_done: endpwent called in "
113 "between\n"));
114 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
115 return;
117 subreq = wb_next_pwent_send(state, state->ev, state->cli->pwent_state,
118 &state->users[state->num_users]);
119 if (tevent_req_nomem(subreq, req)) {
120 return;
122 tevent_req_set_callback(subreq, winbindd_getpwent_done, req);
125 NTSTATUS winbindd_getpwent_recv(struct tevent_req *req,
126 struct winbindd_response *response)
128 struct winbindd_getpwent_state *state = tevent_req_data(
129 req, struct winbindd_getpwent_state);
130 NTSTATUS status;
132 if (tevent_req_is_nterror(req, &status)) {
133 DEBUG(5, ("getpwent failed: %s\n", nt_errstr(status)));
134 return status;
137 if (state->num_users == 0) {
138 return NT_STATUS_NO_MORE_ENTRIES;
141 response->data.num_entries = state->num_users;
142 response->extra_data.data = talloc_move(response, &state->users);
143 response->length += state->num_users * sizeof(struct winbindd_pw);
144 return NT_STATUS_OK;