third_party/heimdal: Import lorikeet-heimdal-202407041740 (commit 42ba2a6e5dd1bc14a8b...
[Samba.git] / source3 / winbindd / winbindd_getpwent.c
blob5a788831e12a1f47de36b5b0ad641d7442901665
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 uint32_t max_users;
27 uint32_t 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 D_NOTICE("[%s (%u)] Winbind external command GETPWENT start.\n"
51 "The caller (%s) provided room for %d entries.\n",
52 cli->client_name,
53 (unsigned int)cli->pid,
54 cli->client_name,
55 request->data.num_entries);
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 D_DEBUG("winbindd_getpwent_done: done with %"PRIu32" users\n",
95 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 D_DEBUG("winbindd_getpwent_done: Got enough users: %"PRIu32"\n",
106 state->num_users);
107 tevent_req_done(req);
108 return;
110 if (state->cli->pwent_state == NULL) {
111 D_DEBUG("winbindd_getpwent_done: endpwent called in between\n");
112 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
113 return;
115 subreq = wb_next_pwent_send(state, state->ev, state->cli->pwent_state,
116 &state->users[state->num_users]);
117 if (tevent_req_nomem(subreq, req)) {
118 return;
120 tevent_req_set_callback(subreq, winbindd_getpwent_done, req);
123 NTSTATUS winbindd_getpwent_recv(struct tevent_req *req,
124 struct winbindd_response *response)
126 struct winbindd_getpwent_state *state = tevent_req_data(
127 req, struct winbindd_getpwent_state);
128 NTSTATUS status;
129 uint32_t i;
131 if (tevent_req_is_nterror(req, &status)) {
132 TALLOC_FREE(state->cli->pwent_state);
133 D_WARNING("getpwent failed: %s\n", nt_errstr(status));
134 return status;
137 D_NOTICE("Winbind external command GETPWENT end.\n"
138 "Received %"PRIu32" entries.\n"
139 "(name:passwd:uid:gid:gecos:dir:shell)\n",
140 state->num_users);
142 if (state->num_users == 0) {
143 return NT_STATUS_NO_MORE_ENTRIES;
146 for (i = 0; i < state->num_users; i++) {
147 D_NOTICE("%"PRIu32": %s:%s:%u:%u:%s:%s:%s\n",
149 state->users[i].pw_name,
150 state->users[i].pw_passwd,
151 (unsigned int)state->users[i].pw_uid,
152 (unsigned int)state->users[i].pw_gid,
153 state->users[i].pw_gecos,
154 state->users[i].pw_dir,
155 state->users[i].pw_shell
158 response->data.num_entries = state->num_users;
159 response->extra_data.data = talloc_move(response, &state->users);
160 response->length += state->num_users * sizeof(struct winbindd_pw);
161 return NT_STATUS_OK;