python/samba/tests: add test cases for s3/registry init funcs
[Samba.git] / source3 / winbindd / winbindd_getpwent.c
blob030a013c1609f06ba34eb54f85c113a6c0eec3d1
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 DBG_NOTICE("[%s (%u)] getpwent\n",
51 cli->client_name,
52 (unsigned int)cli->pid);
54 if (cli->pwent_state == NULL) {
55 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
56 return tevent_req_post(req, ev);
59 state->max_users = MIN(500, request->data.num_entries);
60 if (state->max_users == 0) {
61 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
62 return tevent_req_post(req, ev);
65 state->users = talloc_zero_array(state, struct winbindd_pw,
66 state->max_users);
67 if (tevent_req_nomem(state->users, req)) {
68 return tevent_req_post(req, ev);
71 subreq = wb_next_pwent_send(state, ev, cli->pwent_state,
72 &state->users[state->num_users]);
73 if (tevent_req_nomem(subreq, req)) {
74 return tevent_req_post(req, ev);
76 tevent_req_set_callback(subreq, winbindd_getpwent_done, req);
77 return req;
80 static void winbindd_getpwent_done(struct tevent_req *subreq)
82 struct tevent_req *req = tevent_req_callback_data(
83 subreq, struct tevent_req);
84 struct winbindd_getpwent_state *state = tevent_req_data(
85 req, struct winbindd_getpwent_state);
86 NTSTATUS status;
88 status = wb_next_pwent_recv(subreq);
89 TALLOC_FREE(subreq);
90 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
91 DEBUG(10, ("winbindd_getpwent_done: done with %d users\n",
92 (int)state->num_users));
93 TALLOC_FREE(state->cli->pwent_state);
94 tevent_req_done(req);
95 return;
97 if (tevent_req_nterror(req, status)) {
98 return;
100 state->num_users += 1;
101 if (state->num_users >= state->max_users) {
102 DEBUG(10, ("winbindd_getpwent_done: Got enough users: %d\n",
103 (int)state->num_users));
104 tevent_req_done(req);
105 return;
107 if (state->cli->pwent_state == NULL) {
108 DEBUG(10, ("winbindd_getpwent_done: endpwent called in "
109 "between\n"));
110 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
111 return;
113 subreq = wb_next_pwent_send(state, state->ev, state->cli->pwent_state,
114 &state->users[state->num_users]);
115 if (tevent_req_nomem(subreq, req)) {
116 return;
118 tevent_req_set_callback(subreq, winbindd_getpwent_done, req);
121 NTSTATUS winbindd_getpwent_recv(struct tevent_req *req,
122 struct winbindd_response *response)
124 struct winbindd_getpwent_state *state = tevent_req_data(
125 req, struct winbindd_getpwent_state);
126 NTSTATUS status;
128 if (tevent_req_is_nterror(req, &status)) {
129 TALLOC_FREE(state->cli->pwent_state);
130 DEBUG(5, ("getpwent failed: %s\n", nt_errstr(status)));
131 return status;
134 if (state->num_users == 0) {
135 return NT_STATUS_NO_MORE_ENTRIES;
138 response->data.num_entries = state->num_users;
139 response->extra_data.data = talloc_move(response, &state->users);
140 response->length += state->num_users * sizeof(struct winbindd_pw);
141 return NT_STATUS_OK;