Fix bug #6837 - "Too many open files" when trying to access large number of files
[Samba/gbeck.git] / source4 / winbind / wb_cmd_getpwent.c
blob45b966377adf1882c49e0a351d0d8dc0077d5cab
1 /*
2 Unix SMB/CIFS implementation.
4 Command backend for getpwent
6 Copyright (C) Kai Blin 2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "libcli/composite/composite.h"
24 #include "winbind/wb_server.h"
25 #include "smbd/service_task.h"
27 struct cmd_getpwent_state {
28 struct composite_context *ctx;
29 struct wbsrv_service *service;
31 struct wbsrv_pwent *pwent;
32 uint32_t max_users;
34 uint32_t num_users;
35 struct winbindd_pw *result;
38 static void cmd_getpwent_recv_pwnam(struct composite_context *ctx);
39 #if 0 /*FIXME: implement this*/
40 static void cmd_getpwent_recv_user_list(struct composite_context *ctx);
41 #endif
43 struct composite_context *wb_cmd_getpwent_send(TALLOC_CTX *mem_ctx,
44 struct wbsrv_service *service, struct wbsrv_pwent *pwent,
45 uint32_t max_users)
47 struct composite_context *ctx, *result;
48 struct cmd_getpwent_state *state;
50 DEBUG(5, ("wb_cmd_getpwent_send called\n"));
52 result = composite_create(mem_ctx, service->task->event_ctx);
53 if (!result) return NULL;
55 state = talloc(mem_ctx, struct cmd_getpwent_state);
56 if (composite_nomem(state, result)) return result;
58 state->ctx = result;
59 result->private_data = state;
60 state->service = service;
61 state->pwent = pwent;
62 state->max_users = max_users;
63 state->num_users = 0;
65 /* If there are users left in the libnet_UserList and we're below the
66 * maximum number of users to get per winbind getpwent call, use
67 * getpwnam to get the winbindd_pw struct */
68 if (pwent->page_index < pwent->user_list->out.count) {
69 int idx = pwent->page_index;
70 char *username = talloc_strdup(state,
71 pwent->user_list->out.users[idx].username);
73 pwent->page_index++;
74 ctx = wb_cmd_getpwnam_send(state, service, username);
75 if (composite_nomem(ctx, state->ctx)) return result;
77 composite_continue(state->ctx, ctx, cmd_getpwent_recv_pwnam,
78 state);
79 } else {
80 /* If there is no valid user left, call libnet_UserList to get a new
81 * list of users. */
82 composite_error(state->ctx, NT_STATUS_NO_MORE_ENTRIES);
84 return result;
87 static void cmd_getpwent_recv_pwnam(struct composite_context *ctx)
89 struct cmd_getpwent_state *state =
90 talloc_get_type(ctx->async.private_data,
91 struct cmd_getpwent_state);
92 struct winbindd_pw *pw;
94 DEBUG(5, ("cmd_getpwent_recv_pwnam called\n"));
96 state->ctx->status = wb_cmd_getpwnam_recv(ctx, state, &pw);
97 if (!composite_is_ok(state->ctx)) return;
99 /*FIXME: Cheat for now and only get one user per call */
100 state->result = pw;
102 composite_done(state->ctx);
105 NTSTATUS wb_cmd_getpwent_recv(struct composite_context *ctx,
106 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw,
107 uint32_t *num_users)
109 NTSTATUS status = composite_wait(ctx);
111 DEBUG(5, ("wb_cmd_getpwent_recv called\n"));
113 if (NT_STATUS_IS_OK(status)) {
114 struct cmd_getpwent_state *state =
115 talloc_get_type(ctx->private_data,
116 struct cmd_getpwent_state);
117 *pw = talloc_steal(mem_ctx, state->result);
118 /*FIXME: Cheat and only get oner user */
119 *num_users = 1;
122 talloc_free(ctx);
123 return status;