s3-printing: rename queue->job sysjob
[Samba/id10ts.git] / source3 / winbindd / winbindd_getpwent.c
blob3c035eac8a6f7adebeaaa42dbc73cf126e5c9022
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 (tevent_req_nterror(req, status)) {
101 return;
103 state->num_users += 1;
104 if (state->num_users >= state->max_users) {
105 DEBUG(10, ("winbindd_getpwent_done: Got enough users: %d\n",
106 (int)state->num_users));
107 tevent_req_done(req);
108 return;
110 if (state->cli->pwent_state == NULL) {
111 DEBUG(10, ("winbindd_getpwent_done: endpwent called in "
112 "between\n"));
113 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
114 return;
116 subreq = wb_next_pwent_send(state, state->ev, state->cli->pwent_state,
117 &state->users[state->num_users]);
118 if (tevent_req_nomem(subreq, req)) {
119 return;
121 tevent_req_set_callback(subreq, winbindd_getpwent_done, req);
124 NTSTATUS winbindd_getpwent_recv(struct tevent_req *req,
125 struct winbindd_response *response)
127 struct winbindd_getpwent_state *state = tevent_req_data(
128 req, struct winbindd_getpwent_state);
129 NTSTATUS status;
131 if (tevent_req_is_nterror(req, &status)) {
132 DEBUG(5, ("getpwent failed: %s\n", nt_errstr(status)));
133 return status;
136 if (state->num_users == 0) {
137 return NT_STATUS_NO_MORE_ENTRIES;
140 response->data.num_entries = state->num_users;
141 response->extra_data.data = talloc_move(response, &state->users);
142 response->length += state->num_users * sizeof(struct winbindd_pw);
143 return NT_STATUS_OK;